xref: /openbmc/linux/drivers/hwmon/smsc47b397.c (revision e1f7c9ee)
1 /*
2  * smsc47b397.c - Part of lm_sensors, Linux kernel modules
3  * for hardware monitoring
4  *
5  * Supports the SMSC LPC47B397-NC Super-I/O chip.
6  *
7  * Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
8  * Copyright (C) 2004 Utilitek Systems, Inc.
9  *
10  * derived in part from smsc47m1.c:
11  * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
12  * Copyright (C) 2004 Jean Delvare <jdelvare@suse.de>
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/acpi.h>
42 #include <linux/io.h>
43 
44 static unsigned short force_id;
45 module_param(force_id, ushort, 0);
46 MODULE_PARM_DESC(force_id, "Override the detected device ID");
47 
48 static struct platform_device *pdev;
49 
50 #define DRVNAME "smsc47b397"
51 
52 /* Super-I/0 registers and commands */
53 
54 #define	REG	0x2e	/* The register to read/write */
55 #define	VAL	0x2f	/* The value to read/write */
56 
57 static inline void superio_outb(int reg, int val)
58 {
59 	outb(reg, REG);
60 	outb(val, VAL);
61 }
62 
63 static inline int superio_inb(int reg)
64 {
65 	outb(reg, REG);
66 	return inb(VAL);
67 }
68 
69 /* select superio logical device */
70 static inline void superio_select(int ld)
71 {
72 	superio_outb(0x07, ld);
73 }
74 
75 static inline void superio_enter(void)
76 {
77 	outb(0x55, REG);
78 }
79 
80 static inline void superio_exit(void)
81 {
82 	outb(0xAA, REG);
83 }
84 
85 #define SUPERIO_REG_DEVID	0x20
86 #define SUPERIO_REG_DEVREV	0x21
87 #define SUPERIO_REG_BASE_MSB	0x60
88 #define SUPERIO_REG_BASE_LSB	0x61
89 #define SUPERIO_REG_LD8		0x08
90 
91 #define SMSC_EXTENT		0x02
92 
93 /* 0 <= nr <= 3 */
94 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
95 #define SMSC47B397_REG_TEMP(nr)	(smsc47b397_reg_temp[(nr)])
96 
97 /* 0 <= nr <= 3 */
98 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
99 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
100 
101 struct smsc47b397_data {
102 	unsigned short addr;
103 	struct mutex lock;
104 
105 	struct mutex update_lock;
106 	unsigned long last_updated; /* in jiffies */
107 	int valid;
108 
109 	/* register values */
110 	u16 fan[4];
111 	u8 temp[4];
112 };
113 
114 static int smsc47b397_read_value(struct smsc47b397_data *data, u8 reg)
115 {
116 	int res;
117 
118 	mutex_lock(&data->lock);
119 	outb(reg, data->addr);
120 	res = inb_p(data->addr + 1);
121 	mutex_unlock(&data->lock);
122 	return res;
123 }
124 
125 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
126 {
127 	struct smsc47b397_data *data = dev_get_drvdata(dev);
128 	int i;
129 
130 	mutex_lock(&data->update_lock);
131 
132 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
133 		dev_dbg(dev, "starting device update...\n");
134 
135 		/* 4 temperature inputs, 4 fan inputs */
136 		for (i = 0; i < 4; i++) {
137 			data->temp[i] = smsc47b397_read_value(data,
138 					SMSC47B397_REG_TEMP(i));
139 
140 			/* must read LSB first */
141 			data->fan[i]  = smsc47b397_read_value(data,
142 					SMSC47B397_REG_FAN_LSB(i));
143 			data->fan[i] |= smsc47b397_read_value(data,
144 					SMSC47B397_REG_FAN_MSB(i)) << 8;
145 		}
146 
147 		data->last_updated = jiffies;
148 		data->valid = 1;
149 
150 		dev_dbg(dev, "... device update complete\n");
151 	}
152 
153 	mutex_unlock(&data->update_lock);
154 
155 	return data;
156 }
157 
158 /*
159  * TEMP: 0.001C/bit (-128C to +127C)
160  * REG: 1C/bit, two's complement
161  */
162 static int temp_from_reg(u8 reg)
163 {
164 	return (s8)reg * 1000;
165 }
166 
167 static ssize_t show_temp(struct device *dev, struct device_attribute
168 			 *devattr, char *buf)
169 {
170 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
171 	struct smsc47b397_data *data = smsc47b397_update_device(dev);
172 	return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
173 }
174 
175 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
176 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
177 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
178 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
179 
180 /*
181  * FAN: 1 RPM/bit
182  * REG: count of 90kHz pulses / revolution
183  */
184 static int fan_from_reg(u16 reg)
185 {
186 	if (reg == 0 || reg == 0xffff)
187 		return 0;
188 	return 90000 * 60 / reg;
189 }
190 
191 static ssize_t show_fan(struct device *dev, struct device_attribute
192 			*devattr, char *buf)
193 {
194 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
195 	struct smsc47b397_data *data = smsc47b397_update_device(dev);
196 	return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
197 }
198 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
199 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
200 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
201 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
202 
203 static struct attribute *smsc47b397_attrs[] = {
204 	&sensor_dev_attr_temp1_input.dev_attr.attr,
205 	&sensor_dev_attr_temp2_input.dev_attr.attr,
206 	&sensor_dev_attr_temp3_input.dev_attr.attr,
207 	&sensor_dev_attr_temp4_input.dev_attr.attr,
208 	&sensor_dev_attr_fan1_input.dev_attr.attr,
209 	&sensor_dev_attr_fan2_input.dev_attr.attr,
210 	&sensor_dev_attr_fan3_input.dev_attr.attr,
211 	&sensor_dev_attr_fan4_input.dev_attr.attr,
212 
213 	NULL
214 };
215 
216 ATTRIBUTE_GROUPS(smsc47b397);
217 
218 static int smsc47b397_probe(struct platform_device *pdev);
219 
220 static struct platform_driver smsc47b397_driver = {
221 	.driver = {
222 		.owner	= THIS_MODULE,
223 		.name	= DRVNAME,
224 	},
225 	.probe		= smsc47b397_probe,
226 };
227 
228 static int smsc47b397_probe(struct platform_device *pdev)
229 {
230 	struct device *dev = &pdev->dev;
231 	struct smsc47b397_data *data;
232 	struct device *hwmon_dev;
233 	struct resource *res;
234 
235 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
236 	if (!devm_request_region(dev, res->start, SMSC_EXTENT,
237 				 smsc47b397_driver.driver.name)) {
238 		dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
239 			(unsigned long)res->start,
240 			(unsigned long)res->start + SMSC_EXTENT - 1);
241 		return -EBUSY;
242 	}
243 
244 	data = devm_kzalloc(dev, sizeof(struct smsc47b397_data), GFP_KERNEL);
245 	if (!data)
246 		return -ENOMEM;
247 
248 	data->addr = res->start;
249 	mutex_init(&data->lock);
250 	mutex_init(&data->update_lock);
251 
252 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, "smsc47b397",
253 							   data,
254 							   smsc47b397_groups);
255 	return PTR_ERR_OR_ZERO(hwmon_dev);
256 }
257 
258 static int __init smsc47b397_device_add(unsigned short address)
259 {
260 	struct resource res = {
261 		.start	= address,
262 		.end	= address + SMSC_EXTENT - 1,
263 		.name	= DRVNAME,
264 		.flags	= IORESOURCE_IO,
265 	};
266 	int err;
267 
268 	err = acpi_check_resource_conflict(&res);
269 	if (err)
270 		goto exit;
271 
272 	pdev = platform_device_alloc(DRVNAME, address);
273 	if (!pdev) {
274 		err = -ENOMEM;
275 		pr_err("Device allocation failed\n");
276 		goto exit;
277 	}
278 
279 	err = platform_device_add_resources(pdev, &res, 1);
280 	if (err) {
281 		pr_err("Device resource addition failed (%d)\n", err);
282 		goto exit_device_put;
283 	}
284 
285 	err = platform_device_add(pdev);
286 	if (err) {
287 		pr_err("Device addition failed (%d)\n", err);
288 		goto exit_device_put;
289 	}
290 
291 	return 0;
292 
293 exit_device_put:
294 	platform_device_put(pdev);
295 exit:
296 	return err;
297 }
298 
299 static int __init smsc47b397_find(void)
300 {
301 	u8 id, rev;
302 	char *name;
303 	unsigned short addr;
304 
305 	superio_enter();
306 	id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
307 
308 	switch (id) {
309 	case 0x81:
310 		name = "SCH5307-NS";
311 		break;
312 	case 0x6f:
313 		name = "LPC47B397-NC";
314 		break;
315 	case 0x85:
316 	case 0x8c:
317 		name = "SCH5317";
318 		break;
319 	default:
320 		superio_exit();
321 		return -ENODEV;
322 	}
323 
324 	rev = superio_inb(SUPERIO_REG_DEVREV);
325 
326 	superio_select(SUPERIO_REG_LD8);
327 	addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
328 		 |  superio_inb(SUPERIO_REG_BASE_LSB);
329 
330 	pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
331 		name, addr, rev);
332 
333 	superio_exit();
334 	return addr;
335 }
336 
337 static int __init smsc47b397_init(void)
338 {
339 	unsigned short address;
340 	int ret;
341 
342 	ret = smsc47b397_find();
343 	if (ret < 0)
344 		return ret;
345 	address = ret;
346 
347 	ret = platform_driver_register(&smsc47b397_driver);
348 	if (ret)
349 		goto exit;
350 
351 	/* Sets global pdev as a side effect */
352 	ret = smsc47b397_device_add(address);
353 	if (ret)
354 		goto exit_driver;
355 
356 	return 0;
357 
358 exit_driver:
359 	platform_driver_unregister(&smsc47b397_driver);
360 exit:
361 	return ret;
362 }
363 
364 static void __exit smsc47b397_exit(void)
365 {
366 	platform_device_unregister(pdev);
367 	platform_driver_unregister(&smsc47b397_driver);
368 }
369 
370 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
371 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
372 MODULE_LICENSE("GPL");
373 
374 module_init(smsc47b397_init);
375 module_exit(smsc47b397_exit);
376