1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
5 * Copyright © 2010 Intel Corporation
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/acpi.h>
12 #include <linux/backlight.h>
13 #include <linux/bitops.h>
14 #include <linux/bug.h>
15 #include <linux/debugfs.h>
16 #include <linux/device.h>
17 #include <linux/dmi.h>
19 #include <linux/i8042.h>
20 #include <linux/init.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/leds.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/platform_profile.h>
29 #include <linux/rfkill.h>
30 #include <linux/seq_file.h>
31 #include <linux/sysfs.h>
32 #include <linux/types.h>
34 #include <acpi/video.h>
36 #include <dt-bindings/leds/common.h>
38 #define IDEAPAD_RFKILL_DEV_NUM 3
40 #if IS_ENABLED(CONFIG_ACPI_WMI)
41 static const char *const ideapad_wmi_fnesc_events[] = {
42 "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
43 "56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
44 "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", /* Legion 5 */
51 CFG_CAP_WIFI_BIT = 18,
53 CFG_CAP_TOUCHPAD_BIT = 30,
57 GBMD_CONSERVATION_STATE_BIT = 5,
61 SBMC_CONSERVATION_ON = 3,
62 SBMC_CONSERVATION_OFF = 5,
66 HALS_KBD_BL_SUPPORT_BIT = 4,
67 HALS_KBD_BL_STATE_BIT = 5,
68 HALS_USB_CHARGING_SUPPORT_BIT = 6,
69 HALS_USB_CHARGING_STATE_BIT = 7,
70 HALS_FNLOCK_SUPPORT_BIT = 9,
71 HALS_FNLOCK_STATE_BIT = 10,
72 HALS_HOTKEYS_PRIMARY_BIT = 11,
77 SALS_KBD_BL_OFF = 0x9,
78 SALS_USB_CHARGING_ON = 0xa,
79 SALS_USB_CHARGING_OFF = 0xb,
81 SALS_FNLOCK_OFF = 0xf,
102 VPCCMD_R_ODD, /* 0x21 */
107 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
108 VPCCMD_W_BL_POWER = 0x33,
111 struct ideapad_dytc_priv {
112 enum platform_profile_option current_profile;
113 struct platform_profile_handler pprof;
114 struct mutex mutex; /* protects the DYTC interface */
115 struct ideapad_private *priv;
118 struct ideapad_rfk_priv {
120 struct ideapad_private *priv;
123 struct ideapad_private {
124 struct acpi_device *adev;
125 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
126 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
127 struct platform_device *platform_device;
128 struct input_dev *inputdev;
129 struct backlight_device *blightdev;
130 struct ideapad_dytc_priv *dytc;
131 struct dentry *debug;
133 const char *fnesc_guid;
135 bool conservation_mode : 1;
139 bool hw_rfkill_switch : 1;
141 bool touchpad_ctrl_via_ec : 1;
142 bool usb_charging : 1;
146 struct led_classdev led;
147 unsigned int last_brightness;
151 static bool no_bt_rfkill;
152 module_param(no_bt_rfkill, bool, 0444);
153 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
155 static bool allow_v4_dytc;
156 module_param(allow_v4_dytc, bool, 0444);
157 MODULE_PARM_DESC(allow_v4_dytc, "Enable DYTC version 4 platform-profile support.");
162 #define IDEAPAD_EC_TIMEOUT 200 /* in ms */
164 static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
166 unsigned long long result;
169 status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
170 if (ACPI_FAILURE(status))
178 static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
180 acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
182 return ACPI_FAILURE(status) ? -EIO : 0;
185 static int eval_gbmd(acpi_handle handle, unsigned long *res)
187 return eval_int(handle, "GBMD", res);
190 static int exec_sbmc(acpi_handle handle, unsigned long arg)
192 return exec_simple_method(handle, "SBMC", arg);
195 static int eval_hals(acpi_handle handle, unsigned long *res)
197 return eval_int(handle, "HALS", res);
200 static int exec_sals(acpi_handle handle, unsigned long arg)
202 return exec_simple_method(handle, "SALS", arg);
205 static int eval_int_with_arg(acpi_handle handle, const char *name, unsigned long arg, unsigned long *res)
207 struct acpi_object_list params;
208 unsigned long long result;
209 union acpi_object in_obj;
213 params.pointer = &in_obj;
214 in_obj.type = ACPI_TYPE_INTEGER;
215 in_obj.integer.value = arg;
217 status = acpi_evaluate_integer(handle, (char *)name, ¶ms, &result);
218 if (ACPI_FAILURE(status))
227 static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
229 return eval_int_with_arg(handle, "DYTC", cmd, res);
232 static int eval_vpcr(acpi_handle handle, unsigned long cmd, unsigned long *res)
234 return eval_int_with_arg(handle, "VPCR", cmd, res);
237 static int eval_vpcw(acpi_handle handle, unsigned long cmd, unsigned long data)
239 struct acpi_object_list params;
240 union acpi_object in_obj[2];
244 params.pointer = in_obj;
245 in_obj[0].type = ACPI_TYPE_INTEGER;
246 in_obj[0].integer.value = cmd;
247 in_obj[1].type = ACPI_TYPE_INTEGER;
248 in_obj[1].integer.value = data;
250 status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL);
251 if (ACPI_FAILURE(status))
257 static int read_ec_data(acpi_handle handle, unsigned long cmd, unsigned long *data)
259 unsigned long end_jiffies, val;
262 err = eval_vpcw(handle, 1, cmd);
266 end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
268 while (time_before(jiffies, end_jiffies)) {
271 err = eval_vpcr(handle, 1, &val);
276 return eval_vpcr(handle, 0, data);
279 acpi_handle_err(handle, "timeout in %s\n", __func__);
284 static int write_ec_cmd(acpi_handle handle, unsigned long cmd, unsigned long data)
286 unsigned long end_jiffies, val;
289 err = eval_vpcw(handle, 0, data);
293 err = eval_vpcw(handle, 1, cmd);
297 end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
299 while (time_before(jiffies, end_jiffies)) {
302 err = eval_vpcr(handle, 1, &val);
310 acpi_handle_err(handle, "timeout in %s\n", __func__);
318 static int debugfs_status_show(struct seq_file *s, void *data)
320 struct ideapad_private *priv = s->private;
323 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
324 seq_printf(s, "Backlight max: %lu\n", value);
325 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
326 seq_printf(s, "Backlight now: %lu\n", value);
327 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
328 seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
330 seq_puts(s, "=====================\n");
332 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
333 seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
334 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
335 seq_printf(s, "Wifi status: %s (%lu)\n", value ? "on" : "off", value);
336 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
337 seq_printf(s, "BT status: %s (%lu)\n", value ? "on" : "off", value);
338 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
339 seq_printf(s, "3G status: %s (%lu)\n", value ? "on" : "off", value);
341 seq_puts(s, "=====================\n");
343 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
344 seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
345 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
346 seq_printf(s, "Camera status: %s (%lu)\n", value ? "on" : "off", value);
348 seq_puts(s, "=====================\n");
350 if (!eval_gbmd(priv->adev->handle, &value))
351 seq_printf(s, "GBMD: %#010lx\n", value);
352 if (!eval_hals(priv->adev->handle, &value))
353 seq_printf(s, "HALS: %#010lx\n", value);
357 DEFINE_SHOW_ATTRIBUTE(debugfs_status);
359 static int debugfs_cfg_show(struct seq_file *s, void *data)
361 struct ideapad_private *priv = s->private;
363 seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
365 seq_puts(s, "Capabilities:");
366 if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
367 seq_puts(s, " bluetooth");
368 if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
370 if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
371 seq_puts(s, " wifi");
372 if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
373 seq_puts(s, " camera");
374 if (test_bit(CFG_CAP_TOUCHPAD_BIT, &priv->cfg))
375 seq_puts(s, " touchpad");
378 seq_puts(s, "Graphics: ");
379 switch (priv->cfg & 0x700) {
381 seq_puts(s, "Intel");
387 seq_puts(s, "Nvidia");
390 seq_puts(s, "Intel and ATI");
393 seq_puts(s, "Intel and Nvidia");
400 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
402 static void ideapad_debugfs_init(struct ideapad_private *priv)
406 dir = debugfs_create_dir("ideapad", NULL);
409 debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
410 debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
413 static void ideapad_debugfs_exit(struct ideapad_private *priv)
415 debugfs_remove_recursive(priv->debug);
422 static ssize_t camera_power_show(struct device *dev,
423 struct device_attribute *attr,
426 struct ideapad_private *priv = dev_get_drvdata(dev);
427 unsigned long result;
430 err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
434 return sysfs_emit(buf, "%d\n", !!result);
437 static ssize_t camera_power_store(struct device *dev,
438 struct device_attribute *attr,
439 const char *buf, size_t count)
441 struct ideapad_private *priv = dev_get_drvdata(dev);
445 err = kstrtobool(buf, &state);
449 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
456 static DEVICE_ATTR_RW(camera_power);
458 static ssize_t conservation_mode_show(struct device *dev,
459 struct device_attribute *attr,
462 struct ideapad_private *priv = dev_get_drvdata(dev);
463 unsigned long result;
466 err = eval_gbmd(priv->adev->handle, &result);
470 return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
473 static ssize_t conservation_mode_store(struct device *dev,
474 struct device_attribute *attr,
475 const char *buf, size_t count)
477 struct ideapad_private *priv = dev_get_drvdata(dev);
481 err = kstrtobool(buf, &state);
485 err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
492 static DEVICE_ATTR_RW(conservation_mode);
494 static ssize_t fan_mode_show(struct device *dev,
495 struct device_attribute *attr,
498 struct ideapad_private *priv = dev_get_drvdata(dev);
499 unsigned long result;
502 err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
506 return sysfs_emit(buf, "%lu\n", result);
509 static ssize_t fan_mode_store(struct device *dev,
510 struct device_attribute *attr,
511 const char *buf, size_t count)
513 struct ideapad_private *priv = dev_get_drvdata(dev);
517 err = kstrtouint(buf, 0, &state);
521 if (state > 4 || state == 3)
524 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
531 static DEVICE_ATTR_RW(fan_mode);
533 static ssize_t fn_lock_show(struct device *dev,
534 struct device_attribute *attr,
537 struct ideapad_private *priv = dev_get_drvdata(dev);
541 err = eval_hals(priv->adev->handle, &hals);
545 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_FNLOCK_STATE_BIT, &hals));
548 static ssize_t fn_lock_store(struct device *dev,
549 struct device_attribute *attr,
550 const char *buf, size_t count)
552 struct ideapad_private *priv = dev_get_drvdata(dev);
556 err = kstrtobool(buf, &state);
560 err = exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
567 static DEVICE_ATTR_RW(fn_lock);
569 static ssize_t touchpad_show(struct device *dev,
570 struct device_attribute *attr,
573 struct ideapad_private *priv = dev_get_drvdata(dev);
574 unsigned long result;
577 err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
581 return sysfs_emit(buf, "%d\n", !!result);
584 static ssize_t touchpad_store(struct device *dev,
585 struct device_attribute *attr,
586 const char *buf, size_t count)
588 struct ideapad_private *priv = dev_get_drvdata(dev);
592 err = kstrtobool(buf, &state);
596 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
603 static DEVICE_ATTR_RW(touchpad);
605 static ssize_t usb_charging_show(struct device *dev,
606 struct device_attribute *attr,
609 struct ideapad_private *priv = dev_get_drvdata(dev);
613 err = eval_hals(priv->adev->handle, &hals);
617 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
620 static ssize_t usb_charging_store(struct device *dev,
621 struct device_attribute *attr,
622 const char *buf, size_t count)
624 struct ideapad_private *priv = dev_get_drvdata(dev);
628 err = kstrtobool(buf, &state);
632 err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
639 static DEVICE_ATTR_RW(usb_charging);
641 static struct attribute *ideapad_attributes[] = {
642 &dev_attr_camera_power.attr,
643 &dev_attr_conservation_mode.attr,
644 &dev_attr_fan_mode.attr,
645 &dev_attr_fn_lock.attr,
646 &dev_attr_touchpad.attr,
647 &dev_attr_usb_charging.attr,
651 static umode_t ideapad_is_visible(struct kobject *kobj,
652 struct attribute *attr,
655 struct device *dev = kobj_to_dev(kobj);
656 struct ideapad_private *priv = dev_get_drvdata(dev);
657 bool supported = true;
659 if (attr == &dev_attr_camera_power.attr)
660 supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
661 else if (attr == &dev_attr_conservation_mode.attr)
662 supported = priv->features.conservation_mode;
663 else if (attr == &dev_attr_fan_mode.attr)
664 supported = priv->features.fan_mode;
665 else if (attr == &dev_attr_fn_lock.attr)
666 supported = priv->features.fn_lock;
667 else if (attr == &dev_attr_touchpad.attr)
668 supported = priv->features.touchpad_ctrl_via_ec &&
669 test_bit(CFG_CAP_TOUCHPAD_BIT, &priv->cfg);
670 else if (attr == &dev_attr_usb_charging.attr)
671 supported = priv->features.usb_charging;
673 return supported ? attr->mode : 0;
676 static const struct attribute_group ideapad_attribute_group = {
677 .is_visible = ideapad_is_visible,
678 .attrs = ideapad_attributes
682 * DYTC Platform profile
684 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
685 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
686 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
687 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
689 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
690 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
691 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
693 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
694 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
696 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
697 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
698 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
700 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
701 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
702 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
704 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
705 #define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
706 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
708 #define DYTC_SET_COMMAND(function, mode, on) \
709 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
710 (mode) << DYTC_SET_MODE_BIT | \
711 (on) << DYTC_SET_VALID_BIT)
713 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
715 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
717 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
720 case DYTC_MODE_LOW_POWER:
721 *profile = PLATFORM_PROFILE_LOW_POWER;
723 case DYTC_MODE_BALANCE:
724 *profile = PLATFORM_PROFILE_BALANCED;
726 case DYTC_MODE_PERFORM:
727 *profile = PLATFORM_PROFILE_PERFORMANCE;
729 default: /* Unknown mode */
736 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
739 case PLATFORM_PROFILE_LOW_POWER:
740 *perfmode = DYTC_MODE_LOW_POWER;
742 case PLATFORM_PROFILE_BALANCED:
743 *perfmode = DYTC_MODE_BALANCE;
745 case PLATFORM_PROFILE_PERFORMANCE:
746 *perfmode = DYTC_MODE_PERFORM;
748 default: /* Unknown profile */
756 * dytc_profile_get: Function to register with platform_profile
757 * handler. Returns current platform profile.
759 static int dytc_profile_get(struct platform_profile_handler *pprof,
760 enum platform_profile_option *profile)
762 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
764 *profile = dytc->current_profile;
769 * Helper function - check if we are in CQL mode and if we are
773 * If not in CQL mode, just run the command
775 static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
776 unsigned long *output)
778 int err, cmd_err, cur_funcmode;
780 /* Determine if we are in CQL mode. This alters the commands we do */
781 err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
785 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
786 /* Check if we're OK to return immediately */
787 if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
790 if (cur_funcmode == DYTC_FUNCTION_CQL) {
791 err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
796 cmd_err = eval_dytc(priv->adev->handle, cmd, output);
797 /* Check return condition after we've restored CQL state */
799 if (cur_funcmode == DYTC_FUNCTION_CQL) {
800 err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
809 * dytc_profile_set: Function to register with platform_profile
810 * handler. Sets current platform profile.
812 static int dytc_profile_set(struct platform_profile_handler *pprof,
813 enum platform_profile_option profile)
815 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
816 struct ideapad_private *priv = dytc->priv;
817 unsigned long output;
820 err = mutex_lock_interruptible(&dytc->mutex);
824 if (profile == PLATFORM_PROFILE_BALANCED) {
825 /* To get back to balanced mode we just issue a reset command */
826 err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
832 err = convert_profile_to_dytc(profile, &perfmode);
836 /* Determine if we are in CQL mode. This alters the commands we do */
837 err = dytc_cql_command(priv, DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
843 /* Success - update current profile */
844 dytc->current_profile = profile;
847 mutex_unlock(&dytc->mutex);
852 static void dytc_profile_refresh(struct ideapad_private *priv)
854 enum platform_profile_option profile;
855 unsigned long output;
858 mutex_lock(&priv->dytc->mutex);
859 err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
860 mutex_unlock(&priv->dytc->mutex);
864 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
866 if (convert_dytc_to_profile(perfmode, &profile))
869 if (profile != priv->dytc->current_profile) {
870 priv->dytc->current_profile = profile;
871 platform_profile_notify();
875 static const struct dmi_system_id ideapad_dytc_v4_allow_table[] = {
877 /* Ideapad 5 Pro 16ACH6 */
879 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
880 DMI_MATCH(DMI_PRODUCT_NAME, "82L5")
884 /* Ideapad 5 15ITL05 */
886 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
887 DMI_MATCH(DMI_PRODUCT_VERSION, "IdeaPad 5 15ITL05")
893 static int ideapad_dytc_profile_init(struct ideapad_private *priv)
895 int err, dytc_version;
896 unsigned long output;
898 if (!priv->features.dytc)
901 err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
902 /* For all other errors we can flag the failure */
906 /* Check DYTC is enabled and supports mode setting */
907 if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output)) {
908 dev_info(&priv->platform_device->dev, "DYTC_QUERY_ENABLE_BIT returned false\n");
912 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
914 if (dytc_version < 4) {
915 dev_info(&priv->platform_device->dev, "DYTC_VERSION < 4 is not supported\n");
919 if (dytc_version < 5 &&
920 !(allow_v4_dytc || dmi_check_system(ideapad_dytc_v4_allow_table))) {
921 dev_info(&priv->platform_device->dev,
922 "DYTC_VERSION 4 support may not work. Pass ideapad_laptop.allow_v4_dytc=Y on the kernel commandline to enable\n");
926 priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
930 mutex_init(&priv->dytc->mutex);
932 priv->dytc->priv = priv;
933 priv->dytc->pprof.profile_get = dytc_profile_get;
934 priv->dytc->pprof.profile_set = dytc_profile_set;
936 /* Setup supported modes */
937 set_bit(PLATFORM_PROFILE_LOW_POWER, priv->dytc->pprof.choices);
938 set_bit(PLATFORM_PROFILE_BALANCED, priv->dytc->pprof.choices);
939 set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->dytc->pprof.choices);
941 /* Create platform_profile structure and register */
942 err = platform_profile_register(&priv->dytc->pprof);
946 /* Ensure initial values are correct */
947 dytc_profile_refresh(priv);
952 mutex_destroy(&priv->dytc->mutex);
959 static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
964 platform_profile_remove();
965 mutex_destroy(&priv->dytc->mutex);
974 struct ideapad_rfk_data {
981 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
982 { "ideapad_wlan", CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
983 { "ideapad_bluetooth", CFG_CAP_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
984 { "ideapad_3g", CFG_CAP_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
987 static int ideapad_rfk_set(void *data, bool blocked)
989 struct ideapad_rfk_priv *priv = data;
990 int opcode = ideapad_rfk_data[priv->dev].opcode;
992 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
995 static const struct rfkill_ops ideapad_rfk_ops = {
996 .set_block = ideapad_rfk_set,
999 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
1001 unsigned long hw_blocked = 0;
1004 if (priv->features.hw_rfkill_switch) {
1005 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
1007 hw_blocked = !hw_blocked;
1010 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1012 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
1015 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
1017 unsigned long rf_enabled;
1020 if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
1021 /* Force to enable bluetooth when no_bt_rfkill=1 */
1022 write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
1026 priv->rfk_priv[dev].dev = dev;
1027 priv->rfk_priv[dev].priv = priv;
1029 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
1030 &priv->platform_device->dev,
1031 ideapad_rfk_data[dev].type,
1033 &priv->rfk_priv[dev]);
1034 if (!priv->rfk[dev])
1037 err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
1041 rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
1043 err = rfkill_register(priv->rfk[dev]);
1045 rfkill_destroy(priv->rfk[dev]);
1050 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1052 if (!priv->rfk[dev])
1055 rfkill_unregister(priv->rfk[dev]);
1056 rfkill_destroy(priv->rfk[dev]);
1062 static int ideapad_sysfs_init(struct ideapad_private *priv)
1064 return device_add_group(&priv->platform_device->dev,
1065 &ideapad_attribute_group);
1068 static void ideapad_sysfs_exit(struct ideapad_private *priv)
1070 device_remove_group(&priv->platform_device->dev,
1071 &ideapad_attribute_group);
1077 static const struct key_entry ideapad_keymap[] = {
1078 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
1079 { KE_KEY, 7, { KEY_CAMERA } },
1080 { KE_KEY, 8, { KEY_MICMUTE } },
1081 { KE_KEY, 11, { KEY_F16 } },
1082 { KE_KEY, 13, { KEY_WLAN } },
1083 { KE_KEY, 16, { KEY_PROG1 } },
1084 { KE_KEY, 17, { KEY_PROG2 } },
1085 { KE_KEY, 64, { KEY_PROG3 } },
1086 { KE_KEY, 65, { KEY_PROG4 } },
1087 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
1088 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
1089 { KE_KEY, 128, { KEY_ESC } },
1093 static int ideapad_input_init(struct ideapad_private *priv)
1095 struct input_dev *inputdev;
1098 inputdev = input_allocate_device();
1102 inputdev->name = "Ideapad extra buttons";
1103 inputdev->phys = "ideapad/input0";
1104 inputdev->id.bustype = BUS_HOST;
1105 inputdev->dev.parent = &priv->platform_device->dev;
1107 err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1109 dev_err(&priv->platform_device->dev,
1110 "Could not set up input device keymap: %d\n", err);
1114 err = input_register_device(inputdev);
1116 dev_err(&priv->platform_device->dev,
1117 "Could not register input device: %d\n", err);
1121 priv->inputdev = inputdev;
1126 input_free_device(inputdev);
1131 static void ideapad_input_exit(struct ideapad_private *priv)
1133 input_unregister_device(priv->inputdev);
1134 priv->inputdev = NULL;
1137 static void ideapad_input_report(struct ideapad_private *priv,
1138 unsigned long scancode)
1140 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1143 static void ideapad_input_novokey(struct ideapad_private *priv)
1145 unsigned long long_pressed;
1147 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1151 ideapad_input_report(priv, 17);
1153 ideapad_input_report(priv, 16);
1156 static void ideapad_check_special_buttons(struct ideapad_private *priv)
1158 unsigned long bit, value;
1160 if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1163 for_each_set_bit (bit, &value, 16) {
1167 /* Thermal Management button */
1168 ideapad_input_report(priv, 65);
1171 /* OneKey Theater button */
1172 ideapad_input_report(priv, 64);
1175 dev_info(&priv->platform_device->dev,
1176 "Unknown special button: %lu\n", bit);
1185 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1187 struct ideapad_private *priv = bl_get_data(blightdev);
1191 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1198 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1200 struct ideapad_private *priv = bl_get_data(blightdev);
1203 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1204 blightdev->props.brightness);
1208 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1209 blightdev->props.power != FB_BLANK_POWERDOWN);
1216 static const struct backlight_ops ideapad_backlight_ops = {
1217 .get_brightness = ideapad_backlight_get_brightness,
1218 .update_status = ideapad_backlight_update_status,
1221 static int ideapad_backlight_init(struct ideapad_private *priv)
1223 struct backlight_device *blightdev;
1224 struct backlight_properties props;
1225 unsigned long max, now, power;
1228 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1232 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1236 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1240 memset(&props, 0, sizeof(props));
1242 props.max_brightness = max;
1243 props.type = BACKLIGHT_PLATFORM;
1245 blightdev = backlight_device_register("ideapad",
1246 &priv->platform_device->dev,
1248 &ideapad_backlight_ops,
1250 if (IS_ERR(blightdev)) {
1251 err = PTR_ERR(blightdev);
1252 dev_err(&priv->platform_device->dev,
1253 "Could not register backlight device: %d\n", err);
1257 priv->blightdev = blightdev;
1258 blightdev->props.brightness = now;
1259 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1261 backlight_update_status(blightdev);
1266 static void ideapad_backlight_exit(struct ideapad_private *priv)
1268 backlight_device_unregister(priv->blightdev);
1269 priv->blightdev = NULL;
1272 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1274 struct backlight_device *blightdev = priv->blightdev;
1275 unsigned long power;
1280 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1283 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1286 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1290 /* if we control brightness via acpi video driver */
1291 if (!priv->blightdev)
1292 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1294 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1298 * keyboard backlight
1300 static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1305 err = eval_hals(priv->adev->handle, &hals);
1309 return !!test_bit(HALS_KBD_BL_STATE_BIT, &hals);
1312 static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1314 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1316 return ideapad_kbd_bl_brightness_get(priv);
1319 static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1321 int err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1326 priv->kbd_bl.last_brightness = brightness;
1331 static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1332 enum led_brightness brightness)
1334 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1336 return ideapad_kbd_bl_brightness_set(priv, brightness);
1339 static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1343 if (!priv->kbd_bl.initialized)
1346 brightness = ideapad_kbd_bl_brightness_get(priv);
1350 if (brightness == priv->kbd_bl.last_brightness)
1353 priv->kbd_bl.last_brightness = brightness;
1355 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1358 static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1360 int brightness, err;
1362 if (!priv->features.kbd_bl)
1365 if (WARN_ON(priv->kbd_bl.initialized))
1368 brightness = ideapad_kbd_bl_brightness_get(priv);
1372 priv->kbd_bl.last_brightness = brightness;
1374 priv->kbd_bl.led.name = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1375 priv->kbd_bl.led.max_brightness = 1;
1376 priv->kbd_bl.led.brightness_get = ideapad_kbd_bl_led_cdev_brightness_get;
1377 priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1378 priv->kbd_bl.led.flags = LED_BRIGHT_HW_CHANGED;
1380 err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1384 priv->kbd_bl.initialized = true;
1389 static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1391 if (!priv->kbd_bl.initialized)
1394 priv->kbd_bl.initialized = false;
1396 led_classdev_unregister(&priv->kbd_bl.led);
1402 static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
1404 unsigned long value;
1406 if (!priv->features.touchpad_ctrl_via_ec)
1409 /* Without reading from EC touchpad LED doesn't switch state */
1410 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
1411 unsigned char param;
1413 * Some IdeaPads don't really turn off touchpad - they only
1414 * switch the LED state. We (de)activate KBC AUX port to turn
1415 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1416 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1418 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1419 ideapad_input_report(priv, value ? 67 : 66);
1420 sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1424 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1426 struct ideapad_private *priv = data;
1427 unsigned long vpc1, vpc2, bit;
1429 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1432 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1435 vpc1 = (vpc2 << 8) | vpc1;
1437 for_each_set_bit (bit, &vpc1, 16) {
1444 ideapad_input_report(priv, bit);
1448 * This event gets send on a Yoga 300-11IBR when the EC
1449 * believes that the device has changed between laptop/
1450 * tent/stand/tablet mode. The EC relies on getting
1451 * angle info from 2 accelerometers through a special
1452 * windows service calling a DSM on the DUAL250E ACPI-
1453 * device. Linux does not do this, making the laptop/
1454 * tent/stand/tablet mode info unreliable, so we simply
1455 * ignore these events.
1459 ideapad_sync_rfk_state(priv);
1462 ideapad_sync_touchpad_state(priv);
1465 ideapad_backlight_notify_brightness(priv);
1468 ideapad_input_novokey(priv);
1471 ideapad_backlight_notify_power(priv);
1475 * Some IdeaPads report event 1 every ~20
1476 * seconds while on battery power; some
1477 * report this when changing to/from tablet
1478 * mode; some report this when the keyboard
1479 * backlight has changed.
1481 ideapad_kbd_bl_notify(priv);
1484 ideapad_check_special_buttons(priv);
1487 dev_info(&priv->platform_device->dev,
1488 "Unknown event: %lu\n", bit);
1493 #if IS_ENABLED(CONFIG_ACPI_WMI)
1494 static void ideapad_wmi_notify(u32 value, void *context)
1496 struct ideapad_private *priv = context;
1497 unsigned long result;
1501 ideapad_input_report(priv, value);
1504 if (!eval_hals(priv->adev->handle, &result)) {
1505 bool state = test_bit(HALS_FNLOCK_STATE_BIT, &result);
1507 exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
1511 dev_info(&priv->platform_device->dev,
1512 "Unknown WMI event: %u\n", value);
1518 * Some ideapads have a hardware rfkill switch, but most do not have one.
1519 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1520 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1521 * There used to be a long list of DMI ids for models without a hw rfkill
1522 * switch here, but that resulted in playing whack a mole.
1523 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1524 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1525 * hw-blocked results in an empty SSID list, which is a much more benign
1527 * So the default now is the much safer option of assuming there is no
1528 * hardware rfkill switch. This default also actually matches most hardware,
1529 * since having a hw rfkill switch is quite rare on modern hardware, so this
1530 * also leads to a much shorter list.
1532 static const struct dmi_system_id hw_rfkill_list[] = {
1536 static void ideapad_check_features(struct ideapad_private *priv)
1538 acpi_handle handle = priv->adev->handle;
1541 priv->features.hw_rfkill_switch = dmi_check_system(hw_rfkill_list);
1543 /* Most ideapads with ELAN0634 touchpad don't use EC touchpad switch */
1544 priv->features.touchpad_ctrl_via_ec = !acpi_dev_present("ELAN0634", NULL, -1);
1546 if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
1547 priv->features.fan_mode = true;
1549 if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
1550 priv->features.conservation_mode = true;
1552 if (acpi_has_method(handle, "DYTC"))
1553 priv->features.dytc = true;
1555 if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
1556 if (!eval_hals(handle, &val)) {
1557 if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
1558 priv->features.fn_lock = true;
1560 if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val))
1561 priv->features.kbd_bl = true;
1563 if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
1564 priv->features.usb_charging = true;
1569 static int ideapad_acpi_add(struct platform_device *pdev)
1571 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
1572 struct ideapad_private *priv;
1577 if (!adev || eval_int(adev->handle, "_CFG", &cfg))
1580 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1584 dev_set_drvdata(&pdev->dev, priv);
1588 priv->platform_device = pdev;
1590 ideapad_check_features(priv);
1592 err = ideapad_sysfs_init(priv);
1596 ideapad_debugfs_init(priv);
1598 err = ideapad_input_init(priv);
1602 err = ideapad_kbd_bl_init(priv);
1605 dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
1607 dev_info(&pdev->dev, "Keyboard backlight control not available\n");
1611 * On some models without a hw-switch (the yoga 2 13 at least)
1612 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1614 if (!priv->features.hw_rfkill_switch)
1615 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1617 /* The same for Touchpad */
1618 if (!priv->features.touchpad_ctrl_via_ec)
1619 write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, 1);
1621 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1622 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1623 ideapad_register_rfkill(priv, i);
1625 ideapad_sync_rfk_state(priv);
1626 ideapad_sync_touchpad_state(priv);
1628 err = ideapad_dytc_profile_init(priv);
1631 dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
1633 dev_info(&pdev->dev, "DYTC interface is not available\n");
1636 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1637 err = ideapad_backlight_init(priv);
1638 if (err && err != -ENODEV)
1639 goto backlight_failed;
1642 status = acpi_install_notify_handler(adev->handle,
1644 ideapad_acpi_notify, priv);
1645 if (ACPI_FAILURE(status)) {
1647 goto notification_failed;
1650 #if IS_ENABLED(CONFIG_ACPI_WMI)
1651 for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
1652 status = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
1653 ideapad_wmi_notify, priv);
1654 if (ACPI_SUCCESS(status)) {
1655 priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
1660 if (ACPI_FAILURE(status) && status != AE_NOT_EXIST) {
1662 goto notification_failed_wmi;
1668 #if IS_ENABLED(CONFIG_ACPI_WMI)
1669 notification_failed_wmi:
1670 acpi_remove_notify_handler(priv->adev->handle,
1672 ideapad_acpi_notify);
1675 notification_failed:
1676 ideapad_backlight_exit(priv);
1679 ideapad_dytc_profile_exit(priv);
1681 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1682 ideapad_unregister_rfkill(priv, i);
1684 ideapad_kbd_bl_exit(priv);
1685 ideapad_input_exit(priv);
1688 ideapad_debugfs_exit(priv);
1689 ideapad_sysfs_exit(priv);
1694 static int ideapad_acpi_remove(struct platform_device *pdev)
1696 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1699 #if IS_ENABLED(CONFIG_ACPI_WMI)
1700 if (priv->fnesc_guid)
1701 wmi_remove_notify_handler(priv->fnesc_guid);
1704 acpi_remove_notify_handler(priv->adev->handle,
1706 ideapad_acpi_notify);
1708 ideapad_backlight_exit(priv);
1709 ideapad_dytc_profile_exit(priv);
1711 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1712 ideapad_unregister_rfkill(priv, i);
1714 ideapad_kbd_bl_exit(priv);
1715 ideapad_input_exit(priv);
1716 ideapad_debugfs_exit(priv);
1717 ideapad_sysfs_exit(priv);
1722 #ifdef CONFIG_PM_SLEEP
1723 static int ideapad_acpi_resume(struct device *dev)
1725 struct ideapad_private *priv = dev_get_drvdata(dev);
1727 ideapad_sync_rfk_state(priv);
1728 ideapad_sync_touchpad_state(priv);
1731 dytc_profile_refresh(priv);
1736 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1738 static const struct acpi_device_id ideapad_device_ids[] = {
1742 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1744 static struct platform_driver ideapad_acpi_driver = {
1745 .probe = ideapad_acpi_add,
1746 .remove = ideapad_acpi_remove,
1748 .name = "ideapad_acpi",
1750 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1754 module_platform_driver(ideapad_acpi_driver);
1757 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1758 MODULE_LICENSE("GPL");