2 * kernel/power/wakeup_reason.c
4 * Logs the reasons which caused the kernel to resume from
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.
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.
18 #include <linux/wakeup_reason.h>
19 #include <linux/kernel.h>
20 #include <linux/irq.h>
21 #include <linux/interrupt.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 #include <linux/debugfs.h>
31 #ifdef CONFIG_SOC_EXYNOS5433
32 #define MAX_WAKEUP_REASON_IRQS 64
34 #define MAX_WAKEUP_REASON_IRQS 32
36 static int irq_list
[MAX_WAKEUP_REASON_IRQS
];
38 static bool suspend_abort
;
39 static char abort_reason
[MAX_SUSPEND_ABORT_LEN
];
40 static struct kobject
*wakeup_reason
;
41 static DEFINE_SPINLOCK(resume_reason_lock
);
43 static ssize_t
last_resume_reason_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
46 int irq_no
, buf_offset
= 0;
47 struct irq_desc
*desc
;
48 spin_lock(&resume_reason_lock
);
50 buf_offset
= sprintf(buf
, "Abort: %s", abort_reason
);
52 for (irq_no
= 0; irq_no
< irqcount
; irq_no
++) {
53 desc
= irq_to_desc(irq_list
[irq_no
]);
54 if (desc
&& desc
->action
&& desc
->action
->name
)
55 buf_offset
+= sprintf(buf
+ buf_offset
, "%d %s\n",
56 irq_list
[irq_no
], desc
->action
->name
);
58 buf_offset
+= sprintf(buf
+ buf_offset
, "%d\n",
62 spin_unlock(&resume_reason_lock
);
66 static struct kobj_attribute resume_reason
= __ATTR_RO(last_resume_reason
);
68 static struct attribute
*attrs
[] = {
72 static struct attribute_group attr_group
= {
77 * logs all the wake up reasons to the kernel
78 * stores the irqs to expose them to the userspace via sysfs
80 void log_wakeup_reason(int irq
)
82 struct irq_desc
*desc
;
83 desc
= irq_to_desc(irq
);
84 if (desc
&& desc
->action
&& desc
->action
->name
)
85 printk(KERN_INFO
"Resume caused by IRQ %d, %s\n", irq
,
88 printk(KERN_INFO
"Resume caused by IRQ %d\n", irq
);
90 spin_lock(&resume_reason_lock
);
91 if (irqcount
== MAX_WAKEUP_REASON_IRQS
) {
92 spin_unlock(&resume_reason_lock
);
93 printk(KERN_WARNING
"Resume caused by more than %d IRQs\n",
94 MAX_WAKEUP_REASON_IRQS
);
98 irq_list
[irqcount
++] = irq
;
99 spin_unlock(&resume_reason_lock
);
102 int check_wakeup_reason(int irq
)
107 spin_lock(&resume_reason_lock
);
108 for (irq_no
= 0; irq_no
< irqcount
; irq_no
++)
109 if (irq_list
[irq_no
] == irq
) {
113 spin_unlock(&resume_reason_lock
);
117 void log_suspend_abort_reason(const char *fmt
, ...)
121 spin_lock(&resume_reason_lock
);
123 //Suspend abort reason has already been logged.
125 spin_unlock(&resume_reason_lock
);
129 suspend_abort
= true;
131 snprintf(abort_reason
, MAX_SUSPEND_ABORT_LEN
, fmt
, args
);
133 spin_unlock(&resume_reason_lock
);
136 /* Detects a suspend and clears all the previous wake up reasons*/
137 static int wakeup_reason_pm_event(struct notifier_block
*notifier
,
138 unsigned long pm_event
, void *unused
)
141 case PM_SUSPEND_PREPARE
:
142 spin_lock(&resume_reason_lock
);
144 suspend_abort
= false;
145 spin_unlock(&resume_reason_lock
);
153 static struct notifier_block wakeup_reason_pm_notifier_block
= {
154 .notifier_call
= wakeup_reason_pm_event
,
157 /* Initializes the sysfs parameter
158 * registers the pm_event notifier
160 int __init
wakeup_reason_init(void)
164 retval
= register_pm_notifier(&wakeup_reason_pm_notifier_block
);
166 printk(KERN_WARNING
"[%s] failed to register PM notifier %d\n",
169 wakeup_reason
= kobject_create_and_add("wakeup_reasons", kernel_kobj
);
170 if (!wakeup_reason
) {
171 printk(KERN_WARNING
"[%s] failed to create a sysfs kobject\n",
175 retval
= sysfs_create_group(wakeup_reason
, &attr_group
);
177 kobject_put(wakeup_reason
);
178 printk(KERN_WARNING
"[%s] failed to create a sysfs group %d\n",
184 late_initcall(wakeup_reason_init
);
186 #ifdef CONFIG_ARCH_EXYNOS
187 #ifdef CONFIG_SOC_EXYNOS5433
192 struct wakeup_reason_stats
{
194 unsigned int wakeup_count
;
196 static struct wakeup_reason_stats wakeup_reason_stats
[NR_EINT
] = {{0,},};
198 void update_wakeup_reason_stats(int irq
, int eint
)
200 if (eint
>= NR_EINT
) {
201 pr_info("%s : can't update wakeup reason stat infomation\n", __func__
);
205 wakeup_reason_stats
[eint
].irq
= irq
;
206 wakeup_reason_stats
[eint
].wakeup_count
++;
209 #ifdef CONFIG_DEBUG_FS
210 static int wakeup_reason_stats_show(struct seq_file
*s
, void *unused
)
214 seq_puts(s
, "eint_no\tirq\twakeup_count\tname\n");
215 for (i
= 0; i
< NR_EINT
; i
++) {
216 struct irq_desc
*desc
= irq_to_desc(wakeup_reason_stats
[i
].irq
);
217 const char *irq_name
= NULL
;
219 if (!wakeup_reason_stats
[i
].irq
)
222 if (desc
&& desc
->action
&& desc
->action
->name
)
223 irq_name
= desc
->action
->name
;
225 seq_printf(s
, "%d\t%d\t%u\t\t%s\n", i
,
226 wakeup_reason_stats
[i
].irq
,
227 wakeup_reason_stats
[i
].wakeup_count
, irq_name
);
233 static int wakeup_reason_stats_open(struct inode
*inode
, struct file
*file
)
235 return single_open(file
, wakeup_reason_stats_show
, NULL
);
238 static const struct file_operations wakeup_reason_stats_ops
= {
239 .open
= wakeup_reason_stats_open
,
242 .release
= single_release
,
245 static int __init
wakeup_reason_debugfs_init(void)
247 debugfs_create_file("wakeup_reason_stats", S_IFREG
| S_IRUGO
,
248 NULL
, NULL
, &wakeup_reason_stats_ops
);
252 late_initcall(wakeup_reason_debugfs_init
);
253 #endif /* CONFIG_DEBUG_FS */
254 #endif /* CONFIG_ARCH_EXYNOS */