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