blob: 82e69fe52d0bd092bbae6e1dc459fada3557f9a4 [file] [log] [blame]
Ruchi Kandoi6acefbe2014-02-19 15:30:47 -08001/*
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
32static int irq_list[MAX_WAKEUP_REASON_IRQS];
33static int irq_count;
34static struct kobject *wakeup_reason;
35static spinlock_t resume_reason_lock;
36
37static ssize_t reason_show(struct kobject *kobj, struct kobj_attribute *attr,
Ruchi Kandoi11351222014-02-20 19:47:38 -080038 char *buf)
Ruchi Kandoi6acefbe2014-02-19 15:30:47 -080039{
40 int irq_no, buf_offset = 0;
41 struct irq_desc *desc;
42 spin_lock(&resume_reason_lock);
43 for (irq_no = 0; irq_no < irq_count; irq_no++) {
44 desc = irq_to_desc(irq_list[irq_no]);
45 if (desc && desc->action && desc->action->name)
46 buf_offset += sprintf(buf + buf_offset, "%d %s\n",
47 irq_list[irq_no], desc->action->name);
48 else
49 buf_offset += sprintf(buf + buf_offset, "%d\n",
50 irq_list[irq_no]);
51 }
52 spin_unlock(&resume_reason_lock);
53 return buf_offset;
54}
55
56static struct kobj_attribute resume_reason = __ATTR(last_resume_reason, 0666,
57 reason_show, NULL);
58
59static struct attribute *attrs[] = {
60 &resume_reason.attr,
61 NULL,
62};
63static struct attribute_group attr_group = {
64 .attrs = attrs,
65};
66
67/*
68 * logs all the wake up reasons to the kernel
69 * stores the irqs to expose them to the userspace via sysfs
70 */
71void log_wakeup_reason(int irq)
72{
73 struct irq_desc *desc;
74 desc = irq_to_desc(irq);
75 if (desc && desc->action && desc->action->name)
76 printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq,
77 desc->action->name);
78 else
79 printk(KERN_INFO "Resume caused by IRQ %d\n", irq);
80
81 spin_lock(&resume_reason_lock);
82 irq_list[irq_count++] = irq;
83 spin_unlock(&resume_reason_lock);
84}
85
86/* Detects a suspend and clears all the previous wake up reasons*/
87static int wakeup_reason_pm_event(struct notifier_block *notifier,
88 unsigned long pm_event, void *unused)
89{
90 switch (pm_event) {
91 case PM_SUSPEND_PREPARE:
92 spin_lock(&resume_reason_lock);
93 irq_count = 0;
94 spin_unlock(&resume_reason_lock);
95 break;
96 default:
97 break;
98 }
99 return NOTIFY_DONE;
100}
101
102static struct notifier_block wakeup_reason_pm_notifier_block = {
103 .notifier_call = wakeup_reason_pm_event,
104};
105
106/* Initializes the sysfs parameter
107 * registers the pm_event notifier
108 */
Ruchi Kandoi11351222014-02-20 19:47:38 -0800109int __init wakeup_reason_init(void)
Ruchi Kandoi6acefbe2014-02-19 15:30:47 -0800110{
111 int retval;
112 spin_lock_init(&resume_reason_lock);
113 retval = register_pm_notifier(&wakeup_reason_pm_notifier_block);
114 if (retval)
115 printk(KERN_WARNING "[%s] failed to register PM notifier %d\n",
116 __func__, retval);
117
118 wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj);
119 if (!wakeup_reason) {
120 printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n",
121 __func__);
Ruchi Kandoi11351222014-02-20 19:47:38 -0800122 return 1;
Ruchi Kandoi6acefbe2014-02-19 15:30:47 -0800123 }
124 retval = sysfs_create_group(wakeup_reason, &attr_group);
125 if (retval) {
126 kobject_put(wakeup_reason);
127 printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n",
128 __func__, retval);
129 }
Ruchi Kandoi11351222014-02-20 19:47:38 -0800130 return 0;
Ruchi Kandoi6acefbe2014-02-19 15:30:47 -0800131}
132
133late_initcall(wakeup_reason_init);