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