1 /* 2 * pc87427.c - hardware monitoring driver for the 3 * National Semiconductor PC87427 Super-I/O chip 4 * Copyright (C) 2006, 2008, 2010 Jean Delvare <jdelvare@suse.de> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * Supports the following chips: 16 * 17 * Chip #vin #fan #pwm #temp devid 18 * PC87427 - 8 4 6 0xF2 19 * 20 * This driver assumes that no more than one chip is present. 21 * Only fans are fully supported so far. Temperatures are in read-only 22 * mode, and voltages aren't supported at all. 23 */ 24 25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 26 27 #include <linux/module.h> 28 #include <linux/init.h> 29 #include <linux/slab.h> 30 #include <linux/jiffies.h> 31 #include <linux/platform_device.h> 32 #include <linux/hwmon.h> 33 #include <linux/hwmon-sysfs.h> 34 #include <linux/err.h> 35 #include <linux/mutex.h> 36 #include <linux/sysfs.h> 37 #include <linux/ioport.h> 38 #include <linux/acpi.h> 39 #include <linux/io.h> 40 41 static unsigned short force_id; 42 module_param(force_id, ushort, 0); 43 MODULE_PARM_DESC(force_id, "Override the detected device ID"); 44 45 static struct platform_device *pdev; 46 47 #define DRVNAME "pc87427" 48 49 /* 50 * The lock mutex protects both the I/O accesses (needed because the 51 * device is using banked registers) and the register cache (needed to keep 52 * the data in the registers and the cache in sync at any time). 53 */ 54 struct pc87427_data { 55 struct device *hwmon_dev; 56 struct mutex lock; 57 int address[2]; 58 const char *name; 59 60 unsigned long last_updated; /* in jiffies */ 61 u8 fan_enabled; /* bit vector */ 62 u16 fan[8]; /* register values */ 63 u16 fan_min[8]; /* register values */ 64 u8 fan_status[8]; /* register values */ 65 66 u8 pwm_enabled; /* bit vector */ 67 u8 pwm_auto_ok; /* bit vector */ 68 u8 pwm_enable[4]; /* register values */ 69 u8 pwm[4]; /* register values */ 70 71 u8 temp_enabled; /* bit vector */ 72 s16 temp[6]; /* register values */ 73 s8 temp_min[6]; /* register values */ 74 s8 temp_max[6]; /* register values */ 75 s8 temp_crit[6]; /* register values */ 76 u8 temp_status[6]; /* register values */ 77 u8 temp_type[6]; /* register values */ 78 }; 79 80 struct pc87427_sio_data { 81 unsigned short address[2]; 82 u8 has_fanin; 83 u8 has_fanout; 84 }; 85 86 /* 87 * Super-I/O registers and operations 88 */ 89 90 #define SIOREG_LDSEL 0x07 /* Logical device select */ 91 #define SIOREG_DEVID 0x20 /* Device ID */ 92 #define SIOREG_CF2 0x22 /* Configuration 2 */ 93 #define SIOREG_CF3 0x23 /* Configuration 3 */ 94 #define SIOREG_CF4 0x24 /* Configuration 4 */ 95 #define SIOREG_CF5 0x25 /* Configuration 5 */ 96 #define SIOREG_CFB 0x2B /* Configuration B */ 97 #define SIOREG_CFC 0x2C /* Configuration C */ 98 #define SIOREG_CFD 0x2D /* Configuration D */ 99 #define SIOREG_ACT 0x30 /* Device activation */ 100 #define SIOREG_MAP 0x50 /* I/O or memory mapping */ 101 #define SIOREG_IOBASE 0x60 /* I/O base address */ 102 103 static const u8 logdev[2] = { 0x09, 0x14 }; 104 static const char *logdev_str[2] = { DRVNAME " FMC", DRVNAME " HMC" }; 105 #define LD_FAN 0 106 #define LD_IN 1 107 #define LD_TEMP 1 108 109 static inline int superio_enter(int sioaddr) 110 { 111 if (!request_muxed_region(sioaddr, 2, DRVNAME)) 112 return -EBUSY; 113 return 0; 114 } 115 116 static inline void superio_outb(int sioaddr, int reg, int val) 117 { 118 outb(reg, sioaddr); 119 outb(val, sioaddr + 1); 120 } 121 122 static inline int superio_inb(int sioaddr, int reg) 123 { 124 outb(reg, sioaddr); 125 return inb(sioaddr + 1); 126 } 127 128 static inline void superio_exit(int sioaddr) 129 { 130 outb(0x02, sioaddr); 131 outb(0x02, sioaddr + 1); 132 release_region(sioaddr, 2); 133 } 134 135 /* 136 * Logical devices 137 */ 138 139 #define REGION_LENGTH 32 140 #define PC87427_REG_BANK 0x0f 141 #define BANK_FM(nr) (nr) 142 #define BANK_FT(nr) (0x08 + (nr)) 143 #define BANK_FC(nr) (0x10 + (nr) * 2) 144 #define BANK_TM(nr) (nr) 145 #define BANK_VM(nr) (0x08 + (nr)) 146 147 /* 148 * I/O access functions 149 */ 150 151 /* ldi is the logical device index */ 152 static inline int pc87427_read8(struct pc87427_data *data, u8 ldi, u8 reg) 153 { 154 return inb(data->address[ldi] + reg); 155 } 156 157 /* Must be called with data->lock held, except during init */ 158 static inline int pc87427_read8_bank(struct pc87427_data *data, u8 ldi, 159 u8 bank, u8 reg) 160 { 161 outb(bank, data->address[ldi] + PC87427_REG_BANK); 162 return inb(data->address[ldi] + reg); 163 } 164 165 /* Must be called with data->lock held, except during init */ 166 static inline void pc87427_write8_bank(struct pc87427_data *data, u8 ldi, 167 u8 bank, u8 reg, u8 value) 168 { 169 outb(bank, data->address[ldi] + PC87427_REG_BANK); 170 outb(value, data->address[ldi] + reg); 171 } 172 173 /* 174 * Fan registers and conversions 175 */ 176 177 /* fan data registers are 16-bit wide */ 178 #define PC87427_REG_FAN 0x12 179 #define PC87427_REG_FAN_MIN 0x14 180 #define PC87427_REG_FAN_STATUS 0x10 181 182 #define FAN_STATUS_STALL (1 << 3) 183 #define FAN_STATUS_LOSPD (1 << 1) 184 #define FAN_STATUS_MONEN (1 << 0) 185 186 /* 187 * Dedicated function to read all registers related to a given fan input. 188 * This saves us quite a few locks and bank selections. 189 * Must be called with data->lock held. 190 * nr is from 0 to 7 191 */ 192 static void pc87427_readall_fan(struct pc87427_data *data, u8 nr) 193 { 194 int iobase = data->address[LD_FAN]; 195 196 outb(BANK_FM(nr), iobase + PC87427_REG_BANK); 197 data->fan[nr] = inw(iobase + PC87427_REG_FAN); 198 data->fan_min[nr] = inw(iobase + PC87427_REG_FAN_MIN); 199 data->fan_status[nr] = inb(iobase + PC87427_REG_FAN_STATUS); 200 /* Clear fan alarm bits */ 201 outb(data->fan_status[nr], iobase + PC87427_REG_FAN_STATUS); 202 } 203 204 /* 205 * The 2 LSB of fan speed registers are used for something different. 206 * The actual 2 LSB of the measurements are not available. 207 */ 208 static inline unsigned long fan_from_reg(u16 reg) 209 { 210 reg &= 0xfffc; 211 if (reg == 0x0000 || reg == 0xfffc) 212 return 0; 213 return 5400000UL / reg; 214 } 215 216 /* The 2 LSB of the fan speed limit registers are not significant. */ 217 static inline u16 fan_to_reg(unsigned long val) 218 { 219 if (val < 83UL) 220 return 0xffff; 221 if (val >= 1350000UL) 222 return 0x0004; 223 return ((1350000UL + val / 2) / val) << 2; 224 } 225 226 /* 227 * PWM registers and conversions 228 */ 229 230 #define PC87427_REG_PWM_ENABLE 0x10 231 #define PC87427_REG_PWM_DUTY 0x12 232 233 #define PWM_ENABLE_MODE_MASK (7 << 4) 234 #define PWM_ENABLE_CTLEN (1 << 0) 235 236 #define PWM_MODE_MANUAL (0 << 4) 237 #define PWM_MODE_AUTO (1 << 4) 238 #define PWM_MODE_OFF (2 << 4) 239 #define PWM_MODE_ON (7 << 4) 240 241 /* 242 * Dedicated function to read all registers related to a given PWM output. 243 * This saves us quite a few locks and bank selections. 244 * Must be called with data->lock held. 245 * nr is from 0 to 3 246 */ 247 static void pc87427_readall_pwm(struct pc87427_data *data, u8 nr) 248 { 249 int iobase = data->address[LD_FAN]; 250 251 outb(BANK_FC(nr), iobase + PC87427_REG_BANK); 252 data->pwm_enable[nr] = inb(iobase + PC87427_REG_PWM_ENABLE); 253 data->pwm[nr] = inb(iobase + PC87427_REG_PWM_DUTY); 254 } 255 256 static inline int pwm_enable_from_reg(u8 reg) 257 { 258 switch (reg & PWM_ENABLE_MODE_MASK) { 259 case PWM_MODE_ON: 260 return 0; 261 case PWM_MODE_MANUAL: 262 case PWM_MODE_OFF: 263 return 1; 264 case PWM_MODE_AUTO: 265 return 2; 266 default: 267 return -EPROTO; 268 } 269 } 270 271 static inline u8 pwm_enable_to_reg(unsigned long val, u8 pwmval) 272 { 273 switch (val) { 274 default: 275 return PWM_MODE_ON; 276 case 1: 277 return pwmval ? PWM_MODE_MANUAL : PWM_MODE_OFF; 278 case 2: 279 return PWM_MODE_AUTO; 280 } 281 } 282 283 /* 284 * Temperature registers and conversions 285 */ 286 287 #define PC87427_REG_TEMP_STATUS 0x10 288 #define PC87427_REG_TEMP 0x14 289 #define PC87427_REG_TEMP_MAX 0x18 290 #define PC87427_REG_TEMP_MIN 0x19 291 #define PC87427_REG_TEMP_CRIT 0x1a 292 #define PC87427_REG_TEMP_TYPE 0x1d 293 294 #define TEMP_STATUS_CHANEN (1 << 0) 295 #define TEMP_STATUS_LOWFLG (1 << 1) 296 #define TEMP_STATUS_HIGHFLG (1 << 2) 297 #define TEMP_STATUS_CRITFLG (1 << 3) 298 #define TEMP_STATUS_SENSERR (1 << 5) 299 #define TEMP_TYPE_MASK (3 << 5) 300 301 #define TEMP_TYPE_THERMISTOR (1 << 5) 302 #define TEMP_TYPE_REMOTE_DIODE (2 << 5) 303 #define TEMP_TYPE_LOCAL_DIODE (3 << 5) 304 305 /* 306 * Dedicated function to read all registers related to a given temperature 307 * input. This saves us quite a few locks and bank selections. 308 * Must be called with data->lock held. 309 * nr is from 0 to 5 310 */ 311 static void pc87427_readall_temp(struct pc87427_data *data, u8 nr) 312 { 313 int iobase = data->address[LD_TEMP]; 314 315 outb(BANK_TM(nr), iobase + PC87427_REG_BANK); 316 data->temp[nr] = le16_to_cpu(inw(iobase + PC87427_REG_TEMP)); 317 data->temp_max[nr] = inb(iobase + PC87427_REG_TEMP_MAX); 318 data->temp_min[nr] = inb(iobase + PC87427_REG_TEMP_MIN); 319 data->temp_crit[nr] = inb(iobase + PC87427_REG_TEMP_CRIT); 320 data->temp_type[nr] = inb(iobase + PC87427_REG_TEMP_TYPE); 321 data->temp_status[nr] = inb(iobase + PC87427_REG_TEMP_STATUS); 322 /* Clear fan alarm bits */ 323 outb(data->temp_status[nr], iobase + PC87427_REG_TEMP_STATUS); 324 } 325 326 static inline unsigned int temp_type_from_reg(u8 reg) 327 { 328 switch (reg & TEMP_TYPE_MASK) { 329 case TEMP_TYPE_THERMISTOR: 330 return 4; 331 case TEMP_TYPE_REMOTE_DIODE: 332 case TEMP_TYPE_LOCAL_DIODE: 333 return 3; 334 default: 335 return 0; 336 } 337 } 338 339 /* 340 * We assume 8-bit thermal sensors; 9-bit thermal sensors are possible 341 * too, but I have no idea how to figure out when they are used. 342 */ 343 static inline long temp_from_reg(s16 reg) 344 { 345 return reg * 1000 / 256; 346 } 347 348 static inline long temp_from_reg8(s8 reg) 349 { 350 return reg * 1000; 351 } 352 353 /* 354 * Data interface 355 */ 356 357 static struct pc87427_data *pc87427_update_device(struct device *dev) 358 { 359 struct pc87427_data *data = dev_get_drvdata(dev); 360 int i; 361 362 mutex_lock(&data->lock); 363 if (!time_after(jiffies, data->last_updated + HZ) 364 && data->last_updated) 365 goto done; 366 367 /* Fans */ 368 for (i = 0; i < 8; i++) { 369 if (!(data->fan_enabled & (1 << i))) 370 continue; 371 pc87427_readall_fan(data, i); 372 } 373 374 /* PWM outputs */ 375 for (i = 0; i < 4; i++) { 376 if (!(data->pwm_enabled & (1 << i))) 377 continue; 378 pc87427_readall_pwm(data, i); 379 } 380 381 /* Temperature channels */ 382 for (i = 0; i < 6; i++) { 383 if (!(data->temp_enabled & (1 << i))) 384 continue; 385 pc87427_readall_temp(data, i); 386 } 387 388 data->last_updated = jiffies; 389 390 done: 391 mutex_unlock(&data->lock); 392 return data; 393 } 394 395 static ssize_t fan_input_show(struct device *dev, 396 struct device_attribute *devattr, char *buf) 397 { 398 struct pc87427_data *data = pc87427_update_device(dev); 399 int nr = to_sensor_dev_attr(devattr)->index; 400 401 return sprintf(buf, "%lu\n", fan_from_reg(data->fan[nr])); 402 } 403 404 static ssize_t fan_min_show(struct device *dev, 405 struct device_attribute *devattr, char *buf) 406 { 407 struct pc87427_data *data = pc87427_update_device(dev); 408 int nr = to_sensor_dev_attr(devattr)->index; 409 410 return sprintf(buf, "%lu\n", fan_from_reg(data->fan_min[nr])); 411 } 412 413 static ssize_t fan_alarm_show(struct device *dev, 414 struct device_attribute *devattr, char *buf) 415 { 416 struct pc87427_data *data = pc87427_update_device(dev); 417 int nr = to_sensor_dev_attr(devattr)->index; 418 419 return sprintf(buf, "%d\n", !!(data->fan_status[nr] 420 & FAN_STATUS_LOSPD)); 421 } 422 423 static ssize_t fan_fault_show(struct device *dev, 424 struct device_attribute *devattr, char *buf) 425 { 426 struct pc87427_data *data = pc87427_update_device(dev); 427 int nr = to_sensor_dev_attr(devattr)->index; 428 429 return sprintf(buf, "%d\n", !!(data->fan_status[nr] 430 & FAN_STATUS_STALL)); 431 } 432 433 static ssize_t fan_min_store(struct device *dev, 434 struct device_attribute *devattr, 435 const char *buf, size_t count) 436 { 437 struct pc87427_data *data = dev_get_drvdata(dev); 438 int nr = to_sensor_dev_attr(devattr)->index; 439 unsigned long val; 440 int iobase = data->address[LD_FAN]; 441 442 if (kstrtoul(buf, 10, &val) < 0) 443 return -EINVAL; 444 445 mutex_lock(&data->lock); 446 outb(BANK_FM(nr), iobase + PC87427_REG_BANK); 447 /* 448 * The low speed limit registers are read-only while monitoring 449 * is enabled, so we have to disable monitoring, then change the 450 * limit, and finally enable monitoring again. 451 */ 452 outb(0, iobase + PC87427_REG_FAN_STATUS); 453 data->fan_min[nr] = fan_to_reg(val); 454 outw(data->fan_min[nr], iobase + PC87427_REG_FAN_MIN); 455 outb(FAN_STATUS_MONEN, iobase + PC87427_REG_FAN_STATUS); 456 mutex_unlock(&data->lock); 457 458 return count; 459 } 460 461 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0); 462 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1); 463 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan_input, 2); 464 static SENSOR_DEVICE_ATTR_RO(fan4_input, fan_input, 3); 465 static SENSOR_DEVICE_ATTR_RO(fan5_input, fan_input, 4); 466 static SENSOR_DEVICE_ATTR_RO(fan6_input, fan_input, 5); 467 static SENSOR_DEVICE_ATTR_RO(fan7_input, fan_input, 6); 468 static SENSOR_DEVICE_ATTR_RO(fan8_input, fan_input, 7); 469 470 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0); 471 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1); 472 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2); 473 static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3); 474 static SENSOR_DEVICE_ATTR_RW(fan5_min, fan_min, 4); 475 static SENSOR_DEVICE_ATTR_RW(fan6_min, fan_min, 5); 476 static SENSOR_DEVICE_ATTR_RW(fan7_min, fan_min, 6); 477 static SENSOR_DEVICE_ATTR_RW(fan8_min, fan_min, 7); 478 479 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0); 480 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1); 481 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, fan_alarm, 2); 482 static SENSOR_DEVICE_ATTR_RO(fan4_alarm, fan_alarm, 3); 483 static SENSOR_DEVICE_ATTR_RO(fan5_alarm, fan_alarm, 4); 484 static SENSOR_DEVICE_ATTR_RO(fan6_alarm, fan_alarm, 5); 485 static SENSOR_DEVICE_ATTR_RO(fan7_alarm, fan_alarm, 6); 486 static SENSOR_DEVICE_ATTR_RO(fan8_alarm, fan_alarm, 7); 487 488 static SENSOR_DEVICE_ATTR_RO(fan1_fault, fan_fault, 0); 489 static SENSOR_DEVICE_ATTR_RO(fan2_fault, fan_fault, 1); 490 static SENSOR_DEVICE_ATTR_RO(fan3_fault, fan_fault, 2); 491 static SENSOR_DEVICE_ATTR_RO(fan4_fault, fan_fault, 3); 492 static SENSOR_DEVICE_ATTR_RO(fan5_fault, fan_fault, 4); 493 static SENSOR_DEVICE_ATTR_RO(fan6_fault, fan_fault, 5); 494 static SENSOR_DEVICE_ATTR_RO(fan7_fault, fan_fault, 6); 495 static SENSOR_DEVICE_ATTR_RO(fan8_fault, fan_fault, 7); 496 497 static struct attribute *pc87427_attributes_fan[8][5] = { 498 { 499 &sensor_dev_attr_fan1_input.dev_attr.attr, 500 &sensor_dev_attr_fan1_min.dev_attr.attr, 501 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 502 &sensor_dev_attr_fan1_fault.dev_attr.attr, 503 NULL 504 }, { 505 &sensor_dev_attr_fan2_input.dev_attr.attr, 506 &sensor_dev_attr_fan2_min.dev_attr.attr, 507 &sensor_dev_attr_fan2_alarm.dev_attr.attr, 508 &sensor_dev_attr_fan2_fault.dev_attr.attr, 509 NULL 510 }, { 511 &sensor_dev_attr_fan3_input.dev_attr.attr, 512 &sensor_dev_attr_fan3_min.dev_attr.attr, 513 &sensor_dev_attr_fan3_alarm.dev_attr.attr, 514 &sensor_dev_attr_fan3_fault.dev_attr.attr, 515 NULL 516 }, { 517 &sensor_dev_attr_fan4_input.dev_attr.attr, 518 &sensor_dev_attr_fan4_min.dev_attr.attr, 519 &sensor_dev_attr_fan4_alarm.dev_attr.attr, 520 &sensor_dev_attr_fan4_fault.dev_attr.attr, 521 NULL 522 }, { 523 &sensor_dev_attr_fan5_input.dev_attr.attr, 524 &sensor_dev_attr_fan5_min.dev_attr.attr, 525 &sensor_dev_attr_fan5_alarm.dev_attr.attr, 526 &sensor_dev_attr_fan5_fault.dev_attr.attr, 527 NULL 528 }, { 529 &sensor_dev_attr_fan6_input.dev_attr.attr, 530 &sensor_dev_attr_fan6_min.dev_attr.attr, 531 &sensor_dev_attr_fan6_alarm.dev_attr.attr, 532 &sensor_dev_attr_fan6_fault.dev_attr.attr, 533 NULL 534 }, { 535 &sensor_dev_attr_fan7_input.dev_attr.attr, 536 &sensor_dev_attr_fan7_min.dev_attr.attr, 537 &sensor_dev_attr_fan7_alarm.dev_attr.attr, 538 &sensor_dev_attr_fan7_fault.dev_attr.attr, 539 NULL 540 }, { 541 &sensor_dev_attr_fan8_input.dev_attr.attr, 542 &sensor_dev_attr_fan8_min.dev_attr.attr, 543 &sensor_dev_attr_fan8_alarm.dev_attr.attr, 544 &sensor_dev_attr_fan8_fault.dev_attr.attr, 545 NULL 546 } 547 }; 548 549 static const struct attribute_group pc87427_group_fan[8] = { 550 { .attrs = pc87427_attributes_fan[0] }, 551 { .attrs = pc87427_attributes_fan[1] }, 552 { .attrs = pc87427_attributes_fan[2] }, 553 { .attrs = pc87427_attributes_fan[3] }, 554 { .attrs = pc87427_attributes_fan[4] }, 555 { .attrs = pc87427_attributes_fan[5] }, 556 { .attrs = pc87427_attributes_fan[6] }, 557 { .attrs = pc87427_attributes_fan[7] }, 558 }; 559 560 /* 561 * Must be called with data->lock held and pc87427_readall_pwm() freshly 562 * called 563 */ 564 static void update_pwm_enable(struct pc87427_data *data, int nr, u8 mode) 565 { 566 int iobase = data->address[LD_FAN]; 567 data->pwm_enable[nr] &= ~PWM_ENABLE_MODE_MASK; 568 data->pwm_enable[nr] |= mode; 569 outb(data->pwm_enable[nr], iobase + PC87427_REG_PWM_ENABLE); 570 } 571 572 static ssize_t pwm_enable_show(struct device *dev, 573 struct device_attribute *devattr, char *buf) 574 { 575 struct pc87427_data *data = pc87427_update_device(dev); 576 int nr = to_sensor_dev_attr(devattr)->index; 577 int pwm_enable; 578 579 pwm_enable = pwm_enable_from_reg(data->pwm_enable[nr]); 580 if (pwm_enable < 0) 581 return pwm_enable; 582 return sprintf(buf, "%d\n", pwm_enable); 583 } 584 585 static ssize_t pwm_enable_store(struct device *dev, 586 struct device_attribute *devattr, 587 const char *buf, size_t count) 588 { 589 struct pc87427_data *data = dev_get_drvdata(dev); 590 int nr = to_sensor_dev_attr(devattr)->index; 591 unsigned long val; 592 593 if (kstrtoul(buf, 10, &val) < 0 || val > 2) 594 return -EINVAL; 595 /* Can't go to automatic mode if it isn't configured */ 596 if (val == 2 && !(data->pwm_auto_ok & (1 << nr))) 597 return -EINVAL; 598 599 mutex_lock(&data->lock); 600 pc87427_readall_pwm(data, nr); 601 update_pwm_enable(data, nr, pwm_enable_to_reg(val, data->pwm[nr])); 602 mutex_unlock(&data->lock); 603 604 return count; 605 } 606 607 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr, 608 char *buf) 609 { 610 struct pc87427_data *data = pc87427_update_device(dev); 611 int nr = to_sensor_dev_attr(devattr)->index; 612 613 return sprintf(buf, "%d\n", (int)data->pwm[nr]); 614 } 615 616 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr, 617 const char *buf, size_t count) 618 { 619 struct pc87427_data *data = dev_get_drvdata(dev); 620 int nr = to_sensor_dev_attr(devattr)->index; 621 unsigned long val; 622 int iobase = data->address[LD_FAN]; 623 u8 mode; 624 625 if (kstrtoul(buf, 10, &val) < 0 || val > 0xff) 626 return -EINVAL; 627 628 mutex_lock(&data->lock); 629 pc87427_readall_pwm(data, nr); 630 mode = data->pwm_enable[nr] & PWM_ENABLE_MODE_MASK; 631 if (mode != PWM_MODE_MANUAL && mode != PWM_MODE_OFF) { 632 dev_notice(dev, 633 "Can't set PWM%d duty cycle while not in manual mode\n", 634 nr + 1); 635 mutex_unlock(&data->lock); 636 return -EPERM; 637 } 638 639 /* We may have to change the mode */ 640 if (mode == PWM_MODE_MANUAL && val == 0) { 641 /* Transition from Manual to Off */ 642 update_pwm_enable(data, nr, PWM_MODE_OFF); 643 mode = PWM_MODE_OFF; 644 dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1, 645 "manual", "off"); 646 } else if (mode == PWM_MODE_OFF && val != 0) { 647 /* Transition from Off to Manual */ 648 update_pwm_enable(data, nr, PWM_MODE_MANUAL); 649 mode = PWM_MODE_MANUAL; 650 dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1, 651 "off", "manual"); 652 } 653 654 data->pwm[nr] = val; 655 if (mode == PWM_MODE_MANUAL) 656 outb(val, iobase + PC87427_REG_PWM_DUTY); 657 mutex_unlock(&data->lock); 658 659 return count; 660 } 661 662 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0); 663 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1); 664 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2); 665 static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_enable, 3); 666 667 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); 668 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1); 669 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2); 670 static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3); 671 672 static struct attribute *pc87427_attributes_pwm[4][3] = { 673 { 674 &sensor_dev_attr_pwm1_enable.dev_attr.attr, 675 &sensor_dev_attr_pwm1.dev_attr.attr, 676 NULL 677 }, { 678 &sensor_dev_attr_pwm2_enable.dev_attr.attr, 679 &sensor_dev_attr_pwm2.dev_attr.attr, 680 NULL 681 }, { 682 &sensor_dev_attr_pwm3_enable.dev_attr.attr, 683 &sensor_dev_attr_pwm3.dev_attr.attr, 684 NULL 685 }, { 686 &sensor_dev_attr_pwm4_enable.dev_attr.attr, 687 &sensor_dev_attr_pwm4.dev_attr.attr, 688 NULL 689 } 690 }; 691 692 static const struct attribute_group pc87427_group_pwm[4] = { 693 { .attrs = pc87427_attributes_pwm[0] }, 694 { .attrs = pc87427_attributes_pwm[1] }, 695 { .attrs = pc87427_attributes_pwm[2] }, 696 { .attrs = pc87427_attributes_pwm[3] }, 697 }; 698 699 static ssize_t temp_input_show(struct device *dev, 700 struct device_attribute *devattr, char *buf) 701 { 702 struct pc87427_data *data = pc87427_update_device(dev); 703 int nr = to_sensor_dev_attr(devattr)->index; 704 705 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr])); 706 } 707 708 static ssize_t temp_min_show(struct device *dev, 709 struct device_attribute *devattr, char *buf) 710 { 711 struct pc87427_data *data = pc87427_update_device(dev); 712 int nr = to_sensor_dev_attr(devattr)->index; 713 714 return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_min[nr])); 715 } 716 717 static ssize_t temp_max_show(struct device *dev, 718 struct device_attribute *devattr, char *buf) 719 { 720 struct pc87427_data *data = pc87427_update_device(dev); 721 int nr = to_sensor_dev_attr(devattr)->index; 722 723 return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_max[nr])); 724 } 725 726 static ssize_t temp_crit_show(struct device *dev, 727 struct device_attribute *devattr, char *buf) 728 { 729 struct pc87427_data *data = pc87427_update_device(dev); 730 int nr = to_sensor_dev_attr(devattr)->index; 731 732 return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_crit[nr])); 733 } 734 735 static ssize_t temp_type_show(struct device *dev, 736 struct device_attribute *devattr, char *buf) 737 { 738 struct pc87427_data *data = pc87427_update_device(dev); 739 int nr = to_sensor_dev_attr(devattr)->index; 740 741 return sprintf(buf, "%u\n", temp_type_from_reg(data->temp_type[nr])); 742 } 743 744 static ssize_t temp_min_alarm_show(struct device *dev, 745 struct device_attribute *devattr, 746 char *buf) 747 { 748 struct pc87427_data *data = pc87427_update_device(dev); 749 int nr = to_sensor_dev_attr(devattr)->index; 750 751 return sprintf(buf, "%d\n", !!(data->temp_status[nr] 752 & TEMP_STATUS_LOWFLG)); 753 } 754 755 static ssize_t temp_max_alarm_show(struct device *dev, 756 struct device_attribute *devattr, 757 char *buf) 758 { 759 struct pc87427_data *data = pc87427_update_device(dev); 760 int nr = to_sensor_dev_attr(devattr)->index; 761 762 return sprintf(buf, "%d\n", !!(data->temp_status[nr] 763 & TEMP_STATUS_HIGHFLG)); 764 } 765 766 static ssize_t temp_crit_alarm_show(struct device *dev, 767 struct device_attribute *devattr, 768 char *buf) 769 { 770 struct pc87427_data *data = pc87427_update_device(dev); 771 int nr = to_sensor_dev_attr(devattr)->index; 772 773 return sprintf(buf, "%d\n", !!(data->temp_status[nr] 774 & TEMP_STATUS_CRITFLG)); 775 } 776 777 static ssize_t temp_fault_show(struct device *dev, 778 struct device_attribute *devattr, char *buf) 779 { 780 struct pc87427_data *data = pc87427_update_device(dev); 781 int nr = to_sensor_dev_attr(devattr)->index; 782 783 return sprintf(buf, "%d\n", !!(data->temp_status[nr] 784 & TEMP_STATUS_SENSERR)); 785 } 786 787 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); 788 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1); 789 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp_input, 2); 790 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp_input, 3); 791 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp_input, 4); 792 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp_input, 5); 793 794 static SENSOR_DEVICE_ATTR_RO(temp1_min, temp_min, 0); 795 static SENSOR_DEVICE_ATTR_RO(temp2_min, temp_min, 1); 796 static SENSOR_DEVICE_ATTR_RO(temp3_min, temp_min, 2); 797 static SENSOR_DEVICE_ATTR_RO(temp4_min, temp_min, 3); 798 static SENSOR_DEVICE_ATTR_RO(temp5_min, temp_min, 4); 799 static SENSOR_DEVICE_ATTR_RO(temp6_min, temp_min, 5); 800 801 static SENSOR_DEVICE_ATTR_RO(temp1_max, temp_max, 0); 802 static SENSOR_DEVICE_ATTR_RO(temp2_max, temp_max, 1); 803 static SENSOR_DEVICE_ATTR_RO(temp3_max, temp_max, 2); 804 static SENSOR_DEVICE_ATTR_RO(temp4_max, temp_max, 3); 805 static SENSOR_DEVICE_ATTR_RO(temp5_max, temp_max, 4); 806 static SENSOR_DEVICE_ATTR_RO(temp6_max, temp_max, 5); 807 808 static SENSOR_DEVICE_ATTR_RO(temp1_crit, temp_crit, 0); 809 static SENSOR_DEVICE_ATTR_RO(temp2_crit, temp_crit, 1); 810 static SENSOR_DEVICE_ATTR_RO(temp3_crit, temp_crit, 2); 811 static SENSOR_DEVICE_ATTR_RO(temp4_crit, temp_crit, 3); 812 static SENSOR_DEVICE_ATTR_RO(temp5_crit, temp_crit, 4); 813 static SENSOR_DEVICE_ATTR_RO(temp6_crit, temp_crit, 5); 814 815 static SENSOR_DEVICE_ATTR_RO(temp1_type, temp_type, 0); 816 static SENSOR_DEVICE_ATTR_RO(temp2_type, temp_type, 1); 817 static SENSOR_DEVICE_ATTR_RO(temp3_type, temp_type, 2); 818 static SENSOR_DEVICE_ATTR_RO(temp4_type, temp_type, 3); 819 static SENSOR_DEVICE_ATTR_RO(temp5_type, temp_type, 4); 820 static SENSOR_DEVICE_ATTR_RO(temp6_type, temp_type, 5); 821 822 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0); 823 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1); 824 static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2); 825 static SENSOR_DEVICE_ATTR_RO(temp4_min_alarm, temp_min_alarm, 3); 826 static SENSOR_DEVICE_ATTR_RO(temp5_min_alarm, temp_min_alarm, 4); 827 static SENSOR_DEVICE_ATTR_RO(temp6_min_alarm, temp_min_alarm, 5); 828 829 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0); 830 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1); 831 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2); 832 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, temp_max_alarm, 3); 833 static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, temp_max_alarm, 4); 834 static SENSOR_DEVICE_ATTR_RO(temp6_max_alarm, temp_max_alarm, 5); 835 836 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, temp_crit_alarm, 0); 837 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, temp_crit_alarm, 1); 838 static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, temp_crit_alarm, 2); 839 static SENSOR_DEVICE_ATTR_RO(temp4_crit_alarm, temp_crit_alarm, 3); 840 static SENSOR_DEVICE_ATTR_RO(temp5_crit_alarm, temp_crit_alarm, 4); 841 static SENSOR_DEVICE_ATTR_RO(temp6_crit_alarm, temp_crit_alarm, 5); 842 843 static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0); 844 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1); 845 static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2); 846 static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3); 847 static SENSOR_DEVICE_ATTR_RO(temp5_fault, temp_fault, 4); 848 static SENSOR_DEVICE_ATTR_RO(temp6_fault, temp_fault, 5); 849 850 static struct attribute *pc87427_attributes_temp[6][10] = { 851 { 852 &sensor_dev_attr_temp1_input.dev_attr.attr, 853 &sensor_dev_attr_temp1_min.dev_attr.attr, 854 &sensor_dev_attr_temp1_max.dev_attr.attr, 855 &sensor_dev_attr_temp1_crit.dev_attr.attr, 856 &sensor_dev_attr_temp1_type.dev_attr.attr, 857 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 858 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 859 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 860 &sensor_dev_attr_temp1_fault.dev_attr.attr, 861 NULL 862 }, { 863 &sensor_dev_attr_temp2_input.dev_attr.attr, 864 &sensor_dev_attr_temp2_min.dev_attr.attr, 865 &sensor_dev_attr_temp2_max.dev_attr.attr, 866 &sensor_dev_attr_temp2_crit.dev_attr.attr, 867 &sensor_dev_attr_temp2_type.dev_attr.attr, 868 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, 869 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 870 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 871 &sensor_dev_attr_temp2_fault.dev_attr.attr, 872 NULL 873 }, { 874 &sensor_dev_attr_temp3_input.dev_attr.attr, 875 &sensor_dev_attr_temp3_min.dev_attr.attr, 876 &sensor_dev_attr_temp3_max.dev_attr.attr, 877 &sensor_dev_attr_temp3_crit.dev_attr.attr, 878 &sensor_dev_attr_temp3_type.dev_attr.attr, 879 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, 880 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, 881 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, 882 &sensor_dev_attr_temp3_fault.dev_attr.attr, 883 NULL 884 }, { 885 &sensor_dev_attr_temp4_input.dev_attr.attr, 886 &sensor_dev_attr_temp4_min.dev_attr.attr, 887 &sensor_dev_attr_temp4_max.dev_attr.attr, 888 &sensor_dev_attr_temp4_crit.dev_attr.attr, 889 &sensor_dev_attr_temp4_type.dev_attr.attr, 890 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, 891 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, 892 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, 893 &sensor_dev_attr_temp4_fault.dev_attr.attr, 894 NULL 895 }, { 896 &sensor_dev_attr_temp5_input.dev_attr.attr, 897 &sensor_dev_attr_temp5_min.dev_attr.attr, 898 &sensor_dev_attr_temp5_max.dev_attr.attr, 899 &sensor_dev_attr_temp5_crit.dev_attr.attr, 900 &sensor_dev_attr_temp5_type.dev_attr.attr, 901 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr, 902 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, 903 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, 904 &sensor_dev_attr_temp5_fault.dev_attr.attr, 905 NULL 906 }, { 907 &sensor_dev_attr_temp6_input.dev_attr.attr, 908 &sensor_dev_attr_temp6_min.dev_attr.attr, 909 &sensor_dev_attr_temp6_max.dev_attr.attr, 910 &sensor_dev_attr_temp6_crit.dev_attr.attr, 911 &sensor_dev_attr_temp6_type.dev_attr.attr, 912 &sensor_dev_attr_temp6_min_alarm.dev_attr.attr, 913 &sensor_dev_attr_temp6_max_alarm.dev_attr.attr, 914 &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr, 915 &sensor_dev_attr_temp6_fault.dev_attr.attr, 916 NULL 917 } 918 }; 919 920 static const struct attribute_group pc87427_group_temp[6] = { 921 { .attrs = pc87427_attributes_temp[0] }, 922 { .attrs = pc87427_attributes_temp[1] }, 923 { .attrs = pc87427_attributes_temp[2] }, 924 { .attrs = pc87427_attributes_temp[3] }, 925 { .attrs = pc87427_attributes_temp[4] }, 926 { .attrs = pc87427_attributes_temp[5] }, 927 }; 928 929 static ssize_t name_show(struct device *dev, struct device_attribute 930 *devattr, char *buf) 931 { 932 struct pc87427_data *data = dev_get_drvdata(dev); 933 934 return sprintf(buf, "%s\n", data->name); 935 } 936 static DEVICE_ATTR_RO(name); 937 938 939 /* 940 * Device detection, attach and detach 941 */ 942 943 static int pc87427_request_regions(struct platform_device *pdev, 944 int count) 945 { 946 struct resource *res; 947 int i; 948 949 for (i = 0; i < count; i++) { 950 res = platform_get_resource(pdev, IORESOURCE_IO, i); 951 if (!res) { 952 dev_err(&pdev->dev, "Missing resource #%d\n", i); 953 return -ENOENT; 954 } 955 if (!devm_request_region(&pdev->dev, res->start, 956 resource_size(res), DRVNAME)) { 957 dev_err(&pdev->dev, 958 "Failed to request region 0x%lx-0x%lx\n", 959 (unsigned long)res->start, 960 (unsigned long)res->end); 961 return -EBUSY; 962 } 963 } 964 return 0; 965 } 966 967 static void pc87427_init_device(struct device *dev) 968 { 969 struct pc87427_sio_data *sio_data = dev_get_platdata(dev); 970 struct pc87427_data *data = dev_get_drvdata(dev); 971 int i; 972 u8 reg; 973 974 /* The FMC module should be ready */ 975 reg = pc87427_read8(data, LD_FAN, PC87427_REG_BANK); 976 if (!(reg & 0x80)) 977 dev_warn(dev, "%s module not ready!\n", "FMC"); 978 979 /* Check which fans are enabled */ 980 for (i = 0; i < 8; i++) { 981 if (!(sio_data->has_fanin & (1 << i))) /* Not wired */ 982 continue; 983 reg = pc87427_read8_bank(data, LD_FAN, BANK_FM(i), 984 PC87427_REG_FAN_STATUS); 985 if (reg & FAN_STATUS_MONEN) 986 data->fan_enabled |= (1 << i); 987 } 988 989 if (!data->fan_enabled) { 990 dev_dbg(dev, "Enabling monitoring of all fans\n"); 991 for (i = 0; i < 8; i++) { 992 if (!(sio_data->has_fanin & (1 << i))) /* Not wired */ 993 continue; 994 pc87427_write8_bank(data, LD_FAN, BANK_FM(i), 995 PC87427_REG_FAN_STATUS, 996 FAN_STATUS_MONEN); 997 } 998 data->fan_enabled = sio_data->has_fanin; 999 } 1000 1001 /* Check which PWM outputs are enabled */ 1002 for (i = 0; i < 4; i++) { 1003 if (!(sio_data->has_fanout & (1 << i))) /* Not wired */ 1004 continue; 1005 reg = pc87427_read8_bank(data, LD_FAN, BANK_FC(i), 1006 PC87427_REG_PWM_ENABLE); 1007 if (reg & PWM_ENABLE_CTLEN) 1008 data->pwm_enabled |= (1 << i); 1009 1010 /* 1011 * We don't expose an interface to reconfigure the automatic 1012 * fan control mode, so only allow to return to this mode if 1013 * it was originally set. 1014 */ 1015 if ((reg & PWM_ENABLE_MODE_MASK) == PWM_MODE_AUTO) { 1016 dev_dbg(dev, "PWM%d is in automatic control mode\n", 1017 i + 1); 1018 data->pwm_auto_ok |= (1 << i); 1019 } 1020 } 1021 1022 /* The HMC module should be ready */ 1023 reg = pc87427_read8(data, LD_TEMP, PC87427_REG_BANK); 1024 if (!(reg & 0x80)) 1025 dev_warn(dev, "%s module not ready!\n", "HMC"); 1026 1027 /* Check which temperature channels are enabled */ 1028 for (i = 0; i < 6; i++) { 1029 reg = pc87427_read8_bank(data, LD_TEMP, BANK_TM(i), 1030 PC87427_REG_TEMP_STATUS); 1031 if (reg & TEMP_STATUS_CHANEN) 1032 data->temp_enabled |= (1 << i); 1033 } 1034 } 1035 1036 static void pc87427_remove_files(struct device *dev) 1037 { 1038 struct pc87427_data *data = dev_get_drvdata(dev); 1039 int i; 1040 1041 device_remove_file(dev, &dev_attr_name); 1042 for (i = 0; i < 8; i++) { 1043 if (!(data->fan_enabled & (1 << i))) 1044 continue; 1045 sysfs_remove_group(&dev->kobj, &pc87427_group_fan[i]); 1046 } 1047 for (i = 0; i < 4; i++) { 1048 if (!(data->pwm_enabled & (1 << i))) 1049 continue; 1050 sysfs_remove_group(&dev->kobj, &pc87427_group_pwm[i]); 1051 } 1052 for (i = 0; i < 6; i++) { 1053 if (!(data->temp_enabled & (1 << i))) 1054 continue; 1055 sysfs_remove_group(&dev->kobj, &pc87427_group_temp[i]); 1056 } 1057 } 1058 1059 static int pc87427_probe(struct platform_device *pdev) 1060 { 1061 struct pc87427_sio_data *sio_data = dev_get_platdata(&pdev->dev); 1062 struct pc87427_data *data; 1063 int i, err, res_count; 1064 1065 data = devm_kzalloc(&pdev->dev, sizeof(struct pc87427_data), 1066 GFP_KERNEL); 1067 if (!data) 1068 return -ENOMEM; 1069 1070 data->address[0] = sio_data->address[0]; 1071 data->address[1] = sio_data->address[1]; 1072 res_count = (data->address[0] != 0) + (data->address[1] != 0); 1073 1074 err = pc87427_request_regions(pdev, res_count); 1075 if (err) 1076 return err; 1077 1078 mutex_init(&data->lock); 1079 data->name = "pc87427"; 1080 platform_set_drvdata(pdev, data); 1081 pc87427_init_device(&pdev->dev); 1082 1083 /* Register sysfs hooks */ 1084 err = device_create_file(&pdev->dev, &dev_attr_name); 1085 if (err) 1086 return err; 1087 for (i = 0; i < 8; i++) { 1088 if (!(data->fan_enabled & (1 << i))) 1089 continue; 1090 err = sysfs_create_group(&pdev->dev.kobj, 1091 &pc87427_group_fan[i]); 1092 if (err) 1093 goto exit_remove_files; 1094 } 1095 for (i = 0; i < 4; i++) { 1096 if (!(data->pwm_enabled & (1 << i))) 1097 continue; 1098 err = sysfs_create_group(&pdev->dev.kobj, 1099 &pc87427_group_pwm[i]); 1100 if (err) 1101 goto exit_remove_files; 1102 } 1103 for (i = 0; i < 6; i++) { 1104 if (!(data->temp_enabled & (1 << i))) 1105 continue; 1106 err = sysfs_create_group(&pdev->dev.kobj, 1107 &pc87427_group_temp[i]); 1108 if (err) 1109 goto exit_remove_files; 1110 } 1111 1112 data->hwmon_dev = hwmon_device_register(&pdev->dev); 1113 if (IS_ERR(data->hwmon_dev)) { 1114 err = PTR_ERR(data->hwmon_dev); 1115 dev_err(&pdev->dev, "Class registration failed (%d)\n", err); 1116 goto exit_remove_files; 1117 } 1118 1119 return 0; 1120 1121 exit_remove_files: 1122 pc87427_remove_files(&pdev->dev); 1123 return err; 1124 } 1125 1126 static int pc87427_remove(struct platform_device *pdev) 1127 { 1128 struct pc87427_data *data = platform_get_drvdata(pdev); 1129 1130 hwmon_device_unregister(data->hwmon_dev); 1131 pc87427_remove_files(&pdev->dev); 1132 1133 return 0; 1134 } 1135 1136 1137 static struct platform_driver pc87427_driver = { 1138 .driver = { 1139 .name = DRVNAME, 1140 }, 1141 .probe = pc87427_probe, 1142 .remove = pc87427_remove, 1143 }; 1144 1145 static int __init pc87427_device_add(const struct pc87427_sio_data *sio_data) 1146 { 1147 struct resource res[2] = { 1148 { .flags = IORESOURCE_IO }, 1149 { .flags = IORESOURCE_IO }, 1150 }; 1151 int err, i, res_count; 1152 1153 res_count = 0; 1154 for (i = 0; i < 2; i++) { 1155 if (!sio_data->address[i]) 1156 continue; 1157 res[res_count].start = sio_data->address[i]; 1158 res[res_count].end = sio_data->address[i] + REGION_LENGTH - 1; 1159 res[res_count].name = logdev_str[i]; 1160 1161 err = acpi_check_resource_conflict(&res[res_count]); 1162 if (err) 1163 goto exit; 1164 1165 res_count++; 1166 } 1167 1168 pdev = platform_device_alloc(DRVNAME, res[0].start); 1169 if (!pdev) { 1170 err = -ENOMEM; 1171 pr_err("Device allocation failed\n"); 1172 goto exit; 1173 } 1174 1175 err = platform_device_add_resources(pdev, res, res_count); 1176 if (err) { 1177 pr_err("Device resource addition failed (%d)\n", err); 1178 goto exit_device_put; 1179 } 1180 1181 err = platform_device_add_data(pdev, sio_data, 1182 sizeof(struct pc87427_sio_data)); 1183 if (err) { 1184 pr_err("Platform data allocation failed\n"); 1185 goto exit_device_put; 1186 } 1187 1188 err = platform_device_add(pdev); 1189 if (err) { 1190 pr_err("Device addition failed (%d)\n", err); 1191 goto exit_device_put; 1192 } 1193 1194 return 0; 1195 1196 exit_device_put: 1197 platform_device_put(pdev); 1198 exit: 1199 return err; 1200 } 1201 1202 static int __init pc87427_find(int sioaddr, struct pc87427_sio_data *sio_data) 1203 { 1204 u16 val; 1205 u8 cfg, cfg_b; 1206 int i, err; 1207 1208 err = superio_enter(sioaddr); 1209 if (err) 1210 return err; 1211 1212 /* Identify device */ 1213 val = force_id ? force_id : superio_inb(sioaddr, SIOREG_DEVID); 1214 if (val != 0xf2) { /* PC87427 */ 1215 err = -ENODEV; 1216 goto exit; 1217 } 1218 1219 for (i = 0; i < 2; i++) { 1220 sio_data->address[i] = 0; 1221 /* Select logical device */ 1222 superio_outb(sioaddr, SIOREG_LDSEL, logdev[i]); 1223 1224 val = superio_inb(sioaddr, SIOREG_ACT); 1225 if (!(val & 0x01)) { 1226 pr_info("Logical device 0x%02x not activated\n", 1227 logdev[i]); 1228 continue; 1229 } 1230 1231 val = superio_inb(sioaddr, SIOREG_MAP); 1232 if (val & 0x01) { 1233 pr_warn("Logical device 0x%02x is memory-mapped, can't use\n", 1234 logdev[i]); 1235 continue; 1236 } 1237 1238 val = (superio_inb(sioaddr, SIOREG_IOBASE) << 8) 1239 | superio_inb(sioaddr, SIOREG_IOBASE + 1); 1240 if (!val) { 1241 pr_info("I/O base address not set for logical device 0x%02x\n", 1242 logdev[i]); 1243 continue; 1244 } 1245 sio_data->address[i] = val; 1246 } 1247 1248 /* No point in loading the driver if everything is disabled */ 1249 if (!sio_data->address[0] && !sio_data->address[1]) { 1250 err = -ENODEV; 1251 goto exit; 1252 } 1253 1254 /* Check which fan inputs are wired */ 1255 sio_data->has_fanin = (1 << 2) | (1 << 3); /* FANIN2, FANIN3 */ 1256 1257 cfg = superio_inb(sioaddr, SIOREG_CF2); 1258 if (!(cfg & (1 << 3))) 1259 sio_data->has_fanin |= (1 << 0); /* FANIN0 */ 1260 if (!(cfg & (1 << 2))) 1261 sio_data->has_fanin |= (1 << 4); /* FANIN4 */ 1262 1263 cfg = superio_inb(sioaddr, SIOREG_CFD); 1264 if (!(cfg & (1 << 0))) 1265 sio_data->has_fanin |= (1 << 1); /* FANIN1 */ 1266 1267 cfg = superio_inb(sioaddr, SIOREG_CF4); 1268 if (!(cfg & (1 << 0))) 1269 sio_data->has_fanin |= (1 << 7); /* FANIN7 */ 1270 cfg_b = superio_inb(sioaddr, SIOREG_CFB); 1271 if (!(cfg & (1 << 1)) && (cfg_b & (1 << 3))) 1272 sio_data->has_fanin |= (1 << 5); /* FANIN5 */ 1273 cfg = superio_inb(sioaddr, SIOREG_CF3); 1274 if ((cfg & (1 << 3)) && !(cfg_b & (1 << 5))) 1275 sio_data->has_fanin |= (1 << 6); /* FANIN6 */ 1276 1277 /* Check which fan outputs are wired */ 1278 sio_data->has_fanout = (1 << 0); /* FANOUT0 */ 1279 if (cfg_b & (1 << 0)) 1280 sio_data->has_fanout |= (1 << 3); /* FANOUT3 */ 1281 1282 cfg = superio_inb(sioaddr, SIOREG_CFC); 1283 if (!(cfg & (1 << 4))) { 1284 if (cfg_b & (1 << 1)) 1285 sio_data->has_fanout |= (1 << 1); /* FANOUT1 */ 1286 if (cfg_b & (1 << 2)) 1287 sio_data->has_fanout |= (1 << 2); /* FANOUT2 */ 1288 } 1289 1290 /* FANOUT1 and FANOUT2 can each be routed to 2 different pins */ 1291 cfg = superio_inb(sioaddr, SIOREG_CF5); 1292 if (cfg & (1 << 6)) 1293 sio_data->has_fanout |= (1 << 1); /* FANOUT1 */ 1294 if (cfg & (1 << 5)) 1295 sio_data->has_fanout |= (1 << 2); /* FANOUT2 */ 1296 1297 exit: 1298 superio_exit(sioaddr); 1299 return err; 1300 } 1301 1302 static int __init pc87427_init(void) 1303 { 1304 int err; 1305 struct pc87427_sio_data sio_data; 1306 1307 if (pc87427_find(0x2e, &sio_data) 1308 && pc87427_find(0x4e, &sio_data)) 1309 return -ENODEV; 1310 1311 err = platform_driver_register(&pc87427_driver); 1312 if (err) 1313 goto exit; 1314 1315 /* Sets global pdev as a side effect */ 1316 err = pc87427_device_add(&sio_data); 1317 if (err) 1318 goto exit_driver; 1319 1320 return 0; 1321 1322 exit_driver: 1323 platform_driver_unregister(&pc87427_driver); 1324 exit: 1325 return err; 1326 } 1327 1328 static void __exit pc87427_exit(void) 1329 { 1330 platform_device_unregister(pdev); 1331 platform_driver_unregister(&pc87427_driver); 1332 } 1333 1334 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); 1335 MODULE_DESCRIPTION("PC87427 hardware monitoring driver"); 1336 MODULE_LICENSE("GPL"); 1337 1338 module_init(pc87427_init); 1339 module_exit(pc87427_exit); 1340