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