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