Merge branch 'linux-linaro-lsk' into linux-linaro-lsk-android
[firefly-linux-kernel-4.4.55.git] / kernel / power / wakeup_reason.c
1 /*
2  * kernel/power/wakeup_reason.c
3  *
4  * Logs the reasons which caused the kernel to resume from
5  * the suspend mode.
6  *
7  * Copyright (C) 2014 Google, Inc.
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/wakeup_reason.h>
19 #include <linux/kernel.h>
20 #include <linux/irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/kobject.h>
24 #include <linux/sysfs.h>
25 #include <linux/init.h>
26 #include <linux/spinlock.h>
27 #include <linux/notifier.h>
28 #include <linux/suspend.h>
29
30
31 #define MAX_WAKEUP_REASON_IRQS 32
32 static int irq_list[MAX_WAKEUP_REASON_IRQS];
33 static int irqcount;
34 static bool suspend_abort;
35 static char abort_reason[MAX_SUSPEND_ABORT_LEN];
36 static struct kobject *wakeup_reason;
37 static DEFINE_SPINLOCK(resume_reason_lock);
38
39 static ssize_t last_resume_reason_show(struct kobject *kobj, struct kobj_attribute *attr,
40                 char *buf)
41 {
42         int irq_no, buf_offset = 0;
43         struct irq_desc *desc;
44         spin_lock(&resume_reason_lock);
45         if (suspend_abort) {
46                 buf_offset = sprintf(buf, "Abort: %s", abort_reason);
47         } else {
48                 for (irq_no = 0; irq_no < irqcount; irq_no++) {
49                         desc = irq_to_desc(irq_list[irq_no]);
50                         if (desc && desc->action && desc->action->name)
51                                 buf_offset += sprintf(buf + buf_offset, "%d %s\n",
52                                                 irq_list[irq_no], desc->action->name);
53                         else
54                                 buf_offset += sprintf(buf + buf_offset, "%d\n",
55                                                 irq_list[irq_no]);
56                 }
57         }
58         spin_unlock(&resume_reason_lock);
59         return buf_offset;
60 }
61
62 static struct kobj_attribute resume_reason = __ATTR_RO(last_resume_reason);
63
64 static struct attribute *attrs[] = {
65         &resume_reason.attr,
66         NULL,
67 };
68 static struct attribute_group attr_group = {
69         .attrs = attrs,
70 };
71
72 /*
73  * logs all the wake up reasons to the kernel
74  * stores the irqs to expose them to the userspace via sysfs
75  */
76 void log_wakeup_reason(int irq)
77 {
78         struct irq_desc *desc;
79         desc = irq_to_desc(irq);
80         if (desc && desc->action && desc->action->name)
81                 printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq,
82                                 desc->action->name);
83         else
84                 printk(KERN_INFO "Resume caused by IRQ %d\n", irq);
85
86         spin_lock(&resume_reason_lock);
87         if (irqcount == MAX_WAKEUP_REASON_IRQS) {
88                 spin_unlock(&resume_reason_lock);
89                 printk(KERN_WARNING "Resume caused by more than %d IRQs\n",
90                                 MAX_WAKEUP_REASON_IRQS);
91                 return;
92         }
93
94         irq_list[irqcount++] = irq;
95         spin_unlock(&resume_reason_lock);
96 }
97
98 int check_wakeup_reason(int irq)
99 {
100         int irq_no;
101         int ret = false;
102
103         spin_lock(&resume_reason_lock);
104         for (irq_no = 0; irq_no < irqcount; irq_no++)
105                 if (irq_list[irq_no] == irq) {
106                         ret = true;
107                         break;
108         }
109         spin_unlock(&resume_reason_lock);
110         return ret;
111 }
112
113 void log_suspend_abort_reason(const char *fmt, ...)
114 {
115         va_list args;
116
117         spin_lock(&resume_reason_lock);
118
119         //Suspend abort reason has already been logged.
120         if (suspend_abort) {
121                 spin_unlock(&resume_reason_lock);
122                 return;
123         }
124
125         suspend_abort = true;
126         va_start(args, fmt);
127         snprintf(abort_reason, MAX_SUSPEND_ABORT_LEN, fmt, args);
128         va_end(args);
129         spin_unlock(&resume_reason_lock);
130 }
131
132 /* Detects a suspend and clears all the previous wake up reasons*/
133 static int wakeup_reason_pm_event(struct notifier_block *notifier,
134                 unsigned long pm_event, void *unused)
135 {
136         switch (pm_event) {
137         case PM_SUSPEND_PREPARE:
138                 spin_lock(&resume_reason_lock);
139                 irqcount = 0;
140                 suspend_abort = false;
141                 spin_unlock(&resume_reason_lock);
142                 break;
143         default:
144                 break;
145         }
146         return NOTIFY_DONE;
147 }
148
149 static struct notifier_block wakeup_reason_pm_notifier_block = {
150         .notifier_call = wakeup_reason_pm_event,
151 };
152
153 /* Initializes the sysfs parameter
154  * registers the pm_event notifier
155  */
156 int __init wakeup_reason_init(void)
157 {
158         int retval;
159
160         retval = register_pm_notifier(&wakeup_reason_pm_notifier_block);
161         if (retval)
162                 printk(KERN_WARNING "[%s] failed to register PM notifier %d\n",
163                                 __func__, retval);
164
165         wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj);
166         if (!wakeup_reason) {
167                 printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n",
168                                 __func__);
169                 return 1;
170         }
171         retval = sysfs_create_group(wakeup_reason, &attr_group);
172         if (retval) {
173                 kobject_put(wakeup_reason);
174                 printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n",
175                                 __func__, retval);
176         }
177         return 0;
178 }
179
180 late_initcall(wakeup_reason_init);