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