clk-uclass.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. * Copyright (c) 2016, NVIDIA CORPORATION.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <clk.h>
  10. #include <clk-uclass.h>
  11. #include <dm.h>
  12. #include <dt-structs.h>
  13. #include <errno.h>
  14. static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
  15. {
  16. return (const struct clk_ops *)dev->driver->ops;
  17. }
  18. #if CONFIG_IS_ENABLED(OF_CONTROL)
  19. # if CONFIG_IS_ENABLED(OF_PLATDATA)
  20. int clk_get_by_index_platdata(struct udevice *dev, int index,
  21. struct phandle_1_arg *cells, struct clk *clk)
  22. {
  23. int ret;
  24. if (index != 0)
  25. return -ENOSYS;
  26. ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
  27. if (ret)
  28. return ret;
  29. clk->id = cells[0].arg[0];
  30. return 0;
  31. }
  32. # else
  33. static int clk_of_xlate_default(struct clk *clk,
  34. struct ofnode_phandle_args *args)
  35. {
  36. debug("%s(clk=%p)\n", __func__, clk);
  37. if (args->args_count > 1) {
  38. debug("Invaild args_count: %d\n", args->args_count);
  39. return -EINVAL;
  40. }
  41. if (args->args_count)
  42. clk->id = args->args[0];
  43. else
  44. clk->id = 0;
  45. return 0;
  46. }
  47. int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
  48. {
  49. int ret;
  50. struct ofnode_phandle_args args;
  51. struct udevice *dev_clk;
  52. const struct clk_ops *ops;
  53. debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
  54. assert(clk);
  55. clk->dev = NULL;
  56. ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
  57. index, &args);
  58. if (ret) {
  59. debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
  60. __func__, ret);
  61. return ret;
  62. }
  63. ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &dev_clk);
  64. if (ret) {
  65. debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
  66. __func__, ret);
  67. return ret;
  68. }
  69. clk->dev = dev_clk;
  70. ops = clk_dev_ops(dev_clk);
  71. if (ops->of_xlate)
  72. ret = ops->of_xlate(clk, &args);
  73. else
  74. ret = clk_of_xlate_default(clk, &args);
  75. if (ret) {
  76. debug("of_xlate() failed: %d\n", ret);
  77. return ret;
  78. }
  79. return clk_request(dev_clk, clk);
  80. }
  81. # endif /* OF_PLATDATA */
  82. int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
  83. {
  84. int index;
  85. debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
  86. clk->dev = NULL;
  87. index = dev_read_stringlist_search(dev, "clock-names", name);
  88. if (index < 0) {
  89. debug("fdt_stringlist_search() failed: %d\n", index);
  90. return index;
  91. }
  92. return clk_get_by_index(dev, index, clk);
  93. }
  94. int clk_release_all(struct clk *clk, int count)
  95. {
  96. int i, ret;
  97. for (i = 0; i < count; i++) {
  98. debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
  99. /* check if clock has been previously requested */
  100. if (!clk[i].dev)
  101. continue;
  102. ret = clk_disable(&clk[i]);
  103. if (ret && ret != -ENOSYS)
  104. return ret;
  105. ret = clk_free(&clk[i]);
  106. if (ret && ret != -ENOSYS)
  107. return ret;
  108. }
  109. return 0;
  110. }
  111. #endif /* OF_CONTROL */
  112. int clk_request(struct udevice *dev, struct clk *clk)
  113. {
  114. const struct clk_ops *ops = clk_dev_ops(dev);
  115. debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
  116. clk->dev = dev;
  117. if (!ops->request)
  118. return 0;
  119. return ops->request(clk);
  120. }
  121. int clk_free(struct clk *clk)
  122. {
  123. const struct clk_ops *ops = clk_dev_ops(clk->dev);
  124. debug("%s(clk=%p)\n", __func__, clk);
  125. if (!ops->free)
  126. return 0;
  127. return ops->free(clk);
  128. }
  129. ulong clk_get_rate(struct clk *clk)
  130. {
  131. const struct clk_ops *ops = clk_dev_ops(clk->dev);
  132. debug("%s(clk=%p)\n", __func__, clk);
  133. if (!ops->get_rate)
  134. return -ENOSYS;
  135. return ops->get_rate(clk);
  136. }
  137. ulong clk_set_rate(struct clk *clk, ulong rate)
  138. {
  139. const struct clk_ops *ops = clk_dev_ops(clk->dev);
  140. debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
  141. if (!ops->set_rate)
  142. return -ENOSYS;
  143. return ops->set_rate(clk, rate);
  144. }
  145. int clk_set_parent(struct clk *clk, struct clk *parent)
  146. {
  147. const struct clk_ops *ops = clk_dev_ops(clk->dev);
  148. debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
  149. if (!ops->set_parent)
  150. return -ENOSYS;
  151. return ops->set_parent(clk, parent);
  152. }
  153. int clk_enable(struct clk *clk)
  154. {
  155. const struct clk_ops *ops = clk_dev_ops(clk->dev);
  156. debug("%s(clk=%p)\n", __func__, clk);
  157. if (!ops->enable)
  158. return -ENOSYS;
  159. return ops->enable(clk);
  160. }
  161. int clk_disable(struct clk *clk)
  162. {
  163. const struct clk_ops *ops = clk_dev_ops(clk->dev);
  164. debug("%s(clk=%p)\n", __func__, clk);
  165. if (!ops->disable)
  166. return -ENOSYS;
  167. return ops->disable(clk);
  168. }
  169. UCLASS_DRIVER(clk) = {
  170. .id = UCLASS_CLK,
  171. .name = "clk",
  172. };