vt1211: Document module parameters
[linux-2.6-block.git] / drivers / hwmon / lm78.c
CommitLineData
1da177e4
LT
1/*
2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
1da177e4
LT
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.h>
25#include <linux/i2c.h>
fde09509 26#include <linux/i2c-isa.h>
943b0830 27#include <linux/hwmon.h>
19f673ed 28#include <linux/hwmon-vid.h>
943b0830 29#include <linux/err.h>
9a61bf63 30#include <linux/mutex.h>
1da177e4
LT
31#include <asm/io.h>
32
33/* Addresses to scan */
34static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24,
35 0x25, 0x26, 0x27, 0x28, 0x29,
36 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
37 0x2f, I2C_CLIENT_END };
2d8672c5 38static unsigned short isa_address = 0x290;
1da177e4
LT
39
40/* Insmod parameters */
f4b50261 41I2C_CLIENT_INSMOD_2(lm78, lm79);
1da177e4
LT
42
43/* Many LM78 constants specified below */
44
45/* Length of ISA address segment */
46#define LM78_EXTENT 8
47
48/* Where are the ISA address/data registers relative to the base address */
49#define LM78_ADDR_REG_OFFSET 5
50#define LM78_DATA_REG_OFFSET 6
51
52/* The LM78 registers */
53#define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
54#define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
55#define LM78_REG_IN(nr) (0x20 + (nr))
56
57#define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
58#define LM78_REG_FAN(nr) (0x28 + (nr))
59
60#define LM78_REG_TEMP 0x27
61#define LM78_REG_TEMP_OVER 0x39
62#define LM78_REG_TEMP_HYST 0x3a
63
64#define LM78_REG_ALARM1 0x41
65#define LM78_REG_ALARM2 0x42
66
67#define LM78_REG_VID_FANDIV 0x47
68
69#define LM78_REG_CONFIG 0x40
70#define LM78_REG_CHIPID 0x49
71#define LM78_REG_I2C_ADDR 0x48
72
73
74/* Conversions. Rounding and limit checking is only done on the TO_REG
75 variants. */
76
77/* IN: mV, (0V to 4.08V)
78 REG: 16mV/bit */
79static inline u8 IN_TO_REG(unsigned long val)
80{
81 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
82 return (nval + 8) / 16;
83}
84#define IN_FROM_REG(val) ((val) * 16)
85
86static inline u8 FAN_TO_REG(long rpm, int div)
87{
88 if (rpm <= 0)
89 return 255;
90 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
91}
92
93static inline int FAN_FROM_REG(u8 val, int div)
94{
95 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
96}
97
98/* TEMP: mC (-128C to +127C)
99 REG: 1C/bit, two's complement */
100static inline s8 TEMP_TO_REG(int val)
101{
102 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
103 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
104}
105
106static inline int TEMP_FROM_REG(s8 val)
107{
108 return val * 1000;
109}
110
1da177e4
LT
111#define DIV_FROM_REG(val) (1 << (val))
112
113/* There are some complications in a module like this. First off, LM78 chips
114 may be both present on the SMBus and the ISA bus, and we have to handle
115 those cases separately at some places. Second, there might be several
116 LM78 chips available (well, actually, that is probably never done; but
117 it is a clean illustration of how to handle a case like that). Finally,
118 a specific chip may be attached to *both* ISA and SMBus, and we would
119 not like to detect it double. Fortunately, in the case of the LM78 at
120 least, a register tells us what SMBus address we are on, so that helps
121 a bit - except if there could be more than one SMBus. Groan. No solution
122 for this yet. */
123
124/* This module may seem overly long and complicated. In fact, it is not so
125 bad. Quite a lot of bookkeeping is done. A real driver can often cut
126 some corners. */
127
128/* For each registered LM78, we need to keep some data in memory. That
129 data is pointed to by lm78_list[NR]->data. The structure itself is
130 dynamically allocated, at the same time when a new lm78 client is
131 allocated. */
132struct lm78_data {
133 struct i2c_client client;
943b0830 134 struct class_device *class_dev;
9a61bf63 135 struct mutex lock;
1da177e4
LT
136 enum chips type;
137
9a61bf63 138 struct mutex update_lock;
1da177e4
LT
139 char valid; /* !=0 if following fields are valid */
140 unsigned long last_updated; /* In jiffies */
141
142 u8 in[7]; /* Register value */
143 u8 in_max[7]; /* Register value */
144 u8 in_min[7]; /* Register value */
145 u8 fan[3]; /* Register value */
146 u8 fan_min[3]; /* Register value */
147 s8 temp; /* Register value */
148 s8 temp_over; /* Register value */
149 s8 temp_hyst; /* Register value */
150 u8 fan_div[3]; /* Register encoding, shifted right */
151 u8 vid; /* Register encoding, combined */
152 u16 alarms; /* Register encoding, combined */
153};
154
155
156static int lm78_attach_adapter(struct i2c_adapter *adapter);
2d8672c5 157static int lm78_isa_attach_adapter(struct i2c_adapter *adapter);
1da177e4
LT
158static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
159static int lm78_detach_client(struct i2c_client *client);
160
f6c27fc1
DJ
161static int lm78_read_value(struct i2c_client *client, u8 reg);
162static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value);
1da177e4
LT
163static struct lm78_data *lm78_update_device(struct device *dev);
164static void lm78_init_client(struct i2c_client *client);
165
166
167static struct i2c_driver lm78_driver = {
cdaf7934 168 .driver = {
cdaf7934
LR
169 .name = "lm78",
170 },
1da177e4 171 .id = I2C_DRIVERID_LM78,
1da177e4
LT
172 .attach_adapter = lm78_attach_adapter,
173 .detach_client = lm78_detach_client,
174};
175
fde09509 176static struct i2c_driver lm78_isa_driver = {
cdaf7934 177 .driver = {
87218842 178 .owner = THIS_MODULE,
cdaf7934
LR
179 .name = "lm78-isa",
180 },
2d8672c5 181 .attach_adapter = lm78_isa_attach_adapter,
fde09509
JD
182 .detach_client = lm78_detach_client,
183};
184
185
1da177e4
LT
186/* 7 Voltages */
187static ssize_t show_in(struct device *dev, char *buf, int nr)
188{
189 struct lm78_data *data = lm78_update_device(dev);
190 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
191}
192
193static ssize_t show_in_min(struct device *dev, char *buf, int nr)
194{
195 struct lm78_data *data = lm78_update_device(dev);
196 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
197}
198
199static ssize_t show_in_max(struct device *dev, char *buf, int nr)
200{
201 struct lm78_data *data = lm78_update_device(dev);
202 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
203}
204
205static ssize_t set_in_min(struct device *dev, const char *buf,
206 size_t count, int nr)
207{
208 struct i2c_client *client = to_i2c_client(dev);
209 struct lm78_data *data = i2c_get_clientdata(client);
210 unsigned long val = simple_strtoul(buf, NULL, 10);
211
9a61bf63 212 mutex_lock(&data->update_lock);
1da177e4
LT
213 data->in_min[nr] = IN_TO_REG(val);
214 lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
9a61bf63 215 mutex_unlock(&data->update_lock);
1da177e4
LT
216 return count;
217}
218
219static ssize_t set_in_max(struct device *dev, const char *buf,
220 size_t count, int nr)
221{
222 struct i2c_client *client = to_i2c_client(dev);
223 struct lm78_data *data = i2c_get_clientdata(client);
224 unsigned long val = simple_strtoul(buf, NULL, 10);
225
9a61bf63 226 mutex_lock(&data->update_lock);
1da177e4
LT
227 data->in_max[nr] = IN_TO_REG(val);
228 lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
9a61bf63 229 mutex_unlock(&data->update_lock);
1da177e4
LT
230 return count;
231}
232
233#define show_in_offset(offset) \
234static ssize_t \
8627f9ba 235 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
236{ \
237 return show_in(dev, buf, offset); \
238} \
239static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
240 show_in##offset, NULL); \
241static ssize_t \
8627f9ba 242 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
243{ \
244 return show_in_min(dev, buf, offset); \
245} \
246static ssize_t \
8627f9ba 247 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
248{ \
249 return show_in_max(dev, buf, offset); \
250} \
8627f9ba 251static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
252 const char *buf, size_t count) \
253{ \
254 return set_in_min(dev, buf, count, offset); \
255} \
8627f9ba 256static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
257 const char *buf, size_t count) \
258{ \
259 return set_in_max(dev, buf, count, offset); \
260} \
261static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
262 show_in##offset##_min, set_in##offset##_min); \
263static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
264 show_in##offset##_max, set_in##offset##_max);
265
266show_in_offset(0);
267show_in_offset(1);
268show_in_offset(2);
269show_in_offset(3);
270show_in_offset(4);
271show_in_offset(5);
272show_in_offset(6);
273
274/* Temperature */
8627f9ba 275static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
276{
277 struct lm78_data *data = lm78_update_device(dev);
278 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
279}
280
8627f9ba 281static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
282{
283 struct lm78_data *data = lm78_update_device(dev);
284 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
285}
286
8627f9ba 287static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
288{
289 struct i2c_client *client = to_i2c_client(dev);
290 struct lm78_data *data = i2c_get_clientdata(client);
291 long val = simple_strtol(buf, NULL, 10);
292
9a61bf63 293 mutex_lock(&data->update_lock);
1da177e4
LT
294 data->temp_over = TEMP_TO_REG(val);
295 lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
9a61bf63 296 mutex_unlock(&data->update_lock);
1da177e4
LT
297 return count;
298}
299
8627f9ba 300static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
301{
302 struct lm78_data *data = lm78_update_device(dev);
303 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
304}
305
8627f9ba 306static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
307{
308 struct i2c_client *client = to_i2c_client(dev);
309 struct lm78_data *data = i2c_get_clientdata(client);
310 long val = simple_strtol(buf, NULL, 10);
311
9a61bf63 312 mutex_lock(&data->update_lock);
1da177e4
LT
313 data->temp_hyst = TEMP_TO_REG(val);
314 lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
9a61bf63 315 mutex_unlock(&data->update_lock);
1da177e4
LT
316 return count;
317}
318
319static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
320static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
321 show_temp_over, set_temp_over);
322static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
323 show_temp_hyst, set_temp_hyst);
324
325/* 3 Fans */
326static ssize_t show_fan(struct device *dev, char *buf, int nr)
327{
328 struct lm78_data *data = lm78_update_device(dev);
329 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
330 DIV_FROM_REG(data->fan_div[nr])) );
331}
332
333static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
334{
335 struct lm78_data *data = lm78_update_device(dev);
336 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
337 DIV_FROM_REG(data->fan_div[nr])) );
338}
339
340static ssize_t set_fan_min(struct device *dev, const char *buf,
341 size_t count, int nr)
342{
343 struct i2c_client *client = to_i2c_client(dev);
344 struct lm78_data *data = i2c_get_clientdata(client);
345 unsigned long val = simple_strtoul(buf, NULL, 10);
346
9a61bf63 347 mutex_lock(&data->update_lock);
1da177e4
LT
348 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
349 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 350 mutex_unlock(&data->update_lock);
1da177e4
LT
351 return count;
352}
353
354static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
355{
356 struct lm78_data *data = lm78_update_device(dev);
357 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
358}
359
360/* Note: we save and restore the fan minimum here, because its value is
361 determined in part by the fan divisor. This follows the principle of
d6e05edc 362 least surprise; the user doesn't expect the fan minimum to change just
1da177e4
LT
363 because the divisor changed. */
364static ssize_t set_fan_div(struct device *dev, const char *buf,
365 size_t count, int nr)
366{
367 struct i2c_client *client = to_i2c_client(dev);
368 struct lm78_data *data = i2c_get_clientdata(client);
369 unsigned long val = simple_strtoul(buf, NULL, 10);
370 unsigned long min;
371 u8 reg;
372
9a61bf63 373 mutex_lock(&data->update_lock);
1da177e4
LT
374 min = FAN_FROM_REG(data->fan_min[nr],
375 DIV_FROM_REG(data->fan_div[nr]));
376
377 switch (val) {
378 case 1: data->fan_div[nr] = 0; break;
379 case 2: data->fan_div[nr] = 1; break;
380 case 4: data->fan_div[nr] = 2; break;
381 case 8: data->fan_div[nr] = 3; break;
382 default:
383 dev_err(&client->dev, "fan_div value %ld not "
384 "supported. Choose one of 1, 2, 4 or 8!\n", val);
9a61bf63 385 mutex_unlock(&data->update_lock);
1da177e4
LT
386 return -EINVAL;
387 }
388
389 reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
390 switch (nr) {
391 case 0:
392 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
393 break;
394 case 1:
395 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
396 break;
397 }
398 lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
399
400 data->fan_min[nr] =
401 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
402 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 403 mutex_unlock(&data->update_lock);
1da177e4
LT
404
405 return count;
406}
407
408#define show_fan_offset(offset) \
8627f9ba 409static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
410{ \
411 return show_fan(dev, buf, offset - 1); \
412} \
8627f9ba 413static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
414{ \
415 return show_fan_min(dev, buf, offset - 1); \
416} \
8627f9ba 417static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
418{ \
419 return show_fan_div(dev, buf, offset - 1); \
420} \
8627f9ba 421static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
1da177e4
LT
422 const char *buf, size_t count) \
423{ \
424 return set_fan_min(dev, buf, count, offset - 1); \
425} \
426static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
427static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
428 show_fan_##offset##_min, set_fan_##offset##_min);
429
8627f9ba 430static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
431 size_t count)
432{
433 return set_fan_div(dev, buf, count, 0) ;
434}
435
8627f9ba 436static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
437 size_t count)
438{
439 return set_fan_div(dev, buf, count, 1) ;
440}
441
442show_fan_offset(1);
443show_fan_offset(2);
444show_fan_offset(3);
445
446/* Fan 3 divisor is locked in H/W */
447static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
448 show_fan_1_div, set_fan_1_div);
449static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
450 show_fan_2_div, set_fan_2_div);
451static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
452
453/* VID */
8627f9ba 454static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
455{
456 struct lm78_data *data = lm78_update_device(dev);
d0d3cd69 457 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
1da177e4
LT
458}
459static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
460
461/* Alarms */
8627f9ba 462static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
463{
464 struct lm78_data *data = lm78_update_device(dev);
465 return sprintf(buf, "%u\n", data->alarms);
466}
467static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
468
469/* This function is called when:
470 * lm78_driver is inserted (when this module is loaded), for each
471 available adapter
472 * when a new adapter is inserted (and lm78_driver is still present) */
473static int lm78_attach_adapter(struct i2c_adapter *adapter)
474{
475 if (!(adapter->class & I2C_CLASS_HWMON))
476 return 0;
2ed2dc3c 477 return i2c_probe(adapter, &addr_data, lm78_detect);
1da177e4
LT
478}
479
2d8672c5
JD
480static int lm78_isa_attach_adapter(struct i2c_adapter *adapter)
481{
482 return lm78_detect(adapter, isa_address, -1);
483}
484
2ed2dc3c 485/* This function is called by i2c_probe */
d8d20615 486static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
1da177e4
LT
487{
488 int i, err;
489 struct i2c_client *new_client;
490 struct lm78_data *data;
491 const char *client_name = "";
492 int is_isa = i2c_is_isa_adapter(adapter);
493
494 if (!is_isa &&
495 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
496 err = -ENODEV;
497 goto ERROR0;
498 }
499
500 /* Reserve the ISA region */
501 if (is_isa)
fde09509 502 if (!request_region(address, LM78_EXTENT,
cdaf7934 503 lm78_isa_driver.driver.name)) {
1da177e4
LT
504 err = -EBUSY;
505 goto ERROR0;
506 }
507
508 /* Probe whether there is anything available on this address. Already
509 done for SMBus clients */
510 if (kind < 0) {
511 if (is_isa) {
512
513#define REALLY_SLOW_IO
514 /* We need the timeouts for at least some LM78-like
515 chips. But only if we read 'undefined' registers. */
516 i = inb_p(address + 1);
517 if (inb_p(address + 2) != i) {
518 err = -ENODEV;
519 goto ERROR1;
520 }
521 if (inb_p(address + 3) != i) {
522 err = -ENODEV;
523 goto ERROR1;
524 }
525 if (inb_p(address + 7) != i) {
526 err = -ENODEV;
527 goto ERROR1;
528 }
529#undef REALLY_SLOW_IO
530
531 /* Let's just hope nothing breaks here */
532 i = inb_p(address + 5) & 0x7f;
533 outb_p(~i & 0x7f, address + 5);
534 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
535 outb_p(i, address + 5);
536 err = -ENODEV;
537 goto ERROR1;
538 }
539 }
540 }
541
542 /* OK. For now, we presume we have a valid client. We now create the
543 client structure, even though we cannot fill it completely yet.
544 But it allows us to access lm78_{read,write}_value. */
545
ba9c2e8d 546 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
1da177e4
LT
547 err = -ENOMEM;
548 goto ERROR1;
549 }
1da177e4
LT
550
551 new_client = &data->client;
552 if (is_isa)
9a61bf63 553 mutex_init(&data->lock);
1da177e4
LT
554 i2c_set_clientdata(new_client, data);
555 new_client->addr = address;
556 new_client->adapter = adapter;
fde09509 557 new_client->driver = is_isa ? &lm78_isa_driver : &lm78_driver;
1da177e4
LT
558 new_client->flags = 0;
559
560 /* Now, we do the remaining detection. */
561 if (kind < 0) {
562 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
563 err = -ENODEV;
564 goto ERROR2;
565 }
566 if (!is_isa && (lm78_read_value(
567 new_client, LM78_REG_I2C_ADDR) != address)) {
568 err = -ENODEV;
569 goto ERROR2;
570 }
571 }
572
573 /* Determine the chip type. */
574 if (kind <= 0) {
575 i = lm78_read_value(new_client, LM78_REG_CHIPID);
27fe048e
JD
576 if (i == 0x00 || i == 0x20 /* LM78 */
577 || i == 0x40) /* LM78-J */
1da177e4 578 kind = lm78;
1da177e4
LT
579 else if ((i & 0xfe) == 0xc0)
580 kind = lm79;
581 else {
582 if (kind == 0)
583 dev_warn(&adapter->dev, "Ignoring 'force' "
584 "parameter for unknown chip at "
585 "adapter %d, address 0x%02x\n",
586 i2c_adapter_id(adapter), address);
587 err = -ENODEV;
588 goto ERROR2;
589 }
590 }
591
592 if (kind == lm78) {
593 client_name = "lm78";
1da177e4
LT
594 } else if (kind == lm79) {
595 client_name = "lm79";
596 }
597
598 /* Fill in the remaining client fields and put into the global list */
599 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
600 data->type = kind;
601
602 data->valid = 0;
9a61bf63 603 mutex_init(&data->update_lock);
1da177e4
LT
604
605 /* Tell the I2C layer a new client has arrived */
606 if ((err = i2c_attach_client(new_client)))
607 goto ERROR2;
608
609 /* Initialize the LM78 chip */
610 lm78_init_client(new_client);
611
612 /* A few vars need to be filled upon startup */
613 for (i = 0; i < 3; i++) {
614 data->fan_min[i] = lm78_read_value(new_client,
615 LM78_REG_FAN_MIN(i));
616 }
617
618 /* Register sysfs hooks */
943b0830
MH
619 data->class_dev = hwmon_device_register(&new_client->dev);
620 if (IS_ERR(data->class_dev)) {
621 err = PTR_ERR(data->class_dev);
622 goto ERROR3;
623 }
624
1da177e4
LT
625 device_create_file(&new_client->dev, &dev_attr_in0_input);
626 device_create_file(&new_client->dev, &dev_attr_in0_min);
627 device_create_file(&new_client->dev, &dev_attr_in0_max);
628 device_create_file(&new_client->dev, &dev_attr_in1_input);
629 device_create_file(&new_client->dev, &dev_attr_in1_min);
630 device_create_file(&new_client->dev, &dev_attr_in1_max);
631 device_create_file(&new_client->dev, &dev_attr_in2_input);
632 device_create_file(&new_client->dev, &dev_attr_in2_min);
633 device_create_file(&new_client->dev, &dev_attr_in2_max);
634 device_create_file(&new_client->dev, &dev_attr_in3_input);
635 device_create_file(&new_client->dev, &dev_attr_in3_min);
636 device_create_file(&new_client->dev, &dev_attr_in3_max);
637 device_create_file(&new_client->dev, &dev_attr_in4_input);
638 device_create_file(&new_client->dev, &dev_attr_in4_min);
639 device_create_file(&new_client->dev, &dev_attr_in4_max);
640 device_create_file(&new_client->dev, &dev_attr_in5_input);
641 device_create_file(&new_client->dev, &dev_attr_in5_min);
642 device_create_file(&new_client->dev, &dev_attr_in5_max);
643 device_create_file(&new_client->dev, &dev_attr_in6_input);
644 device_create_file(&new_client->dev, &dev_attr_in6_min);
645 device_create_file(&new_client->dev, &dev_attr_in6_max);
646 device_create_file(&new_client->dev, &dev_attr_temp1_input);
647 device_create_file(&new_client->dev, &dev_attr_temp1_max);
648 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
649 device_create_file(&new_client->dev, &dev_attr_fan1_input);
650 device_create_file(&new_client->dev, &dev_attr_fan1_min);
651 device_create_file(&new_client->dev, &dev_attr_fan1_div);
652 device_create_file(&new_client->dev, &dev_attr_fan2_input);
653 device_create_file(&new_client->dev, &dev_attr_fan2_min);
654 device_create_file(&new_client->dev, &dev_attr_fan2_div);
655 device_create_file(&new_client->dev, &dev_attr_fan3_input);
656 device_create_file(&new_client->dev, &dev_attr_fan3_min);
657 device_create_file(&new_client->dev, &dev_attr_fan3_div);
658 device_create_file(&new_client->dev, &dev_attr_alarms);
659 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
660
661 return 0;
662
943b0830
MH
663ERROR3:
664 i2c_detach_client(new_client);
1da177e4
LT
665ERROR2:
666 kfree(data);
667ERROR1:
668 if (is_isa)
669 release_region(address, LM78_EXTENT);
670ERROR0:
671 return err;
672}
673
674static int lm78_detach_client(struct i2c_client *client)
675{
943b0830 676 struct lm78_data *data = i2c_get_clientdata(client);
1da177e4
LT
677 int err;
678
943b0830
MH
679 hwmon_device_unregister(data->class_dev);
680
7bef5594 681 if ((err = i2c_detach_client(client)))
1da177e4 682 return err;
1da177e4
LT
683
684 if(i2c_is_isa_client(client))
685 release_region(client->addr, LM78_EXTENT);
686
943b0830 687 kfree(data);
1da177e4
LT
688
689 return 0;
690}
691
44bbe87e 692/* The SMBus locks itself, but ISA access must be locked explicitly!
1da177e4
LT
693 We don't want to lock the whole ISA bus, so we lock each client
694 separately.
695 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
696 would slow down the LM78 access and should not be necessary. */
697static int lm78_read_value(struct i2c_client *client, u8 reg)
698{
699 int res;
700 if (i2c_is_isa_client(client)) {
701 struct lm78_data *data = i2c_get_clientdata(client);
9a61bf63 702 mutex_lock(&data->lock);
1da177e4
LT
703 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
704 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
9a61bf63 705 mutex_unlock(&data->lock);
1da177e4
LT
706 return res;
707 } else
708 return i2c_smbus_read_byte_data(client, reg);
709}
710
44bbe87e 711/* The SMBus locks itself, but ISA access muse be locked explicitly!
1da177e4
LT
712 We don't want to lock the whole ISA bus, so we lock each client
713 separately.
714 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
715 would slow down the LM78 access and should not be necessary.
716 There are some ugly typecasts here, but the good new is - they should
717 nowhere else be necessary! */
718static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
719{
720 if (i2c_is_isa_client(client)) {
721 struct lm78_data *data = i2c_get_clientdata(client);
9a61bf63 722 mutex_lock(&data->lock);
1da177e4
LT
723 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
724 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
9a61bf63 725 mutex_unlock(&data->lock);
1da177e4
LT
726 return 0;
727 } else
728 return i2c_smbus_write_byte_data(client, reg, value);
729}
730
1da177e4
LT
731static void lm78_init_client(struct i2c_client *client)
732{
733 u8 config = lm78_read_value(client, LM78_REG_CONFIG);
734
735 /* Start monitoring */
736 if (!(config & 0x01))
737 lm78_write_value(client, LM78_REG_CONFIG,
738 (config & 0xf7) | 0x01);
739}
740
741static struct lm78_data *lm78_update_device(struct device *dev)
742{
743 struct i2c_client *client = to_i2c_client(dev);
744 struct lm78_data *data = i2c_get_clientdata(client);
745 int i;
746
9a61bf63 747 mutex_lock(&data->update_lock);
1da177e4
LT
748
749 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
750 || !data->valid) {
751
752 dev_dbg(&client->dev, "Starting lm78 update\n");
753
754 for (i = 0; i <= 6; i++) {
755 data->in[i] =
756 lm78_read_value(client, LM78_REG_IN(i));
757 data->in_min[i] =
758 lm78_read_value(client, LM78_REG_IN_MIN(i));
759 data->in_max[i] =
760 lm78_read_value(client, LM78_REG_IN_MAX(i));
761 }
762 for (i = 0; i < 3; i++) {
763 data->fan[i] =
764 lm78_read_value(client, LM78_REG_FAN(i));
765 data->fan_min[i] =
766 lm78_read_value(client, LM78_REG_FAN_MIN(i));
767 }
768 data->temp = lm78_read_value(client, LM78_REG_TEMP);
769 data->temp_over =
770 lm78_read_value(client, LM78_REG_TEMP_OVER);
771 data->temp_hyst =
772 lm78_read_value(client, LM78_REG_TEMP_HYST);
773 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
774 data->vid = i & 0x0f;
775 if (data->type == lm79)
776 data->vid |=
777 (lm78_read_value(client, LM78_REG_CHIPID) &
778 0x01) << 4;
779 else
780 data->vid |= 0x10;
781 data->fan_div[0] = (i >> 4) & 0x03;
782 data->fan_div[1] = i >> 6;
783 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
784 (lm78_read_value(client, LM78_REG_ALARM2) << 8);
785 data->last_updated = jiffies;
786 data->valid = 1;
787
788 data->fan_div[2] = 1;
789 }
790
9a61bf63 791 mutex_unlock(&data->update_lock);
1da177e4
LT
792
793 return data;
794}
795
796static int __init sm_lm78_init(void)
797{
fde09509
JD
798 int res;
799
800 res = i2c_add_driver(&lm78_driver);
801 if (res)
802 return res;
803
804 res = i2c_isa_add_driver(&lm78_isa_driver);
805 if (res) {
806 i2c_del_driver(&lm78_driver);
807 return res;
808 }
809
810 return 0;
1da177e4
LT
811}
812
813static void __exit sm_lm78_exit(void)
814{
fde09509 815 i2c_isa_del_driver(&lm78_isa_driver);
1da177e4
LT
816 i2c_del_driver(&lm78_driver);
817}
818
819
820
821MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
27fe048e 822MODULE_DESCRIPTION("LM78/LM79 driver");
1da177e4
LT
823MODULE_LICENSE("GPL");
824
825module_init(sm_lm78_init);
826module_exit(sm_lm78_exit);