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 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/ioport.h>
32 #include <linux/jiffies.h>
33 #include <linux/platform_device.h>
34 #include <linux/hwmon.h>
35 #include <linux/err.h>
36 #include <linux/init.h>
37 #include <linux/mutex.h>
40 static struct platform_device *pdev;
42 #define DRVNAME "smsc47b397"
44 /* Super-I/0 registers and commands */
46 #define REG 0x2e /* The register to read/write */
47 #define VAL 0x2f /* The value to read/write */
49 static inline void superio_outb(int reg, int val)
55 static inline int superio_inb(int reg)
61 /* select superio logical device */
62 static inline void superio_select(int ld)
64 superio_outb(0x07, ld);
67 static inline void superio_enter(void)
72 static inline void superio_exit(void)
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
86 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
87 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
90 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
91 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
93 struct smsc47b397_data {
96 struct class_device *class_dev;
99 struct mutex update_lock;
100 unsigned long last_updated; /* in jiffies */
103 /* register values */
108 static int smsc47b397_read_value(struct smsc47b397_data* data, u8 reg)
112 mutex_lock(&data->lock);
113 outb(reg, data->addr);
114 res = inb_p(data->addr + 1);
115 mutex_unlock(&data->lock);
119 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
121 struct smsc47b397_data *data = dev_get_drvdata(dev);
124 mutex_lock(&data->update_lock);
126 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
127 dev_dbg(dev, "starting device update...\n");
129 /* 4 temperature inputs, 4 fan inputs */
130 for (i = 0; i < 4; i++) {
131 data->temp[i] = smsc47b397_read_value(data,
132 SMSC47B397_REG_TEMP(i));
134 /* must read LSB first */
135 data->fan[i] = smsc47b397_read_value(data,
136 SMSC47B397_REG_FAN_LSB(i));
137 data->fan[i] |= smsc47b397_read_value(data,
138 SMSC47B397_REG_FAN_MSB(i)) << 8;
141 data->last_updated = jiffies;
144 dev_dbg(dev, "... device update complete\n");
147 mutex_unlock(&data->update_lock);
152 /* TEMP: 0.001C/bit (-128C to +127C)
153 REG: 1C/bit, two's complement */
154 static int temp_from_reg(u8 reg)
156 return (s8)reg * 1000;
160 static ssize_t show_temp(struct device *dev, char *buf, int nr)
162 struct smsc47b397_data *data = smsc47b397_update_device(dev);
163 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
166 #define sysfs_temp(num) \
167 static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
169 return show_temp(dev, buf, num-1); \
171 static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
179 REG: count of 90kHz pulses / revolution */
180 static int fan_from_reg(u16 reg)
182 return 90000 * 60 / reg;
186 static ssize_t show_fan(struct device *dev, char *buf, int nr)
188 struct smsc47b397_data *data = smsc47b397_update_device(dev);
189 return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
192 #define sysfs_fan(num) \
193 static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
195 return show_fan(dev, buf, num-1); \
197 static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
204 static ssize_t show_name(struct device *dev, struct device_attribute
207 struct smsc47b397_data *data = dev_get_drvdata(dev);
208 return sprintf(buf, "%s\n", data->name);
210 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
212 static struct attribute *smsc47b397_attributes[] = {
213 &dev_attr_temp1_input.attr,
214 &dev_attr_temp2_input.attr,
215 &dev_attr_temp3_input.attr,
216 &dev_attr_temp4_input.attr,
217 &dev_attr_fan1_input.attr,
218 &dev_attr_fan2_input.attr,
219 &dev_attr_fan3_input.attr,
220 &dev_attr_fan4_input.attr,
226 static const struct attribute_group smsc47b397_group = {
227 .attrs = smsc47b397_attributes,
230 static int __devexit smsc47b397_remove(struct platform_device *pdev)
232 struct smsc47b397_data *data = platform_get_drvdata(pdev);
233 struct resource *res;
235 hwmon_device_unregister(data->class_dev);
236 sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
237 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
238 release_region(res->start, SMSC_EXTENT);
244 static int smsc47b397_probe(struct platform_device *pdev);
246 static struct platform_driver smsc47b397_driver = {
248 .owner = THIS_MODULE,
251 .probe = smsc47b397_probe,
252 .remove = __devexit_p(smsc47b397_remove),
255 static int __devinit smsc47b397_probe(struct platform_device *pdev)
257 struct device *dev = &pdev->dev;
258 struct smsc47b397_data *data;
259 struct resource *res;
262 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
263 if (!request_region(res->start, SMSC_EXTENT,
264 smsc47b397_driver.driver.name)) {
265 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
266 (unsigned long)res->start,
267 (unsigned long)res->start + SMSC_EXTENT - 1);
271 if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
276 data->addr = res->start;
277 data->name = "smsc47b397";
278 mutex_init(&data->lock);
279 mutex_init(&data->update_lock);
280 platform_set_drvdata(pdev, data);
282 if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group)))
285 data->class_dev = hwmon_device_register(dev);
286 if (IS_ERR(data->class_dev)) {
287 err = PTR_ERR(data->class_dev);
294 sysfs_remove_group(&dev->kobj, &smsc47b397_group);
298 release_region(res->start, SMSC_EXTENT);
302 static int __init smsc47b397_device_add(unsigned short address)
304 struct resource res = {
306 .end = address + SMSC_EXTENT - 1,
308 .flags = IORESOURCE_IO,
312 pdev = platform_device_alloc(DRVNAME, address);
315 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
319 err = platform_device_add_resources(pdev, &res, 1);
321 printk(KERN_ERR DRVNAME ": Device resource addition failed "
323 goto exit_device_put;
326 err = platform_device_add(pdev);
328 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
330 goto exit_device_put;
336 platform_device_put(pdev);
341 static int __init smsc47b397_find(unsigned short *addr)
346 id = superio_inb(SUPERIO_REG_DEVID);
348 if ((id != 0x6f) && (id != 0x81)) {
353 rev = superio_inb(SUPERIO_REG_DEVREV);
355 superio_select(SUPERIO_REG_LD8);
356 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
357 | superio_inb(SUPERIO_REG_BASE_LSB);
359 printk(KERN_INFO DRVNAME ": found SMSC %s "
360 "(base address 0x%04x, revision %u)\n",
361 id == 0x81 ? "SCH5307-NS" : "LPC47B397-NC", *addr, rev);
367 static int __init smsc47b397_init(void)
369 unsigned short address;
372 if ((ret = smsc47b397_find(&address)))
375 ret = platform_driver_register(&smsc47b397_driver);
379 /* Sets global pdev as a side effect */
380 ret = smsc47b397_device_add(address);
387 platform_driver_unregister(&smsc47b397_driver);
392 static void __exit smsc47b397_exit(void)
394 platform_device_unregister(pdev);
395 platform_driver_unregister(&smsc47b397_driver);
398 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
399 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
400 MODULE_LICENSE("GPL");
402 module_init(smsc47b397_init);
403 module_exit(smsc47b397_exit);