blob: 4cbbf1563de666d187cd47c125dfda371466730a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
Jean Delvare95238362010-03-05 22:17:14 +01004 * Copyright (C) 2003-2010 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
7 * Semiconductor. It reports up to two temperatures (its own plus up to
8 * one external one) with a 0.125 deg resolution (1 deg for local
Jean Delvarea874a102008-10-17 17:51:10 +02009 * temperature) and a 3-4 deg accuracy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This driver also supports the LM89 and LM99, two other sensor chips
12 * made by National Semiconductor. Both have an increased remote
13 * temperature measurement accuracy (1 degree), and the LM99
14 * additionally shifts remote temperatures (measured and limits) by 16
Jean Delvare97ae60b2008-10-26 17:04:39 +010015 * degrees, which allows for higher temperatures measurement.
Steven Cole44bbe872005-05-03 18:21:25 -060016 * Note that there is no way to differentiate between both chips.
Jean Delvare97ae60b2008-10-26 17:04:39 +010017 * When device is auto-detected, the driver will assume an LM99.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * This driver also supports the LM86, another sensor chip made by
20 * National Semiconductor. It is exactly similar to the LM90 except it
21 * has a higher accuracy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
23 * This driver also supports the ADM1032, a sensor chip made by Analog
24 * Devices. That chip is similar to the LM90, with a few differences
Jean Delvarea874a102008-10-17 17:51:10 +020025 * that are not handled by this driver. Among others, it has a higher
26 * accuracy than the LM90, much like the LM86 does.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
Jean Delvarea874a102008-10-17 17:51:10 +020029 * chips made by Maxim. These chips are similar to the LM86.
Steven Cole44bbe872005-05-03 18:21:25 -060030 * Note that there is no easy way to differentiate between the three
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 Delvare6771ea12010-03-05 22:17:13 +010096enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646,
97 w83l771 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * The LM90 registers
101 */
102
103#define LM90_REG_R_MAN_ID 0xFE
104#define LM90_REG_R_CHIP_ID 0xFF
105#define LM90_REG_R_CONFIG1 0x03
106#define LM90_REG_W_CONFIG1 0x09
107#define LM90_REG_R_CONFIG2 0xBF
108#define LM90_REG_W_CONFIG2 0xBF
109#define LM90_REG_R_CONVRATE 0x04
110#define LM90_REG_W_CONVRATE 0x0A
111#define LM90_REG_R_STATUS 0x02
112#define LM90_REG_R_LOCAL_TEMP 0x00
113#define LM90_REG_R_LOCAL_HIGH 0x05
114#define LM90_REG_W_LOCAL_HIGH 0x0B
115#define LM90_REG_R_LOCAL_LOW 0x06
116#define LM90_REG_W_LOCAL_LOW 0x0C
117#define LM90_REG_R_LOCAL_CRIT 0x20
118#define LM90_REG_W_LOCAL_CRIT 0x20
119#define LM90_REG_R_REMOTE_TEMPH 0x01
120#define LM90_REG_R_REMOTE_TEMPL 0x10
121#define LM90_REG_R_REMOTE_OFFSH 0x11
122#define LM90_REG_W_REMOTE_OFFSH 0x11
123#define LM90_REG_R_REMOTE_OFFSL 0x12
124#define LM90_REG_W_REMOTE_OFFSL 0x12
125#define LM90_REG_R_REMOTE_HIGHH 0x07
126#define LM90_REG_W_REMOTE_HIGHH 0x0D
127#define LM90_REG_R_REMOTE_HIGHL 0x13
128#define LM90_REG_W_REMOTE_HIGHL 0x13
129#define LM90_REG_R_REMOTE_LOWH 0x08
130#define LM90_REG_W_REMOTE_LOWH 0x0E
131#define LM90_REG_R_REMOTE_LOWL 0x14
132#define LM90_REG_W_REMOTE_LOWL 0x14
133#define LM90_REG_R_REMOTE_CRIT 0x19
134#define LM90_REG_W_REMOTE_CRIT 0x19
135#define LM90_REG_R_TCRIT_HYST 0x21
136#define LM90_REG_W_TCRIT_HYST 0x21
137
Ben Hutchings271dabf2008-10-17 17:51:11 +0200138/* MAX6646/6647/6649/6657/6658/6659 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200139
140#define MAX6657_REG_R_LOCAL_TEMPL 0x11
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
Nate Case23b2d472008-10-17 17:51:10 +0200143 * Device flags
144 */
145#define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
146
147/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * Functions declaration
149 */
150
Jean Delvare310ec792009-12-14 21:17:23 +0100151static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200152static int lm90_probe(struct i2c_client *client,
153 const struct i2c_device_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static void lm90_init_client(struct i2c_client *client);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200155static int lm90_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static struct lm90_data *lm90_update_device(struct device *dev);
157
158/*
159 * Driver data (common to all clients)
160 */
161
Jean Delvare9b0e8522008-07-16 19:30:15 +0200162static const struct i2c_device_id lm90_id[] = {
163 { "adm1032", adm1032 },
164 { "adt7461", adt7461 },
165 { "lm90", lm90 },
166 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100167 { "lm89", lm86 },
168 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200169 { "max6646", max6646 },
170 { "max6647", max6646 },
171 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200172 { "max6657", max6657 },
173 { "max6658", max6657 },
174 { "max6659", max6657 },
175 { "max6680", max6680 },
176 { "max6681", max6680 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100177 { "w83l771", w83l771 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200178 { }
179};
180MODULE_DEVICE_TABLE(i2c, lm90_id);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static struct i2c_driver lm90_driver = {
Jean Delvare9b0e8522008-07-16 19:30:15 +0200183 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100184 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100185 .name = "lm90",
186 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200187 .probe = lm90_probe,
188 .remove = lm90_remove,
189 .id_table = lm90_id,
190 .detect = lm90_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100191 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194/*
195 * Client data (each client gets its own)
196 */
197
198struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700199 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100200 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 char valid; /* zero until following fields are valid */
202 unsigned long last_updated; /* in jiffies */
203 int kind;
Nate Case23b2d472008-10-17 17:51:10 +0200204 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jean Delvare95238362010-03-05 22:17:14 +0100206 u8 config_orig; /* Original configuration register value */
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /* registers values */
Jean Delvaref65e1702008-10-17 17:51:09 +0200209 s8 temp8[4]; /* 0: local low limit
210 1: local high limit
211 2: local critical limit
212 3: remote critical limit */
213 s16 temp11[5]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200214 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200215 2: remote high limit
Ben Hutchings271dabf2008-10-17 17:51:11 +0200216 3: remote offset (except max6646 and max6657)
Jean Delvaref65e1702008-10-17 17:51:09 +0200217 4: local input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 u8 temp_hyst;
219 u8 alarms; /* bitvector */
220};
221
222/*
Nate Casecea50fe2008-10-17 17:51:10 +0200223 * Conversions
224 * For local temperatures and limits, critical limits and the hysteresis
225 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
226 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200227 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
228 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200229 */
230
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200231static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200232{
233 return val * 1000;
234}
235
Ben Hutchings271dabf2008-10-17 17:51:11 +0200236static inline int temp_from_u8(u8 val)
237{
238 return val * 1000;
239}
240
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200241static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200242{
243 return val / 32 * 125;
244}
245
Ben Hutchings271dabf2008-10-17 17:51:11 +0200246static inline int temp_from_u16(u16 val)
247{
248 return val / 32 * 125;
249}
250
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200251static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200252{
253 if (val <= -128000)
254 return -128;
255 if (val >= 127000)
256 return 127;
257 if (val < 0)
258 return (val - 500) / 1000;
259 return (val + 500) / 1000;
260}
261
Ben Hutchings271dabf2008-10-17 17:51:11 +0200262static u8 temp_to_u8(long val)
263{
264 if (val <= 0)
265 return 0;
266 if (val >= 255000)
267 return 255;
268 return (val + 500) / 1000;
269}
270
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200271static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200272{
273 if (val <= -128000)
274 return 0x8000;
275 if (val >= 127875)
276 return 0x7FE0;
277 if (val < 0)
278 return (val - 62) / 125 * 32;
279 return (val + 62) / 125 * 32;
280}
281
282static u8 hyst_to_reg(long val)
283{
284 if (val <= 0)
285 return 0;
286 if (val >= 30500)
287 return 31;
288 return (val + 500) / 1000;
289}
290
291/*
Nate Case23b2d472008-10-17 17:51:10 +0200292 * ADT7461 in compatibility mode is almost identical to LM90 except that
293 * attempts to write values that are outside the range 0 < temp < 127 are
294 * treated as the boundary value.
295 *
296 * ADT7461 in "extended mode" operation uses unsigned integers offset by
297 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200298 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200299static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200300{
Nate Case23b2d472008-10-17 17:51:10 +0200301 if (data->flags & LM90_FLAG_ADT7461_EXT)
302 return (val - 64) * 1000;
303 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200304 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200305}
306
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200307static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200308{
Nate Case23b2d472008-10-17 17:51:10 +0200309 if (data->flags & LM90_FLAG_ADT7461_EXT)
310 return (val - 0x4000) / 64 * 250;
311 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200312 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200313}
314
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200315static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200316{
317 if (data->flags & LM90_FLAG_ADT7461_EXT) {
318 if (val <= -64000)
319 return 0;
320 if (val >= 191000)
321 return 0xFF;
322 return (val + 500 + 64000) / 1000;
323 } else {
324 if (val <= 0)
325 return 0;
326 if (val >= 127000)
327 return 127;
328 return (val + 500) / 1000;
329 }
330}
331
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200332static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200333{
334 if (data->flags & LM90_FLAG_ADT7461_EXT) {
335 if (val <= -64000)
336 return 0;
337 if (val >= 191750)
338 return 0xFFC0;
339 return (val + 64000 + 125) / 250 * 64;
340 } else {
341 if (val <= 0)
342 return 0;
343 if (val >= 127750)
344 return 0x7FC0;
345 return (val + 125) / 250 * 64;
346 }
Nate Casecea50fe2008-10-17 17:51:10 +0200347}
348
349/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * Sysfs stuff
351 */
352
Jean Delvare30d73942005-06-05 21:27:28 +0200353static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
354 char *buf)
355{
356 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
357 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200358 int temp;
359
360 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200361 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200362 else if (data->kind == max6646)
363 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200364 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200365 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200366
Jean Delvare97ae60b2008-10-26 17:04:39 +0100367 /* +16 degrees offset for temp2 for the LM99 */
368 if (data->kind == lm99 && attr->index == 3)
369 temp += 16000;
370
Nate Case23b2d472008-10-17 17:51:10 +0200371 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Jean Delvare30d73942005-06-05 21:27:28 +0200374static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
375 const char *buf, size_t count)
376{
377 static const u8 reg[4] = {
378 LM90_REG_W_LOCAL_LOW,
379 LM90_REG_W_LOCAL_HIGH,
380 LM90_REG_W_LOCAL_CRIT,
381 LM90_REG_W_REMOTE_CRIT,
382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jean Delvare30d73942005-06-05 21:27:28 +0200384 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
385 struct i2c_client *client = to_i2c_client(dev);
386 struct lm90_data *data = i2c_get_clientdata(client);
387 long val = simple_strtol(buf, NULL, 10);
388 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Jean Delvare97ae60b2008-10-26 17:04:39 +0100390 /* +16 degrees offset for temp2 for the LM99 */
391 if (data->kind == lm99 && attr->index == 3)
392 val -= 16000;
393
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100394 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200395 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200396 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200397 else if (data->kind == max6646)
398 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200399 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200400 data->temp8[nr] = temp_to_s8(val);
Jean Delvaref65e1702008-10-17 17:51:09 +0200401 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200403 return count;
404}
405
406static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
407 char *buf)
408{
409 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
410 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200411 int temp;
412
413 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200414 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200415 else if (data->kind == max6646)
416 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200417 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200418 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200419
Jean Delvare97ae60b2008-10-26 17:04:39 +0100420 /* +16 degrees offset for temp2 for the LM99 */
421 if (data->kind == lm99 && attr->index <= 2)
422 temp += 16000;
423
Nate Case23b2d472008-10-17 17:51:10 +0200424 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200425}
426
427static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
428 const char *buf, size_t count)
429{
Jean Delvare69f2f962007-09-05 14:15:37 +0200430 static const u8 reg[6] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200431 LM90_REG_W_REMOTE_LOWH,
432 LM90_REG_W_REMOTE_LOWL,
433 LM90_REG_W_REMOTE_HIGHH,
434 LM90_REG_W_REMOTE_HIGHL,
Jean Delvare69f2f962007-09-05 14:15:37 +0200435 LM90_REG_W_REMOTE_OFFSH,
436 LM90_REG_W_REMOTE_OFFSL,
Jean Delvare30d73942005-06-05 21:27:28 +0200437 };
438
439 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
440 struct i2c_client *client = to_i2c_client(dev);
441 struct lm90_data *data = i2c_get_clientdata(client);
442 long val = simple_strtol(buf, NULL, 10);
443 int nr = attr->index;
444
Jean Delvare97ae60b2008-10-26 17:04:39 +0100445 /* +16 degrees offset for temp2 for the LM99 */
446 if (data->kind == lm99 && attr->index <= 2)
447 val -= 16000;
448
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100449 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200450 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200451 data->temp11[nr] = temp_to_u16_adt7461(data, val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200452 else if (data->kind == max6657 || data->kind == max6680)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200453 data->temp11[nr] = temp_to_s8(val) << 8;
Ben Hutchings271dabf2008-10-17 17:51:11 +0200454 else if (data->kind == max6646)
455 data->temp11[nr] = temp_to_u8(val) << 8;
Jean Delvare30d73942005-06-05 21:27:28 +0200456 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200457 data->temp11[nr] = temp_to_s16(val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200458
Jean Delvare30d73942005-06-05 21:27:28 +0200459 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
460 data->temp11[nr] >> 8);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200461 if (data->kind != max6657 && data->kind != max6680
462 && data->kind != max6646)
Jean Delvare5f502a82008-10-17 17:51:09 +0200463 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
464 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100465 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200466 return count;
467}
468
469static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
470 char *buf)
471{
472 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
473 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200474 int temp;
475
476 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200477 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100478 else if (data->kind == max6646)
479 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200480 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200481 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200482
Jean Delvare97ae60b2008-10-26 17:04:39 +0100483 /* +16 degrees offset for temp2 for the LM99 */
484 if (data->kind == lm99 && attr->index == 3)
485 temp += 16000;
486
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200487 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200488}
489
490static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
491 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct i2c_client *client = to_i2c_client(dev);
494 struct lm90_data *data = i2c_get_clientdata(client);
495 long val = simple_strtol(buf, NULL, 10);
Jean Delvareec38fa22008-10-26 17:04:39 +0100496 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100498 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100499 if (data->kind == adt7461)
500 temp = temp_from_u8_adt7461(data, data->temp8[2]);
501 else if (data->kind == max6646)
502 temp = temp_from_u8(data->temp8[2]);
503 else
504 temp = temp_from_s8(data->temp8[2]);
505
506 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100508 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100509 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return count;
511}
512
Jean Delvare30d73942005-06-05 21:27:28 +0200513static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
514 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct lm90_data *data = lm90_update_device(dev);
517 return sprintf(buf, "%d\n", data->alarms);
518}
519
Jean Delvare2d457712006-09-24 20:52:15 +0200520static ssize_t show_alarm(struct device *dev, struct device_attribute
521 *devattr, char *buf)
522{
523 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
524 struct lm90_data *data = lm90_update_device(dev);
525 int bitnr = attr->index;
526
527 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
528}
529
Jean Delvaref65e1702008-10-17 17:51:09 +0200530static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
Jean Delvare30d73942005-06-05 21:27:28 +0200531static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
532static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200533 set_temp8, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200534static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
535 set_temp11, 1);
536static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200537 set_temp8, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200538static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
539 set_temp11, 2);
540static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200541 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200542static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200543 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200544static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200545 set_temphyst, 2);
546static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Jean Delvare69f2f962007-09-05 14:15:37 +0200547static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
548 set_temp11, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200549
550/* Individual alarm files */
551static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
552static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400553static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200554static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
555static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
556static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
557static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
558/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
560
Jean Delvare0e39e012006-09-24 21:16:40 +0200561static struct attribute *lm90_attributes[] = {
562 &sensor_dev_attr_temp1_input.dev_attr.attr,
563 &sensor_dev_attr_temp2_input.dev_attr.attr,
564 &sensor_dev_attr_temp1_min.dev_attr.attr,
565 &sensor_dev_attr_temp2_min.dev_attr.attr,
566 &sensor_dev_attr_temp1_max.dev_attr.attr,
567 &sensor_dev_attr_temp2_max.dev_attr.attr,
568 &sensor_dev_attr_temp1_crit.dev_attr.attr,
569 &sensor_dev_attr_temp2_crit.dev_attr.attr,
570 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
571 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
572
573 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
574 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400575 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200576 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
577 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
578 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
579 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
580 &dev_attr_alarms.attr,
581 NULL
582};
583
584static const struct attribute_group lm90_group = {
585 .attrs = lm90_attributes,
586};
587
Jean Delvarec3df5802005-10-26 21:39:40 +0200588/* pec used for ADM1032 only */
589static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
590 char *buf)
591{
592 struct i2c_client *client = to_i2c_client(dev);
593 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
594}
595
596static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
597 const char *buf, size_t count)
598{
599 struct i2c_client *client = to_i2c_client(dev);
600 long val = simple_strtol(buf, NULL, 10);
601
602 switch (val) {
603 case 0:
604 client->flags &= ~I2C_CLIENT_PEC;
605 break;
606 case 1:
607 client->flags |= I2C_CLIENT_PEC;
608 break;
609 default:
610 return -EINVAL;
611 }
612
613 return count;
614}
615
616static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/*
619 * Real code
620 */
621
Jean Delvarec3df5802005-10-26 21:39:40 +0200622/* The ADM1032 supports PEC but not on write byte transactions, so we need
Jean Delvare09664152007-06-09 10:11:15 -0400623 to explicitly ask for a transaction without PEC. */
Jean Delvarec3df5802005-10-26 21:39:40 +0200624static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
625{
626 return i2c_smbus_xfer(client->adapter, client->addr,
627 client->flags & ~I2C_CLIENT_PEC,
628 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
629}
630
631/* It is assumed that client->update_lock is held (unless we are in
632 detection or initialization steps). This matters when PEC is enabled,
633 because we don't want the address pointer to change between the write
634 byte and the read byte transactions. */
Jean Delvare8256fe02005-10-26 21:37:52 +0200635static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
636{
637 int err;
638
Jean Delvarec3df5802005-10-26 21:39:40 +0200639 if (client->flags & I2C_CLIENT_PEC) {
640 err = adm1032_write_byte(client, reg);
641 if (err >= 0)
642 err = i2c_smbus_read_byte(client);
643 } else
644 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200645
646 if (err < 0) {
647 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
648 reg, err);
649 return err;
650 }
651 *value = err;
652
653 return 0;
654}
655
Jean Delvare9b0e8522008-07-16 19:30:15 +0200656/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100657static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200658 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200660 struct i2c_adapter *adapter = new_client->adapter;
661 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +0100662 const char *name = NULL;
663 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200666 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Jean Delvare8f2fa772009-12-09 20:35:53 +0100668 /* detection and identification */
669 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100670 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100671 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100672 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100673 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100674 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100675 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100676 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100677 return -ENODEV;
678
679 if ((address == 0x4C || address == 0x4D)
680 && man_id == 0x01) { /* National Semiconductor */
681 int reg_config2;
682
683 reg_config2 = i2c_smbus_read_byte_data(new_client,
684 LM90_REG_R_CONFIG2);
685 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200686 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Jean Delvare8f2fa772009-12-09 20:35:53 +0100688 if ((reg_config1 & 0x2A) == 0x00
689 && (reg_config2 & 0xF8) == 0x00
690 && reg_convrate <= 0x09) {
691 if (address == 0x4C
692 && (chip_id & 0xF0) == 0x20) { /* LM90 */
693 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100695 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
696 name = "lm99";
697 dev_info(&adapter->dev,
698 "Assuming LM99 chip at 0x%02x\n",
699 address);
700 dev_info(&adapter->dev,
701 "If it is an LM89, instantiate it "
702 "with the new_device sysfs "
703 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400704 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100705 if (address == 0x4C
706 && (chip_id & 0xF0) == 0x10) { /* LM86 */
707 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100710 } else
711 if ((address == 0x4C || address == 0x4D)
712 && man_id == 0x41) { /* Analog Devices */
713 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
714 && (reg_config1 & 0x3F) == 0x00
715 && reg_convrate <= 0x0A) {
716 name = "adm1032";
717 /* The ADM1032 supports PEC, but only if combined
718 transactions are not used. */
719 if (i2c_check_functionality(adapter,
720 I2C_FUNC_SMBUS_BYTE))
721 info->flags |= I2C_CLIENT_PEC;
722 } else
723 if (chip_id == 0x51 /* ADT7461 */
724 && (reg_config1 & 0x1B) == 0x00
725 && reg_convrate <= 0x0A) {
726 name = "adt7461";
727 }
728 } else
729 if (man_id == 0x4D) { /* Maxim */
730 /*
731 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
732 * register. Reading from that address will return the last
733 * read value, which in our case is those of the man_id
734 * register. Likewise, the config1 register seems to lack a
735 * low nibble, so the value will be those of the previous
736 * read, so in our case those of the man_id register.
737 */
738 if (chip_id == man_id
739 && (address == 0x4C || address == 0x4D)
740 && (reg_config1 & 0x1F) == (man_id & 0x0F)
741 && reg_convrate <= 0x09) {
742 name = "max6657";
743 } else
744 /*
745 * The chip_id register of the MAX6680 and MAX6681 holds the
746 * revision of the chip. The lowest bit of the config1 register
747 * is unused and should return zero when read, so should the
748 * second to last bit of config1 (software reset).
749 */
750 if (chip_id == 0x01
751 && (reg_config1 & 0x03) == 0x00
752 && reg_convrate <= 0x07) {
753 name = "max6680";
754 } else
755 /*
756 * The chip_id register of the MAX6646/6647/6649 holds the
757 * revision of the chip. The lowest 6 bits of the config1
758 * register are unused and should return zero when read.
759 */
760 if (chip_id == 0x59
761 && (reg_config1 & 0x3f) == 0x00
762 && reg_convrate <= 0x07) {
763 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Jean Delvare6771ea12010-03-05 22:17:13 +0100765 } else
766 if (address == 0x4C
767 && man_id == 0x5C) { /* Winbond/Nuvoton */
768 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
769 && (reg_config1 & 0x2A) == 0x00
770 && reg_convrate <= 0x08) {
771 name = "w83l771";
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Jean Delvare8f2fa772009-12-09 20:35:53 +0100775 if (!name) { /* identification failed */
776 dev_dbg(&adapter->dev,
777 "Unsupported chip at 0x%02x (man_id=0x%02X, "
778 "chip_id=0x%02X)\n", address, man_id, chip_id);
779 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100781
Jean Delvare9b0e8522008-07-16 19:30:15 +0200782 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Jean Delvare9b0e8522008-07-16 19:30:15 +0200784 return 0;
785}
786
787static int lm90_probe(struct i2c_client *new_client,
788 const struct i2c_device_id *id)
789{
790 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
791 struct lm90_data *data;
792 int err;
793
794 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
795 if (!data) {
796 err = -ENOMEM;
797 goto exit;
798 }
799 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100800 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Jean Delvare9b0e8522008-07-16 19:30:15 +0200802 /* Set the device type */
803 data->kind = id->driver_data;
804 if (data->kind == adm1032) {
805 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
806 new_client->flags &= ~I2C_CLIENT_PEC;
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* Initialize the LM90 chip */
810 lm90_init_client(new_client);
811
812 /* Register sysfs hooks */
Jean Delvare0e39e012006-09-24 21:16:40 +0200813 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200814 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200815 if (new_client->flags & I2C_CLIENT_PEC) {
816 if ((err = device_create_file(&new_client->dev,
817 &dev_attr_pec)))
818 goto exit_remove_files;
819 }
Ben Hutchings271dabf2008-10-17 17:51:11 +0200820 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +0200821 if ((err = device_create_file(&new_client->dev,
822 &sensor_dev_attr_temp2_offset.dev_attr)))
823 goto exit_remove_files;
824 }
Jean Delvare0e39e012006-09-24 21:16:40 +0200825
Tony Jones1beeffe2007-08-20 13:46:20 -0700826 data->hwmon_dev = hwmon_device_register(&new_client->dev);
827 if (IS_ERR(data->hwmon_dev)) {
828 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200829 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833
Jean Delvare0e39e012006-09-24 21:16:40 +0200834exit_remove_files:
835 sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
836 device_remove_file(&new_client->dev, &dev_attr_pec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837exit_free:
838 kfree(data);
839exit:
840 return err;
841}
842
843static void lm90_init_client(struct i2c_client *client)
844{
Jean Delvare95238362010-03-05 22:17:14 +0100845 u8 config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400846 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /*
849 * Start the conversions.
850 */
851 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
852 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +0200853 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
854 dev_warn(&client->dev, "Initialization failed!\n");
855 return;
856 }
Jean Delvare95238362010-03-05 22:17:14 +0100857 data->config_orig = config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400858
Nate Case23b2d472008-10-17 17:51:10 +0200859 /* Check Temperature Range Select */
860 if (data->kind == adt7461) {
861 if (config & 0x04)
862 data->flags |= LM90_FLAG_ADT7461_EXT;
863 }
864
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400865 /*
866 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
867 * 0.125 degree resolution) and range (0x08, extend range
868 * to -64 degree) mode for the remote temperature sensor.
869 */
870 if (data->kind == max6680) {
871 config |= 0x18;
872 }
873
874 config &= 0xBF; /* run */
Jean Delvare95238362010-03-05 22:17:14 +0100875 if (config != data->config_orig) /* Only write if changed */
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400876 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Jean Delvare9b0e8522008-07-16 19:30:15 +0200879static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400881 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Tony Jones1beeffe2007-08-20 13:46:20 -0700883 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200884 sysfs_remove_group(&client->dev.kobj, &lm90_group);
885 device_remove_file(&client->dev, &dev_attr_pec);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200886 if (data->kind != max6657 && data->kind != max6646)
Jean Delvare69f2f962007-09-05 14:15:37 +0200887 device_remove_file(&client->dev,
888 &sensor_dev_attr_temp2_offset.dev_attr);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400889
Jean Delvare95238362010-03-05 22:17:14 +0100890 /* Restore initial configuration */
891 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
892 data->config_orig);
893
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400894 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 0;
896}
897
Jean Delvare6388a382008-10-17 17:51:09 +0200898static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
899{
900 int err;
901 u8 oldh, newh, l;
902
903 /*
904 * There is a trick here. We have to read two registers to have the
905 * sensor temperature, but we have to beware a conversion could occur
906 * inbetween the readings. The datasheet says we should either use
907 * the one-shot conversion register, which we don't want to do
908 * (disables hardware monitoring) or monitor the busy bit, which is
909 * impossible (we can't read the values and monitor that bit at the
910 * exact same time). So the solution used here is to read the high
911 * byte once, then the low byte, then the high byte again. If the new
912 * high byte matches the old one, then we have a valid reading. Else
913 * we have to read the low byte again, and now we believe we have a
914 * correct reading.
915 */
916 if ((err = lm90_read_reg(client, regh, &oldh))
917 || (err = lm90_read_reg(client, regl, &l))
918 || (err = lm90_read_reg(client, regh, &newh)))
919 return err;
920 if (oldh != newh) {
921 err = lm90_read_reg(client, regl, &l);
922 if (err)
923 return err;
924 }
925 *value = (newh << 8) | l;
926
927 return 0;
928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static struct lm90_data *lm90_update_device(struct device *dev)
931{
932 struct i2c_client *client = to_i2c_client(dev);
933 struct lm90_data *data = i2c_get_clientdata(client);
934
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100935 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +0200938 u8 h, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +0200941 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
942 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
943 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
944 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +0200945 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Ben Hutchings271dabf2008-10-17 17:51:11 +0200947 if (data->kind == max6657 || data->kind == max6646) {
Jean Delvaref65e1702008-10-17 17:51:09 +0200948 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
949 MAX6657_REG_R_LOCAL_TEMPL,
950 &data->temp11[4]);
951 } else {
952 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
953 &h) == 0)
954 data->temp11[4] = h << 8;
955 }
Jean Delvare6388a382008-10-17 17:51:09 +0200956 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
957 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Jean Delvare5f502a82008-10-17 17:51:09 +0200959 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
960 data->temp11[1] = h << 8;
961 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +0200962 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +0200963 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
964 &l) == 0)
965 data->temp11[1] |= l;
966 }
967 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
968 data->temp11[2] = h << 8;
969 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +0200970 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +0200971 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
972 &l) == 0)
973 data->temp11[2] |= l;
974 }
975
Ben Hutchings271dabf2008-10-17 17:51:11 +0200976 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +0200977 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +0200978 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +0200979 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
980 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +0200981 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +0200982 }
Jean Delvare8256fe02005-10-26 21:37:52 +0200983 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 data->last_updated = jiffies;
986 data->valid = 1;
987 }
988
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100989 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return data;
992}
993
994static int __init sensors_lm90_init(void)
995{
996 return i2c_add_driver(&lm90_driver);
997}
998
999static void __exit sensors_lm90_exit(void)
1000{
1001 i2c_del_driver(&lm90_driver);
1002}
1003
1004MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1005MODULE_DESCRIPTION("LM90/ADM1032 driver");
1006MODULE_LICENSE("GPL");
1007
1008module_init(sensors_lm90_init);
1009module_exit(sensors_lm90_exit);