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