blob: 08e3945a6fbfdebbfdb67cfa7fa5696814cca27b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm80.c - From lm_sensors, Linux kernel modules for hardware
Guenter Roeck11606312012-01-19 11:02:20 -08003 * monitoring
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Guenter Roeck11606312012-01-19 11:02:20 -08005 * and Philip Edelbrock <phil@netroedge.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/hwmon.h>
Jean Delvaref8181762008-01-05 15:37:05 +010030#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050035static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
36 0x2e, 0x2f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* Many LM80 constants specified below */
39
40/* The LM80 registers */
41#define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
42#define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
43#define LM80_REG_IN(nr) (0x20 + (nr))
44
45#define LM80_REG_FAN1 0x28
46#define LM80_REG_FAN2 0x29
47#define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
48
49#define LM80_REG_TEMP 0x27
50#define LM80_REG_TEMP_HOT_MAX 0x38
51#define LM80_REG_TEMP_HOT_HYST 0x39
52#define LM80_REG_TEMP_OS_MAX 0x3a
53#define LM80_REG_TEMP_OS_HYST 0x3b
54
55#define LM80_REG_CONFIG 0x00
56#define LM80_REG_ALARM1 0x01
57#define LM80_REG_ALARM2 0x02
58#define LM80_REG_MASK1 0x03
59#define LM80_REG_MASK2 0x04
60#define LM80_REG_FANDIV 0x05
61#define LM80_REG_RES 0x06
62
Jean Delvare9908ad42012-01-31 09:27:11 -050063#define LM96080_REG_CONV_RATE 0x07
64#define LM96080_REG_MAN_ID 0x3e
65#define LM96080_REG_DEV_ID 0x3f
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Guenter Roeck11606312012-01-19 11:02:20 -080068/*
69 * Conversions. Rounding and limit checking is only done on the TO_REG
70 * variants. Note that you should be a bit careful with which arguments
71 * these macros are called: arguments may be evaluated more than once.
72 * Fixing this is just not worth it.
73 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Guenter Roeck2a844c12013-01-09 08:09:34 -080075#define IN_TO_REG(val) (clamp_val(((val) + 5) / 10, 0, 255))
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010076#define IN_FROM_REG(val) ((val) * 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
79{
80 if (rpm == 0)
81 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -080082 rpm = clamp_val(rpm, 1, 1000000);
83 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010086#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
87 (val) == 255 ? 0 : 1350000/((div) * (val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Guenter Roeck688a3172014-04-13 09:30:09 -070089#define TEMP_FROM_REG(reg) ((reg) * 125 / 32)
90#define TEMP_TO_REG(temp) (DIV_ROUND_CLOSEST(clamp_val((temp), \
91 -128000, 127000), 1000) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#define DIV_FROM_REG(val) (1 << (val))
94
Guenter Roeck9711bcd2014-04-13 09:45:15 -070095enum temp_index {
96 t_input = 0,
97 t_hot_max,
98 t_hot_hyst,
99 t_os_max,
100 t_os_hyst,
101 t_num_temp
102};
103
104static const u8 temp_regs[t_num_temp] = {
105 [t_input] = LM80_REG_TEMP,
106 [t_hot_max] = LM80_REG_TEMP_HOT_MAX,
107 [t_hot_hyst] = LM80_REG_TEMP_HOT_HYST,
108 [t_os_max] = LM80_REG_TEMP_OS_MAX,
109 [t_os_hyst] = LM80_REG_TEMP_OS_HYST,
110};
111
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700112enum in_index {
113 i_input = 0,
114 i_max,
115 i_min,
116 i_num_in
117};
118
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700119enum fan_index {
120 f_input,
121 f_min,
122 f_num_fan
123};
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * Client data (each client gets its own)
127 */
128
129struct lm80_data {
Guenter Roeck118c9a62014-04-04 18:01:34 +0200130 struct i2c_client *client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100131 struct mutex update_lock;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100132 char error; /* !=0 if error occurred during last update */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 char valid; /* !=0 if following fields are valid */
134 unsigned long last_updated; /* In jiffies */
135
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700136 u8 in[i_num_in][7]; /* Register value, 1st index is enum in_index */
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700137 u8 fan[f_num_fan][2]; /* Register value, 1st index enum fan_index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 u8 fan_div[2]; /* Register encoding, shifted right */
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700139 s16 temp[t_num_temp]; /* Register values, normalized to 16 bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 u16 alarms; /* Register encoding, combined */
141};
142
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700143static int lm80_read_value(struct i2c_client *client, u8 reg)
144{
145 return i2c_smbus_read_byte_data(client, reg);
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700148static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
149{
150 return i2c_smbus_write_byte_data(client, reg, value);
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700153/* Called when we have found a new LM80 and after read errors */
154static void lm80_init_client(struct i2c_client *client)
155{
156 /*
157 * Reset all except Watchdog values and last conversion values
158 * This sets fan-divs to 2, among others. This makes most other
159 * initializations unnecessary
160 */
161 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
162 /* Set 11-bit temperature resolution */
163 lm80_write_value(client, LM80_REG_RES, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700165 /* Start monitoring */
166 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
167}
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200168
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700169static struct lm80_data *lm80_update_device(struct device *dev)
170{
171 struct lm80_data *data = dev_get_drvdata(dev);
172 struct i2c_client *client = data->client;
173 int i;
174 int rv;
175 int prev_rv;
176 struct lm80_data *ret = data;
177
178 mutex_lock(&data->update_lock);
179
180 if (data->error)
181 lm80_init_client(client);
182
183 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
184 dev_dbg(dev, "Starting lm80 update\n");
185 for (i = 0; i <= 6; i++) {
186 rv = lm80_read_value(client, LM80_REG_IN(i));
187 if (rv < 0)
188 goto abort;
189 data->in[i_input][i] = rv;
190
191 rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
192 if (rv < 0)
193 goto abort;
194 data->in[i_min][i] = rv;
195
196 rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
197 if (rv < 0)
198 goto abort;
199 data->in[i_max][i] = rv;
200 }
201
202 rv = lm80_read_value(client, LM80_REG_FAN1);
203 if (rv < 0)
204 goto abort;
205 data->fan[f_input][0] = rv;
206
207 rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
208 if (rv < 0)
209 goto abort;
210 data->fan[f_min][0] = rv;
211
212 rv = lm80_read_value(client, LM80_REG_FAN2);
213 if (rv < 0)
214 goto abort;
215 data->fan[f_input][1] = rv;
216
217 rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
218 if (rv < 0)
219 goto abort;
220 data->fan[f_min][1] = rv;
221
222 prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
223 if (rv < 0)
224 goto abort;
225 rv = lm80_read_value(client, LM80_REG_RES);
226 if (rv < 0)
227 goto abort;
228 data->temp[t_input] = (prev_rv << 8) | (rv & 0xf0);
229
230 for (i = t_input + 1; i < t_num_temp; i++) {
231 rv = lm80_read_value(client, temp_regs[i]);
232 if (rv < 0)
233 goto abort;
234 data->temp[i] = rv << 8;
235 }
236
237 rv = lm80_read_value(client, LM80_REG_FANDIV);
238 if (rv < 0)
239 goto abort;
240 data->fan_div[0] = (rv >> 2) & 0x03;
241 data->fan_div[1] = (rv >> 4) & 0x03;
242
243 prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
244 if (rv < 0)
245 goto abort;
246 rv = lm80_read_value(client, LM80_REG_ALARM2);
247 if (rv < 0)
248 goto abort;
249 data->alarms = prev_rv + (rv << 8);
250
251 data->last_updated = jiffies;
252 data->valid = 1;
253 data->error = 0;
254 }
255 goto done;
256
257abort:
258 ret = ERR_PTR(rv);
259 data->valid = 0;
260 data->error = 1;
261
262done:
263 mutex_unlock(&data->update_lock);
264
265 return ret;
266}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/*
269 * Sysfs stuff
270 */
271
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700272static ssize_t show_in(struct device *dev, struct device_attribute *attr,
273 char *buf)
274{
275 struct lm80_data *data = lm80_update_device(dev);
276 int index = to_sensor_dev_attr_2(attr)->index;
277 int nr = to_sensor_dev_attr_2(attr)->nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700279 if (IS_ERR(data))
280 return PTR_ERR(data);
281 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr][index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700283
284static ssize_t set_in(struct device *dev, struct device_attribute *attr,
285 const char *buf, size_t count)
286{
287 struct lm80_data *data = dev_get_drvdata(dev);
288 struct i2c_client *client = data->client;
289 int index = to_sensor_dev_attr_2(attr)->index;
290 int nr = to_sensor_dev_attr_2(attr)->nr;
291 long val;
292 u8 reg;
293 int err = kstrtol(buf, 10, &val);
294 if (err < 0)
295 return err;
296
297 reg = nr == i_min ? LM80_REG_IN_MIN(index) : LM80_REG_IN_MAX(index);
298
299 mutex_lock(&data->update_lock);
300 data->in[nr][index] = IN_TO_REG(val);
301 lm80_write_value(client, reg, data->in[nr][index]);
302 mutex_unlock(&data->update_lock);
303 return count;
304}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700306static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
307 char *buf)
308{
309 int index = to_sensor_dev_attr_2(attr)->index;
310 int nr = to_sensor_dev_attr_2(attr)->nr;
311 struct lm80_data *data = lm80_update_device(dev);
312 if (IS_ERR(data))
313 return PTR_ERR(data);
314 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr][index],
315 DIV_FROM_REG(data->fan_div[index])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jean Delvaref8181762008-01-05 15:37:05 +0100318static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
319 char *buf)
320{
321 int nr = to_sensor_dev_attr(attr)->index;
322 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100323 if (IS_ERR(data))
324 return PTR_ERR(data);
Jean Delvaref8181762008-01-05 15:37:05 +0100325 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Jean Delvaref8181762008-01-05 15:37:05 +0100328static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
329 const char *buf, size_t count)
330{
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700331 int index = to_sensor_dev_attr_2(attr)->index;
332 int nr = to_sensor_dev_attr_2(attr)->nr;
Guenter Roeck118c9a62014-04-04 18:01:34 +0200333 struct lm80_data *data = dev_get_drvdata(dev);
334 struct i2c_client *client = data->client;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100335 unsigned long val;
336 int err = kstrtoul(buf, 10, &val);
337 if (err < 0)
338 return err;
Jean Delvaref8181762008-01-05 15:37:05 +0100339
340 mutex_lock(&data->update_lock);
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700341 data->fan[nr][index] = FAN_TO_REG(val,
342 DIV_FROM_REG(data->fan_div[index]));
343 lm80_write_value(client, LM80_REG_FAN_MIN(index + 1),
344 data->fan[nr][index]);
Jean Delvaref8181762008-01-05 15:37:05 +0100345 mutex_unlock(&data->update_lock);
346 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Guenter Roeck11606312012-01-19 11:02:20 -0800349/*
350 * Note: we save and restore the fan minimum here, because its value is
351 * determined in part by the fan divisor. This follows the principle of
352 * least surprise; the user doesn't expect the fan minimum to change just
353 * because the divisor changed.
354 */
Jean Delvaref8181762008-01-05 15:37:05 +0100355static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Jean Delvaref8181762008-01-05 15:37:05 +0100358 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck118c9a62014-04-04 18:01:34 +0200359 struct lm80_data *data = dev_get_drvdata(dev);
360 struct i2c_client *client = data->client;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100361 unsigned long min, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 u8 reg;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100363 int err = kstrtoul(buf, 10, &val);
364 if (err < 0)
365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* Save fan_min */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_lock(&data->update_lock);
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700369 min = FAN_FROM_REG(data->fan[f_min][nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 DIV_FROM_REG(data->fan_div[nr]));
371
372 switch (val) {
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100373 case 1:
374 data->fan_div[nr] = 0;
375 break;
376 case 2:
377 data->fan_div[nr] = 1;
378 break;
379 case 4:
380 data->fan_div[nr] = 2;
381 break;
382 case 8:
383 data->fan_div[nr] = 3;
384 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 default:
Guenter Roeck118c9a62014-04-04 18:01:34 +0200386 dev_err(dev,
Guenter Roeckb55f3752013-01-10 10:01:24 -0800387 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
388 val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100389 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -EINVAL;
391 }
392
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700393 reg = (lm80_read_value(client, LM80_REG_FANDIV) &
394 ~(3 << (2 * (nr + 1)))) | (data->fan_div[nr] << (2 * (nr + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 lm80_write_value(client, LM80_REG_FANDIV, reg);
396
397 /* Restore fan_min */
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700398 data->fan[f_min][nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
399 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1),
400 data->fan[f_min][nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100401 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 return count;
404}
405
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700406static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
407 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700409 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100411 if (IS_ERR(data))
412 return PTR_ERR(data);
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700413 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700416static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
417 const char *buf, size_t count)
418{
419 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
420 struct lm80_data *data = dev_get_drvdata(dev);
421 struct i2c_client *client = data->client;
422 int nr = attr->index;
423 long val;
424 int err = kstrtol(buf, 10, &val);
425 if (err < 0)
426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700428 mutex_lock(&data->update_lock);
429 data->temp[nr] = TEMP_TO_REG(val);
430 lm80_write_value(client, temp_regs[nr], data->temp[nr] >> 8);
431 mutex_unlock(&data->update_lock);
432 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Julia Lawall15476902016-12-22 13:04:51 +0100435static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100436 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100439 if (IS_ERR(data))
440 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return sprintf(buf, "%u\n", data->alarms);
442}
443
Jean Delvaree84542f2008-01-05 15:40:38 +0100444static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
445 char *buf)
446{
447 int bitnr = to_sensor_dev_attr(attr)->index;
448 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100449 if (IS_ERR(data))
450 return PTR_ERR(data);
Jean Delvaree84542f2008-01-05 15:40:38 +0100451 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
452}
453
Guenter Roeck1adf3a32014-04-13 10:53:50 -0700454static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO,
455 show_in, set_in, i_min, 0);
456static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO,
457 show_in, set_in, i_min, 1);
458static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO,
459 show_in, set_in, i_min, 2);
460static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO,
461 show_in, set_in, i_min, 3);
462static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO,
463 show_in, set_in, i_min, 4);
464static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO,
465 show_in, set_in, i_min, 5);
466static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO,
467 show_in, set_in, i_min, 6);
468static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO,
469 show_in, set_in, i_max, 0);
470static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO,
471 show_in, set_in, i_max, 1);
472static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO,
473 show_in, set_in, i_max, 2);
474static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO,
475 show_in, set_in, i_max, 3);
476static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO,
477 show_in, set_in, i_max, 4);
478static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO,
479 show_in, set_in, i_max, 5);
480static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO,
481 show_in, set_in, i_max, 6);
482static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, i_input, 0);
483static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, i_input, 1);
484static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, i_input, 2);
485static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, i_input, 3);
486static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, i_input, 4);
487static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, i_input, 5);
488static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, i_input, 6);
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700489static SENSOR_DEVICE_ATTR_2(fan1_min, S_IWUSR | S_IRUGO,
490 show_fan, set_fan_min, f_min, 0);
491static SENSOR_DEVICE_ATTR_2(fan2_min, S_IWUSR | S_IRUGO,
492 show_fan, set_fan_min, f_min, 1);
493static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, f_input, 0);
494static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, f_input, 1);
Jean Delvaref8181762008-01-05 15:37:05 +0100495static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
496 show_fan_div, set_fan_div, 0);
497static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
498 show_fan_div, set_fan_div, 1);
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700499static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input);
500static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
501 set_temp, t_hot_max);
502static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp,
503 set_temp, t_hot_hyst);
504static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp,
505 set_temp, t_os_max);
506static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp,
507 set_temp, t_os_hyst);
Julia Lawall15476902016-12-22 13:04:51 +0100508static DEVICE_ATTR_RO(alarms);
Jean Delvaree84542f2008-01-05 15:40:38 +0100509static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
510static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
511static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
512static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
513static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
514static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
515static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
516static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
517static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
518static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
519static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521/*
522 * Real code
523 */
524
Guenter Roeck118c9a62014-04-04 18:01:34 +0200525static struct attribute *lm80_attrs[] = {
Jean Delvaref8181762008-01-05 15:37:05 +0100526 &sensor_dev_attr_in0_min.dev_attr.attr,
527 &sensor_dev_attr_in1_min.dev_attr.attr,
528 &sensor_dev_attr_in2_min.dev_attr.attr,
529 &sensor_dev_attr_in3_min.dev_attr.attr,
530 &sensor_dev_attr_in4_min.dev_attr.attr,
531 &sensor_dev_attr_in5_min.dev_attr.attr,
532 &sensor_dev_attr_in6_min.dev_attr.attr,
533 &sensor_dev_attr_in0_max.dev_attr.attr,
534 &sensor_dev_attr_in1_max.dev_attr.attr,
535 &sensor_dev_attr_in2_max.dev_attr.attr,
536 &sensor_dev_attr_in3_max.dev_attr.attr,
537 &sensor_dev_attr_in4_max.dev_attr.attr,
538 &sensor_dev_attr_in5_max.dev_attr.attr,
539 &sensor_dev_attr_in6_max.dev_attr.attr,
540 &sensor_dev_attr_in0_input.dev_attr.attr,
541 &sensor_dev_attr_in1_input.dev_attr.attr,
542 &sensor_dev_attr_in2_input.dev_attr.attr,
543 &sensor_dev_attr_in3_input.dev_attr.attr,
544 &sensor_dev_attr_in4_input.dev_attr.attr,
545 &sensor_dev_attr_in5_input.dev_attr.attr,
546 &sensor_dev_attr_in6_input.dev_attr.attr,
547 &sensor_dev_attr_fan1_min.dev_attr.attr,
548 &sensor_dev_attr_fan2_min.dev_attr.attr,
549 &sensor_dev_attr_fan1_input.dev_attr.attr,
550 &sensor_dev_attr_fan2_input.dev_attr.attr,
551 &sensor_dev_attr_fan1_div.dev_attr.attr,
552 &sensor_dev_attr_fan2_div.dev_attr.attr,
Guenter Roeck9711bcd2014-04-13 09:45:15 -0700553 &sensor_dev_attr_temp1_input.dev_attr.attr,
554 &sensor_dev_attr_temp1_max.dev_attr.attr,
555 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
556 &sensor_dev_attr_temp1_crit.dev_attr.attr,
557 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200558 &dev_attr_alarms.attr,
Jean Delvaree84542f2008-01-05 15:40:38 +0100559 &sensor_dev_attr_in0_alarm.dev_attr.attr,
560 &sensor_dev_attr_in1_alarm.dev_attr.attr,
561 &sensor_dev_attr_in2_alarm.dev_attr.attr,
562 &sensor_dev_attr_in3_alarm.dev_attr.attr,
563 &sensor_dev_attr_in4_alarm.dev_attr.attr,
564 &sensor_dev_attr_in5_alarm.dev_attr.attr,
565 &sensor_dev_attr_in6_alarm.dev_attr.attr,
566 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
567 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
568 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
569 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200570 NULL
571};
Guenter Roeck118c9a62014-04-04 18:01:34 +0200572ATTRIBUTE_GROUPS(lm80);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200573
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200574/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100575static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200577 struct i2c_adapter *adapter = client->adapter;
Jean Delvare9908ad42012-01-31 09:27:11 -0500578 int i, cur, man_id, dev_id;
579 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200582 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Jean Delvare9908ad42012-01-31 09:27:11 -0500584 /* First check for unused bits, common to both chip types */
585 if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
586 || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200587 return -ENODEV;
Jean Delvare9908ad42012-01-31 09:27:11 -0500588
589 /*
590 * The LM96080 has manufacturer and stepping/die rev registers so we
591 * can just check that. The LM80 does not have such registers so we
592 * have to use a more expensive trick.
593 */
594 man_id = lm80_read_value(client, LM96080_REG_MAN_ID);
595 dev_id = lm80_read_value(client, LM96080_REG_DEV_ID);
596 if (man_id == 0x01 && dev_id == 0x08) {
597 /* Check more unused bits for confirmation */
598 if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe)
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100599 return -ENODEV;
Jean Delvare9908ad42012-01-31 09:27:11 -0500600
601 name = "lm96080";
602 } else {
603 /* Check 6-bit addressing */
604 for (i = 0x2a; i <= 0x3d; i++) {
605 cur = i2c_smbus_read_byte_data(client, i);
606 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
607 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
608 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
609 return -ENODEV;
610 }
611
612 name = "lm80";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Jean Delvare9908ad42012-01-31 09:27:11 -0500615 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200617 return 0;
618}
619
620static int lm80_probe(struct i2c_client *client,
621 const struct i2c_device_id *id)
622{
Guenter Roeck118c9a62014-04-04 18:01:34 +0200623 struct device *dev = &client->dev;
624 struct device *hwmon_dev;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200625 struct lm80_data *data;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200626
Guenter Roeck118c9a62014-04-04 18:01:34 +0200627 data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL);
Guenter Roeckc38b2822012-06-02 09:58:09 -0700628 if (!data)
629 return -ENOMEM;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200630
Guenter Roeck118c9a62014-04-04 18:01:34 +0200631 data->client = client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100632 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Initialize the LM80 chip */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100635 lm80_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* A few vars need to be filled upon startup */
Guenter Roeck85b3ee02014-04-13 11:28:29 -0700638 data->fan[f_min][0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
639 data->fan[f_min][1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Guenter Roeck118c9a62014-04-04 18:01:34 +0200641 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
642 data, lm80_groups);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200643
Guenter Roeck118c9a62014-04-04 18:01:34 +0200644 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700647/*
648 * Driver data (common to all clients)
649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700651static const struct i2c_device_id lm80_id[] = {
652 { "lm80", 0 },
653 { "lm96080", 1 },
654 { }
655};
656MODULE_DEVICE_TABLE(i2c, lm80_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Guenter Roeckc254ffd2014-04-13 11:56:15 -0700658static struct i2c_driver lm80_driver = {
659 .class = I2C_CLASS_HWMON,
660 .driver = {
661 .name = "lm80",
662 },
663 .probe = lm80_probe,
664 .id_table = lm80_id,
665 .detect = lm80_detect,
666 .address_list = normal_i2c,
667};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Axel Linf0967ee2012-01-20 15:38:18 +0800669module_i2c_driver(lm80_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
672 "Philip Edelbrock <phil@netroedge.com>");
673MODULE_DESCRIPTION("LM80 driver");
674MODULE_LICENSE("GPL");