]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/platform/x86/eeepc-laptop.c
ACPI: Kconfig text - Fix the ACPI_CONTAINER module name according to the real module...
[linux-2.6-omap-h63xx.git] / drivers / platform / x86 / 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 <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <acpi/acpi_drivers.h>
29 #include <acpi/acpi_bus.h>
30 #include <linux/uaccess.h>
31 #include <linux/input.h>
32 #include <linux/rfkill.h>
33
34 #define EEEPC_LAPTOP_VERSION    "0.1"
35
36 #define EEEPC_HOTK_NAME         "Eee PC Hotkey Driver"
37 #define EEEPC_HOTK_FILE         "eeepc"
38 #define EEEPC_HOTK_CLASS        "hotkey"
39 #define EEEPC_HOTK_DEVICE_NAME  "Hotkey"
40 #define EEEPC_HOTK_HID          "ASUS010"
41
42 #define EEEPC_LOG       EEEPC_HOTK_FILE ": "
43 #define EEEPC_ERR       KERN_ERR        EEEPC_LOG
44 #define EEEPC_WARNING   KERN_WARNING    EEEPC_LOG
45 #define EEEPC_NOTICE    KERN_NOTICE     EEEPC_LOG
46 #define EEEPC_INFO      KERN_INFO       EEEPC_LOG
47
48 /*
49  * Definitions for Asus EeePC
50  */
51 #define NOTIFY_WLAN_ON  0x10
52 #define NOTIFY_BRN_MIN  0x20
53 #define NOTIFY_BRN_MAX  0x2f
54
55 enum {
56         DISABLE_ASL_WLAN = 0x0001,
57         DISABLE_ASL_BLUETOOTH = 0x0002,
58         DISABLE_ASL_IRDA = 0x0004,
59         DISABLE_ASL_CAMERA = 0x0008,
60         DISABLE_ASL_TV = 0x0010,
61         DISABLE_ASL_GPS = 0x0020,
62         DISABLE_ASL_DISPLAYSWITCH = 0x0040,
63         DISABLE_ASL_MODEM = 0x0080,
64         DISABLE_ASL_CARDREADER = 0x0100
65 };
66
67 enum {
68         CM_ASL_WLAN = 0,
69         CM_ASL_BLUETOOTH,
70         CM_ASL_IRDA,
71         CM_ASL_1394,
72         CM_ASL_CAMERA,
73         CM_ASL_TV,
74         CM_ASL_GPS,
75         CM_ASL_DVDROM,
76         CM_ASL_DISPLAYSWITCH,
77         CM_ASL_PANELBRIGHT,
78         CM_ASL_BIOSFLASH,
79         CM_ASL_ACPIFLASH,
80         CM_ASL_CPUFV,
81         CM_ASL_CPUTEMPERATURE,
82         CM_ASL_FANCPU,
83         CM_ASL_FANCHASSIS,
84         CM_ASL_USBPORT1,
85         CM_ASL_USBPORT2,
86         CM_ASL_USBPORT3,
87         CM_ASL_MODEM,
88         CM_ASL_CARDREADER,
89         CM_ASL_LID
90 };
91
92 static const char *cm_getv[] = {
93         "WLDG", "BTHG", NULL, NULL,
94         "CAMG", NULL, NULL, NULL,
95         NULL, "PBLG", NULL, NULL,
96         "CFVG", NULL, NULL, NULL,
97         "USBG", NULL, NULL, "MODG",
98         "CRDG", "LIDG"
99 };
100
101 static const char *cm_setv[] = {
102         "WLDS", "BTHS", NULL, NULL,
103         "CAMS", NULL, NULL, NULL,
104         "SDSP", "PBLS", "HDPS", NULL,
105         "CFVS", NULL, NULL, NULL,
106         "USBG", NULL, NULL, "MODS",
107         "CRDS", NULL
108 };
109
110 #define EEEPC_EC        "\\_SB.PCI0.SBRG.EC0."
111
112 #define EEEPC_EC_FAN_PWM        EEEPC_EC "SC02" /* Fan PWM duty cycle (%) */
113 #define EEEPC_EC_SC02           0x63
114 #define EEEPC_EC_FAN_HRPM       EEEPC_EC "SC05" /* High byte, fan speed (RPM) */
115 #define EEEPC_EC_FAN_LRPM       EEEPC_EC "SC06" /* Low byte, fan speed (RPM) */
116 #define EEEPC_EC_FAN_CTRL       EEEPC_EC "SFB3" /* Byte containing SF25  */
117 #define EEEPC_EC_SFB3           0xD3
118
119 /*
120  * This is the main structure, we can use it to store useful information
121  * about the hotk device
122  */
123 struct eeepc_hotk {
124         struct acpi_device *device;     /* the device we are in */
125         acpi_handle handle;             /* the handle of the hotk device */
126         u32 cm_supported;               /* the control methods supported
127                                            by this BIOS */
128         uint init_flag;                 /* Init flags */
129         u16 event_count[128];           /* count for each event */
130         struct input_dev *inputdev;
131         u16 *keycode_map;
132         struct rfkill *eeepc_wlan_rfkill;
133         struct rfkill *eeepc_bluetooth_rfkill;
134 };
135
136 /* The actual device the driver binds to */
137 static struct eeepc_hotk *ehotk;
138
139 /* Platform device/driver */
140 static struct platform_driver platform_driver = {
141         .driver = {
142                 .name = EEEPC_HOTK_FILE,
143                 .owner = THIS_MODULE,
144         }
145 };
146
147 static struct platform_device *platform_device;
148
149 struct key_entry {
150         char type;
151         u8 code;
152         u16 keycode;
153 };
154
155 enum { KE_KEY, KE_END };
156
157 static struct key_entry eeepc_keymap[] = {
158         /* Sleep already handled via generic ACPI code */
159         {KE_KEY, 0x10, KEY_WLAN },
160         {KE_KEY, 0x12, KEY_PROG1 },
161         {KE_KEY, 0x13, KEY_MUTE },
162         {KE_KEY, 0x14, KEY_VOLUMEDOWN },
163         {KE_KEY, 0x15, KEY_VOLUMEUP },
164         {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
165         {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
166         {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
167         {KE_END, 0},
168 };
169
170 /*
171  * The hotkey driver declaration
172  */
173 static int eeepc_hotk_add(struct acpi_device *device);
174 static int eeepc_hotk_remove(struct acpi_device *device, int type);
175
176 static const struct acpi_device_id eeepc_device_ids[] = {
177         {EEEPC_HOTK_HID, 0},
178         {"", 0},
179 };
180 MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
181
182 static struct acpi_driver eeepc_hotk_driver = {
183         .name = EEEPC_HOTK_NAME,
184         .class = EEEPC_HOTK_CLASS,
185         .ids = eeepc_device_ids,
186         .ops = {
187                 .add = eeepc_hotk_add,
188                 .remove = eeepc_hotk_remove,
189         },
190 };
191
192 /* The backlight device /sys/class/backlight */
193 static struct backlight_device *eeepc_backlight_device;
194
195 /* The hwmon device */
196 static struct device *eeepc_hwmon_device;
197
198 /*
199  * The backlight class declaration
200  */
201 static int read_brightness(struct backlight_device *bd);
202 static int update_bl_status(struct backlight_device *bd);
203 static struct backlight_ops eeepcbl_ops = {
204         .get_brightness = read_brightness,
205         .update_status = update_bl_status,
206 };
207
208 MODULE_AUTHOR("Corentin Chary, Eric Cooper");
209 MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
210 MODULE_LICENSE("GPL");
211
212 /*
213  * ACPI Helpers
214  */
215 static int write_acpi_int(acpi_handle handle, const char *method, int val,
216                           struct acpi_buffer *output)
217 {
218         struct acpi_object_list params;
219         union acpi_object in_obj;
220         acpi_status status;
221
222         params.count = 1;
223         params.pointer = &in_obj;
224         in_obj.type = ACPI_TYPE_INTEGER;
225         in_obj.integer.value = val;
226
227         status = acpi_evaluate_object(handle, (char *)method, &params, output);
228         return (status == AE_OK ? 0 : -1);
229 }
230
231 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
232 {
233         acpi_status status;
234         unsigned long long result;
235
236         status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
237         if (ACPI_FAILURE(status)) {
238                 *val = -1;
239                 return -1;
240         } else {
241                 *val = result;
242                 return 0;
243         }
244 }
245
246 static int set_acpi(int cm, int value)
247 {
248         if (ehotk->cm_supported & (0x1 << cm)) {
249                 const char *method = cm_setv[cm];
250                 if (method == NULL)
251                         return -ENODEV;
252                 if (write_acpi_int(ehotk->handle, method, value, NULL))
253                         printk(EEEPC_WARNING "Error writing %s\n", method);
254         }
255         return 0;
256 }
257
258 static int get_acpi(int cm)
259 {
260         int value = -1;
261         if ((ehotk->cm_supported & (0x1 << cm))) {
262                 const char *method = cm_getv[cm];
263                 if (method == NULL)
264                         return -ENODEV;
265                 if (read_acpi_int(ehotk->handle, method, &value))
266                         printk(EEEPC_WARNING "Error reading %s\n", method);
267         }
268         return value;
269 }
270
271 /*
272  * Backlight
273  */
274 static int read_brightness(struct backlight_device *bd)
275 {
276         return get_acpi(CM_ASL_PANELBRIGHT);
277 }
278
279 static int set_brightness(struct backlight_device *bd, int value)
280 {
281         value = max(0, min(15, value));
282         return set_acpi(CM_ASL_PANELBRIGHT, value);
283 }
284
285 static int update_bl_status(struct backlight_device *bd)
286 {
287         return set_brightness(bd, bd->props.brightness);
288 }
289
290 /*
291  * Rfkill helpers
292  */
293
294 static int eeepc_wlan_rfkill_set(void *data, enum rfkill_state state)
295 {
296         if (state == RFKILL_STATE_SOFT_BLOCKED)
297                 return set_acpi(CM_ASL_WLAN, 0);
298         else
299                 return set_acpi(CM_ASL_WLAN, 1);
300 }
301
302 static int eeepc_wlan_rfkill_state(void *data, enum rfkill_state *state)
303 {
304         if (get_acpi(CM_ASL_WLAN) == 1)
305                 *state = RFKILL_STATE_UNBLOCKED;
306         else
307                 *state = RFKILL_STATE_SOFT_BLOCKED;
308         return 0;
309 }
310
311 static int eeepc_bluetooth_rfkill_set(void *data, enum rfkill_state state)
312 {
313         if (state == RFKILL_STATE_SOFT_BLOCKED)
314                 return set_acpi(CM_ASL_BLUETOOTH, 0);
315         else
316                 return set_acpi(CM_ASL_BLUETOOTH, 1);
317 }
318
319 static int eeepc_bluetooth_rfkill_state(void *data, enum rfkill_state *state)
320 {
321         if (get_acpi(CM_ASL_BLUETOOTH) == 1)
322                 *state = RFKILL_STATE_UNBLOCKED;
323         else
324                 *state = RFKILL_STATE_SOFT_BLOCKED;
325         return 0;
326 }
327
328 /*
329  * Sys helpers
330  */
331 static int parse_arg(const char *buf, unsigned long count, int *val)
332 {
333         if (!count)
334                 return 0;
335         if (sscanf(buf, "%i", val) != 1)
336                 return -EINVAL;
337         return count;
338 }
339
340 static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
341 {
342         int rv, value;
343
344         rv = parse_arg(buf, count, &value);
345         if (rv > 0)
346                 set_acpi(cm, value);
347         return rv;
348 }
349
350 static ssize_t show_sys_acpi(int cm, char *buf)
351 {
352         return sprintf(buf, "%d\n", get_acpi(cm));
353 }
354
355 #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm)                            \
356         static ssize_t show_##_name(struct device *dev,                 \
357                                     struct device_attribute *attr,      \
358                                     char *buf)                          \
359         {                                                               \
360                 return show_sys_acpi(_cm, buf);                         \
361         }                                                               \
362         static ssize_t store_##_name(struct device *dev,                \
363                                      struct device_attribute *attr,     \
364                                      const char *buf, size_t count)     \
365         {                                                               \
366                 return store_sys_acpi(_cm, buf, count);                 \
367         }                                                               \
368         static struct device_attribute dev_attr_##_name = {             \
369                 .attr = {                                               \
370                         .name = __stringify(_name),                     \
371                         .mode = 0644 },                                 \
372                 .show   = show_##_name,                                 \
373                 .store  = store_##_name,                                \
374         }
375
376 EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
377 EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
378 EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
379
380 static struct attribute *platform_attributes[] = {
381         &dev_attr_camera.attr,
382         &dev_attr_cardr.attr,
383         &dev_attr_disp.attr,
384         NULL
385 };
386
387 static struct attribute_group platform_attribute_group = {
388         .attrs = platform_attributes
389 };
390
391 /*
392  * Hotkey functions
393  */
394 static struct key_entry *eepc_get_entry_by_scancode(int code)
395 {
396         struct key_entry *key;
397
398         for (key = eeepc_keymap; key->type != KE_END; key++)
399                 if (code == key->code)
400                         return key;
401
402         return NULL;
403 }
404
405 static struct key_entry *eepc_get_entry_by_keycode(int code)
406 {
407         struct key_entry *key;
408
409         for (key = eeepc_keymap; key->type != KE_END; key++)
410                 if (code == key->keycode && key->type == KE_KEY)
411                         return key;
412
413         return NULL;
414 }
415
416 static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
417 {
418         struct key_entry *key = eepc_get_entry_by_scancode(scancode);
419
420         if (key && key->type == KE_KEY) {
421                 *keycode = key->keycode;
422                 return 0;
423         }
424
425         return -EINVAL;
426 }
427
428 static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
429 {
430         struct key_entry *key;
431         int old_keycode;
432
433         if (keycode < 0 || keycode > KEY_MAX)
434                 return -EINVAL;
435
436         key = eepc_get_entry_by_scancode(scancode);
437         if (key && key->type == KE_KEY) {
438                 old_keycode = key->keycode;
439                 key->keycode = keycode;
440                 set_bit(keycode, dev->keybit);
441                 if (!eepc_get_entry_by_keycode(old_keycode))
442                         clear_bit(old_keycode, dev->keybit);
443                 return 0;
444         }
445
446         return -EINVAL;
447 }
448
449 static int eeepc_hotk_check(void)
450 {
451         const struct key_entry *key;
452         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
453         int result;
454
455         result = acpi_bus_get_status(ehotk->device);
456         if (result)
457                 return result;
458         if (ehotk->device->status.present) {
459                 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
460                                     &buffer)) {
461                         printk(EEEPC_ERR "Hotkey initialization failed\n");
462                         return -ENODEV;
463                 } else {
464                         printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
465                                ehotk->init_flag);
466                 }
467                 /* get control methods supported */
468                 if (read_acpi_int(ehotk->handle, "CMSG"
469                                    , &ehotk->cm_supported)) {
470                         printk(EEEPC_ERR
471                                "Get control methods supported failed\n");
472                         return -ENODEV;
473                 } else {
474                         printk(EEEPC_INFO
475                                "Get control methods supported: 0x%x\n",
476                                ehotk->cm_supported);
477                 }
478                 ehotk->inputdev = input_allocate_device();
479                 if (!ehotk->inputdev) {
480                         printk(EEEPC_INFO "Unable to allocate input device\n");
481                         return 0;
482                 }
483                 ehotk->inputdev->name = "Asus EeePC extra buttons";
484                 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
485                 ehotk->inputdev->id.bustype = BUS_HOST;
486                 ehotk->inputdev->getkeycode = eeepc_getkeycode;
487                 ehotk->inputdev->setkeycode = eeepc_setkeycode;
488
489                 for (key = eeepc_keymap; key->type != KE_END; key++) {
490                         switch (key->type) {
491                         case KE_KEY:
492                                 set_bit(EV_KEY, ehotk->inputdev->evbit);
493                                 set_bit(key->keycode, ehotk->inputdev->keybit);
494                                 break;
495                         }
496                 }
497                 result = input_register_device(ehotk->inputdev);
498                 if (result) {
499                         printk(EEEPC_INFO "Unable to register input device\n");
500                         input_free_device(ehotk->inputdev);
501                         return 0;
502                 }
503         } else {
504                 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
505                 return -EINVAL;
506         }
507         return 0;
508 }
509
510 static void notify_brn(void)
511 {
512         struct backlight_device *bd = eeepc_backlight_device;
513         if (bd)
514                 bd->props.brightness = read_brightness(bd);
515 }
516
517 static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
518 {
519         static struct key_entry *key;
520         if (!ehotk)
521                 return;
522         if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
523                 notify_brn();
524         acpi_bus_generate_proc_event(ehotk->device, event,
525                                      ehotk->event_count[event % 128]++);
526         if (ehotk->inputdev) {
527                 key = eepc_get_entry_by_scancode(event);
528                 if (key) {
529                         switch (key->type) {
530                         case KE_KEY:
531                                 input_report_key(ehotk->inputdev, key->keycode,
532                                                  1);
533                                 input_sync(ehotk->inputdev);
534                                 input_report_key(ehotk->inputdev, key->keycode,
535                                                  0);
536                                 input_sync(ehotk->inputdev);
537                                 break;
538                         }
539                 }
540         }
541 }
542
543 static int eeepc_hotk_add(struct acpi_device *device)
544 {
545         acpi_status status = AE_OK;
546         int result;
547
548         if (!device)
549                  return -EINVAL;
550         printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
551         ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
552         if (!ehotk)
553                 return -ENOMEM;
554         ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
555         ehotk->handle = device->handle;
556         strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
557         strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
558         device->driver_data = ehotk;
559         ehotk->device = device;
560         result = eeepc_hotk_check();
561         if (result)
562                 goto end;
563         status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
564                                              eeepc_hotk_notify, ehotk);
565         if (ACPI_FAILURE(status))
566                 printk(EEEPC_ERR "Error installing notify handler\n");
567
568         if (get_acpi(CM_ASL_WLAN) != -1) {
569                 ehotk->eeepc_wlan_rfkill = rfkill_allocate(&device->dev,
570                                                            RFKILL_TYPE_WLAN);
571
572                 if (!ehotk->eeepc_wlan_rfkill)
573                         goto end;
574
575                 ehotk->eeepc_wlan_rfkill->name = "eeepc-wlan";
576                 ehotk->eeepc_wlan_rfkill->toggle_radio = eeepc_wlan_rfkill_set;
577                 ehotk->eeepc_wlan_rfkill->get_state = eeepc_wlan_rfkill_state;
578                 if (get_acpi(CM_ASL_WLAN) == 1)
579                         ehotk->eeepc_wlan_rfkill->state =
580                                 RFKILL_STATE_UNBLOCKED;
581                 else
582                         ehotk->eeepc_wlan_rfkill->state =
583                                 RFKILL_STATE_SOFT_BLOCKED;
584                 rfkill_register(ehotk->eeepc_wlan_rfkill);
585         }
586
587         if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
588                 ehotk->eeepc_bluetooth_rfkill =
589                         rfkill_allocate(&device->dev, RFKILL_TYPE_BLUETOOTH);
590
591                 if (!ehotk->eeepc_bluetooth_rfkill)
592                         goto end;
593
594                 ehotk->eeepc_bluetooth_rfkill->name = "eeepc-bluetooth";
595                 ehotk->eeepc_bluetooth_rfkill->toggle_radio =
596                         eeepc_bluetooth_rfkill_set;
597                 ehotk->eeepc_bluetooth_rfkill->get_state =
598                         eeepc_bluetooth_rfkill_state;
599                 if (get_acpi(CM_ASL_BLUETOOTH) == 1)
600                         ehotk->eeepc_bluetooth_rfkill->state =
601                                 RFKILL_STATE_UNBLOCKED;
602                 else
603                         ehotk->eeepc_bluetooth_rfkill->state =
604                                 RFKILL_STATE_SOFT_BLOCKED;
605                 rfkill_register(ehotk->eeepc_bluetooth_rfkill);
606         }
607
608  end:
609         if (result) {
610                 kfree(ehotk);
611                 ehotk = NULL;
612         }
613         return result;
614 }
615
616 static int eeepc_hotk_remove(struct acpi_device *device, int type)
617 {
618         acpi_status status = 0;
619
620         if (!device || !acpi_driver_data(device))
621                  return -EINVAL;
622         status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
623                                             eeepc_hotk_notify);
624         if (ACPI_FAILURE(status))
625                 printk(EEEPC_ERR "Error removing notify handler\n");
626         kfree(ehotk);
627         return 0;
628 }
629
630 /*
631  * Hwmon
632  */
633 static int eeepc_get_fan_pwm(void)
634 {
635         int value = 0;
636
637         read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
638         value = value * 255 / 100;
639         return (value);
640 }
641
642 static void eeepc_set_fan_pwm(int value)
643 {
644         value = SENSORS_LIMIT(value, 0, 255);
645         value = value * 100 / 255;
646         ec_write(EEEPC_EC_SC02, value);
647 }
648
649 static int eeepc_get_fan_rpm(void)
650 {
651         int high = 0;
652         int low = 0;
653
654         read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
655         read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
656         return (high << 8 | low);
657 }
658
659 static int eeepc_get_fan_ctrl(void)
660 {
661         int value = 0;
662
663         read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
664         return ((value & 0x02 ? 1 : 0));
665 }
666
667 static void eeepc_set_fan_ctrl(int manual)
668 {
669         int value = 0;
670
671         read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
672         if (manual)
673                 value |= 0x02;
674         else
675                 value &= ~0x02;
676         ec_write(EEEPC_EC_SFB3, value);
677 }
678
679 static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
680 {
681         int rv, value;
682
683         rv = parse_arg(buf, count, &value);
684         if (rv > 0)
685                 set(value);
686         return rv;
687 }
688
689 static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
690 {
691         return sprintf(buf, "%d\n", get());
692 }
693
694 #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get)              \
695         static ssize_t show_##_name(struct device *dev,                 \
696                                     struct device_attribute *attr,      \
697                                     char *buf)                          \
698         {                                                               \
699                 return show_sys_hwmon(_set, buf);                       \
700         }                                                               \
701         static ssize_t store_##_name(struct device *dev,                \
702                                      struct device_attribute *attr,     \
703                                      const char *buf, size_t count)     \
704         {                                                               \
705                 return store_sys_hwmon(_get, buf, count);               \
706         }                                                               \
707         static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
708
709 EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
710 EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
711                          eeepc_get_fan_pwm, eeepc_set_fan_pwm);
712 EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
713                          eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
714
715 static ssize_t
716 show_name(struct device *dev, struct device_attribute *attr, char *buf)
717 {
718         return sprintf(buf, "eeepc\n");
719 }
720 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
721
722 static struct attribute *hwmon_attributes[] = {
723         &sensor_dev_attr_pwm1.dev_attr.attr,
724         &sensor_dev_attr_fan1_input.dev_attr.attr,
725         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
726         &sensor_dev_attr_name.dev_attr.attr,
727         NULL
728 };
729
730 static struct attribute_group hwmon_attribute_group = {
731         .attrs = hwmon_attributes
732 };
733
734 /*
735  * exit/init
736  */
737 static void eeepc_backlight_exit(void)
738 {
739         if (eeepc_backlight_device)
740                 backlight_device_unregister(eeepc_backlight_device);
741         if (ehotk->inputdev)
742                 input_unregister_device(ehotk->inputdev);
743         if (ehotk->eeepc_wlan_rfkill)
744                 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
745         if (ehotk->eeepc_bluetooth_rfkill)
746                 rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
747         eeepc_backlight_device = NULL;
748 }
749
750 static void eeepc_hwmon_exit(void)
751 {
752         struct device *hwmon;
753
754         hwmon = eeepc_hwmon_device;
755         if (!hwmon)
756                 return ;
757         sysfs_remove_group(&hwmon->kobj,
758                            &hwmon_attribute_group);
759         hwmon_device_unregister(hwmon);
760         eeepc_hwmon_device = NULL;
761 }
762
763 static void __exit eeepc_laptop_exit(void)
764 {
765         eeepc_backlight_exit();
766         eeepc_hwmon_exit();
767         acpi_bus_unregister_driver(&eeepc_hotk_driver);
768         sysfs_remove_group(&platform_device->dev.kobj,
769                            &platform_attribute_group);
770         platform_device_unregister(platform_device);
771         platform_driver_unregister(&platform_driver);
772 }
773
774 static int eeepc_backlight_init(struct device *dev)
775 {
776         struct backlight_device *bd;
777
778         bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
779                                        NULL, &eeepcbl_ops);
780         if (IS_ERR(bd)) {
781                 printk(EEEPC_ERR
782                        "Could not register eeepc backlight device\n");
783                 eeepc_backlight_device = NULL;
784                 return PTR_ERR(bd);
785         }
786         eeepc_backlight_device = bd;
787         bd->props.max_brightness = 15;
788         bd->props.brightness = read_brightness(NULL);
789         bd->props.power = FB_BLANK_UNBLANK;
790         backlight_update_status(bd);
791         return 0;
792 }
793
794 static int eeepc_hwmon_init(struct device *dev)
795 {
796         struct device *hwmon;
797         int result;
798
799         hwmon = hwmon_device_register(dev);
800         if (IS_ERR(hwmon)) {
801                 printk(EEEPC_ERR
802                        "Could not register eeepc hwmon device\n");
803                 eeepc_hwmon_device = NULL;
804                 return PTR_ERR(hwmon);
805         }
806         eeepc_hwmon_device = hwmon;
807         result = sysfs_create_group(&hwmon->kobj,
808                                     &hwmon_attribute_group);
809         if (result)
810                 eeepc_hwmon_exit();
811         return result;
812 }
813
814 static int __init eeepc_laptop_init(void)
815 {
816         struct device *dev;
817         int result;
818
819         if (acpi_disabled)
820                 return -ENODEV;
821         result = acpi_bus_register_driver(&eeepc_hotk_driver);
822         if (result < 0)
823                 return result;
824         if (!ehotk) {
825                 acpi_bus_unregister_driver(&eeepc_hotk_driver);
826                 return -ENODEV;
827         }
828         dev = acpi_get_physical_device(ehotk->device->handle);
829
830         if (!acpi_video_backlight_support()) {
831                 result = eeepc_backlight_init(dev);
832                 if (result)
833                         goto fail_backlight;
834         } else
835                 printk(EEEPC_INFO "Backlight controlled by ACPI video "
836                        "driver\n");
837
838         result = eeepc_hwmon_init(dev);
839         if (result)
840                 goto fail_hwmon;
841         /* Register platform stuff */
842         result = platform_driver_register(&platform_driver);
843         if (result)
844                 goto fail_platform_driver;
845         platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
846         if (!platform_device) {
847                 result = -ENOMEM;
848                 goto fail_platform_device1;
849         }
850         result = platform_device_add(platform_device);
851         if (result)
852                 goto fail_platform_device2;
853         result = sysfs_create_group(&platform_device->dev.kobj,
854                                     &platform_attribute_group);
855         if (result)
856                 goto fail_sysfs;
857         return 0;
858 fail_sysfs:
859         platform_device_del(platform_device);
860 fail_platform_device2:
861         platform_device_put(platform_device);
862 fail_platform_device1:
863         platform_driver_unregister(&platform_driver);
864 fail_platform_driver:
865         eeepc_hwmon_exit();
866 fail_hwmon:
867         eeepc_backlight_exit();
868 fail_backlight:
869         return result;
870 }
871
872 module_init(eeepc_laptop_init);
873 module_exit(eeepc_laptop_exit);