blob: f1e6e7512ffa2e8f308b3de9483b943507d19913 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm87.c
3 *
4 * Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>
5 * Philip Edelbrock <phil@netroedge.com>
6 * Stephen Rousset <stephen.rousset@rocketlogix.com>
7 * Dan Eaton <dan.eaton@rocketlogix.com>
Jean Delvarea8884202008-07-16 19:30:14 +02008 * Copyright (C) 2004-2008 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Original port to Linux 2.6 by Jeff Oliver.
11 *
12 * The LM87 is a sensor chip made by National Semiconductor. It monitors up
13 * to 8 voltages (including its own power source), up to three temperatures
14 * (its own plus up to two external ones) and up to two fans. The default
15 * configuration is 6 voltages, two temperatures and two fans (see below).
16 * Voltages are scaled internally with ratios such that the nominal value of
17 * each voltage correspond to a register value of 192 (which means a
18 * resolution of about 0.5% of the nominal value). Temperature values are
19 * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
20 * datasheet can be obtained from National's website at:
21 * http://www.national.com/pf/LM/LM87.html
22 *
23 * Some functions share pins, so not all functions are available at the same
Ben Hutchings47064d62008-10-17 17:51:12 +020024 * time. Which are depends on the hardware setup. This driver normally
25 * assumes that firmware configured the chip correctly. Where this is not
26 * the case, platform code must set the I2C client's platform_data to point
27 * to a u8 value to be written to the channel register.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * For reference, here is the list of exclusive functions:
29 * - in0+in5 (default) or temp3
30 * - fan1 (default) or in6
31 * - fan2 (default) or in7
32 * - VID lines (default) or IRQ lines (not handled by this driver)
33 *
34 * The LM87 additionally features an analog output, supposedly usable to
35 * control the speed of a fan. All new chips use pulse width modulation
36 * instead. The LM87 is the only hardware monitoring chipset I know of
37 * which uses amplitude modulation. Be careful when using this feature.
38 *
Jean Delvarec7fa3732007-10-09 15:22:22 +020039 * This driver also supports the ADM1024, a sensor chip made by Analog
40 * Devices. That chip is fully compatible with the LM87. Complete
41 * datasheet can be obtained from Analog's website at:
42 * http://www.analog.com/en/prod/0,2877,ADM1024,00.html
43 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * This program is free software; you can redistribute it and/or modify
45 * it under the terms of the GNU General Public License as published by
46 * the Free Software Foundation; either version 2 of the License, or
47 * (at your option) any later version.
48 *
49 * This program is distributed in the hope that it will be useful,
50 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 * GNU General Public License for more details.
53 *
54 * You should have received a copy of the GNU General Public License
55 * along with this program; if not, write to the Free Software
56 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
57 */
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/module.h>
60#include <linux/init.h>
61#include <linux/slab.h>
62#include <linux/jiffies.h>
63#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040064#include <linux/hwmon.h>
Jean Delvarec2803b92007-09-26 23:39:01 +020065#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020066#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040067#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010068#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * Addresses to scan
72 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
73 */
74
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050075static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jean Delvaree5e9f442009-12-14 21:17:27 +010077enum chips { lm87, adm1024 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * The LM87 registers
81 */
82
83/* nr in 0..5 */
84#define LM87_REG_IN(nr) (0x20 + (nr))
85#define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
86#define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
87/* nr in 0..1 */
88#define LM87_REG_AIN(nr) (0x28 + (nr))
89#define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
90#define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
91
92static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
93static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
94static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
95
96#define LM87_REG_TEMP_HW_INT_LOCK 0x13
97#define LM87_REG_TEMP_HW_EXT_LOCK 0x14
98#define LM87_REG_TEMP_HW_INT 0x17
99#define LM87_REG_TEMP_HW_EXT 0x18
100
101/* nr in 0..1 */
102#define LM87_REG_FAN(nr) (0x28 + (nr))
103#define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
104#define LM87_REG_AOUT 0x19
105
106#define LM87_REG_CONFIG 0x40
107#define LM87_REG_CHANNEL_MODE 0x16
108#define LM87_REG_VID_FAN_DIV 0x47
109#define LM87_REG_VID4 0x49
110
111#define LM87_REG_ALARMS1 0x41
112#define LM87_REG_ALARMS2 0x42
113
114#define LM87_REG_COMPANY_ID 0x3E
115#define LM87_REG_REVISION 0x3F
116
117/*
118 * Conversions and various macros
119 * The LM87 uses signed 8-bit values for temperatures.
120 */
121
122#define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
123#define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
124 (val) * 192 >= (scale) * 255 ? 255 : \
125 ((val) * 192 + (scale)/2) / (scale))
126
127#define TEMP_FROM_REG(reg) ((reg) * 1000)
128#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
129 (val) >= 126500 ? 127 : \
130 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
131
132#define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
Jean Delvareb965d4b2007-09-26 22:16:56 +0200133 (1350000 + (reg)*(div) / 2) / ((reg)*(div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
135 (1350000 + (val)*(div) / 2) / ((val)*(div)))
136
137#define FAN_DIV_FROM_REG(reg) (1 << (reg))
138
139/* analog out is 9.80mV/LSB */
140#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
141#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
142 (val) >= 2500 ? 255 : \
143 ((val) * 10 + 49) / 98)
144
145/* nr in 0..1 */
146#define CHAN_NO_FAN(nr) (1 << (nr))
147#define CHAN_TEMP3 (1 << 2)
148#define CHAN_VCC_5V (1 << 3)
Jean Delvare889af3d2007-10-08 22:48:10 +0200149#define CHAN_NO_VID (1 << 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/*
152 * Functions declaration
153 */
154
Jean Delvarea8884202008-07-16 19:30:14 +0200155static int lm87_probe(struct i2c_client *client,
156 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100157static int lm87_detect(struct i2c_client *new_client,
Jean Delvarea8884202008-07-16 19:30:14 +0200158 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void lm87_init_client(struct i2c_client *client);
Jean Delvarea8884202008-07-16 19:30:14 +0200160static int lm87_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static struct lm87_data *lm87_update_device(struct device *dev);
162
163/*
164 * Driver data (common to all clients)
165 */
166
Jean Delvarea8884202008-07-16 19:30:14 +0200167static const struct i2c_device_id lm87_id[] = {
168 { "lm87", lm87 },
169 { "adm1024", adm1024 },
170 { }
171};
172MODULE_DEVICE_TABLE(i2c, lm87_id);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static struct i2c_driver lm87_driver = {
Jean Delvarea8884202008-07-16 19:30:14 +0200175 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100176 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100177 .name = "lm87",
178 },
Jean Delvarea8884202008-07-16 19:30:14 +0200179 .probe = lm87_probe,
180 .remove = lm87_remove,
181 .id_table = lm87_id,
182 .detect = lm87_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100183 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
186/*
187 * Client data (each client gets its own)
188 */
189
190struct lm87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700191 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100192 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 char valid; /* zero until following fields are valid */
194 unsigned long last_updated; /* In jiffies */
195
196 u8 channel; /* register value */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200197 u8 config; /* original register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 u8 in[8]; /* register value */
200 u8 in_max[8]; /* register value */
201 u8 in_min[8]; /* register value */
202 u16 in_scale[8];
203
204 s8 temp[3]; /* register value */
205 s8 temp_high[3]; /* register value */
206 s8 temp_low[3]; /* register value */
207 s8 temp_crit_int; /* min of two register values */
208 s8 temp_crit_ext; /* min of two register values */
209
210 u8 fan[2]; /* register value */
211 u8 fan_min[2]; /* register value */
212 u8 fan_div[2]; /* register value, shifted right */
213 u8 aout; /* register value */
214
215 u16 alarms; /* register values, combined */
216 u8 vid; /* register values, combined */
217 u8 vrm;
218};
219
220/*
221 * Sysfs stuff
222 */
223
224static inline int lm87_read_value(struct i2c_client *client, u8 reg)
225{
226 return i2c_smbus_read_byte_data(client, reg);
227}
228
229static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
230{
231 return i2c_smbus_write_byte_data(client, reg, value);
232}
233
234#define show_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400235static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{ \
237 struct lm87_data *data = lm87_update_device(dev); \
238 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
239 data->in_scale[offset])); \
240} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400241static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{ \
243 struct lm87_data *data = lm87_update_device(dev); \
244 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
245 data->in_scale[offset])); \
246} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400247static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{ \
249 struct lm87_data *data = lm87_update_device(dev); \
250 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
251 data->in_scale[offset])); \
252} \
253static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
254 show_in##offset##_input, NULL);
255show_in(0);
256show_in(1);
257show_in(2);
258show_in(3);
259show_in(4);
260show_in(5);
261show_in(6);
262show_in(7);
263
264static void set_in_min(struct device *dev, const char *buf, int nr)
265{
266 struct i2c_client *client = to_i2c_client(dev);
267 struct lm87_data *data = i2c_get_clientdata(client);
268 long val = simple_strtol(buf, NULL, 10);
269
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100270 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
272 lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
273 LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100274 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277static void set_in_max(struct device *dev, const char *buf, int nr)
278{
279 struct i2c_client *client = to_i2c_client(dev);
280 struct lm87_data *data = i2c_get_clientdata(client);
281 long val = simple_strtol(buf, NULL, 10);
282
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100283 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
285 lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
286 LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100287 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290#define set_in(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400291static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 const char *buf, size_t count) \
293{ \
294 set_in_min(dev, buf, offset); \
295 return count; \
296} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400297static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 const char *buf, size_t count) \
299{ \
300 set_in_max(dev, buf, offset); \
301 return count; \
302} \
303static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
304 show_in##offset##_min, set_in##offset##_min); \
305static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
306 show_in##offset##_max, set_in##offset##_max);
307set_in(0);
308set_in(1);
309set_in(2);
310set_in(3);
311set_in(4);
312set_in(5);
313set_in(6);
314set_in(7);
315
316#define show_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400317static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{ \
319 struct lm87_data *data = lm87_update_device(dev); \
320 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
321} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400322static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{ \
324 struct lm87_data *data = lm87_update_device(dev); \
325 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
326} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400327static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{ \
329 struct lm87_data *data = lm87_update_device(dev); \
330 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
331}\
332static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
333 show_temp##offset##_input, NULL);
334show_temp(1);
335show_temp(2);
336show_temp(3);
337
338static void set_temp_low(struct device *dev, const char *buf, int nr)
339{
340 struct i2c_client *client = to_i2c_client(dev);
341 struct lm87_data *data = i2c_get_clientdata(client);
342 long val = simple_strtol(buf, NULL, 10);
343
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100344 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 data->temp_low[nr] = TEMP_TO_REG(val);
346 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100347 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350static void set_temp_high(struct device *dev, const char *buf, int nr)
351{
352 struct i2c_client *client = to_i2c_client(dev);
353 struct lm87_data *data = i2c_get_clientdata(client);
354 long val = simple_strtol(buf, NULL, 10);
355
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100356 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 data->temp_high[nr] = TEMP_TO_REG(val);
358 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100359 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362#define set_temp(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400363static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 const char *buf, size_t count) \
365{ \
366 set_temp_low(dev, buf, offset-1); \
367 return count; \
368} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400369static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 const char *buf, size_t count) \
371{ \
372 set_temp_high(dev, buf, offset-1); \
373 return count; \
374} \
375static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
376 show_temp##offset##_high, set_temp##offset##_high); \
377static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
378 show_temp##offset##_low, set_temp##offset##_low);
379set_temp(1);
380set_temp(2);
381set_temp(3);
382
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400383static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct lm87_data *data = lm87_update_device(dev);
386 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
387}
388
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400389static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct lm87_data *data = lm87_update_device(dev);
392 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
393}
394
395static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
396static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
397static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
398
399#define show_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400400static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{ \
402 struct lm87_data *data = lm87_update_device(dev); \
403 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
404 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
405} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400406static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{ \
408 struct lm87_data *data = lm87_update_device(dev); \
409 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
410 FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
411} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400412static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{ \
414 struct lm87_data *data = lm87_update_device(dev); \
415 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
416} \
417static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
418 show_fan##offset##_input, NULL);
419show_fan(1);
420show_fan(2);
421
422static void set_fan_min(struct device *dev, const char *buf, int nr)
423{
424 struct i2c_client *client = to_i2c_client(dev);
425 struct lm87_data *data = i2c_get_clientdata(client);
426 long val = simple_strtol(buf, NULL, 10);
427
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100428 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 data->fan_min[nr] = FAN_TO_REG(val,
430 FAN_DIV_FROM_REG(data->fan_div[nr]));
431 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100432 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/* Note: we save and restore the fan minimum here, because its value is
436 determined in part by the fan clock divider. This follows the principle
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200437 of least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 because the divider changed. */
439static ssize_t set_fan_div(struct device *dev, const char *buf,
440 size_t count, int nr)
441{
442 struct i2c_client *client = to_i2c_client(dev);
443 struct lm87_data *data = i2c_get_clientdata(client);
444 long val = simple_strtol(buf, NULL, 10);
445 unsigned long min;
446 u8 reg;
447
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100448 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 min = FAN_FROM_REG(data->fan_min[nr],
450 FAN_DIV_FROM_REG(data->fan_div[nr]));
451
452 switch (val) {
453 case 1: data->fan_div[nr] = 0; break;
454 case 2: data->fan_div[nr] = 1; break;
455 case 4: data->fan_div[nr] = 2; break;
456 case 8: data->fan_div[nr] = 3; break;
457 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100458 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -EINVAL;
460 }
461
462 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
463 switch (nr) {
464 case 0:
465 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
466 break;
467 case 1:
468 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
469 break;
470 }
471 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
472
473 data->fan_min[nr] = FAN_TO_REG(min, val);
474 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
475 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100476 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 return count;
479}
480
481#define set_fan(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400482static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 size_t count) \
484{ \
485 set_fan_min(dev, buf, offset-1); \
486 return count; \
487} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400488static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 size_t count) \
490{ \
491 return set_fan_div(dev, buf, count, offset-1); \
492} \
493static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
494 show_fan##offset##_min, set_fan##offset##_min); \
495static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
496 show_fan##offset##_div, set_fan##offset##_div);
497set_fan(1);
498set_fan(2);
499
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400500static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct lm87_data *data = lm87_update_device(dev);
503 return sprintf(buf, "%d\n", data->alarms);
504}
505static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
506
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400507static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct lm87_data *data = lm87_update_device(dev);
510 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
511}
512static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
513
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400514static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jean Delvare90d66192007-10-08 18:24:35 +0200516 struct lm87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return sprintf(buf, "%d\n", data->vrm);
518}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400519static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100521 struct lm87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 data->vrm = simple_strtoul(buf, NULL, 10);
523 return count;
524}
525static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
526
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400527static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct lm87_data *data = lm87_update_device(dev);
530 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
531}
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400532static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct i2c_client *client = to_i2c_client(dev);
535 struct lm87_data *data = i2c_get_clientdata(client);
536 long val = simple_strtol(buf, NULL, 10);
537
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100538 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 data->aout = AOUT_TO_REG(val);
540 lm87_write_value(client, LM87_REG_AOUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100541 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return count;
543}
544static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
545
Jean Delvarec2803b92007-09-26 23:39:01 +0200546static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
547 char *buf)
548{
549 struct lm87_data *data = lm87_update_device(dev);
550 int bitnr = to_sensor_dev_attr(attr)->index;
551 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
552}
553static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
554static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
555static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
556static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
557static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
558static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
559static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
560static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
561static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
562static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
563static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
564static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
565static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
566static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
567static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/*
570 * Real code
571 */
572
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200573static struct attribute *lm87_attributes[] = {
574 &dev_attr_in1_input.attr,
575 &dev_attr_in1_min.attr,
576 &dev_attr_in1_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200577 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200578 &dev_attr_in2_input.attr,
579 &dev_attr_in2_min.attr,
580 &dev_attr_in2_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200581 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200582 &dev_attr_in3_input.attr,
583 &dev_attr_in3_min.attr,
584 &dev_attr_in3_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200585 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200586 &dev_attr_in4_input.attr,
587 &dev_attr_in4_min.attr,
588 &dev_attr_in4_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200589 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200590
591 &dev_attr_temp1_input.attr,
592 &dev_attr_temp1_max.attr,
593 &dev_attr_temp1_min.attr,
594 &dev_attr_temp1_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200595 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200596 &dev_attr_temp2_input.attr,
597 &dev_attr_temp2_max.attr,
598 &dev_attr_temp2_min.attr,
599 &dev_attr_temp2_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200600 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
601 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200602
603 &dev_attr_alarms.attr,
604 &dev_attr_aout_output.attr,
605
606 NULL
607};
608
609static const struct attribute_group lm87_group = {
610 .attrs = lm87_attributes,
611};
612
613static struct attribute *lm87_attributes_opt[] = {
614 &dev_attr_in6_input.attr,
615 &dev_attr_in6_min.attr,
616 &dev_attr_in6_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200617 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200618
619 &dev_attr_fan1_input.attr,
620 &dev_attr_fan1_min.attr,
621 &dev_attr_fan1_div.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200622 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200623
624 &dev_attr_in7_input.attr,
625 &dev_attr_in7_min.attr,
626 &dev_attr_in7_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200627 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200628
629 &dev_attr_fan2_input.attr,
630 &dev_attr_fan2_min.attr,
631 &dev_attr_fan2_div.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200632 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200633
634 &dev_attr_temp3_input.attr,
635 &dev_attr_temp3_max.attr,
636 &dev_attr_temp3_min.attr,
637 &dev_attr_temp3_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200638 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
639 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200640
641 &dev_attr_in0_input.attr,
642 &dev_attr_in0_min.attr,
643 &dev_attr_in0_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200644 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200645 &dev_attr_in5_input.attr,
646 &dev_attr_in5_min.attr,
647 &dev_attr_in5_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200648 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200649
650 &dev_attr_cpu0_vid.attr,
651 &dev_attr_vrm.attr,
652
653 NULL
654};
655
656static const struct attribute_group lm87_group_opt = {
657 .attrs = lm87_attributes_opt,
658};
659
Jean Delvarea8884202008-07-16 19:30:14 +0200660/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100661static int lm87_detect(struct i2c_client *new_client,
Jean Delvarea8884202008-07-16 19:30:14 +0200662 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Jean Delvarea8884202008-07-16 19:30:14 +0200664 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100665 const char *name;
666 u8 cid, rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvarea8884202008-07-16 19:30:14 +0200669 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jean Delvare52df6442009-12-09 20:35:57 +0100671 if (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
672 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* Now, we do the remaining detection. */
Jean Delvare52df6442009-12-09 20:35:57 +0100675 cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID);
676 rev = lm87_read_value(new_client, LM87_REG_REVISION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Jean Delvare52df6442009-12-09 20:35:57 +0100678 if (cid == 0x02 /* National Semiconductor */
679 && (rev >= 0x01 && rev <= 0x08))
680 name = "lm87";
681 else if (cid == 0x41 /* Analog Devices */
682 && (rev & 0xf0) == 0x10)
683 name = "adm1024";
684 else {
685 dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
686 new_client->addr);
687 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Jean Delvare52df6442009-12-09 20:35:57 +0100690 strlcpy(info->type, name, I2C_NAME_SIZE);
Jean Delvarea8884202008-07-16 19:30:14 +0200691
692 return 0;
693}
694
695static int lm87_probe(struct i2c_client *new_client,
696 const struct i2c_device_id *id)
697{
698 struct lm87_data *data;
699 int err;
700
701 data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL);
702 if (!data) {
703 err = -ENOMEM;
704 goto exit;
705 }
706
707 i2c_set_clientdata(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100709 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* Initialize the LM87 chip */
712 lm87_init_client(new_client);
713
714 data->in_scale[0] = 2500;
715 data->in_scale[1] = 2700;
716 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
717 data->in_scale[3] = 5000;
718 data->in_scale[4] = 12000;
719 data->in_scale[5] = 2700;
720 data->in_scale[6] = 1875;
721 data->in_scale[7] = 1875;
722
723 /* Register sysfs hooks */
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200724 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
Jean Delvarea8884202008-07-16 19:30:14 +0200725 goto exit_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (data->channel & CHAN_NO_FAN(0)) {
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200728 if ((err = device_create_file(&new_client->dev,
729 &dev_attr_in6_input))
730 || (err = device_create_file(&new_client->dev,
731 &dev_attr_in6_min))
732 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200733 &dev_attr_in6_max))
734 || (err = device_create_file(&new_client->dev,
735 &sensor_dev_attr_in6_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200736 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 } else {
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200738 if ((err = device_create_file(&new_client->dev,
739 &dev_attr_fan1_input))
740 || (err = device_create_file(&new_client->dev,
741 &dev_attr_fan1_min))
742 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200743 &dev_attr_fan1_div))
744 || (err = device_create_file(&new_client->dev,
745 &sensor_dev_attr_fan1_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200746 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200749 if (data->channel & CHAN_NO_FAN(1)) {
750 if ((err = device_create_file(&new_client->dev,
751 &dev_attr_in7_input))
752 || (err = device_create_file(&new_client->dev,
753 &dev_attr_in7_min))
754 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200755 &dev_attr_in7_max))
756 || (err = device_create_file(&new_client->dev,
757 &sensor_dev_attr_in7_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200758 goto exit_remove;
759 } else {
760 if ((err = device_create_file(&new_client->dev,
761 &dev_attr_fan2_input))
762 || (err = device_create_file(&new_client->dev,
763 &dev_attr_fan2_min))
764 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200765 &dev_attr_fan2_div))
766 || (err = device_create_file(&new_client->dev,
767 &sensor_dev_attr_fan2_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200768 goto exit_remove;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (data->channel & CHAN_TEMP3) {
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200772 if ((err = device_create_file(&new_client->dev,
773 &dev_attr_temp3_input))
774 || (err = device_create_file(&new_client->dev,
775 &dev_attr_temp3_max))
776 || (err = device_create_file(&new_client->dev,
777 &dev_attr_temp3_min))
778 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200779 &dev_attr_temp3_crit))
780 || (err = device_create_file(&new_client->dev,
781 &sensor_dev_attr_temp3_alarm.dev_attr))
782 || (err = device_create_file(&new_client->dev,
783 &sensor_dev_attr_temp3_fault.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200784 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 } else {
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200786 if ((err = device_create_file(&new_client->dev,
787 &dev_attr_in0_input))
788 || (err = device_create_file(&new_client->dev,
789 &dev_attr_in0_min))
790 || (err = device_create_file(&new_client->dev,
791 &dev_attr_in0_max))
792 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200793 &sensor_dev_attr_in0_alarm.dev_attr))
794 || (err = device_create_file(&new_client->dev,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200795 &dev_attr_in5_input))
796 || (err = device_create_file(&new_client->dev,
797 &dev_attr_in5_min))
798 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200799 &dev_attr_in5_max))
800 || (err = device_create_file(&new_client->dev,
801 &sensor_dev_attr_in5_alarm.dev_attr)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200802 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
805 if (!(data->channel & CHAN_NO_VID)) {
Jean Delvare8a665a02007-05-08 17:21:59 +0200806 data->vrm = vid_which_vrm();
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200807 if ((err = device_create_file(&new_client->dev,
808 &dev_attr_cpu0_vid))
809 || (err = device_create_file(&new_client->dev,
810 &dev_attr_vrm)))
811 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
Tony Jones1beeffe2007-08-20 13:46:20 -0700814 data->hwmon_dev = hwmon_device_register(&new_client->dev);
815 if (IS_ERR(data->hwmon_dev)) {
816 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200817 goto exit_remove;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 return 0;
821
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200822exit_remove:
823 sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
824 sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825exit_free:
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200826 lm87_write_value(new_client, LM87_REG_CONFIG, data->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 kfree(data);
828exit:
829 return err;
830}
831
832static void lm87_init_client(struct i2c_client *client)
833{
834 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Ben Hutchings47064d62008-10-17 17:51:12 +0200836 if (client->dev.platform_data) {
837 data->channel = *(u8 *)client->dev.platform_data;
838 lm87_write_value(client,
839 LM87_REG_CHANNEL_MODE, data->channel);
840 } else {
841 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
842 }
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200843 data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200845 if (!(data->config & 0x01)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int i;
847
848 /* Limits are left uninitialized after power-up */
849 for (i = 1; i < 6; i++) {
850 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
851 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
852 }
853 for (i = 0; i < 2; i++) {
854 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
855 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
856 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
857 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
858 }
859 if (data->channel & CHAN_TEMP3) {
860 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
861 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
862 } else {
863 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
864 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
865 }
866 }
Ben Hutchings49ae6cc2008-10-17 17:51:11 +0200867
868 /* Make sure Start is set and INT#_Clear is clear */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200869 if ((data->config & 0x09) != 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 lm87_write_value(client, LM87_REG_CONFIG,
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200871 (data->config & 0x77) | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Jean Delvarea8884202008-07-16 19:30:14 +0200874static int lm87_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400876 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Tony Jones1beeffe2007-08-20 13:46:20 -0700878 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200879 sysfs_remove_group(&client->dev.kobj, &lm87_group);
880 sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400881
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200882 lm87_write_value(client, LM87_REG_CONFIG, data->config);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400883 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 0;
885}
886
887static struct lm87_data *lm87_update_device(struct device *dev)
888{
889 struct i2c_client *client = to_i2c_client(dev);
890 struct lm87_data *data = i2c_get_clientdata(client);
891
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100892 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
895 int i, j;
896
897 dev_dbg(&client->dev, "Updating data.\n");
898
899 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
900 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
901 for (; i < j; i++) {
902 data->in[i] = lm87_read_value(client,
903 LM87_REG_IN(i));
904 data->in_min[i] = lm87_read_value(client,
905 LM87_REG_IN_MIN(i));
906 data->in_max[i] = lm87_read_value(client,
907 LM87_REG_IN_MAX(i));
908 }
909
910 for (i = 0; i < 2; i++) {
911 if (data->channel & CHAN_NO_FAN(i)) {
912 data->in[6+i] = lm87_read_value(client,
913 LM87_REG_AIN(i));
914 data->in_max[6+i] = lm87_read_value(client,
915 LM87_REG_AIN_MAX(i));
916 data->in_min[6+i] = lm87_read_value(client,
917 LM87_REG_AIN_MIN(i));
918
919 } else {
920 data->fan[i] = lm87_read_value(client,
921 LM87_REG_FAN(i));
922 data->fan_min[i] = lm87_read_value(client,
923 LM87_REG_FAN_MIN(i));
924 }
925 }
926
927 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
928 for (i = 0 ; i < j; i++) {
929 data->temp[i] = lm87_read_value(client,
930 LM87_REG_TEMP[i]);
931 data->temp_high[i] = lm87_read_value(client,
932 LM87_REG_TEMP_HIGH[i]);
933 data->temp_low[i] = lm87_read_value(client,
934 LM87_REG_TEMP_LOW[i]);
935 }
936
937 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
938 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
939 data->temp_crit_int = min(i, j);
940
941 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
942 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
943 data->temp_crit_ext = min(i, j);
944
945 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
946 data->fan_div[0] = (i >> 4) & 0x03;
947 data->fan_div[1] = (i >> 6) & 0x03;
948 data->vid = (i & 0x0F)
949 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
950 << 4;
951
952 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
953 | (lm87_read_value(client, LM87_REG_ALARMS2)
954 << 8);
955 data->aout = lm87_read_value(client, LM87_REG_AOUT);
956
957 data->last_updated = jiffies;
958 data->valid = 1;
959 }
960
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100961 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 return data;
964}
965
966static int __init sensors_lm87_init(void)
967{
968 return i2c_add_driver(&lm87_driver);
969}
970
971static void __exit sensors_lm87_exit(void)
972{
973 i2c_del_driver(&lm87_driver);
974}
975
976MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
977MODULE_DESCRIPTION("LM87 driver");
978MODULE_LICENSE("GPL");
979
980module_init(sensors_lm87_init);
981module_exit(sensors_lm87_exit);