blob: 68ee8f78843ef9a6a10b5994a7b9285ac4197a73 [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.
Guenter Roeck13c84952010-10-28 20:31:43 +020088 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
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
Guenter Roeck13c84952010-10-28 20:31:43 +020096enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
97 max6646, 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 */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200145#define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
146/* Device features */
147#define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
148#define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
149#define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
Nate Case23b2d472008-10-17 17:51:10 +0200150
151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Functions declaration
153 */
154
Jean Delvare310ec792009-12-14 21:17:23 +0100155static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200156static int lm90_probe(struct i2c_client *client,
157 const struct i2c_device_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void lm90_init_client(struct i2c_client *client);
Jean Delvare53de3342010-03-05 22:17:15 +0100159static void lm90_alert(struct i2c_client *client, unsigned int flag);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200160static int lm90_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static struct lm90_data *lm90_update_device(struct device *dev);
162
163/*
164 * Driver data (common to all clients)
165 */
166
Jean Delvare9b0e8522008-07-16 19:30:15 +0200167static const struct i2c_device_id lm90_id[] = {
168 { "adm1032", adm1032 },
169 { "adt7461", adt7461 },
170 { "lm90", lm90 },
171 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100172 { "lm89", lm86 },
173 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200174 { "max6646", max6646 },
175 { "max6647", max6646 },
176 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200177 { "max6657", max6657 },
178 { "max6658", max6657 },
Guenter Roeck13c84952010-10-28 20:31:43 +0200179 { "max6659", max6659 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200180 { "max6680", max6680 },
181 { "max6681", max6680 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100182 { "w83l771", w83l771 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200183 { }
184};
185MODULE_DEVICE_TABLE(i2c, lm90_id);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static struct i2c_driver lm90_driver = {
Jean Delvare9b0e8522008-07-16 19:30:15 +0200188 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100189 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100190 .name = "lm90",
191 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200192 .probe = lm90_probe,
193 .remove = lm90_remove,
Jean Delvare53de3342010-03-05 22:17:15 +0100194 .alert = lm90_alert,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200195 .id_table = lm90_id,
196 .detect = lm90_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100197 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200/*
201 * Client data (each client gets its own)
202 */
203
204struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700205 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100206 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 char valid; /* zero until following fields are valid */
208 unsigned long last_updated; /* in jiffies */
209 int kind;
Nate Case23b2d472008-10-17 17:51:10 +0200210 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jean Delvare95238362010-03-05 22:17:14 +0100212 u8 config_orig; /* Original configuration register value */
Jean Delvare53de3342010-03-05 22:17:15 +0100213 u8 alert_alarms; /* Which alarm bits trigger ALERT# */
Jean Delvare95238362010-03-05 22:17:14 +0100214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* registers values */
Jean Delvaref65e1702008-10-17 17:51:09 +0200216 s8 temp8[4]; /* 0: local low limit
217 1: local high limit
218 2: local critical limit
219 3: remote critical limit */
220 s16 temp11[5]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200221 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200222 2: remote high limit
Guenter Roeck13c84952010-10-28 20:31:43 +0200223 3: remote offset (except max6646 and max6657/58/59)
Jean Delvaref65e1702008-10-17 17:51:09 +0200224 4: local input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 u8 temp_hyst;
226 u8 alarms; /* bitvector */
227};
228
229/*
Nate Casecea50fe2008-10-17 17:51:10 +0200230 * Conversions
231 * For local temperatures and limits, critical limits and the hysteresis
232 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
233 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200234 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
235 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200236 */
237
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200238static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200239{
240 return val * 1000;
241}
242
Ben Hutchings271dabf2008-10-17 17:51:11 +0200243static inline int temp_from_u8(u8 val)
244{
245 return val * 1000;
246}
247
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200248static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200249{
250 return val / 32 * 125;
251}
252
Ben Hutchings271dabf2008-10-17 17:51:11 +0200253static inline int temp_from_u16(u16 val)
254{
255 return val / 32 * 125;
256}
257
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200258static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200259{
260 if (val <= -128000)
261 return -128;
262 if (val >= 127000)
263 return 127;
264 if (val < 0)
265 return (val - 500) / 1000;
266 return (val + 500) / 1000;
267}
268
Ben Hutchings271dabf2008-10-17 17:51:11 +0200269static u8 temp_to_u8(long val)
270{
271 if (val <= 0)
272 return 0;
273 if (val >= 255000)
274 return 255;
275 return (val + 500) / 1000;
276}
277
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200278static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200279{
280 if (val <= -128000)
281 return 0x8000;
282 if (val >= 127875)
283 return 0x7FE0;
284 if (val < 0)
285 return (val - 62) / 125 * 32;
286 return (val + 62) / 125 * 32;
287}
288
289static u8 hyst_to_reg(long val)
290{
291 if (val <= 0)
292 return 0;
293 if (val >= 30500)
294 return 31;
295 return (val + 500) / 1000;
296}
297
298/*
Nate Case23b2d472008-10-17 17:51:10 +0200299 * ADT7461 in compatibility mode is almost identical to LM90 except that
300 * attempts to write values that are outside the range 0 < temp < 127 are
301 * treated as the boundary value.
302 *
303 * ADT7461 in "extended mode" operation uses unsigned integers offset by
304 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200305 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200306static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200307{
Nate Case23b2d472008-10-17 17:51:10 +0200308 if (data->flags & LM90_FLAG_ADT7461_EXT)
309 return (val - 64) * 1000;
310 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200311 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200312}
313
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200314static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200315{
Nate Case23b2d472008-10-17 17:51:10 +0200316 if (data->flags & LM90_FLAG_ADT7461_EXT)
317 return (val - 0x4000) / 64 * 250;
318 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200319 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200320}
321
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200322static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200323{
324 if (data->flags & LM90_FLAG_ADT7461_EXT) {
325 if (val <= -64000)
326 return 0;
327 if (val >= 191000)
328 return 0xFF;
329 return (val + 500 + 64000) / 1000;
330 } else {
331 if (val <= 0)
332 return 0;
333 if (val >= 127000)
334 return 127;
335 return (val + 500) / 1000;
336 }
337}
338
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200339static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200340{
341 if (data->flags & LM90_FLAG_ADT7461_EXT) {
342 if (val <= -64000)
343 return 0;
344 if (val >= 191750)
345 return 0xFFC0;
346 return (val + 64000 + 125) / 250 * 64;
347 } else {
348 if (val <= 0)
349 return 0;
350 if (val >= 127750)
351 return 0x7FC0;
352 return (val + 125) / 250 * 64;
353 }
Nate Casecea50fe2008-10-17 17:51:10 +0200354}
355
356/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * Sysfs stuff
358 */
359
Jean Delvare30d73942005-06-05 21:27:28 +0200360static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
361 char *buf)
362{
363 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
364 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200365 int temp;
366
367 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200368 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200369 else if (data->kind == max6646)
370 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200371 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200372 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200373
Jean Delvare97ae60b2008-10-26 17:04:39 +0100374 /* +16 degrees offset for temp2 for the LM99 */
375 if (data->kind == lm99 && attr->index == 3)
376 temp += 16000;
377
Nate Case23b2d472008-10-17 17:51:10 +0200378 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jean Delvare30d73942005-06-05 21:27:28 +0200381static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
382 const char *buf, size_t count)
383{
384 static const u8 reg[4] = {
385 LM90_REG_W_LOCAL_LOW,
386 LM90_REG_W_LOCAL_HIGH,
387 LM90_REG_W_LOCAL_CRIT,
388 LM90_REG_W_REMOTE_CRIT,
389 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Jean Delvare30d73942005-06-05 21:27:28 +0200391 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
392 struct i2c_client *client = to_i2c_client(dev);
393 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200394 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200395 long val;
396 int err;
397
398 err = strict_strtol(buf, 10, &val);
399 if (err < 0)
400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jean Delvare97ae60b2008-10-26 17:04:39 +0100402 /* +16 degrees offset for temp2 for the LM99 */
403 if (data->kind == lm99 && attr->index == 3)
404 val -= 16000;
405
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200407 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200408 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200409 else if (data->kind == max6646)
410 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200411 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200412 data->temp8[nr] = temp_to_s8(val);
Jean Delvaref65e1702008-10-17 17:51:09 +0200413 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100414 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200415 return count;
416}
417
418static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
419 char *buf)
420{
Guenter Roeck96512862010-10-28 20:31:43 +0200421 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200422 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200423 int temp;
424
425 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200426 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200427 else if (data->kind == max6646)
428 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200429 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200430 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200431
Jean Delvare97ae60b2008-10-26 17:04:39 +0100432 /* +16 degrees offset for temp2 for the LM99 */
433 if (data->kind == lm99 && attr->index <= 2)
434 temp += 16000;
435
Nate Case23b2d472008-10-17 17:51:10 +0200436 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200437}
438
439static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
440 const char *buf, size_t count)
441{
Guenter Roeck96512862010-10-28 20:31:43 +0200442 struct {
443 u8 high;
444 u8 low;
445 } reg[3] = {
446 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
447 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
448 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL }
Jean Delvare30d73942005-06-05 21:27:28 +0200449 };
450
Guenter Roeck96512862010-10-28 20:31:43 +0200451 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200452 struct i2c_client *client = to_i2c_client(dev);
453 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck96512862010-10-28 20:31:43 +0200454 int nr = attr->nr;
455 int index = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200456 long val;
457 int err;
458
459 err = strict_strtol(buf, 10, &val);
460 if (err < 0)
461 return err;
Jean Delvare30d73942005-06-05 21:27:28 +0200462
Jean Delvare97ae60b2008-10-26 17:04:39 +0100463 /* +16 degrees offset for temp2 for the LM99 */
Guenter Roeck96512862010-10-28 20:31:43 +0200464 if (data->kind == lm99 && index <= 2)
Jean Delvare97ae60b2008-10-26 17:04:39 +0100465 val -= 16000;
466
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100467 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200468 if (data->kind == adt7461)
Guenter Roeck96512862010-10-28 20:31:43 +0200469 data->temp11[index] = temp_to_u16_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200470 else if (data->kind == max6646)
Guenter Roeck96512862010-10-28 20:31:43 +0200471 data->temp11[index] = temp_to_u8(val) << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +0200472 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200473 data->temp11[index] = temp_to_s16(val);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200474 else
Guenter Roeck96512862010-10-28 20:31:43 +0200475 data->temp11[index] = temp_to_s8(val) << 8;
Jean Delvare5f502a82008-10-17 17:51:09 +0200476
Guenter Roeck96512862010-10-28 20:31:43 +0200477 i2c_smbus_write_byte_data(client, reg[nr].high,
478 data->temp11[index] >> 8);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200479 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200480 i2c_smbus_write_byte_data(client, reg[nr].low,
481 data->temp11[index] & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100482 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200483 return count;
484}
485
Guenter Roeck11e57812010-10-28 20:31:42 +0200486static ssize_t show_temphyst(struct device *dev,
487 struct device_attribute *devattr,
Jean Delvare30d73942005-06-05 21:27:28 +0200488 char *buf)
489{
490 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
491 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200492 int temp;
493
494 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200495 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100496 else if (data->kind == max6646)
497 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200498 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200499 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200500
Jean Delvare97ae60b2008-10-26 17:04:39 +0100501 /* +16 degrees offset for temp2 for the LM99 */
502 if (data->kind == lm99 && attr->index == 3)
503 temp += 16000;
504
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200505 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200506}
507
508static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
509 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct i2c_client *client = to_i2c_client(dev);
512 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck11e57812010-10-28 20:31:42 +0200513 long val;
514 int err;
Jean Delvareec38fa22008-10-26 17:04:39 +0100515 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Guenter Roeck11e57812010-10-28 20:31:42 +0200517 err = strict_strtol(buf, 10, &val);
518 if (err < 0)
519 return err;
520
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100521 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100522 if (data->kind == adt7461)
523 temp = temp_from_u8_adt7461(data, data->temp8[2]);
524 else if (data->kind == max6646)
525 temp = temp_from_u8(data->temp8[2]);
526 else
527 temp = temp_from_s8(data->temp8[2]);
528
529 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100531 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100532 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return count;
534}
535
Jean Delvare30d73942005-06-05 21:27:28 +0200536static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
537 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct lm90_data *data = lm90_update_device(dev);
540 return sprintf(buf, "%d\n", data->alarms);
541}
542
Jean Delvare2d457712006-09-24 20:52:15 +0200543static ssize_t show_alarm(struct device *dev, struct device_attribute
544 *devattr, char *buf)
545{
546 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
547 struct lm90_data *data = lm90_update_device(dev);
548 int bitnr = attr->index;
549
550 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
551}
552
Guenter Roeck96512862010-10-28 20:31:43 +0200553static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
554static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200555static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200556 set_temp8, 0);
Guenter Roeck96512862010-10-28 20:31:43 +0200557static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
558 set_temp11, 0, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200559static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200560 set_temp8, 1);
Guenter Roeck96512862010-10-28 20:31:43 +0200561static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
562 set_temp11, 1, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200563static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200564 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200565static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200566 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200567static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200568 set_temphyst, 2);
569static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Guenter Roeck96512862010-10-28 20:31:43 +0200570static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
571 set_temp11, 2, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200572
573/* Individual alarm files */
574static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
575static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400576static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200577static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
578static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
579static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
580static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
581/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
583
Jean Delvare0e39e012006-09-24 21:16:40 +0200584static struct attribute *lm90_attributes[] = {
585 &sensor_dev_attr_temp1_input.dev_attr.attr,
586 &sensor_dev_attr_temp2_input.dev_attr.attr,
587 &sensor_dev_attr_temp1_min.dev_attr.attr,
588 &sensor_dev_attr_temp2_min.dev_attr.attr,
589 &sensor_dev_attr_temp1_max.dev_attr.attr,
590 &sensor_dev_attr_temp2_max.dev_attr.attr,
591 &sensor_dev_attr_temp1_crit.dev_attr.attr,
592 &sensor_dev_attr_temp2_crit.dev_attr.attr,
593 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
594 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
595
596 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
597 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400598 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200599 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
600 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
601 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
602 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
603 &dev_attr_alarms.attr,
604 NULL
605};
606
607static const struct attribute_group lm90_group = {
608 .attrs = lm90_attributes,
609};
610
Jean Delvarec3df5802005-10-26 21:39:40 +0200611/* pec used for ADM1032 only */
612static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
613 char *buf)
614{
615 struct i2c_client *client = to_i2c_client(dev);
616 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
617}
618
619static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
620 const char *buf, size_t count)
621{
622 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200623 long val;
624 int err;
625
626 err = strict_strtol(buf, 10, &val);
627 if (err < 0)
628 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200629
630 switch (val) {
631 case 0:
632 client->flags &= ~I2C_CLIENT_PEC;
633 break;
634 case 1:
635 client->flags |= I2C_CLIENT_PEC;
636 break;
637 default:
638 return -EINVAL;
639 }
640
641 return count;
642}
643
644static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/*
647 * Real code
648 */
649
Guenter Roeck11e57812010-10-28 20:31:42 +0200650/*
651 * The ADM1032 supports PEC but not on write byte transactions, so we need
652 * to explicitly ask for a transaction without PEC.
653 */
Jean Delvarec3df5802005-10-26 21:39:40 +0200654static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
655{
656 return i2c_smbus_xfer(client->adapter, client->addr,
657 client->flags & ~I2C_CLIENT_PEC,
658 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
659}
660
Guenter Roeck11e57812010-10-28 20:31:42 +0200661/*
662 * It is assumed that client->update_lock is held (unless we are in
663 * detection or initialization steps). This matters when PEC is enabled,
664 * because we don't want the address pointer to change between the write
665 * byte and the read byte transactions.
666 */
667static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
Jean Delvare8256fe02005-10-26 21:37:52 +0200668{
669 int err;
670
Guenter Roeck11e57812010-10-28 20:31:42 +0200671 if (client->flags & I2C_CLIENT_PEC) {
672 err = adm1032_write_byte(client, reg);
673 if (err >= 0)
674 err = i2c_smbus_read_byte(client);
675 } else
676 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200677
678 if (err < 0) {
679 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
680 reg, err);
681 return err;
682 }
683 *value = err;
684
685 return 0;
686}
687
Jean Delvare9b0e8522008-07-16 19:30:15 +0200688/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100689static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200690 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200692 struct i2c_adapter *adapter = new_client->adapter;
693 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +0100694 const char *name = NULL;
695 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200698 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Jean Delvare8f2fa772009-12-09 20:35:53 +0100700 /* detection and identification */
701 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100702 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100703 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100704 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100705 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100706 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100707 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100708 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100709 return -ENODEV;
710
711 if ((address == 0x4C || address == 0x4D)
712 && man_id == 0x01) { /* National Semiconductor */
713 int reg_config2;
714
715 reg_config2 = i2c_smbus_read_byte_data(new_client,
716 LM90_REG_R_CONFIG2);
717 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200718 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jean Delvare8f2fa772009-12-09 20:35:53 +0100720 if ((reg_config1 & 0x2A) == 0x00
721 && (reg_config2 & 0xF8) == 0x00
722 && reg_convrate <= 0x09) {
723 if (address == 0x4C
724 && (chip_id & 0xF0) == 0x20) { /* LM90 */
725 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100727 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
728 name = "lm99";
729 dev_info(&adapter->dev,
730 "Assuming LM99 chip at 0x%02x\n",
731 address);
732 dev_info(&adapter->dev,
733 "If it is an LM89, instantiate it "
734 "with the new_device sysfs "
735 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400736 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100737 if (address == 0x4C
738 && (chip_id & 0xF0) == 0x10) { /* LM86 */
739 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100742 } else
743 if ((address == 0x4C || address == 0x4D)
744 && man_id == 0x41) { /* Analog Devices */
745 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
746 && (reg_config1 & 0x3F) == 0x00
747 && reg_convrate <= 0x0A) {
748 name = "adm1032";
749 /* The ADM1032 supports PEC, but only if combined
750 transactions are not used. */
751 if (i2c_check_functionality(adapter,
752 I2C_FUNC_SMBUS_BYTE))
753 info->flags |= I2C_CLIENT_PEC;
754 } else
755 if (chip_id == 0x51 /* ADT7461 */
756 && (reg_config1 & 0x1B) == 0x00
757 && reg_convrate <= 0x0A) {
758 name = "adt7461";
759 }
760 } else
761 if (man_id == 0x4D) { /* Maxim */
762 /*
763 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
764 * register. Reading from that address will return the last
765 * read value, which in our case is those of the man_id
766 * register. Likewise, the config1 register seems to lack a
767 * low nibble, so the value will be those of the previous
768 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +0200769 * MAX6659 has a third set of upper temperature limit registers.
770 * Those registers also return values on MAX6657 and MAX6658,
771 * thus the only way to detect MAX6659 is by its address.
772 * For this reason it will be mis-detected as MAX6657 if its
773 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +0100774 */
775 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +0200776 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100777 && (reg_config1 & 0x1F) == (man_id & 0x0F)
778 && reg_convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +0200779 if (address == 0x4C)
780 name = "max6657";
781 else
782 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +0100783 } else
784 /*
785 * The chip_id register of the MAX6680 and MAX6681 holds the
786 * revision of the chip. The lowest bit of the config1 register
787 * is unused and should return zero when read, so should the
788 * second to last bit of config1 (software reset).
789 */
790 if (chip_id == 0x01
791 && (reg_config1 & 0x03) == 0x00
792 && reg_convrate <= 0x07) {
793 name = "max6680";
794 } else
795 /*
796 * The chip_id register of the MAX6646/6647/6649 holds the
797 * revision of the chip. The lowest 6 bits of the config1
798 * register are unused and should return zero when read.
799 */
800 if (chip_id == 0x59
801 && (reg_config1 & 0x3f) == 0x00
802 && reg_convrate <= 0x07) {
803 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Jean Delvare6771ea12010-03-05 22:17:13 +0100805 } else
806 if (address == 0x4C
807 && man_id == 0x5C) { /* Winbond/Nuvoton */
808 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
809 && (reg_config1 & 0x2A) == 0x00
810 && reg_convrate <= 0x08) {
811 name = "w83l771";
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
Jean Delvare8f2fa772009-12-09 20:35:53 +0100815 if (!name) { /* identification failed */
816 dev_dbg(&adapter->dev,
817 "Unsupported chip at 0x%02x (man_id=0x%02X, "
818 "chip_id=0x%02X)\n", address, man_id, chip_id);
819 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100821
Jean Delvare9b0e8522008-07-16 19:30:15 +0200822 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Jean Delvare9b0e8522008-07-16 19:30:15 +0200824 return 0;
825}
826
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +0200827static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
828{
829 if (data->flags & LM90_HAVE_OFFSET)
830 device_remove_file(&client->dev,
831 &sensor_dev_attr_temp2_offset.dev_attr);
832 device_remove_file(&client->dev, &dev_attr_pec);
833 sysfs_remove_group(&client->dev.kobj, &lm90_group);
834}
835
Jean Delvare9b0e8522008-07-16 19:30:15 +0200836static int lm90_probe(struct i2c_client *new_client,
837 const struct i2c_device_id *id)
838{
839 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
840 struct lm90_data *data;
841 int err;
842
843 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
844 if (!data) {
845 err = -ENOMEM;
846 goto exit;
847 }
848 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100849 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Jean Delvare9b0e8522008-07-16 19:30:15 +0200851 /* Set the device type */
852 data->kind = id->driver_data;
853 if (data->kind == adm1032) {
854 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
855 new_client->flags &= ~I2C_CLIENT_PEC;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Jean Delvare53de3342010-03-05 22:17:15 +0100858 /* Different devices have different alarm bits triggering the
859 * ALERT# output */
860 switch (data->kind) {
861 case lm90:
862 case lm99:
863 case lm86:
864 data->alert_alarms = 0x7b;
865 break;
866 default:
867 data->alert_alarms = 0x7c;
868 break;
869 }
870
Guenter Roeck88073bb2010-10-28 20:31:43 +0200871 /* Set chip capabilities */
Guenter Roeck13c84952010-10-28 20:31:43 +0200872 if (data->kind != max6657 && data->kind != max6659
873 && data->kind != max6646)
Guenter Roeck88073bb2010-10-28 20:31:43 +0200874 data->flags |= LM90_HAVE_OFFSET;
875
Guenter Roeck13c84952010-10-28 20:31:43 +0200876 if (data->kind == max6657 || data->kind == max6659
877 || data->kind == max6646)
Guenter Roeck88073bb2010-10-28 20:31:43 +0200878 data->flags |= LM90_HAVE_LOCAL_EXT;
879
Guenter Roeck13c84952010-10-28 20:31:43 +0200880 if (data->kind != max6657 && data->kind != max6659
881 && data->kind != max6646 && data->kind != max6680)
Guenter Roeck88073bb2010-10-28 20:31:43 +0200882 data->flags |= LM90_HAVE_REM_LIMIT_EXT;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* Initialize the LM90 chip */
885 lm90_init_client(new_client);
886
887 /* Register sysfs hooks */
Guenter Roeck11e57812010-10-28 20:31:42 +0200888 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
889 if (err)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200890 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +0200891 if (new_client->flags & I2C_CLIENT_PEC) {
Guenter Roeck11e57812010-10-28 20:31:42 +0200892 err = device_create_file(&new_client->dev, &dev_attr_pec);
893 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +0200894 goto exit_remove_files;
895 }
Guenter Roeck88073bb2010-10-28 20:31:43 +0200896 if (data->flags & LM90_HAVE_OFFSET) {
Guenter Roeck11e57812010-10-28 20:31:42 +0200897 err = device_create_file(&new_client->dev,
898 &sensor_dev_attr_temp2_offset.dev_attr);
899 if (err)
Jean Delvare69f2f962007-09-05 14:15:37 +0200900 goto exit_remove_files;
901 }
Jean Delvare0e39e012006-09-24 21:16:40 +0200902
Tony Jones1beeffe2007-08-20 13:46:20 -0700903 data->hwmon_dev = hwmon_device_register(&new_client->dev);
904 if (IS_ERR(data->hwmon_dev)) {
905 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200906 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400907 }
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return 0;
910
Jean Delvare0e39e012006-09-24 21:16:40 +0200911exit_remove_files:
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +0200912 lm90_remove_files(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913exit_free:
914 kfree(data);
915exit:
916 return err;
917}
918
919static void lm90_init_client(struct i2c_client *client)
920{
Jean Delvare95238362010-03-05 22:17:14 +0100921 u8 config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400922 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /*
925 * Start the conversions.
926 */
927 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
928 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +0200929 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
930 dev_warn(&client->dev, "Initialization failed!\n");
931 return;
932 }
Jean Delvare95238362010-03-05 22:17:14 +0100933 data->config_orig = config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400934
Nate Case23b2d472008-10-17 17:51:10 +0200935 /* Check Temperature Range Select */
936 if (data->kind == adt7461) {
937 if (config & 0x04)
938 data->flags |= LM90_FLAG_ADT7461_EXT;
939 }
940
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400941 /*
942 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
943 * 0.125 degree resolution) and range (0x08, extend range
944 * to -64 degree) mode for the remote temperature sensor.
945 */
Guenter Roeck11e57812010-10-28 20:31:42 +0200946 if (data->kind == max6680)
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400947 config |= 0x18;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400948
949 config &= 0xBF; /* run */
Jean Delvare95238362010-03-05 22:17:14 +0100950 if (config != data->config_orig) /* Only write if changed */
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400951 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Jean Delvare9b0e8522008-07-16 19:30:15 +0200954static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400956 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Tony Jones1beeffe2007-08-20 13:46:20 -0700958 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +0200959 lm90_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400960
Jean Delvare95238362010-03-05 22:17:14 +0100961 /* Restore initial configuration */
962 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
963 data->config_orig);
964
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400965 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
967}
968
Jean Delvare53de3342010-03-05 22:17:15 +0100969static void lm90_alert(struct i2c_client *client, unsigned int flag)
970{
971 struct lm90_data *data = i2c_get_clientdata(client);
972 u8 config, alarms;
973
974 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
975 if ((alarms & 0x7f) == 0) {
976 dev_info(&client->dev, "Everything OK\n");
977 } else {
978 if (alarms & 0x61)
979 dev_warn(&client->dev,
980 "temp%d out of range, please check!\n", 1);
981 if (alarms & 0x1a)
982 dev_warn(&client->dev,
983 "temp%d out of range, please check!\n", 2);
984 if (alarms & 0x04)
985 dev_warn(&client->dev,
986 "temp%d diode open, please check!\n", 2);
987
988 /* Disable ALERT# output, because these chips don't implement
989 SMBus alert correctly; they should only hold the alert line
990 low briefly. */
991 if ((data->kind == adm1032 || data->kind == adt7461)
992 && (alarms & data->alert_alarms)) {
993 dev_dbg(&client->dev, "Disabling ALERT#\n");
994 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
995 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
996 config | 0x80);
997 }
998 }
999}
1000
Jean Delvare6388a382008-10-17 17:51:09 +02001001static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
1002{
1003 int err;
1004 u8 oldh, newh, l;
1005
1006 /*
1007 * There is a trick here. We have to read two registers to have the
1008 * sensor temperature, but we have to beware a conversion could occur
1009 * inbetween the readings. The datasheet says we should either use
1010 * the one-shot conversion register, which we don't want to do
1011 * (disables hardware monitoring) or monitor the busy bit, which is
1012 * impossible (we can't read the values and monitor that bit at the
1013 * exact same time). So the solution used here is to read the high
1014 * byte once, then the low byte, then the high byte again. If the new
1015 * high byte matches the old one, then we have a valid reading. Else
1016 * we have to read the low byte again, and now we believe we have a
1017 * correct reading.
1018 */
1019 if ((err = lm90_read_reg(client, regh, &oldh))
1020 || (err = lm90_read_reg(client, regl, &l))
1021 || (err = lm90_read_reg(client, regh, &newh)))
1022 return err;
1023 if (oldh != newh) {
1024 err = lm90_read_reg(client, regl, &l);
1025 if (err)
1026 return err;
1027 }
1028 *value = (newh << 8) | l;
1029
1030 return 0;
1031}
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033static struct lm90_data *lm90_update_device(struct device *dev)
1034{
1035 struct i2c_client *client = to_i2c_client(dev);
1036 struct lm90_data *data = i2c_get_clientdata(client);
1037
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001038 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Ira W. Snyder8c3c7a22010-05-27 19:58:44 +02001040 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
1041 || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +02001042 u8 h, l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +02001045 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
1046 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
1047 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
1048 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +02001049 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Guenter Roeck88073bb2010-10-28 20:31:43 +02001051 if (data->flags & LM90_HAVE_LOCAL_EXT) {
Jean Delvaref65e1702008-10-17 17:51:09 +02001052 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
1053 MAX6657_REG_R_LOCAL_TEMPL,
1054 &data->temp11[4]);
1055 } else {
1056 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
1057 &h) == 0)
1058 data->temp11[4] = h << 8;
1059 }
Jean Delvare6388a382008-10-17 17:51:09 +02001060 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
1061 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Jean Delvare5f502a82008-10-17 17:51:09 +02001063 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
1064 data->temp11[1] = h << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +02001065 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
Jean Delvare5f502a82008-10-17 17:51:09 +02001066 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
1067 &l) == 0)
1068 data->temp11[1] |= l;
1069 }
1070 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
1071 data->temp11[2] = h << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +02001072 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
Jean Delvare5f502a82008-10-17 17:51:09 +02001073 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
1074 &l) == 0)
1075 data->temp11[2] |= l;
1076 }
1077
Guenter Roeck88073bb2010-10-28 20:31:43 +02001078 if (data->flags & LM90_HAVE_OFFSET) {
Jean Delvare69f2f962007-09-05 14:15:37 +02001079 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +02001080 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +02001081 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
1082 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +02001083 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +02001084 }
Jean Delvare8256fe02005-10-26 21:37:52 +02001085 lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Jean Delvare53de3342010-03-05 22:17:15 +01001087 /* Re-enable ALERT# output if it was originally enabled and
1088 * relevant alarms are all clear */
1089 if ((data->config_orig & 0x80) == 0
1090 && (data->alarms & data->alert_alarms) == 0) {
1091 u8 config;
1092
1093 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1094 if (config & 0x80) {
1095 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1096 i2c_smbus_write_byte_data(client,
1097 LM90_REG_W_CONFIG1,
1098 config & ~0x80);
1099 }
1100 }
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 data->last_updated = jiffies;
1103 data->valid = 1;
1104 }
1105
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001106 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 return data;
1109}
1110
1111static int __init sensors_lm90_init(void)
1112{
1113 return i2c_add_driver(&lm90_driver);
1114}
1115
1116static void __exit sensors_lm90_exit(void)
1117{
1118 i2c_del_driver(&lm90_driver);
1119}
1120
1121MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1122MODULE_DESCRIPTION("LM90/ADM1032 driver");
1123MODULE_LICENSE("GPL");
1124
1125module_init(sensors_lm90_init);
1126module_exit(sensors_lm90_exit);