Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * devfreq-event: a framework to provide raw data and events of devfreq devices
0004  *
0005  * Copyright (C) 2015 Samsung Electronics
0006  * Author: Chanwoo Choi <cw00.choi@samsung.com>
0007  *
0008  * This driver is based on drivers/devfreq/devfreq.c.
0009  */
0010 
0011 #include <linux/devfreq-event.h>
0012 #include <linux/kernel.h>
0013 #include <linux/err.h>
0014 #include <linux/init.h>
0015 #include <linux/export.h>
0016 #include <linux/slab.h>
0017 #include <linux/list.h>
0018 #include <linux/of.h>
0019 
0020 static struct class *devfreq_event_class;
0021 
0022 /* The list of all devfreq event list */
0023 static LIST_HEAD(devfreq_event_list);
0024 static DEFINE_MUTEX(devfreq_event_list_lock);
0025 
0026 #define to_devfreq_event(DEV) container_of(DEV, struct devfreq_event_dev, dev)
0027 
0028 /**
0029  * devfreq_event_enable_edev() - Enable the devfreq-event dev and increase
0030  *               the enable_count of devfreq-event dev.
0031  * @edev    : the devfreq-event device
0032  *
0033  * Note that this function increase the enable_count and enable the
0034  * devfreq-event device. The devfreq-event device should be enabled before
0035  * using it by devfreq device.
0036  */
0037 int devfreq_event_enable_edev(struct devfreq_event_dev *edev)
0038 {
0039     int ret = 0;
0040 
0041     if (!edev || !edev->desc)
0042         return -EINVAL;
0043 
0044     mutex_lock(&edev->lock);
0045     if (edev->desc->ops && edev->desc->ops->enable
0046             && edev->enable_count == 0) {
0047         ret = edev->desc->ops->enable(edev);
0048         if (ret < 0)
0049             goto err;
0050     }
0051     edev->enable_count++;
0052 err:
0053     mutex_unlock(&edev->lock);
0054 
0055     return ret;
0056 }
0057 EXPORT_SYMBOL_GPL(devfreq_event_enable_edev);
0058 
0059 /**
0060  * devfreq_event_disable_edev() - Disable the devfreq-event dev and decrease
0061  *                the enable_count of the devfreq-event dev.
0062  * @edev    : the devfreq-event device
0063  *
0064  * Note that this function decrease the enable_count and disable the
0065  * devfreq-event device. After the devfreq-event device is disabled,
0066  * devfreq device can't use the devfreq-event device for get/set/reset
0067  * operations.
0068  */
0069 int devfreq_event_disable_edev(struct devfreq_event_dev *edev)
0070 {
0071     int ret = 0;
0072 
0073     if (!edev || !edev->desc)
0074         return -EINVAL;
0075 
0076     mutex_lock(&edev->lock);
0077     if (edev->enable_count <= 0) {
0078         dev_warn(&edev->dev, "unbalanced enable_count\n");
0079         ret = -EIO;
0080         goto err;
0081     }
0082 
0083     if (edev->desc->ops && edev->desc->ops->disable
0084             && edev->enable_count == 1) {
0085         ret = edev->desc->ops->disable(edev);
0086         if (ret < 0)
0087             goto err;
0088     }
0089     edev->enable_count--;
0090 err:
0091     mutex_unlock(&edev->lock);
0092 
0093     return ret;
0094 }
0095 EXPORT_SYMBOL_GPL(devfreq_event_disable_edev);
0096 
0097 /**
0098  * devfreq_event_is_enabled() - Check whether devfreq-event dev is enabled or
0099  *              not.
0100  * @edev    : the devfreq-event device
0101  *
0102  * Note that this function check whether devfreq-event dev is enabled or not.
0103  * If return true, the devfreq-event dev is enabeld. If return false, the
0104  * devfreq-event dev is disabled.
0105  */
0106 bool devfreq_event_is_enabled(struct devfreq_event_dev *edev)
0107 {
0108     bool enabled = false;
0109 
0110     if (!edev || !edev->desc)
0111         return enabled;
0112 
0113     mutex_lock(&edev->lock);
0114 
0115     if (edev->enable_count > 0)
0116         enabled = true;
0117 
0118     mutex_unlock(&edev->lock);
0119 
0120     return enabled;
0121 }
0122 EXPORT_SYMBOL_GPL(devfreq_event_is_enabled);
0123 
0124 /**
0125  * devfreq_event_set_event() - Set event to devfreq-event dev to start.
0126  * @edev    : the devfreq-event device
0127  *
0128  * Note that this function set the event to the devfreq-event device to start
0129  * for getting the event data which could be various event type.
0130  */
0131 int devfreq_event_set_event(struct devfreq_event_dev *edev)
0132 {
0133     int ret;
0134 
0135     if (!edev || !edev->desc)
0136         return -EINVAL;
0137 
0138     if (!edev->desc->ops || !edev->desc->ops->set_event)
0139         return -EINVAL;
0140 
0141     if (!devfreq_event_is_enabled(edev))
0142         return -EPERM;
0143 
0144     mutex_lock(&edev->lock);
0145     ret = edev->desc->ops->set_event(edev);
0146     mutex_unlock(&edev->lock);
0147 
0148     return ret;
0149 }
0150 EXPORT_SYMBOL_GPL(devfreq_event_set_event);
0151 
0152 /**
0153  * devfreq_event_get_event() - Get {load|total}_count from devfreq-event dev.
0154  * @edev    : the devfreq-event device
0155  * @edata   : the calculated data of devfreq-event device
0156  *
0157  * Note that this function get the calculated event data from devfreq-event dev
0158  * after stoping the progress of whole sequence of devfreq-event dev.
0159  */
0160 int devfreq_event_get_event(struct devfreq_event_dev *edev,
0161                 struct devfreq_event_data *edata)
0162 {
0163     int ret;
0164 
0165     if (!edev || !edev->desc)
0166         return -EINVAL;
0167 
0168     if (!edev->desc->ops || !edev->desc->ops->get_event)
0169         return -EINVAL;
0170 
0171     if (!devfreq_event_is_enabled(edev))
0172         return -EINVAL;
0173 
0174     edata->total_count = edata->load_count = 0;
0175 
0176     mutex_lock(&edev->lock);
0177     ret = edev->desc->ops->get_event(edev, edata);
0178     if (ret < 0)
0179         edata->total_count = edata->load_count = 0;
0180     mutex_unlock(&edev->lock);
0181 
0182     return ret;
0183 }
0184 EXPORT_SYMBOL_GPL(devfreq_event_get_event);
0185 
0186 /**
0187  * devfreq_event_reset_event() - Reset all opeations of devfreq-event dev.
0188  * @edev    : the devfreq-event device
0189  *
0190  * Note that this function stop all operations of devfreq-event dev and reset
0191  * the current event data to make the devfreq-event device into initial state.
0192  */
0193 int devfreq_event_reset_event(struct devfreq_event_dev *edev)
0194 {
0195     int ret = 0;
0196 
0197     if (!edev || !edev->desc)
0198         return -EINVAL;
0199 
0200     if (!devfreq_event_is_enabled(edev))
0201         return -EPERM;
0202 
0203     mutex_lock(&edev->lock);
0204     if (edev->desc->ops && edev->desc->ops->reset)
0205         ret = edev->desc->ops->reset(edev);
0206     mutex_unlock(&edev->lock);
0207 
0208     return ret;
0209 }
0210 EXPORT_SYMBOL_GPL(devfreq_event_reset_event);
0211 
0212 /**
0213  * devfreq_event_get_edev_by_phandle() - Get the devfreq-event dev from
0214  *                   devicetree.
0215  * @dev     : the pointer to the given device
0216  * @phandle_name: name of property holding a phandle value
0217  * @index   : the index into list of devfreq-event device
0218  *
0219  * Note that this function return the pointer of devfreq-event device.
0220  */
0221 struct devfreq_event_dev *devfreq_event_get_edev_by_phandle(struct device *dev,
0222                     const char *phandle_name, int index)
0223 {
0224     struct device_node *node;
0225     struct devfreq_event_dev *edev;
0226 
0227     if (!dev->of_node || !phandle_name)
0228         return ERR_PTR(-EINVAL);
0229 
0230     node = of_parse_phandle(dev->of_node, phandle_name, index);
0231     if (!node)
0232         return ERR_PTR(-ENODEV);
0233 
0234     mutex_lock(&devfreq_event_list_lock);
0235     list_for_each_entry(edev, &devfreq_event_list, node) {
0236         if (edev->dev.parent && edev->dev.parent->of_node == node)
0237             goto out;
0238     }
0239 
0240     list_for_each_entry(edev, &devfreq_event_list, node) {
0241         if (of_node_name_eq(node, edev->desc->name))
0242             goto out;
0243     }
0244     edev = NULL;
0245 out:
0246     mutex_unlock(&devfreq_event_list_lock);
0247 
0248     if (!edev) {
0249         of_node_put(node);
0250         return ERR_PTR(-ENODEV);
0251     }
0252 
0253     of_node_put(node);
0254 
0255     return edev;
0256 }
0257 EXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle);
0258 
0259 /**
0260  * devfreq_event_get_edev_count() - Get the count of devfreq-event dev
0261  * @dev     : the pointer to the given device
0262  * @phandle_name: name of property holding a phandle value
0263  *
0264  * Note that this function return the count of devfreq-event devices.
0265  */
0266 int devfreq_event_get_edev_count(struct device *dev, const char *phandle_name)
0267 {
0268     int count;
0269 
0270     if (!dev->of_node || !phandle_name) {
0271         dev_err(dev, "device does not have a device node entry\n");
0272         return -EINVAL;
0273     }
0274 
0275     count = of_property_count_elems_of_size(dev->of_node, phandle_name,
0276                         sizeof(u32));
0277     if (count < 0) {
0278         dev_err(dev,
0279             "failed to get the count of devfreq-event in %pOF node\n",
0280             dev->of_node);
0281         return count;
0282     }
0283 
0284     return count;
0285 }
0286 EXPORT_SYMBOL_GPL(devfreq_event_get_edev_count);
0287 
0288 static void devfreq_event_release_edev(struct device *dev)
0289 {
0290     struct devfreq_event_dev *edev = to_devfreq_event(dev);
0291 
0292     kfree(edev);
0293 }
0294 
0295 /**
0296  * devfreq_event_add_edev() - Add new devfreq-event device.
0297  * @dev     : the device owning the devfreq-event device being created
0298  * @desc    : the devfreq-event device's descriptor which include essential
0299  *        data for devfreq-event device.
0300  *
0301  * Note that this function add new devfreq-event device to devfreq-event class
0302  * list and register the device of the devfreq-event device.
0303  */
0304 struct devfreq_event_dev *devfreq_event_add_edev(struct device *dev,
0305                         struct devfreq_event_desc *desc)
0306 {
0307     struct devfreq_event_dev *edev;
0308     static atomic_t event_no = ATOMIC_INIT(-1);
0309     int ret;
0310 
0311     if (!dev || !desc)
0312         return ERR_PTR(-EINVAL);
0313 
0314     if (!desc->name || !desc->ops)
0315         return ERR_PTR(-EINVAL);
0316 
0317     if (!desc->ops->set_event || !desc->ops->get_event)
0318         return ERR_PTR(-EINVAL);
0319 
0320     edev = kzalloc(sizeof(struct devfreq_event_dev), GFP_KERNEL);
0321     if (!edev)
0322         return ERR_PTR(-ENOMEM);
0323 
0324     mutex_init(&edev->lock);
0325     edev->desc = desc;
0326     edev->enable_count = 0;
0327     edev->dev.parent = dev;
0328     edev->dev.class = devfreq_event_class;
0329     edev->dev.release = devfreq_event_release_edev;
0330 
0331     dev_set_name(&edev->dev, "event%d", atomic_inc_return(&event_no));
0332     ret = device_register(&edev->dev);
0333     if (ret < 0) {
0334         put_device(&edev->dev);
0335         return ERR_PTR(ret);
0336     }
0337     dev_set_drvdata(&edev->dev, edev);
0338 
0339     INIT_LIST_HEAD(&edev->node);
0340 
0341     mutex_lock(&devfreq_event_list_lock);
0342     list_add(&edev->node, &devfreq_event_list);
0343     mutex_unlock(&devfreq_event_list_lock);
0344 
0345     return edev;
0346 }
0347 EXPORT_SYMBOL_GPL(devfreq_event_add_edev);
0348 
0349 /**
0350  * devfreq_event_remove_edev() - Remove the devfreq-event device registered.
0351  * @edev    : the devfreq-event device
0352  *
0353  * Note that this function removes the registered devfreq-event device.
0354  */
0355 int devfreq_event_remove_edev(struct devfreq_event_dev *edev)
0356 {
0357     if (!edev)
0358         return -EINVAL;
0359 
0360     WARN_ON(edev->enable_count);
0361 
0362     mutex_lock(&devfreq_event_list_lock);
0363     list_del(&edev->node);
0364     mutex_unlock(&devfreq_event_list_lock);
0365 
0366     device_unregister(&edev->dev);
0367 
0368     return 0;
0369 }
0370 EXPORT_SYMBOL_GPL(devfreq_event_remove_edev);
0371 
0372 static int devm_devfreq_event_match(struct device *dev, void *res, void *data)
0373 {
0374     struct devfreq_event_dev **r = res;
0375 
0376     if (WARN_ON(!r || !*r))
0377         return 0;
0378 
0379     return *r == data;
0380 }
0381 
0382 static void devm_devfreq_event_release(struct device *dev, void *res)
0383 {
0384     devfreq_event_remove_edev(*(struct devfreq_event_dev **)res);
0385 }
0386 
0387 /**
0388  * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev()
0389  * @dev     : the device owning the devfreq-event device being created
0390  * @desc    : the devfreq-event device's descriptor which include essential
0391  *        data for devfreq-event device.
0392  *
0393  * Note that this function manages automatically the memory of devfreq-event
0394  * device using device resource management and simplify the free operation
0395  * for memory of devfreq-event device.
0396  */
0397 struct devfreq_event_dev *devm_devfreq_event_add_edev(struct device *dev,
0398                         struct devfreq_event_desc *desc)
0399 {
0400     struct devfreq_event_dev **ptr, *edev;
0401 
0402     ptr = devres_alloc(devm_devfreq_event_release, sizeof(*ptr),
0403                 GFP_KERNEL);
0404     if (!ptr)
0405         return ERR_PTR(-ENOMEM);
0406 
0407     edev = devfreq_event_add_edev(dev, desc);
0408     if (IS_ERR(edev)) {
0409         devres_free(ptr);
0410         return ERR_PTR(-ENOMEM);
0411     }
0412 
0413     *ptr = edev;
0414     devres_add(dev, ptr);
0415 
0416     return edev;
0417 }
0418 EXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev);
0419 
0420 /**
0421  * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev()
0422  * @dev     : the device owning the devfreq-event device being created
0423  * @edev    : the devfreq-event device
0424  *
0425  * Note that this function manages automatically the memory of devfreq-event
0426  * device using device resource management.
0427  */
0428 void devm_devfreq_event_remove_edev(struct device *dev,
0429                 struct devfreq_event_dev *edev)
0430 {
0431     WARN_ON(devres_release(dev, devm_devfreq_event_release,
0432                    devm_devfreq_event_match, edev));
0433 }
0434 EXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev);
0435 
0436 /*
0437  * Device attributes for devfreq-event class.
0438  */
0439 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
0440              char *buf)
0441 {
0442     struct devfreq_event_dev *edev = to_devfreq_event(dev);
0443 
0444     if (!edev || !edev->desc)
0445         return -EINVAL;
0446 
0447     return sprintf(buf, "%s\n", edev->desc->name);
0448 }
0449 static DEVICE_ATTR_RO(name);
0450 
0451 static ssize_t enable_count_show(struct device *dev,
0452                   struct device_attribute *attr, char *buf)
0453 {
0454     struct devfreq_event_dev *edev = to_devfreq_event(dev);
0455 
0456     if (!edev || !edev->desc)
0457         return -EINVAL;
0458 
0459     return sprintf(buf, "%d\n", edev->enable_count);
0460 }
0461 static DEVICE_ATTR_RO(enable_count);
0462 
0463 static struct attribute *devfreq_event_attrs[] = {
0464     &dev_attr_name.attr,
0465     &dev_attr_enable_count.attr,
0466     NULL,
0467 };
0468 ATTRIBUTE_GROUPS(devfreq_event);
0469 
0470 static int __init devfreq_event_init(void)
0471 {
0472     devfreq_event_class = class_create(THIS_MODULE, "devfreq-event");
0473     if (IS_ERR(devfreq_event_class)) {
0474         pr_err("%s: couldn't create class\n", __FILE__);
0475         return PTR_ERR(devfreq_event_class);
0476     }
0477 
0478     devfreq_event_class->dev_groups = devfreq_event_groups;
0479 
0480     return 0;
0481 }
0482 subsys_initcall(devfreq_event_init);