blob: f61d8f4185b243efcf7bd3fb0f10fa6284dde6d2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/io.h>
40
Jean Delvare67b671b2007-12-06 23:13:42 +010041static unsigned short force_id;
42module_param(force_id, ushort, 0);
43MODULE_PARM_DESC(force_id, "Override the detected device ID");
44
Jean Delvare292fc1a2007-05-08 17:22:03 +020045static struct platform_device *pdev;
46
47#define DRVNAME "smsc47b397"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Super-I/0 registers and commands */
50
51#define REG 0x2e /* The register to read/write */
52#define VAL 0x2f /* The value to read/write */
53
54static inline void superio_outb(int reg, int val)
55{
56 outb(reg, REG);
57 outb(val, VAL);
58}
59
60static inline int superio_inb(int reg)
61{
62 outb(reg, REG);
63 return inb(VAL);
64}
65
66/* select superio logical device */
67static inline void superio_select(int ld)
68{
69 superio_outb(0x07, ld);
70}
71
72static inline void superio_enter(void)
73{
74 outb(0x55, REG);
75}
76
77static inline void superio_exit(void)
78{
79 outb(0xAA, REG);
80}
81
82#define SUPERIO_REG_DEVID 0x20
83#define SUPERIO_REG_DEVREV 0x21
84#define SUPERIO_REG_BASE_MSB 0x60
85#define SUPERIO_REG_BASE_LSB 0x61
86#define SUPERIO_REG_LD8 0x08
87
88#define SMSC_EXTENT 0x02
89
90/* 0 <= nr <= 3 */
91static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
92#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
93
94/* 0 <= nr <= 3 */
95#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
96#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
97
98struct smsc47b397_data {
Jean Delvare292fc1a2007-05-08 17:22:03 +020099 unsigned short addr;
100 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700101 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100102 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100104 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned long last_updated; /* in jiffies */
106 int valid;
107
108 /* register values */
109 u16 fan[4];
110 u8 temp[4];
111};
112
Jean Delvare292fc1a2007-05-08 17:22:03 +0200113static int smsc47b397_read_value(struct smsc47b397_data* data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int res;
116
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100117 mutex_lock(&data->lock);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200118 outb(reg, data->addr);
119 res = inb_p(data->addr + 1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100120 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return res;
122}
123
124static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
125{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200126 struct smsc47b397_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int i;
128
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100129 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200132 dev_dbg(dev, "starting device update...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /* 4 temperature inputs, 4 fan inputs */
135 for (i = 0; i < 4; i++) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200136 data->temp[i] = smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 SMSC47B397_REG_TEMP(i));
138
139 /* must read LSB first */
Jean Delvare292fc1a2007-05-08 17:22:03 +0200140 data->fan[i] = smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 SMSC47B397_REG_FAN_LSB(i));
Jean Delvare292fc1a2007-05-08 17:22:03 +0200142 data->fan[i] |= smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 SMSC47B397_REG_FAN_MSB(i)) << 8;
144 }
145
146 data->last_updated = jiffies;
147 data->valid = 1;
148
Jean Delvare292fc1a2007-05-08 17:22:03 +0200149 dev_dbg(dev, "... device update complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100152 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return data;
155}
156
157/* TEMP: 0.001C/bit (-128C to +127C)
158 REG: 1C/bit, two's complement */
159static int temp_from_reg(u8 reg)
160{
161 return (s8)reg * 1000;
162}
163
Jean Delvare3659a012007-05-08 17:22:03 +0200164static ssize_t show_temp(struct device *dev, struct device_attribute
165 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Jean Delvare3659a012007-05-08 17:22:03 +0200167 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct smsc47b397_data *data = smsc47b397_update_device(dev);
Jean Delvare3659a012007-05-08 17:22:03 +0200169 return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Jean Delvare3659a012007-05-08 17:22:03 +0200172static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
173static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
174static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
175static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/* FAN: 1 RPM/bit
178 REG: count of 90kHz pulses / revolution */
179static int fan_from_reg(u16 reg)
180{
Jean Delvare90205c62007-06-23 14:58:22 +0200181 if (reg == 0 || reg == 0xffff)
182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return 90000 * 60 / reg;
184}
185
Jean Delvare3659a012007-05-08 17:22:03 +0200186static ssize_t show_fan(struct device *dev, struct device_attribute
187 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Jean Delvare3659a012007-05-08 17:22:03 +0200189 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
190 struct smsc47b397_data *data = smsc47b397_update_device(dev);
191 return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Jean Delvare3659a012007-05-08 17:22:03 +0200193static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
194static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
195static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
196static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jean Delvare292fc1a2007-05-08 17:22:03 +0200198static ssize_t show_name(struct device *dev, struct device_attribute
199 *devattr, char *buf)
200{
201 struct smsc47b397_data *data = dev_get_drvdata(dev);
202 return sprintf(buf, "%s\n", data->name);
203}
204static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
205
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200206static struct attribute *smsc47b397_attributes[] = {
Jean Delvare3659a012007-05-08 17:22:03 +0200207 &sensor_dev_attr_temp1_input.dev_attr.attr,
208 &sensor_dev_attr_temp2_input.dev_attr.attr,
209 &sensor_dev_attr_temp3_input.dev_attr.attr,
210 &sensor_dev_attr_temp4_input.dev_attr.attr,
211 &sensor_dev_attr_fan1_input.dev_attr.attr,
212 &sensor_dev_attr_fan2_input.dev_attr.attr,
213 &sensor_dev_attr_fan3_input.dev_attr.attr,
214 &sensor_dev_attr_fan4_input.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200215
Jean Delvare292fc1a2007-05-08 17:22:03 +0200216 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200217 NULL
218};
219
220static const struct attribute_group smsc47b397_group = {
221 .attrs = smsc47b397_attributes,
222};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jean Delvare292fc1a2007-05-08 17:22:03 +0200224static int __devexit smsc47b397_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200226 struct smsc47b397_data *data = platform_get_drvdata(pdev);
227 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Tony Jones1beeffe2007-08-20 13:46:20 -0700229 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200230 sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
231 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
232 release_region(res->start, SMSC_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400233 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 return 0;
236}
237
Jean Delvare292fc1a2007-05-08 17:22:03 +0200238static int smsc47b397_probe(struct platform_device *pdev);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200239
Jean Delvare292fc1a2007-05-08 17:22:03 +0200240static struct platform_driver smsc47b397_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100241 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200242 .owner = THIS_MODULE,
Jean Delvare292fc1a2007-05-08 17:22:03 +0200243 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100244 },
Jean Delvare292fc1a2007-05-08 17:22:03 +0200245 .probe = smsc47b397_probe,
246 .remove = __devexit_p(smsc47b397_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Jean Delvare292fc1a2007-05-08 17:22:03 +0200249static int __devinit smsc47b397_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200251 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct smsc47b397_data *data;
Jean Delvare292fc1a2007-05-08 17:22:03 +0200253 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int err = 0;
255
Jean Delvare292fc1a2007-05-08 17:22:03 +0200256 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
257 if (!request_region(res->start, SMSC_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100258 smsc47b397_driver.driver.name)) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200259 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
260 (unsigned long)res->start,
261 (unsigned long)res->start + SMSC_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -EBUSY;
263 }
264
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200265 if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 err = -ENOMEM;
267 goto error_release;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jean Delvare292fc1a2007-05-08 17:22:03 +0200270 data->addr = res->start;
271 data->name = "smsc47b397";
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100272 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100273 mutex_init(&data->update_lock);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200274 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Jean Delvare292fc1a2007-05-08 17:22:03 +0200276 if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto error_free;
278
Tony Jones1beeffe2007-08-20 13:46:20 -0700279 data->hwmon_dev = hwmon_device_register(dev);
280 if (IS_ERR(data->hwmon_dev)) {
281 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200282 goto error_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400283 }
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200287error_remove:
Jean Delvare292fc1a2007-05-08 17:22:03 +0200288 sysfs_remove_group(&dev->kobj, &smsc47b397_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289error_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400290 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291error_release:
Jean Delvare292fc1a2007-05-08 17:22:03 +0200292 release_region(res->start, SMSC_EXTENT);
293 return err;
294}
295
296static int __init smsc47b397_device_add(unsigned short address)
297{
298 struct resource res = {
299 .start = address,
300 .end = address + SMSC_EXTENT - 1,
301 .name = DRVNAME,
302 .flags = IORESOURCE_IO,
303 };
304 int err;
305
306 pdev = platform_device_alloc(DRVNAME, address);
307 if (!pdev) {
308 err = -ENOMEM;
309 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
310 goto exit;
311 }
312
313 err = platform_device_add_resources(pdev, &res, 1);
314 if (err) {
315 printk(KERN_ERR DRVNAME ": Device resource addition failed "
316 "(%d)\n", err);
317 goto exit_device_put;
318 }
319
320 err = platform_device_add(pdev);
321 if (err) {
322 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
323 err);
324 goto exit_device_put;
325 }
326
327 return 0;
328
329exit_device_put:
330 platform_device_put(pdev);
331exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return err;
333}
334
Jean Delvare2d8672c2005-07-19 23:56:35 +0200335static int __init smsc47b397_find(unsigned short *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 u8 id, rev;
338
339 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +0100340 id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Juerg Haefliger2dbbdb32007-06-20 15:41:33 -0700342 if ((id != 0x6f) && (id != 0x81) && (id != 0x85)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 superio_exit();
344 return -ENODEV;
345 }
346
347 rev = superio_inb(SUPERIO_REG_DEVREV);
348
349 superio_select(SUPERIO_REG_LD8);
350 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
351 | superio_inb(SUPERIO_REG_BASE_LSB);
352
Jean Delvare292fc1a2007-05-08 17:22:03 +0200353 printk(KERN_INFO DRVNAME ": found SMSC %s "
Mark M. Hoffman7ab83a92005-10-17 23:01:45 +0200354 "(base address 0x%04x, revision %u)\n",
Juerg Haefliger2dbbdb32007-06-20 15:41:33 -0700355 id == 0x81 ? "SCH5307-NS" : id == 0x85 ? "SCH5317" :
356 "LPC47B397-NC", *addr, rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 superio_exit();
359 return 0;
360}
361
362static int __init smsc47b397_init(void)
363{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200364 unsigned short address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int ret;
366
Jean Delvare2d8672c2005-07-19 23:56:35 +0200367 if ((ret = smsc47b397_find(&address)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return ret;
369
Jean Delvare292fc1a2007-05-08 17:22:03 +0200370 ret = platform_driver_register(&smsc47b397_driver);
371 if (ret)
372 goto exit;
373
374 /* Sets global pdev as a side effect */
375 ret = smsc47b397_device_add(address);
376 if (ret)
377 goto exit_driver;
378
379 return 0;
380
381exit_driver:
382 platform_driver_unregister(&smsc47b397_driver);
383exit:
384 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387static void __exit smsc47b397_exit(void)
388{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200389 platform_device_unregister(pdev);
390 platform_driver_unregister(&smsc47b397_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
394MODULE_DESCRIPTION("SMSC LPC47B397 driver");
395MODULE_LICENSE("GPL");
396
397module_init(smsc47b397_init);
398module_exit(smsc47b397_exit);