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