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 <khali@linux-fr.org>
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>
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)
63 static inline int superio_inb(int reg)
69 /* select superio logical device */
70 static inline void superio_select(int ld)
72 superio_outb(0x07, ld);
75 static inline void superio_enter(void)
80 static inline void superio_exit(void)
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
91 #define SMSC_EXTENT 0x02
94 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
95 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
98 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
99 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
101 struct smsc47b397_data {
104 struct device *hwmon_dev;
107 struct mutex update_lock;
108 unsigned long last_updated; /* in jiffies */
111 /* register values */
116 static int smsc47b397_read_value(struct smsc47b397_data *data, u8 reg)
120 mutex_lock(&data->lock);
121 outb(reg, data->addr);
122 res = inb_p(data->addr + 1);
123 mutex_unlock(&data->lock);
127 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
129 struct smsc47b397_data *data = dev_get_drvdata(dev);
132 mutex_lock(&data->update_lock);
134 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
135 dev_dbg(dev, "starting device update...\n");
137 /* 4 temperature inputs, 4 fan inputs */
138 for (i = 0; i < 4; i++) {
139 data->temp[i] = smsc47b397_read_value(data,
140 SMSC47B397_REG_TEMP(i));
142 /* must read LSB first */
143 data->fan[i] = smsc47b397_read_value(data,
144 SMSC47B397_REG_FAN_LSB(i));
145 data->fan[i] |= smsc47b397_read_value(data,
146 SMSC47B397_REG_FAN_MSB(i)) << 8;
149 data->last_updated = jiffies;
152 dev_dbg(dev, "... device update complete\n");
155 mutex_unlock(&data->update_lock);
161 * TEMP: 0.001C/bit (-128C to +127C)
162 * REG: 1C/bit, two's complement
164 static int temp_from_reg(u8 reg)
166 return (s8)reg * 1000;
169 static ssize_t show_temp(struct device *dev, struct device_attribute
172 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
173 struct smsc47b397_data *data = smsc47b397_update_device(dev);
174 return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
177 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
178 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
179 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
180 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
184 * REG: count of 90kHz pulses / revolution
186 static int fan_from_reg(u16 reg)
188 if (reg == 0 || reg == 0xffff)
190 return 90000 * 60 / reg;
193 static ssize_t show_fan(struct device *dev, struct device_attribute
196 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
197 struct smsc47b397_data *data = smsc47b397_update_device(dev);
198 return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
200 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
201 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
202 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
203 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
205 static ssize_t show_name(struct device *dev, struct device_attribute
208 struct smsc47b397_data *data = dev_get_drvdata(dev);
209 return sprintf(buf, "%s\n", data->name);
211 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
213 static struct attribute *smsc47b397_attributes[] = {
214 &sensor_dev_attr_temp1_input.dev_attr.attr,
215 &sensor_dev_attr_temp2_input.dev_attr.attr,
216 &sensor_dev_attr_temp3_input.dev_attr.attr,
217 &sensor_dev_attr_temp4_input.dev_attr.attr,
218 &sensor_dev_attr_fan1_input.dev_attr.attr,
219 &sensor_dev_attr_fan2_input.dev_attr.attr,
220 &sensor_dev_attr_fan3_input.dev_attr.attr,
221 &sensor_dev_attr_fan4_input.dev_attr.attr,
227 static const struct attribute_group smsc47b397_group = {
228 .attrs = smsc47b397_attributes,
231 static int smsc47b397_remove(struct platform_device *pdev)
233 struct smsc47b397_data *data = platform_get_drvdata(pdev);
235 hwmon_device_unregister(data->hwmon_dev);
236 sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
241 static int smsc47b397_probe(struct platform_device *pdev);
243 static struct platform_driver smsc47b397_driver = {
245 .owner = THIS_MODULE,
248 .probe = smsc47b397_probe,
249 .remove = smsc47b397_remove,
252 static int smsc47b397_probe(struct platform_device *pdev)
254 struct device *dev = &pdev->dev;
255 struct smsc47b397_data *data;
256 struct resource *res;
259 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
260 if (!devm_request_region(dev, res->start, SMSC_EXTENT,
261 smsc47b397_driver.driver.name)) {
262 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
263 (unsigned long)res->start,
264 (unsigned long)res->start + SMSC_EXTENT - 1);
268 data = devm_kzalloc(dev, sizeof(struct smsc47b397_data), GFP_KERNEL);
272 data->addr = res->start;
273 data->name = "smsc47b397";
274 mutex_init(&data->lock);
275 mutex_init(&data->update_lock);
276 platform_set_drvdata(pdev, data);
278 err = sysfs_create_group(&dev->kobj, &smsc47b397_group);
282 data->hwmon_dev = hwmon_device_register(dev);
283 if (IS_ERR(data->hwmon_dev)) {
284 err = PTR_ERR(data->hwmon_dev);
291 sysfs_remove_group(&dev->kobj, &smsc47b397_group);
295 static int __init smsc47b397_device_add(unsigned short address)
297 struct resource res = {
299 .end = address + SMSC_EXTENT - 1,
301 .flags = IORESOURCE_IO,
305 err = acpi_check_resource_conflict(&res);
309 pdev = platform_device_alloc(DRVNAME, address);
312 pr_err("Device allocation failed\n");
316 err = platform_device_add_resources(pdev, &res, 1);
318 pr_err("Device resource addition failed (%d)\n", err);
319 goto exit_device_put;
322 err = platform_device_add(pdev);
324 pr_err("Device addition failed (%d)\n", err);
325 goto exit_device_put;
331 platform_device_put(pdev);
336 static int __init smsc47b397_find(void)
343 id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
350 name = "LPC47B397-NC";
361 rev = superio_inb(SUPERIO_REG_DEVREV);
363 superio_select(SUPERIO_REG_LD8);
364 addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
365 | superio_inb(SUPERIO_REG_BASE_LSB);
367 pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
374 static int __init smsc47b397_init(void)
376 unsigned short address;
379 ret = smsc47b397_find();
384 ret = platform_driver_register(&smsc47b397_driver);
388 /* Sets global pdev as a side effect */
389 ret = smsc47b397_device_add(address);
396 platform_driver_unregister(&smsc47b397_driver);
401 static void __exit smsc47b397_exit(void)
403 platform_device_unregister(pdev);
404 platform_driver_unregister(&smsc47b397_driver);
407 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
408 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
409 MODULE_LICENSE("GPL");
411 module_init(smsc47b397_init);
412 module_exit(smsc47b397_exit);