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