blob: 1612abde5188169935d7919517077a36473f90d2 [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>
29#include <acpi/acpi_bus.h>
30#include <acpi/acpi_drivers.h>
31#include <linux/rfkill.h>
Ike Panhc98ee6912010-12-13 18:00:15 +080032#include <linux/platform_device.h>
Ike Panhcf63409a2010-12-13 18:00:38 +080033#include <linux/input.h>
34#include <linux/input/sparse-keymap.h>
David Woodhouse58ac7aa2010-08-10 23:44:05 +010035
Ike Panhcc1f73652010-12-13 18:01:12 +080036#define IDEAPAD_RFKILL_DEV_NUM (3)
David Woodhouse58ac7aa2010-08-10 23:44:05 +010037
Ike Panhc3371f482011-06-30 19:50:40 +080038#define CFG_BT_BIT (16)
39#define CFG_3G_BIT (17)
40#define CFG_WIFI_BIT (18)
Ike Panhca84511f2011-06-30 19:50:47 +080041#define CFG_CAMERA_BIT (19)
Ike Panhc3371f482011-06-30 19:50:40 +080042
David Woodhousece326322010-08-11 17:59:35 +010043struct ideapad_private {
Ike Panhcc1f73652010-12-13 18:01:12 +080044 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
Ike Panhc98ee6912010-12-13 18:00:15 +080045 struct platform_device *platform_device;
Ike Panhcf63409a2010-12-13 18:00:38 +080046 struct input_dev *inputdev;
Ike Panhc3371f482011-06-30 19:50:40 +080047 unsigned long cfg;
David Woodhouse58ac7aa2010-08-10 23:44:05 +010048};
49
Ike Panhcc1f73652010-12-13 18:01:12 +080050static acpi_handle ideapad_handle;
Ike Panhcbfa97b72010-10-01 15:40:22 +080051static bool no_bt_rfkill;
52module_param(no_bt_rfkill, bool, 0444);
53MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
54
Ike Panhc6a09f212010-10-01 15:38:46 +080055/*
56 * ACPI Helpers
57 */
58#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
59
60static int read_method_int(acpi_handle handle, const char *method, int *val)
61{
62 acpi_status status;
63 unsigned long long result;
64
65 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
66 if (ACPI_FAILURE(status)) {
67 *val = -1;
68 return -1;
69 } else {
70 *val = result;
71 return 0;
72 }
73}
74
75static int method_vpcr(acpi_handle handle, int cmd, int *ret)
76{
77 acpi_status status;
78 unsigned long long result;
79 struct acpi_object_list params;
80 union acpi_object in_obj;
81
82 params.count = 1;
83 params.pointer = &in_obj;
84 in_obj.type = ACPI_TYPE_INTEGER;
85 in_obj.integer.value = cmd;
86
87 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
88
89 if (ACPI_FAILURE(status)) {
90 *ret = -1;
91 return -1;
92 } else {
93 *ret = result;
94 return 0;
95 }
96}
97
98static int method_vpcw(acpi_handle handle, int cmd, int data)
99{
100 struct acpi_object_list params;
101 union acpi_object in_obj[2];
102 acpi_status status;
103
104 params.count = 2;
105 params.pointer = in_obj;
106 in_obj[0].type = ACPI_TYPE_INTEGER;
107 in_obj[0].integer.value = cmd;
108 in_obj[1].type = ACPI_TYPE_INTEGER;
109 in_obj[1].integer.value = data;
110
111 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
112 if (status != AE_OK)
113 return -1;
114 return 0;
115}
116
117static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
118{
119 int val;
120 unsigned long int end_jiffies;
121
122 if (method_vpcw(handle, 1, cmd))
123 return -1;
124
125 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
126 time_before(jiffies, end_jiffies);) {
127 schedule();
128 if (method_vpcr(handle, 1, &val))
129 return -1;
130 if (val == 0) {
131 if (method_vpcr(handle, 0, &val))
132 return -1;
133 *data = val;
134 return 0;
135 }
136 }
137 pr_err("timeout in read_ec_cmd\n");
138 return -1;
139}
140
141static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
142{
143 int val;
144 unsigned long int end_jiffies;
145
146 if (method_vpcw(handle, 0, data))
147 return -1;
148 if (method_vpcw(handle, 1, cmd))
149 return -1;
150
151 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
152 time_before(jiffies, end_jiffies);) {
153 schedule();
154 if (method_vpcr(handle, 1, &val))
155 return -1;
156 if (val == 0)
157 return 0;
158 }
159 pr_err("timeout in write_ec_cmd\n");
160 return -1;
161}
Ike Panhc6a09f212010-10-01 15:38:46 +0800162
Ike Panhca4b5a272010-12-13 18:00:48 +0800163/*
Ike Panhc3371f482011-06-30 19:50:40 +0800164 * sysfs
Ike Panhca4b5a272010-12-13 18:00:48 +0800165 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100166static ssize_t show_ideapad_cam(struct device *dev,
167 struct device_attribute *attr,
168 char *buf)
169{
Ike Panhc26c81d52010-10-01 15:39:40 +0800170 unsigned long result;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100171
Ike Panhcc1f73652010-12-13 18:01:12 +0800172 if (read_ec_data(ideapad_handle, 0x1D, &result))
Ike Panhc26c81d52010-10-01 15:39:40 +0800173 return sprintf(buf, "-1\n");
174 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100175}
176
177static ssize_t store_ideapad_cam(struct device *dev,
178 struct device_attribute *attr,
179 const char *buf, size_t count)
180{
181 int ret, state;
182
183 if (!count)
184 return 0;
185 if (sscanf(buf, "%i", &state) != 1)
186 return -EINVAL;
Ike Panhcc1f73652010-12-13 18:01:12 +0800187 ret = write_ec_cmd(ideapad_handle, 0x1E, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100188 if (ret < 0)
189 return ret;
190 return count;
191}
192
193static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
194
Ike Panhc3371f482011-06-30 19:50:40 +0800195static ssize_t show_ideapad_cfg(struct device *dev,
196 struct device_attribute *attr,
197 char *buf)
198{
199 struct ideapad_private *priv = dev_get_drvdata(dev);
200
201 return sprintf(buf, "0x%.8lX\n", priv->cfg);
202}
203
204static DEVICE_ATTR(cfg, 0444, show_ideapad_cfg, NULL);
205
206static struct attribute *ideapad_attributes[] = {
207 &dev_attr_camera_power.attr,
208 &dev_attr_cfg.attr,
209 NULL
210};
211
Ike Panhca84511f2011-06-30 19:50:47 +0800212static mode_t ideapad_is_visible(struct kobject *kobj,
213 struct attribute *attr,
214 int idx)
215{
216 struct device *dev = container_of(kobj, struct device, kobj);
217 struct ideapad_private *priv = dev_get_drvdata(dev);
218 bool supported;
219
220 if (attr == &dev_attr_camera_power.attr)
221 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
222 else
223 supported = true;
224
225 return supported ? attr->mode : 0;
226}
227
Ike Panhc3371f482011-06-30 19:50:40 +0800228static struct attribute_group ideapad_attribute_group = {
Ike Panhca84511f2011-06-30 19:50:47 +0800229 .is_visible = ideapad_is_visible,
Ike Panhc3371f482011-06-30 19:50:40 +0800230 .attrs = ideapad_attributes
231};
232
Ike Panhca4b5a272010-12-13 18:00:48 +0800233/*
234 * Rfkill
235 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800236struct ideapad_rfk_data {
237 char *name;
238 int cfgbit;
239 int opcode;
240 int type;
241};
242
243const struct ideapad_rfk_data ideapad_rfk_data[] = {
Ike Panhc3371f482011-06-30 19:50:40 +0800244 { "ideapad_wlan", CFG_WIFI_BIT, 0x15, RFKILL_TYPE_WLAN },
245 { "ideapad_bluetooth", CFG_BT_BIT, 0x17, RFKILL_TYPE_BLUETOOTH },
246 { "ideapad_3g", CFG_3G_BIT, 0x20, RFKILL_TYPE_WWAN },
Ike Panhcc1f73652010-12-13 18:01:12 +0800247};
248
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100249static int ideapad_rfk_set(void *data, bool blocked)
250{
Ike Panhcc1f73652010-12-13 18:01:12 +0800251 unsigned long opcode = (unsigned long)data;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100252
Ike Panhcc1f73652010-12-13 18:01:12 +0800253 return write_ec_cmd(ideapad_handle, opcode, !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100254}
255
256static struct rfkill_ops ideapad_rfk_ops = {
257 .set_block = ideapad_rfk_set,
258};
259
David Woodhousece326322010-08-11 17:59:35 +0100260static void ideapad_sync_rfk_state(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100261{
David Woodhousece326322010-08-11 17:59:35 +0100262 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
Ike Panhc2b7266b2010-10-01 15:39:49 +0800263 unsigned long hw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100264 int i;
265
Ike Panhcc1f73652010-12-13 18:01:12 +0800266 if (read_ec_data(ideapad_handle, 0x23, &hw_blocked))
Ike Panhc2b7266b2010-10-01 15:39:49 +0800267 return;
268 hw_blocked = !hw_blocked;
269
Ike Panhcc1f73652010-12-13 18:01:12 +0800270 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100271 if (priv->rfk[i])
272 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100273}
274
Ike Panhca4b5a272010-12-13 18:00:48 +0800275static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
276 int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100277{
David Woodhousece326322010-08-11 17:59:35 +0100278 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100279 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800280 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100281
Ike Panhcbfa97b72010-10-01 15:40:22 +0800282 if (no_bt_rfkill &&
283 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
284 /* Force to enable bluetooth when no_bt_rfkill=1 */
Ike Panhcc1f73652010-12-13 18:01:12 +0800285 write_ec_cmd(ideapad_handle,
Ike Panhcbfa97b72010-10-01 15:40:22 +0800286 ideapad_rfk_data[dev].opcode, 1);
287 return 0;
288 }
289
Ike Panhc2b7266b2010-10-01 15:39:49 +0800290 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
291 ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
David Woodhousece326322010-08-11 17:59:35 +0100292 (void *)(long)dev);
293 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100294 return -ENOMEM;
295
Ike Panhcc1f73652010-12-13 18:01:12 +0800296 if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1,
Ike Panhc2b7266b2010-10-01 15:39:49 +0800297 &sw_blocked)) {
298 rfkill_init_sw_state(priv->rfk[dev], 0);
299 } else {
300 sw_blocked = !sw_blocked;
301 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
302 }
303
David Woodhousece326322010-08-11 17:59:35 +0100304 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100305 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100306 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100307 return ret;
308 }
309 return 0;
310}
311
Ike Panhca4b5a272010-12-13 18:00:48 +0800312static void __devexit ideapad_unregister_rfkill(struct acpi_device *adevice,
313 int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100314{
David Woodhousece326322010-08-11 17:59:35 +0100315 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
316
317 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100318 return;
319
David Woodhousece326322010-08-11 17:59:35 +0100320 rfkill_unregister(priv->rfk[dev]);
321 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100322}
323
Ike Panhc98ee6912010-12-13 18:00:15 +0800324/*
325 * Platform device
326 */
Ike Panhc8693ae82010-12-13 18:01:01 +0800327static int __devinit ideapad_platform_init(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800328{
329 int result;
330
Ike Panhc8693ae82010-12-13 18:01:01 +0800331 priv->platform_device = platform_device_alloc("ideapad", -1);
332 if (!priv->platform_device)
Ike Panhc98ee6912010-12-13 18:00:15 +0800333 return -ENOMEM;
Ike Panhc8693ae82010-12-13 18:01:01 +0800334 platform_set_drvdata(priv->platform_device, priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800335
Ike Panhc8693ae82010-12-13 18:01:01 +0800336 result = platform_device_add(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800337 if (result)
338 goto fail_platform_device;
339
Ike Panhc8693ae82010-12-13 18:01:01 +0800340 result = sysfs_create_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800341 &ideapad_attribute_group);
342 if (result)
343 goto fail_sysfs;
Ike Panhc98ee6912010-12-13 18:00:15 +0800344 return 0;
345
Ike Panhcc9f718d2010-12-13 18:00:27 +0800346fail_sysfs:
Ike Panhc8693ae82010-12-13 18:01:01 +0800347 platform_device_del(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800348fail_platform_device:
Ike Panhc8693ae82010-12-13 18:01:01 +0800349 platform_device_put(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800350 return result;
351}
352
Ike Panhc8693ae82010-12-13 18:01:01 +0800353static void ideapad_platform_exit(struct ideapad_private *priv)
Ike Panhc98ee6912010-12-13 18:00:15 +0800354{
Ike Panhc8693ae82010-12-13 18:01:01 +0800355 sysfs_remove_group(&priv->platform_device->dev.kobj,
Ike Panhcc9f718d2010-12-13 18:00:27 +0800356 &ideapad_attribute_group);
Ike Panhc8693ae82010-12-13 18:01:01 +0800357 platform_device_unregister(priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800358}
Ike Panhc98ee6912010-12-13 18:00:15 +0800359
Ike Panhcf63409a2010-12-13 18:00:38 +0800360/*
361 * input device
362 */
363static const struct key_entry ideapad_keymap[] = {
364 { KE_KEY, 0x06, { KEY_SWITCHVIDEOMODE } },
365 { KE_KEY, 0x0D, { KEY_WLAN } },
366 { KE_END, 0 },
367};
368
Ike Panhc8693ae82010-12-13 18:01:01 +0800369static int __devinit ideapad_input_init(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800370{
371 struct input_dev *inputdev;
372 int error;
373
374 inputdev = input_allocate_device();
375 if (!inputdev) {
376 pr_info("Unable to allocate input device\n");
377 return -ENOMEM;
378 }
379
380 inputdev->name = "Ideapad extra buttons";
381 inputdev->phys = "ideapad/input0";
382 inputdev->id.bustype = BUS_HOST;
Ike Panhc8693ae82010-12-13 18:01:01 +0800383 inputdev->dev.parent = &priv->platform_device->dev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800384
385 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
386 if (error) {
387 pr_err("Unable to setup input device keymap\n");
388 goto err_free_dev;
389 }
390
391 error = input_register_device(inputdev);
392 if (error) {
393 pr_err("Unable to register input device\n");
394 goto err_free_keymap;
395 }
396
Ike Panhc8693ae82010-12-13 18:01:01 +0800397 priv->inputdev = inputdev;
Ike Panhcf63409a2010-12-13 18:00:38 +0800398 return 0;
399
400err_free_keymap:
401 sparse_keymap_free(inputdev);
402err_free_dev:
403 input_free_device(inputdev);
404 return error;
405}
406
Ike Panhc8693ae82010-12-13 18:01:01 +0800407static void __devexit ideapad_input_exit(struct ideapad_private *priv)
Ike Panhcf63409a2010-12-13 18:00:38 +0800408{
Ike Panhc8693ae82010-12-13 18:01:01 +0800409 sparse_keymap_free(priv->inputdev);
410 input_unregister_device(priv->inputdev);
411 priv->inputdev = NULL;
Ike Panhcf63409a2010-12-13 18:00:38 +0800412}
413
Ike Panhc8693ae82010-12-13 18:01:01 +0800414static void ideapad_input_report(struct ideapad_private *priv,
415 unsigned long scancode)
Ike Panhcf63409a2010-12-13 18:00:38 +0800416{
Ike Panhc8693ae82010-12-13 18:01:01 +0800417 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
Ike Panhcf63409a2010-12-13 18:00:38 +0800418}
Ike Panhcf63409a2010-12-13 18:00:38 +0800419
Ike Panhca4b5a272010-12-13 18:00:48 +0800420/*
421 * module init/exit
422 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100423static const struct acpi_device_id ideapad_device_ids[] = {
424 { "VPC2004", 0},
425 { "", 0},
426};
427MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
428
Ike Panhca4b5a272010-12-13 18:00:48 +0800429static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100430{
Ike Panhc3371f482011-06-30 19:50:40 +0800431 int ret, i;
432 unsigned long cfg;
David Woodhousece326322010-08-11 17:59:35 +0100433 struct ideapad_private *priv;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100434
Ike Panhc3371f482011-06-30 19:50:40 +0800435 if (read_method_int(adevice->handle, "_CFG", (int *)&cfg))
Ike Panhc6f8371c2010-10-01 15:39:14 +0800436 return -ENODEV;
437
David Woodhousece326322010-08-11 17:59:35 +0100438 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
439 if (!priv)
440 return -ENOMEM;
Ike Panhcc9f718d2010-12-13 18:00:27 +0800441 dev_set_drvdata(&adevice->dev, priv);
Ike Panhcc1f73652010-12-13 18:01:12 +0800442 ideapad_handle = adevice->handle;
Ike Panhc3371f482011-06-30 19:50:40 +0800443 priv->cfg = cfg;
Ike Panhc98ee6912010-12-13 18:00:15 +0800444
Ike Panhc8693ae82010-12-13 18:01:01 +0800445 ret = ideapad_platform_init(priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800446 if (ret)
447 goto platform_failed;
David Woodhousece326322010-08-11 17:59:35 +0100448
Ike Panhc8693ae82010-12-13 18:01:01 +0800449 ret = ideapad_input_init(priv);
Ike Panhcf63409a2010-12-13 18:00:38 +0800450 if (ret)
451 goto input_failed;
452
Ike Panhcc1f73652010-12-13 18:01:12 +0800453 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
Ike Panhc3371f482011-06-30 19:50:40 +0800454 if (test_bit(ideapad_rfk_data[i].cfgbit, &cfg))
Ike Panhcc9f718d2010-12-13 18:00:27 +0800455 ideapad_register_rfkill(adevice, i);
Ike Panhcc1f73652010-12-13 18:01:12 +0800456 else
457 priv->rfk[i] = NULL;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100458 }
David Woodhousece326322010-08-11 17:59:35 +0100459 ideapad_sync_rfk_state(adevice);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800460
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100461 return 0;
Ike Panhc98ee6912010-12-13 18:00:15 +0800462
Ike Panhcf63409a2010-12-13 18:00:38 +0800463input_failed:
Ike Panhc8693ae82010-12-13 18:01:01 +0800464 ideapad_platform_exit(priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800465platform_failed:
466 kfree(priv);
467 return ret;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100468}
469
Ike Panhca4b5a272010-12-13 18:00:48 +0800470static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100471{
David Woodhousece326322010-08-11 17:59:35 +0100472 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100473 int i;
David Woodhousece326322010-08-11 17:59:35 +0100474
Ike Panhcc1f73652010-12-13 18:01:12 +0800475 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
David Woodhousece326322010-08-11 17:59:35 +0100476 ideapad_unregister_rfkill(adevice, i);
Ike Panhc8693ae82010-12-13 18:01:01 +0800477 ideapad_input_exit(priv);
478 ideapad_platform_exit(priv);
David Woodhousece326322010-08-11 17:59:35 +0100479 dev_set_drvdata(&adevice->dev, NULL);
480 kfree(priv);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800481
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100482 return 0;
483}
484
David Woodhousece326322010-08-11 17:59:35 +0100485static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100486{
Ike Panhc8693ae82010-12-13 18:01:01 +0800487 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
Ike Panhc8e7d3542010-10-01 15:39:05 +0800488 acpi_handle handle = adevice->handle;
489 unsigned long vpc1, vpc2, vpc_bit;
490
491 if (read_ec_data(handle, 0x10, &vpc1))
492 return;
493 if (read_ec_data(handle, 0x1A, &vpc2))
494 return;
495
496 vpc1 = (vpc2 << 8) | vpc1;
497 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
498 if (test_bit(vpc_bit, &vpc1)) {
499 if (vpc_bit == 9)
500 ideapad_sync_rfk_state(adevice);
Ike Panhc883ae792011-02-23 21:39:59 +0800501 else if (vpc_bit == 4)
502 read_ec_data(handle, 0x12, &vpc2);
Ike Panhcf63409a2010-12-13 18:00:38 +0800503 else
Ike Panhc8693ae82010-12-13 18:01:01 +0800504 ideapad_input_report(priv, vpc_bit);
Ike Panhc8e7d3542010-10-01 15:39:05 +0800505 }
506 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100507}
508
509static struct acpi_driver ideapad_acpi_driver = {
510 .name = "ideapad_acpi",
511 .class = "IdeaPad",
512 .ids = ideapad_device_ids,
513 .ops.add = ideapad_acpi_add,
514 .ops.remove = ideapad_acpi_remove,
515 .ops.notify = ideapad_acpi_notify,
516 .owner = THIS_MODULE,
517};
518
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100519static int __init ideapad_acpi_module_init(void)
520{
Ike Panhca4b5a272010-12-13 18:00:48 +0800521 return acpi_bus_register_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100522}
523
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100524static void __exit ideapad_acpi_module_exit(void)
525{
526 acpi_bus_unregister_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100527}
528
529MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
530MODULE_DESCRIPTION("IdeaPad ACPI Extras");
531MODULE_LICENSE("GPL");
532
533module_init(ideapad_acpi_module_init);
534module_exit(ideapad_acpi_module_exit);