xref: /openbmc/linux/drivers/hwmon/smsc47m1.c (revision d87c25e8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * smsc47m1.c - Part of lm_sensors, Linux kernel modules
4  *		for hardware monitoring
5  *
6  * Supports the SMSC LPC47B27x, LPC47M10x, LPC47M112, LPC47M13x,
7  * LPC47M14x, LPC47M15x, LPC47M192, LPC47M292 and LPC47M997
8  * Super-I/O chips.
9  *
10  * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
11  * Copyright (C) 2004-2007 Jean Delvare <jdelvare@suse.de>
12  * Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com>
13  *			and Jean Delvare
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/ioport.h>
21 #include <linux/jiffies.h>
22 #include <linux/platform_device.h>
23 #include <linux/hwmon.h>
24 #include <linux/hwmon-sysfs.h>
25 #include <linux/err.h>
26 #include <linux/init.h>
27 #include <linux/mutex.h>
28 #include <linux/sysfs.h>
29 #include <linux/acpi.h>
30 #include <linux/io.h>
31 
32 static unsigned short force_id;
33 module_param(force_id, ushort, 0);
34 MODULE_PARM_DESC(force_id, "Override the detected device ID");
35 
36 static struct platform_device *pdev;
37 
38 #define DRVNAME "smsc47m1"
39 enum chips { smsc47m1, smsc47m2 };
40 
41 /* Super-I/0 registers and commands */
42 
43 #define REG	0x2e	/* The register to read/write */
44 #define VAL	0x2f	/* The value to read/write */
45 
46 static inline void
47 superio_outb(int reg, int val)
48 {
49 	outb(reg, REG);
50 	outb(val, VAL);
51 }
52 
53 static inline int
54 superio_inb(int reg)
55 {
56 	outb(reg, REG);
57 	return inb(VAL);
58 }
59 
60 /* logical device for fans is 0x0A */
61 #define superio_select() superio_outb(0x07, 0x0A)
62 
63 static inline int
64 superio_enter(void)
65 {
66 	if (!request_muxed_region(REG, 2, DRVNAME))
67 		return -EBUSY;
68 
69 	outb(0x55, REG);
70 	return 0;
71 }
72 
73 static inline void
74 superio_exit(void)
75 {
76 	outb(0xAA, REG);
77 	release_region(REG, 2);
78 }
79 
80 #define SUPERIO_REG_ACT		0x30
81 #define SUPERIO_REG_BASE	0x60
82 #define SUPERIO_REG_DEVID	0x20
83 #define SUPERIO_REG_DEVREV	0x21
84 
85 /* Logical device registers */
86 
87 #define SMSC_EXTENT		0x80
88 
89 /* nr is 0 or 1 in the macros below */
90 #define SMSC47M1_REG_ALARM		0x04
91 #define SMSC47M1_REG_TPIN(nr)		(0x34 - (nr))
92 #define SMSC47M1_REG_PPIN(nr)		(0x36 - (nr))
93 #define SMSC47M1_REG_FANDIV		0x58
94 
95 static const u8 SMSC47M1_REG_FAN[3]		= { 0x59, 0x5a, 0x6b };
96 static const u8 SMSC47M1_REG_FAN_PRELOAD[3]	= { 0x5b, 0x5c, 0x6c };
97 static const u8 SMSC47M1_REG_PWM[3]		= { 0x56, 0x57, 0x69 };
98 
99 #define SMSC47M2_REG_ALARM6		0x09
100 #define SMSC47M2_REG_TPIN1		0x38
101 #define SMSC47M2_REG_TPIN2		0x37
102 #define SMSC47M2_REG_TPIN3		0x2d
103 #define SMSC47M2_REG_PPIN3		0x2c
104 #define SMSC47M2_REG_FANDIV3		0x6a
105 
106 #define MIN_FROM_REG(reg, div)		((reg) >= 192 ? 0 : \
107 					 983040 / ((192 - (reg)) * (div)))
108 #define FAN_FROM_REG(reg, div, preload)	((reg) <= (preload) || (reg) == 255 ? \
109 					 0 : \
110 					 983040 / (((reg) - (preload)) * (div)))
111 #define DIV_FROM_REG(reg)		(1 << (reg))
112 #define PWM_FROM_REG(reg)		(((reg) & 0x7E) << 1)
113 #define PWM_EN_FROM_REG(reg)		((~(reg)) & 0x01)
114 #define PWM_TO_REG(reg)			(((reg) >> 1) & 0x7E)
115 
116 struct smsc47m1_data {
117 	unsigned short addr;
118 	const char *name;
119 	enum chips type;
120 	struct device *hwmon_dev;
121 
122 	struct mutex update_lock;
123 	unsigned long last_updated;	/* In jiffies */
124 
125 	u8 fan[3];		/* Register value */
126 	u8 fan_preload[3];	/* Register value */
127 	u8 fan_div[3];		/* Register encoding, shifted right */
128 	u8 alarms;		/* Register encoding */
129 	u8 pwm[3];		/* Register value (bit 0 is disable) */
130 };
131 
132 struct smsc47m1_sio_data {
133 	enum chips type;
134 	u8 activate;		/* Remember initial device state */
135 };
136 
137 static inline int smsc47m1_read_value(struct smsc47m1_data *data, u8 reg)
138 {
139 	return inb_p(data->addr + reg);
140 }
141 
142 static inline void smsc47m1_write_value(struct smsc47m1_data *data, u8 reg,
143 		u8 value)
144 {
145 	outb_p(value, data->addr + reg);
146 }
147 
148 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
149 		int init)
150 {
151 	struct smsc47m1_data *data = dev_get_drvdata(dev);
152 
153 	mutex_lock(&data->update_lock);
154 
155 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) {
156 		int i, fan_nr;
157 		fan_nr = data->type == smsc47m2 ? 3 : 2;
158 
159 		for (i = 0; i < fan_nr; i++) {
160 			data->fan[i] = smsc47m1_read_value(data,
161 				       SMSC47M1_REG_FAN[i]);
162 			data->fan_preload[i] = smsc47m1_read_value(data,
163 					       SMSC47M1_REG_FAN_PRELOAD[i]);
164 			data->pwm[i] = smsc47m1_read_value(data,
165 				       SMSC47M1_REG_PWM[i]);
166 		}
167 
168 		i = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV);
169 		data->fan_div[0] = (i >> 4) & 0x03;
170 		data->fan_div[1] = i >> 6;
171 
172 		data->alarms = smsc47m1_read_value(data,
173 			       SMSC47M1_REG_ALARM) >> 6;
174 		/* Clear alarms if needed */
175 		if (data->alarms)
176 			smsc47m1_write_value(data, SMSC47M1_REG_ALARM, 0xC0);
177 
178 		if (fan_nr >= 3) {
179 			data->fan_div[2] = (smsc47m1_read_value(data,
180 					    SMSC47M2_REG_FANDIV3) >> 4) & 0x03;
181 			data->alarms |= (smsc47m1_read_value(data,
182 					 SMSC47M2_REG_ALARM6) & 0x40) >> 4;
183 			/* Clear alarm if needed */
184 			if (data->alarms & 0x04)
185 				smsc47m1_write_value(data,
186 						     SMSC47M2_REG_ALARM6,
187 						     0x40);
188 		}
189 
190 		data->last_updated = jiffies;
191 	}
192 
193 	mutex_unlock(&data->update_lock);
194 	return data;
195 }
196 
197 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
198 			char *buf)
199 {
200 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
201 	struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
202 	int nr = attr->index;
203 	/*
204 	 * This chip (stupidly) stops monitoring fan speed if PWM is
205 	 * enabled and duty cycle is 0%. This is fine if the monitoring
206 	 * and control concern the same fan, but troublesome if they are
207 	 * not (which could as well happen).
208 	 */
209 	int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 :
210 		  FAN_FROM_REG(data->fan[nr],
211 			       DIV_FROM_REG(data->fan_div[nr]),
212 			       data->fan_preload[nr]);
213 	return sprintf(buf, "%d\n", rpm);
214 }
215 
216 static ssize_t fan_min_show(struct device *dev,
217 			    struct device_attribute *devattr, char *buf)
218 {
219 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
220 	struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
221 	int nr = attr->index;
222 	int rpm = MIN_FROM_REG(data->fan_preload[nr],
223 			       DIV_FROM_REG(data->fan_div[nr]));
224 	return sprintf(buf, "%d\n", rpm);
225 }
226 
227 static ssize_t fan_div_show(struct device *dev,
228 			    struct device_attribute *devattr, char *buf)
229 {
230 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
231 	struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
232 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
233 }
234 
235 static ssize_t fan_alarm_show(struct device *dev,
236 			      struct device_attribute *devattr, char *buf)
237 {
238 	int bitnr = to_sensor_dev_attr(devattr)->index;
239 	struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
240 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
241 }
242 
243 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
244 			char *buf)
245 {
246 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
247 	struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
248 	return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[attr->index]));
249 }
250 
251 static ssize_t pwm_en_show(struct device *dev,
252 			   struct device_attribute *devattr, char *buf)
253 {
254 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
255 	struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
256 	return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[attr->index]));
257 }
258 
259 static ssize_t alarms_show(struct device *dev,
260 			   struct device_attribute *devattr, char *buf)
261 {
262 	struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
263 	return sprintf(buf, "%d\n", data->alarms);
264 }
265 
266 static ssize_t fan_min_store(struct device *dev,
267 			     struct device_attribute *devattr,
268 			     const char *buf, size_t count)
269 {
270 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
271 	struct smsc47m1_data *data = dev_get_drvdata(dev);
272 	int nr = attr->index;
273 	long rpmdiv;
274 	long val;
275 	int err;
276 
277 	err = kstrtol(buf, 10, &val);
278 	if (err)
279 		return err;
280 
281 	mutex_lock(&data->update_lock);
282 	rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
283 
284 	if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
285 		mutex_unlock(&data->update_lock);
286 		return -EINVAL;
287 	}
288 
289 	data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv);
290 	smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
291 			     data->fan_preload[nr]);
292 	mutex_unlock(&data->update_lock);
293 
294 	return count;
295 }
296 
297 /*
298  * Note: we save and restore the fan minimum here, because its value is
299  * determined in part by the fan clock divider.  This follows the principle
300  * of least surprise; the user doesn't expect the fan minimum to change just
301  * because the divider changed.
302  */
303 static ssize_t fan_div_store(struct device *dev,
304 			     struct device_attribute *devattr,
305 			     const char *buf, size_t count)
306 {
307 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
308 	struct smsc47m1_data *data = dev_get_drvdata(dev);
309 	int nr = attr->index;
310 	long new_div;
311 	int err;
312 	long tmp;
313 	u8 old_div = DIV_FROM_REG(data->fan_div[nr]);
314 
315 	err = kstrtol(buf, 10, &new_div);
316 	if (err)
317 		return err;
318 
319 	if (new_div == old_div) /* No change */
320 		return count;
321 
322 	mutex_lock(&data->update_lock);
323 	switch (new_div) {
324 	case 1:
325 		data->fan_div[nr] = 0;
326 		break;
327 	case 2:
328 		data->fan_div[nr] = 1;
329 		break;
330 	case 4:
331 		data->fan_div[nr] = 2;
332 		break;
333 	case 8:
334 		data->fan_div[nr] = 3;
335 		break;
336 	default:
337 		mutex_unlock(&data->update_lock);
338 		return -EINVAL;
339 	}
340 
341 	switch (nr) {
342 	case 0:
343 	case 1:
344 		tmp = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV)
345 		      & ~(0x03 << (4 + 2 * nr));
346 		tmp |= data->fan_div[nr] << (4 + 2 * nr);
347 		smsc47m1_write_value(data, SMSC47M1_REG_FANDIV, tmp);
348 		break;
349 	case 2:
350 		tmp = smsc47m1_read_value(data, SMSC47M2_REG_FANDIV3) & 0xCF;
351 		tmp |= data->fan_div[2] << 4;
352 		smsc47m1_write_value(data, SMSC47M2_REG_FANDIV3, tmp);
353 		break;
354 	default:
355 		BUG();
356 	}
357 
358 	/* Preserve fan min */
359 	tmp = 192 - (old_div * (192 - data->fan_preload[nr])
360 		     + new_div / 2) / new_div;
361 	data->fan_preload[nr] = clamp_val(tmp, 0, 191);
362 	smsc47m1_write_value(data, SMSC47M1_REG_FAN_PRELOAD[nr],
363 			     data->fan_preload[nr]);
364 	mutex_unlock(&data->update_lock);
365 
366 	return count;
367 }
368 
369 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
370 			 const char *buf, size_t count)
371 {
372 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
373 	struct smsc47m1_data *data = dev_get_drvdata(dev);
374 	int nr = attr->index;
375 	long val;
376 	int err;
377 
378 	err = kstrtol(buf, 10, &val);
379 	if (err)
380 		return err;
381 
382 	if (val < 0 || val > 255)
383 		return -EINVAL;
384 
385 	mutex_lock(&data->update_lock);
386 	data->pwm[nr] &= 0x81; /* Preserve additional bits */
387 	data->pwm[nr] |= PWM_TO_REG(val);
388 	smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
389 			     data->pwm[nr]);
390 	mutex_unlock(&data->update_lock);
391 
392 	return count;
393 }
394 
395 static ssize_t pwm_en_store(struct device *dev,
396 			    struct device_attribute *devattr, const char *buf,
397 			    size_t count)
398 {
399 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
400 	struct smsc47m1_data *data = dev_get_drvdata(dev);
401 	int nr = attr->index;
402 	unsigned long val;
403 	int err;
404 
405 	err = kstrtoul(buf, 10, &val);
406 	if (err)
407 		return err;
408 
409 	if (val > 1)
410 		return -EINVAL;
411 
412 	mutex_lock(&data->update_lock);
413 	data->pwm[nr] &= 0xFE; /* preserve the other bits */
414 	data->pwm[nr] |= !val;
415 	smsc47m1_write_value(data, SMSC47M1_REG_PWM[nr],
416 			     data->pwm[nr]);
417 	mutex_unlock(&data->update_lock);
418 
419 	return count;
420 }
421 
422 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
423 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
424 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
425 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
426 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
427 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_en, 0);
428 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
429 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
430 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
431 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
432 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
433 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_en, 1);
434 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
435 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
436 static SENSOR_DEVICE_ATTR_RW(fan3_div, fan_div, 2);
437 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, fan_alarm, 2);
438 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
439 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_en, 2);
440 
441 static DEVICE_ATTR_RO(alarms);
442 
443 static ssize_t name_show(struct device *dev, struct device_attribute
444 			 *devattr, char *buf)
445 {
446 	struct smsc47m1_data *data = dev_get_drvdata(dev);
447 
448 	return sprintf(buf, "%s\n", data->name);
449 }
450 static DEVICE_ATTR_RO(name);
451 
452 static struct attribute *smsc47m1_attributes_fan1[] = {
453 	&sensor_dev_attr_fan1_input.dev_attr.attr,
454 	&sensor_dev_attr_fan1_min.dev_attr.attr,
455 	&sensor_dev_attr_fan1_div.dev_attr.attr,
456 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
457 	NULL
458 };
459 
460 static const struct attribute_group smsc47m1_group_fan1 = {
461 	.attrs = smsc47m1_attributes_fan1,
462 };
463 
464 static struct attribute *smsc47m1_attributes_fan2[] = {
465 	&sensor_dev_attr_fan2_input.dev_attr.attr,
466 	&sensor_dev_attr_fan2_min.dev_attr.attr,
467 	&sensor_dev_attr_fan2_div.dev_attr.attr,
468 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
469 	NULL
470 };
471 
472 static const struct attribute_group smsc47m1_group_fan2 = {
473 	.attrs = smsc47m1_attributes_fan2,
474 };
475 
476 static struct attribute *smsc47m1_attributes_fan3[] = {
477 	&sensor_dev_attr_fan3_input.dev_attr.attr,
478 	&sensor_dev_attr_fan3_min.dev_attr.attr,
479 	&sensor_dev_attr_fan3_div.dev_attr.attr,
480 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
481 	NULL
482 };
483 
484 static const struct attribute_group smsc47m1_group_fan3 = {
485 	.attrs = smsc47m1_attributes_fan3,
486 };
487 
488 static struct attribute *smsc47m1_attributes_pwm1[] = {
489 	&sensor_dev_attr_pwm1.dev_attr.attr,
490 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
491 	NULL
492 };
493 
494 static const struct attribute_group smsc47m1_group_pwm1 = {
495 	.attrs = smsc47m1_attributes_pwm1,
496 };
497 
498 static struct attribute *smsc47m1_attributes_pwm2[] = {
499 	&sensor_dev_attr_pwm2.dev_attr.attr,
500 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
501 	NULL
502 };
503 
504 static const struct attribute_group smsc47m1_group_pwm2 = {
505 	.attrs = smsc47m1_attributes_pwm2,
506 };
507 
508 static struct attribute *smsc47m1_attributes_pwm3[] = {
509 	&sensor_dev_attr_pwm3.dev_attr.attr,
510 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
511 	NULL
512 };
513 
514 static const struct attribute_group smsc47m1_group_pwm3 = {
515 	.attrs = smsc47m1_attributes_pwm3,
516 };
517 
518 static struct attribute *smsc47m1_attributes[] = {
519 	&dev_attr_alarms.attr,
520 	&dev_attr_name.attr,
521 	NULL
522 };
523 
524 static const struct attribute_group smsc47m1_group = {
525 	.attrs = smsc47m1_attributes,
526 };
527 
528 static int __init smsc47m1_find(struct smsc47m1_sio_data *sio_data)
529 {
530 	u8 val;
531 	unsigned short addr;
532 	int err;
533 
534 	err = superio_enter();
535 	if (err)
536 		return err;
537 
538 	val = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
539 
540 	/*
541 	 * SMSC LPC47M10x/LPC47M112/LPC47M13x (device id 0x59), LPC47M14x
542 	 * (device id 0x5F) and LPC47B27x (device id 0x51) have fan control.
543 	 * The LPC47M15x and LPC47M192 chips "with hardware monitoring block"
544 	 * can do much more besides (device id 0x60).
545 	 * The LPC47M997 is undocumented, but seems to be compatible with
546 	 * the LPC47M192, and has the same device id.
547 	 * The LPC47M292 (device id 0x6B) is somewhat compatible, but it
548 	 * supports a 3rd fan, and the pin configuration registers are
549 	 * unfortunately different.
550 	 * The LPC47M233 has the same device id (0x6B) but is not compatible.
551 	 * We check the high bit of the device revision register to
552 	 * differentiate them.
553 	 */
554 	switch (val) {
555 	case 0x51:
556 		pr_info("Found SMSC LPC47B27x\n");
557 		sio_data->type = smsc47m1;
558 		break;
559 	case 0x59:
560 		pr_info("Found SMSC LPC47M10x/LPC47M112/LPC47M13x\n");
561 		sio_data->type = smsc47m1;
562 		break;
563 	case 0x5F:
564 		pr_info("Found SMSC LPC47M14x\n");
565 		sio_data->type = smsc47m1;
566 		break;
567 	case 0x60:
568 		pr_info("Found SMSC LPC47M15x/LPC47M192/LPC47M997\n");
569 		sio_data->type = smsc47m1;
570 		break;
571 	case 0x6B:
572 		if (superio_inb(SUPERIO_REG_DEVREV) & 0x80) {
573 			pr_debug("Found SMSC LPC47M233, unsupported\n");
574 			superio_exit();
575 			return -ENODEV;
576 		}
577 
578 		pr_info("Found SMSC LPC47M292\n");
579 		sio_data->type = smsc47m2;
580 		break;
581 	default:
582 		superio_exit();
583 		return -ENODEV;
584 	}
585 
586 	superio_select();
587 	addr = (superio_inb(SUPERIO_REG_BASE) << 8)
588 	      |  superio_inb(SUPERIO_REG_BASE + 1);
589 	if (addr == 0) {
590 		pr_info("Device address not set, will not use\n");
591 		superio_exit();
592 		return -ENODEV;
593 	}
594 
595 	/*
596 	 * Enable only if address is set (needed at least on the
597 	 * Compaq Presario S4000NX)
598 	 */
599 	sio_data->activate = superio_inb(SUPERIO_REG_ACT);
600 	if ((sio_data->activate & 0x01) == 0) {
601 		pr_info("Enabling device\n");
602 		superio_outb(SUPERIO_REG_ACT, sio_data->activate | 0x01);
603 	}
604 
605 	superio_exit();
606 	return addr;
607 }
608 
609 /* Restore device to its initial state */
610 static void smsc47m1_restore(const struct smsc47m1_sio_data *sio_data)
611 {
612 	if ((sio_data->activate & 0x01) == 0) {
613 		if (!superio_enter()) {
614 			superio_select();
615 			pr_info("Disabling device\n");
616 			superio_outb(SUPERIO_REG_ACT, sio_data->activate);
617 			superio_exit();
618 		} else {
619 			pr_warn("Failed to disable device\n");
620 		}
621 	}
622 }
623 
624 #define CHECK		1
625 #define REQUEST		2
626 
627 /*
628  * This function can be used to:
629  *  - test for resource conflicts with ACPI
630  *  - request the resources
631  * We only allocate the I/O ports we really need, to minimize the risk of
632  * conflicts with ACPI or with other drivers.
633  */
634 static int __init smsc47m1_handle_resources(unsigned short address,
635 					    enum chips type, int action,
636 					    struct device *dev)
637 {
638 	static const u8 ports_m1[] = {
639 		/* register, region length */
640 		0x04, 1,
641 		0x33, 4,
642 		0x56, 7,
643 	};
644 
645 	static const u8 ports_m2[] = {
646 		/* register, region length */
647 		0x04, 1,
648 		0x09, 1,
649 		0x2c, 2,
650 		0x35, 4,
651 		0x56, 7,
652 		0x69, 4,
653 	};
654 
655 	int i, ports_size, err;
656 	const u8 *ports;
657 
658 	switch (type) {
659 	case smsc47m1:
660 	default:
661 		ports = ports_m1;
662 		ports_size = ARRAY_SIZE(ports_m1);
663 		break;
664 	case smsc47m2:
665 		ports = ports_m2;
666 		ports_size = ARRAY_SIZE(ports_m2);
667 		break;
668 	}
669 
670 	for (i = 0; i + 1 < ports_size; i += 2) {
671 		unsigned short start = address + ports[i];
672 		unsigned short len = ports[i + 1];
673 
674 		switch (action) {
675 		case CHECK:
676 			/* Only check for conflicts */
677 			err = acpi_check_region(start, len, DRVNAME);
678 			if (err)
679 				return err;
680 			break;
681 		case REQUEST:
682 			/* Request the resources */
683 			if (!devm_request_region(dev, start, len, DRVNAME)) {
684 				dev_err(dev,
685 					"Region 0x%x-0x%x already in use!\n",
686 					start, start + len);
687 				return -EBUSY;
688 			}
689 			break;
690 		}
691 	}
692 
693 	return 0;
694 }
695 
696 static void smsc47m1_remove_files(struct device *dev)
697 {
698 	sysfs_remove_group(&dev->kobj, &smsc47m1_group);
699 	sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan1);
700 	sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan2);
701 	sysfs_remove_group(&dev->kobj, &smsc47m1_group_fan3);
702 	sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm1);
703 	sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm2);
704 	sysfs_remove_group(&dev->kobj, &smsc47m1_group_pwm3);
705 }
706 
707 static int __init smsc47m1_probe(struct platform_device *pdev)
708 {
709 	struct device *dev = &pdev->dev;
710 	struct smsc47m1_sio_data *sio_data = dev_get_platdata(dev);
711 	struct smsc47m1_data *data;
712 	struct resource *res;
713 	int err;
714 	int fan1, fan2, fan3, pwm1, pwm2, pwm3;
715 
716 	static const char * const names[] = {
717 		"smsc47m1",
718 		"smsc47m2",
719 	};
720 
721 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
722 	err = smsc47m1_handle_resources(res->start, sio_data->type,
723 					REQUEST, dev);
724 	if (err < 0)
725 		return err;
726 
727 	data = devm_kzalloc(dev, sizeof(struct smsc47m1_data), GFP_KERNEL);
728 	if (!data)
729 		return -ENOMEM;
730 
731 	data->addr = res->start;
732 	data->type = sio_data->type;
733 	data->name = names[sio_data->type];
734 	mutex_init(&data->update_lock);
735 	platform_set_drvdata(pdev, data);
736 
737 	/*
738 	 * If no function is properly configured, there's no point in
739 	 * actually registering the chip.
740 	 */
741 	pwm1 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(0)) & 0x05)
742 	       == 0x04;
743 	pwm2 = (smsc47m1_read_value(data, SMSC47M1_REG_PPIN(1)) & 0x05)
744 	       == 0x04;
745 	if (data->type == smsc47m2) {
746 		fan1 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN1)
747 			& 0x0d) == 0x09;
748 		fan2 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN2)
749 			& 0x0d) == 0x09;
750 		fan3 = (smsc47m1_read_value(data, SMSC47M2_REG_TPIN3)
751 			& 0x0d) == 0x0d;
752 		pwm3 = (smsc47m1_read_value(data, SMSC47M2_REG_PPIN3)
753 			& 0x0d) == 0x08;
754 	} else {
755 		fan1 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(0))
756 			& 0x05) == 0x05;
757 		fan2 = (smsc47m1_read_value(data, SMSC47M1_REG_TPIN(1))
758 			& 0x05) == 0x05;
759 		fan3 = 0;
760 		pwm3 = 0;
761 	}
762 	if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) {
763 		dev_warn(dev, "Device not configured, will not use\n");
764 		return -ENODEV;
765 	}
766 
767 	/*
768 	 * Some values (fan min, clock dividers, pwm registers) may be
769 	 * needed before any update is triggered, so we better read them
770 	 * at least once here. We don't usually do it that way, but in
771 	 * this particular case, manually reading 5 registers out of 8
772 	 * doesn't make much sense and we're better using the existing
773 	 * function.
774 	 */
775 	smsc47m1_update_device(dev, 1);
776 
777 	/* Register sysfs hooks */
778 	if (fan1) {
779 		err = sysfs_create_group(&dev->kobj,
780 					 &smsc47m1_group_fan1);
781 		if (err)
782 			goto error_remove_files;
783 	} else
784 		dev_dbg(dev, "Fan 1 not enabled by hardware, skipping\n");
785 
786 	if (fan2) {
787 		err = sysfs_create_group(&dev->kobj,
788 					 &smsc47m1_group_fan2);
789 		if (err)
790 			goto error_remove_files;
791 	} else
792 		dev_dbg(dev, "Fan 2 not enabled by hardware, skipping\n");
793 
794 	if (fan3) {
795 		err = sysfs_create_group(&dev->kobj,
796 					 &smsc47m1_group_fan3);
797 		if (err)
798 			goto error_remove_files;
799 	} else if (data->type == smsc47m2)
800 		dev_dbg(dev, "Fan 3 not enabled by hardware, skipping\n");
801 
802 	if (pwm1) {
803 		err = sysfs_create_group(&dev->kobj,
804 					 &smsc47m1_group_pwm1);
805 		if (err)
806 			goto error_remove_files;
807 	} else
808 		dev_dbg(dev, "PWM 1 not enabled by hardware, skipping\n");
809 
810 	if (pwm2) {
811 		err = sysfs_create_group(&dev->kobj,
812 					 &smsc47m1_group_pwm2);
813 		if (err)
814 			goto error_remove_files;
815 	} else
816 		dev_dbg(dev, "PWM 2 not enabled by hardware, skipping\n");
817 
818 	if (pwm3) {
819 		err = sysfs_create_group(&dev->kobj,
820 					 &smsc47m1_group_pwm3);
821 		if (err)
822 			goto error_remove_files;
823 	} else if (data->type == smsc47m2)
824 		dev_dbg(dev, "PWM 3 not enabled by hardware, skipping\n");
825 
826 	err = sysfs_create_group(&dev->kobj, &smsc47m1_group);
827 	if (err)
828 		goto error_remove_files;
829 
830 	data->hwmon_dev = hwmon_device_register(dev);
831 	if (IS_ERR(data->hwmon_dev)) {
832 		err = PTR_ERR(data->hwmon_dev);
833 		goto error_remove_files;
834 	}
835 
836 	return 0;
837 
838 error_remove_files:
839 	smsc47m1_remove_files(dev);
840 	return err;
841 }
842 
843 static int __exit smsc47m1_remove(struct platform_device *pdev)
844 {
845 	struct smsc47m1_data *data = platform_get_drvdata(pdev);
846 
847 	hwmon_device_unregister(data->hwmon_dev);
848 	smsc47m1_remove_files(&pdev->dev);
849 
850 	return 0;
851 }
852 
853 static struct platform_driver smsc47m1_driver = {
854 	.driver = {
855 		.name	= DRVNAME,
856 	},
857 	.remove		= __exit_p(smsc47m1_remove),
858 };
859 
860 static int __init smsc47m1_device_add(unsigned short address,
861 				      const struct smsc47m1_sio_data *sio_data)
862 {
863 	struct resource res = {
864 		.start	= address,
865 		.end	= address + SMSC_EXTENT - 1,
866 		.name	= DRVNAME,
867 		.flags	= IORESOURCE_IO,
868 	};
869 	int err;
870 
871 	err = smsc47m1_handle_resources(address, sio_data->type, CHECK, NULL);
872 	if (err)
873 		goto exit;
874 
875 	pdev = platform_device_alloc(DRVNAME, address);
876 	if (!pdev) {
877 		err = -ENOMEM;
878 		pr_err("Device allocation failed\n");
879 		goto exit;
880 	}
881 
882 	err = platform_device_add_resources(pdev, &res, 1);
883 	if (err) {
884 		pr_err("Device resource addition failed (%d)\n", err);
885 		goto exit_device_put;
886 	}
887 
888 	err = platform_device_add_data(pdev, sio_data,
889 				       sizeof(struct smsc47m1_sio_data));
890 	if (err) {
891 		pr_err("Platform data allocation failed\n");
892 		goto exit_device_put;
893 	}
894 
895 	err = platform_device_add(pdev);
896 	if (err) {
897 		pr_err("Device addition failed (%d)\n", err);
898 		goto exit_device_put;
899 	}
900 
901 	return 0;
902 
903 exit_device_put:
904 	platform_device_put(pdev);
905 exit:
906 	return err;
907 }
908 
909 static int __init sm_smsc47m1_init(void)
910 {
911 	int err;
912 	unsigned short address;
913 	struct smsc47m1_sio_data sio_data;
914 
915 	err = smsc47m1_find(&sio_data);
916 	if (err < 0)
917 		return err;
918 	address = err;
919 
920 	/* Sets global pdev as a side effect */
921 	err = smsc47m1_device_add(address, &sio_data);
922 	if (err)
923 		return err;
924 
925 	err = platform_driver_probe(&smsc47m1_driver, smsc47m1_probe);
926 	if (err)
927 		goto exit_device;
928 
929 	return 0;
930 
931 exit_device:
932 	platform_device_unregister(pdev);
933 	smsc47m1_restore(&sio_data);
934 	return err;
935 }
936 
937 static void __exit sm_smsc47m1_exit(void)
938 {
939 	platform_driver_unregister(&smsc47m1_driver);
940 	smsc47m1_restore(dev_get_platdata(&pdev->dev));
941 	platform_device_unregister(pdev);
942 }
943 
944 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
945 MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
946 MODULE_LICENSE("GPL");
947 
948 module_init(sm_smsc47m1_init);
949 module_exit(sm_smsc47m1_exit);
950