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 
45ade50296SHao Wei Tee #define BM_CONSERVATION_BIT (5)
46ade50296SHao Wei Tee 
473371f481SIke Panhc #define CFG_BT_BIT	(16)
483371f481SIke Panhc #define CFG_3G_BIT	(17)
493371f481SIke Panhc #define CFG_WIFI_BIT	(18)
50a84511f7SIke Panhc #define CFG_CAMERA_BIT	(19)
513371f481SIke Panhc 
5274caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
532d98e0b9SArnd Bergmann static const char *const ideapad_wmi_fnesc_events[] = {
542d98e0b9SArnd Bergmann 	"26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
552d98e0b9SArnd Bergmann 	"56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
562d98e0b9SArnd Bergmann };
5774caab99SArnd Bergmann #endif
5874caab99SArnd Bergmann 
592be1dc21SIke Panhc enum {
60ade50296SHao Wei Tee 	BMCMD_CONSERVATION_ON = 3,
61ade50296SHao Wei Tee 	BMCMD_CONSERVATION_OFF = 5,
62ade50296SHao Wei Tee };
63ade50296SHao Wei Tee 
64ade50296SHao Wei Tee enum {
652be1dc21SIke Panhc 	VPCCMD_R_VPC1 = 0x10,
662be1dc21SIke Panhc 	VPCCMD_R_BL_MAX,
672be1dc21SIke Panhc 	VPCCMD_R_BL,
682be1dc21SIke Panhc 	VPCCMD_W_BL,
692be1dc21SIke Panhc 	VPCCMD_R_WIFI,
702be1dc21SIke Panhc 	VPCCMD_W_WIFI,
712be1dc21SIke Panhc 	VPCCMD_R_BT,
722be1dc21SIke Panhc 	VPCCMD_W_BT,
732be1dc21SIke Panhc 	VPCCMD_R_BL_POWER,
742be1dc21SIke Panhc 	VPCCMD_R_NOVO,
752be1dc21SIke Panhc 	VPCCMD_R_VPC2,
762be1dc21SIke Panhc 	VPCCMD_R_TOUCHPAD,
772be1dc21SIke Panhc 	VPCCMD_W_TOUCHPAD,
782be1dc21SIke Panhc 	VPCCMD_R_CAMERA,
792be1dc21SIke Panhc 	VPCCMD_W_CAMERA,
802be1dc21SIke Panhc 	VPCCMD_R_3G,
812be1dc21SIke Panhc 	VPCCMD_W_3G,
822be1dc21SIke Panhc 	VPCCMD_R_ODD, /* 0x21 */
830c7bbeb9SMaxim Mikityanskiy 	VPCCMD_W_FAN,
840c7bbeb9SMaxim Mikityanskiy 	VPCCMD_R_RF,
852be1dc21SIke Panhc 	VPCCMD_W_RF,
860c7bbeb9SMaxim Mikityanskiy 	VPCCMD_R_FAN = 0x2B,
87296f9fe0SMaxim Mikityanskiy 	VPCCMD_R_SPECIAL_BUTTONS = 0x31,
882be1dc21SIke Panhc 	VPCCMD_W_BL_POWER = 0x33,
892be1dc21SIke Panhc };
902be1dc21SIke Panhc 
91331e0ea2SZhang Rui struct ideapad_rfk_priv {
92331e0ea2SZhang Rui 	int dev;
93331e0ea2SZhang Rui 	struct ideapad_private *priv;
94331e0ea2SZhang Rui };
95331e0ea2SZhang Rui 
9657ac3b05SIke Panhc struct ideapad_private {
97469f6434SZhang Rui 	struct acpi_device *adev;
98c1f73658SIke Panhc 	struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
99331e0ea2SZhang Rui 	struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
10098ee6919SIke Panhc 	struct platform_device *platform_device;
101f63409aeSIke Panhc 	struct input_dev *inputdev;
102a4ecbb8aSIke Panhc 	struct backlight_device *blightdev;
103773e3206SIke Panhc 	struct dentry *debug;
1043371f481SIke Panhc 	unsigned long cfg;
105ce363c2bSHans de Goede 	bool has_hw_rfkill_switch;
1062d98e0b9SArnd Bergmann 	const char *fnesc_guid;
10757ac3b05SIke Panhc };
10857ac3b05SIke Panhc 
109bfa97b7dSIke Panhc static bool no_bt_rfkill;
110bfa97b7dSIke Panhc module_param(no_bt_rfkill, bool, 0444);
111bfa97b7dSIke Panhc MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
112bfa97b7dSIke Panhc 
11357ac3b05SIke Panhc /*
11457ac3b05SIke Panhc  * ACPI Helpers
11557ac3b05SIke Panhc  */
116ed5b9ba7SAaron Ma #define IDEAPAD_EC_TIMEOUT (200) /* in ms */
11757ac3b05SIke Panhc 
11857ac3b05SIke Panhc static int read_method_int(acpi_handle handle, const char *method, int *val)
11957ac3b05SIke Panhc {
12057ac3b05SIke Panhc 	acpi_status status;
12157ac3b05SIke Panhc 	unsigned long long result;
12257ac3b05SIke Panhc 
12357ac3b05SIke Panhc 	status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
12457ac3b05SIke Panhc 	if (ACPI_FAILURE(status)) {
12557ac3b05SIke Panhc 		*val = -1;
12657ac3b05SIke Panhc 		return -1;
127ba3a3387SJiaxun Yang 	}
12857ac3b05SIke Panhc 	*val = result;
12957ac3b05SIke Panhc 	return 0;
130ba3a3387SJiaxun Yang 
13157ac3b05SIke Panhc }
13257ac3b05SIke Panhc 
133ade50296SHao Wei Tee static int method_gbmd(acpi_handle handle, unsigned long *ret)
134ade50296SHao Wei Tee {
135ade50296SHao Wei Tee 	int result, val;
136ade50296SHao Wei Tee 
137ade50296SHao Wei Tee 	result = read_method_int(handle, "GBMD", &val);
138ade50296SHao Wei Tee 	*ret = val;
139ade50296SHao Wei Tee 	return result;
140ade50296SHao Wei Tee }
141ade50296SHao Wei Tee 
142ade50296SHao Wei Tee static int method_sbmc(acpi_handle handle, int cmd)
143ade50296SHao Wei Tee {
144ade50296SHao Wei Tee 	acpi_status status;
145ade50296SHao Wei Tee 
146ade50296SHao Wei Tee 	status = acpi_execute_simple_method(handle, "SBMC", cmd);
147ade50296SHao Wei Tee 	return ACPI_FAILURE(status) ? -1 : 0;
148ade50296SHao Wei Tee }
149ade50296SHao Wei Tee 
15057ac3b05SIke Panhc static int method_vpcr(acpi_handle handle, int cmd, int *ret)
15157ac3b05SIke Panhc {
15257ac3b05SIke Panhc 	acpi_status status;
15357ac3b05SIke Panhc 	unsigned long long result;
15457ac3b05SIke Panhc 	struct acpi_object_list params;
15557ac3b05SIke Panhc 	union acpi_object in_obj;
15657ac3b05SIke Panhc 
15757ac3b05SIke Panhc 	params.count = 1;
15857ac3b05SIke Panhc 	params.pointer = &in_obj;
15957ac3b05SIke Panhc 	in_obj.type = ACPI_TYPE_INTEGER;
16057ac3b05SIke Panhc 	in_obj.integer.value = cmd;
16157ac3b05SIke Panhc 
16257ac3b05SIke Panhc 	status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
16357ac3b05SIke Panhc 
16457ac3b05SIke Panhc 	if (ACPI_FAILURE(status)) {
16557ac3b05SIke Panhc 		*ret = -1;
16657ac3b05SIke Panhc 		return -1;
167ba3a3387SJiaxun Yang 	}
16857ac3b05SIke Panhc 	*ret = result;
16957ac3b05SIke Panhc 	return 0;
170ba3a3387SJiaxun Yang 
17157ac3b05SIke Panhc }
17257ac3b05SIke Panhc 
17357ac3b05SIke Panhc static int method_vpcw(acpi_handle handle, int cmd, int data)
17457ac3b05SIke Panhc {
17557ac3b05SIke Panhc 	struct acpi_object_list params;
17657ac3b05SIke Panhc 	union acpi_object in_obj[2];
17757ac3b05SIke Panhc 	acpi_status status;
17857ac3b05SIke Panhc 
17957ac3b05SIke Panhc 	params.count = 2;
18057ac3b05SIke Panhc 	params.pointer = in_obj;
18157ac3b05SIke Panhc 	in_obj[0].type = ACPI_TYPE_INTEGER;
18257ac3b05SIke Panhc 	in_obj[0].integer.value = cmd;
18357ac3b05SIke Panhc 	in_obj[1].type = ACPI_TYPE_INTEGER;
18457ac3b05SIke Panhc 	in_obj[1].integer.value = data;
18557ac3b05SIke Panhc 
18657ac3b05SIke Panhc 	status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
18757ac3b05SIke Panhc 	if (status != AE_OK)
18857ac3b05SIke Panhc 		return -1;
18957ac3b05SIke Panhc 	return 0;
19057ac3b05SIke Panhc }
19157ac3b05SIke Panhc 
19257ac3b05SIke Panhc static int read_ec_data(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, 1, cmd))
19857ac3b05SIke Panhc 		return -1;
19957ac3b05SIke Panhc 
20057ac3b05SIke Panhc 	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
20157ac3b05SIke Panhc 	     time_before(jiffies, end_jiffies);) {
20257ac3b05SIke Panhc 		schedule();
20357ac3b05SIke Panhc 		if (method_vpcr(handle, 1, &val))
20457ac3b05SIke Panhc 			return -1;
20557ac3b05SIke Panhc 		if (val == 0) {
20657ac3b05SIke Panhc 			if (method_vpcr(handle, 0, &val))
20757ac3b05SIke Panhc 				return -1;
20857ac3b05SIke Panhc 			*data = val;
20957ac3b05SIke Panhc 			return 0;
21057ac3b05SIke Panhc 		}
21157ac3b05SIke Panhc 	}
21257ac3b05SIke Panhc 	pr_err("timeout in read_ec_cmd\n");
21357ac3b05SIke Panhc 	return -1;
21457ac3b05SIke Panhc }
21557ac3b05SIke Panhc 
21657ac3b05SIke Panhc static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
21757ac3b05SIke Panhc {
21857ac3b05SIke Panhc 	int val;
21957ac3b05SIke Panhc 	unsigned long int end_jiffies;
22057ac3b05SIke Panhc 
22157ac3b05SIke Panhc 	if (method_vpcw(handle, 0, data))
22257ac3b05SIke Panhc 		return -1;
22357ac3b05SIke Panhc 	if (method_vpcw(handle, 1, cmd))
22457ac3b05SIke Panhc 		return -1;
22557ac3b05SIke Panhc 
22657ac3b05SIke Panhc 	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
22757ac3b05SIke Panhc 	     time_before(jiffies, end_jiffies);) {
22857ac3b05SIke Panhc 		schedule();
22957ac3b05SIke Panhc 		if (method_vpcr(handle, 1, &val))
23057ac3b05SIke Panhc 			return -1;
23157ac3b05SIke Panhc 		if (val == 0)
23257ac3b05SIke Panhc 			return 0;
23357ac3b05SIke Panhc 	}
234f1395edbSJiaxun Yang 	pr_err("timeout in %s\n", __func__);
23557ac3b05SIke Panhc 	return -1;
23657ac3b05SIke Panhc }
23757ac3b05SIke Panhc 
238a4b5a279SIke Panhc /*
239773e3206SIke Panhc  * debugfs
240773e3206SIke Panhc  */
241773e3206SIke Panhc static int debugfs_status_show(struct seq_file *s, void *data)
242773e3206SIke Panhc {
243331e0ea2SZhang Rui 	struct ideapad_private *priv = s->private;
244773e3206SIke Panhc 	unsigned long value;
245773e3206SIke Panhc 
246331e0ea2SZhang Rui 	if (!priv)
247331e0ea2SZhang Rui 		return -EINVAL;
248331e0ea2SZhang Rui 
249331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
250773e3206SIke Panhc 		seq_printf(s, "Backlight max:\t%lu\n", value);
251331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
252773e3206SIke Panhc 		seq_printf(s, "Backlight now:\t%lu\n", value);
253331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
254773e3206SIke Panhc 		seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
255773e3206SIke Panhc 	seq_printf(s, "=====================\n");
256773e3206SIke Panhc 
257331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
258773e3206SIke Panhc 		seq_printf(s, "Radio status:\t%s(%lu)\n",
259773e3206SIke Panhc 			   value ? "On" : "Off", value);
260331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
261773e3206SIke Panhc 		seq_printf(s, "Wifi status:\t%s(%lu)\n",
262773e3206SIke Panhc 			   value ? "On" : "Off", value);
263331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
264773e3206SIke Panhc 		seq_printf(s, "BT status:\t%s(%lu)\n",
265773e3206SIke Panhc 			   value ? "On" : "Off", value);
266331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
267773e3206SIke Panhc 		seq_printf(s, "3G status:\t%s(%lu)\n",
268773e3206SIke Panhc 			   value ? "On" : "Off", value);
269773e3206SIke Panhc 	seq_printf(s, "=====================\n");
270773e3206SIke Panhc 
271331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
272773e3206SIke Panhc 		seq_printf(s, "Touchpad status:%s(%lu)\n",
273773e3206SIke Panhc 			   value ? "On" : "Off", value);
274331e0ea2SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
275773e3206SIke Panhc 		seq_printf(s, "Camera status:\t%s(%lu)\n",
276773e3206SIke Panhc 			   value ? "On" : "Off", value);
277ade50296SHao Wei Tee 	seq_puts(s, "=====================\n");
278ade50296SHao Wei Tee 
279ade50296SHao Wei Tee 	if (!method_gbmd(priv->adev->handle, &value)) {
280ade50296SHao Wei Tee 		seq_printf(s, "Conservation mode:\t%s(%lu)\n",
281ade50296SHao Wei Tee 			   test_bit(BM_CONSERVATION_BIT, &value) ? "On" : "Off",
282ade50296SHao Wei Tee 			   value);
283ade50296SHao Wei Tee 	}
284773e3206SIke Panhc 
285773e3206SIke Panhc 	return 0;
286773e3206SIke Panhc }
287334c4efdSAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(debugfs_status);
288773e3206SIke Panhc 
289773e3206SIke Panhc static int debugfs_cfg_show(struct seq_file *s, void *data)
290773e3206SIke Panhc {
291331e0ea2SZhang Rui 	struct ideapad_private *priv = s->private;
292331e0ea2SZhang Rui 
293331e0ea2SZhang Rui 	if (!priv) {
294773e3206SIke Panhc 		seq_printf(s, "cfg: N/A\n");
295773e3206SIke Panhc 	} else {
296773e3206SIke Panhc 		seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
297331e0ea2SZhang Rui 			   priv->cfg);
298331e0ea2SZhang Rui 		if (test_bit(CFG_BT_BIT, &priv->cfg))
299773e3206SIke Panhc 			seq_printf(s, "Bluetooth ");
300331e0ea2SZhang Rui 		if (test_bit(CFG_3G_BIT, &priv->cfg))
301773e3206SIke Panhc 			seq_printf(s, "3G ");
302331e0ea2SZhang Rui 		if (test_bit(CFG_WIFI_BIT, &priv->cfg))
303773e3206SIke Panhc 			seq_printf(s, "Wireless ");
304331e0ea2SZhang Rui 		if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
305773e3206SIke Panhc 			seq_printf(s, "Camera ");
306773e3206SIke Panhc 		seq_printf(s, "\nGraphic: ");
307331e0ea2SZhang Rui 		switch ((priv->cfg)&0x700) {
308773e3206SIke Panhc 		case 0x100:
309773e3206SIke Panhc 			seq_printf(s, "Intel");
310773e3206SIke Panhc 			break;
311773e3206SIke Panhc 		case 0x200:
312773e3206SIke Panhc 			seq_printf(s, "ATI");
313773e3206SIke Panhc 			break;
314773e3206SIke Panhc 		case 0x300:
315773e3206SIke Panhc 			seq_printf(s, "Nvidia");
316773e3206SIke Panhc 			break;
317773e3206SIke Panhc 		case 0x400:
318773e3206SIke Panhc 			seq_printf(s, "Intel and ATI");
319773e3206SIke Panhc 			break;
320773e3206SIke Panhc 		case 0x500:
321773e3206SIke Panhc 			seq_printf(s, "Intel and Nvidia");
322773e3206SIke Panhc 			break;
323773e3206SIke Panhc 		}
324773e3206SIke Panhc 		seq_printf(s, "\n");
325773e3206SIke Panhc 	}
326773e3206SIke Panhc 	return 0;
327773e3206SIke Panhc }
328334c4efdSAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
329773e3206SIke Panhc 
330b859f159SGreg Kroah-Hartman static int ideapad_debugfs_init(struct ideapad_private *priv)
331773e3206SIke Panhc {
332773e3206SIke Panhc 	struct dentry *node;
333773e3206SIke Panhc 
334773e3206SIke Panhc 	priv->debug = debugfs_create_dir("ideapad", NULL);
335773e3206SIke Panhc 	if (priv->debug == NULL) {
336773e3206SIke Panhc 		pr_err("failed to create debugfs directory");
337773e3206SIke Panhc 		goto errout;
338773e3206SIke Panhc 	}
339773e3206SIke Panhc 
340331e0ea2SZhang Rui 	node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
341773e3206SIke Panhc 				   &debugfs_cfg_fops);
342773e3206SIke Panhc 	if (!node) {
343773e3206SIke Panhc 		pr_err("failed to create cfg in debugfs");
344773e3206SIke Panhc 		goto errout;
345773e3206SIke Panhc 	}
346773e3206SIke Panhc 
347331e0ea2SZhang Rui 	node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
348773e3206SIke Panhc 				   &debugfs_status_fops);
349773e3206SIke Panhc 	if (!node) {
350a5c3892fSIke Panhc 		pr_err("failed to create status in debugfs");
351773e3206SIke Panhc 		goto errout;
352773e3206SIke Panhc 	}
353773e3206SIke Panhc 
354773e3206SIke Panhc 	return 0;
355773e3206SIke Panhc 
356773e3206SIke Panhc errout:
357773e3206SIke Panhc 	return -ENOMEM;
358773e3206SIke Panhc }
359773e3206SIke Panhc 
360773e3206SIke Panhc static void ideapad_debugfs_exit(struct ideapad_private *priv)
361773e3206SIke Panhc {
362773e3206SIke Panhc 	debugfs_remove_recursive(priv->debug);
363773e3206SIke Panhc 	priv->debug = NULL;
364773e3206SIke Panhc }
365773e3206SIke Panhc 
366773e3206SIke Panhc /*
3673371f481SIke Panhc  * sysfs
368a4b5a279SIke Panhc  */
36957ac3b05SIke Panhc static ssize_t show_ideapad_cam(struct device *dev,
37057ac3b05SIke Panhc 				struct device_attribute *attr,
37157ac3b05SIke Panhc 				char *buf)
37257ac3b05SIke Panhc {
37357ac3b05SIke Panhc 	unsigned long result;
374331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
37557ac3b05SIke Panhc 
376331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
37757ac3b05SIke Panhc 		return sprintf(buf, "-1\n");
37857ac3b05SIke Panhc 	return sprintf(buf, "%lu\n", result);
37957ac3b05SIke Panhc }
38057ac3b05SIke Panhc 
38157ac3b05SIke Panhc static ssize_t store_ideapad_cam(struct device *dev,
38257ac3b05SIke Panhc 				 struct device_attribute *attr,
38357ac3b05SIke Panhc 				 const char *buf, size_t count)
38457ac3b05SIke Panhc {
38557ac3b05SIke Panhc 	int ret, state;
386331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
38757ac3b05SIke Panhc 
38857ac3b05SIke Panhc 	if (!count)
38957ac3b05SIke Panhc 		return 0;
39057ac3b05SIke Panhc 	if (sscanf(buf, "%i", &state) != 1)
39157ac3b05SIke Panhc 		return -EINVAL;
392331e0ea2SZhang Rui 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
39357ac3b05SIke Panhc 	if (ret < 0)
3940c7bbeb9SMaxim Mikityanskiy 		return -EIO;
39557ac3b05SIke Panhc 	return count;
39657ac3b05SIke Panhc }
39757ac3b05SIke Panhc 
39857ac3b05SIke Panhc static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
39957ac3b05SIke Panhc 
4000c7bbeb9SMaxim Mikityanskiy static ssize_t show_ideapad_fan(struct device *dev,
4010c7bbeb9SMaxim Mikityanskiy 				struct device_attribute *attr,
4020c7bbeb9SMaxim Mikityanskiy 				char *buf)
4030c7bbeb9SMaxim Mikityanskiy {
4040c7bbeb9SMaxim Mikityanskiy 	unsigned long result;
405331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
4060c7bbeb9SMaxim Mikityanskiy 
407331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
4080c7bbeb9SMaxim Mikityanskiy 		return sprintf(buf, "-1\n");
4090c7bbeb9SMaxim Mikityanskiy 	return sprintf(buf, "%lu\n", result);
4100c7bbeb9SMaxim Mikityanskiy }
4110c7bbeb9SMaxim Mikityanskiy 
4120c7bbeb9SMaxim Mikityanskiy static ssize_t store_ideapad_fan(struct device *dev,
4130c7bbeb9SMaxim Mikityanskiy 				 struct device_attribute *attr,
4140c7bbeb9SMaxim Mikityanskiy 				 const char *buf, size_t count)
4150c7bbeb9SMaxim Mikityanskiy {
4160c7bbeb9SMaxim Mikityanskiy 	int ret, state;
417331e0ea2SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(dev);
4180c7bbeb9SMaxim Mikityanskiy 
4190c7bbeb9SMaxim Mikityanskiy 	if (!count)
4200c7bbeb9SMaxim Mikityanskiy 		return 0;
4210c7bbeb9SMaxim Mikityanskiy 	if (sscanf(buf, "%i", &state) != 1)
4220c7bbeb9SMaxim Mikityanskiy 		return -EINVAL;
4230c7bbeb9SMaxim Mikityanskiy 	if (state < 0 || state > 4 || state == 3)
4240c7bbeb9SMaxim Mikityanskiy 		return -EINVAL;
425331e0ea2SZhang Rui 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
4260c7bbeb9SMaxim Mikityanskiy 	if (ret < 0)
4270c7bbeb9SMaxim Mikityanskiy 		return -EIO;
4280c7bbeb9SMaxim Mikityanskiy 	return count;
4290c7bbeb9SMaxim Mikityanskiy }
4300c7bbeb9SMaxim Mikityanskiy 
4310c7bbeb9SMaxim Mikityanskiy static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
4320c7bbeb9SMaxim Mikityanskiy 
43336ac0d43SRitesh Raj Sarraf static ssize_t touchpad_show(struct device *dev,
43436ac0d43SRitesh Raj Sarraf 			     struct device_attribute *attr,
43536ac0d43SRitesh Raj Sarraf 			     char *buf)
43636ac0d43SRitesh Raj Sarraf {
43736ac0d43SRitesh Raj Sarraf 	struct ideapad_private *priv = dev_get_drvdata(dev);
43836ac0d43SRitesh Raj Sarraf 	unsigned long result;
43936ac0d43SRitesh Raj Sarraf 
44036ac0d43SRitesh Raj Sarraf 	if (read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result))
44136ac0d43SRitesh Raj Sarraf 		return sprintf(buf, "-1\n");
44236ac0d43SRitesh Raj Sarraf 	return sprintf(buf, "%lu\n", result);
44336ac0d43SRitesh Raj Sarraf }
44436ac0d43SRitesh Raj Sarraf 
44546936fd6SArnd Bergmann /* Switch to RO for now: It might be revisited in the future */
44646936fd6SArnd Bergmann static ssize_t __maybe_unused touchpad_store(struct device *dev,
44736ac0d43SRitesh Raj Sarraf 					     struct device_attribute *attr,
44836ac0d43SRitesh Raj Sarraf 					     const char *buf, size_t count)
44936ac0d43SRitesh Raj Sarraf {
45036ac0d43SRitesh Raj Sarraf 	struct ideapad_private *priv = dev_get_drvdata(dev);
45136ac0d43SRitesh Raj Sarraf 	bool state;
45236ac0d43SRitesh Raj Sarraf 	int ret;
45336ac0d43SRitesh Raj Sarraf 
45436ac0d43SRitesh Raj Sarraf 	ret = kstrtobool(buf, &state);
45536ac0d43SRitesh Raj Sarraf 	if (ret)
45636ac0d43SRitesh Raj Sarraf 		return ret;
45736ac0d43SRitesh Raj Sarraf 
45836ac0d43SRitesh Raj Sarraf 	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
45936ac0d43SRitesh Raj Sarraf 	if (ret < 0)
46036ac0d43SRitesh Raj Sarraf 		return -EIO;
46136ac0d43SRitesh Raj Sarraf 	return count;
46236ac0d43SRitesh Raj Sarraf }
46336ac0d43SRitesh Raj Sarraf 
4647f363145SAndy Shevchenko static DEVICE_ATTR_RO(touchpad);
46536ac0d43SRitesh Raj Sarraf 
466ade50296SHao Wei Tee static ssize_t conservation_mode_show(struct device *dev,
467ade50296SHao Wei Tee 				struct device_attribute *attr,
468ade50296SHao Wei Tee 				char *buf)
469ade50296SHao Wei Tee {
470ade50296SHao Wei Tee 	struct ideapad_private *priv = dev_get_drvdata(dev);
471ade50296SHao Wei Tee 	unsigned long result;
472ade50296SHao Wei Tee 
473ade50296SHao Wei Tee 	if (method_gbmd(priv->adev->handle, &result))
474ade50296SHao Wei Tee 		return sprintf(buf, "-1\n");
475ade50296SHao Wei Tee 	return sprintf(buf, "%u\n", test_bit(BM_CONSERVATION_BIT, &result));
476ade50296SHao Wei Tee }
477ade50296SHao Wei Tee 
478ade50296SHao Wei Tee static ssize_t conservation_mode_store(struct device *dev,
479ade50296SHao Wei Tee 				 struct device_attribute *attr,
480ade50296SHao Wei Tee 				 const char *buf, size_t count)
481ade50296SHao Wei Tee {
482ade50296SHao Wei Tee 	struct ideapad_private *priv = dev_get_drvdata(dev);
483ade50296SHao Wei Tee 	bool state;
484ade50296SHao Wei Tee 	int ret;
485ade50296SHao Wei Tee 
486ade50296SHao Wei Tee 	ret = kstrtobool(buf, &state);
487ade50296SHao Wei Tee 	if (ret)
488ade50296SHao Wei Tee 		return ret;
489ade50296SHao Wei Tee 
490ade50296SHao Wei Tee 	ret = method_sbmc(priv->adev->handle, state ?
491ade50296SHao Wei Tee 					      BMCMD_CONSERVATION_ON :
492ade50296SHao Wei Tee 					      BMCMD_CONSERVATION_OFF);
493ade50296SHao Wei Tee 	if (ret < 0)
494ade50296SHao Wei Tee 		return -EIO;
495ade50296SHao Wei Tee 	return count;
496ade50296SHao Wei Tee }
497ade50296SHao Wei Tee 
498ade50296SHao Wei Tee static DEVICE_ATTR_RW(conservation_mode);
499ade50296SHao Wei Tee 
5003371f481SIke Panhc static struct attribute *ideapad_attributes[] = {
5013371f481SIke Panhc 	&dev_attr_camera_power.attr,
5020c7bbeb9SMaxim Mikityanskiy 	&dev_attr_fan_mode.attr,
50336ac0d43SRitesh Raj Sarraf 	&dev_attr_touchpad.attr,
504ade50296SHao Wei Tee 	&dev_attr_conservation_mode.attr,
5053371f481SIke Panhc 	NULL
5063371f481SIke Panhc };
5073371f481SIke Panhc 
508587a1f16SAl Viro static umode_t ideapad_is_visible(struct kobject *kobj,
509a84511f7SIke Panhc 				 struct attribute *attr,
510a84511f7SIke Panhc 				 int idx)
511a84511f7SIke Panhc {
512a84511f7SIke Panhc 	struct device *dev = container_of(kobj, struct device, kobj);
513a84511f7SIke Panhc 	struct ideapad_private *priv = dev_get_drvdata(dev);
514a84511f7SIke Panhc 	bool supported;
515a84511f7SIke Panhc 
516a84511f7SIke Panhc 	if (attr == &dev_attr_camera_power.attr)
517a84511f7SIke Panhc 		supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
5180c7bbeb9SMaxim Mikityanskiy 	else if (attr == &dev_attr_fan_mode.attr) {
5190c7bbeb9SMaxim Mikityanskiy 		unsigned long value;
520331e0ea2SZhang Rui 		supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
521331e0ea2SZhang Rui 					  &value);
522ade50296SHao Wei Tee 	} else if (attr == &dev_attr_conservation_mode.attr) {
523ade50296SHao Wei Tee 		supported = acpi_has_method(priv->adev->handle, "GBMD") &&
524ade50296SHao Wei Tee 			    acpi_has_method(priv->adev->handle, "SBMC");
5250c7bbeb9SMaxim Mikityanskiy 	} else
526a84511f7SIke Panhc 		supported = true;
527a84511f7SIke Panhc 
528a84511f7SIke Panhc 	return supported ? attr->mode : 0;
529a84511f7SIke Panhc }
530a84511f7SIke Panhc 
53149458e83SMathias Krause static const struct attribute_group ideapad_attribute_group = {
532a84511f7SIke Panhc 	.is_visible = ideapad_is_visible,
5333371f481SIke Panhc 	.attrs = ideapad_attributes
5343371f481SIke Panhc };
5353371f481SIke Panhc 
536a4b5a279SIke Panhc /*
537a4b5a279SIke Panhc  * Rfkill
538a4b5a279SIke Panhc  */
539c1f73658SIke Panhc struct ideapad_rfk_data {
540c1f73658SIke Panhc 	char *name;
541c1f73658SIke Panhc 	int cfgbit;
542c1f73658SIke Panhc 	int opcode;
543c1f73658SIke Panhc 	int type;
544c1f73658SIke Panhc };
545c1f73658SIke Panhc 
546b3d94d70SMathias Krause static const struct ideapad_rfk_data ideapad_rfk_data[] = {
5472be1dc21SIke Panhc 	{ "ideapad_wlan",    CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
5482be1dc21SIke Panhc 	{ "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
5492be1dc21SIke Panhc 	{ "ideapad_3g",        CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
550c1f73658SIke Panhc };
551c1f73658SIke Panhc 
55257ac3b05SIke Panhc static int ideapad_rfk_set(void *data, bool blocked)
55357ac3b05SIke Panhc {
554331e0ea2SZhang Rui 	struct ideapad_rfk_priv *priv = data;
5554b200b46SArnd Bergmann 	int opcode = ideapad_rfk_data[priv->dev].opcode;
55657ac3b05SIke Panhc 
5574b200b46SArnd Bergmann 	return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
55857ac3b05SIke Panhc }
55957ac3b05SIke Panhc 
5603d59dfcdSBhumika Goyal static const struct rfkill_ops ideapad_rfk_ops = {
56157ac3b05SIke Panhc 	.set_block = ideapad_rfk_set,
56257ac3b05SIke Panhc };
56357ac3b05SIke Panhc 
564923de84aSIke Panhc static void ideapad_sync_rfk_state(struct ideapad_private *priv)
56557ac3b05SIke Panhc {
566ce363c2bSHans de Goede 	unsigned long hw_blocked = 0;
56757ac3b05SIke Panhc 	int i;
56857ac3b05SIke Panhc 
569ce363c2bSHans de Goede 	if (priv->has_hw_rfkill_switch) {
570331e0ea2SZhang Rui 		if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
57157ac3b05SIke Panhc 			return;
57257ac3b05SIke Panhc 		hw_blocked = !hw_blocked;
573ce363c2bSHans de Goede 	}
57457ac3b05SIke Panhc 
575c1f73658SIke Panhc 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
57657ac3b05SIke Panhc 		if (priv->rfk[i])
57757ac3b05SIke Panhc 			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
57857ac3b05SIke Panhc }
57957ac3b05SIke Panhc 
58075a11f11SZhang Rui static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
58157ac3b05SIke Panhc {
58257ac3b05SIke Panhc 	int ret;
58357ac3b05SIke Panhc 	unsigned long sw_blocked;
58457ac3b05SIke Panhc 
585bfa97b7dSIke Panhc 	if (no_bt_rfkill &&
586bfa97b7dSIke Panhc 	    (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
587bfa97b7dSIke Panhc 		/* Force to enable bluetooth when no_bt_rfkill=1 */
588331e0ea2SZhang Rui 		write_ec_cmd(priv->adev->handle,
589bfa97b7dSIke Panhc 			     ideapad_rfk_data[dev].opcode, 1);
590bfa97b7dSIke Panhc 		return 0;
591bfa97b7dSIke Panhc 	}
592331e0ea2SZhang Rui 	priv->rfk_priv[dev].dev = dev;
593331e0ea2SZhang Rui 	priv->rfk_priv[dev].priv = priv;
594bfa97b7dSIke Panhc 
59575a11f11SZhang Rui 	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
596b5c37b79SZhang Rui 				      &priv->platform_device->dev,
59775a11f11SZhang Rui 				      ideapad_rfk_data[dev].type,
59875a11f11SZhang Rui 				      &ideapad_rfk_ops,
599331e0ea2SZhang Rui 				      &priv->rfk_priv[dev]);
60057ac3b05SIke Panhc 	if (!priv->rfk[dev])
60157ac3b05SIke Panhc 		return -ENOMEM;
60257ac3b05SIke Panhc 
603331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
60457ac3b05SIke Panhc 			 &sw_blocked)) {
60557ac3b05SIke Panhc 		rfkill_init_sw_state(priv->rfk[dev], 0);
60657ac3b05SIke Panhc 	} else {
60757ac3b05SIke Panhc 		sw_blocked = !sw_blocked;
60857ac3b05SIke Panhc 		rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
60957ac3b05SIke Panhc 	}
61057ac3b05SIke Panhc 
61157ac3b05SIke Panhc 	ret = rfkill_register(priv->rfk[dev]);
61257ac3b05SIke Panhc 	if (ret) {
61357ac3b05SIke Panhc 		rfkill_destroy(priv->rfk[dev]);
61457ac3b05SIke Panhc 		return ret;
61557ac3b05SIke Panhc 	}
61657ac3b05SIke Panhc 	return 0;
61757ac3b05SIke Panhc }
61857ac3b05SIke Panhc 
61975a11f11SZhang Rui static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
62057ac3b05SIke Panhc {
62157ac3b05SIke Panhc 	if (!priv->rfk[dev])
62257ac3b05SIke Panhc 		return;
62357ac3b05SIke Panhc 
62457ac3b05SIke Panhc 	rfkill_unregister(priv->rfk[dev]);
62557ac3b05SIke Panhc 	rfkill_destroy(priv->rfk[dev]);
62657ac3b05SIke Panhc }
62757ac3b05SIke Panhc 
62898ee6919SIke Panhc /*
62998ee6919SIke Panhc  * Platform device
63098ee6919SIke Panhc  */
631b5c37b79SZhang Rui static int ideapad_sysfs_init(struct ideapad_private *priv)
63298ee6919SIke Panhc {
633b5c37b79SZhang Rui 	return sysfs_create_group(&priv->platform_device->dev.kobj,
634c9f718d0SIke Panhc 				    &ideapad_attribute_group);
63598ee6919SIke Panhc }
63698ee6919SIke Panhc 
637b5c37b79SZhang Rui static void ideapad_sysfs_exit(struct ideapad_private *priv)
63898ee6919SIke Panhc {
6398693ae84SIke Panhc 	sysfs_remove_group(&priv->platform_device->dev.kobj,
640c9f718d0SIke Panhc 			   &ideapad_attribute_group);
64198ee6919SIke Panhc }
64298ee6919SIke Panhc 
643f63409aeSIke Panhc /*
644f63409aeSIke Panhc  * input device
645f63409aeSIke Panhc  */
646f63409aeSIke Panhc static const struct key_entry ideapad_keymap[] = {
647f43d9ec0SIke Panhc 	{ KE_KEY, 6,  { KEY_SWITCHVIDEOMODE } },
648296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 7,  { KEY_CAMERA } },
64948f67d62SAlex Hung 	{ KE_KEY, 8,  { KEY_MICMUTE } },
650296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 11, { KEY_F16 } },
651f43d9ec0SIke Panhc 	{ KE_KEY, 13, { KEY_WLAN } },
652f43d9ec0SIke Panhc 	{ KE_KEY, 16, { KEY_PROG1 } },
653f43d9ec0SIke Panhc 	{ KE_KEY, 17, { KEY_PROG2 } },
654296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 64, { KEY_PROG3 } },
655296f9fe0SMaxim Mikityanskiy 	{ KE_KEY, 65, { KEY_PROG4 } },
65607a4a4fcSMaxim Mikityanskiy 	{ KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
65707a4a4fcSMaxim Mikityanskiy 	{ KE_KEY, 67, { KEY_TOUCHPAD_ON } },
65874caab99SArnd Bergmann 	{ KE_KEY, 128, { KEY_ESC } },
65974caab99SArnd Bergmann 
660f63409aeSIke Panhc 	{ KE_END, 0 },
661f63409aeSIke Panhc };
662f63409aeSIke Panhc 
663b859f159SGreg Kroah-Hartman static int ideapad_input_init(struct ideapad_private *priv)
664f63409aeSIke Panhc {
665f63409aeSIke Panhc 	struct input_dev *inputdev;
666f63409aeSIke Panhc 	int error;
667f63409aeSIke Panhc 
668f63409aeSIke Panhc 	inputdev = input_allocate_device();
669b222cca6SJoe Perches 	if (!inputdev)
670f63409aeSIke Panhc 		return -ENOMEM;
671f63409aeSIke Panhc 
672f63409aeSIke Panhc 	inputdev->name = "Ideapad extra buttons";
673f63409aeSIke Panhc 	inputdev->phys = "ideapad/input0";
674f63409aeSIke Panhc 	inputdev->id.bustype = BUS_HOST;
6758693ae84SIke Panhc 	inputdev->dev.parent = &priv->platform_device->dev;
676f63409aeSIke Panhc 
677f63409aeSIke Panhc 	error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
678f63409aeSIke Panhc 	if (error) {
679f63409aeSIke Panhc 		pr_err("Unable to setup input device keymap\n");
680f63409aeSIke Panhc 		goto err_free_dev;
681f63409aeSIke Panhc 	}
682f63409aeSIke Panhc 
683f63409aeSIke Panhc 	error = input_register_device(inputdev);
684f63409aeSIke Panhc 	if (error) {
685f63409aeSIke Panhc 		pr_err("Unable to register input device\n");
686c973d4b5SMichał Kępień 		goto err_free_dev;
687f63409aeSIke Panhc 	}
688f63409aeSIke Panhc 
6898693ae84SIke Panhc 	priv->inputdev = inputdev;
690f63409aeSIke Panhc 	return 0;
691f63409aeSIke Panhc 
692f63409aeSIke Panhc err_free_dev:
693f63409aeSIke Panhc 	input_free_device(inputdev);
694f63409aeSIke Panhc 	return error;
695f63409aeSIke Panhc }
696f63409aeSIke Panhc 
6977451a55aSAxel Lin static void ideapad_input_exit(struct ideapad_private *priv)
698f63409aeSIke Panhc {
6998693ae84SIke Panhc 	input_unregister_device(priv->inputdev);
7008693ae84SIke Panhc 	priv->inputdev = NULL;
701f63409aeSIke Panhc }
702f63409aeSIke Panhc 
7038693ae84SIke Panhc static void ideapad_input_report(struct ideapad_private *priv,
7048693ae84SIke Panhc 				 unsigned long scancode)
705f63409aeSIke Panhc {
7068693ae84SIke Panhc 	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
707f63409aeSIke Panhc }
708f63409aeSIke Panhc 
709f43d9ec0SIke Panhc static void ideapad_input_novokey(struct ideapad_private *priv)
710f43d9ec0SIke Panhc {
711f43d9ec0SIke Panhc 	unsigned long long_pressed;
712f43d9ec0SIke Panhc 
713331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
714f43d9ec0SIke Panhc 		return;
715f43d9ec0SIke Panhc 	if (long_pressed)
716f43d9ec0SIke Panhc 		ideapad_input_report(priv, 17);
717f43d9ec0SIke Panhc 	else
718f43d9ec0SIke Panhc 		ideapad_input_report(priv, 16);
719f43d9ec0SIke Panhc }
720f43d9ec0SIke Panhc 
721296f9fe0SMaxim Mikityanskiy static void ideapad_check_special_buttons(struct ideapad_private *priv)
722296f9fe0SMaxim Mikityanskiy {
723296f9fe0SMaxim Mikityanskiy 	unsigned long bit, value;
724296f9fe0SMaxim Mikityanskiy 
725331e0ea2SZhang Rui 	read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
726296f9fe0SMaxim Mikityanskiy 
727296f9fe0SMaxim Mikityanskiy 	for (bit = 0; bit < 16; bit++) {
728296f9fe0SMaxim Mikityanskiy 		if (test_bit(bit, &value)) {
729296f9fe0SMaxim Mikityanskiy 			switch (bit) {
730a1ec56edSMaxim Mikityanskiy 			case 0:	/* Z580 */
731a1ec56edSMaxim Mikityanskiy 			case 6:	/* Z570 */
732296f9fe0SMaxim Mikityanskiy 				/* Thermal Management button */
733296f9fe0SMaxim Mikityanskiy 				ideapad_input_report(priv, 65);
734296f9fe0SMaxim Mikityanskiy 				break;
735296f9fe0SMaxim Mikityanskiy 			case 1:
736296f9fe0SMaxim Mikityanskiy 				/* OneKey Theater button */
737296f9fe0SMaxim Mikityanskiy 				ideapad_input_report(priv, 64);
738296f9fe0SMaxim Mikityanskiy 				break;
739a1ec56edSMaxim Mikityanskiy 			default:
740a1ec56edSMaxim Mikityanskiy 				pr_info("Unknown special button: %lu\n", bit);
741a1ec56edSMaxim Mikityanskiy 				break;
742296f9fe0SMaxim Mikityanskiy 			}
743296f9fe0SMaxim Mikityanskiy 		}
744296f9fe0SMaxim Mikityanskiy 	}
745296f9fe0SMaxim Mikityanskiy }
746296f9fe0SMaxim Mikityanskiy 
747a4b5a279SIke Panhc /*
748a4ecbb8aSIke Panhc  * backlight
749a4ecbb8aSIke Panhc  */
750a4ecbb8aSIke Panhc static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
751a4ecbb8aSIke Panhc {
752331e0ea2SZhang Rui 	struct ideapad_private *priv = bl_get_data(blightdev);
753a4ecbb8aSIke Panhc 	unsigned long now;
754a4ecbb8aSIke Panhc 
755331e0ea2SZhang Rui 	if (!priv)
756331e0ea2SZhang Rui 		return -EINVAL;
757331e0ea2SZhang Rui 
758331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
759a4ecbb8aSIke Panhc 		return -EIO;
760a4ecbb8aSIke Panhc 	return now;
761a4ecbb8aSIke Panhc }
762a4ecbb8aSIke Panhc 
763a4ecbb8aSIke Panhc static int ideapad_backlight_update_status(struct backlight_device *blightdev)
764a4ecbb8aSIke Panhc {
765331e0ea2SZhang Rui 	struct ideapad_private *priv = bl_get_data(blightdev);
766331e0ea2SZhang Rui 
767331e0ea2SZhang Rui 	if (!priv)
768331e0ea2SZhang Rui 		return -EINVAL;
769331e0ea2SZhang Rui 
770331e0ea2SZhang Rui 	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
7712be1dc21SIke Panhc 			 blightdev->props.brightness))
772a4ecbb8aSIke Panhc 		return -EIO;
773331e0ea2SZhang Rui 	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
774a4ecbb8aSIke Panhc 			 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
775a4ecbb8aSIke Panhc 		return -EIO;
776a4ecbb8aSIke Panhc 
777a4ecbb8aSIke Panhc 	return 0;
778a4ecbb8aSIke Panhc }
779a4ecbb8aSIke Panhc 
780a4ecbb8aSIke Panhc static const struct backlight_ops ideapad_backlight_ops = {
781a4ecbb8aSIke Panhc 	.get_brightness = ideapad_backlight_get_brightness,
782a4ecbb8aSIke Panhc 	.update_status = ideapad_backlight_update_status,
783a4ecbb8aSIke Panhc };
784a4ecbb8aSIke Panhc 
785a4ecbb8aSIke Panhc static int ideapad_backlight_init(struct ideapad_private *priv)
786a4ecbb8aSIke Panhc {
787a4ecbb8aSIke Panhc 	struct backlight_device *blightdev;
788a4ecbb8aSIke Panhc 	struct backlight_properties props;
789a4ecbb8aSIke Panhc 	unsigned long max, now, power;
790a4ecbb8aSIke Panhc 
791331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
792a4ecbb8aSIke Panhc 		return -EIO;
793331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
794a4ecbb8aSIke Panhc 		return -EIO;
795331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
796a4ecbb8aSIke Panhc 		return -EIO;
797a4ecbb8aSIke Panhc 
798a4ecbb8aSIke Panhc 	memset(&props, 0, sizeof(struct backlight_properties));
799a4ecbb8aSIke Panhc 	props.max_brightness = max;
800a4ecbb8aSIke Panhc 	props.type = BACKLIGHT_PLATFORM;
801a4ecbb8aSIke Panhc 	blightdev = backlight_device_register("ideapad",
802a4ecbb8aSIke Panhc 					      &priv->platform_device->dev,
803a4ecbb8aSIke Panhc 					      priv,
804a4ecbb8aSIke Panhc 					      &ideapad_backlight_ops,
805a4ecbb8aSIke Panhc 					      &props);
806a4ecbb8aSIke Panhc 	if (IS_ERR(blightdev)) {
807a4ecbb8aSIke Panhc 		pr_err("Could not register backlight device\n");
808a4ecbb8aSIke Panhc 		return PTR_ERR(blightdev);
809a4ecbb8aSIke Panhc 	}
810a4ecbb8aSIke Panhc 
811a4ecbb8aSIke Panhc 	priv->blightdev = blightdev;
812a4ecbb8aSIke Panhc 	blightdev->props.brightness = now;
813a4ecbb8aSIke Panhc 	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
814a4ecbb8aSIke Panhc 	backlight_update_status(blightdev);
815a4ecbb8aSIke Panhc 
816a4ecbb8aSIke Panhc 	return 0;
817a4ecbb8aSIke Panhc }
818a4ecbb8aSIke Panhc 
819a4ecbb8aSIke Panhc static void ideapad_backlight_exit(struct ideapad_private *priv)
820a4ecbb8aSIke Panhc {
821a4ecbb8aSIke Panhc 	backlight_device_unregister(priv->blightdev);
822a4ecbb8aSIke Panhc 	priv->blightdev = NULL;
823a4ecbb8aSIke Panhc }
824a4ecbb8aSIke Panhc 
825a4ecbb8aSIke Panhc static void ideapad_backlight_notify_power(struct ideapad_private *priv)
826a4ecbb8aSIke Panhc {
827a4ecbb8aSIke Panhc 	unsigned long power;
828a4ecbb8aSIke Panhc 	struct backlight_device *blightdev = priv->blightdev;
829a4ecbb8aSIke Panhc 
830d4afc775SRene Bollford 	if (!blightdev)
831d4afc775SRene Bollford 		return;
832331e0ea2SZhang Rui 	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
833a4ecbb8aSIke Panhc 		return;
834a4ecbb8aSIke Panhc 	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
835a4ecbb8aSIke Panhc }
836a4ecbb8aSIke Panhc 
837a4ecbb8aSIke Panhc static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
838a4ecbb8aSIke Panhc {
839a4ecbb8aSIke Panhc 	unsigned long now;
840a4ecbb8aSIke Panhc 
841a4ecbb8aSIke Panhc 	/* if we control brightness via acpi video driver */
842a4ecbb8aSIke Panhc 	if (priv->blightdev == NULL) {
843331e0ea2SZhang Rui 		read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
844a4ecbb8aSIke Panhc 		return;
845a4ecbb8aSIke Panhc 	}
846a4ecbb8aSIke Panhc 
847a4ecbb8aSIke Panhc 	backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
848a4ecbb8aSIke Panhc }
849a4ecbb8aSIke Panhc 
850a4ecbb8aSIke Panhc /*
851a4b5a279SIke Panhc  * module init/exit
852a4b5a279SIke Panhc  */
85375a11f11SZhang Rui static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
85407a4a4fcSMaxim Mikityanskiy {
85507a4a4fcSMaxim Mikityanskiy 	unsigned long value;
85607a4a4fcSMaxim Mikityanskiy 
85707a4a4fcSMaxim Mikityanskiy 	/* Without reading from EC touchpad LED doesn't switch state */
85875a11f11SZhang Rui 	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
85907a4a4fcSMaxim Mikityanskiy 		/* Some IdeaPads don't really turn off touchpad - they only
86007a4a4fcSMaxim Mikityanskiy 		 * switch the LED state. We (de)activate KBC AUX port to turn
86107a4a4fcSMaxim Mikityanskiy 		 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
86207a4a4fcSMaxim Mikityanskiy 		 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
86307a4a4fcSMaxim Mikityanskiy 		unsigned char param;
86407a4a4fcSMaxim Mikityanskiy 		i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
86507a4a4fcSMaxim Mikityanskiy 			      I8042_CMD_AUX_DISABLE);
86607a4a4fcSMaxim Mikityanskiy 		ideapad_input_report(priv, value ? 67 : 66);
86707a4a4fcSMaxim Mikityanskiy 	}
86807a4a4fcSMaxim Mikityanskiy }
86907a4a4fcSMaxim Mikityanskiy 
870b5c37b79SZhang Rui static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
87157ac3b05SIke Panhc {
872b5c37b79SZhang Rui 	struct ideapad_private *priv = data;
87357ac3b05SIke Panhc 	unsigned long vpc1, vpc2, vpc_bit;
87457ac3b05SIke Panhc 
8752be1dc21SIke Panhc 	if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
87657ac3b05SIke Panhc 		return;
8772be1dc21SIke Panhc 	if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
87857ac3b05SIke Panhc 		return;
87957ac3b05SIke Panhc 
88057ac3b05SIke Panhc 	vpc1 = (vpc2 << 8) | vpc1;
88157ac3b05SIke Panhc 	for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
88257ac3b05SIke Panhc 		if (test_bit(vpc_bit, &vpc1)) {
883a4ecbb8aSIke Panhc 			switch (vpc_bit) {
884a4ecbb8aSIke Panhc 			case 9:
885923de84aSIke Panhc 				ideapad_sync_rfk_state(priv);
886a4ecbb8aSIke Panhc 				break;
88720a769c1SIke Panhc 			case 13:
888296f9fe0SMaxim Mikityanskiy 			case 11:
88948f67d62SAlex Hung 			case 8:
890296f9fe0SMaxim Mikityanskiy 			case 7:
89120a769c1SIke Panhc 			case 6:
89220a769c1SIke Panhc 				ideapad_input_report(priv, vpc_bit);
89320a769c1SIke Panhc 				break;
89407a4a4fcSMaxim Mikityanskiy 			case 5:
89575a11f11SZhang Rui 				ideapad_sync_touchpad_state(priv);
89607a4a4fcSMaxim Mikityanskiy 				break;
897a4ecbb8aSIke Panhc 			case 4:
898a4ecbb8aSIke Panhc 				ideapad_backlight_notify_brightness(priv);
899a4ecbb8aSIke Panhc 				break;
900f43d9ec0SIke Panhc 			case 3:
901f43d9ec0SIke Panhc 				ideapad_input_novokey(priv);
902f43d9ec0SIke Panhc 				break;
903a4ecbb8aSIke Panhc 			case 2:
904a4ecbb8aSIke Panhc 				ideapad_backlight_notify_power(priv);
905a4ecbb8aSIke Panhc 				break;
906296f9fe0SMaxim Mikityanskiy 			case 0:
907296f9fe0SMaxim Mikityanskiy 				ideapad_check_special_buttons(priv);
908296f9fe0SMaxim Mikityanskiy 				break;
9093cfd956bSHao Wei Tee 			case 1:
9103cfd956bSHao Wei Tee 				/* Some IdeaPads report event 1 every ~20
9113cfd956bSHao Wei Tee 				 * seconds while on battery power; some
9123cfd956bSHao Wei Tee 				 * report this when changing to/from tablet
9133cfd956bSHao Wei Tee 				 * mode. Squelch this event.
9143cfd956bSHao Wei Tee 				 */
9153cfd956bSHao Wei Tee 				break;
916a4ecbb8aSIke Panhc 			default:
91720a769c1SIke Panhc 				pr_info("Unknown event: %lu\n", vpc_bit);
91857ac3b05SIke Panhc 			}
91957ac3b05SIke Panhc 		}
92057ac3b05SIke Panhc 	}
921a4ecbb8aSIke Panhc }
92257ac3b05SIke Panhc 
92374caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
92474caab99SArnd Bergmann static void ideapad_wmi_notify(u32 value, void *context)
92574caab99SArnd Bergmann {
92674caab99SArnd Bergmann 	switch (value) {
92774caab99SArnd Bergmann 	case 128:
92874caab99SArnd Bergmann 		ideapad_input_report(context, value);
92974caab99SArnd Bergmann 		break;
93074caab99SArnd Bergmann 	default:
93174caab99SArnd Bergmann 		pr_info("Unknown WMI event %u\n", value);
93274caab99SArnd Bergmann 	}
93374caab99SArnd Bergmann }
93474caab99SArnd Bergmann #endif
93574caab99SArnd Bergmann 
936ce363c2bSHans de Goede /*
937ce363c2bSHans de Goede  * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
938ce363c2bSHans de Goede  * always results in 0 on these models, causing ideapad_laptop to wrongly
939ce363c2bSHans de Goede  * report all radios as hardware-blocked.
940ce363c2bSHans de Goede  */
941b3d94d70SMathias Krause static const struct dmi_system_id no_hw_rfkill_list[] = {
94285093f79SHans de Goede 	{
943ae7c8cbaSJiaxun Yang 		.ident = "Lenovo RESCUER R720-15IKBN",
944ae7c8cbaSJiaxun Yang 		.matches = {
945ae7c8cbaSJiaxun Yang 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
946ae7c8cbaSJiaxun Yang 			DMI_MATCH(DMI_BOARD_NAME, "80WW"),
947ae7c8cbaSJiaxun Yang 		},
948ae7c8cbaSJiaxun Yang 	},
949ae7c8cbaSJiaxun Yang 	{
9509b071a43SPhilippe Coval 		.ident = "Lenovo G40-30",
9519b071a43SPhilippe Coval 		.matches = {
9529b071a43SPhilippe Coval 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9539b071a43SPhilippe Coval 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G40-30"),
9549b071a43SPhilippe Coval 		},
9559b071a43SPhilippe Coval 	},
9569b071a43SPhilippe Coval 	{
9574fa9dabcSDmitry Tunin 		.ident = "Lenovo G50-30",
9584fa9dabcSDmitry Tunin 		.matches = {
9594fa9dabcSDmitry Tunin 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9604fa9dabcSDmitry Tunin 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G50-30"),
9614fa9dabcSDmitry Tunin 		},
9624fa9dabcSDmitry Tunin 	},
9634fa9dabcSDmitry Tunin 	{
964710c059cSYang Jiaxun 		.ident = "Lenovo V310-14IKB",
965710c059cSYang Jiaxun 		.matches = {
966710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
967710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14IKB"),
968710c059cSYang Jiaxun 		},
969710c059cSYang Jiaxun 	},
970710c059cSYang Jiaxun 	{
971710c059cSYang Jiaxun 		.ident = "Lenovo V310-14ISK",
972710c059cSYang Jiaxun 		.matches = {
973710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
974710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14ISK"),
975710c059cSYang Jiaxun 		},
976710c059cSYang Jiaxun 	},
977710c059cSYang Jiaxun 	{
978710c059cSYang Jiaxun 		.ident = "Lenovo V310-15IKB",
979710c059cSYang Jiaxun 		.matches = {
980710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
981710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15IKB"),
982710c059cSYang Jiaxun 		},
983710c059cSYang Jiaxun 	},
984710c059cSYang Jiaxun 	{
985ccc7179fSAndy Shevchenko 		.ident = "Lenovo V310-15ISK",
986ccc7179fSAndy Shevchenko 		.matches = {
987ccc7179fSAndy Shevchenko 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
988ccc7179fSAndy Shevchenko 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15ISK"),
989ccc7179fSAndy Shevchenko 		},
990ccc7179fSAndy Shevchenko 	},
991ccc7179fSAndy Shevchenko 	{
9920df4b805SSven Eckelmann 		.ident = "Lenovo V510-15IKB",
9930df4b805SSven Eckelmann 		.matches = {
9940df4b805SSven Eckelmann 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
9950df4b805SSven Eckelmann 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V510-15IKB"),
9960df4b805SSven Eckelmann 		},
9970df4b805SSven Eckelmann 	},
9980df4b805SSven Eckelmann 	{
999710c059cSYang Jiaxun 		.ident = "Lenovo ideapad 300-15IBR",
1000710c059cSYang Jiaxun 		.matches = {
1001710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1002710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IBR"),
1003710c059cSYang Jiaxun 		},
1004710c059cSYang Jiaxun 	},
1005710c059cSYang Jiaxun 	{
1006710c059cSYang Jiaxun 		.ident = "Lenovo ideapad 300-15IKB",
1007710c059cSYang Jiaxun 		.matches = {
1008710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1009710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IKB"),
1010710c059cSYang Jiaxun 		},
1011710c059cSYang Jiaxun 	},
1012710c059cSYang Jiaxun 	{
1013710c059cSYang Jiaxun 		.ident = "Lenovo ideapad 300S-11IBR",
1014710c059cSYang Jiaxun 		.matches = {
1015710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1016710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300S-11BR"),
1017710c059cSYang Jiaxun 		},
1018710c059cSYang Jiaxun 	},
1019710c059cSYang Jiaxun 	{
1020710c059cSYang Jiaxun 		.ident = "Lenovo ideapad 310-15ABR",
1021710c059cSYang Jiaxun 		.matches = {
1022710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1023710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ABR"),
1024710c059cSYang Jiaxun 		},
1025710c059cSYang Jiaxun 	},
1026710c059cSYang Jiaxun 	{
1027710c059cSYang Jiaxun 		.ident = "Lenovo ideapad 310-15IAP",
1028710c059cSYang Jiaxun 		.matches = {
1029710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1030710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IAP"),
1031710c059cSYang Jiaxun 		},
1032710c059cSYang Jiaxun 	},
1033710c059cSYang Jiaxun 	{
10341f3bc53dSSven Rebhan 		.ident = "Lenovo ideapad 310-15IKB",
10351f3bc53dSSven Rebhan 		.matches = {
10361f3bc53dSSven Rebhan 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
10371f3bc53dSSven Rebhan 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IKB"),
10381f3bc53dSSven Rebhan 		},
10391f3bc53dSSven Rebhan 	},
10401f3bc53dSSven Rebhan 	{
1041710c059cSYang Jiaxun 		.ident = "Lenovo ideapad 310-15ISK",
1042710c059cSYang Jiaxun 		.matches = {
1043710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1044710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ISK"),
1045710c059cSYang Jiaxun 		},
1046710c059cSYang Jiaxun 	},
1047710c059cSYang Jiaxun 	{
1048710c059cSYang Jiaxun 		.ident = "Lenovo ideapad Y700-14ISK",
1049710c059cSYang Jiaxun 		.matches = {
1050710c059cSYang Jiaxun 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1051710c059cSYang Jiaxun 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-14ISK"),
1052710c059cSYang Jiaxun 		},
1053710c059cSYang Jiaxun 	},
1054710c059cSYang Jiaxun 	{
1055e2970468Svelemas 		.ident = "Lenovo ideapad Y700-15ACZ",
1056e2970468Svelemas 		.matches = {
1057e2970468Svelemas 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1058e2970468Svelemas 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ACZ"),
1059e2970468Svelemas 		},
1060e2970468Svelemas 	},
1061e2970468Svelemas 	{
10624db9675dSJohn Dahlstrom 		.ident = "Lenovo ideapad Y700-15ISK",
10634db9675dSJohn Dahlstrom 		.matches = {
10644db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
10654db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ISK"),
10664db9675dSJohn Dahlstrom 		},
10674db9675dSJohn Dahlstrom 	},
10684db9675dSJohn Dahlstrom 	{
10694db9675dSJohn Dahlstrom 		.ident = "Lenovo ideapad Y700 Touch-15ISK",
10704db9675dSJohn Dahlstrom 		.matches = {
10714db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
10724db9675dSJohn Dahlstrom 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700 Touch-15ISK"),
10734db9675dSJohn Dahlstrom 		},
10744db9675dSJohn Dahlstrom 	},
10754db9675dSJohn Dahlstrom 	{
1076edde316aSJosh Boyer 		.ident = "Lenovo ideapad Y700-17ISK",
1077edde316aSJosh Boyer 		.matches = {
1078edde316aSJosh Boyer 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1079edde316aSJosh Boyer 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-17ISK"),
1080edde316aSJosh Boyer 		},
1081edde316aSJosh Boyer 	},
1082edde316aSJosh Boyer 	{
10837caf0c1eSTakashi Iwai 		.ident = "Lenovo ideapad MIIX 720-12IKB",
10847caf0c1eSTakashi Iwai 		.matches = {
10857caf0c1eSTakashi Iwai 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
10867caf0c1eSTakashi Iwai 			DMI_MATCH(DMI_PRODUCT_VERSION, "MIIX 720-12IKB"),
10877caf0c1eSTakashi Iwai 		},
10887caf0c1eSTakashi Iwai 	},
10897caf0c1eSTakashi Iwai 	{
10905d9f40b5SOlle Liljenzin 		.ident = "Lenovo Legion Y520-15IKBN",
10915d9f40b5SOlle Liljenzin 		.matches = {
10925d9f40b5SOlle Liljenzin 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
10935d9f40b5SOlle Liljenzin 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y520-15IKBN"),
10945d9f40b5SOlle Liljenzin 		},
10955d9f40b5SOlle Liljenzin 	},
10965d9f40b5SOlle Liljenzin 	{
109728d71ae9SOlle Liljenzin 		.ident = "Lenovo Legion Y720-15IKB",
109828d71ae9SOlle Liljenzin 		.matches = {
109928d71ae9SOlle Liljenzin 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
110028d71ae9SOlle Liljenzin 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKB"),
110128d71ae9SOlle Liljenzin 		},
110228d71ae9SOlle Liljenzin 	},
110328d71ae9SOlle Liljenzin 	{
1104b2f2fe20SOlle Liljenzin 		.ident = "Lenovo Legion Y720-15IKBN",
1105b2f2fe20SOlle Liljenzin 		.matches = {
1106b2f2fe20SOlle Liljenzin 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1107b2f2fe20SOlle Liljenzin 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKBN"),
1108b2f2fe20SOlle Liljenzin 		},
1109b2f2fe20SOlle Liljenzin 	},
1110b2f2fe20SOlle Liljenzin 	{
1111ce363c2bSHans de Goede 		.ident = "Lenovo Yoga 2 11 / 13 / Pro",
111285093f79SHans de Goede 		.matches = {
111385093f79SHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1114ce363c2bSHans de Goede 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
111585093f79SHans de Goede 		},
111685093f79SHans de Goede 	},
1117725c7f61SStephan Mueller 	{
11186d212b8aSSebastian Krzyszkowiak 		.ident = "Lenovo Yoga 2 11 / 13 / Pro",
11196d212b8aSSebastian Krzyszkowiak 		.matches = {
11206d212b8aSSebastian Krzyszkowiak 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
11216d212b8aSSebastian Krzyszkowiak 			DMI_MATCH(DMI_BOARD_NAME, "Yoga2"),
11226d212b8aSSebastian Krzyszkowiak 		},
11236d212b8aSSebastian Krzyszkowiak 	},
11246d212b8aSSebastian Krzyszkowiak 	{
1125c789fffcSArnd Bergmann 		.ident = "Lenovo Yoga 3 1170 / 1470",
1126c789fffcSArnd Bergmann 		.matches = {
1127c789fffcSArnd Bergmann 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1128c789fffcSArnd Bergmann 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 3"),
1129c789fffcSArnd Bergmann 		},
1130c789fffcSArnd Bergmann 	},
1131c789fffcSArnd Bergmann 	{
1132725c7f61SStephan Mueller 		.ident = "Lenovo Yoga 3 Pro 1370",
1133725c7f61SStephan Mueller 		.matches = {
1134725c7f61SStephan Mueller 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1135c789fffcSArnd Bergmann 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
1136725c7f61SStephan Mueller 		},
1137725c7f61SStephan Mueller 	},
1138f71c882dSHans de Goede 	{
11396b31de3eSJosh Boyer 		.ident = "Lenovo Yoga 700",
11406b31de3eSJosh Boyer 		.matches = {
11416b31de3eSJosh Boyer 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
11426b31de3eSJosh Boyer 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 700"),
11436b31de3eSJosh Boyer 		},
11446b31de3eSJosh Boyer 	},
11456b31de3eSJosh Boyer 	{
1146f71c882dSHans de Goede 		.ident = "Lenovo Yoga 900",
1147f71c882dSHans de Goede 		.matches = {
1148f71c882dSHans de Goede 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1149f71c882dSHans de Goede 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 900"),
1150f71c882dSHans de Goede 		},
1151f71c882dSHans de Goede 	},
115240c30bbfSBrian Masney 	{
1153446647d4SMika Westerberg 		.ident = "Lenovo Yoga 900",
1154446647d4SMika Westerberg 		.matches = {
1155446647d4SMika Westerberg 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1156446647d4SMika Westerberg 			DMI_MATCH(DMI_BOARD_NAME, "VIUU4"),
1157446647d4SMika Westerberg 		},
1158446647d4SMika Westerberg 	},
1159446647d4SMika Westerberg 	{
116040c30bbfSBrian Masney 		.ident = "Lenovo YOGA 910-13IKB",
116140c30bbfSBrian Masney 		.matches = {
116240c30bbfSBrian Masney 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
116340c30bbfSBrian Masney 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 910-13IKB"),
116440c30bbfSBrian Masney 		},
116540c30bbfSBrian Masney 	},
1166b231669cSPhilipp Hug 	{
1167b231669cSPhilipp Hug 		.ident = "Lenovo YOGA 920-13IKB",
1168b231669cSPhilipp Hug 		.matches = {
1169b231669cSPhilipp Hug 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1170b231669cSPhilipp Hug 			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 920-13IKB"),
1171b231669cSPhilipp Hug 		},
1172b231669cSPhilipp Hug 	},
117385093f79SHans de Goede 	{}
117485093f79SHans de Goede };
117585093f79SHans de Goede 
1176b5c37b79SZhang Rui static int ideapad_acpi_add(struct platform_device *pdev)
1177b5c37b79SZhang Rui {
1178b5c37b79SZhang Rui 	int ret, i;
1179b5c37b79SZhang Rui 	int cfg;
1180b5c37b79SZhang Rui 	struct ideapad_private *priv;
1181b5c37b79SZhang Rui 	struct acpi_device *adev;
1182b5c37b79SZhang Rui 
1183b5c37b79SZhang Rui 	ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
1184b5c37b79SZhang Rui 	if (ret)
1185b5c37b79SZhang Rui 		return -ENODEV;
1186b5c37b79SZhang Rui 
1187b5c37b79SZhang Rui 	if (read_method_int(adev->handle, "_CFG", &cfg))
1188b5c37b79SZhang Rui 		return -ENODEV;
1189b5c37b79SZhang Rui 
1190b3facd7bSHimangi Saraogi 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1191b5c37b79SZhang Rui 	if (!priv)
1192b5c37b79SZhang Rui 		return -ENOMEM;
1193b5c37b79SZhang Rui 
1194b5c37b79SZhang Rui 	dev_set_drvdata(&pdev->dev, priv);
1195b5c37b79SZhang Rui 	priv->cfg = cfg;
1196b5c37b79SZhang Rui 	priv->adev = adev;
1197b5c37b79SZhang Rui 	priv->platform_device = pdev;
1198ce363c2bSHans de Goede 	priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
1199b5c37b79SZhang Rui 
1200b5c37b79SZhang Rui 	ret = ideapad_sysfs_init(priv);
1201b5c37b79SZhang Rui 	if (ret)
1202b3facd7bSHimangi Saraogi 		return ret;
1203b5c37b79SZhang Rui 
1204b5c37b79SZhang Rui 	ret = ideapad_debugfs_init(priv);
1205b5c37b79SZhang Rui 	if (ret)
1206b5c37b79SZhang Rui 		goto debugfs_failed;
1207b5c37b79SZhang Rui 
1208b5c37b79SZhang Rui 	ret = ideapad_input_init(priv);
1209b5c37b79SZhang Rui 	if (ret)
1210b5c37b79SZhang Rui 		goto input_failed;
1211b5c37b79SZhang Rui 
1212ce363c2bSHans de Goede 	/*
1213ce363c2bSHans de Goede 	 * On some models without a hw-switch (the yoga 2 13 at least)
1214ce363c2bSHans de Goede 	 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1215ce363c2bSHans de Goede 	 */
1216ce363c2bSHans de Goede 	if (!priv->has_hw_rfkill_switch)
1217ce363c2bSHans de Goede 		write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1218ce363c2bSHans de Goede 
121985093f79SHans de Goede 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1220b5c37b79SZhang Rui 		if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1221b5c37b79SZhang Rui 			ideapad_register_rfkill(priv, i);
1222ce363c2bSHans de Goede 
1223b5c37b79SZhang Rui 	ideapad_sync_rfk_state(priv);
1224b5c37b79SZhang Rui 	ideapad_sync_touchpad_state(priv);
1225b5c37b79SZhang Rui 
122626bff5f0SHans de Goede 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1227b5c37b79SZhang Rui 		ret = ideapad_backlight_init(priv);
1228b5c37b79SZhang Rui 		if (ret && ret != -ENODEV)
1229b5c37b79SZhang Rui 			goto backlight_failed;
1230b5c37b79SZhang Rui 	}
1231b5c37b79SZhang Rui 	ret = acpi_install_notify_handler(adev->handle,
1232b5c37b79SZhang Rui 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
1233b5c37b79SZhang Rui 	if (ret)
1234b5c37b79SZhang Rui 		goto notification_failed;
12352d98e0b9SArnd Bergmann 
123674caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
12372d98e0b9SArnd Bergmann 	for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
12382d98e0b9SArnd Bergmann 		ret = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
12392d98e0b9SArnd Bergmann 						 ideapad_wmi_notify, priv);
12402d98e0b9SArnd Bergmann 		if (ret == AE_OK) {
12412d98e0b9SArnd Bergmann 			priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
12422d98e0b9SArnd Bergmann 			break;
12432d98e0b9SArnd Bergmann 		}
12442d98e0b9SArnd Bergmann 	}
124574caab99SArnd Bergmann 	if (ret != AE_OK && ret != AE_NOT_EXIST)
124674caab99SArnd Bergmann 		goto notification_failed_wmi;
124774caab99SArnd Bergmann #endif
1248b5c37b79SZhang Rui 
1249b5c37b79SZhang Rui 	return 0;
125074caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
125174caab99SArnd Bergmann notification_failed_wmi:
125274caab99SArnd Bergmann 	acpi_remove_notify_handler(priv->adev->handle,
125374caab99SArnd Bergmann 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
125474caab99SArnd Bergmann #endif
1255b5c37b79SZhang Rui notification_failed:
1256b5c37b79SZhang Rui 	ideapad_backlight_exit(priv);
1257b5c37b79SZhang Rui backlight_failed:
1258b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1259b5c37b79SZhang Rui 		ideapad_unregister_rfkill(priv, i);
1260b5c37b79SZhang Rui 	ideapad_input_exit(priv);
1261b5c37b79SZhang Rui input_failed:
1262b5c37b79SZhang Rui 	ideapad_debugfs_exit(priv);
1263b5c37b79SZhang Rui debugfs_failed:
1264b5c37b79SZhang Rui 	ideapad_sysfs_exit(priv);
1265b5c37b79SZhang Rui 	return ret;
1266b5c37b79SZhang Rui }
1267b5c37b79SZhang Rui 
1268b5c37b79SZhang Rui static int ideapad_acpi_remove(struct platform_device *pdev)
1269b5c37b79SZhang Rui {
1270b5c37b79SZhang Rui 	struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1271b5c37b79SZhang Rui 	int i;
1272b5c37b79SZhang Rui 
127374caab99SArnd Bergmann #if IS_ENABLED(CONFIG_ACPI_WMI)
12742d98e0b9SArnd Bergmann 	if (priv->fnesc_guid)
12752d98e0b9SArnd Bergmann 		wmi_remove_notify_handler(priv->fnesc_guid);
127674caab99SArnd Bergmann #endif
1277b5c37b79SZhang Rui 	acpi_remove_notify_handler(priv->adev->handle,
1278b5c37b79SZhang Rui 		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1279b5c37b79SZhang Rui 	ideapad_backlight_exit(priv);
1280b5c37b79SZhang Rui 	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1281b5c37b79SZhang Rui 		ideapad_unregister_rfkill(priv, i);
1282b5c37b79SZhang Rui 	ideapad_input_exit(priv);
1283b5c37b79SZhang Rui 	ideapad_debugfs_exit(priv);
1284b5c37b79SZhang Rui 	ideapad_sysfs_exit(priv);
1285b5c37b79SZhang Rui 	dev_set_drvdata(&pdev->dev, NULL);
1286b5c37b79SZhang Rui 
1287b5c37b79SZhang Rui 	return 0;
1288b5c37b79SZhang Rui }
1289b5c37b79SZhang Rui 
129011fa8da5SZhang Rui #ifdef CONFIG_PM_SLEEP
129107a4a4fcSMaxim Mikityanskiy static int ideapad_acpi_resume(struct device *device)
129207a4a4fcSMaxim Mikityanskiy {
129375a11f11SZhang Rui 	struct ideapad_private *priv;
129475a11f11SZhang Rui 
129575a11f11SZhang Rui 	if (!device)
129675a11f11SZhang Rui 		return -EINVAL;
129775a11f11SZhang Rui 	priv = dev_get_drvdata(device);
129875a11f11SZhang Rui 
129975a11f11SZhang Rui 	ideapad_sync_rfk_state(priv);
130075a11f11SZhang Rui 	ideapad_sync_touchpad_state(priv);
130107a4a4fcSMaxim Mikityanskiy 	return 0;
130207a4a4fcSMaxim Mikityanskiy }
1303b5c37b79SZhang Rui #endif
130407a4a4fcSMaxim Mikityanskiy static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
130507a4a4fcSMaxim Mikityanskiy 
1306b5c37b79SZhang Rui static const struct acpi_device_id ideapad_device_ids[] = {
1307b5c37b79SZhang Rui 	{ "VPC2004", 0},
1308b5c37b79SZhang Rui 	{ "", 0},
130957ac3b05SIke Panhc };
1310b5c37b79SZhang Rui MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1311b5c37b79SZhang Rui 
1312b5c37b79SZhang Rui static struct platform_driver ideapad_acpi_driver = {
1313b5c37b79SZhang Rui 	.probe = ideapad_acpi_add,
1314b5c37b79SZhang Rui 	.remove = ideapad_acpi_remove,
1315b5c37b79SZhang Rui 	.driver = {
1316b5c37b79SZhang Rui 		.name   = "ideapad_acpi",
1317b5c37b79SZhang Rui 		.pm     = &ideapad_pm,
1318b5c37b79SZhang Rui 		.acpi_match_table = ACPI_PTR(ideapad_device_ids),
1319b5c37b79SZhang Rui 	},
1320b5c37b79SZhang Rui };
1321b5c37b79SZhang Rui 
1322b5c37b79SZhang Rui module_platform_driver(ideapad_acpi_driver);
132357ac3b05SIke Panhc 
132457ac3b05SIke Panhc MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
132557ac3b05SIke Panhc MODULE_DESCRIPTION("IdeaPad ACPI Extras");
132657ac3b05SIke Panhc MODULE_LICENSE("GPL");
1327