devfreq-event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * devfreq-event: a framework to provide raw data and events of devfreq devices
  3. *
  4. * Copyright (C) 2015 Samsung Electronics
  5. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This driver is based on drivers/devfreq/devfreq.c.
  12. */
  13. #include <linux/devfreq-event.h>
  14. #include <linux/kernel.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/export.h>
  18. #include <linux/slab.h>
  19. #include <linux/list.h>
  20. #include <linux/of.h>
  21. static struct class *devfreq_event_class;
  22. /* The list of all devfreq event list */
  23. static LIST_HEAD(devfreq_event_list);
  24. static DEFINE_MUTEX(devfreq_event_list_lock);
  25. #define to_devfreq_event(DEV) container_of(DEV, struct devfreq_event_dev, dev)
  26. /**
  27. * devfreq_event_enable_edev() - Enable the devfreq-event dev and increase
  28. * the enable_count of devfreq-event dev.
  29. * @edev : the devfreq-event device
  30. *
  31. * Note that this function increase the enable_count and enable the
  32. * devfreq-event device. The devfreq-event device should be enabled before
  33. * using it by devfreq device.
  34. */
  35. int devfreq_event_enable_edev(struct devfreq_event_dev *edev)
  36. {
  37. int ret = 0;
  38. if (!edev || !edev->desc)
  39. return -EINVAL;
  40. mutex_lock(&edev->lock);
  41. if (edev->desc->ops && edev->desc->ops->enable
  42. && edev->enable_count == 0) {
  43. ret = edev->desc->ops->enable(edev);
  44. if (ret < 0)
  45. goto err;
  46. }
  47. edev->enable_count++;
  48. err:
  49. mutex_unlock(&edev->lock);
  50. return ret;
  51. }
  52. EXPORT_SYMBOL_GPL(devfreq_event_enable_edev);
  53. /**
  54. * devfreq_event_disable_edev() - Disable the devfreq-event dev and decrease
  55. * the enable_count of the devfreq-event dev.
  56. * @edev : the devfreq-event device
  57. *
  58. * Note that this function decrease the enable_count and disable the
  59. * devfreq-event device. After the devfreq-event device is disabled,
  60. * devfreq device can't use the devfreq-event device for get/set/reset
  61. * operations.
  62. */
  63. int devfreq_event_disable_edev(struct devfreq_event_dev *edev)
  64. {
  65. int ret = 0;
  66. if (!edev || !edev->desc)
  67. return -EINVAL;
  68. mutex_lock(&edev->lock);
  69. if (edev->enable_count <= 0) {
  70. dev_warn(&edev->dev, "unbalanced enable_count\n");
  71. ret = -EIO;
  72. goto err;
  73. }
  74. if (edev->desc->ops && edev->desc->ops->disable
  75. && edev->enable_count == 1) {
  76. ret = edev->desc->ops->disable(edev);
  77. if (ret < 0)
  78. goto err;
  79. }
  80. edev->enable_count--;
  81. err:
  82. mutex_unlock(&edev->lock);
  83. return ret;
  84. }
  85. EXPORT_SYMBOL_GPL(devfreq_event_disable_edev);
  86. /**
  87. * devfreq_event_is_enabled() - Check whether devfreq-event dev is enabled or
  88. * not.
  89. * @edev : the devfreq-event device
  90. *
  91. * Note that this function check whether devfreq-event dev is enabled or not.
  92. * If return true, the devfreq-event dev is enabeld. If return false, the
  93. * devfreq-event dev is disabled.
  94. */
  95. bool devfreq_event_is_enabled(struct devfreq_event_dev *edev)
  96. {
  97. bool enabled = false;
  98. if (!edev || !edev->desc)
  99. return enabled;
  100. mutex_lock(&edev->lock);
  101. if (edev->enable_count > 0)
  102. enabled = true;
  103. mutex_unlock(&edev->lock);
  104. return enabled;
  105. }
  106. EXPORT_SYMBOL_GPL(devfreq_event_is_enabled);
  107. /**
  108. * devfreq_event_set_event() - Set event to devfreq-event dev to start.
  109. * @edev : the devfreq-event device
  110. *
  111. * Note that this function set the event to the devfreq-event device to start
  112. * for getting the event data which could be various event type.
  113. */
  114. int devfreq_event_set_event(struct devfreq_event_dev *edev)
  115. {
  116. int ret;
  117. if (!edev || !edev->desc)
  118. return -EINVAL;
  119. if (!edev->desc->ops || !edev->desc->ops->set_event)
  120. return -EINVAL;
  121. if (!devfreq_event_is_enabled(edev))
  122. return -EPERM;
  123. mutex_lock(&edev->lock);
  124. ret = edev->desc->ops->set_event(edev);
  125. mutex_unlock(&edev->lock);
  126. return ret;
  127. }
  128. EXPORT_SYMBOL_GPL(devfreq_event_set_event);
  129. /**
  130. * devfreq_event_get_event() - Get {load|total}_count from devfreq-event dev.
  131. * @edev : the devfreq-event device
  132. * @edata : the calculated data of devfreq-event device
  133. *
  134. * Note that this function get the calculated event data from devfreq-event dev
  135. * after stoping the progress of whole sequence of devfreq-event dev.
  136. */
  137. int devfreq_event_get_event(struct devfreq_event_dev *edev,
  138. struct devfreq_event_data *edata)
  139. {
  140. int ret;
  141. if (!edev || !edev->desc)
  142. return -EINVAL;
  143. if (!edev->desc->ops || !edev->desc->ops->get_event)
  144. return -EINVAL;
  145. if (!devfreq_event_is_enabled(edev))
  146. return -EINVAL;
  147. edata->total_count = edata->load_count = 0;
  148. mutex_lock(&edev->lock);
  149. ret = edev->desc->ops->get_event(edev, edata);
  150. if (ret < 0)
  151. edata->total_count = edata->load_count = 0;
  152. mutex_unlock(&edev->lock);
  153. return ret;
  154. }
  155. EXPORT_SYMBOL_GPL(devfreq_event_get_event);
  156. /**
  157. * devfreq_event_reset_event() - Reset all opeations of devfreq-event dev.
  158. * @edev : the devfreq-event device
  159. *
  160. * Note that this function stop all operations of devfreq-event dev and reset
  161. * the current event data to make the devfreq-event device into initial state.
  162. */
  163. int devfreq_event_reset_event(struct devfreq_event_dev *edev)
  164. {
  165. int ret = 0;
  166. if (!edev || !edev->desc)
  167. return -EINVAL;
  168. if (!devfreq_event_is_enabled(edev))
  169. return -EPERM;
  170. mutex_lock(&edev->lock);
  171. if (edev->desc->ops && edev->desc->ops->reset)
  172. ret = edev->desc->ops->reset(edev);
  173. mutex_unlock(&edev->lock);
  174. return ret;
  175. }
  176. EXPORT_SYMBOL_GPL(devfreq_event_reset_event);
  177. /**
  178. * devfreq_event_get_edev_by_phandle() - Get the devfreq-event dev from
  179. * devicetree.
  180. * @dev : the pointer to the given device
  181. * @index : the index into list of devfreq-event device
  182. *
  183. * Note that this function return the pointer of devfreq-event device.
  184. */
  185. struct devfreq_event_dev *devfreq_event_get_edev_by_phandle(struct device *dev,
  186. int index)
  187. {
  188. struct device_node *node;
  189. struct devfreq_event_dev *edev;
  190. if (!dev->of_node)
  191. return ERR_PTR(-EINVAL);
  192. node = of_parse_phandle(dev->of_node, "devfreq-events", index);
  193. if (!node)
  194. return ERR_PTR(-ENODEV);
  195. mutex_lock(&devfreq_event_list_lock);
  196. list_for_each_entry(edev, &devfreq_event_list, node) {
  197. if (edev->dev.parent && edev->dev.parent->of_node == node)
  198. goto out;
  199. }
  200. list_for_each_entry(edev, &devfreq_event_list, node) {
  201. if (!strcmp(edev->desc->name, node->name))
  202. goto out;
  203. }
  204. edev = NULL;
  205. out:
  206. mutex_unlock(&devfreq_event_list_lock);
  207. if (!edev) {
  208. of_node_put(node);
  209. return ERR_PTR(-ENODEV);
  210. }
  211. of_node_put(node);
  212. return edev;
  213. }
  214. EXPORT_SYMBOL_GPL(devfreq_event_get_edev_by_phandle);
  215. /**
  216. * devfreq_event_get_edev_count() - Get the count of devfreq-event dev
  217. * @dev : the pointer to the given device
  218. *
  219. * Note that this function return the count of devfreq-event devices.
  220. */
  221. int devfreq_event_get_edev_count(struct device *dev)
  222. {
  223. int count;
  224. if (!dev->of_node) {
  225. dev_err(dev, "device does not have a device node entry\n");
  226. return -EINVAL;
  227. }
  228. count = of_property_count_elems_of_size(dev->of_node, "devfreq-events",
  229. sizeof(u32));
  230. if (count < 0) {
  231. dev_err(dev,
  232. "failed to get the count of devfreq-event in %s node\n",
  233. dev->of_node->full_name);
  234. return count;
  235. }
  236. return count;
  237. }
  238. EXPORT_SYMBOL_GPL(devfreq_event_get_edev_count);
  239. static void devfreq_event_release_edev(struct device *dev)
  240. {
  241. struct devfreq_event_dev *edev = to_devfreq_event(dev);
  242. kfree(edev);
  243. }
  244. /**
  245. * devfreq_event_add_edev() - Add new devfreq-event device.
  246. * @dev : the device owning the devfreq-event device being created
  247. * @desc : the devfreq-event device's decriptor which include essential
  248. * data for devfreq-event device.
  249. *
  250. * Note that this function add new devfreq-event device to devfreq-event class
  251. * list and register the device of the devfreq-event device.
  252. */
  253. struct devfreq_event_dev *devfreq_event_add_edev(struct device *dev,
  254. struct devfreq_event_desc *desc)
  255. {
  256. struct devfreq_event_dev *edev;
  257. static atomic_t event_no = ATOMIC_INIT(0);
  258. int ret;
  259. if (!dev || !desc)
  260. return ERR_PTR(-EINVAL);
  261. if (!desc->name || !desc->ops)
  262. return ERR_PTR(-EINVAL);
  263. if (!desc->ops->set_event || !desc->ops->get_event)
  264. return ERR_PTR(-EINVAL);
  265. edev = kzalloc(sizeof(struct devfreq_event_dev), GFP_KERNEL);
  266. if (!edev)
  267. return ERR_PTR(-ENOMEM);
  268. mutex_init(&edev->lock);
  269. edev->desc = desc;
  270. edev->enable_count = 0;
  271. edev->dev.parent = dev;
  272. edev->dev.class = devfreq_event_class;
  273. edev->dev.release = devfreq_event_release_edev;
  274. dev_set_name(&edev->dev, "event.%d", atomic_inc_return(&event_no) - 1);
  275. ret = device_register(&edev->dev);
  276. if (ret < 0) {
  277. put_device(&edev->dev);
  278. return ERR_PTR(ret);
  279. }
  280. dev_set_drvdata(&edev->dev, edev);
  281. INIT_LIST_HEAD(&edev->node);
  282. mutex_lock(&devfreq_event_list_lock);
  283. list_add(&edev->node, &devfreq_event_list);
  284. mutex_unlock(&devfreq_event_list_lock);
  285. return edev;
  286. }
  287. EXPORT_SYMBOL_GPL(devfreq_event_add_edev);
  288. /**
  289. * devfreq_event_remove_edev() - Remove the devfreq-event device registered.
  290. * @dev : the devfreq-event device
  291. *
  292. * Note that this function remove the registered devfreq-event device.
  293. */
  294. int devfreq_event_remove_edev(struct devfreq_event_dev *edev)
  295. {
  296. if (!edev)
  297. return -EINVAL;
  298. WARN_ON(edev->enable_count);
  299. mutex_lock(&devfreq_event_list_lock);
  300. list_del(&edev->node);
  301. mutex_unlock(&devfreq_event_list_lock);
  302. device_unregister(&edev->dev);
  303. return 0;
  304. }
  305. EXPORT_SYMBOL_GPL(devfreq_event_remove_edev);
  306. static int devm_devfreq_event_match(struct device *dev, void *res, void *data)
  307. {
  308. struct devfreq_event_dev **r = res;
  309. if (WARN_ON(!r || !*r))
  310. return 0;
  311. return *r == data;
  312. }
  313. static void devm_devfreq_event_release(struct device *dev, void *res)
  314. {
  315. devfreq_event_remove_edev(*(struct devfreq_event_dev **)res);
  316. }
  317. /**
  318. * devm_devfreq_event_add_edev() - Resource-managed devfreq_event_add_edev()
  319. * @dev : the device owning the devfreq-event device being created
  320. * @desc : the devfreq-event device's decriptor which include essential
  321. * data for devfreq-event device.
  322. *
  323. * Note that this function manages automatically the memory of devfreq-event
  324. * device using device resource management and simplify the free operation
  325. * for memory of devfreq-event device.
  326. */
  327. struct devfreq_event_dev *devm_devfreq_event_add_edev(struct device *dev,
  328. struct devfreq_event_desc *desc)
  329. {
  330. struct devfreq_event_dev **ptr, *edev;
  331. ptr = devres_alloc(devm_devfreq_event_release, sizeof(*ptr),
  332. GFP_KERNEL);
  333. if (!ptr)
  334. return ERR_PTR(-ENOMEM);
  335. edev = devfreq_event_add_edev(dev, desc);
  336. if (IS_ERR(edev)) {
  337. devres_free(ptr);
  338. return ERR_PTR(-ENOMEM);
  339. }
  340. *ptr = edev;
  341. devres_add(dev, ptr);
  342. return edev;
  343. }
  344. EXPORT_SYMBOL_GPL(devm_devfreq_event_add_edev);
  345. /**
  346. * devm_devfreq_event_remove_edev()- Resource-managed devfreq_event_remove_edev()
  347. * @dev : the device owning the devfreq-event device being created
  348. * @edev : the devfreq-event device
  349. *
  350. * Note that this function manages automatically the memory of devfreq-event
  351. * device using device resource management.
  352. */
  353. void devm_devfreq_event_remove_edev(struct device *dev,
  354. struct devfreq_event_dev *edev)
  355. {
  356. WARN_ON(devres_release(dev, devm_devfreq_event_release,
  357. devm_devfreq_event_match, edev));
  358. }
  359. EXPORT_SYMBOL_GPL(devm_devfreq_event_remove_edev);
  360. /*
  361. * Device attributes for devfreq-event class.
  362. */
  363. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  364. char *buf)
  365. {
  366. struct devfreq_event_dev *edev = to_devfreq_event(dev);
  367. if (!edev || !edev->desc)
  368. return -EINVAL;
  369. return sprintf(buf, "%s\n", edev->desc->name);
  370. }
  371. static DEVICE_ATTR_RO(name);
  372. static ssize_t enable_count_show(struct device *dev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. struct devfreq_event_dev *edev = to_devfreq_event(dev);
  376. if (!edev || !edev->desc)
  377. return -EINVAL;
  378. return sprintf(buf, "%d\n", edev->enable_count);
  379. }
  380. static DEVICE_ATTR_RO(enable_count);
  381. static struct attribute *devfreq_event_attrs[] = {
  382. &dev_attr_name.attr,
  383. &dev_attr_enable_count.attr,
  384. NULL,
  385. };
  386. ATTRIBUTE_GROUPS(devfreq_event);
  387. static int __init devfreq_event_init(void)
  388. {
  389. devfreq_event_class = class_create(THIS_MODULE, "devfreq-event");
  390. if (IS_ERR(devfreq_event_class)) {
  391. pr_err("%s: couldn't create class\n", __FILE__);
  392. return PTR_ERR(devfreq_event_class);
  393. }
  394. devfreq_event_class->dev_groups = devfreq_event_groups;
  395. return 0;
  396. }
  397. subsys_initcall(devfreq_event_init);