blob: 366bb624e655628bd646e44f4d030f7f411016d5 [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
Guenter Roeck69487082010-10-28 20:31:43 +020031 * variants. We use the device address to detect MAX6659, which will result
32 * in a detection as max6657 if it is on address 0x4c. The extra address
33 * and features of the MAX6659 are only supported if the chip is configured
34 * explicitly as max6659, or if its address is not 0x4c.
35 * These chips lack the remote temperature offset feature.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
Darrick J. Wong1a51e062009-03-12 13:36:38 +010037 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
38 * MAX6692 chips made by Maxim. These are again similar to the LM86,
39 * but they use unsigned temperature values and can report temperatures
40 * from 0 to 145 degrees.
Ben Hutchings271dabf2008-10-17 17:51:11 +020041 *
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040042 * This driver also supports the MAX6680 and MAX6681, two other sensor
43 * chips made by Maxim. These are quite similar to the other Maxim
Jean Delvarea874a102008-10-17 17:51:10 +020044 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
45 * be treated identically.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040046 *
Guenter Roeck06e1c0a2010-10-28 20:31:43 +020047 * This driver also supports the MAX6695 and MAX6696, two other sensor
48 * chips made by Maxim. These are also quite similar to other Maxim
49 * chips, but support three temperature sensors instead of two. MAX6695
50 * and MAX6696 only differ in the pinout so they can be treated identically.
51 *
Nate Case23b2d472008-10-17 17:51:10 +020052 * This driver also supports the ADT7461 chip from Analog Devices.
53 * It's supported in both compatibility and extended mode. It is mostly
54 * compatible with LM90 except for a data format difference for the
55 * temperature value registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 *
57 * Since the LM90 was the first chipset supported by this driver, most
58 * comments will refer to this chipset, but are actually general and
59 * concern all supported chipsets, unless mentioned otherwise.
60 *
61 * This program is free software; you can redistribute it and/or modify
62 * it under the terms of the GNU General Public License as published by
63 * the Free Software Foundation; either version 2 of the License, or
64 * (at your option) any later version.
65 *
66 * This program is distributed in the hope that it will be useful,
67 * but WITHOUT ANY WARRANTY; without even the implied warranty of
68 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69 * GNU General Public License for more details.
70 *
71 * You should have received a copy of the GNU General Public License
72 * along with this program; if not, write to the Free Software
73 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
74 */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/module.h>
77#include <linux/init.h>
78#include <linux/slab.h>
79#include <linux/jiffies.h>
80#include <linux/i2c.h>
Jean Delvare10c08f82005-06-06 19:34:45 +020081#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040082#include <linux/hwmon.h>
83#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010084#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020085#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Addresses to scan
89 * Address is fully defined internally and cannot be changed except for
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040090 * MAX6659, MAX6680 and MAX6681.
Ben Hutchings271dabf2008-10-17 17:51:11 +020091 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
92 * and MAX6658 have address 0x4c.
93 * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
94 * MAX6647 has address 0x4e.
Guenter Roeck13c84952010-10-28 20:31:43 +020095 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040096 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
97 * 0x4c, 0x4d or 0x4e.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
99
Mark M. Hoffman25e9c862008-02-17 22:28:03 -0500100static const unsigned short normal_i2c[] = {
101 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Guenter Roeck13c84952010-10-28 20:31:43 +0200103enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200104 max6646, w83l771, max6696 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * The LM90 registers
108 */
109
110#define LM90_REG_R_MAN_ID 0xFE
111#define LM90_REG_R_CHIP_ID 0xFF
112#define LM90_REG_R_CONFIG1 0x03
113#define LM90_REG_W_CONFIG1 0x09
114#define LM90_REG_R_CONFIG2 0xBF
115#define LM90_REG_W_CONFIG2 0xBF
116#define LM90_REG_R_CONVRATE 0x04
117#define LM90_REG_W_CONVRATE 0x0A
118#define LM90_REG_R_STATUS 0x02
119#define LM90_REG_R_LOCAL_TEMP 0x00
120#define LM90_REG_R_LOCAL_HIGH 0x05
121#define LM90_REG_W_LOCAL_HIGH 0x0B
122#define LM90_REG_R_LOCAL_LOW 0x06
123#define LM90_REG_W_LOCAL_LOW 0x0C
124#define LM90_REG_R_LOCAL_CRIT 0x20
125#define LM90_REG_W_LOCAL_CRIT 0x20
126#define LM90_REG_R_REMOTE_TEMPH 0x01
127#define LM90_REG_R_REMOTE_TEMPL 0x10
128#define LM90_REG_R_REMOTE_OFFSH 0x11
129#define LM90_REG_W_REMOTE_OFFSH 0x11
130#define LM90_REG_R_REMOTE_OFFSL 0x12
131#define LM90_REG_W_REMOTE_OFFSL 0x12
132#define LM90_REG_R_REMOTE_HIGHH 0x07
133#define LM90_REG_W_REMOTE_HIGHH 0x0D
134#define LM90_REG_R_REMOTE_HIGHL 0x13
135#define LM90_REG_W_REMOTE_HIGHL 0x13
136#define LM90_REG_R_REMOTE_LOWH 0x08
137#define LM90_REG_W_REMOTE_LOWH 0x0E
138#define LM90_REG_R_REMOTE_LOWL 0x14
139#define LM90_REG_W_REMOTE_LOWL 0x14
140#define LM90_REG_R_REMOTE_CRIT 0x19
141#define LM90_REG_W_REMOTE_CRIT 0x19
142#define LM90_REG_R_TCRIT_HYST 0x21
143#define LM90_REG_W_TCRIT_HYST 0x21
144
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200145/* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200146
147#define MAX6657_REG_R_LOCAL_TEMPL 0x11
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200148#define MAX6696_REG_R_STATUS2 0x12
Guenter Roeck69487082010-10-28 20:31:43 +0200149#define MAX6659_REG_R_REMOTE_EMERG 0x16
150#define MAX6659_REG_W_REMOTE_EMERG 0x16
151#define MAX6659_REG_R_LOCAL_EMERG 0x17
152#define MAX6659_REG_W_LOCAL_EMERG 0x17
Jean Delvaref65e1702008-10-17 17:51:09 +0200153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Nate Case23b2d472008-10-17 17:51:10 +0200155 * Device flags
156 */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200157#define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
158/* Device features */
159#define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
160#define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
161#define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
Guenter Roeck69487082010-10-28 20:31:43 +0200162#define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200163#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
164#define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
Nate Case23b2d472008-10-17 17:51:10 +0200165
166/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * Functions declaration
168 */
169
Jean Delvare310ec792009-12-14 21:17:23 +0100170static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200171static int lm90_probe(struct i2c_client *client,
172 const struct i2c_device_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static void lm90_init_client(struct i2c_client *client);
Jean Delvare53de3342010-03-05 22:17:15 +0100174static void lm90_alert(struct i2c_client *client, unsigned int flag);
Jean Delvare9b0e8522008-07-16 19:30:15 +0200175static int lm90_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static struct lm90_data *lm90_update_device(struct device *dev);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200177static inline void lm90_select_remote_channel(struct i2c_client *client,
178 struct lm90_data *data,
179 int channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/*
182 * Driver data (common to all clients)
183 */
184
Jean Delvare9b0e8522008-07-16 19:30:15 +0200185static const struct i2c_device_id lm90_id[] = {
186 { "adm1032", adm1032 },
187 { "adt7461", adt7461 },
188 { "lm90", lm90 },
189 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100190 { "lm89", lm86 },
191 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200192 { "max6646", max6646 },
193 { "max6647", max6646 },
194 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200195 { "max6657", max6657 },
196 { "max6658", max6657 },
Guenter Roeck13c84952010-10-28 20:31:43 +0200197 { "max6659", max6659 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200198 { "max6680", max6680 },
199 { "max6681", max6680 },
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200200 { "max6695", max6696 },
201 { "max6696", max6696 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100202 { "w83l771", w83l771 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200203 { }
204};
205MODULE_DEVICE_TABLE(i2c, lm90_id);
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static struct i2c_driver lm90_driver = {
Jean Delvare9b0e8522008-07-16 19:30:15 +0200208 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100209 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100210 .name = "lm90",
211 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200212 .probe = lm90_probe,
213 .remove = lm90_remove,
Jean Delvare53de3342010-03-05 22:17:15 +0100214 .alert = lm90_alert,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200215 .id_table = lm90_id,
216 .detect = lm90_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100217 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
220/*
221 * Client data (each client gets its own)
222 */
223
224struct lm90_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700225 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100226 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 char valid; /* zero until following fields are valid */
228 unsigned long last_updated; /* in jiffies */
229 int kind;
Nate Case23b2d472008-10-17 17:51:10 +0200230 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jean Delvare95238362010-03-05 22:17:14 +0100232 u8 config_orig; /* Original configuration register value */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200233 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
234 /* Upper 8 bits for max6695/96 */
Jean Delvare95238362010-03-05 22:17:14 +0100235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* registers values */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200237 s8 temp8[8]; /* 0: local low limit
Jean Delvaref65e1702008-10-17 17:51:09 +0200238 1: local high limit
239 2: local critical limit
Guenter Roeck69487082010-10-28 20:31:43 +0200240 3: remote critical limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200241 4: local emergency limit (max6659 and max6695/96)
242 5: remote emergency limit (max6659 and max6695/96)
243 6: remote 2 critical limit (max6695/96 only)
244 7: remote 2 emergency limit (max6695/96 only) */
245 s16 temp11[8]; /* 0: remote input
Jean Delvare30d73942005-06-05 21:27:28 +0200246 1: remote low limit
Jean Delvare69f2f962007-09-05 14:15:37 +0200247 2: remote high limit
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200248 3: remote offset (except max6646, max6657/58/59,
249 and max6695/96)
250 4: local input
251 5: remote 2 input (max6695/96 only)
252 6: remote 2 low limit (max6695/96 only)
253 7: remote 2 high limit (ma6695/96 only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 u8 temp_hyst;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200255 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
258/*
Nate Casecea50fe2008-10-17 17:51:10 +0200259 * Conversions
260 * For local temperatures and limits, critical limits and the hysteresis
261 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
262 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200263 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
264 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200265 */
266
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200267static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200268{
269 return val * 1000;
270}
271
Ben Hutchings271dabf2008-10-17 17:51:11 +0200272static inline int temp_from_u8(u8 val)
273{
274 return val * 1000;
275}
276
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200277static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200278{
279 return val / 32 * 125;
280}
281
Ben Hutchings271dabf2008-10-17 17:51:11 +0200282static inline int temp_from_u16(u16 val)
283{
284 return val / 32 * 125;
285}
286
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200287static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200288{
289 if (val <= -128000)
290 return -128;
291 if (val >= 127000)
292 return 127;
293 if (val < 0)
294 return (val - 500) / 1000;
295 return (val + 500) / 1000;
296}
297
Ben Hutchings271dabf2008-10-17 17:51:11 +0200298static u8 temp_to_u8(long val)
299{
300 if (val <= 0)
301 return 0;
302 if (val >= 255000)
303 return 255;
304 return (val + 500) / 1000;
305}
306
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200307static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200308{
309 if (val <= -128000)
310 return 0x8000;
311 if (val >= 127875)
312 return 0x7FE0;
313 if (val < 0)
314 return (val - 62) / 125 * 32;
315 return (val + 62) / 125 * 32;
316}
317
318static u8 hyst_to_reg(long val)
319{
320 if (val <= 0)
321 return 0;
322 if (val >= 30500)
323 return 31;
324 return (val + 500) / 1000;
325}
326
327/*
Nate Case23b2d472008-10-17 17:51:10 +0200328 * ADT7461 in compatibility mode is almost identical to LM90 except that
329 * attempts to write values that are outside the range 0 < temp < 127 are
330 * treated as the boundary value.
331 *
332 * ADT7461 in "extended mode" operation uses unsigned integers offset by
333 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200334 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200335static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200336{
Nate Case23b2d472008-10-17 17:51:10 +0200337 if (data->flags & LM90_FLAG_ADT7461_EXT)
338 return (val - 64) * 1000;
339 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200340 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200341}
342
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200343static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200344{
Nate Case23b2d472008-10-17 17:51:10 +0200345 if (data->flags & LM90_FLAG_ADT7461_EXT)
346 return (val - 0x4000) / 64 * 250;
347 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200348 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200349}
350
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200351static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200352{
353 if (data->flags & LM90_FLAG_ADT7461_EXT) {
354 if (val <= -64000)
355 return 0;
356 if (val >= 191000)
357 return 0xFF;
358 return (val + 500 + 64000) / 1000;
359 } else {
360 if (val <= 0)
361 return 0;
362 if (val >= 127000)
363 return 127;
364 return (val + 500) / 1000;
365 }
366}
367
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200368static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200369{
370 if (data->flags & LM90_FLAG_ADT7461_EXT) {
371 if (val <= -64000)
372 return 0;
373 if (val >= 191750)
374 return 0xFFC0;
375 return (val + 64000 + 125) / 250 * 64;
376 } else {
377 if (val <= 0)
378 return 0;
379 if (val >= 127750)
380 return 0x7FC0;
381 return (val + 125) / 250 * 64;
382 }
Nate Casecea50fe2008-10-17 17:51:10 +0200383}
384
385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * Sysfs stuff
387 */
388
Jean Delvare30d73942005-06-05 21:27:28 +0200389static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
390 char *buf)
391{
392 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
393 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200394 int temp;
395
396 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200397 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200398 else if (data->kind == max6646)
399 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200400 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200401 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200402
Jean Delvare97ae60b2008-10-26 17:04:39 +0100403 /* +16 degrees offset for temp2 for the LM99 */
404 if (data->kind == lm99 && attr->index == 3)
405 temp += 16000;
406
Nate Case23b2d472008-10-17 17:51:10 +0200407 return sprintf(buf, "%d\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jean Delvare30d73942005-06-05 21:27:28 +0200410static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
411 const char *buf, size_t count)
412{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200413 static const u8 reg[8] = {
Jean Delvare30d73942005-06-05 21:27:28 +0200414 LM90_REG_W_LOCAL_LOW,
415 LM90_REG_W_LOCAL_HIGH,
416 LM90_REG_W_LOCAL_CRIT,
417 LM90_REG_W_REMOTE_CRIT,
Guenter Roeck69487082010-10-28 20:31:43 +0200418 MAX6659_REG_W_LOCAL_EMERG,
419 MAX6659_REG_W_REMOTE_EMERG,
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200420 LM90_REG_W_REMOTE_CRIT,
421 MAX6659_REG_W_REMOTE_EMERG,
Jean Delvare30d73942005-06-05 21:27:28 +0200422 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Jean Delvare30d73942005-06-05 21:27:28 +0200424 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
425 struct i2c_client *client = to_i2c_client(dev);
426 struct lm90_data *data = i2c_get_clientdata(client);
Jean Delvare30d73942005-06-05 21:27:28 +0200427 int nr = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200428 long val;
429 int err;
430
431 err = strict_strtol(buf, 10, &val);
432 if (err < 0)
433 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Jean Delvare97ae60b2008-10-26 17:04:39 +0100435 /* +16 degrees offset for temp2 for the LM99 */
436 if (data->kind == lm99 && attr->index == 3)
437 val -= 16000;
438
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100439 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200440 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200441 data->temp8[nr] = temp_to_u8_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200442 else if (data->kind == max6646)
443 data->temp8[nr] = temp_to_u8(val);
Jean Delvare30d73942005-06-05 21:27:28 +0200444 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200445 data->temp8[nr] = temp_to_s8(val);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200446
447 lm90_select_remote_channel(client, data, nr >= 6);
Jean Delvaref65e1702008-10-17 17:51:09 +0200448 i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200449 lm90_select_remote_channel(client, data, 0);
450
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100451 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200452 return count;
453}
454
455static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
456 char *buf)
457{
Guenter Roeck96512862010-10-28 20:31:43 +0200458 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200459 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200460 int temp;
461
462 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200463 temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200464 else if (data->kind == max6646)
465 temp = temp_from_u16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200466 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200467 temp = temp_from_s16(data->temp11[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200468
Jean Delvare97ae60b2008-10-26 17:04:39 +0100469 /* +16 degrees offset for temp2 for the LM99 */
470 if (data->kind == lm99 && attr->index <= 2)
471 temp += 16000;
472
Nate Case23b2d472008-10-17 17:51:10 +0200473 return sprintf(buf, "%d\n", temp);
Jean Delvare30d73942005-06-05 21:27:28 +0200474}
475
476static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
477 const char *buf, size_t count)
478{
Guenter Roeck96512862010-10-28 20:31:43 +0200479 struct {
480 u8 high;
481 u8 low;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200482 int channel;
483 } reg[5] = {
484 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
485 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
486 { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
487 { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
488 { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
Jean Delvare30d73942005-06-05 21:27:28 +0200489 };
490
Guenter Roeck96512862010-10-28 20:31:43 +0200491 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
Jean Delvare30d73942005-06-05 21:27:28 +0200492 struct i2c_client *client = to_i2c_client(dev);
493 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck96512862010-10-28 20:31:43 +0200494 int nr = attr->nr;
495 int index = attr->index;
Guenter Roeck11e57812010-10-28 20:31:42 +0200496 long val;
497 int err;
498
499 err = strict_strtol(buf, 10, &val);
500 if (err < 0)
501 return err;
Jean Delvare30d73942005-06-05 21:27:28 +0200502
Jean Delvare97ae60b2008-10-26 17:04:39 +0100503 /* +16 degrees offset for temp2 for the LM99 */
Guenter Roeck96512862010-10-28 20:31:43 +0200504 if (data->kind == lm99 && index <= 2)
Jean Delvare97ae60b2008-10-26 17:04:39 +0100505 val -= 16000;
506
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100507 mutex_lock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200508 if (data->kind == adt7461)
Guenter Roeck96512862010-10-28 20:31:43 +0200509 data->temp11[index] = temp_to_u16_adt7461(data, val);
Ben Hutchings271dabf2008-10-17 17:51:11 +0200510 else if (data->kind == max6646)
Guenter Roeck96512862010-10-28 20:31:43 +0200511 data->temp11[index] = temp_to_u8(val) << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +0200512 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200513 data->temp11[index] = temp_to_s16(val);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200514 else
Guenter Roeck96512862010-10-28 20:31:43 +0200515 data->temp11[index] = temp_to_s8(val) << 8;
Jean Delvare5f502a82008-10-17 17:51:09 +0200516
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200517 lm90_select_remote_channel(client, data, reg[nr].channel);
Guenter Roeck96512862010-10-28 20:31:43 +0200518 i2c_smbus_write_byte_data(client, reg[nr].high,
519 data->temp11[index] >> 8);
Guenter Roeck88073bb2010-10-28 20:31:43 +0200520 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
Guenter Roeck96512862010-10-28 20:31:43 +0200521 i2c_smbus_write_byte_data(client, reg[nr].low,
522 data->temp11[index] & 0xff);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200523 lm90_select_remote_channel(client, data, 0);
524
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100525 mutex_unlock(&data->update_lock);
Jean Delvare30d73942005-06-05 21:27:28 +0200526 return count;
527}
528
Guenter Roeck11e57812010-10-28 20:31:42 +0200529static ssize_t show_temphyst(struct device *dev,
530 struct device_attribute *devattr,
Jean Delvare30d73942005-06-05 21:27:28 +0200531 char *buf)
532{
533 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
534 struct lm90_data *data = lm90_update_device(dev);
Nate Case23b2d472008-10-17 17:51:10 +0200535 int temp;
536
537 if (data->kind == adt7461)
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200538 temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
Jean Delvareec38fa22008-10-26 17:04:39 +0100539 else if (data->kind == max6646)
540 temp = temp_from_u8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200541 else
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200542 temp = temp_from_s8(data->temp8[attr->index]);
Nate Case23b2d472008-10-17 17:51:10 +0200543
Jean Delvare97ae60b2008-10-26 17:04:39 +0100544 /* +16 degrees offset for temp2 for the LM99 */
545 if (data->kind == lm99 && attr->index == 3)
546 temp += 16000;
547
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200548 return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
Jean Delvare30d73942005-06-05 21:27:28 +0200549}
550
551static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
552 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct i2c_client *client = to_i2c_client(dev);
555 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck11e57812010-10-28 20:31:42 +0200556 long val;
557 int err;
Jean Delvareec38fa22008-10-26 17:04:39 +0100558 int temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Guenter Roeck11e57812010-10-28 20:31:42 +0200560 err = strict_strtol(buf, 10, &val);
561 if (err < 0)
562 return err;
563
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100564 mutex_lock(&data->update_lock);
Jean Delvareec38fa22008-10-26 17:04:39 +0100565 if (data->kind == adt7461)
566 temp = temp_from_u8_adt7461(data, data->temp8[2]);
567 else if (data->kind == max6646)
568 temp = temp_from_u8(data->temp8[2]);
569 else
570 temp = temp_from_s8(data->temp8[2]);
571
572 data->temp_hyst = hyst_to_reg(temp - val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
Jean Delvareec38fa22008-10-26 17:04:39 +0100574 data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100575 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return count;
577}
578
Jean Delvare30d73942005-06-05 21:27:28 +0200579static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
580 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct lm90_data *data = lm90_update_device(dev);
583 return sprintf(buf, "%d\n", data->alarms);
584}
585
Jean Delvare2d457712006-09-24 20:52:15 +0200586static ssize_t show_alarm(struct device *dev, struct device_attribute
587 *devattr, char *buf)
588{
589 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
590 struct lm90_data *data = lm90_update_device(dev);
591 int bitnr = attr->index;
592
593 return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
594}
595
Guenter Roeck96512862010-10-28 20:31:43 +0200596static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
597static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
Jean Delvare30d73942005-06-05 21:27:28 +0200598static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200599 set_temp8, 0);
Guenter Roeck96512862010-10-28 20:31:43 +0200600static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
601 set_temp11, 0, 1);
Jean Delvare30d73942005-06-05 21:27:28 +0200602static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200603 set_temp8, 1);
Guenter Roeck96512862010-10-28 20:31:43 +0200604static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
605 set_temp11, 1, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200606static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200607 set_temp8, 2);
Jean Delvare30d73942005-06-05 21:27:28 +0200608static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
Jean Delvaref65e1702008-10-17 17:51:09 +0200609 set_temp8, 3);
Jean Delvare30d73942005-06-05 21:27:28 +0200610static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
Jean Delvaref65e1702008-10-17 17:51:09 +0200611 set_temphyst, 2);
612static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
Guenter Roeck96512862010-10-28 20:31:43 +0200613static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
614 set_temp11, 2, 3);
Jean Delvare2d457712006-09-24 20:52:15 +0200615
616/* Individual alarm files */
617static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
618static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
Jean Delvare7817a392007-06-09 10:11:16 -0400619static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
Jean Delvare2d457712006-09-24 20:52:15 +0200620static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
621static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
622static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
623static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
624/* Raw alarm file for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
626
Jean Delvare0e39e012006-09-24 21:16:40 +0200627static struct attribute *lm90_attributes[] = {
628 &sensor_dev_attr_temp1_input.dev_attr.attr,
629 &sensor_dev_attr_temp2_input.dev_attr.attr,
630 &sensor_dev_attr_temp1_min.dev_attr.attr,
631 &sensor_dev_attr_temp2_min.dev_attr.attr,
632 &sensor_dev_attr_temp1_max.dev_attr.attr,
633 &sensor_dev_attr_temp2_max.dev_attr.attr,
634 &sensor_dev_attr_temp1_crit.dev_attr.attr,
635 &sensor_dev_attr_temp2_crit.dev_attr.attr,
636 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
637 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
638
639 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
640 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
Jean Delvare7817a392007-06-09 10:11:16 -0400641 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200642 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
643 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
644 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
645 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
646 &dev_attr_alarms.attr,
647 NULL
648};
649
650static const struct attribute_group lm90_group = {
651 .attrs = lm90_attributes,
652};
653
Guenter Roeck69487082010-10-28 20:31:43 +0200654/*
655 * Additional attributes for devices with emergency sensors
656 */
657static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
658 set_temp8, 4);
659static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
660 set_temp8, 5);
661static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
662 NULL, 4);
663static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
664 NULL, 5);
665
666static struct attribute *lm90_emergency_attributes[] = {
667 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
668 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
669 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
670 &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
671 NULL
672};
673
674static const struct attribute_group lm90_emergency_group = {
675 .attrs = lm90_emergency_attributes,
676};
677
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200678static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
679static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
680
681static struct attribute *lm90_emergency_alarm_attributes[] = {
682 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
683 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
684 NULL
685};
686
687static const struct attribute_group lm90_emergency_alarm_group = {
688 .attrs = lm90_emergency_alarm_attributes,
689};
690
691/*
692 * Additional attributes for devices with 3 temperature sensors
693 */
694static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
695static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
696 set_temp11, 3, 6);
697static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
698 set_temp11, 4, 7);
699static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
700 set_temp8, 6);
701static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
702static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
703 set_temp8, 7);
704static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
705 NULL, 7);
706
707static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
708static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
709static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
710static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
711static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
712
713static struct attribute *lm90_temp3_attributes[] = {
714 &sensor_dev_attr_temp3_input.dev_attr.attr,
715 &sensor_dev_attr_temp3_min.dev_attr.attr,
716 &sensor_dev_attr_temp3_max.dev_attr.attr,
717 &sensor_dev_attr_temp3_crit.dev_attr.attr,
718 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
719 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
720 &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
721
722 &sensor_dev_attr_temp3_fault.dev_attr.attr,
723 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
724 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
725 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
726 &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
727 NULL
728};
729
730static const struct attribute_group lm90_temp3_group = {
731 .attrs = lm90_temp3_attributes,
732};
733
Jean Delvarec3df5802005-10-26 21:39:40 +0200734/* pec used for ADM1032 only */
735static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
736 char *buf)
737{
738 struct i2c_client *client = to_i2c_client(dev);
739 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
740}
741
742static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
743 const char *buf, size_t count)
744{
745 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200746 long val;
747 int err;
748
749 err = strict_strtol(buf, 10, &val);
750 if (err < 0)
751 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200752
753 switch (val) {
754 case 0:
755 client->flags &= ~I2C_CLIENT_PEC;
756 break;
757 case 1:
758 client->flags |= I2C_CLIENT_PEC;
759 break;
760 default:
761 return -EINVAL;
762 }
763
764 return count;
765}
766
767static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * Real code
771 */
772
Guenter Roeck11e57812010-10-28 20:31:42 +0200773/*
774 * The ADM1032 supports PEC but not on write byte transactions, so we need
775 * to explicitly ask for a transaction without PEC.
776 */
Jean Delvarec3df5802005-10-26 21:39:40 +0200777static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
778{
779 return i2c_smbus_xfer(client->adapter, client->addr,
780 client->flags & ~I2C_CLIENT_PEC,
781 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
782}
783
Guenter Roeck11e57812010-10-28 20:31:42 +0200784/*
785 * It is assumed that client->update_lock is held (unless we are in
786 * detection or initialization steps). This matters when PEC is enabled,
787 * because we don't want the address pointer to change between the write
788 * byte and the read byte transactions.
789 */
790static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
Jean Delvare8256fe02005-10-26 21:37:52 +0200791{
792 int err;
793
Guenter Roeck11e57812010-10-28 20:31:42 +0200794 if (client->flags & I2C_CLIENT_PEC) {
795 err = adm1032_write_byte(client, reg);
796 if (err >= 0)
797 err = i2c_smbus_read_byte(client);
798 } else
799 err = i2c_smbus_read_byte_data(client, reg);
Jean Delvare8256fe02005-10-26 21:37:52 +0200800
801 if (err < 0) {
802 dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
803 reg, err);
804 return err;
805 }
806 *value = err;
807
808 return 0;
809}
810
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200811/*
812 * client->update_lock must be held when calling this function (unless we are
813 * in detection or initialization steps), and while a remote channel other
814 * than channel 0 is selected. Also, calling code must make sure to re-select
815 * external channel 0 before releasing the lock. This is necessary because
816 * various registers have different meanings as a result of selecting a
817 * non-default remote channel.
818 */
819static inline void lm90_select_remote_channel(struct i2c_client *client,
820 struct lm90_data *data,
821 int channel)
822{
823 u8 config;
824
825 if (data->kind == max6696) {
826 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
827 config &= ~0x08;
828 if (channel)
829 config |= 0x08;
830 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
831 config);
832 }
833}
834
Jean Delvare9b0e8522008-07-16 19:30:15 +0200835/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100836static int lm90_detect(struct i2c_client *new_client,
Jean Delvare9b0e8522008-07-16 19:30:15 +0200837 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Jean Delvare9b0e8522008-07-16 19:30:15 +0200839 struct i2c_adapter *adapter = new_client->adapter;
840 int address = new_client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +0100841 const char *name = NULL;
842 int man_id, chip_id, reg_config1, reg_convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +0200845 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Jean Delvare8f2fa772009-12-09 20:35:53 +0100847 /* detection and identification */
848 if ((man_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100849 LM90_REG_R_MAN_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100850 || (chip_id = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100851 LM90_REG_R_CHIP_ID)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100852 || (reg_config1 = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100853 LM90_REG_R_CONFIG1)) < 0
Jean Delvare8f2fa772009-12-09 20:35:53 +0100854 || (reg_convrate = i2c_smbus_read_byte_data(new_client,
Jean Delvaree0ae87a2007-11-25 21:58:21 +0100855 LM90_REG_R_CONVRATE)) < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100856 return -ENODEV;
857
858 if ((address == 0x4C || address == 0x4D)
859 && man_id == 0x01) { /* National Semiconductor */
860 int reg_config2;
861
862 reg_config2 = i2c_smbus_read_byte_data(new_client,
863 LM90_REG_R_CONFIG2);
864 if (reg_config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +0200865 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Jean Delvare8f2fa772009-12-09 20:35:53 +0100867 if ((reg_config1 & 0x2A) == 0x00
868 && (reg_config2 & 0xF8) == 0x00
869 && reg_convrate <= 0x09) {
870 if (address == 0x4C
871 && (chip_id & 0xF0) == 0x20) { /* LM90 */
872 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100874 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
875 name = "lm99";
876 dev_info(&adapter->dev,
877 "Assuming LM99 chip at 0x%02x\n",
878 address);
879 dev_info(&adapter->dev,
880 "If it is an LM89, instantiate it "
881 "with the new_device sysfs "
882 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -0400883 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +0100884 if (address == 0x4C
885 && (chip_id & 0xF0) == 0x10) { /* LM86 */
886 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 }
Jean Delvare8f2fa772009-12-09 20:35:53 +0100889 } else
890 if ((address == 0x4C || address == 0x4D)
891 && man_id == 0x41) { /* Analog Devices */
892 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
893 && (reg_config1 & 0x3F) == 0x00
894 && reg_convrate <= 0x0A) {
895 name = "adm1032";
896 /* The ADM1032 supports PEC, but only if combined
897 transactions are not used. */
898 if (i2c_check_functionality(adapter,
899 I2C_FUNC_SMBUS_BYTE))
900 info->flags |= I2C_CLIENT_PEC;
901 } else
902 if (chip_id == 0x51 /* ADT7461 */
903 && (reg_config1 & 0x1B) == 0x00
904 && reg_convrate <= 0x0A) {
905 name = "adt7461";
906 }
907 } else
908 if (man_id == 0x4D) { /* Maxim */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200909 int reg_emerg, reg_emerg2, reg_status2;
910
911 /*
912 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
913 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
914 * exists, both readings will reflect the same value. Otherwise,
915 * the readings will be different.
916 */
917 if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
918 MAX6659_REG_R_REMOTE_EMERG)) < 0
919 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
920 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
921 MAX6659_REG_R_REMOTE_EMERG)) < 0
922 || (reg_status2 = i2c_smbus_read_byte_data(new_client,
923 MAX6696_REG_R_STATUS2)) < 0)
924 return -ENODEV;
925
Jean Delvare8f2fa772009-12-09 20:35:53 +0100926 /*
927 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
928 * register. Reading from that address will return the last
929 * read value, which in our case is those of the man_id
930 * register. Likewise, the config1 register seems to lack a
931 * low nibble, so the value will be those of the previous
932 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +0200933 * MAX6659 has a third set of upper temperature limit registers.
934 * Those registers also return values on MAX6657 and MAX6658,
935 * thus the only way to detect MAX6659 is by its address.
936 * For this reason it will be mis-detected as MAX6657 if its
937 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +0100938 */
939 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +0200940 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvare8f2fa772009-12-09 20:35:53 +0100941 && (reg_config1 & 0x1F) == (man_id & 0x0F)
942 && reg_convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +0200943 if (address == 0x4C)
944 name = "max6657";
945 else
946 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +0100947 } else
948 /*
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200949 * Even though MAX6695 and MAX6696 do not have a chip ID
950 * register, reading it returns 0x01. Bit 4 of the config1
951 * register is unused and should return zero when read. Bit 0 of
952 * the status2 register is unused and should return zero when
953 * read.
954 *
955 * MAX6695 and MAX6696 have an additional set of temperature
956 * limit registers. We can detect those chips by checking if
957 * one of those registers exists.
958 */
959 if (chip_id == 0x01
960 && (reg_config1 & 0x10) == 0x00
961 && (reg_status2 & 0x01) == 0x00
962 && reg_emerg == reg_emerg2
963 && reg_convrate <= 0x07) {
964 name = "max6696";
965 } else
966 /*
Jean Delvare8f2fa772009-12-09 20:35:53 +0100967 * The chip_id register of the MAX6680 and MAX6681 holds the
968 * revision of the chip. The lowest bit of the config1 register
969 * is unused and should return zero when read, so should the
970 * second to last bit of config1 (software reset).
971 */
972 if (chip_id == 0x01
973 && (reg_config1 & 0x03) == 0x00
974 && reg_convrate <= 0x07) {
975 name = "max6680";
976 } else
977 /*
978 * The chip_id register of the MAX6646/6647/6649 holds the
979 * revision of the chip. The lowest 6 bits of the config1
980 * register are unused and should return zero when read.
981 */
982 if (chip_id == 0x59
983 && (reg_config1 & 0x3f) == 0x00
984 && reg_convrate <= 0x07) {
985 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Jean Delvare6771ea12010-03-05 22:17:13 +0100987 } else
988 if (address == 0x4C
989 && man_id == 0x5C) { /* Winbond/Nuvoton */
990 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
991 && (reg_config1 & 0x2A) == 0x00
992 && reg_convrate <= 0x08) {
993 name = "w83l771";
994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
Jean Delvare8f2fa772009-12-09 20:35:53 +0100997 if (!name) { /* identification failed */
998 dev_dbg(&adapter->dev,
999 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1000 "chip_id=0x%02X)\n", address, man_id, chip_id);
1001 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001003
Jean Delvare9b0e8522008-07-16 19:30:15 +02001004 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Jean Delvare9b0e8522008-07-16 19:30:15 +02001006 return 0;
1007}
1008
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001009static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
1010{
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001011 if (data->flags & LM90_HAVE_TEMP3)
1012 sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
1013 if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
1014 sysfs_remove_group(&client->dev.kobj,
1015 &lm90_emergency_alarm_group);
Guenter Roeck69487082010-10-28 20:31:43 +02001016 if (data->flags & LM90_HAVE_EMERGENCY)
1017 sysfs_remove_group(&client->dev.kobj,
1018 &lm90_emergency_group);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001019 if (data->flags & LM90_HAVE_OFFSET)
1020 device_remove_file(&client->dev,
1021 &sensor_dev_attr_temp2_offset.dev_attr);
1022 device_remove_file(&client->dev, &dev_attr_pec);
1023 sysfs_remove_group(&client->dev.kobj, &lm90_group);
1024}
1025
Jean Delvare9b0e8522008-07-16 19:30:15 +02001026static int lm90_probe(struct i2c_client *new_client,
1027 const struct i2c_device_id *id)
1028{
1029 struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
1030 struct lm90_data *data;
1031 int err;
1032
1033 data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
1034 if (!data) {
1035 err = -ENOMEM;
1036 goto exit;
1037 }
1038 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001039 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jean Delvare9b0e8522008-07-16 19:30:15 +02001041 /* Set the device type */
1042 data->kind = id->driver_data;
1043 if (data->kind == adm1032) {
1044 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
1045 new_client->flags &= ~I2C_CLIENT_PEC;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Jean Delvare53de3342010-03-05 22:17:15 +01001048 /* Different devices have different alarm bits triggering the
1049 * ALERT# output */
1050 switch (data->kind) {
1051 case lm90:
1052 case lm99:
1053 case lm86:
1054 data->alert_alarms = 0x7b;
1055 break;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001056 case max6696:
1057 data->alert_alarms = 0x187c;
1058 break;
Jean Delvare53de3342010-03-05 22:17:15 +01001059 default:
1060 data->alert_alarms = 0x7c;
1061 break;
1062 }
1063
Guenter Roeck88073bb2010-10-28 20:31:43 +02001064 /* Set chip capabilities */
Guenter Roeck13c84952010-10-28 20:31:43 +02001065 if (data->kind != max6657 && data->kind != max6659
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001066 && data->kind != max6646 && data->kind != max6696)
Guenter Roeck88073bb2010-10-28 20:31:43 +02001067 data->flags |= LM90_HAVE_OFFSET;
1068
Guenter Roeck13c84952010-10-28 20:31:43 +02001069 if (data->kind == max6657 || data->kind == max6659
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001070 || data->kind == max6646 || data->kind == max6696)
Guenter Roeck88073bb2010-10-28 20:31:43 +02001071 data->flags |= LM90_HAVE_LOCAL_EXT;
1072
Guenter Roeck13c84952010-10-28 20:31:43 +02001073 if (data->kind != max6657 && data->kind != max6659
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001074 && data->kind != max6646 && data->kind != max6680
1075 && data->kind != max6696)
Guenter Roeck88073bb2010-10-28 20:31:43 +02001076 data->flags |= LM90_HAVE_REM_LIMIT_EXT;
1077
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001078 if (data->kind == max6659 || data->kind == max6696)
Guenter Roeck69487082010-10-28 20:31:43 +02001079 data->flags |= LM90_HAVE_EMERGENCY;
1080
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001081 if (data->kind == max6696)
1082 data->flags |= LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* Initialize the LM90 chip */
1085 lm90_init_client(new_client);
1086
1087 /* Register sysfs hooks */
Guenter Roeck11e57812010-10-28 20:31:42 +02001088 err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
1089 if (err)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001090 goto exit_free;
Jean Delvare0e39e012006-09-24 21:16:40 +02001091 if (new_client->flags & I2C_CLIENT_PEC) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001092 err = device_create_file(&new_client->dev, &dev_attr_pec);
1093 if (err)
Jean Delvare0e39e012006-09-24 21:16:40 +02001094 goto exit_remove_files;
1095 }
Guenter Roeck88073bb2010-10-28 20:31:43 +02001096 if (data->flags & LM90_HAVE_OFFSET) {
Guenter Roeck11e57812010-10-28 20:31:42 +02001097 err = device_create_file(&new_client->dev,
1098 &sensor_dev_attr_temp2_offset.dev_attr);
1099 if (err)
Jean Delvare69f2f962007-09-05 14:15:37 +02001100 goto exit_remove_files;
1101 }
Guenter Roeck69487082010-10-28 20:31:43 +02001102 if (data->flags & LM90_HAVE_EMERGENCY) {
1103 err = sysfs_create_group(&new_client->dev.kobj,
1104 &lm90_emergency_group);
1105 if (err)
1106 goto exit_remove_files;
1107 }
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001108 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1109 err = sysfs_create_group(&new_client->dev.kobj,
1110 &lm90_emergency_alarm_group);
1111 if (err)
1112 goto exit_remove_files;
1113 }
1114 if (data->flags & LM90_HAVE_TEMP3) {
1115 err = sysfs_create_group(&new_client->dev.kobj,
1116 &lm90_temp3_group);
1117 if (err)
1118 goto exit_remove_files;
1119 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001120
Tony Jones1beeffe2007-08-20 13:46:20 -07001121 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1122 if (IS_ERR(data->hwmon_dev)) {
1123 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001124 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001125 }
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 0;
1128
Jean Delvare0e39e012006-09-24 21:16:40 +02001129exit_remove_files:
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001130 lm90_remove_files(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131exit_free:
1132 kfree(data);
1133exit:
1134 return err;
1135}
1136
1137static void lm90_init_client(struct i2c_client *client)
1138{
Jean Delvare95238362010-03-05 22:17:14 +01001139 u8 config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001140 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /*
1143 * Start the conversions.
1144 */
1145 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1146 5); /* 2 Hz */
Jean Delvare8256fe02005-10-26 21:37:52 +02001147 if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
1148 dev_warn(&client->dev, "Initialization failed!\n");
1149 return;
1150 }
Jean Delvare95238362010-03-05 22:17:14 +01001151 data->config_orig = config;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001152
Nate Case23b2d472008-10-17 17:51:10 +02001153 /* Check Temperature Range Select */
1154 if (data->kind == adt7461) {
1155 if (config & 0x04)
1156 data->flags |= LM90_FLAG_ADT7461_EXT;
1157 }
1158
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001159 /*
1160 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1161 * 0.125 degree resolution) and range (0x08, extend range
1162 * to -64 degree) mode for the remote temperature sensor.
1163 */
Guenter Roeck11e57812010-10-28 20:31:42 +02001164 if (data->kind == max6680)
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001165 config |= 0x18;
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001166
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001167 /*
1168 * Select external channel 0 for max6695/96
1169 */
1170 if (data->kind == max6696)
1171 config &= ~0x08;
1172
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001173 config &= 0xBF; /* run */
Jean Delvare95238362010-03-05 22:17:14 +01001174 if (config != data->config_orig) /* Only write if changed */
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001175 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Jean Delvare9b0e8522008-07-16 19:30:15 +02001178static int lm90_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001180 struct lm90_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Tony Jones1beeffe2007-08-20 13:46:20 -07001182 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckb6fc1ba2010-10-28 20:31:43 +02001183 lm90_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001184
Jean Delvare95238362010-03-05 22:17:14 +01001185 /* Restore initial configuration */
1186 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1187 data->config_orig);
1188
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001189 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return 0;
1191}
1192
Jean Delvare53de3342010-03-05 22:17:15 +01001193static void lm90_alert(struct i2c_client *client, unsigned int flag)
1194{
1195 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001196 u8 config, alarms, alarms2 = 0;
Jean Delvare53de3342010-03-05 22:17:15 +01001197
1198 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001199
1200 if (data->kind == max6696)
1201 lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
1202
1203 if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
Jean Delvare53de3342010-03-05 22:17:15 +01001204 dev_info(&client->dev, "Everything OK\n");
1205 } else {
1206 if (alarms & 0x61)
1207 dev_warn(&client->dev,
1208 "temp%d out of range, please check!\n", 1);
1209 if (alarms & 0x1a)
1210 dev_warn(&client->dev,
1211 "temp%d out of range, please check!\n", 2);
1212 if (alarms & 0x04)
1213 dev_warn(&client->dev,
1214 "temp%d diode open, please check!\n", 2);
1215
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001216 if (alarms2 & 0x18)
1217 dev_warn(&client->dev,
1218 "temp%d out of range, please check!\n", 3);
1219
Jean Delvare53de3342010-03-05 22:17:15 +01001220 /* Disable ALERT# output, because these chips don't implement
1221 SMBus alert correctly; they should only hold the alert line
1222 low briefly. */
1223 if ((data->kind == adm1032 || data->kind == adt7461)
1224 && (alarms & data->alert_alarms)) {
1225 dev_dbg(&client->dev, "Disabling ALERT#\n");
1226 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1227 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1228 config | 0x80);
1229 }
1230 }
1231}
1232
Jean Delvare6388a382008-10-17 17:51:09 +02001233static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
1234{
1235 int err;
1236 u8 oldh, newh, l;
1237
1238 /*
1239 * There is a trick here. We have to read two registers to have the
1240 * sensor temperature, but we have to beware a conversion could occur
1241 * inbetween the readings. The datasheet says we should either use
1242 * the one-shot conversion register, which we don't want to do
1243 * (disables hardware monitoring) or monitor the busy bit, which is
1244 * impossible (we can't read the values and monitor that bit at the
1245 * exact same time). So the solution used here is to read the high
1246 * byte once, then the low byte, then the high byte again. If the new
1247 * high byte matches the old one, then we have a valid reading. Else
1248 * we have to read the low byte again, and now we believe we have a
1249 * correct reading.
1250 */
1251 if ((err = lm90_read_reg(client, regh, &oldh))
1252 || (err = lm90_read_reg(client, regl, &l))
1253 || (err = lm90_read_reg(client, regh, &newh)))
1254 return err;
1255 if (oldh != newh) {
1256 err = lm90_read_reg(client, regl, &l);
1257 if (err)
1258 return err;
1259 }
1260 *value = (newh << 8) | l;
1261
1262 return 0;
1263}
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265static struct lm90_data *lm90_update_device(struct device *dev)
1266{
1267 struct i2c_client *client = to_i2c_client(dev);
1268 struct lm90_data *data = i2c_get_clientdata(client);
1269
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001270 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Ira W. Snyder8c3c7a22010-05-27 19:58:44 +02001272 if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
1273 || !data->valid) {
Jean Delvare6388a382008-10-17 17:51:09 +02001274 u8 h, l;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001275 u8 alarms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 dev_dbg(&client->dev, "Updating lm90 data.\n");
Jean Delvaref65e1702008-10-17 17:51:09 +02001278 lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
1279 lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
1280 lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
1281 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
Jean Delvare8256fe02005-10-26 21:37:52 +02001282 lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Guenter Roeck88073bb2010-10-28 20:31:43 +02001284 if (data->flags & LM90_HAVE_LOCAL_EXT) {
Jean Delvaref65e1702008-10-17 17:51:09 +02001285 lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
1286 MAX6657_REG_R_LOCAL_TEMPL,
1287 &data->temp11[4]);
1288 } else {
1289 if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
1290 &h) == 0)
1291 data->temp11[4] = h << 8;
1292 }
Jean Delvare6388a382008-10-17 17:51:09 +02001293 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
1294 LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Jean Delvare5f502a82008-10-17 17:51:09 +02001296 if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
1297 data->temp11[1] = h << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +02001298 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
Jean Delvare5f502a82008-10-17 17:51:09 +02001299 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
1300 &l) == 0)
1301 data->temp11[1] |= l;
1302 }
1303 if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
1304 data->temp11[2] = h << 8;
Guenter Roeck88073bb2010-10-28 20:31:43 +02001305 if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
Jean Delvare5f502a82008-10-17 17:51:09 +02001306 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
1307 &l) == 0)
1308 data->temp11[2] |= l;
1309 }
1310
Guenter Roeck88073bb2010-10-28 20:31:43 +02001311 if (data->flags & LM90_HAVE_OFFSET) {
Jean Delvare69f2f962007-09-05 14:15:37 +02001312 if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
Jean Delvare6388a382008-10-17 17:51:09 +02001313 &h) == 0
Jean Delvare69f2f962007-09-05 14:15:37 +02001314 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
1315 &l) == 0)
Jean Delvare6388a382008-10-17 17:51:09 +02001316 data->temp11[3] = (h << 8) | l;
Jean Delvare69f2f962007-09-05 14:15:37 +02001317 }
Guenter Roeck69487082010-10-28 20:31:43 +02001318 if (data->flags & LM90_HAVE_EMERGENCY) {
1319 lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
1320 &data->temp8[4]);
1321 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
1322 &data->temp8[5]);
1323 }
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001324 lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
1325 data->alarms = alarms; /* save as 16 bit value */
1326
1327 if (data->kind == max6696) {
1328 lm90_select_remote_channel(client, data, 1);
1329 lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
1330 &data->temp8[6]);
1331 lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
1332 &data->temp8[7]);
1333 lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
1334 LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
1335 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
1336 data->temp11[6] = h << 8;
1337 if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
1338 data->temp11[7] = h << 8;
1339 lm90_select_remote_channel(client, data, 0);
1340
1341 if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
1342 &alarms))
1343 data->alarms |= alarms << 8;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Jean Delvare53de3342010-03-05 22:17:15 +01001346 /* Re-enable ALERT# output if it was originally enabled and
1347 * relevant alarms are all clear */
1348 if ((data->config_orig & 0x80) == 0
1349 && (data->alarms & data->alert_alarms) == 0) {
1350 u8 config;
1351
1352 lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
1353 if (config & 0x80) {
1354 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
1355 i2c_smbus_write_byte_data(client,
1356 LM90_REG_W_CONFIG1,
1357 config & ~0x80);
1358 }
1359 }
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 data->last_updated = jiffies;
1362 data->valid = 1;
1363 }
1364
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001365 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 return data;
1368}
1369
1370static int __init sensors_lm90_init(void)
1371{
1372 return i2c_add_driver(&lm90_driver);
1373}
1374
1375static void __exit sensors_lm90_exit(void)
1376{
1377 i2c_del_driver(&lm90_driver);
1378}
1379
1380MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1381MODULE_DESCRIPTION("LM90/ADM1032 driver");
1382MODULE_LICENSE("GPL");
1383
1384module_init(sensors_lm90_init);
1385module_exit(sensors_lm90_exit);