blob: 644e61eae5740511bcb73b9b496a15ffd7cecbca [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
79 * The LM87 registers
80 */
81
82/* nr in 0..5 */
83#define LM87_REG_IN(nr) (0x20 + (nr))
84#define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
85#define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
86/* nr in 0..1 */
87#define LM87_REG_AIN(nr) (0x28 + (nr))
88#define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
89#define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
90
91static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
92static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
93static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
94
95#define LM87_REG_TEMP_HW_INT_LOCK 0x13
96#define LM87_REG_TEMP_HW_EXT_LOCK 0x14
97#define LM87_REG_TEMP_HW_INT 0x17
98#define LM87_REG_TEMP_HW_EXT 0x18
99
100/* nr in 0..1 */
101#define LM87_REG_FAN(nr) (0x28 + (nr))
102#define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
103#define LM87_REG_AOUT 0x19
104
105#define LM87_REG_CONFIG 0x40
106#define LM87_REG_CHANNEL_MODE 0x16
107#define LM87_REG_VID_FAN_DIV 0x47
108#define LM87_REG_VID4 0x49
109
110#define LM87_REG_ALARMS1 0x41
111#define LM87_REG_ALARMS2 0x42
112
113#define LM87_REG_COMPANY_ID 0x3E
114#define LM87_REG_REVISION 0x3F
115
116/*
117 * Conversions and various macros
118 * The LM87 uses signed 8-bit values for temperatures.
119 */
120
Guenter Roeckc6370db2012-01-14 20:58:08 -0800121#define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192)
122#define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \
Guenter Roeck12fa55c2016-12-04 18:16:02 -0800123 (val) >= (scale) * 255 / 192 ? 255 : \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800124 ((val) * 192 + (scale) / 2) / (scale))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126#define TEMP_FROM_REG(reg) ((reg) * 1000)
127#define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
128 (val) >= 126500 ? 127 : \
Guenter Roeckc6370db2012-01-14 20:58:08 -0800129 (((val) < 0 ? (val) - 500 : \
130 (val) + 500) / 1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Guenter Roeckc6370db2012-01-14 20:58:08 -0800132#define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \
133 (1350000 + (reg)*(div) / 2) / ((reg) * (div)))
134#define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \
135 (1350000 + (val)*(div) / 2) / ((val) * (div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#define FAN_DIV_FROM_REG(reg) (1 << (reg))
138
139/* analog out is 9.80mV/LSB */
140#define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
141#define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
142 (val) >= 2500 ? 255 : \
143 ((val) * 10 + 49) / 98)
144
145/* nr in 0..1 */
146#define CHAN_NO_FAN(nr) (1 << (nr))
147#define CHAN_TEMP3 (1 << 2)
148#define CHAN_VCC_5V (1 << 3)
Jean Delvare889af3d2007-10-08 22:48:10 +0200149#define CHAN_NO_VID (1 << 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Client data (each client gets its own)
153 */
154
155struct lm87_data {
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100156 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 char valid; /* zero until following fields are valid */
158 unsigned long last_updated; /* In jiffies */
159
160 u8 channel; /* register value */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200161 u8 config; /* original register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 u8 in[8]; /* register value */
164 u8 in_max[8]; /* register value */
165 u8 in_min[8]; /* register value */
166 u16 in_scale[8];
167
168 s8 temp[3]; /* register value */
169 s8 temp_high[3]; /* register value */
170 s8 temp_low[3]; /* register value */
171 s8 temp_crit_int; /* min of two register values */
172 s8 temp_crit_ext; /* min of two register values */
173
174 u8 fan[2]; /* register value */
175 u8 fan_min[2]; /* register value */
176 u8 fan_div[2]; /* register value, shifted right */
177 u8 aout; /* register value */
178
179 u16 alarms; /* register values, combined */
180 u8 vid; /* register values, combined */
181 u8 vrm;
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600182
183 const struct attribute_group *attr_groups[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
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{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600198 struct i2c_client *client = dev_get_drvdata(dev);
Jean Delvare8652a262012-01-26 09:37:50 -0800199 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
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800279static ssize_t in_input_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
Jean Delvare0e190b72012-01-26 09:38:26 -0800281{
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
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800289static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
290 char *buf)
Jean Delvare0e190b72012-01-26 09:38:26 -0800291{
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
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800299static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
300 char *buf)
Jean Delvare0e190b72012-01-26 09:38:26 -0800301{
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
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800309static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
310 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600312 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 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
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800330static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
331 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600333 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 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
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800351static SENSOR_DEVICE_ATTR_RO(in0_input, in_input, 0);
352static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
353static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
354static SENSOR_DEVICE_ATTR_RO(in1_input, in_input, 1);
355static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
356static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
357static SENSOR_DEVICE_ATTR_RO(in2_input, in_input, 2);
358static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
359static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
360static SENSOR_DEVICE_ATTR_RO(in3_input, in_input, 3);
361static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
362static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
363static SENSOR_DEVICE_ATTR_RO(in4_input, in_input, 4);
364static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
365static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
366static SENSOR_DEVICE_ATTR_RO(in5_input, in_input, 5);
367static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
368static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
369static SENSOR_DEVICE_ATTR_RO(in6_input, in_input, 6);
370static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
371static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
372static SENSOR_DEVICE_ATTR_RO(in7_input, in_input, 7);
373static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
374static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800376static ssize_t temp_input_show(struct device *dev,
Jean Delvare0e190b72012-01-26 09:38:26 -0800377 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Jean Delvare0e190b72012-01-26 09:38:26 -0800382 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
383}
384
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800385static ssize_t temp_low_show(struct device *dev,
Jean Delvare0e190b72012-01-26 09:38:26 -0800386 struct device_attribute *attr, char *buf)
387{
388 struct lm87_data *data = lm87_update_device(dev);
389 int nr = to_sensor_dev_attr(attr)->index;
390
391 return sprintf(buf, "%d\n",
392 TEMP_FROM_REG(data->temp_low[nr]));
393}
394
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800395static ssize_t temp_high_show(struct device *dev,
Jean Delvare0e190b72012-01-26 09:38:26 -0800396 struct device_attribute *attr, char *buf)
397{
398 struct lm87_data *data = lm87_update_device(dev);
399 int nr = to_sensor_dev_attr(attr)->index;
400
401 return sprintf(buf, "%d\n",
402 TEMP_FROM_REG(data->temp_high[nr]));
403}
404
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800405static ssize_t temp_low_store(struct device *dev,
406 struct device_attribute *attr, const char *buf,
407 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600409 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800411 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800412 long val;
413 int err;
414
415 err = kstrtol(buf, 10, &val);
416 if (err)
417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100419 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 data->temp_low[nr] = TEMP_TO_REG(val);
421 lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100422 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800423 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800426static ssize_t temp_high_store(struct device *dev,
427 struct device_attribute *attr, const char *buf,
428 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600430 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800432 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800433 long val;
434 int err;
435
436 err = kstrtol(buf, 10, &val);
437 if (err)
438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100440 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 data->temp_high[nr] = TEMP_TO_REG(val);
442 lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100443 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800444 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800447static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
448static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_low, 0);
449static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_high, 0);
450static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
451static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_low, 1);
452static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_high, 1);
453static SENSOR_DEVICE_ATTR_RO(temp3_input, temp_input, 2);
454static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_low, 2);
455static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_high, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Julia Lawall07a366c2016-12-22 13:04:53 +0100457static ssize_t temp1_crit_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct lm87_data *data = lm87_update_device(dev);
461 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
462}
463
Julia Lawall07a366c2016-12-22 13:04:53 +0100464static ssize_t temp2_crit_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct lm87_data *data = lm87_update_device(dev);
468 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
469}
470
Julia Lawall07a366c2016-12-22 13:04:53 +0100471static DEVICE_ATTR_RO(temp1_crit);
472static DEVICE_ATTR_RO(temp2_crit);
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800473static DEVICE_ATTR(temp3_crit, 0444, temp2_crit_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800475static ssize_t fan_input_show(struct device *dev,
Jean Delvare0e190b72012-01-26 09:38:26 -0800476 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jean Delvare0e190b72012-01-26 09:38:26 -0800481 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
482 FAN_DIV_FROM_REG(data->fan_div[nr])));
483}
484
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800485static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
486 char *buf)
Jean Delvare0e190b72012-01-26 09:38:26 -0800487{
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", FAN_FROM_REG(data->fan_min[nr],
492 FAN_DIV_FROM_REG(data->fan_div[nr])));
493}
494
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800495static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
496 char *buf)
Jean Delvare0e190b72012-01-26 09:38:26 -0800497{
498 struct lm87_data *data = lm87_update_device(dev);
499 int nr = to_sensor_dev_attr(attr)->index;
500
501 return sprintf(buf, "%d\n",
502 FAN_DIV_FROM_REG(data->fan_div[nr]));
503}
504
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800505static ssize_t fan_min_store(struct device *dev,
506 struct device_attribute *attr, const char *buf,
507 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600509 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800511 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800512 long val;
513 int err;
514
515 err = kstrtol(buf, 10, &val);
516 if (err)
517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100519 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 data->fan_min[nr] = FAN_TO_REG(val,
521 FAN_DIV_FROM_REG(data->fan_div[nr]));
522 lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100523 mutex_unlock(&data->update_lock);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800524 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Guenter Roeckc6370db2012-01-14 20:58:08 -0800527/*
528 * Note: we save and restore the fan minimum here, because its value is
529 * determined in part by the fan clock divider. This follows the principle
530 * of least surprise; the user doesn't expect the fan minimum to change just
531 * because the divider changed.
532 */
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800533static ssize_t fan_div_store(struct device *dev,
534 struct device_attribute *attr, const char *buf,
535 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600537 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct lm87_data *data = i2c_get_clientdata(client);
Jean Delvare0e190b72012-01-26 09:38:26 -0800539 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeckc6370db2012-01-14 20:58:08 -0800540 long val;
541 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 unsigned long min;
543 u8 reg;
544
Guenter Roeckc6370db2012-01-14 20:58:08 -0800545 err = kstrtol(buf, 10, &val);
546 if (err)
547 return err;
548
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100549 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 min = FAN_FROM_REG(data->fan_min[nr],
551 FAN_DIV_FROM_REG(data->fan_div[nr]));
552
553 switch (val) {
Guenter Roeckc6370db2012-01-14 20:58:08 -0800554 case 1:
555 data->fan_div[nr] = 0;
556 break;
557 case 2:
558 data->fan_div[nr] = 1;
559 break;
560 case 4:
561 data->fan_div[nr] = 2;
562 break;
563 case 8:
564 data->fan_div[nr] = 3;
565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100567 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -EINVAL;
569 }
570
571 reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
572 switch (nr) {
573 case 0:
574 reg = (reg & 0xCF) | (data->fan_div[0] << 4);
575 break;
576 case 1:
577 reg = (reg & 0x3F) | (data->fan_div[1] << 6);
578 break;
579 }
580 lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
581
582 data->fan_min[nr] = FAN_TO_REG(min, val);
583 lm87_write_value(client, LM87_REG_FAN_MIN(nr),
584 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100585 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 return count;
588}
589
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800590static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
591static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
592static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
593static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
594static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
595static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Julia Lawall07a366c2016-12-22 13:04:53 +0100597static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Guenter Roeckc6370db2012-01-14 20:58:08 -0800598 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct lm87_data *data = lm87_update_device(dev);
601 return sprintf(buf, "%d\n", data->alarms);
602}
Julia Lawall07a366c2016-12-22 13:04:53 +0100603static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Julia Lawall07a366c2016-12-22 13:04:53 +0100605static ssize_t cpu0_vid_show(struct device *dev,
606 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct lm87_data *data = lm87_update_device(dev);
609 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
610}
Julia Lawall07a366c2016-12-22 13:04:53 +0100611static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Julia Lawall07a366c2016-12-22 13:04:53 +0100613static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
Guenter Roeckc6370db2012-01-14 20:58:08 -0800614 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Jean Delvare90d66192007-10-08 18:24:35 +0200616 struct lm87_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return sprintf(buf, "%d\n", data->vrm);
618}
Julia Lawall07a366c2016-12-22 13:04:53 +0100619static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
620 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100622 struct lm87_data *data = dev_get_drvdata(dev);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800623 unsigned long val;
624 int err;
625
626 err = kstrtoul(buf, 10, &val);
627 if (err)
628 return err;
Axel Linfa642d92014-08-06 08:24:00 +0800629
630 if (val > 255)
631 return -EINVAL;
632
Guenter Roeckc6370db2012-01-14 20:58:08 -0800633 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return count;
635}
Julia Lawall07a366c2016-12-22 13:04:53 +0100636static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Julia Lawall07a366c2016-12-22 13:04:53 +0100638static ssize_t aout_output_show(struct device *dev,
639 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct lm87_data *data = lm87_update_device(dev);
642 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
643}
Julia Lawall07a366c2016-12-22 13:04:53 +0100644static ssize_t aout_output_store(struct device *dev,
645 struct device_attribute *attr,
646 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600648 struct i2c_client *client = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeckc6370db2012-01-14 20:58:08 -0800650 long val;
651 int err;
652
653 err = kstrtol(buf, 10, &val);
654 if (err)
655 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100657 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 data->aout = AOUT_TO_REG(val);
659 lm87_write_value(client, LM87_REG_AOUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100660 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return count;
662}
Julia Lawall07a366c2016-12-22 13:04:53 +0100663static DEVICE_ATTR_RW(aout_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800665static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200666 char *buf)
667{
668 struct lm87_data *data = lm87_update_device(dev);
669 int bitnr = to_sensor_dev_attr(attr)->index;
670 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
671}
Guenter Roeckf6c93ae2019-01-21 20:47:57 -0800672static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
673static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
674static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
675static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
676static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
677static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
678static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 6);
679static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 7);
680static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
681static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
682static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 5);
683static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
684static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
685static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 14);
686static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 15);
Jean Delvarec2803b92007-09-26 23:39:01 +0200687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*
689 * Real code
690 */
691
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200692static struct attribute *lm87_attributes[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800693 &sensor_dev_attr_in1_input.dev_attr.attr,
694 &sensor_dev_attr_in1_min.dev_attr.attr,
695 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200696 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800697 &sensor_dev_attr_in2_input.dev_attr.attr,
698 &sensor_dev_attr_in2_min.dev_attr.attr,
699 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200700 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800701 &sensor_dev_attr_in3_input.dev_attr.attr,
702 &sensor_dev_attr_in3_min.dev_attr.attr,
703 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200704 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800705 &sensor_dev_attr_in4_input.dev_attr.attr,
706 &sensor_dev_attr_in4_min.dev_attr.attr,
707 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200708 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200709
Jean Delvare0e190b72012-01-26 09:38:26 -0800710 &sensor_dev_attr_temp1_input.dev_attr.attr,
711 &sensor_dev_attr_temp1_max.dev_attr.attr,
712 &sensor_dev_attr_temp1_min.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200713 &dev_attr_temp1_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200714 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800715 &sensor_dev_attr_temp2_input.dev_attr.attr,
716 &sensor_dev_attr_temp2_max.dev_attr.attr,
717 &sensor_dev_attr_temp2_min.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200718 &dev_attr_temp2_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200719 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
720 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200721
722 &dev_attr_alarms.attr,
723 &dev_attr_aout_output.attr,
724
725 NULL
726};
727
728static const struct attribute_group lm87_group = {
729 .attrs = lm87_attributes,
730};
731
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800732static struct attribute *lm87_attributes_in6[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800733 &sensor_dev_attr_in6_input.dev_attr.attr,
734 &sensor_dev_attr_in6_min.dev_attr.attr,
735 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200736 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800737 NULL
738};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200739
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800740static const struct attribute_group lm87_group_in6 = {
741 .attrs = lm87_attributes_in6,
742};
743
744static struct attribute *lm87_attributes_fan1[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800745 &sensor_dev_attr_fan1_input.dev_attr.attr,
746 &sensor_dev_attr_fan1_min.dev_attr.attr,
747 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200748 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800749 NULL
750};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200751
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800752static const struct attribute_group lm87_group_fan1 = {
753 .attrs = lm87_attributes_fan1,
754};
755
756static struct attribute *lm87_attributes_in7[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800757 &sensor_dev_attr_in7_input.dev_attr.attr,
758 &sensor_dev_attr_in7_min.dev_attr.attr,
759 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200760 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800761 NULL
762};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200763
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800764static const struct attribute_group lm87_group_in7 = {
765 .attrs = lm87_attributes_in7,
766};
767
768static struct attribute *lm87_attributes_fan2[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800769 &sensor_dev_attr_fan2_input.dev_attr.attr,
770 &sensor_dev_attr_fan2_min.dev_attr.attr,
771 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200772 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800773 NULL
774};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200775
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800776static const struct attribute_group lm87_group_fan2 = {
777 .attrs = lm87_attributes_fan2,
778};
779
780static struct attribute *lm87_attributes_temp3[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800781 &sensor_dev_attr_temp3_input.dev_attr.attr,
782 &sensor_dev_attr_temp3_max.dev_attr.attr,
783 &sensor_dev_attr_temp3_min.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200784 &dev_attr_temp3_crit.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200785 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
786 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800787 NULL
788};
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200789
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800790static const struct attribute_group lm87_group_temp3 = {
791 .attrs = lm87_attributes_temp3,
792};
793
794static struct attribute *lm87_attributes_in0_5[] = {
Jean Delvare0e190b72012-01-26 09:38:26 -0800795 &sensor_dev_attr_in0_input.dev_attr.attr,
796 &sensor_dev_attr_in0_min.dev_attr.attr,
797 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200798 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare0e190b72012-01-26 09:38:26 -0800799 &sensor_dev_attr_in5_input.dev_attr.attr,
800 &sensor_dev_attr_in5_min.dev_attr.attr,
801 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvarec2803b92007-09-26 23:39:01 +0200802 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200803 NULL
804};
805
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800806static const struct attribute_group lm87_group_in0_5 = {
807 .attrs = lm87_attributes_in0_5,
808};
809
810static struct attribute *lm87_attributes_vid[] = {
811 &dev_attr_cpu0_vid.attr,
812 &dev_attr_vrm.attr,
813 NULL
814};
815
816static const struct attribute_group lm87_group_vid = {
817 .attrs = lm87_attributes_vid,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200818};
819
Jean Delvarea8884202008-07-16 19:30:14 +0200820/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare8652a262012-01-26 09:37:50 -0800821static int lm87_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Jean Delvare8652a262012-01-26 09:37:50 -0800823 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +0100824 const char *name;
825 u8 cid, rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvarea8884202008-07-16 19:30:14 +0200828 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Jean Delvare8652a262012-01-26 09:37:50 -0800830 if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80)
Jean Delvare52df6442009-12-09 20:35:57 +0100831 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Now, we do the remaining detection. */
Jean Delvare8652a262012-01-26 09:37:50 -0800834 cid = lm87_read_value(client, LM87_REG_COMPANY_ID);
835 rev = lm87_read_value(client, LM87_REG_REVISION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Jean Delvare52df6442009-12-09 20:35:57 +0100837 if (cid == 0x02 /* National Semiconductor */
838 && (rev >= 0x01 && rev <= 0x08))
839 name = "lm87";
840 else if (cid == 0x41 /* Analog Devices */
841 && (rev & 0xf0) == 0x10)
842 name = "adm1024";
843 else {
844 dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n",
Jean Delvare8652a262012-01-26 09:37:50 -0800845 client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +0100846 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
Jean Delvare52df6442009-12-09 20:35:57 +0100849 strlcpy(info->type, name, I2C_NAME_SIZE);
Jean Delvarea8884202008-07-16 19:30:14 +0200850
851 return 0;
852}
853
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600854static void lm87_restore_config(void *arg)
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800855{
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600856 struct i2c_client *client = arg;
857 struct lm87_data *data = i2c_get_clientdata(client);
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800858
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600859 lm87_write_value(client, LM87_REG_CONFIG, data->config);
Guenter Roeck073f1e6c2012-01-16 15:11:57 -0800860}
861
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600862static int lm87_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct lm87_data *data = i2c_get_clientdata(client);
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600865 int rc;
Mahoda Ratnayaka67043d12017-03-31 12:01:00 +1300866 struct device_node *of_node = client->dev.of_node;
867 u8 val = 0;
868 struct regulator *vcc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Mahoda Ratnayaka67043d12017-03-31 12:01:00 +1300870 if (of_node) {
871 if (of_property_read_bool(of_node, "has-temp3"))
872 val |= CHAN_TEMP3;
873 if (of_property_read_bool(of_node, "has-in6"))
874 val |= CHAN_NO_FAN(0);
875 if (of_property_read_bool(of_node, "has-in7"))
876 val |= CHAN_NO_FAN(1);
877 vcc = devm_regulator_get_optional(&client->dev, "vcc");
878 if (!IS_ERR(vcc)) {
879 if (regulator_get_voltage(vcc) == 5000000)
880 val |= CHAN_VCC_5V;
881 }
882 data->channel = val;
883 lm87_write_value(client,
884 LM87_REG_CHANNEL_MODE, data->channel);
885 } else if (dev_get_platdata(&client->dev)) {
Jingoo Hana8b3a3a2013-07-30 17:13:06 +0900886 data->channel = *(u8 *)dev_get_platdata(&client->dev);
Ben Hutchings47064d62008-10-17 17:51:12 +0200887 lm87_write_value(client,
888 LM87_REG_CHANNEL_MODE, data->channel);
889 } else {
890 data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
891 }
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200892 data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600894 rc = devm_add_action(&client->dev, lm87_restore_config, client);
895 if (rc)
896 return rc;
897
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200898 if (!(data->config & 0x01)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 int i;
900
901 /* Limits are left uninitialized after power-up */
902 for (i = 1; i < 6; i++) {
903 lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
904 lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
905 }
906 for (i = 0; i < 2; i++) {
907 lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
908 lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
909 lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
910 lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
911 }
912 if (data->channel & CHAN_TEMP3) {
913 lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
914 lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
915 } else {
916 lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
917 lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
918 }
919 }
Ben Hutchings49ae6cc82008-10-17 17:51:11 +0200920
921 /* Make sure Start is set and INT#_Clear is clear */
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200922 if ((data->config & 0x09) != 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 lm87_write_value(client, LM87_REG_CONFIG,
Ben Hutchingsd2cac802008-10-17 17:51:11 +0200924 (data->config & 0x77) | 0x01);
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Jean Delvare8652a262012-01-26 09:37:50 -0800928static int lm87_probe(struct i2c_client *client, const struct i2c_device_id *id)
929{
930 struct lm87_data *data;
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600931 struct device *hwmon_dev;
Jean Delvare8652a262012-01-26 09:37:50 -0800932 int err;
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600933 unsigned int group_tail = 0;
Jean Delvare8652a262012-01-26 09:37:50 -0800934
Guenter Roeck5ff512b2012-06-02 09:58:10 -0700935 data = devm_kzalloc(&client->dev, sizeof(struct lm87_data), GFP_KERNEL);
936 if (!data)
937 return -ENOMEM;
Jean Delvare8652a262012-01-26 09:37:50 -0800938
939 i2c_set_clientdata(client, data);
Jean Delvare8652a262012-01-26 09:37:50 -0800940 mutex_init(&data->update_lock);
941
942 /* Initialize the LM87 chip */
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600943 err = lm87_init_client(client);
944 if (err)
945 return err;
Jean Delvare8652a262012-01-26 09:37:50 -0800946
947 data->in_scale[0] = 2500;
948 data->in_scale[1] = 2700;
949 data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
950 data->in_scale[3] = 5000;
951 data->in_scale[4] = 12000;
952 data->in_scale[5] = 2700;
953 data->in_scale[6] = 1875;
954 data->in_scale[7] = 1875;
955
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600956 /*
957 * Construct the list of attributes, the list depends on the
958 * configuration of the chip
959 */
960 data->attr_groups[group_tail++] = &lm87_group;
961 if (data->channel & CHAN_NO_FAN(0))
962 data->attr_groups[group_tail++] = &lm87_group_in6;
963 else
964 data->attr_groups[group_tail++] = &lm87_group_fan1;
Jean Delvare8652a262012-01-26 09:37:50 -0800965
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600966 if (data->channel & CHAN_NO_FAN(1))
967 data->attr_groups[group_tail++] = &lm87_group_in7;
968 else
969 data->attr_groups[group_tail++] = &lm87_group_fan2;
Jean Delvare8652a262012-01-26 09:37:50 -0800970
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600971 if (data->channel & CHAN_TEMP3)
972 data->attr_groups[group_tail++] = &lm87_group_temp3;
973 else
974 data->attr_groups[group_tail++] = &lm87_group_in0_5;
Jean Delvare8652a262012-01-26 09:37:50 -0800975
976 if (!(data->channel & CHAN_NO_VID)) {
977 data->vrm = vid_which_vrm();
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600978 data->attr_groups[group_tail++] = &lm87_group_vid;
Jean Delvare8652a262012-01-26 09:37:50 -0800979 }
980
Jason Gunthorpe00a0c902016-10-26 10:56:59 -0600981 hwmon_dev = devm_hwmon_device_register_with_groups(
982 &client->dev, client->name, client, data->attr_groups);
983 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Jean Delvare8652a262012-01-26 09:37:50 -0800986/*
987 * Driver data (common to all clients)
988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Jean Delvare8652a262012-01-26 09:37:50 -0800990static const struct i2c_device_id lm87_id[] = {
Javier Martinez Canillas0439bf712017-04-12 21:21:38 -0300991 { "lm87", 0 },
992 { "adm1024", 0 },
Jean Delvare8652a262012-01-26 09:37:50 -0800993 { }
994};
995MODULE_DEVICE_TABLE(i2c, lm87_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Javier Martinez Canillasa02c24a32017-04-12 21:21:39 -0300997static const struct of_device_id lm87_of_match[] = {
998 { .compatible = "ti,lm87" },
999 { .compatible = "adi,adm1024" },
1000 { },
1001};
1002MODULE_DEVICE_TABLE(of, lm87_of_match);
1003
Jean Delvare8652a262012-01-26 09:37:50 -08001004static struct i2c_driver lm87_driver = {
1005 .class = I2C_CLASS_HWMON,
1006 .driver = {
1007 .name = "lm87",
Javier Martinez Canillasa02c24a32017-04-12 21:21:39 -03001008 .of_match_table = lm87_of_match,
Jean Delvare8652a262012-01-26 09:37:50 -08001009 },
1010 .probe = lm87_probe,
Jean Delvare8652a262012-01-26 09:37:50 -08001011 .id_table = lm87_id,
1012 .detect = lm87_detect,
1013 .address_list = normal_i2c,
1014};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Axel Linf0967ee2012-01-20 15:38:18 +08001016module_i2c_driver(lm87_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jean Delvare7c81c60f2014-01-29 20:40:08 +01001018MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de> and others");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019MODULE_DESCRIPTION("LM87 driver");
1020MODULE_LICENSE("GPL");