1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f262f28cSChanwoo Choi /*
3f262f28cSChanwoo Choi * devfreq-event: a framework to provide raw data and events of devfreq devices
4f262f28cSChanwoo Choi *
5f262f28cSChanwoo Choi * Copyright (C) 2015 Samsung Electronics
6f262f28cSChanwoo Choi * Author: Chanwoo Choi <[email protected]>
7f262f28cSChanwoo Choi *
8f262f28cSChanwoo Choi * This driver is based on drivers/devfreq/devfreq.c.
9f262f28cSChanwoo Choi */
10f262f28cSChanwoo Choi
11f262f28cSChanwoo Choi #include <linux/devfreq-event.h>
12f262f28cSChanwoo Choi #include <linux/kernel.h>
13f262f28cSChanwoo Choi #include <linux/err.h>
14f262f28cSChanwoo Choi #include <linux/init.h>
15e32363bcSPaul Gortmaker #include <linux/export.h>
16f262f28cSChanwoo Choi #include <linux/slab.h>
17f262f28cSChanwoo Choi #include <linux/list.h>
18f262f28cSChanwoo Choi #include <linux/of.h>
19f262f28cSChanwoo Choi
20f262f28cSChanwoo Choi static struct class *devfreq_event_class;
21f262f28cSChanwoo Choi
22f262f28cSChanwoo Choi /* The list of all devfreq event list */
23f262f28cSChanwoo Choi static LIST_HEAD(devfreq_event_list);
24f262f28cSChanwoo Choi static DEFINE_MUTEX(devfreq_event_list_lock);
25f262f28cSChanwoo Choi
26f262f28cSChanwoo Choi #define to_devfreq_event(DEV) container_of(DEV, struct devfreq_event_dev, dev)
27f262f28cSChanwoo Choi
28f262f28cSChanwoo Choi /**
29f262f28cSChanwoo Choi * devfreq_event_enable_edev() - Enable the devfreq-event dev and increase
30f262f28cSChanwoo Choi * the enable_count of devfreq-event dev.
31f262f28cSChanwoo Choi * @edev : the devfreq-event device
32f262f28cSChanwoo Choi *
33f262f28cSChanwoo Choi * Note that this function increase the enable_count and enable the
34f262f28cSChanwoo Choi * devfreq-event device. The devfreq-event device should be enabled before
35f262f28cSChanwoo Choi * using it by devfreq device.
36f262f28cSChanwoo Choi */
devfreq_event_enable_edev(struct devfreq_event_dev * edev)37f262f28cSChanwoo Choi int devfreq_event_enable_edev(struct devfreq_event_dev *edev)
38f262f28cSChanwoo Choi {
39f262f28cSChanwoo Choi int ret = 0;
40f262f28cSChanwoo Choi
41f262f28cSChanwoo Choi if (!edev || !edev->desc)
42f262f28cSChanwoo Choi return -EINVAL;
43f262f28cSChanwoo Choi
44f262f28cSChanwoo Choi mutex_lock(&edev->lock);
45f262f28cSChanwoo Choi if (edev->desc->ops && edev->desc->ops->enable
46f262f28cSChanwoo Choi && edev->enable_count == 0) {
47f262f28cSChanwoo Choi ret = edev->desc->ops->enable(edev);
48f262f28cSChanwoo Choi if (ret < 0)
49f262f28cSChanwoo Choi goto err;
50f262f28cSChanwoo Choi }
51f262f28cSChanwoo Choi edev->enable_count++;
52f262f28cSChanwoo Choi err:
53f262f28cSChanwoo Choi mutex_unlock(&edev->lock);
54f262f28cSChanwoo Choi
55f262f28cSChanwoo Choi return ret;
56f262f28cSChanwoo Choi }
57f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_enable_edev);
58f262f28cSChanwoo Choi
59f262f28cSChanwoo Choi /**
60f262f28cSChanwoo Choi * devfreq_event_disable_edev() - Disable the devfreq-event dev and decrease
61f262f28cSChanwoo Choi * the enable_count of the devfreq-event dev.
62f262f28cSChanwoo Choi * @edev : the devfreq-event device
63f262f28cSChanwoo Choi *
64f262f28cSChanwoo Choi * Note that this function decrease the enable_count and disable the
65f262f28cSChanwoo Choi * devfreq-event device. After the devfreq-event device is disabled,
66f262f28cSChanwoo Choi * devfreq device can't use the devfreq-event device for get/set/reset
67f262f28cSChanwoo Choi * operations.
68f262f28cSChanwoo Choi */
devfreq_event_disable_edev(struct devfreq_event_dev * edev)69f262f28cSChanwoo Choi int devfreq_event_disable_edev(struct devfreq_event_dev *edev)
70f262f28cSChanwoo Choi {
71f262f28cSChanwoo Choi int ret = 0;
72f262f28cSChanwoo Choi
73f262f28cSChanwoo Choi if (!edev || !edev->desc)
74f262f28cSChanwoo Choi return -EINVAL;
75f262f28cSChanwoo Choi
76f262f28cSChanwoo Choi mutex_lock(&edev->lock);
77f262f28cSChanwoo Choi if (edev->enable_count <= 0) {
78f262f28cSChanwoo Choi dev_warn(&edev->dev, "unbalanced enable_count\n");
79f262f28cSChanwoo Choi ret = -EIO;
80f262f28cSChanwoo Choi goto err;
81f262f28cSChanwoo Choi }
82f262f28cSChanwoo Choi
83f262f28cSChanwoo Choi if (edev->desc->ops && edev->desc->ops->disable
84f262f28cSChanwoo Choi && edev->enable_count == 1) {
85f262f28cSChanwoo Choi ret = edev->desc->ops->disable(edev);
86f262f28cSChanwoo Choi if (ret < 0)
87f262f28cSChanwoo Choi goto err;
88f262f28cSChanwoo Choi }
89f262f28cSChanwoo Choi edev->enable_count--;
90f262f28cSChanwoo Choi err:
91f262f28cSChanwoo Choi mutex_unlock(&edev->lock);
92f262f28cSChanwoo Choi
93f262f28cSChanwoo Choi return ret;
94f262f28cSChanwoo Choi }
95f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_disable_edev);
96f262f28cSChanwoo Choi
97f262f28cSChanwoo Choi /**
98f262f28cSChanwoo Choi * devfreq_event_is_enabled() - Check whether devfreq-event dev is enabled or
99f262f28cSChanwoo Choi * not.
100f262f28cSChanwoo Choi * @edev : the devfreq-event device
101f262f28cSChanwoo Choi *
102f262f28cSChanwoo Choi * Note that this function check whether devfreq-event dev is enabled or not.
103f262f28cSChanwoo Choi * If return true, the devfreq-event dev is enabeld. If return false, the
104f262f28cSChanwoo Choi * devfreq-event dev is disabled.
105f262f28cSChanwoo Choi */
devfreq_event_is_enabled(struct devfreq_event_dev * edev)106f262f28cSChanwoo Choi bool devfreq_event_is_enabled(struct devfreq_event_dev *edev)
107f262f28cSChanwoo Choi {
108f262f28cSChanwoo Choi bool enabled = false;
109f262f28cSChanwoo Choi
110f262f28cSChanwoo Choi if (!edev || !edev->desc)
111f262f28cSChanwoo Choi return enabled;
112f262f28cSChanwoo Choi
113f262f28cSChanwoo Choi mutex_lock(&edev->lock);
114f262f28cSChanwoo Choi
115f262f28cSChanwoo Choi if (edev->enable_count > 0)
116f262f28cSChanwoo Choi enabled = true;
117f262f28cSChanwoo Choi
118f262f28cSChanwoo Choi mutex_unlock(&edev->lock);
119f262f28cSChanwoo Choi
120f262f28cSChanwoo Choi return enabled;
121f262f28cSChanwoo Choi }
122f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_is_enabled);
123f262f28cSChanwoo Choi
124f262f28cSChanwoo Choi /**
125f262f28cSChanwoo Choi * devfreq_event_set_event() - Set event to devfreq-event dev to start.
126f262f28cSChanwoo Choi * @edev : the devfreq-event device
127f262f28cSChanwoo Choi *
128f262f28cSChanwoo Choi * Note that this function set the event to the devfreq-event device to start
129f262f28cSChanwoo Choi * for getting the event data which could be various event type.
130f262f28cSChanwoo Choi */
devfreq_event_set_event(struct devfreq_event_dev * edev)131f262f28cSChanwoo Choi int devfreq_event_set_event(struct devfreq_event_dev *edev)
132f262f28cSChanwoo Choi {
133f262f28cSChanwoo Choi int ret;
134f262f28cSChanwoo Choi
135f262f28cSChanwoo Choi if (!edev || !edev->desc)
136f262f28cSChanwoo Choi return -EINVAL;
137f262f28cSChanwoo Choi
138f262f28cSChanwoo Choi if (!edev->desc->ops || !edev->desc->ops->set_event)
139f262f28cSChanwoo Choi return -EINVAL;
140f262f28cSChanwoo Choi
141f262f28cSChanwoo Choi if (!devfreq_event_is_enabled(edev))
142f262f28cSChanwoo Choi return -EPERM;
143f262f28cSChanwoo Choi
144f262f28cSChanwoo Choi mutex_lock(&edev->lock);
145f262f28cSChanwoo Choi ret = edev->desc->ops->set_event(edev);
146f262f28cSChanwoo Choi mutex_unlock(&edev->lock);
147f262f28cSChanwoo Choi
148f262f28cSChanwoo Choi return ret;
149f262f28cSChanwoo Choi }
150f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_set_event);
151f262f28cSChanwoo Choi
152f262f28cSChanwoo Choi /**
153f262f28cSChanwoo Choi * devfreq_event_get_event() - Get {load|total}_count from devfreq-event dev.
154f262f28cSChanwoo Choi * @edev : the devfreq-event device
155f262f28cSChanwoo Choi * @edata : the calculated data of devfreq-event device
156f262f28cSChanwoo Choi *
157f262f28cSChanwoo Choi * Note that this function get the calculated event data from devfreq-event dev
158f262f28cSChanwoo Choi * after stoping the progress of whole sequence of devfreq-event dev.
159f262f28cSChanwoo Choi */
devfreq_event_get_event(struct devfreq_event_dev * edev,struct devfreq_event_data * edata)160f262f28cSChanwoo Choi int devfreq_event_get_event(struct devfreq_event_dev *edev,
161f262f28cSChanwoo Choi struct devfreq_event_data *edata)
162f262f28cSChanwoo Choi {
163f262f28cSChanwoo Choi int ret;
164f262f28cSChanwoo Choi
165f262f28cSChanwoo Choi if (!edev || !edev->desc)
166f262f28cSChanwoo Choi return -EINVAL;
167f262f28cSChanwoo Choi
168f262f28cSChanwoo Choi if (!edev->desc->ops || !edev->desc->ops->get_event)
169f262f28cSChanwoo Choi return -EINVAL;
170f262f28cSChanwoo Choi
171f262f28cSChanwoo Choi if (!devfreq_event_is_enabled(edev))
172f262f28cSChanwoo Choi return -EINVAL;
173f262f28cSChanwoo Choi
174f262f28cSChanwoo Choi edata->total_count = edata->load_count = 0;
175f262f28cSChanwoo Choi
176f262f28cSChanwoo Choi mutex_lock(&edev->lock);
177f262f28cSChanwoo Choi ret = edev->desc->ops->get_event(edev, edata);
178f262f28cSChanwoo Choi if (ret < 0)
179f262f28cSChanwoo Choi edata->total_count = edata->load_count = 0;
180f262f28cSChanwoo Choi mutex_unlock(&edev->lock);
181f262f28cSChanwoo Choi
182f262f28cSChanwoo Choi return ret;
183f262f28cSChanwoo Choi }
184f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_get_event);
185f262f28cSChanwoo Choi
186f262f28cSChanwoo Choi /**
187f262f28cSChanwoo Choi * devfreq_event_reset_event() - Reset all opeations of devfreq-event dev.
188f262f28cSChanwoo Choi * @edev : the devfreq-event device
189f262f28cSChanwoo Choi *
190f262f28cSChanwoo Choi * Note that this function stop all operations of devfreq-event dev and reset
191f262f28cSChanwoo Choi * the current event data to make the devfreq-event device into initial state.
192f262f28cSChanwoo Choi */
devfreq_event_reset_event(struct devfreq_event_dev * edev)193f262f28cSChanwoo Choi int devfreq_event_reset_event(struct devfreq_event_dev *edev)
194f262f28cSChanwoo Choi {
195f262f28cSChanwoo Choi int ret = 0;
196f262f28cSChanwoo Choi
197f262f28cSChanwoo Choi if (!edev || !edev->desc)
198f262f28cSChanwoo Choi return -EINVAL;
199f262f28cSChanwoo Choi
200f262f28cSChanwoo Choi if (!devfreq_event_is_enabled(edev))
201f262f28cSChanwoo Choi return -EPERM;
202f262f28cSChanwoo Choi
203f262f28cSChanwoo Choi mutex_lock(&edev->lock);
204f262f28cSChanwoo Choi if (edev->desc->ops && edev->desc->ops->reset)
205f262f28cSChanwoo Choi ret = edev->desc->ops->reset(edev);
206f262f28cSChanwoo Choi mutex_unlock(&edev->lock);
207f262f28cSChanwoo Choi
208f262f28cSChanwoo Choi return ret;
209f262f28cSChanwoo Choi }
210f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_reset_event);
211f262f28cSChanwoo Choi
212f262f28cSChanwoo Choi /**
213f262f28cSChanwoo Choi * devfreq_event_get_edev_by_phandle() - Get the devfreq-event dev from
214f262f28cSChanwoo Choi * devicetree.
215f262f28cSChanwoo Choi * @dev : the pointer to the given device
21602bdbf7dSChanwoo Choi * @phandle_name: name of property holding a phandle value
217f262f28cSChanwoo Choi * @index : the index into list of devfreq-event device
218f262f28cSChanwoo Choi *
219f262f28cSChanwoo Choi * Note that this function return the pointer of devfreq-event device.
220f262f28cSChanwoo Choi */
devfreq_event_get_edev_by_phandle(struct device * dev,const char * phandle_name,int index)221f262f28cSChanwoo Choi struct devfreq_event_dev *devfreq_event_get_edev_by_phandle(struct device *dev,
22202bdbf7dSChanwoo Choi const char *phandle_name, int index)
223f262f28cSChanwoo Choi {
224f262f28cSChanwoo Choi struct device_node *node;
225f262f28cSChanwoo Choi struct devfreq_event_dev *edev;
226f262f28cSChanwoo Choi
22702bdbf7dSChanwoo Choi if (!dev->of_node || !phandle_name)
228f262f28cSChanwoo Choi return ERR_PTR(-EINVAL);
229f262f28cSChanwoo Choi
23002bdbf7dSChanwoo Choi node = of_parse_phandle(dev->of_node, phandle_name, index);
231df678ff7SChanwoo Choi if (!node)
232f262f28cSChanwoo Choi return ERR_PTR(-ENODEV);
233f262f28cSChanwoo Choi
234f262f28cSChanwoo Choi mutex_lock(&devfreq_event_list_lock);
235f262f28cSChanwoo Choi list_for_each_entry(edev, &devfreq_event_list, node) {
236ad44c2fdSye xingchen if (edev->dev.parent && device_match_of_node(edev->dev.parent, node))
23719cf91d0SChanwoo Choi goto out;
23819cf91d0SChanwoo Choi }
23919cf91d0SChanwoo Choi
24019cf91d0SChanwoo Choi list_for_each_entry(edev, &devfreq_event_list, node) {
2410d00a239SRob Herring if (of_node_name_eq(node, edev->desc->name))
242f262f28cSChanwoo Choi goto out;
243f262f28cSChanwoo Choi }
244f262f28cSChanwoo Choi edev = NULL;
245f262f28cSChanwoo Choi out:
246f262f28cSChanwoo Choi mutex_unlock(&devfreq_event_list_lock);
247f262f28cSChanwoo Choi of_node_put(node);
248*ed33fbb5SMarkus Elfring if (!edev)
249f262f28cSChanwoo Choi return ERR_PTR(-ENODEV);
250f262f28cSChanwoo Choi
251f262f28cSChanwoo Choi return edev;
252f262f28cSChanwoo Choi }
253f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle);
254f262f28cSChanwoo Choi
255f262f28cSChanwoo Choi /**
256f262f28cSChanwoo Choi * devfreq_event_get_edev_count() - Get the count of devfreq-event dev
257f262f28cSChanwoo Choi * @dev : the pointer to the given device
25802bdbf7dSChanwoo Choi * @phandle_name: name of property holding a phandle value
259f262f28cSChanwoo Choi *
260f262f28cSChanwoo Choi * Note that this function return the count of devfreq-event devices.
261f262f28cSChanwoo Choi */
devfreq_event_get_edev_count(struct device * dev,const char * phandle_name)26202bdbf7dSChanwoo Choi int devfreq_event_get_edev_count(struct device *dev, const char *phandle_name)
263f262f28cSChanwoo Choi {
264f262f28cSChanwoo Choi int count;
265f262f28cSChanwoo Choi
26602bdbf7dSChanwoo Choi if (!dev->of_node || !phandle_name) {
267f262f28cSChanwoo Choi dev_err(dev, "device does not have a device node entry\n");
268f262f28cSChanwoo Choi return -EINVAL;
269f262f28cSChanwoo Choi }
270f262f28cSChanwoo Choi
27102bdbf7dSChanwoo Choi count = of_property_count_elems_of_size(dev->of_node, phandle_name,
272f262f28cSChanwoo Choi sizeof(u32));
273f262f28cSChanwoo Choi if (count < 0) {
274f262f28cSChanwoo Choi dev_err(dev,
275e8305d40SRob Herring "failed to get the count of devfreq-event in %pOF node\n",
276e8305d40SRob Herring dev->of_node);
277f262f28cSChanwoo Choi return count;
278f262f28cSChanwoo Choi }
279f262f28cSChanwoo Choi
280f262f28cSChanwoo Choi return count;
281f262f28cSChanwoo Choi }
282f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_get_edev_count);
283f262f28cSChanwoo Choi
devfreq_event_release_edev(struct device * dev)284f262f28cSChanwoo Choi static void devfreq_event_release_edev(struct device *dev)
285f262f28cSChanwoo Choi {
286f262f28cSChanwoo Choi struct devfreq_event_dev *edev = to_devfreq_event(dev);
287f262f28cSChanwoo Choi
288f262f28cSChanwoo Choi kfree(edev);
289f262f28cSChanwoo Choi }
290f262f28cSChanwoo Choi
291f262f28cSChanwoo Choi /**
292f262f28cSChanwoo Choi * devfreq_event_add_edev() - Add new devfreq-event device.
293f262f28cSChanwoo Choi * @dev : the device owning the devfreq-event device being created
294ada79596SKieran Bingham * @desc : the devfreq-event device's descriptor which include essential
295f262f28cSChanwoo Choi * data for devfreq-event device.
296f262f28cSChanwoo Choi *
297f262f28cSChanwoo Choi * Note that this function add new devfreq-event device to devfreq-event class
298f262f28cSChanwoo Choi * list and register the device of the devfreq-event device.
299f262f28cSChanwoo Choi */
devfreq_event_add_edev(struct device * dev,struct devfreq_event_desc * desc)300f262f28cSChanwoo Choi struct devfreq_event_dev *devfreq_event_add_edev(struct device *dev,
301f262f28cSChanwoo Choi struct devfreq_event_desc *desc)
302f262f28cSChanwoo Choi {
303f262f28cSChanwoo Choi struct devfreq_event_dev *edev;
304775fa8c3SChanwoo Choi static atomic_t event_no = ATOMIC_INIT(-1);
305f262f28cSChanwoo Choi int ret;
306f262f28cSChanwoo Choi
307f262f28cSChanwoo Choi if (!dev || !desc)
308f262f28cSChanwoo Choi return ERR_PTR(-EINVAL);
309f262f28cSChanwoo Choi
310f262f28cSChanwoo Choi if (!desc->name || !desc->ops)
311f262f28cSChanwoo Choi return ERR_PTR(-EINVAL);
312f262f28cSChanwoo Choi
313f262f28cSChanwoo Choi if (!desc->ops->set_event || !desc->ops->get_event)
314f262f28cSChanwoo Choi return ERR_PTR(-EINVAL);
315f262f28cSChanwoo Choi
316f262f28cSChanwoo Choi edev = kzalloc(sizeof(struct devfreq_event_dev), GFP_KERNEL);
317f262f28cSChanwoo Choi if (!edev)
318f262f28cSChanwoo Choi return ERR_PTR(-ENOMEM);
319f262f28cSChanwoo Choi
320f262f28cSChanwoo Choi mutex_init(&edev->lock);
321f262f28cSChanwoo Choi edev->desc = desc;
322f262f28cSChanwoo Choi edev->enable_count = 0;
323f262f28cSChanwoo Choi edev->dev.parent = dev;
324f262f28cSChanwoo Choi edev->dev.class = devfreq_event_class;
325f262f28cSChanwoo Choi edev->dev.release = devfreq_event_release_edev;
326f262f28cSChanwoo Choi
327775fa8c3SChanwoo Choi dev_set_name(&edev->dev, "event%d", atomic_inc_return(&event_no));
328f262f28cSChanwoo Choi ret = device_register(&edev->dev);
329f262f28cSChanwoo Choi if (ret < 0) {
330f262f28cSChanwoo Choi put_device(&edev->dev);
331f262f28cSChanwoo Choi return ERR_PTR(ret);
332f262f28cSChanwoo Choi }
333f262f28cSChanwoo Choi dev_set_drvdata(&edev->dev, edev);
334f262f28cSChanwoo Choi
335f262f28cSChanwoo Choi INIT_LIST_HEAD(&edev->node);
336f262f28cSChanwoo Choi
337f262f28cSChanwoo Choi mutex_lock(&devfreq_event_list_lock);
338f262f28cSChanwoo Choi list_add(&edev->node, &devfreq_event_list);
339f262f28cSChanwoo Choi mutex_unlock(&devfreq_event_list_lock);
340f262f28cSChanwoo Choi
341f262f28cSChanwoo Choi return edev;
342f262f28cSChanwoo Choi }
343f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_add_edev);
344f262f28cSChanwoo Choi
345f262f28cSChanwoo Choi /**
346f262f28cSChanwoo Choi * devfreq_event_remove_edev() - Remove the devfreq-event device registered.
34754cb5740SRandy Dunlap * @edev : the devfreq-event device
348f262f28cSChanwoo Choi *
34954cb5740SRandy Dunlap * Note that this function removes the registered devfreq-event device.
350f262f28cSChanwoo Choi */
devfreq_event_remove_edev(struct devfreq_event_dev * edev)351f262f28cSChanwoo Choi int devfreq_event_remove_edev(struct devfreq_event_dev *edev)
352f262f28cSChanwoo Choi {
353f262f28cSChanwoo Choi if (!edev)
354f262f28cSChanwoo Choi return -EINVAL;
355f262f28cSChanwoo Choi
356f262f28cSChanwoo Choi WARN_ON(edev->enable_count);
357f262f28cSChanwoo Choi
358f262f28cSChanwoo Choi mutex_lock(&devfreq_event_list_lock);
359f262f28cSChanwoo Choi list_del(&edev->node);
360f262f28cSChanwoo Choi mutex_unlock(&devfreq_event_list_lock);
361f262f28cSChanwoo Choi
362f262f28cSChanwoo Choi device_unregister(&edev->dev);
363f262f28cSChanwoo Choi
364f262f28cSChanwoo Choi return 0;
365f262f28cSChanwoo Choi }
366f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devfreq_event_remove_edev);
367f262f28cSChanwoo Choi
devm_devfreq_event_match(struct device * dev,void * res,void * data)368f262f28cSChanwoo Choi static int devm_devfreq_event_match(struct device *dev, void *res, void *data)
369f262f28cSChanwoo Choi {
370f262f28cSChanwoo Choi struct devfreq_event_dev **r = res;
371f262f28cSChanwoo Choi
372f262f28cSChanwoo Choi if (WARN_ON(!r || !*r))
373f262f28cSChanwoo Choi return 0;
374f262f28cSChanwoo Choi
375f262f28cSChanwoo Choi return *r == data;
376f262f28cSChanwoo Choi }
377f262f28cSChanwoo Choi
devm_devfreq_event_release(struct device * dev,void * res)378f262f28cSChanwoo Choi static void devm_devfreq_event_release(struct device *dev, void *res)
379f262f28cSChanwoo Choi {
380f262f28cSChanwoo Choi devfreq_event_remove_edev(*(struct devfreq_event_dev **)res);
381f262f28cSChanwoo Choi }
382f262f28cSChanwoo Choi
383f262f28cSChanwoo Choi /**
384f262f28cSChanwoo Choi * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev()
385f262f28cSChanwoo Choi * @dev : the device owning the devfreq-event device being created
386ada79596SKieran Bingham * @desc : the devfreq-event device's descriptor which include essential
387f262f28cSChanwoo Choi * data for devfreq-event device.
388f262f28cSChanwoo Choi *
389f262f28cSChanwoo Choi * Note that this function manages automatically the memory of devfreq-event
390f262f28cSChanwoo Choi * device using device resource management and simplify the free operation
391f262f28cSChanwoo Choi * for memory of devfreq-event device.
392f262f28cSChanwoo Choi */
devm_devfreq_event_add_edev(struct device * dev,struct devfreq_event_desc * desc)393f262f28cSChanwoo Choi struct devfreq_event_dev *devm_devfreq_event_add_edev(struct device *dev,
394f262f28cSChanwoo Choi struct devfreq_event_desc *desc)
395f262f28cSChanwoo Choi {
396f262f28cSChanwoo Choi struct devfreq_event_dev **ptr, *edev;
397f262f28cSChanwoo Choi
398e54916c6SChanwoo Choi ptr = devres_alloc(devm_devfreq_event_release, sizeof(*ptr),
399e54916c6SChanwoo Choi GFP_KERNEL);
400f262f28cSChanwoo Choi if (!ptr)
401f262f28cSChanwoo Choi return ERR_PTR(-ENOMEM);
402f262f28cSChanwoo Choi
403f262f28cSChanwoo Choi edev = devfreq_event_add_edev(dev, desc);
404f262f28cSChanwoo Choi if (IS_ERR(edev)) {
405f262f28cSChanwoo Choi devres_free(ptr);
406f262f28cSChanwoo Choi return ERR_PTR(-ENOMEM);
407f262f28cSChanwoo Choi }
408f262f28cSChanwoo Choi
409f262f28cSChanwoo Choi *ptr = edev;
410f262f28cSChanwoo Choi devres_add(dev, ptr);
411f262f28cSChanwoo Choi
412f262f28cSChanwoo Choi return edev;
413f262f28cSChanwoo Choi }
414f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev);
415f262f28cSChanwoo Choi
416f262f28cSChanwoo Choi /**
417f262f28cSChanwoo Choi * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev()
418f262f28cSChanwoo Choi * @dev : the device owning the devfreq-event device being created
419f262f28cSChanwoo Choi * @edev : the devfreq-event device
420f262f28cSChanwoo Choi *
421f262f28cSChanwoo Choi * Note that this function manages automatically the memory of devfreq-event
422f262f28cSChanwoo Choi * device using device resource management.
423f262f28cSChanwoo Choi */
devm_devfreq_event_remove_edev(struct device * dev,struct devfreq_event_dev * edev)424f262f28cSChanwoo Choi void devm_devfreq_event_remove_edev(struct device *dev,
425f262f28cSChanwoo Choi struct devfreq_event_dev *edev)
426f262f28cSChanwoo Choi {
427f262f28cSChanwoo Choi WARN_ON(devres_release(dev, devm_devfreq_event_release,
428f262f28cSChanwoo Choi devm_devfreq_event_match, edev));
429f262f28cSChanwoo Choi }
430f262f28cSChanwoo Choi EXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev);
431f262f28cSChanwoo Choi
432f262f28cSChanwoo Choi /*
433f262f28cSChanwoo Choi * Device attributes for devfreq-event class.
434f262f28cSChanwoo Choi */
name_show(struct device * dev,struct device_attribute * attr,char * buf)435f262f28cSChanwoo Choi static ssize_t name_show(struct device *dev, struct device_attribute *attr,
436f262f28cSChanwoo Choi char *buf)
437f262f28cSChanwoo Choi {
438f262f28cSChanwoo Choi struct devfreq_event_dev *edev = to_devfreq_event(dev);
439f262f28cSChanwoo Choi
440f262f28cSChanwoo Choi if (!edev || !edev->desc)
441f262f28cSChanwoo Choi return -EINVAL;
442f262f28cSChanwoo Choi
443f262f28cSChanwoo Choi return sprintf(buf, "%s\n", edev->desc->name);
444f262f28cSChanwoo Choi }
445f262f28cSChanwoo Choi static DEVICE_ATTR_RO(name);
446f262f28cSChanwoo Choi
enable_count_show(struct device * dev,struct device_attribute * attr,char * buf)447f262f28cSChanwoo Choi static ssize_t enable_count_show(struct device *dev,
448f262f28cSChanwoo Choi struct device_attribute *attr, char *buf)
449f262f28cSChanwoo Choi {
450f262f28cSChanwoo Choi struct devfreq_event_dev *edev = to_devfreq_event(dev);
451f262f28cSChanwoo Choi
452f262f28cSChanwoo Choi if (!edev || !edev->desc)
453f262f28cSChanwoo Choi return -EINVAL;
454f262f28cSChanwoo Choi
455f262f28cSChanwoo Choi return sprintf(buf, "%d\n", edev->enable_count);
456f262f28cSChanwoo Choi }
457f262f28cSChanwoo Choi static DEVICE_ATTR_RO(enable_count);
458f262f28cSChanwoo Choi
459f262f28cSChanwoo Choi static struct attribute *devfreq_event_attrs[] = {
460f262f28cSChanwoo Choi &dev_attr_name.attr,
461f262f28cSChanwoo Choi &dev_attr_enable_count.attr,
462f262f28cSChanwoo Choi NULL,
463f262f28cSChanwoo Choi };
464f262f28cSChanwoo Choi ATTRIBUTE_GROUPS(devfreq_event);
465f262f28cSChanwoo Choi
devfreq_event_init(void)466f262f28cSChanwoo Choi static int __init devfreq_event_init(void)
467f262f28cSChanwoo Choi {
4681aaba11dSGreg Kroah-Hartman devfreq_event_class = class_create("devfreq-event");
469f262f28cSChanwoo Choi if (IS_ERR(devfreq_event_class)) {
470f262f28cSChanwoo Choi pr_err("%s: couldn't create class\n", __FILE__);
471f262f28cSChanwoo Choi return PTR_ERR(devfreq_event_class);
472f262f28cSChanwoo Choi }
473f262f28cSChanwoo Choi
474f262f28cSChanwoo Choi devfreq_event_class->dev_groups = devfreq_event_groups;
475f262f28cSChanwoo Choi
476f262f28cSChanwoo Choi return 0;
477f262f28cSChanwoo Choi }
478f262f28cSChanwoo Choi subsys_initcall(devfreq_event_init);
479