Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / smsc47m1.c
blob5eeac9853d0ae8dd4deeef82d2137d2c35c21314
1 /*
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
7 * Super-I/O chips.
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>
12 * and Jean Delvare
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>
43 #include <linux/io.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 */
59 static inline void
60 superio_outb(int reg, int val)
62 outb(reg, REG);
63 outb(val, VAL);
66 static inline int
67 superio_inb(int reg)
69 outb(reg, REG);
70 return inb(VAL);
73 /* logical device for fans is 0x0A */
74 #define superio_select() superio_outb(0x07, 0x0A)
76 static inline int
77 superio_enter(void)
79 if (!request_muxed_region(REG, 2, DRVNAME))
80 return -EBUSY;
82 outb(0x55, REG);
83 return 0;
86 static inline void
87 superio_exit(void)
89 outb(0xAA, REG);
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 ? \
122 0 : \
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 {
130 unsigned short addr;
131 const char *name;
132 enum chips type;
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 {
146 enum chips type;
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,
156 u8 value)
158 outb_p(value, data->addr + reg);
161 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
162 int init)
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) {
169 int i, fan_nr;
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 */
188 if (data->alarms)
189 smsc47m1_write_value(data, SMSC47M1_REG_ALARM, 0xC0);
191 if (fan_nr >= 3) {
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,
199 SMSC47M2_REG_ALARM6,
200 0x40);
203 data->last_updated = jiffies;
206 mutex_unlock(&data->update_lock);
207 return data;
210 static ssize_t get_fan(struct device *dev, struct device_attribute
211 *devattr, char *buf)
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
230 *devattr, char *buf)
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
241 *devattr, char *buf)
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
249 *devattr, char *buf)
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
257 *devattr, char *buf)
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
265 *devattr, char *buf)
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;
285 long rpmdiv;
286 long val;
287 int err;
289 err = kstrtol(buf, 10, &val);
290 if (err)
291 return err;
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);
298 return -EINVAL;
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);
306 return count;
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;
321 long new_div;
322 int err;
323 long tmp;
324 u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
326 err = kstrtol(buf, 10, &new_div);
327 if (err)
328 return err;
330 if (new_div == old_div) /* No change */
331 return count;
333 mutex_lock(&data->update_lock);
334 switch (new_div) {
335 case 1:
336 data->fan_div[nr] = 0;
337 break;
338 case 2:
339 data->fan_div[nr] = 1;
340 break;
341 case 4:
342 data->fan_div[nr] = 2;
343 break;
344 case 8:
345 data->fan_div[nr] = 3;
346 break;
347 default:
348 mutex_unlock(&data->update_lock);
349 return -EINVAL;
352 switch (nr) {
353 case 0:
354 case 1:
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);
359 break;
360 case 2:
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);
364 break;
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);
375 return count;
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;
384 long val;
385 int err;
387 err = kstrtol(buf, 10, &val);
388 if (err)
389 return err;
391 if (val < 0 || val > 255)
392 return -EINVAL;
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],
398 data->pwm[nr]);
399 mutex_unlock(&data->update_lock);
401 return count;
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;
410 unsigned long val;
411 int err;
413 err = kstrtoul(buf, 10, &val);
414 if (err)
415 return err;
417 if (val > 1)
418 return -EINVAL;
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],
424 data->pwm[nr]);
425 mutex_unlock(&data->update_lock);
427 return count;
430 #define fan_present(offset) \
431 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan, \
432 NULL, offset - 1); \
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, \
438 NULL, offset - 1); \
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)
444 fan_present(1);
445 fan_present(2);
446 fan_present(3);
448 static DEVICE_ATTR_RO(alarms);
450 static ssize_t name_show(struct device *dev, struct device_attribute
451 *devattr, char *buf)
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,
464 NULL
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,
476 NULL
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,
488 NULL
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,
498 NULL
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,
508 NULL
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,
518 NULL
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,
527 &dev_attr_name.attr,
528 NULL
531 static const struct attribute_group smsc47m1_group = {
532 .attrs = smsc47m1_attributes,
535 static int __init smsc47m1_find(struct smsc47m1_sio_data *sio_data)
537 u8 val;
538 unsigned short addr;
539 int err;
541 err = superio_enter();
542 if (err)
543 return err;
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.
561 switch (val) {
562 case 0x51:
563 pr_info("Found SMSC LPC47B27x\n");
564 sio_data->type = smsc47m1;
565 break;
566 case 0x59:
567 pr_info("Found SMSC LPC47M10x/LPC47M112/LPC47M13x\n");
568 sio_data->type = smsc47m1;
569 break;
570 case 0x5F:
571 pr_info("Found SMSC LPC47M14x\n");
572 sio_data->type = smsc47m1;
573 break;
574 case 0x60:
575 pr_info("Found SMSC LPC47M15x/LPC47M192/LPC47M997\n");
576 sio_data->type = smsc47m1;
577 break;
578 case 0x6B:
579 if (superio_inb(SUPERIO_REG_DEVREV) & 0x80) {
580 pr_debug("Found SMSC LPC47M233, unsupported\n");
581 superio_exit();
582 return -ENODEV;
585 pr_info("Found SMSC LPC47M292\n");
586 sio_data->type = smsc47m2;
587 break;
588 default:
589 superio_exit();
590 return -ENODEV;
593 superio_select();
594 addr = (superio_inb(SUPERIO_REG_BASE) << 8)
595 | superio_inb(SUPERIO_REG_BASE + 1);
596 if (addr == 0) {
597 pr_info("Device address not set, will not use\n");
598 superio_exit();
599 return -ENODEV;
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);
612 superio_exit();
613 return addr;
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()) {
621 superio_select();
622 pr_info("Disabling device\n");
623 superio_outb(SUPERIO_REG_ACT, sio_data->activate);
624 superio_exit();
625 } else {
626 pr_warn("Failed to disable device\n");
631 #define CHECK 1
632 #define REQUEST 2
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,
643 struct device *dev)
645 static const u8 ports_m1[] = {
646 /* register, region length */
647 0x04, 1,
648 0x33, 4,
649 0x56, 7,
652 static const u8 ports_m2[] = {
653 /* register, region length */
654 0x04, 1,
655 0x09, 1,
656 0x2c, 2,
657 0x35, 4,
658 0x56, 7,
659 0x69, 4,
662 int i, ports_size, err;
663 const u8 *ports;
665 switch (type) {
666 case smsc47m1:
667 default:
668 ports = ports_m1;
669 ports_size = ARRAY_SIZE(ports_m1);
670 break;
671 case smsc47m2:
672 ports = ports_m2;
673 ports_size = ARRAY_SIZE(ports_m2);
674 break;
677 for (i = 0; i + 1 < ports_size; i += 2) {
678 unsigned short start = address + ports[i];
679 unsigned short len = ports[i + 1];
681 switch (action) {
682 case CHECK:
683 /* Only check for conflicts */
684 err = acpi_check_region(start, len, DRVNAME);
685 if (err)
686 return err;
687 break;
688 case REQUEST:
689 /* Request the resources */
690 if (!devm_request_region(dev, start, len, DRVNAME)) {
691 dev_err(dev,
692 "Region 0x%hx-0x%hx already in use!\n",
693 start, start + len);
694 return -EBUSY;
696 break;
700 return 0;
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;
720 int err;
721 int fan1, fan2, fan3, pwm1, pwm2, pwm3;
723 static const char * const names[] = {
724 "smsc47m1",
725 "smsc47m2",
728 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
729 err = smsc47m1_handle_resources(res->start, sio_data->type,
730 REQUEST, dev);
731 if (err < 0)
732 return err;
734 data = devm_kzalloc(dev, sizeof(struct smsc47m1_data), GFP_KERNEL);
735 if (!data)
736 return -ENOMEM;
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)
749 == 0x04;
750 pwm2 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(1)) & 0x05)
751 == 0x04;
752 if (data->type == smsc47m2) {
753 fan1 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN1)
754 & 0x0d) == 0x09;
755 fan2 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN2)
756 & 0x0d) == 0x09;
757 fan3 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN3)
758 & 0x0d) == 0x0d;
759 pwm3 = (smsc47m1_read_value(data, SMSC47M2_REG_PPIN3)
760 & 0x0d) == 0x08;
761 } else {
762 fan1 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(0))
763 & 0x05) == 0x05;
764 fan2 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(1))
765 & 0x05) == 0x05;
766 fan3 = 0;
767 pwm3 = 0;
769 if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) {
770 dev_warn(dev, "Device not configured, will not use\n");
771 return -ENODEV;
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
780 * function.
782 smsc47m1_update_device(dev, 1);
784 /* Register sysfs hooks */
785 if (fan1) {
786 err = sysfs_create_group(&dev->kobj,
787 &smsc47m1_group_fan1);
788 if (err)
789 goto error_remove_files;
790 } else
791 dev_dbg(dev, "Fan 1 not enabled by hardware, skipping\n");
793 if (fan2) {
794 err = sysfs_create_group(&dev->kobj,
795 &smsc47m1_group_fan2);
796 if (err)
797 goto error_remove_files;
798 } else
799 dev_dbg(dev, "Fan 2 not enabled by hardware, skipping\n");
801 if (fan3) {
802 err = sysfs_create_group(&dev->kobj,
803 &smsc47m1_group_fan3);
804 if (err)
805 goto error_remove_files;
806 } else if (data->type == smsc47m2)
807 dev_dbg(dev, "Fan 3 not enabled by hardware, skipping\n");
809 if (pwm1) {
810 err = sysfs_create_group(&dev->kobj,
811 &smsc47m1_group_pwm1);
812 if (err)
813 goto error_remove_files;
814 } else
815 dev_dbg(dev, "PWM 1 not enabled by hardware, skipping\n");
817 if (pwm2) {
818 err = sysfs_create_group(&dev->kobj,
819 &smsc47m1_group_pwm2);
820 if (err)
821 goto error_remove_files;
822 } else
823 dev_dbg(dev, "PWM 2 not enabled by hardware, skipping\n");
825 if (pwm3) {
826 err = sysfs_create_group(&dev->kobj,
827 &smsc47m1_group_pwm3);
828 if (err)
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);
834 if (err)
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;
843 return 0;
845 error_remove_files:
846 smsc47m1_remove_files(dev);
847 return err;
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);
857 return 0;
860 static struct platform_driver smsc47m1_driver = {
861 .driver = {
862 .name = DRVNAME,
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 = {
871 .start = address,
872 .end = address + SMSC_EXTENT - 1,
873 .name = DRVNAME,
874 .flags = IORESOURCE_IO,
876 int err;
878 err = smsc47m1_handle_resources(address, sio_data->type, CHECK, NULL);
879 if (err)
880 goto exit;
882 pdev = platform_device_alloc(DRVNAME, address);
883 if (!pdev) {
884 err = -ENOMEM;
885 pr_err("Device allocation failed\n");
886 goto exit;
889 err = platform_device_add_resources(pdev, &res, 1);
890 if (err) {
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));
897 if (err) {
898 pr_err("Platform data allocation failed\n");
899 goto exit_device_put;
902 err = platform_device_add(pdev);
903 if (err) {
904 pr_err("Device addition failed (%d)\n", err);
905 goto exit_device_put;
908 return 0;
910 exit_device_put:
911 platform_device_put(pdev);
912 exit:
913 return err;
916 static int __init sm_smsc47m1_init(void)
918 int err;
919 unsigned short address;
920 struct smsc47m1_sio_data sio_data;
922 err = smsc47m1_find(&sio_data);
923 if (err < 0)
924 return err;
925 address = err;
927 /* Sets global pdev as a side effect */
928 err = smsc47m1_device_add(address, &sio_data);
929 if (err)
930 return err;
932 err = platform_driver_probe(&smsc47m1_driver, smsc47m1_probe);
933 if (err)
934 goto exit_device;
936 return 0;
938 exit_device:
939 platform_device_unregister(pdev);
940 smsc47m1_restore(&sio_data);
941 return err;
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);