Linux v2.6.13
[linux-2.6/next.git] / drivers / hwmon / smsc47b397.c
blobfdeeb3ab6f2f3e9146b607ddc00c5d3ffd239bed
1 /*
2 smsc47b397.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
5 Supports the SMSC LPC47B397-NC Super-I/O chip.
7 Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
8 Copyright (C) 2004 Utilitek Systems, Inc.
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>
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.
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.
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.
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>
38 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
39 /* Address is autodetected, there is no default value */
40 static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
41 static struct i2c_force_data forces[] = {{NULL}};
43 enum chips { any_chip, smsc47b397 };
44 static 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,
52 /* Super-I/0 registers and commands */
54 #define REG 0x2e /* The register to read/write */
55 #define VAL 0x2f /* The value to read/write */
57 static inline void superio_outb(int reg, int val)
59 outb(reg, REG);
60 outb(val, VAL);
63 static inline int superio_inb(int reg)
65 outb(reg, REG);
66 return inb(VAL);
69 /* select superio logical device */
70 static inline void superio_select(int ld)
72 superio_outb(0x07, ld);
75 static inline void superio_enter(void)
77 outb(0x55, REG);
80 static inline void superio_exit(void)
82 outb(0xAA, REG);
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
91 #define SMSC_EXTENT 0x02
93 /* 0 <= nr <= 3 */
94 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
95 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
97 /* 0 <= nr <= 3 */
98 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
99 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
101 struct smsc47b397_data {
102 struct i2c_client client;
103 struct semaphore lock;
105 struct semaphore update_lock;
106 unsigned long last_updated; /* in jiffies */
107 int valid;
109 /* register values */
110 u16 fan[4];
111 u8 temp[4];
114 static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
116 struct smsc47b397_data *data = i2c_get_clientdata(client);
117 int res;
119 down(&data->lock);
120 outb(reg, client->addr);
121 res = inb_p(client->addr + 1);
122 up(&data->lock);
123 return res;
126 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
128 struct i2c_client *client = to_i2c_client(dev);
129 struct smsc47b397_data *data = i2c_get_clientdata(client);
130 int i;
132 down(&data->update_lock);
134 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
135 dev_dbg(&client->dev, "starting device update...\n");
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));
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;
149 data->last_updated = jiffies;
150 data->valid = 1;
152 dev_dbg(&client->dev, "... device update complete\n");
155 up(&data->update_lock);
157 return data;
160 /* TEMP: 0.001C/bit (-128C to +127C)
161 REG: 1C/bit, two's complement */
162 static int temp_from_reg(u8 reg)
164 return (s8)reg * 1000;
167 /* 0 <= nr <= 3 */
168 static ssize_t show_temp(struct device *dev, char *buf, int nr)
170 struct smsc47b397_data *data = smsc47b397_update_device(dev);
171 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
174 #define sysfs_temp(num) \
175 static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
177 return show_temp(dev, buf, num-1); \
179 static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
181 sysfs_temp(1);
182 sysfs_temp(2);
183 sysfs_temp(3);
184 sysfs_temp(4);
186 #define device_create_file_temp(client, num) \
187 device_create_file(&client->dev, &dev_attr_temp##num##_input)
189 /* FAN: 1 RPM/bit
190 REG: count of 90kHz pulses / revolution */
191 static int fan_from_reg(u16 reg)
193 return 90000 * 60 / reg;
196 /* 0 <= nr <= 3 */
197 static ssize_t show_fan(struct device *dev, char *buf, int nr)
199 struct smsc47b397_data *data = smsc47b397_update_device(dev);
200 return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
203 #define sysfs_fan(num) \
204 static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
206 return show_fan(dev, buf, num-1); \
208 static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
210 sysfs_fan(1);
211 sysfs_fan(2);
212 sysfs_fan(3);
213 sysfs_fan(4);
215 #define device_create_file_fan(client, num) \
216 device_create_file(&client->dev, &dev_attr_fan##num##_input)
218 static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind);
220 static int smsc47b397_attach_adapter(struct i2c_adapter *adapter)
222 if (!(adapter->class & I2C_CLASS_HWMON))
223 return 0;
224 return i2c_detect(adapter, &addr_data, smsc47b397_detect);
227 static int smsc47b397_detach_client(struct i2c_client *client)
229 int err;
231 if ((err = i2c_detach_client(client))) {
232 dev_err(&client->dev, "Client deregistration failed, "
233 "client not detached.\n");
234 return err;
237 release_region(client->addr, SMSC_EXTENT);
238 kfree(i2c_get_clientdata(client));
240 return 0;
243 static 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,
252 static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind)
254 struct i2c_client *new_client;
255 struct smsc47b397_data *data;
256 int err = 0;
258 if (!i2c_is_isa_adapter(adapter)) {
259 return 0;
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;
267 if (!(data = kmalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
268 err = -ENOMEM;
269 goto error_release;
271 memset(data, 0x00, sizeof(struct smsc47b397_data));
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;
281 strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
283 init_MUTEX(&data->update_lock);
285 if ((err = i2c_attach_client(new_client)))
286 goto error_free;
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);
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);
298 return 0;
300 error_free:
301 kfree(data);
302 error_release:
303 release_region(addr, SMSC_EXTENT);
304 return err;
307 static int __init smsc47b397_find(unsigned int *addr)
309 u8 id, rev;
311 superio_enter();
312 id = superio_inb(SUPERIO_REG_DEVID);
314 if (id != 0x6f) {
315 superio_exit();
316 return -ENODEV;
319 rev = superio_inb(SUPERIO_REG_DEVREV);
321 superio_select(SUPERIO_REG_LD8);
322 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
323 | superio_inb(SUPERIO_REG_BASE_LSB);
325 printk(KERN_INFO "smsc47b397: found SMSC LPC47B397-NC "
326 "(base address 0x%04x, revision %u)\n", *addr, rev);
328 superio_exit();
329 return 0;
332 static int __init smsc47b397_init(void)
334 int ret;
336 if ((ret = smsc47b397_find(normal_isa)))
337 return ret;
339 return i2c_add_driver(&smsc47b397_driver);
342 static void __exit smsc47b397_exit(void)
344 i2c_del_driver(&smsc47b397_driver);
347 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
348 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
349 MODULE_LICENSE("GPL");
351 module_init(smsc47b397_init);
352 module_exit(smsc47b397_exit);