blob: bcffc189940317186dadbf68798139723fba3fd0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lm80.c - From lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 * and Philip Edelbrock <phil@netroedge.com>
6 *
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
38/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020039I2C_CLIENT_INSMOD_1(lm80);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Many LM80 constants specified below */
42
43/* The LM80 registers */
44#define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
45#define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
46#define LM80_REG_IN(nr) (0x20 + (nr))
47
48#define LM80_REG_FAN1 0x28
49#define LM80_REG_FAN2 0x29
50#define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
51
52#define LM80_REG_TEMP 0x27
53#define LM80_REG_TEMP_HOT_MAX 0x38
54#define LM80_REG_TEMP_HOT_HYST 0x39
55#define LM80_REG_TEMP_OS_MAX 0x3a
56#define LM80_REG_TEMP_OS_HYST 0x3b
57
58#define LM80_REG_CONFIG 0x00
59#define LM80_REG_ALARM1 0x01
60#define LM80_REG_ALARM2 0x02
61#define LM80_REG_MASK1 0x03
62#define LM80_REG_MASK2 0x04
63#define LM80_REG_FANDIV 0x05
64#define LM80_REG_RES 0x06
65
66
67/* Conversions. Rounding and limit checking is only done on the TO_REG
68 variants. Note that you should be a bit careful with which arguments
69 these macros are called: arguments may be evaluated more than once.
70 Fixing this is just not worth it. */
71
72#define IN_TO_REG(val) (SENSORS_LIMIT(((val)+5)/10,0,255))
73#define IN_FROM_REG(val) ((val)*10)
74
75static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
76{
77 if (rpm == 0)
78 return 255;
79 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
80 return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254);
81}
82
83#define FAN_FROM_REG(val,div) ((val)==0?-1:\
84 (val)==255?0:1350000/((div)*(val)))
85
86static inline long TEMP_FROM_REG(u16 temp)
87{
88 long res;
89
90 temp >>= 4;
91 if (temp < 0x0800)
92 res = 625 * (long) temp;
93 else
94 res = ((long) temp - 0x01000) * 625;
95
96 return res / 10;
97}
98
99#define TEMP_LIMIT_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
100
101#define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val)<0?\
102 ((val)-500)/1000:((val)+500)/1000,0,255)
103
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 char valid; /* !=0 if following fields are valid */
114 unsigned long last_updated; /* In jiffies */
115
116 u8 in[7]; /* Register value */
117 u8 in_max[7]; /* Register value */
118 u8 in_min[7]; /* Register value */
119 u8 fan[2]; /* Register value */
120 u8 fan_min[2]; /* Register value */
121 u8 fan_div[2]; /* Register encoding, shifted right */
122 u16 temp; /* Register values, shifted right */
123 u8 temp_hot_max; /* Register value */
124 u8 temp_hot_hyst; /* Register value */
125 u8 temp_os_max; /* Register value */
126 u8 temp_os_hyst; /* Register value */
127 u16 alarms; /* Register encoding, combined */
128};
129
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100130/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * Functions declaration
132 */
133
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200134static int lm80_probe(struct i2c_client *client,
135 const struct i2c_device_id *id);
136static int lm80_detect(struct i2c_client *client, int kind,
137 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[] = {
149 { "lm80", lm80 },
150 { }
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,
163 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
166/*
167 * Sysfs stuff
168 */
169
170#define show_in(suffix, value) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400171static ssize_t show_in_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100173 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct lm80_data *data = lm80_update_device(dev); \
Jean Delvaref8181762008-01-05 15:37:05 +0100175 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Jean Delvaref8181762008-01-05 15:37:05 +0100177show_in(min, in_min)
178show_in(max, in_max)
179show_in(input, in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181#define set_in(suffix, value, reg) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400182static ssize_t set_in_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 size_t count) \
184{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100185 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct i2c_client *client = to_i2c_client(dev); \
187 struct lm80_data *data = i2c_get_clientdata(client); \
188 long val = simple_strtol(buf, NULL, 10); \
189 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100190 mutex_lock(&data->update_lock);\
Jean Delvaref8181762008-01-05 15:37:05 +0100191 data->value[nr] = IN_TO_REG(val); \
192 lm80_write_value(client, reg(nr), data->value[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100193 mutex_unlock(&data->update_lock);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return count; \
195}
Jean Delvaref8181762008-01-05 15:37:05 +0100196set_in(min, in_min, LM80_REG_IN_MIN)
197set_in(max, in_max, LM80_REG_IN_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jean Delvaref8181762008-01-05 15:37:05 +0100199#define show_fan(suffix, value) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400200static ssize_t show_fan_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100202 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct lm80_data *data = lm80_update_device(dev); \
Jean Delvaref8181762008-01-05 15:37:05 +0100204 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
205 DIV_FROM_REG(data->fan_div[nr]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Jean Delvaref8181762008-01-05 15:37:05 +0100207show_fan(min, fan_min)
208show_fan(input, fan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Jean Delvaref8181762008-01-05 15:37:05 +0100210static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
211 char *buf)
212{
213 int nr = to_sensor_dev_attr(attr)->index;
214 struct lm80_data *data = lm80_update_device(dev);
215 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jean Delvaref8181762008-01-05 15:37:05 +0100218static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
219 const char *buf, size_t count)
220{
221 int nr = to_sensor_dev_attr(attr)->index;
222 struct i2c_client *client = to_i2c_client(dev);
223 struct lm80_data *data = i2c_get_clientdata(client);
224 long val = simple_strtoul(buf, NULL, 10);
225
226 mutex_lock(&data->update_lock);
227 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
228 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
229 mutex_unlock(&data->update_lock);
230 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/* Note: we save and restore the fan minimum here, because its value is
234 determined in part by the fan divisor. This follows the principle of
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200235 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 because the divisor changed. */
Jean Delvaref8181762008-01-05 15:37:05 +0100237static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
238 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Jean Delvaref8181762008-01-05 15:37:05 +0100240 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct i2c_client *client = to_i2c_client(dev);
242 struct lm80_data *data = i2c_get_clientdata(client);
243 unsigned long min, val = simple_strtoul(buf, NULL, 10);
244 u8 reg;
245
246 /* Save fan_min */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100247 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 min = FAN_FROM_REG(data->fan_min[nr],
249 DIV_FROM_REG(data->fan_div[nr]));
250
251 switch (val) {
252 case 1: data->fan_div[nr] = 0; break;
253 case 2: data->fan_div[nr] = 1; break;
254 case 4: data->fan_div[nr] = 2; break;
255 case 8: data->fan_div[nr] = 3; break;
256 default:
257 dev_err(&client->dev, "fan_div value %ld not "
258 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100259 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -EINVAL;
261 }
262
263 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
264 | (data->fan_div[nr] << (2 * (nr + 1)));
265 lm80_write_value(client, LM80_REG_FANDIV, reg);
266
267 /* Restore fan_min */
268 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
269 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100270 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return count;
273}
274
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400275static ssize_t show_temp_input1(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct lm80_data *data = lm80_update_device(dev);
278 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
279}
280
281#define show_temp(suffix, value) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400282static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{ \
284 struct lm80_data *data = lm80_update_device(dev); \
285 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
286}
287show_temp(hot_max, temp_hot_max);
288show_temp(hot_hyst, temp_hot_hyst);
289show_temp(os_max, temp_os_max);
290show_temp(os_hyst, temp_os_hyst);
291
292#define set_temp(suffix, value, reg) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400293static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 size_t count) \
295{ \
296 struct i2c_client *client = to_i2c_client(dev); \
297 struct lm80_data *data = i2c_get_clientdata(client); \
298 long val = simple_strtoul(buf, NULL, 10); \
299 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100300 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 data->value = TEMP_LIMIT_TO_REG(val); \
302 lm80_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100303 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return count; \
305}
306set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
307set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
308set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
309set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
310
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100311static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
312 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct lm80_data *data = lm80_update_device(dev);
315 return sprintf(buf, "%u\n", data->alarms);
316}
317
Jean Delvaree84542f2008-01-05 15:40:38 +0100318static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
319 char *buf)
320{
321 int bitnr = to_sensor_dev_attr(attr)->index;
322 struct lm80_data *data = lm80_update_device(dev);
323 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
324}
325
Jean Delvaref8181762008-01-05 15:37:05 +0100326static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
327 show_in_min, set_in_min, 0);
328static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
329 show_in_min, set_in_min, 1);
330static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
331 show_in_min, set_in_min, 2);
332static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
333 show_in_min, set_in_min, 3);
334static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
335 show_in_min, set_in_min, 4);
336static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
337 show_in_min, set_in_min, 5);
338static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
339 show_in_min, set_in_min, 6);
340static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
341 show_in_max, set_in_max, 0);
342static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
343 show_in_max, set_in_max, 1);
344static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
345 show_in_max, set_in_max, 2);
346static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
347 show_in_max, set_in_max, 3);
348static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
349 show_in_max, set_in_max, 4);
350static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
351 show_in_max, set_in_max, 5);
352static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
353 show_in_max, set_in_max, 6);
354static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0);
355static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1);
356static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2);
357static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3);
358static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4);
359static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5);
360static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6);
361static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
362 show_fan_min, set_fan_min, 0);
363static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
364 show_fan_min, set_fan_min, 1);
365static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
366static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
367static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
368 show_fan_div, set_fan_div, 0);
369static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
370 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
372static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
373 set_temp_hot_max);
374static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
375 set_temp_hot_hyst);
376static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
377 set_temp_os_max);
378static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
379 set_temp_os_hyst);
380static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Jean Delvaree84542f2008-01-05 15:40:38 +0100381static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
382static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
383static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
384static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
385static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
386static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
387static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
388static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
389static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
390static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
391static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/*
394 * Real code
395 */
396
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200397static struct attribute *lm80_attributes[] = {
Jean Delvaref8181762008-01-05 15:37:05 +0100398 &sensor_dev_attr_in0_min.dev_attr.attr,
399 &sensor_dev_attr_in1_min.dev_attr.attr,
400 &sensor_dev_attr_in2_min.dev_attr.attr,
401 &sensor_dev_attr_in3_min.dev_attr.attr,
402 &sensor_dev_attr_in4_min.dev_attr.attr,
403 &sensor_dev_attr_in5_min.dev_attr.attr,
404 &sensor_dev_attr_in6_min.dev_attr.attr,
405 &sensor_dev_attr_in0_max.dev_attr.attr,
406 &sensor_dev_attr_in1_max.dev_attr.attr,
407 &sensor_dev_attr_in2_max.dev_attr.attr,
408 &sensor_dev_attr_in3_max.dev_attr.attr,
409 &sensor_dev_attr_in4_max.dev_attr.attr,
410 &sensor_dev_attr_in5_max.dev_attr.attr,
411 &sensor_dev_attr_in6_max.dev_attr.attr,
412 &sensor_dev_attr_in0_input.dev_attr.attr,
413 &sensor_dev_attr_in1_input.dev_attr.attr,
414 &sensor_dev_attr_in2_input.dev_attr.attr,
415 &sensor_dev_attr_in3_input.dev_attr.attr,
416 &sensor_dev_attr_in4_input.dev_attr.attr,
417 &sensor_dev_attr_in5_input.dev_attr.attr,
418 &sensor_dev_attr_in6_input.dev_attr.attr,
419 &sensor_dev_attr_fan1_min.dev_attr.attr,
420 &sensor_dev_attr_fan2_min.dev_attr.attr,
421 &sensor_dev_attr_fan1_input.dev_attr.attr,
422 &sensor_dev_attr_fan2_input.dev_attr.attr,
423 &sensor_dev_attr_fan1_div.dev_attr.attr,
424 &sensor_dev_attr_fan2_div.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200425 &dev_attr_temp1_input.attr,
426 &dev_attr_temp1_max.attr,
427 &dev_attr_temp1_max_hyst.attr,
428 &dev_attr_temp1_crit.attr,
429 &dev_attr_temp1_crit_hyst.attr,
430 &dev_attr_alarms.attr,
Jean Delvaree84542f2008-01-05 15:40:38 +0100431 &sensor_dev_attr_in0_alarm.dev_attr.attr,
432 &sensor_dev_attr_in1_alarm.dev_attr.attr,
433 &sensor_dev_attr_in2_alarm.dev_attr.attr,
434 &sensor_dev_attr_in3_alarm.dev_attr.attr,
435 &sensor_dev_attr_in4_alarm.dev_attr.attr,
436 &sensor_dev_attr_in5_alarm.dev_attr.attr,
437 &sensor_dev_attr_in6_alarm.dev_attr.attr,
438 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
439 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
440 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
441 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200442 NULL
443};
444
445static const struct attribute_group lm80_group = {
446 .attrs = lm80_attributes,
447};
448
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200449/* Return 0 if detection is successful, -ENODEV otherwise */
450static int lm80_detect(struct i2c_client *client, int kind,
451 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200453 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int i, cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200457 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* Now, we do the remaining detection. It is lousy. */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100460 if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200461 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 for (i = 0x2a; i <= 0x3d; i++) {
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100463 cur = i2c_smbus_read_byte_data(client, i);
464 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
465 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
466 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200467 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200470 strlcpy(info->type, "lm80", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200472 return 0;
473}
474
475static int lm80_probe(struct i2c_client *client,
476 const struct i2c_device_id *id)
477{
478 struct lm80_data *data;
479 int err;
480
481 data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL);
482 if (!data) {
483 err = -ENOMEM;
484 goto exit;
485 }
486
487 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100488 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Initialize the LM80 chip */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100491 lm80_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* A few vars need to be filled upon startup */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100494 data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
495 data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* Register sysfs hooks */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100498 if ((err = sysfs_create_group(&client->dev.kobj, &lm80_group)))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200499 goto error_free;
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200500
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100501 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700502 if (IS_ERR(data->hwmon_dev)) {
503 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200504 goto error_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200509error_remove:
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100510 sysfs_remove_group(&client->dev.kobj, &lm80_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511error_free:
512 kfree(data);
513exit:
514 return err;
515}
516
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200517static int lm80_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400519 struct lm80_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Tony Jones1beeffe2007-08-20 13:46:20 -0700521 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200522 sysfs_remove_group(&client->dev.kobj, &lm80_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400524 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 0;
526}
527
528static int lm80_read_value(struct i2c_client *client, u8 reg)
529{
530 return i2c_smbus_read_byte_data(client, reg);
531}
532
533static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
534{
535 return i2c_smbus_write_byte_data(client, reg, value);
536}
537
538/* Called when we have found a new LM80. */
539static void lm80_init_client(struct i2c_client *client)
540{
541 /* Reset all except Watchdog values and last conversion values
542 This sets fan-divs to 2, among others. This makes most other
543 initializations unnecessary */
544 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
545 /* Set 11-bit temperature resolution */
546 lm80_write_value(client, LM80_REG_RES, 0x08);
547
548 /* Start monitoring */
549 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
550}
551
552static struct lm80_data *lm80_update_device(struct device *dev)
553{
554 struct i2c_client *client = to_i2c_client(dev);
555 struct lm80_data *data = i2c_get_clientdata(client);
556 int i;
557
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100558 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
561 dev_dbg(&client->dev, "Starting lm80 update\n");
562 for (i = 0; i <= 6; i++) {
563 data->in[i] =
564 lm80_read_value(client, LM80_REG_IN(i));
565 data->in_min[i] =
566 lm80_read_value(client, LM80_REG_IN_MIN(i));
567 data->in_max[i] =
568 lm80_read_value(client, LM80_REG_IN_MAX(i));
569 }
570 data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
571 data->fan_min[0] =
572 lm80_read_value(client, LM80_REG_FAN_MIN(1));
573 data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
574 data->fan_min[1] =
575 lm80_read_value(client, LM80_REG_FAN_MIN(2));
576
577 data->temp =
578 (lm80_read_value(client, LM80_REG_TEMP) << 8) |
579 (lm80_read_value(client, LM80_REG_RES) & 0xf0);
580 data->temp_os_max =
581 lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
582 data->temp_os_hyst =
583 lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
584 data->temp_hot_max =
585 lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
586 data->temp_hot_hyst =
587 lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
588
589 i = lm80_read_value(client, LM80_REG_FANDIV);
590 data->fan_div[0] = (i >> 2) & 0x03;
591 data->fan_div[1] = (i >> 4) & 0x03;
592 data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
593 (lm80_read_value(client, LM80_REG_ALARM2) << 8);
594 data->last_updated = jiffies;
595 data->valid = 1;
596 }
597
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100598 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 return data;
601}
602
603static int __init sensors_lm80_init(void)
604{
605 return i2c_add_driver(&lm80_driver);
606}
607
608static void __exit sensors_lm80_exit(void)
609{
610 i2c_del_driver(&lm80_driver);
611}
612
613MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
614 "Philip Edelbrock <phil@netroedge.com>");
615MODULE_DESCRIPTION("LM80 driver");
616MODULE_LICENSE("GPL");
617
618module_init(sensors_lm80_init);
619module_exit(sensors_lm80_exit);