1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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 <[email protected]>
7 *
8 * 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>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/of.h>
19
20 static struct class *devfreq_event_class;
21
22 /* The list of all devfreq event list */
23 static LIST_HEAD(devfreq_event_list);
24 static 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 */
devfreq_event_enable_edev(struct devfreq_event_dev * edev)37 int 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++;
52 err:
53 mutex_unlock(&edev->lock);
54
55 return ret;
56 }
57 EXPORT_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 */
devfreq_event_disable_edev(struct devfreq_event_dev * edev)69 int 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--;
90 err:
91 mutex_unlock(&edev->lock);
92
93 return ret;
94 }
95 EXPORT_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 */
devfreq_event_is_enabled(struct devfreq_event_dev * edev)106 bool 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 }
122 EXPORT_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 */
devfreq_event_set_event(struct devfreq_event_dev * edev)131 int 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 }
150 EXPORT_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 */
devfreq_event_get_event(struct devfreq_event_dev * edev,struct devfreq_event_data * edata)160 int 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 }
184 EXPORT_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 */
devfreq_event_reset_event(struct devfreq_event_dev * edev)193 int 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 }
210 EXPORT_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 * @phandle_name: name of property holding a phandle value
217 * @index : the index into list of devfreq-event device
218 *
219 * Note that this function return the pointer of devfreq-event device.
220 */
devfreq_event_get_edev_by_phandle(struct device * dev,const char * phandle_name,int index)221 struct devfreq_event_dev *devfreq_event_get_edev_by_phandle(struct device *dev,
222 const char *phandle_name, int index)
223 {
224 struct device_node *node;
225 struct devfreq_event_dev *edev;
226
227 if (!dev->of_node || !phandle_name)
228 return ERR_PTR(-EINVAL);
229
230 node = of_parse_phandle(dev->of_node, phandle_name, index);
231 if (!node)
232 return ERR_PTR(-ENODEV);
233
234 mutex_lock(&devfreq_event_list_lock);
235 list_for_each_entry(edev, &devfreq_event_list, node) {
236 if (edev->dev.parent && device_match_of_node(edev->dev.parent, node))
237 goto out;
238 }
239
240 list_for_each_entry(edev, &devfreq_event_list, node) {
241 if (of_node_name_eq(node, edev->desc->name))
242 goto out;
243 }
244 edev = NULL;
245 out:
246 mutex_unlock(&devfreq_event_list_lock);
247 of_node_put(node);
248 if (!edev)
249 return ERR_PTR(-ENODEV);
250
251 return edev;
252 }
253 EXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle);
254
255 /**
256 * devfreq_event_get_edev_count() - Get the count of devfreq-event dev
257 * @dev : the pointer to the given device
258 * @phandle_name: name of property holding a phandle value
259 *
260 * Note that this function return the count of devfreq-event devices.
261 */
devfreq_event_get_edev_count(struct device * dev,const char * phandle_name)262 int devfreq_event_get_edev_count(struct device *dev, const char *phandle_name)
263 {
264 int count;
265
266 if (!dev->of_node || !phandle_name) {
267 dev_err(dev, "device does not have a device node entry\n");
268 return -EINVAL;
269 }
270
271 count = of_property_count_elems_of_size(dev->of_node, phandle_name,
272 sizeof(u32));
273 if (count < 0) {
274 dev_err(dev,
275 "failed to get the count of devfreq-event in %pOF node\n",
276 dev->of_node);
277 return count;
278 }
279
280 return count;
281 }
282 EXPORT_SYMBOL_GPL(devfreq_event_get_edev_count);
283
devfreq_event_release_edev(struct device * dev)284 static void devfreq_event_release_edev(struct device *dev)
285 {
286 struct devfreq_event_dev *edev = to_devfreq_event(dev);
287
288 kfree(edev);
289 }
290
291 /**
292 * devfreq_event_add_edev() - Add new devfreq-event device.
293 * @dev : the device owning the devfreq-event device being created
294 * @desc : the devfreq-event device's descriptor which include essential
295 * data for devfreq-event device.
296 *
297 * Note that this function add new devfreq-event device to devfreq-event class
298 * list and register the device of the devfreq-event device.
299 */
devfreq_event_add_edev(struct device * dev,struct devfreq_event_desc * desc)300 struct devfreq_event_dev *devfreq_event_add_edev(struct device *dev,
301 struct devfreq_event_desc *desc)
302 {
303 struct devfreq_event_dev *edev;
304 static atomic_t event_no = ATOMIC_INIT(-1);
305 int ret;
306
307 if (!dev || !desc)
308 return ERR_PTR(-EINVAL);
309
310 if (!desc->name || !desc->ops)
311 return ERR_PTR(-EINVAL);
312
313 if (!desc->ops->set_event || !desc->ops->get_event)
314 return ERR_PTR(-EINVAL);
315
316 edev = kzalloc(sizeof(struct devfreq_event_dev), GFP_KERNEL);
317 if (!edev)
318 return ERR_PTR(-ENOMEM);
319
320 mutex_init(&edev->lock);
321 edev->desc = desc;
322 edev->enable_count = 0;
323 edev->dev.parent = dev;
324 edev->dev.class = devfreq_event_class;
325 edev->dev.release = devfreq_event_release_edev;
326
327 dev_set_name(&edev->dev, "event%d", atomic_inc_return(&event_no));
328 ret = device_register(&edev->dev);
329 if (ret < 0) {
330 put_device(&edev->dev);
331 return ERR_PTR(ret);
332 }
333 dev_set_drvdata(&edev->dev, edev);
334
335 INIT_LIST_HEAD(&edev->node);
336
337 mutex_lock(&devfreq_event_list_lock);
338 list_add(&edev->node, &devfreq_event_list);
339 mutex_unlock(&devfreq_event_list_lock);
340
341 return edev;
342 }
343 EXPORT_SYMBOL_GPL(devfreq_event_add_edev);
344
345 /**
346 * devfreq_event_remove_edev() - Remove the devfreq-event device registered.
347 * @edev : the devfreq-event device
348 *
349 * Note that this function removes the registered devfreq-event device.
350 */
devfreq_event_remove_edev(struct devfreq_event_dev * edev)351 int devfreq_event_remove_edev(struct devfreq_event_dev *edev)
352 {
353 if (!edev)
354 return -EINVAL;
355
356 WARN_ON(edev->enable_count);
357
358 mutex_lock(&devfreq_event_list_lock);
359 list_del(&edev->node);
360 mutex_unlock(&devfreq_event_list_lock);
361
362 device_unregister(&edev->dev);
363
364 return 0;
365 }
366 EXPORT_SYMBOL_GPL(devfreq_event_remove_edev);
367
devm_devfreq_event_match(struct device * dev,void * res,void * data)368 static int devm_devfreq_event_match(struct device *dev, void *res, void *data)
369 {
370 struct devfreq_event_dev **r = res;
371
372 if (WARN_ON(!r || !*r))
373 return 0;
374
375 return *r == data;
376 }
377
devm_devfreq_event_release(struct device * dev,void * res)378 static void devm_devfreq_event_release(struct device *dev, void *res)
379 {
380 devfreq_event_remove_edev(*(struct devfreq_event_dev **)res);
381 }
382
383 /**
384 * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev()
385 * @dev : the device owning the devfreq-event device being created
386 * @desc : the devfreq-event device's descriptor which include essential
387 * data for devfreq-event device.
388 *
389 * Note that this function manages automatically the memory of devfreq-event
390 * device using device resource management and simplify the free operation
391 * for memory of devfreq-event device.
392 */
devm_devfreq_event_add_edev(struct device * dev,struct devfreq_event_desc * desc)393 struct devfreq_event_dev *devm_devfreq_event_add_edev(struct device *dev,
394 struct devfreq_event_desc *desc)
395 {
396 struct devfreq_event_dev **ptr, *edev;
397
398 ptr = devres_alloc(devm_devfreq_event_release, sizeof(*ptr),
399 GFP_KERNEL);
400 if (!ptr)
401 return ERR_PTR(-ENOMEM);
402
403 edev = devfreq_event_add_edev(dev, desc);
404 if (IS_ERR(edev)) {
405 devres_free(ptr);
406 return ERR_PTR(-ENOMEM);
407 }
408
409 *ptr = edev;
410 devres_add(dev, ptr);
411
412 return edev;
413 }
414 EXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev);
415
416 /**
417 * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev()
418 * @dev : the device owning the devfreq-event device being created
419 * @edev : the devfreq-event device
420 *
421 * Note that this function manages automatically the memory of devfreq-event
422 * device using device resource management.
423 */
devm_devfreq_event_remove_edev(struct device * dev,struct devfreq_event_dev * edev)424 void devm_devfreq_event_remove_edev(struct device *dev,
425 struct devfreq_event_dev *edev)
426 {
427 WARN_ON(devres_release(dev, devm_devfreq_event_release,
428 devm_devfreq_event_match, edev));
429 }
430 EXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev);
431
432 /*
433 * Device attributes for devfreq-event class.
434 */
name_show(struct device * dev,struct device_attribute * attr,char * buf)435 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
436 char *buf)
437 {
438 struct devfreq_event_dev *edev = to_devfreq_event(dev);
439
440 if (!edev || !edev->desc)
441 return -EINVAL;
442
443 return sprintf(buf, "%s\n", edev->desc->name);
444 }
445 static DEVICE_ATTR_RO(name);
446
enable_count_show(struct device * dev,struct device_attribute * attr,char * buf)447 static ssize_t enable_count_show(struct device *dev,
448 struct device_attribute *attr, char *buf)
449 {
450 struct devfreq_event_dev *edev = to_devfreq_event(dev);
451
452 if (!edev || !edev->desc)
453 return -EINVAL;
454
455 return sprintf(buf, "%d\n", edev->enable_count);
456 }
457 static DEVICE_ATTR_RO(enable_count);
458
459 static struct attribute *devfreq_event_attrs[] = {
460 &dev_attr_name.attr,
461 &dev_attr_enable_count.attr,
462 NULL,
463 };
464 ATTRIBUTE_GROUPS(devfreq_event);
465
devfreq_event_init(void)466 static int __init devfreq_event_init(void)
467 {
468 devfreq_event_class = class_create("devfreq-event");
469 if (IS_ERR(devfreq_event_class)) {
470 pr_err("%s: couldn't create class\n", __FILE__);
471 return PTR_ERR(devfreq_event_class);
472 }
473
474 devfreq_event_class->dev_groups = devfreq_event_groups;
475
476 return 0;
477 }
478 subsys_initcall(devfreq_event_init);
479