Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | smsc47m1.c - Part of lm_sensors, Linux kernel modules |
| 3 | for hardware monitoring |
| 4 | |
| 5 | Supports the SMSC LPC47B27x, LPC47M10x, LPC47M13x and LPC47M14x |
| 6 | Super-I/O chips. |
| 7 | |
| 8 | Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com> |
| 9 | Copyright (C) 2004 Jean Delvare <khali@linux-fr.org> |
| 10 | Ported to Linux 2.6 by Gabriele Gorla <gorlik@yahoo.com> |
| 11 | and Jean Delvare |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or modify |
| 14 | it under the terms of the GNU General Public License as published by |
| 15 | the Free Software Foundation; either version 2 of the License, or |
| 16 | (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/ioport.h> |
| 31 | #include <linux/jiffies.h> |
| 32 | #include <linux/i2c.h> |
| 33 | #include <linux/i2c-sensor.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame^] | 34 | #include <linux/hwmon.h> |
| 35 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/init.h> |
| 37 | #include <asm/io.h> |
| 38 | |
| 39 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; |
| 40 | /* Address is autodetected, there is no default value */ |
| 41 | static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END }; |
| 42 | static struct i2c_force_data forces[] = {{NULL}}; |
| 43 | |
| 44 | enum chips { any_chip, smsc47m1 }; |
| 45 | static struct i2c_address_data addr_data = { |
| 46 | .normal_i2c = normal_i2c, |
| 47 | .normal_isa = normal_isa, |
| 48 | .forces = forces, |
| 49 | }; |
| 50 | |
| 51 | /* Super-I/0 registers and commands */ |
| 52 | |
| 53 | #define REG 0x2e /* The register to read/write */ |
| 54 | #define VAL 0x2f /* The value to read/write */ |
| 55 | |
| 56 | static inline void |
| 57 | superio_outb(int reg, int val) |
| 58 | { |
| 59 | outb(reg, REG); |
| 60 | outb(val, VAL); |
| 61 | } |
| 62 | |
| 63 | static inline int |
| 64 | superio_inb(int reg) |
| 65 | { |
| 66 | outb(reg, REG); |
| 67 | return inb(VAL); |
| 68 | } |
| 69 | |
| 70 | /* logical device for fans is 0x0A */ |
| 71 | #define superio_select() superio_outb(0x07, 0x0A) |
| 72 | |
| 73 | static inline void |
| 74 | superio_enter(void) |
| 75 | { |
| 76 | outb(0x55, REG); |
| 77 | } |
| 78 | |
| 79 | static inline void |
| 80 | superio_exit(void) |
| 81 | { |
| 82 | outb(0xAA, REG); |
| 83 | } |
| 84 | |
| 85 | #define SUPERIO_REG_ACT 0x30 |
| 86 | #define SUPERIO_REG_BASE 0x60 |
| 87 | #define SUPERIO_REG_DEVID 0x20 |
| 88 | |
| 89 | /* Logical device registers */ |
| 90 | |
| 91 | #define SMSC_EXTENT 0x80 |
| 92 | |
| 93 | /* nr is 0 or 1 in the macros below */ |
| 94 | #define SMSC47M1_REG_ALARM 0x04 |
| 95 | #define SMSC47M1_REG_TPIN(nr) (0x34 - (nr)) |
| 96 | #define SMSC47M1_REG_PPIN(nr) (0x36 - (nr)) |
| 97 | #define SMSC47M1_REG_PWM(nr) (0x56 + (nr)) |
| 98 | #define SMSC47M1_REG_FANDIV 0x58 |
| 99 | #define SMSC47M1_REG_FAN(nr) (0x59 + (nr)) |
| 100 | #define SMSC47M1_REG_FAN_PRELOAD(nr) (0x5B + (nr)) |
| 101 | |
| 102 | #define MIN_FROM_REG(reg,div) ((reg)>=192 ? 0 : \ |
| 103 | 983040/((192-(reg))*(div))) |
| 104 | #define FAN_FROM_REG(reg,div,preload) ((reg)<=(preload) || (reg)==255 ? 0 : \ |
| 105 | 983040/(((reg)-(preload))*(div))) |
| 106 | #define DIV_FROM_REG(reg) (1 << (reg)) |
| 107 | #define PWM_FROM_REG(reg) (((reg) & 0x7E) << 1) |
| 108 | #define PWM_EN_FROM_REG(reg) ((~(reg)) & 0x01) |
| 109 | #define PWM_TO_REG(reg) (((reg) >> 1) & 0x7E) |
| 110 | |
| 111 | struct smsc47m1_data { |
| 112 | struct i2c_client client; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame^] | 113 | struct class_device *class_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | struct semaphore lock; |
| 115 | |
| 116 | struct semaphore update_lock; |
| 117 | unsigned long last_updated; /* In jiffies */ |
| 118 | |
| 119 | u8 fan[2]; /* Register value */ |
| 120 | u8 fan_preload[2]; /* Register value */ |
| 121 | u8 fan_div[2]; /* Register encoding, shifted right */ |
| 122 | u8 alarms; /* Register encoding */ |
| 123 | u8 pwm[2]; /* Register value (bit 7 is enable) */ |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | static int smsc47m1_attach_adapter(struct i2c_adapter *adapter); |
| 128 | static int smsc47m1_find(int *address); |
| 129 | static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind); |
| 130 | static int smsc47m1_detach_client(struct i2c_client *client); |
| 131 | |
| 132 | static int smsc47m1_read_value(struct i2c_client *client, u8 reg); |
| 133 | static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value); |
| 134 | |
| 135 | static struct smsc47m1_data *smsc47m1_update_device(struct device *dev, |
| 136 | int init); |
| 137 | |
| 138 | |
| 139 | static struct i2c_driver smsc47m1_driver = { |
| 140 | .owner = THIS_MODULE, |
| 141 | .name = "smsc47m1", |
| 142 | .id = I2C_DRIVERID_SMSC47M1, |
| 143 | .flags = I2C_DF_NOTIFY, |
| 144 | .attach_adapter = smsc47m1_attach_adapter, |
| 145 | .detach_client = smsc47m1_detach_client, |
| 146 | }; |
| 147 | |
| 148 | /* nr is 0 or 1 in the callback functions below */ |
| 149 | |
| 150 | static ssize_t get_fan(struct device *dev, char *buf, int nr) |
| 151 | { |
| 152 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
| 153 | /* This chip (stupidly) stops monitoring fan speed if PWM is |
| 154 | enabled and duty cycle is 0%. This is fine if the monitoring |
| 155 | and control concern the same fan, but troublesome if they are |
| 156 | not (which could as well happen). */ |
| 157 | int rpm = (data->pwm[nr] & 0x7F) == 0x00 ? 0 : |
| 158 | FAN_FROM_REG(data->fan[nr], |
| 159 | DIV_FROM_REG(data->fan_div[nr]), |
| 160 | data->fan_preload[nr]); |
| 161 | return sprintf(buf, "%d\n", rpm); |
| 162 | } |
| 163 | |
| 164 | static ssize_t get_fan_min(struct device *dev, char *buf, int nr) |
| 165 | { |
| 166 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
| 167 | int rpm = MIN_FROM_REG(data->fan_preload[nr], |
| 168 | DIV_FROM_REG(data->fan_div[nr])); |
| 169 | return sprintf(buf, "%d\n", rpm); |
| 170 | } |
| 171 | |
| 172 | static ssize_t get_fan_div(struct device *dev, char *buf, int nr) |
| 173 | { |
| 174 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
| 175 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
| 176 | } |
| 177 | |
| 178 | static ssize_t get_pwm(struct device *dev, char *buf, int nr) |
| 179 | { |
| 180 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
| 181 | return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr])); |
| 182 | } |
| 183 | |
| 184 | static ssize_t get_pwm_en(struct device *dev, char *buf, int nr) |
| 185 | { |
| 186 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
| 187 | return sprintf(buf, "%d\n", PWM_EN_FROM_REG(data->pwm[nr])); |
| 188 | } |
| 189 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 190 | static ssize_t get_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | struct smsc47m1_data *data = smsc47m1_update_device(dev, 0); |
| 193 | return sprintf(buf, "%d\n", data->alarms); |
| 194 | } |
| 195 | |
| 196 | static ssize_t set_fan_min(struct device *dev, const char *buf, |
| 197 | size_t count, int nr) |
| 198 | { |
| 199 | struct i2c_client *client = to_i2c_client(dev); |
| 200 | struct smsc47m1_data *data = i2c_get_clientdata(client); |
| 201 | long rpmdiv, val = simple_strtol(buf, NULL, 10); |
| 202 | |
| 203 | down(&data->update_lock); |
| 204 | rpmdiv = val * DIV_FROM_REG(data->fan_div[nr]); |
| 205 | |
| 206 | if (983040 > 192 * rpmdiv || 2 * rpmdiv > 983040) { |
| 207 | up(&data->update_lock); |
| 208 | return -EINVAL; |
| 209 | } |
| 210 | |
| 211 | data->fan_preload[nr] = 192 - ((983040 + rpmdiv / 2) / rpmdiv); |
| 212 | smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr), |
| 213 | data->fan_preload[nr]); |
| 214 | up(&data->update_lock); |
| 215 | |
| 216 | return count; |
| 217 | } |
| 218 | |
| 219 | /* Note: we save and restore the fan minimum here, because its value is |
| 220 | determined in part by the fan clock divider. This follows the principle |
| 221 | of least suprise; the user doesn't expect the fan minimum to change just |
| 222 | because the divider changed. */ |
| 223 | static ssize_t set_fan_div(struct device *dev, const char *buf, |
| 224 | size_t count, int nr) |
| 225 | { |
| 226 | struct i2c_client *client = to_i2c_client(dev); |
| 227 | struct smsc47m1_data *data = i2c_get_clientdata(client); |
| 228 | |
| 229 | long new_div = simple_strtol(buf, NULL, 10), tmp; |
| 230 | u8 old_div = DIV_FROM_REG(data->fan_div[nr]); |
| 231 | |
| 232 | if (new_div == old_div) /* No change */ |
| 233 | return count; |
| 234 | |
| 235 | down(&data->update_lock); |
| 236 | switch (new_div) { |
| 237 | case 1: data->fan_div[nr] = 0; break; |
| 238 | case 2: data->fan_div[nr] = 1; break; |
| 239 | case 4: data->fan_div[nr] = 2; break; |
| 240 | case 8: data->fan_div[nr] = 3; break; |
| 241 | default: |
| 242 | up(&data->update_lock); |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | |
| 246 | tmp = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV) & 0x0F; |
| 247 | tmp |= (data->fan_div[0] << 4) | (data->fan_div[1] << 6); |
| 248 | smsc47m1_write_value(client, SMSC47M1_REG_FANDIV, tmp); |
| 249 | |
| 250 | /* Preserve fan min */ |
| 251 | tmp = 192 - (old_div * (192 - data->fan_preload[nr]) |
| 252 | + new_div / 2) / new_div; |
| 253 | data->fan_preload[nr] = SENSORS_LIMIT(tmp, 0, 191); |
| 254 | smsc47m1_write_value(client, SMSC47M1_REG_FAN_PRELOAD(nr), |
| 255 | data->fan_preload[nr]); |
| 256 | up(&data->update_lock); |
| 257 | |
| 258 | return count; |
| 259 | } |
| 260 | |
| 261 | static ssize_t set_pwm(struct device *dev, const char *buf, |
| 262 | size_t count, int nr) |
| 263 | { |
| 264 | struct i2c_client *client = to_i2c_client(dev); |
| 265 | struct smsc47m1_data *data = i2c_get_clientdata(client); |
| 266 | |
| 267 | long val = simple_strtol(buf, NULL, 10); |
| 268 | |
| 269 | if (val < 0 || val > 255) |
| 270 | return -EINVAL; |
| 271 | |
| 272 | down(&data->update_lock); |
| 273 | data->pwm[nr] &= 0x81; /* Preserve additional bits */ |
| 274 | data->pwm[nr] |= PWM_TO_REG(val); |
| 275 | smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr), |
| 276 | data->pwm[nr]); |
| 277 | up(&data->update_lock); |
| 278 | |
| 279 | return count; |
| 280 | } |
| 281 | |
| 282 | static ssize_t set_pwm_en(struct device *dev, const char *buf, |
| 283 | size_t count, int nr) |
| 284 | { |
| 285 | struct i2c_client *client = to_i2c_client(dev); |
| 286 | struct smsc47m1_data *data = i2c_get_clientdata(client); |
| 287 | |
| 288 | long val = simple_strtol(buf, NULL, 10); |
| 289 | |
| 290 | if (val != 0 && val != 1) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | down(&data->update_lock); |
| 294 | data->pwm[nr] &= 0xFE; /* preserve the other bits */ |
| 295 | data->pwm[nr] |= !val; |
| 296 | smsc47m1_write_value(client, SMSC47M1_REG_PWM(nr), |
| 297 | data->pwm[nr]); |
| 298 | up(&data->update_lock); |
| 299 | |
| 300 | return count; |
| 301 | } |
| 302 | |
| 303 | #define fan_present(offset) \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 304 | static ssize_t get_fan##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { \ |
| 306 | return get_fan(dev, buf, offset - 1); \ |
| 307 | } \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 308 | static ssize_t get_fan##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { \ |
| 310 | return get_fan_min(dev, buf, offset - 1); \ |
| 311 | } \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 312 | static ssize_t set_fan##offset##_min (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | const char *buf, size_t count) \ |
| 314 | { \ |
| 315 | return set_fan_min(dev, buf, count, offset - 1); \ |
| 316 | } \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 317 | static ssize_t get_fan##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { \ |
| 319 | return get_fan_div(dev, buf, offset - 1); \ |
| 320 | } \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 321 | static ssize_t set_fan##offset##_div (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | const char *buf, size_t count) \ |
| 323 | { \ |
| 324 | return set_fan_div(dev, buf, count, offset - 1); \ |
| 325 | } \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 326 | static ssize_t get_pwm##offset (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { \ |
| 328 | return get_pwm(dev, buf, offset - 1); \ |
| 329 | } \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 330 | static ssize_t set_pwm##offset (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | const char *buf, size_t count) \ |
| 332 | { \ |
| 333 | return set_pwm(dev, buf, count, offset - 1); \ |
| 334 | } \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 335 | static ssize_t get_pwm##offset##_en (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { \ |
| 337 | return get_pwm_en(dev, buf, offset - 1); \ |
| 338 | } \ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 339 | static ssize_t set_pwm##offset##_en (struct device *dev, struct device_attribute *attr, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | const char *buf, size_t count) \ |
| 341 | { \ |
| 342 | return set_pwm_en(dev, buf, count, offset - 1); \ |
| 343 | } \ |
| 344 | static DEVICE_ATTR(fan##offset##_input, S_IRUGO, get_fan##offset, \ |
| 345 | NULL); \ |
| 346 | static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 347 | get_fan##offset##_min, set_fan##offset##_min); \ |
| 348 | static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 349 | get_fan##offset##_div, set_fan##offset##_div); \ |
| 350 | static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ |
| 351 | get_pwm##offset, set_pwm##offset); \ |
| 352 | static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ |
| 353 | get_pwm##offset##_en, set_pwm##offset##_en); |
| 354 | |
| 355 | fan_present(1); |
| 356 | fan_present(2); |
| 357 | |
| 358 | static DEVICE_ATTR(alarms, S_IRUGO, get_alarms, NULL); |
| 359 | |
| 360 | static int smsc47m1_attach_adapter(struct i2c_adapter *adapter) |
| 361 | { |
| 362 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 363 | return 0; |
| 364 | return i2c_detect(adapter, &addr_data, smsc47m1_detect); |
| 365 | } |
| 366 | |
| 367 | static int smsc47m1_find(int *address) |
| 368 | { |
| 369 | u8 val; |
| 370 | |
| 371 | superio_enter(); |
| 372 | val = superio_inb(SUPERIO_REG_DEVID); |
| 373 | |
| 374 | /* |
| 375 | * SMSC LPC47M10x/LPC47M13x (device id 0x59), LPC47M14x (device id |
| 376 | * 0x5F) and LPC47B27x (device id 0x51) have fan control. |
| 377 | * The LPC47M15x and LPC47M192 chips "with hardware monitoring block" |
Jean Delvare | ec5ce55 | 2005-04-26 22:09:43 +0200 | [diff] [blame] | 378 | * can do much more besides (device id 0x60). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | */ |
| 380 | if (val == 0x51) |
Jean Delvare | ec5ce55 | 2005-04-26 22:09:43 +0200 | [diff] [blame] | 381 | printk(KERN_INFO "smsc47m1: Found SMSC LPC47B27x\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | else if (val == 0x59) |
Jean Delvare | ec5ce55 | 2005-04-26 22:09:43 +0200 | [diff] [blame] | 383 | printk(KERN_INFO "smsc47m1: Found SMSC LPC47M10x/LPC47M13x\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | else if (val == 0x5F) |
Jean Delvare | ec5ce55 | 2005-04-26 22:09:43 +0200 | [diff] [blame] | 385 | printk(KERN_INFO "smsc47m1: Found SMSC LPC47M14x\n"); |
| 386 | else if (val == 0x60) |
| 387 | printk(KERN_INFO "smsc47m1: Found SMSC LPC47M15x/LPC47M192\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | else { |
| 389 | superio_exit(); |
| 390 | return -ENODEV; |
| 391 | } |
| 392 | |
| 393 | superio_select(); |
| 394 | *address = (superio_inb(SUPERIO_REG_BASE) << 8) |
| 395 | | superio_inb(SUPERIO_REG_BASE + 1); |
| 396 | val = superio_inb(SUPERIO_REG_ACT); |
| 397 | if (*address == 0 || (val & 0x01) == 0) { |
| 398 | printk(KERN_INFO "smsc47m1: Device is disabled, will not use\n"); |
| 399 | superio_exit(); |
| 400 | return -ENODEV; |
| 401 | } |
| 402 | |
| 403 | superio_exit(); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int smsc47m1_detect(struct i2c_adapter *adapter, int address, int kind) |
| 408 | { |
| 409 | struct i2c_client *new_client; |
| 410 | struct smsc47m1_data *data; |
| 411 | int err = 0; |
| 412 | int fan1, fan2, pwm1, pwm2; |
| 413 | |
| 414 | if (!i2c_is_isa_adapter(adapter)) { |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | if (!request_region(address, SMSC_EXTENT, smsc47m1_driver.name)) { |
| 419 | dev_err(&adapter->dev, "Region 0x%x already in use!\n", address); |
| 420 | return -EBUSY; |
| 421 | } |
| 422 | |
| 423 | if (!(data = kmalloc(sizeof(struct smsc47m1_data), GFP_KERNEL))) { |
| 424 | err = -ENOMEM; |
| 425 | goto error_release; |
| 426 | } |
| 427 | memset(data, 0x00, sizeof(struct smsc47m1_data)); |
| 428 | |
| 429 | new_client = &data->client; |
| 430 | i2c_set_clientdata(new_client, data); |
| 431 | new_client->addr = address; |
| 432 | init_MUTEX(&data->lock); |
| 433 | new_client->adapter = adapter; |
| 434 | new_client->driver = &smsc47m1_driver; |
| 435 | new_client->flags = 0; |
| 436 | |
| 437 | strlcpy(new_client->name, "smsc47m1", I2C_NAME_SIZE); |
| 438 | init_MUTEX(&data->update_lock); |
| 439 | |
| 440 | /* If no function is properly configured, there's no point in |
| 441 | actually registering the chip. */ |
| 442 | fan1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(0)) & 0x05) |
| 443 | == 0x05; |
| 444 | fan2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_TPIN(1)) & 0x05) |
| 445 | == 0x05; |
| 446 | pwm1 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(0)) & 0x05) |
| 447 | == 0x04; |
| 448 | pwm2 = (smsc47m1_read_value(new_client, SMSC47M1_REG_PPIN(1)) & 0x05) |
| 449 | == 0x04; |
| 450 | if (!(fan1 || fan2 || pwm1 || pwm2)) { |
| 451 | dev_warn(&new_client->dev, "Device is not configured, will not use\n"); |
| 452 | err = -ENODEV; |
| 453 | goto error_free; |
| 454 | } |
| 455 | |
| 456 | if ((err = i2c_attach_client(new_client))) |
| 457 | goto error_free; |
| 458 | |
| 459 | /* Some values (fan min, clock dividers, pwm registers) may be |
| 460 | needed before any update is triggered, so we better read them |
| 461 | at least once here. We don't usually do it that way, but in |
| 462 | this particular case, manually reading 5 registers out of 8 |
| 463 | doesn't make much sense and we're better using the existing |
| 464 | function. */ |
| 465 | smsc47m1_update_device(&new_client->dev, 1); |
| 466 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame^] | 467 | /* Register sysfs hooks */ |
| 468 | data->class_dev = hwmon_device_register(&new_client->dev); |
| 469 | if (IS_ERR(data->class_dev)) { |
| 470 | err = PTR_ERR(data->class_dev); |
| 471 | goto error_detach; |
| 472 | } |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (fan1) { |
| 475 | device_create_file(&new_client->dev, &dev_attr_fan1_input); |
| 476 | device_create_file(&new_client->dev, &dev_attr_fan1_min); |
| 477 | device_create_file(&new_client->dev, &dev_attr_fan1_div); |
| 478 | } else |
| 479 | dev_dbg(&new_client->dev, "Fan 1 not enabled by hardware, " |
| 480 | "skipping\n"); |
| 481 | |
| 482 | if (fan2) { |
| 483 | device_create_file(&new_client->dev, &dev_attr_fan2_input); |
| 484 | device_create_file(&new_client->dev, &dev_attr_fan2_min); |
| 485 | device_create_file(&new_client->dev, &dev_attr_fan2_div); |
| 486 | } else |
| 487 | dev_dbg(&new_client->dev, "Fan 2 not enabled by hardware, " |
| 488 | "skipping\n"); |
| 489 | |
| 490 | if (pwm1) { |
| 491 | device_create_file(&new_client->dev, &dev_attr_pwm1); |
| 492 | device_create_file(&new_client->dev, &dev_attr_pwm1_enable); |
| 493 | } else |
| 494 | dev_dbg(&new_client->dev, "PWM 1 not enabled by hardware, " |
| 495 | "skipping\n"); |
| 496 | if (pwm2) { |
| 497 | device_create_file(&new_client->dev, &dev_attr_pwm2); |
| 498 | device_create_file(&new_client->dev, &dev_attr_pwm2_enable); |
| 499 | } else |
| 500 | dev_dbg(&new_client->dev, "PWM 2 not enabled by hardware, " |
| 501 | "skipping\n"); |
| 502 | |
| 503 | device_create_file(&new_client->dev, &dev_attr_alarms); |
| 504 | |
| 505 | return 0; |
| 506 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame^] | 507 | error_detach: |
| 508 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | error_free: |
Alexey Dobriyan | 1f57ff8 | 2005-08-26 01:49:14 +0400 | [diff] [blame] | 510 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | error_release: |
| 512 | release_region(address, SMSC_EXTENT); |
| 513 | return err; |
| 514 | } |
| 515 | |
| 516 | static int smsc47m1_detach_client(struct i2c_client *client) |
| 517 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame^] | 518 | struct smsc47m1_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | int err; |
| 520 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame^] | 521 | hwmon_device_unregister(data->class_dev); |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if ((err = i2c_detach_client(client))) { |
| 524 | dev_err(&client->dev, "Client deregistration failed, " |
| 525 | "client not detached.\n"); |
| 526 | return err; |
| 527 | } |
| 528 | |
| 529 | release_region(client->addr, SMSC_EXTENT); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame^] | 530 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static int smsc47m1_read_value(struct i2c_client *client, u8 reg) |
| 536 | { |
| 537 | int res; |
| 538 | |
| 539 | down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock); |
| 540 | res = inb_p(client->addr + reg); |
| 541 | up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock); |
| 542 | return res; |
| 543 | } |
| 544 | |
| 545 | static void smsc47m1_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 546 | { |
| 547 | down(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock); |
| 548 | outb_p(value, client->addr + reg); |
| 549 | up(&((struct smsc47m1_data *) i2c_get_clientdata(client))->lock); |
| 550 | } |
| 551 | |
| 552 | static struct smsc47m1_data *smsc47m1_update_device(struct device *dev, |
| 553 | int init) |
| 554 | { |
| 555 | struct i2c_client *client = to_i2c_client(dev); |
| 556 | struct smsc47m1_data *data = i2c_get_clientdata(client); |
| 557 | |
| 558 | down(&data->update_lock); |
| 559 | |
| 560 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) || init) { |
| 561 | int i; |
| 562 | |
| 563 | for (i = 0; i < 2; i++) { |
| 564 | data->fan[i] = smsc47m1_read_value(client, |
| 565 | SMSC47M1_REG_FAN(i)); |
| 566 | data->fan_preload[i] = smsc47m1_read_value(client, |
| 567 | SMSC47M1_REG_FAN_PRELOAD(i)); |
| 568 | data->pwm[i] = smsc47m1_read_value(client, |
| 569 | SMSC47M1_REG_PWM(i)); |
| 570 | } |
| 571 | |
| 572 | i = smsc47m1_read_value(client, SMSC47M1_REG_FANDIV); |
| 573 | data->fan_div[0] = (i >> 4) & 0x03; |
| 574 | data->fan_div[1] = i >> 6; |
| 575 | |
| 576 | data->alarms = smsc47m1_read_value(client, |
| 577 | SMSC47M1_REG_ALARM) >> 6; |
| 578 | /* Clear alarms if needed */ |
| 579 | if (data->alarms) |
| 580 | smsc47m1_write_value(client, SMSC47M1_REG_ALARM, 0xC0); |
| 581 | |
| 582 | data->last_updated = jiffies; |
| 583 | } |
| 584 | |
| 585 | up(&data->update_lock); |
| 586 | return data; |
| 587 | } |
| 588 | |
| 589 | static int __init sm_smsc47m1_init(void) |
| 590 | { |
| 591 | if (smsc47m1_find(normal_isa)) { |
| 592 | return -ENODEV; |
| 593 | } |
| 594 | |
| 595 | return i2c_add_driver(&smsc47m1_driver); |
| 596 | } |
| 597 | |
| 598 | static void __exit sm_smsc47m1_exit(void) |
| 599 | { |
| 600 | i2c_del_driver(&smsc47m1_driver); |
| 601 | } |
| 602 | |
| 603 | MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>"); |
| 604 | MODULE_DESCRIPTION("SMSC LPC47M1xx fan sensors driver"); |
| 605 | MODULE_LICENSE("GPL"); |
| 606 | |
| 607 | module_init(sm_smsc47m1_init); |
| 608 | module_exit(sm_smsc47m1_exit); |