hwmon: (pmbus) Add client driver for LM25066, LM5064, and LM5066
[linux-block.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 <khali@linux-fr.org>
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  * Since the LM90 was the first chipset supported by this driver, most
61  * comments will refer to this chipset, but are actually general and
62  * concern all supported chipsets, unless mentioned otherwise.
63  *
64  * This program is free software; you can redistribute it and/or modify
65  * it under the terms of the GNU General Public License as published by
66  * the Free Software Foundation; either version 2 of the License, or
67  * (at your option) any later version.
68  *
69  * This program is distributed in the hope that it will be useful,
70  * but WITHOUT ANY WARRANTY; without even the implied warranty of
71  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
72  * GNU General Public License for more details.
73  *
74  * You should have received a copy of the GNU General Public License
75  * along with this program; if not, write to the Free Software
76  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
77  */
78
79 #include <linux/module.h>
80 #include <linux/init.h>
81 #include <linux/slab.h>
82 #include <linux/jiffies.h>
83 #include <linux/i2c.h>
84 #include <linux/hwmon-sysfs.h>
85 #include <linux/hwmon.h>
86 #include <linux/err.h>
87 #include <linux/mutex.h>
88 #include <linux/sysfs.h>
89
90 /*
91  * Addresses to scan
92  * Address is fully defined internally and cannot be changed except for
93  * MAX6659, MAX6680 and MAX6681.
94  * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
95  * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
96  * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
97  * have address 0x4d.
98  * MAX6647 has address 0x4e.
99  * MAX6659 can have address 0x4c, 0x4d or 0x4e.
100  * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
101  * 0x4c, 0x4d or 0x4e.
102  * SA56004 can have address 0x48 through 0x4F.
103  */
104
105 static const unsigned short normal_i2c[] = {
106         0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
107         0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
108
109 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
110         max6646, w83l771, max6696, sa56004 };
111
112 /*
113  * The LM90 registers
114  */
115
116 #define LM90_REG_R_MAN_ID               0xFE
117 #define LM90_REG_R_CHIP_ID              0xFF
118 #define LM90_REG_R_CONFIG1              0x03
119 #define LM90_REG_W_CONFIG1              0x09
120 #define LM90_REG_R_CONFIG2              0xBF
121 #define LM90_REG_W_CONFIG2              0xBF
122 #define LM90_REG_R_CONVRATE             0x04
123 #define LM90_REG_W_CONVRATE             0x0A
124 #define LM90_REG_R_STATUS               0x02
125 #define LM90_REG_R_LOCAL_TEMP           0x00
126 #define LM90_REG_R_LOCAL_HIGH           0x05
127 #define LM90_REG_W_LOCAL_HIGH           0x0B
128 #define LM90_REG_R_LOCAL_LOW            0x06
129 #define LM90_REG_W_LOCAL_LOW            0x0C
130 #define LM90_REG_R_LOCAL_CRIT           0x20
131 #define LM90_REG_W_LOCAL_CRIT           0x20
132 #define LM90_REG_R_REMOTE_TEMPH         0x01
133 #define LM90_REG_R_REMOTE_TEMPL         0x10
134 #define LM90_REG_R_REMOTE_OFFSH         0x11
135 #define LM90_REG_W_REMOTE_OFFSH         0x11
136 #define LM90_REG_R_REMOTE_OFFSL         0x12
137 #define LM90_REG_W_REMOTE_OFFSL         0x12
138 #define LM90_REG_R_REMOTE_HIGHH         0x07
139 #define LM90_REG_W_REMOTE_HIGHH         0x0D
140 #define LM90_REG_R_REMOTE_HIGHL         0x13
141 #define LM90_REG_W_REMOTE_HIGHL         0x13
142 #define LM90_REG_R_REMOTE_LOWH          0x08
143 #define LM90_REG_W_REMOTE_LOWH          0x0E
144 #define LM90_REG_R_REMOTE_LOWL          0x14
145 #define LM90_REG_W_REMOTE_LOWL          0x14
146 #define LM90_REG_R_REMOTE_CRIT          0x19
147 #define LM90_REG_W_REMOTE_CRIT          0x19
148 #define LM90_REG_R_TCRIT_HYST           0x21
149 #define LM90_REG_W_TCRIT_HYST           0x21
150
151 /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
152
153 #define MAX6657_REG_R_LOCAL_TEMPL       0x11
154 #define MAX6696_REG_R_STATUS2           0x12
155 #define MAX6659_REG_R_REMOTE_EMERG      0x16
156 #define MAX6659_REG_W_REMOTE_EMERG      0x16
157 #define MAX6659_REG_R_LOCAL_EMERG       0x17
158 #define MAX6659_REG_W_LOCAL_EMERG       0x17
159
160 /*  SA56004 registers */
161
162 #define SA56004_REG_R_LOCAL_TEMPL 0x22
163
164 #define LM90_DEF_CONVRATE_RVAL  6       /* Def conversion rate register value */
165 #define LM90_MAX_CONVRATE_MS    16000   /* Maximum conversion rate in ms */
166
167 /*
168  * Device flags
169  */
170 #define LM90_FLAG_ADT7461_EXT   (1 << 0) /* ADT7461 extended mode       */
171 /* Device features */
172 #define LM90_HAVE_OFFSET        (1 << 1) /* temperature offset register */
173 #define LM90_HAVE_LOCAL_EXT     (1 << 2) /* extended local temperature  */
174 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit       */
175 #define LM90_HAVE_EMERGENCY     (1 << 4) /* 3rd upper (emergency) limit */
176 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm            */
177 #define LM90_HAVE_TEMP3         (1 << 6) /* 3rd temperature sensor      */
178 #define LM90_HAVE_BROKEN_ALERT  (1 << 7) /* Broken alert                */
179
180 /*
181  * Driver data (common to all clients)
182  */
183
184 static const struct i2c_device_id lm90_id[] = {
185         { "adm1032", adm1032 },
186         { "adt7461", adt7461 },
187         { "adt7461a", adt7461 },
188         { "lm90", lm90 },
189         { "lm86", lm86 },
190         { "lm89", lm86 },
191         { "lm99", lm99 },
192         { "max6646", max6646 },
193         { "max6647", max6646 },
194         { "max6649", max6646 },
195         { "max6657", max6657 },
196         { "max6658", max6657 },
197         { "max6659", max6659 },
198         { "max6680", max6680 },
199         { "max6681", max6680 },
200         { "max6695", max6696 },
201         { "max6696", max6696 },
202         { "nct1008", adt7461 },
203         { "w83l771", w83l771 },
204         { "sa56004", sa56004 },
205         { }
206 };
207 MODULE_DEVICE_TABLE(i2c, lm90_id);
208
209 /*
210  * chip type specific parameters
211  */
212 struct lm90_params {
213         u32 flags;              /* Capabilities */
214         u16 alert_alarms;       /* Which alarm bits trigger ALERT# */
215                                 /* Upper 8 bits for max6695/96 */
216         u8 max_convrate;        /* Maximum conversion rate register value */
217         u8 reg_local_ext;       /* Local extension register if
218                                    LM90_HAVE_LOCAL_EXT is set*/
219 };
220
221 static const struct lm90_params lm90_params[] = {
222         [adm1032] = {
223                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
224                   | LM90_HAVE_BROKEN_ALERT,
225                 .alert_alarms = 0x7c,
226                 .max_convrate = 10,
227         },
228         [adt7461] = {
229                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
230                   | LM90_HAVE_BROKEN_ALERT,
231                 .alert_alarms = 0x7c,
232                 .max_convrate = 10,
233         },
234         [lm86] = {
235                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
236                 .alert_alarms = 0x7b,
237                 .max_convrate = 9,
238         },
239         [lm90] = {
240                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
241                 .alert_alarms = 0x7b,
242                 .max_convrate = 9,
243         },
244         [lm99] = {
245                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
246                 .alert_alarms = 0x7b,
247                 .max_convrate = 9,
248         },
249         [max6646] = {
250                 .flags = LM90_HAVE_LOCAL_EXT,
251                 .alert_alarms = 0x7c,
252                 .max_convrate = 6,
253                 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
254         },
255         [max6657] = {
256                 .flags = LM90_HAVE_LOCAL_EXT,
257                 .alert_alarms = 0x7c,
258                 .max_convrate = 8,
259                 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
260         },
261         [max6659] = {
262                 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY,
263                 .alert_alarms = 0x7c,
264                 .max_convrate = 8,
265                 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
266         },
267         [max6680] = {
268                 .flags = LM90_HAVE_OFFSET,
269                 .alert_alarms = 0x7c,
270                 .max_convrate = 7,
271         },
272         [max6696] = {
273                 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY
274                   | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
275                 .alert_alarms = 0x187c,
276                 .max_convrate = 6,
277                 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
278         },
279         [w83l771] = {
280                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
281                 .alert_alarms = 0x7c,
282                 .max_convrate = 8,
283         },
284         [sa56004] = {
285                 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
286                   | LM90_HAVE_LOCAL_EXT,
287                 .alert_alarms = 0x7b,
288                 .max_convrate = 9,
289                 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
290         },
291 };
292
293 /*
294  * Client data (each client gets its own)
295  */
296
297 struct lm90_data {
298         struct device *hwmon_dev;
299         struct mutex update_lock;
300         char valid; /* zero until following fields are valid */
301         unsigned long last_updated; /* in jiffies */
302         int kind;
303         u32 flags;
304
305         int update_interval;    /* in milliseconds */
306
307         u8 config_orig;         /* Original configuration register value */
308         u8 convrate_orig;       /* Original conversion rate register value */
309         u16 alert_alarms;       /* Which alarm bits trigger ALERT# */
310                                 /* Upper 8 bits for max6695/96 */
311         u8 max_convrate;        /* Maximum conversion rate */
312         u8 reg_local_ext;       /* local extension register offset */
313
314         /* registers values */
315         s8 temp8[8];    /* 0: local low limit
316                            1: local high limit
317                            2: local critical limit
318                            3: remote critical limit
319                            4: local emergency limit (max6659 and max6695/96)
320                            5: remote emergency limit (max6659 and max6695/96)
321                            6: remote 2 critical limit (max6695/96 only)
322                            7: remote 2 emergency limit (max6695/96 only) */
323         s16 temp11[8];  /* 0: remote input
324                            1: remote low limit
325                            2: remote high limit
326                            3: remote offset (except max6646, max6657/58/59,
327                                              and max6695/96)
328                            4: local input
329                            5: remote 2 input (max6695/96 only)
330                            6: remote 2 low limit (max6695/96 only)
331                            7: remote 2 high limit (ma6695/96 only) */
332         u8 temp_hyst;
333         u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
334 };
335
336 /*
337  * Support functions
338  */
339
340 /*
341  * The ADM1032 supports PEC but not on write byte transactions, so we need
342  * to explicitly ask for a transaction without PEC.
343  */
344 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
345 {
346         return i2c_smbus_xfer(client->adapter, client->addr,
347                               client->flags & ~I2C_CLIENT_PEC,
348                               I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
349 }
350
351 /*
352  * It is assumed that client->update_lock is held (unless we are in
353  * detection or initialization steps). This matters when PEC is enabled,
354  * because we don't want the address pointer to change between the write
355  * byte and the read byte transactions.
356  */
357 static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
358 {
359         int err;
360
361         if (client->flags & I2C_CLIENT_PEC) {
362                 err = adm1032_write_byte(client, reg);
363                 if (err >= 0)
364                         err = i2c_smbus_read_byte(client);
365         } else
366                 err = i2c_smbus_read_byte_data(client, reg);
367
368         if (err < 0) {
369                 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
370                          reg, err);
371                 return err;
372         }
373         *value = err;
374
375         return 0;
376 }
377
378 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
379 {
380         int err;
381         u8 oldh, newh, l;
382
383         /*
384          * There is a trick here. We have to read two registers to have the
385          * sensor temperature, but we have to beware a conversion could occur
386          * between the readings. The datasheet says we should either use
387          * the one-shot conversion register, which we don't want to do
388          * (disables hardware monitoring) or monitor the busy bit, which is
389          * impossible (we can't read the values and monitor that bit at the
390          * exact same time). So the solution used here is to read the high
391          * byte once, then the low byte, then the high byte again. If the new
392          * high byte matches the old one, then we have a valid reading. Else
393          * we have to read the low byte again, and now we believe we have a
394          * correct reading.
395          */
396         if ((err = lm90_read_reg(client, regh, &oldh))
397          || (err = lm90_read_reg(client, regl, &l))
398          || (err = lm90_read_reg(client, regh, &newh)))
399                 return err;
400         if (oldh != newh) {
401                 err = lm90_read_reg(client, regl, &l);
402                 if (err)
403                         return err;
404         }
405         *value = (newh << 8) | l;
406
407         return 0;
408 }
409
410 /*
411  * client->update_lock must be held when calling this function (unless we are
412  * in detection or initialization steps), and while a remote channel other
413  * than channel 0 is selected. Also, calling code must make sure to re-select
414  * external channel 0 before releasing the lock. This is necessary because
415  * various registers have different meanings as a result of selecting a
416  * non-default remote channel.
417  */
418 static inline void lm90_select_remote_channel(struct i2c_client *client,
419                                               struct lm90_data *data,
420                                               int channel)
421 {
422         u8 config;
423
424         if (data->kind == max6696) {
425                 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
426                 config &= ~0x08;
427                 if (channel)
428                         config |= 0x08;
429                 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
430                                           config);
431         }
432 }
433
434 /*
435  * Set conversion rate.
436  * client->update_lock must be held when calling this function (unless we are
437  * in detection or initialization steps).
438  */
439 static void lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
440                               unsigned int interval)
441 {
442         int i;
443         unsigned int update_interval;
444
445         /* Shift calculations to avoid rounding errors */
446         interval <<= 6;
447
448         /* find the nearest update rate */
449         for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
450              i < data->max_convrate; i++, update_interval >>= 1)
451                 if (interval >= update_interval * 3 / 4)
452                         break;
453
454         i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i);
455         data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
456 }
457
458 static struct lm90_data *lm90_update_device(struct device *dev)
459 {
460         struct i2c_client *client = to_i2c_client(dev);
461         struct lm90_data *data = i2c_get_clientdata(client);
462         unsigned long next_update;
463
464         mutex_lock(&data->update_lock);
465
466         next_update = data->last_updated
467           + msecs_to_jiffies(data->update_interval) + 1;
468         if (time_after(jiffies, next_update) || !data->valid) {
469                 u8 h, l;
470                 u8 alarms;
471
472                 dev_dbg(&client->dev, "Updating lm90 data.\n");
473                 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
474                 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
475                 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
476                 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
477                 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
478
479                 if (data->flags & LM90_HAVE_LOCAL_EXT) {
480                         lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
481                                     data->reg_local_ext,
482                                     &data->temp11[4]);
483                 } else {
484                         if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
485                                           &h) == 0)
486                                 data->temp11[4] = h << 8;
487                 }
488                 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
489                             LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
490
491                 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
492                         data->temp11[1] = h << 8;
493                         if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
494                          && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
495                                           &l) == 0)
496                                 data->temp11[1] |= l;
497                 }
498                 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
499                         data->temp11[2] = h << 8;
500                         if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
501                          && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
502                                           &l) == 0)
503                                 data->temp11[2] |= l;
504                 }
505
506                 if (data->flags & LM90_HAVE_OFFSET) {
507                         if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
508                                           &h) == 0
509                          && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
510                                           &l) == 0)
511                                 data->temp11[3] = (h << 8) | l;
512                 }
513                 if (data->flags & LM90_HAVE_EMERGENCY) {
514                         lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
515                                       &data->temp8[4]);
516                         lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
517                                       &data->temp8[5]);
518                 }
519                 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
520                 data->alarms = alarms;  /* save as 16 bit value */
521
522                 if (data->kind == max6696) {
523                         lm90_select_remote_channel(client, data, 1);
524                         lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
525                                       &data->temp8[6]);
526                         lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
527                                       &data->temp8[7]);
528                         lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
529                                     LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
530                         if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
531                                 data->temp11[6] = h << 8;
532                         if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
533                                 data->temp11[7] = h << 8;
534                         lm90_select_remote_channel(client, data, 0);
535
536                         if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
537                                            &alarms))
538                                 data->alarms |= alarms << 8;
539                 }
540
541                 /* Re-enable ALERT# output if it was originally enabled and
542                  * relevant alarms are all clear */
543                 if ((data->config_orig & 0x80) == 0
544                  && (data->alarms & data->alert_alarms) == 0) {
545                         u8 config;
546
547                         lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
548                         if (config & 0x80) {
549                                 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
550                                 i2c_smbus_write_byte_data(client,
551                                                           LM90_REG_W_CONFIG1,
552                                                           config & ~0x80);
553                         }
554                 }
555
556                 data->last_updated = jiffies;
557                 data->valid = 1;
558         }
559
560         mutex_unlock(&data->update_lock);
561
562         return data;
563 }
564
565 /*
566  * Conversions
567  * For local temperatures and limits, critical limits and the hysteresis
568  * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
569  * For remote temperatures and limits, it uses signed 11-bit values with
570  * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some
571  * Maxim chips use unsigned values.
572  */
573
574 static inline int temp_from_s8(s8 val)
575 {
576         return val * 1000;
577 }
578
579 static inline int temp_from_u8(u8 val)
580 {
581         return val * 1000;
582 }
583
584 static inline int temp_from_s16(s16 val)
585 {
586         return val / 32 * 125;
587 }
588
589 static inline int temp_from_u16(u16 val)
590 {
591         return val / 32 * 125;
592 }
593
594 static s8 temp_to_s8(long val)
595 {
596         if (val <= -128000)
597                 return -128;
598         if (val >= 127000)
599                 return 127;
600         if (val < 0)
601                 return (val - 500) / 1000;
602         return (val + 500) / 1000;
603 }
604
605 static u8 temp_to_u8(long val)
606 {
607         if (val <= 0)
608                 return 0;
609         if (val >= 255000)
610                 return 255;
611         return (val + 500) / 1000;
612 }
613
614 static s16 temp_to_s16(long val)
615 {
616         if (val <= -128000)
617                 return 0x8000;
618         if (val >= 127875)
619                 return 0x7FE0;
620         if (val < 0)
621                 return (val - 62) / 125 * 32;
622         return (val + 62) / 125 * 32;
623 }
624
625 static u8 hyst_to_reg(long val)
626 {
627         if (val <= 0)
628                 return 0;
629         if (val >= 30500)
630                 return 31;
631         return (val + 500) / 1000;
632 }
633
634 /*
635  * ADT7461 in compatibility mode is almost identical to LM90 except that
636  * attempts to write values that are outside the range 0 < temp < 127 are
637  * treated as the boundary value.
638  *
639  * ADT7461 in "extended mode" operation uses unsigned integers offset by
640  * 64 (e.g., 0 -> -64 degC).  The range is restricted to -64..191 degC.
641  */
642 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
643 {
644         if (data->flags & LM90_FLAG_ADT7461_EXT)
645                 return (val - 64) * 1000;
646         else
647                 return temp_from_s8(val);
648 }
649
650 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
651 {
652         if (data->flags & LM90_FLAG_ADT7461_EXT)
653                 return (val - 0x4000) / 64 * 250;
654         else
655                 return temp_from_s16(val);
656 }
657
658 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
659 {
660         if (data->flags & LM90_FLAG_ADT7461_EXT) {
661                 if (val <= -64000)
662                         return 0;
663                 if (val >= 191000)
664                         return 0xFF;
665                 return (val + 500 + 64000) / 1000;
666         } else {
667                 if (val <= 0)
668                         return 0;
669                 if (val >= 127000)
670                         return 127;
671                 return (val + 500) / 1000;
672         }
673 }
674
675 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
676 {
677         if (data->flags & LM90_FLAG_ADT7461_EXT) {
678                 if (val <= -64000)
679                         return 0;
680                 if (val >= 191750)
681                         return 0xFFC0;
682                 return (val + 64000 + 125) / 250 * 64;
683         } else {
684                 if (val <= 0)
685                         return 0;
686                 if (val >= 127750)
687                         return 0x7FC0;
688                 return (val + 125) / 250 * 64;
689         }
690 }
691
692 /*
693  * Sysfs stuff
694  */
695
696 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
697                           char *buf)
698 {
699         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
700         struct lm90_data *data = lm90_update_device(dev);
701         int temp;
702
703         if (data->kind == adt7461)
704                 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
705         else if (data->kind == max6646)
706                 temp = temp_from_u8(data->temp8[attr->index]);
707         else
708                 temp = temp_from_s8(data->temp8[attr->index]);
709
710         /* +16 degrees offset for temp2 for the LM99 */
711         if (data->kind == lm99 && attr->index == 3)
712                 temp += 16000;
713
714         return sprintf(buf, "%d\n", temp);
715 }
716
717 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
718                          const char *buf, size_t count)
719 {
720         static const u8 reg[8] = {
721                 LM90_REG_W_LOCAL_LOW,
722                 LM90_REG_W_LOCAL_HIGH,
723                 LM90_REG_W_LOCAL_CRIT,
724                 LM90_REG_W_REMOTE_CRIT,
725                 MAX6659_REG_W_LOCAL_EMERG,
726                 MAX6659_REG_W_REMOTE_EMERG,
727                 LM90_REG_W_REMOTE_CRIT,
728                 MAX6659_REG_W_REMOTE_EMERG,
729         };
730
731         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
732         struct i2c_client *client = to_i2c_client(dev);
733         struct lm90_data *data = i2c_get_clientdata(client);
734         int nr = attr->index;
735         long val;
736         int err;
737
738         err = strict_strtol(buf, 10, &val);
739         if (err < 0)
740                 return err;
741
742         /* +16 degrees offset for temp2 for the LM99 */
743         if (data->kind == lm99 && attr->index == 3)
744                 val -= 16000;
745
746         mutex_lock(&data->update_lock);
747         if (data->kind == adt7461)
748                 data->temp8[nr] = temp_to_u8_adt7461(data, val);
749         else if (data->kind == max6646)
750                 data->temp8[nr] = temp_to_u8(val);
751         else
752                 data->temp8[nr] = temp_to_s8(val);
753
754         lm90_select_remote_channel(client, data, nr >= 6);
755         i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
756         lm90_select_remote_channel(client, data, 0);
757
758         mutex_unlock(&data->update_lock);
759         return count;
760 }
761
762 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
763                            char *buf)
764 {
765         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
766         struct lm90_data *data = lm90_update_device(dev);
767         int temp;
768
769         if (data->kind == adt7461)
770                 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
771         else if (data->kind == max6646)
772                 temp = temp_from_u16(data->temp11[attr->index]);
773         else
774                 temp = temp_from_s16(data->temp11[attr->index]);
775
776         /* +16 degrees offset for temp2 for the LM99 */
777         if (data->kind == lm99 &&  attr->index <= 2)
778                 temp += 16000;
779
780         return sprintf(buf, "%d\n", temp);
781 }
782
783 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
784                           const char *buf, size_t count)
785 {
786         struct {
787                 u8 high;
788                 u8 low;
789                 int channel;
790         } reg[5] = {
791                 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
792                 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
793                 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
794                 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
795                 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
796         };
797
798         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
799         struct i2c_client *client = to_i2c_client(dev);
800         struct lm90_data *data = i2c_get_clientdata(client);
801         int nr = attr->nr;
802         int index = attr->index;
803         long val;
804         int err;
805
806         err = strict_strtol(buf, 10, &val);
807         if (err < 0)
808                 return err;
809
810         /* +16 degrees offset for temp2 for the LM99 */
811         if (data->kind == lm99 && index <= 2)
812                 val -= 16000;
813
814         mutex_lock(&data->update_lock);
815         if (data->kind == adt7461)
816                 data->temp11[index] = temp_to_u16_adt7461(data, val);
817         else if (data->kind == max6646)
818                 data->temp11[index] = temp_to_u8(val) << 8;
819         else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
820                 data->temp11[index] = temp_to_s16(val);
821         else
822                 data->temp11[index] = temp_to_s8(val) << 8;
823
824         lm90_select_remote_channel(client, data, reg[nr].channel);
825         i2c_smbus_write_byte_data(client, reg[nr].high,
826                                   data->temp11[index] >> 8);
827         if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
828                 i2c_smbus_write_byte_data(client, reg[nr].low,
829                                           data->temp11[index] & 0xff);
830         lm90_select_remote_channel(client, data, 0);
831
832         mutex_unlock(&data->update_lock);
833         return count;
834 }
835
836 static ssize_t show_temphyst(struct device *dev,
837                              struct device_attribute *devattr,
838                              char *buf)
839 {
840         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
841         struct lm90_data *data = lm90_update_device(dev);
842         int temp;
843
844         if (data->kind == adt7461)
845                 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
846         else if (data->kind == max6646)
847                 temp = temp_from_u8(data->temp8[attr->index]);
848         else
849                 temp = temp_from_s8(data->temp8[attr->index]);
850
851         /* +16 degrees offset for temp2 for the LM99 */
852         if (data->kind == lm99 && attr->index == 3)
853                 temp += 16000;
854
855         return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
856 }
857
858 static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
859                             const char *buf, size_t count)
860 {
861         struct i2c_client *client = to_i2c_client(dev);
862         struct lm90_data *data = i2c_get_clientdata(client);
863         long val;
864         int err;
865         int temp;
866
867         err = strict_strtol(buf, 10, &val);
868         if (err < 0)
869                 return err;
870
871         mutex_lock(&data->update_lock);
872         if (data->kind == adt7461)
873                 temp = temp_from_u8_adt7461(data, data->temp8[2]);
874         else if (data->kind == max6646)
875                 temp = temp_from_u8(data->temp8[2]);
876         else
877                 temp = temp_from_s8(data->temp8[2]);
878
879         data->temp_hyst = hyst_to_reg(temp - val);
880         i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
881                                   data->temp_hyst);
882         mutex_unlock(&data->update_lock);
883         return count;
884 }
885
886 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
887                            char *buf)
888 {
889         struct lm90_data *data = lm90_update_device(dev);
890         return sprintf(buf, "%d\n", data->alarms);
891 }
892
893 static ssize_t show_alarm(struct device *dev, struct device_attribute
894                           *devattr, char *buf)
895 {
896         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
897         struct lm90_data *data = lm90_update_device(dev);
898         int bitnr = attr->index;
899
900         return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
901 }
902
903 static ssize_t show_update_interval(struct device *dev,
904                                     struct device_attribute *attr, char *buf)
905 {
906         struct lm90_data *data = dev_get_drvdata(dev);
907
908         return sprintf(buf, "%u\n", data->update_interval);
909 }
910
911 static ssize_t set_update_interval(struct device *dev,
912                                    struct device_attribute *attr,
913                                    const char *buf, size_t count)
914 {
915         struct i2c_client *client = to_i2c_client(dev);
916         struct lm90_data *data = i2c_get_clientdata(client);
917         unsigned long val;
918         int err;
919
920         err = strict_strtoul(buf, 10, &val);
921         if (err)
922                 return err;
923
924         mutex_lock(&data->update_lock);
925         lm90_set_convrate(client, data, val);
926         mutex_unlock(&data->update_lock);
927
928         return count;
929 }
930
931 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
932 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
933 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
934         set_temp8, 0);
935 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
936         set_temp11, 0, 1);
937 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
938         set_temp8, 1);
939 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
940         set_temp11, 1, 2);
941 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
942         set_temp8, 2);
943 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
944         set_temp8, 3);
945 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
946         set_temphyst, 2);
947 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
948 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
949         set_temp11, 2, 3);
950
951 /* Individual alarm files */
952 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
953 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
954 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
955 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
956 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
957 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
958 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
959 /* Raw alarm file for compatibility */
960 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
961
962 static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
963                    set_update_interval);
964
965 static struct attribute *lm90_attributes[] = {
966         &sensor_dev_attr_temp1_input.dev_attr.attr,
967         &sensor_dev_attr_temp2_input.dev_attr.attr,
968         &sensor_dev_attr_temp1_min.dev_attr.attr,
969         &sensor_dev_attr_temp2_min.dev_attr.attr,
970         &sensor_dev_attr_temp1_max.dev_attr.attr,
971         &sensor_dev_attr_temp2_max.dev_attr.attr,
972         &sensor_dev_attr_temp1_crit.dev_attr.attr,
973         &sensor_dev_attr_temp2_crit.dev_attr.attr,
974         &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
975         &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
976
977         &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
978         &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
979         &sensor_dev_attr_temp2_fault.dev_attr.attr,
980         &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
981         &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
982         &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
983         &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
984         &dev_attr_alarms.attr,
985         &dev_attr_update_interval.attr,
986         NULL
987 };
988
989 static const struct attribute_group lm90_group = {
990         .attrs = lm90_attributes,
991 };
992
993 /*
994  * Additional attributes for devices with emergency sensors
995  */
996 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
997         set_temp8, 4);
998 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
999         set_temp8, 5);
1000 static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
1001                           NULL, 4);
1002 static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
1003                           NULL, 5);
1004
1005 static struct attribute *lm90_emergency_attributes[] = {
1006         &sensor_dev_attr_temp1_emergency.dev_attr.attr,
1007         &sensor_dev_attr_temp2_emergency.dev_attr.attr,
1008         &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
1009         &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
1010         NULL
1011 };
1012
1013 static const struct attribute_group lm90_emergency_group = {
1014         .attrs = lm90_emergency_attributes,
1015 };
1016
1017 static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
1018 static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
1019
1020 static struct attribute *lm90_emergency_alarm_attributes[] = {
1021         &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
1022         &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
1023         NULL
1024 };
1025
1026 static const struct attribute_group lm90_emergency_alarm_group = {
1027         .attrs = lm90_emergency_alarm_attributes,
1028 };
1029
1030 /*
1031  * Additional attributes for devices with 3 temperature sensors
1032  */
1033 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
1034 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
1035         set_temp11, 3, 6);
1036 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
1037         set_temp11, 4, 7);
1038 static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
1039         set_temp8, 6);
1040 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
1041 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
1042         set_temp8, 7);
1043 static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
1044                           NULL, 7);
1045
1046 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
1047 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
1048 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
1049 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
1050 static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
1051
1052 static struct attribute *lm90_temp3_attributes[] = {
1053         &sensor_dev_attr_temp3_input.dev_attr.attr,
1054         &sensor_dev_attr_temp3_min.dev_attr.attr,
1055         &sensor_dev_attr_temp3_max.dev_attr.attr,
1056         &sensor_dev_attr_temp3_crit.dev_attr.attr,
1057         &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1058         &sensor_dev_attr_temp3_emergency.dev_attr.attr,
1059         &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
1060
1061         &sensor_dev_attr_temp3_fault.dev_attr.attr,
1062         &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
1063         &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
1064         &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
1065         &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
1066         NULL
1067 };
1068
1069 static const struct attribute_group lm90_temp3_group = {
1070         .attrs = lm90_temp3_attributes,
1071 };
1072
1073 /* pec used for ADM1032 only */
1074 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
1075                         char *buf)
1076 {
1077         struct i2c_client *client = to_i2c_client(dev);
1078         return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
1079 }
1080
1081 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
1082                        const char *buf, size_t count)
1083 {
1084         struct i2c_client *client = to_i2c_client(dev);
1085         long val;
1086         int err;
1087
1088         err = strict_strtol(buf, 10, &val);
1089         if (err < 0)
1090                 return err;
1091
1092         switch (val) {
1093         case 0:
1094                 client->flags &= ~I2C_CLIENT_PEC;
1095                 break;
1096         case 1:
1097                 client->flags |= I2C_CLIENT_PEC;
1098                 break;
1099         default:
1100                 return -EINVAL;
1101         }
1102
1103         return count;
1104 }
1105
1106 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
1107
1108 /*
1109  * Real code
1110  */
1111
1112 /* Return 0 if detection is successful, -ENODEV otherwise */
1113 static int lm90_detect(struct i2c_client *new_client,
1114                        struct i2c_board_info *info)
1115 {
1116         struct i2c_adapter *adapter = new_client->adapter;
1117         int address = new_client->addr;
1118         const char *name = NULL;
1119         int man_id, chip_id, reg_config1, reg_convrate;
1120
1121         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1122                 return -ENODEV;
1123
1124         /* detection and identification */
1125         if ((man_id = i2c_smbus_read_byte_data(new_client,
1126                                                 LM90_REG_R_MAN_ID)) < 0
1127          || (chip_id = i2c_smbus_read_byte_data(new_client,
1128                                                 LM90_REG_R_CHIP_ID)) < 0
1129          || (reg_config1 = i2c_smbus_read_byte_data(new_client,
1130                                                 LM90_REG_R_CONFIG1)) < 0
1131          || (reg_convrate = i2c_smbus_read_byte_data(new_client,
1132                                                 LM90_REG_R_CONVRATE)) < 0)
1133                 return -ENODEV;
1134
1135         if ((address == 0x4C || address == 0x4D)
1136          && man_id == 0x01) { /* National Semiconductor */
1137                 int reg_config2;
1138
1139                 reg_config2 = i2c_smbus_read_byte_data(new_client,
1140                                                 LM90_REG_R_CONFIG2);
1141                 if (reg_config2 < 0)
1142                         return -ENODEV;
1143
1144                 if ((reg_config1 & 0x2A) == 0x00
1145                  && (reg_config2 & 0xF8) == 0x00
1146                  && reg_convrate <= 0x09) {
1147                         if (address == 0x4C
1148                          && (chip_id & 0xF0) == 0x20) { /* LM90 */
1149                                 name = "lm90";
1150                         } else
1151                         if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1152                                 name = "lm99";
1153                                 dev_info(&adapter->dev,
1154                                          "Assuming LM99 chip at 0x%02x\n",
1155                                          address);
1156                                 dev_info(&adapter->dev,
1157                                          "If it is an LM89, instantiate it "
1158                                          "with the new_device sysfs "
1159                                          "interface\n");
1160                         } else
1161                         if (address == 0x4C
1162                          && (chip_id & 0xF0) == 0x10) { /* LM86 */
1163                                 name = "lm86";
1164                         }
1165                 }
1166         } else
1167         if ((address == 0x4C || address == 0x4D)
1168          && man_id == 0x41) { /* Analog Devices */
1169                 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1170                  && (reg_config1 & 0x3F) == 0x00
1171                  && reg_convrate <= 0x0A) {
1172                         name = "adm1032";
1173                         /* The ADM1032 supports PEC, but only if combined
1174                            transactions are not used. */
1175                         if (i2c_check_functionality(adapter,
1176                                                     I2C_FUNC_SMBUS_BYTE))
1177                                 info->flags |= I2C_CLIENT_PEC;
1178                 } else
1179                 if (chip_id == 0x51 /* ADT7461 */
1180                  && (reg_config1 & 0x1B) == 0x00
1181                  && reg_convrate <= 0x0A) {
1182                         name = "adt7461";
1183                 } else
1184                 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1185                  && (reg_config1 & 0x1B) == 0x00
1186                  && reg_convrate <= 0x0A) {
1187                         name = "adt7461a";
1188                 }
1189         } else
1190         if (man_id == 0x4D) { /* Maxim */
1191                 int reg_emerg, reg_emerg2, reg_status2;
1192
1193                 /*
1194                  * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1195                  * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1196                  * exists, both readings will reflect the same value. Otherwise,
1197                  * the readings will be different.
1198                  */
1199                 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
1200                                                 MAX6659_REG_R_REMOTE_EMERG)) < 0
1201                  || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
1202                  || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
1203                                                 MAX6659_REG_R_REMOTE_EMERG)) < 0
1204                  || (reg_status2 = i2c_smbus_read_byte_data(new_client,
1205                                                 MAX6696_REG_R_STATUS2)) < 0)
1206                         return -ENODEV;
1207
1208                 /*
1209                  * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1210                  * register. Reading from that address will return the last
1211                  * read value, which in our case is those of the man_id
1212                  * register. Likewise, the config1 register seems to lack a
1213                  * low nibble, so the value will be those of the previous
1214                  * read, so in our case those of the man_id register.
1215                  * MAX6659 has a third set of upper temperature limit registers.
1216                  * Those registers also return values on MAX6657 and MAX6658,
1217                  * thus the only way to detect MAX6659 is by its address.
1218                  * For this reason it will be mis-detected as MAX6657 if its
1219                  * address is 0x4C.
1220                  */
1221                 if (chip_id == man_id
1222                  && (address == 0x4C || address == 0x4D || address == 0x4E)
1223                  && (reg_config1 & 0x1F) == (man_id & 0x0F)
1224                  && reg_convrate <= 0x09) {
1225                         if (address == 0x4C)
1226                                 name = "max6657";
1227                         else
1228                                 name = "max6659";
1229                 } else
1230                 /*
1231                  * Even though MAX6695 and MAX6696 do not have a chip ID
1232                  * register, reading it returns 0x01. Bit 4 of the config1
1233                  * register is unused and should return zero when read. Bit 0 of
1234                  * the status2 register is unused and should return zero when
1235                  * read.
1236                  *
1237                  * MAX6695 and MAX6696 have an additional set of temperature
1238                  * limit registers. We can detect those chips by checking if
1239                  * one of those registers exists.
1240                  */
1241                 if (chip_id == 0x01
1242                  && (reg_config1 & 0x10) == 0x00
1243                  && (reg_status2 & 0x01) == 0x00
1244                  && reg_emerg == reg_emerg2
1245                  && reg_convrate <= 0x07) {
1246                         name = "max6696";
1247                 } else
1248                 /*
1249                  * The chip_id register of the MAX6680 and MAX6681 holds the
1250                  * revision of the chip. The lowest bit of the config1 register
1251                  * is unused and should return zero when read, so should the
1252                  * second to last bit of config1 (software reset).
1253                  */
1254                 if (chip_id == 0x01
1255                  && (reg_config1 & 0x03) == 0x00
1256                  && reg_convrate <= 0x07) {
1257                         name = "max6680";
1258                 } else
1259                 /*
1260                  * The chip_id register of the MAX6646/6647/6649 holds the
1261                  * revision of the chip. The lowest 6 bits of the config1
1262                  * register are unused and should return zero when read.
1263                  */
1264                 if (chip_id == 0x59
1265                  && (reg_config1 & 0x3f) == 0x00
1266                  && reg_convrate <= 0x07) {
1267                         name = "max6646";
1268                 }
1269         } else
1270         if (address == 0x4C
1271          && man_id == 0x5C) { /* Winbond/Nuvoton */
1272                 int reg_config2;
1273
1274                 reg_config2 = i2c_smbus_read_byte_data(new_client,
1275                                                 LM90_REG_R_CONFIG2);
1276                 if (reg_config2 < 0)
1277                         return -ENODEV;
1278
1279                 if ((reg_config1 & 0x2A) == 0x00
1280                  && (reg_config2 & 0xF8) == 0x00) {
1281                         if (chip_id == 0x01 /* W83L771W/G */
1282                          && reg_convrate <= 0x09) {
1283                                 name = "w83l771";
1284                         } else
1285                         if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1286                          && reg_convrate <= 0x08) {
1287                                 name = "w83l771";
1288                         }
1289                 }
1290         } else
1291         if (man_id == 0xA1) { /*  NXP Semiconductor/Philips */
1292                 if (chip_id == 0x00 && address >= 0x48 && address <= 0x4F) {
1293                         name = "sa56004";
1294                 }
1295         }
1296
1297         if (!name) { /* identification failed */
1298                 dev_dbg(&adapter->dev,
1299                         "Unsupported chip at 0x%02x (man_id=0x%02X, "
1300                         "chip_id=0x%02X)\n", address, man_id, chip_id);
1301                 return -ENODEV;
1302         }
1303
1304         strlcpy(info->type, name, I2C_NAME_SIZE);
1305
1306         return 0;
1307 }
1308
1309 static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1310 {
1311         if (data->flags & LM90_HAVE_TEMP3)
1312                 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1313         if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1314                 sysfs_remove_group(&client->dev.kobj,
1315                                    &lm90_emergency_alarm_group);
1316         if (data->flags & LM90_HAVE_EMERGENCY)
1317                 sysfs_remove_group(&client->dev.kobj,
1318                                    &lm90_emergency_group);
1319         if (data->flags & LM90_HAVE_OFFSET)
1320                 device_remove_file(&client->dev,
1321                                    &sensor_dev_attr_temp2_offset.dev_attr);
1322         device_remove_file(&client->dev, &dev_attr_pec);
1323         sysfs_remove_group(&client->dev.kobj, &lm90_group);
1324 }
1325
1326 static void lm90_init_client(struct i2c_client *client)
1327 {
1328         u8 config, convrate;
1329         struct lm90_data *data = i2c_get_clientdata(client);
1330
1331         if (lm90_read_reg(client, LM90_REG_R_CONVRATE, &convrate) < 0) {
1332                 dev_warn(&client->dev, "Failed to read convrate register!\n");
1333                 convrate = LM90_DEF_CONVRATE_RVAL;
1334         }
1335         data->convrate_orig = convrate;
1336
1337         /*
1338          * Start the conversions.
1339          */
1340         lm90_set_convrate(client, data, 500);   /* 500ms; 2Hz conversion rate */
1341         if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1342                 dev_warn(&client->dev, "Initialization failed!\n");
1343                 return;
1344         }
1345         data->config_orig = config;
1346
1347         /* Check Temperature Range Select */
1348         if (data->kind == adt7461) {
1349                 if (config & 0x04)
1350                         data->flags |= LM90_FLAG_ADT7461_EXT;
1351         }
1352
1353         /*
1354          * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1355          * 0.125 degree resolution) and range (0x08, extend range
1356          * to -64 degree) mode for the remote temperature sensor.
1357          */
1358         if (data->kind == max6680)
1359                 config |= 0x18;
1360
1361         /*
1362          * Select external channel 0 for max6695/96
1363          */
1364         if (data->kind == max6696)
1365                 config &= ~0x08;
1366
1367         config &= 0xBF; /* run */
1368         if (config != data->config_orig) /* Only write if changed */
1369                 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1370 }
1371
1372 static int lm90_probe(struct i2c_client *new_client,
1373                       const struct i2c_device_id *id)
1374 {
1375         struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
1376         struct lm90_data *data;
1377         int err;
1378
1379         data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
1380         if (!data) {
1381                 err = -ENOMEM;
1382                 goto exit;
1383         }
1384         i2c_set_clientdata(new_client, data);
1385         mutex_init(&data->update_lock);
1386
1387         /* Set the device type */
1388         data->kind = id->driver_data;
1389         if (data->kind == adm1032) {
1390                 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1391                         new_client->flags &= ~I2C_CLIENT_PEC;
1392         }
1393
1394         /* Different devices have different alarm bits triggering the
1395          * ALERT# output */
1396         data->alert_alarms = lm90_params[data->kind].alert_alarms;
1397
1398         /* Set chip capabilities */
1399         data->flags = lm90_params[data->kind].flags;
1400
1401         /* Set maximum conversion rate */
1402         data->max_convrate = lm90_params[data->kind].max_convrate;
1403
1404         if (data->flags & LM90_HAVE_LOCAL_EXT) {
1405                 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
1406                 WARN_ON(data->reg_local_ext == 0);
1407         }
1408
1409         /* Initialize the LM90 chip */
1410         lm90_init_client(new_client);
1411
1412         /* Register sysfs hooks */
1413         err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1414         if (err)
1415                 goto exit_free;
1416         if (new_client->flags & I2C_CLIENT_PEC) {
1417                 err = device_create_file(&new_client->dev, &dev_attr_pec);
1418                 if (err)
1419                         goto exit_remove_files;
1420         }
1421         if (data->flags & LM90_HAVE_OFFSET) {
1422                 err = device_create_file(&new_client->dev,
1423                                         &sensor_dev_attr_temp2_offset.dev_attr);
1424                 if (err)
1425                         goto exit_remove_files;
1426         }
1427         if (data->flags & LM90_HAVE_EMERGENCY) {
1428                 err = sysfs_create_group(&new_client->dev.kobj,
1429                                          &lm90_emergency_group);
1430                 if (err)
1431                         goto exit_remove_files;
1432         }
1433         if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1434                 err = sysfs_create_group(&new_client->dev.kobj,
1435                                          &lm90_emergency_alarm_group);
1436                 if (err)
1437                         goto exit_remove_files;
1438         }
1439         if (data->flags & LM90_HAVE_TEMP3) {
1440                 err = sysfs_create_group(&new_client->dev.kobj,
1441                                          &lm90_temp3_group);
1442                 if (err)
1443                         goto exit_remove_files;
1444         }
1445
1446         data->hwmon_dev = hwmon_device_register(&new_client->dev);
1447         if (IS_ERR(data->hwmon_dev)) {
1448                 err = PTR_ERR(data->hwmon_dev);
1449                 goto exit_remove_files;
1450         }
1451
1452         return 0;
1453
1454 exit_remove_files:
1455         lm90_remove_files(new_client, data);
1456 exit_free:
1457         kfree(data);
1458 exit:
1459         return err;
1460 }
1461
1462 static int lm90_remove(struct i2c_client *client)
1463 {
1464         struct lm90_data *data = i2c_get_clientdata(client);
1465
1466         hwmon_device_unregister(data->hwmon_dev);
1467         lm90_remove_files(client, data);
1468
1469         /* Restore initial configuration */
1470         i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1471                                   data->convrate_orig);
1472         i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1473                                   data->config_orig);
1474
1475         kfree(data);
1476         return 0;
1477 }
1478
1479 static void lm90_alert(struct i2c_client *client, unsigned int flag)
1480 {
1481         struct lm90_data *data = i2c_get_clientdata(client);
1482         u8 config, alarms, alarms2 = 0;
1483
1484         lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
1485
1486         if (data->kind == max6696)
1487                 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1488
1489         if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
1490                 dev_info(&client->dev, "Everything OK\n");
1491         } else {
1492                 if (alarms & 0x61)
1493                         dev_warn(&client->dev,
1494                                  "temp%d out of range, please check!\n", 1);
1495                 if (alarms & 0x1a)
1496                         dev_warn(&client->dev,
1497                                  "temp%d out of range, please check!\n", 2);
1498                 if (alarms & 0x04)
1499                         dev_warn(&client->dev,
1500                                  "temp%d diode open, please check!\n", 2);
1501
1502                 if (alarms2 & 0x18)
1503                         dev_warn(&client->dev,
1504                                  "temp%d out of range, please check!\n", 3);
1505
1506                 /* Disable ALERT# output, because these chips don't implement
1507                   SMBus alert correctly; they should only hold the alert line
1508                   low briefly. */
1509                 if ((data->flags & LM90_HAVE_BROKEN_ALERT)
1510                  && (alarms & data->alert_alarms)) {
1511                         dev_dbg(&client->dev, "Disabling ALERT#\n");
1512                         lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1513                         i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1514                                                   config | 0x80);
1515                 }
1516         }
1517 }
1518
1519 static struct i2c_driver lm90_driver = {
1520         .class          = I2C_CLASS_HWMON,
1521         .driver = {
1522                 .name   = "lm90",
1523         },
1524         .probe          = lm90_probe,
1525         .remove         = lm90_remove,
1526         .alert          = lm90_alert,
1527         .id_table       = lm90_id,
1528         .detect         = lm90_detect,
1529         .address_list   = normal_i2c,
1530 };
1531
1532 static int __init sensors_lm90_init(void)
1533 {
1534         return i2c_add_driver(&lm90_driver);
1535 }
1536
1537 static void __exit sensors_lm90_exit(void)
1538 {
1539         i2c_del_driver(&lm90_driver);
1540 }
1541
1542 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1543 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1544 MODULE_LICENSE("GPL");
1545
1546 module_init(sensors_lm90_init);
1547 module_exit(sensors_lm90_exit);