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 static bool no_bt_rfkill;
158 module_param(no_bt_rfkill, bool, 0444);
159 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
161 static bool allow_v4_dytc;
162 module_param(allow_v4_dytc, bool, 0444);
163 MODULE_PARM_DESC(allow_v4_dytc,
164 "Enable DYTC version 4 platform-profile support. "
165 "If you need this please report this to: platform-driver-x86@vger.kernel.org");
167 static bool hw_rfkill_switch;
168 module_param(hw_rfkill_switch, bool, 0444);
169 MODULE_PARM_DESC(hw_rfkill_switch,
170 "Enable rfkill support for laptops with a hw on/off wifi switch/slider. "
171 "If you need this please report this to: platform-driver-x86@vger.kernel.org");
173 static bool set_fn_lock_led;
174 module_param(set_fn_lock_led, bool, 0444);
175 MODULE_PARM_DESC(set_fn_lock_led,
176 "Enable driver based updates of the fn-lock LED on fn-lock changes. "
177 "If you need this please report this to: platform-driver-x86@vger.kernel.org");
179 static bool ctrl_ps2_aux_port;
180 module_param(ctrl_ps2_aux_port, bool, 0444);
181 MODULE_PARM_DESC(ctrl_ps2_aux_port,
182 "Enable driver based PS/2 aux port en-/dis-abling on touchpad on/off toggle. "
183 "If you need this please report this to: platform-driver-x86@vger.kernel.org");
185 static bool touchpad_ctrl_via_ec;
186 module_param(touchpad_ctrl_via_ec, bool, 0444);
187 MODULE_PARM_DESC(touchpad_ctrl_via_ec,
188 "Enable registering a 'touchpad' sysfs-attribute which can be used to manually "
189 "tell the EC to enable/disable the touchpad. This may not work on all models.");
195 static struct ideapad_private *ideapad_shared;
196 static DEFINE_MUTEX(ideapad_shared_mutex);
198 static int ideapad_shared_init(struct ideapad_private *priv)
202 mutex_lock(&ideapad_shared_mutex);
204 if (!ideapad_shared) {
205 ideapad_shared = priv;
208 dev_warn(&priv->adev->dev, "found multiple platform devices\n");
212 mutex_unlock(&ideapad_shared_mutex);
217 static void ideapad_shared_exit(struct ideapad_private *priv)
219 mutex_lock(&ideapad_shared_mutex);
221 if (ideapad_shared == priv)
222 ideapad_shared = NULL;
224 mutex_unlock(&ideapad_shared_mutex);
231 static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
233 unsigned long long result;
236 status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
237 if (ACPI_FAILURE(status))
245 static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
247 acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
249 return ACPI_FAILURE(status) ? -EIO : 0;
252 static int eval_gbmd(acpi_handle handle, unsigned long *res)
254 return eval_int(handle, "GBMD", res);
257 static int exec_sbmc(acpi_handle handle, unsigned long arg)
259 return exec_simple_method(handle, "SBMC", arg);
262 static int eval_hals(acpi_handle handle, unsigned long *res)
264 return eval_int(handle, "HALS", res);
267 static int exec_sals(acpi_handle handle, unsigned long arg)
269 return exec_simple_method(handle, "SALS", arg);
272 static int exec_kblc(acpi_handle handle, unsigned long arg)
274 return exec_simple_method(handle, "KBLC", arg);
277 static int eval_kblc(acpi_handle handle, unsigned long cmd, unsigned long *res)
279 return eval_int_with_arg(handle, "KBLC", cmd, res);
282 static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
284 return eval_int_with_arg(handle, "DYTC", cmd, res);
290 static int debugfs_status_show(struct seq_file *s, void *data)
292 struct ideapad_private *priv = s->private;
295 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
296 seq_printf(s, "Backlight max: %lu\n", value);
297 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
298 seq_printf(s, "Backlight now: %lu\n", value);
299 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
300 seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
302 seq_puts(s, "=====================\n");
304 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
305 seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
306 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
307 seq_printf(s, "Wifi status: %s (%lu)\n", value ? "on" : "off", value);
308 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
309 seq_printf(s, "BT status: %s (%lu)\n", value ? "on" : "off", value);
310 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
311 seq_printf(s, "3G status: %s (%lu)\n", value ? "on" : "off", value);
313 seq_puts(s, "=====================\n");
315 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
316 seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
317 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
318 seq_printf(s, "Camera status: %s (%lu)\n", value ? "on" : "off", value);
320 seq_puts(s, "=====================\n");
322 if (!eval_gbmd(priv->adev->handle, &value))
323 seq_printf(s, "GBMD: %#010lx\n", value);
324 if (!eval_hals(priv->adev->handle, &value))
325 seq_printf(s, "HALS: %#010lx\n", value);
329 DEFINE_SHOW_ATTRIBUTE(debugfs_status);
331 static int debugfs_cfg_show(struct seq_file *s, void *data)
333 struct ideapad_private *priv = s->private;
335 seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
337 seq_puts(s, "Capabilities:");
338 if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
339 seq_puts(s, " bluetooth");
340 if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
342 if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
343 seq_puts(s, " wifi");
344 if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
345 seq_puts(s, " camera");
348 seq_puts(s, "OSD support:");
349 if (test_bit(CFG_OSD_NUMLK_BIT, &priv->cfg))
350 seq_puts(s, " num-lock");
351 if (test_bit(CFG_OSD_CAPSLK_BIT, &priv->cfg))
352 seq_puts(s, " caps-lock");
353 if (test_bit(CFG_OSD_MICMUTE_BIT, &priv->cfg))
354 seq_puts(s, " mic-mute");
355 if (test_bit(CFG_OSD_TOUCHPAD_BIT, &priv->cfg))
356 seq_puts(s, " touchpad");
357 if (test_bit(CFG_OSD_CAM_BIT, &priv->cfg))
358 seq_puts(s, " camera");
361 seq_puts(s, "Graphics: ");
362 switch (priv->cfg & 0x700) {
364 seq_puts(s, "Intel");
370 seq_puts(s, "Nvidia");
373 seq_puts(s, "Intel and ATI");
376 seq_puts(s, "Intel and Nvidia");
383 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
385 static void ideapad_debugfs_init(struct ideapad_private *priv)
389 dir = debugfs_create_dir("ideapad", NULL);
392 debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
393 debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
396 static void ideapad_debugfs_exit(struct ideapad_private *priv)
398 debugfs_remove_recursive(priv->debug);
405 static ssize_t camera_power_show(struct device *dev,
406 struct device_attribute *attr,
409 struct ideapad_private *priv = dev_get_drvdata(dev);
410 unsigned long result;
413 err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
417 return sysfs_emit(buf, "%d\n", !!result);
420 static ssize_t camera_power_store(struct device *dev,
421 struct device_attribute *attr,
422 const char *buf, size_t count)
424 struct ideapad_private *priv = dev_get_drvdata(dev);
428 err = kstrtobool(buf, &state);
432 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
439 static DEVICE_ATTR_RW(camera_power);
441 static ssize_t conservation_mode_show(struct device *dev,
442 struct device_attribute *attr,
445 struct ideapad_private *priv = dev_get_drvdata(dev);
446 unsigned long result;
449 err = eval_gbmd(priv->adev->handle, &result);
453 return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
456 static ssize_t conservation_mode_store(struct device *dev,
457 struct device_attribute *attr,
458 const char *buf, size_t count)
460 struct ideapad_private *priv = dev_get_drvdata(dev);
464 err = kstrtobool(buf, &state);
468 err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
475 static DEVICE_ATTR_RW(conservation_mode);
477 static ssize_t fan_mode_show(struct device *dev,
478 struct device_attribute *attr,
481 struct ideapad_private *priv = dev_get_drvdata(dev);
482 unsigned long result;
485 err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
489 return sysfs_emit(buf, "%lu\n", result);
492 static ssize_t fan_mode_store(struct device *dev,
493 struct device_attribute *attr,
494 const char *buf, size_t count)
496 struct ideapad_private *priv = dev_get_drvdata(dev);
500 err = kstrtouint(buf, 0, &state);
504 if (state > 4 || state == 3)
507 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
514 static DEVICE_ATTR_RW(fan_mode);
516 static ssize_t fn_lock_show(struct device *dev,
517 struct device_attribute *attr,
520 struct ideapad_private *priv = dev_get_drvdata(dev);
524 err = eval_hals(priv->adev->handle, &hals);
528 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_FNLOCK_STATE_BIT, &hals));
531 static ssize_t fn_lock_store(struct device *dev,
532 struct device_attribute *attr,
533 const char *buf, size_t count)
535 struct ideapad_private *priv = dev_get_drvdata(dev);
539 err = kstrtobool(buf, &state);
543 err = exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
550 static DEVICE_ATTR_RW(fn_lock);
552 static ssize_t touchpad_show(struct device *dev,
553 struct device_attribute *attr,
556 struct ideapad_private *priv = dev_get_drvdata(dev);
557 unsigned long result;
560 err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
564 priv->r_touchpad_val = result;
566 return sysfs_emit(buf, "%d\n", !!result);
569 static ssize_t touchpad_store(struct device *dev,
570 struct device_attribute *attr,
571 const char *buf, size_t count)
573 struct ideapad_private *priv = dev_get_drvdata(dev);
577 err = kstrtobool(buf, &state);
581 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
585 priv->r_touchpad_val = state;
590 static DEVICE_ATTR_RW(touchpad);
592 static ssize_t usb_charging_show(struct device *dev,
593 struct device_attribute *attr,
596 struct ideapad_private *priv = dev_get_drvdata(dev);
600 err = eval_hals(priv->adev->handle, &hals);
604 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
607 static ssize_t usb_charging_store(struct device *dev,
608 struct device_attribute *attr,
609 const char *buf, size_t count)
611 struct ideapad_private *priv = dev_get_drvdata(dev);
615 err = kstrtobool(buf, &state);
619 err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
626 static DEVICE_ATTR_RW(usb_charging);
628 static struct attribute *ideapad_attributes[] = {
629 &dev_attr_camera_power.attr,
630 &dev_attr_conservation_mode.attr,
631 &dev_attr_fan_mode.attr,
632 &dev_attr_fn_lock.attr,
633 &dev_attr_touchpad.attr,
634 &dev_attr_usb_charging.attr,
638 static umode_t ideapad_is_visible(struct kobject *kobj,
639 struct attribute *attr,
642 struct device *dev = kobj_to_dev(kobj);
643 struct ideapad_private *priv = dev_get_drvdata(dev);
644 bool supported = true;
646 if (attr == &dev_attr_camera_power.attr)
647 supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
648 else if (attr == &dev_attr_conservation_mode.attr)
649 supported = priv->features.conservation_mode;
650 else if (attr == &dev_attr_fan_mode.attr)
651 supported = priv->features.fan_mode;
652 else if (attr == &dev_attr_fn_lock.attr)
653 supported = priv->features.fn_lock;
654 else if (attr == &dev_attr_touchpad.attr)
655 supported = priv->features.touchpad_ctrl_via_ec;
656 else if (attr == &dev_attr_usb_charging.attr)
657 supported = priv->features.usb_charging;
659 return supported ? attr->mode : 0;
662 static const struct attribute_group ideapad_attribute_group = {
663 .is_visible = ideapad_is_visible,
664 .attrs = ideapad_attributes
668 * DYTC Platform profile
670 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
671 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
672 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
673 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
675 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
676 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
677 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
679 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
680 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
682 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
683 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
684 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
686 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
687 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
688 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
690 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
691 #define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
692 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
694 #define DYTC_SET_COMMAND(function, mode, on) \
695 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
696 (mode) << DYTC_SET_MODE_BIT | \
697 (on) << DYTC_SET_VALID_BIT)
699 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
701 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
703 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
706 case DYTC_MODE_LOW_POWER:
707 *profile = PLATFORM_PROFILE_LOW_POWER;
709 case DYTC_MODE_BALANCE:
710 *profile = PLATFORM_PROFILE_BALANCED;
712 case DYTC_MODE_PERFORM:
713 *profile = PLATFORM_PROFILE_PERFORMANCE;
715 default: /* Unknown mode */
722 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
725 case PLATFORM_PROFILE_LOW_POWER:
726 *perfmode = DYTC_MODE_LOW_POWER;
728 case PLATFORM_PROFILE_BALANCED:
729 *perfmode = DYTC_MODE_BALANCE;
731 case PLATFORM_PROFILE_PERFORMANCE:
732 *perfmode = DYTC_MODE_PERFORM;
734 default: /* Unknown profile */
742 * dytc_profile_get: Function to register with platform_profile
743 * handler. Returns current platform profile.
745 static int dytc_profile_get(struct platform_profile_handler *pprof,
746 enum platform_profile_option *profile)
748 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
750 *profile = dytc->current_profile;
755 * Helper function - check if we are in CQL mode and if we are
759 * If not in CQL mode, just run the command
761 static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
762 unsigned long *output)
764 int err, cmd_err, cur_funcmode;
766 /* Determine if we are in CQL mode. This alters the commands we do */
767 err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
771 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
772 /* Check if we're OK to return immediately */
773 if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
776 if (cur_funcmode == DYTC_FUNCTION_CQL) {
777 err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
782 cmd_err = eval_dytc(priv->adev->handle, cmd, output);
783 /* Check return condition after we've restored CQL state */
785 if (cur_funcmode == DYTC_FUNCTION_CQL) {
786 err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
795 * dytc_profile_set: Function to register with platform_profile
796 * handler. Sets current platform profile.
798 static int dytc_profile_set(struct platform_profile_handler *pprof,
799 enum platform_profile_option profile)
801 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
802 struct ideapad_private *priv = dytc->priv;
803 unsigned long output;
806 err = mutex_lock_interruptible(&dytc->mutex);
810 if (profile == PLATFORM_PROFILE_BALANCED) {
811 /* To get back to balanced mode we just issue a reset command */
812 err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
818 err = convert_profile_to_dytc(profile, &perfmode);
822 /* Determine if we are in CQL mode. This alters the commands we do */
823 err = dytc_cql_command(priv, DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
829 /* Success - update current profile */
830 dytc->current_profile = profile;
833 mutex_unlock(&dytc->mutex);
838 static void dytc_profile_refresh(struct ideapad_private *priv)
840 enum platform_profile_option profile;
841 unsigned long output;
844 mutex_lock(&priv->dytc->mutex);
845 err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
846 mutex_unlock(&priv->dytc->mutex);
850 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
852 if (convert_dytc_to_profile(perfmode, &profile))
855 if (profile != priv->dytc->current_profile) {
856 priv->dytc->current_profile = profile;
857 platform_profile_notify();
861 static const struct dmi_system_id ideapad_dytc_v4_allow_table[] = {
863 /* Ideapad 5 Pro 16ACH6 */
865 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
866 DMI_MATCH(DMI_PRODUCT_NAME, "82L5")
870 /* Ideapad 5 15ITL05 */
872 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
873 DMI_MATCH(DMI_PRODUCT_VERSION, "IdeaPad 5 15ITL05")
879 static int ideapad_dytc_profile_init(struct ideapad_private *priv)
881 int err, dytc_version;
882 unsigned long output;
884 if (!priv->features.dytc)
887 err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
888 /* For all other errors we can flag the failure */
892 /* Check DYTC is enabled and supports mode setting */
893 if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output)) {
894 dev_info(&priv->platform_device->dev, "DYTC_QUERY_ENABLE_BIT returned false\n");
898 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
900 if (dytc_version < 4) {
901 dev_info(&priv->platform_device->dev, "DYTC_VERSION < 4 is not supported\n");
905 if (dytc_version < 5 &&
906 !(allow_v4_dytc || dmi_check_system(ideapad_dytc_v4_allow_table))) {
907 dev_info(&priv->platform_device->dev,
908 "DYTC_VERSION 4 support may not work. Pass ideapad_laptop.allow_v4_dytc=Y on the kernel commandline to enable\n");
912 priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
916 mutex_init(&priv->dytc->mutex);
918 priv->dytc->priv = priv;
919 priv->dytc->pprof.profile_get = dytc_profile_get;
920 priv->dytc->pprof.profile_set = dytc_profile_set;
922 /* Setup supported modes */
923 set_bit(PLATFORM_PROFILE_LOW_POWER, priv->dytc->pprof.choices);
924 set_bit(PLATFORM_PROFILE_BALANCED, priv->dytc->pprof.choices);
925 set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->dytc->pprof.choices);
927 /* Create platform_profile structure and register */
928 err = platform_profile_register(&priv->dytc->pprof);
932 /* Ensure initial values are correct */
933 dytc_profile_refresh(priv);
938 mutex_destroy(&priv->dytc->mutex);
945 static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
950 platform_profile_remove();
951 mutex_destroy(&priv->dytc->mutex);
960 struct ideapad_rfk_data {
967 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
968 { "ideapad_wlan", CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
969 { "ideapad_bluetooth", CFG_CAP_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
970 { "ideapad_3g", CFG_CAP_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
973 static int ideapad_rfk_set(void *data, bool blocked)
975 struct ideapad_rfk_priv *priv = data;
976 int opcode = ideapad_rfk_data[priv->dev].opcode;
978 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
981 static const struct rfkill_ops ideapad_rfk_ops = {
982 .set_block = ideapad_rfk_set,
985 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
987 unsigned long hw_blocked = 0;
990 if (priv->features.hw_rfkill_switch) {
991 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
993 hw_blocked = !hw_blocked;
996 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
998 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
1001 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
1003 unsigned long rf_enabled;
1006 if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
1007 /* Force to enable bluetooth when no_bt_rfkill=1 */
1008 write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
1012 priv->rfk_priv[dev].dev = dev;
1013 priv->rfk_priv[dev].priv = priv;
1015 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
1016 &priv->platform_device->dev,
1017 ideapad_rfk_data[dev].type,
1019 &priv->rfk_priv[dev]);
1020 if (!priv->rfk[dev])
1023 err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
1027 rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
1029 err = rfkill_register(priv->rfk[dev]);
1031 rfkill_destroy(priv->rfk[dev]);
1036 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1038 if (!priv->rfk[dev])
1041 rfkill_unregister(priv->rfk[dev]);
1042 rfkill_destroy(priv->rfk[dev]);
1048 static int ideapad_sysfs_init(struct ideapad_private *priv)
1050 return device_add_group(&priv->platform_device->dev,
1051 &ideapad_attribute_group);
1054 static void ideapad_sysfs_exit(struct ideapad_private *priv)
1056 device_remove_group(&priv->platform_device->dev,
1057 &ideapad_attribute_group);
1063 #define IDEAPAD_WMI_KEY 0x100
1065 static const struct key_entry ideapad_keymap[] = {
1066 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
1067 { KE_KEY, 7, { KEY_CAMERA } },
1068 { KE_KEY, 8, { KEY_MICMUTE } },
1069 { KE_KEY, 11, { KEY_F16 } },
1070 { KE_KEY, 13, { KEY_WLAN } },
1071 { KE_KEY, 16, { KEY_PROG1 } },
1072 { KE_KEY, 17, { KEY_PROG2 } },
1073 { KE_KEY, 64, { KEY_PROG3 } },
1074 { KE_KEY, 65, { KEY_PROG4 } },
1075 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
1076 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
1077 { KE_KEY, 128, { KEY_ESC } },
1083 /* FnLock (handled by the firmware) */
1084 { KE_IGNORE, 0x02 | IDEAPAD_WMI_KEY },
1085 /* Esc (handled by the firmware) */
1086 { KE_IGNORE, 0x03 | IDEAPAD_WMI_KEY },
1087 /* Customizable Lenovo Hotkey ("star" with 'S' inside) */
1088 { KE_KEY, 0x01 | IDEAPAD_WMI_KEY, { KEY_FAVORITES } },
1089 { KE_KEY, 0x04 | IDEAPAD_WMI_KEY, { KEY_SELECTIVE_SCREENSHOT } },
1090 /* Lenovo Support */
1091 { KE_KEY, 0x07 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1092 { KE_KEY, 0x0e | IDEAPAD_WMI_KEY, { KEY_PICKUP_PHONE } },
1093 { KE_KEY, 0x0f | IDEAPAD_WMI_KEY, { KEY_HANGUP_PHONE } },
1094 /* Dark mode toggle */
1095 { KE_KEY, 0x13 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
1096 /* Sound profile switch */
1097 { KE_KEY, 0x12 | IDEAPAD_WMI_KEY, { KEY_PROG2 } },
1098 /* Lenovo Virtual Background application */
1099 { KE_KEY, 0x28 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
1100 /* Lenovo Support */
1101 { KE_KEY, 0x27 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1102 /* Refresh Rate Toggle */
1103 { KE_KEY, 0x0a | IDEAPAD_WMI_KEY, { KEY_DISPLAYTOGGLE } },
1108 static int ideapad_input_init(struct ideapad_private *priv)
1110 struct input_dev *inputdev;
1113 inputdev = input_allocate_device();
1117 inputdev->name = "Ideapad extra buttons";
1118 inputdev->phys = "ideapad/input0";
1119 inputdev->id.bustype = BUS_HOST;
1120 inputdev->dev.parent = &priv->platform_device->dev;
1122 err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1124 dev_err(&priv->platform_device->dev,
1125 "Could not set up input device keymap: %d\n", err);
1129 err = input_register_device(inputdev);
1131 dev_err(&priv->platform_device->dev,
1132 "Could not register input device: %d\n", err);
1136 priv->inputdev = inputdev;
1141 input_free_device(inputdev);
1146 static void ideapad_input_exit(struct ideapad_private *priv)
1148 input_unregister_device(priv->inputdev);
1149 priv->inputdev = NULL;
1152 static void ideapad_input_report(struct ideapad_private *priv,
1153 unsigned long scancode)
1155 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1158 static void ideapad_input_novokey(struct ideapad_private *priv)
1160 unsigned long long_pressed;
1162 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1166 ideapad_input_report(priv, 17);
1168 ideapad_input_report(priv, 16);
1171 static void ideapad_check_special_buttons(struct ideapad_private *priv)
1173 unsigned long bit, value;
1175 if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1178 for_each_set_bit (bit, &value, 16) {
1182 /* Thermal Management button */
1183 ideapad_input_report(priv, 65);
1186 /* OneKey Theater button */
1187 ideapad_input_report(priv, 64);
1190 dev_info(&priv->platform_device->dev,
1191 "Unknown special button: %lu\n", bit);
1200 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1202 struct ideapad_private *priv = bl_get_data(blightdev);
1206 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1213 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1215 struct ideapad_private *priv = bl_get_data(blightdev);
1218 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1219 blightdev->props.brightness);
1223 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1224 blightdev->props.power != FB_BLANK_POWERDOWN);
1231 static const struct backlight_ops ideapad_backlight_ops = {
1232 .get_brightness = ideapad_backlight_get_brightness,
1233 .update_status = ideapad_backlight_update_status,
1236 static int ideapad_backlight_init(struct ideapad_private *priv)
1238 struct backlight_device *blightdev;
1239 struct backlight_properties props;
1240 unsigned long max, now, power;
1243 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1247 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1251 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1255 memset(&props, 0, sizeof(props));
1257 props.max_brightness = max;
1258 props.type = BACKLIGHT_PLATFORM;
1260 blightdev = backlight_device_register("ideapad",
1261 &priv->platform_device->dev,
1263 &ideapad_backlight_ops,
1265 if (IS_ERR(blightdev)) {
1266 err = PTR_ERR(blightdev);
1267 dev_err(&priv->platform_device->dev,
1268 "Could not register backlight device: %d\n", err);
1272 priv->blightdev = blightdev;
1273 blightdev->props.brightness = now;
1274 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1276 backlight_update_status(blightdev);
1281 static void ideapad_backlight_exit(struct ideapad_private *priv)
1283 backlight_device_unregister(priv->blightdev);
1284 priv->blightdev = NULL;
1287 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1289 struct backlight_device *blightdev = priv->blightdev;
1290 unsigned long power;
1295 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1298 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1301 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1305 /* if we control brightness via acpi video driver */
1306 if (!priv->blightdev)
1307 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1309 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1313 * keyboard backlight
1315 static int ideapad_kbd_bl_check_tristate(int type)
1317 return (type == KBD_BL_TRISTATE) || (type == KBD_BL_TRISTATE_AUTO);
1320 static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1322 unsigned long value;
1325 if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
1326 err = eval_kblc(priv->adev->handle,
1327 FIELD_PREP(KBD_BL_COMMAND_TYPE, priv->kbd_bl.type) |
1334 /* Convert returned value to brightness level */
1335 value = FIELD_GET(KBD_BL_GET_BRIGHTNESS, value);
1337 /* Off, low or high */
1338 if (value <= priv->kbd_bl.led.max_brightness)
1341 /* Auto, report as off */
1342 if (value == priv->kbd_bl.led.max_brightness + 1)
1346 dev_warn(&priv->platform_device->dev,
1347 "Unknown keyboard backlight value: %lu", value);
1351 err = eval_hals(priv->adev->handle, &value);
1355 return !!test_bit(HALS_KBD_BL_STATE_BIT, &value);
1358 static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1360 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1362 return ideapad_kbd_bl_brightness_get(priv);
1365 static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1368 unsigned long value;
1369 int type = priv->kbd_bl.type;
1371 if (ideapad_kbd_bl_check_tristate(type)) {
1372 if (brightness > priv->kbd_bl.led.max_brightness)
1375 value = FIELD_PREP(KBD_BL_SET_BRIGHTNESS, brightness) |
1376 FIELD_PREP(KBD_BL_COMMAND_TYPE, type) |
1378 err = exec_kblc(priv->adev->handle, value);
1380 err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1386 priv->kbd_bl.last_brightness = brightness;
1391 static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1392 enum led_brightness brightness)
1394 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1396 return ideapad_kbd_bl_brightness_set(priv, brightness);
1399 static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1403 if (!priv->kbd_bl.initialized)
1406 brightness = ideapad_kbd_bl_brightness_get(priv);
1410 if (brightness == priv->kbd_bl.last_brightness)
1413 priv->kbd_bl.last_brightness = brightness;
1415 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1418 static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1420 int brightness, err;
1422 if (!priv->features.kbd_bl)
1425 if (WARN_ON(priv->kbd_bl.initialized))
1428 brightness = ideapad_kbd_bl_brightness_get(priv);
1432 priv->kbd_bl.last_brightness = brightness;
1434 if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
1435 priv->kbd_bl.led.max_brightness = 2;
1437 priv->kbd_bl.led.max_brightness = 1;
1440 priv->kbd_bl.led.name = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1441 priv->kbd_bl.led.brightness_get = ideapad_kbd_bl_led_cdev_brightness_get;
1442 priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1443 priv->kbd_bl.led.flags = LED_BRIGHT_HW_CHANGED;
1445 err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1449 priv->kbd_bl.initialized = true;
1454 static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1456 if (!priv->kbd_bl.initialized)
1459 priv->kbd_bl.initialized = false;
1461 led_classdev_unregister(&priv->kbd_bl.led);
1467 static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_events)
1469 unsigned long value;
1470 unsigned char param;
1473 /* Without reading from EC touchpad LED doesn't switch state */
1474 ret = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value);
1479 * Some IdeaPads don't really turn off touchpad - they only
1480 * switch the LED state. We (de)activate KBC AUX port to turn
1481 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1482 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1484 if (priv->features.ctrl_ps2_aux_port)
1485 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1488 * On older models the EC controls the touchpad and toggles it on/off
1489 * itself, in this case we report KEY_TOUCHPAD_ON/_OFF. Some models do
1490 * an acpi-notify with VPC bit 5 set on resume, so this function get
1491 * called with send_events=true on every resume. Therefor if the EC did
1492 * not toggle, do nothing to avoid sending spurious KEY_TOUCHPAD_TOGGLE.
1494 if (send_events && value != priv->r_touchpad_val) {
1495 ideapad_input_report(priv, value ? 67 : 66);
1496 sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1499 priv->r_touchpad_val = value;
1502 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1504 struct ideapad_private *priv = data;
1505 unsigned long vpc1, vpc2, bit;
1507 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1510 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1513 vpc1 = (vpc2 << 8) | vpc1;
1515 for_each_set_bit (bit, &vpc1, 16) {
1522 ideapad_input_report(priv, bit);
1526 * This event gets send on a Yoga 300-11IBR when the EC
1527 * believes that the device has changed between laptop/
1528 * tent/stand/tablet mode. The EC relies on getting
1529 * angle info from 2 accelerometers through a special
1530 * windows service calling a DSM on the DUAL250E ACPI-
1531 * device. Linux does not do this, making the laptop/
1532 * tent/stand/tablet mode info unreliable, so we simply
1533 * ignore these events.
1537 ideapad_sync_rfk_state(priv);
1540 ideapad_sync_touchpad_state(priv, true);
1543 ideapad_backlight_notify_brightness(priv);
1546 ideapad_input_novokey(priv);
1549 ideapad_backlight_notify_power(priv);
1551 case KBD_BL_KBLC_CHANGED_EVENT:
1554 * Some IdeaPads report event 1 every ~20
1555 * seconds while on battery power; some
1556 * report this when changing to/from tablet
1557 * mode; some report this when the keyboard
1558 * backlight has changed.
1560 ideapad_kbd_bl_notify(priv);
1563 ideapad_check_special_buttons(priv);
1566 dev_info(&priv->platform_device->dev,
1567 "Unknown event: %lu\n", bit);
1572 /* On some models we need to call exec_sals(SALS_FNLOCK_ON/OFF) to set the LED */
1573 static const struct dmi_system_id set_fn_lock_led_list[] = {
1575 /* https://bugzilla.kernel.org/show_bug.cgi?id=212671 */
1577 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1578 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion R7000P2020H"),
1583 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1584 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion 5 15ARH05"),
1591 * Some ideapads have a hardware rfkill switch, but most do not have one.
1592 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1593 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1594 * There used to be a long list of DMI ids for models without a hw rfkill
1595 * switch here, but that resulted in playing whack a mole.
1596 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1597 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1598 * hw-blocked results in an empty SSID list, which is a much more benign
1600 * So the default now is the much safer option of assuming there is no
1601 * hardware rfkill switch. This default also actually matches most hardware,
1602 * since having a hw rfkill switch is quite rare on modern hardware, so this
1603 * also leads to a much shorter list.
1605 static const struct dmi_system_id hw_rfkill_list[] = {
1610 * On some models the EC toggles the touchpad muted LED on touchpad toggle
1611 * hotkey presses, but the EC does not actually disable the touchpad itself.
1612 * On these models the driver needs to explicitly enable/disable the i8042
1615 static const struct dmi_system_id ctrl_ps2_aux_port_list[] = {
1617 /* Lenovo Ideapad Z570 */
1619 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1620 DMI_MATCH(DMI_PRODUCT_VERSION, "Ideapad Z570"),
1626 static void ideapad_check_features(struct ideapad_private *priv)
1628 acpi_handle handle = priv->adev->handle;
1631 priv->features.set_fn_lock_led =
1632 set_fn_lock_led || dmi_check_system(set_fn_lock_led_list);
1633 priv->features.hw_rfkill_switch =
1634 hw_rfkill_switch || dmi_check_system(hw_rfkill_list);
1635 priv->features.ctrl_ps2_aux_port =
1636 ctrl_ps2_aux_port || dmi_check_system(ctrl_ps2_aux_port_list);
1637 priv->features.touchpad_ctrl_via_ec = touchpad_ctrl_via_ec;
1639 if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
1640 priv->features.fan_mode = true;
1642 if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
1643 priv->features.conservation_mode = true;
1645 if (acpi_has_method(handle, "DYTC"))
1646 priv->features.dytc = true;
1648 if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
1649 if (!eval_hals(handle, &val)) {
1650 if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
1651 priv->features.fn_lock = true;
1653 if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val)) {
1654 priv->features.kbd_bl = true;
1655 priv->kbd_bl.type = KBD_BL_STANDARD;
1658 if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
1659 priv->features.usb_charging = true;
1663 if (acpi_has_method(handle, "KBLC")) {
1664 if (!eval_kblc(priv->adev->handle, KBD_BL_QUERY_TYPE, &val)) {
1665 if (val == KBD_BL_TRISTATE_TYPE) {
1666 priv->features.kbd_bl = true;
1667 priv->kbd_bl.type = KBD_BL_TRISTATE;
1668 } else if (val == KBD_BL_TRISTATE_AUTO_TYPE) {
1669 priv->features.kbd_bl = true;
1670 priv->kbd_bl.type = KBD_BL_TRISTATE_AUTO;
1672 dev_warn(&priv->platform_device->dev,
1673 "Unknown keyboard type: %lu",
1680 #if IS_ENABLED(CONFIG_ACPI_WMI)
1684 enum ideapad_wmi_event_type {
1685 IDEAPAD_WMI_EVENT_ESC,
1686 IDEAPAD_WMI_EVENT_FN_KEYS,
1689 struct ideapad_wmi_private {
1690 enum ideapad_wmi_event_type event;
1693 static int ideapad_wmi_probe(struct wmi_device *wdev, const void *context)
1695 struct ideapad_wmi_private *wpriv;
1697 wpriv = devm_kzalloc(&wdev->dev, sizeof(*wpriv), GFP_KERNEL);
1701 *wpriv = *(const struct ideapad_wmi_private *)context;
1703 dev_set_drvdata(&wdev->dev, wpriv);
1707 static void ideapad_wmi_notify(struct wmi_device *wdev, union acpi_object *data)
1709 struct ideapad_wmi_private *wpriv = dev_get_drvdata(&wdev->dev);
1710 struct ideapad_private *priv;
1711 unsigned long result;
1713 mutex_lock(&ideapad_shared_mutex);
1715 priv = ideapad_shared;
1719 switch (wpriv->event) {
1720 case IDEAPAD_WMI_EVENT_ESC:
1721 ideapad_input_report(priv, 128);
1723 case IDEAPAD_WMI_EVENT_FN_KEYS:
1724 if (priv->features.set_fn_lock_led &&
1725 !eval_hals(priv->adev->handle, &result)) {
1726 bool state = test_bit(HALS_FNLOCK_STATE_BIT, &result);
1728 exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
1731 if (data->type != ACPI_TYPE_INTEGER) {
1732 dev_warn(&wdev->dev,
1733 "WMI event data is not an integer\n");
1737 dev_dbg(&wdev->dev, "WMI fn-key event: 0x%llx\n",
1738 data->integer.value);
1740 ideapad_input_report(priv,
1741 data->integer.value | IDEAPAD_WMI_KEY);
1746 mutex_unlock(&ideapad_shared_mutex);
1749 static const struct ideapad_wmi_private ideapad_wmi_context_esc = {
1750 .event = IDEAPAD_WMI_EVENT_ESC
1753 static const struct ideapad_wmi_private ideapad_wmi_context_fn_keys = {
1754 .event = IDEAPAD_WMI_EVENT_FN_KEYS
1757 static const struct wmi_device_id ideapad_wmi_ids[] = {
1758 { "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", &ideapad_wmi_context_esc }, /* Yoga 3 */
1759 { "56322276-8493-4CE8-A783-98C991274F5E", &ideapad_wmi_context_esc }, /* Yoga 700 */
1760 { "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", &ideapad_wmi_context_fn_keys }, /* Legion 5 */
1763 MODULE_DEVICE_TABLE(wmi, ideapad_wmi_ids);
1765 static struct wmi_driver ideapad_wmi_driver = {
1767 .name = "ideapad_wmi",
1769 .id_table = ideapad_wmi_ids,
1770 .probe = ideapad_wmi_probe,
1771 .notify = ideapad_wmi_notify,
1774 static int ideapad_wmi_driver_register(void)
1776 return wmi_driver_register(&ideapad_wmi_driver);
1779 static void ideapad_wmi_driver_unregister(void)
1781 return wmi_driver_unregister(&ideapad_wmi_driver);
1785 static inline int ideapad_wmi_driver_register(void) { return 0; }
1786 static inline void ideapad_wmi_driver_unregister(void) { }
1792 static int ideapad_acpi_add(struct platform_device *pdev)
1794 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
1795 struct ideapad_private *priv;
1800 if (!adev || eval_int(adev->handle, "_CFG", &cfg))
1803 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1807 dev_set_drvdata(&pdev->dev, priv);
1811 priv->platform_device = pdev;
1813 ideapad_check_features(priv);
1815 err = ideapad_sysfs_init(priv);
1819 ideapad_debugfs_init(priv);
1821 err = ideapad_input_init(priv);
1825 err = ideapad_kbd_bl_init(priv);
1828 dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
1830 dev_info(&pdev->dev, "Keyboard backlight control not available\n");
1834 * On some models without a hw-switch (the yoga 2 13 at least)
1835 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1837 if (!priv->features.hw_rfkill_switch)
1838 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1840 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1841 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1842 ideapad_register_rfkill(priv, i);
1844 ideapad_sync_rfk_state(priv);
1845 ideapad_sync_touchpad_state(priv, false);
1847 err = ideapad_dytc_profile_init(priv);
1850 dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
1852 dev_info(&pdev->dev, "DYTC interface is not available\n");
1855 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1856 err = ideapad_backlight_init(priv);
1857 if (err && err != -ENODEV)
1858 goto backlight_failed;
1861 status = acpi_install_notify_handler(adev->handle,
1863 ideapad_acpi_notify, priv);
1864 if (ACPI_FAILURE(status)) {
1866 goto notification_failed;
1869 err = ideapad_shared_init(priv);
1871 goto shared_init_failed;
1876 acpi_remove_notify_handler(priv->adev->handle,
1878 ideapad_acpi_notify);
1880 notification_failed:
1881 ideapad_backlight_exit(priv);
1884 ideapad_dytc_profile_exit(priv);
1886 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1887 ideapad_unregister_rfkill(priv, i);
1889 ideapad_kbd_bl_exit(priv);
1890 ideapad_input_exit(priv);
1893 ideapad_debugfs_exit(priv);
1894 ideapad_sysfs_exit(priv);
1899 static void ideapad_acpi_remove(struct platform_device *pdev)
1901 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1904 ideapad_shared_exit(priv);
1906 acpi_remove_notify_handler(priv->adev->handle,
1908 ideapad_acpi_notify);
1910 ideapad_backlight_exit(priv);
1911 ideapad_dytc_profile_exit(priv);
1913 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1914 ideapad_unregister_rfkill(priv, i);
1916 ideapad_kbd_bl_exit(priv);
1917 ideapad_input_exit(priv);
1918 ideapad_debugfs_exit(priv);
1919 ideapad_sysfs_exit(priv);
1922 #ifdef CONFIG_PM_SLEEP
1923 static int ideapad_acpi_resume(struct device *dev)
1925 struct ideapad_private *priv = dev_get_drvdata(dev);
1927 ideapad_sync_rfk_state(priv);
1928 ideapad_sync_touchpad_state(priv, false);
1931 dytc_profile_refresh(priv);
1936 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1938 static const struct acpi_device_id ideapad_device_ids[] = {
1942 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1944 static struct platform_driver ideapad_acpi_driver = {
1945 .probe = ideapad_acpi_add,
1946 .remove_new = ideapad_acpi_remove,
1948 .name = "ideapad_acpi",
1950 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1954 static int __init ideapad_laptop_init(void)
1958 err = ideapad_wmi_driver_register();
1962 err = platform_driver_register(&ideapad_acpi_driver);
1964 ideapad_wmi_driver_unregister();
1970 module_init(ideapad_laptop_init)
1972 static void __exit ideapad_laptop_exit(void)
1974 ideapad_wmi_driver_unregister();
1975 platform_driver_unregister(&ideapad_acpi_driver);
1977 module_exit(ideapad_laptop_exit)
1979 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1980 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1981 MODULE_LICENSE("GPL");