clk-dra7-atl.c 7.7 KB

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