clk-scpi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 long scpi_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  29. unsigned long *parent_rate)
  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 rate;
  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. .round_rate = scpi_clk_round_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 long scpi_dvfs_round_rate(struct clk_hw *hw, unsigned long rate,
  80. unsigned long *parent_rate)
  81. {
  82. struct scpi_clk *clk = to_scpi_clk(hw);
  83. return __scpi_dvfs_round_rate(clk, rate);
  84. }
  85. static int __scpi_find_dvfs_index(struct scpi_clk *clk, unsigned long rate)
  86. {
  87. int idx, max_opp = clk->info->count;
  88. const struct scpi_opp *opp = clk->info->opps;
  89. for (idx = 0; idx < max_opp; idx++, opp++)
  90. if (opp->freq == rate)
  91. return idx;
  92. return -EINVAL;
  93. }
  94. static int scpi_dvfs_set_rate(struct clk_hw *hw, unsigned long rate,
  95. unsigned long parent_rate)
  96. {
  97. struct scpi_clk *clk = to_scpi_clk(hw);
  98. int ret = __scpi_find_dvfs_index(clk, rate);
  99. if (ret < 0)
  100. return ret;
  101. return clk->scpi_ops->dvfs_set_idx(clk->id, (u8)ret);
  102. }
  103. static const struct clk_ops scpi_dvfs_ops = {
  104. .recalc_rate = scpi_dvfs_recalc_rate,
  105. .round_rate = scpi_dvfs_round_rate,
  106. .set_rate = scpi_dvfs_set_rate,
  107. };
  108. static const struct of_device_id scpi_clk_match[] __maybe_unused = {
  109. { .compatible = "arm,scpi-dvfs-clocks", .data = &scpi_dvfs_ops, },
  110. { .compatible = "arm,scpi-variable-clocks", .data = &scpi_clk_ops, },
  111. {}
  112. };
  113. static int
  114. scpi_clk_ops_init(struct device *dev, const struct of_device_id *match,
  115. struct scpi_clk *sclk, const char *name)
  116. {
  117. struct clk_init_data init;
  118. unsigned long min = 0, max = 0;
  119. int ret;
  120. init.name = name;
  121. init.flags = 0;
  122. init.num_parents = 0;
  123. init.ops = match->data;
  124. sclk->hw.init = &init;
  125. sclk->scpi_ops = get_scpi_ops();
  126. if (init.ops == &scpi_dvfs_ops) {
  127. sclk->info = sclk->scpi_ops->dvfs_get_info(sclk->id);
  128. if (IS_ERR(sclk->info))
  129. return PTR_ERR(sclk->info);
  130. } else if (init.ops == &scpi_clk_ops) {
  131. if (sclk->scpi_ops->clk_get_range(sclk->id, &min, &max) || !max)
  132. return -EINVAL;
  133. } else {
  134. return -EINVAL;
  135. }
  136. ret = devm_clk_hw_register(dev, &sclk->hw);
  137. if (!ret && max)
  138. clk_hw_set_rate_range(&sclk->hw, min, max);
  139. return ret;
  140. }
  141. struct scpi_clk_data {
  142. struct scpi_clk **clk;
  143. unsigned int clk_num;
  144. };
  145. static struct clk_hw *
  146. scpi_of_clk_src_get(struct of_phandle_args *clkspec, void *data)
  147. {
  148. struct scpi_clk *sclk;
  149. struct scpi_clk_data *clk_data = data;
  150. unsigned int idx = clkspec->args[0], count;
  151. for (count = 0; count < clk_data->clk_num; count++) {
  152. sclk = clk_data->clk[count];
  153. if (idx == sclk->id)
  154. return &sclk->hw;
  155. }
  156. return ERR_PTR(-EINVAL);
  157. }
  158. static int scpi_clk_add(struct device *dev, struct device_node *np,
  159. const struct of_device_id *match)
  160. {
  161. int idx, count, err;
  162. struct scpi_clk_data *clk_data;
  163. count = of_property_count_strings(np, "clock-output-names");
  164. if (count < 0) {
  165. dev_err(dev, "%pOFn: invalid clock output count\n", np);
  166. return -EINVAL;
  167. }
  168. clk_data = devm_kmalloc(dev, sizeof(*clk_data), GFP_KERNEL);
  169. if (!clk_data)
  170. return -ENOMEM;
  171. clk_data->clk_num = count;
  172. clk_data->clk = devm_kcalloc(dev, count, sizeof(*clk_data->clk),
  173. GFP_KERNEL);
  174. if (!clk_data->clk)
  175. return -ENOMEM;
  176. for (idx = 0; idx < count; idx++) {
  177. struct scpi_clk *sclk;
  178. const char *name;
  179. u32 val;
  180. sclk = devm_kzalloc(dev, sizeof(*sclk), GFP_KERNEL);
  181. if (!sclk)
  182. return -ENOMEM;
  183. if (of_property_read_string_index(np, "clock-output-names",
  184. idx, &name)) {
  185. dev_err(dev, "invalid clock name @ %pOFn\n", np);
  186. return -EINVAL;
  187. }
  188. if (of_property_read_u32_index(np, "clock-indices",
  189. idx, &val)) {
  190. dev_err(dev, "invalid clock index @ %pOFn\n", np);
  191. return -EINVAL;
  192. }
  193. sclk->id = val;
  194. err = scpi_clk_ops_init(dev, match, sclk, name);
  195. if (err) {
  196. dev_err(dev, "failed to register clock '%s'\n", name);
  197. return err;
  198. }
  199. dev_dbg(dev, "Registered clock '%s'\n", name);
  200. clk_data->clk[idx] = sclk;
  201. }
  202. return of_clk_add_hw_provider(np, scpi_of_clk_src_get, clk_data);
  203. }
  204. static void scpi_clocks_remove(struct platform_device *pdev)
  205. {
  206. struct device *dev = &pdev->dev;
  207. struct device_node *child, *np = dev->of_node;
  208. if (cpufreq_dev) {
  209. platform_device_unregister(cpufreq_dev);
  210. cpufreq_dev = NULL;
  211. }
  212. for_each_available_child_of_node(np, child)
  213. of_clk_del_provider(np);
  214. }
  215. static int scpi_clocks_probe(struct platform_device *pdev)
  216. {
  217. int ret;
  218. struct device *dev = &pdev->dev;
  219. struct device_node *child, *np = dev->of_node;
  220. const struct of_device_id *match;
  221. if (!get_scpi_ops())
  222. return -ENXIO;
  223. for_each_available_child_of_node(np, child) {
  224. match = of_match_node(scpi_clk_match, child);
  225. if (!match)
  226. continue;
  227. ret = scpi_clk_add(dev, child, match);
  228. if (ret) {
  229. scpi_clocks_remove(pdev);
  230. of_node_put(child);
  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");