blob: a313dfc0245f550ac5322fec17d890662d52e790 [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)
51static const char ideapad_wmi_fnesc_event[] = "26CAB2E5-5CF1-46AE-AAC3-4A12B6BA50E6";
52#endif
53
Ike Panhc2be1dc22011-09-06 02:31:53 +080054enum {
55 VPCCMD_R_VPC1 = 0x10,
56 VPCCMD_R_BL_MAX,
57 VPCCMD_R_BL,
58 VPCCMD_W_BL,
59 VPCCMD_R_WIFI,
60 VPCCMD_W_WIFI,
61 VPCCMD_R_BT,
62 VPCCMD_W_BT,
63 VPCCMD_R_BL_POWER,
64 VPCCMD_R_NOVO,
65 VPCCMD_R_VPC2,
66 VPCCMD_R_TOUCHPAD,
67 VPCCMD_W_TOUCHPAD,
68 VPCCMD_R_CAMERA,
69 VPCCMD_W_CAMERA,
70 VPCCMD_R_3G,
71 VPCCMD_W_3G,
72 VPCCMD_R_ODD, /* 0x21 */
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +080073 VPCCMD_W_FAN,
74 VPCCMD_R_RF,
Ike Panhc2be1dc22011-09-06 02:31:53 +080075 VPCCMD_W_RF,
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +080076 VPCCMD_R_FAN = 0x2B,
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +080077 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
Ike Panhc2be1dc22011-09-06 02:31:53 +080078 VPCCMD_W_BL_POWER = 0x33,
79};
80
Zhang Rui331e0ea2013-09-25 20:39:49 +080081struct ideapad_rfk_priv {
82 int dev;
83 struct ideapad_private *priv;
84};
85
David Woodhousece326322010-08-11 17:59:35 +010086struct ideapad_private {
Zhang Rui469f6432013-09-25 20:39:47 +080087 struct acpi_device *adev;
Ike Panhcc1f73652010-12-13 18:01:12 +080088 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
Zhang Rui331e0ea2013-09-25 20:39:49 +080089 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
Ike Panhc98ee6912010-12-13 18:00:15 +080090 struct platform_device *platform_device;
Ike Panhcf63409a2010-12-13 18:00:38 +080091 struct input_dev *inputdev;
Ike Panhca4ecbb82011-06-30 19:50:52 +080092 struct backlight_device *blightdev;
Ike Panhc773e3202011-09-06 02:32:52 +080093 struct dentry *debug;
Ike Panhc3371f482011-06-30 19:50:40 +080094 unsigned long cfg;
Hans de Goedece363c22014-06-23 16:45:51 +020095 bool has_hw_rfkill_switch;
David Woodhouse58ac7aa2010-08-10 23:44:05 +010096};
97
Ike Panhcbfa97b72010-10-01 15:40:22 +080098static bool no_bt_rfkill;
99module_param(no_bt_rfkill, bool, 0444);
100MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
101
Ike Panhc6a09f212010-10-01 15:38:46 +0800102/*
103 * ACPI Helpers
104 */
105#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
106
107static int read_method_int(acpi_handle handle, const char *method, int *val)
108{
109 acpi_status status;
110 unsigned long long result;
111
112 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
113 if (ACPI_FAILURE(status)) {
114 *val = -1;
115 return -1;
116 } else {
117 *val = result;
118 return 0;
119 }
120}
121
122static int method_vpcr(acpi_handle handle, int cmd, int *ret)
123{
124 acpi_status status;
125 unsigned long long result;
126 struct acpi_object_list params;
127 union acpi_object in_obj;
128
129 params.count = 1;
130 params.pointer = &in_obj;
131 in_obj.type = ACPI_TYPE_INTEGER;
132 in_obj.integer.value = cmd;
133
134 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
135
136 if (ACPI_FAILURE(status)) {
137 *ret = -1;
138 return -1;
139 } else {
140 *ret = result;
141 return 0;
142 }
143}
144
145static int method_vpcw(acpi_handle handle, int cmd, int data)
146{
147 struct acpi_object_list params;
148 union acpi_object in_obj[2];
149 acpi_status status;
150
151 params.count = 2;
152 params.pointer = in_obj;
153 in_obj[0].type = ACPI_TYPE_INTEGER;
154 in_obj[0].integer.value = cmd;
155 in_obj[1].type = ACPI_TYPE_INTEGER;
156 in_obj[1].integer.value = data;
157
158 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
159 if (status != AE_OK)
160 return -1;
161 return 0;
162}
163
164static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
165{
166 int val;
167 unsigned long int end_jiffies;
168
169 if (method_vpcw(handle, 1, cmd))
170 return -1;
171
172 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
173 time_before(jiffies, end_jiffies);) {
174 schedule();
175 if (method_vpcr(handle, 1, &val))
176 return -1;
177 if (val == 0) {
178 if (method_vpcr(handle, 0, &val))
179 return -1;
180 *data = val;
181 return 0;
182 }
183 }
184 pr_err("timeout in read_ec_cmd\n");
185 return -1;
186}
187
188static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
189{
190 int val;
191 unsigned long int end_jiffies;
192
193 if (method_vpcw(handle, 0, data))
194 return -1;
195 if (method_vpcw(handle, 1, cmd))
196 return -1;
197
198 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
199 time_before(jiffies, end_jiffies);) {
200 schedule();
201 if (method_vpcr(handle, 1, &val))
202 return -1;
203 if (val == 0)
204 return 0;
205 }
206 pr_err("timeout in write_ec_cmd\n");
207 return -1;
208}
Ike Panhc6a09f212010-10-01 15:38:46 +0800209
Ike Panhca4b5a272010-12-13 18:00:48 +0800210/*
Ike Panhc773e3202011-09-06 02:32:52 +0800211 * debugfs
212 */
Ike Panhc773e3202011-09-06 02:32:52 +0800213static int debugfs_status_show(struct seq_file *s, void *data)
214{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800215 struct ideapad_private *priv = s->private;
Ike Panhc773e3202011-09-06 02:32:52 +0800216 unsigned long value;
217
Zhang Rui331e0ea2013-09-25 20:39:49 +0800218 if (!priv)
219 return -EINVAL;
220
221 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800222 seq_printf(s, "Backlight max:\t%lu\n", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800223 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800224 seq_printf(s, "Backlight now:\t%lu\n", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800225 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800226 seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
227 seq_printf(s, "=====================\n");
228
Zhang Rui331e0ea2013-09-25 20:39:49 +0800229 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800230 seq_printf(s, "Radio status:\t%s(%lu)\n",
231 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800232 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800233 seq_printf(s, "Wifi status:\t%s(%lu)\n",
234 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800235 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800236 seq_printf(s, "BT status:\t%s(%lu)\n",
237 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800238 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800239 seq_printf(s, "3G status:\t%s(%lu)\n",
240 value ? "On" : "Off", value);
241 seq_printf(s, "=====================\n");
242
Zhang Rui331e0ea2013-09-25 20:39:49 +0800243 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800244 seq_printf(s, "Touchpad status:%s(%lu)\n",
245 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800246 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800247 seq_printf(s, "Camera status:\t%s(%lu)\n",
248 value ? "On" : "Off", value);
249
250 return 0;
251}
252
253static int debugfs_status_open(struct inode *inode, struct file *file)
254{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800255 return single_open(file, debugfs_status_show, inode->i_private);
Ike Panhc773e3202011-09-06 02:32:52 +0800256}
257
258static const struct file_operations debugfs_status_fops = {
259 .owner = THIS_MODULE,
260 .open = debugfs_status_open,
261 .read = seq_read,
262 .llseek = seq_lseek,
263 .release = single_release,
264};
265
266static int debugfs_cfg_show(struct seq_file *s, void *data)
267{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800268 struct ideapad_private *priv = s->private;
269
270 if (!priv) {
Ike Panhc773e3202011-09-06 02:32:52 +0800271 seq_printf(s, "cfg: N/A\n");
272 } else {
273 seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
Zhang Rui331e0ea2013-09-25 20:39:49 +0800274 priv->cfg);
275 if (test_bit(CFG_BT_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800276 seq_printf(s, "Bluetooth ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800277 if (test_bit(CFG_3G_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800278 seq_printf(s, "3G ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800279 if (test_bit(CFG_WIFI_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800280 seq_printf(s, "Wireless ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800281 if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800282 seq_printf(s, "Camera ");
283 seq_printf(s, "\nGraphic: ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800284 switch ((priv->cfg)&0x700) {
Ike Panhc773e3202011-09-06 02:32:52 +0800285 case 0x100:
286 seq_printf(s, "Intel");
287 break;
288 case 0x200:
289 seq_printf(s, "ATI");
290 break;
291 case 0x300:
292 seq_printf(s, "Nvidia");
293 break;
294 case 0x400:
295 seq_printf(s, "Intel and ATI");
296 break;
297 case 0x500:
298 seq_printf(s, "Intel and Nvidia");
299 break;
300 }
301 seq_printf(s, "\n");
302 }
303 return 0;
304}
305
306static int debugfs_cfg_open(struct inode *inode, struct file *file)
307{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800308 return single_open(file, debugfs_cfg_show, inode->i_private);
Ike Panhc773e3202011-09-06 02:32:52 +0800309}
310
311static const struct file_operations debugfs_cfg_fops = {
312 .owner = THIS_MODULE,
313 .open = debugfs_cfg_open,
314 .read = seq_read,
315 .llseek = seq_lseek,
316 .release = single_release,
317};
318
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800319static int ideapad_debugfs_init(struct ideapad_private *priv)
Ike Panhc773e3202011-09-06 02:32:52 +0800320{
321 struct dentry *node;
322
323 priv->debug = debugfs_create_dir("ideapad", NULL);
324 if (priv->debug == NULL) {
325 pr_err("failed to create debugfs directory");
326 goto errout;
327 }
328
Zhang Rui331e0ea2013-09-25 20:39:49 +0800329 node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
Ike Panhc773e3202011-09-06 02:32:52 +0800330 &debugfs_cfg_fops);
331 if (!node) {
332 pr_err("failed to create cfg in debugfs");
333 goto errout;
334 }
335
Zhang Rui331e0ea2013-09-25 20:39:49 +0800336 node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
Ike Panhc773e3202011-09-06 02:32:52 +0800337 &debugfs_status_fops);
338 if (!node) {
Ike Panhca5c38922012-05-03 17:38:35 +0800339 pr_err("failed to create status in debugfs");
Ike Panhc773e3202011-09-06 02:32:52 +0800340 goto errout;
341 }
342
343 return 0;
344
345errout:
346 return -ENOMEM;
347}
348
349static void ideapad_debugfs_exit(struct ideapad_private *priv)
350{
351 debugfs_remove_recursive(priv->debug);
352 priv->debug = NULL;
353}
354
355/*
Ike Panhc3371f482011-06-30 19:50:40 +0800356 * sysfs
Ike Panhca4b5a272010-12-13 18:00:48 +0800357 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100358static ssize_t show_ideapad_cam(struct device *dev,
359 struct device_attribute *attr,
360 char *buf)
361{
Ike Panhc26c81d52010-10-01 15:39:40 +0800362 unsigned long result;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800363 struct ideapad_private *priv = dev_get_drvdata(dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100364
Zhang Rui331e0ea2013-09-25 20:39:49 +0800365 if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
Ike Panhc26c81d52010-10-01 15:39:40 +0800366 return sprintf(buf, "-1\n");
367 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100368}
369
370static ssize_t store_ideapad_cam(struct device *dev,
371 struct device_attribute *attr,
372 const char *buf, size_t count)
373{
374 int ret, state;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800375 struct ideapad_private *priv = dev_get_drvdata(dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100376
377 if (!count)
378 return 0;
379 if (sscanf(buf, "%i", &state) != 1)
380 return -EINVAL;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800381 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100382 if (ret < 0)
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800383 return -EIO;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100384 return count;
385}
386
387static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
388
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800389static ssize_t show_ideapad_fan(struct device *dev,
390 struct device_attribute *attr,
391 char *buf)
392{
393 unsigned long result;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800394 struct ideapad_private *priv = dev_get_drvdata(dev);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800395
Zhang Rui331e0ea2013-09-25 20:39:49 +0800396 if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800397 return sprintf(buf, "-1\n");
398 return sprintf(buf, "%lu\n", result);
399}
400
401static ssize_t store_ideapad_fan(struct device *dev,
402 struct device_attribute *attr,
403 const char *buf, size_t count)
404{
405 int ret, state;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800406 struct ideapad_private *priv = dev_get_drvdata(dev);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800407
408 if (!count)
409 return 0;
410 if (sscanf(buf, "%i", &state) != 1)
411 return -EINVAL;
412 if (state < 0 || state > 4 || state == 3)
413 return -EINVAL;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800414 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800415 if (ret < 0)
416 return -EIO;
417 return count;
418}
419
420static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
421
Ike Panhc3371f482011-06-30 19:50:40 +0800422static struct attribute *ideapad_attributes[] = {
423 &dev_attr_camera_power.attr,
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800424 &dev_attr_fan_mode.attr,
Ike Panhc3371f482011-06-30 19:50:40 +0800425 NULL
426};
427
Al Viro587a1f12011-07-23 23:11:19 -0400428static umode_t ideapad_is_visible(struct kobject *kobj,
Ike Panhca84511f2011-06-30 19:50:47 +0800429 struct attribute *attr,
430 int idx)
431{
432 struct device *dev = container_of(kobj, struct device, kobj);
433 struct ideapad_private *priv = dev_get_drvdata(dev);
434 bool supported;
435
436 if (attr == &dev_attr_camera_power.attr)
437 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800438 else if (attr == &dev_attr_fan_mode.attr) {
439 unsigned long value;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800440 supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
441 &value);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800442 } else
Ike Panhca84511f2011-06-30 19:50:47 +0800443 supported = true;
444
445 return supported ? attr->mode : 0;
446}
447
Mathias Krause49458e82014-07-16 19:43:15 +0200448static const struct attribute_group ideapad_attribute_group = {
Ike Panhca84511f2011-06-30 19:50:47 +0800449 .is_visible = ideapad_is_visible,
Ike Panhc3371f482011-06-30 19:50:40 +0800450 .attrs = ideapad_attributes
451};
452
Ike Panhca4b5a272010-12-13 18:00:48 +0800453/*
454 * Rfkill
455 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800456struct ideapad_rfk_data {
457 char *name;
458 int cfgbit;
459 int opcode;
460 int type;
461};
462
Mathias Krauseb3d94d72014-08-28 13:02:49 +0200463static const struct ideapad_rfk_data ideapad_rfk_data[] = {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800464 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
465 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
466 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
Ike Panhcc1f73652010-12-13 18:01:12 +0800467};
468
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100469static int ideapad_rfk_set(void *data, bool blocked)
470{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800471 struct ideapad_rfk_priv *priv = data;
Arnd Bergmann4b200b42015-06-13 15:23:33 +0200472 int opcode = ideapad_rfk_data[priv->dev].opcode;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100473
Arnd Bergmann4b200b42015-06-13 15:23:33 +0200474 return write_ec_cmd(priv->priv->adev->handle, opcode, !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100475}
476
477static struct rfkill_ops ideapad_rfk_ops = {
478 .set_block = ideapad_rfk_set,
479};
480
Ike Panhc923de842011-09-06 02:32:01 +0800481static void ideapad_sync_rfk_state(struct ideapad_private *priv)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100482{
Hans de Goedece363c22014-06-23 16:45:51 +0200483 unsigned long hw_blocked = 0;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100484 int i;
485
Hans de Goedece363c22014-06-23 16:45:51 +0200486 if (priv->has_hw_rfkill_switch) {
487 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
488 return;
489 hw_blocked = !hw_blocked;
490 }
Ike Panhc2b7266b2010-10-01 15:39:49 +0800491
Ike Panhcc1f73652010-12-13 18:01:12 +0800492 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100493 if (priv->rfk[i])
494 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100495}
496
Zhang Rui75a11f12013-09-25 20:39:48 +0800497static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100498{
499 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800500 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100501
Ike Panhcbfa97b72010-10-01 15:40:22 +0800502 if (no_bt_rfkill &&
503 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
504 /* Force to enable bluetooth when no_bt_rfkill=1 */
Zhang Rui331e0ea2013-09-25 20:39:49 +0800505 write_ec_cmd(priv->adev->handle,
Ike Panhcbfa97b72010-10-01 15:40:22 +0800506 ideapad_rfk_data[dev].opcode, 1);
507 return 0;
508 }
Zhang Rui331e0ea2013-09-25 20:39:49 +0800509 priv->rfk_priv[dev].dev = dev;
510 priv->rfk_priv[dev].priv = priv;
Ike Panhcbfa97b72010-10-01 15:40:22 +0800511
Zhang Rui75a11f12013-09-25 20:39:48 +0800512 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
Zhang Ruib5c37b72013-09-25 20:39:50 +0800513 &priv->platform_device->dev,
Zhang Rui75a11f12013-09-25 20:39:48 +0800514 ideapad_rfk_data[dev].type,
515 &ideapad_rfk_ops,
Zhang Rui331e0ea2013-09-25 20:39:49 +0800516 &priv->rfk_priv[dev]);
David Woodhousece326322010-08-11 17:59:35 +0100517 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100518 return -ENOMEM;
519
Zhang Rui331e0ea2013-09-25 20:39:49 +0800520 if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
Ike Panhc2b7266b2010-10-01 15:39:49 +0800521 &sw_blocked)) {
522 rfkill_init_sw_state(priv->rfk[dev], 0);
523 } else {
524 sw_blocked = !sw_blocked;
525 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
526 }
527
David Woodhousece326322010-08-11 17:59:35 +0100528 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100529 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100530 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100531 return ret;
532 }
533 return 0;
534}
535
Zhang Rui75a11f12013-09-25 20:39:48 +0800536static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100537{
David Woodhousece326322010-08-11 17:59:35 +0100538 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100539 return;
540
David Woodhousece326322010-08-11 17:59:35 +0100541 rfkill_unregister(priv->rfk[dev]);
542 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100543}
544
Ike Panhc98ee6912010-12-13 18:00:15 +0800545/*
546 * Platform device
547 */
Zhang Ruib5c37b72013-09-25 20:39:50 +0800548static int ideapad_sysfs_init(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800549{
Zhang Ruib5c37b72013-09-25 20:39:50 +0800550 return sysfs_create_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800551 &ideapad_attribute_group);
Ike Panhc98ee6912010-12-13 18:00:15 +0800552}
553
Zhang Ruib5c37b72013-09-25 20:39:50 +0800554static void ideapad_sysfs_exit(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800555{
Ike Panhc8693ae82010-12-13 18:01:01 +0800556 sysfs_remove_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800557 &ideapad_attribute_group);
Ike Panhc98ee6912010-12-13 18:00:15 +0800558}
Ike Panhc98ee6912010-12-13 18:00:15 +0800559
Ike Panhcf63409a2010-12-13 18:00:38 +0800560/*
561 * input device
562 */
563static const struct key_entry ideapad_keymap[] = {
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800564 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800565 { KE_KEY, 7, { KEY_CAMERA } },
566 { KE_KEY, 11, { KEY_F16 } },
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800567 { KE_KEY, 13, { KEY_WLAN } },
568 { KE_KEY, 16, { KEY_PROG1 } },
569 { KE_KEY, 17, { KEY_PROG2 } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800570 { KE_KEY, 64, { KEY_PROG3 } },
571 { KE_KEY, 65, { KEY_PROG4 } },
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800572 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
573 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
Arnd Bergmann74caab92015-11-06 22:28:49 +0100574 { KE_KEY, 128, { KEY_ESC } },
575
Ike Panhcf63409a2010-12-13 18:00:38 +0800576 { KE_END, 0 },
577};
578
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800579static int ideapad_input_init(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800580{
581 struct input_dev *inputdev;
582 int error;
583
584 inputdev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -0700585 if (!inputdev)
Ike Panhcf63409a2010-12-13 18:00:38 +0800586 return -ENOMEM;
Ike Panhcf63409a2010-12-13 18:00:38 +0800587
588 inputdev->name = "Ideapad extra buttons";
589 inputdev->phys = "ideapad/input0";
590 inputdev->id.bustype = BUS_HOST;
Ike Panhc8693ae82010-12-13 18:01:01 +0800591 inputdev->dev.parent = &priv->platform_device->dev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800592
593 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
594 if (error) {
595 pr_err("Unable to setup input device keymap\n");
596 goto err_free_dev;
597 }
598
599 error = input_register_device(inputdev);
600 if (error) {
601 pr_err("Unable to register input device\n");
602 goto err_free_keymap;
603 }
604
Ike Panhc8693ae82010-12-13 18:01:01 +0800605 priv->inputdev = inputdev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800606 return 0;
607
608err_free_keymap:
609 sparse_keymap_free(inputdev);
610err_free_dev:
611 input_free_device(inputdev);
612 return error;
613}
614
Axel Lin7451a552011-07-27 15:27:34 +0800615static void ideapad_input_exit(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800616{
Ike Panhc8693ae82010-12-13 18:01:01 +0800617 sparse_keymap_free(priv->inputdev);
618 input_unregister_device(priv->inputdev);
619 priv->inputdev = NULL;
Ike Panhcf63409a2010-12-13 18:00:38 +0800620}
621
Ike Panhc8693ae82010-12-13 18:01:01 +0800622static void ideapad_input_report(struct ideapad_private *priv,
623 unsigned long scancode)
Ike Panhcf63409a2010-12-13 18:00:38 +0800624{
Ike Panhc8693ae82010-12-13 18:01:01 +0800625 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
Ike Panhcf63409a2010-12-13 18:00:38 +0800626}
Ike Panhcf63409a2010-12-13 18:00:38 +0800627
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800628static void ideapad_input_novokey(struct ideapad_private *priv)
629{
630 unsigned long long_pressed;
631
Zhang Rui331e0ea2013-09-25 20:39:49 +0800632 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800633 return;
634 if (long_pressed)
635 ideapad_input_report(priv, 17);
636 else
637 ideapad_input_report(priv, 16);
638}
639
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800640static void ideapad_check_special_buttons(struct ideapad_private *priv)
641{
642 unsigned long bit, value;
643
Zhang Rui331e0ea2013-09-25 20:39:49 +0800644 read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800645
646 for (bit = 0; bit < 16; bit++) {
647 if (test_bit(bit, &value)) {
648 switch (bit) {
Maxim Mikityanskiya1ec56e2013-03-20 12:34:17 +0200649 case 0: /* Z580 */
650 case 6: /* Z570 */
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800651 /* Thermal Management button */
652 ideapad_input_report(priv, 65);
653 break;
654 case 1:
655 /* OneKey Theater button */
656 ideapad_input_report(priv, 64);
657 break;
Maxim Mikityanskiya1ec56e2013-03-20 12:34:17 +0200658 default:
659 pr_info("Unknown special button: %lu\n", bit);
660 break;
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800661 }
662 }
663 }
664}
665
Ike Panhca4b5a272010-12-13 18:00:48 +0800666/*
Ike Panhca4ecbb82011-06-30 19:50:52 +0800667 * backlight
668 */
669static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
670{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800671 struct ideapad_private *priv = bl_get_data(blightdev);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800672 unsigned long now;
673
Zhang Rui331e0ea2013-09-25 20:39:49 +0800674 if (!priv)
675 return -EINVAL;
676
677 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800678 return -EIO;
679 return now;
680}
681
682static int ideapad_backlight_update_status(struct backlight_device *blightdev)
683{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800684 struct ideapad_private *priv = bl_get_data(blightdev);
685
686 if (!priv)
687 return -EINVAL;
688
689 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
Ike Panhc2be1dc22011-09-06 02:31:53 +0800690 blightdev->props.brightness))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800691 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800692 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
Ike Panhca4ecbb82011-06-30 19:50:52 +0800693 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
694 return -EIO;
695
696 return 0;
697}
698
699static const struct backlight_ops ideapad_backlight_ops = {
700 .get_brightness = ideapad_backlight_get_brightness,
701 .update_status = ideapad_backlight_update_status,
702};
703
704static int ideapad_backlight_init(struct ideapad_private *priv)
705{
706 struct backlight_device *blightdev;
707 struct backlight_properties props;
708 unsigned long max, now, power;
709
Zhang Rui331e0ea2013-09-25 20:39:49 +0800710 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800711 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800712 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800713 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800714 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800715 return -EIO;
716
717 memset(&props, 0, sizeof(struct backlight_properties));
718 props.max_brightness = max;
719 props.type = BACKLIGHT_PLATFORM;
720 blightdev = backlight_device_register("ideapad",
721 &priv->platform_device->dev,
722 priv,
723 &ideapad_backlight_ops,
724 &props);
725 if (IS_ERR(blightdev)) {
726 pr_err("Could not register backlight device\n");
727 return PTR_ERR(blightdev);
728 }
729
730 priv->blightdev = blightdev;
731 blightdev->props.brightness = now;
732 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
733 backlight_update_status(blightdev);
734
735 return 0;
736}
737
738static void ideapad_backlight_exit(struct ideapad_private *priv)
739{
Markus Elfring00981812014-11-24 20:30:29 +0100740 backlight_device_unregister(priv->blightdev);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800741 priv->blightdev = NULL;
742}
743
744static void ideapad_backlight_notify_power(struct ideapad_private *priv)
745{
746 unsigned long power;
747 struct backlight_device *blightdev = priv->blightdev;
748
Rene Bollfordd4afc772011-10-23 09:56:42 +0200749 if (!blightdev)
750 return;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800751 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800752 return;
753 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
754}
755
756static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
757{
758 unsigned long now;
759
760 /* if we control brightness via acpi video driver */
761 if (priv->blightdev == NULL) {
Zhang Rui331e0ea2013-09-25 20:39:49 +0800762 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800763 return;
764 }
765
766 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
767}
768
769/*
Ike Panhca4b5a272010-12-13 18:00:48 +0800770 * module init/exit
771 */
Zhang Rui75a11f12013-09-25 20:39:48 +0800772static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800773{
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800774 unsigned long value;
775
776 /* Without reading from EC touchpad LED doesn't switch state */
Zhang Rui75a11f12013-09-25 20:39:48 +0800777 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800778 /* Some IdeaPads don't really turn off touchpad - they only
779 * switch the LED state. We (de)activate KBC AUX port to turn
780 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
781 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
782 unsigned char param;
783 i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
784 I8042_CMD_AUX_DISABLE);
785 ideapad_input_report(priv, value ? 67 : 66);
786 }
787}
788
Zhang Ruib5c37b72013-09-25 20:39:50 +0800789static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100790{
Zhang Ruib5c37b72013-09-25 20:39:50 +0800791 struct ideapad_private *priv = data;
Ike Panhc8e7d3542010-10-01 15:39:05 +0800792 unsigned long vpc1, vpc2, vpc_bit;
793
Ike Panhc2be1dc22011-09-06 02:31:53 +0800794 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800795 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800796 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800797 return;
798
799 vpc1 = (vpc2 << 8) | vpc1;
800 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
801 if (test_bit(vpc_bit, &vpc1)) {
Ike Panhca4ecbb82011-06-30 19:50:52 +0800802 switch (vpc_bit) {
803 case 9:
Ike Panhc923de842011-09-06 02:32:01 +0800804 ideapad_sync_rfk_state(priv);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800805 break;
Ike Panhc20a769c2012-05-03 17:38:46 +0800806 case 13:
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800807 case 11:
808 case 7:
Ike Panhc20a769c2012-05-03 17:38:46 +0800809 case 6:
810 ideapad_input_report(priv, vpc_bit);
811 break;
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800812 case 5:
Zhang Rui75a11f12013-09-25 20:39:48 +0800813 ideapad_sync_touchpad_state(priv);
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800814 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800815 case 4:
816 ideapad_backlight_notify_brightness(priv);
817 break;
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800818 case 3:
819 ideapad_input_novokey(priv);
820 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800821 case 2:
822 ideapad_backlight_notify_power(priv);
823 break;
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800824 case 0:
825 ideapad_check_special_buttons(priv);
826 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800827 default:
Ike Panhc20a769c2012-05-03 17:38:46 +0800828 pr_info("Unknown event: %lu\n", vpc_bit);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800829 }
Ike Panhc8e7d3542010-10-01 15:39:05 +0800830 }
831 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100832}
833
Arnd Bergmann74caab92015-11-06 22:28:49 +0100834#if IS_ENABLED(CONFIG_ACPI_WMI)
835static void ideapad_wmi_notify(u32 value, void *context)
836{
837 switch (value) {
838 case 128:
839 ideapad_input_report(context, value);
840 break;
841 default:
842 pr_info("Unknown WMI event %u\n", value);
843 }
844}
845#endif
846
Hans de Goedece363c22014-06-23 16:45:51 +0200847/*
848 * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
849 * always results in 0 on these models, causing ideapad_laptop to wrongly
850 * report all radios as hardware-blocked.
851 */
Mathias Krauseb3d94d72014-08-28 13:02:49 +0200852static const struct dmi_system_id no_hw_rfkill_list[] = {
Hans de Goede85093f72014-05-13 16:00:28 +0200853 {
Philippe Coval9b071a42015-05-02 15:14:08 +0200854 .ident = "Lenovo G40-30",
855 .matches = {
856 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
857 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G40-30"),
858 },
859 },
860 {
Dmitry Tunin4fa9dab2015-01-18 15:44:40 +0300861 .ident = "Lenovo G50-30",
862 .matches = {
863 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
864 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo G50-30"),
865 },
866 },
867 {
Hans de Goedece363c22014-06-23 16:45:51 +0200868 .ident = "Lenovo Yoga 2 11 / 13 / Pro",
Hans de Goede85093f72014-05-13 16:00:28 +0200869 .matches = {
870 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
Hans de Goedece363c22014-06-23 16:45:51 +0200871 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
Hans de Goede85093f72014-05-13 16:00:28 +0200872 },
873 },
Stephan Mueller725c7f62014-10-27 04:09:50 +0100874 {
Sebastian Krzyszkowiak6d212b82015-07-19 01:10:21 +0200875 .ident = "Lenovo Yoga 2 11 / 13 / Pro",
876 .matches = {
877 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
878 DMI_MATCH(DMI_BOARD_NAME, "Yoga2"),
879 },
880 },
881 {
Arnd Bergmannc789fff2015-11-06 23:26:59 +0100882 .ident = "Lenovo Yoga 3 1170 / 1470",
883 .matches = {
884 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
885 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 3"),
886 },
887 },
888 {
Stephan Mueller725c7f62014-10-27 04:09:50 +0100889 .ident = "Lenovo Yoga 3 Pro 1370",
890 .matches = {
891 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
Arnd Bergmannc789fff2015-11-06 23:26:59 +0100892 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3"),
Stephan Mueller725c7f62014-10-27 04:09:50 +0100893 },
894 },
Hans de Goedef71c8822015-11-09 17:09:05 +0100895 {
896 .ident = "Lenovo Yoga 900",
897 .matches = {
898 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
899 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 900"),
900 },
901 },
Hans de Goede85093f72014-05-13 16:00:28 +0200902 {}
903};
904
Zhang Ruib5c37b72013-09-25 20:39:50 +0800905static int ideapad_acpi_add(struct platform_device *pdev)
906{
907 int ret, i;
908 int cfg;
909 struct ideapad_private *priv;
910 struct acpi_device *adev;
911
912 ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
913 if (ret)
914 return -ENODEV;
915
916 if (read_method_int(adev->handle, "_CFG", &cfg))
917 return -ENODEV;
918
Himangi Saraogib3facd72014-06-09 17:46:50 -0400919 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800920 if (!priv)
921 return -ENOMEM;
922
923 dev_set_drvdata(&pdev->dev, priv);
924 priv->cfg = cfg;
925 priv->adev = adev;
926 priv->platform_device = pdev;
Hans de Goedece363c22014-06-23 16:45:51 +0200927 priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800928
929 ret = ideapad_sysfs_init(priv);
930 if (ret)
Himangi Saraogib3facd72014-06-09 17:46:50 -0400931 return ret;
Zhang Ruib5c37b72013-09-25 20:39:50 +0800932
933 ret = ideapad_debugfs_init(priv);
934 if (ret)
935 goto debugfs_failed;
936
937 ret = ideapad_input_init(priv);
938 if (ret)
939 goto input_failed;
940
Hans de Goedece363c22014-06-23 16:45:51 +0200941 /*
942 * On some models without a hw-switch (the yoga 2 13 at least)
943 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
944 */
945 if (!priv->has_hw_rfkill_switch)
946 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
947
948 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
949 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
950 ideapad_register_rfkill(priv, i);
951
Zhang Ruib5c37b72013-09-25 20:39:50 +0800952 ideapad_sync_rfk_state(priv);
953 ideapad_sync_touchpad_state(priv);
954
Hans de Goede26bff5f2015-06-16 16:28:04 +0200955 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Zhang Ruib5c37b72013-09-25 20:39:50 +0800956 ret = ideapad_backlight_init(priv);
957 if (ret && ret != -ENODEV)
958 goto backlight_failed;
959 }
960 ret = acpi_install_notify_handler(adev->handle,
961 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
962 if (ret)
963 goto notification_failed;
Arnd Bergmann74caab92015-11-06 22:28:49 +0100964#if IS_ENABLED(CONFIG_ACPI_WMI)
965 ret = wmi_install_notify_handler(ideapad_wmi_fnesc_event, ideapad_wmi_notify, priv);
966 if (ret != AE_OK && ret != AE_NOT_EXIST)
967 goto notification_failed_wmi;
968#endif
Zhang Ruib5c37b72013-09-25 20:39:50 +0800969
970 return 0;
Arnd Bergmann74caab92015-11-06 22:28:49 +0100971#if IS_ENABLED(CONFIG_ACPI_WMI)
972notification_failed_wmi:
973 acpi_remove_notify_handler(priv->adev->handle,
974 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
975#endif
Zhang Ruib5c37b72013-09-25 20:39:50 +0800976notification_failed:
977 ideapad_backlight_exit(priv);
978backlight_failed:
979 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
980 ideapad_unregister_rfkill(priv, i);
981 ideapad_input_exit(priv);
982input_failed:
983 ideapad_debugfs_exit(priv);
984debugfs_failed:
985 ideapad_sysfs_exit(priv);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800986 return ret;
987}
988
989static int ideapad_acpi_remove(struct platform_device *pdev)
990{
991 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
992 int i;
993
Arnd Bergmann74caab92015-11-06 22:28:49 +0100994#if IS_ENABLED(CONFIG_ACPI_WMI)
995 wmi_remove_notify_handler(ideapad_wmi_fnesc_event);
996#endif
Zhang Ruib5c37b72013-09-25 20:39:50 +0800997 acpi_remove_notify_handler(priv->adev->handle,
998 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
999 ideapad_backlight_exit(priv);
1000 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
1001 ideapad_unregister_rfkill(priv, i);
1002 ideapad_input_exit(priv);
1003 ideapad_debugfs_exit(priv);
1004 ideapad_sysfs_exit(priv);
1005 dev_set_drvdata(&pdev->dev, NULL);
Zhang Ruib5c37b72013-09-25 20:39:50 +08001006
1007 return 0;
1008}
1009
Zhang Rui11fa8da2013-09-25 20:39:46 +08001010#ifdef CONFIG_PM_SLEEP
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +08001011static int ideapad_acpi_resume(struct device *device)
1012{
Zhang Rui75a11f12013-09-25 20:39:48 +08001013 struct ideapad_private *priv;
1014
1015 if (!device)
1016 return -EINVAL;
1017 priv = dev_get_drvdata(device);
1018
1019 ideapad_sync_rfk_state(priv);
1020 ideapad_sync_touchpad_state(priv);
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +08001021 return 0;
1022}
Zhang Ruib5c37b72013-09-25 20:39:50 +08001023#endif
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +08001024static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
1025
Zhang Ruib5c37b72013-09-25 20:39:50 +08001026static const struct acpi_device_id ideapad_device_ids[] = {
1027 { "VPC2004", 0},
1028 { "", 0},
David Woodhouse58ac7aa2010-08-10 23:44:05 +01001029};
Zhang Ruib5c37b72013-09-25 20:39:50 +08001030MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
1031
1032static struct platform_driver ideapad_acpi_driver = {
1033 .probe = ideapad_acpi_add,
1034 .remove = ideapad_acpi_remove,
1035 .driver = {
1036 .name = "ideapad_acpi",
Zhang Ruib5c37b72013-09-25 20:39:50 +08001037 .pm = &ideapad_pm,
1038 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
1039 },
1040};
1041
1042module_platform_driver(ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +01001043
1044MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1045MODULE_DESCRIPTION("IdeaPad ACPI Extras");
1046MODULE_LICENSE("GPL");