debugfs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Generic OPP debugfs interface
  3. *
  4. * Copyright (C) 2015-2016 Viresh Kumar <viresh.kumar@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/debugfs.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/limits.h>
  16. #include <linux/slab.h>
  17. #include "opp.h"
  18. static struct dentry *rootdir;
  19. static void opp_set_dev_name(const struct device *dev, char *name)
  20. {
  21. if (dev->parent)
  22. snprintf(name, NAME_MAX, "%s-%s", dev_name(dev->parent),
  23. dev_name(dev));
  24. else
  25. snprintf(name, NAME_MAX, "%s", dev_name(dev));
  26. }
  27. void opp_debug_remove_one(struct dev_pm_opp *opp)
  28. {
  29. debugfs_remove_recursive(opp->dentry);
  30. }
  31. static bool opp_debug_create_supplies(struct dev_pm_opp *opp,
  32. struct opp_table *opp_table,
  33. struct dentry *pdentry)
  34. {
  35. struct dentry *d;
  36. int i = 0;
  37. char *name;
  38. /* Always create at least supply-0 directory */
  39. do {
  40. name = kasprintf(GFP_KERNEL, "supply-%d", i);
  41. /* Create per-opp directory */
  42. d = debugfs_create_dir(name, pdentry);
  43. kfree(name);
  44. if (!d)
  45. return false;
  46. if (!debugfs_create_ulong("u_volt_target", S_IRUGO, d,
  47. &opp->supplies[i].u_volt))
  48. return false;
  49. if (!debugfs_create_ulong("u_volt_min", S_IRUGO, d,
  50. &opp->supplies[i].u_volt_min))
  51. return false;
  52. if (!debugfs_create_ulong("u_volt_max", S_IRUGO, d,
  53. &opp->supplies[i].u_volt_max))
  54. return false;
  55. if (!debugfs_create_ulong("u_amp", S_IRUGO, d,
  56. &opp->supplies[i].u_amp))
  57. return false;
  58. } while (++i < opp_table->regulator_count);
  59. return true;
  60. }
  61. int opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table)
  62. {
  63. struct dentry *pdentry = opp_table->dentry;
  64. struct dentry *d;
  65. char name[25]; /* 20 chars for 64 bit value + 5 (opp:\0) */
  66. /* Rate is unique to each OPP, use it to give opp-name */
  67. snprintf(name, sizeof(name), "opp:%lu", opp->rate);
  68. /* Create per-opp directory */
  69. d = debugfs_create_dir(name, pdentry);
  70. if (!d)
  71. return -ENOMEM;
  72. if (!debugfs_create_bool("available", S_IRUGO, d, &opp->available))
  73. return -ENOMEM;
  74. if (!debugfs_create_bool("dynamic", S_IRUGO, d, &opp->dynamic))
  75. return -ENOMEM;
  76. if (!debugfs_create_bool("turbo", S_IRUGO, d, &opp->turbo))
  77. return -ENOMEM;
  78. if (!debugfs_create_bool("suspend", S_IRUGO, d, &opp->suspend))
  79. return -ENOMEM;
  80. if (!debugfs_create_ulong("rate_hz", S_IRUGO, d, &opp->rate))
  81. return -ENOMEM;
  82. if (!opp_debug_create_supplies(opp, opp_table, d))
  83. return -ENOMEM;
  84. if (!debugfs_create_ulong("clock_latency_ns", S_IRUGO, d,
  85. &opp->clock_latency_ns))
  86. return -ENOMEM;
  87. opp->dentry = d;
  88. return 0;
  89. }
  90. static int opp_list_debug_create_dir(struct opp_device *opp_dev,
  91. struct opp_table *opp_table)
  92. {
  93. const struct device *dev = opp_dev->dev;
  94. struct dentry *d;
  95. opp_set_dev_name(dev, opp_table->dentry_name);
  96. /* Create device specific directory */
  97. d = debugfs_create_dir(opp_table->dentry_name, rootdir);
  98. if (!d) {
  99. dev_err(dev, "%s: Failed to create debugfs dir\n", __func__);
  100. return -ENOMEM;
  101. }
  102. opp_dev->dentry = d;
  103. opp_table->dentry = d;
  104. return 0;
  105. }
  106. static int opp_list_debug_create_link(struct opp_device *opp_dev,
  107. struct opp_table *opp_table)
  108. {
  109. const struct device *dev = opp_dev->dev;
  110. char name[NAME_MAX];
  111. struct dentry *d;
  112. opp_set_dev_name(opp_dev->dev, name);
  113. /* Create device specific directory link */
  114. d = debugfs_create_symlink(name, rootdir, opp_table->dentry_name);
  115. if (!d) {
  116. dev_err(dev, "%s: Failed to create link\n", __func__);
  117. return -ENOMEM;
  118. }
  119. opp_dev->dentry = d;
  120. return 0;
  121. }
  122. /**
  123. * opp_debug_register - add a device opp node to the debugfs 'opp' directory
  124. * @opp_dev: opp-dev pointer for device
  125. * @opp_table: the device-opp being added
  126. *
  127. * Dynamically adds device specific directory in debugfs 'opp' directory. If the
  128. * device-opp is shared with other devices, then links will be created for all
  129. * devices except the first.
  130. *
  131. * Return: 0 on success, otherwise negative error.
  132. */
  133. int opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
  134. {
  135. if (!rootdir) {
  136. pr_debug("%s: Uninitialized rootdir\n", __func__);
  137. return -EINVAL;
  138. }
  139. if (opp_table->dentry)
  140. return opp_list_debug_create_link(opp_dev, opp_table);
  141. return opp_list_debug_create_dir(opp_dev, opp_table);
  142. }
  143. static void opp_migrate_dentry(struct opp_device *opp_dev,
  144. struct opp_table *opp_table)
  145. {
  146. struct opp_device *new_dev;
  147. const struct device *dev;
  148. struct dentry *dentry;
  149. /* Look for next opp-dev */
  150. list_for_each_entry(new_dev, &opp_table->dev_list, node)
  151. if (new_dev != opp_dev)
  152. break;
  153. /* new_dev is guaranteed to be valid here */
  154. dev = new_dev->dev;
  155. debugfs_remove_recursive(new_dev->dentry);
  156. opp_set_dev_name(dev, opp_table->dentry_name);
  157. dentry = debugfs_rename(rootdir, opp_dev->dentry, rootdir,
  158. opp_table->dentry_name);
  159. if (!dentry) {
  160. dev_err(dev, "%s: Failed to rename link from: %s to %s\n",
  161. __func__, dev_name(opp_dev->dev), dev_name(dev));
  162. return;
  163. }
  164. new_dev->dentry = dentry;
  165. opp_table->dentry = dentry;
  166. }
  167. /**
  168. * opp_debug_unregister - remove a device opp node from debugfs opp directory
  169. * @opp_dev: opp-dev pointer for device
  170. * @opp_table: the device-opp being removed
  171. *
  172. * Dynamically removes device specific directory from debugfs 'opp' directory.
  173. */
  174. void opp_debug_unregister(struct opp_device *opp_dev,
  175. struct opp_table *opp_table)
  176. {
  177. if (opp_dev->dentry == opp_table->dentry) {
  178. /* Move the real dentry object under another device */
  179. if (!list_is_singular(&opp_table->dev_list)) {
  180. opp_migrate_dentry(opp_dev, opp_table);
  181. goto out;
  182. }
  183. opp_table->dentry = NULL;
  184. }
  185. debugfs_remove_recursive(opp_dev->dentry);
  186. out:
  187. opp_dev->dentry = NULL;
  188. }
  189. static int __init opp_debug_init(void)
  190. {
  191. /* Create /sys/kernel/debug/opp directory */
  192. rootdir = debugfs_create_dir("opp", NULL);
  193. if (!rootdir) {
  194. pr_err("%s: Failed to create root directory\n", __func__);
  195. return -ENOMEM;
  196. }
  197. return 0;
  198. }
  199. core_initcall(opp_debug_init);