blob: c5f6be478bad2468dd495ec1c3659c9abf550f2e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck2a52dd62012-01-19 11:02:25 -08002 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Joe Perchesbf1a85e2010-10-20 06:51:48 +000029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/ioport.h>
34#include <linux/jiffies.h>
Jean Delvare292fc1a2007-05-08 17:22:03 +020035#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040036#include <linux/hwmon.h>
Jean Delvare3659a012007-05-08 17:22:03 +020037#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040038#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010040#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010041#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020042#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jean Delvare67b671b2007-12-06 23:13:42 +010044static unsigned short force_id;
45module_param(force_id, ushort, 0);
46MODULE_PARM_DESC(force_id, "Override the detected device ID");
47
Jean Delvare292fc1a2007-05-08 17:22:03 +020048static struct platform_device *pdev;
49
50#define DRVNAME "smsc47b397"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Super-I/0 registers and commands */
53
54#define REG 0x2e /* The register to read/write */
55#define VAL 0x2f /* The value to read/write */
56
57static inline void superio_outb(int reg, int val)
58{
59 outb(reg, REG);
60 outb(val, VAL);
61}
62
63static inline int superio_inb(int reg)
64{
65 outb(reg, REG);
66 return inb(VAL);
67}
68
69/* select superio logical device */
70static inline void superio_select(int ld)
71{
72 superio_outb(0x07, ld);
73}
74
75static inline void superio_enter(void)
76{
77 outb(0x55, REG);
78}
79
80static inline void superio_exit(void)
81{
82 outb(0xAA, REG);
83}
84
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
90
91#define SMSC_EXTENT 0x02
92
93/* 0 <= nr <= 3 */
94static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
95#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
96
97/* 0 <= nr <= 3 */
98#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
99#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
100
101struct smsc47b397_data {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200102 unsigned short addr;
103 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700104 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100105 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100107 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned long last_updated; /* in jiffies */
109 int valid;
110
111 /* register values */
112 u16 fan[4];
113 u8 temp[4];
114};
115
Jean Delvare371f2e02011-11-04 12:00:47 +0100116static int smsc47b397_read_value(struct smsc47b397_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 int res;
119
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100120 mutex_lock(&data->lock);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200121 outb(reg, data->addr);
122 res = inb_p(data->addr + 1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100123 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return res;
125}
126
127static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
128{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200129 struct smsc47b397_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int i;
131
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100132 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200135 dev_dbg(dev, "starting device update...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /* 4 temperature inputs, 4 fan inputs */
138 for (i = 0; i < 4; i++) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200139 data->temp[i] = smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 SMSC47B397_REG_TEMP(i));
141
142 /* must read LSB first */
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_LSB(i));
Jean Delvare292fc1a2007-05-08 17:22:03 +0200145 data->fan[i] |= smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 SMSC47B397_REG_FAN_MSB(i)) << 8;
147 }
148
149 data->last_updated = jiffies;
150 data->valid = 1;
151
Jean Delvare292fc1a2007-05-08 17:22:03 +0200152 dev_dbg(dev, "... device update complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100155 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return data;
158}
159
Guenter Roeck2a52dd62012-01-19 11:02:25 -0800160/*
161 * TEMP: 0.001C/bit (-128C to +127C)
162 * REG: 1C/bit, two's complement
163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static int temp_from_reg(u8 reg)
165{
166 return (s8)reg * 1000;
167}
168
Jean Delvare3659a012007-05-08 17:22:03 +0200169static ssize_t show_temp(struct device *dev, struct device_attribute
170 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Jean Delvare3659a012007-05-08 17:22:03 +0200172 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct smsc47b397_data *data = smsc47b397_update_device(dev);
Jean Delvare3659a012007-05-08 17:22:03 +0200174 return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Jean Delvare3659a012007-05-08 17:22:03 +0200177static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
178static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
179static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
180static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Guenter Roeck2a52dd62012-01-19 11:02:25 -0800182/*
183 * FAN: 1 RPM/bit
184 * REG: count of 90kHz pulses / revolution
185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static int fan_from_reg(u16 reg)
187{
Jean Delvare90205c62007-06-23 14:58:22 +0200188 if (reg == 0 || reg == 0xffff)
189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return 90000 * 60 / reg;
191}
192
Jean Delvare3659a012007-05-08 17:22:03 +0200193static ssize_t show_fan(struct device *dev, struct device_attribute
194 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Jean Delvare3659a012007-05-08 17:22:03 +0200196 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]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
Jean Delvare3659a012007-05-08 17:22:03 +0200200static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
201static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
202static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
203static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Jean Delvare292fc1a2007-05-08 17:22:03 +0200205static ssize_t show_name(struct device *dev, struct device_attribute
206 *devattr, char *buf)
207{
208 struct smsc47b397_data *data = dev_get_drvdata(dev);
209 return sprintf(buf, "%s\n", data->name);
210}
211static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
212
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200213static struct attribute *smsc47b397_attributes[] = {
Jean Delvare3659a012007-05-08 17:22:03 +0200214 &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,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200222
Jean Delvare292fc1a2007-05-08 17:22:03 +0200223 &dev_attr_name.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200224 NULL
225};
226
227static const struct attribute_group smsc47b397_group = {
228 .attrs = smsc47b397_attributes,
229};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Jean Delvare292fc1a2007-05-08 17:22:03 +0200231static int __devexit smsc47b397_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200233 struct smsc47b397_data *data = platform_get_drvdata(pdev);
234 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Tony Jones1beeffe2007-08-20 13:46:20 -0700236 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200237 sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
238 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
239 release_region(res->start, SMSC_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400240 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 return 0;
243}
244
Jean Delvare292fc1a2007-05-08 17:22:03 +0200245static int smsc47b397_probe(struct platform_device *pdev);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200246
Jean Delvare292fc1a2007-05-08 17:22:03 +0200247static struct platform_driver smsc47b397_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100248 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200249 .owner = THIS_MODULE,
Jean Delvare292fc1a2007-05-08 17:22:03 +0200250 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100251 },
Jean Delvare292fc1a2007-05-08 17:22:03 +0200252 .probe = smsc47b397_probe,
253 .remove = __devexit_p(smsc47b397_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
Jean Delvare292fc1a2007-05-08 17:22:03 +0200256static int __devinit smsc47b397_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200258 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct smsc47b397_data *data;
Jean Delvare292fc1a2007-05-08 17:22:03 +0200260 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int err = 0;
262
Jean Delvare292fc1a2007-05-08 17:22:03 +0200263 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
264 if (!request_region(res->start, SMSC_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100265 smsc47b397_driver.driver.name)) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200266 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
267 (unsigned long)res->start,
268 (unsigned long)res->start + SMSC_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -EBUSY;
270 }
271
Jean Delvare371f2e02011-11-04 12:00:47 +0100272 data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL);
273 if (!data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 err = -ENOMEM;
275 goto error_release;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Jean Delvare292fc1a2007-05-08 17:22:03 +0200278 data->addr = res->start;
279 data->name = "smsc47b397";
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100280 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100281 mutex_init(&data->update_lock);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200282 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jean Delvare371f2e02011-11-04 12:00:47 +0100284 err = sysfs_create_group(&dev->kobj, &smsc47b397_group);
285 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto error_free;
287
Tony Jones1beeffe2007-08-20 13:46:20 -0700288 data->hwmon_dev = hwmon_device_register(dev);
289 if (IS_ERR(data->hwmon_dev)) {
290 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200291 goto error_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400292 }
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200296error_remove:
Jean Delvare292fc1a2007-05-08 17:22:03 +0200297 sysfs_remove_group(&dev->kobj, &smsc47b397_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298error_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400299 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300error_release:
Jean Delvare292fc1a2007-05-08 17:22:03 +0200301 release_region(res->start, SMSC_EXTENT);
302 return err;
303}
304
305static int __init smsc47b397_device_add(unsigned short address)
306{
307 struct resource res = {
308 .start = address,
309 .end = address + SMSC_EXTENT - 1,
310 .name = DRVNAME,
311 .flags = IORESOURCE_IO,
312 };
313 int err;
314
Jean Delvareb9acb642009-01-07 16:37:35 +0100315 err = acpi_check_resource_conflict(&res);
316 if (err)
317 goto exit;
318
Jean Delvare292fc1a2007-05-08 17:22:03 +0200319 pdev = platform_device_alloc(DRVNAME, address);
320 if (!pdev) {
321 err = -ENOMEM;
Joe Perchesbf1a85e2010-10-20 06:51:48 +0000322 pr_err("Device allocation failed\n");
Jean Delvare292fc1a2007-05-08 17:22:03 +0200323 goto exit;
324 }
325
326 err = platform_device_add_resources(pdev, &res, 1);
327 if (err) {
Joe Perchesbf1a85e2010-10-20 06:51:48 +0000328 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200329 goto exit_device_put;
330 }
331
332 err = platform_device_add(pdev);
333 if (err) {
Joe Perchesbf1a85e2010-10-20 06:51:48 +0000334 pr_err("Device addition failed (%d)\n", err);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200335 goto exit_device_put;
336 }
337
338 return 0;
339
340exit_device_put:
341 platform_device_put(pdev);
342exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return err;
344}
345
Guenter Roeck8528e072012-03-28 08:35:26 -0700346static int __init smsc47b397_find(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 u8 id, rev;
Craig Kelley80930772008-02-29 10:24:44 -0700349 char *name;
Guenter Roeck8528e072012-03-28 08:35:26 -0700350 unsigned short addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 superio_enter();
Jean Delvare67b671b2007-12-06 23:13:42 +0100353 id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Jean Delvare371f2e02011-11-04 12:00:47 +0100355 switch (id) {
Craig Kelley80930772008-02-29 10:24:44 -0700356 case 0x81:
357 name = "SCH5307-NS";
358 break;
359 case 0x6f:
360 name = "LPC47B397-NC";
361 break;
362 case 0x85:
363 case 0x8c:
364 name = "SCH5317";
365 break;
366 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 superio_exit();
368 return -ENODEV;
369 }
370
371 rev = superio_inb(SUPERIO_REG_DEVREV);
372
373 superio_select(SUPERIO_REG_LD8);
Guenter Roeck8528e072012-03-28 08:35:26 -0700374 addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 | superio_inb(SUPERIO_REG_BASE_LSB);
376
Joe Perchesbf1a85e2010-10-20 06:51:48 +0000377 pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
Guenter Roeck8528e072012-03-28 08:35:26 -0700378 name, addr, rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 superio_exit();
Guenter Roeck8528e072012-03-28 08:35:26 -0700381 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384static int __init smsc47b397_init(void)
385{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200386 unsigned short address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int ret;
388
Guenter Roeck8528e072012-03-28 08:35:26 -0700389 ret = smsc47b397_find();
390 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return ret;
Guenter Roeck8528e072012-03-28 08:35:26 -0700392 address = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jean Delvare292fc1a2007-05-08 17:22:03 +0200394 ret = platform_driver_register(&smsc47b397_driver);
395 if (ret)
396 goto exit;
397
398 /* Sets global pdev as a side effect */
399 ret = smsc47b397_device_add(address);
400 if (ret)
401 goto exit_driver;
402
403 return 0;
404
405exit_driver:
406 platform_driver_unregister(&smsc47b397_driver);
407exit:
408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411static void __exit smsc47b397_exit(void)
412{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200413 platform_device_unregister(pdev);
414 platform_driver_unregister(&smsc47b397_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
418MODULE_DESCRIPTION("SMSC LPC47B397 driver");
419MODULE_LICENSE("GPL");
420
421module_init(smsc47b397_init);
422module_exit(smsc47b397_exit);