2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 Copyright (c) 2007 Jean Delvare <khali@linux-fr.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/jiffies.h>
26 #include <linux/i2c.h>
27 #include <linux/platform_device.h>
28 #include <linux/ioport.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-vid.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
35 /* ISA device, if found */
36 static struct platform_device *pdev;
38 /* Addresses to scan */
39 static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24,
40 0x25, 0x26, 0x27, 0x28, 0x29,
41 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
42 0x2f, I2C_CLIENT_END };
43 static unsigned short isa_address = 0x290;
45 /* Insmod parameters */
46 I2C_CLIENT_INSMOD_2(lm78, lm79);
48 /* Many LM78 constants specified below */
50 /* Length of ISA address segment */
53 /* Where are the ISA address/data registers relative to the base address */
54 #define LM78_ADDR_REG_OFFSET 5
55 #define LM78_DATA_REG_OFFSET 6
57 /* The LM78 registers */
58 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
59 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
60 #define LM78_REG_IN(nr) (0x20 + (nr))
62 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
63 #define LM78_REG_FAN(nr) (0x28 + (nr))
65 #define LM78_REG_TEMP 0x27
66 #define LM78_REG_TEMP_OVER 0x39
67 #define LM78_REG_TEMP_HYST 0x3a
69 #define LM78_REG_ALARM1 0x41
70 #define LM78_REG_ALARM2 0x42
72 #define LM78_REG_VID_FANDIV 0x47
74 #define LM78_REG_CONFIG 0x40
75 #define LM78_REG_CHIPID 0x49
76 #define LM78_REG_I2C_ADDR 0x48
79 /* Conversions. Rounding and limit checking is only done on the TO_REG
82 /* IN: mV, (0V to 4.08V)
84 static inline u8 IN_TO_REG(unsigned long val)
86 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
87 return (nval + 8) / 16;
89 #define IN_FROM_REG(val) ((val) * 16)
91 static inline u8 FAN_TO_REG(long rpm, int div)
95 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
98 static inline int FAN_FROM_REG(u8 val, int div)
100 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
103 /* TEMP: mC (-128C to +127C)
104 REG: 1C/bit, two's complement */
105 static inline s8 TEMP_TO_REG(int val)
107 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
108 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
111 static inline int TEMP_FROM_REG(s8 val)
116 #define DIV_FROM_REG(val) (1 << (val))
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
129 /* For ISA chips, we abuse the i2c_client addr and name fields. We also use
130 the driver field to differentiate between I2C and ISA chips. */
132 struct i2c_client client;
133 struct class_device *class_dev;
137 struct mutex update_lock;
138 char valid; /* !=0 if following fields are valid */
139 unsigned long last_updated; /* In jiffies */
141 u8 in[7]; /* Register value */
142 u8 in_max[7]; /* Register value */
143 u8 in_min[7]; /* Register value */
144 u8 fan[3]; /* Register value */
145 u8 fan_min[3]; /* Register value */
146 s8 temp; /* Register value */
147 s8 temp_over; /* Register value */
148 s8 temp_hyst; /* Register value */
149 u8 fan_div[3]; /* Register encoding, shifted right */
150 u8 vid; /* Register encoding, combined */
151 u16 alarms; /* Register encoding, combined */
155 static int lm78_attach_adapter(struct i2c_adapter *adapter);
156 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
157 static int lm78_detach_client(struct i2c_client *client);
159 static int __devinit lm78_isa_probe(struct platform_device *pdev);
160 static int __devexit lm78_isa_remove(struct platform_device *pdev);
162 static int lm78_read_value(struct lm78_data *data, u8 reg);
163 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
164 static struct lm78_data *lm78_update_device(struct device *dev);
165 static void lm78_init_device(struct lm78_data *data);
168 static struct i2c_driver lm78_driver = {
172 .id = I2C_DRIVERID_LM78,
173 .attach_adapter = lm78_attach_adapter,
174 .detach_client = lm78_detach_client,
177 static struct platform_driver lm78_isa_driver = {
179 .owner = THIS_MODULE,
182 .probe = lm78_isa_probe,
183 .remove = lm78_isa_remove,
188 static ssize_t show_in(struct device *dev, char *buf, int nr)
190 struct lm78_data *data = lm78_update_device(dev);
191 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
194 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
196 struct lm78_data *data = lm78_update_device(dev);
197 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
200 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
202 struct lm78_data *data = lm78_update_device(dev);
203 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
206 static ssize_t set_in_min(struct device *dev, const char *buf,
207 size_t count, int nr)
209 struct lm78_data *data = dev_get_drvdata(dev);
210 unsigned long val = simple_strtoul(buf, NULL, 10);
212 mutex_lock(&data->update_lock);
213 data->in_min[nr] = IN_TO_REG(val);
214 lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
215 mutex_unlock(&data->update_lock);
219 static ssize_t set_in_max(struct device *dev, const char *buf,
220 size_t count, int nr)
222 struct lm78_data *data = dev_get_drvdata(dev);
223 unsigned long val = simple_strtoul(buf, NULL, 10);
225 mutex_lock(&data->update_lock);
226 data->in_max[nr] = IN_TO_REG(val);
227 lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
228 mutex_unlock(&data->update_lock);
232 #define show_in_offset(offset) \
234 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
236 return show_in(dev, buf, offset); \
238 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
239 show_in##offset, NULL); \
241 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
243 return show_in_min(dev, buf, offset); \
246 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
248 return show_in_max(dev, buf, offset); \
250 static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
251 const char *buf, size_t count) \
253 return set_in_min(dev, buf, count, offset); \
255 static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
256 const char *buf, size_t count) \
258 return set_in_max(dev, buf, count, offset); \
260 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
261 show_in##offset##_min, set_in##offset##_min); \
262 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
263 show_in##offset##_max, set_in##offset##_max);
274 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
276 struct lm78_data *data = lm78_update_device(dev);
277 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
280 static ssize_t show_temp_over(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_over));
286 static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
288 struct lm78_data *data = dev_get_drvdata(dev);
289 long val = simple_strtol(buf, NULL, 10);
291 mutex_lock(&data->update_lock);
292 data->temp_over = TEMP_TO_REG(val);
293 lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
294 mutex_unlock(&data->update_lock);
298 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
300 struct lm78_data *data = lm78_update_device(dev);
301 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
304 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
306 struct lm78_data *data = dev_get_drvdata(dev);
307 long val = simple_strtol(buf, NULL, 10);
309 mutex_lock(&data->update_lock);
310 data->temp_hyst = TEMP_TO_REG(val);
311 lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
312 mutex_unlock(&data->update_lock);
316 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
317 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
318 show_temp_over, set_temp_over);
319 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
320 show_temp_hyst, set_temp_hyst);
323 static ssize_t show_fan(struct device *dev, char *buf, int nr)
325 struct lm78_data *data = lm78_update_device(dev);
326 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
327 DIV_FROM_REG(data->fan_div[nr])) );
330 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
332 struct lm78_data *data = lm78_update_device(dev);
333 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
334 DIV_FROM_REG(data->fan_div[nr])) );
337 static ssize_t set_fan_min(struct device *dev, const char *buf,
338 size_t count, int nr)
340 struct lm78_data *data = dev_get_drvdata(dev);
341 unsigned long val = simple_strtoul(buf, NULL, 10);
343 mutex_lock(&data->update_lock);
344 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
345 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
346 mutex_unlock(&data->update_lock);
350 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
352 struct lm78_data *data = lm78_update_device(dev);
353 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
356 /* Note: we save and restore the fan minimum here, because its value is
357 determined in part by the fan divisor. This follows the principle of
358 least surprise; the user doesn't expect the fan minimum to change just
359 because the divisor changed. */
360 static ssize_t set_fan_div(struct device *dev, const char *buf,
361 size_t count, int nr)
363 struct lm78_data *data = dev_get_drvdata(dev);
364 unsigned long val = simple_strtoul(buf, NULL, 10);
368 mutex_lock(&data->update_lock);
369 min = FAN_FROM_REG(data->fan_min[nr],
370 DIV_FROM_REG(data->fan_div[nr]));
373 case 1: data->fan_div[nr] = 0; break;
374 case 2: data->fan_div[nr] = 1; break;
375 case 4: data->fan_div[nr] = 2; break;
376 case 8: data->fan_div[nr] = 3; break;
378 dev_err(dev, "fan_div value %ld not "
379 "supported. Choose one of 1, 2, 4 or 8!\n", val);
380 mutex_unlock(&data->update_lock);
384 reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
387 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
390 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
393 lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
396 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
397 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
398 mutex_unlock(&data->update_lock);
403 #define show_fan_offset(offset) \
404 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
406 return show_fan(dev, buf, offset - 1); \
408 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
410 return show_fan_min(dev, buf, offset - 1); \
412 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
414 return show_fan_div(dev, buf, offset - 1); \
416 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
417 const char *buf, size_t count) \
419 return set_fan_min(dev, buf, count, offset - 1); \
421 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
422 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
423 show_fan_##offset##_min, set_fan_##offset##_min);
425 static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
428 return set_fan_div(dev, buf, count, 0) ;
431 static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
434 return set_fan_div(dev, buf, count, 1) ;
441 /* Fan 3 divisor is locked in H/W */
442 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
443 show_fan_1_div, set_fan_1_div);
444 static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
445 show_fan_2_div, set_fan_2_div);
446 static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
449 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
451 struct lm78_data *data = lm78_update_device(dev);
452 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
454 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
457 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
459 struct lm78_data *data = lm78_update_device(dev);
460 return sprintf(buf, "%u\n", data->alarms);
462 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
464 /* This function is called when:
465 * lm78_driver is inserted (when this module is loaded), for each
467 * when a new adapter is inserted (and lm78_driver is still present) */
468 static int lm78_attach_adapter(struct i2c_adapter *adapter)
470 if (!(adapter->class & I2C_CLASS_HWMON))
472 return i2c_probe(adapter, &addr_data, lm78_detect);
475 static struct attribute *lm78_attributes[] = {
476 &dev_attr_in0_input.attr,
477 &dev_attr_in0_min.attr,
478 &dev_attr_in0_max.attr,
479 &dev_attr_in1_input.attr,
480 &dev_attr_in1_min.attr,
481 &dev_attr_in1_max.attr,
482 &dev_attr_in2_input.attr,
483 &dev_attr_in2_min.attr,
484 &dev_attr_in2_max.attr,
485 &dev_attr_in3_input.attr,
486 &dev_attr_in3_min.attr,
487 &dev_attr_in3_max.attr,
488 &dev_attr_in4_input.attr,
489 &dev_attr_in4_min.attr,
490 &dev_attr_in4_max.attr,
491 &dev_attr_in5_input.attr,
492 &dev_attr_in5_min.attr,
493 &dev_attr_in5_max.attr,
494 &dev_attr_in6_input.attr,
495 &dev_attr_in6_min.attr,
496 &dev_attr_in6_max.attr,
497 &dev_attr_temp1_input.attr,
498 &dev_attr_temp1_max.attr,
499 &dev_attr_temp1_max_hyst.attr,
500 &dev_attr_fan1_input.attr,
501 &dev_attr_fan1_min.attr,
502 &dev_attr_fan1_div.attr,
503 &dev_attr_fan2_input.attr,
504 &dev_attr_fan2_min.attr,
505 &dev_attr_fan2_div.attr,
506 &dev_attr_fan3_input.attr,
507 &dev_attr_fan3_min.attr,
508 &dev_attr_fan3_div.attr,
509 &dev_attr_alarms.attr,
510 &dev_attr_cpu0_vid.attr,
515 static const struct attribute_group lm78_group = {
516 .attrs = lm78_attributes,
519 /* I2C devices get this name attribute automatically, but for ISA devices
520 we must create it by ourselves. */
521 static ssize_t show_name(struct device *dev, struct device_attribute
524 struct lm78_data *data = dev_get_drvdata(dev);
526 return sprintf(buf, "%s\n", data->client.name);
528 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
530 /* This function is called by i2c_probe */
531 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
534 struct i2c_client *new_client;
535 struct lm78_data *data;
536 const char *client_name = "";
538 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
543 /* OK. For now, we presume we have a valid client. We now create the
544 client structure, even though we cannot fill it completely yet.
545 But it allows us to access lm78_{read,write}_value. */
547 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
552 new_client = &data->client;
553 i2c_set_clientdata(new_client, data);
554 new_client->addr = address;
555 new_client->adapter = adapter;
556 new_client->driver = &lm78_driver;
558 /* Now, we do the remaining detection. */
560 if (lm78_read_value(data, LM78_REG_CONFIG) & 0x80) {
564 if (lm78_read_value(data, LM78_REG_I2C_ADDR) !=
571 /* Determine the chip type. */
573 i = lm78_read_value(data, LM78_REG_CHIPID);
574 if (i == 0x00 || i == 0x20 /* LM78 */
575 || i == 0x40) /* LM78-J */
577 else if ((i & 0xfe) == 0xc0)
581 dev_warn(&adapter->dev, "Ignoring 'force' "
582 "parameter for unknown chip at "
583 "adapter %d, address 0x%02x\n",
584 i2c_adapter_id(adapter), address);
591 client_name = "lm78";
592 } else if (kind == lm79) {
593 client_name = "lm79";
596 /* Fill in the remaining client fields and put into the global list */
597 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
600 /* Tell the I2C layer a new client has arrived */
601 if ((err = i2c_attach_client(new_client)))
604 /* Initialize the LM78 chip */
605 lm78_init_device(data);
607 /* Register sysfs hooks */
608 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group)))
611 data->class_dev = hwmon_device_register(&new_client->dev);
612 if (IS_ERR(data->class_dev)) {
613 err = PTR_ERR(data->class_dev);
620 sysfs_remove_group(&new_client->dev.kobj, &lm78_group);
622 i2c_detach_client(new_client);
629 static int lm78_detach_client(struct i2c_client *client)
631 struct lm78_data *data = i2c_get_clientdata(client);
634 hwmon_device_unregister(data->class_dev);
635 sysfs_remove_group(&client->dev.kobj, &lm78_group);
637 if ((err = i2c_detach_client(client)))
645 static int __devinit lm78_isa_probe(struct platform_device *pdev)
648 struct lm78_data *data;
649 struct resource *res;
652 /* Reserve the ISA region */
653 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
654 if (!request_region(res->start, LM78_EXTENT, "lm78")) {
659 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
661 goto exit_release_region;
663 mutex_init(&data->lock);
664 data->client.addr = res->start;
665 i2c_set_clientdata(&data->client, data);
666 platform_set_drvdata(pdev, data);
668 if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
675 strlcpy(data->client.name, name, I2C_NAME_SIZE);
677 /* Initialize the LM78 chip */
678 lm78_init_device(data);
680 /* Register sysfs hooks */
681 if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group))
682 || (err = device_create_file(&pdev->dev, &dev_attr_name)))
683 goto exit_remove_files;
685 data->class_dev = hwmon_device_register(&pdev->dev);
686 if (IS_ERR(data->class_dev)) {
687 err = PTR_ERR(data->class_dev);
688 goto exit_remove_files;
694 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
695 device_remove_file(&pdev->dev, &dev_attr_name);
698 release_region(res->start, LM78_EXTENT);
703 static int __devexit lm78_isa_remove(struct platform_device *pdev)
705 struct lm78_data *data = platform_get_drvdata(pdev);
707 hwmon_device_unregister(data->class_dev);
708 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
709 device_remove_file(&pdev->dev, &dev_attr_name);
710 release_region(data->client.addr, LM78_EXTENT);
716 /* The SMBus locks itself, but ISA access must be locked explicitly!
717 We don't want to lock the whole ISA bus, so we lock each client
719 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
720 would slow down the LM78 access and should not be necessary. */
721 static int lm78_read_value(struct lm78_data *data, u8 reg)
723 struct i2c_client *client = &data->client;
725 if (!client->driver) { /* ISA device */
727 mutex_lock(&data->lock);
728 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
729 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
730 mutex_unlock(&data->lock);
733 return i2c_smbus_read_byte_data(client, reg);
736 /* The SMBus locks itself, but ISA access muse be locked explicitly!
737 We don't want to lock the whole ISA bus, so we lock each client
739 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
740 would slow down the LM78 access and should not be necessary.
741 There are some ugly typecasts here, but the good new is - they should
742 nowhere else be necessary! */
743 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
745 struct i2c_client *client = &data->client;
747 if (!client->driver) { /* ISA device */
748 mutex_lock(&data->lock);
749 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
750 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
751 mutex_unlock(&data->lock);
754 return i2c_smbus_write_byte_data(client, reg, value);
757 static void lm78_init_device(struct lm78_data *data)
762 /* Start monitoring */
763 config = lm78_read_value(data, LM78_REG_CONFIG);
764 if ((config & 0x09) != 0x01)
765 lm78_write_value(data, LM78_REG_CONFIG,
766 (config & 0xf7) | 0x01);
768 /* A few vars need to be filled upon startup */
769 for (i = 0; i < 3; i++) {
770 data->fan_min[i] = lm78_read_value(data,
771 LM78_REG_FAN_MIN(i));
774 mutex_init(&data->update_lock);
777 static struct lm78_data *lm78_update_device(struct device *dev)
779 struct lm78_data *data = dev_get_drvdata(dev);
782 mutex_lock(&data->update_lock);
784 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
787 dev_dbg(dev, "Starting lm78 update\n");
789 for (i = 0; i <= 6; i++) {
791 lm78_read_value(data, LM78_REG_IN(i));
793 lm78_read_value(data, LM78_REG_IN_MIN(i));
795 lm78_read_value(data, LM78_REG_IN_MAX(i));
797 for (i = 0; i < 3; i++) {
799 lm78_read_value(data, LM78_REG_FAN(i));
801 lm78_read_value(data, LM78_REG_FAN_MIN(i));
803 data->temp = lm78_read_value(data, LM78_REG_TEMP);
805 lm78_read_value(data, LM78_REG_TEMP_OVER);
807 lm78_read_value(data, LM78_REG_TEMP_HYST);
808 i = lm78_read_value(data, LM78_REG_VID_FANDIV);
809 data->vid = i & 0x0f;
810 if (data->type == lm79)
812 (lm78_read_value(data, LM78_REG_CHIPID) &
816 data->fan_div[0] = (i >> 4) & 0x03;
817 data->fan_div[1] = i >> 6;
818 data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
819 (lm78_read_value(data, LM78_REG_ALARM2) << 8);
820 data->last_updated = jiffies;
823 data->fan_div[2] = 1;
826 mutex_unlock(&data->update_lock);
831 /* return 1 if a supported chip is found, 0 otherwise */
832 static int __init lm78_isa_found(unsigned short address)
834 int val, save, found = 0;
836 if (!request_region(address, LM78_EXTENT, "lm78"))
839 #define REALLY_SLOW_IO
840 /* We need the timeouts for at least some LM78-like
841 chips. But only if we read 'undefined' registers. */
842 val = inb_p(address + 1);
843 if (inb_p(address + 2) != val
844 || inb_p(address + 3) != val
845 || inb_p(address + 7) != val)
847 #undef REALLY_SLOW_IO
849 /* We should be able to change the 7 LSB of the address port. The
850 MSB (busy flag) should be clear initially, set after the write. */
851 save = inb_p(address + LM78_ADDR_REG_OFFSET);
855 outb_p(val, address + LM78_ADDR_REG_OFFSET);
856 if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
857 outb_p(save, address + LM78_ADDR_REG_OFFSET);
861 /* We found a device, now see if it could be an LM78 */
862 outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
863 val = inb_p(address + LM78_DATA_REG_OFFSET);
866 outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
867 val = inb_p(address + LM78_DATA_REG_OFFSET);
868 if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
871 /* The busy flag should be clear again */
872 if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
875 /* Explicitly prevent the misdetection of Winbond chips */
876 outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
877 val = inb_p(address + LM78_DATA_REG_OFFSET);
878 if (val == 0xa3 || val == 0x5c)
881 /* Explicitly prevent the misdetection of ITE chips */
882 outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
883 val = inb_p(address + LM78_DATA_REG_OFFSET);
887 /* Determine the chip type */
888 outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
889 val = inb_p(address + LM78_DATA_REG_OFFSET);
890 if (val == 0x00 /* LM78 */
891 || val == 0x40 /* LM78-J */
892 || (val & 0xfe) == 0xc0) /* LM79 */
896 pr_info("lm78: Found an %s chip at %#x\n",
897 val & 0x80 ? "LM79" : "LM78", (int)address);
900 release_region(address, LM78_EXTENT);
904 static int __init lm78_isa_device_add(unsigned short address)
906 struct resource res = {
908 .end = address + LM78_EXTENT,
910 .flags = IORESOURCE_IO,
914 pdev = platform_device_alloc("lm78", address);
917 printk(KERN_ERR "lm78: Device allocation failed\n");
921 err = platform_device_add_resources(pdev, &res, 1);
923 printk(KERN_ERR "lm78: Device resource addition failed "
925 goto exit_device_put;
928 err = platform_device_add(pdev);
930 printk(KERN_ERR "lm78: Device addition failed (%d)\n",
932 goto exit_device_put;
938 platform_device_put(pdev);
944 static int __init sm_lm78_init(void)
948 res = i2c_add_driver(&lm78_driver);
952 if (lm78_isa_found(isa_address)) {
953 res = platform_driver_register(&lm78_isa_driver);
955 goto exit_unreg_i2c_driver;
957 /* Sets global pdev as a side effect */
958 res = lm78_isa_device_add(isa_address);
960 goto exit_unreg_isa_driver;
965 exit_unreg_isa_driver:
966 platform_driver_unregister(&lm78_isa_driver);
967 exit_unreg_i2c_driver:
968 i2c_del_driver(&lm78_driver);
973 static void __exit sm_lm78_exit(void)
976 platform_device_unregister(pdev);
977 platform_driver_unregister(&lm78_isa_driver);
979 i2c_del_driver(&lm78_driver);
984 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
985 MODULE_DESCRIPTION("LM78/LM79 driver");
986 MODULE_LICENSE("GPL");
988 module_init(sm_lm78_init);
989 module_exit(sm_lm78_exit);