clk-dra7-atl.c 7.5 KB

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