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