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 
43846936fd6SArnd Bergmann /* Switch to RO for now: It might be revisited in the future */
43946936fd6SArnd Bergmann static ssize_t __maybe_unused touchpad_store(struct device *dev,
44036ac0d43SRitesh Raj Sarraf 					     struct device_attribute *attr,
44136ac0d43SRitesh Raj Sarraf 					     const char *buf, size_t count)
44236ac0d43SRitesh Raj Sarraf {
44336ac0d43SRitesh Raj Sarraf 	struct ideapad_private *priv = dev_get_drvdata(dev);
44436ac0d43SRitesh Raj Sarraf 	bool state;
44536ac0d43SRitesh Raj Sarraf 	int ret;
44636ac0d43SRitesh Raj Sarraf 
44736ac0d43SRitesh Raj Sarraf 	ret = kstrtobool(buf, &state);
44836ac0d43SRitesh Raj Sarraf 	if (ret)
44936ac0d43SRitesh Raj Sarraf 		return ret;
45036ac0d43SRitesh Raj Sarraf 
45136ac0d43SRitesh Raj Sarraf 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
45236ac0d43SRitesh Raj Sarraf 	if (ret < 0)
45336ac0d43SRitesh Raj Sarraf 		return -EIO;
45436ac0d43SRitesh Raj Sarraf 	return count;
45536ac0d43SRitesh Raj Sarraf }
45636ac0d43SRitesh Raj Sarraf 
4577f363145SAndy Shevchenko static DEVICE_ATTR_RO(touchpad);
45836ac0d43SRitesh Raj Sarraf 
4593371f481SIke Panhc static struct attribute *ideapad_attributes[] = {
4603371f481SIke Panhc 	&dev_attr_camera_power.attr,
4610c7bbeb9SMaxim Mikityanskiy 	&dev_attr_fan_mode.attr,
46236ac0d43SRitesh Raj Sarraf 	&dev_attr_touchpad.attr,
4633371f481SIke Panhc 	NULL
4643371f481SIke Panhc };
4653371f481SIke Panhc 
466587a1f16SAl Viro static umode_t ideapad_is_visible(struct kobject *kobj,
467a84511f7SIke Panhc 				 struct attribute *attr,
468a84511f7SIke Panhc 				 int idx)
469a84511f7SIke Panhc {
470a84511f7SIke Panhc 	struct device *dev = container_of(kobj, struct device, kobj);
471a84511f7SIke Panhc 	struct ideapad_private *priv = dev_get_drvdata(dev);
472a84511f7SIke Panhc 	bool supported;
473a84511f7SIke Panhc 
474a84511f7SIke Panhc 	if (attr == &dev_attr_camera_power.attr)
475a84511f7SIke Panhc 		supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
4760c7bbeb9SMaxim Mikityanskiy 	else if (attr == &dev_attr_fan_mode.attr) {
4770c7bbeb9SMaxim Mikityanskiy 		unsigned long value;
478331e0ea2SZhang Rui 		supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
479331e0ea2SZhang Rui 					  &value);
4800c7bbeb9SMaxim Mikityanskiy 	} else
481a84511f7SIke Panhc 		supported = true;
482a84511f7SIke Panhc 
483a84511f7SIke Panhc 	return supported ? attr->mode : 0;
484a84511f7SIke Panhc }
485a84511f7SIke Panhc 
48649458e83SMathias Krause static const struct attribute_group ideapad_attribute_group = {
487a84511f7SIke Panhc 	.is_visible = ideapad_is_visible,
4883371f481SIke Panhc 	.attrs = ideapad_attributes
4893371f481SIke Panhc };
4903371f481SIke Panhc 
491a4b5a279SIke Panhc /*
492a4b5a279SIke Panhc  * Rfkill
493a4b5a279SIke Panhc  */
494c1f73658SIke Panhc struct ideapad_rfk_data {
495c1f73658SIke Panhc 	char *name;
496c1f73658SIke Panhc 	int cfgbit;
497c1f73658SIke Panhc 	int opcode;
498c1f73658SIke Panhc 	int type;
499c1f73658SIke Panhc };
500c1f73658SIke Panhc 
501b3d94d70SMathias Krause static const struct ideapad_rfk_data ideapad_rfk_data[] = {
5022be1dc21SIke Panhc 	{ "ideapad_wlan",    CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
5032be1dc21SIke Panhc 	{ "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
5042be1dc21SIke Panhc 	{ "ideapad_3g",        CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
505c1f73658SIke Panhc };
506c1f73658SIke Panhc 
50757ac3b05SIke Panhc static int ideapad_rfk_set(void *data, bool blocked)
50857ac3b05SIke Panhc {
509331e0ea2SZhang Rui 	struct ideapad_rfk_priv *priv = data;
5104b200b46SArnd Bergmann 	int opcode = ideapad_rfk_data[priv->dev].opcode;
51157ac3b05SIke Panhc 
5124b200b46SArnd Bergmann 	return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
51357ac3b05SIke Panhc }
51457ac3b05SIke Panhc 
5153d59dfcdSBhumika Goyal static const struct rfkill_ops ideapad_rfk_ops = {
51657ac3b05SIke Panhc 	.set_block = ideapad_rfk_set,
51757ac3b05SIke Panhc };
51857ac3b05SIke Panhc 
519923de84aSIke Panhc static void ideapad_sync_rfk_state(struct ideapad_private *priv)
52057ac3b05SIke Panhc {
521ce363c2bSHans de Goede 	unsigned long hw_blocked = 0;
52257ac3b05SIke Panhc 	int i;
52357ac3b05SIke Panhc 
524ce363c2bSHans de Goede 	if (priv->has_hw_rfkill_switch) {
525331e0ea2SZhang Rui 		if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
52657ac3b05SIke Panhc 			return;
52757ac3b05SIke Panhc 		hw_blocked = !hw_blocked;
528ce363c2bSHans de Goede 	}
52957ac3b05SIke Panhc 
530c1f73658SIke Panhc 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
53157ac3b05SIke Panhc 		if (priv->rfk[i])
53257ac3b05SIke Panhc 			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
53357ac3b05SIke Panhc }
53457ac3b05SIke Panhc 
53575a11f11SZhang Rui static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
53657ac3b05SIke Panhc {
53757ac3b05SIke Panhc 	int ret;
53857ac3b05SIke Panhc 	unsigned long sw_blocked;
53957ac3b05SIke Panhc 
540bfa97b7dSIke Panhc 	if (no_bt_rfkill &&
541bfa97b7dSIke Panhc 	    (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
542bfa97b7dSIke Panhc 		/* Force to enable bluetooth when no_bt_rfkill=1 */
543331e0ea2SZhang Rui 		write_ec_cmd(priv->adev->handle,
544bfa97b7dSIke Panhc 			     ideapad_rfk_data[dev].opcode, 1);
545bfa97b7dSIke Panhc 		return 0;
546bfa97b7dSIke Panhc 	}
547331e0ea2SZhang Rui 	priv->rfk_priv[dev].dev = dev;
548331e0ea2SZhang Rui 	priv->rfk_priv[dev].priv = priv;
549bfa97b7dSIke Panhc 
55075a11f11SZhang Rui 	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
551b5c37b79SZhang Rui 				      &priv->platform_device->dev,
55275a11f11SZhang Rui 				      ideapad_rfk_data[dev].type,
55375a11f11SZhang Rui 				      &ideapad_rfk_ops,
554331e0ea2SZhang Rui 				      &priv->rfk_priv[dev]);
55557ac3b05SIke Panhc 	if (!priv->rfk[dev])
55657ac3b05SIke Panhc 		return -ENOMEM;
55757ac3b05SIke Panhc 
558331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
55957ac3b05SIke Panhc 			 &sw_blocked)) {
56057ac3b05SIke Panhc 		rfkill_init_sw_state(priv->rfk[dev], 0);
56157ac3b05SIke Panhc 	} else {
56257ac3b05SIke Panhc 		sw_blocked = !sw_blocked;
56357ac3b05SIke Panhc 		rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
56457ac3b05SIke Panhc 	}
56557ac3b05SIke Panhc 
56657ac3b05SIke Panhc 	ret = rfkill_register(priv->rfk[dev]);
56757ac3b05SIke Panhc 	if (ret) {
56857ac3b05SIke Panhc 		rfkill_destroy(priv->rfk[dev]);
56957ac3b05SIke Panhc 		return ret;
57057ac3b05SIke Panhc 	}
57157ac3b05SIke Panhc 	return 0;
57257ac3b05SIke Panhc }
57357ac3b05SIke Panhc 
57475a11f11SZhang Rui static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
57557ac3b05SIke Panhc {
57657ac3b05SIke Panhc 	if (!priv->rfk[dev])
57757ac3b05SIke Panhc 		return;
57857ac3b05SIke Panhc 
57957ac3b05SIke Panhc 	rfkill_unregister(priv->rfk[dev]);
58057ac3b05SIke Panhc 	rfkill_destroy(priv->rfk[dev]);
58157ac3b05SIke Panhc }
58257ac3b05SIke Panhc 
58398ee6919SIke Panhc /*
58498ee6919SIke Panhc  * Platform device
58598ee6919SIke Panhc  */
586b5c37b79SZhang Rui static int ideapad_sysfs_init(struct ideapad_private *priv)
58798ee6919SIke Panhc {
588b5c37b79SZhang Rui 	return sysfs_create_group(&priv->platform_device->dev.kobj,
589c9f718d0SIke Panhc 				    &ideapad_attribute_group);
59098ee6919SIke Panhc }
59198ee6919SIke Panhc 
592b5c37b79SZhang Rui static void ideapad_sysfs_exit(struct ideapad_private *priv)
59398ee6919SIke Panhc {
5948693ae84SIke Panhc 	sysfs_remove_group(&priv->platform_device->dev.kobj,
595c9f718d0SIke Panhc 			   &ideapad_attribute_group);
59698ee6919SIke Panhc }
59798ee6919SIke Panhc 
598f63409aeSIke Panhc /*
599f63409aeSIke Panhc  * input device
600f63409aeSIke Panhc  */
601f63409aeSIke Panhc static const struct key_entry ideapad_keymap[] = {
602f43d9ec0SIke Panhc 	{ KE_KEY, 6,  { KEY_SWITCHVIDEOMODE } },
603296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 7,  { KEY_CAMERA } },
60448f67d62SAlex Hung 	{ KE_KEY, 8,  { KEY_MICMUTE } },
605296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 11, { KEY_F16 } },
606f43d9ec0SIke Panhc 	{ KE_KEY, 13, { KEY_WLAN } },
607f43d9ec0SIke Panhc 	{ KE_KEY, 16, { KEY_PROG1 } },
608f43d9ec0SIke Panhc 	{ KE_KEY, 17, { KEY_PROG2 } },
609296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 64, { KEY_PROG3 } },
610296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 65, { KEY_PROG4 } },
61107a4a4fcSMaxim Mikityanskiy 	{ KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
61207a4a4fcSMaxim Mikityanskiy 	{ KE_KEY, 67, { KEY_TOUCHPAD_ON } },
61374caab99SArnd Bergmann 	{ KE_KEY, 128, { KEY_ESC } },
61474caab99SArnd Bergmann 
615f63409aeSIke Panhc 	{ KE_END, 0 },
616f63409aeSIke Panhc };
617f63409aeSIke Panhc 
618b859f159SGreg Kroah-Hartman static int ideapad_input_init(struct ideapad_private *priv)
619f63409aeSIke Panhc {
620f63409aeSIke Panhc 	struct input_dev *inputdev;
621f63409aeSIke Panhc 	int error;
622f63409aeSIke Panhc 
623f63409aeSIke Panhc 	inputdev = input_allocate_device();
624b222cca6SJoe Perches 	if (!inputdev)
625f63409aeSIke Panhc 		return -ENOMEM;
626f63409aeSIke Panhc 
627f63409aeSIke Panhc 	inputdev->name = "Ideapad extra buttons";
628f63409aeSIke Panhc 	inputdev->phys = "ideapad/input0";
629f63409aeSIke Panhc 	inputdev->id.bustype = BUS_HOST;
6308693ae84SIke Panhc 	inputdev->dev.parent = &priv->platform_device->dev;
631f63409aeSIke Panhc 
632f63409aeSIke Panhc 	error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
633f63409aeSIke Panhc 	if (error) {
634f63409aeSIke Panhc 		pr_err("Unable to setup input device keymap\n");
635f63409aeSIke Panhc 		goto err_free_dev;
636f63409aeSIke Panhc 	}
637f63409aeSIke Panhc 
638f63409aeSIke Panhc 	error = input_register_device(inputdev);
639f63409aeSIke Panhc 	if (error) {
640f63409aeSIke Panhc 		pr_err("Unable to register input device\n");
641c973d4b5SMichał Kępień 		goto err_free_dev;
642f63409aeSIke Panhc 	}
643f63409aeSIke Panhc 
6448693ae84SIke Panhc 	priv->inputdev = inputdev;
645f63409aeSIke Panhc 	return 0;
646f63409aeSIke Panhc 
647f63409aeSIke Panhc err_free_dev:
648f63409aeSIke Panhc 	input_free_device(inputdev);
649f63409aeSIke Panhc 	return error;
650f63409aeSIke Panhc }
651f63409aeSIke Panhc 
6527451a55aSAxel Lin static void ideapad_input_exit(struct ideapad_private *priv)
653f63409aeSIke Panhc {
6548693ae84SIke Panhc 	input_unregister_device(priv->inputdev);
6558693ae84SIke Panhc 	priv->inputdev = NULL;
656f63409aeSIke Panhc }
657f63409aeSIke Panhc 
6588693ae84SIke Panhc static void ideapad_input_report(struct ideapad_private *priv,
6598693ae84SIke Panhc 				 unsigned long scancode)
660f63409aeSIke Panhc {
6618693ae84SIke Panhc 	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
662f63409aeSIke Panhc }
663f63409aeSIke Panhc 
664f43d9ec0SIke Panhc static void ideapad_input_novokey(struct ideapad_private *priv)
665f43d9ec0SIke Panhc {
666f43d9ec0SIke Panhc 	unsigned long long_pressed;
667f43d9ec0SIke Panhc 
668331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
669f43d9ec0SIke Panhc 		return;
670f43d9ec0SIke Panhc 	if (long_pressed)
671f43d9ec0SIke Panhc 		ideapad_input_report(priv, 17);
672f43d9ec0SIke Panhc 	else
673f43d9ec0SIke Panhc 		ideapad_input_report(priv, 16);
674f43d9ec0SIke Panhc }
675f43d9ec0SIke Panhc 
676296f9fe0SMaxim Mikityanskiy static void ideapad_check_special_buttons(struct ideapad_private *priv)
677296f9fe0SMaxim Mikityanskiy {
678296f9fe0SMaxim Mikityanskiy 	unsigned long bit, value;
679296f9fe0SMaxim Mikityanskiy 
680331e0ea2SZhang Rui 	read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
681296f9fe0SMaxim Mikityanskiy 
682296f9fe0SMaxim Mikityanskiy 	for (bit = 0; bit < 16; bit++) {
683296f9fe0SMaxim Mikityanskiy 		if (test_bit(bit, &value)) {
684296f9fe0SMaxim Mikityanskiy 			switch (bit) {
685a1ec56edSMaxim Mikityanskiy 			case 0:	/* Z580 */
686a1ec56edSMaxim Mikityanskiy 			case 6:	/* Z570 */
687296f9fe0SMaxim Mikityanskiy 				/* Thermal Management button */
688296f9fe0SMaxim Mikityanskiy 				ideapad_input_report(priv, 65);
689296f9fe0SMaxim Mikityanskiy 				break;
690296f9fe0SMaxim Mikityanskiy 			case 1:
691296f9fe0SMaxim Mikityanskiy 				/* OneKey Theater button */
692296f9fe0SMaxim Mikityanskiy 				ideapad_input_report(priv, 64);
693296f9fe0SMaxim Mikityanskiy 				break;
694a1ec56edSMaxim Mikityanskiy 			default:
695a1ec56edSMaxim Mikityanskiy 				pr_info("Unknown special button: %lu\n", bit);
696a1ec56edSMaxim Mikityanskiy 				break;
697296f9fe0SMaxim Mikityanskiy 			}
698296f9fe0SMaxim Mikityanskiy 		}
699296f9fe0SMaxim Mikityanskiy 	}
700296f9fe0SMaxim Mikityanskiy }
701296f9fe0SMaxim Mikityanskiy 
702a4b5a279SIke Panhc /*
703a4ecbb8aSIke Panhc  * backlight
704a4ecbb8aSIke Panhc  */
705a4ecbb8aSIke Panhc static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
706a4ecbb8aSIke Panhc {
707331e0ea2SZhang Rui 	struct ideapad_private *priv = bl_get_data(blightdev);
708a4ecbb8aSIke Panhc 	unsigned long now;
709a4ecbb8aSIke Panhc 
710331e0ea2SZhang Rui 	if (!priv)
711331e0ea2SZhang Rui 		return -EINVAL;
712331e0ea2SZhang Rui 
713331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
714a4ecbb8aSIke Panhc 		return -EIO;
715a4ecbb8aSIke Panhc 	return now;
716a4ecbb8aSIke Panhc }
717a4ecbb8aSIke Panhc 
718a4ecbb8aSIke Panhc static int ideapad_backlight_update_status(struct backlight_device *blightdev)
719a4ecbb8aSIke Panhc {
720331e0ea2SZhang Rui 	struct ideapad_private *priv = bl_get_data(blightdev);
721331e0ea2SZhang Rui 
722331e0ea2SZhang Rui 	if (!priv)
723331e0ea2SZhang Rui 		return -EINVAL;
724331e0ea2SZhang Rui 
725331e0ea2SZhang Rui 	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
7262be1dc21SIke Panhc 			 blightdev->props.brightness))
727a4ecbb8aSIke Panhc 		return -EIO;
728331e0ea2SZhang Rui 	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
729a4ecbb8aSIke Panhc 			 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
730a4ecbb8aSIke Panhc 		return -EIO;
731a4ecbb8aSIke Panhc 
732a4ecbb8aSIke Panhc 	return 0;
733a4ecbb8aSIke Panhc }
734a4ecbb8aSIke Panhc 
735a4ecbb8aSIke Panhc static const struct backlight_ops ideapad_backlight_ops = {
736a4ecbb8aSIke Panhc 	.get_brightness = ideapad_backlight_get_brightness,
737a4ecbb8aSIke Panhc 	.update_status = ideapad_backlight_update_status,
738a4ecbb8aSIke Panhc };
739a4ecbb8aSIke Panhc 
740a4ecbb8aSIke Panhc static int ideapad_backlight_init(struct ideapad_private *priv)
741a4ecbb8aSIke Panhc {
742a4ecbb8aSIke Panhc 	struct backlight_device *blightdev;
743a4ecbb8aSIke Panhc 	struct backlight_properties props;
744a4ecbb8aSIke Panhc 	unsigned long max, now, power;
745a4ecbb8aSIke Panhc 
746331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
747a4ecbb8aSIke Panhc 		return -EIO;
748331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
749a4ecbb8aSIke Panhc 		return -EIO;
750331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
751a4ecbb8aSIke Panhc 		return -EIO;
752a4ecbb8aSIke Panhc 
753a4ecbb8aSIke Panhc 	memset(&props, 0, sizeof(struct backlight_properties));
754a4ecbb8aSIke Panhc 	props.max_brightness = max;
755a4ecbb8aSIke Panhc 	props.type = BACKLIGHT_PLATFORM;
756a4ecbb8aSIke Panhc 	blightdev = backlight_device_register("ideapad",
757a4ecbb8aSIke Panhc 					      &priv->platform_device->dev,
758a4ecbb8aSIke Panhc 					      priv,
759a4ecbb8aSIke Panhc 					      &ideapad_backlight_ops,
760a4ecbb8aSIke Panhc 					      &props);
761a4ecbb8aSIke Panhc 	if (IS_ERR(blightdev)) {
762a4ecbb8aSIke Panhc 		pr_err("Could not register backlight device\n");
763a4ecbb8aSIke Panhc 		return PTR_ERR(blightdev);
764a4ecbb8aSIke Panhc 	}
765a4ecbb8aSIke Panhc 
766a4ecbb8aSIke Panhc 	priv->blightdev = blightdev;
767a4ecbb8aSIke Panhc 	blightdev->props.brightness = now;
768a4ecbb8aSIke Panhc 	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
769a4ecbb8aSIke Panhc 	backlight_update_status(blightdev);
770a4ecbb8aSIke Panhc 
771a4ecbb8aSIke Panhc 	return 0;
772a4ecbb8aSIke Panhc }
773a4ecbb8aSIke Panhc 
774a4ecbb8aSIke Panhc static void ideapad_backlight_exit(struct ideapad_private *priv)
775a4ecbb8aSIke Panhc {
776a4ecbb8aSIke Panhc 	backlight_device_unregister(priv->blightdev);
777a4ecbb8aSIke Panhc 	priv->blightdev = NULL;
778a4ecbb8aSIke Panhc }
779a4ecbb8aSIke Panhc 
780a4ecbb8aSIke Panhc static void ideapad_backlight_notify_power(struct ideapad_private *priv)
781a4ecbb8aSIke Panhc {
782a4ecbb8aSIke Panhc 	unsigned long power;
783a4ecbb8aSIke Panhc 	struct backlight_device *blightdev = priv->blightdev;
784a4ecbb8aSIke Panhc 
785d4afc775SRene Bollford 	if (!blightdev)
786d4afc775SRene Bollford 		return;
787331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
788a4ecbb8aSIke Panhc 		return;
789a4ecbb8aSIke Panhc 	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
790a4ecbb8aSIke Panhc }
791a4ecbb8aSIke Panhc 
792a4ecbb8aSIke Panhc static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
793a4ecbb8aSIke Panhc {
794a4ecbb8aSIke Panhc 	unsigned long now;
795a4ecbb8aSIke Panhc 
796a4ecbb8aSIke Panhc 	/* if we control brightness via acpi video driver */
797a4ecbb8aSIke Panhc 	if (priv->blightdev == NULL) {
798331e0ea2SZhang Rui 		read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
799a4ecbb8aSIke Panhc 		return;
800a4ecbb8aSIke Panhc 	}
801a4ecbb8aSIke Panhc 
802a4ecbb8aSIke Panhc 	backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
803a4ecbb8aSIke Panhc }
804a4ecbb8aSIke Panhc 
805a4ecbb8aSIke Panhc /*
806a4b5a279SIke Panhc  * module init/exit
807a4b5a279SIke Panhc  */
80875a11f11SZhang Rui static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
80907a4a4fcSMaxim Mikityanskiy {
81007a4a4fcSMaxim Mikityanskiy 	unsigned long value;
81107a4a4fcSMaxim Mikityanskiy 
81207a4a4fcSMaxim Mikityanskiy 	/* Without reading from EC touchpad LED doesn't switch state */
81375a11f11SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
81407a4a4fcSMaxim Mikityanskiy 		/* Some IdeaPads don't really turn off touchpad - they only
81507a4a4fcSMaxim Mikityanskiy 		 * switch the LED state. We (de)activate KBC AUX port to turn
81607a4a4fcSMaxim Mikityanskiy 		 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
81707a4a4fcSMaxim Mikityanskiy 		 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
81807a4a4fcSMaxim Mikityanskiy 		unsigned char param;
81907a4a4fcSMaxim Mikityanskiy 		i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
82007a4a4fcSMaxim Mikityanskiy 			      I8042_CMD_AUX_DISABLE);
82107a4a4fcSMaxim Mikityanskiy 		ideapad_input_report(priv, value ? 67 : 66);
82207a4a4fcSMaxim Mikityanskiy 	}
82307a4a4fcSMaxim Mikityanskiy }
82407a4a4fcSMaxim Mikityanskiy 
825b5c37b79SZhang Rui static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
82657ac3b05SIke Panhc {
827b5c37b79SZhang Rui 	struct ideapad_private *priv = data;
82857ac3b05SIke Panhc 	unsigned long vpc1, vpc2, vpc_bit;
82957ac3b05SIke Panhc 
8302be1dc21SIke Panhc 	if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
83157ac3b05SIke Panhc 		return;
8322be1dc21SIke Panhc 	if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
83357ac3b05SIke Panhc 		return;
83457ac3b05SIke Panhc 
83557ac3b05SIke Panhc 	vpc1 = (vpc2 << 8) | vpc1;
83657ac3b05SIke Panhc 	for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
83757ac3b05SIke Panhc 		if (test_bit(vpc_bit, &vpc1)) {
838a4ecbb8aSIke Panhc 			switch (vpc_bit) {
839a4ecbb8aSIke Panhc 			case 9:
840923de84aSIke Panhc 				ideapad_sync_rfk_state(priv);
841a4ecbb8aSIke Panhc 				break;
84220a769c1SIke Panhc 			case 13:
843296f9fe0SMaxim Mikityanskiy 			case 11:
84448f67d62SAlex Hung 			case 8:
845296f9fe0SMaxim Mikityanskiy 			case 7:
84620a769c1SIke Panhc 			case 6:
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;
8643cfd956bSHao Wei Tee 			case 1:
8653cfd956bSHao Wei Tee 				/* Some IdeaPads report event 1 every ~20
8663cfd956bSHao Wei Tee 				 * seconds while on battery power; some
8673cfd956bSHao Wei Tee 				 * report this when changing to/from tablet
8683cfd956bSHao Wei Tee 				 * mode. Squelch this event.
8693cfd956bSHao Wei Tee 				 */
8703cfd956bSHao Wei Tee 				break;
871a4ecbb8aSIke Panhc 			default:
87220a769c1SIke Panhc 				pr_info("Unknown event: %lu\n", vpc_bit);
87357ac3b05SIke Panhc 			}
87457ac3b05SIke Panhc 		}
87557ac3b05SIke Panhc 	}
876a4ecbb8aSIke Panhc }
87757ac3b05SIke Panhc 
87874caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
87974caab99SArnd Bergmann static void ideapad_wmi_notify(u32 value, void *context)
88074caab99SArnd Bergmann {
88174caab99SArnd Bergmann 	switch (value) {
88274caab99SArnd Bergmann 	case 128:
88374caab99SArnd Bergmann 		ideapad_input_report(context, value);
88474caab99SArnd Bergmann 		break;
88574caab99SArnd Bergmann 	default:
88674caab99SArnd Bergmann 		pr_info("Unknown WMI event %u\n", value);
88774caab99SArnd Bergmann 	}
88874caab99SArnd Bergmann }
88974caab99SArnd Bergmann #endif
89074caab99SArnd Bergmann 
891ce363c2bSHans de Goede /*
892ce363c2bSHans de Goede  * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
893ce363c2bSHans de Goede  * always results in 0 on these models, causing ideapad_laptop to wrongly
894ce363c2bSHans de Goede  * report all radios as hardware-blocked.
895ce363c2bSHans de Goede  */
896b3d94d70SMathias Krause static const struct dmi_system_id no_hw_rfkill_list[] = {
89785093f79SHans de Goede 	{
8989b071a43SPhilippe Coval 		.ident = "Lenovo G40-30",
8999b071a43SPhilippe Coval 		.matches = {
9009b071a43SPhilippe Coval 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9019b071a43SPhilippe Coval 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G40-30"),
9029b071a43SPhilippe Coval 		},
9039b071a43SPhilippe Coval 	},
9049b071a43SPhilippe Coval 	{
9054fa9dabcSDmitry Tunin 		.ident = "Lenovo G50-30",
9064fa9dabcSDmitry Tunin 		.matches = {
9074fa9dabcSDmitry Tunin 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9084fa9dabcSDmitry Tunin 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G50-30"),
9094fa9dabcSDmitry Tunin 		},
9104fa9dabcSDmitry Tunin 	},
9114fa9dabcSDmitry Tunin 	{
912ccc7179fSAndy Shevchenko 		.ident = "Lenovo V310-15ISK",
913ccc7179fSAndy Shevchenko 		.matches = {
914ccc7179fSAndy Shevchenko 		        DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
915ccc7179fSAndy Shevchenko 		        DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15ISK"),
916ccc7179fSAndy Shevchenko 		},
917ccc7179fSAndy Shevchenko 	},
918ccc7179fSAndy Shevchenko 	{
9191f3bc53dSSven Rebhan 		.ident = "Lenovo ideapad 310-15IKB",
9201f3bc53dSSven Rebhan 		.matches = {
9211f3bc53dSSven Rebhan 		        DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9221f3bc53dSSven Rebhan 		        DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IKB"),
9231f3bc53dSSven Rebhan 		},
9241f3bc53dSSven Rebhan 	},
9251f3bc53dSSven Rebhan 	{
926e2970468Svelemas 		.ident = "Lenovo ideapad Y700-15ACZ",
927e2970468Svelemas 		.matches = {
928e2970468Svelemas 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
929e2970468Svelemas 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ACZ"),
930e2970468Svelemas 		},
931e2970468Svelemas 	},
932e2970468Svelemas 	{
9334db9675dSJohn Dahlstrom 		.ident = "Lenovo ideapad Y700-15ISK",
9344db9675dSJohn Dahlstrom 		.matches = {
9354db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9364db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ISK"),
9374db9675dSJohn Dahlstrom 		},
9384db9675dSJohn Dahlstrom 	},
9394db9675dSJohn Dahlstrom 	{
9404db9675dSJohn Dahlstrom 		.ident = "Lenovo ideapad Y700 Touch-15ISK",
9414db9675dSJohn Dahlstrom 		.matches = {
9424db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9434db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700 Touch-15ISK"),
9444db9675dSJohn Dahlstrom 		},
9454db9675dSJohn Dahlstrom 	},
9464db9675dSJohn Dahlstrom 	{
947edde316aSJosh Boyer 		.ident = "Lenovo ideapad Y700-17ISK",
948edde316aSJosh Boyer 		.matches = {
949edde316aSJosh Boyer 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
950edde316aSJosh Boyer 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-17ISK"),
951edde316aSJosh Boyer 		},
952edde316aSJosh Boyer 	},
953edde316aSJosh Boyer 	{
9545d9f40b5SOlle Liljenzin 		.ident = "Lenovo Legion Y520-15IKBN",
9555d9f40b5SOlle Liljenzin 		.matches = {
9565d9f40b5SOlle Liljenzin 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9575d9f40b5SOlle Liljenzin 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y520-15IKBN"),
9585d9f40b5SOlle Liljenzin 		},
9595d9f40b5SOlle Liljenzin 	},
9605d9f40b5SOlle Liljenzin 	{
961ce363c2bSHans de Goede 		.ident = "Lenovo Yoga 2 11 / 13 / Pro",
96285093f79SHans de Goede 		.matches = {
96385093f79SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
964ce363c2bSHans de Goede 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
96585093f79SHans de Goede 		},
96685093f79SHans de Goede 	},
967725c7f61SStephan Mueller 	{
9686d212b8aSSebastian Krzyszkowiak 		.ident = "Lenovo Yoga 2 11 / 13 / Pro",
9696d212b8aSSebastian Krzyszkowiak 		.matches = {
9706d212b8aSSebastian Krzyszkowiak 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9716d212b8aSSebastian Krzyszkowiak 			DMI_MATCH(DMI_BOARD_NAME, "Yoga2"),
9726d212b8aSSebastian Krzyszkowiak 		},
9736d212b8aSSebastian Krzyszkowiak 	},
9746d212b8aSSebastian Krzyszkowiak 	{
975c789fffcSArnd Bergmann 		.ident = "Lenovo Yoga 3 1170 / 1470",
976c789fffcSArnd Bergmann 		.matches = {
977c789fffcSArnd Bergmann 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
978c789fffcSArnd Bergmann 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 3"),
979c789fffcSArnd Bergmann 		},
980c789fffcSArnd Bergmann 	},
981c789fffcSArnd Bergmann 	{
982725c7f61SStephan Mueller 		.ident = "Lenovo Yoga 3 Pro 1370",
983725c7f61SStephan Mueller 		.matches = {
984725c7f61SStephan Mueller 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
985c789fffcSArnd Bergmann 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
986725c7f61SStephan Mueller 		},
987725c7f61SStephan Mueller 	},
988f71c882dSHans de Goede 	{
9896b31de3eSJosh Boyer 		.ident = "Lenovo Yoga 700",
9906b31de3eSJosh Boyer 		.matches = {
9916b31de3eSJosh Boyer 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9926b31de3eSJosh Boyer 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 700"),
9936b31de3eSJosh Boyer 		},
9946b31de3eSJosh Boyer 	},
9956b31de3eSJosh Boyer 	{
996f71c882dSHans de Goede 		.ident = "Lenovo Yoga 900",
997f71c882dSHans de Goede 		.matches = {
998f71c882dSHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
999f71c882dSHans de Goede 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 900"),
1000f71c882dSHans de Goede 		},
1001f71c882dSHans de Goede 	},
100240c30bbfSBrian Masney 	{
1003446647d4SMika Westerberg 		.ident = "Lenovo Yoga 900",
1004446647d4SMika Westerberg 		.matches = {
1005446647d4SMika Westerberg 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1006446647d4SMika Westerberg 			DMI_MATCH(DMI_BOARD_NAME, "VIUU4"),
1007446647d4SMika Westerberg 		},
1008446647d4SMika Westerberg 	},
1009446647d4SMika Westerberg 	{
101040c30bbfSBrian Masney 		.ident = "Lenovo YOGA 910-13IKB",
101140c30bbfSBrian Masney 		.matches = {
101240c30bbfSBrian Masney 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
101340c30bbfSBrian Masney 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 910-13IKB"),
101440c30bbfSBrian Masney 		},
101540c30bbfSBrian Masney 	},
101685093f79SHans de Goede 	{}
101785093f79SHans de Goede };
101885093f79SHans de Goede 
1019b5c37b79SZhang Rui static int ideapad_acpi_add(struct platform_device *pdev)
1020b5c37b79SZhang Rui {
1021b5c37b79SZhang Rui 	int ret, i;
1022b5c37b79SZhang Rui 	int cfg;
1023b5c37b79SZhang Rui 	struct ideapad_private *priv;
1024b5c37b79SZhang Rui 	struct acpi_device *adev;
1025b5c37b79SZhang Rui 
1026b5c37b79SZhang Rui 	ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
1027b5c37b79SZhang Rui 	if (ret)
1028b5c37b79SZhang Rui 		return -ENODEV;
1029b5c37b79SZhang Rui 
1030b5c37b79SZhang Rui 	if (read_method_int(adev->handle, "_CFG", &cfg))
1031b5c37b79SZhang Rui 		return -ENODEV;
1032b5c37b79SZhang Rui 
1033b3facd7bSHimangi Saraogi 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1034b5c37b79SZhang Rui 	if (!priv)
1035b5c37b79SZhang Rui 		return -ENOMEM;
1036b5c37b79SZhang Rui 
1037b5c37b79SZhang Rui 	dev_set_drvdata(&pdev->dev, priv);
1038b5c37b79SZhang Rui 	priv->cfg = cfg;
1039b5c37b79SZhang Rui 	priv->adev = adev;
1040b5c37b79SZhang Rui 	priv->platform_device = pdev;
1041ce363c2bSHans de Goede 	priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
1042b5c37b79SZhang Rui 
1043b5c37b79SZhang Rui 	ret = ideapad_sysfs_init(priv);
1044b5c37b79SZhang Rui 	if (ret)
1045b3facd7bSHimangi Saraogi 		return ret;
1046b5c37b79SZhang Rui 
1047b5c37b79SZhang Rui 	ret = ideapad_debugfs_init(priv);
1048b5c37b79SZhang Rui 	if (ret)
1049b5c37b79SZhang Rui 		goto debugfs_failed;
1050b5c37b79SZhang Rui 
1051b5c37b79SZhang Rui 	ret = ideapad_input_init(priv);
1052b5c37b79SZhang Rui 	if (ret)
1053b5c37b79SZhang Rui 		goto input_failed;
1054b5c37b79SZhang Rui 
1055ce363c2bSHans de Goede 	/*
1056ce363c2bSHans de Goede 	 * On some models without a hw-switch (the yoga 2 13 at least)
1057ce363c2bSHans de Goede 	 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1058ce363c2bSHans de Goede 	 */
1059ce363c2bSHans de Goede 	if (!priv->has_hw_rfkill_switch)
1060ce363c2bSHans de Goede 		write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1061ce363c2bSHans de Goede 
106285093f79SHans de Goede 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1063b5c37b79SZhang Rui 		if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1064b5c37b79SZhang Rui 			ideapad_register_rfkill(priv, i);
1065ce363c2bSHans de Goede 
1066b5c37b79SZhang Rui 	ideapad_sync_rfk_state(priv);
1067b5c37b79SZhang Rui 	ideapad_sync_touchpad_state(priv);
1068b5c37b79SZhang Rui 
106926bff5f0SHans de Goede 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1070b5c37b79SZhang Rui 		ret = ideapad_backlight_init(priv);
1071b5c37b79SZhang Rui 		if (ret && ret != -ENODEV)
1072b5c37b79SZhang Rui 			goto backlight_failed;
1073b5c37b79SZhang Rui 	}
1074b5c37b79SZhang Rui 	ret = acpi_install_notify_handler(adev->handle,
1075b5c37b79SZhang Rui 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
1076b5c37b79SZhang Rui 	if (ret)
1077b5c37b79SZhang Rui 		goto notification_failed;
10782d98e0b9SArnd Bergmann 
107974caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
10802d98e0b9SArnd Bergmann 	for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
10812d98e0b9SArnd Bergmann 		ret = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
10822d98e0b9SArnd Bergmann 						 ideapad_wmi_notify, priv);
10832d98e0b9SArnd Bergmann 		if (ret == AE_OK) {
10842d98e0b9SArnd Bergmann 			priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
10852d98e0b9SArnd Bergmann 			break;
10862d98e0b9SArnd Bergmann 		}
10872d98e0b9SArnd Bergmann 	}
108874caab99SArnd Bergmann 	if (ret != AE_OK && ret != AE_NOT_EXIST)
108974caab99SArnd Bergmann 		goto notification_failed_wmi;
109074caab99SArnd Bergmann #endif
1091b5c37b79SZhang Rui 
1092b5c37b79SZhang Rui 	return 0;
109374caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
109474caab99SArnd Bergmann notification_failed_wmi:
109574caab99SArnd Bergmann 	acpi_remove_notify_handler(priv->adev->handle,
109674caab99SArnd Bergmann 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
109774caab99SArnd Bergmann #endif
1098b5c37b79SZhang Rui notification_failed:
1099b5c37b79SZhang Rui 	ideapad_backlight_exit(priv);
1100b5c37b79SZhang Rui backlight_failed:
1101b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1102b5c37b79SZhang Rui 		ideapad_unregister_rfkill(priv, i);
1103b5c37b79SZhang Rui 	ideapad_input_exit(priv);
1104b5c37b79SZhang Rui input_failed:
1105b5c37b79SZhang Rui 	ideapad_debugfs_exit(priv);
1106b5c37b79SZhang Rui debugfs_failed:
1107b5c37b79SZhang Rui 	ideapad_sysfs_exit(priv);
1108b5c37b79SZhang Rui 	return ret;
1109b5c37b79SZhang Rui }
1110b5c37b79SZhang Rui 
1111b5c37b79SZhang Rui static int ideapad_acpi_remove(struct platform_device *pdev)
1112b5c37b79SZhang Rui {
1113b5c37b79SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1114b5c37b79SZhang Rui 	int i;
1115b5c37b79SZhang Rui 
111674caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
11172d98e0b9SArnd Bergmann 	if (priv->fnesc_guid)
11182d98e0b9SArnd Bergmann 		wmi_remove_notify_handler(priv->fnesc_guid);
111974caab99SArnd Bergmann #endif
1120b5c37b79SZhang Rui 	acpi_remove_notify_handler(priv->adev->handle,
1121b5c37b79SZhang Rui 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1122b5c37b79SZhang Rui 	ideapad_backlight_exit(priv);
1123b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1124b5c37b79SZhang Rui 		ideapad_unregister_rfkill(priv, i);
1125b5c37b79SZhang Rui 	ideapad_input_exit(priv);
1126b5c37b79SZhang Rui 	ideapad_debugfs_exit(priv);
1127b5c37b79SZhang Rui 	ideapad_sysfs_exit(priv);
1128b5c37b79SZhang Rui 	dev_set_drvdata(&pdev->dev, NULL);
1129b5c37b79SZhang Rui 
1130b5c37b79SZhang Rui 	return 0;
1131b5c37b79SZhang Rui }
1132b5c37b79SZhang Rui 
113311fa8da5SZhang Rui #ifdef CONFIG_PM_SLEEP
113407a4a4fcSMaxim Mikityanskiy static int ideapad_acpi_resume(struct device *device)
113507a4a4fcSMaxim Mikityanskiy {
113675a11f11SZhang Rui 	struct ideapad_private *priv;
113775a11f11SZhang Rui 
113875a11f11SZhang Rui 	if (!device)
113975a11f11SZhang Rui 		return -EINVAL;
114075a11f11SZhang Rui 	priv = dev_get_drvdata(device);
114175a11f11SZhang Rui 
114275a11f11SZhang Rui 	ideapad_sync_rfk_state(priv);
114375a11f11SZhang Rui 	ideapad_sync_touchpad_state(priv);
114407a4a4fcSMaxim Mikityanskiy 	return 0;
114507a4a4fcSMaxim Mikityanskiy }
1146b5c37b79SZhang Rui #endif
114707a4a4fcSMaxim Mikityanskiy static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
114807a4a4fcSMaxim Mikityanskiy 
1149b5c37b79SZhang Rui static const struct acpi_device_id ideapad_device_ids[] = {
1150b5c37b79SZhang Rui 	{ "VPC2004", 0},
1151b5c37b79SZhang Rui 	{ "", 0},
115257ac3b05SIke Panhc };
1153b5c37b79SZhang Rui MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1154b5c37b79SZhang Rui 
1155b5c37b79SZhang Rui static struct platform_driver ideapad_acpi_driver = {
1156b5c37b79SZhang Rui 	.probe = ideapad_acpi_add,
1157b5c37b79SZhang Rui 	.remove = ideapad_acpi_remove,
1158b5c37b79SZhang Rui 	.driver = {
1159b5c37b79SZhang Rui 		.name   = "ideapad_acpi",
1160b5c37b79SZhang Rui 		.pm     = &ideapad_pm,
1161b5c37b79SZhang Rui 		.acpi_match_table = ACPI_PTR(ideapad_device_ids),
1162b5c37b79SZhang Rui 	},
1163b5c37b79SZhang Rui };
1164b5c37b79SZhang Rui 
1165b5c37b79SZhang Rui module_platform_driver(ideapad_acpi_driver);
116657ac3b05SIke Panhc 
116757ac3b05SIke Panhc MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
116857ac3b05SIke Panhc MODULE_DESCRIPTION("IdeaPad ACPI Extras");
116957ac3b05SIke Panhc MODULE_LICENSE("GPL");
1170