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> |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 34 | #include <linux/hwmon.h> |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 35 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 36 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/init.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/io.h> |
| 40 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 41 | static struct platform_device *pdev; |
| 42 | |
| 43 | #define DRVNAME "smsc47b397" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 95 | unsigned short addr; |
| 96 | const char *name; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 97 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 98 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 100 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | unsigned long last_updated; /* in jiffies */ |
| 102 | int valid; |
| 103 | |
| 104 | /* register values */ |
| 105 | u16 fan[4]; |
| 106 | u8 temp[4]; |
| 107 | }; |
| 108 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 109 | static int smsc47b397_read_value(struct smsc47b397_data* data, u8 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | int res; |
| 112 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 113 | mutex_lock(&data->lock); |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 114 | outb(reg, data->addr); |
| 115 | res = inb_p(data->addr + 1); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 116 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return res; |
| 118 | } |
| 119 | |
| 120 | static struct smsc47b397_data *smsc47b397_update_device(struct device *dev) |
| 121 | { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 122 | struct smsc47b397_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | int i; |
| 124 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 125 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 128 | dev_dbg(dev, "starting device update...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | /* 4 temperature inputs, 4 fan inputs */ |
| 131 | for (i = 0; i < 4; i++) { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 132 | data->temp[i] = smsc47b397_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | SMSC47B397_REG_TEMP(i)); |
| 134 | |
| 135 | /* must read LSB first */ |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 136 | data->fan[i] = smsc47b397_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | SMSC47B397_REG_FAN_LSB(i)); |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 138 | data->fan[i] |= smsc47b397_read_value(data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | SMSC47B397_REG_FAN_MSB(i)) << 8; |
| 140 | } |
| 141 | |
| 142 | data->last_updated = jiffies; |
| 143 | data->valid = 1; |
| 144 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 145 | dev_dbg(dev, "... device update complete\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 148 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 160 | static ssize_t show_temp(struct device *dev, struct device_attribute |
| 161 | *devattr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 163 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | struct smsc47b397_data *data = smsc47b397_update_device(dev); |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 165 | return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* FAN: 1 RPM/bit |
| 174 | REG: count of 90kHz pulses / revolution */ |
| 175 | static int fan_from_reg(u16 reg) |
| 176 | { |
Jean Delvare | 90205c6 | 2007-06-23 14:58:22 +0200 | [diff] [blame] | 177 | if (reg == 0 || reg == 0xffff) |
| 178 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return 90000 * 60 / reg; |
| 180 | } |
| 181 | |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 182 | static ssize_t show_fan(struct device *dev, struct device_attribute |
| 183 | *devattr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 185 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 186 | struct smsc47b397_data *data = smsc47b397_update_device(dev); |
| 187 | return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 189 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
| 190 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); |
| 191 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2); |
| 192 | static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 194 | static ssize_t show_name(struct device *dev, struct device_attribute |
| 195 | *devattr, char *buf) |
| 196 | { |
| 197 | struct smsc47b397_data *data = dev_get_drvdata(dev); |
| 198 | return sprintf(buf, "%s\n", data->name); |
| 199 | } |
| 200 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 201 | |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 202 | static struct attribute *smsc47b397_attributes[] = { |
Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 203 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 204 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 205 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 206 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 207 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 208 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 209 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 210 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 211 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 212 | &dev_attr_name.attr, |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 213 | NULL |
| 214 | }; |
| 215 | |
| 216 | static const struct attribute_group smsc47b397_group = { |
| 217 | .attrs = smsc47b397_attributes, |
| 218 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 220 | static int __devexit smsc47b397_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 222 | struct smsc47b397_data *data = platform_get_drvdata(pdev); |
| 223 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 225 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 226 | sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group); |
| 227 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 228 | release_region(res->start, SMSC_EXTENT); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 229 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 234 | static int smsc47b397_probe(struct platform_device *pdev); |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 235 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 236 | static struct platform_driver smsc47b397_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 237 | .driver = { |
Jean Delvare | 8721884 | 2006-09-03 22:36:14 +0200 | [diff] [blame] | 238 | .owner = THIS_MODULE, |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 239 | .name = DRVNAME, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 240 | }, |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 241 | .probe = smsc47b397_probe, |
| 242 | .remove = __devexit_p(smsc47b397_remove), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 245 | static int __devinit smsc47b397_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 247 | struct device *dev = &pdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | struct smsc47b397_data *data; |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 249 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | int err = 0; |
| 251 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 252 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 253 | if (!request_region(res->start, SMSC_EXTENT, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 254 | smsc47b397_driver.driver.name)) { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 255 | dev_err(dev, "Region 0x%lx-0x%lx already in use!\n", |
| 256 | (unsigned long)res->start, |
| 257 | (unsigned long)res->start + SMSC_EXTENT - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | return -EBUSY; |
| 259 | } |
| 260 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 261 | if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | err = -ENOMEM; |
| 263 | goto error_release; |
| 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 266 | data->addr = res->start; |
| 267 | data->name = "smsc47b397"; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 268 | mutex_init(&data->lock); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 269 | mutex_init(&data->update_lock); |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 270 | platform_set_drvdata(pdev, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 272 | if ((err = sysfs_create_group(&dev->kobj, &smsc47b397_group))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | goto error_free; |
| 274 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 275 | data->hwmon_dev = hwmon_device_register(dev); |
| 276 | if (IS_ERR(data->hwmon_dev)) { |
| 277 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 278 | goto error_remove; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return 0; |
| 282 | |
Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 283 | error_remove: |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 284 | sysfs_remove_group(&dev->kobj, &smsc47b397_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | error_free: |
Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 286 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | error_release: |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 288 | release_region(res->start, SMSC_EXTENT); |
| 289 | return err; |
| 290 | } |
| 291 | |
| 292 | static int __init smsc47b397_device_add(unsigned short address) |
| 293 | { |
| 294 | struct resource res = { |
| 295 | .start = address, |
| 296 | .end = address + SMSC_EXTENT - 1, |
| 297 | .name = DRVNAME, |
| 298 | .flags = IORESOURCE_IO, |
| 299 | }; |
| 300 | int err; |
| 301 | |
| 302 | pdev = platform_device_alloc(DRVNAME, address); |
| 303 | if (!pdev) { |
| 304 | err = -ENOMEM; |
| 305 | printk(KERN_ERR DRVNAME ": Device allocation failed\n"); |
| 306 | goto exit; |
| 307 | } |
| 308 | |
| 309 | err = platform_device_add_resources(pdev, &res, 1); |
| 310 | if (err) { |
| 311 | printk(KERN_ERR DRVNAME ": Device resource addition failed " |
| 312 | "(%d)\n", err); |
| 313 | goto exit_device_put; |
| 314 | } |
| 315 | |
| 316 | err = platform_device_add(pdev); |
| 317 | if (err) { |
| 318 | printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", |
| 319 | err); |
| 320 | goto exit_device_put; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | |
| 325 | exit_device_put: |
| 326 | platform_device_put(pdev); |
| 327 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return err; |
| 329 | } |
| 330 | |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 331 | static int __init smsc47b397_find(unsigned short *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | u8 id, rev; |
| 334 | |
| 335 | superio_enter(); |
| 336 | id = superio_inb(SUPERIO_REG_DEVID); |
| 337 | |
Juerg Haefliger | 2dbbdb3 | 2007-06-20 15:41:33 -0700 | [diff] [blame] | 338 | if ((id != 0x6f) && (id != 0x81) && (id != 0x85)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | superio_exit(); |
| 340 | return -ENODEV; |
| 341 | } |
| 342 | |
| 343 | rev = superio_inb(SUPERIO_REG_DEVREV); |
| 344 | |
| 345 | superio_select(SUPERIO_REG_LD8); |
| 346 | *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8) |
| 347 | | superio_inb(SUPERIO_REG_BASE_LSB); |
| 348 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 349 | printk(KERN_INFO DRVNAME ": found SMSC %s " |
Mark M. Hoffman | 7ab83a9 | 2005-10-17 23:01:45 +0200 | [diff] [blame] | 350 | "(base address 0x%04x, revision %u)\n", |
Juerg Haefliger | 2dbbdb3 | 2007-06-20 15:41:33 -0700 | [diff] [blame] | 351 | id == 0x81 ? "SCH5307-NS" : id == 0x85 ? "SCH5317" : |
| 352 | "LPC47B397-NC", *addr, rev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | superio_exit(); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int __init smsc47b397_init(void) |
| 359 | { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 360 | unsigned short address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | int ret; |
| 362 | |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 363 | if ((ret = smsc47b397_find(&address))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | return ret; |
| 365 | |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 366 | ret = platform_driver_register(&smsc47b397_driver); |
| 367 | if (ret) |
| 368 | goto exit; |
| 369 | |
| 370 | /* Sets global pdev as a side effect */ |
| 371 | ret = smsc47b397_device_add(address); |
| 372 | if (ret) |
| 373 | goto exit_driver; |
| 374 | |
| 375 | return 0; |
| 376 | |
| 377 | exit_driver: |
| 378 | platform_driver_unregister(&smsc47b397_driver); |
| 379 | exit: |
| 380 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static void __exit smsc47b397_exit(void) |
| 384 | { |
Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 385 | platform_device_unregister(pdev); |
| 386 | platform_driver_unregister(&smsc47b397_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 390 | MODULE_DESCRIPTION("SMSC LPC47B397 driver"); |
| 391 | MODULE_LICENSE("GPL"); |
| 392 | |
| 393 | module_init(smsc47b397_init); |
| 394 | module_exit(smsc47b397_exit); |