blob: fe980d64dc05f4f6b8eb5ead699f9cdd2defab1c [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
95/*
96 * Client data (each client gets its own)
97 */
98
99struct lm80_data {
Guenter Roeck118c9a62014-04-04 18:01:34 +0200100 struct i2c_client *client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100101 struct mutex update_lock;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100102 char error; /* !=0 if error occurred during last update */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 char valid; /* !=0 if following fields are valid */
104 unsigned long last_updated; /* In jiffies */
105
106 u8 in[7]; /* Register value */
107 u8 in_max[7]; /* Register value */
108 u8 in_min[7]; /* Register value */
109 u8 fan[2]; /* Register value */
110 u8 fan_min[2]; /* Register value */
111 u8 fan_div[2]; /* Register encoding, shifted right */
Guenter Roeck9028ff82014-04-13 09:15:09 -0700112 s16 temp; /* Register values */
Guenter Roeck688a3172014-04-13 09:30:09 -0700113 s16 temp_hot_max; /* Register value, left shifted */
114 s16 temp_hot_hyst; /* Register value, left shifted */
115 s16 temp_os_max; /* Register value, left shifted */
116 s16 temp_os_hyst; /* Register value, left shifted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 u16 alarms; /* Register encoding, combined */
118};
119
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * Functions declaration
122 */
123
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200124static int lm80_probe(struct i2c_client *client,
125 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100126static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static void lm80_init_client(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static struct lm80_data *lm80_update_device(struct device *dev);
129static int lm80_read_value(struct i2c_client *client, u8 reg);
130static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
131
132/*
133 * Driver data (common to all clients)
134 */
135
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200136static const struct i2c_device_id lm80_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100137 { "lm80", 0 },
Jean Delvare9908ad42012-01-31 09:27:11 -0500138 { "lm96080", 1 },
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200139 { }
140};
141MODULE_DEVICE_TABLE(i2c, lm80_id);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static struct i2c_driver lm80_driver = {
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200144 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100145 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100146 .name = "lm80",
147 },
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200148 .probe = lm80_probe,
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200149 .id_table = lm80_id,
150 .detect = lm80_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100151 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154/*
155 * Sysfs stuff
156 */
157
158#define show_in(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100159static ssize_t show_in_##suffix(struct device *dev, \
160 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100162 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100164 if (IS_ERR(data)) \
165 return PTR_ERR(data); \
Jean Delvaref8181762008-01-05 15:37:05 +0100166 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Jean Delvaref8181762008-01-05 15:37:05 +0100168show_in(min, in_min)
169show_in(max, in_max)
170show_in(input, in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172#define set_in(suffix, value, reg) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100173static ssize_t set_in_##suffix(struct device *dev, \
174 struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100176 int nr = to_sensor_dev_attr(attr)->index; \
Guenter Roeck118c9a62014-04-04 18:01:34 +0200177 struct lm80_data *data = dev_get_drvdata(dev); \
178 struct i2c_client *client = data->client; \
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100179 long val; \
180 int err = kstrtol(buf, 10, &val); \
181 if (err < 0) \
182 return err; \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100183\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100184 mutex_lock(&data->update_lock);\
Jean Delvaref8181762008-01-05 15:37:05 +0100185 data->value[nr] = IN_TO_REG(val); \
186 lm80_write_value(client, reg(nr), data->value[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100187 mutex_unlock(&data->update_lock);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return count; \
189}
Jean Delvaref8181762008-01-05 15:37:05 +0100190set_in(min, in_min, LM80_REG_IN_MIN)
191set_in(max, in_max, LM80_REG_IN_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Jean Delvaref8181762008-01-05 15:37:05 +0100193#define show_fan(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100194static ssize_t show_fan_##suffix(struct device *dev, \
195 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100197 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100199 if (IS_ERR(data)) \
200 return PTR_ERR(data); \
Jean Delvaref8181762008-01-05 15:37:05 +0100201 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
202 DIV_FROM_REG(data->fan_div[nr]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
Jean Delvaref8181762008-01-05 15:37:05 +0100204show_fan(min, fan_min)
205show_fan(input, fan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Jean Delvaref8181762008-01-05 15:37:05 +0100207static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
208 char *buf)
209{
210 int nr = to_sensor_dev_attr(attr)->index;
211 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100212 if (IS_ERR(data))
213 return PTR_ERR(data);
Jean Delvaref8181762008-01-05 15:37:05 +0100214 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jean Delvaref8181762008-01-05 15:37:05 +0100217static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
218 const char *buf, size_t count)
219{
220 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck118c9a62014-04-04 18:01:34 +0200221 struct lm80_data *data = dev_get_drvdata(dev);
222 struct i2c_client *client = data->client;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100223 unsigned long val;
224 int err = kstrtoul(buf, 10, &val);
225 if (err < 0)
226 return err;
Jean Delvaref8181762008-01-05 15:37:05 +0100227
228 mutex_lock(&data->update_lock);
229 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
230 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
231 mutex_unlock(&data->update_lock);
232 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Guenter Roeck11606312012-01-19 11:02:20 -0800235/*
236 * Note: we save and restore the fan minimum here, because its value is
237 * determined in part by the fan divisor. This follows the principle of
238 * least surprise; the user doesn't expect the fan minimum to change just
239 * because the divisor changed.
240 */
Jean Delvaref8181762008-01-05 15:37:05 +0100241static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Jean Delvaref8181762008-01-05 15:37:05 +0100244 int nr = to_sensor_dev_attr(attr)->index;
Guenter Roeck118c9a62014-04-04 18:01:34 +0200245 struct lm80_data *data = dev_get_drvdata(dev);
246 struct i2c_client *client = data->client;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100247 unsigned long min, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 u8 reg;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100249 int err = kstrtoul(buf, 10, &val);
250 if (err < 0)
251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* Save fan_min */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100254 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 min = FAN_FROM_REG(data->fan_min[nr],
256 DIV_FROM_REG(data->fan_div[nr]));
257
258 switch (val) {
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100259 case 1:
260 data->fan_div[nr] = 0;
261 break;
262 case 2:
263 data->fan_div[nr] = 1;
264 break;
265 case 4:
266 data->fan_div[nr] = 2;
267 break;
268 case 8:
269 data->fan_div[nr] = 3;
270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 default:
Guenter Roeck118c9a62014-04-04 18:01:34 +0200272 dev_err(dev,
Guenter Roeckb55f3752013-01-10 10:01:24 -0800273 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
274 val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100275 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINVAL;
277 }
278
279 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
280 | (data->fan_div[nr] << (2 * (nr + 1)));
281 lm80_write_value(client, LM80_REG_FANDIV, reg);
282
283 /* Restore fan_min */
284 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
285 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100286 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return count;
289}
290
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100291static ssize_t show_temp_input1(struct device *dev,
292 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100295 if (IS_ERR(data))
296 return PTR_ERR(data);
Guenter Roeck9028ff82014-04-13 09:15:09 -0700297 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300#define show_temp(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100301static ssize_t show_temp_##suffix(struct device *dev, \
302 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{ \
304 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100305 if (IS_ERR(data)) \
306 return PTR_ERR(data); \
Guenter Roeck688a3172014-04-13 09:30:09 -0700307 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309show_temp(hot_max, temp_hot_max);
310show_temp(hot_hyst, temp_hot_hyst);
311show_temp(os_max, temp_os_max);
312show_temp(os_hyst, temp_os_hyst);
313
314#define set_temp(suffix, value, reg) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100315static ssize_t set_temp_##suffix(struct device *dev, \
316 struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{ \
Guenter Roeck118c9a62014-04-04 18:01:34 +0200318 struct lm80_data *data = dev_get_drvdata(dev); \
319 struct i2c_client *client = data->client; \
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100320 long val; \
321 int err = kstrtol(buf, 10, &val); \
322 if (err < 0) \
323 return err; \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100324\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100325 mutex_lock(&data->update_lock); \
Guenter Roeck688a3172014-04-13 09:30:09 -0700326 data->value = TEMP_TO_REG(val); \
327 lm80_write_value(client, reg, data->value >> 8); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100328 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return count; \
330}
331set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
332set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
333set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
334set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
335
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100336static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
337 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100340 if (IS_ERR(data))
341 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return sprintf(buf, "%u\n", data->alarms);
343}
344
Jean Delvaree84542f2008-01-05 15:40:38 +0100345static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
346 char *buf)
347{
348 int bitnr = to_sensor_dev_attr(attr)->index;
349 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100350 if (IS_ERR(data))
351 return PTR_ERR(data);
Jean Delvaree84542f2008-01-05 15:40:38 +0100352 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
353}
354
Jean Delvaref8181762008-01-05 15:37:05 +0100355static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
356 show_in_min, set_in_min, 0);
357static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
358 show_in_min, set_in_min, 1);
359static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
360 show_in_min, set_in_min, 2);
361static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
362 show_in_min, set_in_min, 3);
363static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
364 show_in_min, set_in_min, 4);
365static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
366 show_in_min, set_in_min, 5);
367static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
368 show_in_min, set_in_min, 6);
369static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
370 show_in_max, set_in_max, 0);
371static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
372 show_in_max, set_in_max, 1);
373static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
374 show_in_max, set_in_max, 2);
375static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
376 show_in_max, set_in_max, 3);
377static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
378 show_in_max, set_in_max, 4);
379static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
380 show_in_max, set_in_max, 5);
381static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
382 show_in_max, set_in_max, 6);
383static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0);
384static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1);
385static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2);
386static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3);
387static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4);
388static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5);
389static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6);
390static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
391 show_fan_min, set_fan_min, 0);
392static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
393 show_fan_min, set_fan_min, 1);
394static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
395static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
396static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
397 show_fan_div, set_fan_div, 0);
398static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
399 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
401static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100402 set_temp_hot_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100404 set_temp_hot_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100406 set_temp_os_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100408 set_temp_os_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Jean Delvaree84542f2008-01-05 15:40:38 +0100410static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
411static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
412static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
413static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
414static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
415static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
416static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
417static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
418static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
419static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
420static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422/*
423 * Real code
424 */
425
Guenter Roeck118c9a62014-04-04 18:01:34 +0200426static struct attribute *lm80_attrs[] = {
Jean Delvaref8181762008-01-05 15:37:05 +0100427 &sensor_dev_attr_in0_min.dev_attr.attr,
428 &sensor_dev_attr_in1_min.dev_attr.attr,
429 &sensor_dev_attr_in2_min.dev_attr.attr,
430 &sensor_dev_attr_in3_min.dev_attr.attr,
431 &sensor_dev_attr_in4_min.dev_attr.attr,
432 &sensor_dev_attr_in5_min.dev_attr.attr,
433 &sensor_dev_attr_in6_min.dev_attr.attr,
434 &sensor_dev_attr_in0_max.dev_attr.attr,
435 &sensor_dev_attr_in1_max.dev_attr.attr,
436 &sensor_dev_attr_in2_max.dev_attr.attr,
437 &sensor_dev_attr_in3_max.dev_attr.attr,
438 &sensor_dev_attr_in4_max.dev_attr.attr,
439 &sensor_dev_attr_in5_max.dev_attr.attr,
440 &sensor_dev_attr_in6_max.dev_attr.attr,
441 &sensor_dev_attr_in0_input.dev_attr.attr,
442 &sensor_dev_attr_in1_input.dev_attr.attr,
443 &sensor_dev_attr_in2_input.dev_attr.attr,
444 &sensor_dev_attr_in3_input.dev_attr.attr,
445 &sensor_dev_attr_in4_input.dev_attr.attr,
446 &sensor_dev_attr_in5_input.dev_attr.attr,
447 &sensor_dev_attr_in6_input.dev_attr.attr,
448 &sensor_dev_attr_fan1_min.dev_attr.attr,
449 &sensor_dev_attr_fan2_min.dev_attr.attr,
450 &sensor_dev_attr_fan1_input.dev_attr.attr,
451 &sensor_dev_attr_fan2_input.dev_attr.attr,
452 &sensor_dev_attr_fan1_div.dev_attr.attr,
453 &sensor_dev_attr_fan2_div.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200454 &dev_attr_temp1_input.attr,
455 &dev_attr_temp1_max.attr,
456 &dev_attr_temp1_max_hyst.attr,
457 &dev_attr_temp1_crit.attr,
458 &dev_attr_temp1_crit_hyst.attr,
459 &dev_attr_alarms.attr,
Jean Delvaree84542f2008-01-05 15:40:38 +0100460 &sensor_dev_attr_in0_alarm.dev_attr.attr,
461 &sensor_dev_attr_in1_alarm.dev_attr.attr,
462 &sensor_dev_attr_in2_alarm.dev_attr.attr,
463 &sensor_dev_attr_in3_alarm.dev_attr.attr,
464 &sensor_dev_attr_in4_alarm.dev_attr.attr,
465 &sensor_dev_attr_in5_alarm.dev_attr.attr,
466 &sensor_dev_attr_in6_alarm.dev_attr.attr,
467 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
468 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
469 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
470 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200471 NULL
472};
Guenter Roeck118c9a62014-04-04 18:01:34 +0200473ATTRIBUTE_GROUPS(lm80);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200474
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200475/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100476static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200478 struct i2c_adapter *adapter = client->adapter;
Jean Delvare9908ad42012-01-31 09:27:11 -0500479 int i, cur, man_id, dev_id;
480 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200483 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jean Delvare9908ad42012-01-31 09:27:11 -0500485 /* First check for unused bits, common to both chip types */
486 if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
487 || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200488 return -ENODEV;
Jean Delvare9908ad42012-01-31 09:27:11 -0500489
490 /*
491 * The LM96080 has manufacturer and stepping/die rev registers so we
492 * can just check that. The LM80 does not have such registers so we
493 * have to use a more expensive trick.
494 */
495 man_id = lm80_read_value(client, LM96080_REG_MAN_ID);
496 dev_id = lm80_read_value(client, LM96080_REG_DEV_ID);
497 if (man_id == 0x01 && dev_id == 0x08) {
498 /* Check more unused bits for confirmation */
499 if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe)
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100500 return -ENODEV;
Jean Delvare9908ad42012-01-31 09:27:11 -0500501
502 name = "lm96080";
503 } else {
504 /* Check 6-bit addressing */
505 for (i = 0x2a; i <= 0x3d; i++) {
506 cur = i2c_smbus_read_byte_data(client, i);
507 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
508 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
509 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
510 return -ENODEV;
511 }
512
513 name = "lm80";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
Jean Delvare9908ad42012-01-31 09:27:11 -0500516 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200518 return 0;
519}
520
521static int lm80_probe(struct i2c_client *client,
522 const struct i2c_device_id *id)
523{
Guenter Roeck118c9a62014-04-04 18:01:34 +0200524 struct device *dev = &client->dev;
525 struct device *hwmon_dev;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200526 struct lm80_data *data;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200527
Guenter Roeck118c9a62014-04-04 18:01:34 +0200528 data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL);
Guenter Roeckc38b2822012-06-02 09:58:09 -0700529 if (!data)
530 return -ENOMEM;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200531
Guenter Roeck118c9a62014-04-04 18:01:34 +0200532 data->client = client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100533 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* Initialize the LM80 chip */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100536 lm80_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* A few vars need to be filled upon startup */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100539 data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
540 data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Guenter Roeck118c9a62014-04-04 18:01:34 +0200542 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
543 data, lm80_groups);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200544
Guenter Roeck118c9a62014-04-04 18:01:34 +0200545 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548static int lm80_read_value(struct i2c_client *client, u8 reg)
549{
550 return i2c_smbus_read_byte_data(client, reg);
551}
552
553static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
554{
555 return i2c_smbus_write_byte_data(client, reg, value);
556}
557
558/* Called when we have found a new LM80. */
559static void lm80_init_client(struct i2c_client *client)
560{
Guenter Roeck11606312012-01-19 11:02:20 -0800561 /*
562 * Reset all except Watchdog values and last conversion values
563 * This sets fan-divs to 2, among others. This makes most other
564 * initializations unnecessary
565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
567 /* Set 11-bit temperature resolution */
568 lm80_write_value(client, LM80_REG_RES, 0x08);
569
570 /* Start monitoring */
571 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
572}
573
574static struct lm80_data *lm80_update_device(struct device *dev)
575{
Guenter Roeck118c9a62014-04-04 18:01:34 +0200576 struct lm80_data *data = dev_get_drvdata(dev);
577 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int i;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100579 int rv;
580 int prev_rv;
581 struct lm80_data *ret = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100583 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100585 if (data->error)
586 lm80_init_client(client);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
Guenter Roeck118c9a62014-04-04 18:01:34 +0200589 dev_dbg(dev, "Starting lm80 update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 for (i = 0; i <= 6; i++) {
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100591 rv = lm80_read_value(client, LM80_REG_IN(i));
592 if (rv < 0)
593 goto abort;
594 data->in[i] = rv;
595
596 rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
597 if (rv < 0)
598 goto abort;
599 data->in_min[i] = rv;
600
601 rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
602 if (rv < 0)
603 goto abort;
604 data->in_max[i] = rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100607 rv = lm80_read_value(client, LM80_REG_FAN1);
608 if (rv < 0)
609 goto abort;
610 data->fan[0] = rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100612 rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
613 if (rv < 0)
614 goto abort;
615 data->fan_min[0] = rv;
616
617 rv = lm80_read_value(client, LM80_REG_FAN2);
618 if (rv < 0)
619 goto abort;
620 data->fan[1] = rv;
621
622 rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
623 if (rv < 0)
624 goto abort;
625 data->fan_min[1] = rv;
626
627 prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
628 if (rv < 0)
629 goto abort;
630 rv = lm80_read_value(client, LM80_REG_RES);
631 if (rv < 0)
632 goto abort;
633 data->temp = (prev_rv << 8) | (rv & 0xf0);
634
635 rv = lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
636 if (rv < 0)
637 goto abort;
Guenter Roeck688a3172014-04-13 09:30:09 -0700638 data->temp_os_max = rv << 8;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100639
640 rv = lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
641 if (rv < 0)
642 goto abort;
Guenter Roeck688a3172014-04-13 09:30:09 -0700643 data->temp_os_hyst = rv << 8;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100644
645 rv = lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
646 if (rv < 0)
647 goto abort;
Guenter Roeck688a3172014-04-13 09:30:09 -0700648 data->temp_hot_max = rv << 8;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100649
650 rv = lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
651 if (rv < 0)
652 goto abort;
Guenter Roeck688a3172014-04-13 09:30:09 -0700653 data->temp_hot_hyst = rv << 8;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100654
655 rv = lm80_read_value(client, LM80_REG_FANDIV);
656 if (rv < 0)
657 goto abort;
658 data->fan_div[0] = (rv >> 2) & 0x03;
659 data->fan_div[1] = (rv >> 4) & 0x03;
660
661 prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
662 if (rv < 0)
663 goto abort;
664 rv = lm80_read_value(client, LM80_REG_ALARM2);
665 if (rv < 0)
666 goto abort;
667 data->alarms = prev_rv + (rv << 8);
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 data->last_updated = jiffies;
670 data->valid = 1;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100671 data->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100673 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100675abort:
676 ret = ERR_PTR(rv);
677 data->valid = 0;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100678 data->error = 1;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100679
680done:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100681 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100683 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Axel Linf0967ee2012-01-20 15:38:18 +0800686module_i2c_driver(lm80_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
689 "Philip Edelbrock <phil@netroedge.com>");
690MODULE_DESCRIPTION("LM80 driver");
691MODULE_LICENSE("GPL");