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