clk-scmi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. struct clk_init_data init = {
  89. .flags = CLK_GET_RATE_NOCACHE,
  90. .num_parents = 0,
  91. .ops = &scmi_clk_ops,
  92. .name = sclk->info->name,
  93. };
  94. sclk->hw.init = &init;
  95. ret = devm_clk_hw_register(dev, &sclk->hw);
  96. if (!ret)
  97. clk_hw_set_rate_range(&sclk->hw, sclk->info->range.min_rate,
  98. sclk->info->range.max_rate);
  99. return ret;
  100. }
  101. static int scmi_clocks_probe(struct scmi_device *sdev)
  102. {
  103. int idx, count, err;
  104. struct clk_hw **hws;
  105. struct clk_hw_onecell_data *clk_data;
  106. struct device *dev = &sdev->dev;
  107. struct device_node *np = dev->of_node;
  108. const struct scmi_handle *handle = sdev->handle;
  109. if (!handle || !handle->clk_ops)
  110. return -ENODEV;
  111. count = handle->clk_ops->count_get(handle);
  112. if (count < 0) {
  113. dev_err(dev, "%pOFn: invalid clock output count\n", np);
  114. return -EINVAL;
  115. }
  116. clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
  117. GFP_KERNEL);
  118. if (!clk_data)
  119. return -ENOMEM;
  120. clk_data->num = count;
  121. hws = clk_data->hws;
  122. for (idx = 0; idx < count; idx++) {
  123. struct scmi_clk *sclk;
  124. sclk = devm_kzalloc(dev, sizeof(*sclk), GFP_KERNEL);
  125. if (!sclk)
  126. return -ENOMEM;
  127. sclk->info = handle->clk_ops->info_get(handle, idx);
  128. if (!sclk->info) {
  129. dev_dbg(dev, "invalid clock info for idx %d\n", idx);
  130. continue;
  131. }
  132. sclk->id = idx;
  133. sclk->handle = handle;
  134. err = scmi_clk_ops_init(dev, sclk);
  135. if (err) {
  136. dev_err(dev, "failed to register clock %d\n", idx);
  137. devm_kfree(dev, sclk);
  138. hws[idx] = NULL;
  139. } else {
  140. dev_dbg(dev, "Registered clock:%s\n", sclk->info->name);
  141. hws[idx] = &sclk->hw;
  142. }
  143. }
  144. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
  145. clk_data);
  146. }
  147. static const struct scmi_device_id scmi_id_table[] = {
  148. { SCMI_PROTOCOL_CLOCK },
  149. { },
  150. };
  151. MODULE_DEVICE_TABLE(scmi, scmi_id_table);
  152. static struct scmi_driver scmi_clocks_driver = {
  153. .name = "scmi-clocks",
  154. .probe = scmi_clocks_probe,
  155. .id_table = scmi_id_table,
  156. };
  157. module_scmi_driver(scmi_clocks_driver);
  158. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  159. MODULE_DESCRIPTION("ARM SCMI clock driver");
  160. MODULE_LICENSE("GPL v2");