blob: 357fbb9987284cf87fa3faf9bf05235a5427e5a1 [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
89static inline long TEMP_FROM_REG(u16 temp)
90{
91 long res;
92
93 temp >>= 4;
94 if (temp < 0x0800)
95 res = 625 * (long) temp;
96 else
97 res = ((long) temp - 0x01000) * 625;
98
99 return res / 10;
100}
101
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100102#define TEMP_LIMIT_FROM_REG(val) (((val) > 0x80 ? \
103 (val) - 0x100 : (val)) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Guenter Roeck2a844c12013-01-09 08:09:34 -0800105#define TEMP_LIMIT_TO_REG(val) clamp_val((val) < 0 ? \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100106 ((val) - 500) / 1000 : ((val) + 500) / 1000, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#define DIV_FROM_REG(val) (1 << (val))
109
110/*
111 * Client data (each client gets its own)
112 */
113
114struct lm80_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700115 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100116 struct mutex update_lock;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100117 char error; /* !=0 if error occurred during last update */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 char valid; /* !=0 if following fields are valid */
119 unsigned long last_updated; /* In jiffies */
120
121 u8 in[7]; /* Register value */
122 u8 in_max[7]; /* Register value */
123 u8 in_min[7]; /* Register value */
124 u8 fan[2]; /* Register value */
125 u8 fan_min[2]; /* Register value */
126 u8 fan_div[2]; /* Register encoding, shifted right */
127 u16 temp; /* Register values, shifted right */
128 u8 temp_hot_max; /* Register value */
129 u8 temp_hot_hyst; /* Register value */
130 u8 temp_os_max; /* Register value */
131 u8 temp_os_hyst; /* Register value */
132 u16 alarms; /* Register encoding, combined */
133};
134
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100135/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * Functions declaration
137 */
138
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200139static int lm80_probe(struct i2c_client *client,
140 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100141static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static void lm80_init_client(struct i2c_client *client);
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200143static int lm80_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static struct lm80_data *lm80_update_device(struct device *dev);
145static int lm80_read_value(struct i2c_client *client, u8 reg);
146static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
147
148/*
149 * Driver data (common to all clients)
150 */
151
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200152static const struct i2c_device_id lm80_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100153 { "lm80", 0 },
Jean Delvare9908ad42012-01-31 09:27:11 -0500154 { "lm96080", 1 },
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200155 { }
156};
157MODULE_DEVICE_TABLE(i2c, lm80_id);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static struct i2c_driver lm80_driver = {
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200160 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100161 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100162 .name = "lm80",
163 },
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200164 .probe = lm80_probe,
165 .remove = lm80_remove,
166 .id_table = lm80_id,
167 .detect = lm80_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100168 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
171/*
172 * Sysfs stuff
173 */
174
175#define show_in(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100176static ssize_t show_in_##suffix(struct device *dev, \
177 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100179 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100181 if (IS_ERR(data)) \
182 return PTR_ERR(data); \
Jean Delvaref8181762008-01-05 15:37:05 +0100183 return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
Jean Delvaref8181762008-01-05 15:37:05 +0100185show_in(min, in_min)
186show_in(max, in_max)
187show_in(input, in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189#define set_in(suffix, value, reg) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100190static ssize_t set_in_##suffix(struct device *dev, \
191 struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100193 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct i2c_client *client = to_i2c_client(dev); \
195 struct lm80_data *data = i2c_get_clientdata(client); \
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100196 long val; \
197 int err = kstrtol(buf, 10, &val); \
198 if (err < 0) \
199 return err; \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100200\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100201 mutex_lock(&data->update_lock);\
Jean Delvaref8181762008-01-05 15:37:05 +0100202 data->value[nr] = IN_TO_REG(val); \
203 lm80_write_value(client, reg(nr), data->value[nr]); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100204 mutex_unlock(&data->update_lock);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return count; \
206}
Jean Delvaref8181762008-01-05 15:37:05 +0100207set_in(min, in_min, LM80_REG_IN_MIN)
208set_in(max, in_max, LM80_REG_IN_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Jean Delvaref8181762008-01-05 15:37:05 +0100210#define show_fan(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100211static ssize_t show_fan_##suffix(struct device *dev, \
212 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{ \
Jean Delvaref8181762008-01-05 15:37:05 +0100214 int nr = to_sensor_dev_attr(attr)->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100216 if (IS_ERR(data)) \
217 return PTR_ERR(data); \
Jean Delvaref8181762008-01-05 15:37:05 +0100218 return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
219 DIV_FROM_REG(data->fan_div[nr]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
Jean Delvaref8181762008-01-05 15:37:05 +0100221show_fan(min, fan_min)
222show_fan(input, fan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jean Delvaref8181762008-01-05 15:37:05 +0100224static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
225 char *buf)
226{
227 int nr = to_sensor_dev_attr(attr)->index;
228 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100229 if (IS_ERR(data))
230 return PTR_ERR(data);
Jean Delvaref8181762008-01-05 15:37:05 +0100231 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jean Delvaref8181762008-01-05 15:37:05 +0100234static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
236{
237 int nr = to_sensor_dev_attr(attr)->index;
238 struct i2c_client *client = to_i2c_client(dev);
239 struct lm80_data *data = i2c_get_clientdata(client);
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100240 unsigned long val;
241 int err = kstrtoul(buf, 10, &val);
242 if (err < 0)
243 return err;
Jean Delvaref8181762008-01-05 15:37:05 +0100244
245 mutex_lock(&data->update_lock);
246 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
247 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
248 mutex_unlock(&data->update_lock);
249 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Guenter Roeck11606312012-01-19 11:02:20 -0800252/*
253 * Note: we save and restore the fan minimum here, because its value is
254 * determined in part by the fan divisor. This follows the principle of
255 * least surprise; the user doesn't expect the fan minimum to change just
256 * because the divisor changed.
257 */
Jean Delvaref8181762008-01-05 15:37:05 +0100258static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
259 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jean Delvaref8181762008-01-05 15:37:05 +0100261 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct i2c_client *client = to_i2c_client(dev);
263 struct lm80_data *data = i2c_get_clientdata(client);
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100264 unsigned long min, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 u8 reg;
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100266 int err = kstrtoul(buf, 10, &val);
267 if (err < 0)
268 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* Save fan_min */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100271 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 min = FAN_FROM_REG(data->fan_min[nr],
273 DIV_FROM_REG(data->fan_div[nr]));
274
275 switch (val) {
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100276 case 1:
277 data->fan_div[nr] = 0;
278 break;
279 case 2:
280 data->fan_div[nr] = 1;
281 break;
282 case 4:
283 data->fan_div[nr] = 2;
284 break;
285 case 8:
286 data->fan_div[nr] = 3;
287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 default:
289 dev_err(&client->dev, "fan_div value %ld not "
290 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100291 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return -EINVAL;
293 }
294
295 reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
296 | (data->fan_div[nr] << (2 * (nr + 1)));
297 lm80_write_value(client, LM80_REG_FANDIV, reg);
298
299 /* Restore fan_min */
300 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
301 lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100302 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return count;
305}
306
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100307static ssize_t show_temp_input1(struct device *dev,
308 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100311 if (IS_ERR(data))
312 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
314}
315
316#define show_temp(suffix, value) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100317static ssize_t show_temp_##suffix(struct device *dev, \
318 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{ \
320 struct lm80_data *data = lm80_update_device(dev); \
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100321 if (IS_ERR(data)) \
322 return PTR_ERR(data); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
324}
325show_temp(hot_max, temp_hot_max);
326show_temp(hot_hyst, temp_hot_hyst);
327show_temp(os_max, temp_os_max);
328show_temp(os_hyst, temp_os_hyst);
329
330#define set_temp(suffix, value, reg) \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100331static ssize_t set_temp_##suffix(struct device *dev, \
332 struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{ \
334 struct i2c_client *client = to_i2c_client(dev); \
335 struct lm80_data *data = i2c_get_clientdata(client); \
Frans Meulenbroeks6a9e7c4c2012-01-10 15:49:35 +0100336 long val; \
337 int err = kstrtol(buf, 10, &val); \
338 if (err < 0) \
339 return err; \
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100340\
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100341 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 data->value = TEMP_LIMIT_TO_REG(val); \
343 lm80_write_value(client, reg, data->value); \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100344 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return count; \
346}
347set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
348set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
349set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
350set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
351
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100352static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
353 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100356 if (IS_ERR(data))
357 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return sprintf(buf, "%u\n", data->alarms);
359}
360
Jean Delvaree84542f2008-01-05 15:40:38 +0100361static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
362 char *buf)
363{
364 int bitnr = to_sensor_dev_attr(attr)->index;
365 struct lm80_data *data = lm80_update_device(dev);
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100366 if (IS_ERR(data))
367 return PTR_ERR(data);
Jean Delvaree84542f2008-01-05 15:40:38 +0100368 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
369}
370
Jean Delvaref8181762008-01-05 15:37:05 +0100371static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
372 show_in_min, set_in_min, 0);
373static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
374 show_in_min, set_in_min, 1);
375static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
376 show_in_min, set_in_min, 2);
377static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
378 show_in_min, set_in_min, 3);
379static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
380 show_in_min, set_in_min, 4);
381static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
382 show_in_min, set_in_min, 5);
383static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
384 show_in_min, set_in_min, 6);
385static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
386 show_in_max, set_in_max, 0);
387static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
388 show_in_max, set_in_max, 1);
389static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
390 show_in_max, set_in_max, 2);
391static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
392 show_in_max, set_in_max, 3);
393static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
394 show_in_max, set_in_max, 4);
395static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
396 show_in_max, set_in_max, 5);
397static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
398 show_in_max, set_in_max, 6);
399static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0);
400static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1);
401static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2);
402static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3);
403static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4);
404static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5);
405static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6);
406static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
407 show_fan_min, set_fan_min, 0);
408static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
409 show_fan_min, set_fan_min, 1);
410static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
411static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
412static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
413 show_fan_div, set_fan_div, 0);
414static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
415 show_fan_div, set_fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
417static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100418 set_temp_hot_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100420 set_temp_hot_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100422 set_temp_os_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100424 set_temp_os_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Jean Delvaree84542f2008-01-05 15:40:38 +0100426static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
427static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
428static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
429static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
430static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
431static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
432static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
433static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
434static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
435static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
436static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438/*
439 * Real code
440 */
441
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200442static struct attribute *lm80_attributes[] = {
Jean Delvaref8181762008-01-05 15:37:05 +0100443 &sensor_dev_attr_in0_min.dev_attr.attr,
444 &sensor_dev_attr_in1_min.dev_attr.attr,
445 &sensor_dev_attr_in2_min.dev_attr.attr,
446 &sensor_dev_attr_in3_min.dev_attr.attr,
447 &sensor_dev_attr_in4_min.dev_attr.attr,
448 &sensor_dev_attr_in5_min.dev_attr.attr,
449 &sensor_dev_attr_in6_min.dev_attr.attr,
450 &sensor_dev_attr_in0_max.dev_attr.attr,
451 &sensor_dev_attr_in1_max.dev_attr.attr,
452 &sensor_dev_attr_in2_max.dev_attr.attr,
453 &sensor_dev_attr_in3_max.dev_attr.attr,
454 &sensor_dev_attr_in4_max.dev_attr.attr,
455 &sensor_dev_attr_in5_max.dev_attr.attr,
456 &sensor_dev_attr_in6_max.dev_attr.attr,
457 &sensor_dev_attr_in0_input.dev_attr.attr,
458 &sensor_dev_attr_in1_input.dev_attr.attr,
459 &sensor_dev_attr_in2_input.dev_attr.attr,
460 &sensor_dev_attr_in3_input.dev_attr.attr,
461 &sensor_dev_attr_in4_input.dev_attr.attr,
462 &sensor_dev_attr_in5_input.dev_attr.attr,
463 &sensor_dev_attr_in6_input.dev_attr.attr,
464 &sensor_dev_attr_fan1_min.dev_attr.attr,
465 &sensor_dev_attr_fan2_min.dev_attr.attr,
466 &sensor_dev_attr_fan1_input.dev_attr.attr,
467 &sensor_dev_attr_fan2_input.dev_attr.attr,
468 &sensor_dev_attr_fan1_div.dev_attr.attr,
469 &sensor_dev_attr_fan2_div.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200470 &dev_attr_temp1_input.attr,
471 &dev_attr_temp1_max.attr,
472 &dev_attr_temp1_max_hyst.attr,
473 &dev_attr_temp1_crit.attr,
474 &dev_attr_temp1_crit_hyst.attr,
475 &dev_attr_alarms.attr,
Jean Delvaree84542f2008-01-05 15:40:38 +0100476 &sensor_dev_attr_in0_alarm.dev_attr.attr,
477 &sensor_dev_attr_in1_alarm.dev_attr.attr,
478 &sensor_dev_attr_in2_alarm.dev_attr.attr,
479 &sensor_dev_attr_in3_alarm.dev_attr.attr,
480 &sensor_dev_attr_in4_alarm.dev_attr.attr,
481 &sensor_dev_attr_in5_alarm.dev_attr.attr,
482 &sensor_dev_attr_in6_alarm.dev_attr.attr,
483 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
484 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
485 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
486 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200487 NULL
488};
489
490static const struct attribute_group lm80_group = {
491 .attrs = lm80_attributes,
492};
493
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200494/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100495static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200497 struct i2c_adapter *adapter = client->adapter;
Jean Delvare9908ad42012-01-31 09:27:11 -0500498 int i, cur, man_id, dev_id;
499 const char *name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200502 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jean Delvare9908ad42012-01-31 09:27:11 -0500504 /* First check for unused bits, common to both chip types */
505 if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
506 || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80))
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200507 return -ENODEV;
Jean Delvare9908ad42012-01-31 09:27:11 -0500508
509 /*
510 * The LM96080 has manufacturer and stepping/die rev registers so we
511 * can just check that. The LM80 does not have such registers so we
512 * have to use a more expensive trick.
513 */
514 man_id = lm80_read_value(client, LM96080_REG_MAN_ID);
515 dev_id = lm80_read_value(client, LM96080_REG_DEV_ID);
516 if (man_id == 0x01 && dev_id == 0x08) {
517 /* Check more unused bits for confirmation */
518 if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe)
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100519 return -ENODEV;
Jean Delvare9908ad42012-01-31 09:27:11 -0500520
521 name = "lm96080";
522 } else {
523 /* Check 6-bit addressing */
524 for (i = 0x2a; i <= 0x3d; i++) {
525 cur = i2c_smbus_read_byte_data(client, i);
526 if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
527 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
528 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
529 return -ENODEV;
530 }
531
532 name = "lm80";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
Jean Delvare9908ad42012-01-31 09:27:11 -0500535 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200537 return 0;
538}
539
540static int lm80_probe(struct i2c_client *client,
541 const struct i2c_device_id *id)
542{
543 struct lm80_data *data;
544 int err;
545
Guenter Roeckc38b2822012-06-02 09:58:09 -0700546 data = devm_kzalloc(&client->dev, sizeof(struct lm80_data), GFP_KERNEL);
547 if (!data)
548 return -ENOMEM;
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200549
550 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100551 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Initialize the LM80 chip */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100554 lm80_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /* A few vars need to be filled upon startup */
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100557 data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
558 data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* Register sysfs hooks */
Frans Meulenbroeks66b3b1f2012-01-04 20:58:53 +0100561 err = sysfs_create_group(&client->dev.kobj, &lm80_group);
562 if (err)
Guenter Roeckc38b2822012-06-02 09:58:09 -0700563 return err;
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200564
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100565 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700566 if (IS_ERR(data->hwmon_dev)) {
567 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200568 goto error_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400569 }
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 0;
572
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200573error_remove:
Jean Delvare6cc37ee2008-01-05 15:35:09 +0100574 sysfs_remove_group(&client->dev.kobj, &lm80_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return err;
576}
577
Jean Delvare8c8bacc2008-07-16 19:30:14 +0200578static int lm80_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400580 struct lm80_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Tony Jones1beeffe2007-08-20 13:46:20 -0700582 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200583 sysfs_remove_group(&client->dev.kobj, &lm80_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586}
587
588static int lm80_read_value(struct i2c_client *client, u8 reg)
589{
590 return i2c_smbus_read_byte_data(client, reg);
591}
592
593static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
594{
595 return i2c_smbus_write_byte_data(client, reg, value);
596}
597
598/* Called when we have found a new LM80. */
599static void lm80_init_client(struct i2c_client *client)
600{
Guenter Roeck11606312012-01-19 11:02:20 -0800601 /*
602 * Reset all except Watchdog values and last conversion values
603 * This sets fan-divs to 2, among others. This makes most other
604 * initializations unnecessary
605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 lm80_write_value(client, LM80_REG_CONFIG, 0x80);
607 /* Set 11-bit temperature resolution */
608 lm80_write_value(client, LM80_REG_RES, 0x08);
609
610 /* Start monitoring */
611 lm80_write_value(client, LM80_REG_CONFIG, 0x01);
612}
613
614static struct lm80_data *lm80_update_device(struct device *dev)
615{
616 struct i2c_client *client = to_i2c_client(dev);
617 struct lm80_data *data = i2c_get_clientdata(client);
618 int i;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100619 int rv;
620 int prev_rv;
621 struct lm80_data *ret = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100623 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100625 if (data->error)
626 lm80_init_client(client);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
629 dev_dbg(&client->dev, "Starting lm80 update\n");
630 for (i = 0; i <= 6; i++) {
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100631 rv = lm80_read_value(client, LM80_REG_IN(i));
632 if (rv < 0)
633 goto abort;
634 data->in[i] = rv;
635
636 rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
637 if (rv < 0)
638 goto abort;
639 data->in_min[i] = rv;
640
641 rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
642 if (rv < 0)
643 goto abort;
644 data->in_max[i] = rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100647 rv = lm80_read_value(client, LM80_REG_FAN1);
648 if (rv < 0)
649 goto abort;
650 data->fan[0] = rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100652 rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
653 if (rv < 0)
654 goto abort;
655 data->fan_min[0] = rv;
656
657 rv = lm80_read_value(client, LM80_REG_FAN2);
658 if (rv < 0)
659 goto abort;
660 data->fan[1] = rv;
661
662 rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
663 if (rv < 0)
664 goto abort;
665 data->fan_min[1] = rv;
666
667 prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
668 if (rv < 0)
669 goto abort;
670 rv = lm80_read_value(client, LM80_REG_RES);
671 if (rv < 0)
672 goto abort;
673 data->temp = (prev_rv << 8) | (rv & 0xf0);
674
675 rv = lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
676 if (rv < 0)
677 goto abort;
678 data->temp_os_max = rv;
679
680 rv = lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
681 if (rv < 0)
682 goto abort;
683 data->temp_os_hyst = rv;
684
685 rv = lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
686 if (rv < 0)
687 goto abort;
688 data->temp_hot_max = rv;
689
690 rv = lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
691 if (rv < 0)
692 goto abort;
693 data->temp_hot_hyst = rv;
694
695 rv = lm80_read_value(client, LM80_REG_FANDIV);
696 if (rv < 0)
697 goto abort;
698 data->fan_div[0] = (rv >> 2) & 0x03;
699 data->fan_div[1] = (rv >> 4) & 0x03;
700
701 prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
702 if (rv < 0)
703 goto abort;
704 rv = lm80_read_value(client, LM80_REG_ALARM2);
705 if (rv < 0)
706 goto abort;
707 data->alarms = prev_rv + (rv << 8);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 data->last_updated = jiffies;
710 data->valid = 1;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100711 data->error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100713 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100715abort:
716 ret = ERR_PTR(rv);
717 data->valid = 0;
Frans Meulenbroeks96585f12012-01-10 15:49:36 +0100718 data->error = 1;
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100719
720done:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100721 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Frans Meulenbroeks2faaa932012-01-05 14:41:53 +0100723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Axel Linf0967ee2012-01-20 15:38:18 +0800726module_i2c_driver(lm80_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
729 "Philip Edelbrock <phil@netroedge.com>");
730MODULE_DESCRIPTION("LM80 driver");
731MODULE_LICENSE("GPL");