2 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
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.
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.
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
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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 #include <acpi/video.h>
43 #define IDEAPAD_RFKILL_DEV_NUM (3)
45 #define BM_CONSERVATION_BIT (5)
46 #define HA_FNLOCK_BIT (10)
48 #define CFG_BT_BIT (16)
49 #define CFG_3G_BIT (17)
50 #define CFG_WIFI_BIT (18)
51 #define CFG_CAMERA_BIT (19)
53 #if IS_ENABLED(CONFIG_ACPI_WMI)
54 static const char *const ideapad_wmi_fnesc_events[] = {
55 "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
56 "56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
61 BMCMD_CONSERVATION_ON = 3,
62 BMCMD_CONSERVATION_OFF = 5,
63 HACMD_FNLOCK_ON = 0xe,
64 HACMD_FNLOCK_OFF = 0xf,
85 VPCCMD_R_ODD, /* 0x21 */
90 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
91 VPCCMD_W_BL_POWER = 0x33,
94 struct ideapad_rfk_priv {
96 struct ideapad_private *priv;
99 struct ideapad_private {
100 struct acpi_device *adev;
101 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
102 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
103 struct platform_device *platform_device;
104 struct input_dev *inputdev;
105 struct backlight_device *blightdev;
106 struct dentry *debug;
108 bool has_hw_rfkill_switch;
109 const char *fnesc_guid;
112 static bool no_bt_rfkill;
113 module_param(no_bt_rfkill, bool, 0444);
114 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
119 #define IDEAPAD_EC_TIMEOUT (200) /* in ms */
121 static int read_method_int(acpi_handle handle, const char *method, int *val)
124 unsigned long long result;
126 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
127 if (ACPI_FAILURE(status)) {
136 static int method_gbmd(acpi_handle handle, unsigned long *ret)
140 result = read_method_int(handle, "GBMD", &val);
145 static int method_int1(acpi_handle handle, char *method, int cmd)
149 status = acpi_execute_simple_method(handle, method, cmd);
150 return ACPI_FAILURE(status) ? -1 : 0;
153 static int method_vpcr(acpi_handle handle, int cmd, int *ret)
156 unsigned long long result;
157 struct acpi_object_list params;
158 union acpi_object in_obj;
161 params.pointer = &in_obj;
162 in_obj.type = ACPI_TYPE_INTEGER;
163 in_obj.integer.value = cmd;
165 status = acpi_evaluate_integer(handle, "VPCR", ¶ms, &result);
167 if (ACPI_FAILURE(status)) {
176 static int method_vpcw(acpi_handle handle, int cmd, int data)
178 struct acpi_object_list params;
179 union acpi_object in_obj[2];
183 params.pointer = in_obj;
184 in_obj[0].type = ACPI_TYPE_INTEGER;
185 in_obj[0].integer.value = cmd;
186 in_obj[1].type = ACPI_TYPE_INTEGER;
187 in_obj[1].integer.value = data;
189 status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL);
195 static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
198 unsigned long int end_jiffies;
200 if (method_vpcw(handle, 1, cmd))
203 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
204 time_before(jiffies, end_jiffies);) {
206 if (method_vpcr(handle, 1, &val))
209 if (method_vpcr(handle, 0, &val))
215 pr_err("timeout in read_ec_cmd\n");
219 static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
222 unsigned long int end_jiffies;
224 if (method_vpcw(handle, 0, data))
226 if (method_vpcw(handle, 1, cmd))
229 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
230 time_before(jiffies, end_jiffies);) {
232 if (method_vpcr(handle, 1, &val))
237 pr_err("timeout in %s\n", __func__);
244 static int debugfs_status_show(struct seq_file *s, void *data)
246 struct ideapad_private *priv = s->private;
252 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
253 seq_printf(s, "Backlight max:\t%lu\n", value);
254 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
255 seq_printf(s, "Backlight now:\t%lu\n", value);
256 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
257 seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
258 seq_printf(s, "=====================\n");
260 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
261 seq_printf(s, "Radio status:\t%s(%lu)\n",
262 value ? "On" : "Off", value);
263 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
264 seq_printf(s, "Wifi status:\t%s(%lu)\n",
265 value ? "On" : "Off", value);
266 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
267 seq_printf(s, "BT status:\t%s(%lu)\n",
268 value ? "On" : "Off", value);
269 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
270 seq_printf(s, "3G status:\t%s(%lu)\n",
271 value ? "On" : "Off", value);
272 seq_printf(s, "=====================\n");
274 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
275 seq_printf(s, "Touchpad status:%s(%lu)\n",
276 value ? "On" : "Off", value);
277 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
278 seq_printf(s, "Camera status:\t%s(%lu)\n",
279 value ? "On" : "Off", value);
280 seq_puts(s, "=====================\n");
282 if (!method_gbmd(priv->adev->handle, &value)) {
283 seq_printf(s, "Conservation mode:\t%s(%lu)\n",
284 test_bit(BM_CONSERVATION_BIT, &value) ? "On" : "Off",
290 DEFINE_SHOW_ATTRIBUTE(debugfs_status);
292 static int debugfs_cfg_show(struct seq_file *s, void *data)
294 struct ideapad_private *priv = s->private;
297 seq_printf(s, "cfg: N/A\n");
299 seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
301 if (test_bit(CFG_BT_BIT, &priv->cfg))
302 seq_printf(s, "Bluetooth ");
303 if (test_bit(CFG_3G_BIT, &priv->cfg))
304 seq_printf(s, "3G ");
305 if (test_bit(CFG_WIFI_BIT, &priv->cfg))
306 seq_printf(s, "Wireless ");
307 if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
308 seq_printf(s, "Camera ");
309 seq_printf(s, "\nGraphic: ");
310 switch ((priv->cfg)&0x700) {
312 seq_printf(s, "Intel");
315 seq_printf(s, "ATI");
318 seq_printf(s, "Nvidia");
321 seq_printf(s, "Intel and ATI");
324 seq_printf(s, "Intel and Nvidia");
331 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
333 static int ideapad_debugfs_init(struct ideapad_private *priv)
337 priv->debug = debugfs_create_dir("ideapad", NULL);
338 if (priv->debug == NULL) {
339 pr_err("failed to create debugfs directory");
343 node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
346 pr_err("failed to create cfg in debugfs");
350 node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
351 &debugfs_status_fops);
353 pr_err("failed to create status in debugfs");
363 static void ideapad_debugfs_exit(struct ideapad_private *priv)
365 debugfs_remove_recursive(priv->debug);
372 static ssize_t show_ideapad_cam(struct device *dev,
373 struct device_attribute *attr,
376 unsigned long result;
377 struct ideapad_private *priv = dev_get_drvdata(dev);
379 if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
380 return sprintf(buf, "-1\n");
381 return sprintf(buf, "%lu\n", result);
384 static ssize_t store_ideapad_cam(struct device *dev,
385 struct device_attribute *attr,
386 const char *buf, size_t count)
389 struct ideapad_private *priv = dev_get_drvdata(dev);
393 if (sscanf(buf, "%i", &state) != 1)
395 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
401 static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
403 static ssize_t show_ideapad_fan(struct device *dev,
404 struct device_attribute *attr,
407 unsigned long result;
408 struct ideapad_private *priv = dev_get_drvdata(dev);
410 if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
411 return sprintf(buf, "-1\n");
412 return sprintf(buf, "%lu\n", result);
415 static ssize_t store_ideapad_fan(struct device *dev,
416 struct device_attribute *attr,
417 const char *buf, size_t count)
420 struct ideapad_private *priv = dev_get_drvdata(dev);
424 if (sscanf(buf, "%i", &state) != 1)
426 if (state < 0 || state > 4 || state == 3)
428 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
434 static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
436 static ssize_t touchpad_show(struct device *dev,
437 struct device_attribute *attr,
440 struct ideapad_private *priv = dev_get_drvdata(dev);
441 unsigned long result;
443 if (read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result))
444 return sprintf(buf, "-1\n");
445 return sprintf(buf, "%lu\n", result);
448 /* Switch to RO for now: It might be revisited in the future */
449 static ssize_t __maybe_unused touchpad_store(struct device *dev,
450 struct device_attribute *attr,
451 const char *buf, size_t count)
453 struct ideapad_private *priv = dev_get_drvdata(dev);
457 ret = kstrtobool(buf, &state);
461 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
467 static DEVICE_ATTR_RO(touchpad);
469 static ssize_t conservation_mode_show(struct device *dev,
470 struct device_attribute *attr,
473 struct ideapad_private *priv = dev_get_drvdata(dev);
474 unsigned long result;
476 if (method_gbmd(priv->adev->handle, &result))
477 return sprintf(buf, "-1\n");
478 return sprintf(buf, "%u\n", test_bit(BM_CONSERVATION_BIT, &result));
481 static ssize_t conservation_mode_store(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
485 struct ideapad_private *priv = dev_get_drvdata(dev);
489 ret = kstrtobool(buf, &state);
493 ret = method_int1(priv->adev->handle, "SBMC", state ?
494 BMCMD_CONSERVATION_ON :
495 BMCMD_CONSERVATION_OFF);
501 static DEVICE_ATTR_RW(conservation_mode);
503 static ssize_t fn_lock_show(struct device *dev,
504 struct device_attribute *attr,
507 struct ideapad_private *priv = dev_get_drvdata(dev);
508 unsigned long result;
510 int fail = read_method_int(priv->adev->handle, "HALS", &hals);
513 return sprintf(buf, "-1\n");
516 return sprintf(buf, "%u\n", test_bit(HA_FNLOCK_BIT, &result));
519 static ssize_t fn_lock_store(struct device *dev,
520 struct device_attribute *attr,
521 const char *buf, size_t count)
523 struct ideapad_private *priv = dev_get_drvdata(dev);
527 ret = kstrtobool(buf, &state);
531 ret = method_int1(priv->adev->handle, "SALS", state ?
539 static DEVICE_ATTR_RW(fn_lock);
542 static struct attribute *ideapad_attributes[] = {
543 &dev_attr_camera_power.attr,
544 &dev_attr_fan_mode.attr,
545 &dev_attr_touchpad.attr,
546 &dev_attr_conservation_mode.attr,
547 &dev_attr_fn_lock.attr,
551 static umode_t ideapad_is_visible(struct kobject *kobj,
552 struct attribute *attr,
555 struct device *dev = container_of(kobj, struct device, kobj);
556 struct ideapad_private *priv = dev_get_drvdata(dev);
559 if (attr == &dev_attr_camera_power.attr)
560 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
561 else if (attr == &dev_attr_fan_mode.attr) {
563 supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
565 } else if (attr == &dev_attr_conservation_mode.attr) {
566 supported = acpi_has_method(priv->adev->handle, "GBMD") &&
567 acpi_has_method(priv->adev->handle, "SBMC");
568 } else if (attr == &dev_attr_fn_lock.attr) {
569 supported = acpi_has_method(priv->adev->handle, "HALS") &&
570 acpi_has_method(priv->adev->handle, "SALS");
574 return supported ? attr->mode : 0;
577 static const struct attribute_group ideapad_attribute_group = {
578 .is_visible = ideapad_is_visible,
579 .attrs = ideapad_attributes
585 struct ideapad_rfk_data {
592 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
593 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
594 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
595 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
598 static int ideapad_rfk_set(void *data, bool blocked)
600 struct ideapad_rfk_priv *priv = data;
601 int opcode = ideapad_rfk_data[priv->dev].opcode;
603 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
606 static const struct rfkill_ops ideapad_rfk_ops = {
607 .set_block = ideapad_rfk_set,
610 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
612 unsigned long hw_blocked = 0;
615 if (priv->has_hw_rfkill_switch) {
616 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
618 hw_blocked = !hw_blocked;
621 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
623 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
626 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
629 unsigned long sw_blocked;
632 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
633 /* Force to enable bluetooth when no_bt_rfkill=1 */
634 write_ec_cmd(priv->adev->handle,
635 ideapad_rfk_data[dev].opcode, 1);
638 priv->rfk_priv[dev].dev = dev;
639 priv->rfk_priv[dev].priv = priv;
641 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
642 &priv->platform_device->dev,
643 ideapad_rfk_data[dev].type,
645 &priv->rfk_priv[dev]);
649 if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
651 rfkill_init_sw_state(priv->rfk[dev], 0);
653 sw_blocked = !sw_blocked;
654 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
657 ret = rfkill_register(priv->rfk[dev]);
659 rfkill_destroy(priv->rfk[dev]);
665 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
670 rfkill_unregister(priv->rfk[dev]);
671 rfkill_destroy(priv->rfk[dev]);
677 static int ideapad_sysfs_init(struct ideapad_private *priv)
679 return sysfs_create_group(&priv->platform_device->dev.kobj,
680 &ideapad_attribute_group);
683 static void ideapad_sysfs_exit(struct ideapad_private *priv)
685 sysfs_remove_group(&priv->platform_device->dev.kobj,
686 &ideapad_attribute_group);
692 static const struct key_entry ideapad_keymap[] = {
693 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
694 { KE_KEY, 7, { KEY_CAMERA } },
695 { KE_KEY, 8, { KEY_MICMUTE } },
696 { KE_KEY, 11, { KEY_F16 } },
697 { KE_KEY, 13, { KEY_WLAN } },
698 { KE_KEY, 16, { KEY_PROG1 } },
699 { KE_KEY, 17, { KEY_PROG2 } },
700 { KE_KEY, 64, { KEY_PROG3 } },
701 { KE_KEY, 65, { KEY_PROG4 } },
702 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
703 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
704 { KE_KEY, 128, { KEY_ESC } },
709 static int ideapad_input_init(struct ideapad_private *priv)
711 struct input_dev *inputdev;
714 inputdev = input_allocate_device();
718 inputdev->name = "Ideapad extra buttons";
719 inputdev->phys = "ideapad/input0";
720 inputdev->id.bustype = BUS_HOST;
721 inputdev->dev.parent = &priv->platform_device->dev;
723 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
725 pr_err("Unable to setup input device keymap\n");
729 error = input_register_device(inputdev);
731 pr_err("Unable to register input device\n");
735 priv->inputdev = inputdev;
739 input_free_device(inputdev);
743 static void ideapad_input_exit(struct ideapad_private *priv)
745 input_unregister_device(priv->inputdev);
746 priv->inputdev = NULL;
749 static void ideapad_input_report(struct ideapad_private *priv,
750 unsigned long scancode)
752 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
755 static void ideapad_input_novokey(struct ideapad_private *priv)
757 unsigned long long_pressed;
759 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
762 ideapad_input_report(priv, 17);
764 ideapad_input_report(priv, 16);
767 static void ideapad_check_special_buttons(struct ideapad_private *priv)
769 unsigned long bit, value;
771 read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
773 for (bit = 0; bit < 16; bit++) {
774 if (test_bit(bit, &value)) {
778 /* Thermal Management button */
779 ideapad_input_report(priv, 65);
782 /* OneKey Theater button */
783 ideapad_input_report(priv, 64);
786 pr_info("Unknown special button: %lu\n", bit);
796 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
798 struct ideapad_private *priv = bl_get_data(blightdev);
804 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
809 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
811 struct ideapad_private *priv = bl_get_data(blightdev);
816 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
817 blightdev->props.brightness))
819 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
820 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
826 static const struct backlight_ops ideapad_backlight_ops = {
827 .get_brightness = ideapad_backlight_get_brightness,
828 .update_status = ideapad_backlight_update_status,
831 static int ideapad_backlight_init(struct ideapad_private *priv)
833 struct backlight_device *blightdev;
834 struct backlight_properties props;
835 unsigned long max, now, power;
837 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
839 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
841 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
844 memset(&props, 0, sizeof(struct backlight_properties));
845 props.max_brightness = max;
846 props.type = BACKLIGHT_PLATFORM;
847 blightdev = backlight_device_register("ideapad",
848 &priv->platform_device->dev,
850 &ideapad_backlight_ops,
852 if (IS_ERR(blightdev)) {
853 pr_err("Could not register backlight device\n");
854 return PTR_ERR(blightdev);
857 priv->blightdev = blightdev;
858 blightdev->props.brightness = now;
859 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
860 backlight_update_status(blightdev);
865 static void ideapad_backlight_exit(struct ideapad_private *priv)
867 backlight_device_unregister(priv->blightdev);
868 priv->blightdev = NULL;
871 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
874 struct backlight_device *blightdev = priv->blightdev;
878 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
880 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
883 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
887 /* if we control brightness via acpi video driver */
888 if (priv->blightdev == NULL) {
889 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
893 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
899 static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
903 /* Without reading from EC touchpad LED doesn't switch state */
904 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
905 /* Some IdeaPads don't really turn off touchpad - they only
906 * switch the LED state. We (de)activate KBC AUX port to turn
907 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
908 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
910 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE :
911 I8042_CMD_AUX_DISABLE);
912 ideapad_input_report(priv, value ? 67 : 66);
916 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
918 struct ideapad_private *priv = data;
919 unsigned long vpc1, vpc2, vpc_bit;
921 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
923 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
926 vpc1 = (vpc2 << 8) | vpc1;
927 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
928 if (test_bit(vpc_bit, &vpc1)) {
931 ideapad_sync_rfk_state(priv);
938 ideapad_input_report(priv, vpc_bit);
941 ideapad_sync_touchpad_state(priv);
944 ideapad_backlight_notify_brightness(priv);
947 ideapad_input_novokey(priv);
950 ideapad_backlight_notify_power(priv);
953 ideapad_check_special_buttons(priv);
956 /* Some IdeaPads report event 1 every ~20
957 * seconds while on battery power; some
958 * report this when changing to/from tablet
959 * mode. Squelch this event.
963 pr_info("Unknown event: %lu\n", vpc_bit);
969 #if IS_ENABLED(CONFIG_ACPI_WMI)
970 static void ideapad_wmi_notify(u32 value, void *context)
974 ideapad_input_report(context, value);
977 pr_info("Unknown WMI event %u\n", value);
983 * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
984 * always results in 0 on these models, causing ideapad_laptop to wrongly
985 * report all radios as hardware-blocked.
987 static const struct dmi_system_id no_hw_rfkill_list[] = {
989 .ident = "Lenovo RESCUER R720-15IKBN",
991 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
992 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo R720-15IKBN"),
996 .ident = "Lenovo G40-30",
998 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
999 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G40-30"),
1003 .ident = "Lenovo G50-30",
1005 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1006 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G50-30"),
1010 .ident = "Lenovo V310-14IKB",
1012 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1013 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14IKB"),
1017 .ident = "Lenovo V310-14ISK",
1019 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1020 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14ISK"),
1024 .ident = "Lenovo V310-15IKB",
1026 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1027 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15IKB"),
1031 .ident = "Lenovo V310-15ISK",
1033 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1034 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15ISK"),
1038 .ident = "Lenovo V510-15IKB",
1040 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1041 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V510-15IKB"),
1045 .ident = "Lenovo ideapad 300-15IBR",
1047 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1048 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IBR"),
1052 .ident = "Lenovo ideapad 300-15IKB",
1054 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1055 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IKB"),
1059 .ident = "Lenovo ideapad 300S-11IBR",
1061 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1062 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300S-11BR"),
1066 .ident = "Lenovo ideapad 310-15ABR",
1068 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1069 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ABR"),
1073 .ident = "Lenovo ideapad 310-15IAP",
1075 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1076 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IAP"),
1080 .ident = "Lenovo ideapad 310-15IKB",
1082 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1083 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IKB"),
1087 .ident = "Lenovo ideapad 310-15ISK",
1089 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1090 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ISK"),
1094 .ident = "Lenovo ideapad Y700-14ISK",
1096 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1097 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-14ISK"),
1101 .ident = "Lenovo ideapad Y700-15ACZ",
1103 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1104 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ACZ"),
1108 .ident = "Lenovo ideapad Y700-15ISK",
1110 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1111 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ISK"),
1115 .ident = "Lenovo ideapad Y700 Touch-15ISK",
1117 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1118 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700 Touch-15ISK"),
1122 .ident = "Lenovo ideapad Y700-17ISK",
1124 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1125 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-17ISK"),
1129 .ident = "Lenovo ideapad MIIX 720-12IKB",
1131 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1132 DMI_MATCH(DMI_PRODUCT_VERSION, "MIIX 720-12IKB"),
1136 .ident = "Lenovo Legion Y520-15IKB",
1138 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1139 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y520-15IKB"),
1143 .ident = "Lenovo Y520-15IKBM",
1145 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1146 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y520-15IKBM"),
1150 .ident = "Lenovo Legion Y720-15IKB",
1152 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1153 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKB"),
1157 .ident = "Lenovo Legion Y720-15IKBN",
1159 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1160 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKBN"),
1164 .ident = "Lenovo Y720-15IKBM",
1166 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1167 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKBM"),
1171 .ident = "Lenovo Yoga 2 11 / 13 / Pro",
1173 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1174 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
1178 .ident = "Lenovo Yoga 2 11 / 13 / Pro",
1180 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1181 DMI_MATCH(DMI_BOARD_NAME, "Yoga2"),
1185 .ident = "Lenovo Yoga 3 1170 / 1470",
1187 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1188 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 3"),
1192 .ident = "Lenovo Yoga 3 Pro 1370",
1194 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1195 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
1199 .ident = "Lenovo Yoga 700",
1201 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1202 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 700"),
1206 .ident = "Lenovo Yoga 900",
1208 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1209 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 900"),
1213 .ident = "Lenovo Yoga 900",
1215 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1216 DMI_MATCH(DMI_BOARD_NAME, "VIUU4"),
1220 .ident = "Lenovo YOGA 910-13IKB",
1222 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1223 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 910-13IKB"),
1227 .ident = "Lenovo YOGA 920-13IKB",
1229 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1230 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 920-13IKB"),
1234 .ident = "Lenovo Zhaoyang E42-80",
1236 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1237 DMI_MATCH(DMI_PRODUCT_VERSION, "ZHAOYANG E42-80"),
1243 static int ideapad_acpi_add(struct platform_device *pdev)
1247 struct ideapad_private *priv;
1248 struct acpi_device *adev;
1250 ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
1254 if (read_method_int(adev->handle, "_CFG", &cfg))
1257 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1261 dev_set_drvdata(&pdev->dev, priv);
1264 priv->platform_device = pdev;
1265 priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
1267 ret = ideapad_sysfs_init(priv);
1271 ret = ideapad_debugfs_init(priv);
1273 goto debugfs_failed;
1275 ret = ideapad_input_init(priv);
1280 * On some models without a hw-switch (the yoga 2 13 at least)
1281 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1283 if (!priv->has_hw_rfkill_switch)
1284 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1286 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1287 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1288 ideapad_register_rfkill(priv, i);
1290 ideapad_sync_rfk_state(priv);
1291 ideapad_sync_touchpad_state(priv);
1293 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1294 ret = ideapad_backlight_init(priv);
1295 if (ret && ret != -ENODEV)
1296 goto backlight_failed;
1298 ret = acpi_install_notify_handler(adev->handle,
1299 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
1301 goto notification_failed;
1303 #if IS_ENABLED(CONFIG_ACPI_WMI)
1304 for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
1305 ret = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
1306 ideapad_wmi_notify, priv);
1308 priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
1312 if (ret != AE_OK && ret != AE_NOT_EXIST)
1313 goto notification_failed_wmi;
1317 #if IS_ENABLED(CONFIG_ACPI_WMI)
1318 notification_failed_wmi:
1319 acpi_remove_notify_handler(priv->adev->handle,
1320 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1322 notification_failed:
1323 ideapad_backlight_exit(priv);
1325 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1326 ideapad_unregister_rfkill(priv, i);
1327 ideapad_input_exit(priv);
1329 ideapad_debugfs_exit(priv);
1331 ideapad_sysfs_exit(priv);
1335 static int ideapad_acpi_remove(struct platform_device *pdev)
1337 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1340 #if IS_ENABLED(CONFIG_ACPI_WMI)
1341 if (priv->fnesc_guid)
1342 wmi_remove_notify_handler(priv->fnesc_guid);
1344 acpi_remove_notify_handler(priv->adev->handle,
1345 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1346 ideapad_backlight_exit(priv);
1347 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1348 ideapad_unregister_rfkill(priv, i);
1349 ideapad_input_exit(priv);
1350 ideapad_debugfs_exit(priv);
1351 ideapad_sysfs_exit(priv);
1352 dev_set_drvdata(&pdev->dev, NULL);
1357 #ifdef CONFIG_PM_SLEEP
1358 static int ideapad_acpi_resume(struct device *device)
1360 struct ideapad_private *priv;
1364 priv = dev_get_drvdata(device);
1366 ideapad_sync_rfk_state(priv);
1367 ideapad_sync_touchpad_state(priv);
1371 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1373 static const struct acpi_device_id ideapad_device_ids[] = {
1377 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1379 static struct platform_driver ideapad_acpi_driver = {
1380 .probe = ideapad_acpi_add,
1381 .remove = ideapad_acpi_remove,
1383 .name = "ideapad_acpi",
1385 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1389 module_platform_driver(ideapad_acpi_driver);
1391 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1392 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1393 MODULE_LICENSE("GPL");