157ac3b05SIke Panhc /*
2a4b5a279SIke Panhc  *  ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
357ac3b05SIke Panhc  *
457ac3b05SIke Panhc  *  Copyright © 2010 Intel Corporation
557ac3b05SIke Panhc  *  Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
657ac3b05SIke Panhc  *
757ac3b05SIke Panhc  *  This program is free software; you can redistribute it and/or modify
857ac3b05SIke Panhc  *  it under the terms of the GNU General Public License as published by
957ac3b05SIke Panhc  *  the Free Software Foundation; either version 2 of the License, or
1057ac3b05SIke Panhc  *  (at your option) any later version.
1157ac3b05SIke Panhc  *
1257ac3b05SIke Panhc  *  This program is distributed in the hope that it will be useful,
1357ac3b05SIke Panhc  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1457ac3b05SIke Panhc  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1557ac3b05SIke Panhc  *  GNU General Public License for more details.
1657ac3b05SIke Panhc  *
1757ac3b05SIke Panhc  *  You should have received a copy of the GNU General Public License
1857ac3b05SIke Panhc  *  along with this program; if not, write to the Free Software
1957ac3b05SIke Panhc  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
2057ac3b05SIke Panhc  *  02110-1301, USA.
2157ac3b05SIke Panhc  */
2257ac3b05SIke Panhc 
239ab23989SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
249ab23989SJoe Perches 
2557ac3b05SIke Panhc #include <linux/kernel.h>
2657ac3b05SIke Panhc #include <linux/module.h>
2757ac3b05SIke Panhc #include <linux/init.h>
2857ac3b05SIke Panhc #include <linux/types.h>
298b48463fSLv Zheng #include <linux/acpi.h>
3057ac3b05SIke Panhc #include <linux/rfkill.h>
3198ee6919SIke Panhc #include <linux/platform_device.h>
32f63409aeSIke Panhc #include <linux/input.h>
33f63409aeSIke Panhc #include <linux/input/sparse-keymap.h>
34a4ecbb8aSIke Panhc #include <linux/backlight.h>
35a4ecbb8aSIke Panhc #include <linux/fb.h>
36773e3206SIke Panhc #include <linux/debugfs.h>
37773e3206SIke Panhc #include <linux/seq_file.h>
3807a4a4fcSMaxim Mikityanskiy #include <linux/i8042.h>
3957ac3b05SIke Panhc 
40c1f73658SIke Panhc #define IDEAPAD_RFKILL_DEV_NUM	(3)
4157ac3b05SIke Panhc 
423371f481SIke Panhc #define CFG_BT_BIT	(16)
433371f481SIke Panhc #define CFG_3G_BIT	(17)
443371f481SIke Panhc #define CFG_WIFI_BIT	(18)
45a84511f7SIke Panhc #define CFG_CAMERA_BIT	(19)
463371f481SIke Panhc 
472be1dc21SIke Panhc enum {
482be1dc21SIke Panhc 	VPCCMD_R_VPC1 = 0x10,
492be1dc21SIke Panhc 	VPCCMD_R_BL_MAX,
502be1dc21SIke Panhc 	VPCCMD_R_BL,
512be1dc21SIke Panhc 	VPCCMD_W_BL,
522be1dc21SIke Panhc 	VPCCMD_R_WIFI,
532be1dc21SIke Panhc 	VPCCMD_W_WIFI,
542be1dc21SIke Panhc 	VPCCMD_R_BT,
552be1dc21SIke Panhc 	VPCCMD_W_BT,
562be1dc21SIke Panhc 	VPCCMD_R_BL_POWER,
572be1dc21SIke Panhc 	VPCCMD_R_NOVO,
582be1dc21SIke Panhc 	VPCCMD_R_VPC2,
592be1dc21SIke Panhc 	VPCCMD_R_TOUCHPAD,
602be1dc21SIke Panhc 	VPCCMD_W_TOUCHPAD,
612be1dc21SIke Panhc 	VPCCMD_R_CAMERA,
622be1dc21SIke Panhc 	VPCCMD_W_CAMERA,
632be1dc21SIke Panhc 	VPCCMD_R_3G,
642be1dc21SIke Panhc 	VPCCMD_W_3G,
652be1dc21SIke Panhc 	VPCCMD_R_ODD, /* 0x21 */
660c7bbeb9SMaxim Mikityanskiy 	VPCCMD_W_FAN,
670c7bbeb9SMaxim Mikityanskiy 	VPCCMD_R_RF,
682be1dc21SIke Panhc 	VPCCMD_W_RF,
690c7bbeb9SMaxim Mikityanskiy 	VPCCMD_R_FAN = 0x2B,
70296f9fe0SMaxim Mikityanskiy 	VPCCMD_R_SPECIAL_BUTTONS = 0x31,
712be1dc21SIke Panhc 	VPCCMD_W_BL_POWER = 0x33,
722be1dc21SIke Panhc };
732be1dc21SIke Panhc 
74331e0ea2SZhang Rui struct ideapad_rfk_priv {
75331e0ea2SZhang Rui 	int dev;
76331e0ea2SZhang Rui 	struct ideapad_private *priv;
77331e0ea2SZhang Rui };
78331e0ea2SZhang Rui 
7957ac3b05SIke Panhc struct ideapad_private {
80469f6434SZhang Rui 	struct acpi_device *adev;
81c1f73658SIke Panhc 	struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
82331e0ea2SZhang Rui 	struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
8398ee6919SIke Panhc 	struct platform_device *platform_device;
84f63409aeSIke Panhc 	struct input_dev *inputdev;
85a4ecbb8aSIke Panhc 	struct backlight_device *blightdev;
86773e3206SIke Panhc 	struct dentry *debug;
873371f481SIke Panhc 	unsigned long cfg;
8857ac3b05SIke Panhc };
8957ac3b05SIke Panhc 
90bfa97b7dSIke Panhc static bool no_bt_rfkill;
91bfa97b7dSIke Panhc module_param(no_bt_rfkill, bool, 0444);
92bfa97b7dSIke Panhc MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
93bfa97b7dSIke Panhc 
9457ac3b05SIke Panhc /*
9557ac3b05SIke Panhc  * ACPI Helpers
9657ac3b05SIke Panhc  */
9757ac3b05SIke Panhc #define IDEAPAD_EC_TIMEOUT (100) /* in ms */
9857ac3b05SIke Panhc 
9957ac3b05SIke Panhc static int read_method_int(acpi_handle handle, const char *method, int *val)
10057ac3b05SIke Panhc {
10157ac3b05SIke Panhc 	acpi_status status;
10257ac3b05SIke Panhc 	unsigned long long result;
10357ac3b05SIke Panhc 
10457ac3b05SIke Panhc 	status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
10557ac3b05SIke Panhc 	if (ACPI_FAILURE(status)) {
10657ac3b05SIke Panhc 		*val = -1;
10757ac3b05SIke Panhc 		return -1;
10857ac3b05SIke Panhc 	} else {
10957ac3b05SIke Panhc 		*val = result;
11057ac3b05SIke Panhc 		return 0;
11157ac3b05SIke Panhc 	}
11257ac3b05SIke Panhc }
11357ac3b05SIke Panhc 
11457ac3b05SIke Panhc static int method_vpcr(acpi_handle handle, int cmd, int *ret)
11557ac3b05SIke Panhc {
11657ac3b05SIke Panhc 	acpi_status status;
11757ac3b05SIke Panhc 	unsigned long long result;
11857ac3b05SIke Panhc 	struct acpi_object_list params;
11957ac3b05SIke Panhc 	union acpi_object in_obj;
12057ac3b05SIke Panhc 
12157ac3b05SIke Panhc 	params.count = 1;
12257ac3b05SIke Panhc 	params.pointer = &in_obj;
12357ac3b05SIke Panhc 	in_obj.type = ACPI_TYPE_INTEGER;
12457ac3b05SIke Panhc 	in_obj.integer.value = cmd;
12557ac3b05SIke Panhc 
12657ac3b05SIke Panhc 	status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
12757ac3b05SIke Panhc 
12857ac3b05SIke Panhc 	if (ACPI_FAILURE(status)) {
12957ac3b05SIke Panhc 		*ret = -1;
13057ac3b05SIke Panhc 		return -1;
13157ac3b05SIke Panhc 	} else {
13257ac3b05SIke Panhc 		*ret = result;
13357ac3b05SIke Panhc 		return 0;
13457ac3b05SIke Panhc 	}
13557ac3b05SIke Panhc }
13657ac3b05SIke Panhc 
13757ac3b05SIke Panhc static int method_vpcw(acpi_handle handle, int cmd, int data)
13857ac3b05SIke Panhc {
13957ac3b05SIke Panhc 	struct acpi_object_list params;
14057ac3b05SIke Panhc 	union acpi_object in_obj[2];
14157ac3b05SIke Panhc 	acpi_status status;
14257ac3b05SIke Panhc 
14357ac3b05SIke Panhc 	params.count = 2;
14457ac3b05SIke Panhc 	params.pointer = in_obj;
14557ac3b05SIke Panhc 	in_obj[0].type = ACPI_TYPE_INTEGER;
14657ac3b05SIke Panhc 	in_obj[0].integer.value = cmd;
14757ac3b05SIke Panhc 	in_obj[1].type = ACPI_TYPE_INTEGER;
14857ac3b05SIke Panhc 	in_obj[1].integer.value = data;
14957ac3b05SIke Panhc 
15057ac3b05SIke Panhc 	status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
15157ac3b05SIke Panhc 	if (status != AE_OK)
15257ac3b05SIke Panhc 		return -1;
15357ac3b05SIke Panhc 	return 0;
15457ac3b05SIke Panhc }
15557ac3b05SIke Panhc 
15657ac3b05SIke Panhc static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
15757ac3b05SIke Panhc {
15857ac3b05SIke Panhc 	int val;
15957ac3b05SIke Panhc 	unsigned long int end_jiffies;
16057ac3b05SIke Panhc 
16157ac3b05SIke Panhc 	if (method_vpcw(handle, 1, cmd))
16257ac3b05SIke Panhc 		return -1;
16357ac3b05SIke Panhc 
16457ac3b05SIke Panhc 	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
16557ac3b05SIke Panhc 	     time_before(jiffies, end_jiffies);) {
16657ac3b05SIke Panhc 		schedule();
16757ac3b05SIke Panhc 		if (method_vpcr(handle, 1, &val))
16857ac3b05SIke Panhc 			return -1;
16957ac3b05SIke Panhc 		if (val == 0) {
17057ac3b05SIke Panhc 			if (method_vpcr(handle, 0, &val))
17157ac3b05SIke Panhc 				return -1;
17257ac3b05SIke Panhc 			*data = val;
17357ac3b05SIke Panhc 			return 0;
17457ac3b05SIke Panhc 		}
17557ac3b05SIke Panhc 	}
17657ac3b05SIke Panhc 	pr_err("timeout in read_ec_cmd\n");
17757ac3b05SIke Panhc 	return -1;
17857ac3b05SIke Panhc }
17957ac3b05SIke Panhc 
18057ac3b05SIke Panhc static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
18157ac3b05SIke Panhc {
18257ac3b05SIke Panhc 	int val;
18357ac3b05SIke Panhc 	unsigned long int end_jiffies;
18457ac3b05SIke Panhc 
18557ac3b05SIke Panhc 	if (method_vpcw(handle, 0, data))
18657ac3b05SIke Panhc 		return -1;
18757ac3b05SIke Panhc 	if (method_vpcw(handle, 1, cmd))
18857ac3b05SIke Panhc 		return -1;
18957ac3b05SIke Panhc 
19057ac3b05SIke Panhc 	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
19157ac3b05SIke Panhc 	     time_before(jiffies, end_jiffies);) {
19257ac3b05SIke Panhc 		schedule();
19357ac3b05SIke Panhc 		if (method_vpcr(handle, 1, &val))
19457ac3b05SIke Panhc 			return -1;
19557ac3b05SIke Panhc 		if (val == 0)
19657ac3b05SIke Panhc 			return 0;
19757ac3b05SIke Panhc 	}
19857ac3b05SIke Panhc 	pr_err("timeout in write_ec_cmd\n");
19957ac3b05SIke Panhc 	return -1;
20057ac3b05SIke Panhc }
20157ac3b05SIke Panhc 
202a4b5a279SIke Panhc /*
203773e3206SIke Panhc  * debugfs
204773e3206SIke Panhc  */
205773e3206SIke Panhc static int debugfs_status_show(struct seq_file *s, void *data)
206773e3206SIke Panhc {
207331e0ea2SZhang Rui 	struct ideapad_private *priv = s->private;
208773e3206SIke Panhc 	unsigned long value;
209773e3206SIke Panhc 
210331e0ea2SZhang Rui 	if (!priv)
211331e0ea2SZhang Rui 		return -EINVAL;
212331e0ea2SZhang Rui 
213331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
214773e3206SIke Panhc 		seq_printf(s, "Backlight max:\t%lu\n", value);
215331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
216773e3206SIke Panhc 		seq_printf(s, "Backlight now:\t%lu\n", value);
217331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
218773e3206SIke Panhc 		seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
219773e3206SIke Panhc 	seq_printf(s, "=====================\n");
220773e3206SIke Panhc 
221331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
222773e3206SIke Panhc 		seq_printf(s, "Radio status:\t%s(%lu)\n",
223773e3206SIke Panhc 			   value ? "On" : "Off", value);
224331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
225773e3206SIke Panhc 		seq_printf(s, "Wifi status:\t%s(%lu)\n",
226773e3206SIke Panhc 			   value ? "On" : "Off", value);
227331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
228773e3206SIke Panhc 		seq_printf(s, "BT status:\t%s(%lu)\n",
229773e3206SIke Panhc 			   value ? "On" : "Off", value);
230331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
231773e3206SIke Panhc 		seq_printf(s, "3G status:\t%s(%lu)\n",
232773e3206SIke Panhc 			   value ? "On" : "Off", value);
233773e3206SIke Panhc 	seq_printf(s, "=====================\n");
234773e3206SIke Panhc 
235331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
236773e3206SIke Panhc 		seq_printf(s, "Touchpad status:%s(%lu)\n",
237773e3206SIke Panhc 			   value ? "On" : "Off", value);
238331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
239773e3206SIke Panhc 		seq_printf(s, "Camera status:\t%s(%lu)\n",
240773e3206SIke Panhc 			   value ? "On" : "Off", value);
241773e3206SIke Panhc 
242773e3206SIke Panhc 	return 0;
243773e3206SIke Panhc }
244773e3206SIke Panhc 
245773e3206SIke Panhc static int debugfs_status_open(struct inode *inode, struct file *file)
246773e3206SIke Panhc {
247331e0ea2SZhang Rui 	return single_open(file, debugfs_status_show, inode->i_private);
248773e3206SIke Panhc }
249773e3206SIke Panhc 
250773e3206SIke Panhc static const struct file_operations debugfs_status_fops = {
251773e3206SIke Panhc 	.owner = THIS_MODULE,
252773e3206SIke Panhc 	.open = debugfs_status_open,
253773e3206SIke Panhc 	.read = seq_read,
254773e3206SIke Panhc 	.llseek = seq_lseek,
255773e3206SIke Panhc 	.release = single_release,
256773e3206SIke Panhc };
257773e3206SIke Panhc 
258773e3206SIke Panhc static int debugfs_cfg_show(struct seq_file *s, void *data)
259773e3206SIke Panhc {
260331e0ea2SZhang Rui 	struct ideapad_private *priv = s->private;
261331e0ea2SZhang Rui 
262331e0ea2SZhang Rui 	if (!priv) {
263773e3206SIke Panhc 		seq_printf(s, "cfg: N/A\n");
264773e3206SIke Panhc 	} else {
265773e3206SIke Panhc 		seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
266331e0ea2SZhang Rui 			   priv->cfg);
267331e0ea2SZhang Rui 		if (test_bit(CFG_BT_BIT, &priv->cfg))
268773e3206SIke Panhc 			seq_printf(s, "Bluetooth ");
269331e0ea2SZhang Rui 		if (test_bit(CFG_3G_BIT, &priv->cfg))
270773e3206SIke Panhc 			seq_printf(s, "3G ");
271331e0ea2SZhang Rui 		if (test_bit(CFG_WIFI_BIT, &priv->cfg))
272773e3206SIke Panhc 			seq_printf(s, "Wireless ");
273331e0ea2SZhang Rui 		if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
274773e3206SIke Panhc 			seq_printf(s, "Camera ");
275773e3206SIke Panhc 		seq_printf(s, "\nGraphic: ");
276331e0ea2SZhang Rui 		switch ((priv->cfg)&0x700) {
277773e3206SIke Panhc 		case 0x100:
278773e3206SIke Panhc 			seq_printf(s, "Intel");
279773e3206SIke Panhc 			break;
280773e3206SIke Panhc 		case 0x200:
281773e3206SIke Panhc 			seq_printf(s, "ATI");
282773e3206SIke Panhc 			break;
283773e3206SIke Panhc 		case 0x300:
284773e3206SIke Panhc 			seq_printf(s, "Nvidia");
285773e3206SIke Panhc 			break;
286773e3206SIke Panhc 		case 0x400:
287773e3206SIke Panhc 			seq_printf(s, "Intel and ATI");
288773e3206SIke Panhc 			break;
289773e3206SIke Panhc 		case 0x500:
290773e3206SIke Panhc 			seq_printf(s, "Intel and Nvidia");
291773e3206SIke Panhc 			break;
292773e3206SIke Panhc 		}
293773e3206SIke Panhc 		seq_printf(s, "\n");
294773e3206SIke Panhc 	}
295773e3206SIke Panhc 	return 0;
296773e3206SIke Panhc }
297773e3206SIke Panhc 
298773e3206SIke Panhc static int debugfs_cfg_open(struct inode *inode, struct file *file)
299773e3206SIke Panhc {
300331e0ea2SZhang Rui 	return single_open(file, debugfs_cfg_show, inode->i_private);
301773e3206SIke Panhc }
302773e3206SIke Panhc 
303773e3206SIke Panhc static const struct file_operations debugfs_cfg_fops = {
304773e3206SIke Panhc 	.owner = THIS_MODULE,
305773e3206SIke Panhc 	.open = debugfs_cfg_open,
306773e3206SIke Panhc 	.read = seq_read,
307773e3206SIke Panhc 	.llseek = seq_lseek,
308773e3206SIke Panhc 	.release = single_release,
309773e3206SIke Panhc };
310773e3206SIke Panhc 
311b859f159SGreg Kroah-Hartman static int ideapad_debugfs_init(struct ideapad_private *priv)
312773e3206SIke Panhc {
313773e3206SIke Panhc 	struct dentry *node;
314773e3206SIke Panhc 
315773e3206SIke Panhc 	priv->debug = debugfs_create_dir("ideapad", NULL);
316773e3206SIke Panhc 	if (priv->debug == NULL) {
317773e3206SIke Panhc 		pr_err("failed to create debugfs directory");
318773e3206SIke Panhc 		goto errout;
319773e3206SIke Panhc 	}
320773e3206SIke Panhc 
321331e0ea2SZhang Rui 	node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
322773e3206SIke Panhc 				   &debugfs_cfg_fops);
323773e3206SIke Panhc 	if (!node) {
324773e3206SIke Panhc 		pr_err("failed to create cfg in debugfs");
325773e3206SIke Panhc 		goto errout;
326773e3206SIke Panhc 	}
327773e3206SIke Panhc 
328331e0ea2SZhang Rui 	node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
329773e3206SIke Panhc 				   &debugfs_status_fops);
330773e3206SIke Panhc 	if (!node) {
331a5c3892fSIke Panhc 		pr_err("failed to create status in debugfs");
332773e3206SIke Panhc 		goto errout;
333773e3206SIke Panhc 	}
334773e3206SIke Panhc 
335773e3206SIke Panhc 	return 0;
336773e3206SIke Panhc 
337773e3206SIke Panhc errout:
338773e3206SIke Panhc 	return -ENOMEM;
339773e3206SIke Panhc }
340773e3206SIke Panhc 
341773e3206SIke Panhc static void ideapad_debugfs_exit(struct ideapad_private *priv)
342773e3206SIke Panhc {
343773e3206SIke Panhc 	debugfs_remove_recursive(priv->debug);
344773e3206SIke Panhc 	priv->debug = NULL;
345773e3206SIke Panhc }
346773e3206SIke Panhc 
347773e3206SIke Panhc /*
3483371f481SIke Panhc  * sysfs
349a4b5a279SIke Panhc  */
35057ac3b05SIke Panhc static ssize_t show_ideapad_cam(struct device *dev,
35157ac3b05SIke Panhc 				struct device_attribute *attr,
35257ac3b05SIke Panhc 				char *buf)
35357ac3b05SIke Panhc {
35457ac3b05SIke Panhc 	unsigned long result;
355331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
35657ac3b05SIke Panhc 
357331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
35857ac3b05SIke Panhc 		return sprintf(buf, "-1\n");
35957ac3b05SIke Panhc 	return sprintf(buf, "%lu\n", result);
36057ac3b05SIke Panhc }
36157ac3b05SIke Panhc 
36257ac3b05SIke Panhc static ssize_t store_ideapad_cam(struct device *dev,
36357ac3b05SIke Panhc 				 struct device_attribute *attr,
36457ac3b05SIke Panhc 				 const char *buf, size_t count)
36557ac3b05SIke Panhc {
36657ac3b05SIke Panhc 	int ret, state;
367331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
36857ac3b05SIke Panhc 
36957ac3b05SIke Panhc 	if (!count)
37057ac3b05SIke Panhc 		return 0;
37157ac3b05SIke Panhc 	if (sscanf(buf, "%i", &state) != 1)
37257ac3b05SIke Panhc 		return -EINVAL;
373331e0ea2SZhang Rui 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
37457ac3b05SIke Panhc 	if (ret < 0)
3750c7bbeb9SMaxim Mikityanskiy 		return -EIO;
37657ac3b05SIke Panhc 	return count;
37757ac3b05SIke Panhc }
37857ac3b05SIke Panhc 
37957ac3b05SIke Panhc static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
38057ac3b05SIke Panhc 
3810c7bbeb9SMaxim Mikityanskiy static ssize_t show_ideapad_fan(struct device *dev,
3820c7bbeb9SMaxim Mikityanskiy 				struct device_attribute *attr,
3830c7bbeb9SMaxim Mikityanskiy 				char *buf)
3840c7bbeb9SMaxim Mikityanskiy {
3850c7bbeb9SMaxim Mikityanskiy 	unsigned long result;
386331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
3870c7bbeb9SMaxim Mikityanskiy 
388331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
3890c7bbeb9SMaxim Mikityanskiy 		return sprintf(buf, "-1\n");
3900c7bbeb9SMaxim Mikityanskiy 	return sprintf(buf, "%lu\n", result);
3910c7bbeb9SMaxim Mikityanskiy }
3920c7bbeb9SMaxim Mikityanskiy 
3930c7bbeb9SMaxim Mikityanskiy static ssize_t store_ideapad_fan(struct device *dev,
3940c7bbeb9SMaxim Mikityanskiy 				 struct device_attribute *attr,
3950c7bbeb9SMaxim Mikityanskiy 				 const char *buf, size_t count)
3960c7bbeb9SMaxim Mikityanskiy {
3970c7bbeb9SMaxim Mikityanskiy 	int ret, state;
398331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
3990c7bbeb9SMaxim Mikityanskiy 
4000c7bbeb9SMaxim Mikityanskiy 	if (!count)
4010c7bbeb9SMaxim Mikityanskiy 		return 0;
4020c7bbeb9SMaxim Mikityanskiy 	if (sscanf(buf, "%i", &state) != 1)
4030c7bbeb9SMaxim Mikityanskiy 		return -EINVAL;
4040c7bbeb9SMaxim Mikityanskiy 	if (state < 0 || state > 4 || state == 3)
4050c7bbeb9SMaxim Mikityanskiy 		return -EINVAL;
406331e0ea2SZhang Rui 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
4070c7bbeb9SMaxim Mikityanskiy 	if (ret < 0)
4080c7bbeb9SMaxim Mikityanskiy 		return -EIO;
4090c7bbeb9SMaxim Mikityanskiy 	return count;
4100c7bbeb9SMaxim Mikityanskiy }
4110c7bbeb9SMaxim Mikityanskiy 
4120c7bbeb9SMaxim Mikityanskiy static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
4130c7bbeb9SMaxim Mikityanskiy 
4143371f481SIke Panhc static struct attribute *ideapad_attributes[] = {
4153371f481SIke Panhc 	&dev_attr_camera_power.attr,
4160c7bbeb9SMaxim Mikityanskiy 	&dev_attr_fan_mode.attr,
4173371f481SIke Panhc 	NULL
4183371f481SIke Panhc };
4193371f481SIke Panhc 
420587a1f16SAl Viro static umode_t ideapad_is_visible(struct kobject *kobj,
421a84511f7SIke Panhc 				 struct attribute *attr,
422a84511f7SIke Panhc 				 int idx)
423a84511f7SIke Panhc {
424a84511f7SIke Panhc 	struct device *dev = container_of(kobj, struct device, kobj);
425a84511f7SIke Panhc 	struct ideapad_private *priv = dev_get_drvdata(dev);
426a84511f7SIke Panhc 	bool supported;
427a84511f7SIke Panhc 
428a84511f7SIke Panhc 	if (attr == &dev_attr_camera_power.attr)
429a84511f7SIke Panhc 		supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
4300c7bbeb9SMaxim Mikityanskiy 	else if (attr == &dev_attr_fan_mode.attr) {
4310c7bbeb9SMaxim Mikityanskiy 		unsigned long value;
432331e0ea2SZhang Rui 		supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
433331e0ea2SZhang Rui 					  &value);
4340c7bbeb9SMaxim Mikityanskiy 	} else
435a84511f7SIke Panhc 		supported = true;
436a84511f7SIke Panhc 
437a84511f7SIke Panhc 	return supported ? attr->mode : 0;
438a84511f7SIke Panhc }
439a84511f7SIke Panhc 
4403371f481SIke Panhc static struct attribute_group ideapad_attribute_group = {
441a84511f7SIke Panhc 	.is_visible = ideapad_is_visible,
4423371f481SIke Panhc 	.attrs = ideapad_attributes
4433371f481SIke Panhc };
4443371f481SIke Panhc 
445a4b5a279SIke Panhc /*
446a4b5a279SIke Panhc  * Rfkill
447a4b5a279SIke Panhc  */
448c1f73658SIke Panhc struct ideapad_rfk_data {
449c1f73658SIke Panhc 	char *name;
450c1f73658SIke Panhc 	int cfgbit;
451c1f73658SIke Panhc 	int opcode;
452c1f73658SIke Panhc 	int type;
453c1f73658SIke Panhc };
454c1f73658SIke Panhc 
455c1f73658SIke Panhc const struct ideapad_rfk_data ideapad_rfk_data[] = {
4562be1dc21SIke Panhc 	{ "ideapad_wlan",    CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
4572be1dc21SIke Panhc 	{ "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
4582be1dc21SIke Panhc 	{ "ideapad_3g",        CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
459c1f73658SIke Panhc };
460c1f73658SIke Panhc 
46157ac3b05SIke Panhc static int ideapad_rfk_set(void *data, bool blocked)
46257ac3b05SIke Panhc {
463331e0ea2SZhang Rui 	struct ideapad_rfk_priv *priv = data;
46457ac3b05SIke Panhc 
465331e0ea2SZhang Rui 	return write_ec_cmd(priv->priv->adev->handle, priv->dev, !blocked);
46657ac3b05SIke Panhc }
46757ac3b05SIke Panhc 
46857ac3b05SIke Panhc static struct rfkill_ops ideapad_rfk_ops = {
46957ac3b05SIke Panhc 	.set_block = ideapad_rfk_set,
47057ac3b05SIke Panhc };
47157ac3b05SIke Panhc 
472923de84aSIke Panhc static void ideapad_sync_rfk_state(struct ideapad_private *priv)
47357ac3b05SIke Panhc {
47457ac3b05SIke Panhc 	unsigned long hw_blocked;
47557ac3b05SIke Panhc 	int i;
47657ac3b05SIke Panhc 
477331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
47857ac3b05SIke Panhc 		return;
47957ac3b05SIke Panhc 	hw_blocked = !hw_blocked;
48057ac3b05SIke Panhc 
481c1f73658SIke Panhc 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
48257ac3b05SIke Panhc 		if (priv->rfk[i])
48357ac3b05SIke Panhc 			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
48457ac3b05SIke Panhc }
48557ac3b05SIke Panhc 
48675a11f11SZhang Rui static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
48757ac3b05SIke Panhc {
48857ac3b05SIke Panhc 	int ret;
48957ac3b05SIke Panhc 	unsigned long sw_blocked;
49057ac3b05SIke Panhc 
491bfa97b7dSIke Panhc 	if (no_bt_rfkill &&
492bfa97b7dSIke Panhc 	    (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
493bfa97b7dSIke Panhc 		/* Force to enable bluetooth when no_bt_rfkill=1 */
494331e0ea2SZhang Rui 		write_ec_cmd(priv->adev->handle,
495bfa97b7dSIke Panhc 			     ideapad_rfk_data[dev].opcode, 1);
496bfa97b7dSIke Panhc 		return 0;
497bfa97b7dSIke Panhc 	}
498331e0ea2SZhang Rui 	priv->rfk_priv[dev].dev = dev;
499331e0ea2SZhang Rui 	priv->rfk_priv[dev].priv = priv;
500bfa97b7dSIke Panhc 
50175a11f11SZhang Rui 	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
502b5c37b79SZhang Rui 				      &priv->platform_device->dev,
50375a11f11SZhang Rui 				      ideapad_rfk_data[dev].type,
50475a11f11SZhang Rui 				      &ideapad_rfk_ops,
505331e0ea2SZhang Rui 				      &priv->rfk_priv[dev]);
50657ac3b05SIke Panhc 	if (!priv->rfk[dev])
50757ac3b05SIke Panhc 		return -ENOMEM;
50857ac3b05SIke Panhc 
509331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
51057ac3b05SIke Panhc 			 &sw_blocked)) {
51157ac3b05SIke Panhc 		rfkill_init_sw_state(priv->rfk[dev], 0);
51257ac3b05SIke Panhc 	} else {
51357ac3b05SIke Panhc 		sw_blocked = !sw_blocked;
51457ac3b05SIke Panhc 		rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
51557ac3b05SIke Panhc 	}
51657ac3b05SIke Panhc 
51757ac3b05SIke Panhc 	ret = rfkill_register(priv->rfk[dev]);
51857ac3b05SIke Panhc 	if (ret) {
51957ac3b05SIke Panhc 		rfkill_destroy(priv->rfk[dev]);
52057ac3b05SIke Panhc 		return ret;
52157ac3b05SIke Panhc 	}
52257ac3b05SIke Panhc 	return 0;
52357ac3b05SIke Panhc }
52457ac3b05SIke Panhc 
52575a11f11SZhang Rui static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
52657ac3b05SIke Panhc {
52757ac3b05SIke Panhc 	if (!priv->rfk[dev])
52857ac3b05SIke Panhc 		return;
52957ac3b05SIke Panhc 
53057ac3b05SIke Panhc 	rfkill_unregister(priv->rfk[dev]);
53157ac3b05SIke Panhc 	rfkill_destroy(priv->rfk[dev]);
53257ac3b05SIke Panhc }
53357ac3b05SIke Panhc 
53498ee6919SIke Panhc /*
53598ee6919SIke Panhc  * Platform device
53698ee6919SIke Panhc  */
537b5c37b79SZhang Rui static int ideapad_sysfs_init(struct ideapad_private *priv)
53898ee6919SIke Panhc {
539b5c37b79SZhang Rui 	return sysfs_create_group(&priv->platform_device->dev.kobj,
540c9f718d0SIke Panhc 				    &ideapad_attribute_group);
54198ee6919SIke Panhc }
54298ee6919SIke Panhc 
543b5c37b79SZhang Rui static void ideapad_sysfs_exit(struct ideapad_private *priv)
54498ee6919SIke Panhc {
5458693ae84SIke Panhc 	sysfs_remove_group(&priv->platform_device->dev.kobj,
546c9f718d0SIke Panhc 			   &ideapad_attribute_group);
54798ee6919SIke Panhc }
54898ee6919SIke Panhc 
549f63409aeSIke Panhc /*
550f63409aeSIke Panhc  * input device
551f63409aeSIke Panhc  */
552f63409aeSIke Panhc static const struct key_entry ideapad_keymap[] = {
553f43d9ec0SIke Panhc 	{ KE_KEY, 6,  { KEY_SWITCHVIDEOMODE } },
554296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 7,  { KEY_CAMERA } },
555296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 11, { KEY_F16 } },
556f43d9ec0SIke Panhc 	{ KE_KEY, 13, { KEY_WLAN } },
557f43d9ec0SIke Panhc 	{ KE_KEY, 16, { KEY_PROG1 } },
558f43d9ec0SIke Panhc 	{ KE_KEY, 17, { KEY_PROG2 } },
559296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 64, { KEY_PROG3 } },
560296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 65, { KEY_PROG4 } },
56107a4a4fcSMaxim Mikityanskiy 	{ KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
56207a4a4fcSMaxim Mikityanskiy 	{ KE_KEY, 67, { KEY_TOUCHPAD_ON } },
563f63409aeSIke Panhc 	{ KE_END, 0 },
564f63409aeSIke Panhc };
565f63409aeSIke Panhc 
566b859f159SGreg Kroah-Hartman static int ideapad_input_init(struct ideapad_private *priv)
567f63409aeSIke Panhc {
568f63409aeSIke Panhc 	struct input_dev *inputdev;
569f63409aeSIke Panhc 	int error;
570f63409aeSIke Panhc 
571f63409aeSIke Panhc 	inputdev = input_allocate_device();
572f63409aeSIke Panhc 	if (!inputdev) {
573f63409aeSIke Panhc 		pr_info("Unable to allocate input device\n");
574f63409aeSIke Panhc 		return -ENOMEM;
575f63409aeSIke Panhc 	}
576f63409aeSIke Panhc 
577f63409aeSIke Panhc 	inputdev->name = "Ideapad extra buttons";
578f63409aeSIke Panhc 	inputdev->phys = "ideapad/input0";
579f63409aeSIke Panhc 	inputdev->id.bustype = BUS_HOST;
5808693ae84SIke Panhc 	inputdev->dev.parent = &priv->platform_device->dev;
581f63409aeSIke Panhc 
582f63409aeSIke Panhc 	error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
583f63409aeSIke Panhc 	if (error) {
584f63409aeSIke Panhc 		pr_err("Unable to setup input device keymap\n");
585f63409aeSIke Panhc 		goto err_free_dev;
586f63409aeSIke Panhc 	}
587f63409aeSIke Panhc 
588f63409aeSIke Panhc 	error = input_register_device(inputdev);
589f63409aeSIke Panhc 	if (error) {
590f63409aeSIke Panhc 		pr_err("Unable to register input device\n");
591f63409aeSIke Panhc 		goto err_free_keymap;
592f63409aeSIke Panhc 	}
593f63409aeSIke Panhc 
5948693ae84SIke Panhc 	priv->inputdev = inputdev;
595f63409aeSIke Panhc 	return 0;
596f63409aeSIke Panhc 
597f63409aeSIke Panhc err_free_keymap:
598f63409aeSIke Panhc 	sparse_keymap_free(inputdev);
599f63409aeSIke Panhc err_free_dev:
600f63409aeSIke Panhc 	input_free_device(inputdev);
601f63409aeSIke Panhc 	return error;
602f63409aeSIke Panhc }
603f63409aeSIke Panhc 
6047451a55aSAxel Lin static void ideapad_input_exit(struct ideapad_private *priv)
605f63409aeSIke Panhc {
6068693ae84SIke Panhc 	sparse_keymap_free(priv->inputdev);
6078693ae84SIke Panhc 	input_unregister_device(priv->inputdev);
6088693ae84SIke Panhc 	priv->inputdev = NULL;
609f63409aeSIke Panhc }
610f63409aeSIke Panhc 
6118693ae84SIke Panhc static void ideapad_input_report(struct ideapad_private *priv,
6128693ae84SIke Panhc 				 unsigned long scancode)
613f63409aeSIke Panhc {
6148693ae84SIke Panhc 	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
615f63409aeSIke Panhc }
616f63409aeSIke Panhc 
617f43d9ec0SIke Panhc static void ideapad_input_novokey(struct ideapad_private *priv)
618f43d9ec0SIke Panhc {
619f43d9ec0SIke Panhc 	unsigned long long_pressed;
620f43d9ec0SIke Panhc 
621331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
622f43d9ec0SIke Panhc 		return;
623f43d9ec0SIke Panhc 	if (long_pressed)
624f43d9ec0SIke Panhc 		ideapad_input_report(priv, 17);
625f43d9ec0SIke Panhc 	else
626f43d9ec0SIke Panhc 		ideapad_input_report(priv, 16);
627f43d9ec0SIke Panhc }
628f43d9ec0SIke Panhc 
629296f9fe0SMaxim Mikityanskiy static void ideapad_check_special_buttons(struct ideapad_private *priv)
630296f9fe0SMaxim Mikityanskiy {
631296f9fe0SMaxim Mikityanskiy 	unsigned long bit, value;
632296f9fe0SMaxim Mikityanskiy 
633331e0ea2SZhang Rui 	read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
634296f9fe0SMaxim Mikityanskiy 
635296f9fe0SMaxim Mikityanskiy 	for (bit = 0; bit < 16; bit++) {
636296f9fe0SMaxim Mikityanskiy 		if (test_bit(bit, &value)) {
637296f9fe0SMaxim Mikityanskiy 			switch (bit) {
638a1ec56edSMaxim Mikityanskiy 			case 0:	/* Z580 */
639a1ec56edSMaxim Mikityanskiy 			case 6:	/* Z570 */
640296f9fe0SMaxim Mikityanskiy 				/* Thermal Management button */
641296f9fe0SMaxim Mikityanskiy 				ideapad_input_report(priv, 65);
642296f9fe0SMaxim Mikityanskiy 				break;
643296f9fe0SMaxim Mikityanskiy 			case 1:
644296f9fe0SMaxim Mikityanskiy 				/* OneKey Theater button */
645296f9fe0SMaxim Mikityanskiy 				ideapad_input_report(priv, 64);
646296f9fe0SMaxim Mikityanskiy 				break;
647a1ec56edSMaxim Mikityanskiy 			default:
648a1ec56edSMaxim Mikityanskiy 				pr_info("Unknown special button: %lu\n", bit);
649a1ec56edSMaxim Mikityanskiy 				break;
650296f9fe0SMaxim Mikityanskiy 			}
651296f9fe0SMaxim Mikityanskiy 		}
652296f9fe0SMaxim Mikityanskiy 	}
653296f9fe0SMaxim Mikityanskiy }
654296f9fe0SMaxim Mikityanskiy 
655a4b5a279SIke Panhc /*
656a4ecbb8aSIke Panhc  * backlight
657a4ecbb8aSIke Panhc  */
658a4ecbb8aSIke Panhc static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
659a4ecbb8aSIke Panhc {
660331e0ea2SZhang Rui 	struct ideapad_private *priv = bl_get_data(blightdev);
661a4ecbb8aSIke Panhc 	unsigned long now;
662a4ecbb8aSIke Panhc 
663331e0ea2SZhang Rui 	if (!priv)
664331e0ea2SZhang Rui 		return -EINVAL;
665331e0ea2SZhang Rui 
666331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
667a4ecbb8aSIke Panhc 		return -EIO;
668a4ecbb8aSIke Panhc 	return now;
669a4ecbb8aSIke Panhc }
670a4ecbb8aSIke Panhc 
671a4ecbb8aSIke Panhc static int ideapad_backlight_update_status(struct backlight_device *blightdev)
672a4ecbb8aSIke Panhc {
673331e0ea2SZhang Rui 	struct ideapad_private *priv = bl_get_data(blightdev);
674331e0ea2SZhang Rui 
675331e0ea2SZhang Rui 	if (!priv)
676331e0ea2SZhang Rui 		return -EINVAL;
677331e0ea2SZhang Rui 
678331e0ea2SZhang Rui 	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
6792be1dc21SIke Panhc 			 blightdev->props.brightness))
680a4ecbb8aSIke Panhc 		return -EIO;
681331e0ea2SZhang Rui 	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
682a4ecbb8aSIke Panhc 			 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
683a4ecbb8aSIke Panhc 		return -EIO;
684a4ecbb8aSIke Panhc 
685a4ecbb8aSIke Panhc 	return 0;
686a4ecbb8aSIke Panhc }
687a4ecbb8aSIke Panhc 
688a4ecbb8aSIke Panhc static const struct backlight_ops ideapad_backlight_ops = {
689a4ecbb8aSIke Panhc 	.get_brightness = ideapad_backlight_get_brightness,
690a4ecbb8aSIke Panhc 	.update_status = ideapad_backlight_update_status,
691a4ecbb8aSIke Panhc };
692a4ecbb8aSIke Panhc 
693a4ecbb8aSIke Panhc static int ideapad_backlight_init(struct ideapad_private *priv)
694a4ecbb8aSIke Panhc {
695a4ecbb8aSIke Panhc 	struct backlight_device *blightdev;
696a4ecbb8aSIke Panhc 	struct backlight_properties props;
697a4ecbb8aSIke Panhc 	unsigned long max, now, power;
698a4ecbb8aSIke Panhc 
699331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
700a4ecbb8aSIke Panhc 		return -EIO;
701331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
702a4ecbb8aSIke Panhc 		return -EIO;
703331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
704a4ecbb8aSIke Panhc 		return -EIO;
705a4ecbb8aSIke Panhc 
706a4ecbb8aSIke Panhc 	memset(&props, 0, sizeof(struct backlight_properties));
707a4ecbb8aSIke Panhc 	props.max_brightness = max;
708a4ecbb8aSIke Panhc 	props.type = BACKLIGHT_PLATFORM;
709a4ecbb8aSIke Panhc 	blightdev = backlight_device_register("ideapad",
710a4ecbb8aSIke Panhc 					      &priv->platform_device->dev,
711a4ecbb8aSIke Panhc 					      priv,
712a4ecbb8aSIke Panhc 					      &ideapad_backlight_ops,
713a4ecbb8aSIke Panhc 					      &props);
714a4ecbb8aSIke Panhc 	if (IS_ERR(blightdev)) {
715a4ecbb8aSIke Panhc 		pr_err("Could not register backlight device\n");
716a4ecbb8aSIke Panhc 		return PTR_ERR(blightdev);
717a4ecbb8aSIke Panhc 	}
718a4ecbb8aSIke Panhc 
719a4ecbb8aSIke Panhc 	priv->blightdev = blightdev;
720a4ecbb8aSIke Panhc 	blightdev->props.brightness = now;
721a4ecbb8aSIke Panhc 	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
722a4ecbb8aSIke Panhc 	backlight_update_status(blightdev);
723a4ecbb8aSIke Panhc 
724a4ecbb8aSIke Panhc 	return 0;
725a4ecbb8aSIke Panhc }
726a4ecbb8aSIke Panhc 
727a4ecbb8aSIke Panhc static void ideapad_backlight_exit(struct ideapad_private *priv)
728a4ecbb8aSIke Panhc {
729a4ecbb8aSIke Panhc 	if (priv->blightdev)
730a4ecbb8aSIke Panhc 		backlight_device_unregister(priv->blightdev);
731a4ecbb8aSIke Panhc 	priv->blightdev = NULL;
732a4ecbb8aSIke Panhc }
733a4ecbb8aSIke Panhc 
734a4ecbb8aSIke Panhc static void ideapad_backlight_notify_power(struct ideapad_private *priv)
735a4ecbb8aSIke Panhc {
736a4ecbb8aSIke Panhc 	unsigned long power;
737a4ecbb8aSIke Panhc 	struct backlight_device *blightdev = priv->blightdev;
738a4ecbb8aSIke Panhc 
739d4afc775SRene Bollford 	if (!blightdev)
740d4afc775SRene Bollford 		return;
741331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
742a4ecbb8aSIke Panhc 		return;
743a4ecbb8aSIke Panhc 	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
744a4ecbb8aSIke Panhc }
745a4ecbb8aSIke Panhc 
746a4ecbb8aSIke Panhc static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
747a4ecbb8aSIke Panhc {
748a4ecbb8aSIke Panhc 	unsigned long now;
749a4ecbb8aSIke Panhc 
750a4ecbb8aSIke Panhc 	/* if we control brightness via acpi video driver */
751a4ecbb8aSIke Panhc 	if (priv->blightdev == NULL) {
752331e0ea2SZhang Rui 		read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
753a4ecbb8aSIke Panhc 		return;
754a4ecbb8aSIke Panhc 	}
755a4ecbb8aSIke Panhc 
756a4ecbb8aSIke Panhc 	backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
757a4ecbb8aSIke Panhc }
758a4ecbb8aSIke Panhc 
759a4ecbb8aSIke Panhc /*
760a4b5a279SIke Panhc  * module init/exit
761a4b5a279SIke Panhc  */
76275a11f11SZhang Rui static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
76307a4a4fcSMaxim Mikityanskiy {
76407a4a4fcSMaxim Mikityanskiy 	unsigned long value;
76507a4a4fcSMaxim Mikityanskiy 
76607a4a4fcSMaxim Mikityanskiy 	/* Without reading from EC touchpad LED doesn't switch state */
76775a11f11SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
76807a4a4fcSMaxim Mikityanskiy 		/* Some IdeaPads don't really turn off touchpad - they only
76907a4a4fcSMaxim Mikityanskiy 		 * switch the LED state. We (de)activate KBC AUX port to turn
77007a4a4fcSMaxim Mikityanskiy 		 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
77107a4a4fcSMaxim Mikityanskiy 		 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
77207a4a4fcSMaxim Mikityanskiy 		unsigned char param;
77307a4a4fcSMaxim Mikityanskiy 		i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
77407a4a4fcSMaxim Mikityanskiy 			      I8042_CMD_AUX_DISABLE);
77507a4a4fcSMaxim Mikityanskiy 		ideapad_input_report(priv, value ? 67 : 66);
77607a4a4fcSMaxim Mikityanskiy 	}
77707a4a4fcSMaxim Mikityanskiy }
77807a4a4fcSMaxim Mikityanskiy 
779b5c37b79SZhang Rui static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
78057ac3b05SIke Panhc {
781b5c37b79SZhang Rui 	struct ideapad_private *priv = data;
78257ac3b05SIke Panhc 	unsigned long vpc1, vpc2, vpc_bit;
78357ac3b05SIke Panhc 
7842be1dc21SIke Panhc 	if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
78557ac3b05SIke Panhc 		return;
7862be1dc21SIke Panhc 	if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
78757ac3b05SIke Panhc 		return;
78857ac3b05SIke Panhc 
78957ac3b05SIke Panhc 	vpc1 = (vpc2 << 8) | vpc1;
79057ac3b05SIke Panhc 	for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
79157ac3b05SIke Panhc 		if (test_bit(vpc_bit, &vpc1)) {
792a4ecbb8aSIke Panhc 			switch (vpc_bit) {
793a4ecbb8aSIke Panhc 			case 9:
794923de84aSIke Panhc 				ideapad_sync_rfk_state(priv);
795a4ecbb8aSIke Panhc 				break;
79620a769c1SIke Panhc 			case 13:
797296f9fe0SMaxim Mikityanskiy 			case 11:
798296f9fe0SMaxim Mikityanskiy 			case 7:
79920a769c1SIke Panhc 			case 6:
80020a769c1SIke Panhc 				ideapad_input_report(priv, vpc_bit);
80120a769c1SIke Panhc 				break;
80207a4a4fcSMaxim Mikityanskiy 			case 5:
80375a11f11SZhang Rui 				ideapad_sync_touchpad_state(priv);
80407a4a4fcSMaxim Mikityanskiy 				break;
805a4ecbb8aSIke Panhc 			case 4:
806a4ecbb8aSIke Panhc 				ideapad_backlight_notify_brightness(priv);
807a4ecbb8aSIke Panhc 				break;
808f43d9ec0SIke Panhc 			case 3:
809f43d9ec0SIke Panhc 				ideapad_input_novokey(priv);
810f43d9ec0SIke Panhc 				break;
811a4ecbb8aSIke Panhc 			case 2:
812a4ecbb8aSIke Panhc 				ideapad_backlight_notify_power(priv);
813a4ecbb8aSIke Panhc 				break;
814296f9fe0SMaxim Mikityanskiy 			case 0:
815296f9fe0SMaxim Mikityanskiy 				ideapad_check_special_buttons(priv);
816296f9fe0SMaxim Mikityanskiy 				break;
817a4ecbb8aSIke Panhc 			default:
81820a769c1SIke Panhc 				pr_info("Unknown event: %lu\n", vpc_bit);
81957ac3b05SIke Panhc 			}
82057ac3b05SIke Panhc 		}
82157ac3b05SIke Panhc 	}
822a4ecbb8aSIke Panhc }
82357ac3b05SIke Panhc 
824b5c37b79SZhang Rui static int ideapad_acpi_add(struct platform_device *pdev)
825b5c37b79SZhang Rui {
826b5c37b79SZhang Rui 	int ret, i;
827b5c37b79SZhang Rui 	int cfg;
828b5c37b79SZhang Rui 	struct ideapad_private *priv;
829b5c37b79SZhang Rui 	struct acpi_device *adev;
830b5c37b79SZhang Rui 
831b5c37b79SZhang Rui 	ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
832b5c37b79SZhang Rui 	if (ret)
833b5c37b79SZhang Rui 		return -ENODEV;
834b5c37b79SZhang Rui 
835b5c37b79SZhang Rui 	if (read_method_int(adev->handle, "_CFG", &cfg))
836b5c37b79SZhang Rui 		return -ENODEV;
837b5c37b79SZhang Rui 
838b5c37b79SZhang Rui 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
839b5c37b79SZhang Rui 	if (!priv)
840b5c37b79SZhang Rui 		return -ENOMEM;
841b5c37b79SZhang Rui 
842b5c37b79SZhang Rui 	dev_set_drvdata(&pdev->dev, priv);
843b5c37b79SZhang Rui 	priv->cfg = cfg;
844b5c37b79SZhang Rui 	priv->adev = adev;
845b5c37b79SZhang Rui 	priv->platform_device = pdev;
846b5c37b79SZhang Rui 
847b5c37b79SZhang Rui 	ret = ideapad_sysfs_init(priv);
848b5c37b79SZhang Rui 	if (ret)
849b5c37b79SZhang Rui 		goto sysfs_failed;
850b5c37b79SZhang Rui 
851b5c37b79SZhang Rui 	ret = ideapad_debugfs_init(priv);
852b5c37b79SZhang Rui 	if (ret)
853b5c37b79SZhang Rui 		goto debugfs_failed;
854b5c37b79SZhang Rui 
855b5c37b79SZhang Rui 	ret = ideapad_input_init(priv);
856b5c37b79SZhang Rui 	if (ret)
857b5c37b79SZhang Rui 		goto input_failed;
858b5c37b79SZhang Rui 
859b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
860b5c37b79SZhang Rui 		if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
861b5c37b79SZhang Rui 			ideapad_register_rfkill(priv, i);
862b5c37b79SZhang Rui 		else
863b5c37b79SZhang Rui 			priv->rfk[i] = NULL;
864b5c37b79SZhang Rui 	}
865b5c37b79SZhang Rui 	ideapad_sync_rfk_state(priv);
866b5c37b79SZhang Rui 	ideapad_sync_touchpad_state(priv);
867b5c37b79SZhang Rui 
868b5c37b79SZhang Rui 	if (!acpi_video_backlight_support()) {
869b5c37b79SZhang Rui 		ret = ideapad_backlight_init(priv);
870b5c37b79SZhang Rui 		if (ret && ret != -ENODEV)
871b5c37b79SZhang Rui 			goto backlight_failed;
872b5c37b79SZhang Rui 	}
873b5c37b79SZhang Rui 	ret = acpi_install_notify_handler(adev->handle,
874b5c37b79SZhang Rui 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
875b5c37b79SZhang Rui 	if (ret)
876b5c37b79SZhang Rui 		goto notification_failed;
877b5c37b79SZhang Rui 
878b5c37b79SZhang Rui 	return 0;
879b5c37b79SZhang Rui notification_failed:
880b5c37b79SZhang Rui 	ideapad_backlight_exit(priv);
881b5c37b79SZhang Rui backlight_failed:
882b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
883b5c37b79SZhang Rui 		ideapad_unregister_rfkill(priv, i);
884b5c37b79SZhang Rui 	ideapad_input_exit(priv);
885b5c37b79SZhang Rui input_failed:
886b5c37b79SZhang Rui 	ideapad_debugfs_exit(priv);
887b5c37b79SZhang Rui debugfs_failed:
888b5c37b79SZhang Rui 	ideapad_sysfs_exit(priv);
889b5c37b79SZhang Rui sysfs_failed:
890b5c37b79SZhang Rui 	kfree(priv);
891b5c37b79SZhang Rui 	return ret;
892b5c37b79SZhang Rui }
893b5c37b79SZhang Rui 
894b5c37b79SZhang Rui static int ideapad_acpi_remove(struct platform_device *pdev)
895b5c37b79SZhang Rui {
896b5c37b79SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
897b5c37b79SZhang Rui 	int i;
898b5c37b79SZhang Rui 
899b5c37b79SZhang Rui 	acpi_remove_notify_handler(priv->adev->handle,
900b5c37b79SZhang Rui 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
901b5c37b79SZhang Rui 	ideapad_backlight_exit(priv);
902b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
903b5c37b79SZhang Rui 		ideapad_unregister_rfkill(priv, i);
904b5c37b79SZhang Rui 	ideapad_input_exit(priv);
905b5c37b79SZhang Rui 	ideapad_debugfs_exit(priv);
906b5c37b79SZhang Rui 	ideapad_sysfs_exit(priv);
907b5c37b79SZhang Rui 	dev_set_drvdata(&pdev->dev, NULL);
908b5c37b79SZhang Rui 	kfree(priv);
909b5c37b79SZhang Rui 
910b5c37b79SZhang Rui 	return 0;
911b5c37b79SZhang Rui }
912b5c37b79SZhang Rui 
91311fa8da5SZhang Rui #ifdef CONFIG_PM_SLEEP
91407a4a4fcSMaxim Mikityanskiy static int ideapad_acpi_resume(struct device *device)
91507a4a4fcSMaxim Mikityanskiy {
91675a11f11SZhang Rui 	struct ideapad_private *priv;
91775a11f11SZhang Rui 
91875a11f11SZhang Rui 	if (!device)
91975a11f11SZhang Rui 		return -EINVAL;
92075a11f11SZhang Rui 	priv = dev_get_drvdata(device);
92175a11f11SZhang Rui 
92275a11f11SZhang Rui 	ideapad_sync_rfk_state(priv);
92375a11f11SZhang Rui 	ideapad_sync_touchpad_state(priv);
92407a4a4fcSMaxim Mikityanskiy 	return 0;
92507a4a4fcSMaxim Mikityanskiy }
926b5c37b79SZhang Rui #endif
92707a4a4fcSMaxim Mikityanskiy static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
92807a4a4fcSMaxim Mikityanskiy 
929b5c37b79SZhang Rui static const struct acpi_device_id ideapad_device_ids[] = {
930b5c37b79SZhang Rui 	{ "VPC2004", 0},
931b5c37b79SZhang Rui 	{ "", 0},
93257ac3b05SIke Panhc };
933b5c37b79SZhang Rui MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
934b5c37b79SZhang Rui 
935b5c37b79SZhang Rui static struct platform_driver ideapad_acpi_driver = {
936b5c37b79SZhang Rui 	.probe = ideapad_acpi_add,
937b5c37b79SZhang Rui 	.remove = ideapad_acpi_remove,
938b5c37b79SZhang Rui 	.driver = {
939b5c37b79SZhang Rui 		.name   = "ideapad_acpi",
940b5c37b79SZhang Rui 		.owner  = THIS_MODULE,
941b5c37b79SZhang Rui 		.pm     = &ideapad_pm,
942b5c37b79SZhang Rui 		.acpi_match_table = ACPI_PTR(ideapad_device_ids),
943b5c37b79SZhang Rui 	},
944b5c37b79SZhang Rui };
945b5c37b79SZhang Rui 
946b5c37b79SZhang Rui module_platform_driver(ideapad_acpi_driver);
94757ac3b05SIke Panhc 
94857ac3b05SIke Panhc MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
94957ac3b05SIke Panhc MODULE_DESCRIPTION("IdeaPad ACPI Extras");
95057ac3b05SIke Panhc MODULE_LICENSE("GPL");
951