18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * devfreq-event: a framework to provide raw data and events of devfreq devices 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Samsung Electronics 68c2ecf20Sopenharmony_ci * Author: Chanwoo Choi <cw00.choi@samsung.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * This driver is based on drivers/devfreq/devfreq.c. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/devfreq-event.h> 128c2ecf20Sopenharmony_ci#include <linux/kernel.h> 138c2ecf20Sopenharmony_ci#include <linux/err.h> 148c2ecf20Sopenharmony_ci#include <linux/init.h> 158c2ecf20Sopenharmony_ci#include <linux/export.h> 168c2ecf20Sopenharmony_ci#include <linux/slab.h> 178c2ecf20Sopenharmony_ci#include <linux/list.h> 188c2ecf20Sopenharmony_ci#include <linux/of.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistatic struct class *devfreq_event_class; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/* The list of all devfreq event list */ 238c2ecf20Sopenharmony_cistatic LIST_HEAD(devfreq_event_list); 248c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(devfreq_event_list_lock); 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define to_devfreq_event(DEV) container_of(DEV, struct devfreq_event_dev, dev) 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/** 298c2ecf20Sopenharmony_ci * devfreq_event_enable_edev() - Enable the devfreq-event dev and increase 308c2ecf20Sopenharmony_ci * the enable_count of devfreq-event dev. 318c2ecf20Sopenharmony_ci * @edev : the devfreq-event device 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * Note that this function increase the enable_count and enable the 348c2ecf20Sopenharmony_ci * devfreq-event device. The devfreq-event device should be enabled before 358c2ecf20Sopenharmony_ci * using it by devfreq device. 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_ciint devfreq_event_enable_edev(struct devfreq_event_dev *edev) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci int ret = 0; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci if (!edev || !edev->desc) 428c2ecf20Sopenharmony_ci return -EINVAL; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci mutex_lock(&edev->lock); 458c2ecf20Sopenharmony_ci if (edev->desc->ops && edev->desc->ops->enable 468c2ecf20Sopenharmony_ci && edev->enable_count == 0) { 478c2ecf20Sopenharmony_ci ret = edev->desc->ops->enable(edev); 488c2ecf20Sopenharmony_ci if (ret < 0) 498c2ecf20Sopenharmony_ci goto err; 508c2ecf20Sopenharmony_ci } 518c2ecf20Sopenharmony_ci edev->enable_count++; 528c2ecf20Sopenharmony_cierr: 538c2ecf20Sopenharmony_ci mutex_unlock(&edev->lock); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci return ret; 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_enable_edev); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci/** 608c2ecf20Sopenharmony_ci * devfreq_event_disable_edev() - Disable the devfreq-event dev and decrease 618c2ecf20Sopenharmony_ci * the enable_count of the devfreq-event dev. 628c2ecf20Sopenharmony_ci * @edev : the devfreq-event device 638c2ecf20Sopenharmony_ci * 648c2ecf20Sopenharmony_ci * Note that this function decrease the enable_count and disable the 658c2ecf20Sopenharmony_ci * devfreq-event device. After the devfreq-event device is disabled, 668c2ecf20Sopenharmony_ci * devfreq device can't use the devfreq-event device for get/set/reset 678c2ecf20Sopenharmony_ci * operations. 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_ciint devfreq_event_disable_edev(struct devfreq_event_dev *edev) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci int ret = 0; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci if (!edev || !edev->desc) 748c2ecf20Sopenharmony_ci return -EINVAL; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci mutex_lock(&edev->lock); 778c2ecf20Sopenharmony_ci if (edev->enable_count <= 0) { 788c2ecf20Sopenharmony_ci dev_warn(&edev->dev, "unbalanced enable_count\n"); 798c2ecf20Sopenharmony_ci ret = -EIO; 808c2ecf20Sopenharmony_ci goto err; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (edev->desc->ops && edev->desc->ops->disable 848c2ecf20Sopenharmony_ci && edev->enable_count == 1) { 858c2ecf20Sopenharmony_ci ret = edev->desc->ops->disable(edev); 868c2ecf20Sopenharmony_ci if (ret < 0) 878c2ecf20Sopenharmony_ci goto err; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci edev->enable_count--; 908c2ecf20Sopenharmony_cierr: 918c2ecf20Sopenharmony_ci mutex_unlock(&edev->lock); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci return ret; 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_disable_edev); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/** 988c2ecf20Sopenharmony_ci * devfreq_event_is_enabled() - Check whether devfreq-event dev is enabled or 998c2ecf20Sopenharmony_ci * not. 1008c2ecf20Sopenharmony_ci * @edev : the devfreq-event device 1018c2ecf20Sopenharmony_ci * 1028c2ecf20Sopenharmony_ci * Note that this function check whether devfreq-event dev is enabled or not. 1038c2ecf20Sopenharmony_ci * If return true, the devfreq-event dev is enabeld. If return false, the 1048c2ecf20Sopenharmony_ci * devfreq-event dev is disabled. 1058c2ecf20Sopenharmony_ci */ 1068c2ecf20Sopenharmony_cibool devfreq_event_is_enabled(struct devfreq_event_dev *edev) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci bool enabled = false; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci if (!edev || !edev->desc) 1118c2ecf20Sopenharmony_ci return enabled; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci mutex_lock(&edev->lock); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci if (edev->enable_count > 0) 1168c2ecf20Sopenharmony_ci enabled = true; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci mutex_unlock(&edev->lock); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return enabled; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_is_enabled); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/** 1258c2ecf20Sopenharmony_ci * devfreq_event_set_event() - Set event to devfreq-event dev to start. 1268c2ecf20Sopenharmony_ci * @edev : the devfreq-event device 1278c2ecf20Sopenharmony_ci * 1288c2ecf20Sopenharmony_ci * Note that this function set the event to the devfreq-event device to start 1298c2ecf20Sopenharmony_ci * for getting the event data which could be various event type. 1308c2ecf20Sopenharmony_ci */ 1318c2ecf20Sopenharmony_ciint devfreq_event_set_event(struct devfreq_event_dev *edev) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci int ret; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (!edev || !edev->desc) 1368c2ecf20Sopenharmony_ci return -EINVAL; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci if (!edev->desc->ops || !edev->desc->ops->set_event) 1398c2ecf20Sopenharmony_ci return -EINVAL; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci if (!devfreq_event_is_enabled(edev)) 1428c2ecf20Sopenharmony_ci return -EPERM; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci mutex_lock(&edev->lock); 1458c2ecf20Sopenharmony_ci ret = edev->desc->ops->set_event(edev); 1468c2ecf20Sopenharmony_ci mutex_unlock(&edev->lock); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return ret; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_set_event); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci/** 1538c2ecf20Sopenharmony_ci * devfreq_event_get_event() - Get {load|total}_count from devfreq-event dev. 1548c2ecf20Sopenharmony_ci * @edev : the devfreq-event device 1558c2ecf20Sopenharmony_ci * @edata : the calculated data of devfreq-event device 1568c2ecf20Sopenharmony_ci * 1578c2ecf20Sopenharmony_ci * Note that this function get the calculated event data from devfreq-event dev 1588c2ecf20Sopenharmony_ci * after stoping the progress of whole sequence of devfreq-event dev. 1598c2ecf20Sopenharmony_ci */ 1608c2ecf20Sopenharmony_ciint devfreq_event_get_event(struct devfreq_event_dev *edev, 1618c2ecf20Sopenharmony_ci struct devfreq_event_data *edata) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci int ret; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if (!edev || !edev->desc) 1668c2ecf20Sopenharmony_ci return -EINVAL; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (!edev->desc->ops || !edev->desc->ops->get_event) 1698c2ecf20Sopenharmony_ci return -EINVAL; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci if (!devfreq_event_is_enabled(edev)) 1728c2ecf20Sopenharmony_ci return -EINVAL; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci edata->total_count = edata->load_count = 0; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci mutex_lock(&edev->lock); 1778c2ecf20Sopenharmony_ci ret = edev->desc->ops->get_event(edev, edata); 1788c2ecf20Sopenharmony_ci if (ret < 0) 1798c2ecf20Sopenharmony_ci edata->total_count = edata->load_count = 0; 1808c2ecf20Sopenharmony_ci mutex_unlock(&edev->lock); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci return ret; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_get_event); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci/** 1878c2ecf20Sopenharmony_ci * devfreq_event_reset_event() - Reset all opeations of devfreq-event dev. 1888c2ecf20Sopenharmony_ci * @edev : the devfreq-event device 1898c2ecf20Sopenharmony_ci * 1908c2ecf20Sopenharmony_ci * Note that this function stop all operations of devfreq-event dev and reset 1918c2ecf20Sopenharmony_ci * the current event data to make the devfreq-event device into initial state. 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_ciint devfreq_event_reset_event(struct devfreq_event_dev *edev) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci int ret = 0; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci if (!edev || !edev->desc) 1988c2ecf20Sopenharmony_ci return -EINVAL; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci if (!devfreq_event_is_enabled(edev)) 2018c2ecf20Sopenharmony_ci return -EPERM; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci mutex_lock(&edev->lock); 2048c2ecf20Sopenharmony_ci if (edev->desc->ops && edev->desc->ops->reset) 2058c2ecf20Sopenharmony_ci ret = edev->desc->ops->reset(edev); 2068c2ecf20Sopenharmony_ci mutex_unlock(&edev->lock); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci return ret; 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_reset_event); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci/** 2138c2ecf20Sopenharmony_ci * devfreq_event_get_edev_by_phandle() - Get the devfreq-event dev from 2148c2ecf20Sopenharmony_ci * devicetree. 2158c2ecf20Sopenharmony_ci * @dev : the pointer to the given device 2168c2ecf20Sopenharmony_ci * @phandle_name: name of property holding a phandle value 2178c2ecf20Sopenharmony_ci * @index : the index into list of devfreq-event device 2188c2ecf20Sopenharmony_ci * 2198c2ecf20Sopenharmony_ci * Note that this function return the pointer of devfreq-event device. 2208c2ecf20Sopenharmony_ci */ 2218c2ecf20Sopenharmony_cistruct devfreq_event_dev *devfreq_event_get_edev_by_phandle(struct device *dev, 2228c2ecf20Sopenharmony_ci const char *phandle_name, int index) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci struct device_node *node; 2258c2ecf20Sopenharmony_ci struct devfreq_event_dev *edev; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci if (!dev->of_node || !phandle_name) 2288c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci node = of_parse_phandle(dev->of_node, phandle_name, index); 2318c2ecf20Sopenharmony_ci if (!node) 2328c2ecf20Sopenharmony_ci return ERR_PTR(-ENODEV); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci mutex_lock(&devfreq_event_list_lock); 2358c2ecf20Sopenharmony_ci list_for_each_entry(edev, &devfreq_event_list, node) { 2368c2ecf20Sopenharmony_ci if (edev->dev.parent && edev->dev.parent->of_node == node) 2378c2ecf20Sopenharmony_ci goto out; 2388c2ecf20Sopenharmony_ci } 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci list_for_each_entry(edev, &devfreq_event_list, node) { 2418c2ecf20Sopenharmony_ci if (of_node_name_eq(node, edev->desc->name)) 2428c2ecf20Sopenharmony_ci goto out; 2438c2ecf20Sopenharmony_ci } 2448c2ecf20Sopenharmony_ci edev = NULL; 2458c2ecf20Sopenharmony_ciout: 2468c2ecf20Sopenharmony_ci mutex_unlock(&devfreq_event_list_lock); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci if (!edev) { 2498c2ecf20Sopenharmony_ci of_node_put(node); 2508c2ecf20Sopenharmony_ci return ERR_PTR(-ENODEV); 2518c2ecf20Sopenharmony_ci } 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci of_node_put(node); 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci return edev; 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci/** 2608c2ecf20Sopenharmony_ci * devfreq_event_get_edev_count() - Get the count of devfreq-event dev 2618c2ecf20Sopenharmony_ci * @dev : the pointer to the given device 2628c2ecf20Sopenharmony_ci * @phandle_name: name of property holding a phandle value 2638c2ecf20Sopenharmony_ci * 2648c2ecf20Sopenharmony_ci * Note that this function return the count of devfreq-event devices. 2658c2ecf20Sopenharmony_ci */ 2668c2ecf20Sopenharmony_ciint devfreq_event_get_edev_count(struct device *dev, const char *phandle_name) 2678c2ecf20Sopenharmony_ci{ 2688c2ecf20Sopenharmony_ci int count; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci if (!dev->of_node || !phandle_name) { 2718c2ecf20Sopenharmony_ci dev_err(dev, "device does not have a device node entry\n"); 2728c2ecf20Sopenharmony_ci return -EINVAL; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci count = of_property_count_elems_of_size(dev->of_node, phandle_name, 2768c2ecf20Sopenharmony_ci sizeof(u32)); 2778c2ecf20Sopenharmony_ci if (count < 0) { 2788c2ecf20Sopenharmony_ci dev_err(dev, 2798c2ecf20Sopenharmony_ci "failed to get the count of devfreq-event in %pOF node\n", 2808c2ecf20Sopenharmony_ci dev->of_node); 2818c2ecf20Sopenharmony_ci return count; 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci return count; 2858c2ecf20Sopenharmony_ci} 2868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_get_edev_count); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_cistatic void devfreq_event_release_edev(struct device *dev) 2898c2ecf20Sopenharmony_ci{ 2908c2ecf20Sopenharmony_ci struct devfreq_event_dev *edev = to_devfreq_event(dev); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci kfree(edev); 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci/** 2968c2ecf20Sopenharmony_ci * devfreq_event_add_edev() - Add new devfreq-event device. 2978c2ecf20Sopenharmony_ci * @dev : the device owning the devfreq-event device being created 2988c2ecf20Sopenharmony_ci * @desc : the devfreq-event device's descriptor which include essential 2998c2ecf20Sopenharmony_ci * data for devfreq-event device. 3008c2ecf20Sopenharmony_ci * 3018c2ecf20Sopenharmony_ci * Note that this function add new devfreq-event device to devfreq-event class 3028c2ecf20Sopenharmony_ci * list and register the device of the devfreq-event device. 3038c2ecf20Sopenharmony_ci */ 3048c2ecf20Sopenharmony_cistruct devfreq_event_dev *devfreq_event_add_edev(struct device *dev, 3058c2ecf20Sopenharmony_ci struct devfreq_event_desc *desc) 3068c2ecf20Sopenharmony_ci{ 3078c2ecf20Sopenharmony_ci struct devfreq_event_dev *edev; 3088c2ecf20Sopenharmony_ci static atomic_t event_no = ATOMIC_INIT(-1); 3098c2ecf20Sopenharmony_ci int ret; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci if (!dev || !desc) 3128c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci if (!desc->name || !desc->ops) 3158c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci if (!desc->ops->set_event || !desc->ops->get_event) 3188c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci edev = kzalloc(sizeof(struct devfreq_event_dev), GFP_KERNEL); 3218c2ecf20Sopenharmony_ci if (!edev) 3228c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci mutex_init(&edev->lock); 3258c2ecf20Sopenharmony_ci edev->desc = desc; 3268c2ecf20Sopenharmony_ci edev->enable_count = 0; 3278c2ecf20Sopenharmony_ci edev->dev.parent = dev; 3288c2ecf20Sopenharmony_ci edev->dev.class = devfreq_event_class; 3298c2ecf20Sopenharmony_ci edev->dev.release = devfreq_event_release_edev; 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci dev_set_name(&edev->dev, "event%d", atomic_inc_return(&event_no)); 3328c2ecf20Sopenharmony_ci ret = device_register(&edev->dev); 3338c2ecf20Sopenharmony_ci if (ret < 0) { 3348c2ecf20Sopenharmony_ci put_device(&edev->dev); 3358c2ecf20Sopenharmony_ci return ERR_PTR(ret); 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci dev_set_drvdata(&edev->dev, edev); 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&edev->node); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci mutex_lock(&devfreq_event_list_lock); 3428c2ecf20Sopenharmony_ci list_add(&edev->node, &devfreq_event_list); 3438c2ecf20Sopenharmony_ci mutex_unlock(&devfreq_event_list_lock); 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci return edev; 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_add_edev); 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci/** 3508c2ecf20Sopenharmony_ci * devfreq_event_remove_edev() - Remove the devfreq-event device registered. 3518c2ecf20Sopenharmony_ci * @edev : the devfreq-event device 3528c2ecf20Sopenharmony_ci * 3538c2ecf20Sopenharmony_ci * Note that this function removes the registered devfreq-event device. 3548c2ecf20Sopenharmony_ci */ 3558c2ecf20Sopenharmony_ciint devfreq_event_remove_edev(struct devfreq_event_dev *edev) 3568c2ecf20Sopenharmony_ci{ 3578c2ecf20Sopenharmony_ci if (!edev) 3588c2ecf20Sopenharmony_ci return -EINVAL; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci WARN_ON(edev->enable_count); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci mutex_lock(&devfreq_event_list_lock); 3638c2ecf20Sopenharmony_ci list_del(&edev->node); 3648c2ecf20Sopenharmony_ci mutex_unlock(&devfreq_event_list_lock); 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci device_unregister(&edev->dev); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci return 0; 3698c2ecf20Sopenharmony_ci} 3708c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devfreq_event_remove_edev); 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_cistatic int devm_devfreq_event_match(struct device *dev, void *res, void *data) 3738c2ecf20Sopenharmony_ci{ 3748c2ecf20Sopenharmony_ci struct devfreq_event_dev **r = res; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci if (WARN_ON(!r || !*r)) 3778c2ecf20Sopenharmony_ci return 0; 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci return *r == data; 3808c2ecf20Sopenharmony_ci} 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_cistatic void devm_devfreq_event_release(struct device *dev, void *res) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci devfreq_event_remove_edev(*(struct devfreq_event_dev **)res); 3858c2ecf20Sopenharmony_ci} 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci/** 3888c2ecf20Sopenharmony_ci * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev() 3898c2ecf20Sopenharmony_ci * @dev : the device owning the devfreq-event device being created 3908c2ecf20Sopenharmony_ci * @desc : the devfreq-event device's descriptor which include essential 3918c2ecf20Sopenharmony_ci * data for devfreq-event device. 3928c2ecf20Sopenharmony_ci * 3938c2ecf20Sopenharmony_ci * Note that this function manages automatically the memory of devfreq-event 3948c2ecf20Sopenharmony_ci * device using device resource management and simplify the free operation 3958c2ecf20Sopenharmony_ci * for memory of devfreq-event device. 3968c2ecf20Sopenharmony_ci */ 3978c2ecf20Sopenharmony_cistruct devfreq_event_dev *devm_devfreq_event_add_edev(struct device *dev, 3988c2ecf20Sopenharmony_ci struct devfreq_event_desc *desc) 3998c2ecf20Sopenharmony_ci{ 4008c2ecf20Sopenharmony_ci struct devfreq_event_dev **ptr, *edev; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci ptr = devres_alloc(devm_devfreq_event_release, sizeof(*ptr), 4038c2ecf20Sopenharmony_ci GFP_KERNEL); 4048c2ecf20Sopenharmony_ci if (!ptr) 4058c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci edev = devfreq_event_add_edev(dev, desc); 4088c2ecf20Sopenharmony_ci if (IS_ERR(edev)) { 4098c2ecf20Sopenharmony_ci devres_free(ptr); 4108c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4118c2ecf20Sopenharmony_ci } 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci *ptr = edev; 4148c2ecf20Sopenharmony_ci devres_add(dev, ptr); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci return edev; 4178c2ecf20Sopenharmony_ci} 4188c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev); 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci/** 4218c2ecf20Sopenharmony_ci * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev() 4228c2ecf20Sopenharmony_ci * @dev : the device owning the devfreq-event device being created 4238c2ecf20Sopenharmony_ci * @edev : the devfreq-event device 4248c2ecf20Sopenharmony_ci * 4258c2ecf20Sopenharmony_ci * Note that this function manages automatically the memory of devfreq-event 4268c2ecf20Sopenharmony_ci * device using device resource management. 4278c2ecf20Sopenharmony_ci */ 4288c2ecf20Sopenharmony_civoid devm_devfreq_event_remove_edev(struct device *dev, 4298c2ecf20Sopenharmony_ci struct devfreq_event_dev *edev) 4308c2ecf20Sopenharmony_ci{ 4318c2ecf20Sopenharmony_ci WARN_ON(devres_release(dev, devm_devfreq_event_release, 4328c2ecf20Sopenharmony_ci devm_devfreq_event_match, edev)); 4338c2ecf20Sopenharmony_ci} 4348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev); 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci/* 4378c2ecf20Sopenharmony_ci * Device attributes for devfreq-event class. 4388c2ecf20Sopenharmony_ci */ 4398c2ecf20Sopenharmony_cistatic ssize_t name_show(struct device *dev, struct device_attribute *attr, 4408c2ecf20Sopenharmony_ci char *buf) 4418c2ecf20Sopenharmony_ci{ 4428c2ecf20Sopenharmony_ci struct devfreq_event_dev *edev = to_devfreq_event(dev); 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci if (!edev || !edev->desc) 4458c2ecf20Sopenharmony_ci return -EINVAL; 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci return sprintf(buf, "%s\n", edev->desc->name); 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(name); 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_cistatic ssize_t enable_count_show(struct device *dev, 4528c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 4538c2ecf20Sopenharmony_ci{ 4548c2ecf20Sopenharmony_ci struct devfreq_event_dev *edev = to_devfreq_event(dev); 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci if (!edev || !edev->desc) 4578c2ecf20Sopenharmony_ci return -EINVAL; 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", edev->enable_count); 4608c2ecf20Sopenharmony_ci} 4618c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(enable_count); 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_cistatic struct attribute *devfreq_event_attrs[] = { 4648c2ecf20Sopenharmony_ci &dev_attr_name.attr, 4658c2ecf20Sopenharmony_ci &dev_attr_enable_count.attr, 4668c2ecf20Sopenharmony_ci NULL, 4678c2ecf20Sopenharmony_ci}; 4688c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(devfreq_event); 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_cistatic int __init devfreq_event_init(void) 4718c2ecf20Sopenharmony_ci{ 4728c2ecf20Sopenharmony_ci devfreq_event_class = class_create(THIS_MODULE, "devfreq-event"); 4738c2ecf20Sopenharmony_ci if (IS_ERR(devfreq_event_class)) { 4748c2ecf20Sopenharmony_ci pr_err("%s: couldn't create class\n", __FILE__); 4758c2ecf20Sopenharmony_ci return PTR_ERR(devfreq_event_class); 4768c2ecf20Sopenharmony_ci } 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci devfreq_event_class->dev_groups = devfreq_event_groups; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci return 0; 4818c2ecf20Sopenharmony_ci} 4828c2ecf20Sopenharmony_cisubsys_initcall(devfreq_event_init); 483