1 // SPDX-License-Identifier: GPL-2.0-only
3 * devfreq-event: a framework to provide raw data and events of devfreq devices
5 * Copyright (C) 2015 Samsung Electronics
6 * Author: Chanwoo Choi <cw00.choi@samsung.com>
8 * This driver is based on drivers/devfreq/devfreq.c.
11 #include <linux/devfreq-event.h>
12 #include <linux/kernel.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
20 static struct class *devfreq_event_class;
22 /* The list of all devfreq event list */
23 static LIST_HEAD(devfreq_event_list);
24 static DEFINE_MUTEX(devfreq_event_list_lock);
26 #define to_devfreq_event(DEV) container_of(DEV, struct devfreq_event_dev, dev)
29 * devfreq_event_enable_edev() - Enable the devfreq-event dev and increase
30 * the enable_count of devfreq-event dev.
31 * @edev : the devfreq-event device
33 * Note that this function increase the enable_count and enable the
34 * devfreq-event device. The devfreq-event device should be enabled before
35 * using it by devfreq device.
37 int devfreq_event_enable_edev(struct devfreq_event_dev *edev)
41 if (!edev || !edev->desc)
44 mutex_lock(&edev->lock);
45 if (edev->desc->ops && edev->desc->ops->enable
46 && edev->enable_count == 0) {
47 ret = edev->desc->ops->enable(edev);
53 mutex_unlock(&edev->lock);
57 EXPORT_SYMBOL_GPL(devfreq_event_enable_edev);
60 * devfreq_event_disable_edev() - Disable the devfreq-event dev and decrease
61 * the enable_count of the devfreq-event dev.
62 * @edev : the devfreq-event device
64 * Note that this function decrease the enable_count and disable the
65 * devfreq-event device. After the devfreq-event device is disabled,
66 * devfreq device can't use the devfreq-event device for get/set/reset
69 int devfreq_event_disable_edev(struct devfreq_event_dev *edev)
73 if (!edev || !edev->desc)
76 mutex_lock(&edev->lock);
77 if (edev->enable_count <= 0) {
78 dev_warn(&edev->dev, "unbalanced enable_count\n");
83 if (edev->desc->ops && edev->desc->ops->disable
84 && edev->enable_count == 1) {
85 ret = edev->desc->ops->disable(edev);
91 mutex_unlock(&edev->lock);
95 EXPORT_SYMBOL_GPL(devfreq_event_disable_edev);
98 * devfreq_event_is_enabled() - Check whether devfreq-event dev is enabled or
100 * @edev : the devfreq-event device
102 * Note that this function check whether devfreq-event dev is enabled or not.
103 * If return true, the devfreq-event dev is enabeld. If return false, the
104 * devfreq-event dev is disabled.
106 bool devfreq_event_is_enabled(struct devfreq_event_dev *edev)
108 bool enabled = false;
110 if (!edev || !edev->desc)
113 mutex_lock(&edev->lock);
115 if (edev->enable_count > 0)
118 mutex_unlock(&edev->lock);
122 EXPORT_SYMBOL_GPL(devfreq_event_is_enabled);
125 * devfreq_event_set_event() - Set event to devfreq-event dev to start.
126 * @edev : the devfreq-event device
128 * Note that this function set the event to the devfreq-event device to start
129 * for getting the event data which could be various event type.
131 int devfreq_event_set_event(struct devfreq_event_dev *edev)
135 if (!edev || !edev->desc)
138 if (!edev->desc->ops || !edev->desc->ops->set_event)
141 if (!devfreq_event_is_enabled(edev))
144 mutex_lock(&edev->lock);
145 ret = edev->desc->ops->set_event(edev);
146 mutex_unlock(&edev->lock);
150 EXPORT_SYMBOL_GPL(devfreq_event_set_event);
153 * devfreq_event_get_event() - Get {load|total}_count from devfreq-event dev.
154 * @edev : the devfreq-event device
155 * @edata : the calculated data of devfreq-event device
157 * Note that this function get the calculated event data from devfreq-event dev
158 * after stoping the progress of whole sequence of devfreq-event dev.
160 int devfreq_event_get_event(struct devfreq_event_dev *edev,
161 struct devfreq_event_data *edata)
165 if (!edev || !edev->desc)
168 if (!edev->desc->ops || !edev->desc->ops->get_event)
171 if (!devfreq_event_is_enabled(edev))
174 edata->total_count = edata->load_count = 0;
176 mutex_lock(&edev->lock);
177 ret = edev->desc->ops->get_event(edev, edata);
179 edata->total_count = edata->load_count = 0;
180 mutex_unlock(&edev->lock);
184 EXPORT_SYMBOL_GPL(devfreq_event_get_event);
187 * devfreq_event_reset_event() - Reset all opeations of devfreq-event dev.
188 * @edev : the devfreq-event device
190 * Note that this function stop all operations of devfreq-event dev and reset
191 * the current event data to make the devfreq-event device into initial state.
193 int devfreq_event_reset_event(struct devfreq_event_dev *edev)
197 if (!edev || !edev->desc)
200 if (!devfreq_event_is_enabled(edev))
203 mutex_lock(&edev->lock);
204 if (edev->desc->ops && edev->desc->ops->reset)
205 ret = edev->desc->ops->reset(edev);
206 mutex_unlock(&edev->lock);
210 EXPORT_SYMBOL_GPL(devfreq_event_reset_event);
213 * devfreq_event_get_edev_by_phandle() - Get the devfreq-event dev from
215 * @dev : the pointer to the given device
216 * @phandle_name: name of property holding a phandle value
217 * @index : the index into list of devfreq-event device
219 * Note that this function return the pointer of devfreq-event device.
221 struct devfreq_event_dev *devfreq_event_get_edev_by_phandle(struct device *dev,
222 const char *phandle_name, int index)
224 struct device_node *node;
225 struct devfreq_event_dev *edev;
227 if (!dev->of_node || !phandle_name)
228 return ERR_PTR(-EINVAL);
230 node = of_parse_phandle(dev->of_node, phandle_name, index);
232 return ERR_PTR(-ENODEV);
234 mutex_lock(&devfreq_event_list_lock);
235 list_for_each_entry(edev, &devfreq_event_list, node) {
236 if (edev->dev.parent && edev->dev.parent->of_node == node)
240 list_for_each_entry(edev, &devfreq_event_list, node) {
241 if (of_node_name_eq(node, edev->desc->name))
246 mutex_unlock(&devfreq_event_list_lock);
250 return ERR_PTR(-ENODEV);
257 EXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle);
260 * devfreq_event_get_edev_count() - Get the count of devfreq-event dev
261 * @dev : the pointer to the given device
262 * @phandle_name: name of property holding a phandle value
264 * Note that this function return the count of devfreq-event devices.
266 int devfreq_event_get_edev_count(struct device *dev, const char *phandle_name)
270 if (!dev->of_node || !phandle_name) {
271 dev_err(dev, "device does not have a device node entry\n");
275 count = of_property_count_elems_of_size(dev->of_node, phandle_name,
279 "failed to get the count of devfreq-event in %pOF node\n",
286 EXPORT_SYMBOL_GPL(devfreq_event_get_edev_count);
288 static void devfreq_event_release_edev(struct device *dev)
290 struct devfreq_event_dev *edev = to_devfreq_event(dev);
296 * devfreq_event_add_edev() - Add new devfreq-event device.
297 * @dev : the device owning the devfreq-event device being created
298 * @desc : the devfreq-event device's descriptor which include essential
299 * data for devfreq-event device.
301 * Note that this function add new devfreq-event device to devfreq-event class
302 * list and register the device of the devfreq-event device.
304 struct devfreq_event_dev *devfreq_event_add_edev(struct device *dev,
305 struct devfreq_event_desc *desc)
307 struct devfreq_event_dev *edev;
308 static atomic_t event_no = ATOMIC_INIT(-1);
312 return ERR_PTR(-EINVAL);
314 if (!desc->name || !desc->ops)
315 return ERR_PTR(-EINVAL);
317 if (!desc->ops->set_event || !desc->ops->get_event)
318 return ERR_PTR(-EINVAL);
320 edev = kzalloc(sizeof(struct devfreq_event_dev), GFP_KERNEL);
322 return ERR_PTR(-ENOMEM);
324 mutex_init(&edev->lock);
326 edev->enable_count = 0;
327 edev->dev.parent = dev;
328 edev->dev.class = devfreq_event_class;
329 edev->dev.release = devfreq_event_release_edev;
331 dev_set_name(&edev->dev, "event%d", atomic_inc_return(&event_no));
332 ret = device_register(&edev->dev);
334 put_device(&edev->dev);
337 dev_set_drvdata(&edev->dev, edev);
339 INIT_LIST_HEAD(&edev->node);
341 mutex_lock(&devfreq_event_list_lock);
342 list_add(&edev->node, &devfreq_event_list);
343 mutex_unlock(&devfreq_event_list_lock);
347 EXPORT_SYMBOL_GPL(devfreq_event_add_edev);
350 * devfreq_event_remove_edev() - Remove the devfreq-event device registered.
351 * @edev : the devfreq-event device
353 * Note that this function removes the registered devfreq-event device.
355 int devfreq_event_remove_edev(struct devfreq_event_dev *edev)
360 WARN_ON(edev->enable_count);
362 mutex_lock(&devfreq_event_list_lock);
363 list_del(&edev->node);
364 mutex_unlock(&devfreq_event_list_lock);
366 device_unregister(&edev->dev);
370 EXPORT_SYMBOL_GPL(devfreq_event_remove_edev);
372 static int devm_devfreq_event_match(struct device *dev, void *res, void *data)
374 struct devfreq_event_dev **r = res;
376 if (WARN_ON(!r || !*r))
382 static void devm_devfreq_event_release(struct device *dev, void *res)
384 devfreq_event_remove_edev(*(struct devfreq_event_dev **)res);
388 * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev()
389 * @dev : the device owning the devfreq-event device being created
390 * @desc : the devfreq-event device's descriptor which include essential
391 * data for devfreq-event device.
393 * Note that this function manages automatically the memory of devfreq-event
394 * device using device resource management and simplify the free operation
395 * for memory of devfreq-event device.
397 struct devfreq_event_dev *devm_devfreq_event_add_edev(struct device *dev,
398 struct devfreq_event_desc *desc)
400 struct devfreq_event_dev **ptr, *edev;
402 ptr = devres_alloc(devm_devfreq_event_release, sizeof(*ptr),
405 return ERR_PTR(-ENOMEM);
407 edev = devfreq_event_add_edev(dev, desc);
410 return ERR_PTR(-ENOMEM);
414 devres_add(dev, ptr);
418 EXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev);
421 * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev()
422 * @dev : the device owning the devfreq-event device being created
423 * @edev : the devfreq-event device
425 * Note that this function manages automatically the memory of devfreq-event
426 * device using device resource management.
428 void devm_devfreq_event_remove_edev(struct device *dev,
429 struct devfreq_event_dev *edev)
431 WARN_ON(devres_release(dev, devm_devfreq_event_release,
432 devm_devfreq_event_match, edev));
434 EXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev);
437 * Device attributes for devfreq-event class.
439 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
442 struct devfreq_event_dev *edev = to_devfreq_event(dev);
444 if (!edev || !edev->desc)
447 return sprintf(buf, "%s\n", edev->desc->name);
449 static DEVICE_ATTR_RO(name);
451 static ssize_t enable_count_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 struct devfreq_event_dev *edev = to_devfreq_event(dev);
456 if (!edev || !edev->desc)
459 return sprintf(buf, "%d\n", edev->enable_count);
461 static DEVICE_ATTR_RO(enable_count);
463 static struct attribute *devfreq_event_attrs[] = {
465 &dev_attr_enable_count.attr,
468 ATTRIBUTE_GROUPS(devfreq_event);
470 static int __init devfreq_event_init(void)
472 devfreq_event_class = class_create(THIS_MODULE, "devfreq-event");
473 if (IS_ERR(devfreq_event_class)) {
474 pr_err("%s: couldn't create class\n", __FILE__);
475 return PTR_ERR(devfreq_event_class);
478 devfreq_event_class->dev_groups = devfreq_event_groups;
482 subsys_initcall(devfreq_event_init);