blob: 302d9eb9f27553183df877c09b89d9a18c7af3f1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
Jean Delvare95238362010-03-05 22:17:14 +01004 * Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Jean Delvarea874a102008-10-17 17:51:10 +02009 * temperature) and a 3-4 deg accuracy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Jean Delvare97ae60b2008-10-26 17:04:39 +010015 * degrees, which allows for higher temperatures measurement.
Steven Cole44bbe872005-05-03 18:21:25 -060016 * Note that there is no way to differentiate between both chips.
Jean Delvare97ae60b2008-10-26 17:04:39 +010017 * When device is auto-detected, the driver will assume an LM99.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
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
Jean Delvarea874a102008-10-17 17:51:10 +020025 * that are not handled by this driver. Among others, it has a higher
26 * accuracy than the LM90, much like the LM86 does.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
Jean Delvarea874a102008-10-17 17:51:10 +020029 * chips made by Maxim. These chips are similar to the LM86.
Steven Cole44bbe872005-05-03 18:21:25 -060030 * Note that there is no easy way to differentiate between the three
Guenter Roeck69487082010-10-28 20:31:43 +020031 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
Darrick J. Wong1a51e062009-03-12 13:36:38 +010037 * 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.
Ben Hutchings271dabf2008-10-17 17:51:11 +020041 *
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040042 * This driver also supports the MAX6680 and MAX6681, two other sensor
43 * chips made by Maxim. These are quite similar to the other Maxim
Jean Delvarea874a102008-10-17 17:51:10 +020044 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
45 * be treated identically.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040046 *
Guenter Roeck06e1c0a2010-10-28 20:31:43 +020047 * 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 *
Nate Case23b2d472008-10-17 17:51:10 +020052 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/module.h>
77#include <linux/init.h>
78#include <linux/slab.h>
79#include <linux/jiffies.h>
80#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020081#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040082#include <linux/hwmon.h>
83#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010084#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020085#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Addresses to scan
89 * Address is fully defined internally and cannot be changed except for
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040090 * MAX6659, MAX6680 and MAX6681.
Ben Hutchings271dabf2008-10-17 17:51:11 +020091 * 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.
Guenter Roeck13c84952010-10-28 20:31:43 +020095 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040096 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
97 * 0x4c, 0x4d or 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
99
Mark M. Hoffman25e9c862008-02-17 22:28:03 -0500100static const unsigned short normal_i2c[] = {
101 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Guenter Roeck13c84952010-10-28 20:31:43 +0200103enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200104 max6646, w83l771, max6696 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
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
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200145/* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200146
147#define MAX6657_REG_R_LOCAL_TEMPL 0x11
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200148#define MAX6696_REG_R_STATUS2 0x12
Guenter Roeck69487082010-10-28 20:31:43 +0200149#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
Jean Delvaref65e1702008-10-17 17:51:09 +0200153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Nate Case23b2d472008-10-17 17:51:10 +0200155 * Device flags
156 */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200157#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 */
Guenter Roeck69487082010-10-28 20:31:43 +0200162#define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200163#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
164#define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
Nate Case23b2d472008-10-17 17:51:10 +0200165
166/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * Driver data (common to all clients)
168 */
169
Jean Delvare9b0e8522008-07-16 19:30:15 +0200170static const struct i2c_device_id lm90_id[] = {
171 { "adm1032", adm1032 },
172 { "adt7461", adt7461 },
173 { "lm90", lm90 },
174 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100175 { "lm89", lm86 },
176 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200177 { "max6646", max6646 },
178 { "max6647", max6646 },
179 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200180 { "max6657", max6657 },
181 { "max6658", max6657 },
Guenter Roeck13c84952010-10-28 20:31:43 +0200182 { "max6659", max6659 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200183 { "max6680", max6680 },
184 { "max6681", max6680 },
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200185 { "max6695", max6696 },
186 { "max6696", max6696 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100187 { "w83l771", w83l771 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200188 { }
189};
190MODULE_DEVICE_TABLE(i2c, lm90_id);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * Client data (each client gets its own)
194 */
195
196struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700197 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100198 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 char valid; /* zero until following fields are valid */
200 unsigned long last_updated; /* in jiffies */
201 int kind;
Nate Case23b2d472008-10-17 17:51:10 +0200202 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Jean Delvare95238362010-03-05 22:17:14 +0100204 u8 config_orig; /* Original configuration register value */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200205 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
206 /* Upper 8 bits for max6695/96 */
Jean Delvare95238362010-03-05 22:17:14 +0100207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* registers values */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200209 s8 temp8[8]; /* 0: local low limit
Jean Delvaref65e1702008-10-17 17:51:09 +0200210 1: local high limit
211 2: local critical limit
Guenter Roeck69487082010-10-28 20:31:43 +0200212 3: remote critical limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200213 4: local emergency limit (max6659 and max6695/96)
214 5: remote emergency limit (max6659 and max6695/96)
215 6: remote 2 critical limit (max6695/96 only)
216 7: remote 2 emergency limit (max6695/96 only) */
217 s16 temp11[8]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200218 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200219 2: remote high limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200220 3: remote offset (except max6646, max6657/58/59,
221 and max6695/96)
222 4: local input
223 5: remote 2 input (max6695/96 only)
224 6: remote 2 low limit (max6695/96 only)
225 7: remote 2 high limit (ma6695/96 only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 u8 temp_hyst;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200227 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
230/*
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200231 * Support functions
232 */
233
234/*
235 * The ADM1032 supports PEC but not on write byte transactions, so we need
236 * to explicitly ask for a transaction without PEC.
237 */
238static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
239{
240 return i2c_smbus_xfer(client->adapter, client->addr,
241 client->flags & ~I2C_CLIENT_PEC,
242 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
243}
244
245/*
246 * It is assumed that client->update_lock is held (unless we are in
247 * detection or initialization steps). This matters when PEC is enabled,
248 * because we don't want the address pointer to change between the write
249 * byte and the read byte transactions.
250 */
251static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
252{
253 int err;
254
255 if (client->flags & I2C_CLIENT_PEC) {
256 err = adm1032_write_byte(client, reg);
257 if (err >= 0)
258 err = i2c_smbus_read_byte(client);
259 } else
260 err = i2c_smbus_read_byte_data(client, reg);
261
262 if (err < 0) {
263 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
264 reg, err);
265 return err;
266 }
267 *value = err;
268
269 return 0;
270}
271
272static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
273{
274 int err;
275 u8 oldh, newh, l;
276
277 /*
278 * There is a trick here. We have to read two registers to have the
279 * sensor temperature, but we have to beware a conversion could occur
280 * inbetween the readings. The datasheet says we should either use
281 * the one-shot conversion register, which we don't want to do
282 * (disables hardware monitoring) or monitor the busy bit, which is
283 * impossible (we can't read the values and monitor that bit at the
284 * exact same time). So the solution used here is to read the high
285 * byte once, then the low byte, then the high byte again. If the new
286 * high byte matches the old one, then we have a valid reading. Else
287 * we have to read the low byte again, and now we believe we have a
288 * correct reading.
289 */
290 if ((err = lm90_read_reg(client, regh, &oldh))
291 || (err = lm90_read_reg(client, regl, &l))
292 || (err = lm90_read_reg(client, regh, &newh)))
293 return err;
294 if (oldh != newh) {
295 err = lm90_read_reg(client, regl, &l);
296 if (err)
297 return err;
298 }
299 *value = (newh << 8) | l;
300
301 return 0;
302}
303
304/*
305 * client->update_lock must be held when calling this function (unless we are
306 * in detection or initialization steps), and while a remote channel other
307 * than channel 0 is selected. Also, calling code must make sure to re-select
308 * external channel 0 before releasing the lock. This is necessary because
309 * various registers have different meanings as a result of selecting a
310 * non-default remote channel.
311 */
312static inline void lm90_select_remote_channel(struct i2c_client *client,
313 struct lm90_data *data,
314 int channel)
315{
316 u8 config;
317
318 if (data->kind == max6696) {
319 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
320 config &= ~0x08;
321 if (channel)
322 config |= 0x08;
323 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
324 config);
325 }
326}
327
328static struct lm90_data *lm90_update_device(struct device *dev)
329{
330 struct i2c_client *client = to_i2c_client(dev);
331 struct lm90_data *data = i2c_get_clientdata(client);
332
333 mutex_lock(&data->update_lock);
334
335 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
336 || !data->valid) {
337 u8 h, l;
338 u8 alarms;
339
340 dev_dbg(&client->dev, "Updating lm90 data.\n");
341 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
342 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
343 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
344 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
345 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
346
347 if (data->flags & LM90_HAVE_LOCAL_EXT) {
348 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
349 MAX6657_REG_R_LOCAL_TEMPL,
350 &data->temp11[4]);
351 } else {
352 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
353 &h) == 0)
354 data->temp11[4] = h << 8;
355 }
356 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
357 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
358
359 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
360 data->temp11[1] = h << 8;
361 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
362 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
363 &l) == 0)
364 data->temp11[1] |= l;
365 }
366 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
367 data->temp11[2] = h << 8;
368 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
369 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
370 &l) == 0)
371 data->temp11[2] |= l;
372 }
373
374 if (data->flags & LM90_HAVE_OFFSET) {
375 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
376 &h) == 0
377 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
378 &l) == 0)
379 data->temp11[3] = (h << 8) | l;
380 }
381 if (data->flags & LM90_HAVE_EMERGENCY) {
382 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
383 &data->temp8[4]);
384 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
385 &data->temp8[5]);
386 }
387 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
388 data->alarms = alarms; /* save as 16 bit value */
389
390 if (data->kind == max6696) {
391 lm90_select_remote_channel(client, data, 1);
392 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
393 &data->temp8[6]);
394 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
395 &data->temp8[7]);
396 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
397 LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
398 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
399 data->temp11[6] = h << 8;
400 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
401 data->temp11[7] = h << 8;
402 lm90_select_remote_channel(client, data, 0);
403
404 if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
405 &alarms))
406 data->alarms |= alarms << 8;
407 }
408
409 /* Re-enable ALERT# output if it was originally enabled and
410 * relevant alarms are all clear */
411 if ((data->config_orig & 0x80) == 0
412 && (data->alarms & data->alert_alarms) == 0) {
413 u8 config;
414
415 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
416 if (config & 0x80) {
417 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
418 i2c_smbus_write_byte_data(client,
419 LM90_REG_W_CONFIG1,
420 config & ~0x80);
421 }
422 }
423
424 data->last_updated = jiffies;
425 data->valid = 1;
426 }
427
428 mutex_unlock(&data->update_lock);
429
430 return data;
431}
432
433/*
Nate Casecea50fe2008-10-17 17:51:10 +0200434 * Conversions
435 * For local temperatures and limits, critical limits and the hysteresis
436 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
437 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200438 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
439 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200440 */
441
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200442static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200443{
444 return val * 1000;
445}
446
Ben Hutchings271dabf2008-10-17 17:51:11 +0200447static inline int temp_from_u8(u8 val)
448{
449 return val * 1000;
450}
451
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200452static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200453{
454 return val / 32 * 125;
455}
456
Ben Hutchings271dabf2008-10-17 17:51:11 +0200457static inline int temp_from_u16(u16 val)
458{
459 return val / 32 * 125;
460}
461
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200462static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200463{
464 if (val <= -128000)
465 return -128;
466 if (val >= 127000)
467 return 127;
468 if (val < 0)
469 return (val - 500) / 1000;
470 return (val + 500) / 1000;
471}
472
Ben Hutchings271dabf2008-10-17 17:51:11 +0200473static u8 temp_to_u8(long val)
474{
475 if (val <= 0)
476 return 0;
477 if (val >= 255000)
478 return 255;
479 return (val + 500) / 1000;
480}
481
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200482static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200483{
484 if (val <= -128000)
485 return 0x8000;
486 if (val >= 127875)
487 return 0x7FE0;
488 if (val < 0)
489 return (val - 62) / 125 * 32;
490 return (val + 62) / 125 * 32;
491}
492
493static u8 hyst_to_reg(long val)
494{
495 if (val <= 0)
496 return 0;
497 if (val >= 30500)
498 return 31;
499 return (val + 500) / 1000;
500}
501
502/*
Nate Case23b2d472008-10-17 17:51:10 +0200503 * ADT7461 in compatibility mode is almost identical to LM90 except that
504 * attempts to write values that are outside the range 0 < temp < 127 are
505 * treated as the boundary value.
506 *
507 * ADT7461 in "extended mode" operation uses unsigned integers offset by
508 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200509 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200510static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200511{
Nate Case23b2d472008-10-17 17:51:10 +0200512 if (data->flags & LM90_FLAG_ADT7461_EXT)
513 return (val - 64) * 1000;
514 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200515 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200516}
517
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200518static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200519{
Nate Case23b2d472008-10-17 17:51:10 +0200520 if (data->flags & LM90_FLAG_ADT7461_EXT)
521 return (val - 0x4000) / 64 * 250;
522 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200523 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200524}
525
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200526static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200527{
528 if (data->flags & LM90_FLAG_ADT7461_EXT) {
529 if (val <= -64000)
530 return 0;
531 if (val >= 191000)
532 return 0xFF;
533 return (val + 500 + 64000) / 1000;
534 } else {
535 if (val <= 0)
536 return 0;
537 if (val >= 127000)
538 return 127;
539 return (val + 500) / 1000;
540 }
541}
542
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200543static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200544{
545 if (data->flags & LM90_FLAG_ADT7461_EXT) {
546 if (val <= -64000)
547 return 0;
548 if (val >= 191750)
549 return 0xFFC0;
550 return (val + 64000 + 125) / 250 * 64;
551 } else {
552 if (val <= 0)
553 return 0;
554 if (val >= 127750)
555 return 0x7FC0;
556 return (val + 125) / 250 * 64;
557 }
Nate Casecea50fe2008-10-17 17:51:10 +0200558}
559
560/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * Sysfs stuff
562 */
563
Jean Delvare30d73942005-06-05 21:27:28 +0200564static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
565 char *buf)
566{
567 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
568 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200569 int temp;
570
571 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200572 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200573 else if (data->kind == max6646)
574 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200575 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200576 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200577
Jean Delvare97ae60b2008-10-26 17:04:39 +0100578 /* +16 degrees offset for temp2 for the LM99 */
579 if (data->kind == lm99 && attr->index == 3)
580 temp += 16000;
581
Nate Case23b2d472008-10-17 17:51:10 +0200582 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Jean Delvare30d73942005-06-05 21:27:28 +0200585static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
586 const char *buf, size_t count)
587{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200588 static const u8 reg[8] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200589 LM90_REG_W_LOCAL_LOW,
590 LM90_REG_W_LOCAL_HIGH,
591 LM90_REG_W_LOCAL_CRIT,
592 LM90_REG_W_REMOTE_CRIT,
Guenter Roeck69487082010-10-28 20:31:43 +0200593 MAX6659_REG_W_LOCAL_EMERG,
594 MAX6659_REG_W_REMOTE_EMERG,
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200595 LM90_REG_W_REMOTE_CRIT,
596 MAX6659_REG_W_REMOTE_EMERG,
Jean Delvare30d73942005-06-05 21:27:28 +0200597 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Jean Delvare30d73942005-06-05 21:27:28 +0200599 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
600 struct i2c_client *client = to_i2c_client(dev);
601 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200602 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200603 long val;
604 int err;
605
606 err = strict_strtol(buf, 10, &val);
607 if (err < 0)
608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jean Delvare97ae60b2008-10-26 17:04:39 +0100610 /* +16 degrees offset for temp2 for the LM99 */
611 if (data->kind == lm99 && attr->index == 3)
612 val -= 16000;
613
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100614 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200615 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200616 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200617 else if (data->kind == max6646)
618 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200619 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200620 data->temp8[nr] = temp_to_s8(val);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200621
622 lm90_select_remote_channel(client, data, nr >= 6);
Jean Delvaref65e1702008-10-17 17:51:09 +0200623 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200624 lm90_select_remote_channel(client, data, 0);
625
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100626 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200627 return count;
628}
629
630static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
631 char *buf)
632{
Guenter Roeck96512862010-10-28 20:31:43 +0200633 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200634 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200635 int temp;
636
637 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200638 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200639 else if (data->kind == max6646)
640 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200641 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200642 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200643
Jean Delvare97ae60b2008-10-26 17:04:39 +0100644 /* +16 degrees offset for temp2 for the LM99 */
645 if (data->kind == lm99 && attr->index <= 2)
646 temp += 16000;
647
Nate Case23b2d472008-10-17 17:51:10 +0200648 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200649}
650
651static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
652 const char *buf, size_t count)
653{
Guenter Roeck96512862010-10-28 20:31:43 +0200654 struct {
655 u8 high;
656 u8 low;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200657 int channel;
658 } reg[5] = {
659 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
660 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
661 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
662 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
663 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
Jean Delvare30d73942005-06-05 21:27:28 +0200664 };
665
Guenter Roeck96512862010-10-28 20:31:43 +0200666 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200667 struct i2c_client *client = to_i2c_client(dev);
668 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck96512862010-10-28 20:31:43 +0200669 int nr = attr->nr;
670 int index = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200671 long val;
672 int err;
673
674 err = strict_strtol(buf, 10, &val);
675 if (err < 0)
676 return err;
Jean Delvare30d73942005-06-05 21:27:28 +0200677
Jean Delvare97ae60b2008-10-26 17:04:39 +0100678 /* +16 degrees offset for temp2 for the LM99 */
Guenter Roeck96512862010-10-28 20:31:43 +0200679 if (data->kind == lm99 && index <= 2)
Jean Delvare97ae60b2008-10-26 17:04:39 +0100680 val -= 16000;
681
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100682 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200683 if (data->kind == adt7461)
Guenter Roeck96512862010-10-28 20:31:43 +0200684 data->temp11[index] = temp_to_u16_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200685 else if (data->kind == max6646)
Guenter Roeck96512862010-10-28 20:31:43 +0200686 data->temp11[index] = temp_to_u8(val) << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +0200687 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200688 data->temp11[index] = temp_to_s16(val);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200689 else
Guenter Roeck96512862010-10-28 20:31:43 +0200690 data->temp11[index] = temp_to_s8(val) << 8;
Jean Delvare5f502a82008-10-17 17:51:09 +0200691
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200692 lm90_select_remote_channel(client, data, reg[nr].channel);
Guenter Roeck96512862010-10-28 20:31:43 +0200693 i2c_smbus_write_byte_data(client, reg[nr].high,
694 data->temp11[index] >> 8);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200695 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200696 i2c_smbus_write_byte_data(client, reg[nr].low,
697 data->temp11[index] & 0xff);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200698 lm90_select_remote_channel(client, data, 0);
699
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100700 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200701 return count;
702}
703
Guenter Roeck11e57812010-10-28 20:31:42 +0200704static ssize_t show_temphyst(struct device *dev,
705 struct device_attribute *devattr,
Jean Delvare30d73942005-06-05 21:27:28 +0200706 char *buf)
707{
708 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
709 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200710 int temp;
711
712 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200713 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100714 else if (data->kind == max6646)
715 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200716 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200717 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200718
Jean Delvare97ae60b2008-10-26 17:04:39 +0100719 /* +16 degrees offset for temp2 for the LM99 */
720 if (data->kind == lm99 && attr->index == 3)
721 temp += 16000;
722
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200723 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200724}
725
726static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
727 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 struct i2c_client *client = to_i2c_client(dev);
730 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck11e57812010-10-28 20:31:42 +0200731 long val;
732 int err;
Jean Delvareec38fa22008-10-26 17:04:39 +0100733 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Guenter Roeck11e57812010-10-28 20:31:42 +0200735 err = strict_strtol(buf, 10, &val);
736 if (err < 0)
737 return err;
738
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100739 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100740 if (data->kind == adt7461)
741 temp = temp_from_u8_adt7461(data, data->temp8[2]);
742 else if (data->kind == max6646)
743 temp = temp_from_u8(data->temp8[2]);
744 else
745 temp = temp_from_s8(data->temp8[2]);
746
747 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100749 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100750 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return count;
752}
753
Jean Delvare30d73942005-06-05 21:27:28 +0200754static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
755 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct lm90_data *data = lm90_update_device(dev);
758 return sprintf(buf, "%d\n", data->alarms);
759}
760
Jean Delvare2d457712006-09-24 20:52:15 +0200761static ssize_t show_alarm(struct device *dev, struct device_attribute
762 *devattr, char *buf)
763{
764 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
765 struct lm90_data *data = lm90_update_device(dev);
766 int bitnr = attr->index;
767
768 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
769}
770
Guenter Roeck96512862010-10-28 20:31:43 +0200771static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
772static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200773static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200774 set_temp8, 0);
Guenter Roeck96512862010-10-28 20:31:43 +0200775static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
776 set_temp11, 0, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200777static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200778 set_temp8, 1);
Guenter Roeck96512862010-10-28 20:31:43 +0200779static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
780 set_temp11, 1, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200781static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200782 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200783static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200784 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200785static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200786 set_temphyst, 2);
787static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Guenter Roeck96512862010-10-28 20:31:43 +0200788static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
789 set_temp11, 2, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200790
791/* Individual alarm files */
792static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
793static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400794static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200795static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
796static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
797static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
798static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
799/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
801
Jean Delvare0e39e012006-09-24 21:16:40 +0200802static struct attribute *lm90_attributes[] = {
803 &sensor_dev_attr_temp1_input.dev_attr.attr,
804 &sensor_dev_attr_temp2_input.dev_attr.attr,
805 &sensor_dev_attr_temp1_min.dev_attr.attr,
806 &sensor_dev_attr_temp2_min.dev_attr.attr,
807 &sensor_dev_attr_temp1_max.dev_attr.attr,
808 &sensor_dev_attr_temp2_max.dev_attr.attr,
809 &sensor_dev_attr_temp1_crit.dev_attr.attr,
810 &sensor_dev_attr_temp2_crit.dev_attr.attr,
811 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
812 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
813
814 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
815 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400816 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200817 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
818 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
819 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
820 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
821 &dev_attr_alarms.attr,
822 NULL
823};
824
825static const struct attribute_group lm90_group = {
826 .attrs = lm90_attributes,
827};
828
Guenter Roeck69487082010-10-28 20:31:43 +0200829/*
830 * Additional attributes for devices with emergency sensors
831 */
832static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
833 set_temp8, 4);
834static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
835 set_temp8, 5);
836static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
837 NULL, 4);
838static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
839 NULL, 5);
840
841static struct attribute *lm90_emergency_attributes[] = {
842 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
843 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
844 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
845 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
846 NULL
847};
848
849static const struct attribute_group lm90_emergency_group = {
850 .attrs = lm90_emergency_attributes,
851};
852
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200853static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
854static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
855
856static struct attribute *lm90_emergency_alarm_attributes[] = {
857 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
858 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
859 NULL
860};
861
862static const struct attribute_group lm90_emergency_alarm_group = {
863 .attrs = lm90_emergency_alarm_attributes,
864};
865
866/*
867 * Additional attributes for devices with 3 temperature sensors
868 */
869static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
870static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
871 set_temp11, 3, 6);
872static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
873 set_temp11, 4, 7);
874static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
875 set_temp8, 6);
876static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
877static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
878 set_temp8, 7);
879static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
880 NULL, 7);
881
882static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
883static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
884static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
885static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
886static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
887
888static struct attribute *lm90_temp3_attributes[] = {
889 &sensor_dev_attr_temp3_input.dev_attr.attr,
890 &sensor_dev_attr_temp3_min.dev_attr.attr,
891 &sensor_dev_attr_temp3_max.dev_attr.attr,
892 &sensor_dev_attr_temp3_crit.dev_attr.attr,
893 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
894 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
895 &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
896
897 &sensor_dev_attr_temp3_fault.dev_attr.attr,
898 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
899 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
900 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
901 &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
902 NULL
903};
904
905static const struct attribute_group lm90_temp3_group = {
906 .attrs = lm90_temp3_attributes,
907};
908
Jean Delvarec3df5802005-10-26 21:39:40 +0200909/* pec used for ADM1032 only */
910static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
911 char *buf)
912{
913 struct i2c_client *client = to_i2c_client(dev);
914 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
915}
916
917static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
918 const char *buf, size_t count)
919{
920 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200921 long val;
922 int err;
923
924 err = strict_strtol(buf, 10, &val);
925 if (err < 0)
926 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200927
928 switch (val) {
929 case 0:
930 client->flags &= ~I2C_CLIENT_PEC;
931 break;
932 case 1:
933 client->flags |= I2C_CLIENT_PEC;
934 break;
935 default:
936 return -EINVAL;
937 }
938
939 return count;
940}
941
942static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
945 * Real code
946 */
947
Jean Delvare9b0e8522008-07-16 19:30:15 +0200948/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100949static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200950 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200952 struct i2c_adapter *adapter = new_client->adapter;
953 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +0100954 const char *name = NULL;
955 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200958 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Jean Delvare8f2fa772009-12-09 20:35:53 +0100960 /* detection and identification */
961 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100962 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100963 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100964 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100965 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100966 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100967 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100968 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100969 return -ENODEV;
970
971 if ((address == 0x4C || address == 0x4D)
972 && man_id == 0x01) { /* National Semiconductor */
973 int reg_config2;
974
975 reg_config2 = i2c_smbus_read_byte_data(new_client,
976 LM90_REG_R_CONFIG2);
977 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200978 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Jean Delvare8f2fa772009-12-09 20:35:53 +0100980 if ((reg_config1 & 0x2A) == 0x00
981 && (reg_config2 & 0xF8) == 0x00
982 && reg_convrate <= 0x09) {
983 if (address == 0x4C
984 && (chip_id & 0xF0) == 0x20) { /* LM90 */
985 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100987 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
988 name = "lm99";
989 dev_info(&adapter->dev,
990 "Assuming LM99 chip at 0x%02x\n",
991 address);
992 dev_info(&adapter->dev,
993 "If it is an LM89, instantiate it "
994 "with the new_device sysfs "
995 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400996 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100997 if (address == 0x4C
998 && (chip_id & 0xF0) == 0x10) { /* LM86 */
999 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001002 } else
1003 if ((address == 0x4C || address == 0x4D)
1004 && man_id == 0x41) { /* Analog Devices */
1005 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1006 && (reg_config1 & 0x3F) == 0x00
1007 && reg_convrate <= 0x0A) {
1008 name = "adm1032";
1009 /* The ADM1032 supports PEC, but only if combined
1010 transactions are not used. */
1011 if (i2c_check_functionality(adapter,
1012 I2C_FUNC_SMBUS_BYTE))
1013 info->flags |= I2C_CLIENT_PEC;
1014 } else
1015 if (chip_id == 0x51 /* ADT7461 */
1016 && (reg_config1 & 0x1B) == 0x00
1017 && reg_convrate <= 0x0A) {
1018 name = "adt7461";
1019 }
1020 } else
1021 if (man_id == 0x4D) { /* Maxim */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001022 int reg_emerg, reg_emerg2, reg_status2;
1023
1024 /*
1025 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1026 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1027 * exists, both readings will reflect the same value. Otherwise,
1028 * the readings will be different.
1029 */
1030 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
1031 MAX6659_REG_R_REMOTE_EMERG)) < 0
1032 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
1033 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
1034 MAX6659_REG_R_REMOTE_EMERG)) < 0
1035 || (reg_status2 = i2c_smbus_read_byte_data(new_client,
1036 MAX6696_REG_R_STATUS2)) < 0)
1037 return -ENODEV;
1038
Jean Delvare8f2fa772009-12-09 20:35:53 +01001039 /*
1040 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1041 * register. Reading from that address will return the last
1042 * read value, which in our case is those of the man_id
1043 * register. Likewise, the config1 register seems to lack a
1044 * low nibble, so the value will be those of the previous
1045 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +02001046 * MAX6659 has a third set of upper temperature limit registers.
1047 * Those registers also return values on MAX6657 and MAX6658,
1048 * thus the only way to detect MAX6659 is by its address.
1049 * For this reason it will be mis-detected as MAX6657 if its
1050 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +01001051 */
1052 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +02001053 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001054 && (reg_config1 & 0x1F) == (man_id & 0x0F)
1055 && reg_convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +02001056 if (address == 0x4C)
1057 name = "max6657";
1058 else
1059 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001060 } else
1061 /*
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001062 * Even though MAX6695 and MAX6696 do not have a chip ID
1063 * register, reading it returns 0x01. Bit 4 of the config1
1064 * register is unused and should return zero when read. Bit 0 of
1065 * the status2 register is unused and should return zero when
1066 * read.
1067 *
1068 * MAX6695 and MAX6696 have an additional set of temperature
1069 * limit registers. We can detect those chips by checking if
1070 * one of those registers exists.
1071 */
1072 if (chip_id == 0x01
1073 && (reg_config1 & 0x10) == 0x00
1074 && (reg_status2 & 0x01) == 0x00
1075 && reg_emerg == reg_emerg2
1076 && reg_convrate <= 0x07) {
1077 name = "max6696";
1078 } else
1079 /*
Jean Delvare8f2fa772009-12-09 20:35:53 +01001080 * The chip_id register of the MAX6680 and MAX6681 holds the
1081 * revision of the chip. The lowest bit of the config1 register
1082 * is unused and should return zero when read, so should the
1083 * second to last bit of config1 (software reset).
1084 */
1085 if (chip_id == 0x01
1086 && (reg_config1 & 0x03) == 0x00
1087 && reg_convrate <= 0x07) {
1088 name = "max6680";
1089 } else
1090 /*
1091 * The chip_id register of the MAX6646/6647/6649 holds the
1092 * revision of the chip. The lowest 6 bits of the config1
1093 * register are unused and should return zero when read.
1094 */
1095 if (chip_id == 0x59
1096 && (reg_config1 & 0x3f) == 0x00
1097 && reg_convrate <= 0x07) {
1098 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001100 } else
1101 if (address == 0x4C
1102 && man_id == 0x5C) { /* Winbond/Nuvoton */
1103 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1104 && (reg_config1 & 0x2A) == 0x00
1105 && reg_convrate <= 0x08) {
1106 name = "w83l771";
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
Jean Delvare8f2fa772009-12-09 20:35:53 +01001110 if (!name) { /* identification failed */
1111 dev_dbg(&adapter->dev,
1112 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1113 "chip_id=0x%02X)\n", address, man_id, chip_id);
1114 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001116
Jean Delvare9b0e8522008-07-16 19:30:15 +02001117 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Jean Delvare9b0e8522008-07-16 19:30:15 +02001119 return 0;
1120}
1121
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001122static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1123{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001124 if (data->flags & LM90_HAVE_TEMP3)
1125 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1126 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1127 sysfs_remove_group(&client->dev.kobj,
1128 &lm90_emergency_alarm_group);
Guenter Roeck69487082010-10-28 20:31:43 +02001129 if (data->flags & LM90_HAVE_EMERGENCY)
1130 sysfs_remove_group(&client->dev.kobj,
1131 &lm90_emergency_group);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001132 if (data->flags & LM90_HAVE_OFFSET)
1133 device_remove_file(&client->dev,
1134 &sensor_dev_attr_temp2_offset.dev_attr);
1135 device_remove_file(&client->dev, &dev_attr_pec);
1136 sysfs_remove_group(&client->dev.kobj, &lm90_group);
1137}
1138
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001139static void lm90_init_client(struct i2c_client *client)
1140{
1141 u8 config;
1142 struct lm90_data *data = i2c_get_clientdata(client);
1143
1144 /*
1145 * Start the conversions.
1146 */
1147 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1148 5); /* 2 Hz */
1149 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1150 dev_warn(&client->dev, "Initialization failed!\n");
1151 return;
1152 }
1153 data->config_orig = config;
1154
1155 /* Check Temperature Range Select */
1156 if (data->kind == adt7461) {
1157 if (config & 0x04)
1158 data->flags |= LM90_FLAG_ADT7461_EXT;
1159 }
1160
1161 /*
1162 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1163 * 0.125 degree resolution) and range (0x08, extend range
1164 * to -64 degree) mode for the remote temperature sensor.
1165 */
1166 if (data->kind == max6680)
1167 config |= 0x18;
1168
1169 /*
1170 * Select external channel 0 for max6695/96
1171 */
1172 if (data->kind == max6696)
1173 config &= ~0x08;
1174
1175 config &= 0xBF; /* run */
1176 if (config != data->config_orig) /* Only write if changed */
1177 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1178}
1179
Jean Delvare9b0e8522008-07-16 19:30:15 +02001180static int lm90_probe(struct i2c_client *new_client,
1181 const struct i2c_device_id *id)
1182{
1183 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
1184 struct lm90_data *data;
1185 int err;
1186
1187 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
1188 if (!data) {
1189 err = -ENOMEM;
1190 goto exit;
1191 }
1192 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001193 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jean Delvare9b0e8522008-07-16 19:30:15 +02001195 /* Set the device type */
1196 data->kind = id->driver_data;
1197 if (data->kind == adm1032) {
1198 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1199 new_client->flags &= ~I2C_CLIENT_PEC;
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Jean Delvare53de3342010-03-05 22:17:15 +01001202 /* Different devices have different alarm bits triggering the
1203 * ALERT# output */
1204 switch (data->kind) {
1205 case lm90:
1206 case lm99:
1207 case lm86:
1208 data->alert_alarms = 0x7b;
1209 break;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001210 case max6696:
1211 data->alert_alarms = 0x187c;
1212 break;
Jean Delvare53de3342010-03-05 22:17:15 +01001213 default:
1214 data->alert_alarms = 0x7c;
1215 break;
1216 }
1217
Guenter Roeck88073bb2010-10-28 20:31:43 +02001218 /* Set chip capabilities */
Guenter Roeck13c84952010-10-28 20:31:43 +02001219 if (data->kind != max6657 && data->kind != max6659
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001220 && data->kind != max6646 && data->kind != max6696)
Guenter Roeck88073bb2010-10-28 20:31:43 +02001221 data->flags |= LM90_HAVE_OFFSET;
1222
Guenter Roeck13c84952010-10-28 20:31:43 +02001223 if (data->kind == max6657 || data->kind == max6659
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001224 || data->kind == max6646 || data->kind == max6696)
Guenter Roeck88073bb2010-10-28 20:31:43 +02001225 data->flags |= LM90_HAVE_LOCAL_EXT;
1226
Guenter Roeck13c84952010-10-28 20:31:43 +02001227 if (data->kind != max6657 && data->kind != max6659
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001228 && data->kind != max6646 && data->kind != max6680
1229 && data->kind != max6696)
Guenter Roeck88073bb2010-10-28 20:31:43 +02001230 data->flags |= LM90_HAVE_REM_LIMIT_EXT;
1231
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001232 if (data->kind == max6659 || data->kind == max6696)
Guenter Roeck69487082010-10-28 20:31:43 +02001233 data->flags |= LM90_HAVE_EMERGENCY;
1234
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001235 if (data->kind == max6696)
1236 data->flags |= LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3;
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /* Initialize the LM90 chip */
1239 lm90_init_client(new_client);
1240
1241 /* Register sysfs hooks */
Guenter Roeck11e57812010-10-28 20:31:42 +02001242 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1243 if (err)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001244 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +02001245 if (new_client->flags & I2C_CLIENT_PEC) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001246 err = device_create_file(&new_client->dev, &dev_attr_pec);
1247 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +02001248 goto exit_remove_files;
1249 }
Guenter Roeck88073bb2010-10-28 20:31:43 +02001250 if (data->flags & LM90_HAVE_OFFSET) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001251 err = device_create_file(&new_client->dev,
1252 &sensor_dev_attr_temp2_offset.dev_attr);
1253 if (err)
Jean Delvare69f2f962007-09-05 14:15:37 +02001254 goto exit_remove_files;
1255 }
Guenter Roeck69487082010-10-28 20:31:43 +02001256 if (data->flags & LM90_HAVE_EMERGENCY) {
1257 err = sysfs_create_group(&new_client->dev.kobj,
1258 &lm90_emergency_group);
1259 if (err)
1260 goto exit_remove_files;
1261 }
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001262 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1263 err = sysfs_create_group(&new_client->dev.kobj,
1264 &lm90_emergency_alarm_group);
1265 if (err)
1266 goto exit_remove_files;
1267 }
1268 if (data->flags & LM90_HAVE_TEMP3) {
1269 err = sysfs_create_group(&new_client->dev.kobj,
1270 &lm90_temp3_group);
1271 if (err)
1272 goto exit_remove_files;
1273 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001274
Tony Jones1beeffe2007-08-20 13:46:20 -07001275 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1276 if (IS_ERR(data->hwmon_dev)) {
1277 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001278 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001279 }
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
1282
Jean Delvare0e39e012006-09-24 21:16:40 +02001283exit_remove_files:
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001284 lm90_remove_files(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285exit_free:
1286 kfree(data);
1287exit:
1288 return err;
1289}
1290
Jean Delvare9b0e8522008-07-16 19:30:15 +02001291static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001293 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Tony Jones1beeffe2007-08-20 13:46:20 -07001295 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001296 lm90_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001297
Jean Delvare95238362010-03-05 22:17:14 +01001298 /* Restore initial configuration */
1299 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1300 data->config_orig);
1301
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001302 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return 0;
1304}
1305
Jean Delvare53de3342010-03-05 22:17:15 +01001306static void lm90_alert(struct i2c_client *client, unsigned int flag)
1307{
1308 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001309 u8 config, alarms, alarms2 = 0;
Jean Delvare53de3342010-03-05 22:17:15 +01001310
1311 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001312
1313 if (data->kind == max6696)
1314 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1315
1316 if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
Jean Delvare53de3342010-03-05 22:17:15 +01001317 dev_info(&client->dev, "Everything OK\n");
1318 } else {
1319 if (alarms & 0x61)
1320 dev_warn(&client->dev,
1321 "temp%d out of range, please check!\n", 1);
1322 if (alarms & 0x1a)
1323 dev_warn(&client->dev,
1324 "temp%d out of range, please check!\n", 2);
1325 if (alarms & 0x04)
1326 dev_warn(&client->dev,
1327 "temp%d diode open, please check!\n", 2);
1328
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001329 if (alarms2 & 0x18)
1330 dev_warn(&client->dev,
1331 "temp%d out of range, please check!\n", 3);
1332
Jean Delvare53de3342010-03-05 22:17:15 +01001333 /* Disable ALERT# output, because these chips don't implement
1334 SMBus alert correctly; they should only hold the alert line
1335 low briefly. */
1336 if ((data->kind == adm1032 || data->kind == adt7461)
1337 && (alarms & data->alert_alarms)) {
1338 dev_dbg(&client->dev, "Disabling ALERT#\n");
1339 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1340 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1341 config | 0x80);
1342 }
1343 }
1344}
1345
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001346static struct i2c_driver lm90_driver = {
1347 .class = I2C_CLASS_HWMON,
1348 .driver = {
1349 .name = "lm90",
1350 },
1351 .probe = lm90_probe,
1352 .remove = lm90_remove,
1353 .alert = lm90_alert,
1354 .id_table = lm90_id,
1355 .detect = lm90_detect,
1356 .address_list = normal_i2c,
1357};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359static int __init sensors_lm90_init(void)
1360{
1361 return i2c_add_driver(&lm90_driver);
1362}
1363
1364static void __exit sensors_lm90_exit(void)
1365{
1366 i2c_del_driver(&lm90_driver);
1367}
1368
1369MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1370MODULE_DESCRIPTION("LM90/ADM1032 driver");
1371MODULE_LICENSE("GPL");
1372
1373module_init(sensors_lm90_init);
1374module_exit(sensors_lm90_exit);