Linux Audio

Check our new training course

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