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