clk-scpi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * System Control and Power Interface (SCPI) Protocol based clock driver
  4. *
  5. * Copyright (C) 2015 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/platform_device.h>
  13. #include <linux/scpi_protocol.h>
  14. struct scpi_clk {
  15. u32 id;
  16. struct clk_hw hw;
  17. struct scpi_dvfs_info *info;
  18. struct scpi_ops *scpi_ops;
  19. };
  20. #define to_scpi_clk(clk) container_of(clk, struct scpi_clk, hw)
  21. static struct platform_device *cpufreq_dev;
  22. static unsigned long scpi_clk_recalc_rate(struct clk_hw *hw,
  23. unsigned long parent_rate)
  24. {
  25. struct scpi_clk *clk = to_scpi_clk(hw);
  26. return clk->scpi_ops->clk_get_val(clk->id);
  27. }
  28. static int scpi_clk_determine_rate(struct clk_hw *hw,
  29. struct clk_rate_request *req)
  30. {
  31. /*
  32. * We can't figure out what rate it will be, so just return the
  33. * rate back to the caller. scpi_clk_recalc_rate() will be called
  34. * after the rate is set and we'll know what rate the clock is
  35. * running at then.
  36. */
  37. return 0;
  38. }
  39. static int scpi_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  40. unsigned long parent_rate)
  41. {
  42. struct scpi_clk *clk = to_scpi_clk(hw);
  43. return clk->scpi_ops->clk_set_val(clk->id, rate);
  44. }
  45. static const struct clk_ops scpi_clk_ops = {
  46. .recalc_rate = scpi_clk_recalc_rate,
  47. .determine_rate = scpi_clk_determine_rate,
  48. .set_rate = scpi_clk_set_rate,
  49. };
  50. /* find closest match to given frequency in OPP table */
  51. static long __scpi_dvfs_round_rate(struct scpi_clk *clk, unsigned long rate)
  52. {
  53. int idx;
  54. unsigned long fmin = 0, fmax = ~0, ftmp;
  55. const struct scpi_opp *opp = clk->info->opps;
  56. for (idx = 0; idx < clk->info->count; idx++, opp++) {
  57. ftmp = opp->freq;
  58. if (ftmp >= rate) {
  59. if (ftmp <= fmax)
  60. fmax = ftmp;
  61. break;
  62. } else if (ftmp >= fmin) {
  63. fmin = ftmp;
  64. }
  65. }
  66. return fmax != ~0 ? fmax : fmin;
  67. }
  68. static unsigned long scpi_dvfs_recalc_rate(struct clk_hw *hw,
  69. unsigned long parent_rate)
  70. {
  71. struct scpi_clk *clk = to_scpi_clk(hw);
  72. int idx = clk->scpi_ops->dvfs_get_idx(clk->id);
  73. const struct scpi_opp *opp;
  74. if (idx < 0)
  75. return 0;
  76. opp = clk->info->opps + idx;
  77. return opp->freq;
  78. }
  79. static int scpi_dvfs_determine_rate(struct clk_hw *hw,
  80. struct clk_rate_request *req)
  81. {
  82. struct scpi_clk *clk = to_scpi_clk(hw);
  83. req->rate = __scpi_dvfs_round_rate(clk, req->rate);
  84. return 0;
  85. }
  86. static int __scpi_find_dvfs_index(struct scpi_clk *clk, unsigned long rate)
  87. {
  88. int idx, max_opp = clk->info->count;
  89. const struct scpi_opp *opp = clk->info->opps;
  90. for (idx = 0; idx < max_opp; idx++, opp++)
  91. if (opp->freq == rate)
  92. return idx;
  93. return -EINVAL;
  94. }
  95. static int scpi_dvfs_set_rate(struct clk_hw *hw, unsigned long rate,
  96. unsigned long parent_rate)
  97. {
  98. struct scpi_clk *clk = to_scpi_clk(hw);
  99. int ret = __scpi_find_dvfs_index(clk, rate);
  100. if (ret < 0)
  101. return ret;
  102. return clk->scpi_ops->dvfs_set_idx(clk->id, (u8)ret);
  103. }
  104. static const struct clk_ops scpi_dvfs_ops = {
  105. .recalc_rate = scpi_dvfs_recalc_rate,
  106. .determine_rate = scpi_dvfs_determine_rate,
  107. .set_rate = scpi_dvfs_set_rate,
  108. };
  109. static const struct of_device_id scpi_clk_match[] __maybe_unused = {
  110. { .compatible = "arm,scpi-dvfs-clocks", .data = &scpi_dvfs_ops, },
  111. { .compatible = "arm,scpi-variable-clocks", .data = &scpi_clk_ops, },
  112. {}
  113. };
  114. static int
  115. scpi_clk_ops_init(struct device *dev, const struct of_device_id *match,
  116. struct scpi_clk *sclk, const char *name)
  117. {
  118. struct clk_init_data init;
  119. unsigned long min = 0, max = 0;
  120. int ret;
  121. init.name = name;
  122. init.flags = 0;
  123. init.num_parents = 0;
  124. init.ops = match->data;
  125. sclk->hw.init = &init;
  126. sclk->scpi_ops = get_scpi_ops();
  127. if (init.ops == &scpi_dvfs_ops) {
  128. sclk->info = sclk->scpi_ops->dvfs_get_info(sclk->id);
  129. if (IS_ERR(sclk->info))
  130. return PTR_ERR(sclk->info);
  131. } else if (init.ops == &scpi_clk_ops) {
  132. if (sclk->scpi_ops->clk_get_range(sclk->id, &min, &max) || !max)
  133. return -EINVAL;
  134. } else {
  135. return -EINVAL;
  136. }
  137. ret = devm_clk_hw_register(dev, &sclk->hw);
  138. if (!ret && max)
  139. clk_hw_set_rate_range(&sclk->hw, min, max);
  140. return ret;
  141. }
  142. struct scpi_clk_data {
  143. struct scpi_clk **clk;
  144. unsigned int clk_num;
  145. };
  146. static struct clk_hw *
  147. scpi_of_clk_src_get(struct of_phandle_args *clkspec, void *data)
  148. {
  149. struct scpi_clk *sclk;
  150. struct scpi_clk_data *clk_data = data;
  151. unsigned int idx = clkspec->args[0], count;
  152. for (count = 0; count < clk_data->clk_num; count++) {
  153. sclk = clk_data->clk[count];
  154. if (idx == sclk->id)
  155. return &sclk->hw;
  156. }
  157. return ERR_PTR(-EINVAL);
  158. }
  159. static int scpi_clk_add(struct device *dev, struct device_node *np,
  160. const struct of_device_id *match)
  161. {
  162. int idx, count, err;
  163. struct scpi_clk_data *clk_data;
  164. count = of_property_count_strings(np, "clock-output-names");
  165. if (count < 0) {
  166. dev_err(dev, "%pOFn: invalid clock output count\n", np);
  167. return -EINVAL;
  168. }
  169. clk_data = devm_kmalloc(dev, sizeof(*clk_data), GFP_KERNEL);
  170. if (!clk_data)
  171. return -ENOMEM;
  172. clk_data->clk_num = count;
  173. clk_data->clk = devm_kcalloc(dev, count, sizeof(*clk_data->clk),
  174. GFP_KERNEL);
  175. if (!clk_data->clk)
  176. return -ENOMEM;
  177. for (idx = 0; idx < count; idx++) {
  178. struct scpi_clk *sclk;
  179. const char *name;
  180. u32 val;
  181. sclk = devm_kzalloc(dev, sizeof(*sclk), GFP_KERNEL);
  182. if (!sclk)
  183. return -ENOMEM;
  184. if (of_property_read_string_index(np, "clock-output-names",
  185. idx, &name)) {
  186. dev_err(dev, "invalid clock name @ %pOFn\n", np);
  187. return -EINVAL;
  188. }
  189. if (of_property_read_u32_index(np, "clock-indices",
  190. idx, &val)) {
  191. dev_err(dev, "invalid clock index @ %pOFn\n", np);
  192. return -EINVAL;
  193. }
  194. sclk->id = val;
  195. err = scpi_clk_ops_init(dev, match, sclk, name);
  196. if (err) {
  197. dev_err(dev, "failed to register clock '%s'\n", name);
  198. return err;
  199. }
  200. dev_dbg(dev, "Registered clock '%s'\n", name);
  201. clk_data->clk[idx] = sclk;
  202. }
  203. return of_clk_add_hw_provider(np, scpi_of_clk_src_get, clk_data);
  204. }
  205. static void scpi_clocks_remove(struct platform_device *pdev)
  206. {
  207. struct device *dev = &pdev->dev;
  208. struct device_node *child, *np = dev->of_node;
  209. if (cpufreq_dev) {
  210. platform_device_unregister(cpufreq_dev);
  211. cpufreq_dev = NULL;
  212. }
  213. for_each_available_child_of_node(np, child)
  214. of_clk_del_provider(np);
  215. }
  216. static int scpi_clocks_probe(struct platform_device *pdev)
  217. {
  218. int ret;
  219. struct device *dev = &pdev->dev;
  220. struct device_node *np = dev->of_node;
  221. const struct of_device_id *match;
  222. if (!get_scpi_ops())
  223. return -ENXIO;
  224. for_each_available_child_of_node_scoped(np, child) {
  225. match = of_match_node(scpi_clk_match, child);
  226. if (!match)
  227. continue;
  228. ret = scpi_clk_add(dev, child, match);
  229. if (ret) {
  230. scpi_clocks_remove(pdev);
  231. return ret;
  232. }
  233. if (match->data != &scpi_dvfs_ops)
  234. continue;
  235. /* Add the virtual cpufreq device if it's DVFS clock provider */
  236. cpufreq_dev = platform_device_register_simple("scpi-cpufreq",
  237. -1, NULL, 0);
  238. if (IS_ERR(cpufreq_dev))
  239. pr_warn("unable to register cpufreq device");
  240. }
  241. return 0;
  242. }
  243. static const struct of_device_id scpi_clocks_ids[] = {
  244. { .compatible = "arm,scpi-clocks", },
  245. {}
  246. };
  247. MODULE_DEVICE_TABLE(of, scpi_clocks_ids);
  248. static struct platform_driver scpi_clocks_driver = {
  249. .driver = {
  250. .name = "scpi_clocks",
  251. .of_match_table = scpi_clocks_ids,
  252. },
  253. .probe = scpi_clocks_probe,
  254. .remove = scpi_clocks_remove,
  255. };
  256. module_platform_driver(scpi_clocks_driver);
  257. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  258. MODULE_DESCRIPTION("ARM SCPI clock driver");
  259. MODULE_LICENSE("GPL v2");