GNU Linux-libre 4.9.294-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
200 #define MAX6696_STATUS2_R2THRM  (1 << 1) /* remote2 THERM limit tripped */
201 #define MAX6696_STATUS2_R2OPEN  (1 << 2) /* remote2 is an open circuit */
202 #define MAX6696_STATUS2_R2LOW   (1 << 3) /* remote2 low temp limit tripped */
203 #define MAX6696_STATUS2_R2HIGH  (1 << 4) /* remote2 high temp limit tripped */
204 #define MAX6696_STATUS2_ROT2    (1 << 5) /* remote emergency limit tripped */
205 #define MAX6696_STATUS2_R2OT2   (1 << 6) /* remote2 emergency limit tripped */
206 #define MAX6696_STATUS2_LOT2    (1 << 7) /* local emergency limit tripped */
207
208 /*
209  * Driver data (common to all clients)
210  */
211
212 static const struct i2c_device_id lm90_id[] = {
213         { "adm1032", adm1032 },
214         { "adt7461", adt7461 },
215         { "adt7461a", adt7461 },
216         { "g781", g781 },
217         { "lm90", lm90 },
218         { "lm86", lm86 },
219         { "lm89", lm86 },
220         { "lm99", lm99 },
221         { "max6646", max6646 },
222         { "max6647", max6646 },
223         { "max6649", max6646 },
224         { "max6657", max6657 },
225         { "max6658", max6657 },
226         { "max6659", max6659 },
227         { "max6680", max6680 },
228         { "max6681", max6680 },
229         { "max6695", max6696 },
230         { "max6696", max6696 },
231         { "nct1008", adt7461 },
232         { "w83l771", w83l771 },
233         { "sa56004", sa56004 },
234         { "tmp451", tmp451 },
235         { }
236 };
237 MODULE_DEVICE_TABLE(i2c, lm90_id);
238
239 /*
240  * chip type specific parameters
241  */
242 struct lm90_params {
243         u32 flags;              /* Capabilities */
244         u16 alert_alarms;       /* Which alarm bits trigger ALERT# */
245                                 /* Upper 8 bits for max6695/96 */
246         u8 max_convrate;        /* Maximum conversion rate register value */
247         u8 reg_local_ext;       /* Extended local temp register (optional) */
248 };
249
250 static const struct lm90_params lm90_params[] = {
251         [adm1032] = {
252                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
253                   | LM90_HAVE_BROKEN_ALERT,
254                 .alert_alarms = 0x7c,
255                 .max_convrate = 10,
256         },
257         [adt7461] = {
258                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
259                   | LM90_HAVE_BROKEN_ALERT,
260                 .alert_alarms = 0x7c,
261                 .max_convrate = 10,
262         },
263         [g781] = {
264                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
265                   | LM90_HAVE_BROKEN_ALERT,
266                 .alert_alarms = 0x7c,
267                 .max_convrate = 8,
268         },
269         [lm86] = {
270                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
271                 .alert_alarms = 0x7b,
272                 .max_convrate = 9,
273         },
274         [lm90] = {
275                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
276                 .alert_alarms = 0x7b,
277                 .max_convrate = 9,
278         },
279         [lm99] = {
280                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
281                 .alert_alarms = 0x7b,
282                 .max_convrate = 9,
283         },
284         [max6646] = {
285                 .alert_alarms = 0x7c,
286                 .max_convrate = 6,
287                 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
288         },
289         [max6657] = {
290                 .flags = LM90_PAUSE_FOR_CONFIG,
291                 .alert_alarms = 0x7c,
292                 .max_convrate = 8,
293                 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
294         },
295         [max6659] = {
296                 .flags = LM90_HAVE_EMERGENCY,
297                 .alert_alarms = 0x7c,
298                 .max_convrate = 8,
299                 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
300         },
301         [max6680] = {
302                 .flags = LM90_HAVE_OFFSET,
303                 .alert_alarms = 0x7c,
304                 .max_convrate = 7,
305         },
306         [max6696] = {
307                 .flags = LM90_HAVE_EMERGENCY
308                   | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
309                 .alert_alarms = 0x1c7c,
310                 .max_convrate = 6,
311                 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
312         },
313         [w83l771] = {
314                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
315                 .alert_alarms = 0x7c,
316                 .max_convrate = 8,
317         },
318         [sa56004] = {
319                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
320                 .alert_alarms = 0x7b,
321                 .max_convrate = 9,
322                 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
323         },
324         [tmp451] = {
325                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
326                   | LM90_HAVE_BROKEN_ALERT,
327                 .alert_alarms = 0x7c,
328                 .max_convrate = 9,
329                 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
330         },
331 };
332
333 /*
334  * TEMP8 register index
335  */
336 enum lm90_temp8_reg_index {
337         LOCAL_LOW = 0,
338         LOCAL_HIGH,
339         LOCAL_CRIT,
340         REMOTE_CRIT,
341         LOCAL_EMERG,    /* max6659 and max6695/96 */
342         REMOTE_EMERG,   /* max6659 and max6695/96 */
343         REMOTE2_CRIT,   /* max6695/96 only */
344         REMOTE2_EMERG,  /* max6695/96 only */
345         TEMP8_REG_NUM
346 };
347
348 /*
349  * TEMP11 register index
350  */
351 enum lm90_temp11_reg_index {
352         REMOTE_TEMP = 0,
353         REMOTE_LOW,
354         REMOTE_HIGH,
355         REMOTE_OFFSET,  /* except max6646, max6657/58/59, and max6695/96 */
356         LOCAL_TEMP,
357         REMOTE2_TEMP,   /* max6695/96 only */
358         REMOTE2_LOW,    /* max6695/96 only */
359         REMOTE2_HIGH,   /* max6695/96 only */
360         TEMP11_REG_NUM
361 };
362
363 /*
364  * Client data (each client gets its own)
365  */
366
367 struct lm90_data {
368         struct i2c_client *client;
369         u32 channel_config[4];
370         struct hwmon_channel_info temp_info;
371         const struct hwmon_channel_info *info[3];
372         struct hwmon_chip_info chip;
373         struct mutex update_lock;
374         bool valid;             /* true if register values are valid */
375         unsigned long last_updated; /* in jiffies */
376         int kind;
377         u32 flags;
378
379         unsigned int update_interval; /* in milliseconds */
380
381         u8 config_orig;         /* Original configuration register value */
382         u8 convrate_orig;       /* Original conversion rate register value */
383         u16 alert_alarms;       /* Which alarm bits trigger ALERT# */
384                                 /* Upper 8 bits for max6695/96 */
385         u8 max_convrate;        /* Maximum conversion rate */
386         u8 reg_local_ext;       /* local extension register offset */
387
388         /* registers values */
389         s8 temp8[TEMP8_REG_NUM];
390         s16 temp11[TEMP11_REG_NUM];
391         u8 temp_hyst;
392         u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
393 };
394
395 /*
396  * Support functions
397  */
398
399 /*
400  * The ADM1032 supports PEC but not on write byte transactions, so we need
401  * to explicitly ask for a transaction without PEC.
402  */
403 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
404 {
405         return i2c_smbus_xfer(client->adapter, client->addr,
406                               client->flags & ~I2C_CLIENT_PEC,
407                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
408 }
409
410 /*
411  * It is assumed that client->update_lock is held (unless we are in
412  * detection or initialization steps). This matters when PEC is enabled,
413  * because we don't want the address pointer to change between the write
414  * byte and the read byte transactions.
415  */
416 static int lm90_read_reg(struct i2c_client *client, u8 reg)
417 {
418         int err;
419
420         if (client->flags & I2C_CLIENT_PEC) {
421                 err = adm1032_write_byte(client, reg);
422                 if (err >= 0)
423                         err = i2c_smbus_read_byte(client);
424         } else
425                 err = i2c_smbus_read_byte_data(client, reg);
426
427         return err;
428 }
429
430 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
431 {
432         int oldh, newh, l;
433
434         /*
435          * There is a trick here. We have to read two registers to have the
436          * sensor temperature, but we have to beware a conversion could occur
437          * between the readings. The datasheet says we should either use
438          * the one-shot conversion register, which we don't want to do
439          * (disables hardware monitoring) or monitor the busy bit, which is
440          * impossible (we can't read the values and monitor that bit at the
441          * exact same time). So the solution used here is to read the high
442          * byte once, then the low byte, then the high byte again. If the new
443          * high byte matches the old one, then we have a valid reading. Else
444          * we have to read the low byte again, and now we believe we have a
445          * correct reading.
446          */
447         oldh = lm90_read_reg(client, regh);
448         if (oldh < 0)
449                 return oldh;
450         l = lm90_read_reg(client, regl);
451         if (l < 0)
452                 return l;
453         newh = lm90_read_reg(client, regh);
454         if (newh < 0)
455                 return newh;
456         if (oldh != newh) {
457                 l = lm90_read_reg(client, regl);
458                 if (l < 0)
459                         return l;
460         }
461         return (newh << 8) | l;
462 }
463
464 /*
465  * client->update_lock must be held when calling this function (unless we are
466  * in detection or initialization steps), and while a remote channel other
467  * than channel 0 is selected. Also, calling code must make sure to re-select
468  * external channel 0 before releasing the lock. This is necessary because
469  * various registers have different meanings as a result of selecting a
470  * non-default remote channel.
471  */
472 static inline int lm90_select_remote_channel(struct i2c_client *client,
473                                              struct lm90_data *data,
474                                              int channel)
475 {
476         int config;
477
478         if (data->kind == max6696) {
479                 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
480                 if (config < 0)
481                         return config;
482                 config &= ~0x08;
483                 if (channel)
484                         config |= 0x08;
485                 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
486                                           config);
487         }
488         return 0;
489 }
490
491 static int lm90_write_convrate(struct i2c_client *client,
492                                struct lm90_data *data, int val)
493 {
494         int err;
495         int config_orig, config_stop;
496
497         /* Save config and pause conversion */
498         if (data->flags & LM90_PAUSE_FOR_CONFIG) {
499                 config_orig = lm90_read_reg(client, LM90_REG_R_CONFIG1);
500                 if (config_orig < 0)
501                         return config_orig;
502                 config_stop = config_orig | 0x40;
503                 if (config_orig != config_stop) {
504                         err = i2c_smbus_write_byte_data(client,
505                                                         LM90_REG_W_CONFIG1,
506                                                         config_stop);
507                         if (err < 0)
508                                 return err;
509                 }
510         }
511
512         /* Set conv rate */
513         err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, val);
514
515         /* Revert change to config */
516         if (data->flags & LM90_PAUSE_FOR_CONFIG && config_orig != config_stop)
517                 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
518                                           config_orig);
519
520         return err;
521 }
522
523 /*
524  * Set conversion rate.
525  * client->update_lock must be held when calling this function (unless we are
526  * in detection or initialization steps).
527  */
528 static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
529                              unsigned int interval)
530 {
531         unsigned int update_interval;
532         int i, err;
533
534         /* Shift calculations to avoid rounding errors */
535         interval <<= 6;
536
537         /* find the nearest update rate */
538         for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
539              i < data->max_convrate; i++, update_interval >>= 1)
540                 if (interval >= update_interval * 3 / 4)
541                         break;
542
543         err = lm90_write_convrate(client, data, i);
544         data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
545         return err;
546 }
547
548 static int lm90_update_limits(struct device *dev)
549 {
550         struct lm90_data *data = dev_get_drvdata(dev);
551         struct i2c_client *client = data->client;
552         int val;
553
554         val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
555         if (val < 0)
556                 return val;
557         data->temp8[LOCAL_CRIT] = val;
558
559         val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
560         if (val < 0)
561                 return val;
562         data->temp8[REMOTE_CRIT] = val;
563
564         val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
565         if (val < 0)
566                 return val;
567         data->temp_hyst = val;
568
569         val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
570         if (val < 0)
571                 return val;
572         data->temp11[REMOTE_LOW] = val << 8;
573
574         if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
575                 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
576                 if (val < 0)
577                         return val;
578                 data->temp11[REMOTE_LOW] |= val;
579         }
580
581         val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
582         if (val < 0)
583                 return val;
584         data->temp11[REMOTE_HIGH] = val << 8;
585
586         if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
587                 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
588                 if (val < 0)
589                         return val;
590                 data->temp11[REMOTE_HIGH] |= val;
591         }
592
593         if (data->flags & LM90_HAVE_OFFSET) {
594                 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
595                                   LM90_REG_R_REMOTE_OFFSL);
596                 if (val < 0)
597                         return val;
598                 data->temp11[REMOTE_OFFSET] = val;
599         }
600
601         if (data->flags & LM90_HAVE_EMERGENCY) {
602                 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
603                 if (val < 0)
604                         return val;
605                 data->temp8[LOCAL_EMERG] = val;
606
607                 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
608                 if (val < 0)
609                         return val;
610                 data->temp8[REMOTE_EMERG] = val;
611         }
612
613         if (data->kind == max6696) {
614                 val = lm90_select_remote_channel(client, data, 1);
615                 if (val < 0)
616                         return val;
617
618                 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
619                 if (val < 0)
620                         return val;
621                 data->temp8[REMOTE2_CRIT] = val;
622
623                 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
624                 if (val < 0)
625                         return val;
626                 data->temp8[REMOTE2_EMERG] = val;
627
628                 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
629                 if (val < 0)
630                         return val;
631                 data->temp11[REMOTE2_LOW] = val << 8;
632
633                 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
634                 if (val < 0)
635                         return val;
636                 data->temp11[REMOTE2_HIGH] = val << 8;
637
638                 lm90_select_remote_channel(client, data, 0);
639         }
640
641         return 0;
642 }
643
644 static int lm90_update_device(struct device *dev)
645 {
646         struct lm90_data *data = dev_get_drvdata(dev);
647         struct i2c_client *client = data->client;
648         unsigned long next_update;
649         int val;
650
651         if (!data->valid) {
652                 val = lm90_update_limits(dev);
653                 if (val < 0)
654                         return val;
655         }
656
657         next_update = data->last_updated +
658                       msecs_to_jiffies(data->update_interval);
659         if (time_after(jiffies, next_update) || !data->valid) {
660                 dev_dbg(&client->dev, "Updating lm90 data.\n");
661
662                 data->valid = false;
663
664                 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
665                 if (val < 0)
666                         return val;
667                 data->temp8[LOCAL_LOW] = val;
668
669                 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
670                 if (val < 0)
671                         return val;
672                 data->temp8[LOCAL_HIGH] = val;
673
674                 if (data->reg_local_ext) {
675                         val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
676                                           data->reg_local_ext);
677                         if (val < 0)
678                                 return val;
679                         data->temp11[LOCAL_TEMP] = val;
680                 } else {
681                         val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
682                         if (val < 0)
683                                 return val;
684                         data->temp11[LOCAL_TEMP] = val << 8;
685                 }
686                 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
687                                   LM90_REG_R_REMOTE_TEMPL);
688                 if (val < 0)
689                         return val;
690                 data->temp11[REMOTE_TEMP] = val;
691
692                 val = lm90_read_reg(client, LM90_REG_R_STATUS);
693                 if (val < 0)
694                         return val;
695                 data->alarms = val;     /* lower 8 bit of alarms */
696
697                 if (data->kind == max6696) {
698                         val = lm90_select_remote_channel(client, data, 1);
699                         if (val < 0)
700                                 return val;
701
702                         val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
703                                           LM90_REG_R_REMOTE_TEMPL);
704                         if (val < 0) {
705                                 lm90_select_remote_channel(client, data, 0);
706                                 return val;
707                         }
708                         data->temp11[REMOTE2_TEMP] = val;
709
710                         lm90_select_remote_channel(client, data, 0);
711
712                         val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
713                         if (val < 0)
714                                 return val;
715                         data->alarms |= val << 8;
716                 }
717
718                 /*
719                  * Re-enable ALERT# output if it was originally enabled and
720                  * relevant alarms are all clear
721                  */
722                 if (!(data->config_orig & 0x80) &&
723                     !(data->alarms & data->alert_alarms)) {
724                         val = lm90_read_reg(client, LM90_REG_R_CONFIG1);
725                         if (val < 0)
726                                 return val;
727
728                         if (val & 0x80) {
729                                 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
730                                 i2c_smbus_write_byte_data(client,
731                                                           LM90_REG_W_CONFIG1,
732                                                           val & ~0x80);
733                         }
734                 }
735
736                 data->last_updated = jiffies;
737                 data->valid = true;
738         }
739
740         return 0;
741 }
742
743 /*
744  * Conversions
745  * For local temperatures and limits, critical limits and the hysteresis
746  * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
747  * For remote temperatures and limits, it uses signed 11-bit values with
748  * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some
749  * Maxim chips use unsigned values.
750  */
751
752 static inline int temp_from_s8(s8 val)
753 {
754         return val * 1000;
755 }
756
757 static inline int temp_from_u8(u8 val)
758 {
759         return val * 1000;
760 }
761
762 static inline int temp_from_s16(s16 val)
763 {
764         return val / 32 * 125;
765 }
766
767 static inline int temp_from_u16(u16 val)
768 {
769         return val / 32 * 125;
770 }
771
772 static s8 temp_to_s8(long val)
773 {
774         if (val <= -128000)
775                 return -128;
776         if (val >= 127000)
777                 return 127;
778         if (val < 0)
779                 return (val - 500) / 1000;
780         return (val + 500) / 1000;
781 }
782
783 static u8 temp_to_u8(long val)
784 {
785         if (val <= 0)
786                 return 0;
787         if (val >= 255000)
788                 return 255;
789         return (val + 500) / 1000;
790 }
791
792 static s16 temp_to_s16(long val)
793 {
794         if (val <= -128000)
795                 return 0x8000;
796         if (val >= 127875)
797                 return 0x7FE0;
798         if (val < 0)
799                 return (val - 62) / 125 * 32;
800         return (val + 62) / 125 * 32;
801 }
802
803 static u8 hyst_to_reg(long val)
804 {
805         if (val <= 0)
806                 return 0;
807         if (val >= 30500)
808                 return 31;
809         return (val + 500) / 1000;
810 }
811
812 /*
813  * ADT7461 in compatibility mode is almost identical to LM90 except that
814  * attempts to write values that are outside the range 0 < temp < 127 are
815  * treated as the boundary value.
816  *
817  * ADT7461 in "extended mode" operation uses unsigned integers offset by
818  * 64 (e.g., 0 -> -64 degC).  The range is restricted to -64..191 degC.
819  */
820 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
821 {
822         if (data->flags & LM90_FLAG_ADT7461_EXT)
823                 return (val - 64) * 1000;
824         return temp_from_s8(val);
825 }
826
827 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
828 {
829         if (data->flags & LM90_FLAG_ADT7461_EXT)
830                 return (val - 0x4000) / 64 * 250;
831         return temp_from_s16(val);
832 }
833
834 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
835 {
836         if (data->flags & LM90_FLAG_ADT7461_EXT) {
837                 if (val <= -64000)
838                         return 0;
839                 if (val >= 191000)
840                         return 0xFF;
841                 return (val + 500 + 64000) / 1000;
842         }
843         if (val <= 0)
844                 return 0;
845         if (val >= 127000)
846                 return 127;
847         return (val + 500) / 1000;
848 }
849
850 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
851 {
852         if (data->flags & LM90_FLAG_ADT7461_EXT) {
853                 if (val <= -64000)
854                         return 0;
855                 if (val >= 191750)
856                         return 0xFFC0;
857                 return (val + 64000 + 125) / 250 * 64;
858         }
859         if (val <= 0)
860                 return 0;
861         if (val >= 127750)
862                 return 0x7FC0;
863         return (val + 125) / 250 * 64;
864 }
865
866 /* pec used for ADM1032 only */
867 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
868                         char *buf)
869 {
870         struct i2c_client *client = to_i2c_client(dev);
871
872         return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
873 }
874
875 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
876                        const char *buf, size_t count)
877 {
878         struct i2c_client *client = to_i2c_client(dev);
879         long val;
880         int err;
881
882         err = kstrtol(buf, 10, &val);
883         if (err < 0)
884                 return err;
885
886         switch (val) {
887         case 0:
888                 client->flags &= ~I2C_CLIENT_PEC;
889                 break;
890         case 1:
891                 client->flags |= I2C_CLIENT_PEC;
892                 break;
893         default:
894                 return -EINVAL;
895         }
896
897         return count;
898 }
899
900 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
901
902 static int lm90_get_temp11(struct lm90_data *data, int index)
903 {
904         s16 temp11 = data->temp11[index];
905         int temp;
906
907         if (data->kind == adt7461 || data->kind == tmp451)
908                 temp = temp_from_u16_adt7461(data, temp11);
909         else if (data->kind == max6646)
910                 temp = temp_from_u16(temp11);
911         else
912                 temp = temp_from_s16(temp11);
913
914         /* +16 degrees offset for temp2 for the LM99 */
915         if (data->kind == lm99 && index <= 2)
916                 temp += 16000;
917
918         return temp;
919 }
920
921 static int lm90_set_temp11(struct lm90_data *data, int index, long val)
922 {
923         static struct reg {
924                 u8 high;
925                 u8 low;
926         } reg[] = {
927         [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
928         [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
929         [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
930         [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
931         [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
932         };
933         struct i2c_client *client = data->client;
934         struct reg *regp = &reg[index];
935         int err;
936
937         /* +16 degrees offset for temp2 for the LM99 */
938         if (data->kind == lm99 && index <= 2)
939                 val -= 16000;
940
941         if (data->kind == adt7461 || data->kind == tmp451)
942                 data->temp11[index] = temp_to_u16_adt7461(data, val);
943         else if (data->kind == max6646)
944                 data->temp11[index] = temp_to_u8(val) << 8;
945         else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
946                 data->temp11[index] = temp_to_s16(val);
947         else
948                 data->temp11[index] = temp_to_s8(val) << 8;
949
950         lm90_select_remote_channel(client, data, index >= 3);
951         err = i2c_smbus_write_byte_data(client, regp->high,
952                                   data->temp11[index] >> 8);
953         if (err < 0)
954                 return err;
955         if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
956                 err = i2c_smbus_write_byte_data(client, regp->low,
957                                                 data->temp11[index] & 0xff);
958
959         lm90_select_remote_channel(client, data, 0);
960         return err;
961 }
962
963 static int lm90_get_temp8(struct lm90_data *data, int index)
964 {
965         s8 temp8 = data->temp8[index];
966         int temp;
967
968         if (data->kind == adt7461 || data->kind == tmp451)
969                 temp = temp_from_u8_adt7461(data, temp8);
970         else if (data->kind == max6646)
971                 temp = temp_from_u8(temp8);
972         else
973                 temp = temp_from_s8(temp8);
974
975         /* +16 degrees offset for temp2 for the LM99 */
976         if (data->kind == lm99 && index == 3)
977                 temp += 16000;
978
979         return temp;
980 }
981
982 static int lm90_set_temp8(struct lm90_data *data, int index, long val)
983 {
984         static const u8 reg[TEMP8_REG_NUM] = {
985                 LM90_REG_W_LOCAL_LOW,
986                 LM90_REG_W_LOCAL_HIGH,
987                 LM90_REG_W_LOCAL_CRIT,
988                 LM90_REG_W_REMOTE_CRIT,
989                 MAX6659_REG_W_LOCAL_EMERG,
990                 MAX6659_REG_W_REMOTE_EMERG,
991                 LM90_REG_W_REMOTE_CRIT,
992                 MAX6659_REG_W_REMOTE_EMERG,
993         };
994         struct i2c_client *client = data->client;
995         int err;
996
997         /* +16 degrees offset for temp2 for the LM99 */
998         if (data->kind == lm99 && index == 3)
999                 val -= 16000;
1000
1001         if (data->kind == adt7461 || data->kind == tmp451)
1002                 data->temp8[index] = temp_to_u8_adt7461(data, val);
1003         else if (data->kind == max6646)
1004                 data->temp8[index] = temp_to_u8(val);
1005         else
1006                 data->temp8[index] = temp_to_s8(val);
1007
1008         lm90_select_remote_channel(client, data, index >= 6);
1009         err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1010         lm90_select_remote_channel(client, data, 0);
1011
1012         return err;
1013 }
1014
1015 static int lm90_get_temphyst(struct lm90_data *data, int index)
1016 {
1017         int temp;
1018
1019         if (data->kind == adt7461 || data->kind == tmp451)
1020                 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1021         else if (data->kind == max6646)
1022                 temp = temp_from_u8(data->temp8[index]);
1023         else
1024                 temp = temp_from_s8(data->temp8[index]);
1025
1026         /* +16 degrees offset for temp2 for the LM99 */
1027         if (data->kind == lm99 && index == 3)
1028                 temp += 16000;
1029
1030         return temp - temp_from_s8(data->temp_hyst);
1031 }
1032
1033 static int lm90_set_temphyst(struct lm90_data *data, long val)
1034 {
1035         struct i2c_client *client = data->client;
1036         int temp;
1037         int err;
1038
1039         if (data->kind == adt7461 || data->kind == tmp451)
1040                 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1041         else if (data->kind == max6646)
1042                 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1043         else
1044                 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1045
1046         data->temp_hyst = hyst_to_reg(temp - val);
1047         err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1048                                         data->temp_hyst);
1049         return err;
1050 }
1051
1052 static const u8 lm90_temp_index[3] = {
1053         LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1054 };
1055
1056 static const u8 lm90_temp_min_index[3] = {
1057         LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1058 };
1059
1060 static const u8 lm90_temp_max_index[3] = {
1061         LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1062 };
1063
1064 static const u8 lm90_temp_crit_index[3] = {
1065         LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1066 };
1067
1068 static const u8 lm90_temp_emerg_index[3] = {
1069         LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1070 };
1071
1072 static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
1073 static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
1074 static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1075 static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1076 static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1077
1078 static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1079 {
1080         struct lm90_data *data = dev_get_drvdata(dev);
1081         int err;
1082
1083         mutex_lock(&data->update_lock);
1084         err = lm90_update_device(dev);
1085         mutex_unlock(&data->update_lock);
1086         if (err)
1087                 return err;
1088
1089         switch (attr) {
1090         case hwmon_temp_input:
1091                 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1092                 break;
1093         case hwmon_temp_min_alarm:
1094                 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1095                 break;
1096         case hwmon_temp_max_alarm:
1097                 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1098                 break;
1099         case hwmon_temp_crit_alarm:
1100                 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1101                 break;
1102         case hwmon_temp_emergency_alarm:
1103                 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1104                 break;
1105         case hwmon_temp_fault:
1106                 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1107                 break;
1108         case hwmon_temp_min:
1109                 if (channel == 0)
1110                         *val = lm90_get_temp8(data,
1111                                               lm90_temp_min_index[channel]);
1112                 else
1113                         *val = lm90_get_temp11(data,
1114                                                lm90_temp_min_index[channel]);
1115                 break;
1116         case hwmon_temp_max:
1117                 if (channel == 0)
1118                         *val = lm90_get_temp8(data,
1119                                               lm90_temp_max_index[channel]);
1120                 else
1121                         *val = lm90_get_temp11(data,
1122                                                lm90_temp_max_index[channel]);
1123                 break;
1124         case hwmon_temp_crit:
1125                 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1126                 break;
1127         case hwmon_temp_crit_hyst:
1128                 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1129                 break;
1130         case hwmon_temp_emergency:
1131                 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1132                 break;
1133         case hwmon_temp_emergency_hyst:
1134                 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1135                 break;
1136         case hwmon_temp_offset:
1137                 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1138                 break;
1139         default:
1140                 return -EOPNOTSUPP;
1141         }
1142         return 0;
1143 }
1144
1145 static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1146 {
1147         struct lm90_data *data = dev_get_drvdata(dev);
1148         int err;
1149
1150         mutex_lock(&data->update_lock);
1151
1152         err = lm90_update_device(dev);
1153         if (err)
1154                 goto error;
1155
1156         switch (attr) {
1157         case hwmon_temp_min:
1158                 if (channel == 0)
1159                         err = lm90_set_temp8(data,
1160                                               lm90_temp_min_index[channel],
1161                                               val);
1162                 else
1163                         err = lm90_set_temp11(data,
1164                                               lm90_temp_min_index[channel],
1165                                               val);
1166                 break;
1167         case hwmon_temp_max:
1168                 if (channel == 0)
1169                         err = lm90_set_temp8(data,
1170                                              lm90_temp_max_index[channel],
1171                                              val);
1172                 else
1173                         err = lm90_set_temp11(data,
1174                                               lm90_temp_max_index[channel],
1175                                               val);
1176                 break;
1177         case hwmon_temp_crit:
1178                 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1179                 break;
1180         case hwmon_temp_crit_hyst:
1181                 err = lm90_set_temphyst(data, val);
1182                 break;
1183         case hwmon_temp_emergency:
1184                 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1185                 break;
1186         case hwmon_temp_offset:
1187                 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1188                 break;
1189         default:
1190                 err = -EOPNOTSUPP;
1191                 break;
1192         }
1193 error:
1194         mutex_unlock(&data->update_lock);
1195
1196         return err;
1197 }
1198
1199 static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1200 {
1201         switch (attr) {
1202         case hwmon_temp_input:
1203         case hwmon_temp_min_alarm:
1204         case hwmon_temp_max_alarm:
1205         case hwmon_temp_crit_alarm:
1206         case hwmon_temp_emergency_alarm:
1207         case hwmon_temp_emergency_hyst:
1208         case hwmon_temp_fault:
1209                 return S_IRUGO;
1210         case hwmon_temp_min:
1211         case hwmon_temp_max:
1212         case hwmon_temp_crit:
1213         case hwmon_temp_emergency:
1214         case hwmon_temp_offset:
1215                 return S_IRUGO | S_IWUSR;
1216         case hwmon_temp_crit_hyst:
1217                 if (channel == 0)
1218                         return S_IRUGO | S_IWUSR;
1219                 return S_IRUGO;
1220         default:
1221                 return 0;
1222         }
1223 }
1224
1225 static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1226 {
1227         struct lm90_data *data = dev_get_drvdata(dev);
1228         int err;
1229
1230         mutex_lock(&data->update_lock);
1231         err = lm90_update_device(dev);
1232         mutex_unlock(&data->update_lock);
1233         if (err)
1234                 return err;
1235
1236         switch (attr) {
1237         case hwmon_chip_update_interval:
1238                 *val = data->update_interval;
1239                 break;
1240         case hwmon_chip_alarms:
1241                 *val = data->alarms;
1242                 break;
1243         default:
1244                 return -EOPNOTSUPP;
1245         }
1246
1247         return 0;
1248 }
1249
1250 static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1251 {
1252         struct lm90_data *data = dev_get_drvdata(dev);
1253         struct i2c_client *client = data->client;
1254         int err;
1255
1256         mutex_lock(&data->update_lock);
1257
1258         err = lm90_update_device(dev);
1259         if (err)
1260                 goto error;
1261
1262         switch (attr) {
1263         case hwmon_chip_update_interval:
1264                 err = lm90_set_convrate(client, data,
1265                                         clamp_val(val, 0, 100000));
1266                 break;
1267         default:
1268                 err = -EOPNOTSUPP;
1269                 break;
1270         }
1271 error:
1272         mutex_unlock(&data->update_lock);
1273
1274         return err;
1275 }
1276
1277 static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1278 {
1279         switch (attr) {
1280         case hwmon_chip_update_interval:
1281                 return S_IRUGO | S_IWUSR;
1282         case hwmon_chip_alarms:
1283                 return S_IRUGO;
1284         default:
1285                 return 0;
1286         }
1287 }
1288
1289 static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1290                      u32 attr, int channel, long *val)
1291 {
1292         switch (type) {
1293         case hwmon_chip:
1294                 return lm90_chip_read(dev, attr, channel, val);
1295         case hwmon_temp:
1296                 return lm90_temp_read(dev, attr, channel, val);
1297         default:
1298                 return -EOPNOTSUPP;
1299         }
1300 }
1301
1302 static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1303                       u32 attr, int channel, long val)
1304 {
1305         switch (type) {
1306         case hwmon_chip:
1307                 return lm90_chip_write(dev, attr, channel, val);
1308         case hwmon_temp:
1309                 return lm90_temp_write(dev, attr, channel, val);
1310         default:
1311                 return -EOPNOTSUPP;
1312         }
1313 }
1314
1315 static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1316                                u32 attr, int channel)
1317 {
1318         switch (type) {
1319         case hwmon_chip:
1320                 return lm90_chip_is_visible(data, attr, channel);
1321         case hwmon_temp:
1322                 return lm90_temp_is_visible(data, attr, channel);
1323         default:
1324                 return 0;
1325         }
1326 }
1327
1328 /* Return 0 if detection is successful, -ENODEV otherwise */
1329 static int lm90_detect(struct i2c_client *client,
1330                        struct i2c_board_info *info)
1331 {
1332         struct i2c_adapter *adapter = client->adapter;
1333         int address = client->addr;
1334         const char *name = NULL;
1335         int man_id, chip_id, config1, config2, convrate;
1336
1337         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1338                 return -ENODEV;
1339
1340         /* detection and identification */
1341         man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1342         chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1343         config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1344         convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1345         if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
1346                 return -ENODEV;
1347
1348         if (man_id == 0x01 || man_id == 0x5C || man_id == 0x41) {
1349                 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1350                 if (config2 < 0)
1351                         return -ENODEV;
1352         } else
1353                 config2 = 0;            /* Make compiler happy */
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");