1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature 4 * sensors, fan control, keyboard backlight control) used in Intel-based Apple 5 * computers. 6 * 7 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch> 8 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se> 9 * 10 * Based on hdaps.c driver: 11 * Copyright (C) 2005 Robert Love <rml@novell.com> 12 * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net> 13 * 14 * Fan control based on smcFanControl: 15 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com> 16 */ 17 18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19 20 #include <linux/delay.h> 21 #include <linux/platform_device.h> 22 #include <linux/input.h> 23 #include <linux/kernel.h> 24 #include <linux/slab.h> 25 #include <linux/module.h> 26 #include <linux/timer.h> 27 #include <linux/dmi.h> 28 #include <linux/mutex.h> 29 #include <linux/hwmon-sysfs.h> 30 #include <linux/io.h> 31 #include <linux/leds.h> 32 #include <linux/hwmon.h> 33 #include <linux/workqueue.h> 34 #include <linux/err.h> 35 #include <linux/bits.h> 36 #include <asm/barrier.h> 37 38 /* data port used by Apple SMC */ 39 #define APPLESMC_DATA_PORT 0x300 40 /* command/status port used by Apple SMC */ 41 #define APPLESMC_CMD_PORT 0x304 42 43 #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */ 44 45 #define APPLESMC_MAX_DATA_LENGTH 32 46 47 /* Apple SMC status bits */ 48 #define SMC_STATUS_AWAITING_DATA BIT(0) /* SMC has data waiting to be read */ 49 #define SMC_STATUS_IB_CLOSED BIT(1) /* Will ignore any input */ 50 #define SMC_STATUS_BUSY BIT(2) /* Command in progress */ 51 52 /* Initial wait is 8us */ 53 #define APPLESMC_MIN_WAIT 0x0008 54 55 #define APPLESMC_READ_CMD 0x10 56 #define APPLESMC_WRITE_CMD 0x11 57 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12 58 #define APPLESMC_GET_KEY_TYPE_CMD 0x13 59 60 #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */ 61 62 #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */ 63 #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */ 64 #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */ 65 66 #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */ 67 68 #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */ 69 #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */ 70 #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */ 71 #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */ 72 73 #define FANS_COUNT "FNum" /* r-o ui8 */ 74 #define FANS_MANUAL "FS! " /* r-w ui16 */ 75 #define FAN_ID_FMT "F%dID" /* r-o char[16] */ 76 77 #define TEMP_SENSOR_TYPE "sp78" 78 79 /* List of keys used to read/write fan speeds */ 80 static const char *const fan_speed_fmt[] = { 81 "F%dAc", /* actual speed */ 82 "F%dMn", /* minimum speed (rw) */ 83 "F%dMx", /* maximum speed */ 84 "F%dSf", /* safe speed - not all models */ 85 "F%dTg", /* target speed (manual: rw) */ 86 }; 87 88 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */ 89 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */ 90 91 #define APPLESMC_POLL_INTERVAL 50 /* msecs */ 92 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */ 93 #define APPLESMC_INPUT_FLAT 4 94 95 #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff) 96 #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16) 97 98 /* Dynamic device node attributes */ 99 struct applesmc_dev_attr { 100 struct sensor_device_attribute sda; /* hwmon attributes */ 101 char name[32]; /* room for node file name */ 102 }; 103 104 /* Dynamic device node group */ 105 struct applesmc_node_group { 106 char *format; /* format string */ 107 void *show; /* show function */ 108 void *store; /* store function */ 109 int option; /* function argument */ 110 struct applesmc_dev_attr *nodes; /* dynamic node array */ 111 }; 112 113 /* AppleSMC entry - cached register information */ 114 struct applesmc_entry { 115 char key[5]; /* four-letter key code */ 116 u8 valid; /* set when entry is successfully read once */ 117 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */ 118 char type[5]; /* four-letter type code */ 119 u8 flags; /* 0x10: func; 0x40: write; 0x80: read */ 120 }; 121 122 /* Register lookup and registers common to all SMCs */ 123 static struct applesmc_registers { 124 struct mutex mutex; /* register read/write mutex */ 125 unsigned int key_count; /* number of SMC registers */ 126 unsigned int fan_count; /* number of fans */ 127 unsigned int temp_count; /* number of temperature registers */ 128 unsigned int temp_begin; /* temperature lower index bound */ 129 unsigned int temp_end; /* temperature upper index bound */ 130 unsigned int index_count; /* size of temperature index array */ 131 int num_light_sensors; /* number of light sensors */ 132 bool has_accelerometer; /* has motion sensor */ 133 bool has_key_backlight; /* has keyboard backlight */ 134 bool init_complete; /* true when fully initialized */ 135 struct applesmc_entry *cache; /* cached key entries */ 136 const char **index; /* temperature key index */ 137 char fan_positions[10][17]; /* cached fan position labels */ 138 } smcreg = { 139 .mutex = __MUTEX_INITIALIZER(smcreg.mutex), 140 }; 141 142 static const int debug; 143 static struct platform_device *pdev; 144 static s16 rest_x; 145 static s16 rest_y; 146 static u8 backlight_state[2]; 147 148 static struct input_dev *applesmc_idev; 149 static struct device *hwmon_dev; 150 151 /* 152 * Last index written to key_at_index sysfs file, and value to use for all other 153 * key_at_index_* sysfs files. 154 */ 155 static unsigned int key_at_index; 156 157 static struct workqueue_struct *applesmc_led_wq; 158 159 /* 160 * Wait for specific status bits with a mask on the SMC. 161 * Used before all transactions. 162 * This does 10 fast loops of 8us then exponentially backs off for a 163 * minimum total wait of 262ms. Depending on usleep_range this could 164 * run out past 500ms. 165 */ 166 167 static int wait_status(u8 val, u8 mask) 168 { 169 u8 status; 170 int us; 171 int i; 172 173 us = APPLESMC_MIN_WAIT; 174 for (i = 0; i < 24 ; i++) { 175 status = inb(APPLESMC_CMD_PORT); 176 if ((status & mask) == val) 177 return 0; 178 usleep_range(us, us * 2); 179 if (i > 9) 180 us <<= 1; 181 } 182 return -EIO; 183 } 184 185 /* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */ 186 187 static int send_byte(u8 cmd, u16 port) 188 { 189 int status; 190 191 status = wait_status(0, SMC_STATUS_IB_CLOSED); 192 if (status) 193 return status; 194 /* 195 * This needs to be a separate read looking for bit 0x04 196 * after bit 0x02 falls. If consolidated with the wait above 197 * this extra read may not happen if status returns both 198 * simultaneously and this would appear to be required. 199 */ 200 status = wait_status(SMC_STATUS_BUSY, SMC_STATUS_BUSY); 201 if (status) 202 return status; 203 204 outb(cmd, port); 205 return 0; 206 } 207 208 /* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */ 209 210 static int send_command(u8 cmd) 211 { 212 int ret; 213 214 ret = wait_status(0, SMC_STATUS_IB_CLOSED); 215 if (ret) 216 return ret; 217 outb(cmd, APPLESMC_CMD_PORT); 218 return 0; 219 } 220 221 /* 222 * Based on logic from the Apple driver. This is issued before any interaction 223 * If busy is stuck high, issue a read command to reset the SMC state machine. 224 * If busy is stuck high after the command then the SMC is jammed. 225 */ 226 227 static int smc_sane(void) 228 { 229 int ret; 230 231 ret = wait_status(0, SMC_STATUS_BUSY); 232 if (!ret) 233 return ret; 234 ret = send_command(APPLESMC_READ_CMD); 235 if (ret) 236 return ret; 237 return wait_status(0, SMC_STATUS_BUSY); 238 } 239 240 static int send_argument(const char *key) 241 { 242 int i; 243 244 for (i = 0; i < 4; i++) 245 if (send_byte(key[i], APPLESMC_DATA_PORT)) 246 return -EIO; 247 return 0; 248 } 249 250 static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len) 251 { 252 u8 status, data = 0; 253 int i; 254 int ret; 255 256 ret = smc_sane(); 257 if (ret) 258 return ret; 259 260 if (send_command(cmd) || send_argument(key)) { 261 pr_warn("%.4s: read arg fail\n", key); 262 return -EIO; 263 } 264 265 /* This has no effect on newer (2012) SMCs */ 266 if (send_byte(len, APPLESMC_DATA_PORT)) { 267 pr_warn("%.4s: read len fail\n", key); 268 return -EIO; 269 } 270 271 for (i = 0; i < len; i++) { 272 if (wait_status(SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY, 273 SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) { 274 pr_warn("%.4s: read data[%d] fail\n", key, i); 275 return -EIO; 276 } 277 buffer[i] = inb(APPLESMC_DATA_PORT); 278 } 279 280 /* Read the data port until bit0 is cleared */ 281 for (i = 0; i < 16; i++) { 282 udelay(APPLESMC_MIN_WAIT); 283 status = inb(APPLESMC_CMD_PORT); 284 if (!(status & SMC_STATUS_AWAITING_DATA)) 285 break; 286 data = inb(APPLESMC_DATA_PORT); 287 } 288 if (i) 289 pr_warn("flushed %d bytes, last value is: %d\n", i, data); 290 291 return wait_status(0, SMC_STATUS_BUSY); 292 } 293 294 static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len) 295 { 296 int i; 297 int ret; 298 299 ret = smc_sane(); 300 if (ret) 301 return ret; 302 303 if (send_command(cmd) || send_argument(key)) { 304 pr_warn("%s: write arg fail\n", key); 305 return -EIO; 306 } 307 308 if (send_byte(len, APPLESMC_DATA_PORT)) { 309 pr_warn("%.4s: write len fail\n", key); 310 return -EIO; 311 } 312 313 for (i = 0; i < len; i++) { 314 if (send_byte(buffer[i], APPLESMC_DATA_PORT)) { 315 pr_warn("%s: write data fail\n", key); 316 return -EIO; 317 } 318 } 319 320 return wait_status(0, SMC_STATUS_BUSY); 321 } 322 323 static int read_register_count(unsigned int *count) 324 { 325 __be32 be; 326 int ret; 327 328 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4); 329 if (ret) 330 return ret; 331 332 *count = be32_to_cpu(be); 333 return 0; 334 } 335 336 /* 337 * Serialized I/O 338 * 339 * Returns zero on success or a negative error on failure. 340 * All functions below are concurrency safe - callers should NOT hold lock. 341 */ 342 343 static int applesmc_read_entry(const struct applesmc_entry *entry, 344 u8 *buf, u8 len) 345 { 346 int ret; 347 348 if (entry->len != len) 349 return -EINVAL; 350 mutex_lock(&smcreg.mutex); 351 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len); 352 mutex_unlock(&smcreg.mutex); 353 354 return ret; 355 } 356 357 static int applesmc_write_entry(const struct applesmc_entry *entry, 358 const u8 *buf, u8 len) 359 { 360 int ret; 361 362 if (entry->len != len) 363 return -EINVAL; 364 mutex_lock(&smcreg.mutex); 365 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len); 366 mutex_unlock(&smcreg.mutex); 367 return ret; 368 } 369 370 static const struct applesmc_entry *applesmc_get_entry_by_index(int index) 371 { 372 struct applesmc_entry *cache = &smcreg.cache[index]; 373 u8 key[4], info[6]; 374 __be32 be; 375 int ret = 0; 376 377 /* Pairs with smp_store_release() to ensure cache contents are visible */ 378 if (smp_load_acquire(&cache->valid)) 379 return cache; 380 381 mutex_lock(&smcreg.mutex); 382 383 if (cache->valid) 384 goto out; 385 be = cpu_to_be32(index); 386 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4); 387 if (ret) 388 goto out; 389 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6); 390 if (ret) 391 goto out; 392 393 memcpy(cache->key, key, 4); 394 cache->len = info[0]; 395 memcpy(cache->type, &info[1], 4); 396 cache->flags = info[5]; 397 /* Pairs with smp_load_acquire() to commit cache contents before setting valid */ 398 smp_store_release(&cache->valid, true); 399 400 out: 401 mutex_unlock(&smcreg.mutex); 402 if (ret) 403 return ERR_PTR(ret); 404 return cache; 405 } 406 407 static int applesmc_get_lower_bound(unsigned int *lo, const char *key) 408 { 409 int begin = 0, end = smcreg.key_count; 410 const struct applesmc_entry *entry; 411 412 while (begin != end) { 413 int middle = begin + (end - begin) / 2; 414 entry = applesmc_get_entry_by_index(middle); 415 if (IS_ERR(entry)) { 416 *lo = 0; 417 return PTR_ERR(entry); 418 } 419 if (strcmp(entry->key, key) < 0) 420 begin = middle + 1; 421 else 422 end = middle; 423 } 424 425 *lo = begin; 426 return 0; 427 } 428 429 static int applesmc_get_upper_bound(unsigned int *hi, const char *key) 430 { 431 int begin = 0, end = smcreg.key_count; 432 const struct applesmc_entry *entry; 433 434 while (begin != end) { 435 int middle = begin + (end - begin) / 2; 436 entry = applesmc_get_entry_by_index(middle); 437 if (IS_ERR(entry)) { 438 *hi = smcreg.key_count; 439 return PTR_ERR(entry); 440 } 441 if (strcmp(key, entry->key) < 0) 442 end = middle; 443 else 444 begin = middle + 1; 445 } 446 447 *hi = begin; 448 return 0; 449 } 450 451 static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key) 452 { 453 int begin, end; 454 int ret; 455 456 ret = applesmc_get_lower_bound(&begin, key); 457 if (ret) 458 return ERR_PTR(ret); 459 ret = applesmc_get_upper_bound(&end, key); 460 if (ret) 461 return ERR_PTR(ret); 462 if (end - begin != 1) 463 return ERR_PTR(-EINVAL); 464 465 return applesmc_get_entry_by_index(begin); 466 } 467 468 static int applesmc_read_key(const char *key, u8 *buffer, u8 len) 469 { 470 const struct applesmc_entry *entry; 471 472 entry = applesmc_get_entry_by_key(key); 473 if (IS_ERR(entry)) 474 return PTR_ERR(entry); 475 476 return applesmc_read_entry(entry, buffer, len); 477 } 478 479 static int applesmc_write_key(const char *key, const u8 *buffer, u8 len) 480 { 481 const struct applesmc_entry *entry; 482 483 entry = applesmc_get_entry_by_key(key); 484 if (IS_ERR(entry)) 485 return PTR_ERR(entry); 486 487 return applesmc_write_entry(entry, buffer, len); 488 } 489 490 static int applesmc_has_key(const char *key, bool *value) 491 { 492 const struct applesmc_entry *entry; 493 494 entry = applesmc_get_entry_by_key(key); 495 if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL) 496 return PTR_ERR(entry); 497 498 *value = !IS_ERR(entry); 499 return 0; 500 } 501 502 /* 503 * applesmc_read_s16 - Read 16-bit signed big endian register 504 */ 505 static int applesmc_read_s16(const char *key, s16 *value) 506 { 507 u8 buffer[2]; 508 int ret; 509 510 ret = applesmc_read_key(key, buffer, 2); 511 if (ret) 512 return ret; 513 514 *value = ((s16)buffer[0] << 8) | buffer[1]; 515 return 0; 516 } 517 518 /* 519 * applesmc_device_init - initialize the accelerometer. Can sleep. 520 */ 521 static void applesmc_device_init(void) 522 { 523 int total; 524 u8 buffer[2]; 525 526 if (!smcreg.has_accelerometer) 527 return; 528 529 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { 530 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) && 531 (buffer[0] != 0x00 || buffer[1] != 0x00)) 532 return; 533 buffer[0] = 0xe0; 534 buffer[1] = 0x00; 535 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2); 536 msleep(INIT_WAIT_MSECS); 537 } 538 539 pr_warn("failed to init the device\n"); 540 } 541 542 static int applesmc_init_index(struct applesmc_registers *s) 543 { 544 const struct applesmc_entry *entry; 545 unsigned int i; 546 547 if (s->index) 548 return 0; 549 550 s->index = kcalloc(s->temp_count, sizeof(s->index[0]), GFP_KERNEL); 551 if (!s->index) 552 return -ENOMEM; 553 554 for (i = s->temp_begin; i < s->temp_end; i++) { 555 entry = applesmc_get_entry_by_index(i); 556 if (IS_ERR(entry)) 557 continue; 558 if (strcmp(entry->type, TEMP_SENSOR_TYPE)) 559 continue; 560 s->index[s->index_count++] = entry->key; 561 } 562 563 return 0; 564 } 565 566 /* 567 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent. 568 */ 569 static int applesmc_init_smcreg_try(void) 570 { 571 struct applesmc_registers *s = &smcreg; 572 bool left_light_sensor = false, right_light_sensor = false; 573 unsigned int count, i; 574 u8 tmp[1]; 575 int ret; 576 577 if (s->init_complete) 578 return 0; 579 580 ret = read_register_count(&count); 581 if (ret) 582 return ret; 583 584 if (s->cache && s->key_count != count) { 585 pr_warn("key count changed from %d to %d\n", 586 s->key_count, count); 587 kfree(s->cache); 588 s->cache = NULL; 589 } 590 s->key_count = count; 591 592 if (!s->cache) 593 s->cache = kzalloc_objs(*s->cache, s->key_count); 594 if (!s->cache) 595 return -ENOMEM; 596 597 ret = applesmc_read_key(FANS_COUNT, tmp, 1); 598 if (ret) 599 return ret; 600 s->fan_count = tmp[0]; 601 if (s->fan_count > 10) 602 s->fan_count = 10; 603 604 for (i = 0; i < s->fan_count; i++) { 605 char newkey[5]; 606 607 scnprintf(newkey, sizeof(newkey), FAN_ID_FMT, i); 608 ret = applesmc_read_key(newkey, s->fan_positions[i], 16); 609 s->fan_positions[i][16] = 0; 610 if (ret) 611 scnprintf(s->fan_positions[i], 17, " Fan %d", i); 612 } 613 614 ret = applesmc_get_lower_bound(&s->temp_begin, "T"); 615 if (ret) 616 return ret; 617 ret = applesmc_get_lower_bound(&s->temp_end, "U"); 618 if (ret) 619 return ret; 620 s->temp_count = s->temp_end - s->temp_begin; 621 622 ret = applesmc_init_index(s); 623 if (ret) 624 return ret; 625 626 ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor); 627 if (ret) 628 return ret; 629 ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor); 630 if (ret) 631 return ret; 632 ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer); 633 if (ret) 634 return ret; 635 ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight); 636 if (ret) 637 return ret; 638 639 s->num_light_sensors = left_light_sensor + right_light_sensor; 640 s->init_complete = true; 641 642 pr_info("key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n", 643 s->key_count, s->fan_count, s->temp_count, s->index_count, 644 s->has_accelerometer, 645 s->num_light_sensors, 646 s->has_key_backlight); 647 648 return 0; 649 } 650 651 static void applesmc_destroy_smcreg(void) 652 { 653 kfree(smcreg.index); 654 smcreg.index = NULL; 655 kfree(smcreg.cache); 656 smcreg.cache = NULL; 657 smcreg.init_complete = false; 658 } 659 660 /* 661 * applesmc_init_smcreg - Initialize register cache. 662 * 663 * Retries until initialization is successful, or the operation times out. 664 * 665 */ 666 static int applesmc_init_smcreg(void) 667 { 668 int ms, ret; 669 670 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) { 671 ret = applesmc_init_smcreg_try(); 672 if (!ret) { 673 if (ms) 674 pr_info("init_smcreg() took %d ms\n", ms); 675 return 0; 676 } 677 msleep(INIT_WAIT_MSECS); 678 } 679 680 applesmc_destroy_smcreg(); 681 682 return ret; 683 } 684 685 /* Device model stuff */ 686 static int applesmc_probe(struct platform_device *dev) 687 { 688 int ret; 689 690 ret = applesmc_init_smcreg(); 691 if (ret) 692 return ret; 693 694 applesmc_device_init(); 695 696 return 0; 697 } 698 699 /* Synchronize device with memorized backlight state */ 700 static int applesmc_pm_resume(struct device *dev) 701 { 702 if (smcreg.has_key_backlight) 703 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); 704 return 0; 705 } 706 707 /* Reinitialize device on resume from hibernation */ 708 static int applesmc_pm_restore(struct device *dev) 709 { 710 applesmc_device_init(); 711 return applesmc_pm_resume(dev); 712 } 713 714 static const struct dev_pm_ops applesmc_pm_ops = { 715 .resume = applesmc_pm_resume, 716 .restore = applesmc_pm_restore, 717 }; 718 719 static struct platform_driver applesmc_driver = { 720 .probe = applesmc_probe, 721 .driver = { 722 .name = "applesmc", 723 .pm = &applesmc_pm_ops, 724 }, 725 }; 726 727 /* 728 * applesmc_calibrate - Set our "resting" values. Callers must 729 * hold applesmc_lock. 730 */ 731 static void applesmc_calibrate(void) 732 { 733 applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x); 734 applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y); 735 rest_x = -rest_x; 736 } 737 738 static void applesmc_idev_poll(struct input_dev *idev) 739 { 740 s16 x, y; 741 742 if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x)) 743 return; 744 if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y)) 745 return; 746 747 x = -x; 748 input_report_abs(idev, ABS_X, x - rest_x); 749 input_report_abs(idev, ABS_Y, y - rest_y); 750 input_sync(idev); 751 } 752 753 /* Sysfs Files */ 754 755 static ssize_t applesmc_name_show(struct device *dev, 756 struct device_attribute *attr, char *buf) 757 { 758 return sysfs_emit(buf, "applesmc\n"); 759 } 760 761 static ssize_t applesmc_position_show(struct device *dev, 762 struct device_attribute *attr, char *buf) 763 { 764 int ret; 765 s16 x, y, z; 766 767 ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x); 768 if (ret) 769 goto out; 770 ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y); 771 if (ret) 772 goto out; 773 ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z); 774 if (ret) 775 goto out; 776 777 out: 778 if (ret) 779 return ret; 780 781 return sysfs_emit(buf, "(%d,%d,%d)\n", x, y, z); 782 } 783 784 static ssize_t applesmc_light_show(struct device *dev, 785 struct device_attribute *attr, char *sysfsbuf) 786 { 787 const struct applesmc_entry *entry; 788 static int data_length; 789 int ret; 790 u8 left = 0, right = 0; 791 u8 buffer[10]; 792 793 if (!data_length) { 794 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY); 795 if (IS_ERR(entry)) 796 return PTR_ERR(entry); 797 if (entry->len > 10) 798 return -ENXIO; 799 data_length = entry->len; 800 pr_info("light sensor data length set to %d\n", data_length); 801 } 802 803 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length); 804 if (ret) 805 goto out; 806 /* newer macbooks report a single 10-bit bigendian value */ 807 if (data_length == 10) { 808 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2; 809 goto out; 810 } 811 left = buffer[2]; 812 813 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length); 814 if (ret) 815 goto out; 816 right = buffer[2]; 817 818 out: 819 if (ret) 820 return ret; 821 822 return sysfs_emit(sysfsbuf, "(%d,%d)\n", left, right); 823 } 824 825 static ssize_t applesmc_show_fan_speed(struct device *dev, 826 struct device_attribute *attr, char *sysfsbuf) 827 { 828 int ret; 829 unsigned int speed = 0; 830 char newkey[5]; 831 u8 buffer[2]; 832 833 scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)], 834 to_index(attr)); 835 836 ret = applesmc_read_key(newkey, buffer, 2); 837 if (ret) 838 return ret; 839 840 speed = ((buffer[0] << 8 | buffer[1]) >> 2); 841 return sysfs_emit(sysfsbuf, "%u\n", speed); 842 } 843 844 static ssize_t applesmc_calibrate_show(struct device *dev, 845 struct device_attribute *attr, char *sysfsbuf) 846 { 847 return sysfs_emit(sysfsbuf, "(%d,%d)\n", rest_x, rest_y); 848 } 849 850 static ssize_t applesmc_calibrate_store(struct device *dev, 851 struct device_attribute *attr, const char *sysfsbuf, size_t count) 852 { 853 applesmc_calibrate(); 854 855 return count; 856 } 857 858 static void applesmc_backlight_set(struct work_struct *work) 859 { 860 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); 861 } 862 static DECLARE_WORK(backlight_work, &applesmc_backlight_set); 863 864 static void applesmc_brightness_set(struct led_classdev *led_cdev, 865 enum led_brightness value) 866 { 867 int ret; 868 869 backlight_state[0] = value; 870 ret = queue_work(applesmc_led_wq, &backlight_work); 871 872 if (debug && (!ret)) 873 dev_dbg(led_cdev->dev, "work was already on the queue.\n"); 874 } 875 876 static ssize_t applesmc_key_count_show(struct device *dev, 877 struct device_attribute *attr, char *sysfsbuf) 878 { 879 int ret; 880 u8 buffer[4]; 881 u32 count; 882 883 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4); 884 if (ret) 885 return ret; 886 887 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) + 888 ((u32)buffer[2]<<8) + buffer[3]; 889 return sysfs_emit(sysfsbuf, "%d\n", count); 890 } 891 892 static ssize_t applesmc_key_at_index_read_show(struct device *dev, 893 struct device_attribute *attr, char *sysfsbuf) 894 { 895 const struct applesmc_entry *entry; 896 int ret; 897 898 entry = applesmc_get_entry_by_index(key_at_index); 899 if (IS_ERR(entry)) 900 return PTR_ERR(entry); 901 ret = applesmc_read_entry(entry, sysfsbuf, entry->len); 902 if (ret) 903 return ret; 904 905 return entry->len; 906 } 907 908 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev, 909 struct device_attribute *attr, char *sysfsbuf) 910 { 911 const struct applesmc_entry *entry; 912 913 entry = applesmc_get_entry_by_index(key_at_index); 914 if (IS_ERR(entry)) 915 return PTR_ERR(entry); 916 917 return sysfs_emit(sysfsbuf, "%d\n", entry->len); 918 } 919 920 static ssize_t applesmc_key_at_index_type_show(struct device *dev, 921 struct device_attribute *attr, char *sysfsbuf) 922 { 923 const struct applesmc_entry *entry; 924 925 entry = applesmc_get_entry_by_index(key_at_index); 926 if (IS_ERR(entry)) 927 return PTR_ERR(entry); 928 929 return sysfs_emit(sysfsbuf, "%s\n", entry->type); 930 } 931 932 static ssize_t applesmc_key_at_index_name_show(struct device *dev, 933 struct device_attribute *attr, char *sysfsbuf) 934 { 935 const struct applesmc_entry *entry; 936 937 entry = applesmc_get_entry_by_index(key_at_index); 938 if (IS_ERR(entry)) 939 return PTR_ERR(entry); 940 941 return sysfs_emit(sysfsbuf, "%s\n", entry->key); 942 } 943 944 static ssize_t applesmc_key_at_index_show(struct device *dev, 945 struct device_attribute *attr, char *sysfsbuf) 946 { 947 return sysfs_emit(sysfsbuf, "%d\n", key_at_index); 948 } 949 950 static ssize_t applesmc_key_at_index_store(struct device *dev, 951 struct device_attribute *attr, const char *sysfsbuf, size_t count) 952 { 953 unsigned long newkey; 954 955 if (kstrtoul(sysfsbuf, 10, &newkey) < 0 956 || newkey >= smcreg.key_count) 957 return -EINVAL; 958 959 key_at_index = newkey; 960 return count; 961 } 962 963 static struct led_classdev applesmc_backlight = { 964 .name = "smc::kbd_backlight", 965 .default_trigger = "nand-disk", 966 .brightness_set = applesmc_brightness_set, 967 }; 968 969 static struct applesmc_node_group info_group[] = { 970 { "name", applesmc_name_show }, 971 { "key_count", applesmc_key_count_show }, 972 { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store }, 973 { "key_at_index_name", applesmc_key_at_index_name_show }, 974 { "key_at_index_type", applesmc_key_at_index_type_show }, 975 { "key_at_index_data_length", applesmc_key_at_index_data_length_show }, 976 { "key_at_index_data", applesmc_key_at_index_read_show }, 977 { } 978 }; 979 980 static struct applesmc_node_group accelerometer_group[] = { 981 { "position", applesmc_position_show }, 982 { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store }, 983 { } 984 }; 985 986 static struct applesmc_node_group light_sensor_group[] = { 987 { "light", applesmc_light_show }, 988 { } 989 }; 990 991 992 993 /* Module stuff */ 994 995 /* 996 * applesmc_destroy_nodes - remove files and free associated memory 997 */ 998 static void applesmc_destroy_nodes(struct applesmc_node_group *groups) 999 { 1000 struct applesmc_node_group *grp; 1001 struct applesmc_dev_attr *node; 1002 1003 for (grp = groups; grp->nodes; grp++) { 1004 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++) 1005 sysfs_remove_file(&pdev->dev.kobj, 1006 &node->sda.dev_attr.attr); 1007 kfree(grp->nodes); 1008 grp->nodes = NULL; 1009 } 1010 } 1011 1012 /* 1013 * applesmc_create_nodes - create a two-dimensional group of sysfs files 1014 */ 1015 static int applesmc_create_nodes(struct applesmc_node_group *groups, int num) 1016 { 1017 struct applesmc_node_group *grp; 1018 struct applesmc_dev_attr *node; 1019 struct attribute *attr; 1020 int ret, i; 1021 1022 for (grp = groups; grp->format; grp++) { 1023 grp->nodes = kzalloc_objs(*node, num + 1); 1024 if (!grp->nodes) { 1025 ret = -ENOMEM; 1026 goto out; 1027 } 1028 for (i = 0; i < num; i++) { 1029 node = &grp->nodes[i]; 1030 scnprintf(node->name, sizeof(node->name), grp->format, 1031 i + 1); 1032 node->sda.index = (grp->option << 16) | (i & 0xffff); 1033 node->sda.dev_attr.show = grp->show; 1034 node->sda.dev_attr.store = grp->store; 1035 attr = &node->sda.dev_attr.attr; 1036 sysfs_attr_init(attr); 1037 attr->name = node->name; 1038 attr->mode = 0444 | (grp->store ? 0200 : 0); 1039 ret = sysfs_create_file(&pdev->dev.kobj, attr); 1040 if (ret) { 1041 attr->name = NULL; 1042 goto out; 1043 } 1044 } 1045 } 1046 1047 return 0; 1048 out: 1049 applesmc_destroy_nodes(groups); 1050 return ret; 1051 } 1052 1053 /* Create accelerometer resources */ 1054 static int applesmc_create_accelerometer(void) 1055 { 1056 int ret; 1057 1058 if (!smcreg.has_accelerometer) 1059 return 0; 1060 1061 ret = applesmc_create_nodes(accelerometer_group, 1); 1062 if (ret) 1063 goto out; 1064 1065 applesmc_idev = input_allocate_device(); 1066 if (!applesmc_idev) { 1067 ret = -ENOMEM; 1068 goto out_sysfs; 1069 } 1070 1071 /* initial calibrate for the input device */ 1072 applesmc_calibrate(); 1073 1074 /* initialize the input device */ 1075 applesmc_idev->name = "applesmc"; 1076 applesmc_idev->id.bustype = BUS_HOST; 1077 applesmc_idev->dev.parent = &pdev->dev; 1078 input_set_abs_params(applesmc_idev, ABS_X, 1079 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT); 1080 input_set_abs_params(applesmc_idev, ABS_Y, 1081 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT); 1082 1083 ret = input_setup_polling(applesmc_idev, applesmc_idev_poll); 1084 if (ret) 1085 goto out_idev; 1086 1087 input_set_poll_interval(applesmc_idev, APPLESMC_POLL_INTERVAL); 1088 1089 ret = input_register_device(applesmc_idev); 1090 if (ret) 1091 goto out_idev; 1092 1093 return 0; 1094 1095 out_idev: 1096 input_free_device(applesmc_idev); 1097 1098 out_sysfs: 1099 applesmc_destroy_nodes(accelerometer_group); 1100 1101 out: 1102 pr_warn("driver init failed (ret=%d)!\n", ret); 1103 return ret; 1104 } 1105 1106 /* Release all resources used by the accelerometer */ 1107 static void applesmc_release_accelerometer(void) 1108 { 1109 if (!smcreg.has_accelerometer) 1110 return; 1111 input_unregister_device(applesmc_idev); 1112 applesmc_destroy_nodes(accelerometer_group); 1113 } 1114 1115 static int applesmc_create_light_sensor(void) 1116 { 1117 if (!smcreg.num_light_sensors) 1118 return 0; 1119 return applesmc_create_nodes(light_sensor_group, 1); 1120 } 1121 1122 static void applesmc_release_light_sensor(void) 1123 { 1124 if (!smcreg.num_light_sensors) 1125 return; 1126 applesmc_destroy_nodes(light_sensor_group); 1127 } 1128 1129 static int applesmc_create_key_backlight(void) 1130 { 1131 int ret; 1132 1133 if (!smcreg.has_key_backlight) 1134 return 0; 1135 applesmc_led_wq = create_singlethread_workqueue("applesmc-led"); 1136 if (!applesmc_led_wq) 1137 return -ENOMEM; 1138 ret = led_classdev_register(&pdev->dev, &applesmc_backlight); 1139 if (ret) 1140 destroy_workqueue(applesmc_led_wq); 1141 return ret; 1142 } 1143 1144 static void applesmc_release_key_backlight(void) 1145 { 1146 if (!smcreg.has_key_backlight) 1147 return; 1148 led_classdev_unregister(&applesmc_backlight); 1149 destroy_workqueue(applesmc_led_wq); 1150 } 1151 1152 static int applesmc_dmi_match(const struct dmi_system_id *id) 1153 { 1154 return 1; 1155 } 1156 1157 /* 1158 * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1". 1159 * So we need to put "Apple MacBook Pro" before "Apple MacBook". 1160 */ 1161 static const struct dmi_system_id applesmc_whitelist[] __initconst = { 1162 { applesmc_dmi_match, "Apple MacBook Air", { 1163 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1164 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") }, 1165 }, 1166 { applesmc_dmi_match, "Apple MacBook Pro", { 1167 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1168 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") }, 1169 }, 1170 { applesmc_dmi_match, "Apple MacBook", { 1171 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1172 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") }, 1173 }, 1174 { applesmc_dmi_match, "Apple Macmini", { 1175 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1176 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") }, 1177 }, 1178 { applesmc_dmi_match, "Apple MacPro", { 1179 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1180 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") }, 1181 }, 1182 { applesmc_dmi_match, "Apple iMac", { 1183 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1184 DMI_MATCH(DMI_PRODUCT_NAME, "iMac") }, 1185 }, 1186 { applesmc_dmi_match, "Apple Xserve", { 1187 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1188 DMI_MATCH(DMI_PRODUCT_NAME, "Xserve") }, 1189 }, 1190 { .ident = NULL } 1191 }; 1192 MODULE_DEVICE_TABLE(dmi, applesmc_whitelist); 1193 1194 static struct applesmc_dev_attr *fan_safe_attrs; 1195 static struct attribute **fan_safe_attr_list; 1196 static struct attribute_group fan_safe_group; 1197 static const struct attribute_group *applesmc_extra_groups[2]; 1198 1199 static u32 *applesmc_temp_config; 1200 static u32 *applesmc_fan_config; 1201 static u32 *applesmc_pwm_config; 1202 static struct hwmon_channel_info *applesmc_info_temp; 1203 static struct hwmon_channel_info *applesmc_info_fan; 1204 static struct hwmon_channel_info *applesmc_info_pwm; 1205 static const struct hwmon_channel_info **applesmc_info_arr; 1206 static struct hwmon_chip_info *applesmc_chip; 1207 1208 static void applesmc_free_hwmon(void) 1209 { 1210 kfree(applesmc_temp_config); 1211 kfree(applesmc_fan_config); 1212 kfree(applesmc_pwm_config); 1213 kfree(applesmc_info_temp); 1214 kfree(applesmc_info_fan); 1215 kfree(applesmc_info_pwm); 1216 kfree(applesmc_info_arr); 1217 kfree(applesmc_chip); 1218 kfree(fan_safe_attrs); 1219 kfree(fan_safe_attr_list); 1220 } 1221 1222 static umode_t applesmc_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type, 1223 u32 attr, int channel) 1224 { 1225 switch (type) { 1226 case hwmon_temp: 1227 if (attr == hwmon_temp_input || attr == hwmon_temp_label) 1228 return 0444; 1229 break; 1230 case hwmon_fan: 1231 switch (attr) { 1232 case hwmon_fan_input: 1233 case hwmon_fan_label: 1234 case hwmon_fan_max: 1235 return 0444; 1236 case hwmon_fan_min: 1237 case hwmon_fan_target: 1238 return 0644; 1239 default: 1240 break; 1241 } 1242 break; 1243 case hwmon_pwm: 1244 if (attr == hwmon_pwm_enable) 1245 return 0644; 1246 break; 1247 default: 1248 break; 1249 } 1250 return 0; 1251 } 1252 1253 static int applesmc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 1254 u32 attr, int channel, long *val) 1255 { 1256 int ret; 1257 1258 switch (type) { 1259 case hwmon_temp: 1260 if (attr == hwmon_temp_input) { 1261 const char *key = smcreg.index[channel]; 1262 s16 value; 1263 1264 ret = applesmc_read_s16(key, &value); 1265 if (ret) 1266 return ret; 1267 *val = 250 * (value >> 6); 1268 return 0; 1269 } 1270 break; 1271 case hwmon_fan: 1272 switch (attr) { 1273 case hwmon_fan_input: { 1274 char key[5]; 1275 u8 buffer[2]; 1276 1277 scnprintf(key, sizeof(key), "F%dAc", channel); 1278 ret = applesmc_read_key(key, buffer, 2); 1279 if (ret) 1280 return ret; 1281 *val = ((buffer[0] << 8 | buffer[1]) >> 2); 1282 return 0; 1283 } 1284 case hwmon_fan_min: { 1285 char key[5]; 1286 u8 buffer[2]; 1287 1288 scnprintf(key, sizeof(key), "F%dMn", channel); 1289 ret = applesmc_read_key(key, buffer, 2); 1290 if (ret) 1291 return ret; 1292 *val = ((buffer[0] << 8 | buffer[1]) >> 2); 1293 return 0; 1294 } 1295 case hwmon_fan_max: { 1296 char key[5]; 1297 u8 buffer[2]; 1298 1299 scnprintf(key, sizeof(key), "F%dMx", channel); 1300 ret = applesmc_read_key(key, buffer, 2); 1301 if (ret) 1302 return ret; 1303 *val = ((buffer[0] << 8 | buffer[1]) >> 2); 1304 return 0; 1305 } 1306 case hwmon_fan_target: { 1307 char key[5]; 1308 u8 buffer[2]; 1309 1310 scnprintf(key, sizeof(key), "F%dTg", channel); 1311 ret = applesmc_read_key(key, buffer, 2); 1312 if (ret) 1313 return ret; 1314 *val = ((buffer[0] << 8 | buffer[1]) >> 2); 1315 return 0; 1316 } 1317 default: 1318 break; 1319 } 1320 break; 1321 case hwmon_pwm: 1322 if (attr == hwmon_pwm_enable) { 1323 u8 buffer[2]; 1324 1325 ret = applesmc_read_key(FANS_MANUAL, buffer, 2); 1326 if (ret) 1327 return ret; 1328 *val = (((buffer[0] << 8 | buffer[1]) >> channel) & 0x01) ? 1 : 2; 1329 return 0; 1330 } 1331 break; 1332 default: 1333 break; 1334 } 1335 return -EOPNOTSUPP; 1336 } 1337 1338 static int applesmc_hwmon_write(struct device *dev, enum hwmon_sensor_types type, 1339 u32 attr, int channel, long val) 1340 { 1341 int ret; 1342 1343 switch (type) { 1344 case hwmon_fan: 1345 if (attr == hwmon_fan_min || attr == hwmon_fan_target) { 1346 char key[5]; 1347 u8 buffer[2]; 1348 const char *fmt = (attr == hwmon_fan_min) ? "F%dMn" : "F%dTg"; 1349 1350 if (val < 0 || val >= 0x4000) 1351 return -EINVAL; 1352 scnprintf(key, sizeof(key), fmt, channel); 1353 buffer[0] = (val >> 6) & 0xff; 1354 buffer[1] = (val << 2) & 0xff; 1355 return applesmc_write_key(key, buffer, 2); 1356 } 1357 break; 1358 case hwmon_pwm: 1359 if (attr == hwmon_pwm_enable) { 1360 u8 buffer[2]; 1361 u16 manual_val; 1362 const struct applesmc_entry *entry; 1363 1364 if (val != 1 && val != 2) 1365 return -EINVAL; 1366 1367 entry = applesmc_get_entry_by_key(FANS_MANUAL); 1368 if (IS_ERR(entry)) 1369 return PTR_ERR(entry); 1370 1371 mutex_lock(&smcreg.mutex); 1372 ret = read_smc(APPLESMC_READ_CMD, entry->key, buffer, 2); 1373 if (ret) 1374 goto out_unlock; 1375 manual_val = (buffer[0] << 8 | buffer[1]); 1376 if (val == 1) 1377 manual_val |= (0x01 << channel); 1378 else 1379 manual_val &= ~(0x01 << channel); 1380 buffer[0] = (manual_val >> 8) & 0xff; 1381 buffer[1] = manual_val & 0xff; 1382 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buffer, 2); 1383 out_unlock: 1384 mutex_unlock(&smcreg.mutex); 1385 return ret; 1386 } 1387 break; 1388 default: 1389 break; 1390 } 1391 return -EOPNOTSUPP; 1392 } 1393 1394 static int applesmc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type, 1395 u32 attr, int channel, const char **str) 1396 { 1397 switch (type) { 1398 case hwmon_temp: 1399 if (attr == hwmon_temp_label) { 1400 *str = smcreg.index[channel]; 1401 return 0; 1402 } 1403 break; 1404 case hwmon_fan: 1405 if (attr == hwmon_fan_label) { 1406 *str = smcreg.fan_positions[channel] + 4; 1407 return 0; 1408 } 1409 break; 1410 default: 1411 break; 1412 } 1413 return -EOPNOTSUPP; 1414 } 1415 1416 static const struct hwmon_ops applesmc_hwmon_ops = { 1417 .is_visible = applesmc_hwmon_is_visible, 1418 .read = applesmc_hwmon_read, 1419 .write = applesmc_hwmon_write, 1420 .read_string = applesmc_hwmon_read_string, 1421 }; 1422 1423 static int __init applesmc_init(void) 1424 { 1425 int i; 1426 int ret; 1427 1428 if (!dmi_check_system(applesmc_whitelist)) { 1429 pr_warn("supported laptop not found!\n"); 1430 ret = -ENODEV; 1431 goto out; 1432 } 1433 1434 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS, 1435 "applesmc")) { 1436 ret = -ENXIO; 1437 goto out; 1438 } 1439 1440 ret = platform_driver_register(&applesmc_driver); 1441 if (ret) 1442 goto out_region; 1443 1444 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT, 1445 NULL, 0); 1446 if (IS_ERR(pdev)) { 1447 ret = PTR_ERR(pdev); 1448 goto out_driver; 1449 } 1450 1451 /* create register cache */ 1452 ret = applesmc_init_smcreg(); 1453 if (ret) 1454 goto out_device; 1455 1456 ret = applesmc_create_nodes(info_group, 1); 1457 if (ret) 1458 goto out_smcreg; 1459 1460 /* allocate hwmon channel configs */ 1461 applesmc_temp_config = kcalloc(smcreg.index_count + 1, 1462 sizeof(*applesmc_temp_config), GFP_KERNEL); 1463 applesmc_fan_config = kcalloc(smcreg.fan_count + 1, 1464 sizeof(*applesmc_fan_config), GFP_KERNEL); 1465 applesmc_pwm_config = kcalloc(smcreg.fan_count + 1, 1466 sizeof(*applesmc_pwm_config), GFP_KERNEL); 1467 if (!applesmc_temp_config || !applesmc_fan_config || !applesmc_pwm_config) { 1468 ret = -ENOMEM; 1469 goto out_info; 1470 } 1471 1472 for (i = 0; i < smcreg.index_count; i++) 1473 applesmc_temp_config[i] = HWMON_T_INPUT | HWMON_T_LABEL; 1474 applesmc_temp_config[smcreg.index_count] = 0; 1475 1476 for (i = 0; i < smcreg.fan_count; i++) { 1477 applesmc_fan_config[i] = HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | 1478 HWMON_F_MAX | HWMON_F_TARGET; 1479 applesmc_pwm_config[i] = HWMON_PWM_ENABLE; 1480 } 1481 applesmc_fan_config[smcreg.fan_count] = 0; 1482 applesmc_pwm_config[smcreg.fan_count] = 0; 1483 1484 applesmc_info_temp = kzalloc_obj(*applesmc_info_temp); 1485 applesmc_info_fan = kzalloc_obj(*applesmc_info_fan); 1486 applesmc_info_pwm = kzalloc_obj(*applesmc_info_pwm); 1487 if (!applesmc_info_temp || !applesmc_info_fan || !applesmc_info_pwm) { 1488 ret = -ENOMEM; 1489 goto out_info; 1490 } 1491 1492 applesmc_info_temp->type = hwmon_temp; 1493 applesmc_info_temp->config = applesmc_temp_config; 1494 1495 applesmc_info_fan->type = hwmon_fan; 1496 applesmc_info_fan->config = applesmc_fan_config; 1497 1498 applesmc_info_pwm->type = hwmon_pwm; 1499 applesmc_info_pwm->config = applesmc_pwm_config; 1500 1501 applesmc_info_arr = kzalloc_objs(*applesmc_info_arr, 4); 1502 if (!applesmc_info_arr) { 1503 ret = -ENOMEM; 1504 goto out_info; 1505 } 1506 1507 applesmc_info_arr[0] = applesmc_info_temp; 1508 applesmc_info_arr[1] = applesmc_info_fan; 1509 applesmc_info_arr[2] = applesmc_info_pwm; 1510 applesmc_info_arr[3] = NULL; 1511 1512 applesmc_chip = kzalloc_obj(*applesmc_chip); 1513 if (!applesmc_chip) { 1514 ret = -ENOMEM; 1515 goto out_info; 1516 } 1517 1518 applesmc_chip->ops = &applesmc_hwmon_ops; 1519 applesmc_chip->info = applesmc_info_arr; 1520 1521 /* Create non-standard fanX_safe attributes group */ 1522 fan_safe_attrs = kzalloc_objs(*fan_safe_attrs, smcreg.fan_count); 1523 fan_safe_attr_list = kzalloc_objs(*fan_safe_attr_list, 1524 smcreg.fan_count + 1); 1525 if (!fan_safe_attrs || !fan_safe_attr_list) { 1526 ret = -ENOMEM; 1527 goto out_info; 1528 } 1529 1530 for (i = 0; i < smcreg.fan_count; i++) { 1531 struct applesmc_dev_attr *node = &fan_safe_attrs[i]; 1532 1533 scnprintf(node->name, sizeof(node->name), "fan%d_safe", i + 1); 1534 node->sda.index = (3 << 16) | (i & 0xffff); /* Option 3 (safe speed) */ 1535 node->sda.dev_attr.show = applesmc_show_fan_speed; 1536 node->sda.dev_attr.store = NULL; 1537 sysfs_attr_init(&node->sda.dev_attr.attr); 1538 node->sda.dev_attr.attr.name = node->name; 1539 node->sda.dev_attr.attr.mode = 0444; 1540 fan_safe_attr_list[i] = &node->sda.dev_attr.attr; 1541 } 1542 fan_safe_attr_list[smcreg.fan_count] = NULL; 1543 fan_safe_group.attrs = fan_safe_attr_list; 1544 applesmc_extra_groups[0] = &fan_safe_group; 1545 applesmc_extra_groups[1] = NULL; 1546 1547 ret = applesmc_create_accelerometer(); 1548 if (ret) 1549 goto out_info; 1550 1551 ret = applesmc_create_light_sensor(); 1552 if (ret) 1553 goto out_accelerometer; 1554 1555 ret = applesmc_create_key_backlight(); 1556 if (ret) 1557 goto out_light_sysfs; 1558 1559 hwmon_dev = hwmon_device_register_with_info(&pdev->dev, "applesmc", NULL, 1560 applesmc_chip, applesmc_extra_groups); 1561 if (IS_ERR(hwmon_dev)) { 1562 ret = PTR_ERR(hwmon_dev); 1563 goto out_light_ledclass; 1564 } 1565 1566 return 0; 1567 1568 out_light_ledclass: 1569 applesmc_release_key_backlight(); 1570 out_light_sysfs: 1571 applesmc_release_light_sensor(); 1572 out_accelerometer: 1573 applesmc_release_accelerometer(); 1574 out_info: 1575 applesmc_free_hwmon(); 1576 applesmc_destroy_nodes(info_group); 1577 out_smcreg: 1578 applesmc_destroy_smcreg(); 1579 out_device: 1580 platform_device_unregister(pdev); 1581 out_driver: 1582 platform_driver_unregister(&applesmc_driver); 1583 out_region: 1584 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); 1585 out: 1586 pr_warn("driver init failed (ret=%d)!\n", ret); 1587 return ret; 1588 } 1589 1590 static void __exit applesmc_exit(void) 1591 { 1592 hwmon_device_unregister(hwmon_dev); 1593 applesmc_release_key_backlight(); 1594 applesmc_release_light_sensor(); 1595 applesmc_release_accelerometer(); 1596 applesmc_destroy_nodes(info_group); 1597 applesmc_destroy_smcreg(); 1598 platform_device_unregister(pdev); 1599 platform_driver_unregister(&applesmc_driver); 1600 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); 1601 applesmc_free_hwmon(); 1602 } 1603 1604 module_init(applesmc_init); 1605 module_exit(applesmc_exit); 1606 1607 MODULE_AUTHOR("Nicolas Boichat"); 1608 MODULE_DESCRIPTION("Apple SMC"); 1609 MODULE_LICENSE("GPL v2"); 1610