2 * devfreq-event: a framework to provide raw data and events of devfreq devices
4 * Copyright (C) 2015 Samsung Electronics
5 * Author: Chanwoo Choi <cw00.choi@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This driver is based on drivers/devfreq/devfreq.c.
14 #include <linux/devfreq-event.h>
15 #include <linux/kernel.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
23 static struct class *devfreq_event_class
;
25 /* The list of all devfreq event list */
26 static LIST_HEAD(devfreq_event_list
);
27 static DEFINE_MUTEX(devfreq_event_list_lock
);
29 #define to_devfreq_event(DEV) container_of(DEV, struct devfreq_event_dev, dev)
32 * devfreq_event_enable_edev() - Enable the devfreq-event dev and increase
33 * the enable_count of devfreq-event dev.
34 * @edev : the devfreq-event device
36 * Note that this function increase the enable_count and enable the
37 * devfreq-event device. The devfreq-event device should be enabled before
38 * using it by devfreq device.
40 int devfreq_event_enable_edev(struct devfreq_event_dev
*edev
)
44 if (!edev
|| !edev
->desc
)
47 mutex_lock(&edev
->lock
);
48 if (edev
->desc
->ops
&& edev
->desc
->ops
->enable
49 && edev
->enable_count
== 0) {
50 ret
= edev
->desc
->ops
->enable(edev
);
56 mutex_unlock(&edev
->lock
);
60 EXPORT_SYMBOL_GPL(devfreq_event_enable_edev
);
63 * devfreq_event_disable_edev() - Disable the devfreq-event dev and decrease
64 * the enable_count of the devfreq-event dev.
65 * @edev : the devfreq-event device
67 * Note that this function decrease the enable_count and disable the
68 * devfreq-event device. After the devfreq-event device is disabled,
69 * devfreq device can't use the devfreq-event device for get/set/reset
72 int devfreq_event_disable_edev(struct devfreq_event_dev
*edev
)
76 if (!edev
|| !edev
->desc
)
79 mutex_lock(&edev
->lock
);
80 if (edev
->enable_count
<= 0) {
81 dev_warn(&edev
->dev
, "unbalanced enable_count\n");
86 if (edev
->desc
->ops
&& edev
->desc
->ops
->disable
87 && edev
->enable_count
== 1) {
88 ret
= edev
->desc
->ops
->disable(edev
);
94 mutex_unlock(&edev
->lock
);
98 EXPORT_SYMBOL_GPL(devfreq_event_disable_edev
);
101 * devfreq_event_is_enabled() - Check whether devfreq-event dev is enabled or
103 * @edev : the devfreq-event device
105 * Note that this function check whether devfreq-event dev is enabled or not.
106 * If return true, the devfreq-event dev is enabeld. If return false, the
107 * devfreq-event dev is disabled.
109 bool devfreq_event_is_enabled(struct devfreq_event_dev
*edev
)
111 bool enabled
= false;
113 if (!edev
|| !edev
->desc
)
116 mutex_lock(&edev
->lock
);
118 if (edev
->enable_count
> 0)
121 mutex_unlock(&edev
->lock
);
125 EXPORT_SYMBOL_GPL(devfreq_event_is_enabled
);
128 * devfreq_event_set_event() - Set event to devfreq-event dev to start.
129 * @edev : the devfreq-event device
131 * Note that this function set the event to the devfreq-event device to start
132 * for getting the event data which could be various event type.
134 int devfreq_event_set_event(struct devfreq_event_dev
*edev
)
138 if (!edev
|| !edev
->desc
)
141 if (!edev
->desc
->ops
|| !edev
->desc
->ops
->set_event
)
144 if (!devfreq_event_is_enabled(edev
))
147 mutex_lock(&edev
->lock
);
148 ret
= edev
->desc
->ops
->set_event(edev
);
149 mutex_unlock(&edev
->lock
);
153 EXPORT_SYMBOL_GPL(devfreq_event_set_event
);
156 * devfreq_event_get_event() - Get {load|total}_count from devfreq-event dev.
157 * @edev : the devfreq-event device
158 * @edata : the calculated data of devfreq-event device
160 * Note that this function get the calculated event data from devfreq-event dev
161 * after stoping the progress of whole sequence of devfreq-event dev.
163 int devfreq_event_get_event(struct devfreq_event_dev
*edev
,
164 struct devfreq_event_data
*edata
)
168 if (!edev
|| !edev
->desc
)
171 if (!edev
->desc
->ops
|| !edev
->desc
->ops
->get_event
)
174 if (!devfreq_event_is_enabled(edev
))
177 edata
->total_count
= edata
->load_count
= 0;
179 mutex_lock(&edev
->lock
);
180 ret
= edev
->desc
->ops
->get_event(edev
, edata
);
182 edata
->total_count
= edata
->load_count
= 0;
183 mutex_unlock(&edev
->lock
);
187 EXPORT_SYMBOL_GPL(devfreq_event_get_event
);
190 * devfreq_event_reset_event() - Reset all opeations of devfreq-event dev.
191 * @edev : the devfreq-event device
193 * Note that this function stop all operations of devfreq-event dev and reset
194 * the current event data to make the devfreq-event device into initial state.
196 int devfreq_event_reset_event(struct devfreq_event_dev
*edev
)
200 if (!edev
|| !edev
->desc
)
203 if (!devfreq_event_is_enabled(edev
))
206 mutex_lock(&edev
->lock
);
207 if (edev
->desc
->ops
&& edev
->desc
->ops
->reset
)
208 ret
= edev
->desc
->ops
->reset(edev
);
209 mutex_unlock(&edev
->lock
);
213 EXPORT_SYMBOL_GPL(devfreq_event_reset_event
);
216 * devfreq_event_get_edev_by_phandle() - Get the devfreq-event dev from
218 * @dev : the pointer to the given device
219 * @index : the index into list of devfreq-event device
221 * Note that this function return the pointer of devfreq-event device.
223 struct devfreq_event_dev
*devfreq_event_get_edev_by_phandle(struct device
*dev
,
226 struct device_node
*node
;
227 struct devfreq_event_dev
*edev
;
230 return ERR_PTR(-EINVAL
);
232 node
= of_parse_phandle(dev
->of_node
, "devfreq-events", index
);
234 return ERR_PTR(-ENODEV
);
236 mutex_lock(&devfreq_event_list_lock
);
237 list_for_each_entry(edev
, &devfreq_event_list
, node
) {
238 if (edev
->dev
.parent
&& edev
->dev
.parent
->of_node
== node
)
242 list_for_each_entry(edev
, &devfreq_event_list
, node
) {
243 if (!strcmp(edev
->desc
->name
, node
->name
))
248 mutex_unlock(&devfreq_event_list_lock
);
252 return ERR_PTR(-ENODEV
);
259 EXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle
);
262 * devfreq_event_get_edev_count() - Get the count of devfreq-event dev
263 * @dev : the pointer to the given device
265 * Note that this function return the count of devfreq-event devices.
267 int devfreq_event_get_edev_count(struct device
*dev
)
272 dev_err(dev
, "device does not have a device node entry\n");
276 count
= of_property_count_elems_of_size(dev
->of_node
, "devfreq-events",
280 "failed to get the count of devfreq-event in %pOF node\n",
287 EXPORT_SYMBOL_GPL(devfreq_event_get_edev_count
);
289 static void devfreq_event_release_edev(struct device
*dev
)
291 struct devfreq_event_dev
*edev
= to_devfreq_event(dev
);
297 * devfreq_event_add_edev() - Add new devfreq-event device.
298 * @dev : the device owning the devfreq-event device being created
299 * @desc : the devfreq-event device's decriptor which include essential
300 * data for devfreq-event device.
302 * Note that this function add new devfreq-event device to devfreq-event class
303 * list and register the device of the devfreq-event device.
305 struct devfreq_event_dev
*devfreq_event_add_edev(struct device
*dev
,
306 struct devfreq_event_desc
*desc
)
308 struct devfreq_event_dev
*edev
;
309 static atomic_t event_no
= ATOMIC_INIT(-1);
313 return ERR_PTR(-EINVAL
);
315 if (!desc
->name
|| !desc
->ops
)
316 return ERR_PTR(-EINVAL
);
318 if (!desc
->ops
->set_event
|| !desc
->ops
->get_event
)
319 return ERR_PTR(-EINVAL
);
321 edev
= kzalloc(sizeof(struct devfreq_event_dev
), GFP_KERNEL
);
323 return ERR_PTR(-ENOMEM
);
325 mutex_init(&edev
->lock
);
327 edev
->enable_count
= 0;
328 edev
->dev
.parent
= dev
;
329 edev
->dev
.class = devfreq_event_class
;
330 edev
->dev
.release
= devfreq_event_release_edev
;
332 dev_set_name(&edev
->dev
, "event%d", atomic_inc_return(&event_no
));
333 ret
= device_register(&edev
->dev
);
335 put_device(&edev
->dev
);
338 dev_set_drvdata(&edev
->dev
, edev
);
340 INIT_LIST_HEAD(&edev
->node
);
342 mutex_lock(&devfreq_event_list_lock
);
343 list_add(&edev
->node
, &devfreq_event_list
);
344 mutex_unlock(&devfreq_event_list_lock
);
348 EXPORT_SYMBOL_GPL(devfreq_event_add_edev
);
351 * devfreq_event_remove_edev() - Remove the devfreq-event device registered.
352 * @dev : the devfreq-event device
354 * Note that this function remove the registered devfreq-event device.
356 int devfreq_event_remove_edev(struct devfreq_event_dev
*edev
)
361 WARN_ON(edev
->enable_count
);
363 mutex_lock(&devfreq_event_list_lock
);
364 list_del(&edev
->node
);
365 mutex_unlock(&devfreq_event_list_lock
);
367 device_unregister(&edev
->dev
);
371 EXPORT_SYMBOL_GPL(devfreq_event_remove_edev
);
373 static int devm_devfreq_event_match(struct device
*dev
, void *res
, void *data
)
375 struct devfreq_event_dev
**r
= res
;
377 if (WARN_ON(!r
|| !*r
))
383 static void devm_devfreq_event_release(struct device
*dev
, void *res
)
385 devfreq_event_remove_edev(*(struct devfreq_event_dev
**)res
);
389 * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev()
390 * @dev : the device owning the devfreq-event device being created
391 * @desc : the devfreq-event device's decriptor which include essential
392 * data for devfreq-event device.
394 * Note that this function manages automatically the memory of devfreq-event
395 * device using device resource management and simplify the free operation
396 * for memory of devfreq-event device.
398 struct devfreq_event_dev
*devm_devfreq_event_add_edev(struct device
*dev
,
399 struct devfreq_event_desc
*desc
)
401 struct devfreq_event_dev
**ptr
, *edev
;
403 ptr
= devres_alloc(devm_devfreq_event_release
, sizeof(*ptr
),
406 return ERR_PTR(-ENOMEM
);
408 edev
= devfreq_event_add_edev(dev
, desc
);
411 return ERR_PTR(-ENOMEM
);
415 devres_add(dev
, ptr
);
419 EXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev
);
422 * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev()
423 * @dev : the device owning the devfreq-event device being created
424 * @edev : the devfreq-event device
426 * Note that this function manages automatically the memory of devfreq-event
427 * device using device resource management.
429 void devm_devfreq_event_remove_edev(struct device
*dev
,
430 struct devfreq_event_dev
*edev
)
432 WARN_ON(devres_release(dev
, devm_devfreq_event_release
,
433 devm_devfreq_event_match
, edev
));
435 EXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev
);
438 * Device attributes for devfreq-event class.
440 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
443 struct devfreq_event_dev
*edev
= to_devfreq_event(dev
);
445 if (!edev
|| !edev
->desc
)
448 return sprintf(buf
, "%s\n", edev
->desc
->name
);
450 static DEVICE_ATTR_RO(name
);
452 static ssize_t
enable_count_show(struct device
*dev
,
453 struct device_attribute
*attr
, char *buf
)
455 struct devfreq_event_dev
*edev
= to_devfreq_event(dev
);
457 if (!edev
|| !edev
->desc
)
460 return sprintf(buf
, "%d\n", edev
->enable_count
);
462 static DEVICE_ATTR_RO(enable_count
);
464 static struct attribute
*devfreq_event_attrs
[] = {
466 &dev_attr_enable_count
.attr
,
469 ATTRIBUTE_GROUPS(devfreq_event
);
471 static int __init
devfreq_event_init(void)
473 devfreq_event_class
= class_create(THIS_MODULE
, "devfreq-event");
474 if (IS_ERR(devfreq_event_class
)) {
475 pr_err("%s: couldn't create class\n", __FILE__
);
476 return PTR_ERR(devfreq_event_class
);
479 devfreq_event_class
->dev_groups
= devfreq_event_groups
;
483 subsys_initcall(devfreq_event_init
);