blob: ddf617f3a7135bdf4ef2680ebb4221ff824381a9 [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 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
206 /* registers values */
Jean Delvaref65e1702008-10-17 17:51:09 +0200207 s8 temp8[4]; /* 0: local low limit
208 1: local high limit
209 2: local critical limit
210 3: remote critical limit */
211 s16 temp11[5]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200212 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200213 2: remote high limit
Ben Hutchings271dabf2008-10-17 17:51:11 +0200214 3: remote offset (except max6646 and max6657)
Jean Delvaref65e1702008-10-17 17:51:09 +0200215 4: local input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 u8 temp_hyst;
217 u8 alarms; /* bitvector */
218};
219
220/*
Nate Casecea50fe2008-10-17 17:51:10 +0200221 * Conversions
222 * For local temperatures and limits, critical limits and the hysteresis
223 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
224 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200225 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
226 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200227 */
228
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200229static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200230{
231 return val * 1000;
232}
233
Ben Hutchings271dabf2008-10-17 17:51:11 +0200234static inline int temp_from_u8(u8 val)
235{
236 return val * 1000;
237}
238
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200239static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200240{
241 return val / 32 * 125;
242}
243
Ben Hutchings271dabf2008-10-17 17:51:11 +0200244static inline int temp_from_u16(u16 val)
245{
246 return val / 32 * 125;
247}
248
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200249static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200250{
251 if (val <= -128000)
252 return -128;
253 if (val >= 127000)
254 return 127;
255 if (val < 0)
256 return (val - 500) / 1000;
257 return (val + 500) / 1000;
258}
259
Ben Hutchings271dabf2008-10-17 17:51:11 +0200260static u8 temp_to_u8(long val)
261{
262 if (val <= 0)
263 return 0;
264 if (val >= 255000)
265 return 255;
266 return (val + 500) / 1000;
267}
268
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200269static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200270{
271 if (val <= -128000)
272 return 0x8000;
273 if (val >= 127875)
274 return 0x7FE0;
275 if (val < 0)
276 return (val - 62) / 125 * 32;
277 return (val + 62) / 125 * 32;
278}
279
280static u8 hyst_to_reg(long val)
281{
282 if (val <= 0)
283 return 0;
284 if (val >= 30500)
285 return 31;
286 return (val + 500) / 1000;
287}
288
289/*
Nate Case23b2d472008-10-17 17:51:10 +0200290 * ADT7461 in compatibility mode is almost identical to LM90 except that
291 * attempts to write values that are outside the range 0 < temp < 127 are
292 * treated as the boundary value.
293 *
294 * ADT7461 in "extended mode" operation uses unsigned integers offset by
295 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200296 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200297static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200298{
Nate Case23b2d472008-10-17 17:51:10 +0200299 if (data->flags & LM90_FLAG_ADT7461_EXT)
300 return (val - 64) * 1000;
301 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200302 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200303}
304
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200305static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200306{
Nate Case23b2d472008-10-17 17:51:10 +0200307 if (data->flags & LM90_FLAG_ADT7461_EXT)
308 return (val - 0x4000) / 64 * 250;
309 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200310 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200311}
312
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200313static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200314{
315 if (data->flags & LM90_FLAG_ADT7461_EXT) {
316 if (val <= -64000)
317 return 0;
318 if (val >= 191000)
319 return 0xFF;
320 return (val + 500 + 64000) / 1000;
321 } else {
322 if (val <= 0)
323 return 0;
324 if (val >= 127000)
325 return 127;
326 return (val + 500) / 1000;
327 }
328}
329
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200330static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200331{
332 if (data->flags & LM90_FLAG_ADT7461_EXT) {
333 if (val <= -64000)
334 return 0;
335 if (val >= 191750)
336 return 0xFFC0;
337 return (val + 64000 + 125) / 250 * 64;
338 } else {
339 if (val <= 0)
340 return 0;
341 if (val >= 127750)
342 return 0x7FC0;
343 return (val + 125) / 250 * 64;
344 }
Nate Casecea50fe2008-10-17 17:51:10 +0200345}
346
347/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * Sysfs stuff
349 */
350
Jean Delvare30d73942005-06-05 21:27:28 +0200351static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
352 char *buf)
353{
354 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
355 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200356 int temp;
357
358 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200359 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200360 else if (data->kind == max6646)
361 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200362 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200363 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200364
Jean Delvare97ae60b2008-10-26 17:04:39 +0100365 /* +16 degrees offset for temp2 for the LM99 */
366 if (data->kind == lm99 && attr->index == 3)
367 temp += 16000;
368
Nate Case23b2d472008-10-17 17:51:10 +0200369 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Jean Delvare30d73942005-06-05 21:27:28 +0200372static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
373 const char *buf, size_t count)
374{
375 static const u8 reg[4] = {
376 LM90_REG_W_LOCAL_LOW,
377 LM90_REG_W_LOCAL_HIGH,
378 LM90_REG_W_LOCAL_CRIT,
379 LM90_REG_W_REMOTE_CRIT,
380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Jean Delvare30d73942005-06-05 21:27:28 +0200382 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
383 struct i2c_client *client = to_i2c_client(dev);
384 struct lm90_data *data = i2c_get_clientdata(client);
385 long val = simple_strtol(buf, NULL, 10);
386 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Jean Delvare97ae60b2008-10-26 17:04:39 +0100388 /* +16 degrees offset for temp2 for the LM99 */
389 if (data->kind == lm99 && attr->index == 3)
390 val -= 16000;
391
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100392 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200393 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200394 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200395 else if (data->kind == max6646)
396 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200397 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200398 data->temp8[nr] = temp_to_s8(val);
Jean Delvaref65e1702008-10-17 17:51:09 +0200399 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100400 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200401 return count;
402}
403
404static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
405 char *buf)
406{
407 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
408 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200409 int temp;
410
411 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200412 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200413 else if (data->kind == max6646)
414 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200415 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200416 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200417
Jean Delvare97ae60b2008-10-26 17:04:39 +0100418 /* +16 degrees offset for temp2 for the LM99 */
419 if (data->kind == lm99 && attr->index <= 2)
420 temp += 16000;
421
Nate Case23b2d472008-10-17 17:51:10 +0200422 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200423}
424
425static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
426 const char *buf, size_t count)
427{
Jean Delvare69f2f962007-09-05 14:15:37 +0200428 static const u8 reg[6] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200429 LM90_REG_W_REMOTE_LOWH,
430 LM90_REG_W_REMOTE_LOWL,
431 LM90_REG_W_REMOTE_HIGHH,
432 LM90_REG_W_REMOTE_HIGHL,
Jean Delvare69f2f962007-09-05 14:15:37 +0200433 LM90_REG_W_REMOTE_OFFSH,
434 LM90_REG_W_REMOTE_OFFSL,
Jean Delvare30d73942005-06-05 21:27:28 +0200435 };
436
437 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
438 struct i2c_client *client = to_i2c_client(dev);
439 struct lm90_data *data = i2c_get_clientdata(client);
440 long val = simple_strtol(buf, NULL, 10);
441 int nr = attr->index;
442
Jean Delvare97ae60b2008-10-26 17:04:39 +0100443 /* +16 degrees offset for temp2 for the LM99 */
444 if (data->kind == lm99 && attr->index <= 2)
445 val -= 16000;
446
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100447 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200448 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200449 data->temp11[nr] = temp_to_u16_adt7461(data, val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200450 else if (data->kind == max6657 || data->kind == max6680)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200451 data->temp11[nr] = temp_to_s8(val) << 8;
Ben Hutchings271dabf2008-10-17 17:51:11 +0200452 else if (data->kind == max6646)
453 data->temp11[nr] = temp_to_u8(val) << 8;
Jean Delvare30d73942005-06-05 21:27:28 +0200454 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200455 data->temp11[nr] = temp_to_s16(val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200456
Jean Delvare30d73942005-06-05 21:27:28 +0200457 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
458 data->temp11[nr] >> 8);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200459 if (data->kind != max6657 && data->kind != max6680
460 && data->kind != max6646)
Jean Delvare5f502a82008-10-17 17:51:09 +0200461 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
462 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100463 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200464 return count;
465}
466
467static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
468 char *buf)
469{
470 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
471 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200472 int temp;
473
474 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200475 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100476 else if (data->kind == max6646)
477 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200478 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200479 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200480
Jean Delvare97ae60b2008-10-26 17:04:39 +0100481 /* +16 degrees offset for temp2 for the LM99 */
482 if (data->kind == lm99 && attr->index == 3)
483 temp += 16000;
484
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200485 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200486}
487
488static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
489 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct i2c_client *client = to_i2c_client(dev);
492 struct lm90_data *data = i2c_get_clientdata(client);
493 long val = simple_strtol(buf, NULL, 10);
Jean Delvareec38fa22008-10-26 17:04:39 +0100494 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100496 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100497 if (data->kind == adt7461)
498 temp = temp_from_u8_adt7461(data, data->temp8[2]);
499 else if (data->kind == max6646)
500 temp = temp_from_u8(data->temp8[2]);
501 else
502 temp = temp_from_s8(data->temp8[2]);
503
504 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100506 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100507 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return count;
509}
510
Jean Delvare30d73942005-06-05 21:27:28 +0200511static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
512 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct lm90_data *data = lm90_update_device(dev);
515 return sprintf(buf, "%d\n", data->alarms);
516}
517
Jean Delvare2d457712006-09-24 20:52:15 +0200518static ssize_t show_alarm(struct device *dev, struct device_attribute
519 *devattr, char *buf)
520{
521 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
522 struct lm90_data *data = lm90_update_device(dev);
523 int bitnr = attr->index;
524
525 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
526}
527
Jean Delvaref65e1702008-10-17 17:51:09 +0200528static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
Jean Delvare30d73942005-06-05 21:27:28 +0200529static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
530static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200531 set_temp8, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200532static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
533 set_temp11, 1);
534static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200535 set_temp8, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200536static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
537 set_temp11, 2);
538static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200539 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200540static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200541 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200542static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200543 set_temphyst, 2);
544static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Jean Delvare69f2f962007-09-05 14:15:37 +0200545static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
546 set_temp11, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200547
548/* Individual alarm files */
549static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
550static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400551static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200552static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
553static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
554static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
555static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
556/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
558
Jean Delvare0e39e012006-09-24 21:16:40 +0200559static struct attribute *lm90_attributes[] = {
560 &sensor_dev_attr_temp1_input.dev_attr.attr,
561 &sensor_dev_attr_temp2_input.dev_attr.attr,
562 &sensor_dev_attr_temp1_min.dev_attr.attr,
563 &sensor_dev_attr_temp2_min.dev_attr.attr,
564 &sensor_dev_attr_temp1_max.dev_attr.attr,
565 &sensor_dev_attr_temp2_max.dev_attr.attr,
566 &sensor_dev_attr_temp1_crit.dev_attr.attr,
567 &sensor_dev_attr_temp2_crit.dev_attr.attr,
568 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
569 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
570
571 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
572 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400573 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200574 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
575 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
576 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
577 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
578 &dev_attr_alarms.attr,
579 NULL
580};
581
582static const struct attribute_group lm90_group = {
583 .attrs = lm90_attributes,
584};
585
Jean Delvarec3df5802005-10-26 21:39:40 +0200586/* pec used for ADM1032 only */
587static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
588 char *buf)
589{
590 struct i2c_client *client = to_i2c_client(dev);
591 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
592}
593
594static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
595 const char *buf, size_t count)
596{
597 struct i2c_client *client = to_i2c_client(dev);
598 long val = simple_strtol(buf, NULL, 10);
599
600 switch (val) {
601 case 0:
602 client->flags &= ~I2C_CLIENT_PEC;
603 break;
604 case 1:
605 client->flags |= I2C_CLIENT_PEC;
606 break;
607 default:
608 return -EINVAL;
609 }
610
611 return count;
612}
613
614static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
617 * Real code
618 */
619
Jean Delvarec3df5802005-10-26 21:39:40 +0200620/* The ADM1032 supports PEC but not on write byte transactions, so we need
Jean Delvare09664152007-06-09 10:11:15 -0400621 to explicitly ask for a transaction without PEC. */
Jean Delvarec3df5802005-10-26 21:39:40 +0200622static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
623{
624 return i2c_smbus_xfer(client->adapter, client->addr,
625 client->flags & ~I2C_CLIENT_PEC,
626 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
627}
628
629/* It is assumed that client->update_lock is held (unless we are in
630 detection or initialization steps). This matters when PEC is enabled,
631 because we don't want the address pointer to change between the write
632 byte and the read byte transactions. */
Jean Delvare8256fe02005-10-26 21:37:52 +0200633static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
634{
635 int err;
636
Jean Delvarec3df5802005-10-26 21:39:40 +0200637 if (client->flags & I2C_CLIENT_PEC) {
638 err = adm1032_write_byte(client, reg);
639 if (err >= 0)
640 err = i2c_smbus_read_byte(client);
641 } else
642 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200643
644 if (err < 0) {
645 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
646 reg, err);
647 return err;
648 }
649 *value = err;
650
651 return 0;
652}
653
Jean Delvare9b0e8522008-07-16 19:30:15 +0200654/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100655static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200656 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200658 struct i2c_adapter *adapter = new_client->adapter;
659 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +0100660 const char *name = NULL;
661 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200664 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Jean Delvare8f2fa772009-12-09 20:35:53 +0100666 /* detection and identification */
667 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100668 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100669 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100670 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100671 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100672 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100673 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100674 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100675 return -ENODEV;
676
677 if ((address == 0x4C || address == 0x4D)
678 && man_id == 0x01) { /* National Semiconductor */
679 int reg_config2;
680
681 reg_config2 = i2c_smbus_read_byte_data(new_client,
682 LM90_REG_R_CONFIG2);
683 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200684 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jean Delvare8f2fa772009-12-09 20:35:53 +0100686 if ((reg_config1 & 0x2A) == 0x00
687 && (reg_config2 & 0xF8) == 0x00
688 && reg_convrate <= 0x09) {
689 if (address == 0x4C
690 && (chip_id & 0xF0) == 0x20) { /* LM90 */
691 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100693 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
694 name = "lm99";
695 dev_info(&adapter->dev,
696 "Assuming LM99 chip at 0x%02x\n",
697 address);
698 dev_info(&adapter->dev,
699 "If it is an LM89, instantiate it "
700 "with the new_device sysfs "
701 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400702 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100703 if (address == 0x4C
704 && (chip_id & 0xF0) == 0x10) { /* LM86 */
705 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100708 } else
709 if ((address == 0x4C || address == 0x4D)
710 && man_id == 0x41) { /* Analog Devices */
711 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
712 && (reg_config1 & 0x3F) == 0x00
713 && reg_convrate <= 0x0A) {
714 name = "adm1032";
715 /* The ADM1032 supports PEC, but only if combined
716 transactions are not used. */
717 if (i2c_check_functionality(adapter,
718 I2C_FUNC_SMBUS_BYTE))
719 info->flags |= I2C_CLIENT_PEC;
720 } else
721 if (chip_id == 0x51 /* ADT7461 */
722 && (reg_config1 & 0x1B) == 0x00
723 && reg_convrate <= 0x0A) {
724 name = "adt7461";
725 }
726 } else
727 if (man_id == 0x4D) { /* Maxim */
728 /*
729 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
730 * register. Reading from that address will return the last
731 * read value, which in our case is those of the man_id
732 * register. Likewise, the config1 register seems to lack a
733 * low nibble, so the value will be those of the previous
734 * read, so in our case those of the man_id register.
735 */
736 if (chip_id == man_id
737 && (address == 0x4C || address == 0x4D)
738 && (reg_config1 & 0x1F) == (man_id & 0x0F)
739 && reg_convrate <= 0x09) {
740 name = "max6657";
741 } else
742 /*
743 * The chip_id register of the MAX6680 and MAX6681 holds the
744 * revision of the chip. The lowest bit of the config1 register
745 * is unused and should return zero when read, so should the
746 * second to last bit of config1 (software reset).
747 */
748 if (chip_id == 0x01
749 && (reg_config1 & 0x03) == 0x00
750 && reg_convrate <= 0x07) {
751 name = "max6680";
752 } else
753 /*
754 * The chip_id register of the MAX6646/6647/6649 holds the
755 * revision of the chip. The lowest 6 bits of the config1
756 * register are unused and should return zero when read.
757 */
758 if (chip_id == 0x59
759 && (reg_config1 & 0x3f) == 0x00
760 && reg_convrate <= 0x07) {
761 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Jean Delvare6771ea12010-03-05 22:17:13 +0100763 } else
764 if (address == 0x4C
765 && man_id == 0x5C) { /* Winbond/Nuvoton */
766 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
767 && (reg_config1 & 0x2A) == 0x00
768 && reg_convrate <= 0x08) {
769 name = "w83l771";
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
Jean Delvare8f2fa772009-12-09 20:35:53 +0100773 if (!name) { /* identification failed */
774 dev_dbg(&adapter->dev,
775 "Unsupported chip at 0x%02x (man_id=0x%02X, "
776 "chip_id=0x%02X)\n", address, man_id, chip_id);
777 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100779
Jean Delvare9b0e8522008-07-16 19:30:15 +0200780 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Jean Delvare9b0e8522008-07-16 19:30:15 +0200782 return 0;
783}
784
785static int lm90_probe(struct i2c_client *new_client,
786 const struct i2c_device_id *id)
787{
788 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
789 struct lm90_data *data;
790 int err;
791
792 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
793 if (!data) {
794 err = -ENOMEM;
795 goto exit;
796 }
797 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100798 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jean Delvare9b0e8522008-07-16 19:30:15 +0200800 /* Set the device type */
801 data->kind = id->driver_data;
802 if (data->kind == adm1032) {
803 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
804 new_client->flags &= ~I2C_CLIENT_PEC;
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /* Initialize the LM90 chip */
808 lm90_init_client(new_client);
809
810 /* Register sysfs hooks */
Jean Delvare0e39e012006-09-24 21:16:40 +0200811 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200812 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200813 if (new_client->flags & I2C_CLIENT_PEC) {
814 if ((err = device_create_file(&new_client->dev,
815 &dev_attr_pec)))
816 goto exit_remove_files;
817 }
Ben Hutchings271dabf2008-10-17 17:51:11 +0200818 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +0200819 if ((err = device_create_file(&new_client->dev,
820 &sensor_dev_attr_temp2_offset.dev_attr)))
821 goto exit_remove_files;
822 }
Jean Delvare0e39e012006-09-24 21:16:40 +0200823
Tony Jones1beeffe2007-08-20 13:46:20 -0700824 data->hwmon_dev = hwmon_device_register(&new_client->dev);
825 if (IS_ERR(data->hwmon_dev)) {
826 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200827 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
831
Jean Delvare0e39e012006-09-24 21:16:40 +0200832exit_remove_files:
833 sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
834 device_remove_file(&new_client->dev, &dev_attr_pec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835exit_free:
836 kfree(data);
837exit:
838 return err;
839}
840
841static void lm90_init_client(struct i2c_client *client)
842{
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400843 u8 config, config_orig;
844 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /*
847 * Start the conversions.
848 */
849 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
850 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +0200851 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
852 dev_warn(&client->dev, "Initialization failed!\n");
853 return;
854 }
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400855 config_orig = config;
856
Nate Case23b2d472008-10-17 17:51:10 +0200857 /* Check Temperature Range Select */
858 if (data->kind == adt7461) {
859 if (config & 0x04)
860 data->flags |= LM90_FLAG_ADT7461_EXT;
861 }
862
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400863 /*
864 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
865 * 0.125 degree resolution) and range (0x08, extend range
866 * to -64 degree) mode for the remote temperature sensor.
867 */
868 if (data->kind == max6680) {
869 config |= 0x18;
870 }
871
872 config &= 0xBF; /* run */
873 if (config != config_orig) /* Only write if changed */
874 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Jean Delvare9b0e8522008-07-16 19:30:15 +0200877static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400879 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Tony Jones1beeffe2007-08-20 13:46:20 -0700881 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200882 sysfs_remove_group(&client->dev.kobj, &lm90_group);
883 device_remove_file(&client->dev, &dev_attr_pec);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200884 if (data->kind != max6657 && data->kind != max6646)
Jean Delvare69f2f962007-09-05 14:15:37 +0200885 device_remove_file(&client->dev,
886 &sensor_dev_attr_temp2_offset.dev_attr);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400887
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400888 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return 0;
890}
891
Jean Delvare6388a382008-10-17 17:51:09 +0200892static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
893{
894 int err;
895 u8 oldh, newh, l;
896
897 /*
898 * There is a trick here. We have to read two registers to have the
899 * sensor temperature, but we have to beware a conversion could occur
900 * inbetween the readings. The datasheet says we should either use
901 * the one-shot conversion register, which we don't want to do
902 * (disables hardware monitoring) or monitor the busy bit, which is
903 * impossible (we can't read the values and monitor that bit at the
904 * exact same time). So the solution used here is to read the high
905 * byte once, then the low byte, then the high byte again. If the new
906 * high byte matches the old one, then we have a valid reading. Else
907 * we have to read the low byte again, and now we believe we have a
908 * correct reading.
909 */
910 if ((err = lm90_read_reg(client, regh, &oldh))
911 || (err = lm90_read_reg(client, regl, &l))
912 || (err = lm90_read_reg(client, regh, &newh)))
913 return err;
914 if (oldh != newh) {
915 err = lm90_read_reg(client, regl, &l);
916 if (err)
917 return err;
918 }
919 *value = (newh << 8) | l;
920
921 return 0;
922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924static struct lm90_data *lm90_update_device(struct device *dev)
925{
926 struct i2c_client *client = to_i2c_client(dev);
927 struct lm90_data *data = i2c_get_clientdata(client);
928
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100929 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +0200932 u8 h, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +0200935 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
936 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
937 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
938 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +0200939 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Ben Hutchings271dabf2008-10-17 17:51:11 +0200941 if (data->kind == max6657 || data->kind == max6646) {
Jean Delvaref65e1702008-10-17 17:51:09 +0200942 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
943 MAX6657_REG_R_LOCAL_TEMPL,
944 &data->temp11[4]);
945 } else {
946 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
947 &h) == 0)
948 data->temp11[4] = h << 8;
949 }
Jean Delvare6388a382008-10-17 17:51:09 +0200950 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
951 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Jean Delvare5f502a82008-10-17 17:51:09 +0200953 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
954 data->temp11[1] = h << 8;
955 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +0200956 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +0200957 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
958 &l) == 0)
959 data->temp11[1] |= l;
960 }
961 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
962 data->temp11[2] = h << 8;
963 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +0200964 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +0200965 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
966 &l) == 0)
967 data->temp11[2] |= l;
968 }
969
Ben Hutchings271dabf2008-10-17 17:51:11 +0200970 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +0200971 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +0200972 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +0200973 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
974 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +0200975 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +0200976 }
Jean Delvare8256fe02005-10-26 21:37:52 +0200977 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 data->last_updated = jiffies;
980 data->valid = 1;
981 }
982
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100983 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 return data;
986}
987
988static int __init sensors_lm90_init(void)
989{
990 return i2c_add_driver(&lm90_driver);
991}
992
993static void __exit sensors_lm90_exit(void)
994{
995 i2c_del_driver(&lm90_driver);
996}
997
998MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
999MODULE_DESCRIPTION("LM90/ADM1032 driver");
1000MODULE_LICENSE("GPL");
1001
1002module_init(sensors_lm90_init);
1003module_exit(sensors_lm90_exit);