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>
3985093f79SHans de Goede #include <linux/dmi.h>
40b3facd7bSHimangi Saraogi #include <linux/device.h>
4126bff5f0SHans de Goede #include <acpi/video.h>
4257ac3b05SIke Panhc 
43c1f73658SIke Panhc #define IDEAPAD_RFKILL_DEV_NUM	(3)
4457ac3b05SIke Panhc 
453371f481SIke Panhc #define CFG_BT_BIT	(16)
463371f481SIke Panhc #define CFG_3G_BIT	(17)
473371f481SIke Panhc #define CFG_WIFI_BIT	(18)
48a84511f7SIke Panhc #define CFG_CAMERA_BIT	(19)
493371f481SIke Panhc 
5074caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
512d98e0b9SArnd Bergmann static const char *const ideapad_wmi_fnesc_events[] = {
522d98e0b9SArnd Bergmann 	"26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
532d98e0b9SArnd Bergmann 	"56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
542d98e0b9SArnd Bergmann };
5574caab99SArnd Bergmann #endif
5674caab99SArnd Bergmann 
572be1dc21SIke Panhc enum {
582be1dc21SIke Panhc 	VPCCMD_R_VPC1 = 0x10,
592be1dc21SIke Panhc 	VPCCMD_R_BL_MAX,
602be1dc21SIke Panhc 	VPCCMD_R_BL,
612be1dc21SIke Panhc 	VPCCMD_W_BL,
622be1dc21SIke Panhc 	VPCCMD_R_WIFI,
632be1dc21SIke Panhc 	VPCCMD_W_WIFI,
642be1dc21SIke Panhc 	VPCCMD_R_BT,
652be1dc21SIke Panhc 	VPCCMD_W_BT,
662be1dc21SIke Panhc 	VPCCMD_R_BL_POWER,
672be1dc21SIke Panhc 	VPCCMD_R_NOVO,
682be1dc21SIke Panhc 	VPCCMD_R_VPC2,
692be1dc21SIke Panhc 	VPCCMD_R_TOUCHPAD,
702be1dc21SIke Panhc 	VPCCMD_W_TOUCHPAD,
712be1dc21SIke Panhc 	VPCCMD_R_CAMERA,
722be1dc21SIke Panhc 	VPCCMD_W_CAMERA,
732be1dc21SIke Panhc 	VPCCMD_R_3G,
742be1dc21SIke Panhc 	VPCCMD_W_3G,
752be1dc21SIke Panhc 	VPCCMD_R_ODD, /* 0x21 */
760c7bbeb9SMaxim Mikityanskiy 	VPCCMD_W_FAN,
770c7bbeb9SMaxim Mikityanskiy 	VPCCMD_R_RF,
782be1dc21SIke Panhc 	VPCCMD_W_RF,
790c7bbeb9SMaxim Mikityanskiy 	VPCCMD_R_FAN = 0x2B,
80296f9fe0SMaxim Mikityanskiy 	VPCCMD_R_SPECIAL_BUTTONS = 0x31,
812be1dc21SIke Panhc 	VPCCMD_W_BL_POWER = 0x33,
822be1dc21SIke Panhc };
832be1dc21SIke Panhc 
84331e0ea2SZhang Rui struct ideapad_rfk_priv {
85331e0ea2SZhang Rui 	int dev;
86331e0ea2SZhang Rui 	struct ideapad_private *priv;
87331e0ea2SZhang Rui };
88331e0ea2SZhang Rui 
8957ac3b05SIke Panhc struct ideapad_private {
90469f6434SZhang Rui 	struct acpi_device *adev;
91c1f73658SIke Panhc 	struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
92331e0ea2SZhang Rui 	struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
9398ee6919SIke Panhc 	struct platform_device *platform_device;
94f63409aeSIke Panhc 	struct input_dev *inputdev;
95a4ecbb8aSIke Panhc 	struct backlight_device *blightdev;
96773e3206SIke Panhc 	struct dentry *debug;
973371f481SIke Panhc 	unsigned long cfg;
98ce363c2bSHans de Goede 	bool has_hw_rfkill_switch;
992d98e0b9SArnd Bergmann 	const char *fnesc_guid;
10057ac3b05SIke Panhc };
10157ac3b05SIke Panhc 
102bfa97b7dSIke Panhc static bool no_bt_rfkill;
103bfa97b7dSIke Panhc module_param(no_bt_rfkill, bool, 0444);
104bfa97b7dSIke Panhc MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
105bfa97b7dSIke Panhc 
10657ac3b05SIke Panhc /*
10757ac3b05SIke Panhc  * ACPI Helpers
10857ac3b05SIke Panhc  */
10957ac3b05SIke Panhc #define IDEAPAD_EC_TIMEOUT (100) /* in ms */
11057ac3b05SIke Panhc 
11157ac3b05SIke Panhc static int read_method_int(acpi_handle handle, const char *method, int *val)
11257ac3b05SIke Panhc {
11357ac3b05SIke Panhc 	acpi_status status;
11457ac3b05SIke Panhc 	unsigned long long result;
11557ac3b05SIke Panhc 
11657ac3b05SIke Panhc 	status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
11757ac3b05SIke Panhc 	if (ACPI_FAILURE(status)) {
11857ac3b05SIke Panhc 		*val = -1;
11957ac3b05SIke Panhc 		return -1;
12057ac3b05SIke Panhc 	} else {
12157ac3b05SIke Panhc 		*val = result;
12257ac3b05SIke Panhc 		return 0;
12357ac3b05SIke Panhc 	}
12457ac3b05SIke Panhc }
12557ac3b05SIke Panhc 
12657ac3b05SIke Panhc static int method_vpcr(acpi_handle handle, int cmd, int *ret)
12757ac3b05SIke Panhc {
12857ac3b05SIke Panhc 	acpi_status status;
12957ac3b05SIke Panhc 	unsigned long long result;
13057ac3b05SIke Panhc 	struct acpi_object_list params;
13157ac3b05SIke Panhc 	union acpi_object in_obj;
13257ac3b05SIke Panhc 
13357ac3b05SIke Panhc 	params.count = 1;
13457ac3b05SIke Panhc 	params.pointer = &in_obj;
13557ac3b05SIke Panhc 	in_obj.type = ACPI_TYPE_INTEGER;
13657ac3b05SIke Panhc 	in_obj.integer.value = cmd;
13757ac3b05SIke Panhc 
13857ac3b05SIke Panhc 	status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
13957ac3b05SIke Panhc 
14057ac3b05SIke Panhc 	if (ACPI_FAILURE(status)) {
14157ac3b05SIke Panhc 		*ret = -1;
14257ac3b05SIke Panhc 		return -1;
14357ac3b05SIke Panhc 	} else {
14457ac3b05SIke Panhc 		*ret = result;
14557ac3b05SIke Panhc 		return 0;
14657ac3b05SIke Panhc 	}
14757ac3b05SIke Panhc }
14857ac3b05SIke Panhc 
14957ac3b05SIke Panhc static int method_vpcw(acpi_handle handle, int cmd, int data)
15057ac3b05SIke Panhc {
15157ac3b05SIke Panhc 	struct acpi_object_list params;
15257ac3b05SIke Panhc 	union acpi_object in_obj[2];
15357ac3b05SIke Panhc 	acpi_status status;
15457ac3b05SIke Panhc 
15557ac3b05SIke Panhc 	params.count = 2;
15657ac3b05SIke Panhc 	params.pointer = in_obj;
15757ac3b05SIke Panhc 	in_obj[0].type = ACPI_TYPE_INTEGER;
15857ac3b05SIke Panhc 	in_obj[0].integer.value = cmd;
15957ac3b05SIke Panhc 	in_obj[1].type = ACPI_TYPE_INTEGER;
16057ac3b05SIke Panhc 	in_obj[1].integer.value = data;
16157ac3b05SIke Panhc 
16257ac3b05SIke Panhc 	status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
16357ac3b05SIke Panhc 	if (status != AE_OK)
16457ac3b05SIke Panhc 		return -1;
16557ac3b05SIke Panhc 	return 0;
16657ac3b05SIke Panhc }
16757ac3b05SIke Panhc 
16857ac3b05SIke Panhc static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
16957ac3b05SIke Panhc {
17057ac3b05SIke Panhc 	int val;
17157ac3b05SIke Panhc 	unsigned long int end_jiffies;
17257ac3b05SIke Panhc 
17357ac3b05SIke Panhc 	if (method_vpcw(handle, 1, cmd))
17457ac3b05SIke Panhc 		return -1;
17557ac3b05SIke Panhc 
17657ac3b05SIke Panhc 	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
17757ac3b05SIke Panhc 	     time_before(jiffies, end_jiffies);) {
17857ac3b05SIke Panhc 		schedule();
17957ac3b05SIke Panhc 		if (method_vpcr(handle, 1, &val))
18057ac3b05SIke Panhc 			return -1;
18157ac3b05SIke Panhc 		if (val == 0) {
18257ac3b05SIke Panhc 			if (method_vpcr(handle, 0, &val))
18357ac3b05SIke Panhc 				return -1;
18457ac3b05SIke Panhc 			*data = val;
18557ac3b05SIke Panhc 			return 0;
18657ac3b05SIke Panhc 		}
18757ac3b05SIke Panhc 	}
18857ac3b05SIke Panhc 	pr_err("timeout in read_ec_cmd\n");
18957ac3b05SIke Panhc 	return -1;
19057ac3b05SIke Panhc }
19157ac3b05SIke Panhc 
19257ac3b05SIke Panhc static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
19357ac3b05SIke Panhc {
19457ac3b05SIke Panhc 	int val;
19557ac3b05SIke Panhc 	unsigned long int end_jiffies;
19657ac3b05SIke Panhc 
19757ac3b05SIke Panhc 	if (method_vpcw(handle, 0, data))
19857ac3b05SIke Panhc 		return -1;
19957ac3b05SIke Panhc 	if (method_vpcw(handle, 1, cmd))
20057ac3b05SIke Panhc 		return -1;
20157ac3b05SIke Panhc 
20257ac3b05SIke Panhc 	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
20357ac3b05SIke Panhc 	     time_before(jiffies, end_jiffies);) {
20457ac3b05SIke Panhc 		schedule();
20557ac3b05SIke Panhc 		if (method_vpcr(handle, 1, &val))
20657ac3b05SIke Panhc 			return -1;
20757ac3b05SIke Panhc 		if (val == 0)
20857ac3b05SIke Panhc 			return 0;
20957ac3b05SIke Panhc 	}
21057ac3b05SIke Panhc 	pr_err("timeout in write_ec_cmd\n");
21157ac3b05SIke Panhc 	return -1;
21257ac3b05SIke Panhc }
21357ac3b05SIke Panhc 
214a4b5a279SIke Panhc /*
215773e3206SIke Panhc  * debugfs
216773e3206SIke Panhc  */
217773e3206SIke Panhc static int debugfs_status_show(struct seq_file *s, void *data)
218773e3206SIke Panhc {
219331e0ea2SZhang Rui 	struct ideapad_private *priv = s->private;
220773e3206SIke Panhc 	unsigned long value;
221773e3206SIke Panhc 
222331e0ea2SZhang Rui 	if (!priv)
223331e0ea2SZhang Rui 		return -EINVAL;
224331e0ea2SZhang Rui 
225331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
226773e3206SIke Panhc 		seq_printf(s, "Backlight max:\t%lu\n", value);
227331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
228773e3206SIke Panhc 		seq_printf(s, "Backlight now:\t%lu\n", value);
229331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
230773e3206SIke Panhc 		seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
231773e3206SIke Panhc 	seq_printf(s, "=====================\n");
232773e3206SIke Panhc 
233331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
234773e3206SIke Panhc 		seq_printf(s, "Radio status:\t%s(%lu)\n",
235773e3206SIke Panhc 			   value ? "On" : "Off", value);
236331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
237773e3206SIke Panhc 		seq_printf(s, "Wifi status:\t%s(%lu)\n",
238773e3206SIke Panhc 			   value ? "On" : "Off", value);
239331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
240773e3206SIke Panhc 		seq_printf(s, "BT status:\t%s(%lu)\n",
241773e3206SIke Panhc 			   value ? "On" : "Off", value);
242331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
243773e3206SIke Panhc 		seq_printf(s, "3G status:\t%s(%lu)\n",
244773e3206SIke Panhc 			   value ? "On" : "Off", value);
245773e3206SIke Panhc 	seq_printf(s, "=====================\n");
246773e3206SIke Panhc 
247331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
248773e3206SIke Panhc 		seq_printf(s, "Touchpad status:%s(%lu)\n",
249773e3206SIke Panhc 			   value ? "On" : "Off", value);
250331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
251773e3206SIke Panhc 		seq_printf(s, "Camera status:\t%s(%lu)\n",
252773e3206SIke Panhc 			   value ? "On" : "Off", value);
253773e3206SIke Panhc 
254773e3206SIke Panhc 	return 0;
255773e3206SIke Panhc }
256773e3206SIke Panhc 
257773e3206SIke Panhc static int debugfs_status_open(struct inode *inode, struct file *file)
258773e3206SIke Panhc {
259331e0ea2SZhang Rui 	return single_open(file, debugfs_status_show, inode->i_private);
260773e3206SIke Panhc }
261773e3206SIke Panhc 
262773e3206SIke Panhc static const struct file_operations debugfs_status_fops = {
263773e3206SIke Panhc 	.owner = THIS_MODULE,
264773e3206SIke Panhc 	.open = debugfs_status_open,
265773e3206SIke Panhc 	.read = seq_read,
266773e3206SIke Panhc 	.llseek = seq_lseek,
267773e3206SIke Panhc 	.release = single_release,
268773e3206SIke Panhc };
269773e3206SIke Panhc 
270773e3206SIke Panhc static int debugfs_cfg_show(struct seq_file *s, void *data)
271773e3206SIke Panhc {
272331e0ea2SZhang Rui 	struct ideapad_private *priv = s->private;
273331e0ea2SZhang Rui 
274331e0ea2SZhang Rui 	if (!priv) {
275773e3206SIke Panhc 		seq_printf(s, "cfg: N/A\n");
276773e3206SIke Panhc 	} else {
277773e3206SIke Panhc 		seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
278331e0ea2SZhang Rui 			   priv->cfg);
279331e0ea2SZhang Rui 		if (test_bit(CFG_BT_BIT, &priv->cfg))
280773e3206SIke Panhc 			seq_printf(s, "Bluetooth ");
281331e0ea2SZhang Rui 		if (test_bit(CFG_3G_BIT, &priv->cfg))
282773e3206SIke Panhc 			seq_printf(s, "3G ");
283331e0ea2SZhang Rui 		if (test_bit(CFG_WIFI_BIT, &priv->cfg))
284773e3206SIke Panhc 			seq_printf(s, "Wireless ");
285331e0ea2SZhang Rui 		if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
286773e3206SIke Panhc 			seq_printf(s, "Camera ");
287773e3206SIke Panhc 		seq_printf(s, "\nGraphic: ");
288331e0ea2SZhang Rui 		switch ((priv->cfg)&0x700) {
289773e3206SIke Panhc 		case 0x100:
290773e3206SIke Panhc 			seq_printf(s, "Intel");
291773e3206SIke Panhc 			break;
292773e3206SIke Panhc 		case 0x200:
293773e3206SIke Panhc 			seq_printf(s, "ATI");
294773e3206SIke Panhc 			break;
295773e3206SIke Panhc 		case 0x300:
296773e3206SIke Panhc 			seq_printf(s, "Nvidia");
297773e3206SIke Panhc 			break;
298773e3206SIke Panhc 		case 0x400:
299773e3206SIke Panhc 			seq_printf(s, "Intel and ATI");
300773e3206SIke Panhc 			break;
301773e3206SIke Panhc 		case 0x500:
302773e3206SIke Panhc 			seq_printf(s, "Intel and Nvidia");
303773e3206SIke Panhc 			break;
304773e3206SIke Panhc 		}
305773e3206SIke Panhc 		seq_printf(s, "\n");
306773e3206SIke Panhc 	}
307773e3206SIke Panhc 	return 0;
308773e3206SIke Panhc }
309773e3206SIke Panhc 
310773e3206SIke Panhc static int debugfs_cfg_open(struct inode *inode, struct file *file)
311773e3206SIke Panhc {
312331e0ea2SZhang Rui 	return single_open(file, debugfs_cfg_show, inode->i_private);
313773e3206SIke Panhc }
314773e3206SIke Panhc 
315773e3206SIke Panhc static const struct file_operations debugfs_cfg_fops = {
316773e3206SIke Panhc 	.owner = THIS_MODULE,
317773e3206SIke Panhc 	.open = debugfs_cfg_open,
318773e3206SIke Panhc 	.read = seq_read,
319773e3206SIke Panhc 	.llseek = seq_lseek,
320773e3206SIke Panhc 	.release = single_release,
321773e3206SIke Panhc };
322773e3206SIke Panhc 
323b859f159SGreg Kroah-Hartman static int ideapad_debugfs_init(struct ideapad_private *priv)
324773e3206SIke Panhc {
325773e3206SIke Panhc 	struct dentry *node;
326773e3206SIke Panhc 
327773e3206SIke Panhc 	priv->debug = debugfs_create_dir("ideapad", NULL);
328773e3206SIke Panhc 	if (priv->debug == NULL) {
329773e3206SIke Panhc 		pr_err("failed to create debugfs directory");
330773e3206SIke Panhc 		goto errout;
331773e3206SIke Panhc 	}
332773e3206SIke Panhc 
333331e0ea2SZhang Rui 	node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
334773e3206SIke Panhc 				   &debugfs_cfg_fops);
335773e3206SIke Panhc 	if (!node) {
336773e3206SIke Panhc 		pr_err("failed to create cfg in debugfs");
337773e3206SIke Panhc 		goto errout;
338773e3206SIke Panhc 	}
339773e3206SIke Panhc 
340331e0ea2SZhang Rui 	node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
341773e3206SIke Panhc 				   &debugfs_status_fops);
342773e3206SIke Panhc 	if (!node) {
343a5c3892fSIke Panhc 		pr_err("failed to create status in debugfs");
344773e3206SIke Panhc 		goto errout;
345773e3206SIke Panhc 	}
346773e3206SIke Panhc 
347773e3206SIke Panhc 	return 0;
348773e3206SIke Panhc 
349773e3206SIke Panhc errout:
350773e3206SIke Panhc 	return -ENOMEM;
351773e3206SIke Panhc }
352773e3206SIke Panhc 
353773e3206SIke Panhc static void ideapad_debugfs_exit(struct ideapad_private *priv)
354773e3206SIke Panhc {
355773e3206SIke Panhc 	debugfs_remove_recursive(priv->debug);
356773e3206SIke Panhc 	priv->debug = NULL;
357773e3206SIke Panhc }
358773e3206SIke Panhc 
359773e3206SIke Panhc /*
3603371f481SIke Panhc  * sysfs
361a4b5a279SIke Panhc  */
36257ac3b05SIke Panhc static ssize_t show_ideapad_cam(struct device *dev,
36357ac3b05SIke Panhc 				struct device_attribute *attr,
36457ac3b05SIke Panhc 				char *buf)
36557ac3b05SIke Panhc {
36657ac3b05SIke Panhc 	unsigned long result;
367331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
36857ac3b05SIke Panhc 
369331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
37057ac3b05SIke Panhc 		return sprintf(buf, "-1\n");
37157ac3b05SIke Panhc 	return sprintf(buf, "%lu\n", result);
37257ac3b05SIke Panhc }
37357ac3b05SIke Panhc 
37457ac3b05SIke Panhc static ssize_t store_ideapad_cam(struct device *dev,
37557ac3b05SIke Panhc 				 struct device_attribute *attr,
37657ac3b05SIke Panhc 				 const char *buf, size_t count)
37757ac3b05SIke Panhc {
37857ac3b05SIke Panhc 	int ret, state;
379331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
38057ac3b05SIke Panhc 
38157ac3b05SIke Panhc 	if (!count)
38257ac3b05SIke Panhc 		return 0;
38357ac3b05SIke Panhc 	if (sscanf(buf, "%i", &state) != 1)
38457ac3b05SIke Panhc 		return -EINVAL;
385331e0ea2SZhang Rui 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
38657ac3b05SIke Panhc 	if (ret < 0)
3870c7bbeb9SMaxim Mikityanskiy 		return -EIO;
38857ac3b05SIke Panhc 	return count;
38957ac3b05SIke Panhc }
39057ac3b05SIke Panhc 
39157ac3b05SIke Panhc static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
39257ac3b05SIke Panhc 
3930c7bbeb9SMaxim Mikityanskiy static ssize_t show_ideapad_fan(struct device *dev,
3940c7bbeb9SMaxim Mikityanskiy 				struct device_attribute *attr,
3950c7bbeb9SMaxim Mikityanskiy 				char *buf)
3960c7bbeb9SMaxim Mikityanskiy {
3970c7bbeb9SMaxim Mikityanskiy 	unsigned long result;
398331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
3990c7bbeb9SMaxim Mikityanskiy 
400331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
4010c7bbeb9SMaxim Mikityanskiy 		return sprintf(buf, "-1\n");
4020c7bbeb9SMaxim Mikityanskiy 	return sprintf(buf, "%lu\n", result);
4030c7bbeb9SMaxim Mikityanskiy }
4040c7bbeb9SMaxim Mikityanskiy 
4050c7bbeb9SMaxim Mikityanskiy static ssize_t store_ideapad_fan(struct device *dev,
4060c7bbeb9SMaxim Mikityanskiy 				 struct device_attribute *attr,
4070c7bbeb9SMaxim Mikityanskiy 				 const char *buf, size_t count)
4080c7bbeb9SMaxim Mikityanskiy {
4090c7bbeb9SMaxim Mikityanskiy 	int ret, state;
410331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
4110c7bbeb9SMaxim Mikityanskiy 
4120c7bbeb9SMaxim Mikityanskiy 	if (!count)
4130c7bbeb9SMaxim Mikityanskiy 		return 0;
4140c7bbeb9SMaxim Mikityanskiy 	if (sscanf(buf, "%i", &state) != 1)
4150c7bbeb9SMaxim Mikityanskiy 		return -EINVAL;
4160c7bbeb9SMaxim Mikityanskiy 	if (state < 0 || state > 4 || state == 3)
4170c7bbeb9SMaxim Mikityanskiy 		return -EINVAL;
418331e0ea2SZhang Rui 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
4190c7bbeb9SMaxim Mikityanskiy 	if (ret < 0)
4200c7bbeb9SMaxim Mikityanskiy 		return -EIO;
4210c7bbeb9SMaxim Mikityanskiy 	return count;
4220c7bbeb9SMaxim Mikityanskiy }
4230c7bbeb9SMaxim Mikityanskiy 
4240c7bbeb9SMaxim Mikityanskiy static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
4250c7bbeb9SMaxim Mikityanskiy 
42636ac0d43SRitesh Raj Sarraf static ssize_t touchpad_show(struct device *dev,
42736ac0d43SRitesh Raj Sarraf 			     struct device_attribute *attr,
42836ac0d43SRitesh Raj Sarraf 			     char *buf)
42936ac0d43SRitesh Raj Sarraf {
43036ac0d43SRitesh Raj Sarraf 	struct ideapad_private *priv = dev_get_drvdata(dev);
43136ac0d43SRitesh Raj Sarraf 	unsigned long result;
43236ac0d43SRitesh Raj Sarraf 
43336ac0d43SRitesh Raj Sarraf 	if (read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result))
43436ac0d43SRitesh Raj Sarraf 		return sprintf(buf, "-1\n");
43536ac0d43SRitesh Raj Sarraf 	return sprintf(buf, "%lu\n", result);
43636ac0d43SRitesh Raj Sarraf }
43736ac0d43SRitesh Raj Sarraf 
43836ac0d43SRitesh Raj Sarraf static ssize_t touchpad_store(struct device *dev,
43936ac0d43SRitesh Raj Sarraf 			      struct device_attribute *attr,
44036ac0d43SRitesh Raj Sarraf 			      const char *buf, size_t count)
44136ac0d43SRitesh Raj Sarraf {
44236ac0d43SRitesh Raj Sarraf 	struct ideapad_private *priv = dev_get_drvdata(dev);
44336ac0d43SRitesh Raj Sarraf 	bool state;
44436ac0d43SRitesh Raj Sarraf 	int ret;
44536ac0d43SRitesh Raj Sarraf 
44636ac0d43SRitesh Raj Sarraf 	ret = kstrtobool(buf, &state);
44736ac0d43SRitesh Raj Sarraf 	if (ret)
44836ac0d43SRitesh Raj Sarraf 		return ret;
44936ac0d43SRitesh Raj Sarraf 
45036ac0d43SRitesh Raj Sarraf 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
45136ac0d43SRitesh Raj Sarraf 	if (ret < 0)
45236ac0d43SRitesh Raj Sarraf 		return -EIO;
45336ac0d43SRitesh Raj Sarraf 	return count;
45436ac0d43SRitesh Raj Sarraf }
45536ac0d43SRitesh Raj Sarraf 
45636ac0d43SRitesh Raj Sarraf static DEVICE_ATTR_RW(touchpad);
45736ac0d43SRitesh Raj Sarraf 
4583371f481SIke Panhc static struct attribute *ideapad_attributes[] = {
4593371f481SIke Panhc 	&dev_attr_camera_power.attr,
4600c7bbeb9SMaxim Mikityanskiy 	&dev_attr_fan_mode.attr,
46136ac0d43SRitesh Raj Sarraf 	&dev_attr_touchpad.attr,
4623371f481SIke Panhc 	NULL
4633371f481SIke Panhc };
4643371f481SIke Panhc 
465587a1f16SAl Viro static umode_t ideapad_is_visible(struct kobject *kobj,
466a84511f7SIke Panhc 				 struct attribute *attr,
467a84511f7SIke Panhc 				 int idx)
468a84511f7SIke Panhc {
469a84511f7SIke Panhc 	struct device *dev = container_of(kobj, struct device, kobj);
470a84511f7SIke Panhc 	struct ideapad_private *priv = dev_get_drvdata(dev);
471a84511f7SIke Panhc 	bool supported;
472a84511f7SIke Panhc 
473a84511f7SIke Panhc 	if (attr == &dev_attr_camera_power.attr)
474a84511f7SIke Panhc 		supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
4750c7bbeb9SMaxim Mikityanskiy 	else if (attr == &dev_attr_fan_mode.attr) {
4760c7bbeb9SMaxim Mikityanskiy 		unsigned long value;
477331e0ea2SZhang Rui 		supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
478331e0ea2SZhang Rui 					  &value);
4790c7bbeb9SMaxim Mikityanskiy 	} else
480a84511f7SIke Panhc 		supported = true;
481a84511f7SIke Panhc 
482a84511f7SIke Panhc 	return supported ? attr->mode : 0;
483a84511f7SIke Panhc }
484a84511f7SIke Panhc 
48549458e83SMathias Krause static const struct attribute_group ideapad_attribute_group = {
486a84511f7SIke Panhc 	.is_visible = ideapad_is_visible,
4873371f481SIke Panhc 	.attrs = ideapad_attributes
4883371f481SIke Panhc };
4893371f481SIke Panhc 
490a4b5a279SIke Panhc /*
491a4b5a279SIke Panhc  * Rfkill
492a4b5a279SIke Panhc  */
493c1f73658SIke Panhc struct ideapad_rfk_data {
494c1f73658SIke Panhc 	char *name;
495c1f73658SIke Panhc 	int cfgbit;
496c1f73658SIke Panhc 	int opcode;
497c1f73658SIke Panhc 	int type;
498c1f73658SIke Panhc };
499c1f73658SIke Panhc 
500b3d94d70SMathias Krause static const struct ideapad_rfk_data ideapad_rfk_data[] = {
5012be1dc21SIke Panhc 	{ "ideapad_wlan",    CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
5022be1dc21SIke Panhc 	{ "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
5032be1dc21SIke Panhc 	{ "ideapad_3g",        CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
504c1f73658SIke Panhc };
505c1f73658SIke Panhc 
50657ac3b05SIke Panhc static int ideapad_rfk_set(void *data, bool blocked)
50757ac3b05SIke Panhc {
508331e0ea2SZhang Rui 	struct ideapad_rfk_priv *priv = data;
5094b200b46SArnd Bergmann 	int opcode = ideapad_rfk_data[priv->dev].opcode;
51057ac3b05SIke Panhc 
5114b200b46SArnd Bergmann 	return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
51257ac3b05SIke Panhc }
51357ac3b05SIke Panhc 
51457ac3b05SIke Panhc static struct rfkill_ops ideapad_rfk_ops = {
51557ac3b05SIke Panhc 	.set_block = ideapad_rfk_set,
51657ac3b05SIke Panhc };
51757ac3b05SIke Panhc 
518923de84aSIke Panhc static void ideapad_sync_rfk_state(struct ideapad_private *priv)
51957ac3b05SIke Panhc {
520ce363c2bSHans de Goede 	unsigned long hw_blocked = 0;
52157ac3b05SIke Panhc 	int i;
52257ac3b05SIke Panhc 
523ce363c2bSHans de Goede 	if (priv->has_hw_rfkill_switch) {
524331e0ea2SZhang Rui 		if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
52557ac3b05SIke Panhc 			return;
52657ac3b05SIke Panhc 		hw_blocked = !hw_blocked;
527ce363c2bSHans de Goede 	}
52857ac3b05SIke Panhc 
529c1f73658SIke Panhc 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
53057ac3b05SIke Panhc 		if (priv->rfk[i])
53157ac3b05SIke Panhc 			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
53257ac3b05SIke Panhc }
53357ac3b05SIke Panhc 
53475a11f11SZhang Rui static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
53557ac3b05SIke Panhc {
53657ac3b05SIke Panhc 	int ret;
53757ac3b05SIke Panhc 	unsigned long sw_blocked;
53857ac3b05SIke Panhc 
539bfa97b7dSIke Panhc 	if (no_bt_rfkill &&
540bfa97b7dSIke Panhc 	    (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
541bfa97b7dSIke Panhc 		/* Force to enable bluetooth when no_bt_rfkill=1 */
542331e0ea2SZhang Rui 		write_ec_cmd(priv->adev->handle,
543bfa97b7dSIke Panhc 			     ideapad_rfk_data[dev].opcode, 1);
544bfa97b7dSIke Panhc 		return 0;
545bfa97b7dSIke Panhc 	}
546331e0ea2SZhang Rui 	priv->rfk_priv[dev].dev = dev;
547331e0ea2SZhang Rui 	priv->rfk_priv[dev].priv = priv;
548bfa97b7dSIke Panhc 
54975a11f11SZhang Rui 	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
550b5c37b79SZhang Rui 				      &priv->platform_device->dev,
55175a11f11SZhang Rui 				      ideapad_rfk_data[dev].type,
55275a11f11SZhang Rui 				      &ideapad_rfk_ops,
553331e0ea2SZhang Rui 				      &priv->rfk_priv[dev]);
55457ac3b05SIke Panhc 	if (!priv->rfk[dev])
55557ac3b05SIke Panhc 		return -ENOMEM;
55657ac3b05SIke Panhc 
557331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
55857ac3b05SIke Panhc 			 &sw_blocked)) {
55957ac3b05SIke Panhc 		rfkill_init_sw_state(priv->rfk[dev], 0);
56057ac3b05SIke Panhc 	} else {
56157ac3b05SIke Panhc 		sw_blocked = !sw_blocked;
56257ac3b05SIke Panhc 		rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
56357ac3b05SIke Panhc 	}
56457ac3b05SIke Panhc 
56557ac3b05SIke Panhc 	ret = rfkill_register(priv->rfk[dev]);
56657ac3b05SIke Panhc 	if (ret) {
56757ac3b05SIke Panhc 		rfkill_destroy(priv->rfk[dev]);
56857ac3b05SIke Panhc 		return ret;
56957ac3b05SIke Panhc 	}
57057ac3b05SIke Panhc 	return 0;
57157ac3b05SIke Panhc }
57257ac3b05SIke Panhc 
57375a11f11SZhang Rui static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
57457ac3b05SIke Panhc {
57557ac3b05SIke Panhc 	if (!priv->rfk[dev])
57657ac3b05SIke Panhc 		return;
57757ac3b05SIke Panhc 
57857ac3b05SIke Panhc 	rfkill_unregister(priv->rfk[dev]);
57957ac3b05SIke Panhc 	rfkill_destroy(priv->rfk[dev]);
58057ac3b05SIke Panhc }
58157ac3b05SIke Panhc 
58298ee6919SIke Panhc /*
58398ee6919SIke Panhc  * Platform device
58498ee6919SIke Panhc  */
585b5c37b79SZhang Rui static int ideapad_sysfs_init(struct ideapad_private *priv)
58698ee6919SIke Panhc {
587b5c37b79SZhang Rui 	return sysfs_create_group(&priv->platform_device->dev.kobj,
588c9f718d0SIke Panhc 				    &ideapad_attribute_group);
58998ee6919SIke Panhc }
59098ee6919SIke Panhc 
591b5c37b79SZhang Rui static void ideapad_sysfs_exit(struct ideapad_private *priv)
59298ee6919SIke Panhc {
5938693ae84SIke Panhc 	sysfs_remove_group(&priv->platform_device->dev.kobj,
594c9f718d0SIke Panhc 			   &ideapad_attribute_group);
59598ee6919SIke Panhc }
59698ee6919SIke Panhc 
597f63409aeSIke Panhc /*
598f63409aeSIke Panhc  * input device
599f63409aeSIke Panhc  */
600f63409aeSIke Panhc static const struct key_entry ideapad_keymap[] = {
601f43d9ec0SIke Panhc 	{ KE_KEY, 6,  { KEY_SWITCHVIDEOMODE } },
602296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 7,  { KEY_CAMERA } },
60348f67d62SAlex Hung 	{ KE_KEY, 8,  { KEY_MICMUTE } },
604296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 11, { KEY_F16 } },
605f43d9ec0SIke Panhc 	{ KE_KEY, 13, { KEY_WLAN } },
606f43d9ec0SIke Panhc 	{ KE_KEY, 16, { KEY_PROG1 } },
607f43d9ec0SIke Panhc 	{ KE_KEY, 17, { KEY_PROG2 } },
608296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 64, { KEY_PROG3 } },
609296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 65, { KEY_PROG4 } },
61007a4a4fcSMaxim Mikityanskiy 	{ KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
61107a4a4fcSMaxim Mikityanskiy 	{ KE_KEY, 67, { KEY_TOUCHPAD_ON } },
61274caab99SArnd Bergmann 	{ KE_KEY, 128, { KEY_ESC } },
61374caab99SArnd Bergmann 
614f63409aeSIke Panhc 	{ KE_END, 0 },
615f63409aeSIke Panhc };
616f63409aeSIke Panhc 
617b859f159SGreg Kroah-Hartman static int ideapad_input_init(struct ideapad_private *priv)
618f63409aeSIke Panhc {
619f63409aeSIke Panhc 	struct input_dev *inputdev;
620f63409aeSIke Panhc 	int error;
621f63409aeSIke Panhc 
622f63409aeSIke Panhc 	inputdev = input_allocate_device();
623b222cca6SJoe Perches 	if (!inputdev)
624f63409aeSIke Panhc 		return -ENOMEM;
625f63409aeSIke Panhc 
626f63409aeSIke Panhc 	inputdev->name = "Ideapad extra buttons";
627f63409aeSIke Panhc 	inputdev->phys = "ideapad/input0";
628f63409aeSIke Panhc 	inputdev->id.bustype = BUS_HOST;
6298693ae84SIke Panhc 	inputdev->dev.parent = &priv->platform_device->dev;
630f63409aeSIke Panhc 
631f63409aeSIke Panhc 	error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
632f63409aeSIke Panhc 	if (error) {
633f63409aeSIke Panhc 		pr_err("Unable to setup input device keymap\n");
634f63409aeSIke Panhc 		goto err_free_dev;
635f63409aeSIke Panhc 	}
636f63409aeSIke Panhc 
637f63409aeSIke Panhc 	error = input_register_device(inputdev);
638f63409aeSIke Panhc 	if (error) {
639f63409aeSIke Panhc 		pr_err("Unable to register input device\n");
640c973d4b5SMichał Kępień 		goto err_free_dev;
641f63409aeSIke Panhc 	}
642f63409aeSIke Panhc 
6438693ae84SIke Panhc 	priv->inputdev = inputdev;
644f63409aeSIke Panhc 	return 0;
645f63409aeSIke Panhc 
646f63409aeSIke Panhc err_free_dev:
647f63409aeSIke Panhc 	input_free_device(inputdev);
648f63409aeSIke Panhc 	return error;
649f63409aeSIke Panhc }
650f63409aeSIke Panhc 
6517451a55aSAxel Lin static void ideapad_input_exit(struct ideapad_private *priv)
652f63409aeSIke Panhc {
6538693ae84SIke Panhc 	input_unregister_device(priv->inputdev);
6548693ae84SIke Panhc 	priv->inputdev = NULL;
655f63409aeSIke Panhc }
656f63409aeSIke Panhc 
6578693ae84SIke Panhc static void ideapad_input_report(struct ideapad_private *priv,
6588693ae84SIke Panhc 				 unsigned long scancode)
659f63409aeSIke Panhc {
6608693ae84SIke Panhc 	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
661f63409aeSIke Panhc }
662f63409aeSIke Panhc 
663f43d9ec0SIke Panhc static void ideapad_input_novokey(struct ideapad_private *priv)
664f43d9ec0SIke Panhc {
665f43d9ec0SIke Panhc 	unsigned long long_pressed;
666f43d9ec0SIke Panhc 
667331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
668f43d9ec0SIke Panhc 		return;
669f43d9ec0SIke Panhc 	if (long_pressed)
670f43d9ec0SIke Panhc 		ideapad_input_report(priv, 17);
671f43d9ec0SIke Panhc 	else
672f43d9ec0SIke Panhc 		ideapad_input_report(priv, 16);
673f43d9ec0SIke Panhc }
674f43d9ec0SIke Panhc 
675296f9fe0SMaxim Mikityanskiy static void ideapad_check_special_buttons(struct ideapad_private *priv)
676296f9fe0SMaxim Mikityanskiy {
677296f9fe0SMaxim Mikityanskiy 	unsigned long bit, value;
678296f9fe0SMaxim Mikityanskiy 
679331e0ea2SZhang Rui 	read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
680296f9fe0SMaxim Mikityanskiy 
681296f9fe0SMaxim Mikityanskiy 	for (bit = 0; bit < 16; bit++) {
682296f9fe0SMaxim Mikityanskiy 		if (test_bit(bit, &value)) {
683296f9fe0SMaxim Mikityanskiy 			switch (bit) {
684a1ec56edSMaxim Mikityanskiy 			case 0:	/* Z580 */
685a1ec56edSMaxim Mikityanskiy 			case 6:	/* Z570 */
686296f9fe0SMaxim Mikityanskiy 				/* Thermal Management button */
687296f9fe0SMaxim Mikityanskiy 				ideapad_input_report(priv, 65);
688296f9fe0SMaxim Mikityanskiy 				break;
689296f9fe0SMaxim Mikityanskiy 			case 1:
690296f9fe0SMaxim Mikityanskiy 				/* OneKey Theater button */
691296f9fe0SMaxim Mikityanskiy 				ideapad_input_report(priv, 64);
692296f9fe0SMaxim Mikityanskiy 				break;
693a1ec56edSMaxim Mikityanskiy 			default:
694a1ec56edSMaxim Mikityanskiy 				pr_info("Unknown special button: %lu\n", bit);
695a1ec56edSMaxim Mikityanskiy 				break;
696296f9fe0SMaxim Mikityanskiy 			}
697296f9fe0SMaxim Mikityanskiy 		}
698296f9fe0SMaxim Mikityanskiy 	}
699296f9fe0SMaxim Mikityanskiy }
700296f9fe0SMaxim Mikityanskiy 
701a4b5a279SIke Panhc /*
702a4ecbb8aSIke Panhc  * backlight
703a4ecbb8aSIke Panhc  */
704a4ecbb8aSIke Panhc static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
705a4ecbb8aSIke Panhc {
706331e0ea2SZhang Rui 	struct ideapad_private *priv = bl_get_data(blightdev);
707a4ecbb8aSIke Panhc 	unsigned long now;
708a4ecbb8aSIke Panhc 
709331e0ea2SZhang Rui 	if (!priv)
710331e0ea2SZhang Rui 		return -EINVAL;
711331e0ea2SZhang Rui 
712331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
713a4ecbb8aSIke Panhc 		return -EIO;
714a4ecbb8aSIke Panhc 	return now;
715a4ecbb8aSIke Panhc }
716a4ecbb8aSIke Panhc 
717a4ecbb8aSIke Panhc static int ideapad_backlight_update_status(struct backlight_device *blightdev)
718a4ecbb8aSIke Panhc {
719331e0ea2SZhang Rui 	struct ideapad_private *priv = bl_get_data(blightdev);
720331e0ea2SZhang Rui 
721331e0ea2SZhang Rui 	if (!priv)
722331e0ea2SZhang Rui 		return -EINVAL;
723331e0ea2SZhang Rui 
724331e0ea2SZhang Rui 	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
7252be1dc21SIke Panhc 			 blightdev->props.brightness))
726a4ecbb8aSIke Panhc 		return -EIO;
727331e0ea2SZhang Rui 	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
728a4ecbb8aSIke Panhc 			 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
729a4ecbb8aSIke Panhc 		return -EIO;
730a4ecbb8aSIke Panhc 
731a4ecbb8aSIke Panhc 	return 0;
732a4ecbb8aSIke Panhc }
733a4ecbb8aSIke Panhc 
734a4ecbb8aSIke Panhc static const struct backlight_ops ideapad_backlight_ops = {
735a4ecbb8aSIke Panhc 	.get_brightness = ideapad_backlight_get_brightness,
736a4ecbb8aSIke Panhc 	.update_status = ideapad_backlight_update_status,
737a4ecbb8aSIke Panhc };
738a4ecbb8aSIke Panhc 
739a4ecbb8aSIke Panhc static int ideapad_backlight_init(struct ideapad_private *priv)
740a4ecbb8aSIke Panhc {
741a4ecbb8aSIke Panhc 	struct backlight_device *blightdev;
742a4ecbb8aSIke Panhc 	struct backlight_properties props;
743a4ecbb8aSIke Panhc 	unsigned long max, now, power;
744a4ecbb8aSIke Panhc 
745331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
746a4ecbb8aSIke Panhc 		return -EIO;
747331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
748a4ecbb8aSIke Panhc 		return -EIO;
749331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
750a4ecbb8aSIke Panhc 		return -EIO;
751a4ecbb8aSIke Panhc 
752a4ecbb8aSIke Panhc 	memset(&props, 0, sizeof(struct backlight_properties));
753a4ecbb8aSIke Panhc 	props.max_brightness = max;
754a4ecbb8aSIke Panhc 	props.type = BACKLIGHT_PLATFORM;
755a4ecbb8aSIke Panhc 	blightdev = backlight_device_register("ideapad",
756a4ecbb8aSIke Panhc 					      &priv->platform_device->dev,
757a4ecbb8aSIke Panhc 					      priv,
758a4ecbb8aSIke Panhc 					      &ideapad_backlight_ops,
759a4ecbb8aSIke Panhc 					      &props);
760a4ecbb8aSIke Panhc 	if (IS_ERR(blightdev)) {
761a4ecbb8aSIke Panhc 		pr_err("Could not register backlight device\n");
762a4ecbb8aSIke Panhc 		return PTR_ERR(blightdev);
763a4ecbb8aSIke Panhc 	}
764a4ecbb8aSIke Panhc 
765a4ecbb8aSIke Panhc 	priv->blightdev = blightdev;
766a4ecbb8aSIke Panhc 	blightdev->props.brightness = now;
767a4ecbb8aSIke Panhc 	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
768a4ecbb8aSIke Panhc 	backlight_update_status(blightdev);
769a4ecbb8aSIke Panhc 
770a4ecbb8aSIke Panhc 	return 0;
771a4ecbb8aSIke Panhc }
772a4ecbb8aSIke Panhc 
773a4ecbb8aSIke Panhc static void ideapad_backlight_exit(struct ideapad_private *priv)
774a4ecbb8aSIke Panhc {
775a4ecbb8aSIke Panhc 	backlight_device_unregister(priv->blightdev);
776a4ecbb8aSIke Panhc 	priv->blightdev = NULL;
777a4ecbb8aSIke Panhc }
778a4ecbb8aSIke Panhc 
779a4ecbb8aSIke Panhc static void ideapad_backlight_notify_power(struct ideapad_private *priv)
780a4ecbb8aSIke Panhc {
781a4ecbb8aSIke Panhc 	unsigned long power;
782a4ecbb8aSIke Panhc 	struct backlight_device *blightdev = priv->blightdev;
783a4ecbb8aSIke Panhc 
784d4afc775SRene Bollford 	if (!blightdev)
785d4afc775SRene Bollford 		return;
786331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
787a4ecbb8aSIke Panhc 		return;
788a4ecbb8aSIke Panhc 	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
789a4ecbb8aSIke Panhc }
790a4ecbb8aSIke Panhc 
791a4ecbb8aSIke Panhc static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
792a4ecbb8aSIke Panhc {
793a4ecbb8aSIke Panhc 	unsigned long now;
794a4ecbb8aSIke Panhc 
795a4ecbb8aSIke Panhc 	/* if we control brightness via acpi video driver */
796a4ecbb8aSIke Panhc 	if (priv->blightdev == NULL) {
797331e0ea2SZhang Rui 		read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
798a4ecbb8aSIke Panhc 		return;
799a4ecbb8aSIke Panhc 	}
800a4ecbb8aSIke Panhc 
801a4ecbb8aSIke Panhc 	backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
802a4ecbb8aSIke Panhc }
803a4ecbb8aSIke Panhc 
804a4ecbb8aSIke Panhc /*
805a4b5a279SIke Panhc  * module init/exit
806a4b5a279SIke Panhc  */
80775a11f11SZhang Rui static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
80807a4a4fcSMaxim Mikityanskiy {
80907a4a4fcSMaxim Mikityanskiy 	unsigned long value;
81007a4a4fcSMaxim Mikityanskiy 
81107a4a4fcSMaxim Mikityanskiy 	/* Without reading from EC touchpad LED doesn't switch state */
81275a11f11SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
81307a4a4fcSMaxim Mikityanskiy 		/* Some IdeaPads don't really turn off touchpad - they only
81407a4a4fcSMaxim Mikityanskiy 		 * switch the LED state. We (de)activate KBC AUX port to turn
81507a4a4fcSMaxim Mikityanskiy 		 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
81607a4a4fcSMaxim Mikityanskiy 		 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
81707a4a4fcSMaxim Mikityanskiy 		unsigned char param;
81807a4a4fcSMaxim Mikityanskiy 		i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
81907a4a4fcSMaxim Mikityanskiy 			      I8042_CMD_AUX_DISABLE);
82007a4a4fcSMaxim Mikityanskiy 		ideapad_input_report(priv, value ? 67 : 66);
82107a4a4fcSMaxim Mikityanskiy 	}
82207a4a4fcSMaxim Mikityanskiy }
82307a4a4fcSMaxim Mikityanskiy 
824b5c37b79SZhang Rui static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
82557ac3b05SIke Panhc {
826b5c37b79SZhang Rui 	struct ideapad_private *priv = data;
82757ac3b05SIke Panhc 	unsigned long vpc1, vpc2, vpc_bit;
82857ac3b05SIke Panhc 
8292be1dc21SIke Panhc 	if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
83057ac3b05SIke Panhc 		return;
8312be1dc21SIke Panhc 	if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
83257ac3b05SIke Panhc 		return;
83357ac3b05SIke Panhc 
83457ac3b05SIke Panhc 	vpc1 = (vpc2 << 8) | vpc1;
83557ac3b05SIke Panhc 	for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
83657ac3b05SIke Panhc 		if (test_bit(vpc_bit, &vpc1)) {
837a4ecbb8aSIke Panhc 			switch (vpc_bit) {
838a4ecbb8aSIke Panhc 			case 9:
839923de84aSIke Panhc 				ideapad_sync_rfk_state(priv);
840a4ecbb8aSIke Panhc 				break;
84120a769c1SIke Panhc 			case 13:
842296f9fe0SMaxim Mikityanskiy 			case 11:
84348f67d62SAlex Hung 			case 8:
844296f9fe0SMaxim Mikityanskiy 			case 7:
84520a769c1SIke Panhc 			case 6:
846cfee5d63SZach Ploskey 			case 1:
84720a769c1SIke Panhc 				ideapad_input_report(priv, vpc_bit);
84820a769c1SIke Panhc 				break;
84907a4a4fcSMaxim Mikityanskiy 			case 5:
85075a11f11SZhang Rui 				ideapad_sync_touchpad_state(priv);
85107a4a4fcSMaxim Mikityanskiy 				break;
852a4ecbb8aSIke Panhc 			case 4:
853a4ecbb8aSIke Panhc 				ideapad_backlight_notify_brightness(priv);
854a4ecbb8aSIke Panhc 				break;
855f43d9ec0SIke Panhc 			case 3:
856f43d9ec0SIke Panhc 				ideapad_input_novokey(priv);
857f43d9ec0SIke Panhc 				break;
858a4ecbb8aSIke Panhc 			case 2:
859a4ecbb8aSIke Panhc 				ideapad_backlight_notify_power(priv);
860a4ecbb8aSIke Panhc 				break;
861296f9fe0SMaxim Mikityanskiy 			case 0:
862296f9fe0SMaxim Mikityanskiy 				ideapad_check_special_buttons(priv);
863296f9fe0SMaxim Mikityanskiy 				break;
864a4ecbb8aSIke Panhc 			default:
86520a769c1SIke Panhc 				pr_info("Unknown event: %lu\n", vpc_bit);
86657ac3b05SIke Panhc 			}
86757ac3b05SIke Panhc 		}
86857ac3b05SIke Panhc 	}
869a4ecbb8aSIke Panhc }
87057ac3b05SIke Panhc 
87174caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
87274caab99SArnd Bergmann static void ideapad_wmi_notify(u32 value, void *context)
87374caab99SArnd Bergmann {
87474caab99SArnd Bergmann 	switch (value) {
87574caab99SArnd Bergmann 	case 128:
87674caab99SArnd Bergmann 		ideapad_input_report(context, value);
87774caab99SArnd Bergmann 		break;
87874caab99SArnd Bergmann 	default:
87974caab99SArnd Bergmann 		pr_info("Unknown WMI event %u\n", value);
88074caab99SArnd Bergmann 	}
88174caab99SArnd Bergmann }
88274caab99SArnd Bergmann #endif
88374caab99SArnd Bergmann 
884ce363c2bSHans de Goede /*
885ce363c2bSHans de Goede  * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
886ce363c2bSHans de Goede  * always results in 0 on these models, causing ideapad_laptop to wrongly
887ce363c2bSHans de Goede  * report all radios as hardware-blocked.
888ce363c2bSHans de Goede  */
889b3d94d70SMathias Krause static const struct dmi_system_id no_hw_rfkill_list[] = {
89085093f79SHans de Goede 	{
8919b071a43SPhilippe Coval 		.ident = "Lenovo G40-30",
8929b071a43SPhilippe Coval 		.matches = {
8939b071a43SPhilippe Coval 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
8949b071a43SPhilippe Coval 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G40-30"),
8959b071a43SPhilippe Coval 		},
8969b071a43SPhilippe Coval 	},
8979b071a43SPhilippe Coval 	{
8984fa9dabcSDmitry Tunin 		.ident = "Lenovo G50-30",
8994fa9dabcSDmitry Tunin 		.matches = {
9004fa9dabcSDmitry Tunin 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9014fa9dabcSDmitry Tunin 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G50-30"),
9024fa9dabcSDmitry Tunin 		},
9034fa9dabcSDmitry Tunin 	},
9044fa9dabcSDmitry Tunin 	{
905ccc7179fSAndy Shevchenko 		.ident = "Lenovo V310-15ISK",
906ccc7179fSAndy Shevchenko 		.matches = {
907ccc7179fSAndy Shevchenko 		        DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
908ccc7179fSAndy Shevchenko 		        DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15ISK"),
909ccc7179fSAndy Shevchenko 		},
910ccc7179fSAndy Shevchenko 	},
911ccc7179fSAndy Shevchenko 	{
9121f3bc53dSSven Rebhan 		.ident = "Lenovo ideapad 310-15IKB",
9131f3bc53dSSven Rebhan 		.matches = {
9141f3bc53dSSven Rebhan 		        DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9151f3bc53dSSven Rebhan 		        DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IKB"),
9161f3bc53dSSven Rebhan 		},
9171f3bc53dSSven Rebhan 	},
9181f3bc53dSSven Rebhan 	{
919e2970468Svelemas 		.ident = "Lenovo ideapad Y700-15ACZ",
920e2970468Svelemas 		.matches = {
921e2970468Svelemas 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
922e2970468Svelemas 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ACZ"),
923e2970468Svelemas 		},
924e2970468Svelemas 	},
925e2970468Svelemas 	{
9264db9675dSJohn Dahlstrom 		.ident = "Lenovo ideapad Y700-15ISK",
9274db9675dSJohn Dahlstrom 		.matches = {
9284db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9294db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ISK"),
9304db9675dSJohn Dahlstrom 		},
9314db9675dSJohn Dahlstrom 	},
9324db9675dSJohn Dahlstrom 	{
9334db9675dSJohn Dahlstrom 		.ident = "Lenovo ideapad Y700 Touch-15ISK",
9344db9675dSJohn Dahlstrom 		.matches = {
9354db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9364db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700 Touch-15ISK"),
9374db9675dSJohn Dahlstrom 		},
9384db9675dSJohn Dahlstrom 	},
9394db9675dSJohn Dahlstrom 	{
940edde316aSJosh Boyer 		.ident = "Lenovo ideapad Y700-17ISK",
941edde316aSJosh Boyer 		.matches = {
942edde316aSJosh Boyer 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
943edde316aSJosh Boyer 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-17ISK"),
944edde316aSJosh Boyer 		},
945edde316aSJosh Boyer 	},
946edde316aSJosh Boyer 	{
947ce363c2bSHans de Goede 		.ident = "Lenovo Yoga 2 11 / 13 / Pro",
94885093f79SHans de Goede 		.matches = {
94985093f79SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
950ce363c2bSHans de Goede 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
95185093f79SHans de Goede 		},
95285093f79SHans de Goede 	},
953725c7f61SStephan Mueller 	{
9546d212b8aSSebastian Krzyszkowiak 		.ident = "Lenovo Yoga 2 11 / 13 / Pro",
9556d212b8aSSebastian Krzyszkowiak 		.matches = {
9566d212b8aSSebastian Krzyszkowiak 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9576d212b8aSSebastian Krzyszkowiak 			DMI_MATCH(DMI_BOARD_NAME, "Yoga2"),
9586d212b8aSSebastian Krzyszkowiak 		},
9596d212b8aSSebastian Krzyszkowiak 	},
9606d212b8aSSebastian Krzyszkowiak 	{
961c789fffcSArnd Bergmann 		.ident = "Lenovo Yoga 3 1170 / 1470",
962c789fffcSArnd Bergmann 		.matches = {
963c789fffcSArnd Bergmann 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
964c789fffcSArnd Bergmann 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 3"),
965c789fffcSArnd Bergmann 		},
966c789fffcSArnd Bergmann 	},
967c789fffcSArnd Bergmann 	{
968725c7f61SStephan Mueller 		.ident = "Lenovo Yoga 3 Pro 1370",
969725c7f61SStephan Mueller 		.matches = {
970725c7f61SStephan Mueller 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
971c789fffcSArnd Bergmann 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
972725c7f61SStephan Mueller 		},
973725c7f61SStephan Mueller 	},
974f71c882dSHans de Goede 	{
9756b31de3eSJosh Boyer 		.ident = "Lenovo Yoga 700",
9766b31de3eSJosh Boyer 		.matches = {
9776b31de3eSJosh Boyer 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9786b31de3eSJosh Boyer 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 700"),
9796b31de3eSJosh Boyer 		},
9806b31de3eSJosh Boyer 	},
9816b31de3eSJosh Boyer 	{
982f71c882dSHans de Goede 		.ident = "Lenovo Yoga 900",
983f71c882dSHans de Goede 		.matches = {
984f71c882dSHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
985f71c882dSHans de Goede 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 900"),
986f71c882dSHans de Goede 		},
987f71c882dSHans de Goede 	},
98840c30bbfSBrian Masney 	{
989446647d4SMika Westerberg 		.ident = "Lenovo Yoga 900",
990446647d4SMika Westerberg 		.matches = {
991446647d4SMika Westerberg 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
992446647d4SMika Westerberg 			DMI_MATCH(DMI_BOARD_NAME, "VIUU4"),
993446647d4SMika Westerberg 		},
994446647d4SMika Westerberg 	},
995446647d4SMika Westerberg 	{
99640c30bbfSBrian Masney 		.ident = "Lenovo YOGA 910-13IKB",
99740c30bbfSBrian Masney 		.matches = {
99840c30bbfSBrian Masney 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
99940c30bbfSBrian Masney 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 910-13IKB"),
100040c30bbfSBrian Masney 		},
100140c30bbfSBrian Masney 	},
100285093f79SHans de Goede 	{}
100385093f79SHans de Goede };
100485093f79SHans de Goede 
1005b5c37b79SZhang Rui static int ideapad_acpi_add(struct platform_device *pdev)
1006b5c37b79SZhang Rui {
1007b5c37b79SZhang Rui 	int ret, i;
1008b5c37b79SZhang Rui 	int cfg;
1009b5c37b79SZhang Rui 	struct ideapad_private *priv;
1010b5c37b79SZhang Rui 	struct acpi_device *adev;
1011b5c37b79SZhang Rui 
1012b5c37b79SZhang Rui 	ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
1013b5c37b79SZhang Rui 	if (ret)
1014b5c37b79SZhang Rui 		return -ENODEV;
1015b5c37b79SZhang Rui 
1016b5c37b79SZhang Rui 	if (read_method_int(adev->handle, "_CFG", &cfg))
1017b5c37b79SZhang Rui 		return -ENODEV;
1018b5c37b79SZhang Rui 
1019b3facd7bSHimangi Saraogi 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1020b5c37b79SZhang Rui 	if (!priv)
1021b5c37b79SZhang Rui 		return -ENOMEM;
1022b5c37b79SZhang Rui 
1023b5c37b79SZhang Rui 	dev_set_drvdata(&pdev->dev, priv);
1024b5c37b79SZhang Rui 	priv->cfg = cfg;
1025b5c37b79SZhang Rui 	priv->adev = adev;
1026b5c37b79SZhang Rui 	priv->platform_device = pdev;
1027ce363c2bSHans de Goede 	priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
1028b5c37b79SZhang Rui 
1029b5c37b79SZhang Rui 	ret = ideapad_sysfs_init(priv);
1030b5c37b79SZhang Rui 	if (ret)
1031b3facd7bSHimangi Saraogi 		return ret;
1032b5c37b79SZhang Rui 
1033b5c37b79SZhang Rui 	ret = ideapad_debugfs_init(priv);
1034b5c37b79SZhang Rui 	if (ret)
1035b5c37b79SZhang Rui 		goto debugfs_failed;
1036b5c37b79SZhang Rui 
1037b5c37b79SZhang Rui 	ret = ideapad_input_init(priv);
1038b5c37b79SZhang Rui 	if (ret)
1039b5c37b79SZhang Rui 		goto input_failed;
1040b5c37b79SZhang Rui 
1041ce363c2bSHans de Goede 	/*
1042ce363c2bSHans de Goede 	 * On some models without a hw-switch (the yoga 2 13 at least)
1043ce363c2bSHans de Goede 	 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1044ce363c2bSHans de Goede 	 */
1045ce363c2bSHans de Goede 	if (!priv->has_hw_rfkill_switch)
1046ce363c2bSHans de Goede 		write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1047ce363c2bSHans de Goede 
104885093f79SHans de Goede 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1049b5c37b79SZhang Rui 		if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1050b5c37b79SZhang Rui 			ideapad_register_rfkill(priv, i);
1051ce363c2bSHans de Goede 
1052b5c37b79SZhang Rui 	ideapad_sync_rfk_state(priv);
1053b5c37b79SZhang Rui 	ideapad_sync_touchpad_state(priv);
1054b5c37b79SZhang Rui 
105526bff5f0SHans de Goede 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1056b5c37b79SZhang Rui 		ret = ideapad_backlight_init(priv);
1057b5c37b79SZhang Rui 		if (ret && ret != -ENODEV)
1058b5c37b79SZhang Rui 			goto backlight_failed;
1059b5c37b79SZhang Rui 	}
1060b5c37b79SZhang Rui 	ret = acpi_install_notify_handler(adev->handle,
1061b5c37b79SZhang Rui 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
1062b5c37b79SZhang Rui 	if (ret)
1063b5c37b79SZhang Rui 		goto notification_failed;
10642d98e0b9SArnd Bergmann 
106574caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
10662d98e0b9SArnd Bergmann 	for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
10672d98e0b9SArnd Bergmann 		ret = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
10682d98e0b9SArnd Bergmann 						 ideapad_wmi_notify, priv);
10692d98e0b9SArnd Bergmann 		if (ret == AE_OK) {
10702d98e0b9SArnd Bergmann 			priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
10712d98e0b9SArnd Bergmann 			break;
10722d98e0b9SArnd Bergmann 		}
10732d98e0b9SArnd Bergmann 	}
107474caab99SArnd Bergmann 	if (ret != AE_OK && ret != AE_NOT_EXIST)
107574caab99SArnd Bergmann 		goto notification_failed_wmi;
107674caab99SArnd Bergmann #endif
1077b5c37b79SZhang Rui 
1078b5c37b79SZhang Rui 	return 0;
107974caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
108074caab99SArnd Bergmann notification_failed_wmi:
108174caab99SArnd Bergmann 	acpi_remove_notify_handler(priv->adev->handle,
108274caab99SArnd Bergmann 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
108374caab99SArnd Bergmann #endif
1084b5c37b79SZhang Rui notification_failed:
1085b5c37b79SZhang Rui 	ideapad_backlight_exit(priv);
1086b5c37b79SZhang Rui backlight_failed:
1087b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1088b5c37b79SZhang Rui 		ideapad_unregister_rfkill(priv, i);
1089b5c37b79SZhang Rui 	ideapad_input_exit(priv);
1090b5c37b79SZhang Rui input_failed:
1091b5c37b79SZhang Rui 	ideapad_debugfs_exit(priv);
1092b5c37b79SZhang Rui debugfs_failed:
1093b5c37b79SZhang Rui 	ideapad_sysfs_exit(priv);
1094b5c37b79SZhang Rui 	return ret;
1095b5c37b79SZhang Rui }
1096b5c37b79SZhang Rui 
1097b5c37b79SZhang Rui static int ideapad_acpi_remove(struct platform_device *pdev)
1098b5c37b79SZhang Rui {
1099b5c37b79SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1100b5c37b79SZhang Rui 	int i;
1101b5c37b79SZhang Rui 
110274caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
11032d98e0b9SArnd Bergmann 	if (priv->fnesc_guid)
11042d98e0b9SArnd Bergmann 		wmi_remove_notify_handler(priv->fnesc_guid);
110574caab99SArnd Bergmann #endif
1106b5c37b79SZhang Rui 	acpi_remove_notify_handler(priv->adev->handle,
1107b5c37b79SZhang Rui 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1108b5c37b79SZhang Rui 	ideapad_backlight_exit(priv);
1109b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1110b5c37b79SZhang Rui 		ideapad_unregister_rfkill(priv, i);
1111b5c37b79SZhang Rui 	ideapad_input_exit(priv);
1112b5c37b79SZhang Rui 	ideapad_debugfs_exit(priv);
1113b5c37b79SZhang Rui 	ideapad_sysfs_exit(priv);
1114b5c37b79SZhang Rui 	dev_set_drvdata(&pdev->dev, NULL);
1115b5c37b79SZhang Rui 
1116b5c37b79SZhang Rui 	return 0;
1117b5c37b79SZhang Rui }
1118b5c37b79SZhang Rui 
111911fa8da5SZhang Rui #ifdef CONFIG_PM_SLEEP
112007a4a4fcSMaxim Mikityanskiy static int ideapad_acpi_resume(struct device *device)
112107a4a4fcSMaxim Mikityanskiy {
112275a11f11SZhang Rui 	struct ideapad_private *priv;
112375a11f11SZhang Rui 
112475a11f11SZhang Rui 	if (!device)
112575a11f11SZhang Rui 		return -EINVAL;
112675a11f11SZhang Rui 	priv = dev_get_drvdata(device);
112775a11f11SZhang Rui 
112875a11f11SZhang Rui 	ideapad_sync_rfk_state(priv);
112975a11f11SZhang Rui 	ideapad_sync_touchpad_state(priv);
113007a4a4fcSMaxim Mikityanskiy 	return 0;
113107a4a4fcSMaxim Mikityanskiy }
1132b5c37b79SZhang Rui #endif
113307a4a4fcSMaxim Mikityanskiy static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
113407a4a4fcSMaxim Mikityanskiy 
1135b5c37b79SZhang Rui static const struct acpi_device_id ideapad_device_ids[] = {
1136b5c37b79SZhang Rui 	{ "VPC2004", 0},
1137b5c37b79SZhang Rui 	{ "", 0},
113857ac3b05SIke Panhc };
1139b5c37b79SZhang Rui MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1140b5c37b79SZhang Rui 
1141b5c37b79SZhang Rui static struct platform_driver ideapad_acpi_driver = {
1142b5c37b79SZhang Rui 	.probe = ideapad_acpi_add,
1143b5c37b79SZhang Rui 	.remove = ideapad_acpi_remove,
1144b5c37b79SZhang Rui 	.driver = {
1145b5c37b79SZhang Rui 		.name   = "ideapad_acpi",
1146b5c37b79SZhang Rui 		.pm     = &ideapad_pm,
1147b5c37b79SZhang Rui 		.acpi_match_table = ACPI_PTR(ideapad_device_ids),
1148b5c37b79SZhang Rui 	},
1149b5c37b79SZhang Rui };
1150b5c37b79SZhang Rui 
1151b5c37b79SZhang Rui module_platform_driver(ideapad_acpi_driver);
115257ac3b05SIke Panhc 
115357ac3b05SIke Panhc MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
115457ac3b05SIke Panhc MODULE_DESCRIPTION("IdeaPad ACPI Extras");
115557ac3b05SIke Panhc MODULE_LICENSE("GPL");
1156