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