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