Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / drivers / hwmon / 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/jiffies.h>
33 #include <linux/platform_device.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/err.h>
37 #include <linux/init.h>
38 #include <linux/mutex.h>
39 #include <asm/io.h>
40
41 static struct platform_device *pdev;
42
43 #define DRVNAME "smsc47b397"
44
45 /* Super-I/0 registers and commands */
46
47 #define REG     0x2e    /* The register to read/write */
48 #define VAL     0x2f    /* The value to read/write */
49
50 static inline void superio_outb(int reg, int val)
51 {
52         outb(reg, REG);
53         outb(val, VAL);
54 }
55
56 static inline int superio_inb(int reg)
57 {
58         outb(reg, REG);
59         return inb(VAL);
60 }
61
62 /* select superio logical device */
63 static inline void superio_select(int ld)
64 {
65         superio_outb(0x07, ld);
66 }
67
68 static inline void superio_enter(void)
69 {
70         outb(0x55, REG);
71 }
72
73 static inline void superio_exit(void)
74 {
75         outb(0xAA, REG);
76 }
77
78 #define SUPERIO_REG_DEVID       0x20
79 #define SUPERIO_REG_DEVREV      0x21
80 #define SUPERIO_REG_BASE_MSB    0x60
81 #define SUPERIO_REG_BASE_LSB    0x61
82 #define SUPERIO_REG_LD8         0x08
83
84 #define SMSC_EXTENT             0x02
85
86 /* 0 <= nr <= 3 */
87 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
88 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
89
90 /* 0 <= nr <= 3 */
91 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
92 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
93
94 struct smsc47b397_data {
95         unsigned short addr;
96         const char *name;
97         struct class_device *class_dev;
98         struct mutex lock;
99
100         struct mutex update_lock;
101         unsigned long last_updated; /* in jiffies */
102         int valid;
103
104         /* register values */
105         u16 fan[4];
106         u8 temp[4];
107 };
108
109 static int smsc47b397_read_value(struct smsc47b397_data* data, u8 reg)
110 {
111         int res;
112
113         mutex_lock(&data->lock);
114         outb(reg, data->addr);
115         res = inb_p(data->addr + 1);
116         mutex_unlock(&data->lock);
117         return res;
118 }
119
120 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
121 {
122         struct smsc47b397_data *data = dev_get_drvdata(dev);
123         int i;
124
125         mutex_lock(&data->update_lock);
126
127         if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
128                 dev_dbg(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(data,
133                                         SMSC47B397_REG_TEMP(i));
134
135                         /* must read LSB first */
136                         data->fan[i]  = smsc47b397_read_value(data,
137                                         SMSC47B397_REG_FAN_LSB(i));
138                         data->fan[i] |= smsc47b397_read_value(data,
139                                         SMSC47B397_REG_FAN_MSB(i)) << 8;
140                 }
141
142                 data->last_updated = jiffies;
143                 data->valid = 1;
144
145                 dev_dbg(dev, "... device update complete\n");
146         }
147
148         mutex_unlock(&data->update_lock);
149
150         return data;
151 }
152
153 /* TEMP: 0.001C/bit (-128C to +127C)
154    REG: 1C/bit, two's complement */
155 static int temp_from_reg(u8 reg)
156 {
157         return (s8)reg * 1000;
158 }
159
160 static ssize_t show_temp(struct device *dev, struct device_attribute
161                          *devattr, char *buf)
162 {
163         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
164         struct smsc47b397_data *data = smsc47b397_update_device(dev);
165         return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
166 }
167
168 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
169 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
170 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
171 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
172
173 /* FAN: 1 RPM/bit
174    REG: count of 90kHz pulses / revolution */
175 static int fan_from_reg(u16 reg)
176 {
177         return 90000 * 60 / reg;
178 }
179
180 static ssize_t show_fan(struct device *dev, struct device_attribute
181                         *devattr, char *buf)
182 {
183         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
184         struct smsc47b397_data *data = smsc47b397_update_device(dev);
185         return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
186 }
187 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
188 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
189 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
190 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
191
192 static ssize_t show_name(struct device *dev, struct device_attribute
193                          *devattr, char *buf)
194 {
195         struct smsc47b397_data *data = dev_get_drvdata(dev);
196         return sprintf(buf, "%s\n", data->name);
197 }
198 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
199
200 static struct attribute *smsc47b397_attributes[] = {
201         &sensor_dev_attr_temp1_input.dev_attr.attr,
202         &sensor_dev_attr_temp2_input.dev_attr.attr,
203         &sensor_dev_attr_temp3_input.dev_attr.attr,
204         &sensor_dev_attr_temp4_input.dev_attr.attr,
205         &sensor_dev_attr_fan1_input.dev_attr.attr,
206         &sensor_dev_attr_fan2_input.dev_attr.attr,
207         &sensor_dev_attr_fan3_input.dev_attr.attr,
208         &sensor_dev_attr_fan4_input.dev_attr.attr,
209
210         &dev_attr_name.attr,
211         NULL
212 };
213
214 static const struct attribute_group smsc47b397_group = {
215         .attrs = smsc47b397_attributes,
216 };
217
218 static int __devexit smsc47b397_remove(struct platform_device *pdev)
219 {
220         struct smsc47b397_data *data = platform_get_drvdata(pdev);
221         struct resource *res;
222
223         hwmon_device_unregister(data->class_dev);
224         sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
225         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
226         release_region(res->start, SMSC_EXTENT);
227         kfree(data);
228
229         return 0;
230 }
231
232 static int smsc47b397_probe(struct platform_device *pdev);
233
234 static struct platform_driver smsc47b397_driver = {
235         .driver = {
236                 .owner  = THIS_MODULE,
237                 .name   = DRVNAME,
238         },
239         .probe          = smsc47b397_probe,
240         .remove         = __devexit_p(smsc47b397_remove),
241 };
242
243 static int __devinit smsc47b397_probe(struct platform_device *pdev)
244 {
245         struct device *dev = &pdev->dev;
246         struct smsc47b397_data *data;
247         struct resource *res;
248         int err = 0;
249
250         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
251         if (!request_region(res->start, SMSC_EXTENT,
252                             smsc47b397_driver.driver.name)) {
253                 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
254                         (unsigned long)res->start,
255                         (unsigned long)res->start + SMSC_EXTENT - 1);
256                 return -EBUSY;
257         }
258
259         if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
260                 err = -ENOMEM;
261                 goto error_release;
262         }
263
264         data->addr = res->start;
265         data->name = "smsc47b397";
266         mutex_init(&data->lock);
267         mutex_init(&data->update_lock);
268         platform_set_drvdata(pdev, data);
269
270         if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group)))
271                 goto error_free;
272
273         data->class_dev = hwmon_device_register(dev);
274         if (IS_ERR(data->class_dev)) {
275                 err = PTR_ERR(data->class_dev);
276                 goto error_remove;
277         }
278
279         return 0;
280
281 error_remove:
282         sysfs_remove_group(&dev->kobj, &smsc47b397_group);
283 error_free:
284         kfree(data);
285 error_release:
286         release_region(res->start, SMSC_EXTENT);
287         return err;
288 }
289
290 static int __init smsc47b397_device_add(unsigned short address)
291 {
292         struct resource res = {
293                 .start  = address,
294                 .end    = address + SMSC_EXTENT - 1,
295                 .name   = DRVNAME,
296                 .flags  = IORESOURCE_IO,
297         };
298         int err;
299
300         pdev = platform_device_alloc(DRVNAME, address);
301         if (!pdev) {
302                 err = -ENOMEM;
303                 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
304                 goto exit;
305         }
306
307         err = platform_device_add_resources(pdev, &res, 1);
308         if (err) {
309                 printk(KERN_ERR DRVNAME ": Device resource addition failed "
310                        "(%d)\n", err);
311                 goto exit_device_put;
312         }
313
314         err = platform_device_add(pdev);
315         if (err) {
316                 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
317                        err);
318                 goto exit_device_put;
319         }
320
321         return 0;
322
323 exit_device_put:
324         platform_device_put(pdev);
325 exit:
326         return err;
327 }
328
329 static int __init smsc47b397_find(unsigned short *addr)
330 {
331         u8 id, rev;
332
333         superio_enter();
334         id = superio_inb(SUPERIO_REG_DEVID);
335
336         if ((id != 0x6f) && (id != 0x81)) {
337                 superio_exit();
338                 return -ENODEV;
339         }
340
341         rev = superio_inb(SUPERIO_REG_DEVREV);
342
343         superio_select(SUPERIO_REG_LD8);
344         *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
345                  |  superio_inb(SUPERIO_REG_BASE_LSB);
346
347         printk(KERN_INFO DRVNAME ": found SMSC %s "
348                 "(base address 0x%04x, revision %u)\n",
349                 id == 0x81 ? "SCH5307-NS" : "LPC47B397-NC", *addr, rev);
350
351         superio_exit();
352         return 0;
353 }
354
355 static int __init smsc47b397_init(void)
356 {
357         unsigned short address;
358         int ret;
359
360         if ((ret = smsc47b397_find(&address)))
361                 return ret;
362
363         ret = platform_driver_register(&smsc47b397_driver);
364         if (ret)
365                 goto exit;
366
367         /* Sets global pdev as a side effect */
368         ret = smsc47b397_device_add(address);
369         if (ret)
370                 goto exit_driver;
371
372         return 0;
373
374 exit_driver:
375         platform_driver_unregister(&smsc47b397_driver);
376 exit:
377         return ret;
378 }
379
380 static void __exit smsc47b397_exit(void)
381 {
382         platform_device_unregister(pdev);
383         platform_driver_unregister(&smsc47b397_driver);
384 }
385
386 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
387 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
388 MODULE_LICENSE("GPL");
389
390 module_init(smsc47b397_init);
391 module_exit(smsc47b397_exit);