blob: d96e403d59e089347e3068b459e9ae16b5a034ae [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 Delvare6388a382008-10-17 17:51:09 +02004 * Copyright (C) 2003-2008 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
15 * degrees, which allows for higher temperatures measurement. The
16 * driver doesn't handle it since it can be done easily in user-space.
Steven Cole44bbe872005-05-03 18:21:25 -060017 * Note that there is no way to differentiate between both chips.
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * variants. The extra address and features of the MAX6659 are not
Jean Delvare69f2f962007-09-05 14:15:37 +020032 * supported by this driver. These chips lack the remote temperature
33 * offset feature.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040035 * This driver also supports the MAX6680 and MAX6681, two other sensor
36 * chips made by Maxim. These are quite similar to the other Maxim
Jean Delvarea874a102008-10-17 17:51:10 +020037 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
38 * be treated identically.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040039 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * This driver also supports the ADT7461 chip from Analog Devices but
41 * only in its "compatability mode". If an ADT7461 chip is found but
42 * is configured in non-compatible mode (where its temperature
43 * register values are decoded differently) it is ignored by this
Jean Delvarea874a102008-10-17 17:51:10 +020044 * driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
46 * Since the LM90 was the first chipset supported by this driver, most
47 * comments will refer to this chipset, but are actually general and
48 * concern all supported chipsets, unless mentioned otherwise.
49 *
50 * This program is free software; you can redistribute it and/or modify
51 * it under the terms of the GNU General Public License as published by
52 * the Free Software Foundation; either version 2 of the License, or
53 * (at your option) any later version.
54 *
55 * This program is distributed in the hope that it will be useful,
56 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 * GNU General Public License for more details.
59 *
60 * You should have received a copy of the GNU General Public License
61 * along with this program; if not, write to the Free Software
62 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
63 */
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/module.h>
66#include <linux/init.h>
67#include <linux/slab.h>
68#include <linux/jiffies.h>
69#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020070#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040071#include <linux/hwmon.h>
72#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010073#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020074#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/*
77 * Addresses to scan
78 * Address is fully defined internally and cannot be changed except for
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040079 * MAX6659, MAX6680 and MAX6681.
Jean Delvare90209b42005-10-26 22:20:21 +020080 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6657 and MAX6658
81 * have address 0x4c.
82 * ADM1032-2, ADT7461-2, LM89-1, and LM99-1 have address 0x4d.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040084 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
85 * 0x4c, 0x4d or 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050088static const unsigned short normal_i2c[] = {
89 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * Insmod parameters
93 */
94
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040095I2C_CLIENT_INSMOD_7(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*
98 * The LM90 registers
99 */
100
101#define LM90_REG_R_MAN_ID 0xFE
102#define LM90_REG_R_CHIP_ID 0xFF
103#define LM90_REG_R_CONFIG1 0x03
104#define LM90_REG_W_CONFIG1 0x09
105#define LM90_REG_R_CONFIG2 0xBF
106#define LM90_REG_W_CONFIG2 0xBF
107#define LM90_REG_R_CONVRATE 0x04
108#define LM90_REG_W_CONVRATE 0x0A
109#define LM90_REG_R_STATUS 0x02
110#define LM90_REG_R_LOCAL_TEMP 0x00
111#define LM90_REG_R_LOCAL_HIGH 0x05
112#define LM90_REG_W_LOCAL_HIGH 0x0B
113#define LM90_REG_R_LOCAL_LOW 0x06
114#define LM90_REG_W_LOCAL_LOW 0x0C
115#define LM90_REG_R_LOCAL_CRIT 0x20
116#define LM90_REG_W_LOCAL_CRIT 0x20
117#define LM90_REG_R_REMOTE_TEMPH 0x01
118#define LM90_REG_R_REMOTE_TEMPL 0x10
119#define LM90_REG_R_REMOTE_OFFSH 0x11
120#define LM90_REG_W_REMOTE_OFFSH 0x11
121#define LM90_REG_R_REMOTE_OFFSL 0x12
122#define LM90_REG_W_REMOTE_OFFSL 0x12
123#define LM90_REG_R_REMOTE_HIGHH 0x07
124#define LM90_REG_W_REMOTE_HIGHH 0x0D
125#define LM90_REG_R_REMOTE_HIGHL 0x13
126#define LM90_REG_W_REMOTE_HIGHL 0x13
127#define LM90_REG_R_REMOTE_LOWH 0x08
128#define LM90_REG_W_REMOTE_LOWH 0x0E
129#define LM90_REG_R_REMOTE_LOWL 0x14
130#define LM90_REG_W_REMOTE_LOWL 0x14
131#define LM90_REG_R_REMOTE_CRIT 0x19
132#define LM90_REG_W_REMOTE_CRIT 0x19
133#define LM90_REG_R_TCRIT_HYST 0x21
134#define LM90_REG_W_TCRIT_HYST 0x21
135
Jean Delvaref65e1702008-10-17 17:51:09 +0200136/* MAX6657-specific registers */
137
138#define MAX6657_REG_R_LOCAL_TEMPL 0x11
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Conversions and various macros
142 * For local temperatures and limits, critical limits and the hysteresis
Steven Cole44bbe872005-05-03 18:21:25 -0600143 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * For remote temperatures and limits, it uses signed 11-bit values with
Steven Cole44bbe872005-05-03 18:21:25 -0600145 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
147
148#define TEMP1_FROM_REG(val) ((val) * 1000)
149#define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
150 (val) >= 127000 ? 127 : \
151 (val) < 0 ? ((val) - 500) / 1000 : \
152 ((val) + 500) / 1000)
153#define TEMP2_FROM_REG(val) ((val) / 32 * 125)
154#define TEMP2_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
155 (val) >= 127875 ? 0x7FE0 : \
156 (val) < 0 ? ((val) - 62) / 125 * 32 : \
157 ((val) + 62) / 125 * 32)
158#define HYST_TO_REG(val) ((val) <= 0 ? 0 : (val) >= 30500 ? 31 : \
159 ((val) + 500) / 1000)
160
161/*
162 * ADT7461 is almost identical to LM90 except that attempts to write
163 * values that are outside the range 0 < temp < 127 are treated as
164 * the boundary value.
165 */
166
167#define TEMP1_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
168 (val) >= 127000 ? 127 : \
169 ((val) + 500) / 1000)
170#define TEMP2_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
171 (val) >= 127750 ? 0x7FC0 : \
172 ((val) + 125) / 250 * 64)
173
174/*
175 * Functions declaration
176 */
177
Jean Delvare9b0e8522008-07-16 19:30:15 +0200178static int lm90_detect(struct i2c_client *client, int kind,
179 struct i2c_board_info *info);
180static int lm90_probe(struct i2c_client *client,
181 const struct i2c_device_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void lm90_init_client(struct i2c_client *client);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200183static int lm90_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static struct lm90_data *lm90_update_device(struct device *dev);
185
186/*
187 * Driver data (common to all clients)
188 */
189
Jean Delvare9b0e8522008-07-16 19:30:15 +0200190static const struct i2c_device_id lm90_id[] = {
191 { "adm1032", adm1032 },
192 { "adt7461", adt7461 },
193 { "lm90", lm90 },
194 { "lm86", lm86 },
195 { "lm89", lm99 },
196 { "lm99", lm99 }, /* Missing temperature offset */
197 { "max6657", max6657 },
198 { "max6658", max6657 },
199 { "max6659", max6657 },
200 { "max6680", max6680 },
201 { "max6681", max6680 },
202 { }
203};
204MODULE_DEVICE_TABLE(i2c, lm90_id);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static struct i2c_driver lm90_driver = {
Jean Delvare9b0e8522008-07-16 19:30:15 +0200207 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100208 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100209 .name = "lm90",
210 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200211 .probe = lm90_probe,
212 .remove = lm90_remove,
213 .id_table = lm90_id,
214 .detect = lm90_detect,
215 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218/*
219 * Client data (each client gets its own)
220 */
221
222struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700223 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100224 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 char valid; /* zero until following fields are valid */
226 unsigned long last_updated; /* in jiffies */
227 int kind;
228
229 /* registers values */
Jean Delvaref65e1702008-10-17 17:51:09 +0200230 s8 temp8[4]; /* 0: local low limit
231 1: local high limit
232 2: local critical limit
233 3: remote critical limit */
234 s16 temp11[5]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200235 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200236 2: remote high limit
Jean Delvaref65e1702008-10-17 17:51:09 +0200237 3: remote offset (except max6657)
238 4: local input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 u8 temp_hyst;
240 u8 alarms; /* bitvector */
241};
242
243/*
244 * Sysfs stuff
245 */
246
Jean Delvare30d73942005-06-05 21:27:28 +0200247static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
248 char *buf)
249{
250 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
251 struct lm90_data *data = lm90_update_device(dev);
252 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jean Delvare30d73942005-06-05 21:27:28 +0200255static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
256 const char *buf, size_t count)
257{
258 static const u8 reg[4] = {
259 LM90_REG_W_LOCAL_LOW,
260 LM90_REG_W_LOCAL_HIGH,
261 LM90_REG_W_LOCAL_CRIT,
262 LM90_REG_W_REMOTE_CRIT,
263 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Jean Delvare30d73942005-06-05 21:27:28 +0200265 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
266 struct i2c_client *client = to_i2c_client(dev);
267 struct lm90_data *data = i2c_get_clientdata(client);
268 long val = simple_strtol(buf, NULL, 10);
269 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100271 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200272 if (data->kind == adt7461)
273 data->temp8[nr] = TEMP1_TO_REG_ADT7461(val);
274 else
275 data->temp8[nr] = TEMP1_TO_REG(val);
Jean Delvaref65e1702008-10-17 17:51:09 +0200276 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100277 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200278 return count;
279}
280
281static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
282 char *buf)
283{
284 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
285 struct lm90_data *data = lm90_update_device(dev);
286 return sprintf(buf, "%d\n", TEMP2_FROM_REG(data->temp11[attr->index]));
287}
288
289static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
290 const char *buf, size_t count)
291{
Jean Delvare69f2f962007-09-05 14:15:37 +0200292 static const u8 reg[6] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200293 LM90_REG_W_REMOTE_LOWH,
294 LM90_REG_W_REMOTE_LOWL,
295 LM90_REG_W_REMOTE_HIGHH,
296 LM90_REG_W_REMOTE_HIGHL,
Jean Delvare69f2f962007-09-05 14:15:37 +0200297 LM90_REG_W_REMOTE_OFFSH,
298 LM90_REG_W_REMOTE_OFFSL,
Jean Delvare30d73942005-06-05 21:27:28 +0200299 };
300
301 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
302 struct i2c_client *client = to_i2c_client(dev);
303 struct lm90_data *data = i2c_get_clientdata(client);
304 long val = simple_strtol(buf, NULL, 10);
305 int nr = attr->index;
306
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100307 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200308 if (data->kind == adt7461)
309 data->temp11[nr] = TEMP2_TO_REG_ADT7461(val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200310 else if (data->kind == max6657 || data->kind == max6680)
311 data->temp11[nr] = TEMP1_TO_REG(val) << 8;
Jean Delvare30d73942005-06-05 21:27:28 +0200312 else
313 data->temp11[nr] = TEMP2_TO_REG(val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200314
Jean Delvare30d73942005-06-05 21:27:28 +0200315 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
316 data->temp11[nr] >> 8);
Jean Delvare5f502a82008-10-17 17:51:09 +0200317 if (data->kind != max6657 && data->kind != max6680)
318 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
319 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100320 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200321 return count;
322}
323
324static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
325 char *buf)
326{
327 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
328 struct lm90_data *data = lm90_update_device(dev);
329 return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index])
330 - TEMP1_FROM_REG(data->temp_hyst));
331}
332
333static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
334 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct i2c_client *client = to_i2c_client(dev);
337 struct lm90_data *data = i2c_get_clientdata(client);
338 long val = simple_strtol(buf, NULL, 10);
339 long hyst;
340
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100341 mutex_lock(&data->update_lock);
Jean Delvaref65e1702008-10-17 17:51:09 +0200342 hyst = TEMP1_FROM_REG(data->temp8[2]) - val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
344 HYST_TO_REG(hyst));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100345 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return count;
347}
348
Jean Delvare30d73942005-06-05 21:27:28 +0200349static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
350 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct lm90_data *data = lm90_update_device(dev);
353 return sprintf(buf, "%d\n", data->alarms);
354}
355
Jean Delvare2d457712006-09-24 20:52:15 +0200356static ssize_t show_alarm(struct device *dev, struct device_attribute
357 *devattr, char *buf)
358{
359 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
360 struct lm90_data *data = lm90_update_device(dev);
361 int bitnr = attr->index;
362
363 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
364}
365
Jean Delvaref65e1702008-10-17 17:51:09 +0200366static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
Jean Delvare30d73942005-06-05 21:27:28 +0200367static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
368static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200369 set_temp8, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200370static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
371 set_temp11, 1);
372static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200373 set_temp8, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200374static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
375 set_temp11, 2);
376static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200377 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200378static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200379 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200380static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200381 set_temphyst, 2);
382static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Jean Delvare69f2f962007-09-05 14:15:37 +0200383static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
384 set_temp11, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200385
386/* Individual alarm files */
387static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
388static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400389static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200390static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
391static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
392static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
393static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
394/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
396
Jean Delvare0e39e012006-09-24 21:16:40 +0200397static struct attribute *lm90_attributes[] = {
398 &sensor_dev_attr_temp1_input.dev_attr.attr,
399 &sensor_dev_attr_temp2_input.dev_attr.attr,
400 &sensor_dev_attr_temp1_min.dev_attr.attr,
401 &sensor_dev_attr_temp2_min.dev_attr.attr,
402 &sensor_dev_attr_temp1_max.dev_attr.attr,
403 &sensor_dev_attr_temp2_max.dev_attr.attr,
404 &sensor_dev_attr_temp1_crit.dev_attr.attr,
405 &sensor_dev_attr_temp2_crit.dev_attr.attr,
406 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
407 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
408
409 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
410 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400411 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200412 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
413 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
414 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
415 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
416 &dev_attr_alarms.attr,
417 NULL
418};
419
420static const struct attribute_group lm90_group = {
421 .attrs = lm90_attributes,
422};
423
Jean Delvarec3df5802005-10-26 21:39:40 +0200424/* pec used for ADM1032 only */
425static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
426 char *buf)
427{
428 struct i2c_client *client = to_i2c_client(dev);
429 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
430}
431
432static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
433 const char *buf, size_t count)
434{
435 struct i2c_client *client = to_i2c_client(dev);
436 long val = simple_strtol(buf, NULL, 10);
437
438 switch (val) {
439 case 0:
440 client->flags &= ~I2C_CLIENT_PEC;
441 break;
442 case 1:
443 client->flags |= I2C_CLIENT_PEC;
444 break;
445 default:
446 return -EINVAL;
447 }
448
449 return count;
450}
451
452static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
455 * Real code
456 */
457
Jean Delvarec3df5802005-10-26 21:39:40 +0200458/* The ADM1032 supports PEC but not on write byte transactions, so we need
Jean Delvare09664152007-06-09 10:11:15 -0400459 to explicitly ask for a transaction without PEC. */
Jean Delvarec3df5802005-10-26 21:39:40 +0200460static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
461{
462 return i2c_smbus_xfer(client->adapter, client->addr,
463 client->flags & ~I2C_CLIENT_PEC,
464 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
465}
466
467/* It is assumed that client->update_lock is held (unless we are in
468 detection or initialization steps). This matters when PEC is enabled,
469 because we don't want the address pointer to change between the write
470 byte and the read byte transactions. */
Jean Delvare8256fe02005-10-26 21:37:52 +0200471static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
472{
473 int err;
474
Jean Delvarec3df5802005-10-26 21:39:40 +0200475 if (client->flags & I2C_CLIENT_PEC) {
476 err = adm1032_write_byte(client, reg);
477 if (err >= 0)
478 err = i2c_smbus_read_byte(client);
479 } else
480 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200481
482 if (err < 0) {
483 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
484 reg, err);
485 return err;
486 }
487 *value = err;
488
489 return 0;
490}
491
Jean Delvare9b0e8522008-07-16 19:30:15 +0200492/* Return 0 if detection is successful, -ENODEV otherwise */
493static int lm90_detect(struct i2c_client *new_client, int kind,
494 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200496 struct i2c_adapter *adapter = new_client->adapter;
497 int address = new_client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 const char *name = "";
499
500 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200501 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /*
504 * Now we do the remaining detection. A negative kind means that
505 * the driver was loaded with no force parameter (default), so we
506 * must both detect and identify the chip. A zero kind means that
507 * the driver was loaded with the force parameter, the detection
508 * step shall be skipped. A positive kind means that the driver
509 * was loaded with the force parameter and a given kind of chip is
510 * requested, so both the detection and the identification steps
511 * are skipped.
512 */
513
514 /* Default to an LM90 if forced */
515 if (kind == 0)
516 kind = lm90;
517
518 if (kind < 0) { /* detection and identification */
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100519 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100521 if ((man_id = i2c_smbus_read_byte_data(new_client,
522 LM90_REG_R_MAN_ID)) < 0
523 || (chip_id = i2c_smbus_read_byte_data(new_client,
524 LM90_REG_R_CHIP_ID)) < 0
525 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
526 LM90_REG_R_CONFIG1)) < 0
527 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
528 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200529 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400531 if ((address == 0x4C || address == 0x4D)
532 && man_id == 0x01) { /* National Semiconductor */
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100533 int reg_config2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100535 if ((reg_config2 = i2c_smbus_read_byte_data(new_client,
536 LM90_REG_R_CONFIG2)) < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200537 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if ((reg_config1 & 0x2A) == 0x00
540 && (reg_config2 & 0xF8) == 0x00
541 && reg_convrate <= 0x09) {
542 if (address == 0x4C
543 && (chip_id & 0xF0) == 0x20) { /* LM90 */
544 kind = lm90;
545 } else
546 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
547 kind = lm99;
548 } else
549 if (address == 0x4C
550 && (chip_id & 0xF0) == 0x10) { /* LM86 */
551 kind = lm86;
552 }
553 }
554 } else
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400555 if ((address == 0x4C || address == 0x4D)
556 && man_id == 0x41) { /* Analog Devices */
Jean Delvare90209b42005-10-26 22:20:21 +0200557 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 && (reg_config1 & 0x3F) == 0x00
559 && reg_convrate <= 0x0A) {
560 kind = adm1032;
561 } else
Jean Delvare90209b42005-10-26 22:20:21 +0200562 if (chip_id == 0x51 /* ADT7461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 && (reg_config1 & 0x1F) == 0x00 /* check compat mode */
564 && reg_convrate <= 0x0A) {
565 kind = adt7461;
566 }
567 } else
568 if (man_id == 0x4D) { /* Maxim */
569 /*
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400570 * The MAX6657, MAX6658 and MAX6659 do NOT have a
571 * chip_id register. Reading from that address will
572 * return the last read value, which in our case is
573 * those of the man_id register. Likewise, the config1
574 * register seems to lack a low nibble, so the value
575 * will be those of the previous read, so in our case
576 * those of the man_id register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
578 if (chip_id == man_id
Guillaume Chazarainf5744e32007-07-27 01:04:22 +0200579 && (address == 0x4C || address == 0x4D)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 && (reg_config1 & 0x1F) == (man_id & 0x0F)
581 && reg_convrate <= 0x09) {
582 kind = max6657;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400583 } else
584 /* The chip_id register of the MAX6680 and MAX6681
585 * holds the revision of the chip.
586 * the lowest bit of the config1 register is unused
587 * and should return zero when read, so should the
588 * second to last bit of config1 (software reset)
589 */
590 if (chip_id == 0x01
591 && (reg_config1 & 0x03) == 0x00
592 && reg_convrate <= 0x07) {
593 kind = max6680;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 }
596
597 if (kind <= 0) { /* identification failed */
598 dev_info(&adapter->dev,
599 "Unsupported chip (man_id=0x%02X, "
600 "chip_id=0x%02X).\n", man_id, chip_id);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200601 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 }
604
Jean Delvare9b0e8522008-07-16 19:30:15 +0200605 /* Fill the i2c board info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (kind == lm90) {
607 name = "lm90";
608 } else if (kind == adm1032) {
609 name = "adm1032";
Jean Delvarec3df5802005-10-26 21:39:40 +0200610 /* The ADM1032 supports PEC, but only if combined
611 transactions are not used. */
612 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200613 info->flags |= I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 } else if (kind == lm99) {
615 name = "lm99";
616 } else if (kind == lm86) {
617 name = "lm86";
618 } else if (kind == max6657) {
619 name = "max6657";
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400620 } else if (kind == max6680) {
621 name = "max6680";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 } else if (kind == adt7461) {
623 name = "adt7461";
624 }
Jean Delvare9b0e8522008-07-16 19:30:15 +0200625 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jean Delvare9b0e8522008-07-16 19:30:15 +0200627 return 0;
628}
629
630static int lm90_probe(struct i2c_client *new_client,
631 const struct i2c_device_id *id)
632{
633 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
634 struct lm90_data *data;
635 int err;
636
637 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
638 if (!data) {
639 err = -ENOMEM;
640 goto exit;
641 }
642 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100643 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Jean Delvare9b0e8522008-07-16 19:30:15 +0200645 /* Set the device type */
646 data->kind = id->driver_data;
647 if (data->kind == adm1032) {
648 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
649 new_client->flags &= ~I2C_CLIENT_PEC;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /* Initialize the LM90 chip */
653 lm90_init_client(new_client);
654
655 /* Register sysfs hooks */
Jean Delvare0e39e012006-09-24 21:16:40 +0200656 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200657 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200658 if (new_client->flags & I2C_CLIENT_PEC) {
659 if ((err = device_create_file(&new_client->dev,
660 &dev_attr_pec)))
661 goto exit_remove_files;
662 }
Jean Delvare69f2f962007-09-05 14:15:37 +0200663 if (data->kind != max6657) {
664 if ((err = device_create_file(&new_client->dev,
665 &sensor_dev_attr_temp2_offset.dev_attr)))
666 goto exit_remove_files;
667 }
Jean Delvare0e39e012006-09-24 21:16:40 +0200668
Tony Jones1beeffe2007-08-20 13:46:20 -0700669 data->hwmon_dev = hwmon_device_register(&new_client->dev);
670 if (IS_ERR(data->hwmon_dev)) {
671 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200672 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return 0;
676
Jean Delvare0e39e012006-09-24 21:16:40 +0200677exit_remove_files:
678 sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
679 device_remove_file(&new_client->dev, &dev_attr_pec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680exit_free:
681 kfree(data);
682exit:
683 return err;
684}
685
686static void lm90_init_client(struct i2c_client *client)
687{
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400688 u8 config, config_orig;
689 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /*
692 * Start the conversions.
693 */
694 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
695 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +0200696 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
697 dev_warn(&client->dev, "Initialization failed!\n");
698 return;
699 }
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400700 config_orig = config;
701
702 /*
703 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
704 * 0.125 degree resolution) and range (0x08, extend range
705 * to -64 degree) mode for the remote temperature sensor.
706 */
707 if (data->kind == max6680) {
708 config |= 0x18;
709 }
710
711 config &= 0xBF; /* run */
712 if (config != config_orig) /* Only write if changed */
713 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Jean Delvare9b0e8522008-07-16 19:30:15 +0200716static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400718 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Tony Jones1beeffe2007-08-20 13:46:20 -0700720 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200721 sysfs_remove_group(&client->dev.kobj, &lm90_group);
722 device_remove_file(&client->dev, &dev_attr_pec);
Jean Delvare69f2f962007-09-05 14:15:37 +0200723 if (data->kind != max6657)
724 device_remove_file(&client->dev,
725 &sensor_dev_attr_temp2_offset.dev_attr);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400726
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400727 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
729}
730
Jean Delvare6388a382008-10-17 17:51:09 +0200731static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
732{
733 int err;
734 u8 oldh, newh, l;
735
736 /*
737 * There is a trick here. We have to read two registers to have the
738 * sensor temperature, but we have to beware a conversion could occur
739 * inbetween the readings. The datasheet says we should either use
740 * the one-shot conversion register, which we don't want to do
741 * (disables hardware monitoring) or monitor the busy bit, which is
742 * impossible (we can't read the values and monitor that bit at the
743 * exact same time). So the solution used here is to read the high
744 * byte once, then the low byte, then the high byte again. If the new
745 * high byte matches the old one, then we have a valid reading. Else
746 * we have to read the low byte again, and now we believe we have a
747 * correct reading.
748 */
749 if ((err = lm90_read_reg(client, regh, &oldh))
750 || (err = lm90_read_reg(client, regl, &l))
751 || (err = lm90_read_reg(client, regh, &newh)))
752 return err;
753 if (oldh != newh) {
754 err = lm90_read_reg(client, regl, &l);
755 if (err)
756 return err;
757 }
758 *value = (newh << 8) | l;
759
760 return 0;
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static struct lm90_data *lm90_update_device(struct device *dev)
764{
765 struct i2c_client *client = to_i2c_client(dev);
766 struct lm90_data *data = i2c_get_clientdata(client);
767
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100768 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +0200771 u8 h, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +0200774 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
775 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
776 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
777 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +0200778 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Jean Delvaref65e1702008-10-17 17:51:09 +0200780 if (data->kind == max6657) {
781 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
782 MAX6657_REG_R_LOCAL_TEMPL,
783 &data->temp11[4]);
784 } else {
785 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
786 &h) == 0)
787 data->temp11[4] = h << 8;
788 }
Jean Delvare6388a382008-10-17 17:51:09 +0200789 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
790 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Jean Delvare5f502a82008-10-17 17:51:09 +0200792 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
793 data->temp11[1] = h << 8;
794 if (data->kind != max6657 && data->kind != max6680
795 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
796 &l) == 0)
797 data->temp11[1] |= l;
798 }
799 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
800 data->temp11[2] = h << 8;
801 if (data->kind != max6657 && data->kind != max6680
802 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
803 &l) == 0)
804 data->temp11[2] |= l;
805 }
806
Jean Delvare69f2f962007-09-05 14:15:37 +0200807 if (data->kind != max6657) {
808 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +0200809 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +0200810 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
811 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +0200812 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +0200813 }
Jean Delvare8256fe02005-10-26 21:37:52 +0200814 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 data->last_updated = jiffies;
817 data->valid = 1;
818 }
819
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100820 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 return data;
823}
824
825static int __init sensors_lm90_init(void)
826{
827 return i2c_add_driver(&lm90_driver);
828}
829
830static void __exit sensors_lm90_exit(void)
831{
832 i2c_del_driver(&lm90_driver);
833}
834
835MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
836MODULE_DESCRIPTION("LM90/ADM1032 driver");
837MODULE_LICENSE("GPL");
838
839module_init(sensors_lm90_init);
840module_exit(sensors_lm90_exit);