blob: 0891b38ffec071d4b35e96db3f31fe674a5a7a17 [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
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
64/* Conversions. Rounding and limit checking is only done on the TO_REG
65 variants. Note that you should be a bit careful with which arguments
66 these macros are called: arguments may be evaluated more than once.
67 Fixing this is just not worth it. */
68
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010069#define IN_TO_REG(val) (SENSORS_LIMIT(((val) + 5) / 10, 0, 255))
70#define IN_FROM_REG(val) ((val) * 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
73{
74 if (rpm == 0)
75 return 255;
76 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010077 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010080#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
81 (val) == 255 ? 0 : 1350000/((div) * (val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static inline long TEMP_FROM_REG(u16 temp)
84{
85 long res;
86
87 temp >>= 4;
88 if (temp < 0x0800)
89 res = 625 * (long) temp;
90 else
91 res = ((long) temp - 0x01000) * 625;
92
93 return res / 10;
94}
95
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010096#define TEMP_LIMIT_FROM_REG(val) (((val) > 0x80 ? \
97 (val) - 0x100 : (val)) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +010099#define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val) < 0 ? \
100 ((val) - 500) / 1000 : ((val) + 500) / 1000, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#define DIV_FROM_REG(val) (1 << (val))
103
104/*
105 * Client data (each client gets its own)
106 */
107
108struct lm80_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700109 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100110 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 char valid; /* !=0 if following fields are valid */
112 unsigned long last_updated; /* In jiffies */
113
114 u8 in[7]; /* Register value */
115 u8 in_max[7]; /* Register value */
116 u8 in_min[7]; /* Register value */
117 u8 fan[2]; /* Register value */
118 u8 fan_min[2]; /* Register value */
119 u8 fan_div[2]; /* Register encoding, shifted right */
120 u16 temp; /* Register values, shifted right */
121 u8 temp_hot_max; /* Register value */
122 u8 temp_hot_hyst; /* Register value */
123 u8 temp_os_max; /* Register value */
124 u8 temp_os_hyst; /* Register value */
125 u16 alarms; /* Register encoding, combined */
126};
127
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100128/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * Functions declaration
130 */
131
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200132static int lm80_probe(struct i2c_client *client,
133 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100134static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static void lm80_init_client(struct i2c_client *client);
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200136static int lm80_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct lm80_data *lm80_update_device(struct device *dev);
138static int lm80_read_value(struct i2c_client *client, u8 reg);
139static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
140
141/*
142 * Driver data (common to all clients)
143 */
144
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200145static const struct i2c_device_id lm80_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100146 { "lm80", 0 },
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200147 { }
148};
149MODULE_DEVICE_TABLE(i2c, lm80_id);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static struct i2c_driver lm80_driver = {
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200152 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100153 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100154 .name = "lm80",
155 },
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200156 .probe = lm80_probe,
157 .remove = lm80_remove,
158 .id_table = lm80_id,
159 .detect = lm80_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100160 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163/*
164 * Sysfs stuff
165 */
166
167#define show_in(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100168static ssize_t show_in_##suffix(struct device *dev, \
169 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100171 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct lm80_data *data = lm80_update_device(dev); \
Jean Delvaref8181762008-01-05 15:37:05 +0100173 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Jean Delvaref8181762008-01-05 15:37:05 +0100175show_in(min, in_min)
176show_in(max, in_max)
177show_in(input, in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179#define set_in(suffix, value, reg) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100180static ssize_t set_in_##suffix(struct device *dev, \
181 struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100183 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct i2c_client *client = to_i2c_client(dev); \
185 struct lm80_data *data = i2c_get_clientdata(client); \
186 long val = simple_strtol(buf, NULL, 10); \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100187\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100188 mutex_lock(&data->update_lock);\
Jean Delvaref8181762008-01-05 15:37:05 +0100189 data->value[nr] = IN_TO_REG(val); \
190 lm80_write_value(client, reg(nr), data->value[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100191 mutex_unlock(&data->update_lock);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return count; \
193}
Jean Delvaref8181762008-01-05 15:37:05 +0100194set_in(min, in_min, LM80_REG_IN_MIN)
195set_in(max, in_max, LM80_REG_IN_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jean Delvaref8181762008-01-05 15:37:05 +0100197#define show_fan(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100198static ssize_t show_fan_##suffix(struct device *dev, \
199 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100201 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct lm80_data *data = lm80_update_device(dev); \
Jean Delvaref8181762008-01-05 15:37:05 +0100203 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
204 DIV_FROM_REG(data->fan_div[nr]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
Jean Delvaref8181762008-01-05 15:37:05 +0100206show_fan(min, fan_min)
207show_fan(input, fan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Jean Delvaref8181762008-01-05 15:37:05 +0100209static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
210 char *buf)
211{
212 int nr = to_sensor_dev_attr(attr)->index;
213 struct lm80_data *data = lm80_update_device(dev);
214 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;
221 struct i2c_client *client = to_i2c_client(dev);
222 struct lm80_data *data = i2c_get_clientdata(client);
223 long val = simple_strtoul(buf, NULL, 10);
224
225 mutex_lock(&data->update_lock);
226 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
227 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
228 mutex_unlock(&data->update_lock);
229 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/* Note: we save and restore the fan minimum here, because its value is
233 determined in part by the fan divisor. This follows the principle of
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200234 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 because the divisor changed. */
Jean Delvaref8181762008-01-05 15:37:05 +0100236static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
237 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Jean Delvaref8181762008-01-05 15:37:05 +0100239 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct i2c_client *client = to_i2c_client(dev);
241 struct lm80_data *data = i2c_get_clientdata(client);
242 unsigned long min, val = simple_strtoul(buf, NULL, 10);
243 u8 reg;
244
245 /* Save fan_min */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100246 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 min = FAN_FROM_REG(data->fan_min[nr],
248 DIV_FROM_REG(data->fan_div[nr]));
249
250 switch (val) {
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100251 case 1:
252 data->fan_div[nr] = 0;
253 break;
254 case 2:
255 data->fan_div[nr] = 1;
256 break;
257 case 4:
258 data->fan_div[nr] = 2;
259 break;
260 case 8:
261 data->fan_div[nr] = 3;
262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 default:
264 dev_err(&client->dev, "fan_div value %ld not "
265 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100266 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return -EINVAL;
268 }
269
270 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
271 | (data->fan_div[nr] << (2 * (nr + 1)));
272 lm80_write_value(client, LM80_REG_FANDIV, reg);
273
274 /* Restore fan_min */
275 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
276 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100277 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 return count;
280}
281
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100282static ssize_t show_temp_input1(struct device *dev,
283 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct lm80_data *data = lm80_update_device(dev);
286 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
287}
288
289#define show_temp(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100290static ssize_t show_temp_##suffix(struct device *dev, \
291 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{ \
293 struct lm80_data *data = lm80_update_device(dev); \
294 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
295}
296show_temp(hot_max, temp_hot_max);
297show_temp(hot_hyst, temp_hot_hyst);
298show_temp(os_max, temp_os_max);
299show_temp(os_hyst, temp_os_hyst);
300
301#define set_temp(suffix, value, reg) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100302static ssize_t set_temp_##suffix(struct device *dev, \
303 struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{ \
305 struct i2c_client *client = to_i2c_client(dev); \
306 struct lm80_data *data = i2c_get_clientdata(client); \
307 long val = simple_strtoul(buf, NULL, 10); \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100308\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100309 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 data->value = TEMP_LIMIT_TO_REG(val); \
311 lm80_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100312 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return count; \
314}
315set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
316set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
317set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
318set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
319
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100320static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
321 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct lm80_data *data = lm80_update_device(dev);
324 return sprintf(buf, "%u\n", data->alarms);
325}
326
Jean Delvaree84542f2008-01-05 15:40:38 +0100327static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
328 char *buf)
329{
330 int bitnr = to_sensor_dev_attr(attr)->index;
331 struct lm80_data *data = lm80_update_device(dev);
332 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
333}
334
Jean Delvaref8181762008-01-05 15:37:05 +0100335static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
336 show_in_min, set_in_min, 0);
337static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
338 show_in_min, set_in_min, 1);
339static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
340 show_in_min, set_in_min, 2);
341static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
342 show_in_min, set_in_min, 3);
343static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
344 show_in_min, set_in_min, 4);
345static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
346 show_in_min, set_in_min, 5);
347static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
348 show_in_min, set_in_min, 6);
349static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
350 show_in_max, set_in_max, 0);
351static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
352 show_in_max, set_in_max, 1);
353static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
354 show_in_max, set_in_max, 2);
355static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
356 show_in_max, set_in_max, 3);
357static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
358 show_in_max, set_in_max, 4);
359static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
360 show_in_max, set_in_max, 5);
361static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
362 show_in_max, set_in_max, 6);
363static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0);
364static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1);
365static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2);
366static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3);
367static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4);
368static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5);
369static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6);
370static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
371 show_fan_min, set_fan_min, 0);
372static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
373 show_fan_min, set_fan_min, 1);
374static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
375static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
376static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
377 show_fan_div, set_fan_div, 0);
378static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
379 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
381static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100382 set_temp_hot_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100384 set_temp_hot_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100386 set_temp_os_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100388 set_temp_os_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Jean Delvaree84542f2008-01-05 15:40:38 +0100390static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
391static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
392static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
393static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
394static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
395static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
396static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
397static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
398static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
399static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
400static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402/*
403 * Real code
404 */
405
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200406static struct attribute *lm80_attributes[] = {
Jean Delvaref8181762008-01-05 15:37:05 +0100407 &sensor_dev_attr_in0_min.dev_attr.attr,
408 &sensor_dev_attr_in1_min.dev_attr.attr,
409 &sensor_dev_attr_in2_min.dev_attr.attr,
410 &sensor_dev_attr_in3_min.dev_attr.attr,
411 &sensor_dev_attr_in4_min.dev_attr.attr,
412 &sensor_dev_attr_in5_min.dev_attr.attr,
413 &sensor_dev_attr_in6_min.dev_attr.attr,
414 &sensor_dev_attr_in0_max.dev_attr.attr,
415 &sensor_dev_attr_in1_max.dev_attr.attr,
416 &sensor_dev_attr_in2_max.dev_attr.attr,
417 &sensor_dev_attr_in3_max.dev_attr.attr,
418 &sensor_dev_attr_in4_max.dev_attr.attr,
419 &sensor_dev_attr_in5_max.dev_attr.attr,
420 &sensor_dev_attr_in6_max.dev_attr.attr,
421 &sensor_dev_attr_in0_input.dev_attr.attr,
422 &sensor_dev_attr_in1_input.dev_attr.attr,
423 &sensor_dev_attr_in2_input.dev_attr.attr,
424 &sensor_dev_attr_in3_input.dev_attr.attr,
425 &sensor_dev_attr_in4_input.dev_attr.attr,
426 &sensor_dev_attr_in5_input.dev_attr.attr,
427 &sensor_dev_attr_in6_input.dev_attr.attr,
428 &sensor_dev_attr_fan1_min.dev_attr.attr,
429 &sensor_dev_attr_fan2_min.dev_attr.attr,
430 &sensor_dev_attr_fan1_input.dev_attr.attr,
431 &sensor_dev_attr_fan2_input.dev_attr.attr,
432 &sensor_dev_attr_fan1_div.dev_attr.attr,
433 &sensor_dev_attr_fan2_div.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200434 &dev_attr_temp1_input.attr,
435 &dev_attr_temp1_max.attr,
436 &dev_attr_temp1_max_hyst.attr,
437 &dev_attr_temp1_crit.attr,
438 &dev_attr_temp1_crit_hyst.attr,
439 &dev_attr_alarms.attr,
Jean Delvaree84542f2008-01-05 15:40:38 +0100440 &sensor_dev_attr_in0_alarm.dev_attr.attr,
441 &sensor_dev_attr_in1_alarm.dev_attr.attr,
442 &sensor_dev_attr_in2_alarm.dev_attr.attr,
443 &sensor_dev_attr_in3_alarm.dev_attr.attr,
444 &sensor_dev_attr_in4_alarm.dev_attr.attr,
445 &sensor_dev_attr_in5_alarm.dev_attr.attr,
446 &sensor_dev_attr_in6_alarm.dev_attr.attr,
447 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
448 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
449 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
450 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200451 NULL
452};
453
454static const struct attribute_group lm80_group = {
455 .attrs = lm80_attributes,
456};
457
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200458/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100459static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200461 struct i2c_adapter *adapter = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int i, cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200465 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /* Now, we do the remaining detection. It is lousy. */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100468 if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200469 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 for (i = 0x2a; i <= 0x3d; i++) {
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100471 cur = i2c_smbus_read_byte_data(client, i);
472 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
473 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
474 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100475 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200478 strlcpy(info->type, "lm80", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200480 return 0;
481}
482
483static int lm80_probe(struct i2c_client *client,
484 const struct i2c_device_id *id)
485{
486 struct lm80_data *data;
487 int err;
488
489 data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL);
490 if (!data) {
491 err = -ENOMEM;
492 goto exit;
493 }
494
495 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100496 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* Initialize the LM80 chip */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100499 lm80_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* A few vars need to be filled upon startup */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100502 data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
503 data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* Register sysfs hooks */
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100506 err = sysfs_create_group(&client->dev.kobj, &lm80_group);
507 if (err)
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200508 goto error_free;
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200509
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100510 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700511 if (IS_ERR(data->hwmon_dev)) {
512 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200513 goto error_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return 0;
517
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200518error_remove:
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100519 sysfs_remove_group(&client->dev.kobj, &lm80_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520error_free:
521 kfree(data);
522exit:
523 return err;
524}
525
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200526static int lm80_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400528 struct lm80_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Tony Jones1beeffe2007-08-20 13:46:20 -0700530 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200531 sysfs_remove_group(&client->dev.kobj, &lm80_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400533 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535}
536
537static int lm80_read_value(struct i2c_client *client, u8 reg)
538{
539 return i2c_smbus_read_byte_data(client, reg);
540}
541
542static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
543{
544 return i2c_smbus_write_byte_data(client, reg, value);
545}
546
547/* Called when we have found a new LM80. */
548static void lm80_init_client(struct i2c_client *client)
549{
550 /* Reset all except Watchdog values and last conversion values
551 This sets fan-divs to 2, among others. This makes most other
552 initializations unnecessary */
553 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
554 /* Set 11-bit temperature resolution */
555 lm80_write_value(client, LM80_REG_RES, 0x08);
556
557 /* Start monitoring */
558 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
559}
560
561static struct lm80_data *lm80_update_device(struct device *dev)
562{
563 struct i2c_client *client = to_i2c_client(dev);
564 struct lm80_data *data = i2c_get_clientdata(client);
565 int i;
566
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100567 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
570 dev_dbg(&client->dev, "Starting lm80 update\n");
571 for (i = 0; i <= 6; i++) {
572 data->in[i] =
573 lm80_read_value(client, LM80_REG_IN(i));
574 data->in_min[i] =
575 lm80_read_value(client, LM80_REG_IN_MIN(i));
576 data->in_max[i] =
577 lm80_read_value(client, LM80_REG_IN_MAX(i));
578 }
579 data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
580 data->fan_min[0] =
581 lm80_read_value(client, LM80_REG_FAN_MIN(1));
582 data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
583 data->fan_min[1] =
584 lm80_read_value(client, LM80_REG_FAN_MIN(2));
585
586 data->temp =
587 (lm80_read_value(client, LM80_REG_TEMP) << 8) |
588 (lm80_read_value(client, LM80_REG_RES) & 0xf0);
589 data->temp_os_max =
590 lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
591 data->temp_os_hyst =
592 lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
593 data->temp_hot_max =
594 lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
595 data->temp_hot_hyst =
596 lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
597
598 i = lm80_read_value(client, LM80_REG_FANDIV);
599 data->fan_div[0] = (i >> 2) & 0x03;
600 data->fan_div[1] = (i >> 4) & 0x03;
601 data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
602 (lm80_read_value(client, LM80_REG_ALARM2) << 8);
603 data->last_updated = jiffies;
604 data->valid = 1;
605 }
606
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100607 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 return data;
610}
611
612static int __init sensors_lm80_init(void)
613{
614 return i2c_add_driver(&lm80_driver);
615}
616
617static void __exit sensors_lm80_exit(void)
618{
619 i2c_del_driver(&lm80_driver);
620}
621
622MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
623 "Philip Edelbrock <phil@netroedge.com>");
624MODULE_DESCRIPTION("LM80 driver");
625MODULE_LICENSE("GPL");
626
627module_init(sensors_lm80_init);
628module_exit(sensors_lm80_exit);