1 // SPDX-License-Identifier: GPL-2.0
3 * Wakeup statistics in sysfs
5 * Copyright (c) 2019 Linux Foundation
7 * Copyright (c) 2019 Google Inc.
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>
21 static struct class *wakeup_class;
23 #define wakeup_attr(_name) \
24 static ssize_t _name##_show(struct device *dev, \
25 struct device_attribute *attr, char *buf) \
27 struct wakeup_source *ws = dev_get_drvdata(dev); \
29 return sprintf(buf, "%lu\n", ws->_name); \
31 static DEVICE_ATTR_RO(_name)
33 wakeup_attr(active_count);
34 wakeup_attr(event_count);
35 wakeup_attr(wakeup_count);
36 wakeup_attr(expire_count);
38 static ssize_t active_time_ms_show(struct device *dev,
39 struct device_attribute *attr, char *buf)
41 struct wakeup_source *ws = dev_get_drvdata(dev);
43 ws->active ? ktime_sub(ktime_get(), ws->last_time) : 0;
45 return sprintf(buf, "%lld\n", ktime_to_ms(active_time));
47 static DEVICE_ATTR_RO(active_time_ms);
49 static ssize_t total_time_ms_show(struct device *dev,
50 struct device_attribute *attr, char *buf)
52 struct wakeup_source *ws = dev_get_drvdata(dev);
54 ktime_t total_time = ws->total_time;
57 active_time = ktime_sub(ktime_get(), ws->last_time);
58 total_time = ktime_add(total_time, active_time);
60 return sprintf(buf, "%lld\n", ktime_to_ms(total_time));
62 static DEVICE_ATTR_RO(total_time_ms);
64 static ssize_t max_time_ms_show(struct device *dev,
65 struct device_attribute *attr, char *buf)
67 struct wakeup_source *ws = dev_get_drvdata(dev);
69 ktime_t max_time = ws->max_time;
72 active_time = ktime_sub(ktime_get(), ws->last_time);
73 if (active_time > max_time)
74 max_time = active_time;
76 return sprintf(buf, "%lld\n", ktime_to_ms(max_time));
78 static DEVICE_ATTR_RO(max_time_ms);
80 static ssize_t last_change_ms_show(struct device *dev,
81 struct device_attribute *attr, char *buf)
83 struct wakeup_source *ws = dev_get_drvdata(dev);
85 return sprintf(buf, "%lld\n", ktime_to_ms(ws->last_time));
87 static DEVICE_ATTR_RO(last_change_ms);
89 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
92 struct wakeup_source *ws = dev_get_drvdata(dev);
94 return sprintf(buf, "%s\n", ws->name);
96 static DEVICE_ATTR_RO(name);
98 static ssize_t prevent_suspend_time_ms_show(struct device *dev,
99 struct device_attribute *attr,
102 struct wakeup_source *ws = dev_get_drvdata(dev);
103 ktime_t prevent_sleep_time = ws->prevent_sleep_time;
105 if (ws->active && ws->autosleep_enabled) {
106 prevent_sleep_time = ktime_add(prevent_sleep_time,
107 ktime_sub(ktime_get(), ws->start_prevent_time));
109 return sprintf(buf, "%lld\n", ktime_to_ms(prevent_sleep_time));
111 static DEVICE_ATTR_RO(prevent_suspend_time_ms);
113 static struct attribute *wakeup_source_attrs[] = {
115 &dev_attr_active_count.attr,
116 &dev_attr_event_count.attr,
117 &dev_attr_wakeup_count.attr,
118 &dev_attr_expire_count.attr,
119 &dev_attr_active_time_ms.attr,
120 &dev_attr_total_time_ms.attr,
121 &dev_attr_max_time_ms.attr,
122 &dev_attr_last_change_ms.attr,
123 &dev_attr_prevent_suspend_time_ms.attr,
126 ATTRIBUTE_GROUPS(wakeup_source);
128 static void device_create_release(struct device *dev)
133 static struct device *wakeup_source_device_create(struct device *parent,
134 struct wakeup_source *ws)
136 struct device *dev = NULL;
137 int retval = -ENODEV;
139 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
145 device_initialize(dev);
146 dev->devt = MKDEV(0, 0);
147 dev->class = wakeup_class;
148 dev->parent = parent;
149 dev->groups = wakeup_source_groups;
150 dev->release = device_create_release;
151 dev_set_drvdata(dev, ws);
152 device_set_pm_not_required(dev);
154 retval = kobject_set_name(&dev->kobj, "wakeup%d", ws->id);
158 retval = device_add(dev);
166 return ERR_PTR(retval);
170 * wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs.
171 * @parent: Device given wakeup source is associated with (or NULL if virtual).
172 * @ws: Wakeup source to be added in sysfs.
174 int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws)
178 dev = wakeup_source_device_create(parent, ws);
187 * pm_wakeup_source_sysfs_add - Add wakeup_source attributes to sysfs
188 * for a device if they're missing.
189 * @parent: Device given wakeup source is associated with
191 int pm_wakeup_source_sysfs_add(struct device *parent)
193 if (!parent->power.wakeup || parent->power.wakeup->dev)
196 return wakeup_source_sysfs_add(parent, parent->power.wakeup);
200 * wakeup_source_sysfs_remove - Remove wakeup_source attributes from sysfs.
201 * @ws: Wakeup source to be removed from sysfs.
203 void wakeup_source_sysfs_remove(struct wakeup_source *ws)
205 device_unregister(ws->dev);
208 static int __init wakeup_sources_sysfs_init(void)
210 wakeup_class = class_create(THIS_MODULE, "wakeup");
212 return PTR_ERR_OR_ZERO(wakeup_class);
214 postcore_initcall(wakeup_sources_sysfs_init);