2 * smsc47m1.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
5 * Supports the SMSC LPC47B27x, LPC47M10x, LPC47M112, LPC47M13x,
6 * LPC47M14x, LPC47M15x, LPC47M192, LPC47M292 and LPC47M997
9 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
10 * Copyright (C) 2004-2007 Jean Delvare <jdelvare@suse.de>
11 * Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/ioport.h>
34 #include <linux/jiffies.h>
35 #include <linux/platform_device.h>
36 #include <linux/hwmon.h>
37 #include <linux/hwmon-sysfs.h>
38 #include <linux/err.h>
39 #include <linux/init.h>
40 #include <linux/mutex.h>
41 #include <linux/sysfs.h>
42 #include <linux/acpi.h>
45 static unsigned short force_id
;
46 module_param(force_id
, ushort
, 0);
47 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
49 static struct platform_device
*pdev
;
51 #define DRVNAME "smsc47m1"
52 enum chips
{ smsc47m1
, smsc47m2
};
54 /* Super-I/0 registers and commands */
56 #define REG 0x2e /* The register to read/write */
57 #define VAL 0x2f /* The value to read/write */
60 superio_outb(int reg
, int val
)
73 /* logical device for fans is 0x0A */
74 #define superio_select() superio_outb(0x07, 0x0A)
79 if (!request_muxed_region(REG
, 2, DRVNAME
))
90 release_region(REG
, 2);
93 #define SUPERIO_REG_ACT 0x30
94 #define SUPERIO_REG_BASE 0x60
95 #define SUPERIO_REG_DEVID 0x20
96 #define SUPERIO_REG_DEVREV 0x21
98 /* Logical device registers */
100 #define SMSC_EXTENT 0x80
102 /* nr is 0 or 1 in the macros below */
103 #define SMSC47M1_REG_ALARM 0x04
104 #define SMSC47M1_REG_TPIN(nr) (0x34 - (nr))
105 #define SMSC47M1_REG_PPIN(nr) (0x36 - (nr))
106 #define SMSC47M1_REG_FANDIV 0x58
108 static const u8 SMSC47M1_REG_FAN
[3] = { 0x59, 0x5a, 0x6b };
109 static const u8 SMSC47M1_REG_FAN_PRELOAD
[3] = { 0x5b, 0x5c, 0x6c };
110 static const u8 SMSC47M1_REG_PWM
[3] = { 0x56, 0x57, 0x69 };
112 #define SMSC47M2_REG_ALARM6 0x09
113 #define SMSC47M2_REG_TPIN1 0x38
114 #define SMSC47M2_REG_TPIN2 0x37
115 #define SMSC47M2_REG_TPIN3 0x2d
116 #define SMSC47M2_REG_PPIN3 0x2c
117 #define SMSC47M2_REG_FANDIV3 0x6a
119 #define MIN_FROM_REG(reg, div) ((reg) >= 192 ? 0 : \
120 983040 / ((192 - (reg)) * (div)))
121 #define FAN_FROM_REG(reg, div, preload) ((reg) <= (preload) || (reg) == 255 ? \
123 983040 / (((reg) - (preload)) * (div)))
124 #define DIV_FROM_REG(reg) (1 << (reg))
125 #define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1)
126 #define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01)
127 #define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E)
129 struct smsc47m1_data
{
133 struct device
*hwmon_dev
;
135 struct mutex update_lock
;
136 unsigned long last_updated
; /* In jiffies */
138 u8 fan
[3]; /* Register value */
139 u8 fan_preload
[3]; /* Register value */
140 u8 fan_div
[3]; /* Register encoding, shifted right */
141 u8 alarms
; /* Register encoding */
142 u8 pwm
[3]; /* Register value (bit 0 is disable) */
145 struct smsc47m1_sio_data
{
147 u8 activate
; /* Remember initial device state */
150 static inline int smsc47m1_read_value(struct smsc47m1_data
*data
, u8 reg
)
152 return inb_p(data
->addr
+ reg
);
155 static inline void smsc47m1_write_value(struct smsc47m1_data
*data
, u8 reg
,
158 outb_p(value
, data
->addr
+ reg
);
161 static struct smsc47m1_data
*smsc47m1_update_device(struct device
*dev
,
164 struct smsc47m1_data
*data
= dev_get_drvdata(dev
);
166 mutex_lock(&data
->update_lock
);
168 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2) || init
) {
170 fan_nr
= data
->type
== smsc47m2
? 3 : 2;
172 for (i
= 0; i
< fan_nr
; i
++) {
173 data
->fan
[i
] = smsc47m1_read_value(data
,
174 SMSC47M1_REG_FAN
[i
]);
175 data
->fan_preload
[i
] = smsc47m1_read_value(data
,
176 SMSC47M1_REG_FAN_PRELOAD
[i
]);
177 data
->pwm
[i
] = smsc47m1_read_value(data
,
178 SMSC47M1_REG_PWM
[i
]);
181 i
= smsc47m1_read_value(data
, SMSC47M1_REG_FANDIV
);
182 data
->fan_div
[0] = (i
>> 4) & 0x03;
183 data
->fan_div
[1] = i
>> 6;
185 data
->alarms
= smsc47m1_read_value(data
,
186 SMSC47M1_REG_ALARM
) >> 6;
187 /* Clear alarms if needed */
189 smsc47m1_write_value(data
, SMSC47M1_REG_ALARM
, 0xC0);
192 data
->fan_div
[2] = (smsc47m1_read_value(data
,
193 SMSC47M2_REG_FANDIV3
) >> 4) & 0x03;
194 data
->alarms
|= (smsc47m1_read_value(data
,
195 SMSC47M2_REG_ALARM6
) & 0x40) >> 4;
196 /* Clear alarm if needed */
197 if (data
->alarms
& 0x04)
198 smsc47m1_write_value(data
,
203 data
->last_updated
= jiffies
;
206 mutex_unlock(&data
->update_lock
);
210 static ssize_t
get_fan(struct device
*dev
, struct device_attribute
213 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
214 struct smsc47m1_data
*data
= smsc47m1_update_device(dev
, 0);
215 int nr
= attr
->index
;
217 * This chip (stupidly) stops monitoring fan speed if PWM is
218 * enabled and duty cycle is 0%. This is fine if the monitoring
219 * and control concern the same fan, but troublesome if they are
220 * not (which could as well happen).
222 int rpm
= (data
->pwm
[nr
] & 0x7F) == 0x00 ? 0 :
223 FAN_FROM_REG(data
->fan
[nr
],
224 DIV_FROM_REG(data
->fan_div
[nr
]),
225 data
->fan_preload
[nr
]);
226 return sprintf(buf
, "%d\n", rpm
);
229 static ssize_t
get_fan_min(struct device
*dev
, struct device_attribute
232 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
233 struct smsc47m1_data
*data
= smsc47m1_update_device(dev
, 0);
234 int nr
= attr
->index
;
235 int rpm
= MIN_FROM_REG(data
->fan_preload
[nr
],
236 DIV_FROM_REG(data
->fan_div
[nr
]));
237 return sprintf(buf
, "%d\n", rpm
);
240 static ssize_t
get_fan_div(struct device
*dev
, struct device_attribute
243 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
244 struct smsc47m1_data
*data
= smsc47m1_update_device(dev
, 0);
245 return sprintf(buf
, "%d\n", DIV_FROM_REG(data
->fan_div
[attr
->index
]));
248 static ssize_t
get_fan_alarm(struct device
*dev
, struct device_attribute
251 int bitnr
= to_sensor_dev_attr(devattr
)->index
;
252 struct smsc47m1_data
*data
= smsc47m1_update_device(dev
, 0);
253 return sprintf(buf
, "%u\n", (data
->alarms
>> bitnr
) & 1);
256 static ssize_t
get_pwm(struct device
*dev
, struct device_attribute
259 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
260 struct smsc47m1_data
*data
= smsc47m1_update_device(dev
, 0);
261 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[attr
->index
]));
264 static ssize_t
get_pwm_en(struct device
*dev
, struct device_attribute
267 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
268 struct smsc47m1_data
*data
= smsc47m1_update_device(dev
, 0);
269 return sprintf(buf
, "%d\n", PWM_EN_FROM_REG(data
->pwm
[attr
->index
]));
272 static ssize_t
alarms_show(struct device
*dev
,
273 struct device_attribute
*devattr
, char *buf
)
275 struct smsc47m1_data
*data
= smsc47m1_update_device(dev
, 0);
276 return sprintf(buf
, "%d\n", data
->alarms
);
279 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
280 *devattr
, const char *buf
, size_t count
)
282 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
283 struct smsc47m1_data
*data
= dev_get_drvdata(dev
);
284 int nr
= attr
->index
;
289 err
= kstrtol(buf
, 10, &val
);
293 mutex_lock(&data
->update_lock
);
294 rpmdiv
= val
* DIV_FROM_REG(data
->fan_div
[nr
]);
296 if (983040 > 192 * rpmdiv
|| 2 * rpmdiv
> 983040) {
297 mutex_unlock(&data
->update_lock
);
301 data
->fan_preload
[nr
] = 192 - ((983040 + rpmdiv
/ 2) / rpmdiv
);
302 smsc47m1_write_value(data
, SMSC47M1_REG_FAN_PRELOAD
[nr
],
303 data
->fan_preload
[nr
]);
304 mutex_unlock(&data
->update_lock
);
310 * Note: we save and restore the fan minimum here, because its value is
311 * determined in part by the fan clock divider. This follows the principle
312 * of least surprise; the user doesn't expect the fan minimum to change just
313 * because the divider changed.
315 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
316 *devattr
, const char *buf
, size_t count
)
318 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
319 struct smsc47m1_data
*data
= dev_get_drvdata(dev
);
320 int nr
= attr
->index
;
324 u8 old_div
= DIV_FROM_REG(data
->fan_div
[nr
]);
326 err
= kstrtol(buf
, 10, &new_div
);
330 if (new_div
== old_div
) /* No change */
333 mutex_lock(&data
->update_lock
);
336 data
->fan_div
[nr
] = 0;
339 data
->fan_div
[nr
] = 1;
342 data
->fan_div
[nr
] = 2;
345 data
->fan_div
[nr
] = 3;
348 mutex_unlock(&data
->update_lock
);
355 tmp
= smsc47m1_read_value(data
, SMSC47M1_REG_FANDIV
)
356 & ~(0x03 << (4 + 2 * nr
));
357 tmp
|= data
->fan_div
[nr
] << (4 + 2 * nr
);
358 smsc47m1_write_value(data
, SMSC47M1_REG_FANDIV
, tmp
);
361 tmp
= smsc47m1_read_value(data
, SMSC47M2_REG_FANDIV3
) & 0xCF;
362 tmp
|= data
->fan_div
[2] << 4;
363 smsc47m1_write_value(data
, SMSC47M2_REG_FANDIV3
, tmp
);
367 /* Preserve fan min */
368 tmp
= 192 - (old_div
* (192 - data
->fan_preload
[nr
])
369 + new_div
/ 2) / new_div
;
370 data
->fan_preload
[nr
] = clamp_val(tmp
, 0, 191);
371 smsc47m1_write_value(data
, SMSC47M1_REG_FAN_PRELOAD
[nr
],
372 data
->fan_preload
[nr
]);
373 mutex_unlock(&data
->update_lock
);
378 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
379 *devattr
, const char *buf
, size_t count
)
381 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
382 struct smsc47m1_data
*data
= dev_get_drvdata(dev
);
383 int nr
= attr
->index
;
387 err
= kstrtol(buf
, 10, &val
);
391 if (val
< 0 || val
> 255)
394 mutex_lock(&data
->update_lock
);
395 data
->pwm
[nr
] &= 0x81; /* Preserve additional bits */
396 data
->pwm
[nr
] |= PWM_TO_REG(val
);
397 smsc47m1_write_value(data
, SMSC47M1_REG_PWM
[nr
],
399 mutex_unlock(&data
->update_lock
);
404 static ssize_t
set_pwm_en(struct device
*dev
, struct device_attribute
405 *devattr
, const char *buf
, size_t count
)
407 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
408 struct smsc47m1_data
*data
= dev_get_drvdata(dev
);
409 int nr
= attr
->index
;
413 err
= kstrtoul(buf
, 10, &val
);
420 mutex_lock(&data
->update_lock
);
421 data
->pwm
[nr
] &= 0xFE; /* preserve the other bits */
422 data
->pwm
[nr
] |= !val
;
423 smsc47m1_write_value(data
, SMSC47M1_REG_PWM
[nr
],
425 mutex_unlock(&data
->update_lock
);
430 #define fan_present(offset) \
431 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan, \
433 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
434 get_fan_min, set_fan_min, offset - 1); \
435 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
436 get_fan_div, set_fan_div, offset - 1); \
437 static SENSOR_DEVICE_ATTR(fan##offset##_alarm, S_IRUGO, get_fan_alarm, \
439 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
440 get_pwm, set_pwm, offset - 1); \
441 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
442 get_pwm_en, set_pwm_en, offset - 1)
448 static DEVICE_ATTR_RO(alarms
);
450 static ssize_t
name_show(struct device
*dev
, struct device_attribute
453 struct smsc47m1_data
*data
= dev_get_drvdata(dev
);
455 return sprintf(buf
, "%s\n", data
->name
);
457 static DEVICE_ATTR_RO(name
);
459 static struct attribute
*smsc47m1_attributes_fan1
[] = {
460 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
461 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
462 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
463 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
467 static const struct attribute_group smsc47m1_group_fan1
= {
468 .attrs
= smsc47m1_attributes_fan1
,
471 static struct attribute
*smsc47m1_attributes_fan2
[] = {
472 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
473 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
474 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
475 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
479 static const struct attribute_group smsc47m1_group_fan2
= {
480 .attrs
= smsc47m1_attributes_fan2
,
483 static struct attribute
*smsc47m1_attributes_fan3
[] = {
484 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
485 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
486 &sensor_dev_attr_fan3_div
.dev_attr
.attr
,
487 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
491 static const struct attribute_group smsc47m1_group_fan3
= {
492 .attrs
= smsc47m1_attributes_fan3
,
495 static struct attribute
*smsc47m1_attributes_pwm1
[] = {
496 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
497 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
501 static const struct attribute_group smsc47m1_group_pwm1
= {
502 .attrs
= smsc47m1_attributes_pwm1
,
505 static struct attribute
*smsc47m1_attributes_pwm2
[] = {
506 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
507 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
511 static const struct attribute_group smsc47m1_group_pwm2
= {
512 .attrs
= smsc47m1_attributes_pwm2
,
515 static struct attribute
*smsc47m1_attributes_pwm3
[] = {
516 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
517 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
521 static const struct attribute_group smsc47m1_group_pwm3
= {
522 .attrs
= smsc47m1_attributes_pwm3
,
525 static struct attribute
*smsc47m1_attributes
[] = {
526 &dev_attr_alarms
.attr
,
531 static const struct attribute_group smsc47m1_group
= {
532 .attrs
= smsc47m1_attributes
,
535 static int __init
smsc47m1_find(struct smsc47m1_sio_data
*sio_data
)
541 err
= superio_enter();
545 val
= force_id
? force_id
: superio_inb(SUPERIO_REG_DEVID
);
548 * SMSC LPC47M10x/LPC47M112/LPC47M13x (device id 0x59), LPC47M14x
549 * (device id 0x5F) and LPC47B27x (device id 0x51) have fan control.
550 * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
551 * can do much more besides (device id 0x60).
552 * The LPC47M997 is undocumented, but seems to be compatible with
553 * the LPC47M192, and has the same device id.
554 * The LPC47M292 (device id 0x6B) is somewhat compatible, but it
555 * supports a 3rd fan, and the pin configuration registers are
556 * unfortunately different.
557 * The LPC47M233 has the same device id (0x6B) but is not compatible.
558 * We check the high bit of the device revision register to
559 * differentiate them.
563 pr_info("Found SMSC LPC47B27x\n");
564 sio_data
->type
= smsc47m1
;
567 pr_info("Found SMSC LPC47M10x/LPC47M112/LPC47M13x\n");
568 sio_data
->type
= smsc47m1
;
571 pr_info("Found SMSC LPC47M14x\n");
572 sio_data
->type
= smsc47m1
;
575 pr_info("Found SMSC LPC47M15x/LPC47M192/LPC47M997\n");
576 sio_data
->type
= smsc47m1
;
579 if (superio_inb(SUPERIO_REG_DEVREV
) & 0x80) {
580 pr_debug("Found SMSC LPC47M233, unsupported\n");
585 pr_info("Found SMSC LPC47M292\n");
586 sio_data
->type
= smsc47m2
;
594 addr
= (superio_inb(SUPERIO_REG_BASE
) << 8)
595 | superio_inb(SUPERIO_REG_BASE
+ 1);
597 pr_info("Device address not set, will not use\n");
603 * Enable only if address is set (needed at least on the
604 * Compaq Presario S4000NX)
606 sio_data
->activate
= superio_inb(SUPERIO_REG_ACT
);
607 if ((sio_data
->activate
& 0x01) == 0) {
608 pr_info("Enabling device\n");
609 superio_outb(SUPERIO_REG_ACT
, sio_data
->activate
| 0x01);
616 /* Restore device to its initial state */
617 static void smsc47m1_restore(const struct smsc47m1_sio_data
*sio_data
)
619 if ((sio_data
->activate
& 0x01) == 0) {
620 if (!superio_enter()) {
622 pr_info("Disabling device\n");
623 superio_outb(SUPERIO_REG_ACT
, sio_data
->activate
);
626 pr_warn("Failed to disable device\n");
635 * This function can be used to:
636 * - test for resource conflicts with ACPI
637 * - request the resources
638 * We only allocate the I/O ports we really need, to minimize the risk of
639 * conflicts with ACPI or with other drivers.
641 static int __init
smsc47m1_handle_resources(unsigned short address
,
642 enum chips type
, int action
,
645 static const u8 ports_m1
[] = {
646 /* register, region length */
652 static const u8 ports_m2
[] = {
653 /* register, region length */
662 int i
, ports_size
, err
;
669 ports_size
= ARRAY_SIZE(ports_m1
);
673 ports_size
= ARRAY_SIZE(ports_m2
);
677 for (i
= 0; i
+ 1 < ports_size
; i
+= 2) {
678 unsigned short start
= address
+ ports
[i
];
679 unsigned short len
= ports
[i
+ 1];
683 /* Only check for conflicts */
684 err
= acpi_check_region(start
, len
, DRVNAME
);
689 /* Request the resources */
690 if (!devm_request_region(dev
, start
, len
, DRVNAME
)) {
692 "Region 0x%hx-0x%hx already in use!\n",
703 static void smsc47m1_remove_files(struct device
*dev
)
705 sysfs_remove_group(&dev
->kobj
, &smsc47m1_group
);
706 sysfs_remove_group(&dev
->kobj
, &smsc47m1_group_fan1
);
707 sysfs_remove_group(&dev
->kobj
, &smsc47m1_group_fan2
);
708 sysfs_remove_group(&dev
->kobj
, &smsc47m1_group_fan3
);
709 sysfs_remove_group(&dev
->kobj
, &smsc47m1_group_pwm1
);
710 sysfs_remove_group(&dev
->kobj
, &smsc47m1_group_pwm2
);
711 sysfs_remove_group(&dev
->kobj
, &smsc47m1_group_pwm3
);
714 static int __init
smsc47m1_probe(struct platform_device
*pdev
)
716 struct device
*dev
= &pdev
->dev
;
717 struct smsc47m1_sio_data
*sio_data
= dev_get_platdata(dev
);
718 struct smsc47m1_data
*data
;
719 struct resource
*res
;
721 int fan1
, fan2
, fan3
, pwm1
, pwm2
, pwm3
;
723 static const char * const names
[] = {
728 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
729 err
= smsc47m1_handle_resources(res
->start
, sio_data
->type
,
734 data
= devm_kzalloc(dev
, sizeof(struct smsc47m1_data
), GFP_KERNEL
);
738 data
->addr
= res
->start
;
739 data
->type
= sio_data
->type
;
740 data
->name
= names
[sio_data
->type
];
741 mutex_init(&data
->update_lock
);
742 platform_set_drvdata(pdev
, data
);
745 * If no function is properly configured, there's no point in
746 * actually registering the chip.
748 pwm1
= (smsc47m1_read_value(data
, SMSC47M1_REG_PPIN(0)) & 0x05)
750 pwm2
= (smsc47m1_read_value(data
, SMSC47M1_REG_PPIN(1)) & 0x05)
752 if (data
->type
== smsc47m2
) {
753 fan1
= (smsc47m1_read_value(data
, SMSC47M2_REG_TPIN1
)
755 fan2
= (smsc47m1_read_value(data
, SMSC47M2_REG_TPIN2
)
757 fan3
= (smsc47m1_read_value(data
, SMSC47M2_REG_TPIN3
)
759 pwm3
= (smsc47m1_read_value(data
, SMSC47M2_REG_PPIN3
)
762 fan1
= (smsc47m1_read_value(data
, SMSC47M1_REG_TPIN(0))
764 fan2
= (smsc47m1_read_value(data
, SMSC47M1_REG_TPIN(1))
769 if (!(fan1
|| fan2
|| fan3
|| pwm1
|| pwm2
|| pwm3
)) {
770 dev_warn(dev
, "Device not configured, will not use\n");
775 * Some values (fan min, clock dividers, pwm registers) may be
776 * needed before any update is triggered, so we better read them
777 * at least once here. We don't usually do it that way, but in
778 * this particular case, manually reading 5 registers out of 8
779 * doesn't make much sense and we're better using the existing
782 smsc47m1_update_device(dev
, 1);
784 /* Register sysfs hooks */
786 err
= sysfs_create_group(&dev
->kobj
,
787 &smsc47m1_group_fan1
);
789 goto error_remove_files
;
791 dev_dbg(dev
, "Fan 1 not enabled by hardware, skipping\n");
794 err
= sysfs_create_group(&dev
->kobj
,
795 &smsc47m1_group_fan2
);
797 goto error_remove_files
;
799 dev_dbg(dev
, "Fan 2 not enabled by hardware, skipping\n");
802 err
= sysfs_create_group(&dev
->kobj
,
803 &smsc47m1_group_fan3
);
805 goto error_remove_files
;
806 } else if (data
->type
== smsc47m2
)
807 dev_dbg(dev
, "Fan 3 not enabled by hardware, skipping\n");
810 err
= sysfs_create_group(&dev
->kobj
,
811 &smsc47m1_group_pwm1
);
813 goto error_remove_files
;
815 dev_dbg(dev
, "PWM 1 not enabled by hardware, skipping\n");
818 err
= sysfs_create_group(&dev
->kobj
,
819 &smsc47m1_group_pwm2
);
821 goto error_remove_files
;
823 dev_dbg(dev
, "PWM 2 not enabled by hardware, skipping\n");
826 err
= sysfs_create_group(&dev
->kobj
,
827 &smsc47m1_group_pwm3
);
829 goto error_remove_files
;
830 } else if (data
->type
== smsc47m2
)
831 dev_dbg(dev
, "PWM 3 not enabled by hardware, skipping\n");
833 err
= sysfs_create_group(&dev
->kobj
, &smsc47m1_group
);
835 goto error_remove_files
;
837 data
->hwmon_dev
= hwmon_device_register(dev
);
838 if (IS_ERR(data
->hwmon_dev
)) {
839 err
= PTR_ERR(data
->hwmon_dev
);
840 goto error_remove_files
;
846 smsc47m1_remove_files(dev
);
850 static int __exit
smsc47m1_remove(struct platform_device
*pdev
)
852 struct smsc47m1_data
*data
= platform_get_drvdata(pdev
);
854 hwmon_device_unregister(data
->hwmon_dev
);
855 smsc47m1_remove_files(&pdev
->dev
);
860 static struct platform_driver smsc47m1_driver
= {
864 .remove
= __exit_p(smsc47m1_remove
),
867 static int __init
smsc47m1_device_add(unsigned short address
,
868 const struct smsc47m1_sio_data
*sio_data
)
870 struct resource res
= {
872 .end
= address
+ SMSC_EXTENT
- 1,
874 .flags
= IORESOURCE_IO
,
878 err
= smsc47m1_handle_resources(address
, sio_data
->type
, CHECK
, NULL
);
882 pdev
= platform_device_alloc(DRVNAME
, address
);
885 pr_err("Device allocation failed\n");
889 err
= platform_device_add_resources(pdev
, &res
, 1);
891 pr_err("Device resource addition failed (%d)\n", err
);
892 goto exit_device_put
;
895 err
= platform_device_add_data(pdev
, sio_data
,
896 sizeof(struct smsc47m1_sio_data
));
898 pr_err("Platform data allocation failed\n");
899 goto exit_device_put
;
902 err
= platform_device_add(pdev
);
904 pr_err("Device addition failed (%d)\n", err
);
905 goto exit_device_put
;
911 platform_device_put(pdev
);
916 static int __init
sm_smsc47m1_init(void)
919 unsigned short address
;
920 struct smsc47m1_sio_data sio_data
;
922 err
= smsc47m1_find(&sio_data
);
927 /* Sets global pdev as a side effect */
928 err
= smsc47m1_device_add(address
, &sio_data
);
932 err
= platform_driver_probe(&smsc47m1_driver
, smsc47m1_probe
);
939 platform_device_unregister(pdev
);
940 smsc47m1_restore(&sio_data
);
944 static void __exit
sm_smsc47m1_exit(void)
946 platform_driver_unregister(&smsc47m1_driver
);
947 smsc47m1_restore(dev_get_platdata(&pdev
->dev
));
948 platform_device_unregister(pdev
);
951 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
952 MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
953 MODULE_LICENSE("GPL");
955 module_init(sm_smsc47m1_init
);
956 module_exit(sm_smsc47m1_exit
);