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