xref: /linux/drivers/base/power/wakeup_stats.c (revision c89756bcf406af313d191cfe3709e7c175c5b0cd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Wakeup statistics in sysfs
4  *
5  * Copyright (c) 2019 Linux Foundation
6  * Copyright (c) 2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
7  * Copyright (c) 2019 Google Inc.
8  */
9 
10 #include <linux/device.h>
11 #include <linux/idr.h>
12 #include <linux/init.h>
13 #include <linux/kdev_t.h>
14 #include <linux/kernel.h>
15 #include <linux/kobject.h>
16 #include <linux/slab.h>
17 #include <linux/timekeeping.h>
18 
19 #include "power.h"
20 
21 static struct class *wakeup_class;
22 
23 #define wakeup_attr(_name)						\
24 static ssize_t _name##_show(struct device *dev,				\
25 			    struct device_attribute *attr, char *buf)	\
26 {									\
27 	struct wakeup_source *ws = dev_get_drvdata(dev);		\
28 									\
29 	return sysfs_emit(buf, "%lu\n", ws->_name);			\
30 }									\
31 static DEVICE_ATTR_RO(_name)
32 
33 wakeup_attr(active_count);
34 wakeup_attr(event_count);
35 wakeup_attr(wakeup_count);
36 wakeup_attr(expire_count);
37 wakeup_attr(relax_count);
38 
active_time_ms_show(struct device * dev,struct device_attribute * attr,char * buf)39 static ssize_t active_time_ms_show(struct device *dev,
40 				   struct device_attribute *attr, char *buf)
41 {
42 	struct wakeup_source *ws = dev_get_drvdata(dev);
43 	ktime_t active_time =
44 		ws->active ? ktime_sub(ktime_get(), ws->last_time) : 0;
45 
46 	return sysfs_emit(buf, "%lld\n", ktime_to_ms(active_time));
47 }
48 static DEVICE_ATTR_RO(active_time_ms);
49 
total_time_ms_show(struct device * dev,struct device_attribute * attr,char * buf)50 static ssize_t total_time_ms_show(struct device *dev,
51 				  struct device_attribute *attr, char *buf)
52 {
53 	struct wakeup_source *ws = dev_get_drvdata(dev);
54 	ktime_t active_time;
55 	ktime_t total_time = ws->total_time;
56 
57 	if (ws->active) {
58 		active_time = ktime_sub(ktime_get(), ws->last_time);
59 		total_time = ktime_add(total_time, active_time);
60 	}
61 
62 	return sysfs_emit(buf, "%lld\n", ktime_to_ms(total_time));
63 }
64 static DEVICE_ATTR_RO(total_time_ms);
65 
max_time_ms_show(struct device * dev,struct device_attribute * attr,char * buf)66 static ssize_t max_time_ms_show(struct device *dev,
67 				struct device_attribute *attr, char *buf)
68 {
69 	struct wakeup_source *ws = dev_get_drvdata(dev);
70 	ktime_t active_time;
71 	ktime_t max_time = ws->max_time;
72 
73 	if (ws->active) {
74 		active_time = ktime_sub(ktime_get(), ws->last_time);
75 		if (active_time > max_time)
76 			max_time = active_time;
77 	}
78 
79 	return sysfs_emit(buf, "%lld\n", ktime_to_ms(max_time));
80 }
81 static DEVICE_ATTR_RO(max_time_ms);
82 
last_change_ms_show(struct device * dev,struct device_attribute * attr,char * buf)83 static ssize_t last_change_ms_show(struct device *dev,
84 				   struct device_attribute *attr, char *buf)
85 {
86 	struct wakeup_source *ws = dev_get_drvdata(dev);
87 
88 	return sysfs_emit(buf, "%lld\n", ktime_to_ms(ws->last_time));
89 }
90 static DEVICE_ATTR_RO(last_change_ms);
91 
name_show(struct device * dev,struct device_attribute * attr,char * buf)92 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
93 			 char *buf)
94 {
95 	struct wakeup_source *ws = dev_get_drvdata(dev);
96 
97 	return sysfs_emit(buf, "%s\n", ws->name);
98 }
99 static DEVICE_ATTR_RO(name);
100 
prevent_suspend_time_ms_show(struct device * dev,struct device_attribute * attr,char * buf)101 static ssize_t prevent_suspend_time_ms_show(struct device *dev,
102 					    struct device_attribute *attr,
103 					    char *buf)
104 {
105 	struct wakeup_source *ws = dev_get_drvdata(dev);
106 	ktime_t prevent_sleep_time = ws->prevent_sleep_time;
107 
108 	if (ws->active && ws->autosleep_enabled) {
109 		prevent_sleep_time = ktime_add(prevent_sleep_time,
110 			ktime_sub(ktime_get(), ws->start_prevent_time));
111 	}
112 
113 	return sysfs_emit(buf, "%lld\n", ktime_to_ms(prevent_sleep_time));
114 }
115 static DEVICE_ATTR_RO(prevent_suspend_time_ms);
116 
117 static struct attribute *wakeup_source_attrs[] = {
118 	&dev_attr_name.attr,
119 	&dev_attr_active_count.attr,
120 	&dev_attr_event_count.attr,
121 	&dev_attr_wakeup_count.attr,
122 	&dev_attr_expire_count.attr,
123 	&dev_attr_relax_count.attr,
124 	&dev_attr_active_time_ms.attr,
125 	&dev_attr_total_time_ms.attr,
126 	&dev_attr_max_time_ms.attr,
127 	&dev_attr_last_change_ms.attr,
128 	&dev_attr_prevent_suspend_time_ms.attr,
129 	NULL,
130 };
131 ATTRIBUTE_GROUPS(wakeup_source);
132 
device_create_release(struct device * dev)133 static void device_create_release(struct device *dev)
134 {
135 	kfree(dev);
136 }
137 
wakeup_source_device_create(struct device * parent,struct wakeup_source * ws)138 static struct device *wakeup_source_device_create(struct device *parent,
139 						  struct wakeup_source *ws)
140 {
141 	struct device *dev = NULL;
142 	int retval;
143 
144 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
145 	if (!dev) {
146 		retval = -ENOMEM;
147 		goto error;
148 	}
149 
150 	device_initialize(dev);
151 	dev->devt = MKDEV(0, 0);
152 	dev->class = wakeup_class;
153 	dev->parent = parent;
154 	dev->groups = wakeup_source_groups;
155 	dev->release = device_create_release;
156 	dev_set_drvdata(dev, ws);
157 	device_set_pm_not_required(dev);
158 
159 	retval = dev_set_name(dev, "wakeup%d", ws->id);
160 	if (retval)
161 		goto error;
162 
163 	retval = device_add(dev);
164 	if (retval)
165 		goto error;
166 
167 	return dev;
168 
169 error:
170 	put_device(dev);
171 	return ERR_PTR(retval);
172 }
173 
174 /**
175  * wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs.
176  * @parent: Device given wakeup source is associated with (or NULL if virtual).
177  * @ws: Wakeup source to be added in sysfs.
178  */
wakeup_source_sysfs_add(struct device * parent,struct wakeup_source * ws)179 int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)
180 {
181 	struct device *dev;
182 
183 	dev = wakeup_source_device_create(parent, ws);
184 	if (IS_ERR(dev))
185 		return PTR_ERR(dev);
186 	ws->dev = dev;
187 
188 	return 0;
189 }
190 
191 /**
192  * pm_wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs
193  * for a device if they're missing.
194  * @parent: Device given wakeup source is associated with
195  */
pm_wakeup_source_sysfs_add(struct device * parent)196 int pm_wakeup_source_sysfs_add(struct device *parent)
197 {
198 	if (!parent->power.wakeup || parent->power.wakeup->dev)
199 		return 0;
200 
201 	return wakeup_source_sysfs_add(parent, parent->power.wakeup);
202 }
203 
204 /**
205  * wakeup_source_sysfs_remove - Remove wakeup_source attributes from sysfs.
206  * @ws: Wakeup source to be removed from sysfs.
207  */
wakeup_source_sysfs_remove(struct wakeup_source * ws)208 void wakeup_source_sysfs_remove(struct wakeup_source *ws)
209 {
210 	device_unregister(ws->dev);
211 }
212 
wakeup_sources_sysfs_init(void)213 static int __init wakeup_sources_sysfs_init(void)
214 {
215 	wakeup_class = class_create("wakeup");
216 
217 	return PTR_ERR_OR_ZERO(wakeup_class);
218 }
219 postcore_initcall(wakeup_sources_sysfs_init);
220