2 * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/workqueue.h>
23 #include <acpi/acpi_drivers.h>
24 #include <linux/backlight.h>
25 #include <linux/input.h>
27 MODULE_LICENSE("GPL");
34 #define CMPC_ACCEL_SENSITIVITY_DEFAULT 5
37 #define CMPC_ACCEL_HID "ACCE0000"
38 #define CMPC_TABLET_HID "TBLT0000"
39 #define CMPC_BL_HID "IPML200"
40 #define CMPC_KEYS_HID "FnBT0000"
43 * Generic input device code.
46 typedef void (*input_device_init
)(struct input_dev
*dev
);
48 static int cmpc_add_acpi_notify_device(struct acpi_device
*acpi
, char *name
,
49 input_device_init idev_init
)
51 struct input_dev
*inputdev
;
54 inputdev
= input_allocate_device();
57 inputdev
->name
= name
;
58 inputdev
->dev
.parent
= &acpi
->dev
;
60 error
= input_register_device(inputdev
);
62 input_free_device(inputdev
);
65 dev_set_drvdata(&acpi
->dev
, inputdev
);
69 static int cmpc_remove_acpi_notify_device(struct acpi_device
*acpi
)
71 struct input_dev
*inputdev
= dev_get_drvdata(&acpi
->dev
);
72 input_unregister_device(inputdev
);
79 static acpi_status
cmpc_start_accel(acpi_handle handle
)
81 union acpi_object param
[2];
82 struct acpi_object_list input
;
85 param
[0].type
= ACPI_TYPE_INTEGER
;
86 param
[0].integer
.value
= 0x3;
87 param
[1].type
= ACPI_TYPE_INTEGER
;
89 input
.pointer
= param
;
90 status
= acpi_evaluate_object(handle
, "ACMD", &input
, NULL
);
94 static acpi_status
cmpc_stop_accel(acpi_handle handle
)
96 union acpi_object param
[2];
97 struct acpi_object_list input
;
100 param
[0].type
= ACPI_TYPE_INTEGER
;
101 param
[0].integer
.value
= 0x4;
102 param
[1].type
= ACPI_TYPE_INTEGER
;
104 input
.pointer
= param
;
105 status
= acpi_evaluate_object(handle
, "ACMD", &input
, NULL
);
109 static acpi_status
cmpc_accel_set_sensitivity(acpi_handle handle
, int val
)
111 union acpi_object param
[2];
112 struct acpi_object_list input
;
114 param
[0].type
= ACPI_TYPE_INTEGER
;
115 param
[0].integer
.value
= 0x02;
116 param
[1].type
= ACPI_TYPE_INTEGER
;
117 param
[1].integer
.value
= val
;
119 input
.pointer
= param
;
120 return acpi_evaluate_object(handle
, "ACMD", &input
, NULL
);
123 static acpi_status
cmpc_get_accel(acpi_handle handle
,
128 union acpi_object param
[2];
129 struct acpi_object_list input
;
130 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, 0 };
134 param
[0].type
= ACPI_TYPE_INTEGER
;
135 param
[0].integer
.value
= 0x01;
136 param
[1].type
= ACPI_TYPE_INTEGER
;
138 input
.pointer
= param
;
139 status
= acpi_evaluate_object(handle
, "ACMD", &input
, &output
);
140 if (ACPI_SUCCESS(status
)) {
141 union acpi_object
*obj
;
142 obj
= output
.pointer
;
143 locs
= obj
->buffer
.pointer
;
147 kfree(output
.pointer
);
152 static void cmpc_accel_handler(struct acpi_device
*dev
, u32 event
)
155 unsigned char x
, y
, z
;
158 status
= cmpc_get_accel(dev
->handle
, &x
, &y
, &z
);
159 if (ACPI_SUCCESS(status
)) {
160 struct input_dev
*inputdev
= dev_get_drvdata(&dev
->dev
);
162 input_report_abs(inputdev
, ABS_X
, x
);
163 input_report_abs(inputdev
, ABS_Y
, y
);
164 input_report_abs(inputdev
, ABS_Z
, z
);
165 input_sync(inputdev
);
170 static ssize_t
cmpc_accel_sensitivity_show(struct device
*dev
,
171 struct device_attribute
*attr
,
174 struct acpi_device
*acpi
;
175 struct input_dev
*inputdev
;
176 struct cmpc_accel
*accel
;
178 acpi
= to_acpi_device(dev
);
179 inputdev
= dev_get_drvdata(&acpi
->dev
);
180 accel
= dev_get_drvdata(&inputdev
->dev
);
182 return sprintf(buf
, "%d\n", accel
->sensitivity
);
185 static ssize_t
cmpc_accel_sensitivity_store(struct device
*dev
,
186 struct device_attribute
*attr
,
187 const char *buf
, size_t count
)
189 struct acpi_device
*acpi
;
190 struct input_dev
*inputdev
;
191 struct cmpc_accel
*accel
;
192 unsigned long sensitivity
;
195 acpi
= to_acpi_device(dev
);
196 inputdev
= dev_get_drvdata(&acpi
->dev
);
197 accel
= dev_get_drvdata(&inputdev
->dev
);
199 r
= strict_strtoul(buf
, 0, &sensitivity
);
203 accel
->sensitivity
= sensitivity
;
204 cmpc_accel_set_sensitivity(acpi
->handle
, sensitivity
);
206 return strnlen(buf
, count
);
209 struct device_attribute cmpc_accel_sensitivity_attr
= {
210 .attr
= { .name
= "sensitivity", .mode
= 0660 },
211 .show
= cmpc_accel_sensitivity_show
,
212 .store
= cmpc_accel_sensitivity_store
215 static int cmpc_accel_open(struct input_dev
*input
)
217 struct acpi_device
*acpi
;
219 acpi
= to_acpi_device(input
->dev
.parent
);
220 if (ACPI_SUCCESS(cmpc_start_accel(acpi
->handle
)))
225 static void cmpc_accel_close(struct input_dev
*input
)
227 struct acpi_device
*acpi
;
229 acpi
= to_acpi_device(input
->dev
.parent
);
230 cmpc_stop_accel(acpi
->handle
);
233 static void cmpc_accel_idev_init(struct input_dev
*inputdev
)
235 set_bit(EV_ABS
, inputdev
->evbit
);
236 input_set_abs_params(inputdev
, ABS_X
, 0, 255, 8, 0);
237 input_set_abs_params(inputdev
, ABS_Y
, 0, 255, 8, 0);
238 input_set_abs_params(inputdev
, ABS_Z
, 0, 255, 8, 0);
239 inputdev
->open
= cmpc_accel_open
;
240 inputdev
->close
= cmpc_accel_close
;
243 static int cmpc_accel_add(struct acpi_device
*acpi
)
246 struct input_dev
*inputdev
;
247 struct cmpc_accel
*accel
;
249 accel
= kmalloc(sizeof(*accel
), GFP_KERNEL
);
253 accel
->sensitivity
= CMPC_ACCEL_SENSITIVITY_DEFAULT
;
254 cmpc_accel_set_sensitivity(acpi
->handle
, accel
->sensitivity
);
256 error
= device_create_file(&acpi
->dev
, &cmpc_accel_sensitivity_attr
);
260 error
= cmpc_add_acpi_notify_device(acpi
, "cmpc_accel",
261 cmpc_accel_idev_init
);
265 inputdev
= dev_get_drvdata(&acpi
->dev
);
266 dev_set_drvdata(&inputdev
->dev
, accel
);
271 device_remove_file(&acpi
->dev
, &cmpc_accel_sensitivity_attr
);
277 static int cmpc_accel_remove(struct acpi_device
*acpi
, int type
)
279 struct input_dev
*inputdev
;
280 struct cmpc_accel
*accel
;
282 inputdev
= dev_get_drvdata(&acpi
->dev
);
283 accel
= dev_get_drvdata(&inputdev
->dev
);
285 device_remove_file(&acpi
->dev
, &cmpc_accel_sensitivity_attr
);
286 return cmpc_remove_acpi_notify_device(acpi
);
289 static const struct acpi_device_id cmpc_accel_device_ids
[] = {
294 static struct acpi_driver cmpc_accel_acpi_driver
= {
295 .owner
= THIS_MODULE
,
296 .name
= "cmpc_accel",
297 .class = "cmpc_accel",
298 .ids
= cmpc_accel_device_ids
,
300 .add
= cmpc_accel_add
,
301 .remove
= cmpc_accel_remove
,
302 .notify
= cmpc_accel_handler
,
310 static acpi_status
cmpc_get_tablet(acpi_handle handle
,
311 unsigned long long *value
)
313 union acpi_object param
;
314 struct acpi_object_list input
;
315 unsigned long long output
;
318 param
.type
= ACPI_TYPE_INTEGER
;
319 param
.integer
.value
= 0x01;
321 input
.pointer
= ¶m
;
322 status
= acpi_evaluate_integer(handle
, "TCMD", &input
, &output
);
323 if (ACPI_SUCCESS(status
))
328 static void cmpc_tablet_handler(struct acpi_device
*dev
, u32 event
)
330 unsigned long long val
= 0;
331 struct input_dev
*inputdev
= dev_get_drvdata(&dev
->dev
);
334 if (ACPI_SUCCESS(cmpc_get_tablet(dev
->handle
, &val
)))
335 input_report_switch(inputdev
, SW_TABLET_MODE
, !val
);
339 static void cmpc_tablet_idev_init(struct input_dev
*inputdev
)
341 unsigned long long val
= 0;
342 struct acpi_device
*acpi
;
344 set_bit(EV_SW
, inputdev
->evbit
);
345 set_bit(SW_TABLET_MODE
, inputdev
->swbit
);
347 acpi
= to_acpi_device(inputdev
->dev
.parent
);
348 if (ACPI_SUCCESS(cmpc_get_tablet(acpi
->handle
, &val
)))
349 input_report_switch(inputdev
, SW_TABLET_MODE
, !val
);
352 static int cmpc_tablet_add(struct acpi_device
*acpi
)
354 return cmpc_add_acpi_notify_device(acpi
, "cmpc_tablet",
355 cmpc_tablet_idev_init
);
358 static int cmpc_tablet_remove(struct acpi_device
*acpi
, int type
)
360 return cmpc_remove_acpi_notify_device(acpi
);
363 static int cmpc_tablet_resume(struct acpi_device
*acpi
)
365 struct input_dev
*inputdev
= dev_get_drvdata(&acpi
->dev
);
366 unsigned long long val
= 0;
367 if (ACPI_SUCCESS(cmpc_get_tablet(acpi
->handle
, &val
)))
368 input_report_switch(inputdev
, SW_TABLET_MODE
, !val
);
372 static const struct acpi_device_id cmpc_tablet_device_ids
[] = {
373 {CMPC_TABLET_HID
, 0},
377 static struct acpi_driver cmpc_tablet_acpi_driver
= {
378 .owner
= THIS_MODULE
,
379 .name
= "cmpc_tablet",
380 .class = "cmpc_tablet",
381 .ids
= cmpc_tablet_device_ids
,
383 .add
= cmpc_tablet_add
,
384 .remove
= cmpc_tablet_remove
,
385 .resume
= cmpc_tablet_resume
,
386 .notify
= cmpc_tablet_handler
,
395 static acpi_status
cmpc_get_brightness(acpi_handle handle
,
396 unsigned long long *value
)
398 union acpi_object param
;
399 struct acpi_object_list input
;
400 unsigned long long output
;
403 param
.type
= ACPI_TYPE_INTEGER
;
404 param
.integer
.value
= 0xC0;
406 input
.pointer
= ¶m
;
407 status
= acpi_evaluate_integer(handle
, "GRDI", &input
, &output
);
408 if (ACPI_SUCCESS(status
))
413 static acpi_status
cmpc_set_brightness(acpi_handle handle
,
414 unsigned long long value
)
416 union acpi_object param
[2];
417 struct acpi_object_list input
;
419 unsigned long long output
;
421 param
[0].type
= ACPI_TYPE_INTEGER
;
422 param
[0].integer
.value
= 0xC0;
423 param
[1].type
= ACPI_TYPE_INTEGER
;
424 param
[1].integer
.value
= value
;
426 input
.pointer
= param
;
427 status
= acpi_evaluate_integer(handle
, "GWRI", &input
, &output
);
431 static int cmpc_bl_get_brightness(struct backlight_device
*bd
)
435 unsigned long long brightness
;
437 handle
= bl_get_data(bd
);
438 status
= cmpc_get_brightness(handle
, &brightness
);
439 if (ACPI_SUCCESS(status
))
445 static int cmpc_bl_update_status(struct backlight_device
*bd
)
450 handle
= bl_get_data(bd
);
451 status
= cmpc_set_brightness(handle
, bd
->props
.brightness
);
452 if (ACPI_SUCCESS(status
))
458 static const struct backlight_ops cmpc_bl_ops
= {
459 .get_brightness
= cmpc_bl_get_brightness
,
460 .update_status
= cmpc_bl_update_status
463 static int cmpc_bl_add(struct acpi_device
*acpi
)
465 struct backlight_properties props
;
466 struct backlight_device
*bd
;
468 memset(&props
, 0, sizeof(struct backlight_properties
));
469 props
.max_brightness
= 7;
470 bd
= backlight_device_register("cmpc_bl", &acpi
->dev
, acpi
->handle
,
471 &cmpc_bl_ops
, &props
);
474 dev_set_drvdata(&acpi
->dev
, bd
);
478 static int cmpc_bl_remove(struct acpi_device
*acpi
, int type
)
480 struct backlight_device
*bd
;
482 bd
= dev_get_drvdata(&acpi
->dev
);
483 backlight_device_unregister(bd
);
487 static const struct acpi_device_id cmpc_bl_device_ids
[] = {
492 static struct acpi_driver cmpc_bl_acpi_driver
= {
493 .owner
= THIS_MODULE
,
496 .ids
= cmpc_bl_device_ids
,
499 .remove
= cmpc_bl_remove
507 static int cmpc_keys_codes
[] = {
521 static void cmpc_keys_handler(struct acpi_device
*dev
, u32 event
)
523 struct input_dev
*inputdev
;
526 if ((event
& 0x0F) < ARRAY_SIZE(cmpc_keys_codes
))
527 code
= cmpc_keys_codes
[event
& 0x0F];
528 inputdev
= dev_get_drvdata(&dev
->dev
);;
529 input_report_key(inputdev
, code
, !(event
& 0x10));
532 static void cmpc_keys_idev_init(struct input_dev
*inputdev
)
536 set_bit(EV_KEY
, inputdev
->evbit
);
537 for (i
= 0; cmpc_keys_codes
[i
] != KEY_MAX
; i
++)
538 set_bit(cmpc_keys_codes
[i
], inputdev
->keybit
);
541 static int cmpc_keys_add(struct acpi_device
*acpi
)
543 return cmpc_add_acpi_notify_device(acpi
, "cmpc_keys",
544 cmpc_keys_idev_init
);
547 static int cmpc_keys_remove(struct acpi_device
*acpi
, int type
)
549 return cmpc_remove_acpi_notify_device(acpi
);
552 static const struct acpi_device_id cmpc_keys_device_ids
[] = {
557 static struct acpi_driver cmpc_keys_acpi_driver
= {
558 .owner
= THIS_MODULE
,
560 .class = "cmpc_keys",
561 .ids
= cmpc_keys_device_ids
,
563 .add
= cmpc_keys_add
,
564 .remove
= cmpc_keys_remove
,
565 .notify
= cmpc_keys_handler
,
571 * General init/exit code.
574 static int cmpc_init(void)
578 r
= acpi_bus_register_driver(&cmpc_keys_acpi_driver
);
582 r
= acpi_bus_register_driver(&cmpc_bl_acpi_driver
);
586 r
= acpi_bus_register_driver(&cmpc_tablet_acpi_driver
);
590 r
= acpi_bus_register_driver(&cmpc_accel_acpi_driver
);
597 acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver
);
600 acpi_bus_unregister_driver(&cmpc_bl_acpi_driver
);
603 acpi_bus_unregister_driver(&cmpc_keys_acpi_driver
);
609 static void cmpc_exit(void)
611 acpi_bus_unregister_driver(&cmpc_accel_acpi_driver
);
612 acpi_bus_unregister_driver(&cmpc_tablet_acpi_driver
);
613 acpi_bus_unregister_driver(&cmpc_bl_acpi_driver
);
614 acpi_bus_unregister_driver(&cmpc_keys_acpi_driver
);
617 module_init(cmpc_init
);
618 module_exit(cmpc_exit
);
620 static const struct acpi_device_id cmpc_device_ids
[] = {
622 {CMPC_TABLET_HID
, 0},
628 MODULE_DEVICE_TABLE(acpi
, cmpc_device_ids
);