blob: 0216592a794b901f317d4e21054a15c1e01b68b5 [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
Guenter Roeckc6370db2012-01-14 20:58:08 -0800122#define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192)
123#define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 (val) * 192 >= (scale) * 255 ? 255 : \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800125 ((val) * 192 + (scale) / 2) / (scale))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127#define TEMP_FROM_REG(reg) ((reg) * 1000)
128#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
129 (val) >= 126500 ? 127 : \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800130 (((val) < 0 ? (val) - 500 : \
131 (val) + 500) / 1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Guenter Roeckc6370db2012-01-14 20:58:08 -0800133#define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \
134 (1350000 + (reg)*(div) / 2) / ((reg) * (div)))
135#define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \
136 (1350000 + (val)*(div) / 2) / ((val) * (div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138#define FAN_DIV_FROM_REG(reg) (1 << (reg))
139
140/* analog out is 9.80mV/LSB */
141#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
142#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
143 (val) >= 2500 ? 255 : \
144 ((val) * 10 + 49) / 98)
145
146/* nr in 0..1 */
147#define CHAN_NO_FAN(nr) (1 << (nr))
148#define CHAN_TEMP3 (1 << 2)
149#define CHAN_VCC_5V (1 << 3)
Jean Delvare889af3d2007-10-08 22:48:10 +0200150#define CHAN_NO_VID (1 << 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/*
153 * Functions declaration
154 */
155
Jean Delvarea8884202008-07-16 19:30:14 +0200156static int lm87_probe(struct i2c_client *client,
157 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100158static int lm87_detect(struct i2c_client *new_client,
Jean Delvarea8884202008-07-16 19:30:14 +0200159 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void lm87_init_client(struct i2c_client *client);
Jean Delvarea8884202008-07-16 19:30:14 +0200161static int lm87_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static struct lm87_data *lm87_update_device(struct device *dev);
163
164/*
165 * Driver data (common to all clients)
166 */
167
Jean Delvarea8884202008-07-16 19:30:14 +0200168static const struct i2c_device_id lm87_id[] = {
169 { "lm87", lm87 },
170 { "adm1024", adm1024 },
171 { }
172};
173MODULE_DEVICE_TABLE(i2c, lm87_id);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static struct i2c_driver lm87_driver = {
Jean Delvarea8884202008-07-16 19:30:14 +0200176 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100177 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100178 .name = "lm87",
179 },
Jean Delvarea8884202008-07-16 19:30:14 +0200180 .probe = lm87_probe,
181 .remove = lm87_remove,
182 .id_table = lm87_id,
183 .detect = lm87_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100184 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
187/*
188 * Client data (each client gets its own)
189 */
190
191struct lm87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700192 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100193 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 char valid; /* zero until following fields are valid */
195 unsigned long last_updated; /* In jiffies */
196
197 u8 channel; /* register value */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200198 u8 config; /* original register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 u8 in[8]; /* register value */
201 u8 in_max[8]; /* register value */
202 u8 in_min[8]; /* register value */
203 u16 in_scale[8];
204
205 s8 temp[3]; /* register value */
206 s8 temp_high[3]; /* register value */
207 s8 temp_low[3]; /* register value */
208 s8 temp_crit_int; /* min of two register values */
209 s8 temp_crit_ext; /* min of two register values */
210
211 u8 fan[2]; /* register value */
212 u8 fan_min[2]; /* register value */
213 u8 fan_div[2]; /* register value, shifted right */
214 u8 aout; /* register value */
215
216 u16 alarms; /* register values, combined */
217 u8 vid; /* register values, combined */
218 u8 vrm;
219};
220
221/*
222 * Sysfs stuff
223 */
224
225static inline int lm87_read_value(struct i2c_client *client, u8 reg)
226{
227 return i2c_smbus_read_byte_data(client, reg);
228}
229
230static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
231{
232 return i2c_smbus_write_byte_data(client, reg, value);
233}
234
235#define show_in(offset) \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800236static ssize_t show_in##offset##_input(struct device *dev, \
237 struct device_attribute *attr, \
238 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{ \
240 struct lm87_data *data = lm87_update_device(dev); \
241 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
242 data->in_scale[offset])); \
243} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800244static ssize_t show_in##offset##_min(struct device *dev, \
245 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{ \
247 struct lm87_data *data = lm87_update_device(dev); \
248 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
249 data->in_scale[offset])); \
250} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800251static ssize_t show_in##offset##_max(struct device *dev, \
252 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{ \
254 struct lm87_data *data = lm87_update_device(dev); \
255 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
256 data->in_scale[offset])); \
257} \
258static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
259 show_in##offset##_input, NULL);
260show_in(0);
261show_in(1);
262show_in(2);
263show_in(3);
264show_in(4);
265show_in(5);
266show_in(6);
267show_in(7);
268
Guenter Roeckc6370db2012-01-14 20:58:08 -0800269static ssize_t set_in_min(struct device *dev, const char *buf, int nr,
270 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct i2c_client *client = to_i2c_client(dev);
273 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800274 long val;
275 int err;
276
277 err = kstrtol(buf, 10, &val);
278 if (err)
279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100281 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800283 lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) :
284 LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100285 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800286 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Guenter Roeckc6370db2012-01-14 20:58:08 -0800289static ssize_t set_in_max(struct device *dev, const char *buf, int nr,
290 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct i2c_client *client = to_i2c_client(dev);
293 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800294 long val;
295 int err;
296
297 err = kstrtol(buf, 10, &val);
298 if (err)
299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100301 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800303 lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) :
304 LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100305 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800306 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309#define set_in(offset) \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800310static ssize_t set_in##offset##_min(struct device *dev, \
311 struct device_attribute *attr, \
312 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{ \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800314 return set_in_min(dev, buf, offset, count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800316static ssize_t set_in##offset##_max(struct device *dev, \
317 struct device_attribute *attr, \
318 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{ \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800320 return set_in_max(dev, buf, offset, count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321} \
322static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
323 show_in##offset##_min, set_in##offset##_min); \
324static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
325 show_in##offset##_max, set_in##offset##_max);
326set_in(0);
327set_in(1);
328set_in(2);
329set_in(3);
330set_in(4);
331set_in(5);
332set_in(6);
333set_in(7);
334
335#define show_temp(offset) \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800336static ssize_t show_temp##offset##_input(struct device *dev, \
337 struct device_attribute *attr, \
338 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{ \
340 struct lm87_data *data = lm87_update_device(dev); \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800341 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset - 1])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800343static ssize_t show_temp##offset##_low(struct device *dev, \
344 struct device_attribute *attr, \
345 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{ \
347 struct lm87_data *data = lm87_update_device(dev); \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800348 return sprintf(buf, "%d\n", \
349 TEMP_FROM_REG(data->temp_low[offset - 1])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800351static ssize_t show_temp##offset##_high(struct device *dev, \
352 struct device_attribute *attr, \
353 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{ \
355 struct lm87_data *data = lm87_update_device(dev); \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800356 return sprintf(buf, "%d\n", \
357 TEMP_FROM_REG(data->temp_high[offset - 1])); \
358} \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
360 show_temp##offset##_input, NULL);
361show_temp(1);
362show_temp(2);
363show_temp(3);
364
Guenter Roeckc6370db2012-01-14 20:58:08 -0800365static ssize_t set_temp_low(struct device *dev, const char *buf, int nr,
366 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct i2c_client *client = to_i2c_client(dev);
369 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800370 long val;
371 int err;
372
373 err = kstrtol(buf, 10, &val);
374 if (err)
375 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100377 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 data->temp_low[nr] = TEMP_TO_REG(val);
379 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100380 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800381 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Guenter Roeckc6370db2012-01-14 20:58:08 -0800384static ssize_t set_temp_high(struct device *dev, const char *buf, int nr,
385 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct i2c_client *client = to_i2c_client(dev);
388 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800389 long val;
390 int err;
391
392 err = kstrtol(buf, 10, &val);
393 if (err)
394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 data->temp_high[nr] = TEMP_TO_REG(val);
398 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800400 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403#define set_temp(offset) \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800404static ssize_t set_temp##offset##_low(struct device *dev, \
405 struct device_attribute *attr, \
406 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{ \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800408 return set_temp_low(dev, buf, offset - 1, count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800410static ssize_t set_temp##offset##_high(struct device *dev, \
411 struct device_attribute *attr, \
412 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{ \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800414 return set_temp_high(dev, buf, offset - 1, count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415} \
416static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
417 show_temp##offset##_high, set_temp##offset##_high); \
418static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
419 show_temp##offset##_low, set_temp##offset##_low);
420set_temp(1);
421set_temp(2);
422set_temp(3);
423
Guenter Roeckc6370db2012-01-14 20:58:08 -0800424static ssize_t show_temp_crit_int(struct device *dev,
425 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct lm87_data *data = lm87_update_device(dev);
428 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
429}
430
Guenter Roeckc6370db2012-01-14 20:58:08 -0800431static ssize_t show_temp_crit_ext(struct device *dev,
432 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct lm87_data *data = lm87_update_device(dev);
435 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
436}
437
438static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
439static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
440static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
441
442#define show_fan(offset) \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800443static ssize_t show_fan##offset##_input(struct device *dev, \
444 struct device_attribute *attr, \
445 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{ \
447 struct lm87_data *data = lm87_update_device(dev); \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800448 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset - 1], \
449 FAN_DIV_FROM_REG(data->fan_div[offset - 1]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800451static ssize_t show_fan##offset##_min(struct device *dev, \
452 struct device_attribute *attr, \
453 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{ \
455 struct lm87_data *data = lm87_update_device(dev); \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800456 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset - 1], \
457 FAN_DIV_FROM_REG(data->fan_div[offset - 1]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800459static ssize_t show_fan##offset##_div(struct device *dev, \
460 struct device_attribute *attr, \
461 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{ \
463 struct lm87_data *data = lm87_update_device(dev); \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800464 return sprintf(buf, "%d\n", \
465 FAN_DIV_FROM_REG(data->fan_div[offset - 1])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466} \
467static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
468 show_fan##offset##_input, NULL);
469show_fan(1);
470show_fan(2);
471
Guenter Roeckc6370db2012-01-14 20:58:08 -0800472static ssize_t set_fan_min(struct device *dev, const char *buf, int nr,
473 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct i2c_client *client = to_i2c_client(dev);
476 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800477 long val;
478 int err;
479
480 err = kstrtol(buf, 10, &val);
481 if (err)
482 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100484 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 data->fan_min[nr] = FAN_TO_REG(val,
486 FAN_DIV_FROM_REG(data->fan_div[nr]));
487 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100488 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800489 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Guenter Roeckc6370db2012-01-14 20:58:08 -0800492/*
493 * Note: we save and restore the fan minimum here, because its value is
494 * determined in part by the fan clock divider. This follows the principle
495 * of least surprise; the user doesn't expect the fan minimum to change just
496 * because the divider changed.
497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static ssize_t set_fan_div(struct device *dev, const char *buf,
499 size_t count, int nr)
500{
501 struct i2c_client *client = to_i2c_client(dev);
502 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800503 long val;
504 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned long min;
506 u8 reg;
507
Guenter Roeckc6370db2012-01-14 20:58:08 -0800508 err = kstrtol(buf, 10, &val);
509 if (err)
510 return err;
511
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100512 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 min = FAN_FROM_REG(data->fan_min[nr],
514 FAN_DIV_FROM_REG(data->fan_div[nr]));
515
516 switch (val) {
Guenter Roeckc6370db2012-01-14 20:58:08 -0800517 case 1:
518 data->fan_div[nr] = 0;
519 break;
520 case 2:
521 data->fan_div[nr] = 1;
522 break;
523 case 4:
524 data->fan_div[nr] = 2;
525 break;
526 case 8:
527 data->fan_div[nr] = 3;
528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100530 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return -EINVAL;
532 }
533
534 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
535 switch (nr) {
536 case 0:
537 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
538 break;
539 case 1:
540 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
541 break;
542 }
543 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
544
545 data->fan_min[nr] = FAN_TO_REG(min, val);
546 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
547 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100548 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 return count;
551}
552
553#define set_fan(offset) \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800554static ssize_t set_fan##offset##_min(struct device *dev, \
555 struct device_attribute *attr, \
556 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{ \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800558 return set_fan_min(dev, buf, offset - 1, count); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559} \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800560static ssize_t set_fan##offset##_div(struct device *dev, \
561 struct device_attribute *attr, \
562 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{ \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800564 return set_fan_div(dev, buf, count, offset - 1); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565} \
566static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
567 show_fan##offset##_min, set_fan##offset##_min); \
568static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
569 show_fan##offset##_div, set_fan##offset##_div);
570set_fan(1);
571set_fan(2);
572
Guenter Roeckc6370db2012-01-14 20:58:08 -0800573static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
574 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct lm87_data *data = lm87_update_device(dev);
577 return sprintf(buf, "%d\n", data->alarms);
578}
579static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
580
Guenter Roeckc6370db2012-01-14 20:58:08 -0800581static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
582 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct lm87_data *data = lm87_update_device(dev);
585 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
586}
587static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
588
Guenter Roeckc6370db2012-01-14 20:58:08 -0800589static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
590 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Jean Delvare90d66192007-10-08 18:24:35 +0200592 struct lm87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return sprintf(buf, "%d\n", data->vrm);
594}
Guenter Roeckc6370db2012-01-14 20:58:08 -0800595static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
596 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100598 struct lm87_data *data = dev_get_drvdata(dev);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800599 unsigned long val;
600 int err;
601
602 err = kstrtoul(buf, 10, &val);
603 if (err)
604 return err;
605 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return count;
607}
608static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
609
Guenter Roeckc6370db2012-01-14 20:58:08 -0800610static ssize_t show_aout(struct device *dev, struct device_attribute *attr,
611 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct lm87_data *data = lm87_update_device(dev);
614 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
615}
Guenter Roeckc6370db2012-01-14 20:58:08 -0800616static ssize_t set_aout(struct device *dev, struct device_attribute *attr,
617 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct i2c_client *client = to_i2c_client(dev);
620 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800621 long val;
622 int err;
623
624 err = kstrtol(buf, 10, &val);
625 if (err)
626 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100628 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 data->aout = AOUT_TO_REG(val);
630 lm87_write_value(client, LM87_REG_AOUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return count;
633}
634static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
635
Jean Delvarec2803b92007-09-26 23:39:01 +0200636static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
637 char *buf)
638{
639 struct lm87_data *data = lm87_update_device(dev);
640 int bitnr = to_sensor_dev_attr(attr)->index;
641 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
642}
643static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
644static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
645static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
646static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
647static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
648static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
649static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
650static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
651static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
652static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
653static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
654static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
655static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
656static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
657static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/*
660 * Real code
661 */
662
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200663static struct attribute *lm87_attributes[] = {
664 &dev_attr_in1_input.attr,
665 &dev_attr_in1_min.attr,
666 &dev_attr_in1_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200667 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200668 &dev_attr_in2_input.attr,
669 &dev_attr_in2_min.attr,
670 &dev_attr_in2_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200671 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200672 &dev_attr_in3_input.attr,
673 &dev_attr_in3_min.attr,
674 &dev_attr_in3_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200675 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200676 &dev_attr_in4_input.attr,
677 &dev_attr_in4_min.attr,
678 &dev_attr_in4_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200679 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200680
681 &dev_attr_temp1_input.attr,
682 &dev_attr_temp1_max.attr,
683 &dev_attr_temp1_min.attr,
684 &dev_attr_temp1_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200685 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200686 &dev_attr_temp2_input.attr,
687 &dev_attr_temp2_max.attr,
688 &dev_attr_temp2_min.attr,
689 &dev_attr_temp2_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200690 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
691 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200692
693 &dev_attr_alarms.attr,
694 &dev_attr_aout_output.attr,
695
696 NULL
697};
698
699static const struct attribute_group lm87_group = {
700 .attrs = lm87_attributes,
701};
702
703static struct attribute *lm87_attributes_opt[] = {
704 &dev_attr_in6_input.attr,
705 &dev_attr_in6_min.attr,
706 &dev_attr_in6_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200707 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200708
709 &dev_attr_fan1_input.attr,
710 &dev_attr_fan1_min.attr,
711 &dev_attr_fan1_div.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200712 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200713
714 &dev_attr_in7_input.attr,
715 &dev_attr_in7_min.attr,
716 &dev_attr_in7_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200717 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200718
719 &dev_attr_fan2_input.attr,
720 &dev_attr_fan2_min.attr,
721 &dev_attr_fan2_div.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200722 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200723
724 &dev_attr_temp3_input.attr,
725 &dev_attr_temp3_max.attr,
726 &dev_attr_temp3_min.attr,
727 &dev_attr_temp3_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200728 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
729 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200730
731 &dev_attr_in0_input.attr,
732 &dev_attr_in0_min.attr,
733 &dev_attr_in0_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200734 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200735 &dev_attr_in5_input.attr,
736 &dev_attr_in5_min.attr,
737 &dev_attr_in5_max.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200738 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200739
740 &dev_attr_cpu0_vid.attr,
741 &dev_attr_vrm.attr,
742
743 NULL
744};
745
746static const struct attribute_group lm87_group_opt = {
747 .attrs = lm87_attributes_opt,
748};
749
Jean Delvarea8884202008-07-16 19:30:14 +0200750/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100751static int lm87_detect(struct i2c_client *new_client,
Jean Delvarea8884202008-07-16 19:30:14 +0200752 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Jean Delvarea8884202008-07-16 19:30:14 +0200754 struct i2c_adapter *adapter = new_client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100755 const char *name;
756 u8 cid, rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvarea8884202008-07-16 19:30:14 +0200759 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Jean Delvare52df6442009-12-09 20:35:57 +0100761 if (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
762 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* Now, we do the remaining detection. */
Jean Delvare52df6442009-12-09 20:35:57 +0100765 cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID);
766 rev = lm87_read_value(new_client, LM87_REG_REVISION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Jean Delvare52df6442009-12-09 20:35:57 +0100768 if (cid == 0x02 /* National Semiconductor */
769 && (rev >= 0x01 && rev <= 0x08))
770 name = "lm87";
771 else if (cid == 0x41 /* Analog Devices */
772 && (rev & 0xf0) == 0x10)
773 name = "adm1024";
774 else {
775 dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
776 new_client->addr);
777 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
Jean Delvare52df6442009-12-09 20:35:57 +0100780 strlcpy(info->type, name, I2C_NAME_SIZE);
Jean Delvarea8884202008-07-16 19:30:14 +0200781
782 return 0;
783}
784
785static int lm87_probe(struct i2c_client *new_client,
786 const struct i2c_device_id *id)
787{
788 struct lm87_data *data;
789 int err;
790
791 data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL);
792 if (!data) {
793 err = -ENOMEM;
794 goto exit;
795 }
796
797 i2c_set_clientdata(new_client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100799 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* Initialize the LM87 chip */
802 lm87_init_client(new_client);
803
804 data->in_scale[0] = 2500;
805 data->in_scale[1] = 2700;
806 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
807 data->in_scale[3] = 5000;
808 data->in_scale[4] = 12000;
809 data->in_scale[5] = 2700;
810 data->in_scale[6] = 1875;
811 data->in_scale[7] = 1875;
812
813 /* Register sysfs hooks */
Guenter Roeckc6370db2012-01-14 20:58:08 -0800814 err = sysfs_create_group(&new_client->dev.kobj, &lm87_group);
815 if (err)
Jean Delvarea8884202008-07-16 19:30:14 +0200816 goto exit_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (data->channel & CHAN_NO_FAN(0)) {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200819 if ((err = device_create_file(&new_client->dev,
820 &dev_attr_in6_input))
821 || (err = device_create_file(&new_client->dev,
822 &dev_attr_in6_min))
823 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200824 &dev_attr_in6_max))
825 || (err = device_create_file(&new_client->dev,
826 &sensor_dev_attr_in6_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200827 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 } else {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200829 if ((err = device_create_file(&new_client->dev,
830 &dev_attr_fan1_input))
831 || (err = device_create_file(&new_client->dev,
832 &dev_attr_fan1_min))
833 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200834 &dev_attr_fan1_div))
835 || (err = device_create_file(&new_client->dev,
836 &sensor_dev_attr_fan1_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200837 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200840 if (data->channel & CHAN_NO_FAN(1)) {
841 if ((err = device_create_file(&new_client->dev,
842 &dev_attr_in7_input))
843 || (err = device_create_file(&new_client->dev,
844 &dev_attr_in7_min))
845 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200846 &dev_attr_in7_max))
847 || (err = device_create_file(&new_client->dev,
848 &sensor_dev_attr_in7_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200849 goto exit_remove;
850 } else {
851 if ((err = device_create_file(&new_client->dev,
852 &dev_attr_fan2_input))
853 || (err = device_create_file(&new_client->dev,
854 &dev_attr_fan2_min))
855 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200856 &dev_attr_fan2_div))
857 || (err = device_create_file(&new_client->dev,
858 &sensor_dev_attr_fan2_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200859 goto exit_remove;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (data->channel & CHAN_TEMP3) {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200863 if ((err = device_create_file(&new_client->dev,
864 &dev_attr_temp3_input))
865 || (err = device_create_file(&new_client->dev,
866 &dev_attr_temp3_max))
867 || (err = device_create_file(&new_client->dev,
868 &dev_attr_temp3_min))
869 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200870 &dev_attr_temp3_crit))
871 || (err = device_create_file(&new_client->dev,
872 &sensor_dev_attr_temp3_alarm.dev_attr))
873 || (err = device_create_file(&new_client->dev,
874 &sensor_dev_attr_temp3_fault.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200875 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 } else {
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200877 if ((err = device_create_file(&new_client->dev,
878 &dev_attr_in0_input))
879 || (err = device_create_file(&new_client->dev,
880 &dev_attr_in0_min))
881 || (err = device_create_file(&new_client->dev,
882 &dev_attr_in0_max))
883 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200884 &sensor_dev_attr_in0_alarm.dev_attr))
885 || (err = device_create_file(&new_client->dev,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200886 &dev_attr_in5_input))
887 || (err = device_create_file(&new_client->dev,
888 &dev_attr_in5_min))
889 || (err = device_create_file(&new_client->dev,
Jean Delvarec2803b92007-09-26 23:39:01 +0200890 &dev_attr_in5_max))
891 || (err = device_create_file(&new_client->dev,
892 &sensor_dev_attr_in5_alarm.dev_attr)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200893 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
896 if (!(data->channel & CHAN_NO_VID)) {
Jean Delvare8a665a02007-05-08 17:21:59 +0200897 data->vrm = vid_which_vrm();
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200898 if ((err = device_create_file(&new_client->dev,
899 &dev_attr_cpu0_vid))
900 || (err = device_create_file(&new_client->dev,
901 &dev_attr_vrm)))
902 goto exit_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
Tony Jones1beeffe2007-08-20 13:46:20 -0700905 data->hwmon_dev = hwmon_device_register(&new_client->dev);
906 if (IS_ERR(data->hwmon_dev)) {
907 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200908 goto exit_remove;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 return 0;
912
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200913exit_remove:
914 sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
915 sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916exit_free:
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200917 lm87_write_value(new_client, LM87_REG_CONFIG, data->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 kfree(data);
919exit:
920 return err;
921}
922
923static void lm87_init_client(struct i2c_client *client)
924{
925 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Ben Hutchings47064d62008-10-17 17:51:12 +0200927 if (client->dev.platform_data) {
928 data->channel = *(u8 *)client->dev.platform_data;
929 lm87_write_value(client,
930 LM87_REG_CHANNEL_MODE, data->channel);
931 } else {
932 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
933 }
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200934 data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200936 if (!(data->config & 0x01)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int i;
938
939 /* Limits are left uninitialized after power-up */
940 for (i = 1; i < 6; i++) {
941 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
942 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
943 }
944 for (i = 0; i < 2; i++) {
945 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
946 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
947 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
948 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
949 }
950 if (data->channel & CHAN_TEMP3) {
951 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
952 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
953 } else {
954 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
955 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
956 }
957 }
Ben Hutchings49ae6cc2008-10-17 17:51:11 +0200958
959 /* Make sure Start is set and INT#_Clear is clear */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200960 if ((data->config & 0x09) != 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 lm87_write_value(client, LM87_REG_CONFIG,
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200962 (data->config & 0x77) | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Jean Delvarea8884202008-07-16 19:30:14 +0200965static int lm87_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400967 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Tony Jones1beeffe2007-08-20 13:46:20 -0700969 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200970 sysfs_remove_group(&client->dev.kobj, &lm87_group);
971 sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400972
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200973 lm87_write_value(client, LM87_REG_CONFIG, data->config);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400974 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976}
977
978static struct lm87_data *lm87_update_device(struct device *dev)
979{
980 struct i2c_client *client = to_i2c_client(dev);
981 struct lm87_data *data = i2c_get_clientdata(client);
982
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100983 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
986 int i, j;
987
988 dev_dbg(&client->dev, "Updating data.\n");
989
990 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
991 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
992 for (; i < j; i++) {
993 data->in[i] = lm87_read_value(client,
994 LM87_REG_IN(i));
995 data->in_min[i] = lm87_read_value(client,
996 LM87_REG_IN_MIN(i));
997 data->in_max[i] = lm87_read_value(client,
998 LM87_REG_IN_MAX(i));
999 }
1000
1001 for (i = 0; i < 2; i++) {
1002 if (data->channel & CHAN_NO_FAN(i)) {
1003 data->in[6+i] = lm87_read_value(client,
1004 LM87_REG_AIN(i));
1005 data->in_max[6+i] = lm87_read_value(client,
1006 LM87_REG_AIN_MAX(i));
1007 data->in_min[6+i] = lm87_read_value(client,
1008 LM87_REG_AIN_MIN(i));
1009
1010 } else {
1011 data->fan[i] = lm87_read_value(client,
1012 LM87_REG_FAN(i));
1013 data->fan_min[i] = lm87_read_value(client,
1014 LM87_REG_FAN_MIN(i));
1015 }
1016 }
1017
1018 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
1019 for (i = 0 ; i < j; i++) {
1020 data->temp[i] = lm87_read_value(client,
1021 LM87_REG_TEMP[i]);
1022 data->temp_high[i] = lm87_read_value(client,
1023 LM87_REG_TEMP_HIGH[i]);
1024 data->temp_low[i] = lm87_read_value(client,
1025 LM87_REG_TEMP_LOW[i]);
1026 }
1027
1028 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
1029 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
1030 data->temp_crit_int = min(i, j);
1031
1032 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
1033 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
1034 data->temp_crit_ext = min(i, j);
1035
1036 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
1037 data->fan_div[0] = (i >> 4) & 0x03;
1038 data->fan_div[1] = (i >> 6) & 0x03;
1039 data->vid = (i & 0x0F)
1040 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
1041 << 4;
1042
1043 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
1044 | (lm87_read_value(client, LM87_REG_ALARMS2)
1045 << 8);
1046 data->aout = lm87_read_value(client, LM87_REG_AOUT);
1047
1048 data->last_updated = jiffies;
1049 data->valid = 1;
1050 }
1051
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001052 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 return data;
1055}
1056
Axel Linf0967ee2012-01-20 15:38:18 +08001057module_i2c_driver(lm87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
1060MODULE_DESCRIPTION("LM87 driver");
1061MODULE_LICENSE("GPL");