blob: a85869393bab323f361a94b3daee02fb46bb599f [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>
33#include <linux/i2c.h>
Jean Delvarefde09502005-07-19 23:51:07 +020034#include <linux/i2c-isa.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040035#include <linux/hwmon.h>
36#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* Address is autodetected, there is no default value */
Jean Delvare2d8672c2005-07-19 23:56:35 +020042static unsigned short address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* Super-I/0 registers and commands */
45
46#define REG 0x2e /* The register to read/write */
47#define VAL 0x2f /* The value to read/write */
48
49static inline void superio_outb(int reg, int val)
50{
51 outb(reg, REG);
52 outb(val, VAL);
53}
54
55static inline int superio_inb(int reg)
56{
57 outb(reg, REG);
58 return inb(VAL);
59}
60
61/* select superio logical device */
62static inline void superio_select(int ld)
63{
64 superio_outb(0x07, ld);
65}
66
67static inline void superio_enter(void)
68{
69 outb(0x55, REG);
70}
71
72static inline void superio_exit(void)
73{
74 outb(0xAA, REG);
75}
76
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
82
83#define SMSC_EXTENT 0x02
84
85/* 0 <= nr <= 3 */
86static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
87#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
88
89/* 0 <= nr <= 3 */
90#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
91#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
92
93struct smsc47b397_data {
94 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -040095 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +010096 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Ingo Molnar9a61bf62006-01-18 23:19:26 +010098 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned long last_updated; /* in jiffies */
100 int valid;
101
102 /* register values */
103 u16 fan[4];
104 u8 temp[4];
105};
106
107static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
108{
109 struct smsc47b397_data *data = i2c_get_clientdata(client);
110 int res;
111
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100112 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 outb(reg, client->addr);
114 res = inb_p(client->addr + 1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100115 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return res;
117}
118
119static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
120{
121 struct i2c_client *client = to_i2c_client(dev);
122 struct smsc47b397_data *data = i2c_get_clientdata(client);
123 int i;
124
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100125 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
128 dev_dbg(&client->dev, "starting device update...\n");
129
130 /* 4 temperature inputs, 4 fan inputs */
131 for (i = 0; i < 4; i++) {
132 data->temp[i] = smsc47b397_read_value(client,
133 SMSC47B397_REG_TEMP(i));
134
135 /* must read LSB first */
136 data->fan[i] = smsc47b397_read_value(client,
137 SMSC47B397_REG_FAN_LSB(i));
138 data->fan[i] |= smsc47b397_read_value(client,
139 SMSC47B397_REG_FAN_MSB(i)) << 8;
140 }
141
142 data->last_updated = jiffies;
143 data->valid = 1;
144
145 dev_dbg(&client->dev, "... device update complete\n");
146 }
147
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100148 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return data;
151}
152
153/* TEMP: 0.001C/bit (-128C to +127C)
154 REG: 1C/bit, two's complement */
155static int temp_from_reg(u8 reg)
156{
157 return (s8)reg * 1000;
158}
159
160/* 0 <= nr <= 3 */
161static ssize_t show_temp(struct device *dev, char *buf, int nr)
162{
163 struct smsc47b397_data *data = smsc47b397_update_device(dev);
164 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
165}
166
167#define sysfs_temp(num) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400168static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{ \
170 return show_temp(dev, buf, num-1); \
171} \
172static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
173
174sysfs_temp(1);
175sysfs_temp(2);
176sysfs_temp(3);
177sysfs_temp(4);
178
179#define device_create_file_temp(client, num) \
180 device_create_file(&client->dev, &dev_attr_temp##num##_input)
181
182/* FAN: 1 RPM/bit
183 REG: count of 90kHz pulses / revolution */
184static int fan_from_reg(u16 reg)
185{
186 return 90000 * 60 / reg;
187}
188
189/* 0 <= nr <= 3 */
190static ssize_t show_fan(struct device *dev, char *buf, int nr)
191{
192 struct smsc47b397_data *data = smsc47b397_update_device(dev);
193 return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
194}
195
196#define sysfs_fan(num) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400197static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{ \
199 return show_fan(dev, buf, num-1); \
200} \
201static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
202
203sysfs_fan(1);
204sysfs_fan(2);
205sysfs_fan(3);
206sysfs_fan(4);
207
208#define device_create_file_fan(client, num) \
209 device_create_file(&client->dev, &dev_attr_fan##num##_input)
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static int smsc47b397_detach_client(struct i2c_client *client)
212{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400213 struct smsc47b397_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int err;
215
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400216 hwmon_device_unregister(data->class_dev);
217
Jean Delvare7bef5592005-07-27 22:14:49 +0200218 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 release_region(client->addr, SMSC_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400222 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 return 0;
225}
226
Jean Delvare2d8672c2005-07-19 23:56:35 +0200227static int smsc47b397_detect(struct i2c_adapter *adapter);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static struct i2c_driver smsc47b397_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100230 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200231 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100232 .name = "smsc47b397",
233 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200234 .attach_adapter = smsc47b397_detect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .detach_client = smsc47b397_detach_client,
236};
237
Jean Delvare2d8672c2005-07-19 23:56:35 +0200238static int smsc47b397_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct i2c_client *new_client;
241 struct smsc47b397_data *data;
242 int err = 0;
243
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100244 if (!request_region(address, SMSC_EXTENT,
245 smsc47b397_driver.driver.name)) {
Jean Delvare2d8672c2005-07-19 23:56:35 +0200246 dev_err(&adapter->dev, "Region 0x%x already in use!\n",
247 address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EBUSY;
249 }
250
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200251 if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 err = -ENOMEM;
253 goto error_release;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 new_client = &data->client;
257 i2c_set_clientdata(new_client, data);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200258 new_client->addr = address;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100259 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 new_client->adapter = adapter;
261 new_client->driver = &smsc47b397_driver;
262 new_client->flags = 0;
263
264 strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
265
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100266 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if ((err = i2c_attach_client(new_client)))
269 goto error_free;
270
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400271 data->class_dev = hwmon_device_register(&new_client->dev);
272 if (IS_ERR(data->class_dev)) {
273 err = PTR_ERR(data->class_dev);
274 goto error_detach;
275 }
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 device_create_file_temp(new_client, 1);
278 device_create_file_temp(new_client, 2);
279 device_create_file_temp(new_client, 3);
280 device_create_file_temp(new_client, 4);
281
282 device_create_file_fan(new_client, 1);
283 device_create_file_fan(new_client, 2);
284 device_create_file_fan(new_client, 3);
285 device_create_file_fan(new_client, 4);
286
287 return 0;
288
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400289error_detach:
290 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291error_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400292 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293error_release:
Jean Delvare2d8672c2005-07-19 23:56:35 +0200294 release_region(address, SMSC_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return err;
296}
297
Jean Delvare2d8672c2005-07-19 23:56:35 +0200298static int __init smsc47b397_find(unsigned short *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 u8 id, rev;
301
302 superio_enter();
303 id = superio_inb(SUPERIO_REG_DEVID);
304
Mark M. Hoffman7ab83a92005-10-17 23:01:45 +0200305 if ((id != 0x6f) && (id != 0x81)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 superio_exit();
307 return -ENODEV;
308 }
309
310 rev = superio_inb(SUPERIO_REG_DEVREV);
311
312 superio_select(SUPERIO_REG_LD8);
313 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
314 | superio_inb(SUPERIO_REG_BASE_LSB);
315
Mark M. Hoffman7ab83a92005-10-17 23:01:45 +0200316 printk(KERN_INFO "smsc47b397: found SMSC %s "
317 "(base address 0x%04x, revision %u)\n",
318 id == 0x81 ? "SCH5307-NS" : "LPC47B397-NC", *addr, rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 superio_exit();
321 return 0;
322}
323
324static int __init smsc47b397_init(void)
325{
326 int ret;
327
Jean Delvare2d8672c2005-07-19 23:56:35 +0200328 if ((ret = smsc47b397_find(&address)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return ret;
330
Jean Delvarefde09502005-07-19 23:51:07 +0200331 return i2c_isa_add_driver(&smsc47b397_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334static void __exit smsc47b397_exit(void)
335{
Jean Delvarefde09502005-07-19 23:51:07 +0200336 i2c_isa_del_driver(&smsc47b397_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
340MODULE_DESCRIPTION("SMSC LPC47B397 driver");
341MODULE_LICENSE("GPL");
342
343module_init(smsc47b397_init);
344module_exit(smsc47b397_exit);