| 1 | |
| 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 attribute *devfreq_event_attrs[];
|
| 21 | ATTRIBUTE_GROUPS(devfreq_event);
|
| 22 |
|
| 23 | static const struct class devfreq_event_class = {
|
| 24 | .name = "devfreq-event",
|
| 25 | .dev_groups = devfreq_event_groups
|
| 26 | };
|
| 27 |
|
| 28 |
|
| 29 | static LIST_HEAD(devfreq_event_list);
|
| 30 | static DEFINE_MUTEX(devfreq_event_list_lock);
|
| 31 |
|
| 32 | #define to_devfreq_event(DEV) container_of(DEV, struct devfreq_event_dev, dev)
|
| 33 |
|
| 34 | |
| 35 | * devfreq_event_enable_edev() - Enable the devfreq-event dev and increase
|
| 36 | * the enable_count of devfreq-event dev.
|
| 37 | * @edev : the devfreq-event device
|
| 38 | *
|
| 39 | * Note that this function increase the enable_count and enable the
|
| 40 | * devfreq-event device. The devfreq-event device should be enabled before
|
| 41 | * using it by devfreq device.
|
| 42 |
|
| 43 | int devfreq_event_enable_edev(struct devfreq_event_dev *edev)
|
| 44 | {
|
| 45 | int ret = 0;
|
| 46 |
|
| 47 | if (!edev || !edev->desc)
|
| 48 | return -EINVAL;
|
| 49 |
|
| 50 | mutex_lock(&edev->lock);
|
| 51 | if (edev->desc->ops && edev->desc->ops->enable
|
| 52 | && edev->enable_count == 0) {
|
| 53 | ret = edev->desc->ops->enable(edev);
|
| 54 | if (ret < 0)
|
| 55 | goto err;
|
| 56 | }
|
| 57 | edev->enable_count++;
|
| 58 | err:
|
| 59 | mutex_unlock(&edev->lock);
|
| 60 |
|
| 61 | return ret;
|
| 62 | }
|
| 63 | EXPORT_SYMBOL_GPL(devfreq_event_enable_edev);
|
| 64 |
|
| 65 | |
| 66 | * devfreq_event_disable_edev() - Disable the devfreq-event dev and decrease
|
| 67 | * the enable_count of the devfreq-event dev.
|
| 68 | * @edev : the devfreq-event device
|
| 69 | *
|
| 70 | * Note that this function decrease the enable_count and disable the
|
| 71 | * devfreq-event device. After the devfreq-event device is disabled,
|
| 72 | * devfreq device can't use the devfreq-event device for get/set/reset
|
| 73 | * operations.
|
| 74 |
|
| 75 | int devfreq_event_disable_edev(struct devfreq_event_dev *edev)
|
| 76 | {
|
| 77 | int ret = 0;
|
| 78 |
|
| 79 | if (!edev || !edev->desc)
|
| 80 | return -EINVAL;
|
| 81 |
|
| 82 | mutex_lock(&edev->lock);
|
| 83 | if (edev->enable_count <= 0) {
|
| 84 | dev_warn(&edev->dev, "unbalanced enable_count\n");
|
| 85 | ret = -EIO;
|
| 86 | goto err;
|
| 87 | }
|
| 88 |
|
| 89 | if (edev->desc->ops && edev->desc->ops->disable
|
| 90 | && edev->enable_count == 1) {
|
| 91 | ret = edev->desc->ops->disable(edev);
|
| 92 | if (ret < 0)
|
| 93 | goto err;
|
| 94 | }
|
| 95 | edev->enable_count--;
|
| 96 | err:
|
| 97 | mutex_unlock(&edev->lock);
|
| 98 |
|
| 99 | return ret;
|
| 100 | }
|
| 101 | EXPORT_SYMBOL_GPL(devfreq_event_disable_edev);
|
| 102 |
|
| 103 | |
| 104 | * devfreq_event_is_enabled() - Check whether devfreq-event dev is enabled or
|
| 105 | * not.
|
| 106 | * @edev : the devfreq-event device
|
| 107 | *
|
| 108 | * Note that this function check whether devfreq-event dev is enabled or not.
|
| 109 | * If return true, the devfreq-event dev is enabeld. If return false, the
|
| 110 | * devfreq-event dev is disabled.
|
| 111 |
|
| 112 | bool devfreq_event_is_enabled(struct devfreq_event_dev *edev)
|
| 113 | {
|
| 114 | bool enabled = false;
|
| 115 |
|
| 116 | if (!edev || !edev->desc)
|
| 117 | return enabled;
|
| 118 |
|
| 119 | mutex_lock(&edev->lock);
|
| 120 |
|
| 121 | if (edev->enable_count > 0)
|
| 122 | enabled = true;
|
| 123 |
|
| 124 | mutex_unlock(&edev->lock);
|
| 125 |
|
| 126 | return enabled;
|
| 127 | }
|
| 128 | EXPORT_SYMBOL_GPL(devfreq_event_is_enabled);
|
| 129 |
|
| 130 | |
| 131 | * devfreq_event_set_event() - Set event to devfreq-event dev to start.
|
| 132 | * @edev : the devfreq-event device
|
| 133 | *
|
| 134 | * Note that this function set the event to the devfreq-event device to start
|
| 135 | * for getting the event data which could be various event type.
|
| 136 |
|
| 137 | int devfreq_event_set_event(struct devfreq_event_dev *edev)
|
| 138 | {
|
| 139 | int ret;
|
| 140 |
|
| 141 | if (!edev || !edev->desc)
|
| 142 | return -EINVAL;
|
| 143 |
|
| 144 | if (!edev->desc->ops || !edev->desc->ops->set_event)
|
| 145 | return -EINVAL;
|
| 146 |
|
| 147 | if (!devfreq_event_is_enabled(edev))
|
| 148 | return -EPERM;
|
| 149 |
|
| 150 | mutex_lock(&edev->lock);
|
| 151 | ret = edev->desc->ops->set_event(edev);
|
| 152 | mutex_unlock(&edev->lock);
|
| 153 |
|
| 154 | return ret;
|
| 155 | }
|
| 156 | EXPORT_SYMBOL_GPL(devfreq_event_set_event);
|
| 157 |
|
| 158 | |
| 159 | * devfreq_event_get_event() - Get {load|total}_count from devfreq-event dev.
|
| 160 | * @edev : the devfreq-event device
|
| 161 | * @edata : the calculated data of devfreq-event device
|
| 162 | *
|
| 163 | * Note that this function get the calculated event data from devfreq-event dev
|
| 164 | * after stoping the progress of whole sequence of devfreq-event dev.
|
| 165 |
|
| 166 | int devfreq_event_get_event(struct devfreq_event_dev *edev,
|
| 167 | struct devfreq_event_data *edata)
|
| 168 | {
|
| 169 | int ret;
|
| 170 |
|
| 171 | if (!edev || !edev->desc)
|
| 172 | return -EINVAL;
|
| 173 |
|
| 174 | if (!edev->desc->ops || !edev->desc->ops->get_event)
|
| 175 | return -EINVAL;
|
| 176 |
|
| 177 | if (!devfreq_event_is_enabled(edev))
|
| 178 | return -EINVAL;
|
| 179 |
|
| 180 | edata->total_count = edata->load_count = 0;
|
| 181 |
|
| 182 | mutex_lock(&edev->lock);
|
| 183 | ret = edev->desc->ops->get_event(edev, edata);
|
| 184 | if (ret < 0)
|
| 185 | edata->total_count = edata->load_count = 0;
|
| 186 | mutex_unlock(&edev->lock);
|
| 187 |
|
| 188 | return ret;
|
| 189 | }
|
| 190 | EXPORT_SYMBOL_GPL(devfreq_event_get_event);
|
| 191 |
|
| 192 | |
| 193 | * devfreq_event_reset_event() - Reset all opeations of devfreq-event dev.
|
| 194 | * @edev : the devfreq-event device
|
| 195 | *
|
| 196 | * Note that this function stop all operations of devfreq-event dev and reset
|
| 197 | * the current event data to make the devfreq-event device into initial state.
|
| 198 |
|
| 199 | int devfreq_event_reset_event(struct devfreq_event_dev *edev)
|
| 200 | {
|
| 201 | int ret = 0;
|
| 202 |
|
| 203 | if (!edev || !edev->desc)
|
| 204 | return -EINVAL;
|
| 205 |
|
| 206 | if (!devfreq_event_is_enabled(edev))
|
| 207 | return -EPERM;
|
| 208 |
|
| 209 | mutex_lock(&edev->lock);
|
| 210 | if (edev->desc->ops && edev->desc->ops->reset)
|
| 211 | ret = edev->desc->ops->reset(edev);
|
| 212 | mutex_unlock(&edev->lock);
|
| 213 |
|
| 214 | return ret;
|
| 215 | }
|
| 216 | EXPORT_SYMBOL_GPL(devfreq_event_reset_event);
|
| 217 |
|
| 218 | |
| 219 | * devfreq_event_get_edev_by_phandle() - Get the devfreq-event dev from
|
| 220 | * devicetree.
|
| 221 | * @dev : the pointer to the given device
|
| 222 | * @phandle_name: name of property holding a phandle value
|
| 223 | * @index : the index into list of devfreq-event device
|
| 224 | *
|
| 225 | * Note that this function return the pointer of devfreq-event device.
|
| 226 |
|
| 227 | struct devfreq_event_dev *devfreq_event_get_edev_by_phandle(struct device *dev,
|
| 228 | const char *phandle_name, int index)
|
| 229 | {
|
| 230 | struct device_node *node;
|
| 231 | struct devfreq_event_dev *edev;
|
| 232 |
|
| 233 | if (!dev->of_node || !phandle_name)
|
| 234 | return ERR_PTR(-EINVAL);
|
| 235 |
|
| 236 | node = of_parse_phandle(dev->of_node, phandle_name, index);
|
| 237 | if (!node)
|
| 238 | return ERR_PTR(-ENODEV);
|
| 239 |
|
| 240 | mutex_lock(&devfreq_event_list_lock);
|
| 241 | list_for_each_entry(edev, &devfreq_event_list, node) {
|
| 242 | if (edev->dev.parent && device_match_of_node(edev->dev.parent, node))
|
| 243 | goto out;
|
| 244 | }
|
| 245 |
|
| 246 | list_for_each_entry(edev, &devfreq_event_list, node) {
|
| 247 | if (of_node_name_eq(node, edev->desc->name))
|
| 248 | goto out;
|
| 249 | }
|
| 250 | edev = NULL;
|
| 251 | out:
|
| 252 | mutex_unlock(&devfreq_event_list_lock);
|
| 253 | of_node_put(node);
|
| 254 | if (!edev)
|
| 255 | return ERR_PTR(-ENODEV);
|
| 256 |
|
| 257 | return edev;
|
| 258 | }
|
| 259 | EXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle);
|
| 260 |
|
| 261 | |
| 262 | * devfreq_event_get_edev_count() - Get the count of devfreq-event dev
|
| 263 | * @dev : the pointer to the given device
|
| 264 | * @phandle_name: name of property holding a phandle value
|
| 265 | *
|
| 266 | * Note that this function return the count of devfreq-event devices.
|
| 267 |
|
| 268 | int devfreq_event_get_edev_count(struct device *dev, const char *phandle_name)
|
| 269 | {
|
| 270 | int count;
|
| 271 |
|
| 272 | if (!dev->of_node || !phandle_name) {
|
| 273 | dev_err(dev, "device does not have a device node entry\n");
|
| 274 | return -EINVAL;
|
| 275 | }
|
| 276 |
|
| 277 | count = of_property_count_elems_of_size(dev->of_node, phandle_name,
|
| 278 | sizeof(u32));
|
| 279 | if (count < 0) {
|
| 280 | dev_err(dev,
|
| 281 | "failed to get the count of devfreq-event in %pOF node\n",
|
| 282 | dev->of_node);
|
| 283 | return count;
|
| 284 | }
|
| 285 |
|
| 286 | return count;
|
| 287 | }
|
| 288 | EXPORT_SYMBOL_GPL(devfreq_event_get_edev_count);
|
| 289 |
|
| 290 | static void devfreq_event_release_edev(struct device *dev)
|
| 291 | {
|
| 292 | struct devfreq_event_dev *edev = to_devfreq_event(dev);
|
| 293 |
|
| 294 | kfree(edev);
|
| 295 | }
|
| 296 |
|
| 297 | |
| 298 | * devfreq_event_add_edev() - Add new devfreq-event device.
|
| 299 | * @dev : the device owning the devfreq-event device being created
|
| 300 | * @desc : the devfreq-event device's descriptor which include essential
|
| 301 | * data for devfreq-event device.
|
| 302 | *
|
| 303 | * Note that this function add new devfreq-event device to devfreq-event class
|
| 304 | * list and register the device of the devfreq-event device.
|
| 305 |
|
| 306 | struct devfreq_event_dev *devfreq_event_add_edev(struct device *dev,
|
| 307 | struct devfreq_event_desc *desc)
|
| 308 | {
|
| 309 | struct devfreq_event_dev *edev;
|
| 310 | static atomic_t event_no = ATOMIC_INIT(-1);
|
| 311 | int ret;
|
| 312 |
|
| 313 | if (!dev || !desc)
|
| 314 | return ERR_PTR(-EINVAL);
|
| 315 |
|
| 316 | if (!desc->name || !desc->ops)
|
| 317 | return ERR_PTR(-EINVAL);
|
| 318 |
|
| 319 | if (!desc->ops->set_event || !desc->ops->get_event)
|
| 320 | return ERR_PTR(-EINVAL);
|
| 321 |
|
| 322 | edev = kzalloc_obj(struct devfreq_event_dev);
|
| 323 | if (!edev)
|
| 324 | return ERR_PTR(-ENOMEM);
|
| 325 |
|
| 326 | mutex_init(&edev->lock);
|
| 327 | edev->desc = desc;
|
| 328 | edev->enable_count = 0;
|
| 329 | edev->dev.parent = dev;
|
| 330 | edev->dev.class = &devfreq_event_class;
|
| 331 | edev->dev.release = devfreq_event_release_edev;
|
| 332 |
|
| 333 | dev_set_name(&edev->dev, "event%d", atomic_inc_return(&event_no));
|
| 334 | ret = device_register(&edev->dev);
|
| 335 | if (ret < 0) {
|
| 336 | put_device(&edev->dev);
|
| 337 | return ERR_PTR(ret);
|
| 338 | }
|
| 339 | dev_set_drvdata(&edev->dev, edev);
|
| 340 |
|
| 341 | INIT_LIST_HEAD(&edev->node);
|
| 342 |
|
| 343 | mutex_lock(&devfreq_event_list_lock);
|
| 344 | list_add(&edev->node, &devfreq_event_list);
|
| 345 | mutex_unlock(&devfreq_event_list_lock);
|
| 346 |
|
| 347 | return edev;
|
| 348 | }
|
| 349 | EXPORT_SYMBOL_GPL(devfreq_event_add_edev);
|
| 350 |
|
| 351 | |
| 352 | * devfreq_event_remove_edev() - Remove the devfreq-event device registered.
|
| 353 | * @edev : the devfreq-event device
|
| 354 | *
|
| 355 | * Note that this function removes the registered devfreq-event device.
|
| 356 |
|
| 357 | int devfreq_event_remove_edev(struct devfreq_event_dev *edev)
|
| 358 | {
|
| 359 | if (!edev)
|
| 360 | return -EINVAL;
|
| 361 |
|
| 362 | WARN_ON(edev->enable_count);
|
| 363 |
|
| 364 | mutex_lock(&devfreq_event_list_lock);
|
| 365 | list_del(&edev->node);
|
| 366 | mutex_unlock(&devfreq_event_list_lock);
|
| 367 |
|
| 368 | device_unregister(&edev->dev);
|
| 369 |
|
| 370 | return 0;
|
| 371 | }
|
| 372 | EXPORT_SYMBOL_GPL(devfreq_event_remove_edev);
|
| 373 |
|
| 374 | static int devm_devfreq_event_match(struct device *dev, void *res, void *data)
|
| 375 | {
|
| 376 | struct devfreq_event_dev **r = res;
|
| 377 |
|
| 378 | if (WARN_ON(!r || !*r))
|
| 379 | return 0;
|
| 380 |
|
| 381 | return *r == data;
|
| 382 | }
|
| 383 |
|
| 384 | static void devm_devfreq_event_release(struct device *dev, void *res)
|
| 385 | {
|
| 386 | devfreq_event_remove_edev(*(struct devfreq_event_dev **)res);
|
| 387 | }
|
| 388 |
|
| 389 | |
| 390 | * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev()
|
| 391 | * @dev : the device owning the devfreq-event device being created
|
| 392 | * @desc : the devfreq-event device's descriptor which include essential
|
| 393 | * data for devfreq-event device.
|
| 394 | *
|
| 395 | * Note that this function manages automatically the memory of devfreq-event
|
| 396 | * device using device resource management and simplify the free operation
|
| 397 | * for memory of devfreq-event device.
|
| 398 |
|
| 399 | struct devfreq_event_dev *devm_devfreq_event_add_edev(struct device *dev,
|
| 400 | struct devfreq_event_desc *desc)
|
| 401 | {
|
| 402 | struct devfreq_event_dev **ptr, *edev;
|
| 403 |
|
| 404 | ptr = devres_alloc(devm_devfreq_event_release, sizeof(*ptr),
|
| 405 | GFP_KERNEL);
|
| 406 | if (!ptr)
|
| 407 | return ERR_PTR(-ENOMEM);
|
| 408 |
|
| 409 | edev = devfreq_event_add_edev(dev, desc);
|
| 410 | if (IS_ERR(edev)) {
|
| 411 | devres_free(ptr);
|
| 412 | return ERR_PTR(-ENOMEM);
|
| 413 | }
|
| 414 |
|
| 415 | *ptr = edev;
|
| 416 | devres_add(dev, ptr);
|
| 417 |
|
| 418 | return edev;
|
| 419 | }
|
| 420 | EXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev);
|
| 421 |
|
| 422 | |
| 423 | * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev()
|
| 424 | * @dev : the device owning the devfreq-event device being created
|
| 425 | * @edev : the devfreq-event device
|
| 426 | *
|
| 427 | * Note that this function manages automatically the memory of devfreq-event
|
| 428 | * device using device resource management.
|
| 429 |
|
| 430 | void devm_devfreq_event_remove_edev(struct device *dev,
|
| 431 | struct devfreq_event_dev *edev)
|
| 432 | {
|
| 433 | WARN_ON(devres_release(dev, devm_devfreq_event_release,
|
| 434 | devm_devfreq_event_match, edev));
|
| 435 | }
|
| 436 | EXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev);
|
| 437 |
|
| 438 | |
| 439 | * Device attributes for devfreq-event class.
|
| 440 |
|
| 441 | static ssize_t name_show(struct device *dev, struct device_attribute *attr,
|
| 442 | char *buf)
|
| 443 | {
|
| 444 | struct devfreq_event_dev *edev = to_devfreq_event(dev);
|
| 445 |
|
| 446 | if (!edev || !edev->desc)
|
| 447 | return -EINVAL;
|
| 448 |
|
| 449 | return sprintf(buf, "%s\n", edev->desc->name);
|
| 450 | }
|
| 451 | static DEVICE_ATTR_RO(name);
|
| 452 |
|
| 453 | static ssize_t enable_count_show(struct device *dev,
|
| 454 | struct device_attribute *attr, char *buf)
|
| 455 | {
|
| 456 | struct devfreq_event_dev *edev = to_devfreq_event(dev);
|
| 457 |
|
| 458 | if (!edev || !edev->desc)
|
| 459 | return -EINVAL;
|
| 460 |
|
| 461 | return sprintf(buf, "%d\n", edev->enable_count);
|
| 462 | }
|
| 463 | static DEVICE_ATTR_RO(enable_count);
|
| 464 |
|
| 465 | static struct attribute *devfreq_event_attrs[] = {
|
| 466 | &dev_attr_name.attr,
|
| 467 | &dev_attr_enable_count.attr,
|
| 468 | NULL,
|
| 469 | };
|
| 470 |
|
| 471 | static int __init devfreq_event_init(void)
|
| 472 | {
|
| 473 | int err;
|
| 474 |
|
| 475 | err = class_register(&devfreq_event_class);
|
| 476 | if (err)
|
| 477 | pr_err("%s: couldn't create class\n", __FILE__);
|
| 478 |
|
| 479 | return err;
|
| 480 | }
|
| 481 | subsys_initcall(devfreq_event_init);
|