Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / pc87427.c
blob81a05cd1a5121afd608a003abbf18dacdaf35fc9
1 /*
2 * pc87427.c - hardware monitoring driver for the
3 * National Semiconductor PC87427 Super-I/O chip
4 * Copyright (C) 2006, 2008, 2010 Jean Delvare <jdelvare@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * Supports the following chips:
17 * Chip #vin #fan #pwm #temp devid
18 * PC87427 - 8 4 6 0xF2
20 * This driver assumes that no more than one chip is present.
21 * Only fans are fully supported so far. Temperatures are in read-only
22 * mode, and voltages aren't supported at all.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/jiffies.h>
31 #include <linux/platform_device.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/err.h>
35 #include <linux/mutex.h>
36 #include <linux/sysfs.h>
37 #include <linux/ioport.h>
38 #include <linux/acpi.h>
39 #include <linux/io.h>
41 static unsigned short force_id;
42 module_param(force_id, ushort, 0);
43 MODULE_PARM_DESC(force_id, "Override the detected device ID");
45 static struct platform_device *pdev;
47 #define DRVNAME "pc87427"
50 * The lock mutex protects both the I/O accesses (needed because the
51 * device is using banked registers) and the register cache (needed to keep
52 * the data in the registers and the cache in sync at any time).
54 struct pc87427_data {
55 struct device *hwmon_dev;
56 struct mutex lock;
57 int address[2];
58 const char *name;
60 unsigned long last_updated; /* in jiffies */
61 u8 fan_enabled; /* bit vector */
62 u16 fan[8]; /* register values */
63 u16 fan_min[8]; /* register values */
64 u8 fan_status[8]; /* register values */
66 u8 pwm_enabled; /* bit vector */
67 u8 pwm_auto_ok; /* bit vector */
68 u8 pwm_enable[4]; /* register values */
69 u8 pwm[4]; /* register values */
71 u8 temp_enabled; /* bit vector */
72 s16 temp[6]; /* register values */
73 s8 temp_min[6]; /* register values */
74 s8 temp_max[6]; /* register values */
75 s8 temp_crit[6]; /* register values */
76 u8 temp_status[6]; /* register values */
77 u8 temp_type[6]; /* register values */
80 struct pc87427_sio_data {
81 unsigned short address[2];
82 u8 has_fanin;
83 u8 has_fanout;
87 * Super-I/O registers and operations
90 #define SIOREG_LDSEL 0x07 /* Logical device select */
91 #define SIOREG_DEVID 0x20 /* Device ID */
92 #define SIOREG_CF2 0x22 /* Configuration 2 */
93 #define SIOREG_CF3 0x23 /* Configuration 3 */
94 #define SIOREG_CF4 0x24 /* Configuration 4 */
95 #define SIOREG_CF5 0x25 /* Configuration 5 */
96 #define SIOREG_CFB 0x2B /* Configuration B */
97 #define SIOREG_CFC 0x2C /* Configuration C */
98 #define SIOREG_CFD 0x2D /* Configuration D */
99 #define SIOREG_ACT 0x30 /* Device activation */
100 #define SIOREG_MAP 0x50 /* I/O or memory mapping */
101 #define SIOREG_IOBASE 0x60 /* I/O base address */
103 static const u8 logdev[2] = { 0x09, 0x14 };
104 static const char *logdev_str[2] = { DRVNAME " FMC", DRVNAME " HMC" };
105 #define LD_FAN 0
106 #define LD_IN 1
107 #define LD_TEMP 1
109 static inline int superio_enter(int sioaddr)
111 if (!request_muxed_region(sioaddr, 2, DRVNAME))
112 return -EBUSY;
113 return 0;
116 static inline void superio_outb(int sioaddr, int reg, int val)
118 outb(reg, sioaddr);
119 outb(val, sioaddr + 1);
122 static inline int superio_inb(int sioaddr, int reg)
124 outb(reg, sioaddr);
125 return inb(sioaddr + 1);
128 static inline void superio_exit(int sioaddr)
130 outb(0x02, sioaddr);
131 outb(0x02, sioaddr + 1);
132 release_region(sioaddr, 2);
136 * Logical devices
139 #define REGION_LENGTH 32
140 #define PC87427_REG_BANK 0x0f
141 #define BANK_FM(nr) (nr)
142 #define BANK_FT(nr) (0x08 + (nr))
143 #define BANK_FC(nr) (0x10 + (nr) * 2)
144 #define BANK_TM(nr) (nr)
145 #define BANK_VM(nr) (0x08 + (nr))
148 * I/O access functions
151 /* ldi is the logical device index */
152 static inline int pc87427_read8(struct pc87427_data *data, u8 ldi, u8 reg)
154 return inb(data->address[ldi] + reg);
157 /* Must be called with data->lock held, except during init */
158 static inline int pc87427_read8_bank(struct pc87427_data *data, u8 ldi,
159 u8 bank, u8 reg)
161 outb(bank, data->address[ldi] + PC87427_REG_BANK);
162 return inb(data->address[ldi] + reg);
165 /* Must be called with data->lock held, except during init */
166 static inline void pc87427_write8_bank(struct pc87427_data *data, u8 ldi,
167 u8 bank, u8 reg, u8 value)
169 outb(bank, data->address[ldi] + PC87427_REG_BANK);
170 outb(value, data->address[ldi] + reg);
174 * Fan registers and conversions
177 /* fan data registers are 16-bit wide */
178 #define PC87427_REG_FAN 0x12
179 #define PC87427_REG_FAN_MIN 0x14
180 #define PC87427_REG_FAN_STATUS 0x10
182 #define FAN_STATUS_STALL (1 << 3)
183 #define FAN_STATUS_LOSPD (1 << 1)
184 #define FAN_STATUS_MONEN (1 << 0)
187 * Dedicated function to read all registers related to a given fan input.
188 * This saves us quite a few locks and bank selections.
189 * Must be called with data->lock held.
190 * nr is from 0 to 7
192 static void pc87427_readall_fan(struct pc87427_data *data, u8 nr)
194 int iobase = data->address[LD_FAN];
196 outb(BANK_FM(nr), iobase + PC87427_REG_BANK);
197 data->fan[nr] = inw(iobase + PC87427_REG_FAN);
198 data->fan_min[nr] = inw(iobase + PC87427_REG_FAN_MIN);
199 data->fan_status[nr] = inb(iobase + PC87427_REG_FAN_STATUS);
200 /* Clear fan alarm bits */
201 outb(data->fan_status[nr], iobase + PC87427_REG_FAN_STATUS);
205 * The 2 LSB of fan speed registers are used for something different.
206 * The actual 2 LSB of the measurements are not available.
208 static inline unsigned long fan_from_reg(u16 reg)
210 reg &= 0xfffc;
211 if (reg == 0x0000 || reg == 0xfffc)
212 return 0;
213 return 5400000UL / reg;
216 /* The 2 LSB of the fan speed limit registers are not significant. */
217 static inline u16 fan_to_reg(unsigned long val)
219 if (val < 83UL)
220 return 0xffff;
221 if (val >= 1350000UL)
222 return 0x0004;
223 return ((1350000UL + val / 2) / val) << 2;
227 * PWM registers and conversions
230 #define PC87427_REG_PWM_ENABLE 0x10
231 #define PC87427_REG_PWM_DUTY 0x12
233 #define PWM_ENABLE_MODE_MASK (7 << 4)
234 #define PWM_ENABLE_CTLEN (1 << 0)
236 #define PWM_MODE_MANUAL (0 << 4)
237 #define PWM_MODE_AUTO (1 << 4)
238 #define PWM_MODE_OFF (2 << 4)
239 #define PWM_MODE_ON (7 << 4)
242 * Dedicated function to read all registers related to a given PWM output.
243 * This saves us quite a few locks and bank selections.
244 * Must be called with data->lock held.
245 * nr is from 0 to 3
247 static void pc87427_readall_pwm(struct pc87427_data *data, u8 nr)
249 int iobase = data->address[LD_FAN];
251 outb(BANK_FC(nr), iobase + PC87427_REG_BANK);
252 data->pwm_enable[nr] = inb(iobase + PC87427_REG_PWM_ENABLE);
253 data->pwm[nr] = inb(iobase + PC87427_REG_PWM_DUTY);
256 static inline int pwm_enable_from_reg(u8 reg)
258 switch (reg & PWM_ENABLE_MODE_MASK) {
259 case PWM_MODE_ON:
260 return 0;
261 case PWM_MODE_MANUAL:
262 case PWM_MODE_OFF:
263 return 1;
264 case PWM_MODE_AUTO:
265 return 2;
266 default:
267 return -EPROTO;
271 static inline u8 pwm_enable_to_reg(unsigned long val, u8 pwmval)
273 switch (val) {
274 default:
275 return PWM_MODE_ON;
276 case 1:
277 return pwmval ? PWM_MODE_MANUAL : PWM_MODE_OFF;
278 case 2:
279 return PWM_MODE_AUTO;
284 * Temperature registers and conversions
287 #define PC87427_REG_TEMP_STATUS 0x10
288 #define PC87427_REG_TEMP 0x14
289 #define PC87427_REG_TEMP_MAX 0x18
290 #define PC87427_REG_TEMP_MIN 0x19
291 #define PC87427_REG_TEMP_CRIT 0x1a
292 #define PC87427_REG_TEMP_TYPE 0x1d
294 #define TEMP_STATUS_CHANEN (1 << 0)
295 #define TEMP_STATUS_LOWFLG (1 << 1)
296 #define TEMP_STATUS_HIGHFLG (1 << 2)
297 #define TEMP_STATUS_CRITFLG (1 << 3)
298 #define TEMP_STATUS_SENSERR (1 << 5)
299 #define TEMP_TYPE_MASK (3 << 5)
301 #define TEMP_TYPE_THERMISTOR (1 << 5)
302 #define TEMP_TYPE_REMOTE_DIODE (2 << 5)
303 #define TEMP_TYPE_LOCAL_DIODE (3 << 5)
306 * Dedicated function to read all registers related to a given temperature
307 * input. This saves us quite a few locks and bank selections.
308 * Must be called with data->lock held.
309 * nr is from 0 to 5
311 static void pc87427_readall_temp(struct pc87427_data *data, u8 nr)
313 int iobase = data->address[LD_TEMP];
315 outb(BANK_TM(nr), iobase + PC87427_REG_BANK);
316 data->temp[nr] = le16_to_cpu(inw(iobase + PC87427_REG_TEMP));
317 data->temp_max[nr] = inb(iobase + PC87427_REG_TEMP_MAX);
318 data->temp_min[nr] = inb(iobase + PC87427_REG_TEMP_MIN);
319 data->temp_crit[nr] = inb(iobase + PC87427_REG_TEMP_CRIT);
320 data->temp_type[nr] = inb(iobase + PC87427_REG_TEMP_TYPE);
321 data->temp_status[nr] = inb(iobase + PC87427_REG_TEMP_STATUS);
322 /* Clear fan alarm bits */
323 outb(data->temp_status[nr], iobase + PC87427_REG_TEMP_STATUS);
326 static inline unsigned int temp_type_from_reg(u8 reg)
328 switch (reg & TEMP_TYPE_MASK) {
329 case TEMP_TYPE_THERMISTOR:
330 return 4;
331 case TEMP_TYPE_REMOTE_DIODE:
332 case TEMP_TYPE_LOCAL_DIODE:
333 return 3;
334 default:
335 return 0;
340 * We assume 8-bit thermal sensors; 9-bit thermal sensors are possible
341 * too, but I have no idea how to figure out when they are used.
343 static inline long temp_from_reg(s16 reg)
345 return reg * 1000 / 256;
348 static inline long temp_from_reg8(s8 reg)
350 return reg * 1000;
354 * Data interface
357 static struct pc87427_data *pc87427_update_device(struct device *dev)
359 struct pc87427_data *data = dev_get_drvdata(dev);
360 int i;
362 mutex_lock(&data->lock);
363 if (!time_after(jiffies, data->last_updated + HZ)
364 && data->last_updated)
365 goto done;
367 /* Fans */
368 for (i = 0; i < 8; i++) {
369 if (!(data->fan_enabled & (1 << i)))
370 continue;
371 pc87427_readall_fan(data, i);
374 /* PWM outputs */
375 for (i = 0; i < 4; i++) {
376 if (!(data->pwm_enabled & (1 << i)))
377 continue;
378 pc87427_readall_pwm(data, i);
381 /* Temperature channels */
382 for (i = 0; i < 6; i++) {
383 if (!(data->temp_enabled & (1 << i)))
384 continue;
385 pc87427_readall_temp(data, i);
388 data->last_updated = jiffies;
390 done:
391 mutex_unlock(&data->lock);
392 return data;
395 static ssize_t show_fan_input(struct device *dev, struct device_attribute
396 *devattr, char *buf)
398 struct pc87427_data *data = pc87427_update_device(dev);
399 int nr = to_sensor_dev_attr(devattr)->index;
401 return sprintf(buf, "%lu\n", fan_from_reg(data->fan[nr]));
404 static ssize_t show_fan_min(struct device *dev, struct device_attribute
405 *devattr, char *buf)
407 struct pc87427_data *data = pc87427_update_device(dev);
408 int nr = to_sensor_dev_attr(devattr)->index;
410 return sprintf(buf, "%lu\n", fan_from_reg(data->fan_min[nr]));
413 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
414 *devattr, char *buf)
416 struct pc87427_data *data = pc87427_update_device(dev);
417 int nr = to_sensor_dev_attr(devattr)->index;
419 return sprintf(buf, "%d\n", !!(data->fan_status[nr]
420 & FAN_STATUS_LOSPD));
423 static ssize_t show_fan_fault(struct device *dev, struct device_attribute
424 *devattr, char *buf)
426 struct pc87427_data *data = pc87427_update_device(dev);
427 int nr = to_sensor_dev_attr(devattr)->index;
429 return sprintf(buf, "%d\n", !!(data->fan_status[nr]
430 & FAN_STATUS_STALL));
433 static ssize_t set_fan_min(struct device *dev, struct device_attribute
434 *devattr, const char *buf, size_t count)
436 struct pc87427_data *data = dev_get_drvdata(dev);
437 int nr = to_sensor_dev_attr(devattr)->index;
438 unsigned long val;
439 int iobase = data->address[LD_FAN];
441 if (kstrtoul(buf, 10, &val) < 0)
442 return -EINVAL;
444 mutex_lock(&data->lock);
445 outb(BANK_FM(nr), iobase + PC87427_REG_BANK);
447 * The low speed limit registers are read-only while monitoring
448 * is enabled, so we have to disable monitoring, then change the
449 * limit, and finally enable monitoring again.
451 outb(0, iobase + PC87427_REG_FAN_STATUS);
452 data->fan_min[nr] = fan_to_reg(val);
453 outw(data->fan_min[nr], iobase + PC87427_REG_FAN_MIN);
454 outb(FAN_STATUS_MONEN, iobase + PC87427_REG_FAN_STATUS);
455 mutex_unlock(&data->lock);
457 return count;
460 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
461 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
462 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2);
463 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_input, NULL, 3);
464 static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan_input, NULL, 4);
465 static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan_input, NULL, 5);
466 static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan_input, NULL, 6);
467 static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan_input, NULL, 7);
469 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
470 show_fan_min, set_fan_min, 0);
471 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
472 show_fan_min, set_fan_min, 1);
473 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO,
474 show_fan_min, set_fan_min, 2);
475 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO,
476 show_fan_min, set_fan_min, 3);
477 static SENSOR_DEVICE_ATTR(fan5_min, S_IWUSR | S_IRUGO,
478 show_fan_min, set_fan_min, 4);
479 static SENSOR_DEVICE_ATTR(fan6_min, S_IWUSR | S_IRUGO,
480 show_fan_min, set_fan_min, 5);
481 static SENSOR_DEVICE_ATTR(fan7_min, S_IWUSR | S_IRUGO,
482 show_fan_min, set_fan_min, 6);
483 static SENSOR_DEVICE_ATTR(fan8_min, S_IWUSR | S_IRUGO,
484 show_fan_min, set_fan_min, 7);
486 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0);
487 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 1);
488 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 2);
489 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 3);
490 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_fan_alarm, NULL, 4);
491 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_fan_alarm, NULL, 5);
492 static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_fan_alarm, NULL, 6);
493 static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_fan_alarm, NULL, 7);
495 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL, 0);
496 static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault, NULL, 1);
497 static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, show_fan_fault, NULL, 2);
498 static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, show_fan_fault, NULL, 3);
499 static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, show_fan_fault, NULL, 4);
500 static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, show_fan_fault, NULL, 5);
501 static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, show_fan_fault, NULL, 6);
502 static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, show_fan_fault, NULL, 7);
504 static struct attribute *pc87427_attributes_fan[8][5] = {
506 &sensor_dev_attr_fan1_input.dev_attr.attr,
507 &sensor_dev_attr_fan1_min.dev_attr.attr,
508 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
509 &sensor_dev_attr_fan1_fault.dev_attr.attr,
510 NULL
511 }, {
512 &sensor_dev_attr_fan2_input.dev_attr.attr,
513 &sensor_dev_attr_fan2_min.dev_attr.attr,
514 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
515 &sensor_dev_attr_fan2_fault.dev_attr.attr,
516 NULL
517 }, {
518 &sensor_dev_attr_fan3_input.dev_attr.attr,
519 &sensor_dev_attr_fan3_min.dev_attr.attr,
520 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
521 &sensor_dev_attr_fan3_fault.dev_attr.attr,
522 NULL
523 }, {
524 &sensor_dev_attr_fan4_input.dev_attr.attr,
525 &sensor_dev_attr_fan4_min.dev_attr.attr,
526 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
527 &sensor_dev_attr_fan4_fault.dev_attr.attr,
528 NULL
529 }, {
530 &sensor_dev_attr_fan5_input.dev_attr.attr,
531 &sensor_dev_attr_fan5_min.dev_attr.attr,
532 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
533 &sensor_dev_attr_fan5_fault.dev_attr.attr,
534 NULL
535 }, {
536 &sensor_dev_attr_fan6_input.dev_attr.attr,
537 &sensor_dev_attr_fan6_min.dev_attr.attr,
538 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
539 &sensor_dev_attr_fan6_fault.dev_attr.attr,
540 NULL
541 }, {
542 &sensor_dev_attr_fan7_input.dev_attr.attr,
543 &sensor_dev_attr_fan7_min.dev_attr.attr,
544 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
545 &sensor_dev_attr_fan7_fault.dev_attr.attr,
546 NULL
547 }, {
548 &sensor_dev_attr_fan8_input.dev_attr.attr,
549 &sensor_dev_attr_fan8_min.dev_attr.attr,
550 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
551 &sensor_dev_attr_fan8_fault.dev_attr.attr,
552 NULL
556 static const struct attribute_group pc87427_group_fan[8] = {
557 { .attrs = pc87427_attributes_fan[0] },
558 { .attrs = pc87427_attributes_fan[1] },
559 { .attrs = pc87427_attributes_fan[2] },
560 { .attrs = pc87427_attributes_fan[3] },
561 { .attrs = pc87427_attributes_fan[4] },
562 { .attrs = pc87427_attributes_fan[5] },
563 { .attrs = pc87427_attributes_fan[6] },
564 { .attrs = pc87427_attributes_fan[7] },
568 * Must be called with data->lock held and pc87427_readall_pwm() freshly
569 * called
571 static void update_pwm_enable(struct pc87427_data *data, int nr, u8 mode)
573 int iobase = data->address[LD_FAN];
574 data->pwm_enable[nr] &= ~PWM_ENABLE_MODE_MASK;
575 data->pwm_enable[nr] |= mode;
576 outb(data->pwm_enable[nr], iobase + PC87427_REG_PWM_ENABLE);
579 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
580 *devattr, char *buf)
582 struct pc87427_data *data = pc87427_update_device(dev);
583 int nr = to_sensor_dev_attr(devattr)->index;
584 int pwm_enable;
586 pwm_enable = pwm_enable_from_reg(data->pwm_enable[nr]);
587 if (pwm_enable < 0)
588 return pwm_enable;
589 return sprintf(buf, "%d\n", pwm_enable);
592 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
593 *devattr, const char *buf, size_t count)
595 struct pc87427_data *data = dev_get_drvdata(dev);
596 int nr = to_sensor_dev_attr(devattr)->index;
597 unsigned long val;
599 if (kstrtoul(buf, 10, &val) < 0 || val > 2)
600 return -EINVAL;
601 /* Can't go to automatic mode if it isn't configured */
602 if (val == 2 && !(data->pwm_auto_ok & (1 << nr)))
603 return -EINVAL;
605 mutex_lock(&data->lock);
606 pc87427_readall_pwm(data, nr);
607 update_pwm_enable(data, nr, pwm_enable_to_reg(val, data->pwm[nr]));
608 mutex_unlock(&data->lock);
610 return count;
613 static ssize_t show_pwm(struct device *dev, struct device_attribute
614 *devattr, char *buf)
616 struct pc87427_data *data = pc87427_update_device(dev);
617 int nr = to_sensor_dev_attr(devattr)->index;
619 return sprintf(buf, "%d\n", (int)data->pwm[nr]);
622 static ssize_t set_pwm(struct device *dev, struct device_attribute
623 *devattr, const char *buf, size_t count)
625 struct pc87427_data *data = dev_get_drvdata(dev);
626 int nr = to_sensor_dev_attr(devattr)->index;
627 unsigned long val;
628 int iobase = data->address[LD_FAN];
629 u8 mode;
631 if (kstrtoul(buf, 10, &val) < 0 || val > 0xff)
632 return -EINVAL;
634 mutex_lock(&data->lock);
635 pc87427_readall_pwm(data, nr);
636 mode = data->pwm_enable[nr] & PWM_ENABLE_MODE_MASK;
637 if (mode != PWM_MODE_MANUAL && mode != PWM_MODE_OFF) {
638 dev_notice(dev,
639 "Can't set PWM%d duty cycle while not in manual mode\n",
640 nr + 1);
641 mutex_unlock(&data->lock);
642 return -EPERM;
645 /* We may have to change the mode */
646 if (mode == PWM_MODE_MANUAL && val == 0) {
647 /* Transition from Manual to Off */
648 update_pwm_enable(data, nr, PWM_MODE_OFF);
649 mode = PWM_MODE_OFF;
650 dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1,
651 "manual", "off");
652 } else if (mode == PWM_MODE_OFF && val != 0) {
653 /* Transition from Off to Manual */
654 update_pwm_enable(data, nr, PWM_MODE_MANUAL);
655 mode = PWM_MODE_MANUAL;
656 dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1,
657 "off", "manual");
660 data->pwm[nr] = val;
661 if (mode == PWM_MODE_MANUAL)
662 outb(val, iobase + PC87427_REG_PWM_DUTY);
663 mutex_unlock(&data->lock);
665 return count;
668 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
669 show_pwm_enable, set_pwm_enable, 0);
670 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
671 show_pwm_enable, set_pwm_enable, 1);
672 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
673 show_pwm_enable, set_pwm_enable, 2);
674 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO,
675 show_pwm_enable, set_pwm_enable, 3);
677 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
678 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
679 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
680 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
682 static struct attribute *pc87427_attributes_pwm[4][3] = {
684 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
685 &sensor_dev_attr_pwm1.dev_attr.attr,
686 NULL
687 }, {
688 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
689 &sensor_dev_attr_pwm2.dev_attr.attr,
690 NULL
691 }, {
692 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
693 &sensor_dev_attr_pwm3.dev_attr.attr,
694 NULL
695 }, {
696 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
697 &sensor_dev_attr_pwm4.dev_attr.attr,
698 NULL
702 static const struct attribute_group pc87427_group_pwm[4] = {
703 { .attrs = pc87427_attributes_pwm[0] },
704 { .attrs = pc87427_attributes_pwm[1] },
705 { .attrs = pc87427_attributes_pwm[2] },
706 { .attrs = pc87427_attributes_pwm[3] },
709 static ssize_t show_temp_input(struct device *dev, struct device_attribute
710 *devattr, char *buf)
712 struct pc87427_data *data = pc87427_update_device(dev);
713 int nr = to_sensor_dev_attr(devattr)->index;
715 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
718 static ssize_t show_temp_min(struct device *dev, struct device_attribute
719 *devattr, char *buf)
721 struct pc87427_data *data = pc87427_update_device(dev);
722 int nr = to_sensor_dev_attr(devattr)->index;
724 return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_min[nr]));
727 static ssize_t show_temp_max(struct device *dev, struct device_attribute
728 *devattr, char *buf)
730 struct pc87427_data *data = pc87427_update_device(dev);
731 int nr = to_sensor_dev_attr(devattr)->index;
733 return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_max[nr]));
736 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
737 *devattr, char *buf)
739 struct pc87427_data *data = pc87427_update_device(dev);
740 int nr = to_sensor_dev_attr(devattr)->index;
742 return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_crit[nr]));
745 static ssize_t show_temp_type(struct device *dev, struct device_attribute
746 *devattr, char *buf)
748 struct pc87427_data *data = pc87427_update_device(dev);
749 int nr = to_sensor_dev_attr(devattr)->index;
751 return sprintf(buf, "%u\n", temp_type_from_reg(data->temp_type[nr]));
754 static ssize_t show_temp_min_alarm(struct device *dev, struct device_attribute
755 *devattr, char *buf)
757 struct pc87427_data *data = pc87427_update_device(dev);
758 int nr = to_sensor_dev_attr(devattr)->index;
760 return sprintf(buf, "%d\n", !!(data->temp_status[nr]
761 & TEMP_STATUS_LOWFLG));
764 static ssize_t show_temp_max_alarm(struct device *dev, struct device_attribute
765 *devattr, char *buf)
767 struct pc87427_data *data = pc87427_update_device(dev);
768 int nr = to_sensor_dev_attr(devattr)->index;
770 return sprintf(buf, "%d\n", !!(data->temp_status[nr]
771 & TEMP_STATUS_HIGHFLG));
774 static ssize_t show_temp_crit_alarm(struct device *dev, struct device_attribute
775 *devattr, char *buf)
777 struct pc87427_data *data = pc87427_update_device(dev);
778 int nr = to_sensor_dev_attr(devattr)->index;
780 return sprintf(buf, "%d\n", !!(data->temp_status[nr]
781 & TEMP_STATUS_CRITFLG));
784 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
785 *devattr, char *buf)
787 struct pc87427_data *data = pc87427_update_device(dev);
788 int nr = to_sensor_dev_attr(devattr)->index;
790 return sprintf(buf, "%d\n", !!(data->temp_status[nr]
791 & TEMP_STATUS_SENSERR));
794 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
795 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1);
796 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2);
797 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input, NULL, 3);
798 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_input, NULL, 4);
799 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_input, NULL, 5);
801 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min, NULL, 0);
802 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min, NULL, 1);
803 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min, NULL, 2);
804 static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min, NULL, 3);
805 static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min, NULL, 4);
806 static SENSOR_DEVICE_ATTR(temp6_min, S_IRUGO, show_temp_min, NULL, 5);
808 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL, 0);
809 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, NULL, 1);
810 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, NULL, 2);
811 static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, NULL, 3);
812 static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, NULL, 4);
813 static SENSOR_DEVICE_ATTR(temp6_max, S_IRUGO, show_temp_max, NULL, 5);
815 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0);
816 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL, 1);
817 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit, NULL, 2);
818 static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL, 3);
819 static SENSOR_DEVICE_ATTR(temp5_crit, S_IRUGO, show_temp_crit, NULL, 4);
820 static SENSOR_DEVICE_ATTR(temp6_crit, S_IRUGO, show_temp_crit, NULL, 5);
822 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
823 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
824 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
825 static SENSOR_DEVICE_ATTR(temp4_type, S_IRUGO, show_temp_type, NULL, 3);
826 static SENSOR_DEVICE_ATTR(temp5_type, S_IRUGO, show_temp_type, NULL, 4);
827 static SENSOR_DEVICE_ATTR(temp6_type, S_IRUGO, show_temp_type, NULL, 5);
829 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
830 show_temp_min_alarm, NULL, 0);
831 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
832 show_temp_min_alarm, NULL, 1);
833 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO,
834 show_temp_min_alarm, NULL, 2);
835 static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO,
836 show_temp_min_alarm, NULL, 3);
837 static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO,
838 show_temp_min_alarm, NULL, 4);
839 static SENSOR_DEVICE_ATTR(temp6_min_alarm, S_IRUGO,
840 show_temp_min_alarm, NULL, 5);
842 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
843 show_temp_max_alarm, NULL, 0);
844 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
845 show_temp_max_alarm, NULL, 1);
846 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO,
847 show_temp_max_alarm, NULL, 2);
848 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO,
849 show_temp_max_alarm, NULL, 3);
850 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO,
851 show_temp_max_alarm, NULL, 4);
852 static SENSOR_DEVICE_ATTR(temp6_max_alarm, S_IRUGO,
853 show_temp_max_alarm, NULL, 5);
855 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
856 show_temp_crit_alarm, NULL, 0);
857 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
858 show_temp_crit_alarm, NULL, 1);
859 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO,
860 show_temp_crit_alarm, NULL, 2);
861 static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO,
862 show_temp_crit_alarm, NULL, 3);
863 static SENSOR_DEVICE_ATTR(temp5_crit_alarm, S_IRUGO,
864 show_temp_crit_alarm, NULL, 4);
865 static SENSOR_DEVICE_ATTR(temp6_crit_alarm, S_IRUGO,
866 show_temp_crit_alarm, NULL, 5);
868 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
869 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
870 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
871 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
872 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4);
873 static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5);
875 static struct attribute *pc87427_attributes_temp[6][10] = {
877 &sensor_dev_attr_temp1_input.dev_attr.attr,
878 &sensor_dev_attr_temp1_min.dev_attr.attr,
879 &sensor_dev_attr_temp1_max.dev_attr.attr,
880 &sensor_dev_attr_temp1_crit.dev_attr.attr,
881 &sensor_dev_attr_temp1_type.dev_attr.attr,
882 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
883 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
884 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
885 &sensor_dev_attr_temp1_fault.dev_attr.attr,
886 NULL
887 }, {
888 &sensor_dev_attr_temp2_input.dev_attr.attr,
889 &sensor_dev_attr_temp2_min.dev_attr.attr,
890 &sensor_dev_attr_temp2_max.dev_attr.attr,
891 &sensor_dev_attr_temp2_crit.dev_attr.attr,
892 &sensor_dev_attr_temp2_type.dev_attr.attr,
893 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
894 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
895 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
896 &sensor_dev_attr_temp2_fault.dev_attr.attr,
897 NULL
898 }, {
899 &sensor_dev_attr_temp3_input.dev_attr.attr,
900 &sensor_dev_attr_temp3_min.dev_attr.attr,
901 &sensor_dev_attr_temp3_max.dev_attr.attr,
902 &sensor_dev_attr_temp3_crit.dev_attr.attr,
903 &sensor_dev_attr_temp3_type.dev_attr.attr,
904 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
905 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
906 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
907 &sensor_dev_attr_temp3_fault.dev_attr.attr,
908 NULL
909 }, {
910 &sensor_dev_attr_temp4_input.dev_attr.attr,
911 &sensor_dev_attr_temp4_min.dev_attr.attr,
912 &sensor_dev_attr_temp4_max.dev_attr.attr,
913 &sensor_dev_attr_temp4_crit.dev_attr.attr,
914 &sensor_dev_attr_temp4_type.dev_attr.attr,
915 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
916 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
917 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
918 &sensor_dev_attr_temp4_fault.dev_attr.attr,
919 NULL
920 }, {
921 &sensor_dev_attr_temp5_input.dev_attr.attr,
922 &sensor_dev_attr_temp5_min.dev_attr.attr,
923 &sensor_dev_attr_temp5_max.dev_attr.attr,
924 &sensor_dev_attr_temp5_crit.dev_attr.attr,
925 &sensor_dev_attr_temp5_type.dev_attr.attr,
926 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
927 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
928 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
929 &sensor_dev_attr_temp5_fault.dev_attr.attr,
930 NULL
931 }, {
932 &sensor_dev_attr_temp6_input.dev_attr.attr,
933 &sensor_dev_attr_temp6_min.dev_attr.attr,
934 &sensor_dev_attr_temp6_max.dev_attr.attr,
935 &sensor_dev_attr_temp6_crit.dev_attr.attr,
936 &sensor_dev_attr_temp6_type.dev_attr.attr,
937 &sensor_dev_attr_temp6_min_alarm.dev_attr.attr,
938 &sensor_dev_attr_temp6_max_alarm.dev_attr.attr,
939 &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr,
940 &sensor_dev_attr_temp6_fault.dev_attr.attr,
941 NULL
945 static const struct attribute_group pc87427_group_temp[6] = {
946 { .attrs = pc87427_attributes_temp[0] },
947 { .attrs = pc87427_attributes_temp[1] },
948 { .attrs = pc87427_attributes_temp[2] },
949 { .attrs = pc87427_attributes_temp[3] },
950 { .attrs = pc87427_attributes_temp[4] },
951 { .attrs = pc87427_attributes_temp[5] },
954 static ssize_t name_show(struct device *dev, struct device_attribute
955 *devattr, char *buf)
957 struct pc87427_data *data = dev_get_drvdata(dev);
959 return sprintf(buf, "%s\n", data->name);
961 static DEVICE_ATTR_RO(name);
965 * Device detection, attach and detach
968 static int pc87427_request_regions(struct platform_device *pdev,
969 int count)
971 struct resource *res;
972 int i;
974 for (i = 0; i < count; i++) {
975 res = platform_get_resource(pdev, IORESOURCE_IO, i);
976 if (!res) {
977 dev_err(&pdev->dev, "Missing resource #%d\n", i);
978 return -ENOENT;
980 if (!devm_request_region(&pdev->dev, res->start,
981 resource_size(res), DRVNAME)) {
982 dev_err(&pdev->dev,
983 "Failed to request region 0x%lx-0x%lx\n",
984 (unsigned long)res->start,
985 (unsigned long)res->end);
986 return -EBUSY;
989 return 0;
992 static void pc87427_init_device(struct device *dev)
994 struct pc87427_sio_data *sio_data = dev_get_platdata(dev);
995 struct pc87427_data *data = dev_get_drvdata(dev);
996 int i;
997 u8 reg;
999 /* The FMC module should be ready */
1000 reg = pc87427_read8(data, LD_FAN, PC87427_REG_BANK);
1001 if (!(reg & 0x80))
1002 dev_warn(dev, "%s module not ready!\n", "FMC");
1004 /* Check which fans are enabled */
1005 for (i = 0; i < 8; i++) {
1006 if (!(sio_data->has_fanin & (1 << i))) /* Not wired */
1007 continue;
1008 reg = pc87427_read8_bank(data, LD_FAN, BANK_FM(i),
1009 PC87427_REG_FAN_STATUS);
1010 if (reg & FAN_STATUS_MONEN)
1011 data->fan_enabled |= (1 << i);
1014 if (!data->fan_enabled) {
1015 dev_dbg(dev, "Enabling monitoring of all fans\n");
1016 for (i = 0; i < 8; i++) {
1017 if (!(sio_data->has_fanin & (1 << i))) /* Not wired */
1018 continue;
1019 pc87427_write8_bank(data, LD_FAN, BANK_FM(i),
1020 PC87427_REG_FAN_STATUS,
1021 FAN_STATUS_MONEN);
1023 data->fan_enabled = sio_data->has_fanin;
1026 /* Check which PWM outputs are enabled */
1027 for (i = 0; i < 4; i++) {
1028 if (!(sio_data->has_fanout & (1 << i))) /* Not wired */
1029 continue;
1030 reg = pc87427_read8_bank(data, LD_FAN, BANK_FC(i),
1031 PC87427_REG_PWM_ENABLE);
1032 if (reg & PWM_ENABLE_CTLEN)
1033 data->pwm_enabled |= (1 << i);
1036 * We don't expose an interface to reconfigure the automatic
1037 * fan control mode, so only allow to return to this mode if
1038 * it was originally set.
1040 if ((reg & PWM_ENABLE_MODE_MASK) == PWM_MODE_AUTO) {
1041 dev_dbg(dev, "PWM%d is in automatic control mode\n",
1042 i + 1);
1043 data->pwm_auto_ok |= (1 << i);
1047 /* The HMC module should be ready */
1048 reg = pc87427_read8(data, LD_TEMP, PC87427_REG_BANK);
1049 if (!(reg & 0x80))
1050 dev_warn(dev, "%s module not ready!\n", "HMC");
1052 /* Check which temperature channels are enabled */
1053 for (i = 0; i < 6; i++) {
1054 reg = pc87427_read8_bank(data, LD_TEMP, BANK_TM(i),
1055 PC87427_REG_TEMP_STATUS);
1056 if (reg & TEMP_STATUS_CHANEN)
1057 data->temp_enabled |= (1 << i);
1061 static void pc87427_remove_files(struct device *dev)
1063 struct pc87427_data *data = dev_get_drvdata(dev);
1064 int i;
1066 device_remove_file(dev, &dev_attr_name);
1067 for (i = 0; i < 8; i++) {
1068 if (!(data->fan_enabled & (1 << i)))
1069 continue;
1070 sysfs_remove_group(&dev->kobj, &pc87427_group_fan[i]);
1072 for (i = 0; i < 4; i++) {
1073 if (!(data->pwm_enabled & (1 << i)))
1074 continue;
1075 sysfs_remove_group(&dev->kobj, &pc87427_group_pwm[i]);
1077 for (i = 0; i < 6; i++) {
1078 if (!(data->temp_enabled & (1 << i)))
1079 continue;
1080 sysfs_remove_group(&dev->kobj, &pc87427_group_temp[i]);
1084 static int pc87427_probe(struct platform_device *pdev)
1086 struct pc87427_sio_data *sio_data = dev_get_platdata(&pdev->dev);
1087 struct pc87427_data *data;
1088 int i, err, res_count;
1090 data = devm_kzalloc(&pdev->dev, sizeof(struct pc87427_data),
1091 GFP_KERNEL);
1092 if (!data)
1093 return -ENOMEM;
1095 data->address[0] = sio_data->address[0];
1096 data->address[1] = sio_data->address[1];
1097 res_count = (data->address[0] != 0) + (data->address[1] != 0);
1099 err = pc87427_request_regions(pdev, res_count);
1100 if (err)
1101 return err;
1103 mutex_init(&data->lock);
1104 data->name = "pc87427";
1105 platform_set_drvdata(pdev, data);
1106 pc87427_init_device(&pdev->dev);
1108 /* Register sysfs hooks */
1109 err = device_create_file(&pdev->dev, &dev_attr_name);
1110 if (err)
1111 return err;
1112 for (i = 0; i < 8; i++) {
1113 if (!(data->fan_enabled & (1 << i)))
1114 continue;
1115 err = sysfs_create_group(&pdev->dev.kobj,
1116 &pc87427_group_fan[i]);
1117 if (err)
1118 goto exit_remove_files;
1120 for (i = 0; i < 4; i++) {
1121 if (!(data->pwm_enabled & (1 << i)))
1122 continue;
1123 err = sysfs_create_group(&pdev->dev.kobj,
1124 &pc87427_group_pwm[i]);
1125 if (err)
1126 goto exit_remove_files;
1128 for (i = 0; i < 6; i++) {
1129 if (!(data->temp_enabled & (1 << i)))
1130 continue;
1131 err = sysfs_create_group(&pdev->dev.kobj,
1132 &pc87427_group_temp[i]);
1133 if (err)
1134 goto exit_remove_files;
1137 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1138 if (IS_ERR(data->hwmon_dev)) {
1139 err = PTR_ERR(data->hwmon_dev);
1140 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
1141 goto exit_remove_files;
1144 return 0;
1146 exit_remove_files:
1147 pc87427_remove_files(&pdev->dev);
1148 return err;
1151 static int pc87427_remove(struct platform_device *pdev)
1153 struct pc87427_data *data = platform_get_drvdata(pdev);
1155 hwmon_device_unregister(data->hwmon_dev);
1156 pc87427_remove_files(&pdev->dev);
1158 return 0;
1162 static struct platform_driver pc87427_driver = {
1163 .driver = {
1164 .name = DRVNAME,
1166 .probe = pc87427_probe,
1167 .remove = pc87427_remove,
1170 static int __init pc87427_device_add(const struct pc87427_sio_data *sio_data)
1172 struct resource res[2] = {
1173 { .flags = IORESOURCE_IO },
1174 { .flags = IORESOURCE_IO },
1176 int err, i, res_count;
1178 res_count = 0;
1179 for (i = 0; i < 2; i++) {
1180 if (!sio_data->address[i])
1181 continue;
1182 res[res_count].start = sio_data->address[i];
1183 res[res_count].end = sio_data->address[i] + REGION_LENGTH - 1;
1184 res[res_count].name = logdev_str[i];
1186 err = acpi_check_resource_conflict(&res[res_count]);
1187 if (err)
1188 goto exit;
1190 res_count++;
1193 pdev = platform_device_alloc(DRVNAME, res[0].start);
1194 if (!pdev) {
1195 err = -ENOMEM;
1196 pr_err("Device allocation failed\n");
1197 goto exit;
1200 err = platform_device_add_resources(pdev, res, res_count);
1201 if (err) {
1202 pr_err("Device resource addition failed (%d)\n", err);
1203 goto exit_device_put;
1206 err = platform_device_add_data(pdev, sio_data,
1207 sizeof(struct pc87427_sio_data));
1208 if (err) {
1209 pr_err("Platform data allocation failed\n");
1210 goto exit_device_put;
1213 err = platform_device_add(pdev);
1214 if (err) {
1215 pr_err("Device addition failed (%d)\n", err);
1216 goto exit_device_put;
1219 return 0;
1221 exit_device_put:
1222 platform_device_put(pdev);
1223 exit:
1224 return err;
1227 static int __init pc87427_find(int sioaddr, struct pc87427_sio_data *sio_data)
1229 u16 val;
1230 u8 cfg, cfg_b;
1231 int i, err;
1233 err = superio_enter(sioaddr);
1234 if (err)
1235 return err;
1237 /* Identify device */
1238 val = force_id ? force_id : superio_inb(sioaddr, SIOREG_DEVID);
1239 if (val != 0xf2) { /* PC87427 */
1240 err = -ENODEV;
1241 goto exit;
1244 for (i = 0; i < 2; i++) {
1245 sio_data->address[i] = 0;
1246 /* Select logical device */
1247 superio_outb(sioaddr, SIOREG_LDSEL, logdev[i]);
1249 val = superio_inb(sioaddr, SIOREG_ACT);
1250 if (!(val & 0x01)) {
1251 pr_info("Logical device 0x%02x not activated\n",
1252 logdev[i]);
1253 continue;
1256 val = superio_inb(sioaddr, SIOREG_MAP);
1257 if (val & 0x01) {
1258 pr_warn("Logical device 0x%02x is memory-mapped, can't use\n",
1259 logdev[i]);
1260 continue;
1263 val = (superio_inb(sioaddr, SIOREG_IOBASE) << 8)
1264 | superio_inb(sioaddr, SIOREG_IOBASE + 1);
1265 if (!val) {
1266 pr_info("I/O base address not set for logical device 0x%02x\n",
1267 logdev[i]);
1268 continue;
1270 sio_data->address[i] = val;
1273 /* No point in loading the driver if everything is disabled */
1274 if (!sio_data->address[0] && !sio_data->address[1]) {
1275 err = -ENODEV;
1276 goto exit;
1279 /* Check which fan inputs are wired */
1280 sio_data->has_fanin = (1 << 2) | (1 << 3); /* FANIN2, FANIN3 */
1282 cfg = superio_inb(sioaddr, SIOREG_CF2);
1283 if (!(cfg & (1 << 3)))
1284 sio_data->has_fanin |= (1 << 0); /* FANIN0 */
1285 if (!(cfg & (1 << 2)))
1286 sio_data->has_fanin |= (1 << 4); /* FANIN4 */
1288 cfg = superio_inb(sioaddr, SIOREG_CFD);
1289 if (!(cfg & (1 << 0)))
1290 sio_data->has_fanin |= (1 << 1); /* FANIN1 */
1292 cfg = superio_inb(sioaddr, SIOREG_CF4);
1293 if (!(cfg & (1 << 0)))
1294 sio_data->has_fanin |= (1 << 7); /* FANIN7 */
1295 cfg_b = superio_inb(sioaddr, SIOREG_CFB);
1296 if (!(cfg & (1 << 1)) && (cfg_b & (1 << 3)))
1297 sio_data->has_fanin |= (1 << 5); /* FANIN5 */
1298 cfg = superio_inb(sioaddr, SIOREG_CF3);
1299 if ((cfg & (1 << 3)) && !(cfg_b & (1 << 5)))
1300 sio_data->has_fanin |= (1 << 6); /* FANIN6 */
1302 /* Check which fan outputs are wired */
1303 sio_data->has_fanout = (1 << 0); /* FANOUT0 */
1304 if (cfg_b & (1 << 0))
1305 sio_data->has_fanout |= (1 << 3); /* FANOUT3 */
1307 cfg = superio_inb(sioaddr, SIOREG_CFC);
1308 if (!(cfg & (1 << 4))) {
1309 if (cfg_b & (1 << 1))
1310 sio_data->has_fanout |= (1 << 1); /* FANOUT1 */
1311 if (cfg_b & (1 << 2))
1312 sio_data->has_fanout |= (1 << 2); /* FANOUT2 */
1315 /* FANOUT1 and FANOUT2 can each be routed to 2 different pins */
1316 cfg = superio_inb(sioaddr, SIOREG_CF5);
1317 if (cfg & (1 << 6))
1318 sio_data->has_fanout |= (1 << 1); /* FANOUT1 */
1319 if (cfg & (1 << 5))
1320 sio_data->has_fanout |= (1 << 2); /* FANOUT2 */
1322 exit:
1323 superio_exit(sioaddr);
1324 return err;
1327 static int __init pc87427_init(void)
1329 int err;
1330 struct pc87427_sio_data sio_data;
1332 if (pc87427_find(0x2e, &sio_data)
1333 && pc87427_find(0x4e, &sio_data))
1334 return -ENODEV;
1336 err = platform_driver_register(&pc87427_driver);
1337 if (err)
1338 goto exit;
1340 /* Sets global pdev as a side effect */
1341 err = pc87427_device_add(&sio_data);
1342 if (err)
1343 goto exit_driver;
1345 return 0;
1347 exit_driver:
1348 platform_driver_unregister(&pc87427_driver);
1349 exit:
1350 return err;
1353 static void __exit pc87427_exit(void)
1355 platform_device_unregister(pdev);
1356 platform_driver_unregister(&pc87427_driver);
1359 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1360 MODULE_DESCRIPTION("PC87427 hardware monitoring driver");
1361 MODULE_LICENSE("GPL");
1363 module_init(pc87427_init);
1364 module_exit(pc87427_exit);