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/bitops.h>
14 #include <linux/bug.h>
15 #include <linux/debugfs.h>
16 #include <linux/device.h>
17 #include <linux/dmi.h>
19 #include <linux/i8042.h>
20 #include <linux/init.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/leds.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/platform_profile.h>
29 #include <linux/rfkill.h>
30 #include <linux/seq_file.h>
31 #include <linux/sysfs.h>
32 #include <linux/types.h>
34 #include <acpi/video.h>
36 #include <dt-bindings/leds/common.h>
38 #define IDEAPAD_RFKILL_DEV_NUM 3
40 #if IS_ENABLED(CONFIG_ACPI_WMI)
41 static const char *const ideapad_wmi_fnesc_events[] = {
42 "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
43 "56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
44 "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", /* Legion 5 */
51 CFG_CAP_WIFI_BIT = 18,
53 CFG_CAP_TOUCHPAD_BIT = 30,
57 GBMD_CONSERVATION_STATE_BIT = 5,
61 SBMC_CONSERVATION_ON = 3,
62 SBMC_CONSERVATION_OFF = 5,
66 HALS_KBD_BL_SUPPORT_BIT = 4,
67 HALS_KBD_BL_STATE_BIT = 5,
68 HALS_USB_CHARGING_SUPPORT_BIT = 6,
69 HALS_USB_CHARGING_STATE_BIT = 7,
70 HALS_FNLOCK_SUPPORT_BIT = 9,
71 HALS_FNLOCK_STATE_BIT = 10,
72 HALS_HOTKEYS_PRIMARY_BIT = 11,
77 SALS_KBD_BL_OFF = 0x9,
78 SALS_USB_CHARGING_ON = 0xa,
79 SALS_USB_CHARGING_OFF = 0xb,
81 SALS_FNLOCK_OFF = 0xf,
102 VPCCMD_R_ODD, /* 0x21 */
107 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
108 VPCCMD_W_BL_POWER = 0x33,
111 struct ideapad_dytc_priv {
112 enum platform_profile_option current_profile;
113 struct platform_profile_handler pprof;
114 struct mutex mutex; /* protects the DYTC interface */
115 struct ideapad_private *priv;
118 struct ideapad_rfk_priv {
120 struct ideapad_private *priv;
123 struct ideapad_private {
124 struct acpi_device *adev;
125 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
126 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
127 struct platform_device *platform_device;
128 struct input_dev *inputdev;
129 struct backlight_device *blightdev;
130 struct ideapad_dytc_priv *dytc;
131 struct dentry *debug;
133 const char *fnesc_guid;
135 bool conservation_mode : 1;
139 bool hw_rfkill_switch : 1;
141 bool touchpad_ctrl_via_ec : 1;
142 bool usb_charging : 1;
146 struct led_classdev led;
147 unsigned int last_brightness;
151 static bool no_bt_rfkill;
152 module_param(no_bt_rfkill, bool, 0444);
153 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
158 #define IDEAPAD_EC_TIMEOUT 200 /* in ms */
160 static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
162 unsigned long long result;
165 status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
166 if (ACPI_FAILURE(status))
174 static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
176 acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
178 return ACPI_FAILURE(status) ? -EIO : 0;
181 static int eval_gbmd(acpi_handle handle, unsigned long *res)
183 return eval_int(handle, "GBMD", res);
186 static int exec_sbmc(acpi_handle handle, unsigned long arg)
188 return exec_simple_method(handle, "SBMC", arg);
191 static int eval_hals(acpi_handle handle, unsigned long *res)
193 return eval_int(handle, "HALS", res);
196 static int exec_sals(acpi_handle handle, unsigned long arg)
198 return exec_simple_method(handle, "SALS", arg);
201 static int eval_int_with_arg(acpi_handle handle, const char *name, unsigned long arg, unsigned long *res)
203 struct acpi_object_list params;
204 unsigned long long result;
205 union acpi_object in_obj;
209 params.pointer = &in_obj;
210 in_obj.type = ACPI_TYPE_INTEGER;
211 in_obj.integer.value = arg;
213 status = acpi_evaluate_integer(handle, (char *)name, ¶ms, &result);
214 if (ACPI_FAILURE(status))
223 static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
225 return eval_int_with_arg(handle, "DYTC", cmd, res);
228 static int eval_vpcr(acpi_handle handle, unsigned long cmd, unsigned long *res)
230 return eval_int_with_arg(handle, "VPCR", cmd, res);
233 static int eval_vpcw(acpi_handle handle, unsigned long cmd, unsigned long data)
235 struct acpi_object_list params;
236 union acpi_object in_obj[2];
240 params.pointer = in_obj;
241 in_obj[0].type = ACPI_TYPE_INTEGER;
242 in_obj[0].integer.value = cmd;
243 in_obj[1].type = ACPI_TYPE_INTEGER;
244 in_obj[1].integer.value = data;
246 status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL);
247 if (ACPI_FAILURE(status))
253 static int read_ec_data(acpi_handle handle, unsigned long cmd, unsigned long *data)
255 unsigned long end_jiffies, val;
258 err = eval_vpcw(handle, 1, cmd);
262 end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
264 while (time_before(jiffies, end_jiffies)) {
267 err = eval_vpcr(handle, 1, &val);
272 return eval_vpcr(handle, 0, data);
275 acpi_handle_err(handle, "timeout in %s\n", __func__);
280 static int write_ec_cmd(acpi_handle handle, unsigned long cmd, unsigned long data)
282 unsigned long end_jiffies, val;
285 err = eval_vpcw(handle, 0, data);
289 err = eval_vpcw(handle, 1, cmd);
293 end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
295 while (time_before(jiffies, end_jiffies)) {
298 err = eval_vpcr(handle, 1, &val);
306 acpi_handle_err(handle, "timeout in %s\n", __func__);
314 static int debugfs_status_show(struct seq_file *s, void *data)
316 struct ideapad_private *priv = s->private;
319 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
320 seq_printf(s, "Backlight max: %lu\n", value);
321 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
322 seq_printf(s, "Backlight now: %lu\n", value);
323 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
324 seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
326 seq_puts(s, "=====================\n");
328 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
329 seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
330 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
331 seq_printf(s, "Wifi status: %s (%lu)\n", value ? "on" : "off", value);
332 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
333 seq_printf(s, "BT status: %s (%lu)\n", value ? "on" : "off", value);
334 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
335 seq_printf(s, "3G status: %s (%lu)\n", value ? "on" : "off", value);
337 seq_puts(s, "=====================\n");
339 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
340 seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
341 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
342 seq_printf(s, "Camera status: %s (%lu)\n", value ? "on" : "off", value);
344 seq_puts(s, "=====================\n");
346 if (!eval_gbmd(priv->adev->handle, &value))
347 seq_printf(s, "GBMD: %#010lx\n", value);
348 if (!eval_hals(priv->adev->handle, &value))
349 seq_printf(s, "HALS: %#010lx\n", value);
353 DEFINE_SHOW_ATTRIBUTE(debugfs_status);
355 static int debugfs_cfg_show(struct seq_file *s, void *data)
357 struct ideapad_private *priv = s->private;
359 seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
361 seq_puts(s, "Capabilities:");
362 if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
363 seq_puts(s, " bluetooth");
364 if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
366 if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
367 seq_puts(s, " wifi");
368 if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
369 seq_puts(s, " camera");
370 if (test_bit(CFG_CAP_TOUCHPAD_BIT, &priv->cfg))
371 seq_puts(s, " touchpad");
374 seq_puts(s, "Graphics: ");
375 switch (priv->cfg & 0x700) {
377 seq_puts(s, "Intel");
383 seq_puts(s, "Nvidia");
386 seq_puts(s, "Intel and ATI");
389 seq_puts(s, "Intel and Nvidia");
396 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
398 static void ideapad_debugfs_init(struct ideapad_private *priv)
402 dir = debugfs_create_dir("ideapad", NULL);
405 debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
406 debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
409 static void ideapad_debugfs_exit(struct ideapad_private *priv)
411 debugfs_remove_recursive(priv->debug);
418 static ssize_t camera_power_show(struct device *dev,
419 struct device_attribute *attr,
422 struct ideapad_private *priv = dev_get_drvdata(dev);
423 unsigned long result;
426 err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
430 return sysfs_emit(buf, "%d\n", !!result);
433 static ssize_t camera_power_store(struct device *dev,
434 struct device_attribute *attr,
435 const char *buf, size_t count)
437 struct ideapad_private *priv = dev_get_drvdata(dev);
441 err = kstrtobool(buf, &state);
445 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
452 static DEVICE_ATTR_RW(camera_power);
454 static ssize_t conservation_mode_show(struct device *dev,
455 struct device_attribute *attr,
458 struct ideapad_private *priv = dev_get_drvdata(dev);
459 unsigned long result;
462 err = eval_gbmd(priv->adev->handle, &result);
466 return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
469 static ssize_t conservation_mode_store(struct device *dev,
470 struct device_attribute *attr,
471 const char *buf, size_t count)
473 struct ideapad_private *priv = dev_get_drvdata(dev);
477 err = kstrtobool(buf, &state);
481 err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
488 static DEVICE_ATTR_RW(conservation_mode);
490 static ssize_t fan_mode_show(struct device *dev,
491 struct device_attribute *attr,
494 struct ideapad_private *priv = dev_get_drvdata(dev);
495 unsigned long result;
498 err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
502 return sysfs_emit(buf, "%lu\n", result);
505 static ssize_t fan_mode_store(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
509 struct ideapad_private *priv = dev_get_drvdata(dev);
513 err = kstrtouint(buf, 0, &state);
517 if (state > 4 || state == 3)
520 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
527 static DEVICE_ATTR_RW(fan_mode);
529 static ssize_t fn_lock_show(struct device *dev,
530 struct device_attribute *attr,
533 struct ideapad_private *priv = dev_get_drvdata(dev);
537 err = eval_hals(priv->adev->handle, &hals);
541 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_FNLOCK_STATE_BIT, &hals));
544 static ssize_t fn_lock_store(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf, size_t count)
548 struct ideapad_private *priv = dev_get_drvdata(dev);
552 err = kstrtobool(buf, &state);
556 err = exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
563 static DEVICE_ATTR_RW(fn_lock);
565 static ssize_t touchpad_show(struct device *dev,
566 struct device_attribute *attr,
569 struct ideapad_private *priv = dev_get_drvdata(dev);
570 unsigned long result;
573 err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
577 return sysfs_emit(buf, "%d\n", !!result);
580 static ssize_t touchpad_store(struct device *dev,
581 struct device_attribute *attr,
582 const char *buf, size_t count)
584 struct ideapad_private *priv = dev_get_drvdata(dev);
588 err = kstrtobool(buf, &state);
592 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
599 static DEVICE_ATTR_RW(touchpad);
601 static ssize_t usb_charging_show(struct device *dev,
602 struct device_attribute *attr,
605 struct ideapad_private *priv = dev_get_drvdata(dev);
609 err = eval_hals(priv->adev->handle, &hals);
613 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
616 static ssize_t usb_charging_store(struct device *dev,
617 struct device_attribute *attr,
618 const char *buf, size_t count)
620 struct ideapad_private *priv = dev_get_drvdata(dev);
624 err = kstrtobool(buf, &state);
628 err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
635 static DEVICE_ATTR_RW(usb_charging);
637 static struct attribute *ideapad_attributes[] = {
638 &dev_attr_camera_power.attr,
639 &dev_attr_conservation_mode.attr,
640 &dev_attr_fan_mode.attr,
641 &dev_attr_fn_lock.attr,
642 &dev_attr_touchpad.attr,
643 &dev_attr_usb_charging.attr,
647 static umode_t ideapad_is_visible(struct kobject *kobj,
648 struct attribute *attr,
651 struct device *dev = kobj_to_dev(kobj);
652 struct ideapad_private *priv = dev_get_drvdata(dev);
653 bool supported = true;
655 if (attr == &dev_attr_camera_power.attr)
656 supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
657 else if (attr == &dev_attr_conservation_mode.attr)
658 supported = priv->features.conservation_mode;
659 else if (attr == &dev_attr_fan_mode.attr)
660 supported = priv->features.fan_mode;
661 else if (attr == &dev_attr_fn_lock.attr)
662 supported = priv->features.fn_lock;
663 else if (attr == &dev_attr_touchpad.attr)
664 supported = priv->features.touchpad_ctrl_via_ec &&
665 test_bit(CFG_CAP_TOUCHPAD_BIT, &priv->cfg);
666 else if (attr == &dev_attr_usb_charging.attr)
667 supported = priv->features.usb_charging;
669 return supported ? attr->mode : 0;
672 static const struct attribute_group ideapad_attribute_group = {
673 .is_visible = ideapad_is_visible,
674 .attrs = ideapad_attributes
678 * DYTC Platform profile
680 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
681 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
682 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
683 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
685 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
686 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
687 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
689 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
690 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
692 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
693 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
694 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
696 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
697 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
698 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
700 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
701 #define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
702 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
704 #define DYTC_SET_COMMAND(function, mode, on) \
705 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
706 (mode) << DYTC_SET_MODE_BIT | \
707 (on) << DYTC_SET_VALID_BIT)
709 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
711 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
713 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
716 case DYTC_MODE_LOW_POWER:
717 *profile = PLATFORM_PROFILE_LOW_POWER;
719 case DYTC_MODE_BALANCE:
720 *profile = PLATFORM_PROFILE_BALANCED;
722 case DYTC_MODE_PERFORM:
723 *profile = PLATFORM_PROFILE_PERFORMANCE;
725 default: /* Unknown mode */
732 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
735 case PLATFORM_PROFILE_LOW_POWER:
736 *perfmode = DYTC_MODE_LOW_POWER;
738 case PLATFORM_PROFILE_BALANCED:
739 *perfmode = DYTC_MODE_BALANCE;
741 case PLATFORM_PROFILE_PERFORMANCE:
742 *perfmode = DYTC_MODE_PERFORM;
744 default: /* Unknown profile */
752 * dytc_profile_get: Function to register with platform_profile
753 * handler. Returns current platform profile.
755 static int dytc_profile_get(struct platform_profile_handler *pprof,
756 enum platform_profile_option *profile)
758 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
760 *profile = dytc->current_profile;
765 * Helper function - check if we are in CQL mode and if we are
769 * If not in CQL mode, just run the command
771 static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
772 unsigned long *output)
774 int err, cmd_err, cur_funcmode;
776 /* Determine if we are in CQL mode. This alters the commands we do */
777 err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
781 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
782 /* Check if we're OK to return immediately */
783 if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
786 if (cur_funcmode == DYTC_FUNCTION_CQL) {
787 err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
792 cmd_err = eval_dytc(priv->adev->handle, cmd, output);
793 /* Check return condition after we've restored CQL state */
795 if (cur_funcmode == DYTC_FUNCTION_CQL) {
796 err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
805 * dytc_profile_set: Function to register with platform_profile
806 * handler. Sets current platform profile.
808 static int dytc_profile_set(struct platform_profile_handler *pprof,
809 enum platform_profile_option profile)
811 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
812 struct ideapad_private *priv = dytc->priv;
813 unsigned long output;
816 err = mutex_lock_interruptible(&dytc->mutex);
820 if (profile == PLATFORM_PROFILE_BALANCED) {
821 /* To get back to balanced mode we just issue a reset command */
822 err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
828 err = convert_profile_to_dytc(profile, &perfmode);
832 /* Determine if we are in CQL mode. This alters the commands we do */
833 err = dytc_cql_command(priv, DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
839 /* Success - update current profile */
840 dytc->current_profile = profile;
843 mutex_unlock(&dytc->mutex);
848 static void dytc_profile_refresh(struct ideapad_private *priv)
850 enum platform_profile_option profile;
851 unsigned long output;
854 mutex_lock(&priv->dytc->mutex);
855 err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
856 mutex_unlock(&priv->dytc->mutex);
860 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
862 if (convert_dytc_to_profile(perfmode, &profile))
865 if (profile != priv->dytc->current_profile) {
866 priv->dytc->current_profile = profile;
867 platform_profile_notify();
871 static int ideapad_dytc_profile_init(struct ideapad_private *priv)
873 int err, dytc_version;
874 unsigned long output;
876 if (!priv->features.dytc)
879 err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
880 /* For all other errors we can flag the failure */
884 /* Check DYTC is enabled and supports mode setting */
885 if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output))
888 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
889 if (dytc_version < 5)
892 priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
896 mutex_init(&priv->dytc->mutex);
898 priv->dytc->priv = priv;
899 priv->dytc->pprof.profile_get = dytc_profile_get;
900 priv->dytc->pprof.profile_set = dytc_profile_set;
902 /* Setup supported modes */
903 set_bit(PLATFORM_PROFILE_LOW_POWER, priv->dytc->pprof.choices);
904 set_bit(PLATFORM_PROFILE_BALANCED, priv->dytc->pprof.choices);
905 set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->dytc->pprof.choices);
907 /* Create platform_profile structure and register */
908 err = platform_profile_register(&priv->dytc->pprof);
912 /* Ensure initial values are correct */
913 dytc_profile_refresh(priv);
918 mutex_destroy(&priv->dytc->mutex);
925 static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
930 platform_profile_remove();
931 mutex_destroy(&priv->dytc->mutex);
940 struct ideapad_rfk_data {
947 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
948 { "ideapad_wlan", CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
949 { "ideapad_bluetooth", CFG_CAP_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
950 { "ideapad_3g", CFG_CAP_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
953 static int ideapad_rfk_set(void *data, bool blocked)
955 struct ideapad_rfk_priv *priv = data;
956 int opcode = ideapad_rfk_data[priv->dev].opcode;
958 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
961 static const struct rfkill_ops ideapad_rfk_ops = {
962 .set_block = ideapad_rfk_set,
965 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
967 unsigned long hw_blocked = 0;
970 if (priv->features.hw_rfkill_switch) {
971 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
973 hw_blocked = !hw_blocked;
976 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
978 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
981 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
983 unsigned long rf_enabled;
986 if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
987 /* Force to enable bluetooth when no_bt_rfkill=1 */
988 write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
992 priv->rfk_priv[dev].dev = dev;
993 priv->rfk_priv[dev].priv = priv;
995 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
996 &priv->platform_device->dev,
997 ideapad_rfk_data[dev].type,
999 &priv->rfk_priv[dev]);
1000 if (!priv->rfk[dev])
1003 err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
1007 rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
1009 err = rfkill_register(priv->rfk[dev]);
1011 rfkill_destroy(priv->rfk[dev]);
1016 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1018 if (!priv->rfk[dev])
1021 rfkill_unregister(priv->rfk[dev]);
1022 rfkill_destroy(priv->rfk[dev]);
1028 static int ideapad_sysfs_init(struct ideapad_private *priv)
1030 return device_add_group(&priv->platform_device->dev,
1031 &ideapad_attribute_group);
1034 static void ideapad_sysfs_exit(struct ideapad_private *priv)
1036 device_remove_group(&priv->platform_device->dev,
1037 &ideapad_attribute_group);
1043 static const struct key_entry ideapad_keymap[] = {
1044 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
1045 { KE_KEY, 7, { KEY_CAMERA } },
1046 { KE_KEY, 8, { KEY_MICMUTE } },
1047 { KE_KEY, 11, { KEY_F16 } },
1048 { KE_KEY, 13, { KEY_WLAN } },
1049 { KE_KEY, 16, { KEY_PROG1 } },
1050 { KE_KEY, 17, { KEY_PROG2 } },
1051 { KE_KEY, 64, { KEY_PROG3 } },
1052 { KE_KEY, 65, { KEY_PROG4 } },
1053 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
1054 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
1055 { KE_KEY, 128, { KEY_ESC } },
1059 static int ideapad_input_init(struct ideapad_private *priv)
1061 struct input_dev *inputdev;
1064 inputdev = input_allocate_device();
1068 inputdev->name = "Ideapad extra buttons";
1069 inputdev->phys = "ideapad/input0";
1070 inputdev->id.bustype = BUS_HOST;
1071 inputdev->dev.parent = &priv->platform_device->dev;
1073 err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1075 dev_err(&priv->platform_device->dev,
1076 "Could not set up input device keymap: %d\n", err);
1080 err = input_register_device(inputdev);
1082 dev_err(&priv->platform_device->dev,
1083 "Could not register input device: %d\n", err);
1087 priv->inputdev = inputdev;
1092 input_free_device(inputdev);
1097 static void ideapad_input_exit(struct ideapad_private *priv)
1099 input_unregister_device(priv->inputdev);
1100 priv->inputdev = NULL;
1103 static void ideapad_input_report(struct ideapad_private *priv,
1104 unsigned long scancode)
1106 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1109 static void ideapad_input_novokey(struct ideapad_private *priv)
1111 unsigned long long_pressed;
1113 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1117 ideapad_input_report(priv, 17);
1119 ideapad_input_report(priv, 16);
1122 static void ideapad_check_special_buttons(struct ideapad_private *priv)
1124 unsigned long bit, value;
1126 if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1129 for_each_set_bit (bit, &value, 16) {
1133 /* Thermal Management button */
1134 ideapad_input_report(priv, 65);
1137 /* OneKey Theater button */
1138 ideapad_input_report(priv, 64);
1141 dev_info(&priv->platform_device->dev,
1142 "Unknown special button: %lu\n", bit);
1151 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1153 struct ideapad_private *priv = bl_get_data(blightdev);
1157 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1164 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1166 struct ideapad_private *priv = bl_get_data(blightdev);
1169 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1170 blightdev->props.brightness);
1174 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1175 blightdev->props.power != FB_BLANK_POWERDOWN);
1182 static const struct backlight_ops ideapad_backlight_ops = {
1183 .get_brightness = ideapad_backlight_get_brightness,
1184 .update_status = ideapad_backlight_update_status,
1187 static int ideapad_backlight_init(struct ideapad_private *priv)
1189 struct backlight_device *blightdev;
1190 struct backlight_properties props;
1191 unsigned long max, now, power;
1194 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1198 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1202 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1206 memset(&props, 0, sizeof(props));
1208 props.max_brightness = max;
1209 props.type = BACKLIGHT_PLATFORM;
1211 blightdev = backlight_device_register("ideapad",
1212 &priv->platform_device->dev,
1214 &ideapad_backlight_ops,
1216 if (IS_ERR(blightdev)) {
1217 err = PTR_ERR(blightdev);
1218 dev_err(&priv->platform_device->dev,
1219 "Could not register backlight device: %d\n", err);
1223 priv->blightdev = blightdev;
1224 blightdev->props.brightness = now;
1225 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1227 backlight_update_status(blightdev);
1232 static void ideapad_backlight_exit(struct ideapad_private *priv)
1234 backlight_device_unregister(priv->blightdev);
1235 priv->blightdev = NULL;
1238 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1240 struct backlight_device *blightdev = priv->blightdev;
1241 unsigned long power;
1246 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1249 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1252 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1256 /* if we control brightness via acpi video driver */
1257 if (!priv->blightdev)
1258 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1260 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1264 * keyboard backlight
1266 static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1271 err = eval_hals(priv->adev->handle, &hals);
1275 return !!test_bit(HALS_KBD_BL_STATE_BIT, &hals);
1278 static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1280 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1282 return ideapad_kbd_bl_brightness_get(priv);
1285 static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1287 int err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1292 priv->kbd_bl.last_brightness = brightness;
1297 static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1298 enum led_brightness brightness)
1300 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1302 return ideapad_kbd_bl_brightness_set(priv, brightness);
1305 static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1309 if (!priv->kbd_bl.initialized)
1312 brightness = ideapad_kbd_bl_brightness_get(priv);
1316 if (brightness == priv->kbd_bl.last_brightness)
1319 priv->kbd_bl.last_brightness = brightness;
1321 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1324 static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1326 int brightness, err;
1328 if (!priv->features.kbd_bl)
1331 if (WARN_ON(priv->kbd_bl.initialized))
1334 brightness = ideapad_kbd_bl_brightness_get(priv);
1338 priv->kbd_bl.last_brightness = brightness;
1340 priv->kbd_bl.led.name = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1341 priv->kbd_bl.led.max_brightness = 1;
1342 priv->kbd_bl.led.brightness_get = ideapad_kbd_bl_led_cdev_brightness_get;
1343 priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1344 priv->kbd_bl.led.flags = LED_BRIGHT_HW_CHANGED;
1346 err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1350 priv->kbd_bl.initialized = true;
1355 static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1357 if (!priv->kbd_bl.initialized)
1360 priv->kbd_bl.initialized = false;
1362 led_classdev_unregister(&priv->kbd_bl.led);
1368 static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
1370 unsigned long value;
1372 if (!priv->features.touchpad_ctrl_via_ec)
1375 /* Without reading from EC touchpad LED doesn't switch state */
1376 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
1377 unsigned char param;
1379 * Some IdeaPads don't really turn off touchpad - they only
1380 * switch the LED state. We (de)activate KBC AUX port to turn
1381 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1382 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1384 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1385 ideapad_input_report(priv, value ? 67 : 66);
1386 sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1390 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1392 struct ideapad_private *priv = data;
1393 unsigned long vpc1, vpc2, bit;
1395 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1398 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1401 vpc1 = (vpc2 << 8) | vpc1;
1403 for_each_set_bit (bit, &vpc1, 16) {
1410 ideapad_input_report(priv, bit);
1414 * This event gets send on a Yoga 300-11IBR when the EC
1415 * believes that the device has changed between laptop/
1416 * tent/stand/tablet mode. The EC relies on getting
1417 * angle info from 2 accelerometers through a special
1418 * windows service calling a DSM on the DUAL250E ACPI-
1419 * device. Linux does not do this, making the laptop/
1420 * tent/stand/tablet mode info unreliable, so we simply
1421 * ignore these events.
1425 ideapad_sync_rfk_state(priv);
1428 ideapad_sync_touchpad_state(priv);
1431 ideapad_backlight_notify_brightness(priv);
1434 ideapad_input_novokey(priv);
1437 ideapad_backlight_notify_power(priv);
1441 * Some IdeaPads report event 1 every ~20
1442 * seconds while on battery power; some
1443 * report this when changing to/from tablet
1444 * mode; some report this when the keyboard
1445 * backlight has changed.
1447 ideapad_kbd_bl_notify(priv);
1450 ideapad_check_special_buttons(priv);
1453 dev_info(&priv->platform_device->dev,
1454 "Unknown event: %lu\n", bit);
1459 #if IS_ENABLED(CONFIG_ACPI_WMI)
1460 static void ideapad_wmi_notify(u32 value, void *context)
1462 struct ideapad_private *priv = context;
1463 unsigned long result;
1467 ideapad_input_report(priv, value);
1470 if (!eval_hals(priv->adev->handle, &result)) {
1471 bool state = test_bit(HALS_FNLOCK_STATE_BIT, &result);
1473 exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
1477 dev_info(&priv->platform_device->dev,
1478 "Unknown WMI event: %u\n", value);
1484 * Some ideapads have a hardware rfkill switch, but most do not have one.
1485 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1486 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1487 * There used to be a long list of DMI ids for models without a hw rfkill
1488 * switch here, but that resulted in playing whack a mole.
1489 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1490 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1491 * hw-blocked results in an empty SSID list, which is a much more benign
1493 * So the default now is the much safer option of assuming there is no
1494 * hardware rfkill switch. This default also actually matches most hardware,
1495 * since having a hw rfkill switch is quite rare on modern hardware, so this
1496 * also leads to a much shorter list.
1498 static const struct dmi_system_id hw_rfkill_list[] = {
1502 static void ideapad_check_features(struct ideapad_private *priv)
1504 acpi_handle handle = priv->adev->handle;
1507 priv->features.hw_rfkill_switch = dmi_check_system(hw_rfkill_list);
1509 /* Most ideapads with ELAN0634 touchpad don't use EC touchpad switch */
1510 priv->features.touchpad_ctrl_via_ec = !acpi_dev_present("ELAN0634", NULL, -1);
1512 if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
1513 priv->features.fan_mode = true;
1515 if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
1516 priv->features.conservation_mode = true;
1518 if (acpi_has_method(handle, "DYTC"))
1519 priv->features.dytc = true;
1521 if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
1522 if (!eval_hals(handle, &val)) {
1523 if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
1524 priv->features.fn_lock = true;
1526 if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val))
1527 priv->features.kbd_bl = true;
1529 if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
1530 priv->features.usb_charging = true;
1535 static int ideapad_acpi_add(struct platform_device *pdev)
1537 struct ideapad_private *priv;
1538 struct acpi_device *adev;
1543 err = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
1547 if (eval_int(adev->handle, "_CFG", &cfg))
1550 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1554 dev_set_drvdata(&pdev->dev, priv);
1558 priv->platform_device = pdev;
1560 ideapad_check_features(priv);
1562 err = ideapad_sysfs_init(priv);
1566 ideapad_debugfs_init(priv);
1568 err = ideapad_input_init(priv);
1572 err = ideapad_kbd_bl_init(priv);
1575 dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
1577 dev_info(&pdev->dev, "Keyboard backlight control not available\n");
1581 * On some models without a hw-switch (the yoga 2 13 at least)
1582 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1584 if (!priv->features.hw_rfkill_switch)
1585 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1587 /* The same for Touchpad */
1588 if (!priv->features.touchpad_ctrl_via_ec)
1589 write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, 1);
1591 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1592 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1593 ideapad_register_rfkill(priv, i);
1595 ideapad_sync_rfk_state(priv);
1596 ideapad_sync_touchpad_state(priv);
1598 err = ideapad_dytc_profile_init(priv);
1601 dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
1603 dev_info(&pdev->dev, "DYTC interface is not available\n");
1606 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1607 err = ideapad_backlight_init(priv);
1608 if (err && err != -ENODEV)
1609 goto backlight_failed;
1612 status = acpi_install_notify_handler(adev->handle,
1614 ideapad_acpi_notify, priv);
1615 if (ACPI_FAILURE(status)) {
1617 goto notification_failed;
1620 #if IS_ENABLED(CONFIG_ACPI_WMI)
1621 for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
1622 status = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
1623 ideapad_wmi_notify, priv);
1624 if (ACPI_SUCCESS(status)) {
1625 priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
1630 if (ACPI_FAILURE(status) && status != AE_NOT_EXIST) {
1632 goto notification_failed_wmi;
1638 #if IS_ENABLED(CONFIG_ACPI_WMI)
1639 notification_failed_wmi:
1640 acpi_remove_notify_handler(priv->adev->handle,
1642 ideapad_acpi_notify);
1645 notification_failed:
1646 ideapad_backlight_exit(priv);
1649 ideapad_dytc_profile_exit(priv);
1651 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1652 ideapad_unregister_rfkill(priv, i);
1654 ideapad_kbd_bl_exit(priv);
1655 ideapad_input_exit(priv);
1658 ideapad_debugfs_exit(priv);
1659 ideapad_sysfs_exit(priv);
1664 static int ideapad_acpi_remove(struct platform_device *pdev)
1666 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1669 #if IS_ENABLED(CONFIG_ACPI_WMI)
1670 if (priv->fnesc_guid)
1671 wmi_remove_notify_handler(priv->fnesc_guid);
1674 acpi_remove_notify_handler(priv->adev->handle,
1676 ideapad_acpi_notify);
1678 ideapad_backlight_exit(priv);
1679 ideapad_dytc_profile_exit(priv);
1681 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1682 ideapad_unregister_rfkill(priv, i);
1684 ideapad_kbd_bl_exit(priv);
1685 ideapad_input_exit(priv);
1686 ideapad_debugfs_exit(priv);
1687 ideapad_sysfs_exit(priv);
1692 #ifdef CONFIG_PM_SLEEP
1693 static int ideapad_acpi_resume(struct device *dev)
1695 struct ideapad_private *priv = dev_get_drvdata(dev);
1697 ideapad_sync_rfk_state(priv);
1698 ideapad_sync_touchpad_state(priv);
1701 dytc_profile_refresh(priv);
1706 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1708 static const struct acpi_device_id ideapad_device_ids[] = {
1712 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1714 static struct platform_driver ideapad_acpi_driver = {
1715 .probe = ideapad_acpi_add,
1716 .remove = ideapad_acpi_remove,
1718 .name = "ideapad_acpi",
1720 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1724 module_platform_driver(ideapad_acpi_driver);
1726 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1727 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1728 MODULE_LICENSE("GPL");