blob: 812781c655a7a9e952653909812472807580bb6e [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.
Jean Delvarec4f99a22010-10-28 20:31:44 +020091 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657,
92 * MAX6658 and W83L771 have address 0x4c.
Ben Hutchings271dabf2008-10-17 17:51:11 +020093 * 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
Guenter Roeck0c01b642010-10-28 20:31:44 +0200154#define LM90_DEF_CONVRATE_RVAL 6 /* Def conversion rate register value */
155#define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
Nate Case23b2d472008-10-17 17:51:10 +0200158 * Device flags
159 */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200160#define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
161/* Device features */
162#define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
163#define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
164#define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
Guenter Roeck69487082010-10-28 20:31:43 +0200165#define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200166#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
167#define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
Guenter Roeck11793242010-10-28 20:31:44 +0200168#define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
Nate Case23b2d472008-10-17 17:51:10 +0200169
170/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * Driver data (common to all clients)
172 */
173
Jean Delvare9b0e8522008-07-16 19:30:15 +0200174static const struct i2c_device_id lm90_id[] = {
175 { "adm1032", adm1032 },
176 { "adt7461", adt7461 },
177 { "lm90", lm90 },
178 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100179 { "lm89", lm86 },
180 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200181 { "max6646", max6646 },
182 { "max6647", max6646 },
183 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200184 { "max6657", max6657 },
185 { "max6658", max6657 },
Guenter Roeck13c84952010-10-28 20:31:43 +0200186 { "max6659", max6659 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200187 { "max6680", max6680 },
188 { "max6681", max6680 },
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200189 { "max6695", max6696 },
190 { "max6696", max6696 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100191 { "w83l771", w83l771 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200192 { }
193};
194MODULE_DEVICE_TABLE(i2c, lm90_id);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200197 * chip type specific parameters
198 */
199struct lm90_params {
200 u32 flags; /* Capabilities */
201 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
202 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200203 u8 max_convrate; /* Maximum conversion rate register value */
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200204};
205
206static const struct lm90_params lm90_params[] = {
207 [adm1032] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200208 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
209 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200210 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200211 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200212 },
213 [adt7461] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200214 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
215 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200216 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200217 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200218 },
219 [lm86] = {
220 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
221 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200222 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200223 },
224 [lm90] = {
225 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
226 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200227 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200228 },
229 [lm99] = {
230 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
231 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200232 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200233 },
234 [max6646] = {
235 .flags = LM90_HAVE_LOCAL_EXT,
236 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200237 .max_convrate = 6,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200238 },
239 [max6657] = {
240 .flags = LM90_HAVE_LOCAL_EXT,
241 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200242 .max_convrate = 8,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200243 },
244 [max6659] = {
245 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY,
246 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200247 .max_convrate = 8,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200248 },
249 [max6680] = {
250 .flags = LM90_HAVE_OFFSET,
251 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200252 .max_convrate = 7,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200253 },
254 [max6696] = {
255 .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY
256 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
257 .alert_alarms = 0x187c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200258 .max_convrate = 6,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200259 },
260 [w83l771] = {
261 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
262 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200263 .max_convrate = 8,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200264 },
265};
266
267/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * Client data (each client gets its own)
269 */
270
271struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700272 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100273 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 char valid; /* zero until following fields are valid */
275 unsigned long last_updated; /* in jiffies */
276 int kind;
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200277 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Guenter Roeck0c01b642010-10-28 20:31:44 +0200279 int update_interval; /* in milliseconds */
280
Jean Delvare95238362010-03-05 22:17:14 +0100281 u8 config_orig; /* Original configuration register value */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200282 u8 convrate_orig; /* Original conversion rate register value */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200283 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
284 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200285 u8 max_convrate; /* Maximum conversion rate */
Jean Delvare95238362010-03-05 22:17:14 +0100286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* registers values */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200288 s8 temp8[8]; /* 0: local low limit
Jean Delvaref65e1702008-10-17 17:51:09 +0200289 1: local high limit
290 2: local critical limit
Guenter Roeck69487082010-10-28 20:31:43 +0200291 3: remote critical limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200292 4: local emergency limit (max6659 and max6695/96)
293 5: remote emergency limit (max6659 and max6695/96)
294 6: remote 2 critical limit (max6695/96 only)
295 7: remote 2 emergency limit (max6695/96 only) */
296 s16 temp11[8]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200297 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200298 2: remote high limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200299 3: remote offset (except max6646, max6657/58/59,
300 and max6695/96)
301 4: local input
302 5: remote 2 input (max6695/96 only)
303 6: remote 2 low limit (max6695/96 only)
304 7: remote 2 high limit (ma6695/96 only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 u8 temp_hyst;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200306 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
309/*
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200310 * Support functions
311 */
312
313/*
314 * The ADM1032 supports PEC but not on write byte transactions, so we need
315 * to explicitly ask for a transaction without PEC.
316 */
317static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
318{
319 return i2c_smbus_xfer(client->adapter, client->addr,
320 client->flags & ~I2C_CLIENT_PEC,
321 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
322}
323
324/*
325 * It is assumed that client->update_lock is held (unless we are in
326 * detection or initialization steps). This matters when PEC is enabled,
327 * because we don't want the address pointer to change between the write
328 * byte and the read byte transactions.
329 */
330static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
331{
332 int err;
333
334 if (client->flags & I2C_CLIENT_PEC) {
335 err = adm1032_write_byte(client, reg);
336 if (err >= 0)
337 err = i2c_smbus_read_byte(client);
338 } else
339 err = i2c_smbus_read_byte_data(client, reg);
340
341 if (err < 0) {
342 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
343 reg, err);
344 return err;
345 }
346 *value = err;
347
348 return 0;
349}
350
351static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
352{
353 int err;
354 u8 oldh, newh, l;
355
356 /*
357 * There is a trick here. We have to read two registers to have the
358 * sensor temperature, but we have to beware a conversion could occur
359 * inbetween the readings. The datasheet says we should either use
360 * the one-shot conversion register, which we don't want to do
361 * (disables hardware monitoring) or monitor the busy bit, which is
362 * impossible (we can't read the values and monitor that bit at the
363 * exact same time). So the solution used here is to read the high
364 * byte once, then the low byte, then the high byte again. If the new
365 * high byte matches the old one, then we have a valid reading. Else
366 * we have to read the low byte again, and now we believe we have a
367 * correct reading.
368 */
369 if ((err = lm90_read_reg(client, regh, &oldh))
370 || (err = lm90_read_reg(client, regl, &l))
371 || (err = lm90_read_reg(client, regh, &newh)))
372 return err;
373 if (oldh != newh) {
374 err = lm90_read_reg(client, regl, &l);
375 if (err)
376 return err;
377 }
378 *value = (newh << 8) | l;
379
380 return 0;
381}
382
383/*
384 * client->update_lock must be held when calling this function (unless we are
385 * in detection or initialization steps), and while a remote channel other
386 * than channel 0 is selected. Also, calling code must make sure to re-select
387 * external channel 0 before releasing the lock. This is necessary because
388 * various registers have different meanings as a result of selecting a
389 * non-default remote channel.
390 */
391static inline void lm90_select_remote_channel(struct i2c_client *client,
392 struct lm90_data *data,
393 int channel)
394{
395 u8 config;
396
397 if (data->kind == max6696) {
398 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
399 config &= ~0x08;
400 if (channel)
401 config |= 0x08;
402 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
403 config);
404 }
405}
406
Guenter Roeck0c01b642010-10-28 20:31:44 +0200407/*
408 * Set conversion rate.
409 * client->update_lock must be held when calling this function (unless we are
410 * in detection or initialization steps).
411 */
412static void lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
413 unsigned int interval)
414{
415 int i;
416 unsigned int update_interval;
417
418 /* Shift calculations to avoid rounding errors */
419 interval <<= 6;
420
421 /* find the nearest update rate */
422 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
423 i < data->max_convrate; i++, update_interval >>= 1)
424 if (interval >= update_interval * 3 / 4)
425 break;
426
427 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i);
428 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
429}
430
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200431static struct lm90_data *lm90_update_device(struct device *dev)
432{
433 struct i2c_client *client = to_i2c_client(dev);
434 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck0c01b642010-10-28 20:31:44 +0200435 unsigned long next_update;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200436
437 mutex_lock(&data->update_lock);
438
Guenter Roeck0c01b642010-10-28 20:31:44 +0200439 next_update = data->last_updated
440 + msecs_to_jiffies(data->update_interval) + 1;
441 if (time_after(jiffies, next_update) || !data->valid) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200442 u8 h, l;
443 u8 alarms;
444
445 dev_dbg(&client->dev, "Updating lm90 data.\n");
446 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
447 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
448 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
449 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
450 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
451
452 if (data->flags & LM90_HAVE_LOCAL_EXT) {
453 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
454 MAX6657_REG_R_LOCAL_TEMPL,
455 &data->temp11[4]);
456 } else {
457 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
458 &h) == 0)
459 data->temp11[4] = h << 8;
460 }
461 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
462 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
463
464 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
465 data->temp11[1] = h << 8;
466 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
467 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
468 &l) == 0)
469 data->temp11[1] |= l;
470 }
471 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
472 data->temp11[2] = h << 8;
473 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
474 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
475 &l) == 0)
476 data->temp11[2] |= l;
477 }
478
479 if (data->flags & LM90_HAVE_OFFSET) {
480 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
481 &h) == 0
482 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
483 &l) == 0)
484 data->temp11[3] = (h << 8) | l;
485 }
486 if (data->flags & LM90_HAVE_EMERGENCY) {
487 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
488 &data->temp8[4]);
489 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
490 &data->temp8[5]);
491 }
492 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
493 data->alarms = alarms; /* save as 16 bit value */
494
495 if (data->kind == max6696) {
496 lm90_select_remote_channel(client, data, 1);
497 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
498 &data->temp8[6]);
499 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
500 &data->temp8[7]);
501 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
502 LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
503 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
504 data->temp11[6] = h << 8;
505 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
506 data->temp11[7] = h << 8;
507 lm90_select_remote_channel(client, data, 0);
508
509 if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
510 &alarms))
511 data->alarms |= alarms << 8;
512 }
513
514 /* Re-enable ALERT# output if it was originally enabled and
515 * relevant alarms are all clear */
516 if ((data->config_orig & 0x80) == 0
517 && (data->alarms & data->alert_alarms) == 0) {
518 u8 config;
519
520 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
521 if (config & 0x80) {
522 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
523 i2c_smbus_write_byte_data(client,
524 LM90_REG_W_CONFIG1,
525 config & ~0x80);
526 }
527 }
528
529 data->last_updated = jiffies;
530 data->valid = 1;
531 }
532
533 mutex_unlock(&data->update_lock);
534
535 return data;
536}
537
538/*
Nate Casecea50fe2008-10-17 17:51:10 +0200539 * Conversions
540 * For local temperatures and limits, critical limits and the hysteresis
541 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
542 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200543 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
544 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200545 */
546
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200547static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200548{
549 return val * 1000;
550}
551
Ben Hutchings271dabf2008-10-17 17:51:11 +0200552static inline int temp_from_u8(u8 val)
553{
554 return val * 1000;
555}
556
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200557static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200558{
559 return val / 32 * 125;
560}
561
Ben Hutchings271dabf2008-10-17 17:51:11 +0200562static inline int temp_from_u16(u16 val)
563{
564 return val / 32 * 125;
565}
566
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200567static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200568{
569 if (val <= -128000)
570 return -128;
571 if (val >= 127000)
572 return 127;
573 if (val < 0)
574 return (val - 500) / 1000;
575 return (val + 500) / 1000;
576}
577
Ben Hutchings271dabf2008-10-17 17:51:11 +0200578static u8 temp_to_u8(long val)
579{
580 if (val <= 0)
581 return 0;
582 if (val >= 255000)
583 return 255;
584 return (val + 500) / 1000;
585}
586
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200587static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200588{
589 if (val <= -128000)
590 return 0x8000;
591 if (val >= 127875)
592 return 0x7FE0;
593 if (val < 0)
594 return (val - 62) / 125 * 32;
595 return (val + 62) / 125 * 32;
596}
597
598static u8 hyst_to_reg(long val)
599{
600 if (val <= 0)
601 return 0;
602 if (val >= 30500)
603 return 31;
604 return (val + 500) / 1000;
605}
606
607/*
Nate Case23b2d472008-10-17 17:51:10 +0200608 * ADT7461 in compatibility mode is almost identical to LM90 except that
609 * attempts to write values that are outside the range 0 < temp < 127 are
610 * treated as the boundary value.
611 *
612 * ADT7461 in "extended mode" operation uses unsigned integers offset by
613 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200614 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200615static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200616{
Nate Case23b2d472008-10-17 17:51:10 +0200617 if (data->flags & LM90_FLAG_ADT7461_EXT)
618 return (val - 64) * 1000;
619 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200620 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200621}
622
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200623static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200624{
Nate Case23b2d472008-10-17 17:51:10 +0200625 if (data->flags & LM90_FLAG_ADT7461_EXT)
626 return (val - 0x4000) / 64 * 250;
627 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200628 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200629}
630
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200631static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200632{
633 if (data->flags & LM90_FLAG_ADT7461_EXT) {
634 if (val <= -64000)
635 return 0;
636 if (val >= 191000)
637 return 0xFF;
638 return (val + 500 + 64000) / 1000;
639 } else {
640 if (val <= 0)
641 return 0;
642 if (val >= 127000)
643 return 127;
644 return (val + 500) / 1000;
645 }
646}
647
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200648static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200649{
650 if (data->flags & LM90_FLAG_ADT7461_EXT) {
651 if (val <= -64000)
652 return 0;
653 if (val >= 191750)
654 return 0xFFC0;
655 return (val + 64000 + 125) / 250 * 64;
656 } else {
657 if (val <= 0)
658 return 0;
659 if (val >= 127750)
660 return 0x7FC0;
661 return (val + 125) / 250 * 64;
662 }
Nate Casecea50fe2008-10-17 17:51:10 +0200663}
664
665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * Sysfs stuff
667 */
668
Jean Delvare30d73942005-06-05 21:27:28 +0200669static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
670 char *buf)
671{
672 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
673 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200674 int temp;
675
676 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200677 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200678 else if (data->kind == max6646)
679 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200680 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200681 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200682
Jean Delvare97ae60b2008-10-26 17:04:39 +0100683 /* +16 degrees offset for temp2 for the LM99 */
684 if (data->kind == lm99 && attr->index == 3)
685 temp += 16000;
686
Nate Case23b2d472008-10-17 17:51:10 +0200687 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Jean Delvare30d73942005-06-05 21:27:28 +0200690static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
691 const char *buf, size_t count)
692{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200693 static const u8 reg[8] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200694 LM90_REG_W_LOCAL_LOW,
695 LM90_REG_W_LOCAL_HIGH,
696 LM90_REG_W_LOCAL_CRIT,
697 LM90_REG_W_REMOTE_CRIT,
Guenter Roeck69487082010-10-28 20:31:43 +0200698 MAX6659_REG_W_LOCAL_EMERG,
699 MAX6659_REG_W_REMOTE_EMERG,
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200700 LM90_REG_W_REMOTE_CRIT,
701 MAX6659_REG_W_REMOTE_EMERG,
Jean Delvare30d73942005-06-05 21:27:28 +0200702 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jean Delvare30d73942005-06-05 21:27:28 +0200704 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
705 struct i2c_client *client = to_i2c_client(dev);
706 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200707 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200708 long val;
709 int err;
710
711 err = strict_strtol(buf, 10, &val);
712 if (err < 0)
713 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Jean Delvare97ae60b2008-10-26 17:04:39 +0100715 /* +16 degrees offset for temp2 for the LM99 */
716 if (data->kind == lm99 && attr->index == 3)
717 val -= 16000;
718
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100719 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200720 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200721 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200722 else if (data->kind == max6646)
723 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200724 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200725 data->temp8[nr] = temp_to_s8(val);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200726
727 lm90_select_remote_channel(client, data, nr >= 6);
Jean Delvaref65e1702008-10-17 17:51:09 +0200728 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200729 lm90_select_remote_channel(client, data, 0);
730
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100731 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200732 return count;
733}
734
735static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
736 char *buf)
737{
Guenter Roeck96512862010-10-28 20:31:43 +0200738 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200739 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200740 int temp;
741
742 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200743 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200744 else if (data->kind == max6646)
745 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200746 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200747 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200748
Jean Delvare97ae60b2008-10-26 17:04:39 +0100749 /* +16 degrees offset for temp2 for the LM99 */
750 if (data->kind == lm99 && attr->index <= 2)
751 temp += 16000;
752
Nate Case23b2d472008-10-17 17:51:10 +0200753 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200754}
755
756static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
757 const char *buf, size_t count)
758{
Guenter Roeck96512862010-10-28 20:31:43 +0200759 struct {
760 u8 high;
761 u8 low;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200762 int channel;
763 } reg[5] = {
764 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
765 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
766 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
767 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
768 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
Jean Delvare30d73942005-06-05 21:27:28 +0200769 };
770
Guenter Roeck96512862010-10-28 20:31:43 +0200771 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200772 struct i2c_client *client = to_i2c_client(dev);
773 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck96512862010-10-28 20:31:43 +0200774 int nr = attr->nr;
775 int index = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200776 long val;
777 int err;
778
779 err = strict_strtol(buf, 10, &val);
780 if (err < 0)
781 return err;
Jean Delvare30d73942005-06-05 21:27:28 +0200782
Jean Delvare97ae60b2008-10-26 17:04:39 +0100783 /* +16 degrees offset for temp2 for the LM99 */
Guenter Roeck96512862010-10-28 20:31:43 +0200784 if (data->kind == lm99 && index <= 2)
Jean Delvare97ae60b2008-10-26 17:04:39 +0100785 val -= 16000;
786
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100787 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200788 if (data->kind == adt7461)
Guenter Roeck96512862010-10-28 20:31:43 +0200789 data->temp11[index] = temp_to_u16_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200790 else if (data->kind == max6646)
Guenter Roeck96512862010-10-28 20:31:43 +0200791 data->temp11[index] = temp_to_u8(val) << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +0200792 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200793 data->temp11[index] = temp_to_s16(val);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200794 else
Guenter Roeck96512862010-10-28 20:31:43 +0200795 data->temp11[index] = temp_to_s8(val) << 8;
Jean Delvare5f502a82008-10-17 17:51:09 +0200796
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200797 lm90_select_remote_channel(client, data, reg[nr].channel);
Guenter Roeck96512862010-10-28 20:31:43 +0200798 i2c_smbus_write_byte_data(client, reg[nr].high,
799 data->temp11[index] >> 8);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200800 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200801 i2c_smbus_write_byte_data(client, reg[nr].low,
802 data->temp11[index] & 0xff);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200803 lm90_select_remote_channel(client, data, 0);
804
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100805 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200806 return count;
807}
808
Guenter Roeck11e57812010-10-28 20:31:42 +0200809static ssize_t show_temphyst(struct device *dev,
810 struct device_attribute *devattr,
Jean Delvare30d73942005-06-05 21:27:28 +0200811 char *buf)
812{
813 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
814 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200815 int temp;
816
817 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200818 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100819 else if (data->kind == max6646)
820 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200821 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200822 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200823
Jean Delvare97ae60b2008-10-26 17:04:39 +0100824 /* +16 degrees offset for temp2 for the LM99 */
825 if (data->kind == lm99 && attr->index == 3)
826 temp += 16000;
827
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200828 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200829}
830
831static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
832 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct i2c_client *client = to_i2c_client(dev);
835 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck11e57812010-10-28 20:31:42 +0200836 long val;
837 int err;
Jean Delvareec38fa22008-10-26 17:04:39 +0100838 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Guenter Roeck11e57812010-10-28 20:31:42 +0200840 err = strict_strtol(buf, 10, &val);
841 if (err < 0)
842 return err;
843
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100844 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100845 if (data->kind == adt7461)
846 temp = temp_from_u8_adt7461(data, data->temp8[2]);
847 else if (data->kind == max6646)
848 temp = temp_from_u8(data->temp8[2]);
849 else
850 temp = temp_from_s8(data->temp8[2]);
851
852 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100854 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100855 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return count;
857}
858
Jean Delvare30d73942005-06-05 21:27:28 +0200859static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
860 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct lm90_data *data = lm90_update_device(dev);
863 return sprintf(buf, "%d\n", data->alarms);
864}
865
Jean Delvare2d457712006-09-24 20:52:15 +0200866static ssize_t show_alarm(struct device *dev, struct device_attribute
867 *devattr, char *buf)
868{
869 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
870 struct lm90_data *data = lm90_update_device(dev);
871 int bitnr = attr->index;
872
873 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
874}
875
Guenter Roeck0c01b642010-10-28 20:31:44 +0200876static ssize_t show_update_interval(struct device *dev,
877 struct device_attribute *attr, char *buf)
878{
879 struct lm90_data *data = dev_get_drvdata(dev);
880
881 return sprintf(buf, "%u\n", data->update_interval);
882}
883
884static ssize_t set_update_interval(struct device *dev,
885 struct device_attribute *attr,
886 const char *buf, size_t count)
887{
888 struct i2c_client *client = to_i2c_client(dev);
889 struct lm90_data *data = i2c_get_clientdata(client);
890 unsigned long val;
891 int err;
892
893 err = strict_strtoul(buf, 10, &val);
894 if (err)
895 return err;
896
897 mutex_lock(&data->update_lock);
898 lm90_set_convrate(client, data, val);
899 mutex_unlock(&data->update_lock);
900
901 return count;
902}
903
Guenter Roeck96512862010-10-28 20:31:43 +0200904static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
905static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200906static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200907 set_temp8, 0);
Guenter Roeck96512862010-10-28 20:31:43 +0200908static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
909 set_temp11, 0, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200910static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200911 set_temp8, 1);
Guenter Roeck96512862010-10-28 20:31:43 +0200912static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
913 set_temp11, 1, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200914static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200915 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200916static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200917 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200918static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200919 set_temphyst, 2);
920static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Guenter Roeck96512862010-10-28 20:31:43 +0200921static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
922 set_temp11, 2, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200923
924/* Individual alarm files */
925static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
926static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400927static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200928static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
929static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
930static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
931static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
932/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
934
Guenter Roeck0c01b642010-10-28 20:31:44 +0200935static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
936 set_update_interval);
937
Jean Delvare0e39e012006-09-24 21:16:40 +0200938static struct attribute *lm90_attributes[] = {
939 &sensor_dev_attr_temp1_input.dev_attr.attr,
940 &sensor_dev_attr_temp2_input.dev_attr.attr,
941 &sensor_dev_attr_temp1_min.dev_attr.attr,
942 &sensor_dev_attr_temp2_min.dev_attr.attr,
943 &sensor_dev_attr_temp1_max.dev_attr.attr,
944 &sensor_dev_attr_temp2_max.dev_attr.attr,
945 &sensor_dev_attr_temp1_crit.dev_attr.attr,
946 &sensor_dev_attr_temp2_crit.dev_attr.attr,
947 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
948 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
949
950 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
951 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400952 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200953 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
954 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
955 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
956 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
957 &dev_attr_alarms.attr,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200958 &dev_attr_update_interval.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200959 NULL
960};
961
962static const struct attribute_group lm90_group = {
963 .attrs = lm90_attributes,
964};
965
Guenter Roeck69487082010-10-28 20:31:43 +0200966/*
967 * Additional attributes for devices with emergency sensors
968 */
969static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
970 set_temp8, 4);
971static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
972 set_temp8, 5);
973static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
974 NULL, 4);
975static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
976 NULL, 5);
977
978static struct attribute *lm90_emergency_attributes[] = {
979 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
980 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
981 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
982 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
983 NULL
984};
985
986static const struct attribute_group lm90_emergency_group = {
987 .attrs = lm90_emergency_attributes,
988};
989
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200990static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
991static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
992
993static struct attribute *lm90_emergency_alarm_attributes[] = {
994 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
995 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
996 NULL
997};
998
999static const struct attribute_group lm90_emergency_alarm_group = {
1000 .attrs = lm90_emergency_alarm_attributes,
1001};
1002
1003/*
1004 * Additional attributes for devices with 3 temperature sensors
1005 */
1006static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
1007static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
1008 set_temp11, 3, 6);
1009static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
1010 set_temp11, 4, 7);
1011static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
1012 set_temp8, 6);
1013static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
1014static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
1015 set_temp8, 7);
1016static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
1017 NULL, 7);
1018
1019static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
1020static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
1021static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
1022static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
1023static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
1024
1025static struct attribute *lm90_temp3_attributes[] = {
1026 &sensor_dev_attr_temp3_input.dev_attr.attr,
1027 &sensor_dev_attr_temp3_min.dev_attr.attr,
1028 &sensor_dev_attr_temp3_max.dev_attr.attr,
1029 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1030 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1031 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
1032 &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
1033
1034 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1035 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
1036 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
1037 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
1038 &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
1039 NULL
1040};
1041
1042static const struct attribute_group lm90_temp3_group = {
1043 .attrs = lm90_temp3_attributes,
1044};
1045
Jean Delvarec3df5802005-10-26 21:39:40 +02001046/* pec used for ADM1032 only */
1047static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
1048 char *buf)
1049{
1050 struct i2c_client *client = to_i2c_client(dev);
1051 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
1052}
1053
1054static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
1055 const char *buf, size_t count)
1056{
1057 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +02001058 long val;
1059 int err;
1060
1061 err = strict_strtol(buf, 10, &val);
1062 if (err < 0)
1063 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +02001064
1065 switch (val) {
1066 case 0:
1067 client->flags &= ~I2C_CLIENT_PEC;
1068 break;
1069 case 1:
1070 client->flags |= I2C_CLIENT_PEC;
1071 break;
1072 default:
1073 return -EINVAL;
1074 }
1075
1076 return count;
1077}
1078
1079static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081/*
1082 * Real code
1083 */
1084
Jean Delvare9b0e8522008-07-16 19:30:15 +02001085/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001086static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +02001087 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Jean Delvare9b0e8522008-07-16 19:30:15 +02001089 struct i2c_adapter *adapter = new_client->adapter;
1090 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +01001091 const char *name = NULL;
1092 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +02001095 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Jean Delvare8f2fa772009-12-09 20:35:53 +01001097 /* detection and identification */
1098 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001099 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001100 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001101 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001102 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001103 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +01001104 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +01001105 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001106 return -ENODEV;
1107
1108 if ((address == 0x4C || address == 0x4D)
1109 && man_id == 0x01) { /* National Semiconductor */
1110 int reg_config2;
1111
1112 reg_config2 = i2c_smbus_read_byte_data(new_client,
1113 LM90_REG_R_CONFIG2);
1114 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001115 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jean Delvare8f2fa772009-12-09 20:35:53 +01001117 if ((reg_config1 & 0x2A) == 0x00
1118 && (reg_config2 & 0xF8) == 0x00
1119 && reg_convrate <= 0x09) {
1120 if (address == 0x4C
1121 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1122 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001124 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1125 name = "lm99";
1126 dev_info(&adapter->dev,
1127 "Assuming LM99 chip at 0x%02x\n",
1128 address);
1129 dev_info(&adapter->dev,
1130 "If it is an LM89, instantiate it "
1131 "with the new_device sysfs "
1132 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001133 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001134 if (address == 0x4C
1135 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1136 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001139 } else
1140 if ((address == 0x4C || address == 0x4D)
1141 && man_id == 0x41) { /* Analog Devices */
1142 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
1143 && (reg_config1 & 0x3F) == 0x00
1144 && reg_convrate <= 0x0A) {
1145 name = "adm1032";
1146 /* The ADM1032 supports PEC, but only if combined
1147 transactions are not used. */
1148 if (i2c_check_functionality(adapter,
1149 I2C_FUNC_SMBUS_BYTE))
1150 info->flags |= I2C_CLIENT_PEC;
1151 } else
1152 if (chip_id == 0x51 /* ADT7461 */
1153 && (reg_config1 & 0x1B) == 0x00
1154 && reg_convrate <= 0x0A) {
1155 name = "adt7461";
1156 }
1157 } else
1158 if (man_id == 0x4D) { /* Maxim */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001159 int reg_emerg, reg_emerg2, reg_status2;
1160
1161 /*
1162 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1163 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1164 * exists, both readings will reflect the same value. Otherwise,
1165 * the readings will be different.
1166 */
1167 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
1168 MAX6659_REG_R_REMOTE_EMERG)) < 0
1169 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
1170 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
1171 MAX6659_REG_R_REMOTE_EMERG)) < 0
1172 || (reg_status2 = i2c_smbus_read_byte_data(new_client,
1173 MAX6696_REG_R_STATUS2)) < 0)
1174 return -ENODEV;
1175
Jean Delvare8f2fa772009-12-09 20:35:53 +01001176 /*
1177 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1178 * register. Reading from that address will return the last
1179 * read value, which in our case is those of the man_id
1180 * register. Likewise, the config1 register seems to lack a
1181 * low nibble, so the value will be those of the previous
1182 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +02001183 * MAX6659 has a third set of upper temperature limit registers.
1184 * Those registers also return values on MAX6657 and MAX6658,
1185 * thus the only way to detect MAX6659 is by its address.
1186 * For this reason it will be mis-detected as MAX6657 if its
1187 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +01001188 */
1189 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +02001190 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001191 && (reg_config1 & 0x1F) == (man_id & 0x0F)
1192 && reg_convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +02001193 if (address == 0x4C)
1194 name = "max6657";
1195 else
1196 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001197 } else
1198 /*
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001199 * Even though MAX6695 and MAX6696 do not have a chip ID
1200 * register, reading it returns 0x01. Bit 4 of the config1
1201 * register is unused and should return zero when read. Bit 0 of
1202 * the status2 register is unused and should return zero when
1203 * read.
1204 *
1205 * MAX6695 and MAX6696 have an additional set of temperature
1206 * limit registers. We can detect those chips by checking if
1207 * one of those registers exists.
1208 */
1209 if (chip_id == 0x01
1210 && (reg_config1 & 0x10) == 0x00
1211 && (reg_status2 & 0x01) == 0x00
1212 && reg_emerg == reg_emerg2
1213 && reg_convrate <= 0x07) {
1214 name = "max6696";
1215 } else
1216 /*
Jean Delvare8f2fa772009-12-09 20:35:53 +01001217 * The chip_id register of the MAX6680 and MAX6681 holds the
1218 * revision of the chip. The lowest bit of the config1 register
1219 * is unused and should return zero when read, so should the
1220 * second to last bit of config1 (software reset).
1221 */
1222 if (chip_id == 0x01
1223 && (reg_config1 & 0x03) == 0x00
1224 && reg_convrate <= 0x07) {
1225 name = "max6680";
1226 } else
1227 /*
1228 * The chip_id register of the MAX6646/6647/6649 holds the
1229 * revision of the chip. The lowest 6 bits of the config1
1230 * register are unused and should return zero when read.
1231 */
1232 if (chip_id == 0x59
1233 && (reg_config1 & 0x3f) == 0x00
1234 && reg_convrate <= 0x07) {
1235 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001237 } else
1238 if (address == 0x4C
1239 && man_id == 0x5C) { /* Winbond/Nuvoton */
Jean Delvarec4f99a22010-10-28 20:31:44 +02001240 int reg_config2;
1241
1242 reg_config2 = i2c_smbus_read_byte_data(new_client,
1243 LM90_REG_R_CONFIG2);
1244 if (reg_config2 < 0)
1245 return -ENODEV;
1246
1247 if ((reg_config1 & 0x2A) == 0x00
1248 && (reg_config2 & 0xF8) == 0x00) {
1249 if (chip_id == 0x01 /* W83L771W/G */
1250 && reg_convrate <= 0x09) {
1251 name = "w83l771";
1252 } else
1253 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
1254 && reg_convrate <= 0x08) {
1255 name = "w83l771";
1256 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
Jean Delvare8f2fa772009-12-09 20:35:53 +01001260 if (!name) { /* identification failed */
1261 dev_dbg(&adapter->dev,
1262 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1263 "chip_id=0x%02X)\n", address, man_id, chip_id);
1264 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001266
Jean Delvare9b0e8522008-07-16 19:30:15 +02001267 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Jean Delvare9b0e8522008-07-16 19:30:15 +02001269 return 0;
1270}
1271
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001272static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1273{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001274 if (data->flags & LM90_HAVE_TEMP3)
1275 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1276 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1277 sysfs_remove_group(&client->dev.kobj,
1278 &lm90_emergency_alarm_group);
Guenter Roeck69487082010-10-28 20:31:43 +02001279 if (data->flags & LM90_HAVE_EMERGENCY)
1280 sysfs_remove_group(&client->dev.kobj,
1281 &lm90_emergency_group);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001282 if (data->flags & LM90_HAVE_OFFSET)
1283 device_remove_file(&client->dev,
1284 &sensor_dev_attr_temp2_offset.dev_attr);
1285 device_remove_file(&client->dev, &dev_attr_pec);
1286 sysfs_remove_group(&client->dev.kobj, &lm90_group);
1287}
1288
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001289static void lm90_init_client(struct i2c_client *client)
1290{
Guenter Roeck0c01b642010-10-28 20:31:44 +02001291 u8 config, convrate;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001292 struct lm90_data *data = i2c_get_clientdata(client);
1293
Guenter Roeck0c01b642010-10-28 20:31:44 +02001294 if (lm90_read_reg(client, LM90_REG_R_CONVRATE, &convrate) < 0) {
1295 dev_warn(&client->dev, "Failed to read convrate register!\n");
1296 convrate = LM90_DEF_CONVRATE_RVAL;
1297 }
1298 data->convrate_orig = convrate;
1299
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001300 /*
1301 * Start the conversions.
1302 */
Guenter Roeck0c01b642010-10-28 20:31:44 +02001303 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001304 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1305 dev_warn(&client->dev, "Initialization failed!\n");
1306 return;
1307 }
1308 data->config_orig = config;
1309
1310 /* Check Temperature Range Select */
1311 if (data->kind == adt7461) {
1312 if (config & 0x04)
1313 data->flags |= LM90_FLAG_ADT7461_EXT;
1314 }
1315
1316 /*
1317 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1318 * 0.125 degree resolution) and range (0x08, extend range
1319 * to -64 degree) mode for the remote temperature sensor.
1320 */
1321 if (data->kind == max6680)
1322 config |= 0x18;
1323
1324 /*
1325 * Select external channel 0 for max6695/96
1326 */
1327 if (data->kind == max6696)
1328 config &= ~0x08;
1329
1330 config &= 0xBF; /* run */
1331 if (config != data->config_orig) /* Only write if changed */
1332 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
1333}
1334
Jean Delvare9b0e8522008-07-16 19:30:15 +02001335static int lm90_probe(struct i2c_client *new_client,
1336 const struct i2c_device_id *id)
1337{
1338 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
1339 struct lm90_data *data;
1340 int err;
1341
1342 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
1343 if (!data) {
1344 err = -ENOMEM;
1345 goto exit;
1346 }
1347 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001348 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jean Delvare9b0e8522008-07-16 19:30:15 +02001350 /* Set the device type */
1351 data->kind = id->driver_data;
1352 if (data->kind == adm1032) {
1353 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1354 new_client->flags &= ~I2C_CLIENT_PEC;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jean Delvare53de3342010-03-05 22:17:15 +01001357 /* Different devices have different alarm bits triggering the
1358 * ALERT# output */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001359 data->alert_alarms = lm90_params[data->kind].alert_alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001360
Guenter Roeck88073bb2010-10-28 20:31:43 +02001361 /* Set chip capabilities */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001362 data->flags = lm90_params[data->kind].flags;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001363
Guenter Roeck0c01b642010-10-28 20:31:44 +02001364 /* Set maximum conversion rate */
1365 data->max_convrate = lm90_params[data->kind].max_convrate;
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Initialize the LM90 chip */
1368 lm90_init_client(new_client);
1369
1370 /* Register sysfs hooks */
Guenter Roeck11e57812010-10-28 20:31:42 +02001371 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1372 if (err)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001373 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +02001374 if (new_client->flags & I2C_CLIENT_PEC) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001375 err = device_create_file(&new_client->dev, &dev_attr_pec);
1376 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +02001377 goto exit_remove_files;
1378 }
Guenter Roeck88073bb2010-10-28 20:31:43 +02001379 if (data->flags & LM90_HAVE_OFFSET) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001380 err = device_create_file(&new_client->dev,
1381 &sensor_dev_attr_temp2_offset.dev_attr);
1382 if (err)
Jean Delvare69f2f962007-09-05 14:15:37 +02001383 goto exit_remove_files;
1384 }
Guenter Roeck69487082010-10-28 20:31:43 +02001385 if (data->flags & LM90_HAVE_EMERGENCY) {
1386 err = sysfs_create_group(&new_client->dev.kobj,
1387 &lm90_emergency_group);
1388 if (err)
1389 goto exit_remove_files;
1390 }
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001391 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1392 err = sysfs_create_group(&new_client->dev.kobj,
1393 &lm90_emergency_alarm_group);
1394 if (err)
1395 goto exit_remove_files;
1396 }
1397 if (data->flags & LM90_HAVE_TEMP3) {
1398 err = sysfs_create_group(&new_client->dev.kobj,
1399 &lm90_temp3_group);
1400 if (err)
1401 goto exit_remove_files;
1402 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001403
Tony Jones1beeffe2007-08-20 13:46:20 -07001404 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1405 if (IS_ERR(data->hwmon_dev)) {
1406 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001407 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001408 }
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return 0;
1411
Jean Delvare0e39e012006-09-24 21:16:40 +02001412exit_remove_files:
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001413 lm90_remove_files(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414exit_free:
1415 kfree(data);
1416exit:
1417 return err;
1418}
1419
Jean Delvare9b0e8522008-07-16 19:30:15 +02001420static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001422 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Tony Jones1beeffe2007-08-20 13:46:20 -07001424 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001425 lm90_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001426
Jean Delvare95238362010-03-05 22:17:14 +01001427 /* Restore initial configuration */
Guenter Roeck0c01b642010-10-28 20:31:44 +02001428 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1429 data->convrate_orig);
Jean Delvare95238362010-03-05 22:17:14 +01001430 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1431 data->config_orig);
1432
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001433 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
1435}
1436
Jean Delvare53de3342010-03-05 22:17:15 +01001437static void lm90_alert(struct i2c_client *client, unsigned int flag)
1438{
1439 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001440 u8 config, alarms, alarms2 = 0;
Jean Delvare53de3342010-03-05 22:17:15 +01001441
1442 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001443
1444 if (data->kind == max6696)
1445 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1446
1447 if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
Jean Delvare53de3342010-03-05 22:17:15 +01001448 dev_info(&client->dev, "Everything OK\n");
1449 } else {
1450 if (alarms & 0x61)
1451 dev_warn(&client->dev,
1452 "temp%d out of range, please check!\n", 1);
1453 if (alarms & 0x1a)
1454 dev_warn(&client->dev,
1455 "temp%d out of range, please check!\n", 2);
1456 if (alarms & 0x04)
1457 dev_warn(&client->dev,
1458 "temp%d diode open, please check!\n", 2);
1459
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001460 if (alarms2 & 0x18)
1461 dev_warn(&client->dev,
1462 "temp%d out of range, please check!\n", 3);
1463
Jean Delvare53de3342010-03-05 22:17:15 +01001464 /* Disable ALERT# output, because these chips don't implement
1465 SMBus alert correctly; they should only hold the alert line
1466 low briefly. */
Guenter Roeck11793242010-10-28 20:31:44 +02001467 if ((data->flags & LM90_HAVE_BROKEN_ALERT)
Jean Delvare53de3342010-03-05 22:17:15 +01001468 && (alarms & data->alert_alarms)) {
1469 dev_dbg(&client->dev, "Disabling ALERT#\n");
1470 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1471 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1472 config | 0x80);
1473 }
1474 }
1475}
1476
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001477static struct i2c_driver lm90_driver = {
1478 .class = I2C_CLASS_HWMON,
1479 .driver = {
1480 .name = "lm90",
1481 },
1482 .probe = lm90_probe,
1483 .remove = lm90_remove,
1484 .alert = lm90_alert,
1485 .id_table = lm90_id,
1486 .detect = lm90_detect,
1487 .address_list = normal_i2c,
1488};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490static int __init sensors_lm90_init(void)
1491{
1492 return i2c_add_driver(&lm90_driver);
1493}
1494
1495static void __exit sensors_lm90_exit(void)
1496{
1497 i2c_del_driver(&lm90_driver);
1498}
1499
1500MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1501MODULE_DESCRIPTION("LM90/ADM1032 driver");
1502MODULE_LICENSE("GPL");
1503
1504module_init(sensors_lm90_init);
1505module_exit(sensors_lm90_exit);