revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / drivers / hwmon / lm78.c
blob934378eb4f8f197ad23de3ca6a363971cffba21a
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 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 .id = I2C_DRIVERID_LM78,
172 .attach_adapter = lm78_attach_adapter,
173 .detach_client = lm78_detach_client,
176 static struct platform_driver lm78_isa_driver = {
177 .driver = {
178 .owner = THIS_MODULE,
179 .name = "lm78",
181 .probe = lm78_isa_probe,
182 .remove = lm78_isa_remove,
186 /* 7 Voltages */
187 static ssize_t show_in(struct device *dev, struct device_attribute *da,
188 char *buf)
190 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
191 struct lm78_data *data = lm78_update_device(dev);
192 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index]));
195 static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
196 char *buf)
198 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
199 struct lm78_data *data = lm78_update_device(dev);
200 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index]));
203 static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
204 char *buf)
206 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
207 struct lm78_data *data = lm78_update_device(dev);
208 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index]));
211 static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
212 const char *buf, size_t count)
214 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
215 struct lm78_data *data = dev_get_drvdata(dev);
216 unsigned long val = simple_strtoul(buf, NULL, 10);
217 int nr = attr->index;
219 mutex_lock(&data->update_lock);
220 data->in_min[nr] = IN_TO_REG(val);
221 lm78_write_value(data, LM78_REG_IN_MIN(nr), data->in_min[nr]);
222 mutex_unlock(&data->update_lock);
223 return count;
226 static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
227 const char *buf, size_t count)
229 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
230 struct lm78_data *data = dev_get_drvdata(dev);
231 unsigned long val = simple_strtoul(buf, NULL, 10);
232 int nr = attr->index;
234 mutex_lock(&data->update_lock);
235 data->in_max[nr] = IN_TO_REG(val);
236 lm78_write_value(data, LM78_REG_IN_MAX(nr), data->in_max[nr]);
237 mutex_unlock(&data->update_lock);
238 return count;
241 #define show_in_offset(offset) \
242 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
243 show_in, NULL, offset); \
244 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
245 show_in_min, set_in_min, offset); \
246 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
247 show_in_max, set_in_max, offset);
249 show_in_offset(0);
250 show_in_offset(1);
251 show_in_offset(2);
252 show_in_offset(3);
253 show_in_offset(4);
254 show_in_offset(5);
255 show_in_offset(6);
257 /* Temperature */
258 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
259 char *buf)
261 struct lm78_data *data = lm78_update_device(dev);
262 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
265 static ssize_t show_temp_over(struct device *dev, struct device_attribute *da,
266 char *buf)
268 struct lm78_data *data = lm78_update_device(dev);
269 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
272 static ssize_t set_temp_over(struct device *dev, struct device_attribute *da,
273 const char *buf, size_t count)
275 struct lm78_data *data = dev_get_drvdata(dev);
276 long val = simple_strtol(buf, NULL, 10);
278 mutex_lock(&data->update_lock);
279 data->temp_over = TEMP_TO_REG(val);
280 lm78_write_value(data, LM78_REG_TEMP_OVER, data->temp_over);
281 mutex_unlock(&data->update_lock);
282 return count;
285 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *da,
286 char *buf)
288 struct lm78_data *data = lm78_update_device(dev);
289 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
292 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *da,
293 const char *buf, size_t count)
295 struct lm78_data *data = dev_get_drvdata(dev);
296 long val = simple_strtol(buf, NULL, 10);
298 mutex_lock(&data->update_lock);
299 data->temp_hyst = TEMP_TO_REG(val);
300 lm78_write_value(data, LM78_REG_TEMP_HYST, data->temp_hyst);
301 mutex_unlock(&data->update_lock);
302 return count;
305 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
306 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
307 show_temp_over, set_temp_over);
308 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
309 show_temp_hyst, set_temp_hyst);
311 /* 3 Fans */
312 static ssize_t show_fan(struct device *dev, struct device_attribute *da,
313 char *buf)
315 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
316 struct lm78_data *data = lm78_update_device(dev);
317 int nr = attr->index;
318 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
319 DIV_FROM_REG(data->fan_div[nr])) );
322 static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
323 char *buf)
325 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
326 struct lm78_data *data = lm78_update_device(dev);
327 int nr = attr->index;
328 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
329 DIV_FROM_REG(data->fan_div[nr])) );
332 static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
333 const char *buf, size_t count)
335 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
336 struct lm78_data *data = dev_get_drvdata(dev);
337 int nr = attr->index;
338 unsigned long val = simple_strtoul(buf, NULL, 10);
340 mutex_lock(&data->update_lock);
341 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
342 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
343 mutex_unlock(&data->update_lock);
344 return count;
347 static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
348 char *buf)
350 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
351 struct lm78_data *data = lm78_update_device(dev);
352 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. */
359 static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
360 const char *buf, size_t count)
362 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
363 struct lm78_data *data = dev_get_drvdata(dev);
364 int nr = attr->index;
365 unsigned long val = simple_strtoul(buf, NULL, 10);
366 unsigned long min;
367 u8 reg;
369 mutex_lock(&data->update_lock);
370 min = FAN_FROM_REG(data->fan_min[nr],
371 DIV_FROM_REG(data->fan_div[nr]));
373 switch (val) {
374 case 1: data->fan_div[nr] = 0; break;
375 case 2: data->fan_div[nr] = 1; break;
376 case 4: data->fan_div[nr] = 2; break;
377 case 8: data->fan_div[nr] = 3; break;
378 default:
379 dev_err(dev, "fan_div value %ld not "
380 "supported. Choose one of 1, 2, 4 or 8!\n", val);
381 mutex_unlock(&data->update_lock);
382 return -EINVAL;
385 reg = lm78_read_value(data, LM78_REG_VID_FANDIV);
386 switch (nr) {
387 case 0:
388 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
389 break;
390 case 1:
391 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
392 break;
394 lm78_write_value(data, LM78_REG_VID_FANDIV, reg);
396 data->fan_min[nr] =
397 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
398 lm78_write_value(data, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
399 mutex_unlock(&data->update_lock);
401 return count;
404 #define show_fan_offset(offset) \
405 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
406 show_fan, NULL, offset - 1); \
407 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
408 show_fan_min, set_fan_min, offset - 1);
410 show_fan_offset(1);
411 show_fan_offset(2);
412 show_fan_offset(3);
414 /* Fan 3 divisor is locked in H/W */
415 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
416 show_fan_div, set_fan_div, 0);
417 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
418 show_fan_div, set_fan_div, 1);
419 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2);
421 /* VID */
422 static ssize_t show_vid(struct device *dev, struct device_attribute *da,
423 char *buf)
425 struct lm78_data *data = lm78_update_device(dev);
426 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
428 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
430 /* Alarms */
431 static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
432 char *buf)
434 struct lm78_data *data = lm78_update_device(dev);
435 return sprintf(buf, "%u\n", data->alarms);
437 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
439 static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
440 char *buf)
442 struct lm78_data *data = lm78_update_device(dev);
443 int nr = to_sensor_dev_attr(da)->index;
444 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
446 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
447 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
448 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
449 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
450 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
451 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
452 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
453 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
454 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
455 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
456 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
458 /* This function is called when:
459 * lm78_driver is inserted (when this module is loaded), for each
460 available adapter
461 * when a new adapter is inserted (and lm78_driver is still present) */
462 static int lm78_attach_adapter(struct i2c_adapter *adapter)
464 if (!(adapter->class & I2C_CLASS_HWMON))
465 return 0;
466 return i2c_probe(adapter, &addr_data, lm78_detect);
469 static struct attribute *lm78_attributes[] = {
470 &sensor_dev_attr_in0_input.dev_attr.attr,
471 &sensor_dev_attr_in0_min.dev_attr.attr,
472 &sensor_dev_attr_in0_max.dev_attr.attr,
473 &sensor_dev_attr_in0_alarm.dev_attr.attr,
474 &sensor_dev_attr_in1_input.dev_attr.attr,
475 &sensor_dev_attr_in1_min.dev_attr.attr,
476 &sensor_dev_attr_in1_max.dev_attr.attr,
477 &sensor_dev_attr_in1_alarm.dev_attr.attr,
478 &sensor_dev_attr_in2_input.dev_attr.attr,
479 &sensor_dev_attr_in2_min.dev_attr.attr,
480 &sensor_dev_attr_in2_max.dev_attr.attr,
481 &sensor_dev_attr_in2_alarm.dev_attr.attr,
482 &sensor_dev_attr_in3_input.dev_attr.attr,
483 &sensor_dev_attr_in3_min.dev_attr.attr,
484 &sensor_dev_attr_in3_max.dev_attr.attr,
485 &sensor_dev_attr_in3_alarm.dev_attr.attr,
486 &sensor_dev_attr_in4_input.dev_attr.attr,
487 &sensor_dev_attr_in4_min.dev_attr.attr,
488 &sensor_dev_attr_in4_max.dev_attr.attr,
489 &sensor_dev_attr_in4_alarm.dev_attr.attr,
490 &sensor_dev_attr_in5_input.dev_attr.attr,
491 &sensor_dev_attr_in5_min.dev_attr.attr,
492 &sensor_dev_attr_in5_max.dev_attr.attr,
493 &sensor_dev_attr_in5_alarm.dev_attr.attr,
494 &sensor_dev_attr_in6_input.dev_attr.attr,
495 &sensor_dev_attr_in6_min.dev_attr.attr,
496 &sensor_dev_attr_in6_max.dev_attr.attr,
497 &sensor_dev_attr_in6_alarm.dev_attr.attr,
498 &dev_attr_temp1_input.attr,
499 &dev_attr_temp1_max.attr,
500 &dev_attr_temp1_max_hyst.attr,
501 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
502 &sensor_dev_attr_fan1_input.dev_attr.attr,
503 &sensor_dev_attr_fan1_min.dev_attr.attr,
504 &sensor_dev_attr_fan1_div.dev_attr.attr,
505 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
506 &sensor_dev_attr_fan2_input.dev_attr.attr,
507 &sensor_dev_attr_fan2_min.dev_attr.attr,
508 &sensor_dev_attr_fan2_div.dev_attr.attr,
509 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
510 &sensor_dev_attr_fan3_input.dev_attr.attr,
511 &sensor_dev_attr_fan3_min.dev_attr.attr,
512 &sensor_dev_attr_fan3_div.dev_attr.attr,
513 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
514 &dev_attr_alarms.attr,
515 &dev_attr_cpu0_vid.attr,
517 NULL
520 static const struct attribute_group lm78_group = {
521 .attrs = lm78_attributes,
524 /* I2C devices get this name attribute automatically, but for ISA devices
525 we must create it by ourselves. */
526 static ssize_t show_name(struct device *dev, struct device_attribute
527 *devattr, char *buf)
529 struct lm78_data *data = dev_get_drvdata(dev);
531 return sprintf(buf, "%s\n", data->client.name);
533 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
535 /* This function is called by i2c_probe */
536 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
538 int i, err;
539 struct i2c_client *new_client;
540 struct lm78_data *data;
541 const char *client_name = "";
543 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
544 err = -ENODEV;
545 goto ERROR1;
548 /* OK. For now, we presume we have a valid client. We now create the
549 client structure, even though we cannot fill it completely yet.
550 But it allows us to access lm78_{read,write}_value. */
552 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
553 err = -ENOMEM;
554 goto ERROR1;
557 new_client = &data->client;
558 i2c_set_clientdata(new_client, data);
559 new_client->addr = address;
560 new_client->adapter = adapter;
561 new_client->driver = &lm78_driver;
563 /* Now, we do the remaining detection. */
564 if (kind < 0) {
565 if (lm78_read_value(data, LM78_REG_CONFIG) & 0x80) {
566 err = -ENODEV;
567 goto ERROR2;
569 if (lm78_read_value(data, LM78_REG_I2C_ADDR) !=
570 address) {
571 err = -ENODEV;
572 goto ERROR2;
576 /* Determine the chip type. */
577 if (kind <= 0) {
578 i = lm78_read_value(data, LM78_REG_CHIPID);
579 if (i == 0x00 || i == 0x20 /* LM78 */
580 || i == 0x40) /* LM78-J */
581 kind = lm78;
582 else if ((i & 0xfe) == 0xc0)
583 kind = lm79;
584 else {
585 if (kind == 0)
586 dev_warn(&adapter->dev, "Ignoring 'force' "
587 "parameter for unknown chip at "
588 "adapter %d, address 0x%02x\n",
589 i2c_adapter_id(adapter), address);
590 err = -ENODEV;
591 goto ERROR2;
595 if (kind == lm78) {
596 client_name = "lm78";
597 } else if (kind == lm79) {
598 client_name = "lm79";
601 /* Fill in the remaining client fields and put into the global list */
602 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
603 data->type = kind;
605 /* Tell the I2C layer a new client has arrived */
606 if ((err = i2c_attach_client(new_client)))
607 goto ERROR2;
609 /* Initialize the LM78 chip */
610 lm78_init_device(data);
612 /* Register sysfs hooks */
613 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group)))
614 goto ERROR3;
616 data->hwmon_dev = hwmon_device_register(&new_client->dev);
617 if (IS_ERR(data->hwmon_dev)) {
618 err = PTR_ERR(data->hwmon_dev);
619 goto ERROR4;
622 return 0;
624 ERROR4:
625 sysfs_remove_group(&new_client->dev.kobj, &lm78_group);
626 ERROR3:
627 i2c_detach_client(new_client);
628 ERROR2:
629 kfree(data);
630 ERROR1:
631 return err;
634 static int lm78_detach_client(struct i2c_client *client)
636 struct lm78_data *data = i2c_get_clientdata(client);
637 int err;
639 hwmon_device_unregister(data->hwmon_dev);
640 sysfs_remove_group(&client->dev.kobj, &lm78_group);
642 if ((err = i2c_detach_client(client)))
643 return err;
645 kfree(data);
647 return 0;
650 static int __devinit lm78_isa_probe(struct platform_device *pdev)
652 int err;
653 struct lm78_data *data;
654 struct resource *res;
655 const char *name;
657 /* Reserve the ISA region */
658 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
659 if (!request_region(res->start, LM78_EXTENT, "lm78")) {
660 err = -EBUSY;
661 goto exit;
664 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
665 err = -ENOMEM;
666 goto exit_release_region;
668 mutex_init(&data->lock);
669 data->client.addr = res->start;
670 i2c_set_clientdata(&data->client, data);
671 platform_set_drvdata(pdev, data);
673 if (lm78_read_value(data, LM78_REG_CHIPID) & 0x80) {
674 data->type = lm79;
675 name = "lm79";
676 } else {
677 data->type = lm78;
678 name = "lm78";
680 strlcpy(data->client.name, name, I2C_NAME_SIZE);
682 /* Initialize the LM78 chip */
683 lm78_init_device(data);
685 /* Register sysfs hooks */
686 if ((err = sysfs_create_group(&pdev->dev.kobj, &lm78_group))
687 || (err = device_create_file(&pdev->dev, &dev_attr_name)))
688 goto exit_remove_files;
690 data->hwmon_dev = hwmon_device_register(&pdev->dev);
691 if (IS_ERR(data->hwmon_dev)) {
692 err = PTR_ERR(data->hwmon_dev);
693 goto exit_remove_files;
696 return 0;
698 exit_remove_files:
699 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
700 device_remove_file(&pdev->dev, &dev_attr_name);
701 kfree(data);
702 exit_release_region:
703 release_region(res->start, LM78_EXTENT);
704 exit:
705 return err;
708 static int __devexit lm78_isa_remove(struct platform_device *pdev)
710 struct lm78_data *data = platform_get_drvdata(pdev);
712 hwmon_device_unregister(data->hwmon_dev);
713 sysfs_remove_group(&pdev->dev.kobj, &lm78_group);
714 device_remove_file(&pdev->dev, &dev_attr_name);
715 release_region(data->client.addr, LM78_EXTENT);
716 kfree(data);
718 return 0;
721 /* The SMBus locks itself, but ISA access must be locked explicitly!
722 We don't want to lock the whole ISA bus, so we lock each client
723 separately.
724 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
725 would slow down the LM78 access and should not be necessary. */
726 static int lm78_read_value(struct lm78_data *data, u8 reg)
728 struct i2c_client *client = &data->client;
730 if (!client->driver) { /* ISA device */
731 int res;
732 mutex_lock(&data->lock);
733 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
734 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
735 mutex_unlock(&data->lock);
736 return res;
737 } else
738 return i2c_smbus_read_byte_data(client, reg);
741 /* The SMBus locks itself, but ISA access muse be locked explicitly!
742 We don't want to lock the whole ISA bus, so we lock each client
743 separately.
744 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
745 would slow down the LM78 access and should not be necessary.
746 There are some ugly typecasts here, but the good new is - they should
747 nowhere else be necessary! */
748 static int lm78_write_value(struct lm78_data *data, u8 reg, u8 value)
750 struct i2c_client *client = &data->client;
752 if (!client->driver) { /* ISA device */
753 mutex_lock(&data->lock);
754 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
755 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
756 mutex_unlock(&data->lock);
757 return 0;
758 } else
759 return i2c_smbus_write_byte_data(client, reg, value);
762 static void lm78_init_device(struct lm78_data *data)
764 u8 config;
765 int i;
767 /* Start monitoring */
768 config = lm78_read_value(data, LM78_REG_CONFIG);
769 if ((config & 0x09) != 0x01)
770 lm78_write_value(data, LM78_REG_CONFIG,
771 (config & 0xf7) | 0x01);
773 /* A few vars need to be filled upon startup */
774 for (i = 0; i < 3; i++) {
775 data->fan_min[i] = lm78_read_value(data,
776 LM78_REG_FAN_MIN(i));
779 mutex_init(&data->update_lock);
782 static struct lm78_data *lm78_update_device(struct device *dev)
784 struct lm78_data *data = dev_get_drvdata(dev);
785 int i;
787 mutex_lock(&data->update_lock);
789 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
790 || !data->valid) {
792 dev_dbg(dev, "Starting lm78 update\n");
794 for (i = 0; i <= 6; i++) {
795 data->in[i] =
796 lm78_read_value(data, LM78_REG_IN(i));
797 data->in_min[i] =
798 lm78_read_value(data, LM78_REG_IN_MIN(i));
799 data->in_max[i] =
800 lm78_read_value(data, LM78_REG_IN_MAX(i));
802 for (i = 0; i < 3; i++) {
803 data->fan[i] =
804 lm78_read_value(data, LM78_REG_FAN(i));
805 data->fan_min[i] =
806 lm78_read_value(data, LM78_REG_FAN_MIN(i));
808 data->temp = lm78_read_value(data, LM78_REG_TEMP);
809 data->temp_over =
810 lm78_read_value(data, LM78_REG_TEMP_OVER);
811 data->temp_hyst =
812 lm78_read_value(data, LM78_REG_TEMP_HYST);
813 i = lm78_read_value(data, LM78_REG_VID_FANDIV);
814 data->vid = i & 0x0f;
815 if (data->type == lm79)
816 data->vid |=
817 (lm78_read_value(data, LM78_REG_CHIPID) &
818 0x01) << 4;
819 else
820 data->vid |= 0x10;
821 data->fan_div[0] = (i >> 4) & 0x03;
822 data->fan_div[1] = i >> 6;
823 data->alarms = lm78_read_value(data, LM78_REG_ALARM1) +
824 (lm78_read_value(data, LM78_REG_ALARM2) << 8);
825 data->last_updated = jiffies;
826 data->valid = 1;
828 data->fan_div[2] = 1;
831 mutex_unlock(&data->update_lock);
833 return data;
836 /* return 1 if a supported chip is found, 0 otherwise */
837 static int __init lm78_isa_found(unsigned short address)
839 int val, save, found = 0;
841 if (!request_region(address, LM78_EXTENT, "lm78"))
842 return 0;
844 #define REALLY_SLOW_IO
845 /* We need the timeouts for at least some LM78-like
846 chips. But only if we read 'undefined' registers. */
847 val = inb_p(address + 1);
848 if (inb_p(address + 2) != val
849 || inb_p(address + 3) != val
850 || inb_p(address + 7) != val)
851 goto release;
852 #undef REALLY_SLOW_IO
854 /* We should be able to change the 7 LSB of the address port. The
855 MSB (busy flag) should be clear initially, set after the write. */
856 save = inb_p(address + LM78_ADDR_REG_OFFSET);
857 if (save & 0x80)
858 goto release;
859 val = ~save & 0x7f;
860 outb_p(val, address + LM78_ADDR_REG_OFFSET);
861 if (inb_p(address + LM78_ADDR_REG_OFFSET) != (val | 0x80)) {
862 outb_p(save, address + LM78_ADDR_REG_OFFSET);
863 goto release;
866 /* We found a device, now see if it could be an LM78 */
867 outb_p(LM78_REG_CONFIG, address + LM78_ADDR_REG_OFFSET);
868 val = inb_p(address + LM78_DATA_REG_OFFSET);
869 if (val & 0x80)
870 goto release;
871 outb_p(LM78_REG_I2C_ADDR, address + LM78_ADDR_REG_OFFSET);
872 val = inb_p(address + LM78_DATA_REG_OFFSET);
873 if (val < 0x03 || val > 0x77) /* Not a valid I2C address */
874 goto release;
876 /* The busy flag should be clear again */
877 if (inb_p(address + LM78_ADDR_REG_OFFSET) & 0x80)
878 goto release;
880 /* Explicitly prevent the misdetection of Winbond chips */
881 outb_p(0x4f, address + LM78_ADDR_REG_OFFSET);
882 val = inb_p(address + LM78_DATA_REG_OFFSET);
883 if (val == 0xa3 || val == 0x5c)
884 goto release;
886 /* Explicitly prevent the misdetection of ITE chips */
887 outb_p(0x58, address + LM78_ADDR_REG_OFFSET);
888 val = inb_p(address + LM78_DATA_REG_OFFSET);
889 if (val == 0x90)
890 goto release;
892 /* Determine the chip type */
893 outb_p(LM78_REG_CHIPID, address + LM78_ADDR_REG_OFFSET);
894 val = inb_p(address + LM78_DATA_REG_OFFSET);
895 if (val == 0x00 || val == 0x20 /* LM78 */
896 || val == 0x40 /* LM78-J */
897 || (val & 0xfe) == 0xc0) /* LM79 */
898 found = 1;
900 if (found)
901 pr_info("lm78: Found an %s chip at %#x\n",
902 val & 0x80 ? "LM79" : "LM78", (int)address);
904 release:
905 release_region(address, LM78_EXTENT);
906 return found;
909 static int __init lm78_isa_device_add(unsigned short address)
911 struct resource res = {
912 .start = address,
913 .end = address + LM78_EXTENT - 1,
914 .name = "lm78",
915 .flags = IORESOURCE_IO,
917 int err;
919 pdev = platform_device_alloc("lm78", address);
920 if (!pdev) {
921 err = -ENOMEM;
922 printk(KERN_ERR "lm78: Device allocation failed\n");
923 goto exit;
926 err = platform_device_add_resources(pdev, &res, 1);
927 if (err) {
928 printk(KERN_ERR "lm78: Device resource addition failed "
929 "(%d)\n", err);
930 goto exit_device_put;
933 err = platform_device_add(pdev);
934 if (err) {
935 printk(KERN_ERR "lm78: Device addition failed (%d)\n",
936 err);
937 goto exit_device_put;
940 return 0;
942 exit_device_put:
943 platform_device_put(pdev);
944 exit:
945 pdev = NULL;
946 return err;
949 static int __init sm_lm78_init(void)
951 int res;
953 res = i2c_add_driver(&lm78_driver);
954 if (res)
955 goto exit;
957 if (lm78_isa_found(isa_address)) {
958 res = platform_driver_register(&lm78_isa_driver);
959 if (res)
960 goto exit_unreg_i2c_driver;
962 /* Sets global pdev as a side effect */
963 res = lm78_isa_device_add(isa_address);
964 if (res)
965 goto exit_unreg_isa_driver;
968 return 0;
970 exit_unreg_isa_driver:
971 platform_driver_unregister(&lm78_isa_driver);
972 exit_unreg_i2c_driver:
973 i2c_del_driver(&lm78_driver);
974 exit:
975 return res;
978 static void __exit sm_lm78_exit(void)
980 if (pdev) {
981 platform_device_unregister(pdev);
982 platform_driver_unregister(&lm78_isa_driver);
984 i2c_del_driver(&lm78_driver);
989 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
990 MODULE_DESCRIPTION("LM78/LM79 driver");
991 MODULE_LICENSE("GPL");
993 module_init(sm_lm78_init);
994 module_exit(sm_lm78_exit);