2 * ideapad-laptop.c - Lenovo IdeaPad ACPI Extras
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
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.
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.
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
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #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>
32 #include <linux/platform_device.h>
33 #include <linux/input.h>
34 #include <linux/input/sparse-keymap.h>
36 #define IDEAPAD_RFKILL_DEV_NUM (3)
38 #define CFG_BT_BIT (16)
39 #define CFG_3G_BIT (17)
40 #define CFG_WIFI_BIT (18)
41 #define CFG_CAMERA_BIT (19)
43 struct ideapad_private {
44 struct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
45 struct platform_device *platform_device;
46 struct input_dev *inputdev;
50 static acpi_handle ideapad_handle;
51 static bool no_bt_rfkill;
52 module_param(no_bt_rfkill, bool, 0444);
53 MODULE_PARM_DESC(no_bt_rfkill, "No rfkill for bluetooth.");
58 #define IDEAPAD_EC_TIMEOUT (100) /* in ms */
60 static int read_method_int(acpi_handle handle, const char *method, int *val)
63 unsigned long long result;
65 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
66 if (ACPI_FAILURE(status)) {
75 static int method_vpcr(acpi_handle handle, int cmd, int *ret)
78 unsigned long long result;
79 struct acpi_object_list params;
80 union acpi_object in_obj;
83 params.pointer = &in_obj;
84 in_obj.type = ACPI_TYPE_INTEGER;
85 in_obj.integer.value = cmd;
87 status = acpi_evaluate_integer(handle, "VPCR", ¶ms, &result);
89 if (ACPI_FAILURE(status)) {
98 static int method_vpcw(acpi_handle handle, int cmd, int data)
100 struct acpi_object_list params;
101 union acpi_object in_obj[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;
111 status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL);
117 static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data)
120 unsigned long int end_jiffies;
122 if (method_vpcw(handle, 1, cmd))
125 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
126 time_before(jiffies, end_jiffies);) {
128 if (method_vpcr(handle, 1, &val))
131 if (method_vpcr(handle, 0, &val))
137 pr_err("timeout in read_ec_cmd\n");
141 static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data)
144 unsigned long int end_jiffies;
146 if (method_vpcw(handle, 0, data))
148 if (method_vpcw(handle, 1, cmd))
151 for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1;
152 time_before(jiffies, end_jiffies);) {
154 if (method_vpcr(handle, 1, &val))
159 pr_err("timeout in write_ec_cmd\n");
166 static ssize_t show_ideapad_cam(struct device *dev,
167 struct device_attribute *attr,
170 unsigned long result;
172 if (read_ec_data(ideapad_handle, 0x1D, &result))
173 return sprintf(buf, "-1\n");
174 return sprintf(buf, "%lu\n", result);
177 static ssize_t store_ideapad_cam(struct device *dev,
178 struct device_attribute *attr,
179 const char *buf, size_t count)
185 if (sscanf(buf, "%i", &state) != 1)
187 ret = write_ec_cmd(ideapad_handle, 0x1E, state);
193 static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
195 static ssize_t show_ideapad_cfg(struct device *dev,
196 struct device_attribute *attr,
199 struct ideapad_private *priv = dev_get_drvdata(dev);
201 return sprintf(buf, "0x%.8lX\n", priv->cfg);
204 static DEVICE_ATTR(cfg, 0444, show_ideapad_cfg, NULL);
206 static struct attribute *ideapad_attributes[] = {
207 &dev_attr_camera_power.attr,
212 static mode_t ideapad_is_visible(struct kobject *kobj,
213 struct attribute *attr,
216 struct device *dev = container_of(kobj, struct device, kobj);
217 struct ideapad_private *priv = dev_get_drvdata(dev);
220 if (attr == &dev_attr_camera_power.attr)
221 supported = test_bit(CFG_CAMERA_BIT, &(priv->cfg));
225 return supported ? attr->mode : 0;
228 static struct attribute_group ideapad_attribute_group = {
229 .is_visible = ideapad_is_visible,
230 .attrs = ideapad_attributes
236 struct ideapad_rfk_data {
243 const struct ideapad_rfk_data ideapad_rfk_data[] = {
244 { "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 },
249 static int ideapad_rfk_set(void *data, bool blocked)
251 unsigned long opcode = (unsigned long)data;
253 return write_ec_cmd(ideapad_handle, opcode, !blocked);
256 static struct rfkill_ops ideapad_rfk_ops = {
257 .set_block = ideapad_rfk_set,
260 static void ideapad_sync_rfk_state(struct acpi_device *adevice)
262 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
263 unsigned long hw_blocked;
266 if (read_ec_data(ideapad_handle, 0x23, &hw_blocked))
268 hw_blocked = !hw_blocked;
270 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
272 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
275 static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
278 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
280 unsigned long sw_blocked;
283 (ideapad_rfk_data[dev].type == RFKILL_TYPE_BLUETOOTH)) {
284 /* Force to enable bluetooth when no_bt_rfkill=1 */
285 write_ec_cmd(ideapad_handle,
286 ideapad_rfk_data[dev].opcode, 1);
290 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
291 ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
296 if (read_ec_data(ideapad_handle, ideapad_rfk_data[dev].opcode-1,
298 rfkill_init_sw_state(priv->rfk[dev], 0);
300 sw_blocked = !sw_blocked;
301 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
304 ret = rfkill_register(priv->rfk[dev]);
306 rfkill_destroy(priv->rfk[dev]);
312 static void __devexit ideapad_unregister_rfkill(struct acpi_device *adevice,
315 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
320 rfkill_unregister(priv->rfk[dev]);
321 rfkill_destroy(priv->rfk[dev]);
327 static int __devinit ideapad_platform_init(struct ideapad_private *priv)
331 priv->platform_device = platform_device_alloc("ideapad", -1);
332 if (!priv->platform_device)
334 platform_set_drvdata(priv->platform_device, priv);
336 result = platform_device_add(priv->platform_device);
338 goto fail_platform_device;
340 result = sysfs_create_group(&priv->platform_device->dev.kobj,
341 &ideapad_attribute_group);
347 platform_device_del(priv->platform_device);
348 fail_platform_device:
349 platform_device_put(priv->platform_device);
353 static void ideapad_platform_exit(struct ideapad_private *priv)
355 sysfs_remove_group(&priv->platform_device->dev.kobj,
356 &ideapad_attribute_group);
357 platform_device_unregister(priv->platform_device);
363 static const struct key_entry ideapad_keymap[] = {
364 { KE_KEY, 0x06, { KEY_SWITCHVIDEOMODE } },
365 { KE_KEY, 0x0D, { KEY_WLAN } },
369 static int __devinit ideapad_input_init(struct ideapad_private *priv)
371 struct input_dev *inputdev;
374 inputdev = input_allocate_device();
376 pr_info("Unable to allocate input device\n");
380 inputdev->name = "Ideapad extra buttons";
381 inputdev->phys = "ideapad/input0";
382 inputdev->id.bustype = BUS_HOST;
383 inputdev->dev.parent = &priv->platform_device->dev;
385 error = sparse_keymap_setup(inputdev, ideapad_keymap, NULL);
387 pr_err("Unable to setup input device keymap\n");
391 error = input_register_device(inputdev);
393 pr_err("Unable to register input device\n");
394 goto err_free_keymap;
397 priv->inputdev = inputdev;
401 sparse_keymap_free(inputdev);
403 input_free_device(inputdev);
407 static void __devexit ideapad_input_exit(struct ideapad_private *priv)
409 sparse_keymap_free(priv->inputdev);
410 input_unregister_device(priv->inputdev);
411 priv->inputdev = NULL;
414 static void ideapad_input_report(struct ideapad_private *priv,
415 unsigned long scancode)
417 sparse_keymap_report_event(priv->inputdev, scancode, 1, true);
423 static const struct acpi_device_id ideapad_device_ids[] = {
427 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
429 static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
433 struct ideapad_private *priv;
435 if (read_method_int(adevice->handle, "_CFG", (int *)&cfg))
438 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
441 dev_set_drvdata(&adevice->dev, priv);
442 ideapad_handle = adevice->handle;
445 ret = ideapad_platform_init(priv);
447 goto platform_failed;
449 ret = ideapad_input_init(priv);
453 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++) {
454 if (test_bit(ideapad_rfk_data[i].cfgbit, &cfg))
455 ideapad_register_rfkill(adevice, i);
459 ideapad_sync_rfk_state(adevice);
464 ideapad_platform_exit(priv);
470 static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
472 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
475 for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
476 ideapad_unregister_rfkill(adevice, i);
477 ideapad_input_exit(priv);
478 ideapad_platform_exit(priv);
479 dev_set_drvdata(&adevice->dev, NULL);
485 static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
487 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
488 acpi_handle handle = adevice->handle;
489 unsigned long vpc1, vpc2, vpc_bit;
491 if (read_ec_data(handle, 0x10, &vpc1))
493 if (read_ec_data(handle, 0x1A, &vpc2))
496 vpc1 = (vpc2 << 8) | vpc1;
497 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
498 if (test_bit(vpc_bit, &vpc1)) {
500 ideapad_sync_rfk_state(adevice);
501 else if (vpc_bit == 4)
502 read_ec_data(handle, 0x12, &vpc2);
504 ideapad_input_report(priv, vpc_bit);
509 static struct acpi_driver ideapad_acpi_driver = {
510 .name = "ideapad_acpi",
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,
519 static int __init ideapad_acpi_module_init(void)
521 return acpi_bus_register_driver(&ideapad_acpi_driver);
524 static void __exit ideapad_acpi_module_exit(void)
526 acpi_bus_unregister_driver(&ideapad_acpi_driver);
529 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
530 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
531 MODULE_LICENSE("GPL");
533 module_init(ideapad_acpi_module_init);
534 module_exit(ideapad_acpi_module_exit);