blob: 5b7c69570619a45fd0f7156ddc2f321499b77600 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Jean Delvarec40769f2007-05-08 17:22:00 +02005 Copyright (c) 2007 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
Joe Perchesce47da72011-01-12 21:55:10 +010022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
Jean Delvarec40769f2007-05-08 17:22:00 +020029#include <linux/platform_device.h>
30#include <linux/ioport.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/hwmon.h>
Jean Delvare19f673e2005-07-31 22:12:09 +020032#include <linux/hwmon-vid.h>
Jean Delvare247dde42007-05-08 17:22:01 +020033#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040034#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010035#include <linux/mutex.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020036#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Jean Delvarec40769f2007-05-08 17:22:00 +020038/* ISA device, if found */
39static struct platform_device *pdev;
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050042static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 0x2e, 0x2f, I2C_CLIENT_END };
Jean Delvare2d8672c2005-07-19 23:56:35 +020044static unsigned short isa_address = 0x290;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jean Delvaree5e9f442009-12-14 21:17:27 +010046enum chips { lm78, lm79 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* Many LM78 constants specified below */
49
50/* Length of ISA address segment */
51#define LM78_EXTENT 8
52
53/* Where are the ISA address/data registers relative to the base address */
54#define LM78_ADDR_REG_OFFSET 5
55#define LM78_DATA_REG_OFFSET 6
56
57/* The LM78 registers */
58#define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
59#define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
60#define LM78_REG_IN(nr) (0x20 + (nr))
61
62#define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
63#define LM78_REG_FAN(nr) (0x28 + (nr))
64
65#define LM78_REG_TEMP 0x27
66#define LM78_REG_TEMP_OVER 0x39
67#define LM78_REG_TEMP_HYST 0x3a
68
69#define LM78_REG_ALARM1 0x41
70#define LM78_REG_ALARM2 0x42
71
72#define LM78_REG_VID_FANDIV 0x47
73
74#define LM78_REG_CONFIG 0x40
75#define LM78_REG_CHIPID 0x49
76#define LM78_REG_I2C_ADDR 0x48
77
78
79/* Conversions. Rounding and limit checking is only done on the TO_REG
80 variants. */
81
82/* IN: mV, (0V to 4.08V)
83 REG: 16mV/bit */
84static inline u8 IN_TO_REG(unsigned long val)
85{
86 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
87 return (nval + 8) / 16;
88}
89#define IN_FROM_REG(val) ((val) * 16)
90
91static inline u8 FAN_TO_REG(long rpm, int div)
92{
93 if (rpm <= 0)
94 return 255;
95 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
96}
97
98static inline int FAN_FROM_REG(u8 val, int div)
99{
100 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
101}
102
103/* TEMP: mC (-128C to +127C)
104 REG: 1C/bit, two's complement */
105static inline s8 TEMP_TO_REG(int val)
106{
107 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
108 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
109}
110
111static inline int TEMP_FROM_REG(s8 val)
112{
113 return val * 1000;
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define DIV_FROM_REG(val) (1 << (val))
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118struct lm78_data {
Jean Delvare0c6e9732008-10-17 17:51:16 +0200119 struct i2c_client *client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700120 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100121 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 enum chips type;
123
Jean Delvare6e1b5022008-10-17 17:51:15 +0200124 /* For ISA device only */
125 const char *name;
126 int isa_addr;
127
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100128 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 char valid; /* !=0 if following fields are valid */
130 unsigned long last_updated; /* In jiffies */
131
132 u8 in[7]; /* Register value */
133 u8 in_max[7]; /* Register value */
134 u8 in_min[7]; /* Register value */
135 u8 fan[3]; /* Register value */
136 u8 fan_min[3]; /* Register value */
137 s8 temp; /* Register value */
138 s8 temp_over; /* Register value */
139 s8 temp_hyst; /* Register value */
140 u8 fan_div[3]; /* Register encoding, shifted right */
141 u8 vid; /* Register encoding, combined */
142 u16 alarms; /* Register encoding, combined */
143};
144
145
Jean Delvarec59cc302007-05-08 17:22:01 +0200146static int lm78_read_value(struct lm78_data *data, u8 reg);
147static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static struct lm78_data *lm78_update_device(struct device *dev);
Jean Delvarec59cc302007-05-08 17:22:01 +0200149static void lm78_init_device(struct lm78_data *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* 7 Voltages */
Jean Delvare247dde42007-05-08 17:22:01 +0200153static ssize_t show_in(struct device *dev, struct device_attribute *da,
154 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Jean Delvare247dde42007-05-08 17:22:01 +0200156 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200158 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Jean Delvare247dde42007-05-08 17:22:01 +0200161static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
162 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Jean Delvare247dde42007-05-08 17:22:01 +0200164 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200166 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Jean Delvare247dde42007-05-08 17:22:01 +0200169static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
170 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Jean Delvare247dde42007-05-08 17:22:01 +0200172 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200174 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Jean Delvare247dde42007-05-08 17:22:01 +0200177static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
178 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Jean Delvare247dde42007-05-08 17:22:01 +0200180 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvarec40769f2007-05-08 17:22:00 +0200181 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare247dde42007-05-08 17:22:01 +0200183 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100185 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 data->in_min[nr] = IN_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200187 lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100188 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return count;
190}
191
Jean Delvare247dde42007-05-08 17:22:01 +0200192static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
193 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Jean Delvare247dde42007-05-08 17:22:01 +0200195 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvarec40769f2007-05-08 17:22:00 +0200196 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned long val = simple_strtoul(buf, NULL, 10);
Jean Delvare247dde42007-05-08 17:22:01 +0200198 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100200 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 data->in_max[nr] = IN_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200202 lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100203 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return count;
205}
206
207#define show_in_offset(offset) \
Jean Delvare247dde42007-05-08 17:22:01 +0200208static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
209 show_in, NULL, offset); \
210static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
211 show_in_min, set_in_min, offset); \
212static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
213 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215show_in_offset(0);
216show_in_offset(1);
217show_in_offset(2);
218show_in_offset(3);
219show_in_offset(4);
220show_in_offset(5);
221show_in_offset(6);
222
223/* Temperature */
Jean Delvare247dde42007-05-08 17:22:01 +0200224static ssize_t show_temp(struct device *dev, struct device_attribute *da,
225 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct lm78_data *data = lm78_update_device(dev);
228 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
229}
230
Jean Delvare247dde42007-05-08 17:22:01 +0200231static ssize_t show_temp_over(struct device *dev, struct device_attribute *da,
232 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct lm78_data *data = lm78_update_device(dev);
235 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
236}
237
Jean Delvare247dde42007-05-08 17:22:01 +0200238static ssize_t set_temp_over(struct device *dev, struct device_attribute *da,
239 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Jean Delvarec40769f2007-05-08 17:22:00 +0200241 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 long val = simple_strtol(buf, NULL, 10);
243
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100244 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 data->temp_over = TEMP_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200246 lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100247 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return count;
249}
250
Jean Delvare247dde42007-05-08 17:22:01 +0200251static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da,
252 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct lm78_data *data = lm78_update_device(dev);
255 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
256}
257
Jean Delvare247dde42007-05-08 17:22:01 +0200258static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da,
259 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jean Delvarec40769f2007-05-08 17:22:00 +0200261 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 long val = simple_strtol(buf, NULL, 10);
263
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100264 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 data->temp_hyst = TEMP_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200266 lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100267 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return count;
269}
270
271static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
272static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
273 show_temp_over, set_temp_over);
274static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
275 show_temp_hyst, set_temp_hyst);
276
277/* 3 Fans */
Jean Delvare247dde42007-05-08 17:22:01 +0200278static ssize_t show_fan(struct device *dev, struct device_attribute *da,
279 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Jean Delvare247dde42007-05-08 17:22:01 +0200281 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200283 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
285 DIV_FROM_REG(data->fan_div[nr])) );
286}
287
Jean Delvare247dde42007-05-08 17:22:01 +0200288static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
289 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Jean Delvare247dde42007-05-08 17:22:01 +0200291 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200293 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
295 DIV_FROM_REG(data->fan_div[nr])) );
296}
297
Jean Delvare247dde42007-05-08 17:22:01 +0200298static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
299 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Jean Delvare247dde42007-05-08 17:22:01 +0200301 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvarec40769f2007-05-08 17:22:00 +0200302 struct lm78_data *data = dev_get_drvdata(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200303 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned long val = simple_strtoul(buf, NULL, 10);
305
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100306 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec59cc302007-05-08 17:22:01 +0200308 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100309 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return count;
311}
312
Jean Delvare247dde42007-05-08 17:22:01 +0200313static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
314 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Jean Delvare247dde42007-05-08 17:22:01 +0200316 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct lm78_data *data = lm78_update_device(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200318 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/* Note: we save and restore the fan minimum here, because its value is
322 determined in part by the fan divisor. This follows the principle of
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200323 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 because the divisor changed. */
Jean Delvare247dde42007-05-08 17:22:01 +0200325static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
326 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Jean Delvare247dde42007-05-08 17:22:01 +0200328 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvarec40769f2007-05-08 17:22:00 +0200329 struct lm78_data *data = dev_get_drvdata(dev);
Jean Delvare247dde42007-05-08 17:22:01 +0200330 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 unsigned long val = simple_strtoul(buf, NULL, 10);
332 unsigned long min;
333 u8 reg;
334
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100335 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 min = FAN_FROM_REG(data->fan_min[nr],
337 DIV_FROM_REG(data->fan_div[nr]));
338
339 switch (val) {
340 case 1: data->fan_div[nr] = 0; break;
341 case 2: data->fan_div[nr] = 1; break;
342 case 4: data->fan_div[nr] = 2; break;
343 case 8: data->fan_div[nr] = 3; break;
344 default:
Jean Delvarec40769f2007-05-08 17:22:00 +0200345 dev_err(dev, "fan_div value %ld not "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100347 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return -EINVAL;
349 }
350
Jean Delvarec59cc302007-05-08 17:22:01 +0200351 reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 switch (nr) {
353 case 0:
354 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
355 break;
356 case 1:
357 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
358 break;
359 }
Jean Delvarec59cc302007-05-08 17:22:01 +0200360 lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 data->fan_min[nr] =
363 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec59cc302007-05-08 17:22:01 +0200364 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100365 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 return count;
368}
369
Jean Delvare247dde42007-05-08 17:22:01 +0200370#define show_fan_offset(offset) \
371static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
372 show_fan, NULL, offset - 1); \
373static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
374 show_fan_min, set_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376show_fan_offset(1);
377show_fan_offset(2);
378show_fan_offset(3);
379
380/* Fan 3 divisor is locked in H/W */
Jean Delvare247dde42007-05-08 17:22:01 +0200381static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
382 show_fan_div, set_fan_div, 0);
383static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
384 show_fan_div, set_fan_div, 1);
385static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387/* VID */
Jean Delvare247dde42007-05-08 17:22:01 +0200388static ssize_t show_vid(struct device *dev, struct device_attribute *da,
389 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct lm78_data *data = lm78_update_device(dev);
Jean Delvared0d3cd62005-11-23 15:44:26 -0800392 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
395
396/* Alarms */
Jean Delvare247dde42007-05-08 17:22:01 +0200397static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
398 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct lm78_data *data = lm78_update_device(dev);
401 return sprintf(buf, "%u\n", data->alarms);
402}
403static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
404
Jean Delvare428a7032007-09-04 23:25:33 +0200405static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
406 char *buf)
407{
408 struct lm78_data *data = lm78_update_device(dev);
409 int nr = to_sensor_dev_attr(da)->index;
410 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
411}
412static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
413static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
414static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
415static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
416static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
417static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
418static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
419static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
420static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
421static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
422static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
423
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200424static struct attribute *lm78_attributes[] = {
Jean Delvare247dde42007-05-08 17:22:01 +0200425 &sensor_dev_attr_in0_input.dev_attr.attr,
426 &sensor_dev_attr_in0_min.dev_attr.attr,
427 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200428 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200429 &sensor_dev_attr_in1_input.dev_attr.attr,
430 &sensor_dev_attr_in1_min.dev_attr.attr,
431 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200432 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200433 &sensor_dev_attr_in2_input.dev_attr.attr,
434 &sensor_dev_attr_in2_min.dev_attr.attr,
435 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200436 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200437 &sensor_dev_attr_in3_input.dev_attr.attr,
438 &sensor_dev_attr_in3_min.dev_attr.attr,
439 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200440 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200441 &sensor_dev_attr_in4_input.dev_attr.attr,
442 &sensor_dev_attr_in4_min.dev_attr.attr,
443 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200444 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200445 &sensor_dev_attr_in5_input.dev_attr.attr,
446 &sensor_dev_attr_in5_min.dev_attr.attr,
447 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200448 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200449 &sensor_dev_attr_in6_input.dev_attr.attr,
450 &sensor_dev_attr_in6_min.dev_attr.attr,
451 &sensor_dev_attr_in6_max.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200452 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200453 &dev_attr_temp1_input.attr,
454 &dev_attr_temp1_max.attr,
455 &dev_attr_temp1_max_hyst.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200456 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200457 &sensor_dev_attr_fan1_input.dev_attr.attr,
458 &sensor_dev_attr_fan1_min.dev_attr.attr,
459 &sensor_dev_attr_fan1_div.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200460 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200461 &sensor_dev_attr_fan2_input.dev_attr.attr,
462 &sensor_dev_attr_fan2_min.dev_attr.attr,
463 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200464 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Jean Delvare247dde42007-05-08 17:22:01 +0200465 &sensor_dev_attr_fan3_input.dev_attr.attr,
466 &sensor_dev_attr_fan3_min.dev_attr.attr,
467 &sensor_dev_attr_fan3_div.dev_attr.attr,
Jean Delvare428a7032007-09-04 23:25:33 +0200468 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200469 &dev_attr_alarms.attr,
470 &dev_attr_cpu0_vid.attr,
471
472 NULL
473};
474
475static const struct attribute_group lm78_group = {
476 .attrs = lm78_attributes,
477};
478
Jean Delvarec40769f2007-05-08 17:22:00 +0200479/* I2C devices get this name attribute automatically, but for ISA devices
480 we must create it by ourselves. */
481static ssize_t show_name(struct device *dev, struct device_attribute
482 *devattr, char *buf)
483{
484 struct lm78_data *data = dev_get_drvdata(dev);
485
Jean Delvare6e1b5022008-10-17 17:51:15 +0200486 return sprintf(buf, "%s\n", data->name);
Jean Delvarec40769f2007-05-08 17:22:00 +0200487}
488static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
489
Jean Delvare18c73f92008-10-17 17:51:15 +0200490/* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
491static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
492{
Jean Delvare0c6e9732008-10-17 17:51:16 +0200493 struct lm78_data *isa;
Jean Delvare18c73f92008-10-17 17:51:15 +0200494 int i;
495
496 if (!pdev) /* No ISA chip */
497 return 0;
Jean Delvare18c73f92008-10-17 17:51:15 +0200498 isa = platform_get_drvdata(pdev);
499
500 if (lm78_read_value(isa, LM78_REG_I2C_ADDR) != client->addr)
501 return 0; /* Address doesn't match */
502 if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe))
503 return 0; /* Chip type doesn't match */
504
505 /* We compare all the limit registers, the config register and the
506 * interrupt mask registers */
507 for (i = 0x2b; i <= 0x3d; i++) {
Jean Delvare0c6e9732008-10-17 17:51:16 +0200508 if (lm78_read_value(isa, i) !=
509 i2c_smbus_read_byte_data(client, i))
Jean Delvare18c73f92008-10-17 17:51:15 +0200510 return 0;
511 }
512 if (lm78_read_value(isa, LM78_REG_CONFIG) !=
Jean Delvare0c6e9732008-10-17 17:51:16 +0200513 i2c_smbus_read_byte_data(client, LM78_REG_CONFIG))
Jean Delvare18c73f92008-10-17 17:51:15 +0200514 return 0;
515 for (i = 0x43; i <= 0x46; i++) {
Jean Delvare0c6e9732008-10-17 17:51:16 +0200516 if (lm78_read_value(isa, i) !=
517 i2c_smbus_read_byte_data(client, i))
Jean Delvare18c73f92008-10-17 17:51:15 +0200518 return 0;
519 }
520
521 return 1;
522}
523
Jean Delvare310ec792009-12-14 21:17:23 +0100524static int lm78_i2c_detect(struct i2c_client *client,
Jean Delvare0c6e9732008-10-17 17:51:16 +0200525 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Jean Delvare0c6e9732008-10-17 17:51:16 +0200527 int i;
528 struct lm78_data *isa = pdev ? platform_get_drvdata(pdev) : NULL;
529 const char *client_name;
530 struct i2c_adapter *adapter = client->adapter;
531 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jean Delvare0c6e9732008-10-17 17:51:16 +0200533 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
534 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jean Delvare0c6e9732008-10-17 17:51:16 +0200536 /* We block updates of the ISA device to minimize the risk of
537 concurrent access to the same LM78 chip through different
538 interfaces. */
539 if (isa)
540 mutex_lock(&isa->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Jean Delvare52df6442009-12-09 20:35:57 +0100542 if ((i2c_smbus_read_byte_data(client, LM78_REG_CONFIG) & 0x80)
543 || i2c_smbus_read_byte_data(client, LM78_REG_I2C_ADDR) != address)
544 goto err_nodev;
Jean Delvare0c6e9732008-10-17 17:51:16 +0200545
Jean Delvare52df6442009-12-09 20:35:57 +0100546 /* Explicitly prevent the misdetection of Winbond chips */
547 i = i2c_smbus_read_byte_data(client, 0x4f);
548 if (i == 0xa3 || i == 0x5c)
549 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* Determine the chip type. */
Jean Delvare52df6442009-12-09 20:35:57 +0100552 i = i2c_smbus_read_byte_data(client, LM78_REG_CHIPID);
553 if (i == 0x00 || i == 0x20 /* LM78 */
554 || i == 0x40) /* LM78-J */
555 client_name = "lm78";
556 else if ((i & 0xfe) == 0xc0)
557 client_name = "lm79";
558 else
559 goto err_nodev;
Jean Delvare18c73f92008-10-17 17:51:15 +0200560
Jean Delvare52df6442009-12-09 20:35:57 +0100561 if (lm78_alias_detect(client, i)) {
562 dev_dbg(&adapter->dev, "Device at 0x%02x appears to "
563 "be the same as ISA device\n", address);
564 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Jean Delvare0c6e9732008-10-17 17:51:16 +0200567 if (isa)
568 mutex_unlock(&isa->update_lock);
569
Jean Delvare0c6e9732008-10-17 17:51:16 +0200570 strlcpy(info->type, client_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jean Delvare0c6e9732008-10-17 17:51:16 +0200572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Jean Delvare0c6e9732008-10-17 17:51:16 +0200574 err_nodev:
575 if (isa)
576 mutex_unlock(&isa->update_lock);
577 return -ENODEV;
578}
579
580static int lm78_i2c_probe(struct i2c_client *client,
581 const struct i2c_device_id *id)
582{
583 struct lm78_data *data;
584 int err;
585
586 data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL);
587 if (!data)
588 return -ENOMEM;
589
590 i2c_set_clientdata(client, data);
591 data->client = client;
592 data->type = id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* Initialize the LM78 chip */
Jean Delvarec59cc302007-05-08 17:22:01 +0200595 lm78_init_device(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /* Register sysfs hooks */
Jean Delvare0c6e9732008-10-17 17:51:16 +0200598 err = sysfs_create_group(&client->dev.kobj, &lm78_group);
599 if (err)
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200600 goto ERROR3;
601
Jean Delvare0c6e9732008-10-17 17:51:16 +0200602 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -0700603 if (IS_ERR(data->hwmon_dev)) {
604 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200605 goto ERROR4;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 0;
609
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200610ERROR4:
Jean Delvare0c6e9732008-10-17 17:51:16 +0200611 sysfs_remove_group(&client->dev.kobj, &lm78_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400612ERROR3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return err;
615}
616
Jean Delvare0c6e9732008-10-17 17:51:16 +0200617static int lm78_i2c_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400619 struct lm78_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Tony Jones1beeffe2007-08-20 13:46:20 -0700621 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200622 sysfs_remove_group(&client->dev.kobj, &lm78_group);
Jean Delvarec40769f2007-05-08 17:22:00 +0200623 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jean Delvarec40769f2007-05-08 17:22:00 +0200625 return 0;
626}
627
Jean Delvareed4cebd2011-07-25 21:46:11 +0200628static const struct i2c_device_id lm78_i2c_id[] = {
629 { "lm78", lm78 },
630 { "lm79", lm79 },
631 { }
632};
633MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
Jean Delvarec40769f2007-05-08 17:22:00 +0200634
Jean Delvareed4cebd2011-07-25 21:46:11 +0200635static struct i2c_driver lm78_driver = {
636 .class = I2C_CLASS_HWMON,
637 .driver = {
638 .name = "lm78",
639 },
640 .probe = lm78_i2c_probe,
641 .remove = lm78_i2c_remove,
642 .id_table = lm78_i2c_id,
643 .detect = lm78_i2c_detect,
644 .address_list = normal_i2c,
645};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Steven Cole44bbe872005-05-03 18:21:25 -0600647/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 We don't want to lock the whole ISA bus, so we lock each client
649 separately.
650 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
651 would slow down the LM78 access and should not be necessary. */
Jean Delvarec59cc302007-05-08 17:22:01 +0200652static int lm78_read_value(struct lm78_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jean Delvare0c6e9732008-10-17 17:51:16 +0200654 struct i2c_client *client = data->client;
Jean Delvarec59cc302007-05-08 17:22:01 +0200655
Jean Delvare0c6e9732008-10-17 17:51:16 +0200656 if (!client) { /* ISA device */
Jean Delvarec59cc302007-05-08 17:22:01 +0200657 int res;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100658 mutex_lock(&data->lock);
Jean Delvare6e1b5022008-10-17 17:51:15 +0200659 outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
660 res = inb_p(data->isa_addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100661 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return res;
663 } else
664 return i2c_smbus_read_byte_data(client, reg);
665}
666
Steven Cole44bbe872005-05-03 18:21:25 -0600667/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 We don't want to lock the whole ISA bus, so we lock each client
669 separately.
670 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
671 would slow down the LM78 access and should not be necessary.
672 There are some ugly typecasts here, but the good new is - they should
673 nowhere else be necessary! */
Jean Delvarec59cc302007-05-08 17:22:01 +0200674static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Jean Delvare0c6e9732008-10-17 17:51:16 +0200676 struct i2c_client *client = data->client;
Jean Delvarec59cc302007-05-08 17:22:01 +0200677
Jean Delvare0c6e9732008-10-17 17:51:16 +0200678 if (!client) { /* ISA device */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100679 mutex_lock(&data->lock);
Jean Delvare6e1b5022008-10-17 17:51:15 +0200680 outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
681 outb_p(value, data->isa_addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100682 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684 } else
685 return i2c_smbus_write_byte_data(client, reg, value);
686}
687
Jean Delvarec59cc302007-05-08 17:22:01 +0200688static void lm78_init_device(struct lm78_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Jean Delvarec40769f2007-05-08 17:22:00 +0200690 u8 config;
691 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* Start monitoring */
Jean Delvarec59cc302007-05-08 17:22:01 +0200694 config = lm78_read_value(data, LM78_REG_CONFIG);
Jean Delvarec40769f2007-05-08 17:22:00 +0200695 if ((config & 0x09) != 0x01)
Jean Delvarec59cc302007-05-08 17:22:01 +0200696 lm78_write_value(data, LM78_REG_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 (config & 0xf7) | 0x01);
Jean Delvarec40769f2007-05-08 17:22:00 +0200698
699 /* A few vars need to be filled upon startup */
700 for (i = 0; i < 3; i++) {
Jean Delvarec59cc302007-05-08 17:22:01 +0200701 data->fan_min[i] = lm78_read_value(data,
Jean Delvarec40769f2007-05-08 17:22:00 +0200702 LM78_REG_FAN_MIN(i));
703 }
704
705 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708static struct lm78_data *lm78_update_device(struct device *dev)
709{
Jean Delvarec40769f2007-05-08 17:22:00 +0200710 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int i;
712
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100713 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
716 || !data->valid) {
717
Jean Delvarec40769f2007-05-08 17:22:00 +0200718 dev_dbg(dev, "Starting lm78 update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 for (i = 0; i <= 6; i++) {
721 data->in[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200722 lm78_read_value(data, LM78_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 data->in_min[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200724 lm78_read_value(data, LM78_REG_IN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 data->in_max[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200726 lm78_read_value(data, LM78_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728 for (i = 0; i < 3; i++) {
729 data->fan[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200730 lm78_read_value(data, LM78_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 data->fan_min[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200732 lm78_read_value(data, LM78_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Jean Delvarec59cc302007-05-08 17:22:01 +0200734 data->temp = lm78_read_value(data, LM78_REG_TEMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 data->temp_over =
Jean Delvarec59cc302007-05-08 17:22:01 +0200736 lm78_read_value(data, LM78_REG_TEMP_OVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 data->temp_hyst =
Jean Delvarec59cc302007-05-08 17:22:01 +0200738 lm78_read_value(data, LM78_REG_TEMP_HYST);
739 i = lm78_read_value(data, LM78_REG_VID_FANDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 data->vid = i & 0x0f;
741 if (data->type == lm79)
742 data->vid |=
Jean Delvarec59cc302007-05-08 17:22:01 +0200743 (lm78_read_value(data, LM78_REG_CHIPID) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 0x01) << 4;
745 else
746 data->vid |= 0x10;
747 data->fan_div[0] = (i >> 4) & 0x03;
748 data->fan_div[1] = i >> 6;
Jean Delvarec59cc302007-05-08 17:22:01 +0200749 data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
750 (lm78_read_value(data, LM78_REG_ALARM2) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 data->last_updated = jiffies;
752 data->valid = 1;
753
754 data->fan_div[2] = 1;
755 }
756
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100757 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 return data;
760}
761
Jean Delvareed4cebd2011-07-25 21:46:11 +0200762static int __devinit lm78_isa_probe(struct platform_device *pdev)
763{
764 int err;
765 struct lm78_data *data;
766 struct resource *res;
767
768 /* Reserve the ISA region */
769 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
770 if (!request_region(res->start + LM78_ADDR_REG_OFFSET, 2, "lm78")) {
771 err = -EBUSY;
772 goto exit;
773 }
774
775 data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL);
776 if (!data) {
777 err = -ENOMEM;
778 goto exit_release_region;
779 }
780 mutex_init(&data->lock);
781 data->isa_addr = res->start;
782 platform_set_drvdata(pdev, data);
783
784 if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
785 data->type = lm79;
786 data->name = "lm79";
787 } else {
788 data->type = lm78;
789 data->name = "lm78";
790 }
791
792 /* Initialize the LM78 chip */
793 lm78_init_device(data);
794
795 /* Register sysfs hooks */
796 if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group))
797 || (err = device_create_file(&pdev->dev, &dev_attr_name)))
798 goto exit_remove_files;
799
800 data->hwmon_dev = hwmon_device_register(&pdev->dev);
801 if (IS_ERR(data->hwmon_dev)) {
802 err = PTR_ERR(data->hwmon_dev);
803 goto exit_remove_files;
804 }
805
806 return 0;
807
808 exit_remove_files:
809 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
810 device_remove_file(&pdev->dev, &dev_attr_name);
811 kfree(data);
812 exit_release_region:
813 release_region(res->start + LM78_ADDR_REG_OFFSET, 2);
814 exit:
815 return err;
816}
817
818static int __devexit lm78_isa_remove(struct platform_device *pdev)
819{
820 struct lm78_data *data = platform_get_drvdata(pdev);
821 struct resource *res;
822
823 hwmon_device_unregister(data->hwmon_dev);
824 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
825 device_remove_file(&pdev->dev, &dev_attr_name);
826 kfree(data);
827
828 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
829 release_region(res->start + LM78_ADDR_REG_OFFSET, 2);
830
831 return 0;
832}
833
834static struct platform_driver lm78_isa_driver = {
835 .driver = {
836 .owner = THIS_MODULE,
837 .name = "lm78",
838 },
839 .probe = lm78_isa_probe,
840 .remove = __devexit_p(lm78_isa_remove),
841};
842
Jean Delvarec40769f2007-05-08 17:22:00 +0200843/* return 1 if a supported chip is found, 0 otherwise */
844static int __init lm78_isa_found(unsigned short address)
845{
846 int val, save, found = 0;
Jean Delvare197027e2010-02-05 19:58:36 +0100847 int port;
Jean Delvarec40769f2007-05-08 17:22:00 +0200848
Jean Delvare197027e2010-02-05 19:58:36 +0100849 /* Some boards declare base+0 to base+7 as a PNP device, some base+4
850 * to base+7 and some base+5 to base+6. So we better request each port
851 * individually for the probing phase. */
852 for (port = address; port < address + LM78_EXTENT; port++) {
853 if (!request_region(port, 1, "lm78")) {
Joe Perchesce47da72011-01-12 21:55:10 +0100854 pr_debug("Failed to request port 0x%x\n", port);
Jean Delvare197027e2010-02-05 19:58:36 +0100855 goto release;
856 }
Jean Delvare47c15532008-10-17 17:51:15 +0200857 }
Jean Delvarec40769f2007-05-08 17:22:00 +0200858
859#define REALLY_SLOW_IO
860 /* We need the timeouts for at least some LM78-like
861 chips. But only if we read 'undefined' registers. */
862 val = inb_p(address + 1);
863 if (inb_p(address + 2) != val
864 || inb_p(address + 3) != val
865 || inb_p(address + 7) != val)
866 goto release;
867#undef REALLY_SLOW_IO
868
869 /* We should be able to change the 7 LSB of the address port. The
870 MSB (busy flag) should be clear initially, set after the write. */
871 save = inb_p(address + LM78_ADDR_REG_OFFSET);
872 if (save & 0x80)
873 goto release;
874 val = ~save & 0x7f;
875 outb_p(val, address + LM78_ADDR_REG_OFFSET);
876 if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
877 outb_p(save, address + LM78_ADDR_REG_OFFSET);
878 goto release;
879 }
880
881 /* We found a device, now see if it could be an LM78 */
882 outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
883 val = inb_p(address + LM78_DATA_REG_OFFSET);
884 if (val & 0x80)
885 goto release;
886 outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
887 val = inb_p(address + LM78_DATA_REG_OFFSET);
888 if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
889 goto release;
890
891 /* The busy flag should be clear again */
892 if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
893 goto release;
894
895 /* Explicitly prevent the misdetection of Winbond chips */
896 outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
897 val = inb_p(address + LM78_DATA_REG_OFFSET);
898 if (val == 0xa3 || val == 0x5c)
899 goto release;
900
901 /* Explicitly prevent the misdetection of ITE chips */
902 outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
903 val = inb_p(address + LM78_DATA_REG_OFFSET);
904 if (val == 0x90)
905 goto release;
906
907 /* Determine the chip type */
908 outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
909 val = inb_p(address + LM78_DATA_REG_OFFSET);
Hans de Goedeacf346a2007-07-24 23:36:00 +0200910 if (val == 0x00 || val == 0x20 /* LM78 */
Jean Delvarec40769f2007-05-08 17:22:00 +0200911 || val == 0x40 /* LM78-J */
912 || (val & 0xfe) == 0xc0) /* LM79 */
913 found = 1;
914
915 if (found)
Joe Perchesce47da72011-01-12 21:55:10 +0100916 pr_info("Found an %s chip at %#x\n",
Jean Delvarec40769f2007-05-08 17:22:00 +0200917 val & 0x80 ? "LM79" : "LM78", (int)address);
918
919 release:
Jean Delvare197027e2010-02-05 19:58:36 +0100920 for (port--; port >= address; port--)
921 release_region(port, 1);
Jean Delvarec40769f2007-05-08 17:22:00 +0200922 return found;
923}
924
925static int __init lm78_isa_device_add(unsigned short address)
926{
927 struct resource res = {
928 .start = address,
Jean Delvare15bde2f2007-08-29 10:39:57 +0200929 .end = address + LM78_EXTENT - 1,
Jean Delvarec40769f2007-05-08 17:22:00 +0200930 .name = "lm78",
931 .flags = IORESOURCE_IO,
932 };
933 int err;
934
935 pdev = platform_device_alloc("lm78", address);
936 if (!pdev) {
937 err = -ENOMEM;
Joe Perchesce47da72011-01-12 21:55:10 +0100938 pr_err("Device allocation failed\n");
Jean Delvarec40769f2007-05-08 17:22:00 +0200939 goto exit;
940 }
941
942 err = platform_device_add_resources(pdev, &res, 1);
943 if (err) {
Joe Perchesce47da72011-01-12 21:55:10 +0100944 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvarec40769f2007-05-08 17:22:00 +0200945 goto exit_device_put;
946 }
947
948 err = platform_device_add(pdev);
949 if (err) {
Joe Perchesce47da72011-01-12 21:55:10 +0100950 pr_err("Device addition failed (%d)\n", err);
Jean Delvarec40769f2007-05-08 17:22:00 +0200951 goto exit_device_put;
952 }
953
954 return 0;
955
956 exit_device_put:
957 platform_device_put(pdev);
958 exit:
959 pdev = NULL;
960 return err;
961}
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963static int __init sm_lm78_init(void)
964{
Jean Delvarefde09502005-07-19 23:51:07 +0200965 int res;
966
Jean Delvare18c73f92008-10-17 17:51:15 +0200967 /* We register the ISA device first, so that we can skip the
968 * registration of an I2C interface to the same device. */
Jean Delvarec40769f2007-05-08 17:22:00 +0200969 if (lm78_isa_found(isa_address)) {
970 res = platform_driver_register(&lm78_isa_driver);
971 if (res)
Jean Delvare18c73f92008-10-17 17:51:15 +0200972 goto exit;
Jean Delvarec40769f2007-05-08 17:22:00 +0200973
974 /* Sets global pdev as a side effect */
975 res = lm78_isa_device_add(isa_address);
976 if (res)
977 goto exit_unreg_isa_driver;
978 }
Jean Delvarefde09502005-07-19 23:51:07 +0200979
Jean Delvare18c73f92008-10-17 17:51:15 +0200980 res = i2c_add_driver(&lm78_driver);
981 if (res)
982 goto exit_unreg_isa_device;
983
Jean Delvarefde09502005-07-19 23:51:07 +0200984 return 0;
Jean Delvarec40769f2007-05-08 17:22:00 +0200985
Jean Delvare18c73f92008-10-17 17:51:15 +0200986 exit_unreg_isa_device:
987 platform_device_unregister(pdev);
Jean Delvarec40769f2007-05-08 17:22:00 +0200988 exit_unreg_isa_driver:
989 platform_driver_unregister(&lm78_isa_driver);
Jean Delvarec40769f2007-05-08 17:22:00 +0200990 exit:
991 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static void __exit sm_lm78_exit(void)
995{
Jean Delvarec40769f2007-05-08 17:22:00 +0200996 if (pdev) {
997 platform_device_unregister(pdev);
998 platform_driver_unregister(&lm78_isa_driver);
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 i2c_del_driver(&lm78_driver);
1001}
1002
1003
1004
1005MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
Jean Delvare27fe0482005-07-27 21:30:16 +02001006MODULE_DESCRIPTION("LM78/LM79 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007MODULE_LICENSE("GPL");
1008
1009module_init(sm_lm78_init);
1010module_exit(sm_lm78_exit);