blob: aafed2898d9518bf7743f82becbddbdd14f3acde [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 Delvare53de3342010-03-05 22:17:15 +0100155static void lm90_alert(struct i2c_client *client, unsigned int flag);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200156static int lm90_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static struct lm90_data *lm90_update_device(struct device *dev);
158
159/*
160 * Driver data (common to all clients)
161 */
162
Jean Delvare9b0e8522008-07-16 19:30:15 +0200163static const struct i2c_device_id lm90_id[] = {
164 { "adm1032", adm1032 },
165 { "adt7461", adt7461 },
166 { "lm90", lm90 },
167 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100168 { "lm89", lm86 },
169 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200170 { "max6646", max6646 },
171 { "max6647", max6646 },
172 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200173 { "max6657", max6657 },
174 { "max6658", max6657 },
175 { "max6659", max6657 },
176 { "max6680", max6680 },
177 { "max6681", max6680 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100178 { "w83l771", w83l771 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200179 { }
180};
181MODULE_DEVICE_TABLE(i2c, lm90_id);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static struct i2c_driver lm90_driver = {
Jean Delvare9b0e8522008-07-16 19:30:15 +0200184 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100185 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100186 .name = "lm90",
187 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200188 .probe = lm90_probe,
189 .remove = lm90_remove,
Jean Delvare53de3342010-03-05 22:17:15 +0100190 .alert = lm90_alert,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200191 .id_table = lm90_id,
192 .detect = lm90_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100193 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
196/*
197 * Client data (each client gets its own)
198 */
199
200struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700201 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100202 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 char valid; /* zero until following fields are valid */
204 unsigned long last_updated; /* in jiffies */
205 int kind;
Nate Case23b2d472008-10-17 17:51:10 +0200206 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jean Delvare95238362010-03-05 22:17:14 +0100208 u8 config_orig; /* Original configuration register value */
Jean Delvare53de3342010-03-05 22:17:15 +0100209 u8 alert_alarms; /* Which alarm bits trigger ALERT# */
Jean Delvare95238362010-03-05 22:17:14 +0100210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* registers values */
Jean Delvaref65e1702008-10-17 17:51:09 +0200212 s8 temp8[4]; /* 0: local low limit
213 1: local high limit
214 2: local critical limit
215 3: remote critical limit */
216 s16 temp11[5]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200217 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200218 2: remote high limit
Ben Hutchings271dabf2008-10-17 17:51:11 +0200219 3: remote offset (except max6646 and max6657)
Jean Delvaref65e1702008-10-17 17:51:09 +0200220 4: local input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 u8 temp_hyst;
222 u8 alarms; /* bitvector */
223};
224
225/*
Nate Casecea50fe2008-10-17 17:51:10 +0200226 * Conversions
227 * For local temperatures and limits, critical limits and the hysteresis
228 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
229 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200230 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
231 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200232 */
233
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200234static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200235{
236 return val * 1000;
237}
238
Ben Hutchings271dabf2008-10-17 17:51:11 +0200239static inline int temp_from_u8(u8 val)
240{
241 return val * 1000;
242}
243
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200244static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200245{
246 return val / 32 * 125;
247}
248
Ben Hutchings271dabf2008-10-17 17:51:11 +0200249static inline int temp_from_u16(u16 val)
250{
251 return val / 32 * 125;
252}
253
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200254static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200255{
256 if (val <= -128000)
257 return -128;
258 if (val >= 127000)
259 return 127;
260 if (val < 0)
261 return (val - 500) / 1000;
262 return (val + 500) / 1000;
263}
264
Ben Hutchings271dabf2008-10-17 17:51:11 +0200265static u8 temp_to_u8(long val)
266{
267 if (val <= 0)
268 return 0;
269 if (val >= 255000)
270 return 255;
271 return (val + 500) / 1000;
272}
273
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200274static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200275{
276 if (val <= -128000)
277 return 0x8000;
278 if (val >= 127875)
279 return 0x7FE0;
280 if (val < 0)
281 return (val - 62) / 125 * 32;
282 return (val + 62) / 125 * 32;
283}
284
285static u8 hyst_to_reg(long val)
286{
287 if (val <= 0)
288 return 0;
289 if (val >= 30500)
290 return 31;
291 return (val + 500) / 1000;
292}
293
294/*
Nate Case23b2d472008-10-17 17:51:10 +0200295 * ADT7461 in compatibility mode is almost identical to LM90 except that
296 * attempts to write values that are outside the range 0 < temp < 127 are
297 * treated as the boundary value.
298 *
299 * ADT7461 in "extended mode" operation uses unsigned integers offset by
300 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200301 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200302static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200303{
Nate Case23b2d472008-10-17 17:51:10 +0200304 if (data->flags & LM90_FLAG_ADT7461_EXT)
305 return (val - 64) * 1000;
306 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200307 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200308}
309
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200310static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200311{
Nate Case23b2d472008-10-17 17:51:10 +0200312 if (data->flags & LM90_FLAG_ADT7461_EXT)
313 return (val - 0x4000) / 64 * 250;
314 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200315 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200316}
317
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200318static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200319{
320 if (data->flags & LM90_FLAG_ADT7461_EXT) {
321 if (val <= -64000)
322 return 0;
323 if (val >= 191000)
324 return 0xFF;
325 return (val + 500 + 64000) / 1000;
326 } else {
327 if (val <= 0)
328 return 0;
329 if (val >= 127000)
330 return 127;
331 return (val + 500) / 1000;
332 }
333}
334
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200335static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200336{
337 if (data->flags & LM90_FLAG_ADT7461_EXT) {
338 if (val <= -64000)
339 return 0;
340 if (val >= 191750)
341 return 0xFFC0;
342 return (val + 64000 + 125) / 250 * 64;
343 } else {
344 if (val <= 0)
345 return 0;
346 if (val >= 127750)
347 return 0x7FC0;
348 return (val + 125) / 250 * 64;
349 }
Nate Casecea50fe2008-10-17 17:51:10 +0200350}
351
352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Sysfs stuff
354 */
355
Jean Delvare30d73942005-06-05 21:27:28 +0200356static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
357 char *buf)
358{
359 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
360 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200361 int temp;
362
363 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200364 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200365 else if (data->kind == max6646)
366 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200367 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200368 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200369
Jean Delvare97ae60b2008-10-26 17:04:39 +0100370 /* +16 degrees offset for temp2 for the LM99 */
371 if (data->kind == lm99 && attr->index == 3)
372 temp += 16000;
373
Nate Case23b2d472008-10-17 17:51:10 +0200374 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jean Delvare30d73942005-06-05 21:27:28 +0200377static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
378 const char *buf, size_t count)
379{
380 static const u8 reg[4] = {
381 LM90_REG_W_LOCAL_LOW,
382 LM90_REG_W_LOCAL_HIGH,
383 LM90_REG_W_LOCAL_CRIT,
384 LM90_REG_W_REMOTE_CRIT,
385 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jean Delvare30d73942005-06-05 21:27:28 +0200387 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
388 struct i2c_client *client = to_i2c_client(dev);
389 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200390 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200391 long val;
392 int err;
393
394 err = strict_strtol(buf, 10, &val);
395 if (err < 0)
396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Jean Delvare97ae60b2008-10-26 17:04:39 +0100398 /* +16 degrees offset for temp2 for the LM99 */
399 if (data->kind == lm99 && attr->index == 3)
400 val -= 16000;
401
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200403 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200404 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200405 else if (data->kind == max6646)
406 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200407 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200408 data->temp8[nr] = temp_to_s8(val);
Jean Delvaref65e1702008-10-17 17:51:09 +0200409 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100410 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200411 return count;
412}
413
414static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
415 char *buf)
416{
417 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
418 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200419 int temp;
420
421 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200422 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200423 else if (data->kind == max6646)
424 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200425 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200426 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200427
Jean Delvare97ae60b2008-10-26 17:04:39 +0100428 /* +16 degrees offset for temp2 for the LM99 */
429 if (data->kind == lm99 && attr->index <= 2)
430 temp += 16000;
431
Nate Case23b2d472008-10-17 17:51:10 +0200432 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200433}
434
435static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
436 const char *buf, size_t count)
437{
Jean Delvare69f2f962007-09-05 14:15:37 +0200438 static const u8 reg[6] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200439 LM90_REG_W_REMOTE_LOWH,
440 LM90_REG_W_REMOTE_LOWL,
441 LM90_REG_W_REMOTE_HIGHH,
442 LM90_REG_W_REMOTE_HIGHL,
Jean Delvare69f2f962007-09-05 14:15:37 +0200443 LM90_REG_W_REMOTE_OFFSH,
444 LM90_REG_W_REMOTE_OFFSL,
Jean Delvare30d73942005-06-05 21:27:28 +0200445 };
446
447 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
448 struct i2c_client *client = to_i2c_client(dev);
449 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200450 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200451 long val;
452 int err;
453
454 err = strict_strtol(buf, 10, &val);
455 if (err < 0)
456 return err;
Jean Delvare30d73942005-06-05 21:27:28 +0200457
Jean Delvare97ae60b2008-10-26 17:04:39 +0100458 /* +16 degrees offset for temp2 for the LM99 */
459 if (data->kind == lm99 && attr->index <= 2)
460 val -= 16000;
461
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100462 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200463 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200464 data->temp11[nr] = temp_to_u16_adt7461(data, val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200465 else if (data->kind == max6657 || data->kind == max6680)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200466 data->temp11[nr] = temp_to_s8(val) << 8;
Ben Hutchings271dabf2008-10-17 17:51:11 +0200467 else if (data->kind == max6646)
468 data->temp11[nr] = temp_to_u8(val) << 8;
Jean Delvare30d73942005-06-05 21:27:28 +0200469 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200470 data->temp11[nr] = temp_to_s16(val);
Jean Delvare5f502a82008-10-17 17:51:09 +0200471
Jean Delvare30d73942005-06-05 21:27:28 +0200472 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
473 data->temp11[nr] >> 8);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200474 if (data->kind != max6657 && data->kind != max6680
475 && data->kind != max6646)
Jean Delvare5f502a82008-10-17 17:51:09 +0200476 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
477 data->temp11[nr] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100478 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200479 return count;
480}
481
Guenter Roeck11e57812010-10-28 20:31:42 +0200482static ssize_t show_temphyst(struct device *dev,
483 struct device_attribute *devattr,
Jean Delvare30d73942005-06-05 21:27:28 +0200484 char *buf)
485{
486 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
487 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200488 int temp;
489
490 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200491 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100492 else if (data->kind == max6646)
493 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200494 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200495 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200496
Jean Delvare97ae60b2008-10-26 17:04:39 +0100497 /* +16 degrees offset for temp2 for the LM99 */
498 if (data->kind == lm99 && attr->index == 3)
499 temp += 16000;
500
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200501 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200502}
503
504static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
505 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct i2c_client *client = to_i2c_client(dev);
508 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck11e57812010-10-28 20:31:42 +0200509 long val;
510 int err;
Jean Delvareec38fa22008-10-26 17:04:39 +0100511 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Guenter Roeck11e57812010-10-28 20:31:42 +0200513 err = strict_strtol(buf, 10, &val);
514 if (err < 0)
515 return err;
516
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100517 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100518 if (data->kind == adt7461)
519 temp = temp_from_u8_adt7461(data, data->temp8[2]);
520 else if (data->kind == max6646)
521 temp = temp_from_u8(data->temp8[2]);
522 else
523 temp = temp_from_s8(data->temp8[2]);
524
525 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100527 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100528 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return count;
530}
531
Jean Delvare30d73942005-06-05 21:27:28 +0200532static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
533 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct lm90_data *data = lm90_update_device(dev);
536 return sprintf(buf, "%d\n", data->alarms);
537}
538
Jean Delvare2d457712006-09-24 20:52:15 +0200539static ssize_t show_alarm(struct device *dev, struct device_attribute
540 *devattr, char *buf)
541{
542 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
543 struct lm90_data *data = lm90_update_device(dev);
544 int bitnr = attr->index;
545
546 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
547}
548
Jean Delvaref65e1702008-10-17 17:51:09 +0200549static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
Jean Delvare30d73942005-06-05 21:27:28 +0200550static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
551static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200552 set_temp8, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200553static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
554 set_temp11, 1);
555static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200556 set_temp8, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200557static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
558 set_temp11, 2);
559static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200560 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200561static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200562 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200563static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200564 set_temphyst, 2);
565static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Jean Delvare69f2f962007-09-05 14:15:37 +0200566static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
567 set_temp11, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200568
569/* Individual alarm files */
570static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
571static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400572static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200573static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
574static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
575static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
576static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
577/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
579
Jean Delvare0e39e012006-09-24 21:16:40 +0200580static struct attribute *lm90_attributes[] = {
581 &sensor_dev_attr_temp1_input.dev_attr.attr,
582 &sensor_dev_attr_temp2_input.dev_attr.attr,
583 &sensor_dev_attr_temp1_min.dev_attr.attr,
584 &sensor_dev_attr_temp2_min.dev_attr.attr,
585 &sensor_dev_attr_temp1_max.dev_attr.attr,
586 &sensor_dev_attr_temp2_max.dev_attr.attr,
587 &sensor_dev_attr_temp1_crit.dev_attr.attr,
588 &sensor_dev_attr_temp2_crit.dev_attr.attr,
589 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
590 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
591
592 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
593 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400594 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200595 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
596 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
597 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
598 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
599 &dev_attr_alarms.attr,
600 NULL
601};
602
603static const struct attribute_group lm90_group = {
604 .attrs = lm90_attributes,
605};
606
Jean Delvarec3df5802005-10-26 21:39:40 +0200607/* pec used for ADM1032 only */
608static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
609 char *buf)
610{
611 struct i2c_client *client = to_i2c_client(dev);
612 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
613}
614
615static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
616 const char *buf, size_t count)
617{
618 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200619 long val;
620 int err;
621
622 err = strict_strtol(buf, 10, &val);
623 if (err < 0)
624 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200625
626 switch (val) {
627 case 0:
628 client->flags &= ~I2C_CLIENT_PEC;
629 break;
630 case 1:
631 client->flags |= I2C_CLIENT_PEC;
632 break;
633 default:
634 return -EINVAL;
635 }
636
637 return count;
638}
639
640static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * Real code
644 */
645
Guenter Roeck11e57812010-10-28 20:31:42 +0200646/*
647 * The ADM1032 supports PEC but not on write byte transactions, so we need
648 * to explicitly ask for a transaction without PEC.
649 */
Jean Delvarec3df5802005-10-26 21:39:40 +0200650static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
651{
652 return i2c_smbus_xfer(client->adapter, client->addr,
653 client->flags & ~I2C_CLIENT_PEC,
654 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
655}
656
Guenter Roeck11e57812010-10-28 20:31:42 +0200657/*
658 * It is assumed that client->update_lock is held (unless we are in
659 * detection or initialization steps). This matters when PEC is enabled,
660 * because we don't want the address pointer to change between the write
661 * byte and the read byte transactions.
662 */
663static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
Jean Delvare8256fe02005-10-26 21:37:52 +0200664{
665 int err;
666
Guenter Roeck11e57812010-10-28 20:31:42 +0200667 if (client->flags & I2C_CLIENT_PEC) {
668 err = adm1032_write_byte(client, reg);
669 if (err >= 0)
670 err = i2c_smbus_read_byte(client);
671 } else
672 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200673
674 if (err < 0) {
675 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
676 reg, err);
677 return err;
678 }
679 *value = err;
680
681 return 0;
682}
683
Jean Delvare9b0e8522008-07-16 19:30:15 +0200684/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100685static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200686 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200688 struct i2c_adapter *adapter = new_client->adapter;
689 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +0100690 const char *name = NULL;
691 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200694 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jean Delvare8f2fa772009-12-09 20:35:53 +0100696 /* detection and identification */
697 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100698 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100699 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100700 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100701 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100702 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100703 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100704 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100705 return -ENODEV;
706
707 if ((address == 0x4C || address == 0x4D)
708 && man_id == 0x01) { /* National Semiconductor */
709 int reg_config2;
710
711 reg_config2 = i2c_smbus_read_byte_data(new_client,
712 LM90_REG_R_CONFIG2);
713 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200714 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Jean Delvare8f2fa772009-12-09 20:35:53 +0100716 if ((reg_config1 & 0x2A) == 0x00
717 && (reg_config2 & 0xF8) == 0x00
718 && reg_convrate <= 0x09) {
719 if (address == 0x4C
720 && (chip_id & 0xF0) == 0x20) { /* LM90 */
721 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100723 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
724 name = "lm99";
725 dev_info(&adapter->dev,
726 "Assuming LM99 chip at 0x%02x\n",
727 address);
728 dev_info(&adapter->dev,
729 "If it is an LM89, instantiate it "
730 "with the new_device sysfs "
731 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400732 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100733 if (address == 0x4C
734 && (chip_id & 0xF0) == 0x10) { /* LM86 */
735 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100738 } else
739 if ((address == 0x4C || address == 0x4D)
740 && man_id == 0x41) { /* Analog Devices */
741 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
742 && (reg_config1 & 0x3F) == 0x00
743 && reg_convrate <= 0x0A) {
744 name = "adm1032";
745 /* The ADM1032 supports PEC, but only if combined
746 transactions are not used. */
747 if (i2c_check_functionality(adapter,
748 I2C_FUNC_SMBUS_BYTE))
749 info->flags |= I2C_CLIENT_PEC;
750 } else
751 if (chip_id == 0x51 /* ADT7461 */
752 && (reg_config1 & 0x1B) == 0x00
753 && reg_convrate <= 0x0A) {
754 name = "adt7461";
755 }
756 } else
757 if (man_id == 0x4D) { /* Maxim */
758 /*
759 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
760 * register. Reading from that address will return the last
761 * read value, which in our case is those of the man_id
762 * register. Likewise, the config1 register seems to lack a
763 * low nibble, so the value will be those of the previous
764 * read, so in our case those of the man_id register.
765 */
766 if (chip_id == man_id
767 && (address == 0x4C || address == 0x4D)
768 && (reg_config1 & 0x1F) == (man_id & 0x0F)
769 && reg_convrate <= 0x09) {
770 name = "max6657";
771 } else
772 /*
773 * The chip_id register of the MAX6680 and MAX6681 holds the
774 * revision of the chip. The lowest bit of the config1 register
775 * is unused and should return zero when read, so should the
776 * second to last bit of config1 (software reset).
777 */
778 if (chip_id == 0x01
779 && (reg_config1 & 0x03) == 0x00
780 && reg_convrate <= 0x07) {
781 name = "max6680";
782 } else
783 /*
784 * The chip_id register of the MAX6646/6647/6649 holds the
785 * revision of the chip. The lowest 6 bits of the config1
786 * register are unused and should return zero when read.
787 */
788 if (chip_id == 0x59
789 && (reg_config1 & 0x3f) == 0x00
790 && reg_convrate <= 0x07) {
791 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
Jean Delvare6771ea12010-03-05 22:17:13 +0100793 } else
794 if (address == 0x4C
795 && man_id == 0x5C) { /* Winbond/Nuvoton */
796 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
797 && (reg_config1 & 0x2A) == 0x00
798 && reg_convrate <= 0x08) {
799 name = "w83l771";
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Jean Delvare8f2fa772009-12-09 20:35:53 +0100803 if (!name) { /* identification failed */
804 dev_dbg(&adapter->dev,
805 "Unsupported chip at 0x%02x (man_id=0x%02X, "
806 "chip_id=0x%02X)\n", address, man_id, chip_id);
807 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100809
Jean Delvare9b0e8522008-07-16 19:30:15 +0200810 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Jean Delvare9b0e8522008-07-16 19:30:15 +0200812 return 0;
813}
814
815static int lm90_probe(struct i2c_client *new_client,
816 const struct i2c_device_id *id)
817{
818 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
819 struct lm90_data *data;
820 int err;
821
822 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
823 if (!data) {
824 err = -ENOMEM;
825 goto exit;
826 }
827 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100828 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Jean Delvare9b0e8522008-07-16 19:30:15 +0200830 /* Set the device type */
831 data->kind = id->driver_data;
832 if (data->kind == adm1032) {
833 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
834 new_client->flags &= ~I2C_CLIENT_PEC;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Jean Delvare53de3342010-03-05 22:17:15 +0100837 /* Different devices have different alarm bits triggering the
838 * ALERT# output */
839 switch (data->kind) {
840 case lm90:
841 case lm99:
842 case lm86:
843 data->alert_alarms = 0x7b;
844 break;
845 default:
846 data->alert_alarms = 0x7c;
847 break;
848 }
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* Initialize the LM90 chip */
851 lm90_init_client(new_client);
852
853 /* Register sysfs hooks */
Guenter Roeck11e57812010-10-28 20:31:42 +0200854 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
855 if (err)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200856 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200857 if (new_client->flags & I2C_CLIENT_PEC) {
Guenter Roeck11e57812010-10-28 20:31:42 +0200858 err = device_create_file(&new_client->dev, &dev_attr_pec);
859 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +0200860 goto exit_remove_files;
861 }
Ben Hutchings271dabf2008-10-17 17:51:11 +0200862 if (data->kind != max6657 && data->kind != max6646) {
Guenter Roeck11e57812010-10-28 20:31:42 +0200863 err = device_create_file(&new_client->dev,
864 &sensor_dev_attr_temp2_offset.dev_attr);
865 if (err)
Jean Delvare69f2f962007-09-05 14:15:37 +0200866 goto exit_remove_files;
867 }
Jean Delvare0e39e012006-09-24 21:16:40 +0200868
Tony Jones1beeffe2007-08-20 13:46:20 -0700869 data->hwmon_dev = hwmon_device_register(&new_client->dev);
870 if (IS_ERR(data->hwmon_dev)) {
871 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200872 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400873 }
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return 0;
876
Jean Delvare0e39e012006-09-24 21:16:40 +0200877exit_remove_files:
878 sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
879 device_remove_file(&new_client->dev, &dev_attr_pec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880exit_free:
881 kfree(data);
882exit:
883 return err;
884}
885
886static void lm90_init_client(struct i2c_client *client)
887{
Jean Delvare95238362010-03-05 22:17:14 +0100888 u8 config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400889 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /*
892 * Start the conversions.
893 */
894 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
895 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +0200896 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
897 dev_warn(&client->dev, "Initialization failed!\n");
898 return;
899 }
Jean Delvare95238362010-03-05 22:17:14 +0100900 data->config_orig = config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400901
Nate Case23b2d472008-10-17 17:51:10 +0200902 /* Check Temperature Range Select */
903 if (data->kind == adt7461) {
904 if (config & 0x04)
905 data->flags |= LM90_FLAG_ADT7461_EXT;
906 }
907
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400908 /*
909 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
910 * 0.125 degree resolution) and range (0x08, extend range
911 * to -64 degree) mode for the remote temperature sensor.
912 */
Guenter Roeck11e57812010-10-28 20:31:42 +0200913 if (data->kind == max6680)
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400914 config |= 0x18;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400915
916 config &= 0xBF; /* run */
Jean Delvare95238362010-03-05 22:17:14 +0100917 if (config != data->config_orig) /* Only write if changed */
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400918 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Jean Delvare9b0e8522008-07-16 19:30:15 +0200921static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400923 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Tony Jones1beeffe2007-08-20 13:46:20 -0700925 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200926 sysfs_remove_group(&client->dev.kobj, &lm90_group);
927 device_remove_file(&client->dev, &dev_attr_pec);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200928 if (data->kind != max6657 && data->kind != max6646)
Jean Delvare69f2f962007-09-05 14:15:37 +0200929 device_remove_file(&client->dev,
930 &sensor_dev_attr_temp2_offset.dev_attr);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400931
Jean Delvare95238362010-03-05 22:17:14 +0100932 /* Restore initial configuration */
933 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
934 data->config_orig);
935
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400936 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938}
939
Jean Delvare53de3342010-03-05 22:17:15 +0100940static void lm90_alert(struct i2c_client *client, unsigned int flag)
941{
942 struct lm90_data *data = i2c_get_clientdata(client);
943 u8 config, alarms;
944
945 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
946 if ((alarms & 0x7f) == 0) {
947 dev_info(&client->dev, "Everything OK\n");
948 } else {
949 if (alarms & 0x61)
950 dev_warn(&client->dev,
951 "temp%d out of range, please check!\n", 1);
952 if (alarms & 0x1a)
953 dev_warn(&client->dev,
954 "temp%d out of range, please check!\n", 2);
955 if (alarms & 0x04)
956 dev_warn(&client->dev,
957 "temp%d diode open, please check!\n", 2);
958
959 /* Disable ALERT# output, because these chips don't implement
960 SMBus alert correctly; they should only hold the alert line
961 low briefly. */
962 if ((data->kind == adm1032 || data->kind == adt7461)
963 && (alarms & data->alert_alarms)) {
964 dev_dbg(&client->dev, "Disabling ALERT#\n");
965 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
966 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
967 config | 0x80);
968 }
969 }
970}
971
Jean Delvare6388a382008-10-17 17:51:09 +0200972static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
973{
974 int err;
975 u8 oldh, newh, l;
976
977 /*
978 * There is a trick here. We have to read two registers to have the
979 * sensor temperature, but we have to beware a conversion could occur
980 * inbetween the readings. The datasheet says we should either use
981 * the one-shot conversion register, which we don't want to do
982 * (disables hardware monitoring) or monitor the busy bit, which is
983 * impossible (we can't read the values and monitor that bit at the
984 * exact same time). So the solution used here is to read the high
985 * byte once, then the low byte, then the high byte again. If the new
986 * high byte matches the old one, then we have a valid reading. Else
987 * we have to read the low byte again, and now we believe we have a
988 * correct reading.
989 */
990 if ((err = lm90_read_reg(client, regh, &oldh))
991 || (err = lm90_read_reg(client, regl, &l))
992 || (err = lm90_read_reg(client, regh, &newh)))
993 return err;
994 if (oldh != newh) {
995 err = lm90_read_reg(client, regl, &l);
996 if (err)
997 return err;
998 }
999 *value = (newh << 8) | l;
1000
1001 return 0;
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static struct lm90_data *lm90_update_device(struct device *dev)
1005{
1006 struct i2c_client *client = to_i2c_client(dev);
1007 struct lm90_data *data = i2c_get_clientdata(client);
1008
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001009 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Ira W. Snyder8c3c7a22010-05-27 19:58:44 +02001011 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
1012 || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +02001013 u8 h, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +02001016 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
1017 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
1018 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
1019 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +02001020 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Ben Hutchings271dabf2008-10-17 17:51:11 +02001022 if (data->kind == max6657 || data->kind == max6646) {
Jean Delvaref65e1702008-10-17 17:51:09 +02001023 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
1024 MAX6657_REG_R_LOCAL_TEMPL,
1025 &data->temp11[4]);
1026 } else {
1027 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
1028 &h) == 0)
1029 data->temp11[4] = h << 8;
1030 }
Jean Delvare6388a382008-10-17 17:51:09 +02001031 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
1032 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Jean Delvare5f502a82008-10-17 17:51:09 +02001034 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
1035 data->temp11[1] = h << 8;
1036 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +02001037 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +02001038 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
1039 &l) == 0)
1040 data->temp11[1] |= l;
1041 }
1042 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
1043 data->temp11[2] = h << 8;
1044 if (data->kind != max6657 && data->kind != max6680
Ben Hutchings271dabf2008-10-17 17:51:11 +02001045 && data->kind != max6646
Jean Delvare5f502a82008-10-17 17:51:09 +02001046 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
1047 &l) == 0)
1048 data->temp11[2] |= l;
1049 }
1050
Ben Hutchings271dabf2008-10-17 17:51:11 +02001051 if (data->kind != max6657 && data->kind != max6646) {
Jean Delvare69f2f962007-09-05 14:15:37 +02001052 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +02001053 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +02001054 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
1055 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +02001056 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +02001057 }
Jean Delvare8256fe02005-10-26 21:37:52 +02001058 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Jean Delvare53de3342010-03-05 22:17:15 +01001060 /* Re-enable ALERT# output if it was originally enabled and
1061 * relevant alarms are all clear */
1062 if ((data->config_orig & 0x80) == 0
1063 && (data->alarms & data->alert_alarms) == 0) {
1064 u8 config;
1065
1066 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1067 if (config & 0x80) {
1068 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1069 i2c_smbus_write_byte_data(client,
1070 LM90_REG_W_CONFIG1,
1071 config & ~0x80);
1072 }
1073 }
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 data->last_updated = jiffies;
1076 data->valid = 1;
1077 }
1078
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001079 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 return data;
1082}
1083
1084static int __init sensors_lm90_init(void)
1085{
1086 return i2c_add_driver(&lm90_driver);
1087}
1088
1089static void __exit sensors_lm90_exit(void)
1090{
1091 i2c_del_driver(&lm90_driver);
1092}
1093
1094MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1095MODULE_DESCRIPTION("LM90/ADM1032 driver");
1096MODULE_LICENSE("GPL");
1097
1098module_init(sensors_lm90_init);
1099module_exit(sensors_lm90_exit);