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 static 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 static 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
)
207 unsigned long long result
;
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
;
385 bd
->props
.brightness
= read_brightness(bd
);
388 static void eeepc_hotk_notify(acpi_handle handle
, u32 event
, void *data
)
392 if (event
== NOTIFY_WLAN_ON
&& (DISABLE_ASL_WLAN
& ehotk
->init_flag
))
394 if (event
>= NOTIFY_BRN_MIN
&& event
<= NOTIFY_BRN_MAX
)
396 acpi_bus_generate_proc_event(ehotk
->device
, event
,
397 ehotk
->event_count
[event
% 128]++);
400 static int eeepc_hotk_add(struct acpi_device
*device
)
402 acpi_status status
= AE_OK
;
407 printk(EEEPC_NOTICE EEEPC_HOTK_NAME
"\n");
408 ehotk
= kzalloc(sizeof(struct eeepc_hotk
), GFP_KERNEL
);
411 ehotk
->init_flag
= DISABLE_ASL_WLAN
| DISABLE_ASL_DISPLAYSWITCH
;
412 ehotk
->handle
= device
->handle
;
413 strcpy(acpi_device_name(device
), EEEPC_HOTK_DEVICE_NAME
);
414 strcpy(acpi_device_class(device
), EEEPC_HOTK_CLASS
);
415 acpi_driver_data(device
) = ehotk
;
416 ehotk
->device
= device
;
417 result
= eeepc_hotk_check();
420 status
= acpi_install_notify_handler(ehotk
->handle
, ACPI_SYSTEM_NOTIFY
,
421 eeepc_hotk_notify
, ehotk
);
422 if (ACPI_FAILURE(status
))
423 printk(EEEPC_ERR
"Error installing notify handler\n");
432 static int eeepc_hotk_remove(struct acpi_device
*device
, int type
)
434 acpi_status status
= 0;
436 if (!device
|| !acpi_driver_data(device
))
438 status
= acpi_remove_notify_handler(ehotk
->handle
, ACPI_SYSTEM_NOTIFY
,
440 if (ACPI_FAILURE(status
))
441 printk(EEEPC_ERR
"Error removing notify handler\n");
449 static int eeepc_get_fan_pwm(void)
453 read_acpi_int(NULL
, EEEPC_EC_FAN_PWM
, &value
);
454 value
= value
* 255 / 100;
458 static void eeepc_set_fan_pwm(int value
)
460 value
= SENSORS_LIMIT(value
, 0, 255);
461 value
= value
* 100 / 255;
462 ec_write(EEEPC_EC_SC02
, value
);
465 static int eeepc_get_fan_rpm(void)
470 read_acpi_int(NULL
, EEEPC_EC_FAN_HRPM
, &high
);
471 read_acpi_int(NULL
, EEEPC_EC_FAN_LRPM
, &low
);
472 return (high
<< 8 | low
);
475 static int eeepc_get_fan_ctrl(void)
479 read_acpi_int(NULL
, EEEPC_EC_FAN_CTRL
, &value
);
480 return ((value
& 0x02 ? 1 : 0));
483 static void eeepc_set_fan_ctrl(int manual
)
487 read_acpi_int(NULL
, EEEPC_EC_FAN_CTRL
, &value
);
492 ec_write(EEEPC_EC_SFB3
, value
);
495 static ssize_t
store_sys_hwmon(void (*set
)(int), const char *buf
, size_t count
)
499 rv
= parse_arg(buf
, count
, &value
);
505 static ssize_t
show_sys_hwmon(int (*get
)(void), char *buf
)
507 return sprintf(buf
, "%d\n", get());
510 #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
511 static ssize_t show_##_name(struct device *dev, \
512 struct device_attribute *attr, \
515 return show_sys_hwmon(_set, buf); \
517 static ssize_t store_##_name(struct device *dev, \
518 struct device_attribute *attr, \
519 const char *buf, size_t count) \
521 return store_sys_hwmon(_get, buf, count); \
523 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
525 EEEPC_CREATE_SENSOR_ATTR(fan1_input
, S_IRUGO
, eeepc_get_fan_rpm
, NULL
);
526 EEEPC_CREATE_SENSOR_ATTR(pwm1
, S_IRUGO
| S_IWUSR
,
527 eeepc_get_fan_pwm
, eeepc_set_fan_pwm
);
528 EEEPC_CREATE_SENSOR_ATTR(pwm1_enable
, S_IRUGO
| S_IWUSR
,
529 eeepc_get_fan_ctrl
, eeepc_set_fan_ctrl
);
532 show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
534 return sprintf(buf
, "eeepc\n");
536 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
, 0);
538 static struct attribute
*hwmon_attributes
[] = {
539 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
540 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
541 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
542 &sensor_dev_attr_name
.dev_attr
.attr
,
546 static struct attribute_group hwmon_attribute_group
= {
547 .attrs
= hwmon_attributes
553 static void eeepc_backlight_exit(void)
555 if (eeepc_backlight_device
)
556 backlight_device_unregister(eeepc_backlight_device
);
557 eeepc_backlight_device
= NULL
;
560 static void eeepc_hwmon_exit(void)
562 struct device
*hwmon
;
564 hwmon
= eeepc_hwmon_device
;
567 sysfs_remove_group(&hwmon
->kobj
,
568 &hwmon_attribute_group
);
569 hwmon_device_unregister(hwmon
);
570 eeepc_hwmon_device
= NULL
;
573 static void __exit
eeepc_laptop_exit(void)
575 eeepc_backlight_exit();
577 acpi_bus_unregister_driver(&eeepc_hotk_driver
);
578 sysfs_remove_group(&platform_device
->dev
.kobj
,
579 &platform_attribute_group
);
580 platform_device_unregister(platform_device
);
581 platform_driver_unregister(&platform_driver
);
584 static int eeepc_backlight_init(struct device
*dev
)
586 struct backlight_device
*bd
;
588 bd
= backlight_device_register(EEEPC_HOTK_FILE
, dev
,
592 "Could not register eeepc backlight device\n");
593 eeepc_backlight_device
= NULL
;
596 eeepc_backlight_device
= bd
;
597 bd
->props
.max_brightness
= 15;
598 bd
->props
.brightness
= read_brightness(NULL
);
599 bd
->props
.power
= FB_BLANK_UNBLANK
;
600 backlight_update_status(bd
);
604 static int eeepc_hwmon_init(struct device
*dev
)
606 struct device
*hwmon
;
609 hwmon
= hwmon_device_register(dev
);
612 "Could not register eeepc hwmon device\n");
613 eeepc_hwmon_device
= NULL
;
614 return PTR_ERR(hwmon
);
616 eeepc_hwmon_device
= hwmon
;
617 result
= sysfs_create_group(&hwmon
->kobj
,
618 &hwmon_attribute_group
);
624 static int __init
eeepc_laptop_init(void)
631 result
= acpi_bus_register_driver(&eeepc_hotk_driver
);
635 acpi_bus_unregister_driver(&eeepc_hotk_driver
);
638 dev
= acpi_get_physical_device(ehotk
->device
->handle
);
639 result
= eeepc_backlight_init(dev
);
642 result
= eeepc_hwmon_init(dev
);
645 /* Register platform stuff */
646 result
= platform_driver_register(&platform_driver
);
648 goto fail_platform_driver
;
649 platform_device
= platform_device_alloc(EEEPC_HOTK_FILE
, -1);
650 if (!platform_device
) {
652 goto fail_platform_device1
;
654 result
= platform_device_add(platform_device
);
656 goto fail_platform_device2
;
657 result
= sysfs_create_group(&platform_device
->dev
.kobj
,
658 &platform_attribute_group
);
663 platform_device_del(platform_device
);
664 fail_platform_device2
:
665 platform_device_put(platform_device
);
666 fail_platform_device1
:
667 platform_driver_unregister(&platform_driver
);
668 fail_platform_driver
:
671 eeepc_backlight_exit();
676 module_init(eeepc_laptop_init
);
677 module_exit(eeepc_laptop_exit
);