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/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/acpi.h>
16 #include <linux/rfkill.h>
17 #include <linux/platform_device.h>
18 #include <linux/input.h>
19 #include <linux/input/sparse-keymap.h>
20 #include <linux/backlight.h>
22 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
24 #include <linux/i8042.h>
25 #include <linux/dmi.h>
26 #include <linux/device.h>
27 #include <acpi/video.h>
29 #define IDEAPAD_RFKILL_DEV_NUM (3)
31 #define BM_CONSERVATION_BIT (5)
32 #define HA_FNLOCK_BIT (10)
34 #define CFG_BT_BIT (16)
35 #define CFG_3G_BIT (17)
36 #define CFG_WIFI_BIT (18)
37 #define CFG_CAMERA_BIT (19)
39 #if IS_ENABLED(CONFIG_ACPI_WMI)
40 static const char *const ideapad_wmi_fnesc_events
[] = {
41 "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
42 "56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
47 BMCMD_CONSERVATION_ON
= 3,
48 BMCMD_CONSERVATION_OFF
= 5,
49 HACMD_FNLOCK_ON
= 0xe,
50 HACMD_FNLOCK_OFF
= 0xf,
71 VPCCMD_R_ODD
, /* 0x21 */
76 VPCCMD_R_SPECIAL_BUTTONS
= 0x31,
77 VPCCMD_W_BL_POWER
= 0x33,
80 struct ideapad_rfk_priv
{
82 struct ideapad_private
*priv
;
85 struct ideapad_private
{
86 struct acpi_device
*adev
;
87 struct rfkill
*rfk
[IDEAPAD_RFKILL_DEV_NUM
];
88 struct ideapad_rfk_priv rfk_priv
[IDEAPAD_RFKILL_DEV_NUM
];
89 struct platform_device
*platform_device
;
90 struct input_dev
*inputdev
;
91 struct backlight_device
*blightdev
;
94 bool has_hw_rfkill_switch
;
95 const char *fnesc_guid
;
98 static bool no_bt_rfkill
;
99 module_param(no_bt_rfkill
, bool, 0444);
100 MODULE_PARM_DESC(no_bt_rfkill
, "No rfkill for bluetooth.");
105 #define IDEAPAD_EC_TIMEOUT (200) /* in ms */
107 static int read_method_int(acpi_handle handle
, const char *method
, int *val
)
110 unsigned long long result
;
112 status
= acpi_evaluate_integer(handle
, (char *)method
, NULL
, &result
);
113 if (ACPI_FAILURE(status
)) {
122 static int method_gbmd(acpi_handle handle
, unsigned long *ret
)
126 result
= read_method_int(handle
, "GBMD", &val
);
131 static int method_int1(acpi_handle handle
, char *method
, int cmd
)
135 status
= acpi_execute_simple_method(handle
, method
, cmd
);
136 return ACPI_FAILURE(status
) ? -1 : 0;
139 static int method_vpcr(acpi_handle handle
, int cmd
, int *ret
)
142 unsigned long long result
;
143 struct acpi_object_list params
;
144 union acpi_object in_obj
;
147 params
.pointer
= &in_obj
;
148 in_obj
.type
= ACPI_TYPE_INTEGER
;
149 in_obj
.integer
.value
= cmd
;
151 status
= acpi_evaluate_integer(handle
, "VPCR", ¶ms
, &result
);
153 if (ACPI_FAILURE(status
)) {
162 static int method_vpcw(acpi_handle handle
, int cmd
, int data
)
164 struct acpi_object_list params
;
165 union acpi_object in_obj
[2];
169 params
.pointer
= in_obj
;
170 in_obj
[0].type
= ACPI_TYPE_INTEGER
;
171 in_obj
[0].integer
.value
= cmd
;
172 in_obj
[1].type
= ACPI_TYPE_INTEGER
;
173 in_obj
[1].integer
.value
= data
;
175 status
= acpi_evaluate_object(handle
, "VPCW", ¶ms
, NULL
);
181 static int read_ec_data(acpi_handle handle
, int cmd
, unsigned long *data
)
184 unsigned long int end_jiffies
;
186 if (method_vpcw(handle
, 1, cmd
))
189 for (end_jiffies
= jiffies
+(HZ
)*IDEAPAD_EC_TIMEOUT
/1000+1;
190 time_before(jiffies
, end_jiffies
);) {
192 if (method_vpcr(handle
, 1, &val
))
195 if (method_vpcr(handle
, 0, &val
))
201 pr_err("timeout in %s\n", __func__
);
205 static int write_ec_cmd(acpi_handle handle
, int cmd
, unsigned long data
)
208 unsigned long int end_jiffies
;
210 if (method_vpcw(handle
, 0, data
))
212 if (method_vpcw(handle
, 1, cmd
))
215 for (end_jiffies
= jiffies
+(HZ
)*IDEAPAD_EC_TIMEOUT
/1000+1;
216 time_before(jiffies
, end_jiffies
);) {
218 if (method_vpcr(handle
, 1, &val
))
223 pr_err("timeout in %s\n", __func__
);
230 static int debugfs_status_show(struct seq_file
*s
, void *data
)
232 struct ideapad_private
*priv
= s
->private;
238 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL_MAX
, &value
))
239 seq_printf(s
, "Backlight max:\t%lu\n", value
);
240 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL
, &value
))
241 seq_printf(s
, "Backlight now:\t%lu\n", value
);
242 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL_POWER
, &value
))
243 seq_printf(s
, "BL power value:\t%s\n", value
? "On" : "Off");
244 seq_printf(s
, "=====================\n");
246 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_RF
, &value
))
247 seq_printf(s
, "Radio status:\t%s(%lu)\n",
248 value
? "On" : "Off", value
);
249 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_WIFI
, &value
))
250 seq_printf(s
, "Wifi status:\t%s(%lu)\n",
251 value
? "On" : "Off", value
);
252 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_BT
, &value
))
253 seq_printf(s
, "BT status:\t%s(%lu)\n",
254 value
? "On" : "Off", value
);
255 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_3G
, &value
))
256 seq_printf(s
, "3G status:\t%s(%lu)\n",
257 value
? "On" : "Off", value
);
258 seq_printf(s
, "=====================\n");
260 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_TOUCHPAD
, &value
))
261 seq_printf(s
, "Touchpad status:%s(%lu)\n",
262 value
? "On" : "Off", value
);
263 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_CAMERA
, &value
))
264 seq_printf(s
, "Camera status:\t%s(%lu)\n",
265 value
? "On" : "Off", value
);
266 seq_puts(s
, "=====================\n");
268 if (!method_gbmd(priv
->adev
->handle
, &value
)) {
269 seq_printf(s
, "Conservation mode:\t%s(%lu)\n",
270 test_bit(BM_CONSERVATION_BIT
, &value
) ? "On" : "Off",
276 DEFINE_SHOW_ATTRIBUTE(debugfs_status
);
278 static int debugfs_cfg_show(struct seq_file
*s
, void *data
)
280 struct ideapad_private
*priv
= s
->private;
283 seq_printf(s
, "cfg: N/A\n");
285 seq_printf(s
, "cfg: 0x%.8lX\n\nCapability: ",
287 if (test_bit(CFG_BT_BIT
, &priv
->cfg
))
288 seq_printf(s
, "Bluetooth ");
289 if (test_bit(CFG_3G_BIT
, &priv
->cfg
))
290 seq_printf(s
, "3G ");
291 if (test_bit(CFG_WIFI_BIT
, &priv
->cfg
))
292 seq_printf(s
, "Wireless ");
293 if (test_bit(CFG_CAMERA_BIT
, &priv
->cfg
))
294 seq_printf(s
, "Camera ");
295 seq_printf(s
, "\nGraphic: ");
296 switch ((priv
->cfg
)&0x700) {
298 seq_printf(s
, "Intel");
301 seq_printf(s
, "ATI");
304 seq_printf(s
, "Nvidia");
307 seq_printf(s
, "Intel and ATI");
310 seq_printf(s
, "Intel and Nvidia");
317 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg
);
319 static int ideapad_debugfs_init(struct ideapad_private
*priv
)
323 priv
->debug
= debugfs_create_dir("ideapad", NULL
);
324 if (priv
->debug
== NULL
) {
325 pr_err("failed to create debugfs directory");
329 node
= debugfs_create_file("cfg", S_IRUGO
, priv
->debug
, priv
,
332 pr_err("failed to create cfg in debugfs");
336 node
= debugfs_create_file("status", S_IRUGO
, priv
->debug
, priv
,
337 &debugfs_status_fops
);
339 pr_err("failed to create status in debugfs");
349 static void ideapad_debugfs_exit(struct ideapad_private
*priv
)
351 debugfs_remove_recursive(priv
->debug
);
358 static ssize_t
show_ideapad_cam(struct device
*dev
,
359 struct device_attribute
*attr
,
362 unsigned long result
;
363 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
365 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_CAMERA
, &result
))
366 return sprintf(buf
, "-1\n");
367 return sprintf(buf
, "%lu\n", result
);
370 static ssize_t
store_ideapad_cam(struct device
*dev
,
371 struct device_attribute
*attr
,
372 const char *buf
, size_t count
)
375 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
379 if (sscanf(buf
, "%i", &state
) != 1)
381 ret
= write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_CAMERA
, state
);
387 static DEVICE_ATTR(camera_power
, 0644, show_ideapad_cam
, store_ideapad_cam
);
389 static ssize_t
show_ideapad_fan(struct device
*dev
,
390 struct device_attribute
*attr
,
393 unsigned long result
;
394 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
396 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_FAN
, &result
))
397 return sprintf(buf
, "-1\n");
398 return sprintf(buf
, "%lu\n", result
);
401 static ssize_t
store_ideapad_fan(struct device
*dev
,
402 struct device_attribute
*attr
,
403 const char *buf
, size_t count
)
406 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
410 if (sscanf(buf
, "%i", &state
) != 1)
412 if (state
< 0 || state
> 4 || state
== 3)
414 ret
= write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_FAN
, state
);
420 static DEVICE_ATTR(fan_mode
, 0644, show_ideapad_fan
, store_ideapad_fan
);
422 static ssize_t
touchpad_show(struct device
*dev
,
423 struct device_attribute
*attr
,
426 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
427 unsigned long result
;
429 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_TOUCHPAD
, &result
))
430 return sprintf(buf
, "-1\n");
431 return sprintf(buf
, "%lu\n", result
);
434 /* Switch to RO for now: It might be revisited in the future */
435 static ssize_t __maybe_unused
touchpad_store(struct device
*dev
,
436 struct device_attribute
*attr
,
437 const char *buf
, size_t count
)
439 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
443 ret
= kstrtobool(buf
, &state
);
447 ret
= write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_TOUCHPAD
, state
);
453 static DEVICE_ATTR_RO(touchpad
);
455 static ssize_t
conservation_mode_show(struct device
*dev
,
456 struct device_attribute
*attr
,
459 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
460 unsigned long result
;
462 if (method_gbmd(priv
->adev
->handle
, &result
))
463 return sprintf(buf
, "-1\n");
464 return sprintf(buf
, "%u\n", test_bit(BM_CONSERVATION_BIT
, &result
));
467 static ssize_t
conservation_mode_store(struct device
*dev
,
468 struct device_attribute
*attr
,
469 const char *buf
, size_t count
)
471 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
475 ret
= kstrtobool(buf
, &state
);
479 ret
= method_int1(priv
->adev
->handle
, "SBMC", state
?
480 BMCMD_CONSERVATION_ON
:
481 BMCMD_CONSERVATION_OFF
);
487 static DEVICE_ATTR_RW(conservation_mode
);
489 static ssize_t
fn_lock_show(struct device
*dev
,
490 struct device_attribute
*attr
,
493 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
494 unsigned long result
;
496 int fail
= read_method_int(priv
->adev
->handle
, "HALS", &hals
);
499 return sprintf(buf
, "-1\n");
502 return sprintf(buf
, "%u\n", test_bit(HA_FNLOCK_BIT
, &result
));
505 static ssize_t
fn_lock_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 ret
= kstrtobool(buf
, &state
);
517 ret
= method_int1(priv
->adev
->handle
, "SALS", state
?
525 static DEVICE_ATTR_RW(fn_lock
);
528 static struct attribute
*ideapad_attributes
[] = {
529 &dev_attr_camera_power
.attr
,
530 &dev_attr_fan_mode
.attr
,
531 &dev_attr_touchpad
.attr
,
532 &dev_attr_conservation_mode
.attr
,
533 &dev_attr_fn_lock
.attr
,
537 static umode_t
ideapad_is_visible(struct kobject
*kobj
,
538 struct attribute
*attr
,
541 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
542 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
545 if (attr
== &dev_attr_camera_power
.attr
)
546 supported
= test_bit(CFG_CAMERA_BIT
, &(priv
->cfg
));
547 else if (attr
== &dev_attr_fan_mode
.attr
) {
549 supported
= !read_ec_data(priv
->adev
->handle
, VPCCMD_R_FAN
,
551 } else if (attr
== &dev_attr_conservation_mode
.attr
) {
552 supported
= acpi_has_method(priv
->adev
->handle
, "GBMD") &&
553 acpi_has_method(priv
->adev
->handle
, "SBMC");
554 } else if (attr
== &dev_attr_fn_lock
.attr
) {
555 supported
= acpi_has_method(priv
->adev
->handle
, "HALS") &&
556 acpi_has_method(priv
->adev
->handle
, "SALS");
560 return supported
? attr
->mode
: 0;
563 static const struct attribute_group ideapad_attribute_group
= {
564 .is_visible
= ideapad_is_visible
,
565 .attrs
= ideapad_attributes
571 struct ideapad_rfk_data
{
578 static const struct ideapad_rfk_data ideapad_rfk_data
[] = {
579 { "ideapad_wlan", CFG_WIFI_BIT
, VPCCMD_W_WIFI
, RFKILL_TYPE_WLAN
},
580 { "ideapad_bluetooth", CFG_BT_BIT
, VPCCMD_W_BT
, RFKILL_TYPE_BLUETOOTH
},
581 { "ideapad_3g", CFG_3G_BIT
, VPCCMD_W_3G
, RFKILL_TYPE_WWAN
},
584 static int ideapad_rfk_set(void *data
, bool blocked
)
586 struct ideapad_rfk_priv
*priv
= data
;
587 int opcode
= ideapad_rfk_data
[priv
->dev
].opcode
;
589 return write_ec_cmd(priv
->priv
->adev
->handle
, opcode
, !blocked
);
592 static const struct rfkill_ops ideapad_rfk_ops
= {
593 .set_block
= ideapad_rfk_set
,
596 static void ideapad_sync_rfk_state(struct ideapad_private
*priv
)
598 unsigned long hw_blocked
= 0;
601 if (priv
->has_hw_rfkill_switch
) {
602 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_RF
, &hw_blocked
))
604 hw_blocked
= !hw_blocked
;
607 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
609 rfkill_set_hw_state(priv
->rfk
[i
], hw_blocked
);
612 static int ideapad_register_rfkill(struct ideapad_private
*priv
, int dev
)
615 unsigned long sw_blocked
;
618 (ideapad_rfk_data
[dev
].type
== RFKILL_TYPE_BLUETOOTH
)) {
619 /* Force to enable bluetooth when no_bt_rfkill=1 */
620 write_ec_cmd(priv
->adev
->handle
,
621 ideapad_rfk_data
[dev
].opcode
, 1);
624 priv
->rfk_priv
[dev
].dev
= dev
;
625 priv
->rfk_priv
[dev
].priv
= priv
;
627 priv
->rfk
[dev
] = rfkill_alloc(ideapad_rfk_data
[dev
].name
,
628 &priv
->platform_device
->dev
,
629 ideapad_rfk_data
[dev
].type
,
631 &priv
->rfk_priv
[dev
]);
635 if (read_ec_data(priv
->adev
->handle
, ideapad_rfk_data
[dev
].opcode
-1,
637 rfkill_init_sw_state(priv
->rfk
[dev
], 0);
639 sw_blocked
= !sw_blocked
;
640 rfkill_init_sw_state(priv
->rfk
[dev
], sw_blocked
);
643 ret
= rfkill_register(priv
->rfk
[dev
]);
645 rfkill_destroy(priv
->rfk
[dev
]);
651 static void ideapad_unregister_rfkill(struct ideapad_private
*priv
, int dev
)
656 rfkill_unregister(priv
->rfk
[dev
]);
657 rfkill_destroy(priv
->rfk
[dev
]);
663 static int ideapad_sysfs_init(struct ideapad_private
*priv
)
665 return sysfs_create_group(&priv
->platform_device
->dev
.kobj
,
666 &ideapad_attribute_group
);
669 static void ideapad_sysfs_exit(struct ideapad_private
*priv
)
671 sysfs_remove_group(&priv
->platform_device
->dev
.kobj
,
672 &ideapad_attribute_group
);
678 static const struct key_entry ideapad_keymap
[] = {
679 { KE_KEY
, 6, { KEY_SWITCHVIDEOMODE
} },
680 { KE_KEY
, 7, { KEY_CAMERA
} },
681 { KE_KEY
, 8, { KEY_MICMUTE
} },
682 { KE_KEY
, 11, { KEY_F16
} },
683 { KE_KEY
, 13, { KEY_WLAN
} },
684 { KE_KEY
, 16, { KEY_PROG1
} },
685 { KE_KEY
, 17, { KEY_PROG2
} },
686 { KE_KEY
, 64, { KEY_PROG3
} },
687 { KE_KEY
, 65, { KEY_PROG4
} },
688 { KE_KEY
, 66, { KEY_TOUCHPAD_OFF
} },
689 { KE_KEY
, 67, { KEY_TOUCHPAD_ON
} },
690 { KE_KEY
, 128, { KEY_ESC
} },
695 static int ideapad_input_init(struct ideapad_private
*priv
)
697 struct input_dev
*inputdev
;
700 inputdev
= input_allocate_device();
704 inputdev
->name
= "Ideapad extra buttons";
705 inputdev
->phys
= "ideapad/input0";
706 inputdev
->id
.bustype
= BUS_HOST
;
707 inputdev
->dev
.parent
= &priv
->platform_device
->dev
;
709 error
= sparse_keymap_setup(inputdev
, ideapad_keymap
, NULL
);
711 pr_err("Unable to setup input device keymap\n");
715 error
= input_register_device(inputdev
);
717 pr_err("Unable to register input device\n");
721 priv
->inputdev
= inputdev
;
725 input_free_device(inputdev
);
729 static void ideapad_input_exit(struct ideapad_private
*priv
)
731 input_unregister_device(priv
->inputdev
);
732 priv
->inputdev
= NULL
;
735 static void ideapad_input_report(struct ideapad_private
*priv
,
736 unsigned long scancode
)
738 sparse_keymap_report_event(priv
->inputdev
, scancode
, 1, true);
741 static void ideapad_input_novokey(struct ideapad_private
*priv
)
743 unsigned long long_pressed
;
745 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_NOVO
, &long_pressed
))
748 ideapad_input_report(priv
, 17);
750 ideapad_input_report(priv
, 16);
753 static void ideapad_check_special_buttons(struct ideapad_private
*priv
)
755 unsigned long bit
, value
;
757 read_ec_data(priv
->adev
->handle
, VPCCMD_R_SPECIAL_BUTTONS
, &value
);
759 for (bit
= 0; bit
< 16; bit
++) {
760 if (test_bit(bit
, &value
)) {
764 /* Thermal Management button */
765 ideapad_input_report(priv
, 65);
768 /* OneKey Theater button */
769 ideapad_input_report(priv
, 64);
772 pr_info("Unknown special button: %lu\n", bit
);
782 static int ideapad_backlight_get_brightness(struct backlight_device
*blightdev
)
784 struct ideapad_private
*priv
= bl_get_data(blightdev
);
790 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL
, &now
))
795 static int ideapad_backlight_update_status(struct backlight_device
*blightdev
)
797 struct ideapad_private
*priv
= bl_get_data(blightdev
);
802 if (write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_BL
,
803 blightdev
->props
.brightness
))
805 if (write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_BL_POWER
,
806 blightdev
->props
.power
== FB_BLANK_POWERDOWN
? 0 : 1))
812 static const struct backlight_ops ideapad_backlight_ops
= {
813 .get_brightness
= ideapad_backlight_get_brightness
,
814 .update_status
= ideapad_backlight_update_status
,
817 static int ideapad_backlight_init(struct ideapad_private
*priv
)
819 struct backlight_device
*blightdev
;
820 struct backlight_properties props
;
821 unsigned long max
, now
, power
;
823 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL_MAX
, &max
))
825 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL
, &now
))
827 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL_POWER
, &power
))
830 memset(&props
, 0, sizeof(struct backlight_properties
));
831 props
.max_brightness
= max
;
832 props
.type
= BACKLIGHT_PLATFORM
;
833 blightdev
= backlight_device_register("ideapad",
834 &priv
->platform_device
->dev
,
836 &ideapad_backlight_ops
,
838 if (IS_ERR(blightdev
)) {
839 pr_err("Could not register backlight device\n");
840 return PTR_ERR(blightdev
);
843 priv
->blightdev
= blightdev
;
844 blightdev
->props
.brightness
= now
;
845 blightdev
->props
.power
= power
? FB_BLANK_UNBLANK
: FB_BLANK_POWERDOWN
;
846 backlight_update_status(blightdev
);
851 static void ideapad_backlight_exit(struct ideapad_private
*priv
)
853 backlight_device_unregister(priv
->blightdev
);
854 priv
->blightdev
= NULL
;
857 static void ideapad_backlight_notify_power(struct ideapad_private
*priv
)
860 struct backlight_device
*blightdev
= priv
->blightdev
;
864 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL_POWER
, &power
))
866 blightdev
->props
.power
= power
? FB_BLANK_UNBLANK
: FB_BLANK_POWERDOWN
;
869 static void ideapad_backlight_notify_brightness(struct ideapad_private
*priv
)
873 /* if we control brightness via acpi video driver */
874 if (priv
->blightdev
== NULL
) {
875 read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL
, &now
);
879 backlight_force_update(priv
->blightdev
, BACKLIGHT_UPDATE_HOTKEY
);
885 static void ideapad_sync_touchpad_state(struct ideapad_private
*priv
)
889 /* Without reading from EC touchpad LED doesn't switch state */
890 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_TOUCHPAD
, &value
)) {
891 /* Some IdeaPads don't really turn off touchpad - they only
892 * switch the LED state. We (de)activate KBC AUX port to turn
893 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
894 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
896 i8042_command(¶m
, value
? I8042_CMD_AUX_ENABLE
:
897 I8042_CMD_AUX_DISABLE
);
898 ideapad_input_report(priv
, value
? 67 : 66);
902 static void ideapad_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
904 struct ideapad_private
*priv
= data
;
905 unsigned long vpc1
, vpc2
, vpc_bit
;
907 if (read_ec_data(handle
, VPCCMD_R_VPC1
, &vpc1
))
909 if (read_ec_data(handle
, VPCCMD_R_VPC2
, &vpc2
))
912 vpc1
= (vpc2
<< 8) | vpc1
;
913 for (vpc_bit
= 0; vpc_bit
< 16; vpc_bit
++) {
914 if (test_bit(vpc_bit
, &vpc1
)) {
917 ideapad_sync_rfk_state(priv
);
924 ideapad_input_report(priv
, vpc_bit
);
927 ideapad_sync_touchpad_state(priv
);
930 ideapad_backlight_notify_brightness(priv
);
933 ideapad_input_novokey(priv
);
936 ideapad_backlight_notify_power(priv
);
939 ideapad_check_special_buttons(priv
);
942 /* Some IdeaPads report event 1 every ~20
943 * seconds while on battery power; some
944 * report this when changing to/from tablet
945 * mode. Squelch this event.
949 pr_info("Unknown event: %lu\n", vpc_bit
);
955 #if IS_ENABLED(CONFIG_ACPI_WMI)
956 static void ideapad_wmi_notify(u32 value
, void *context
)
960 ideapad_input_report(context
, value
);
963 pr_info("Unknown WMI event %u\n", value
);
969 * Some ideapads have a hardware rfkill switch, but most do not have one.
970 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
971 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
972 * There used to be a long list of DMI ids for models without a hw rfkill
973 * switch here, but that resulted in playing whack a mole.
974 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
975 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
976 * hw-blocked results in an empty SSID list, which is a much more benign
978 * So the default now is the much safer option of assuming there is no
979 * hardware rfkill switch. This default also actually matches most hardware,
980 * since having a hw rfkill switch is quite rare on modern hardware, so this
981 * also leads to a much shorter list.
983 static const struct dmi_system_id hw_rfkill_list
[] = {
987 static int ideapad_acpi_add(struct platform_device
*pdev
)
991 struct ideapad_private
*priv
;
992 struct acpi_device
*adev
;
994 ret
= acpi_bus_get_device(ACPI_HANDLE(&pdev
->dev
), &adev
);
998 if (read_method_int(adev
->handle
, "_CFG", &cfg
))
1001 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
1005 dev_set_drvdata(&pdev
->dev
, priv
);
1008 priv
->platform_device
= pdev
;
1009 priv
->has_hw_rfkill_switch
= dmi_check_system(hw_rfkill_list
);
1011 ret
= ideapad_sysfs_init(priv
);
1015 ret
= ideapad_debugfs_init(priv
);
1017 goto debugfs_failed
;
1019 ret
= ideapad_input_init(priv
);
1024 * On some models without a hw-switch (the yoga 2 13 at least)
1025 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1027 if (!priv
->has_hw_rfkill_switch
)
1028 write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_RF
, 1);
1030 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
1031 if (test_bit(ideapad_rfk_data
[i
].cfgbit
, &priv
->cfg
))
1032 ideapad_register_rfkill(priv
, i
);
1034 ideapad_sync_rfk_state(priv
);
1035 ideapad_sync_touchpad_state(priv
);
1037 if (acpi_video_get_backlight_type() == acpi_backlight_vendor
) {
1038 ret
= ideapad_backlight_init(priv
);
1039 if (ret
&& ret
!= -ENODEV
)
1040 goto backlight_failed
;
1042 ret
= acpi_install_notify_handler(adev
->handle
,
1043 ACPI_DEVICE_NOTIFY
, ideapad_acpi_notify
, priv
);
1045 goto notification_failed
;
1047 #if IS_ENABLED(CONFIG_ACPI_WMI)
1048 for (i
= 0; i
< ARRAY_SIZE(ideapad_wmi_fnesc_events
); i
++) {
1049 ret
= wmi_install_notify_handler(ideapad_wmi_fnesc_events
[i
],
1050 ideapad_wmi_notify
, priv
);
1052 priv
->fnesc_guid
= ideapad_wmi_fnesc_events
[i
];
1056 if (ret
!= AE_OK
&& ret
!= AE_NOT_EXIST
)
1057 goto notification_failed_wmi
;
1061 #if IS_ENABLED(CONFIG_ACPI_WMI)
1062 notification_failed_wmi
:
1063 acpi_remove_notify_handler(priv
->adev
->handle
,
1064 ACPI_DEVICE_NOTIFY
, ideapad_acpi_notify
);
1066 notification_failed
:
1067 ideapad_backlight_exit(priv
);
1069 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
1070 ideapad_unregister_rfkill(priv
, i
);
1071 ideapad_input_exit(priv
);
1073 ideapad_debugfs_exit(priv
);
1075 ideapad_sysfs_exit(priv
);
1079 static int ideapad_acpi_remove(struct platform_device
*pdev
)
1081 struct ideapad_private
*priv
= dev_get_drvdata(&pdev
->dev
);
1084 #if IS_ENABLED(CONFIG_ACPI_WMI)
1085 if (priv
->fnesc_guid
)
1086 wmi_remove_notify_handler(priv
->fnesc_guid
);
1088 acpi_remove_notify_handler(priv
->adev
->handle
,
1089 ACPI_DEVICE_NOTIFY
, ideapad_acpi_notify
);
1090 ideapad_backlight_exit(priv
);
1091 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
1092 ideapad_unregister_rfkill(priv
, i
);
1093 ideapad_input_exit(priv
);
1094 ideapad_debugfs_exit(priv
);
1095 ideapad_sysfs_exit(priv
);
1096 dev_set_drvdata(&pdev
->dev
, NULL
);
1101 #ifdef CONFIG_PM_SLEEP
1102 static int ideapad_acpi_resume(struct device
*device
)
1104 struct ideapad_private
*priv
;
1108 priv
= dev_get_drvdata(device
);
1110 ideapad_sync_rfk_state(priv
);
1111 ideapad_sync_touchpad_state(priv
);
1115 static SIMPLE_DEV_PM_OPS(ideapad_pm
, NULL
, ideapad_acpi_resume
);
1117 static const struct acpi_device_id ideapad_device_ids
[] = {
1121 MODULE_DEVICE_TABLE(acpi
, ideapad_device_ids
);
1123 static struct platform_driver ideapad_acpi_driver
= {
1124 .probe
= ideapad_acpi_add
,
1125 .remove
= ideapad_acpi_remove
,
1127 .name
= "ideapad_acpi",
1129 .acpi_match_table
= ACPI_PTR(ideapad_device_ids
),
1133 module_platform_driver(ideapad_acpi_driver
);
1135 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1136 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1137 MODULE_LICENSE("GPL");