| 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 |  | 
| Joe Perches | bf1a85e | 2010-10-20 06:51:48 +0000 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
|  | 30 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/module.h> | 
|  | 32 | #include <linux/slab.h> | 
|  | 33 | #include <linux/ioport.h> | 
|  | 34 | #include <linux/jiffies.h> | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 35 | #include <linux/platform_device.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 36 | #include <linux/hwmon.h> | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 37 | #include <linux/hwmon-sysfs.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 38 | #include <linux/err.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/init.h> | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 40 | #include <linux/mutex.h> | 
| Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 41 | #include <linux/acpi.h> | 
| H Hartley Sweeten | 6055fae | 2009-09-15 17:18:13 +0200 | [diff] [blame] | 42 | #include <linux/io.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
| Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 44 | static unsigned short force_id; | 
|  | 45 | module_param(force_id, ushort, 0); | 
|  | 46 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); | 
|  | 47 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 48 | static struct platform_device *pdev; | 
|  | 49 |  | 
|  | 50 | #define DRVNAME "smsc47b397" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | /* Super-I/0 registers and commands */ | 
|  | 53 |  | 
|  | 54 | #define	REG	0x2e	/* The register to read/write */ | 
|  | 55 | #define	VAL	0x2f	/* The value to read/write */ | 
|  | 56 |  | 
|  | 57 | static inline void superio_outb(int reg, int val) | 
|  | 58 | { | 
|  | 59 | outb(reg, REG); | 
|  | 60 | outb(val, VAL); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | static inline int superio_inb(int reg) | 
|  | 64 | { | 
|  | 65 | outb(reg, REG); | 
|  | 66 | return inb(VAL); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | /* select superio logical device */ | 
|  | 70 | static inline void superio_select(int ld) | 
|  | 71 | { | 
|  | 72 | superio_outb(0x07, ld); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | static inline void superio_enter(void) | 
|  | 76 | { | 
|  | 77 | outb(0x55, REG); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | static inline void superio_exit(void) | 
|  | 81 | { | 
|  | 82 | outb(0xAA, REG); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | #define SUPERIO_REG_DEVID	0x20 | 
|  | 86 | #define SUPERIO_REG_DEVREV	0x21 | 
|  | 87 | #define SUPERIO_REG_BASE_MSB	0x60 | 
|  | 88 | #define SUPERIO_REG_BASE_LSB	0x61 | 
|  | 89 | #define SUPERIO_REG_LD8		0x08 | 
|  | 90 |  | 
|  | 91 | #define SMSC_EXTENT		0x02 | 
|  | 92 |  | 
|  | 93 | /* 0 <= nr <= 3 */ | 
|  | 94 | static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80}; | 
|  | 95 | #define SMSC47B397_REG_TEMP(nr)	(smsc47b397_reg_temp[(nr)]) | 
|  | 96 |  | 
|  | 97 | /* 0 <= nr <= 3 */ | 
|  | 98 | #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr)) | 
|  | 99 | #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr)) | 
|  | 100 |  | 
|  | 101 | struct smsc47b397_data { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 102 | unsigned short addr; | 
|  | 103 | const char *name; | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 104 | struct device *hwmon_dev; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 105 | struct mutex lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 107 | struct mutex update_lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | unsigned long last_updated; /* in jiffies */ | 
|  | 109 | int valid; | 
|  | 110 |  | 
|  | 111 | /* register values */ | 
|  | 112 | u16 fan[4]; | 
|  | 113 | u8 temp[4]; | 
|  | 114 | }; | 
|  | 115 |  | 
| Jean Delvare | 371f2e0 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 116 | static int smsc47b397_read_value(struct smsc47b397_data *data, u8 reg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | int res; | 
|  | 119 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 120 | mutex_lock(&data->lock); | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 121 | outb(reg, data->addr); | 
|  | 122 | res = inb_p(data->addr + 1); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 123 | mutex_unlock(&data->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return res; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | static struct smsc47b397_data *smsc47b397_update_device(struct device *dev) | 
|  | 128 | { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 129 | struct smsc47b397_data *data = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | int i; | 
|  | 131 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 132 | mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 135 | dev_dbg(dev, "starting device update...\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 |  | 
|  | 137 | /* 4 temperature inputs, 4 fan inputs */ | 
|  | 138 | for (i = 0; i < 4; i++) { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 139 | data->temp[i] = smsc47b397_read_value(data, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | SMSC47B397_REG_TEMP(i)); | 
|  | 141 |  | 
|  | 142 | /* must read LSB first */ | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 143 | data->fan[i]  = smsc47b397_read_value(data, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | SMSC47B397_REG_FAN_LSB(i)); | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 145 | data->fan[i] |= smsc47b397_read_value(data, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | SMSC47B397_REG_FAN_MSB(i)) << 8; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | data->last_updated = jiffies; | 
|  | 150 | data->valid = 1; | 
|  | 151 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 152 | dev_dbg(dev, "... device update complete\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 155 | mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 |  | 
|  | 157 | return data; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | /* TEMP: 0.001C/bit (-128C to +127C) | 
|  | 161 | REG: 1C/bit, two's complement */ | 
|  | 162 | static int temp_from_reg(u8 reg) | 
|  | 163 | { | 
|  | 164 | return (s8)reg * 1000; | 
|  | 165 | } | 
|  | 166 |  | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 167 | static ssize_t show_temp(struct device *dev, struct device_attribute | 
|  | 168 | *devattr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 170 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | struct smsc47b397_data *data = smsc47b397_update_device(dev); | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 172 | return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 175 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | 
|  | 176 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); | 
|  | 177 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); | 
|  | 178 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* FAN: 1 RPM/bit | 
|  | 181 | REG: count of 90kHz pulses / revolution */ | 
|  | 182 | static int fan_from_reg(u16 reg) | 
|  | 183 | { | 
| Jean Delvare | 90205c6 | 2007-06-23 14:58:22 +0200 | [diff] [blame] | 184 | if (reg == 0 || reg == 0xffff) | 
|  | 185 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return 90000 * 60 / reg; | 
|  | 187 | } | 
|  | 188 |  | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 189 | static ssize_t show_fan(struct device *dev, struct device_attribute | 
|  | 190 | *devattr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 192 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 193 | struct smsc47b397_data *data = smsc47b397_update_device(dev); | 
|  | 194 | return sprintf(buf, "%d\n", fan_from_reg(data->fan[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 196 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); | 
|  | 197 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); | 
|  | 198 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2); | 
|  | 199 | static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 201 | static ssize_t show_name(struct device *dev, struct device_attribute | 
|  | 202 | *devattr, char *buf) | 
|  | 203 | { | 
|  | 204 | struct smsc47b397_data *data = dev_get_drvdata(dev); | 
|  | 205 | return sprintf(buf, "%s\n", data->name); | 
|  | 206 | } | 
|  | 207 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | 
|  | 208 |  | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 209 | static struct attribute *smsc47b397_attributes[] = { | 
| Jean Delvare | 3659a01 | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 210 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 
|  | 211 | &sensor_dev_attr_temp2_input.dev_attr.attr, | 
|  | 212 | &sensor_dev_attr_temp3_input.dev_attr.attr, | 
|  | 213 | &sensor_dev_attr_temp4_input.dev_attr.attr, | 
|  | 214 | &sensor_dev_attr_fan1_input.dev_attr.attr, | 
|  | 215 | &sensor_dev_attr_fan2_input.dev_attr.attr, | 
|  | 216 | &sensor_dev_attr_fan3_input.dev_attr.attr, | 
|  | 217 | &sensor_dev_attr_fan4_input.dev_attr.attr, | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 218 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 219 | &dev_attr_name.attr, | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 220 | NULL | 
|  | 221 | }; | 
|  | 222 |  | 
|  | 223 | static const struct attribute_group smsc47b397_group = { | 
|  | 224 | .attrs = smsc47b397_attributes, | 
|  | 225 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 227 | static int __devexit smsc47b397_remove(struct platform_device *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 229 | struct smsc47b397_data *data = platform_get_drvdata(pdev); | 
|  | 230 | struct resource *res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 232 | hwmon_device_unregister(data->hwmon_dev); | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 233 | sysfs_remove_group(&pdev->dev.kobj, &smsc47b397_group); | 
|  | 234 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); | 
|  | 235 | release_region(res->start, SMSC_EXTENT); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 236 | kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 |  | 
|  | 238 | return 0; | 
|  | 239 | } | 
|  | 240 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 241 | static int smsc47b397_probe(struct platform_device *pdev); | 
| Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 242 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 243 | static struct platform_driver smsc47b397_driver = { | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 244 | .driver = { | 
| Jean Delvare | 8721884 | 2006-09-03 22:36:14 +0200 | [diff] [blame] | 245 | .owner	= THIS_MODULE, | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 246 | .name	= DRVNAME, | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 247 | }, | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 248 | .probe		= smsc47b397_probe, | 
|  | 249 | .remove		= __devexit_p(smsc47b397_remove), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | }; | 
|  | 251 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 252 | static int __devinit smsc47b397_probe(struct platform_device *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 254 | struct device *dev = &pdev->dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | struct smsc47b397_data *data; | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 256 | struct resource *res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | int err = 0; | 
|  | 258 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 259 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); | 
|  | 260 | if (!request_region(res->start, SMSC_EXTENT, | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 261 | smsc47b397_driver.driver.name)) { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 262 | dev_err(dev, "Region 0x%lx-0x%lx already in use!\n", | 
|  | 263 | (unsigned long)res->start, | 
|  | 264 | (unsigned long)res->start + SMSC_EXTENT - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return -EBUSY; | 
|  | 266 | } | 
|  | 267 |  | 
| Jean Delvare | 371f2e0 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 268 | data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL); | 
|  | 269 | if (!data) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | err = -ENOMEM; | 
|  | 271 | goto error_release; | 
|  | 272 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 274 | data->addr = res->start; | 
|  | 275 | data->name = "smsc47b397"; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 276 | mutex_init(&data->lock); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 277 | mutex_init(&data->update_lock); | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 278 | platform_set_drvdata(pdev, data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 |  | 
| Jean Delvare | 371f2e0 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 280 | err = sysfs_create_group(&dev->kobj, &smsc47b397_group); | 
|  | 281 | if (err) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | goto error_free; | 
|  | 283 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 284 | data->hwmon_dev = hwmon_device_register(dev); | 
|  | 285 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 286 | err = PTR_ERR(data->hwmon_dev); | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 287 | goto error_remove; | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 288 | } | 
|  | 289 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | return 0; | 
|  | 291 |  | 
| Mark M. Hoffman | c1685f6 | 2006-09-24 20:59:49 +0200 | [diff] [blame] | 292 | error_remove: | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 293 | sysfs_remove_group(&dev->kobj, &smsc47b397_group); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | error_free: | 
| Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 295 | kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | error_release: | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 297 | release_region(res->start, SMSC_EXTENT); | 
|  | 298 | return err; | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | static int __init smsc47b397_device_add(unsigned short address) | 
|  | 302 | { | 
|  | 303 | struct resource res = { | 
|  | 304 | .start	= address, | 
|  | 305 | .end	= address + SMSC_EXTENT - 1, | 
|  | 306 | .name	= DRVNAME, | 
|  | 307 | .flags	= IORESOURCE_IO, | 
|  | 308 | }; | 
|  | 309 | int err; | 
|  | 310 |  | 
| Jean Delvare | b9acb64 | 2009-01-07 16:37:35 +0100 | [diff] [blame] | 311 | err = acpi_check_resource_conflict(&res); | 
|  | 312 | if (err) | 
|  | 313 | goto exit; | 
|  | 314 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 315 | pdev = platform_device_alloc(DRVNAME, address); | 
|  | 316 | if (!pdev) { | 
|  | 317 | err = -ENOMEM; | 
| Joe Perches | bf1a85e | 2010-10-20 06:51:48 +0000 | [diff] [blame] | 318 | pr_err("Device allocation failed\n"); | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 319 | goto exit; | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | err = platform_device_add_resources(pdev, &res, 1); | 
|  | 323 | if (err) { | 
| Joe Perches | bf1a85e | 2010-10-20 06:51:48 +0000 | [diff] [blame] | 324 | pr_err("Device resource addition failed (%d)\n", err); | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 325 | goto exit_device_put; | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | err = platform_device_add(pdev); | 
|  | 329 | if (err) { | 
| Joe Perches | bf1a85e | 2010-10-20 06:51:48 +0000 | [diff] [blame] | 330 | pr_err("Device addition failed (%d)\n", err); | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 331 | goto exit_device_put; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | return 0; | 
|  | 335 |  | 
|  | 336 | exit_device_put: | 
|  | 337 | platform_device_put(pdev); | 
|  | 338 | exit: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return err; | 
|  | 340 | } | 
|  | 341 |  | 
| Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 342 | static int __init smsc47b397_find(unsigned short *addr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { | 
|  | 344 | u8 id, rev; | 
| Craig Kelley | 8093077 | 2008-02-29 10:24:44 -0700 | [diff] [blame] | 345 | char *name; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 |  | 
|  | 347 | superio_enter(); | 
| Jean Delvare | 67b671b | 2007-12-06 23:13:42 +0100 | [diff] [blame] | 348 | id = force_id ? force_id : superio_inb(SUPERIO_REG_DEVID); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 |  | 
| Jean Delvare | 371f2e0 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 350 | switch (id) { | 
| Craig Kelley | 8093077 | 2008-02-29 10:24:44 -0700 | [diff] [blame] | 351 | case 0x81: | 
|  | 352 | name = "SCH5307-NS"; | 
|  | 353 | break; | 
|  | 354 | case 0x6f: | 
|  | 355 | name = "LPC47B397-NC"; | 
|  | 356 | break; | 
|  | 357 | case 0x85: | 
|  | 358 | case 0x8c: | 
|  | 359 | name = "SCH5317"; | 
|  | 360 | break; | 
|  | 361 | default: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | superio_exit(); | 
|  | 363 | return -ENODEV; | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | rev = superio_inb(SUPERIO_REG_DEVREV); | 
|  | 367 |  | 
|  | 368 | superio_select(SUPERIO_REG_LD8); | 
|  | 369 | *addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8) | 
|  | 370 | |  superio_inb(SUPERIO_REG_BASE_LSB); | 
|  | 371 |  | 
| Joe Perches | bf1a85e | 2010-10-20 06:51:48 +0000 | [diff] [blame] | 372 | pr_info("found SMSC %s (base address 0x%04x, revision %u)\n", | 
| Craig Kelley | 8093077 | 2008-02-29 10:24:44 -0700 | [diff] [blame] | 373 | name, *addr, rev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 |  | 
|  | 375 | superio_exit(); | 
|  | 376 | return 0; | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | static int __init smsc47b397_init(void) | 
|  | 380 | { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 381 | unsigned short address; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | int ret; | 
|  | 383 |  | 
| Jean Delvare | 371f2e0 | 2011-11-04 12:00:47 +0100 | [diff] [blame] | 384 | ret = smsc47b397_find(&address); | 
|  | 385 | if (ret) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return ret; | 
|  | 387 |  | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 388 | ret = platform_driver_register(&smsc47b397_driver); | 
|  | 389 | if (ret) | 
|  | 390 | goto exit; | 
|  | 391 |  | 
|  | 392 | /* Sets global pdev as a side effect */ | 
|  | 393 | ret = smsc47b397_device_add(address); | 
|  | 394 | if (ret) | 
|  | 395 | goto exit_driver; | 
|  | 396 |  | 
|  | 397 | return 0; | 
|  | 398 |  | 
|  | 399 | exit_driver: | 
|  | 400 | platform_driver_unregister(&smsc47b397_driver); | 
|  | 401 | exit: | 
|  | 402 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } | 
|  | 404 |  | 
|  | 405 | static void __exit smsc47b397_exit(void) | 
|  | 406 | { | 
| Jean Delvare | 292fc1a | 2007-05-08 17:22:03 +0200 | [diff] [blame] | 407 | platform_device_unregister(pdev); | 
|  | 408 | platform_driver_unregister(&smsc47b397_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } | 
|  | 410 |  | 
|  | 411 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); | 
|  | 412 | MODULE_DESCRIPTION("SMSC LPC47B397 driver"); | 
|  | 413 | MODULE_LICENSE("GPL"); | 
|  | 414 |  | 
|  | 415 | module_init(smsc47b397_init); | 
|  | 416 | module_exit(smsc47b397_exit); |