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