blob: 1aff7575799d57d2ad5837c54c46bd8d5a65fd1a [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
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
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 *
Darrick J. Wong1a51e062009-03-12 13:36:38 +010035 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
36 * MAX6692 chips made by Maxim. These are again similar to the LM86,
37 * but they use unsigned temperature values and can report temperatures
38 * from 0 to 145 degrees.
Ben Hutchings271dabf2008-10-17 17:51:11 +020039 *
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040040 * This driver also supports the MAX6680 and MAX6681, two other sensor
41 * chips made by Maxim. These are quite similar to the other Maxim
Jean Delvarea874a102008-10-17 17:51:10 +020042 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
43 * be treated identically.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040044 *
Nate Case23b2d472008-10-17 17:51:10 +020045 * This driver also supports the ADT7461 chip from Analog Devices.
46 * It's supported in both compatibility and extended mode. It is mostly
47 * compatible with LM90 except for a data format difference for the
48 * temperature value registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 *
50 * Since the LM90 was the first chipset supported by this driver, most
51 * comments will refer to this chipset, but are actually general and
52 * concern all supported chipsets, unless mentioned otherwise.
53 *
54 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
56 * the Free Software Foundation; either version 2 of the License, or
57 * (at your option) any later version.
58 *
59 * This program is distributed in the hope that it will be useful,
60 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62 * GNU General Public License for more details.
63 *
64 * You should have received a copy of the GNU General Public License
65 * along with this program; if not, write to the Free Software
66 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
67 */
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/module.h>
70#include <linux/init.h>
71#include <linux/slab.h>
72#include <linux/jiffies.h>
73#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020074#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040075#include <linux/hwmon.h>
76#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010077#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020078#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * Addresses to scan
82 * Address is fully defined internally and cannot be changed except for
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040083 * MAX6659, MAX6680 and MAX6681.
Ben Hutchings271dabf2008-10-17 17:51:11 +020084 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
85 * and MAX6658 have address 0x4c.
86 * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
87 * MAX6647 has address 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040089 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
90 * 0x4c, 0x4d or 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050093static const unsigned short normal_i2c[] = {
94 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
97 * Insmod parameters
98 */
99
Ben Hutchings271dabf2008-10-17 17:51:11 +0200100I2C_CLIENT_INSMOD_8(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680,
101 max6646);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
104 * The LM90 registers
105 */
106
107#define LM90_REG_R_MAN_ID 0xFE
108#define LM90_REG_R_CHIP_ID 0xFF
109#define LM90_REG_R_CONFIG1 0x03
110#define LM90_REG_W_CONFIG1 0x09
111#define LM90_REG_R_CONFIG2 0xBF
112#define LM90_REG_W_CONFIG2 0xBF
113#define LM90_REG_R_CONVRATE 0x04
114#define LM90_REG_W_CONVRATE 0x0A
115#define LM90_REG_R_STATUS 0x02
116#define LM90_REG_R_LOCAL_TEMP 0x00
117#define LM90_REG_R_LOCAL_HIGH 0x05
118#define LM90_REG_W_LOCAL_HIGH 0x0B
119#define LM90_REG_R_LOCAL_LOW 0x06
120#define LM90_REG_W_LOCAL_LOW 0x0C
121#define LM90_REG_R_LOCAL_CRIT 0x20
122#define LM90_REG_W_LOCAL_CRIT 0x20
123#define LM90_REG_R_REMOTE_TEMPH 0x01
124#define LM90_REG_R_REMOTE_TEMPL 0x10
125#define LM90_REG_R_REMOTE_OFFSH 0x11
126#define LM90_REG_W_REMOTE_OFFSH 0x11
127#define LM90_REG_R_REMOTE_OFFSL 0x12
128#define LM90_REG_W_REMOTE_OFFSL 0x12
129#define LM90_REG_R_REMOTE_HIGHH 0x07
130#define LM90_REG_W_REMOTE_HIGHH 0x0D
131#define LM90_REG_R_REMOTE_HIGHL 0x13
132#define LM90_REG_W_REMOTE_HIGHL 0x13
133#define LM90_REG_R_REMOTE_LOWH 0x08
134#define LM90_REG_W_REMOTE_LOWH 0x0E
135#define LM90_REG_R_REMOTE_LOWL 0x14
136#define LM90_REG_W_REMOTE_LOWL 0x14
137#define LM90_REG_R_REMOTE_CRIT 0x19
138#define LM90_REG_W_REMOTE_CRIT 0x19
139#define LM90_REG_R_TCRIT_HYST 0x21
140#define LM90_REG_W_TCRIT_HYST 0x21
141
Ben Hutchings271dabf2008-10-17 17:51:11 +0200142/* MAX6646/6647/6649/6657/6658/6659 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200143
144#define MAX6657_REG_R_LOCAL_TEMPL 0x11
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
Nate Case23b2d472008-10-17 17:51:10 +0200147 * Device flags
148 */
149#define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
150
151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Functions declaration
153 */
154
Jean Delvare9b0e8522008-07-16 19:30:15 +0200155static int lm90_detect(struct i2c_client *client, int kind,
156 struct i2c_board_info *info);
157static int lm90_probe(struct i2c_client *client,
158 const struct i2c_device_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void lm90_init_client(struct i2c_client *client);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200160static int lm90_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static struct lm90_data *lm90_update_device(struct device *dev);
162
163/*
164 * Driver data (common to all clients)
165 */
166
Jean Delvare9b0e8522008-07-16 19:30:15 +0200167static const struct i2c_device_id lm90_id[] = {
168 { "adm1032", adm1032 },
169 { "adt7461", adt7461 },
170 { "lm90", lm90 },
171 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100172 { "lm89", lm86 },
173 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200174 { "max6646", max6646 },
175 { "max6647", max6646 },
176 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200177 { "max6657", max6657 },
178 { "max6658", max6657 },
179 { "max6659", max6657 },
180 { "max6680", max6680 },
181 { "max6681", max6680 },
182 { }
183};
184MODULE_DEVICE_TABLE(i2c, lm90_id);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static struct i2c_driver lm90_driver = {
Jean Delvare9b0e8522008-07-16 19:30:15 +0200187 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100188 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100189 .name = "lm90",
190 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200191 .probe = lm90_probe,
192 .remove = lm90_remove,
193 .id_table = lm90_id,
194 .detect = lm90_detect,
195 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198/*
199 * Client data (each client gets its own)
200 */
201
202struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700203 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100204 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 char valid; /* zero until following fields are valid */
206 unsigned long last_updated; /* in jiffies */
207 int kind;
Nate Case23b2d472008-10-17 17:51:10 +0200208 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* registers values */
Jean Delvaref65e1702008-10-17 17:51:09 +0200211 s8 temp8[4]; /* 0: local low limit
212 1: local high limit
213 2: local critical limit
214 3: remote critical limit */
215 s16 temp11[5]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200216 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200217 2: remote high limit
Ben Hutchings271dabf2008-10-17 17:51:11 +0200218 3: remote offset (except max6646 and max6657)
Jean Delvaref65e1702008-10-17 17:51:09 +0200219 4: local input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 u8 temp_hyst;
221 u8 alarms; /* bitvector */
222};
223
224/*
Nate Casecea50fe2008-10-17 17:51:10 +0200225 * Conversions
226 * For local temperatures and limits, critical limits and the hysteresis
227 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
228 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200229 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
230 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200231 */
232
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200233static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200234{
235 return val * 1000;
236}
237
Ben Hutchings271dabf2008-10-17 17:51:11 +0200238static inline int temp_from_u8(u8 val)
239{
240 return val * 1000;
241}
242
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200243static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200244{
245 return val / 32 * 125;
246}
247
Ben Hutchings271dabf2008-10-17 17:51:11 +0200248static inline int temp_from_u16(u16 val)
249{
250 return val / 32 * 125;
251}
252
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200253static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200254{
255 if (val <= -128000)
256 return -128;
257 if (val >= 127000)
258 return 127;
259 if (val < 0)
260 return (val - 500) / 1000;
261 return (val + 500) / 1000;
262}
263
Ben Hutchings271dabf2008-10-17 17:51:11 +0200264static u8 temp_to_u8(long val)
265{
266 if (val <= 0)
267 return 0;
268 if (val >= 255000)
269 return 255;
270 return (val + 500) / 1000;
271}
272
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200273static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200274{
275 if (val <= -128000)
276 return 0x8000;
277 if (val >= 127875)
278 return 0x7FE0;
279 if (val < 0)
280 return (val - 62) / 125 * 32;
281 return (val + 62) / 125 * 32;
282}
283
284static u8 hyst_to_reg(long val)
285{
286 if (val <= 0)
287 return 0;
288 if (val >= 30500)
289 return 31;
290 return (val + 500) / 1000;
291}
292
293/*
Nate Case23b2d472008-10-17 17:51:10 +0200294 * ADT7461 in compatibility mode is almost identical to LM90 except that
295 * attempts to write values that are outside the range 0 < temp < 127 are
296 * treated as the boundary value.
297 *
298 * ADT7461 in "extended mode" operation uses unsigned integers offset by
299 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200300 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200301static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200302{
Nate Case23b2d472008-10-17 17:51:10 +0200303 if (data->flags & LM90_FLAG_ADT7461_EXT)
304 return (val - 64) * 1000;
305 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200306 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200307}
308
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200309static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200310{
Nate Case23b2d472008-10-17 17:51:10 +0200311 if (data->flags & LM90_FLAG_ADT7461_EXT)
312 return (val - 0x4000) / 64 * 250;
313 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200314 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200315}
316
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200317static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200318{
319 if (data->flags & LM90_FLAG_ADT7461_EXT) {
320 if (val <= -64000)
321 return 0;
322 if (val >= 191000)
323 return 0xFF;
324 return (val + 500 + 64000) / 1000;
325 } else {
326 if (val <= 0)
327 return 0;
328 if (val >= 127000)
329 return 127;
330 return (val + 500) / 1000;
331 }
332}
333
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200334static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200335{
336 if (data->flags & LM90_FLAG_ADT7461_EXT) {
337 if (val <= -64000)
338 return 0;
339 if (val >= 191750)
340 return 0xFFC0;
341 return (val + 64000 + 125) / 250 * 64;
342 } else {
343 if (val <= 0)
344 return 0;
345 if (val >= 127750)
346 return 0x7FC0;
347 return (val + 125) / 250 * 64;
348 }
Nate Casecea50fe2008-10-17 17:51:10 +0200349}
350
351/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * Sysfs stuff
353 */
354
Jean Delvare30d73942005-06-05 21:27:28 +0200355static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
356 char *buf)
357{
358 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
359 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200360 int temp;
361
362 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200363 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200364 else if (data->kind == max6646)
365 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200366 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200367 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200368
Jean Delvare97ae60b2008-10-26 17:04:39 +0100369 /* +16 degrees offset for temp2 for the LM99 */
370 if (data->kind == lm99 && attr->index == 3)
371 temp += 16000;
372
Nate Case23b2d472008-10-17 17:51:10 +0200373 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jean Delvare30d73942005-06-05 21:27:28 +0200376static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
377 const char *buf, size_t count)
378{
379 static const u8 reg[4] = {
380 LM90_REG_W_LOCAL_LOW,
381 LM90_REG_W_LOCAL_HIGH,
382 LM90_REG_W_LOCAL_CRIT,
383 LM90_REG_W_REMOTE_CRIT,
384 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Jean Delvare30d73942005-06-05 21:27:28 +0200386 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
387 struct i2c_client *client = to_i2c_client(dev);
388 struct lm90_data *data = i2c_get_clientdata(client);
389 long val = simple_strtol(buf, NULL, 10);
390 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Jean Delvare97ae60b2008-10-26 17:04:39 +0100392 /* +16 degrees offset for temp2 for the LM99 */
393 if (data->kind == lm99 && attr->index == 3)
394 val -= 16000;
395
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200397 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200398 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200399 else if (data->kind == max6646)
400 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200401 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200402 data->temp8[nr] = temp_to_s8(val);
Jean Delvaref65e1702008-10-17 17:51:09 +0200403 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100404 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200405 return count;
406}
407
408static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
409 char *buf)
410{
411 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
412 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200413 int temp;
414
415 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200416 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200417 else if (data->kind == max6646)
418 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200419 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200420 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200421
Jean Delvare97ae60b2008-10-26 17:04:39 +0100422 /* +16 degrees offset for temp2 for the LM99 */
423 if (data->kind == lm99 && attr->index <= 2)
424 temp += 16000;
425
Nate Case23b2d472008-10-17 17:51:10 +0200426 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200427}
428
429static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
430 const char *buf, size_t count)
431{
Jean Delvare69f2f962007-09-05 14:15:37 +0200432 static const u8 reg[6] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200433 LM90_REG_W_REMOTE_LOWH,
434 LM90_REG_W_REMOTE_LOWL,
435 LM90_REG_W_REMOTE_HIGHH,
436 LM90_REG_W_REMOTE_HIGHL,
Jean Delvare69f2f962007-09-05 14:15:37 +0200437 LM90_REG_W_REMOTE_OFFSH,
438 LM90_REG_W_REMOTE_OFFSL,
Jean Delvare30d73942005-06-05 21:27:28 +0200439 };
440
441 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
442 struct i2c_client *client = to_i2c_client(dev);
443 struct lm90_data *data = i2c_get_clientdata(client);
444 long val = simple_strtol(buf, NULL, 10);
445 int nr = attr->index;
446
Jean Delvare97ae60b2008-10-26 17:04:39 +0100447 /* +16 degrees offset for temp2 for the LM99 */
448 if (data->kind == lm99 && attr->index <= 2)
449 val -= 16000;
450
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100451 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200452 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200453 data->temp11[nr] = temp_to_u16_adt7461(data, val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200454 else if (data->kind == max6657 || data->kind == max6680)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200455 data->temp11[nr] = temp_to_s8(val) << 8;
Ben Hutchings271dabf2008-10-17 17:51:11 +0200456 else if (data->kind == max6646)
457 data->temp11[nr] = temp_to_u8(val) << 8;
Jean Delvare30d73942005-06-05 21:27:28 +0200458 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200459 data->temp11[nr] = temp_to_s16(val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200460
Jean Delvare30d73942005-06-05 21:27:28 +0200461 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
462 data->temp11[nr] >> 8);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200463 if (data->kind != max6657 && data->kind != max6680
464 && data->kind != max6646)
Jean Delvare5f502a82008-10-17 17:51:09 +0200465 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
466 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100467 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200468 return count;
469}
470
471static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
472 char *buf)
473{
474 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
475 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200476 int temp;
477
478 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200479 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100480 else if (data->kind == max6646)
481 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200482 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200483 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200484
Jean Delvare97ae60b2008-10-26 17:04:39 +0100485 /* +16 degrees offset for temp2 for the LM99 */
486 if (data->kind == lm99 && attr->index == 3)
487 temp += 16000;
488
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200489 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200490}
491
492static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
493 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct i2c_client *client = to_i2c_client(dev);
496 struct lm90_data *data = i2c_get_clientdata(client);
497 long val = simple_strtol(buf, NULL, 10);
Jean Delvareec38fa22008-10-26 17:04:39 +0100498 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100500 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100501 if (data->kind == adt7461)
502 temp = temp_from_u8_adt7461(data, data->temp8[2]);
503 else if (data->kind == max6646)
504 temp = temp_from_u8(data->temp8[2]);
505 else
506 temp = temp_from_s8(data->temp8[2]);
507
508 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100510 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100511 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return count;
513}
514
Jean Delvare30d73942005-06-05 21:27:28 +0200515static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
516 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct lm90_data *data = lm90_update_device(dev);
519 return sprintf(buf, "%d\n", data->alarms);
520}
521
Jean Delvare2d457712006-09-24 20:52:15 +0200522static ssize_t show_alarm(struct device *dev, struct device_attribute
523 *devattr, char *buf)
524{
525 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
526 struct lm90_data *data = lm90_update_device(dev);
527 int bitnr = attr->index;
528
529 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
530}
531
Jean Delvaref65e1702008-10-17 17:51:09 +0200532static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
Jean Delvare30d73942005-06-05 21:27:28 +0200533static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
534static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200535 set_temp8, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200536static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
537 set_temp11, 1);
538static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200539 set_temp8, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200540static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
541 set_temp11, 2);
542static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200543 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200544static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200545 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200546static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200547 set_temphyst, 2);
548static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Jean Delvare69f2f962007-09-05 14:15:37 +0200549static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
550 set_temp11, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200551
552/* Individual alarm files */
553static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
554static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400555static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200556static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
557static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
558static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
559static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
560/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
562
Jean Delvare0e39e012006-09-24 21:16:40 +0200563static struct attribute *lm90_attributes[] = {
564 &sensor_dev_attr_temp1_input.dev_attr.attr,
565 &sensor_dev_attr_temp2_input.dev_attr.attr,
566 &sensor_dev_attr_temp1_min.dev_attr.attr,
567 &sensor_dev_attr_temp2_min.dev_attr.attr,
568 &sensor_dev_attr_temp1_max.dev_attr.attr,
569 &sensor_dev_attr_temp2_max.dev_attr.attr,
570 &sensor_dev_attr_temp1_crit.dev_attr.attr,
571 &sensor_dev_attr_temp2_crit.dev_attr.attr,
572 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
573 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
574
575 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
576 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400577 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200578 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
579 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
580 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
581 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
582 &dev_attr_alarms.attr,
583 NULL
584};
585
586static const struct attribute_group lm90_group = {
587 .attrs = lm90_attributes,
588};
589
Jean Delvarec3df5802005-10-26 21:39:40 +0200590/* pec used for ADM1032 only */
591static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
592 char *buf)
593{
594 struct i2c_client *client = to_i2c_client(dev);
595 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
596}
597
598static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
599 const char *buf, size_t count)
600{
601 struct i2c_client *client = to_i2c_client(dev);
602 long val = simple_strtol(buf, NULL, 10);
603
604 switch (val) {
605 case 0:
606 client->flags &= ~I2C_CLIENT_PEC;
607 break;
608 case 1:
609 client->flags |= I2C_CLIENT_PEC;
610 break;
611 default:
612 return -EINVAL;
613 }
614
615 return count;
616}
617
618static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * Real code
622 */
623
Jean Delvarec3df5802005-10-26 21:39:40 +0200624/* The ADM1032 supports PEC but not on write byte transactions, so we need
Jean Delvare09664152007-06-09 10:11:15 -0400625 to explicitly ask for a transaction without PEC. */
Jean Delvarec3df5802005-10-26 21:39:40 +0200626static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
627{
628 return i2c_smbus_xfer(client->adapter, client->addr,
629 client->flags & ~I2C_CLIENT_PEC,
630 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
631}
632
633/* It is assumed that client->update_lock is held (unless we are in
634 detection or initialization steps). This matters when PEC is enabled,
635 because we don't want the address pointer to change between the write
636 byte and the read byte transactions. */
Jean Delvare8256fe02005-10-26 21:37:52 +0200637static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
638{
639 int err;
640
Jean Delvarec3df5802005-10-26 21:39:40 +0200641 if (client->flags & I2C_CLIENT_PEC) {
642 err = adm1032_write_byte(client, reg);
643 if (err >= 0)
644 err = i2c_smbus_read_byte(client);
645 } else
646 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200647
648 if (err < 0) {
649 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
650 reg, err);
651 return err;
652 }
653 *value = err;
654
655 return 0;
656}
657
Jean Delvare9b0e8522008-07-16 19:30:15 +0200658/* Return 0 if detection is successful, -ENODEV otherwise */
659static int lm90_detect(struct i2c_client *new_client, int kind,
660 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200662 struct i2c_adapter *adapter = new_client->adapter;
663 int address = new_client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 const char *name = "";
665
666 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200667 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /*
670 * Now we do the remaining detection. A negative kind means that
671 * the driver was loaded with no force parameter (default), so we
672 * must both detect and identify the chip. A zero kind means that
673 * the driver was loaded with the force parameter, the detection
674 * step shall be skipped. A positive kind means that the driver
675 * was loaded with the force parameter and a given kind of chip is
676 * requested, so both the detection and the identification steps
677 * are skipped.
678 */
679
680 /* Default to an LM90 if forced */
681 if (kind == 0)
682 kind = lm90;
683
684 if (kind < 0) { /* detection and identification */
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100685 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100687 if ((man_id = i2c_smbus_read_byte_data(new_client,
688 LM90_REG_R_MAN_ID)) < 0
689 || (chip_id = i2c_smbus_read_byte_data(new_client,
690 LM90_REG_R_CHIP_ID)) < 0
691 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
692 LM90_REG_R_CONFIG1)) < 0
693 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
694 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200695 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400697 if ((address == 0x4C || address == 0x4D)
698 && man_id == 0x01) { /* National Semiconductor */
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100699 int reg_config2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100701 if ((reg_config2 = i2c_smbus_read_byte_data(new_client,
702 LM90_REG_R_CONFIG2)) < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200703 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if ((reg_config1 & 0x2A) == 0x00
706 && (reg_config2 & 0xF8) == 0x00
707 && reg_convrate <= 0x09) {
708 if (address == 0x4C
709 && (chip_id & 0xF0) == 0x20) { /* LM90 */
710 kind = lm90;
711 } else
712 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
713 kind = lm99;
Jean Delvare97ae60b2008-10-26 17:04:39 +0100714 dev_info(&adapter->dev,
715 "Assuming LM99 chip at "
716 "0x%02x\n", address);
717 dev_info(&adapter->dev,
718 "If it is an LM89, pass "
719 "force_lm86=%d,0x%02x when "
720 "loading the lm90 driver\n",
721 i2c_adapter_id(adapter),
722 address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 } else
724 if (address == 0x4C
725 && (chip_id & 0xF0) == 0x10) { /* LM86 */
726 kind = lm86;
727 }
728 }
729 } else
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400730 if ((address == 0x4C || address == 0x4D)
731 && man_id == 0x41) { /* Analog Devices */
Jean Delvare90209b42005-10-26 22:20:21 +0200732 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 && (reg_config1 & 0x3F) == 0x00
734 && reg_convrate <= 0x0A) {
735 kind = adm1032;
736 } else
Jean Delvare90209b42005-10-26 22:20:21 +0200737 if (chip_id == 0x51 /* ADT7461 */
Nate Case23b2d472008-10-17 17:51:10 +0200738 && (reg_config1 & 0x1B) == 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 && reg_convrate <= 0x0A) {
740 kind = adt7461;
741 }
742 } else
743 if (man_id == 0x4D) { /* Maxim */
744 /*
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400745 * The MAX6657, MAX6658 and MAX6659 do NOT have a
746 * chip_id register. Reading from that address will
747 * return the last read value, which in our case is
748 * those of the man_id register. Likewise, the config1
749 * register seems to lack a low nibble, so the value
750 * will be those of the previous read, so in our case
751 * those of the man_id register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
753 if (chip_id == man_id
Guillaume Chazarainf5744e32007-07-27 01:04:22 +0200754 && (address == 0x4C || address == 0x4D)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 && (reg_config1 & 0x1F) == (man_id & 0x0F)
756 && reg_convrate <= 0x09) {
757 kind = max6657;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400758 } else
759 /* The chip_id register of the MAX6680 and MAX6681
760 * holds the revision of the chip.
761 * the lowest bit of the config1 register is unused
762 * and should return zero when read, so should the
763 * second to last bit of config1 (software reset)
764 */
765 if (chip_id == 0x01
766 && (reg_config1 & 0x03) == 0x00
767 && reg_convrate <= 0x07) {
768 kind = max6680;
Ben Hutchings271dabf2008-10-17 17:51:11 +0200769 } else
770 /* The chip_id register of the MAX6646/6647/6649
771 * holds the revision of the chip.
772 * The lowest 6 bits of the config1 register are
773 * unused and should return zero when read.
774 */
775 if (chip_id == 0x59
776 && (reg_config1 & 0x3f) == 0x00
777 && reg_convrate <= 0x07) {
778 kind = max6646;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 }
781
782 if (kind <= 0) { /* identification failed */
Jean Delvare4b4e7a72008-10-17 17:51:11 +0200783 dev_dbg(&adapter->dev,
784 "Unsupported chip at 0x%02x (man_id=0x%02X, "
785 "chip_id=0x%02X)\n", address, man_id, chip_id);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200786 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 }
789
Jean Delvare9b0e8522008-07-16 19:30:15 +0200790 /* Fill the i2c board info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (kind == lm90) {
792 name = "lm90";
793 } else if (kind == adm1032) {
794 name = "adm1032";
Jean Delvarec3df5802005-10-26 21:39:40 +0200795 /* The ADM1032 supports PEC, but only if combined
796 transactions are not used. */
797 if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200798 info->flags |= I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 } else if (kind == lm99) {
800 name = "lm99";
801 } else if (kind == lm86) {
802 name = "lm86";
803 } else if (kind == max6657) {
804 name = "max6657";
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400805 } else if (kind == max6680) {
806 name = "max6680";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 } else if (kind == adt7461) {
808 name = "adt7461";
Ben Hutchings271dabf2008-10-17 17:51:11 +0200809 } else if (kind == max6646) {
810 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Jean Delvare9b0e8522008-07-16 19:30:15 +0200812 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jean Delvare9b0e8522008-07-16 19:30:15 +0200814 return 0;
815}
816
817static int lm90_probe(struct i2c_client *new_client,
818 const struct i2c_device_id *id)
819{
820 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
821 struct lm90_data *data;
822 int err;
823
824 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
825 if (!data) {
826 err = -ENOMEM;
827 goto exit;
828 }
829 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100830 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jean Delvare9b0e8522008-07-16 19:30:15 +0200832 /* Set the device type */
833 data->kind = id->driver_data;
834 if (data->kind == adm1032) {
835 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
836 new_client->flags &= ~I2C_CLIENT_PEC;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* Initialize the LM90 chip */
840 lm90_init_client(new_client);
841
842 /* Register sysfs hooks */
Jean Delvare0e39e012006-09-24 21:16:40 +0200843 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200844 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200845 if (new_client->flags & I2C_CLIENT_PEC) {
846 if ((err = device_create_file(&new_client->dev,
847 &dev_attr_pec)))
848 goto exit_remove_files;
849 }
Ben Hutchings271dabf2008-10-17 17:51:11 +0200850 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +0200851 if ((err = device_create_file(&new_client->dev,
852 &sensor_dev_attr_temp2_offset.dev_attr)))
853 goto exit_remove_files;
854 }
Jean Delvare0e39e012006-09-24 21:16:40 +0200855
Tony Jones1beeffe2007-08-20 13:46:20 -0700856 data->hwmon_dev = hwmon_device_register(&new_client->dev);
857 if (IS_ERR(data->hwmon_dev)) {
858 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200859 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 0;
863
Jean Delvare0e39e012006-09-24 21:16:40 +0200864exit_remove_files:
865 sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
866 device_remove_file(&new_client->dev, &dev_attr_pec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867exit_free:
868 kfree(data);
869exit:
870 return err;
871}
872
873static void lm90_init_client(struct i2c_client *client)
874{
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400875 u8 config, config_orig;
876 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /*
879 * Start the conversions.
880 */
881 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
882 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +0200883 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
884 dev_warn(&client->dev, "Initialization failed!\n");
885 return;
886 }
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400887 config_orig = config;
888
Nate Case23b2d472008-10-17 17:51:10 +0200889 /* Check Temperature Range Select */
890 if (data->kind == adt7461) {
891 if (config & 0x04)
892 data->flags |= LM90_FLAG_ADT7461_EXT;
893 }
894
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400895 /*
896 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
897 * 0.125 degree resolution) and range (0x08, extend range
898 * to -64 degree) mode for the remote temperature sensor.
899 */
900 if (data->kind == max6680) {
901 config |= 0x18;
902 }
903
904 config &= 0xBF; /* run */
905 if (config != config_orig) /* Only write if changed */
906 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Jean Delvare9b0e8522008-07-16 19:30:15 +0200909static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400911 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Tony Jones1beeffe2007-08-20 13:46:20 -0700913 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200914 sysfs_remove_group(&client->dev.kobj, &lm90_group);
915 device_remove_file(&client->dev, &dev_attr_pec);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200916 if (data->kind != max6657 && data->kind != max6646)
Jean Delvare69f2f962007-09-05 14:15:37 +0200917 device_remove_file(&client->dev,
918 &sensor_dev_attr_temp2_offset.dev_attr);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400919
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400920 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
923
Jean Delvare6388a382008-10-17 17:51:09 +0200924static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
925{
926 int err;
927 u8 oldh, newh, l;
928
929 /*
930 * There is a trick here. We have to read two registers to have the
931 * sensor temperature, but we have to beware a conversion could occur
932 * inbetween the readings. The datasheet says we should either use
933 * the one-shot conversion register, which we don't want to do
934 * (disables hardware monitoring) or monitor the busy bit, which is
935 * impossible (we can't read the values and monitor that bit at the
936 * exact same time). So the solution used here is to read the high
937 * byte once, then the low byte, then the high byte again. If the new
938 * high byte matches the old one, then we have a valid reading. Else
939 * we have to read the low byte again, and now we believe we have a
940 * correct reading.
941 */
942 if ((err = lm90_read_reg(client, regh, &oldh))
943 || (err = lm90_read_reg(client, regl, &l))
944 || (err = lm90_read_reg(client, regh, &newh)))
945 return err;
946 if (oldh != newh) {
947 err = lm90_read_reg(client, regl, &l);
948 if (err)
949 return err;
950 }
951 *value = (newh << 8) | l;
952
953 return 0;
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956static struct lm90_data *lm90_update_device(struct device *dev)
957{
958 struct i2c_client *client = to_i2c_client(dev);
959 struct lm90_data *data = i2c_get_clientdata(client);
960
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100961 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +0200964 u8 h, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +0200967 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
968 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
969 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
970 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +0200971 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Ben Hutchings271dabf2008-10-17 17:51:11 +0200973 if (data->kind == max6657 || data->kind == max6646) {
Jean Delvaref65e1702008-10-17 17:51:09 +0200974 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
975 MAX6657_REG_R_LOCAL_TEMPL,
976 &data->temp11[4]);
977 } else {
978 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
979 &h) == 0)
980 data->temp11[4] = h << 8;
981 }
Jean Delvare6388a382008-10-17 17:51:09 +0200982 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
983 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jean Delvare5f502a82008-10-17 17:51:09 +0200985 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
986 data->temp11[1] = h << 8;
987 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +0200988 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +0200989 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
990 &l) == 0)
991 data->temp11[1] |= l;
992 }
993 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
994 data->temp11[2] = h << 8;
995 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +0200996 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +0200997 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
998 &l) == 0)
999 data->temp11[2] |= l;
1000 }
1001
Ben Hutchings271dabf2008-10-17 17:51:11 +02001002 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +02001003 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +02001004 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +02001005 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
1006 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +02001007 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +02001008 }
Jean Delvare8256fe02005-10-26 21:37:52 +02001009 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 data->last_updated = jiffies;
1012 data->valid = 1;
1013 }
1014
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001015 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 return data;
1018}
1019
1020static int __init sensors_lm90_init(void)
1021{
1022 return i2c_add_driver(&lm90_driver);
1023}
1024
1025static void __exit sensors_lm90_exit(void)
1026{
1027 i2c_del_driver(&lm90_driver);
1028}
1029
1030MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1031MODULE_DESCRIPTION("LM90/ADM1032 driver");
1032MODULE_LICENSE("GPL");
1033
1034module_init(sensors_lm90_init);
1035module_exit(sensors_lm90_exit);