1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
5 * Copyright © 2010 Intel Corporation
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/acpi.h>
12 #include <linux/backlight.h>
13 #include <linux/bitops.h>
14 #include <linux/bug.h>
15 #include <linux/debugfs.h>
16 #include <linux/device.h>
17 #include <linux/dmi.h>
19 #include <linux/i8042.h>
20 #include <linux/init.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/kernel.h>
24 #include <linux/leds.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/platform_profile.h>
28 #include <linux/rfkill.h>
29 #include <linux/seq_file.h>
30 #include <linux/sysfs.h>
31 #include <linux/types.h>
32 #include <linux/wmi.h>
33 #include "ideapad-laptop.h"
35 #include <acpi/video.h>
37 #include <dt-bindings/leds/common.h>
39 #define IDEAPAD_RFKILL_DEV_NUM 3
44 CFG_CAP_WIFI_BIT = 18,
48 * These are OnScreenDisplay support bits that can be useful to determine
49 * whether a hotkey exists/should show OSD. But they aren't particularly
50 * meaningful since they were introduced later, i.e. 2010 IdeaPads
51 * don't have these, but they still have had OSD for hotkeys.
53 CFG_OSD_NUMLK_BIT = 27,
54 CFG_OSD_CAPSLK_BIT = 28,
55 CFG_OSD_MICMUTE_BIT = 29,
56 CFG_OSD_TOUCHPAD_BIT = 30,
61 GBMD_CONSERVATION_STATE_BIT = 5,
65 SBMC_CONSERVATION_ON = 3,
66 SBMC_CONSERVATION_OFF = 5,
70 HALS_KBD_BL_SUPPORT_BIT = 4,
71 HALS_KBD_BL_STATE_BIT = 5,
72 HALS_USB_CHARGING_SUPPORT_BIT = 6,
73 HALS_USB_CHARGING_STATE_BIT = 7,
74 HALS_FNLOCK_SUPPORT_BIT = 9,
75 HALS_FNLOCK_STATE_BIT = 10,
76 HALS_HOTKEYS_PRIMARY_BIT = 11,
81 SALS_KBD_BL_OFF = 0x9,
82 SALS_USB_CHARGING_ON = 0xa,
83 SALS_USB_CHARGING_OFF = 0xb,
85 SALS_FNLOCK_OFF = 0xf,
88 struct ideapad_dytc_priv {
89 enum platform_profile_option current_profile;
90 struct platform_profile_handler pprof;
91 struct mutex mutex; /* protects the DYTC interface */
92 struct ideapad_private *priv;
95 struct ideapad_rfk_priv {
97 struct ideapad_private *priv;
100 struct ideapad_private {
101 struct acpi_device *adev;
102 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
103 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
104 struct platform_device *platform_device;
105 struct input_dev *inputdev;
106 struct backlight_device *blightdev;
107 struct ideapad_dytc_priv *dytc;
108 struct dentry *debug;
110 unsigned long r_touchpad_val;
112 bool conservation_mode : 1;
116 bool set_fn_lock_led : 1;
117 bool hw_rfkill_switch : 1;
119 bool touchpad_ctrl_via_ec : 1;
120 bool ctrl_ps2_aux_port : 1;
121 bool usb_charging : 1;
125 struct led_classdev led;
126 unsigned int last_brightness;
130 static bool no_bt_rfkill;
131 module_param(no_bt_rfkill, bool, 0444);
132 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
134 static bool allow_v4_dytc;
135 module_param(allow_v4_dytc, bool, 0444);
136 MODULE_PARM_DESC(allow_v4_dytc,
137 "Enable DYTC version 4 platform-profile support. "
140 static bool hw_rfkill_switch;
141 module_param(hw_rfkill_switch, bool, 0444);
142 MODULE_PARM_DESC(hw_rfkill_switch,
143 "Enable rfkill support for laptops with a hw on/off wifi switch/slider. "
146 static bool set_fn_lock_led;
147 module_param(set_fn_lock_led, bool, 0444);
148 MODULE_PARM_DESC(set_fn_lock_led,
149 "Enable driver based updates of the fn-lock LED on fn-lock changes. "
152 static bool ctrl_ps2_aux_port;
153 module_param(ctrl_ps2_aux_port, bool, 0444);
154 MODULE_PARM_DESC(ctrl_ps2_aux_port,
155 "Enable driver based PS/2 aux port en-/dis-abling on touchpad on/off toggle. "
158 static bool touchpad_ctrl_via_ec;
159 module_param(touchpad_ctrl_via_ec, bool, 0444);
160 MODULE_PARM_DESC(touchpad_ctrl_via_ec,
161 "Enable registering a 'touchpad' sysfs-attribute which can be used to manually "
162 "tell the EC to enable/disable the touchpad. This may not work on all models.");
168 static struct ideapad_private *ideapad_shared;
169 static DEFINE_MUTEX(ideapad_shared_mutex);
171 static int ideapad_shared_init(struct ideapad_private *priv)
175 mutex_lock(&ideapad_shared_mutex);
177 if (!ideapad_shared) {
178 ideapad_shared = priv;
181 dev_warn(&priv->adev->dev, "found multiple platform devices\n");
185 mutex_unlock(&ideapad_shared_mutex);
190 static void ideapad_shared_exit(struct ideapad_private *priv)
192 mutex_lock(&ideapad_shared_mutex);
194 if (ideapad_shared == priv)
195 ideapad_shared = NULL;
197 mutex_unlock(&ideapad_shared_mutex);
204 static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
206 unsigned long long result;
209 status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
210 if (ACPI_FAILURE(status))
218 static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
220 acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
222 return ACPI_FAILURE(status) ? -EIO : 0;
225 static int eval_gbmd(acpi_handle handle, unsigned long *res)
227 return eval_int(handle, "GBMD", res);
230 static int exec_sbmc(acpi_handle handle, unsigned long arg)
232 return exec_simple_method(handle, "SBMC", arg);
235 static int eval_hals(acpi_handle handle, unsigned long *res)
237 return eval_int(handle, "HALS", res);
240 static int exec_sals(acpi_handle handle, unsigned long arg)
242 return exec_simple_method(handle, "SALS", arg);
245 static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
247 return eval_int_with_arg(handle, "DYTC", cmd, res);
253 static int debugfs_status_show(struct seq_file *s, void *data)
255 struct ideapad_private *priv = s->private;
258 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
259 seq_printf(s, "Backlight max: %lu\n", value);
260 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
261 seq_printf(s, "Backlight now: %lu\n", value);
262 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
263 seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
265 seq_puts(s, "=====================\n");
267 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
268 seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
269 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
270 seq_printf(s, "Wifi status: %s (%lu)\n", value ? "on" : "off", value);
271 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
272 seq_printf(s, "BT status: %s (%lu)\n", value ? "on" : "off", value);
273 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
274 seq_printf(s, "3G status: %s (%lu)\n", value ? "on" : "off", value);
276 seq_puts(s, "=====================\n");
278 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
279 seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
280 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
281 seq_printf(s, "Camera status: %s (%lu)\n", value ? "on" : "off", value);
283 seq_puts(s, "=====================\n");
285 if (!eval_gbmd(priv->adev->handle, &value))
286 seq_printf(s, "GBMD: %#010lx\n", value);
287 if (!eval_hals(priv->adev->handle, &value))
288 seq_printf(s, "HALS: %#010lx\n", value);
292 DEFINE_SHOW_ATTRIBUTE(debugfs_status);
294 static int debugfs_cfg_show(struct seq_file *s, void *data)
296 struct ideapad_private *priv = s->private;
298 seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
300 seq_puts(s, "Capabilities:");
301 if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
302 seq_puts(s, " bluetooth");
303 if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
305 if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
306 seq_puts(s, " wifi");
307 if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
308 seq_puts(s, " camera");
311 seq_puts(s, "OSD support:");
312 if (test_bit(CFG_OSD_NUMLK_BIT, &priv->cfg))
313 seq_puts(s, " num-lock");
314 if (test_bit(CFG_OSD_CAPSLK_BIT, &priv->cfg))
315 seq_puts(s, " caps-lock");
316 if (test_bit(CFG_OSD_MICMUTE_BIT, &priv->cfg))
317 seq_puts(s, " mic-mute");
318 if (test_bit(CFG_OSD_TOUCHPAD_BIT, &priv->cfg))
319 seq_puts(s, " touchpad");
320 if (test_bit(CFG_OSD_CAM_BIT, &priv->cfg))
321 seq_puts(s, " camera");
324 seq_puts(s, "Graphics: ");
325 switch (priv->cfg & 0x700) {
327 seq_puts(s, "Intel");
333 seq_puts(s, "Nvidia");
336 seq_puts(s, "Intel and ATI");
339 seq_puts(s, "Intel and Nvidia");
346 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
348 static void ideapad_debugfs_init(struct ideapad_private *priv)
352 dir = debugfs_create_dir("ideapad", NULL);
355 debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
356 debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
359 static void ideapad_debugfs_exit(struct ideapad_private *priv)
361 debugfs_remove_recursive(priv->debug);
368 static ssize_t camera_power_show(struct device *dev,
369 struct device_attribute *attr,
372 struct ideapad_private *priv = dev_get_drvdata(dev);
373 unsigned long result;
376 err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
380 return sysfs_emit(buf, "%d\n", !!result);
383 static ssize_t camera_power_store(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
387 struct ideapad_private *priv = dev_get_drvdata(dev);
391 err = kstrtobool(buf, &state);
395 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
402 static DEVICE_ATTR_RW(camera_power);
404 static ssize_t conservation_mode_show(struct device *dev,
405 struct device_attribute *attr,
408 struct ideapad_private *priv = dev_get_drvdata(dev);
409 unsigned long result;
412 err = eval_gbmd(priv->adev->handle, &result);
416 return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
419 static ssize_t conservation_mode_store(struct device *dev,
420 struct device_attribute *attr,
421 const char *buf, size_t count)
423 struct ideapad_private *priv = dev_get_drvdata(dev);
427 err = kstrtobool(buf, &state);
431 err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
438 static DEVICE_ATTR_RW(conservation_mode);
440 static ssize_t fan_mode_show(struct device *dev,
441 struct device_attribute *attr,
444 struct ideapad_private *priv = dev_get_drvdata(dev);
445 unsigned long result;
448 err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
452 return sysfs_emit(buf, "%lu\n", result);
455 static ssize_t fan_mode_store(struct device *dev,
456 struct device_attribute *attr,
457 const char *buf, size_t count)
459 struct ideapad_private *priv = dev_get_drvdata(dev);
463 err = kstrtouint(buf, 0, &state);
467 if (state > 4 || state == 3)
470 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
477 static DEVICE_ATTR_RW(fan_mode);
479 static ssize_t fn_lock_show(struct device *dev,
480 struct device_attribute *attr,
483 struct ideapad_private *priv = dev_get_drvdata(dev);
487 err = eval_hals(priv->adev->handle, &hals);
491 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_FNLOCK_STATE_BIT, &hals));
494 static ssize_t fn_lock_store(struct device *dev,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
498 struct ideapad_private *priv = dev_get_drvdata(dev);
502 err = kstrtobool(buf, &state);
506 err = exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
513 static DEVICE_ATTR_RW(fn_lock);
515 static ssize_t touchpad_show(struct device *dev,
516 struct device_attribute *attr,
519 struct ideapad_private *priv = dev_get_drvdata(dev);
520 unsigned long result;
523 err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
527 priv->r_touchpad_val = result;
529 return sysfs_emit(buf, "%d\n", !!result);
532 static ssize_t touchpad_store(struct device *dev,
533 struct device_attribute *attr,
534 const char *buf, size_t count)
536 struct ideapad_private *priv = dev_get_drvdata(dev);
540 err = kstrtobool(buf, &state);
544 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
548 priv->r_touchpad_val = state;
553 static DEVICE_ATTR_RW(touchpad);
555 static ssize_t usb_charging_show(struct device *dev,
556 struct device_attribute *attr,
559 struct ideapad_private *priv = dev_get_drvdata(dev);
563 err = eval_hals(priv->adev->handle, &hals);
567 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
570 static ssize_t usb_charging_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
574 struct ideapad_private *priv = dev_get_drvdata(dev);
578 err = kstrtobool(buf, &state);
582 err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
589 static DEVICE_ATTR_RW(usb_charging);
591 static struct attribute *ideapad_attributes[] = {
592 &dev_attr_camera_power.attr,
593 &dev_attr_conservation_mode.attr,
594 &dev_attr_fan_mode.attr,
595 &dev_attr_fn_lock.attr,
596 &dev_attr_touchpad.attr,
597 &dev_attr_usb_charging.attr,
601 static umode_t ideapad_is_visible(struct kobject *kobj,
602 struct attribute *attr,
605 struct device *dev = kobj_to_dev(kobj);
606 struct ideapad_private *priv = dev_get_drvdata(dev);
607 bool supported = true;
609 if (attr == &dev_attr_camera_power.attr)
610 supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
611 else if (attr == &dev_attr_conservation_mode.attr)
612 supported = priv->features.conservation_mode;
613 else if (attr == &dev_attr_fan_mode.attr)
614 supported = priv->features.fan_mode;
615 else if (attr == &dev_attr_fn_lock.attr)
616 supported = priv->features.fn_lock;
617 else if (attr == &dev_attr_touchpad.attr)
618 supported = priv->features.touchpad_ctrl_via_ec;
619 else if (attr == &dev_attr_usb_charging.attr)
620 supported = priv->features.usb_charging;
622 return supported ? attr->mode : 0;
625 static const struct attribute_group ideapad_attribute_group = {
626 .is_visible = ideapad_is_visible,
627 .attrs = ideapad_attributes
631 * DYTC Platform profile
633 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
634 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
635 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
636 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
638 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
639 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
640 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
642 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
643 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
645 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
646 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
647 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
649 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
650 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
651 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
653 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
654 #define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
655 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
657 #define DYTC_SET_COMMAND(function, mode, on) \
658 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
659 (mode) << DYTC_SET_MODE_BIT | \
660 (on) << DYTC_SET_VALID_BIT)
662 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
664 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
666 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
669 case DYTC_MODE_LOW_POWER:
670 *profile = PLATFORM_PROFILE_LOW_POWER;
672 case DYTC_MODE_BALANCE:
673 *profile = PLATFORM_PROFILE_BALANCED;
675 case DYTC_MODE_PERFORM:
676 *profile = PLATFORM_PROFILE_PERFORMANCE;
678 default: /* Unknown mode */
685 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
688 case PLATFORM_PROFILE_LOW_POWER:
689 *perfmode = DYTC_MODE_LOW_POWER;
691 case PLATFORM_PROFILE_BALANCED:
692 *perfmode = DYTC_MODE_BALANCE;
694 case PLATFORM_PROFILE_PERFORMANCE:
695 *perfmode = DYTC_MODE_PERFORM;
697 default: /* Unknown profile */
705 * dytc_profile_get: Function to register with platform_profile
706 * handler. Returns current platform profile.
708 static int dytc_profile_get(struct platform_profile_handler *pprof,
709 enum platform_profile_option *profile)
711 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
713 *profile = dytc->current_profile;
718 * Helper function - check if we are in CQL mode and if we are
722 * If not in CQL mode, just run the command
724 static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
725 unsigned long *output)
727 int err, cmd_err, cur_funcmode;
729 /* Determine if we are in CQL mode. This alters the commands we do */
730 err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
734 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
735 /* Check if we're OK to return immediately */
736 if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
739 if (cur_funcmode == DYTC_FUNCTION_CQL) {
740 err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
745 cmd_err = eval_dytc(priv->adev->handle, cmd, output);
746 /* Check return condition after we've restored CQL state */
748 if (cur_funcmode == DYTC_FUNCTION_CQL) {
749 err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
758 * dytc_profile_set: Function to register with platform_profile
759 * handler. Sets current platform profile.
761 static int dytc_profile_set(struct platform_profile_handler *pprof,
762 enum platform_profile_option profile)
764 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
765 struct ideapad_private *priv = dytc->priv;
766 unsigned long output;
769 err = mutex_lock_interruptible(&dytc->mutex);
773 if (profile == PLATFORM_PROFILE_BALANCED) {
774 /* To get back to balanced mode we just issue a reset command */
775 err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
781 err = convert_profile_to_dytc(profile, &perfmode);
785 /* Determine if we are in CQL mode. This alters the commands we do */
786 err = dytc_cql_command(priv, DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
792 /* Success - update current profile */
793 dytc->current_profile = profile;
796 mutex_unlock(&dytc->mutex);
801 static void dytc_profile_refresh(struct ideapad_private *priv)
803 enum platform_profile_option profile;
804 unsigned long output;
807 mutex_lock(&priv->dytc->mutex);
808 err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
809 mutex_unlock(&priv->dytc->mutex);
813 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
815 if (convert_dytc_to_profile(perfmode, &profile))
818 if (profile != priv->dytc->current_profile) {
819 priv->dytc->current_profile = profile;
820 platform_profile_notify();
824 static const struct dmi_system_id ideapad_dytc_v4_allow_table[] = {
826 /* Ideapad 5 Pro 16ACH6 */
828 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
829 DMI_MATCH(DMI_PRODUCT_NAME, "82L5")
833 /* Ideapad 5 15ITL05 */
835 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
836 DMI_MATCH(DMI_PRODUCT_VERSION, "IdeaPad 5 15ITL05")
842 static int ideapad_dytc_profile_init(struct ideapad_private *priv)
844 int err, dytc_version;
845 unsigned long output;
847 if (!priv->features.dytc)
850 err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
851 /* For all other errors we can flag the failure */
855 /* Check DYTC is enabled and supports mode setting */
856 if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output)) {
857 dev_info(&priv->platform_device->dev, "DYTC_QUERY_ENABLE_BIT returned false\n");
861 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
863 if (dytc_version < 4) {
864 dev_info(&priv->platform_device->dev, "DYTC_VERSION < 4 is not supported\n");
868 if (dytc_version < 5 &&
869 !(allow_v4_dytc || dmi_check_system(ideapad_dytc_v4_allow_table))) {
870 dev_info(&priv->platform_device->dev,
871 "DYTC_VERSION 4 support may not work. Pass ideapad_laptop.allow_v4_dytc=Y on the kernel commandline to enable\n");
875 priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
879 mutex_init(&priv->dytc->mutex);
881 priv->dytc->priv = priv;
882 priv->dytc->pprof.profile_get = dytc_profile_get;
883 priv->dytc->pprof.profile_set = dytc_profile_set;
885 /* Setup supported modes */
886 set_bit(PLATFORM_PROFILE_LOW_POWER, priv->dytc->pprof.choices);
887 set_bit(PLATFORM_PROFILE_BALANCED, priv->dytc->pprof.choices);
888 set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->dytc->pprof.choices);
890 /* Create platform_profile structure and register */
891 err = platform_profile_register(&priv->dytc->pprof);
895 /* Ensure initial values are correct */
896 dytc_profile_refresh(priv);
901 mutex_destroy(&priv->dytc->mutex);
908 static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
913 platform_profile_remove();
914 mutex_destroy(&priv->dytc->mutex);
923 struct ideapad_rfk_data {
930 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
931 { "ideapad_wlan", CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
932 { "ideapad_bluetooth", CFG_CAP_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
933 { "ideapad_3g", CFG_CAP_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
936 static int ideapad_rfk_set(void *data, bool blocked)
938 struct ideapad_rfk_priv *priv = data;
939 int opcode = ideapad_rfk_data[priv->dev].opcode;
941 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
944 static const struct rfkill_ops ideapad_rfk_ops = {
945 .set_block = ideapad_rfk_set,
948 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
950 unsigned long hw_blocked = 0;
953 if (priv->features.hw_rfkill_switch) {
954 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
956 hw_blocked = !hw_blocked;
959 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
961 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
964 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
966 unsigned long rf_enabled;
969 if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
970 /* Force to enable bluetooth when no_bt_rfkill=1 */
971 write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
975 priv->rfk_priv[dev].dev = dev;
976 priv->rfk_priv[dev].priv = priv;
978 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
979 &priv->platform_device->dev,
980 ideapad_rfk_data[dev].type,
982 &priv->rfk_priv[dev]);
986 err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
990 rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
992 err = rfkill_register(priv->rfk[dev]);
994 rfkill_destroy(priv->rfk[dev]);
999 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1001 if (!priv->rfk[dev])
1004 rfkill_unregister(priv->rfk[dev]);
1005 rfkill_destroy(priv->rfk[dev]);
1011 static int ideapad_sysfs_init(struct ideapad_private *priv)
1013 return device_add_group(&priv->platform_device->dev,
1014 &ideapad_attribute_group);
1017 static void ideapad_sysfs_exit(struct ideapad_private *priv)
1019 device_remove_group(&priv->platform_device->dev,
1020 &ideapad_attribute_group);
1026 #define IDEAPAD_WMI_KEY 0x100
1028 static const struct key_entry ideapad_keymap[] = {
1029 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
1030 { KE_KEY, 7, { KEY_CAMERA } },
1031 { KE_KEY, 8, { KEY_MICMUTE } },
1032 { KE_KEY, 11, { KEY_F16 } },
1033 { KE_KEY, 13, { KEY_WLAN } },
1034 { KE_KEY, 16, { KEY_PROG1 } },
1035 { KE_KEY, 17, { KEY_PROG2 } },
1036 { KE_KEY, 64, { KEY_PROG3 } },
1037 { KE_KEY, 65, { KEY_PROG4 } },
1038 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
1039 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
1040 { KE_KEY, 128, { KEY_ESC } },
1046 /* FnLock (handled by the firmware) */
1047 { KE_IGNORE, 0x02 | IDEAPAD_WMI_KEY },
1048 /* Esc (handled by the firmware) */
1049 { KE_IGNORE, 0x03 | IDEAPAD_WMI_KEY },
1050 /* Customizable Lenovo Hotkey ("star" with 'S' inside) */
1051 { KE_KEY, 0x01 | IDEAPAD_WMI_KEY, { KEY_FAVORITES } },
1052 /* Dark mode toggle */
1053 { KE_KEY, 0x13 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
1054 /* Sound profile switch */
1055 { KE_KEY, 0x12 | IDEAPAD_WMI_KEY, { KEY_PROG2 } },
1056 /* Lenovo Virtual Background application */
1057 { KE_KEY, 0x28 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
1058 /* Lenovo Support */
1059 { KE_KEY, 0x27 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1060 /* Refresh Rate Toggle */
1061 { KE_KEY, 0x0a | IDEAPAD_WMI_KEY, { KEY_DISPLAYTOGGLE } },
1066 static int ideapad_input_init(struct ideapad_private *priv)
1068 struct input_dev *inputdev;
1071 inputdev = input_allocate_device();
1075 inputdev->name = "Ideapad extra buttons";
1076 inputdev->phys = "ideapad/input0";
1077 inputdev->id.bustype = BUS_HOST;
1078 inputdev->dev.parent = &priv->platform_device->dev;
1080 err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1082 dev_err(&priv->platform_device->dev,
1083 "Could not set up input device keymap: %d\n", err);
1087 err = input_register_device(inputdev);
1089 dev_err(&priv->platform_device->dev,
1090 "Could not register input device: %d\n", err);
1094 priv->inputdev = inputdev;
1099 input_free_device(inputdev);
1104 static void ideapad_input_exit(struct ideapad_private *priv)
1106 input_unregister_device(priv->inputdev);
1107 priv->inputdev = NULL;
1110 static void ideapad_input_report(struct ideapad_private *priv,
1111 unsigned long scancode)
1113 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1116 static void ideapad_input_novokey(struct ideapad_private *priv)
1118 unsigned long long_pressed;
1120 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1124 ideapad_input_report(priv, 17);
1126 ideapad_input_report(priv, 16);
1129 static void ideapad_check_special_buttons(struct ideapad_private *priv)
1131 unsigned long bit, value;
1133 if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1136 for_each_set_bit (bit, &value, 16) {
1140 /* Thermal Management button */
1141 ideapad_input_report(priv, 65);
1144 /* OneKey Theater button */
1145 ideapad_input_report(priv, 64);
1148 dev_info(&priv->platform_device->dev,
1149 "Unknown special button: %lu\n", bit);
1158 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1160 struct ideapad_private *priv = bl_get_data(blightdev);
1164 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1171 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1173 struct ideapad_private *priv = bl_get_data(blightdev);
1176 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1177 blightdev->props.brightness);
1181 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1182 blightdev->props.power != FB_BLANK_POWERDOWN);
1189 static const struct backlight_ops ideapad_backlight_ops = {
1190 .get_brightness = ideapad_backlight_get_brightness,
1191 .update_status = ideapad_backlight_update_status,
1194 static int ideapad_backlight_init(struct ideapad_private *priv)
1196 struct backlight_device *blightdev;
1197 struct backlight_properties props;
1198 unsigned long max, now, power;
1201 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1205 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1209 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1213 memset(&props, 0, sizeof(props));
1215 props.max_brightness = max;
1216 props.type = BACKLIGHT_PLATFORM;
1218 blightdev = backlight_device_register("ideapad",
1219 &priv->platform_device->dev,
1221 &ideapad_backlight_ops,
1223 if (IS_ERR(blightdev)) {
1224 err = PTR_ERR(blightdev);
1225 dev_err(&priv->platform_device->dev,
1226 "Could not register backlight device: %d\n", err);
1230 priv->blightdev = blightdev;
1231 blightdev->props.brightness = now;
1232 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1234 backlight_update_status(blightdev);
1239 static void ideapad_backlight_exit(struct ideapad_private *priv)
1241 backlight_device_unregister(priv->blightdev);
1242 priv->blightdev = NULL;
1245 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1247 struct backlight_device *blightdev = priv->blightdev;
1248 unsigned long power;
1253 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1256 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1259 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1263 /* if we control brightness via acpi video driver */
1264 if (!priv->blightdev)
1265 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1267 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1271 * keyboard backlight
1273 static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1278 err = eval_hals(priv->adev->handle, &hals);
1282 return !!test_bit(HALS_KBD_BL_STATE_BIT, &hals);
1285 static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1287 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1289 return ideapad_kbd_bl_brightness_get(priv);
1292 static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1294 int err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1299 priv->kbd_bl.last_brightness = brightness;
1304 static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1305 enum led_brightness brightness)
1307 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1309 return ideapad_kbd_bl_brightness_set(priv, brightness);
1312 static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1316 if (!priv->kbd_bl.initialized)
1319 brightness = ideapad_kbd_bl_brightness_get(priv);
1323 if (brightness == priv->kbd_bl.last_brightness)
1326 priv->kbd_bl.last_brightness = brightness;
1328 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1331 static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1333 int brightness, err;
1335 if (!priv->features.kbd_bl)
1338 if (WARN_ON(priv->kbd_bl.initialized))
1341 brightness = ideapad_kbd_bl_brightness_get(priv);
1345 priv->kbd_bl.last_brightness = brightness;
1347 priv->kbd_bl.led.name = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1348 priv->kbd_bl.led.max_brightness = 1;
1349 priv->kbd_bl.led.brightness_get = ideapad_kbd_bl_led_cdev_brightness_get;
1350 priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1351 priv->kbd_bl.led.flags = LED_BRIGHT_HW_CHANGED;
1353 err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1357 priv->kbd_bl.initialized = true;
1362 static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1364 if (!priv->kbd_bl.initialized)
1367 priv->kbd_bl.initialized = false;
1369 led_classdev_unregister(&priv->kbd_bl.led);
1375 static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_events)
1377 unsigned long value;
1378 unsigned char param;
1381 /* Without reading from EC touchpad LED doesn't switch state */
1382 ret = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value);
1387 * Some IdeaPads don't really turn off touchpad - they only
1388 * switch the LED state. We (de)activate KBC AUX port to turn
1389 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1390 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1392 if (priv->features.ctrl_ps2_aux_port)
1393 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1396 * On older models the EC controls the touchpad and toggles it on/off
1397 * itself, in this case we report KEY_TOUCHPAD_ON/_OFF. Some models do
1398 * an acpi-notify with VPC bit 5 set on resume, so this function get
1399 * called with send_events=true on every resume. Therefor if the EC did
1400 * not toggle, do nothing to avoid sending spurious KEY_TOUCHPAD_TOGGLE.
1402 if (send_events && value != priv->r_touchpad_val) {
1403 ideapad_input_report(priv, value ? 67 : 66);
1404 sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1407 priv->r_touchpad_val = value;
1410 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1412 struct ideapad_private *priv = data;
1413 unsigned long vpc1, vpc2, bit;
1415 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1418 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1421 vpc1 = (vpc2 << 8) | vpc1;
1423 for_each_set_bit (bit, &vpc1, 16) {
1430 ideapad_input_report(priv, bit);
1434 * This event gets send on a Yoga 300-11IBR when the EC
1435 * believes that the device has changed between laptop/
1436 * tent/stand/tablet mode. The EC relies on getting
1437 * angle info from 2 accelerometers through a special
1438 * windows service calling a DSM on the DUAL250E ACPI-
1439 * device. Linux does not do this, making the laptop/
1440 * tent/stand/tablet mode info unreliable, so we simply
1441 * ignore these events.
1445 ideapad_sync_rfk_state(priv);
1448 ideapad_sync_touchpad_state(priv, true);
1451 ideapad_backlight_notify_brightness(priv);
1454 ideapad_input_novokey(priv);
1457 ideapad_backlight_notify_power(priv);
1461 * Some IdeaPads report event 1 every ~20
1462 * seconds while on battery power; some
1463 * report this when changing to/from tablet
1464 * mode; some report this when the keyboard
1465 * backlight has changed.
1467 ideapad_kbd_bl_notify(priv);
1470 ideapad_check_special_buttons(priv);
1473 dev_info(&priv->platform_device->dev,
1474 "Unknown event: %lu\n", bit);
1479 /* On some models we need to call exec_sals(SALS_FNLOCK_ON/OFF) to set the LED */
1480 static const struct dmi_system_id set_fn_lock_led_list[] = {
1482 /* https://bugzilla.kernel.org/show_bug.cgi?id=212671 */
1484 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1485 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion R7000P2020H"),
1490 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1491 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion 5 15ARH05"),
1498 * Some ideapads have a hardware rfkill switch, but most do not have one.
1499 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1500 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1501 * There used to be a long list of DMI ids for models without a hw rfkill
1502 * switch here, but that resulted in playing whack a mole.
1503 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1504 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1505 * hw-blocked results in an empty SSID list, which is a much more benign
1507 * So the default now is the much safer option of assuming there is no
1508 * hardware rfkill switch. This default also actually matches most hardware,
1509 * since having a hw rfkill switch is quite rare on modern hardware, so this
1510 * also leads to a much shorter list.
1512 static const struct dmi_system_id hw_rfkill_list[] = {
1517 * On some models the EC toggles the touchpad muted LED on touchpad toggle
1518 * hotkey presses, but the EC does not actually disable the touchpad itself.
1519 * On these models the driver needs to explicitly enable/disable the i8042
1522 static const struct dmi_system_id ctrl_ps2_aux_port_list[] = {
1524 /* Lenovo Ideapad Z570 */
1526 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1527 DMI_MATCH(DMI_PRODUCT_VERSION, "Ideapad Z570"),
1533 static void ideapad_check_features(struct ideapad_private *priv)
1535 acpi_handle handle = priv->adev->handle;
1538 priv->features.set_fn_lock_led =
1539 set_fn_lock_led || dmi_check_system(set_fn_lock_led_list);
1540 priv->features.hw_rfkill_switch =
1541 hw_rfkill_switch || dmi_check_system(hw_rfkill_list);
1542 priv->features.ctrl_ps2_aux_port =
1543 ctrl_ps2_aux_port || dmi_check_system(ctrl_ps2_aux_port_list);
1544 priv->features.touchpad_ctrl_via_ec = touchpad_ctrl_via_ec;
1546 if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
1547 priv->features.fan_mode = true;
1549 if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
1550 priv->features.conservation_mode = true;
1552 if (acpi_has_method(handle, "DYTC"))
1553 priv->features.dytc = true;
1555 if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
1556 if (!eval_hals(handle, &val)) {
1557 if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
1558 priv->features.fn_lock = true;
1560 if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val))
1561 priv->features.kbd_bl = true;
1563 if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
1564 priv->features.usb_charging = true;
1569 #if IS_ENABLED(CONFIG_ACPI_WMI)
1573 enum ideapad_wmi_event_type {
1574 IDEAPAD_WMI_EVENT_ESC,
1575 IDEAPAD_WMI_EVENT_FN_KEYS,
1578 struct ideapad_wmi_private {
1579 enum ideapad_wmi_event_type event;
1582 static int ideapad_wmi_probe(struct wmi_device *wdev, const void *context)
1584 struct ideapad_wmi_private *wpriv;
1586 wpriv = devm_kzalloc(&wdev->dev, sizeof(*wpriv), GFP_KERNEL);
1590 *wpriv = *(const struct ideapad_wmi_private *)context;
1592 dev_set_drvdata(&wdev->dev, wpriv);
1596 static void ideapad_wmi_notify(struct wmi_device *wdev, union acpi_object *data)
1598 struct ideapad_wmi_private *wpriv = dev_get_drvdata(&wdev->dev);
1599 struct ideapad_private *priv;
1600 unsigned long result;
1602 mutex_lock(&ideapad_shared_mutex);
1604 priv = ideapad_shared;
1608 switch (wpriv->event) {
1609 case IDEAPAD_WMI_EVENT_ESC:
1610 ideapad_input_report(priv, 128);
1612 case IDEAPAD_WMI_EVENT_FN_KEYS:
1613 if (priv->features.set_fn_lock_led &&
1614 !eval_hals(priv->adev->handle, &result)) {
1615 bool state = test_bit(HALS_FNLOCK_STATE_BIT, &result);
1617 exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
1620 if (data->type != ACPI_TYPE_INTEGER) {
1621 dev_warn(&wdev->dev,
1622 "WMI event data is not an integer\n");
1626 dev_dbg(&wdev->dev, "WMI fn-key event: 0x%llx\n",
1627 data->integer.value);
1629 ideapad_input_report(priv,
1630 data->integer.value | IDEAPAD_WMI_KEY);
1635 mutex_unlock(&ideapad_shared_mutex);
1638 static const struct ideapad_wmi_private ideapad_wmi_context_esc = {
1639 .event = IDEAPAD_WMI_EVENT_ESC
1642 static const struct ideapad_wmi_private ideapad_wmi_context_fn_keys = {
1643 .event = IDEAPAD_WMI_EVENT_FN_KEYS
1646 static const struct wmi_device_id ideapad_wmi_ids[] = {
1647 { "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", &ideapad_wmi_context_esc }, /* Yoga 3 */
1648 { "56322276-8493-4CE8-A783-98C991274F5E", &ideapad_wmi_context_esc }, /* Yoga 700 */
1649 { "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", &ideapad_wmi_context_fn_keys }, /* Legion 5 */
1652 MODULE_DEVICE_TABLE(wmi, ideapad_wmi_ids);
1654 static struct wmi_driver ideapad_wmi_driver = {
1656 .name = "ideapad_wmi",
1658 .id_table = ideapad_wmi_ids,
1659 .probe = ideapad_wmi_probe,
1660 .notify = ideapad_wmi_notify,
1663 static int ideapad_wmi_driver_register(void)
1665 return wmi_driver_register(&ideapad_wmi_driver);
1668 static void ideapad_wmi_driver_unregister(void)
1670 return wmi_driver_unregister(&ideapad_wmi_driver);
1674 static inline int ideapad_wmi_driver_register(void) { return 0; }
1675 static inline void ideapad_wmi_driver_unregister(void) { }
1681 static int ideapad_acpi_add(struct platform_device *pdev)
1683 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
1684 struct ideapad_private *priv;
1689 if (!adev || eval_int(adev->handle, "_CFG", &cfg))
1692 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1696 dev_set_drvdata(&pdev->dev, priv);
1700 priv->platform_device = pdev;
1702 ideapad_check_features(priv);
1704 err = ideapad_sysfs_init(priv);
1708 ideapad_debugfs_init(priv);
1710 err = ideapad_input_init(priv);
1714 err = ideapad_kbd_bl_init(priv);
1717 dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
1719 dev_info(&pdev->dev, "Keyboard backlight control not available\n");
1723 * On some models without a hw-switch (the yoga 2 13 at least)
1724 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1726 if (!priv->features.hw_rfkill_switch)
1727 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1729 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1730 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1731 ideapad_register_rfkill(priv, i);
1733 ideapad_sync_rfk_state(priv);
1734 ideapad_sync_touchpad_state(priv, false);
1736 err = ideapad_dytc_profile_init(priv);
1739 dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
1741 dev_info(&pdev->dev, "DYTC interface is not available\n");
1744 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1745 err = ideapad_backlight_init(priv);
1746 if (err && err != -ENODEV)
1747 goto backlight_failed;
1750 status = acpi_install_notify_handler(adev->handle,
1752 ideapad_acpi_notify, priv);
1753 if (ACPI_FAILURE(status)) {
1755 goto notification_failed;
1758 err = ideapad_shared_init(priv);
1760 goto shared_init_failed;
1765 acpi_remove_notify_handler(priv->adev->handle,
1767 ideapad_acpi_notify);
1769 notification_failed:
1770 ideapad_backlight_exit(priv);
1773 ideapad_dytc_profile_exit(priv);
1775 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1776 ideapad_unregister_rfkill(priv, i);
1778 ideapad_kbd_bl_exit(priv);
1779 ideapad_input_exit(priv);
1782 ideapad_debugfs_exit(priv);
1783 ideapad_sysfs_exit(priv);
1788 static void ideapad_acpi_remove(struct platform_device *pdev)
1790 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1793 ideapad_shared_exit(priv);
1795 acpi_remove_notify_handler(priv->adev->handle,
1797 ideapad_acpi_notify);
1799 ideapad_backlight_exit(priv);
1800 ideapad_dytc_profile_exit(priv);
1802 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1803 ideapad_unregister_rfkill(priv, i);
1805 ideapad_kbd_bl_exit(priv);
1806 ideapad_input_exit(priv);
1807 ideapad_debugfs_exit(priv);
1808 ideapad_sysfs_exit(priv);
1811 #ifdef CONFIG_PM_SLEEP
1812 static int ideapad_acpi_resume(struct device *dev)
1814 struct ideapad_private *priv = dev_get_drvdata(dev);
1816 ideapad_sync_rfk_state(priv);
1817 ideapad_sync_touchpad_state(priv, false);
1820 dytc_profile_refresh(priv);
1825 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1827 static const struct acpi_device_id ideapad_device_ids[] = {
1831 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1833 static struct platform_driver ideapad_acpi_driver = {
1834 .probe = ideapad_acpi_add,
1835 .remove_new = ideapad_acpi_remove,
1837 .name = "ideapad_acpi",
1839 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1843 static int __init ideapad_laptop_init(void)
1847 err = ideapad_wmi_driver_register();
1851 err = platform_driver_register(&ideapad_acpi_driver);
1853 ideapad_wmi_driver_unregister();
1859 module_init(ideapad_laptop_init)
1861 static void __exit ideapad_laptop_exit(void)
1863 ideapad_wmi_driver_unregister();
1864 platform_driver_unregister(&ideapad_acpi_driver);
1866 module_exit(ideapad_laptop_exit)
1869 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1870 MODULE_LICENSE("GPL");