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