blob: b3d419a8472341dabee36c3cb40765157be443a9 [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>
David Woodhouse58ac7aa2010-08-10 23:44:05 +010041
Ike Panhcc1f73652010-12-13 18:01:12 +080042#define IDEAPAD_RFKILL_DEV_NUM (3)
David Woodhouse58ac7aa2010-08-10 23:44:05 +010043
Ike Panhc3371f482011-06-30 19:50:40 +080044#define CFG_BT_BIT (16)
45#define CFG_3G_BIT (17)
46#define CFG_WIFI_BIT (18)
Ike Panhca84511f2011-06-30 19:50:47 +080047#define CFG_CAMERA_BIT (19)
Ike Panhc3371f482011-06-30 19:50:40 +080048
Ike Panhc2be1dc22011-09-06 02:31:53 +080049enum {
50 VPCCMD_R_VPC1 = 0x10,
51 VPCCMD_R_BL_MAX,
52 VPCCMD_R_BL,
53 VPCCMD_W_BL,
54 VPCCMD_R_WIFI,
55 VPCCMD_W_WIFI,
56 VPCCMD_R_BT,
57 VPCCMD_W_BT,
58 VPCCMD_R_BL_POWER,
59 VPCCMD_R_NOVO,
60 VPCCMD_R_VPC2,
61 VPCCMD_R_TOUCHPAD,
62 VPCCMD_W_TOUCHPAD,
63 VPCCMD_R_CAMERA,
64 VPCCMD_W_CAMERA,
65 VPCCMD_R_3G,
66 VPCCMD_W_3G,
67 VPCCMD_R_ODD, /* 0x21 */
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +080068 VPCCMD_W_FAN,
69 VPCCMD_R_RF,
Ike Panhc2be1dc22011-09-06 02:31:53 +080070 VPCCMD_W_RF,
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +080071 VPCCMD_R_FAN = 0x2B,
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +080072 VPCCMD_R_SPECIAL_BUTTONS = 0x31,
Ike Panhc2be1dc22011-09-06 02:31:53 +080073 VPCCMD_W_BL_POWER = 0x33,
74};
75
Zhang Rui331e0ea2013-09-25 20:39:49 +080076struct ideapad_rfk_priv {
77 int dev;
78 struct ideapad_private *priv;
79};
80
David Woodhousece326322010-08-11 17:59:35 +010081struct ideapad_private {
Zhang Rui469f6432013-09-25 20:39:47 +080082 struct acpi_device *adev;
Ike Panhcc1f73652010-12-13 18:01:12 +080083 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
Zhang Rui331e0ea2013-09-25 20:39:49 +080084 struct ideapad_rfk_priv rfk_priv[IDEAPAD_RFKILL_DEV_NUM];
Ike Panhc98ee6912010-12-13 18:00:15 +080085 struct platform_device *platform_device;
Ike Panhcf63409a2010-12-13 18:00:38 +080086 struct input_dev *inputdev;
Ike Panhca4ecbb82011-06-30 19:50:52 +080087 struct backlight_device *blightdev;
Ike Panhc773e3202011-09-06 02:32:52 +080088 struct dentry *debug;
Ike Panhc3371f482011-06-30 19:50:40 +080089 unsigned long cfg;
Hans de Goedece363c22014-06-23 16:45:51 +020090 bool has_hw_rfkill_switch;
David Woodhouse58ac7aa2010-08-10 23:44:05 +010091};
92
Ike Panhcbfa97b72010-10-01 15:40:22 +080093static bool no_bt_rfkill;
94module_param(no_bt_rfkill, bool, 0444);
95MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
96
Ike Panhc6a09f212010-10-01 15:38:46 +080097/*
98 * ACPI Helpers
99 */
100#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
101
102static int read_method_int(acpi_handle handle, const char *method, int *val)
103{
104 acpi_status status;
105 unsigned long long result;
106
107 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
108 if (ACPI_FAILURE(status)) {
109 *val = -1;
110 return -1;
111 } else {
112 *val = result;
113 return 0;
114 }
115}
116
117static int method_vpcr(acpi_handle handle, int cmd, int *ret)
118{
119 acpi_status status;
120 unsigned long long result;
121 struct acpi_object_list params;
122 union acpi_object in_obj;
123
124 params.count = 1;
125 params.pointer = &in_obj;
126 in_obj.type = ACPI_TYPE_INTEGER;
127 in_obj.integer.value = cmd;
128
129 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
130
131 if (ACPI_FAILURE(status)) {
132 *ret = -1;
133 return -1;
134 } else {
135 *ret = result;
136 return 0;
137 }
138}
139
140static int method_vpcw(acpi_handle handle, int cmd, int data)
141{
142 struct acpi_object_list params;
143 union acpi_object in_obj[2];
144 acpi_status status;
145
146 params.count = 2;
147 params.pointer = in_obj;
148 in_obj[0].type = ACPI_TYPE_INTEGER;
149 in_obj[0].integer.value = cmd;
150 in_obj[1].type = ACPI_TYPE_INTEGER;
151 in_obj[1].integer.value = data;
152
153 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
154 if (status != AE_OK)
155 return -1;
156 return 0;
157}
158
159static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
160{
161 int val;
162 unsigned long int end_jiffies;
163
164 if (method_vpcw(handle, 1, cmd))
165 return -1;
166
167 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
168 time_before(jiffies, end_jiffies);) {
169 schedule();
170 if (method_vpcr(handle, 1, &val))
171 return -1;
172 if (val == 0) {
173 if (method_vpcr(handle, 0, &val))
174 return -1;
175 *data = val;
176 return 0;
177 }
178 }
179 pr_err("timeout in read_ec_cmd\n");
180 return -1;
181}
182
183static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
184{
185 int val;
186 unsigned long int end_jiffies;
187
188 if (method_vpcw(handle, 0, data))
189 return -1;
190 if (method_vpcw(handle, 1, cmd))
191 return -1;
192
193 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
194 time_before(jiffies, end_jiffies);) {
195 schedule();
196 if (method_vpcr(handle, 1, &val))
197 return -1;
198 if (val == 0)
199 return 0;
200 }
201 pr_err("timeout in write_ec_cmd\n");
202 return -1;
203}
Ike Panhc6a09f212010-10-01 15:38:46 +0800204
Ike Panhca4b5a272010-12-13 18:00:48 +0800205/*
Ike Panhc773e3202011-09-06 02:32:52 +0800206 * debugfs
207 */
Ike Panhc773e3202011-09-06 02:32:52 +0800208static int debugfs_status_show(struct seq_file *s, void *data)
209{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800210 struct ideapad_private *priv = s->private;
Ike Panhc773e3202011-09-06 02:32:52 +0800211 unsigned long value;
212
Zhang Rui331e0ea2013-09-25 20:39:49 +0800213 if (!priv)
214 return -EINVAL;
215
216 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800217 seq_printf(s, "Backlight max:\t%lu\n", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800218 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800219 seq_printf(s, "Backlight now:\t%lu\n", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800220 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800221 seq_printf(s, "BL power value:\t%s\n", value ? "On" : "Off");
222 seq_printf(s, "=====================\n");
223
Zhang Rui331e0ea2013-09-25 20:39:49 +0800224 if (!read_ec_data(priv->adev->handle, VPCCMD_R_RF, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800225 seq_printf(s, "Radio status:\t%s(%lu)\n",
226 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800227 if (!read_ec_data(priv->adev->handle, VPCCMD_R_WIFI, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800228 seq_printf(s, "Wifi status:\t%s(%lu)\n",
229 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800230 if (!read_ec_data(priv->adev->handle, VPCCMD_R_BT, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800231 seq_printf(s, "BT status:\t%s(%lu)\n",
232 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800233 if (!read_ec_data(priv->adev->handle, VPCCMD_R_3G, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800234 seq_printf(s, "3G status:\t%s(%lu)\n",
235 value ? "On" : "Off", value);
236 seq_printf(s, "=====================\n");
237
Zhang Rui331e0ea2013-09-25 20:39:49 +0800238 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800239 seq_printf(s, "Touchpad status:%s(%lu)\n",
240 value ? "On" : "Off", value);
Zhang Rui331e0ea2013-09-25 20:39:49 +0800241 if (!read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &value))
Ike Panhc773e3202011-09-06 02:32:52 +0800242 seq_printf(s, "Camera status:\t%s(%lu)\n",
243 value ? "On" : "Off", value);
244
245 return 0;
246}
247
248static int debugfs_status_open(struct inode *inode, struct file *file)
249{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800250 return single_open(file, debugfs_status_show, inode->i_private);
Ike Panhc773e3202011-09-06 02:32:52 +0800251}
252
253static const struct file_operations debugfs_status_fops = {
254 .owner = THIS_MODULE,
255 .open = debugfs_status_open,
256 .read = seq_read,
257 .llseek = seq_lseek,
258 .release = single_release,
259};
260
261static int debugfs_cfg_show(struct seq_file *s, void *data)
262{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800263 struct ideapad_private *priv = s->private;
264
265 if (!priv) {
Ike Panhc773e3202011-09-06 02:32:52 +0800266 seq_printf(s, "cfg: N/A\n");
267 } else {
268 seq_printf(s, "cfg: 0x%.8lX\n\nCapability: ",
Zhang Rui331e0ea2013-09-25 20:39:49 +0800269 priv->cfg);
270 if (test_bit(CFG_BT_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800271 seq_printf(s, "Bluetooth ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800272 if (test_bit(CFG_3G_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800273 seq_printf(s, "3G ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800274 if (test_bit(CFG_WIFI_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800275 seq_printf(s, "Wireless ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800276 if (test_bit(CFG_CAMERA_BIT, &priv->cfg))
Ike Panhc773e3202011-09-06 02:32:52 +0800277 seq_printf(s, "Camera ");
278 seq_printf(s, "\nGraphic: ");
Zhang Rui331e0ea2013-09-25 20:39:49 +0800279 switch ((priv->cfg)&0x700) {
Ike Panhc773e3202011-09-06 02:32:52 +0800280 case 0x100:
281 seq_printf(s, "Intel");
282 break;
283 case 0x200:
284 seq_printf(s, "ATI");
285 break;
286 case 0x300:
287 seq_printf(s, "Nvidia");
288 break;
289 case 0x400:
290 seq_printf(s, "Intel and ATI");
291 break;
292 case 0x500:
293 seq_printf(s, "Intel and Nvidia");
294 break;
295 }
296 seq_printf(s, "\n");
297 }
298 return 0;
299}
300
301static int debugfs_cfg_open(struct inode *inode, struct file *file)
302{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800303 return single_open(file, debugfs_cfg_show, inode->i_private);
Ike Panhc773e3202011-09-06 02:32:52 +0800304}
305
306static const struct file_operations debugfs_cfg_fops = {
307 .owner = THIS_MODULE,
308 .open = debugfs_cfg_open,
309 .read = seq_read,
310 .llseek = seq_lseek,
311 .release = single_release,
312};
313
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800314static int ideapad_debugfs_init(struct ideapad_private *priv)
Ike Panhc773e3202011-09-06 02:32:52 +0800315{
316 struct dentry *node;
317
318 priv->debug = debugfs_create_dir("ideapad", NULL);
319 if (priv->debug == NULL) {
320 pr_err("failed to create debugfs directory");
321 goto errout;
322 }
323
Zhang Rui331e0ea2013-09-25 20:39:49 +0800324 node = debugfs_create_file("cfg", S_IRUGO, priv->debug, priv,
Ike Panhc773e3202011-09-06 02:32:52 +0800325 &debugfs_cfg_fops);
326 if (!node) {
327 pr_err("failed to create cfg in debugfs");
328 goto errout;
329 }
330
Zhang Rui331e0ea2013-09-25 20:39:49 +0800331 node = debugfs_create_file("status", S_IRUGO, priv->debug, priv,
Ike Panhc773e3202011-09-06 02:32:52 +0800332 &debugfs_status_fops);
333 if (!node) {
Ike Panhca5c38922012-05-03 17:38:35 +0800334 pr_err("failed to create status in debugfs");
Ike Panhc773e3202011-09-06 02:32:52 +0800335 goto errout;
336 }
337
338 return 0;
339
340errout:
341 return -ENOMEM;
342}
343
344static void ideapad_debugfs_exit(struct ideapad_private *priv)
345{
346 debugfs_remove_recursive(priv->debug);
347 priv->debug = NULL;
348}
349
350/*
Ike Panhc3371f482011-06-30 19:50:40 +0800351 * sysfs
Ike Panhca4b5a272010-12-13 18:00:48 +0800352 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100353static ssize_t show_ideapad_cam(struct device *dev,
354 struct device_attribute *attr,
355 char *buf)
356{
Ike Panhc26c81d52010-10-01 15:39:40 +0800357 unsigned long result;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800358 struct ideapad_private *priv = dev_get_drvdata(dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100359
Zhang Rui331e0ea2013-09-25 20:39:49 +0800360 if (read_ec_data(priv->adev->handle, VPCCMD_R_CAMERA, &result))
Ike Panhc26c81d52010-10-01 15:39:40 +0800361 return sprintf(buf, "-1\n");
362 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100363}
364
365static ssize_t store_ideapad_cam(struct device *dev,
366 struct device_attribute *attr,
367 const char *buf, size_t count)
368{
369 int ret, state;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800370 struct ideapad_private *priv = dev_get_drvdata(dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100371
372 if (!count)
373 return 0;
374 if (sscanf(buf, "%i", &state) != 1)
375 return -EINVAL;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800376 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_CAMERA, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100377 if (ret < 0)
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800378 return -EIO;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100379 return count;
380}
381
382static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
383
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800384static ssize_t show_ideapad_fan(struct device *dev,
385 struct device_attribute *attr,
386 char *buf)
387{
388 unsigned long result;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800389 struct ideapad_private *priv = dev_get_drvdata(dev);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800390
Zhang Rui331e0ea2013-09-25 20:39:49 +0800391 if (read_ec_data(priv->adev->handle, VPCCMD_R_FAN, &result))
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800392 return sprintf(buf, "-1\n");
393 return sprintf(buf, "%lu\n", result);
394}
395
396static ssize_t store_ideapad_fan(struct device *dev,
397 struct device_attribute *attr,
398 const char *buf, size_t count)
399{
400 int ret, state;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800401 struct ideapad_private *priv = dev_get_drvdata(dev);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800402
403 if (!count)
404 return 0;
405 if (sscanf(buf, "%i", &state) != 1)
406 return -EINVAL;
407 if (state < 0 || state > 4 || state == 3)
408 return -EINVAL;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800409 ret = write_ec_cmd(priv->adev->handle, VPCCMD_W_FAN, state);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800410 if (ret < 0)
411 return -EIO;
412 return count;
413}
414
415static DEVICE_ATTR(fan_mode, 0644, show_ideapad_fan, store_ideapad_fan);
416
Ike Panhc3371f482011-06-30 19:50:40 +0800417static struct attribute *ideapad_attributes[] = {
418 &dev_attr_camera_power.attr,
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800419 &dev_attr_fan_mode.attr,
Ike Panhc3371f482011-06-30 19:50:40 +0800420 NULL
421};
422
Al Viro587a1f12011-07-23 23:11:19 -0400423static umode_t ideapad_is_visible(struct kobject *kobj,
Ike Panhca84511f2011-06-30 19:50:47 +0800424 struct attribute *attr,
425 int idx)
426{
427 struct device *dev = container_of(kobj, struct device, kobj);
428 struct ideapad_private *priv = dev_get_drvdata(dev);
429 bool supported;
430
431 if (attr == &dev_attr_camera_power.attr)
432 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800433 else if (attr == &dev_attr_fan_mode.attr) {
434 unsigned long value;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800435 supported = !read_ec_data(priv->adev->handle, VPCCMD_R_FAN,
436 &value);
Maxim Mikityanskiy0c7bbeb92012-07-06 16:08:11 +0800437 } else
Ike Panhca84511f2011-06-30 19:50:47 +0800438 supported = true;
439
440 return supported ? attr->mode : 0;
441}
442
Mathias Krause49458e82014-07-16 19:43:15 +0200443static const struct attribute_group ideapad_attribute_group = {
Ike Panhca84511f2011-06-30 19:50:47 +0800444 .is_visible = ideapad_is_visible,
Ike Panhc3371f482011-06-30 19:50:40 +0800445 .attrs = ideapad_attributes
446};
447
Ike Panhca4b5a272010-12-13 18:00:48 +0800448/*
449 * Rfkill
450 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800451struct ideapad_rfk_data {
452 char *name;
453 int cfgbit;
454 int opcode;
455 int type;
456};
457
Mathias Krauseb3d94d72014-08-28 13:02:49 +0200458static const struct ideapad_rfk_data ideapad_rfk_data[] = {
Ike Panhc2be1dc22011-09-06 02:31:53 +0800459 { "ideapad_wlan", CFG_WIFI_BIT, VPCCMD_W_WIFI, RFKILL_TYPE_WLAN },
460 { "ideapad_bluetooth", CFG_BT_BIT, VPCCMD_W_BT, RFKILL_TYPE_BLUETOOTH },
461 { "ideapad_3g", CFG_3G_BIT, VPCCMD_W_3G, RFKILL_TYPE_WWAN },
Ike Panhcc1f73652010-12-13 18:01:12 +0800462};
463
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100464static int ideapad_rfk_set(void *data, bool blocked)
465{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800466 struct ideapad_rfk_priv *priv = data;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100467
Zhang Rui331e0ea2013-09-25 20:39:49 +0800468 return write_ec_cmd(priv->priv->adev->handle, priv->dev, !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100469}
470
471static struct rfkill_ops ideapad_rfk_ops = {
472 .set_block = ideapad_rfk_set,
473};
474
Ike Panhc923de842011-09-06 02:32:01 +0800475static void ideapad_sync_rfk_state(struct ideapad_private *priv)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100476{
Hans de Goedece363c22014-06-23 16:45:51 +0200477 unsigned long hw_blocked = 0;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100478 int i;
479
Hans de Goedece363c22014-06-23 16:45:51 +0200480 if (priv->has_hw_rfkill_switch) {
481 if (read_ec_data(priv->adev->handle, VPCCMD_R_RF, &hw_blocked))
482 return;
483 hw_blocked = !hw_blocked;
484 }
Ike Panhc2b7266b2010-10-01 15:39:49 +0800485
Ike Panhcc1f73652010-12-13 18:01:12 +0800486 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100487 if (priv->rfk[i])
488 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100489}
490
Zhang Rui75a11f12013-09-25 20:39:48 +0800491static int ideapad_register_rfkill(struct ideapad_private *priv, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100492{
493 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800494 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100495
Ike Panhcbfa97b72010-10-01 15:40:22 +0800496 if (no_bt_rfkill &&
497 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
498 /* Force to enable bluetooth when no_bt_rfkill=1 */
Zhang Rui331e0ea2013-09-25 20:39:49 +0800499 write_ec_cmd(priv->adev->handle,
Ike Panhcbfa97b72010-10-01 15:40:22 +0800500 ideapad_rfk_data[dev].opcode, 1);
501 return 0;
502 }
Zhang Rui331e0ea2013-09-25 20:39:49 +0800503 priv->rfk_priv[dev].dev = dev;
504 priv->rfk_priv[dev].priv = priv;
Ike Panhcbfa97b72010-10-01 15:40:22 +0800505
Zhang Rui75a11f12013-09-25 20:39:48 +0800506 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name,
Zhang Ruib5c37b72013-09-25 20:39:50 +0800507 &priv->platform_device->dev,
Zhang Rui75a11f12013-09-25 20:39:48 +0800508 ideapad_rfk_data[dev].type,
509 &ideapad_rfk_ops,
Zhang Rui331e0ea2013-09-25 20:39:49 +0800510 &priv->rfk_priv[dev]);
David Woodhousece326322010-08-11 17:59:35 +0100511 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100512 return -ENOMEM;
513
Zhang Rui331e0ea2013-09-25 20:39:49 +0800514 if (read_ec_data(priv->adev->handle, ideapad_rfk_data[dev].opcode-1,
Ike Panhc2b7266b2010-10-01 15:39:49 +0800515 &sw_blocked)) {
516 rfkill_init_sw_state(priv->rfk[dev], 0);
517 } else {
518 sw_blocked = !sw_blocked;
519 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
520 }
521
David Woodhousece326322010-08-11 17:59:35 +0100522 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100523 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100524 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100525 return ret;
526 }
527 return 0;
528}
529
Zhang Rui75a11f12013-09-25 20:39:48 +0800530static void ideapad_unregister_rfkill(struct ideapad_private *priv, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100531{
David Woodhousece326322010-08-11 17:59:35 +0100532 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100533 return;
534
David Woodhousece326322010-08-11 17:59:35 +0100535 rfkill_unregister(priv->rfk[dev]);
536 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100537}
538
Ike Panhc98ee6912010-12-13 18:00:15 +0800539/*
540 * Platform device
541 */
Zhang Ruib5c37b72013-09-25 20:39:50 +0800542static int ideapad_sysfs_init(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800543{
Zhang Ruib5c37b72013-09-25 20:39:50 +0800544 return sysfs_create_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800545 &ideapad_attribute_group);
Ike Panhc98ee6912010-12-13 18:00:15 +0800546}
547
Zhang Ruib5c37b72013-09-25 20:39:50 +0800548static void ideapad_sysfs_exit(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800549{
Ike Panhc8693ae82010-12-13 18:01:01 +0800550 sysfs_remove_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}
Ike Panhc98ee6912010-12-13 18:00:15 +0800553
Ike Panhcf63409a2010-12-13 18:00:38 +0800554/*
555 * input device
556 */
557static const struct key_entry ideapad_keymap[] = {
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800558 { KE_KEY, 6, { KEY_SWITCHVIDEOMODE } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800559 { KE_KEY, 7, { KEY_CAMERA } },
560 { KE_KEY, 11, { KEY_F16 } },
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800561 { KE_KEY, 13, { KEY_WLAN } },
562 { KE_KEY, 16, { KEY_PROG1 } },
563 { KE_KEY, 17, { KEY_PROG2 } },
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800564 { KE_KEY, 64, { KEY_PROG3 } },
565 { KE_KEY, 65, { KEY_PROG4 } },
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800566 { KE_KEY, 66, { KEY_TOUCHPAD_OFF } },
567 { KE_KEY, 67, { KEY_TOUCHPAD_ON } },
Ike Panhcf63409a2010-12-13 18:00:38 +0800568 { KE_END, 0 },
569};
570
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800571static int ideapad_input_init(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800572{
573 struct input_dev *inputdev;
574 int error;
575
576 inputdev = input_allocate_device();
Joe Perchesb222cca2013-10-23 12:14:52 -0700577 if (!inputdev)
Ike Panhcf63409a2010-12-13 18:00:38 +0800578 return -ENOMEM;
Ike Panhcf63409a2010-12-13 18:00:38 +0800579
580 inputdev->name = "Ideapad extra buttons";
581 inputdev->phys = "ideapad/input0";
582 inputdev->id.bustype = BUS_HOST;
Ike Panhc8693ae82010-12-13 18:01:01 +0800583 inputdev->dev.parent = &priv->platform_device->dev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800584
585 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
586 if (error) {
587 pr_err("Unable to setup input device keymap\n");
588 goto err_free_dev;
589 }
590
591 error = input_register_device(inputdev);
592 if (error) {
593 pr_err("Unable to register input device\n");
594 goto err_free_keymap;
595 }
596
Ike Panhc8693ae82010-12-13 18:01:01 +0800597 priv->inputdev = inputdev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800598 return 0;
599
600err_free_keymap:
601 sparse_keymap_free(inputdev);
602err_free_dev:
603 input_free_device(inputdev);
604 return error;
605}
606
Axel Lin7451a552011-07-27 15:27:34 +0800607static void ideapad_input_exit(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800608{
Ike Panhc8693ae82010-12-13 18:01:01 +0800609 sparse_keymap_free(priv->inputdev);
610 input_unregister_device(priv->inputdev);
611 priv->inputdev = NULL;
Ike Panhcf63409a2010-12-13 18:00:38 +0800612}
613
Ike Panhc8693ae82010-12-13 18:01:01 +0800614static void ideapad_input_report(struct ideapad_private *priv,
615 unsigned long scancode)
Ike Panhcf63409a2010-12-13 18:00:38 +0800616{
Ike Panhc8693ae82010-12-13 18:01:01 +0800617 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
Ike Panhcf63409a2010-12-13 18:00:38 +0800618}
Ike Panhcf63409a2010-12-13 18:00:38 +0800619
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800620static void ideapad_input_novokey(struct ideapad_private *priv)
621{
622 unsigned long long_pressed;
623
Zhang Rui331e0ea2013-09-25 20:39:49 +0800624 if (read_ec_data(priv->adev->handle, VPCCMD_R_NOVO, &long_pressed))
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800625 return;
626 if (long_pressed)
627 ideapad_input_report(priv, 17);
628 else
629 ideapad_input_report(priv, 16);
630}
631
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800632static void ideapad_check_special_buttons(struct ideapad_private *priv)
633{
634 unsigned long bit, value;
635
Zhang Rui331e0ea2013-09-25 20:39:49 +0800636 read_ec_data(priv->adev->handle, VPCCMD_R_SPECIAL_BUTTONS, &value);
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800637
638 for (bit = 0; bit < 16; bit++) {
639 if (test_bit(bit, &value)) {
640 switch (bit) {
Maxim Mikityanskiya1ec56e2013-03-20 12:34:17 +0200641 case 0: /* Z580 */
642 case 6: /* Z570 */
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800643 /* Thermal Management button */
644 ideapad_input_report(priv, 65);
645 break;
646 case 1:
647 /* OneKey Theater button */
648 ideapad_input_report(priv, 64);
649 break;
Maxim Mikityanskiya1ec56e2013-03-20 12:34:17 +0200650 default:
651 pr_info("Unknown special button: %lu\n", bit);
652 break;
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800653 }
654 }
655 }
656}
657
Ike Panhca4b5a272010-12-13 18:00:48 +0800658/*
Ike Panhca4ecbb82011-06-30 19:50:52 +0800659 * backlight
660 */
661static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
662{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800663 struct ideapad_private *priv = bl_get_data(blightdev);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800664 unsigned long now;
665
Zhang Rui331e0ea2013-09-25 20:39:49 +0800666 if (!priv)
667 return -EINVAL;
668
669 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800670 return -EIO;
671 return now;
672}
673
674static int ideapad_backlight_update_status(struct backlight_device *blightdev)
675{
Zhang Rui331e0ea2013-09-25 20:39:49 +0800676 struct ideapad_private *priv = bl_get_data(blightdev);
677
678 if (!priv)
679 return -EINVAL;
680
681 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL,
Ike Panhc2be1dc22011-09-06 02:31:53 +0800682 blightdev->props.brightness))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800683 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800684 if (write_ec_cmd(priv->adev->handle, VPCCMD_W_BL_POWER,
Ike Panhca4ecbb82011-06-30 19:50:52 +0800685 blightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
686 return -EIO;
687
688 return 0;
689}
690
691static const struct backlight_ops ideapad_backlight_ops = {
692 .get_brightness = ideapad_backlight_get_brightness,
693 .update_status = ideapad_backlight_update_status,
694};
695
696static int ideapad_backlight_init(struct ideapad_private *priv)
697{
698 struct backlight_device *blightdev;
699 struct backlight_properties props;
700 unsigned long max, now, power;
701
Zhang Rui331e0ea2013-09-25 20:39:49 +0800702 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_MAX, &max))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800703 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800704 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800705 return -EIO;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800706 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800707 return -EIO;
708
709 memset(&props, 0, sizeof(struct backlight_properties));
710 props.max_brightness = max;
711 props.type = BACKLIGHT_PLATFORM;
712 blightdev = backlight_device_register("ideapad",
713 &priv->platform_device->dev,
714 priv,
715 &ideapad_backlight_ops,
716 &props);
717 if (IS_ERR(blightdev)) {
718 pr_err("Could not register backlight device\n");
719 return PTR_ERR(blightdev);
720 }
721
722 priv->blightdev = blightdev;
723 blightdev->props.brightness = now;
724 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
725 backlight_update_status(blightdev);
726
727 return 0;
728}
729
730static void ideapad_backlight_exit(struct ideapad_private *priv)
731{
Markus Elfring00981812014-11-24 20:30:29 +0100732 backlight_device_unregister(priv->blightdev);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800733 priv->blightdev = NULL;
734}
735
736static void ideapad_backlight_notify_power(struct ideapad_private *priv)
737{
738 unsigned long power;
739 struct backlight_device *blightdev = priv->blightdev;
740
Rene Bollfordd4afc772011-10-23 09:56:42 +0200741 if (!blightdev)
742 return;
Zhang Rui331e0ea2013-09-25 20:39:49 +0800743 if (read_ec_data(priv->adev->handle, VPCCMD_R_BL_POWER, &power))
Ike Panhca4ecbb82011-06-30 19:50:52 +0800744 return;
745 blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
746}
747
748static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
749{
750 unsigned long now;
751
752 /* if we control brightness via acpi video driver */
753 if (priv->blightdev == NULL) {
Zhang Rui331e0ea2013-09-25 20:39:49 +0800754 read_ec_data(priv->adev->handle, VPCCMD_R_BL, &now);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800755 return;
756 }
757
758 backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
759}
760
761/*
Ike Panhca4b5a272010-12-13 18:00:48 +0800762 * module init/exit
763 */
Zhang Rui75a11f12013-09-25 20:39:48 +0800764static void ideapad_sync_touchpad_state(struct ideapad_private *priv)
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800765{
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800766 unsigned long value;
767
768 /* Without reading from EC touchpad LED doesn't switch state */
Zhang Rui75a11f12013-09-25 20:39:48 +0800769 if (!read_ec_data(priv->adev->handle, VPCCMD_R_TOUCHPAD, &value)) {
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800770 /* Some IdeaPads don't really turn off touchpad - they only
771 * switch the LED state. We (de)activate KBC AUX port to turn
772 * touchpad off and on. We send KEY_TOUCHPAD_OFF and
773 * KEY_TOUCHPAD_ON to not to get out of sync with LED */
774 unsigned char param;
775 i8042_command(&param, value ? I8042_CMD_AUX_ENABLE :
776 I8042_CMD_AUX_DISABLE);
777 ideapad_input_report(priv, value ? 67 : 66);
778 }
779}
780
Zhang Ruib5c37b72013-09-25 20:39:50 +0800781static void ideapad_acpi_notify(acpi_handle handle, u32 event, void *data)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100782{
Zhang Ruib5c37b72013-09-25 20:39:50 +0800783 struct ideapad_private *priv = data;
Ike Panhc8e7d3542010-10-01 15:39:05 +0800784 unsigned long vpc1, vpc2, vpc_bit;
785
Ike Panhc2be1dc22011-09-06 02:31:53 +0800786 if (read_ec_data(handle, VPCCMD_R_VPC1, &vpc1))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800787 return;
Ike Panhc2be1dc22011-09-06 02:31:53 +0800788 if (read_ec_data(handle, VPCCMD_R_VPC2, &vpc2))
Ike Panhc8e7d3542010-10-01 15:39:05 +0800789 return;
790
791 vpc1 = (vpc2 << 8) | vpc1;
792 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
793 if (test_bit(vpc_bit, &vpc1)) {
Ike Panhca4ecbb82011-06-30 19:50:52 +0800794 switch (vpc_bit) {
795 case 9:
Ike Panhc923de842011-09-06 02:32:01 +0800796 ideapad_sync_rfk_state(priv);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800797 break;
Ike Panhc20a769c2012-05-03 17:38:46 +0800798 case 13:
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800799 case 11:
800 case 7:
Ike Panhc20a769c2012-05-03 17:38:46 +0800801 case 6:
802 ideapad_input_report(priv, vpc_bit);
803 break;
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800804 case 5:
Zhang Rui75a11f12013-09-25 20:39:48 +0800805 ideapad_sync_touchpad_state(priv);
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800806 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800807 case 4:
808 ideapad_backlight_notify_brightness(priv);
809 break;
Ike Panhcf43d9ec2011-09-06 02:32:10 +0800810 case 3:
811 ideapad_input_novokey(priv);
812 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800813 case 2:
814 ideapad_backlight_notify_power(priv);
815 break;
Maxim Mikityanskiy296f9fe2012-07-06 16:07:50 +0800816 case 0:
817 ideapad_check_special_buttons(priv);
818 break;
Ike Panhca4ecbb82011-06-30 19:50:52 +0800819 default:
Ike Panhc20a769c2012-05-03 17:38:46 +0800820 pr_info("Unknown event: %lu\n", vpc_bit);
Ike Panhca4ecbb82011-06-30 19:50:52 +0800821 }
Ike Panhc8e7d3542010-10-01 15:39:05 +0800822 }
823 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100824}
825
Hans de Goedece363c22014-06-23 16:45:51 +0200826/*
827 * Some ideapads don't have a hardware rfkill switch, reading VPCCMD_R_RF
828 * always results in 0 on these models, causing ideapad_laptop to wrongly
829 * report all radios as hardware-blocked.
830 */
Mathias Krauseb3d94d72014-08-28 13:02:49 +0200831static const struct dmi_system_id no_hw_rfkill_list[] = {
Hans de Goede85093f72014-05-13 16:00:28 +0200832 {
Hans de Goedece363c22014-06-23 16:45:51 +0200833 .ident = "Lenovo Yoga 2 11 / 13 / Pro",
Hans de Goede85093f72014-05-13 16:00:28 +0200834 .matches = {
835 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
Hans de Goedece363c22014-06-23 16:45:51 +0200836 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo Yoga 2"),
Hans de Goede85093f72014-05-13 16:00:28 +0200837 },
838 },
Stephan Mueller725c7f62014-10-27 04:09:50 +0100839 {
840 .ident = "Lenovo Yoga 3 Pro 1370",
841 .matches = {
842 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
843 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo YOGA 3 Pro-1370"),
844 },
845 },
Hans de Goede85093f72014-05-13 16:00:28 +0200846 {}
847};
848
Zhang Ruib5c37b72013-09-25 20:39:50 +0800849static int ideapad_acpi_add(struct platform_device *pdev)
850{
851 int ret, i;
852 int cfg;
853 struct ideapad_private *priv;
854 struct acpi_device *adev;
855
856 ret = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev);
857 if (ret)
858 return -ENODEV;
859
860 if (read_method_int(adev->handle, "_CFG", &cfg))
861 return -ENODEV;
862
Himangi Saraogib3facd72014-06-09 17:46:50 -0400863 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800864 if (!priv)
865 return -ENOMEM;
866
867 dev_set_drvdata(&pdev->dev, priv);
868 priv->cfg = cfg;
869 priv->adev = adev;
870 priv->platform_device = pdev;
Hans de Goedece363c22014-06-23 16:45:51 +0200871 priv->has_hw_rfkill_switch = !dmi_check_system(no_hw_rfkill_list);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800872
873 ret = ideapad_sysfs_init(priv);
874 if (ret)
Himangi Saraogib3facd72014-06-09 17:46:50 -0400875 return ret;
Zhang Ruib5c37b72013-09-25 20:39:50 +0800876
877 ret = ideapad_debugfs_init(priv);
878 if (ret)
879 goto debugfs_failed;
880
881 ret = ideapad_input_init(priv);
882 if (ret)
883 goto input_failed;
884
Hans de Goedece363c22014-06-23 16:45:51 +0200885 /*
886 * On some models without a hw-switch (the yoga 2 13 at least)
887 * VPCCMD_W_RF must be explicitly set to 1 for the wifi to work.
888 */
889 if (!priv->has_hw_rfkill_switch)
890 write_ec_cmd(priv->adev->handle, VPCCMD_W_RF, 1);
891
892 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
893 if (test_bit(ideapad_rfk_data[i].cfgbit, &priv->cfg))
894 ideapad_register_rfkill(priv, i);
895
Zhang Ruib5c37b72013-09-25 20:39:50 +0800896 ideapad_sync_rfk_state(priv);
897 ideapad_sync_touchpad_state(priv);
898
899 if (!acpi_video_backlight_support()) {
900 ret = ideapad_backlight_init(priv);
901 if (ret && ret != -ENODEV)
902 goto backlight_failed;
903 }
904 ret = acpi_install_notify_handler(adev->handle,
905 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify, priv);
906 if (ret)
907 goto notification_failed;
908
909 return 0;
910notification_failed:
911 ideapad_backlight_exit(priv);
912backlight_failed:
913 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
914 ideapad_unregister_rfkill(priv, i);
915 ideapad_input_exit(priv);
916input_failed:
917 ideapad_debugfs_exit(priv);
918debugfs_failed:
919 ideapad_sysfs_exit(priv);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800920 return ret;
921}
922
923static int ideapad_acpi_remove(struct platform_device *pdev)
924{
925 struct ideapad_private *priv = dev_get_drvdata(&pdev->dev);
926 int i;
927
928 acpi_remove_notify_handler(priv->adev->handle,
929 ACPI_DEVICE_NOTIFY, ideapad_acpi_notify);
930 ideapad_backlight_exit(priv);
931 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
932 ideapad_unregister_rfkill(priv, i);
933 ideapad_input_exit(priv);
934 ideapad_debugfs_exit(priv);
935 ideapad_sysfs_exit(priv);
936 dev_set_drvdata(&pdev->dev, NULL);
Zhang Ruib5c37b72013-09-25 20:39:50 +0800937
938 return 0;
939}
940
Zhang Rui11fa8da2013-09-25 20:39:46 +0800941#ifdef CONFIG_PM_SLEEP
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800942static int ideapad_acpi_resume(struct device *device)
943{
Zhang Rui75a11f12013-09-25 20:39:48 +0800944 struct ideapad_private *priv;
945
946 if (!device)
947 return -EINVAL;
948 priv = dev_get_drvdata(device);
949
950 ideapad_sync_rfk_state(priv);
951 ideapad_sync_touchpad_state(priv);
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800952 return 0;
953}
Zhang Ruib5c37b72013-09-25 20:39:50 +0800954#endif
Maxim Mikityanskiy07a4a4f2012-07-06 16:08:00 +0800955static SIMPLE_DEV_PM_OPS(ideapad_pm, NULL, ideapad_acpi_resume);
956
Zhang Ruib5c37b72013-09-25 20:39:50 +0800957static const struct acpi_device_id ideapad_device_ids[] = {
958 { "VPC2004", 0},
959 { "", 0},
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100960};
Zhang Ruib5c37b72013-09-25 20:39:50 +0800961MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
962
963static struct platform_driver ideapad_acpi_driver = {
964 .probe = ideapad_acpi_add,
965 .remove = ideapad_acpi_remove,
966 .driver = {
967 .name = "ideapad_acpi",
Zhang Ruib5c37b72013-09-25 20:39:50 +0800968 .pm = &ideapad_pm,
969 .acpi_match_table = ACPI_PTR(ideapad_device_ids),
970 },
971};
972
973module_platform_driver(ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100974
975MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
976MODULE_DESCRIPTION("IdeaPad ACPI Extras");
977MODULE_LICENSE("GPL");