platform/x86: thinkpad_acpi: Add platform profile support
[linux-2.6-block.git] / drivers / platform / x86 / ideapad-laptop.c
CommitLineData
16216333 1// SPDX-License-Identifier: GPL-2.0-or-later
58ac7aa0 2/*
a4b5a279 3 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
58ac7aa0
DW
4 *
5 * Copyright © 2010 Intel Corporation
6 * Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
58ac7aa0
DW
7 */
8
9ab23989
JP
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
58ac7aa0
DW
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/types.h>
8b48463f 15#include <linux/acpi.h>
58ac7aa0 16#include <linux/rfkill.h>
98ee6919 17#include <linux/platform_device.h>
f63409ae
IP
18#include <linux/input.h>
19#include <linux/input/sparse-keymap.h>
a4ecbb8a
IP
20#include <linux/backlight.h>
21#include <linux/fb.h>
773e3206
IP
22#include <linux/debugfs.h>
23#include <linux/seq_file.h>
07a4a4fc 24#include <linux/i8042.h>
85093f79 25#include <linux/dmi.h>
b3facd7b 26#include <linux/device.h>
26bff5f0 27#include <acpi/video.h>
58ac7aa0 28
c1f73658 29#define IDEAPAD_RFKILL_DEV_NUM (3)
58ac7aa0 30
ade50296 31#define BM_CONSERVATION_BIT (5)
40760717 32#define HA_FNLOCK_BIT (10)
ade50296 33
3371f481
IP
34#define CFG_BT_BIT (16)
35#define CFG_3G_BIT (17)
36#define CFG_WIFI_BIT (18)
a84511f7 37#define CFG_CAMERA_BIT (19)
3371f481 38
74caab99 39#if IS_ENABLED(CONFIG_ACPI_WMI)
2d98e0b9
AB
40static const char *const ideapad_wmi_fnesc_events[] = {
41 "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
42 "56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
43};
74caab99
AB
44#endif
45
ade50296
HWT
46enum {
47 BMCMD_CONSERVATION_ON = 3,
48 BMCMD_CONSERVATION_OFF = 5,
40760717
OK
49 HACMD_FNLOCK_ON = 0xe,
50 HACMD_FNLOCK_OFF = 0xf,
ade50296
HWT
51};
52
2be1dc21
IP
53enum {
54 VPCCMD_R_VPC1 = 0x10,
55 VPCCMD_R_BL_MAX,
56 VPCCMD_R_BL,
57 VPCCMD_W_BL,
58 VPCCMD_R_WIFI,
59 VPCCMD_W_WIFI,
60 VPCCMD_R_BT,
61 VPCCMD_W_BT,
62 VPCCMD_R_BL_POWER,
63 VPCCMD_R_NOVO,
64 VPCCMD_R_VPC2,
65 VPCCMD_R_TOUCHPAD,
66 VPCCMD_W_TOUCHPAD,
67 VPCCMD_R_CAMERA,
68 VPCCMD_W_CAMERA,
69 VPCCMD_R_3G,
70 VPCCMD_W_3G,
71 VPCCMD_R_ODD, /* 0x21 */
0c7bbeb9
MM
72 VPCCMD_W_FAN,
73 VPCCMD_R_RF,
2be1dc21 74 VPCCMD_W_RF,
0c7bbeb9 75 VPCCMD_R_FAN = 0x2B,
296f9fe0 76 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
2be1dc21
IP
77 VPCCMD_W_BL_POWER = 0x33,
78};
79
331e0ea2
ZR
80struct ideapad_rfk_priv {
81 int dev;
82 struct ideapad_private *priv;
83};
84
ce326329 85struct ideapad_private {
469f6434 86 struct acpi_device *adev;
c1f73658 87 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
331e0ea2 88 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
98ee6919 89 struct platform_device *platform_device;
f63409ae 90 struct input_dev *inputdev;
a4ecbb8a 91 struct backlight_device *blightdev;
773e3206 92 struct dentry *debug;
3371f481 93 unsigned long cfg;
ce363c2b 94 bool has_hw_rfkill_switch;
d69cd7ee 95 bool has_touchpad_switch;
2d98e0b9 96 const char *fnesc_guid;
58ac7aa0
DW
97};
98
bfa97b7d
IP
99static bool no_bt_rfkill;
100module_param(no_bt_rfkill, bool, 0444);
101MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
102
6a09f21d
IP
103/*
104 * ACPI Helpers
105 */
ed5b9ba7 106#define IDEAPAD_EC_TIMEOUT (200) /* in ms */
6a09f21d
IP
107
108static int read_method_int(acpi_handle handle, const char *method, int *val)
109{
110 acpi_status status;
111 unsigned long long result;
112
113 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
114 if (ACPI_FAILURE(status)) {
115 *val = -1;
116 return -1;
6a09f21d 117 }
ba3a3387
JY
118 *val = result;
119 return 0;
120
6a09f21d
IP
121}
122
ade50296
HWT
123static int method_gbmd(acpi_handle handle, unsigned long *ret)
124{
125 int result, val;
126
127 result = read_method_int(handle, "GBMD", &val);
128 *ret = val;
129 return result;
130}
131
40760717 132static int method_int1(acpi_handle handle, char *method, int cmd)
ade50296
HWT
133{
134 acpi_status status;
135
40760717 136 status = acpi_execute_simple_method(handle, method, cmd);
ade50296
HWT
137 return ACPI_FAILURE(status) ? -1 : 0;
138}
139
6a09f21d
IP
140static int method_vpcr(acpi_handle handle, int cmd, int *ret)
141{
142 acpi_status status;
143 unsigned long long result;
144 struct acpi_object_list params;
145 union acpi_object in_obj;
146
147 params.count = 1;
148 params.pointer = &in_obj;
149 in_obj.type = ACPI_TYPE_INTEGER;
150 in_obj.integer.value = cmd;
151
152 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
153
154 if (ACPI_FAILURE(status)) {
155 *ret = -1;
156 return -1;
6a09f21d 157 }
ba3a3387
JY
158 *ret = result;
159 return 0;
160
6a09f21d
IP
161}
162
163static int method_vpcw(acpi_handle handle, int cmd, int data)
164{
165 struct acpi_object_list params;
166 union acpi_object in_obj[2];
167 acpi_status status;
168
169 params.count = 2;
170 params.pointer = in_obj;
171 in_obj[0].type = ACPI_TYPE_INTEGER;
172 in_obj[0].integer.value = cmd;
173 in_obj[1].type = ACPI_TYPE_INTEGER;
174 in_obj[1].integer.value = data;
175
176 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
177 if (status != AE_OK)
178 return -1;
179 return 0;
180}
181
182static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
183{
184 int val;
185 unsigned long int end_jiffies;
186
187 if (method_vpcw(handle, 1, cmd))
188 return -1;
189
190 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
191 time_before(jiffies, end_jiffies);) {
192 schedule();
193 if (method_vpcr(handle, 1, &val))
194 return -1;
195 if (val == 0) {
196 if (method_vpcr(handle, 0, &val))
197 return -1;
198 *data = val;
199 return 0;
200 }
201 }
31e56f23 202 pr_err("timeout in %s\n", __func__);
6a09f21d
IP
203 return -1;
204}
205
206static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
207{
208 int val;
209 unsigned long int end_jiffies;
210
211 if (method_vpcw(handle, 0, data))
212 return -1;
213 if (method_vpcw(handle, 1, cmd))
214 return -1;
215
216 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
217 time_before(jiffies, end_jiffies);) {
218 schedule();
219 if (method_vpcr(handle, 1, &val))
220 return -1;
221 if (val == 0)
222 return 0;
223 }
f1395edb 224 pr_err("timeout in %s\n", __func__);
6a09f21d
IP
225 return -1;
226}
6a09f21d 227
773e3206
IP
228/*
229 * debugfs
230 */
773e3206
IP
231static int debugfs_status_show(struct seq_file *s, void *data)
232{
331e0ea2 233 struct ideapad_private *priv = s->private;
773e3206
IP
234 unsigned long value;
235
331e0ea2
ZR
236 if (!priv)
237 return -EINVAL;
238
239 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
773e3206 240 seq_printf(s, "Backlight max:\t%lu\n", value);
331e0ea2 241 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
773e3206 242 seq_printf(s, "Backlight now:\t%lu\n", value);
331e0ea2 243 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
773e3206
IP
244 seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
245 seq_printf(s, "=====================\n");
246
331e0ea2 247 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
773e3206
IP
248 seq_printf(s, "Radio status:\t%s(%lu)\n",
249 value ? "On" : "Off", value);
331e0ea2 250 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
773e3206
IP
251 seq_printf(s, "Wifi status:\t%s(%lu)\n",
252 value ? "On" : "Off", value);
331e0ea2 253 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
773e3206
IP
254 seq_printf(s, "BT status:\t%s(%lu)\n",
255 value ? "On" : "Off", value);
331e0ea2 256 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
773e3206
IP
257 seq_printf(s, "3G status:\t%s(%lu)\n",
258 value ? "On" : "Off", value);
259 seq_printf(s, "=====================\n");
260
331e0ea2 261 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
773e3206
IP
262 seq_printf(s, "Touchpad status:%s(%lu)\n",
263 value ? "On" : "Off", value);
331e0ea2 264 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
773e3206
IP
265 seq_printf(s, "Camera status:\t%s(%lu)\n",
266 value ? "On" : "Off", value);
ade50296
HWT
267 seq_puts(s, "=====================\n");
268
269 if (!method_gbmd(priv->adev->handle, &value)) {
270 seq_printf(s, "Conservation mode:\t%s(%lu)\n",
271 test_bit(BM_CONSERVATION_BIT, &value) ? "On" : "Off",
272 value);
273 }
773e3206
IP
274
275 return 0;
276}
334c4efd 277DEFINE_SHOW_ATTRIBUTE(debugfs_status);
773e3206
IP
278
279static int debugfs_cfg_show(struct seq_file *s, void *data)
280{
331e0ea2
ZR
281 struct ideapad_private *priv = s->private;
282
283 if (!priv) {
773e3206
IP
284 seq_printf(s, "cfg: N/A\n");
285 } else {
286 seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
331e0ea2
ZR
287 priv->cfg);
288 if (test_bit(CFG_BT_BIT, &priv->cfg))
773e3206 289 seq_printf(s, "Bluetooth ");
331e0ea2 290 if (test_bit(CFG_3G_BIT, &priv->cfg))
773e3206 291 seq_printf(s, "3G ");
331e0ea2 292 if (test_bit(CFG_WIFI_BIT, &priv->cfg))
773e3206 293 seq_printf(s, "Wireless ");
331e0ea2 294 if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
773e3206
IP
295 seq_printf(s, "Camera ");
296 seq_printf(s, "\nGraphic: ");
331e0ea2 297 switch ((priv->cfg)&0x700) {
773e3206
IP
298 case 0x100:
299 seq_printf(s, "Intel");
300 break;
301 case 0x200:
302 seq_printf(s, "ATI");
303 break;
304 case 0x300:
305 seq_printf(s, "Nvidia");
306 break;
307 case 0x400:
308 seq_printf(s, "Intel and ATI");
309 break;
310 case 0x500:
311 seq_printf(s, "Intel and Nvidia");
312 break;
313 }
314 seq_printf(s, "\n");
315 }
316 return 0;
317}
334c4efd 318DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
773e3206 319
17f1bf38 320static void ideapad_debugfs_init(struct ideapad_private *priv)
773e3206 321{
17f1bf38 322 struct dentry *dir;
773e3206 323
17f1bf38
GKH
324 dir = debugfs_create_dir("ideapad", NULL);
325 priv->debug = dir;
773e3206 326
17f1bf38
GKH
327 debugfs_create_file("cfg", S_IRUGO, dir, priv, &debugfs_cfg_fops);
328 debugfs_create_file("status", S_IRUGO, dir, priv, &debugfs_status_fops);
773e3206
IP
329}
330
331static void ideapad_debugfs_exit(struct ideapad_private *priv)
332{
333 debugfs_remove_recursive(priv->debug);
334 priv->debug = NULL;
335}
336
a4b5a279 337/*
3371f481 338 * sysfs
a4b5a279 339 */
58ac7aa0
DW
340static ssize_t show_ideapad_cam(struct device *dev,
341 struct device_attribute *attr,
342 char *buf)
343{
26c81d5c 344 unsigned long result;
331e0ea2 345 struct ideapad_private *priv = dev_get_drvdata(dev);
58ac7aa0 346
331e0ea2 347 if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
26c81d5c
IP
348 return sprintf(buf, "-1\n");
349 return sprintf(buf, "%lu\n", result);
58ac7aa0
DW
350}
351
352static ssize_t store_ideapad_cam(struct device *dev,
353 struct device_attribute *attr,
354 const char *buf, size_t count)
355{
356 int ret, state;
331e0ea2 357 struct ideapad_private *priv = dev_get_drvdata(dev);
58ac7aa0
DW
358
359 if (!count)
360 return 0;
361 if (sscanf(buf, "%i", &state) != 1)
362 return -EINVAL;
331e0ea2 363 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
58ac7aa0 364 if (ret < 0)
0c7bbeb9 365 return -EIO;
58ac7aa0
DW
366 return count;
367}
368
369static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
370
0c7bbeb9
MM
371static ssize_t show_ideapad_fan(struct device *dev,
372 struct device_attribute *attr,
373 char *buf)
374{
375 unsigned long result;
331e0ea2 376 struct ideapad_private *priv = dev_get_drvdata(dev);
0c7bbeb9 377
331e0ea2 378 if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
0c7bbeb9
MM
379 return sprintf(buf, "-1\n");
380 return sprintf(buf, "%lu\n", result);
381}
382
383static ssize_t store_ideapad_fan(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
386{
387 int ret, state;
331e0ea2 388 struct ideapad_private *priv = dev_get_drvdata(dev);
0c7bbeb9
MM
389
390 if (!count)
391 return 0;
392 if (sscanf(buf, "%i", &state) != 1)
393 return -EINVAL;
394 if (state < 0 || state > 4 || state == 3)
395 return -EINVAL;
331e0ea2 396 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
0c7bbeb9
MM
397 if (ret < 0)
398 return -EIO;
399 return count;
400}
401
402static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
403
36ac0d43
RRS
404static ssize_t touchpad_show(struct device *dev,
405 struct device_attribute *attr,
406 char *buf)
407{
408 struct ideapad_private *priv = dev_get_drvdata(dev);
409 unsigned long result;
410
411 if (read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result))
412 return sprintf(buf, "-1\n");
413 return sprintf(buf, "%lu\n", result);
414}
415
46936fd6
AB
416/* Switch to RO for now: It might be revisited in the future */
417static ssize_t __maybe_unused touchpad_store(struct device *dev,
418 struct device_attribute *attr,
419 const char *buf, size_t count)
36ac0d43
RRS
420{
421 struct ideapad_private *priv = dev_get_drvdata(dev);
422 bool state;
423 int ret;
424
425 ret = kstrtobool(buf, &state);
426 if (ret)
427 return ret;
428
429 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
430 if (ret < 0)
431 return -EIO;
432 return count;
433}
434
7f363145 435static DEVICE_ATTR_RO(touchpad);
36ac0d43 436
ade50296
HWT
437static ssize_t conservation_mode_show(struct device *dev,
438 struct device_attribute *attr,
439 char *buf)
440{
441 struct ideapad_private *priv = dev_get_drvdata(dev);
442 unsigned long result;
443
444 if (method_gbmd(priv->adev->handle, &result))
445 return sprintf(buf, "-1\n");
446 return sprintf(buf, "%u\n", test_bit(BM_CONSERVATION_BIT, &result));
447}
448
449static ssize_t conservation_mode_store(struct device *dev,
450 struct device_attribute *attr,
451 const char *buf, size_t count)
452{
453 struct ideapad_private *priv = dev_get_drvdata(dev);
454 bool state;
455 int ret;
456
457 ret = kstrtobool(buf, &state);
458 if (ret)
459 return ret;
460
40760717 461 ret = method_int1(priv->adev->handle, "SBMC", state ?
ade50296
HWT
462 BMCMD_CONSERVATION_ON :
463 BMCMD_CONSERVATION_OFF);
464 if (ret < 0)
465 return -EIO;
466 return count;
467}
468
469static DEVICE_ATTR_RW(conservation_mode);
470
40760717
OK
471static ssize_t fn_lock_show(struct device *dev,
472 struct device_attribute *attr,
473 char *buf)
474{
475 struct ideapad_private *priv = dev_get_drvdata(dev);
476 unsigned long result;
477 int hals;
478 int fail = read_method_int(priv->adev->handle, "HALS", &hals);
479
480 if (fail)
481 return sprintf(buf, "-1\n");
482
483 result = hals;
484 return sprintf(buf, "%u\n", test_bit(HA_FNLOCK_BIT, &result));
485}
486
487static ssize_t fn_lock_store(struct device *dev,
488 struct device_attribute *attr,
489 const char *buf, size_t count)
490{
491 struct ideapad_private *priv = dev_get_drvdata(dev);
492 bool state;
493 int ret;
494
495 ret = kstrtobool(buf, &state);
496 if (ret)
497 return ret;
498
499 ret = method_int1(priv->adev->handle, "SALS", state ?
500 HACMD_FNLOCK_ON :
501 HACMD_FNLOCK_OFF);
502 if (ret < 0)
503 return -EIO;
504 return count;
505}
506
507static DEVICE_ATTR_RW(fn_lock);
508
509
3371f481
IP
510static struct attribute *ideapad_attributes[] = {
511 &dev_attr_camera_power.attr,
0c7bbeb9 512 &dev_attr_fan_mode.attr,
36ac0d43 513 &dev_attr_touchpad.attr,
ade50296 514 &dev_attr_conservation_mode.attr,
40760717 515 &dev_attr_fn_lock.attr,
3371f481
IP
516 NULL
517};
518
587a1f16 519static umode_t ideapad_is_visible(struct kobject *kobj,
a84511f7
IP
520 struct attribute *attr,
521 int idx)
522{
523 struct device *dev = container_of(kobj, struct device, kobj);
524 struct ideapad_private *priv = dev_get_drvdata(dev);
525 bool supported;
526
527 if (attr == &dev_attr_camera_power.attr)
528 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
0c7bbeb9
MM
529 else if (attr == &dev_attr_fan_mode.attr) {
530 unsigned long value;
331e0ea2
ZR
531 supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
532 &value);
ade50296
HWT
533 } else if (attr == &dev_attr_conservation_mode.attr) {
534 supported = acpi_has_method(priv->adev->handle, "GBMD") &&
535 acpi_has_method(priv->adev->handle, "SBMC");
40760717
OK
536 } else if (attr == &dev_attr_fn_lock.attr) {
537 supported = acpi_has_method(priv->adev->handle, "HALS") &&
538 acpi_has_method(priv->adev->handle, "SALS");
d69cd7ee
JY
539 } else if (attr == &dev_attr_touchpad.attr)
540 supported = priv->has_touchpad_switch;
541 else
a84511f7
IP
542 supported = true;
543
544 return supported ? attr->mode : 0;
545}
546
49458e83 547static const struct attribute_group ideapad_attribute_group = {
a84511f7 548 .is_visible = ideapad_is_visible,
3371f481
IP
549 .attrs = ideapad_attributes
550};
551
a4b5a279
IP
552/*
553 * Rfkill
554 */
c1f73658
IP
555struct ideapad_rfk_data {
556 char *name;
557 int cfgbit;
558 int opcode;
559 int type;
560};
561
b3d94d70 562static const struct ideapad_rfk_data ideapad_rfk_data[] = {
2be1dc21
IP
563 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
564 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
565 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
c1f73658
IP
566};
567
58ac7aa0
DW
568static int ideapad_rfk_set(void *data, bool blocked)
569{
331e0ea2 570 struct ideapad_rfk_priv *priv = data;
4b200b46 571 int opcode = ideapad_rfk_data[priv->dev].opcode;
fa08359e 572
4b200b46 573 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
58ac7aa0
DW
574}
575
3d59dfcd 576static const struct rfkill_ops ideapad_rfk_ops = {
58ac7aa0
DW
577 .set_block = ideapad_rfk_set,
578};
579
923de84a 580static void ideapad_sync_rfk_state(struct ideapad_private *priv)
58ac7aa0 581{
ce363c2b 582 unsigned long hw_blocked = 0;
58ac7aa0
DW
583 int i;
584
ce363c2b
HG
585 if (priv->has_hw_rfkill_switch) {
586 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
587 return;
588 hw_blocked = !hw_blocked;
589 }
58ac7aa0 590
c1f73658 591 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
ce326329 592 if (priv->rfk[i])
2b7266bd 593 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
58ac7aa0
DW
594}
595
75a11f11 596static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
58ac7aa0
DW
597{
598 int ret;
2b7266bd 599 unsigned long sw_blocked;
58ac7aa0 600
bfa97b7d
IP
601 if (no_bt_rfkill &&
602 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
603 /* Force to enable bluetooth when no_bt_rfkill=1 */
331e0ea2 604 write_ec_cmd(priv->adev->handle,
bfa97b7d
IP
605 ideapad_rfk_data[dev].opcode, 1);
606 return 0;
607 }
331e0ea2
ZR
608 priv->rfk_priv[dev].dev = dev;
609 priv->rfk_priv[dev].priv = priv;
bfa97b7d 610
75a11f11 611 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
b5c37b79 612 &priv->platform_device->dev,
75a11f11
ZR
613 ideapad_rfk_data[dev].type,
614 &ideapad_rfk_ops,
331e0ea2 615 &priv->rfk_priv[dev]);
ce326329 616 if (!priv->rfk[dev])
58ac7aa0
DW
617 return -ENOMEM;
618
331e0ea2 619 if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
2b7266bd
IP
620 &sw_blocked)) {
621 rfkill_init_sw_state(priv->rfk[dev], 0);
622 } else {
623 sw_blocked = !sw_blocked;
624 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
625 }
626
ce326329 627 ret = rfkill_register(priv->rfk[dev]);
58ac7aa0 628 if (ret) {
ce326329 629 rfkill_destroy(priv->rfk[dev]);
58ac7aa0
DW
630 return ret;
631 }
632 return 0;
633}
634
75a11f11 635static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
58ac7aa0 636{
ce326329 637 if (!priv->rfk[dev])
58ac7aa0
DW
638 return;
639
ce326329
DW
640 rfkill_unregister(priv->rfk[dev]);
641 rfkill_destroy(priv->rfk[dev]);
58ac7aa0
DW
642}
643
98ee6919
IP
644/*
645 * Platform device
646 */
b5c37b79 647static int ideapad_sysfs_init(struct ideapad_private *priv)
98ee6919 648{
b5c37b79 649 return sysfs_create_group(&priv->platform_device->dev.kobj,
c9f718d0 650 &ideapad_attribute_group);
98ee6919
IP
651}
652
b5c37b79 653static void ideapad_sysfs_exit(struct ideapad_private *priv)
98ee6919 654{
8693ae84 655 sysfs_remove_group(&priv->platform_device->dev.kobj,
c9f718d0 656 &ideapad_attribute_group);
98ee6919 657}
98ee6919 658
f63409ae
IP
659/*
660 * input device
661 */
662static const struct key_entry ideapad_keymap[] = {
f43d9ec0 663 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
296f9fe0 664 { KE_KEY, 7, { KEY_CAMERA } },
48f67d62 665 { KE_KEY, 8, { KEY_MICMUTE } },
296f9fe0 666 { KE_KEY, 11, { KEY_F16 } },
f43d9ec0
IP
667 { KE_KEY, 13, { KEY_WLAN } },
668 { KE_KEY, 16, { KEY_PROG1 } },
669 { KE_KEY, 17, { KEY_PROG2 } },
296f9fe0
MM
670 { KE_KEY, 64, { KEY_PROG3 } },
671 { KE_KEY, 65, { KEY_PROG4 } },
07a4a4fc
MM
672 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
673 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
74caab99
AB
674 { KE_KEY, 128, { KEY_ESC } },
675
f63409ae
IP
676 { KE_END, 0 },
677};
678
b859f159 679static int ideapad_input_init(struct ideapad_private *priv)
f63409ae
IP
680{
681 struct input_dev *inputdev;
682 int error;
683
684 inputdev = input_allocate_device();
b222cca6 685 if (!inputdev)
f63409ae 686 return -ENOMEM;
f63409ae
IP
687
688 inputdev->name = "Ideapad extra buttons";
689 inputdev->phys = "ideapad/input0";
690 inputdev->id.bustype = BUS_HOST;
8693ae84 691 inputdev->dev.parent = &priv->platform_device->dev;
f63409ae
IP
692
693 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
694 if (error) {
695 pr_err("Unable to setup input device keymap\n");
696 goto err_free_dev;
697 }
698
699 error = input_register_device(inputdev);
700 if (error) {
701 pr_err("Unable to register input device\n");
c973d4b5 702 goto err_free_dev;
f63409ae
IP
703 }
704
8693ae84 705 priv->inputdev = inputdev;
f63409ae
IP
706 return 0;
707
f63409ae
IP
708err_free_dev:
709 input_free_device(inputdev);
710 return error;
711}
712
7451a55a 713static void ideapad_input_exit(struct ideapad_private *priv)
f63409ae 714{
8693ae84
IP
715 input_unregister_device(priv->inputdev);
716 priv->inputdev = NULL;
f63409ae
IP
717}
718
8693ae84
IP
719static void ideapad_input_report(struct ideapad_private *priv,
720 unsigned long scancode)
f63409ae 721{
8693ae84 722 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
f63409ae 723}
f63409ae 724
f43d9ec0
IP
725static void ideapad_input_novokey(struct ideapad_private *priv)
726{
727 unsigned long long_pressed;
728
331e0ea2 729 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
f43d9ec0
IP
730 return;
731 if (long_pressed)
732 ideapad_input_report(priv, 17);
733 else
734 ideapad_input_report(priv, 16);
735}
736
296f9fe0
MM
737static void ideapad_check_special_buttons(struct ideapad_private *priv)
738{
739 unsigned long bit, value;
740
331e0ea2 741 read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
296f9fe0
MM
742
743 for (bit = 0; bit < 16; bit++) {
744 if (test_bit(bit, &value)) {
745 switch (bit) {
a1ec56ed
MM
746 case 0: /* Z580 */
747 case 6: /* Z570 */
296f9fe0
MM
748 /* Thermal Management button */
749 ideapad_input_report(priv, 65);
750 break;
751 case 1:
752 /* OneKey Theater button */
753 ideapad_input_report(priv, 64);
754 break;
a1ec56ed
MM
755 default:
756 pr_info("Unknown special button: %lu\n", bit);
757 break;
296f9fe0
MM
758 }
759 }
760 }
761}
762
a4ecbb8a
IP
763/*
764 * backlight
765 */
766static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
767{
331e0ea2 768 struct ideapad_private *priv = bl_get_data(blightdev);
a4ecbb8a
IP
769 unsigned long now;
770
331e0ea2
ZR
771 if (!priv)
772 return -EINVAL;
773
774 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
a4ecbb8a
IP
775 return -EIO;
776 return now;
777}
778
779static int ideapad_backlight_update_status(struct backlight_device *blightdev)
780{
331e0ea2
ZR
781 struct ideapad_private *priv = bl_get_data(blightdev);
782
783 if (!priv)
784 return -EINVAL;
785
786 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
2be1dc21 787 blightdev->props.brightness))
a4ecbb8a 788 return -EIO;
331e0ea2 789 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
a4ecbb8a
IP
790 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
791 return -EIO;
792
793 return 0;
794}
795
796static const struct backlight_ops ideapad_backlight_ops = {
797 .get_brightness = ideapad_backlight_get_brightness,
798 .update_status = ideapad_backlight_update_status,
799};
800
801static int ideapad_backlight_init(struct ideapad_private *priv)
802{
803 struct backlight_device *blightdev;
804 struct backlight_properties props;
805 unsigned long max, now, power;
806
331e0ea2 807 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
a4ecbb8a 808 return -EIO;
331e0ea2 809 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
a4ecbb8a 810 return -EIO;
331e0ea2 811 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
a4ecbb8a
IP
812 return -EIO;
813
814 memset(&props, 0, sizeof(struct backlight_properties));
815 props.max_brightness = max;
816 props.type = BACKLIGHT_PLATFORM;
817 blightdev = backlight_device_register("ideapad",
818 &priv->platform_device->dev,
819 priv,
820 &ideapad_backlight_ops,
821 &props);
822 if (IS_ERR(blightdev)) {
823 pr_err("Could not register backlight device\n");
824 return PTR_ERR(blightdev);
825 }
826
827 priv->blightdev = blightdev;
828 blightdev->props.brightness = now;
829 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
830 backlight_update_status(blightdev);
831
832 return 0;
833}
834
835static void ideapad_backlight_exit(struct ideapad_private *priv)
836{
00981810 837 backlight_device_unregister(priv->blightdev);
a4ecbb8a
IP
838 priv->blightdev = NULL;
839}
840
841static void ideapad_backlight_notify_power(struct ideapad_private *priv)
842{
843 unsigned long power;
844 struct backlight_device *blightdev = priv->blightdev;
845
d4afc775
RB
846 if (!blightdev)
847 return;
331e0ea2 848 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
a4ecbb8a
IP
849 return;
850 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
851}
852
853static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
854{
855 unsigned long now;
856
857 /* if we control brightness via acpi video driver */
858 if (priv->blightdev == NULL) {
331e0ea2 859 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
a4ecbb8a
IP
860 return;
861 }
862
863 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
864}
865
a4b5a279
IP
866/*
867 * module init/exit
868 */
75a11f11 869static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
07a4a4fc 870{
07a4a4fc
MM
871 unsigned long value;
872
d69cd7ee
JY
873 if (!priv->has_touchpad_switch)
874 return;
875
07a4a4fc 876 /* Without reading from EC touchpad LED doesn't switch state */
75a11f11 877 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
07a4a4fc
MM
878 /* Some IdeaPads don't really turn off touchpad - they only
879 * switch the LED state. We (de)activate KBC AUX port to turn
880 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
881 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
882 unsigned char param;
883 i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
884 I8042_CMD_AUX_DISABLE);
885 ideapad_input_report(priv, value ? 67 : 66);
886 }
887}
888
b5c37b79
ZR
889static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
890{
891 struct ideapad_private *priv = data;
892 unsigned long vpc1, vpc2, vpc_bit;
893
894 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
895 return;
896 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
897 return;
898
899 vpc1 = (vpc2 << 8) | vpc1;
900 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
901 if (test_bit(vpc_bit, &vpc1)) {
902 switch (vpc_bit) {
903 case 9:
904 ideapad_sync_rfk_state(priv);
905 break;
906 case 13:
907 case 11:
48f67d62 908 case 8:
b5c37b79
ZR
909 case 7:
910 case 6:
911 ideapad_input_report(priv, vpc_bit);
912 break;
913 case 5:
914 ideapad_sync_touchpad_state(priv);
915 break;
916 case 4:
917 ideapad_backlight_notify_brightness(priv);
918 break;
919 case 3:
920 ideapad_input_novokey(priv);
921 break;
922 case 2:
923 ideapad_backlight_notify_power(priv);
924 break;
925 case 0:
926 ideapad_check_special_buttons(priv);
927 break;
3cfd956b
HWT
928 case 1:
929 /* Some IdeaPads report event 1 every ~20
930 * seconds while on battery power; some
931 * report this when changing to/from tablet
932 * mode. Squelch this event.
933 */
934 break;
b5c37b79
ZR
935 default:
936 pr_info("Unknown event: %lu\n", vpc_bit);
937 }
938 }
939 }
940}
941
74caab99
AB
942#if IS_ENABLED(CONFIG_ACPI_WMI)
943static void ideapad_wmi_notify(u32 value, void *context)
944{
945 switch (value) {
946 case 128:
947 ideapad_input_report(context, value);
948 break;
949 default:
950 pr_info("Unknown WMI event %u\n", value);
951 }
952}
953#endif
954
ce363c2b 955/*
5105e78e
HG
956 * Some ideapads have a hardware rfkill switch, but most do not have one.
957 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
958 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
959 * There used to be a long list of DMI ids for models without a hw rfkill
960 * switch here, but that resulted in playing whack a mole.
961 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
962 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
963 * hw-blocked results in an empty SSID list, which is a much more benign
964 * failure mode.
965 * So the default now is the much safer option of assuming there is no
966 * hardware rfkill switch. This default also actually matches most hardware,
967 * since having a hw rfkill switch is quite rare on modern hardware, so this
968 * also leads to a much shorter list.
ce363c2b 969 */
5105e78e 970static const struct dmi_system_id hw_rfkill_list[] = {
85093f79
HG
971 {}
972};
973
b5c37b79 974static int ideapad_acpi_add(struct platform_device *pdev)
58ac7aa0 975{
3371f481 976 int ret, i;
57f9616b 977 int cfg;
ce326329 978 struct ideapad_private *priv;
b5c37b79
ZR
979 struct acpi_device *adev;
980
981 ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
982 if (ret)
983 return -ENODEV;
58ac7aa0 984
469f6434 985 if (read_method_int(adev->handle, "_CFG", &cfg))
6f8371c0
IP
986 return -ENODEV;
987
b3facd7b 988 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
ce326329
DW
989 if (!priv)
990 return -ENOMEM;
b5c37b79
ZR
991
992 dev_set_drvdata(&pdev->dev, priv);
3371f481 993 priv->cfg = cfg;
469f6434 994 priv->adev = adev;
b5c37b79 995 priv->platform_device = pdev;
5105e78e 996 priv->has_hw_rfkill_switch = dmi_check_system(hw_rfkill_list);
98ee6919 997
d69cd7ee
JY
998 /* Most ideapads with ELAN0634 touchpad don't use EC touchpad switch */
999 priv->has_touchpad_switch = !acpi_dev_present("ELAN0634", NULL, -1);
1000
b5c37b79 1001 ret = ideapad_sysfs_init(priv);
98ee6919 1002 if (ret)
b3facd7b 1003 return ret;
ce326329 1004
17f1bf38 1005 ideapad_debugfs_init(priv);
773e3206 1006
8693ae84 1007 ret = ideapad_input_init(priv);
f63409ae
IP
1008 if (ret)
1009 goto input_failed;
1010
ce363c2b
HG
1011 /*
1012 * On some models without a hw-switch (the yoga 2 13 at least)
1013 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1014 */
1015 if (!priv->has_hw_rfkill_switch)
1016 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1017
d69cd7ee
JY
1018 /* The same for Touchpad */
1019 if (!priv->has_touchpad_switch)
1020 write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, 1);
1021
ce363c2b
HG
1022 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1023 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1024 ideapad_register_rfkill(priv, i);
1025
923de84a 1026 ideapad_sync_rfk_state(priv);
75a11f11 1027 ideapad_sync_touchpad_state(priv);
c9f718d0 1028
26bff5f0 1029 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
a4ecbb8a
IP
1030 ret = ideapad_backlight_init(priv);
1031 if (ret && ret != -ENODEV)
1032 goto backlight_failed;
1033 }
b5c37b79
ZR
1034 ret = acpi_install_notify_handler(adev->handle,
1035 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
1036 if (ret)
1037 goto notification_failed;
2d98e0b9 1038
74caab99 1039#if IS_ENABLED(CONFIG_ACPI_WMI)
2d98e0b9
AB
1040 for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
1041 ret = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
1042 ideapad_wmi_notify, priv);
1043 if (ret == AE_OK) {
1044 priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
1045 break;
1046 }
1047 }
74caab99
AB
1048 if (ret != AE_OK && ret != AE_NOT_EXIST)
1049 goto notification_failed_wmi;
1050#endif
a4ecbb8a 1051
58ac7aa0 1052 return 0;
74caab99
AB
1053#if IS_ENABLED(CONFIG_ACPI_WMI)
1054notification_failed_wmi:
1055 acpi_remove_notify_handler(priv->adev->handle,
1056 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1057#endif
b5c37b79
ZR
1058notification_failed:
1059 ideapad_backlight_exit(priv);
a4ecbb8a
IP
1060backlight_failed:
1061 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
75a11f11 1062 ideapad_unregister_rfkill(priv, i);
7451a55a 1063 ideapad_input_exit(priv);
f63409ae 1064input_failed:
773e3206 1065 ideapad_debugfs_exit(priv);
b5c37b79 1066 ideapad_sysfs_exit(priv);
98ee6919 1067 return ret;
58ac7aa0
DW
1068}
1069
b5c37b79 1070static int ideapad_acpi_remove(struct platform_device *pdev)
58ac7aa0 1071{
b5c37b79 1072 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
58ac7aa0 1073 int i;
ce326329 1074
74caab99 1075#if IS_ENABLED(CONFIG_ACPI_WMI)
2d98e0b9
AB
1076 if (priv->fnesc_guid)
1077 wmi_remove_notify_handler(priv->fnesc_guid);
74caab99 1078#endif
b5c37b79
ZR
1079 acpi_remove_notify_handler(priv->adev->handle,
1080 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
a4ecbb8a 1081 ideapad_backlight_exit(priv);
c1f73658 1082 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
75a11f11 1083 ideapad_unregister_rfkill(priv, i);
8693ae84 1084 ideapad_input_exit(priv);
773e3206 1085 ideapad_debugfs_exit(priv);
b5c37b79
ZR
1086 ideapad_sysfs_exit(priv);
1087 dev_set_drvdata(&pdev->dev, NULL);
c9f718d0 1088
58ac7aa0
DW
1089 return 0;
1090}
1091
11fa8da5 1092#ifdef CONFIG_PM_SLEEP
07a4a4fc
MM
1093static int ideapad_acpi_resume(struct device *device)
1094{
75a11f11
ZR
1095 struct ideapad_private *priv;
1096
1097 if (!device)
1098 return -EINVAL;
1099 priv = dev_get_drvdata(device);
1100
1101 ideapad_sync_rfk_state(priv);
1102 ideapad_sync_touchpad_state(priv);
07a4a4fc
MM
1103 return 0;
1104}
11fa8da5 1105#endif
b5c37b79 1106static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
07a4a4fc 1107
b5c37b79
ZR
1108static const struct acpi_device_id ideapad_device_ids[] = {
1109 { "VPC2004", 0},
1110 { "", 0},
58ac7aa0 1111};
b5c37b79
ZR
1112MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1113
1114static struct platform_driver ideapad_acpi_driver = {
1115 .probe = ideapad_acpi_add,
1116 .remove = ideapad_acpi_remove,
1117 .driver = {
1118 .name = "ideapad_acpi",
b5c37b79
ZR
1119 .pm = &ideapad_pm,
1120 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1121 },
1122};
1123
1124module_platform_driver(ideapad_acpi_driver);
58ac7aa0
DW
1125
1126MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1127MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1128MODULE_LICENSE("GPL");