hwmon: (lm90) Introduce chip parameter structure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / lm90.c
blob9df08e1cc5185c639dc5cf9bd6a0c94632b19d3b
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>
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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>
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.
100 static const unsigned short normal_i2c[] = {
101 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
103 enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
104 max6646, w83l771, max6696 };
107 * The LM90 registers
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
145 /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
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
155 * Device flags
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 */
167 * Driver data (common to all clients)
170 static const struct i2c_device_id lm90_id[] = {
171 { "adm1032", adm1032 },
172 { "adt7461", adt7461 },
173 { "lm90", lm90 },
174 { "lm86", lm86 },
175 { "lm89", lm86 },
176 { "lm99", lm99 },
177 { "max6646", max6646 },
178 { "max6647", max6646 },
179 { "max6649", max6646 },
180 { "max6657", max6657 },
181 { "max6658", max6657 },
182 { "max6659", max6659 },
183 { "max6680", max6680 },
184 { "max6681", max6680 },
185 { "max6695", max6696 },
186 { "max6696", max6696 },
187 { "w83l771", w83l771 },
190 MODULE_DEVICE_TABLE(i2c, lm90_id);
193 * chip type specific parameters
195 struct lm90_params {
196 u32 flags; /* Capabilities */
197 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
198 /* Upper 8 bits for max6695/96 */
201 static const struct lm90_params lm90_params[] = {
202 [adm1032] = {
203 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
204 .alert_alarms = 0x7c,
206 [adt7461] = {
207 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
208 .alert_alarms = 0x7c,
210 [lm86] = {
211 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
212 .alert_alarms = 0x7b,
214 [lm90] = {
215 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
216 .alert_alarms = 0x7b,
218 [lm99] = {
219 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
220 .alert_alarms = 0x7b,
222 [max6646] = {
223 .flags = LM90_HAVE_LOCAL_EXT,
224 .alert_alarms = 0x7c,
226 [max6657] = {
227 .flags = LM90_HAVE_LOCAL_EXT,
228 .alert_alarms = 0x7c,
230 [max6659] = {
231 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY,
232 .alert_alarms = 0x7c,
234 [max6680] = {
235 .flags = LM90_HAVE_OFFSET,
236 .alert_alarms = 0x7c,
238 [max6696] = {
239 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY
240 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
241 .alert_alarms = 0x187c,
243 [w83l771] = {
244 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
245 .alert_alarms = 0x7c,
250 * Client data (each client gets its own)
253 struct lm90_data {
254 struct device *hwmon_dev;
255 struct mutex update_lock;
256 char valid; /* zero until following fields are valid */
257 unsigned long last_updated; /* in jiffies */
258 int kind;
259 u32 flags;
261 u8 config_orig; /* Original configuration register value */
262 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
263 /* Upper 8 bits for max6695/96 */
265 /* registers values */
266 s8 temp8[8]; /* 0: local low limit
267 1: local high limit
268 2: local critical limit
269 3: remote critical limit
270 4: local emergency limit (max6659 and max6695/96)
271 5: remote emergency limit (max6659 and max6695/96)
272 6: remote 2 critical limit (max6695/96 only)
273 7: remote 2 emergency limit (max6695/96 only) */
274 s16 temp11[8]; /* 0: remote input
275 1: remote low limit
276 2: remote high limit
277 3: remote offset (except max6646, max6657/58/59,
278 and max6695/96)
279 4: local input
280 5: remote 2 input (max6695/96 only)
281 6: remote 2 low limit (max6695/96 only)
282 7: remote 2 high limit (ma6695/96 only) */
283 u8 temp_hyst;
284 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
288 * Support functions
292 * The ADM1032 supports PEC but not on write byte transactions, so we need
293 * to explicitly ask for a transaction without PEC.
295 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
297 return i2c_smbus_xfer(client->adapter, client->addr,
298 client->flags & ~I2C_CLIENT_PEC,
299 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
303 * It is assumed that client->update_lock is held (unless we are in
304 * detection or initialization steps). This matters when PEC is enabled,
305 * because we don't want the address pointer to change between the write
306 * byte and the read byte transactions.
308 static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
310 int err;
312 if (client->flags & I2C_CLIENT_PEC) {
313 err = adm1032_write_byte(client, reg);
314 if (err >= 0)
315 err = i2c_smbus_read_byte(client);
316 } else
317 err = i2c_smbus_read_byte_data(client, reg);
319 if (err < 0) {
320 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
321 reg, err);
322 return err;
324 *value = err;
326 return 0;
329 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
331 int err;
332 u8 oldh, newh, l;
335 * There is a trick here. We have to read two registers to have the
336 * sensor temperature, but we have to beware a conversion could occur
337 * inbetween the readings. The datasheet says we should either use
338 * the one-shot conversion register, which we don't want to do
339 * (disables hardware monitoring) or monitor the busy bit, which is
340 * impossible (we can't read the values and monitor that bit at the
341 * exact same time). So the solution used here is to read the high
342 * byte once, then the low byte, then the high byte again. If the new
343 * high byte matches the old one, then we have a valid reading. Else
344 * we have to read the low byte again, and now we believe we have a
345 * correct reading.
347 if ((err = lm90_read_reg(client, regh, &oldh))
348 || (err = lm90_read_reg(client, regl, &l))
349 || (err = lm90_read_reg(client, regh, &newh)))
350 return err;
351 if (oldh != newh) {
352 err = lm90_read_reg(client, regl, &l);
353 if (err)
354 return err;
356 *value = (newh << 8) | l;
358 return 0;
362 * client->update_lock must be held when calling this function (unless we are
363 * in detection or initialization steps), and while a remote channel other
364 * than channel 0 is selected. Also, calling code must make sure to re-select
365 * external channel 0 before releasing the lock. This is necessary because
366 * various registers have different meanings as a result of selecting a
367 * non-default remote channel.
369 static inline void lm90_select_remote_channel(struct i2c_client *client,
370 struct lm90_data *data,
371 int channel)
373 u8 config;
375 if (data->kind == max6696) {
376 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
377 config &= ~0x08;
378 if (channel)
379 config |= 0x08;
380 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
381 config);
385 static struct lm90_data *lm90_update_device(struct device *dev)
387 struct i2c_client *client = to_i2c_client(dev);
388 struct lm90_data *data = i2c_get_clientdata(client);
390 mutex_lock(&data->update_lock);
392 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
393 || !data->valid) {
394 u8 h, l;
395 u8 alarms;
397 dev_dbg(&client->dev, "Updating lm90 data.\n");
398 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
399 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
400 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
401 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
402 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
404 if (data->flags & LM90_HAVE_LOCAL_EXT) {
405 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
406 MAX6657_REG_R_LOCAL_TEMPL,
407 &data->temp11[4]);
408 } else {
409 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
410 &h) == 0)
411 data->temp11[4] = h << 8;
413 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
414 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
416 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
417 data->temp11[1] = h << 8;
418 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
419 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
420 &l) == 0)
421 data->temp11[1] |= l;
423 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
424 data->temp11[2] = h << 8;
425 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
426 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
427 &l) == 0)
428 data->temp11[2] |= l;
431 if (data->flags & LM90_HAVE_OFFSET) {
432 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
433 &h) == 0
434 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
435 &l) == 0)
436 data->temp11[3] = (h << 8) | l;
438 if (data->flags & LM90_HAVE_EMERGENCY) {
439 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
440 &data->temp8[4]);
441 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
442 &data->temp8[5]);
444 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
445 data->alarms = alarms; /* save as 16 bit value */
447 if (data->kind == max6696) {
448 lm90_select_remote_channel(client, data, 1);
449 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
450 &data->temp8[6]);
451 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
452 &data->temp8[7]);
453 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
454 LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
455 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
456 data->temp11[6] = h << 8;
457 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
458 data->temp11[7] = h << 8;
459 lm90_select_remote_channel(client, data, 0);
461 if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
462 &alarms))
463 data->alarms |= alarms << 8;
466 /* Re-enable ALERT# output if it was originally enabled and
467 * relevant alarms are all clear */
468 if ((data->config_orig & 0x80) == 0
469 && (data->alarms & data->alert_alarms) == 0) {
470 u8 config;
472 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
473 if (config & 0x80) {
474 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
475 i2c_smbus_write_byte_data(client,
476 LM90_REG_W_CONFIG1,
477 config & ~0x80);
481 data->last_updated = jiffies;
482 data->valid = 1;
485 mutex_unlock(&data->update_lock);
487 return data;
491 * Conversions
492 * For local temperatures and limits, critical limits and the hysteresis
493 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
494 * For remote temperatures and limits, it uses signed 11-bit values with
495 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
496 * Maxim chips use unsigned values.
499 static inline int temp_from_s8(s8 val)
501 return val * 1000;
504 static inline int temp_from_u8(u8 val)
506 return val * 1000;
509 static inline int temp_from_s16(s16 val)
511 return val / 32 * 125;
514 static inline int temp_from_u16(u16 val)
516 return val / 32 * 125;
519 static s8 temp_to_s8(long val)
521 if (val <= -128000)
522 return -128;
523 if (val >= 127000)
524 return 127;
525 if (val < 0)
526 return (val - 500) / 1000;
527 return (val + 500) / 1000;
530 static u8 temp_to_u8(long val)
532 if (val <= 0)
533 return 0;
534 if (val >= 255000)
535 return 255;
536 return (val + 500) / 1000;
539 static s16 temp_to_s16(long val)
541 if (val <= -128000)
542 return 0x8000;
543 if (val >= 127875)
544 return 0x7FE0;
545 if (val < 0)
546 return (val - 62) / 125 * 32;
547 return (val + 62) / 125 * 32;
550 static u8 hyst_to_reg(long val)
552 if (val <= 0)
553 return 0;
554 if (val >= 30500)
555 return 31;
556 return (val + 500) / 1000;
560 * ADT7461 in compatibility mode is almost identical to LM90 except that
561 * attempts to write values that are outside the range 0 < temp < 127 are
562 * treated as the boundary value.
564 * ADT7461 in "extended mode" operation uses unsigned integers offset by
565 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
567 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
569 if (data->flags & LM90_FLAG_ADT7461_EXT)
570 return (val - 64) * 1000;
571 else
572 return temp_from_s8(val);
575 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
577 if (data->flags & LM90_FLAG_ADT7461_EXT)
578 return (val - 0x4000) / 64 * 250;
579 else
580 return temp_from_s16(val);
583 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
585 if (data->flags & LM90_FLAG_ADT7461_EXT) {
586 if (val <= -64000)
587 return 0;
588 if (val >= 191000)
589 return 0xFF;
590 return (val + 500 + 64000) / 1000;
591 } else {
592 if (val <= 0)
593 return 0;
594 if (val >= 127000)
595 return 127;
596 return (val + 500) / 1000;
600 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
602 if (data->flags & LM90_FLAG_ADT7461_EXT) {
603 if (val <= -64000)
604 return 0;
605 if (val >= 191750)
606 return 0xFFC0;
607 return (val + 64000 + 125) / 250 * 64;
608 } else {
609 if (val <= 0)
610 return 0;
611 if (val >= 127750)
612 return 0x7FC0;
613 return (val + 125) / 250 * 64;
618 * Sysfs stuff
621 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
622 char *buf)
624 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
625 struct lm90_data *data = lm90_update_device(dev);
626 int temp;
628 if (data->kind == adt7461)
629 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
630 else if (data->kind == max6646)
631 temp = temp_from_u8(data->temp8[attr->index]);
632 else
633 temp = temp_from_s8(data->temp8[attr->index]);
635 /* +16 degrees offset for temp2 for the LM99 */
636 if (data->kind == lm99 && attr->index == 3)
637 temp += 16000;
639 return sprintf(buf, "%d\n", temp);
642 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
643 const char *buf, size_t count)
645 static const u8 reg[8] = {
646 LM90_REG_W_LOCAL_LOW,
647 LM90_REG_W_LOCAL_HIGH,
648 LM90_REG_W_LOCAL_CRIT,
649 LM90_REG_W_REMOTE_CRIT,
650 MAX6659_REG_W_LOCAL_EMERG,
651 MAX6659_REG_W_REMOTE_EMERG,
652 LM90_REG_W_REMOTE_CRIT,
653 MAX6659_REG_W_REMOTE_EMERG,
656 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
657 struct i2c_client *client = to_i2c_client(dev);
658 struct lm90_data *data = i2c_get_clientdata(client);
659 int nr = attr->index;
660 long val;
661 int err;
663 err = strict_strtol(buf, 10, &val);
664 if (err < 0)
665 return err;
667 /* +16 degrees offset for temp2 for the LM99 */
668 if (data->kind == lm99 && attr->index == 3)
669 val -= 16000;
671 mutex_lock(&data->update_lock);
672 if (data->kind == adt7461)
673 data->temp8[nr] = temp_to_u8_adt7461(data, val);
674 else if (data->kind == max6646)
675 data->temp8[nr] = temp_to_u8(val);
676 else
677 data->temp8[nr] = temp_to_s8(val);
679 lm90_select_remote_channel(client, data, nr >= 6);
680 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
681 lm90_select_remote_channel(client, data, 0);
683 mutex_unlock(&data->update_lock);
684 return count;
687 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
688 char *buf)
690 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
691 struct lm90_data *data = lm90_update_device(dev);
692 int temp;
694 if (data->kind == adt7461)
695 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
696 else if (data->kind == max6646)
697 temp = temp_from_u16(data->temp11[attr->index]);
698 else
699 temp = temp_from_s16(data->temp11[attr->index]);
701 /* +16 degrees offset for temp2 for the LM99 */
702 if (data->kind == lm99 && attr->index <= 2)
703 temp += 16000;
705 return sprintf(buf, "%d\n", temp);
708 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
709 const char *buf, size_t count)
711 struct {
712 u8 high;
713 u8 low;
714 int channel;
715 } reg[5] = {
716 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
717 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
718 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
719 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
720 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
723 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
724 struct i2c_client *client = to_i2c_client(dev);
725 struct lm90_data *data = i2c_get_clientdata(client);
726 int nr = attr->nr;
727 int index = attr->index;
728 long val;
729 int err;
731 err = strict_strtol(buf, 10, &val);
732 if (err < 0)
733 return err;
735 /* +16 degrees offset for temp2 for the LM99 */
736 if (data->kind == lm99 && index <= 2)
737 val -= 16000;
739 mutex_lock(&data->update_lock);
740 if (data->kind == adt7461)
741 data->temp11[index] = temp_to_u16_adt7461(data, val);
742 else if (data->kind == max6646)
743 data->temp11[index] = temp_to_u8(val) << 8;
744 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
745 data->temp11[index] = temp_to_s16(val);
746 else
747 data->temp11[index] = temp_to_s8(val) << 8;
749 lm90_select_remote_channel(client, data, reg[nr].channel);
750 i2c_smbus_write_byte_data(client, reg[nr].high,
751 data->temp11[index] >> 8);
752 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
753 i2c_smbus_write_byte_data(client, reg[nr].low,
754 data->temp11[index] & 0xff);
755 lm90_select_remote_channel(client, data, 0);
757 mutex_unlock(&data->update_lock);
758 return count;
761 static ssize_t show_temphyst(struct device *dev,
762 struct device_attribute *devattr,
763 char *buf)
765 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
766 struct lm90_data *data = lm90_update_device(dev);
767 int temp;
769 if (data->kind == adt7461)
770 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
771 else if (data->kind == max6646)
772 temp = temp_from_u8(data->temp8[attr->index]);
773 else
774 temp = temp_from_s8(data->temp8[attr->index]);
776 /* +16 degrees offset for temp2 for the LM99 */
777 if (data->kind == lm99 && attr->index == 3)
778 temp += 16000;
780 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
783 static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
784 const char *buf, size_t count)
786 struct i2c_client *client = to_i2c_client(dev);
787 struct lm90_data *data = i2c_get_clientdata(client);
788 long val;
789 int err;
790 int temp;
792 err = strict_strtol(buf, 10, &val);
793 if (err < 0)
794 return err;
796 mutex_lock(&data->update_lock);
797 if (data->kind == adt7461)
798 temp = temp_from_u8_adt7461(data, data->temp8[2]);
799 else if (data->kind == max6646)
800 temp = temp_from_u8(data->temp8[2]);
801 else
802 temp = temp_from_s8(data->temp8[2]);
804 data->temp_hyst = hyst_to_reg(temp - val);
805 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
806 data->temp_hyst);
807 mutex_unlock(&data->update_lock);
808 return count;
811 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
812 char *buf)
814 struct lm90_data *data = lm90_update_device(dev);
815 return sprintf(buf, "%d\n", data->alarms);
818 static ssize_t show_alarm(struct device *dev, struct device_attribute
819 *devattr, char *buf)
821 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
822 struct lm90_data *data = lm90_update_device(dev);
823 int bitnr = attr->index;
825 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
828 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
829 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
830 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
831 set_temp8, 0);
832 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
833 set_temp11, 0, 1);
834 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
835 set_temp8, 1);
836 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
837 set_temp11, 1, 2);
838 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
839 set_temp8, 2);
840 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
841 set_temp8, 3);
842 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
843 set_temphyst, 2);
844 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
845 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
846 set_temp11, 2, 3);
848 /* Individual alarm files */
849 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
850 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
851 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
852 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
853 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
854 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
855 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
856 /* Raw alarm file for compatibility */
857 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
859 static struct attribute *lm90_attributes[] = {
860 &sensor_dev_attr_temp1_input.dev_attr.attr,
861 &sensor_dev_attr_temp2_input.dev_attr.attr,
862 &sensor_dev_attr_temp1_min.dev_attr.attr,
863 &sensor_dev_attr_temp2_min.dev_attr.attr,
864 &sensor_dev_attr_temp1_max.dev_attr.attr,
865 &sensor_dev_attr_temp2_max.dev_attr.attr,
866 &sensor_dev_attr_temp1_crit.dev_attr.attr,
867 &sensor_dev_attr_temp2_crit.dev_attr.attr,
868 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
869 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
871 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
872 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
873 &sensor_dev_attr_temp2_fault.dev_attr.attr,
874 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
875 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
876 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
877 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
878 &dev_attr_alarms.attr,
879 NULL
882 static const struct attribute_group lm90_group = {
883 .attrs = lm90_attributes,
887 * Additional attributes for devices with emergency sensors
889 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
890 set_temp8, 4);
891 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
892 set_temp8, 5);
893 static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
894 NULL, 4);
895 static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
896 NULL, 5);
898 static struct attribute *lm90_emergency_attributes[] = {
899 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
900 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
901 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
902 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
903 NULL
906 static const struct attribute_group lm90_emergency_group = {
907 .attrs = lm90_emergency_attributes,
910 static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
911 static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
913 static struct attribute *lm90_emergency_alarm_attributes[] = {
914 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
915 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
916 NULL
919 static const struct attribute_group lm90_emergency_alarm_group = {
920 .attrs = lm90_emergency_alarm_attributes,
924 * Additional attributes for devices with 3 temperature sensors
926 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
927 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
928 set_temp11, 3, 6);
929 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
930 set_temp11, 4, 7);
931 static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
932 set_temp8, 6);
933 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
934 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
935 set_temp8, 7);
936 static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
937 NULL, 7);
939 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
940 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
941 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
942 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
943 static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
945 static struct attribute *lm90_temp3_attributes[] = {
946 &sensor_dev_attr_temp3_input.dev_attr.attr,
947 &sensor_dev_attr_temp3_min.dev_attr.attr,
948 &sensor_dev_attr_temp3_max.dev_attr.attr,
949 &sensor_dev_attr_temp3_crit.dev_attr.attr,
950 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
951 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
952 &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
954 &sensor_dev_attr_temp3_fault.dev_attr.attr,
955 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
956 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
957 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
958 &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
959 NULL
962 static const struct attribute_group lm90_temp3_group = {
963 .attrs = lm90_temp3_attributes,
966 /* pec used for ADM1032 only */
967 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
968 char *buf)
970 struct i2c_client *client = to_i2c_client(dev);
971 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
974 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
975 const char *buf, size_t count)
977 struct i2c_client *client = to_i2c_client(dev);
978 long val;
979 int err;
981 err = strict_strtol(buf, 10, &val);
982 if (err < 0)
983 return err;
985 switch (val) {
986 case 0:
987 client->flags &= ~I2C_CLIENT_PEC;
988 break;
989 case 1:
990 client->flags |= I2C_CLIENT_PEC;
991 break;
992 default:
993 return -EINVAL;
996 return count;
999 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
1002 * Real code
1005 /* Return 0 if detection is successful, -ENODEV otherwise */
1006 static int lm90_detect(struct i2c_client *new_client,
1007 struct i2c_board_info *info)
1009 struct i2c_adapter *adapter = new_client->adapter;
1010 int address = new_client->addr;
1011 const char *name = NULL;
1012 int man_id, chip_id, reg_config1, reg_convrate;
1014 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1015 return -ENODEV;
1017 /* detection and identification */
1018 if ((man_id = i2c_smbus_read_byte_data(new_client,
1019 LM90_REG_R_MAN_ID)) < 0
1020 || (chip_id = i2c_smbus_read_byte_data(new_client,
1021 LM90_REG_R_CHIP_ID)) < 0
1022 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
1023 LM90_REG_R_CONFIG1)) < 0
1024 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
1025 LM90_REG_R_CONVRATE)) < 0)
1026 return -ENODEV;
1028 if ((address == 0x4C || address == 0x4D)
1029 && man_id == 0x01) { /* National Semiconductor */
1030 int reg_config2;
1032 reg_config2 = i2c_smbus_read_byte_data(new_client,
1033 LM90_REG_R_CONFIG2);
1034 if (reg_config2 < 0)
1035 return -ENODEV;
1037 if ((reg_config1 & 0x2A) == 0x00
1038 && (reg_config2 & 0xF8) == 0x00
1039 && reg_convrate <= 0x09) {
1040 if (address == 0x4C
1041 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1042 name = "lm90";
1043 } else
1044 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1045 name = "lm99";
1046 dev_info(&adapter->dev,
1047 "Assuming LM99 chip at 0x%02x\n",
1048 address);
1049 dev_info(&adapter->dev,
1050 "If it is an LM89, instantiate it "
1051 "with the new_device sysfs "
1052 "interface\n");
1053 } else
1054 if (address == 0x4C
1055 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1056 name = "lm86";
1059 } else
1060 if ((address == 0x4C || address == 0x4D)
1061 && man_id == 0x41) { /* Analog Devices */
1062 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1063 && (reg_config1 & 0x3F) == 0x00
1064 && reg_convrate <= 0x0A) {
1065 name = "adm1032";
1066 /* The ADM1032 supports PEC, but only if combined
1067 transactions are not used. */
1068 if (i2c_check_functionality(adapter,
1069 I2C_FUNC_SMBUS_BYTE))
1070 info->flags |= I2C_CLIENT_PEC;
1071 } else
1072 if (chip_id == 0x51 /* ADT7461 */
1073 && (reg_config1 & 0x1B) == 0x00
1074 && reg_convrate <= 0x0A) {
1075 name = "adt7461";
1077 } else
1078 if (man_id == 0x4D) { /* Maxim */
1079 int reg_emerg, reg_emerg2, reg_status2;
1082 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1083 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1084 * exists, both readings will reflect the same value. Otherwise,
1085 * the readings will be different.
1087 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
1088 MAX6659_REG_R_REMOTE_EMERG)) < 0
1089 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
1090 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
1091 MAX6659_REG_R_REMOTE_EMERG)) < 0
1092 || (reg_status2 = i2c_smbus_read_byte_data(new_client,
1093 MAX6696_REG_R_STATUS2)) < 0)
1094 return -ENODEV;
1097 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1098 * register. Reading from that address will return the last
1099 * read value, which in our case is those of the man_id
1100 * register. Likewise, the config1 register seems to lack a
1101 * low nibble, so the value will be those of the previous
1102 * read, so in our case those of the man_id register.
1103 * MAX6659 has a third set of upper temperature limit registers.
1104 * Those registers also return values on MAX6657 and MAX6658,
1105 * thus the only way to detect MAX6659 is by its address.
1106 * For this reason it will be mis-detected as MAX6657 if its
1107 * address is 0x4C.
1109 if (chip_id == man_id
1110 && (address == 0x4C || address == 0x4D || address == 0x4E)
1111 && (reg_config1 & 0x1F) == (man_id & 0x0F)
1112 && reg_convrate <= 0x09) {
1113 if (address == 0x4C)
1114 name = "max6657";
1115 else
1116 name = "max6659";
1117 } else
1119 * Even though MAX6695 and MAX6696 do not have a chip ID
1120 * register, reading it returns 0x01. Bit 4 of the config1
1121 * register is unused and should return zero when read. Bit 0 of
1122 * the status2 register is unused and should return zero when
1123 * read.
1125 * MAX6695 and MAX6696 have an additional set of temperature
1126 * limit registers. We can detect those chips by checking if
1127 * one of those registers exists.
1129 if (chip_id == 0x01
1130 && (reg_config1 & 0x10) == 0x00
1131 && (reg_status2 & 0x01) == 0x00
1132 && reg_emerg == reg_emerg2
1133 && reg_convrate <= 0x07) {
1134 name = "max6696";
1135 } else
1137 * The chip_id register of the MAX6680 and MAX6681 holds the
1138 * revision of the chip. The lowest bit of the config1 register
1139 * is unused and should return zero when read, so should the
1140 * second to last bit of config1 (software reset).
1142 if (chip_id == 0x01
1143 && (reg_config1 & 0x03) == 0x00
1144 && reg_convrate <= 0x07) {
1145 name = "max6680";
1146 } else
1148 * The chip_id register of the MAX6646/6647/6649 holds the
1149 * revision of the chip. The lowest 6 bits of the config1
1150 * register are unused and should return zero when read.
1152 if (chip_id == 0x59
1153 && (reg_config1 & 0x3f) == 0x00
1154 && reg_convrate <= 0x07) {
1155 name = "max6646";
1157 } else
1158 if (address == 0x4C
1159 && man_id == 0x5C) { /* Winbond/Nuvoton */
1160 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1161 && (reg_config1 & 0x2A) == 0x00
1162 && reg_convrate <= 0x08) {
1163 name = "w83l771";
1167 if (!name) { /* identification failed */
1168 dev_dbg(&adapter->dev,
1169 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1170 "chip_id=0x%02X)\n", address, man_id, chip_id);
1171 return -ENODEV;
1174 strlcpy(info->type, name, I2C_NAME_SIZE);
1176 return 0;
1179 static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1181 if (data->flags & LM90_HAVE_TEMP3)
1182 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1183 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1184 sysfs_remove_group(&client->dev.kobj,
1185 &lm90_emergency_alarm_group);
1186 if (data->flags & LM90_HAVE_EMERGENCY)
1187 sysfs_remove_group(&client->dev.kobj,
1188 &lm90_emergency_group);
1189 if (data->flags & LM90_HAVE_OFFSET)
1190 device_remove_file(&client->dev,
1191 &sensor_dev_attr_temp2_offset.dev_attr);
1192 device_remove_file(&client->dev, &dev_attr_pec);
1193 sysfs_remove_group(&client->dev.kobj, &lm90_group);
1196 static void lm90_init_client(struct i2c_client *client)
1198 u8 config;
1199 struct lm90_data *data = i2c_get_clientdata(client);
1202 * Start the conversions.
1204 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1205 5); /* 2 Hz */
1206 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1207 dev_warn(&client->dev, "Initialization failed!\n");
1208 return;
1210 data->config_orig = config;
1212 /* Check Temperature Range Select */
1213 if (data->kind == adt7461) {
1214 if (config & 0x04)
1215 data->flags |= LM90_FLAG_ADT7461_EXT;
1219 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1220 * 0.125 degree resolution) and range (0x08, extend range
1221 * to -64 degree) mode for the remote temperature sensor.
1223 if (data->kind == max6680)
1224 config |= 0x18;
1227 * Select external channel 0 for max6695/96
1229 if (data->kind == max6696)
1230 config &= ~0x08;
1232 config &= 0xBF; /* run */
1233 if (config != data->config_orig) /* Only write if changed */
1234 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1237 static int lm90_probe(struct i2c_client *new_client,
1238 const struct i2c_device_id *id)
1240 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
1241 struct lm90_data *data;
1242 int err;
1244 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
1245 if (!data) {
1246 err = -ENOMEM;
1247 goto exit;
1249 i2c_set_clientdata(new_client, data);
1250 mutex_init(&data->update_lock);
1252 /* Set the device type */
1253 data->kind = id->driver_data;
1254 if (data->kind == adm1032) {
1255 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1256 new_client->flags &= ~I2C_CLIENT_PEC;
1259 /* Different devices have different alarm bits triggering the
1260 * ALERT# output */
1261 data->alert_alarms = lm90_params[data->kind].alert_alarms;
1263 /* Set chip capabilities */
1264 data->flags = lm90_params[data->kind].flags;
1266 /* Initialize the LM90 chip */
1267 lm90_init_client(new_client);
1269 /* Register sysfs hooks */
1270 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1271 if (err)
1272 goto exit_free;
1273 if (new_client->flags & I2C_CLIENT_PEC) {
1274 err = device_create_file(&new_client->dev, &dev_attr_pec);
1275 if (err)
1276 goto exit_remove_files;
1278 if (data->flags & LM90_HAVE_OFFSET) {
1279 err = device_create_file(&new_client->dev,
1280 &sensor_dev_attr_temp2_offset.dev_attr);
1281 if (err)
1282 goto exit_remove_files;
1284 if (data->flags & LM90_HAVE_EMERGENCY) {
1285 err = sysfs_create_group(&new_client->dev.kobj,
1286 &lm90_emergency_group);
1287 if (err)
1288 goto exit_remove_files;
1290 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1291 err = sysfs_create_group(&new_client->dev.kobj,
1292 &lm90_emergency_alarm_group);
1293 if (err)
1294 goto exit_remove_files;
1296 if (data->flags & LM90_HAVE_TEMP3) {
1297 err = sysfs_create_group(&new_client->dev.kobj,
1298 &lm90_temp3_group);
1299 if (err)
1300 goto exit_remove_files;
1303 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1304 if (IS_ERR(data->hwmon_dev)) {
1305 err = PTR_ERR(data->hwmon_dev);
1306 goto exit_remove_files;
1309 return 0;
1311 exit_remove_files:
1312 lm90_remove_files(new_client, data);
1313 exit_free:
1314 kfree(data);
1315 exit:
1316 return err;
1319 static int lm90_remove(struct i2c_client *client)
1321 struct lm90_data *data = i2c_get_clientdata(client);
1323 hwmon_device_unregister(data->hwmon_dev);
1324 lm90_remove_files(client, data);
1326 /* Restore initial configuration */
1327 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1328 data->config_orig);
1330 kfree(data);
1331 return 0;
1334 static void lm90_alert(struct i2c_client *client, unsigned int flag)
1336 struct lm90_data *data = i2c_get_clientdata(client);
1337 u8 config, alarms, alarms2 = 0;
1339 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
1341 if (data->kind == max6696)
1342 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1344 if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
1345 dev_info(&client->dev, "Everything OK\n");
1346 } else {
1347 if (alarms & 0x61)
1348 dev_warn(&client->dev,
1349 "temp%d out of range, please check!\n", 1);
1350 if (alarms & 0x1a)
1351 dev_warn(&client->dev,
1352 "temp%d out of range, please check!\n", 2);
1353 if (alarms & 0x04)
1354 dev_warn(&client->dev,
1355 "temp%d diode open, please check!\n", 2);
1357 if (alarms2 & 0x18)
1358 dev_warn(&client->dev,
1359 "temp%d out of range, please check!\n", 3);
1361 /* Disable ALERT# output, because these chips don't implement
1362 SMBus alert correctly; they should only hold the alert line
1363 low briefly. */
1364 if ((data->kind == adm1032 || data->kind == adt7461)
1365 && (alarms & data->alert_alarms)) {
1366 dev_dbg(&client->dev, "Disabling ALERT#\n");
1367 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1368 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1369 config | 0x80);
1374 static struct i2c_driver lm90_driver = {
1375 .class = I2C_CLASS_HWMON,
1376 .driver = {
1377 .name = "lm90",
1379 .probe = lm90_probe,
1380 .remove = lm90_remove,
1381 .alert = lm90_alert,
1382 .id_table = lm90_id,
1383 .detect = lm90_detect,
1384 .address_list = normal_i2c,
1387 static int __init sensors_lm90_init(void)
1389 return i2c_add_driver(&lm90_driver);
1392 static void __exit sensors_lm90_exit(void)
1394 i2c_del_driver(&lm90_driver);
1397 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1398 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1399 MODULE_LICENSE("GPL");
1401 module_init(sensors_lm90_init);
1402 module_exit(sensors_lm90_exit);