clk-dra7-atl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * DRA7 ATL (Audio Tracking Logic) clock driver
  4. *
  5. * Copyright (C) 2013 Texas Instruments, Inc.
  6. *
  7. * Peter Ujfalusi <[email protected]>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/clk/ti.h>
  19. #include "clock.h"
  20. #define DRA7_ATL_INSTANCES 4
  21. #define DRA7_ATL_PPMR_REG(id) (0x200 + (id * 0x80))
  22. #define DRA7_ATL_BBSR_REG(id) (0x204 + (id * 0x80))
  23. #define DRA7_ATL_ATLCR_REG(id) (0x208 + (id * 0x80))
  24. #define DRA7_ATL_SWEN_REG(id) (0x210 + (id * 0x80))
  25. #define DRA7_ATL_BWSMUX_REG(id) (0x214 + (id * 0x80))
  26. #define DRA7_ATL_AWSMUX_REG(id) (0x218 + (id * 0x80))
  27. #define DRA7_ATL_PCLKMUX_REG(id) (0x21c + (id * 0x80))
  28. #define DRA7_ATL_SWEN BIT(0)
  29. #define DRA7_ATL_DIVIDER_MASK (0x1f)
  30. #define DRA7_ATL_PCLKMUX BIT(0)
  31. struct dra7_atl_clock_info;
  32. struct dra7_atl_desc {
  33. struct clk *clk;
  34. struct clk_hw hw;
  35. struct dra7_atl_clock_info *cinfo;
  36. int id;
  37. bool probed; /* the driver for the IP has been loaded */
  38. bool valid; /* configured */
  39. bool enabled;
  40. u32 bws; /* Baseband Word Select Mux */
  41. u32 aws; /* Audio Word Select Mux */
  42. u32 divider; /* Cached divider value */
  43. };
  44. struct dra7_atl_clock_info {
  45. struct device *dev;
  46. void __iomem *iobase;
  47. struct dra7_atl_desc *cdesc;
  48. };
  49. #define to_atl_desc(_hw) container_of(_hw, struct dra7_atl_desc, hw)
  50. static inline void atl_write(struct dra7_atl_clock_info *cinfo, u32 reg,
  51. u32 val)
  52. {
  53. __raw_writel(val, cinfo->iobase + reg);
  54. }
  55. static inline int atl_read(struct dra7_atl_clock_info *cinfo, u32 reg)
  56. {
  57. return __raw_readl(cinfo->iobase + reg);
  58. }
  59. static int atl_clk_enable(struct clk_hw *hw)
  60. {
  61. struct dra7_atl_desc *cdesc = to_atl_desc(hw);
  62. if (!cdesc->probed)
  63. goto out;
  64. if (unlikely(!cdesc->valid))
  65. dev_warn(cdesc->cinfo->dev, "atl%d has not been configured\n",
  66. cdesc->id);
  67. pm_runtime_get_sync(cdesc->cinfo->dev);
  68. atl_write(cdesc->cinfo, DRA7_ATL_ATLCR_REG(cdesc->id),
  69. cdesc->divider - 1);
  70. atl_write(cdesc->cinfo, DRA7_ATL_SWEN_REG(cdesc->id), DRA7_ATL_SWEN);
  71. out:
  72. cdesc->enabled = true;
  73. return 0;
  74. }
  75. static void atl_clk_disable(struct clk_hw *hw)
  76. {
  77. struct dra7_atl_desc *cdesc = to_atl_desc(hw);
  78. if (!cdesc->probed)
  79. goto out;
  80. atl_write(cdesc->cinfo, DRA7_ATL_SWEN_REG(cdesc->id), 0);
  81. pm_runtime_put_sync(cdesc->cinfo->dev);
  82. out:
  83. cdesc->enabled = false;
  84. }
  85. static int atl_clk_is_enabled(struct clk_hw *hw)
  86. {
  87. struct dra7_atl_desc *cdesc = to_atl_desc(hw);
  88. return cdesc->enabled;
  89. }
  90. static unsigned long atl_clk_recalc_rate(struct clk_hw *hw,
  91. unsigned long parent_rate)
  92. {
  93. struct dra7_atl_desc *cdesc = to_atl_desc(hw);
  94. return parent_rate / cdesc->divider;
  95. }
  96. static long atl_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  97. unsigned long *parent_rate)
  98. {
  99. unsigned divider;
  100. divider = (*parent_rate + rate / 2) / rate;
  101. if (divider > DRA7_ATL_DIVIDER_MASK + 1)
  102. divider = DRA7_ATL_DIVIDER_MASK + 1;
  103. return *parent_rate / divider;
  104. }
  105. static int atl_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  106. unsigned long parent_rate)
  107. {
  108. struct dra7_atl_desc *cdesc;
  109. u32 divider;
  110. if (!hw || !rate)
  111. return -EINVAL;
  112. cdesc = to_atl_desc(hw);
  113. divider = ((parent_rate + rate / 2) / rate) - 1;
  114. if (divider > DRA7_ATL_DIVIDER_MASK)
  115. divider = DRA7_ATL_DIVIDER_MASK;
  116. cdesc->divider = divider + 1;
  117. return 0;
  118. }
  119. static const struct clk_ops atl_clk_ops = {
  120. .enable = atl_clk_enable,
  121. .disable = atl_clk_disable,
  122. .is_enabled = atl_clk_is_enabled,
  123. .recalc_rate = atl_clk_recalc_rate,
  124. .round_rate = atl_clk_round_rate,
  125. .set_rate = atl_clk_set_rate,
  126. };
  127. static void __init of_dra7_atl_clock_setup(struct device_node *node)
  128. {
  129. struct dra7_atl_desc *clk_hw = NULL;
  130. struct clk_init_data init = { NULL };
  131. const char **parent_names = NULL;
  132. const char *name;
  133. struct clk *clk;
  134. clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
  135. if (!clk_hw) {
  136. pr_err("%s: could not allocate dra7_atl_desc\n", __func__);
  137. return;
  138. }
  139. clk_hw->hw.init = &init;
  140. clk_hw->divider = 1;
  141. name = ti_dt_clk_name(node);
  142. init.name = name;
  143. init.ops = &atl_clk_ops;
  144. init.flags = CLK_IGNORE_UNUSED;
  145. init.num_parents = of_clk_get_parent_count(node);
  146. if (init.num_parents != 1) {
  147. pr_err("%s: atl clock %pOFn must have 1 parent\n", __func__,
  148. node);
  149. goto cleanup;
  150. }
  151. parent_names = kzalloc(sizeof(char *), GFP_KERNEL);
  152. if (!parent_names)
  153. goto cleanup;
  154. parent_names[0] = of_clk_get_parent_name(node, 0);
  155. init.parent_names = parent_names;
  156. clk = of_ti_clk_register(node, &clk_hw->hw, name);
  157. if (!IS_ERR(clk)) {
  158. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  159. kfree(parent_names);
  160. return;
  161. }
  162. cleanup:
  163. kfree(parent_names);
  164. kfree(clk_hw);
  165. }
  166. CLK_OF_DECLARE(dra7_atl_clock, "ti,dra7-atl-clock", of_dra7_atl_clock_setup);
  167. static int of_dra7_atl_clk_probe(struct platform_device *pdev)
  168. {
  169. struct device_node *node = pdev->dev.of_node;
  170. struct dra7_atl_clock_info *cinfo;
  171. int i;
  172. int ret = 0;
  173. if (!node)
  174. return -ENODEV;
  175. cinfo = devm_kzalloc(&pdev->dev, sizeof(*cinfo), GFP_KERNEL);
  176. if (!cinfo)
  177. return -ENOMEM;
  178. cinfo->iobase = of_iomap(node, 0);
  179. cinfo->dev = &pdev->dev;
  180. pm_runtime_enable(cinfo->dev);
  181. pm_runtime_get_sync(cinfo->dev);
  182. atl_write(cinfo, DRA7_ATL_PCLKMUX_REG(0), DRA7_ATL_PCLKMUX);
  183. for (i = 0; i < DRA7_ATL_INSTANCES; i++) {
  184. struct device_node *cfg_node;
  185. char prop[5];
  186. struct dra7_atl_desc *cdesc;
  187. struct of_phandle_args clkspec;
  188. struct clk *clk;
  189. int rc;
  190. rc = of_parse_phandle_with_args(node, "ti,provided-clocks",
  191. NULL, i, &clkspec);
  192. if (rc) {
  193. pr_err("%s: failed to lookup atl clock %d\n", __func__,
  194. i);
  195. ret = -EINVAL;
  196. goto pm_put;
  197. }
  198. clk = of_clk_get_from_provider(&clkspec);
  199. if (IS_ERR(clk)) {
  200. pr_err("%s: failed to get atl clock %d from provider\n",
  201. __func__, i);
  202. ret = PTR_ERR(clk);
  203. goto pm_put;
  204. }
  205. cdesc = to_atl_desc(__clk_get_hw(clk));
  206. cdesc->cinfo = cinfo;
  207. cdesc->id = i;
  208. /* Get configuration for the ATL instances */
  209. snprintf(prop, sizeof(prop), "atl%u", i);
  210. cfg_node = of_get_child_by_name(node, prop);
  211. if (cfg_node) {
  212. ret = of_property_read_u32(cfg_node, "bws",
  213. &cdesc->bws);
  214. ret |= of_property_read_u32(cfg_node, "aws",
  215. &cdesc->aws);
  216. if (!ret) {
  217. cdesc->valid = true;
  218. atl_write(cinfo, DRA7_ATL_BWSMUX_REG(i),
  219. cdesc->bws);
  220. atl_write(cinfo, DRA7_ATL_AWSMUX_REG(i),
  221. cdesc->aws);
  222. }
  223. of_node_put(cfg_node);
  224. }
  225. cdesc->probed = true;
  226. /*
  227. * Enable the clock if it has been asked prior to loading the
  228. * hw driver
  229. */
  230. if (cdesc->enabled)
  231. atl_clk_enable(__clk_get_hw(clk));
  232. }
  233. pm_put:
  234. pm_runtime_put_sync(cinfo->dev);
  235. return ret;
  236. }
  237. static const struct of_device_id of_dra7_atl_clk_match_tbl[] = {
  238. { .compatible = "ti,dra7-atl", },
  239. {},
  240. };
  241. static struct platform_driver dra7_atl_clk_driver = {
  242. .driver = {
  243. .name = "dra7-atl",
  244. .suppress_bind_attrs = true,
  245. .of_match_table = of_dra7_atl_clk_match_tbl,
  246. },
  247. .probe = of_dra7_atl_clk_probe,
  248. };
  249. builtin_platform_driver(dra7_atl_clk_driver);