blob: 96b9eb40f4256b37301b4fcfe2e425c8d1ac1f6c [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>
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>
38#include <asm/io.h>
39
40static unsigned short normal_i2c[] = { I2C_CLIENT_END };
41/* Address is autodetected, there is no default value */
42static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
43static struct i2c_force_data forces[] = {{NULL}};
44
45enum chips { any_chip, smsc47b397 };
46static struct i2c_address_data addr_data = {
47 .normal_i2c = normal_i2c,
48 .normal_isa = normal_isa,
49 .probe = normal_i2c, /* cheat */
50 .ignore = normal_i2c, /* cheat */
51 .forces = forces,
52};
53
54/* Super-I/0 registers and commands */
55
56#define REG 0x2e /* The register to read/write */
57#define VAL 0x2f /* The value to read/write */
58
59static inline void superio_outb(int reg, int val)
60{
61 outb(reg, REG);
62 outb(val, VAL);
63}
64
65static inline int superio_inb(int reg)
66{
67 outb(reg, REG);
68 return inb(VAL);
69}
70
71/* select superio logical device */
72static inline void superio_select(int ld)
73{
74 superio_outb(0x07, ld);
75}
76
77static inline void superio_enter(void)
78{
79 outb(0x55, REG);
80}
81
82static inline void superio_exit(void)
83{
84 outb(0xAA, REG);
85}
86
87#define SUPERIO_REG_DEVID 0x20
88#define SUPERIO_REG_DEVREV 0x21
89#define SUPERIO_REG_BASE_MSB 0x60
90#define SUPERIO_REG_BASE_LSB 0x61
91#define SUPERIO_REG_LD8 0x08
92
93#define SMSC_EXTENT 0x02
94
95/* 0 <= nr <= 3 */
96static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
97#define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
98
99/* 0 <= nr <= 3 */
100#define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
101#define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
102
103struct smsc47b397_data {
104 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400105 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct semaphore lock;
107
108 struct semaphore update_lock;
109 unsigned long last_updated; /* in jiffies */
110 int valid;
111
112 /* register values */
113 u16 fan[4];
114 u8 temp[4];
115};
116
117static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
118{
119 struct smsc47b397_data *data = i2c_get_clientdata(client);
120 int res;
121
122 down(&data->lock);
123 outb(reg, client->addr);
124 res = inb_p(client->addr + 1);
125 up(&data->lock);
126 return res;
127}
128
129static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
130{
131 struct i2c_client *client = to_i2c_client(dev);
132 struct smsc47b397_data *data = i2c_get_clientdata(client);
133 int i;
134
135 down(&data->update_lock);
136
137 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
138 dev_dbg(&client->dev, "starting device update...\n");
139
140 /* 4 temperature inputs, 4 fan inputs */
141 for (i = 0; i < 4; i++) {
142 data->temp[i] = smsc47b397_read_value(client,
143 SMSC47B397_REG_TEMP(i));
144
145 /* must read LSB first */
146 data->fan[i] = smsc47b397_read_value(client,
147 SMSC47B397_REG_FAN_LSB(i));
148 data->fan[i] |= smsc47b397_read_value(client,
149 SMSC47B397_REG_FAN_MSB(i)) << 8;
150 }
151
152 data->last_updated = jiffies;
153 data->valid = 1;
154
155 dev_dbg(&client->dev, "... device update complete\n");
156 }
157
158 up(&data->update_lock);
159
160 return data;
161}
162
163/* TEMP: 0.001C/bit (-128C to +127C)
164 REG: 1C/bit, two's complement */
165static int temp_from_reg(u8 reg)
166{
167 return (s8)reg * 1000;
168}
169
170/* 0 <= nr <= 3 */
171static ssize_t show_temp(struct device *dev, char *buf, int nr)
172{
173 struct smsc47b397_data *data = smsc47b397_update_device(dev);
174 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
175}
176
177#define sysfs_temp(num) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400178static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{ \
180 return show_temp(dev, buf, num-1); \
181} \
182static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
183
184sysfs_temp(1);
185sysfs_temp(2);
186sysfs_temp(3);
187sysfs_temp(4);
188
189#define device_create_file_temp(client, num) \
190 device_create_file(&client->dev, &dev_attr_temp##num##_input)
191
192/* FAN: 1 RPM/bit
193 REG: count of 90kHz pulses / revolution */
194static int fan_from_reg(u16 reg)
195{
196 return 90000 * 60 / reg;
197}
198
199/* 0 <= nr <= 3 */
200static ssize_t show_fan(struct device *dev, char *buf, int nr)
201{
202 struct smsc47b397_data *data = smsc47b397_update_device(dev);
203 return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
204}
205
206#define sysfs_fan(num) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400207static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{ \
209 return show_fan(dev, buf, num-1); \
210} \
211static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
212
213sysfs_fan(1);
214sysfs_fan(2);
215sysfs_fan(3);
216sysfs_fan(4);
217
218#define device_create_file_fan(client, num) \
219 device_create_file(&client->dev, &dev_attr_fan##num##_input)
220
221static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind);
222
223static int smsc47b397_attach_adapter(struct i2c_adapter *adapter)
224{
225 if (!(adapter->class & I2C_CLASS_HWMON))
226 return 0;
227 return i2c_detect(adapter, &addr_data, smsc47b397_detect);
228}
229
230static int smsc47b397_detach_client(struct i2c_client *client)
231{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400232 struct smsc47b397_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int err;
234
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400235 hwmon_device_unregister(data->class_dev);
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if ((err = i2c_detach_client(client))) {
238 dev_err(&client->dev, "Client deregistration failed, "
239 "client not detached.\n");
240 return err;
241 }
242
243 release_region(client->addr, SMSC_EXTENT);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400244 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 return 0;
247}
248
249static struct i2c_driver smsc47b397_driver = {
250 .owner = THIS_MODULE,
251 .name = "smsc47b397",
252 .id = I2C_DRIVERID_SMSC47B397,
253 .flags = I2C_DF_NOTIFY,
254 .attach_adapter = smsc47b397_attach_adapter,
255 .detach_client = smsc47b397_detach_client,
256};
257
258static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind)
259{
260 struct i2c_client *new_client;
261 struct smsc47b397_data *data;
262 int err = 0;
263
264 if (!i2c_is_isa_adapter(adapter)) {
265 return 0;
266 }
267
268 if (!request_region(addr, SMSC_EXTENT, smsc47b397_driver.name)) {
269 dev_err(&adapter->dev, "Region 0x%x already in use!\n", addr);
270 return -EBUSY;
271 }
272
273 if (!(data = kmalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
274 err = -ENOMEM;
275 goto error_release;
276 }
277 memset(data, 0x00, sizeof(struct smsc47b397_data));
278
279 new_client = &data->client;
280 i2c_set_clientdata(new_client, data);
281 new_client->addr = addr;
282 init_MUTEX(&data->lock);
283 new_client->adapter = adapter;
284 new_client->driver = &smsc47b397_driver;
285 new_client->flags = 0;
286
287 strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
288
289 init_MUTEX(&data->update_lock);
290
291 if ((err = i2c_attach_client(new_client)))
292 goto error_free;
293
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400294 data->class_dev = hwmon_device_register(&new_client->dev);
295 if (IS_ERR(data->class_dev)) {
296 err = PTR_ERR(data->class_dev);
297 goto error_detach;
298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 device_create_file_temp(new_client, 1);
301 device_create_file_temp(new_client, 2);
302 device_create_file_temp(new_client, 3);
303 device_create_file_temp(new_client, 4);
304
305 device_create_file_fan(new_client, 1);
306 device_create_file_fan(new_client, 2);
307 device_create_file_fan(new_client, 3);
308 device_create_file_fan(new_client, 4);
309
310 return 0;
311
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400312error_detach:
313 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314error_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400315 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316error_release:
317 release_region(addr, SMSC_EXTENT);
318 return err;
319}
320
321static int __init smsc47b397_find(unsigned int *addr)
322{
323 u8 id, rev;
324
325 superio_enter();
326 id = superio_inb(SUPERIO_REG_DEVID);
327
328 if (id != 0x6f) {
329 superio_exit();
330 return -ENODEV;
331 }
332
333 rev = superio_inb(SUPERIO_REG_DEVREV);
334
335 superio_select(SUPERIO_REG_LD8);
336 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
337 | superio_inb(SUPERIO_REG_BASE_LSB);
338
339 printk(KERN_INFO "smsc47b397: found SMSC LPC47B397-NC "
340 "(base address 0x%04x, revision %u)\n", *addr, rev);
341
342 superio_exit();
343 return 0;
344}
345
346static int __init smsc47b397_init(void)
347{
348 int ret;
349
350 if ((ret = smsc47b397_find(normal_isa)))
351 return ret;
352
353 return i2c_add_driver(&smsc47b397_driver);
354}
355
356static void __exit smsc47b397_exit(void)
357{
358 i2c_del_driver(&smsc47b397_driver);
359}
360
361MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
362MODULE_DESCRIPTION("SMSC LPC47B397 driver");
363MODULE_LICENSE("GPL");
364
365module_init(smsc47b397_init);
366module_exit(smsc47b397_exit);