1 /*************************************************************************** 2 * Copyright (C) 2010-2011 Hans de Goede <hdegoede@redhat.com> * 3 * * 4 * This program is free software; you can redistribute it and/or modify * 5 * it under the terms of the GNU General Public License as published by * 6 * the Free Software Foundation; either version 2 of the License, or * 7 * (at your option) any later version. * 8 * * 9 * This program is distributed in the hope that it will be useful, * 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 12 * GNU General Public License for more details. * 13 * * 14 * You should have received a copy of the GNU General Public License * 15 * along with this program; if not, write to the * 16 * Free Software Foundation, Inc., * 17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 18 ***************************************************************************/ 19 20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 21 22 #include <linux/module.h> 23 #include <linux/init.h> 24 #include <linux/slab.h> 25 #include <linux/jiffies.h> 26 #include <linux/platform_device.h> 27 #include <linux/hwmon.h> 28 #include <linux/hwmon-sysfs.h> 29 #include <linux/err.h> 30 #include <linux/mutex.h> 31 #include <linux/io.h> 32 #include <linux/acpi.h> 33 #include <linux/delay.h> 34 35 #define DRVNAME "sch5627" 36 #define DEVNAME DRVNAME /* We only support one model */ 37 38 #define SIO_SCH5627_EM_LD 0x0C /* Embedded Microcontroller LD */ 39 #define SIO_UNLOCK_KEY 0x55 /* Key to enable Super-I/O */ 40 #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */ 41 42 #define SIO_REG_LDSEL 0x07 /* Logical device select */ 43 #define SIO_REG_DEVID 0x20 /* Device ID */ 44 #define SIO_REG_ENABLE 0x30 /* Logical device enable */ 45 #define SIO_REG_ADDR 0x66 /* Logical device address (2 bytes) */ 46 47 #define SIO_SCH5627_ID 0xC6 /* Chipset ID */ 48 49 #define REGION_LENGTH 9 50 51 #define SCH5627_HWMON_ID 0xa5 52 #define SCH5627_COMPANY_ID 0x5c 53 #define SCH5627_PRIMARY_ID 0xa0 54 55 #define SCH5627_CMD_READ 0x02 56 #define SCH5627_CMD_WRITE 0x03 57 58 #define SCH5627_REG_BUILD_CODE 0x39 59 #define SCH5627_REG_BUILD_ID 0x3a 60 #define SCH5627_REG_HWMON_ID 0x3c 61 #define SCH5627_REG_HWMON_REV 0x3d 62 #define SCH5627_REG_COMPANY_ID 0x3e 63 #define SCH5627_REG_PRIMARY_ID 0x3f 64 #define SCH5627_REG_CTRL 0x40 65 66 #define SCH5627_NO_TEMPS 8 67 #define SCH5627_NO_FANS 4 68 #define SCH5627_NO_IN 5 69 70 static const u16 SCH5627_REG_TEMP_MSB[SCH5627_NO_TEMPS] = { 71 0x2B, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x180, 0x181 }; 72 static const u16 SCH5627_REG_TEMP_LSN[SCH5627_NO_TEMPS] = { 73 0xE2, 0xE1, 0xE1, 0xE5, 0xE5, 0xE6, 0x182, 0x182 }; 74 static const u16 SCH5627_REG_TEMP_HIGH_NIBBLE[SCH5627_NO_TEMPS] = { 75 0, 0, 1, 1, 0, 0, 0, 1 }; 76 static const u16 SCH5627_REG_TEMP_HIGH[SCH5627_NO_TEMPS] = { 77 0x61, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x184, 0x186 }; 78 static const u16 SCH5627_REG_TEMP_ABS[SCH5627_NO_TEMPS] = { 79 0x9B, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x1A8, 0x1A9 }; 80 81 static const u16 SCH5627_REG_FAN[SCH5627_NO_FANS] = { 82 0x2C, 0x2E, 0x30, 0x32 }; 83 static const u16 SCH5627_REG_FAN_MIN[SCH5627_NO_FANS] = { 84 0x62, 0x64, 0x66, 0x68 }; 85 86 static const u16 SCH5627_REG_IN_MSB[SCH5627_NO_IN] = { 87 0x22, 0x23, 0x24, 0x25, 0x189 }; 88 static const u16 SCH5627_REG_IN_LSN[SCH5627_NO_IN] = { 89 0xE4, 0xE4, 0xE3, 0xE3, 0x18A }; 90 static const u16 SCH5627_REG_IN_HIGH_NIBBLE[SCH5627_NO_IN] = { 91 1, 0, 1, 0, 1 }; 92 static const u16 SCH5627_REG_IN_FACTOR[SCH5627_NO_IN] = { 93 10745, 3660, 9765, 10745, 3660 }; 94 static const char * const SCH5627_IN_LABELS[SCH5627_NO_IN] = { 95 "VCC", "VTT", "VBAT", "VTR", "V_IN" }; 96 97 struct sch5627_data { 98 unsigned short addr; 99 struct device *hwmon_dev; 100 u8 control; 101 u8 temp_max[SCH5627_NO_TEMPS]; 102 u8 temp_crit[SCH5627_NO_TEMPS]; 103 u16 fan_min[SCH5627_NO_FANS]; 104 105 struct mutex update_lock; 106 unsigned long last_battery; /* In jiffies */ 107 char valid; /* !=0 if following fields are valid */ 108 unsigned long last_updated; /* In jiffies */ 109 u16 temp[SCH5627_NO_TEMPS]; 110 u16 fan[SCH5627_NO_FANS]; 111 u16 in[SCH5627_NO_IN]; 112 }; 113 114 static struct platform_device *sch5627_pdev; 115 116 /* Super I/O functions */ 117 static inline int superio_inb(int base, int reg) 118 { 119 outb(reg, base); 120 return inb(base + 1); 121 } 122 123 static inline int superio_enter(int base) 124 { 125 /* Don't step on other drivers' I/O space by accident */ 126 if (!request_muxed_region(base, 2, DRVNAME)) { 127 pr_err("I/O address 0x%04x already in use\n", base); 128 return -EBUSY; 129 } 130 131 outb(SIO_UNLOCK_KEY, base); 132 133 return 0; 134 } 135 136 static inline void superio_select(int base, int ld) 137 { 138 outb(SIO_REG_LDSEL, base); 139 outb(ld, base + 1); 140 } 141 142 static inline void superio_exit(int base) 143 { 144 outb(SIO_LOCK_KEY, base); 145 release_region(base, 2); 146 } 147 148 static int sch5627_send_cmd(struct sch5627_data *data, u8 cmd, u16 reg, u8 v) 149 { 150 u8 val; 151 int i; 152 /* 153 * According to SMSC for the commands we use the maximum time for 154 * the EM to respond is 15 ms, but testing shows in practice it 155 * responds within 15-32 reads, so we first busy poll, and if 156 * that fails sleep a bit and try again until we are way past 157 * the 15 ms maximum response time. 158 */ 159 const int max_busy_polls = 64; 160 const int max_lazy_polls = 32; 161 162 /* (Optional) Write-Clear the EC to Host Mailbox Register */ 163 val = inb(data->addr + 1); 164 outb(val, data->addr + 1); 165 166 /* Set Mailbox Address Pointer to first location in Region 1 */ 167 outb(0x00, data->addr + 2); 168 outb(0x80, data->addr + 3); 169 170 /* Write Request Packet Header */ 171 outb(cmd, data->addr + 4); /* VREG Access Type read:0x02 write:0x03 */ 172 outb(0x01, data->addr + 5); /* # of Entries: 1 Byte (8-bit) */ 173 outb(0x04, data->addr + 2); /* Mailbox AP to first data entry loc. */ 174 175 /* Write Value field */ 176 if (cmd == SCH5627_CMD_WRITE) 177 outb(v, data->addr + 4); 178 179 /* Write Address field */ 180 outb(reg & 0xff, data->addr + 6); 181 outb(reg >> 8, data->addr + 7); 182 183 /* Execute the Random Access Command */ 184 outb(0x01, data->addr); /* Write 01h to the Host-to-EC register */ 185 186 /* EM Interface Polling "Algorithm" */ 187 for (i = 0; i < max_busy_polls + max_lazy_polls; i++) { 188 if (i >= max_busy_polls) 189 msleep(1); 190 /* Read Interrupt source Register */ 191 val = inb(data->addr + 8); 192 /* Write Clear the interrupt source bits */ 193 if (val) 194 outb(val, data->addr + 8); 195 /* Command Completed ? */ 196 if (val & 0x01) 197 break; 198 } 199 if (i == max_busy_polls + max_lazy_polls) { 200 pr_err("Max retries exceeded reading virtual " 201 "register 0x%04hx (%d)\n", reg, 1); 202 return -EIO; 203 } 204 205 /* 206 * According to SMSC we may need to retry this, but sofar I've always 207 * seen this succeed in 1 try. 208 */ 209 for (i = 0; i < max_busy_polls; i++) { 210 /* Read EC-to-Host Register */ 211 val = inb(data->addr + 1); 212 /* Command Completed ? */ 213 if (val == 0x01) 214 break; 215 216 if (i == 0) 217 pr_warn("EC reports: 0x%02x reading virtual register " 218 "0x%04hx\n", (unsigned int)val, reg); 219 } 220 if (i == max_busy_polls) { 221 pr_err("Max retries exceeded reading virtual " 222 "register 0x%04hx (%d)\n", reg, 2); 223 return -EIO; 224 } 225 226 /* 227 * According to the SMSC app note we should now do: 228 * 229 * Set Mailbox Address Pointer to first location in Region 1 * 230 * outb(0x00, data->addr + 2); 231 * outb(0x80, data->addr + 3); 232 * 233 * But if we do that things don't work, so let's not. 234 */ 235 236 /* Read Value field */ 237 if (cmd == SCH5627_CMD_READ) 238 return inb(data->addr + 4); 239 240 return 0; 241 } 242 243 static int sch5627_read_virtual_reg(struct sch5627_data *data, u16 reg) 244 { 245 return sch5627_send_cmd(data, SCH5627_CMD_READ, reg, 0); 246 } 247 248 static int sch5627_write_virtual_reg(struct sch5627_data *data, 249 u16 reg, u8 val) 250 { 251 return sch5627_send_cmd(data, SCH5627_CMD_WRITE, reg, val); 252 } 253 254 static int sch5627_read_virtual_reg16(struct sch5627_data *data, u16 reg) 255 { 256 int lsb, msb; 257 258 /* Read LSB first, this will cause the matching MSB to be latched */ 259 lsb = sch5627_read_virtual_reg(data, reg); 260 if (lsb < 0) 261 return lsb; 262 263 msb = sch5627_read_virtual_reg(data, reg + 1); 264 if (msb < 0) 265 return msb; 266 267 return lsb | (msb << 8); 268 } 269 270 static int sch5627_read_virtual_reg12(struct sch5627_data *data, u16 msb_reg, 271 u16 lsn_reg, int high_nibble) 272 { 273 int msb, lsn; 274 275 /* Read MSB first, this will cause the matching LSN to be latched */ 276 msb = sch5627_read_virtual_reg(data, msb_reg); 277 if (msb < 0) 278 return msb; 279 280 lsn = sch5627_read_virtual_reg(data, lsn_reg); 281 if (lsn < 0) 282 return lsn; 283 284 if (high_nibble) 285 return (msb << 4) | (lsn >> 4); 286 else 287 return (msb << 4) | (lsn & 0x0f); 288 } 289 290 static struct sch5627_data *sch5627_update_device(struct device *dev) 291 { 292 struct sch5627_data *data = dev_get_drvdata(dev); 293 struct sch5627_data *ret = data; 294 int i, val; 295 296 mutex_lock(&data->update_lock); 297 298 /* Trigger a Vbat voltage measurement every 5 minutes */ 299 if (time_after(jiffies, data->last_battery + 300 * HZ)) { 300 sch5627_write_virtual_reg(data, SCH5627_REG_CTRL, 301 data->control | 0x10); 302 data->last_battery = jiffies; 303 } 304 305 /* Cache the values for 1 second */ 306 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 307 for (i = 0; i < SCH5627_NO_TEMPS; i++) { 308 val = sch5627_read_virtual_reg12(data, 309 SCH5627_REG_TEMP_MSB[i], 310 SCH5627_REG_TEMP_LSN[i], 311 SCH5627_REG_TEMP_HIGH_NIBBLE[i]); 312 if (unlikely(val < 0)) { 313 ret = ERR_PTR(val); 314 goto abort; 315 } 316 data->temp[i] = val; 317 } 318 319 for (i = 0; i < SCH5627_NO_FANS; i++) { 320 val = sch5627_read_virtual_reg16(data, 321 SCH5627_REG_FAN[i]); 322 if (unlikely(val < 0)) { 323 ret = ERR_PTR(val); 324 goto abort; 325 } 326 data->fan[i] = val; 327 } 328 329 for (i = 0; i < SCH5627_NO_IN; i++) { 330 val = sch5627_read_virtual_reg12(data, 331 SCH5627_REG_IN_MSB[i], 332 SCH5627_REG_IN_LSN[i], 333 SCH5627_REG_IN_HIGH_NIBBLE[i]); 334 if (unlikely(val < 0)) { 335 ret = ERR_PTR(val); 336 goto abort; 337 } 338 data->in[i] = val; 339 } 340 341 data->last_updated = jiffies; 342 data->valid = 1; 343 } 344 abort: 345 mutex_unlock(&data->update_lock); 346 return ret; 347 } 348 349 static int __devinit sch5627_read_limits(struct sch5627_data *data) 350 { 351 int i, val; 352 353 for (i = 0; i < SCH5627_NO_TEMPS; i++) { 354 /* 355 * Note what SMSC calls ABS, is what lm_sensors calls max 356 * (aka high), and HIGH is what lm_sensors calls crit. 357 */ 358 val = sch5627_read_virtual_reg(data, SCH5627_REG_TEMP_ABS[i]); 359 if (val < 0) 360 return val; 361 data->temp_max[i] = val; 362 363 val = sch5627_read_virtual_reg(data, SCH5627_REG_TEMP_HIGH[i]); 364 if (val < 0) 365 return val; 366 data->temp_crit[i] = val; 367 } 368 for (i = 0; i < SCH5627_NO_FANS; i++) { 369 val = sch5627_read_virtual_reg16(data, SCH5627_REG_FAN_MIN[i]); 370 if (val < 0) 371 return val; 372 data->fan_min[i] = val; 373 } 374 375 return 0; 376 } 377 378 static int reg_to_temp(u16 reg) 379 { 380 return (reg * 625) / 10 - 64000; 381 } 382 383 static int reg_to_temp_limit(u8 reg) 384 { 385 return (reg - 64) * 1000; 386 } 387 388 static int reg_to_rpm(u16 reg) 389 { 390 if (reg == 0) 391 return -EIO; 392 if (reg == 0xffff) 393 return 0; 394 395 return 5400540 / reg; 396 } 397 398 static ssize_t show_name(struct device *dev, struct device_attribute *devattr, 399 char *buf) 400 { 401 return snprintf(buf, PAGE_SIZE, "%s\n", DEVNAME); 402 } 403 404 static ssize_t show_temp(struct device *dev, struct device_attribute 405 *devattr, char *buf) 406 { 407 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 408 struct sch5627_data *data = sch5627_update_device(dev); 409 int val; 410 411 if (IS_ERR(data)) 412 return PTR_ERR(data); 413 414 val = reg_to_temp(data->temp[attr->index]); 415 return snprintf(buf, PAGE_SIZE, "%d\n", val); 416 } 417 418 static ssize_t show_temp_fault(struct device *dev, struct device_attribute 419 *devattr, char *buf) 420 { 421 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 422 struct sch5627_data *data = sch5627_update_device(dev); 423 424 if (IS_ERR(data)) 425 return PTR_ERR(data); 426 427 return snprintf(buf, PAGE_SIZE, "%d\n", data->temp[attr->index] == 0); 428 } 429 430 static ssize_t show_temp_max(struct device *dev, struct device_attribute 431 *devattr, char *buf) 432 { 433 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 434 struct sch5627_data *data = dev_get_drvdata(dev); 435 int val; 436 437 val = reg_to_temp_limit(data->temp_max[attr->index]); 438 return snprintf(buf, PAGE_SIZE, "%d\n", val); 439 } 440 441 static ssize_t show_temp_crit(struct device *dev, struct device_attribute 442 *devattr, char *buf) 443 { 444 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 445 struct sch5627_data *data = dev_get_drvdata(dev); 446 int val; 447 448 val = reg_to_temp_limit(data->temp_crit[attr->index]); 449 return snprintf(buf, PAGE_SIZE, "%d\n", val); 450 } 451 452 static ssize_t show_fan(struct device *dev, struct device_attribute 453 *devattr, char *buf) 454 { 455 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 456 struct sch5627_data *data = sch5627_update_device(dev); 457 int val; 458 459 if (IS_ERR(data)) 460 return PTR_ERR(data); 461 462 val = reg_to_rpm(data->fan[attr->index]); 463 if (val < 0) 464 return val; 465 466 return snprintf(buf, PAGE_SIZE, "%d\n", val); 467 } 468 469 static ssize_t show_fan_fault(struct device *dev, struct device_attribute 470 *devattr, char *buf) 471 { 472 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 473 struct sch5627_data *data = sch5627_update_device(dev); 474 475 if (IS_ERR(data)) 476 return PTR_ERR(data); 477 478 return snprintf(buf, PAGE_SIZE, "%d\n", 479 data->fan[attr->index] == 0xffff); 480 } 481 482 static ssize_t show_fan_min(struct device *dev, struct device_attribute 483 *devattr, char *buf) 484 { 485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 486 struct sch5627_data *data = dev_get_drvdata(dev); 487 int val = reg_to_rpm(data->fan_min[attr->index]); 488 if (val < 0) 489 return val; 490 491 return snprintf(buf, PAGE_SIZE, "%d\n", val); 492 } 493 494 static ssize_t show_in(struct device *dev, struct device_attribute 495 *devattr, char *buf) 496 { 497 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 498 struct sch5627_data *data = sch5627_update_device(dev); 499 int val; 500 501 if (IS_ERR(data)) 502 return PTR_ERR(data); 503 504 val = DIV_ROUND_CLOSEST( 505 data->in[attr->index] * SCH5627_REG_IN_FACTOR[attr->index], 506 10000); 507 return snprintf(buf, PAGE_SIZE, "%d\n", val); 508 } 509 510 static ssize_t show_in_label(struct device *dev, struct device_attribute 511 *devattr, char *buf) 512 { 513 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 514 515 return snprintf(buf, PAGE_SIZE, "%s\n", 516 SCH5627_IN_LABELS[attr->index]); 517 } 518 519 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 520 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); 521 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); 522 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); 523 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); 524 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4); 525 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5); 526 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6); 527 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7); 528 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0); 529 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1); 530 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2); 531 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3); 532 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4); 533 static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5); 534 static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_temp_fault, NULL, 6); 535 static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_temp_fault, NULL, 7); 536 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL, 0); 537 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, NULL, 1); 538 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, NULL, 2); 539 static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, NULL, 3); 540 static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, NULL, 4); 541 static SENSOR_DEVICE_ATTR(temp6_max, S_IRUGO, show_temp_max, NULL, 5); 542 static SENSOR_DEVICE_ATTR(temp7_max, S_IRUGO, show_temp_max, NULL, 6); 543 static SENSOR_DEVICE_ATTR(temp8_max, S_IRUGO, show_temp_max, NULL, 7); 544 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0); 545 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL, 1); 546 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit, NULL, 2); 547 static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL, 3); 548 static SENSOR_DEVICE_ATTR(temp5_crit, S_IRUGO, show_temp_crit, NULL, 4); 549 static SENSOR_DEVICE_ATTR(temp6_crit, S_IRUGO, show_temp_crit, NULL, 5); 550 static SENSOR_DEVICE_ATTR(temp7_crit, S_IRUGO, show_temp_crit, NULL, 6); 551 static SENSOR_DEVICE_ATTR(temp8_crit, S_IRUGO, show_temp_crit, NULL, 7); 552 553 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); 554 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); 555 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2); 556 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3); 557 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL, 0); 558 static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault, NULL, 1); 559 static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, show_fan_fault, NULL, 2); 560 static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, show_fan_fault, NULL, 3); 561 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan_min, NULL, 0); 562 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan_min, NULL, 1); 563 static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO, show_fan_min, NULL, 2); 564 static SENSOR_DEVICE_ATTR(fan4_min, S_IRUGO, show_fan_min, NULL, 3); 565 566 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0); 567 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1); 568 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2); 569 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3); 570 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4); 571 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_in_label, NULL, 0); 572 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_in_label, NULL, 1); 573 static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_in_label, NULL, 2); 574 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_in_label, NULL, 3); 575 576 static struct attribute *sch5627_attributes[] = { 577 &dev_attr_name.attr, 578 579 &sensor_dev_attr_temp1_input.dev_attr.attr, 580 &sensor_dev_attr_temp2_input.dev_attr.attr, 581 &sensor_dev_attr_temp3_input.dev_attr.attr, 582 &sensor_dev_attr_temp4_input.dev_attr.attr, 583 &sensor_dev_attr_temp5_input.dev_attr.attr, 584 &sensor_dev_attr_temp6_input.dev_attr.attr, 585 &sensor_dev_attr_temp7_input.dev_attr.attr, 586 &sensor_dev_attr_temp8_input.dev_attr.attr, 587 &sensor_dev_attr_temp1_fault.dev_attr.attr, 588 &sensor_dev_attr_temp2_fault.dev_attr.attr, 589 &sensor_dev_attr_temp3_fault.dev_attr.attr, 590 &sensor_dev_attr_temp4_fault.dev_attr.attr, 591 &sensor_dev_attr_temp5_fault.dev_attr.attr, 592 &sensor_dev_attr_temp6_fault.dev_attr.attr, 593 &sensor_dev_attr_temp7_fault.dev_attr.attr, 594 &sensor_dev_attr_temp8_fault.dev_attr.attr, 595 &sensor_dev_attr_temp1_max.dev_attr.attr, 596 &sensor_dev_attr_temp2_max.dev_attr.attr, 597 &sensor_dev_attr_temp3_max.dev_attr.attr, 598 &sensor_dev_attr_temp4_max.dev_attr.attr, 599 &sensor_dev_attr_temp5_max.dev_attr.attr, 600 &sensor_dev_attr_temp6_max.dev_attr.attr, 601 &sensor_dev_attr_temp7_max.dev_attr.attr, 602 &sensor_dev_attr_temp8_max.dev_attr.attr, 603 &sensor_dev_attr_temp1_crit.dev_attr.attr, 604 &sensor_dev_attr_temp2_crit.dev_attr.attr, 605 &sensor_dev_attr_temp3_crit.dev_attr.attr, 606 &sensor_dev_attr_temp4_crit.dev_attr.attr, 607 &sensor_dev_attr_temp5_crit.dev_attr.attr, 608 &sensor_dev_attr_temp6_crit.dev_attr.attr, 609 &sensor_dev_attr_temp7_crit.dev_attr.attr, 610 &sensor_dev_attr_temp8_crit.dev_attr.attr, 611 612 &sensor_dev_attr_fan1_input.dev_attr.attr, 613 &sensor_dev_attr_fan2_input.dev_attr.attr, 614 &sensor_dev_attr_fan3_input.dev_attr.attr, 615 &sensor_dev_attr_fan4_input.dev_attr.attr, 616 &sensor_dev_attr_fan1_fault.dev_attr.attr, 617 &sensor_dev_attr_fan2_fault.dev_attr.attr, 618 &sensor_dev_attr_fan3_fault.dev_attr.attr, 619 &sensor_dev_attr_fan4_fault.dev_attr.attr, 620 &sensor_dev_attr_fan1_min.dev_attr.attr, 621 &sensor_dev_attr_fan2_min.dev_attr.attr, 622 &sensor_dev_attr_fan3_min.dev_attr.attr, 623 &sensor_dev_attr_fan4_min.dev_attr.attr, 624 625 &sensor_dev_attr_in0_input.dev_attr.attr, 626 &sensor_dev_attr_in1_input.dev_attr.attr, 627 &sensor_dev_attr_in2_input.dev_attr.attr, 628 &sensor_dev_attr_in3_input.dev_attr.attr, 629 &sensor_dev_attr_in4_input.dev_attr.attr, 630 &sensor_dev_attr_in0_label.dev_attr.attr, 631 &sensor_dev_attr_in1_label.dev_attr.attr, 632 &sensor_dev_attr_in2_label.dev_attr.attr, 633 &sensor_dev_attr_in3_label.dev_attr.attr, 634 /* No in4_label as in4 is a generic input pin */ 635 636 NULL 637 }; 638 639 static const struct attribute_group sch5627_group = { 640 .attrs = sch5627_attributes, 641 }; 642 643 static int sch5627_remove(struct platform_device *pdev) 644 { 645 struct sch5627_data *data = platform_get_drvdata(pdev); 646 647 if (data->hwmon_dev) 648 hwmon_device_unregister(data->hwmon_dev); 649 650 sysfs_remove_group(&pdev->dev.kobj, &sch5627_group); 651 platform_set_drvdata(pdev, NULL); 652 kfree(data); 653 654 return 0; 655 } 656 657 static int __devinit sch5627_probe(struct platform_device *pdev) 658 { 659 struct sch5627_data *data; 660 int err, build_code, build_id, hwmon_rev, val; 661 662 data = kzalloc(sizeof(struct sch5627_data), GFP_KERNEL); 663 if (!data) 664 return -ENOMEM; 665 666 data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start; 667 mutex_init(&data->update_lock); 668 platform_set_drvdata(pdev, data); 669 670 val = sch5627_read_virtual_reg(data, SCH5627_REG_HWMON_ID); 671 if (val < 0) { 672 err = val; 673 goto error; 674 } 675 if (val != SCH5627_HWMON_ID) { 676 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "hwmon", 677 val, SCH5627_HWMON_ID); 678 err = -ENODEV; 679 goto error; 680 } 681 682 val = sch5627_read_virtual_reg(data, SCH5627_REG_COMPANY_ID); 683 if (val < 0) { 684 err = val; 685 goto error; 686 } 687 if (val != SCH5627_COMPANY_ID) { 688 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "company", 689 val, SCH5627_COMPANY_ID); 690 err = -ENODEV; 691 goto error; 692 } 693 694 val = sch5627_read_virtual_reg(data, SCH5627_REG_PRIMARY_ID); 695 if (val < 0) { 696 err = val; 697 goto error; 698 } 699 if (val != SCH5627_PRIMARY_ID) { 700 pr_err("invalid %s id: 0x%02X (expected 0x%02X)\n", "primary", 701 val, SCH5627_PRIMARY_ID); 702 err = -ENODEV; 703 goto error; 704 } 705 706 build_code = sch5627_read_virtual_reg(data, SCH5627_REG_BUILD_CODE); 707 if (build_code < 0) { 708 err = build_code; 709 goto error; 710 } 711 712 build_id = sch5627_read_virtual_reg16(data, SCH5627_REG_BUILD_ID); 713 if (build_id < 0) { 714 err = build_id; 715 goto error; 716 } 717 718 hwmon_rev = sch5627_read_virtual_reg(data, SCH5627_REG_HWMON_REV); 719 if (hwmon_rev < 0) { 720 err = hwmon_rev; 721 goto error; 722 } 723 724 val = sch5627_read_virtual_reg(data, SCH5627_REG_CTRL); 725 if (val < 0) { 726 err = val; 727 goto error; 728 } 729 data->control = val; 730 if (!(data->control & 0x01)) { 731 pr_err("hardware monitoring not enabled\n"); 732 err = -ENODEV; 733 goto error; 734 } 735 /* Trigger a Vbat voltage measurement, so that we get a valid reading 736 the first time we read Vbat */ 737 sch5627_write_virtual_reg(data, SCH5627_REG_CTRL, 738 data->control | 0x10); 739 data->last_battery = jiffies; 740 741 /* 742 * Read limits, we do this only once as reading a register on 743 * the sch5627 is quite expensive (and they don't change). 744 */ 745 err = sch5627_read_limits(data); 746 if (err) 747 goto error; 748 749 pr_info("firmware build: code 0x%02X, id 0x%04X, hwmon: rev 0x%02X\n", 750 build_code, build_id, hwmon_rev); 751 752 /* Register sysfs interface files */ 753 err = sysfs_create_group(&pdev->dev.kobj, &sch5627_group); 754 if (err) 755 goto error; 756 757 data->hwmon_dev = hwmon_device_register(&pdev->dev); 758 if (IS_ERR(data->hwmon_dev)) { 759 err = PTR_ERR(data->hwmon_dev); 760 data->hwmon_dev = NULL; 761 goto error; 762 } 763 764 return 0; 765 766 error: 767 sch5627_remove(pdev); 768 return err; 769 } 770 771 static int __init sch5627_find(int sioaddr, unsigned short *address) 772 { 773 u8 devid; 774 int err = superio_enter(sioaddr); 775 if (err) 776 return err; 777 778 devid = superio_inb(sioaddr, SIO_REG_DEVID); 779 if (devid != SIO_SCH5627_ID) { 780 pr_debug("Unsupported device id: 0x%02x\n", 781 (unsigned int)devid); 782 err = -ENODEV; 783 goto exit; 784 } 785 786 superio_select(sioaddr, SIO_SCH5627_EM_LD); 787 788 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) { 789 pr_warn("Device not activated\n"); 790 err = -ENODEV; 791 goto exit; 792 } 793 794 /* 795 * Warning the order of the low / high byte is the other way around 796 * as on most other superio devices!! 797 */ 798 *address = superio_inb(sioaddr, SIO_REG_ADDR) | 799 superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8; 800 if (*address == 0) { 801 pr_warn("Base address not set\n"); 802 err = -ENODEV; 803 goto exit; 804 } 805 806 pr_info("Found %s chip at %#hx\n", DEVNAME, *address); 807 exit: 808 superio_exit(sioaddr); 809 return err; 810 } 811 812 static int __init sch5627_device_add(unsigned short address) 813 { 814 struct resource res = { 815 .start = address, 816 .end = address + REGION_LENGTH - 1, 817 .flags = IORESOURCE_IO, 818 }; 819 int err; 820 821 sch5627_pdev = platform_device_alloc(DRVNAME, address); 822 if (!sch5627_pdev) 823 return -ENOMEM; 824 825 res.name = sch5627_pdev->name; 826 err = acpi_check_resource_conflict(&res); 827 if (err) 828 goto exit_device_put; 829 830 err = platform_device_add_resources(sch5627_pdev, &res, 1); 831 if (err) { 832 pr_err("Device resource addition failed\n"); 833 goto exit_device_put; 834 } 835 836 err = platform_device_add(sch5627_pdev); 837 if (err) { 838 pr_err("Device addition failed\n"); 839 goto exit_device_put; 840 } 841 842 return 0; 843 844 exit_device_put: 845 platform_device_put(sch5627_pdev); 846 847 return err; 848 } 849 850 static struct platform_driver sch5627_driver = { 851 .driver = { 852 .owner = THIS_MODULE, 853 .name = DRVNAME, 854 }, 855 .probe = sch5627_probe, 856 .remove = sch5627_remove, 857 }; 858 859 static int __init sch5627_init(void) 860 { 861 int err = -ENODEV; 862 unsigned short address; 863 864 if (sch5627_find(0x4e, &address) && sch5627_find(0x2e, &address)) 865 goto exit; 866 867 err = platform_driver_register(&sch5627_driver); 868 if (err) 869 goto exit; 870 871 err = sch5627_device_add(address); 872 if (err) 873 goto exit_driver; 874 875 return 0; 876 877 exit_driver: 878 platform_driver_unregister(&sch5627_driver); 879 exit: 880 return err; 881 } 882 883 static void __exit sch5627_exit(void) 884 { 885 platform_device_unregister(sch5627_pdev); 886 platform_driver_unregister(&sch5627_driver); 887 } 888 889 MODULE_DESCRIPTION("SMSC SCH5627 Hardware Monitoring Driver"); 890 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); 891 MODULE_LICENSE("GPL"); 892 893 module_init(sch5627_init); 894 module_exit(sch5627_exit); 895