audio-ext-clk-up.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include "../../../drivers/clk/qcom/common.h"
  20. #include <linux/platform_device.h>
  21. #include <dt-bindings/clock/qcom,audio-ext-clk.h>
  22. #include <dsp/q6afe-v2.h>
  23. #include "audio-ext-clk-up.h"
  24. enum {
  25. AUDIO_EXT_CLK_PMI,
  26. AUDIO_EXT_CLK_LNBB2,
  27. AUDIO_EXT_CLK_LPASS,
  28. AUDIO_EXT_CLK_MAX,
  29. };
  30. struct pinctrl_info {
  31. struct pinctrl *pinctrl;
  32. struct pinctrl_state *sleep;
  33. struct pinctrl_state *active;
  34. char __iomem *base;
  35. };
  36. struct audio_ext_clk {
  37. struct pinctrl_info pnctrl_info;
  38. struct clk_fixed_factor fact;
  39. };
  40. struct audio_ext_clk_priv {
  41. struct device *dev;
  42. int clk_src;
  43. struct afe_clk_set clk_cfg;
  44. struct audio_ext_clk audio_clk;
  45. };
  46. static inline struct audio_ext_clk_priv *to_audio_clk(struct clk_hw *hw)
  47. {
  48. return container_of(hw, struct audio_ext_clk_priv, audio_clk.fact.hw);
  49. }
  50. static int audio_ext_clk_prepare(struct clk_hw *hw)
  51. {
  52. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  53. struct pinctrl_info *pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  54. int ret;
  55. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS) {
  56. clk_priv->clk_cfg.enable = 1;
  57. ret = afe_set_lpass_clk_cfg(IDX_RSVD_3, &clk_priv->clk_cfg);
  58. if (ret < 0) {
  59. pr_err("%s afe_set_digital_codec_core_clock failed\n",
  60. __func__);
  61. return ret;
  62. }
  63. }
  64. if (pnctrl_info->pinctrl) {
  65. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  66. pnctrl_info->active);
  67. if (ret) {
  68. pr_err("%s: active state select failed with %d\n",
  69. __func__, ret);
  70. return -EIO;
  71. }
  72. }
  73. if (pnctrl_info->base)
  74. iowrite32(1, pnctrl_info->base);
  75. return 0;
  76. }
  77. static void audio_ext_clk_unprepare(struct clk_hw *hw)
  78. {
  79. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  80. struct pinctrl_info *pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  81. int ret;
  82. if (pnctrl_info->pinctrl) {
  83. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  84. pnctrl_info->sleep);
  85. if (ret) {
  86. pr_err("%s: active state select failed with %d\n",
  87. __func__, ret);
  88. return;
  89. }
  90. }
  91. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS) {
  92. clk_priv->clk_cfg.enable = 0;
  93. ret = afe_set_lpass_clk_cfg(IDX_RSVD_3, &clk_priv->clk_cfg);
  94. if (ret < 0)
  95. pr_err("%s: afe_set_lpass_clk_cfg failed, ret = %d\n",
  96. __func__, ret);
  97. }
  98. if (pnctrl_info->base)
  99. iowrite32(0, pnctrl_info->base);
  100. }
  101. static const struct clk_ops audio_ext_clk_ops = {
  102. .prepare = audio_ext_clk_prepare,
  103. .unprepare = audio_ext_clk_unprepare,
  104. };
  105. static struct audio_ext_clk audio_clk_array[] = {
  106. {
  107. .pnctrl_info = {NULL},
  108. .fact = {
  109. .mult = 1,
  110. .div = 1,
  111. .hw.init = &(struct clk_init_data){
  112. .name = "audio_ext_pmi_clk",
  113. .parent_names = (const char *[])
  114. { "qpnp_clkdiv_1" },
  115. .num_parents = 1,
  116. .ops = &audio_ext_clk_ops,
  117. },
  118. },
  119. },
  120. {
  121. .pnctrl_info = {NULL},
  122. .fact = {
  123. .mult = 1,
  124. .div = 1,
  125. .hw.init = &(struct clk_init_data){
  126. .name = "audio_ext_pmi_lnbb_clk",
  127. .parent_names = (const char *[])
  128. { "ln_bb_clk2" },
  129. .num_parents = 1,
  130. .ops = &clk_dummy_ops,
  131. },
  132. },
  133. },
  134. {
  135. .pnctrl_info = {NULL},
  136. .fact = {
  137. .mult = 1,
  138. .div = 1,
  139. .hw.init = &(struct clk_init_data){
  140. .name = "audio_lpass_mclk",
  141. .ops = &audio_ext_clk_ops,
  142. },
  143. },
  144. },
  145. };
  146. static int audio_get_pinctrl(struct platform_device *pdev)
  147. {
  148. struct device *dev = &pdev->dev;
  149. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  150. struct pinctrl_info *pnctrl_info;
  151. struct pinctrl *pinctrl;
  152. int ret;
  153. u32 reg;
  154. if (clk_priv->clk_src == AUDIO_EXT_CLK_LNBB2) {
  155. dev_dbg(dev, "%s no pinctrl for clk_src = %d\n",
  156. __func__, clk_priv->clk_src);
  157. return 0;
  158. }
  159. pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  160. if (pnctrl_info->pinctrl) {
  161. dev_err(dev, "%s: already requested before\n",
  162. __func__);
  163. return -EINVAL;
  164. }
  165. pinctrl = devm_pinctrl_get(dev);
  166. if (IS_ERR_OR_NULL(pinctrl)) {
  167. dev_err(dev, "%s: Unable to get pinctrl handle\n",
  168. __func__);
  169. return -EINVAL;
  170. }
  171. pnctrl_info->pinctrl = pinctrl;
  172. /* get all state handles from Device Tree */
  173. pnctrl_info->sleep = pinctrl_lookup_state(pinctrl, "sleep");
  174. if (IS_ERR(pnctrl_info->sleep)) {
  175. dev_err(dev, "%s: could not get sleep pinstate\n",
  176. __func__);
  177. goto err;
  178. }
  179. pnctrl_info->active = pinctrl_lookup_state(pinctrl, "active");
  180. if (IS_ERR(pnctrl_info->active)) {
  181. dev_err(dev, "%s: could not get active pinstate\n",
  182. __func__);
  183. goto err;
  184. }
  185. /* Reset the TLMM pins to a default state */
  186. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  187. pnctrl_info->sleep);
  188. if (ret) {
  189. dev_err(dev, "%s: Disable TLMM pins failed with %d\n",
  190. __func__, ret);
  191. goto err;
  192. }
  193. ret = of_property_read_u32(dev->of_node, "qcom,mclk-clk-reg", &reg);
  194. if (ret < 0) {
  195. dev_dbg(dev, "%s: miss mclk reg\n", __func__);
  196. } else {
  197. pnctrl_info->base = ioremap(reg, sizeof(u32));
  198. if (pnctrl_info->base == NULL) {
  199. dev_err(dev, "%s ioremap failed\n", __func__);
  200. goto err;
  201. }
  202. }
  203. return 0;
  204. err:
  205. devm_pinctrl_put(pnctrl_info->pinctrl);
  206. return -EINVAL;
  207. }
  208. static int audio_put_pinctrl(struct platform_device *pdev)
  209. {
  210. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  211. struct pinctrl_info *pnctrl_info = NULL;
  212. pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  213. if (pnctrl_info && pnctrl_info->pinctrl) {
  214. devm_pinctrl_put(pnctrl_info->pinctrl);
  215. pnctrl_info->pinctrl = NULL;
  216. }
  217. return 0;
  218. }
  219. static int audio_get_clk_data(struct platform_device *pdev)
  220. {
  221. int ret;
  222. struct clk *audio_clk;
  223. struct clk_hw *clkhw;
  224. struct clk_onecell_data *clk_data;
  225. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  226. clk_data = devm_kzalloc(&pdev->dev, sizeof(*clk_data), GFP_KERNEL);
  227. if (!clk_data)
  228. return -ENOMEM;
  229. clk_data->clk_num = 1;
  230. clk_data->clks = devm_kzalloc(&pdev->dev,
  231. sizeof(struct clk *),
  232. GFP_KERNEL);
  233. if (!clk_data->clks)
  234. return -ENOMEM;
  235. clkhw = &clk_priv->audio_clk.fact.hw;
  236. audio_clk = devm_clk_register(&pdev->dev, clkhw);
  237. if (IS_ERR(audio_clk)) {
  238. dev_err(&pdev->dev,
  239. "%s: clock register failed for clk_src = %d\\n",
  240. __func__, clk_priv->clk_src);
  241. ret = PTR_ERR(audio_clk);
  242. return ret;
  243. }
  244. clk_data->clks[0] = audio_clk;
  245. ret = of_clk_add_provider(pdev->dev.of_node,
  246. of_clk_src_onecell_get, clk_data);
  247. if (ret)
  248. dev_err(&pdev->dev, "%s: clock add failed for clk_src = %d\n",
  249. __func__, clk_priv->clk_src);
  250. return ret;
  251. }
  252. static int audio_ref_clk_probe(struct platform_device *pdev)
  253. {
  254. int ret;
  255. struct audio_ext_clk_priv *clk_priv;
  256. u32 clk_freq = 0, clk_id = 0, clk_src = 0;
  257. clk_priv = devm_kzalloc(&pdev->dev, sizeof(*clk_priv), GFP_KERNEL);
  258. if (!clk_priv)
  259. return -ENOMEM;
  260. ret = of_property_read_u32(pdev->dev.of_node,
  261. "qcom,codec-ext-clk-src",
  262. &clk_src);
  263. if (ret) {
  264. dev_err(&pdev->dev, "%s: could not get clk source, ret = %d\n",
  265. __func__, ret);
  266. return ret;
  267. }
  268. if (clk_src >= AUDIO_EXT_CLK_MAX) {
  269. dev_err(&pdev->dev, "%s: Invalid clk source = %d\n",
  270. __func__, clk_src);
  271. return -EINVAL;
  272. }
  273. clk_priv->clk_src = clk_src;
  274. memcpy(&clk_priv->audio_clk, &audio_clk_array[clk_src],
  275. sizeof(struct audio_ext_clk));
  276. /* Init lpass clk default values */
  277. clk_priv->clk_cfg.clk_set_minor_version =
  278. Q6AFE_LPASS_CLK_CONFIG_API_VERSION;
  279. clk_priv->clk_cfg.clk_id = Q6AFE_LPASS_CLK_ID_SPEAKER_I2S_OSR;
  280. clk_priv->clk_cfg.clk_freq_in_hz = Q6AFE_LPASS_OSR_CLK_9_P600_MHZ;
  281. clk_priv->clk_cfg.clk_attri = Q6AFE_LPASS_CLK_ATTRIBUTE_COUPLE_NO;
  282. ret = of_property_read_u32(pdev->dev.of_node,
  283. "qcom,codec-lpass-ext-clk-freq",
  284. &clk_freq);
  285. if (!ret)
  286. clk_priv->clk_cfg.clk_freq_in_hz = clk_freq;
  287. ret = of_property_read_u32(pdev->dev.of_node,
  288. "qcom,codec-lpass-clk-id",
  289. &clk_id);
  290. if (!ret)
  291. clk_priv->clk_cfg.clk_id = clk_id;
  292. dev_dbg(&pdev->dev, "%s: ext-clk freq: %d, lpass clk_id: %d, clk_src: %d\n",
  293. __func__, clk_priv->clk_cfg.clk_freq_in_hz,
  294. clk_priv->clk_cfg.clk_id, clk_priv->clk_src);
  295. platform_set_drvdata(pdev, clk_priv);
  296. ret = audio_get_pinctrl(pdev);
  297. if (ret) {
  298. dev_err(&pdev->dev, "%s: Parsing PMI pinctrl failed\n",
  299. __func__);
  300. return ret;
  301. }
  302. ret = audio_get_clk_data(pdev);
  303. if (ret) {
  304. dev_err(&pdev->dev, "%s: clk_init is failed\n",
  305. __func__);
  306. audio_put_pinctrl(pdev);
  307. return ret;
  308. }
  309. return 0;
  310. }
  311. static int audio_ref_clk_remove(struct platform_device *pdev)
  312. {
  313. audio_put_pinctrl(pdev);
  314. return 0;
  315. }
  316. static const struct of_device_id audio_ref_clk_match[] = {
  317. {.compatible = "qcom,audio-ref-clk"},
  318. {}
  319. };
  320. MODULE_DEVICE_TABLE(of, audio_ref_clk_match);
  321. static struct platform_driver audio_ref_clk_driver = {
  322. .driver = {
  323. .name = "audio-ref-clk",
  324. .owner = THIS_MODULE,
  325. .of_match_table = audio_ref_clk_match,
  326. },
  327. .probe = audio_ref_clk_probe,
  328. .remove = audio_ref_clk_remove,
  329. };
  330. int audio_ref_clk_platform_init(void)
  331. {
  332. return platform_driver_register(&audio_ref_clk_driver);
  333. }
  334. void audio_ref_clk_platform_exit(void)
  335. {
  336. platform_driver_unregister(&audio_ref_clk_driver);
  337. }
  338. MODULE_DESCRIPTION("Audio Ref Up Clock module platform driver");
  339. MODULE_LICENSE("GPL v2");