blob: 9df08e1cc5185c639dc5cf9bd6a0c94632b19d3b [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/*
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200193 * chip type specific parameters
194 */
195struct lm90_params {
196 u32 flags; /* Capabilities */
197 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
198 /* Upper 8 bits for max6695/96 */
199};
200
201static const struct lm90_params lm90_params[] = {
202 [adm1032] = {
203 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
204 .alert_alarms = 0x7c,
205 },
206 [adt7461] = {
207 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
208 .alert_alarms = 0x7c,
209 },
210 [lm86] = {
211 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
212 .alert_alarms = 0x7b,
213 },
214 [lm90] = {
215 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
216 .alert_alarms = 0x7b,
217 },
218 [lm99] = {
219 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
220 .alert_alarms = 0x7b,
221 },
222 [max6646] = {
223 .flags = LM90_HAVE_LOCAL_EXT,
224 .alert_alarms = 0x7c,
225 },
226 [max6657] = {
227 .flags = LM90_HAVE_LOCAL_EXT,
228 .alert_alarms = 0x7c,
229 },
230 [max6659] = {
231 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY,
232 .alert_alarms = 0x7c,
233 },
234 [max6680] = {
235 .flags = LM90_HAVE_OFFSET,
236 .alert_alarms = 0x7c,
237 },
238 [max6696] = {
239 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY
240 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
241 .alert_alarms = 0x187c,
242 },
243 [w83l771] = {
244 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
245 .alert_alarms = 0x7c,
246 },
247};
248
249/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * Client data (each client gets its own)
251 */
252
253struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700254 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100255 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 char valid; /* zero until following fields are valid */
257 unsigned long last_updated; /* in jiffies */
258 int kind;
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200259 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jean Delvare95238362010-03-05 22:17:14 +0100261 u8 config_orig; /* Original configuration register value */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200262 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
263 /* Upper 8 bits for max6695/96 */
Jean Delvare95238362010-03-05 22:17:14 +0100264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* registers values */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200266 s8 temp8[8]; /* 0: local low limit
Jean Delvaref65e1702008-10-17 17:51:09 +0200267 1: local high limit
268 2: local critical limit
Guenter Roeck69487082010-10-28 20:31:43 +0200269 3: remote critical limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200270 4: local emergency limit (max6659 and max6695/96)
271 5: remote emergency limit (max6659 and max6695/96)
272 6: remote 2 critical limit (max6695/96 only)
273 7: remote 2 emergency limit (max6695/96 only) */
274 s16 temp11[8]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200275 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200276 2: remote high limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200277 3: remote offset (except max6646, max6657/58/59,
278 and max6695/96)
279 4: local input
280 5: remote 2 input (max6695/96 only)
281 6: remote 2 low limit (max6695/96 only)
282 7: remote 2 high limit (ma6695/96 only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u8 temp_hyst;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200284 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
287/*
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200288 * Support functions
289 */
290
291/*
292 * The ADM1032 supports PEC but not on write byte transactions, so we need
293 * to explicitly ask for a transaction without PEC.
294 */
295static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
296{
297 return i2c_smbus_xfer(client->adapter, client->addr,
298 client->flags & ~I2C_CLIENT_PEC,
299 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
300}
301
302/*
303 * It is assumed that client->update_lock is held (unless we are in
304 * detection or initialization steps). This matters when PEC is enabled,
305 * because we don't want the address pointer to change between the write
306 * byte and the read byte transactions.
307 */
308static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
309{
310 int err;
311
312 if (client->flags & I2C_CLIENT_PEC) {
313 err = adm1032_write_byte(client, reg);
314 if (err >= 0)
315 err = i2c_smbus_read_byte(client);
316 } else
317 err = i2c_smbus_read_byte_data(client, reg);
318
319 if (err < 0) {
320 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
321 reg, err);
322 return err;
323 }
324 *value = err;
325
326 return 0;
327}
328
329static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
330{
331 int err;
332 u8 oldh, newh, l;
333
334 /*
335 * There is a trick here. We have to read two registers to have the
336 * sensor temperature, but we have to beware a conversion could occur
337 * inbetween the readings. The datasheet says we should either use
338 * the one-shot conversion register, which we don't want to do
339 * (disables hardware monitoring) or monitor the busy bit, which is
340 * impossible (we can't read the values and monitor that bit at the
341 * exact same time). So the solution used here is to read the high
342 * byte once, then the low byte, then the high byte again. If the new
343 * high byte matches the old one, then we have a valid reading. Else
344 * we have to read the low byte again, and now we believe we have a
345 * correct reading.
346 */
347 if ((err = lm90_read_reg(client, regh, &oldh))
348 || (err = lm90_read_reg(client, regl, &l))
349 || (err = lm90_read_reg(client, regh, &newh)))
350 return err;
351 if (oldh != newh) {
352 err = lm90_read_reg(client, regl, &l);
353 if (err)
354 return err;
355 }
356 *value = (newh << 8) | l;
357
358 return 0;
359}
360
361/*
362 * client->update_lock must be held when calling this function (unless we are
363 * in detection or initialization steps), and while a remote channel other
364 * than channel 0 is selected. Also, calling code must make sure to re-select
365 * external channel 0 before releasing the lock. This is necessary because
366 * various registers have different meanings as a result of selecting a
367 * non-default remote channel.
368 */
369static inline void lm90_select_remote_channel(struct i2c_client *client,
370 struct lm90_data *data,
371 int channel)
372{
373 u8 config;
374
375 if (data->kind == max6696) {
376 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
377 config &= ~0x08;
378 if (channel)
379 config |= 0x08;
380 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
381 config);
382 }
383}
384
385static struct lm90_data *lm90_update_device(struct device *dev)
386{
387 struct i2c_client *client = to_i2c_client(dev);
388 struct lm90_data *data = i2c_get_clientdata(client);
389
390 mutex_lock(&data->update_lock);
391
392 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
393 || !data->valid) {
394 u8 h, l;
395 u8 alarms;
396
397 dev_dbg(&client->dev, "Updating lm90 data.\n");
398 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
399 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
400 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
401 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
402 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
403
404 if (data->flags & LM90_HAVE_LOCAL_EXT) {
405 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
406 MAX6657_REG_R_LOCAL_TEMPL,
407 &data->temp11[4]);
408 } else {
409 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
410 &h) == 0)
411 data->temp11[4] = h << 8;
412 }
413 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
414 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
415
416 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
417 data->temp11[1] = h << 8;
418 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
419 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
420 &l) == 0)
421 data->temp11[1] |= l;
422 }
423 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
424 data->temp11[2] = h << 8;
425 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
426 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
427 &l) == 0)
428 data->temp11[2] |= l;
429 }
430
431 if (data->flags & LM90_HAVE_OFFSET) {
432 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
433 &h) == 0
434 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
435 &l) == 0)
436 data->temp11[3] = (h << 8) | l;
437 }
438 if (data->flags & LM90_HAVE_EMERGENCY) {
439 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
440 &data->temp8[4]);
441 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
442 &data->temp8[5]);
443 }
444 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
445 data->alarms = alarms; /* save as 16 bit value */
446
447 if (data->kind == max6696) {
448 lm90_select_remote_channel(client, data, 1);
449 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
450 &data->temp8[6]);
451 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
452 &data->temp8[7]);
453 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
454 LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
455 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
456 data->temp11[6] = h << 8;
457 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
458 data->temp11[7] = h << 8;
459 lm90_select_remote_channel(client, data, 0);
460
461 if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
462 &alarms))
463 data->alarms |= alarms << 8;
464 }
465
466 /* Re-enable ALERT# output if it was originally enabled and
467 * relevant alarms are all clear */
468 if ((data->config_orig & 0x80) == 0
469 && (data->alarms & data->alert_alarms) == 0) {
470 u8 config;
471
472 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
473 if (config & 0x80) {
474 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
475 i2c_smbus_write_byte_data(client,
476 LM90_REG_W_CONFIG1,
477 config & ~0x80);
478 }
479 }
480
481 data->last_updated = jiffies;
482 data->valid = 1;
483 }
484
485 mutex_unlock(&data->update_lock);
486
487 return data;
488}
489
490/*
Nate Casecea50fe2008-10-17 17:51:10 +0200491 * Conversions
492 * For local temperatures and limits, critical limits and the hysteresis
493 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
494 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200495 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
496 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200497 */
498
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200499static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200500{
501 return val * 1000;
502}
503
Ben Hutchings271dabf2008-10-17 17:51:11 +0200504static inline int temp_from_u8(u8 val)
505{
506 return val * 1000;
507}
508
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200509static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200510{
511 return val / 32 * 125;
512}
513
Ben Hutchings271dabf2008-10-17 17:51:11 +0200514static inline int temp_from_u16(u16 val)
515{
516 return val / 32 * 125;
517}
518
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200519static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200520{
521 if (val <= -128000)
522 return -128;
523 if (val >= 127000)
524 return 127;
525 if (val < 0)
526 return (val - 500) / 1000;
527 return (val + 500) / 1000;
528}
529
Ben Hutchings271dabf2008-10-17 17:51:11 +0200530static u8 temp_to_u8(long val)
531{
532 if (val <= 0)
533 return 0;
534 if (val >= 255000)
535 return 255;
536 return (val + 500) / 1000;
537}
538
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200539static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200540{
541 if (val <= -128000)
542 return 0x8000;
543 if (val >= 127875)
544 return 0x7FE0;
545 if (val < 0)
546 return (val - 62) / 125 * 32;
547 return (val + 62) / 125 * 32;
548}
549
550static u8 hyst_to_reg(long val)
551{
552 if (val <= 0)
553 return 0;
554 if (val >= 30500)
555 return 31;
556 return (val + 500) / 1000;
557}
558
559/*
Nate Case23b2d472008-10-17 17:51:10 +0200560 * ADT7461 in compatibility mode is almost identical to LM90 except that
561 * attempts to write values that are outside the range 0 < temp < 127 are
562 * treated as the boundary value.
563 *
564 * ADT7461 in "extended mode" operation uses unsigned integers offset by
565 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200566 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200567static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200568{
Nate Case23b2d472008-10-17 17:51:10 +0200569 if (data->flags & LM90_FLAG_ADT7461_EXT)
570 return (val - 64) * 1000;
571 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200572 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200573}
574
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200575static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200576{
Nate Case23b2d472008-10-17 17:51:10 +0200577 if (data->flags & LM90_FLAG_ADT7461_EXT)
578 return (val - 0x4000) / 64 * 250;
579 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200580 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200581}
582
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200583static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200584{
585 if (data->flags & LM90_FLAG_ADT7461_EXT) {
586 if (val <= -64000)
587 return 0;
588 if (val >= 191000)
589 return 0xFF;
590 return (val + 500 + 64000) / 1000;
591 } else {
592 if (val <= 0)
593 return 0;
594 if (val >= 127000)
595 return 127;
596 return (val + 500) / 1000;
597 }
598}
599
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200600static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200601{
602 if (data->flags & LM90_FLAG_ADT7461_EXT) {
603 if (val <= -64000)
604 return 0;
605 if (val >= 191750)
606 return 0xFFC0;
607 return (val + 64000 + 125) / 250 * 64;
608 } else {
609 if (val <= 0)
610 return 0;
611 if (val >= 127750)
612 return 0x7FC0;
613 return (val + 125) / 250 * 64;
614 }
Nate Casecea50fe2008-10-17 17:51:10 +0200615}
616
617/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * Sysfs stuff
619 */
620
Jean Delvare30d73942005-06-05 21:27:28 +0200621static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
622 char *buf)
623{
624 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
625 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200626 int temp;
627
628 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200629 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200630 else if (data->kind == max6646)
631 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200632 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200633 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200634
Jean Delvare97ae60b2008-10-26 17:04:39 +0100635 /* +16 degrees offset for temp2 for the LM99 */
636 if (data->kind == lm99 && attr->index == 3)
637 temp += 16000;
638
Nate Case23b2d472008-10-17 17:51:10 +0200639 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jean Delvare30d73942005-06-05 21:27:28 +0200642static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
643 const char *buf, size_t count)
644{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200645 static const u8 reg[8] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200646 LM90_REG_W_LOCAL_LOW,
647 LM90_REG_W_LOCAL_HIGH,
648 LM90_REG_W_LOCAL_CRIT,
649 LM90_REG_W_REMOTE_CRIT,
Guenter Roeck69487082010-10-28 20:31:43 +0200650 MAX6659_REG_W_LOCAL_EMERG,
651 MAX6659_REG_W_REMOTE_EMERG,
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200652 LM90_REG_W_REMOTE_CRIT,
653 MAX6659_REG_W_REMOTE_EMERG,
Jean Delvare30d73942005-06-05 21:27:28 +0200654 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Jean Delvare30d73942005-06-05 21:27:28 +0200656 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
657 struct i2c_client *client = to_i2c_client(dev);
658 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200659 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200660 long val;
661 int err;
662
663 err = strict_strtol(buf, 10, &val);
664 if (err < 0)
665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Jean Delvare97ae60b2008-10-26 17:04:39 +0100667 /* +16 degrees offset for temp2 for the LM99 */
668 if (data->kind == lm99 && attr->index == 3)
669 val -= 16000;
670
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100671 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200672 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200673 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200674 else if (data->kind == max6646)
675 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200676 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200677 data->temp8[nr] = temp_to_s8(val);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200678
679 lm90_select_remote_channel(client, data, nr >= 6);
Jean Delvaref65e1702008-10-17 17:51:09 +0200680 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200681 lm90_select_remote_channel(client, data, 0);
682
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100683 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200684 return count;
685}
686
687static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
688 char *buf)
689{
Guenter Roeck96512862010-10-28 20:31:43 +0200690 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200691 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200692 int temp;
693
694 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200695 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200696 else if (data->kind == max6646)
697 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200698 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200699 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200700
Jean Delvare97ae60b2008-10-26 17:04:39 +0100701 /* +16 degrees offset for temp2 for the LM99 */
702 if (data->kind == lm99 && attr->index <= 2)
703 temp += 16000;
704
Nate Case23b2d472008-10-17 17:51:10 +0200705 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200706}
707
708static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
709 const char *buf, size_t count)
710{
Guenter Roeck96512862010-10-28 20:31:43 +0200711 struct {
712 u8 high;
713 u8 low;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200714 int channel;
715 } reg[5] = {
716 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
717 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
718 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
719 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
720 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
Jean Delvare30d73942005-06-05 21:27:28 +0200721 };
722
Guenter Roeck96512862010-10-28 20:31:43 +0200723 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200724 struct i2c_client *client = to_i2c_client(dev);
725 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck96512862010-10-28 20:31:43 +0200726 int nr = attr->nr;
727 int index = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200728 long val;
729 int err;
730
731 err = strict_strtol(buf, 10, &val);
732 if (err < 0)
733 return err;
Jean Delvare30d73942005-06-05 21:27:28 +0200734
Jean Delvare97ae60b2008-10-26 17:04:39 +0100735 /* +16 degrees offset for temp2 for the LM99 */
Guenter Roeck96512862010-10-28 20:31:43 +0200736 if (data->kind == lm99 && index <= 2)
Jean Delvare97ae60b2008-10-26 17:04:39 +0100737 val -= 16000;
738
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100739 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200740 if (data->kind == adt7461)
Guenter Roeck96512862010-10-28 20:31:43 +0200741 data->temp11[index] = temp_to_u16_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200742 else if (data->kind == max6646)
Guenter Roeck96512862010-10-28 20:31:43 +0200743 data->temp11[index] = temp_to_u8(val) << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +0200744 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200745 data->temp11[index] = temp_to_s16(val);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200746 else
Guenter Roeck96512862010-10-28 20:31:43 +0200747 data->temp11[index] = temp_to_s8(val) << 8;
Jean Delvare5f502a82008-10-17 17:51:09 +0200748
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200749 lm90_select_remote_channel(client, data, reg[nr].channel);
Guenter Roeck96512862010-10-28 20:31:43 +0200750 i2c_smbus_write_byte_data(client, reg[nr].high,
751 data->temp11[index] >> 8);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200752 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200753 i2c_smbus_write_byte_data(client, reg[nr].low,
754 data->temp11[index] & 0xff);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200755 lm90_select_remote_channel(client, data, 0);
756
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100757 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200758 return count;
759}
760
Guenter Roeck11e57812010-10-28 20:31:42 +0200761static ssize_t show_temphyst(struct device *dev,
762 struct device_attribute *devattr,
Jean Delvare30d73942005-06-05 21:27:28 +0200763 char *buf)
764{
765 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
766 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200767 int temp;
768
769 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200770 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100771 else if (data->kind == max6646)
772 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200773 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200774 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200775
Jean Delvare97ae60b2008-10-26 17:04:39 +0100776 /* +16 degrees offset for temp2 for the LM99 */
777 if (data->kind == lm99 && attr->index == 3)
778 temp += 16000;
779
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200780 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200781}
782
783static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
784 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct i2c_client *client = to_i2c_client(dev);
787 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck11e57812010-10-28 20:31:42 +0200788 long val;
789 int err;
Jean Delvareec38fa22008-10-26 17:04:39 +0100790 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Guenter Roeck11e57812010-10-28 20:31:42 +0200792 err = strict_strtol(buf, 10, &val);
793 if (err < 0)
794 return err;
795
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100796 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100797 if (data->kind == adt7461)
798 temp = temp_from_u8_adt7461(data, data->temp8[2]);
799 else if (data->kind == max6646)
800 temp = temp_from_u8(data->temp8[2]);
801 else
802 temp = temp_from_s8(data->temp8[2]);
803
804 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100806 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100807 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return count;
809}
810
Jean Delvare30d73942005-06-05 21:27:28 +0200811static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
812 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct lm90_data *data = lm90_update_device(dev);
815 return sprintf(buf, "%d\n", data->alarms);
816}
817
Jean Delvare2d457712006-09-24 20:52:15 +0200818static ssize_t show_alarm(struct device *dev, struct device_attribute
819 *devattr, char *buf)
820{
821 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
822 struct lm90_data *data = lm90_update_device(dev);
823 int bitnr = attr->index;
824
825 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
826}
827
Guenter Roeck96512862010-10-28 20:31:43 +0200828static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
829static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200830static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200831 set_temp8, 0);
Guenter Roeck96512862010-10-28 20:31:43 +0200832static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
833 set_temp11, 0, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200834static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200835 set_temp8, 1);
Guenter Roeck96512862010-10-28 20:31:43 +0200836static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
837 set_temp11, 1, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200838static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200839 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200840static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200841 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200842static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200843 set_temphyst, 2);
844static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Guenter Roeck96512862010-10-28 20:31:43 +0200845static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
846 set_temp11, 2, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200847
848/* Individual alarm files */
849static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
850static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400851static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200852static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
853static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
854static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
855static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
856/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
858
Jean Delvare0e39e012006-09-24 21:16:40 +0200859static struct attribute *lm90_attributes[] = {
860 &sensor_dev_attr_temp1_input.dev_attr.attr,
861 &sensor_dev_attr_temp2_input.dev_attr.attr,
862 &sensor_dev_attr_temp1_min.dev_attr.attr,
863 &sensor_dev_attr_temp2_min.dev_attr.attr,
864 &sensor_dev_attr_temp1_max.dev_attr.attr,
865 &sensor_dev_attr_temp2_max.dev_attr.attr,
866 &sensor_dev_attr_temp1_crit.dev_attr.attr,
867 &sensor_dev_attr_temp2_crit.dev_attr.attr,
868 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
869 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
870
871 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
872 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400873 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200874 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
875 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
876 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
877 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
878 &dev_attr_alarms.attr,
879 NULL
880};
881
882static const struct attribute_group lm90_group = {
883 .attrs = lm90_attributes,
884};
885
Guenter Roeck69487082010-10-28 20:31:43 +0200886/*
887 * Additional attributes for devices with emergency sensors
888 */
889static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
890 set_temp8, 4);
891static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
892 set_temp8, 5);
893static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
894 NULL, 4);
895static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
896 NULL, 5);
897
898static struct attribute *lm90_emergency_attributes[] = {
899 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
900 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
901 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
902 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
903 NULL
904};
905
906static const struct attribute_group lm90_emergency_group = {
907 .attrs = lm90_emergency_attributes,
908};
909
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200910static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
911static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
912
913static struct attribute *lm90_emergency_alarm_attributes[] = {
914 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
915 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
916 NULL
917};
918
919static const struct attribute_group lm90_emergency_alarm_group = {
920 .attrs = lm90_emergency_alarm_attributes,
921};
922
923/*
924 * Additional attributes for devices with 3 temperature sensors
925 */
926static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
927static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
928 set_temp11, 3, 6);
929static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
930 set_temp11, 4, 7);
931static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
932 set_temp8, 6);
933static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
934static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
935 set_temp8, 7);
936static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
937 NULL, 7);
938
939static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
940static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
941static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
942static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
943static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
944
945static struct attribute *lm90_temp3_attributes[] = {
946 &sensor_dev_attr_temp3_input.dev_attr.attr,
947 &sensor_dev_attr_temp3_min.dev_attr.attr,
948 &sensor_dev_attr_temp3_max.dev_attr.attr,
949 &sensor_dev_attr_temp3_crit.dev_attr.attr,
950 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
951 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
952 &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
953
954 &sensor_dev_attr_temp3_fault.dev_attr.attr,
955 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
956 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
957 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
958 &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
959 NULL
960};
961
962static const struct attribute_group lm90_temp3_group = {
963 .attrs = lm90_temp3_attributes,
964};
965
Jean Delvarec3df5802005-10-26 21:39:40 +0200966/* pec used for ADM1032 only */
967static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
968 char *buf)
969{
970 struct i2c_client *client = to_i2c_client(dev);
971 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
972}
973
974static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
975 const char *buf, size_t count)
976{
977 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200978 long val;
979 int err;
980
981 err = strict_strtol(buf, 10, &val);
982 if (err < 0)
983 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200984
985 switch (val) {
986 case 0:
987 client->flags &= ~I2C_CLIENT_PEC;
988 break;
989 case 1:
990 client->flags |= I2C_CLIENT_PEC;
991 break;
992 default:
993 return -EINVAL;
994 }
995
996 return count;
997}
998
999static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/*
1002 * Real code
1003 */
1004
Jean Delvare9b0e8522008-07-16 19:30:15 +02001005/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001006static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +02001007 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Jean Delvare9b0e8522008-07-16 19:30:15 +02001009 struct i2c_adapter *adapter = new_client->adapter;
1010 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +01001011 const char *name = NULL;
1012 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +02001015 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Jean Delvare8f2fa772009-12-09 20:35:53 +01001017 /* detection and identification */
1018 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001019 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001020 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001021 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001022 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001023 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001024 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001025 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001026 return -ENODEV;
1027
1028 if ((address == 0x4C || address == 0x4D)
1029 && man_id == 0x01) { /* National Semiconductor */
1030 int reg_config2;
1031
1032 reg_config2 = i2c_smbus_read_byte_data(new_client,
1033 LM90_REG_R_CONFIG2);
1034 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001035 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Jean Delvare8f2fa772009-12-09 20:35:53 +01001037 if ((reg_config1 & 0x2A) == 0x00
1038 && (reg_config2 & 0xF8) == 0x00
1039 && reg_convrate <= 0x09) {
1040 if (address == 0x4C
1041 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1042 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001044 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1045 name = "lm99";
1046 dev_info(&adapter->dev,
1047 "Assuming LM99 chip at 0x%02x\n",
1048 address);
1049 dev_info(&adapter->dev,
1050 "If it is an LM89, instantiate it "
1051 "with the new_device sysfs "
1052 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001053 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001054 if (address == 0x4C
1055 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1056 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001059 } else
1060 if ((address == 0x4C || address == 0x4D)
1061 && man_id == 0x41) { /* Analog Devices */
1062 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1063 && (reg_config1 & 0x3F) == 0x00
1064 && reg_convrate <= 0x0A) {
1065 name = "adm1032";
1066 /* The ADM1032 supports PEC, but only if combined
1067 transactions are not used. */
1068 if (i2c_check_functionality(adapter,
1069 I2C_FUNC_SMBUS_BYTE))
1070 info->flags |= I2C_CLIENT_PEC;
1071 } else
1072 if (chip_id == 0x51 /* ADT7461 */
1073 && (reg_config1 & 0x1B) == 0x00
1074 && reg_convrate <= 0x0A) {
1075 name = "adt7461";
1076 }
1077 } else
1078 if (man_id == 0x4D) { /* Maxim */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001079 int reg_emerg, reg_emerg2, reg_status2;
1080
1081 /*
1082 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1083 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1084 * exists, both readings will reflect the same value. Otherwise,
1085 * the readings will be different.
1086 */
1087 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
1088 MAX6659_REG_R_REMOTE_EMERG)) < 0
1089 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
1090 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
1091 MAX6659_REG_R_REMOTE_EMERG)) < 0
1092 || (reg_status2 = i2c_smbus_read_byte_data(new_client,
1093 MAX6696_REG_R_STATUS2)) < 0)
1094 return -ENODEV;
1095
Jean Delvare8f2fa772009-12-09 20:35:53 +01001096 /*
1097 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1098 * register. Reading from that address will return the last
1099 * read value, which in our case is those of the man_id
1100 * register. Likewise, the config1 register seems to lack a
1101 * low nibble, so the value will be those of the previous
1102 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +02001103 * MAX6659 has a third set of upper temperature limit registers.
1104 * Those registers also return values on MAX6657 and MAX6658,
1105 * thus the only way to detect MAX6659 is by its address.
1106 * For this reason it will be mis-detected as MAX6657 if its
1107 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +01001108 */
1109 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +02001110 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001111 && (reg_config1 & 0x1F) == (man_id & 0x0F)
1112 && reg_convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +02001113 if (address == 0x4C)
1114 name = "max6657";
1115 else
1116 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001117 } else
1118 /*
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001119 * Even though MAX6695 and MAX6696 do not have a chip ID
1120 * register, reading it returns 0x01. Bit 4 of the config1
1121 * register is unused and should return zero when read. Bit 0 of
1122 * the status2 register is unused and should return zero when
1123 * read.
1124 *
1125 * MAX6695 and MAX6696 have an additional set of temperature
1126 * limit registers. We can detect those chips by checking if
1127 * one of those registers exists.
1128 */
1129 if (chip_id == 0x01
1130 && (reg_config1 & 0x10) == 0x00
1131 && (reg_status2 & 0x01) == 0x00
1132 && reg_emerg == reg_emerg2
1133 && reg_convrate <= 0x07) {
1134 name = "max6696";
1135 } else
1136 /*
Jean Delvare8f2fa772009-12-09 20:35:53 +01001137 * The chip_id register of the MAX6680 and MAX6681 holds the
1138 * revision of the chip. The lowest bit of the config1 register
1139 * is unused and should return zero when read, so should the
1140 * second to last bit of config1 (software reset).
1141 */
1142 if (chip_id == 0x01
1143 && (reg_config1 & 0x03) == 0x00
1144 && reg_convrate <= 0x07) {
1145 name = "max6680";
1146 } else
1147 /*
1148 * The chip_id register of the MAX6646/6647/6649 holds the
1149 * revision of the chip. The lowest 6 bits of the config1
1150 * register are unused and should return zero when read.
1151 */
1152 if (chip_id == 0x59
1153 && (reg_config1 & 0x3f) == 0x00
1154 && reg_convrate <= 0x07) {
1155 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001157 } else
1158 if (address == 0x4C
1159 && man_id == 0x5C) { /* Winbond/Nuvoton */
1160 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1161 && (reg_config1 & 0x2A) == 0x00
1162 && reg_convrate <= 0x08) {
1163 name = "w83l771";
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
Jean Delvare8f2fa772009-12-09 20:35:53 +01001167 if (!name) { /* identification failed */
1168 dev_dbg(&adapter->dev,
1169 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1170 "chip_id=0x%02X)\n", address, man_id, chip_id);
1171 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001173
Jean Delvare9b0e8522008-07-16 19:30:15 +02001174 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Jean Delvare9b0e8522008-07-16 19:30:15 +02001176 return 0;
1177}
1178
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001179static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1180{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001181 if (data->flags & LM90_HAVE_TEMP3)
1182 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1183 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1184 sysfs_remove_group(&client->dev.kobj,
1185 &lm90_emergency_alarm_group);
Guenter Roeck69487082010-10-28 20:31:43 +02001186 if (data->flags & LM90_HAVE_EMERGENCY)
1187 sysfs_remove_group(&client->dev.kobj,
1188 &lm90_emergency_group);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001189 if (data->flags & LM90_HAVE_OFFSET)
1190 device_remove_file(&client->dev,
1191 &sensor_dev_attr_temp2_offset.dev_attr);
1192 device_remove_file(&client->dev, &dev_attr_pec);
1193 sysfs_remove_group(&client->dev.kobj, &lm90_group);
1194}
1195
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001196static void lm90_init_client(struct i2c_client *client)
1197{
1198 u8 config;
1199 struct lm90_data *data = i2c_get_clientdata(client);
1200
1201 /*
1202 * Start the conversions.
1203 */
1204 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1205 5); /* 2 Hz */
1206 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1207 dev_warn(&client->dev, "Initialization failed!\n");
1208 return;
1209 }
1210 data->config_orig = config;
1211
1212 /* Check Temperature Range Select */
1213 if (data->kind == adt7461) {
1214 if (config & 0x04)
1215 data->flags |= LM90_FLAG_ADT7461_EXT;
1216 }
1217
1218 /*
1219 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1220 * 0.125 degree resolution) and range (0x08, extend range
1221 * to -64 degree) mode for the remote temperature sensor.
1222 */
1223 if (data->kind == max6680)
1224 config |= 0x18;
1225
1226 /*
1227 * Select external channel 0 for max6695/96
1228 */
1229 if (data->kind == max6696)
1230 config &= ~0x08;
1231
1232 config &= 0xBF; /* run */
1233 if (config != data->config_orig) /* Only write if changed */
1234 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1235}
1236
Jean Delvare9b0e8522008-07-16 19:30:15 +02001237static int lm90_probe(struct i2c_client *new_client,
1238 const struct i2c_device_id *id)
1239{
1240 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
1241 struct lm90_data *data;
1242 int err;
1243
1244 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
1245 if (!data) {
1246 err = -ENOMEM;
1247 goto exit;
1248 }
1249 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001250 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jean Delvare9b0e8522008-07-16 19:30:15 +02001252 /* Set the device type */
1253 data->kind = id->driver_data;
1254 if (data->kind == adm1032) {
1255 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1256 new_client->flags &= ~I2C_CLIENT_PEC;
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jean Delvare53de3342010-03-05 22:17:15 +01001259 /* Different devices have different alarm bits triggering the
1260 * ALERT# output */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001261 data->alert_alarms = lm90_params[data->kind].alert_alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001262
Guenter Roeck88073bb2010-10-28 20:31:43 +02001263 /* Set chip capabilities */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001264 data->flags = lm90_params[data->kind].flags;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* Initialize the LM90 chip */
1267 lm90_init_client(new_client);
1268
1269 /* Register sysfs hooks */
Guenter Roeck11e57812010-10-28 20:31:42 +02001270 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1271 if (err)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001272 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +02001273 if (new_client->flags & I2C_CLIENT_PEC) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001274 err = device_create_file(&new_client->dev, &dev_attr_pec);
1275 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +02001276 goto exit_remove_files;
1277 }
Guenter Roeck88073bb2010-10-28 20:31:43 +02001278 if (data->flags & LM90_HAVE_OFFSET) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001279 err = device_create_file(&new_client->dev,
1280 &sensor_dev_attr_temp2_offset.dev_attr);
1281 if (err)
Jean Delvare69f2f962007-09-05 14:15:37 +02001282 goto exit_remove_files;
1283 }
Guenter Roeck69487082010-10-28 20:31:43 +02001284 if (data->flags & LM90_HAVE_EMERGENCY) {
1285 err = sysfs_create_group(&new_client->dev.kobj,
1286 &lm90_emergency_group);
1287 if (err)
1288 goto exit_remove_files;
1289 }
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001290 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1291 err = sysfs_create_group(&new_client->dev.kobj,
1292 &lm90_emergency_alarm_group);
1293 if (err)
1294 goto exit_remove_files;
1295 }
1296 if (data->flags & LM90_HAVE_TEMP3) {
1297 err = sysfs_create_group(&new_client->dev.kobj,
1298 &lm90_temp3_group);
1299 if (err)
1300 goto exit_remove_files;
1301 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001302
Tony Jones1beeffe2007-08-20 13:46:20 -07001303 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1304 if (IS_ERR(data->hwmon_dev)) {
1305 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001306 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001307 }
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return 0;
1310
Jean Delvare0e39e012006-09-24 21:16:40 +02001311exit_remove_files:
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001312 lm90_remove_files(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313exit_free:
1314 kfree(data);
1315exit:
1316 return err;
1317}
1318
Jean Delvare9b0e8522008-07-16 19:30:15 +02001319static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001321 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Tony Jones1beeffe2007-08-20 13:46:20 -07001323 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001324 lm90_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001325
Jean Delvare95238362010-03-05 22:17:14 +01001326 /* Restore initial configuration */
1327 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1328 data->config_orig);
1329
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001330 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return 0;
1332}
1333
Jean Delvare53de3342010-03-05 22:17:15 +01001334static void lm90_alert(struct i2c_client *client, unsigned int flag)
1335{
1336 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001337 u8 config, alarms, alarms2 = 0;
Jean Delvare53de3342010-03-05 22:17:15 +01001338
1339 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001340
1341 if (data->kind == max6696)
1342 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1343
1344 if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
Jean Delvare53de3342010-03-05 22:17:15 +01001345 dev_info(&client->dev, "Everything OK\n");
1346 } else {
1347 if (alarms & 0x61)
1348 dev_warn(&client->dev,
1349 "temp%d out of range, please check!\n", 1);
1350 if (alarms & 0x1a)
1351 dev_warn(&client->dev,
1352 "temp%d out of range, please check!\n", 2);
1353 if (alarms & 0x04)
1354 dev_warn(&client->dev,
1355 "temp%d diode open, please check!\n", 2);
1356
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001357 if (alarms2 & 0x18)
1358 dev_warn(&client->dev,
1359 "temp%d out of range, please check!\n", 3);
1360
Jean Delvare53de3342010-03-05 22:17:15 +01001361 /* Disable ALERT# output, because these chips don't implement
1362 SMBus alert correctly; they should only hold the alert line
1363 low briefly. */
1364 if ((data->kind == adm1032 || data->kind == adt7461)
1365 && (alarms & data->alert_alarms)) {
1366 dev_dbg(&client->dev, "Disabling ALERT#\n");
1367 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1368 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1369 config | 0x80);
1370 }
1371 }
1372}
1373
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001374static struct i2c_driver lm90_driver = {
1375 .class = I2C_CLASS_HWMON,
1376 .driver = {
1377 .name = "lm90",
1378 },
1379 .probe = lm90_probe,
1380 .remove = lm90_remove,
1381 .alert = lm90_alert,
1382 .id_table = lm90_id,
1383 .detect = lm90_detect,
1384 .address_list = normal_i2c,
1385};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387static int __init sensors_lm90_init(void)
1388{
1389 return i2c_add_driver(&lm90_driver);
1390}
1391
1392static void __exit sensors_lm90_exit(void)
1393{
1394 i2c_del_driver(&lm90_driver);
1395}
1396
1397MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1398MODULE_DESCRIPTION("LM90/ADM1032 driver");
1399MODULE_LICENSE("GPL");
1400
1401module_init(sensors_lm90_init);
1402module_exit(sensors_lm90_exit);