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