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