xref: /linux/drivers/hwmon/lm90.c (revision a1a9b71ebd6582c94090ee5b70fb3eabf7088f22)
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.
46  *
47  * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
48  * MAX6692 chips made by Maxim.  These are again similar to the LM86,
49  * but they use unsigned temperature values and can report temperatures
50  * from 0 to 145 degrees.
51  *
52  * This driver also supports the MAX6680 and MAX6681, two other sensor
53  * chips made by Maxim. These are quite similar to the other Maxim
54  * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
55  * be treated identically.
56  *
57  * This driver also supports the MAX6695 and MAX6696, two other sensor
58  * chips made by Maxim. These are also quite similar to other Maxim
59  * chips, but support three temperature sensors instead of two. MAX6695
60  * and MAX6696 only differ in the pinout so they can be treated identically.
61  *
62  * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
63  * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
64  * and extended mode. They are mostly compatible with LM90 except for a data
65  * format difference for the temperature value registers.
66  *
67  * This driver also supports the SA56004 from Philips. This device is
68  * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
69  *
70  * This driver also supports the G781 from GMT. This device is compatible
71  * with the ADM1032.
72  *
73  * This driver also supports TMP451 and TMP461 from Texas Instruments.
74  * Those devices are supported in both compatibility and extended mode.
75  * They are mostly compatible with ADT7461 except for local temperature
76  * low byte register and max conversion rate.
77  *
78  * Since the LM90 was the first chipset supported by this driver, most
79  * comments will refer to this chipset, but are actually general and
80  * concern all supported chipsets, unless mentioned otherwise.
81  */
82 
83 #include <linux/module.h>
84 #include <linux/init.h>
85 #include <linux/slab.h>
86 #include <linux/jiffies.h>
87 #include <linux/i2c.h>
88 #include <linux/hwmon.h>
89 #include <linux/err.h>
90 #include <linux/mutex.h>
91 #include <linux/of_device.h>
92 #include <linux/sysfs.h>
93 #include <linux/interrupt.h>
94 #include <linux/regulator/consumer.h>
95 
96 /*
97  * Addresses to scan
98  * Address is fully defined internally and cannot be changed except for
99  * MAX6659, MAX6680 and MAX6681.
100  * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
101  * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
102  * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
103  * have address 0x4d.
104  * MAX6647 has address 0x4e.
105  * MAX6659 can have address 0x4c, 0x4d or 0x4e.
106  * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
107  * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
108  * SA56004 can have address 0x48 through 0x4F.
109  */
110 
111 static const unsigned short normal_i2c[] = {
112 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
113 	0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
114 
115 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
116 	max6646, w83l771, max6696, sa56004, g781, tmp451, tmp461, max6654 };
117 
118 /*
119  * The LM90 registers
120  */
121 
122 #define LM90_REG_R_MAN_ID		0xFE
123 #define LM90_REG_R_CHIP_ID		0xFF
124 #define LM90_REG_R_CONFIG1		0x03
125 #define LM90_REG_W_CONFIG1		0x09
126 #define LM90_REG_R_CONFIG2		0xBF
127 #define LM90_REG_W_CONFIG2		0xBF
128 #define LM90_REG_R_CONVRATE		0x04
129 #define LM90_REG_W_CONVRATE		0x0A
130 #define LM90_REG_R_STATUS		0x02
131 #define LM90_REG_R_LOCAL_TEMP		0x00
132 #define LM90_REG_R_LOCAL_HIGH		0x05
133 #define LM90_REG_W_LOCAL_HIGH		0x0B
134 #define LM90_REG_R_LOCAL_LOW		0x06
135 #define LM90_REG_W_LOCAL_LOW		0x0C
136 #define LM90_REG_R_LOCAL_CRIT		0x20
137 #define LM90_REG_W_LOCAL_CRIT		0x20
138 #define LM90_REG_R_REMOTE_TEMPH		0x01
139 #define LM90_REG_R_REMOTE_TEMPL		0x10
140 #define LM90_REG_R_REMOTE_OFFSH		0x11
141 #define LM90_REG_W_REMOTE_OFFSH		0x11
142 #define LM90_REG_R_REMOTE_OFFSL		0x12
143 #define LM90_REG_W_REMOTE_OFFSL		0x12
144 #define LM90_REG_R_REMOTE_HIGHH		0x07
145 #define LM90_REG_W_REMOTE_HIGHH		0x0D
146 #define LM90_REG_R_REMOTE_HIGHL		0x13
147 #define LM90_REG_W_REMOTE_HIGHL		0x13
148 #define LM90_REG_R_REMOTE_LOWH		0x08
149 #define LM90_REG_W_REMOTE_LOWH		0x0E
150 #define LM90_REG_R_REMOTE_LOWL		0x14
151 #define LM90_REG_W_REMOTE_LOWL		0x14
152 #define LM90_REG_R_REMOTE_CRIT		0x19
153 #define LM90_REG_W_REMOTE_CRIT		0x19
154 #define LM90_REG_R_TCRIT_HYST		0x21
155 #define LM90_REG_W_TCRIT_HYST		0x21
156 
157 /* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
158 
159 #define MAX6657_REG_R_LOCAL_TEMPL	0x11
160 #define MAX6696_REG_R_STATUS2		0x12
161 #define MAX6659_REG_R_REMOTE_EMERG	0x16
162 #define MAX6659_REG_W_REMOTE_EMERG	0x16
163 #define MAX6659_REG_R_LOCAL_EMERG	0x17
164 #define MAX6659_REG_W_LOCAL_EMERG	0x17
165 
166 /*  SA56004 registers */
167 
168 #define SA56004_REG_R_LOCAL_TEMPL 0x22
169 
170 #define LM90_MAX_CONVRATE_MS	16000	/* Maximum conversion rate in ms */
171 
172 /* TMP451/TMP461 registers */
173 #define TMP451_REG_R_LOCAL_TEMPL	0x15
174 #define TMP451_REG_CONALERT		0x22
175 
176 #define TMP461_REG_CHEN			0x16
177 #define TMP461_REG_DFC			0x24
178 
179 /*
180  * Device flags
181  */
182 #define LM90_FLAG_ADT7461_EXT	(1 << 0) /* ADT7461 extended mode	*/
183 /* Device features */
184 #define LM90_HAVE_OFFSET	(1 << 1) /* temperature offset register	*/
185 #define LM90_HAVE_REM_LIMIT_EXT	(1 << 3) /* extended remote limit	*/
186 #define LM90_HAVE_EMERGENCY	(1 << 4) /* 3rd upper (emergency) limit	*/
187 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm		*/
188 #define LM90_HAVE_TEMP3		(1 << 6) /* 3rd temperature sensor	*/
189 #define LM90_HAVE_BROKEN_ALERT	(1 << 7) /* Broken alert		*/
190 #define LM90_HAVE_EXTENDED_TEMP	(1 << 8) /* extended temperature support*/
191 #define LM90_PAUSE_FOR_CONFIG	(1 << 9) /* Pause conversion for config	*/
192 #define LM90_HAVE_CRIT		(1 << 10)/* Chip supports CRIT/OVERT register	*/
193 #define LM90_HAVE_CRIT_ALRM_SWP	(1 << 11)/* critical alarm bits swapped	*/
194 
195 /* LM90 status */
196 #define LM90_STATUS_LTHRM	(1 << 0) /* local THERM limit tripped */
197 #define LM90_STATUS_RTHRM	(1 << 1) /* remote THERM limit tripped */
198 #define LM90_STATUS_ROPEN	(1 << 2) /* remote is an open circuit */
199 #define LM90_STATUS_RLOW	(1 << 3) /* remote low temp limit tripped */
200 #define LM90_STATUS_RHIGH	(1 << 4) /* remote high temp limit tripped */
201 #define LM90_STATUS_LLOW	(1 << 5) /* local low temp limit tripped */
202 #define LM90_STATUS_LHIGH	(1 << 6) /* local high temp limit tripped */
203 #define LM90_STATUS_BUSY	(1 << 7) /* conversion is ongoing */
204 
205 #define MAX6696_STATUS2_R2THRM	(1 << 1) /* remote2 THERM limit tripped */
206 #define MAX6696_STATUS2_R2OPEN	(1 << 2) /* remote2 is an open circuit */
207 #define MAX6696_STATUS2_R2LOW	(1 << 3) /* remote2 low temp limit tripped */
208 #define MAX6696_STATUS2_R2HIGH	(1 << 4) /* remote2 high temp limit tripped */
209 #define MAX6696_STATUS2_ROT2	(1 << 5) /* remote emergency limit tripped */
210 #define MAX6696_STATUS2_R2OT2	(1 << 6) /* remote2 emergency limit tripped */
211 #define MAX6696_STATUS2_LOT2	(1 << 7) /* local emergency limit tripped */
212 
213 /*
214  * Driver data (common to all clients)
215  */
216 
217 static const struct i2c_device_id lm90_id[] = {
218 	{ "adm1032", adm1032 },
219 	{ "adt7461", adt7461 },
220 	{ "adt7461a", adt7461 },
221 	{ "g781", g781 },
222 	{ "lm90", lm90 },
223 	{ "lm86", lm86 },
224 	{ "lm89", lm86 },
225 	{ "lm99", lm99 },
226 	{ "max6646", max6646 },
227 	{ "max6647", max6646 },
228 	{ "max6649", max6646 },
229 	{ "max6654", max6654 },
230 	{ "max6657", max6657 },
231 	{ "max6658", max6657 },
232 	{ "max6659", max6659 },
233 	{ "max6680", max6680 },
234 	{ "max6681", max6680 },
235 	{ "max6695", max6696 },
236 	{ "max6696", max6696 },
237 	{ "nct1008", adt7461 },
238 	{ "w83l771", w83l771 },
239 	{ "sa56004", sa56004 },
240 	{ "tmp451", tmp451 },
241 	{ "tmp461", tmp461 },
242 	{ }
243 };
244 MODULE_DEVICE_TABLE(i2c, lm90_id);
245 
246 static const struct of_device_id __maybe_unused lm90_of_match[] = {
247 	{
248 		.compatible = "adi,adm1032",
249 		.data = (void *)adm1032
250 	},
251 	{
252 		.compatible = "adi,adt7461",
253 		.data = (void *)adt7461
254 	},
255 	{
256 		.compatible = "adi,adt7461a",
257 		.data = (void *)adt7461
258 	},
259 	{
260 		.compatible = "gmt,g781",
261 		.data = (void *)g781
262 	},
263 	{
264 		.compatible = "national,lm90",
265 		.data = (void *)lm90
266 	},
267 	{
268 		.compatible = "national,lm86",
269 		.data = (void *)lm86
270 	},
271 	{
272 		.compatible = "national,lm89",
273 		.data = (void *)lm86
274 	},
275 	{
276 		.compatible = "national,lm99",
277 		.data = (void *)lm99
278 	},
279 	{
280 		.compatible = "dallas,max6646",
281 		.data = (void *)max6646
282 	},
283 	{
284 		.compatible = "dallas,max6647",
285 		.data = (void *)max6646
286 	},
287 	{
288 		.compatible = "dallas,max6649",
289 		.data = (void *)max6646
290 	},
291 	{
292 		.compatible = "dallas,max6654",
293 		.data = (void *)max6654
294 	},
295 	{
296 		.compatible = "dallas,max6657",
297 		.data = (void *)max6657
298 	},
299 	{
300 		.compatible = "dallas,max6658",
301 		.data = (void *)max6657
302 	},
303 	{
304 		.compatible = "dallas,max6659",
305 		.data = (void *)max6659
306 	},
307 	{
308 		.compatible = "dallas,max6680",
309 		.data = (void *)max6680
310 	},
311 	{
312 		.compatible = "dallas,max6681",
313 		.data = (void *)max6680
314 	},
315 	{
316 		.compatible = "dallas,max6695",
317 		.data = (void *)max6696
318 	},
319 	{
320 		.compatible = "dallas,max6696",
321 		.data = (void *)max6696
322 	},
323 	{
324 		.compatible = "onnn,nct1008",
325 		.data = (void *)adt7461
326 	},
327 	{
328 		.compatible = "winbond,w83l771",
329 		.data = (void *)w83l771
330 	},
331 	{
332 		.compatible = "nxp,sa56004",
333 		.data = (void *)sa56004
334 	},
335 	{
336 		.compatible = "ti,tmp451",
337 		.data = (void *)tmp451
338 	},
339 	{
340 		.compatible = "ti,tmp461",
341 		.data = (void *)tmp461
342 	},
343 	{ },
344 };
345 MODULE_DEVICE_TABLE(of, lm90_of_match);
346 
347 /*
348  * chip type specific parameters
349  */
350 struct lm90_params {
351 	u32 flags;		/* Capabilities */
352 	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */
353 				/* Upper 8 bits for max6695/96 */
354 	u8 max_convrate;	/* Maximum conversion rate register value */
355 	u8 reg_local_ext;	/* Extended local temp register (optional) */
356 };
357 
358 static const struct lm90_params lm90_params[] = {
359 	[adm1032] = {
360 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
361 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT,
362 		.alert_alarms = 0x7c,
363 		.max_convrate = 10,
364 	},
365 	[adt7461] = {
366 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
367 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
368 		  | LM90_HAVE_CRIT,
369 		.alert_alarms = 0x7c,
370 		.max_convrate = 10,
371 	},
372 	[g781] = {
373 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
374 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT,
375 		.alert_alarms = 0x7c,
376 		.max_convrate = 7,
377 	},
378 	[lm86] = {
379 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
380 		  | LM90_HAVE_CRIT,
381 		.alert_alarms = 0x7b,
382 		.max_convrate = 9,
383 	},
384 	[lm90] = {
385 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
386 		  | LM90_HAVE_CRIT,
387 		.alert_alarms = 0x7b,
388 		.max_convrate = 9,
389 	},
390 	[lm99] = {
391 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
392 		  | LM90_HAVE_CRIT,
393 		.alert_alarms = 0x7b,
394 		.max_convrate = 9,
395 	},
396 	[max6646] = {
397 		.flags = LM90_HAVE_CRIT | LM90_HAVE_BROKEN_ALERT,
398 		.alert_alarms = 0x7c,
399 		.max_convrate = 6,
400 		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
401 	},
402 	[max6654] = {
403 		.flags = LM90_HAVE_BROKEN_ALERT,
404 		.alert_alarms = 0x7c,
405 		.max_convrate = 7,
406 		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
407 	},
408 	[max6657] = {
409 		.flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_CRIT,
410 		.alert_alarms = 0x7c,
411 		.max_convrate = 8,
412 		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
413 	},
414 	[max6659] = {
415 		.flags = LM90_HAVE_EMERGENCY | LM90_HAVE_CRIT,
416 		.alert_alarms = 0x7c,
417 		.max_convrate = 8,
418 		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
419 	},
420 	[max6680] = {
421 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_CRIT
422 		  | LM90_HAVE_CRIT_ALRM_SWP | LM90_HAVE_BROKEN_ALERT,
423 		.alert_alarms = 0x7c,
424 		.max_convrate = 7,
425 	},
426 	[max6696] = {
427 		.flags = LM90_HAVE_EMERGENCY
428 		  | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT,
429 		.alert_alarms = 0x1c7c,
430 		.max_convrate = 6,
431 		.reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
432 	},
433 	[w83l771] = {
434 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT,
435 		.alert_alarms = 0x7c,
436 		.max_convrate = 8,
437 	},
438 	[sa56004] = {
439 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT,
440 		.alert_alarms = 0x7b,
441 		.max_convrate = 9,
442 		.reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
443 	},
444 	[tmp451] = {
445 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
446 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT,
447 		.alert_alarms = 0x7c,
448 		.max_convrate = 9,
449 		.reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
450 	},
451 	[tmp461] = {
452 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
453 		  | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT,
454 		.alert_alarms = 0x7c,
455 		.max_convrate = 9,
456 		.reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
457 	},
458 };
459 
460 /*
461  * TEMP8 register index
462  */
463 enum lm90_temp8_reg_index {
464 	LOCAL_LOW = 0,
465 	LOCAL_HIGH,
466 	LOCAL_CRIT,
467 	REMOTE_CRIT,
468 	LOCAL_EMERG,	/* max6659 and max6695/96 */
469 	REMOTE_EMERG,	/* max6659 and max6695/96 */
470 	REMOTE2_CRIT,	/* max6695/96 only */
471 	REMOTE2_EMERG,	/* max6695/96 only */
472 	TEMP8_REG_NUM
473 };
474 
475 /*
476  * TEMP11 register index
477  */
478 enum lm90_temp11_reg_index {
479 	REMOTE_TEMP = 0,
480 	REMOTE_LOW,
481 	REMOTE_HIGH,
482 	REMOTE_OFFSET,	/* except max6646, max6657/58/59, and max6695/96 */
483 	LOCAL_TEMP,
484 	REMOTE2_TEMP,	/* max6695/96 only */
485 	REMOTE2_LOW,	/* max6695/96 only */
486 	REMOTE2_HIGH,	/* max6695/96 only */
487 	TEMP11_REG_NUM
488 };
489 
490 /*
491  * Client data (each client gets its own)
492  */
493 
494 struct lm90_data {
495 	struct i2c_client *client;
496 	struct device *hwmon_dev;
497 	u32 channel_config[4];
498 	struct hwmon_channel_info temp_info;
499 	const struct hwmon_channel_info *info[3];
500 	struct hwmon_chip_info chip;
501 	struct mutex update_lock;
502 	bool valid;		/* true if register values are valid */
503 	unsigned long last_updated; /* in jiffies */
504 	int kind;
505 	u32 flags;
506 
507 	unsigned int update_interval; /* in milliseconds */
508 
509 	u8 config;		/* Current configuration register value */
510 	u8 config_orig;		/* Original configuration register value */
511 	u8 convrate_orig;	/* Original conversion rate register value */
512 	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */
513 				/* Upper 8 bits for max6695/96 */
514 	u8 max_convrate;	/* Maximum conversion rate */
515 	u8 reg_local_ext;	/* local extension register offset */
516 
517 	/* registers values */
518 	s8 temp8[TEMP8_REG_NUM];
519 	s16 temp11[TEMP11_REG_NUM];
520 	u8 temp_hyst;
521 	u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
522 };
523 
524 /*
525  * Support functions
526  */
527 
528 /*
529  * The ADM1032 supports PEC but not on write byte transactions, so we need
530  * to explicitly ask for a transaction without PEC.
531  */
532 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
533 {
534 	return i2c_smbus_xfer(client->adapter, client->addr,
535 			      client->flags & ~I2C_CLIENT_PEC,
536 			      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
537 }
538 
539 /*
540  * It is assumed that client->update_lock is held (unless we are in
541  * detection or initialization steps). This matters when PEC is enabled,
542  * because we don't want the address pointer to change between the write
543  * byte and the read byte transactions.
544  */
545 static int lm90_read_reg(struct i2c_client *client, u8 reg)
546 {
547 	int err;
548 
549 	if (client->flags & I2C_CLIENT_PEC) {
550 		err = adm1032_write_byte(client, reg);
551 		if (err >= 0)
552 			err = i2c_smbus_read_byte(client);
553 	} else
554 		err = i2c_smbus_read_byte_data(client, reg);
555 
556 	return err;
557 }
558 
559 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
560 {
561 	int oldh, newh, l;
562 
563 	/*
564 	 * There is a trick here. We have to read two registers to have the
565 	 * sensor temperature, but we have to beware a conversion could occur
566 	 * between the readings. The datasheet says we should either use
567 	 * the one-shot conversion register, which we don't want to do
568 	 * (disables hardware monitoring) or monitor the busy bit, which is
569 	 * impossible (we can't read the values and monitor that bit at the
570 	 * exact same time). So the solution used here is to read the high
571 	 * byte once, then the low byte, then the high byte again. If the new
572 	 * high byte matches the old one, then we have a valid reading. Else
573 	 * we have to read the low byte again, and now we believe we have a
574 	 * correct reading.
575 	 */
576 	oldh = lm90_read_reg(client, regh);
577 	if (oldh < 0)
578 		return oldh;
579 	l = lm90_read_reg(client, regl);
580 	if (l < 0)
581 		return l;
582 	newh = lm90_read_reg(client, regh);
583 	if (newh < 0)
584 		return newh;
585 	if (oldh != newh) {
586 		l = lm90_read_reg(client, regl);
587 		if (l < 0)
588 			return l;
589 	}
590 	return (newh << 8) | l;
591 }
592 
593 static int lm90_update_confreg(struct lm90_data *data, u8 config)
594 {
595 	if (data->config != config) {
596 		int err;
597 
598 		err = i2c_smbus_write_byte_data(data->client,
599 						LM90_REG_W_CONFIG1,
600 						config);
601 		if (err)
602 			return err;
603 		data->config = config;
604 	}
605 	return 0;
606 }
607 
608 /*
609  * client->update_lock must be held when calling this function (unless we are
610  * in detection or initialization steps), and while a remote channel other
611  * than channel 0 is selected. Also, calling code must make sure to re-select
612  * external channel 0 before releasing the lock. This is necessary because
613  * various registers have different meanings as a result of selecting a
614  * non-default remote channel.
615  */
616 static int lm90_select_remote_channel(struct lm90_data *data, int channel)
617 {
618 	int err = 0;
619 
620 	if (data->kind == max6696) {
621 		u8 config = data->config & ~0x08;
622 
623 		if (channel)
624 			config |= 0x08;
625 		err = lm90_update_confreg(data, config);
626 	}
627 	return err;
628 }
629 
630 static int lm90_write_convrate(struct lm90_data *data, int val)
631 {
632 	u8 config = data->config;
633 	int err;
634 
635 	/* Save config and pause conversion */
636 	if (data->flags & LM90_PAUSE_FOR_CONFIG) {
637 		err = lm90_update_confreg(data, config | 0x40);
638 		if (err < 0)
639 			return err;
640 	}
641 
642 	/* Set conv rate */
643 	err = i2c_smbus_write_byte_data(data->client, LM90_REG_W_CONVRATE, val);
644 
645 	/* Revert change to config */
646 	lm90_update_confreg(data, config);
647 
648 	return err;
649 }
650 
651 /*
652  * Set conversion rate.
653  * client->update_lock must be held when calling this function (unless we are
654  * in detection or initialization steps).
655  */
656 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
657 			     unsigned int interval)
658 {
659 	unsigned int update_interval;
660 	int i, err;
661 
662 	/* Shift calculations to avoid rounding errors */
663 	interval <<= 6;
664 
665 	/* find the nearest update rate */
666 	for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
667 	     i < data->max_convrate; i++, update_interval >>= 1)
668 		if (interval >= update_interval * 3 / 4)
669 			break;
670 
671 	err = lm90_write_convrate(data, i);
672 	data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
673 	return err;
674 }
675 
676 static int lm90_update_limits(struct device *dev)
677 {
678 	struct lm90_data *data = dev_get_drvdata(dev);
679 	struct i2c_client *client = data->client;
680 	int val;
681 
682 	if (data->flags & LM90_HAVE_CRIT) {
683 		val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
684 		if (val < 0)
685 			return val;
686 		data->temp8[LOCAL_CRIT] = val;
687 
688 		val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
689 		if (val < 0)
690 			return val;
691 		data->temp8[REMOTE_CRIT] = val;
692 
693 		val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
694 		if (val < 0)
695 			return val;
696 		data->temp_hyst = val;
697 	}
698 
699 	val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
700 	if (val < 0)
701 		return val;
702 	data->temp11[REMOTE_LOW] = val << 8;
703 
704 	if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
705 		val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
706 		if (val < 0)
707 			return val;
708 		data->temp11[REMOTE_LOW] |= val;
709 	}
710 
711 	val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
712 	if (val < 0)
713 		return val;
714 	data->temp11[REMOTE_HIGH] = val << 8;
715 
716 	if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
717 		val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
718 		if (val < 0)
719 			return val;
720 		data->temp11[REMOTE_HIGH] |= val;
721 	}
722 
723 	if (data->flags & LM90_HAVE_OFFSET) {
724 		val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
725 				  LM90_REG_R_REMOTE_OFFSL);
726 		if (val < 0)
727 			return val;
728 		data->temp11[REMOTE_OFFSET] = val;
729 	}
730 
731 	if (data->flags & LM90_HAVE_EMERGENCY) {
732 		val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
733 		if (val < 0)
734 			return val;
735 		data->temp8[LOCAL_EMERG] = val;
736 
737 		val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
738 		if (val < 0)
739 			return val;
740 		data->temp8[REMOTE_EMERG] = val;
741 	}
742 
743 	if (data->kind == max6696) {
744 		val = lm90_select_remote_channel(data, 1);
745 		if (val < 0)
746 			return val;
747 
748 		val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
749 		if (val < 0)
750 			return val;
751 		data->temp8[REMOTE2_CRIT] = val;
752 
753 		val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
754 		if (val < 0)
755 			return val;
756 		data->temp8[REMOTE2_EMERG] = val;
757 
758 		val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
759 		if (val < 0)
760 			return val;
761 		data->temp11[REMOTE2_LOW] = val << 8;
762 
763 		val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
764 		if (val < 0)
765 			return val;
766 		data->temp11[REMOTE2_HIGH] = val << 8;
767 
768 		lm90_select_remote_channel(data, 0);
769 	}
770 
771 	return 0;
772 }
773 
774 static int lm90_update_device(struct device *dev)
775 {
776 	struct lm90_data *data = dev_get_drvdata(dev);
777 	struct i2c_client *client = data->client;
778 	unsigned long next_update;
779 	int val;
780 
781 	if (!data->valid) {
782 		val = lm90_update_limits(dev);
783 		if (val < 0)
784 			return val;
785 	}
786 
787 	next_update = data->last_updated +
788 		      msecs_to_jiffies(data->update_interval);
789 	if (time_after(jiffies, next_update) || !data->valid) {
790 		dev_dbg(&client->dev, "Updating lm90 data.\n");
791 
792 		data->valid = false;
793 
794 		val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
795 		if (val < 0)
796 			return val;
797 		data->temp8[LOCAL_LOW] = val;
798 
799 		val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
800 		if (val < 0)
801 			return val;
802 		data->temp8[LOCAL_HIGH] = val;
803 
804 		if (data->reg_local_ext) {
805 			val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
806 					  data->reg_local_ext);
807 			if (val < 0)
808 				return val;
809 			data->temp11[LOCAL_TEMP] = val;
810 		} else {
811 			val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
812 			if (val < 0)
813 				return val;
814 			data->temp11[LOCAL_TEMP] = val << 8;
815 		}
816 		val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
817 				  LM90_REG_R_REMOTE_TEMPL);
818 		if (val < 0)
819 			return val;
820 		data->temp11[REMOTE_TEMP] = val;
821 
822 		val = lm90_read_reg(client, LM90_REG_R_STATUS);
823 		if (val < 0)
824 			return val;
825 		data->alarms = val & ~LM90_STATUS_BUSY;
826 
827 		if (data->kind == max6696) {
828 			val = lm90_select_remote_channel(data, 1);
829 			if (val < 0)
830 				return val;
831 
832 			val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
833 					  LM90_REG_R_REMOTE_TEMPL);
834 			if (val < 0) {
835 				lm90_select_remote_channel(data, 0);
836 				return val;
837 			}
838 			data->temp11[REMOTE2_TEMP] = val;
839 
840 			lm90_select_remote_channel(data, 0);
841 
842 			val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
843 			if (val < 0)
844 				return val;
845 			data->alarms |= val << 8;
846 		}
847 
848 		/*
849 		 * Re-enable ALERT# output if it was originally enabled and
850 		 * relevant alarms are all clear
851 		 */
852 		if ((client->irq || !(data->config_orig & 0x80)) &&
853 		    !(data->alarms & data->alert_alarms)) {
854 			if (data->config & 0x80) {
855 				dev_dbg(&client->dev, "Re-enabling ALERT#\n");
856 				lm90_update_confreg(data, data->config & ~0x80);
857 			}
858 		}
859 
860 		data->last_updated = jiffies;
861 		data->valid = true;
862 	}
863 
864 	return 0;
865 }
866 
867 /*
868  * Conversions
869  * For local temperatures and limits, critical limits and the hysteresis
870  * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
871  * For remote temperatures and limits, it uses signed 11-bit values with
872  * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some
873  * Maxim chips use unsigned values.
874  */
875 
876 static inline int temp_from_s8(s8 val)
877 {
878 	return val * 1000;
879 }
880 
881 static inline int temp_from_u8(u8 val)
882 {
883 	return val * 1000;
884 }
885 
886 static inline int temp_from_s16(s16 val)
887 {
888 	return val / 32 * 125;
889 }
890 
891 static inline int temp_from_u16(u16 val)
892 {
893 	return val / 32 * 125;
894 }
895 
896 static s8 temp_to_s8(long val)
897 {
898 	if (val <= -128000)
899 		return -128;
900 	if (val >= 127000)
901 		return 127;
902 	if (val < 0)
903 		return (val - 500) / 1000;
904 	return (val + 500) / 1000;
905 }
906 
907 static u8 temp_to_u8(long val)
908 {
909 	if (val <= 0)
910 		return 0;
911 	if (val >= 255000)
912 		return 255;
913 	return (val + 500) / 1000;
914 }
915 
916 static s16 temp_to_s16(long val)
917 {
918 	if (val <= -128000)
919 		return 0x8000;
920 	if (val >= 127875)
921 		return 0x7FE0;
922 	if (val < 0)
923 		return (val - 62) / 125 * 32;
924 	return (val + 62) / 125 * 32;
925 }
926 
927 static u8 hyst_to_reg(long val)
928 {
929 	if (val <= 0)
930 		return 0;
931 	if (val >= 30500)
932 		return 31;
933 	return (val + 500) / 1000;
934 }
935 
936 /*
937  * ADT7461 in compatibility mode is almost identical to LM90 except that
938  * attempts to write values that are outside the range 0 < temp < 127 are
939  * treated as the boundary value.
940  *
941  * ADT7461 in "extended mode" operation uses unsigned integers offset by
942  * 64 (e.g., 0 -> -64 degC).  The range is restricted to -64..191 degC.
943  */
944 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
945 {
946 	if (data->flags & LM90_FLAG_ADT7461_EXT)
947 		return (val - 64) * 1000;
948 	return temp_from_s8(val);
949 }
950 
951 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
952 {
953 	if (data->flags & LM90_FLAG_ADT7461_EXT)
954 		return (val - 0x4000) / 64 * 250;
955 	return temp_from_s16(val);
956 }
957 
958 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
959 {
960 	if (data->flags & LM90_FLAG_ADT7461_EXT) {
961 		if (val <= -64000)
962 			return 0;
963 		if (val >= 191000)
964 			return 0xFF;
965 		return (val + 500 + 64000) / 1000;
966 	}
967 	if (val <= 0)
968 		return 0;
969 	if (val >= 127000)
970 		return 127;
971 	return (val + 500) / 1000;
972 }
973 
974 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
975 {
976 	if (data->flags & LM90_FLAG_ADT7461_EXT) {
977 		if (val <= -64000)
978 			return 0;
979 		if (val >= 191750)
980 			return 0xFFC0;
981 		return (val + 64000 + 125) / 250 * 64;
982 	}
983 	if (val <= 0)
984 		return 0;
985 	if (val >= 127750)
986 		return 0x7FC0;
987 	return (val + 125) / 250 * 64;
988 }
989 
990 /* pec used for ADM1032 only */
991 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
992 			char *buf)
993 {
994 	struct i2c_client *client = to_i2c_client(dev);
995 
996 	return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
997 }
998 
999 static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
1000 			 const char *buf, size_t count)
1001 {
1002 	struct i2c_client *client = to_i2c_client(dev);
1003 	long val;
1004 	int err;
1005 
1006 	err = kstrtol(buf, 10, &val);
1007 	if (err < 0)
1008 		return err;
1009 
1010 	switch (val) {
1011 	case 0:
1012 		client->flags &= ~I2C_CLIENT_PEC;
1013 		break;
1014 	case 1:
1015 		client->flags |= I2C_CLIENT_PEC;
1016 		break;
1017 	default:
1018 		return -EINVAL;
1019 	}
1020 
1021 	return count;
1022 }
1023 
1024 static DEVICE_ATTR_RW(pec);
1025 
1026 static int lm90_get_temp11(struct lm90_data *data, int index)
1027 {
1028 	s16 temp11 = data->temp11[index];
1029 	int temp;
1030 
1031 	if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1032 		temp = temp_from_u16_adt7461(data, temp11);
1033 	else if (data->kind == max6646)
1034 		temp = temp_from_u16(temp11);
1035 	else
1036 		temp = temp_from_s16(temp11);
1037 
1038 	/* +16 degrees offset for temp2 for the LM99 */
1039 	if (data->kind == lm99 && index <= 2)
1040 		temp += 16000;
1041 
1042 	return temp;
1043 }
1044 
1045 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
1046 {
1047 	static struct reg {
1048 		u8 high;
1049 		u8 low;
1050 	} reg[] = {
1051 	[REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1052 	[REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
1053 	[REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
1054 	[REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1055 	[REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
1056 	};
1057 	struct i2c_client *client = data->client;
1058 	struct reg *regp = &reg[index];
1059 	int err;
1060 
1061 	/* +16 degrees offset for temp2 for the LM99 */
1062 	if (data->kind == lm99 && index <= 2) {
1063 		/* prevent integer underflow */
1064 		val = max(val, -128000l);
1065 		val -= 16000;
1066 	}
1067 
1068 	if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1069 		data->temp11[index] = temp_to_u16_adt7461(data, val);
1070 	else if (data->kind == max6646)
1071 		data->temp11[index] = temp_to_u8(val) << 8;
1072 	else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1073 		data->temp11[index] = temp_to_s16(val);
1074 	else
1075 		data->temp11[index] = temp_to_s8(val) << 8;
1076 
1077 	lm90_select_remote_channel(data, index >= 3);
1078 	err = i2c_smbus_write_byte_data(client, regp->high,
1079 				  data->temp11[index] >> 8);
1080 	if (err < 0)
1081 		return err;
1082 	if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1083 		err = i2c_smbus_write_byte_data(client, regp->low,
1084 						data->temp11[index] & 0xff);
1085 
1086 	lm90_select_remote_channel(data, 0);
1087 	return err;
1088 }
1089 
1090 static int lm90_get_temp8(struct lm90_data *data, int index)
1091 {
1092 	s8 temp8 = data->temp8[index];
1093 	int temp;
1094 
1095 	if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1096 		temp = temp_from_u8_adt7461(data, temp8);
1097 	else if (data->kind == max6646)
1098 		temp = temp_from_u8(temp8);
1099 	else
1100 		temp = temp_from_s8(temp8);
1101 
1102 	/* +16 degrees offset for temp2 for the LM99 */
1103 	if (data->kind == lm99 && index == 3)
1104 		temp += 16000;
1105 
1106 	return temp;
1107 }
1108 
1109 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1110 {
1111 	static const u8 reg[TEMP8_REG_NUM] = {
1112 		LM90_REG_W_LOCAL_LOW,
1113 		LM90_REG_W_LOCAL_HIGH,
1114 		LM90_REG_W_LOCAL_CRIT,
1115 		LM90_REG_W_REMOTE_CRIT,
1116 		MAX6659_REG_W_LOCAL_EMERG,
1117 		MAX6659_REG_W_REMOTE_EMERG,
1118 		LM90_REG_W_REMOTE_CRIT,
1119 		MAX6659_REG_W_REMOTE_EMERG,
1120 	};
1121 	struct i2c_client *client = data->client;
1122 	int err;
1123 
1124 	/* +16 degrees offset for temp2 for the LM99 */
1125 	if (data->kind == lm99 && index == 3) {
1126 		/* prevent integer underflow */
1127 		val = max(val, -128000l);
1128 		val -= 16000;
1129 	}
1130 
1131 	if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1132 		data->temp8[index] = temp_to_u8_adt7461(data, val);
1133 	else if (data->kind == max6646)
1134 		data->temp8[index] = temp_to_u8(val);
1135 	else
1136 		data->temp8[index] = temp_to_s8(val);
1137 
1138 	lm90_select_remote_channel(data, index >= 6);
1139 	err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1140 	lm90_select_remote_channel(data, 0);
1141 
1142 	return err;
1143 }
1144 
1145 static int lm90_get_temphyst(struct lm90_data *data, int index)
1146 {
1147 	int temp;
1148 
1149 	if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1150 		temp = temp_from_u8_adt7461(data, data->temp8[index]);
1151 	else if (data->kind == max6646)
1152 		temp = temp_from_u8(data->temp8[index]);
1153 	else
1154 		temp = temp_from_s8(data->temp8[index]);
1155 
1156 	/* +16 degrees offset for temp2 for the LM99 */
1157 	if (data->kind == lm99 && index == 3)
1158 		temp += 16000;
1159 
1160 	return temp - temp_from_s8(data->temp_hyst);
1161 }
1162 
1163 static int lm90_set_temphyst(struct lm90_data *data, long val)
1164 {
1165 	struct i2c_client *client = data->client;
1166 	int temp;
1167 	int err;
1168 
1169 	if (data->flags & LM90_HAVE_EXTENDED_TEMP)
1170 		temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1171 	else if (data->kind == max6646)
1172 		temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1173 	else
1174 		temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1175 
1176 	/* prevent integer overflow/underflow */
1177 	val = clamp_val(val, -128000l, 255000l);
1178 
1179 	data->temp_hyst = hyst_to_reg(temp - val);
1180 	err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1181 					data->temp_hyst);
1182 	return err;
1183 }
1184 
1185 static const u8 lm90_temp_index[3] = {
1186 	LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1187 };
1188 
1189 static const u8 lm90_temp_min_index[3] = {
1190 	LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1191 };
1192 
1193 static const u8 lm90_temp_max_index[3] = {
1194 	LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1195 };
1196 
1197 static const u8 lm90_temp_crit_index[3] = {
1198 	LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1199 };
1200 
1201 static const u8 lm90_temp_emerg_index[3] = {
1202 	LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1203 };
1204 
1205 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1206 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1207 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1208 static const u8 lm90_crit_alarm_bits_swapped[3] = { 1, 0, 9 };
1209 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1210 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1211 
1212 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1213 {
1214 	struct lm90_data *data = dev_get_drvdata(dev);
1215 	int err;
1216 
1217 	mutex_lock(&data->update_lock);
1218 	err = lm90_update_device(dev);
1219 	mutex_unlock(&data->update_lock);
1220 	if (err)
1221 		return err;
1222 
1223 	switch (attr) {
1224 	case hwmon_temp_input:
1225 		*val = lm90_get_temp11(data, lm90_temp_index[channel]);
1226 		break;
1227 	case hwmon_temp_min_alarm:
1228 		*val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1229 		break;
1230 	case hwmon_temp_max_alarm:
1231 		*val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1232 		break;
1233 	case hwmon_temp_crit_alarm:
1234 		if (data->flags & LM90_HAVE_CRIT_ALRM_SWP)
1235 			*val = (data->alarms >> lm90_crit_alarm_bits_swapped[channel]) & 1;
1236 		else
1237 			*val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1238 		break;
1239 	case hwmon_temp_emergency_alarm:
1240 		*val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1241 		break;
1242 	case hwmon_temp_fault:
1243 		*val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1244 		break;
1245 	case hwmon_temp_min:
1246 		if (channel == 0)
1247 			*val = lm90_get_temp8(data,
1248 					      lm90_temp_min_index[channel]);
1249 		else
1250 			*val = lm90_get_temp11(data,
1251 					       lm90_temp_min_index[channel]);
1252 		break;
1253 	case hwmon_temp_max:
1254 		if (channel == 0)
1255 			*val = lm90_get_temp8(data,
1256 					      lm90_temp_max_index[channel]);
1257 		else
1258 			*val = lm90_get_temp11(data,
1259 					       lm90_temp_max_index[channel]);
1260 		break;
1261 	case hwmon_temp_crit:
1262 		*val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1263 		break;
1264 	case hwmon_temp_crit_hyst:
1265 		*val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1266 		break;
1267 	case hwmon_temp_emergency:
1268 		*val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1269 		break;
1270 	case hwmon_temp_emergency_hyst:
1271 		*val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1272 		break;
1273 	case hwmon_temp_offset:
1274 		*val = lm90_get_temp11(data, REMOTE_OFFSET);
1275 		break;
1276 	default:
1277 		return -EOPNOTSUPP;
1278 	}
1279 	return 0;
1280 }
1281 
1282 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1283 {
1284 	struct lm90_data *data = dev_get_drvdata(dev);
1285 	int err;
1286 
1287 	mutex_lock(&data->update_lock);
1288 
1289 	err = lm90_update_device(dev);
1290 	if (err)
1291 		goto error;
1292 
1293 	switch (attr) {
1294 	case hwmon_temp_min:
1295 		if (channel == 0)
1296 			err = lm90_set_temp8(data,
1297 					      lm90_temp_min_index[channel],
1298 					      val);
1299 		else
1300 			err = lm90_set_temp11(data,
1301 					      lm90_temp_min_index[channel],
1302 					      val);
1303 		break;
1304 	case hwmon_temp_max:
1305 		if (channel == 0)
1306 			err = lm90_set_temp8(data,
1307 					     lm90_temp_max_index[channel],
1308 					     val);
1309 		else
1310 			err = lm90_set_temp11(data,
1311 					      lm90_temp_max_index[channel],
1312 					      val);
1313 		break;
1314 	case hwmon_temp_crit:
1315 		err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1316 		break;
1317 	case hwmon_temp_crit_hyst:
1318 		err = lm90_set_temphyst(data, val);
1319 		break;
1320 	case hwmon_temp_emergency:
1321 		err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1322 		break;
1323 	case hwmon_temp_offset:
1324 		err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1325 		break;
1326 	default:
1327 		err = -EOPNOTSUPP;
1328 		break;
1329 	}
1330 error:
1331 	mutex_unlock(&data->update_lock);
1332 
1333 	return err;
1334 }
1335 
1336 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1337 {
1338 	switch (attr) {
1339 	case hwmon_temp_input:
1340 	case hwmon_temp_min_alarm:
1341 	case hwmon_temp_max_alarm:
1342 	case hwmon_temp_crit_alarm:
1343 	case hwmon_temp_emergency_alarm:
1344 	case hwmon_temp_emergency_hyst:
1345 	case hwmon_temp_fault:
1346 		return 0444;
1347 	case hwmon_temp_min:
1348 	case hwmon_temp_max:
1349 	case hwmon_temp_crit:
1350 	case hwmon_temp_emergency:
1351 	case hwmon_temp_offset:
1352 		return 0644;
1353 	case hwmon_temp_crit_hyst:
1354 		if (channel == 0)
1355 			return 0644;
1356 		return 0444;
1357 	default:
1358 		return 0;
1359 	}
1360 }
1361 
1362 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1363 {
1364 	struct lm90_data *data = dev_get_drvdata(dev);
1365 	int err;
1366 
1367 	mutex_lock(&data->update_lock);
1368 	err = lm90_update_device(dev);
1369 	mutex_unlock(&data->update_lock);
1370 	if (err)
1371 		return err;
1372 
1373 	switch (attr) {
1374 	case hwmon_chip_update_interval:
1375 		*val = data->update_interval;
1376 		break;
1377 	case hwmon_chip_alarms:
1378 		*val = data->alarms;
1379 		break;
1380 	default:
1381 		return -EOPNOTSUPP;
1382 	}
1383 
1384 	return 0;
1385 }
1386 
1387 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1388 {
1389 	struct lm90_data *data = dev_get_drvdata(dev);
1390 	struct i2c_client *client = data->client;
1391 	int err;
1392 
1393 	mutex_lock(&data->update_lock);
1394 
1395 	err = lm90_update_device(dev);
1396 	if (err)
1397 		goto error;
1398 
1399 	switch (attr) {
1400 	case hwmon_chip_update_interval:
1401 		err = lm90_set_convrate(client, data,
1402 					clamp_val(val, 0, 100000));
1403 		break;
1404 	default:
1405 		err = -EOPNOTSUPP;
1406 		break;
1407 	}
1408 error:
1409 	mutex_unlock(&data->update_lock);
1410 
1411 	return err;
1412 }
1413 
1414 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1415 {
1416 	switch (attr) {
1417 	case hwmon_chip_update_interval:
1418 		return 0644;
1419 	case hwmon_chip_alarms:
1420 		return 0444;
1421 	default:
1422 		return 0;
1423 	}
1424 }
1425 
1426 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1427 		     u32 attr, int channel, long *val)
1428 {
1429 	switch (type) {
1430 	case hwmon_chip:
1431 		return lm90_chip_read(dev, attr, channel, val);
1432 	case hwmon_temp:
1433 		return lm90_temp_read(dev, attr, channel, val);
1434 	default:
1435 		return -EOPNOTSUPP;
1436 	}
1437 }
1438 
1439 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1440 		      u32 attr, int channel, long val)
1441 {
1442 	switch (type) {
1443 	case hwmon_chip:
1444 		return lm90_chip_write(dev, attr, channel, val);
1445 	case hwmon_temp:
1446 		return lm90_temp_write(dev, attr, channel, val);
1447 	default:
1448 		return -EOPNOTSUPP;
1449 	}
1450 }
1451 
1452 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1453 			       u32 attr, int channel)
1454 {
1455 	switch (type) {
1456 	case hwmon_chip:
1457 		return lm90_chip_is_visible(data, attr, channel);
1458 	case hwmon_temp:
1459 		return lm90_temp_is_visible(data, attr, channel);
1460 	default:
1461 		return 0;
1462 	}
1463 }
1464 
1465 /* Return 0 if detection is successful, -ENODEV otherwise */
1466 static int lm90_detect(struct i2c_client *client,
1467 		       struct i2c_board_info *info)
1468 {
1469 	struct i2c_adapter *adapter = client->adapter;
1470 	int address = client->addr;
1471 	const char *name = NULL;
1472 	int man_id, chip_id, config1, config2, convrate;
1473 
1474 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1475 		return -ENODEV;
1476 
1477 	/* detection and identification */
1478 	man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1479 	chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1480 	config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1481 	convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1482 	if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1483 		return -ENODEV;
1484 
1485 	if (man_id == 0x01 || man_id == 0x5C || man_id == 0xA1) {
1486 		config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1487 		if (config2 < 0)
1488 			return -ENODEV;
1489 	}
1490 
1491 	if ((address == 0x4C || address == 0x4D)
1492 	 && man_id == 0x01) { /* National Semiconductor */
1493 		if ((config1 & 0x2A) == 0x00
1494 		 && (config2 & 0xF8) == 0x00
1495 		 && convrate <= 0x09) {
1496 			if (address == 0x4C
1497 			 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1498 				name = "lm90";
1499 			} else
1500 			if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1501 				name = "lm99";
1502 				dev_info(&adapter->dev,
1503 					 "Assuming LM99 chip at 0x%02x\n",
1504 					 address);
1505 				dev_info(&adapter->dev,
1506 					 "If it is an LM89, instantiate it "
1507 					 "with the new_device sysfs "
1508 					 "interface\n");
1509 			} else
1510 			if (address == 0x4C
1511 			 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1512 				name = "lm86";
1513 			}
1514 		}
1515 	} else
1516 	if ((address == 0x4C || address == 0x4D)
1517 	 && man_id == 0x41) { /* Analog Devices */
1518 		if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1519 		 && (config1 & 0x3F) == 0x00
1520 		 && convrate <= 0x0A) {
1521 			name = "adm1032";
1522 			/*
1523 			 * The ADM1032 supports PEC, but only if combined
1524 			 * transactions are not used.
1525 			 */
1526 			if (i2c_check_functionality(adapter,
1527 						    I2C_FUNC_SMBUS_BYTE))
1528 				info->flags |= I2C_CLIENT_PEC;
1529 		} else
1530 		if (chip_id == 0x51 /* ADT7461 */
1531 		 && (config1 & 0x1B) == 0x00
1532 		 && convrate <= 0x0A) {
1533 			name = "adt7461";
1534 		} else
1535 		if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1536 		 && (config1 & 0x1B) == 0x00
1537 		 && convrate <= 0x0A) {
1538 			name = "adt7461a";
1539 		}
1540 	} else
1541 	if (man_id == 0x4D) { /* Maxim */
1542 		int emerg, emerg2, status2;
1543 
1544 		/*
1545 		 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1546 		 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1547 		 * exists, both readings will reflect the same value. Otherwise,
1548 		 * the readings will be different.
1549 		 */
1550 		emerg = i2c_smbus_read_byte_data(client,
1551 						 MAX6659_REG_R_REMOTE_EMERG);
1552 		man_id = i2c_smbus_read_byte_data(client,
1553 						  LM90_REG_R_MAN_ID);
1554 		emerg2 = i2c_smbus_read_byte_data(client,
1555 						  MAX6659_REG_R_REMOTE_EMERG);
1556 		status2 = i2c_smbus_read_byte_data(client,
1557 						   MAX6696_REG_R_STATUS2);
1558 		if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
1559 			return -ENODEV;
1560 
1561 		/*
1562 		 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1563 		 * register. Reading from that address will return the last
1564 		 * read value, which in our case is those of the man_id
1565 		 * register. Likewise, the config1 register seems to lack a
1566 		 * low nibble, so the value will be those of the previous
1567 		 * read, so in our case those of the man_id register.
1568 		 * MAX6659 has a third set of upper temperature limit registers.
1569 		 * Those registers also return values on MAX6657 and MAX6658,
1570 		 * thus the only way to detect MAX6659 is by its address.
1571 		 * For this reason it will be mis-detected as MAX6657 if its
1572 		 * address is 0x4C.
1573 		 */
1574 		if (chip_id == man_id
1575 		 && (address == 0x4C || address == 0x4D || address == 0x4E)
1576 		 && (config1 & 0x1F) == (man_id & 0x0F)
1577 		 && convrate <= 0x09) {
1578 			if (address == 0x4C)
1579 				name = "max6657";
1580 			else
1581 				name = "max6659";
1582 		} else
1583 		/*
1584 		 * Even though MAX6695 and MAX6696 do not have a chip ID
1585 		 * register, reading it returns 0x01. Bit 4 of the config1
1586 		 * register is unused and should return zero when read. Bit 0 of
1587 		 * the status2 register is unused and should return zero when
1588 		 * read.
1589 		 *
1590 		 * MAX6695 and MAX6696 have an additional set of temperature
1591 		 * limit registers. We can detect those chips by checking if
1592 		 * one of those registers exists.
1593 		 */
1594 		if (chip_id == 0x01
1595 		 && (config1 & 0x10) == 0x00
1596 		 && (status2 & 0x01) == 0x00
1597 		 && emerg == emerg2
1598 		 && convrate <= 0x07) {
1599 			name = "max6696";
1600 		} else
1601 		/*
1602 		 * The chip_id register of the MAX6680 and MAX6681 holds the
1603 		 * revision of the chip. The lowest bit of the config1 register
1604 		 * is unused and should return zero when read, so should the
1605 		 * second to last bit of config1 (software reset).
1606 		 */
1607 		if (chip_id == 0x01
1608 		 && (config1 & 0x03) == 0x00
1609 		 && convrate <= 0x07) {
1610 			name = "max6680";
1611 		} else
1612 		/*
1613 		 * The chip_id register of the MAX6646/6647/6649 holds the
1614 		 * revision of the chip. The lowest 6 bits of the config1
1615 		 * register are unused and should return zero when read.
1616 		 */
1617 		if (chip_id == 0x59
1618 		 && (config1 & 0x3f) == 0x00
1619 		 && convrate <= 0x07) {
1620 			name = "max6646";
1621 		} else
1622 		/*
1623 		 * The chip_id of the MAX6654 holds the revision of the chip.
1624 		 * The lowest 3 bits of the config1 register are unused and
1625 		 * should return zero when read.
1626 		 */
1627 		if (chip_id == 0x08
1628 		 && (config1 & 0x07) == 0x00
1629 		 && convrate <= 0x07) {
1630 			name = "max6654";
1631 		}
1632 	} else
1633 	if (address == 0x4C
1634 	 && man_id == 0x5C) { /* Winbond/Nuvoton */
1635 		if ((config1 & 0x2A) == 0x00
1636 		 && (config2 & 0xF8) == 0x00) {
1637 			if (chip_id == 0x01 /* W83L771W/G */
1638 			 && convrate <= 0x09) {
1639 				name = "w83l771";
1640 			} else
1641 			if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1642 			 && convrate <= 0x08) {
1643 				name = "w83l771";
1644 			}
1645 		}
1646 	} else
1647 	if (address >= 0x48 && address <= 0x4F
1648 	 && man_id == 0xA1) { /*  NXP Semiconductor/Philips */
1649 		if (chip_id == 0x00
1650 		 && (config1 & 0x2A) == 0x00
1651 		 && (config2 & 0xFE) == 0x00
1652 		 && convrate <= 0x09) {
1653 			name = "sa56004";
1654 		}
1655 	} else
1656 	if ((address == 0x4C || address == 0x4D)
1657 	 && man_id == 0x47) { /* GMT */
1658 		if (chip_id == 0x01 /* G781 */
1659 		 && (config1 & 0x3F) == 0x00
1660 		 && convrate <= 0x08)
1661 			name = "g781";
1662 	} else
1663 	if (man_id == 0x55 && chip_id == 0x00 &&
1664 	    (config1 & 0x1B) == 0x00 && convrate <= 0x09) {
1665 		int local_ext, conalert, chen, dfc;
1666 
1667 		local_ext = i2c_smbus_read_byte_data(client,
1668 						     TMP451_REG_R_LOCAL_TEMPL);
1669 		conalert = i2c_smbus_read_byte_data(client,
1670 						    TMP451_REG_CONALERT);
1671 		chen = i2c_smbus_read_byte_data(client, TMP461_REG_CHEN);
1672 		dfc = i2c_smbus_read_byte_data(client, TMP461_REG_DFC);
1673 
1674 		if ((local_ext & 0x0F) == 0x00 &&
1675 		    (conalert & 0xf1) == 0x01 &&
1676 		    (chen & 0xfc) == 0x00 &&
1677 		    (dfc & 0xfc) == 0x00) {
1678 			if (address == 0x4c && !(chen & 0x03))
1679 				name = "tmp451";
1680 			else if (address >= 0x48 && address <= 0x4f)
1681 				name = "tmp461";
1682 		}
1683 	}
1684 
1685 	if (!name) { /* identification failed */
1686 		dev_dbg(&adapter->dev,
1687 			"Unsupported chip at 0x%02x (man_id=0x%02X, "
1688 			"chip_id=0x%02X)\n", address, man_id, chip_id);
1689 		return -ENODEV;
1690 	}
1691 
1692 	strlcpy(info->type, name, I2C_NAME_SIZE);
1693 
1694 	return 0;
1695 }
1696 
1697 static void lm90_restore_conf(void *_data)
1698 {
1699 	struct lm90_data *data = _data;
1700 	struct i2c_client *client = data->client;
1701 
1702 	/* Restore initial configuration */
1703 	lm90_write_convrate(data, data->convrate_orig);
1704 	i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1705 				  data->config_orig);
1706 }
1707 
1708 static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
1709 {
1710 	struct device_node *np = client->dev.of_node;
1711 	int config, convrate;
1712 
1713 	convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1714 	if (convrate < 0)
1715 		return convrate;
1716 	data->convrate_orig = convrate;
1717 
1718 	/*
1719 	 * Start the conversions.
1720 	 */
1721 	config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1722 	if (config < 0)
1723 		return config;
1724 	data->config_orig = config;
1725 	data->config = config;
1726 
1727 	lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1728 
1729 	/* Check Temperature Range Select */
1730 	if (data->flags & LM90_HAVE_EXTENDED_TEMP) {
1731 		if (of_property_read_bool(np, "ti,extended-range-enable"))
1732 			config |= 0x04;
1733 
1734 		if (config & 0x04)
1735 			data->flags |= LM90_FLAG_ADT7461_EXT;
1736 	}
1737 
1738 	/*
1739 	 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1740 	 * 0.125 degree resolution) and range (0x08, extend range
1741 	 * to -64 degree) mode for the remote temperature sensor.
1742 	 */
1743 	if (data->kind == max6680)
1744 		config |= 0x18;
1745 
1746 	/*
1747 	 * Put MAX6654 into extended range (0x20, extend minimum range from
1748 	 * 0 degrees to -64 degrees). Note that extended resolution is not
1749 	 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
1750 	 * slower, which is intentionally not done by default.
1751 	 */
1752 	if (data->kind == max6654)
1753 		config |= 0x20;
1754 
1755 	/*
1756 	 * Select external channel 0 for max6695/96
1757 	 */
1758 	if (data->kind == max6696)
1759 		config &= ~0x08;
1760 
1761 	/*
1762 	 * Interrupt is enabled by default on reset, but it may be disabled
1763 	 * by bootloader, unmask it.
1764 	 */
1765 	if (client->irq)
1766 		config &= ~0x80;
1767 
1768 	config &= 0xBF;	/* run */
1769 	lm90_update_confreg(data, config);
1770 
1771 	return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
1772 }
1773 
1774 static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1775 {
1776 	struct lm90_data *data = i2c_get_clientdata(client);
1777 	int st, st2 = 0;
1778 
1779 	st = lm90_read_reg(client, LM90_REG_R_STATUS);
1780 	if (st < 0)
1781 		return false;
1782 
1783 	if (data->kind == max6696) {
1784 		st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1785 		if (st2 < 0)
1786 			return false;
1787 	}
1788 
1789 	*status = st | (st2 << 8);
1790 
1791 	if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1792 		return false;
1793 
1794 	if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1795 	    (st2 & MAX6696_STATUS2_LOT2))
1796 		dev_dbg(&client->dev,
1797 			"temp%d out of range, please check!\n", 1);
1798 	if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1799 	    (st2 & MAX6696_STATUS2_ROT2))
1800 		dev_dbg(&client->dev,
1801 			"temp%d out of range, please check!\n", 2);
1802 	if (st & LM90_STATUS_ROPEN)
1803 		dev_dbg(&client->dev,
1804 			"temp%d diode open, please check!\n", 2);
1805 	if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1806 		   MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1807 		dev_dbg(&client->dev,
1808 			"temp%d out of range, please check!\n", 3);
1809 	if (st2 & MAX6696_STATUS2_R2OPEN)
1810 		dev_dbg(&client->dev,
1811 			"temp%d diode open, please check!\n", 3);
1812 
1813 	if (st & LM90_STATUS_LLOW)
1814 		hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1815 				   hwmon_temp_min_alarm, 0);
1816 	if (st & LM90_STATUS_RLOW)
1817 		hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1818 				   hwmon_temp_min_alarm, 1);
1819 	if (st2 & MAX6696_STATUS2_R2LOW)
1820 		hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1821 				   hwmon_temp_min_alarm, 2);
1822 	if (st & LM90_STATUS_LHIGH)
1823 		hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1824 				   hwmon_temp_max_alarm, 0);
1825 	if (st & LM90_STATUS_RHIGH)
1826 		hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1827 				   hwmon_temp_max_alarm, 1);
1828 	if (st2 & MAX6696_STATUS2_R2HIGH)
1829 		hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1830 				   hwmon_temp_max_alarm, 2);
1831 
1832 	return true;
1833 }
1834 
1835 static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1836 {
1837 	struct i2c_client *client = dev_id;
1838 	u16 status;
1839 
1840 	if (lm90_is_tripped(client, &status))
1841 		return IRQ_HANDLED;
1842 	else
1843 		return IRQ_NONE;
1844 }
1845 
1846 static void lm90_remove_pec(void *dev)
1847 {
1848 	device_remove_file(dev, &dev_attr_pec);
1849 }
1850 
1851 static void lm90_regulator_disable(void *regulator)
1852 {
1853 	regulator_disable(regulator);
1854 }
1855 
1856 
1857 static const struct hwmon_ops lm90_ops = {
1858 	.is_visible = lm90_is_visible,
1859 	.read = lm90_read,
1860 	.write = lm90_write,
1861 };
1862 
1863 static int lm90_probe(struct i2c_client *client)
1864 {
1865 	struct device *dev = &client->dev;
1866 	struct i2c_adapter *adapter = client->adapter;
1867 	struct hwmon_channel_info *info;
1868 	struct regulator *regulator;
1869 	struct device *hwmon_dev;
1870 	struct lm90_data *data;
1871 	int err;
1872 
1873 	regulator = devm_regulator_get(dev, "vcc");
1874 	if (IS_ERR(regulator))
1875 		return PTR_ERR(regulator);
1876 
1877 	err = regulator_enable(regulator);
1878 	if (err < 0) {
1879 		dev_err(dev, "Failed to enable regulator: %d\n", err);
1880 		return err;
1881 	}
1882 
1883 	err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1884 	if (err)
1885 		return err;
1886 
1887 	data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
1888 	if (!data)
1889 		return -ENOMEM;
1890 
1891 	data->client = client;
1892 	i2c_set_clientdata(client, data);
1893 	mutex_init(&data->update_lock);
1894 
1895 	/* Set the device type */
1896 	if (client->dev.of_node)
1897 		data->kind = (enum chips)of_device_get_match_data(&client->dev);
1898 	else
1899 		data->kind = i2c_match_id(lm90_id, client)->driver_data;
1900 	if (data->kind == adm1032) {
1901 		if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1902 			client->flags &= ~I2C_CLIENT_PEC;
1903 	}
1904 
1905 	/*
1906 	 * Different devices have different alarm bits triggering the
1907 	 * ALERT# output
1908 	 */
1909 	data->alert_alarms = lm90_params[data->kind].alert_alarms;
1910 
1911 	/* Set chip capabilities */
1912 	data->flags = lm90_params[data->kind].flags;
1913 
1914 	data->chip.ops = &lm90_ops;
1915 	data->chip.info = data->info;
1916 
1917 	data->info[0] = HWMON_CHANNEL_INFO(chip,
1918 		HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
1919 	data->info[1] = &data->temp_info;
1920 
1921 	info = &data->temp_info;
1922 	info->type = hwmon_temp;
1923 	info->config = data->channel_config;
1924 
1925 	data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1926 		HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM;
1927 	data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1928 		HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM | HWMON_T_FAULT;
1929 
1930 	if (data->flags & LM90_HAVE_CRIT) {
1931 		data->channel_config[0] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
1932 		data->channel_config[1] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
1933 	}
1934 
1935 	if (data->flags & LM90_HAVE_OFFSET)
1936 		data->channel_config[1] |= HWMON_T_OFFSET;
1937 
1938 	if (data->flags & LM90_HAVE_EMERGENCY) {
1939 		data->channel_config[0] |= HWMON_T_EMERGENCY |
1940 			HWMON_T_EMERGENCY_HYST;
1941 		data->channel_config[1] |= HWMON_T_EMERGENCY |
1942 			HWMON_T_EMERGENCY_HYST;
1943 	}
1944 
1945 	if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1946 		data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1947 		data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1948 	}
1949 
1950 	if (data->flags & LM90_HAVE_TEMP3) {
1951 		data->channel_config[2] = HWMON_T_INPUT |
1952 			HWMON_T_MIN | HWMON_T_MAX |
1953 			HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1954 			HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1955 			HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1956 			HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1957 			HWMON_T_FAULT;
1958 	}
1959 
1960 	data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1961 
1962 	/* Set maximum conversion rate */
1963 	data->max_convrate = lm90_params[data->kind].max_convrate;
1964 
1965 	/* Initialize the LM90 chip */
1966 	err = lm90_init_client(client, data);
1967 	if (err < 0) {
1968 		dev_err(dev, "Failed to initialize device\n");
1969 		return err;
1970 	}
1971 
1972 	/*
1973 	 * The 'pec' attribute is attached to the i2c device and thus created
1974 	 * separately.
1975 	 */
1976 	if (client->flags & I2C_CLIENT_PEC) {
1977 		err = device_create_file(dev, &dev_attr_pec);
1978 		if (err)
1979 			return err;
1980 		err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1981 		if (err)
1982 			return err;
1983 	}
1984 
1985 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1986 							 data, &data->chip,
1987 							 NULL);
1988 	if (IS_ERR(hwmon_dev))
1989 		return PTR_ERR(hwmon_dev);
1990 
1991 	data->hwmon_dev = hwmon_dev;
1992 
1993 	if (client->irq) {
1994 		dev_dbg(dev, "IRQ: %d\n", client->irq);
1995 		err = devm_request_threaded_irq(dev, client->irq,
1996 						NULL, lm90_irq_thread,
1997 						IRQF_ONESHOT, "lm90", client);
1998 		if (err < 0) {
1999 			dev_err(dev, "cannot request IRQ %d\n", client->irq);
2000 			return err;
2001 		}
2002 	}
2003 
2004 	return 0;
2005 }
2006 
2007 static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
2008 		       unsigned int flag)
2009 {
2010 	u16 alarms;
2011 
2012 	if (type != I2C_PROTOCOL_SMBUS_ALERT)
2013 		return;
2014 
2015 	if (lm90_is_tripped(client, &alarms)) {
2016 		/*
2017 		 * Disable ALERT# output, because these chips don't implement
2018 		 * SMBus alert correctly; they should only hold the alert line
2019 		 * low briefly.
2020 		 */
2021 		struct lm90_data *data = i2c_get_clientdata(client);
2022 
2023 		if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
2024 		    (alarms & data->alert_alarms)) {
2025 			dev_dbg(&client->dev, "Disabling ALERT#\n");
2026 			lm90_update_confreg(data, data->config | 0x80);
2027 		}
2028 	} else {
2029 		dev_dbg(&client->dev, "Everything OK\n");
2030 	}
2031 }
2032 
2033 static int __maybe_unused lm90_suspend(struct device *dev)
2034 {
2035 	struct lm90_data *data = dev_get_drvdata(dev);
2036 	struct i2c_client *client = data->client;
2037 
2038 	if (client->irq)
2039 		disable_irq(client->irq);
2040 
2041 	return 0;
2042 }
2043 
2044 static int __maybe_unused lm90_resume(struct device *dev)
2045 {
2046 	struct lm90_data *data = dev_get_drvdata(dev);
2047 	struct i2c_client *client = data->client;
2048 
2049 	if (client->irq)
2050 		enable_irq(client->irq);
2051 
2052 	return 0;
2053 }
2054 
2055 static SIMPLE_DEV_PM_OPS(lm90_pm_ops, lm90_suspend, lm90_resume);
2056 
2057 static struct i2c_driver lm90_driver = {
2058 	.class		= I2C_CLASS_HWMON,
2059 	.driver = {
2060 		.name	= "lm90",
2061 		.of_match_table = of_match_ptr(lm90_of_match),
2062 		.pm	= &lm90_pm_ops,
2063 	},
2064 	.probe_new	= lm90_probe,
2065 	.alert		= lm90_alert,
2066 	.id_table	= lm90_id,
2067 	.detect		= lm90_detect,
2068 	.address_list	= normal_i2c,
2069 };
2070 
2071 module_i2c_driver(lm90_driver);
2072 
2073 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
2074 MODULE_DESCRIPTION("LM90/ADM1032 driver");
2075 MODULE_LICENSE("GPL");
2076