blob: 7c9bdc16742653e0dd2bcb812542f7c1f61a233d [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 Delvare8f2fa772009-12-09 20:35:53 +01004 * Copyright (C) 2003-2009 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
Jean Delvaree5e9f442009-12-14 21:17:27 +010096enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * The LM90 registers
100 */
101
102#define LM90_REG_R_MAN_ID 0xFE
103#define LM90_REG_R_CHIP_ID 0xFF
104#define LM90_REG_R_CONFIG1 0x03
105#define LM90_REG_W_CONFIG1 0x09
106#define LM90_REG_R_CONFIG2 0xBF
107#define LM90_REG_W_CONFIG2 0xBF
108#define LM90_REG_R_CONVRATE 0x04
109#define LM90_REG_W_CONVRATE 0x0A
110#define LM90_REG_R_STATUS 0x02
111#define LM90_REG_R_LOCAL_TEMP 0x00
112#define LM90_REG_R_LOCAL_HIGH 0x05
113#define LM90_REG_W_LOCAL_HIGH 0x0B
114#define LM90_REG_R_LOCAL_LOW 0x06
115#define LM90_REG_W_LOCAL_LOW 0x0C
116#define LM90_REG_R_LOCAL_CRIT 0x20
117#define LM90_REG_W_LOCAL_CRIT 0x20
118#define LM90_REG_R_REMOTE_TEMPH 0x01
119#define LM90_REG_R_REMOTE_TEMPL 0x10
120#define LM90_REG_R_REMOTE_OFFSH 0x11
121#define LM90_REG_W_REMOTE_OFFSH 0x11
122#define LM90_REG_R_REMOTE_OFFSL 0x12
123#define LM90_REG_W_REMOTE_OFFSL 0x12
124#define LM90_REG_R_REMOTE_HIGHH 0x07
125#define LM90_REG_W_REMOTE_HIGHH 0x0D
126#define LM90_REG_R_REMOTE_HIGHL 0x13
127#define LM90_REG_W_REMOTE_HIGHL 0x13
128#define LM90_REG_R_REMOTE_LOWH 0x08
129#define LM90_REG_W_REMOTE_LOWH 0x0E
130#define LM90_REG_R_REMOTE_LOWL 0x14
131#define LM90_REG_W_REMOTE_LOWL 0x14
132#define LM90_REG_R_REMOTE_CRIT 0x19
133#define LM90_REG_W_REMOTE_CRIT 0x19
134#define LM90_REG_R_TCRIT_HYST 0x21
135#define LM90_REG_W_TCRIT_HYST 0x21
136
Ben Hutchings271dabf2008-10-17 17:51:11 +0200137/* MAX6646/6647/6649/6657/6658/6659 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200138
139#define MAX6657_REG_R_LOCAL_TEMPL 0x11
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
Nate Case23b2d472008-10-17 17:51:10 +0200142 * Device flags
143 */
144#define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
145
146/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * Functions declaration
148 */
149
Jean Delvare310ec792009-12-14 21:17:23 +0100150static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200151static int lm90_probe(struct i2c_client *client,
152 const struct i2c_device_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static void lm90_init_client(struct i2c_client *client);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200154static int lm90_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static struct lm90_data *lm90_update_device(struct device *dev);
156
157/*
158 * Driver data (common to all clients)
159 */
160
Jean Delvare9b0e8522008-07-16 19:30:15 +0200161static const struct i2c_device_id lm90_id[] = {
162 { "adm1032", adm1032 },
163 { "adt7461", adt7461 },
164 { "lm90", lm90 },
165 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100166 { "lm89", lm86 },
167 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200168 { "max6646", max6646 },
169 { "max6647", max6646 },
170 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200171 { "max6657", max6657 },
172 { "max6658", max6657 },
173 { "max6659", max6657 },
174 { "max6680", max6680 },
175 { "max6681", max6680 },
176 { }
177};
178MODULE_DEVICE_TABLE(i2c, lm90_id);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static struct i2c_driver lm90_driver = {
Jean Delvare9b0e8522008-07-16 19:30:15 +0200181 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100182 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100183 .name = "lm90",
184 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200185 .probe = lm90_probe,
186 .remove = lm90_remove,
187 .id_table = lm90_id,
188 .detect = lm90_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100189 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};
191
192/*
193 * Client data (each client gets its own)
194 */
195
196struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700197 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100198 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 char valid; /* zero until following fields are valid */
200 unsigned long last_updated; /* in jiffies */
201 int kind;
Nate Case23b2d472008-10-17 17:51:10 +0200202 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* registers values */
Jean Delvaref65e1702008-10-17 17:51:09 +0200205 s8 temp8[4]; /* 0: local low limit
206 1: local high limit
207 2: local critical limit
208 3: remote critical limit */
209 s16 temp11[5]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200210 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200211 2: remote high limit
Ben Hutchings271dabf2008-10-17 17:51:11 +0200212 3: remote offset (except max6646 and max6657)
Jean Delvaref65e1702008-10-17 17:51:09 +0200213 4: local input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 u8 temp_hyst;
215 u8 alarms; /* bitvector */
216};
217
218/*
Nate Casecea50fe2008-10-17 17:51:10 +0200219 * Conversions
220 * For local temperatures and limits, critical limits and the hysteresis
221 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
222 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200223 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
224 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200225 */
226
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200227static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200228{
229 return val * 1000;
230}
231
Ben Hutchings271dabf2008-10-17 17:51:11 +0200232static inline int temp_from_u8(u8 val)
233{
234 return val * 1000;
235}
236
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200237static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200238{
239 return val / 32 * 125;
240}
241
Ben Hutchings271dabf2008-10-17 17:51:11 +0200242static inline int temp_from_u16(u16 val)
243{
244 return val / 32 * 125;
245}
246
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200247static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200248{
249 if (val <= -128000)
250 return -128;
251 if (val >= 127000)
252 return 127;
253 if (val < 0)
254 return (val - 500) / 1000;
255 return (val + 500) / 1000;
256}
257
Ben Hutchings271dabf2008-10-17 17:51:11 +0200258static u8 temp_to_u8(long val)
259{
260 if (val <= 0)
261 return 0;
262 if (val >= 255000)
263 return 255;
264 return (val + 500) / 1000;
265}
266
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200267static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200268{
269 if (val <= -128000)
270 return 0x8000;
271 if (val >= 127875)
272 return 0x7FE0;
273 if (val < 0)
274 return (val - 62) / 125 * 32;
275 return (val + 62) / 125 * 32;
276}
277
278static u8 hyst_to_reg(long val)
279{
280 if (val <= 0)
281 return 0;
282 if (val >= 30500)
283 return 31;
284 return (val + 500) / 1000;
285}
286
287/*
Nate Case23b2d472008-10-17 17:51:10 +0200288 * ADT7461 in compatibility mode is almost identical to LM90 except that
289 * attempts to write values that are outside the range 0 < temp < 127 are
290 * treated as the boundary value.
291 *
292 * ADT7461 in "extended mode" operation uses unsigned integers offset by
293 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200294 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200295static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200296{
Nate Case23b2d472008-10-17 17:51:10 +0200297 if (data->flags & LM90_FLAG_ADT7461_EXT)
298 return (val - 64) * 1000;
299 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200300 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200301}
302
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200303static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200304{
Nate Case23b2d472008-10-17 17:51:10 +0200305 if (data->flags & LM90_FLAG_ADT7461_EXT)
306 return (val - 0x4000) / 64 * 250;
307 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200308 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200309}
310
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200311static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200312{
313 if (data->flags & LM90_FLAG_ADT7461_EXT) {
314 if (val <= -64000)
315 return 0;
316 if (val >= 191000)
317 return 0xFF;
318 return (val + 500 + 64000) / 1000;
319 } else {
320 if (val <= 0)
321 return 0;
322 if (val >= 127000)
323 return 127;
324 return (val + 500) / 1000;
325 }
326}
327
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200328static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200329{
330 if (data->flags & LM90_FLAG_ADT7461_EXT) {
331 if (val <= -64000)
332 return 0;
333 if (val >= 191750)
334 return 0xFFC0;
335 return (val + 64000 + 125) / 250 * 64;
336 } else {
337 if (val <= 0)
338 return 0;
339 if (val >= 127750)
340 return 0x7FC0;
341 return (val + 125) / 250 * 64;
342 }
Nate Casecea50fe2008-10-17 17:51:10 +0200343}
344
345/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * Sysfs stuff
347 */
348
Jean Delvare30d73942005-06-05 21:27:28 +0200349static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
350 char *buf)
351{
352 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
353 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200354 int temp;
355
356 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200357 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200358 else if (data->kind == max6646)
359 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200360 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200361 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200362
Jean Delvare97ae60b2008-10-26 17:04:39 +0100363 /* +16 degrees offset for temp2 for the LM99 */
364 if (data->kind == lm99 && attr->index == 3)
365 temp += 16000;
366
Nate Case23b2d472008-10-17 17:51:10 +0200367 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Jean Delvare30d73942005-06-05 21:27:28 +0200370static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
371 const char *buf, size_t count)
372{
373 static const u8 reg[4] = {
374 LM90_REG_W_LOCAL_LOW,
375 LM90_REG_W_LOCAL_HIGH,
376 LM90_REG_W_LOCAL_CRIT,
377 LM90_REG_W_REMOTE_CRIT,
378 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Jean Delvare30d73942005-06-05 21:27:28 +0200380 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
381 struct i2c_client *client = to_i2c_client(dev);
382 struct lm90_data *data = i2c_get_clientdata(client);
383 long val = simple_strtol(buf, NULL, 10);
384 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Jean Delvare97ae60b2008-10-26 17:04:39 +0100386 /* +16 degrees offset for temp2 for the LM99 */
387 if (data->kind == lm99 && attr->index == 3)
388 val -= 16000;
389
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100390 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200391 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200392 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200393 else if (data->kind == max6646)
394 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200395 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200396 data->temp8[nr] = temp_to_s8(val);
Jean Delvaref65e1702008-10-17 17:51:09 +0200397 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100398 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200399 return count;
400}
401
402static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
403 char *buf)
404{
405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
406 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200407 int temp;
408
409 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200410 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200411 else if (data->kind == max6646)
412 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200413 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200414 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200415
Jean Delvare97ae60b2008-10-26 17:04:39 +0100416 /* +16 degrees offset for temp2 for the LM99 */
417 if (data->kind == lm99 && attr->index <= 2)
418 temp += 16000;
419
Nate Case23b2d472008-10-17 17:51:10 +0200420 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200421}
422
423static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
424 const char *buf, size_t count)
425{
Jean Delvare69f2f962007-09-05 14:15:37 +0200426 static const u8 reg[6] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200427 LM90_REG_W_REMOTE_LOWH,
428 LM90_REG_W_REMOTE_LOWL,
429 LM90_REG_W_REMOTE_HIGHH,
430 LM90_REG_W_REMOTE_HIGHL,
Jean Delvare69f2f962007-09-05 14:15:37 +0200431 LM90_REG_W_REMOTE_OFFSH,
432 LM90_REG_W_REMOTE_OFFSL,
Jean Delvare30d73942005-06-05 21:27:28 +0200433 };
434
435 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
436 struct i2c_client *client = to_i2c_client(dev);
437 struct lm90_data *data = i2c_get_clientdata(client);
438 long val = simple_strtol(buf, NULL, 10);
439 int nr = attr->index;
440
Jean Delvare97ae60b2008-10-26 17:04:39 +0100441 /* +16 degrees offset for temp2 for the LM99 */
442 if (data->kind == lm99 && attr->index <= 2)
443 val -= 16000;
444
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100445 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200446 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200447 data->temp11[nr] = temp_to_u16_adt7461(data, val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200448 else if (data->kind == max6657 || data->kind == max6680)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200449 data->temp11[nr] = temp_to_s8(val) << 8;
Ben Hutchings271dabf2008-10-17 17:51:11 +0200450 else if (data->kind == max6646)
451 data->temp11[nr] = temp_to_u8(val) << 8;
Jean Delvare30d73942005-06-05 21:27:28 +0200452 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200453 data->temp11[nr] = temp_to_s16(val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200454
Jean Delvare30d73942005-06-05 21:27:28 +0200455 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
456 data->temp11[nr] >> 8);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200457 if (data->kind != max6657 && data->kind != max6680
458 && data->kind != max6646)
Jean Delvare5f502a82008-10-17 17:51:09 +0200459 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
460 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100461 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200462 return count;
463}
464
465static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
466 char *buf)
467{
468 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
469 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200470 int temp;
471
472 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200473 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100474 else if (data->kind == max6646)
475 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200476 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200477 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200478
Jean Delvare97ae60b2008-10-26 17:04:39 +0100479 /* +16 degrees offset for temp2 for the LM99 */
480 if (data->kind == lm99 && attr->index == 3)
481 temp += 16000;
482
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200483 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200484}
485
486static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
487 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct i2c_client *client = to_i2c_client(dev);
490 struct lm90_data *data = i2c_get_clientdata(client);
491 long val = simple_strtol(buf, NULL, 10);
Jean Delvareec38fa22008-10-26 17:04:39 +0100492 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100494 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100495 if (data->kind == adt7461)
496 temp = temp_from_u8_adt7461(data, data->temp8[2]);
497 else if (data->kind == max6646)
498 temp = temp_from_u8(data->temp8[2]);
499 else
500 temp = temp_from_s8(data->temp8[2]);
501
502 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100504 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100505 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return count;
507}
508
Jean Delvare30d73942005-06-05 21:27:28 +0200509static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
510 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct lm90_data *data = lm90_update_device(dev);
513 return sprintf(buf, "%d\n", data->alarms);
514}
515
Jean Delvare2d457712006-09-24 20:52:15 +0200516static ssize_t show_alarm(struct device *dev, struct device_attribute
517 *devattr, char *buf)
518{
519 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
520 struct lm90_data *data = lm90_update_device(dev);
521 int bitnr = attr->index;
522
523 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
524}
525
Jean Delvaref65e1702008-10-17 17:51:09 +0200526static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
Jean Delvare30d73942005-06-05 21:27:28 +0200527static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
528static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200529 set_temp8, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200530static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
531 set_temp11, 1);
532static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200533 set_temp8, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200534static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
535 set_temp11, 2);
536static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200537 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200538static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200539 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200540static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200541 set_temphyst, 2);
542static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Jean Delvare69f2f962007-09-05 14:15:37 +0200543static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
544 set_temp11, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200545
546/* Individual alarm files */
547static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
548static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400549static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200550static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
551static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
552static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
553static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
554/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
556
Jean Delvare0e39e012006-09-24 21:16:40 +0200557static struct attribute *lm90_attributes[] = {
558 &sensor_dev_attr_temp1_input.dev_attr.attr,
559 &sensor_dev_attr_temp2_input.dev_attr.attr,
560 &sensor_dev_attr_temp1_min.dev_attr.attr,
561 &sensor_dev_attr_temp2_min.dev_attr.attr,
562 &sensor_dev_attr_temp1_max.dev_attr.attr,
563 &sensor_dev_attr_temp2_max.dev_attr.attr,
564 &sensor_dev_attr_temp1_crit.dev_attr.attr,
565 &sensor_dev_attr_temp2_crit.dev_attr.attr,
566 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
567 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
568
569 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
570 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400571 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200572 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
573 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
574 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
575 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
576 &dev_attr_alarms.attr,
577 NULL
578};
579
580static const struct attribute_group lm90_group = {
581 .attrs = lm90_attributes,
582};
583
Jean Delvarec3df5802005-10-26 21:39:40 +0200584/* pec used for ADM1032 only */
585static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
586 char *buf)
587{
588 struct i2c_client *client = to_i2c_client(dev);
589 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
590}
591
592static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
593 const char *buf, size_t count)
594{
595 struct i2c_client *client = to_i2c_client(dev);
596 long val = simple_strtol(buf, NULL, 10);
597
598 switch (val) {
599 case 0:
600 client->flags &= ~I2C_CLIENT_PEC;
601 break;
602 case 1:
603 client->flags |= I2C_CLIENT_PEC;
604 break;
605 default:
606 return -EINVAL;
607 }
608
609 return count;
610}
611
612static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/*
615 * Real code
616 */
617
Jean Delvarec3df5802005-10-26 21:39:40 +0200618/* The ADM1032 supports PEC but not on write byte transactions, so we need
Jean Delvare09664152007-06-09 10:11:15 -0400619 to explicitly ask for a transaction without PEC. */
Jean Delvarec3df5802005-10-26 21:39:40 +0200620static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
621{
622 return i2c_smbus_xfer(client->adapter, client->addr,
623 client->flags & ~I2C_CLIENT_PEC,
624 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
625}
626
627/* It is assumed that client->update_lock is held (unless we are in
628 detection or initialization steps). This matters when PEC is enabled,
629 because we don't want the address pointer to change between the write
630 byte and the read byte transactions. */
Jean Delvare8256fe02005-10-26 21:37:52 +0200631static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
632{
633 int err;
634
Jean Delvarec3df5802005-10-26 21:39:40 +0200635 if (client->flags & I2C_CLIENT_PEC) {
636 err = adm1032_write_byte(client, reg);
637 if (err >= 0)
638 err = i2c_smbus_read_byte(client);
639 } else
640 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200641
642 if (err < 0) {
643 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
644 reg, err);
645 return err;
646 }
647 *value = err;
648
649 return 0;
650}
651
Jean Delvare9b0e8522008-07-16 19:30:15 +0200652/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100653static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200654 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200656 struct i2c_adapter *adapter = new_client->adapter;
657 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +0100658 const char *name = NULL;
659 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200662 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Jean Delvare8f2fa772009-12-09 20:35:53 +0100664 /* detection and identification */
665 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100666 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100667 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100668 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100669 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100670 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100671 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100672 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100673 return -ENODEV;
674
675 if ((address == 0x4C || address == 0x4D)
676 && man_id == 0x01) { /* National Semiconductor */
677 int reg_config2;
678
679 reg_config2 = i2c_smbus_read_byte_data(new_client,
680 LM90_REG_R_CONFIG2);
681 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200682 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Jean Delvare8f2fa772009-12-09 20:35:53 +0100684 if ((reg_config1 & 0x2A) == 0x00
685 && (reg_config2 & 0xF8) == 0x00
686 && reg_convrate <= 0x09) {
687 if (address == 0x4C
688 && (chip_id & 0xF0) == 0x20) { /* LM90 */
689 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100691 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
692 name = "lm99";
693 dev_info(&adapter->dev,
694 "Assuming LM99 chip at 0x%02x\n",
695 address);
696 dev_info(&adapter->dev,
697 "If it is an LM89, instantiate it "
698 "with the new_device sysfs "
699 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400700 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100701 if (address == 0x4C
702 && (chip_id & 0xF0) == 0x10) { /* LM86 */
703 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100706 } else
707 if ((address == 0x4C || address == 0x4D)
708 && man_id == 0x41) { /* Analog Devices */
709 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
710 && (reg_config1 & 0x3F) == 0x00
711 && reg_convrate <= 0x0A) {
712 name = "adm1032";
713 /* The ADM1032 supports PEC, but only if combined
714 transactions are not used. */
715 if (i2c_check_functionality(adapter,
716 I2C_FUNC_SMBUS_BYTE))
717 info->flags |= I2C_CLIENT_PEC;
718 } else
719 if (chip_id == 0x51 /* ADT7461 */
720 && (reg_config1 & 0x1B) == 0x00
721 && reg_convrate <= 0x0A) {
722 name = "adt7461";
723 }
724 } else
725 if (man_id == 0x4D) { /* Maxim */
726 /*
727 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
728 * register. Reading from that address will return the last
729 * read value, which in our case is those of the man_id
730 * register. Likewise, the config1 register seems to lack a
731 * low nibble, so the value will be those of the previous
732 * read, so in our case those of the man_id register.
733 */
734 if (chip_id == man_id
735 && (address == 0x4C || address == 0x4D)
736 && (reg_config1 & 0x1F) == (man_id & 0x0F)
737 && reg_convrate <= 0x09) {
738 name = "max6657";
739 } else
740 /*
741 * The chip_id register of the MAX6680 and MAX6681 holds the
742 * revision of the chip. The lowest bit of the config1 register
743 * is unused and should return zero when read, so should the
744 * second to last bit of config1 (software reset).
745 */
746 if (chip_id == 0x01
747 && (reg_config1 & 0x03) == 0x00
748 && reg_convrate <= 0x07) {
749 name = "max6680";
750 } else
751 /*
752 * The chip_id register of the MAX6646/6647/6649 holds the
753 * revision of the chip. The lowest 6 bits of the config1
754 * register are unused and should return zero when read.
755 */
756 if (chip_id == 0x59
757 && (reg_config1 & 0x3f) == 0x00
758 && reg_convrate <= 0x07) {
759 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 }
762
Jean Delvare8f2fa772009-12-09 20:35:53 +0100763 if (!name) { /* identification failed */
764 dev_dbg(&adapter->dev,
765 "Unsupported chip at 0x%02x (man_id=0x%02X, "
766 "chip_id=0x%02X)\n", address, man_id, chip_id);
767 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100769
Jean Delvare9b0e8522008-07-16 19:30:15 +0200770 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Jean Delvare9b0e8522008-07-16 19:30:15 +0200772 return 0;
773}
774
775static int lm90_probe(struct i2c_client *new_client,
776 const struct i2c_device_id *id)
777{
778 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
779 struct lm90_data *data;
780 int err;
781
782 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
783 if (!data) {
784 err = -ENOMEM;
785 goto exit;
786 }
787 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100788 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jean Delvare9b0e8522008-07-16 19:30:15 +0200790 /* Set the device type */
791 data->kind = id->driver_data;
792 if (data->kind == adm1032) {
793 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
794 new_client->flags &= ~I2C_CLIENT_PEC;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* Initialize the LM90 chip */
798 lm90_init_client(new_client);
799
800 /* Register sysfs hooks */
Jean Delvare0e39e012006-09-24 21:16:40 +0200801 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200802 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200803 if (new_client->flags & I2C_CLIENT_PEC) {
804 if ((err = device_create_file(&new_client->dev,
805 &dev_attr_pec)))
806 goto exit_remove_files;
807 }
Ben Hutchings271dabf2008-10-17 17:51:11 +0200808 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +0200809 if ((err = device_create_file(&new_client->dev,
810 &sensor_dev_attr_temp2_offset.dev_attr)))
811 goto exit_remove_files;
812 }
Jean Delvare0e39e012006-09-24 21:16:40 +0200813
Tony Jones1beeffe2007-08-20 13:46:20 -0700814 data->hwmon_dev = hwmon_device_register(&new_client->dev);
815 if (IS_ERR(data->hwmon_dev)) {
816 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200817 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400818 }
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return 0;
821
Jean Delvare0e39e012006-09-24 21:16:40 +0200822exit_remove_files:
823 sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
824 device_remove_file(&new_client->dev, &dev_attr_pec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825exit_free:
826 kfree(data);
827exit:
828 return err;
829}
830
831static void lm90_init_client(struct i2c_client *client)
832{
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400833 u8 config, config_orig;
834 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /*
837 * Start the conversions.
838 */
839 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
840 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +0200841 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
842 dev_warn(&client->dev, "Initialization failed!\n");
843 return;
844 }
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400845 config_orig = config;
846
Nate Case23b2d472008-10-17 17:51:10 +0200847 /* Check Temperature Range Select */
848 if (data->kind == adt7461) {
849 if (config & 0x04)
850 data->flags |= LM90_FLAG_ADT7461_EXT;
851 }
852
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400853 /*
854 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
855 * 0.125 degree resolution) and range (0x08, extend range
856 * to -64 degree) mode for the remote temperature sensor.
857 */
858 if (data->kind == max6680) {
859 config |= 0x18;
860 }
861
862 config &= 0xBF; /* run */
863 if (config != config_orig) /* Only write if changed */
864 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Jean Delvare9b0e8522008-07-16 19:30:15 +0200867static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400869 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Tony Jones1beeffe2007-08-20 13:46:20 -0700871 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200872 sysfs_remove_group(&client->dev.kobj, &lm90_group);
873 device_remove_file(&client->dev, &dev_attr_pec);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200874 if (data->kind != max6657 && data->kind != max6646)
Jean Delvare69f2f962007-09-05 14:15:37 +0200875 device_remove_file(&client->dev,
876 &sensor_dev_attr_temp2_offset.dev_attr);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400877
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400878 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
880}
881
Jean Delvare6388a382008-10-17 17:51:09 +0200882static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
883{
884 int err;
885 u8 oldh, newh, l;
886
887 /*
888 * There is a trick here. We have to read two registers to have the
889 * sensor temperature, but we have to beware a conversion could occur
890 * inbetween the readings. The datasheet says we should either use
891 * the one-shot conversion register, which we don't want to do
892 * (disables hardware monitoring) or monitor the busy bit, which is
893 * impossible (we can't read the values and monitor that bit at the
894 * exact same time). So the solution used here is to read the high
895 * byte once, then the low byte, then the high byte again. If the new
896 * high byte matches the old one, then we have a valid reading. Else
897 * we have to read the low byte again, and now we believe we have a
898 * correct reading.
899 */
900 if ((err = lm90_read_reg(client, regh, &oldh))
901 || (err = lm90_read_reg(client, regl, &l))
902 || (err = lm90_read_reg(client, regh, &newh)))
903 return err;
904 if (oldh != newh) {
905 err = lm90_read_reg(client, regl, &l);
906 if (err)
907 return err;
908 }
909 *value = (newh << 8) | l;
910
911 return 0;
912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static struct lm90_data *lm90_update_device(struct device *dev)
915{
916 struct i2c_client *client = to_i2c_client(dev);
917 struct lm90_data *data = i2c_get_clientdata(client);
918
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100919 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +0200922 u8 h, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +0200925 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
926 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
927 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
928 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +0200929 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Ben Hutchings271dabf2008-10-17 17:51:11 +0200931 if (data->kind == max6657 || data->kind == max6646) {
Jean Delvaref65e1702008-10-17 17:51:09 +0200932 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
933 MAX6657_REG_R_LOCAL_TEMPL,
934 &data->temp11[4]);
935 } else {
936 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
937 &h) == 0)
938 data->temp11[4] = h << 8;
939 }
Jean Delvare6388a382008-10-17 17:51:09 +0200940 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
941 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Jean Delvare5f502a82008-10-17 17:51:09 +0200943 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
944 data->temp11[1] = h << 8;
945 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +0200946 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +0200947 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
948 &l) == 0)
949 data->temp11[1] |= l;
950 }
951 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
952 data->temp11[2] = h << 8;
953 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +0200954 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +0200955 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
956 &l) == 0)
957 data->temp11[2] |= l;
958 }
959
Ben Hutchings271dabf2008-10-17 17:51:11 +0200960 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +0200961 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +0200962 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +0200963 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
964 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +0200965 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +0200966 }
Jean Delvare8256fe02005-10-26 21:37:52 +0200967 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 data->last_updated = jiffies;
970 data->valid = 1;
971 }
972
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100973 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 return data;
976}
977
978static int __init sensors_lm90_init(void)
979{
980 return i2c_add_driver(&lm90_driver);
981}
982
983static void __exit sensors_lm90_exit(void)
984{
985 i2c_del_driver(&lm90_driver);
986}
987
988MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
989MODULE_DESCRIPTION("LM90/ADM1032 driver");
990MODULE_LICENSE("GPL");
991
992module_init(sensors_lm90_init);
993module_exit(sensors_lm90_exit);