blob: de544817d673212b04b1a90a9464019bb4f3f9fa [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 *
Nate Case23b2d472008-10-17 17:51:10 +020047 * This driver also supports the ADT7461 chip from Analog Devices.
48 * It's supported in both compatibility and extended mode. It is mostly
49 * compatible with LM90 except for a data format difference for the
50 * temperature value registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 *
52 * Since the LM90 was the first chipset supported by this driver, most
53 * comments will refer to this chipset, but are actually general and
54 * concern all supported chipsets, unless mentioned otherwise.
55 *
56 * This program is free software; you can redistribute it and/or modify
57 * it under the terms of the GNU General Public License as published by
58 * the Free Software Foundation; either version 2 of the License, or
59 * (at your option) any later version.
60 *
61 * This program is distributed in the hope that it will be useful,
62 * but WITHOUT ANY WARRANTY; without even the implied warranty of
63 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 * GNU General Public License for more details.
65 *
66 * You should have received a copy of the GNU General Public License
67 * along with this program; if not, write to the Free Software
68 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
69 */
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/module.h>
72#include <linux/init.h>
73#include <linux/slab.h>
74#include <linux/jiffies.h>
75#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020076#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040077#include <linux/hwmon.h>
78#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010079#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020080#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * Addresses to scan
84 * Address is fully defined internally and cannot be changed except for
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040085 * MAX6659, MAX6680 and MAX6681.
Ben Hutchings271dabf2008-10-17 17:51:11 +020086 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
87 * and MAX6658 have address 0x4c.
88 * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
89 * MAX6647 has address 0x4e.
Guenter Roeck13c84952010-10-28 20:31:43 +020090 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040091 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
92 * 0x4c, 0x4d or 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050095static const unsigned short normal_i2c[] = {
96 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Guenter Roeck13c84952010-10-28 20:31:43 +020098enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
99 max6646, w83l771 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * The LM90 registers
103 */
104
105#define LM90_REG_R_MAN_ID 0xFE
106#define LM90_REG_R_CHIP_ID 0xFF
107#define LM90_REG_R_CONFIG1 0x03
108#define LM90_REG_W_CONFIG1 0x09
109#define LM90_REG_R_CONFIG2 0xBF
110#define LM90_REG_W_CONFIG2 0xBF
111#define LM90_REG_R_CONVRATE 0x04
112#define LM90_REG_W_CONVRATE 0x0A
113#define LM90_REG_R_STATUS 0x02
114#define LM90_REG_R_LOCAL_TEMP 0x00
115#define LM90_REG_R_LOCAL_HIGH 0x05
116#define LM90_REG_W_LOCAL_HIGH 0x0B
117#define LM90_REG_R_LOCAL_LOW 0x06
118#define LM90_REG_W_LOCAL_LOW 0x0C
119#define LM90_REG_R_LOCAL_CRIT 0x20
120#define LM90_REG_W_LOCAL_CRIT 0x20
121#define LM90_REG_R_REMOTE_TEMPH 0x01
122#define LM90_REG_R_REMOTE_TEMPL 0x10
123#define LM90_REG_R_REMOTE_OFFSH 0x11
124#define LM90_REG_W_REMOTE_OFFSH 0x11
125#define LM90_REG_R_REMOTE_OFFSL 0x12
126#define LM90_REG_W_REMOTE_OFFSL 0x12
127#define LM90_REG_R_REMOTE_HIGHH 0x07
128#define LM90_REG_W_REMOTE_HIGHH 0x0D
129#define LM90_REG_R_REMOTE_HIGHL 0x13
130#define LM90_REG_W_REMOTE_HIGHL 0x13
131#define LM90_REG_R_REMOTE_LOWH 0x08
132#define LM90_REG_W_REMOTE_LOWH 0x0E
133#define LM90_REG_R_REMOTE_LOWL 0x14
134#define LM90_REG_W_REMOTE_LOWL 0x14
135#define LM90_REG_R_REMOTE_CRIT 0x19
136#define LM90_REG_W_REMOTE_CRIT 0x19
137#define LM90_REG_R_TCRIT_HYST 0x21
138#define LM90_REG_W_TCRIT_HYST 0x21
139
Ben Hutchings271dabf2008-10-17 17:51:11 +0200140/* MAX6646/6647/6649/6657/6658/6659 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200141
142#define MAX6657_REG_R_LOCAL_TEMPL 0x11
Guenter Roeck69487082010-10-28 20:31:43 +0200143#define MAX6659_REG_R_REMOTE_EMERG 0x16
144#define MAX6659_REG_W_REMOTE_EMERG 0x16
145#define MAX6659_REG_R_LOCAL_EMERG 0x17
146#define MAX6659_REG_W_LOCAL_EMERG 0x17
Jean Delvaref65e1702008-10-17 17:51:09 +0200147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
Nate Case23b2d472008-10-17 17:51:10 +0200149 * Device flags
150 */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200151#define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
152/* Device features */
153#define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
154#define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
155#define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
Guenter Roeck69487082010-10-28 20:31:43 +0200156#define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
Nate Case23b2d472008-10-17 17:51:10 +0200157
158/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * Functions declaration
160 */
161
Jean Delvare310ec792009-12-14 21:17:23 +0100162static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200163static int lm90_probe(struct i2c_client *client,
164 const struct i2c_device_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void lm90_init_client(struct i2c_client *client);
Jean Delvare53de3342010-03-05 22:17:15 +0100166static void lm90_alert(struct i2c_client *client, unsigned int flag);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200167static int lm90_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static struct lm90_data *lm90_update_device(struct device *dev);
169
170/*
171 * 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 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100189 { "w83l771", w83l771 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200190 { }
191};
192MODULE_DEVICE_TABLE(i2c, lm90_id);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static struct i2c_driver lm90_driver = {
Jean Delvare9b0e8522008-07-16 19:30:15 +0200195 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100196 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100197 .name = "lm90",
198 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200199 .probe = lm90_probe,
200 .remove = lm90_remove,
Jean Delvare53de3342010-03-05 22:17:15 +0100201 .alert = lm90_alert,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200202 .id_table = lm90_id,
203 .detect = lm90_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100204 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
207/*
208 * Client data (each client gets its own)
209 */
210
211struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700212 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100213 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 char valid; /* zero until following fields are valid */
215 unsigned long last_updated; /* in jiffies */
216 int kind;
Nate Case23b2d472008-10-17 17:51:10 +0200217 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Jean Delvare95238362010-03-05 22:17:14 +0100219 u8 config_orig; /* Original configuration register value */
Jean Delvare53de3342010-03-05 22:17:15 +0100220 u8 alert_alarms; /* Which alarm bits trigger ALERT# */
Jean Delvare95238362010-03-05 22:17:14 +0100221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* registers values */
Guenter Roeck69487082010-10-28 20:31:43 +0200223 s8 temp8[6]; /* 0: local low limit
Jean Delvaref65e1702008-10-17 17:51:09 +0200224 1: local high limit
225 2: local critical limit
Guenter Roeck69487082010-10-28 20:31:43 +0200226 3: remote critical limit
227 4: local emergency limit (max6659 only)
228 5: remote emergency limit (max6659 only) */
Jean Delvaref65e1702008-10-17 17:51:09 +0200229 s16 temp11[5]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200230 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200231 2: remote high limit
Guenter Roeck13c84952010-10-28 20:31:43 +0200232 3: remote offset (except max6646 and max6657/58/59)
Jean Delvaref65e1702008-10-17 17:51:09 +0200233 4: local input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 u8 temp_hyst;
235 u8 alarms; /* bitvector */
236};
237
238/*
Nate Casecea50fe2008-10-17 17:51:10 +0200239 * Conversions
240 * For local temperatures and limits, critical limits and the hysteresis
241 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
242 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200243 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
244 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200245 */
246
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200247static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200248{
249 return val * 1000;
250}
251
Ben Hutchings271dabf2008-10-17 17:51:11 +0200252static inline int temp_from_u8(u8 val)
253{
254 return val * 1000;
255}
256
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200257static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200258{
259 return val / 32 * 125;
260}
261
Ben Hutchings271dabf2008-10-17 17:51:11 +0200262static inline int temp_from_u16(u16 val)
263{
264 return val / 32 * 125;
265}
266
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200267static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200268{
269 if (val <= -128000)
270 return -128;
271 if (val >= 127000)
272 return 127;
273 if (val < 0)
274 return (val - 500) / 1000;
275 return (val + 500) / 1000;
276}
277
Ben Hutchings271dabf2008-10-17 17:51:11 +0200278static u8 temp_to_u8(long val)
279{
280 if (val <= 0)
281 return 0;
282 if (val >= 255000)
283 return 255;
284 return (val + 500) / 1000;
285}
286
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200287static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200288{
289 if (val <= -128000)
290 return 0x8000;
291 if (val >= 127875)
292 return 0x7FE0;
293 if (val < 0)
294 return (val - 62) / 125 * 32;
295 return (val + 62) / 125 * 32;
296}
297
298static u8 hyst_to_reg(long val)
299{
300 if (val <= 0)
301 return 0;
302 if (val >= 30500)
303 return 31;
304 return (val + 500) / 1000;
305}
306
307/*
Nate Case23b2d472008-10-17 17:51:10 +0200308 * ADT7461 in compatibility mode is almost identical to LM90 except that
309 * attempts to write values that are outside the range 0 < temp < 127 are
310 * treated as the boundary value.
311 *
312 * ADT7461 in "extended mode" operation uses unsigned integers offset by
313 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200314 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200315static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200316{
Nate Case23b2d472008-10-17 17:51:10 +0200317 if (data->flags & LM90_FLAG_ADT7461_EXT)
318 return (val - 64) * 1000;
319 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200320 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200321}
322
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200323static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200324{
Nate Case23b2d472008-10-17 17:51:10 +0200325 if (data->flags & LM90_FLAG_ADT7461_EXT)
326 return (val - 0x4000) / 64 * 250;
327 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200328 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200329}
330
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200331static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200332{
333 if (data->flags & LM90_FLAG_ADT7461_EXT) {
334 if (val <= -64000)
335 return 0;
336 if (val >= 191000)
337 return 0xFF;
338 return (val + 500 + 64000) / 1000;
339 } else {
340 if (val <= 0)
341 return 0;
342 if (val >= 127000)
343 return 127;
344 return (val + 500) / 1000;
345 }
346}
347
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200348static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200349{
350 if (data->flags & LM90_FLAG_ADT7461_EXT) {
351 if (val <= -64000)
352 return 0;
353 if (val >= 191750)
354 return 0xFFC0;
355 return (val + 64000 + 125) / 250 * 64;
356 } else {
357 if (val <= 0)
358 return 0;
359 if (val >= 127750)
360 return 0x7FC0;
361 return (val + 125) / 250 * 64;
362 }
Nate Casecea50fe2008-10-17 17:51:10 +0200363}
364
365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * Sysfs stuff
367 */
368
Jean Delvare30d73942005-06-05 21:27:28 +0200369static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
370 char *buf)
371{
372 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
373 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200374 int temp;
375
376 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200377 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200378 else if (data->kind == max6646)
379 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200380 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200381 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200382
Jean Delvare97ae60b2008-10-26 17:04:39 +0100383 /* +16 degrees offset for temp2 for the LM99 */
384 if (data->kind == lm99 && attr->index == 3)
385 temp += 16000;
386
Nate Case23b2d472008-10-17 17:51:10 +0200387 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Jean Delvare30d73942005-06-05 21:27:28 +0200390static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
391 const char *buf, size_t count)
392{
Guenter Roeck69487082010-10-28 20:31:43 +0200393 static const u8 reg[6] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200394 LM90_REG_W_LOCAL_LOW,
395 LM90_REG_W_LOCAL_HIGH,
396 LM90_REG_W_LOCAL_CRIT,
397 LM90_REG_W_REMOTE_CRIT,
Guenter Roeck69487082010-10-28 20:31:43 +0200398 MAX6659_REG_W_LOCAL_EMERG,
399 MAX6659_REG_W_REMOTE_EMERG,
Jean Delvare30d73942005-06-05 21:27:28 +0200400 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jean Delvare30d73942005-06-05 21:27:28 +0200402 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
403 struct i2c_client *client = to_i2c_client(dev);
404 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200405 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200406 long val;
407 int err;
408
409 err = strict_strtol(buf, 10, &val);
410 if (err < 0)
411 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jean Delvare97ae60b2008-10-26 17:04:39 +0100413 /* +16 degrees offset for temp2 for the LM99 */
414 if (data->kind == lm99 && attr->index == 3)
415 val -= 16000;
416
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100417 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200418 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200419 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200420 else if (data->kind == max6646)
421 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200422 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200423 data->temp8[nr] = temp_to_s8(val);
Jean Delvaref65e1702008-10-17 17:51:09 +0200424 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100425 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200426 return count;
427}
428
429static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
430 char *buf)
431{
Guenter Roeck96512862010-10-28 20:31:43 +0200432 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200433 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200434 int temp;
435
436 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200437 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200438 else if (data->kind == max6646)
439 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200440 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200441 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200442
Jean Delvare97ae60b2008-10-26 17:04:39 +0100443 /* +16 degrees offset for temp2 for the LM99 */
444 if (data->kind == lm99 && attr->index <= 2)
445 temp += 16000;
446
Nate Case23b2d472008-10-17 17:51:10 +0200447 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200448}
449
450static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
451 const char *buf, size_t count)
452{
Guenter Roeck96512862010-10-28 20:31:43 +0200453 struct {
454 u8 high;
455 u8 low;
456 } reg[3] = {
457 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
458 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
459 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL }
Jean Delvare30d73942005-06-05 21:27:28 +0200460 };
461
Guenter Roeck96512862010-10-28 20:31:43 +0200462 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200463 struct i2c_client *client = to_i2c_client(dev);
464 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck96512862010-10-28 20:31:43 +0200465 int nr = attr->nr;
466 int index = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200467 long val;
468 int err;
469
470 err = strict_strtol(buf, 10, &val);
471 if (err < 0)
472 return err;
Jean Delvare30d73942005-06-05 21:27:28 +0200473
Jean Delvare97ae60b2008-10-26 17:04:39 +0100474 /* +16 degrees offset for temp2 for the LM99 */
Guenter Roeck96512862010-10-28 20:31:43 +0200475 if (data->kind == lm99 && index <= 2)
Jean Delvare97ae60b2008-10-26 17:04:39 +0100476 val -= 16000;
477
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100478 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200479 if (data->kind == adt7461)
Guenter Roeck96512862010-10-28 20:31:43 +0200480 data->temp11[index] = temp_to_u16_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200481 else if (data->kind == max6646)
Guenter Roeck96512862010-10-28 20:31:43 +0200482 data->temp11[index] = temp_to_u8(val) << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +0200483 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200484 data->temp11[index] = temp_to_s16(val);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200485 else
Guenter Roeck96512862010-10-28 20:31:43 +0200486 data->temp11[index] = temp_to_s8(val) << 8;
Jean Delvare5f502a82008-10-17 17:51:09 +0200487
Guenter Roeck96512862010-10-28 20:31:43 +0200488 i2c_smbus_write_byte_data(client, reg[nr].high,
489 data->temp11[index] >> 8);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200490 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200491 i2c_smbus_write_byte_data(client, reg[nr].low,
492 data->temp11[index] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100493 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200494 return count;
495}
496
Guenter Roeck11e57812010-10-28 20:31:42 +0200497static ssize_t show_temphyst(struct device *dev,
498 struct device_attribute *devattr,
Jean Delvare30d73942005-06-05 21:27:28 +0200499 char *buf)
500{
501 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
502 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200503 int temp;
504
505 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200506 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100507 else if (data->kind == max6646)
508 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200509 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200510 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200511
Jean Delvare97ae60b2008-10-26 17:04:39 +0100512 /* +16 degrees offset for temp2 for the LM99 */
513 if (data->kind == lm99 && attr->index == 3)
514 temp += 16000;
515
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200516 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200517}
518
519static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
520 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct i2c_client *client = to_i2c_client(dev);
523 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck11e57812010-10-28 20:31:42 +0200524 long val;
525 int err;
Jean Delvareec38fa22008-10-26 17:04:39 +0100526 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Guenter Roeck11e57812010-10-28 20:31:42 +0200528 err = strict_strtol(buf, 10, &val);
529 if (err < 0)
530 return err;
531
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100532 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100533 if (data->kind == adt7461)
534 temp = temp_from_u8_adt7461(data, data->temp8[2]);
535 else if (data->kind == max6646)
536 temp = temp_from_u8(data->temp8[2]);
537 else
538 temp = temp_from_s8(data->temp8[2]);
539
540 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100542 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100543 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return count;
545}
546
Jean Delvare30d73942005-06-05 21:27:28 +0200547static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
548 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct lm90_data *data = lm90_update_device(dev);
551 return sprintf(buf, "%d\n", data->alarms);
552}
553
Jean Delvare2d457712006-09-24 20:52:15 +0200554static ssize_t show_alarm(struct device *dev, struct device_attribute
555 *devattr, char *buf)
556{
557 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
558 struct lm90_data *data = lm90_update_device(dev);
559 int bitnr = attr->index;
560
561 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
562}
563
Guenter Roeck96512862010-10-28 20:31:43 +0200564static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
565static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200566static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200567 set_temp8, 0);
Guenter Roeck96512862010-10-28 20:31:43 +0200568static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
569 set_temp11, 0, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200570static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200571 set_temp8, 1);
Guenter Roeck96512862010-10-28 20:31:43 +0200572static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
573 set_temp11, 1, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200574static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200575 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200576static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200577 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200578static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200579 set_temphyst, 2);
580static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Guenter Roeck96512862010-10-28 20:31:43 +0200581static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
582 set_temp11, 2, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200583
584/* Individual alarm files */
585static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
586static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400587static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200588static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
589static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
590static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
591static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
592/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
594
Jean Delvare0e39e012006-09-24 21:16:40 +0200595static struct attribute *lm90_attributes[] = {
596 &sensor_dev_attr_temp1_input.dev_attr.attr,
597 &sensor_dev_attr_temp2_input.dev_attr.attr,
598 &sensor_dev_attr_temp1_min.dev_attr.attr,
599 &sensor_dev_attr_temp2_min.dev_attr.attr,
600 &sensor_dev_attr_temp1_max.dev_attr.attr,
601 &sensor_dev_attr_temp2_max.dev_attr.attr,
602 &sensor_dev_attr_temp1_crit.dev_attr.attr,
603 &sensor_dev_attr_temp2_crit.dev_attr.attr,
604 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
605 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
606
607 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
608 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400609 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200610 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
611 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
612 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
613 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
614 &dev_attr_alarms.attr,
615 NULL
616};
617
618static const struct attribute_group lm90_group = {
619 .attrs = lm90_attributes,
620};
621
Guenter Roeck69487082010-10-28 20:31:43 +0200622/*
623 * Additional attributes for devices with emergency sensors
624 */
625static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
626 set_temp8, 4);
627static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
628 set_temp8, 5);
629static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
630 NULL, 4);
631static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
632 NULL, 5);
633
634static struct attribute *lm90_emergency_attributes[] = {
635 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
636 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
637 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
638 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
639 NULL
640};
641
642static const struct attribute_group lm90_emergency_group = {
643 .attrs = lm90_emergency_attributes,
644};
645
Jean Delvarec3df5802005-10-26 21:39:40 +0200646/* pec used for ADM1032 only */
647static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
648 char *buf)
649{
650 struct i2c_client *client = to_i2c_client(dev);
651 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
652}
653
654static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
655 const char *buf, size_t count)
656{
657 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200658 long val;
659 int err;
660
661 err = strict_strtol(buf, 10, &val);
662 if (err < 0)
663 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200664
665 switch (val) {
666 case 0:
667 client->flags &= ~I2C_CLIENT_PEC;
668 break;
669 case 1:
670 client->flags |= I2C_CLIENT_PEC;
671 break;
672 default:
673 return -EINVAL;
674 }
675
676 return count;
677}
678
679static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/*
682 * Real code
683 */
684
Guenter Roeck11e57812010-10-28 20:31:42 +0200685/*
686 * The ADM1032 supports PEC but not on write byte transactions, so we need
687 * to explicitly ask for a transaction without PEC.
688 */
Jean Delvarec3df5802005-10-26 21:39:40 +0200689static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
690{
691 return i2c_smbus_xfer(client->adapter, client->addr,
692 client->flags & ~I2C_CLIENT_PEC,
693 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
694}
695
Guenter Roeck11e57812010-10-28 20:31:42 +0200696/*
697 * It is assumed that client->update_lock is held (unless we are in
698 * detection or initialization steps). This matters when PEC is enabled,
699 * because we don't want the address pointer to change between the write
700 * byte and the read byte transactions.
701 */
702static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
Jean Delvare8256fe02005-10-26 21:37:52 +0200703{
704 int err;
705
Guenter Roeck11e57812010-10-28 20:31:42 +0200706 if (client->flags & I2C_CLIENT_PEC) {
707 err = adm1032_write_byte(client, reg);
708 if (err >= 0)
709 err = i2c_smbus_read_byte(client);
710 } else
711 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200712
713 if (err < 0) {
714 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
715 reg, err);
716 return err;
717 }
718 *value = err;
719
720 return 0;
721}
722
Jean Delvare9b0e8522008-07-16 19:30:15 +0200723/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100724static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200725 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200727 struct i2c_adapter *adapter = new_client->adapter;
728 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +0100729 const char *name = NULL;
730 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200733 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Jean Delvare8f2fa772009-12-09 20:35:53 +0100735 /* detection and identification */
736 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100737 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100738 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100739 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100740 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100741 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100742 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100743 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100744 return -ENODEV;
745
746 if ((address == 0x4C || address == 0x4D)
747 && man_id == 0x01) { /* National Semiconductor */
748 int reg_config2;
749
750 reg_config2 = i2c_smbus_read_byte_data(new_client,
751 LM90_REG_R_CONFIG2);
752 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200753 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Jean Delvare8f2fa772009-12-09 20:35:53 +0100755 if ((reg_config1 & 0x2A) == 0x00
756 && (reg_config2 & 0xF8) == 0x00
757 && reg_convrate <= 0x09) {
758 if (address == 0x4C
759 && (chip_id & 0xF0) == 0x20) { /* LM90 */
760 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100762 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
763 name = "lm99";
764 dev_info(&adapter->dev,
765 "Assuming LM99 chip at 0x%02x\n",
766 address);
767 dev_info(&adapter->dev,
768 "If it is an LM89, instantiate it "
769 "with the new_device sysfs "
770 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400771 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100772 if (address == 0x4C
773 && (chip_id & 0xF0) == 0x10) { /* LM86 */
774 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100777 } else
778 if ((address == 0x4C || address == 0x4D)
779 && man_id == 0x41) { /* Analog Devices */
780 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
781 && (reg_config1 & 0x3F) == 0x00
782 && reg_convrate <= 0x0A) {
783 name = "adm1032";
784 /* The ADM1032 supports PEC, but only if combined
785 transactions are not used. */
786 if (i2c_check_functionality(adapter,
787 I2C_FUNC_SMBUS_BYTE))
788 info->flags |= I2C_CLIENT_PEC;
789 } else
790 if (chip_id == 0x51 /* ADT7461 */
791 && (reg_config1 & 0x1B) == 0x00
792 && reg_convrate <= 0x0A) {
793 name = "adt7461";
794 }
795 } else
796 if (man_id == 0x4D) { /* Maxim */
797 /*
798 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
799 * register. Reading from that address will return the last
800 * read value, which in our case is those of the man_id
801 * register. Likewise, the config1 register seems to lack a
802 * low nibble, so the value will be those of the previous
803 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +0200804 * MAX6659 has a third set of upper temperature limit registers.
805 * Those registers also return values on MAX6657 and MAX6658,
806 * thus the only way to detect MAX6659 is by its address.
807 * For this reason it will be mis-detected as MAX6657 if its
808 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +0100809 */
810 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +0200811 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100812 && (reg_config1 & 0x1F) == (man_id & 0x0F)
813 && reg_convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +0200814 if (address == 0x4C)
815 name = "max6657";
816 else
817 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +0100818 } else
819 /*
820 * The chip_id register of the MAX6680 and MAX6681 holds the
821 * revision of the chip. The lowest bit of the config1 register
822 * is unused and should return zero when read, so should the
823 * second to last bit of config1 (software reset).
824 */
825 if (chip_id == 0x01
826 && (reg_config1 & 0x03) == 0x00
827 && reg_convrate <= 0x07) {
828 name = "max6680";
829 } else
830 /*
831 * The chip_id register of the MAX6646/6647/6649 holds the
832 * revision of the chip. The lowest 6 bits of the config1
833 * register are unused and should return zero when read.
834 */
835 if (chip_id == 0x59
836 && (reg_config1 & 0x3f) == 0x00
837 && reg_convrate <= 0x07) {
838 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Jean Delvare6771ea12010-03-05 22:17:13 +0100840 } else
841 if (address == 0x4C
842 && man_id == 0x5C) { /* Winbond/Nuvoton */
843 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
844 && (reg_config1 & 0x2A) == 0x00
845 && reg_convrate <= 0x08) {
846 name = "w83l771";
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Jean Delvare8f2fa772009-12-09 20:35:53 +0100850 if (!name) { /* identification failed */
851 dev_dbg(&adapter->dev,
852 "Unsupported chip at 0x%02x (man_id=0x%02X, "
853 "chip_id=0x%02X)\n", address, man_id, chip_id);
854 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100856
Jean Delvare9b0e8522008-07-16 19:30:15 +0200857 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Jean Delvare9b0e8522008-07-16 19:30:15 +0200859 return 0;
860}
861
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +0200862static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
863{
Guenter Roeck69487082010-10-28 20:31:43 +0200864 if (data->flags & LM90_HAVE_EMERGENCY)
865 sysfs_remove_group(&client->dev.kobj,
866 &lm90_emergency_group);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +0200867 if (data->flags & LM90_HAVE_OFFSET)
868 device_remove_file(&client->dev,
869 &sensor_dev_attr_temp2_offset.dev_attr);
870 device_remove_file(&client->dev, &dev_attr_pec);
871 sysfs_remove_group(&client->dev.kobj, &lm90_group);
872}
873
Jean Delvare9b0e8522008-07-16 19:30:15 +0200874static int lm90_probe(struct i2c_client *new_client,
875 const struct i2c_device_id *id)
876{
877 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
878 struct lm90_data *data;
879 int err;
880
881 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
882 if (!data) {
883 err = -ENOMEM;
884 goto exit;
885 }
886 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100887 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Jean Delvare9b0e8522008-07-16 19:30:15 +0200889 /* Set the device type */
890 data->kind = id->driver_data;
891 if (data->kind == adm1032) {
892 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
893 new_client->flags &= ~I2C_CLIENT_PEC;
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Jean Delvare53de3342010-03-05 22:17:15 +0100896 /* Different devices have different alarm bits triggering the
897 * ALERT# output */
898 switch (data->kind) {
899 case lm90:
900 case lm99:
901 case lm86:
902 data->alert_alarms = 0x7b;
903 break;
904 default:
905 data->alert_alarms = 0x7c;
906 break;
907 }
908
Guenter Roeck88073bb2010-10-28 20:31:43 +0200909 /* Set chip capabilities */
Guenter Roeck13c84952010-10-28 20:31:43 +0200910 if (data->kind != max6657 && data->kind != max6659
911 && data->kind != max6646)
Guenter Roeck88073bb2010-10-28 20:31:43 +0200912 data->flags |= LM90_HAVE_OFFSET;
913
Guenter Roeck13c84952010-10-28 20:31:43 +0200914 if (data->kind == max6657 || data->kind == max6659
915 || data->kind == max6646)
Guenter Roeck88073bb2010-10-28 20:31:43 +0200916 data->flags |= LM90_HAVE_LOCAL_EXT;
917
Guenter Roeck13c84952010-10-28 20:31:43 +0200918 if (data->kind != max6657 && data->kind != max6659
919 && data->kind != max6646 && data->kind != max6680)
Guenter Roeck88073bb2010-10-28 20:31:43 +0200920 data->flags |= LM90_HAVE_REM_LIMIT_EXT;
921
Guenter Roeck69487082010-10-28 20:31:43 +0200922 if (data->kind == max6659)
923 data->flags |= LM90_HAVE_EMERGENCY;
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* Initialize the LM90 chip */
926 lm90_init_client(new_client);
927
928 /* Register sysfs hooks */
Guenter Roeck11e57812010-10-28 20:31:42 +0200929 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
930 if (err)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200931 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200932 if (new_client->flags & I2C_CLIENT_PEC) {
Guenter Roeck11e57812010-10-28 20:31:42 +0200933 err = device_create_file(&new_client->dev, &dev_attr_pec);
934 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +0200935 goto exit_remove_files;
936 }
Guenter Roeck88073bb2010-10-28 20:31:43 +0200937 if (data->flags & LM90_HAVE_OFFSET) {
Guenter Roeck11e57812010-10-28 20:31:42 +0200938 err = device_create_file(&new_client->dev,
939 &sensor_dev_attr_temp2_offset.dev_attr);
940 if (err)
Jean Delvare69f2f962007-09-05 14:15:37 +0200941 goto exit_remove_files;
942 }
Guenter Roeck69487082010-10-28 20:31:43 +0200943 if (data->flags & LM90_HAVE_EMERGENCY) {
944 err = sysfs_create_group(&new_client->dev.kobj,
945 &lm90_emergency_group);
946 if (err)
947 goto exit_remove_files;
948 }
Jean Delvare0e39e012006-09-24 21:16:40 +0200949
Tony Jones1beeffe2007-08-20 13:46:20 -0700950 data->hwmon_dev = hwmon_device_register(&new_client->dev);
951 if (IS_ERR(data->hwmon_dev)) {
952 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200953 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400954 }
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957
Jean Delvare0e39e012006-09-24 21:16:40 +0200958exit_remove_files:
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +0200959 lm90_remove_files(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960exit_free:
961 kfree(data);
962exit:
963 return err;
964}
965
966static void lm90_init_client(struct i2c_client *client)
967{
Jean Delvare95238362010-03-05 22:17:14 +0100968 u8 config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400969 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /*
972 * Start the conversions.
973 */
974 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
975 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +0200976 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
977 dev_warn(&client->dev, "Initialization failed!\n");
978 return;
979 }
Jean Delvare95238362010-03-05 22:17:14 +0100980 data->config_orig = config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400981
Nate Case23b2d472008-10-17 17:51:10 +0200982 /* Check Temperature Range Select */
983 if (data->kind == adt7461) {
984 if (config & 0x04)
985 data->flags |= LM90_FLAG_ADT7461_EXT;
986 }
987
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400988 /*
989 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
990 * 0.125 degree resolution) and range (0x08, extend range
991 * to -64 degree) mode for the remote temperature sensor.
992 */
Guenter Roeck11e57812010-10-28 20:31:42 +0200993 if (data->kind == max6680)
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400994 config |= 0x18;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400995
996 config &= 0xBF; /* run */
Jean Delvare95238362010-03-05 22:17:14 +0100997 if (config != data->config_orig) /* Only write if changed */
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400998 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Jean Delvare9b0e8522008-07-16 19:30:15 +02001001static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001003 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Tony Jones1beeffe2007-08-20 13:46:20 -07001005 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001006 lm90_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001007
Jean Delvare95238362010-03-05 22:17:14 +01001008 /* Restore initial configuration */
1009 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1010 data->config_orig);
1011
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001012 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014}
1015
Jean Delvare53de3342010-03-05 22:17:15 +01001016static void lm90_alert(struct i2c_client *client, unsigned int flag)
1017{
1018 struct lm90_data *data = i2c_get_clientdata(client);
1019 u8 config, alarms;
1020
1021 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
1022 if ((alarms & 0x7f) == 0) {
1023 dev_info(&client->dev, "Everything OK\n");
1024 } else {
1025 if (alarms & 0x61)
1026 dev_warn(&client->dev,
1027 "temp%d out of range, please check!\n", 1);
1028 if (alarms & 0x1a)
1029 dev_warn(&client->dev,
1030 "temp%d out of range, please check!\n", 2);
1031 if (alarms & 0x04)
1032 dev_warn(&client->dev,
1033 "temp%d diode open, please check!\n", 2);
1034
1035 /* Disable ALERT# output, because these chips don't implement
1036 SMBus alert correctly; they should only hold the alert line
1037 low briefly. */
1038 if ((data->kind == adm1032 || data->kind == adt7461)
1039 && (alarms & data->alert_alarms)) {
1040 dev_dbg(&client->dev, "Disabling ALERT#\n");
1041 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1042 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1043 config | 0x80);
1044 }
1045 }
1046}
1047
Jean Delvare6388a382008-10-17 17:51:09 +02001048static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
1049{
1050 int err;
1051 u8 oldh, newh, l;
1052
1053 /*
1054 * There is a trick here. We have to read two registers to have the
1055 * sensor temperature, but we have to beware a conversion could occur
1056 * inbetween the readings. The datasheet says we should either use
1057 * the one-shot conversion register, which we don't want to do
1058 * (disables hardware monitoring) or monitor the busy bit, which is
1059 * impossible (we can't read the values and monitor that bit at the
1060 * exact same time). So the solution used here is to read the high
1061 * byte once, then the low byte, then the high byte again. If the new
1062 * high byte matches the old one, then we have a valid reading. Else
1063 * we have to read the low byte again, and now we believe we have a
1064 * correct reading.
1065 */
1066 if ((err = lm90_read_reg(client, regh, &oldh))
1067 || (err = lm90_read_reg(client, regl, &l))
1068 || (err = lm90_read_reg(client, regh, &newh)))
1069 return err;
1070 if (oldh != newh) {
1071 err = lm90_read_reg(client, regl, &l);
1072 if (err)
1073 return err;
1074 }
1075 *value = (newh << 8) | l;
1076
1077 return 0;
1078}
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080static struct lm90_data *lm90_update_device(struct device *dev)
1081{
1082 struct i2c_client *client = to_i2c_client(dev);
1083 struct lm90_data *data = i2c_get_clientdata(client);
1084
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001085 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Ira W. Snyder8c3c7a22010-05-27 19:58:44 +02001087 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
1088 || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +02001089 u8 h, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +02001092 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
1093 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
1094 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
1095 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +02001096 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Guenter Roeck88073bb2010-10-28 20:31:43 +02001098 if (data->flags & LM90_HAVE_LOCAL_EXT) {
Jean Delvaref65e1702008-10-17 17:51:09 +02001099 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
1100 MAX6657_REG_R_LOCAL_TEMPL,
1101 &data->temp11[4]);
1102 } else {
1103 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
1104 &h) == 0)
1105 data->temp11[4] = h << 8;
1106 }
Jean Delvare6388a382008-10-17 17:51:09 +02001107 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
1108 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jean Delvare5f502a82008-10-17 17:51:09 +02001110 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
1111 data->temp11[1] = h << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +02001112 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
Jean Delvare5f502a82008-10-17 17:51:09 +02001113 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
1114 &l) == 0)
1115 data->temp11[1] |= l;
1116 }
1117 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
1118 data->temp11[2] = h << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +02001119 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
Jean Delvare5f502a82008-10-17 17:51:09 +02001120 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
1121 &l) == 0)
1122 data->temp11[2] |= l;
1123 }
1124
Guenter Roeck88073bb2010-10-28 20:31:43 +02001125 if (data->flags & LM90_HAVE_OFFSET) {
Jean Delvare69f2f962007-09-05 14:15:37 +02001126 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +02001127 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +02001128 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
1129 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +02001130 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +02001131 }
Guenter Roeck69487082010-10-28 20:31:43 +02001132 if (data->flags & LM90_HAVE_EMERGENCY) {
1133 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
1134 &data->temp8[4]);
1135 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
1136 &data->temp8[5]);
1137 }
Jean Delvare8256fe02005-10-26 21:37:52 +02001138 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Jean Delvare53de3342010-03-05 22:17:15 +01001140 /* Re-enable ALERT# output if it was originally enabled and
1141 * relevant alarms are all clear */
1142 if ((data->config_orig & 0x80) == 0
1143 && (data->alarms & data->alert_alarms) == 0) {
1144 u8 config;
1145
1146 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1147 if (config & 0x80) {
1148 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1149 i2c_smbus_write_byte_data(client,
1150 LM90_REG_W_CONFIG1,
1151 config & ~0x80);
1152 }
1153 }
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 data->last_updated = jiffies;
1156 data->valid = 1;
1157 }
1158
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001159 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 return data;
1162}
1163
1164static int __init sensors_lm90_init(void)
1165{
1166 return i2c_add_driver(&lm90_driver);
1167}
1168
1169static void __exit sensors_lm90_exit(void)
1170{
1171 i2c_del_driver(&lm90_driver);
1172}
1173
1174MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1175MODULE_DESCRIPTION("LM90/ADM1032 driver");
1176MODULE_LICENSE("GPL");
1177
1178module_init(sensors_lm90_init);
1179module_exit(sensors_lm90_exit);