blob: cc68c85e1561d172f5214f77b4856114655537f1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
Jean Delvarec40769f2007-05-08 17:22:00 +020027#include <linux/platform_device.h>
28#include <linux/ioport.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/hwmon.h>
Jean Delvare19f673e2005-07-31 22:12:09 +020030#include <linux/hwmon-vid.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#include <asm/io.h>
34
Jean Delvarec40769f2007-05-08 17:22:00 +020035/* ISA device, if found */
36static struct platform_device *pdev;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* Addresses to scan */
39static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24,
40 0x25, 0x26, 0x27, 0x28, 0x29,
41 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
42 0x2f, I2C_CLIENT_END };
Jean Delvare2d8672c2005-07-19 23:56:35 +020043static unsigned short isa_address = 0x290;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020046I2C_CLIENT_INSMOD_2(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
118/* There are some complications in a module like this. First off, LM78 chips
119 may be both present on the SMBus and the ISA bus, and we have to handle
120 those cases separately at some places. Second, there might be several
121 LM78 chips available (well, actually, that is probably never done; but
122 it is a clean illustration of how to handle a case like that). Finally,
123 a specific chip may be attached to *both* ISA and SMBus, and we would
124 not like to detect it double. Fortunately, in the case of the LM78 at
125 least, a register tells us what SMBus address we are on, so that helps
126 a bit - except if there could be more than one SMBus. Groan. No solution
127 for this yet. */
128
Jean Delvarec40769f2007-05-08 17:22:00 +0200129/* For ISA chips, we abuse the i2c_client addr and name fields. We also use
130 the driver field to differentiate between I2C and ISA chips. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131struct lm78_data {
132 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400133 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100134 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 enum chips type;
136
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100137 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 char valid; /* !=0 if following fields are valid */
139 unsigned long last_updated; /* In jiffies */
140
141 u8 in[7]; /* Register value */
142 u8 in_max[7]; /* Register value */
143 u8 in_min[7]; /* Register value */
144 u8 fan[3]; /* Register value */
145 u8 fan_min[3]; /* Register value */
146 s8 temp; /* Register value */
147 s8 temp_over; /* Register value */
148 s8 temp_hyst; /* Register value */
149 u8 fan_div[3]; /* Register encoding, shifted right */
150 u8 vid; /* Register encoding, combined */
151 u16 alarms; /* Register encoding, combined */
152};
153
154
155static int lm78_attach_adapter(struct i2c_adapter *adapter);
156static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
157static int lm78_detach_client(struct i2c_client *client);
158
Jean Delvarec40769f2007-05-08 17:22:00 +0200159static int __devinit lm78_isa_probe(struct platform_device *pdev);
160static int __devexit lm78_isa_remove(struct platform_device *pdev);
161
Jean Delvarec59cc302007-05-08 17:22:01 +0200162static int lm78_read_value(struct lm78_data *data, u8 reg);
163static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static struct lm78_data *lm78_update_device(struct device *dev);
Jean Delvarec59cc302007-05-08 17:22:01 +0200165static void lm78_init_device(struct lm78_data *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167
168static struct i2c_driver lm78_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100169 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100170 .name = "lm78",
171 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .id = I2C_DRIVERID_LM78,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .attach_adapter = lm78_attach_adapter,
174 .detach_client = lm78_detach_client,
175};
176
Jean Delvarec40769f2007-05-08 17:22:00 +0200177static struct platform_driver lm78_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100178 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200179 .owner = THIS_MODULE,
Jean Delvarec40769f2007-05-08 17:22:00 +0200180 .name = "lm78",
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100181 },
Jean Delvarec40769f2007-05-08 17:22:00 +0200182 .probe = lm78_isa_probe,
183 .remove = lm78_isa_remove,
Jean Delvarefde09502005-07-19 23:51:07 +0200184};
185
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* 7 Voltages */
188static ssize_t show_in(struct device *dev, char *buf, int nr)
189{
190 struct lm78_data *data = lm78_update_device(dev);
191 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
192}
193
194static ssize_t show_in_min(struct device *dev, char *buf, int nr)
195{
196 struct lm78_data *data = lm78_update_device(dev);
197 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
198}
199
200static ssize_t show_in_max(struct device *dev, char *buf, int nr)
201{
202 struct lm78_data *data = lm78_update_device(dev);
203 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
204}
205
206static ssize_t set_in_min(struct device *dev, const char *buf,
207 size_t count, int nr)
208{
Jean Delvarec40769f2007-05-08 17:22:00 +0200209 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned long val = simple_strtoul(buf, NULL, 10);
211
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100212 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 data->in_min[nr] = IN_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200214 lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100215 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return count;
217}
218
219static ssize_t set_in_max(struct device *dev, const char *buf,
220 size_t count, int nr)
221{
Jean Delvarec40769f2007-05-08 17:22:00 +0200222 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 unsigned long val = simple_strtoul(buf, NULL, 10);
224
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100225 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 data->in_max[nr] = IN_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200227 lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100228 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return count;
230}
231
232#define show_in_offset(offset) \
233static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400234 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{ \
236 return show_in(dev, buf, offset); \
237} \
238static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
239 show_in##offset, NULL); \
240static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400241 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{ \
243 return show_in_min(dev, buf, offset); \
244} \
245static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400246 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{ \
248 return show_in_max(dev, buf, offset); \
249} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400250static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 const char *buf, size_t count) \
252{ \
253 return set_in_min(dev, buf, count, offset); \
254} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400255static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 const char *buf, size_t count) \
257{ \
258 return set_in_max(dev, buf, count, offset); \
259} \
260static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
261 show_in##offset##_min, set_in##offset##_min); \
262static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
263 show_in##offset##_max, set_in##offset##_max);
264
265show_in_offset(0);
266show_in_offset(1);
267show_in_offset(2);
268show_in_offset(3);
269show_in_offset(4);
270show_in_offset(5);
271show_in_offset(6);
272
273/* Temperature */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400274static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct lm78_data *data = lm78_update_device(dev);
277 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
278}
279
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400280static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct lm78_data *data = lm78_update_device(dev);
283 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
284}
285
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400286static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Jean Delvarec40769f2007-05-08 17:22:00 +0200288 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 long val = simple_strtol(buf, NULL, 10);
290
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100291 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 data->temp_over = TEMP_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200293 lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100294 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return count;
296}
297
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400298static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct lm78_data *data = lm78_update_device(dev);
301 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
302}
303
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400304static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Jean Delvarec40769f2007-05-08 17:22:00 +0200306 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 long val = simple_strtol(buf, NULL, 10);
308
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100309 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 data->temp_hyst = TEMP_TO_REG(val);
Jean Delvarec59cc302007-05-08 17:22:01 +0200311 lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100312 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return count;
314}
315
316static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
317static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
318 show_temp_over, set_temp_over);
319static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
320 show_temp_hyst, set_temp_hyst);
321
322/* 3 Fans */
323static ssize_t show_fan(struct device *dev, char *buf, int nr)
324{
325 struct lm78_data *data = lm78_update_device(dev);
326 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
327 DIV_FROM_REG(data->fan_div[nr])) );
328}
329
330static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
331{
332 struct lm78_data *data = lm78_update_device(dev);
333 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
334 DIV_FROM_REG(data->fan_div[nr])) );
335}
336
337static ssize_t set_fan_min(struct device *dev, const char *buf,
338 size_t count, int nr)
339{
Jean Delvarec40769f2007-05-08 17:22:00 +0200340 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned long val = simple_strtoul(buf, NULL, 10);
342
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100343 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec59cc302007-05-08 17:22:01 +0200345 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100346 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return count;
348}
349
350static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
351{
352 struct lm78_data *data = lm78_update_device(dev);
353 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
354}
355
356/* Note: we save and restore the fan minimum here, because its value is
357 determined in part by the fan divisor. This follows the principle of
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200358 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 because the divisor changed. */
360static ssize_t set_fan_div(struct device *dev, const char *buf,
361 size_t count, int nr)
362{
Jean Delvarec40769f2007-05-08 17:22:00 +0200363 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 unsigned long val = simple_strtoul(buf, NULL, 10);
365 unsigned long min;
366 u8 reg;
367
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 min = FAN_FROM_REG(data->fan_min[nr],
370 DIV_FROM_REG(data->fan_div[nr]));
371
372 switch (val) {
373 case 1: data->fan_div[nr] = 0; break;
374 case 2: data->fan_div[nr] = 1; break;
375 case 4: data->fan_div[nr] = 2; break;
376 case 8: data->fan_div[nr] = 3; break;
377 default:
Jean Delvarec40769f2007-05-08 17:22:00 +0200378 dev_err(dev, "fan_div value %ld not "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100380 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -EINVAL;
382 }
383
Jean Delvarec59cc302007-05-08 17:22:01 +0200384 reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 switch (nr) {
386 case 0:
387 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
388 break;
389 case 1:
390 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
391 break;
392 }
Jean Delvarec59cc302007-05-08 17:22:01 +0200393 lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 data->fan_min[nr] =
396 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvarec59cc302007-05-08 17:22:01 +0200397 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100398 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 return count;
401}
402
403#define show_fan_offset(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400404static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{ \
406 return show_fan(dev, buf, offset - 1); \
407} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400408static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{ \
410 return show_fan_min(dev, buf, offset - 1); \
411} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400412static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{ \
414 return show_fan_div(dev, buf, offset - 1); \
415} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400416static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 const char *buf, size_t count) \
418{ \
419 return set_fan_min(dev, buf, count, offset - 1); \
420} \
421static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
422static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
423 show_fan_##offset##_min, set_fan_##offset##_min);
424
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400425static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 size_t count)
427{
428 return set_fan_div(dev, buf, count, 0) ;
429}
430
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400431static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 size_t count)
433{
434 return set_fan_div(dev, buf, count, 1) ;
435}
436
437show_fan_offset(1);
438show_fan_offset(2);
439show_fan_offset(3);
440
441/* Fan 3 divisor is locked in H/W */
442static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
443 show_fan_1_div, set_fan_1_div);
444static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
445 show_fan_2_div, set_fan_2_div);
446static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
447
448/* VID */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400449static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct lm78_data *data = lm78_update_device(dev);
Jean Delvared0d3cd62005-11-23 15:44:26 -0800452 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
455
456/* Alarms */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400457static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct lm78_data *data = lm78_update_device(dev);
460 return sprintf(buf, "%u\n", data->alarms);
461}
462static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
463
464/* This function is called when:
465 * lm78_driver is inserted (when this module is loaded), for each
466 available adapter
467 * when a new adapter is inserted (and lm78_driver is still present) */
468static int lm78_attach_adapter(struct i2c_adapter *adapter)
469{
470 if (!(adapter->class & I2C_CLASS_HWMON))
471 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200472 return i2c_probe(adapter, &addr_data, lm78_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200475static struct attribute *lm78_attributes[] = {
476 &dev_attr_in0_input.attr,
477 &dev_attr_in0_min.attr,
478 &dev_attr_in0_max.attr,
479 &dev_attr_in1_input.attr,
480 &dev_attr_in1_min.attr,
481 &dev_attr_in1_max.attr,
482 &dev_attr_in2_input.attr,
483 &dev_attr_in2_min.attr,
484 &dev_attr_in2_max.attr,
485 &dev_attr_in3_input.attr,
486 &dev_attr_in3_min.attr,
487 &dev_attr_in3_max.attr,
488 &dev_attr_in4_input.attr,
489 &dev_attr_in4_min.attr,
490 &dev_attr_in4_max.attr,
491 &dev_attr_in5_input.attr,
492 &dev_attr_in5_min.attr,
493 &dev_attr_in5_max.attr,
494 &dev_attr_in6_input.attr,
495 &dev_attr_in6_min.attr,
496 &dev_attr_in6_max.attr,
497 &dev_attr_temp1_input.attr,
498 &dev_attr_temp1_max.attr,
499 &dev_attr_temp1_max_hyst.attr,
500 &dev_attr_fan1_input.attr,
501 &dev_attr_fan1_min.attr,
502 &dev_attr_fan1_div.attr,
503 &dev_attr_fan2_input.attr,
504 &dev_attr_fan2_min.attr,
505 &dev_attr_fan2_div.attr,
506 &dev_attr_fan3_input.attr,
507 &dev_attr_fan3_min.attr,
508 &dev_attr_fan3_div.attr,
509 &dev_attr_alarms.attr,
510 &dev_attr_cpu0_vid.attr,
511
512 NULL
513};
514
515static const struct attribute_group lm78_group = {
516 .attrs = lm78_attributes,
517};
518
Jean Delvarec40769f2007-05-08 17:22:00 +0200519/* I2C devices get this name attribute automatically, but for ISA devices
520 we must create it by ourselves. */
521static ssize_t show_name(struct device *dev, struct device_attribute
522 *devattr, char *buf)
523{
524 struct lm78_data *data = dev_get_drvdata(dev);
525
526 return sprintf(buf, "%s\n", data->client.name);
527}
528static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
529
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200530/* This function is called by i2c_probe */
Ben Dooksd8d20612005-10-26 21:05:46 +0200531static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 int i, err;
534 struct i2c_client *new_client;
535 struct lm78_data *data;
536 const char *client_name = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jean Delvarec40769f2007-05-08 17:22:00 +0200538 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 err = -ENODEV;
Jean Delvarec40769f2007-05-08 17:22:00 +0200540 goto ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542
543 /* OK. For now, we presume we have a valid client. We now create the
544 client structure, even though we cannot fill it completely yet.
545 But it allows us to access lm78_{read,write}_value. */
546
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200547 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 err = -ENOMEM;
549 goto ERROR1;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 new_client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 i2c_set_clientdata(new_client, data);
554 new_client->addr = address;
555 new_client->adapter = adapter;
Jean Delvarec40769f2007-05-08 17:22:00 +0200556 new_client->driver = &lm78_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* Now, we do the remaining detection. */
559 if (kind < 0) {
Jean Delvarec59cc302007-05-08 17:22:01 +0200560 if (lm78_read_value(data, LM78_REG_CONFIG) & 0x80) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 err = -ENODEV;
562 goto ERROR2;
563 }
Jean Delvarec59cc302007-05-08 17:22:01 +0200564 if (lm78_read_value(data, LM78_REG_I2C_ADDR) !=
Jean Delvarec40769f2007-05-08 17:22:00 +0200565 address) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 err = -ENODEV;
567 goto ERROR2;
568 }
569 }
570
571 /* Determine the chip type. */
572 if (kind <= 0) {
Jean Delvarec59cc302007-05-08 17:22:01 +0200573 i = lm78_read_value(data, LM78_REG_CHIPID);
Jean Delvare27fe0482005-07-27 21:30:16 +0200574 if (i == 0x00 || i == 0x20 /* LM78 */
575 || i == 0x40) /* LM78-J */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 kind = lm78;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 else if ((i & 0xfe) == 0xc0)
578 kind = lm79;
579 else {
580 if (kind == 0)
581 dev_warn(&adapter->dev, "Ignoring 'force' "
582 "parameter for unknown chip at "
583 "adapter %d, address 0x%02x\n",
584 i2c_adapter_id(adapter), address);
585 err = -ENODEV;
586 goto ERROR2;
587 }
588 }
589
590 if (kind == lm78) {
591 client_name = "lm78";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 } else if (kind == lm79) {
593 client_name = "lm79";
594 }
595
596 /* Fill in the remaining client fields and put into the global list */
597 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
598 data->type = kind;
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* Tell the I2C layer a new client has arrived */
601 if ((err = i2c_attach_client(new_client)))
602 goto ERROR2;
603
604 /* Initialize the LM78 chip */
Jean Delvarec59cc302007-05-08 17:22:01 +0200605 lm78_init_device(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Register sysfs hooks */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200608 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group)))
609 goto ERROR3;
610
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400611 data->class_dev = hwmon_device_register(&new_client->dev);
612 if (IS_ERR(data->class_dev)) {
613 err = PTR_ERR(data->class_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200614 goto ERROR4;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400615 }
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return 0;
618
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200619ERROR4:
620 sysfs_remove_group(&new_client->dev.kobj, &lm78_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400621ERROR3:
622 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623ERROR2:
624 kfree(data);
625ERROR1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return err;
627}
628
629static int lm78_detach_client(struct i2c_client *client)
630{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400631 struct lm78_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int err;
633
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400634 hwmon_device_unregister(data->class_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200635 sysfs_remove_group(&client->dev.kobj, &lm78_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400636
Jean Delvare7bef5592005-07-27 22:14:49 +0200637 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Jean Delvarec40769f2007-05-08 17:22:00 +0200640 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jean Delvarec40769f2007-05-08 17:22:00 +0200642 return 0;
643}
644
645static int __devinit lm78_isa_probe(struct platform_device *pdev)
646{
647 int err;
648 struct lm78_data *data;
649 struct resource *res;
650 const char *name;
651
652 /* Reserve the ISA region */
653 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
654 if (!request_region(res->start, LM78_EXTENT, "lm78")) {
655 err = -EBUSY;
656 goto exit;
657 }
658
659 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
660 err = -ENOMEM;
661 goto exit_release_region;
662 }
663 mutex_init(&data->lock);
664 data->client.addr = res->start;
665 i2c_set_clientdata(&data->client, data);
666 platform_set_drvdata(pdev, data);
667
Jean Delvarec59cc302007-05-08 17:22:01 +0200668 if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
Jean Delvarec40769f2007-05-08 17:22:00 +0200669 data->type = lm79;
670 name = "lm79";
671 } else {
672 data->type = lm78;
673 name = "lm78";
674 }
675 strlcpy(data->client.name, name, I2C_NAME_SIZE);
676
677 /* Initialize the LM78 chip */
Jean Delvarec59cc302007-05-08 17:22:01 +0200678 lm78_init_device(data);
Jean Delvarec40769f2007-05-08 17:22:00 +0200679
680 /* Register sysfs hooks */
681 if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group))
682 || (err = device_create_file(&pdev->dev, &dev_attr_name)))
683 goto exit_remove_files;
684
685 data->class_dev = hwmon_device_register(&pdev->dev);
686 if (IS_ERR(data->class_dev)) {
687 err = PTR_ERR(data->class_dev);
688 goto exit_remove_files;
689 }
690
691 return 0;
692
693 exit_remove_files:
694 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
695 device_remove_file(&pdev->dev, &dev_attr_name);
696 kfree(data);
697 exit_release_region:
698 release_region(res->start, LM78_EXTENT);
699 exit:
700 return err;
701}
702
703static int __devexit lm78_isa_remove(struct platform_device *pdev)
704{
705 struct lm78_data *data = platform_get_drvdata(pdev);
706
707 hwmon_device_unregister(data->class_dev);
708 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
709 device_remove_file(&pdev->dev, &dev_attr_name);
710 release_region(data->client.addr, LM78_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400711 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 return 0;
714}
715
Steven Cole44bbe872005-05-03 18:21:25 -0600716/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 We don't want to lock the whole ISA bus, so we lock each client
718 separately.
719 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
720 would slow down the LM78 access and should not be necessary. */
Jean Delvarec59cc302007-05-08 17:22:01 +0200721static int lm78_read_value(struct lm78_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Jean Delvarec59cc302007-05-08 17:22:01 +0200723 struct i2c_client *client = &data->client;
724
Jean Delvarec40769f2007-05-08 17:22:00 +0200725 if (!client->driver) { /* ISA device */
Jean Delvarec59cc302007-05-08 17:22:01 +0200726 int res;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100727 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
729 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100730 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return res;
732 } else
733 return i2c_smbus_read_byte_data(client, reg);
734}
735
Steven Cole44bbe872005-05-03 18:21:25 -0600736/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 We don't want to lock the whole ISA bus, so we lock each client
738 separately.
739 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
740 would slow down the LM78 access and should not be necessary.
741 There are some ugly typecasts here, but the good new is - they should
742 nowhere else be necessary! */
Jean Delvarec59cc302007-05-08 17:22:01 +0200743static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Jean Delvarec59cc302007-05-08 17:22:01 +0200745 struct i2c_client *client = &data->client;
746
Jean Delvarec40769f2007-05-08 17:22:00 +0200747 if (!client->driver) { /* ISA device */
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100748 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
750 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100751 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753 } else
754 return i2c_smbus_write_byte_data(client, reg, value);
755}
756
Jean Delvarec59cc302007-05-08 17:22:01 +0200757static void lm78_init_device(struct lm78_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Jean Delvarec40769f2007-05-08 17:22:00 +0200759 u8 config;
760 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* Start monitoring */
Jean Delvarec59cc302007-05-08 17:22:01 +0200763 config = lm78_read_value(data, LM78_REG_CONFIG);
Jean Delvarec40769f2007-05-08 17:22:00 +0200764 if ((config & 0x09) != 0x01)
Jean Delvarec59cc302007-05-08 17:22:01 +0200765 lm78_write_value(data, LM78_REG_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 (config & 0xf7) | 0x01);
Jean Delvarec40769f2007-05-08 17:22:00 +0200767
768 /* A few vars need to be filled upon startup */
769 for (i = 0; i < 3; i++) {
Jean Delvarec59cc302007-05-08 17:22:01 +0200770 data->fan_min[i] = lm78_read_value(data,
Jean Delvarec40769f2007-05-08 17:22:00 +0200771 LM78_REG_FAN_MIN(i));
772 }
773
774 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777static struct lm78_data *lm78_update_device(struct device *dev)
778{
Jean Delvarec40769f2007-05-08 17:22:00 +0200779 struct lm78_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int i;
781
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100782 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
785 || !data->valid) {
786
Jean Delvarec40769f2007-05-08 17:22:00 +0200787 dev_dbg(dev, "Starting lm78 update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 for (i = 0; i <= 6; i++) {
790 data->in[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200791 lm78_read_value(data, LM78_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 data->in_min[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200793 lm78_read_value(data, LM78_REG_IN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 data->in_max[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200795 lm78_read_value(data, LM78_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 for (i = 0; i < 3; i++) {
798 data->fan[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200799 lm78_read_value(data, LM78_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 data->fan_min[i] =
Jean Delvarec59cc302007-05-08 17:22:01 +0200801 lm78_read_value(data, LM78_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Jean Delvarec59cc302007-05-08 17:22:01 +0200803 data->temp = lm78_read_value(data, LM78_REG_TEMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 data->temp_over =
Jean Delvarec59cc302007-05-08 17:22:01 +0200805 lm78_read_value(data, LM78_REG_TEMP_OVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 data->temp_hyst =
Jean Delvarec59cc302007-05-08 17:22:01 +0200807 lm78_read_value(data, LM78_REG_TEMP_HYST);
808 i = lm78_read_value(data, LM78_REG_VID_FANDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 data->vid = i & 0x0f;
810 if (data->type == lm79)
811 data->vid |=
Jean Delvarec59cc302007-05-08 17:22:01 +0200812 (lm78_read_value(data, LM78_REG_CHIPID) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 0x01) << 4;
814 else
815 data->vid |= 0x10;
816 data->fan_div[0] = (i >> 4) & 0x03;
817 data->fan_div[1] = i >> 6;
Jean Delvarec59cc302007-05-08 17:22:01 +0200818 data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
819 (lm78_read_value(data, LM78_REG_ALARM2) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 data->last_updated = jiffies;
821 data->valid = 1;
822
823 data->fan_div[2] = 1;
824 }
825
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100826 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 return data;
829}
830
Jean Delvarec40769f2007-05-08 17:22:00 +0200831/* return 1 if a supported chip is found, 0 otherwise */
832static int __init lm78_isa_found(unsigned short address)
833{
834 int val, save, found = 0;
835
836 if (!request_region(address, LM78_EXTENT, "lm78"))
837 return 0;
838
839#define REALLY_SLOW_IO
840 /* We need the timeouts for at least some LM78-like
841 chips. But only if we read 'undefined' registers. */
842 val = inb_p(address + 1);
843 if (inb_p(address + 2) != val
844 || inb_p(address + 3) != val
845 || inb_p(address + 7) != val)
846 goto release;
847#undef REALLY_SLOW_IO
848
849 /* We should be able to change the 7 LSB of the address port. The
850 MSB (busy flag) should be clear initially, set after the write. */
851 save = inb_p(address + LM78_ADDR_REG_OFFSET);
852 if (save & 0x80)
853 goto release;
854 val = ~save & 0x7f;
855 outb_p(val, address + LM78_ADDR_REG_OFFSET);
856 if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
857 outb_p(save, address + LM78_ADDR_REG_OFFSET);
858 goto release;
859 }
860
861 /* We found a device, now see if it could be an LM78 */
862 outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
863 val = inb_p(address + LM78_DATA_REG_OFFSET);
864 if (val & 0x80)
865 goto release;
866 outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
867 val = inb_p(address + LM78_DATA_REG_OFFSET);
868 if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
869 goto release;
870
871 /* The busy flag should be clear again */
872 if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
873 goto release;
874
875 /* Explicitly prevent the misdetection of Winbond chips */
876 outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
877 val = inb_p(address + LM78_DATA_REG_OFFSET);
878 if (val == 0xa3 || val == 0x5c)
879 goto release;
880
881 /* Explicitly prevent the misdetection of ITE chips */
882 outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
883 val = inb_p(address + LM78_DATA_REG_OFFSET);
884 if (val == 0x90)
885 goto release;
886
887 /* Determine the chip type */
888 outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
889 val = inb_p(address + LM78_DATA_REG_OFFSET);
890 if (val == 0x00 /* LM78 */
891 || val == 0x40 /* LM78-J */
892 || (val & 0xfe) == 0xc0) /* LM79 */
893 found = 1;
894
895 if (found)
896 pr_info("lm78: Found an %s chip at %#x\n",
897 val & 0x80 ? "LM79" : "LM78", (int)address);
898
899 release:
900 release_region(address, LM78_EXTENT);
901 return found;
902}
903
904static int __init lm78_isa_device_add(unsigned short address)
905{
906 struct resource res = {
907 .start = address,
908 .end = address + LM78_EXTENT,
909 .name = "lm78",
910 .flags = IORESOURCE_IO,
911 };
912 int err;
913
914 pdev = platform_device_alloc("lm78", address);
915 if (!pdev) {
916 err = -ENOMEM;
917 printk(KERN_ERR "lm78: Device allocation failed\n");
918 goto exit;
919 }
920
921 err = platform_device_add_resources(pdev, &res, 1);
922 if (err) {
923 printk(KERN_ERR "lm78: Device resource addition failed "
924 "(%d)\n", err);
925 goto exit_device_put;
926 }
927
928 err = platform_device_add(pdev);
929 if (err) {
930 printk(KERN_ERR "lm78: Device addition failed (%d)\n",
931 err);
932 goto exit_device_put;
933 }
934
935 return 0;
936
937 exit_device_put:
938 platform_device_put(pdev);
939 exit:
940 pdev = NULL;
941 return err;
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944static int __init sm_lm78_init(void)
945{
Jean Delvarefde09502005-07-19 23:51:07 +0200946 int res;
947
948 res = i2c_add_driver(&lm78_driver);
949 if (res)
Jean Delvarec40769f2007-05-08 17:22:00 +0200950 goto exit;
Jean Delvarefde09502005-07-19 23:51:07 +0200951
Jean Delvarec40769f2007-05-08 17:22:00 +0200952 if (lm78_isa_found(isa_address)) {
953 res = platform_driver_register(&lm78_isa_driver);
954 if (res)
955 goto exit_unreg_i2c_driver;
956
957 /* Sets global pdev as a side effect */
958 res = lm78_isa_device_add(isa_address);
959 if (res)
960 goto exit_unreg_isa_driver;
961 }
Jean Delvarefde09502005-07-19 23:51:07 +0200962
963 return 0;
Jean Delvarec40769f2007-05-08 17:22:00 +0200964
965 exit_unreg_isa_driver:
966 platform_driver_unregister(&lm78_isa_driver);
967 exit_unreg_i2c_driver:
968 i2c_del_driver(&lm78_driver);
969 exit:
970 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973static void __exit sm_lm78_exit(void)
974{
Jean Delvarec40769f2007-05-08 17:22:00 +0200975 if (pdev) {
976 platform_device_unregister(pdev);
977 platform_driver_unregister(&lm78_isa_driver);
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 i2c_del_driver(&lm78_driver);
980}
981
982
983
984MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
Jean Delvare27fe0482005-07-27 21:30:16 +0200985MODULE_DESCRIPTION("LM78/LM79 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986MODULE_LICENSE("GPL");
987
988module_init(sm_lm78_init);
989module_exit(sm_lm78_exit);