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