• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * smsc47m1.c - Part of lm_sensors, Linux kernel modules
3  *		for hardware monitoring
4  *
5  * Supports the SMSC LPC47B27x, LPC47M10x, LPC47M112, LPC47M13x,
6  * LPC47M14x, LPC47M15x, LPC47M192, LPC47M292 and LPC47M997
7  * Super-I/O chips.
8  *
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
13  *
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.
18  *
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.
23  *
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.
27  */
28 
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 
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>
44 
45 static unsigned short force_id;
46 module_param(force_id, ushort, 0);
47 MODULE_PARM_DESC(force_id, "Override the detected device ID");
48 
49 static struct platform_device *pdev;
50 
51 #define DRVNAME "smsc47m1"
52 enum chips { smsc47m1, smsc47m2 };
53 
54 /* Super-I/0 registers and commands */
55 
56 #define REG	0x2e	/* The register to read/write */
57 #define VAL	0x2f	/* The value to read/write */
58 
59 static inline void
superio_outb(int reg,int val)60 superio_outb(int reg, int val)
61 {
62 	outb(reg, REG);
63 	outb(val, VAL);
64 }
65 
66 static inline int
superio_inb(int reg)67 superio_inb(int reg)
68 {
69 	outb(reg, REG);
70 	return inb(VAL);
71 }
72 
73 /* logical device for fans is 0x0A */
74 #define superio_select() superio_outb(0x07, 0x0A)
75 
76 static inline int
superio_enter(void)77 superio_enter(void)
78 {
79 	if (!request_muxed_region(REG, 2, DRVNAME))
80 		return -EBUSY;
81 
82 	outb(0x55, REG);
83 	return 0;
84 }
85 
86 static inline void
superio_exit(void)87 superio_exit(void)
88 {
89 	outb(0xAA, REG);
90 	release_region(REG, 2);
91 }
92 
93 #define SUPERIO_REG_ACT		0x30
94 #define SUPERIO_REG_BASE	0x60
95 #define SUPERIO_REG_DEVID	0x20
96 #define SUPERIO_REG_DEVREV	0x21
97 
98 /* Logical device registers */
99 
100 #define SMSC_EXTENT		0x80
101 
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
107 
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 };
111 
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
118 
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)
128 
129 struct smsc47m1_data {
130 	unsigned short addr;
131 	const char *name;
132 	enum chips type;
133 	struct device *hwmon_dev;
134 
135 	struct mutex update_lock;
136 	unsigned long last_updated;	/* In jiffies */
137 
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) */
143 };
144 
145 struct smsc47m1_sio_data {
146 	enum chips type;
147 	u8 activate;		/* Remember initial device state */
148 };
149 
smsc47m1_read_value(struct smsc47m1_data * data,u8 reg)150 static inline int smsc47m1_read_value(struct smsc47m1_data *data, u8 reg)
151 {
152 	return inb_p(data->addr + reg);
153 }
154 
smsc47m1_write_value(struct smsc47m1_data * data,u8 reg,u8 value)155 static inline void smsc47m1_write_value(struct smsc47m1_data *data, u8 reg,
156 		u8 value)
157 {
158 	outb_p(value, data->addr + reg);
159 }
160 
smsc47m1_update_device(struct device * dev,int init)161 static struct smsc47m1_data *smsc47m1_update_device(struct device *dev,
162 		int init)
163 {
164 	struct smsc47m1_data *data = dev_get_drvdata(dev);
165 
166 	mutex_lock(&data->update_lock);
167 
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;
171 
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]);
179 		}
180 
181 		i = smsc47m1_read_value(data, SMSC47M1_REG_FANDIV);
182 		data->fan_div[0] = (i >> 4) & 0x03;
183 		data->fan_div[1] = i >> 6;
184 
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);
190 
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);
201 		}
202 
203 		data->last_updated = jiffies;
204 	}
205 
206 	mutex_unlock(&data->update_lock);
207 	return data;
208 }
209 
get_fan(struct device * dev,struct device_attribute * devattr,char * buf)210 static ssize_t get_fan(struct device *dev, struct device_attribute
211 		       *devattr, char *buf)
212 {
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;
216 	/*
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).
221 	 */
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);
227 }
228 
get_fan_min(struct device * dev,struct device_attribute * devattr,char * buf)229 static ssize_t get_fan_min(struct device *dev, struct device_attribute
230 			   *devattr, char *buf)
231 {
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);
238 }
239 
get_fan_div(struct device * dev,struct device_attribute * devattr,char * buf)240 static ssize_t get_fan_div(struct device *dev, struct device_attribute
241 			   *devattr, char *buf)
242 {
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]));
246 }
247 
get_fan_alarm(struct device * dev,struct device_attribute * devattr,char * buf)248 static ssize_t get_fan_alarm(struct device *dev, struct device_attribute
249 			     *devattr, char *buf)
250 {
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);
254 }
255 
get_pwm(struct device * dev,struct device_attribute * devattr,char * buf)256 static ssize_t get_pwm(struct device *dev, struct device_attribute
257 		       *devattr, char *buf)
258 {
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]));
262 }
263 
get_pwm_en(struct device * dev,struct device_attribute * devattr,char * buf)264 static ssize_t get_pwm_en(struct device *dev, struct device_attribute
265 			  *devattr, char *buf)
266 {
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]));
270 }
271 
alarms_show(struct device * dev,struct device_attribute * devattr,char * buf)272 static ssize_t alarms_show(struct device *dev,
273 			   struct device_attribute *devattr, char *buf)
274 {
275 	struct smsc47m1_data *data = smsc47m1_update_device(dev, 0);
276 	return sprintf(buf, "%d\n", data->alarms);
277 }
278 
set_fan_min(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)279 static ssize_t set_fan_min(struct device *dev, struct device_attribute
280 			   *devattr, const char *buf, size_t count)
281 {
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;
288 
289 	err = kstrtol(buf, 10, &val);
290 	if (err)
291 		return err;
292 
293 	mutex_lock(&data->update_lock);
294 	rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]);
295 
296 	if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) {
297 		mutex_unlock(&data->update_lock);
298 		return -EINVAL;
299 	}
300 
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);
305 
306 	return count;
307 }
308 
309 /*
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.
314  */
set_fan_div(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)315 static ssize_t set_fan_div(struct device *dev, struct device_attribute
316 			   *devattr, const char *buf, size_t count)
317 {
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]);
325 
326 	err = kstrtol(buf, 10, &new_div);
327 	if (err)
328 		return err;
329 
330 	if (new_div == old_div) /* No change */
331 		return count;
332 
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;
350 	}
351 
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;
365 	}
366 
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);
374 
375 	return count;
376 }
377 
set_pwm(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)378 static ssize_t set_pwm(struct device *dev, struct device_attribute
379 		       *devattr, const char *buf, size_t count)
380 {
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;
386 
387 	err = kstrtol(buf, 10, &val);
388 	if (err)
389 		return err;
390 
391 	if (val < 0 || val > 255)
392 		return -EINVAL;
393 
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);
400 
401 	return count;
402 }
403 
set_pwm_en(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)404 static ssize_t set_pwm_en(struct device *dev, struct device_attribute
405 			  *devattr, const char *buf, size_t count)
406 {
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;
412 
413 	err = kstrtoul(buf, 10, &val);
414 	if (err)
415 		return err;
416 
417 	if (val > 1)
418 		return -EINVAL;
419 
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);
426 
427 	return count;
428 }
429 
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)
443 
444 fan_present(1);
445 fan_present(2);
446 fan_present(3);
447 
448 static DEVICE_ATTR_RO(alarms);
449 
name_show(struct device * dev,struct device_attribute * devattr,char * buf)450 static ssize_t name_show(struct device *dev, struct device_attribute
451 			 *devattr, char *buf)
452 {
453 	struct smsc47m1_data *data = dev_get_drvdata(dev);
454 
455 	return sprintf(buf, "%s\n", data->name);
456 }
457 static DEVICE_ATTR_RO(name);
458 
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
465 };
466 
467 static const struct attribute_group smsc47m1_group_fan1 = {
468 	.attrs = smsc47m1_attributes_fan1,
469 };
470 
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
477 };
478 
479 static const struct attribute_group smsc47m1_group_fan2 = {
480 	.attrs = smsc47m1_attributes_fan2,
481 };
482 
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
489 };
490 
491 static const struct attribute_group smsc47m1_group_fan3 = {
492 	.attrs = smsc47m1_attributes_fan3,
493 };
494 
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
499 };
500 
501 static const struct attribute_group smsc47m1_group_pwm1 = {
502 	.attrs = smsc47m1_attributes_pwm1,
503 };
504 
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
509 };
510 
511 static const struct attribute_group smsc47m1_group_pwm2 = {
512 	.attrs = smsc47m1_attributes_pwm2,
513 };
514 
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
519 };
520 
521 static const struct attribute_group smsc47m1_group_pwm3 = {
522 	.attrs = smsc47m1_attributes_pwm3,
523 };
524 
525 static struct attribute *smsc47m1_attributes[] = {
526 	&dev_attr_alarms.attr,
527 	&dev_attr_name.attr,
528 	NULL
529 };
530 
531 static const struct attribute_group smsc47m1_group = {
532 	.attrs = smsc47m1_attributes,
533 };
534 
smsc47m1_find(struct smsc47m1_sio_data * sio_data)535 static int __init smsc47m1_find(struct smsc47m1_sio_data *sio_data)
536 {
537 	u8 val;
538 	unsigned short addr;
539 	int err;
540 
541 	err = superio_enter();
542 	if (err)
543 		return err;
544 
545 	val = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
546 
547 	/*
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.
560 	 */
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;
583 		}
584 
585 		pr_info("Found SMSC LPC47M292\n");
586 		sio_data->type = smsc47m2;
587 		break;
588 	default:
589 		superio_exit();
590 		return -ENODEV;
591 	}
592 
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;
600 	}
601 
602 	/*
603 	 * Enable only if address is set (needed at least on the
604 	 * Compaq Presario S4000NX)
605 	 */
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);
610 	}
611 
612 	superio_exit();
613 	return addr;
614 }
615 
616 /* Restore device to its initial state */
smsc47m1_restore(const struct smsc47m1_sio_data * sio_data)617 static void smsc47m1_restore(const struct smsc47m1_sio_data *sio_data)
618 {
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");
627 		}
628 	}
629 }
630 
631 #define CHECK		1
632 #define REQUEST		2
633 
634 /*
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.
640  */
smsc47m1_handle_resources(unsigned short address,enum chips type,int action,struct device * dev)641 static int __init smsc47m1_handle_resources(unsigned short address,
642 					    enum chips type, int action,
643 					    struct device *dev)
644 {
645 	static const u8 ports_m1[] = {
646 		/* register, region length */
647 		0x04, 1,
648 		0x33, 4,
649 		0x56, 7,
650 	};
651 
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,
660 	};
661 
662 	int i, ports_size, err;
663 	const u8 *ports;
664 
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;
675 	}
676 
677 	for (i = 0; i + 1 < ports_size; i += 2) {
678 		unsigned short start = address + ports[i];
679 		unsigned short len = ports[i + 1];
680 
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;
695 			}
696 			break;
697 		}
698 	}
699 
700 	return 0;
701 }
702 
smsc47m1_remove_files(struct device * dev)703 static void smsc47m1_remove_files(struct device *dev)
704 {
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);
712 }
713 
smsc47m1_probe(struct platform_device * pdev)714 static int __init smsc47m1_probe(struct platform_device *pdev)
715 {
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;
722 
723 	static const char * const names[] = {
724 		"smsc47m1",
725 		"smsc47m2",
726 	};
727 
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;
733 
734 	data = devm_kzalloc(dev, sizeof(struct smsc47m1_data), GFP_KERNEL);
735 	if (!data)
736 		return -ENOMEM;
737 
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);
743 
744 	/*
745 	 * If no function is properly configured, there's no point in
746 	 * actually registering the chip.
747 	 */
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;
768 	}
769 	if (!(fan1 || fan2 || fan3 || pwm1 || pwm2 || pwm3)) {
770 		dev_warn(dev, "Device not configured, will not use\n");
771 		return -ENODEV;
772 	}
773 
774 	/*
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.
781 	 */
782 	smsc47m1_update_device(dev, 1);
783 
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");
792 
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");
800 
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");
808 
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");
816 
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");
824 
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");
832 
833 	err = sysfs_create_group(&dev->kobj, &smsc47m1_group);
834 	if (err)
835 		goto error_remove_files;
836 
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;
841 	}
842 
843 	return 0;
844 
845 error_remove_files:
846 	smsc47m1_remove_files(dev);
847 	return err;
848 }
849 
smsc47m1_remove(struct platform_device * pdev)850 static int __exit smsc47m1_remove(struct platform_device *pdev)
851 {
852 	struct smsc47m1_data *data = platform_get_drvdata(pdev);
853 
854 	hwmon_device_unregister(data->hwmon_dev);
855 	smsc47m1_remove_files(&pdev->dev);
856 
857 	return 0;
858 }
859 
860 static struct platform_driver smsc47m1_driver = {
861 	.driver = {
862 		.name	= DRVNAME,
863 	},
864 	.remove		= __exit_p(smsc47m1_remove),
865 };
866 
smsc47m1_device_add(unsigned short address,const struct smsc47m1_sio_data * sio_data)867 static int __init smsc47m1_device_add(unsigned short address,
868 				      const struct smsc47m1_sio_data *sio_data)
869 {
870 	struct resource res = {
871 		.start	= address,
872 		.end	= address + SMSC_EXTENT - 1,
873 		.name	= DRVNAME,
874 		.flags	= IORESOURCE_IO,
875 	};
876 	int err;
877 
878 	err = smsc47m1_handle_resources(address, sio_data->type, CHECK, NULL);
879 	if (err)
880 		goto exit;
881 
882 	pdev = platform_device_alloc(DRVNAME, address);
883 	if (!pdev) {
884 		err = -ENOMEM;
885 		pr_err("Device allocation failed\n");
886 		goto exit;
887 	}
888 
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;
893 	}
894 
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;
900 	}
901 
902 	err = platform_device_add(pdev);
903 	if (err) {
904 		pr_err("Device addition failed (%d)\n", err);
905 		goto exit_device_put;
906 	}
907 
908 	return 0;
909 
910 exit_device_put:
911 	platform_device_put(pdev);
912 exit:
913 	return err;
914 }
915 
sm_smsc47m1_init(void)916 static int __init sm_smsc47m1_init(void)
917 {
918 	int err;
919 	unsigned short address;
920 	struct smsc47m1_sio_data sio_data;
921 
922 	err = smsc47m1_find(&sio_data);
923 	if (err < 0)
924 		return err;
925 	address = err;
926 
927 	/* Sets global pdev as a side effect */
928 	err = smsc47m1_device_add(address, &sio_data);
929 	if (err)
930 		return err;
931 
932 	err = platform_driver_probe(&smsc47m1_driver, smsc47m1_probe);
933 	if (err)
934 		goto exit_device;
935 
936 	return 0;
937 
938 exit_device:
939 	platform_device_unregister(pdev);
940 	smsc47m1_restore(&sio_data);
941 	return err;
942 }
943 
sm_smsc47m1_exit(void)944 static void __exit sm_smsc47m1_exit(void)
945 {
946 	platform_driver_unregister(&smsc47m1_driver);
947 	smsc47m1_restore(dev_get_platdata(&pdev->dev));
948 	platform_device_unregister(pdev);
949 }
950 
951 MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
952 MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver");
953 MODULE_LICENSE("GPL");
954 
955 module_init(sm_smsc47m1_init);
956 module_exit(sm_smsc47m1_exit);
957