1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
5 * Copyright © 2010 Intel Corporation
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/acpi.h>
12 #include <linux/backlight.h>
13 #include <linux/bitops.h>
14 #include <linux/bug.h>
15 #include <linux/debugfs.h>
16 #include <linux/device.h>
17 #include <linux/dmi.h>
19 #include <linux/i8042.h>
20 #include <linux/init.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/leds.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/platform_profile.h>
29 #include <linux/rfkill.h>
30 #include <linux/seq_file.h>
31 #include <linux/sysfs.h>
32 #include <linux/types.h>
34 #include <acpi/video.h>
36 #include <dt-bindings/leds/common.h>
38 #define IDEAPAD_RFKILL_DEV_NUM 3
40 #if IS_ENABLED(CONFIG_ACPI_WMI)
41 static const char *const ideapad_wmi_fnesc_events[] = {
42 "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
43 "56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
50 CFG_CAP_WIFI_BIT = 18,
52 CFG_CAP_TOUCHPAD_BIT = 30,
56 GBMD_CONSERVATION_STATE_BIT = 5,
60 SMBC_CONSERVATION_ON = 3,
61 SMBC_CONSERVATION_OFF = 5,
65 HALS_KBD_BL_SUPPORT_BIT = 4,
66 HALS_KBD_BL_STATE_BIT = 5,
67 HALS_USB_CHARGING_SUPPORT_BIT = 6,
68 HALS_USB_CHARGING_STATE_BIT = 7,
69 HALS_FNLOCK_SUPPORT_BIT = 9,
70 HALS_FNLOCK_STATE_BIT = 10,
71 HALS_HOTKEYS_PRIMARY_BIT = 11,
76 SALS_KBD_BL_OFF = 0x9,
77 SALS_USB_CHARGING_ON = 0xa,
78 SALS_USB_CHARGING_OFF = 0xb,
80 SALS_FNLOCK_OFF = 0xf,
101 VPCCMD_R_ODD, /* 0x21 */
106 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
107 VPCCMD_W_BL_POWER = 0x33,
110 struct ideapad_dytc_priv {
111 enum platform_profile_option current_profile;
112 struct platform_profile_handler pprof;
113 struct mutex mutex; /* protects the DYTC interface */
114 struct ideapad_private *priv;
117 struct ideapad_rfk_priv {
119 struct ideapad_private *priv;
122 struct ideapad_private {
123 struct acpi_device *adev;
124 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
125 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
126 struct platform_device *platform_device;
127 struct input_dev *inputdev;
128 struct backlight_device *blightdev;
129 struct ideapad_dytc_priv *dytc;
130 struct dentry *debug;
132 const char *fnesc_guid;
134 bool conservation_mode : 1;
138 bool hw_rfkill_switch : 1;
140 bool touchpad_ctrl_via_ec : 1;
141 bool usb_charging : 1;
145 struct led_classdev led;
146 unsigned int last_brightness;
150 static bool no_bt_rfkill;
151 module_param(no_bt_rfkill, bool, 0444);
152 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
157 #define IDEAPAD_EC_TIMEOUT 200 /* in ms */
159 static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
161 unsigned long long result;
164 status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
165 if (ACPI_FAILURE(status))
173 static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
175 acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
177 return ACPI_FAILURE(status) ? -EIO : 0;
180 static int eval_gbmd(acpi_handle handle, unsigned long *res)
182 return eval_int(handle, "GBMD", res);
185 static int exec_smbc(acpi_handle handle, unsigned long arg)
187 return exec_simple_method(handle, "SMBC", arg);
190 static int eval_hals(acpi_handle handle, unsigned long *res)
192 return eval_int(handle, "HALS", res);
195 static int exec_sals(acpi_handle handle, unsigned long arg)
197 return exec_simple_method(handle, "SALS", arg);
200 static int eval_int_with_arg(acpi_handle handle, const char *name, unsigned long arg, unsigned long *res)
202 struct acpi_object_list params;
203 unsigned long long result;
204 union acpi_object in_obj;
208 params.pointer = &in_obj;
209 in_obj.type = ACPI_TYPE_INTEGER;
210 in_obj.integer.value = arg;
212 status = acpi_evaluate_integer(handle, (char *)name, ¶ms, &result);
213 if (ACPI_FAILURE(status))
222 static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
224 return eval_int_with_arg(handle, "DYTC", cmd, res);
227 static int eval_vpcr(acpi_handle handle, unsigned long cmd, unsigned long *res)
229 return eval_int_with_arg(handle, "VPCR", cmd, res);
232 static int eval_vpcw(acpi_handle handle, unsigned long cmd, unsigned long data)
234 struct acpi_object_list params;
235 union acpi_object in_obj[2];
239 params.pointer = in_obj;
240 in_obj[0].type = ACPI_TYPE_INTEGER;
241 in_obj[0].integer.value = cmd;
242 in_obj[1].type = ACPI_TYPE_INTEGER;
243 in_obj[1].integer.value = data;
245 status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL);
246 if (ACPI_FAILURE(status))
252 static int read_ec_data(acpi_handle handle, unsigned long cmd, unsigned long *data)
254 unsigned long end_jiffies, val;
257 err = eval_vpcw(handle, 1, cmd);
261 end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
263 while (time_before(jiffies, end_jiffies)) {
266 err = eval_vpcr(handle, 1, &val);
271 return eval_vpcr(handle, 0, data);
274 acpi_handle_err(handle, "timeout in %s\n", __func__);
279 static int write_ec_cmd(acpi_handle handle, unsigned long cmd, unsigned long data)
281 unsigned long end_jiffies, val;
284 err = eval_vpcw(handle, 0, data);
288 err = eval_vpcw(handle, 1, cmd);
292 end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
294 while (time_before(jiffies, end_jiffies)) {
297 err = eval_vpcr(handle, 1, &val);
305 acpi_handle_err(handle, "timeout in %s\n", __func__);
313 static int debugfs_status_show(struct seq_file *s, void *data)
315 struct ideapad_private *priv = s->private;
318 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
319 seq_printf(s, "Backlight max: %lu\n", value);
320 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
321 seq_printf(s, "Backlight now: %lu\n", value);
322 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
323 seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
325 seq_puts(s, "=====================\n");
327 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
328 seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
329 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
330 seq_printf(s, "Wifi status: %s (%lu)\n", value ? "on" : "off", value);
331 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
332 seq_printf(s, "BT status: %s (%lu)\n", value ? "on" : "off", value);
333 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
334 seq_printf(s, "3G status: %s (%lu)\n", value ? "on" : "off", value);
336 seq_puts(s, "=====================\n");
338 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
339 seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
340 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
341 seq_printf(s, "Camera status: %s (%lu)\n", value ? "on" : "off", value);
343 seq_puts(s, "=====================\n");
345 if (!eval_gbmd(priv->adev->handle, &value))
346 seq_printf(s, "GBMD: %#010lx\n", value);
347 if (!eval_hals(priv->adev->handle, &value))
348 seq_printf(s, "HALS: %#010lx\n", value);
352 DEFINE_SHOW_ATTRIBUTE(debugfs_status);
354 static int debugfs_cfg_show(struct seq_file *s, void *data)
356 struct ideapad_private *priv = s->private;
358 seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
360 seq_puts(s, "Capabilities:");
361 if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
362 seq_puts(s, " bluetooth");
363 if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
365 if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
366 seq_puts(s, " wifi");
367 if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
368 seq_puts(s, " camera");
369 if (test_bit(CFG_CAP_TOUCHPAD_BIT, &priv->cfg))
370 seq_puts(s, " touchpad");
373 seq_puts(s, "Graphics: ");
374 switch (priv->cfg & 0x700) {
376 seq_puts(s, "Intel");
382 seq_puts(s, "Nvidia");
385 seq_puts(s, "Intel and ATI");
388 seq_puts(s, "Intel and Nvidia");
395 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
397 static void ideapad_debugfs_init(struct ideapad_private *priv)
401 dir = debugfs_create_dir("ideapad", NULL);
404 debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
405 debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
408 static void ideapad_debugfs_exit(struct ideapad_private *priv)
410 debugfs_remove_recursive(priv->debug);
417 static ssize_t camera_power_show(struct device *dev,
418 struct device_attribute *attr,
421 struct ideapad_private *priv = dev_get_drvdata(dev);
422 unsigned long result;
425 err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
429 return sysfs_emit(buf, "%d\n", !!result);
432 static ssize_t camera_power_store(struct device *dev,
433 struct device_attribute *attr,
434 const char *buf, size_t count)
436 struct ideapad_private *priv = dev_get_drvdata(dev);
440 err = kstrtobool(buf, &state);
444 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
451 static DEVICE_ATTR_RW(camera_power);
453 static ssize_t conservation_mode_show(struct device *dev,
454 struct device_attribute *attr,
457 struct ideapad_private *priv = dev_get_drvdata(dev);
458 unsigned long result;
461 err = eval_gbmd(priv->adev->handle, &result);
465 return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
468 static ssize_t conservation_mode_store(struct device *dev,
469 struct device_attribute *attr,
470 const char *buf, size_t count)
472 struct ideapad_private *priv = dev_get_drvdata(dev);
476 err = kstrtobool(buf, &state);
480 err = exec_smbc(priv->adev->handle, state ? SMBC_CONSERVATION_ON : SMBC_CONSERVATION_OFF);
487 static DEVICE_ATTR_RW(conservation_mode);
489 static ssize_t fan_mode_show(struct device *dev,
490 struct device_attribute *attr,
493 struct ideapad_private *priv = dev_get_drvdata(dev);
494 unsigned long result;
497 err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
501 return sysfs_emit(buf, "%lu\n", result);
504 static ssize_t fan_mode_store(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct ideapad_private *priv = dev_get_drvdata(dev);
512 err = kstrtouint(buf, 0, &state);
516 if (state > 4 || state == 3)
519 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
526 static DEVICE_ATTR_RW(fan_mode);
528 static ssize_t fn_lock_show(struct device *dev,
529 struct device_attribute *attr,
532 struct ideapad_private *priv = dev_get_drvdata(dev);
536 err = eval_hals(priv->adev->handle, &hals);
540 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_FNLOCK_STATE_BIT, &hals));
543 static ssize_t fn_lock_store(struct device *dev,
544 struct device_attribute *attr,
545 const char *buf, size_t count)
547 struct ideapad_private *priv = dev_get_drvdata(dev);
551 err = kstrtobool(buf, &state);
555 err = exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
562 static DEVICE_ATTR_RW(fn_lock);
564 static ssize_t touchpad_show(struct device *dev,
565 struct device_attribute *attr,
568 struct ideapad_private *priv = dev_get_drvdata(dev);
569 unsigned long result;
572 err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
576 return sysfs_emit(buf, "%d\n", !!result);
579 static ssize_t touchpad_store(struct device *dev,
580 struct device_attribute *attr,
581 const char *buf, size_t count)
583 struct ideapad_private *priv = dev_get_drvdata(dev);
587 err = kstrtobool(buf, &state);
591 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
598 static DEVICE_ATTR_RW(touchpad);
600 static ssize_t usb_charging_show(struct device *dev,
601 struct device_attribute *attr,
604 struct ideapad_private *priv = dev_get_drvdata(dev);
608 err = eval_hals(priv->adev->handle, &hals);
612 return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
615 static ssize_t usb_charging_store(struct device *dev,
616 struct device_attribute *attr,
617 const char *buf, size_t count)
619 struct ideapad_private *priv = dev_get_drvdata(dev);
623 err = kstrtobool(buf, &state);
627 err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
634 static DEVICE_ATTR_RW(usb_charging);
636 static struct attribute *ideapad_attributes[] = {
637 &dev_attr_camera_power.attr,
638 &dev_attr_conservation_mode.attr,
639 &dev_attr_fan_mode.attr,
640 &dev_attr_fn_lock.attr,
641 &dev_attr_touchpad.attr,
642 &dev_attr_usb_charging.attr,
646 static umode_t ideapad_is_visible(struct kobject *kobj,
647 struct attribute *attr,
650 struct device *dev = kobj_to_dev(kobj);
651 struct ideapad_private *priv = dev_get_drvdata(dev);
652 bool supported = true;
654 if (attr == &dev_attr_camera_power.attr)
655 supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
656 else if (attr == &dev_attr_conservation_mode.attr)
657 supported = priv->features.conservation_mode;
658 else if (attr == &dev_attr_fan_mode.attr)
659 supported = priv->features.fan_mode;
660 else if (attr == &dev_attr_fn_lock.attr)
661 supported = priv->features.fn_lock;
662 else if (attr == &dev_attr_touchpad.attr)
663 supported = priv->features.touchpad_ctrl_via_ec &&
664 test_bit(CFG_CAP_TOUCHPAD_BIT, &priv->cfg);
665 else if (attr == &dev_attr_usb_charging.attr)
666 supported = priv->features.usb_charging;
668 return supported ? attr->mode : 0;
671 static const struct attribute_group ideapad_attribute_group = {
672 .is_visible = ideapad_is_visible,
673 .attrs = ideapad_attributes
677 * DYTC Platform profile
679 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
680 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
681 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
682 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
684 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
685 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
686 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
688 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
689 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
691 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
692 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
693 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
695 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
696 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
697 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
699 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
700 #define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
701 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
703 #define DYTC_SET_COMMAND(function, mode, on) \
704 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
705 (mode) << DYTC_SET_MODE_BIT | \
706 (on) << DYTC_SET_VALID_BIT)
708 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
710 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
712 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
715 case DYTC_MODE_LOW_POWER:
716 *profile = PLATFORM_PROFILE_LOW_POWER;
718 case DYTC_MODE_BALANCE:
719 *profile = PLATFORM_PROFILE_BALANCED;
721 case DYTC_MODE_PERFORM:
722 *profile = PLATFORM_PROFILE_PERFORMANCE;
724 default: /* Unknown mode */
731 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
734 case PLATFORM_PROFILE_LOW_POWER:
735 *perfmode = DYTC_MODE_LOW_POWER;
737 case PLATFORM_PROFILE_BALANCED:
738 *perfmode = DYTC_MODE_BALANCE;
740 case PLATFORM_PROFILE_PERFORMANCE:
741 *perfmode = DYTC_MODE_PERFORM;
743 default: /* Unknown profile */
751 * dytc_profile_get: Function to register with platform_profile
752 * handler. Returns current platform profile.
754 static int dytc_profile_get(struct platform_profile_handler *pprof,
755 enum platform_profile_option *profile)
757 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
759 *profile = dytc->current_profile;
764 * Helper function - check if we are in CQL mode and if we are
768 * If not in CQL mode, just run the command
770 static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
771 unsigned long *output)
773 int err, cmd_err, cur_funcmode;
775 /* Determine if we are in CQL mode. This alters the commands we do */
776 err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
780 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
781 /* Check if we're OK to return immediately */
782 if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
785 if (cur_funcmode == DYTC_FUNCTION_CQL) {
786 err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
791 cmd_err = eval_dytc(priv->adev->handle, cmd, output);
792 /* Check return condition after we've restored CQL state */
794 if (cur_funcmode == DYTC_FUNCTION_CQL) {
795 err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
804 * dytc_profile_set: Function to register with platform_profile
805 * handler. Sets current platform profile.
807 static int dytc_profile_set(struct platform_profile_handler *pprof,
808 enum platform_profile_option profile)
810 struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
811 struct ideapad_private *priv = dytc->priv;
814 err = mutex_lock_interruptible(&dytc->mutex);
818 if (profile == PLATFORM_PROFILE_BALANCED) {
819 /* To get back to balanced mode we just issue a reset command */
820 err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
826 err = convert_profile_to_dytc(profile, &perfmode);
830 /* Determine if we are in CQL mode. This alters the commands we do */
831 err = dytc_cql_command(priv, DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
837 /* Success - update current profile */
838 dytc->current_profile = profile;
841 mutex_unlock(&dytc->mutex);
846 static void dytc_profile_refresh(struct ideapad_private *priv)
848 enum platform_profile_option profile;
849 unsigned long output;
852 mutex_lock(&priv->dytc->mutex);
853 err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
854 mutex_unlock(&priv->dytc->mutex);
858 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
860 if (convert_dytc_to_profile(perfmode, &profile))
863 if (profile != priv->dytc->current_profile) {
864 priv->dytc->current_profile = profile;
865 platform_profile_notify();
869 static int ideapad_dytc_profile_init(struct ideapad_private *priv)
871 int err, dytc_version;
872 unsigned long output;
874 if (!priv->features.dytc)
877 err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
878 /* For all other errors we can flag the failure */
882 /* Check DYTC is enabled and supports mode setting */
883 if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output))
886 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
887 if (dytc_version < 5)
890 priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
894 mutex_init(&priv->dytc->mutex);
896 priv->dytc->priv = priv;
897 priv->dytc->pprof.profile_get = dytc_profile_get;
898 priv->dytc->pprof.profile_set = dytc_profile_set;
900 /* Setup supported modes */
901 set_bit(PLATFORM_PROFILE_LOW_POWER, priv->dytc->pprof.choices);
902 set_bit(PLATFORM_PROFILE_BALANCED, priv->dytc->pprof.choices);
903 set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->dytc->pprof.choices);
905 /* Create platform_profile structure and register */
906 err = platform_profile_register(&priv->dytc->pprof);
910 /* Ensure initial values are correct */
911 dytc_profile_refresh(priv);
916 mutex_destroy(&priv->dytc->mutex);
923 static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
928 platform_profile_remove();
929 mutex_destroy(&priv->dytc->mutex);
938 struct ideapad_rfk_data {
945 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
946 { "ideapad_wlan", CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
947 { "ideapad_bluetooth", CFG_CAP_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
948 { "ideapad_3g", CFG_CAP_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
951 static int ideapad_rfk_set(void *data, bool blocked)
953 struct ideapad_rfk_priv *priv = data;
954 int opcode = ideapad_rfk_data[priv->dev].opcode;
956 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
959 static const struct rfkill_ops ideapad_rfk_ops = {
960 .set_block = ideapad_rfk_set,
963 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
965 unsigned long hw_blocked = 0;
968 if (priv->features.hw_rfkill_switch) {
969 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
971 hw_blocked = !hw_blocked;
974 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
976 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
979 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
981 unsigned long rf_enabled;
984 if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
985 /* Force to enable bluetooth when no_bt_rfkill=1 */
986 write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
990 priv->rfk_priv[dev].dev = dev;
991 priv->rfk_priv[dev].priv = priv;
993 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
994 &priv->platform_device->dev,
995 ideapad_rfk_data[dev].type,
997 &priv->rfk_priv[dev]);
1001 err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
1005 rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
1007 err = rfkill_register(priv->rfk[dev]);
1009 rfkill_destroy(priv->rfk[dev]);
1014 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1016 if (!priv->rfk[dev])
1019 rfkill_unregister(priv->rfk[dev]);
1020 rfkill_destroy(priv->rfk[dev]);
1026 static int ideapad_sysfs_init(struct ideapad_private *priv)
1028 return device_add_group(&priv->platform_device->dev,
1029 &ideapad_attribute_group);
1032 static void ideapad_sysfs_exit(struct ideapad_private *priv)
1034 device_remove_group(&priv->platform_device->dev,
1035 &ideapad_attribute_group);
1041 static const struct key_entry ideapad_keymap[] = {
1042 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
1043 { KE_KEY, 7, { KEY_CAMERA } },
1044 { KE_KEY, 8, { KEY_MICMUTE } },
1045 { KE_KEY, 11, { KEY_F16 } },
1046 { KE_KEY, 13, { KEY_WLAN } },
1047 { KE_KEY, 16, { KEY_PROG1 } },
1048 { KE_KEY, 17, { KEY_PROG2 } },
1049 { KE_KEY, 64, { KEY_PROG3 } },
1050 { KE_KEY, 65, { KEY_PROG4 } },
1051 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
1052 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
1053 { KE_KEY, 128, { KEY_ESC } },
1057 static int ideapad_input_init(struct ideapad_private *priv)
1059 struct input_dev *inputdev;
1062 inputdev = input_allocate_device();
1066 inputdev->name = "Ideapad extra buttons";
1067 inputdev->phys = "ideapad/input0";
1068 inputdev->id.bustype = BUS_HOST;
1069 inputdev->dev.parent = &priv->platform_device->dev;
1071 err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1073 dev_err(&priv->platform_device->dev,
1074 "Could not set up input device keymap: %d\n", err);
1078 err = input_register_device(inputdev);
1080 dev_err(&priv->platform_device->dev,
1081 "Could not register input device: %d\n", err);
1085 priv->inputdev = inputdev;
1090 input_free_device(inputdev);
1095 static void ideapad_input_exit(struct ideapad_private *priv)
1097 input_unregister_device(priv->inputdev);
1098 priv->inputdev = NULL;
1101 static void ideapad_input_report(struct ideapad_private *priv,
1102 unsigned long scancode)
1104 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1107 static void ideapad_input_novokey(struct ideapad_private *priv)
1109 unsigned long long_pressed;
1111 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1115 ideapad_input_report(priv, 17);
1117 ideapad_input_report(priv, 16);
1120 static void ideapad_check_special_buttons(struct ideapad_private *priv)
1122 unsigned long bit, value;
1124 if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1127 for_each_set_bit (bit, &value, 16) {
1131 /* Thermal Management button */
1132 ideapad_input_report(priv, 65);
1135 /* OneKey Theater button */
1136 ideapad_input_report(priv, 64);
1139 dev_info(&priv->platform_device->dev,
1140 "Unknown special button: %lu\n", bit);
1149 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1151 struct ideapad_private *priv = bl_get_data(blightdev);
1155 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1162 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1164 struct ideapad_private *priv = bl_get_data(blightdev);
1167 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1168 blightdev->props.brightness);
1172 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1173 blightdev->props.power != FB_BLANK_POWERDOWN);
1180 static const struct backlight_ops ideapad_backlight_ops = {
1181 .get_brightness = ideapad_backlight_get_brightness,
1182 .update_status = ideapad_backlight_update_status,
1185 static int ideapad_backlight_init(struct ideapad_private *priv)
1187 struct backlight_device *blightdev;
1188 struct backlight_properties props;
1189 unsigned long max, now, power;
1192 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1196 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1200 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1204 memset(&props, 0, sizeof(props));
1206 props.max_brightness = max;
1207 props.type = BACKLIGHT_PLATFORM;
1209 blightdev = backlight_device_register("ideapad",
1210 &priv->platform_device->dev,
1212 &ideapad_backlight_ops,
1214 if (IS_ERR(blightdev)) {
1215 err = PTR_ERR(blightdev);
1216 dev_err(&priv->platform_device->dev,
1217 "Could not register backlight device: %d\n", err);
1221 priv->blightdev = blightdev;
1222 blightdev->props.brightness = now;
1223 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1225 backlight_update_status(blightdev);
1230 static void ideapad_backlight_exit(struct ideapad_private *priv)
1232 backlight_device_unregister(priv->blightdev);
1233 priv->blightdev = NULL;
1236 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1238 struct backlight_device *blightdev = priv->blightdev;
1239 unsigned long power;
1244 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1247 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1250 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1254 /* if we control brightness via acpi video driver */
1255 if (!priv->blightdev)
1256 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1258 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1262 * keyboard backlight
1264 static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1269 err = eval_hals(priv->adev->handle, &hals);
1273 return !!test_bit(HALS_KBD_BL_STATE_BIT, &hals);
1276 static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1278 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1280 return ideapad_kbd_bl_brightness_get(priv);
1283 static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1285 int err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1290 priv->kbd_bl.last_brightness = brightness;
1295 static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1296 enum led_brightness brightness)
1298 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1300 return ideapad_kbd_bl_brightness_set(priv, brightness);
1303 static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1307 if (!priv->kbd_bl.initialized)
1310 brightness = ideapad_kbd_bl_brightness_get(priv);
1314 if (brightness == priv->kbd_bl.last_brightness)
1317 priv->kbd_bl.last_brightness = brightness;
1319 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1322 static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1324 int brightness, err;
1326 if (!priv->features.kbd_bl)
1329 if (WARN_ON(priv->kbd_bl.initialized))
1332 brightness = ideapad_kbd_bl_brightness_get(priv);
1336 priv->kbd_bl.last_brightness = brightness;
1338 priv->kbd_bl.led.name = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1339 priv->kbd_bl.led.max_brightness = 1;
1340 priv->kbd_bl.led.brightness_get = ideapad_kbd_bl_led_cdev_brightness_get;
1341 priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1342 priv->kbd_bl.led.flags = LED_BRIGHT_HW_CHANGED;
1344 err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1348 priv->kbd_bl.initialized = true;
1353 static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1355 if (!priv->kbd_bl.initialized)
1358 priv->kbd_bl.initialized = false;
1360 led_classdev_unregister(&priv->kbd_bl.led);
1366 static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
1368 unsigned long value;
1370 if (!priv->features.touchpad_ctrl_via_ec)
1373 /* Without reading from EC touchpad LED doesn't switch state */
1374 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
1375 unsigned char param;
1377 * Some IdeaPads don't really turn off touchpad - they only
1378 * switch the LED state. We (de)activate KBC AUX port to turn
1379 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1380 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1382 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1383 ideapad_input_report(priv, value ? 67 : 66);
1384 sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1388 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1390 struct ideapad_private *priv = data;
1391 unsigned long vpc1, vpc2, bit;
1393 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1396 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1399 vpc1 = (vpc2 << 8) | vpc1;
1401 for_each_set_bit (bit, &vpc1, 16) {
1408 ideapad_input_report(priv, bit);
1411 ideapad_sync_rfk_state(priv);
1414 ideapad_sync_touchpad_state(priv);
1417 ideapad_backlight_notify_brightness(priv);
1420 ideapad_input_novokey(priv);
1423 ideapad_backlight_notify_power(priv);
1427 * Some IdeaPads report event 1 every ~20
1428 * seconds while on battery power; some
1429 * report this when changing to/from tablet
1430 * mode; some report this when the keyboard
1431 * backlight has changed.
1433 ideapad_kbd_bl_notify(priv);
1436 ideapad_check_special_buttons(priv);
1439 dev_info(&priv->platform_device->dev,
1440 "Unknown event: %lu\n", bit);
1445 #if IS_ENABLED(CONFIG_ACPI_WMI)
1446 static void ideapad_wmi_notify(u32 value, void *context)
1448 struct ideapad_private *priv = context;
1452 ideapad_input_report(priv, value);
1455 dev_info(&priv->platform_device->dev,
1456 "Unknown WMI event: %u\n", value);
1462 * Some ideapads have a hardware rfkill switch, but most do not have one.
1463 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1464 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1465 * There used to be a long list of DMI ids for models without a hw rfkill
1466 * switch here, but that resulted in playing whack a mole.
1467 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1468 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1469 * hw-blocked results in an empty SSID list, which is a much more benign
1471 * So the default now is the much safer option of assuming there is no
1472 * hardware rfkill switch. This default also actually matches most hardware,
1473 * since having a hw rfkill switch is quite rare on modern hardware, so this
1474 * also leads to a much shorter list.
1476 static const struct dmi_system_id hw_rfkill_list[] = {
1480 static void ideapad_check_features(struct ideapad_private *priv)
1482 acpi_handle handle = priv->adev->handle;
1485 priv->features.hw_rfkill_switch = dmi_check_system(hw_rfkill_list);
1487 /* Most ideapads with ELAN0634 touchpad don't use EC touchpad switch */
1488 priv->features.touchpad_ctrl_via_ec = !acpi_dev_present("ELAN0634", NULL, -1);
1490 if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
1491 priv->features.fan_mode = true;
1493 if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
1494 priv->features.conservation_mode = true;
1496 if (acpi_has_method(handle, "DYTC"))
1497 priv->features.dytc = true;
1499 if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
1500 if (!eval_hals(handle, &val)) {
1501 if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
1502 priv->features.fn_lock = true;
1504 if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val))
1505 priv->features.kbd_bl = true;
1507 if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
1508 priv->features.usb_charging = true;
1513 static int ideapad_acpi_add(struct platform_device *pdev)
1515 struct ideapad_private *priv;
1516 struct acpi_device *adev;
1521 err = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
1525 if (eval_int(adev->handle, "_CFG", &cfg))
1528 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1532 dev_set_drvdata(&pdev->dev, priv);
1536 priv->platform_device = pdev;
1538 ideapad_check_features(priv);
1540 err = ideapad_sysfs_init(priv);
1544 ideapad_debugfs_init(priv);
1546 err = ideapad_input_init(priv);
1550 err = ideapad_kbd_bl_init(priv);
1553 dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
1555 dev_info(&pdev->dev, "Keyboard backlight control not available\n");
1559 * On some models without a hw-switch (the yoga 2 13 at least)
1560 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1562 if (!priv->features.hw_rfkill_switch)
1563 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1565 /* The same for Touchpad */
1566 if (!priv->features.touchpad_ctrl_via_ec)
1567 write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, 1);
1569 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1570 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1571 ideapad_register_rfkill(priv, i);
1573 ideapad_sync_rfk_state(priv);
1574 ideapad_sync_touchpad_state(priv);
1576 err = ideapad_dytc_profile_init(priv);
1579 dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
1581 dev_info(&pdev->dev, "DYTC interface is not available\n");
1584 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1585 err = ideapad_backlight_init(priv);
1586 if (err && err != -ENODEV)
1587 goto backlight_failed;
1590 status = acpi_install_notify_handler(adev->handle,
1592 ideapad_acpi_notify, priv);
1593 if (ACPI_FAILURE(status)) {
1595 goto notification_failed;
1598 #if IS_ENABLED(CONFIG_ACPI_WMI)
1599 for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
1600 status = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
1601 ideapad_wmi_notify, priv);
1602 if (ACPI_SUCCESS(status)) {
1603 priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
1608 if (ACPI_FAILURE(status) && status != AE_NOT_EXIST) {
1610 goto notification_failed_wmi;
1616 #if IS_ENABLED(CONFIG_ACPI_WMI)
1617 notification_failed_wmi:
1618 acpi_remove_notify_handler(priv->adev->handle,
1620 ideapad_acpi_notify);
1623 notification_failed:
1624 ideapad_backlight_exit(priv);
1627 ideapad_dytc_profile_exit(priv);
1629 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1630 ideapad_unregister_rfkill(priv, i);
1632 ideapad_kbd_bl_exit(priv);
1633 ideapad_input_exit(priv);
1636 ideapad_debugfs_exit(priv);
1637 ideapad_sysfs_exit(priv);
1642 static int ideapad_acpi_remove(struct platform_device *pdev)
1644 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1647 #if IS_ENABLED(CONFIG_ACPI_WMI)
1648 if (priv->fnesc_guid)
1649 wmi_remove_notify_handler(priv->fnesc_guid);
1652 acpi_remove_notify_handler(priv->adev->handle,
1654 ideapad_acpi_notify);
1656 ideapad_backlight_exit(priv);
1657 ideapad_dytc_profile_exit(priv);
1659 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1660 ideapad_unregister_rfkill(priv, i);
1662 ideapad_kbd_bl_exit(priv);
1663 ideapad_input_exit(priv);
1664 ideapad_debugfs_exit(priv);
1665 ideapad_sysfs_exit(priv);
1670 #ifdef CONFIG_PM_SLEEP
1671 static int ideapad_acpi_resume(struct device *dev)
1673 struct ideapad_private *priv = dev_get_drvdata(dev);
1675 ideapad_sync_rfk_state(priv);
1676 ideapad_sync_touchpad_state(priv);
1679 dytc_profile_refresh(priv);
1684 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1686 static const struct acpi_device_id ideapad_device_ids[] = {
1690 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1692 static struct platform_driver ideapad_acpi_driver = {
1693 .probe = ideapad_acpi_add,
1694 .remove = ideapad_acpi_remove,
1696 .name = "ideapad_acpi",
1698 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1702 module_platform_driver(ideapad_acpi_driver);
1705 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1706 MODULE_LICENSE("GPL");