blob: 56efbeb7851e7a1ee5816f3345896648701f733d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Chanwoo Choif262f282015-01-26 13:16:27 +09002/*
3 * devfreq-event: a framework to provide raw data and events of devfreq devices
4 *
5 * Copyright (C) 2015 Samsung Electronics
6 * Author: Chanwoo Choi <cw00.choi@samsung.com>
7 *
Chanwoo Choif262f282015-01-26 13:16:27 +09008 * This driver is based on drivers/devfreq/devfreq.c.
9 */
10
11#include <linux/devfreq-event.h>
12#include <linux/kernel.h>
13#include <linux/err.h>
14#include <linux/init.h>
Paul Gortmakere32363b2016-06-26 03:43:48 +090015#include <linux/export.h>
Chanwoo Choif262f282015-01-26 13:16:27 +090016#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/of.h>
19
20static struct class *devfreq_event_class;
21
22/* The list of all devfreq event list */
23static LIST_HEAD(devfreq_event_list);
24static DEFINE_MUTEX(devfreq_event_list_lock);
25
26#define to_devfreq_event(DEV) container_of(DEV, struct devfreq_event_dev, dev)
27
28/**
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
32 *
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.
36 */
37int devfreq_event_enable_edev(struct devfreq_event_dev *edev)
38{
39 int ret = 0;
40
41 if (!edev || !edev->desc)
42 return -EINVAL;
43
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);
48 if (ret < 0)
49 goto err;
50 }
51 edev->enable_count++;
52err:
53 mutex_unlock(&edev->lock);
54
55 return ret;
56}
57EXPORT_SYMBOL_GPL(devfreq_event_enable_edev);
58
59/**
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
63 *
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
67 * operations.
68 */
69int devfreq_event_disable_edev(struct devfreq_event_dev *edev)
70{
71 int ret = 0;
72
73 if (!edev || !edev->desc)
74 return -EINVAL;
75
76 mutex_lock(&edev->lock);
77 if (edev->enable_count <= 0) {
78 dev_warn(&edev->dev, "unbalanced enable_count\n");
79 ret = -EIO;
80 goto err;
81 }
82
83 if (edev->desc->ops && edev->desc->ops->disable
84 && edev->enable_count == 1) {
85 ret = edev->desc->ops->disable(edev);
86 if (ret < 0)
87 goto err;
88 }
89 edev->enable_count--;
90err:
91 mutex_unlock(&edev->lock);
92
93 return ret;
94}
95EXPORT_SYMBOL_GPL(devfreq_event_disable_edev);
96
97/**
98 * devfreq_event_is_enabled() - Check whether devfreq-event dev is enabled or
99 * not.
100 * @edev : the devfreq-event device
101 *
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.
105 */
106bool devfreq_event_is_enabled(struct devfreq_event_dev *edev)
107{
108 bool enabled = false;
109
110 if (!edev || !edev->desc)
111 return enabled;
112
113 mutex_lock(&edev->lock);
114
115 if (edev->enable_count > 0)
116 enabled = true;
117
118 mutex_unlock(&edev->lock);
119
120 return enabled;
121}
122EXPORT_SYMBOL_GPL(devfreq_event_is_enabled);
123
124/**
125 * devfreq_event_set_event() - Set event to devfreq-event dev to start.
126 * @edev : the devfreq-event device
127 *
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.
130 */
131int devfreq_event_set_event(struct devfreq_event_dev *edev)
132{
133 int ret;
134
135 if (!edev || !edev->desc)
136 return -EINVAL;
137
138 if (!edev->desc->ops || !edev->desc->ops->set_event)
139 return -EINVAL;
140
141 if (!devfreq_event_is_enabled(edev))
142 return -EPERM;
143
144 mutex_lock(&edev->lock);
145 ret = edev->desc->ops->set_event(edev);
146 mutex_unlock(&edev->lock);
147
148 return ret;
149}
150EXPORT_SYMBOL_GPL(devfreq_event_set_event);
151
152/**
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
156 *
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.
159 */
160int devfreq_event_get_event(struct devfreq_event_dev *edev,
161 struct devfreq_event_data *edata)
162{
163 int ret;
164
165 if (!edev || !edev->desc)
166 return -EINVAL;
167
168 if (!edev->desc->ops || !edev->desc->ops->get_event)
169 return -EINVAL;
170
171 if (!devfreq_event_is_enabled(edev))
172 return -EINVAL;
173
174 edata->total_count = edata->load_count = 0;
175
176 mutex_lock(&edev->lock);
177 ret = edev->desc->ops->get_event(edev, edata);
178 if (ret < 0)
179 edata->total_count = edata->load_count = 0;
180 mutex_unlock(&edev->lock);
181
182 return ret;
183}
184EXPORT_SYMBOL_GPL(devfreq_event_get_event);
185
186/**
187 * devfreq_event_reset_event() - Reset all opeations of devfreq-event dev.
188 * @edev : the devfreq-event device
189 *
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.
192 */
193int devfreq_event_reset_event(struct devfreq_event_dev *edev)
194{
195 int ret = 0;
196
197 if (!edev || !edev->desc)
198 return -EINVAL;
199
200 if (!devfreq_event_is_enabled(edev))
201 return -EPERM;
202
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);
207
208 return ret;
209}
210EXPORT_SYMBOL_GPL(devfreq_event_reset_event);
211
212/**
213 * devfreq_event_get_edev_by_phandle() - Get the devfreq-event dev from
214 * devicetree.
215 * @dev : the pointer to the given device
216 * @index : the index into list of devfreq-event device
217 *
218 * Note that this function return the pointer of devfreq-event device.
219 */
220struct devfreq_event_dev *devfreq_event_get_edev_by_phandle(struct device *dev,
221 int index)
222{
223 struct device_node *node;
224 struct devfreq_event_dev *edev;
225
Chanwoo Choidf678ff2015-11-17 14:25:06 +0900226 if (!dev->of_node)
Chanwoo Choif262f282015-01-26 13:16:27 +0900227 return ERR_PTR(-EINVAL);
Chanwoo Choif262f282015-01-26 13:16:27 +0900228
229 node = of_parse_phandle(dev->of_node, "devfreq-events", index);
Chanwoo Choidf678ff2015-11-17 14:25:06 +0900230 if (!node)
Chanwoo Choif262f282015-01-26 13:16:27 +0900231 return ERR_PTR(-ENODEV);
Chanwoo Choif262f282015-01-26 13:16:27 +0900232
233 mutex_lock(&devfreq_event_list_lock);
234 list_for_each_entry(edev, &devfreq_event_list, node) {
Chanwoo Choi19cf91d2016-04-14 14:37:12 +0900235 if (edev->dev.parent && edev->dev.parent->of_node == node)
236 goto out;
237 }
238
239 list_for_each_entry(edev, &devfreq_event_list, node) {
Rob Herring0d00a232018-12-05 13:50:22 -0600240 if (of_node_name_eq(node, edev->desc->name))
Chanwoo Choif262f282015-01-26 13:16:27 +0900241 goto out;
242 }
243 edev = NULL;
244out:
245 mutex_unlock(&devfreq_event_list_lock);
246
247 if (!edev) {
Chanwoo Choif262f282015-01-26 13:16:27 +0900248 of_node_put(node);
249 return ERR_PTR(-ENODEV);
250 }
251
252 of_node_put(node);
253
254 return edev;
255}
256EXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle);
257
258/**
259 * devfreq_event_get_edev_count() - Get the count of devfreq-event dev
260 * @dev : the pointer to the given device
261 *
262 * Note that this function return the count of devfreq-event devices.
263 */
264int devfreq_event_get_edev_count(struct device *dev)
265{
266 int count;
267
268 if (!dev->of_node) {
269 dev_err(dev, "device does not have a device node entry\n");
270 return -EINVAL;
271 }
272
273 count = of_property_count_elems_of_size(dev->of_node, "devfreq-events",
274 sizeof(u32));
Chanwoo Choie54916c2015-11-19 17:03:44 +0900275 if (count < 0) {
Chanwoo Choif262f282015-01-26 13:16:27 +0900276 dev_err(dev,
Rob Herringe8305d42017-07-18 16:42:57 -0500277 "failed to get the count of devfreq-event in %pOF node\n",
278 dev->of_node);
Chanwoo Choif262f282015-01-26 13:16:27 +0900279 return count;
280 }
281
282 return count;
283}
284EXPORT_SYMBOL_GPL(devfreq_event_get_edev_count);
285
286static void devfreq_event_release_edev(struct device *dev)
287{
288 struct devfreq_event_dev *edev = to_devfreq_event(dev);
289
290 kfree(edev);
291}
292
293/**
294 * devfreq_event_add_edev() - Add new devfreq-event device.
295 * @dev : the device owning the devfreq-event device being created
Kieran Binghamada79592020-06-09 13:46:05 +0100296 * @desc : the devfreq-event device's descriptor which include essential
Chanwoo Choif262f282015-01-26 13:16:27 +0900297 * data for devfreq-event device.
298 *
299 * Note that this function add new devfreq-event device to devfreq-event class
300 * list and register the device of the devfreq-event device.
301 */
302struct devfreq_event_dev *devfreq_event_add_edev(struct device *dev,
303 struct devfreq_event_desc *desc)
304{
305 struct devfreq_event_dev *edev;
Chanwoo Choi775fa8c2017-01-31 16:47:56 +0900306 static atomic_t event_no = ATOMIC_INIT(-1);
Chanwoo Choif262f282015-01-26 13:16:27 +0900307 int ret;
308
309 if (!dev || !desc)
310 return ERR_PTR(-EINVAL);
311
312 if (!desc->name || !desc->ops)
313 return ERR_PTR(-EINVAL);
314
315 if (!desc->ops->set_event || !desc->ops->get_event)
316 return ERR_PTR(-EINVAL);
317
318 edev = kzalloc(sizeof(struct devfreq_event_dev), GFP_KERNEL);
319 if (!edev)
320 return ERR_PTR(-ENOMEM);
321
322 mutex_init(&edev->lock);
323 edev->desc = desc;
324 edev->enable_count = 0;
325 edev->dev.parent = dev;
326 edev->dev.class = devfreq_event_class;
327 edev->dev.release = devfreq_event_release_edev;
328
Chanwoo Choi775fa8c2017-01-31 16:47:56 +0900329 dev_set_name(&edev->dev, "event%d", atomic_inc_return(&event_no));
Chanwoo Choif262f282015-01-26 13:16:27 +0900330 ret = device_register(&edev->dev);
331 if (ret < 0) {
332 put_device(&edev->dev);
333 return ERR_PTR(ret);
334 }
335 dev_set_drvdata(&edev->dev, edev);
336
337 INIT_LIST_HEAD(&edev->node);
338
339 mutex_lock(&devfreq_event_list_lock);
340 list_add(&edev->node, &devfreq_event_list);
341 mutex_unlock(&devfreq_event_list_lock);
342
343 return edev;
344}
345EXPORT_SYMBOL_GPL(devfreq_event_add_edev);
346
347/**
348 * devfreq_event_remove_edev() - Remove the devfreq-event device registered.
Randy Dunlap54cb5742019-12-14 08:03:11 -0800349 * @edev : the devfreq-event device
Chanwoo Choif262f282015-01-26 13:16:27 +0900350 *
Randy Dunlap54cb5742019-12-14 08:03:11 -0800351 * Note that this function removes the registered devfreq-event device.
Chanwoo Choif262f282015-01-26 13:16:27 +0900352 */
353int devfreq_event_remove_edev(struct devfreq_event_dev *edev)
354{
355 if (!edev)
356 return -EINVAL;
357
358 WARN_ON(edev->enable_count);
359
360 mutex_lock(&devfreq_event_list_lock);
361 list_del(&edev->node);
362 mutex_unlock(&devfreq_event_list_lock);
363
364 device_unregister(&edev->dev);
365
366 return 0;
367}
368EXPORT_SYMBOL_GPL(devfreq_event_remove_edev);
369
370static int devm_devfreq_event_match(struct device *dev, void *res, void *data)
371{
372 struct devfreq_event_dev **r = res;
373
374 if (WARN_ON(!r || !*r))
375 return 0;
376
377 return *r == data;
378}
379
380static void devm_devfreq_event_release(struct device *dev, void *res)
381{
382 devfreq_event_remove_edev(*(struct devfreq_event_dev **)res);
383}
384
385/**
386 * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev()
387 * @dev : the device owning the devfreq-event device being created
Kieran Binghamada79592020-06-09 13:46:05 +0100388 * @desc : the devfreq-event device's descriptor which include essential
Chanwoo Choif262f282015-01-26 13:16:27 +0900389 * data for devfreq-event device.
390 *
391 * Note that this function manages automatically the memory of devfreq-event
392 * device using device resource management and simplify the free operation
393 * for memory of devfreq-event device.
394 */
395struct devfreq_event_dev *devm_devfreq_event_add_edev(struct device *dev,
396 struct devfreq_event_desc *desc)
397{
398 struct devfreq_event_dev **ptr, *edev;
399
Chanwoo Choie54916c2015-11-19 17:03:44 +0900400 ptr = devres_alloc(devm_devfreq_event_release, sizeof(*ptr),
401 GFP_KERNEL);
Chanwoo Choif262f282015-01-26 13:16:27 +0900402 if (!ptr)
403 return ERR_PTR(-ENOMEM);
404
405 edev = devfreq_event_add_edev(dev, desc);
406 if (IS_ERR(edev)) {
407 devres_free(ptr);
408 return ERR_PTR(-ENOMEM);
409 }
410
411 *ptr = edev;
412 devres_add(dev, ptr);
413
414 return edev;
415}
416EXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev);
417
418/**
419 * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev()
420 * @dev : the device owning the devfreq-event device being created
421 * @edev : the devfreq-event device
422 *
423 * Note that this function manages automatically the memory of devfreq-event
424 * device using device resource management.
425 */
426void devm_devfreq_event_remove_edev(struct device *dev,
427 struct devfreq_event_dev *edev)
428{
429 WARN_ON(devres_release(dev, devm_devfreq_event_release,
430 devm_devfreq_event_match, edev));
431}
432EXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev);
433
434/*
435 * Device attributes for devfreq-event class.
436 */
437static ssize_t name_show(struct device *dev, struct device_attribute *attr,
438 char *buf)
439{
440 struct devfreq_event_dev *edev = to_devfreq_event(dev);
441
442 if (!edev || !edev->desc)
443 return -EINVAL;
444
445 return sprintf(buf, "%s\n", edev->desc->name);
446}
447static DEVICE_ATTR_RO(name);
448
449static ssize_t enable_count_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
452 struct devfreq_event_dev *edev = to_devfreq_event(dev);
453
454 if (!edev || !edev->desc)
455 return -EINVAL;
456
457 return sprintf(buf, "%d\n", edev->enable_count);
458}
459static DEVICE_ATTR_RO(enable_count);
460
461static struct attribute *devfreq_event_attrs[] = {
462 &dev_attr_name.attr,
463 &dev_attr_enable_count.attr,
464 NULL,
465};
466ATTRIBUTE_GROUPS(devfreq_event);
467
468static int __init devfreq_event_init(void)
469{
470 devfreq_event_class = class_create(THIS_MODULE, "devfreq-event");
471 if (IS_ERR(devfreq_event_class)) {
472 pr_err("%s: couldn't create class\n", __FILE__);
473 return PTR_ERR(devfreq_event_class);
474 }
475
476 devfreq_event_class->dev_groups = devfreq_event_groups;
477
478 return 0;
479}
480subsys_initcall(devfreq_event_init);