blob: a996f2795db0c40892f679ee4b6938893c09a1a5 [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
63
Guenter Roeck11606312012-01-19 11:02:20 -080064/*
65 * Conversions. Rounding and limit checking is only done on the TO_REG
66 * variants. Note that you should be a bit careful with which arguments
67 * these macros are called: arguments may be evaluated more than once.
68 * Fixing this is just not worth it.
69 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010071#define IN_TO_REG(val) (SENSORS_LIMIT(((val) + 5) / 10, 0, 255))
72#define IN_FROM_REG(val) ((val) * 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
75{
76 if (rpm == 0)
77 return 255;
78 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010079 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010082#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
83 (val) == 255 ? 0 : 1350000/((div) * (val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static inline long TEMP_FROM_REG(u16 temp)
86{
87 long res;
88
89 temp >>= 4;
90 if (temp < 0x0800)
91 res = 625 * (long) temp;
92 else
93 res = ((long) temp - 0x01000) * 625;
94
95 return res / 10;
96}
97
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010098#define TEMP_LIMIT_FROM_REG(val) (((val) > 0x80 ? \
99 (val) - 0x100 : (val)) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100101#define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val) < 0 ? \
102 ((val) - 500) / 1000 : ((val) + 500) / 1000, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104#define DIV_FROM_REG(val) (1 << (val))
105
106/*
107 * Client data (each client gets its own)
108 */
109
110struct lm80_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700111 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100112 struct mutex update_lock;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100113 char error; /* !=0 if error occurred during last update */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 char valid; /* !=0 if following fields are valid */
115 unsigned long last_updated; /* In jiffies */
116
117 u8 in[7]; /* Register value */
118 u8 in_max[7]; /* Register value */
119 u8 in_min[7]; /* Register value */
120 u8 fan[2]; /* Register value */
121 u8 fan_min[2]; /* Register value */
122 u8 fan_div[2]; /* Register encoding, shifted right */
123 u16 temp; /* Register values, shifted right */
124 u8 temp_hot_max; /* Register value */
125 u8 temp_hot_hyst; /* Register value */
126 u8 temp_os_max; /* Register value */
127 u8 temp_os_hyst; /* Register value */
128 u16 alarms; /* Register encoding, combined */
129};
130
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100131/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * Functions declaration
133 */
134
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200135static int lm80_probe(struct i2c_client *client,
136 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100137static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static void lm80_init_client(struct i2c_client *client);
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200139static int lm80_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static struct lm80_data *lm80_update_device(struct device *dev);
141static int lm80_read_value(struct i2c_client *client, u8 reg);
142static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
143
144/*
145 * Driver data (common to all clients)
146 */
147
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200148static const struct i2c_device_id lm80_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100149 { "lm80", 0 },
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200150 { }
151};
152MODULE_DEVICE_TABLE(i2c, lm80_id);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static struct i2c_driver lm80_driver = {
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200155 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100156 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100157 .name = "lm80",
158 },
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200159 .probe = lm80_probe,
160 .remove = lm80_remove,
161 .id_table = lm80_id,
162 .detect = lm80_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100163 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
166/*
167 * Sysfs stuff
168 */
169
170#define show_in(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100171static ssize_t show_in_##suffix(struct device *dev, \
172 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100174 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100176 if (IS_ERR(data)) \
177 return PTR_ERR(data); \
Jean Delvaref8181762008-01-05 15:37:05 +0100178 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
Jean Delvaref8181762008-01-05 15:37:05 +0100180show_in(min, in_min)
181show_in(max, in_max)
182show_in(input, in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184#define set_in(suffix, value, reg) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100185static ssize_t set_in_##suffix(struct device *dev, \
186 struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100188 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct i2c_client *client = to_i2c_client(dev); \
190 struct lm80_data *data = i2c_get_clientdata(client); \
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100191 long val; \
192 int err = kstrtol(buf, 10, &val); \
193 if (err < 0) \
194 return err; \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100195\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100196 mutex_lock(&data->update_lock);\
Jean Delvaref8181762008-01-05 15:37:05 +0100197 data->value[nr] = IN_TO_REG(val); \
198 lm80_write_value(client, reg(nr), data->value[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100199 mutex_unlock(&data->update_lock);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return count; \
201}
Jean Delvaref8181762008-01-05 15:37:05 +0100202set_in(min, in_min, LM80_REG_IN_MIN)
203set_in(max, in_max, LM80_REG_IN_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Jean Delvaref8181762008-01-05 15:37:05 +0100205#define show_fan(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100206static ssize_t show_fan_##suffix(struct device *dev, \
207 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100209 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100211 if (IS_ERR(data)) \
212 return PTR_ERR(data); \
Jean Delvaref8181762008-01-05 15:37:05 +0100213 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
214 DIV_FROM_REG(data->fan_div[nr]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Jean Delvaref8181762008-01-05 15:37:05 +0100216show_fan(min, fan_min)
217show_fan(input, fan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Jean Delvaref8181762008-01-05 15:37:05 +0100219static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
220 char *buf)
221{
222 int nr = to_sensor_dev_attr(attr)->index;
223 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100224 if (IS_ERR(data))
225 return PTR_ERR(data);
Jean Delvaref8181762008-01-05 15:37:05 +0100226 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Jean Delvaref8181762008-01-05 15:37:05 +0100229static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
230 const char *buf, size_t count)
231{
232 int nr = to_sensor_dev_attr(attr)->index;
233 struct i2c_client *client = to_i2c_client(dev);
234 struct lm80_data *data = i2c_get_clientdata(client);
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100235 unsigned long val;
236 int err = kstrtoul(buf, 10, &val);
237 if (err < 0)
238 return err;
Jean Delvaref8181762008-01-05 15:37:05 +0100239
240 mutex_lock(&data->update_lock);
241 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
242 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
243 mutex_unlock(&data->update_lock);
244 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Guenter Roeck11606312012-01-19 11:02:20 -0800247/*
248 * Note: we save and restore the fan minimum here, because its value is
249 * determined in part by the fan divisor. This follows the principle of
250 * least surprise; the user doesn't expect the fan minimum to change just
251 * because the divisor changed.
252 */
Jean Delvaref8181762008-01-05 15:37:05 +0100253static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Jean Delvaref8181762008-01-05 15:37:05 +0100256 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct i2c_client *client = to_i2c_client(dev);
258 struct lm80_data *data = i2c_get_clientdata(client);
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100259 unsigned long min, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 u8 reg;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100261 int err = kstrtoul(buf, 10, &val);
262 if (err < 0)
263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* Save fan_min */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100266 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 min = FAN_FROM_REG(data->fan_min[nr],
268 DIV_FROM_REG(data->fan_div[nr]));
269
270 switch (val) {
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100271 case 1:
272 data->fan_div[nr] = 0;
273 break;
274 case 2:
275 data->fan_div[nr] = 1;
276 break;
277 case 4:
278 data->fan_div[nr] = 2;
279 break;
280 case 8:
281 data->fan_div[nr] = 3;
282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 default:
284 dev_err(&client->dev, "fan_div value %ld not "
285 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100286 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EINVAL;
288 }
289
290 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
291 | (data->fan_div[nr] << (2 * (nr + 1)));
292 lm80_write_value(client, LM80_REG_FANDIV, reg);
293
294 /* Restore fan_min */
295 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
296 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100297 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 return count;
300}
301
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100302static ssize_t show_temp_input1(struct device *dev,
303 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100306 if (IS_ERR(data))
307 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
309}
310
311#define show_temp(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100312static ssize_t show_temp_##suffix(struct device *dev, \
313 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{ \
315 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100316 if (IS_ERR(data)) \
317 return PTR_ERR(data); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
319}
320show_temp(hot_max, temp_hot_max);
321show_temp(hot_hyst, temp_hot_hyst);
322show_temp(os_max, temp_os_max);
323show_temp(os_hyst, temp_os_hyst);
324
325#define set_temp(suffix, value, reg) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100326static ssize_t set_temp_##suffix(struct device *dev, \
327 struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{ \
329 struct i2c_client *client = to_i2c_client(dev); \
330 struct lm80_data *data = i2c_get_clientdata(client); \
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100331 long val; \
332 int err = kstrtol(buf, 10, &val); \
333 if (err < 0) \
334 return err; \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100335\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100336 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 data->value = TEMP_LIMIT_TO_REG(val); \
338 lm80_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100339 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return count; \
341}
342set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
343set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
344set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
345set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
346
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100347static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
348 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100351 if (IS_ERR(data))
352 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return sprintf(buf, "%u\n", data->alarms);
354}
355
Jean Delvaree84542f2008-01-05 15:40:38 +0100356static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
357 char *buf)
358{
359 int bitnr = to_sensor_dev_attr(attr)->index;
360 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100361 if (IS_ERR(data))
362 return PTR_ERR(data);
Jean Delvaree84542f2008-01-05 15:40:38 +0100363 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
364}
365
Jean Delvaref8181762008-01-05 15:37:05 +0100366static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
367 show_in_min, set_in_min, 0);
368static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
369 show_in_min, set_in_min, 1);
370static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
371 show_in_min, set_in_min, 2);
372static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
373 show_in_min, set_in_min, 3);
374static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
375 show_in_min, set_in_min, 4);
376static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
377 show_in_min, set_in_min, 5);
378static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
379 show_in_min, set_in_min, 6);
380static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
381 show_in_max, set_in_max, 0);
382static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
383 show_in_max, set_in_max, 1);
384static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
385 show_in_max, set_in_max, 2);
386static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
387 show_in_max, set_in_max, 3);
388static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
389 show_in_max, set_in_max, 4);
390static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
391 show_in_max, set_in_max, 5);
392static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
393 show_in_max, set_in_max, 6);
394static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0);
395static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1);
396static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2);
397static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3);
398static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4);
399static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5);
400static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6);
401static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
402 show_fan_min, set_fan_min, 0);
403static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
404 show_fan_min, set_fan_min, 1);
405static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
406static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
407static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
408 show_fan_div, set_fan_div, 0);
409static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
410 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
412static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100413 set_temp_hot_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100415 set_temp_hot_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100417 set_temp_os_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100419 set_temp_os_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Jean Delvaree84542f2008-01-05 15:40:38 +0100421static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
422static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
423static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
424static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
425static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
426static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
427static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
428static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
429static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
430static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
431static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433/*
434 * Real code
435 */
436
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200437static struct attribute *lm80_attributes[] = {
Jean Delvaref8181762008-01-05 15:37:05 +0100438 &sensor_dev_attr_in0_min.dev_attr.attr,
439 &sensor_dev_attr_in1_min.dev_attr.attr,
440 &sensor_dev_attr_in2_min.dev_attr.attr,
441 &sensor_dev_attr_in3_min.dev_attr.attr,
442 &sensor_dev_attr_in4_min.dev_attr.attr,
443 &sensor_dev_attr_in5_min.dev_attr.attr,
444 &sensor_dev_attr_in6_min.dev_attr.attr,
445 &sensor_dev_attr_in0_max.dev_attr.attr,
446 &sensor_dev_attr_in1_max.dev_attr.attr,
447 &sensor_dev_attr_in2_max.dev_attr.attr,
448 &sensor_dev_attr_in3_max.dev_attr.attr,
449 &sensor_dev_attr_in4_max.dev_attr.attr,
450 &sensor_dev_attr_in5_max.dev_attr.attr,
451 &sensor_dev_attr_in6_max.dev_attr.attr,
452 &sensor_dev_attr_in0_input.dev_attr.attr,
453 &sensor_dev_attr_in1_input.dev_attr.attr,
454 &sensor_dev_attr_in2_input.dev_attr.attr,
455 &sensor_dev_attr_in3_input.dev_attr.attr,
456 &sensor_dev_attr_in4_input.dev_attr.attr,
457 &sensor_dev_attr_in5_input.dev_attr.attr,
458 &sensor_dev_attr_in6_input.dev_attr.attr,
459 &sensor_dev_attr_fan1_min.dev_attr.attr,
460 &sensor_dev_attr_fan2_min.dev_attr.attr,
461 &sensor_dev_attr_fan1_input.dev_attr.attr,
462 &sensor_dev_attr_fan2_input.dev_attr.attr,
463 &sensor_dev_attr_fan1_div.dev_attr.attr,
464 &sensor_dev_attr_fan2_div.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200465 &dev_attr_temp1_input.attr,
466 &dev_attr_temp1_max.attr,
467 &dev_attr_temp1_max_hyst.attr,
468 &dev_attr_temp1_crit.attr,
469 &dev_attr_temp1_crit_hyst.attr,
470 &dev_attr_alarms.attr,
Jean Delvaree84542f2008-01-05 15:40:38 +0100471 &sensor_dev_attr_in0_alarm.dev_attr.attr,
472 &sensor_dev_attr_in1_alarm.dev_attr.attr,
473 &sensor_dev_attr_in2_alarm.dev_attr.attr,
474 &sensor_dev_attr_in3_alarm.dev_attr.attr,
475 &sensor_dev_attr_in4_alarm.dev_attr.attr,
476 &sensor_dev_attr_in5_alarm.dev_attr.attr,
477 &sensor_dev_attr_in6_alarm.dev_attr.attr,
478 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
479 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
480 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
481 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200482 NULL
483};
484
485static const struct attribute_group lm80_group = {
486 .attrs = lm80_attributes,
487};
488
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200489/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100490static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200492 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int i, cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200496 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* Now, we do the remaining detection. It is lousy. */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100499 if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200500 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 for (i = 0x2a; i <= 0x3d; i++) {
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100502 cur = i2c_smbus_read_byte_data(client, i);
503 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
504 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
505 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100506 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200509 strlcpy(info->type, "lm80", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200511 return 0;
512}
513
514static int lm80_probe(struct i2c_client *client,
515 const struct i2c_device_id *id)
516{
517 struct lm80_data *data;
518 int err;
519
520 data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL);
521 if (!data) {
522 err = -ENOMEM;
523 goto exit;
524 }
525
526 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100527 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Initialize the LM80 chip */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100530 lm80_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* A few vars need to be filled upon startup */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100533 data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
534 data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* Register sysfs hooks */
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100537 err = sysfs_create_group(&client->dev.kobj, &lm80_group);
538 if (err)
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200539 goto error_free;
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200540
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100541 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700542 if (IS_ERR(data->hwmon_dev)) {
543 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200544 goto error_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0;
548
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200549error_remove:
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100550 sysfs_remove_group(&client->dev.kobj, &lm80_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551error_free:
552 kfree(data);
553exit:
554 return err;
555}
556
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200557static int lm80_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400559 struct lm80_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Tony Jones1beeffe2007-08-20 13:46:20 -0700561 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200562 sysfs_remove_group(&client->dev.kobj, &lm80_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400564 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return 0;
566}
567
568static int lm80_read_value(struct i2c_client *client, u8 reg)
569{
570 return i2c_smbus_read_byte_data(client, reg);
571}
572
573static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
574{
575 return i2c_smbus_write_byte_data(client, reg, value);
576}
577
578/* Called when we have found a new LM80. */
579static void lm80_init_client(struct i2c_client *client)
580{
Guenter Roeck11606312012-01-19 11:02:20 -0800581 /*
582 * Reset all except Watchdog values and last conversion values
583 * This sets fan-divs to 2, among others. This makes most other
584 * initializations unnecessary
585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
587 /* Set 11-bit temperature resolution */
588 lm80_write_value(client, LM80_REG_RES, 0x08);
589
590 /* Start monitoring */
591 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
592}
593
594static struct lm80_data *lm80_update_device(struct device *dev)
595{
596 struct i2c_client *client = to_i2c_client(dev);
597 struct lm80_data *data = i2c_get_clientdata(client);
598 int i;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100599 int rv;
600 int prev_rv;
601 struct lm80_data *ret = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100603 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100605 if (data->error)
606 lm80_init_client(client);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
609 dev_dbg(&client->dev, "Starting lm80 update\n");
610 for (i = 0; i <= 6; i++) {
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100611 rv = lm80_read_value(client, LM80_REG_IN(i));
612 if (rv < 0)
613 goto abort;
614 data->in[i] = rv;
615
616 rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
617 if (rv < 0)
618 goto abort;
619 data->in_min[i] = rv;
620
621 rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
622 if (rv < 0)
623 goto abort;
624 data->in_max[i] = rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100627 rv = lm80_read_value(client, LM80_REG_FAN1);
628 if (rv < 0)
629 goto abort;
630 data->fan[0] = rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100632 rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
633 if (rv < 0)
634 goto abort;
635 data->fan_min[0] = rv;
636
637 rv = lm80_read_value(client, LM80_REG_FAN2);
638 if (rv < 0)
639 goto abort;
640 data->fan[1] = rv;
641
642 rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
643 if (rv < 0)
644 goto abort;
645 data->fan_min[1] = rv;
646
647 prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
648 if (rv < 0)
649 goto abort;
650 rv = lm80_read_value(client, LM80_REG_RES);
651 if (rv < 0)
652 goto abort;
653 data->temp = (prev_rv << 8) | (rv & 0xf0);
654
655 rv = lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
656 if (rv < 0)
657 goto abort;
658 data->temp_os_max = rv;
659
660 rv = lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
661 if (rv < 0)
662 goto abort;
663 data->temp_os_hyst = rv;
664
665 rv = lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
666 if (rv < 0)
667 goto abort;
668 data->temp_hot_max = rv;
669
670 rv = lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
671 if (rv < 0)
672 goto abort;
673 data->temp_hot_hyst = rv;
674
675 rv = lm80_read_value(client, LM80_REG_FANDIV);
676 if (rv < 0)
677 goto abort;
678 data->fan_div[0] = (rv >> 2) & 0x03;
679 data->fan_div[1] = (rv >> 4) & 0x03;
680
681 prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
682 if (rv < 0)
683 goto abort;
684 rv = lm80_read_value(client, LM80_REG_ALARM2);
685 if (rv < 0)
686 goto abort;
687 data->alarms = prev_rv + (rv << 8);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 data->last_updated = jiffies;
690 data->valid = 1;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100691 data->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100693 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100695abort:
696 ret = ERR_PTR(rv);
697 data->valid = 0;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100698 data->error = 1;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100699
700done:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100701 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100703 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Axel Linf0967ee2012-01-20 15:38:18 +0800706module_i2c_driver(lm80_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
709 "Philip Edelbrock <phil@netroedge.com>");
710MODULE_DESCRIPTION("LM80 driver");
711MODULE_LICENSE("GPL");