Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / hwmon / smsc47b397.c
blobf928b8d4ff4892a761a526737148561f8a5dcfdf
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * smsc47b397.c - Part of lm_sensors, Linux kernel modules
4 * for hardware monitoring
6 * Supports the SMSC LPC47B397-NC Super-I/O chip.
8 * Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
9 * Copyright (C) 2004 Utilitek Systems, Inc.
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>
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
31 static unsigned short force_id;
32 module_param(force_id, ushort, 0);
33 MODULE_PARM_DESC(force_id, "Override the detected device ID");
35 static struct platform_device *pdev;
37 #define DRVNAME "smsc47b397"
39 /* Super-I/0 registers and commands */
41 #define REG 0x2e /* The register to read/write */
42 #define VAL 0x2f /* The value to read/write */
44 static inline void superio_outb(int reg, int val)
46 outb(reg, REG);
47 outb(val, VAL);
50 static inline int superio_inb(int reg)
52 outb(reg, REG);
53 return inb(VAL);
56 /* select superio logical device */
57 static inline void superio_select(int ld)
59 superio_outb(0x07, ld);
62 static inline int superio_enter(void)
64 if (!request_muxed_region(REG, 2, DRVNAME))
65 return -EBUSY;
67 outb(0x55, REG);
68 return 0;
71 static inline void superio_exit(void)
73 outb(0xAA, REG);
74 release_region(REG, 2);
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
83 #define SMSC_EXTENT 0x02
85 /* 0 <= nr <= 3 */
86 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
87 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
89 /* 0 <= nr <= 3 */
90 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
91 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
93 struct smsc47b397_data {
94 unsigned short addr;
95 struct mutex lock;
97 struct mutex update_lock;
98 unsigned long last_updated; /* in jiffies */
99 int valid;
101 /* register values */
102 u16 fan[4];
103 u8 temp[4];
106 static int smsc47b397_read_value(struct smsc47b397_data *data, u8 reg)
108 int res;
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;
117 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
119 struct smsc47b397_data *data = dev_get_drvdata(dev);
120 int i;
122 mutex_lock(&data->update_lock);
124 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
125 dev_dbg(dev, "starting device update...\n");
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));
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;
139 data->last_updated = jiffies;
140 data->valid = 1;
142 dev_dbg(dev, "... device update complete\n");
145 mutex_unlock(&data->update_lock);
147 return data;
151 * TEMP: 0.001C/bit (-128C to +127C)
152 * REG: 1C/bit, two's complement
154 static int temp_from_reg(u8 reg)
156 return (s8)reg * 1000;
159 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
160 char *buf)
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]));
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);
173 * FAN: 1 RPM/bit
174 * REG: count of 90kHz pulses / revolution
176 static int fan_from_reg(u16 reg)
178 if (reg == 0 || reg == 0xffff)
179 return 0;
180 return 90000 * 60 / reg;
183 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
184 char *buf)
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]));
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);
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,
205 NULL
208 ATTRIBUTE_GROUPS(smsc47b397);
210 static int smsc47b397_probe(struct platform_device *pdev);
212 static struct platform_driver smsc47b397_driver = {
213 .driver = {
214 .name = DRVNAME,
216 .probe = smsc47b397_probe,
219 static int smsc47b397_probe(struct platform_device *pdev)
221 struct device *dev = &pdev->dev;
222 struct smsc47b397_data *data;
223 struct device *hwmon_dev;
224 struct resource *res;
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;
235 data = devm_kzalloc(dev, sizeof(struct smsc47b397_data), GFP_KERNEL);
236 if (!data)
237 return -ENOMEM;
239 data->addr = res->start;
240 mutex_init(&data->lock);
241 mutex_init(&data->update_lock);
243 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "smsc47b397",
244 data,
245 smsc47b397_groups);
246 return PTR_ERR_OR_ZERO(hwmon_dev);
249 static int __init smsc47b397_device_add(unsigned short address)
251 struct resource res = {
252 .start = address,
253 .end = address + SMSC_EXTENT - 1,
254 .name = DRVNAME,
255 .flags = IORESOURCE_IO,
257 int err;
259 err = acpi_check_resource_conflict(&res);
260 if (err)
261 goto exit;
263 pdev = platform_device_alloc(DRVNAME, address);
264 if (!pdev) {
265 err = -ENOMEM;
266 pr_err("Device allocation failed\n");
267 goto exit;
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;
276 err = platform_device_add(pdev);
277 if (err) {
278 pr_err("Device addition failed (%d)\n", err);
279 goto exit_device_put;
282 return 0;
284 exit_device_put:
285 platform_device_put(pdev);
286 exit:
287 return err;
290 static int __init smsc47b397_find(void)
292 u8 id, rev;
293 char *name;
294 unsigned short addr;
295 int err;
297 err = superio_enter();
298 if (err)
299 return err;
301 id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
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;
319 rev = superio_inb(SUPERIO_REG_DEVREV);
321 superio_select(SUPERIO_REG_LD8);
322 addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
323 | superio_inb(SUPERIO_REG_BASE_LSB);
325 pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
326 name, addr, rev);
328 superio_exit();
329 return addr;
332 static int __init smsc47b397_init(void)
334 unsigned short address;
335 int ret;
337 ret = smsc47b397_find();
338 if (ret < 0)
339 return ret;
340 address = ret;
342 ret = platform_driver_register(&smsc47b397_driver);
343 if (ret)
344 goto exit;
346 /* Sets global pdev as a side effect */
347 ret = smsc47b397_device_add(address);
348 if (ret)
349 goto exit_driver;
351 return 0;
353 exit_driver:
354 platform_driver_unregister(&smsc47b397_driver);
355 exit:
356 return ret;
359 static void __exit smsc47b397_exit(void)
361 platform_device_unregister(pdev);
362 platform_driver_unregister(&smsc47b397_driver);
365 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
366 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
367 MODULE_LICENSE("GPL");
369 module_init(smsc47b397_init);
370 module_exit(smsc47b397_exit);