blob: cfb08978459dfa03399ce52dc405ed660124dfba [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
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100162static int lm78_read_value(struct i2c_client *client, u8 reg);
163static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static struct lm78_data *lm78_update_device(struct device *dev);
165static void lm78_init_client(struct i2c_client *client);
166
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);
210 struct i2c_client *client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned long val = simple_strtoul(buf, NULL, 10);
212
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100213 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 data->in_min[nr] = IN_TO_REG(val);
215 lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100216 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return count;
218}
219
220static ssize_t set_in_max(struct device *dev, const char *buf,
221 size_t count, int nr)
222{
Jean Delvarec40769f2007-05-08 17:22:00 +0200223 struct lm78_data *data = dev_get_drvdata(dev);
224 struct i2c_client *client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 unsigned long val = simple_strtoul(buf, NULL, 10);
226
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100227 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 data->in_max[nr] = IN_TO_REG(val);
229 lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100230 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return count;
232}
233
234#define show_in_offset(offset) \
235static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400236 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{ \
238 return show_in(dev, buf, offset); \
239} \
240static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
241 show_in##offset, NULL); \
242static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400243 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{ \
245 return show_in_min(dev, buf, offset); \
246} \
247static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400248 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{ \
250 return show_in_max(dev, buf, offset); \
251} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400252static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 const char *buf, size_t count) \
254{ \
255 return set_in_min(dev, buf, count, offset); \
256} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400257static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 const char *buf, size_t count) \
259{ \
260 return set_in_max(dev, buf, count, offset); \
261} \
262static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
263 show_in##offset##_min, set_in##offset##_min); \
264static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
265 show_in##offset##_max, set_in##offset##_max);
266
267show_in_offset(0);
268show_in_offset(1);
269show_in_offset(2);
270show_in_offset(3);
271show_in_offset(4);
272show_in_offset(5);
273show_in_offset(6);
274
275/* Temperature */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400276static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct lm78_data *data = lm78_update_device(dev);
279 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
280}
281
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400282static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct lm78_data *data = lm78_update_device(dev);
285 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
286}
287
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400288static 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 -0700289{
Jean Delvarec40769f2007-05-08 17:22:00 +0200290 struct lm78_data *data = dev_get_drvdata(dev);
291 struct i2c_client *client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 long val = simple_strtol(buf, NULL, 10);
293
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100294 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 data->temp_over = TEMP_TO_REG(val);
296 lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100297 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return count;
299}
300
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400301static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct lm78_data *data = lm78_update_device(dev);
304 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
305}
306
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400307static 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 -0700308{
Jean Delvarec40769f2007-05-08 17:22:00 +0200309 struct lm78_data *data = dev_get_drvdata(dev);
310 struct i2c_client *client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 long val = simple_strtol(buf, NULL, 10);
312
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100313 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 data->temp_hyst = TEMP_TO_REG(val);
315 lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100316 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return count;
318}
319
320static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
321static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
322 show_temp_over, set_temp_over);
323static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
324 show_temp_hyst, set_temp_hyst);
325
326/* 3 Fans */
327static ssize_t show_fan(struct device *dev, char *buf, int nr)
328{
329 struct lm78_data *data = lm78_update_device(dev);
330 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
331 DIV_FROM_REG(data->fan_div[nr])) );
332}
333
334static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
335{
336 struct lm78_data *data = lm78_update_device(dev);
337 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
338 DIV_FROM_REG(data->fan_div[nr])) );
339}
340
341static ssize_t set_fan_min(struct device *dev, const char *buf,
342 size_t count, int nr)
343{
Jean Delvarec40769f2007-05-08 17:22:00 +0200344 struct lm78_data *data = dev_get_drvdata(dev);
345 struct i2c_client *client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned long val = simple_strtoul(buf, NULL, 10);
347
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100348 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
350 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100351 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return count;
353}
354
355static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
356{
357 struct lm78_data *data = lm78_update_device(dev);
358 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
359}
360
361/* Note: we save and restore the fan minimum here, because its value is
362 determined in part by the fan divisor. This follows the principle of
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200363 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 because the divisor changed. */
365static ssize_t set_fan_div(struct device *dev, const char *buf,
366 size_t count, int nr)
367{
Jean Delvarec40769f2007-05-08 17:22:00 +0200368 struct lm78_data *data = dev_get_drvdata(dev);
369 struct i2c_client *client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned long val = simple_strtoul(buf, NULL, 10);
371 unsigned long min;
372 u8 reg;
373
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100374 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 min = FAN_FROM_REG(data->fan_min[nr],
376 DIV_FROM_REG(data->fan_div[nr]));
377
378 switch (val) {
379 case 1: data->fan_div[nr] = 0; break;
380 case 2: data->fan_div[nr] = 1; break;
381 case 4: data->fan_div[nr] = 2; break;
382 case 8: data->fan_div[nr] = 3; break;
383 default:
Jean Delvarec40769f2007-05-08 17:22:00 +0200384 dev_err(dev, "fan_div value %ld not "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100386 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return -EINVAL;
388 }
389
390 reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
391 switch (nr) {
392 case 0:
393 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
394 break;
395 case 1:
396 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
397 break;
398 }
399 lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
400
401 data->fan_min[nr] =
402 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
403 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100404 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 return count;
407}
408
409#define show_fan_offset(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400410static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{ \
412 return show_fan(dev, buf, offset - 1); \
413} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400414static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{ \
416 return show_fan_min(dev, buf, offset - 1); \
417} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400418static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{ \
420 return show_fan_div(dev, buf, offset - 1); \
421} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400422static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 const char *buf, size_t count) \
424{ \
425 return set_fan_min(dev, buf, count, offset - 1); \
426} \
427static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
428static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
429 show_fan_##offset##_min, set_fan_##offset##_min);
430
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400431static ssize_t set_fan_1_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, 0) ;
435}
436
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400437static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 size_t count)
439{
440 return set_fan_div(dev, buf, count, 1) ;
441}
442
443show_fan_offset(1);
444show_fan_offset(2);
445show_fan_offset(3);
446
447/* Fan 3 divisor is locked in H/W */
448static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
449 show_fan_1_div, set_fan_1_div);
450static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
451 show_fan_2_div, set_fan_2_div);
452static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
453
454/* VID */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400455static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct lm78_data *data = lm78_update_device(dev);
Jean Delvared0d3cd62005-11-23 15:44:26 -0800458 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
461
462/* Alarms */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400463static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct lm78_data *data = lm78_update_device(dev);
466 return sprintf(buf, "%u\n", data->alarms);
467}
468static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
469
470/* This function is called when:
471 * lm78_driver is inserted (when this module is loaded), for each
472 available adapter
473 * when a new adapter is inserted (and lm78_driver is still present) */
474static int lm78_attach_adapter(struct i2c_adapter *adapter)
475{
476 if (!(adapter->class & I2C_CLASS_HWMON))
477 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200478 return i2c_probe(adapter, &addr_data, lm78_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200481static struct attribute *lm78_attributes[] = {
482 &dev_attr_in0_input.attr,
483 &dev_attr_in0_min.attr,
484 &dev_attr_in0_max.attr,
485 &dev_attr_in1_input.attr,
486 &dev_attr_in1_min.attr,
487 &dev_attr_in1_max.attr,
488 &dev_attr_in2_input.attr,
489 &dev_attr_in2_min.attr,
490 &dev_attr_in2_max.attr,
491 &dev_attr_in3_input.attr,
492 &dev_attr_in3_min.attr,
493 &dev_attr_in3_max.attr,
494 &dev_attr_in4_input.attr,
495 &dev_attr_in4_min.attr,
496 &dev_attr_in4_max.attr,
497 &dev_attr_in5_input.attr,
498 &dev_attr_in5_min.attr,
499 &dev_attr_in5_max.attr,
500 &dev_attr_in6_input.attr,
501 &dev_attr_in6_min.attr,
502 &dev_attr_in6_max.attr,
503 &dev_attr_temp1_input.attr,
504 &dev_attr_temp1_max.attr,
505 &dev_attr_temp1_max_hyst.attr,
506 &dev_attr_fan1_input.attr,
507 &dev_attr_fan1_min.attr,
508 &dev_attr_fan1_div.attr,
509 &dev_attr_fan2_input.attr,
510 &dev_attr_fan2_min.attr,
511 &dev_attr_fan2_div.attr,
512 &dev_attr_fan3_input.attr,
513 &dev_attr_fan3_min.attr,
514 &dev_attr_fan3_div.attr,
515 &dev_attr_alarms.attr,
516 &dev_attr_cpu0_vid.attr,
517
518 NULL
519};
520
521static const struct attribute_group lm78_group = {
522 .attrs = lm78_attributes,
523};
524
Jean Delvarec40769f2007-05-08 17:22:00 +0200525/* I2C devices get this name attribute automatically, but for ISA devices
526 we must create it by ourselves. */
527static ssize_t show_name(struct device *dev, struct device_attribute
528 *devattr, char *buf)
529{
530 struct lm78_data *data = dev_get_drvdata(dev);
531
532 return sprintf(buf, "%s\n", data->client.name);
533}
534static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
535
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200536/* This function is called by i2c_probe */
Ben Dooksd8d20612005-10-26 21:05:46 +0200537static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 int i, err;
540 struct i2c_client *new_client;
541 struct lm78_data *data;
542 const char *client_name = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Jean Delvarec40769f2007-05-08 17:22:00 +0200544 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 err = -ENODEV;
Jean Delvarec40769f2007-05-08 17:22:00 +0200546 goto ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 /* OK. For now, we presume we have a valid client. We now create the
550 client structure, even though we cannot fill it completely yet.
551 But it allows us to access lm78_{read,write}_value. */
552
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200553 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 err = -ENOMEM;
555 goto ERROR1;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 new_client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 i2c_set_clientdata(new_client, data);
560 new_client->addr = address;
561 new_client->adapter = adapter;
Jean Delvarec40769f2007-05-08 17:22:00 +0200562 new_client->driver = &lm78_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Now, we do the remaining detection. */
565 if (kind < 0) {
566 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
567 err = -ENODEV;
568 goto ERROR2;
569 }
Jean Delvarec40769f2007-05-08 17:22:00 +0200570 if (lm78_read_value(new_client, LM78_REG_I2C_ADDR) !=
571 address) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 err = -ENODEV;
573 goto ERROR2;
574 }
575 }
576
577 /* Determine the chip type. */
578 if (kind <= 0) {
579 i = lm78_read_value(new_client, LM78_REG_CHIPID);
Jean Delvare27fe0482005-07-27 21:30:16 +0200580 if (i == 0x00 || i == 0x20 /* LM78 */
581 || i == 0x40) /* LM78-J */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 kind = lm78;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 else if ((i & 0xfe) == 0xc0)
584 kind = lm79;
585 else {
586 if (kind == 0)
587 dev_warn(&adapter->dev, "Ignoring 'force' "
588 "parameter for unknown chip at "
589 "adapter %d, address 0x%02x\n",
590 i2c_adapter_id(adapter), address);
591 err = -ENODEV;
592 goto ERROR2;
593 }
594 }
595
596 if (kind == lm78) {
597 client_name = "lm78";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 } else if (kind == lm79) {
599 client_name = "lm79";
600 }
601
602 /* Fill in the remaining client fields and put into the global list */
603 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
604 data->type = kind;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* Tell the I2C layer a new client has arrived */
607 if ((err = i2c_attach_client(new_client)))
608 goto ERROR2;
609
610 /* Initialize the LM78 chip */
611 lm78_init_client(new_client);
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Register sysfs hooks */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200614 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group)))
615 goto ERROR3;
616
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400617 data->class_dev = hwmon_device_register(&new_client->dev);
618 if (IS_ERR(data->class_dev)) {
619 err = PTR_ERR(data->class_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200620 goto ERROR4;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400621 }
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200625ERROR4:
626 sysfs_remove_group(&new_client->dev.kobj, &lm78_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400627ERROR3:
628 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629ERROR2:
630 kfree(data);
631ERROR1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return err;
633}
634
635static int lm78_detach_client(struct i2c_client *client)
636{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400637 struct lm78_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int err;
639
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400640 hwmon_device_unregister(data->class_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200641 sysfs_remove_group(&client->dev.kobj, &lm78_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400642
Jean Delvare7bef5592005-07-27 22:14:49 +0200643 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Jean Delvarec40769f2007-05-08 17:22:00 +0200646 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Jean Delvarec40769f2007-05-08 17:22:00 +0200648 return 0;
649}
650
651static int __devinit lm78_isa_probe(struct platform_device *pdev)
652{
653 int err;
654 struct lm78_data *data;
655 struct resource *res;
656 const char *name;
657
658 /* Reserve the ISA region */
659 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
660 if (!request_region(res->start, LM78_EXTENT, "lm78")) {
661 err = -EBUSY;
662 goto exit;
663 }
664
665 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
666 err = -ENOMEM;
667 goto exit_release_region;
668 }
669 mutex_init(&data->lock);
670 data->client.addr = res->start;
671 i2c_set_clientdata(&data->client, data);
672 platform_set_drvdata(pdev, data);
673
674 if (lm78_read_value(&data->client, LM78_REG_CHIPID) & 0x80) {
675 data->type = lm79;
676 name = "lm79";
677 } else {
678 data->type = lm78;
679 name = "lm78";
680 }
681 strlcpy(data->client.name, name, I2C_NAME_SIZE);
682
683 /* Initialize the LM78 chip */
684 lm78_init_client(&data->client);
685
686 /* Register sysfs hooks */
687 if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group))
688 || (err = device_create_file(&pdev->dev, &dev_attr_name)))
689 goto exit_remove_files;
690
691 data->class_dev = hwmon_device_register(&pdev->dev);
692 if (IS_ERR(data->class_dev)) {
693 err = PTR_ERR(data->class_dev);
694 goto exit_remove_files;
695 }
696
697 return 0;
698
699 exit_remove_files:
700 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
701 device_remove_file(&pdev->dev, &dev_attr_name);
702 kfree(data);
703 exit_release_region:
704 release_region(res->start, LM78_EXTENT);
705 exit:
706 return err;
707}
708
709static int __devexit lm78_isa_remove(struct platform_device *pdev)
710{
711 struct lm78_data *data = platform_get_drvdata(pdev);
712
713 hwmon_device_unregister(data->class_dev);
714 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
715 device_remove_file(&pdev->dev, &dev_attr_name);
716 release_region(data->client.addr, LM78_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400717 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 return 0;
720}
721
Steven Cole44bbe872005-05-03 18:21:25 -0600722/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 We don't want to lock the whole ISA bus, so we lock each client
724 separately.
725 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
726 would slow down the LM78 access and should not be necessary. */
727static int lm78_read_value(struct i2c_client *client, u8 reg)
728{
729 int res;
Jean Delvarec40769f2007-05-08 17:22:00 +0200730 if (!client->driver) { /* ISA device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct lm78_data *data = i2c_get_clientdata(client);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100732 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
734 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100735 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return res;
737 } else
738 return i2c_smbus_read_byte_data(client, reg);
739}
740
Steven Cole44bbe872005-05-03 18:21:25 -0600741/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 We don't want to lock the whole ISA bus, so we lock each client
743 separately.
744 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
745 would slow down the LM78 access and should not be necessary.
746 There are some ugly typecasts here, but the good new is - they should
747 nowhere else be necessary! */
748static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
749{
Jean Delvarec40769f2007-05-08 17:22:00 +0200750 if (!client->driver) { /* ISA device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct lm78_data *data = i2c_get_clientdata(client);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100752 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
754 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100755 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return 0;
757 } else
758 return i2c_smbus_write_byte_data(client, reg, value);
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static void lm78_init_client(struct i2c_client *client)
762{
Jean Delvarec40769f2007-05-08 17:22:00 +0200763 struct lm78_data *data = i2c_get_clientdata(client);
764 u8 config;
765 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* Start monitoring */
Jean Delvarec40769f2007-05-08 17:22:00 +0200768 config = lm78_read_value(client, LM78_REG_CONFIG);
769 if ((config & 0x09) != 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 lm78_write_value(client, LM78_REG_CONFIG,
771 (config & 0xf7) | 0x01);
Jean Delvarec40769f2007-05-08 17:22:00 +0200772
773 /* A few vars need to be filled upon startup */
774 for (i = 0; i < 3; i++) {
775 data->fan_min[i] = lm78_read_value(client,
776 LM78_REG_FAN_MIN(i));
777 }
778
779 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782static struct lm78_data *lm78_update_device(struct device *dev)
783{
Jean Delvarec40769f2007-05-08 17:22:00 +0200784 struct lm78_data *data = dev_get_drvdata(dev);
785 struct i2c_client *client = &data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int i;
787
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100788 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
791 || !data->valid) {
792
Jean Delvarec40769f2007-05-08 17:22:00 +0200793 dev_dbg(dev, "Starting lm78 update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 for (i = 0; i <= 6; i++) {
796 data->in[i] =
797 lm78_read_value(client, LM78_REG_IN(i));
798 data->in_min[i] =
799 lm78_read_value(client, LM78_REG_IN_MIN(i));
800 data->in_max[i] =
801 lm78_read_value(client, LM78_REG_IN_MAX(i));
802 }
803 for (i = 0; i < 3; i++) {
804 data->fan[i] =
805 lm78_read_value(client, LM78_REG_FAN(i));
806 data->fan_min[i] =
807 lm78_read_value(client, LM78_REG_FAN_MIN(i));
808 }
809 data->temp = lm78_read_value(client, LM78_REG_TEMP);
810 data->temp_over =
811 lm78_read_value(client, LM78_REG_TEMP_OVER);
812 data->temp_hyst =
813 lm78_read_value(client, LM78_REG_TEMP_HYST);
814 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
815 data->vid = i & 0x0f;
816 if (data->type == lm79)
817 data->vid |=
818 (lm78_read_value(client, LM78_REG_CHIPID) &
819 0x01) << 4;
820 else
821 data->vid |= 0x10;
822 data->fan_div[0] = (i >> 4) & 0x03;
823 data->fan_div[1] = i >> 6;
824 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
825 (lm78_read_value(client, LM78_REG_ALARM2) << 8);
826 data->last_updated = jiffies;
827 data->valid = 1;
828
829 data->fan_div[2] = 1;
830 }
831
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100832 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 return data;
835}
836
Jean Delvarec40769f2007-05-08 17:22:00 +0200837/* return 1 if a supported chip is found, 0 otherwise */
838static int __init lm78_isa_found(unsigned short address)
839{
840 int val, save, found = 0;
841
842 if (!request_region(address, LM78_EXTENT, "lm78"))
843 return 0;
844
845#define REALLY_SLOW_IO
846 /* We need the timeouts for at least some LM78-like
847 chips. But only if we read 'undefined' registers. */
848 val = inb_p(address + 1);
849 if (inb_p(address + 2) != val
850 || inb_p(address + 3) != val
851 || inb_p(address + 7) != val)
852 goto release;
853#undef REALLY_SLOW_IO
854
855 /* We should be able to change the 7 LSB of the address port. The
856 MSB (busy flag) should be clear initially, set after the write. */
857 save = inb_p(address + LM78_ADDR_REG_OFFSET);
858 if (save & 0x80)
859 goto release;
860 val = ~save & 0x7f;
861 outb_p(val, address + LM78_ADDR_REG_OFFSET);
862 if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
863 outb_p(save, address + LM78_ADDR_REG_OFFSET);
864 goto release;
865 }
866
867 /* We found a device, now see if it could be an LM78 */
868 outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
869 val = inb_p(address + LM78_DATA_REG_OFFSET);
870 if (val & 0x80)
871 goto release;
872 outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
873 val = inb_p(address + LM78_DATA_REG_OFFSET);
874 if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
875 goto release;
876
877 /* The busy flag should be clear again */
878 if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
879 goto release;
880
881 /* Explicitly prevent the misdetection of Winbond chips */
882 outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
883 val = inb_p(address + LM78_DATA_REG_OFFSET);
884 if (val == 0xa3 || val == 0x5c)
885 goto release;
886
887 /* Explicitly prevent the misdetection of ITE chips */
888 outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
889 val = inb_p(address + LM78_DATA_REG_OFFSET);
890 if (val == 0x90)
891 goto release;
892
893 /* Determine the chip type */
894 outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
895 val = inb_p(address + LM78_DATA_REG_OFFSET);
896 if (val == 0x00 /* LM78 */
897 || val == 0x40 /* LM78-J */
898 || (val & 0xfe) == 0xc0) /* LM79 */
899 found = 1;
900
901 if (found)
902 pr_info("lm78: Found an %s chip at %#x\n",
903 val & 0x80 ? "LM79" : "LM78", (int)address);
904
905 release:
906 release_region(address, LM78_EXTENT);
907 return found;
908}
909
910static int __init lm78_isa_device_add(unsigned short address)
911{
912 struct resource res = {
913 .start = address,
914 .end = address + LM78_EXTENT,
915 .name = "lm78",
916 .flags = IORESOURCE_IO,
917 };
918 int err;
919
920 pdev = platform_device_alloc("lm78", address);
921 if (!pdev) {
922 err = -ENOMEM;
923 printk(KERN_ERR "lm78: Device allocation failed\n");
924 goto exit;
925 }
926
927 err = platform_device_add_resources(pdev, &res, 1);
928 if (err) {
929 printk(KERN_ERR "lm78: Device resource addition failed "
930 "(%d)\n", err);
931 goto exit_device_put;
932 }
933
934 err = platform_device_add(pdev);
935 if (err) {
936 printk(KERN_ERR "lm78: Device addition failed (%d)\n",
937 err);
938 goto exit_device_put;
939 }
940
941 return 0;
942
943 exit_device_put:
944 platform_device_put(pdev);
945 exit:
946 pdev = NULL;
947 return err;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static int __init sm_lm78_init(void)
951{
Jean Delvarefde09502005-07-19 23:51:07 +0200952 int res;
953
954 res = i2c_add_driver(&lm78_driver);
955 if (res)
Jean Delvarec40769f2007-05-08 17:22:00 +0200956 goto exit;
Jean Delvarefde09502005-07-19 23:51:07 +0200957
Jean Delvarec40769f2007-05-08 17:22:00 +0200958 if (lm78_isa_found(isa_address)) {
959 res = platform_driver_register(&lm78_isa_driver);
960 if (res)
961 goto exit_unreg_i2c_driver;
962
963 /* Sets global pdev as a side effect */
964 res = lm78_isa_device_add(isa_address);
965 if (res)
966 goto exit_unreg_isa_driver;
967 }
Jean Delvarefde09502005-07-19 23:51:07 +0200968
969 return 0;
Jean Delvarec40769f2007-05-08 17:22:00 +0200970
971 exit_unreg_isa_driver:
972 platform_driver_unregister(&lm78_isa_driver);
973 exit_unreg_i2c_driver:
974 i2c_del_driver(&lm78_driver);
975 exit:
976 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979static void __exit sm_lm78_exit(void)
980{
Jean Delvarec40769f2007-05-08 17:22:00 +0200981 if (pdev) {
982 platform_device_unregister(pdev);
983 platform_driver_unregister(&lm78_isa_driver);
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 i2c_del_driver(&lm78_driver);
986}
987
988
989
990MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
Jean Delvare27fe0482005-07-27 21:30:16 +0200991MODULE_DESCRIPTION("LM78/LM79 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992MODULE_LICENSE("GPL");
993
994module_init(sm_lm78_init);
995module_exit(sm_lm78_exit);