debugfs.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Generic OPP debugfs interface
  3. *
  4. * Copyright (C) 2015-2016 Viresh Kumar <[email protected]>
  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 "opp.h"
  17. static struct dentry *rootdir;
  18. static void opp_set_dev_name(const struct device *dev, char *name)
  19. {
  20. if (dev->parent)
  21. snprintf(name, NAME_MAX, "%s-%s", dev_name(dev->parent),
  22. dev_name(dev));
  23. else
  24. snprintf(name, NAME_MAX, "%s", dev_name(dev));
  25. }
  26. void opp_debug_remove_one(struct dev_pm_opp *opp)
  27. {
  28. debugfs_remove_recursive(opp->dentry);
  29. }
  30. int opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table)
  31. {
  32. struct dentry *pdentry = opp_table->dentry;
  33. struct dentry *d;
  34. char name[25]; /* 20 chars for 64 bit value + 5 (opp:\0) */
  35. /* Rate is unique to each OPP, use it to give opp-name */
  36. snprintf(name, sizeof(name), "opp:%lu", opp->rate);
  37. /* Create per-opp directory */
  38. d = debugfs_create_dir(name, pdentry);
  39. if (!d)
  40. return -ENOMEM;
  41. if (!debugfs_create_bool("available", S_IRUGO, d, &opp->available))
  42. return -ENOMEM;
  43. if (!debugfs_create_bool("dynamic", S_IRUGO, d, &opp->dynamic))
  44. return -ENOMEM;
  45. if (!debugfs_create_bool("turbo", S_IRUGO, d, &opp->turbo))
  46. return -ENOMEM;
  47. if (!debugfs_create_bool("suspend", S_IRUGO, d, &opp->suspend))
  48. return -ENOMEM;
  49. if (!debugfs_create_ulong("rate_hz", S_IRUGO, d, &opp->rate))
  50. return -ENOMEM;
  51. if (!debugfs_create_ulong("u_volt_target", S_IRUGO, d, &opp->u_volt))
  52. return -ENOMEM;
  53. if (!debugfs_create_ulong("u_volt_min", S_IRUGO, d, &opp->u_volt_min))
  54. return -ENOMEM;
  55. if (!debugfs_create_ulong("u_volt_max", S_IRUGO, d, &opp->u_volt_max))
  56. return -ENOMEM;
  57. if (!debugfs_create_ulong("u_amp", S_IRUGO, d, &opp->u_amp))
  58. return -ENOMEM;
  59. if (!debugfs_create_ulong("clock_latency_ns", S_IRUGO, d,
  60. &opp->clock_latency_ns))
  61. return -ENOMEM;
  62. opp->dentry = d;
  63. return 0;
  64. }
  65. static int opp_list_debug_create_dir(struct opp_device *opp_dev,
  66. struct opp_table *opp_table)
  67. {
  68. const struct device *dev = opp_dev->dev;
  69. struct dentry *d;
  70. opp_set_dev_name(dev, opp_table->dentry_name);
  71. /* Create device specific directory */
  72. d = debugfs_create_dir(opp_table->dentry_name, rootdir);
  73. if (!d) {
  74. dev_err(dev, "%s: Failed to create debugfs dir\n", __func__);
  75. return -ENOMEM;
  76. }
  77. opp_dev->dentry = d;
  78. opp_table->dentry = d;
  79. return 0;
  80. }
  81. static int opp_list_debug_create_link(struct opp_device *opp_dev,
  82. struct opp_table *opp_table)
  83. {
  84. const struct device *dev = opp_dev->dev;
  85. char name[NAME_MAX];
  86. struct dentry *d;
  87. opp_set_dev_name(opp_dev->dev, name);
  88. /* Create device specific directory link */
  89. d = debugfs_create_symlink(name, rootdir, opp_table->dentry_name);
  90. if (!d) {
  91. dev_err(dev, "%s: Failed to create link\n", __func__);
  92. return -ENOMEM;
  93. }
  94. opp_dev->dentry = d;
  95. return 0;
  96. }
  97. /**
  98. * opp_debug_register - add a device opp node to the debugfs 'opp' directory
  99. * @opp_dev: opp-dev pointer for device
  100. * @opp_table: the device-opp being added
  101. *
  102. * Dynamically adds device specific directory in debugfs 'opp' directory. If the
  103. * device-opp is shared with other devices, then links will be created for all
  104. * devices except the first.
  105. *
  106. * Return: 0 on success, otherwise negative error.
  107. */
  108. int opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
  109. {
  110. if (!rootdir) {
  111. pr_debug("%s: Uninitialized rootdir\n", __func__);
  112. return -EINVAL;
  113. }
  114. if (opp_table->dentry)
  115. return opp_list_debug_create_link(opp_dev, opp_table);
  116. return opp_list_debug_create_dir(opp_dev, opp_table);
  117. }
  118. static void opp_migrate_dentry(struct opp_device *opp_dev,
  119. struct opp_table *opp_table)
  120. {
  121. struct opp_device *new_dev;
  122. const struct device *dev;
  123. struct dentry *dentry;
  124. /* Look for next opp-dev */
  125. list_for_each_entry(new_dev, &opp_table->dev_list, node)
  126. if (new_dev != opp_dev)
  127. break;
  128. /* new_dev is guaranteed to be valid here */
  129. dev = new_dev->dev;
  130. debugfs_remove_recursive(new_dev->dentry);
  131. opp_set_dev_name(dev, opp_table->dentry_name);
  132. dentry = debugfs_rename(rootdir, opp_dev->dentry, rootdir,
  133. opp_table->dentry_name);
  134. if (!dentry) {
  135. dev_err(dev, "%s: Failed to rename link from: %s to %s\n",
  136. __func__, dev_name(opp_dev->dev), dev_name(dev));
  137. return;
  138. }
  139. new_dev->dentry = dentry;
  140. opp_table->dentry = dentry;
  141. }
  142. /**
  143. * opp_debug_unregister - remove a device opp node from debugfs opp directory
  144. * @opp_dev: opp-dev pointer for device
  145. * @opp_table: the device-opp being removed
  146. *
  147. * Dynamically removes device specific directory from debugfs 'opp' directory.
  148. */
  149. void opp_debug_unregister(struct opp_device *opp_dev,
  150. struct opp_table *opp_table)
  151. {
  152. if (opp_dev->dentry == opp_table->dentry) {
  153. /* Move the real dentry object under another device */
  154. if (!list_is_singular(&opp_table->dev_list)) {
  155. opp_migrate_dentry(opp_dev, opp_table);
  156. goto out;
  157. }
  158. opp_table->dentry = NULL;
  159. }
  160. debugfs_remove_recursive(opp_dev->dentry);
  161. out:
  162. opp_dev->dentry = NULL;
  163. }
  164. static int __init opp_debug_init(void)
  165. {
  166. /* Create /sys/kernel/debug/opp directory */
  167. rootdir = debugfs_create_dir("opp", NULL);
  168. if (!rootdir) {
  169. pr_err("%s: Failed to create root directory\n", __func__);
  170. return -ENOMEM;
  171. }
  172. return 0;
  173. }
  174. core_initcall(opp_debug_init);