xref: /linux/drivers/platform/x86/lenovo/ideapad-laptop.c (revision 9669b2499ea377764f8320dd562dd6cd4ea80a5d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
4  *
5  *  Copyright © 2010 Intel Corporation
6  *  Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/acpi.h>
12 #include <linux/backlight.h>
13 #include <linux/bitfield.h>
14 #include <linux/bitops.h>
15 #include <linux/bug.h>
16 #include <linux/cleanup.h>
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/dmi.h>
21 #include <linux/i8042.h>
22 #include <linux/init.h>
23 #include <linux/input.h>
24 #include <linux/input/sparse-keymap.h>
25 #include <linux/jiffies.h>
26 #include <linux/kernel.h>
27 #include <linux/leds.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/platform_profile.h>
31 #include <linux/power_supply.h>
32 #include <linux/rfkill.h>
33 #include <linux/seq_file.h>
34 #include <linux/sysfs.h>
35 #include <linux/types.h>
36 #include <linux/wmi.h>
37 #include "ideapad-laptop.h"
38 
39 #include <acpi/battery.h>
40 #include <acpi/video.h>
41 
42 #include <dt-bindings/leds/common.h>
43 
44 #define IDEAPAD_RFKILL_DEV_NUM	3
45 
46 enum {
47 	CFG_CAP_BT_BIT       = 16,
48 	CFG_CAP_3G_BIT       = 17,
49 	CFG_CAP_WIFI_BIT     = 18,
50 	CFG_CAP_CAM_BIT      = 19,
51 
52 	/*
53 	 * These are OnScreenDisplay support bits that can be useful to determine
54 	 * whether a hotkey exists/should show OSD. But they aren't particularly
55 	 * meaningful since they were introduced later, i.e. 2010 IdeaPads
56 	 * don't have these, but they still have had OSD for hotkeys.
57 	 */
58 	CFG_OSD_NUMLK_BIT    = 27,
59 	CFG_OSD_CAPSLK_BIT   = 28,
60 	CFG_OSD_MICMUTE_BIT  = 29,
61 	CFG_OSD_TOUCHPAD_BIT = 30,
62 	CFG_OSD_CAM_BIT      = 31,
63 };
64 
65 enum {
66 	GBMD_CONSERVATION_STATE_BIT = 5,
67 };
68 
69 enum {
70 	SBMC_CONSERVATION_ON  = 3,
71 	SBMC_CONSERVATION_OFF = 5,
72 };
73 
74 enum {
75 	HALS_KBD_BL_SUPPORT_BIT       = 4,
76 	HALS_KBD_BL_STATE_BIT         = 5,
77 	HALS_USB_CHARGING_SUPPORT_BIT = 6,
78 	HALS_USB_CHARGING_STATE_BIT   = 7,
79 	HALS_FNLOCK_SUPPORT_BIT       = 9,
80 	HALS_FNLOCK_STATE_BIT         = 10,
81 	HALS_HOTKEYS_PRIMARY_BIT      = 11,
82 };
83 
84 enum {
85 	SALS_KBD_BL_ON        = 0x8,
86 	SALS_KBD_BL_OFF       = 0x9,
87 	SALS_USB_CHARGING_ON  = 0xa,
88 	SALS_USB_CHARGING_OFF = 0xb,
89 	SALS_FNLOCK_ON        = 0xe,
90 	SALS_FNLOCK_OFF       = 0xf,
91 };
92 
93 enum {
94 	VPCCMD_R_VPC1 = 0x10,
95 	VPCCMD_R_BL_MAX,
96 	VPCCMD_R_BL,
97 	VPCCMD_W_BL,
98 	VPCCMD_R_WIFI,
99 	VPCCMD_W_WIFI,
100 	VPCCMD_R_BT,
101 	VPCCMD_W_BT,
102 	VPCCMD_R_BL_POWER,
103 	VPCCMD_R_NOVO,
104 	VPCCMD_R_VPC2,
105 	VPCCMD_R_TOUCHPAD,
106 	VPCCMD_W_TOUCHPAD,
107 	VPCCMD_R_CAMERA,
108 	VPCCMD_W_CAMERA,
109 	VPCCMD_R_3G,
110 	VPCCMD_W_3G,
111 	VPCCMD_R_ODD, /* 0x21 */
112 	VPCCMD_W_FAN,
113 	VPCCMD_R_RF,
114 	VPCCMD_W_RF,
115 	VPCCMD_W_YMC = 0x2A,
116 	VPCCMD_R_FAN = 0x2B,
117 	VPCCMD_R_SPECIAL_BUTTONS = 0x31,
118 	VPCCMD_W_BL_POWER = 0x33,
119 };
120 
121 /*
122  * These correspond to the number of supported states - 1
123  * Future keyboard types may need a new system, if there's a collision
124  * KBD_BL_TRISTATE_AUTO has no way to report or set the auto state
125  * so it effectively has 3 states, but needs to handle 4
126  */
127 enum {
128 	KBD_BL_STANDARD      = 1,
129 	KBD_BL_TRISTATE      = 2,
130 	KBD_BL_TRISTATE_AUTO = 3,
131 };
132 
133 #define KBD_BL_QUERY_TYPE		0x1
134 #define KBD_BL_TRISTATE_TYPE		0x5
135 #define KBD_BL_TRISTATE_AUTO_TYPE	0x7
136 
137 #define KBD_BL_COMMAND_GET		0x2
138 #define KBD_BL_COMMAND_SET		0x3
139 #define KBD_BL_COMMAND_TYPE		GENMASK(7, 4)
140 
141 #define KBD_BL_GET_BRIGHTNESS		GENMASK(15, 1)
142 #define KBD_BL_SET_BRIGHTNESS		GENMASK(19, 16)
143 
144 #define KBD_BL_KBLC_CHANGED_EVENT	12
145 
146 struct ideapad_dytc_priv {
147 	enum platform_profile_option current_profile;
148 	struct device *ppdev; /* platform profile device */
149 	struct mutex mutex; /* protects the DYTC interface */
150 	struct ideapad_private *priv;
151 };
152 
153 struct ideapad_rfk_priv {
154 	int dev;
155 	struct ideapad_private *priv;
156 };
157 
158 struct ideapad_private {
159 	struct acpi_device *adev;
160 	struct mutex vpc_mutex; /* protects the VPC calls */
161 	struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
162 	struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
163 	struct platform_device *platform_device;
164 	struct input_dev *inputdev;
165 	struct backlight_device *blightdev;
166 	struct ideapad_dytc_priv *dytc;
167 	struct dentry *debug;
168 	struct acpi_battery_hook battery_hook;
169 	unsigned long cfg;
170 	unsigned long r_touchpad_val;
171 	struct {
172 		bool conservation_mode    : 1;
173 		bool dytc                 : 1;
174 		bool fan_mode             : 1;
175 		bool fn_lock              : 1;
176 		bool set_fn_lock_led      : 1;
177 		bool hw_rfkill_switch     : 1;
178 		bool kbd_bl               : 1;
179 		bool touchpad_ctrl_via_ec : 1;
180 		bool ctrl_ps2_aux_port    : 1;
181 		bool usb_charging         : 1;
182 		bool ymc_ec_trigger       : 1;
183 	} features;
184 	struct {
185 		bool initialized;
186 		int type;
187 		struct led_classdev led;
188 		unsigned int last_brightness;
189 	} kbd_bl;
190 	struct {
191 		bool initialized;
192 		struct led_classdev led;
193 		unsigned int last_brightness;
194 	} fn_lock;
195 };
196 
197 static bool no_bt_rfkill;
198 module_param(no_bt_rfkill, bool, 0444);
199 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
200 
201 static bool allow_v4_dytc;
202 module_param(allow_v4_dytc, bool, 0444);
203 MODULE_PARM_DESC(allow_v4_dytc,
204 	"Enable DYTC version 4 platform-profile support. "
205 	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
206 
207 static bool hw_rfkill_switch;
208 module_param(hw_rfkill_switch, bool, 0444);
209 MODULE_PARM_DESC(hw_rfkill_switch,
210 	"Enable rfkill support for laptops with a hw on/off wifi switch/slider. "
211 	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
212 
213 static bool set_fn_lock_led;
214 module_param(set_fn_lock_led, bool, 0444);
215 MODULE_PARM_DESC(set_fn_lock_led,
216 	"Enable driver based updates of the fn-lock LED on fn-lock changes. "
217 	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
218 
219 static bool ctrl_ps2_aux_port;
220 module_param(ctrl_ps2_aux_port, bool, 0444);
221 MODULE_PARM_DESC(ctrl_ps2_aux_port,
222 	"Enable driver based PS/2 aux port en-/dis-abling on touchpad on/off toggle. "
223 	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
224 
225 static bool touchpad_ctrl_via_ec;
226 module_param(touchpad_ctrl_via_ec, bool, 0444);
227 MODULE_PARM_DESC(touchpad_ctrl_via_ec,
228 	"Enable registering a 'touchpad' sysfs-attribute which can be used to manually "
229 	"tell the EC to enable/disable the touchpad. This may not work on all models.");
230 
231 static bool ymc_ec_trigger __read_mostly;
232 module_param(ymc_ec_trigger, bool, 0444);
233 MODULE_PARM_DESC(ymc_ec_trigger,
234 	"Enable EC triggering work-around to force emitting tablet mode events. "
235 	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
236 
237 /*
238  * shared data
239  */
240 
241 static struct ideapad_private *ideapad_shared;
242 static DEFINE_MUTEX(ideapad_shared_mutex);
243 
ideapad_shared_init(struct ideapad_private * priv)244 static int ideapad_shared_init(struct ideapad_private *priv)
245 {
246 	int ret;
247 
248 	guard(mutex)(&ideapad_shared_mutex);
249 
250 	if (!ideapad_shared) {
251 		ideapad_shared = priv;
252 		ret = 0;
253 	} else {
254 		dev_warn(&priv->adev->dev, "found multiple platform devices\n");
255 		ret = -EINVAL;
256 	}
257 
258 	return ret;
259 }
260 
ideapad_shared_exit(struct ideapad_private * priv)261 static void ideapad_shared_exit(struct ideapad_private *priv)
262 {
263 	guard(mutex)(&ideapad_shared_mutex);
264 
265 	if (ideapad_shared == priv)
266 		ideapad_shared = NULL;
267 }
268 
269 /*
270  * ACPI Helpers
271  */
272 #define IDEAPAD_EC_TIMEOUT 200 /* in ms */
273 
274 /*
275  * Some models (e.g., ThinkBook since 2024) have a low tolerance for being
276  * polled too frequently. Doing so may break the state machine in the EC,
277  * resulting in a hard shutdown.
278  *
279  * It is also observed that frequent polls may disturb the ongoing operation
280  * and notably delay the availability of EC response.
281  *
282  * These values are used as the delay before the first poll and the interval
283  * between subsequent polls to solve the above issues.
284  */
285 #define IDEAPAD_EC_POLL_MIN_US 150
286 #define IDEAPAD_EC_POLL_MAX_US 300
287 
eval_int(acpi_handle handle,const char * name,unsigned long * res)288 static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
289 {
290 	unsigned long long result;
291 	acpi_status status;
292 
293 	status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
294 	if (ACPI_FAILURE(status))
295 		return -EIO;
296 
297 	*res = result;
298 
299 	return 0;
300 }
301 
eval_int_with_arg(acpi_handle handle,const char * name,unsigned long arg,unsigned long * res)302 static int eval_int_with_arg(acpi_handle handle, const char *name, unsigned long arg,
303 			     unsigned long *res)
304 {
305 	struct acpi_object_list params;
306 	unsigned long long result;
307 	union acpi_object in_obj;
308 	acpi_status status;
309 
310 	params.count = 1;
311 	params.pointer = &in_obj;
312 	in_obj.type = ACPI_TYPE_INTEGER;
313 	in_obj.integer.value = arg;
314 
315 	status = acpi_evaluate_integer(handle, (char *)name, &params, &result);
316 	if (ACPI_FAILURE(status))
317 		return -EIO;
318 
319 	if (res)
320 		*res = result;
321 
322 	return 0;
323 }
324 
exec_simple_method(acpi_handle handle,const char * name,unsigned long arg)325 static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
326 {
327 	acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
328 
329 	return ACPI_FAILURE(status) ? -EIO : 0;
330 }
331 
eval_gbmd(acpi_handle handle,unsigned long * res)332 static int eval_gbmd(acpi_handle handle, unsigned long *res)
333 {
334 	return eval_int(handle, "GBMD", res);
335 }
336 
exec_sbmc(acpi_handle handle,unsigned long arg)337 static int exec_sbmc(acpi_handle handle, unsigned long arg)
338 {
339 	return exec_simple_method(handle, "SBMC", arg);
340 }
341 
eval_hals(acpi_handle handle,unsigned long * res)342 static int eval_hals(acpi_handle handle, unsigned long *res)
343 {
344 	return eval_int(handle, "HALS", res);
345 }
346 
exec_sals(acpi_handle handle,unsigned long arg)347 static int exec_sals(acpi_handle handle, unsigned long arg)
348 {
349 	return exec_simple_method(handle, "SALS", arg);
350 }
351 
exec_kblc(acpi_handle handle,unsigned long arg)352 static int exec_kblc(acpi_handle handle, unsigned long arg)
353 {
354 	return exec_simple_method(handle, "KBLC", arg);
355 }
356 
eval_kblc(acpi_handle handle,unsigned long cmd,unsigned long * res)357 static int eval_kblc(acpi_handle handle, unsigned long cmd, unsigned long *res)
358 {
359 	return eval_int_with_arg(handle, "KBLC", cmd, res);
360 }
361 
eval_dytc(acpi_handle handle,unsigned long cmd,unsigned long * res)362 static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
363 {
364 	return eval_int_with_arg(handle, "DYTC", cmd, res);
365 }
366 
eval_vpcr(acpi_handle handle,unsigned long cmd,unsigned long * res)367 static int eval_vpcr(acpi_handle handle, unsigned long cmd, unsigned long *res)
368 {
369 	return eval_int_with_arg(handle, "VPCR", cmd, res);
370 }
371 
eval_vpcw(acpi_handle handle,unsigned long cmd,unsigned long data)372 static int eval_vpcw(acpi_handle handle, unsigned long cmd, unsigned long data)
373 {
374 	struct acpi_object_list params;
375 	union acpi_object in_obj[2];
376 	acpi_status status;
377 
378 	params.count = 2;
379 	params.pointer = in_obj;
380 	in_obj[0].type = ACPI_TYPE_INTEGER;
381 	in_obj[0].integer.value = cmd;
382 	in_obj[1].type = ACPI_TYPE_INTEGER;
383 	in_obj[1].integer.value = data;
384 
385 	status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
386 	if (ACPI_FAILURE(status))
387 		return -EIO;
388 
389 	return 0;
390 }
391 
read_ec_data(acpi_handle handle,unsigned long cmd,unsigned long * data)392 static int read_ec_data(acpi_handle handle, unsigned long cmd, unsigned long *data)
393 {
394 	unsigned long end_jiffies, val;
395 	int err;
396 
397 	err = eval_vpcw(handle, 1, cmd);
398 	if (err)
399 		return err;
400 
401 	end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
402 
403 	while (time_before(jiffies, end_jiffies)) {
404 		usleep_range(IDEAPAD_EC_POLL_MIN_US, IDEAPAD_EC_POLL_MAX_US);
405 
406 		err = eval_vpcr(handle, 1, &val);
407 		if (err)
408 			return err;
409 
410 		if (val == 0)
411 			return eval_vpcr(handle, 0, data);
412 	}
413 
414 	acpi_handle_err(handle, "timeout in %s\n", __func__);
415 
416 	return -ETIMEDOUT;
417 }
418 
write_ec_cmd(acpi_handle handle,unsigned long cmd,unsigned long data)419 static int write_ec_cmd(acpi_handle handle, unsigned long cmd, unsigned long data)
420 {
421 	unsigned long end_jiffies, val;
422 	int err;
423 
424 	err = eval_vpcw(handle, 0, data);
425 	if (err)
426 		return err;
427 
428 	err = eval_vpcw(handle, 1, cmd);
429 	if (err)
430 		return err;
431 
432 	end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
433 
434 	while (time_before(jiffies, end_jiffies)) {
435 		usleep_range(IDEAPAD_EC_POLL_MIN_US, IDEAPAD_EC_POLL_MAX_US);
436 
437 		err = eval_vpcr(handle, 1, &val);
438 		if (err)
439 			return err;
440 
441 		if (val == 0)
442 			return 0;
443 	}
444 
445 	acpi_handle_err(handle, "timeout in %s\n", __func__);
446 
447 	return -ETIMEDOUT;
448 }
449 
450 /*
451  * debugfs
452  */
debugfs_status_show(struct seq_file * s,void * data)453 static int debugfs_status_show(struct seq_file *s, void *data)
454 {
455 	struct ideapad_private *priv = s->private;
456 	unsigned long value;
457 
458 	guard(mutex)(&priv->vpc_mutex);
459 
460 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
461 		seq_printf(s, "Backlight max:  %lu\n", value);
462 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
463 		seq_printf(s, "Backlight now:  %lu\n", value);
464 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
465 		seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
466 
467 	seq_puts(s, "=====================\n");
468 
469 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
470 		seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
471 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
472 		seq_printf(s, "Wifi status:  %s (%lu)\n", value ? "on" : "off", value);
473 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
474 		seq_printf(s, "BT status:    %s (%lu)\n", value ? "on" : "off", value);
475 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
476 		seq_printf(s, "3G status:    %s (%lu)\n", value ? "on" : "off", value);
477 
478 	seq_puts(s, "=====================\n");
479 
480 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
481 		seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
482 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
483 		seq_printf(s, "Camera status:   %s (%lu)\n", value ? "on" : "off", value);
484 
485 	seq_puts(s, "=====================\n");
486 
487 	if (!eval_gbmd(priv->adev->handle, &value))
488 		seq_printf(s, "GBMD: %#010lx\n", value);
489 	if (!eval_hals(priv->adev->handle, &value))
490 		seq_printf(s, "HALS: %#010lx\n", value);
491 
492 	return 0;
493 }
494 DEFINE_SHOW_ATTRIBUTE(debugfs_status);
495 
debugfs_cfg_show(struct seq_file * s,void * data)496 static int debugfs_cfg_show(struct seq_file *s, void *data)
497 {
498 	struct ideapad_private *priv = s->private;
499 
500 	seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
501 
502 	seq_puts(s, "Capabilities:");
503 	if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
504 		seq_puts(s, " bluetooth");
505 	if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
506 		seq_puts(s, " 3G");
507 	if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
508 		seq_puts(s, " wifi");
509 	if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
510 		seq_puts(s, " camera");
511 	seq_puts(s, "\n");
512 
513 	seq_puts(s, "OSD support:");
514 	if (test_bit(CFG_OSD_NUMLK_BIT, &priv->cfg))
515 		seq_puts(s, " num-lock");
516 	if (test_bit(CFG_OSD_CAPSLK_BIT, &priv->cfg))
517 		seq_puts(s, " caps-lock");
518 	if (test_bit(CFG_OSD_MICMUTE_BIT, &priv->cfg))
519 		seq_puts(s, " mic-mute");
520 	if (test_bit(CFG_OSD_TOUCHPAD_BIT, &priv->cfg))
521 		seq_puts(s, " touchpad");
522 	if (test_bit(CFG_OSD_CAM_BIT, &priv->cfg))
523 		seq_puts(s, " camera");
524 	seq_puts(s, "\n");
525 
526 	seq_puts(s, "Graphics: ");
527 	switch (priv->cfg & 0x700) {
528 	case 0x100:
529 		seq_puts(s, "Intel");
530 		break;
531 	case 0x200:
532 		seq_puts(s, "ATI");
533 		break;
534 	case 0x300:
535 		seq_puts(s, "Nvidia");
536 		break;
537 	case 0x400:
538 		seq_puts(s, "Intel and ATI");
539 		break;
540 	case 0x500:
541 		seq_puts(s, "Intel and Nvidia");
542 		break;
543 	}
544 	seq_puts(s, "\n");
545 
546 	return 0;
547 }
548 DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
549 
ideapad_debugfs_init(struct ideapad_private * priv)550 static void ideapad_debugfs_init(struct ideapad_private *priv)
551 {
552 	struct dentry *dir;
553 
554 	dir = debugfs_create_dir("ideapad", NULL);
555 	priv->debug = dir;
556 
557 	debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
558 	debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
559 }
560 
ideapad_debugfs_exit(struct ideapad_private * priv)561 static void ideapad_debugfs_exit(struct ideapad_private *priv)
562 {
563 	debugfs_remove_recursive(priv->debug);
564 	priv->debug = NULL;
565 }
566 
567 /*
568  * sysfs
569  */
camera_power_show(struct device * dev,struct device_attribute * attr,char * buf)570 static ssize_t camera_power_show(struct device *dev,
571 				 struct device_attribute *attr,
572 				 char *buf)
573 {
574 	struct ideapad_private *priv = dev_get_drvdata(dev);
575 	unsigned long result = 0;
576 	int err;
577 
578 	scoped_guard(mutex, &priv->vpc_mutex) {
579 		err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
580 		if (err)
581 			return err;
582 	}
583 
584 	return sysfs_emit(buf, "%d\n", !!result);
585 }
586 
camera_power_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)587 static ssize_t camera_power_store(struct device *dev,
588 				  struct device_attribute *attr,
589 				  const char *buf, size_t count)
590 {
591 	struct ideapad_private *priv = dev_get_drvdata(dev);
592 	bool state;
593 	int err;
594 
595 	err = kstrtobool(buf, &state);
596 	if (err)
597 		return err;
598 
599 	scoped_guard(mutex, &priv->vpc_mutex) {
600 		err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
601 		if (err)
602 			return err;
603 	}
604 
605 	return count;
606 }
607 
608 static DEVICE_ATTR_RW(camera_power);
609 
show_conservation_mode_deprecation_warning(struct device * dev)610 static void show_conservation_mode_deprecation_warning(struct device *dev)
611 {
612 	dev_warn_once(dev, "conservation_mode attribute has been deprecated, see charge_types.\n");
613 }
614 
conservation_mode_show(struct device * dev,struct device_attribute * attr,char * buf)615 static ssize_t conservation_mode_show(struct device *dev,
616 				      struct device_attribute *attr,
617 				      char *buf)
618 {
619 	struct ideapad_private *priv = dev_get_drvdata(dev);
620 	unsigned long result;
621 	int err;
622 
623 	show_conservation_mode_deprecation_warning(dev);
624 
625 	err = eval_gbmd(priv->adev->handle, &result);
626 	if (err)
627 		return err;
628 
629 	return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
630 }
631 
conservation_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)632 static ssize_t conservation_mode_store(struct device *dev,
633 				       struct device_attribute *attr,
634 				       const char *buf, size_t count)
635 {
636 	struct ideapad_private *priv = dev_get_drvdata(dev);
637 	bool state;
638 	int err;
639 
640 	show_conservation_mode_deprecation_warning(dev);
641 
642 	err = kstrtobool(buf, &state);
643 	if (err)
644 		return err;
645 
646 	err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
647 	if (err)
648 		return err;
649 
650 	return count;
651 }
652 
653 static DEVICE_ATTR_RW(conservation_mode);
654 
fan_mode_show(struct device * dev,struct device_attribute * attr,char * buf)655 static ssize_t fan_mode_show(struct device *dev,
656 			     struct device_attribute *attr,
657 			     char *buf)
658 {
659 	struct ideapad_private *priv = dev_get_drvdata(dev);
660 	unsigned long result = 0;
661 	int err;
662 
663 	scoped_guard(mutex, &priv->vpc_mutex) {
664 		err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
665 		if (err)
666 			return err;
667 	}
668 
669 	return sysfs_emit(buf, "%lu\n", result);
670 }
671 
fan_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)672 static ssize_t fan_mode_store(struct device *dev,
673 			      struct device_attribute *attr,
674 			      const char *buf, size_t count)
675 {
676 	struct ideapad_private *priv = dev_get_drvdata(dev);
677 	unsigned int state;
678 	int err;
679 
680 	err = kstrtouint(buf, 0, &state);
681 	if (err)
682 		return err;
683 
684 	if (state > 4 || state == 3)
685 		return -EINVAL;
686 
687 	scoped_guard(mutex, &priv->vpc_mutex) {
688 		err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
689 		if (err)
690 			return err;
691 	}
692 
693 	return count;
694 }
695 
696 static DEVICE_ATTR_RW(fan_mode);
697 
ideapad_fn_lock_get(struct ideapad_private * priv)698 static int ideapad_fn_lock_get(struct ideapad_private *priv)
699 {
700 	unsigned long hals;
701 	int err;
702 
703 	err = eval_hals(priv->adev->handle, &hals);
704 	if (err)
705 		return err;
706 
707 	return !!test_bit(HALS_FNLOCK_STATE_BIT, &hals);
708 }
709 
ideapad_fn_lock_set(struct ideapad_private * priv,bool state)710 static int ideapad_fn_lock_set(struct ideapad_private *priv, bool state)
711 {
712 	return exec_sals(priv->adev->handle,
713 		state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
714 }
715 
ideapad_fn_lock_led_notify(struct ideapad_private * priv,int brightness)716 static void ideapad_fn_lock_led_notify(struct ideapad_private *priv, int brightness)
717 {
718 	if (!priv->fn_lock.initialized)
719 		return;
720 
721 	if (brightness == priv->fn_lock.last_brightness)
722 		return;
723 
724 	priv->fn_lock.last_brightness = brightness;
725 
726 	led_classdev_notify_brightness_hw_changed(&priv->fn_lock.led, brightness);
727 }
728 
fn_lock_show(struct device * dev,struct device_attribute * attr,char * buf)729 static ssize_t fn_lock_show(struct device *dev,
730 			    struct device_attribute *attr,
731 			    char *buf)
732 {
733 	struct ideapad_private *priv = dev_get_drvdata(dev);
734 	int brightness;
735 
736 	brightness = ideapad_fn_lock_get(priv);
737 	if (brightness < 0)
738 		return brightness;
739 
740 	return sysfs_emit(buf, "%d\n", brightness);
741 }
742 
fn_lock_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)743 static ssize_t fn_lock_store(struct device *dev,
744 			     struct device_attribute *attr,
745 			     const char *buf, size_t count)
746 {
747 	struct ideapad_private *priv = dev_get_drvdata(dev);
748 	bool state;
749 	int err;
750 
751 	err = kstrtobool(buf, &state);
752 	if (err)
753 		return err;
754 
755 	err = ideapad_fn_lock_set(priv, state);
756 	if (err)
757 		return err;
758 
759 	ideapad_fn_lock_led_notify(priv, state);
760 
761 	return count;
762 }
763 
764 static DEVICE_ATTR_RW(fn_lock);
765 
touchpad_show(struct device * dev,struct device_attribute * attr,char * buf)766 static ssize_t touchpad_show(struct device *dev,
767 			     struct device_attribute *attr,
768 			     char *buf)
769 {
770 	struct ideapad_private *priv = dev_get_drvdata(dev);
771 	unsigned long result = 0;
772 	int err;
773 
774 	scoped_guard(mutex, &priv->vpc_mutex) {
775 		err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
776 		if (err)
777 			return err;
778 	}
779 
780 	priv->r_touchpad_val = result;
781 
782 	return sysfs_emit(buf, "%d\n", !!result);
783 }
784 
touchpad_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)785 static ssize_t touchpad_store(struct device *dev,
786 			      struct device_attribute *attr,
787 			      const char *buf, size_t count)
788 {
789 	struct ideapad_private *priv = dev_get_drvdata(dev);
790 	bool state;
791 	int err;
792 
793 	err = kstrtobool(buf, &state);
794 	if (err)
795 		return err;
796 
797 	scoped_guard(mutex, &priv->vpc_mutex) {
798 		err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
799 		if (err)
800 			return err;
801 	}
802 
803 	priv->r_touchpad_val = state;
804 
805 	return count;
806 }
807 
808 static DEVICE_ATTR_RW(touchpad);
809 
usb_charging_show(struct device * dev,struct device_attribute * attr,char * buf)810 static ssize_t usb_charging_show(struct device *dev,
811 				 struct device_attribute *attr,
812 				 char *buf)
813 {
814 	struct ideapad_private *priv = dev_get_drvdata(dev);
815 	unsigned long hals;
816 	int err;
817 
818 	err = eval_hals(priv->adev->handle, &hals);
819 	if (err)
820 		return err;
821 
822 	return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
823 }
824 
usb_charging_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)825 static ssize_t usb_charging_store(struct device *dev,
826 				  struct device_attribute *attr,
827 				  const char *buf, size_t count)
828 {
829 	struct ideapad_private *priv = dev_get_drvdata(dev);
830 	bool state;
831 	int err;
832 
833 	err = kstrtobool(buf, &state);
834 	if (err)
835 		return err;
836 
837 	err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
838 	if (err)
839 		return err;
840 
841 	return count;
842 }
843 
844 static DEVICE_ATTR_RW(usb_charging);
845 
846 static struct attribute *ideapad_attributes[] = {
847 	&dev_attr_camera_power.attr,
848 	&dev_attr_conservation_mode.attr,
849 	&dev_attr_fan_mode.attr,
850 	&dev_attr_fn_lock.attr,
851 	&dev_attr_touchpad.attr,
852 	&dev_attr_usb_charging.attr,
853 	NULL
854 };
855 
ideapad_is_visible(struct kobject * kobj,struct attribute * attr,int idx)856 static umode_t ideapad_is_visible(struct kobject *kobj,
857 				  struct attribute *attr,
858 				  int idx)
859 {
860 	struct device *dev = kobj_to_dev(kobj);
861 	struct ideapad_private *priv = dev_get_drvdata(dev);
862 	bool supported = true;
863 
864 	if (attr == &dev_attr_camera_power.attr)
865 		supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
866 	else if (attr == &dev_attr_conservation_mode.attr)
867 		supported = priv->features.conservation_mode;
868 	else if (attr == &dev_attr_fan_mode.attr)
869 		supported = priv->features.fan_mode;
870 	else if (attr == &dev_attr_fn_lock.attr)
871 		supported = priv->features.fn_lock;
872 	else if (attr == &dev_attr_touchpad.attr)
873 		supported = priv->features.touchpad_ctrl_via_ec;
874 	else if (attr == &dev_attr_usb_charging.attr)
875 		supported = priv->features.usb_charging;
876 
877 	return supported ? attr->mode : 0;
878 }
879 
880 static const struct attribute_group ideapad_attribute_group = {
881 	.is_visible = ideapad_is_visible,
882 	.attrs = ideapad_attributes
883 };
884 __ATTRIBUTE_GROUPS(ideapad_attribute);
885 
886 /*
887  * DYTC Platform profile
888  */
889 #define DYTC_CMD_QUERY        0 /* To get DYTC status - enable/revision */
890 #define DYTC_CMD_SET          1 /* To enable/disable IC function mode */
891 #define DYTC_CMD_GET          2 /* To get current IC function and mode */
892 #define DYTC_CMD_RESET    0x1ff /* To reset back to default */
893 
894 #define DYTC_QUERY_ENABLE_BIT 8  /* Bit        8 - 0 = disabled, 1 = enabled */
895 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
896 #define DYTC_QUERY_REV_BIT    28 /* Bits 28 - 31 - revision */
897 
898 #define DYTC_GET_FUNCTION_BIT 8  /* Bits  8-11 - function setting */
899 #define DYTC_GET_MODE_BIT     12 /* Bits 12-15 - mode setting */
900 
901 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
902 #define DYTC_SET_MODE_BIT     16 /* Bits 16-19 - mode setting */
903 #define DYTC_SET_VALID_BIT    20 /* Bit     20 - 1 = on, 0 = off */
904 
905 #define DYTC_FUNCTION_STD     0  /* Function = 0, standard mode */
906 #define DYTC_FUNCTION_CQL     1  /* Function = 1, lap mode */
907 #define DYTC_FUNCTION_MMC     11 /* Function = 11, desk mode */
908 
909 #define DYTC_MODE_PERFORM     2  /* High power mode aka performance */
910 #define DYTC_MODE_LOW_POWER       3  /* Low power mode aka quiet */
911 #define DYTC_MODE_BALANCE   0xF  /* Default mode aka balanced */
912 
913 #define DYTC_SET_COMMAND(function, mode, on) \
914 	(DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
915 	 (mode) << DYTC_SET_MODE_BIT | \
916 	 (on) << DYTC_SET_VALID_BIT)
917 
918 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
919 
920 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
921 
convert_dytc_to_profile(int dytcmode,enum platform_profile_option * profile)922 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
923 {
924 	switch (dytcmode) {
925 	case DYTC_MODE_LOW_POWER:
926 		*profile = PLATFORM_PROFILE_LOW_POWER;
927 		break;
928 	case DYTC_MODE_BALANCE:
929 		*profile =  PLATFORM_PROFILE_BALANCED;
930 		break;
931 	case DYTC_MODE_PERFORM:
932 		*profile =  PLATFORM_PROFILE_PERFORMANCE;
933 		break;
934 	default: /* Unknown mode */
935 		return -EINVAL;
936 	}
937 
938 	return 0;
939 }
940 
convert_profile_to_dytc(enum platform_profile_option profile,int * perfmode)941 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
942 {
943 	switch (profile) {
944 	case PLATFORM_PROFILE_LOW_POWER:
945 		*perfmode = DYTC_MODE_LOW_POWER;
946 		break;
947 	case PLATFORM_PROFILE_BALANCED:
948 		*perfmode = DYTC_MODE_BALANCE;
949 		break;
950 	case PLATFORM_PROFILE_PERFORMANCE:
951 		*perfmode = DYTC_MODE_PERFORM;
952 		break;
953 	default: /* Unknown profile */
954 		return -EOPNOTSUPP;
955 	}
956 
957 	return 0;
958 }
959 
960 /*
961  * dytc_profile_get: Function to register with platform_profile
962  * handler. Returns current platform profile.
963  */
dytc_profile_get(struct device * dev,enum platform_profile_option * profile)964 static int dytc_profile_get(struct device *dev,
965 			    enum platform_profile_option *profile)
966 {
967 	struct ideapad_dytc_priv *dytc = dev_get_drvdata(dev);
968 
969 	*profile = dytc->current_profile;
970 	return 0;
971 }
972 
973 /*
974  * Helper function - check if we are in CQL mode and if we are
975  *  - disable CQL,
976  *  - run the command
977  *  - enable CQL
978  *  If not in CQL mode, just run the command
979  */
dytc_cql_command(struct ideapad_private * priv,unsigned long cmd,unsigned long * output)980 static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
981 			    unsigned long *output)
982 {
983 	int err, cmd_err, cur_funcmode;
984 
985 	/* Determine if we are in CQL mode. This alters the commands we do */
986 	err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
987 	if (err)
988 		return err;
989 
990 	cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
991 	/* Check if we're OK to return immediately */
992 	if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
993 		return 0;
994 
995 	if (cur_funcmode == DYTC_FUNCTION_CQL) {
996 		err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
997 		if (err)
998 			return err;
999 	}
1000 
1001 	cmd_err = eval_dytc(priv->adev->handle, cmd, output);
1002 	/* Check return condition after we've restored CQL state */
1003 
1004 	if (cur_funcmode == DYTC_FUNCTION_CQL) {
1005 		err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
1006 		if (err)
1007 			return err;
1008 	}
1009 
1010 	return cmd_err;
1011 }
1012 
1013 /*
1014  * dytc_profile_set: Function to register with platform_profile
1015  * handler. Sets current platform profile.
1016  */
dytc_profile_set(struct device * dev,enum platform_profile_option profile)1017 static int dytc_profile_set(struct device *dev,
1018 			    enum platform_profile_option profile)
1019 {
1020 	struct ideapad_dytc_priv *dytc = dev_get_drvdata(dev);
1021 	struct ideapad_private *priv = dytc->priv;
1022 	unsigned long output;
1023 	int err;
1024 
1025 	scoped_guard(mutex_intr, &dytc->mutex) {
1026 		if (profile == PLATFORM_PROFILE_BALANCED) {
1027 			/* To get back to balanced mode we just issue a reset command */
1028 			err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
1029 			if (err)
1030 				return err;
1031 		} else {
1032 			int perfmode;
1033 
1034 			err = convert_profile_to_dytc(profile, &perfmode);
1035 			if (err)
1036 				return err;
1037 
1038 			/* Determine if we are in CQL mode. This alters the commands we do */
1039 			err = dytc_cql_command(priv,
1040 					       DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
1041 					       &output);
1042 			if (err)
1043 				return err;
1044 		}
1045 
1046 		/* Success - update current profile */
1047 		dytc->current_profile = profile;
1048 		return 0;
1049 	}
1050 
1051 	return -EINTR;
1052 }
1053 
dytc_profile_probe(void * drvdata,unsigned long * choices)1054 static int dytc_profile_probe(void *drvdata, unsigned long *choices)
1055 {
1056 	set_bit(PLATFORM_PROFILE_LOW_POWER, choices);
1057 	set_bit(PLATFORM_PROFILE_BALANCED, choices);
1058 	set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
1059 
1060 	return 0;
1061 }
1062 
dytc_profile_refresh(struct ideapad_private * priv)1063 static void dytc_profile_refresh(struct ideapad_private *priv)
1064 {
1065 	enum platform_profile_option profile;
1066 	unsigned long output;
1067 	int err, perfmode;
1068 
1069 	scoped_guard(mutex, &priv->dytc->mutex)
1070 		err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
1071 	if (err)
1072 		return;
1073 
1074 	perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
1075 
1076 	if (convert_dytc_to_profile(perfmode, &profile))
1077 		return;
1078 
1079 	if (profile != priv->dytc->current_profile) {
1080 		priv->dytc->current_profile = profile;
1081 		platform_profile_notify(priv->dytc->ppdev);
1082 	}
1083 }
1084 
1085 static const struct dmi_system_id ideapad_dytc_v4_allow_table[] = {
1086 	{
1087 		/* Ideapad 5 Pro 16ACH6 */
1088 		.matches = {
1089 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1090 			DMI_MATCH(DMI_PRODUCT_NAME, "82L5")
1091 		}
1092 	},
1093 	{
1094 		/* Ideapad 5 15ITL05 */
1095 		.matches = {
1096 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1097 			DMI_MATCH(DMI_PRODUCT_VERSION, "IdeaPad 5 15ITL05")
1098 		}
1099 	},
1100 	{}
1101 };
1102 
1103 static const struct platform_profile_ops dytc_profile_ops = {
1104 	.probe = dytc_profile_probe,
1105 	.profile_get = dytc_profile_get,
1106 	.profile_set = dytc_profile_set,
1107 };
1108 
ideapad_dytc_profile_init(struct ideapad_private * priv)1109 static int ideapad_dytc_profile_init(struct ideapad_private *priv)
1110 {
1111 	int err, dytc_version;
1112 	unsigned long output;
1113 
1114 	if (!priv->features.dytc)
1115 		return -ENODEV;
1116 
1117 	err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
1118 	/* For all other errors we can flag the failure */
1119 	if (err)
1120 		return err;
1121 
1122 	/* Check DYTC is enabled and supports mode setting */
1123 	if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output)) {
1124 		dev_info(&priv->platform_device->dev, "DYTC_QUERY_ENABLE_BIT returned false\n");
1125 		return -ENODEV;
1126 	}
1127 
1128 	dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
1129 
1130 	if (dytc_version < 4) {
1131 		dev_info(&priv->platform_device->dev, "DYTC_VERSION < 4 is not supported\n");
1132 		return -ENODEV;
1133 	}
1134 
1135 	if (dytc_version < 5 &&
1136 	    !(allow_v4_dytc || dmi_check_system(ideapad_dytc_v4_allow_table))) {
1137 		dev_info(&priv->platform_device->dev,
1138 			 "DYTC_VERSION 4 support may not work. Pass ideapad_laptop.allow_v4_dytc=Y on the kernel commandline to enable\n");
1139 		return -ENODEV;
1140 	}
1141 
1142 	priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
1143 	if (!priv->dytc)
1144 		return -ENOMEM;
1145 
1146 	mutex_init(&priv->dytc->mutex);
1147 
1148 	priv->dytc->priv = priv;
1149 
1150 	/* Create platform_profile structure and register */
1151 	priv->dytc->ppdev = devm_platform_profile_register(&priv->platform_device->dev,
1152 							   "ideapad-laptop", priv->dytc,
1153 							   &dytc_profile_ops);
1154 	if (IS_ERR(priv->dytc->ppdev)) {
1155 		err = PTR_ERR(priv->dytc->ppdev);
1156 		goto pp_reg_failed;
1157 	}
1158 
1159 	/* Ensure initial values are correct */
1160 	dytc_profile_refresh(priv);
1161 
1162 	return 0;
1163 
1164 pp_reg_failed:
1165 	mutex_destroy(&priv->dytc->mutex);
1166 	kfree(priv->dytc);
1167 	priv->dytc = NULL;
1168 
1169 	return err;
1170 }
1171 
ideapad_dytc_profile_exit(struct ideapad_private * priv)1172 static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
1173 {
1174 	if (!priv->dytc)
1175 		return;
1176 
1177 	mutex_destroy(&priv->dytc->mutex);
1178 	kfree(priv->dytc);
1179 
1180 	priv->dytc = NULL;
1181 }
1182 
1183 /*
1184  * Rfkill
1185  */
1186 struct ideapad_rfk_data {
1187 	char *name;
1188 	int cfgbit;
1189 	int opcode;
1190 	int type;
1191 };
1192 
1193 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
1194 	{ "ideapad_wlan",      CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
1195 	{ "ideapad_bluetooth", CFG_CAP_BT_BIT,   VPCCMD_W_BT,   RFKILL_TYPE_BLUETOOTH },
1196 	{ "ideapad_3g",        CFG_CAP_3G_BIT,   VPCCMD_W_3G,   RFKILL_TYPE_WWAN },
1197 };
1198 
ideapad_rfk_set(void * data,bool blocked)1199 static int ideapad_rfk_set(void *data, bool blocked)
1200 {
1201 	struct ideapad_rfk_priv *priv = data;
1202 	int opcode = ideapad_rfk_data[priv->dev].opcode;
1203 
1204 	guard(mutex)(&priv->priv->vpc_mutex);
1205 
1206 	return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
1207 }
1208 
1209 static const struct rfkill_ops ideapad_rfk_ops = {
1210 	.set_block = ideapad_rfk_set,
1211 };
1212 
ideapad_sync_rfk_state(struct ideapad_private * priv)1213 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
1214 {
1215 	unsigned long hw_blocked = 0;
1216 	int i;
1217 
1218 	if (priv->features.hw_rfkill_switch) {
1219 		guard(mutex)(&priv->vpc_mutex);
1220 
1221 		if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
1222 			return;
1223 		hw_blocked = !hw_blocked;
1224 	}
1225 
1226 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1227 		if (priv->rfk[i])
1228 			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
1229 }
1230 
ideapad_register_rfkill(struct ideapad_private * priv,int dev)1231 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
1232 {
1233 	unsigned long rf_enabled;
1234 	int err;
1235 
1236 	if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
1237 		/* Force to enable bluetooth when no_bt_rfkill=1 */
1238 		write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
1239 		return 0;
1240 	}
1241 
1242 	priv->rfk_priv[dev].dev = dev;
1243 	priv->rfk_priv[dev].priv = priv;
1244 
1245 	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
1246 				      &priv->platform_device->dev,
1247 				      ideapad_rfk_data[dev].type,
1248 				      &ideapad_rfk_ops,
1249 				      &priv->rfk_priv[dev]);
1250 	if (!priv->rfk[dev])
1251 		return -ENOMEM;
1252 
1253 	err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
1254 	if (err)
1255 		rf_enabled = 1;
1256 
1257 	rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
1258 
1259 	err = rfkill_register(priv->rfk[dev]);
1260 	if (err)
1261 		rfkill_destroy(priv->rfk[dev]);
1262 
1263 	return err;
1264 }
1265 
ideapad_unregister_rfkill(struct ideapad_private * priv,int dev)1266 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1267 {
1268 	if (!priv->rfk[dev])
1269 		return;
1270 
1271 	rfkill_unregister(priv->rfk[dev]);
1272 	rfkill_destroy(priv->rfk[dev]);
1273 }
1274 
1275 /*
1276  * input device
1277  */
1278 #define IDEAPAD_WMI_KEY 0x100
1279 
1280 static const struct key_entry ideapad_keymap[] = {
1281 	{ KE_KEY,   6, { KEY_SWITCHVIDEOMODE } },
1282 	{ KE_KEY,   7, { KEY_CAMERA } },
1283 	{ KE_KEY,   8, { KEY_MICMUTE } },
1284 	{ KE_KEY,  11, { KEY_F16 } },
1285 	{ KE_KEY,  13, { KEY_WLAN } },
1286 	{ KE_KEY,  16, { KEY_PROG1 } },
1287 	{ KE_KEY,  17, { KEY_PROG2 } },
1288 	{ KE_KEY,  64, { KEY_PROG3 } },
1289 	{ KE_KEY,  65, { KEY_PROG4 } },
1290 	{ KE_KEY,  66, { KEY_TOUCHPAD_OFF } },
1291 	{ KE_KEY,  67, { KEY_TOUCHPAD_ON } },
1292 	{ KE_KEY, 128, { KEY_ESC } },
1293 
1294 	/*
1295 	 * WMI keys
1296 	 */
1297 
1298 	/* FnLock (handled by the firmware) */
1299 	{ KE_IGNORE,	0x02 | IDEAPAD_WMI_KEY },
1300 	/* Esc (handled by the firmware) */
1301 	{ KE_IGNORE,	0x03 | IDEAPAD_WMI_KEY },
1302 	/* Customizable Lenovo Hotkey ("star" with 'S' inside) */
1303 	{ KE_KEY,	0x01 | IDEAPAD_WMI_KEY, { KEY_FAVORITES } },
1304 	{ KE_KEY,	0x04 | IDEAPAD_WMI_KEY, { KEY_SELECTIVE_SCREENSHOT } },
1305 	/* Lenovo Support */
1306 	{ KE_KEY,	0x07 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1307 	{ KE_KEY,	0x0e | IDEAPAD_WMI_KEY, { KEY_PICKUP_PHONE } },
1308 	{ KE_KEY,	0x0f | IDEAPAD_WMI_KEY, { KEY_HANGUP_PHONE } },
1309 	/* Refresh Rate Toggle (Fn+R) */
1310 	{ KE_KEY,	0x10 | IDEAPAD_WMI_KEY, { KEY_REFRESH_RATE_TOGGLE } },
1311 	/* Dark mode toggle */
1312 	{ KE_KEY,	0x13 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
1313 	/* Sound profile switch */
1314 	{ KE_KEY,	0x12 | IDEAPAD_WMI_KEY, { KEY_PROG2 } },
1315 	/* Lenovo Virtual Background application */
1316 	{ KE_KEY,	0x28 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
1317 	/* Lenovo Support */
1318 	{ KE_KEY,	0x27 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1319 	/* Refresh Rate Toggle */
1320 	{ KE_KEY,	0x0a | IDEAPAD_WMI_KEY, { KEY_REFRESH_RATE_TOGGLE } },
1321 	/* Specific to some newer models */
1322 	{ KE_KEY,	0x3e | IDEAPAD_WMI_KEY, { KEY_MICMUTE } },
1323 	{ KE_KEY,	0x3f | IDEAPAD_WMI_KEY, { KEY_RFKILL } },
1324 	/* Star- (User Assignable Key) */
1325 	{ KE_KEY,	0x44 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
1326 	/* Eye */
1327 	{ KE_KEY,	0x45 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
1328 	/* Performance toggle also Fn+Q, handled inside ideapad_wmi_notify() */
1329 	{ KE_KEY,	0x3d | IDEAPAD_WMI_KEY, { KEY_PROG4 } },
1330 	/* shift + prtsc */
1331 	{ KE_KEY,   0x2d | IDEAPAD_WMI_KEY, { KEY_CUT } },
1332 	{ KE_KEY,   0x29 | IDEAPAD_WMI_KEY, { KEY_TOUCHPAD_TOGGLE } },
1333 	{ KE_KEY,   0x2a | IDEAPAD_WMI_KEY, { KEY_ROOT_MENU } },
1334 
1335 	{ KE_END },
1336 };
1337 
ideapad_input_init(struct ideapad_private * priv)1338 static int ideapad_input_init(struct ideapad_private *priv)
1339 {
1340 	struct input_dev *inputdev;
1341 	int err;
1342 
1343 	inputdev = input_allocate_device();
1344 	if (!inputdev)
1345 		return -ENOMEM;
1346 
1347 	inputdev->name = "Ideapad extra buttons";
1348 	inputdev->phys = "ideapad/input0";
1349 	inputdev->id.bustype = BUS_HOST;
1350 	inputdev->dev.parent = &priv->platform_device->dev;
1351 
1352 	err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1353 	if (err) {
1354 		dev_err(&priv->platform_device->dev,
1355 			"Could not set up input device keymap: %d\n", err);
1356 		goto err_free_dev;
1357 	}
1358 
1359 	err = input_register_device(inputdev);
1360 	if (err) {
1361 		dev_err(&priv->platform_device->dev,
1362 			"Could not register input device: %d\n", err);
1363 		goto err_free_dev;
1364 	}
1365 
1366 	priv->inputdev = inputdev;
1367 
1368 	return 0;
1369 
1370 err_free_dev:
1371 	input_free_device(inputdev);
1372 
1373 	return err;
1374 }
1375 
ideapad_input_exit(struct ideapad_private * priv)1376 static void ideapad_input_exit(struct ideapad_private *priv)
1377 {
1378 	input_unregister_device(priv->inputdev);
1379 	priv->inputdev = NULL;
1380 }
1381 
ideapad_input_report(struct ideapad_private * priv,unsigned long scancode)1382 static void ideapad_input_report(struct ideapad_private *priv,
1383 				 unsigned long scancode)
1384 {
1385 	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1386 }
1387 
ideapad_input_novokey(struct ideapad_private * priv)1388 static void ideapad_input_novokey(struct ideapad_private *priv)
1389 {
1390 	unsigned long long_pressed;
1391 
1392 	scoped_guard(mutex, &priv->vpc_mutex)
1393 		if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1394 			return;
1395 
1396 	if (long_pressed)
1397 		ideapad_input_report(priv, 17);
1398 	else
1399 		ideapad_input_report(priv, 16);
1400 }
1401 
ideapad_check_special_buttons(struct ideapad_private * priv)1402 static void ideapad_check_special_buttons(struct ideapad_private *priv)
1403 {
1404 	unsigned long bit, value;
1405 
1406 	scoped_guard(mutex, &priv->vpc_mutex)
1407 		if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1408 			return;
1409 
1410 	for_each_set_bit (bit, &value, 16) {
1411 		switch (bit) {
1412 		case 6:	/* Z570 */
1413 		case 0:	/* Z580 */
1414 			/* Thermal Management / Performance Mode button */
1415 			if (priv->dytc)
1416 				platform_profile_cycle();
1417 			else
1418 				ideapad_input_report(priv, 65);
1419 			break;
1420 		case 1:
1421 			/* OneKey Theater button */
1422 			ideapad_input_report(priv, 64);
1423 			break;
1424 		default:
1425 			dev_info(&priv->platform_device->dev,
1426 				 "Unknown special button: %lu\n", bit);
1427 			break;
1428 		}
1429 	}
1430 }
1431 
1432 /*
1433  * backlight
1434  */
ideapad_backlight_get_brightness(struct backlight_device * blightdev)1435 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1436 {
1437 	struct ideapad_private *priv = bl_get_data(blightdev);
1438 	unsigned long now;
1439 	int err;
1440 
1441 	guard(mutex)(&priv->vpc_mutex);
1442 
1443 	err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1444 	if (err)
1445 		return err;
1446 
1447 	return now;
1448 }
1449 
ideapad_backlight_update_status(struct backlight_device * blightdev)1450 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1451 {
1452 	struct ideapad_private *priv = bl_get_data(blightdev);
1453 	int err;
1454 
1455 	guard(mutex)(&priv->vpc_mutex);
1456 
1457 	err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1458 			   blightdev->props.brightness);
1459 	if (err)
1460 		return err;
1461 
1462 	err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1463 			   blightdev->props.power != BACKLIGHT_POWER_OFF);
1464 	if (err)
1465 		return err;
1466 
1467 	return 0;
1468 }
1469 
1470 static const struct backlight_ops ideapad_backlight_ops = {
1471 	.get_brightness = ideapad_backlight_get_brightness,
1472 	.update_status = ideapad_backlight_update_status,
1473 };
1474 
ideapad_backlight_init(struct ideapad_private * priv)1475 static int ideapad_backlight_init(struct ideapad_private *priv)
1476 {
1477 	struct backlight_device *blightdev;
1478 	struct backlight_properties props;
1479 	unsigned long max, now, power;
1480 	int err;
1481 
1482 	err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1483 	if (err)
1484 		return err;
1485 
1486 	err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1487 	if (err)
1488 		return err;
1489 
1490 	err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1491 	if (err)
1492 		return err;
1493 
1494 	memset(&props, 0, sizeof(props));
1495 
1496 	props.max_brightness = max;
1497 	props.type = BACKLIGHT_PLATFORM;
1498 
1499 	blightdev = backlight_device_register("ideapad",
1500 					      &priv->platform_device->dev,
1501 					      priv,
1502 					      &ideapad_backlight_ops,
1503 					      &props);
1504 	if (IS_ERR(blightdev)) {
1505 		err = PTR_ERR(blightdev);
1506 		dev_err(&priv->platform_device->dev,
1507 			"Could not register backlight device: %d\n", err);
1508 		return err;
1509 	}
1510 
1511 	priv->blightdev = blightdev;
1512 	blightdev->props.brightness = now;
1513 	blightdev->props.power = power ? BACKLIGHT_POWER_ON : BACKLIGHT_POWER_OFF;
1514 
1515 	backlight_update_status(blightdev);
1516 
1517 	return 0;
1518 }
1519 
ideapad_backlight_exit(struct ideapad_private * priv)1520 static void ideapad_backlight_exit(struct ideapad_private *priv)
1521 {
1522 	backlight_device_unregister(priv->blightdev);
1523 	priv->blightdev = NULL;
1524 }
1525 
ideapad_backlight_notify_power(struct ideapad_private * priv)1526 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1527 {
1528 	struct backlight_device *blightdev = priv->blightdev;
1529 	unsigned long power;
1530 
1531 	if (!blightdev)
1532 		return;
1533 
1534 	guard(mutex)(&priv->vpc_mutex);
1535 
1536 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1537 		return;
1538 
1539 	blightdev->props.power = power ? BACKLIGHT_POWER_ON : BACKLIGHT_POWER_OFF;
1540 }
1541 
ideapad_backlight_notify_brightness(struct ideapad_private * priv)1542 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1543 {
1544 	unsigned long now;
1545 
1546 	/* if we control brightness via acpi video driver */
1547 	if (!priv->blightdev)
1548 		scoped_guard(mutex, &priv->vpc_mutex)
1549 			read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1550 	else
1551 		backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1552 }
1553 
1554 /*
1555  * keyboard backlight
1556  */
ideapad_kbd_bl_check_tristate(int type)1557 static int ideapad_kbd_bl_check_tristate(int type)
1558 {
1559 	return (type == KBD_BL_TRISTATE) || (type == KBD_BL_TRISTATE_AUTO);
1560 }
1561 
ideapad_kbd_bl_brightness_get(struct ideapad_private * priv)1562 static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1563 {
1564 	unsigned long value;
1565 	int err;
1566 
1567 	if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
1568 		err = eval_kblc(priv->adev->handle,
1569 				FIELD_PREP(KBD_BL_COMMAND_TYPE, priv->kbd_bl.type) |
1570 				KBD_BL_COMMAND_GET,
1571 				&value);
1572 
1573 		if (err)
1574 			return err;
1575 
1576 		/* Convert returned value to brightness level */
1577 		value = FIELD_GET(KBD_BL_GET_BRIGHTNESS, value);
1578 
1579 		/* Off, low or high */
1580 		if (value <= priv->kbd_bl.led.max_brightness)
1581 			return value;
1582 
1583 		/* Auto, report as off */
1584 		if (value == priv->kbd_bl.led.max_brightness + 1)
1585 			return 0;
1586 
1587 		/* Unknown value */
1588 		dev_warn(&priv->platform_device->dev,
1589 			 "Unknown keyboard backlight value: %lu", value);
1590 		return -EINVAL;
1591 	}
1592 
1593 	err = eval_hals(priv->adev->handle, &value);
1594 	if (err)
1595 		return err;
1596 
1597 	return !!test_bit(HALS_KBD_BL_STATE_BIT, &value);
1598 }
1599 
ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev * led_cdev)1600 static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1601 {
1602 	struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1603 
1604 	return ideapad_kbd_bl_brightness_get(priv);
1605 }
1606 
ideapad_kbd_bl_brightness_set(struct ideapad_private * priv,unsigned int brightness)1607 static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1608 {
1609 	int err;
1610 	unsigned long value;
1611 	int type = priv->kbd_bl.type;
1612 
1613 	if (ideapad_kbd_bl_check_tristate(type)) {
1614 		if (brightness > priv->kbd_bl.led.max_brightness)
1615 			return -EINVAL;
1616 
1617 		value = FIELD_PREP(KBD_BL_SET_BRIGHTNESS, brightness) |
1618 			FIELD_PREP(KBD_BL_COMMAND_TYPE, type) |
1619 			KBD_BL_COMMAND_SET;
1620 		err = exec_kblc(priv->adev->handle, value);
1621 	} else {
1622 		err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1623 	}
1624 
1625 	if (err)
1626 		return err;
1627 
1628 	priv->kbd_bl.last_brightness = brightness;
1629 
1630 	return 0;
1631 }
1632 
ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev * led_cdev,enum led_brightness brightness)1633 static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1634 						  enum led_brightness brightness)
1635 {
1636 	struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1637 
1638 	return ideapad_kbd_bl_brightness_set(priv, brightness);
1639 }
1640 
ideapad_kbd_bl_notify(struct ideapad_private * priv)1641 static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1642 {
1643 	int brightness;
1644 
1645 	if (!priv->kbd_bl.initialized)
1646 		return;
1647 
1648 	brightness = ideapad_kbd_bl_brightness_get(priv);
1649 	if (brightness < 0)
1650 		return;
1651 
1652 	if (brightness == priv->kbd_bl.last_brightness)
1653 		return;
1654 
1655 	priv->kbd_bl.last_brightness = brightness;
1656 
1657 	led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1658 }
1659 
ideapad_kbd_bl_init(struct ideapad_private * priv)1660 static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1661 {
1662 	int brightness, err;
1663 
1664 	if (!priv->features.kbd_bl)
1665 		return -ENODEV;
1666 
1667 	if (WARN_ON(priv->kbd_bl.initialized))
1668 		return -EEXIST;
1669 
1670 	if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
1671 		priv->kbd_bl.led.max_brightness = 2;
1672 	} else {
1673 		priv->kbd_bl.led.max_brightness = 1;
1674 	}
1675 
1676 	brightness = ideapad_kbd_bl_brightness_get(priv);
1677 	if (brightness < 0)
1678 		return brightness;
1679 
1680 	priv->kbd_bl.last_brightness = brightness;
1681 	priv->kbd_bl.led.name                    = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1682 	priv->kbd_bl.led.brightness_get          = ideapad_kbd_bl_led_cdev_brightness_get;
1683 	priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1684 	priv->kbd_bl.led.flags                   = LED_BRIGHT_HW_CHANGED | LED_RETAIN_AT_SHUTDOWN;
1685 
1686 	err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1687 	if (err)
1688 		return err;
1689 
1690 	priv->kbd_bl.initialized = true;
1691 
1692 	return 0;
1693 }
1694 
ideapad_kbd_bl_exit(struct ideapad_private * priv)1695 static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1696 {
1697 	if (!priv->kbd_bl.initialized)
1698 		return;
1699 
1700 	priv->kbd_bl.initialized = false;
1701 
1702 	led_classdev_unregister(&priv->kbd_bl.led);
1703 }
1704 
1705 /*
1706  * FnLock LED
1707  */
ideapad_fn_lock_led_cdev_get(struct led_classdev * led_cdev)1708 static enum led_brightness ideapad_fn_lock_led_cdev_get(struct led_classdev *led_cdev)
1709 {
1710 	struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, fn_lock.led);
1711 
1712 	return ideapad_fn_lock_get(priv);
1713 }
1714 
ideapad_fn_lock_led_cdev_set(struct led_classdev * led_cdev,enum led_brightness brightness)1715 static int ideapad_fn_lock_led_cdev_set(struct led_classdev *led_cdev,
1716 	enum led_brightness brightness)
1717 {
1718 	struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, fn_lock.led);
1719 
1720 	return ideapad_fn_lock_set(priv, brightness);
1721 }
1722 
ideapad_fn_lock_led_init(struct ideapad_private * priv)1723 static int ideapad_fn_lock_led_init(struct ideapad_private *priv)
1724 {
1725 	int brightness, err;
1726 
1727 	if (!priv->features.fn_lock)
1728 		return -ENODEV;
1729 
1730 	if (WARN_ON(priv->fn_lock.initialized))
1731 		return -EEXIST;
1732 
1733 	priv->fn_lock.led.max_brightness = 1;
1734 
1735 	brightness = ideapad_fn_lock_get(priv);
1736 	if (brightness < 0)
1737 		return brightness;
1738 
1739 	priv->fn_lock.last_brightness = brightness;
1740 	priv->fn_lock.led.name                    = "platform::" LED_FUNCTION_FNLOCK;
1741 	priv->fn_lock.led.brightness_get          = ideapad_fn_lock_led_cdev_get;
1742 	priv->fn_lock.led.brightness_set_blocking = ideapad_fn_lock_led_cdev_set;
1743 	priv->fn_lock.led.flags                   = LED_BRIGHT_HW_CHANGED | LED_RETAIN_AT_SHUTDOWN;
1744 
1745 	err = led_classdev_register(&priv->platform_device->dev, &priv->fn_lock.led);
1746 	if (err)
1747 		return err;
1748 
1749 	priv->fn_lock.initialized = true;
1750 
1751 	return 0;
1752 }
1753 
ideapad_fn_lock_led_exit(struct ideapad_private * priv)1754 static void ideapad_fn_lock_led_exit(struct ideapad_private *priv)
1755 {
1756 	if (!priv->fn_lock.initialized)
1757 		return;
1758 
1759 	priv->fn_lock.initialized = false;
1760 
1761 	led_classdev_unregister(&priv->fn_lock.led);
1762 }
1763 
1764 /*
1765  * module init/exit
1766  */
ideapad_sync_touchpad_state(struct ideapad_private * priv,bool send_events)1767 static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_events)
1768 {
1769 	unsigned long value;
1770 	unsigned char param;
1771 	int ret;
1772 
1773 	/* Without reading from EC touchpad LED doesn't switch state */
1774 	scoped_guard(mutex, &priv->vpc_mutex)
1775 		ret = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value);
1776 	if (ret)
1777 		return;
1778 
1779 	/*
1780 	 * Some IdeaPads don't really turn off touchpad - they only
1781 	 * switch the LED state. We (de)activate KBC AUX port to turn
1782 	 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1783 	 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1784 	 */
1785 	if (priv->features.ctrl_ps2_aux_port)
1786 		i8042_command(&param, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1787 
1788 	/*
1789 	 * On older models the EC controls the touchpad and toggles it on/off
1790 	 * itself, in this case we report KEY_TOUCHPAD_ON/_OFF. Some models do
1791 	 * an acpi-notify with VPC bit 5 set on resume, so this function get
1792 	 * called with send_events=true on every resume. Therefor if the EC did
1793 	 * not toggle, do nothing to avoid sending spurious KEY_TOUCHPAD_TOGGLE.
1794 	 */
1795 	if (send_events && value != priv->r_touchpad_val) {
1796 		ideapad_input_report(priv, value ? 67 : 66);
1797 		sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1798 	}
1799 
1800 	priv->r_touchpad_val = value;
1801 }
1802 
1803 static const struct dmi_system_id ymc_ec_trigger_quirk_dmi_table[] = {
1804 	{
1805 		/* Lenovo Yoga 7 14ARB7 */
1806 		.matches = {
1807 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1808 			DMI_MATCH(DMI_PRODUCT_NAME, "82QF"),
1809 		},
1810 	},
1811 	{
1812 		/* Lenovo Yoga 7 14ACN6 */
1813 		.matches = {
1814 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1815 			DMI_MATCH(DMI_PRODUCT_NAME, "82N7"),
1816 		},
1817 	},
1818 	{ }
1819 };
1820 
ideapad_laptop_trigger_ec(void)1821 static void ideapad_laptop_trigger_ec(void)
1822 {
1823 	struct ideapad_private *priv;
1824 	int ret;
1825 
1826 	guard(mutex)(&ideapad_shared_mutex);
1827 
1828 	priv = ideapad_shared;
1829 	if (!priv)
1830 		return;
1831 
1832 	if (!priv->features.ymc_ec_trigger)
1833 		return;
1834 
1835 	scoped_guard(mutex, &priv->vpc_mutex)
1836 		ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_YMC, 1);
1837 	if (ret)
1838 		dev_warn(&priv->platform_device->dev, "Could not write YMC: %d\n", ret);
1839 }
1840 
ideapad_laptop_nb_notify(struct notifier_block * nb,unsigned long action,void * data)1841 static int ideapad_laptop_nb_notify(struct notifier_block *nb,
1842 				    unsigned long action, void *data)
1843 {
1844 	switch (action) {
1845 	case IDEAPAD_LAPTOP_YMC_EVENT:
1846 		ideapad_laptop_trigger_ec();
1847 		break;
1848 	}
1849 
1850 	return 0;
1851 }
1852 
1853 static struct notifier_block ideapad_laptop_notifier = {
1854 	.notifier_call = ideapad_laptop_nb_notify,
1855 };
1856 
1857 static BLOCKING_NOTIFIER_HEAD(ideapad_laptop_chain_head);
1858 
ideapad_laptop_register_notifier(struct notifier_block * nb)1859 int ideapad_laptop_register_notifier(struct notifier_block *nb)
1860 {
1861 	return blocking_notifier_chain_register(&ideapad_laptop_chain_head, nb);
1862 }
1863 EXPORT_SYMBOL_NS_GPL(ideapad_laptop_register_notifier, "IDEAPAD_LAPTOP");
1864 
ideapad_laptop_unregister_notifier(struct notifier_block * nb)1865 int ideapad_laptop_unregister_notifier(struct notifier_block *nb)
1866 {
1867 	return blocking_notifier_chain_unregister(&ideapad_laptop_chain_head, nb);
1868 }
1869 EXPORT_SYMBOL_NS_GPL(ideapad_laptop_unregister_notifier, "IDEAPAD_LAPTOP");
1870 
ideapad_laptop_call_notifier(unsigned long action,void * data)1871 void ideapad_laptop_call_notifier(unsigned long action, void *data)
1872 {
1873 	blocking_notifier_call_chain(&ideapad_laptop_chain_head, action, data);
1874 }
1875 EXPORT_SYMBOL_NS_GPL(ideapad_laptop_call_notifier, "IDEAPAD_LAPTOP");
1876 
ideapad_acpi_notify(acpi_handle handle,u32 event,void * data)1877 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1878 {
1879 	struct ideapad_private *priv = data;
1880 	unsigned long vpc1, vpc2, bit;
1881 
1882 	scoped_guard(mutex, &priv->vpc_mutex) {
1883 		if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1884 			return;
1885 
1886 		if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1887 			return;
1888 	}
1889 
1890 	vpc1 = (vpc2 << 8) | vpc1;
1891 
1892 	for_each_set_bit (bit, &vpc1, 16) {
1893 		switch (bit) {
1894 		case 13:
1895 		case 11:
1896 		case 8:
1897 		case 7:
1898 		case 6:
1899 			ideapad_input_report(priv, bit);
1900 			break;
1901 		case 10:
1902 			/*
1903 			 * This event gets send on a Yoga 300-11IBR when the EC
1904 			 * believes that the device has changed between laptop/
1905 			 * tent/stand/tablet mode. The EC relies on getting
1906 			 * angle info from 2 accelerometers through a special
1907 			 * windows service calling a DSM on the DUAL250E ACPI-
1908 			 * device. Linux does not do this, making the laptop/
1909 			 * tent/stand/tablet mode info unreliable, so we simply
1910 			 * ignore these events.
1911 			 */
1912 			break;
1913 		case 9:
1914 			ideapad_sync_rfk_state(priv);
1915 			break;
1916 		case 5:
1917 			ideapad_sync_touchpad_state(priv, true);
1918 			break;
1919 		case 4:
1920 			ideapad_backlight_notify_brightness(priv);
1921 			break;
1922 		case 3:
1923 			ideapad_input_novokey(priv);
1924 			break;
1925 		case 2:
1926 			ideapad_backlight_notify_power(priv);
1927 			break;
1928 		case KBD_BL_KBLC_CHANGED_EVENT:
1929 		case 1:
1930 			/*
1931 			 * Some IdeaPads report event 1 every ~20
1932 			 * seconds while on battery power; some
1933 			 * report this when changing to/from tablet
1934 			 * mode; some report this when the keyboard
1935 			 * backlight has changed.
1936 			 */
1937 			ideapad_kbd_bl_notify(priv);
1938 			break;
1939 		case 0:
1940 			ideapad_check_special_buttons(priv);
1941 			break;
1942 		default:
1943 			dev_info(&priv->platform_device->dev,
1944 				 "Unknown event: %lu\n", bit);
1945 		}
1946 	}
1947 }
1948 
1949 /* On some models we need to call exec_sals(SALS_FNLOCK_ON/OFF) to set the LED */
1950 static const struct dmi_system_id set_fn_lock_led_list[] = {
1951 	{
1952 		/* https://bugzilla.kernel.org/show_bug.cgi?id=212671 */
1953 		.matches = {
1954 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1955 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion R7000P2020H"),
1956 		}
1957 	},
1958 	{
1959 		.matches = {
1960 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1961 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion 5 15ARH05"),
1962 		}
1963 	},
1964 	{}
1965 };
1966 
1967 /*
1968  * Some ideapads have a hardware rfkill switch, but most do not have one.
1969  * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1970  * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1971  * There used to be a long list of DMI ids for models without a hw rfkill
1972  * switch here, but that resulted in playing whack a mole.
1973  * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1974  * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1975  * hw-blocked results in an empty SSID list, which is a much more benign
1976  * failure mode.
1977  * So the default now is the much safer option of assuming there is no
1978  * hardware rfkill switch. This default also actually matches most hardware,
1979  * since having a hw rfkill switch is quite rare on modern hardware, so this
1980  * also leads to a much shorter list.
1981  */
1982 static const struct dmi_system_id hw_rfkill_list[] = {
1983 	{}
1984 };
1985 
1986 /*
1987  * On some models the EC toggles the touchpad muted LED on touchpad toggle
1988  * hotkey presses, but the EC does not actually disable the touchpad itself.
1989  * On these models the driver needs to explicitly enable/disable the i8042
1990  * (PS/2) aux port.
1991  */
1992 static const struct dmi_system_id ctrl_ps2_aux_port_list[] = {
1993 	{
1994 	/* Lenovo Ideapad Z570 */
1995 	.matches = {
1996 		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1997 		DMI_MATCH(DMI_PRODUCT_VERSION, "Ideapad Z570"),
1998 		},
1999 	},
2000 	{}
2001 };
2002 
ideapad_psy_ext_set_prop(struct power_supply * psy,const struct power_supply_ext * ext,void * ext_data,enum power_supply_property psp,const union power_supply_propval * val)2003 static int ideapad_psy_ext_set_prop(struct power_supply *psy,
2004 				    const struct power_supply_ext *ext,
2005 				    void *ext_data,
2006 				    enum power_supply_property psp,
2007 				    const union power_supply_propval *val)
2008 {
2009 	struct ideapad_private *priv = ext_data;
2010 
2011 	switch (val->intval) {
2012 	case POWER_SUPPLY_CHARGE_TYPE_LONGLIFE:
2013 		return exec_sbmc(priv->adev->handle, SBMC_CONSERVATION_ON);
2014 	case POWER_SUPPLY_CHARGE_TYPE_STANDARD:
2015 		return exec_sbmc(priv->adev->handle, SBMC_CONSERVATION_OFF);
2016 	default:
2017 		return -EINVAL;
2018 	}
2019 }
2020 
ideapad_psy_ext_get_prop(struct power_supply * psy,const struct power_supply_ext * ext,void * ext_data,enum power_supply_property psp,union power_supply_propval * val)2021 static int ideapad_psy_ext_get_prop(struct power_supply *psy,
2022 				    const struct power_supply_ext *ext,
2023 				    void *ext_data,
2024 				    enum power_supply_property psp,
2025 				    union power_supply_propval *val)
2026 {
2027 	struct ideapad_private *priv = ext_data;
2028 	unsigned long result;
2029 	int err;
2030 
2031 	err = eval_gbmd(priv->adev->handle, &result);
2032 	if (err)
2033 		return err;
2034 
2035 	if (test_bit(GBMD_CONSERVATION_STATE_BIT, &result))
2036 		val->intval = POWER_SUPPLY_CHARGE_TYPE_LONGLIFE;
2037 	else
2038 		val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
2039 
2040 	return 0;
2041 }
2042 
ideapad_psy_prop_is_writeable(struct power_supply * psy,const struct power_supply_ext * ext,void * data,enum power_supply_property psp)2043 static int ideapad_psy_prop_is_writeable(struct power_supply *psy,
2044 					 const struct power_supply_ext *ext,
2045 					 void *data,
2046 					 enum power_supply_property psp)
2047 {
2048 	return true;
2049 }
2050 
2051 static const enum power_supply_property ideapad_power_supply_props[] = {
2052 	POWER_SUPPLY_PROP_CHARGE_TYPES,
2053 };
2054 
2055 static const struct power_supply_ext ideapad_battery_ext = {
2056 	.name			= "ideapad_laptop",
2057 	.properties		= ideapad_power_supply_props,
2058 	.num_properties		= ARRAY_SIZE(ideapad_power_supply_props),
2059 	.charge_types		= (BIT(POWER_SUPPLY_CHARGE_TYPE_STANDARD) |
2060 				   BIT(POWER_SUPPLY_CHARGE_TYPE_LONGLIFE)),
2061 	.get_property		= ideapad_psy_ext_get_prop,
2062 	.set_property		= ideapad_psy_ext_set_prop,
2063 	.property_is_writeable	= ideapad_psy_prop_is_writeable,
2064 };
2065 
ideapad_battery_add(struct power_supply * battery,struct acpi_battery_hook * hook)2066 static int ideapad_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
2067 {
2068 	struct ideapad_private *priv = container_of(hook, struct ideapad_private, battery_hook);
2069 
2070 	return power_supply_register_extension(battery, &ideapad_battery_ext,
2071 					       &priv->platform_device->dev, priv);
2072 }
2073 
ideapad_battery_remove(struct power_supply * battery,struct acpi_battery_hook * hook)2074 static int ideapad_battery_remove(struct power_supply *battery,
2075 				  struct acpi_battery_hook *hook)
2076 {
2077 	power_supply_unregister_extension(battery, &ideapad_battery_ext);
2078 
2079 	return 0;
2080 }
2081 
ideapad_check_features(struct ideapad_private * priv)2082 static int ideapad_check_features(struct ideapad_private *priv)
2083 {
2084 	acpi_handle handle = priv->adev->handle;
2085 	unsigned long val;
2086 	int err;
2087 
2088 	priv->features.set_fn_lock_led =
2089 		set_fn_lock_led || dmi_check_system(set_fn_lock_led_list);
2090 	priv->features.hw_rfkill_switch =
2091 		hw_rfkill_switch || dmi_check_system(hw_rfkill_list);
2092 	priv->features.ctrl_ps2_aux_port =
2093 		ctrl_ps2_aux_port || dmi_check_system(ctrl_ps2_aux_port_list);
2094 	priv->features.touchpad_ctrl_via_ec = touchpad_ctrl_via_ec;
2095 	priv->features.ymc_ec_trigger =
2096 		ymc_ec_trigger || dmi_check_system(ymc_ec_trigger_quirk_dmi_table);
2097 
2098 	if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
2099 		priv->features.fan_mode = true;
2100 
2101 	if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC")) {
2102 		priv->features.conservation_mode = true;
2103 		priv->battery_hook.add_battery = ideapad_battery_add;
2104 		priv->battery_hook.remove_battery = ideapad_battery_remove;
2105 		priv->battery_hook.name = "Ideapad Battery Extension";
2106 
2107 		err = devm_battery_hook_register(&priv->platform_device->dev, &priv->battery_hook);
2108 		if (err)
2109 			return err;
2110 	}
2111 
2112 	if (acpi_has_method(handle, "DYTC"))
2113 		priv->features.dytc = true;
2114 
2115 	if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
2116 		if (!eval_hals(handle, &val)) {
2117 			if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
2118 				priv->features.fn_lock = true;
2119 
2120 			if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val)) {
2121 				priv->features.kbd_bl = true;
2122 				priv->kbd_bl.type = KBD_BL_STANDARD;
2123 			}
2124 
2125 			if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
2126 				priv->features.usb_charging = true;
2127 		}
2128 	}
2129 
2130 	if (acpi_has_method(handle, "KBLC")) {
2131 		if (!eval_kblc(priv->adev->handle, KBD_BL_QUERY_TYPE, &val)) {
2132 			if (val == KBD_BL_TRISTATE_TYPE) {
2133 				priv->features.kbd_bl = true;
2134 				priv->kbd_bl.type = KBD_BL_TRISTATE;
2135 			} else if (val == KBD_BL_TRISTATE_AUTO_TYPE) {
2136 				priv->features.kbd_bl = true;
2137 				priv->kbd_bl.type = KBD_BL_TRISTATE_AUTO;
2138 			} else {
2139 				dev_warn(&priv->platform_device->dev,
2140 					 "Unknown keyboard type: %lu",
2141 					 val);
2142 			}
2143 		}
2144 	}
2145 
2146 	return 0;
2147 }
2148 
2149 #if IS_ENABLED(CONFIG_ACPI_WMI)
2150 /*
2151  * WMI driver
2152  */
2153 enum ideapad_wmi_event_type {
2154 	IDEAPAD_WMI_EVENT_ESC,
2155 	IDEAPAD_WMI_EVENT_FN_KEYS,
2156 };
2157 
2158 struct ideapad_wmi_private {
2159 	enum ideapad_wmi_event_type event;
2160 };
2161 
ideapad_wmi_probe(struct wmi_device * wdev,const void * context)2162 static int ideapad_wmi_probe(struct wmi_device *wdev, const void *context)
2163 {
2164 	struct ideapad_wmi_private *wpriv;
2165 
2166 	wpriv = devm_kzalloc(&wdev->dev, sizeof(*wpriv), GFP_KERNEL);
2167 	if (!wpriv)
2168 		return -ENOMEM;
2169 
2170 	*wpriv = *(const struct ideapad_wmi_private *)context;
2171 
2172 	dev_set_drvdata(&wdev->dev, wpriv);
2173 	return 0;
2174 }
2175 
ideapad_wmi_notify(struct wmi_device * wdev,union acpi_object * data)2176 static void ideapad_wmi_notify(struct wmi_device *wdev, union acpi_object *data)
2177 {
2178 	struct ideapad_wmi_private *wpriv = dev_get_drvdata(&wdev->dev);
2179 	struct ideapad_private *priv;
2180 
2181 	guard(mutex)(&ideapad_shared_mutex);
2182 
2183 	priv = ideapad_shared;
2184 	if (!priv)
2185 		return;
2186 
2187 	switch (wpriv->event) {
2188 	case IDEAPAD_WMI_EVENT_ESC:
2189 		ideapad_input_report(priv, 128);
2190 		break;
2191 	case IDEAPAD_WMI_EVENT_FN_KEYS:
2192 		if (priv->features.set_fn_lock_led) {
2193 			int brightness = ideapad_fn_lock_get(priv);
2194 
2195 			if (brightness >= 0) {
2196 				ideapad_fn_lock_set(priv, brightness);
2197 				ideapad_fn_lock_led_notify(priv, brightness);
2198 			}
2199 		}
2200 
2201 		if (data->type != ACPI_TYPE_INTEGER) {
2202 			dev_warn(&wdev->dev,
2203 				 "WMI event data is not an integer\n");
2204 			break;
2205 		}
2206 
2207 		dev_dbg(&wdev->dev, "WMI fn-key event: 0x%llx\n",
2208 			data->integer.value);
2209 
2210 		/* performance button triggered by 0x3d */
2211 		if (data->integer.value == 0x3d && priv->dytc) {
2212 			platform_profile_cycle();
2213 			break;
2214 		}
2215 
2216 		/* 0x02 FnLock, 0x03 Esc */
2217 		if (data->integer.value == 0x02 || data->integer.value == 0x03)
2218 			ideapad_fn_lock_led_notify(priv, data->integer.value == 0x02);
2219 
2220 		ideapad_input_report(priv,
2221 				     data->integer.value | IDEAPAD_WMI_KEY);
2222 
2223 		break;
2224 	}
2225 }
2226 
2227 static const struct ideapad_wmi_private ideapad_wmi_context_esc = {
2228 	.event = IDEAPAD_WMI_EVENT_ESC
2229 };
2230 
2231 static const struct ideapad_wmi_private ideapad_wmi_context_fn_keys = {
2232 	.event = IDEAPAD_WMI_EVENT_FN_KEYS
2233 };
2234 
2235 static const struct wmi_device_id ideapad_wmi_ids[] = {
2236 	{ "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", &ideapad_wmi_context_esc }, /* Yoga 3 */
2237 	{ "56322276-8493-4CE8-A783-98C991274F5E", &ideapad_wmi_context_esc }, /* Yoga 700 */
2238 	{ "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", &ideapad_wmi_context_fn_keys }, /* Legion 5 */
2239 	{},
2240 };
2241 MODULE_DEVICE_TABLE(wmi, ideapad_wmi_ids);
2242 
2243 static struct wmi_driver ideapad_wmi_driver = {
2244 	.driver = {
2245 		.name = "ideapad_wmi",
2246 	},
2247 	.id_table = ideapad_wmi_ids,
2248 	.probe = ideapad_wmi_probe,
2249 	.notify = ideapad_wmi_notify,
2250 };
2251 
ideapad_wmi_driver_register(void)2252 static int ideapad_wmi_driver_register(void)
2253 {
2254 	return wmi_driver_register(&ideapad_wmi_driver);
2255 }
2256 
ideapad_wmi_driver_unregister(void)2257 static void ideapad_wmi_driver_unregister(void)
2258 {
2259 	return wmi_driver_unregister(&ideapad_wmi_driver);
2260 }
2261 
2262 #else
ideapad_wmi_driver_register(void)2263 static inline int ideapad_wmi_driver_register(void) { return 0; }
ideapad_wmi_driver_unregister(void)2264 static inline void ideapad_wmi_driver_unregister(void) { }
2265 #endif
2266 
2267 /*
2268  * ACPI driver
2269  */
ideapad_acpi_add(struct platform_device * pdev)2270 static int ideapad_acpi_add(struct platform_device *pdev)
2271 {
2272 	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
2273 	struct ideapad_private *priv;
2274 	acpi_status status;
2275 	unsigned long cfg;
2276 	int err, i;
2277 
2278 	if (!adev || eval_int(adev->handle, "_CFG", &cfg))
2279 		return -ENODEV;
2280 
2281 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
2282 	if (!priv)
2283 		return -ENOMEM;
2284 
2285 	dev_set_drvdata(&pdev->dev, priv);
2286 
2287 	priv->cfg = cfg;
2288 	priv->adev = adev;
2289 	priv->platform_device = pdev;
2290 
2291 	err = devm_mutex_init(&pdev->dev, &priv->vpc_mutex);
2292 	if (err)
2293 		return err;
2294 
2295 	err = ideapad_check_features(priv);
2296 	if (err)
2297 		return err;
2298 
2299 	ideapad_debugfs_init(priv);
2300 
2301 	err = ideapad_input_init(priv);
2302 	if (err)
2303 		goto input_failed;
2304 
2305 	err = ideapad_kbd_bl_init(priv);
2306 	if (err) {
2307 		if (err != -ENODEV)
2308 			dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
2309 		else
2310 			dev_info(&pdev->dev, "Keyboard backlight control not available\n");
2311 	}
2312 
2313 	err = ideapad_fn_lock_led_init(priv);
2314 	if (err) {
2315 		if (err != -ENODEV)
2316 			dev_warn(&pdev->dev, "Could not set up FnLock LED: %d\n", err);
2317 		else
2318 			dev_info(&pdev->dev, "FnLock control not available\n");
2319 	}
2320 
2321 	/*
2322 	 * On some models without a hw-switch (the yoga 2 13 at least)
2323 	 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
2324 	 */
2325 	if (!priv->features.hw_rfkill_switch)
2326 		write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
2327 
2328 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
2329 		if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
2330 			ideapad_register_rfkill(priv, i);
2331 
2332 	ideapad_sync_rfk_state(priv);
2333 	ideapad_sync_touchpad_state(priv, false);
2334 
2335 	err = ideapad_dytc_profile_init(priv);
2336 	if (err) {
2337 		if (err != -ENODEV)
2338 			dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
2339 		else
2340 			dev_info(&pdev->dev, "DYTC interface is not available\n");
2341 	}
2342 
2343 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2344 		err = ideapad_backlight_init(priv);
2345 		if (err && err != -ENODEV)
2346 			goto backlight_failed;
2347 	}
2348 
2349 	status = acpi_install_notify_handler(adev->handle,
2350 					     ACPI_DEVICE_NOTIFY,
2351 					     ideapad_acpi_notify, priv);
2352 	if (ACPI_FAILURE(status)) {
2353 		err = -EIO;
2354 		goto notification_failed;
2355 	}
2356 
2357 	err = ideapad_shared_init(priv);
2358 	if (err)
2359 		goto shared_init_failed;
2360 
2361 	ideapad_laptop_register_notifier(&ideapad_laptop_notifier);
2362 
2363 	return 0;
2364 
2365 shared_init_failed:
2366 	acpi_remove_notify_handler(priv->adev->handle,
2367 				   ACPI_DEVICE_NOTIFY,
2368 				   ideapad_acpi_notify);
2369 
2370 notification_failed:
2371 	ideapad_backlight_exit(priv);
2372 
2373 backlight_failed:
2374 	ideapad_dytc_profile_exit(priv);
2375 
2376 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
2377 		ideapad_unregister_rfkill(priv, i);
2378 
2379 	ideapad_fn_lock_led_exit(priv);
2380 	ideapad_kbd_bl_exit(priv);
2381 	ideapad_input_exit(priv);
2382 
2383 input_failed:
2384 	ideapad_debugfs_exit(priv);
2385 
2386 	return err;
2387 }
2388 
ideapad_acpi_remove(struct platform_device * pdev)2389 static void ideapad_acpi_remove(struct platform_device *pdev)
2390 {
2391 	struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
2392 	int i;
2393 
2394 	ideapad_laptop_unregister_notifier(&ideapad_laptop_notifier);
2395 
2396 	ideapad_shared_exit(priv);
2397 
2398 	acpi_remove_notify_handler(priv->adev->handle,
2399 				   ACPI_DEVICE_NOTIFY,
2400 				   ideapad_acpi_notify);
2401 
2402 	ideapad_backlight_exit(priv);
2403 	ideapad_dytc_profile_exit(priv);
2404 
2405 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
2406 		ideapad_unregister_rfkill(priv, i);
2407 
2408 	ideapad_fn_lock_led_exit(priv);
2409 	ideapad_kbd_bl_exit(priv);
2410 	ideapad_input_exit(priv);
2411 	ideapad_debugfs_exit(priv);
2412 }
2413 
2414 #ifdef CONFIG_PM_SLEEP
ideapad_acpi_resume(struct device * dev)2415 static int ideapad_acpi_resume(struct device *dev)
2416 {
2417 	struct ideapad_private *priv = dev_get_drvdata(dev);
2418 
2419 	ideapad_sync_rfk_state(priv);
2420 	ideapad_sync_touchpad_state(priv, false);
2421 
2422 	if (priv->dytc)
2423 		dytc_profile_refresh(priv);
2424 
2425 	return 0;
2426 }
2427 #endif
2428 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
2429 
2430 static const struct acpi_device_id ideapad_device_ids[] = {
2431 	{"VPC2004", 0},
2432 	{"", 0},
2433 };
2434 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
2435 
2436 static struct platform_driver ideapad_acpi_driver = {
2437 	.probe = ideapad_acpi_add,
2438 	.remove = ideapad_acpi_remove,
2439 	.driver = {
2440 		.name   = "ideapad_acpi",
2441 		.pm     = &ideapad_pm,
2442 		.acpi_match_table = ACPI_PTR(ideapad_device_ids),
2443 		.dev_groups = ideapad_attribute_groups,
2444 	},
2445 };
2446 
ideapad_laptop_init(void)2447 static int __init ideapad_laptop_init(void)
2448 {
2449 	int err;
2450 
2451 	err = ideapad_wmi_driver_register();
2452 	if (err)
2453 		return err;
2454 
2455 	err = platform_driver_register(&ideapad_acpi_driver);
2456 	if (err) {
2457 		ideapad_wmi_driver_unregister();
2458 		return err;
2459 	}
2460 
2461 	return 0;
2462 }
module_init(ideapad_laptop_init)2463 module_init(ideapad_laptop_init)
2464 
2465 static void __exit ideapad_laptop_exit(void)
2466 {
2467 	ideapad_wmi_driver_unregister();
2468 	platform_driver_unregister(&ideapad_acpi_driver);
2469 }
2470 module_exit(ideapad_laptop_exit)
2471 
2472 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2473 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
2474 MODULE_LICENSE("GPL");
2475