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[] = { I2C_CLIENT_ISA_END };
38 static unsigned short isa_address = 0x290;
40 /* Insmod parameters */
41 SENSORS_INSMOD_2(lm78, lm79);
43 /* Many LM78 constants specified below */
45 /* Length of ISA address segment */
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
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))
57 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
58 #define LM78_REG_FAN(nr) (0x28 + (nr))
60 #define LM78_REG_TEMP 0x27
61 #define LM78_REG_TEMP_OVER 0x39
62 #define LM78_REG_TEMP_HYST 0x3a
64 #define LM78_REG_ALARM1 0x41
65 #define LM78_REG_ALARM2 0x42
67 #define LM78_REG_VID_FANDIV 0x47
69 #define LM78_REG_CONFIG 0x40
70 #define LM78_REG_CHIPID 0x49
71 #define LM78_REG_I2C_ADDR 0x48
74 /* Conversions. Rounding and limit checking is only done on the TO_REG
77 /* IN: mV, (0V to 4.08V)
79 static inline u8 IN_TO_REG(unsigned long val)
81 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
82 return (nval + 8) / 16;
84 #define IN_FROM_REG(val) ((val) * 16)
86 static inline u8 FAN_TO_REG(long rpm, int div)
90 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
93 static inline int FAN_FROM_REG(u8 val, int div)
95 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
98 /* TEMP: mC (-128C to +127C)
99 REG: 1C/bit, two's complement */
100 static inline s8 TEMP_TO_REG(int val)
102 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
103 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
106 static inline int TEMP_FROM_REG(s8 val)
112 REG: (see doc/vid) */
113 static inline int VID_FROM_REG(u8 val)
115 return val==0x1f ? 0 : val>=0x10 ? 5100-val*100 : 2050-val*50;
118 #define DIV_FROM_REG(val) (1 << (val))
120 /* There are some complications in a module like this. First off, LM78 chips
121 may be both present on the SMBus and the ISA bus, and we have to handle
122 those cases separately at some places. Second, there might be several
123 LM78 chips available (well, actually, that is probably never done; but
124 it is a clean illustration of how to handle a case like that). Finally,
125 a specific chip may be attached to *both* ISA and SMBus, and we would
126 not like to detect it double. Fortunately, in the case of the LM78 at
127 least, a register tells us what SMBus address we are on, so that helps
128 a bit - except if there could be more than one SMBus. Groan. No solution
131 /* This module may seem overly long and complicated. In fact, it is not so
132 bad. Quite a lot of bookkeeping is done. A real driver can often cut
135 /* For each registered LM78, we need to keep some data in memory. That
136 data is pointed to by lm78_list[NR]->data. The structure itself is
137 dynamically allocated, at the same time when a new lm78 client is
140 struct i2c_client client;
141 struct class_device *class_dev;
142 struct semaphore lock;
145 struct semaphore update_lock;
146 char valid; /* !=0 if following fields are valid */
147 unsigned long last_updated; /* In jiffies */
149 u8 in[7]; /* Register value */
150 u8 in_max[7]; /* Register value */
151 u8 in_min[7]; /* Register value */
152 u8 fan[3]; /* Register value */
153 u8 fan_min[3]; /* Register value */
154 s8 temp; /* Register value */
155 s8 temp_over; /* Register value */
156 s8 temp_hyst; /* Register value */
157 u8 fan_div[3]; /* Register encoding, shifted right */
158 u8 vid; /* Register encoding, combined */
159 u16 alarms; /* Register encoding, combined */
163 static int lm78_attach_adapter(struct i2c_adapter *adapter);
164 static int lm78_isa_attach_adapter(struct i2c_adapter *adapter);
165 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
166 static int lm78_detach_client(struct i2c_client *client);
168 static int lm78_read_value(struct i2c_client *client, u8 register);
169 static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
170 static struct lm78_data *lm78_update_device(struct device *dev);
171 static void lm78_init_client(struct i2c_client *client);
174 static struct i2c_driver lm78_driver = {
175 .owner = THIS_MODULE,
177 .id = I2C_DRIVERID_LM78,
178 .flags = I2C_DF_NOTIFY,
179 .attach_adapter = lm78_attach_adapter,
180 .detach_client = lm78_detach_client,
183 static struct i2c_driver lm78_isa_driver = {
184 .owner = THIS_MODULE,
186 .attach_adapter = lm78_isa_attach_adapter,
187 .detach_client = lm78_detach_client,
192 static ssize_t show_in(struct device *dev, char *buf, int nr)
194 struct lm78_data *data = lm78_update_device(dev);
195 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
198 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
200 struct lm78_data *data = lm78_update_device(dev);
201 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
204 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
206 struct lm78_data *data = lm78_update_device(dev);
207 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
210 static ssize_t set_in_min(struct device *dev, const char *buf,
211 size_t count, int nr)
213 struct i2c_client *client = to_i2c_client(dev);
214 struct lm78_data *data = i2c_get_clientdata(client);
215 unsigned long val = simple_strtoul(buf, NULL, 10);
217 down(&data->update_lock);
218 data->in_min[nr] = IN_TO_REG(val);
219 lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
220 up(&data->update_lock);
224 static ssize_t set_in_max(struct device *dev, const char *buf,
225 size_t count, int nr)
227 struct i2c_client *client = to_i2c_client(dev);
228 struct lm78_data *data = i2c_get_clientdata(client);
229 unsigned long val = simple_strtoul(buf, NULL, 10);
231 down(&data->update_lock);
232 data->in_max[nr] = IN_TO_REG(val);
233 lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
234 up(&data->update_lock);
238 #define show_in_offset(offset) \
240 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
242 return show_in(dev, buf, offset); \
244 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
245 show_in##offset, NULL); \
247 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
249 return show_in_min(dev, buf, offset); \
252 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
254 return show_in_max(dev, buf, offset); \
256 static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
257 const char *buf, size_t count) \
259 return set_in_min(dev, buf, count, offset); \
261 static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
262 const char *buf, size_t count) \
264 return set_in_max(dev, buf, count, offset); \
266 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
267 show_in##offset##_min, set_in##offset##_min); \
268 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
269 show_in##offset##_max, set_in##offset##_max);
280 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
282 struct lm78_data *data = lm78_update_device(dev);
283 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
286 static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
288 struct lm78_data *data = lm78_update_device(dev);
289 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
292 static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
294 struct i2c_client *client = to_i2c_client(dev);
295 struct lm78_data *data = i2c_get_clientdata(client);
296 long val = simple_strtol(buf, NULL, 10);
298 down(&data->update_lock);
299 data->temp_over = TEMP_TO_REG(val);
300 lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
301 up(&data->update_lock);
305 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
307 struct lm78_data *data = lm78_update_device(dev);
308 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
311 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
313 struct i2c_client *client = to_i2c_client(dev);
314 struct lm78_data *data = i2c_get_clientdata(client);
315 long val = simple_strtol(buf, NULL, 10);
317 down(&data->update_lock);
318 data->temp_hyst = TEMP_TO_REG(val);
319 lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
320 up(&data->update_lock);
324 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
325 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
326 show_temp_over, set_temp_over);
327 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
328 show_temp_hyst, set_temp_hyst);
331 static ssize_t show_fan(struct device *dev, char *buf, int nr)
333 struct lm78_data *data = lm78_update_device(dev);
334 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
335 DIV_FROM_REG(data->fan_div[nr])) );
338 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
340 struct lm78_data *data = lm78_update_device(dev);
341 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
342 DIV_FROM_REG(data->fan_div[nr])) );
345 static ssize_t set_fan_min(struct device *dev, const char *buf,
346 size_t count, int nr)
348 struct i2c_client *client = to_i2c_client(dev);
349 struct lm78_data *data = i2c_get_clientdata(client);
350 unsigned long val = simple_strtoul(buf, NULL, 10);
352 down(&data->update_lock);
353 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
354 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
355 up(&data->update_lock);
359 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
361 struct lm78_data *data = lm78_update_device(dev);
362 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
365 /* Note: we save and restore the fan minimum here, because its value is
366 determined in part by the fan divisor. This follows the principle of
367 least suprise; the user doesn't expect the fan minimum to change just
368 because the divisor changed. */
369 static ssize_t set_fan_div(struct device *dev, const char *buf,
370 size_t count, int nr)
372 struct i2c_client *client = to_i2c_client(dev);
373 struct lm78_data *data = i2c_get_clientdata(client);
374 unsigned long val = simple_strtoul(buf, NULL, 10);
378 down(&data->update_lock);
379 min = FAN_FROM_REG(data->fan_min[nr],
380 DIV_FROM_REG(data->fan_div[nr]));
383 case 1: data->fan_div[nr] = 0; break;
384 case 2: data->fan_div[nr] = 1; break;
385 case 4: data->fan_div[nr] = 2; break;
386 case 8: data->fan_div[nr] = 3; break;
388 dev_err(&client->dev, "fan_div value %ld not "
389 "supported. Choose one of 1, 2, 4 or 8!\n", val);
390 up(&data->update_lock);
394 reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
397 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
400 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
403 lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
406 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
407 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
408 up(&data->update_lock);
413 #define show_fan_offset(offset) \
414 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
416 return show_fan(dev, buf, offset - 1); \
418 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
420 return show_fan_min(dev, buf, offset - 1); \
422 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
424 return show_fan_div(dev, buf, offset - 1); \
426 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
427 const char *buf, size_t count) \
429 return set_fan_min(dev, buf, count, offset - 1); \
431 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
432 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
433 show_fan_##offset##_min, set_fan_##offset##_min);
435 static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
438 return set_fan_div(dev, buf, count, 0) ;
441 static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
444 return set_fan_div(dev, buf, count, 1) ;
451 /* Fan 3 divisor is locked in H/W */
452 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
453 show_fan_1_div, set_fan_1_div);
454 static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
455 show_fan_2_div, set_fan_2_div);
456 static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
459 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
461 struct lm78_data *data = lm78_update_device(dev);
462 return sprintf(buf, "%d\n", VID_FROM_REG(data->vid));
464 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
467 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
469 struct lm78_data *data = lm78_update_device(dev);
470 return sprintf(buf, "%u\n", data->alarms);
472 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
474 /* This function is called when:
475 * lm78_driver is inserted (when this module is loaded), for each
477 * when a new adapter is inserted (and lm78_driver is still present) */
478 static int lm78_attach_adapter(struct i2c_adapter *adapter)
480 if (!(adapter->class & I2C_CLASS_HWMON))
482 return i2c_detect(adapter, &addr_data, lm78_detect);
485 static int lm78_isa_attach_adapter(struct i2c_adapter *adapter)
487 return lm78_detect(adapter, isa_address, -1);
490 /* This function is called by i2c_detect */
491 int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
494 struct i2c_client *new_client;
495 struct lm78_data *data;
496 const char *client_name = "";
497 int is_isa = i2c_is_isa_adapter(adapter);
500 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
505 /* Reserve the ISA region */
507 if (!request_region(address, LM78_EXTENT,
508 lm78_isa_driver.name)) {
513 /* Probe whether there is anything available on this address. Already
514 done for SMBus clients */
518 #define REALLY_SLOW_IO
519 /* We need the timeouts for at least some LM78-like
520 chips. But only if we read 'undefined' registers. */
521 i = inb_p(address + 1);
522 if (inb_p(address + 2) != i) {
526 if (inb_p(address + 3) != i) {
530 if (inb_p(address + 7) != i) {
534 #undef REALLY_SLOW_IO
536 /* Let's just hope nothing breaks here */
537 i = inb_p(address + 5) & 0x7f;
538 outb_p(~i & 0x7f, address + 5);
539 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
540 outb_p(i, address + 5);
547 /* OK. For now, we presume we have a valid client. We now create the
548 client structure, even though we cannot fill it completely yet.
549 But it allows us to access lm78_{read,write}_value. */
551 if (!(data = kmalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
555 memset(data, 0, sizeof(struct lm78_data));
557 new_client = &data->client;
559 init_MUTEX(&data->lock);
560 i2c_set_clientdata(new_client, data);
561 new_client->addr = address;
562 new_client->adapter = adapter;
563 new_client->driver = is_isa ? &lm78_isa_driver : &lm78_driver;
564 new_client->flags = 0;
566 /* Now, we do the remaining detection. */
568 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
572 if (!is_isa && (lm78_read_value(
573 new_client, LM78_REG_I2C_ADDR) != address)) {
579 /* Determine the chip type. */
581 i = lm78_read_value(new_client, LM78_REG_CHIPID);
582 if (i == 0x00 || i == 0x20 /* LM78 */
583 || i == 0x40) /* LM78-J */
585 else if ((i & 0xfe) == 0xc0)
589 dev_warn(&adapter->dev, "Ignoring 'force' "
590 "parameter for unknown chip at "
591 "adapter %d, address 0x%02x\n",
592 i2c_adapter_id(adapter), address);
599 client_name = "lm78";
600 } else if (kind == lm79) {
601 client_name = "lm79";
604 /* Fill in the remaining client fields and put into the global list */
605 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
609 init_MUTEX(&data->update_lock);
611 /* Tell the I2C layer a new client has arrived */
612 if ((err = i2c_attach_client(new_client)))
615 /* Initialize the LM78 chip */
616 lm78_init_client(new_client);
618 /* A few vars need to be filled upon startup */
619 for (i = 0; i < 3; i++) {
620 data->fan_min[i] = lm78_read_value(new_client,
621 LM78_REG_FAN_MIN(i));
624 /* Register sysfs hooks */
625 data->class_dev = hwmon_device_register(&new_client->dev);
626 if (IS_ERR(data->class_dev)) {
627 err = PTR_ERR(data->class_dev);
631 device_create_file(&new_client->dev, &dev_attr_in0_input);
632 device_create_file(&new_client->dev, &dev_attr_in0_min);
633 device_create_file(&new_client->dev, &dev_attr_in0_max);
634 device_create_file(&new_client->dev, &dev_attr_in1_input);
635 device_create_file(&new_client->dev, &dev_attr_in1_min);
636 device_create_file(&new_client->dev, &dev_attr_in1_max);
637 device_create_file(&new_client->dev, &dev_attr_in2_input);
638 device_create_file(&new_client->dev, &dev_attr_in2_min);
639 device_create_file(&new_client->dev, &dev_attr_in2_max);
640 device_create_file(&new_client->dev, &dev_attr_in3_input);
641 device_create_file(&new_client->dev, &dev_attr_in3_min);
642 device_create_file(&new_client->dev, &dev_attr_in3_max);
643 device_create_file(&new_client->dev, &dev_attr_in4_input);
644 device_create_file(&new_client->dev, &dev_attr_in4_min);
645 device_create_file(&new_client->dev, &dev_attr_in4_max);
646 device_create_file(&new_client->dev, &dev_attr_in5_input);
647 device_create_file(&new_client->dev, &dev_attr_in5_min);
648 device_create_file(&new_client->dev, &dev_attr_in5_max);
649 device_create_file(&new_client->dev, &dev_attr_in6_input);
650 device_create_file(&new_client->dev, &dev_attr_in6_min);
651 device_create_file(&new_client->dev, &dev_attr_in6_max);
652 device_create_file(&new_client->dev, &dev_attr_temp1_input);
653 device_create_file(&new_client->dev, &dev_attr_temp1_max);
654 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
655 device_create_file(&new_client->dev, &dev_attr_fan1_input);
656 device_create_file(&new_client->dev, &dev_attr_fan1_min);
657 device_create_file(&new_client->dev, &dev_attr_fan1_div);
658 device_create_file(&new_client->dev, &dev_attr_fan2_input);
659 device_create_file(&new_client->dev, &dev_attr_fan2_min);
660 device_create_file(&new_client->dev, &dev_attr_fan2_div);
661 device_create_file(&new_client->dev, &dev_attr_fan3_input);
662 device_create_file(&new_client->dev, &dev_attr_fan3_min);
663 device_create_file(&new_client->dev, &dev_attr_fan3_div);
664 device_create_file(&new_client->dev, &dev_attr_alarms);
665 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
670 i2c_detach_client(new_client);
675 release_region(address, LM78_EXTENT);
680 static int lm78_detach_client(struct i2c_client *client)
682 struct lm78_data *data = i2c_get_clientdata(client);
685 hwmon_device_unregister(data->class_dev);
687 if ((err = i2c_detach_client(client))) {
688 dev_err(&client->dev,
689 "Client deregistration failed, client not detached.\n");
693 if(i2c_is_isa_client(client))
694 release_region(client->addr, LM78_EXTENT);
701 /* The SMBus locks itself, but ISA access must be locked explicitly!
702 We don't want to lock the whole ISA bus, so we lock each client
704 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
705 would slow down the LM78 access and should not be necessary. */
706 static int lm78_read_value(struct i2c_client *client, u8 reg)
709 if (i2c_is_isa_client(client)) {
710 struct lm78_data *data = i2c_get_clientdata(client);
712 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
713 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
717 return i2c_smbus_read_byte_data(client, reg);
720 /* The SMBus locks itself, but ISA access muse be locked explicitly!
721 We don't want to lock the whole ISA bus, so we lock each client
723 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
724 would slow down the LM78 access and should not be necessary.
725 There are some ugly typecasts here, but the good new is - they should
726 nowhere else be necessary! */
727 static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
729 if (i2c_is_isa_client(client)) {
730 struct lm78_data *data = i2c_get_clientdata(client);
732 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
733 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
737 return i2c_smbus_write_byte_data(client, reg, value);
740 /* Called when we have found a new LM78. It should set limits, etc. */
741 static void lm78_init_client(struct i2c_client *client)
743 u8 config = lm78_read_value(client, LM78_REG_CONFIG);
745 /* Start monitoring */
746 if (!(config & 0x01))
747 lm78_write_value(client, LM78_REG_CONFIG,
748 (config & 0xf7) | 0x01);
751 static struct lm78_data *lm78_update_device(struct device *dev)
753 struct i2c_client *client = to_i2c_client(dev);
754 struct lm78_data *data = i2c_get_clientdata(client);
757 down(&data->update_lock);
759 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
762 dev_dbg(&client->dev, "Starting lm78 update\n");
764 for (i = 0; i <= 6; i++) {
766 lm78_read_value(client, LM78_REG_IN(i));
768 lm78_read_value(client, LM78_REG_IN_MIN(i));
770 lm78_read_value(client, LM78_REG_IN_MAX(i));
772 for (i = 0; i < 3; i++) {
774 lm78_read_value(client, LM78_REG_FAN(i));
776 lm78_read_value(client, LM78_REG_FAN_MIN(i));
778 data->temp = lm78_read_value(client, LM78_REG_TEMP);
780 lm78_read_value(client, LM78_REG_TEMP_OVER);
782 lm78_read_value(client, LM78_REG_TEMP_HYST);
783 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
784 data->vid = i & 0x0f;
785 if (data->type == lm79)
787 (lm78_read_value(client, LM78_REG_CHIPID) &
791 data->fan_div[0] = (i >> 4) & 0x03;
792 data->fan_div[1] = i >> 6;
793 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
794 (lm78_read_value(client, LM78_REG_ALARM2) << 8);
795 data->last_updated = jiffies;
798 data->fan_div[2] = 1;
801 up(&data->update_lock);
806 static int __init sm_lm78_init(void)
810 res = i2c_add_driver(&lm78_driver);
814 res = i2c_isa_add_driver(&lm78_isa_driver);
816 i2c_del_driver(&lm78_driver);
823 static void __exit sm_lm78_exit(void)
825 i2c_isa_del_driver(&lm78_isa_driver);
826 i2c_del_driver(&lm78_driver);
831 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
832 MODULE_DESCRIPTION("LM78/LM79 driver");
833 MODULE_LICENSE("GPL");
835 module_init(sm_lm78_init);
836 module_exit(sm_lm78_exit);