clk-scmi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Power Interface (SCMI) Protocol based clock driver
  4. *
  5. * Copyright (C) 2018 ARM Ltd.
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/of.h>
  11. #include <linux/module.h>
  12. #include <linux/scmi_protocol.h>
  13. #include <asm/div64.h>
  14. struct scmi_clk {
  15. u32 id;
  16. struct clk_hw hw;
  17. const struct scmi_clock_info *info;
  18. const struct scmi_handle *handle;
  19. };
  20. #define to_scmi_clk(clk) container_of(clk, struct scmi_clk, hw)
  21. static unsigned long scmi_clk_recalc_rate(struct clk_hw *hw,
  22. unsigned long parent_rate)
  23. {
  24. int ret;
  25. u64 rate;
  26. struct scmi_clk *clk = to_scmi_clk(hw);
  27. ret = clk->handle->clk_ops->rate_get(clk->handle, clk->id, &rate);
  28. if (ret)
  29. return 0;
  30. return rate;
  31. }
  32. static long scmi_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  33. unsigned long *parent_rate)
  34. {
  35. u64 fmin, fmax, ftmp;
  36. struct scmi_clk *clk = to_scmi_clk(hw);
  37. /*
  38. * We can't figure out what rate it will be, so just return the
  39. * rate back to the caller. scmi_clk_recalc_rate() will be called
  40. * after the rate is set and we'll know what rate the clock is
  41. * running at then.
  42. */
  43. if (clk->info->rate_discrete)
  44. return rate;
  45. fmin = clk->info->range.min_rate;
  46. fmax = clk->info->range.max_rate;
  47. if (rate <= fmin)
  48. return fmin;
  49. else if (rate >= fmax)
  50. return fmax;
  51. ftmp = rate - fmin;
  52. ftmp += clk->info->range.step_size - 1; /* to round up */
  53. do_div(ftmp, clk->info->range.step_size);
  54. return ftmp * clk->info->range.step_size + fmin;
  55. }
  56. static int scmi_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  57. unsigned long parent_rate)
  58. {
  59. struct scmi_clk *clk = to_scmi_clk(hw);
  60. return clk->handle->clk_ops->rate_set(clk->handle, clk->id, 0, rate);
  61. }
  62. static int scmi_clk_enable(struct clk_hw *hw)
  63. {
  64. struct scmi_clk *clk = to_scmi_clk(hw);
  65. return clk->handle->clk_ops->enable(clk->handle, clk->id);
  66. }
  67. static void scmi_clk_disable(struct clk_hw *hw)
  68. {
  69. struct scmi_clk *clk = to_scmi_clk(hw);
  70. clk->handle->clk_ops->disable(clk->handle, clk->id);
  71. }
  72. static const struct clk_ops scmi_clk_ops = {
  73. .recalc_rate = scmi_clk_recalc_rate,
  74. .round_rate = scmi_clk_round_rate,
  75. .set_rate = scmi_clk_set_rate,
  76. /*
  77. * We can't provide enable/disable callback as we can't perform the same
  78. * in atomic context. Since the clock framework provides standard API
  79. * clk_prepare_enable that helps cases using clk_enable in non-atomic
  80. * context, it should be fine providing prepare/unprepare.
  81. */
  82. .prepare = scmi_clk_enable,
  83. .unprepare = scmi_clk_disable,
  84. };
  85. static int scmi_clk_ops_init(struct device *dev, struct scmi_clk *sclk)
  86. {
  87. int ret;
  88. unsigned long min_rate, max_rate;
  89. struct clk_init_data init = {
  90. .flags = CLK_GET_RATE_NOCACHE,
  91. .num_parents = 0,
  92. .ops = &scmi_clk_ops,
  93. .name = sclk->info->name,
  94. };
  95. sclk->hw.init = &init;
  96. ret = devm_clk_hw_register(dev, &sclk->hw);
  97. if (ret)
  98. return ret;
  99. if (sclk->info->rate_discrete) {
  100. int num_rates = sclk->info->list.num_rates;
  101. if (num_rates <= 0)
  102. return -EINVAL;
  103. min_rate = sclk->info->list.rates[0];
  104. max_rate = sclk->info->list.rates[num_rates - 1];
  105. } else {
  106. min_rate = sclk->info->range.min_rate;
  107. max_rate = sclk->info->range.max_rate;
  108. }
  109. clk_hw_set_rate_range(&sclk->hw, min_rate, max_rate);
  110. return ret;
  111. }
  112. static int scmi_clocks_probe(struct scmi_device *sdev)
  113. {
  114. int idx, count, err;
  115. struct clk_hw **hws;
  116. struct clk_hw_onecell_data *clk_data;
  117. struct device *dev = &sdev->dev;
  118. struct device_node *np = dev->of_node;
  119. const struct scmi_handle *handle = sdev->handle;
  120. if (!handle || !handle->clk_ops)
  121. return -ENODEV;
  122. count = handle->clk_ops->count_get(handle);
  123. if (count < 0) {
  124. dev_err(dev, "%s: invalid clock output count\n", np->name);
  125. return -EINVAL;
  126. }
  127. clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
  128. GFP_KERNEL);
  129. if (!clk_data)
  130. return -ENOMEM;
  131. clk_data->num = count;
  132. hws = clk_data->hws;
  133. for (idx = 0; idx < count; idx++) {
  134. struct scmi_clk *sclk;
  135. sclk = devm_kzalloc(dev, sizeof(*sclk), GFP_KERNEL);
  136. if (!sclk)
  137. return -ENOMEM;
  138. sclk->info = handle->clk_ops->info_get(handle, idx);
  139. if (!sclk->info) {
  140. dev_dbg(dev, "invalid clock info for idx %d\n", idx);
  141. continue;
  142. }
  143. sclk->id = idx;
  144. sclk->handle = handle;
  145. err = scmi_clk_ops_init(dev, sclk);
  146. if (err) {
  147. dev_err(dev, "failed to register clock %d\n", idx);
  148. devm_kfree(dev, sclk);
  149. hws[idx] = NULL;
  150. } else {
  151. dev_dbg(dev, "Registered clock:%s\n", sclk->info->name);
  152. hws[idx] = &sclk->hw;
  153. }
  154. }
  155. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
  156. clk_data);
  157. }
  158. static const struct scmi_device_id scmi_id_table[] = {
  159. { SCMI_PROTOCOL_CLOCK },
  160. { },
  161. };
  162. MODULE_DEVICE_TABLE(scmi, scmi_id_table);
  163. static struct scmi_driver scmi_clocks_driver = {
  164. .name = "scmi-clocks",
  165. .probe = scmi_clocks_probe,
  166. .id_table = scmi_id_table,
  167. };
  168. module_scmi_driver(scmi_clocks_driver);
  169. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  170. MODULE_DESCRIPTION("ARM SCMI clock driver");
  171. MODULE_LICENSE("GPL v2");