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 struct ideapad_private
{
39 struct rfkill
*rfk
[IDEAPAD_RFKILL_DEV_NUM
];
40 struct platform_device
*platform_device
;
41 struct input_dev
*inputdev
;
44 static acpi_handle ideapad_handle
;
45 static bool no_bt_rfkill
;
46 module_param(no_bt_rfkill
, bool, 0444);
47 MODULE_PARM_DESC(no_bt_rfkill
, "No rfkill for bluetooth.");
52 #define IDEAPAD_EC_TIMEOUT (100) /* in ms */
54 static int read_method_int(acpi_handle handle
, const char *method
, int *val
)
57 unsigned long long result
;
59 status
= acpi_evaluate_integer(handle
, (char *)method
, NULL
, &result
);
60 if (ACPI_FAILURE(status
)) {
69 static int method_vpcr(acpi_handle handle
, int cmd
, int *ret
)
72 unsigned long long result
;
73 struct acpi_object_list params
;
74 union acpi_object in_obj
;
77 params
.pointer
= &in_obj
;
78 in_obj
.type
= ACPI_TYPE_INTEGER
;
79 in_obj
.integer
.value
= cmd
;
81 status
= acpi_evaluate_integer(handle
, "VPCR", ¶ms
, &result
);
83 if (ACPI_FAILURE(status
)) {
92 static int method_vpcw(acpi_handle handle
, int cmd
, int data
)
94 struct acpi_object_list params
;
95 union acpi_object in_obj
[2];
99 params
.pointer
= in_obj
;
100 in_obj
[0].type
= ACPI_TYPE_INTEGER
;
101 in_obj
[0].integer
.value
= cmd
;
102 in_obj
[1].type
= ACPI_TYPE_INTEGER
;
103 in_obj
[1].integer
.value
= data
;
105 status
= acpi_evaluate_object(handle
, "VPCW", ¶ms
, NULL
);
111 static int read_ec_data(acpi_handle handle
, int cmd
, unsigned long *data
)
114 unsigned long int end_jiffies
;
116 if (method_vpcw(handle
, 1, cmd
))
119 for (end_jiffies
= jiffies
+(HZ
)*IDEAPAD_EC_TIMEOUT
/1000+1;
120 time_before(jiffies
, end_jiffies
);) {
122 if (method_vpcr(handle
, 1, &val
))
125 if (method_vpcr(handle
, 0, &val
))
131 pr_err("timeout in read_ec_cmd\n");
135 static int write_ec_cmd(acpi_handle handle
, int cmd
, unsigned long data
)
138 unsigned long int end_jiffies
;
140 if (method_vpcw(handle
, 0, data
))
142 if (method_vpcw(handle
, 1, cmd
))
145 for (end_jiffies
= jiffies
+(HZ
)*IDEAPAD_EC_TIMEOUT
/1000+1;
146 time_before(jiffies
, end_jiffies
);) {
148 if (method_vpcr(handle
, 1, &val
))
153 pr_err("timeout in write_ec_cmd\n");
160 static ssize_t
show_ideapad_cam(struct device
*dev
,
161 struct device_attribute
*attr
,
164 unsigned long result
;
166 if (read_ec_data(ideapad_handle
, 0x1D, &result
))
167 return sprintf(buf
, "-1\n");
168 return sprintf(buf
, "%lu\n", result
);
171 static ssize_t
store_ideapad_cam(struct device
*dev
,
172 struct device_attribute
*attr
,
173 const char *buf
, size_t count
)
179 if (sscanf(buf
, "%i", &state
) != 1)
181 ret
= write_ec_cmd(ideapad_handle
, 0x1E, state
);
187 static DEVICE_ATTR(camera_power
, 0644, show_ideapad_cam
, store_ideapad_cam
);
192 struct ideapad_rfk_data
{
199 const struct ideapad_rfk_data ideapad_rfk_data
[] = {
200 { "ideapad_wlan", 18, 0x15, RFKILL_TYPE_WLAN
},
201 { "ideapad_bluetooth", 16, 0x17, RFKILL_TYPE_BLUETOOTH
},
202 { "ideapad_3g", 17, 0x20, RFKILL_TYPE_WWAN
},
205 static int ideapad_rfk_set(void *data
, bool blocked
)
207 unsigned long opcode
= (unsigned long)data
;
209 return write_ec_cmd(ideapad_handle
, opcode
, !blocked
);
212 static struct rfkill_ops ideapad_rfk_ops
= {
213 .set_block
= ideapad_rfk_set
,
216 static void ideapad_sync_rfk_state(struct acpi_device
*adevice
)
218 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
219 unsigned long hw_blocked
;
222 if (read_ec_data(ideapad_handle
, 0x23, &hw_blocked
))
224 hw_blocked
= !hw_blocked
;
226 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
228 rfkill_set_hw_state(priv
->rfk
[i
], hw_blocked
);
231 static int __devinit
ideapad_register_rfkill(struct acpi_device
*adevice
,
234 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
236 unsigned long sw_blocked
;
239 (ideapad_rfk_data
[dev
].type
== RFKILL_TYPE_BLUETOOTH
)) {
240 /* Force to enable bluetooth when no_bt_rfkill=1 */
241 write_ec_cmd(ideapad_handle
,
242 ideapad_rfk_data
[dev
].opcode
, 1);
246 priv
->rfk
[dev
] = rfkill_alloc(ideapad_rfk_data
[dev
].name
, &adevice
->dev
,
247 ideapad_rfk_data
[dev
].type
, &ideapad_rfk_ops
,
252 if (read_ec_data(ideapad_handle
, ideapad_rfk_data
[dev
].opcode
-1,
254 rfkill_init_sw_state(priv
->rfk
[dev
], 0);
256 sw_blocked
= !sw_blocked
;
257 rfkill_init_sw_state(priv
->rfk
[dev
], sw_blocked
);
260 ret
= rfkill_register(priv
->rfk
[dev
]);
262 rfkill_destroy(priv
->rfk
[dev
]);
268 static void __devexit
ideapad_unregister_rfkill(struct acpi_device
*adevice
,
271 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
276 rfkill_unregister(priv
->rfk
[dev
]);
277 rfkill_destroy(priv
->rfk
[dev
]);
283 static struct attribute
*ideapad_attributes
[] = {
284 &dev_attr_camera_power
.attr
,
288 static struct attribute_group ideapad_attribute_group
= {
289 .attrs
= ideapad_attributes
292 static int __devinit
ideapad_platform_init(struct ideapad_private
*priv
)
296 priv
->platform_device
= platform_device_alloc("ideapad", -1);
297 if (!priv
->platform_device
)
299 platform_set_drvdata(priv
->platform_device
, priv
);
301 result
= platform_device_add(priv
->platform_device
);
303 goto fail_platform_device
;
305 result
= sysfs_create_group(&priv
->platform_device
->dev
.kobj
,
306 &ideapad_attribute_group
);
312 platform_device_del(priv
->platform_device
);
313 fail_platform_device
:
314 platform_device_put(priv
->platform_device
);
318 static void ideapad_platform_exit(struct ideapad_private
*priv
)
320 sysfs_remove_group(&priv
->platform_device
->dev
.kobj
,
321 &ideapad_attribute_group
);
322 platform_device_unregister(priv
->platform_device
);
328 static const struct key_entry ideapad_keymap
[] = {
329 { KE_KEY
, 0x06, { KEY_SWITCHVIDEOMODE
} },
330 { KE_KEY
, 0x0D, { KEY_WLAN
} },
334 static int __devinit
ideapad_input_init(struct ideapad_private
*priv
)
336 struct input_dev
*inputdev
;
339 inputdev
= input_allocate_device();
341 pr_info("Unable to allocate input device\n");
345 inputdev
->name
= "Ideapad extra buttons";
346 inputdev
->phys
= "ideapad/input0";
347 inputdev
->id
.bustype
= BUS_HOST
;
348 inputdev
->dev
.parent
= &priv
->platform_device
->dev
;
350 error
= sparse_keymap_setup(inputdev
, ideapad_keymap
, NULL
);
352 pr_err("Unable to setup input device keymap\n");
356 error
= input_register_device(inputdev
);
358 pr_err("Unable to register input device\n");
359 goto err_free_keymap
;
362 priv
->inputdev
= inputdev
;
366 sparse_keymap_free(inputdev
);
368 input_free_device(inputdev
);
372 static void __devexit
ideapad_input_exit(struct ideapad_private
*priv
)
374 sparse_keymap_free(priv
->inputdev
);
375 input_unregister_device(priv
->inputdev
);
376 priv
->inputdev
= NULL
;
379 static void ideapad_input_report(struct ideapad_private
*priv
,
380 unsigned long scancode
)
382 sparse_keymap_report_event(priv
->inputdev
, scancode
, 1, true);
388 static const struct acpi_device_id ideapad_device_ids
[] = {
392 MODULE_DEVICE_TABLE(acpi
, ideapad_device_ids
);
394 static int __devinit
ideapad_acpi_add(struct acpi_device
*adevice
)
397 struct ideapad_private
*priv
;
399 if (read_method_int(adevice
->handle
, "_CFG", &cfg
))
402 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
405 dev_set_drvdata(&adevice
->dev
, priv
);
406 ideapad_handle
= adevice
->handle
;
408 ret
= ideapad_platform_init(priv
);
410 goto platform_failed
;
412 ret
= ideapad_input_init(priv
);
416 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++) {
417 if (test_bit(ideapad_rfk_data
[i
].cfgbit
, (unsigned long *)&cfg
))
418 ideapad_register_rfkill(adevice
, i
);
422 ideapad_sync_rfk_state(adevice
);
427 ideapad_platform_exit(priv
);
433 static int __devexit
ideapad_acpi_remove(struct acpi_device
*adevice
, int type
)
435 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
438 for (i
= 0; i
< IDEAPAD_RFKILL_DEV_NUM
; i
++)
439 ideapad_unregister_rfkill(adevice
, i
);
440 ideapad_input_exit(priv
);
441 ideapad_platform_exit(priv
);
442 dev_set_drvdata(&adevice
->dev
, NULL
);
448 static void ideapad_acpi_notify(struct acpi_device
*adevice
, u32 event
)
450 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
451 acpi_handle handle
= adevice
->handle
;
452 unsigned long vpc1
, vpc2
, vpc_bit
;
454 if (read_ec_data(handle
, 0x10, &vpc1
))
456 if (read_ec_data(handle
, 0x1A, &vpc2
))
459 vpc1
= (vpc2
<< 8) | vpc1
;
460 for (vpc_bit
= 0; vpc_bit
< 16; vpc_bit
++) {
461 if (test_bit(vpc_bit
, &vpc1
)) {
463 ideapad_sync_rfk_state(adevice
);
464 else if (vpc_bit
== 4)
465 read_ec_data(handle
, 0x12, &vpc2
);
467 ideapad_input_report(priv
, vpc_bit
);
472 static struct acpi_driver ideapad_acpi_driver
= {
473 .name
= "ideapad_acpi",
475 .ids
= ideapad_device_ids
,
476 .ops
.add
= ideapad_acpi_add
,
477 .ops
.remove
= ideapad_acpi_remove
,
478 .ops
.notify
= ideapad_acpi_notify
,
479 .owner
= THIS_MODULE
,
482 static int __init
ideapad_acpi_module_init(void)
484 return acpi_bus_register_driver(&ideapad_acpi_driver
);
487 static void __exit
ideapad_acpi_module_exit(void)
489 acpi_bus_unregister_driver(&ideapad_acpi_driver
);
492 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
493 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
494 MODULE_LICENSE("GPL");
496 module_init(ideapad_acpi_module_init
);
497 module_exit(ideapad_acpi_module_exit
);