blob: 6f6d52b4fb64520f67581682acb3d63995343805 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 smsc47b397.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Supports the SMSC LPC47B397-NC Super-I/O chip.
6
7 Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
8 Copyright (C) 2004 Utilitek Systems, Inc.
9
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>
13
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.
18
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.
23
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.
27*/
28
29#include <linux/module.h>
30#include <linux/slab.h>
31#include <linux/ioport.h>
32#include <linux/jiffies.h>
Jean Delvare292fc1a2007-05-08 17:22:03 +020033#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040034#include <linux/hwmon.h>
Jean Delvare3659a012007-05-08 17:22:03 +020035#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040036#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010038#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010039#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
41
Jean Delvare67b671b2007-12-06 23:13:42 +010042static unsigned short force_id;
43module_param(force_id, ushort, 0);
44MODULE_PARM_DESC(force_id, "Override the detected device ID");
45
Jean Delvare292fc1a2007-05-08 17:22:03 +020046static struct platform_device *pdev;
47
48#define DRVNAME "smsc47b397"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Super-I/0 registers and commands */
51
52#define REG 0x2e /* The register to read/write */
53#define VAL 0x2f /* The value to read/write */
54
55static inline void superio_outb(int reg, int val)
56{
57 outb(reg, REG);
58 outb(val, VAL);
59}
60
61static inline int superio_inb(int reg)
62{
63 outb(reg, REG);
64 return inb(VAL);
65}
66
67/* select superio logical device */
68static inline void superio_select(int ld)
69{
70 superio_outb(0x07, ld);
71}
72
73static inline void superio_enter(void)
74{
75 outb(0x55, REG);
76}
77
78static inline void superio_exit(void)
79{
80 outb(0xAA, REG);
81}
82
83#define SUPERIO_REG_DEVID 0x20
84#define SUPERIO_REG_DEVREV 0x21
85#define SUPERIO_REG_BASE_MSB 0x60
86#define SUPERIO_REG_BASE_LSB 0x61
87#define SUPERIO_REG_LD8 0x08
88
89#define SMSC_EXTENT 0x02
90
91/* 0 <= nr <= 3 */
92static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
93#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
94
95/* 0 <= nr <= 3 */
96#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
97#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
98
99struct smsc47b397_data {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200100 unsigned short addr;
101 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700102 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100103 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100105 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned long last_updated; /* in jiffies */
107 int valid;
108
109 /* register values */
110 u16 fan[4];
111 u8 temp[4];
112};
113
Jean Delvare292fc1a2007-05-08 17:22:03 +0200114static int smsc47b397_read_value(struct smsc47b397_data* data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int res;
117
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100118 mutex_lock(&data->lock);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200119 outb(reg, data->addr);
120 res = inb_p(data->addr + 1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100121 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return res;
123}
124
125static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
126{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200127 struct smsc47b397_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int i;
129
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100130 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200133 dev_dbg(dev, "starting device update...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /* 4 temperature inputs, 4 fan inputs */
136 for (i = 0; i < 4; i++) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200137 data->temp[i] = smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 SMSC47B397_REG_TEMP(i));
139
140 /* must read LSB first */
Jean Delvare292fc1a2007-05-08 17:22:03 +0200141 data->fan[i] = smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 SMSC47B397_REG_FAN_LSB(i));
Jean Delvare292fc1a2007-05-08 17:22:03 +0200143 data->fan[i] |= smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 SMSC47B397_REG_FAN_MSB(i)) << 8;
145 }
146
147 data->last_updated = jiffies;
148 data->valid = 1;
149
Jean Delvare292fc1a2007-05-08 17:22:03 +0200150 dev_dbg(dev, "... device update complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100153 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return data;
156}
157
158/* TEMP: 0.001C/bit (-128C to +127C)
159 REG: 1C/bit, two's complement */
160static int temp_from_reg(u8 reg)
161{
162 return (s8)reg * 1000;
163}
164
Jean Delvare3659a012007-05-08 17:22:03 +0200165static ssize_t show_temp(struct device *dev, struct device_attribute
166 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jean Delvare3659a012007-05-08 17:22:03 +0200168 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct smsc47b397_data *data = smsc47b397_update_device(dev);
Jean Delvare3659a012007-05-08 17:22:03 +0200170 return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Jean Delvare3659a012007-05-08 17:22:03 +0200173static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
174static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
175static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
176static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* FAN: 1 RPM/bit
179 REG: count of 90kHz pulses / revolution */
180static int fan_from_reg(u16 reg)
181{
Jean Delvare90205c62007-06-23 14:58:22 +0200182 if (reg == 0 || reg == 0xffff)
183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 90000 * 60 / reg;
185}
186
Jean Delvare3659a012007-05-08 17:22:03 +0200187static ssize_t show_fan(struct device *dev, struct device_attribute
188 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Jean Delvare3659a012007-05-08 17:22:03 +0200190 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
191 struct smsc47b397_data *data = smsc47b397_update_device(dev);
192 return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
Jean Delvare3659a012007-05-08 17:22:03 +0200194static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
195static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
196static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
197static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jean Delvare292fc1a2007-05-08 17:22:03 +0200199static ssize_t show_name(struct device *dev, struct device_attribute
200 *devattr, char *buf)
201{
202 struct smsc47b397_data *data = dev_get_drvdata(dev);
203 return sprintf(buf, "%s\n", data->name);
204}
205static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
206
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200207static struct attribute *smsc47b397_attributes[] = {
Jean Delvare3659a012007-05-08 17:22:03 +0200208 &sensor_dev_attr_temp1_input.dev_attr.attr,
209 &sensor_dev_attr_temp2_input.dev_attr.attr,
210 &sensor_dev_attr_temp3_input.dev_attr.attr,
211 &sensor_dev_attr_temp4_input.dev_attr.attr,
212 &sensor_dev_attr_fan1_input.dev_attr.attr,
213 &sensor_dev_attr_fan2_input.dev_attr.attr,
214 &sensor_dev_attr_fan3_input.dev_attr.attr,
215 &sensor_dev_attr_fan4_input.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200216
Jean Delvare292fc1a2007-05-08 17:22:03 +0200217 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200218 NULL
219};
220
221static const struct attribute_group smsc47b397_group = {
222 .attrs = smsc47b397_attributes,
223};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Jean Delvare292fc1a2007-05-08 17:22:03 +0200225static int __devexit smsc47b397_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200227 struct smsc47b397_data *data = platform_get_drvdata(pdev);
228 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Tony Jones1beeffe2007-08-20 13:46:20 -0700230 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200231 sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
232 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
233 release_region(res->start, SMSC_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400234 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 return 0;
237}
238
Jean Delvare292fc1a2007-05-08 17:22:03 +0200239static int smsc47b397_probe(struct platform_device *pdev);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200240
Jean Delvare292fc1a2007-05-08 17:22:03 +0200241static struct platform_driver smsc47b397_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100242 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200243 .owner = THIS_MODULE,
Jean Delvare292fc1a2007-05-08 17:22:03 +0200244 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100245 },
Jean Delvare292fc1a2007-05-08 17:22:03 +0200246 .probe = smsc47b397_probe,
247 .remove = __devexit_p(smsc47b397_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
Jean Delvare292fc1a2007-05-08 17:22:03 +0200250static int __devinit smsc47b397_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200252 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct smsc47b397_data *data;
Jean Delvare292fc1a2007-05-08 17:22:03 +0200254 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int err = 0;
256
Jean Delvare292fc1a2007-05-08 17:22:03 +0200257 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
258 if (!request_region(res->start, SMSC_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100259 smsc47b397_driver.driver.name)) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200260 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
261 (unsigned long)res->start,
262 (unsigned long)res->start + SMSC_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EBUSY;
264 }
265
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200266 if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 err = -ENOMEM;
268 goto error_release;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jean Delvare292fc1a2007-05-08 17:22:03 +0200271 data->addr = res->start;
272 data->name = "smsc47b397";
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100273 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100274 mutex_init(&data->update_lock);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200275 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Jean Delvare292fc1a2007-05-08 17:22:03 +0200277 if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto error_free;
279
Tony Jones1beeffe2007-08-20 13:46:20 -0700280 data->hwmon_dev = hwmon_device_register(dev);
281 if (IS_ERR(data->hwmon_dev)) {
282 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200283 goto error_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400284 }
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200288error_remove:
Jean Delvare292fc1a2007-05-08 17:22:03 +0200289 sysfs_remove_group(&dev->kobj, &smsc47b397_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290error_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400291 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292error_release:
Jean Delvare292fc1a2007-05-08 17:22:03 +0200293 release_region(res->start, SMSC_EXTENT);
294 return err;
295}
296
297static int __init smsc47b397_device_add(unsigned short address)
298{
299 struct resource res = {
300 .start = address,
301 .end = address + SMSC_EXTENT - 1,
302 .name = DRVNAME,
303 .flags = IORESOURCE_IO,
304 };
305 int err;
306
Jean Delvareb9acb642009-01-07 16:37:35 +0100307 err = acpi_check_resource_conflict(&res);
308 if (err)
309 goto exit;
310
Jean Delvare292fc1a2007-05-08 17:22:03 +0200311 pdev = platform_device_alloc(DRVNAME, address);
312 if (!pdev) {
313 err = -ENOMEM;
314 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
315 goto exit;
316 }
317
318 err = platform_device_add_resources(pdev, &res, 1);
319 if (err) {
320 printk(KERN_ERR DRVNAME ": Device resource addition failed "
321 "(%d)\n", err);
322 goto exit_device_put;
323 }
324
325 err = platform_device_add(pdev);
326 if (err) {
327 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
328 err);
329 goto exit_device_put;
330 }
331
332 return 0;
333
334exit_device_put:
335 platform_device_put(pdev);
336exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return err;
338}
339
Jean Delvare2d8672c2005-07-19 23:56:35 +0200340static int __init smsc47b397_find(unsigned short *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 u8 id, rev;
Craig Kelley80930772008-02-29 10:24:44 -0700343 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +0100346 id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Craig Kelley80930772008-02-29 10:24:44 -0700348 switch(id) {
349 case 0x81:
350 name = "SCH5307-NS";
351 break;
352 case 0x6f:
353 name = "LPC47B397-NC";
354 break;
355 case 0x85:
356 case 0x8c:
357 name = "SCH5317";
358 break;
359 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 superio_exit();
361 return -ENODEV;
362 }
363
364 rev = superio_inb(SUPERIO_REG_DEVREV);
365
366 superio_select(SUPERIO_REG_LD8);
367 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
368 | superio_inb(SUPERIO_REG_BASE_LSB);
369
Jean Delvare292fc1a2007-05-08 17:22:03 +0200370 printk(KERN_INFO DRVNAME ": found SMSC %s "
Mark M. Hoffman7ab83a92005-10-17 23:01:45 +0200371 "(base address 0x%04x, revision %u)\n",
Craig Kelley80930772008-02-29 10:24:44 -0700372 name, *addr, rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 superio_exit();
375 return 0;
376}
377
378static int __init smsc47b397_init(void)
379{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200380 unsigned short address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int ret;
382
Jean Delvare2d8672c2005-07-19 23:56:35 +0200383 if ((ret = smsc47b397_find(&address)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return ret;
385
Jean Delvare292fc1a2007-05-08 17:22:03 +0200386 ret = platform_driver_register(&smsc47b397_driver);
387 if (ret)
388 goto exit;
389
390 /* Sets global pdev as a side effect */
391 ret = smsc47b397_device_add(address);
392 if (ret)
393 goto exit_driver;
394
395 return 0;
396
397exit_driver:
398 platform_driver_unregister(&smsc47b397_driver);
399exit:
400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403static void __exit smsc47b397_exit(void)
404{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200405 platform_device_unregister(pdev);
406 platform_driver_unregister(&smsc47b397_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
410MODULE_DESCRIPTION("SMSC LPC47B397 driver");
411MODULE_LICENSE("GPL");
412
413module_init(smsc47b397_init);
414module_exit(smsc47b397_exit);