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