ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / hwmon / lm78.c
blobed7859f0e16a952c8b63cefc376e0edb92d56934
1 /*
2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 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/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <asm/io.h>
36 /* ISA device, if found */
37 static struct platform_device *pdev;
39 /* Addresses to scan */
40 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
41 0x2e, 0x2f, I2C_CLIENT_END };
42 static unsigned short isa_address = 0x290;
44 /* Insmod parameters */
45 I2C_CLIENT_INSMOD_2(lm78, lm79);
47 /* Many LM78 constants specified below */
49 /* Length of ISA address segment */
50 #define LM78_EXTENT 8
52 /* Where are the ISA address/data registers relative to the base address */
53 #define LM78_ADDR_REG_OFFSET 5
54 #define LM78_DATA_REG_OFFSET 6
56 /* The LM78 registers */
57 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
58 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
59 #define LM78_REG_IN(nr) (0x20 + (nr))
61 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
62 #define LM78_REG_FAN(nr) (0x28 + (nr))
64 #define LM78_REG_TEMP 0x27
65 #define LM78_REG_TEMP_OVER 0x39
66 #define LM78_REG_TEMP_HYST 0x3a
68 #define LM78_REG_ALARM1 0x41
69 #define LM78_REG_ALARM2 0x42
71 #define LM78_REG_VID_FANDIV 0x47
73 #define LM78_REG_CONFIG 0x40
74 #define LM78_REG_CHIPID 0x49
75 #define LM78_REG_I2C_ADDR 0x48
78 /* Conversions. Rounding and limit checking is only done on the TO_REG
79 variants. */
81 /* IN: mV, (0V to 4.08V)
82 REG: 16mV/bit */
83 static inline u8 IN_TO_REG(unsigned long val)
85 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
86 return (nval + 8) / 16;
88 #define IN_FROM_REG(val) ((val) * 16)
90 static inline u8 FAN_TO_REG(long rpm, int div)
92 if (rpm <= 0)
93 return 255;
94 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
97 static inline int FAN_FROM_REG(u8 val, int div)
99 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
102 /* TEMP: mC (-128C to +127C)
103 REG: 1C/bit, two's complement */
104 static inline s8 TEMP_TO_REG(int val)
106 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
107 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
110 static inline int TEMP_FROM_REG(s8 val)
112 return val * 1000;
115 #define DIV_FROM_REG(val) (1 << (val))
117 /* There are some complications in a module like this. First off, LM78 chips
118 may be both present on the SMBus and the ISA bus, and we have to handle
119 those cases separately at some places. Second, there might be several
120 LM78 chips available (well, actually, that is probably never done; but
121 it is a clean illustration of how to handle a case like that). Finally,
122 a specific chip may be attached to *both* ISA and SMBus, and we would
123 not like to detect it double. Fortunately, in the case of the LM78 at
124 least, a register tells us what SMBus address we are on, so that helps
125 a bit - except if there could be more than one SMBus. Groan. No solution
126 for this yet. */
128 /* For ISA chips, we abuse the i2c_client addr and name fields. We also use
129 the driver field to differentiate between I2C and ISA chips. */
130 struct lm78_data {
131 struct i2c_client client;
132 struct device *hwmon_dev;
133 struct mutex lock;
134 enum chips type;
136 struct mutex update_lock;
137 char valid; /* !=0 if following fields are valid */
138 unsigned long last_updated; /* In jiffies */
140 u8 in[7]; /* Register value */
141 u8 in_max[7]; /* Register value */
142 u8 in_min[7]; /* Register value */
143 u8 fan[3]; /* Register value */
144 u8 fan_min[3]; /* Register value */
145 s8 temp; /* Register value */
146 s8 temp_over; /* Register value */
147 s8 temp_hyst; /* Register value */
148 u8 fan_div[3]; /* Register encoding, shifted right */
149 u8 vid; /* Register encoding, combined */
150 u16 alarms; /* Register encoding, combined */
154 static int lm78_attach_adapter(struct i2c_adapter *adapter);
155 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
156 static int lm78_detach_client(struct i2c_client *client);
158 static int __devinit lm78_isa_probe(struct platform_device *pdev);
159 static int __devexit lm78_isa_remove(struct platform_device *pdev);
161 static int lm78_read_value(struct lm78_data *data, u8 reg);
162 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value);
163 static struct lm78_data *lm78_update_device(struct device *dev);
164 static void lm78_init_device(struct lm78_data *data);
167 static struct i2c_driver lm78_driver = {
168 .driver = {
169 .name = "lm78",
171 .attach_adapter = lm78_attach_adapter,
172 .detach_client = lm78_detach_client,
175 static struct platform_driver lm78_isa_driver = {
176 .driver = {
177 .owner = THIS_MODULE,
178 .name = "lm78",
180 .probe = lm78_isa_probe,
181 .remove = lm78_isa_remove,
185 /* 7 Voltages */
186 static ssize_t show_in(struct device *dev, struct device_attribute *da,
187 char *buf)
189 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
190 struct lm78_data *data = lm78_update_device(dev);
191 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index]));
194 static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
195 char *buf)
197 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
198 struct lm78_data *data = lm78_update_device(dev);
199 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index]));
202 static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
203 char *buf)
205 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
206 struct lm78_data *data = lm78_update_device(dev);
207 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index]));
210 static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
211 const char *buf, size_t count)
213 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
214 struct lm78_data *data = dev_get_drvdata(dev);
215 unsigned long val = simple_strtoul(buf, NULL, 10);
216 int nr = attr->index;
218 mutex_lock(&data->update_lock);
219 data->in_min[nr] = IN_TO_REG(val);
220 lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
221 mutex_unlock(&data->update_lock);
222 return count;
225 static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
226 const char *buf, size_t count)
228 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
229 struct lm78_data *data = dev_get_drvdata(dev);
230 unsigned long val = simple_strtoul(buf, NULL, 10);
231 int nr = attr->index;
233 mutex_lock(&data->update_lock);
234 data->in_max[nr] = IN_TO_REG(val);
235 lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
236 mutex_unlock(&data->update_lock);
237 return count;
240 #define show_in_offset(offset) \
241 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
242 show_in, NULL, offset); \
243 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
244 show_in_min, set_in_min, offset); \
245 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
246 show_in_max, set_in_max, offset);
248 show_in_offset(0);
249 show_in_offset(1);
250 show_in_offset(2);
251 show_in_offset(3);
252 show_in_offset(4);
253 show_in_offset(5);
254 show_in_offset(6);
256 /* Temperature */
257 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
258 char *buf)
260 struct lm78_data *data = lm78_update_device(dev);
261 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
264 static ssize_t show_temp_over(struct device *dev, struct device_attribute *da,
265 char *buf)
267 struct lm78_data *data = lm78_update_device(dev);
268 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
271 static ssize_t set_temp_over(struct device *dev, struct device_attribute *da,
272 const char *buf, size_t count)
274 struct lm78_data *data = dev_get_drvdata(dev);
275 long val = simple_strtol(buf, NULL, 10);
277 mutex_lock(&data->update_lock);
278 data->temp_over = TEMP_TO_REG(val);
279 lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
280 mutex_unlock(&data->update_lock);
281 return count;
284 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da,
285 char *buf)
287 struct lm78_data *data = lm78_update_device(dev);
288 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
291 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da,
292 const char *buf, size_t count)
294 struct lm78_data *data = dev_get_drvdata(dev);
295 long val = simple_strtol(buf, NULL, 10);
297 mutex_lock(&data->update_lock);
298 data->temp_hyst = TEMP_TO_REG(val);
299 lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
300 mutex_unlock(&data->update_lock);
301 return count;
304 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
305 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
306 show_temp_over, set_temp_over);
307 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
308 show_temp_hyst, set_temp_hyst);
310 /* 3 Fans */
311 static ssize_t show_fan(struct device *dev, struct device_attribute *da,
312 char *buf)
314 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
315 struct lm78_data *data = lm78_update_device(dev);
316 int nr = attr->index;
317 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
318 DIV_FROM_REG(data->fan_div[nr])) );
321 static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
322 char *buf)
324 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
325 struct lm78_data *data = lm78_update_device(dev);
326 int nr = attr->index;
327 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
328 DIV_FROM_REG(data->fan_div[nr])) );
331 static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
332 const char *buf, size_t count)
334 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
335 struct lm78_data *data = dev_get_drvdata(dev);
336 int nr = attr->index;
337 unsigned long val = simple_strtoul(buf, NULL, 10);
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);
343 return count;
346 static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
347 char *buf)
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]));
354 /* Note: we save and restore the fan minimum here, because its value is
355 determined in part by the fan divisor. This follows the principle of
356 least surprise; the user doesn't expect the fan minimum to change just
357 because the divisor changed. */
358 static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
359 const char *buf, size_t count)
361 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
362 struct lm78_data *data = dev_get_drvdata(dev);
363 int nr = attr->index;
364 unsigned long val = simple_strtoul(buf, NULL, 10);
365 unsigned long min;
366 u8 reg;
368 mutex_lock(&data->update_lock);
369 min = FAN_FROM_REG(data->fan_min[nr],
370 DIV_FROM_REG(data->fan_div[nr]));
372 switch (val) {
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;
377 default:
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);
381 return -EINVAL;
384 reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
385 switch (nr) {
386 case 0:
387 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
388 break;
389 case 1:
390 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
391 break;
393 lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
395 data->fan_min[nr] =
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);
400 return count;
403 #define show_fan_offset(offset) \
404 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
405 show_fan, NULL, offset - 1); \
406 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
407 show_fan_min, set_fan_min, offset - 1);
409 show_fan_offset(1);
410 show_fan_offset(2);
411 show_fan_offset(3);
413 /* Fan 3 divisor is locked in H/W */
414 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
415 show_fan_div, set_fan_div, 0);
416 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
417 show_fan_div, set_fan_div, 1);
418 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2);
420 /* VID */
421 static ssize_t show_vid(struct device *dev, struct device_attribute *da,
422 char *buf)
424 struct lm78_data *data = lm78_update_device(dev);
425 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
427 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
429 /* Alarms */
430 static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
431 char *buf)
433 struct lm78_data *data = lm78_update_device(dev);
434 return sprintf(buf, "%u\n", data->alarms);
436 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
438 static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
439 char *buf)
441 struct lm78_data *data = lm78_update_device(dev);
442 int nr = to_sensor_dev_attr(da)->index;
443 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
445 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
446 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
447 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
448 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
449 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
450 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
451 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
452 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
453 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
454 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
455 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
457 /* This function is called when:
458 * lm78_driver is inserted (when this module is loaded), for each
459 available adapter
460 * when a new adapter is inserted (and lm78_driver is still present) */
461 static int lm78_attach_adapter(struct i2c_adapter *adapter)
463 if (!(adapter->class & I2C_CLASS_HWMON))
464 return 0;
465 return i2c_probe(adapter, &addr_data, lm78_detect);
468 static struct attribute *lm78_attributes[] = {
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,
516 NULL
519 static const struct attribute_group lm78_group = {
520 .attrs = lm78_attributes,
523 /* I2C devices get this name attribute automatically, but for ISA devices
524 we must create it by ourselves. */
525 static ssize_t show_name(struct device *dev, struct device_attribute
526 *devattr, char *buf)
528 struct lm78_data *data = dev_get_drvdata(dev);
530 return sprintf(buf, "%s\n", data->client.name);
532 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
534 /* This function is called by i2c_probe */
535 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
537 int i, err;
538 struct i2c_client *new_client;
539 struct lm78_data *data;
540 const char *client_name = "";
542 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
543 err = -ENODEV;
544 goto ERROR1;
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 = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
552 err = -ENOMEM;
553 goto ERROR1;
556 new_client = &data->client;
557 i2c_set_clientdata(new_client, data);
558 new_client->addr = address;
559 new_client->adapter = adapter;
560 new_client->driver = &lm78_driver;
562 /* Now, we do the remaining detection. */
563 if (kind < 0) {
564 if (lm78_read_value(data, LM78_REG_CONFIG) & 0x80) {
565 err = -ENODEV;
566 goto ERROR2;
568 if (lm78_read_value(data, LM78_REG_I2C_ADDR) !=
569 address) {
570 err = -ENODEV;
571 goto ERROR2;
575 /* Determine the chip type. */
576 if (kind <= 0) {
577 i = lm78_read_value(data, LM78_REG_CHIPID);
578 if (i == 0x00 || i == 0x20 /* LM78 */
579 || i == 0x40) /* LM78-J */
580 kind = lm78;
581 else if ((i & 0xfe) == 0xc0)
582 kind = lm79;
583 else {
584 if (kind == 0)
585 dev_warn(&adapter->dev, "Ignoring 'force' "
586 "parameter for unknown chip at "
587 "adapter %d, address 0x%02x\n",
588 i2c_adapter_id(adapter), address);
589 err = -ENODEV;
590 goto ERROR2;
594 if (kind == lm78) {
595 client_name = "lm78";
596 } else if (kind == lm79) {
597 client_name = "lm79";
600 /* Fill in the remaining client fields and put into the global list */
601 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
602 data->type = kind;
604 /* Tell the I2C layer a new client has arrived */
605 if ((err = i2c_attach_client(new_client)))
606 goto ERROR2;
608 /* Initialize the LM78 chip */
609 lm78_init_device(data);
611 /* Register sysfs hooks */
612 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group)))
613 goto ERROR3;
615 data->hwmon_dev = hwmon_device_register(&new_client->dev);
616 if (IS_ERR(data->hwmon_dev)) {
617 err = PTR_ERR(data->hwmon_dev);
618 goto ERROR4;
621 return 0;
623 ERROR4:
624 sysfs_remove_group(&new_client->dev.kobj, &lm78_group);
625 ERROR3:
626 i2c_detach_client(new_client);
627 ERROR2:
628 kfree(data);
629 ERROR1:
630 return err;
633 static int lm78_detach_client(struct i2c_client *client)
635 struct lm78_data *data = i2c_get_clientdata(client);
636 int err;
638 hwmon_device_unregister(data->hwmon_dev);
639 sysfs_remove_group(&client->dev.kobj, &lm78_group);
641 if ((err = i2c_detach_client(client)))
642 return err;
644 kfree(data);
646 return 0;
649 static int __devinit lm78_isa_probe(struct platform_device *pdev)
651 int err;
652 struct lm78_data *data;
653 struct resource *res;
654 const char *name;
656 /* Reserve the ISA region */
657 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
658 if (!request_region(res->start, LM78_EXTENT, "lm78")) {
659 err = -EBUSY;
660 goto exit;
663 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
664 err = -ENOMEM;
665 goto exit_release_region;
667 mutex_init(&data->lock);
668 data->client.addr = res->start;
669 i2c_set_clientdata(&data->client, data);
670 platform_set_drvdata(pdev, data);
672 if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
673 data->type = lm79;
674 name = "lm79";
675 } else {
676 data->type = lm78;
677 name = "lm78";
679 strlcpy(data->client.name, name, I2C_NAME_SIZE);
681 /* Initialize the LM78 chip */
682 lm78_init_device(data);
684 /* Register sysfs hooks */
685 if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group))
686 || (err = device_create_file(&pdev->dev, &dev_attr_name)))
687 goto exit_remove_files;
689 data->hwmon_dev = hwmon_device_register(&pdev->dev);
690 if (IS_ERR(data->hwmon_dev)) {
691 err = PTR_ERR(data->hwmon_dev);
692 goto exit_remove_files;
695 return 0;
697 exit_remove_files:
698 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
699 device_remove_file(&pdev->dev, &dev_attr_name);
700 kfree(data);
701 exit_release_region:
702 release_region(res->start, LM78_EXTENT);
703 exit:
704 return err;
707 static int __devexit lm78_isa_remove(struct platform_device *pdev)
709 struct lm78_data *data = platform_get_drvdata(pdev);
711 hwmon_device_unregister(data->hwmon_dev);
712 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
713 device_remove_file(&pdev->dev, &dev_attr_name);
714 release_region(data->client.addr, LM78_EXTENT);
715 kfree(data);
717 return 0;
720 /* The SMBus locks itself, but ISA access must be locked explicitly!
721 We don't want to lock the whole ISA bus, so we lock each client
722 separately.
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 static int lm78_read_value(struct lm78_data *data, u8 reg)
727 struct i2c_client *client = &data->client;
729 if (!client->driver) { /* ISA device */
730 int res;
731 mutex_lock(&data->lock);
732 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
733 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
734 mutex_unlock(&data->lock);
735 return res;
736 } else
737 return i2c_smbus_read_byte_data(client, reg);
740 /* The SMBus locks itself, but ISA access muse be locked explicitly!
741 We don't want to lock the whole ISA bus, so we lock each client
742 separately.
743 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
744 would slow down the LM78 access and should not be necessary.
745 There are some ugly typecasts here, but the good new is - they should
746 nowhere else be necessary! */
747 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
749 struct i2c_client *client = &data->client;
751 if (!client->driver) { /* ISA device */
752 mutex_lock(&data->lock);
753 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
754 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
755 mutex_unlock(&data->lock);
756 return 0;
757 } else
758 return i2c_smbus_write_byte_data(client, reg, value);
761 static void lm78_init_device(struct lm78_data *data)
763 u8 config;
764 int i;
766 /* Start monitoring */
767 config = lm78_read_value(data, LM78_REG_CONFIG);
768 if ((config & 0x09) != 0x01)
769 lm78_write_value(data, LM78_REG_CONFIG,
770 (config & 0xf7) | 0x01);
772 /* A few vars need to be filled upon startup */
773 for (i = 0; i < 3; i++) {
774 data->fan_min[i] = lm78_read_value(data,
775 LM78_REG_FAN_MIN(i));
778 mutex_init(&data->update_lock);
781 static struct lm78_data *lm78_update_device(struct device *dev)
783 struct lm78_data *data = dev_get_drvdata(dev);
784 int i;
786 mutex_lock(&data->update_lock);
788 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
789 || !data->valid) {
791 dev_dbg(dev, "Starting lm78 update\n");
793 for (i = 0; i <= 6; i++) {
794 data->in[i] =
795 lm78_read_value(data, LM78_REG_IN(i));
796 data->in_min[i] =
797 lm78_read_value(data, LM78_REG_IN_MIN(i));
798 data->in_max[i] =
799 lm78_read_value(data, LM78_REG_IN_MAX(i));
801 for (i = 0; i < 3; i++) {
802 data->fan[i] =
803 lm78_read_value(data, LM78_REG_FAN(i));
804 data->fan_min[i] =
805 lm78_read_value(data, LM78_REG_FAN_MIN(i));
807 data->temp = lm78_read_value(data, LM78_REG_TEMP);
808 data->temp_over =
809 lm78_read_value(data, LM78_REG_TEMP_OVER);
810 data->temp_hyst =
811 lm78_read_value(data, LM78_REG_TEMP_HYST);
812 i = lm78_read_value(data, LM78_REG_VID_FANDIV);
813 data->vid = i & 0x0f;
814 if (data->type == lm79)
815 data->vid |=
816 (lm78_read_value(data, LM78_REG_CHIPID) &
817 0x01) << 4;
818 else
819 data->vid |= 0x10;
820 data->fan_div[0] = (i >> 4) & 0x03;
821 data->fan_div[1] = i >> 6;
822 data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
823 (lm78_read_value(data, LM78_REG_ALARM2) << 8);
824 data->last_updated = jiffies;
825 data->valid = 1;
827 data->fan_div[2] = 1;
830 mutex_unlock(&data->update_lock);
832 return data;
835 /* return 1 if a supported chip is found, 0 otherwise */
836 static int __init lm78_isa_found(unsigned short address)
838 int val, save, found = 0;
840 if (!request_region(address, LM78_EXTENT, "lm78"))
841 return 0;
843 #define REALLY_SLOW_IO
844 /* We need the timeouts for at least some LM78-like
845 chips. But only if we read 'undefined' registers. */
846 val = inb_p(address + 1);
847 if (inb_p(address + 2) != val
848 || inb_p(address + 3) != val
849 || inb_p(address + 7) != val)
850 goto release;
851 #undef REALLY_SLOW_IO
853 /* We should be able to change the 7 LSB of the address port. The
854 MSB (busy flag) should be clear initially, set after the write. */
855 save = inb_p(address + LM78_ADDR_REG_OFFSET);
856 if (save & 0x80)
857 goto release;
858 val = ~save & 0x7f;
859 outb_p(val, address + LM78_ADDR_REG_OFFSET);
860 if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
861 outb_p(save, address + LM78_ADDR_REG_OFFSET);
862 goto release;
865 /* We found a device, now see if it could be an LM78 */
866 outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
867 val = inb_p(address + LM78_DATA_REG_OFFSET);
868 if (val & 0x80)
869 goto release;
870 outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
871 val = inb_p(address + LM78_DATA_REG_OFFSET);
872 if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
873 goto release;
875 /* The busy flag should be clear again */
876 if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
877 goto release;
879 /* Explicitly prevent the misdetection of Winbond chips */
880 outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
881 val = inb_p(address + LM78_DATA_REG_OFFSET);
882 if (val == 0xa3 || val == 0x5c)
883 goto release;
885 /* Explicitly prevent the misdetection of ITE chips */
886 outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
887 val = inb_p(address + LM78_DATA_REG_OFFSET);
888 if (val == 0x90)
889 goto release;
891 /* Determine the chip type */
892 outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
893 val = inb_p(address + LM78_DATA_REG_OFFSET);
894 if (val == 0x00 || val == 0x20 /* LM78 */
895 || val == 0x40 /* LM78-J */
896 || (val & 0xfe) == 0xc0) /* LM79 */
897 found = 1;
899 if (found)
900 pr_info("lm78: Found an %s chip at %#x\n",
901 val & 0x80 ? "LM79" : "LM78", (int)address);
903 release:
904 release_region(address, LM78_EXTENT);
905 return found;
908 static int __init lm78_isa_device_add(unsigned short address)
910 struct resource res = {
911 .start = address,
912 .end = address + LM78_EXTENT - 1,
913 .name = "lm78",
914 .flags = IORESOURCE_IO,
916 int err;
918 pdev = platform_device_alloc("lm78", address);
919 if (!pdev) {
920 err = -ENOMEM;
921 printk(KERN_ERR "lm78: Device allocation failed\n");
922 goto exit;
925 err = platform_device_add_resources(pdev, &res, 1);
926 if (err) {
927 printk(KERN_ERR "lm78: Device resource addition failed "
928 "(%d)\n", err);
929 goto exit_device_put;
932 err = platform_device_add(pdev);
933 if (err) {
934 printk(KERN_ERR "lm78: Device addition failed (%d)\n",
935 err);
936 goto exit_device_put;
939 return 0;
941 exit_device_put:
942 platform_device_put(pdev);
943 exit:
944 pdev = NULL;
945 return err;
948 static int __init sm_lm78_init(void)
950 int res;
952 res = i2c_add_driver(&lm78_driver);
953 if (res)
954 goto exit;
956 if (lm78_isa_found(isa_address)) {
957 res = platform_driver_register(&lm78_isa_driver);
958 if (res)
959 goto exit_unreg_i2c_driver;
961 /* Sets global pdev as a side effect */
962 res = lm78_isa_device_add(isa_address);
963 if (res)
964 goto exit_unreg_isa_driver;
967 return 0;
969 exit_unreg_isa_driver:
970 platform_driver_unregister(&lm78_isa_driver);
971 exit_unreg_i2c_driver:
972 i2c_del_driver(&lm78_driver);
973 exit:
974 return res;
977 static void __exit sm_lm78_exit(void)
979 if (pdev) {
980 platform_device_unregister(pdev);
981 platform_driver_unregister(&lm78_isa_driver);
983 i2c_del_driver(&lm78_driver);
988 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
989 MODULE_DESCRIPTION("LM78/LM79 driver");
990 MODULE_LICENSE("GPL");
992 module_init(sm_lm78_init);
993 module_exit(sm_lm78_exit);