blob: 4a23fbc66b715bcc4fce2a6ddccb2c8b743ae7b2 [file] [log] [blame]
David Woodhouse58ac7aa2010-08-10 23:44:05 +01001/*
Ike Panhca4b5a272010-12-13 18:00:48 +08002 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
David Woodhouse58ac7aa2010-08-10 23:44:05 +01003 *
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
Joe Perches9ab23982011-03-29 15:21:43 -070023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
David Woodhouse58ac7aa2010-08-10 23:44:05 +010025#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
Lv Zheng8b484632013-12-03 08:49:16 +080029#include <linux/acpi.h>
David Woodhouse58ac7aa2010-08-10 23:44:05 +010030#include <linux/rfkill.h>
Ike Panhc98ee6912010-12-13 18:00:15 +080031#include <linux/platform_device.h>
Ike Panhcf63409a2010-12-13 18:00:38 +080032#include <linux/input.h>
33#include <linux/input/sparse-keymap.h>
Ike Panhca4ecbb82011-06-30 19:50:52 +080034#include <linux/backlight.h>
35#include <linux/fb.h>
Ike Panhc773e3202011-09-06 02:32:52 +080036#include <linux/debugfs.h>
37#include <linux/seq_file.h>
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +080038#include <linux/i8042.h>
Hans de Goede85093f72014-05-13 16:00:28 +020039#include <linux/dmi.h>
Himangi Saraogib3facd72014-06-09 17:46:50 -040040#include <linux/device.h>
Hans de Goede26bff5f2015-06-16 16:28:04 +020041#include <acpi/video.h>
David Woodhouse58ac7aa2010-08-10 23:44:05 +010042
Ike Panhcc1f73652010-12-13 18:01:12 +080043#define IDEAPAD_RFKILL_DEV_NUM (3)
David Woodhouse58ac7aa2010-08-10 23:44:05 +010044
Ike Panhc3371f482011-06-30 19:50:40 +080045#define CFG_BT_BIT (16)
46#define CFG_3G_BIT (17)
47#define CFG_WIFI_BIT (18)
Ike Panhca84511f2011-06-30 19:50:47 +080048#define CFG_CAMERA_BIT (19)
Ike Panhc3371f482011-06-30 19:50:40 +080049
Arnd Bergmann74caab92015-11-06 22:28:49 +010050#if IS_ENABLED(CONFIG_ACPI_WMI)
Arnd Bergmann2d98e0b2016-05-09 23:49:21 +020051static const char *const ideapad_wmi_fnesc_events[] = {
52 "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6", /* Yoga 3 */
53 "56322276-8493-4CE8-A783-98C991274F5E", /* Yoga 700 */
54};
Arnd Bergmann74caab92015-11-06 22:28:49 +010055#endif
56
Ike Panhc2be1dc22011-09-06 02:31:53 +080057enum {
58 VPCCMD_R_VPC1 = 0x10,
59 VPCCMD_R_BL_MAX,
60 VPCCMD_R_BL,
61 VPCCMD_W_BL,
62 VPCCMD_R_WIFI,
63 VPCCMD_W_WIFI,
64 VPCCMD_R_BT,
65 VPCCMD_W_BT,
66 VPCCMD_R_BL_POWER,
67 VPCCMD_R_NOVO,
68 VPCCMD_R_VPC2,
69 VPCCMD_R_TOUCHPAD,
70 VPCCMD_W_TOUCHPAD,
71 VPCCMD_R_CAMERA,
72 VPCCMD_W_CAMERA,
73 VPCCMD_R_3G,
74 VPCCMD_W_3G,
75 VPCCMD_R_ODD, /* 0x21 */
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +080076 VPCCMD_W_FAN,
77 VPCCMD_R_RF,
Ike Panhc2be1dc22011-09-06 02:31:53 +080078 VPCCMD_W_RF,
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +080079 VPCCMD_R_FAN = 0x2B,
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +080080 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
Ike Panhc2be1dc22011-09-06 02:31:53 +080081 VPCCMD_W_BL_POWER = 0x33,
82};
83
Zhang Rui331e0ea2013-09-25 20:39:49 +080084struct ideapad_rfk_priv {
85 int dev;
86 struct ideapad_private *priv;
87};
88
David Woodhousece326322010-08-11 17:59:35 +010089struct ideapad_private {
Zhang Rui469f6432013-09-25 20:39:47 +080090 struct acpi_device *adev;
Ike Panhcc1f73652010-12-13 18:01:12 +080091 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
Zhang Rui331e0ea2013-09-25 20:39:49 +080092 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
Ike Panhc98ee6912010-12-13 18:00:15 +080093 struct platform_device *platform_device;
Ike Panhcf63409a2010-12-13 18:00:38 +080094 struct input_dev *inputdev;
Ike Panhca4ecbb82011-06-30 19:50:52 +080095 struct backlight_device *blightdev;
Ike Panhc773e3202011-09-06 02:32:52 +080096 struct dentry *debug;
Ike Panhc3371f482011-06-30 19:50:40 +080097 unsigned long cfg;
Hans de Goedece363c22014-06-23 16:45:51 +020098 bool has_hw_rfkill_switch;
Arnd Bergmann2d98e0b2016-05-09 23:49:21 +020099 const char *fnesc_guid;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100100};
101
Ike Panhcbfa97b72010-10-01 15:40:22 +0800102static bool no_bt_rfkill;
103module_param(no_bt_rfkill, bool, 0444);
104MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
105
Ike Panhc6a09f212010-10-01 15:38:46 +0800106/*
107 * ACPI Helpers
108 */
109#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
110
111static int read_method_int(acpi_handle handle, const char *method, int *val)
112{
113 acpi_status status;
114 unsigned long long result;
115
116 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
117 if (ACPI_FAILURE(status)) {
118 *val = -1;
119 return -1;
120 } else {
121 *val = result;
122 return 0;
123 }
124}
125
126static int method_vpcr(acpi_handle handle, int cmd, int *ret)
127{
128 acpi_status status;
129 unsigned long long result;
130 struct acpi_object_list params;
131 union acpi_object in_obj;
132
133 params.count = 1;
134 params.pointer = &in_obj;
135 in_obj.type = ACPI_TYPE_INTEGER;
136 in_obj.integer.value = cmd;
137
138 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
139
140 if (ACPI_FAILURE(status)) {
141 *ret = -1;
142 return -1;
143 } else {
144 *ret = result;
145 return 0;
146 }
147}
148
149static int method_vpcw(acpi_handle handle, int cmd, int data)
150{
151 struct acpi_object_list params;
152 union acpi_object in_obj[2];
153 acpi_status status;
154
155 params.count = 2;
156 params.pointer = in_obj;
157 in_obj[0].type = ACPI_TYPE_INTEGER;
158 in_obj[0].integer.value = cmd;
159 in_obj[1].type = ACPI_TYPE_INTEGER;
160 in_obj[1].integer.value = data;
161
162 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
163 if (status != AE_OK)
164 return -1;
165 return 0;
166}
167
168static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
169{
170 int val;
171 unsigned long int end_jiffies;
172
173 if (method_vpcw(handle, 1, cmd))
174 return -1;
175
176 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
177 time_before(jiffies, end_jiffies);) {
178 schedule();
179 if (method_vpcr(handle, 1, &val))
180 return -1;
181 if (val == 0) {
182 if (method_vpcr(handle, 0, &val))
183 return -1;
184 *data = val;
185 return 0;
186 }
187 }
188 pr_err("timeout in read_ec_cmd\n");
189 return -1;
190}
191
192static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
193{
194 int val;
195 unsigned long int end_jiffies;
196
197 if (method_vpcw(handle, 0, data))
198 return -1;
199 if (method_vpcw(handle, 1, cmd))
200 return -1;
201
202 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
203 time_before(jiffies, end_jiffies);) {
204 schedule();
205 if (method_vpcr(handle, 1, &val))
206 return -1;
207 if (val == 0)
208 return 0;
209 }
210 pr_err("timeout in write_ec_cmd\n");
211 return -1;
212}
Ike Panhc6a09f212010-10-01 15:38:46 +0800213
Ike Panhca4b5a272010-12-13 18:00:48 +0800214/*
Ike Panhc773e3202011-09-06 02:32:52 +0800215 * debugfs
216 */
Ike Panhc773e3202011-09-06 02:32:52 +0800217static int debugfs_status_show(struct seq_file *s, void *data)
218{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800219 struct ideapad_private *priv = s->private;
Ike Panhc773e3202011-09-06 02:32:52 +0800220 unsigned long value;
221
Zhang Rui331e0ea2013-09-25 20:39:49 +0800222 if (!priv)
223 return -EINVAL;
224
225 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800226 seq_printf(s, "Backlight max:\t%lu\n", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800227 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800228 seq_printf(s, "Backlight now:\t%lu\n", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800229 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800230 seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
231 seq_printf(s, "=====================\n");
232
Zhang Rui331e0ea2013-09-25 20:39:49 +0800233 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800234 seq_printf(s, "Radio status:\t%s(%lu)\n",
235 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800236 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800237 seq_printf(s, "Wifi status:\t%s(%lu)\n",
238 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800239 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800240 seq_printf(s, "BT status:\t%s(%lu)\n",
241 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800242 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800243 seq_printf(s, "3G status:\t%s(%lu)\n",
244 value ? "On" : "Off", value);
245 seq_printf(s, "=====================\n");
246
Zhang Rui331e0ea2013-09-25 20:39:49 +0800247 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800248 seq_printf(s, "Touchpad status:%s(%lu)\n",
249 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800250 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800251 seq_printf(s, "Camera status:\t%s(%lu)\n",
252 value ? "On" : "Off", value);
253
254 return 0;
255}
256
257static int debugfs_status_open(struct inode *inode, struct file *file)
258{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800259 return single_open(file, debugfs_status_show, inode->i_private);
Ike Panhc773e3202011-09-06 02:32:52 +0800260}
261
262static const struct file_operations debugfs_status_fops = {
263 .owner = THIS_MODULE,
264 .open = debugfs_status_open,
265 .read = seq_read,
266 .llseek = seq_lseek,
267 .release = single_release,
268};
269
270static int debugfs_cfg_show(struct seq_file *s, void *data)
271{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800272 struct ideapad_private *priv = s->private;
273
274 if (!priv) {
Ike Panhc773e3202011-09-06 02:32:52 +0800275 seq_printf(s, "cfg: N/A\n");
276 } else {
277 seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
Zhang Rui331e0ea2013-09-25 20:39:49 +0800278 priv->cfg);
279 if (test_bit(CFG_BT_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800280 seq_printf(s, "Bluetooth ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800281 if (test_bit(CFG_3G_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800282 seq_printf(s, "3G ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800283 if (test_bit(CFG_WIFI_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800284 seq_printf(s, "Wireless ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800285 if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800286 seq_printf(s, "Camera ");
287 seq_printf(s, "\nGraphic: ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800288 switch ((priv->cfg)&0x700) {
Ike Panhc773e3202011-09-06 02:32:52 +0800289 case 0x100:
290 seq_printf(s, "Intel");
291 break;
292 case 0x200:
293 seq_printf(s, "ATI");
294 break;
295 case 0x300:
296 seq_printf(s, "Nvidia");
297 break;
298 case 0x400:
299 seq_printf(s, "Intel and ATI");
300 break;
301 case 0x500:
302 seq_printf(s, "Intel and Nvidia");
303 break;
304 }
305 seq_printf(s, "\n");
306 }
307 return 0;
308}
309
310static int debugfs_cfg_open(struct inode *inode, struct file *file)
311{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800312 return single_open(file, debugfs_cfg_show, inode->i_private);
Ike Panhc773e3202011-09-06 02:32:52 +0800313}
314
315static const struct file_operations debugfs_cfg_fops = {
316 .owner = THIS_MODULE,
317 .open = debugfs_cfg_open,
318 .read = seq_read,
319 .llseek = seq_lseek,
320 .release = single_release,
321};
322
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800323static int ideapad_debugfs_init(struct ideapad_private *priv)
Ike Panhc773e3202011-09-06 02:32:52 +0800324{
325 struct dentry *node;
326
327 priv->debug = debugfs_create_dir("ideapad", NULL);
328 if (priv->debug == NULL) {
329 pr_err("failed to create debugfs directory");
330 goto errout;
331 }
332
Zhang Rui331e0ea2013-09-25 20:39:49 +0800333 node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
Ike Panhc773e3202011-09-06 02:32:52 +0800334 &debugfs_cfg_fops);
335 if (!node) {
336 pr_err("failed to create cfg in debugfs");
337 goto errout;
338 }
339
Zhang Rui331e0ea2013-09-25 20:39:49 +0800340 node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
Ike Panhc773e3202011-09-06 02:32:52 +0800341 &debugfs_status_fops);
342 if (!node) {
Ike Panhca5c38922012-05-03 17:38:35 +0800343 pr_err("failed to create status in debugfs");
Ike Panhc773e3202011-09-06 02:32:52 +0800344 goto errout;
345 }
346
347 return 0;
348
349errout:
350 return -ENOMEM;
351}
352
353static void ideapad_debugfs_exit(struct ideapad_private *priv)
354{
355 debugfs_remove_recursive(priv->debug);
356 priv->debug = NULL;
357}
358
359/*
Ike Panhc3371f482011-06-30 19:50:40 +0800360 * sysfs
Ike Panhca4b5a272010-12-13 18:00:48 +0800361 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100362static ssize_t show_ideapad_cam(struct device *dev,
363 struct device_attribute *attr,
364 char *buf)
365{
Ike Panhc26c81d52010-10-01 15:39:40 +0800366 unsigned long result;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800367 struct ideapad_private *priv = dev_get_drvdata(dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100368
Zhang Rui331e0ea2013-09-25 20:39:49 +0800369 if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
Ike Panhc26c81d52010-10-01 15:39:40 +0800370 return sprintf(buf, "-1\n");
371 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100372}
373
374static ssize_t store_ideapad_cam(struct device *dev,
375 struct device_attribute *attr,
376 const char *buf, size_t count)
377{
378 int ret, state;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800379 struct ideapad_private *priv = dev_get_drvdata(dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100380
381 if (!count)
382 return 0;
383 if (sscanf(buf, "%i", &state) != 1)
384 return -EINVAL;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800385 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100386 if (ret < 0)
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800387 return -EIO;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100388 return count;
389}
390
391static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
392
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800393static ssize_t show_ideapad_fan(struct device *dev,
394 struct device_attribute *attr,
395 char *buf)
396{
397 unsigned long result;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800398 struct ideapad_private *priv = dev_get_drvdata(dev);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800399
Zhang Rui331e0ea2013-09-25 20:39:49 +0800400 if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800401 return sprintf(buf, "-1\n");
402 return sprintf(buf, "%lu\n", result);
403}
404
405static ssize_t store_ideapad_fan(struct device *dev,
406 struct device_attribute *attr,
407 const char *buf, size_t count)
408{
409 int ret, state;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800410 struct ideapad_private *priv = dev_get_drvdata(dev);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800411
412 if (!count)
413 return 0;
414 if (sscanf(buf, "%i", &state) != 1)
415 return -EINVAL;
416 if (state < 0 || state > 4 || state == 3)
417 return -EINVAL;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800418 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800419 if (ret < 0)
420 return -EIO;
421 return count;
422}
423
424static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
425
Ike Panhc3371f482011-06-30 19:50:40 +0800426static struct attribute *ideapad_attributes[] = {
427 &dev_attr_camera_power.attr,
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800428 &dev_attr_fan_mode.attr,
Ike Panhc3371f482011-06-30 19:50:40 +0800429 NULL
430};
431
Al Viro587a1f12011-07-23 23:11:19 -0400432static umode_t ideapad_is_visible(struct kobject *kobj,
Ike Panhca84511f2011-06-30 19:50:47 +0800433 struct attribute *attr,
434 int idx)
435{
436 struct device *dev = container_of(kobj, struct device, kobj);
437 struct ideapad_private *priv = dev_get_drvdata(dev);
438 bool supported;
439
440 if (attr == &dev_attr_camera_power.attr)
441 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800442 else if (attr == &dev_attr_fan_mode.attr) {
443 unsigned long value;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800444 supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
445 &value);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800446 } else
Ike Panhca84511f2011-06-30 19:50:47 +0800447 supported = true;
448
449 return supported ? attr->mode : 0;
450}
451
Mathias Krause49458e82014-07-16 19:43:15 +0200452static const struct attribute_group ideapad_attribute_group = {
Ike Panhca84511f2011-06-30 19:50:47 +0800453 .is_visible = ideapad_is_visible,
Ike Panhc3371f482011-06-30 19:50:40 +0800454 .attrs = ideapad_attributes
455};
456
Ike Panhca4b5a272010-12-13 18:00:48 +0800457/*
458 * Rfkill
459 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800460struct ideapad_rfk_data {
461 char *name;
462 int cfgbit;
463 int opcode;
464 int type;
465};
466
Mathias Krauseb3d94d72014-08-28 13:02:49 +0200467static const struct ideapad_rfk_data ideapad_rfk_data[] = {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800468 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
469 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
470 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
Ike Panhcc1f73652010-12-13 18:01:12 +0800471};
472
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100473static int ideapad_rfk_set(void *data, bool blocked)
474{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800475 struct ideapad_rfk_priv *priv = data;
Arnd Bergmann4b200b42015-06-13 15:23:33 +0200476 int opcode = ideapad_rfk_data[priv->dev].opcode;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100477
Arnd Bergmann4b200b42015-06-13 15:23:33 +0200478 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100479}
480
481static struct rfkill_ops ideapad_rfk_ops = {
482 .set_block = ideapad_rfk_set,
483};
484
Ike Panhc923de842011-09-06 02:32:01 +0800485static void ideapad_sync_rfk_state(struct ideapad_private *priv)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100486{
Hans de Goedece363c22014-06-23 16:45:51 +0200487 unsigned long hw_blocked = 0;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100488 int i;
489
Hans de Goedece363c22014-06-23 16:45:51 +0200490 if (priv->has_hw_rfkill_switch) {
491 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
492 return;
493 hw_blocked = !hw_blocked;
494 }
Ike Panhc2b7266b2010-10-01 15:39:49 +0800495
Ike Panhcc1f73652010-12-13 18:01:12 +0800496 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100497 if (priv->rfk[i])
498 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100499}
500
Zhang Rui75a11f12013-09-25 20:39:48 +0800501static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100502{
503 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800504 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100505
Ike Panhcbfa97b72010-10-01 15:40:22 +0800506 if (no_bt_rfkill &&
507 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
508 /* Force to enable bluetooth when no_bt_rfkill=1 */
Zhang Rui331e0ea2013-09-25 20:39:49 +0800509 write_ec_cmd(priv->adev->handle,
Ike Panhcbfa97b72010-10-01 15:40:22 +0800510 ideapad_rfk_data[dev].opcode, 1);
511 return 0;
512 }
Zhang Rui331e0ea2013-09-25 20:39:49 +0800513 priv->rfk_priv[dev].dev = dev;
514 priv->rfk_priv[dev].priv = priv;
Ike Panhcbfa97b72010-10-01 15:40:22 +0800515
Zhang Rui75a11f12013-09-25 20:39:48 +0800516 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
Zhang Ruib5c37b72013-09-25 20:39:50 +0800517 &priv->platform_device->dev,
Zhang Rui75a11f12013-09-25 20:39:48 +0800518 ideapad_rfk_data[dev].type,
519 &ideapad_rfk_ops,
Zhang Rui331e0ea2013-09-25 20:39:49 +0800520 &priv->rfk_priv[dev]);
David Woodhousece326322010-08-11 17:59:35 +0100521 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100522 return -ENOMEM;
523
Zhang Rui331e0ea2013-09-25 20:39:49 +0800524 if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
Ike Panhc2b7266b2010-10-01 15:39:49 +0800525 &sw_blocked)) {
526 rfkill_init_sw_state(priv->rfk[dev], 0);
527 } else {
528 sw_blocked = !sw_blocked;
529 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
530 }
531
David Woodhousece326322010-08-11 17:59:35 +0100532 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100533 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100534 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100535 return ret;
536 }
537 return 0;
538}
539
Zhang Rui75a11f12013-09-25 20:39:48 +0800540static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100541{
David Woodhousece326322010-08-11 17:59:35 +0100542 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100543 return;
544
David Woodhousece326322010-08-11 17:59:35 +0100545 rfkill_unregister(priv->rfk[dev]);
546 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100547}
548
Ike Panhc98ee6912010-12-13 18:00:15 +0800549/*
550 * Platform device
551 */
Zhang Ruib5c37b72013-09-25 20:39:50 +0800552static int ideapad_sysfs_init(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800553{
Zhang Ruib5c37b72013-09-25 20:39:50 +0800554 return sysfs_create_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800555 &ideapad_attribute_group);
Ike Panhc98ee6912010-12-13 18:00:15 +0800556}
557
Zhang Ruib5c37b72013-09-25 20:39:50 +0800558static void ideapad_sysfs_exit(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800559{
Ike Panhc8693ae82010-12-13 18:01:01 +0800560 sysfs_remove_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800561 &ideapad_attribute_group);
Ike Panhc98ee6912010-12-13 18:00:15 +0800562}
Ike Panhc98ee6912010-12-13 18:00:15 +0800563
Ike Panhcf63409a2010-12-13 18:00:38 +0800564/*
565 * input device
566 */
567static const struct key_entry ideapad_keymap[] = {
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800568 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800569 { KE_KEY, 7, { KEY_CAMERA } },
570 { KE_KEY, 11, { KEY_F16 } },
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800571 { KE_KEY, 13, { KEY_WLAN } },
572 { KE_KEY, 16, { KEY_PROG1 } },
573 { KE_KEY, 17, { KEY_PROG2 } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800574 { KE_KEY, 64, { KEY_PROG3 } },
575 { KE_KEY, 65, { KEY_PROG4 } },
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800576 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
577 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
Arnd Bergmann74caab92015-11-06 22:28:49 +0100578 { KE_KEY, 128, { KEY_ESC } },
579
Ike Panhcf63409a2010-12-13 18:00:38 +0800580 { KE_END, 0 },
581};
582
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800583static int ideapad_input_init(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800584{
585 struct input_dev *inputdev;
586 int error;
587
588 inputdev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -0700589 if (!inputdev)
Ike Panhcf63409a2010-12-13 18:00:38 +0800590 return -ENOMEM;
Ike Panhcf63409a2010-12-13 18:00:38 +0800591
592 inputdev->name = "Ideapad extra buttons";
593 inputdev->phys = "ideapad/input0";
594 inputdev->id.bustype = BUS_HOST;
Ike Panhc8693ae82010-12-13 18:01:01 +0800595 inputdev->dev.parent = &priv->platform_device->dev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800596
597 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
598 if (error) {
599 pr_err("Unable to setup input device keymap\n");
600 goto err_free_dev;
601 }
602
603 error = input_register_device(inputdev);
604 if (error) {
605 pr_err("Unable to register input device\n");
606 goto err_free_keymap;
607 }
608
Ike Panhc8693ae82010-12-13 18:01:01 +0800609 priv->inputdev = inputdev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800610 return 0;
611
612err_free_keymap:
613 sparse_keymap_free(inputdev);
614err_free_dev:
615 input_free_device(inputdev);
616 return error;
617}
618
Axel Lin7451a552011-07-27 15:27:34 +0800619static void ideapad_input_exit(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800620{
Ike Panhc8693ae82010-12-13 18:01:01 +0800621 sparse_keymap_free(priv->inputdev);
622 input_unregister_device(priv->inputdev);
623 priv->inputdev = NULL;
Ike Panhcf63409a2010-12-13 18:00:38 +0800624}
625
Ike Panhc8693ae82010-12-13 18:01:01 +0800626static void ideapad_input_report(struct ideapad_private *priv,
627 unsigned long scancode)
Ike Panhcf63409a2010-12-13 18:00:38 +0800628{
Ike Panhc8693ae82010-12-13 18:01:01 +0800629 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
Ike Panhcf63409a2010-12-13 18:00:38 +0800630}
Ike Panhcf63409a2010-12-13 18:00:38 +0800631
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800632static void ideapad_input_novokey(struct ideapad_private *priv)
633{
634 unsigned long long_pressed;
635
Zhang Rui331e0ea2013-09-25 20:39:49 +0800636 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800637 return;
638 if (long_pressed)
639 ideapad_input_report(priv, 17);
640 else
641 ideapad_input_report(priv, 16);
642}
643
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800644static void ideapad_check_special_buttons(struct ideapad_private *priv)
645{
646 unsigned long bit, value;
647
Zhang Rui331e0ea2013-09-25 20:39:49 +0800648 read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800649
650 for (bit = 0; bit < 16; bit++) {
651 if (test_bit(bit, &value)) {
652 switch (bit) {
Maxim Mikityanskiya1ec56e2013-03-20 12:34:17 +0200653 case 0: /* Z580 */
654 case 6: /* Z570 */
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800655 /* Thermal Management button */
656 ideapad_input_report(priv, 65);
657 break;
658 case 1:
659 /* OneKey Theater button */
660 ideapad_input_report(priv, 64);
661 break;
Maxim Mikityanskiya1ec56e2013-03-20 12:34:17 +0200662 default:
663 pr_info("Unknown special button: %lu\n", bit);
664 break;
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800665 }
666 }
667 }
668}
669
Ike Panhca4b5a272010-12-13 18:00:48 +0800670/*
Ike Panhca4ecbb82011-06-30 19:50:52 +0800671 * backlight
672 */
673static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
674{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800675 struct ideapad_private *priv = bl_get_data(blightdev);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800676 unsigned long now;
677
Zhang Rui331e0ea2013-09-25 20:39:49 +0800678 if (!priv)
679 return -EINVAL;
680
681 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800682 return -EIO;
683 return now;
684}
685
686static int ideapad_backlight_update_status(struct backlight_device *blightdev)
687{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800688 struct ideapad_private *priv = bl_get_data(blightdev);
689
690 if (!priv)
691 return -EINVAL;
692
693 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
Ike Panhc2be1dc22011-09-06 02:31:53 +0800694 blightdev->props.brightness))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800695 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800696 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
Ike Panhca4ecbb82011-06-30 19:50:52 +0800697 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
698 return -EIO;
699
700 return 0;
701}
702
703static const struct backlight_ops ideapad_backlight_ops = {
704 .get_brightness = ideapad_backlight_get_brightness,
705 .update_status = ideapad_backlight_update_status,
706};
707
708static int ideapad_backlight_init(struct ideapad_private *priv)
709{
710 struct backlight_device *blightdev;
711 struct backlight_properties props;
712 unsigned long max, now, power;
713
Zhang Rui331e0ea2013-09-25 20:39:49 +0800714 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800715 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800716 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800717 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800718 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800719 return -EIO;
720
721 memset(&props, 0, sizeof(struct backlight_properties));
722 props.max_brightness = max;
723 props.type = BACKLIGHT_PLATFORM;
724 blightdev = backlight_device_register("ideapad",
725 &priv->platform_device->dev,
726 priv,
727 &ideapad_backlight_ops,
728 &props);
729 if (IS_ERR(blightdev)) {
730 pr_err("Could not register backlight device\n");
731 return PTR_ERR(blightdev);
732 }
733
734 priv->blightdev = blightdev;
735 blightdev->props.brightness = now;
736 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
737 backlight_update_status(blightdev);
738
739 return 0;
740}
741
742static void ideapad_backlight_exit(struct ideapad_private *priv)
743{
Markus Elfring00981812014-11-24 20:30:29 +0100744 backlight_device_unregister(priv->blightdev);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800745 priv->blightdev = NULL;
746}
747
748static void ideapad_backlight_notify_power(struct ideapad_private *priv)
749{
750 unsigned long power;
751 struct backlight_device *blightdev = priv->blightdev;
752
Rene Bollfordd4afc772011-10-23 09:56:42 +0200753 if (!blightdev)
754 return;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800755 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800756 return;
757 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
758}
759
760static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
761{
762 unsigned long now;
763
764 /* if we control brightness via acpi video driver */
765 if (priv->blightdev == NULL) {
Zhang Rui331e0ea2013-09-25 20:39:49 +0800766 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800767 return;
768 }
769
770 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
771}
772
773/*
Ike Panhca4b5a272010-12-13 18:00:48 +0800774 * module init/exit
775 */
Zhang Rui75a11f12013-09-25 20:39:48 +0800776static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800777{
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800778 unsigned long value;
779
780 /* Without reading from EC touchpad LED doesn't switch state */
Zhang Rui75a11f12013-09-25 20:39:48 +0800781 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800782 /* Some IdeaPads don't really turn off touchpad - they only
783 * switch the LED state. We (de)activate KBC AUX port to turn
784 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
785 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
786 unsigned char param;
787 i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
788 I8042_CMD_AUX_DISABLE);
789 ideapad_input_report(priv, value ? 67 : 66);
790 }
791}
792
Zhang Ruib5c37b72013-09-25 20:39:50 +0800793static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100794{
Zhang Ruib5c37b72013-09-25 20:39:50 +0800795 struct ideapad_private *priv = data;
Ike Panhc8e7d3542010-10-01 15:39:05 +0800796 unsigned long vpc1, vpc2, vpc_bit;
797
Ike Panhc2be1dc22011-09-06 02:31:53 +0800798 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800799 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800800 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800801 return;
802
803 vpc1 = (vpc2 << 8) | vpc1;
804 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
805 if (test_bit(vpc_bit, &vpc1)) {
Ike Panhca4ecbb82011-06-30 19:50:52 +0800806 switch (vpc_bit) {
807 case 9:
Ike Panhc923de842011-09-06 02:32:01 +0800808 ideapad_sync_rfk_state(priv);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800809 break;
Ike Panhc20a769c2012-05-03 17:38:46 +0800810 case 13:
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800811 case 11:
812 case 7:
Ike Panhc20a769c2012-05-03 17:38:46 +0800813 case 6:
814 ideapad_input_report(priv, vpc_bit);
815 break;
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800816 case 5:
Zhang Rui75a11f12013-09-25 20:39:48 +0800817 ideapad_sync_touchpad_state(priv);
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800818 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800819 case 4:
820 ideapad_backlight_notify_brightness(priv);
821 break;
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800822 case 3:
823 ideapad_input_novokey(priv);
824 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800825 case 2:
826 ideapad_backlight_notify_power(priv);
827 break;
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800828 case 0:
829 ideapad_check_special_buttons(priv);
830 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800831 default:
Ike Panhc20a769c2012-05-03 17:38:46 +0800832 pr_info("Unknown event: %lu\n", vpc_bit);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800833 }
Ike Panhc8e7d3542010-10-01 15:39:05 +0800834 }
835 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100836}
837
Arnd Bergmann74caab92015-11-06 22:28:49 +0100838#if IS_ENABLED(CONFIG_ACPI_WMI)
839static void ideapad_wmi_notify(u32 value, void *context)
840{
841 switch (value) {
842 case 128:
843 ideapad_input_report(context, value);
844 break;
845 default:
846 pr_info("Unknown WMI event %u\n", value);
847 }
848}
849#endif
850
Hans de Goedece363c22014-06-23 16:45:51 +0200851/*
852 * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
853 * always results in 0 on these models, causing ideapad_laptop to wrongly
854 * report all radios as hardware-blocked.
855 */
Mathias Krauseb3d94d72014-08-28 13:02:49 +0200856static const struct dmi_system_id no_hw_rfkill_list[] = {
Hans de Goede85093f72014-05-13 16:00:28 +0200857 {
Philippe Coval9b071a42015-05-02 15:14:08 +0200858 .ident = "Lenovo G40-30",
859 .matches = {
860 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
861 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G40-30"),
862 },
863 },
864 {
Dmitry Tunin4fa9dab2015-01-18 15:44:40 +0300865 .ident = "Lenovo G50-30",
866 .matches = {
867 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
868 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G50-30"),
869 },
870 },
871 {
John Dahlstrom4db96752016-02-27 00:09:58 -0600872 .ident = "Lenovo ideapad Y700-15ISK",
873 .matches = {
874 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
875 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-15ISK"),
876 },
877 },
878 {
879 .ident = "Lenovo ideapad Y700 Touch-15ISK",
880 .matches = {
881 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
882 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700 Touch-15ISK"),
883 },
884 },
885 {
Josh Boyeredde3162015-12-09 21:12:52 -0500886 .ident = "Lenovo ideapad Y700-17ISK",
887 .matches = {
888 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
889 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad Y700-17ISK"),
890 },
891 },
892 {
Hans de Goedece363c22014-06-23 16:45:51 +0200893 .ident = "Lenovo Yoga 2 11 / 13 / Pro",
Hans de Goede85093f72014-05-13 16:00:28 +0200894 .matches = {
895 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
Hans de Goedece363c22014-06-23 16:45:51 +0200896 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
Hans de Goede85093f72014-05-13 16:00:28 +0200897 },
898 },
Stephan Mueller725c7f62014-10-27 04:09:50 +0100899 {
Sebastian Krzyszkowiak6d212b82015-07-19 01:10:21 +0200900 .ident = "Lenovo Yoga 2 11 / 13 / Pro",
901 .matches = {
902 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
903 DMI_MATCH(DMI_BOARD_NAME, "Yoga2"),
904 },
905 },
906 {
Arnd Bergmannc789fff2015-11-06 23:26:59 +0100907 .ident = "Lenovo Yoga 3 1170 / 1470",
908 .matches = {
909 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
910 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 3"),
911 },
912 },
913 {
Stephan Mueller725c7f62014-10-27 04:09:50 +0100914 .ident = "Lenovo Yoga 3 Pro 1370",
915 .matches = {
916 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
Arnd Bergmannc789fff2015-11-06 23:26:59 +0100917 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
Stephan Mueller725c7f62014-10-27 04:09:50 +0100918 },
919 },
Hans de Goedef71c8822015-11-09 17:09:05 +0100920 {
Josh Boyer6b31de32016-01-24 10:46:42 -0500921 .ident = "Lenovo Yoga 700",
922 .matches = {
923 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
924 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 700"),
925 },
926 },
927 {
Hans de Goedef71c8822015-11-09 17:09:05 +0100928 .ident = "Lenovo Yoga 900",
929 .matches = {
930 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
931 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 900"),
932 },
933 },
Hans de Goede85093f72014-05-13 16:00:28 +0200934 {}
935};
936
Zhang Ruib5c37b72013-09-25 20:39:50 +0800937static int ideapad_acpi_add(struct platform_device *pdev)
938{
939 int ret, i;
940 int cfg;
941 struct ideapad_private *priv;
942 struct acpi_device *adev;
943
944 ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
945 if (ret)
946 return -ENODEV;
947
948 if (read_method_int(adev->handle, "_CFG", &cfg))
949 return -ENODEV;
950
Himangi Saraogib3facd72014-06-09 17:46:50 -0400951 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800952 if (!priv)
953 return -ENOMEM;
954
955 dev_set_drvdata(&pdev->dev, priv);
956 priv->cfg = cfg;
957 priv->adev = adev;
958 priv->platform_device = pdev;
Hans de Goedece363c22014-06-23 16:45:51 +0200959 priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800960
961 ret = ideapad_sysfs_init(priv);
962 if (ret)
Himangi Saraogib3facd72014-06-09 17:46:50 -0400963 return ret;
Zhang Ruib5c37b72013-09-25 20:39:50 +0800964
965 ret = ideapad_debugfs_init(priv);
966 if (ret)
967 goto debugfs_failed;
968
969 ret = ideapad_input_init(priv);
970 if (ret)
971 goto input_failed;
972
Hans de Goedece363c22014-06-23 16:45:51 +0200973 /*
974 * On some models without a hw-switch (the yoga 2 13 at least)
975 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
976 */
977 if (!priv->has_hw_rfkill_switch)
978 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
979
980 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
981 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
982 ideapad_register_rfkill(priv, i);
983
Zhang Ruib5c37b72013-09-25 20:39:50 +0800984 ideapad_sync_rfk_state(priv);
985 ideapad_sync_touchpad_state(priv);
986
Hans de Goede26bff5f2015-06-16 16:28:04 +0200987 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Zhang Ruib5c37b72013-09-25 20:39:50 +0800988 ret = ideapad_backlight_init(priv);
989 if (ret && ret != -ENODEV)
990 goto backlight_failed;
991 }
992 ret = acpi_install_notify_handler(adev->handle,
993 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
994 if (ret)
995 goto notification_failed;
Arnd Bergmann2d98e0b2016-05-09 23:49:21 +0200996
Arnd Bergmann74caab92015-11-06 22:28:49 +0100997#if IS_ENABLED(CONFIG_ACPI_WMI)
Arnd Bergmann2d98e0b2016-05-09 23:49:21 +0200998 for (i = 0; i < ARRAY_SIZE(ideapad_wmi_fnesc_events); i++) {
999 ret = wmi_install_notify_handler(ideapad_wmi_fnesc_events[i],
1000 ideapad_wmi_notify, priv);
1001 if (ret == AE_OK) {
1002 priv->fnesc_guid = ideapad_wmi_fnesc_events[i];
1003 break;
1004 }
1005 }
Arnd Bergmann74caab92015-11-06 22:28:49 +01001006 if (ret != AE_OK && ret != AE_NOT_EXIST)
1007 goto notification_failed_wmi;
1008#endif
Zhang Ruib5c37b72013-09-25 20:39:50 +08001009
1010 return 0;
Arnd Bergmann74caab92015-11-06 22:28:49 +01001011#if IS_ENABLED(CONFIG_ACPI_WMI)
1012notification_failed_wmi:
1013 acpi_remove_notify_handler(priv->adev->handle,
1014 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1015#endif
Zhang Ruib5c37b72013-09-25 20:39:50 +08001016notification_failed:
1017 ideapad_backlight_exit(priv);
1018backlight_failed:
1019 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1020 ideapad_unregister_rfkill(priv, i);
1021 ideapad_input_exit(priv);
1022input_failed:
1023 ideapad_debugfs_exit(priv);
1024debugfs_failed:
1025 ideapad_sysfs_exit(priv);
Zhang Ruib5c37b72013-09-25 20:39:50 +08001026 return ret;
1027}
1028
1029static int ideapad_acpi_remove(struct platform_device *pdev)
1030{
1031 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
1032 int i;
1033
Arnd Bergmann74caab92015-11-06 22:28:49 +01001034#if IS_ENABLED(CONFIG_ACPI_WMI)
Arnd Bergmann2d98e0b2016-05-09 23:49:21 +02001035 if (priv->fnesc_guid)
1036 wmi_remove_notify_handler(priv->fnesc_guid);
Arnd Bergmann74caab92015-11-06 22:28:49 +01001037#endif
Zhang Ruib5c37b72013-09-25 20:39:50 +08001038 acpi_remove_notify_handler(priv->adev->handle,
1039 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
1040 ideapad_backlight_exit(priv);
1041 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1042 ideapad_unregister_rfkill(priv, i);
1043 ideapad_input_exit(priv);
1044 ideapad_debugfs_exit(priv);
1045 ideapad_sysfs_exit(priv);
1046 dev_set_drvdata(&pdev->dev, NULL);
Zhang Ruib5c37b72013-09-25 20:39:50 +08001047
1048 return 0;
1049}
1050
Zhang Rui11fa8da2013-09-25 20:39:46 +08001051#ifdef CONFIG_PM_SLEEP
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +08001052static int ideapad_acpi_resume(struct device *device)
1053{
Zhang Rui75a11f12013-09-25 20:39:48 +08001054 struct ideapad_private *priv;
1055
1056 if (!device)
1057 return -EINVAL;
1058 priv = dev_get_drvdata(device);
1059
1060 ideapad_sync_rfk_state(priv);
1061 ideapad_sync_touchpad_state(priv);
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +08001062 return 0;
1063}
Zhang Ruib5c37b72013-09-25 20:39:50 +08001064#endif
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +08001065static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1066
Zhang Ruib5c37b72013-09-25 20:39:50 +08001067static const struct acpi_device_id ideapad_device_ids[] = {
1068 { "VPC2004", 0},
1069 { "", 0},
David Woodhouse58ac7aa2010-08-10 23:44:05 +01001070};
Zhang Ruib5c37b72013-09-25 20:39:50 +08001071MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1072
1073static struct platform_driver ideapad_acpi_driver = {
1074 .probe = ideapad_acpi_add,
1075 .remove = ideapad_acpi_remove,
1076 .driver = {
1077 .name = "ideapad_acpi",
Zhang Ruib5c37b72013-09-25 20:39:50 +08001078 .pm = &ideapad_pm,
1079 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1080 },
1081};
1082
1083module_platform_driver(ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +01001084
1085MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1086MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1087MODULE_LICENSE("GPL");