audio-ext-clk-up.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/init.h>
  6. #include <linux/err.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/clk.h>
  10. #include <linux/clk-provider.h>
  11. #include "../../../drivers/clk/qcom/common.h"
  12. #include <linux/pinctrl/consumer.h>
  13. #include <linux/platform_device.h>
  14. #include <dt-bindings/clock/qcom,audio-ext-clk.h>
  15. #include <dsp/q6afe-v2.h>
  16. #include "audio-ext-clk-up.h"
  17. enum {
  18. AUDIO_EXT_CLK_PMI,
  19. AUDIO_EXT_CLK_LNBB2,
  20. AUDIO_EXT_CLK_LPASS,
  21. AUDIO_EXT_CLK_LPASS2,
  22. AUDIO_EXT_CLK_LPASS3,
  23. AUDIO_EXT_CLK_LPASS4,
  24. AUDIO_EXT_CLK_LPASS5,
  25. AUDIO_EXT_CLK_LPASS6,
  26. AUDIO_EXT_CLK_LPASS7,
  27. AUDIO_EXT_CLK_LPASS_CORE_HW_VOTE,
  28. AUDIO_EXT_CLK_LPASS8,
  29. AUDIO_EXT_CLK_LPASS_MAX,
  30. AUDIO_EXT_CLK_EXTERNAL_PLL = AUDIO_EXT_CLK_LPASS_MAX,
  31. AUDIO_EXT_CLK_MAX,
  32. };
  33. struct pinctrl_info {
  34. struct pinctrl *pinctrl;
  35. struct pinctrl_state *sleep;
  36. struct pinctrl_state *active;
  37. char __iomem *base;
  38. };
  39. struct audio_ext_clk {
  40. struct pinctrl_info pnctrl_info;
  41. struct clk_fixed_factor fact;
  42. };
  43. struct audio_ext_clk_priv {
  44. struct device *dev;
  45. int clk_src;
  46. struct afe_clk_set clk_cfg;
  47. struct audio_ext_clk audio_clk;
  48. const char *clk_name;
  49. uint32_t lpass_core_hwvote_client_handle;
  50. };
  51. static inline struct audio_ext_clk_priv *to_audio_clk(struct clk_hw *hw)
  52. {
  53. return container_of(hw, struct audio_ext_clk_priv, audio_clk.fact.hw);
  54. }
  55. static int audio_ext_clk_prepare(struct clk_hw *hw)
  56. {
  57. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  58. struct pinctrl_info *pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  59. int ret;
  60. if ((clk_priv->clk_src >= AUDIO_EXT_CLK_LPASS) &&
  61. (clk_priv->clk_src < AUDIO_EXT_CLK_LPASS_MAX)) {
  62. clk_priv->clk_cfg.enable = 1;
  63. ret = afe_set_lpass_clk_cfg(IDX_RSVD_3, &clk_priv->clk_cfg);
  64. if (ret < 0) {
  65. pr_err("%s afe_set_digital_codec_core_clock failed\n",
  66. __func__);
  67. return ret;
  68. }
  69. }
  70. if (pnctrl_info->pinctrl) {
  71. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  72. pnctrl_info->active);
  73. if (ret) {
  74. pr_err("%s: active state select failed with %d\n",
  75. __func__, ret);
  76. return -EIO;
  77. }
  78. }
  79. if (pnctrl_info->base)
  80. iowrite32(1, pnctrl_info->base);
  81. return 0;
  82. }
  83. static void audio_ext_clk_unprepare(struct clk_hw *hw)
  84. {
  85. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  86. struct pinctrl_info *pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  87. int ret;
  88. if (pnctrl_info->pinctrl) {
  89. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  90. pnctrl_info->sleep);
  91. if (ret) {
  92. pr_err("%s: active state select failed with %d\n",
  93. __func__, ret);
  94. return;
  95. }
  96. }
  97. if ((clk_priv->clk_src >= AUDIO_EXT_CLK_LPASS) &&
  98. (clk_priv->clk_src < AUDIO_EXT_CLK_LPASS_MAX)) {
  99. clk_priv->clk_cfg.enable = 0;
  100. ret = afe_set_lpass_clk_cfg(IDX_RSVD_3, &clk_priv->clk_cfg);
  101. if (ret < 0)
  102. pr_err("%s: afe_set_lpass_clk_cfg failed, ret = %d\n",
  103. __func__, ret);
  104. }
  105. if (pnctrl_info->base)
  106. iowrite32(0, pnctrl_info->base);
  107. }
  108. static u8 audio_ext_clk_get_parent(struct clk_hw *hw)
  109. {
  110. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  111. int num_parents = clk_hw_get_num_parents(hw);
  112. const char * const *parent_names = hw->init->parent_names;
  113. u8 i = 0, ret = hw->init->num_parents + 1;
  114. if ((clk_priv->clk_src == AUDIO_EXT_CLK_PMI) && clk_priv->clk_name) {
  115. for (i = 0; i < num_parents; i++) {
  116. if (!strcmp(parent_names[i], clk_priv->clk_name))
  117. ret = i;
  118. }
  119. pr_debug("%s: parent index = %u\n", __func__, ret);
  120. return ret;
  121. } else
  122. return 0;
  123. }
  124. static int lpass_hw_vote_prepare(struct clk_hw *hw)
  125. {
  126. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  127. int ret;
  128. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS_CORE_HW_VOTE) {
  129. ret = afe_vote_lpass_core_hw(AFE_LPASS_CORE_HW_MACRO_BLOCK,
  130. "LPASS_HW_MACRO",
  131. &clk_priv->lpass_core_hwvote_client_handle);
  132. if (ret < 0) {
  133. pr_err("%s lpass core hw vote failed %d\n",
  134. __func__, ret);
  135. return ret;
  136. }
  137. }
  138. return 0;
  139. }
  140. static void lpass_hw_vote_unprepare(struct clk_hw *hw)
  141. {
  142. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  143. int ret = 0;
  144. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS_CORE_HW_VOTE) {
  145. ret = afe_unvote_lpass_core_hw(
  146. AFE_LPASS_CORE_HW_MACRO_BLOCK,
  147. clk_priv->lpass_core_hwvote_client_handle);
  148. if (ret < 0) {
  149. pr_err("%s lpass core hw vote failed %d\n",
  150. __func__, ret);
  151. }
  152. }
  153. }
  154. static const struct clk_ops audio_ext_clk_ops = {
  155. .prepare = audio_ext_clk_prepare,
  156. .unprepare = audio_ext_clk_unprepare,
  157. .get_parent = audio_ext_clk_get_parent,
  158. };
  159. static const struct clk_ops lpass_hw_vote_ops = {
  160. .prepare = lpass_hw_vote_prepare,
  161. .unprepare = lpass_hw_vote_unprepare,
  162. };
  163. static const char * const audio_ext_pmi_div_clk[] = {
  164. "qpnp_clkdiv_1",
  165. "pms405_div_clk1",
  166. "pm6150_div_clk1",
  167. "pm6125_div_clk1",
  168. };
  169. static int audio_ext_clk_dummy_prepare(struct clk_hw *hw)
  170. {
  171. return 0;
  172. }
  173. static void audio_ext_clk_dummy_unprepare(struct clk_hw *hw)
  174. {
  175. }
  176. static const struct clk_ops audio_ext_clk_dummy_ops = {
  177. .prepare = audio_ext_clk_dummy_prepare,
  178. .unprepare = audio_ext_clk_dummy_unprepare,
  179. };
  180. static struct audio_ext_clk audio_clk_array[] = {
  181. {
  182. .pnctrl_info = {NULL},
  183. .fact = {
  184. .mult = 1,
  185. .div = 1,
  186. .hw.init = &(struct clk_init_data){
  187. .name = "audio_ext_pmi_clk",
  188. .parent_names = audio_ext_pmi_div_clk,
  189. .num_parents =
  190. ARRAY_SIZE(audio_ext_pmi_div_clk),
  191. .ops = &audio_ext_clk_ops,
  192. },
  193. },
  194. },
  195. {
  196. .pnctrl_info = {NULL},
  197. .fact = {
  198. .mult = 1,
  199. .div = 1,
  200. .hw.init = &(struct clk_init_data){
  201. .name = "audio_ext_pmi_lnbb_clk",
  202. .parent_names = (const char *[])
  203. { "ln_bb_clk2" },
  204. .num_parents = 1,
  205. .ops = &audio_ext_clk_dummy_ops,
  206. },
  207. },
  208. },
  209. {
  210. .pnctrl_info = {NULL},
  211. .fact = {
  212. .mult = 1,
  213. .div = 1,
  214. .hw.init = &(struct clk_init_data){
  215. .name = "audio_lpass_mclk",
  216. .ops = &audio_ext_clk_ops,
  217. },
  218. },
  219. },
  220. {
  221. .pnctrl_info = {NULL},
  222. .fact = {
  223. .mult = 1,
  224. .div = 1,
  225. .hw.init = &(struct clk_init_data){
  226. .name = "audio_lpass_mclk2",
  227. .ops = &audio_ext_clk_ops,
  228. },
  229. },
  230. },
  231. {
  232. .pnctrl_info = {NULL},
  233. .fact = {
  234. .mult = 1,
  235. .div = 1,
  236. .hw.init = &(struct clk_init_data){
  237. .name = "audio_lpass_mclk3",
  238. .ops = &audio_ext_clk_ops,
  239. },
  240. },
  241. },
  242. {
  243. .pnctrl_info = {NULL},
  244. .fact = {
  245. .mult = 1,
  246. .div = 1,
  247. .hw.init = &(struct clk_init_data){
  248. .name = "audio_lpass_mclk4",
  249. .ops = &audio_ext_clk_ops,
  250. },
  251. },
  252. },
  253. {
  254. .pnctrl_info = {NULL},
  255. .fact = {
  256. .mult = 1,
  257. .div = 1,
  258. .hw.init = &(struct clk_init_data){
  259. .name = "audio_lpass_mclk5",
  260. .ops = &audio_ext_clk_ops,
  261. },
  262. },
  263. },
  264. {
  265. .pnctrl_info = {NULL},
  266. .fact = {
  267. .mult = 1,
  268. .div = 1,
  269. .hw.init = &(struct clk_init_data){
  270. .name = "audio_lpass_mclk6",
  271. .ops = &audio_ext_clk_ops,
  272. },
  273. },
  274. },
  275. {
  276. .pnctrl_info = {NULL},
  277. .fact = {
  278. .mult = 1,
  279. .div = 1,
  280. .hw.init = &(struct clk_init_data){
  281. .name = "audio_lpass_mclk7",
  282. .ops = &audio_ext_clk_ops,
  283. },
  284. },
  285. },
  286. {
  287. .pnctrl_info = {NULL},
  288. .fact = {
  289. .hw.init = &(struct clk_init_data){
  290. .name = "lpass_hw_vote_clk",
  291. .ops = &lpass_hw_vote_ops,
  292. },
  293. },
  294. },
  295. {
  296. .pnctrl_info = {NULL},
  297. .fact = {
  298. .mult = 1,
  299. .div = 1,
  300. .hw.init = &(struct clk_init_data){
  301. .name = "audio_lpass_mclk8",
  302. .ops = &audio_ext_clk_ops,
  303. },
  304. },
  305. },
  306. {
  307. .pnctrl_info = {NULL},
  308. .fact = {
  309. .mult = 1,
  310. .div = 1,
  311. .hw.init = &(struct clk_init_data){
  312. .name = "audio_external_pll_clk",
  313. .ops = &audio_ext_clk_ops,
  314. },
  315. },
  316. },
  317. };
  318. static int audio_get_pinctrl(struct platform_device *pdev)
  319. {
  320. struct device *dev = &pdev->dev;
  321. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  322. struct pinctrl_info *pnctrl_info;
  323. struct pinctrl *pinctrl;
  324. int ret;
  325. u32 reg;
  326. pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  327. if (pnctrl_info->pinctrl) {
  328. dev_err(dev, "%s: already requested before\n",
  329. __func__);
  330. return -EINVAL;
  331. }
  332. pinctrl = devm_pinctrl_get(dev);
  333. if (IS_ERR_OR_NULL(pinctrl)) {
  334. dev_err(dev, "%s: Unable to get pinctrl handle\n",
  335. __func__);
  336. return -EINVAL;
  337. }
  338. pnctrl_info->pinctrl = pinctrl;
  339. /* get all state handles from Device Tree */
  340. pnctrl_info->sleep = pinctrl_lookup_state(pinctrl, "sleep");
  341. if (IS_ERR(pnctrl_info->sleep)) {
  342. dev_err(dev, "%s: could not get sleep pinstate\n",
  343. __func__);
  344. goto err;
  345. }
  346. pnctrl_info->active = pinctrl_lookup_state(pinctrl, "active");
  347. if (IS_ERR(pnctrl_info->active)) {
  348. dev_err(dev, "%s: could not get active pinstate\n",
  349. __func__);
  350. goto err;
  351. }
  352. /* Reset the TLMM pins to a default state */
  353. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  354. pnctrl_info->sleep);
  355. if (ret) {
  356. dev_err(dev, "%s: Disable TLMM pins failed with %d\n",
  357. __func__, ret);
  358. goto err;
  359. }
  360. ret = of_property_read_u32(dev->of_node, "qcom,mclk-clk-reg", &reg);
  361. if (ret < 0) {
  362. dev_dbg(dev, "%s: miss mclk reg\n", __func__);
  363. } else {
  364. pnctrl_info->base = ioremap(reg, sizeof(u32));
  365. if (pnctrl_info->base == NULL) {
  366. dev_err(dev, "%s ioremap failed\n", __func__);
  367. goto err;
  368. }
  369. }
  370. return 0;
  371. err:
  372. devm_pinctrl_put(pnctrl_info->pinctrl);
  373. return -EINVAL;
  374. }
  375. static int audio_put_pinctrl(struct platform_device *pdev)
  376. {
  377. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  378. struct pinctrl_info *pnctrl_info = NULL;
  379. pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  380. if (pnctrl_info && pnctrl_info->pinctrl) {
  381. devm_pinctrl_put(pnctrl_info->pinctrl);
  382. pnctrl_info->pinctrl = NULL;
  383. }
  384. return 0;
  385. }
  386. static int audio_get_clk_data(struct platform_device *pdev)
  387. {
  388. int ret;
  389. struct clk *audio_clk;
  390. struct clk_hw *clkhw;
  391. struct clk_onecell_data *clk_data;
  392. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  393. clk_data = devm_kzalloc(&pdev->dev, sizeof(*clk_data), GFP_KERNEL);
  394. if (!clk_data)
  395. return -ENOMEM;
  396. clk_data->clk_num = 1;
  397. clk_data->clks = devm_kzalloc(&pdev->dev,
  398. sizeof(struct clk *),
  399. GFP_KERNEL);
  400. if (!clk_data->clks)
  401. return -ENOMEM;
  402. clkhw = &clk_priv->audio_clk.fact.hw;
  403. audio_clk = devm_clk_register(&pdev->dev, clkhw);
  404. if (IS_ERR(audio_clk)) {
  405. dev_err(&pdev->dev,
  406. "%s: clock register failed for clk_src = %d\\n",
  407. __func__, clk_priv->clk_src);
  408. ret = PTR_ERR(audio_clk);
  409. return ret;
  410. }
  411. clk_data->clks[0] = audio_clk;
  412. ret = of_clk_add_provider(pdev->dev.of_node,
  413. of_clk_src_onecell_get, clk_data);
  414. if (ret)
  415. dev_err(&pdev->dev, "%s: clock add failed for clk_src = %d\n",
  416. __func__, clk_priv->clk_src);
  417. return ret;
  418. }
  419. static int audio_ref_clk_probe(struct platform_device *pdev)
  420. {
  421. int ret;
  422. struct audio_ext_clk_priv *clk_priv;
  423. u32 clk_freq = 0, clk_id = 0, clk_src = 0, use_pinctrl = 0;
  424. clk_priv = devm_kzalloc(&pdev->dev, sizeof(*clk_priv), GFP_KERNEL);
  425. if (!clk_priv)
  426. return -ENOMEM;
  427. ret = of_property_read_u32(pdev->dev.of_node,
  428. "qcom,codec-ext-clk-src",
  429. &clk_src);
  430. if (ret) {
  431. dev_err(&pdev->dev, "%s: could not get clk source, ret = %d\n",
  432. __func__, ret);
  433. return ret;
  434. }
  435. if (clk_src >= AUDIO_EXT_CLK_MAX) {
  436. dev_err(&pdev->dev, "%s: Invalid clk source = %d\n",
  437. __func__, clk_src);
  438. return -EINVAL;
  439. }
  440. clk_priv->clk_name = NULL;
  441. clk_priv->clk_src = clk_src;
  442. memcpy(&clk_priv->audio_clk, &audio_clk_array[clk_src],
  443. sizeof(struct audio_ext_clk));
  444. /* Init lpass clk default values */
  445. clk_priv->clk_cfg.clk_set_minor_version =
  446. Q6AFE_LPASS_CLK_CONFIG_API_VERSION;
  447. clk_priv->clk_cfg.clk_id = Q6AFE_LPASS_CLK_ID_SPEAKER_I2S_OSR;
  448. clk_priv->clk_cfg.clk_freq_in_hz = Q6AFE_LPASS_OSR_CLK_9_P600_MHZ;
  449. clk_priv->clk_cfg.clk_attri = Q6AFE_LPASS_CLK_ATTRIBUTE_COUPLE_NO;
  450. ret = of_property_read_u32(pdev->dev.of_node,
  451. "qcom,codec-lpass-ext-clk-freq",
  452. &clk_freq);
  453. if (!ret)
  454. clk_priv->clk_cfg.clk_freq_in_hz = clk_freq;
  455. ret = of_property_read_u32(pdev->dev.of_node,
  456. "qcom,codec-lpass-clk-id",
  457. &clk_id);
  458. if (!ret)
  459. clk_priv->clk_cfg.clk_id = clk_id;
  460. dev_dbg(&pdev->dev, "%s: ext-clk freq: %d, lpass clk_id: %d, clk_src: %d\n",
  461. __func__, clk_priv->clk_cfg.clk_freq_in_hz,
  462. clk_priv->clk_cfg.clk_id, clk_priv->clk_src);
  463. platform_set_drvdata(pdev, clk_priv);
  464. ret = of_property_read_string(pdev->dev.of_node, "pmic-clock-names",
  465. &clk_priv->clk_name);
  466. if (ret)
  467. dev_dbg(&pdev->dev, "%s: could not find pmic clock names\n",
  468. __func__);
  469. /*
  470. * property qcom,use-pinctrl to be defined in DTSI to val 1
  471. * for clock nodes using pinctrl
  472. */
  473. of_property_read_u32(pdev->dev.of_node, "qcom,use-pinctrl",
  474. &use_pinctrl);
  475. dev_dbg(&pdev->dev, "%s: use-pinctrl : %d\n",
  476. __func__, use_pinctrl);
  477. if (use_pinctrl) {
  478. ret = audio_get_pinctrl(pdev);
  479. if (ret) {
  480. dev_err(&pdev->dev, "%s: Parsing PMI pinctrl failed\n",
  481. __func__);
  482. return ret;
  483. }
  484. }
  485. ret = audio_get_clk_data(pdev);
  486. if (ret) {
  487. dev_err(&pdev->dev, "%s: clk_init is failed\n",
  488. __func__);
  489. audio_put_pinctrl(pdev);
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. static int audio_ref_clk_remove(struct platform_device *pdev)
  495. {
  496. audio_put_pinctrl(pdev);
  497. return 0;
  498. }
  499. static const struct of_device_id audio_ref_clk_match[] = {
  500. {.compatible = "qcom,audio-ref-clk"},
  501. {}
  502. };
  503. MODULE_DEVICE_TABLE(of, audio_ref_clk_match);
  504. static struct platform_driver audio_ref_clk_driver = {
  505. .driver = {
  506. .name = "audio-ref-clk",
  507. .owner = THIS_MODULE,
  508. .of_match_table = audio_ref_clk_match,
  509. .suppress_bind_attrs = true,
  510. },
  511. .probe = audio_ref_clk_probe,
  512. .remove = audio_ref_clk_remove,
  513. };
  514. int audio_ref_clk_platform_init(void)
  515. {
  516. return platform_driver_register(&audio_ref_clk_driver);
  517. }
  518. void audio_ref_clk_platform_exit(void)
  519. {
  520. platform_driver_unregister(&audio_ref_clk_driver);
  521. }
  522. MODULE_DESCRIPTION("Audio Ref Up Clock module platform driver");
  523. MODULE_LICENSE("GPL v2");