1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
5 * Copyright © 2010 Intel Corporation
6 * Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/acpi.h>
12 #include <linux/backlight.h>
13 #include <linux/bitfield.h>
14 #include <linux/bitops.h>
15 #include <linux/bug.h>
16 #include <linux/debugfs.h>
17 #include <linux/device.h>
18 #include <linux/dmi.h>
20 #include <linux/i8042.h>
21 #include <linux/init.h>
22 #include <linux/input.h>
23 #include <linux/input/sparse-keymap.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>
33 #include <linux/wmi.h>
34 #include "ideapad-laptop.h"
36 #include <acpi/video.h>
38 #include <dt-bindings/leds/common.h>
40 #define IDEAPAD_RFKILL_DEV_NUM 3
45 CFG_CAP_WIFI_BIT = 18,
49 * These are OnScreenDisplay support bits that can be useful to determine
50 * whether a hotkey exists/should show OSD. But they aren't particularly
51 * meaningful since they were introduced later, i.e. 2010 IdeaPads
52 * don't have these, but they still have had OSD for hotkeys.
54 CFG_OSD_NUMLK_BIT = 27,
55 CFG_OSD_CAPSLK_BIT = 28,
56 CFG_OSD_MICMUTE_BIT = 29,
57 CFG_OSD_TOUCHPAD_BIT = 30,
62 GBMD_CONSERVATION_STATE_BIT = 5,
66 SBMC_CONSERVATION_ON = 3,
67 SBMC_CONSERVATION_OFF = 5,
71 HALS_KBD_BL_SUPPORT_BIT = 4,
72 HALS_KBD_BL_STATE_BIT = 5,
73 HALS_USB_CHARGING_SUPPORT_BIT = 6,
74 HALS_USB_CHARGING_STATE_BIT = 7,
75 HALS_FNLOCK_SUPPORT_BIT = 9,
76 HALS_FNLOCK_STATE_BIT = 10,
77 HALS_HOTKEYS_PRIMARY_BIT = 11,
82 SALS_KBD_BL_OFF = 0x9,
83 SALS_USB_CHARGING_ON = 0xa,
84 SALS_USB_CHARGING_OFF = 0xb,
86 SALS_FNLOCK_OFF = 0xf,
90 * These correspond to the number of supported states - 1
91 * Future keyboard types may need a new system, if there's a collision
92 * KBD_BL_TRISTATE_AUTO has no way to report or set the auto state
93 * so it effectively has 3 states, but needs to handle 4
98 KBD_BL_TRISTATE_AUTO = 3,
101 #define KBD_BL_QUERY_TYPE 0x1
102 #define KBD_BL_TRISTATE_TYPE 0x5
103 #define KBD_BL_TRISTATE_AUTO_TYPE 0x7
105 #define KBD_BL_COMMAND_GET 0x2
106 #define KBD_BL_COMMAND_SET 0x3
107 #define KBD_BL_COMMAND_TYPE GENMASK(7, 4)
109 #define KBD_BL_GET_BRIGHTNESS GENMASK(15, 1)
110 #define KBD_BL_SET_BRIGHTNESS GENMASK(19, 16)
112 #define KBD_BL_KBLC_CHANGED_EVENT 12
114 struct ideapad_dytc_priv {
115 enum platform_profile_option current_profile;
116 struct platform_profile_handler pprof;
117 struct mutex mutex; /* protects the DYTC interface */
118 struct ideapad_private *priv;
121 struct ideapad_rfk_priv {
123 struct ideapad_private *priv;
126 struct ideapad_private {
127 struct acpi_device *adev;
128 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
129 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
130 struct platform_device *platform_device;
131 struct input_dev *inputdev;
132 struct backlight_device *blightdev;
133 struct ideapad_dytc_priv *dytc;
134 struct dentry *debug;
136 unsigned long r_touchpad_val;
138 bool conservation_mode : 1;
142 bool set_fn_lock_led : 1;
143 bool hw_rfkill_switch : 1;
145 bool touchpad_ctrl_via_ec : 1;
146 bool ctrl_ps2_aux_port : 1;
147 bool usb_charging : 1;
152 struct led_classdev led;
153 unsigned int last_brightness;
157 struct led_classdev led;
158 unsigned int last_brightness;
162 static bool no_bt_rfkill;
163 module_param(no_bt_rfkill, bool, 0444);
164 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
166 static bool allow_v4_dytc;
167 module_param(allow_v4_dytc, bool, 0444);
168 MODULE_PARM_DESC(allow_v4_dytc,
169 "Enable DYTC version 4 platform-profile support. "
170 "If you need this please report this to: platform-driver-x86@vger.kernel.org");
172 static bool hw_rfkill_switch;
173 module_param(hw_rfkill_switch, bool, 0444);
174 MODULE_PARM_DESC(hw_rfkill_switch,
175 "Enable rfkill support for laptops with a hw on/off wifi switch/slider. "
176 "If you need this please report this to: platform-driver-x86@vger.kernel.org");
178 static bool set_fn_lock_led;
179 module_param(set_fn_lock_led, bool, 0444);
180 MODULE_PARM_DESC(set_fn_lock_led,
181 "Enable driver based updates of the fn-lock LED on fn-lock changes. "
182 "If you need this please report this to: platform-driver-x86@vger.kernel.org");
184 static bool ctrl_ps2_aux_port;
185 module_param(ctrl_ps2_aux_port, bool, 0444);
186 MODULE_PARM_DESC(ctrl_ps2_aux_port,
187 "Enable driver based PS/2 aux port en-/dis-abling on touchpad on/off toggle. "
188 "If you need this please report this to: platform-driver-x86@vger.kernel.org");
190 static bool touchpad_ctrl_via_ec;
191 module_param(touchpad_ctrl_via_ec, bool, 0444);
192 MODULE_PARM_DESC(touchpad_ctrl_via_ec,
193 "Enable registering a 'touchpad' sysfs-attribute which can be used to manually "
194 "tell the EC to enable/disable the touchpad. This may not work on all models.");
200 static struct ideapad_private *ideapad_shared;
201 static DEFINE_MUTEX(ideapad_shared_mutex);
203 static int ideapad_shared_init(struct ideapad_private *priv)
207 mutex_lock(&ideapad_shared_mutex);
209 if (!ideapad_shared) {
210 ideapad_shared = priv;
213 dev_warn(&priv->adev->dev, "found multiple platform devices\n");
217 mutex_unlock(&ideapad_shared_mutex);
222 static void ideapad_shared_exit(struct ideapad_private *priv)
224 mutex_lock(&ideapad_shared_mutex);
226 if (ideapad_shared == priv)
227 ideapad_shared = NULL;
229 mutex_unlock(&ideapad_shared_mutex);
236 static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
238 unsigned long long result;
241 status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
242 if (ACPI_FAILURE(status))
250 static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
252 acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
254 return ACPI_FAILURE(status) ? -EIO : 0;
257 static int eval_gbmd(acpi_handle handle, unsigned long *res)
259 return eval_int(handle, "GBMD", res);
262 static int exec_sbmc(acpi_handle handle, unsigned long arg)
264 return exec_simple_method(handle, "SBMC", arg);
267 static int eval_hals(acpi_handle handle, unsigned long *res)
269 return eval_int(handle, "HALS", res);
272 static int exec_sals(acpi_handle handle, unsigned long arg)
274 return exec_simple_method(handle, "SALS", arg);
277 static int exec_kblc(acpi_handle handle, unsigned long arg)
279 return exec_simple_method(handle, "KBLC", arg);
282 static int eval_kblc(acpi_handle handle, unsigned long cmd, unsigned long *res)
284 return eval_int_with_arg(handle, "KBLC", cmd, res);
287 static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
289 return eval_int_with_arg(handle, "DYTC", cmd, res);
295 static int debugfs_status_show(struct seq_file *s, void *data)
297 struct ideapad_private *priv = s->private;
300 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
301 seq_printf(s, "Backlight max: %lu\n", value);
302 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
303 seq_printf(s, "Backlight now: %lu\n", value);
304 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
305 seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
307 seq_puts(s, "=====================\n");
309 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
310 seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
311 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
312 seq_printf(s, "Wifi status: %s (%lu)\n", value ? "on" : "off", value);
313 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
314 seq_printf(s, "BT status: %s (%lu)\n", value ? "on" : "off", value);
315 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
316 seq_printf(s, "3G status: %s (%lu)\n", value ? "on" : "off", value);
318 seq_puts(s, "=====================\n");
320 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
321 seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
322 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
323 seq_printf(s, "Camera status: %s (%lu)\n", value ? "on" : "off", value);
325 seq_puts(s, "=====================\n");
327 if (!eval_gbmd(priv->adev->handle, &value))
328 seq_printf(s, "GBMD: %#010lx\n", value);
329 if (!eval_hals(priv->adev->handle, &value))
330 seq_printf(s, "HALS: %#010lx\n", value);
334 DEFINE_SHOW_ATTRIBUTE(debugfs_status);
336 static int debugfs_cfg_show(struct seq_file *s, void *data)
338 struct ideapad_private *priv = s->private;
340 seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
342 seq_puts(s, "Capabilities:");
343 if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
344 seq_puts(s, " bluetooth");
345 if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
347 if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
348 seq_puts(s, " wifi");
349 if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
350 seq_puts(s, " camera");
353 seq_puts(s, "OSD support:");
354 if (test_bit(CFG_OSD_NUMLK_BIT, &priv->cfg))
355 seq_puts(s, " num-lock");
356 if (test_bit(CFG_OSD_CAPSLK_BIT, &priv->cfg))
357 seq_puts(s, " caps-lock");
358 if (test_bit(CFG_OSD_MICMUTE_BIT, &priv->cfg))
359 seq_puts(s, " mic-mute");
360 if (test_bit(CFG_OSD_TOUCHPAD_BIT, &priv->cfg))
361 seq_puts(s, " touchpad");
362 if (test_bit(CFG_OSD_CAM_BIT, &priv->cfg))
363 seq_puts(s, " camera");
366 seq_puts(s, "Graphics: ");
367 switch (priv->cfg & 0x700) {
369 seq_puts(s, "Intel");
375 seq_puts(s, "Nvidia");
378 seq_puts(s, "Intel and ATI");
381 seq_puts(s, "Intel and Nvidia");
388 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
390 static void ideapad_debugfs_init(struct ideapad_private *priv)
394 dir = debugfs_create_dir("ideapad", NULL);
397 debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
398 debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
401 static void ideapad_debugfs_exit(struct ideapad_private *priv)
403 debugfs_remove_recursive(priv->debug);
410 static ssize_t camera_power_show(struct device *dev,
411 struct device_attribute *attr,
414 struct ideapad_private *priv = dev_get_drvdata(dev);
415 unsigned long result;
418 err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
422 return sysfs_emit(buf, "%d\n", !!result);
425 static ssize_t camera_power_store(struct device *dev,
426 struct device_attribute *attr,
427 const char *buf, size_t count)
429 struct ideapad_private *priv = dev_get_drvdata(dev);
433 err = kstrtobool(buf, &state);
437 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
444 static DEVICE_ATTR_RW(camera_power);
446 static ssize_t conservation_mode_show(struct device *dev,
447 struct device_attribute *attr,
450 struct ideapad_private *priv = dev_get_drvdata(dev);
451 unsigned long result;
454 err = eval_gbmd(priv->adev->handle, &result);
458 return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
461 static ssize_t conservation_mode_store(struct device *dev,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
465 struct ideapad_private *priv = dev_get_drvdata(dev);
469 err = kstrtobool(buf, &state);
473 err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
480 static DEVICE_ATTR_RW(conservation_mode);
482 static ssize_t fan_mode_show(struct device *dev,
483 struct device_attribute *attr,
486 struct ideapad_private *priv = dev_get_drvdata(dev);
487 unsigned long result;
490 err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
494 return sysfs_emit(buf, "%lu\n", result);
497 static ssize_t fan_mode_store(struct device *dev,
498 struct device_attribute *attr,
499 const char *buf, size_t count)
501 struct ideapad_private *priv = dev_get_drvdata(dev);
505 err = kstrtouint(buf, 0, &state);
509 if (state > 4 || state == 3)
512 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
519 static DEVICE_ATTR_RW(fan_mode);
521 static int ideapad_fn_lock_get(struct ideapad_private *priv)
526 err = eval_hals(priv->adev->handle, &hals);
530 return !!test_bit(HALS_FNLOCK_STATE_BIT, &hals);
533 static int ideapad_fn_lock_set(struct ideapad_private *priv, bool state)
535 return exec_sals(priv->adev->handle,
536 state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
539 static void ideapad_fn_lock_led_notify(struct ideapad_private *priv, int brightness)
541 if (!priv->fn_lock.initialized)
544 if (brightness == priv->fn_lock.last_brightness)
547 priv->fn_lock.last_brightness = brightness;
549 led_classdev_notify_brightness_hw_changed(&priv->fn_lock.led, brightness);
552 static ssize_t fn_lock_show(struct device *dev,
553 struct device_attribute *attr,
556 struct ideapad_private *priv = dev_get_drvdata(dev);
559 brightness = ideapad_fn_lock_get(priv);
563 return sysfs_emit(buf, "%d\n", brightness);
566 static ssize_t fn_lock_store(struct device *dev,
567 struct device_attribute *attr,
568 const char *buf, size_t count)
570 struct ideapad_private *priv = dev_get_drvdata(dev);
574 err = kstrtobool(buf, &state);
578 err = ideapad_fn_lock_set(priv, state);
582 ideapad_fn_lock_led_notify(priv, state);
587 static DEVICE_ATTR_RW(fn_lock);
589 static ssize_t touchpad_show(struct device *dev,
590 struct device_attribute *attr,
593 struct ideapad_private *priv = dev_get_drvdata(dev);
594 unsigned long result;
597 err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
601 priv->r_touchpad_val = result;
603 return sysfs_emit(buf, "%d\n", !!result);
606 static ssize_t touchpad_store(struct device *dev,
607 struct device_attribute *attr,
608 const char *buf, size_t count)
610 struct ideapad_private *priv = dev_get_drvdata(dev);
614 err = kstrtobool(buf, &state);
618 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
622 priv->r_touchpad_val = state;
627 static DEVICE_ATTR_RW(touchpad);
629 static ssize_t usb_charging_show(struct device *dev,
630 struct device_attribute *attr,
633 struct ideapad_private *priv = dev_get_drvdata(dev);
637 err = eval_hals(priv->adev->handle, &hals);
641 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
644 static ssize_t usb_charging_store(struct device *dev,
645 struct device_attribute *attr,
646 const char *buf, size_t count)
648 struct ideapad_private *priv = dev_get_drvdata(dev);
652 err = kstrtobool(buf, &state);
656 err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
663 static DEVICE_ATTR_RW(usb_charging);
665 static struct attribute *ideapad_attributes[] = {
666 &dev_attr_camera_power.attr,
667 &dev_attr_conservation_mode.attr,
668 &dev_attr_fan_mode.attr,
669 &dev_attr_fn_lock.attr,
670 &dev_attr_touchpad.attr,
671 &dev_attr_usb_charging.attr,
675 static umode_t ideapad_is_visible(struct kobject *kobj,
676 struct attribute *attr,
679 struct device *dev = kobj_to_dev(kobj);
680 struct ideapad_private *priv = dev_get_drvdata(dev);
681 bool supported = true;
683 if (attr == &dev_attr_camera_power.attr)
684 supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
685 else if (attr == &dev_attr_conservation_mode.attr)
686 supported = priv->features.conservation_mode;
687 else if (attr == &dev_attr_fan_mode.attr)
688 supported = priv->features.fan_mode;
689 else if (attr == &dev_attr_fn_lock.attr)
690 supported = priv->features.fn_lock;
691 else if (attr == &dev_attr_touchpad.attr)
692 supported = priv->features.touchpad_ctrl_via_ec;
693 else if (attr == &dev_attr_usb_charging.attr)
694 supported = priv->features.usb_charging;
696 return supported ? attr->mode : 0;
699 static const struct attribute_group ideapad_attribute_group = {
700 .is_visible = ideapad_is_visible,
701 .attrs = ideapad_attributes
705 * DYTC Platform profile
707 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
708 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
709 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
710 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
712 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
713 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
714 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
716 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
717 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
719 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
720 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
721 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
723 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
724 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
725 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
727 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
728 #define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
729 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
731 #define DYTC_SET_COMMAND(function, mode, on) \
732 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
733 (mode) << DYTC_SET_MODE_BIT | \
734 (on) << DYTC_SET_VALID_BIT)
736 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
738 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
740 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
743 case DYTC_MODE_LOW_POWER:
744 *profile = PLATFORM_PROFILE_LOW_POWER;
746 case DYTC_MODE_BALANCE:
747 *profile = PLATFORM_PROFILE_BALANCED;
749 case DYTC_MODE_PERFORM:
750 *profile = PLATFORM_PROFILE_PERFORMANCE;
752 default: /* Unknown mode */
759 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
762 case PLATFORM_PROFILE_LOW_POWER:
763 *perfmode = DYTC_MODE_LOW_POWER;
765 case PLATFORM_PROFILE_BALANCED:
766 *perfmode = DYTC_MODE_BALANCE;
768 case PLATFORM_PROFILE_PERFORMANCE:
769 *perfmode = DYTC_MODE_PERFORM;
771 default: /* Unknown profile */
779 * dytc_profile_get: Function to register with platform_profile
780 * handler. Returns current platform profile.
782 static int dytc_profile_get(struct platform_profile_handler *pprof,
783 enum platform_profile_option *profile)
785 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
787 *profile = dytc->current_profile;
792 * Helper function - check if we are in CQL mode and if we are
796 * If not in CQL mode, just run the command
798 static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
799 unsigned long *output)
801 int err, cmd_err, cur_funcmode;
803 /* Determine if we are in CQL mode. This alters the commands we do */
804 err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
808 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
809 /* Check if we're OK to return immediately */
810 if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
813 if (cur_funcmode == DYTC_FUNCTION_CQL) {
814 err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
819 cmd_err = eval_dytc(priv->adev->handle, cmd, output);
820 /* Check return condition after we've restored CQL state */
822 if (cur_funcmode == DYTC_FUNCTION_CQL) {
823 err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
832 * dytc_profile_set: Function to register with platform_profile
833 * handler. Sets current platform profile.
835 static int dytc_profile_set(struct platform_profile_handler *pprof,
836 enum platform_profile_option profile)
838 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
839 struct ideapad_private *priv = dytc->priv;
840 unsigned long output;
843 err = mutex_lock_interruptible(&dytc->mutex);
847 if (profile == PLATFORM_PROFILE_BALANCED) {
848 /* To get back to balanced mode we just issue a reset command */
849 err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
855 err = convert_profile_to_dytc(profile, &perfmode);
859 /* Determine if we are in CQL mode. This alters the commands we do */
860 err = dytc_cql_command(priv, DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
866 /* Success - update current profile */
867 dytc->current_profile = profile;
870 mutex_unlock(&dytc->mutex);
875 static void dytc_profile_refresh(struct ideapad_private *priv)
877 enum platform_profile_option profile;
878 unsigned long output;
881 mutex_lock(&priv->dytc->mutex);
882 err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
883 mutex_unlock(&priv->dytc->mutex);
887 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
889 if (convert_dytc_to_profile(perfmode, &profile))
892 if (profile != priv->dytc->current_profile) {
893 priv->dytc->current_profile = profile;
894 platform_profile_notify();
898 static const struct dmi_system_id ideapad_dytc_v4_allow_table[] = {
900 /* Ideapad 5 Pro 16ACH6 */
902 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
903 DMI_MATCH(DMI_PRODUCT_NAME, "82L5")
907 /* Ideapad 5 15ITL05 */
909 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
910 DMI_MATCH(DMI_PRODUCT_VERSION, "IdeaPad 5 15ITL05")
916 static int ideapad_dytc_profile_init(struct ideapad_private *priv)
918 int err, dytc_version;
919 unsigned long output;
921 if (!priv->features.dytc)
924 err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
925 /* For all other errors we can flag the failure */
929 /* Check DYTC is enabled and supports mode setting */
930 if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output)) {
931 dev_info(&priv->platform_device->dev, "DYTC_QUERY_ENABLE_BIT returned false\n");
935 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
937 if (dytc_version < 4) {
938 dev_info(&priv->platform_device->dev, "DYTC_VERSION < 4 is not supported\n");
942 if (dytc_version < 5 &&
943 !(allow_v4_dytc || dmi_check_system(ideapad_dytc_v4_allow_table))) {
944 dev_info(&priv->platform_device->dev,
945 "DYTC_VERSION 4 support may not work. Pass ideapad_laptop.allow_v4_dytc=Y on the kernel commandline to enable\n");
949 priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
953 mutex_init(&priv->dytc->mutex);
955 priv->dytc->priv = priv;
956 priv->dytc->pprof.profile_get = dytc_profile_get;
957 priv->dytc->pprof.profile_set = dytc_profile_set;
959 /* Setup supported modes */
960 set_bit(PLATFORM_PROFILE_LOW_POWER, priv->dytc->pprof.choices);
961 set_bit(PLATFORM_PROFILE_BALANCED, priv->dytc->pprof.choices);
962 set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->dytc->pprof.choices);
964 /* Create platform_profile structure and register */
965 err = platform_profile_register(&priv->dytc->pprof);
969 /* Ensure initial values are correct */
970 dytc_profile_refresh(priv);
975 mutex_destroy(&priv->dytc->mutex);
982 static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
987 platform_profile_remove();
988 mutex_destroy(&priv->dytc->mutex);
997 struct ideapad_rfk_data {
1004 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
1005 { "ideapad_wlan", CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
1006 { "ideapad_bluetooth", CFG_CAP_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
1007 { "ideapad_3g", CFG_CAP_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
1010 static int ideapad_rfk_set(void *data, bool blocked)
1012 struct ideapad_rfk_priv *priv = data;
1013 int opcode = ideapad_rfk_data[priv->dev].opcode;
1015 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
1018 static const struct rfkill_ops ideapad_rfk_ops = {
1019 .set_block = ideapad_rfk_set,
1022 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
1024 unsigned long hw_blocked = 0;
1027 if (priv->features.hw_rfkill_switch) {
1028 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
1030 hw_blocked = !hw_blocked;
1033 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1035 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
1038 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
1040 unsigned long rf_enabled;
1043 if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
1044 /* Force to enable bluetooth when no_bt_rfkill=1 */
1045 write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
1049 priv->rfk_priv[dev].dev = dev;
1050 priv->rfk_priv[dev].priv = priv;
1052 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
1053 &priv->platform_device->dev,
1054 ideapad_rfk_data[dev].type,
1056 &priv->rfk_priv[dev]);
1057 if (!priv->rfk[dev])
1060 err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
1064 rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
1066 err = rfkill_register(priv->rfk[dev]);
1068 rfkill_destroy(priv->rfk[dev]);
1073 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1075 if (!priv->rfk[dev])
1078 rfkill_unregister(priv->rfk[dev]);
1079 rfkill_destroy(priv->rfk[dev]);
1085 static int ideapad_sysfs_init(struct ideapad_private *priv)
1087 return device_add_group(&priv->platform_device->dev,
1088 &ideapad_attribute_group);
1091 static void ideapad_sysfs_exit(struct ideapad_private *priv)
1093 device_remove_group(&priv->platform_device->dev,
1094 &ideapad_attribute_group);
1100 #define IDEAPAD_WMI_KEY 0x100
1102 static const struct key_entry ideapad_keymap[] = {
1103 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
1104 { KE_KEY, 7, { KEY_CAMERA } },
1105 { KE_KEY, 8, { KEY_MICMUTE } },
1106 { KE_KEY, 11, { KEY_F16 } },
1107 { KE_KEY, 13, { KEY_WLAN } },
1108 { KE_KEY, 16, { KEY_PROG1 } },
1109 { KE_KEY, 17, { KEY_PROG2 } },
1110 { KE_KEY, 64, { KEY_PROG3 } },
1111 { KE_KEY, 65, { KEY_PROG4 } },
1112 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
1113 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
1114 { KE_KEY, 128, { KEY_ESC } },
1120 /* FnLock (handled by the firmware) */
1121 { KE_IGNORE, 0x02 | IDEAPAD_WMI_KEY },
1122 /* Esc (handled by the firmware) */
1123 { KE_IGNORE, 0x03 | IDEAPAD_WMI_KEY },
1124 /* Customizable Lenovo Hotkey ("star" with 'S' inside) */
1125 { KE_KEY, 0x01 | IDEAPAD_WMI_KEY, { KEY_FAVORITES } },
1126 { KE_KEY, 0x04 | IDEAPAD_WMI_KEY, { KEY_SELECTIVE_SCREENSHOT } },
1127 /* Lenovo Support */
1128 { KE_KEY, 0x07 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1129 { KE_KEY, 0x0e | IDEAPAD_WMI_KEY, { KEY_PICKUP_PHONE } },
1130 { KE_KEY, 0x0f | IDEAPAD_WMI_KEY, { KEY_HANGUP_PHONE } },
1131 /* Refresh Rate Toggle (Fn+R) */
1132 { KE_KEY, 0x10 | IDEAPAD_WMI_KEY, { KEY_REFRESH_RATE_TOGGLE } },
1133 /* Dark mode toggle */
1134 { KE_KEY, 0x13 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
1135 /* Sound profile switch */
1136 { KE_KEY, 0x12 | IDEAPAD_WMI_KEY, { KEY_PROG2 } },
1137 /* Lenovo Virtual Background application */
1138 { KE_KEY, 0x28 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
1139 /* Lenovo Support */
1140 { KE_KEY, 0x27 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1141 /* Refresh Rate Toggle */
1142 { KE_KEY, 0x0a | IDEAPAD_WMI_KEY, { KEY_REFRESH_RATE_TOGGLE } },
1147 static int ideapad_input_init(struct ideapad_private *priv)
1149 struct input_dev *inputdev;
1152 inputdev = input_allocate_device();
1156 inputdev->name = "Ideapad extra buttons";
1157 inputdev->phys = "ideapad/input0";
1158 inputdev->id.bustype = BUS_HOST;
1159 inputdev->dev.parent = &priv->platform_device->dev;
1161 err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1163 dev_err(&priv->platform_device->dev,
1164 "Could not set up input device keymap: %d\n", err);
1168 err = input_register_device(inputdev);
1170 dev_err(&priv->platform_device->dev,
1171 "Could not register input device: %d\n", err);
1175 priv->inputdev = inputdev;
1180 input_free_device(inputdev);
1185 static void ideapad_input_exit(struct ideapad_private *priv)
1187 input_unregister_device(priv->inputdev);
1188 priv->inputdev = NULL;
1191 static void ideapad_input_report(struct ideapad_private *priv,
1192 unsigned long scancode)
1194 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1197 static void ideapad_input_novokey(struct ideapad_private *priv)
1199 unsigned long long_pressed;
1201 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1205 ideapad_input_report(priv, 17);
1207 ideapad_input_report(priv, 16);
1210 static void ideapad_check_special_buttons(struct ideapad_private *priv)
1212 unsigned long bit, value;
1214 if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1217 for_each_set_bit (bit, &value, 16) {
1221 /* Thermal Management / Performance Mode button */
1223 platform_profile_cycle();
1225 ideapad_input_report(priv, 65);
1228 /* OneKey Theater button */
1229 ideapad_input_report(priv, 64);
1232 dev_info(&priv->platform_device->dev,
1233 "Unknown special button: %lu\n", bit);
1242 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1244 struct ideapad_private *priv = bl_get_data(blightdev);
1248 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1255 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1257 struct ideapad_private *priv = bl_get_data(blightdev);
1260 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1261 blightdev->props.brightness);
1265 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1266 blightdev->props.power != FB_BLANK_POWERDOWN);
1273 static const struct backlight_ops ideapad_backlight_ops = {
1274 .get_brightness = ideapad_backlight_get_brightness,
1275 .update_status = ideapad_backlight_update_status,
1278 static int ideapad_backlight_init(struct ideapad_private *priv)
1280 struct backlight_device *blightdev;
1281 struct backlight_properties props;
1282 unsigned long max, now, power;
1285 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1289 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1293 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1297 memset(&props, 0, sizeof(props));
1299 props.max_brightness = max;
1300 props.type = BACKLIGHT_PLATFORM;
1302 blightdev = backlight_device_register("ideapad",
1303 &priv->platform_device->dev,
1305 &ideapad_backlight_ops,
1307 if (IS_ERR(blightdev)) {
1308 err = PTR_ERR(blightdev);
1309 dev_err(&priv->platform_device->dev,
1310 "Could not register backlight device: %d\n", err);
1314 priv->blightdev = blightdev;
1315 blightdev->props.brightness = now;
1316 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1318 backlight_update_status(blightdev);
1323 static void ideapad_backlight_exit(struct ideapad_private *priv)
1325 backlight_device_unregister(priv->blightdev);
1326 priv->blightdev = NULL;
1329 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1331 struct backlight_device *blightdev = priv->blightdev;
1332 unsigned long power;
1337 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1340 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1343 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1347 /* if we control brightness via acpi video driver */
1348 if (!priv->blightdev)
1349 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1351 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1355 * keyboard backlight
1357 static int ideapad_kbd_bl_check_tristate(int type)
1359 return (type == KBD_BL_TRISTATE) || (type == KBD_BL_TRISTATE_AUTO);
1362 static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1364 unsigned long value;
1367 if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
1368 err = eval_kblc(priv->adev->handle,
1369 FIELD_PREP(KBD_BL_COMMAND_TYPE, priv->kbd_bl.type) |
1376 /* Convert returned value to brightness level */
1377 value = FIELD_GET(KBD_BL_GET_BRIGHTNESS, value);
1379 /* Off, low or high */
1380 if (value <= priv->kbd_bl.led.max_brightness)
1383 /* Auto, report as off */
1384 if (value == priv->kbd_bl.led.max_brightness + 1)
1388 dev_warn(&priv->platform_device->dev,
1389 "Unknown keyboard backlight value: %lu", value);
1393 err = eval_hals(priv->adev->handle, &value);
1397 return !!test_bit(HALS_KBD_BL_STATE_BIT, &value);
1400 static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1402 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1404 return ideapad_kbd_bl_brightness_get(priv);
1407 static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1410 unsigned long value;
1411 int type = priv->kbd_bl.type;
1413 if (ideapad_kbd_bl_check_tristate(type)) {
1414 if (brightness > priv->kbd_bl.led.max_brightness)
1417 value = FIELD_PREP(KBD_BL_SET_BRIGHTNESS, brightness) |
1418 FIELD_PREP(KBD_BL_COMMAND_TYPE, type) |
1420 err = exec_kblc(priv->adev->handle, value);
1422 err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1428 priv->kbd_bl.last_brightness = brightness;
1433 static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1434 enum led_brightness brightness)
1436 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1438 return ideapad_kbd_bl_brightness_set(priv, brightness);
1441 static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1445 if (!priv->kbd_bl.initialized)
1448 brightness = ideapad_kbd_bl_brightness_get(priv);
1452 if (brightness == priv->kbd_bl.last_brightness)
1455 priv->kbd_bl.last_brightness = brightness;
1457 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1460 static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1462 int brightness, err;
1464 if (!priv->features.kbd_bl)
1467 if (WARN_ON(priv->kbd_bl.initialized))
1470 if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
1471 priv->kbd_bl.led.max_brightness = 2;
1473 priv->kbd_bl.led.max_brightness = 1;
1476 brightness = ideapad_kbd_bl_brightness_get(priv);
1480 priv->kbd_bl.last_brightness = brightness;
1481 priv->kbd_bl.led.name = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1482 priv->kbd_bl.led.brightness_get = ideapad_kbd_bl_led_cdev_brightness_get;
1483 priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1484 priv->kbd_bl.led.flags = LED_BRIGHT_HW_CHANGED;
1486 err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1490 priv->kbd_bl.initialized = true;
1495 static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1497 if (!priv->kbd_bl.initialized)
1500 priv->kbd_bl.initialized = false;
1502 led_classdev_unregister(&priv->kbd_bl.led);
1508 static enum led_brightness ideapad_fn_lock_led_cdev_get(struct led_classdev *led_cdev)
1510 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, fn_lock.led);
1512 return ideapad_fn_lock_get(priv);
1515 static int ideapad_fn_lock_led_cdev_set(struct led_classdev *led_cdev,
1516 enum led_brightness brightness)
1518 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, fn_lock.led);
1520 return ideapad_fn_lock_set(priv, brightness);
1523 static int ideapad_fn_lock_led_init(struct ideapad_private *priv)
1525 int brightness, err;
1527 if (!priv->features.fn_lock)
1530 if (WARN_ON(priv->fn_lock.initialized))
1533 priv->fn_lock.led.max_brightness = 1;
1535 brightness = ideapad_fn_lock_get(priv);
1539 priv->fn_lock.last_brightness = brightness;
1540 priv->fn_lock.led.name = "platform::" LED_FUNCTION_FNLOCK;
1541 priv->fn_lock.led.brightness_get = ideapad_fn_lock_led_cdev_get;
1542 priv->fn_lock.led.brightness_set_blocking = ideapad_fn_lock_led_cdev_set;
1543 priv->fn_lock.led.flags = LED_BRIGHT_HW_CHANGED;
1545 err = led_classdev_register(&priv->platform_device->dev, &priv->fn_lock.led);
1549 priv->fn_lock.initialized = true;
1554 static void ideapad_fn_lock_led_exit(struct ideapad_private *priv)
1556 if (!priv->fn_lock.initialized)
1559 priv->fn_lock.initialized = false;
1561 led_classdev_unregister(&priv->fn_lock.led);
1567 static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_events)
1569 unsigned long value;
1570 unsigned char param;
1573 /* Without reading from EC touchpad LED doesn't switch state */
1574 ret = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value);
1579 * Some IdeaPads don't really turn off touchpad - they only
1580 * switch the LED state. We (de)activate KBC AUX port to turn
1581 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1582 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1584 if (priv->features.ctrl_ps2_aux_port)
1585 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1588 * On older models the EC controls the touchpad and toggles it on/off
1589 * itself, in this case we report KEY_TOUCHPAD_ON/_OFF. Some models do
1590 * an acpi-notify with VPC bit 5 set on resume, so this function get
1591 * called with send_events=true on every resume. Therefor if the EC did
1592 * not toggle, do nothing to avoid sending spurious KEY_TOUCHPAD_TOGGLE.
1594 if (send_events && value != priv->r_touchpad_val) {
1595 ideapad_input_report(priv, value ? 67 : 66);
1596 sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1599 priv->r_touchpad_val = value;
1602 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1604 struct ideapad_private *priv = data;
1605 unsigned long vpc1, vpc2, bit;
1607 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1610 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1613 vpc1 = (vpc2 << 8) | vpc1;
1615 for_each_set_bit (bit, &vpc1, 16) {
1622 ideapad_input_report(priv, bit);
1626 * This event gets send on a Yoga 300-11IBR when the EC
1627 * believes that the device has changed between laptop/
1628 * tent/stand/tablet mode. The EC relies on getting
1629 * angle info from 2 accelerometers through a special
1630 * windows service calling a DSM on the DUAL250E ACPI-
1631 * device. Linux does not do this, making the laptop/
1632 * tent/stand/tablet mode info unreliable, so we simply
1633 * ignore these events.
1637 ideapad_sync_rfk_state(priv);
1640 ideapad_sync_touchpad_state(priv, true);
1643 ideapad_backlight_notify_brightness(priv);
1646 ideapad_input_novokey(priv);
1649 ideapad_backlight_notify_power(priv);
1651 case KBD_BL_KBLC_CHANGED_EVENT:
1654 * Some IdeaPads report event 1 every ~20
1655 * seconds while on battery power; some
1656 * report this when changing to/from tablet
1657 * mode; some report this when the keyboard
1658 * backlight has changed.
1660 ideapad_kbd_bl_notify(priv);
1663 ideapad_check_special_buttons(priv);
1666 dev_info(&priv->platform_device->dev,
1667 "Unknown event: %lu\n", bit);
1672 /* On some models we need to call exec_sals(SALS_FNLOCK_ON/OFF) to set the LED */
1673 static const struct dmi_system_id set_fn_lock_led_list[] = {
1675 /* https://bugzilla.kernel.org/show_bug.cgi?id=212671 */
1677 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1678 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion R7000P2020H"),
1683 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1684 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion 5 15ARH05"),
1691 * Some ideapads have a hardware rfkill switch, but most do not have one.
1692 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1693 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1694 * There used to be a long list of DMI ids for models without a hw rfkill
1695 * switch here, but that resulted in playing whack a mole.
1696 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1697 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1698 * hw-blocked results in an empty SSID list, which is a much more benign
1700 * So the default now is the much safer option of assuming there is no
1701 * hardware rfkill switch. This default also actually matches most hardware,
1702 * since having a hw rfkill switch is quite rare on modern hardware, so this
1703 * also leads to a much shorter list.
1705 static const struct dmi_system_id hw_rfkill_list[] = {
1710 * On some models the EC toggles the touchpad muted LED on touchpad toggle
1711 * hotkey presses, but the EC does not actually disable the touchpad itself.
1712 * On these models the driver needs to explicitly enable/disable the i8042
1715 static const struct dmi_system_id ctrl_ps2_aux_port_list[] = {
1717 /* Lenovo Ideapad Z570 */
1719 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1720 DMI_MATCH(DMI_PRODUCT_VERSION, "Ideapad Z570"),
1726 static void ideapad_check_features(struct ideapad_private *priv)
1728 acpi_handle handle = priv->adev->handle;
1731 priv->features.set_fn_lock_led =
1732 set_fn_lock_led || dmi_check_system(set_fn_lock_led_list);
1733 priv->features.hw_rfkill_switch =
1734 hw_rfkill_switch || dmi_check_system(hw_rfkill_list);
1735 priv->features.ctrl_ps2_aux_port =
1736 ctrl_ps2_aux_port || dmi_check_system(ctrl_ps2_aux_port_list);
1737 priv->features.touchpad_ctrl_via_ec = touchpad_ctrl_via_ec;
1739 if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
1740 priv->features.fan_mode = true;
1742 if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
1743 priv->features.conservation_mode = true;
1745 if (acpi_has_method(handle, "DYTC"))
1746 priv->features.dytc = true;
1748 if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
1749 if (!eval_hals(handle, &val)) {
1750 if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
1751 priv->features.fn_lock = true;
1753 if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val)) {
1754 priv->features.kbd_bl = true;
1755 priv->kbd_bl.type = KBD_BL_STANDARD;
1758 if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
1759 priv->features.usb_charging = true;
1763 if (acpi_has_method(handle, "KBLC")) {
1764 if (!eval_kblc(priv->adev->handle, KBD_BL_QUERY_TYPE, &val)) {
1765 if (val == KBD_BL_TRISTATE_TYPE) {
1766 priv->features.kbd_bl = true;
1767 priv->kbd_bl.type = KBD_BL_TRISTATE;
1768 } else if (val == KBD_BL_TRISTATE_AUTO_TYPE) {
1769 priv->features.kbd_bl = true;
1770 priv->kbd_bl.type = KBD_BL_TRISTATE_AUTO;
1772 dev_warn(&priv->platform_device->dev,
1773 "Unknown keyboard type: %lu",
1780 #if IS_ENABLED(CONFIG_ACPI_WMI)
1784 enum ideapad_wmi_event_type {
1785 IDEAPAD_WMI_EVENT_ESC,
1786 IDEAPAD_WMI_EVENT_FN_KEYS,
1789 struct ideapad_wmi_private {
1790 enum ideapad_wmi_event_type event;
1793 static int ideapad_wmi_probe(struct wmi_device *wdev, const void *context)
1795 struct ideapad_wmi_private *wpriv;
1797 wpriv = devm_kzalloc(&wdev->dev, sizeof(*wpriv), GFP_KERNEL);
1801 *wpriv = *(const struct ideapad_wmi_private *)context;
1803 dev_set_drvdata(&wdev->dev, wpriv);
1807 static void ideapad_wmi_notify(struct wmi_device *wdev, union acpi_object *data)
1809 struct ideapad_wmi_private *wpriv = dev_get_drvdata(&wdev->dev);
1810 struct ideapad_private *priv;
1812 mutex_lock(&ideapad_shared_mutex);
1814 priv = ideapad_shared;
1818 switch (wpriv->event) {
1819 case IDEAPAD_WMI_EVENT_ESC:
1820 ideapad_input_report(priv, 128);
1822 case IDEAPAD_WMI_EVENT_FN_KEYS:
1823 if (priv->features.set_fn_lock_led) {
1824 int brightness = ideapad_fn_lock_get(priv);
1826 if (brightness >= 0) {
1827 ideapad_fn_lock_set(priv, brightness);
1828 ideapad_fn_lock_led_notify(priv, brightness);
1832 if (data->type != ACPI_TYPE_INTEGER) {
1833 dev_warn(&wdev->dev,
1834 "WMI event data is not an integer\n");
1838 dev_dbg(&wdev->dev, "WMI fn-key event: 0x%llx\n",
1839 data->integer.value);
1841 /* 0x02 FnLock, 0x03 Esc */
1842 if (data->integer.value == 0x02 || data->integer.value == 0x03)
1843 ideapad_fn_lock_led_notify(priv, data->integer.value == 0x02);
1845 ideapad_input_report(priv,
1846 data->integer.value | IDEAPAD_WMI_KEY);
1851 mutex_unlock(&ideapad_shared_mutex);
1854 static const struct ideapad_wmi_private ideapad_wmi_context_esc = {
1855 .event = IDEAPAD_WMI_EVENT_ESC
1858 static const struct ideapad_wmi_private ideapad_wmi_context_fn_keys = {
1859 .event = IDEAPAD_WMI_EVENT_FN_KEYS
1862 static const struct wmi_device_id ideapad_wmi_ids[] = {
1863 { "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", &ideapad_wmi_context_esc }, /* Yoga 3 */
1864 { "56322276-8493-4CE8-A783-98C991274F5E", &ideapad_wmi_context_esc }, /* Yoga 700 */
1865 { "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", &ideapad_wmi_context_fn_keys }, /* Legion 5 */
1868 MODULE_DEVICE_TABLE(wmi, ideapad_wmi_ids);
1870 static struct wmi_driver ideapad_wmi_driver = {
1872 .name = "ideapad_wmi",
1874 .id_table = ideapad_wmi_ids,
1875 .probe = ideapad_wmi_probe,
1876 .notify = ideapad_wmi_notify,
1879 static int ideapad_wmi_driver_register(void)
1881 return wmi_driver_register(&ideapad_wmi_driver);
1884 static void ideapad_wmi_driver_unregister(void)
1886 return wmi_driver_unregister(&ideapad_wmi_driver);
1890 static inline int ideapad_wmi_driver_register(void) { return 0; }
1891 static inline void ideapad_wmi_driver_unregister(void) { }
1897 static int ideapad_acpi_add(struct platform_device *pdev)
1899 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
1900 struct ideapad_private *priv;
1905 if (!adev || eval_int(adev->handle, "_CFG", &cfg))
1908 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1912 dev_set_drvdata(&pdev->dev, priv);
1916 priv->platform_device = pdev;
1918 ideapad_check_features(priv);
1920 err = ideapad_sysfs_init(priv);
1924 ideapad_debugfs_init(priv);
1926 err = ideapad_input_init(priv);
1930 err = ideapad_kbd_bl_init(priv);
1933 dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
1935 dev_info(&pdev->dev, "Keyboard backlight control not available\n");
1938 err = ideapad_fn_lock_led_init(priv);
1941 dev_warn(&pdev->dev, "Could not set up FnLock LED: %d\n", err);
1943 dev_info(&pdev->dev, "FnLock control not available\n");
1947 * On some models without a hw-switch (the yoga 2 13 at least)
1948 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1950 if (!priv->features.hw_rfkill_switch)
1951 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1953 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1954 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1955 ideapad_register_rfkill(priv, i);
1957 ideapad_sync_rfk_state(priv);
1958 ideapad_sync_touchpad_state(priv, false);
1960 err = ideapad_dytc_profile_init(priv);
1963 dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
1965 dev_info(&pdev->dev, "DYTC interface is not available\n");
1968 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1969 err = ideapad_backlight_init(priv);
1970 if (err && err != -ENODEV)
1971 goto backlight_failed;
1974 status = acpi_install_notify_handler(adev->handle,
1976 ideapad_acpi_notify, priv);
1977 if (ACPI_FAILURE(status)) {
1979 goto notification_failed;
1982 err = ideapad_shared_init(priv);
1984 goto shared_init_failed;
1989 acpi_remove_notify_handler(priv->adev->handle,
1991 ideapad_acpi_notify);
1993 notification_failed:
1994 ideapad_backlight_exit(priv);
1997 ideapad_dytc_profile_exit(priv);
1999 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
2000 ideapad_unregister_rfkill(priv, i);
2002 ideapad_fn_lock_led_exit(priv);
2003 ideapad_kbd_bl_exit(priv);
2004 ideapad_input_exit(priv);
2007 ideapad_debugfs_exit(priv);
2008 ideapad_sysfs_exit(priv);
2013 static void ideapad_acpi_remove(struct platform_device *pdev)
2015 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
2018 ideapad_shared_exit(priv);
2020 acpi_remove_notify_handler(priv->adev->handle,
2022 ideapad_acpi_notify);
2024 ideapad_backlight_exit(priv);
2025 ideapad_dytc_profile_exit(priv);
2027 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
2028 ideapad_unregister_rfkill(priv, i);
2030 ideapad_fn_lock_led_exit(priv);
2031 ideapad_kbd_bl_exit(priv);
2032 ideapad_input_exit(priv);
2033 ideapad_debugfs_exit(priv);
2034 ideapad_sysfs_exit(priv);
2037 #ifdef CONFIG_PM_SLEEP
2038 static int ideapad_acpi_resume(struct device *dev)
2040 struct ideapad_private *priv = dev_get_drvdata(dev);
2042 ideapad_sync_rfk_state(priv);
2043 ideapad_sync_touchpad_state(priv, false);
2046 dytc_profile_refresh(priv);
2051 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
2053 static const struct acpi_device_id ideapad_device_ids[] = {
2057 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
2059 static struct platform_driver ideapad_acpi_driver = {
2060 .probe = ideapad_acpi_add,
2061 .remove_new = ideapad_acpi_remove,
2063 .name = "ideapad_acpi",
2065 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
2069 static int __init ideapad_laptop_init(void)
2073 err = ideapad_wmi_driver_register();
2077 err = platform_driver_register(&ideapad_acpi_driver);
2079 ideapad_wmi_driver_unregister();
2085 module_init(ideapad_laptop_init)
2087 static void __exit ideapad_laptop_exit(void)
2089 ideapad_wmi_driver_unregister();
2090 platform_driver_unregister(&ideapad_acpi_driver);
2092 module_exit(ideapad_laptop_exit)
2094 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2095 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
2096 MODULE_LICENSE("GPL");