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