blob: a5e295826aeae10fdbc1e517445f953d644f8811 [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 Delvare7c81c602014-01-29 20:40:08 +01008 * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de>
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * Client data (each client gets its own)
154 */
155
156struct lm87_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700157 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100158 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 char valid; /* zero until following fields are valid */
160 unsigned long last_updated; /* In jiffies */
161
162 u8 channel; /* register value */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200163 u8 config; /* original register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 u8 in[8]; /* register value */
166 u8 in_max[8]; /* register value */
167 u8 in_min[8]; /* register value */
168 u16 in_scale[8];
169
170 s8 temp[3]; /* register value */
171 s8 temp_high[3]; /* register value */
172 s8 temp_low[3]; /* register value */
173 s8 temp_crit_int; /* min of two register values */
174 s8 temp_crit_ext; /* min of two register values */
175
176 u8 fan[2]; /* register value */
177 u8 fan_min[2]; /* register value */
178 u8 fan_div[2]; /* register value, shifted right */
179 u8 aout; /* register value */
180
181 u16 alarms; /* register values, combined */
182 u8 vid; /* register values, combined */
183 u8 vrm;
184};
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static inline int lm87_read_value(struct i2c_client *client, u8 reg)
187{
188 return i2c_smbus_read_byte_data(client, reg);
189}
190
191static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
192{
193 return i2c_smbus_write_byte_data(client, reg, value);
194}
195
Jean Delvare8652a262012-01-26 09:37:50 -0800196static struct lm87_data *lm87_update_device(struct device *dev)
197{
198 struct i2c_client *client = to_i2c_client(dev);
199 struct lm87_data *data = i2c_get_clientdata(client);
200
201 mutex_lock(&data->update_lock);
202
203 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
204 int i, j;
205
206 dev_dbg(&client->dev, "Updating data.\n");
207
208 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
209 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
210 for (; i < j; i++) {
211 data->in[i] = lm87_read_value(client,
212 LM87_REG_IN(i));
213 data->in_min[i] = lm87_read_value(client,
214 LM87_REG_IN_MIN(i));
215 data->in_max[i] = lm87_read_value(client,
216 LM87_REG_IN_MAX(i));
217 }
218
219 for (i = 0; i < 2; i++) {
220 if (data->channel & CHAN_NO_FAN(i)) {
221 data->in[6+i] = lm87_read_value(client,
222 LM87_REG_AIN(i));
223 data->in_max[6+i] = lm87_read_value(client,
224 LM87_REG_AIN_MAX(i));
225 data->in_min[6+i] = lm87_read_value(client,
226 LM87_REG_AIN_MIN(i));
227
228 } else {
229 data->fan[i] = lm87_read_value(client,
230 LM87_REG_FAN(i));
231 data->fan_min[i] = lm87_read_value(client,
232 LM87_REG_FAN_MIN(i));
233 }
234 }
235
236 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
237 for (i = 0 ; i < j; i++) {
238 data->temp[i] = lm87_read_value(client,
239 LM87_REG_TEMP[i]);
240 data->temp_high[i] = lm87_read_value(client,
241 LM87_REG_TEMP_HIGH[i]);
242 data->temp_low[i] = lm87_read_value(client,
243 LM87_REG_TEMP_LOW[i]);
244 }
245
246 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
247 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
248 data->temp_crit_int = min(i, j);
249
250 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
251 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
252 data->temp_crit_ext = min(i, j);
253
254 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
255 data->fan_div[0] = (i >> 4) & 0x03;
256 data->fan_div[1] = (i >> 6) & 0x03;
257 data->vid = (i & 0x0F)
258 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
259 << 4;
260
261 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
262 | (lm87_read_value(client, LM87_REG_ALARMS2)
263 << 8);
264 data->aout = lm87_read_value(client, LM87_REG_AOUT);
265
266 data->last_updated = jiffies;
267 data->valid = 1;
268 }
269
270 mutex_unlock(&data->update_lock);
271
272 return data;
273}
274
275/*
276 * Sysfs stuff
277 */
278
Jean Delvare0e190b72012-01-26 09:38:26 -0800279static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
280 char *buf)
281{
282 struct lm87_data *data = lm87_update_device(dev);
283 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jean Delvare0e190b72012-01-26 09:38:26 -0800285 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[nr],
286 data->in_scale[nr]));
287}
288
289static ssize_t show_in_min(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
292 struct lm87_data *data = lm87_update_device(dev);
293 int nr = to_sensor_dev_attr(attr)->index;
294
295 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[nr],
296 data->in_scale[nr]));
297}
298
299static ssize_t show_in_max(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct lm87_data *data = lm87_update_device(dev);
303 int nr = to_sensor_dev_attr(attr)->index;
304
305 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[nr],
306 data->in_scale[nr]));
307}
308
309static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct i2c_client *client = to_i2c_client(dev);
313 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800314 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800315 long val;
316 int err;
317
318 err = kstrtol(buf, 10, &val);
319 if (err)
320 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100322 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800324 lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) :
325 LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100326 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800327 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Jean Delvare0e190b72012-01-26 09:38:26 -0800330static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
331 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct i2c_client *client = to_i2c_client(dev);
334 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800335 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800336 long val;
337 int err;
338
339 err = kstrtol(buf, 10, &val);
340 if (err)
341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100343 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800345 lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) :
346 LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100347 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800348 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351#define set_in(offset) \
Jean Delvare0e190b72012-01-26 09:38:26 -0800352static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
353 show_in_input, NULL, offset); \
354static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
355 show_in_min, set_in_min, offset); \
356static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
357 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358set_in(0);
359set_in(1);
360set_in(2);
361set_in(3);
362set_in(4);
363set_in(5);
364set_in(6);
365set_in(7);
366
Jean Delvare0e190b72012-01-26 09:38:26 -0800367static ssize_t show_temp_input(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
370 struct lm87_data *data = lm87_update_device(dev);
371 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Jean Delvare0e190b72012-01-26 09:38:26 -0800373 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
374}
375
376static ssize_t show_temp_low(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
379 struct lm87_data *data = lm87_update_device(dev);
380 int nr = to_sensor_dev_attr(attr)->index;
381
382 return sprintf(buf, "%d\n",
383 TEMP_FROM_REG(data->temp_low[nr]));
384}
385
386static ssize_t show_temp_high(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 struct lm87_data *data = lm87_update_device(dev);
390 int nr = to_sensor_dev_attr(attr)->index;
391
392 return sprintf(buf, "%d\n",
393 TEMP_FROM_REG(data->temp_high[nr]));
394}
395
396static ssize_t set_temp_low(struct device *dev, struct device_attribute *attr,
397 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct i2c_client *client = to_i2c_client(dev);
400 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800401 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800402 long val;
403 int err;
404
405 err = kstrtol(buf, 10, &val);
406 if (err)
407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100409 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 data->temp_low[nr] = TEMP_TO_REG(val);
411 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100412 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800413 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Jean Delvare0e190b72012-01-26 09:38:26 -0800416static ssize_t set_temp_high(struct device *dev, struct device_attribute *attr,
417 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct i2c_client *client = to_i2c_client(dev);
420 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800421 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800422 long val;
423 int err;
424
425 err = kstrtol(buf, 10, &val);
426 if (err)
427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100429 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 data->temp_high[nr] = TEMP_TO_REG(val);
431 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100432 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800433 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436#define set_temp(offset) \
Jean Delvare0e190b72012-01-26 09:38:26 -0800437static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
438 show_temp_input, NULL, offset - 1); \
439static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
440 show_temp_high, set_temp_high, offset - 1); \
441static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
442 show_temp_low, set_temp_low, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443set_temp(1);
444set_temp(2);
445set_temp(3);
446
Guenter Roeckc6370db2012-01-14 20:58:08 -0800447static ssize_t show_temp_crit_int(struct device *dev,
448 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct lm87_data *data = lm87_update_device(dev);
451 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
452}
453
Guenter Roeckc6370db2012-01-14 20:58:08 -0800454static ssize_t show_temp_crit_ext(struct device *dev,
455 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct lm87_data *data = lm87_update_device(dev);
458 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
459}
460
461static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
462static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
463static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
464
Jean Delvare0e190b72012-01-26 09:38:26 -0800465static ssize_t show_fan_input(struct device *dev,
466 struct device_attribute *attr, char *buf)
467{
468 struct lm87_data *data = lm87_update_device(dev);
469 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jean Delvare0e190b72012-01-26 09:38:26 -0800471 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
472 FAN_DIV_FROM_REG(data->fan_div[nr])));
473}
474
475static ssize_t show_fan_min(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
478 struct lm87_data *data = lm87_update_device(dev);
479 int nr = to_sensor_dev_attr(attr)->index;
480
481 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
482 FAN_DIV_FROM_REG(data->fan_div[nr])));
483}
484
485static ssize_t show_fan_div(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
488 struct lm87_data *data = lm87_update_device(dev);
489 int nr = to_sensor_dev_attr(attr)->index;
490
491 return sprintf(buf, "%d\n",
492 FAN_DIV_FROM_REG(data->fan_div[nr]));
493}
494
495static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
496 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct i2c_client *client = to_i2c_client(dev);
499 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800500 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800501 long val;
502 int err;
503
504 err = kstrtol(buf, 10, &val);
505 if (err)
506 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 data->fan_min[nr] = FAN_TO_REG(val,
510 FAN_DIV_FROM_REG(data->fan_div[nr]));
511 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100512 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800513 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Guenter Roeckc6370db2012-01-14 20:58:08 -0800516/*
517 * Note: we save and restore the fan minimum here, because its value is
518 * determined in part by the fan clock divider. This follows the principle
519 * of least surprise; the user doesn't expect the fan minimum to change just
520 * because the divider changed.
521 */
Jean Delvare0e190b72012-01-26 09:38:26 -0800522static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
523 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct i2c_client *client = to_i2c_client(dev);
526 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800527 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800528 long val;
529 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 unsigned long min;
531 u8 reg;
532
Guenter Roeckc6370db2012-01-14 20:58:08 -0800533 err = kstrtol(buf, 10, &val);
534 if (err)
535 return err;
536
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100537 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 min = FAN_FROM_REG(data->fan_min[nr],
539 FAN_DIV_FROM_REG(data->fan_div[nr]));
540
541 switch (val) {
Guenter Roeckc6370db2012-01-14 20:58:08 -0800542 case 1:
543 data->fan_div[nr] = 0;
544 break;
545 case 2:
546 data->fan_div[nr] = 1;
547 break;
548 case 4:
549 data->fan_div[nr] = 2;
550 break;
551 case 8:
552 data->fan_div[nr] = 3;
553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100555 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -EINVAL;
557 }
558
559 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
560 switch (nr) {
561 case 0:
562 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
563 break;
564 case 1:
565 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
566 break;
567 }
568 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
569
570 data->fan_min[nr] = FAN_TO_REG(min, val);
571 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
572 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100573 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 return count;
576}
577
578#define set_fan(offset) \
Jean Delvare0e190b72012-01-26 09:38:26 -0800579static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
580 show_fan_input, NULL, offset - 1); \
581static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
582 show_fan_min, set_fan_min, offset - 1); \
583static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
584 show_fan_div, set_fan_div, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585set_fan(1);
586set_fan(2);
587
Guenter Roeckc6370db2012-01-14 20:58:08 -0800588static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
589 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct lm87_data *data = lm87_update_device(dev);
592 return sprintf(buf, "%d\n", data->alarms);
593}
594static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
595
Guenter Roeckc6370db2012-01-14 20:58:08 -0800596static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
597 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct lm87_data *data = lm87_update_device(dev);
600 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
601}
602static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
603
Guenter Roeckc6370db2012-01-14 20:58:08 -0800604static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
605 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Jean Delvare90d66192007-10-08 18:24:35 +0200607 struct lm87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return sprintf(buf, "%d\n", data->vrm);
609}
Guenter Roeckc6370db2012-01-14 20:58:08 -0800610static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
611 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100613 struct lm87_data *data = dev_get_drvdata(dev);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800614 unsigned long val;
615 int err;
616
617 err = kstrtoul(buf, 10, &val);
618 if (err)
619 return err;
Axel Linfa642d92014-08-06 08:24:00 +0800620
621 if (val > 255)
622 return -EINVAL;
623
Guenter Roeckc6370db2012-01-14 20:58:08 -0800624 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return count;
626}
627static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
628
Guenter Roeckc6370db2012-01-14 20:58:08 -0800629static ssize_t show_aout(struct device *dev, struct device_attribute *attr,
630 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct lm87_data *data = lm87_update_device(dev);
633 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
634}
Guenter Roeckc6370db2012-01-14 20:58:08 -0800635static ssize_t set_aout(struct device *dev, struct device_attribute *attr,
636 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct i2c_client *client = to_i2c_client(dev);
639 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800640 long val;
641 int err;
642
643 err = kstrtol(buf, 10, &val);
644 if (err)
645 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100647 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 data->aout = AOUT_TO_REG(val);
649 lm87_write_value(client, LM87_REG_AOUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100650 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return count;
652}
653static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
654
Jean Delvarec2803b92007-09-26 23:39:01 +0200655static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
656 char *buf)
657{
658 struct lm87_data *data = lm87_update_device(dev);
659 int bitnr = to_sensor_dev_attr(attr)->index;
660 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
661}
662static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
663static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
664static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
665static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
666static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
667static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
668static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
669static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
670static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
671static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
672static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
673static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
674static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
675static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
676static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/*
679 * Real code
680 */
681
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200682static struct attribute *lm87_attributes[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800683 &sensor_dev_attr_in1_input.dev_attr.attr,
684 &sensor_dev_attr_in1_min.dev_attr.attr,
685 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200686 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800687 &sensor_dev_attr_in2_input.dev_attr.attr,
688 &sensor_dev_attr_in2_min.dev_attr.attr,
689 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200690 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800691 &sensor_dev_attr_in3_input.dev_attr.attr,
692 &sensor_dev_attr_in3_min.dev_attr.attr,
693 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200694 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800695 &sensor_dev_attr_in4_input.dev_attr.attr,
696 &sensor_dev_attr_in4_min.dev_attr.attr,
697 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200698 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200699
Jean Delvare0e190b72012-01-26 09:38:26 -0800700 &sensor_dev_attr_temp1_input.dev_attr.attr,
701 &sensor_dev_attr_temp1_max.dev_attr.attr,
702 &sensor_dev_attr_temp1_min.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200703 &dev_attr_temp1_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200704 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800705 &sensor_dev_attr_temp2_input.dev_attr.attr,
706 &sensor_dev_attr_temp2_max.dev_attr.attr,
707 &sensor_dev_attr_temp2_min.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200708 &dev_attr_temp2_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200709 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
710 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200711
712 &dev_attr_alarms.attr,
713 &dev_attr_aout_output.attr,
714
715 NULL
716};
717
718static const struct attribute_group lm87_group = {
719 .attrs = lm87_attributes,
720};
721
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800722static struct attribute *lm87_attributes_in6[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800723 &sensor_dev_attr_in6_input.dev_attr.attr,
724 &sensor_dev_attr_in6_min.dev_attr.attr,
725 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200726 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800727 NULL
728};
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200729
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800730static const struct attribute_group lm87_group_in6 = {
731 .attrs = lm87_attributes_in6,
732};
733
734static struct attribute *lm87_attributes_fan1[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800735 &sensor_dev_attr_fan1_input.dev_attr.attr,
736 &sensor_dev_attr_fan1_min.dev_attr.attr,
737 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200738 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800739 NULL
740};
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200741
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800742static const struct attribute_group lm87_group_fan1 = {
743 .attrs = lm87_attributes_fan1,
744};
745
746static struct attribute *lm87_attributes_in7[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800747 &sensor_dev_attr_in7_input.dev_attr.attr,
748 &sensor_dev_attr_in7_min.dev_attr.attr,
749 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200750 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800751 NULL
752};
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200753
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800754static const struct attribute_group lm87_group_in7 = {
755 .attrs = lm87_attributes_in7,
756};
757
758static struct attribute *lm87_attributes_fan2[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800759 &sensor_dev_attr_fan2_input.dev_attr.attr,
760 &sensor_dev_attr_fan2_min.dev_attr.attr,
761 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200762 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800763 NULL
764};
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200765
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800766static const struct attribute_group lm87_group_fan2 = {
767 .attrs = lm87_attributes_fan2,
768};
769
770static struct attribute *lm87_attributes_temp3[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800771 &sensor_dev_attr_temp3_input.dev_attr.attr,
772 &sensor_dev_attr_temp3_max.dev_attr.attr,
773 &sensor_dev_attr_temp3_min.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200774 &dev_attr_temp3_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200775 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
776 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800777 NULL
778};
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200779
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800780static const struct attribute_group lm87_group_temp3 = {
781 .attrs = lm87_attributes_temp3,
782};
783
784static struct attribute *lm87_attributes_in0_5[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800785 &sensor_dev_attr_in0_input.dev_attr.attr,
786 &sensor_dev_attr_in0_min.dev_attr.attr,
787 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200788 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800789 &sensor_dev_attr_in5_input.dev_attr.attr,
790 &sensor_dev_attr_in5_min.dev_attr.attr,
791 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200792 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200793 NULL
794};
795
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800796static const struct attribute_group lm87_group_in0_5 = {
797 .attrs = lm87_attributes_in0_5,
798};
799
800static struct attribute *lm87_attributes_vid[] = {
801 &dev_attr_cpu0_vid.attr,
802 &dev_attr_vrm.attr,
803 NULL
804};
805
806static const struct attribute_group lm87_group_vid = {
807 .attrs = lm87_attributes_vid,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200808};
809
Jean Delvarea8884202008-07-16 19:30:14 +0200810/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare8652a262012-01-26 09:37:50 -0800811static int lm87_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Jean Delvare8652a262012-01-26 09:37:50 -0800813 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100814 const char *name;
815 u8 cid, rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvarea8884202008-07-16 19:30:14 +0200818 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Jean Delvare8652a262012-01-26 09:37:50 -0800820 if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80)
Jean Delvare52df6442009-12-09 20:35:57 +0100821 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* Now, we do the remaining detection. */
Jean Delvare8652a262012-01-26 09:37:50 -0800824 cid = lm87_read_value(client, LM87_REG_COMPANY_ID);
825 rev = lm87_read_value(client, LM87_REG_REVISION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jean Delvare52df6442009-12-09 20:35:57 +0100827 if (cid == 0x02 /* National Semiconductor */
828 && (rev >= 0x01 && rev <= 0x08))
829 name = "lm87";
830 else if (cid == 0x41 /* Analog Devices */
831 && (rev & 0xf0) == 0x10)
832 name = "adm1024";
833 else {
834 dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
Jean Delvare8652a262012-01-26 09:37:50 -0800835 client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +0100836 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Jean Delvare52df6442009-12-09 20:35:57 +0100839 strlcpy(info->type, name, I2C_NAME_SIZE);
Jean Delvarea8884202008-07-16 19:30:14 +0200840
841 return 0;
842}
843
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800844static void lm87_remove_files(struct i2c_client *client)
845{
846 struct device *dev = &client->dev;
847
848 sysfs_remove_group(&dev->kobj, &lm87_group);
849 sysfs_remove_group(&dev->kobj, &lm87_group_in6);
850 sysfs_remove_group(&dev->kobj, &lm87_group_fan1);
851 sysfs_remove_group(&dev->kobj, &lm87_group_in7);
852 sysfs_remove_group(&dev->kobj, &lm87_group_fan2);
853 sysfs_remove_group(&dev->kobj, &lm87_group_temp3);
854 sysfs_remove_group(&dev->kobj, &lm87_group_in0_5);
855 sysfs_remove_group(&dev->kobj, &lm87_group_vid);
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static void lm87_init_client(struct i2c_client *client)
859{
860 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900862 if (dev_get_platdata(&client->dev)) {
863 data->channel = *(u8 *)dev_get_platdata(&client->dev);
Ben Hutchings47064d62008-10-17 17:51:12 +0200864 lm87_write_value(client,
865 LM87_REG_CHANNEL_MODE, data->channel);
866 } else {
867 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
868 }
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200869 data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200871 if (!(data->config & 0x01)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int i;
873
874 /* Limits are left uninitialized after power-up */
875 for (i = 1; i < 6; i++) {
876 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
877 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
878 }
879 for (i = 0; i < 2; i++) {
880 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
881 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
882 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
883 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
884 }
885 if (data->channel & CHAN_TEMP3) {
886 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
887 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
888 } else {
889 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
890 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
891 }
892 }
Ben Hutchings49ae6cc2008-10-17 17:51:11 +0200893
894 /* Make sure Start is set and INT#_Clear is clear */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200895 if ((data->config & 0x09) != 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 lm87_write_value(client, LM87_REG_CONFIG,
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200897 (data->config & 0x77) | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Jean Delvare8652a262012-01-26 09:37:50 -0800900static int lm87_probe(struct i2c_client *client, const struct i2c_device_id *id)
901{
902 struct lm87_data *data;
903 int err;
904
Guenter Roeck5ff512b2012-06-02 09:58:10 -0700905 data = devm_kzalloc(&client->dev, sizeof(struct lm87_data), GFP_KERNEL);
906 if (!data)
907 return -ENOMEM;
Jean Delvare8652a262012-01-26 09:37:50 -0800908
909 i2c_set_clientdata(client, data);
Jean Delvare8652a262012-01-26 09:37:50 -0800910 mutex_init(&data->update_lock);
911
912 /* Initialize the LM87 chip */
913 lm87_init_client(client);
914
915 data->in_scale[0] = 2500;
916 data->in_scale[1] = 2700;
917 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
918 data->in_scale[3] = 5000;
919 data->in_scale[4] = 12000;
920 data->in_scale[5] = 2700;
921 data->in_scale[6] = 1875;
922 data->in_scale[7] = 1875;
923
924 /* Register sysfs hooks */
925 err = sysfs_create_group(&client->dev.kobj, &lm87_group);
926 if (err)
Guenter Roeck5ff512b2012-06-02 09:58:10 -0700927 goto exit_stop;
Jean Delvare8652a262012-01-26 09:37:50 -0800928
929 if (data->channel & CHAN_NO_FAN(0)) {
930 err = sysfs_create_group(&client->dev.kobj, &lm87_group_in6);
931 if (err)
932 goto exit_remove;
933 } else {
934 err = sysfs_create_group(&client->dev.kobj, &lm87_group_fan1);
935 if (err)
936 goto exit_remove;
937 }
938
939 if (data->channel & CHAN_NO_FAN(1)) {
940 err = sysfs_create_group(&client->dev.kobj, &lm87_group_in7);
941 if (err)
942 goto exit_remove;
943 } else {
944 err = sysfs_create_group(&client->dev.kobj, &lm87_group_fan2);
945 if (err)
946 goto exit_remove;
947 }
948
949 if (data->channel & CHAN_TEMP3) {
950 err = sysfs_create_group(&client->dev.kobj, &lm87_group_temp3);
951 if (err)
952 goto exit_remove;
953 } else {
954 err = sysfs_create_group(&client->dev.kobj, &lm87_group_in0_5);
955 if (err)
956 goto exit_remove;
957 }
958
959 if (!(data->channel & CHAN_NO_VID)) {
960 data->vrm = vid_which_vrm();
961 err = sysfs_create_group(&client->dev.kobj, &lm87_group_vid);
962 if (err)
963 goto exit_remove;
964 }
965
966 data->hwmon_dev = hwmon_device_register(&client->dev);
967 if (IS_ERR(data->hwmon_dev)) {
968 err = PTR_ERR(data->hwmon_dev);
969 goto exit_remove;
970 }
971
972 return 0;
973
974exit_remove:
975 lm87_remove_files(client);
Guenter Roeck5ff512b2012-06-02 09:58:10 -0700976exit_stop:
Jean Delvare8652a262012-01-26 09:37:50 -0800977 lm87_write_value(client, LM87_REG_CONFIG, data->config);
Jean Delvare8652a262012-01-26 09:37:50 -0800978 return err;
979}
980
Jean Delvarea8884202008-07-16 19:30:14 +0200981static int lm87_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400983 struct lm87_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Tony Jones1beeffe2007-08-20 13:46:20 -0700985 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800986 lm87_remove_files(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400987
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200988 lm87_write_value(client, LM87_REG_CONFIG, data->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 0;
990}
991
Jean Delvare8652a262012-01-26 09:37:50 -0800992/*
993 * Driver data (common to all clients)
994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Jean Delvare8652a262012-01-26 09:37:50 -0800996static const struct i2c_device_id lm87_id[] = {
997 { "lm87", lm87 },
998 { "adm1024", adm1024 },
999 { }
1000};
1001MODULE_DEVICE_TABLE(i2c, lm87_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Jean Delvare8652a262012-01-26 09:37:50 -08001003static struct i2c_driver lm87_driver = {
1004 .class = I2C_CLASS_HWMON,
1005 .driver = {
1006 .name = "lm87",
1007 },
1008 .probe = lm87_probe,
1009 .remove = lm87_remove,
1010 .id_table = lm87_id,
1011 .detect = lm87_detect,
1012 .address_list = normal_i2c,
1013};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Axel Linf0967ee2012-01-20 15:38:18 +08001015module_i2c_driver(lm87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Jean Delvare7c81c602014-01-29 20:40:08 +01001017MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de> and others");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018MODULE_DESCRIPTION("LM87 driver");
1019MODULE_LICENSE("GPL");