blob: 1119c76791d9e48db843d4191ce6bcd38a5c1849 [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>
34#include <linux/i2c-sensor.h>
35#include <linux/init.h>
36#include <asm/io.h>
37
38static unsigned short normal_i2c[] = { I2C_CLIENT_END };
39/* Address is autodetected, there is no default value */
40static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
41static struct i2c_force_data forces[] = {{NULL}};
42
43enum chips { any_chip, smsc47b397 };
44static struct i2c_address_data addr_data = {
45 .normal_i2c = normal_i2c,
46 .normal_isa = normal_isa,
47 .probe = normal_i2c, /* cheat */
48 .ignore = normal_i2c, /* cheat */
49 .forces = forces,
50};
51
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 {
102 struct i2c_client client;
103 struct semaphore lock;
104
105 struct semaphore update_lock;
106 unsigned long last_updated; /* in jiffies */
107 int valid;
108
109 /* register values */
110 u16 fan[4];
111 u8 temp[4];
112};
113
114static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
115{
116 struct smsc47b397_data *data = i2c_get_clientdata(client);
117 int res;
118
119 down(&data->lock);
120 outb(reg, client->addr);
121 res = inb_p(client->addr + 1);
122 up(&data->lock);
123 return res;
124}
125
126static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
127{
128 struct i2c_client *client = to_i2c_client(dev);
129 struct smsc47b397_data *data = i2c_get_clientdata(client);
130 int i;
131
132 down(&data->update_lock);
133
134 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
135 dev_dbg(&client->dev, "starting device update...\n");
136
137 /* 4 temperature inputs, 4 fan inputs */
138 for (i = 0; i < 4; i++) {
139 data->temp[i] = smsc47b397_read_value(client,
140 SMSC47B397_REG_TEMP(i));
141
142 /* must read LSB first */
143 data->fan[i] = smsc47b397_read_value(client,
144 SMSC47B397_REG_FAN_LSB(i));
145 data->fan[i] |= smsc47b397_read_value(client,
146 SMSC47B397_REG_FAN_MSB(i)) << 8;
147 }
148
149 data->last_updated = jiffies;
150 data->valid = 1;
151
152 dev_dbg(&client->dev, "... device update complete\n");
153 }
154
155 up(&data->update_lock);
156
157 return data;
158}
159
160/* TEMP: 0.001C/bit (-128C to +127C)
161 REG: 1C/bit, two's complement */
162static int temp_from_reg(u8 reg)
163{
164 return (s8)reg * 1000;
165}
166
167/* 0 <= nr <= 3 */
168static ssize_t show_temp(struct device *dev, char *buf, int nr)
169{
170 struct smsc47b397_data *data = smsc47b397_update_device(dev);
171 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
172}
173
174#define sysfs_temp(num) \
175static ssize_t show_temp##num(struct device *dev, char *buf) \
176{ \
177 return show_temp(dev, buf, num-1); \
178} \
179static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
180
181sysfs_temp(1);
182sysfs_temp(2);
183sysfs_temp(3);
184sysfs_temp(4);
185
186#define device_create_file_temp(client, num) \
187 device_create_file(&client->dev, &dev_attr_temp##num##_input)
188
189/* FAN: 1 RPM/bit
190 REG: count of 90kHz pulses / revolution */
191static int fan_from_reg(u16 reg)
192{
193 return 90000 * 60 / reg;
194}
195
196/* 0 <= nr <= 3 */
197static ssize_t show_fan(struct device *dev, char *buf, int nr)
198{
199 struct smsc47b397_data *data = smsc47b397_update_device(dev);
200 return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
201}
202
203#define sysfs_fan(num) \
204static ssize_t show_fan##num(struct device *dev, char *buf) \
205{ \
206 return show_fan(dev, buf, num-1); \
207} \
208static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
209
210sysfs_fan(1);
211sysfs_fan(2);
212sysfs_fan(3);
213sysfs_fan(4);
214
215#define device_create_file_fan(client, num) \
216 device_create_file(&client->dev, &dev_attr_fan##num##_input)
217
218static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind);
219
220static int smsc47b397_attach_adapter(struct i2c_adapter *adapter)
221{
222 if (!(adapter->class & I2C_CLASS_HWMON))
223 return 0;
224 return i2c_detect(adapter, &addr_data, smsc47b397_detect);
225}
226
227static int smsc47b397_detach_client(struct i2c_client *client)
228{
229 int err;
230
231 if ((err = i2c_detach_client(client))) {
232 dev_err(&client->dev, "Client deregistration failed, "
233 "client not detached.\n");
234 return err;
235 }
236
237 release_region(client->addr, SMSC_EXTENT);
238 kfree(i2c_get_clientdata(client));
239
240 return 0;
241}
242
243static struct i2c_driver smsc47b397_driver = {
244 .owner = THIS_MODULE,
245 .name = "smsc47b397",
246 .id = I2C_DRIVERID_SMSC47B397,
247 .flags = I2C_DF_NOTIFY,
248 .attach_adapter = smsc47b397_attach_adapter,
249 .detach_client = smsc47b397_detach_client,
250};
251
252static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind)
253{
254 struct i2c_client *new_client;
255 struct smsc47b397_data *data;
256 int err = 0;
257
258 if (!i2c_is_isa_adapter(adapter)) {
259 return 0;
260 }
261
262 if (!request_region(addr, SMSC_EXTENT, smsc47b397_driver.name)) {
263 dev_err(&adapter->dev, "Region 0x%x already in use!\n", addr);
264 return -EBUSY;
265 }
266
267 if (!(data = kmalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
268 err = -ENOMEM;
269 goto error_release;
270 }
271 memset(data, 0x00, sizeof(struct smsc47b397_data));
272
273 new_client = &data->client;
274 i2c_set_clientdata(new_client, data);
275 new_client->addr = addr;
276 init_MUTEX(&data->lock);
277 new_client->adapter = adapter;
278 new_client->driver = &smsc47b397_driver;
279 new_client->flags = 0;
280
281 strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
282
283 init_MUTEX(&data->update_lock);
284
285 if ((err = i2c_attach_client(new_client)))
286 goto error_free;
287
288 device_create_file_temp(new_client, 1);
289 device_create_file_temp(new_client, 2);
290 device_create_file_temp(new_client, 3);
291 device_create_file_temp(new_client, 4);
292
293 device_create_file_fan(new_client, 1);
294 device_create_file_fan(new_client, 2);
295 device_create_file_fan(new_client, 3);
296 device_create_file_fan(new_client, 4);
297
298 return 0;
299
300error_free:
301 kfree(new_client);
302error_release:
303 release_region(addr, SMSC_EXTENT);
304 return err;
305}
306
307static int __init smsc47b397_find(unsigned int *addr)
308{
309 u8 id, rev;
310
311 superio_enter();
312 id = superio_inb(SUPERIO_REG_DEVID);
313
314 if (id != 0x6f) {
315 superio_exit();
316 return -ENODEV;
317 }
318
319 rev = superio_inb(SUPERIO_REG_DEVREV);
320
321 superio_select(SUPERIO_REG_LD8);
322 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
323 | superio_inb(SUPERIO_REG_BASE_LSB);
324
325 printk(KERN_INFO "smsc47b397: found SMSC LPC47B397-NC "
326 "(base address 0x%04x, revision %u)\n", *addr, rev);
327
328 superio_exit();
329 return 0;
330}
331
332static int __init smsc47b397_init(void)
333{
334 int ret;
335
336 if ((ret = smsc47b397_find(normal_isa)))
337 return ret;
338
339 return i2c_add_driver(&smsc47b397_driver);
340}
341
342static void __exit smsc47b397_exit(void)
343{
344 i2c_del_driver(&smsc47b397_driver);
345}
346
347MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
348MODULE_DESCRIPTION("SMSC LPC47B397 driver");
349MODULE_LICENSE("GPL");
350
351module_init(smsc47b397_init);
352module_exit(smsc47b397_exit);