[PATCH] hwmon: Use attribute arrays in f71805f
[wandboard.git] / drivers / hwmon / f71805f.c
blob8e810f05d0aecc73772c32a5c894fb9045efcaa9
1 /*
2 * f71805f.c - driver for the Fintek F71805F/FG Super-I/O chip integrated
3 * hardware monitoring features
4 * Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
6 * The F71805F/FG is a LPC Super-I/O chip made by Fintek. It integrates
7 * complete hardware monitoring features: voltage, fan and temperature
8 * sensors, and manual and automatic fan speed control.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/platform_device.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <asm/io.h>
35 static struct platform_device *pdev;
37 #define DRVNAME "f71805f"
40 * Super-I/O constants and functions
43 #define F71805F_LD_HWM 0x04
45 #define SIO_REG_LDSEL 0x07 /* Logical device select */
46 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
47 #define SIO_REG_DEVREV 0x22 /* Device revision */
48 #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
49 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
50 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
52 #define SIO_FINTEK_ID 0x1934
53 #define SIO_F71805F_ID 0x0406
55 static inline int
56 superio_inb(int base, int reg)
58 outb(reg, base);
59 return inb(base + 1);
62 static int
63 superio_inw(int base, int reg)
65 int val;
66 outb(reg++, base);
67 val = inb(base + 1) << 8;
68 outb(reg, base);
69 val |= inb(base + 1);
70 return val;
73 static inline void
74 superio_select(int base, int ld)
76 outb(SIO_REG_LDSEL, base);
77 outb(ld, base + 1);
80 static inline void
81 superio_enter(int base)
83 outb(0x87, base);
84 outb(0x87, base);
87 static inline void
88 superio_exit(int base)
90 outb(0xaa, base);
94 * ISA constants
97 #define REGION_LENGTH 2
98 #define ADDR_REG_OFFSET 0
99 #define DATA_REG_OFFSET 1
101 static struct resource f71805f_resource __initdata = {
102 .flags = IORESOURCE_IO,
106 * Registers
109 /* in nr from 0 to 8 (8-bit values) */
110 #define F71805F_REG_IN(nr) (0x10 + (nr))
111 #define F71805F_REG_IN_HIGH(nr) (0x40 + 2 * (nr))
112 #define F71805F_REG_IN_LOW(nr) (0x41 + 2 * (nr))
113 /* fan nr from 0 to 2 (12-bit values, two registers) */
114 #define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
115 #define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
116 #define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
117 /* temp nr from 0 to 2 (8-bit values) */
118 #define F71805F_REG_TEMP(nr) (0x1B + (nr))
119 #define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
120 #define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
121 #define F71805F_REG_TEMP_MODE 0x01
123 #define F71805F_REG_START 0x00
124 /* status nr from 0 to 2 */
125 #define F71805F_REG_STATUS(nr) (0x36 + (nr))
128 * Data structures and manipulation thereof
131 struct f71805f_data {
132 unsigned short addr;
133 const char *name;
134 struct semaphore lock;
135 struct class_device *class_dev;
137 struct semaphore update_lock;
138 char valid; /* !=0 if following fields are valid */
139 unsigned long last_updated; /* In jiffies */
140 unsigned long last_limits; /* In jiffies */
142 /* Register values */
143 u8 in[9];
144 u8 in_high[9];
145 u8 in_low[9];
146 u16 fan[3];
147 u16 fan_low[3];
148 u8 fan_enabled; /* Read once at init time */
149 u8 temp[3];
150 u8 temp_high[3];
151 u8 temp_hyst[3];
152 u8 temp_mode;
153 u8 alarms[3];
156 static inline long in_from_reg(u8 reg)
158 return (reg * 8);
161 /* The 2 least significant bits are not used */
162 static inline u8 in_to_reg(long val)
164 if (val <= 0)
165 return 0;
166 if (val >= 2016)
167 return 0xfc;
168 return (((val + 16) / 32) << 2);
171 /* in0 is downscaled by a factor 2 internally */
172 static inline long in0_from_reg(u8 reg)
174 return (reg * 16);
177 static inline u8 in0_to_reg(long val)
179 if (val <= 0)
180 return 0;
181 if (val >= 4032)
182 return 0xfc;
183 return (((val + 32) / 64) << 2);
186 /* The 4 most significant bits are not used */
187 static inline long fan_from_reg(u16 reg)
189 reg &= 0xfff;
190 if (!reg || reg == 0xfff)
191 return 0;
192 return (1500000 / reg);
195 static inline u16 fan_to_reg(long rpm)
197 /* If the low limit is set below what the chip can measure,
198 store the largest possible 12-bit value in the registers,
199 so that no alarm will ever trigger. */
200 if (rpm < 367)
201 return 0xfff;
202 return (1500000 / rpm);
205 static inline long temp_from_reg(u8 reg)
207 return (reg * 1000);
210 static inline u8 temp_to_reg(long val)
212 if (val < 0)
213 val = 0;
214 else if (val > 1000 * 0xff)
215 val = 0xff;
216 return ((val + 500) / 1000);
220 * Device I/O access
223 static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
225 u8 val;
227 down(&data->lock);
228 outb(reg, data->addr + ADDR_REG_OFFSET);
229 val = inb(data->addr + DATA_REG_OFFSET);
230 up(&data->lock);
232 return val;
235 static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
237 down(&data->lock);
238 outb(reg, data->addr + ADDR_REG_OFFSET);
239 outb(val, data->addr + DATA_REG_OFFSET);
240 up(&data->lock);
243 /* It is important to read the MSB first, because doing so latches the
244 value of the LSB, so we are sure both bytes belong to the same value. */
245 static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
247 u16 val;
249 down(&data->lock);
250 outb(reg, data->addr + ADDR_REG_OFFSET);
251 val = inb(data->addr + DATA_REG_OFFSET) << 8;
252 outb(++reg, data->addr + ADDR_REG_OFFSET);
253 val |= inb(data->addr + DATA_REG_OFFSET);
254 up(&data->lock);
256 return val;
259 static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
261 down(&data->lock);
262 outb(reg, data->addr + ADDR_REG_OFFSET);
263 outb(val >> 8, data->addr + DATA_REG_OFFSET);
264 outb(++reg, data->addr + ADDR_REG_OFFSET);
265 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
266 up(&data->lock);
269 static struct f71805f_data *f71805f_update_device(struct device *dev)
271 struct f71805f_data *data = dev_get_drvdata(dev);
272 int nr;
274 down(&data->update_lock);
276 /* Limit registers cache is refreshed after 60 seconds */
277 if (time_after(jiffies, data->last_updated + 60 * HZ)
278 || !data->valid) {
279 for (nr = 0; nr < 9; nr++) {
280 data->in_high[nr] = f71805f_read8(data,
281 F71805F_REG_IN_HIGH(nr));
282 data->in_low[nr] = f71805f_read8(data,
283 F71805F_REG_IN_LOW(nr));
285 for (nr = 0; nr < 3; nr++) {
286 if (data->fan_enabled & (1 << nr))
287 data->fan_low[nr] = f71805f_read16(data,
288 F71805F_REG_FAN_LOW(nr));
290 for (nr = 0; nr < 3; nr++) {
291 data->temp_high[nr] = f71805f_read8(data,
292 F71805F_REG_TEMP_HIGH(nr));
293 data->temp_hyst[nr] = f71805f_read8(data,
294 F71805F_REG_TEMP_HYST(nr));
296 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
298 data->last_limits = jiffies;
301 /* Measurement registers cache is refreshed after 1 second */
302 if (time_after(jiffies, data->last_updated + HZ)
303 || !data->valid) {
304 for (nr = 0; nr < 9; nr++) {
305 data->in[nr] = f71805f_read8(data,
306 F71805F_REG_IN(nr));
308 for (nr = 0; nr < 3; nr++) {
309 if (data->fan_enabled & (1 << nr))
310 data->fan[nr] = f71805f_read16(data,
311 F71805F_REG_FAN(nr));
313 for (nr = 0; nr < 3; nr++) {
314 data->temp[nr] = f71805f_read8(data,
315 F71805F_REG_TEMP(nr));
317 for (nr = 0; nr < 3; nr++) {
318 data->alarms[nr] = f71805f_read8(data,
319 F71805F_REG_STATUS(nr));
322 data->last_updated = jiffies;
323 data->valid = 1;
326 up(&data->update_lock);
328 return data;
332 * Sysfs interface
335 static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
336 char *buf)
338 struct f71805f_data *data = f71805f_update_device(dev);
340 return sprintf(buf, "%ld\n", in0_from_reg(data->in[0]));
343 static ssize_t show_in0_max(struct device *dev, struct device_attribute
344 *devattr, char *buf)
346 struct f71805f_data *data = f71805f_update_device(dev);
348 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0]));
351 static ssize_t show_in0_min(struct device *dev, struct device_attribute
352 *devattr, char *buf)
354 struct f71805f_data *data = f71805f_update_device(dev);
356 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0]));
359 static ssize_t set_in0_max(struct device *dev, struct device_attribute
360 *devattr, const char *buf, size_t count)
362 struct f71805f_data *data = dev_get_drvdata(dev);
363 long val = simple_strtol(buf, NULL, 10);
365 down(&data->update_lock);
366 data->in_high[0] = in0_to_reg(val);
367 f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]);
368 up(&data->update_lock);
370 return count;
373 static ssize_t set_in0_min(struct device *dev, struct device_attribute
374 *devattr, const char *buf, size_t count)
376 struct f71805f_data *data = dev_get_drvdata(dev);
377 long val = simple_strtol(buf, NULL, 10);
379 down(&data->update_lock);
380 data->in_low[0] = in0_to_reg(val);
381 f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]);
382 up(&data->update_lock);
384 return count;
387 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
388 char *buf)
390 struct f71805f_data *data = f71805f_update_device(dev);
391 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
392 int nr = attr->index;
394 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
397 static ssize_t show_in_max(struct device *dev, struct device_attribute
398 *devattr, char *buf)
400 struct f71805f_data *data = f71805f_update_device(dev);
401 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
402 int nr = attr->index;
404 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
407 static ssize_t show_in_min(struct device *dev, struct device_attribute
408 *devattr, char *buf)
410 struct f71805f_data *data = f71805f_update_device(dev);
411 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
412 int nr = attr->index;
414 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
417 static ssize_t set_in_max(struct device *dev, struct device_attribute
418 *devattr, const char *buf, size_t count)
420 struct f71805f_data *data = dev_get_drvdata(dev);
421 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
422 int nr = attr->index;
423 long val = simple_strtol(buf, NULL, 10);
425 down(&data->update_lock);
426 data->in_high[nr] = in_to_reg(val);
427 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
428 up(&data->update_lock);
430 return count;
433 static ssize_t set_in_min(struct device *dev, struct device_attribute
434 *devattr, const char *buf, size_t count)
436 struct f71805f_data *data = dev_get_drvdata(dev);
437 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
438 int nr = attr->index;
439 long val = simple_strtol(buf, NULL, 10);
441 down(&data->update_lock);
442 data->in_low[nr] = in_to_reg(val);
443 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
444 up(&data->update_lock);
446 return count;
449 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
450 char *buf)
452 struct f71805f_data *data = f71805f_update_device(dev);
453 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
454 int nr = attr->index;
456 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
459 static ssize_t show_fan_min(struct device *dev, struct device_attribute
460 *devattr, char *buf)
462 struct f71805f_data *data = f71805f_update_device(dev);
463 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
464 int nr = attr->index;
466 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
469 static ssize_t set_fan_min(struct device *dev, struct device_attribute
470 *devattr, const char *buf, size_t count)
472 struct f71805f_data *data = dev_get_drvdata(dev);
473 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
474 int nr = attr->index;
475 long val = simple_strtol(buf, NULL, 10);
477 down(&data->update_lock);
478 data->fan_low[nr] = fan_to_reg(val);
479 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
480 up(&data->update_lock);
482 return count;
485 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
486 char *buf)
488 struct f71805f_data *data = f71805f_update_device(dev);
489 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
490 int nr = attr->index;
492 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
495 static ssize_t show_temp_max(struct device *dev, struct device_attribute
496 *devattr, char *buf)
498 struct f71805f_data *data = f71805f_update_device(dev);
499 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
500 int nr = attr->index;
502 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
505 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
506 *devattr, char *buf)
508 struct f71805f_data *data = f71805f_update_device(dev);
509 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
510 int nr = attr->index;
512 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
515 static ssize_t show_temp_type(struct device *dev, struct device_attribute
516 *devattr, char *buf)
518 struct f71805f_data *data = f71805f_update_device(dev);
519 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
520 int nr = attr->index;
522 /* 3 is diode, 4 is thermistor */
523 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
526 static ssize_t set_temp_max(struct device *dev, struct device_attribute
527 *devattr, const char *buf, size_t count)
529 struct f71805f_data *data = dev_get_drvdata(dev);
530 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
531 int nr = attr->index;
532 long val = simple_strtol(buf, NULL, 10);
534 down(&data->update_lock);
535 data->temp_high[nr] = temp_to_reg(val);
536 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
537 up(&data->update_lock);
539 return count;
542 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
543 *devattr, const char *buf, size_t count)
545 struct f71805f_data *data = dev_get_drvdata(dev);
546 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
547 int nr = attr->index;
548 long val = simple_strtol(buf, NULL, 10);
550 down(&data->update_lock);
551 data->temp_hyst[nr] = temp_to_reg(val);
552 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
553 up(&data->update_lock);
555 return count;
558 static ssize_t show_alarms_in(struct device *dev, struct device_attribute
559 *devattr, char *buf)
561 struct f71805f_data *data = f71805f_update_device(dev);
563 return sprintf(buf, "%d\n", data->alarms[0] |
564 ((data->alarms[1] & 0x01) << 8));
567 static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
568 *devattr, char *buf)
570 struct f71805f_data *data = f71805f_update_device(dev);
572 return sprintf(buf, "%d\n", data->alarms[2] & 0x07);
575 static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
576 *devattr, char *buf)
578 struct f71805f_data *data = f71805f_update_device(dev);
580 return sprintf(buf, "%d\n", (data->alarms[1] >> 3) & 0x07);
583 static ssize_t show_name(struct device *dev, struct device_attribute
584 *devattr, char *buf)
586 struct f71805f_data *data = dev_get_drvdata(dev);
588 return sprintf(buf, "%s\n", data->name);
591 static struct device_attribute f71805f_dev_attr[] = {
592 __ATTR(in0_input, S_IRUGO, show_in0, NULL),
593 __ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max),
594 __ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min),
595 __ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL),
596 __ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL),
597 __ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL),
598 __ATTR(name, S_IRUGO, show_name, NULL),
601 static struct sensor_device_attribute f71805f_sensor_attr[] = {
602 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
603 SENSOR_ATTR(in1_max, S_IRUGO | S_IWUSR,
604 show_in_max, set_in_max, 1),
605 SENSOR_ATTR(in1_min, S_IRUGO | S_IWUSR,
606 show_in_min, set_in_min, 1),
607 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
608 SENSOR_ATTR(in2_max, S_IRUGO | S_IWUSR,
609 show_in_max, set_in_max, 2),
610 SENSOR_ATTR(in2_min, S_IRUGO | S_IWUSR,
611 show_in_min, set_in_min, 2),
612 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
613 SENSOR_ATTR(in3_max, S_IRUGO | S_IWUSR,
614 show_in_max, set_in_max, 3),
615 SENSOR_ATTR(in3_min, S_IRUGO | S_IWUSR,
616 show_in_min, set_in_min, 3),
617 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
618 SENSOR_ATTR(in4_max, S_IRUGO | S_IWUSR,
619 show_in_max, set_in_max, 4),
620 SENSOR_ATTR(in4_min, S_IRUGO | S_IWUSR,
621 show_in_min, set_in_min, 4),
622 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
623 SENSOR_ATTR(in5_max, S_IRUGO | S_IWUSR,
624 show_in_max, set_in_max, 5),
625 SENSOR_ATTR(in5_min, S_IRUGO | S_IWUSR,
626 show_in_min, set_in_min, 5),
627 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
628 SENSOR_ATTR(in6_max, S_IRUGO | S_IWUSR,
629 show_in_max, set_in_max, 6),
630 SENSOR_ATTR(in6_min, S_IRUGO | S_IWUSR,
631 show_in_min, set_in_min, 6),
632 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
633 SENSOR_ATTR(in7_max, S_IRUGO | S_IWUSR,
634 show_in_max, set_in_max, 7),
635 SENSOR_ATTR(in7_min, S_IRUGO | S_IWUSR,
636 show_in_min, set_in_min, 7),
637 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
638 SENSOR_ATTR(in8_max, S_IRUGO | S_IWUSR,
639 show_in_max, set_in_max, 8),
640 SENSOR_ATTR(in8_min, S_IRUGO | S_IWUSR,
641 show_in_min, set_in_min, 8),
643 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
644 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
645 show_temp_max, set_temp_max, 0),
646 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
647 show_temp_hyst, set_temp_hyst, 0),
648 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
649 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
650 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
651 show_temp_max, set_temp_max, 1),
652 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
653 show_temp_hyst, set_temp_hyst, 1),
654 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
655 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
656 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
657 show_temp_max, set_temp_max, 2),
658 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
659 show_temp_hyst, set_temp_hyst, 2),
660 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
663 static struct sensor_device_attribute f71805f_fan_attr[] = {
664 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
665 SENSOR_ATTR(fan1_min, S_IRUGO | S_IWUSR,
666 show_fan_min, set_fan_min, 0),
667 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
668 SENSOR_ATTR(fan2_min, S_IRUGO | S_IWUSR,
669 show_fan_min, set_fan_min, 1),
670 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
671 SENSOR_ATTR(fan3_min, S_IRUGO | S_IWUSR,
672 show_fan_min, set_fan_min, 2),
676 * Device registration and initialization
679 static void __devinit f71805f_init_device(struct f71805f_data *data)
681 u8 reg;
682 int i;
684 reg = f71805f_read8(data, F71805F_REG_START);
685 if ((reg & 0x41) != 0x01) {
686 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
687 "operations\n");
688 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
691 /* Fan monitoring can be disabled. If it is, we won't be polling
692 the register values, and won't create the related sysfs files. */
693 for (i = 0; i < 3; i++) {
694 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(i));
695 if (!(reg & 0x80))
696 data->fan_enabled |= (1 << i);
700 static int __devinit f71805f_probe(struct platform_device *pdev)
702 struct f71805f_data *data;
703 struct resource *res;
704 int i, err;
706 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
707 err = -ENOMEM;
708 printk(KERN_ERR DRVNAME ": Out of memory\n");
709 goto exit;
712 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
713 data->addr = res->start;
714 init_MUTEX(&data->lock);
715 data->name = "f71805f";
716 init_MUTEX(&data->update_lock);
718 platform_set_drvdata(pdev, data);
720 data->class_dev = hwmon_device_register(&pdev->dev);
721 if (IS_ERR(data->class_dev)) {
722 err = PTR_ERR(data->class_dev);
723 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
724 goto exit_free;
727 /* Initialize the F71805F chip */
728 f71805f_init_device(data);
730 /* Register sysfs interface files */
731 for (i = 0; i < ARRAY_SIZE(f71805f_dev_attr); i++) {
732 err = device_create_file(&pdev->dev, &f71805f_dev_attr[i]);
733 if (err)
734 goto exit_class;
736 for (i = 0; i < ARRAY_SIZE(f71805f_sensor_attr); i++) {
737 err = device_create_file(&pdev->dev,
738 &f71805f_sensor_attr[i].dev_attr);
739 if (err)
740 goto exit_class;
742 for (i = 0; i < ARRAY_SIZE(f71805f_fan_attr); i++) {
743 if (!(data->fan_enabled & (1 << (i / 2))))
744 continue;
745 err = device_create_file(&pdev->dev,
746 &f71805f_fan_attr[i].dev_attr);
747 if (err)
748 goto exit_class;
751 return 0;
753 exit_class:
754 dev_err(&pdev->dev, "Sysfs interface creation failed\n");
755 hwmon_device_unregister(data->class_dev);
756 exit_free:
757 kfree(data);
758 exit:
759 return err;
762 static int __devexit f71805f_remove(struct platform_device *pdev)
764 struct f71805f_data *data = platform_get_drvdata(pdev);
766 platform_set_drvdata(pdev, NULL);
767 hwmon_device_unregister(data->class_dev);
768 kfree(data);
770 return 0;
773 static struct platform_driver f71805f_driver = {
774 .driver = {
775 .owner = THIS_MODULE,
776 .name = DRVNAME,
778 .probe = f71805f_probe,
779 .remove = __devexit_p(f71805f_remove),
782 static int __init f71805f_device_add(unsigned short address)
784 int err;
786 pdev = platform_device_alloc(DRVNAME, address);
787 if (!pdev) {
788 err = -ENOMEM;
789 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
790 goto exit;
793 f71805f_resource.start = address;
794 f71805f_resource.end = address + REGION_LENGTH - 1;
795 f71805f_resource.name = pdev->name;
796 err = platform_device_add_resources(pdev, &f71805f_resource, 1);
797 if (err) {
798 printk(KERN_ERR DRVNAME ": Device resource addition failed "
799 "(%d)\n", err);
800 goto exit_device_put;
803 err = platform_device_add(pdev);
804 if (err) {
805 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
806 err);
807 goto exit_device_put;
810 return 0;
812 exit_device_put:
813 platform_device_put(pdev);
814 exit:
815 return err;
818 static int __init f71805f_find(int sioaddr, unsigned short *address)
820 int err = -ENODEV;
821 u16 devid;
823 superio_enter(sioaddr);
825 devid = superio_inw(sioaddr, SIO_REG_MANID);
826 if (devid != SIO_FINTEK_ID)
827 goto exit;
829 devid = superio_inw(sioaddr, SIO_REG_DEVID);
830 if (devid != SIO_F71805F_ID) {
831 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
832 "skipping\n");
833 goto exit;
836 superio_select(sioaddr, F71805F_LD_HWM);
837 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
838 printk(KERN_WARNING DRVNAME ": Device not activated, "
839 "skipping\n");
840 goto exit;
843 *address = superio_inw(sioaddr, SIO_REG_ADDR);
844 if (*address == 0) {
845 printk(KERN_WARNING DRVNAME ": Base address not set, "
846 "skipping\n");
847 goto exit;
850 err = 0;
851 printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n",
852 *address, superio_inb(sioaddr, SIO_REG_DEVREV));
854 exit:
855 superio_exit(sioaddr);
856 return err;
859 static int __init f71805f_init(void)
861 int err;
862 unsigned short address;
864 if (f71805f_find(0x2e, &address)
865 && f71805f_find(0x4e, &address))
866 return -ENODEV;
868 err = platform_driver_register(&f71805f_driver);
869 if (err)
870 goto exit;
872 /* Sets global pdev as a side effect */
873 err = f71805f_device_add(address);
874 if (err)
875 goto exit_driver;
877 return 0;
879 exit_driver:
880 platform_driver_unregister(&f71805f_driver);
881 exit:
882 return err;
885 static void __exit f71805f_exit(void)
887 platform_device_unregister(pdev);
888 platform_driver_unregister(&f71805f_driver);
891 MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
892 MODULE_LICENSE("GPL");
893 MODULE_DESCRIPTION("F71805F hardware monitoring driver");
895 module_init(f71805f_init);
896 module_exit(f71805f_exit);