Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / smsc47b397.c
blobcbdb5c4991ae3132538d36d54fb40aa2943bca1a
1 /*
2 * smsc47b397.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
5 * Supports the SMSC LPC47B397-NC Super-I/O chip.
7 * Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
8 * Copyright (C) 2004 Utilitek Systems, Inc.
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>
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.
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.
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.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
44 static unsigned short force_id;
45 module_param(force_id, ushort, 0);
46 MODULE_PARM_DESC(force_id, "Override the detected device ID");
48 static struct platform_device *pdev;
50 #define DRVNAME "smsc47b397"
52 /* Super-I/0 registers and commands */
54 #define REG 0x2e /* The register to read/write */
55 #define VAL 0x2f /* The value to read/write */
57 static inline void superio_outb(int reg, int val)
59 outb(reg, REG);
60 outb(val, VAL);
63 static inline int superio_inb(int reg)
65 outb(reg, REG);
66 return inb(VAL);
69 /* select superio logical device */
70 static inline void superio_select(int ld)
72 superio_outb(0x07, ld);
75 static inline int superio_enter(void)
77 if (!request_muxed_region(REG, 2, DRVNAME))
78 return -EBUSY;
80 outb(0x55, REG);
81 return 0;
84 static inline void superio_exit(void)
86 outb(0xAA, REG);
87 release_region(REG, 2);
90 #define SUPERIO_REG_DEVID 0x20
91 #define SUPERIO_REG_DEVREV 0x21
92 #define SUPERIO_REG_BASE_MSB 0x60
93 #define SUPERIO_REG_BASE_LSB 0x61
94 #define SUPERIO_REG_LD8 0x08
96 #define SMSC_EXTENT 0x02
98 /* 0 <= nr <= 3 */
99 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
100 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
102 /* 0 <= nr <= 3 */
103 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
104 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
106 struct smsc47b397_data {
107 unsigned short addr;
108 struct mutex lock;
110 struct mutex update_lock;
111 unsigned long last_updated; /* in jiffies */
112 int valid;
114 /* register values */
115 u16 fan[4];
116 u8 temp[4];
119 static int smsc47b397_read_value(struct smsc47b397_data *data, u8 reg)
121 int res;
123 mutex_lock(&data->lock);
124 outb(reg, data->addr);
125 res = inb_p(data->addr + 1);
126 mutex_unlock(&data->lock);
127 return res;
130 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
132 struct smsc47b397_data *data = dev_get_drvdata(dev);
133 int i;
135 mutex_lock(&data->update_lock);
137 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
138 dev_dbg(dev, "starting device update...\n");
140 /* 4 temperature inputs, 4 fan inputs */
141 for (i = 0; i < 4; i++) {
142 data->temp[i] = smsc47b397_read_value(data,
143 SMSC47B397_REG_TEMP(i));
145 /* must read LSB first */
146 data->fan[i] = smsc47b397_read_value(data,
147 SMSC47B397_REG_FAN_LSB(i));
148 data->fan[i] |= smsc47b397_read_value(data,
149 SMSC47B397_REG_FAN_MSB(i)) << 8;
152 data->last_updated = jiffies;
153 data->valid = 1;
155 dev_dbg(dev, "... device update complete\n");
158 mutex_unlock(&data->update_lock);
160 return data;
164 * TEMP: 0.001C/bit (-128C to +127C)
165 * REG: 1C/bit, two's complement
167 static int temp_from_reg(u8 reg)
169 return (s8)reg * 1000;
172 static ssize_t show_temp(struct device *dev, struct device_attribute
173 *devattr, char *buf)
175 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
176 struct smsc47b397_data *data = smsc47b397_update_device(dev);
177 return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
180 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
181 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
182 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
183 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
186 * FAN: 1 RPM/bit
187 * REG: count of 90kHz pulses / revolution
189 static int fan_from_reg(u16 reg)
191 if (reg == 0 || reg == 0xffff)
192 return 0;
193 return 90000 * 60 / reg;
196 static ssize_t show_fan(struct device *dev, struct device_attribute
197 *devattr, char *buf)
199 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
200 struct smsc47b397_data *data = smsc47b397_update_device(dev);
201 return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
203 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
204 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
205 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
206 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
208 static struct attribute *smsc47b397_attrs[] = {
209 &sensor_dev_attr_temp1_input.dev_attr.attr,
210 &sensor_dev_attr_temp2_input.dev_attr.attr,
211 &sensor_dev_attr_temp3_input.dev_attr.attr,
212 &sensor_dev_attr_temp4_input.dev_attr.attr,
213 &sensor_dev_attr_fan1_input.dev_attr.attr,
214 &sensor_dev_attr_fan2_input.dev_attr.attr,
215 &sensor_dev_attr_fan3_input.dev_attr.attr,
216 &sensor_dev_attr_fan4_input.dev_attr.attr,
218 NULL
221 ATTRIBUTE_GROUPS(smsc47b397);
223 static int smsc47b397_probe(struct platform_device *pdev);
225 static struct platform_driver smsc47b397_driver = {
226 .driver = {
227 .name = DRVNAME,
229 .probe = smsc47b397_probe,
232 static int smsc47b397_probe(struct platform_device *pdev)
234 struct device *dev = &pdev->dev;
235 struct smsc47b397_data *data;
236 struct device *hwmon_dev;
237 struct resource *res;
239 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
240 if (!devm_request_region(dev, res->start, SMSC_EXTENT,
241 smsc47b397_driver.driver.name)) {
242 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
243 (unsigned long)res->start,
244 (unsigned long)res->start + SMSC_EXTENT - 1);
245 return -EBUSY;
248 data = devm_kzalloc(dev, sizeof(struct smsc47b397_data), GFP_KERNEL);
249 if (!data)
250 return -ENOMEM;
252 data->addr = res->start;
253 mutex_init(&data->lock);
254 mutex_init(&data->update_lock);
256 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "smsc47b397",
257 data,
258 smsc47b397_groups);
259 return PTR_ERR_OR_ZERO(hwmon_dev);
262 static int __init smsc47b397_device_add(unsigned short address)
264 struct resource res = {
265 .start = address,
266 .end = address + SMSC_EXTENT - 1,
267 .name = DRVNAME,
268 .flags = IORESOURCE_IO,
270 int err;
272 err = acpi_check_resource_conflict(&res);
273 if (err)
274 goto exit;
276 pdev = platform_device_alloc(DRVNAME, address);
277 if (!pdev) {
278 err = -ENOMEM;
279 pr_err("Device allocation failed\n");
280 goto exit;
283 err = platform_device_add_resources(pdev, &res, 1);
284 if (err) {
285 pr_err("Device resource addition failed (%d)\n", err);
286 goto exit_device_put;
289 err = platform_device_add(pdev);
290 if (err) {
291 pr_err("Device addition failed (%d)\n", err);
292 goto exit_device_put;
295 return 0;
297 exit_device_put:
298 platform_device_put(pdev);
299 exit:
300 return err;
303 static int __init smsc47b397_find(void)
305 u8 id, rev;
306 char *name;
307 unsigned short addr;
308 int err;
310 err = superio_enter();
311 if (err)
312 return err;
314 id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
316 switch (id) {
317 case 0x81:
318 name = "SCH5307-NS";
319 break;
320 case 0x6f:
321 name = "LPC47B397-NC";
322 break;
323 case 0x85:
324 case 0x8c:
325 name = "SCH5317";
326 break;
327 default:
328 superio_exit();
329 return -ENODEV;
332 rev = superio_inb(SUPERIO_REG_DEVREV);
334 superio_select(SUPERIO_REG_LD8);
335 addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
336 | superio_inb(SUPERIO_REG_BASE_LSB);
338 pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
339 name, addr, rev);
341 superio_exit();
342 return addr;
345 static int __init smsc47b397_init(void)
347 unsigned short address;
348 int ret;
350 ret = smsc47b397_find();
351 if (ret < 0)
352 return ret;
353 address = ret;
355 ret = platform_driver_register(&smsc47b397_driver);
356 if (ret)
357 goto exit;
359 /* Sets global pdev as a side effect */
360 ret = smsc47b397_device_add(address);
361 if (ret)
362 goto exit_driver;
364 return 0;
366 exit_driver:
367 platform_driver_unregister(&smsc47b397_driver);
368 exit:
369 return ret;
372 static void __exit smsc47b397_exit(void)
374 platform_device_unregister(pdev);
375 platform_driver_unregister(&smsc47b397_driver);
378 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
379 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
380 MODULE_LICENSE("GPL");
382 module_init(smsc47b397_init);
383 module_exit(smsc47b397_exit);