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 device *ppdev; /* platform profile device */
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, ¶ms, &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", ¶ms, 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 __ATTRIBUTE_GROUPS(ideapad_attribute);
873
874 /*
875 * DYTC Platform profile
876 */
877 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */
878 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */
879 #define DYTC_CMD_GET 2 /* To get current IC function and mode */
880 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */
881
882 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */
883 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
884 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */
885
886 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */
887 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */
888
889 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
890 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */
891 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */
892
893 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */
894 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */
895 #define DYTC_FUNCTION_MMC 11 /* Function = 11, desk mode */
896
897 #define DYTC_MODE_PERFORM 2 /* High power mode aka performance */
898 #define DYTC_MODE_LOW_POWER 3 /* Low power mode aka quiet */
899 #define DYTC_MODE_BALANCE 0xF /* Default mode aka balanced */
900
901 #define DYTC_SET_COMMAND(function, mode, on) \
902 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
903 (mode) << DYTC_SET_MODE_BIT | \
904 (on) << DYTC_SET_VALID_BIT)
905
906 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
907
908 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
909
convert_dytc_to_profile(int dytcmode,enum platform_profile_option * profile)910 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
911 {
912 switch (dytcmode) {
913 case DYTC_MODE_LOW_POWER:
914 *profile = PLATFORM_PROFILE_LOW_POWER;
915 break;
916 case DYTC_MODE_BALANCE:
917 *profile = PLATFORM_PROFILE_BALANCED;
918 break;
919 case DYTC_MODE_PERFORM:
920 *profile = PLATFORM_PROFILE_PERFORMANCE;
921 break;
922 default: /* Unknown mode */
923 return -EINVAL;
924 }
925
926 return 0;
927 }
928
convert_profile_to_dytc(enum platform_profile_option profile,int * perfmode)929 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
930 {
931 switch (profile) {
932 case PLATFORM_PROFILE_LOW_POWER:
933 *perfmode = DYTC_MODE_LOW_POWER;
934 break;
935 case PLATFORM_PROFILE_BALANCED:
936 *perfmode = DYTC_MODE_BALANCE;
937 break;
938 case PLATFORM_PROFILE_PERFORMANCE:
939 *perfmode = DYTC_MODE_PERFORM;
940 break;
941 default: /* Unknown profile */
942 return -EOPNOTSUPP;
943 }
944
945 return 0;
946 }
947
948 /*
949 * dytc_profile_get: Function to register with platform_profile
950 * handler. Returns current platform profile.
951 */
dytc_profile_get(struct device * dev,enum platform_profile_option * profile)952 static int dytc_profile_get(struct device *dev,
953 enum platform_profile_option *profile)
954 {
955 struct ideapad_dytc_priv *dytc = dev_get_drvdata(dev);
956
957 *profile = dytc->current_profile;
958 return 0;
959 }
960
961 /*
962 * Helper function - check if we are in CQL mode and if we are
963 * - disable CQL,
964 * - run the command
965 * - enable CQL
966 * If not in CQL mode, just run the command
967 */
dytc_cql_command(struct ideapad_private * priv,unsigned long cmd,unsigned long * output)968 static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
969 unsigned long *output)
970 {
971 int err, cmd_err, cur_funcmode;
972
973 /* Determine if we are in CQL mode. This alters the commands we do */
974 err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
975 if (err)
976 return err;
977
978 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
979 /* Check if we're OK to return immediately */
980 if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
981 return 0;
982
983 if (cur_funcmode == DYTC_FUNCTION_CQL) {
984 err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
985 if (err)
986 return err;
987 }
988
989 cmd_err = eval_dytc(priv->adev->handle, cmd, output);
990 /* Check return condition after we've restored CQL state */
991
992 if (cur_funcmode == DYTC_FUNCTION_CQL) {
993 err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
994 if (err)
995 return err;
996 }
997
998 return cmd_err;
999 }
1000
1001 /*
1002 * dytc_profile_set: Function to register with platform_profile
1003 * handler. Sets current platform profile.
1004 */
dytc_profile_set(struct device * dev,enum platform_profile_option profile)1005 static int dytc_profile_set(struct device *dev,
1006 enum platform_profile_option profile)
1007 {
1008 struct ideapad_dytc_priv *dytc = dev_get_drvdata(dev);
1009 struct ideapad_private *priv = dytc->priv;
1010 unsigned long output;
1011 int err;
1012
1013 scoped_guard(mutex_intr, &dytc->mutex) {
1014 if (profile == PLATFORM_PROFILE_BALANCED) {
1015 /* To get back to balanced mode we just issue a reset command */
1016 err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
1017 if (err)
1018 return err;
1019 } else {
1020 int perfmode;
1021
1022 err = convert_profile_to_dytc(profile, &perfmode);
1023 if (err)
1024 return err;
1025
1026 /* Determine if we are in CQL mode. This alters the commands we do */
1027 err = dytc_cql_command(priv,
1028 DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
1029 &output);
1030 if (err)
1031 return err;
1032 }
1033
1034 /* Success - update current profile */
1035 dytc->current_profile = profile;
1036 return 0;
1037 }
1038
1039 return -EINTR;
1040 }
1041
dytc_profile_probe(void * drvdata,unsigned long * choices)1042 static int dytc_profile_probe(void *drvdata, unsigned long *choices)
1043 {
1044 set_bit(PLATFORM_PROFILE_LOW_POWER, choices);
1045 set_bit(PLATFORM_PROFILE_BALANCED, choices);
1046 set_bit(PLATFORM_PROFILE_PERFORMANCE, choices);
1047
1048 return 0;
1049 }
1050
dytc_profile_refresh(struct ideapad_private * priv)1051 static void dytc_profile_refresh(struct ideapad_private *priv)
1052 {
1053 enum platform_profile_option profile;
1054 unsigned long output;
1055 int err, perfmode;
1056
1057 scoped_guard(mutex, &priv->dytc->mutex)
1058 err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
1059 if (err)
1060 return;
1061
1062 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
1063
1064 if (convert_dytc_to_profile(perfmode, &profile))
1065 return;
1066
1067 if (profile != priv->dytc->current_profile) {
1068 priv->dytc->current_profile = profile;
1069 platform_profile_notify(priv->dytc->ppdev);
1070 }
1071 }
1072
1073 static const struct dmi_system_id ideapad_dytc_v4_allow_table[] = {
1074 {
1075 /* Ideapad 5 Pro 16ACH6 */
1076 .matches = {
1077 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1078 DMI_MATCH(DMI_PRODUCT_NAME, "82L5")
1079 }
1080 },
1081 {
1082 /* Ideapad 5 15ITL05 */
1083 .matches = {
1084 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1085 DMI_MATCH(DMI_PRODUCT_VERSION, "IdeaPad 5 15ITL05")
1086 }
1087 },
1088 {}
1089 };
1090
1091 static const struct platform_profile_ops dytc_profile_ops = {
1092 .probe = dytc_profile_probe,
1093 .profile_get = dytc_profile_get,
1094 .profile_set = dytc_profile_set,
1095 };
1096
ideapad_dytc_profile_init(struct ideapad_private * priv)1097 static int ideapad_dytc_profile_init(struct ideapad_private *priv)
1098 {
1099 int err, dytc_version;
1100 unsigned long output;
1101
1102 if (!priv->features.dytc)
1103 return -ENODEV;
1104
1105 err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
1106 /* For all other errors we can flag the failure */
1107 if (err)
1108 return err;
1109
1110 /* Check DYTC is enabled and supports mode setting */
1111 if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output)) {
1112 dev_info(&priv->platform_device->dev, "DYTC_QUERY_ENABLE_BIT returned false\n");
1113 return -ENODEV;
1114 }
1115
1116 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
1117
1118 if (dytc_version < 4) {
1119 dev_info(&priv->platform_device->dev, "DYTC_VERSION < 4 is not supported\n");
1120 return -ENODEV;
1121 }
1122
1123 if (dytc_version < 5 &&
1124 !(allow_v4_dytc || dmi_check_system(ideapad_dytc_v4_allow_table))) {
1125 dev_info(&priv->platform_device->dev,
1126 "DYTC_VERSION 4 support may not work. Pass ideapad_laptop.allow_v4_dytc=Y on the kernel commandline to enable\n");
1127 return -ENODEV;
1128 }
1129
1130 priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
1131 if (!priv->dytc)
1132 return -ENOMEM;
1133
1134 mutex_init(&priv->dytc->mutex);
1135
1136 priv->dytc->priv = priv;
1137
1138 /* Create platform_profile structure and register */
1139 priv->dytc->ppdev = devm_platform_profile_register(&priv->platform_device->dev,
1140 "ideapad-laptop", priv->dytc,
1141 &dytc_profile_ops);
1142 if (IS_ERR(priv->dytc->ppdev)) {
1143 err = PTR_ERR(priv->dytc->ppdev);
1144 goto pp_reg_failed;
1145 }
1146
1147 /* Ensure initial values are correct */
1148 dytc_profile_refresh(priv);
1149
1150 return 0;
1151
1152 pp_reg_failed:
1153 mutex_destroy(&priv->dytc->mutex);
1154 kfree(priv->dytc);
1155 priv->dytc = NULL;
1156
1157 return err;
1158 }
1159
ideapad_dytc_profile_exit(struct ideapad_private * priv)1160 static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
1161 {
1162 if (!priv->dytc)
1163 return;
1164
1165 mutex_destroy(&priv->dytc->mutex);
1166 kfree(priv->dytc);
1167
1168 priv->dytc = NULL;
1169 }
1170
1171 /*
1172 * Rfkill
1173 */
1174 struct ideapad_rfk_data {
1175 char *name;
1176 int cfgbit;
1177 int opcode;
1178 int type;
1179 };
1180
1181 static const struct ideapad_rfk_data ideapad_rfk_data[] = {
1182 { "ideapad_wlan", CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
1183 { "ideapad_bluetooth", CFG_CAP_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
1184 { "ideapad_3g", CFG_CAP_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
1185 };
1186
ideapad_rfk_set(void * data,bool blocked)1187 static int ideapad_rfk_set(void *data, bool blocked)
1188 {
1189 struct ideapad_rfk_priv *priv = data;
1190 int opcode = ideapad_rfk_data[priv->dev].opcode;
1191
1192 guard(mutex)(&priv->priv->vpc_mutex);
1193
1194 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
1195 }
1196
1197 static const struct rfkill_ops ideapad_rfk_ops = {
1198 .set_block = ideapad_rfk_set,
1199 };
1200
ideapad_sync_rfk_state(struct ideapad_private * priv)1201 static void ideapad_sync_rfk_state(struct ideapad_private *priv)
1202 {
1203 unsigned long hw_blocked = 0;
1204 int i;
1205
1206 if (priv->features.hw_rfkill_switch) {
1207 guard(mutex)(&priv->vpc_mutex);
1208
1209 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
1210 return;
1211 hw_blocked = !hw_blocked;
1212 }
1213
1214 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1215 if (priv->rfk[i])
1216 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
1217 }
1218
ideapad_register_rfkill(struct ideapad_private * priv,int dev)1219 static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
1220 {
1221 unsigned long rf_enabled;
1222 int err;
1223
1224 if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
1225 /* Force to enable bluetooth when no_bt_rfkill=1 */
1226 write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
1227 return 0;
1228 }
1229
1230 priv->rfk_priv[dev].dev = dev;
1231 priv->rfk_priv[dev].priv = priv;
1232
1233 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
1234 &priv->platform_device->dev,
1235 ideapad_rfk_data[dev].type,
1236 &ideapad_rfk_ops,
1237 &priv->rfk_priv[dev]);
1238 if (!priv->rfk[dev])
1239 return -ENOMEM;
1240
1241 err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
1242 if (err)
1243 rf_enabled = 1;
1244
1245 rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
1246
1247 err = rfkill_register(priv->rfk[dev]);
1248 if (err)
1249 rfkill_destroy(priv->rfk[dev]);
1250
1251 return err;
1252 }
1253
ideapad_unregister_rfkill(struct ideapad_private * priv,int dev)1254 static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1255 {
1256 if (!priv->rfk[dev])
1257 return;
1258
1259 rfkill_unregister(priv->rfk[dev]);
1260 rfkill_destroy(priv->rfk[dev]);
1261 }
1262
1263 /*
1264 * input device
1265 */
1266 #define IDEAPAD_WMI_KEY 0x100
1267
1268 static const struct key_entry ideapad_keymap[] = {
1269 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
1270 { KE_KEY, 7, { KEY_CAMERA } },
1271 { KE_KEY, 8, { KEY_MICMUTE } },
1272 { KE_KEY, 11, { KEY_F16 } },
1273 { KE_KEY, 13, { KEY_WLAN } },
1274 { KE_KEY, 16, { KEY_PROG1 } },
1275 { KE_KEY, 17, { KEY_PROG2 } },
1276 { KE_KEY, 64, { KEY_PROG3 } },
1277 { KE_KEY, 65, { KEY_PROG4 } },
1278 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
1279 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
1280 { KE_KEY, 128, { KEY_ESC } },
1281
1282 /*
1283 * WMI keys
1284 */
1285
1286 /* FnLock (handled by the firmware) */
1287 { KE_IGNORE, 0x02 | IDEAPAD_WMI_KEY },
1288 /* Esc (handled by the firmware) */
1289 { KE_IGNORE, 0x03 | IDEAPAD_WMI_KEY },
1290 /* Customizable Lenovo Hotkey ("star" with 'S' inside) */
1291 { KE_KEY, 0x01 | IDEAPAD_WMI_KEY, { KEY_FAVORITES } },
1292 { KE_KEY, 0x04 | IDEAPAD_WMI_KEY, { KEY_SELECTIVE_SCREENSHOT } },
1293 /* Lenovo Support */
1294 { KE_KEY, 0x07 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1295 { KE_KEY, 0x0e | IDEAPAD_WMI_KEY, { KEY_PICKUP_PHONE } },
1296 { KE_KEY, 0x0f | IDEAPAD_WMI_KEY, { KEY_HANGUP_PHONE } },
1297 /* Refresh Rate Toggle (Fn+R) */
1298 { KE_KEY, 0x10 | IDEAPAD_WMI_KEY, { KEY_REFRESH_RATE_TOGGLE } },
1299 /* Dark mode toggle */
1300 { KE_KEY, 0x13 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
1301 /* Sound profile switch */
1302 { KE_KEY, 0x12 | IDEAPAD_WMI_KEY, { KEY_PROG2 } },
1303 /* Lenovo Virtual Background application */
1304 { KE_KEY, 0x28 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
1305 /* Lenovo Support */
1306 { KE_KEY, 0x27 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1307 /* Refresh Rate Toggle */
1308 { KE_KEY, 0x0a | IDEAPAD_WMI_KEY, { KEY_REFRESH_RATE_TOGGLE } },
1309 /* Specific to some newer models */
1310 { KE_KEY, 0x3e | IDEAPAD_WMI_KEY, { KEY_MICMUTE } },
1311 { KE_KEY, 0x3f | IDEAPAD_WMI_KEY, { KEY_RFKILL } },
1312 /* Star- (User Assignable Key) */
1313 { KE_KEY, 0x44 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
1314 /* Eye */
1315 { KE_KEY, 0x45 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
1316 /* Performance toggle also Fn+Q, handled inside ideapad_wmi_notify() */
1317 { KE_KEY, 0x3d | IDEAPAD_WMI_KEY, { KEY_PROG4 } },
1318 /* shift + prtsc */
1319 { KE_KEY, 0x2d | IDEAPAD_WMI_KEY, { KEY_CUT } },
1320 { KE_KEY, 0x29 | IDEAPAD_WMI_KEY, { KEY_TOUCHPAD_TOGGLE } },
1321 { KE_KEY, 0x2a | IDEAPAD_WMI_KEY, { KEY_ROOT_MENU } },
1322
1323 { KE_END },
1324 };
1325
ideapad_input_init(struct ideapad_private * priv)1326 static int ideapad_input_init(struct ideapad_private *priv)
1327 {
1328 struct input_dev *inputdev;
1329 int err;
1330
1331 inputdev = input_allocate_device();
1332 if (!inputdev)
1333 return -ENOMEM;
1334
1335 inputdev->name = "Ideapad extra buttons";
1336 inputdev->phys = "ideapad/input0";
1337 inputdev->id.bustype = BUS_HOST;
1338 inputdev->dev.parent = &priv->platform_device->dev;
1339
1340 err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1341 if (err) {
1342 dev_err(&priv->platform_device->dev,
1343 "Could not set up input device keymap: %d\n", err);
1344 goto err_free_dev;
1345 }
1346
1347 err = input_register_device(inputdev);
1348 if (err) {
1349 dev_err(&priv->platform_device->dev,
1350 "Could not register input device: %d\n", err);
1351 goto err_free_dev;
1352 }
1353
1354 priv->inputdev = inputdev;
1355
1356 return 0;
1357
1358 err_free_dev:
1359 input_free_device(inputdev);
1360
1361 return err;
1362 }
1363
ideapad_input_exit(struct ideapad_private * priv)1364 static void ideapad_input_exit(struct ideapad_private *priv)
1365 {
1366 input_unregister_device(priv->inputdev);
1367 priv->inputdev = NULL;
1368 }
1369
ideapad_input_report(struct ideapad_private * priv,unsigned long scancode)1370 static void ideapad_input_report(struct ideapad_private *priv,
1371 unsigned long scancode)
1372 {
1373 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1374 }
1375
ideapad_input_novokey(struct ideapad_private * priv)1376 static void ideapad_input_novokey(struct ideapad_private *priv)
1377 {
1378 unsigned long long_pressed;
1379
1380 scoped_guard(mutex, &priv->vpc_mutex)
1381 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1382 return;
1383
1384 if (long_pressed)
1385 ideapad_input_report(priv, 17);
1386 else
1387 ideapad_input_report(priv, 16);
1388 }
1389
ideapad_check_special_buttons(struct ideapad_private * priv)1390 static void ideapad_check_special_buttons(struct ideapad_private *priv)
1391 {
1392 unsigned long bit, value;
1393
1394 scoped_guard(mutex, &priv->vpc_mutex)
1395 if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1396 return;
1397
1398 for_each_set_bit (bit, &value, 16) {
1399 switch (bit) {
1400 case 6: /* Z570 */
1401 case 0: /* Z580 */
1402 /* Thermal Management / Performance Mode button */
1403 if (priv->dytc)
1404 platform_profile_cycle();
1405 else
1406 ideapad_input_report(priv, 65);
1407 break;
1408 case 1:
1409 /* OneKey Theater button */
1410 ideapad_input_report(priv, 64);
1411 break;
1412 default:
1413 dev_info(&priv->platform_device->dev,
1414 "Unknown special button: %lu\n", bit);
1415 break;
1416 }
1417 }
1418 }
1419
1420 /*
1421 * backlight
1422 */
ideapad_backlight_get_brightness(struct backlight_device * blightdev)1423 static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1424 {
1425 struct ideapad_private *priv = bl_get_data(blightdev);
1426 unsigned long now;
1427 int err;
1428
1429 guard(mutex)(&priv->vpc_mutex);
1430
1431 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1432 if (err)
1433 return err;
1434
1435 return now;
1436 }
1437
ideapad_backlight_update_status(struct backlight_device * blightdev)1438 static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1439 {
1440 struct ideapad_private *priv = bl_get_data(blightdev);
1441 int err;
1442
1443 guard(mutex)(&priv->vpc_mutex);
1444
1445 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1446 blightdev->props.brightness);
1447 if (err)
1448 return err;
1449
1450 err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1451 blightdev->props.power != BACKLIGHT_POWER_OFF);
1452 if (err)
1453 return err;
1454
1455 return 0;
1456 }
1457
1458 static const struct backlight_ops ideapad_backlight_ops = {
1459 .get_brightness = ideapad_backlight_get_brightness,
1460 .update_status = ideapad_backlight_update_status,
1461 };
1462
ideapad_backlight_init(struct ideapad_private * priv)1463 static int ideapad_backlight_init(struct ideapad_private *priv)
1464 {
1465 struct backlight_device *blightdev;
1466 struct backlight_properties props;
1467 unsigned long max, now, power;
1468 int err;
1469
1470 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1471 if (err)
1472 return err;
1473
1474 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1475 if (err)
1476 return err;
1477
1478 err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1479 if (err)
1480 return err;
1481
1482 memset(&props, 0, sizeof(props));
1483
1484 props.max_brightness = max;
1485 props.type = BACKLIGHT_PLATFORM;
1486
1487 blightdev = backlight_device_register("ideapad",
1488 &priv->platform_device->dev,
1489 priv,
1490 &ideapad_backlight_ops,
1491 &props);
1492 if (IS_ERR(blightdev)) {
1493 err = PTR_ERR(blightdev);
1494 dev_err(&priv->platform_device->dev,
1495 "Could not register backlight device: %d\n", err);
1496 return err;
1497 }
1498
1499 priv->blightdev = blightdev;
1500 blightdev->props.brightness = now;
1501 blightdev->props.power = power ? BACKLIGHT_POWER_ON : BACKLIGHT_POWER_OFF;
1502
1503 backlight_update_status(blightdev);
1504
1505 return 0;
1506 }
1507
ideapad_backlight_exit(struct ideapad_private * priv)1508 static void ideapad_backlight_exit(struct ideapad_private *priv)
1509 {
1510 backlight_device_unregister(priv->blightdev);
1511 priv->blightdev = NULL;
1512 }
1513
ideapad_backlight_notify_power(struct ideapad_private * priv)1514 static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1515 {
1516 struct backlight_device *blightdev = priv->blightdev;
1517 unsigned long power;
1518
1519 if (!blightdev)
1520 return;
1521
1522 guard(mutex)(&priv->vpc_mutex);
1523
1524 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1525 return;
1526
1527 blightdev->props.power = power ? BACKLIGHT_POWER_ON : BACKLIGHT_POWER_OFF;
1528 }
1529
ideapad_backlight_notify_brightness(struct ideapad_private * priv)1530 static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1531 {
1532 unsigned long now;
1533
1534 /* if we control brightness via acpi video driver */
1535 if (!priv->blightdev)
1536 scoped_guard(mutex, &priv->vpc_mutex)
1537 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1538 else
1539 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1540 }
1541
1542 /*
1543 * keyboard backlight
1544 */
ideapad_kbd_bl_check_tristate(int type)1545 static int ideapad_kbd_bl_check_tristate(int type)
1546 {
1547 return (type == KBD_BL_TRISTATE) || (type == KBD_BL_TRISTATE_AUTO);
1548 }
1549
ideapad_kbd_bl_brightness_get(struct ideapad_private * priv)1550 static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1551 {
1552 unsigned long value;
1553 int err;
1554
1555 if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
1556 err = eval_kblc(priv->adev->handle,
1557 FIELD_PREP(KBD_BL_COMMAND_TYPE, priv->kbd_bl.type) |
1558 KBD_BL_COMMAND_GET,
1559 &value);
1560
1561 if (err)
1562 return err;
1563
1564 /* Convert returned value to brightness level */
1565 value = FIELD_GET(KBD_BL_GET_BRIGHTNESS, value);
1566
1567 /* Off, low or high */
1568 if (value <= priv->kbd_bl.led.max_brightness)
1569 return value;
1570
1571 /* Auto, report as off */
1572 if (value == priv->kbd_bl.led.max_brightness + 1)
1573 return 0;
1574
1575 /* Unknown value */
1576 dev_warn(&priv->platform_device->dev,
1577 "Unknown keyboard backlight value: %lu", value);
1578 return -EINVAL;
1579 }
1580
1581 err = eval_hals(priv->adev->handle, &value);
1582 if (err)
1583 return err;
1584
1585 return !!test_bit(HALS_KBD_BL_STATE_BIT, &value);
1586 }
1587
ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev * led_cdev)1588 static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1589 {
1590 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1591
1592 return ideapad_kbd_bl_brightness_get(priv);
1593 }
1594
ideapad_kbd_bl_brightness_set(struct ideapad_private * priv,unsigned int brightness)1595 static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1596 {
1597 int err;
1598 unsigned long value;
1599 int type = priv->kbd_bl.type;
1600
1601 if (ideapad_kbd_bl_check_tristate(type)) {
1602 if (brightness > priv->kbd_bl.led.max_brightness)
1603 return -EINVAL;
1604
1605 value = FIELD_PREP(KBD_BL_SET_BRIGHTNESS, brightness) |
1606 FIELD_PREP(KBD_BL_COMMAND_TYPE, type) |
1607 KBD_BL_COMMAND_SET;
1608 err = exec_kblc(priv->adev->handle, value);
1609 } else {
1610 err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1611 }
1612
1613 if (err)
1614 return err;
1615
1616 priv->kbd_bl.last_brightness = brightness;
1617
1618 return 0;
1619 }
1620
ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev * led_cdev,enum led_brightness brightness)1621 static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1622 enum led_brightness brightness)
1623 {
1624 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1625
1626 return ideapad_kbd_bl_brightness_set(priv, brightness);
1627 }
1628
ideapad_kbd_bl_notify(struct ideapad_private * priv)1629 static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1630 {
1631 int brightness;
1632
1633 if (!priv->kbd_bl.initialized)
1634 return;
1635
1636 brightness = ideapad_kbd_bl_brightness_get(priv);
1637 if (brightness < 0)
1638 return;
1639
1640 if (brightness == priv->kbd_bl.last_brightness)
1641 return;
1642
1643 priv->kbd_bl.last_brightness = brightness;
1644
1645 led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1646 }
1647
ideapad_kbd_bl_init(struct ideapad_private * priv)1648 static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1649 {
1650 int brightness, err;
1651
1652 if (!priv->features.kbd_bl)
1653 return -ENODEV;
1654
1655 if (WARN_ON(priv->kbd_bl.initialized))
1656 return -EEXIST;
1657
1658 if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) {
1659 priv->kbd_bl.led.max_brightness = 2;
1660 } else {
1661 priv->kbd_bl.led.max_brightness = 1;
1662 }
1663
1664 brightness = ideapad_kbd_bl_brightness_get(priv);
1665 if (brightness < 0)
1666 return brightness;
1667
1668 priv->kbd_bl.last_brightness = brightness;
1669 priv->kbd_bl.led.name = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1670 priv->kbd_bl.led.brightness_get = ideapad_kbd_bl_led_cdev_brightness_get;
1671 priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1672 priv->kbd_bl.led.flags = LED_BRIGHT_HW_CHANGED;
1673
1674 err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1675 if (err)
1676 return err;
1677
1678 priv->kbd_bl.initialized = true;
1679
1680 return 0;
1681 }
1682
ideapad_kbd_bl_exit(struct ideapad_private * priv)1683 static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1684 {
1685 if (!priv->kbd_bl.initialized)
1686 return;
1687
1688 priv->kbd_bl.initialized = false;
1689
1690 led_classdev_unregister(&priv->kbd_bl.led);
1691 }
1692
1693 /*
1694 * FnLock LED
1695 */
ideapad_fn_lock_led_cdev_get(struct led_classdev * led_cdev)1696 static enum led_brightness ideapad_fn_lock_led_cdev_get(struct led_classdev *led_cdev)
1697 {
1698 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, fn_lock.led);
1699
1700 return ideapad_fn_lock_get(priv);
1701 }
1702
ideapad_fn_lock_led_cdev_set(struct led_classdev * led_cdev,enum led_brightness brightness)1703 static int ideapad_fn_lock_led_cdev_set(struct led_classdev *led_cdev,
1704 enum led_brightness brightness)
1705 {
1706 struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, fn_lock.led);
1707
1708 return ideapad_fn_lock_set(priv, brightness);
1709 }
1710
ideapad_fn_lock_led_init(struct ideapad_private * priv)1711 static int ideapad_fn_lock_led_init(struct ideapad_private *priv)
1712 {
1713 int brightness, err;
1714
1715 if (!priv->features.fn_lock)
1716 return -ENODEV;
1717
1718 if (WARN_ON(priv->fn_lock.initialized))
1719 return -EEXIST;
1720
1721 priv->fn_lock.led.max_brightness = 1;
1722
1723 brightness = ideapad_fn_lock_get(priv);
1724 if (brightness < 0)
1725 return brightness;
1726
1727 priv->fn_lock.last_brightness = brightness;
1728 priv->fn_lock.led.name = "platform::" LED_FUNCTION_FNLOCK;
1729 priv->fn_lock.led.brightness_get = ideapad_fn_lock_led_cdev_get;
1730 priv->fn_lock.led.brightness_set_blocking = ideapad_fn_lock_led_cdev_set;
1731 priv->fn_lock.led.flags = LED_BRIGHT_HW_CHANGED;
1732
1733 err = led_classdev_register(&priv->platform_device->dev, &priv->fn_lock.led);
1734 if (err)
1735 return err;
1736
1737 priv->fn_lock.initialized = true;
1738
1739 return 0;
1740 }
1741
ideapad_fn_lock_led_exit(struct ideapad_private * priv)1742 static void ideapad_fn_lock_led_exit(struct ideapad_private *priv)
1743 {
1744 if (!priv->fn_lock.initialized)
1745 return;
1746
1747 priv->fn_lock.initialized = false;
1748
1749 led_classdev_unregister(&priv->fn_lock.led);
1750 }
1751
1752 /*
1753 * module init/exit
1754 */
ideapad_sync_touchpad_state(struct ideapad_private * priv,bool send_events)1755 static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_events)
1756 {
1757 unsigned long value;
1758 unsigned char param;
1759 int ret;
1760
1761 /* Without reading from EC touchpad LED doesn't switch state */
1762 scoped_guard(mutex, &priv->vpc_mutex)
1763 ret = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value);
1764 if (ret)
1765 return;
1766
1767 /*
1768 * Some IdeaPads don't really turn off touchpad - they only
1769 * switch the LED state. We (de)activate KBC AUX port to turn
1770 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1771 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1772 */
1773 if (priv->features.ctrl_ps2_aux_port)
1774 i8042_command(¶m, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1775
1776 /*
1777 * On older models the EC controls the touchpad and toggles it on/off
1778 * itself, in this case we report KEY_TOUCHPAD_ON/_OFF. Some models do
1779 * an acpi-notify with VPC bit 5 set on resume, so this function get
1780 * called with send_events=true on every resume. Therefor if the EC did
1781 * not toggle, do nothing to avoid sending spurious KEY_TOUCHPAD_TOGGLE.
1782 */
1783 if (send_events && value != priv->r_touchpad_val) {
1784 ideapad_input_report(priv, value ? 67 : 66);
1785 sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1786 }
1787
1788 priv->r_touchpad_val = value;
1789 }
1790
1791 static const struct dmi_system_id ymc_ec_trigger_quirk_dmi_table[] = {
1792 {
1793 /* Lenovo Yoga 7 14ARB7 */
1794 .matches = {
1795 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1796 DMI_MATCH(DMI_PRODUCT_NAME, "82QF"),
1797 },
1798 },
1799 {
1800 /* Lenovo Yoga 7 14ACN6 */
1801 .matches = {
1802 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1803 DMI_MATCH(DMI_PRODUCT_NAME, "82N7"),
1804 },
1805 },
1806 { }
1807 };
1808
ideapad_laptop_trigger_ec(void)1809 static void ideapad_laptop_trigger_ec(void)
1810 {
1811 struct ideapad_private *priv;
1812 int ret;
1813
1814 guard(mutex)(&ideapad_shared_mutex);
1815
1816 priv = ideapad_shared;
1817 if (!priv)
1818 return;
1819
1820 if (!priv->features.ymc_ec_trigger)
1821 return;
1822
1823 scoped_guard(mutex, &priv->vpc_mutex)
1824 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_YMC, 1);
1825 if (ret)
1826 dev_warn(&priv->platform_device->dev, "Could not write YMC: %d\n", ret);
1827 }
1828
ideapad_laptop_nb_notify(struct notifier_block * nb,unsigned long action,void * data)1829 static int ideapad_laptop_nb_notify(struct notifier_block *nb,
1830 unsigned long action, void *data)
1831 {
1832 switch (action) {
1833 case IDEAPAD_LAPTOP_YMC_EVENT:
1834 ideapad_laptop_trigger_ec();
1835 break;
1836 }
1837
1838 return 0;
1839 }
1840
1841 static struct notifier_block ideapad_laptop_notifier = {
1842 .notifier_call = ideapad_laptop_nb_notify,
1843 };
1844
1845 static BLOCKING_NOTIFIER_HEAD(ideapad_laptop_chain_head);
1846
ideapad_laptop_register_notifier(struct notifier_block * nb)1847 int ideapad_laptop_register_notifier(struct notifier_block *nb)
1848 {
1849 return blocking_notifier_chain_register(&ideapad_laptop_chain_head, nb);
1850 }
1851 EXPORT_SYMBOL_NS_GPL(ideapad_laptop_register_notifier, "IDEAPAD_LAPTOP");
1852
ideapad_laptop_unregister_notifier(struct notifier_block * nb)1853 int ideapad_laptop_unregister_notifier(struct notifier_block *nb)
1854 {
1855 return blocking_notifier_chain_unregister(&ideapad_laptop_chain_head, nb);
1856 }
1857 EXPORT_SYMBOL_NS_GPL(ideapad_laptop_unregister_notifier, "IDEAPAD_LAPTOP");
1858
ideapad_laptop_call_notifier(unsigned long action,void * data)1859 void ideapad_laptop_call_notifier(unsigned long action, void *data)
1860 {
1861 blocking_notifier_call_chain(&ideapad_laptop_chain_head, action, data);
1862 }
1863 EXPORT_SYMBOL_NS_GPL(ideapad_laptop_call_notifier, "IDEAPAD_LAPTOP");
1864
ideapad_acpi_notify(acpi_handle handle,u32 event,void * data)1865 static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1866 {
1867 struct ideapad_private *priv = data;
1868 unsigned long vpc1, vpc2, bit;
1869
1870 scoped_guard(mutex, &priv->vpc_mutex) {
1871 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1872 return;
1873
1874 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1875 return;
1876 }
1877
1878 vpc1 = (vpc2 << 8) | vpc1;
1879
1880 for_each_set_bit (bit, &vpc1, 16) {
1881 switch (bit) {
1882 case 13:
1883 case 11:
1884 case 8:
1885 case 7:
1886 case 6:
1887 ideapad_input_report(priv, bit);
1888 break;
1889 case 10:
1890 /*
1891 * This event gets send on a Yoga 300-11IBR when the EC
1892 * believes that the device has changed between laptop/
1893 * tent/stand/tablet mode. The EC relies on getting
1894 * angle info from 2 accelerometers through a special
1895 * windows service calling a DSM on the DUAL250E ACPI-
1896 * device. Linux does not do this, making the laptop/
1897 * tent/stand/tablet mode info unreliable, so we simply
1898 * ignore these events.
1899 */
1900 break;
1901 case 9:
1902 ideapad_sync_rfk_state(priv);
1903 break;
1904 case 5:
1905 ideapad_sync_touchpad_state(priv, true);
1906 break;
1907 case 4:
1908 ideapad_backlight_notify_brightness(priv);
1909 break;
1910 case 3:
1911 ideapad_input_novokey(priv);
1912 break;
1913 case 2:
1914 ideapad_backlight_notify_power(priv);
1915 break;
1916 case KBD_BL_KBLC_CHANGED_EVENT:
1917 case 1:
1918 /*
1919 * Some IdeaPads report event 1 every ~20
1920 * seconds while on battery power; some
1921 * report this when changing to/from tablet
1922 * mode; some report this when the keyboard
1923 * backlight has changed.
1924 */
1925 ideapad_kbd_bl_notify(priv);
1926 break;
1927 case 0:
1928 ideapad_check_special_buttons(priv);
1929 break;
1930 default:
1931 dev_info(&priv->platform_device->dev,
1932 "Unknown event: %lu\n", bit);
1933 }
1934 }
1935 }
1936
1937 /* On some models we need to call exec_sals(SALS_FNLOCK_ON/OFF) to set the LED */
1938 static const struct dmi_system_id set_fn_lock_led_list[] = {
1939 {
1940 /* https://bugzilla.kernel.org/show_bug.cgi?id=212671 */
1941 .matches = {
1942 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1943 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion R7000P2020H"),
1944 }
1945 },
1946 {
1947 .matches = {
1948 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1949 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion 5 15ARH05"),
1950 }
1951 },
1952 {}
1953 };
1954
1955 /*
1956 * Some ideapads have a hardware rfkill switch, but most do not have one.
1957 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1958 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1959 * There used to be a long list of DMI ids for models without a hw rfkill
1960 * switch here, but that resulted in playing whack a mole.
1961 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1962 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1963 * hw-blocked results in an empty SSID list, which is a much more benign
1964 * failure mode.
1965 * So the default now is the much safer option of assuming there is no
1966 * hardware rfkill switch. This default also actually matches most hardware,
1967 * since having a hw rfkill switch is quite rare on modern hardware, so this
1968 * also leads to a much shorter list.
1969 */
1970 static const struct dmi_system_id hw_rfkill_list[] = {
1971 {}
1972 };
1973
1974 /*
1975 * On some models the EC toggles the touchpad muted LED on touchpad toggle
1976 * hotkey presses, but the EC does not actually disable the touchpad itself.
1977 * On these models the driver needs to explicitly enable/disable the i8042
1978 * (PS/2) aux port.
1979 */
1980 static const struct dmi_system_id ctrl_ps2_aux_port_list[] = {
1981 {
1982 /* Lenovo Ideapad Z570 */
1983 .matches = {
1984 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1985 DMI_MATCH(DMI_PRODUCT_VERSION, "Ideapad Z570"),
1986 },
1987 },
1988 {}
1989 };
1990
ideapad_check_features(struct ideapad_private * priv)1991 static void ideapad_check_features(struct ideapad_private *priv)
1992 {
1993 acpi_handle handle = priv->adev->handle;
1994 unsigned long val;
1995
1996 priv->features.set_fn_lock_led =
1997 set_fn_lock_led || dmi_check_system(set_fn_lock_led_list);
1998 priv->features.hw_rfkill_switch =
1999 hw_rfkill_switch || dmi_check_system(hw_rfkill_list);
2000 priv->features.ctrl_ps2_aux_port =
2001 ctrl_ps2_aux_port || dmi_check_system(ctrl_ps2_aux_port_list);
2002 priv->features.touchpad_ctrl_via_ec = touchpad_ctrl_via_ec;
2003 priv->features.ymc_ec_trigger =
2004 ymc_ec_trigger || dmi_check_system(ymc_ec_trigger_quirk_dmi_table);
2005
2006 if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
2007 priv->features.fan_mode = true;
2008
2009 if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
2010 priv->features.conservation_mode = true;
2011
2012 if (acpi_has_method(handle, "DYTC"))
2013 priv->features.dytc = true;
2014
2015 if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
2016 if (!eval_hals(handle, &val)) {
2017 if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
2018 priv->features.fn_lock = true;
2019
2020 if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val)) {
2021 priv->features.kbd_bl = true;
2022 priv->kbd_bl.type = KBD_BL_STANDARD;
2023 }
2024
2025 if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
2026 priv->features.usb_charging = true;
2027 }
2028 }
2029
2030 if (acpi_has_method(handle, "KBLC")) {
2031 if (!eval_kblc(priv->adev->handle, KBD_BL_QUERY_TYPE, &val)) {
2032 if (val == KBD_BL_TRISTATE_TYPE) {
2033 priv->features.kbd_bl = true;
2034 priv->kbd_bl.type = KBD_BL_TRISTATE;
2035 } else if (val == KBD_BL_TRISTATE_AUTO_TYPE) {
2036 priv->features.kbd_bl = true;
2037 priv->kbd_bl.type = KBD_BL_TRISTATE_AUTO;
2038 } else {
2039 dev_warn(&priv->platform_device->dev,
2040 "Unknown keyboard type: %lu",
2041 val);
2042 }
2043 }
2044 }
2045 }
2046
2047 #if IS_ENABLED(CONFIG_ACPI_WMI)
2048 /*
2049 * WMI driver
2050 */
2051 enum ideapad_wmi_event_type {
2052 IDEAPAD_WMI_EVENT_ESC,
2053 IDEAPAD_WMI_EVENT_FN_KEYS,
2054 };
2055
2056 struct ideapad_wmi_private {
2057 enum ideapad_wmi_event_type event;
2058 };
2059
ideapad_wmi_probe(struct wmi_device * wdev,const void * context)2060 static int ideapad_wmi_probe(struct wmi_device *wdev, const void *context)
2061 {
2062 struct ideapad_wmi_private *wpriv;
2063
2064 wpriv = devm_kzalloc(&wdev->dev, sizeof(*wpriv), GFP_KERNEL);
2065 if (!wpriv)
2066 return -ENOMEM;
2067
2068 *wpriv = *(const struct ideapad_wmi_private *)context;
2069
2070 dev_set_drvdata(&wdev->dev, wpriv);
2071 return 0;
2072 }
2073
ideapad_wmi_notify(struct wmi_device * wdev,union acpi_object * data)2074 static void ideapad_wmi_notify(struct wmi_device *wdev, union acpi_object *data)
2075 {
2076 struct ideapad_wmi_private *wpriv = dev_get_drvdata(&wdev->dev);
2077 struct ideapad_private *priv;
2078
2079 guard(mutex)(&ideapad_shared_mutex);
2080
2081 priv = ideapad_shared;
2082 if (!priv)
2083 return;
2084
2085 switch (wpriv->event) {
2086 case IDEAPAD_WMI_EVENT_ESC:
2087 ideapad_input_report(priv, 128);
2088 break;
2089 case IDEAPAD_WMI_EVENT_FN_KEYS:
2090 if (priv->features.set_fn_lock_led) {
2091 int brightness = ideapad_fn_lock_get(priv);
2092
2093 if (brightness >= 0) {
2094 ideapad_fn_lock_set(priv, brightness);
2095 ideapad_fn_lock_led_notify(priv, brightness);
2096 }
2097 }
2098
2099 if (data->type != ACPI_TYPE_INTEGER) {
2100 dev_warn(&wdev->dev,
2101 "WMI event data is not an integer\n");
2102 break;
2103 }
2104
2105 dev_dbg(&wdev->dev, "WMI fn-key event: 0x%llx\n",
2106 data->integer.value);
2107
2108 /* performance button triggered by 0x3d */
2109 if (data->integer.value == 0x3d && priv->dytc) {
2110 platform_profile_cycle();
2111 break;
2112 }
2113
2114 /* 0x02 FnLock, 0x03 Esc */
2115 if (data->integer.value == 0x02 || data->integer.value == 0x03)
2116 ideapad_fn_lock_led_notify(priv, data->integer.value == 0x02);
2117
2118 ideapad_input_report(priv,
2119 data->integer.value | IDEAPAD_WMI_KEY);
2120
2121 break;
2122 }
2123 }
2124
2125 static const struct ideapad_wmi_private ideapad_wmi_context_esc = {
2126 .event = IDEAPAD_WMI_EVENT_ESC
2127 };
2128
2129 static const struct ideapad_wmi_private ideapad_wmi_context_fn_keys = {
2130 .event = IDEAPAD_WMI_EVENT_FN_KEYS
2131 };
2132
2133 static const struct wmi_device_id ideapad_wmi_ids[] = {
2134 { "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", &ideapad_wmi_context_esc }, /* Yoga 3 */
2135 { "56322276-8493-4CE8-A783-98C991274F5E", &ideapad_wmi_context_esc }, /* Yoga 700 */
2136 { "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", &ideapad_wmi_context_fn_keys }, /* Legion 5 */
2137 {},
2138 };
2139 MODULE_DEVICE_TABLE(wmi, ideapad_wmi_ids);
2140
2141 static struct wmi_driver ideapad_wmi_driver = {
2142 .driver = {
2143 .name = "ideapad_wmi",
2144 },
2145 .id_table = ideapad_wmi_ids,
2146 .probe = ideapad_wmi_probe,
2147 .notify = ideapad_wmi_notify,
2148 };
2149
ideapad_wmi_driver_register(void)2150 static int ideapad_wmi_driver_register(void)
2151 {
2152 return wmi_driver_register(&ideapad_wmi_driver);
2153 }
2154
ideapad_wmi_driver_unregister(void)2155 static void ideapad_wmi_driver_unregister(void)
2156 {
2157 return wmi_driver_unregister(&ideapad_wmi_driver);
2158 }
2159
2160 #else
ideapad_wmi_driver_register(void)2161 static inline int ideapad_wmi_driver_register(void) { return 0; }
ideapad_wmi_driver_unregister(void)2162 static inline void ideapad_wmi_driver_unregister(void) { }
2163 #endif
2164
2165 /*
2166 * ACPI driver
2167 */
ideapad_acpi_add(struct platform_device * pdev)2168 static int ideapad_acpi_add(struct platform_device *pdev)
2169 {
2170 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
2171 struct ideapad_private *priv;
2172 acpi_status status;
2173 unsigned long cfg;
2174 int err, i;
2175
2176 if (!adev || eval_int(adev->handle, "_CFG", &cfg))
2177 return -ENODEV;
2178
2179 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
2180 if (!priv)
2181 return -ENOMEM;
2182
2183 dev_set_drvdata(&pdev->dev, priv);
2184
2185 priv->cfg = cfg;
2186 priv->adev = adev;
2187 priv->platform_device = pdev;
2188
2189 err = devm_mutex_init(&pdev->dev, &priv->vpc_mutex);
2190 if (err)
2191 return err;
2192
2193 ideapad_check_features(priv);
2194
2195 ideapad_debugfs_init(priv);
2196
2197 err = ideapad_input_init(priv);
2198 if (err)
2199 goto input_failed;
2200
2201 err = ideapad_kbd_bl_init(priv);
2202 if (err) {
2203 if (err != -ENODEV)
2204 dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
2205 else
2206 dev_info(&pdev->dev, "Keyboard backlight control not available\n");
2207 }
2208
2209 err = ideapad_fn_lock_led_init(priv);
2210 if (err) {
2211 if (err != -ENODEV)
2212 dev_warn(&pdev->dev, "Could not set up FnLock LED: %d\n", err);
2213 else
2214 dev_info(&pdev->dev, "FnLock control not available\n");
2215 }
2216
2217 /*
2218 * On some models without a hw-switch (the yoga 2 13 at least)
2219 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
2220 */
2221 if (!priv->features.hw_rfkill_switch)
2222 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
2223
2224 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
2225 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
2226 ideapad_register_rfkill(priv, i);
2227
2228 ideapad_sync_rfk_state(priv);
2229 ideapad_sync_touchpad_state(priv, false);
2230
2231 err = ideapad_dytc_profile_init(priv);
2232 if (err) {
2233 if (err != -ENODEV)
2234 dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
2235 else
2236 dev_info(&pdev->dev, "DYTC interface is not available\n");
2237 }
2238
2239 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2240 err = ideapad_backlight_init(priv);
2241 if (err && err != -ENODEV)
2242 goto backlight_failed;
2243 }
2244
2245 status = acpi_install_notify_handler(adev->handle,
2246 ACPI_DEVICE_NOTIFY,
2247 ideapad_acpi_notify, priv);
2248 if (ACPI_FAILURE(status)) {
2249 err = -EIO;
2250 goto notification_failed;
2251 }
2252
2253 err = ideapad_shared_init(priv);
2254 if (err)
2255 goto shared_init_failed;
2256
2257 ideapad_laptop_register_notifier(&ideapad_laptop_notifier);
2258
2259 return 0;
2260
2261 shared_init_failed:
2262 acpi_remove_notify_handler(priv->adev->handle,
2263 ACPI_DEVICE_NOTIFY,
2264 ideapad_acpi_notify);
2265
2266 notification_failed:
2267 ideapad_backlight_exit(priv);
2268
2269 backlight_failed:
2270 ideapad_dytc_profile_exit(priv);
2271
2272 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
2273 ideapad_unregister_rfkill(priv, i);
2274
2275 ideapad_fn_lock_led_exit(priv);
2276 ideapad_kbd_bl_exit(priv);
2277 ideapad_input_exit(priv);
2278
2279 input_failed:
2280 ideapad_debugfs_exit(priv);
2281
2282 return err;
2283 }
2284
ideapad_acpi_remove(struct platform_device * pdev)2285 static void ideapad_acpi_remove(struct platform_device *pdev)
2286 {
2287 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
2288 int i;
2289
2290 ideapad_laptop_unregister_notifier(&ideapad_laptop_notifier);
2291
2292 ideapad_shared_exit(priv);
2293
2294 acpi_remove_notify_handler(priv->adev->handle,
2295 ACPI_DEVICE_NOTIFY,
2296 ideapad_acpi_notify);
2297
2298 ideapad_backlight_exit(priv);
2299 ideapad_dytc_profile_exit(priv);
2300
2301 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
2302 ideapad_unregister_rfkill(priv, i);
2303
2304 ideapad_fn_lock_led_exit(priv);
2305 ideapad_kbd_bl_exit(priv);
2306 ideapad_input_exit(priv);
2307 ideapad_debugfs_exit(priv);
2308 }
2309
2310 #ifdef CONFIG_PM_SLEEP
ideapad_acpi_resume(struct device * dev)2311 static int ideapad_acpi_resume(struct device *dev)
2312 {
2313 struct ideapad_private *priv = dev_get_drvdata(dev);
2314
2315 ideapad_sync_rfk_state(priv);
2316 ideapad_sync_touchpad_state(priv, false);
2317
2318 if (priv->dytc)
2319 dytc_profile_refresh(priv);
2320
2321 return 0;
2322 }
2323 #endif
2324 static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
2325
2326 static const struct acpi_device_id ideapad_device_ids[] = {
2327 {"VPC2004", 0},
2328 {"", 0},
2329 };
2330 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
2331
2332 static struct platform_driver ideapad_acpi_driver = {
2333 .probe = ideapad_acpi_add,
2334 .remove = ideapad_acpi_remove,
2335 .driver = {
2336 .name = "ideapad_acpi",
2337 .pm = &ideapad_pm,
2338 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
2339 .dev_groups = ideapad_attribute_groups,
2340 },
2341 };
2342
ideapad_laptop_init(void)2343 static int __init ideapad_laptop_init(void)
2344 {
2345 int err;
2346
2347 err = ideapad_wmi_driver_register();
2348 if (err)
2349 return err;
2350
2351 err = platform_driver_register(&ideapad_acpi_driver);
2352 if (err) {
2353 ideapad_wmi_driver_unregister();
2354 return err;
2355 }
2356
2357 return 0;
2358 }
module_init(ideapad_laptop_init)2359 module_init(ideapad_laptop_init)
2360
2361 static void __exit ideapad_laptop_exit(void)
2362 {
2363 ideapad_wmi_driver_unregister();
2364 platform_driver_unregister(&ideapad_acpi_driver);
2365 }
2366 module_exit(ideapad_laptop_exit)
2367
2368 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2369 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
2370 MODULE_LICENSE("GPL");
2371