2 * eepc-laptop.c - Asus Eee PC extras
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
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.
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.
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>
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>
32 #define EEEPC_LAPTOP_VERSION "0.1"
34 #define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
35 #define EEEPC_HOTK_FILE "eeepc"
36 #define EEEPC_HOTK_CLASS "hotkey"
37 #define EEEPC_HOTK_DEVICE_NAME "Hotkey"
38 #define EEEPC_HOTK_HID "ASUS010"
40 #define EEEPC_LOG EEEPC_HOTK_FILE ": "
41 #define EEEPC_ERR KERN_ERR EEEPC_LOG
42 #define EEEPC_WARNING KERN_WARNING EEEPC_LOG
43 #define EEEPC_NOTICE KERN_NOTICE EEEPC_LOG
44 #define EEEPC_INFO KERN_INFO EEEPC_LOG
47 * Definitions for Asus EeePC
49 #define NOTIFY_WLAN_ON 0x10
50 #define NOTIFY_BRN_MIN 0x20
51 #define NOTIFY_BRN_MAX 0x2f
54 DISABLE_ASL_WLAN = 0x0001,
55 DISABLE_ASL_BLUETOOTH = 0x0002,
56 DISABLE_ASL_IRDA = 0x0004,
57 DISABLE_ASL_CAMERA = 0x0008,
58 DISABLE_ASL_TV = 0x0010,
59 DISABLE_ASL_GPS = 0x0020,
60 DISABLE_ASL_DISPLAYSWITCH = 0x0040,
61 DISABLE_ASL_MODEM = 0x0080,
62 DISABLE_ASL_CARDREADER = 0x0100
79 CM_ASL_CPUTEMPERATURE,
90 const char *cm_getv[] = {
91 "WLDG", NULL, NULL, NULL,
92 "CAMG", NULL, NULL, NULL,
93 NULL, "PBLG", NULL, NULL,
94 "CFVG", NULL, NULL, NULL,
95 "USBG", NULL, NULL, "MODG",
99 const char *cm_setv[] = {
100 "WLDS", NULL, NULL, NULL,
101 "CAMS", NULL, NULL, NULL,
102 "SDSP", "PBLS", "HDPS", NULL,
103 "CFVS", NULL, NULL, NULL,
104 "USBG", NULL, NULL, "MODS",
108 #define EEEPC_EC "\\_SB.PCI0.SBRG.EC0."
110 #define EEEPC_EC_FAN_PWM EEEPC_EC "SC02" /* Fan PWM duty cycle (%) */
111 #define EEEPC_EC_SC02 0x63
112 #define EEEPC_EC_FAN_HRPM EEEPC_EC "SC05" /* High byte, fan speed (RPM) */
113 #define EEEPC_EC_FAN_LRPM EEEPC_EC "SC06" /* Low byte, fan speed (RPM) */
114 #define EEEPC_EC_FAN_CTRL EEEPC_EC "SFB3" /* Byte containing SF25 */
115 #define EEEPC_EC_SFB3 0xD3
118 * This is the main structure, we can use it to store useful information
119 * about the hotk device
122 struct acpi_device *device; /* the device we are in */
123 acpi_handle handle; /* the handle of the hotk device */
124 u32 cm_supported; /* the control methods supported
126 uint init_flag; /* Init flags */
127 u16 event_count[128]; /* count for each event */
130 /* The actual device the driver binds to */
131 static struct eeepc_hotk *ehotk;
133 /* Platform device/driver */
134 static struct platform_driver platform_driver = {
136 .name = EEEPC_HOTK_FILE,
137 .owner = THIS_MODULE,
141 static struct platform_device *platform_device;
144 * The hotkey driver declaration
146 static int eeepc_hotk_add(struct acpi_device *device);
147 static int eeepc_hotk_remove(struct acpi_device *device, int type);
149 static const struct acpi_device_id eeepc_device_ids[] = {
153 MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
155 static struct acpi_driver eeepc_hotk_driver = {
156 .name = EEEPC_HOTK_NAME,
157 .class = EEEPC_HOTK_CLASS,
158 .ids = eeepc_device_ids,
160 .add = eeepc_hotk_add,
161 .remove = eeepc_hotk_remove,
165 /* The backlight device /sys/class/backlight */
166 static struct backlight_device *eeepc_backlight_device;
168 /* The hwmon device */
169 static struct device *eeepc_hwmon_device;
172 * The backlight class declaration
174 static int read_brightness(struct backlight_device *bd);
175 static int update_bl_status(struct backlight_device *bd);
176 static struct backlight_ops eeepcbl_ops = {
177 .get_brightness = read_brightness,
178 .update_status = update_bl_status,
181 MODULE_AUTHOR("Corentin Chary, Eric Cooper");
182 MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
183 MODULE_LICENSE("GPL");
188 static int write_acpi_int(acpi_handle handle, const char *method, int val,
189 struct acpi_buffer *output)
191 struct acpi_object_list params;
192 union acpi_object in_obj;
196 params.pointer = &in_obj;
197 in_obj.type = ACPI_TYPE_INTEGER;
198 in_obj.integer.value = val;
200 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
201 return (status == AE_OK ? 0 : -1);
204 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
209 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
210 if (ACPI_FAILURE(status)) {
219 static int set_acpi(int cm, int value)
221 if (ehotk->cm_supported & (0x1 << cm)) {
222 const char *method = cm_setv[cm];
225 if (write_acpi_int(ehotk->handle, method, value, NULL))
226 printk(EEEPC_WARNING "Error writing %s\n", method);
231 static int get_acpi(int cm)
234 if ((ehotk->cm_supported & (0x1 << cm))) {
235 const char *method = cm_getv[cm];
238 if (read_acpi_int(ehotk->handle, method, &value))
239 printk(EEEPC_WARNING "Error reading %s\n", method);
247 static int read_brightness(struct backlight_device *bd)
249 return get_acpi(CM_ASL_PANELBRIGHT);
252 static int set_brightness(struct backlight_device *bd, int value)
254 value = max(0, min(15, value));
255 return set_acpi(CM_ASL_PANELBRIGHT, value);
258 static int update_bl_status(struct backlight_device *bd)
260 return set_brightness(bd, bd->props.brightness);
266 static int parse_arg(const char *buf, unsigned long count, int *val)
270 if (sscanf(buf, "%i", val) != 1)
275 static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
279 rv = parse_arg(buf, count, &value);
285 static ssize_t show_sys_acpi(int cm, char *buf)
287 return sprintf(buf, "%d\n", get_acpi(cm));
290 #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
291 static ssize_t show_##_name(struct device *dev, \
292 struct device_attribute *attr, \
295 return show_sys_acpi(_cm, buf); \
297 static ssize_t store_##_name(struct device *dev, \
298 struct device_attribute *attr, \
299 const char *buf, size_t count) \
301 return store_sys_acpi(_cm, buf, count); \
303 static struct device_attribute dev_attr_##_name = { \
305 .name = __stringify(_name), \
307 .show = show_##_name, \
308 .store = store_##_name, \
311 EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
312 EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
313 EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
314 EEEPC_CREATE_DEVICE_ATTR(wlan, CM_ASL_WLAN);
316 static struct attribute *platform_attributes[] = {
317 &dev_attr_camera.attr,
318 &dev_attr_cardr.attr,
324 static struct attribute_group platform_attribute_group = {
325 .attrs = platform_attributes
331 static int eeepc_hotk_check(void)
333 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
336 result = acpi_bus_get_status(ehotk->device);
339 if (ehotk->device->status.present) {
340 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
342 printk(EEEPC_ERR "Hotkey initialization failed\n");
345 printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
348 /* get control methods supported */
349 if (read_acpi_int(ehotk->handle, "CMSG"
350 , &ehotk->cm_supported)) {
352 "Get control methods supported failed\n");
356 "Get control methods supported: 0x%x\n",
357 ehotk->cm_supported);
360 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
366 static void notify_wlan(u32 *event)
368 /* if DISABLE_ASL_WLAN is set, the notify code for fn+f2
369 will always be 0x10 */
370 if (ehotk->cm_supported & (0x1 << CM_ASL_WLAN)) {
371 const char *method = cm_getv[CM_ASL_WLAN];
373 if (read_acpi_int(ehotk->handle, method, &value))
374 printk(EEEPC_WARNING "Error reading %s\n",
381 static void notify_brn(void)
383 struct backlight_device *bd = eeepc_backlight_device;
384 bd->props.brightness = read_brightness(bd);
387 static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
391 if (event == NOTIFY_WLAN_ON && (DISABLE_ASL_WLAN & ehotk->init_flag))
393 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
395 acpi_bus_generate_proc_event(ehotk->device, event,
396 ehotk->event_count[event % 128]++);
399 static int eeepc_hotk_add(struct acpi_device *device)
401 acpi_status status = AE_OK;
406 printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
407 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
410 ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
411 ehotk->handle = device->handle;
412 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
413 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
414 acpi_driver_data(device) = ehotk;
415 ehotk->device = device;
416 result = eeepc_hotk_check();
419 status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
420 eeepc_hotk_notify, ehotk);
421 if (ACPI_FAILURE(status))
422 printk(EEEPC_ERR "Error installing notify handler\n");
431 static int eeepc_hotk_remove(struct acpi_device *device, int type)
433 acpi_status status = 0;
435 if (!device || !acpi_driver_data(device))
437 status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
439 if (ACPI_FAILURE(status))
440 printk(EEEPC_ERR "Error removing notify handler\n");
448 static int eeepc_get_fan_pwm(void)
452 read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
456 static void eeepc_set_fan_pwm(int value)
458 value = SENSORS_LIMIT(value, 0, 100);
459 ec_write(EEEPC_EC_SC02, value);
462 static int eeepc_get_fan_rpm(void)
467 read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
468 read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
469 return (high << 8 | low);
472 static int eeepc_get_fan_ctrl(void)
476 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
477 return ((value & 0x02 ? 1 : 0));
480 static void eeepc_set_fan_ctrl(int manual)
484 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
489 ec_write(EEEPC_EC_SFB3, value);
492 static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
496 rv = parse_arg(buf, count, &value);
502 static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
504 return sprintf(buf, "%d\n", get());
507 #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
508 static ssize_t show_##_name(struct device *dev, \
509 struct device_attribute *attr, \
512 return show_sys_hwmon(_set, buf); \
514 static ssize_t store_##_name(struct device *dev, \
515 struct device_attribute *attr, \
516 const char *buf, size_t count) \
518 return store_sys_hwmon(_get, buf, count); \
520 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
522 EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
523 EEEPC_CREATE_SENSOR_ATTR(fan1_pwm, S_IRUGO | S_IWUSR,
524 eeepc_get_fan_pwm, eeepc_set_fan_pwm);
525 EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
526 eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
528 static struct attribute *hwmon_attributes[] = {
529 &sensor_dev_attr_fan1_pwm.dev_attr.attr,
530 &sensor_dev_attr_fan1_input.dev_attr.attr,
531 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
535 static struct attribute_group hwmon_attribute_group = {
536 .attrs = hwmon_attributes
542 static void eeepc_backlight_exit(void)
544 if (eeepc_backlight_device)
545 backlight_device_unregister(eeepc_backlight_device);
546 eeepc_backlight_device = NULL;
549 static void eeepc_hwmon_exit(void)
551 struct device *hwmon;
553 hwmon = eeepc_hwmon_device;
556 hwmon_device_unregister(hwmon);
557 sysfs_remove_group(&hwmon->kobj,
558 &hwmon_attribute_group);
559 eeepc_hwmon_device = NULL;
562 static void __exit eeepc_laptop_exit(void)
564 eeepc_backlight_exit();
566 acpi_bus_unregister_driver(&eeepc_hotk_driver);
567 sysfs_remove_group(&platform_device->dev.kobj,
568 &platform_attribute_group);
569 platform_device_unregister(platform_device);
570 platform_driver_unregister(&platform_driver);
573 static int eeepc_backlight_init(struct device *dev)
575 struct backlight_device *bd;
577 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
581 "Could not register eeepc backlight device\n");
582 eeepc_backlight_device = NULL;
585 eeepc_backlight_device = bd;
586 bd->props.max_brightness = 15;
587 bd->props.brightness = read_brightness(NULL);
588 bd->props.power = FB_BLANK_UNBLANK;
589 backlight_update_status(bd);
593 static int eeepc_hwmon_init(struct device *dev)
595 struct device *hwmon;
598 hwmon = hwmon_device_register(dev);
601 "Could not register eeepc hwmon device\n");
602 eeepc_hwmon_device = NULL;
603 return PTR_ERR(hwmon);
605 eeepc_hwmon_device = hwmon;
606 result = sysfs_create_group(&hwmon->kobj,
607 &hwmon_attribute_group);
613 static int __init eeepc_laptop_init(void)
620 result = acpi_bus_register_driver(&eeepc_hotk_driver);
624 acpi_bus_unregister_driver(&eeepc_hotk_driver);
627 dev = acpi_get_physical_device(ehotk->device->handle);
628 result = eeepc_backlight_init(dev);
631 result = eeepc_hwmon_init(dev);
634 /* Register platform stuff */
635 result = platform_driver_register(&platform_driver);
637 goto fail_platform_driver;
638 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
639 if (!platform_device) {
641 goto fail_platform_device1;
643 result = platform_device_add(platform_device);
645 goto fail_platform_device2;
646 result = sysfs_create_group(&platform_device->dev.kobj,
647 &platform_attribute_group);
652 platform_device_del(platform_device);
653 fail_platform_device2:
654 platform_device_put(platform_device);
655 fail_platform_device1:
656 platform_driver_unregister(&platform_driver);
657 fail_platform_driver:
660 eeepc_backlight_exit();
665 module_init(eeepc_laptop_init);
666 module_exit(eeepc_laptop_exit);