1 /* drivers/leds/ledtrig-sleep.c
2 *
3 * Copyright (C) 2007 Google, Inc.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
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.
13 *
14 */
15
16 #include <linux/earlysuspend.h>
17 #include <linux/leds.h>
18 #include <linux/suspend.h>
19
20 static int ledtrig_sleep_pm_callback(struct notifier_block *nfb,
21 unsigned long action,
22 void *ignored);
23
24 DEFINE_LED_TRIGGER(ledtrig_sleep)
25 static struct notifier_block ledtrig_sleep_pm_notifier = {
26 .notifier_call = ledtrig_sleep_pm_callback,
27 .priority = 0,
28 };
29
ledtrig_sleep_early_suspend(struct early_suspend * h)30 static void ledtrig_sleep_early_suspend(struct early_suspend *h)
31 {
32 led_trigger_event(ledtrig_sleep, LED_FULL);
33 }
34
ledtrig_sleep_early_resume(struct early_suspend * h)35 static void ledtrig_sleep_early_resume(struct early_suspend *h)
36 {
37 led_trigger_event(ledtrig_sleep, LED_OFF);
38 }
39
40 static struct early_suspend ledtrig_sleep_early_suspend_handler = {
41 .suspend = ledtrig_sleep_early_suspend,
42 .resume = ledtrig_sleep_early_resume,
43 };
44
ledtrig_sleep_pm_callback(struct notifier_block * nfb,unsigned long action,void * ignored)45 static int ledtrig_sleep_pm_callback(struct notifier_block *nfb,
46 unsigned long action,
47 void *ignored)
48 {
49 switch (action) {
50 case PM_HIBERNATION_PREPARE:
51 case PM_SUSPEND_PREPARE:
52 led_trigger_event(ledtrig_sleep, LED_OFF);
53 return NOTIFY_OK;
54 case PM_POST_HIBERNATION:
55 case PM_POST_SUSPEND:
56 led_trigger_event(ledtrig_sleep, LED_FULL);
57 return NOTIFY_OK;
58 }
59
60 return NOTIFY_DONE;
61 }
62
ledtrig_sleep_init(void)63 static int __init ledtrig_sleep_init(void)
64 {
65 led_trigger_register_simple("sleep", &ledtrig_sleep);
66 register_pm_notifier(&ledtrig_sleep_pm_notifier);
67 register_early_suspend(&ledtrig_sleep_early_suspend_handler);
68 return 0;
69 }
70
ledtrig_sleep_exit(void)71 static void __exit ledtrig_sleep_exit(void)
72 {
73 unregister_early_suspend(&ledtrig_sleep_early_suspend_handler);
74 unregister_pm_notifier(&ledtrig_sleep_pm_notifier);
75 led_trigger_unregister_simple(ledtrig_sleep);
76 }
77
78 module_init(ledtrig_sleep_init);
79 module_exit(ledtrig_sleep_exit);
80
81