1 /* 2 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras 3 * 4 * Copyright © 2010 Intel Corporation 5 * Copyright © 2010 David Woodhouse <dwmw2@infradead.org> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20 * 02110-1301, USA. 21 */ 22 23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 24 25 #include <linux/kernel.h> 26 #include <linux/module.h> 27 #include <linux/init.h> 28 #include <linux/types.h> 29 #include <linux/acpi.h> 30 #include <linux/rfkill.h> 31 #include <linux/platform_device.h> 32 #include <linux/input.h> 33 #include <linux/input/sparse-keymap.h> 34 #include <linux/backlight.h> 35 #include <linux/fb.h> 36 #include <linux/debugfs.h> 37 #include <linux/seq_file.h> 38 #include <linux/i8042.h> 39 #include <linux/dmi.h> 40 #include <linux/device.h> 41 42 #define IDEAPAD_RFKILL_DEV_NUM (3) 43 44 #define CFG_BT_BIT (16) 45 #define CFG_3G_BIT (17) 46 #define CFG_WIFI_BIT (18) 47 #define CFG_CAMERA_BIT (19) 48 49 enum { 50 VPCCMD_R_VPC1 = 0x10, 51 VPCCMD_R_BL_MAX, 52 VPCCMD_R_BL, 53 VPCCMD_W_BL, 54 VPCCMD_R_WIFI, 55 VPCCMD_W_WIFI, 56 VPCCMD_R_BT, 57 VPCCMD_W_BT, 58 VPCCMD_R_BL_POWER, 59 VPCCMD_R_NOVO, 60 VPCCMD_R_VPC2, 61 VPCCMD_R_TOUCHPAD, 62 VPCCMD_W_TOUCHPAD, 63 VPCCMD_R_CAMERA, 64 VPCCMD_W_CAMERA, 65 VPCCMD_R_3G, 66 VPCCMD_W_3G, 67 VPCCMD_R_ODD, /* 0x21 */ 68 VPCCMD_W_FAN, 69 VPCCMD_R_RF, 70 VPCCMD_W_RF, 71 VPCCMD_R_FAN = 0x2B, 72 VPCCMD_R_SPECIAL_BUTTONS = 0x31, 73 VPCCMD_W_BL_POWER = 0x33, 74 }; 75 76 struct ideapad_rfk_priv { 77 int dev; 78 struct ideapad_private *priv; 79 }; 80 81 struct ideapad_private { 82 struct acpi_device *adev; 83 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM]; 84 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM]; 85 struct platform_device *platform_device; 86 struct input_dev *inputdev; 87 struct backlight_device *blightdev; 88 struct dentry *debug; 89 unsigned long cfg; 90 bool has_hw_rfkill_switch; 91 bool has_touchpad_control; 92 }; 93 94 static bool no_bt_rfkill; 95 module_param(no_bt_rfkill, bool, 0444); 96 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth."); 97 98 /* 99 * ACPI Helpers 100 */ 101 #define IDEAPAD_EC_TIMEOUT (100) /* in ms */ 102 103 static int read_method_int(acpi_handle handle, const char *method, int *val) 104 { 105 acpi_status status; 106 unsigned long long result; 107 108 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result); 109 if (ACPI_FAILURE(status)) { 110 *val = -1; 111 return -1; 112 } else { 113 *val = result; 114 return 0; 115 } 116 } 117 118 static int method_vpcr(acpi_handle handle, int cmd, int *ret) 119 { 120 acpi_status status; 121 unsigned long long result; 122 struct acpi_object_list params; 123 union acpi_object in_obj; 124 125 params.count = 1; 126 params.pointer = &in_obj; 127 in_obj.type = ACPI_TYPE_INTEGER; 128 in_obj.integer.value = cmd; 129 130 status = acpi_evaluate_integer(handle, "VPCR", ¶ms, &result); 131 132 if (ACPI_FAILURE(status)) { 133 *ret = -1; 134 return -1; 135 } else { 136 *ret = result; 137 return 0; 138 } 139 } 140 141 static int method_vpcw(acpi_handle handle, int cmd, int data) 142 { 143 struct acpi_object_list params; 144 union acpi_object in_obj[2]; 145 acpi_status status; 146 147 params.count = 2; 148 params.pointer = in_obj; 149 in_obj[0].type = ACPI_TYPE_INTEGER; 150 in_obj[0].integer.value = cmd; 151 in_obj[1].type = ACPI_TYPE_INTEGER; 152 in_obj[1].integer.value = data; 153 154 status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL); 155 if (status != AE_OK) 156 return -1; 157 return 0; 158 } 159 160 static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data) 161 { 162 int val; 163 unsigned long int end_jiffies; 164 165 if (method_vpcw(handle, 1, cmd)) 166 return -1; 167 168 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1; 169 time_before(jiffies, end_jiffies);) { 170 schedule(); 171 if (method_vpcr(handle, 1, &val)) 172 return -1; 173 if (val == 0) { 174 if (method_vpcr(handle, 0, &val)) 175 return -1; 176 *data = val; 177 return 0; 178 } 179 } 180 pr_err("timeout in read_ec_cmd\n"); 181 return -1; 182 } 183 184 static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data) 185 { 186 int val; 187 unsigned long int end_jiffies; 188 189 if (method_vpcw(handle, 0, data)) 190 return -1; 191 if (method_vpcw(handle, 1, cmd)) 192 return -1; 193 194 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1; 195 time_before(jiffies, end_jiffies);) { 196 schedule(); 197 if (method_vpcr(handle, 1, &val)) 198 return -1; 199 if (val == 0) 200 return 0; 201 } 202 pr_err("timeout in write_ec_cmd\n"); 203 return -1; 204 } 205 206 /* 207 * debugfs 208 */ 209 static int debugfs_status_show(struct seq_file *s, void *data) 210 { 211 struct ideapad_private *priv = s->private; 212 unsigned long value; 213 214 if (!priv) 215 return -EINVAL; 216 217 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value)) 218 seq_printf(s, "Backlight max:\t%lu\n", value); 219 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value)) 220 seq_printf(s, "Backlight now:\t%lu\n", value); 221 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value)) 222 seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off"); 223 seq_printf(s, "=====================\n"); 224 225 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value)) 226 seq_printf(s, "Radio status:\t%s(%lu)\n", 227 value ? "On" : "Off", value); 228 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value)) 229 seq_printf(s, "Wifi status:\t%s(%lu)\n", 230 value ? "On" : "Off", value); 231 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value)) 232 seq_printf(s, "BT status:\t%s(%lu)\n", 233 value ? "On" : "Off", value); 234 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value)) 235 seq_printf(s, "3G status:\t%s(%lu)\n", 236 value ? "On" : "Off", value); 237 seq_printf(s, "=====================\n"); 238 239 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) 240 seq_printf(s, "Touchpad status:%s(%lu)\n", 241 value ? "On" : "Off", value); 242 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value)) 243 seq_printf(s, "Camera status:\t%s(%lu)\n", 244 value ? "On" : "Off", value); 245 246 return 0; 247 } 248 249 static int debugfs_status_open(struct inode *inode, struct file *file) 250 { 251 return single_open(file, debugfs_status_show, inode->i_private); 252 } 253 254 static const struct file_operations debugfs_status_fops = { 255 .owner = THIS_MODULE, 256 .open = debugfs_status_open, 257 .read = seq_read, 258 .llseek = seq_lseek, 259 .release = single_release, 260 }; 261 262 static int debugfs_cfg_show(struct seq_file *s, void *data) 263 { 264 struct ideapad_private *priv = s->private; 265 266 if (!priv) { 267 seq_printf(s, "cfg: N/A\n"); 268 } else { 269 seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ", 270 priv->cfg); 271 if (test_bit(CFG_BT_BIT, &priv->cfg)) 272 seq_printf(s, "Bluetooth "); 273 if (test_bit(CFG_3G_BIT, &priv->cfg)) 274 seq_printf(s, "3G "); 275 if (test_bit(CFG_WIFI_BIT, &priv->cfg)) 276 seq_printf(s, "Wireless "); 277 if (test_bit(CFG_CAMERA_BIT, &priv->cfg)) 278 seq_printf(s, "Camera "); 279 seq_printf(s, "\nGraphic: "); 280 switch ((priv->cfg)&0x700) { 281 case 0x100: 282 seq_printf(s, "Intel"); 283 break; 284 case 0x200: 285 seq_printf(s, "ATI"); 286 break; 287 case 0x300: 288 seq_printf(s, "Nvidia"); 289 break; 290 case 0x400: 291 seq_printf(s, "Intel and ATI"); 292 break; 293 case 0x500: 294 seq_printf(s, "Intel and Nvidia"); 295 break; 296 } 297 seq_printf(s, "\n"); 298 } 299 return 0; 300 } 301 302 static int debugfs_cfg_open(struct inode *inode, struct file *file) 303 { 304 return single_open(file, debugfs_cfg_show, inode->i_private); 305 } 306 307 static const struct file_operations debugfs_cfg_fops = { 308 .owner = THIS_MODULE, 309 .open = debugfs_cfg_open, 310 .read = seq_read, 311 .llseek = seq_lseek, 312 .release = single_release, 313 }; 314 315 static int ideapad_debugfs_init(struct ideapad_private *priv) 316 { 317 struct dentry *node; 318 319 priv->debug = debugfs_create_dir("ideapad", NULL); 320 if (priv->debug == NULL) { 321 pr_err("failed to create debugfs directory"); 322 goto errout; 323 } 324 325 node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv, 326 &debugfs_cfg_fops); 327 if (!node) { 328 pr_err("failed to create cfg in debugfs"); 329 goto errout; 330 } 331 332 node = debugfs_create_file("status", S_IRUGO, priv->debug, priv, 333 &debugfs_status_fops); 334 if (!node) { 335 pr_err("failed to create status in debugfs"); 336 goto errout; 337 } 338 339 return 0; 340 341 errout: 342 return -ENOMEM; 343 } 344 345 static void ideapad_debugfs_exit(struct ideapad_private *priv) 346 { 347 debugfs_remove_recursive(priv->debug); 348 priv->debug = NULL; 349 } 350 351 /* 352 * sysfs 353 */ 354 static ssize_t show_ideapad_cam(struct device *dev, 355 struct device_attribute *attr, 356 char *buf) 357 { 358 unsigned long result; 359 struct ideapad_private *priv = dev_get_drvdata(dev); 360 361 if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result)) 362 return sprintf(buf, "-1\n"); 363 return sprintf(buf, "%lu\n", result); 364 } 365 366 static ssize_t store_ideapad_cam(struct device *dev, 367 struct device_attribute *attr, 368 const char *buf, size_t count) 369 { 370 int ret, state; 371 struct ideapad_private *priv = dev_get_drvdata(dev); 372 373 if (!count) 374 return 0; 375 if (sscanf(buf, "%i", &state) != 1) 376 return -EINVAL; 377 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state); 378 if (ret < 0) 379 return -EIO; 380 return count; 381 } 382 383 static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam); 384 385 static ssize_t show_ideapad_fan(struct device *dev, 386 struct device_attribute *attr, 387 char *buf) 388 { 389 unsigned long result; 390 struct ideapad_private *priv = dev_get_drvdata(dev); 391 392 if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result)) 393 return sprintf(buf, "-1\n"); 394 return sprintf(buf, "%lu\n", result); 395 } 396 397 static ssize_t store_ideapad_fan(struct device *dev, 398 struct device_attribute *attr, 399 const char *buf, size_t count) 400 { 401 int ret, state; 402 struct ideapad_private *priv = dev_get_drvdata(dev); 403 404 if (!count) 405 return 0; 406 if (sscanf(buf, "%i", &state) != 1) 407 return -EINVAL; 408 if (state < 0 || state > 4 || state == 3) 409 return -EINVAL; 410 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state); 411 if (ret < 0) 412 return -EIO; 413 return count; 414 } 415 416 static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan); 417 418 static struct attribute *ideapad_attributes[] = { 419 &dev_attr_camera_power.attr, 420 &dev_attr_fan_mode.attr, 421 NULL 422 }; 423 424 static umode_t ideapad_is_visible(struct kobject *kobj, 425 struct attribute *attr, 426 int idx) 427 { 428 struct device *dev = container_of(kobj, struct device, kobj); 429 struct ideapad_private *priv = dev_get_drvdata(dev); 430 bool supported; 431 432 if (attr == &dev_attr_camera_power.attr) 433 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg)); 434 else if (attr == &dev_attr_fan_mode.attr) { 435 unsigned long value; 436 supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN, 437 &value); 438 } else 439 supported = true; 440 441 return supported ? attr->mode : 0; 442 } 443 444 static const struct attribute_group ideapad_attribute_group = { 445 .is_visible = ideapad_is_visible, 446 .attrs = ideapad_attributes 447 }; 448 449 /* 450 * Rfkill 451 */ 452 struct ideapad_rfk_data { 453 char *name; 454 int cfgbit; 455 int opcode; 456 int type; 457 }; 458 459 const const struct ideapad_rfk_data ideapad_rfk_data[] = { 460 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN }, 461 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH }, 462 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN }, 463 }; 464 465 static int ideapad_rfk_set(void *data, bool blocked) 466 { 467 struct ideapad_rfk_priv *priv = data; 468 469 return write_ec_cmd(priv->priv->adev->handle, priv->dev, !blocked); 470 } 471 472 static struct rfkill_ops ideapad_rfk_ops = { 473 .set_block = ideapad_rfk_set, 474 }; 475 476 static void ideapad_sync_rfk_state(struct ideapad_private *priv) 477 { 478 unsigned long hw_blocked = 0; 479 int i; 480 481 if (priv->has_hw_rfkill_switch) { 482 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked)) 483 return; 484 hw_blocked = !hw_blocked; 485 } 486 487 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) 488 if (priv->rfk[i]) 489 rfkill_set_hw_state(priv->rfk[i], hw_blocked); 490 } 491 492 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev) 493 { 494 int ret; 495 unsigned long sw_blocked; 496 497 if (no_bt_rfkill && 498 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) { 499 /* Force to enable bluetooth when no_bt_rfkill=1 */ 500 write_ec_cmd(priv->adev->handle, 501 ideapad_rfk_data[dev].opcode, 1); 502 return 0; 503 } 504 priv->rfk_priv[dev].dev = dev; 505 priv->rfk_priv[dev].priv = priv; 506 507 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, 508 &priv->platform_device->dev, 509 ideapad_rfk_data[dev].type, 510 &ideapad_rfk_ops, 511 &priv->rfk_priv[dev]); 512 if (!priv->rfk[dev]) 513 return -ENOMEM; 514 515 if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1, 516 &sw_blocked)) { 517 rfkill_init_sw_state(priv->rfk[dev], 0); 518 } else { 519 sw_blocked = !sw_blocked; 520 rfkill_init_sw_state(priv->rfk[dev], sw_blocked); 521 } 522 523 ret = rfkill_register(priv->rfk[dev]); 524 if (ret) { 525 rfkill_destroy(priv->rfk[dev]); 526 return ret; 527 } 528 return 0; 529 } 530 531 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev) 532 { 533 if (!priv->rfk[dev]) 534 return; 535 536 rfkill_unregister(priv->rfk[dev]); 537 rfkill_destroy(priv->rfk[dev]); 538 } 539 540 /* 541 * Platform device 542 */ 543 static int ideapad_sysfs_init(struct ideapad_private *priv) 544 { 545 return sysfs_create_group(&priv->platform_device->dev.kobj, 546 &ideapad_attribute_group); 547 } 548 549 static void ideapad_sysfs_exit(struct ideapad_private *priv) 550 { 551 sysfs_remove_group(&priv->platform_device->dev.kobj, 552 &ideapad_attribute_group); 553 } 554 555 /* 556 * input device 557 */ 558 static const struct key_entry ideapad_keymap[] = { 559 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } }, 560 { KE_KEY, 7, { KEY_CAMERA } }, 561 { KE_KEY, 11, { KEY_F16 } }, 562 { KE_KEY, 13, { KEY_WLAN } }, 563 { KE_KEY, 16, { KEY_PROG1 } }, 564 { KE_KEY, 17, { KEY_PROG2 } }, 565 { KE_KEY, 64, { KEY_PROG3 } }, 566 { KE_KEY, 65, { KEY_PROG4 } }, 567 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } }, 568 { KE_KEY, 67, { KEY_TOUCHPAD_ON } }, 569 { KE_END, 0 }, 570 }; 571 572 static int ideapad_input_init(struct ideapad_private *priv) 573 { 574 struct input_dev *inputdev; 575 int error; 576 577 inputdev = input_allocate_device(); 578 if (!inputdev) 579 return -ENOMEM; 580 581 inputdev->name = "Ideapad extra buttons"; 582 inputdev->phys = "ideapad/input0"; 583 inputdev->id.bustype = BUS_HOST; 584 inputdev->dev.parent = &priv->platform_device->dev; 585 586 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL); 587 if (error) { 588 pr_err("Unable to setup input device keymap\n"); 589 goto err_free_dev; 590 } 591 592 error = input_register_device(inputdev); 593 if (error) { 594 pr_err("Unable to register input device\n"); 595 goto err_free_keymap; 596 } 597 598 priv->inputdev = inputdev; 599 return 0; 600 601 err_free_keymap: 602 sparse_keymap_free(inputdev); 603 err_free_dev: 604 input_free_device(inputdev); 605 return error; 606 } 607 608 static void ideapad_input_exit(struct ideapad_private *priv) 609 { 610 sparse_keymap_free(priv->inputdev); 611 input_unregister_device(priv->inputdev); 612 priv->inputdev = NULL; 613 } 614 615 static void ideapad_input_report(struct ideapad_private *priv, 616 unsigned long scancode) 617 { 618 sparse_keymap_report_event(priv->inputdev, scancode, 1, true); 619 } 620 621 static void ideapad_input_novokey(struct ideapad_private *priv) 622 { 623 unsigned long long_pressed; 624 625 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed)) 626 return; 627 if (long_pressed) 628 ideapad_input_report(priv, 17); 629 else 630 ideapad_input_report(priv, 16); 631 } 632 633 static void ideapad_check_special_buttons(struct ideapad_private *priv) 634 { 635 unsigned long bit, value; 636 637 read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value); 638 639 for (bit = 0; bit < 16; bit++) { 640 if (test_bit(bit, &value)) { 641 switch (bit) { 642 case 0: /* Z580 */ 643 case 6: /* Z570 */ 644 /* Thermal Management button */ 645 ideapad_input_report(priv, 65); 646 break; 647 case 1: 648 /* OneKey Theater button */ 649 ideapad_input_report(priv, 64); 650 break; 651 default: 652 pr_info("Unknown special button: %lu\n", bit); 653 break; 654 } 655 } 656 } 657 } 658 659 /* 660 * backlight 661 */ 662 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev) 663 { 664 struct ideapad_private *priv = bl_get_data(blightdev); 665 unsigned long now; 666 667 if (!priv) 668 return -EINVAL; 669 670 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now)) 671 return -EIO; 672 return now; 673 } 674 675 static int ideapad_backlight_update_status(struct backlight_device *blightdev) 676 { 677 struct ideapad_private *priv = bl_get_data(blightdev); 678 679 if (!priv) 680 return -EINVAL; 681 682 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL, 683 blightdev->props.brightness)) 684 return -EIO; 685 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER, 686 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1)) 687 return -EIO; 688 689 return 0; 690 } 691 692 static const struct backlight_ops ideapad_backlight_ops = { 693 .get_brightness = ideapad_backlight_get_brightness, 694 .update_status = ideapad_backlight_update_status, 695 }; 696 697 static int ideapad_backlight_init(struct ideapad_private *priv) 698 { 699 struct backlight_device *blightdev; 700 struct backlight_properties props; 701 unsigned long max, now, power; 702 703 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max)) 704 return -EIO; 705 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now)) 706 return -EIO; 707 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power)) 708 return -EIO; 709 710 memset(&props, 0, sizeof(struct backlight_properties)); 711 props.max_brightness = max; 712 props.type = BACKLIGHT_PLATFORM; 713 blightdev = backlight_device_register("ideapad", 714 &priv->platform_device->dev, 715 priv, 716 &ideapad_backlight_ops, 717 &props); 718 if (IS_ERR(blightdev)) { 719 pr_err("Could not register backlight device\n"); 720 return PTR_ERR(blightdev); 721 } 722 723 priv->blightdev = blightdev; 724 blightdev->props.brightness = now; 725 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; 726 backlight_update_status(blightdev); 727 728 return 0; 729 } 730 731 static void ideapad_backlight_exit(struct ideapad_private *priv) 732 { 733 if (priv->blightdev) 734 backlight_device_unregister(priv->blightdev); 735 priv->blightdev = NULL; 736 } 737 738 static void ideapad_backlight_notify_power(struct ideapad_private *priv) 739 { 740 unsigned long power; 741 struct backlight_device *blightdev = priv->blightdev; 742 743 if (!blightdev) 744 return; 745 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power)) 746 return; 747 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; 748 } 749 750 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv) 751 { 752 unsigned long now; 753 754 /* if we control brightness via acpi video driver */ 755 if (priv->blightdev == NULL) { 756 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now); 757 return; 758 } 759 760 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY); 761 } 762 763 /* 764 * module init/exit 765 */ 766 static void ideapad_sync_touchpad_state(struct ideapad_private *priv) 767 { 768 unsigned long value; 769 770 if (!priv->has_touchpad_control) 771 return; 772 773 /* Without reading from EC touchpad LED doesn't switch state */ 774 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) { 775 /* Some IdeaPads don't really turn off touchpad - they only 776 * switch the LED state. We (de)activate KBC AUX port to turn 777 * touchpad off and on. We send KEY_TOUCHPAD_OFF and 778 * KEY_TOUCHPAD_ON to not to get out of sync with LED */ 779 unsigned char param; 780 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : 781 I8042_CMD_AUX_DISABLE); 782 ideapad_input_report(priv, value ? 67 : 66); 783 } 784 } 785 786 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data) 787 { 788 struct ideapad_private *priv = data; 789 unsigned long vpc1, vpc2, vpc_bit; 790 791 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1)) 792 return; 793 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2)) 794 return; 795 796 vpc1 = (vpc2 << 8) | vpc1; 797 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) { 798 if (test_bit(vpc_bit, &vpc1)) { 799 switch (vpc_bit) { 800 case 9: 801 ideapad_sync_rfk_state(priv); 802 break; 803 case 13: 804 case 11: 805 case 7: 806 case 6: 807 ideapad_input_report(priv, vpc_bit); 808 break; 809 case 5: 810 ideapad_sync_touchpad_state(priv); 811 break; 812 case 4: 813 ideapad_backlight_notify_brightness(priv); 814 break; 815 case 3: 816 ideapad_input_novokey(priv); 817 break; 818 case 2: 819 ideapad_backlight_notify_power(priv); 820 break; 821 case 0: 822 ideapad_check_special_buttons(priv); 823 break; 824 default: 825 pr_info("Unknown event: %lu\n", vpc_bit); 826 } 827 } 828 } 829 } 830 831 /* 832 * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF 833 * always results in 0 on these models, causing ideapad_laptop to wrongly 834 * report all radios as hardware-blocked. 835 */ 836 static struct dmi_system_id no_hw_rfkill_list[] = { 837 { 838 .ident = "Lenovo Yoga 2 11 / 13 / Pro", 839 .matches = { 840 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 841 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"), 842 }, 843 }, 844 {} 845 }; 846 847 /* 848 * Some models don't offer touchpad ctrl through the ideapad interface, causing 849 * ideapad_sync_touchpad_state to send wrong touchpad enable/disable events. 850 */ 851 static struct dmi_system_id no_touchpad_ctrl_list[] = { 852 { 853 .ident = "Lenovo Yoga 1 series", 854 .matches = { 855 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 856 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo IdeaPad Yoga"), 857 }, 858 }, 859 { 860 .ident = "Lenovo Yoga 2 11 / 13 / Pro", 861 .matches = { 862 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 863 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"), 864 }, 865 }, 866 {} 867 }; 868 869 static int ideapad_acpi_add(struct platform_device *pdev) 870 { 871 int ret, i; 872 int cfg; 873 struct ideapad_private *priv; 874 struct acpi_device *adev; 875 876 ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev); 877 if (ret) 878 return -ENODEV; 879 880 if (read_method_int(adev->handle, "_CFG", &cfg)) 881 return -ENODEV; 882 883 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 884 if (!priv) 885 return -ENOMEM; 886 887 dev_set_drvdata(&pdev->dev, priv); 888 priv->cfg = cfg; 889 priv->adev = adev; 890 priv->platform_device = pdev; 891 priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list); 892 priv->has_touchpad_control = !dmi_check_system(no_touchpad_ctrl_list); 893 894 ret = ideapad_sysfs_init(priv); 895 if (ret) 896 return ret; 897 898 ret = ideapad_debugfs_init(priv); 899 if (ret) 900 goto debugfs_failed; 901 902 ret = ideapad_input_init(priv); 903 if (ret) 904 goto input_failed; 905 906 /* 907 * On some models without a hw-switch (the yoga 2 13 at least) 908 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work. 909 */ 910 if (!priv->has_hw_rfkill_switch) 911 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1); 912 913 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) 914 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg)) 915 ideapad_register_rfkill(priv, i); 916 917 ideapad_sync_rfk_state(priv); 918 ideapad_sync_touchpad_state(priv); 919 920 if (!acpi_video_backlight_support()) { 921 ret = ideapad_backlight_init(priv); 922 if (ret && ret != -ENODEV) 923 goto backlight_failed; 924 } 925 ret = acpi_install_notify_handler(adev->handle, 926 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv); 927 if (ret) 928 goto notification_failed; 929 930 return 0; 931 notification_failed: 932 ideapad_backlight_exit(priv); 933 backlight_failed: 934 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) 935 ideapad_unregister_rfkill(priv, i); 936 ideapad_input_exit(priv); 937 input_failed: 938 ideapad_debugfs_exit(priv); 939 debugfs_failed: 940 ideapad_sysfs_exit(priv); 941 return ret; 942 } 943 944 static int ideapad_acpi_remove(struct platform_device *pdev) 945 { 946 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev); 947 int i; 948 949 acpi_remove_notify_handler(priv->adev->handle, 950 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify); 951 ideapad_backlight_exit(priv); 952 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) 953 ideapad_unregister_rfkill(priv, i); 954 ideapad_input_exit(priv); 955 ideapad_debugfs_exit(priv); 956 ideapad_sysfs_exit(priv); 957 dev_set_drvdata(&pdev->dev, NULL); 958 959 return 0; 960 } 961 962 #ifdef CONFIG_PM_SLEEP 963 static int ideapad_acpi_resume(struct device *device) 964 { 965 struct ideapad_private *priv; 966 967 if (!device) 968 return -EINVAL; 969 priv = dev_get_drvdata(device); 970 971 ideapad_sync_rfk_state(priv); 972 ideapad_sync_touchpad_state(priv); 973 return 0; 974 } 975 #endif 976 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume); 977 978 static const struct acpi_device_id ideapad_device_ids[] = { 979 { "VPC2004", 0}, 980 { "", 0}, 981 }; 982 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids); 983 984 static struct platform_driver ideapad_acpi_driver = { 985 .probe = ideapad_acpi_add, 986 .remove = ideapad_acpi_remove, 987 .driver = { 988 .name = "ideapad_acpi", 989 .owner = THIS_MODULE, 990 .pm = &ideapad_pm, 991 .acpi_match_table = ACPI_PTR(ideapad_device_ids), 992 }, 993 }; 994 995 module_platform_driver(ideapad_acpi_driver); 996 997 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 998 MODULE_DESCRIPTION("IdeaPad ACPI Extras"); 999 MODULE_LICENSE("GPL"); 1000