clk-ti-sci.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Texas Instruments System Control Interface (TI SCI) clock driver
  4. *
  5. * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
  6. * Andreas Dannenberg <dannenberg@ti.com>
  7. *
  8. * Loosely based on Linux kernel sci-clk.c...
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <clk-uclass.h>
  14. #include <log.h>
  15. #include <malloc.h>
  16. #include <dm/device_compat.h>
  17. #include <linux/err.h>
  18. #include <linux/soc/ti/ti_sci_protocol.h>
  19. #include <k3-avs.h>
  20. /**
  21. * struct ti_sci_clk_data - clock controller information structure
  22. * @sci: TI SCI handle used for communication with system controller
  23. */
  24. struct ti_sci_clk_data {
  25. const struct ti_sci_handle *sci;
  26. };
  27. static int ti_sci_clk_probe(struct udevice *dev)
  28. {
  29. struct ti_sci_clk_data *data = dev_get_priv(dev);
  30. debug("%s(dev=%p)\n", __func__, dev);
  31. if (!data)
  32. return -ENOMEM;
  33. /* Store handle for communication with the system controller */
  34. data->sci = ti_sci_get_handle(dev);
  35. if (IS_ERR(data->sci))
  36. return PTR_ERR(data->sci);
  37. return 0;
  38. }
  39. static int ti_sci_clk_of_xlate(struct clk *clk,
  40. struct ofnode_phandle_args *args)
  41. {
  42. debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
  43. if (args->args_count != 2) {
  44. debug("Invalid args_count: %d\n", args->args_count);
  45. return -EINVAL;
  46. }
  47. /*
  48. * On TI SCI-based devices, the clock provider id field is used as a
  49. * device ID, and the data field is used as the associated sub-ID.
  50. */
  51. clk->id = args->args[0];
  52. clk->data = args->args[1];
  53. return 0;
  54. }
  55. static int ti_sci_clk_request(struct clk *clk)
  56. {
  57. debug("%s(clk=%p)\n", __func__, clk);
  58. return 0;
  59. }
  60. static int ti_sci_clk_free(struct clk *clk)
  61. {
  62. debug("%s(clk=%p)\n", __func__, clk);
  63. return 0;
  64. }
  65. static ulong ti_sci_clk_get_rate(struct clk *clk)
  66. {
  67. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  68. const struct ti_sci_handle *sci = data->sci;
  69. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  70. u64 current_freq;
  71. int ret;
  72. debug("%s(clk=%p)\n", __func__, clk);
  73. ret = cops->get_freq(sci, clk->id, clk->data, &current_freq);
  74. if (ret) {
  75. dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
  76. return ret;
  77. }
  78. debug("%s(current_freq=%llu)\n", __func__, current_freq);
  79. return current_freq;
  80. }
  81. static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
  82. {
  83. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  84. const struct ti_sci_handle *sci = data->sci;
  85. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  86. int ret;
  87. debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
  88. #ifdef CONFIG_K3_AVS0
  89. k3_avs_notify_freq(clk->id, clk->data, rate);
  90. #endif
  91. ret = cops->set_freq(sci, clk->id, clk->data, 0, rate, ULONG_MAX);
  92. if (ret)
  93. dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
  94. return ret;
  95. }
  96. static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
  97. {
  98. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  99. const struct ti_sci_handle *sci = data->sci;
  100. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  101. u8 num_parents;
  102. u8 parent_cid;
  103. int ret;
  104. debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
  105. /* Make sure the clock parent is valid for a given device ID */
  106. if (clk->id != parent->id)
  107. return -EINVAL;
  108. /* Make sure clock has parents that can be set */
  109. ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
  110. if (ret) {
  111. dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
  112. __func__, ret);
  113. return ret;
  114. }
  115. if (num_parents < 2) {
  116. dev_err(clk->dev, "%s: clock has no settable parents!\n",
  117. __func__);
  118. return -EINVAL;
  119. }
  120. /* Make sure parent clock ID is valid */
  121. parent_cid = parent->data - clk->data - 1;
  122. if (parent_cid >= num_parents) {
  123. dev_err(clk->dev, "%s: invalid parent clock!\n", __func__);
  124. return -EINVAL;
  125. }
  126. /* Ready to proceed to configure the new clock parent */
  127. ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
  128. if (ret)
  129. dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
  130. ret);
  131. return ret;
  132. }
  133. static int ti_sci_clk_enable(struct clk *clk)
  134. {
  135. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  136. const struct ti_sci_handle *sci = data->sci;
  137. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  138. int ret;
  139. debug("%s(clk=%p)\n", __func__, clk);
  140. /*
  141. * Allow the System Controller to automatically manage the state of
  142. * this clock. If the device is enabled, then the clock is enabled.
  143. */
  144. ret = cops->put_clock(sci, clk->id, clk->data);
  145. if (ret)
  146. dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
  147. return ret;
  148. }
  149. static int ti_sci_clk_disable(struct clk *clk)
  150. {
  151. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  152. const struct ti_sci_handle *sci = data->sci;
  153. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  154. int ret;
  155. debug("%s(clk=%p)\n", __func__, clk);
  156. /* Unconditionally disable clock, regardless of state of the device */
  157. ret = cops->idle_clock(sci, clk->id, clk->data);
  158. if (ret)
  159. dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
  160. ret);
  161. return ret;
  162. }
  163. static const struct udevice_id ti_sci_clk_of_match[] = {
  164. { .compatible = "ti,k2g-sci-clk" },
  165. { /* sentinel */ },
  166. };
  167. static struct clk_ops ti_sci_clk_ops = {
  168. .of_xlate = ti_sci_clk_of_xlate,
  169. .request = ti_sci_clk_request,
  170. .rfree = ti_sci_clk_free,
  171. .get_rate = ti_sci_clk_get_rate,
  172. .set_rate = ti_sci_clk_set_rate,
  173. .set_parent = ti_sci_clk_set_parent,
  174. .enable = ti_sci_clk_enable,
  175. .disable = ti_sci_clk_disable,
  176. };
  177. U_BOOT_DRIVER(ti_sci_clk) = {
  178. .name = "ti-sci-clk",
  179. .id = UCLASS_CLK,
  180. .of_match = ti_sci_clk_of_match,
  181. .probe = ti_sci_clk_probe,
  182. .priv_auto_alloc_size = sizeof(struct ti_sci_clk_data),
  183. .ops = &ti_sci_clk_ops,
  184. };