blob: 3c6c5b5e1d531081aad4bf887d2931d578e819c4 [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
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/types.h>
27#include <acpi/acpi_bus.h>
28#include <acpi/acpi_drivers.h>
29#include <linux/rfkill.h>
Ike Panhc98ee6912010-12-13 18:00:15 +080030#include <linux/platform_device.h>
Ike Panhcf63409a2010-12-13 18:00:38 +080031#include <linux/input.h>
32#include <linux/input/sparse-keymap.h>
David Woodhouse58ac7aa2010-08-10 23:44:05 +010033
34#define IDEAPAD_DEV_CAMERA 0
35#define IDEAPAD_DEV_WLAN 1
36#define IDEAPAD_DEV_BLUETOOTH 2
37#define IDEAPAD_DEV_3G 3
38#define IDEAPAD_DEV_KILLSW 4
39
David Woodhousece326322010-08-11 17:59:35 +010040struct ideapad_private {
Ike Panhc26c81d52010-10-01 15:39:40 +080041 acpi_handle handle;
David Woodhousece326322010-08-11 17:59:35 +010042 struct rfkill *rfk[5];
Ike Panhc98ee6912010-12-13 18:00:15 +080043 struct platform_device *platform_device;
Ike Panhcf63409a2010-12-13 18:00:38 +080044 struct input_dev *inputdev;
Ike Panhcfa083592010-10-01 15:39:59 +080045} *ideapad_priv;
David Woodhousece326322010-08-11 17:59:35 +010046
47static struct {
48 char *name;
Ike Panhcdfa7f6f2010-10-01 15:39:29 +080049 int cfgbit;
Ike Panhcfa083592010-10-01 15:39:59 +080050 int opcode;
David Woodhousece326322010-08-11 17:59:35 +010051 int type;
52} ideapad_rfk_data[] = {
Ike Panhcfa083592010-10-01 15:39:59 +080053 { "ideapad_camera", 19, 0x1E, NUM_RFKILL_TYPES },
54 { "ideapad_wlan", 18, 0x15, RFKILL_TYPE_WLAN },
55 { "ideapad_bluetooth", 16, 0x17, RFKILL_TYPE_BLUETOOTH },
56 { "ideapad_3g", 17, 0x20, RFKILL_TYPE_WWAN },
57 { "ideapad_killsw", 0, 0, RFKILL_TYPE_WLAN }
David Woodhouse58ac7aa2010-08-10 23:44:05 +010058};
59
Ike Panhcbfa97b72010-10-01 15:40:22 +080060static bool no_bt_rfkill;
61module_param(no_bt_rfkill, bool, 0444);
62MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
63
Ike Panhc6a09f212010-10-01 15:38:46 +080064/*
65 * ACPI Helpers
66 */
67#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
68
69static int read_method_int(acpi_handle handle, const char *method, int *val)
70{
71 acpi_status status;
72 unsigned long long result;
73
74 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
75 if (ACPI_FAILURE(status)) {
76 *val = -1;
77 return -1;
78 } else {
79 *val = result;
80 return 0;
81 }
82}
83
84static int method_vpcr(acpi_handle handle, int cmd, int *ret)
85{
86 acpi_status status;
87 unsigned long long result;
88 struct acpi_object_list params;
89 union acpi_object in_obj;
90
91 params.count = 1;
92 params.pointer = &in_obj;
93 in_obj.type = ACPI_TYPE_INTEGER;
94 in_obj.integer.value = cmd;
95
96 status = acpi_evaluate_integer(handle, "VPCR", &params, &result);
97
98 if (ACPI_FAILURE(status)) {
99 *ret = -1;
100 return -1;
101 } else {
102 *ret = result;
103 return 0;
104 }
105}
106
107static int method_vpcw(acpi_handle handle, int cmd, int data)
108{
109 struct acpi_object_list params;
110 union acpi_object in_obj[2];
111 acpi_status status;
112
113 params.count = 2;
114 params.pointer = in_obj;
115 in_obj[0].type = ACPI_TYPE_INTEGER;
116 in_obj[0].integer.value = cmd;
117 in_obj[1].type = ACPI_TYPE_INTEGER;
118 in_obj[1].integer.value = data;
119
120 status = acpi_evaluate_object(handle, "VPCW", &params, NULL);
121 if (status != AE_OK)
122 return -1;
123 return 0;
124}
125
126static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
127{
128 int val;
129 unsigned long int end_jiffies;
130
131 if (method_vpcw(handle, 1, cmd))
132 return -1;
133
134 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
135 time_before(jiffies, end_jiffies);) {
136 schedule();
137 if (method_vpcr(handle, 1, &val))
138 return -1;
139 if (val == 0) {
140 if (method_vpcr(handle, 0, &val))
141 return -1;
142 *data = val;
143 return 0;
144 }
145 }
146 pr_err("timeout in read_ec_cmd\n");
147 return -1;
148}
149
150static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
151{
152 int val;
153 unsigned long int end_jiffies;
154
155 if (method_vpcw(handle, 0, data))
156 return -1;
157 if (method_vpcw(handle, 1, cmd))
158 return -1;
159
160 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
161 time_before(jiffies, end_jiffies);) {
162 schedule();
163 if (method_vpcr(handle, 1, &val))
164 return -1;
165 if (val == 0)
166 return 0;
167 }
168 pr_err("timeout in write_ec_cmd\n");
169 return -1;
170}
Ike Panhc6a09f212010-10-01 15:38:46 +0800171
Ike Panhca4b5a272010-12-13 18:00:48 +0800172/*
173 * camera power
174 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100175static ssize_t show_ideapad_cam(struct device *dev,
176 struct device_attribute *attr,
177 char *buf)
178{
Ike Panhc26c81d52010-10-01 15:39:40 +0800179 struct ideapad_private *priv = dev_get_drvdata(dev);
180 acpi_handle handle = priv->handle;
181 unsigned long result;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100182
Ike Panhc26c81d52010-10-01 15:39:40 +0800183 if (read_ec_data(handle, 0x1D, &result))
184 return sprintf(buf, "-1\n");
185 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100186}
187
188static ssize_t store_ideapad_cam(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t count)
191{
Ike Panhc26c81d52010-10-01 15:39:40 +0800192 struct ideapad_private *priv = dev_get_drvdata(dev);
193 acpi_handle handle = priv->handle;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100194 int ret, state;
195
196 if (!count)
197 return 0;
198 if (sscanf(buf, "%i", &state) != 1)
199 return -EINVAL;
Ike Panhc26c81d52010-10-01 15:39:40 +0800200 ret = write_ec_cmd(handle, 0x1E, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100201 if (ret < 0)
202 return ret;
203 return count;
204}
205
206static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
207
Ike Panhca4b5a272010-12-13 18:00:48 +0800208/*
209 * Rfkill
210 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100211static int ideapad_rfk_set(void *data, bool blocked)
212{
213 int device = (unsigned long)data;
214
215 if (device == IDEAPAD_DEV_KILLSW)
216 return -EINVAL;
Ike Panhcfa083592010-10-01 15:39:59 +0800217
218 return write_ec_cmd(ideapad_priv->handle,
219 ideapad_rfk_data[device].opcode,
220 !blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100221}
222
223static struct rfkill_ops ideapad_rfk_ops = {
224 .set_block = ideapad_rfk_set,
225};
226
David Woodhousece326322010-08-11 17:59:35 +0100227static void ideapad_sync_rfk_state(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100228{
David Woodhousece326322010-08-11 17:59:35 +0100229 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
Ike Panhc2b7266b2010-10-01 15:39:49 +0800230 acpi_handle handle = priv->handle;
231 unsigned long hw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100232 int i;
233
Ike Panhc2b7266b2010-10-01 15:39:49 +0800234 if (read_ec_data(handle, 0x23, &hw_blocked))
235 return;
236 hw_blocked = !hw_blocked;
237
238 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
David Woodhousece326322010-08-11 17:59:35 +0100239 if (priv->rfk[i])
240 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100241}
242
Ike Panhca4b5a272010-12-13 18:00:48 +0800243static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
244 int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100245{
David Woodhousece326322010-08-11 17:59:35 +0100246 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100247 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800248 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100249
Ike Panhcbfa97b72010-10-01 15:40:22 +0800250 if (no_bt_rfkill &&
251 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
252 /* Force to enable bluetooth when no_bt_rfkill=1 */
253 write_ec_cmd(ideapad_priv->handle,
254 ideapad_rfk_data[dev].opcode, 1);
255 return 0;
256 }
257
Ike Panhc2b7266b2010-10-01 15:39:49 +0800258 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
259 ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
David Woodhousece326322010-08-11 17:59:35 +0100260 (void *)(long)dev);
261 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100262 return -ENOMEM;
263
Ike Panhc2b7266b2010-10-01 15:39:49 +0800264 if (read_ec_data(ideapad_priv->handle, ideapad_rfk_data[dev].opcode-1,
265 &sw_blocked)) {
266 rfkill_init_sw_state(priv->rfk[dev], 0);
267 } else {
268 sw_blocked = !sw_blocked;
269 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
270 }
271
David Woodhousece326322010-08-11 17:59:35 +0100272 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100273 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100274 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100275 return ret;
276 }
277 return 0;
278}
279
Ike Panhca4b5a272010-12-13 18:00:48 +0800280static void __devexit ideapad_unregister_rfkill(struct acpi_device *adevice,
281 int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100282{
David Woodhousece326322010-08-11 17:59:35 +0100283 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
284
285 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100286 return;
287
David Woodhousece326322010-08-11 17:59:35 +0100288 rfkill_unregister(priv->rfk[dev]);
289 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100290}
291
Ike Panhc98ee6912010-12-13 18:00:15 +0800292/*
293 * Platform device
294 */
Ike Panhcc9f718d2010-12-13 18:00:27 +0800295static struct attribute *ideapad_attributes[] = {
296 &dev_attr_camera_power.attr,
297 NULL
298};
299
300static struct attribute_group ideapad_attribute_group = {
301 .attrs = ideapad_attributes
302};
303
Ike Panhc98ee6912010-12-13 18:00:15 +0800304static int __devinit ideapad_platform_init(void)
305{
306 int result;
307
308 ideapad_priv->platform_device = platform_device_alloc("ideapad", -1);
309 if (!ideapad_priv->platform_device)
310 return -ENOMEM;
311 platform_set_drvdata(ideapad_priv->platform_device, ideapad_priv);
312
313 result = platform_device_add(ideapad_priv->platform_device);
314 if (result)
315 goto fail_platform_device;
316
Ike Panhcc9f718d2010-12-13 18:00:27 +0800317 result = sysfs_create_group(&ideapad_priv->platform_device->dev.kobj,
318 &ideapad_attribute_group);
319 if (result)
320 goto fail_sysfs;
Ike Panhc98ee6912010-12-13 18:00:15 +0800321 return 0;
322
Ike Panhcc9f718d2010-12-13 18:00:27 +0800323fail_sysfs:
324 platform_device_del(ideapad_priv->platform_device);
Ike Panhc98ee6912010-12-13 18:00:15 +0800325fail_platform_device:
326 platform_device_put(ideapad_priv->platform_device);
327 return result;
328}
329
330static void ideapad_platform_exit(void)
331{
Ike Panhcc9f718d2010-12-13 18:00:27 +0800332 sysfs_remove_group(&ideapad_priv->platform_device->dev.kobj,
333 &ideapad_attribute_group);
Ike Panhc98ee6912010-12-13 18:00:15 +0800334 platform_device_unregister(ideapad_priv->platform_device);
335}
Ike Panhc98ee6912010-12-13 18:00:15 +0800336
Ike Panhcf63409a2010-12-13 18:00:38 +0800337/*
338 * input device
339 */
340static const struct key_entry ideapad_keymap[] = {
341 { KE_KEY, 0x06, { KEY_SWITCHVIDEOMODE } },
342 { KE_KEY, 0x0D, { KEY_WLAN } },
343 { KE_END, 0 },
344};
345
346static int __devinit ideapad_input_init(void)
347{
348 struct input_dev *inputdev;
349 int error;
350
351 inputdev = input_allocate_device();
352 if (!inputdev) {
353 pr_info("Unable to allocate input device\n");
354 return -ENOMEM;
355 }
356
357 inputdev->name = "Ideapad extra buttons";
358 inputdev->phys = "ideapad/input0";
359 inputdev->id.bustype = BUS_HOST;
360 inputdev->dev.parent = &ideapad_priv->platform_device->dev;
361
362 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
363 if (error) {
364 pr_err("Unable to setup input device keymap\n");
365 goto err_free_dev;
366 }
367
368 error = input_register_device(inputdev);
369 if (error) {
370 pr_err("Unable to register input device\n");
371 goto err_free_keymap;
372 }
373
374 ideapad_priv->inputdev = inputdev;
375 return 0;
376
377err_free_keymap:
378 sparse_keymap_free(inputdev);
379err_free_dev:
380 input_free_device(inputdev);
381 return error;
382}
383
384static void __devexit ideapad_input_exit(void)
385{
386 sparse_keymap_free(ideapad_priv->inputdev);
387 input_unregister_device(ideapad_priv->inputdev);
388 ideapad_priv->inputdev = NULL;
389}
390
391static void ideapad_input_report(unsigned long scancode)
392{
393 sparse_keymap_report_event(ideapad_priv->inputdev, scancode, 1, true);
394}
Ike Panhcf63409a2010-12-13 18:00:38 +0800395
Ike Panhca4b5a272010-12-13 18:00:48 +0800396/*
397 * module init/exit
398 */
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100399static const struct acpi_device_id ideapad_device_ids[] = {
400 { "VPC2004", 0},
401 { "", 0},
402};
403MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
404
Ike Panhca4b5a272010-12-13 18:00:48 +0800405static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100406{
Ike Panhc98ee6912010-12-13 18:00:15 +0800407 int ret, i, cfg;
David Woodhousece326322010-08-11 17:59:35 +0100408 struct ideapad_private *priv;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100409
Ike Panhc6f8371c2010-10-01 15:39:14 +0800410 if (read_method_int(adevice->handle, "_CFG", &cfg))
411 return -ENODEV;
412
David Woodhousece326322010-08-11 17:59:35 +0100413 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
414 if (!priv)
415 return -ENOMEM;
Ike Panhc98ee6912010-12-13 18:00:15 +0800416 ideapad_priv = priv;
Ike Panhcc9f718d2010-12-13 18:00:27 +0800417 priv->handle = adevice->handle;
418 dev_set_drvdata(&adevice->dev, priv);
Ike Panhc98ee6912010-12-13 18:00:15 +0800419
420 ret = ideapad_platform_init();
421 if (ret)
422 goto platform_failed;
David Woodhousece326322010-08-11 17:59:35 +0100423
Ike Panhcf63409a2010-12-13 18:00:38 +0800424 ret = ideapad_input_init();
425 if (ret)
426 goto input_failed;
427
Ike Panhcc9f718d2010-12-13 18:00:27 +0800428 for (i = IDEAPAD_DEV_WLAN; i < IDEAPAD_DEV_KILLSW; i++) {
429 if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg))
430 ideapad_register_rfkill(adevice, i);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100431 }
David Woodhousece326322010-08-11 17:59:35 +0100432 ideapad_sync_rfk_state(adevice);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800433
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100434 return 0;
Ike Panhc98ee6912010-12-13 18:00:15 +0800435
Ike Panhcf63409a2010-12-13 18:00:38 +0800436input_failed:
437 ideapad_platform_exit();
Ike Panhc98ee6912010-12-13 18:00:15 +0800438platform_failed:
439 kfree(priv);
440 return ret;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100441}
442
Ike Panhca4b5a272010-12-13 18:00:48 +0800443static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100444{
David Woodhousece326322010-08-11 17:59:35 +0100445 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100446 int i;
David Woodhousece326322010-08-11 17:59:35 +0100447
Ike Panhcc9f718d2010-12-13 18:00:27 +0800448 for (i = IDEAPAD_DEV_WLAN; i < IDEAPAD_DEV_KILLSW; i++)
David Woodhousece326322010-08-11 17:59:35 +0100449 ideapad_unregister_rfkill(adevice, i);
Ike Panhcf63409a2010-12-13 18:00:38 +0800450 ideapad_input_exit();
Ike Panhc98ee6912010-12-13 18:00:15 +0800451 ideapad_platform_exit();
David Woodhousece326322010-08-11 17:59:35 +0100452 dev_set_drvdata(&adevice->dev, NULL);
453 kfree(priv);
Ike Panhcc9f718d2010-12-13 18:00:27 +0800454
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100455 return 0;
456}
457
David Woodhousece326322010-08-11 17:59:35 +0100458static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100459{
Ike Panhc8e7d3542010-10-01 15:39:05 +0800460 acpi_handle handle = adevice->handle;
461 unsigned long vpc1, vpc2, vpc_bit;
462
463 if (read_ec_data(handle, 0x10, &vpc1))
464 return;
465 if (read_ec_data(handle, 0x1A, &vpc2))
466 return;
467
468 vpc1 = (vpc2 << 8) | vpc1;
469 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
470 if (test_bit(vpc_bit, &vpc1)) {
471 if (vpc_bit == 9)
472 ideapad_sync_rfk_state(adevice);
Ike Panhcf63409a2010-12-13 18:00:38 +0800473 else
474 ideapad_input_report(vpc_bit);
Ike Panhc8e7d3542010-10-01 15:39:05 +0800475 }
476 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100477}
478
479static struct acpi_driver ideapad_acpi_driver = {
480 .name = "ideapad_acpi",
481 .class = "IdeaPad",
482 .ids = ideapad_device_ids,
483 .ops.add = ideapad_acpi_add,
484 .ops.remove = ideapad_acpi_remove,
485 .ops.notify = ideapad_acpi_notify,
486 .owner = THIS_MODULE,
487};
488
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100489static int __init ideapad_acpi_module_init(void)
490{
Ike Panhca4b5a272010-12-13 18:00:48 +0800491 return acpi_bus_register_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100492}
493
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100494static void __exit ideapad_acpi_module_exit(void)
495{
496 acpi_bus_unregister_driver(&ideapad_acpi_driver);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100497}
498
499MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
500MODULE_DESCRIPTION("IdeaPad ACPI Extras");
501MODULE_LICENSE("GPL");
502
503module_init(ideapad_acpi_module_init);
504module_exit(ideapad_acpi_module_exit);