]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/misc/eeepc-laptop.c
eeepc-laptop: add backlight
[linux-2.6-omap-h63xx.git] / drivers / misc / eeepc-laptop.c
1 /*
2  *  eepc-laptop.c - Asus Eee PC extras
3  *
4  *  Based on asus_acpi.c as patched for the Eee PC by Asus:
5  *  ftp://ftp.asus.com/pub/ASUS/EeePC/701/ASUS_ACPI_071126.rar
6  *  Based on eee.c from eeepc-linux
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/types.h>
23 #include <linux/platform_device.h>
24 #include <linux/backlight.h>
25 #include <linux/fb.h>
26 #include <acpi/acpi_drivers.h>
27 #include <acpi/acpi_bus.h>
28 #include <linux/uaccess.h>
29
30 #define EEEPC_LAPTOP_VERSION    "0.1"
31
32 #define EEEPC_HOTK_NAME         "Eee PC Hotkey Driver"
33 #define EEEPC_HOTK_FILE         "eeepc"
34 #define EEEPC_HOTK_CLASS        "hotkey"
35 #define EEEPC_HOTK_DEVICE_NAME  "Hotkey"
36 #define EEEPC_HOTK_HID          "ASUS010"
37
38 #define EEEPC_LOG       EEEPC_HOTK_FILE ": "
39 #define EEEPC_ERR       KERN_ERR        EEEPC_LOG
40 #define EEEPC_WARNING   KERN_WARNING    EEEPC_LOG
41 #define EEEPC_NOTICE    KERN_NOTICE     EEEPC_LOG
42 #define EEEPC_INFO      KERN_INFO       EEEPC_LOG
43
44 /*
45  * Definitions for Asus EeePC
46  */
47 #define NOTIFY_WLAN_ON  0x10
48 #define NOTIFY_BRN_MIN  0x20
49 #define NOTIFY_BRN_MAX  0x2f
50
51 enum {
52         DISABLE_ASL_WLAN = 0x0001,
53         DISABLE_ASL_BLUETOOTH = 0x0002,
54         DISABLE_ASL_IRDA = 0x0004,
55         DISABLE_ASL_CAMERA = 0x0008,
56         DISABLE_ASL_TV = 0x0010,
57         DISABLE_ASL_GPS = 0x0020,
58         DISABLE_ASL_DISPLAYSWITCH = 0x0040,
59         DISABLE_ASL_MODEM = 0x0080,
60         DISABLE_ASL_CARDREADER = 0x0100
61 };
62
63 enum {
64         CM_ASL_WLAN = 0,
65         CM_ASL_BLUETOOTH,
66         CM_ASL_IRDA,
67         CM_ASL_1394,
68         CM_ASL_CAMERA,
69         CM_ASL_TV,
70         CM_ASL_GPS,
71         CM_ASL_DVDROM,
72         CM_ASL_DISPLAYSWITCH,
73         CM_ASL_PANELBRIGHT,
74         CM_ASL_BIOSFLASH,
75         CM_ASL_ACPIFLASH,
76         CM_ASL_CPUFV,
77         CM_ASL_CPUTEMPERATURE,
78         CM_ASL_FANCPU,
79         CM_ASL_FANCHASSIS,
80         CM_ASL_USBPORT1,
81         CM_ASL_USBPORT2,
82         CM_ASL_USBPORT3,
83         CM_ASL_MODEM,
84         CM_ASL_CARDREADER,
85         CM_ASL_LID
86 };
87
88 const char *cm_getv[] = {
89         "WLDG", NULL, NULL, NULL,
90         "CAMG", NULL, NULL, NULL,
91         NULL, "PBLG", NULL, NULL,
92         "CFVG", NULL, NULL, NULL,
93         "USBG", NULL, NULL, "MODG",
94         "CRDG", "LIDG"
95 };
96
97 const char *cm_setv[] = {
98         "WLDS", NULL, NULL, NULL,
99         "CAMS", NULL, NULL, NULL,
100         "SDSP", "PBLS", "HDPS", NULL,
101         "CFVS", NULL, NULL, NULL,
102         "USBG", NULL, NULL, "MODS",
103         "CRDS", NULL
104 };
105
106 /*
107  * This is the main structure, we can use it to store useful information
108  * about the hotk device
109  */
110 struct eeepc_hotk {
111         struct acpi_device *device;     /* the device we are in */
112         acpi_handle handle;             /* the handle of the hotk device */
113         u32 cm_supported;               /* the control methods supported
114                                            by this BIOS */
115         uint init_flag;                 /* Init flags */
116         u16 event_count[128];           /* count for each event */
117 };
118
119 /* The actual device the driver binds to */
120 static struct eeepc_hotk *ehotk;
121
122 /* Platform device/driver */
123 static struct platform_driver platform_driver = {
124         .driver = {
125                 .name = EEEPC_HOTK_FILE,
126                 .owner = THIS_MODULE,
127         }
128 };
129
130 static struct platform_device *platform_device;
131
132 /*
133  * The hotkey driver declaration
134  */
135 static int eeepc_hotk_add(struct acpi_device *device);
136 static int eeepc_hotk_remove(struct acpi_device *device, int type);
137
138 static const struct acpi_device_id eeepc_device_ids[] = {
139         {EEEPC_HOTK_HID, 0},
140         {"", 0},
141 };
142 MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
143
144 static struct acpi_driver eeepc_hotk_driver = {
145         .name = EEEPC_HOTK_NAME,
146         .class = EEEPC_HOTK_CLASS,
147         .ids = eeepc_device_ids,
148         .ops = {
149                 .add = eeepc_hotk_add,
150                 .remove = eeepc_hotk_remove,
151         },
152 };
153
154 /* The backlight device /sys/class/backlight */
155 static struct backlight_device *eeepc_backlight_device;
156
157 /*
158  * The backlight class declaration
159  */
160 static int read_brightness(struct backlight_device *bd);
161 static int update_bl_status(struct backlight_device *bd);
162 static struct backlight_ops eeepcbl_ops = {
163         .get_brightness = read_brightness,
164         .update_status = update_bl_status,
165 };
166
167 MODULE_AUTHOR("Corentin Chary, Eric Cooper");
168 MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
169 MODULE_LICENSE("GPL");
170
171 /*
172  * ACPI Helpers
173  */
174 static int write_acpi_int(acpi_handle handle, const char *method, int val,
175                           struct acpi_buffer *output)
176 {
177         struct acpi_object_list params;
178         union acpi_object in_obj;
179         acpi_status status;
180
181         params.count = 1;
182         params.pointer = &in_obj;
183         in_obj.type = ACPI_TYPE_INTEGER;
184         in_obj.integer.value = val;
185
186         status = acpi_evaluate_object(handle, (char *)method, &params, output);
187         return (status == AE_OK ? 0 : -1);
188 }
189
190 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
191 {
192         acpi_status status;
193         ulong result;
194
195         status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
196         if (ACPI_FAILURE(status)) {
197                 *val = -1;
198                 return -1;
199         } else {
200                 *val = result;
201                 return 0;
202         }
203 }
204
205 static int set_acpi(int cm, int value)
206 {
207         if (ehotk->cm_supported & (0x1 << cm)) {
208                 const char *method = cm_setv[cm];
209                 if (method == NULL)
210                         return -ENODEV;
211                 if (write_acpi_int(ehotk->handle, method, value, NULL))
212                         printk(EEEPC_WARNING "Error writing %s\n", method);
213         }
214         return 0;
215 }
216
217 static int get_acpi(int cm)
218 {
219         int value = -1;
220         if ((ehotk->cm_supported & (0x1 << cm))) {
221                 const char *method = cm_getv[cm];
222                 if (method == NULL)
223                         return -ENODEV;
224                 if (read_acpi_int(ehotk->handle, method, &value))
225                         printk(EEEPC_WARNING "Error reading %s\n", method);
226         }
227         return value;
228 }
229
230 /*
231  * Backlight
232  */
233 static int read_brightness(struct backlight_device *bd)
234 {
235         return get_acpi(CM_ASL_PANELBRIGHT);
236 }
237
238 static int set_brightness(struct backlight_device *bd, int value)
239 {
240         value = max(0, min(15, value));
241         return set_acpi(CM_ASL_PANELBRIGHT, value);
242 }
243
244 static int update_bl_status(struct backlight_device *bd)
245 {
246         return set_brightness(bd, bd->props.brightness);
247 }
248
249 /*
250  * Sys helpers
251  */
252 static int parse_arg(const char *buf, unsigned long count, int *val)
253 {
254         if (!count)
255                 return 0;
256         if (sscanf(buf, "%i", val) != 1)
257                 return -EINVAL;
258         return count;
259 }
260
261 static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
262 {
263         int rv, value;
264
265         rv = parse_arg(buf, count, &value);
266         if (rv > 0)
267                 set_acpi(cm, value);
268         return rv;
269 }
270
271 static ssize_t show_sys_acpi(int cm, char *buf)
272 {
273         return sprintf(buf, "%d\n", get_acpi(cm));
274 }
275
276 #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm)                            \
277         static ssize_t show_##_name(struct device *dev,                 \
278                                     struct device_attribute *attr,      \
279                                     char *buf)                          \
280         {                                                               \
281                 return show_sys_acpi(_cm, buf);                         \
282         }                                                               \
283         static ssize_t store_##_name(struct device *dev,                \
284                                      struct device_attribute *attr,     \
285                                      const char *buf, size_t count)     \
286         {                                                               \
287                 return store_sys_acpi(_cm, buf, count);                 \
288         }                                                               \
289         static struct device_attribute dev_attr_##_name = {             \
290                 .attr = {                                               \
291                         .name = __stringify(_name),                     \
292                         .mode = 0644 },                                 \
293                 .show   = show_##_name,                                 \
294                 .store  = store_##_name,                                \
295         }
296
297 EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
298 EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
299 EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
300 EEEPC_CREATE_DEVICE_ATTR(wlan, CM_ASL_WLAN);
301
302 static struct attribute *platform_attributes[] = {
303         &dev_attr_camera.attr,
304         &dev_attr_cardr.attr,
305         &dev_attr_disp.attr,
306         &dev_attr_wlan.attr,
307         NULL
308 };
309
310 static struct attribute_group platform_attribute_group = {
311         .attrs = platform_attributes
312 };
313
314 /*
315  * Hotkey functions
316  */
317 static int eeepc_hotk_check(void)
318 {
319         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
320         int result;
321
322         result = acpi_bus_get_status(ehotk->device);
323         if (result)
324                 return result;
325         if (ehotk->device->status.present) {
326                 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
327                                     &buffer)) {
328                         printk(EEEPC_ERR "Hotkey initialization failed\n");
329                         return -ENODEV;
330                 } else {
331                         printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
332                                ehotk->init_flag);
333                 }
334                 /* get control methods supported */
335                 if (read_acpi_int(ehotk->handle, "CMSG"
336                                    , &ehotk->cm_supported)) {
337                         printk(EEEPC_ERR
338                                "Get control methods supported failed\n");
339                         return -ENODEV;
340                 } else {
341                         printk(EEEPC_INFO
342                                "Get control methods supported: 0x%x\n",
343                                ehotk->cm_supported);
344                 }
345         } else {
346                 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
347                 return -EINVAL;
348         }
349         return 0;
350 }
351
352 static void notify_wlan(u32 *event)
353 {
354         /* if DISABLE_ASL_WLAN is set, the notify code for fn+f2
355            will always be 0x10 */
356         if (ehotk->cm_supported & (0x1 << CM_ASL_WLAN)) {
357                 const char *method = cm_getv[CM_ASL_WLAN];
358                 int value;
359                 if (read_acpi_int(ehotk->handle, method, &value))
360                         printk(EEEPC_WARNING "Error reading %s\n",
361                                method);
362                 else if (value == 1)
363                         *event = 0x11;
364         }
365 }
366
367 static void notify_brn(void)
368 {
369         struct backlight_device *bd = eeepc_backlight_device;
370         bd->props.brightness = read_brightness(bd);
371 }
372
373 static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
374 {
375         if (!ehotk)
376                 return;
377         if (event == NOTIFY_WLAN_ON && (DISABLE_ASL_WLAN & ehotk->init_flag))
378                 notify_wlan(&event);
379         if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
380                 notify_brn();
381         acpi_bus_generate_proc_event(ehotk->device, event,
382                                      ehotk->event_count[event % 128]++);
383 }
384
385 static int eeepc_hotk_add(struct acpi_device *device)
386 {
387         acpi_status status = AE_OK;
388         int result;
389
390         if (!device)
391                  return -EINVAL;
392         printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
393         ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
394         if (!ehotk)
395                 return -ENOMEM;
396         ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
397         ehotk->handle = device->handle;
398         strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
399         strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
400         acpi_driver_data(device) = ehotk;
401         ehotk->device = device;
402         result = eeepc_hotk_check();
403         if (result)
404                 goto end;
405         status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
406                                              eeepc_hotk_notify, ehotk);
407         if (ACPI_FAILURE(status))
408                 printk(EEEPC_ERR "Error installing notify handler\n");
409  end:
410         if (result) {
411                 kfree(ehotk);
412                 ehotk = NULL;
413         }
414         return result;
415 }
416
417 static int eeepc_hotk_remove(struct acpi_device *device, int type)
418 {
419         acpi_status status = 0;
420
421         if (!device || !acpi_driver_data(device))
422                  return -EINVAL;
423         status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
424                                             eeepc_hotk_notify);
425         if (ACPI_FAILURE(status))
426                 printk(EEEPC_ERR "Error removing notify handler\n");
427         kfree(ehotk);
428         return 0;
429 }
430
431 /*
432  * exit/init
433  */
434 static void eeepc_backlight_exit(void)
435 {
436         if (eeepc_backlight_device)
437                 backlight_device_unregister(eeepc_backlight_device);
438         eeepc_backlight_device = NULL;
439 }
440
441 static void __exit eeepc_laptop_exit(void)
442 {
443         eeepc_backlight_exit();
444         acpi_bus_unregister_driver(&eeepc_hotk_driver);
445         sysfs_remove_group(&platform_device->dev.kobj,
446                            &platform_attribute_group);
447         platform_device_unregister(platform_device);
448         platform_driver_unregister(&platform_driver);
449 }
450
451 static int eeepc_backlight_init(struct device *dev)
452 {
453         struct backlight_device *bd;
454
455         bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
456                                        NULL, &eeepcbl_ops);
457         if (IS_ERR(bd)) {
458                 printk(EEEPC_ERR
459                        "Could not register eeepc backlight device\n");
460                 eeepc_backlight_device = NULL;
461                 return PTR_ERR(bd);
462         }
463         eeepc_backlight_device = bd;
464         bd->props.max_brightness = 15;
465         bd->props.brightness = read_brightness(NULL);
466         bd->props.power = FB_BLANK_UNBLANK;
467         backlight_update_status(bd);
468         return 0;
469 }
470
471 static int __init eeepc_laptop_init(void)
472 {
473         struct device *dev;
474         int result;
475
476         if (acpi_disabled)
477                 return -ENODEV;
478         result = acpi_bus_register_driver(&eeepc_hotk_driver);
479         if (result < 0)
480                 return result;
481         if (!ehotk) {
482                 acpi_bus_unregister_driver(&eeepc_hotk_driver);
483                 return -ENODEV;
484         }
485         dev = acpi_get_physical_device(ehotk->device->handle);
486         result = eeepc_backlight_init(dev);
487         if (result)
488                 goto fail_backlight;
489         /* Register platform stuff */
490         result = platform_driver_register(&platform_driver);
491         if (result)
492                 goto fail_platform_driver;
493         platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
494         if (!platform_device) {
495                 result = -ENOMEM;
496                 goto fail_platform_device1;
497         }
498         result = platform_device_add(platform_device);
499         if (result)
500                 goto fail_platform_device2;
501         result = sysfs_create_group(&platform_device->dev.kobj,
502                                     &platform_attribute_group);
503         if (result)
504                 goto fail_sysfs;
505         return 0;
506 fail_sysfs:
507         platform_device_del(platform_device);
508 fail_platform_device2:
509         platform_device_put(platform_device);
510 fail_platform_device1:
511         platform_driver_unregister(&platform_driver);
512 fail_platform_driver:
513         eeepc_backlight_exit();
514 fail_backlight:
515         return result;
516 }
517
518 module_init(eeepc_laptop_init);
519 module_exit(eeepc_laptop_exit);