blob: 04a7a1ddb030cd09e934c497d4653112af6fdc04 [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 Delvare7c81c60f2014-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>
Mahoda Ratnayaka67043d12017-03-31 12:01:00 +130069#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * Addresses to scan
73 * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
74 */
75
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050076static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Jean Delvaree5e9f442009-12-14 21:17:27 +010078enum chips { lm87, adm1024 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
81 * The LM87 registers
82 */
83
84/* nr in 0..5 */
85#define LM87_REG_IN(nr) (0x20 + (nr))
86#define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
87#define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
88/* nr in 0..1 */
89#define LM87_REG_AIN(nr) (0x28 + (nr))
90#define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
91#define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
92
93static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
94static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
95static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
96
97#define LM87_REG_TEMP_HW_INT_LOCK 0x13
98#define LM87_REG_TEMP_HW_EXT_LOCK 0x14
99#define LM87_REG_TEMP_HW_INT 0x17
100#define LM87_REG_TEMP_HW_EXT 0x18
101
102/* nr in 0..1 */
103#define LM87_REG_FAN(nr) (0x28 + (nr))
104#define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
105#define LM87_REG_AOUT 0x19
106
107#define LM87_REG_CONFIG 0x40
108#define LM87_REG_CHANNEL_MODE 0x16
109#define LM87_REG_VID_FAN_DIV 0x47
110#define LM87_REG_VID4 0x49
111
112#define LM87_REG_ALARMS1 0x41
113#define LM87_REG_ALARMS2 0x42
114
115#define LM87_REG_COMPANY_ID 0x3E
116#define LM87_REG_REVISION 0x3F
117
118/*
119 * Conversions and various macros
120 * The LM87 uses signed 8-bit values for temperatures.
121 */
122
Guenter Roeckc6370db2012-01-14 20:58:08 -0800123#define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192)
124#define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \
Guenter Roeck12fa55c2016-12-04 18:16:02 -0800125 (val) >= (scale) * 255 / 192 ? 255 : \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800126 ((val) * 192 + (scale) / 2) / (scale))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128#define TEMP_FROM_REG(reg) ((reg) * 1000)
129#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
130 (val) >= 126500 ? 127 : \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800131 (((val) < 0 ? (val) - 500 : \
132 (val) + 500) / 1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Guenter Roeckc6370db2012-01-14 20:58:08 -0800134#define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \
135 (1350000 + (reg)*(div) / 2) / ((reg) * (div)))
136#define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \
137 (1350000 + (val)*(div) / 2) / ((val) * (div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139#define FAN_DIV_FROM_REG(reg) (1 << (reg))
140
141/* analog out is 9.80mV/LSB */
142#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
143#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
144 (val) >= 2500 ? 255 : \
145 ((val) * 10 + 49) / 98)
146
147/* nr in 0..1 */
148#define CHAN_NO_FAN(nr) (1 << (nr))
149#define CHAN_TEMP3 (1 << 2)
150#define CHAN_VCC_5V (1 << 3)
Jean Delvare889af3d2007-10-08 22:48:10 +0200151#define CHAN_NO_VID (1 << 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Client data (each client gets its own)
155 */
156
157struct lm87_data {
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;
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600184
185 const struct attribute_group *attr_groups[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static inline int lm87_read_value(struct i2c_client *client, u8 reg)
189{
190 return i2c_smbus_read_byte_data(client, reg);
191}
192
193static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
194{
195 return i2c_smbus_write_byte_data(client, reg, value);
196}
197
Jean Delvare8652a262012-01-26 09:37:50 -0800198static struct lm87_data *lm87_update_device(struct device *dev)
199{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600200 struct i2c_client *client = dev_get_drvdata(dev);
Jean Delvare8652a262012-01-26 09:37:50 -0800201 struct lm87_data *data = i2c_get_clientdata(client);
202
203 mutex_lock(&data->update_lock);
204
205 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
206 int i, j;
207
208 dev_dbg(&client->dev, "Updating data.\n");
209
210 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
211 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
212 for (; i < j; i++) {
213 data->in[i] = lm87_read_value(client,
214 LM87_REG_IN(i));
215 data->in_min[i] = lm87_read_value(client,
216 LM87_REG_IN_MIN(i));
217 data->in_max[i] = lm87_read_value(client,
218 LM87_REG_IN_MAX(i));
219 }
220
221 for (i = 0; i < 2; i++) {
222 if (data->channel & CHAN_NO_FAN(i)) {
223 data->in[6+i] = lm87_read_value(client,
224 LM87_REG_AIN(i));
225 data->in_max[6+i] = lm87_read_value(client,
226 LM87_REG_AIN_MAX(i));
227 data->in_min[6+i] = lm87_read_value(client,
228 LM87_REG_AIN_MIN(i));
229
230 } else {
231 data->fan[i] = lm87_read_value(client,
232 LM87_REG_FAN(i));
233 data->fan_min[i] = lm87_read_value(client,
234 LM87_REG_FAN_MIN(i));
235 }
236 }
237
238 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
239 for (i = 0 ; i < j; i++) {
240 data->temp[i] = lm87_read_value(client,
241 LM87_REG_TEMP[i]);
242 data->temp_high[i] = lm87_read_value(client,
243 LM87_REG_TEMP_HIGH[i]);
244 data->temp_low[i] = lm87_read_value(client,
245 LM87_REG_TEMP_LOW[i]);
246 }
247
248 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
249 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
250 data->temp_crit_int = min(i, j);
251
252 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
253 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
254 data->temp_crit_ext = min(i, j);
255
256 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
257 data->fan_div[0] = (i >> 4) & 0x03;
258 data->fan_div[1] = (i >> 6) & 0x03;
259 data->vid = (i & 0x0F)
260 | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
261 << 4;
262
263 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
264 | (lm87_read_value(client, LM87_REG_ALARMS2)
265 << 8);
266 data->aout = lm87_read_value(client, LM87_REG_AOUT);
267
268 data->last_updated = jiffies;
269 data->valid = 1;
270 }
271
272 mutex_unlock(&data->update_lock);
273
274 return data;
275}
276
277/*
278 * Sysfs stuff
279 */
280
Jean Delvare0e190b72012-01-26 09:38:26 -0800281static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
282 char *buf)
283{
284 struct lm87_data *data = lm87_update_device(dev);
285 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jean Delvare0e190b72012-01-26 09:38:26 -0800287 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[nr],
288 data->in_scale[nr]));
289}
290
291static ssize_t show_in_min(struct device *dev,
292 struct device_attribute *attr, char *buf)
293{
294 struct lm87_data *data = lm87_update_device(dev);
295 int nr = to_sensor_dev_attr(attr)->index;
296
297 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[nr],
298 data->in_scale[nr]));
299}
300
301static ssize_t show_in_max(struct device *dev,
302 struct device_attribute *attr, char *buf)
303{
304 struct lm87_data *data = lm87_update_device(dev);
305 int nr = to_sensor_dev_attr(attr)->index;
306
307 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[nr],
308 data->in_scale[nr]));
309}
310
311static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
312 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600314 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800316 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800317 long val;
318 int err;
319
320 err = kstrtol(buf, 10, &val);
321 if (err)
322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100324 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800326 lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) :
327 LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100328 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800329 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Jean Delvare0e190b72012-01-26 09:38:26 -0800332static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
333 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600335 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800337 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800338 long val;
339 int err;
340
341 err = kstrtol(buf, 10, &val);
342 if (err)
343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100345 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800347 lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) :
348 LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100349 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800350 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353#define set_in(offset) \
Jean Delvare0e190b72012-01-26 09:38:26 -0800354static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
355 show_in_input, NULL, offset); \
356static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
357 show_in_min, set_in_min, offset); \
358static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
359 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360set_in(0);
361set_in(1);
362set_in(2);
363set_in(3);
364set_in(4);
365set_in(5);
366set_in(6);
367set_in(7);
368
Jean Delvare0e190b72012-01-26 09:38:26 -0800369static ssize_t show_temp_input(struct device *dev,
370 struct device_attribute *attr, char *buf)
371{
372 struct lm87_data *data = lm87_update_device(dev);
373 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jean Delvare0e190b72012-01-26 09:38:26 -0800375 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
376}
377
378static ssize_t show_temp_low(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
381 struct lm87_data *data = lm87_update_device(dev);
382 int nr = to_sensor_dev_attr(attr)->index;
383
384 return sprintf(buf, "%d\n",
385 TEMP_FROM_REG(data->temp_low[nr]));
386}
387
388static ssize_t show_temp_high(struct device *dev,
389 struct device_attribute *attr, char *buf)
390{
391 struct lm87_data *data = lm87_update_device(dev);
392 int nr = to_sensor_dev_attr(attr)->index;
393
394 return sprintf(buf, "%d\n",
395 TEMP_FROM_REG(data->temp_high[nr]));
396}
397
398static ssize_t set_temp_low(struct device *dev, struct device_attribute *attr,
399 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600401 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800403 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800404 long val;
405 int err;
406
407 err = kstrtol(buf, 10, &val);
408 if (err)
409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100411 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 data->temp_low[nr] = TEMP_TO_REG(val);
413 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100414 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800415 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Jean Delvare0e190b72012-01-26 09:38:26 -0800418static ssize_t set_temp_high(struct device *dev, struct device_attribute *attr,
419 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600421 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800423 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800424 long val;
425 int err;
426
427 err = kstrtol(buf, 10, &val);
428 if (err)
429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100431 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 data->temp_high[nr] = TEMP_TO_REG(val);
433 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100434 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800435 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438#define set_temp(offset) \
Jean Delvare0e190b72012-01-26 09:38:26 -0800439static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
440 show_temp_input, NULL, offset - 1); \
441static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
442 show_temp_high, set_temp_high, offset - 1); \
443static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
444 show_temp_low, set_temp_low, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445set_temp(1);
446set_temp(2);
447set_temp(3);
448
Julia Lawall07a366c2016-12-22 13:04:53 +0100449static ssize_t temp1_crit_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct lm87_data *data = lm87_update_device(dev);
453 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
454}
455
Julia Lawall07a366c2016-12-22 13:04:53 +0100456static ssize_t temp2_crit_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct lm87_data *data = lm87_update_device(dev);
460 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
461}
462
Julia Lawall07a366c2016-12-22 13:04:53 +0100463static DEVICE_ATTR_RO(temp1_crit);
464static DEVICE_ATTR_RO(temp2_crit);
465static DEVICE_ATTR(temp3_crit, S_IRUGO, temp2_crit_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Jean Delvare0e190b72012-01-26 09:38:26 -0800467static ssize_t show_fan_input(struct device *dev,
468 struct device_attribute *attr, char *buf)
469{
470 struct lm87_data *data = lm87_update_device(dev);
471 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Jean Delvare0e190b72012-01-26 09:38:26 -0800473 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
474 FAN_DIV_FROM_REG(data->fan_div[nr])));
475}
476
477static ssize_t show_fan_min(struct device *dev,
478 struct device_attribute *attr, char *buf)
479{
480 struct lm87_data *data = lm87_update_device(dev);
481 int nr = to_sensor_dev_attr(attr)->index;
482
483 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
484 FAN_DIV_FROM_REG(data->fan_div[nr])));
485}
486
487static ssize_t show_fan_div(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
490 struct lm87_data *data = lm87_update_device(dev);
491 int nr = to_sensor_dev_attr(attr)->index;
492
493 return sprintf(buf, "%d\n",
494 FAN_DIV_FROM_REG(data->fan_div[nr]));
495}
496
497static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
498 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600500 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800502 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800503 long val;
504 int err;
505
506 err = kstrtol(buf, 10, &val);
507 if (err)
508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 data->fan_min[nr] = FAN_TO_REG(val,
512 FAN_DIV_FROM_REG(data->fan_div[nr]));
513 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100514 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800515 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Guenter Roeckc6370db2012-01-14 20:58:08 -0800518/*
519 * Note: we save and restore the fan minimum here, because its value is
520 * determined in part by the fan clock divider. This follows the principle
521 * of least surprise; the user doesn't expect the fan minimum to change just
522 * because the divider changed.
523 */
Jean Delvare0e190b72012-01-26 09:38:26 -0800524static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
525 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600527 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800529 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800530 long val;
531 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unsigned long min;
533 u8 reg;
534
Guenter Roeckc6370db2012-01-14 20:58:08 -0800535 err = kstrtol(buf, 10, &val);
536 if (err)
537 return err;
538
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100539 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 min = FAN_FROM_REG(data->fan_min[nr],
541 FAN_DIV_FROM_REG(data->fan_div[nr]));
542
543 switch (val) {
Guenter Roeckc6370db2012-01-14 20:58:08 -0800544 case 1:
545 data->fan_div[nr] = 0;
546 break;
547 case 2:
548 data->fan_div[nr] = 1;
549 break;
550 case 4:
551 data->fan_div[nr] = 2;
552 break;
553 case 8:
554 data->fan_div[nr] = 3;
555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100557 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EINVAL;
559 }
560
561 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
562 switch (nr) {
563 case 0:
564 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
565 break;
566 case 1:
567 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
568 break;
569 }
570 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
571
572 data->fan_min[nr] = FAN_TO_REG(min, val);
573 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
574 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100575 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 return count;
578}
579
580#define set_fan(offset) \
Jean Delvare0e190b72012-01-26 09:38:26 -0800581static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
582 show_fan_input, NULL, offset - 1); \
583static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
584 show_fan_min, set_fan_min, offset - 1); \
585static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
586 show_fan_div, set_fan_div, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587set_fan(1);
588set_fan(2);
589
Julia Lawall07a366c2016-12-22 13:04:53 +0100590static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Guenter Roeckc6370db2012-01-14 20:58:08 -0800591 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct lm87_data *data = lm87_update_device(dev);
594 return sprintf(buf, "%d\n", data->alarms);
595}
Julia Lawall07a366c2016-12-22 13:04:53 +0100596static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Julia Lawall07a366c2016-12-22 13:04:53 +0100598static ssize_t cpu0_vid_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct lm87_data *data = lm87_update_device(dev);
602 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
603}
Julia Lawall07a366c2016-12-22 13:04:53 +0100604static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Julia Lawall07a366c2016-12-22 13:04:53 +0100606static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
Guenter Roeckc6370db2012-01-14 20:58:08 -0800607 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Jean Delvare90d66192007-10-08 18:24:35 +0200609 struct lm87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return sprintf(buf, "%d\n", data->vrm);
611}
Julia Lawall07a366c2016-12-22 13:04:53 +0100612static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
613 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100615 struct lm87_data *data = dev_get_drvdata(dev);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800616 unsigned long val;
617 int err;
618
619 err = kstrtoul(buf, 10, &val);
620 if (err)
621 return err;
Axel Linfa642d92014-08-06 08:24:00 +0800622
623 if (val > 255)
624 return -EINVAL;
625
Guenter Roeckc6370db2012-01-14 20:58:08 -0800626 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return count;
628}
Julia Lawall07a366c2016-12-22 13:04:53 +0100629static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Julia Lawall07a366c2016-12-22 13:04:53 +0100631static ssize_t aout_output_show(struct device *dev,
632 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct lm87_data *data = lm87_update_device(dev);
635 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
636}
Julia Lawall07a366c2016-12-22 13:04:53 +0100637static ssize_t aout_output_store(struct device *dev,
638 struct device_attribute *attr,
639 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600641 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800643 long val;
644 int err;
645
646 err = kstrtol(buf, 10, &val);
647 if (err)
648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100650 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 data->aout = AOUT_TO_REG(val);
652 lm87_write_value(client, LM87_REG_AOUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100653 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return count;
655}
Julia Lawall07a366c2016-12-22 13:04:53 +0100656static DEVICE_ATTR_RW(aout_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jean Delvarec2803b92007-09-26 23:39:01 +0200658static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
659 char *buf)
660{
661 struct lm87_data *data = lm87_update_device(dev);
662 int bitnr = to_sensor_dev_attr(attr)->index;
663 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
664}
665static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
666static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
667static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
668static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
669static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
670static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
671static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
672static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
673static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
674static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
675static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
676static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
677static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
678static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
679static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/*
682 * Real code
683 */
684
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200685static struct attribute *lm87_attributes[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800686 &sensor_dev_attr_in1_input.dev_attr.attr,
687 &sensor_dev_attr_in1_min.dev_attr.attr,
688 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200689 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800690 &sensor_dev_attr_in2_input.dev_attr.attr,
691 &sensor_dev_attr_in2_min.dev_attr.attr,
692 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200693 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800694 &sensor_dev_attr_in3_input.dev_attr.attr,
695 &sensor_dev_attr_in3_min.dev_attr.attr,
696 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200697 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800698 &sensor_dev_attr_in4_input.dev_attr.attr,
699 &sensor_dev_attr_in4_min.dev_attr.attr,
700 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200701 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200702
Jean Delvare0e190b72012-01-26 09:38:26 -0800703 &sensor_dev_attr_temp1_input.dev_attr.attr,
704 &sensor_dev_attr_temp1_max.dev_attr.attr,
705 &sensor_dev_attr_temp1_min.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200706 &dev_attr_temp1_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200707 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800708 &sensor_dev_attr_temp2_input.dev_attr.attr,
709 &sensor_dev_attr_temp2_max.dev_attr.attr,
710 &sensor_dev_attr_temp2_min.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200711 &dev_attr_temp2_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200712 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
713 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200714
715 &dev_attr_alarms.attr,
716 &dev_attr_aout_output.attr,
717
718 NULL
719};
720
721static const struct attribute_group lm87_group = {
722 .attrs = lm87_attributes,
723};
724
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800725static struct attribute *lm87_attributes_in6[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800726 &sensor_dev_attr_in6_input.dev_attr.attr,
727 &sensor_dev_attr_in6_min.dev_attr.attr,
728 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200729 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800730 NULL
731};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200732
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800733static const struct attribute_group lm87_group_in6 = {
734 .attrs = lm87_attributes_in6,
735};
736
737static struct attribute *lm87_attributes_fan1[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800738 &sensor_dev_attr_fan1_input.dev_attr.attr,
739 &sensor_dev_attr_fan1_min.dev_attr.attr,
740 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200741 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800742 NULL
743};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200744
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800745static const struct attribute_group lm87_group_fan1 = {
746 .attrs = lm87_attributes_fan1,
747};
748
749static struct attribute *lm87_attributes_in7[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800750 &sensor_dev_attr_in7_input.dev_attr.attr,
751 &sensor_dev_attr_in7_min.dev_attr.attr,
752 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200753 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800754 NULL
755};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200756
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800757static const struct attribute_group lm87_group_in7 = {
758 .attrs = lm87_attributes_in7,
759};
760
761static struct attribute *lm87_attributes_fan2[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800762 &sensor_dev_attr_fan2_input.dev_attr.attr,
763 &sensor_dev_attr_fan2_min.dev_attr.attr,
764 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200765 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800766 NULL
767};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200768
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800769static const struct attribute_group lm87_group_fan2 = {
770 .attrs = lm87_attributes_fan2,
771};
772
773static struct attribute *lm87_attributes_temp3[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800774 &sensor_dev_attr_temp3_input.dev_attr.attr,
775 &sensor_dev_attr_temp3_max.dev_attr.attr,
776 &sensor_dev_attr_temp3_min.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200777 &dev_attr_temp3_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200778 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
779 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800780 NULL
781};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200782
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800783static const struct attribute_group lm87_group_temp3 = {
784 .attrs = lm87_attributes_temp3,
785};
786
787static struct attribute *lm87_attributes_in0_5[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800788 &sensor_dev_attr_in0_input.dev_attr.attr,
789 &sensor_dev_attr_in0_min.dev_attr.attr,
790 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200791 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800792 &sensor_dev_attr_in5_input.dev_attr.attr,
793 &sensor_dev_attr_in5_min.dev_attr.attr,
794 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200795 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200796 NULL
797};
798
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800799static const struct attribute_group lm87_group_in0_5 = {
800 .attrs = lm87_attributes_in0_5,
801};
802
803static struct attribute *lm87_attributes_vid[] = {
804 &dev_attr_cpu0_vid.attr,
805 &dev_attr_vrm.attr,
806 NULL
807};
808
809static const struct attribute_group lm87_group_vid = {
810 .attrs = lm87_attributes_vid,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200811};
812
Jean Delvarea8884202008-07-16 19:30:14 +0200813/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare8652a262012-01-26 09:37:50 -0800814static int lm87_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Jean Delvare8652a262012-01-26 09:37:50 -0800816 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100817 const char *name;
818 u8 cid, rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvarea8884202008-07-16 19:30:14 +0200821 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Jean Delvare8652a262012-01-26 09:37:50 -0800823 if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80)
Jean Delvare52df6442009-12-09 20:35:57 +0100824 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* Now, we do the remaining detection. */
Jean Delvare8652a262012-01-26 09:37:50 -0800827 cid = lm87_read_value(client, LM87_REG_COMPANY_ID);
828 rev = lm87_read_value(client, LM87_REG_REVISION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Jean Delvare52df6442009-12-09 20:35:57 +0100830 if (cid == 0x02 /* National Semiconductor */
831 && (rev >= 0x01 && rev <= 0x08))
832 name = "lm87";
833 else if (cid == 0x41 /* Analog Devices */
834 && (rev & 0xf0) == 0x10)
835 name = "adm1024";
836 else {
837 dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
Jean Delvare8652a262012-01-26 09:37:50 -0800838 client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +0100839 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
Jean Delvare52df6442009-12-09 20:35:57 +0100842 strlcpy(info->type, name, I2C_NAME_SIZE);
Jean Delvarea8884202008-07-16 19:30:14 +0200843
844 return 0;
845}
846
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600847static void lm87_restore_config(void *arg)
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800848{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600849 struct i2c_client *client = arg;
850 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800851
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600852 lm87_write_value(client, LM87_REG_CONFIG, data->config);
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800853}
854
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600855static int lm87_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 struct lm87_data *data = i2c_get_clientdata(client);
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600858 int rc;
Mahoda Ratnayaka67043d12017-03-31 12:01:00 +1300859 struct device_node *of_node = client->dev.of_node;
860 u8 val = 0;
861 struct regulator *vcc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Mahoda Ratnayaka67043d12017-03-31 12:01:00 +1300863 if (of_node) {
864 if (of_property_read_bool(of_node, "has-temp3"))
865 val |= CHAN_TEMP3;
866 if (of_property_read_bool(of_node, "has-in6"))
867 val |= CHAN_NO_FAN(0);
868 if (of_property_read_bool(of_node, "has-in7"))
869 val |= CHAN_NO_FAN(1);
870 vcc = devm_regulator_get_optional(&client->dev, "vcc");
871 if (!IS_ERR(vcc)) {
872 if (regulator_get_voltage(vcc) == 5000000)
873 val |= CHAN_VCC_5V;
874 }
875 data->channel = val;
876 lm87_write_value(client,
877 LM87_REG_CHANNEL_MODE, data->channel);
878 } else if (dev_get_platdata(&client->dev)) {
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900879 data->channel = *(u8 *)dev_get_platdata(&client->dev);
Ben Hutchings47064d62008-10-17 17:51:12 +0200880 lm87_write_value(client,
881 LM87_REG_CHANNEL_MODE, data->channel);
882 } else {
883 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
884 }
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200885 data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600887 rc = devm_add_action(&client->dev, lm87_restore_config, client);
888 if (rc)
889 return rc;
890
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200891 if (!(data->config & 0x01)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int i;
893
894 /* Limits are left uninitialized after power-up */
895 for (i = 1; i < 6; i++) {
896 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
897 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
898 }
899 for (i = 0; i < 2; i++) {
900 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
901 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
902 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
903 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
904 }
905 if (data->channel & CHAN_TEMP3) {
906 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
907 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
908 } else {
909 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
910 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
911 }
912 }
Ben Hutchings49ae6cc82008-10-17 17:51:11 +0200913
914 /* Make sure Start is set and INT#_Clear is clear */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200915 if ((data->config & 0x09) != 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 lm87_write_value(client, LM87_REG_CONFIG,
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200917 (data->config & 0x77) | 0x01);
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Jean Delvare8652a262012-01-26 09:37:50 -0800921static int lm87_probe(struct i2c_client *client, const struct i2c_device_id *id)
922{
923 struct lm87_data *data;
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600924 struct device *hwmon_dev;
Jean Delvare8652a262012-01-26 09:37:50 -0800925 int err;
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600926 unsigned int group_tail = 0;
Jean Delvare8652a262012-01-26 09:37:50 -0800927
Guenter Roeck5ff512b2012-06-02 09:58:10 -0700928 data = devm_kzalloc(&client->dev, sizeof(struct lm87_data), GFP_KERNEL);
929 if (!data)
930 return -ENOMEM;
Jean Delvare8652a262012-01-26 09:37:50 -0800931
932 i2c_set_clientdata(client, data);
Jean Delvare8652a262012-01-26 09:37:50 -0800933 mutex_init(&data->update_lock);
934
935 /* Initialize the LM87 chip */
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600936 err = lm87_init_client(client);
937 if (err)
938 return err;
Jean Delvare8652a262012-01-26 09:37:50 -0800939
940 data->in_scale[0] = 2500;
941 data->in_scale[1] = 2700;
942 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
943 data->in_scale[3] = 5000;
944 data->in_scale[4] = 12000;
945 data->in_scale[5] = 2700;
946 data->in_scale[6] = 1875;
947 data->in_scale[7] = 1875;
948
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600949 /*
950 * Construct the list of attributes, the list depends on the
951 * configuration of the chip
952 */
953 data->attr_groups[group_tail++] = &lm87_group;
954 if (data->channel & CHAN_NO_FAN(0))
955 data->attr_groups[group_tail++] = &lm87_group_in6;
956 else
957 data->attr_groups[group_tail++] = &lm87_group_fan1;
Jean Delvare8652a262012-01-26 09:37:50 -0800958
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600959 if (data->channel & CHAN_NO_FAN(1))
960 data->attr_groups[group_tail++] = &lm87_group_in7;
961 else
962 data->attr_groups[group_tail++] = &lm87_group_fan2;
Jean Delvare8652a262012-01-26 09:37:50 -0800963
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600964 if (data->channel & CHAN_TEMP3)
965 data->attr_groups[group_tail++] = &lm87_group_temp3;
966 else
967 data->attr_groups[group_tail++] = &lm87_group_in0_5;
Jean Delvare8652a262012-01-26 09:37:50 -0800968
969 if (!(data->channel & CHAN_NO_VID)) {
970 data->vrm = vid_which_vrm();
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600971 data->attr_groups[group_tail++] = &lm87_group_vid;
Jean Delvare8652a262012-01-26 09:37:50 -0800972 }
973
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600974 hwmon_dev = devm_hwmon_device_register_with_groups(
975 &client->dev, client->name, client, data->attr_groups);
976 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Jean Delvare8652a262012-01-26 09:37:50 -0800979/*
980 * Driver data (common to all clients)
981 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jean Delvare8652a262012-01-26 09:37:50 -0800983static const struct i2c_device_id lm87_id[] = {
984 { "lm87", lm87 },
985 { "adm1024", adm1024 },
986 { }
987};
988MODULE_DEVICE_TABLE(i2c, lm87_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Jean Delvare8652a262012-01-26 09:37:50 -0800990static struct i2c_driver lm87_driver = {
991 .class = I2C_CLASS_HWMON,
992 .driver = {
993 .name = "lm87",
994 },
995 .probe = lm87_probe,
Jean Delvare8652a262012-01-26 09:37:50 -0800996 .id_table = lm87_id,
997 .detect = lm87_detect,
998 .address_list = normal_i2c,
999};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Axel Linf0967ee2012-01-20 15:38:18 +08001001module_i2c_driver(lm87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Jean Delvare7c81c60f2014-01-29 20:40:08 +01001003MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de> and others");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004MODULE_DESCRIPTION("LM87 driver");
1005MODULE_LICENSE("GPL");