blob: cbdb5c4991ae3132538d36d54fb40aa2943bca1a [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>
Jean Delvare7c81c602014-01-29 20:40:08 +010012 * Copyright (C) 2004 Jean Delvare <jdelvare@suse.de>
Guenter Roeck2a52dd62012-01-19 11:02:25 -080013 *
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
Guenter Roecka7f8e2d2019-04-04 11:22:42 -070075static inline int superio_enter(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Guenter Roecka7f8e2d2019-04-04 11:22:42 -070077 if (!request_muxed_region(REG, 2, DRVNAME))
78 return -EBUSY;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 outb(0x55, REG);
Guenter Roecka7f8e2d2019-04-04 11:22:42 -070081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84static inline void superio_exit(void)
85{
86 outb(0xAA, REG);
Guenter Roecka7f8e2d2019-04-04 11:22:42 -070087 release_region(REG, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90#define SUPERIO_REG_DEVID 0x20
91#define SUPERIO_REG_DEVREV 0x21
92#define SUPERIO_REG_BASE_MSB 0x60
93#define SUPERIO_REG_BASE_LSB 0x61
94#define SUPERIO_REG_LD8 0x08
95
96#define SMSC_EXTENT 0x02
97
98/* 0 <= nr <= 3 */
99static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
100#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
101
102/* 0 <= nr <= 3 */
103#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
104#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
105
106struct smsc47b397_data {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200107 unsigned short addr;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100108 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100110 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long last_updated; /* in jiffies */
112 int valid;
113
114 /* register values */
115 u16 fan[4];
116 u8 temp[4];
117};
118
Jean Delvare371f2e02011-11-04 12:00:47 +0100119static int smsc47b397_read_value(struct smsc47b397_data *data, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int res;
122
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100123 mutex_lock(&data->lock);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200124 outb(reg, data->addr);
125 res = inb_p(data->addr + 1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100126 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return res;
128}
129
130static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
131{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200132 struct smsc47b397_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int i;
134
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100135 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200138 dev_dbg(dev, "starting device update...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* 4 temperature inputs, 4 fan inputs */
141 for (i = 0; i < 4; i++) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200142 data->temp[i] = smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 SMSC47B397_REG_TEMP(i));
144
145 /* must read LSB first */
Jean Delvare292fc1a2007-05-08 17:22:03 +0200146 data->fan[i] = smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 SMSC47B397_REG_FAN_LSB(i));
Jean Delvare292fc1a2007-05-08 17:22:03 +0200148 data->fan[i] |= smsc47b397_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 SMSC47B397_REG_FAN_MSB(i)) << 8;
150 }
151
152 data->last_updated = jiffies;
153 data->valid = 1;
154
Jean Delvare292fc1a2007-05-08 17:22:03 +0200155 dev_dbg(dev, "... device update complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100158 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return data;
161}
162
Guenter Roeck2a52dd62012-01-19 11:02:25 -0800163/*
164 * TEMP: 0.001C/bit (-128C to +127C)
165 * REG: 1C/bit, two's complement
166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static int temp_from_reg(u8 reg)
168{
169 return (s8)reg * 1000;
170}
171
Jean Delvare3659a012007-05-08 17:22:03 +0200172static ssize_t show_temp(struct device *dev, struct device_attribute
173 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Jean Delvare3659a012007-05-08 17:22:03 +0200175 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct smsc47b397_data *data = smsc47b397_update_device(dev);
Jean Delvare3659a012007-05-08 17:22:03 +0200177 return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Jean Delvare3659a012007-05-08 17:22:03 +0200180static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
181static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
182static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
183static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Guenter Roeck2a52dd62012-01-19 11:02:25 -0800185/*
186 * FAN: 1 RPM/bit
187 * REG: count of 90kHz pulses / revolution
188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int fan_from_reg(u16 reg)
190{
Jean Delvare90205c62007-06-23 14:58:22 +0200191 if (reg == 0 || reg == 0xffff)
192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 90000 * 60 / reg;
194}
195
Jean Delvare3659a012007-05-08 17:22:03 +0200196static ssize_t show_fan(struct device *dev, struct device_attribute
197 *devattr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Jean Delvare3659a012007-05-08 17:22:03 +0200199 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
200 struct smsc47b397_data *data = smsc47b397_update_device(dev);
201 return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
Jean Delvare3659a012007-05-08 17:22:03 +0200203static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
204static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
205static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
206static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Axel Lin9b993e32014-07-08 09:27:38 +0800208static struct attribute *smsc47b397_attrs[] = {
Jean Delvare3659a012007-05-08 17:22:03 +0200209 &sensor_dev_attr_temp1_input.dev_attr.attr,
210 &sensor_dev_attr_temp2_input.dev_attr.attr,
211 &sensor_dev_attr_temp3_input.dev_attr.attr,
212 &sensor_dev_attr_temp4_input.dev_attr.attr,
213 &sensor_dev_attr_fan1_input.dev_attr.attr,
214 &sensor_dev_attr_fan2_input.dev_attr.attr,
215 &sensor_dev_attr_fan3_input.dev_attr.attr,
216 &sensor_dev_attr_fan4_input.dev_attr.attr,
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200217
218 NULL
219};
220
Axel Lin9b993e32014-07-08 09:27:38 +0800221ATTRIBUTE_GROUPS(smsc47b397);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Jean Delvare292fc1a2007-05-08 17:22:03 +0200223static int smsc47b397_probe(struct platform_device *pdev);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200224
Jean Delvare292fc1a2007-05-08 17:22:03 +0200225static struct platform_driver smsc47b397_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100226 .driver = {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200227 .name = DRVNAME,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100228 },
Jean Delvare292fc1a2007-05-08 17:22:03 +0200229 .probe = smsc47b397_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
231
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500232static int smsc47b397_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200234 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct smsc47b397_data *data;
Axel Lin9b993e32014-07-08 09:27:38 +0800236 struct device *hwmon_dev;
Jean Delvare292fc1a2007-05-08 17:22:03 +0200237 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jean Delvare292fc1a2007-05-08 17:22:03 +0200239 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roecke43d5fe2012-06-02 12:04:06 -0700240 if (!devm_request_region(dev, res->start, SMSC_EXTENT,
241 smsc47b397_driver.driver.name)) {
Jean Delvare292fc1a2007-05-08 17:22:03 +0200242 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
243 (unsigned long)res->start,
244 (unsigned long)res->start + SMSC_EXTENT - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -EBUSY;
246 }
247
Guenter Roecke43d5fe2012-06-02 12:04:06 -0700248 data = devm_kzalloc(dev, sizeof(struct smsc47b397_data), GFP_KERNEL);
249 if (!data)
250 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jean Delvare292fc1a2007-05-08 17:22:03 +0200252 data->addr = res->start;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100253 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100254 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Axel Lin9b993e32014-07-08 09:27:38 +0800256 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "smsc47b397",
257 data,
258 smsc47b397_groups);
259 return PTR_ERR_OR_ZERO(hwmon_dev);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200260}
261
262static int __init smsc47b397_device_add(unsigned short address)
263{
264 struct resource res = {
265 .start = address,
266 .end = address + SMSC_EXTENT - 1,
267 .name = DRVNAME,
268 .flags = IORESOURCE_IO,
269 };
270 int err;
271
Jean Delvareb9acb642009-01-07 16:37:35 +0100272 err = acpi_check_resource_conflict(&res);
273 if (err)
274 goto exit;
275
Jean Delvare292fc1a2007-05-08 17:22:03 +0200276 pdev = platform_device_alloc(DRVNAME, address);
277 if (!pdev) {
278 err = -ENOMEM;
Joe Perchesbf1a85e2010-10-20 06:51:48 +0000279 pr_err("Device allocation failed\n");
Jean Delvare292fc1a2007-05-08 17:22:03 +0200280 goto exit;
281 }
282
283 err = platform_device_add_resources(pdev, &res, 1);
284 if (err) {
Joe Perchesbf1a85e2010-10-20 06:51:48 +0000285 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200286 goto exit_device_put;
287 }
288
289 err = platform_device_add(pdev);
290 if (err) {
Joe Perchesbf1a85e2010-10-20 06:51:48 +0000291 pr_err("Device addition failed (%d)\n", err);
Jean Delvare292fc1a2007-05-08 17:22:03 +0200292 goto exit_device_put;
293 }
294
295 return 0;
296
297exit_device_put:
298 platform_device_put(pdev);
299exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return err;
301}
302
Guenter Roeck8528e072012-03-28 08:35:26 -0700303static int __init smsc47b397_find(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 u8 id, rev;
Craig Kelley80930772008-02-29 10:24:44 -0700306 char *name;
Guenter Roeck8528e072012-03-28 08:35:26 -0700307 unsigned short addr;
Guenter Roecka7f8e2d2019-04-04 11:22:42 -0700308 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Guenter Roecka7f8e2d2019-04-04 11:22:42 -0700310 err = superio_enter();
311 if (err)
312 return err;
313
Jean Delvare67b671b2007-12-06 23:13:42 +0100314 id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Jean Delvare371f2e02011-11-04 12:00:47 +0100316 switch (id) {
Craig Kelley80930772008-02-29 10:24:44 -0700317 case 0x81:
318 name = "SCH5307-NS";
319 break;
320 case 0x6f:
321 name = "LPC47B397-NC";
322 break;
323 case 0x85:
324 case 0x8c:
325 name = "SCH5317";
326 break;
327 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 superio_exit();
329 return -ENODEV;
330 }
331
332 rev = superio_inb(SUPERIO_REG_DEVREV);
333
334 superio_select(SUPERIO_REG_LD8);
Guenter Roeck8528e072012-03-28 08:35:26 -0700335 addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 | superio_inb(SUPERIO_REG_BASE_LSB);
337
Joe Perchesbf1a85e2010-10-20 06:51:48 +0000338 pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
Guenter Roeck8528e072012-03-28 08:35:26 -0700339 name, addr, rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 superio_exit();
Guenter Roeck8528e072012-03-28 08:35:26 -0700342 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345static int __init smsc47b397_init(void)
346{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200347 unsigned short address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int ret;
349
Guenter Roeck8528e072012-03-28 08:35:26 -0700350 ret = smsc47b397_find();
351 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return ret;
Guenter Roeck8528e072012-03-28 08:35:26 -0700353 address = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Jean Delvare292fc1a2007-05-08 17:22:03 +0200355 ret = platform_driver_register(&smsc47b397_driver);
356 if (ret)
357 goto exit;
358
359 /* Sets global pdev as a side effect */
360 ret = smsc47b397_device_add(address);
361 if (ret)
362 goto exit_driver;
363
364 return 0;
365
366exit_driver:
367 platform_driver_unregister(&smsc47b397_driver);
368exit:
369 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static void __exit smsc47b397_exit(void)
373{
Jean Delvare292fc1a2007-05-08 17:22:03 +0200374 platform_device_unregister(pdev);
375 platform_driver_unregister(&smsc47b397_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
379MODULE_DESCRIPTION("SMSC LPC47B397 driver");
380MODULE_LICENSE("GPL");
381
382module_init(smsc47b397_init);
383module_exit(smsc47b397_exit);