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