revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / drivers / hwmon / smsc47b397.c
blob1295f01b2ced6a2468dec79c331d99adb30ae5c2
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/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 <linux/acpi.h>
40 #include <asm/io.h>
42 static struct platform_device *pdev;
44 #define DRVNAME "smsc47b397"
46 /* Super-I/0 registers and commands */
48 #define REG 0x2e /* The register to read/write */
49 #define VAL 0x2f /* The value to read/write */
51 static inline void superio_outb(int reg, int val)
53 outb(reg, REG);
54 outb(val, VAL);
57 static inline int superio_inb(int reg)
59 outb(reg, REG);
60 return inb(VAL);
63 /* select superio logical device */
64 static inline void superio_select(int ld)
66 superio_outb(0x07, ld);
69 static inline void superio_enter(void)
71 outb(0x55, REG);
74 static inline void superio_exit(void)
76 outb(0xAA, REG);
79 #define SUPERIO_REG_DEVID 0x20
80 #define SUPERIO_REG_DEVREV 0x21
81 #define SUPERIO_REG_BASE_MSB 0x60
82 #define SUPERIO_REG_BASE_LSB 0x61
83 #define SUPERIO_REG_LD8 0x08
85 #define SMSC_EXTENT 0x02
87 /* 0 <= nr <= 3 */
88 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
89 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
91 /* 0 <= nr <= 3 */
92 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
93 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
95 struct smsc47b397_data {
96 unsigned short addr;
97 const char *name;
98 struct device *hwmon_dev;
99 struct mutex lock;
101 struct mutex update_lock;
102 unsigned long last_updated; /* in jiffies */
103 int valid;
105 /* register values */
106 u16 fan[4];
107 u8 temp[4];
110 static int smsc47b397_read_value(struct smsc47b397_data* data, u8 reg)
112 int res;
114 mutex_lock(&data->lock);
115 outb(reg, data->addr);
116 res = inb_p(data->addr + 1);
117 mutex_unlock(&data->lock);
118 return res;
121 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
123 struct smsc47b397_data *data = dev_get_drvdata(dev);
124 int i;
126 mutex_lock(&data->update_lock);
128 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
129 dev_dbg(dev, "starting device update...\n");
131 /* 4 temperature inputs, 4 fan inputs */
132 for (i = 0; i < 4; i++) {
133 data->temp[i] = smsc47b397_read_value(data,
134 SMSC47B397_REG_TEMP(i));
136 /* must read LSB first */
137 data->fan[i] = smsc47b397_read_value(data,
138 SMSC47B397_REG_FAN_LSB(i));
139 data->fan[i] |= smsc47b397_read_value(data,
140 SMSC47B397_REG_FAN_MSB(i)) << 8;
143 data->last_updated = jiffies;
144 data->valid = 1;
146 dev_dbg(dev, "... device update complete\n");
149 mutex_unlock(&data->update_lock);
151 return data;
154 /* TEMP: 0.001C/bit (-128C to +127C)
155 REG: 1C/bit, two's complement */
156 static int temp_from_reg(u8 reg)
158 return (s8)reg * 1000;
161 static ssize_t show_temp(struct device *dev, struct device_attribute
162 *devattr, char *buf)
164 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
165 struct smsc47b397_data *data = smsc47b397_update_device(dev);
166 return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
169 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
170 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
171 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
172 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
174 /* FAN: 1 RPM/bit
175 REG: count of 90kHz pulses / revolution */
176 static int fan_from_reg(u16 reg)
178 if (reg == 0 || reg == 0xffff)
179 return 0;
180 return 90000 * 60 / reg;
183 static ssize_t show_fan(struct device *dev, struct device_attribute
184 *devattr, char *buf)
186 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
187 struct smsc47b397_data *data = smsc47b397_update_device(dev);
188 return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index]));
190 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
191 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
192 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
193 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
195 static ssize_t show_name(struct device *dev, struct device_attribute
196 *devattr, char *buf)
198 struct smsc47b397_data *data = dev_get_drvdata(dev);
199 return sprintf(buf, "%s\n", data->name);
201 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
203 static struct attribute *smsc47b397_attributes[] = {
204 &sensor_dev_attr_temp1_input.dev_attr.attr,
205 &sensor_dev_attr_temp2_input.dev_attr.attr,
206 &sensor_dev_attr_temp3_input.dev_attr.attr,
207 &sensor_dev_attr_temp4_input.dev_attr.attr,
208 &sensor_dev_attr_fan1_input.dev_attr.attr,
209 &sensor_dev_attr_fan2_input.dev_attr.attr,
210 &sensor_dev_attr_fan3_input.dev_attr.attr,
211 &sensor_dev_attr_fan4_input.dev_attr.attr,
213 &dev_attr_name.attr,
214 NULL
217 static const struct attribute_group smsc47b397_group = {
218 .attrs = smsc47b397_attributes,
221 static int __devexit smsc47b397_remove(struct platform_device *pdev)
223 struct smsc47b397_data *data = platform_get_drvdata(pdev);
224 struct resource *res;
226 hwmon_device_unregister(data->hwmon_dev);
227 sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group);
228 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
229 release_region(res->start, SMSC_EXTENT);
230 kfree(data);
232 return 0;
235 static int smsc47b397_probe(struct platform_device *pdev);
237 static struct platform_driver smsc47b397_driver = {
238 .driver = {
239 .owner = THIS_MODULE,
240 .name = DRVNAME,
242 .probe = smsc47b397_probe,
243 .remove = __devexit_p(smsc47b397_remove),
246 static int __devinit smsc47b397_probe(struct platform_device *pdev)
248 struct device *dev = &pdev->dev;
249 struct smsc47b397_data *data;
250 struct resource *res;
251 int err = 0;
253 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
254 if (!request_region(res->start, SMSC_EXTENT,
255 smsc47b397_driver.driver.name)) {
256 dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
257 (unsigned long)res->start,
258 (unsigned long)res->start + SMSC_EXTENT - 1);
259 return -EBUSY;
262 if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
263 err = -ENOMEM;
264 goto error_release;
267 data->addr = res->start;
268 data->name = "smsc47b397";
269 mutex_init(&data->lock);
270 mutex_init(&data->update_lock);
271 platform_set_drvdata(pdev, data);
273 if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group)))
274 goto error_free;
276 data->hwmon_dev = hwmon_device_register(dev);
277 if (IS_ERR(data->hwmon_dev)) {
278 err = PTR_ERR(data->hwmon_dev);
279 goto error_remove;
282 return 0;
284 error_remove:
285 sysfs_remove_group(&dev->kobj, &smsc47b397_group);
286 error_free:
287 kfree(data);
288 error_release:
289 release_region(res->start, SMSC_EXTENT);
290 return err;
293 static int __init smsc47b397_device_add(unsigned short address)
295 struct resource res = {
296 .start = address,
297 .end = address + SMSC_EXTENT - 1,
298 .name = DRVNAME,
299 .flags = IORESOURCE_IO,
301 int err;
303 err = acpi_check_resource_conflict(&res);
304 if (err)
305 goto exit;
307 pdev = platform_device_alloc(DRVNAME, address);
308 if (!pdev) {
309 err = -ENOMEM;
310 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
311 goto exit;
314 err = platform_device_add_resources(pdev, &res, 1);
315 if (err) {
316 printk(KERN_ERR DRVNAME ": Device resource addition failed "
317 "(%d)\n", err);
318 goto exit_device_put;
321 err = platform_device_add(pdev);
322 if (err) {
323 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
324 err);
325 goto exit_device_put;
328 return 0;
330 exit_device_put:
331 platform_device_put(pdev);
332 exit:
333 return err;
336 static int __init smsc47b397_find(unsigned short *addr)
338 u8 id, rev;
340 superio_enter();
341 id = superio_inb(SUPERIO_REG_DEVID);
343 if ((id != 0x6f) && (id != 0x81) && (id != 0x85)) {
344 superio_exit();
345 return -ENODEV;
348 rev = superio_inb(SUPERIO_REG_DEVREV);
350 superio_select(SUPERIO_REG_LD8);
351 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
352 | superio_inb(SUPERIO_REG_BASE_LSB);
354 printk(KERN_INFO DRVNAME ": found SMSC %s "
355 "(base address 0x%04x, revision %u)\n",
356 id == 0x81 ? "SCH5307-NS" : id == 0x85 ? "SCH5317" :
357 "LPC47B397-NC", *addr, rev);
359 superio_exit();
360 return 0;
363 static int __init smsc47b397_init(void)
365 unsigned short address;
366 int ret;
368 if ((ret = smsc47b397_find(&address)))
369 return ret;
371 ret = platform_driver_register(&smsc47b397_driver);
372 if (ret)
373 goto exit;
375 /* Sets global pdev as a side effect */
376 ret = smsc47b397_device_add(address);
377 if (ret)
378 goto exit_driver;
380 return 0;
382 exit_driver:
383 platform_driver_unregister(&smsc47b397_driver);
384 exit:
385 return ret;
388 static void __exit smsc47b397_exit(void)
390 platform_device_unregister(pdev);
391 platform_driver_unregister(&smsc47b397_driver);
394 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
395 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
396 MODULE_LICENSE("GPL");
398 module_init(smsc47b397_init);
399 module_exit(smsc47b397_exit);