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-isa.h>
35 #include <linux/i2c-sensor.h>
36 #include <linux/hwmon.h>
37 #include <linux/err.h>
38 #include <linux/init.h>
41 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
42 /* Address is autodetected, there is no default value */
43 static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
44 static struct i2c_force_data forces[] = {{NULL}};
46 enum chips { any_chip, smsc47b397 };
47 static 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 */
55 /* Super-I/0 registers and commands */
57 #define REG 0x2e /* The register to read/write */
58 #define VAL 0x2f /* The value to read/write */
60 static inline void superio_outb(int reg, int val)
66 static inline int superio_inb(int reg)
72 /* select superio logical device */
73 static inline void superio_select(int ld)
75 superio_outb(0x07, ld);
78 static inline void superio_enter(void)
83 static inline void superio_exit(void)
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
94 #define SMSC_EXTENT 0x02
97 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
98 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
101 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
102 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
104 struct smsc47b397_data {
105 struct i2c_client client;
106 struct class_device *class_dev;
107 struct semaphore lock;
109 struct semaphore update_lock;
110 unsigned long last_updated; /* in jiffies */
113 /* register values */
118 static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
120 struct smsc47b397_data *data = i2c_get_clientdata(client);
124 outb(reg, client->addr);
125 res = inb_p(client->addr + 1);
130 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
132 struct i2c_client *client = to_i2c_client(dev);
133 struct smsc47b397_data *data = i2c_get_clientdata(client);
136 down(&data->update_lock);
138 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
139 dev_dbg(&client->dev, "starting device update...\n");
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));
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;
153 data->last_updated = jiffies;
156 dev_dbg(&client->dev, "... device update complete\n");
159 up(&data->update_lock);
164 /* TEMP: 0.001C/bit (-128C to +127C)
165 REG: 1C/bit, two's complement */
166 static int temp_from_reg(u8 reg)
168 return (s8)reg * 1000;
172 static ssize_t show_temp(struct device *dev, char *buf, int nr)
174 struct smsc47b397_data *data = smsc47b397_update_device(dev);
175 return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
178 #define sysfs_temp(num) \
179 static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
181 return show_temp(dev, buf, num-1); \
183 static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
190 #define device_create_file_temp(client, num) \
191 device_create_file(&client->dev, &dev_attr_temp##num##_input)
194 REG: count of 90kHz pulses / revolution */
195 static int fan_from_reg(u16 reg)
197 return 90000 * 60 / reg;
201 static ssize_t show_fan(struct device *dev, char *buf, int nr)
203 struct smsc47b397_data *data = smsc47b397_update_device(dev);
204 return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
207 #define sysfs_fan(num) \
208 static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
210 return show_fan(dev, buf, num-1); \
212 static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
219 #define device_create_file_fan(client, num) \
220 device_create_file(&client->dev, &dev_attr_fan##num##_input)
222 static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind);
224 static int smsc47b397_attach_adapter(struct i2c_adapter *adapter)
226 if (!(adapter->class & I2C_CLASS_HWMON))
228 return i2c_detect(adapter, &addr_data, smsc47b397_detect);
231 static int smsc47b397_detach_client(struct i2c_client *client)
233 struct smsc47b397_data *data = i2c_get_clientdata(client);
236 hwmon_device_unregister(data->class_dev);
238 if ((err = i2c_detach_client(client))) {
239 dev_err(&client->dev, "Client deregistration failed, "
240 "client not detached.\n");
244 release_region(client->addr, SMSC_EXTENT);
250 static 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,
259 static int smsc47b397_detect(struct i2c_adapter *adapter, int addr, int kind)
261 struct i2c_client *new_client;
262 struct smsc47b397_data *data;
265 if (!i2c_is_isa_adapter(adapter)) {
269 if (!request_region(addr, SMSC_EXTENT, smsc47b397_driver.name)) {
270 dev_err(&adapter->dev, "Region 0x%x already in use!\n", addr);
274 if (!(data = kmalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
278 memset(data, 0x00, sizeof(struct smsc47b397_data));
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;
288 strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
290 init_MUTEX(&data->update_lock);
292 if ((err = i2c_attach_client(new_client)))
295 data->class_dev = hwmon_device_register(&new_client->dev);
296 if (IS_ERR(data->class_dev)) {
297 err = PTR_ERR(data->class_dev);
301 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);
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);
314 i2c_detach_client(new_client);
318 release_region(addr, SMSC_EXTENT);
322 static int __init smsc47b397_find(unsigned int *addr)
327 id = superio_inb(SUPERIO_REG_DEVID);
334 rev = superio_inb(SUPERIO_REG_DEVREV);
336 superio_select(SUPERIO_REG_LD8);
337 *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
338 | superio_inb(SUPERIO_REG_BASE_LSB);
340 printk(KERN_INFO "smsc47b397: found SMSC LPC47B397-NC "
341 "(base address 0x%04x, revision %u)\n", *addr, rev);
347 static int __init smsc47b397_init(void)
351 if ((ret = smsc47b397_find(normal_isa)))
354 return i2c_isa_add_driver(&smsc47b397_driver);
357 static void __exit smsc47b397_exit(void)
359 i2c_isa_del_driver(&smsc47b397_driver);
362 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
363 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
364 MODULE_LICENSE("GPL");
366 module_init(smsc47b397_init);
367 module_exit(smsc47b397_exit);