xref: /linux/drivers/hwmon/lm90.c (revision bf3bd1f587f5a56ce60173324db27e5e50aa56b0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4  *          monitoring
5  * Copyright (C) 2003-2010  Jean Delvare <jdelvare@suse.de>
6  *
7  * Based on the lm83 driver. The LM90 is a sensor chip made by National
8  * Semiconductor. It reports up to two temperatures (its own plus up to
9  * one external one) with a 0.125 deg resolution (1 deg for local
10  * temperature) and a 3-4 deg accuracy.
11  *
12  * This driver also supports the LM89 and LM99, two other sensor chips
13  * made by National Semiconductor. Both have an increased remote
14  * temperature measurement accuracy (1 degree), and the LM99
15  * additionally shifts remote temperatures (measured and limits) by 16
16  * degrees, which allows for higher temperatures measurement.
17  * Note that there is no way to differentiate between both chips.
18  * When device is auto-detected, the driver will assume an LM99.
19  *
20  * This driver also supports the LM86, another sensor chip made by
21  * National Semiconductor. It is exactly similar to the LM90 except it
22  * has a higher accuracy.
23  *
24  * This driver also supports the ADM1032, a sensor chip made by Analog
25  * Devices. That chip is similar to the LM90, with a few differences
26  * that are not handled by this driver. Among others, it has a higher
27  * accuracy than the LM90, much like the LM86 does.
28  *
29  * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
30  * chips made by Maxim. These chips are similar to the LM86.
31  * Note that there is no easy way to differentiate between the three
32  * variants. We use the device address to detect MAX6659, which will result
33  * in a detection as max6657 if it is on address 0x4c. The extra address
34  * and features of the MAX6659 are only supported if the chip is configured
35  * explicitly as max6659, or if its address is not 0x4c.
36  * These chips lack the remote temperature offset feature.
37  *
38  * This driver also supports the MAX6654 chip made by Maxim. This chip can be
39  * at 9 different addresses, similar to MAX6680/MAX6681. The MAX6654 is similar
40  * to MAX6657/MAX6658/MAX6659, but does not support critical temperature
41  * limits. Extended range is available by setting the configuration register
42  * accordingly, and is done during initialization. Extended precision is only
43  * available at conversion rates of 1 Hz and slower. Note that extended
44  * precision is not enabled by default, as this driver initializes all chips
45  * to 2 Hz by design. The driver also supports MAX6690, which is practically
46  * identical to MAX6654.
47  *
48  * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
49  * MAX6692 chips made by Maxim.  These are again similar to the LM86,
50  * but they use unsigned temperature values and can report temperatures
51  * from 0 to 145 degrees.
52  *
53  * This driver also supports the MAX6680 and MAX6681, two other sensor
54  * chips made by Maxim. These are quite similar to the other Maxim
55  * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
56  * be treated identically.
57  *
58  * This driver also supports the MAX6695 and MAX6696, two other sensor
59  * chips made by Maxim. These are also quite similar to other Maxim
60  * chips, but support three temperature sensors instead of two. MAX6695
61  * and MAX6696 only differ in the pinout so they can be treated identically.
62  *
63  * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
64  * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
65  * and extended mode. They are mostly compatible with LM90 except for a data
66  * format difference for the temperature value registers.
67  *
68  * This driver also supports ADT7481, ADT7482, and ADT7483 from Analog Devices
69  * / ON Semiconductor. The chips are similar to ADT7461 but support two external
70  * temperature sensors.
71  *
72  * This driver also supports NCT72, NCT214, and NCT218 from ON Semiconductor.
73  * The chips are similar to ADT7461/ADT7461A but have full PEC support
74  * (undocumented).
75  *
76  * This driver also supports the SA56004 from Philips. This device is
77  * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
78  *
79  * This driver also supports the G781 from GMT. This device is compatible
80  * with the ADM1032.
81  *
82  * This driver also supports TMP451 and TMP461 from Texas Instruments.
83  * Those devices are supported in both compatibility and extended mode.
84  * They are mostly compatible with ADT7461 except for local temperature
85  * low byte register and max conversion rate.
86  *
87  * This driver also supports MAX1617 and various clones such as G767
88  * and NE1617. Such clones will be detected as MAX1617.
89  *
90  * This driver also supports NE1618 from Philips. It is similar to NE1617
91  * but supports 11 bit external temperature values.
92  *
93  * This driver also supports NCT7716, NCT7717 and NCT7718 from Nuvoton.
94  * The NCT7716 is similar to NCT7717 but has one more address support.
95  *
96  * Since the LM90 was the first chipset supported by this driver, most
97  * comments will refer to this chipset, but are actually general and
98  * concern all supported chipsets, unless mentioned otherwise.
99  */
100 
101 #include <linux/bits.h>
102 #include <linux/device.h>
103 #include <linux/err.h>
104 #include <linux/i2c.h>
105 #include <linux/init.h>
106 #include <linux/interrupt.h>
107 #include <linux/jiffies.h>
108 #include <linux/hwmon.h>
109 #include <linux/kstrtox.h>
110 #include <linux/module.h>
111 #include <linux/of.h>
112 #include <linux/regulator/consumer.h>
113 #include <linux/slab.h>
114 #include <linux/workqueue.h>
115 
116 /* The maximum number of channels currently supported */
117 #define MAX_CHANNELS	3
118 
119 /*
120  * Addresses to scan
121  * Address is fully defined internally and cannot be changed except for
122  * MAX6659, MAX6680 and MAX6681.
123  * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
124  * MAX6657, MAX6658, NCT1008, NCT7718 and W83L771 have address 0x4c.
125  * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
126  * have address 0x4d.
127  * MAX6647 has address 0x4e.
128  * MAX6659 can have address 0x4c, 0x4d or 0x4e.
129  * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
130  * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
131  * NCT7716 can have address 0x48 or 0x49.
132  * NCT7717 has address 0x48.
133  * SA56004 can have address 0x48 through 0x4F.
134  */
135 
136 static const unsigned short normal_i2c[] = {
137 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
138 	0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
139 
140 enum chips { adm1023, adm1032, adt7461, adt7461a, adt7481,
141 	g781, lm84, lm90, lm99,
142 	max1617, max6642, max6646, max6648, max6654, max6657, max6659, max6680, max6696,
143 	nct210, nct72, nct7716, nct7717, nct7718, ne1618, sa56004, tmp451, tmp461, w83l771,
144 };
145 
146 /*
147  * The LM90 registers
148  */
149 
150 #define LM90_REG_MAN_ID			0xFE
151 #define LM90_REG_CHIP_ID		0xFF
152 #define LM90_REG_CONFIG1		0x03
153 #define LM90_REG_CONFIG2		0xBF
154 #define LM90_REG_CONVRATE		0x04
155 #define LM90_REG_STATUS			0x02
156 #define LM90_REG_LOCAL_TEMP		0x00
157 #define LM90_REG_LOCAL_HIGH		0x05
158 #define LM90_REG_LOCAL_LOW		0x06
159 #define LM90_REG_LOCAL_CRIT		0x20
160 #define LM90_REG_REMOTE_TEMPH		0x01
161 #define LM90_REG_REMOTE_TEMPL		0x10
162 #define LM90_REG_REMOTE_OFFSH		0x11
163 #define LM90_REG_REMOTE_OFFSL		0x12
164 #define LM90_REG_REMOTE_HIGHH		0x07
165 #define LM90_REG_REMOTE_HIGHL		0x13
166 #define LM90_REG_REMOTE_LOWH		0x08
167 #define LM90_REG_REMOTE_LOWL		0x14
168 #define LM90_REG_REMOTE_CRIT		0x19
169 #define LM90_REG_TCRIT_HYST		0x21
170 
171 /* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
172 
173 #define MAX6657_REG_LOCAL_TEMPL		0x11
174 #define MAX6696_REG_STATUS2		0x12
175 #define MAX6659_REG_REMOTE_EMERG	0x16
176 #define MAX6659_REG_LOCAL_EMERG		0x17
177 
178 /*  SA56004 registers */
179 
180 #define SA56004_REG_LOCAL_TEMPL		0x22
181 
182 #define LM90_MAX_CONVRATE_MS	16000	/* Maximum conversion rate in ms */
183 
184 /* TMP451/TMP461 registers */
185 #define TMP451_REG_LOCAL_TEMPL		0x15
186 #define TMP451_REG_CONALERT		0x22
187 
188 #define TMP461_REG_CHEN			0x16
189 #define TMP461_REG_DFC			0x24
190 
191 /* ADT7481 registers */
192 #define ADT7481_REG_STATUS2		0x23
193 #define ADT7481_REG_CONFIG2		0x24
194 
195 #define ADT7481_REG_MAN_ID		0x3e
196 #define ADT7481_REG_CHIP_ID		0x3d
197 
198 /* NCT7716/7717/7718 registers */
199 #define NCT7716_REG_CHIP_ID		0xFD
200 
201 /* Device features */
202 #define LM90_HAVE_EXTENDED_TEMP	BIT(0)	/* extended temperature support	*/
203 #define LM90_HAVE_OFFSET	BIT(1)	/* temperature offset register	*/
204 #define LM90_HAVE_UNSIGNED_TEMP	BIT(2)	/* temperatures are unsigned	*/
205 #define LM90_HAVE_REM_LIMIT_EXT	BIT(3)	/* extended remote limit	*/
206 #define LM90_HAVE_EMERGENCY	BIT(4)	/* 3rd upper (emergency) limit	*/
207 #define LM90_HAVE_EMERGENCY_ALARM BIT(5)/* emergency alarm		*/
208 #define LM90_HAVE_TEMP3		BIT(6)	/* 3rd temperature sensor	*/
209 #define LM90_HAVE_BROKEN_ALERT	BIT(7)	/* Broken alert			*/
210 #define LM90_PAUSE_FOR_CONFIG	BIT(8)	/* Pause conversion for config	*/
211 #define LM90_HAVE_CRIT		BIT(9)	/* Chip supports CRIT/OVERT register	*/
212 #define LM90_HAVE_CRIT_ALRM_SWP	BIT(10)	/* critical alarm bits swapped	*/
213 #define LM90_HAVE_PEC		BIT(11)	/* Chip supports PEC		*/
214 #define LM90_HAVE_PARTIAL_PEC	BIT(12)	/* Partial PEC support (adm1032)*/
215 #define LM90_HAVE_ALARMS	BIT(13)	/* Create 'alarms' attribute	*/
216 #define LM90_HAVE_EXT_UNSIGNED	BIT(14)	/* extended unsigned temperature*/
217 #define LM90_HAVE_LOW		BIT(15)	/* low limits			*/
218 #define LM90_HAVE_CONVRATE	BIT(16)	/* conversion rate		*/
219 #define LM90_HAVE_REMOTE_EXT	BIT(17)	/* extended remote temperature	*/
220 #define LM90_HAVE_FAULTQUEUE	BIT(18)	/* configurable samples count	*/
221 
222 /* LM90 status */
223 #define LM90_STATUS_LTHRM	BIT(0)	/* local THERM limit tripped */
224 #define LM90_STATUS_RTHRM	BIT(1)	/* remote THERM limit tripped */
225 #define LM90_STATUS_ROPEN	BIT(2)	/* remote is an open circuit */
226 #define LM90_STATUS_RLOW	BIT(3)	/* remote low temp limit tripped */
227 #define LM90_STATUS_RHIGH	BIT(4)	/* remote high temp limit tripped */
228 #define LM90_STATUS_LLOW	BIT(5)	/* local low temp limit tripped */
229 #define LM90_STATUS_LHIGH	BIT(6)	/* local high temp limit tripped */
230 #define LM90_STATUS_BUSY	BIT(7)	/* conversion is ongoing */
231 
232 /* MAX6695/6696 and ADT7481 2nd status register */
233 #define MAX6696_STATUS2_R2THRM	BIT(1)	/* remote2 THERM limit tripped */
234 #define MAX6696_STATUS2_R2OPEN	BIT(2)	/* remote2 is an open circuit */
235 #define MAX6696_STATUS2_R2LOW	BIT(3)	/* remote2 low temp limit tripped */
236 #define MAX6696_STATUS2_R2HIGH	BIT(4)	/* remote2 high temp limit tripped */
237 #define MAX6696_STATUS2_ROT2	BIT(5)	/* remote emergency limit tripped */
238 #define MAX6696_STATUS2_R2OT2	BIT(6)	/* remote2 emergency limit tripped */
239 #define MAX6696_STATUS2_LOT2	BIT(7)	/* local emergency limit tripped */
240 
241 /*
242  * Driver data (common to all clients)
243  */
244 
245 static const struct i2c_device_id lm90_id[] = {
246 	{ .name = "adm1020", .driver_data = max1617 },
247 	{ .name = "adm1021", .driver_data = max1617 },
248 	{ .name = "adm1023", .driver_data = adm1023 },
249 	{ .name = "adm1032", .driver_data = adm1032 },
250 	{ .name = "adt7421", .driver_data = adt7461a },
251 	{ .name = "adt7461", .driver_data = adt7461 },
252 	{ .name = "adt7461a", .driver_data = adt7461a },
253 	{ .name = "adt7481", .driver_data = adt7481 },
254 	{ .name = "adt7482", .driver_data = adt7481 },
255 	{ .name = "adt7483a", .driver_data = adt7481 },
256 	{ .name = "g781", .driver_data = g781 },
257 	{ .name = "gl523sm", .driver_data = max1617 },
258 	{ .name = "lm84", .driver_data = lm84 },
259 	{ .name = "lm86", .driver_data = lm90 },
260 	{ .name = "lm89", .driver_data = lm90 },
261 	{ .name = "lm90", .driver_data = lm90 },
262 	{ .name = "lm99", .driver_data = lm99 },
263 	{ .name = "max1617", .driver_data = max1617 },
264 	{ .name = "max6642", .driver_data = max6642 },
265 	{ .name = "max6646", .driver_data = max6646 },
266 	{ .name = "max6647", .driver_data = max6646 },
267 	{ .name = "max6648", .driver_data = max6648 },
268 	{ .name = "max6649", .driver_data = max6646 },
269 	{ .name = "max6654", .driver_data = max6654 },
270 	{ .name = "max6657", .driver_data = max6657 },
271 	{ .name = "max6658", .driver_data = max6657 },
272 	{ .name = "max6659", .driver_data = max6659 },
273 	{ .name = "max6680", .driver_data = max6680 },
274 	{ .name = "max6681", .driver_data = max6680 },
275 	{ .name = "max6690", .driver_data = max6654 },
276 	{ .name = "max6692", .driver_data = max6648 },
277 	{ .name = "max6695", .driver_data = max6696 },
278 	{ .name = "max6696", .driver_data = max6696 },
279 	{ .name = "mc1066", .driver_data = max1617 },
280 	{ .name = "nct1008", .driver_data = adt7461a },
281 	{ .name = "nct210", .driver_data = nct210 },
282 	{ .name = "nct214", .driver_data = nct72 },
283 	{ .name = "nct218", .driver_data = nct72 },
284 	{ .name = "nct72", .driver_data = nct72 },
285 	{ .name = "nct7716", .driver_data = nct7716 },
286 	{ .name = "nct7717", .driver_data = nct7717 },
287 	{ .name = "nct7718", .driver_data = nct7718 },
288 	{ .name = "ne1618", .driver_data = ne1618 },
289 	{ .name = "w83l771", .driver_data = w83l771 },
290 	{ .name = "sa56004", .driver_data = sa56004 },
291 	{ .name = "thmc10", .driver_data = max1617 },
292 	{ .name = "tmp451", .driver_data = tmp451 },
293 	{ .name = "tmp461", .driver_data = tmp461 },
294 	{ }
295 };
296 MODULE_DEVICE_TABLE(i2c, lm90_id);
297 
298 static const struct of_device_id __maybe_unused lm90_of_match[] = {
299 	{
300 		.compatible = "adi,adm1032",
301 		.data = (void *)adm1032
302 	},
303 	{
304 		.compatible = "adi,adt7461",
305 		.data = (void *)adt7461
306 	},
307 	{
308 		.compatible = "adi,adt7461a",
309 		.data = (void *)adt7461a
310 	},
311 	{
312 		.compatible = "adi,adt7481",
313 		.data = (void *)adt7481
314 	},
315 	{
316 		.compatible = "gmt,g781",
317 		.data = (void *)g781
318 	},
319 	{
320 		.compatible = "national,lm90",
321 		.data = (void *)lm90
322 	},
323 	{
324 		.compatible = "national,lm86",
325 		.data = (void *)lm90
326 	},
327 	{
328 		.compatible = "national,lm89",
329 		.data = (void *)lm90
330 	},
331 	{
332 		.compatible = "national,lm99",
333 		.data = (void *)lm99
334 	},
335 	{
336 		.compatible = "dallas,max6646",
337 		.data = (void *)max6646
338 	},
339 	{
340 		.compatible = "dallas,max6647",
341 		.data = (void *)max6646
342 	},
343 	{
344 		.compatible = "dallas,max6649",
345 		.data = (void *)max6646
346 	},
347 	{
348 		.compatible = "dallas,max6654",
349 		.data = (void *)max6654
350 	},
351 	{
352 		.compatible = "dallas,max6657",
353 		.data = (void *)max6657
354 	},
355 	{
356 		.compatible = "dallas,max6658",
357 		.data = (void *)max6657
358 	},
359 	{
360 		.compatible = "dallas,max6659",
361 		.data = (void *)max6659
362 	},
363 	{
364 		.compatible = "dallas,max6680",
365 		.data = (void *)max6680
366 	},
367 	{
368 		.compatible = "dallas,max6681",
369 		.data = (void *)max6680
370 	},
371 	{
372 		.compatible = "dallas,max6695",
373 		.data = (void *)max6696
374 	},
375 	{
376 		.compatible = "dallas,max6696",
377 		.data = (void *)max6696
378 	},
379 	{
380 		.compatible = "onnn,nct1008",
381 		.data = (void *)adt7461a
382 	},
383 	{
384 		.compatible = "onnn,nct214",
385 		.data = (void *)nct72
386 	},
387 	{
388 		.compatible = "onnn,nct218",
389 		.data = (void *)nct72
390 	},
391 	{
392 		.compatible = "onnn,nct72",
393 		.data = (void *)nct72
394 	},
395 	{
396 		.compatible = "nuvoton,nct7716",
397 		.data = (void *)nct7716
398 	},
399 	{
400 		.compatible = "nuvoton,nct7717",
401 		.data = (void *)nct7717
402 	},
403 	{
404 		.compatible = "nuvoton,nct7718",
405 		.data = (void *)nct7718
406 	},
407 	{
408 		.compatible = "winbond,w83l771",
409 		.data = (void *)w83l771
410 	},
411 	{
412 		.compatible = "nxp,sa56004",
413 		.data = (void *)sa56004
414 	},
415 	{
416 		.compatible = "ti,tmp451",
417 		.data = (void *)tmp451
418 	},
419 	{
420 		.compatible = "ti,tmp461",
421 		.data = (void *)tmp461
422 	},
423 	{ },
424 };
425 MODULE_DEVICE_TABLE(of, lm90_of_match);
426 
427 /*
428  * chip type specific parameters
429  */
430 struct lm90_params {
431 	u32 flags;		/* Capabilities */
432 	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */
433 				/* Upper 8 bits for max6695/96 */
434 	u8 max_convrate;	/* Maximum conversion rate register value */
435 	u8 resolution;		/* 16-bit resolution (default 11 bit) */
436 	u8 reg_status2;		/* 2nd status register (optional) */
437 	u8 reg_local_ext;	/* Extended local temp register (optional) */
438 	u8 faultqueue_mask;	/* fault queue bit mask */
439 	u8 faultqueue_depth;	/* fault queue depth if mask is used */
440 };
441 
442 static const struct lm90_params lm90_params[] = {
443 	[adm1023] = {
444 		.flags = LM90_HAVE_ALARMS | LM90_HAVE_OFFSET | LM90_HAVE_BROKEN_ALERT
445 		  | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
446 		  | LM90_HAVE_REMOTE_EXT,
447 		.alert_alarms = 0x7c,
448 		.resolution = 8,
449 		.max_convrate = 7,
450 	},
451 	[adm1032] = {
452 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
453 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT
454 		  | LM90_HAVE_PARTIAL_PEC | LM90_HAVE_ALARMS
455 		  | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
456 		  | LM90_HAVE_FAULTQUEUE,
457 		.alert_alarms = 0x7c,
458 		.max_convrate = 10,
459 	},
460 	[adt7461] = {
461 		/*
462 		 * Standard temperature range is supposed to be unsigned,
463 		 * but that does not match reality. Negative temperatures
464 		 * are always reported.
465 		 */
466 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
467 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
468 		  | LM90_HAVE_CRIT | LM90_HAVE_PARTIAL_PEC
469 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
470 		  | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
471 		.alert_alarms = 0x7c,
472 		.max_convrate = 10,
473 		.resolution = 10,
474 	},
475 	[adt7461a] = {
476 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
477 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
478 		  | LM90_HAVE_CRIT | LM90_HAVE_PEC | LM90_HAVE_ALARMS
479 		  | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
480 		  | LM90_HAVE_FAULTQUEUE,
481 		.alert_alarms = 0x7c,
482 		.max_convrate = 10,
483 	},
484 	[adt7481] = {
485 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
486 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
487 		  | LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_PEC
488 		  | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT | LM90_HAVE_LOW
489 		  | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
490 		  | LM90_HAVE_FAULTQUEUE,
491 		.alert_alarms = 0x1c7c,
492 		.max_convrate = 11,
493 		.resolution = 10,
494 		.reg_status2 = ADT7481_REG_STATUS2,
495 	},
496 	[g781] = {
497 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
498 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT
499 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
500 		  | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
501 		.alert_alarms = 0x7c,
502 		.max_convrate = 7,
503 	},
504 	[lm84] = {
505 		.flags = LM90_HAVE_ALARMS,
506 		.resolution = 8,
507 	},
508 	[lm90] = {
509 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
510 		  | LM90_HAVE_CRIT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
511 		  | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
512 		  | LM90_HAVE_FAULTQUEUE,
513 		.alert_alarms = 0x7b,
514 		.max_convrate = 9,
515 		.faultqueue_mask = BIT(0),
516 		.faultqueue_depth = 3,
517 	},
518 	[lm99] = {
519 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
520 		  | LM90_HAVE_CRIT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
521 		  | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
522 		  | LM90_HAVE_FAULTQUEUE,
523 		.alert_alarms = 0x7b,
524 		.max_convrate = 9,
525 		.faultqueue_mask = BIT(0),
526 		.faultqueue_depth = 3,
527 	},
528 	[max1617] = {
529 		.flags = LM90_HAVE_CONVRATE | LM90_HAVE_BROKEN_ALERT |
530 		  LM90_HAVE_LOW | LM90_HAVE_ALARMS,
531 		.alert_alarms = 0x78,
532 		.resolution = 8,
533 		.max_convrate = 7,
534 	},
535 	[max6642] = {
536 		.flags = LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXT_UNSIGNED
537 		  | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
538 		.alert_alarms = 0x50,
539 		.resolution = 10,
540 		.reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
541 		.faultqueue_mask = BIT(4),
542 		.faultqueue_depth = 2,
543 	},
544 	[max6646] = {
545 		.flags = LM90_HAVE_CRIT | LM90_HAVE_BROKEN_ALERT
546 		  | LM90_HAVE_EXT_UNSIGNED | LM90_HAVE_ALARMS | LM90_HAVE_LOW
547 		  | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
548 		.alert_alarms = 0x7c,
549 		.max_convrate = 6,
550 		.reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
551 	},
552 	[max6648] = {
553 		.flags = LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_CRIT
554 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_LOW
555 		  | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
556 		.alert_alarms = 0x7c,
557 		.max_convrate = 6,
558 		.reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
559 	},
560 	[max6654] = {
561 		.flags = LM90_HAVE_BROKEN_ALERT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
562 		  | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
563 		.alert_alarms = 0x7c,
564 		.max_convrate = 7,
565 		.reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
566 	},
567 	[max6657] = {
568 		.flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_CRIT
569 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
570 		  | LM90_HAVE_REMOTE_EXT,
571 		.alert_alarms = 0x7c,
572 		.max_convrate = 8,
573 		.reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
574 	},
575 	[max6659] = {
576 		.flags = LM90_HAVE_EMERGENCY | LM90_HAVE_CRIT
577 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
578 		  | LM90_HAVE_REMOTE_EXT,
579 		.alert_alarms = 0x7c,
580 		.max_convrate = 8,
581 		.reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
582 	},
583 	[max6680] = {
584 		/*
585 		 * Apparent temperatures of 128 degrees C or higher are reported
586 		 * and treated as negative temperatures (meaning min_alarm will
587 		 * be set).
588 		 */
589 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_CRIT
590 		  | LM90_HAVE_CRIT_ALRM_SWP | LM90_HAVE_BROKEN_ALERT
591 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
592 		  | LM90_HAVE_REMOTE_EXT,
593 		.alert_alarms = 0x7c,
594 		.max_convrate = 7,
595 	},
596 	[max6696] = {
597 		.flags = LM90_HAVE_EMERGENCY
598 		  | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT
599 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
600 		  | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
601 		.alert_alarms = 0x1c7c,
602 		.max_convrate = 6,
603 		.reg_status2 = MAX6696_REG_STATUS2,
604 		.reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
605 		.faultqueue_mask = BIT(5),
606 		.faultqueue_depth = 4,
607 	},
608 	[nct72] = {
609 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
610 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
611 		  | LM90_HAVE_CRIT | LM90_HAVE_PEC | LM90_HAVE_UNSIGNED_TEMP
612 		  | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
613 		  | LM90_HAVE_FAULTQUEUE,
614 		.alert_alarms = 0x7c,
615 		.max_convrate = 10,
616 		.resolution = 10,
617 	},
618 	[nct210] = {
619 		.flags = LM90_HAVE_ALARMS | LM90_HAVE_BROKEN_ALERT
620 		  | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
621 		  | LM90_HAVE_REMOTE_EXT,
622 		.alert_alarms = 0x7c,
623 		.resolution = 11,
624 		.max_convrate = 7,
625 	},
626 	[nct7716] = {
627 		.flags = LM90_HAVE_ALARMS | LM90_HAVE_CONVRATE,
628 		.alert_alarms = 0x40,
629 		.resolution = 8,
630 		.max_convrate = 8,
631 	},
632 	[nct7717] = {
633 		.flags = LM90_HAVE_ALARMS | LM90_HAVE_CONVRATE,
634 		.alert_alarms = 0x40,
635 		.resolution = 8,
636 		.max_convrate = 8,
637 	},
638 	[nct7718] = {
639 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT
640 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
641 		  | LM90_HAVE_REMOTE_EXT,
642 		.alert_alarms = 0x7c,
643 		.resolution = 11,
644 		.max_convrate = 8,
645 	},
646 	[ne1618] = {
647 		.flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_BROKEN_ALERT
648 		  | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
649 		.alert_alarms = 0x7c,
650 		.resolution = 11,
651 		.max_convrate = 7,
652 	},
653 	[w83l771] = {
654 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT
655 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
656 		  | LM90_HAVE_REMOTE_EXT,
657 		.alert_alarms = 0x7c,
658 		.max_convrate = 8,
659 	},
660 	[sa56004] = {
661 		/*
662 		 * Apparent temperatures of 128 degrees C or higher are reported
663 		 * and treated as negative temperatures (meaning min_alarm will
664 		 * be set).
665 		 */
666 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT
667 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
668 		  | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
669 		.alert_alarms = 0x7b,
670 		.max_convrate = 9,
671 		.reg_local_ext = SA56004_REG_LOCAL_TEMPL,
672 		.faultqueue_mask = BIT(0),
673 		.faultqueue_depth = 3,
674 	},
675 	[tmp451] = {
676 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
677 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT
678 		  | LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_ALARMS | LM90_HAVE_LOW
679 		  | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
680 		.alert_alarms = 0x7c,
681 		.max_convrate = 9,
682 		.resolution = 12,
683 		.reg_local_ext = TMP451_REG_LOCAL_TEMPL,
684 	},
685 	[tmp461] = {
686 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
687 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT
688 		  | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
689 		  | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
690 		.alert_alarms = 0x7c,
691 		.max_convrate = 9,
692 		.resolution = 12,
693 		.reg_local_ext = TMP451_REG_LOCAL_TEMPL,
694 	},
695 };
696 
697 /*
698  * temperature register index
699  */
700 enum lm90_temp_reg_index {
701 	LOCAL_LOW = 0,
702 	LOCAL_HIGH,
703 	LOCAL_CRIT,
704 	REMOTE_CRIT,
705 	LOCAL_EMERG,	/* max6659 and max6695/96 */
706 	REMOTE_EMERG,	/* max6659 and max6695/96 */
707 	REMOTE2_CRIT,	/* max6695/96 only */
708 	REMOTE2_EMERG,	/* max6695/96 only */
709 
710 	REMOTE_TEMP,
711 	REMOTE_LOW,
712 	REMOTE_HIGH,
713 	REMOTE_OFFSET,	/* except max6646, max6657/58/59, and max6695/96 */
714 	LOCAL_TEMP,
715 	REMOTE2_TEMP,	/* max6695/96 only */
716 	REMOTE2_LOW,	/* max6695/96 only */
717 	REMOTE2_HIGH,	/* max6695/96 only */
718 	REMOTE2_OFFSET,
719 
720 	TEMP_REG_NUM
721 };
722 
723 /*
724  * Client data (each client gets its own)
725  */
726 
727 struct lm90_data {
728 	struct i2c_client *client;
729 	struct device *hwmon_dev;
730 	u32 chip_config[2];
731 	u32 channel_config[MAX_CHANNELS + 1];
732 	const char *channel_label[MAX_CHANNELS];
733 	struct hwmon_channel_info chip_info;
734 	struct hwmon_channel_info temp_info;
735 	const struct hwmon_channel_info *info[3];
736 	struct hwmon_chip_info chip;
737 	struct delayed_work alert_work;
738 	struct work_struct report_work;
739 	bool shutdown;		/* true if shutting down */
740 	bool valid;		/* true if register values are valid */
741 	bool alarms_valid;	/* true if status register values are valid */
742 	unsigned long last_updated; /* in jiffies */
743 	unsigned long alarms_updated; /* in jiffies */
744 	int kind;
745 	u32 flags;
746 
747 	unsigned int update_interval; /* in milliseconds */
748 
749 	u8 config;		/* Current configuration register value */
750 	u8 config_orig;		/* Original configuration register value */
751 	u8 convrate_orig;	/* Original conversion rate register value */
752 	u8 resolution;		/* temperature resolution in bit */
753 	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */
754 				/* Upper 8 bits for max6695/96 */
755 	u8 max_convrate;	/* Maximum conversion rate */
756 	u8 reg_status2;		/* 2nd status register (optional) */
757 	u8 reg_local_ext;	/* local extension register offset */
758 	u8 reg_remote_ext;	/* remote temperature low byte */
759 	u8 faultqueue_mask;	/* fault queue mask */
760 	u8 faultqueue_depth;	/* fault queue mask */
761 
762 	/* registers values */
763 	u16 temp[TEMP_REG_NUM];
764 	u8 temp_hyst;
765 	u8 conalert;
766 	u16 reported_alarms;	/* alarms reported as sysfs/udev events */
767 	u16 current_alarms;	/* current alarms, reported by chip */
768 	u16 alarms;		/* alarms not yet reported to user */
769 };
770 
771 /*
772  * Support functions
773  */
774 
775 /*
776  * If the chip supports PEC but not on write byte transactions, we need
777  * to explicitly ask for a transaction without PEC.
778  */
779 static inline s32 lm90_write_no_pec(struct i2c_client *client, u8 value)
780 {
781 	return i2c_smbus_xfer(client->adapter, client->addr,
782 			      client->flags & ~I2C_CLIENT_PEC,
783 			      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
784 }
785 
786 /*
787  * It is assumed that client->update_lock is held (unless we are in
788  * detection or initialization steps). This matters when PEC is enabled
789  * for chips with partial PEC support, because we don't want the address
790  * pointer to change between the write byte and the read byte transactions.
791  */
792 static int lm90_read_reg(struct i2c_client *client, u8 reg)
793 {
794 	struct lm90_data *data = i2c_get_clientdata(client);
795 	bool partial_pec = (client->flags & I2C_CLIENT_PEC) &&
796 			(data->flags & LM90_HAVE_PARTIAL_PEC);
797 	int err;
798 
799 	if (partial_pec) {
800 		err = lm90_write_no_pec(client, reg);
801 		if (err)
802 			return err;
803 		return i2c_smbus_read_byte(client);
804 	}
805 	return i2c_smbus_read_byte_data(client, reg);
806 }
807 
808 /*
809  * Return register write address
810  *
811  * The write address for registers 0x03 .. 0x08 is the read address plus 6.
812  * For other registers the write address matches the read address.
813  */
814 static u8 lm90_write_reg_addr(u8 reg)
815 {
816 	if (reg >= LM90_REG_CONFIG1 && reg <= LM90_REG_REMOTE_LOWH)
817 		return reg + 6;
818 	return reg;
819 }
820 
821 /*
822  * Write into LM90 register.
823  * Convert register address to write address if needed, then execute the
824  * operation.
825  */
826 static int lm90_write_reg(struct i2c_client *client, u8 reg, u8 val)
827 {
828 	return i2c_smbus_write_byte_data(client, lm90_write_reg_addr(reg), val);
829 }
830 
831 /*
832  * Write into 16-bit LM90 register.
833  * Convert register addresses to write address if needed, then execute the
834  * operation.
835  */
836 static int lm90_write16(struct i2c_client *client, u8 regh, u8 regl, u16 val)
837 {
838 	int ret;
839 
840 	ret = lm90_write_reg(client, regh, val >> 8);
841 	if (ret < 0 || !regl)
842 		return ret;
843 	return lm90_write_reg(client, regl, val & 0xff);
844 }
845 
846 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl,
847 		       bool is_volatile)
848 {
849 	int oldh, newh, l;
850 
851 	oldh = lm90_read_reg(client, regh);
852 	if (oldh < 0)
853 		return oldh;
854 
855 	if (!regl)
856 		return oldh << 8;
857 
858 	l = lm90_read_reg(client, regl);
859 	if (l < 0)
860 		return l;
861 
862 	if (!is_volatile)
863 		return (oldh << 8) | l;
864 
865 	/*
866 	 * For volatile registers we have to use a trick.
867 	 * We have to read two registers to have the sensor temperature,
868 	 * but we have to beware a conversion could occur between the
869 	 * readings. The datasheet says we should either use
870 	 * the one-shot conversion register, which we don't want to do
871 	 * (disables hardware monitoring) or monitor the busy bit, which is
872 	 * impossible (we can't read the values and monitor that bit at the
873 	 * exact same time). So the solution used here is to read the high
874 	 * the high byte again. If the new high byte matches the old one,
875 	 * then we have a valid reading. Otherwise we have to read the low
876 	 * byte again, and now we believe we have a correct reading.
877 	 */
878 	newh = lm90_read_reg(client, regh);
879 	if (newh < 0)
880 		return newh;
881 	if (oldh != newh) {
882 		l = lm90_read_reg(client, regl);
883 		if (l < 0)
884 			return l;
885 	}
886 	return (newh << 8) | l;
887 }
888 
889 static int lm90_update_confreg(struct lm90_data *data, u8 config)
890 {
891 	if (data->config != config) {
892 		int err;
893 
894 		err = lm90_write_reg(data->client, LM90_REG_CONFIG1, config);
895 		if (err)
896 			return err;
897 		data->config = config;
898 	}
899 	return 0;
900 }
901 
902 /*
903  * client->update_lock must be held when calling this function (unless we are
904  * in detection or initialization steps), and while a remote channel other
905  * than channel 0 is selected. Also, calling code must make sure to re-select
906  * external channel 0 before releasing the lock. This is necessary because
907  * various registers have different meanings as a result of selecting a
908  * non-default remote channel.
909  */
910 static int lm90_select_remote_channel(struct lm90_data *data, bool second)
911 {
912 	u8 config = data->config & ~0x08;
913 
914 	if (second)
915 		config |= 0x08;
916 
917 	return lm90_update_confreg(data, config);
918 }
919 
920 static int lm90_write_convrate(struct lm90_data *data, int val)
921 {
922 	u8 config = data->config;
923 	int err;
924 
925 	/* Save config and pause conversion */
926 	if (data->flags & LM90_PAUSE_FOR_CONFIG) {
927 		err = lm90_update_confreg(data, config | 0x40);
928 		if (err < 0)
929 			return err;
930 	}
931 
932 	/* Set conv rate */
933 	err = lm90_write_reg(data->client, LM90_REG_CONVRATE, val);
934 
935 	/* Revert change to config */
936 	lm90_update_confreg(data, config);
937 
938 	return err;
939 }
940 
941 /*
942  * Set conversion rate.
943  * client->update_lock must be held when calling this function (unless we are
944  * in detection or initialization steps).
945  */
946 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
947 			     unsigned int interval)
948 {
949 	unsigned int update_interval;
950 	int i, err;
951 
952 	/* Shift calculations to avoid rounding errors */
953 	interval <<= 6;
954 
955 	/* find the nearest update rate */
956 	for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
957 	     i < data->max_convrate; i++, update_interval >>= 1)
958 		if (interval >= update_interval * 3 / 4)
959 			break;
960 
961 	err = lm90_write_convrate(data, i);
962 	data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
963 	return err;
964 }
965 
966 static int lm90_set_faultqueue(struct i2c_client *client,
967 			       struct lm90_data *data, int val)
968 {
969 	int err;
970 
971 	if (data->faultqueue_mask) {
972 		err = lm90_update_confreg(data, val <= data->faultqueue_depth / 2 ?
973 					  data->config & ~data->faultqueue_mask :
974 					  data->config | data->faultqueue_mask);
975 	} else {
976 		static const u8 values[4] = {0, 2, 6, 0x0e};
977 
978 		data->conalert = (data->conalert & 0xf1) | values[val - 1];
979 		err = lm90_write_reg(data->client, TMP451_REG_CONALERT,
980 				     data->conalert);
981 	}
982 
983 	return err;
984 }
985 
986 static int lm90_update_limits(struct device *dev)
987 {
988 	struct lm90_data *data = dev_get_drvdata(dev);
989 	struct i2c_client *client = data->client;
990 	int val;
991 
992 	if (data->flags & LM90_HAVE_CRIT) {
993 		val = lm90_read_reg(client, LM90_REG_LOCAL_CRIT);
994 		if (val < 0)
995 			return val;
996 		data->temp[LOCAL_CRIT] = val << 8;
997 
998 		val = lm90_read_reg(client, LM90_REG_REMOTE_CRIT);
999 		if (val < 0)
1000 			return val;
1001 		data->temp[REMOTE_CRIT] = val << 8;
1002 
1003 		val = lm90_read_reg(client, LM90_REG_TCRIT_HYST);
1004 		if (val < 0)
1005 			return val;
1006 		data->temp_hyst = val;
1007 	}
1008 	if ((data->flags & LM90_HAVE_FAULTQUEUE) && !data->faultqueue_mask) {
1009 		val = lm90_read_reg(client, TMP451_REG_CONALERT);
1010 		if (val < 0)
1011 			return val;
1012 		data->conalert = val;
1013 	}
1014 
1015 	val = lm90_read16(client, LM90_REG_REMOTE_LOWH,
1016 			  (data->flags & LM90_HAVE_REM_LIMIT_EXT) ? LM90_REG_REMOTE_LOWL : 0,
1017 			  false);
1018 	if (val < 0)
1019 		return val;
1020 	data->temp[REMOTE_LOW] = val;
1021 
1022 	val = lm90_read16(client, LM90_REG_REMOTE_HIGHH,
1023 			  (data->flags & LM90_HAVE_REM_LIMIT_EXT) ? LM90_REG_REMOTE_HIGHL : 0,
1024 			  false);
1025 	if (val < 0)
1026 		return val;
1027 	data->temp[REMOTE_HIGH] = val;
1028 
1029 	if (data->flags & LM90_HAVE_OFFSET) {
1030 		val = lm90_read16(client, LM90_REG_REMOTE_OFFSH,
1031 				  LM90_REG_REMOTE_OFFSL, false);
1032 		if (val < 0)
1033 			return val;
1034 		data->temp[REMOTE_OFFSET] = val;
1035 	}
1036 
1037 	if (data->flags & LM90_HAVE_EMERGENCY) {
1038 		val = lm90_read_reg(client, MAX6659_REG_LOCAL_EMERG);
1039 		if (val < 0)
1040 			return val;
1041 		data->temp[LOCAL_EMERG] = val << 8;
1042 
1043 		val = lm90_read_reg(client, MAX6659_REG_REMOTE_EMERG);
1044 		if (val < 0)
1045 			return val;
1046 		data->temp[REMOTE_EMERG] = val << 8;
1047 	}
1048 
1049 	if (data->flags & LM90_HAVE_TEMP3) {
1050 		val = lm90_select_remote_channel(data, true);
1051 		if (val < 0)
1052 			return val;
1053 
1054 		val = lm90_read_reg(client, LM90_REG_REMOTE_CRIT);
1055 		if (val < 0)
1056 			return val;
1057 		data->temp[REMOTE2_CRIT] = val << 8;
1058 
1059 		if (data->flags & LM90_HAVE_EMERGENCY) {
1060 			val = lm90_read_reg(client, MAX6659_REG_REMOTE_EMERG);
1061 			if (val < 0)
1062 				return val;
1063 			data->temp[REMOTE2_EMERG] = val << 8;
1064 		}
1065 
1066 		val = lm90_read_reg(client, LM90_REG_REMOTE_LOWH);
1067 		if (val < 0)
1068 			return val;
1069 		data->temp[REMOTE2_LOW] = val << 8;
1070 
1071 		val = lm90_read_reg(client, LM90_REG_REMOTE_HIGHH);
1072 		if (val < 0)
1073 			return val;
1074 		data->temp[REMOTE2_HIGH] = val << 8;
1075 
1076 		if (data->flags & LM90_HAVE_OFFSET) {
1077 			val = lm90_read16(client, LM90_REG_REMOTE_OFFSH,
1078 					  LM90_REG_REMOTE_OFFSL, false);
1079 			if (val < 0)
1080 				return val;
1081 			data->temp[REMOTE2_OFFSET] = val;
1082 		}
1083 
1084 		lm90_select_remote_channel(data, false);
1085 	}
1086 
1087 	return 0;
1088 }
1089 
1090 static void lm90_report_alarms(struct work_struct *work)
1091 {
1092 	struct lm90_data *data = container_of(work, struct lm90_data, report_work);
1093 	u16 cleared_alarms, new_alarms, current_alarms;
1094 	struct device *hwmon_dev = data->hwmon_dev;
1095 	struct device *dev = &data->client->dev;
1096 	int st, st2;
1097 
1098 	current_alarms = data->current_alarms;
1099 	cleared_alarms = data->reported_alarms & ~current_alarms;
1100 	new_alarms = current_alarms & ~data->reported_alarms;
1101 
1102 	if (!cleared_alarms && !new_alarms)
1103 		return;
1104 
1105 	st = new_alarms & 0xff;
1106 	st2 = new_alarms >> 8;
1107 
1108 	if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1109 	    (st2 & MAX6696_STATUS2_LOT2))
1110 		dev_dbg(dev, "temp%d out of range, please check!\n", 1);
1111 	if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1112 	    (st2 & MAX6696_STATUS2_ROT2))
1113 		dev_dbg(dev, "temp%d out of range, please check!\n", 2);
1114 	if (st & LM90_STATUS_ROPEN)
1115 		dev_dbg(dev, "temp%d diode open, please check!\n", 2);
1116 	if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1117 		   MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1118 		dev_dbg(dev, "temp%d out of range, please check!\n", 3);
1119 	if (st2 & MAX6696_STATUS2_R2OPEN)
1120 		dev_dbg(dev, "temp%d diode open, please check!\n", 3);
1121 
1122 	st |= cleared_alarms & 0xff;
1123 	st2 |= cleared_alarms >> 8;
1124 
1125 	if (st & LM90_STATUS_LLOW)
1126 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 0);
1127 	if (st & LM90_STATUS_RLOW)
1128 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 1);
1129 	if (st2 & MAX6696_STATUS2_R2LOW)
1130 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 2);
1131 
1132 	if (st & LM90_STATUS_LHIGH)
1133 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 0);
1134 	if (st & LM90_STATUS_RHIGH)
1135 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 1);
1136 	if (st2 & MAX6696_STATUS2_R2HIGH)
1137 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 2);
1138 
1139 	if (st & LM90_STATUS_LTHRM)
1140 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 0);
1141 	if (st & LM90_STATUS_RTHRM)
1142 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 1);
1143 	if (st2 & MAX6696_STATUS2_R2THRM)
1144 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 2);
1145 
1146 	if (st2 & MAX6696_STATUS2_LOT2)
1147 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 0);
1148 	if (st2 & MAX6696_STATUS2_ROT2)
1149 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 1);
1150 	if (st2 & MAX6696_STATUS2_R2OT2)
1151 		hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 2);
1152 
1153 	data->reported_alarms = current_alarms;
1154 }
1155 
1156 static int lm90_update_alarms_locked(struct lm90_data *data, bool force)
1157 {
1158 	if (data->shutdown)
1159 		return 0;
1160 
1161 	if (force || !data->alarms_valid ||
1162 	    time_after(jiffies, data->alarms_updated + msecs_to_jiffies(data->update_interval))) {
1163 		struct i2c_client *client = data->client;
1164 		bool check_enable;
1165 		u16 alarms;
1166 		int val;
1167 
1168 		data->alarms_valid = false;
1169 
1170 		val = lm90_read_reg(client, LM90_REG_STATUS);
1171 		if (val < 0)
1172 			return val;
1173 		alarms = val & ~LM90_STATUS_BUSY;
1174 
1175 		if (data->reg_status2) {
1176 			val = lm90_read_reg(client, data->reg_status2);
1177 			if (val < 0)
1178 				return val;
1179 			alarms |= val << 8;
1180 		}
1181 		/*
1182 		 * If the update is forced (called from interrupt or alert
1183 		 * handler) and alarm data is valid, the alarms may have been
1184 		 * updated after the last update interval, and the status
1185 		 * register may still be cleared. Only add additional alarms
1186 		 * in this case. Alarms will be cleared later if appropriate.
1187 		 */
1188 		if (force && data->alarms_valid)
1189 			data->current_alarms |= alarms;
1190 		else
1191 			data->current_alarms = alarms;
1192 		data->alarms |= alarms;
1193 
1194 		check_enable = (client->irq || !(data->config_orig & 0x80)) &&
1195 			(data->config & 0x80);
1196 
1197 		if (force || check_enable)
1198 			schedule_work(&data->report_work);
1199 
1200 		/*
1201 		 * Re-enable ALERT# output if it was originally enabled, relevant
1202 		 * alarms are all clear, and alerts are currently disabled.
1203 		 * Otherwise (re)schedule worker if needed.
1204 		 */
1205 		if (check_enable) {
1206 			if (!(data->current_alarms & data->alert_alarms)) {
1207 				dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1208 				lm90_update_confreg(data, data->config & ~0x80);
1209 				/*
1210 				 * We may have been called from the update handler.
1211 				 * If so, the worker, if scheduled, is no longer
1212 				 * needed. Cancel it. Don't synchronize because
1213 				 * it may already be running.
1214 				 */
1215 				cancel_delayed_work(&data->alert_work);
1216 			} else {
1217 				schedule_delayed_work(&data->alert_work,
1218 					max_t(int, HZ, msecs_to_jiffies(data->update_interval)));
1219 			}
1220 		}
1221 		data->alarms_updated = jiffies;
1222 		data->alarms_valid = true;
1223 	}
1224 	return 0;
1225 }
1226 
1227 static int lm90_update_alarms(struct lm90_data *data, bool force)
1228 {
1229 	guard(hwmon_lock)(data->hwmon_dev);
1230 	return lm90_update_alarms_locked(data, force);
1231 }
1232 
1233 static void lm90_alert_work(struct work_struct *__work)
1234 {
1235 	struct delayed_work *delayed_work = to_delayed_work(__work);
1236 	struct lm90_data *data = container_of(delayed_work, struct lm90_data, alert_work);
1237 
1238 	/* Nothing to do if alerts are enabled */
1239 	if (!(data->config & 0x80))
1240 		return;
1241 
1242 	lm90_update_alarms(data, true);
1243 }
1244 
1245 static int lm90_update_device(struct device *dev)
1246 {
1247 	struct lm90_data *data = dev_get_drvdata(dev);
1248 	struct i2c_client *client = data->client;
1249 	unsigned long next_update;
1250 	int val;
1251 
1252 	if (!data->valid) {
1253 		val = lm90_update_limits(dev);
1254 		if (val < 0)
1255 			return val;
1256 	}
1257 
1258 	next_update = data->last_updated +
1259 		      msecs_to_jiffies(data->update_interval);
1260 	if (time_after(jiffies, next_update) || !data->valid) {
1261 		dev_dbg(&client->dev, "Updating lm90 data.\n");
1262 
1263 		data->valid = false;
1264 
1265 		val = lm90_read_reg(client, LM90_REG_LOCAL_LOW);
1266 		if (val < 0)
1267 			return val;
1268 		data->temp[LOCAL_LOW] = val << 8;
1269 
1270 		val = lm90_read_reg(client, LM90_REG_LOCAL_HIGH);
1271 		if (val < 0)
1272 			return val;
1273 		data->temp[LOCAL_HIGH] = val << 8;
1274 
1275 		val = lm90_read16(client, LM90_REG_LOCAL_TEMP,
1276 				  data->reg_local_ext, true);
1277 		if (val < 0)
1278 			return val;
1279 		data->temp[LOCAL_TEMP] = val;
1280 		val = lm90_read16(client, LM90_REG_REMOTE_TEMPH,
1281 				  data->reg_remote_ext, true);
1282 		if (val < 0)
1283 			return val;
1284 		data->temp[REMOTE_TEMP] = val;
1285 
1286 		if (data->flags & LM90_HAVE_TEMP3) {
1287 			val = lm90_select_remote_channel(data, true);
1288 			if (val < 0)
1289 				return val;
1290 
1291 			val = lm90_read16(client, LM90_REG_REMOTE_TEMPH,
1292 					  data->reg_remote_ext, true);
1293 			if (val < 0) {
1294 				lm90_select_remote_channel(data, false);
1295 				return val;
1296 			}
1297 			data->temp[REMOTE2_TEMP] = val;
1298 
1299 			lm90_select_remote_channel(data, false);
1300 		}
1301 
1302 		val = lm90_update_alarms_locked(data, false);
1303 		if (val < 0)
1304 			return val;
1305 
1306 		data->last_updated = jiffies;
1307 		data->valid = true;
1308 	}
1309 
1310 	return 0;
1311 }
1312 
1313 static int lm90_temp_get_resolution(struct lm90_data *data, int index)
1314 {
1315 	switch (index) {
1316 	case REMOTE_TEMP:
1317 		if (data->reg_remote_ext)
1318 			return data->resolution;
1319 		return 8;
1320 	case REMOTE_OFFSET:
1321 	case REMOTE2_OFFSET:
1322 	case REMOTE2_TEMP:
1323 		return data->resolution;
1324 	case LOCAL_TEMP:
1325 		if (data->reg_local_ext)
1326 			return data->resolution;
1327 		return 8;
1328 	case REMOTE_LOW:
1329 	case REMOTE_HIGH:
1330 	case REMOTE2_LOW:
1331 	case REMOTE2_HIGH:
1332 		if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1333 			return data->resolution;
1334 		return 8;
1335 	default:
1336 		return 8;
1337 	}
1338 }
1339 
1340 static int lm90_temp_from_reg(u32 flags, u16 regval, u8 resolution)
1341 {
1342 	int val;
1343 
1344 	if (flags & LM90_HAVE_EXTENDED_TEMP)
1345 		val = regval - 0x4000;
1346 	else if (flags & (LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_EXT_UNSIGNED))
1347 		val = regval;
1348 	else
1349 		val = (s16)regval;
1350 
1351 	return ((val >> (16 - resolution)) * 1000) >> (resolution - 8);
1352 }
1353 
1354 static int lm90_get_temp(struct lm90_data *data, int index, int channel)
1355 {
1356 	int temp = lm90_temp_from_reg(data->flags, data->temp[index],
1357 				      lm90_temp_get_resolution(data, index));
1358 
1359 	/* +16 degrees offset for remote temperature on LM99 */
1360 	if (data->kind == lm99 && channel)
1361 		temp += 16000;
1362 
1363 	return temp;
1364 }
1365 
1366 static u16 lm90_temp_to_reg(u32 flags, long val, u8 resolution)
1367 {
1368 	int fraction = resolution > 8 ?
1369 			1000 - DIV_ROUND_CLOSEST(1000, BIT(resolution - 8)) : 0;
1370 
1371 	if (flags & LM90_HAVE_EXTENDED_TEMP) {
1372 		val = clamp_val(val, -64000, 191000 + fraction);
1373 		val += 64000;
1374 	} else if (flags & LM90_HAVE_EXT_UNSIGNED) {
1375 		val = clamp_val(val, 0, 255000 + fraction);
1376 	} else if (flags & LM90_HAVE_UNSIGNED_TEMP) {
1377 		val = clamp_val(val, 0, 127000 + fraction);
1378 	} else {
1379 		val = clamp_val(val, -128000, 127000 + fraction);
1380 	}
1381 
1382 	return DIV_ROUND_CLOSEST(val << (resolution - 8), 1000) << (16 - resolution);
1383 }
1384 
1385 static int lm90_set_temp(struct lm90_data *data, int index, int channel, long val)
1386 {
1387 	static const u8 regs[] = {
1388 		[LOCAL_LOW] = LM90_REG_LOCAL_LOW,
1389 		[LOCAL_HIGH] = LM90_REG_LOCAL_HIGH,
1390 		[LOCAL_CRIT] = LM90_REG_LOCAL_CRIT,
1391 		[REMOTE_CRIT] = LM90_REG_REMOTE_CRIT,
1392 		[LOCAL_EMERG] = MAX6659_REG_LOCAL_EMERG,
1393 		[REMOTE_EMERG] = MAX6659_REG_REMOTE_EMERG,
1394 		[REMOTE2_CRIT] = LM90_REG_REMOTE_CRIT,
1395 		[REMOTE2_EMERG] = MAX6659_REG_REMOTE_EMERG,
1396 		[REMOTE_LOW] = LM90_REG_REMOTE_LOWH,
1397 		[REMOTE_HIGH] = LM90_REG_REMOTE_HIGHH,
1398 		[REMOTE2_LOW] = LM90_REG_REMOTE_LOWH,
1399 		[REMOTE2_HIGH] = LM90_REG_REMOTE_HIGHH,
1400 	};
1401 	struct i2c_client *client = data->client;
1402 	u8 regh = regs[index];
1403 	u8 regl = 0;
1404 	int err;
1405 
1406 	if (channel && (data->flags & LM90_HAVE_REM_LIMIT_EXT)) {
1407 		if (index == REMOTE_LOW || index == REMOTE2_LOW)
1408 			regl = LM90_REG_REMOTE_LOWL;
1409 		else if (index == REMOTE_HIGH || index == REMOTE2_HIGH)
1410 			regl = LM90_REG_REMOTE_HIGHL;
1411 	}
1412 
1413 	/* +16 degrees offset for remote temperature on LM99 */
1414 	if (data->kind == lm99 && channel) {
1415 		/* prevent integer underflow */
1416 		val = max(val, -128000l);
1417 		val -= 16000;
1418 	}
1419 
1420 	data->temp[index] = lm90_temp_to_reg(data->flags, val,
1421 					     lm90_temp_get_resolution(data, index));
1422 
1423 	if (channel > 1)
1424 		lm90_select_remote_channel(data, true);
1425 
1426 	err = lm90_write16(client, regh, regl, data->temp[index]);
1427 
1428 	if (channel > 1)
1429 		lm90_select_remote_channel(data, false);
1430 
1431 	return err;
1432 }
1433 
1434 static int lm90_get_temphyst(struct lm90_data *data, int index, int channel)
1435 {
1436 	int temp = lm90_get_temp(data, index, channel);
1437 
1438 	return temp - data->temp_hyst * 1000;
1439 }
1440 
1441 static int lm90_set_temphyst(struct lm90_data *data, long val)
1442 {
1443 	int temp = lm90_get_temp(data, LOCAL_CRIT, 0);
1444 
1445 	/* prevent integer overflow/underflow */
1446 	val = clamp_val(val, -128000l, 255000l);
1447 	data->temp_hyst = clamp_val(DIV_ROUND_CLOSEST(temp - val, 1000), 0, 31);
1448 
1449 	return lm90_write_reg(data->client, LM90_REG_TCRIT_HYST, data->temp_hyst);
1450 }
1451 
1452 static int lm90_get_temp_offset(struct lm90_data *data, int index)
1453 {
1454 	int res = lm90_temp_get_resolution(data, index);
1455 
1456 	return lm90_temp_from_reg(0, data->temp[index], res);
1457 }
1458 
1459 static int lm90_set_temp_offset(struct lm90_data *data, int index, int channel, long val)
1460 {
1461 	int err;
1462 
1463 	val = lm90_temp_to_reg(0, val, lm90_temp_get_resolution(data, index));
1464 
1465 	/* For ADT7481 we can use the same registers for remote channel 1 and 2 */
1466 	if (channel > 1)
1467 		lm90_select_remote_channel(data, true);
1468 
1469 	err = lm90_write16(data->client, LM90_REG_REMOTE_OFFSH, LM90_REG_REMOTE_OFFSL, val);
1470 
1471 	if (channel > 1)
1472 		lm90_select_remote_channel(data, false);
1473 
1474 	if (err)
1475 		return err;
1476 
1477 	data->temp[index] = val;
1478 
1479 	return 0;
1480 }
1481 
1482 static const u8 lm90_temp_index[MAX_CHANNELS] = {
1483 	LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1484 };
1485 
1486 static const u8 lm90_temp_min_index[MAX_CHANNELS] = {
1487 	LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1488 };
1489 
1490 static const u8 lm90_temp_max_index[MAX_CHANNELS] = {
1491 	LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1492 };
1493 
1494 static const u8 lm90_temp_crit_index[MAX_CHANNELS] = {
1495 	LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1496 };
1497 
1498 static const u8 lm90_temp_emerg_index[MAX_CHANNELS] = {
1499 	LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1500 };
1501 
1502 static const s8 lm90_temp_offset_index[MAX_CHANNELS] = {
1503 	-1, REMOTE_OFFSET, REMOTE2_OFFSET
1504 };
1505 
1506 static const u16 lm90_min_alarm_bits[MAX_CHANNELS] = { BIT(5), BIT(3), BIT(11) };
1507 static const u16 lm90_max_alarm_bits[MAX_CHANNELS] = { BIT(6), BIT(4), BIT(12) };
1508 static const u16 lm90_crit_alarm_bits[MAX_CHANNELS] = { BIT(0), BIT(1), BIT(9) };
1509 static const u16 lm90_crit_alarm_bits_swapped[MAX_CHANNELS] = { BIT(1), BIT(0), BIT(9) };
1510 static const u16 lm90_emergency_alarm_bits[MAX_CHANNELS] = { BIT(15), BIT(13), BIT(14) };
1511 static const u16 lm90_fault_bits[MAX_CHANNELS] = { BIT(0), BIT(2), BIT(10) };
1512 
1513 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1514 {
1515 	struct lm90_data *data = dev_get_drvdata(dev);
1516 	int err;
1517 	u16 bit;
1518 
1519 	err = lm90_update_device(dev);
1520 	if (err)
1521 		return err;
1522 
1523 	switch (attr) {
1524 	case hwmon_temp_input:
1525 		*val = lm90_get_temp(data, lm90_temp_index[channel], channel);
1526 		break;
1527 	case hwmon_temp_min_alarm:
1528 	case hwmon_temp_max_alarm:
1529 	case hwmon_temp_crit_alarm:
1530 	case hwmon_temp_emergency_alarm:
1531 	case hwmon_temp_fault:
1532 		switch (attr) {
1533 		case hwmon_temp_min_alarm:
1534 			bit = lm90_min_alarm_bits[channel];
1535 			break;
1536 		case hwmon_temp_max_alarm:
1537 			bit = lm90_max_alarm_bits[channel];
1538 			break;
1539 		case hwmon_temp_crit_alarm:
1540 			if (data->flags & LM90_HAVE_CRIT_ALRM_SWP)
1541 				bit = lm90_crit_alarm_bits_swapped[channel];
1542 			else
1543 				bit = lm90_crit_alarm_bits[channel];
1544 			break;
1545 		case hwmon_temp_emergency_alarm:
1546 			bit = lm90_emergency_alarm_bits[channel];
1547 			break;
1548 		case hwmon_temp_fault:
1549 			bit = lm90_fault_bits[channel];
1550 			break;
1551 		}
1552 		*val = !!(data->alarms & bit);
1553 		data->alarms &= ~bit;
1554 		data->alarms |= data->current_alarms;
1555 		break;
1556 	case hwmon_temp_min:
1557 		*val = lm90_get_temp(data, lm90_temp_min_index[channel], channel);
1558 		break;
1559 	case hwmon_temp_max:
1560 		*val = lm90_get_temp(data, lm90_temp_max_index[channel], channel);
1561 		break;
1562 	case hwmon_temp_crit:
1563 		*val = lm90_get_temp(data, lm90_temp_crit_index[channel], channel);
1564 		break;
1565 	case hwmon_temp_crit_hyst:
1566 		*val = lm90_get_temphyst(data, lm90_temp_crit_index[channel], channel);
1567 		break;
1568 	case hwmon_temp_emergency:
1569 		*val = lm90_get_temp(data, lm90_temp_emerg_index[channel], channel);
1570 		break;
1571 	case hwmon_temp_emergency_hyst:
1572 		*val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel], channel);
1573 		break;
1574 	case hwmon_temp_offset:
1575 		*val = lm90_get_temp_offset(data, lm90_temp_offset_index[channel]);
1576 		break;
1577 	default:
1578 		return -EOPNOTSUPP;
1579 	}
1580 	return 0;
1581 }
1582 
1583 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1584 {
1585 	struct lm90_data *data = dev_get_drvdata(dev);
1586 	int err;
1587 
1588 	err = lm90_update_device(dev);
1589 	if (err)
1590 		return err;
1591 
1592 	switch (attr) {
1593 	case hwmon_temp_min:
1594 		err = lm90_set_temp(data, lm90_temp_min_index[channel],
1595 				    channel, val);
1596 		break;
1597 	case hwmon_temp_max:
1598 		err = lm90_set_temp(data, lm90_temp_max_index[channel],
1599 				    channel, val);
1600 		break;
1601 	case hwmon_temp_crit:
1602 		err = lm90_set_temp(data, lm90_temp_crit_index[channel],
1603 				    channel, val);
1604 		break;
1605 	case hwmon_temp_crit_hyst:
1606 		err = lm90_set_temphyst(data, val);
1607 		break;
1608 	case hwmon_temp_emergency:
1609 		err = lm90_set_temp(data, lm90_temp_emerg_index[channel],
1610 				    channel, val);
1611 		break;
1612 	case hwmon_temp_offset:
1613 		err = lm90_set_temp_offset(data, lm90_temp_offset_index[channel],
1614 					   channel, val);
1615 		break;
1616 	default:
1617 		err = -EOPNOTSUPP;
1618 		break;
1619 	}
1620 	return err;
1621 }
1622 
1623 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1624 {
1625 	switch (attr) {
1626 	case hwmon_temp_input:
1627 	case hwmon_temp_min_alarm:
1628 	case hwmon_temp_max_alarm:
1629 	case hwmon_temp_crit_alarm:
1630 	case hwmon_temp_emergency_alarm:
1631 	case hwmon_temp_emergency_hyst:
1632 	case hwmon_temp_fault:
1633 	case hwmon_temp_label:
1634 		return 0444;
1635 	case hwmon_temp_min:
1636 	case hwmon_temp_max:
1637 	case hwmon_temp_crit:
1638 	case hwmon_temp_emergency:
1639 	case hwmon_temp_offset:
1640 		return 0644;
1641 	case hwmon_temp_crit_hyst:
1642 		if (channel == 0)
1643 			return 0644;
1644 		return 0444;
1645 	default:
1646 		return 0;
1647 	}
1648 }
1649 
1650 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1651 {
1652 	struct lm90_data *data = dev_get_drvdata(dev);
1653 	int err;
1654 
1655 	err = lm90_update_device(dev);
1656 	if (err)
1657 		return err;
1658 
1659 	switch (attr) {
1660 	case hwmon_chip_update_interval:
1661 		*val = data->update_interval;
1662 		break;
1663 	case hwmon_chip_alarms:
1664 		*val = data->alarms;
1665 		break;
1666 	case hwmon_chip_temp_samples:
1667 		if (data->faultqueue_mask) {
1668 			*val = (data->config & data->faultqueue_mask) ?
1669 				data->faultqueue_depth : 1;
1670 		} else {
1671 			switch (data->conalert & 0x0e) {
1672 			case 0x0:
1673 			default:
1674 				*val = 1;
1675 				break;
1676 			case 0x2:
1677 				*val = 2;
1678 				break;
1679 			case 0x6:
1680 				*val = 3;
1681 				break;
1682 			case 0xe:
1683 				*val = 4;
1684 				break;
1685 			}
1686 		}
1687 		break;
1688 	default:
1689 		return -EOPNOTSUPP;
1690 	}
1691 
1692 	return 0;
1693 }
1694 
1695 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1696 {
1697 	struct lm90_data *data = dev_get_drvdata(dev);
1698 	struct i2c_client *client = data->client;
1699 	int err;
1700 
1701 	err = lm90_update_device(dev);
1702 	if (err)
1703 		return err;
1704 
1705 	switch (attr) {
1706 	case hwmon_chip_update_interval:
1707 		err = lm90_set_convrate(client, data,
1708 					clamp_val(val, 0, 100000));
1709 		break;
1710 	case hwmon_chip_temp_samples:
1711 		err = lm90_set_faultqueue(client, data, clamp_val(val, 1, 4));
1712 		break;
1713 	default:
1714 		err = -EOPNOTSUPP;
1715 		break;
1716 	}
1717 	return err;
1718 }
1719 
1720 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1721 {
1722 	switch (attr) {
1723 	case hwmon_chip_update_interval:
1724 	case hwmon_chip_temp_samples:
1725 		return 0644;
1726 	case hwmon_chip_alarms:
1727 		return 0444;
1728 	default:
1729 		return 0;
1730 	}
1731 }
1732 
1733 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1734 		     u32 attr, int channel, long *val)
1735 {
1736 	switch (type) {
1737 	case hwmon_chip:
1738 		return lm90_chip_read(dev, attr, channel, val);
1739 	case hwmon_temp:
1740 		return lm90_temp_read(dev, attr, channel, val);
1741 	default:
1742 		return -EOPNOTSUPP;
1743 	}
1744 }
1745 
1746 static int lm90_read_string(struct device *dev, enum hwmon_sensor_types type,
1747 			    u32 attr, int channel, const char **str)
1748 {
1749 	struct lm90_data *data = dev_get_drvdata(dev);
1750 
1751 	*str = data->channel_label[channel];
1752 
1753 	return 0;
1754 }
1755 
1756 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1757 		      u32 attr, int channel, long val)
1758 {
1759 	switch (type) {
1760 	case hwmon_chip:
1761 		return lm90_chip_write(dev, attr, channel, val);
1762 	case hwmon_temp:
1763 		return lm90_temp_write(dev, attr, channel, val);
1764 	default:
1765 		return -EOPNOTSUPP;
1766 	}
1767 }
1768 
1769 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1770 			       u32 attr, int channel)
1771 {
1772 	switch (type) {
1773 	case hwmon_chip:
1774 		return lm90_chip_is_visible(data, attr, channel);
1775 	case hwmon_temp:
1776 		return lm90_temp_is_visible(data, attr, channel);
1777 	default:
1778 		return 0;
1779 	}
1780 }
1781 
1782 static const char *lm90_detect_lm84(struct i2c_client *client)
1783 {
1784 	static const u8 regs[] = {
1785 		LM90_REG_STATUS, LM90_REG_LOCAL_TEMP, LM90_REG_LOCAL_HIGH,
1786 		LM90_REG_REMOTE_TEMPH, LM90_REG_REMOTE_HIGHH
1787 	};
1788 	int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
1789 	int reg1, reg2, reg3, reg4;
1790 	bool nonzero = false;
1791 	u8 ff = 0xff;
1792 	int i;
1793 
1794 	if (status < 0 || (status & 0xab))
1795 		return NULL;
1796 
1797 	/*
1798 	 * For LM84, undefined registers return the most recent value.
1799 	 * Repeat several times, each time checking against a different
1800 	 * (presumably) existing register.
1801 	 */
1802 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
1803 		reg1 = i2c_smbus_read_byte_data(client, regs[i]);
1804 		reg2 = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL);
1805 		reg3 = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW);
1806 		reg4 = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH);
1807 
1808 		if (reg1 < 0)
1809 			return NULL;
1810 
1811 		/* If any register has a different value, this is not an LM84 */
1812 		if (reg2 != reg1 || reg3 != reg1 || reg4 != reg1)
1813 			return NULL;
1814 
1815 		nonzero |= reg1 || reg2 || reg3 || reg4;
1816 		ff &= reg1;
1817 	}
1818 	/*
1819 	 * If all registers always returned 0 or 0xff, all bets are off,
1820 	 * and we can not make any predictions about the chip type.
1821 	 */
1822 	return nonzero && ff != 0xff ? "lm84" : NULL;
1823 }
1824 
1825 static const char *lm90_detect_max1617(struct i2c_client *client, int config1)
1826 {
1827 	int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
1828 	int llo, rlo, lhi, rhi;
1829 
1830 	if (status < 0 || (status & 0x03))
1831 		return NULL;
1832 
1833 	if (config1 & 0x3f)
1834 		return NULL;
1835 
1836 	/*
1837 	 * Fail if unsupported registers return anything but 0xff.
1838 	 * The calling code already checked man_id and chip_id.
1839 	 * A byte read operation repeats the most recent read operation
1840 	 * and should also return 0xff.
1841 	 */
1842 	if (i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL) != 0xff ||
1843 	    i2c_smbus_read_byte_data(client, MAX6657_REG_LOCAL_TEMPL) != 0xff ||
1844 	    i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWL) != 0xff ||
1845 	    i2c_smbus_read_byte(client) != 0xff)
1846 		return NULL;
1847 
1848 	llo = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW);
1849 	rlo = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH);
1850 
1851 	lhi = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_HIGH);
1852 	rhi = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_HIGHH);
1853 
1854 	if (llo < 0 || rlo < 0)
1855 		return NULL;
1856 
1857 	/*
1858 	 * A byte read operation repeats the most recent read and should
1859 	 * return the same value.
1860 	 */
1861 	if (i2c_smbus_read_byte(client) != rhi)
1862 		return NULL;
1863 
1864 	/*
1865 	 * The following two checks are marginal since the checked values
1866 	 * are strictly speaking valid.
1867 	 */
1868 
1869 	/* fail for negative high limits; this also catches read errors */
1870 	if ((s8)lhi < 0 || (s8)rhi < 0)
1871 		return NULL;
1872 
1873 	/* fail if low limits are larger than or equal to high limits */
1874 	if ((s8)llo >= lhi || (s8)rlo >= rhi)
1875 		return NULL;
1876 
1877 	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
1878 		/*
1879 		 * Word read operations return 0xff in second byte
1880 		 */
1881 		if (i2c_smbus_read_word_data(client, LM90_REG_REMOTE_TEMPL) !=
1882 						0xffff)
1883 			return NULL;
1884 		if (i2c_smbus_read_word_data(client, LM90_REG_CONFIG1) !=
1885 						(config1 | 0xff00))
1886 			return NULL;
1887 		if (i2c_smbus_read_word_data(client, LM90_REG_LOCAL_HIGH) !=
1888 						(lhi | 0xff00))
1889 			return NULL;
1890 	}
1891 
1892 	return "max1617";
1893 }
1894 
1895 static const char *lm90_detect_national(struct i2c_client *client, int chip_id,
1896 					int config1, int convrate)
1897 {
1898 	int config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
1899 	int address = client->addr;
1900 	const char *name = NULL;
1901 
1902 	if (config2 < 0)
1903 		return NULL;
1904 
1905 	if ((config1 & 0x2a) || (config2 & 0xf8) || convrate > 0x09)
1906 		return NULL;
1907 
1908 	if (address != 0x4c && address != 0x4d)
1909 		return NULL;
1910 
1911 	switch (chip_id & 0xf0) {
1912 	case 0x10:	/* LM86 */
1913 		if (address == 0x4c)
1914 			name = "lm86";
1915 		break;
1916 	case 0x20:	/* LM90 */
1917 		if (address == 0x4c)
1918 			name = "lm90";
1919 		break;
1920 	case 0x30:	/* LM89/LM99 */
1921 		name = "lm99";	/* detect LM89 as LM99 */
1922 		break;
1923 	default:
1924 		break;
1925 	}
1926 
1927 	return name;
1928 }
1929 
1930 static const char *lm90_detect_on(struct i2c_client *client, int chip_id, int config1,
1931 				  int convrate)
1932 {
1933 	int address = client->addr;
1934 	const char *name = NULL;
1935 
1936 	switch (chip_id) {
1937 	case 0xca:		/* NCT218 */
1938 		if ((address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
1939 		    convrate <= 0x0a)
1940 			name = "nct218";
1941 		break;
1942 	default:
1943 		break;
1944 	}
1945 	return name;
1946 }
1947 
1948 static const char *lm90_detect_analog(struct i2c_client *client, bool common_address,
1949 				      int chip_id, int config1, int convrate)
1950 {
1951 	int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
1952 	int config2 = i2c_smbus_read_byte_data(client, ADT7481_REG_CONFIG2);
1953 	int man_id2 = i2c_smbus_read_byte_data(client, ADT7481_REG_MAN_ID);
1954 	int chip_id2 = i2c_smbus_read_byte_data(client, ADT7481_REG_CHIP_ID);
1955 	int address = client->addr;
1956 	const char *name = NULL;
1957 
1958 	if (status < 0 || config2 < 0 || man_id2 < 0 || chip_id2 < 0)
1959 		return NULL;
1960 
1961 	/*
1962 	 * The following chips should be detected by this function. Known
1963 	 * register values are listed. Registers 0x3d .. 0x3e are undocumented
1964 	 * for most of the chips, yet appear to return a well defined value.
1965 	 * Register 0xff is undocumented for some of the chips. Register 0x3f
1966 	 * is undocumented for all chips, but also returns a well defined value.
1967 	 * Values are as reported from real chips unless mentioned otherwise.
1968 	 * The code below checks values for registers 0x3d, 0x3e, and 0xff,
1969 	 * but not for register 0x3f.
1970 	 *
1971 	 * Chip			Register
1972 	 *		3d	3e	3f	fe	ff	Notes
1973 	 * ----------------------------------------------------------
1974 	 * adm1020	00	00	00	41	39
1975 	 * adm1021	00	00	00	41	03
1976 	 * adm1021a	00	00	00	41	3c
1977 	 * adm1023	00	00	00	41	3c	same as adm1021a
1978 	 * adm1032	00	00	00	41	42
1979 	 *
1980 	 * adt7421	21	41	04	41	04
1981 	 * adt7461	00	00	00	41	51
1982 	 * adt7461a	61	41	05	41	57
1983 	 * adt7481	81	41	02	41	62
1984 	 * adt7482	-	-	-	41	65	datasheet
1985 	 *		82	41	05	41	75	real chip
1986 	 * adt7483	83	41	04	41	94
1987 	 *
1988 	 * nct72	61	41	07	41	55
1989 	 * nct210	00	00	00	41	3f
1990 	 * nct214	61	41	08	41	5a
1991 	 * nct1008	-	-	-	41	57	datasheet rev. 3
1992 	 *		61	41	06	41	54	real chip
1993 	 *
1994 	 * nvt210	-	-	-	41	-	datasheet
1995 	 * nvt211	-	-	-	41	-	datasheet
1996 	 */
1997 	switch (chip_id) {
1998 	case 0x00 ... 0x03:	/* ADM1021 */
1999 	case 0x05 ... 0x0f:
2000 		if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
2001 		    !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
2002 			name = "adm1021";
2003 		break;
2004 	case 0x04:		/* ADT7421 (undocumented) */
2005 		if (man_id2 == 0x41 && chip_id2 == 0x21 &&
2006 		    (address == 0x4c || address == 0x4d) &&
2007 		    (config1 & 0x0b) == 0x08 && convrate <= 0x0a)
2008 			name = "adt7421";
2009 		break;
2010 	case 0x30 ... 0x38:	/* ADM1021A, ADM1023 */
2011 	case 0x3a ... 0x3e:
2012 		/*
2013 		 * ADM1021A and compatible chips will be mis-detected as
2014 		 * ADM1023. Chips labeled 'ADM1021A' and 'ADM1023' were both
2015 		 * found to have a Chip ID of 0x3c.
2016 		 * ADM1021A does not officially support low byte registers
2017 		 * (0x12 .. 0x14), but a chip labeled ADM1021A does support it.
2018 		 * Official support for the temperature offset high byte
2019 		 * register (0x11) was added to revision F of the ADM1021A
2020 		 * datasheet.
2021 		 * It is currently unknown if there is a means to distinguish
2022 		 * ADM1021A from ADM1023, and/or if revisions of ADM1021A exist
2023 		 * which differ in functionality from ADM1023.
2024 		 */
2025 		if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
2026 		    !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
2027 			name = "adm1023";
2028 		break;
2029 	case 0x39:		/* ADM1020 (undocumented) */
2030 		if (man_id2 == 0x00 && chip_id2 == 0x00 &&
2031 		    (address == 0x4c || address == 0x4d || address == 0x4e) &&
2032 		    !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
2033 			name = "adm1020";
2034 		break;
2035 	case 0x3f:		/* NCT210 */
2036 		if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
2037 		    !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
2038 			name = "nct210";
2039 		break;
2040 	case 0x40 ... 0x4f:	/* ADM1032 */
2041 		if (man_id2 == 0x00 && chip_id2 == 0x00 &&
2042 		    (address == 0x4c || address == 0x4d) && !(config1 & 0x3f) &&
2043 		    convrate <= 0x0a)
2044 			name = "adm1032";
2045 		break;
2046 	case 0x51:	/* ADT7461 */
2047 		if (man_id2 == 0x00 && chip_id2 == 0x00 &&
2048 		    (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
2049 		    convrate <= 0x0a)
2050 			name = "adt7461";
2051 		break;
2052 	case 0x54:	/* NCT1008 */
2053 		if (man_id2 == 0x41 && chip_id2 == 0x61 &&
2054 		    (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
2055 		    convrate <= 0x0a)
2056 			name = "nct1008";
2057 		break;
2058 	case 0x55:	/* NCT72 */
2059 		if (man_id2 == 0x41 && chip_id2 == 0x61 &&
2060 		    (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
2061 		    convrate <= 0x0a)
2062 			name = "nct72";
2063 		break;
2064 	case 0x57:	/* ADT7461A, NCT1008 (datasheet rev. 3) */
2065 		if (man_id2 == 0x41 && chip_id2 == 0x61 &&
2066 		    (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
2067 		    convrate <= 0x0a)
2068 			name = "adt7461a";
2069 		break;
2070 	case 0x5a:	/* NCT214 */
2071 		if (man_id2 == 0x41 && chip_id2 == 0x61 &&
2072 		    common_address && !(config1 & 0x1b) && convrate <= 0x0a)
2073 			name = "nct214";
2074 		break;
2075 	case 0x62:	/* ADT7481, undocumented */
2076 		if (man_id2 == 0x41 && chip_id2 == 0x81 &&
2077 		    (address == 0x4b || address == 0x4c) && !(config1 & 0x10) &&
2078 		    !(config2 & 0x7f) && (convrate & 0x0f) <= 0x0b) {
2079 			name = "adt7481";
2080 		}
2081 		break;
2082 	case 0x65:	/* ADT7482, datasheet */
2083 	case 0x75:	/* ADT7482, real chip */
2084 		if (man_id2 == 0x41 && chip_id2 == 0x82 &&
2085 		    address == 0x4c && !(config1 & 0x10) && !(config2 & 0x7f) &&
2086 		    convrate <= 0x0a)
2087 			name = "adt7482";
2088 		break;
2089 	case 0x94:	/* ADT7483 */
2090 		if (man_id2 == 0x41 && chip_id2 == 0x83 &&
2091 		    common_address &&
2092 		    ((address >= 0x18 && address <= 0x1a) ||
2093 		     (address >= 0x29 && address <= 0x2b) ||
2094 		     (address >= 0x4c && address <= 0x4e)) &&
2095 		    !(config1 & 0x10) && !(config2 & 0x7f) && convrate <= 0x0a)
2096 			name = "adt7483a";
2097 		break;
2098 	default:
2099 		break;
2100 	}
2101 
2102 	return name;
2103 }
2104 
2105 static const char *lm90_detect_maxim(struct i2c_client *client, bool common_address,
2106 				     int chip_id, int config1, int convrate)
2107 {
2108 	int man_id, emerg, emerg2, status2;
2109 	int address = client->addr;
2110 	const char *name = NULL;
2111 
2112 	switch (chip_id) {
2113 	case 0x01:
2114 		if (!common_address)
2115 			break;
2116 
2117 		/*
2118 		 * We read MAX6659_REG_REMOTE_EMERG twice, and re-read
2119 		 * LM90_REG_MAN_ID in between. If MAX6659_REG_REMOTE_EMERG
2120 		 * exists, both readings will reflect the same value. Otherwise,
2121 		 * the readings will be different.
2122 		 */
2123 		emerg = i2c_smbus_read_byte_data(client,
2124 						 MAX6659_REG_REMOTE_EMERG);
2125 		man_id = i2c_smbus_read_byte_data(client,
2126 						  LM90_REG_MAN_ID);
2127 		emerg2 = i2c_smbus_read_byte_data(client,
2128 						  MAX6659_REG_REMOTE_EMERG);
2129 		status2 = i2c_smbus_read_byte_data(client,
2130 						   MAX6696_REG_STATUS2);
2131 		if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
2132 			return NULL;
2133 
2134 		/*
2135 		 * Even though MAX6695 and MAX6696 do not have a chip ID
2136 		 * register, reading it returns 0x01. Bit 4 of the config1
2137 		 * register is unused and should return zero when read. Bit 0 of
2138 		 * the status2 register is unused and should return zero when
2139 		 * read.
2140 		 *
2141 		 * MAX6695 and MAX6696 have an additional set of temperature
2142 		 * limit registers. We can detect those chips by checking if
2143 		 * one of those registers exists.
2144 		 */
2145 		if (!(config1 & 0x10) && !(status2 & 0x01) && emerg == emerg2 &&
2146 		    convrate <= 0x07)
2147 			name = "max6696";
2148 		/*
2149 		 * The chip_id register of the MAX6680 and MAX6681 holds the
2150 		 * revision of the chip. The lowest bit of the config1 register
2151 		 * is unused and should return zero when read, so should the
2152 		 * second to last bit of config1 (software reset). Register
2153 		 * address 0x12 (LM90_REG_REMOTE_OFFSL) exists for this chip and
2154 		 * should differ from emerg2, and emerg2 should match man_id
2155 		 * since it does not exist.
2156 		 */
2157 		else if (!(config1 & 0x03) && convrate <= 0x07 &&
2158 			 emerg2 == man_id && emerg2 != status2)
2159 			name = "max6680";
2160 		/*
2161 		 * MAX1617A does not have any extended registers (register
2162 		 * address 0x10 or higher) except for manufacturer and
2163 		 * device ID registers. Unlike other chips of this series,
2164 		 * unsupported registers were observed to return a fixed value
2165 		 * of 0x01.
2166 		 * Note: Multiple chips with different markings labeled as
2167 		 * "MAX1617" (no "A") were observed to report manufacturer ID
2168 		 * 0x4d and device ID 0x01. It is unknown if other variants of
2169 		 * MAX1617/MAX617A with different behavior exist. The detection
2170 		 * code below works for those chips.
2171 		 */
2172 		else if (!(config1 & 0x03f) && convrate <= 0x07 &&
2173 			 emerg == 0x01 && emerg2 == 0x01 && status2 == 0x01)
2174 			name = "max1617";
2175 		break;
2176 	case 0x08:
2177 		/*
2178 		 * The chip_id of the MAX6654 holds the revision of the chip.
2179 		 * The lowest 3 bits of the config1 register are unused and
2180 		 * should return zero when read.
2181 		 */
2182 		if (common_address && !(config1 & 0x07) && convrate <= 0x07)
2183 			name = "max6654";
2184 		break;
2185 	case 0x09:
2186 		/*
2187 		 * The chip_id of the MAX6690 holds the revision of the chip.
2188 		 * The lowest 3 bits of the config1 register are unused and
2189 		 * should return zero when read.
2190 		 * Note that MAX6654 and MAX6690 are practically the same chips.
2191 		 * The only diference is the rated accuracy. Rev. 1 of the
2192 		 * MAX6690 datasheet lists a chip ID of 0x08, and a chip labeled
2193 		 * MAX6654 was observed to have a chip ID of 0x09.
2194 		 */
2195 		if (common_address && !(config1 & 0x07) && convrate <= 0x07)
2196 			name = "max6690";
2197 		break;
2198 	case 0x4d:
2199 		/*
2200 		 * MAX6642, MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
2201 		 * register. Reading from that address will return the last
2202 		 * read value, which in our case is those of the man_id
2203 		 * register, or 0x4d.
2204 		 * MAX6642 does not have a conversion rate register, nor low
2205 		 * limit registers. Reading from those registers returns the
2206 		 * last read value.
2207 		 *
2208 		 * For MAX6657, MAX6658 and MAX6659, the config1 register lacks
2209 		 * a low nibble, so the value will be those of the previous
2210 		 * read, so in our case again those of the man_id register.
2211 		 * MAX6659 has a third set of upper temperature limit registers.
2212 		 * Those registers also return values on MAX6657 and MAX6658,
2213 		 * thus the only way to detect MAX6659 is by its address.
2214 		 * For this reason it will be mis-detected as MAX6657 if its
2215 		 * address is 0x4c.
2216 		 */
2217 		if (address >= 0x48 && address <= 0x4f && config1 == convrate &&
2218 		    !(config1 & 0x0f)) {
2219 			int regval;
2220 
2221 			/*
2222 			 * We know that this is not a MAX6657/58/59 because its
2223 			 * configuration register has the wrong value and it does
2224 			 * not appear to have a conversion rate register.
2225 			 */
2226 
2227 			/* re-read manufacturer ID to have a good baseline */
2228 			if (i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID) != 0x4d)
2229 				break;
2230 
2231 			/* check various non-existing registers */
2232 			if (i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE) != 0x4d ||
2233 			    i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW) != 0x4d ||
2234 			    i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH) != 0x4d)
2235 				break;
2236 
2237 			/* check for unused status register bits */
2238 			regval = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
2239 			if (regval < 0 || (regval & 0x2b))
2240 				break;
2241 
2242 			/* re-check unsupported registers */
2243 			if (i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE) != regval ||
2244 			    i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW) != regval ||
2245 			    i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH) != regval)
2246 				break;
2247 
2248 			name = "max6642";
2249 		} else if ((address == 0x4c || address == 0x4d || address == 0x4e) &&
2250 			   (config1 & 0x1f) == 0x0d && convrate <= 0x09) {
2251 			if (address == 0x4c)
2252 				name = "max6657";
2253 			else
2254 				name = "max6659";
2255 		}
2256 		break;
2257 	case 0x59:
2258 		/*
2259 		 * The chip_id register of the MAX6646/6647/6649 holds the
2260 		 * revision of the chip. The lowest 6 bits of the config1
2261 		 * register are unused and should return zero when read.
2262 		 * The I2C address of MAX6648/6692 is fixed at 0x4c.
2263 		 * MAX6646 is at address 0x4d, MAX6647 is at address 0x4e,
2264 		 * and MAX6649 is at address 0x4c. A slight difference between
2265 		 * the two sets of chips is that the remote temperature register
2266 		 * reports different values if the DXP pin is open or shorted.
2267 		 * We can use that information to help distinguish between the
2268 		 * chips. MAX6648 will be mis-detected as MAX6649 if the remote
2269 		 * diode is connected, but there isn't really anything we can
2270 		 * do about that.
2271 		 */
2272 		if (!(config1 & 0x3f) && convrate <= 0x07) {
2273 			int temp;
2274 
2275 			switch (address) {
2276 			case 0x4c:
2277 				/*
2278 				 * MAX6649 reports an external temperature
2279 				 * value of 0xff if DXP is open or shorted.
2280 				 * MAX6648 reports 0x80 in that case.
2281 				 */
2282 				temp = i2c_smbus_read_byte_data(client,
2283 								LM90_REG_REMOTE_TEMPH);
2284 				if (temp == 0x80)
2285 					name = "max6648";
2286 				else
2287 					name = "max6649";
2288 				break;
2289 			case 0x4d:
2290 				name = "max6646";
2291 				break;
2292 			case 0x4e:
2293 				name = "max6647";
2294 				break;
2295 			default:
2296 				break;
2297 			}
2298 		}
2299 		break;
2300 	default:
2301 		break;
2302 	}
2303 
2304 	return name;
2305 }
2306 
2307 static const char *lm90_detect_nuvoton(struct i2c_client *client, int chip_id,
2308 				       int config1, int convrate)
2309 {
2310 	int config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
2311 	int address = client->addr;
2312 	const char *name = NULL;
2313 
2314 	if (config2 < 0)
2315 		return NULL;
2316 
2317 	if (address == 0x4c && !(config1 & 0x2a) && !(config2 & 0xf8)) {
2318 		if (chip_id == 0x01 && convrate <= 0x09) {
2319 			/* W83L771W/G */
2320 			name = "w83l771";
2321 		} else if ((chip_id & 0xfe) == 0x10 && convrate <= 0x08) {
2322 			/* W83L771AWG/ASG */
2323 			name = "w83l771";
2324 		}
2325 	}
2326 	return name;
2327 }
2328 
2329 static const char *lm90_detect_nuvoton_50(struct i2c_client *client, int chip_id,
2330 					  int config1, int convrate)
2331 {
2332 	int chip_id2 = i2c_smbus_read_byte_data(client, NCT7716_REG_CHIP_ID);
2333 	int config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
2334 	int address = client->addr;
2335 	const char *name = NULL;
2336 
2337 	if (chip_id2 < 0 || config2 < 0)
2338 		return NULL;
2339 
2340 	if (chip_id2 != 0x50 || convrate > 0x08)
2341 		return NULL;
2342 
2343 	switch (chip_id) {
2344 	case 0x90:
2345 		if (address == 0x48 && !(config1 & 0x3e) && !(config2 & 0xfe))
2346 			name = "nct7717";
2347 		break;
2348 	case 0x91:
2349 		if ((address == 0x48 || address == 0x49) && !(config1 & 0x3e) &&
2350 		    !(config2 & 0xfe))
2351 			name = "nct7716";
2352 		else if (address == 0x4c && !(config1 & 0x38) && !(config2 & 0xf8))
2353 			name = "nct7718";
2354 		break;
2355 	default:
2356 		break;
2357 	}
2358 	return name;
2359 }
2360 
2361 static const char *lm90_detect_nxp(struct i2c_client *client, bool common_address,
2362 				   int chip_id, int config1, int convrate)
2363 {
2364 	int address = client->addr;
2365 	const char *name = NULL;
2366 	int config2;
2367 
2368 	switch (chip_id) {
2369 	case 0x00:
2370 		config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
2371 		if (config2 < 0)
2372 			return NULL;
2373 		if (address >= 0x48 && address <= 0x4f &&
2374 		    !(config1 & 0x2a) && !(config2 & 0xfe) && convrate <= 0x09)
2375 			name = "sa56004";
2376 		break;
2377 	case 0x80:
2378 		if (common_address && !(config1 & 0x3f) && convrate <= 0x07)
2379 			name = "ne1618";
2380 		break;
2381 	default:
2382 		break;
2383 	}
2384 	return name;
2385 }
2386 
2387 static const char *lm90_detect_gmt(struct i2c_client *client, int chip_id,
2388 				   int config1, int convrate)
2389 {
2390 	int address = client->addr;
2391 
2392 	/*
2393 	 * According to the datasheet, G781 is supposed to be at I2C Address
2394 	 * 0x4c and have a chip ID of 0x01. G781-1 is supposed to be at I2C
2395 	 * address 0x4d and have a chip ID of 0x03. However, when support
2396 	 * for G781 was added, chips at 0x4c and 0x4d were found to have a
2397 	 * chip ID of 0x01. A G781-1 at I2C address 0x4d was now found with
2398 	 * chip ID 0x03.
2399 	 * To avoid detection failures, accept chip ID 0x01 and 0x03 at both
2400 	 * addresses.
2401 	 * G784 reports manufacturer ID 0x47 and chip ID 0x01. A public
2402 	 * datasheet is not available. Extensive testing suggests that
2403 	 * the chip appears to be fully compatible with G781.
2404 	 * Available register dumps show that G751 also reports manufacturer
2405 	 * ID 0x47 and chip ID 0x01 even though that chip does not officially
2406 	 * support those registers. This makes chip detection somewhat
2407 	 * vulnerable. To improve detection quality, read the offset low byte
2408 	 * and alert fault queue registers and verify that only expected bits
2409 	 * are set.
2410 	 */
2411 	if ((chip_id == 0x01 || chip_id == 0x03) &&
2412 	    (address == 0x4c || address == 0x4d) &&
2413 	    !(config1 & 0x3f) && convrate <= 0x08) {
2414 		int reg;
2415 
2416 		reg = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_OFFSL);
2417 		if (reg < 0 || reg & 0x1f)
2418 			return NULL;
2419 		reg = i2c_smbus_read_byte_data(client, TMP451_REG_CONALERT);
2420 		if (reg < 0 || reg & 0xf1)
2421 			return NULL;
2422 
2423 		return "g781";
2424 	}
2425 
2426 	return NULL;
2427 }
2428 
2429 static const char *lm90_detect_ti49(struct i2c_client *client, bool common_address,
2430 				    int chip_id, int config1, int convrate)
2431 {
2432 	if (common_address && chip_id == 0x00 && !(config1 & 0x3f) && !(convrate & 0xf8)) {
2433 		/* THMC10: Unsupported registers return 0xff */
2434 		if (i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL) == 0xff &&
2435 		    i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_CRIT) == 0xff)
2436 			return "thmc10";
2437 	}
2438 	return NULL;
2439 }
2440 
2441 static const char *lm90_detect_ti(struct i2c_client *client, int chip_id,
2442 				  int config1, int convrate)
2443 {
2444 	int address = client->addr;
2445 	const char *name = NULL;
2446 
2447 	if (chip_id == 0x00 && !(config1 & 0x1b) && convrate <= 0x09) {
2448 		int local_ext, conalert, chen, dfc;
2449 
2450 		local_ext = i2c_smbus_read_byte_data(client,
2451 						     TMP451_REG_LOCAL_TEMPL);
2452 		conalert = i2c_smbus_read_byte_data(client,
2453 						    TMP451_REG_CONALERT);
2454 		chen = i2c_smbus_read_byte_data(client, TMP461_REG_CHEN);
2455 		dfc = i2c_smbus_read_byte_data(client, TMP461_REG_DFC);
2456 
2457 		if (!(local_ext & 0x0f) && (conalert & 0xf1) == 0x01 &&
2458 		    (chen & 0xfc) == 0x00 && (dfc & 0xfc) == 0x00) {
2459 			if (address == 0x4c && !(chen & 0x03))
2460 				name = "tmp451";
2461 			else if (address >= 0x48 && address <= 0x4f)
2462 				name = "tmp461";
2463 		}
2464 	}
2465 
2466 	return name;
2467 }
2468 
2469 /* Return 0 if detection is successful, -ENODEV otherwise */
2470 static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info)
2471 {
2472 	struct i2c_adapter *adapter = client->adapter;
2473 	int man_id, chip_id, config1, convrate, lhigh;
2474 	const char *name = NULL;
2475 	int address = client->addr;
2476 	bool common_address =
2477 			(address >= 0x18 && address <= 0x1a) ||
2478 			(address >= 0x29 && address <= 0x2b) ||
2479 			(address >= 0x4c && address <= 0x4e);
2480 
2481 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2482 		return -ENODEV;
2483 
2484 	/*
2485 	 * Get well defined register value for chips with neither man_id nor
2486 	 * chip_id registers.
2487 	 */
2488 	lhigh = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_HIGH);
2489 
2490 	/* detection and identification */
2491 	man_id = i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID);
2492 	chip_id = i2c_smbus_read_byte_data(client, LM90_REG_CHIP_ID);
2493 	config1 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG1);
2494 	convrate = i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE);
2495 	if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0 || lhigh < 0)
2496 		return -ENODEV;
2497 
2498 	/* Bail out immediately if all register report the same value */
2499 	if (lhigh == man_id && lhigh == chip_id && lhigh == config1 && lhigh == convrate)
2500 		return -ENODEV;
2501 
2502 	/*
2503 	 * If reading man_id and chip_id both return the same value as lhigh,
2504 	 * the chip may not support those registers and return the most recent read
2505 	 * value. Check again with a different register and handle accordingly.
2506 	 */
2507 	if (man_id == lhigh && chip_id == lhigh) {
2508 		convrate = i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE);
2509 		man_id = i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID);
2510 		chip_id = i2c_smbus_read_byte_data(client, LM90_REG_CHIP_ID);
2511 		if (convrate < 0 || man_id < 0 || chip_id < 0)
2512 			return -ENODEV;
2513 		if (man_id == convrate && chip_id == convrate)
2514 			man_id = -1;
2515 	}
2516 	switch (man_id) {
2517 	case -1:	/* Chip does not support man_id / chip_id */
2518 		if (common_address && !convrate && !(config1 & 0x7f))
2519 			name = lm90_detect_lm84(client);
2520 		break;
2521 	case 0x01:	/* National Semiconductor */
2522 		name = lm90_detect_national(client, chip_id, config1, convrate);
2523 		break;
2524 	case 0x1a:	/* ON */
2525 		name = lm90_detect_on(client, chip_id, config1, convrate);
2526 		break;
2527 	case 0x23:	/* Genesys Logic */
2528 		if (common_address && !(config1 & 0x3f) && !(convrate & 0xf8))
2529 			name = "gl523sm";
2530 		break;
2531 	case 0x41:	/* Analog Devices */
2532 		name = lm90_detect_analog(client, common_address, chip_id, config1,
2533 					  convrate);
2534 		break;
2535 	case 0x47:	/* GMT */
2536 		name = lm90_detect_gmt(client, chip_id, config1, convrate);
2537 		break;
2538 	case 0x49:	/* TI */
2539 		name = lm90_detect_ti49(client, common_address, chip_id, config1, convrate);
2540 		break;
2541 	case 0x4d:	/* Maxim Integrated */
2542 		name = lm90_detect_maxim(client, common_address, chip_id,
2543 					 config1, convrate);
2544 		break;
2545 	case 0x50:
2546 		name = lm90_detect_nuvoton_50(client, chip_id, config1, convrate);
2547 		break;
2548 	case 0x54:	/* ON MC1066, Microchip TC1068, TCM1617 (originally TelCom) */
2549 		if (common_address && !(config1 & 0x3f) && !(convrate & 0xf8))
2550 			name = "mc1066";
2551 		break;
2552 	case 0x55:	/* TI */
2553 		name = lm90_detect_ti(client, chip_id, config1, convrate);
2554 		break;
2555 	case 0x5c:	/* Winbond/Nuvoton */
2556 		name = lm90_detect_nuvoton(client, chip_id, config1, convrate);
2557 		break;
2558 	case 0xa1:	/*  NXP Semiconductor/Philips */
2559 		name = lm90_detect_nxp(client, common_address, chip_id, config1, convrate);
2560 		break;
2561 	case 0xff:	/* MAX1617, G767, NE1617 */
2562 		if (common_address && chip_id == 0xff && convrate < 8)
2563 			name = lm90_detect_max1617(client, config1);
2564 		break;
2565 	default:
2566 		break;
2567 	}
2568 
2569 	if (!name) {	/* identification failed */
2570 		dev_dbg(&adapter->dev,
2571 			"Unsupported chip at 0x%02x (man_id=0x%02X, chip_id=0x%02X)\n",
2572 			client->addr, man_id, chip_id);
2573 		return -ENODEV;
2574 	}
2575 
2576 	strscpy(info->type, name, I2C_NAME_SIZE);
2577 
2578 	return 0;
2579 }
2580 
2581 static void lm90_restore_conf(void *_data)
2582 {
2583 	struct lm90_data *data = _data;
2584 	struct i2c_client *client = data->client;
2585 
2586 	/* Restore initial configuration */
2587 	if (data->flags & LM90_HAVE_CONVRATE)
2588 		lm90_write_convrate(data, data->convrate_orig);
2589 	lm90_write_reg(client, LM90_REG_CONFIG1, data->config_orig);
2590 }
2591 
2592 static void lm90_stop_work(void *_data)
2593 {
2594 	struct lm90_data *data = _data;
2595 
2596 	scoped_guard(hwmon_lock, data->hwmon_dev) {
2597 		data->shutdown = true;
2598 	}
2599 	cancel_delayed_work_sync(&data->alert_work);
2600 	cancel_work_sync(&data->report_work);
2601 }
2602 
2603 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
2604 {
2605 	struct device_node *np = client->dev.of_node;
2606 	int config, convrate;
2607 
2608 	if (data->flags & LM90_HAVE_CONVRATE) {
2609 		convrate = lm90_read_reg(client, LM90_REG_CONVRATE);
2610 		if (convrate < 0)
2611 			return convrate;
2612 		data->convrate_orig = convrate;
2613 		lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
2614 	} else {
2615 		data->update_interval = 500;
2616 	}
2617 
2618 	/*
2619 	 * Start the conversions.
2620 	 */
2621 	config = lm90_read_reg(client, LM90_REG_CONFIG1);
2622 	if (config < 0)
2623 		return config;
2624 	data->config_orig = config;
2625 	data->config = config;
2626 
2627 	/* Check Temperature Range Select */
2628 	if (data->flags & LM90_HAVE_EXTENDED_TEMP) {
2629 		if (of_property_read_bool(np, "ti,extended-range-enable"))
2630 			config |= 0x04;
2631 		if (!(config & 0x04))
2632 			data->flags &= ~LM90_HAVE_EXTENDED_TEMP;
2633 	}
2634 
2635 	/*
2636 	 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
2637 	 * 0.125 degree resolution) and range (0x08, extend range
2638 	 * to -64 degree) mode for the remote temperature sensor.
2639 	 * Note that expeciments with an actual chip do not show a difference
2640 	 * if bit 3 is set or not.
2641 	 */
2642 	if (data->kind == max6680)
2643 		config |= 0x18;
2644 
2645 	/*
2646 	 * Put MAX6654 into extended range (0x20, extend minimum range from
2647 	 * 0 degrees to -64 degrees). Note that extended resolution is not
2648 	 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
2649 	 * slower, which is intentionally not done by default.
2650 	 */
2651 	if (data->kind == max6654)
2652 		config |= 0x20;
2653 
2654 	/*
2655 	 * Select external channel 0 for devices with three sensors
2656 	 */
2657 	if (data->flags & LM90_HAVE_TEMP3)
2658 		config &= ~0x08;
2659 
2660 	/*
2661 	 * Interrupt is enabled by default on reset, but it may be disabled
2662 	 * by bootloader, unmask it.
2663 	 */
2664 	if (client->irq)
2665 		config &= ~0x80;
2666 
2667 	config &= 0xBF;	/* run */
2668 	lm90_update_confreg(data, config);
2669 
2670 	return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
2671 }
2672 
2673 static bool lm90_is_tripped(struct i2c_client *client)
2674 {
2675 	struct lm90_data *data = i2c_get_clientdata(client);
2676 	int ret;
2677 
2678 	ret = lm90_update_alarms(data, true);
2679 	if (ret < 0)
2680 		return false;
2681 
2682 	return !!data->current_alarms;
2683 }
2684 
2685 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
2686 {
2687 	struct i2c_client *client = dev_id;
2688 
2689 	if (lm90_is_tripped(client))
2690 		return IRQ_HANDLED;
2691 	else
2692 		return IRQ_NONE;
2693 }
2694 
2695 static int lm90_probe_channel_from_dt(struct i2c_client *client,
2696 				      struct device_node *child,
2697 				      struct lm90_data *data)
2698 {
2699 	u32 id;
2700 	s32 val;
2701 	int err;
2702 	struct device *dev = &client->dev;
2703 
2704 	err = of_property_read_u32(child, "reg", &id);
2705 	if (err) {
2706 		dev_err(dev, "missing reg property of %pOFn\n", child);
2707 		return err;
2708 	}
2709 
2710 	if (id >= MAX_CHANNELS) {
2711 		dev_err(dev, "invalid reg property value %d in %pOFn\n", id, child);
2712 		return -EINVAL;
2713 	}
2714 
2715 	err = of_property_read_string(child, "label", &data->channel_label[id]);
2716 	if (err == -ENODATA || err == -EILSEQ) {
2717 		dev_err(dev, "invalid label property in %pOFn\n", child);
2718 		return err;
2719 	}
2720 
2721 	if (data->channel_label[id])
2722 		data->channel_config[id] |= HWMON_T_LABEL;
2723 
2724 	err = of_property_read_s32(child, "temperature-offset-millicelsius", &val);
2725 	if (!err) {
2726 		if (id == 0) {
2727 			dev_err(dev, "temperature-offset-millicelsius can't be set for internal channel\n");
2728 			return -EINVAL;
2729 		}
2730 
2731 		err = lm90_set_temp_offset(data, lm90_temp_offset_index[id], id, val);
2732 		if (err) {
2733 			dev_err(dev, "can't set temperature offset %d for channel %d (%d)\n",
2734 				val, id, err);
2735 			return err;
2736 		}
2737 	}
2738 
2739 	return 0;
2740 }
2741 
2742 static int lm90_parse_dt_channel_info(struct i2c_client *client,
2743 				      struct lm90_data *data)
2744 {
2745 	int err;
2746 	struct device *dev = &client->dev;
2747 	const struct device_node *np = dev->of_node;
2748 
2749 	for_each_child_of_node_scoped(np, child) {
2750 		if (strcmp(child->name, "channel"))
2751 			continue;
2752 
2753 		err = lm90_probe_channel_from_dt(client, child, data);
2754 		if (err)
2755 			return err;
2756 	}
2757 
2758 	return 0;
2759 }
2760 
2761 static const struct hwmon_ops lm90_ops = {
2762 	.is_visible = lm90_is_visible,
2763 	.read = lm90_read,
2764 	.read_string = lm90_read_string,
2765 	.write = lm90_write,
2766 };
2767 
2768 static int lm90_probe(struct i2c_client *client)
2769 {
2770 	struct device *dev = &client->dev;
2771 	struct i2c_adapter *adapter = client->adapter;
2772 	struct hwmon_channel_info *info;
2773 	struct device *hwmon_dev;
2774 	struct lm90_data *data;
2775 	int err;
2776 
2777 	err = devm_regulator_get_enable(dev, "vcc");
2778 	if (err)
2779 		return dev_err_probe(dev, err, "Failed to enable regulator\n");
2780 
2781 	data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
2782 	if (!data)
2783 		return -ENOMEM;
2784 
2785 	data->client = client;
2786 	i2c_set_clientdata(client, data);
2787 	INIT_DELAYED_WORK(&data->alert_work, lm90_alert_work);
2788 	INIT_WORK(&data->report_work, lm90_report_alarms);
2789 
2790 	/* Set the device type */
2791 	data->kind = (uintptr_t)i2c_get_match_data(client);
2792 
2793 	/*
2794 	 * Different devices have different alarm bits triggering the
2795 	 * ALERT# output
2796 	 */
2797 	data->alert_alarms = lm90_params[data->kind].alert_alarms;
2798 	data->resolution = lm90_params[data->kind].resolution ? : 11;
2799 
2800 	/* Set chip capabilities */
2801 	data->flags = lm90_params[data->kind].flags;
2802 
2803 	if ((data->flags & (LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC)) &&
2804 	    !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_PEC))
2805 		data->flags &= ~(LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC);
2806 
2807 	if ((data->flags & LM90_HAVE_PARTIAL_PEC) &&
2808 	    !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
2809 		data->flags &= ~LM90_HAVE_PARTIAL_PEC;
2810 
2811 	data->chip.ops = &lm90_ops;
2812 	data->chip.info = data->info;
2813 
2814 	data->info[0] = &data->chip_info;
2815 	info = &data->chip_info;
2816 	info->type = hwmon_chip;
2817 	info->config = data->chip_config;
2818 
2819 	data->chip_config[0] = HWMON_C_REGISTER_TZ;
2820 	if (data->flags & LM90_HAVE_ALARMS)
2821 		data->chip_config[0] |= HWMON_C_ALARMS;
2822 	if (data->flags & LM90_HAVE_CONVRATE)
2823 		data->chip_config[0] |= HWMON_C_UPDATE_INTERVAL;
2824 	if (data->flags & LM90_HAVE_FAULTQUEUE)
2825 		data->chip_config[0] |= HWMON_C_TEMP_SAMPLES;
2826 	if (data->flags & (LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC))
2827 		data->chip_config[0] |= HWMON_C_PEC;
2828 	data->info[1] = &data->temp_info;
2829 
2830 	info = &data->temp_info;
2831 	info->type = hwmon_temp;
2832 	info->config = data->channel_config;
2833 
2834 	data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MAX |
2835 		HWMON_T_MAX_ALARM;
2836 	data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MAX |
2837 		HWMON_T_MAX_ALARM | HWMON_T_FAULT;
2838 
2839 	if (data->flags & LM90_HAVE_LOW) {
2840 		data->channel_config[0] |= HWMON_T_MIN | HWMON_T_MIN_ALARM;
2841 		data->channel_config[1] |= HWMON_T_MIN | HWMON_T_MIN_ALARM;
2842 	}
2843 
2844 	if (data->flags & LM90_HAVE_CRIT) {
2845 		data->channel_config[0] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
2846 		data->channel_config[1] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
2847 	}
2848 
2849 	if (data->flags & LM90_HAVE_OFFSET)
2850 		data->channel_config[1] |= HWMON_T_OFFSET;
2851 
2852 	if (data->flags & LM90_HAVE_EMERGENCY) {
2853 		data->channel_config[0] |= HWMON_T_EMERGENCY |
2854 			HWMON_T_EMERGENCY_HYST;
2855 		data->channel_config[1] |= HWMON_T_EMERGENCY |
2856 			HWMON_T_EMERGENCY_HYST;
2857 	}
2858 
2859 	if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
2860 		data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
2861 		data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
2862 	}
2863 
2864 	if (data->flags & LM90_HAVE_TEMP3) {
2865 		data->channel_config[2] = HWMON_T_INPUT |
2866 			HWMON_T_MIN | HWMON_T_MAX |
2867 			HWMON_T_CRIT | HWMON_T_CRIT_HYST |
2868 			HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
2869 			HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
2870 		if (data->flags & LM90_HAVE_EMERGENCY) {
2871 			data->channel_config[2] |= HWMON_T_EMERGENCY |
2872 				HWMON_T_EMERGENCY_HYST;
2873 		}
2874 		if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
2875 			data->channel_config[2] |= HWMON_T_EMERGENCY_ALARM;
2876 		if (data->flags & LM90_HAVE_OFFSET)
2877 			data->channel_config[2] |= HWMON_T_OFFSET;
2878 	}
2879 
2880 	data->faultqueue_mask = lm90_params[data->kind].faultqueue_mask;
2881 	data->faultqueue_depth = lm90_params[data->kind].faultqueue_depth;
2882 	data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
2883 	if (data->flags & LM90_HAVE_REMOTE_EXT)
2884 		data->reg_remote_ext = LM90_REG_REMOTE_TEMPL;
2885 	data->reg_status2 = lm90_params[data->kind].reg_status2;
2886 
2887 	/* Set maximum conversion rate */
2888 	data->max_convrate = lm90_params[data->kind].max_convrate;
2889 
2890 	/* Parse device-tree channel information */
2891 	if (client->dev.of_node) {
2892 		err = lm90_parse_dt_channel_info(client, data);
2893 		if (err)
2894 			return err;
2895 	}
2896 
2897 	/* Initialize the LM90 chip */
2898 	err = lm90_init_client(client, data);
2899 	if (err < 0) {
2900 		dev_err(dev, "Failed to initialize device\n");
2901 		return err;
2902 	}
2903 
2904 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
2905 							 data, &data->chip,
2906 							 NULL);
2907 	if (IS_ERR(hwmon_dev))
2908 		return PTR_ERR(hwmon_dev);
2909 
2910 	data->hwmon_dev = hwmon_dev;
2911 
2912 	err = devm_add_action_or_reset(&client->dev, lm90_stop_work, data);
2913 	if (err)
2914 		return err;
2915 
2916 	if (client->irq) {
2917 		dev_dbg(dev, "IRQ: %d\n", client->irq);
2918 		err = devm_request_threaded_irq(dev, client->irq,
2919 						NULL, lm90_irq_thread,
2920 						IRQF_ONESHOT, "lm90", client);
2921 		if (err < 0) {
2922 			dev_err(dev, "cannot request IRQ %d\n", client->irq);
2923 			return err;
2924 		}
2925 	}
2926 
2927 	return 0;
2928 }
2929 
2930 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
2931 		       unsigned int flag)
2932 {
2933 	if (type != I2C_PROTOCOL_SMBUS_ALERT)
2934 		return;
2935 
2936 	if (lm90_is_tripped(client)) {
2937 		/*
2938 		 * Disable ALERT# output, because these chips don't implement
2939 		 * SMBus alert correctly; they should only hold the alert line
2940 		 * low briefly.
2941 		 */
2942 		struct lm90_data *data = i2c_get_clientdata(client);
2943 
2944 		scoped_guard(hwmon_lock, data->hwmon_dev) {
2945 			if (!data->shutdown && (data->flags & LM90_HAVE_BROKEN_ALERT) &&
2946 			    (data->current_alarms & data->alert_alarms)) {
2947 				if (!(data->config & 0x80)) {
2948 					dev_dbg(&client->dev, "Disabling ALERT#\n");
2949 					lm90_update_confreg(data, data->config | 0x80);
2950 				}
2951 				schedule_delayed_work(&data->alert_work,
2952 					max_t(int, HZ, msecs_to_jiffies(data->update_interval)));
2953 			}
2954 		}
2955 	} else {
2956 		dev_dbg(&client->dev, "Everything OK\n");
2957 	}
2958 }
2959 
2960 static int lm90_suspend(struct device *dev)
2961 {
2962 	struct lm90_data *data = dev_get_drvdata(dev);
2963 	struct i2c_client *client = data->client;
2964 
2965 	if (client->irq)
2966 		disable_irq(client->irq);
2967 
2968 	return 0;
2969 }
2970 
2971 static int lm90_resume(struct device *dev)
2972 {
2973 	struct lm90_data *data = dev_get_drvdata(dev);
2974 	struct i2c_client *client = data->client;
2975 
2976 	if (client->irq)
2977 		enable_irq(client->irq);
2978 
2979 	return 0;
2980 }
2981 
2982 static DEFINE_SIMPLE_DEV_PM_OPS(lm90_pm_ops, lm90_suspend, lm90_resume);
2983 
2984 static struct i2c_driver lm90_driver = {
2985 	.class		= I2C_CLASS_HWMON,
2986 	.driver = {
2987 		.name	= "lm90",
2988 		.of_match_table = of_match_ptr(lm90_of_match),
2989 		.pm	= pm_sleep_ptr(&lm90_pm_ops),
2990 	},
2991 	.probe		= lm90_probe,
2992 	.alert		= lm90_alert,
2993 	.id_table	= lm90_id,
2994 	.detect		= lm90_detect,
2995 	.address_list	= normal_i2c,
2996 };
2997 
2998 module_i2c_driver(lm90_driver);
2999 
3000 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
3001 MODULE_DESCRIPTION("LM90/ADM1032 driver");
3002 MODULE_LICENSE("GPL");
3003