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, 2011 Jean Delvare <jdelvare@suse.de>
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-vid.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
36 #include <linux/platform_device.h>
37 #include <linux/ioport.h>
41 /* Addresses to scan */
42 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43 0x2e, 0x2f, I2C_CLIENT_END };
44 enum chips { lm78, lm79 };
46 /* Many LM78 constants specified below */
48 /* Length of ISA address segment */
51 /* Where are the ISA address/data registers relative to the base address */
52 #define LM78_ADDR_REG_OFFSET 5
53 #define LM78_DATA_REG_OFFSET 6
55 /* The LM78 registers */
56 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
57 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
58 #define LM78_REG_IN(nr) (0x20 + (nr))
60 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
61 #define LM78_REG_FAN(nr) (0x28 + (nr))
63 #define LM78_REG_TEMP 0x27
64 #define LM78_REG_TEMP_OVER 0x39
65 #define LM78_REG_TEMP_HYST 0x3a
67 #define LM78_REG_ALARM1 0x41
68 #define LM78_REG_ALARM2 0x42
70 #define LM78_REG_VID_FANDIV 0x47
72 #define LM78_REG_CONFIG 0x40
73 #define LM78_REG_CHIPID 0x49
74 #define LM78_REG_I2C_ADDR 0x48
77 * Conversions. Rounding and limit checking is only done on the TO_REG
82 * IN: mV (0V to 4.08V)
85 static inline u8 IN_TO_REG(unsigned long val)
87 unsigned long nval = clamp_val(val, 0, 4080);
88 return (nval + 8) / 16;
90 #define IN_FROM_REG(val) ((val) * 16)
92 static inline u8 FAN_TO_REG(long rpm, int div)
98 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
101 static inline int FAN_FROM_REG(u8 val, int div)
103 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
107 * TEMP: mC (-128C to +127C)
108 * REG: 1C/bit, two's complement
110 static inline s8 TEMP_TO_REG(long val)
112 int nval = clamp_val(val, -128000, 127000) ;
113 return nval < 0 ? (nval - 500) / 1000 : (nval + 500) / 1000;
116 static inline int TEMP_FROM_REG(s8 val)
121 #define DIV_FROM_REG(val) (1 << (val))
124 struct i2c_client *client;
128 /* For ISA device only */
132 struct mutex update_lock;
133 char valid; /* !=0 if following fields are valid */
134 unsigned long last_updated; /* In jiffies */
136 u8 in[7]; /* Register value */
137 u8 in_max[7]; /* Register value */
138 u8 in_min[7]; /* Register value */
139 u8 fan[3]; /* Register value */
140 u8 fan_min[3]; /* Register value */
141 s8 temp; /* Register value */
142 s8 temp_over; /* Register value */
143 s8 temp_hyst; /* Register value */
144 u8 fan_div[3]; /* Register encoding, shifted right */
145 u8 vid; /* Register encoding, combined */
146 u16 alarms; /* Register encoding, combined */
149 static int lm78_read_value(struct lm78_data *data, u8 reg);
150 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
151 static struct lm78_data *lm78_update_device(struct device *dev);
152 static void lm78_init_device(struct lm78_data *data);
155 static ssize_t in_show(struct device *dev, struct device_attribute *da,
158 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
159 struct lm78_data *data = lm78_update_device(dev);
160 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index]));
163 static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
166 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
167 struct lm78_data *data = lm78_update_device(dev);
168 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index]));
171 static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
174 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
175 struct lm78_data *data = lm78_update_device(dev);
176 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index]));
179 static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
180 const char *buf, size_t count)
182 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
183 struct lm78_data *data = dev_get_drvdata(dev);
184 int nr = attr->index;
188 err = kstrtoul(buf, 10, &val);
192 mutex_lock(&data->update_lock);
193 data->in_min[nr] = IN_TO_REG(val);
194 lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
195 mutex_unlock(&data->update_lock);
199 static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
200 const char *buf, size_t count)
202 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
203 struct lm78_data *data = dev_get_drvdata(dev);
204 int nr = attr->index;
208 err = kstrtoul(buf, 10, &val);
212 mutex_lock(&data->update_lock);
213 data->in_max[nr] = IN_TO_REG(val);
214 lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
215 mutex_unlock(&data->update_lock);
219 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
220 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
221 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
222 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
223 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
224 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
225 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
226 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
227 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
228 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
229 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
230 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
231 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
232 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
233 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
234 static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
235 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
236 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
237 static SENSOR_DEVICE_ATTR_RO(in6_input, in, 6);
238 static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
239 static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
242 static ssize_t temp1_input_show(struct device *dev,
243 struct device_attribute *da, char *buf)
245 struct lm78_data *data = lm78_update_device(dev);
246 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
249 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *da,
252 struct lm78_data *data = lm78_update_device(dev);
253 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
256 static ssize_t temp1_max_store(struct device *dev,
257 struct device_attribute *da, const char *buf,
260 struct lm78_data *data = dev_get_drvdata(dev);
264 err = kstrtol(buf, 10, &val);
268 mutex_lock(&data->update_lock);
269 data->temp_over = TEMP_TO_REG(val);
270 lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
271 mutex_unlock(&data->update_lock);
275 static ssize_t temp1_max_hyst_show(struct device *dev,
276 struct device_attribute *da, char *buf)
278 struct lm78_data *data = lm78_update_device(dev);
279 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
282 static ssize_t temp1_max_hyst_store(struct device *dev,
283 struct device_attribute *da,
284 const char *buf, size_t count)
286 struct lm78_data *data = dev_get_drvdata(dev);
290 err = kstrtol(buf, 10, &val);
294 mutex_lock(&data->update_lock);
295 data->temp_hyst = TEMP_TO_REG(val);
296 lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
297 mutex_unlock(&data->update_lock);
301 static DEVICE_ATTR_RO(temp1_input);
302 static DEVICE_ATTR_RW(temp1_max);
303 static DEVICE_ATTR_RW(temp1_max_hyst);
306 static ssize_t fan_show(struct device *dev, struct device_attribute *da,
309 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
310 struct lm78_data *data = lm78_update_device(dev);
311 int nr = attr->index;
312 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
313 DIV_FROM_REG(data->fan_div[nr])));
316 static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
319 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
320 struct lm78_data *data = lm78_update_device(dev);
321 int nr = attr->index;
322 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
323 DIV_FROM_REG(data->fan_div[nr])));
326 static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
327 const char *buf, size_t count)
329 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
330 struct lm78_data *data = dev_get_drvdata(dev);
331 int nr = attr->index;
335 err = kstrtoul(buf, 10, &val);
339 mutex_lock(&data->update_lock);
340 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
341 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
342 mutex_unlock(&data->update_lock);
346 static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
349 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
350 struct lm78_data *data = lm78_update_device(dev);
351 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
355 * Note: we save and restore the fan minimum here, because its value is
356 * determined in part by the fan divisor. This follows the principle of
357 * least surprise; the user doesn't expect the fan minimum to change just
358 * because the divisor changed.
360 static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
361 const char *buf, size_t count)
363 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
364 struct lm78_data *data = dev_get_drvdata(dev);
365 int nr = attr->index;
371 err = kstrtoul(buf, 10, &val);
375 mutex_lock(&data->update_lock);
376 min = FAN_FROM_REG(data->fan_min[nr],
377 DIV_FROM_REG(data->fan_div[nr]));
381 data->fan_div[nr] = 0;
384 data->fan_div[nr] = 1;
387 data->fan_div[nr] = 2;
390 data->fan_div[nr] = 3;
394 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
396 mutex_unlock(&data->update_lock);
400 reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
403 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
406 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
409 lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
412 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
413 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
414 mutex_unlock(&data->update_lock);
419 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
420 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
421 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
422 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
423 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
424 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
426 /* Fan 3 divisor is locked in H/W */
427 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
428 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
429 static SENSOR_DEVICE_ATTR_RO(fan3_div, fan_div, 2);
432 static ssize_t cpu0_vid_show(struct device *dev, struct device_attribute *da,
435 struct lm78_data *data = lm78_update_device(dev);
436 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
438 static DEVICE_ATTR_RO(cpu0_vid);
441 static ssize_t alarms_show(struct device *dev, struct device_attribute *da,
444 struct lm78_data *data = lm78_update_device(dev);
445 return sprintf(buf, "%u\n", data->alarms);
447 static DEVICE_ATTR_RO(alarms);
449 static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
452 struct lm78_data *data = lm78_update_device(dev);
453 int nr = to_sensor_dev_attr(da)->index;
454 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
456 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
457 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
458 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
459 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
460 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
461 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 9);
462 static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 10);
463 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
464 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
465 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 11);
466 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
468 static struct attribute *lm78_attrs[] = {
469 &sensor_dev_attr_in0_input.dev_attr.attr,
470 &sensor_dev_attr_in0_min.dev_attr.attr,
471 &sensor_dev_attr_in0_max.dev_attr.attr,
472 &sensor_dev_attr_in0_alarm.dev_attr.attr,
473 &sensor_dev_attr_in1_input.dev_attr.attr,
474 &sensor_dev_attr_in1_min.dev_attr.attr,
475 &sensor_dev_attr_in1_max.dev_attr.attr,
476 &sensor_dev_attr_in1_alarm.dev_attr.attr,
477 &sensor_dev_attr_in2_input.dev_attr.attr,
478 &sensor_dev_attr_in2_min.dev_attr.attr,
479 &sensor_dev_attr_in2_max.dev_attr.attr,
480 &sensor_dev_attr_in2_alarm.dev_attr.attr,
481 &sensor_dev_attr_in3_input.dev_attr.attr,
482 &sensor_dev_attr_in3_min.dev_attr.attr,
483 &sensor_dev_attr_in3_max.dev_attr.attr,
484 &sensor_dev_attr_in3_alarm.dev_attr.attr,
485 &sensor_dev_attr_in4_input.dev_attr.attr,
486 &sensor_dev_attr_in4_min.dev_attr.attr,
487 &sensor_dev_attr_in4_max.dev_attr.attr,
488 &sensor_dev_attr_in4_alarm.dev_attr.attr,
489 &sensor_dev_attr_in5_input.dev_attr.attr,
490 &sensor_dev_attr_in5_min.dev_attr.attr,
491 &sensor_dev_attr_in5_max.dev_attr.attr,
492 &sensor_dev_attr_in5_alarm.dev_attr.attr,
493 &sensor_dev_attr_in6_input.dev_attr.attr,
494 &sensor_dev_attr_in6_min.dev_attr.attr,
495 &sensor_dev_attr_in6_max.dev_attr.attr,
496 &sensor_dev_attr_in6_alarm.dev_attr.attr,
497 &dev_attr_temp1_input.attr,
498 &dev_attr_temp1_max.attr,
499 &dev_attr_temp1_max_hyst.attr,
500 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
501 &sensor_dev_attr_fan1_input.dev_attr.attr,
502 &sensor_dev_attr_fan1_min.dev_attr.attr,
503 &sensor_dev_attr_fan1_div.dev_attr.attr,
504 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
505 &sensor_dev_attr_fan2_input.dev_attr.attr,
506 &sensor_dev_attr_fan2_min.dev_attr.attr,
507 &sensor_dev_attr_fan2_div.dev_attr.attr,
508 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
509 &sensor_dev_attr_fan3_input.dev_attr.attr,
510 &sensor_dev_attr_fan3_min.dev_attr.attr,
511 &sensor_dev_attr_fan3_div.dev_attr.attr,
512 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
513 &dev_attr_alarms.attr,
514 &dev_attr_cpu0_vid.attr,
519 ATTRIBUTE_GROUPS(lm78);
526 /* ISA device, if found */
527 static struct platform_device *pdev;
529 static unsigned short isa_address = 0x290;
531 static struct lm78_data *lm78_data_if_isa(void)
533 return pdev ? platform_get_drvdata(pdev) : NULL;
536 /* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
537 static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
539 struct lm78_data *isa;
542 if (!pdev) /* No ISA chip */
544 isa = platform_get_drvdata(pdev);
546 if (lm78_read_value(isa, LM78_REG_I2C_ADDR) != client->addr)
547 return 0; /* Address doesn't match */
548 if ((lm78_read_value(isa, LM78_REG_CHIPID) & 0xfe) != (chipid & 0xfe))
549 return 0; /* Chip type doesn't match */
552 * We compare all the limit registers, the config register and the
553 * interrupt mask registers
555 for (i = 0x2b; i <= 0x3d; i++) {
556 if (lm78_read_value(isa, i) !=
557 i2c_smbus_read_byte_data(client, i))
560 if (lm78_read_value(isa, LM78_REG_CONFIG) !=
561 i2c_smbus_read_byte_data(client, LM78_REG_CONFIG))
563 for (i = 0x43; i <= 0x46; i++) {
564 if (lm78_read_value(isa, i) !=
565 i2c_smbus_read_byte_data(client, i))
571 #else /* !CONFIG_ISA */
573 static int lm78_alias_detect(struct i2c_client *client, u8 chipid)
578 static struct lm78_data *lm78_data_if_isa(void)
582 #endif /* CONFIG_ISA */
584 static int lm78_i2c_detect(struct i2c_client *client,
585 struct i2c_board_info *info)
588 struct lm78_data *isa = lm78_data_if_isa();
589 const char *client_name;
590 struct i2c_adapter *adapter = client->adapter;
591 int address = client->addr;
593 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
597 * We block updates of the ISA device to minimize the risk of
598 * concurrent access to the same LM78 chip through different
602 mutex_lock(&isa->update_lock);
604 if ((i2c_smbus_read_byte_data(client, LM78_REG_CONFIG) & 0x80)
605 || i2c_smbus_read_byte_data(client, LM78_REG_I2C_ADDR) != address)
608 /* Explicitly prevent the misdetection of Winbond chips */
609 i = i2c_smbus_read_byte_data(client, 0x4f);
610 if (i == 0xa3 || i == 0x5c)
613 /* Determine the chip type. */
614 i = i2c_smbus_read_byte_data(client, LM78_REG_CHIPID);
615 if (i == 0x00 || i == 0x20 /* LM78 */
616 || i == 0x40) /* LM78-J */
617 client_name = "lm78";
618 else if ((i & 0xfe) == 0xc0)
619 client_name = "lm79";
623 if (lm78_alias_detect(client, i)) {
624 dev_dbg(&adapter->dev,
625 "Device at 0x%02x appears to be the same as ISA device\n",
631 mutex_unlock(&isa->update_lock);
633 strlcpy(info->type, client_name, I2C_NAME_SIZE);
639 mutex_unlock(&isa->update_lock);
643 static int lm78_i2c_probe(struct i2c_client *client,
644 const struct i2c_device_id *id)
646 struct device *dev = &client->dev;
647 struct device *hwmon_dev;
648 struct lm78_data *data;
650 data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
654 data->client = client;
655 data->type = id->driver_data;
657 /* Initialize the LM78 chip */
658 lm78_init_device(data);
660 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
662 return PTR_ERR_OR_ZERO(hwmon_dev);
665 static const struct i2c_device_id lm78_i2c_id[] = {
670 MODULE_DEVICE_TABLE(i2c, lm78_i2c_id);
672 static struct i2c_driver lm78_driver = {
673 .class = I2C_CLASS_HWMON,
677 .probe = lm78_i2c_probe,
678 .id_table = lm78_i2c_id,
679 .detect = lm78_i2c_detect,
680 .address_list = normal_i2c,
684 * The SMBus locks itself, but ISA access must be locked explicitly!
685 * We don't want to lock the whole ISA bus, so we lock each client
687 * We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
688 * would slow down the LM78 access and should not be necessary.
690 static int lm78_read_value(struct lm78_data *data, u8 reg)
692 struct i2c_client *client = data->client;
695 if (!client) { /* ISA device */
697 mutex_lock(&data->lock);
698 outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
699 res = inb_p(data->isa_addr + LM78_DATA_REG_OFFSET);
700 mutex_unlock(&data->lock);
704 return i2c_smbus_read_byte_data(client, reg);
707 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
709 struct i2c_client *client = data->client;
712 if (!client) { /* ISA device */
713 mutex_lock(&data->lock);
714 outb_p(reg, data->isa_addr + LM78_ADDR_REG_OFFSET);
715 outb_p(value, data->isa_addr + LM78_DATA_REG_OFFSET);
716 mutex_unlock(&data->lock);
720 return i2c_smbus_write_byte_data(client, reg, value);
723 static void lm78_init_device(struct lm78_data *data)
728 /* Start monitoring */
729 config = lm78_read_value(data, LM78_REG_CONFIG);
730 if ((config & 0x09) != 0x01)
731 lm78_write_value(data, LM78_REG_CONFIG,
732 (config & 0xf7) | 0x01);
734 /* A few vars need to be filled upon startup */
735 for (i = 0; i < 3; i++) {
736 data->fan_min[i] = lm78_read_value(data,
737 LM78_REG_FAN_MIN(i));
740 mutex_init(&data->update_lock);
743 static struct lm78_data *lm78_update_device(struct device *dev)
745 struct lm78_data *data = dev_get_drvdata(dev);
748 mutex_lock(&data->update_lock);
750 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
753 dev_dbg(dev, "Starting lm78 update\n");
755 for (i = 0; i <= 6; i++) {
757 lm78_read_value(data, LM78_REG_IN(i));
759 lm78_read_value(data, LM78_REG_IN_MIN(i));
761 lm78_read_value(data, LM78_REG_IN_MAX(i));
763 for (i = 0; i < 3; i++) {
765 lm78_read_value(data, LM78_REG_FAN(i));
767 lm78_read_value(data, LM78_REG_FAN_MIN(i));
769 data->temp = lm78_read_value(data, LM78_REG_TEMP);
771 lm78_read_value(data, LM78_REG_TEMP_OVER);
773 lm78_read_value(data, LM78_REG_TEMP_HYST);
774 i = lm78_read_value(data, LM78_REG_VID_FANDIV);
775 data->vid = i & 0x0f;
776 if (data->type == lm79)
778 (lm78_read_value(data, LM78_REG_CHIPID) &
782 data->fan_div[0] = (i >> 4) & 0x03;
783 data->fan_div[1] = i >> 6;
784 data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
785 (lm78_read_value(data, LM78_REG_ALARM2) << 8);
786 data->last_updated = jiffies;
789 data->fan_div[2] = 1;
792 mutex_unlock(&data->update_lock);
798 static int lm78_isa_probe(struct platform_device *pdev)
800 struct device *dev = &pdev->dev;
801 struct device *hwmon_dev;
802 struct lm78_data *data;
803 struct resource *res;
805 /* Reserve the ISA region */
806 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
807 if (!devm_request_region(dev, res->start + LM78_ADDR_REG_OFFSET,
811 data = devm_kzalloc(dev, sizeof(struct lm78_data), GFP_KERNEL);
815 mutex_init(&data->lock);
816 data->isa_addr = res->start;
817 platform_set_drvdata(pdev, data);
819 if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
827 /* Initialize the LM78 chip */
828 lm78_init_device(data);
830 hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
832 return PTR_ERR_OR_ZERO(hwmon_dev);
835 static struct platform_driver lm78_isa_driver = {
839 .probe = lm78_isa_probe,
842 /* return 1 if a supported chip is found, 0 otherwise */
843 static int __init lm78_isa_found(unsigned short address)
845 int val, save, found = 0;
849 * Some boards declare base+0 to base+7 as a PNP device, some base+4
850 * to base+7 and some base+5 to base+6. So we better request each port
851 * individually for the probing phase.
853 for (port = address; port < address + LM78_EXTENT; port++) {
854 if (!request_region(port, 1, "lm78")) {
855 pr_debug("Failed to request port 0x%x\n", port);
860 #define REALLY_SLOW_IO
862 * We need the timeouts for at least some LM78-like
863 * chips. But only if we read 'undefined' registers.
865 val = inb_p(address + 1);
866 if (inb_p(address + 2) != val
867 || inb_p(address + 3) != val
868 || inb_p(address + 7) != val)
870 #undef REALLY_SLOW_IO
873 * We should be able to change the 7 LSB of the address port. The
874 * MSB (busy flag) should be clear initially, set after the write.
876 save = inb_p(address + LM78_ADDR_REG_OFFSET);
880 outb_p(val, address + LM78_ADDR_REG_OFFSET);
881 if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
882 outb_p(save, address + LM78_ADDR_REG_OFFSET);
886 /* We found a device, now see if it could be an LM78 */
887 outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
888 val = inb_p(address + LM78_DATA_REG_OFFSET);
891 outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
892 val = inb_p(address + LM78_DATA_REG_OFFSET);
893 if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
896 /* The busy flag should be clear again */
897 if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
900 /* Explicitly prevent the misdetection of Winbond chips */
901 outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
902 val = inb_p(address + LM78_DATA_REG_OFFSET);
903 if (val == 0xa3 || val == 0x5c)
906 /* Explicitly prevent the misdetection of ITE chips */
907 outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
908 val = inb_p(address + LM78_DATA_REG_OFFSET);
912 /* Determine the chip type */
913 outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
914 val = inb_p(address + LM78_DATA_REG_OFFSET);
915 if (val == 0x00 || val == 0x20 /* LM78 */
916 || val == 0x40 /* LM78-J */
917 || (val & 0xfe) == 0xc0) /* LM79 */
921 pr_info("Found an %s chip at %#x\n",
922 val & 0x80 ? "LM79" : "LM78", (int)address);
925 for (port--; port >= address; port--)
926 release_region(port, 1);
930 static int __init lm78_isa_device_add(unsigned short address)
932 struct resource res = {
934 .end = address + LM78_EXTENT - 1,
936 .flags = IORESOURCE_IO,
940 pdev = platform_device_alloc("lm78", address);
943 pr_err("Device allocation failed\n");
947 err = platform_device_add_resources(pdev, &res, 1);
949 pr_err("Device resource addition failed (%d)\n", err);
950 goto exit_device_put;
953 err = platform_device_add(pdev);
955 pr_err("Device addition failed (%d)\n", err);
956 goto exit_device_put;
962 platform_device_put(pdev);
968 static int __init lm78_isa_register(void)
972 if (lm78_isa_found(isa_address)) {
973 res = platform_driver_register(&lm78_isa_driver);
977 /* Sets global pdev as a side effect */
978 res = lm78_isa_device_add(isa_address);
980 goto exit_unreg_isa_driver;
985 exit_unreg_isa_driver:
986 platform_driver_unregister(&lm78_isa_driver);
991 static void lm78_isa_unregister(void)
994 platform_device_unregister(pdev);
995 platform_driver_unregister(&lm78_isa_driver);
998 #else /* !CONFIG_ISA */
1000 static int __init lm78_isa_register(void)
1005 static void lm78_isa_unregister(void)
1008 #endif /* CONFIG_ISA */
1010 static int __init sm_lm78_init(void)
1015 * We register the ISA device first, so that we can skip the
1016 * registration of an I2C interface to the same device.
1018 res = lm78_isa_register();
1022 res = i2c_add_driver(&lm78_driver);
1024 goto exit_unreg_isa_device;
1028 exit_unreg_isa_device:
1029 lm78_isa_unregister();
1034 static void __exit sm_lm78_exit(void)
1036 lm78_isa_unregister();
1037 i2c_del_driver(&lm78_driver);
1040 MODULE_AUTHOR("Frodo Looijaard, Jean Delvare <jdelvare@suse.de>");
1041 MODULE_DESCRIPTION("LM78/LM79 driver");
1042 MODULE_LICENSE("GPL");
1044 module_init(sm_lm78_init);
1045 module_exit(sm_lm78_exit);