Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
   4 *
   5 *  Copyright © 2010 Intel Corporation
   6 *  Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   7 */
   8
   9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10
  11#include <linux/acpi.h>
  12#include <linux/backlight.h>
  13#include <linux/bitops.h>
  14#include <linux/bug.h>
  15#include <linux/debugfs.h>
  16#include <linux/device.h>
  17#include <linux/dmi.h>
  18#include <linux/fb.h>
  19#include <linux/i8042.h>
  20#include <linux/init.h>
  21#include <linux/input.h>
  22#include <linux/input/sparse-keymap.h>
  23#include <linux/jiffies.h>
  24#include <linux/kernel.h>
  25#include <linux/leds.h>
  26#include <linux/module.h>
  27#include <linux/platform_device.h>
  28#include <linux/platform_profile.h>
 
  29#include <linux/rfkill.h>
 
 
 
 
 
 
  30#include <linux/seq_file.h>
  31#include <linux/sysfs.h>
  32#include <linux/types.h>
  33#include <linux/wmi.h>
  34
  35#include <acpi/video.h>
  36
  37#include <dt-bindings/leds/common.h>
  38
  39#define IDEAPAD_RFKILL_DEV_NUM	3
  40
  41enum {
  42	CFG_CAP_BT_BIT       = 16,
  43	CFG_CAP_3G_BIT       = 17,
  44	CFG_CAP_WIFI_BIT     = 18,
  45	CFG_CAP_CAM_BIT      = 19,
  46
  47	/*
  48	 * These are OnScreenDisplay support bits that can be useful to determine
  49	 * whether a hotkey exists/should show OSD. But they aren't particularly
  50	 * meaningful since they were introduced later, i.e. 2010 IdeaPads
  51	 * don't have these, but they still have had OSD for hotkeys.
  52	 */
  53	CFG_OSD_NUMLK_BIT    = 27,
  54	CFG_OSD_CAPSLK_BIT   = 28,
  55	CFG_OSD_MICMUTE_BIT  = 29,
  56	CFG_OSD_TOUCHPAD_BIT = 30,
  57	CFG_OSD_CAM_BIT      = 31,
  58};
  59
  60enum {
  61	GBMD_CONSERVATION_STATE_BIT = 5,
  62};
  63
  64enum {
  65	SBMC_CONSERVATION_ON  = 3,
  66	SBMC_CONSERVATION_OFF = 5,
  67};
  68
  69enum {
  70	HALS_KBD_BL_SUPPORT_BIT       = 4,
  71	HALS_KBD_BL_STATE_BIT         = 5,
  72	HALS_USB_CHARGING_SUPPORT_BIT = 6,
  73	HALS_USB_CHARGING_STATE_BIT   = 7,
  74	HALS_FNLOCK_SUPPORT_BIT       = 9,
  75	HALS_FNLOCK_STATE_BIT         = 10,
  76	HALS_HOTKEYS_PRIMARY_BIT      = 11,
  77};
 
  78
  79enum {
  80	SALS_KBD_BL_ON        = 0x8,
  81	SALS_KBD_BL_OFF       = 0x9,
  82	SALS_USB_CHARGING_ON  = 0xa,
  83	SALS_USB_CHARGING_OFF = 0xb,
  84	SALS_FNLOCK_ON        = 0xe,
  85	SALS_FNLOCK_OFF       = 0xf,
  86};
  87
  88enum {
  89	VPCCMD_R_VPC1 = 0x10,
  90	VPCCMD_R_BL_MAX,
  91	VPCCMD_R_BL,
  92	VPCCMD_W_BL,
  93	VPCCMD_R_WIFI,
  94	VPCCMD_W_WIFI,
  95	VPCCMD_R_BT,
  96	VPCCMD_W_BT,
  97	VPCCMD_R_BL_POWER,
  98	VPCCMD_R_NOVO,
  99	VPCCMD_R_VPC2,
 100	VPCCMD_R_TOUCHPAD,
 101	VPCCMD_W_TOUCHPAD,
 102	VPCCMD_R_CAMERA,
 103	VPCCMD_W_CAMERA,
 104	VPCCMD_R_3G,
 105	VPCCMD_W_3G,
 106	VPCCMD_R_ODD, /* 0x21 */
 107	VPCCMD_W_FAN,
 108	VPCCMD_R_RF,
 109	VPCCMD_W_RF,
 110	VPCCMD_R_FAN = 0x2B,
 111	VPCCMD_R_SPECIAL_BUTTONS = 0x31,
 112	VPCCMD_W_BL_POWER = 0x33,
 113};
 114
 115struct ideapad_dytc_priv {
 116	enum platform_profile_option current_profile;
 117	struct platform_profile_handler pprof;
 118	struct mutex mutex; /* protects the DYTC interface */
 119	struct ideapad_private *priv;
 120};
 121
 122struct ideapad_rfk_priv {
 123	int dev;
 124	struct ideapad_private *priv;
 125};
 126
 127struct ideapad_private {
 128	struct acpi_device *adev;
 129	struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
 130	struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
 131	struct platform_device *platform_device;
 132	struct input_dev *inputdev;
 133	struct backlight_device *blightdev;
 134	struct ideapad_dytc_priv *dytc;
 135	struct dentry *debug;
 136	unsigned long cfg;
 137	unsigned long r_touchpad_val;
 138	struct {
 139		bool conservation_mode    : 1;
 140		bool dytc                 : 1;
 141		bool fan_mode             : 1;
 142		bool fn_lock              : 1;
 143		bool set_fn_lock_led      : 1;
 144		bool hw_rfkill_switch     : 1;
 145		bool kbd_bl               : 1;
 146		bool touchpad_ctrl_via_ec : 1;
 147		bool ctrl_ps2_aux_port    : 1;
 148		bool usb_charging         : 1;
 149	} features;
 150	struct {
 151		bool initialized;
 152		struct led_classdev led;
 153		unsigned int last_brightness;
 154	} kbd_bl;
 155};
 156
 157static bool no_bt_rfkill;
 158module_param(no_bt_rfkill, bool, 0444);
 159MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
 160
 161static bool allow_v4_dytc;
 162module_param(allow_v4_dytc, bool, 0444);
 163MODULE_PARM_DESC(allow_v4_dytc,
 164	"Enable DYTC version 4 platform-profile support. "
 165	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
 166
 167static bool hw_rfkill_switch;
 168module_param(hw_rfkill_switch, bool, 0444);
 169MODULE_PARM_DESC(hw_rfkill_switch,
 170	"Enable rfkill support for laptops with a hw on/off wifi switch/slider. "
 171	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
 172
 173static bool set_fn_lock_led;
 174module_param(set_fn_lock_led, bool, 0444);
 175MODULE_PARM_DESC(set_fn_lock_led,
 176	"Enable driver based updates of the fn-lock LED on fn-lock changes. "
 177	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
 178
 179static bool ctrl_ps2_aux_port;
 180module_param(ctrl_ps2_aux_port, bool, 0444);
 181MODULE_PARM_DESC(ctrl_ps2_aux_port,
 182	"Enable driver based PS/2 aux port en-/dis-abling on touchpad on/off toggle. "
 183	"If you need this please report this to: platform-driver-x86@vger.kernel.org");
 184
 185static bool touchpad_ctrl_via_ec;
 186module_param(touchpad_ctrl_via_ec, bool, 0444);
 187MODULE_PARM_DESC(touchpad_ctrl_via_ec,
 188	"Enable registering a 'touchpad' sysfs-attribute which can be used to manually "
 189	"tell the EC to enable/disable the touchpad. This may not work on all models.");
 190
 191/*
 192 * shared data
 193 */
 194
 195static struct ideapad_private *ideapad_shared;
 196static DEFINE_MUTEX(ideapad_shared_mutex);
 197
 198static int ideapad_shared_init(struct ideapad_private *priv)
 199{
 200	int ret;
 201
 202	mutex_lock(&ideapad_shared_mutex);
 203
 204	if (!ideapad_shared) {
 205		ideapad_shared = priv;
 206		ret = 0;
 207	} else {
 208		dev_warn(&priv->adev->dev, "found multiple platform devices\n");
 209		ret = -EINVAL;
 210	}
 211
 212	mutex_unlock(&ideapad_shared_mutex);
 213
 214	return ret;
 215}
 216
 217static void ideapad_shared_exit(struct ideapad_private *priv)
 218{
 219	mutex_lock(&ideapad_shared_mutex);
 220
 221	if (ideapad_shared == priv)
 222		ideapad_shared = NULL;
 223
 224	mutex_unlock(&ideapad_shared_mutex);
 225}
 226
 227/*
 228 * ACPI Helpers
 229 */
 230#define IDEAPAD_EC_TIMEOUT 200 /* in ms */
 231
 232static int eval_int(acpi_handle handle, const char *name, unsigned long *res)
 233{
 234	unsigned long long result;
 235	acpi_status status;
 
 236
 237	status = acpi_evaluate_integer(handle, (char *)name, NULL, &result);
 238	if (ACPI_FAILURE(status))
 239		return -EIO;
 240
 241	*res = result;
 242
 243	return 0;
 244}
 245
 246static int exec_simple_method(acpi_handle handle, const char *name, unsigned long arg)
 247{
 248	acpi_status status = acpi_execute_simple_method(handle, (char *)name, arg);
 249
 250	return ACPI_FAILURE(status) ? -EIO : 0;
 251}
 252
 253static int eval_gbmd(acpi_handle handle, unsigned long *res)
 254{
 255	return eval_int(handle, "GBMD", res);
 256}
 257
 258static int exec_sbmc(acpi_handle handle, unsigned long arg)
 259{
 260	return exec_simple_method(handle, "SBMC", arg);
 261}
 262
 263static int eval_hals(acpi_handle handle, unsigned long *res)
 264{
 265	return eval_int(handle, "HALS", res);
 266}
 267
 268static int exec_sals(acpi_handle handle, unsigned long arg)
 269{
 270	return exec_simple_method(handle, "SALS", arg);
 271}
 272
 273static int eval_int_with_arg(acpi_handle handle, const char *name, unsigned long arg, unsigned long *res)
 274{
 275	struct acpi_object_list params;
 276	unsigned long long result;
 
 277	union acpi_object in_obj;
 278	acpi_status status;
 279
 280	params.count = 1;
 281	params.pointer = &in_obj;
 282	in_obj.type = ACPI_TYPE_INTEGER;
 283	in_obj.integer.value = arg;
 284
 285	status = acpi_evaluate_integer(handle, (char *)name, &params, &result);
 286	if (ACPI_FAILURE(status))
 287		return -EIO;
 288
 289	if (res)
 290		*res = result;
 291
 
 
 
 
 
 292	return 0;
 293}
 294
 295static int eval_dytc(acpi_handle handle, unsigned long cmd, unsigned long *res)
 296{
 297	return eval_int_with_arg(handle, "DYTC", cmd, res);
 298}
 299
 300static int eval_vpcr(acpi_handle handle, unsigned long cmd, unsigned long *res)
 301{
 302	return eval_int_with_arg(handle, "VPCR", cmd, res);
 303}
 304
 305static int eval_vpcw(acpi_handle handle, unsigned long cmd, unsigned long data)
 306{
 307	struct acpi_object_list params;
 308	union acpi_object in_obj[2];
 309	acpi_status status;
 310
 311	params.count = 2;
 312	params.pointer = in_obj;
 313	in_obj[0].type = ACPI_TYPE_INTEGER;
 314	in_obj[0].integer.value = cmd;
 315	in_obj[1].type = ACPI_TYPE_INTEGER;
 316	in_obj[1].integer.value = data;
 317
 318	status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
 319	if (ACPI_FAILURE(status))
 320		return -EIO;
 321
 322	return 0;
 323}
 324
 325static int read_ec_data(acpi_handle handle, unsigned long cmd, unsigned long *data)
 326{
 327	unsigned long end_jiffies, val;
 328	int err;
 329
 330	err = eval_vpcw(handle, 1, cmd);
 331	if (err)
 332		return err;
 333
 334	end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
 
 335
 336	while (time_before(jiffies, end_jiffies)) {
 
 337		schedule();
 338
 339		err = eval_vpcr(handle, 1, &val);
 340		if (err)
 341			return err;
 342
 343		if (val == 0)
 344			return eval_vpcr(handle, 0, data);
 
 345	}
 346
 347	acpi_handle_err(handle, "timeout in %s\n", __func__);
 348
 349	return -ETIMEDOUT;
 350}
 351
 352static int write_ec_cmd(acpi_handle handle, unsigned long cmd, unsigned long data)
 353{
 354	unsigned long end_jiffies, val;
 355	int err;
 356
 357	err = eval_vpcw(handle, 0, data);
 358	if (err)
 359		return err;
 360
 361	err = eval_vpcw(handle, 1, cmd);
 362	if (err)
 363		return err;
 364
 365	end_jiffies = jiffies + msecs_to_jiffies(IDEAPAD_EC_TIMEOUT) + 1;
 
 
 
 366
 367	while (time_before(jiffies, end_jiffies)) {
 
 368		schedule();
 369
 370		err = eval_vpcr(handle, 1, &val);
 371		if (err)
 372			return err;
 373
 374		if (val == 0)
 375			return 0;
 376	}
 377
 378	acpi_handle_err(handle, "timeout in %s\n", __func__);
 379
 380	return -ETIMEDOUT;
 381}
 382
 383/*
 384 * debugfs
 385 */
 386static int debugfs_status_show(struct seq_file *s, void *data)
 387{
 388	struct ideapad_private *priv = s->private;
 389	unsigned long value;
 390
 
 
 
 391	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
 392		seq_printf(s, "Backlight max:  %lu\n", value);
 393	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
 394		seq_printf(s, "Backlight now:  %lu\n", value);
 395	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
 396		seq_printf(s, "BL power value: %s (%lu)\n", value ? "on" : "off", value);
 397
 398	seq_puts(s, "=====================\n");
 399
 400	if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
 401		seq_printf(s, "Radio status: %s (%lu)\n", value ? "on" : "off", value);
 
 402	if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
 403		seq_printf(s, "Wifi status:  %s (%lu)\n", value ? "on" : "off", value);
 
 404	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
 405		seq_printf(s, "BT status:    %s (%lu)\n", value ? "on" : "off", value);
 
 406	if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
 407		seq_printf(s, "3G status:    %s (%lu)\n", value ? "on" : "off", value);
 408
 409	seq_puts(s, "=====================\n");
 410
 411	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
 412		seq_printf(s, "Touchpad status: %s (%lu)\n", value ? "on" : "off", value);
 
 413	if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
 414		seq_printf(s, "Camera status:   %s (%lu)\n", value ? "on" : "off", value);
 415
 416	seq_puts(s, "=====================\n");
 417
 418	if (!eval_gbmd(priv->adev->handle, &value))
 419		seq_printf(s, "GBMD: %#010lx\n", value);
 420	if (!eval_hals(priv->adev->handle, &value))
 421		seq_printf(s, "HALS: %#010lx\n", value);
 
 422
 423	return 0;
 424}
 425DEFINE_SHOW_ATTRIBUTE(debugfs_status);
 426
 427static int debugfs_cfg_show(struct seq_file *s, void *data)
 428{
 429	struct ideapad_private *priv = s->private;
 430
 431	seq_printf(s, "_CFG: %#010lx\n\n", priv->cfg);
 432
 433	seq_puts(s, "Capabilities:");
 434	if (test_bit(CFG_CAP_BT_BIT, &priv->cfg))
 435		seq_puts(s, " bluetooth");
 436	if (test_bit(CFG_CAP_3G_BIT, &priv->cfg))
 437		seq_puts(s, " 3G");
 438	if (test_bit(CFG_CAP_WIFI_BIT, &priv->cfg))
 439		seq_puts(s, " wifi");
 440	if (test_bit(CFG_CAP_CAM_BIT, &priv->cfg))
 441		seq_puts(s, " camera");
 442	seq_puts(s, "\n");
 443
 444	seq_puts(s, "OSD support:");
 445	if (test_bit(CFG_OSD_NUMLK_BIT, &priv->cfg))
 446		seq_puts(s, " num-lock");
 447	if (test_bit(CFG_OSD_CAPSLK_BIT, &priv->cfg))
 448		seq_puts(s, " caps-lock");
 449	if (test_bit(CFG_OSD_MICMUTE_BIT, &priv->cfg))
 450		seq_puts(s, " mic-mute");
 451	if (test_bit(CFG_OSD_TOUCHPAD_BIT, &priv->cfg))
 452		seq_puts(s, " touchpad");
 453	if (test_bit(CFG_OSD_CAM_BIT, &priv->cfg))
 454		seq_puts(s, " camera");
 455	seq_puts(s, "\n");
 456
 457	seq_puts(s, "Graphics: ");
 458	switch (priv->cfg & 0x700) {
 459	case 0x100:
 460		seq_puts(s, "Intel");
 461		break;
 462	case 0x200:
 463		seq_puts(s, "ATI");
 464		break;
 465	case 0x300:
 466		seq_puts(s, "Nvidia");
 467		break;
 468	case 0x400:
 469		seq_puts(s, "Intel and ATI");
 470		break;
 471	case 0x500:
 472		seq_puts(s, "Intel and Nvidia");
 473		break;
 474	}
 475	seq_puts(s, "\n");
 476
 477	return 0;
 478}
 479DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
 480
 481static void ideapad_debugfs_init(struct ideapad_private *priv)
 482{
 483	struct dentry *dir;
 484
 485	dir = debugfs_create_dir("ideapad", NULL);
 486	priv->debug = dir;
 
 
 
 487
 488	debugfs_create_file("cfg", 0444, dir, priv, &debugfs_cfg_fops);
 489	debugfs_create_file("status", 0444, dir, priv, &debugfs_status_fops);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 490}
 491
 492static void ideapad_debugfs_exit(struct ideapad_private *priv)
 493{
 494	debugfs_remove_recursive(priv->debug);
 495	priv->debug = NULL;
 496}
 497
 498/*
 499 * sysfs
 500 */
 501static ssize_t camera_power_show(struct device *dev,
 502				 struct device_attribute *attr,
 503				 char *buf)
 504{
 505	struct ideapad_private *priv = dev_get_drvdata(dev);
 506	unsigned long result;
 507	int err;
 508
 509	err = read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result);
 510	if (err)
 511		return err;
 512
 513	return sysfs_emit(buf, "%d\n", !!result);
 514}
 515
 516static ssize_t camera_power_store(struct device *dev,
 517				  struct device_attribute *attr,
 518				  const char *buf, size_t count)
 519{
 520	struct ideapad_private *priv = dev_get_drvdata(dev);
 521	bool state;
 522	int err;
 523
 524	err = kstrtobool(buf, &state);
 525	if (err)
 526		return err;
 527
 528	err = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
 529	if (err)
 530		return err;
 531
 532	return count;
 
 
 533}
 534
 535static DEVICE_ATTR_RW(camera_power);
 536
 537static ssize_t conservation_mode_show(struct device *dev,
 538				      struct device_attribute *attr,
 539				      char *buf)
 540{
 541	struct ideapad_private *priv = dev_get_drvdata(dev);
 542	unsigned long result;
 543	int err;
 544
 545	err = eval_gbmd(priv->adev->handle, &result);
 546	if (err)
 547		return err;
 548
 549	return sysfs_emit(buf, "%d\n", !!test_bit(GBMD_CONSERVATION_STATE_BIT, &result));
 550}
 551
 552static ssize_t conservation_mode_store(struct device *dev,
 553				       struct device_attribute *attr,
 554				       const char *buf, size_t count)
 555{
 556	struct ideapad_private *priv = dev_get_drvdata(dev);
 557	bool state;
 558	int err;
 559
 560	err = kstrtobool(buf, &state);
 561	if (err)
 562		return err;
 563
 564	err = exec_sbmc(priv->adev->handle, state ? SBMC_CONSERVATION_ON : SBMC_CONSERVATION_OFF);
 565	if (err)
 566		return err;
 567
 568	return count;
 569}
 570
 571static DEVICE_ATTR_RW(conservation_mode);
 572
 573static ssize_t fan_mode_show(struct device *dev,
 574			     struct device_attribute *attr,
 575			     char *buf)
 576{
 577	struct ideapad_private *priv = dev_get_drvdata(dev);
 578	unsigned long result;
 579	int err;
 580
 581	err = read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result);
 582	if (err)
 583		return err;
 584
 585	return sysfs_emit(buf, "%lu\n", result);
 586}
 587
 588static ssize_t fan_mode_store(struct device *dev,
 589			      struct device_attribute *attr,
 590			      const char *buf, size_t count)
 591{
 
 592	struct ideapad_private *priv = dev_get_drvdata(dev);
 593	unsigned int state;
 594	int err;
 595
 596	err = kstrtouint(buf, 0, &state);
 597	if (err)
 598		return err;
 599
 600	if (state > 4 || state == 3)
 601		return -EINVAL;
 602
 603	err = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
 604	if (err)
 605		return err;
 606
 607	return count;
 608}
 609
 610static DEVICE_ATTR_RW(fan_mode);
 611
 612static ssize_t fn_lock_show(struct device *dev,
 613			    struct device_attribute *attr,
 614			    char *buf)
 615{
 
 616	struct ideapad_private *priv = dev_get_drvdata(dev);
 617	unsigned long hals;
 618	int err;
 619
 620	err = eval_hals(priv->adev->handle, &hals);
 621	if (err)
 622		return err;
 623
 624	return sysfs_emit(buf, "%d\n", !!test_bit(HALS_FNLOCK_STATE_BIT, &hals));
 
 
 625}
 626
 627static ssize_t fn_lock_store(struct device *dev,
 628			     struct device_attribute *attr,
 629			     const char *buf, size_t count)
 630{
 
 631	struct ideapad_private *priv = dev_get_drvdata(dev);
 632	bool state;
 633	int err;
 634
 635	err = kstrtobool(buf, &state);
 636	if (err)
 637		return err;
 638
 639	err = exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
 640	if (err)
 641		return err;
 642
 
 
 
 
 
 
 
 
 
 643	return count;
 644}
 645
 646static DEVICE_ATTR_RW(fn_lock);
 647
 648static ssize_t touchpad_show(struct device *dev,
 649			     struct device_attribute *attr,
 650			     char *buf)
 651{
 652	struct ideapad_private *priv = dev_get_drvdata(dev);
 653	unsigned long result;
 654	int err;
 655
 656	err = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result);
 657	if (err)
 658		return err;
 659
 660	priv->r_touchpad_val = result;
 661
 662	return sysfs_emit(buf, "%d\n", !!result);
 
 
 663}
 664
 665static ssize_t touchpad_store(struct device *dev,
 666			      struct device_attribute *attr,
 667			      const char *buf, size_t count)
 
 668{
 669	struct ideapad_private *priv = dev_get_drvdata(dev);
 670	bool state;
 671	int err;
 672
 673	err = kstrtobool(buf, &state);
 674	if (err)
 675		return err;
 676
 677	err = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
 678	if (err)
 679		return err;
 680
 681	priv->r_touchpad_val = state;
 
 
 682
 
 
 
 683	return count;
 684}
 685
 686static DEVICE_ATTR_RW(touchpad);
 687
 688static ssize_t usb_charging_show(struct device *dev,
 689				 struct device_attribute *attr,
 690				 char *buf)
 691{
 692	struct ideapad_private *priv = dev_get_drvdata(dev);
 693	unsigned long hals;
 694	int err;
 695
 696	err = eval_hals(priv->adev->handle, &hals);
 697	if (err)
 698		return err;
 699
 700	return sysfs_emit(buf, "%d\n", !!test_bit(HALS_USB_CHARGING_STATE_BIT, &hals));
 
 
 701}
 702
 703static ssize_t usb_charging_store(struct device *dev,
 704				  struct device_attribute *attr,
 705				  const char *buf, size_t count)
 706{
 707	struct ideapad_private *priv = dev_get_drvdata(dev);
 708	bool state;
 709	int err;
 710
 711	err = kstrtobool(buf, &state);
 712	if (err)
 713		return err;
 714
 715	err = exec_sals(priv->adev->handle, state ? SALS_USB_CHARGING_ON : SALS_USB_CHARGING_OFF);
 716	if (err)
 717		return err;
 718
 
 
 
 
 
 719	return count;
 720}
 721
 722static DEVICE_ATTR_RW(usb_charging);
 723
 724static struct attribute *ideapad_attributes[] = {
 725	&dev_attr_camera_power.attr,
 726	&dev_attr_conservation_mode.attr,
 727	&dev_attr_fan_mode.attr,
 728	&dev_attr_fn_lock.attr,
 729	&dev_attr_touchpad.attr,
 730	&dev_attr_usb_charging.attr,
 731	NULL
 732};
 733
 734static umode_t ideapad_is_visible(struct kobject *kobj,
 735				  struct attribute *attr,
 736				  int idx)
 737{
 738	struct device *dev = kobj_to_dev(kobj);
 739	struct ideapad_private *priv = dev_get_drvdata(dev);
 740	bool supported = true;
 741
 742	if (attr == &dev_attr_camera_power.attr)
 743		supported = test_bit(CFG_CAP_CAM_BIT, &priv->cfg);
 744	else if (attr == &dev_attr_conservation_mode.attr)
 745		supported = priv->features.conservation_mode;
 746	else if (attr == &dev_attr_fan_mode.attr)
 747		supported = priv->features.fan_mode;
 748	else if (attr == &dev_attr_fn_lock.attr)
 749		supported = priv->features.fn_lock;
 750	else if (attr == &dev_attr_touchpad.attr)
 751		supported = priv->features.touchpad_ctrl_via_ec;
 752	else if (attr == &dev_attr_usb_charging.attr)
 753		supported = priv->features.usb_charging;
 754
 755	return supported ? attr->mode : 0;
 756}
 757
 758static const struct attribute_group ideapad_attribute_group = {
 759	.is_visible = ideapad_is_visible,
 760	.attrs = ideapad_attributes
 761};
 762
 763/*
 764 * DYTC Platform profile
 765 */
 766#define DYTC_CMD_QUERY        0 /* To get DYTC status - enable/revision */
 767#define DYTC_CMD_SET          1 /* To enable/disable IC function mode */
 768#define DYTC_CMD_GET          2 /* To get current IC function and mode */
 769#define DYTC_CMD_RESET    0x1ff /* To reset back to default */
 770
 771#define DYTC_QUERY_ENABLE_BIT 8  /* Bit        8 - 0 = disabled, 1 = enabled */
 772#define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */
 773#define DYTC_QUERY_REV_BIT    28 /* Bits 28 - 31 - revision */
 774
 775#define DYTC_GET_FUNCTION_BIT 8  /* Bits  8-11 - function setting */
 776#define DYTC_GET_MODE_BIT     12 /* Bits 12-15 - mode setting */
 777
 778#define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */
 779#define DYTC_SET_MODE_BIT     16 /* Bits 16-19 - mode setting */
 780#define DYTC_SET_VALID_BIT    20 /* Bit     20 - 1 = on, 0 = off */
 781
 782#define DYTC_FUNCTION_STD     0  /* Function = 0, standard mode */
 783#define DYTC_FUNCTION_CQL     1  /* Function = 1, lap mode */
 784#define DYTC_FUNCTION_MMC     11 /* Function = 11, desk mode */
 785
 786#define DYTC_MODE_PERFORM     2  /* High power mode aka performance */
 787#define DYTC_MODE_LOW_POWER       3  /* Low power mode aka quiet */
 788#define DYTC_MODE_BALANCE   0xF  /* Default mode aka balanced */
 789
 790#define DYTC_SET_COMMAND(function, mode, on) \
 791	(DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \
 792	 (mode) << DYTC_SET_MODE_BIT | \
 793	 (on) << DYTC_SET_VALID_BIT)
 794
 795#define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 0)
 796
 797#define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_BALANCE, 1)
 798
 799static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile)
 800{
 801	switch (dytcmode) {
 802	case DYTC_MODE_LOW_POWER:
 803		*profile = PLATFORM_PROFILE_LOW_POWER;
 804		break;
 805	case DYTC_MODE_BALANCE:
 806		*profile =  PLATFORM_PROFILE_BALANCED;
 807		break;
 808	case DYTC_MODE_PERFORM:
 809		*profile =  PLATFORM_PROFILE_PERFORMANCE;
 810		break;
 811	default: /* Unknown mode */
 812		return -EINVAL;
 813	}
 814
 815	return 0;
 816}
 817
 818static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode)
 819{
 820	switch (profile) {
 821	case PLATFORM_PROFILE_LOW_POWER:
 822		*perfmode = DYTC_MODE_LOW_POWER;
 823		break;
 824	case PLATFORM_PROFILE_BALANCED:
 825		*perfmode = DYTC_MODE_BALANCE;
 826		break;
 827	case PLATFORM_PROFILE_PERFORMANCE:
 828		*perfmode = DYTC_MODE_PERFORM;
 829		break;
 830	default: /* Unknown profile */
 831		return -EOPNOTSUPP;
 832	}
 833
 834	return 0;
 835}
 836
 837/*
 838 * dytc_profile_get: Function to register with platform_profile
 839 * handler. Returns current platform profile.
 840 */
 841static int dytc_profile_get(struct platform_profile_handler *pprof,
 842			    enum platform_profile_option *profile)
 843{
 844	struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
 845
 846	*profile = dytc->current_profile;
 847	return 0;
 848}
 849
 850/*
 851 * Helper function - check if we are in CQL mode and if we are
 852 *  - disable CQL,
 853 *  - run the command
 854 *  - enable CQL
 855 *  If not in CQL mode, just run the command
 856 */
 857static int dytc_cql_command(struct ideapad_private *priv, unsigned long cmd,
 858			    unsigned long *output)
 859{
 860	int err, cmd_err, cur_funcmode;
 861
 862	/* Determine if we are in CQL mode. This alters the commands we do */
 863	err = eval_dytc(priv->adev->handle, DYTC_CMD_GET, output);
 864	if (err)
 865		return err;
 866
 867	cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF;
 868	/* Check if we're OK to return immediately */
 869	if (cmd == DYTC_CMD_GET && cur_funcmode != DYTC_FUNCTION_CQL)
 870		return 0;
 871
 872	if (cur_funcmode == DYTC_FUNCTION_CQL) {
 873		err = eval_dytc(priv->adev->handle, DYTC_DISABLE_CQL, NULL);
 874		if (err)
 875			return err;
 876	}
 877
 878	cmd_err = eval_dytc(priv->adev->handle, cmd, output);
 879	/* Check return condition after we've restored CQL state */
 880
 881	if (cur_funcmode == DYTC_FUNCTION_CQL) {
 882		err = eval_dytc(priv->adev->handle, DYTC_ENABLE_CQL, NULL);
 883		if (err)
 884			return err;
 885	}
 886
 887	return cmd_err;
 888}
 889
 890/*
 891 * dytc_profile_set: Function to register with platform_profile
 892 * handler. Sets current platform profile.
 893 */
 894static int dytc_profile_set(struct platform_profile_handler *pprof,
 895			    enum platform_profile_option profile)
 896{
 897	struct ideapad_dytc_priv *dytc = container_of(pprof, struct ideapad_dytc_priv, pprof);
 898	struct ideapad_private *priv = dytc->priv;
 899	unsigned long output;
 900	int err;
 901
 902	err = mutex_lock_interruptible(&dytc->mutex);
 903	if (err)
 904		return err;
 905
 906	if (profile == PLATFORM_PROFILE_BALANCED) {
 907		/* To get back to balanced mode we just issue a reset command */
 908		err = eval_dytc(priv->adev->handle, DYTC_CMD_RESET, NULL);
 909		if (err)
 910			goto unlock;
 911	} else {
 912		int perfmode;
 913
 914		err = convert_profile_to_dytc(profile, &perfmode);
 915		if (err)
 916			goto unlock;
 917
 918		/* Determine if we are in CQL mode. This alters the commands we do */
 919		err = dytc_cql_command(priv, DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1),
 920				       &output);
 921		if (err)
 922			goto unlock;
 923	}
 924
 925	/* Success - update current profile */
 926	dytc->current_profile = profile;
 927
 928unlock:
 929	mutex_unlock(&dytc->mutex);
 930
 931	return err;
 932}
 933
 934static void dytc_profile_refresh(struct ideapad_private *priv)
 935{
 936	enum platform_profile_option profile;
 937	unsigned long output;
 938	int err, perfmode;
 939
 940	mutex_lock(&priv->dytc->mutex);
 941	err = dytc_cql_command(priv, DYTC_CMD_GET, &output);
 942	mutex_unlock(&priv->dytc->mutex);
 943	if (err)
 944		return;
 945
 946	perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF;
 947
 948	if (convert_dytc_to_profile(perfmode, &profile))
 949		return;
 950
 951	if (profile != priv->dytc->current_profile) {
 952		priv->dytc->current_profile = profile;
 953		platform_profile_notify();
 954	}
 955}
 956
 957static const struct dmi_system_id ideapad_dytc_v4_allow_table[] = {
 958	{
 959		/* Ideapad 5 Pro 16ACH6 */
 960		.matches = {
 961			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 962			DMI_MATCH(DMI_PRODUCT_NAME, "82L5")
 963		}
 964	},
 965	{
 966		/* Ideapad 5 15ITL05 */
 967		.matches = {
 968			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 969			DMI_MATCH(DMI_PRODUCT_VERSION, "IdeaPad 5 15ITL05")
 970		}
 971	},
 972	{}
 973};
 974
 975static int ideapad_dytc_profile_init(struct ideapad_private *priv)
 976{
 977	int err, dytc_version;
 978	unsigned long output;
 979
 980	if (!priv->features.dytc)
 981		return -ENODEV;
 982
 983	err = eval_dytc(priv->adev->handle, DYTC_CMD_QUERY, &output);
 984	/* For all other errors we can flag the failure */
 985	if (err)
 986		return err;
 987
 988	/* Check DYTC is enabled and supports mode setting */
 989	if (!test_bit(DYTC_QUERY_ENABLE_BIT, &output)) {
 990		dev_info(&priv->platform_device->dev, "DYTC_QUERY_ENABLE_BIT returned false\n");
 991		return -ENODEV;
 992	}
 993
 994	dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF;
 995
 996	if (dytc_version < 4) {
 997		dev_info(&priv->platform_device->dev, "DYTC_VERSION < 4 is not supported\n");
 998		return -ENODEV;
 999	}
1000
1001	if (dytc_version < 5 &&
1002	    !(allow_v4_dytc || dmi_check_system(ideapad_dytc_v4_allow_table))) {
1003		dev_info(&priv->platform_device->dev,
1004			 "DYTC_VERSION 4 support may not work. Pass ideapad_laptop.allow_v4_dytc=Y on the kernel commandline to enable\n");
1005		return -ENODEV;
1006	}
1007
1008	priv->dytc = kzalloc(sizeof(*priv->dytc), GFP_KERNEL);
1009	if (!priv->dytc)
1010		return -ENOMEM;
1011
1012	mutex_init(&priv->dytc->mutex);
1013
1014	priv->dytc->priv = priv;
1015	priv->dytc->pprof.profile_get = dytc_profile_get;
1016	priv->dytc->pprof.profile_set = dytc_profile_set;
1017
1018	/* Setup supported modes */
1019	set_bit(PLATFORM_PROFILE_LOW_POWER, priv->dytc->pprof.choices);
1020	set_bit(PLATFORM_PROFILE_BALANCED, priv->dytc->pprof.choices);
1021	set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->dytc->pprof.choices);
1022
1023	/* Create platform_profile structure and register */
1024	err = platform_profile_register(&priv->dytc->pprof);
1025	if (err)
1026		goto pp_reg_failed;
1027
1028	/* Ensure initial values are correct */
1029	dytc_profile_refresh(priv);
1030
1031	return 0;
1032
1033pp_reg_failed:
1034	mutex_destroy(&priv->dytc->mutex);
1035	kfree(priv->dytc);
1036	priv->dytc = NULL;
1037
1038	return err;
1039}
1040
1041static void ideapad_dytc_profile_exit(struct ideapad_private *priv)
1042{
1043	if (!priv->dytc)
1044		return;
1045
1046	platform_profile_remove();
1047	mutex_destroy(&priv->dytc->mutex);
1048	kfree(priv->dytc);
1049
1050	priv->dytc = NULL;
1051}
1052
1053/*
1054 * Rfkill
1055 */
1056struct ideapad_rfk_data {
1057	char *name;
1058	int cfgbit;
1059	int opcode;
1060	int type;
1061};
1062
1063static const struct ideapad_rfk_data ideapad_rfk_data[] = {
1064	{ "ideapad_wlan",      CFG_CAP_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
1065	{ "ideapad_bluetooth", CFG_CAP_BT_BIT,   VPCCMD_W_BT,   RFKILL_TYPE_BLUETOOTH },
1066	{ "ideapad_3g",        CFG_CAP_3G_BIT,   VPCCMD_W_3G,   RFKILL_TYPE_WWAN },
1067};
1068
1069static int ideapad_rfk_set(void *data, bool blocked)
1070{
1071	struct ideapad_rfk_priv *priv = data;
1072	int opcode = ideapad_rfk_data[priv->dev].opcode;
1073
1074	return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
1075}
1076
1077static const struct rfkill_ops ideapad_rfk_ops = {
1078	.set_block = ideapad_rfk_set,
1079};
1080
1081static void ideapad_sync_rfk_state(struct ideapad_private *priv)
1082{
1083	unsigned long hw_blocked = 0;
1084	int i;
1085
1086	if (priv->features.hw_rfkill_switch) {
1087		if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
1088			return;
1089		hw_blocked = !hw_blocked;
1090	}
1091
1092	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1093		if (priv->rfk[i])
1094			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
1095}
1096
1097static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
1098{
1099	unsigned long rf_enabled;
1100	int err;
1101
1102	if (no_bt_rfkill && ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH) {
 
1103		/* Force to enable bluetooth when no_bt_rfkill=1 */
1104		write_ec_cmd(priv->adev->handle, ideapad_rfk_data[dev].opcode, 1);
 
1105		return 0;
1106	}
1107
1108	priv->rfk_priv[dev].dev = dev;
1109	priv->rfk_priv[dev].priv = priv;
1110
1111	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
1112				      &priv->platform_device->dev,
1113				      ideapad_rfk_data[dev].type,
1114				      &ideapad_rfk_ops,
1115				      &priv->rfk_priv[dev]);
1116	if (!priv->rfk[dev])
1117		return -ENOMEM;
1118
1119	err = read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode - 1, &rf_enabled);
1120	if (err)
1121		rf_enabled = 1;
1122
1123	rfkill_init_sw_state(priv->rfk[dev], !rf_enabled);
 
 
1124
1125	err = rfkill_register(priv->rfk[dev]);
1126	if (err)
1127		rfkill_destroy(priv->rfk[dev]);
1128
1129	return err;
 
1130}
1131
1132static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
1133{
1134	if (!priv->rfk[dev])
1135		return;
1136
1137	rfkill_unregister(priv->rfk[dev]);
1138	rfkill_destroy(priv->rfk[dev]);
1139}
1140
1141/*
1142 * Platform device
1143 */
1144static int ideapad_sysfs_init(struct ideapad_private *priv)
1145{
1146	return device_add_group(&priv->platform_device->dev,
1147				&ideapad_attribute_group);
1148}
1149
1150static void ideapad_sysfs_exit(struct ideapad_private *priv)
1151{
1152	device_remove_group(&priv->platform_device->dev,
1153			    &ideapad_attribute_group);
1154}
1155
1156/*
1157 * input device
1158 */
1159#define IDEAPAD_WMI_KEY 0x100
1160
1161static const struct key_entry ideapad_keymap[] = {
1162	{ KE_KEY,   6, { KEY_SWITCHVIDEOMODE } },
1163	{ KE_KEY,   7, { KEY_CAMERA } },
1164	{ KE_KEY,   8, { KEY_MICMUTE } },
1165	{ KE_KEY,  11, { KEY_F16 } },
1166	{ KE_KEY,  13, { KEY_WLAN } },
1167	{ KE_KEY,  16, { KEY_PROG1 } },
1168	{ KE_KEY,  17, { KEY_PROG2 } },
1169	{ KE_KEY,  64, { KEY_PROG3 } },
1170	{ KE_KEY,  65, { KEY_PROG4 } },
1171	{ KE_KEY,  66, { KEY_TOUCHPAD_OFF } },
1172	{ KE_KEY,  67, { KEY_TOUCHPAD_ON } },
1173	{ KE_KEY,  68, { KEY_TOUCHPAD_TOGGLE } },
1174	{ KE_KEY, 128, { KEY_ESC } },
1175
1176	/*
1177	 * WMI keys
1178	 */
1179
1180	/* FnLock (handled by the firmware) */
1181	{ KE_IGNORE,	0x02 | IDEAPAD_WMI_KEY },
1182	/* Esc (handled by the firmware) */
1183	{ KE_IGNORE,	0x03 | IDEAPAD_WMI_KEY },
1184	/* Customizable Lenovo Hotkey ("star" with 'S' inside) */
1185	{ KE_KEY,	0x01 | IDEAPAD_WMI_KEY, { KEY_FAVORITES } },
1186	/* Dark mode toggle */
1187	{ KE_KEY,	0x13 | IDEAPAD_WMI_KEY, { KEY_PROG1 } },
1188	/* Sound profile switch */
1189	{ KE_KEY,	0x12 | IDEAPAD_WMI_KEY, { KEY_PROG2 } },
1190	/* Lenovo Virtual Background application */
1191	{ KE_KEY,	0x28 | IDEAPAD_WMI_KEY, { KEY_PROG3 } },
1192	/* Lenovo Support */
1193	{ KE_KEY,	0x27 | IDEAPAD_WMI_KEY, { KEY_HELP } },
1194	/* Refresh Rate Toggle */
1195	{ KE_KEY,	0x0a | IDEAPAD_WMI_KEY, { KEY_DISPLAYTOGGLE } },
1196
1197	{ KE_END },
1198};
1199
1200static int ideapad_input_init(struct ideapad_private *priv)
1201{
1202	struct input_dev *inputdev;
1203	int err;
1204
1205	inputdev = input_allocate_device();
1206	if (!inputdev)
1207		return -ENOMEM;
1208
1209	inputdev->name = "Ideapad extra buttons";
1210	inputdev->phys = "ideapad/input0";
1211	inputdev->id.bustype = BUS_HOST;
1212	inputdev->dev.parent = &priv->platform_device->dev;
1213
1214	err = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
1215	if (err) {
1216		dev_err(&priv->platform_device->dev,
1217			"Could not set up input device keymap: %d\n", err);
1218		goto err_free_dev;
1219	}
1220
1221	err = input_register_device(inputdev);
1222	if (err) {
1223		dev_err(&priv->platform_device->dev,
1224			"Could not register input device: %d\n", err);
1225		goto err_free_dev;
1226	}
1227
1228	priv->inputdev = inputdev;
1229
1230	return 0;
1231
1232err_free_dev:
1233	input_free_device(inputdev);
1234
1235	return err;
1236}
1237
1238static void ideapad_input_exit(struct ideapad_private *priv)
1239{
1240	input_unregister_device(priv->inputdev);
1241	priv->inputdev = NULL;
1242}
1243
1244static void ideapad_input_report(struct ideapad_private *priv,
1245				 unsigned long scancode)
1246{
1247	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
1248}
1249
1250static void ideapad_input_novokey(struct ideapad_private *priv)
1251{
1252	unsigned long long_pressed;
1253
1254	if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
1255		return;
1256
1257	if (long_pressed)
1258		ideapad_input_report(priv, 17);
1259	else
1260		ideapad_input_report(priv, 16);
1261}
1262
1263static void ideapad_check_special_buttons(struct ideapad_private *priv)
1264{
1265	unsigned long bit, value;
1266
1267	if (read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value))
1268		return;
1269
1270	for_each_set_bit (bit, &value, 16) {
1271		switch (bit) {
1272		case 6:	/* Z570 */
1273		case 0:	/* Z580 */
1274			/* Thermal Management button */
1275			ideapad_input_report(priv, 65);
1276			break;
1277		case 1:
1278			/* OneKey Theater button */
1279			ideapad_input_report(priv, 64);
1280			break;
1281		default:
1282			dev_info(&priv->platform_device->dev,
1283				 "Unknown special button: %lu\n", bit);
1284			break;
 
1285		}
1286	}
1287}
1288
1289/*
1290 * backlight
1291 */
1292static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
1293{
1294	struct ideapad_private *priv = bl_get_data(blightdev);
1295	unsigned long now;
1296	int err;
1297
1298	err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1299	if (err)
1300		return err;
1301
 
 
1302	return now;
1303}
1304
1305static int ideapad_backlight_update_status(struct backlight_device *blightdev)
1306{
1307	struct ideapad_private *priv = bl_get_data(blightdev);
1308	int err;
1309
1310	err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
1311			   blightdev->props.brightness);
1312	if (err)
1313		return err;
1314
1315	err = write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
1316			   blightdev->props.power != FB_BLANK_POWERDOWN);
1317	if (err)
1318		return err;
1319
1320	return 0;
1321}
1322
1323static const struct backlight_ops ideapad_backlight_ops = {
1324	.get_brightness = ideapad_backlight_get_brightness,
1325	.update_status = ideapad_backlight_update_status,
1326};
1327
1328static int ideapad_backlight_init(struct ideapad_private *priv)
1329{
1330	struct backlight_device *blightdev;
1331	struct backlight_properties props;
1332	unsigned long max, now, power;
1333	int err;
1334
1335	err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max);
1336	if (err)
1337		return err;
1338
1339	err = read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1340	if (err)
1341		return err;
1342
1343	err = read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power);
1344	if (err)
1345		return err;
1346
1347	memset(&props, 0, sizeof(props));
 
1348
 
1349	props.max_brightness = max;
1350	props.type = BACKLIGHT_PLATFORM;
1351
1352	blightdev = backlight_device_register("ideapad",
1353					      &priv->platform_device->dev,
1354					      priv,
1355					      &ideapad_backlight_ops,
1356					      &props);
1357	if (IS_ERR(blightdev)) {
1358		err = PTR_ERR(blightdev);
1359		dev_err(&priv->platform_device->dev,
1360			"Could not register backlight device: %d\n", err);
1361		return err;
1362	}
1363
1364	priv->blightdev = blightdev;
1365	blightdev->props.brightness = now;
1366	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1367
1368	backlight_update_status(blightdev);
1369
1370	return 0;
1371}
1372
1373static void ideapad_backlight_exit(struct ideapad_private *priv)
1374{
1375	backlight_device_unregister(priv->blightdev);
1376	priv->blightdev = NULL;
1377}
1378
1379static void ideapad_backlight_notify_power(struct ideapad_private *priv)
1380{
1381	struct backlight_device *blightdev = priv->blightdev;
1382	unsigned long power;
 
1383
1384	if (!blightdev)
1385		return;
1386
1387	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
1388		return;
1389
1390	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1391}
1392
1393static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
1394{
1395	unsigned long now;
1396
1397	/* if we control brightness via acpi video driver */
1398	if (!priv->blightdev)
1399		read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
1400	else
1401		backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
1402}
1403
1404/*
1405 * keyboard backlight
1406 */
1407static int ideapad_kbd_bl_brightness_get(struct ideapad_private *priv)
1408{
1409	unsigned long hals;
1410	int err;
1411
1412	err = eval_hals(priv->adev->handle, &hals);
1413	if (err)
1414		return err;
1415
1416	return !!test_bit(HALS_KBD_BL_STATE_BIT, &hals);
1417}
1418
1419static enum led_brightness ideapad_kbd_bl_led_cdev_brightness_get(struct led_classdev *led_cdev)
1420{
1421	struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1422
1423	return ideapad_kbd_bl_brightness_get(priv);
1424}
1425
1426static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned int brightness)
1427{
1428	int err = exec_sals(priv->adev->handle, brightness ? SALS_KBD_BL_ON : SALS_KBD_BL_OFF);
1429
1430	if (err)
1431		return err;
1432
1433	priv->kbd_bl.last_brightness = brightness;
1434
1435	return 0;
1436}
1437
1438static int ideapad_kbd_bl_led_cdev_brightness_set(struct led_classdev *led_cdev,
1439						  enum led_brightness brightness)
1440{
1441	struct ideapad_private *priv = container_of(led_cdev, struct ideapad_private, kbd_bl.led);
1442
1443	return ideapad_kbd_bl_brightness_set(priv, brightness);
1444}
1445
1446static void ideapad_kbd_bl_notify(struct ideapad_private *priv)
1447{
1448	int brightness;
1449
1450	if (!priv->kbd_bl.initialized)
1451		return;
1452
1453	brightness = ideapad_kbd_bl_brightness_get(priv);
1454	if (brightness < 0)
1455		return;
 
1456
1457	if (brightness == priv->kbd_bl.last_brightness)
1458		return;
1459
1460	priv->kbd_bl.last_brightness = brightness;
1461
1462	led_classdev_notify_brightness_hw_changed(&priv->kbd_bl.led, brightness);
1463}
1464
1465static int ideapad_kbd_bl_init(struct ideapad_private *priv)
1466{
1467	int brightness, err;
1468
1469	if (!priv->features.kbd_bl)
1470		return -ENODEV;
1471
1472	if (WARN_ON(priv->kbd_bl.initialized))
1473		return -EEXIST;
1474
1475	brightness = ideapad_kbd_bl_brightness_get(priv);
1476	if (brightness < 0)
1477		return brightness;
1478
1479	priv->kbd_bl.last_brightness = brightness;
1480
1481	priv->kbd_bl.led.name                    = "platform::" LED_FUNCTION_KBD_BACKLIGHT;
1482	priv->kbd_bl.led.max_brightness          = 1;
1483	priv->kbd_bl.led.brightness_get          = ideapad_kbd_bl_led_cdev_brightness_get;
1484	priv->kbd_bl.led.brightness_set_blocking = ideapad_kbd_bl_led_cdev_brightness_set;
1485	priv->kbd_bl.led.flags                   = LED_BRIGHT_HW_CHANGED;
1486
1487	err = led_classdev_register(&priv->platform_device->dev, &priv->kbd_bl.led);
1488	if (err)
1489		return err;
1490
1491	priv->kbd_bl.initialized = true;
1492
1493	return 0;
1494}
1495
1496static void ideapad_kbd_bl_exit(struct ideapad_private *priv)
1497{
1498	if (!priv->kbd_bl.initialized)
1499		return;
1500
1501	priv->kbd_bl.initialized = false;
1502
1503	led_classdev_unregister(&priv->kbd_bl.led);
1504}
1505
1506/*
1507 * module init/exit
1508 */
1509static void ideapad_sync_touchpad_state(struct ideapad_private *priv, bool send_events)
1510{
1511	unsigned long value;
1512	unsigned char param;
1513	int ret;
1514
1515	/* Without reading from EC touchpad LED doesn't switch state */
1516	ret = read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value);
1517	if (ret)
1518		return;
1519
1520	/*
1521	 * Some IdeaPads don't really turn off touchpad - they only
1522	 * switch the LED state. We (de)activate KBC AUX port to turn
1523	 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
1524	 * KEY_TOUCHPAD_ON to not to get out of sync with LED
1525	 */
1526	if (priv->features.ctrl_ps2_aux_port)
1527		i8042_command(&param, value ? I8042_CMD_AUX_ENABLE : I8042_CMD_AUX_DISABLE);
1528
1529	if (send_events) {
1530		/*
1531		 * On older models the EC controls the touchpad and toggles it
1532		 * on/off itself, in this case we report KEY_TOUCHPAD_ON/_OFF.
1533		 * If the EC did not toggle, report KEY_TOUCHPAD_TOGGLE.
1534		 */
1535		if (value != priv->r_touchpad_val) {
1536			ideapad_input_report(priv, value ? 67 : 66);
1537			sysfs_notify(&priv->platform_device->dev.kobj, NULL, "touchpad");
1538		} else {
1539			ideapad_input_report(priv, 68);
1540		}
1541	}
1542
1543	priv->r_touchpad_val = value;
1544}
1545
1546static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
1547{
1548	struct ideapad_private *priv = data;
1549	unsigned long vpc1, vpc2, bit;
1550
1551	if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
1552		return;
1553
1554	if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
1555		return;
1556
1557	vpc1 = (vpc2 << 8) | vpc1;
1558
1559	for_each_set_bit (bit, &vpc1, 16) {
1560		switch (bit) {
1561		case 13:
1562		case 11:
1563		case 8:
1564		case 7:
1565		case 6:
1566			ideapad_input_report(priv, bit);
1567			break;
1568		case 10:
1569			/*
1570			 * This event gets send on a Yoga 300-11IBR when the EC
1571			 * believes that the device has changed between laptop/
1572			 * tent/stand/tablet mode. The EC relies on getting
1573			 * angle info from 2 accelerometers through a special
1574			 * windows service calling a DSM on the DUAL250E ACPI-
1575			 * device. Linux does not do this, making the laptop/
1576			 * tent/stand/tablet mode info unreliable, so we simply
1577			 * ignore these events.
1578			 */
1579			break;
1580		case 9:
1581			ideapad_sync_rfk_state(priv);
1582			break;
1583		case 5:
1584			ideapad_sync_touchpad_state(priv, true);
1585			break;
1586		case 4:
1587			ideapad_backlight_notify_brightness(priv);
1588			break;
1589		case 3:
1590			ideapad_input_novokey(priv);
1591			break;
1592		case 2:
1593			ideapad_backlight_notify_power(priv);
1594			break;
1595		case 1:
1596			/*
1597			 * Some IdeaPads report event 1 every ~20
1598			 * seconds while on battery power; some
1599			 * report this when changing to/from tablet
1600			 * mode; some report this when the keyboard
1601			 * backlight has changed.
1602			 */
1603			ideapad_kbd_bl_notify(priv);
1604			break;
1605		case 0:
1606			ideapad_check_special_buttons(priv);
1607			break;
1608		default:
1609			dev_info(&priv->platform_device->dev,
1610				 "Unknown event: %lu\n", bit);
1611		}
1612	}
1613}
1614
1615/* On some models we need to call exec_sals(SALS_FNLOCK_ON/OFF) to set the LED */
1616static const struct dmi_system_id set_fn_lock_led_list[] = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1617	{
1618		/* https://bugzilla.kernel.org/show_bug.cgi?id=212671 */
1619		.matches = {
1620			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1621			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion R7000P2020H"),
1622		}
1623	},
1624	{
 
1625		.matches = {
1626			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1627			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Legion 5 15ARH05"),
1628		}
1629	},
1630	{}
1631};
1632
1633/*
1634 * Some ideapads have a hardware rfkill switch, but most do not have one.
1635 * Reading VPCCMD_R_RF always results in 0 on models without a hardware rfkill,
1636 * switch causing ideapad_laptop to wrongly report all radios as hw-blocked.
1637 * There used to be a long list of DMI ids for models without a hw rfkill
1638 * switch here, but that resulted in playing whack a mole.
1639 * More importantly wrongly reporting the wifi radio as hw-blocked, results in
1640 * non working wifi. Whereas not reporting it hw-blocked, when it actually is
1641 * hw-blocked results in an empty SSID list, which is a much more benign
1642 * failure mode.
1643 * So the default now is the much safer option of assuming there is no
1644 * hardware rfkill switch. This default also actually matches most hardware,
1645 * since having a hw rfkill switch is quite rare on modern hardware, so this
1646 * also leads to a much shorter list.
1647 */
1648static const struct dmi_system_id hw_rfkill_list[] = {
1649	{}
1650};
1651
1652/*
1653 * On some models the EC toggles the touchpad muted LED on touchpad toggle
1654 * hotkey presses, but the EC does not actually disable the touchpad itself.
1655 * On these models the driver needs to explicitly enable/disable the i8042
1656 * (PS/2) aux port.
1657 */
1658static const struct dmi_system_id ctrl_ps2_aux_port_list[] = {
1659	{
1660	/* Lenovo Ideapad Z570 */
1661	.matches = {
1662		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1663		DMI_MATCH(DMI_PRODUCT_VERSION, "Ideapad Z570"),
1664		},
1665	},
1666	{}
1667};
1668
1669static void ideapad_check_features(struct ideapad_private *priv)
1670{
1671	acpi_handle handle = priv->adev->handle;
1672	unsigned long val;
1673
1674	priv->features.set_fn_lock_led =
1675		set_fn_lock_led || dmi_check_system(set_fn_lock_led_list);
1676	priv->features.hw_rfkill_switch =
1677		hw_rfkill_switch || dmi_check_system(hw_rfkill_list);
1678	priv->features.ctrl_ps2_aux_port =
1679		ctrl_ps2_aux_port || dmi_check_system(ctrl_ps2_aux_port_list);
1680	priv->features.touchpad_ctrl_via_ec = touchpad_ctrl_via_ec;
1681
1682	if (!read_ec_data(handle, VPCCMD_R_FAN, &val))
1683		priv->features.fan_mode = true;
1684
1685	if (acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC"))
1686		priv->features.conservation_mode = true;
1687
1688	if (acpi_has_method(handle, "DYTC"))
1689		priv->features.dytc = true;
1690
1691	if (acpi_has_method(handle, "HALS") && acpi_has_method(handle, "SALS")) {
1692		if (!eval_hals(handle, &val)) {
1693			if (test_bit(HALS_FNLOCK_SUPPORT_BIT, &val))
1694				priv->features.fn_lock = true;
1695
1696			if (test_bit(HALS_KBD_BL_SUPPORT_BIT, &val))
1697				priv->features.kbd_bl = true;
1698
1699			if (test_bit(HALS_USB_CHARGING_SUPPORT_BIT, &val))
1700				priv->features.usb_charging = true;
1701		}
1702	}
1703}
1704
1705#if IS_ENABLED(CONFIG_ACPI_WMI)
1706/*
1707 * WMI driver
1708 */
1709enum ideapad_wmi_event_type {
1710	IDEAPAD_WMI_EVENT_ESC,
1711	IDEAPAD_WMI_EVENT_FN_KEYS,
1712};
1713
1714struct ideapad_wmi_private {
1715	enum ideapad_wmi_event_type event;
1716};
1717
1718static int ideapad_wmi_probe(struct wmi_device *wdev, const void *context)
1719{
1720	struct ideapad_wmi_private *wpriv;
1721
1722	wpriv = devm_kzalloc(&wdev->dev, sizeof(*wpriv), GFP_KERNEL);
1723	if (!wpriv)
1724		return -ENOMEM;
1725
1726	*wpriv = *(const struct ideapad_wmi_private *)context;
1727
1728	dev_set_drvdata(&wdev->dev, wpriv);
1729	return 0;
1730}
1731
1732static void ideapad_wmi_notify(struct wmi_device *wdev, union acpi_object *data)
1733{
1734	struct ideapad_wmi_private *wpriv = dev_get_drvdata(&wdev->dev);
1735	struct ideapad_private *priv;
1736	unsigned long result;
1737
1738	mutex_lock(&ideapad_shared_mutex);
1739
1740	priv = ideapad_shared;
1741	if (!priv)
1742		goto unlock;
1743
1744	switch (wpriv->event) {
1745	case IDEAPAD_WMI_EVENT_ESC:
1746		ideapad_input_report(priv, 128);
1747		break;
1748	case IDEAPAD_WMI_EVENT_FN_KEYS:
1749		if (priv->features.set_fn_lock_led &&
1750		    !eval_hals(priv->adev->handle, &result)) {
1751			bool state = test_bit(HALS_FNLOCK_STATE_BIT, &result);
1752
1753			exec_sals(priv->adev->handle, state ? SALS_FNLOCK_ON : SALS_FNLOCK_OFF);
1754		}
1755
1756		if (data->type != ACPI_TYPE_INTEGER) {
1757			dev_warn(&wdev->dev,
1758				 "WMI event data is not an integer\n");
1759			break;
1760		}
1761
1762		dev_dbg(&wdev->dev, "WMI fn-key event: 0x%llx\n",
1763			data->integer.value);
1764
1765		ideapad_input_report(priv,
1766				     data->integer.value | IDEAPAD_WMI_KEY);
1767
1768		break;
1769	}
1770unlock:
1771	mutex_unlock(&ideapad_shared_mutex);
1772}
1773
1774static const struct ideapad_wmi_private ideapad_wmi_context_esc = {
1775	.event = IDEAPAD_WMI_EVENT_ESC
1776};
1777
1778static const struct ideapad_wmi_private ideapad_wmi_context_fn_keys = {
1779	.event = IDEAPAD_WMI_EVENT_FN_KEYS
1780};
1781
1782static const struct wmi_device_id ideapad_wmi_ids[] = {
1783	{ "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", &ideapad_wmi_context_esc }, /* Yoga 3 */
1784	{ "56322276-8493-4CE8-A783-98C991274F5E", &ideapad_wmi_context_esc }, /* Yoga 700 */
1785	{ "8FC0DE0C-B4E4-43FD-B0F3-8871711C1294", &ideapad_wmi_context_fn_keys }, /* Legion 5 */
1786	{},
1787};
1788MODULE_DEVICE_TABLE(wmi, ideapad_wmi_ids);
1789
1790static struct wmi_driver ideapad_wmi_driver = {
1791	.driver = {
1792		.name = "ideapad_wmi",
1793	},
1794	.id_table = ideapad_wmi_ids,
1795	.probe = ideapad_wmi_probe,
1796	.notify = ideapad_wmi_notify,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1797};
1798
1799static int ideapad_wmi_driver_register(void)
1800{
1801	return wmi_driver_register(&ideapad_wmi_driver);
1802}
1803
1804static void ideapad_wmi_driver_unregister(void)
1805{
1806	return wmi_driver_unregister(&ideapad_wmi_driver);
1807}
1808
1809#else
1810static inline int ideapad_wmi_driver_register(void) { return 0; }
1811static inline void ideapad_wmi_driver_unregister(void) { }
1812#endif
1813
1814/*
1815 * ACPI driver
1816 */
1817static int ideapad_acpi_add(struct platform_device *pdev)
1818{
1819	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
 
1820	struct ideapad_private *priv;
1821	acpi_status status;
1822	unsigned long cfg;
1823	int err, i;
1824
1825	if (!adev || eval_int(adev->handle, "_CFG", &cfg))
 
 
 
 
1826		return -ENODEV;
1827
1828	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1829	if (!priv)
1830		return -ENOMEM;
1831
1832	dev_set_drvdata(&pdev->dev, priv);
1833
1834	priv->cfg = cfg;
1835	priv->adev = adev;
1836	priv->platform_device = pdev;
 
1837
1838	ideapad_check_features(priv);
1839
1840	err = ideapad_sysfs_init(priv);
1841	if (err)
1842		return err;
1843
1844	ideapad_debugfs_init(priv);
 
 
1845
1846	err = ideapad_input_init(priv);
1847	if (err)
1848		goto input_failed;
1849
1850	err = ideapad_kbd_bl_init(priv);
1851	if (err) {
1852		if (err != -ENODEV)
1853			dev_warn(&pdev->dev, "Could not set up keyboard backlight LED: %d\n", err);
1854		else
1855			dev_info(&pdev->dev, "Keyboard backlight control not available\n");
1856	}
1857
1858	/*
1859	 * On some models without a hw-switch (the yoga 2 13 at least)
1860	 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1861	 */
1862	if (!priv->features.hw_rfkill_switch)
1863		write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1864
1865	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1866		if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1867			ideapad_register_rfkill(priv, i);
1868
1869	ideapad_sync_rfk_state(priv);
1870	ideapad_sync_touchpad_state(priv, false);
1871
1872	err = ideapad_dytc_profile_init(priv);
1873	if (err) {
1874		if (err != -ENODEV)
1875			dev_warn(&pdev->dev, "Could not set up DYTC interface: %d\n", err);
1876		else
1877			dev_info(&pdev->dev, "DYTC interface is not available\n");
1878	}
1879
1880	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1881		err = ideapad_backlight_init(priv);
1882		if (err && err != -ENODEV)
1883			goto backlight_failed;
1884	}
1885
1886	status = acpi_install_notify_handler(adev->handle,
1887					     ACPI_DEVICE_NOTIFY,
1888					     ideapad_acpi_notify, priv);
1889	if (ACPI_FAILURE(status)) {
1890		err = -EIO;
1891		goto notification_failed;
1892	}
1893
1894	err = ideapad_shared_init(priv);
1895	if (err)
1896		goto shared_init_failed;
 
 
 
 
 
 
 
 
 
1897
1898	return 0;
1899
1900shared_init_failed:
1901	acpi_remove_notify_handler(priv->adev->handle,
1902				   ACPI_DEVICE_NOTIFY,
1903				   ideapad_acpi_notify);
1904
1905notification_failed:
1906	ideapad_backlight_exit(priv);
1907
1908backlight_failed:
1909	ideapad_dytc_profile_exit(priv);
1910
1911	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1912		ideapad_unregister_rfkill(priv, i);
1913
1914	ideapad_kbd_bl_exit(priv);
1915	ideapad_input_exit(priv);
1916
1917input_failed:
1918	ideapad_debugfs_exit(priv);
 
1919	ideapad_sysfs_exit(priv);
1920
1921	return err;
1922}
1923
1924static int ideapad_acpi_remove(struct platform_device *pdev)
1925{
1926	struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1927	int i;
1928
1929	ideapad_shared_exit(priv);
1930
 
 
1931	acpi_remove_notify_handler(priv->adev->handle,
1932				   ACPI_DEVICE_NOTIFY,
1933				   ideapad_acpi_notify);
1934
1935	ideapad_backlight_exit(priv);
1936	ideapad_dytc_profile_exit(priv);
1937
1938	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1939		ideapad_unregister_rfkill(priv, i);
1940
1941	ideapad_kbd_bl_exit(priv);
1942	ideapad_input_exit(priv);
1943	ideapad_debugfs_exit(priv);
1944	ideapad_sysfs_exit(priv);
 
1945
1946	return 0;
1947}
1948
1949#ifdef CONFIG_PM_SLEEP
1950static int ideapad_acpi_resume(struct device *dev)
1951{
1952	struct ideapad_private *priv = dev_get_drvdata(dev);
1953
1954	ideapad_sync_rfk_state(priv);
1955	ideapad_sync_touchpad_state(priv, false);
1956
1957	if (priv->dytc)
1958		dytc_profile_refresh(priv);
 
1959
 
 
1960	return 0;
1961}
1962#endif
1963static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1964
1965static const struct acpi_device_id ideapad_device_ids[] = {
1966	{"VPC2004", 0},
1967	{"", 0},
1968};
1969MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1970
1971static struct platform_driver ideapad_acpi_driver = {
1972	.probe = ideapad_acpi_add,
1973	.remove = ideapad_acpi_remove,
1974	.driver = {
1975		.name   = "ideapad_acpi",
1976		.pm     = &ideapad_pm,
1977		.acpi_match_table = ACPI_PTR(ideapad_device_ids),
1978	},
1979};
1980
1981static int __init ideapad_laptop_init(void)
1982{
1983	int err;
1984
1985	err = ideapad_wmi_driver_register();
1986	if (err)
1987		return err;
1988
1989	err = platform_driver_register(&ideapad_acpi_driver);
1990	if (err) {
1991		ideapad_wmi_driver_unregister();
1992		return err;
1993	}
1994
1995	return 0;
1996}
1997module_init(ideapad_laptop_init)
1998
1999static void __exit ideapad_laptop_exit(void)
2000{
2001	ideapad_wmi_driver_unregister();
2002	platform_driver_unregister(&ideapad_acpi_driver);
2003}
2004module_exit(ideapad_laptop_exit)
2005
2006MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2007MODULE_DESCRIPTION("IdeaPad ACPI Extras");
2008MODULE_LICENSE("GPL");
v4.17
 
   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 BM_CONSERVATION_BIT (5)
 
 
 
 
  46
  47#define CFG_BT_BIT	(16)
  48#define CFG_3G_BIT	(17)
  49#define CFG_WIFI_BIT	(18)
  50#define CFG_CAMERA_BIT	(19)
 
 
 
 
 
 
 
 
  51
  52#if IS_ENABLED(CONFIG_ACPI_WMI)
  53static const char *const ideapad_wmi_fnesc_events[] = {
  54	"26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
  55	"56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
 
 
 
 
 
 
 
 
 
 
 
 
 
  56};
  57#endif
  58
  59enum {
  60	BMCMD_CONSERVATION_ON = 3,
  61	BMCMD_CONSERVATION_OFF = 5,
 
 
 
 
  62};
  63
  64enum {
  65	VPCCMD_R_VPC1 = 0x10,
  66	VPCCMD_R_BL_MAX,
  67	VPCCMD_R_BL,
  68	VPCCMD_W_BL,
  69	VPCCMD_R_WIFI,
  70	VPCCMD_W_WIFI,
  71	VPCCMD_R_BT,
  72	VPCCMD_W_BT,
  73	VPCCMD_R_BL_POWER,
  74	VPCCMD_R_NOVO,
  75	VPCCMD_R_VPC2,
  76	VPCCMD_R_TOUCHPAD,
  77	VPCCMD_W_TOUCHPAD,
  78	VPCCMD_R_CAMERA,
  79	VPCCMD_W_CAMERA,
  80	VPCCMD_R_3G,
  81	VPCCMD_W_3G,
  82	VPCCMD_R_ODD, /* 0x21 */
  83	VPCCMD_W_FAN,
  84	VPCCMD_R_RF,
  85	VPCCMD_W_RF,
  86	VPCCMD_R_FAN = 0x2B,
  87	VPCCMD_R_SPECIAL_BUTTONS = 0x31,
  88	VPCCMD_W_BL_POWER = 0x33,
  89};
  90
 
 
 
 
 
 
 
  91struct ideapad_rfk_priv {
  92	int dev;
  93	struct ideapad_private *priv;
  94};
  95
  96struct ideapad_private {
  97	struct acpi_device *adev;
  98	struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
  99	struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
 100	struct platform_device *platform_device;
 101	struct input_dev *inputdev;
 102	struct backlight_device *blightdev;
 
 103	struct dentry *debug;
 104	unsigned long cfg;
 105	bool has_hw_rfkill_switch;
 106	const char *fnesc_guid;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 107};
 108
 109static bool no_bt_rfkill;
 110module_param(no_bt_rfkill, bool, 0444);
 111MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
 112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 113/*
 114 * ACPI Helpers
 115 */
 116#define IDEAPAD_EC_TIMEOUT (200) /* in ms */
 117
 118static int read_method_int(acpi_handle handle, const char *method, int *val)
 119{
 
 120	acpi_status status;
 121	unsigned long long result;
 122
 123	status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
 124	if (ACPI_FAILURE(status)) {
 125		*val = -1;
 126		return -1;
 127	}
 128	*val = result;
 129	return 0;
 
 130
 
 
 
 
 
 131}
 132
 133static int method_gbmd(acpi_handle handle, unsigned long *ret)
 134{
 135	int result, val;
 
 136
 137	result = read_method_int(handle, "GBMD", &val);
 138	*ret = val;
 139	return result;
 140}
 141
 142static int method_sbmc(acpi_handle handle, int cmd)
 143{
 144	acpi_status status;
 
 145
 146	status = acpi_execute_simple_method(handle, "SBMC", cmd);
 147	return ACPI_FAILURE(status) ? -1 : 0;
 
 148}
 149
 150static int method_vpcr(acpi_handle handle, int cmd, int *ret)
 151{
 152	acpi_status status;
 153	unsigned long long result;
 154	struct acpi_object_list params;
 155	union acpi_object in_obj;
 
 156
 157	params.count = 1;
 158	params.pointer = &in_obj;
 159	in_obj.type = ACPI_TYPE_INTEGER;
 160	in_obj.integer.value = cmd;
 161
 162	status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
 
 
 
 
 
 163
 164	if (ACPI_FAILURE(status)) {
 165		*ret = -1;
 166		return -1;
 167	}
 168	*ret = result;
 169	return 0;
 
 
 
 
 
 
 170
 
 
 
 171}
 172
 173static int method_vpcw(acpi_handle handle, int cmd, int data)
 174{
 175	struct acpi_object_list params;
 176	union acpi_object in_obj[2];
 177	acpi_status status;
 178
 179	params.count = 2;
 180	params.pointer = in_obj;
 181	in_obj[0].type = ACPI_TYPE_INTEGER;
 182	in_obj[0].integer.value = cmd;
 183	in_obj[1].type = ACPI_TYPE_INTEGER;
 184	in_obj[1].integer.value = data;
 185
 186	status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
 187	if (status != AE_OK)
 188		return -1;
 
 189	return 0;
 190}
 191
 192static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
 193{
 194	int val;
 195	unsigned long int end_jiffies;
 
 
 
 
 196
 197	if (method_vpcw(handle, 1, cmd))
 198		return -1;
 199
 200	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
 201	     time_before(jiffies, end_jiffies);) {
 202		schedule();
 203		if (method_vpcr(handle, 1, &val))
 204			return -1;
 205		if (val == 0) {
 206			if (method_vpcr(handle, 0, &val))
 207				return -1;
 208			*data = val;
 209			return 0;
 210		}
 211	}
 212	pr_err("timeout in read_ec_cmd\n");
 213	return -1;
 
 
 214}
 215
 216static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
 217{
 218	int val;
 219	unsigned long int end_jiffies;
 
 
 
 
 
 
 
 
 220
 221	if (method_vpcw(handle, 0, data))
 222		return -1;
 223	if (method_vpcw(handle, 1, cmd))
 224		return -1;
 225
 226	for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
 227	     time_before(jiffies, end_jiffies);) {
 228		schedule();
 229		if (method_vpcr(handle, 1, &val))
 230			return -1;
 
 
 
 231		if (val == 0)
 232			return 0;
 233	}
 234	pr_err("timeout in %s\n", __func__);
 235	return -1;
 
 
 236}
 237
 238/*
 239 * debugfs
 240 */
 241static int debugfs_status_show(struct seq_file *s, void *data)
 242{
 243	struct ideapad_private *priv = s->private;
 244	unsigned long value;
 245
 246	if (!priv)
 247		return -EINVAL;
 248
 249	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
 250		seq_printf(s, "Backlight max:\t%lu\n", value);
 251	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
 252		seq_printf(s, "Backlight now:\t%lu\n", value);
 253	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
 254		seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
 255	seq_printf(s, "=====================\n");
 
 256
 257	if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
 258		seq_printf(s, "Radio status:\t%s(%lu)\n",
 259			   value ? "On" : "Off", value);
 260	if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
 261		seq_printf(s, "Wifi status:\t%s(%lu)\n",
 262			   value ? "On" : "Off", value);
 263	if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
 264		seq_printf(s, "BT status:\t%s(%lu)\n",
 265			   value ? "On" : "Off", value);
 266	if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
 267		seq_printf(s, "3G status:\t%s(%lu)\n",
 268			   value ? "On" : "Off", value);
 269	seq_printf(s, "=====================\n");
 270
 271	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
 272		seq_printf(s, "Touchpad status:%s(%lu)\n",
 273			   value ? "On" : "Off", value);
 274	if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
 275		seq_printf(s, "Camera status:\t%s(%lu)\n",
 276			   value ? "On" : "Off", value);
 277	seq_puts(s, "=====================\n");
 278
 279	if (!method_gbmd(priv->adev->handle, &value)) {
 280		seq_printf(s, "Conservation mode:\t%s(%lu)\n",
 281			   test_bit(BM_CONSERVATION_BIT, &value) ? "On" : "Off",
 282			   value);
 283	}
 284
 285	return 0;
 286}
 287DEFINE_SHOW_ATTRIBUTE(debugfs_status);
 288
 289static int debugfs_cfg_show(struct seq_file *s, void *data)
 290{
 291	struct ideapad_private *priv = s->private;
 292
 293	if (!priv) {
 294		seq_printf(s, "cfg: N/A\n");
 295	} else {
 296		seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
 297			   priv->cfg);
 298		if (test_bit(CFG_BT_BIT, &priv->cfg))
 299			seq_printf(s, "Bluetooth ");
 300		if (test_bit(CFG_3G_BIT, &priv->cfg))
 301			seq_printf(s, "3G ");
 302		if (test_bit(CFG_WIFI_BIT, &priv->cfg))
 303			seq_printf(s, "Wireless ");
 304		if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
 305			seq_printf(s, "Camera ");
 306		seq_printf(s, "\nGraphic: ");
 307		switch ((priv->cfg)&0x700) {
 308		case 0x100:
 309			seq_printf(s, "Intel");
 310			break;
 311		case 0x200:
 312			seq_printf(s, "ATI");
 313			break;
 314		case 0x300:
 315			seq_printf(s, "Nvidia");
 316			break;
 317		case 0x400:
 318			seq_printf(s, "Intel and ATI");
 319			break;
 320		case 0x500:
 321			seq_printf(s, "Intel and Nvidia");
 322			break;
 323		}
 324		seq_printf(s, "\n");
 
 
 
 
 
 
 
 
 
 
 
 325	}
 
 
 326	return 0;
 327}
 328DEFINE_SHOW_ATTRIBUTE(debugfs_cfg);
 329
 330static int ideapad_debugfs_init(struct ideapad_private *priv)
 331{
 332	struct dentry *node;
 333
 334	priv->debug = debugfs_create_dir("ideapad", NULL);
 335	if (priv->debug == NULL) {
 336		pr_err("failed to create debugfs directory");
 337		goto errout;
 338	}
 339
 340	node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
 341				   &debugfs_cfg_fops);
 342	if (!node) {
 343		pr_err("failed to create cfg in debugfs");
 344		goto errout;
 345	}
 346
 347	node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
 348				   &debugfs_status_fops);
 349	if (!node) {
 350		pr_err("failed to create status in debugfs");
 351		goto errout;
 352	}
 353
 354	return 0;
 355
 356errout:
 357	return -ENOMEM;
 358}
 359
 360static void ideapad_debugfs_exit(struct ideapad_private *priv)
 361{
 362	debugfs_remove_recursive(priv->debug);
 363	priv->debug = NULL;
 364}
 365
 366/*
 367 * sysfs
 368 */
 369static ssize_t show_ideapad_cam(struct device *dev,
 370				struct device_attribute *attr,
 371				char *buf)
 372{
 
 373	unsigned long result;
 
 
 
 
 
 
 
 
 
 
 
 
 
 374	struct ideapad_private *priv = dev_get_drvdata(dev);
 
 
 
 
 
 
 
 
 
 
 375
 376	if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
 377		return sprintf(buf, "-1\n");
 378	return sprintf(buf, "%lu\n", result);
 379}
 380
 381static ssize_t store_ideapad_cam(struct device *dev,
 382				 struct device_attribute *attr,
 383				 const char *buf, size_t count)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 384{
 385	int ret, state;
 386	struct ideapad_private *priv = dev_get_drvdata(dev);
 
 
 387
 388	if (!count)
 389		return 0;
 390	if (sscanf(buf, "%i", &state) != 1)
 
 
 391		return -EINVAL;
 392	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
 393	if (ret < 0)
 394		return -EIO;
 
 
 395	return count;
 396}
 397
 398static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
 399
 400static ssize_t show_ideapad_fan(struct device *dev,
 401				struct device_attribute *attr,
 402				char *buf)
 403{
 404	unsigned long result;
 405	struct ideapad_private *priv = dev_get_drvdata(dev);
 
 
 
 
 
 
 406
 407	if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
 408		return sprintf(buf, "-1\n");
 409	return sprintf(buf, "%lu\n", result);
 410}
 411
 412static ssize_t store_ideapad_fan(struct device *dev,
 413				 struct device_attribute *attr,
 414				 const char *buf, size_t count)
 415{
 416	int ret, state;
 417	struct ideapad_private *priv = dev_get_drvdata(dev);
 
 
 
 
 
 
 
 
 
 
 418
 419	if (!count)
 420		return 0;
 421	if (sscanf(buf, "%i", &state) != 1)
 422		return -EINVAL;
 423	if (state < 0 || state > 4 || state == 3)
 424		return -EINVAL;
 425	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
 426	if (ret < 0)
 427		return -EIO;
 428	return count;
 429}
 430
 431static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
 432
 433static ssize_t touchpad_show(struct device *dev,
 434			     struct device_attribute *attr,
 435			     char *buf)
 436{
 437	struct ideapad_private *priv = dev_get_drvdata(dev);
 438	unsigned long result;
 
 
 
 
 
 
 
 439
 440	if (read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &result))
 441		return sprintf(buf, "-1\n");
 442	return sprintf(buf, "%lu\n", result);
 443}
 444
 445/* Switch to RO for now: It might be revisited in the future */
 446static ssize_t __maybe_unused touchpad_store(struct device *dev,
 447					     struct device_attribute *attr,
 448					     const char *buf, size_t count)
 449{
 450	struct ideapad_private *priv = dev_get_drvdata(dev);
 451	bool state;
 452	int ret;
 
 
 
 
 
 
 
 
 453
 454	ret = kstrtobool(buf, &state);
 455	if (ret)
 456		return ret;
 457
 458	ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_TOUCHPAD, state);
 459	if (ret < 0)
 460		return -EIO;
 461	return count;
 462}
 463
 464static DEVICE_ATTR_RO(touchpad);
 465
 466static ssize_t conservation_mode_show(struct device *dev,
 467				struct device_attribute *attr,
 468				char *buf)
 469{
 470	struct ideapad_private *priv = dev_get_drvdata(dev);
 471	unsigned long result;
 
 
 
 
 
 472
 473	if (method_gbmd(priv->adev->handle, &result))
 474		return sprintf(buf, "-1\n");
 475	return sprintf(buf, "%u\n", test_bit(BM_CONSERVATION_BIT, &result));
 476}
 477
 478static ssize_t conservation_mode_store(struct device *dev,
 479				 struct device_attribute *attr,
 480				 const char *buf, size_t count)
 481{
 482	struct ideapad_private *priv = dev_get_drvdata(dev);
 483	bool state;
 484	int ret;
 485
 486	ret = kstrtobool(buf, &state);
 487	if (ret)
 488		return ret;
 
 
 
 
 489
 490	ret = method_sbmc(priv->adev->handle, state ?
 491					      BMCMD_CONSERVATION_ON :
 492					      BMCMD_CONSERVATION_OFF);
 493	if (ret < 0)
 494		return -EIO;
 495	return count;
 496}
 497
 498static DEVICE_ATTR_RW(conservation_mode);
 499
 500static struct attribute *ideapad_attributes[] = {
 501	&dev_attr_camera_power.attr,
 
 502	&dev_attr_fan_mode.attr,
 
 503	&dev_attr_touchpad.attr,
 504	&dev_attr_conservation_mode.attr,
 505	NULL
 506};
 507
 508static umode_t ideapad_is_visible(struct kobject *kobj,
 509				 struct attribute *attr,
 510				 int idx)
 511{
 512	struct device *dev = container_of(kobj, struct device, kobj);
 513	struct ideapad_private *priv = dev_get_drvdata(dev);
 514	bool supported;
 515
 516	if (attr == &dev_attr_camera_power.attr)
 517		supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
 518	else if (attr == &dev_attr_fan_mode.attr) {
 519		unsigned long value;
 520		supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
 521					  &value);
 522	} else if (attr == &dev_attr_conservation_mode.attr) {
 523		supported = acpi_has_method(priv->adev->handle, "GBMD") &&
 524			    acpi_has_method(priv->adev->handle, "SBMC");
 525	} else
 526		supported = true;
 
 527
 528	return supported ? attr->mode : 0;
 529}
 530
 531static const struct attribute_group ideapad_attribute_group = {
 532	.is_visible = ideapad_is_visible,
 533	.attrs = ideapad_attributes
 534};
 535
 536/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 537 * Rfkill
 538 */
 539struct ideapad_rfk_data {
 540	char *name;
 541	int cfgbit;
 542	int opcode;
 543	int type;
 544};
 545
 546static const struct ideapad_rfk_data ideapad_rfk_data[] = {
 547	{ "ideapad_wlan",    CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
 548	{ "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
 549	{ "ideapad_3g",        CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
 550};
 551
 552static int ideapad_rfk_set(void *data, bool blocked)
 553{
 554	struct ideapad_rfk_priv *priv = data;
 555	int opcode = ideapad_rfk_data[priv->dev].opcode;
 556
 557	return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
 558}
 559
 560static const struct rfkill_ops ideapad_rfk_ops = {
 561	.set_block = ideapad_rfk_set,
 562};
 563
 564static void ideapad_sync_rfk_state(struct ideapad_private *priv)
 565{
 566	unsigned long hw_blocked = 0;
 567	int i;
 568
 569	if (priv->has_hw_rfkill_switch) {
 570		if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
 571			return;
 572		hw_blocked = !hw_blocked;
 573	}
 574
 575	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
 576		if (priv->rfk[i])
 577			rfkill_set_hw_state(priv->rfk[i], hw_blocked);
 578}
 579
 580static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
 581{
 582	int ret;
 583	unsigned long sw_blocked;
 584
 585	if (no_bt_rfkill &&
 586	    (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
 587		/* Force to enable bluetooth when no_bt_rfkill=1 */
 588		write_ec_cmd(priv->adev->handle,
 589			     ideapad_rfk_data[dev].opcode, 1);
 590		return 0;
 591	}
 
 592	priv->rfk_priv[dev].dev = dev;
 593	priv->rfk_priv[dev].priv = priv;
 594
 595	priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
 596				      &priv->platform_device->dev,
 597				      ideapad_rfk_data[dev].type,
 598				      &ideapad_rfk_ops,
 599				      &priv->rfk_priv[dev]);
 600	if (!priv->rfk[dev])
 601		return -ENOMEM;
 602
 603	if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
 604			 &sw_blocked)) {
 605		rfkill_init_sw_state(priv->rfk[dev], 0);
 606	} else {
 607		sw_blocked = !sw_blocked;
 608		rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
 609	}
 610
 611	ret = rfkill_register(priv->rfk[dev]);
 612	if (ret) {
 613		rfkill_destroy(priv->rfk[dev]);
 614		return ret;
 615	}
 616	return 0;
 617}
 618
 619static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
 620{
 621	if (!priv->rfk[dev])
 622		return;
 623
 624	rfkill_unregister(priv->rfk[dev]);
 625	rfkill_destroy(priv->rfk[dev]);
 626}
 627
 628/*
 629 * Platform device
 630 */
 631static int ideapad_sysfs_init(struct ideapad_private *priv)
 632{
 633	return sysfs_create_group(&priv->platform_device->dev.kobj,
 634				    &ideapad_attribute_group);
 635}
 636
 637static void ideapad_sysfs_exit(struct ideapad_private *priv)
 638{
 639	sysfs_remove_group(&priv->platform_device->dev.kobj,
 640			   &ideapad_attribute_group);
 641}
 642
 643/*
 644 * input device
 645 */
 
 
 646static const struct key_entry ideapad_keymap[] = {
 647	{ KE_KEY, 6,  { KEY_SWITCHVIDEOMODE } },
 648	{ KE_KEY, 7,  { KEY_CAMERA } },
 649	{ KE_KEY, 8,  { KEY_MICMUTE } },
 650	{ KE_KEY, 11, { KEY_F16 } },
 651	{ KE_KEY, 13, { KEY_WLAN } },
 652	{ KE_KEY, 16, { KEY_PROG1 } },
 653	{ KE_KEY, 17, { KEY_PROG2 } },
 654	{ KE_KEY, 64, { KEY_PROG3 } },
 655	{ KE_KEY, 65, { KEY_PROG4 } },
 656	{ KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
 657	{ KE_KEY, 67, { KEY_TOUCHPAD_ON } },
 
 658	{ KE_KEY, 128, { KEY_ESC } },
 659
 660	{ KE_END, 0 },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 661};
 662
 663static int ideapad_input_init(struct ideapad_private *priv)
 664{
 665	struct input_dev *inputdev;
 666	int error;
 667
 668	inputdev = input_allocate_device();
 669	if (!inputdev)
 670		return -ENOMEM;
 671
 672	inputdev->name = "Ideapad extra buttons";
 673	inputdev->phys = "ideapad/input0";
 674	inputdev->id.bustype = BUS_HOST;
 675	inputdev->dev.parent = &priv->platform_device->dev;
 676
 677	error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
 678	if (error) {
 679		pr_err("Unable to setup input device keymap\n");
 
 680		goto err_free_dev;
 681	}
 682
 683	error = input_register_device(inputdev);
 684	if (error) {
 685		pr_err("Unable to register input device\n");
 
 686		goto err_free_dev;
 687	}
 688
 689	priv->inputdev = inputdev;
 
 690	return 0;
 691
 692err_free_dev:
 693	input_free_device(inputdev);
 694	return error;
 
 695}
 696
 697static void ideapad_input_exit(struct ideapad_private *priv)
 698{
 699	input_unregister_device(priv->inputdev);
 700	priv->inputdev = NULL;
 701}
 702
 703static void ideapad_input_report(struct ideapad_private *priv,
 704				 unsigned long scancode)
 705{
 706	sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
 707}
 708
 709static void ideapad_input_novokey(struct ideapad_private *priv)
 710{
 711	unsigned long long_pressed;
 712
 713	if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
 714		return;
 
 715	if (long_pressed)
 716		ideapad_input_report(priv, 17);
 717	else
 718		ideapad_input_report(priv, 16);
 719}
 720
 721static void ideapad_check_special_buttons(struct ideapad_private *priv)
 722{
 723	unsigned long bit, value;
 724
 725	read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
 
 726
 727	for (bit = 0; bit < 16; bit++) {
 728		if (test_bit(bit, &value)) {
 729			switch (bit) {
 730			case 0:	/* Z580 */
 731			case 6:	/* Z570 */
 732				/* Thermal Management button */
 733				ideapad_input_report(priv, 65);
 734				break;
 735			case 1:
 736				/* OneKey Theater button */
 737				ideapad_input_report(priv, 64);
 738				break;
 739			default:
 740				pr_info("Unknown special button: %lu\n", bit);
 741				break;
 742			}
 743		}
 744	}
 745}
 746
 747/*
 748 * backlight
 749 */
 750static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
 751{
 752	struct ideapad_private *priv = bl_get_data(blightdev);
 753	unsigned long now;
 
 754
 755	if (!priv)
 756		return -EINVAL;
 
 757
 758	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
 759		return -EIO;
 760	return now;
 761}
 762
 763static int ideapad_backlight_update_status(struct backlight_device *blightdev)
 764{
 765	struct ideapad_private *priv = bl_get_data(blightdev);
 
 766
 767	if (!priv)
 768		return -EINVAL;
 769
 770	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
 771			 blightdev->props.brightness))
 772		return -EIO;
 773	if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
 774			 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
 775		return -EIO;
 776
 777	return 0;
 778}
 779
 780static const struct backlight_ops ideapad_backlight_ops = {
 781	.get_brightness = ideapad_backlight_get_brightness,
 782	.update_status = ideapad_backlight_update_status,
 783};
 784
 785static int ideapad_backlight_init(struct ideapad_private *priv)
 786{
 787	struct backlight_device *blightdev;
 788	struct backlight_properties props;
 789	unsigned long max, now, power;
 
 
 
 
 
 
 
 
 
 790
 791	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
 792		return -EIO;
 793	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
 794		return -EIO;
 795	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
 796		return -EIO;
 797
 798	memset(&props, 0, sizeof(struct backlight_properties));
 799	props.max_brightness = max;
 800	props.type = BACKLIGHT_PLATFORM;
 
 801	blightdev = backlight_device_register("ideapad",
 802					      &priv->platform_device->dev,
 803					      priv,
 804					      &ideapad_backlight_ops,
 805					      &props);
 806	if (IS_ERR(blightdev)) {
 807		pr_err("Could not register backlight device\n");
 808		return PTR_ERR(blightdev);
 
 
 809	}
 810
 811	priv->blightdev = blightdev;
 812	blightdev->props.brightness = now;
 813	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
 
 814	backlight_update_status(blightdev);
 815
 816	return 0;
 817}
 818
 819static void ideapad_backlight_exit(struct ideapad_private *priv)
 820{
 821	backlight_device_unregister(priv->blightdev);
 822	priv->blightdev = NULL;
 823}
 824
 825static void ideapad_backlight_notify_power(struct ideapad_private *priv)
 826{
 
 827	unsigned long power;
 828	struct backlight_device *blightdev = priv->blightdev;
 829
 830	if (!blightdev)
 831		return;
 
 832	if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
 833		return;
 
 834	blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
 835}
 836
 837static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
 838{
 839	unsigned long now;
 840
 841	/* if we control brightness via acpi video driver */
 842	if (priv->blightdev == NULL) {
 843		read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 844		return;
 845	}
 846
 847	backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 848}
 849
 850/*
 851 * module init/exit
 852 */
 853static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
 854{
 855	unsigned long value;
 
 
 856
 857	/* Without reading from EC touchpad LED doesn't switch state */
 858	if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
 859		/* Some IdeaPads don't really turn off touchpad - they only
 860		 * switch the LED state. We (de)activate KBC AUX port to turn
 861		 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
 862		 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
 863		unsigned char param;
 864		i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
 865			      I8042_CMD_AUX_DISABLE);
 866		ideapad_input_report(priv, value ? 67 : 66);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 867	}
 
 
 868}
 869
 870static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
 871{
 872	struct ideapad_private *priv = data;
 873	unsigned long vpc1, vpc2, vpc_bit;
 874
 875	if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
 876		return;
 
 877	if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
 878		return;
 879
 880	vpc1 = (vpc2 << 8) | vpc1;
 881	for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
 882		if (test_bit(vpc_bit, &vpc1)) {
 883			switch (vpc_bit) {
 884			case 9:
 885				ideapad_sync_rfk_state(priv);
 886				break;
 887			case 13:
 888			case 11:
 889			case 8:
 890			case 7:
 891			case 6:
 892				ideapad_input_report(priv, vpc_bit);
 893				break;
 894			case 5:
 895				ideapad_sync_touchpad_state(priv);
 896				break;
 897			case 4:
 898				ideapad_backlight_notify_brightness(priv);
 899				break;
 900			case 3:
 901				ideapad_input_novokey(priv);
 902				break;
 903			case 2:
 904				ideapad_backlight_notify_power(priv);
 905				break;
 906			case 0:
 907				ideapad_check_special_buttons(priv);
 908				break;
 909			case 1:
 910				/* Some IdeaPads report event 1 every ~20
 911				 * seconds while on battery power; some
 912				 * report this when changing to/from tablet
 913				 * mode. Squelch this event.
 914				 */
 915				break;
 916			default:
 917				pr_info("Unknown event: %lu\n", vpc_bit);
 918			}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 919		}
 920	}
 921}
 922
 923#if IS_ENABLED(CONFIG_ACPI_WMI)
 924static void ideapad_wmi_notify(u32 value, void *context)
 925{
 926	switch (value) {
 927	case 128:
 928		ideapad_input_report(context, value);
 929		break;
 930	default:
 931		pr_info("Unknown WMI event %u\n", value);
 932	}
 933}
 934#endif
 935
 936/*
 937 * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
 938 * always results in 0 on these models, causing ideapad_laptop to wrongly
 939 * report all radios as hardware-blocked.
 940 */
 941static const struct dmi_system_id no_hw_rfkill_list[] = {
 942	{
 943		.ident = "Lenovo RESCUER R720-15IKBN",
 944		.matches = {
 945			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 946			DMI_MATCH(DMI_BOARD_NAME, "80WW"),
 947		},
 948	},
 949	{
 950		.ident = "Lenovo G40-30",
 951		.matches = {
 952			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 953			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G40-30"),
 954		},
 955	},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 956	{
 957		.ident = "Lenovo G50-30",
 958		.matches = {
 959			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 960			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G50-30"),
 961		},
 962	},
 963	{
 964		.ident = "Lenovo V310-14IKB",
 965		.matches = {
 966			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 967			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14IKB"),
 968		},
 969	},
 970	{
 971		.ident = "Lenovo V310-14ISK",
 972		.matches = {
 973			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 974			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-14ISK"),
 975		},
 976	},
 977	{
 978		.ident = "Lenovo V310-15IKB",
 979		.matches = {
 980			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 981			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15IKB"),
 982		},
 983	},
 984	{
 985		.ident = "Lenovo V310-15ISK",
 986		.matches = {
 987			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 988			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V310-15ISK"),
 989		},
 990	},
 991	{
 992		.ident = "Lenovo V510-15IKB",
 993		.matches = {
 994			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
 995			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo V510-15IKB"),
 996		},
 997	},
 998	{
 999		.ident = "Lenovo ideapad 300-15IBR",
1000		.matches = {
1001			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1002			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IBR"),
1003		},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1004	},
1005	{
1006		.ident = "Lenovo ideapad 300-15IKB",
1007		.matches = {
1008			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1009			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300-15IKB"),
1010		},
1011	},
1012	{
1013		.ident = "Lenovo ideapad 300S-11IBR",
1014		.matches = {
1015			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1016			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 300S-11BR"),
1017		},
1018	},
1019	{
1020		.ident = "Lenovo ideapad 310-15ABR",
1021		.matches = {
1022			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1023			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ABR"),
1024		},
1025	},
1026	{
1027		.ident = "Lenovo ideapad 310-15IAP",
1028		.matches = {
1029			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1030			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IAP"),
1031		},
1032	},
1033	{
1034		.ident = "Lenovo ideapad 310-15IKB",
1035		.matches = {
1036			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1037			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15IKB"),
1038		},
1039	},
1040	{
1041		.ident = "Lenovo ideapad 310-15ISK",
1042		.matches = {
1043			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1044			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 310-15ISK"),
1045		},
1046	},
1047	{
1048		.ident = "Lenovo ideapad Y700-14ISK",
1049		.matches = {
1050			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1051			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-14ISK"),
1052		},
1053	},
1054	{
1055		.ident = "Lenovo ideapad Y700-15ACZ",
1056		.matches = {
1057			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1058			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ACZ"),
1059		},
1060	},
1061	{
1062		.ident = "Lenovo ideapad Y700-15ISK",
1063		.matches = {
1064			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1065			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ISK"),
1066		},
1067	},
1068	{
1069		.ident = "Lenovo ideapad Y700 Touch-15ISK",
1070		.matches = {
1071			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1072			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700 Touch-15ISK"),
1073		},
1074	},
1075	{
1076		.ident = "Lenovo ideapad Y700-17ISK",
1077		.matches = {
1078			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1079			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-17ISK"),
1080		},
1081	},
1082	{
1083		.ident = "Lenovo Legion Y520-15IKBN",
1084		.matches = {
1085			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1086			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y520-15IKBN"),
1087		},
1088	},
1089	{
1090		.ident = "Lenovo Legion Y720-15IKB",
1091		.matches = {
1092			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1093			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKB"),
1094		},
1095	},
1096	{
1097		.ident = "Lenovo Legion Y720-15IKBN",
1098		.matches = {
1099			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1100			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Y720-15IKBN"),
1101		},
1102	},
1103	{
1104		.ident = "Lenovo Yoga 2 11 / 13 / Pro",
1105		.matches = {
1106			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1107			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
1108		},
1109	},
1110	{
1111		.ident = "Lenovo Yoga 2 11 / 13 / Pro",
1112		.matches = {
1113			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1114			DMI_MATCH(DMI_BOARD_NAME, "Yoga2"),
1115		},
1116	},
1117	{
1118		.ident = "Lenovo Yoga 3 1170 / 1470",
1119		.matches = {
1120			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1121			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 3"),
1122		},
1123	},
1124	{
1125		.ident = "Lenovo Yoga 3 Pro 1370",
1126		.matches = {
1127			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1128			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
1129		},
1130	},
1131	{
1132		.ident = "Lenovo Yoga 700",
1133		.matches = {
1134			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1135			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 700"),
1136		},
1137	},
1138	{
1139		.ident = "Lenovo Yoga 900",
1140		.matches = {
1141			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1142			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 900"),
1143		},
1144	},
1145	{
1146		.ident = "Lenovo Yoga 900",
1147		.matches = {
1148			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1149			DMI_MATCH(DMI_BOARD_NAME, "VIUU4"),
1150		},
1151	},
1152	{
1153		.ident = "Lenovo YOGA 910-13IKB",
1154		.matches = {
1155			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1156			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 910-13IKB"),
1157		},
1158	},
1159	{
1160		.ident = "Lenovo YOGA 920-13IKB",
1161		.matches = {
1162			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1163			DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 920-13IKB"),
1164		},
1165	},
1166	{}
1167};
1168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1169static int ideapad_acpi_add(struct platform_device *pdev)
1170{
1171	int ret, i;
1172	int cfg;
1173	struct ideapad_private *priv;
1174	struct acpi_device *adev;
 
 
1175
1176	ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
1177	if (ret)
1178		return -ENODEV;
1179
1180	if (read_method_int(adev->handle, "_CFG", &cfg))
1181		return -ENODEV;
1182
1183	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1184	if (!priv)
1185		return -ENOMEM;
1186
1187	dev_set_drvdata(&pdev->dev, priv);
 
1188	priv->cfg = cfg;
1189	priv->adev = adev;
1190	priv->platform_device = pdev;
1191	priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
1192
1193	ret = ideapad_sysfs_init(priv);
1194	if (ret)
1195		return ret;
 
 
1196
1197	ret = ideapad_debugfs_init(priv);
1198	if (ret)
1199		goto debugfs_failed;
1200
1201	ret = ideapad_input_init(priv);
1202	if (ret)
1203		goto input_failed;
1204
 
 
 
 
 
 
 
 
1205	/*
1206	 * On some models without a hw-switch (the yoga 2 13 at least)
1207	 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
1208	 */
1209	if (!priv->has_hw_rfkill_switch)
1210		write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
1211
1212	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1213		if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
1214			ideapad_register_rfkill(priv, i);
1215
1216	ideapad_sync_rfk_state(priv);
1217	ideapad_sync_touchpad_state(priv);
 
 
 
 
 
 
 
 
1218
1219	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1220		ret = ideapad_backlight_init(priv);
1221		if (ret && ret != -ENODEV)
1222			goto backlight_failed;
1223	}
1224	ret = acpi_install_notify_handler(adev->handle,
1225		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
1226	if (ret)
 
 
 
1227		goto notification_failed;
 
1228
1229#if IS_ENABLED(CONFIG_ACPI_WMI)
1230	for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
1231		ret = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
1232						 ideapad_wmi_notify, priv);
1233		if (ret == AE_OK) {
1234			priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
1235			break;
1236		}
1237	}
1238	if (ret != AE_OK && ret != AE_NOT_EXIST)
1239		goto notification_failed_wmi;
1240#endif
1241
1242	return 0;
1243#if IS_ENABLED(CONFIG_ACPI_WMI)
1244notification_failed_wmi:
1245	acpi_remove_notify_handler(priv->adev->handle,
1246		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1247#endif
 
1248notification_failed:
1249	ideapad_backlight_exit(priv);
 
1250backlight_failed:
 
 
1251	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1252		ideapad_unregister_rfkill(priv, i);
 
 
1253	ideapad_input_exit(priv);
 
1254input_failed:
1255	ideapad_debugfs_exit(priv);
1256debugfs_failed:
1257	ideapad_sysfs_exit(priv);
1258	return ret;
 
1259}
1260
1261static int ideapad_acpi_remove(struct platform_device *pdev)
1262{
1263	struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1264	int i;
1265
1266#if IS_ENABLED(CONFIG_ACPI_WMI)
1267	if (priv->fnesc_guid)
1268		wmi_remove_notify_handler(priv->fnesc_guid);
1269#endif
1270	acpi_remove_notify_handler(priv->adev->handle,
1271		ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
 
 
1272	ideapad_backlight_exit(priv);
 
 
1273	for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1274		ideapad_unregister_rfkill(priv, i);
 
 
1275	ideapad_input_exit(priv);
1276	ideapad_debugfs_exit(priv);
1277	ideapad_sysfs_exit(priv);
1278	dev_set_drvdata(&pdev->dev, NULL);
1279
1280	return 0;
1281}
1282
1283#ifdef CONFIG_PM_SLEEP
1284static int ideapad_acpi_resume(struct device *device)
1285{
1286	struct ideapad_private *priv;
 
 
 
1287
1288	if (!device)
1289		return -EINVAL;
1290	priv = dev_get_drvdata(device);
1291
1292	ideapad_sync_rfk_state(priv);
1293	ideapad_sync_touchpad_state(priv);
1294	return 0;
1295}
1296#endif
1297static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1298
1299static const struct acpi_device_id ideapad_device_ids[] = {
1300	{ "VPC2004", 0},
1301	{ "", 0},
1302};
1303MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1304
1305static struct platform_driver ideapad_acpi_driver = {
1306	.probe = ideapad_acpi_add,
1307	.remove = ideapad_acpi_remove,
1308	.driver = {
1309		.name   = "ideapad_acpi",
1310		.pm     = &ideapad_pm,
1311		.acpi_match_table = ACPI_PTR(ideapad_device_ids),
1312	},
1313};
1314
1315module_platform_driver(ideapad_acpi_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1316
1317MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1318MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1319MODULE_LICENSE("GPL");