sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / hwmon / lm90.c
blobc43b4e9f96a9ecd6816954e6c9964b581c2ac1b4
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 * MAX6658 and W83L771 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
154 #define LM90_DEF_CONVRATE_RVAL 6 /* Def conversion rate register value */
155 #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
158 * Device flags
160 #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
161 /* Device features */
162 #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
163 #define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
164 #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
165 #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
166 #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
167 #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
168 #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
171 * Driver data (common to all clients)
174 static const struct i2c_device_id lm90_id[] = {
175 { "adm1032", adm1032 },
176 { "adt7461", adt7461 },
177 { "lm90", lm90 },
178 { "lm86", lm86 },
179 { "lm89", lm86 },
180 { "lm99", lm99 },
181 { "max6646", max6646 },
182 { "max6647", max6646 },
183 { "max6649", max6646 },
184 { "max6657", max6657 },
185 { "max6658", max6657 },
186 { "max6659", max6659 },
187 { "max6680", max6680 },
188 { "max6681", max6680 },
189 { "max6695", max6696 },
190 { "max6696", max6696 },
191 { "w83l771", w83l771 },
194 MODULE_DEVICE_TABLE(i2c, lm90_id);
197 * chip type specific parameters
199 struct lm90_params {
200 u32 flags; /* Capabilities */
201 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
202 /* Upper 8 bits for max6695/96 */
203 u8 max_convrate; /* Maximum conversion rate register value */
206 static const struct lm90_params lm90_params[] = {
207 [adm1032] = {
208 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
209 | LM90_HAVE_BROKEN_ALERT,
210 .alert_alarms = 0x7c,
211 .max_convrate = 10,
213 [adt7461] = {
214 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
215 | LM90_HAVE_BROKEN_ALERT,
216 .alert_alarms = 0x7c,
217 .max_convrate = 10,
219 [lm86] = {
220 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
221 .alert_alarms = 0x7b,
222 .max_convrate = 9,
224 [lm90] = {
225 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
226 .alert_alarms = 0x7b,
227 .max_convrate = 9,
229 [lm99] = {
230 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
231 .alert_alarms = 0x7b,
232 .max_convrate = 9,
234 [max6646] = {
235 .flags = LM90_HAVE_LOCAL_EXT,
236 .alert_alarms = 0x7c,
237 .max_convrate = 6,
239 [max6657] = {
240 .flags = LM90_HAVE_LOCAL_EXT,
241 .alert_alarms = 0x7c,
242 .max_convrate = 8,
244 [max6659] = {
245 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY,
246 .alert_alarms = 0x7c,
247 .max_convrate = 8,
249 [max6680] = {
250 .flags = LM90_HAVE_OFFSET,
251 .alert_alarms = 0x7c,
252 .max_convrate = 7,
254 [max6696] = {
255 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY
256 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
257 .alert_alarms = 0x187c,
258 .max_convrate = 6,
260 [w83l771] = {
261 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
262 .alert_alarms = 0x7c,
263 .max_convrate = 8,
268 * Client data (each client gets its own)
271 struct lm90_data {
272 struct device *hwmon_dev;
273 struct mutex update_lock;
274 char valid; /* zero until following fields are valid */
275 unsigned long last_updated; /* in jiffies */
276 int kind;
277 u32 flags;
279 int update_interval; /* in milliseconds */
281 u8 config_orig; /* Original configuration register value */
282 u8 convrate_orig; /* Original conversion rate register value */
283 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
284 /* Upper 8 bits for max6695/96 */
285 u8 max_convrate; /* Maximum conversion rate */
287 /* registers values */
288 s8 temp8[8]; /* 0: local low limit
289 1: local high limit
290 2: local critical limit
291 3: remote critical limit
292 4: local emergency limit (max6659 and max6695/96)
293 5: remote emergency limit (max6659 and max6695/96)
294 6: remote 2 critical limit (max6695/96 only)
295 7: remote 2 emergency limit (max6695/96 only) */
296 s16 temp11[8]; /* 0: remote input
297 1: remote low limit
298 2: remote high limit
299 3: remote offset (except max6646, max6657/58/59,
300 and max6695/96)
301 4: local input
302 5: remote 2 input (max6695/96 only)
303 6: remote 2 low limit (max6695/96 only)
304 7: remote 2 high limit (ma6695/96 only) */
305 u8 temp_hyst;
306 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
310 * Support functions
314 * The ADM1032 supports PEC but not on write byte transactions, so we need
315 * to explicitly ask for a transaction without PEC.
317 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
319 return i2c_smbus_xfer(client->adapter, client->addr,
320 client->flags & ~I2C_CLIENT_PEC,
321 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
325 * It is assumed that client->update_lock is held (unless we are in
326 * detection or initialization steps). This matters when PEC is enabled,
327 * because we don't want the address pointer to change between the write
328 * byte and the read byte transactions.
330 static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
332 int err;
334 if (client->flags & I2C_CLIENT_PEC) {
335 err = adm1032_write_byte(client, reg);
336 if (err >= 0)
337 err = i2c_smbus_read_byte(client);
338 } else
339 err = i2c_smbus_read_byte_data(client, reg);
341 if (err < 0) {
342 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
343 reg, err);
344 return err;
346 *value = err;
348 return 0;
351 static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
353 int err;
354 u8 oldh, newh, l;
357 * There is a trick here. We have to read two registers to have the
358 * sensor temperature, but we have to beware a conversion could occur
359 * between the readings. The datasheet says we should either use
360 * the one-shot conversion register, which we don't want to do
361 * (disables hardware monitoring) or monitor the busy bit, which is
362 * impossible (we can't read the values and monitor that bit at the
363 * exact same time). So the solution used here is to read the high
364 * byte once, then the low byte, then the high byte again. If the new
365 * high byte matches the old one, then we have a valid reading. Else
366 * we have to read the low byte again, and now we believe we have a
367 * correct reading.
369 if ((err = lm90_read_reg(client, regh, &oldh))
370 || (err = lm90_read_reg(client, regl, &l))
371 || (err = lm90_read_reg(client, regh, &newh)))
372 return err;
373 if (oldh != newh) {
374 err = lm90_read_reg(client, regl, &l);
375 if (err)
376 return err;
378 *value = (newh << 8) | l;
380 return 0;
384 * client->update_lock must be held when calling this function (unless we are
385 * in detection or initialization steps), and while a remote channel other
386 * than channel 0 is selected. Also, calling code must make sure to re-select
387 * external channel 0 before releasing the lock. This is necessary because
388 * various registers have different meanings as a result of selecting a
389 * non-default remote channel.
391 static inline void lm90_select_remote_channel(struct i2c_client *client,
392 struct lm90_data *data,
393 int channel)
395 u8 config;
397 if (data->kind == max6696) {
398 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
399 config &= ~0x08;
400 if (channel)
401 config |= 0x08;
402 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
403 config);
408 * Set conversion rate.
409 * client->update_lock must be held when calling this function (unless we are
410 * in detection or initialization steps).
412 static void lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
413 unsigned int interval)
415 int i;
416 unsigned int update_interval;
418 /* Shift calculations to avoid rounding errors */
419 interval <<= 6;
421 /* find the nearest update rate */
422 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
423 i < data->max_convrate; i++, update_interval >>= 1)
424 if (interval >= update_interval * 3 / 4)
425 break;
427 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i);
428 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
431 static struct lm90_data *lm90_update_device(struct device *dev)
433 struct i2c_client *client = to_i2c_client(dev);
434 struct lm90_data *data = i2c_get_clientdata(client);
435 unsigned long next_update;
437 mutex_lock(&data->update_lock);
439 next_update = data->last_updated
440 + msecs_to_jiffies(data->update_interval) + 1;
441 if (time_after(jiffies, next_update) || !data->valid) {
442 u8 h, l;
443 u8 alarms;
445 dev_dbg(&client->dev, "Updating lm90 data.\n");
446 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
447 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
448 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
449 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
450 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
452 if (data->flags & LM90_HAVE_LOCAL_EXT) {
453 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
454 MAX6657_REG_R_LOCAL_TEMPL,
455 &data->temp11[4]);
456 } else {
457 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
458 &h) == 0)
459 data->temp11[4] = h << 8;
461 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
462 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
464 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
465 data->temp11[1] = h << 8;
466 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
467 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
468 &l) == 0)
469 data->temp11[1] |= l;
471 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
472 data->temp11[2] = h << 8;
473 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
474 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
475 &l) == 0)
476 data->temp11[2] |= l;
479 if (data->flags & LM90_HAVE_OFFSET) {
480 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
481 &h) == 0
482 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
483 &l) == 0)
484 data->temp11[3] = (h << 8) | l;
486 if (data->flags & LM90_HAVE_EMERGENCY) {
487 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
488 &data->temp8[4]);
489 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
490 &data->temp8[5]);
492 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
493 data->alarms = alarms; /* save as 16 bit value */
495 if (data->kind == max6696) {
496 lm90_select_remote_channel(client, data, 1);
497 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
498 &data->temp8[6]);
499 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
500 &data->temp8[7]);
501 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
502 LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
503 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
504 data->temp11[6] = h << 8;
505 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
506 data->temp11[7] = h << 8;
507 lm90_select_remote_channel(client, data, 0);
509 if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
510 &alarms))
511 data->alarms |= alarms << 8;
514 /* Re-enable ALERT# output if it was originally enabled and
515 * relevant alarms are all clear */
516 if ((data->config_orig & 0x80) == 0
517 && (data->alarms & data->alert_alarms) == 0) {
518 u8 config;
520 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
521 if (config & 0x80) {
522 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
523 i2c_smbus_write_byte_data(client,
524 LM90_REG_W_CONFIG1,
525 config & ~0x80);
529 data->last_updated = jiffies;
530 data->valid = 1;
533 mutex_unlock(&data->update_lock);
535 return data;
539 * Conversions
540 * For local temperatures and limits, critical limits and the hysteresis
541 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
542 * For remote temperatures and limits, it uses signed 11-bit values with
543 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
544 * Maxim chips use unsigned values.
547 static inline int temp_from_s8(s8 val)
549 return val * 1000;
552 static inline int temp_from_u8(u8 val)
554 return val * 1000;
557 static inline int temp_from_s16(s16 val)
559 return val / 32 * 125;
562 static inline int temp_from_u16(u16 val)
564 return val / 32 * 125;
567 static s8 temp_to_s8(long val)
569 if (val <= -128000)
570 return -128;
571 if (val >= 127000)
572 return 127;
573 if (val < 0)
574 return (val - 500) / 1000;
575 return (val + 500) / 1000;
578 static u8 temp_to_u8(long val)
580 if (val <= 0)
581 return 0;
582 if (val >= 255000)
583 return 255;
584 return (val + 500) / 1000;
587 static s16 temp_to_s16(long val)
589 if (val <= -128000)
590 return 0x8000;
591 if (val >= 127875)
592 return 0x7FE0;
593 if (val < 0)
594 return (val - 62) / 125 * 32;
595 return (val + 62) / 125 * 32;
598 static u8 hyst_to_reg(long val)
600 if (val <= 0)
601 return 0;
602 if (val >= 30500)
603 return 31;
604 return (val + 500) / 1000;
608 * ADT7461 in compatibility mode is almost identical to LM90 except that
609 * attempts to write values that are outside the range 0 < temp < 127 are
610 * treated as the boundary value.
612 * ADT7461 in "extended mode" operation uses unsigned integers offset by
613 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
615 static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
617 if (data->flags & LM90_FLAG_ADT7461_EXT)
618 return (val - 64) * 1000;
619 else
620 return temp_from_s8(val);
623 static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
625 if (data->flags & LM90_FLAG_ADT7461_EXT)
626 return (val - 0x4000) / 64 * 250;
627 else
628 return temp_from_s16(val);
631 static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
633 if (data->flags & LM90_FLAG_ADT7461_EXT) {
634 if (val <= -64000)
635 return 0;
636 if (val >= 191000)
637 return 0xFF;
638 return (val + 500 + 64000) / 1000;
639 } else {
640 if (val <= 0)
641 return 0;
642 if (val >= 127000)
643 return 127;
644 return (val + 500) / 1000;
648 static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
650 if (data->flags & LM90_FLAG_ADT7461_EXT) {
651 if (val <= -64000)
652 return 0;
653 if (val >= 191750)
654 return 0xFFC0;
655 return (val + 64000 + 125) / 250 * 64;
656 } else {
657 if (val <= 0)
658 return 0;
659 if (val >= 127750)
660 return 0x7FC0;
661 return (val + 125) / 250 * 64;
666 * Sysfs stuff
669 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
670 char *buf)
672 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
673 struct lm90_data *data = lm90_update_device(dev);
674 int temp;
676 if (data->kind == adt7461)
677 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
678 else if (data->kind == max6646)
679 temp = temp_from_u8(data->temp8[attr->index]);
680 else
681 temp = temp_from_s8(data->temp8[attr->index]);
683 /* +16 degrees offset for temp2 for the LM99 */
684 if (data->kind == lm99 && attr->index == 3)
685 temp += 16000;
687 return sprintf(buf, "%d\n", temp);
690 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
691 const char *buf, size_t count)
693 static const u8 reg[8] = {
694 LM90_REG_W_LOCAL_LOW,
695 LM90_REG_W_LOCAL_HIGH,
696 LM90_REG_W_LOCAL_CRIT,
697 LM90_REG_W_REMOTE_CRIT,
698 MAX6659_REG_W_LOCAL_EMERG,
699 MAX6659_REG_W_REMOTE_EMERG,
700 LM90_REG_W_REMOTE_CRIT,
701 MAX6659_REG_W_REMOTE_EMERG,
704 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
705 struct i2c_client *client = to_i2c_client(dev);
706 struct lm90_data *data = i2c_get_clientdata(client);
707 int nr = attr->index;
708 long val;
709 int err;
711 err = strict_strtol(buf, 10, &val);
712 if (err < 0)
713 return err;
715 /* +16 degrees offset for temp2 for the LM99 */
716 if (data->kind == lm99 && attr->index == 3)
717 val -= 16000;
719 mutex_lock(&data->update_lock);
720 if (data->kind == adt7461)
721 data->temp8[nr] = temp_to_u8_adt7461(data, val);
722 else if (data->kind == max6646)
723 data->temp8[nr] = temp_to_u8(val);
724 else
725 data->temp8[nr] = temp_to_s8(val);
727 lm90_select_remote_channel(client, data, nr >= 6);
728 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
729 lm90_select_remote_channel(client, data, 0);
731 mutex_unlock(&data->update_lock);
732 return count;
735 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
736 char *buf)
738 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
739 struct lm90_data *data = lm90_update_device(dev);
740 int temp;
742 if (data->kind == adt7461)
743 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
744 else if (data->kind == max6646)
745 temp = temp_from_u16(data->temp11[attr->index]);
746 else
747 temp = temp_from_s16(data->temp11[attr->index]);
749 /* +16 degrees offset for temp2 for the LM99 */
750 if (data->kind == lm99 && attr->index <= 2)
751 temp += 16000;
753 return sprintf(buf, "%d\n", temp);
756 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
757 const char *buf, size_t count)
759 struct {
760 u8 high;
761 u8 low;
762 int channel;
763 } reg[5] = {
764 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
765 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
766 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
767 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
768 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
771 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
772 struct i2c_client *client = to_i2c_client(dev);
773 struct lm90_data *data = i2c_get_clientdata(client);
774 int nr = attr->nr;
775 int index = attr->index;
776 long val;
777 int err;
779 err = strict_strtol(buf, 10, &val);
780 if (err < 0)
781 return err;
783 /* +16 degrees offset for temp2 for the LM99 */
784 if (data->kind == lm99 && index <= 2)
785 val -= 16000;
787 mutex_lock(&data->update_lock);
788 if (data->kind == adt7461)
789 data->temp11[index] = temp_to_u16_adt7461(data, val);
790 else if (data->kind == max6646)
791 data->temp11[index] = temp_to_u8(val) << 8;
792 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
793 data->temp11[index] = temp_to_s16(val);
794 else
795 data->temp11[index] = temp_to_s8(val) << 8;
797 lm90_select_remote_channel(client, data, reg[nr].channel);
798 i2c_smbus_write_byte_data(client, reg[nr].high,
799 data->temp11[index] >> 8);
800 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
801 i2c_smbus_write_byte_data(client, reg[nr].low,
802 data->temp11[index] & 0xff);
803 lm90_select_remote_channel(client, data, 0);
805 mutex_unlock(&data->update_lock);
806 return count;
809 static ssize_t show_temphyst(struct device *dev,
810 struct device_attribute *devattr,
811 char *buf)
813 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
814 struct lm90_data *data = lm90_update_device(dev);
815 int temp;
817 if (data->kind == adt7461)
818 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
819 else if (data->kind == max6646)
820 temp = temp_from_u8(data->temp8[attr->index]);
821 else
822 temp = temp_from_s8(data->temp8[attr->index]);
824 /* +16 degrees offset for temp2 for the LM99 */
825 if (data->kind == lm99 && attr->index == 3)
826 temp += 16000;
828 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
831 static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
832 const char *buf, size_t count)
834 struct i2c_client *client = to_i2c_client(dev);
835 struct lm90_data *data = i2c_get_clientdata(client);
836 long val;
837 int err;
838 int temp;
840 err = strict_strtol(buf, 10, &val);
841 if (err < 0)
842 return err;
844 mutex_lock(&data->update_lock);
845 if (data->kind == adt7461)
846 temp = temp_from_u8_adt7461(data, data->temp8[2]);
847 else if (data->kind == max6646)
848 temp = temp_from_u8(data->temp8[2]);
849 else
850 temp = temp_from_s8(data->temp8[2]);
852 data->temp_hyst = hyst_to_reg(temp - val);
853 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
854 data->temp_hyst);
855 mutex_unlock(&data->update_lock);
856 return count;
859 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
860 char *buf)
862 struct lm90_data *data = lm90_update_device(dev);
863 return sprintf(buf, "%d\n", data->alarms);
866 static ssize_t show_alarm(struct device *dev, struct device_attribute
867 *devattr, char *buf)
869 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
870 struct lm90_data *data = lm90_update_device(dev);
871 int bitnr = attr->index;
873 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
876 static ssize_t show_update_interval(struct device *dev,
877 struct device_attribute *attr, char *buf)
879 struct lm90_data *data = dev_get_drvdata(dev);
881 return sprintf(buf, "%u\n", data->update_interval);
884 static ssize_t set_update_interval(struct device *dev,
885 struct device_attribute *attr,
886 const char *buf, size_t count)
888 struct i2c_client *client = to_i2c_client(dev);
889 struct lm90_data *data = i2c_get_clientdata(client);
890 unsigned long val;
891 int err;
893 err = strict_strtoul(buf, 10, &val);
894 if (err)
895 return err;
897 mutex_lock(&data->update_lock);
898 lm90_set_convrate(client, data, val);
899 mutex_unlock(&data->update_lock);
901 return count;
904 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
905 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
906 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
907 set_temp8, 0);
908 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
909 set_temp11, 0, 1);
910 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
911 set_temp8, 1);
912 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
913 set_temp11, 1, 2);
914 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
915 set_temp8, 2);
916 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
917 set_temp8, 3);
918 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
919 set_temphyst, 2);
920 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
921 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
922 set_temp11, 2, 3);
924 /* Individual alarm files */
925 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
926 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
927 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
928 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
929 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
930 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
931 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
932 /* Raw alarm file for compatibility */
933 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
935 static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
936 set_update_interval);
938 static struct attribute *lm90_attributes[] = {
939 &sensor_dev_attr_temp1_input.dev_attr.attr,
940 &sensor_dev_attr_temp2_input.dev_attr.attr,
941 &sensor_dev_attr_temp1_min.dev_attr.attr,
942 &sensor_dev_attr_temp2_min.dev_attr.attr,
943 &sensor_dev_attr_temp1_max.dev_attr.attr,
944 &sensor_dev_attr_temp2_max.dev_attr.attr,
945 &sensor_dev_attr_temp1_crit.dev_attr.attr,
946 &sensor_dev_attr_temp2_crit.dev_attr.attr,
947 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
948 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
950 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
951 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
952 &sensor_dev_attr_temp2_fault.dev_attr.attr,
953 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
954 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
955 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
956 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
957 &dev_attr_alarms.attr,
958 &dev_attr_update_interval.attr,
959 NULL
962 static const struct attribute_group lm90_group = {
963 .attrs = lm90_attributes,
967 * Additional attributes for devices with emergency sensors
969 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
970 set_temp8, 4);
971 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
972 set_temp8, 5);
973 static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
974 NULL, 4);
975 static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
976 NULL, 5);
978 static struct attribute *lm90_emergency_attributes[] = {
979 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
980 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
981 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
982 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
983 NULL
986 static const struct attribute_group lm90_emergency_group = {
987 .attrs = lm90_emergency_attributes,
990 static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
991 static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
993 static struct attribute *lm90_emergency_alarm_attributes[] = {
994 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
995 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
996 NULL
999 static const struct attribute_group lm90_emergency_alarm_group = {
1000 .attrs = lm90_emergency_alarm_attributes,
1004 * Additional attributes for devices with 3 temperature sensors
1006 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
1007 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
1008 set_temp11, 3, 6);
1009 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
1010 set_temp11, 4, 7);
1011 static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
1012 set_temp8, 6);
1013 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
1014 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
1015 set_temp8, 7);
1016 static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
1017 NULL, 7);
1019 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
1020 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
1021 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
1022 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
1023 static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
1025 static struct attribute *lm90_temp3_attributes[] = {
1026 &sensor_dev_attr_temp3_input.dev_attr.attr,
1027 &sensor_dev_attr_temp3_min.dev_attr.attr,
1028 &sensor_dev_attr_temp3_max.dev_attr.attr,
1029 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1030 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1031 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
1032 &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
1034 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1035 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
1036 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
1037 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
1038 &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
1039 NULL
1042 static const struct attribute_group lm90_temp3_group = {
1043 .attrs = lm90_temp3_attributes,
1046 /* pec used for ADM1032 only */
1047 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
1048 char *buf)
1050 struct i2c_client *client = to_i2c_client(dev);
1051 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
1054 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
1055 const char *buf, size_t count)
1057 struct i2c_client *client = to_i2c_client(dev);
1058 long val;
1059 int err;
1061 err = strict_strtol(buf, 10, &val);
1062 if (err < 0)
1063 return err;
1065 switch (val) {
1066 case 0:
1067 client->flags &= ~I2C_CLIENT_PEC;
1068 break;
1069 case 1:
1070 client->flags |= I2C_CLIENT_PEC;
1071 break;
1072 default:
1073 return -EINVAL;
1076 return count;
1079 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
1082 * Real code
1085 /* Return 0 if detection is successful, -ENODEV otherwise */
1086 static int lm90_detect(struct i2c_client *new_client,
1087 struct i2c_board_info *info)
1089 struct i2c_adapter *adapter = new_client->adapter;
1090 int address = new_client->addr;
1091 const char *name = NULL;
1092 int man_id, chip_id, reg_config1, reg_convrate;
1094 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1095 return -ENODEV;
1097 /* detection and identification */
1098 if ((man_id = i2c_smbus_read_byte_data(new_client,
1099 LM90_REG_R_MAN_ID)) < 0
1100 || (chip_id = i2c_smbus_read_byte_data(new_client,
1101 LM90_REG_R_CHIP_ID)) < 0
1102 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
1103 LM90_REG_R_CONFIG1)) < 0
1104 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
1105 LM90_REG_R_CONVRATE)) < 0)
1106 return -ENODEV;
1108 if ((address == 0x4C || address == 0x4D)
1109 && man_id == 0x01) { /* National Semiconductor */
1110 int reg_config2;
1112 reg_config2 = i2c_smbus_read_byte_data(new_client,
1113 LM90_REG_R_CONFIG2);
1114 if (reg_config2 < 0)
1115 return -ENODEV;
1117 if ((reg_config1 & 0x2A) == 0x00
1118 && (reg_config2 & 0xF8) == 0x00
1119 && reg_convrate <= 0x09) {
1120 if (address == 0x4C
1121 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1122 name = "lm90";
1123 } else
1124 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1125 name = "lm99";
1126 dev_info(&adapter->dev,
1127 "Assuming LM99 chip at 0x%02x\n",
1128 address);
1129 dev_info(&adapter->dev,
1130 "If it is an LM89, instantiate it "
1131 "with the new_device sysfs "
1132 "interface\n");
1133 } else
1134 if (address == 0x4C
1135 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1136 name = "lm86";
1139 } else
1140 if ((address == 0x4C || address == 0x4D)
1141 && man_id == 0x41) { /* Analog Devices */
1142 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1143 && (reg_config1 & 0x3F) == 0x00
1144 && reg_convrate <= 0x0A) {
1145 name = "adm1032";
1146 /* The ADM1032 supports PEC, but only if combined
1147 transactions are not used. */
1148 if (i2c_check_functionality(adapter,
1149 I2C_FUNC_SMBUS_BYTE))
1150 info->flags |= I2C_CLIENT_PEC;
1151 } else
1152 if (chip_id == 0x51 /* ADT7461 */
1153 && (reg_config1 & 0x1B) == 0x00
1154 && reg_convrate <= 0x0A) {
1155 name = "adt7461";
1157 } else
1158 if (man_id == 0x4D) { /* Maxim */
1159 int reg_emerg, reg_emerg2, reg_status2;
1162 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1163 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1164 * exists, both readings will reflect the same value. Otherwise,
1165 * the readings will be different.
1167 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
1168 MAX6659_REG_R_REMOTE_EMERG)) < 0
1169 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
1170 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
1171 MAX6659_REG_R_REMOTE_EMERG)) < 0
1172 || (reg_status2 = i2c_smbus_read_byte_data(new_client,
1173 MAX6696_REG_R_STATUS2)) < 0)
1174 return -ENODEV;
1177 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1178 * register. Reading from that address will return the last
1179 * read value, which in our case is those of the man_id
1180 * register. Likewise, the config1 register seems to lack a
1181 * low nibble, so the value will be those of the previous
1182 * read, so in our case those of the man_id register.
1183 * MAX6659 has a third set of upper temperature limit registers.
1184 * Those registers also return values on MAX6657 and MAX6658,
1185 * thus the only way to detect MAX6659 is by its address.
1186 * For this reason it will be mis-detected as MAX6657 if its
1187 * address is 0x4C.
1189 if (chip_id == man_id
1190 && (address == 0x4C || address == 0x4D || address == 0x4E)
1191 && (reg_config1 & 0x1F) == (man_id & 0x0F)
1192 && reg_convrate <= 0x09) {
1193 if (address == 0x4C)
1194 name = "max6657";
1195 else
1196 name = "max6659";
1197 } else
1199 * Even though MAX6695 and MAX6696 do not have a chip ID
1200 * register, reading it returns 0x01. Bit 4 of the config1
1201 * register is unused and should return zero when read. Bit 0 of
1202 * the status2 register is unused and should return zero when
1203 * read.
1205 * MAX6695 and MAX6696 have an additional set of temperature
1206 * limit registers. We can detect those chips by checking if
1207 * one of those registers exists.
1209 if (chip_id == 0x01
1210 && (reg_config1 & 0x10) == 0x00
1211 && (reg_status2 & 0x01) == 0x00
1212 && reg_emerg == reg_emerg2
1213 && reg_convrate <= 0x07) {
1214 name = "max6696";
1215 } else
1217 * The chip_id register of the MAX6680 and MAX6681 holds the
1218 * revision of the chip. The lowest bit of the config1 register
1219 * is unused and should return zero when read, so should the
1220 * second to last bit of config1 (software reset).
1222 if (chip_id == 0x01
1223 && (reg_config1 & 0x03) == 0x00
1224 && reg_convrate <= 0x07) {
1225 name = "max6680";
1226 } else
1228 * The chip_id register of the MAX6646/6647/6649 holds the
1229 * revision of the chip. The lowest 6 bits of the config1
1230 * register are unused and should return zero when read.
1232 if (chip_id == 0x59
1233 && (reg_config1 & 0x3f) == 0x00
1234 && reg_convrate <= 0x07) {
1235 name = "max6646";
1237 } else
1238 if (address == 0x4C
1239 && man_id == 0x5C) { /* Winbond/Nuvoton */
1240 int reg_config2;
1242 reg_config2 = i2c_smbus_read_byte_data(new_client,
1243 LM90_REG_R_CONFIG2);
1244 if (reg_config2 < 0)
1245 return -ENODEV;
1247 if ((reg_config1 & 0x2A) == 0x00
1248 && (reg_config2 & 0xF8) == 0x00) {
1249 if (chip_id == 0x01 /* W83L771W/G */
1250 && reg_convrate <= 0x09) {
1251 name = "w83l771";
1252 } else
1253 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1254 && reg_convrate <= 0x08) {
1255 name = "w83l771";
1260 if (!name) { /* identification failed */
1261 dev_dbg(&adapter->dev,
1262 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1263 "chip_id=0x%02X)\n", address, man_id, chip_id);
1264 return -ENODEV;
1267 strlcpy(info->type, name, I2C_NAME_SIZE);
1269 return 0;
1272 static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1274 if (data->flags & LM90_HAVE_TEMP3)
1275 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1276 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1277 sysfs_remove_group(&client->dev.kobj,
1278 &lm90_emergency_alarm_group);
1279 if (data->flags & LM90_HAVE_EMERGENCY)
1280 sysfs_remove_group(&client->dev.kobj,
1281 &lm90_emergency_group);
1282 if (data->flags & LM90_HAVE_OFFSET)
1283 device_remove_file(&client->dev,
1284 &sensor_dev_attr_temp2_offset.dev_attr);
1285 device_remove_file(&client->dev, &dev_attr_pec);
1286 sysfs_remove_group(&client->dev.kobj, &lm90_group);
1289 static void lm90_init_client(struct i2c_client *client)
1291 u8 config, convrate;
1292 struct lm90_data *data = i2c_get_clientdata(client);
1294 if (lm90_read_reg(client, LM90_REG_R_CONVRATE, &convrate) < 0) {
1295 dev_warn(&client->dev, "Failed to read convrate register!\n");
1296 convrate = LM90_DEF_CONVRATE_RVAL;
1298 data->convrate_orig = convrate;
1301 * Start the conversions.
1303 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1304 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1305 dev_warn(&client->dev, "Initialization failed!\n");
1306 return;
1308 data->config_orig = config;
1310 /* Check Temperature Range Select */
1311 if (data->kind == adt7461) {
1312 if (config & 0x04)
1313 data->flags |= LM90_FLAG_ADT7461_EXT;
1317 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1318 * 0.125 degree resolution) and range (0x08, extend range
1319 * to -64 degree) mode for the remote temperature sensor.
1321 if (data->kind == max6680)
1322 config |= 0x18;
1325 * Select external channel 0 for max6695/96
1327 if (data->kind == max6696)
1328 config &= ~0x08;
1330 config &= 0xBF; /* run */
1331 if (config != data->config_orig) /* Only write if changed */
1332 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1335 static int lm90_probe(struct i2c_client *new_client,
1336 const struct i2c_device_id *id)
1338 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
1339 struct lm90_data *data;
1340 int err;
1342 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
1343 if (!data) {
1344 err = -ENOMEM;
1345 goto exit;
1347 i2c_set_clientdata(new_client, data);
1348 mutex_init(&data->update_lock);
1350 /* Set the device type */
1351 data->kind = id->driver_data;
1352 if (data->kind == adm1032) {
1353 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1354 new_client->flags &= ~I2C_CLIENT_PEC;
1357 /* Different devices have different alarm bits triggering the
1358 * ALERT# output */
1359 data->alert_alarms = lm90_params[data->kind].alert_alarms;
1361 /* Set chip capabilities */
1362 data->flags = lm90_params[data->kind].flags;
1364 /* Set maximum conversion rate */
1365 data->max_convrate = lm90_params[data->kind].max_convrate;
1367 /* Initialize the LM90 chip */
1368 lm90_init_client(new_client);
1370 /* Register sysfs hooks */
1371 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1372 if (err)
1373 goto exit_free;
1374 if (new_client->flags & I2C_CLIENT_PEC) {
1375 err = device_create_file(&new_client->dev, &dev_attr_pec);
1376 if (err)
1377 goto exit_remove_files;
1379 if (data->flags & LM90_HAVE_OFFSET) {
1380 err = device_create_file(&new_client->dev,
1381 &sensor_dev_attr_temp2_offset.dev_attr);
1382 if (err)
1383 goto exit_remove_files;
1385 if (data->flags & LM90_HAVE_EMERGENCY) {
1386 err = sysfs_create_group(&new_client->dev.kobj,
1387 &lm90_emergency_group);
1388 if (err)
1389 goto exit_remove_files;
1391 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1392 err = sysfs_create_group(&new_client->dev.kobj,
1393 &lm90_emergency_alarm_group);
1394 if (err)
1395 goto exit_remove_files;
1397 if (data->flags & LM90_HAVE_TEMP3) {
1398 err = sysfs_create_group(&new_client->dev.kobj,
1399 &lm90_temp3_group);
1400 if (err)
1401 goto exit_remove_files;
1404 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1405 if (IS_ERR(data->hwmon_dev)) {
1406 err = PTR_ERR(data->hwmon_dev);
1407 goto exit_remove_files;
1410 return 0;
1412 exit_remove_files:
1413 lm90_remove_files(new_client, data);
1414 exit_free:
1415 kfree(data);
1416 exit:
1417 return err;
1420 static int lm90_remove(struct i2c_client *client)
1422 struct lm90_data *data = i2c_get_clientdata(client);
1424 hwmon_device_unregister(data->hwmon_dev);
1425 lm90_remove_files(client, data);
1427 /* Restore initial configuration */
1428 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1429 data->convrate_orig);
1430 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1431 data->config_orig);
1433 kfree(data);
1434 return 0;
1437 static void lm90_alert(struct i2c_client *client, unsigned int flag)
1439 struct lm90_data *data = i2c_get_clientdata(client);
1440 u8 config, alarms, alarms2 = 0;
1442 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
1444 if (data->kind == max6696)
1445 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1447 if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
1448 dev_info(&client->dev, "Everything OK\n");
1449 } else {
1450 if (alarms & 0x61)
1451 dev_warn(&client->dev,
1452 "temp%d out of range, please check!\n", 1);
1453 if (alarms & 0x1a)
1454 dev_warn(&client->dev,
1455 "temp%d out of range, please check!\n", 2);
1456 if (alarms & 0x04)
1457 dev_warn(&client->dev,
1458 "temp%d diode open, please check!\n", 2);
1460 if (alarms2 & 0x18)
1461 dev_warn(&client->dev,
1462 "temp%d out of range, please check!\n", 3);
1464 /* Disable ALERT# output, because these chips don't implement
1465 SMBus alert correctly; they should only hold the alert line
1466 low briefly. */
1467 if ((data->flags & LM90_HAVE_BROKEN_ALERT)
1468 && (alarms & data->alert_alarms)) {
1469 dev_dbg(&client->dev, "Disabling ALERT#\n");
1470 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1471 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1472 config | 0x80);
1477 static struct i2c_driver lm90_driver = {
1478 .class = I2C_CLASS_HWMON,
1479 .driver = {
1480 .name = "lm90",
1482 .probe = lm90_probe,
1483 .remove = lm90_remove,
1484 .alert = lm90_alert,
1485 .id_table = lm90_id,
1486 .detect = lm90_detect,
1487 .address_list = normal_i2c,
1490 static int __init sensors_lm90_init(void)
1492 return i2c_add_driver(&lm90_driver);
1495 static void __exit sensors_lm90_exit(void)
1497 i2c_del_driver(&lm90_driver);
1500 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1501 MODULE_DESCRIPTION("LM90/ADM1032 driver");
1502 MODULE_LICENSE("GPL");
1504 module_init(sensors_lm90_init);
1505 module_exit(sensors_lm90_exit);