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 void ideapad_debugfs_init(struct ideapad_private
*priv
)
323 dir
= debugfs_create_dir("ideapad", NULL
);
326 debugfs_create_file("cfg", S_IRUGO
, dir
, priv
, &debugfs_cfg_fops
);
327 debugfs_create_file("status", S_IRUGO
, dir
, priv
, &debugfs_status_fops
);
330 static void ideapad_debugfs_exit(struct ideapad_private
*priv
)
332 debugfs_remove_recursive(priv
->debug
);
339 static ssize_t
show_ideapad_cam(struct device
*dev
,
340 struct device_attribute
*attr
,
343 unsigned long result
;
344 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
346 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_CAMERA
, &result
))
347 return sprintf(buf
, "-1\n");
348 return sprintf(buf
, "%lu\n", result
);
351 static ssize_t
store_ideapad_cam(struct device
*dev
,
352 struct device_attribute
*attr
,
353 const char *buf
, size_t count
)
356 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
360 if (sscanf(buf
, "%i", &state
) != 1)
362 ret
= write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_CAMERA
, state
);
368 static DEVICE_ATTR(camera_power
, 0644, show_ideapad_cam
, store_ideapad_cam
);
370 static ssize_t
show_ideapad_fan(struct device
*dev
,
371 struct device_attribute
*attr
,
374 unsigned long result
;
375 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
377 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_FAN
, &result
))
378 return sprintf(buf
, "-1\n");
379 return sprintf(buf
, "%lu\n", result
);
382 static ssize_t
store_ideapad_fan(struct device
*dev
,
383 struct device_attribute
*attr
,
384 const char *buf
, size_t count
)
387 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
391 if (sscanf(buf
, "%i", &state
) != 1)
393 if (state
< 0 || state
> 4 || state
== 3)
395 ret
= write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_FAN
, state
);
401 static DEVICE_ATTR(fan_mode
, 0644, show_ideapad_fan
, store_ideapad_fan
);
403 static ssize_t
touchpad_show(struct device
*dev
,
404 struct device_attribute
*attr
,
407 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
408 unsigned long result
;
410 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_TOUCHPAD
, &result
))
411 return sprintf(buf
, "-1\n");
412 return sprintf(buf
, "%lu\n", result
);
415 /* Switch to RO for now: It might be revisited in the future */
416 static ssize_t __maybe_unused
touchpad_store(struct device
*dev
,
417 struct device_attribute
*attr
,
418 const char *buf
, size_t count
)
420 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
424 ret
= kstrtobool(buf
, &state
);
428 ret
= write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_TOUCHPAD
, state
);
434 static DEVICE_ATTR_RO(touchpad
);
436 static ssize_t
conservation_mode_show(struct device
*dev
,
437 struct device_attribute
*attr
,
440 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
441 unsigned long result
;
443 if (method_gbmd(priv
->adev
->handle
, &result
))
444 return sprintf(buf
, "-1\n");
445 return sprintf(buf
, "%u\n", test_bit(BM_CONSERVATION_BIT
, &result
));
448 static ssize_t
conservation_mode_store(struct device
*dev
,
449 struct device_attribute
*attr
,
450 const char *buf
, size_t count
)
452 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
456 ret
= kstrtobool(buf
, &state
);
460 ret
= method_int1(priv
->adev
->handle
, "SBMC", state
?
461 BMCMD_CONSERVATION_ON
:
462 BMCMD_CONSERVATION_OFF
);
468 static DEVICE_ATTR_RW(conservation_mode
);
470 static ssize_t
fn_lock_show(struct device
*dev
,
471 struct device_attribute
*attr
,
474 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
475 unsigned long result
;
477 int fail
= read_method_int(priv
->adev
->handle
, "HALS", &hals
);
480 return sprintf(buf
, "-1\n");
483 return sprintf(buf
, "%u\n", test_bit(HA_FNLOCK_BIT
, &result
));
486 static ssize_t
fn_lock_store(struct device
*dev
,
487 struct device_attribute
*attr
,
488 const char *buf
, size_t count
)
490 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
494 ret
= kstrtobool(buf
, &state
);
498 ret
= method_int1(priv
->adev
->handle
, "SALS", state
?
506 static DEVICE_ATTR_RW(fn_lock
);
509 static struct attribute
*ideapad_attributes
[] = {
510 &dev_attr_camera_power
.attr
,
511 &dev_attr_fan_mode
.attr
,
512 &dev_attr_touchpad
.attr
,
513 &dev_attr_conservation_mode
.attr
,
514 &dev_attr_fn_lock
.attr
,
518 static umode_t
ideapad_is_visible(struct kobject
*kobj
,
519 struct attribute
*attr
,
522 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
523 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
526 if (attr
== &dev_attr_camera_power
.attr
)
527 supported
= test_bit(CFG_CAMERA_BIT
, &(priv
->cfg
));
528 else if (attr
== &dev_attr_fan_mode
.attr
) {
530 supported
= !read_ec_data(priv
->adev
->handle
, VPCCMD_R_FAN
,
532 } else if (attr
== &dev_attr_conservation_mode
.attr
) {
533 supported
= acpi_has_method(priv
->adev
->handle
, "GBMD") &&
534 acpi_has_method(priv
->adev
->handle
, "SBMC");
535 } else if (attr
== &dev_attr_fn_lock
.attr
) {
536 supported
= acpi_has_method(priv
->adev
->handle
, "HALS") &&
537 acpi_has_method(priv
->adev
->handle
, "SALS");
541 return supported
? attr
->mode
: 0;
544 static const struct attribute_group ideapad_attribute_group
= {
545 .is_visible
= ideapad_is_visible
,
546 .attrs
= ideapad_attributes
552 struct ideapad_rfk_data
{
559 static const struct ideapad_rfk_data ideapad_rfk_data
[] = {
560 { "ideapad_wlan", CFG_WIFI_BIT
, VPCCMD_W_WIFI
, RFKILL_TYPE_WLAN
},
561 { "ideapad_bluetooth", CFG_BT_BIT
, VPCCMD_W_BT
, RFKILL_TYPE_BLUETOOTH
},
562 { "ideapad_3g", CFG_3G_BIT
, VPCCMD_W_3G
, RFKILL_TYPE_WWAN
},
565 static int ideapad_rfk_set(void *data
, bool blocked
)
567 struct ideapad_rfk_priv
*priv
= data
;
568 int opcode
= ideapad_rfk_data
[priv
->dev
].opcode
;
570 return write_ec_cmd(priv
->priv
->adev
->handle
, opcode
, !blocked
);
573 static const struct rfkill_ops ideapad_rfk_ops
= {
574 .set_block
= ideapad_rfk_set
,
577 static void ideapad_sync_rfk_state(struct ideapad_private
*priv
)
579 unsigned long hw_blocked
= 0;
582 if (priv
->has_hw_rfkill_switch
) {
583 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_RF
, &hw_blocked
))
585 hw_blocked
= !hw_blocked
;
588 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
590 rfkill_set_hw_state(priv
->rfk
[i
], hw_blocked
);
593 static int ideapad_register_rfkill(struct ideapad_private
*priv
, int dev
)
596 unsigned long sw_blocked
;
599 (ideapad_rfk_data
[dev
].type
== RFKILL_TYPE_BLUETOOTH
)) {
600 /* Force to enable bluetooth when no_bt_rfkill=1 */
601 write_ec_cmd(priv
->adev
->handle
,
602 ideapad_rfk_data
[dev
].opcode
, 1);
605 priv
->rfk_priv
[dev
].dev
= dev
;
606 priv
->rfk_priv
[dev
].priv
= priv
;
608 priv
->rfk
[dev
] = rfkill_alloc(ideapad_rfk_data
[dev
].name
,
609 &priv
->platform_device
->dev
,
610 ideapad_rfk_data
[dev
].type
,
612 &priv
->rfk_priv
[dev
]);
616 if (read_ec_data(priv
->adev
->handle
, ideapad_rfk_data
[dev
].opcode
-1,
618 rfkill_init_sw_state(priv
->rfk
[dev
], 0);
620 sw_blocked
= !sw_blocked
;
621 rfkill_init_sw_state(priv
->rfk
[dev
], sw_blocked
);
624 ret
= rfkill_register(priv
->rfk
[dev
]);
626 rfkill_destroy(priv
->rfk
[dev
]);
632 static void ideapad_unregister_rfkill(struct ideapad_private
*priv
, int dev
)
637 rfkill_unregister(priv
->rfk
[dev
]);
638 rfkill_destroy(priv
->rfk
[dev
]);
644 static int ideapad_sysfs_init(struct ideapad_private
*priv
)
646 return sysfs_create_group(&priv
->platform_device
->dev
.kobj
,
647 &ideapad_attribute_group
);
650 static void ideapad_sysfs_exit(struct ideapad_private
*priv
)
652 sysfs_remove_group(&priv
->platform_device
->dev
.kobj
,
653 &ideapad_attribute_group
);
659 static const struct key_entry ideapad_keymap
[] = {
660 { KE_KEY
, 6, { KEY_SWITCHVIDEOMODE
} },
661 { KE_KEY
, 7, { KEY_CAMERA
} },
662 { KE_KEY
, 8, { KEY_MICMUTE
} },
663 { KE_KEY
, 11, { KEY_F16
} },
664 { KE_KEY
, 13, { KEY_WLAN
} },
665 { KE_KEY
, 16, { KEY_PROG1
} },
666 { KE_KEY
, 17, { KEY_PROG2
} },
667 { KE_KEY
, 64, { KEY_PROG3
} },
668 { KE_KEY
, 65, { KEY_PROG4
} },
669 { KE_KEY
, 66, { KEY_TOUCHPAD_OFF
} },
670 { KE_KEY
, 67, { KEY_TOUCHPAD_ON
} },
671 { KE_KEY
, 128, { KEY_ESC
} },
676 static int ideapad_input_init(struct ideapad_private
*priv
)
678 struct input_dev
*inputdev
;
681 inputdev
= input_allocate_device();
685 inputdev
->name
= "Ideapad extra buttons";
686 inputdev
->phys
= "ideapad/input0";
687 inputdev
->id
.bustype
= BUS_HOST
;
688 inputdev
->dev
.parent
= &priv
->platform_device
->dev
;
690 error
= sparse_keymap_setup(inputdev
, ideapad_keymap
, NULL
);
692 pr_err("Unable to setup input device keymap\n");
696 error
= input_register_device(inputdev
);
698 pr_err("Unable to register input device\n");
702 priv
->inputdev
= inputdev
;
706 input_free_device(inputdev
);
710 static void ideapad_input_exit(struct ideapad_private
*priv
)
712 input_unregister_device(priv
->inputdev
);
713 priv
->inputdev
= NULL
;
716 static void ideapad_input_report(struct ideapad_private
*priv
,
717 unsigned long scancode
)
719 sparse_keymap_report_event(priv
->inputdev
, scancode
, 1, true);
722 static void ideapad_input_novokey(struct ideapad_private
*priv
)
724 unsigned long long_pressed
;
726 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_NOVO
, &long_pressed
))
729 ideapad_input_report(priv
, 17);
731 ideapad_input_report(priv
, 16);
734 static void ideapad_check_special_buttons(struct ideapad_private
*priv
)
736 unsigned long bit
, value
;
738 read_ec_data(priv
->adev
->handle
, VPCCMD_R_SPECIAL_BUTTONS
, &value
);
740 for (bit
= 0; bit
< 16; bit
++) {
741 if (test_bit(bit
, &value
)) {
745 /* Thermal Management button */
746 ideapad_input_report(priv
, 65);
749 /* OneKey Theater button */
750 ideapad_input_report(priv
, 64);
753 pr_info("Unknown special button: %lu\n", bit
);
763 static int ideapad_backlight_get_brightness(struct backlight_device
*blightdev
)
765 struct ideapad_private
*priv
= bl_get_data(blightdev
);
771 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL
, &now
))
776 static int ideapad_backlight_update_status(struct backlight_device
*blightdev
)
778 struct ideapad_private
*priv
= bl_get_data(blightdev
);
783 if (write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_BL
,
784 blightdev
->props
.brightness
))
786 if (write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_BL_POWER
,
787 blightdev
->props
.power
== FB_BLANK_POWERDOWN
? 0 : 1))
793 static const struct backlight_ops ideapad_backlight_ops
= {
794 .get_brightness
= ideapad_backlight_get_brightness
,
795 .update_status
= ideapad_backlight_update_status
,
798 static int ideapad_backlight_init(struct ideapad_private
*priv
)
800 struct backlight_device
*blightdev
;
801 struct backlight_properties props
;
802 unsigned long max
, now
, power
;
804 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL_MAX
, &max
))
806 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL
, &now
))
808 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL_POWER
, &power
))
811 memset(&props
, 0, sizeof(struct backlight_properties
));
812 props
.max_brightness
= max
;
813 props
.type
= BACKLIGHT_PLATFORM
;
814 blightdev
= backlight_device_register("ideapad",
815 &priv
->platform_device
->dev
,
817 &ideapad_backlight_ops
,
819 if (IS_ERR(blightdev
)) {
820 pr_err("Could not register backlight device\n");
821 return PTR_ERR(blightdev
);
824 priv
->blightdev
= blightdev
;
825 blightdev
->props
.brightness
= now
;
826 blightdev
->props
.power
= power
? FB_BLANK_UNBLANK
: FB_BLANK_POWERDOWN
;
827 backlight_update_status(blightdev
);
832 static void ideapad_backlight_exit(struct ideapad_private
*priv
)
834 backlight_device_unregister(priv
->blightdev
);
835 priv
->blightdev
= NULL
;
838 static void ideapad_backlight_notify_power(struct ideapad_private
*priv
)
841 struct backlight_device
*blightdev
= priv
->blightdev
;
845 if (read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL_POWER
, &power
))
847 blightdev
->props
.power
= power
? FB_BLANK_UNBLANK
: FB_BLANK_POWERDOWN
;
850 static void ideapad_backlight_notify_brightness(struct ideapad_private
*priv
)
854 /* if we control brightness via acpi video driver */
855 if (priv
->blightdev
== NULL
) {
856 read_ec_data(priv
->adev
->handle
, VPCCMD_R_BL
, &now
);
860 backlight_force_update(priv
->blightdev
, BACKLIGHT_UPDATE_HOTKEY
);
866 static void ideapad_sync_touchpad_state(struct ideapad_private
*priv
)
870 /* Without reading from EC touchpad LED doesn't switch state */
871 if (!read_ec_data(priv
->adev
->handle
, VPCCMD_R_TOUCHPAD
, &value
)) {
872 /* Some IdeaPads don't really turn off touchpad - they only
873 * switch the LED state. We (de)activate KBC AUX port to turn
874 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
875 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
877 i8042_command(¶m
, value
? I8042_CMD_AUX_ENABLE
:
878 I8042_CMD_AUX_DISABLE
);
879 ideapad_input_report(priv
, value
? 67 : 66);
883 static void ideapad_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
885 struct ideapad_private
*priv
= data
;
886 unsigned long vpc1
, vpc2
, vpc_bit
;
888 if (read_ec_data(handle
, VPCCMD_R_VPC1
, &vpc1
))
890 if (read_ec_data(handle
, VPCCMD_R_VPC2
, &vpc2
))
893 vpc1
= (vpc2
<< 8) | vpc1
;
894 for (vpc_bit
= 0; vpc_bit
< 16; vpc_bit
++) {
895 if (test_bit(vpc_bit
, &vpc1
)) {
898 ideapad_sync_rfk_state(priv
);
905 ideapad_input_report(priv
, vpc_bit
);
908 ideapad_sync_touchpad_state(priv
);
911 ideapad_backlight_notify_brightness(priv
);
914 ideapad_input_novokey(priv
);
917 ideapad_backlight_notify_power(priv
);
920 ideapad_check_special_buttons(priv
);
923 /* Some IdeaPads report event 1 every ~20
924 * seconds while on battery power; some
925 * report this when changing to/from tablet
926 * mode. Squelch this event.
930 pr_info("Unknown event: %lu\n", vpc_bit
);
936 #if IS_ENABLED(CONFIG_ACPI_WMI)
937 static void ideapad_wmi_notify(u32 value
, void *context
)
941 ideapad_input_report(context
, value
);
944 pr_info("Unknown WMI event %u\n", value
);
950 * Some ideapads have a hardware rfkill switch, but most do not have one.
951 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
952 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
953 * There used to be a long list of DMI ids for models without a hw rfkill
954 * switch here, but that resulted in playing whack a mole.
955 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
956 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
957 * hw-blocked results in an empty SSID list, which is a much more benign
959 * So the default now is the much safer option of assuming there is no
960 * hardware rfkill switch. This default also actually matches most hardware,
961 * since having a hw rfkill switch is quite rare on modern hardware, so this
962 * also leads to a much shorter list.
964 static const struct dmi_system_id hw_rfkill_list
[] = {
968 static int ideapad_acpi_add(struct platform_device
*pdev
)
972 struct ideapad_private
*priv
;
973 struct acpi_device
*adev
;
975 ret
= acpi_bus_get_device(ACPI_HANDLE(&pdev
->dev
), &adev
);
979 if (read_method_int(adev
->handle
, "_CFG", &cfg
))
982 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
986 dev_set_drvdata(&pdev
->dev
, priv
);
989 priv
->platform_device
= pdev
;
990 priv
->has_hw_rfkill_switch
= dmi_check_system(hw_rfkill_list
);
992 ret
= ideapad_sysfs_init(priv
);
996 ideapad_debugfs_init(priv
);
998 ret
= ideapad_input_init(priv
);
1003 * On some models without a hw-switch (the yoga 2 13 at least)
1004 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1006 if (!priv
->has_hw_rfkill_switch
)
1007 write_ec_cmd(priv
->adev
->handle
, VPCCMD_W_RF
, 1);
1009 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
1010 if (test_bit(ideapad_rfk_data
[i
].cfgbit
, &priv
->cfg
))
1011 ideapad_register_rfkill(priv
, i
);
1013 ideapad_sync_rfk_state(priv
);
1014 ideapad_sync_touchpad_state(priv
);
1016 if (acpi_video_get_backlight_type() == acpi_backlight_vendor
) {
1017 ret
= ideapad_backlight_init(priv
);
1018 if (ret
&& ret
!= -ENODEV
)
1019 goto backlight_failed
;
1021 ret
= acpi_install_notify_handler(adev
->handle
,
1022 ACPI_DEVICE_NOTIFY
, ideapad_acpi_notify
, priv
);
1024 goto notification_failed
;
1026 #if IS_ENABLED(CONFIG_ACPI_WMI)
1027 for (i
= 0; i
< ARRAY_SIZE(ideapad_wmi_fnesc_events
); i
++) {
1028 ret
= wmi_install_notify_handler(ideapad_wmi_fnesc_events
[i
],
1029 ideapad_wmi_notify
, priv
);
1031 priv
->fnesc_guid
= ideapad_wmi_fnesc_events
[i
];
1035 if (ret
!= AE_OK
&& ret
!= AE_NOT_EXIST
)
1036 goto notification_failed_wmi
;
1040 #if IS_ENABLED(CONFIG_ACPI_WMI)
1041 notification_failed_wmi
:
1042 acpi_remove_notify_handler(priv
->adev
->handle
,
1043 ACPI_DEVICE_NOTIFY
, ideapad_acpi_notify
);
1045 notification_failed
:
1046 ideapad_backlight_exit(priv
);
1048 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
1049 ideapad_unregister_rfkill(priv
, i
);
1050 ideapad_input_exit(priv
);
1052 ideapad_debugfs_exit(priv
);
1053 ideapad_sysfs_exit(priv
);
1057 static int ideapad_acpi_remove(struct platform_device
*pdev
)
1059 struct ideapad_private
*priv
= dev_get_drvdata(&pdev
->dev
);
1062 #if IS_ENABLED(CONFIG_ACPI_WMI)
1063 if (priv
->fnesc_guid
)
1064 wmi_remove_notify_handler(priv
->fnesc_guid
);
1066 acpi_remove_notify_handler(priv
->adev
->handle
,
1067 ACPI_DEVICE_NOTIFY
, ideapad_acpi_notify
);
1068 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
);
1072 ideapad_debugfs_exit(priv
);
1073 ideapad_sysfs_exit(priv
);
1074 dev_set_drvdata(&pdev
->dev
, NULL
);
1079 #ifdef CONFIG_PM_SLEEP
1080 static int ideapad_acpi_resume(struct device
*device
)
1082 struct ideapad_private
*priv
;
1086 priv
= dev_get_drvdata(device
);
1088 ideapad_sync_rfk_state(priv
);
1089 ideapad_sync_touchpad_state(priv
);
1093 static SIMPLE_DEV_PM_OPS(ideapad_pm
, NULL
, ideapad_acpi_resume
);
1095 static const struct acpi_device_id ideapad_device_ids
[] = {
1099 MODULE_DEVICE_TABLE(acpi
, ideapad_device_ids
);
1101 static struct platform_driver ideapad_acpi_driver
= {
1102 .probe
= ideapad_acpi_add
,
1103 .remove
= ideapad_acpi_remove
,
1105 .name
= "ideapad_acpi",
1107 .acpi_match_table
= ACPI_PTR(ideapad_device_ids
),
1111 module_platform_driver(ideapad_acpi_driver
);
1113 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1114 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1115 MODULE_LICENSE("GPL");