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