clk-scmi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. int step;
  36. u64 fmin, fmax, ftmp;
  37. struct scmi_clk *clk = to_scmi_clk(hw);
  38. /*
  39. * We can't figure out what rate it will be, so just return the
  40. * rate back to the caller. scmi_clk_recalc_rate() will be called
  41. * after the rate is set and we'll know what rate the clock is
  42. * running at then.
  43. */
  44. if (clk->info->rate_discrete)
  45. return rate;
  46. fmin = clk->info->range.min_rate;
  47. fmax = clk->info->range.max_rate;
  48. if (rate <= fmin)
  49. return fmin;
  50. else if (rate >= fmax)
  51. return fmax;
  52. ftmp = rate - fmin;
  53. ftmp += clk->info->range.step_size - 1; /* to round up */
  54. step = do_div(ftmp, clk->info->range.step_size);
  55. return step * clk->info->range.step_size + fmin;
  56. }
  57. static int scmi_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long parent_rate)
  59. {
  60. struct scmi_clk *clk = to_scmi_clk(hw);
  61. return clk->handle->clk_ops->rate_set(clk->handle, clk->id, 0, rate);
  62. }
  63. static int scmi_clk_enable(struct clk_hw *hw)
  64. {
  65. struct scmi_clk *clk = to_scmi_clk(hw);
  66. return clk->handle->clk_ops->enable(clk->handle, clk->id);
  67. }
  68. static void scmi_clk_disable(struct clk_hw *hw)
  69. {
  70. struct scmi_clk *clk = to_scmi_clk(hw);
  71. clk->handle->clk_ops->disable(clk->handle, clk->id);
  72. }
  73. static const struct clk_ops scmi_clk_ops = {
  74. .recalc_rate = scmi_clk_recalc_rate,
  75. .round_rate = scmi_clk_round_rate,
  76. .set_rate = scmi_clk_set_rate,
  77. /*
  78. * We can't provide enable/disable callback as we can't perform the same
  79. * in atomic context. Since the clock framework provides standard API
  80. * clk_prepare_enable that helps cases using clk_enable in non-atomic
  81. * context, it should be fine providing prepare/unprepare.
  82. */
  83. .prepare = scmi_clk_enable,
  84. .unprepare = scmi_clk_disable,
  85. };
  86. static int scmi_clk_ops_init(struct device *dev, struct scmi_clk *sclk)
  87. {
  88. int ret;
  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. clk_hw_set_rate_range(&sclk->hw, sclk->info->range.min_rate,
  99. sclk->info->range.max_rate);
  100. return ret;
  101. }
  102. static int scmi_clocks_probe(struct scmi_device *sdev)
  103. {
  104. int idx, count, err;
  105. struct clk_hw **hws;
  106. struct clk_hw_onecell_data *clk_data;
  107. struct device *dev = &sdev->dev;
  108. struct device_node *np = dev->of_node;
  109. const struct scmi_handle *handle = sdev->handle;
  110. if (!handle || !handle->clk_ops)
  111. return -ENODEV;
  112. count = handle->clk_ops->count_get(handle);
  113. if (count < 0) {
  114. dev_err(dev, "%s: invalid clock output count\n", np->name);
  115. return -EINVAL;
  116. }
  117. clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
  118. GFP_KERNEL);
  119. if (!clk_data)
  120. return -ENOMEM;
  121. clk_data->num = count;
  122. hws = clk_data->hws;
  123. for (idx = 0; idx < count; idx++) {
  124. struct scmi_clk *sclk;
  125. sclk = devm_kzalloc(dev, sizeof(*sclk), GFP_KERNEL);
  126. if (!sclk)
  127. return -ENOMEM;
  128. sclk->info = handle->clk_ops->info_get(handle, idx);
  129. if (!sclk->info) {
  130. dev_dbg(dev, "invalid clock info for idx %d\n", idx);
  131. continue;
  132. }
  133. sclk->id = idx;
  134. sclk->handle = handle;
  135. err = scmi_clk_ops_init(dev, sclk);
  136. if (err) {
  137. dev_err(dev, "failed to register clock %d\n", idx);
  138. devm_kfree(dev, sclk);
  139. hws[idx] = NULL;
  140. } else {
  141. dev_dbg(dev, "Registered clock:%s\n", sclk->info->name);
  142. hws[idx] = &sclk->hw;
  143. }
  144. }
  145. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
  146. clk_data);
  147. }
  148. static const struct scmi_device_id scmi_id_table[] = {
  149. { SCMI_PROTOCOL_CLOCK },
  150. { },
  151. };
  152. MODULE_DEVICE_TABLE(scmi, scmi_id_table);
  153. static struct scmi_driver scmi_clocks_driver = {
  154. .name = "scmi-clocks",
  155. .probe = scmi_clocks_probe,
  156. .id_table = scmi_id_table,
  157. };
  158. module_scmi_driver(scmi_clocks_driver);
  159. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  160. MODULE_DESCRIPTION("ARM SCMI clock driver");
  161. MODULE_LICENSE("GPL v2");