2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
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.
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.
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.
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>
26 #include <linux/i2c-isa.h>
27 #include <linux/i2c-sensor.h>
28 #include <linux/hwmon.h>
29 #include <linux/err.h>
32 /* Addresses to scan */
33 static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24,
34 0x25, 0x26, 0x27, 0x28, 0x29,
35 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
36 0x2f, I2C_CLIENT_END };
37 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
39 /* Insmod parameters */
40 SENSORS_INSMOD_2(lm78, lm79);
42 /* Many LM78 constants specified below */
44 /* Length of ISA address segment */
47 /* Where are the ISA address/data registers relative to the base address */
48 #define LM78_ADDR_REG_OFFSET 5
49 #define LM78_DATA_REG_OFFSET 6
51 /* The LM78 registers */
52 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
53 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
54 #define LM78_REG_IN(nr) (0x20 + (nr))
56 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
57 #define LM78_REG_FAN(nr) (0x28 + (nr))
59 #define LM78_REG_TEMP 0x27
60 #define LM78_REG_TEMP_OVER 0x39
61 #define LM78_REG_TEMP_HYST 0x3a
63 #define LM78_REG_ALARM1 0x41
64 #define LM78_REG_ALARM2 0x42
66 #define LM78_REG_VID_FANDIV 0x47
68 #define LM78_REG_CONFIG 0x40
69 #define LM78_REG_CHIPID 0x49
70 #define LM78_REG_I2C_ADDR 0x48
73 /* Conversions. Rounding and limit checking is only done on the TO_REG
76 /* IN: mV, (0V to 4.08V)
78 static inline u8 IN_TO_REG(unsigned long val)
80 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
81 return (nval + 8) / 16;
83 #define IN_FROM_REG(val) ((val) * 16)
85 static inline u8 FAN_TO_REG(long rpm, int div)
89 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
92 static inline int FAN_FROM_REG(u8 val, int div)
94 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
97 /* TEMP: mC (-128C to +127C)
98 REG: 1C/bit, two's complement */
99 static inline s8 TEMP_TO_REG(int val)
101 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
102 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
105 static inline int TEMP_FROM_REG(s8 val)
111 REG: (see doc/vid) */
112 static inline int VID_FROM_REG(u8 val)
114 return val==0x1f ? 0 : val>=0x10 ? 5100-val*100 : 2050-val*50;
117 #define DIV_FROM_REG(val) (1 << (val))
119 /* There are some complications in a module like this. First off, LM78 chips
120 may be both present on the SMBus and the ISA bus, and we have to handle
121 those cases separately at some places. Second, there might be several
122 LM78 chips available (well, actually, that is probably never done; but
123 it is a clean illustration of how to handle a case like that). Finally,
124 a specific chip may be attached to *both* ISA and SMBus, and we would
125 not like to detect it double. Fortunately, in the case of the LM78 at
126 least, a register tells us what SMBus address we are on, so that helps
127 a bit - except if there could be more than one SMBus. Groan. No solution
130 /* This module may seem overly long and complicated. In fact, it is not so
131 bad. Quite a lot of bookkeeping is done. A real driver can often cut
134 /* For each registered LM78, we need to keep some data in memory. That
135 data is pointed to by lm78_list[NR]->data. The structure itself is
136 dynamically allocated, at the same time when a new lm78 client is
139 struct i2c_client client;
140 struct class_device *class_dev;
141 struct semaphore lock;
144 struct semaphore update_lock;
145 char valid; /* !=0 if following fields are valid */
146 unsigned long last_updated; /* In jiffies */
148 u8 in[7]; /* Register value */
149 u8 in_max[7]; /* Register value */
150 u8 in_min[7]; /* Register value */
151 u8 fan[3]; /* Register value */
152 u8 fan_min[3]; /* Register value */
153 s8 temp; /* Register value */
154 s8 temp_over; /* Register value */
155 s8 temp_hyst; /* Register value */
156 u8 fan_div[3]; /* Register encoding, shifted right */
157 u8 vid; /* Register encoding, combined */
158 u16 alarms; /* Register encoding, combined */
162 static int lm78_attach_adapter(struct i2c_adapter *adapter);
163 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
164 static int lm78_detach_client(struct i2c_client *client);
166 static int lm78_read_value(struct i2c_client *client, u8 register);
167 static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
168 static struct lm78_data *lm78_update_device(struct device *dev);
169 static void lm78_init_client(struct i2c_client *client);
172 static struct i2c_driver lm78_driver = {
173 .owner = THIS_MODULE,
175 .id = I2C_DRIVERID_LM78,
176 .flags = I2C_DF_NOTIFY,
177 .attach_adapter = lm78_attach_adapter,
178 .detach_client = lm78_detach_client,
181 static struct i2c_driver lm78_isa_driver = {
182 .owner = THIS_MODULE,
184 .attach_adapter = lm78_attach_adapter,
185 .detach_client = lm78_detach_client,
190 static ssize_t show_in(struct device *dev, char *buf, int nr)
192 struct lm78_data *data = lm78_update_device(dev);
193 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
196 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
198 struct lm78_data *data = lm78_update_device(dev);
199 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
202 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
204 struct lm78_data *data = lm78_update_device(dev);
205 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
208 static ssize_t set_in_min(struct device *dev, const char *buf,
209 size_t count, int nr)
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);
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);
222 static ssize_t set_in_max(struct device *dev, const char *buf,
223 size_t count, int nr)
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);
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);
236 #define show_in_offset(offset) \
238 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
240 return show_in(dev, buf, offset); \
242 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
243 show_in##offset, NULL); \
245 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
247 return show_in_min(dev, buf, offset); \
250 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
252 return show_in_max(dev, buf, offset); \
254 static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
255 const char *buf, size_t count) \
257 return set_in_min(dev, buf, count, offset); \
259 static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
260 const char *buf, size_t count) \
262 return set_in_max(dev, buf, count, offset); \
264 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
265 show_in##offset##_min, set_in##offset##_min); \
266 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
267 show_in##offset##_max, set_in##offset##_max);
278 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
280 struct lm78_data *data = lm78_update_device(dev);
281 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
284 static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
286 struct lm78_data *data = lm78_update_device(dev);
287 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
290 static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
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);
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);
303 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
305 struct lm78_data *data = lm78_update_device(dev);
306 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
309 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
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);
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);
322 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
323 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
324 show_temp_over, set_temp_over);
325 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
326 show_temp_hyst, set_temp_hyst);
329 static ssize_t show_fan(struct device *dev, char *buf, int nr)
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])) );
336 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
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])) );
343 static ssize_t set_fan_min(struct device *dev, const char *buf,
344 size_t count, int nr)
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);
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);
357 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
359 struct lm78_data *data = lm78_update_device(dev);
360 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
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. */
367 static ssize_t set_fan_div(struct device *dev, const char *buf,
368 size_t count, int nr)
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);
376 down(&data->update_lock);
377 min = FAN_FROM_REG(data->fan_min[nr],
378 DIV_FROM_REG(data->fan_div[nr]));
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;
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);
392 reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
395 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
398 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
401 lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
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);
411 #define show_fan_offset(offset) \
412 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
414 return show_fan(dev, buf, offset - 1); \
416 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
418 return show_fan_min(dev, buf, offset - 1); \
420 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
422 return show_fan_div(dev, buf, offset - 1); \
424 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
425 const char *buf, size_t count) \
427 return set_fan_min(dev, buf, count, offset - 1); \
429 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
430 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
431 show_fan_##offset##_min, set_fan_##offset##_min);
433 static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
436 return set_fan_div(dev, buf, count, 0) ;
439 static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
442 return set_fan_div(dev, buf, count, 1) ;
449 /* Fan 3 divisor is locked in H/W */
450 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
451 show_fan_1_div, set_fan_1_div);
452 static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
453 show_fan_2_div, set_fan_2_div);
454 static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
457 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
459 struct lm78_data *data = lm78_update_device(dev);
460 return sprintf(buf, "%d\n", VID_FROM_REG(data->vid));
462 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
465 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
467 struct lm78_data *data = lm78_update_device(dev);
468 return sprintf(buf, "%u\n", data->alarms);
470 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
472 /* This function is called when:
473 * lm78_driver is inserted (when this module is loaded), for each
475 * when a new adapter is inserted (and lm78_driver is still present) */
476 static int lm78_attach_adapter(struct i2c_adapter *adapter)
478 if (!(adapter->class & I2C_CLASS_HWMON))
480 return i2c_detect(adapter, &addr_data, lm78_detect);
483 /* This function is called by i2c_detect */
484 int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
487 struct i2c_client *new_client;
488 struct lm78_data *data;
489 const char *client_name = "";
490 int is_isa = i2c_is_isa_adapter(adapter);
493 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
498 /* Reserve the ISA region */
500 if (!request_region(address, LM78_EXTENT,
501 lm78_isa_driver.name)) {
506 /* Probe whether there is anything available on this address. Already
507 done for SMBus clients */
511 #define REALLY_SLOW_IO
512 /* We need the timeouts for at least some LM78-like
513 chips. But only if we read 'undefined' registers. */
514 i = inb_p(address + 1);
515 if (inb_p(address + 2) != i) {
519 if (inb_p(address + 3) != i) {
523 if (inb_p(address + 7) != i) {
527 #undef REALLY_SLOW_IO
529 /* Let's just hope nothing breaks here */
530 i = inb_p(address + 5) & 0x7f;
531 outb_p(~i & 0x7f, address + 5);
532 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
533 outb_p(i, address + 5);
540 /* OK. For now, we presume we have a valid client. We now create the
541 client structure, even though we cannot fill it completely yet.
542 But it allows us to access lm78_{read,write}_value. */
544 if (!(data = kmalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
548 memset(data, 0, sizeof(struct lm78_data));
550 new_client = &data->client;
552 init_MUTEX(&data->lock);
553 i2c_set_clientdata(new_client, data);
554 new_client->addr = address;
555 new_client->adapter = adapter;
556 new_client->driver = is_isa ? &lm78_isa_driver : &lm78_driver;
557 new_client->flags = 0;
559 /* Now, we do the remaining detection. */
561 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
565 if (!is_isa && (lm78_read_value(
566 new_client, LM78_REG_I2C_ADDR) != address)) {
572 /* Determine the chip type. */
574 i = lm78_read_value(new_client, LM78_REG_CHIPID);
575 if (i == 0x00 || i == 0x20 /* LM78 */
576 || i == 0x40) /* LM78-J */
578 else if ((i & 0xfe) == 0xc0)
582 dev_warn(&adapter->dev, "Ignoring 'force' "
583 "parameter for unknown chip at "
584 "adapter %d, address 0x%02x\n",
585 i2c_adapter_id(adapter), address);
592 client_name = "lm78";
593 } else if (kind == lm79) {
594 client_name = "lm79";
597 /* Fill in the remaining client fields and put into the global list */
598 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
602 init_MUTEX(&data->update_lock);
604 /* Tell the I2C layer a new client has arrived */
605 if ((err = i2c_attach_client(new_client)))
608 /* Initialize the LM78 chip */
609 lm78_init_client(new_client);
611 /* A few vars need to be filled upon startup */
612 for (i = 0; i < 3; i++) {
613 data->fan_min[i] = lm78_read_value(new_client,
614 LM78_REG_FAN_MIN(i));
617 /* Register sysfs hooks */
618 data->class_dev = hwmon_device_register(&new_client->dev);
619 if (IS_ERR(data->class_dev)) {
620 err = PTR_ERR(data->class_dev);
624 device_create_file(&new_client->dev, &dev_attr_in0_input);
625 device_create_file(&new_client->dev, &dev_attr_in0_min);
626 device_create_file(&new_client->dev, &dev_attr_in0_max);
627 device_create_file(&new_client->dev, &dev_attr_in1_input);
628 device_create_file(&new_client->dev, &dev_attr_in1_min);
629 device_create_file(&new_client->dev, &dev_attr_in1_max);
630 device_create_file(&new_client->dev, &dev_attr_in2_input);
631 device_create_file(&new_client->dev, &dev_attr_in2_min);
632 device_create_file(&new_client->dev, &dev_attr_in2_max);
633 device_create_file(&new_client->dev, &dev_attr_in3_input);
634 device_create_file(&new_client->dev, &dev_attr_in3_min);
635 device_create_file(&new_client->dev, &dev_attr_in3_max);
636 device_create_file(&new_client->dev, &dev_attr_in4_input);
637 device_create_file(&new_client->dev, &dev_attr_in4_min);
638 device_create_file(&new_client->dev, &dev_attr_in4_max);
639 device_create_file(&new_client->dev, &dev_attr_in5_input);
640 device_create_file(&new_client->dev, &dev_attr_in5_min);
641 device_create_file(&new_client->dev, &dev_attr_in5_max);
642 device_create_file(&new_client->dev, &dev_attr_in6_input);
643 device_create_file(&new_client->dev, &dev_attr_in6_min);
644 device_create_file(&new_client->dev, &dev_attr_in6_max);
645 device_create_file(&new_client->dev, &dev_attr_temp1_input);
646 device_create_file(&new_client->dev, &dev_attr_temp1_max);
647 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
648 device_create_file(&new_client->dev, &dev_attr_fan1_input);
649 device_create_file(&new_client->dev, &dev_attr_fan1_min);
650 device_create_file(&new_client->dev, &dev_attr_fan1_div);
651 device_create_file(&new_client->dev, &dev_attr_fan2_input);
652 device_create_file(&new_client->dev, &dev_attr_fan2_min);
653 device_create_file(&new_client->dev, &dev_attr_fan2_div);
654 device_create_file(&new_client->dev, &dev_attr_fan3_input);
655 device_create_file(&new_client->dev, &dev_attr_fan3_min);
656 device_create_file(&new_client->dev, &dev_attr_fan3_div);
657 device_create_file(&new_client->dev, &dev_attr_alarms);
658 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
663 i2c_detach_client(new_client);
668 release_region(address, LM78_EXTENT);
673 static int lm78_detach_client(struct i2c_client *client)
675 struct lm78_data *data = i2c_get_clientdata(client);
678 hwmon_device_unregister(data->class_dev);
680 if ((err = i2c_detach_client(client))) {
681 dev_err(&client->dev,
682 "Client deregistration failed, client not detached.\n");
686 if(i2c_is_isa_client(client))
687 release_region(client->addr, LM78_EXTENT);
694 /* The SMBus locks itself, but ISA access must be locked explicitly!
695 We don't want to lock the whole ISA bus, so we lock each client
697 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
698 would slow down the LM78 access and should not be necessary. */
699 static int lm78_read_value(struct i2c_client *client, u8 reg)
702 if (i2c_is_isa_client(client)) {
703 struct lm78_data *data = i2c_get_clientdata(client);
705 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
706 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
710 return i2c_smbus_read_byte_data(client, reg);
713 /* The SMBus locks itself, but ISA access muse be locked explicitly!
714 We don't want to lock the whole ISA bus, so we lock each client
716 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
717 would slow down the LM78 access and should not be necessary.
718 There are some ugly typecasts here, but the good new is - they should
719 nowhere else be necessary! */
720 static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
722 if (i2c_is_isa_client(client)) {
723 struct lm78_data *data = i2c_get_clientdata(client);
725 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
726 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
730 return i2c_smbus_write_byte_data(client, reg, value);
733 /* Called when we have found a new LM78. It should set limits, etc. */
734 static void lm78_init_client(struct i2c_client *client)
736 u8 config = lm78_read_value(client, LM78_REG_CONFIG);
738 /* Start monitoring */
739 if (!(config & 0x01))
740 lm78_write_value(client, LM78_REG_CONFIG,
741 (config & 0xf7) | 0x01);
744 static struct lm78_data *lm78_update_device(struct device *dev)
746 struct i2c_client *client = to_i2c_client(dev);
747 struct lm78_data *data = i2c_get_clientdata(client);
750 down(&data->update_lock);
752 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
755 dev_dbg(&client->dev, "Starting lm78 update\n");
757 for (i = 0; i <= 6; i++) {
759 lm78_read_value(client, LM78_REG_IN(i));
761 lm78_read_value(client, LM78_REG_IN_MIN(i));
763 lm78_read_value(client, LM78_REG_IN_MAX(i));
765 for (i = 0; i < 3; i++) {
767 lm78_read_value(client, LM78_REG_FAN(i));
769 lm78_read_value(client, LM78_REG_FAN_MIN(i));
771 data->temp = lm78_read_value(client, LM78_REG_TEMP);
773 lm78_read_value(client, LM78_REG_TEMP_OVER);
775 lm78_read_value(client, LM78_REG_TEMP_HYST);
776 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
777 data->vid = i & 0x0f;
778 if (data->type == lm79)
780 (lm78_read_value(client, LM78_REG_CHIPID) &
784 data->fan_div[0] = (i >> 4) & 0x03;
785 data->fan_div[1] = i >> 6;
786 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
787 (lm78_read_value(client, LM78_REG_ALARM2) << 8);
788 data->last_updated = jiffies;
791 data->fan_div[2] = 1;
794 up(&data->update_lock);
799 static int __init sm_lm78_init(void)
803 res = i2c_add_driver(&lm78_driver);
807 res = i2c_isa_add_driver(&lm78_isa_driver);
809 i2c_del_driver(&lm78_driver);
816 static void __exit sm_lm78_exit(void)
818 i2c_isa_del_driver(&lm78_isa_driver);
819 i2c_del_driver(&lm78_driver);
824 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
825 MODULE_DESCRIPTION("LM78/LM79 driver");
826 MODULE_LICENSE("GPL");
828 module_init(sm_lm78_init);
829 module_exit(sm_lm78_exit);