aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/lm90.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon/lm90.c')
-rw-r--r--drivers/hwmon/lm90.c1032
1 files changed, 736 insertions, 296 deletions
diff --git a/drivers/hwmon/lm90.c b/drivers/hwmon/lm90.c
index 760ef72eea56..2f94f9504804 100644
--- a/drivers/hwmon/lm90.c
+++ b/drivers/hwmon/lm90.c
@@ -28,9 +28,11 @@
28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor 28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
29 * chips made by Maxim. These chips are similar to the LM86. 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 30 * Note that there is no easy way to differentiate between the three
31 * variants. The extra address and features of the MAX6659 are not 31 * variants. We use the device address to detect MAX6659, which will result
32 * supported by this driver. These chips lack the remote temperature 32 * in a detection as max6657 if it is on address 0x4c. The extra address
33 * offset feature. 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.
34 * 36 *
35 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and 37 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
36 * MAX6692 chips made by Maxim. These are again similar to the LM86, 38 * MAX6692 chips made by Maxim. These are again similar to the LM86,
@@ -42,10 +44,15 @@
42 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can 44 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
43 * be treated identically. 45 * be treated identically.
44 * 46 *
45 * This driver also supports the ADT7461 chip from Analog Devices. 47 * This driver also supports the MAX6695 and MAX6696, two other sensor
46 * It's supported in both compatibility and extended mode. It is mostly 48 * chips made by Maxim. These are also quite similar to other Maxim
47 * compatible with LM90 except for a data format difference for the 49 * chips, but support three temperature sensors instead of two. MAX6695
48 * temperature value registers. 50 * and MAX6696 only differ in the pinout so they can be treated identically.
51 *
52 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
53 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
54 * and extended mode. They are mostly compatible with LM90 except for a data
55 * format difference for the temperature value registers.
49 * 56 *
50 * Since the LM90 was the first chipset supported by this driver, most 57 * Since the LM90 was the first chipset supported by this driver, most
51 * comments will refer to this chipset, but are actually general and 58 * comments will refer to this chipset, but are actually general and
@@ -81,11 +88,12 @@
81 * Addresses to scan 88 * Addresses to scan
82 * Address is fully defined internally and cannot be changed except for 89 * Address is fully defined internally and cannot be changed except for
83 * MAX6659, MAX6680 and MAX6681. 90 * MAX6659, MAX6680 and MAX6681.
84 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657 91 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
85 * and MAX6658 have address 0x4c. 92 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
86 * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d. 93 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
94 * have address 0x4d.
87 * MAX6647 has address 0x4e. 95 * MAX6647 has address 0x4e.
88 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported). 96 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
89 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 97 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
90 * 0x4c, 0x4d or 0x4e. 98 * 0x4c, 0x4d or 0x4e.
91 */ 99 */
@@ -93,8 +101,8 @@
93static const unsigned short normal_i2c[] = { 101static const unsigned short normal_i2c[] = {
94 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; 102 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
95 103
96enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646, 104enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
97 w83l771 }; 105 max6646, w83l771, max6696 };
98 106
99/* 107/*
100 * The LM90 registers 108 * The LM90 registers
@@ -135,26 +143,30 @@ enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646,
135#define LM90_REG_R_TCRIT_HYST 0x21 143#define LM90_REG_R_TCRIT_HYST 0x21
136#define LM90_REG_W_TCRIT_HYST 0x21 144#define LM90_REG_W_TCRIT_HYST 0x21
137 145
138/* MAX6646/6647/6649/6657/6658/6659 registers */ 146/* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
139 147
140#define MAX6657_REG_R_LOCAL_TEMPL 0x11 148#define MAX6657_REG_R_LOCAL_TEMPL 0x11
149#define MAX6696_REG_R_STATUS2 0x12
150#define MAX6659_REG_R_REMOTE_EMERG 0x16
151#define MAX6659_REG_W_REMOTE_EMERG 0x16
152#define MAX6659_REG_R_LOCAL_EMERG 0x17
153#define MAX6659_REG_W_LOCAL_EMERG 0x17
141 154
142/* 155#define LM90_DEF_CONVRATE_RVAL 6 /* Def conversion rate register value */
143 * Device flags 156#define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
144 */
145#define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
146 157
147/* 158/*
148 * Functions declaration 159 * Device flags
149 */ 160 */
150 161#define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
151static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info); 162/* Device features */
152static int lm90_probe(struct i2c_client *client, 163#define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
153 const struct i2c_device_id *id); 164#define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
154static void lm90_init_client(struct i2c_client *client); 165#define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
155static void lm90_alert(struct i2c_client *client, unsigned int flag); 166#define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
156static int lm90_remove(struct i2c_client *client); 167#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
157static struct lm90_data *lm90_update_device(struct device *dev); 168#define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
169#define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
158 170
159/* 171/*
160 * Driver data (common to all clients) 172 * Driver data (common to all clients)
@@ -163,6 +175,7 @@ static struct lm90_data *lm90_update_device(struct device *dev);
163static const struct i2c_device_id lm90_id[] = { 175static const struct i2c_device_id lm90_id[] = {
164 { "adm1032", adm1032 }, 176 { "adm1032", adm1032 },
165 { "adt7461", adt7461 }, 177 { "adt7461", adt7461 },
178 { "adt7461a", adt7461 },
166 { "lm90", lm90 }, 179 { "lm90", lm90 },
167 { "lm86", lm86 }, 180 { "lm86", lm86 },
168 { "lm89", lm86 }, 181 { "lm89", lm86 },
@@ -172,25 +185,86 @@ static const struct i2c_device_id lm90_id[] = {
172 { "max6649", max6646 }, 185 { "max6649", max6646 },
173 { "max6657", max6657 }, 186 { "max6657", max6657 },
174 { "max6658", max6657 }, 187 { "max6658", max6657 },
175 { "max6659", max6657 }, 188 { "max6659", max6659 },
176 { "max6680", max6680 }, 189 { "max6680", max6680 },
177 { "max6681", max6680 }, 190 { "max6681", max6680 },
191 { "max6695", max6696 },
192 { "max6696", max6696 },
193 { "nct1008", adt7461 },
178 { "w83l771", w83l771 }, 194 { "w83l771", w83l771 },
179 { } 195 { }
180}; 196};
181MODULE_DEVICE_TABLE(i2c, lm90_id); 197MODULE_DEVICE_TABLE(i2c, lm90_id);
182 198
183static struct i2c_driver lm90_driver = { 199/*
184 .class = I2C_CLASS_HWMON, 200 * chip type specific parameters
185 .driver = { 201 */
186 .name = "lm90", 202struct lm90_params {
203 u32 flags; /* Capabilities */
204 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
205 /* Upper 8 bits for max6695/96 */
206 u8 max_convrate; /* Maximum conversion rate register value */
207};
208
209static const struct lm90_params lm90_params[] = {
210 [adm1032] = {
211 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
212 | LM90_HAVE_BROKEN_ALERT,
213 .alert_alarms = 0x7c,
214 .max_convrate = 10,
215 },
216 [adt7461] = {
217 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
218 | LM90_HAVE_BROKEN_ALERT,
219 .alert_alarms = 0x7c,
220 .max_convrate = 10,
221 },
222 [lm86] = {
223 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
224 .alert_alarms = 0x7b,
225 .max_convrate = 9,
226 },
227 [lm90] = {
228 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
229 .alert_alarms = 0x7b,
230 .max_convrate = 9,
231 },
232 [lm99] = {
233 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
234 .alert_alarms = 0x7b,
235 .max_convrate = 9,
236 },
237 [max6646] = {
238 .flags = LM90_HAVE_LOCAL_EXT,
239 .alert_alarms = 0x7c,
240 .max_convrate = 6,
241 },
242 [max6657] = {
243 .flags = LM90_HAVE_LOCAL_EXT,
244 .alert_alarms = 0x7c,
245 .max_convrate = 8,
246 },
247 [max6659] = {
248 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY,
249 .alert_alarms = 0x7c,
250 .max_convrate = 8,
251 },
252 [max6680] = {
253 .flags = LM90_HAVE_OFFSET,
254 .alert_alarms = 0x7c,
255 .max_convrate = 7,
256 },
257 [max6696] = {
258 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY
259 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
260 .alert_alarms = 0x187c,
261 .max_convrate = 6,
262 },
263 [w83l771] = {
264 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
265 .alert_alarms = 0x7c,
266 .max_convrate = 8,
187 }, 267 },
188 .probe = lm90_probe,
189 .remove = lm90_remove,
190 .alert = lm90_alert,
191 .id_table = lm90_id,
192 .detect = lm90_detect,
193 .address_list = normal_i2c,
194}; 268};
195 269
196/* 270/*
@@ -203,26 +277,268 @@ struct lm90_data {
203 char valid; /* zero until following fields are valid */ 277 char valid; /* zero until following fields are valid */
204 unsigned long last_updated; /* in jiffies */ 278 unsigned long last_updated; /* in jiffies */
205 int kind; 279 int kind;
206 int flags; 280 u32 flags;
281
282 int update_interval; /* in milliseconds */
207 283
208 u8 config_orig; /* Original configuration register value */ 284 u8 config_orig; /* Original configuration register value */
209 u8 alert_alarms; /* Which alarm bits trigger ALERT# */ 285 u8 convrate_orig; /* Original conversion rate register value */
286 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
287 /* Upper 8 bits for max6695/96 */
288 u8 max_convrate; /* Maximum conversion rate */
210 289
211 /* registers values */ 290 /* registers values */
212 s8 temp8[4]; /* 0: local low limit 291 s8 temp8[8]; /* 0: local low limit
213 1: local high limit 292 1: local high limit
214 2: local critical limit 293 2: local critical limit
215 3: remote critical limit */ 294 3: remote critical limit
216 s16 temp11[5]; /* 0: remote input 295 4: local emergency limit (max6659 and max6695/96)
296 5: remote emergency limit (max6659 and max6695/96)
297 6: remote 2 critical limit (max6695/96 only)
298 7: remote 2 emergency limit (max6695/96 only) */
299 s16 temp11[8]; /* 0: remote input
217 1: remote low limit 300 1: remote low limit
218 2: remote high limit 301 2: remote high limit
219 3: remote offset (except max6646 and max6657) 302 3: remote offset (except max6646, max6657/58/59,
220 4: local input */ 303 and max6695/96)
304 4: local input
305 5: remote 2 input (max6695/96 only)
306 6: remote 2 low limit (max6695/96 only)
307 7: remote 2 high limit (ma6695/96 only) */
221 u8 temp_hyst; 308 u8 temp_hyst;
222 u8 alarms; /* bitvector */ 309 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
223}; 310};
224 311
225/* 312/*
313 * Support functions
314 */
315
316/*
317 * The ADM1032 supports PEC but not on write byte transactions, so we need
318 * to explicitly ask for a transaction without PEC.
319 */
320static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
321{
322 return i2c_smbus_xfer(client->adapter, client->addr,
323 client->flags & ~I2C_CLIENT_PEC,
324 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
325}
326
327/*
328 * It is assumed that client->update_lock is held (unless we are in
329 * detection or initialization steps). This matters when PEC is enabled,
330 * because we don't want the address pointer to change between the write
331 * byte and the read byte transactions.
332 */
333static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
334{
335 int err;
336
337 if (client->flags & I2C_CLIENT_PEC) {
338 err = adm1032_write_byte(client, reg);
339 if (err >= 0)
340 err = i2c_smbus_read_byte(client);
341 } else
342 err = i2c_smbus_read_byte_data(client, reg);
343
344 if (err < 0) {
345 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
346 reg, err);
347 return err;
348 }
349 *value = err;
350
351 return 0;
352}
353
354static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
355{
356 int err;
357 u8 oldh, newh, l;
358
359 /*
360 * There is a trick here. We have to read two registers to have the
361 * sensor temperature, but we have to beware a conversion could occur
362 * between the readings. The datasheet says we should either use
363 * the one-shot conversion register, which we don't want to do
364 * (disables hardware monitoring) or monitor the busy bit, which is
365 * impossible (we can't read the values and monitor that bit at the
366 * exact same time). So the solution used here is to read the high
367 * byte once, then the low byte, then the high byte again. If the new
368 * high byte matches the old one, then we have a valid reading. Else
369 * we have to read the low byte again, and now we believe we have a
370 * correct reading.
371 */
372 if ((err = lm90_read_reg(client, regh, &oldh))
373 || (err = lm90_read_reg(client, regl, &l))
374 || (err = lm90_read_reg(client, regh, &newh)))
375 return err;
376 if (oldh != newh) {
377 err = lm90_read_reg(client, regl, &l);
378 if (err)
379 return err;
380 }
381 *value = (newh << 8) | l;
382
383 return 0;
384}
385
386/*
387 * client->update_lock must be held when calling this function (unless we are
388 * in detection or initialization steps), and while a remote channel other
389 * than channel 0 is selected. Also, calling code must make sure to re-select
390 * external channel 0 before releasing the lock. This is necessary because
391 * various registers have different meanings as a result of selecting a
392 * non-default remote channel.
393 */
394static inline void lm90_select_remote_channel(struct i2c_client *client,
395 struct lm90_data *data,
396 int channel)
397{
398 u8 config;
399
400 if (data->kind == max6696) {
401 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
402 config &= ~0x08;
403 if (channel)
404 config |= 0x08;
405 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
406 config);
407 }
408}
409
410/*
411 * Set conversion rate.
412 * client->update_lock must be held when calling this function (unless we are
413 * in detection or initialization steps).
414 */
415static void lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
416 unsigned int interval)
417{
418 int i;
419 unsigned int update_interval;
420
421 /* Shift calculations to avoid rounding errors */
422 interval <<= 6;
423
424 /* find the nearest update rate */
425 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
426 i < data->max_convrate; i++, update_interval >>= 1)
427 if (interval >= update_interval * 3 / 4)
428 break;
429
430 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i);
431 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
432}
433
434static struct lm90_data *lm90_update_device(struct device *dev)
435{
436 struct i2c_client *client = to_i2c_client(dev);
437 struct lm90_data *data = i2c_get_clientdata(client);
438 unsigned long next_update;
439
440 mutex_lock(&data->update_lock);
441
442 next_update = data->last_updated
443 + msecs_to_jiffies(data->update_interval) + 1;
444 if (time_after(jiffies, next_update) || !data->valid) {
445 u8 h, l;
446 u8 alarms;
447
448 dev_dbg(&client->dev, "Updating lm90 data.\n");
449 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
450 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
451 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
452 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
453 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
454
455 if (data->flags & LM90_HAVE_LOCAL_EXT) {
456 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
457 MAX6657_REG_R_LOCAL_TEMPL,
458 &data->temp11[4]);
459 } else {
460 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
461 &h) == 0)
462 data->temp11[4] = h << 8;
463 }
464 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
465 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
466
467 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
468 data->temp11[1] = h << 8;
469 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
470 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
471 &l) == 0)
472 data->temp11[1] |= l;
473 }
474 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
475 data->temp11[2] = h << 8;
476 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
477 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
478 &l) == 0)
479 data->temp11[2] |= l;
480 }
481
482 if (data->flags & LM90_HAVE_OFFSET) {
483 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
484 &h) == 0
485 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
486 &l) == 0)
487 data->temp11[3] = (h << 8) | l;
488 }
489 if (data->flags & LM90_HAVE_EMERGENCY) {
490 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
491 &data->temp8[4]);
492 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
493 &data->temp8[5]);
494 }
495 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
496 data->alarms = alarms; /* save as 16 bit value */
497
498 if (data->kind == max6696) {
499 lm90_select_remote_channel(client, data, 1);
500 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
501 &data->temp8[6]);
502 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
503 &data->temp8[7]);
504 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
505 LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
506 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
507 data->temp11[6] = h << 8;
508 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
509 data->temp11[7] = h << 8;
510 lm90_select_remote_channel(client, data, 0);
511
512 if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
513 &alarms))
514 data->alarms |= alarms << 8;
515 }
516
517 /* Re-enable ALERT# output if it was originally enabled and
518 * relevant alarms are all clear */
519 if ((data->config_orig & 0x80) == 0
520 && (data->alarms & data->alert_alarms) == 0) {
521 u8 config;
522
523 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
524 if (config & 0x80) {
525 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
526 i2c_smbus_write_byte_data(client,
527 LM90_REG_W_CONFIG1,
528 config & ~0x80);
529 }
530 }
531
532 data->last_updated = jiffies;
533 data->valid = 1;
534 }
535
536 mutex_unlock(&data->update_lock);
537
538 return data;
539}
540
541/*
226 * Conversions 542 * Conversions
227 * For local temperatures and limits, critical limits and the hysteresis 543 * For local temperatures and limits, critical limits and the hysteresis
228 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius. 544 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
@@ -377,18 +693,27 @@ static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
377static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, 693static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
378 const char *buf, size_t count) 694 const char *buf, size_t count)
379{ 695{
380 static const u8 reg[4] = { 696 static const u8 reg[8] = {
381 LM90_REG_W_LOCAL_LOW, 697 LM90_REG_W_LOCAL_LOW,
382 LM90_REG_W_LOCAL_HIGH, 698 LM90_REG_W_LOCAL_HIGH,
383 LM90_REG_W_LOCAL_CRIT, 699 LM90_REG_W_LOCAL_CRIT,
384 LM90_REG_W_REMOTE_CRIT, 700 LM90_REG_W_REMOTE_CRIT,
701 MAX6659_REG_W_LOCAL_EMERG,
702 MAX6659_REG_W_REMOTE_EMERG,
703 LM90_REG_W_REMOTE_CRIT,
704 MAX6659_REG_W_REMOTE_EMERG,
385 }; 705 };
386 706
387 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 707 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
388 struct i2c_client *client = to_i2c_client(dev); 708 struct i2c_client *client = to_i2c_client(dev);
389 struct lm90_data *data = i2c_get_clientdata(client); 709 struct lm90_data *data = i2c_get_clientdata(client);
390 long val = simple_strtol(buf, NULL, 10);
391 int nr = attr->index; 710 int nr = attr->index;
711 long val;
712 int err;
713
714 err = strict_strtol(buf, 10, &val);
715 if (err < 0)
716 return err;
392 717
393 /* +16 degrees offset for temp2 for the LM99 */ 718 /* +16 degrees offset for temp2 for the LM99 */
394 if (data->kind == lm99 && attr->index == 3) 719 if (data->kind == lm99 && attr->index == 3)
@@ -401,7 +726,11 @@ static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
401 data->temp8[nr] = temp_to_u8(val); 726 data->temp8[nr] = temp_to_u8(val);
402 else 727 else
403 data->temp8[nr] = temp_to_s8(val); 728 data->temp8[nr] = temp_to_s8(val);
729
730 lm90_select_remote_channel(client, data, nr >= 6);
404 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]); 731 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
732 lm90_select_remote_channel(client, data, 0);
733
405 mutex_unlock(&data->update_lock); 734 mutex_unlock(&data->update_lock);
406 return count; 735 return count;
407} 736}
@@ -409,7 +738,7 @@ static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
409static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, 738static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
410 char *buf) 739 char *buf)
411{ 740{
412 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 741 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
413 struct lm90_data *data = lm90_update_device(dev); 742 struct lm90_data *data = lm90_update_device(dev);
414 int temp; 743 int temp;
415 744
@@ -430,46 +759,58 @@ static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
430static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, 759static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
431 const char *buf, size_t count) 760 const char *buf, size_t count)
432{ 761{
433 static const u8 reg[6] = { 762 struct {
434 LM90_REG_W_REMOTE_LOWH, 763 u8 high;
435 LM90_REG_W_REMOTE_LOWL, 764 u8 low;
436 LM90_REG_W_REMOTE_HIGHH, 765 int channel;
437 LM90_REG_W_REMOTE_HIGHL, 766 } reg[5] = {
438 LM90_REG_W_REMOTE_OFFSH, 767 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
439 LM90_REG_W_REMOTE_OFFSL, 768 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
769 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
770 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
771 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
440 }; 772 };
441 773
442 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 774 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
443 struct i2c_client *client = to_i2c_client(dev); 775 struct i2c_client *client = to_i2c_client(dev);
444 struct lm90_data *data = i2c_get_clientdata(client); 776 struct lm90_data *data = i2c_get_clientdata(client);
445 long val = simple_strtol(buf, NULL, 10); 777 int nr = attr->nr;
446 int nr = attr->index; 778 int index = attr->index;
779 long val;
780 int err;
781
782 err = strict_strtol(buf, 10, &val);
783 if (err < 0)
784 return err;
447 785
448 /* +16 degrees offset for temp2 for the LM99 */ 786 /* +16 degrees offset for temp2 for the LM99 */
449 if (data->kind == lm99 && attr->index <= 2) 787 if (data->kind == lm99 && index <= 2)
450 val -= 16000; 788 val -= 16000;
451 789
452 mutex_lock(&data->update_lock); 790 mutex_lock(&data->update_lock);
453 if (data->kind == adt7461) 791 if (data->kind == adt7461)
454 data->temp11[nr] = temp_to_u16_adt7461(data, val); 792 data->temp11[index] = temp_to_u16_adt7461(data, val);
455 else if (data->kind == max6657 || data->kind == max6680)
456 data->temp11[nr] = temp_to_s8(val) << 8;
457 else if (data->kind == max6646) 793 else if (data->kind == max6646)
458 data->temp11[nr] = temp_to_u8(val) << 8; 794 data->temp11[index] = temp_to_u8(val) << 8;
795 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
796 data->temp11[index] = temp_to_s16(val);
459 else 797 else
460 data->temp11[nr] = temp_to_s16(val); 798 data->temp11[index] = temp_to_s8(val) << 8;
461 799
462 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], 800 lm90_select_remote_channel(client, data, reg[nr].channel);
463 data->temp11[nr] >> 8); 801 i2c_smbus_write_byte_data(client, reg[nr].high,
464 if (data->kind != max6657 && data->kind != max6680 802 data->temp11[index] >> 8);
465 && data->kind != max6646) 803 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
466 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], 804 i2c_smbus_write_byte_data(client, reg[nr].low,
467 data->temp11[nr] & 0xff); 805 data->temp11[index] & 0xff);
806 lm90_select_remote_channel(client, data, 0);
807
468 mutex_unlock(&data->update_lock); 808 mutex_unlock(&data->update_lock);
469 return count; 809 return count;
470} 810}
471 811
472static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr, 812static ssize_t show_temphyst(struct device *dev,
813 struct device_attribute *devattr,
473 char *buf) 814 char *buf)
474{ 815{
475 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 816 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
@@ -495,9 +836,14 @@ static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
495{ 836{
496 struct i2c_client *client = to_i2c_client(dev); 837 struct i2c_client *client = to_i2c_client(dev);
497 struct lm90_data *data = i2c_get_clientdata(client); 838 struct lm90_data *data = i2c_get_clientdata(client);
498 long val = simple_strtol(buf, NULL, 10); 839 long val;
840 int err;
499 int temp; 841 int temp;
500 842
843 err = strict_strtol(buf, 10, &val);
844 if (err < 0)
845 return err;
846
501 mutex_lock(&data->update_lock); 847 mutex_lock(&data->update_lock);
502 if (data->kind == adt7461) 848 if (data->kind == adt7461)
503 temp = temp_from_u8_adt7461(data, data->temp8[2]); 849 temp = temp_from_u8_adt7461(data, data->temp8[2]);
@@ -530,16 +876,44 @@ static ssize_t show_alarm(struct device *dev, struct device_attribute
530 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); 876 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
531} 877}
532 878
533static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4); 879static ssize_t show_update_interval(struct device *dev,
534static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); 880 struct device_attribute *attr, char *buf)
881{
882 struct lm90_data *data = dev_get_drvdata(dev);
883
884 return sprintf(buf, "%u\n", data->update_interval);
885}
886
887static ssize_t set_update_interval(struct device *dev,
888 struct device_attribute *attr,
889 const char *buf, size_t count)
890{
891 struct i2c_client *client = to_i2c_client(dev);
892 struct lm90_data *data = i2c_get_clientdata(client);
893 unsigned long val;
894 int err;
895
896 err = strict_strtoul(buf, 10, &val);
897 if (err)
898 return err;
899
900 mutex_lock(&data->update_lock);
901 lm90_set_convrate(client, data, val);
902 mutex_unlock(&data->update_lock);
903
904 return count;
905}
906
907static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
908static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
535static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8, 909static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
536 set_temp8, 0); 910 set_temp8, 0);
537static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, 911static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
538 set_temp11, 1); 912 set_temp11, 0, 1);
539static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, 913static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
540 set_temp8, 1); 914 set_temp8, 1);
541static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, 915static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
542 set_temp11, 2); 916 set_temp11, 1, 2);
543static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8, 917static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
544 set_temp8, 2); 918 set_temp8, 2);
545static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8, 919static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
@@ -547,8 +921,8 @@ static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
547static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst, 921static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
548 set_temphyst, 2); 922 set_temphyst, 2);
549static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3); 923static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
550static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, 924static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
551 set_temp11, 3); 925 set_temp11, 2, 3);
552 926
553/* Individual alarm files */ 927/* Individual alarm files */
554static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); 928static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
@@ -561,6 +935,9 @@ static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
561/* Raw alarm file for compatibility */ 935/* Raw alarm file for compatibility */
562static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); 936static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
563 937
938static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
939 set_update_interval);
940
564static struct attribute *lm90_attributes[] = { 941static struct attribute *lm90_attributes[] = {
565 &sensor_dev_attr_temp1_input.dev_attr.attr, 942 &sensor_dev_attr_temp1_input.dev_attr.attr,
566 &sensor_dev_attr_temp2_input.dev_attr.attr, 943 &sensor_dev_attr_temp2_input.dev_attr.attr,
@@ -581,6 +958,7 @@ static struct attribute *lm90_attributes[] = {
581 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 958 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
582 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 959 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
583 &dev_attr_alarms.attr, 960 &dev_attr_alarms.attr,
961 &dev_attr_update_interval.attr,
584 NULL 962 NULL
585}; 963};
586 964
@@ -588,6 +966,86 @@ static const struct attribute_group lm90_group = {
588 .attrs = lm90_attributes, 966 .attrs = lm90_attributes,
589}; 967};
590 968
969/*
970 * Additional attributes for devices with emergency sensors
971 */
972static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
973 set_temp8, 4);
974static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
975 set_temp8, 5);
976static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
977 NULL, 4);
978static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
979 NULL, 5);
980
981static struct attribute *lm90_emergency_attributes[] = {
982 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
983 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
984 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
985 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
986 NULL
987};
988
989static const struct attribute_group lm90_emergency_group = {
990 .attrs = lm90_emergency_attributes,
991};
992
993static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
994static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
995
996static struct attribute *lm90_emergency_alarm_attributes[] = {
997 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
998 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
999 NULL
1000};
1001
1002static const struct attribute_group lm90_emergency_alarm_group = {
1003 .attrs = lm90_emergency_alarm_attributes,
1004};
1005
1006/*
1007 * Additional attributes for devices with 3 temperature sensors
1008 */
1009static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
1010static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
1011 set_temp11, 3, 6);
1012static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
1013 set_temp11, 4, 7);
1014static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
1015 set_temp8, 6);
1016static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
1017static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
1018 set_temp8, 7);
1019static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
1020 NULL, 7);
1021
1022static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
1023static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
1024static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
1025static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
1026static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
1027
1028static struct attribute *lm90_temp3_attributes[] = {
1029 &sensor_dev_attr_temp3_input.dev_attr.attr,
1030 &sensor_dev_attr_temp3_min.dev_attr.attr,
1031 &sensor_dev_attr_temp3_max.dev_attr.attr,
1032 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1033 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1034 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
1035 &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
1036
1037 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1038 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
1039 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
1040 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
1041 &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
1042 NULL
1043};
1044
1045static const struct attribute_group lm90_temp3_group = {
1046 .attrs = lm90_temp3_attributes,
1047};
1048
591/* pec used for ADM1032 only */ 1049/* pec used for ADM1032 only */
592static ssize_t show_pec(struct device *dev, struct device_attribute *dummy, 1050static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
593 char *buf) 1051 char *buf)
@@ -600,7 +1058,12 @@ static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
600 const char *buf, size_t count) 1058 const char *buf, size_t count)
601{ 1059{
602 struct i2c_client *client = to_i2c_client(dev); 1060 struct i2c_client *client = to_i2c_client(dev);
603 long val = simple_strtol(buf, NULL, 10); 1061 long val;
1062 int err;
1063
1064 err = strict_strtol(buf, 10, &val);
1065 if (err < 0)
1066 return err;
604 1067
605 switch (val) { 1068 switch (val) {
606 case 0: 1069 case 0:
@@ -622,40 +1085,6 @@ static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
622 * Real code 1085 * Real code
623 */ 1086 */
624 1087
625/* The ADM1032 supports PEC but not on write byte transactions, so we need
626 to explicitly ask for a transaction without PEC. */
627static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
628{
629 return i2c_smbus_xfer(client->adapter, client->addr,
630 client->flags & ~I2C_CLIENT_PEC,
631 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
632}
633
634/* It is assumed that client->update_lock is held (unless we are in
635 detection or initialization steps). This matters when PEC is enabled,
636 because we don't want the address pointer to change between the write
637 byte and the read byte transactions. */
638static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
639{
640 int err;
641
642 if (client->flags & I2C_CLIENT_PEC) {
643 err = adm1032_write_byte(client, reg);
644 if (err >= 0)
645 err = i2c_smbus_read_byte(client);
646 } else
647 err = i2c_smbus_read_byte_data(client, reg);
648
649 if (err < 0) {
650 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
651 reg, err);
652 return err;
653 }
654 *value = err;
655
656 return 0;
657}
658
659/* Return 0 if detection is successful, -ENODEV otherwise */ 1088/* Return 0 if detection is successful, -ENODEV otherwise */
660static int lm90_detect(struct i2c_client *new_client, 1089static int lm90_detect(struct i2c_client *new_client,
661 struct i2c_board_info *info) 1090 struct i2c_board_info *info)
@@ -727,9 +1156,31 @@ static int lm90_detect(struct i2c_client *new_client,
727 && (reg_config1 & 0x1B) == 0x00 1156 && (reg_config1 & 0x1B) == 0x00
728 && reg_convrate <= 0x0A) { 1157 && reg_convrate <= 0x0A) {
729 name = "adt7461"; 1158 name = "adt7461";
1159 } else
1160 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
1161 && (reg_config1 & 0x1B) == 0x00
1162 && reg_convrate <= 0x0A) {
1163 name = "adt7461a";
730 } 1164 }
731 } else 1165 } else
732 if (man_id == 0x4D) { /* Maxim */ 1166 if (man_id == 0x4D) { /* Maxim */
1167 int reg_emerg, reg_emerg2, reg_status2;
1168
1169 /*
1170 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1171 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1172 * exists, both readings will reflect the same value. Otherwise,
1173 * the readings will be different.
1174 */
1175 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
1176 MAX6659_REG_R_REMOTE_EMERG)) < 0
1177 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
1178 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
1179 MAX6659_REG_R_REMOTE_EMERG)) < 0
1180 || (reg_status2 = i2c_smbus_read_byte_data(new_client,
1181 MAX6696_REG_R_STATUS2)) < 0)
1182 return -ENODEV;
1183
733 /* 1184 /*
734 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id 1185 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
735 * register. Reading from that address will return the last 1186 * register. Reading from that address will return the last
@@ -737,12 +1188,38 @@ static int lm90_detect(struct i2c_client *new_client,
737 * register. Likewise, the config1 register seems to lack a 1188 * register. Likewise, the config1 register seems to lack a
738 * low nibble, so the value will be those of the previous 1189 * low nibble, so the value will be those of the previous
739 * read, so in our case those of the man_id register. 1190 * read, so in our case those of the man_id register.
1191 * MAX6659 has a third set of upper temperature limit registers.
1192 * Those registers also return values on MAX6657 and MAX6658,
1193 * thus the only way to detect MAX6659 is by its address.
1194 * For this reason it will be mis-detected as MAX6657 if its
1195 * address is 0x4C.
740 */ 1196 */
741 if (chip_id == man_id 1197 if (chip_id == man_id
742 && (address == 0x4C || address == 0x4D) 1198 && (address == 0x4C || address == 0x4D || address == 0x4E)
743 && (reg_config1 & 0x1F) == (man_id & 0x0F) 1199 && (reg_config1 & 0x1F) == (man_id & 0x0F)
744 && reg_convrate <= 0x09) { 1200 && reg_convrate <= 0x09) {
745 name = "max6657"; 1201 if (address == 0x4C)
1202 name = "max6657";
1203 else
1204 name = "max6659";
1205 } else
1206 /*
1207 * Even though MAX6695 and MAX6696 do not have a chip ID
1208 * register, reading it returns 0x01. Bit 4 of the config1
1209 * register is unused and should return zero when read. Bit 0 of
1210 * the status2 register is unused and should return zero when
1211 * read.
1212 *
1213 * MAX6695 and MAX6696 have an additional set of temperature
1214 * limit registers. We can detect those chips by checking if
1215 * one of those registers exists.
1216 */
1217 if (chip_id == 0x01
1218 && (reg_config1 & 0x10) == 0x00
1219 && (reg_status2 & 0x01) == 0x00
1220 && reg_emerg == reg_emerg2
1221 && reg_convrate <= 0x07) {
1222 name = "max6696";
746 } else 1223 } else
747 /* 1224 /*
748 * The chip_id register of the MAX6680 and MAX6681 holds the 1225 * The chip_id register of the MAX6680 and MAX6681 holds the
@@ -768,10 +1245,23 @@ static int lm90_detect(struct i2c_client *new_client,
768 } else 1245 } else
769 if (address == 0x4C 1246 if (address == 0x4C
770 && man_id == 0x5C) { /* Winbond/Nuvoton */ 1247 && man_id == 0x5C) { /* Winbond/Nuvoton */
771 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */ 1248 int reg_config2;
772 && (reg_config1 & 0x2A) == 0x00 1249
773 && reg_convrate <= 0x08) { 1250 reg_config2 = i2c_smbus_read_byte_data(new_client,
774 name = "w83l771"; 1251 LM90_REG_R_CONFIG2);
1252 if (reg_config2 < 0)
1253 return -ENODEV;
1254
1255 if ((reg_config1 & 0x2A) == 0x00
1256 && (reg_config2 & 0xF8) == 0x00) {
1257 if (chip_id == 0x01 /* W83L771W/G */
1258 && reg_convrate <= 0x09) {
1259 name = "w83l771";
1260 } else
1261 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1262 && reg_convrate <= 0x08) {
1263 name = "w83l771";
1264 }
775 } 1265 }
776 } 1266 }
777 1267
@@ -787,6 +1277,69 @@ static int lm90_detect(struct i2c_client *new_client,
787 return 0; 1277 return 0;
788} 1278}
789 1279
1280static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1281{
1282 if (data->flags & LM90_HAVE_TEMP3)
1283 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1284 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1285 sysfs_remove_group(&client->dev.kobj,
1286 &lm90_emergency_alarm_group);
1287 if (data->flags & LM90_HAVE_EMERGENCY)
1288 sysfs_remove_group(&client->dev.kobj,
1289 &lm90_emergency_group);
1290 if (data->flags & LM90_HAVE_OFFSET)
1291 device_remove_file(&client->dev,
1292 &sensor_dev_attr_temp2_offset.dev_attr);
1293 device_remove_file(&client->dev, &dev_attr_pec);
1294 sysfs_remove_group(&client->dev.kobj, &lm90_group);
1295}
1296
1297static void lm90_init_client(struct i2c_client *client)
1298{
1299 u8 config, convrate;
1300 struct lm90_data *data = i2c_get_clientdata(client);
1301
1302 if (lm90_read_reg(client, LM90_REG_R_CONVRATE, &convrate) < 0) {
1303 dev_warn(&client->dev, "Failed to read convrate register!\n");
1304 convrate = LM90_DEF_CONVRATE_RVAL;
1305 }
1306 data->convrate_orig = convrate;
1307
1308 /*
1309 * Start the conversions.
1310 */
1311 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1312 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1313 dev_warn(&client->dev, "Initialization failed!\n");
1314 return;
1315 }
1316 data->config_orig = config;
1317
1318 /* Check Temperature Range Select */
1319 if (data->kind == adt7461) {
1320 if (config & 0x04)
1321 data->flags |= LM90_FLAG_ADT7461_EXT;
1322 }
1323
1324 /*
1325 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1326 * 0.125 degree resolution) and range (0x08, extend range
1327 * to -64 degree) mode for the remote temperature sensor.
1328 */
1329 if (data->kind == max6680)
1330 config |= 0x18;
1331
1332 /*
1333 * Select external channel 0 for max6695/96
1334 */
1335 if (data->kind == max6696)
1336 config &= ~0x08;
1337
1338 config &= 0xBF; /* run */
1339 if (config != data->config_orig) /* Only write if changed */
1340 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1341}
1342
790static int lm90_probe(struct i2c_client *new_client, 1343static int lm90_probe(struct i2c_client *new_client,
791 const struct i2c_device_id *id) 1344 const struct i2c_device_id *id)
792{ 1345{
@@ -811,31 +1364,48 @@ static int lm90_probe(struct i2c_client *new_client,
811 1364
812 /* Different devices have different alarm bits triggering the 1365 /* Different devices have different alarm bits triggering the
813 * ALERT# output */ 1366 * ALERT# output */
814 switch (data->kind) { 1367 data->alert_alarms = lm90_params[data->kind].alert_alarms;
815 case lm90: 1368
816 case lm99: 1369 /* Set chip capabilities */
817 case lm86: 1370 data->flags = lm90_params[data->kind].flags;
818 data->alert_alarms = 0x7b; 1371
819 break; 1372 /* Set maximum conversion rate */
820 default: 1373 data->max_convrate = lm90_params[data->kind].max_convrate;
821 data->alert_alarms = 0x7c;
822 break;
823 }
824 1374
825 /* Initialize the LM90 chip */ 1375 /* Initialize the LM90 chip */
826 lm90_init_client(new_client); 1376 lm90_init_client(new_client);
827 1377
828 /* Register sysfs hooks */ 1378 /* Register sysfs hooks */
829 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group))) 1379 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1380 if (err)
830 goto exit_free; 1381 goto exit_free;
831 if (new_client->flags & I2C_CLIENT_PEC) { 1382 if (new_client->flags & I2C_CLIENT_PEC) {
832 if ((err = device_create_file(&new_client->dev, 1383 err = device_create_file(&new_client->dev, &dev_attr_pec);
833 &dev_attr_pec))) 1384 if (err)
834 goto exit_remove_files; 1385 goto exit_remove_files;
835 } 1386 }
836 if (data->kind != max6657 && data->kind != max6646) { 1387 if (data->flags & LM90_HAVE_OFFSET) {
837 if ((err = device_create_file(&new_client->dev, 1388 err = device_create_file(&new_client->dev,
838 &sensor_dev_attr_temp2_offset.dev_attr))) 1389 &sensor_dev_attr_temp2_offset.dev_attr);
1390 if (err)
1391 goto exit_remove_files;
1392 }
1393 if (data->flags & LM90_HAVE_EMERGENCY) {
1394 err = sysfs_create_group(&new_client->dev.kobj,
1395 &lm90_emergency_group);
1396 if (err)
1397 goto exit_remove_files;
1398 }
1399 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1400 err = sysfs_create_group(&new_client->dev.kobj,
1401 &lm90_emergency_alarm_group);
1402 if (err)
1403 goto exit_remove_files;
1404 }
1405 if (data->flags & LM90_HAVE_TEMP3) {
1406 err = sysfs_create_group(&new_client->dev.kobj,
1407 &lm90_temp3_group);
1408 if (err)
839 goto exit_remove_files; 1409 goto exit_remove_files;
840 } 1410 }
841 1411
@@ -848,62 +1418,23 @@ static int lm90_probe(struct i2c_client *new_client,
848 return 0; 1418 return 0;
849 1419
850exit_remove_files: 1420exit_remove_files:
851 sysfs_remove_group(&new_client->dev.kobj, &lm90_group); 1421 lm90_remove_files(new_client, data);
852 device_remove_file(&new_client->dev, &dev_attr_pec);
853exit_free: 1422exit_free:
854 kfree(data); 1423 kfree(data);
855exit: 1424exit:
856 return err; 1425 return err;
857} 1426}
858 1427
859static void lm90_init_client(struct i2c_client *client)
860{
861 u8 config;
862 struct lm90_data *data = i2c_get_clientdata(client);
863
864 /*
865 * Start the conversions.
866 */
867 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
868 5); /* 2 Hz */
869 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
870 dev_warn(&client->dev, "Initialization failed!\n");
871 return;
872 }
873 data->config_orig = config;
874
875 /* Check Temperature Range Select */
876 if (data->kind == adt7461) {
877 if (config & 0x04)
878 data->flags |= LM90_FLAG_ADT7461_EXT;
879 }
880
881 /*
882 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
883 * 0.125 degree resolution) and range (0x08, extend range
884 * to -64 degree) mode for the remote temperature sensor.
885 */
886 if (data->kind == max6680) {
887 config |= 0x18;
888 }
889
890 config &= 0xBF; /* run */
891 if (config != data->config_orig) /* Only write if changed */
892 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
893}
894
895static int lm90_remove(struct i2c_client *client) 1428static int lm90_remove(struct i2c_client *client)
896{ 1429{
897 struct lm90_data *data = i2c_get_clientdata(client); 1430 struct lm90_data *data = i2c_get_clientdata(client);
898 1431
899 hwmon_device_unregister(data->hwmon_dev); 1432 hwmon_device_unregister(data->hwmon_dev);
900 sysfs_remove_group(&client->dev.kobj, &lm90_group); 1433 lm90_remove_files(client, data);
901 device_remove_file(&client->dev, &dev_attr_pec);
902 if (data->kind != max6657 && data->kind != max6646)
903 device_remove_file(&client->dev,
904 &sensor_dev_attr_temp2_offset.dev_attr);
905 1434
906 /* Restore initial configuration */ 1435 /* Restore initial configuration */
1436 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1437 data->convrate_orig);
907 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, 1438 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
908 data->config_orig); 1439 data->config_orig);
909 1440
@@ -914,10 +1445,14 @@ static int lm90_remove(struct i2c_client *client)
914static void lm90_alert(struct i2c_client *client, unsigned int flag) 1445static void lm90_alert(struct i2c_client *client, unsigned int flag)
915{ 1446{
916 struct lm90_data *data = i2c_get_clientdata(client); 1447 struct lm90_data *data = i2c_get_clientdata(client);
917 u8 config, alarms; 1448 u8 config, alarms, alarms2 = 0;
918 1449
919 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms); 1450 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
920 if ((alarms & 0x7f) == 0) { 1451
1452 if (data->kind == max6696)
1453 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1454
1455 if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
921 dev_info(&client->dev, "Everything OK\n"); 1456 dev_info(&client->dev, "Everything OK\n");
922 } else { 1457 } else {
923 if (alarms & 0x61) 1458 if (alarms & 0x61)
@@ -930,10 +1465,14 @@ static void lm90_alert(struct i2c_client *client, unsigned int flag)
930 dev_warn(&client->dev, 1465 dev_warn(&client->dev,
931 "temp%d diode open, please check!\n", 2); 1466 "temp%d diode open, please check!\n", 2);
932 1467
1468 if (alarms2 & 0x18)
1469 dev_warn(&client->dev,
1470 "temp%d out of range, please check!\n", 3);
1471
933 /* Disable ALERT# output, because these chips don't implement 1472 /* Disable ALERT# output, because these chips don't implement
934 SMBus alert correctly; they should only hold the alert line 1473 SMBus alert correctly; they should only hold the alert line
935 low briefly. */ 1474 low briefly. */
936 if ((data->kind == adm1032 || data->kind == adt7461) 1475 if ((data->flags & LM90_HAVE_BROKEN_ALERT)
937 && (alarms & data->alert_alarms)) { 1476 && (alarms & data->alert_alarms)) {
938 dev_dbg(&client->dev, "Disabling ALERT#\n"); 1477 dev_dbg(&client->dev, "Disabling ALERT#\n");
939 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); 1478 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
@@ -943,117 +1482,18 @@ static void lm90_alert(struct i2c_client *client, unsigned int flag)
943 } 1482 }
944} 1483}
945 1484
946static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value) 1485static struct i2c_driver lm90_driver = {
947{ 1486 .class = I2C_CLASS_HWMON,
948 int err; 1487 .driver = {
949 u8 oldh, newh, l; 1488 .name = "lm90",
950 1489 },
951 /* 1490 .probe = lm90_probe,
952 * There is a trick here. We have to read two registers to have the 1491 .remove = lm90_remove,
953 * sensor temperature, but we have to beware a conversion could occur 1492 .alert = lm90_alert,
954 * inbetween the readings. The datasheet says we should either use 1493 .id_table = lm90_id,
955 * the one-shot conversion register, which we don't want to do 1494 .detect = lm90_detect,
956 * (disables hardware monitoring) or monitor the busy bit, which is 1495 .address_list = normal_i2c,
957 * impossible (we can't read the values and monitor that bit at the 1496};
958 * exact same time). So the solution used here is to read the high
959 * byte once, then the low byte, then the high byte again. If the new
960 * high byte matches the old one, then we have a valid reading. Else
961 * we have to read the low byte again, and now we believe we have a
962 * correct reading.
963 */
964 if ((err = lm90_read_reg(client, regh, &oldh))
965 || (err = lm90_read_reg(client, regl, &l))
966 || (err = lm90_read_reg(client, regh, &newh)))
967 return err;
968 if (oldh != newh) {
969 err = lm90_read_reg(client, regl, &l);
970 if (err)
971 return err;
972 }
973 *value = (newh << 8) | l;
974
975 return 0;
976}
977
978static struct lm90_data *lm90_update_device(struct device *dev)
979{
980 struct i2c_client *client = to_i2c_client(dev);
981 struct lm90_data *data = i2c_get_clientdata(client);
982
983 mutex_lock(&data->update_lock);
984
985 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
986 || !data->valid) {
987 u8 h, l;
988
989 dev_dbg(&client->dev, "Updating lm90 data.\n");
990 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
991 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
992 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
993 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
994 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
995
996 if (data->kind == max6657 || data->kind == max6646) {
997 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
998 MAX6657_REG_R_LOCAL_TEMPL,
999 &data->temp11[4]);
1000 } else {
1001 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
1002 &h) == 0)
1003 data->temp11[4] = h << 8;
1004 }
1005 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
1006 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
1007
1008 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
1009 data->temp11[1] = h << 8;
1010 if (data->kind != max6657 && data->kind != max6680
1011 && data->kind != max6646
1012 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
1013 &l) == 0)
1014 data->temp11[1] |= l;
1015 }
1016 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
1017 data->temp11[2] = h << 8;
1018 if (data->kind != max6657 && data->kind != max6680
1019 && data->kind != max6646
1020 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
1021 &l) == 0)
1022 data->temp11[2] |= l;
1023 }
1024
1025 if (data->kind != max6657 && data->kind != max6646) {
1026 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
1027 &h) == 0
1028 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
1029 &l) == 0)
1030 data->temp11[3] = (h << 8) | l;
1031 }
1032 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
1033
1034 /* Re-enable ALERT# output if it was originally enabled and
1035 * relevant alarms are all clear */
1036 if ((data->config_orig & 0x80) == 0
1037 && (data->alarms & data->alert_alarms) == 0) {
1038 u8 config;
1039
1040 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1041 if (config & 0x80) {
1042 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1043 i2c_smbus_write_byte_data(client,
1044 LM90_REG_W_CONFIG1,
1045 config & ~0x80);
1046 }
1047 }
1048
1049 data->last_updated = jiffies;
1050 data->valid = 1;
1051 }
1052
1053 mutex_unlock(&data->update_lock);
1054
1055 return data;
1056}
1057 1497
1058static int __init sensors_lm90_init(void) 1498static int __init sensors_lm90_init(void)
1059{ 1499{