audio-ext-clk-up.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-2018, 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 <dsp/q6core.h>
  17. #include "audio-ext-clk-up.h"
  18. enum {
  19. AUDIO_EXT_CLK_PMI,
  20. AUDIO_EXT_CLK_LNBB2,
  21. AUDIO_EXT_CLK_LPASS,
  22. AUDIO_EXT_CLK_LPASS2,
  23. AUDIO_EXT_CLK_LPASS3,
  24. AUDIO_EXT_CLK_LPASS4,
  25. AUDIO_EXT_CLK_LPASS5,
  26. AUDIO_EXT_CLK_LPASS6,
  27. AUDIO_EXT_CLK_LPASS7,
  28. AUDIO_EXT_CLK_LPASS_NPA_RSC_ISLAND,
  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 npa_rsc_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_npa_rsc_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_NPA_RSC_ISLAND) &&
  129. (clk_priv->clk_src < AUDIO_EXT_CLK_LPASS_MAX)) {
  130. if (q6core_get_avcs_api_version_per_service(
  131. APRV2_IDS_SERVICE_ID_ADSP_CORE_V) >= AVCS_API_VERSION_V4) {
  132. ret = q6core_request_island_transition(
  133. clk_priv->npa_rsc_client_handle, false);
  134. if (ret < 0) {
  135. pr_err("%s q6core_request_island_transition failed %d\n",
  136. __func__, ret);
  137. return ret;
  138. }
  139. }
  140. }
  141. return 0;
  142. }
  143. static void lpass_npa_rsc_unprepare(struct clk_hw *hw)
  144. {
  145. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  146. int ret = 0;
  147. if ((clk_priv->clk_src >= AUDIO_EXT_CLK_LPASS_NPA_RSC_ISLAND) &&
  148. (clk_priv->clk_src < AUDIO_EXT_CLK_LPASS_MAX)) {
  149. if (q6core_get_avcs_api_version_per_service(
  150. APRV2_IDS_SERVICE_ID_ADSP_CORE_V) >= AVCS_API_VERSION_V4) {
  151. ret = q6core_request_island_transition(
  152. clk_priv->npa_rsc_client_handle, true);
  153. if (ret < 0) {
  154. pr_err("%s q6core_request_island_transition failed %d\n",
  155. __func__, ret);
  156. }
  157. }
  158. }
  159. }
  160. static const struct clk_ops audio_ext_clk_ops = {
  161. .prepare = audio_ext_clk_prepare,
  162. .unprepare = audio_ext_clk_unprepare,
  163. .get_parent = audio_ext_clk_get_parent,
  164. };
  165. static const struct clk_ops lpass_npa_rsc_ops = {
  166. .prepare = lpass_npa_rsc_prepare,
  167. .unprepare = lpass_npa_rsc_unprepare,
  168. };
  169. static const char * const audio_ext_pmi_div_clk[] = {
  170. "qpnp_clkdiv_1",
  171. "pms405_div_clk1",
  172. "pm6150_div_clk1",
  173. };
  174. static int audio_ext_clk_dummy_prepare(struct clk_hw *hw)
  175. {
  176. return 0;
  177. }
  178. static void audio_ext_clk_dummy_unprepare(struct clk_hw *hw)
  179. {
  180. }
  181. static const struct clk_ops audio_ext_clk_dummy_ops = {
  182. .prepare = audio_ext_clk_dummy_prepare,
  183. .unprepare = audio_ext_clk_dummy_unprepare,
  184. };
  185. static struct audio_ext_clk audio_clk_array[] = {
  186. {
  187. .pnctrl_info = {NULL},
  188. .fact = {
  189. .mult = 1,
  190. .div = 1,
  191. .hw.init = &(struct clk_init_data){
  192. .name = "audio_ext_pmi_clk",
  193. .parent_names = audio_ext_pmi_div_clk,
  194. .num_parents =
  195. ARRAY_SIZE(audio_ext_pmi_div_clk),
  196. .ops = &audio_ext_clk_ops,
  197. },
  198. },
  199. },
  200. {
  201. .pnctrl_info = {NULL},
  202. .fact = {
  203. .mult = 1,
  204. .div = 1,
  205. .hw.init = &(struct clk_init_data){
  206. .name = "audio_ext_pmi_lnbb_clk",
  207. .parent_names = (const char *[])
  208. { "ln_bb_clk2" },
  209. .num_parents = 1,
  210. .ops = &audio_ext_clk_dummy_ops,
  211. },
  212. },
  213. },
  214. {
  215. .pnctrl_info = {NULL},
  216. .fact = {
  217. .mult = 1,
  218. .div = 1,
  219. .hw.init = &(struct clk_init_data){
  220. .name = "audio_lpass_mclk",
  221. .ops = &audio_ext_clk_ops,
  222. },
  223. },
  224. },
  225. {
  226. .pnctrl_info = {NULL},
  227. .fact = {
  228. .mult = 1,
  229. .div = 1,
  230. .hw.init = &(struct clk_init_data){
  231. .name = "audio_lpass_mclk2",
  232. .ops = &audio_ext_clk_ops,
  233. },
  234. },
  235. },
  236. {
  237. .pnctrl_info = {NULL},
  238. .fact = {
  239. .mult = 1,
  240. .div = 1,
  241. .hw.init = &(struct clk_init_data){
  242. .name = "audio_lpass_mclk3",
  243. .ops = &audio_ext_clk_ops,
  244. },
  245. },
  246. },
  247. {
  248. .pnctrl_info = {NULL},
  249. .fact = {
  250. .mult = 1,
  251. .div = 1,
  252. .hw.init = &(struct clk_init_data){
  253. .name = "audio_lpass_mclk4",
  254. .ops = &audio_ext_clk_ops,
  255. },
  256. },
  257. },
  258. {
  259. .pnctrl_info = {NULL},
  260. .fact = {
  261. .mult = 1,
  262. .div = 1,
  263. .hw.init = &(struct clk_init_data){
  264. .name = "audio_lpass_mclk5",
  265. .ops = &audio_ext_clk_ops,
  266. },
  267. },
  268. },
  269. {
  270. .pnctrl_info = {NULL},
  271. .fact = {
  272. .mult = 1,
  273. .div = 1,
  274. .hw.init = &(struct clk_init_data){
  275. .name = "audio_lpass_mclk6",
  276. .ops = &audio_ext_clk_ops,
  277. },
  278. },
  279. },
  280. {
  281. .pnctrl_info = {NULL},
  282. .fact = {
  283. .mult = 1,
  284. .div = 1,
  285. .hw.init = &(struct clk_init_data){
  286. .name = "audio_lpass_mclk7",
  287. .ops = &audio_ext_clk_ops,
  288. },
  289. },
  290. },
  291. {
  292. .pnctrl_info = {NULL},
  293. .fact = {
  294. .hw.init = &(struct clk_init_data){
  295. .name = "lpass_npa_rsc_island_clk",
  296. .ops = &lpass_npa_rsc_ops,
  297. },
  298. },
  299. },
  300. {
  301. .pnctrl_info = {NULL},
  302. .fact = {
  303. .mult = 1,
  304. .div = 1,
  305. .hw.init = &(struct clk_init_data){
  306. .name = "audio_external_pll_clk",
  307. .ops = &audio_ext_clk_ops,
  308. },
  309. },
  310. },
  311. };
  312. static int audio_get_pinctrl(struct platform_device *pdev)
  313. {
  314. struct device *dev = &pdev->dev;
  315. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  316. struct pinctrl_info *pnctrl_info;
  317. struct pinctrl *pinctrl;
  318. int ret;
  319. u32 reg;
  320. pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  321. if (pnctrl_info->pinctrl) {
  322. dev_err(dev, "%s: already requested before\n",
  323. __func__);
  324. return -EINVAL;
  325. }
  326. pinctrl = devm_pinctrl_get(dev);
  327. if (IS_ERR_OR_NULL(pinctrl)) {
  328. dev_err(dev, "%s: Unable to get pinctrl handle\n",
  329. __func__);
  330. return -EINVAL;
  331. }
  332. pnctrl_info->pinctrl = pinctrl;
  333. /* get all state handles from Device Tree */
  334. pnctrl_info->sleep = pinctrl_lookup_state(pinctrl, "sleep");
  335. if (IS_ERR(pnctrl_info->sleep)) {
  336. dev_err(dev, "%s: could not get sleep pinstate\n",
  337. __func__);
  338. goto err;
  339. }
  340. pnctrl_info->active = pinctrl_lookup_state(pinctrl, "active");
  341. if (IS_ERR(pnctrl_info->active)) {
  342. dev_err(dev, "%s: could not get active pinstate\n",
  343. __func__);
  344. goto err;
  345. }
  346. /* Reset the TLMM pins to a default state */
  347. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  348. pnctrl_info->sleep);
  349. if (ret) {
  350. dev_err(dev, "%s: Disable TLMM pins failed with %d\n",
  351. __func__, ret);
  352. goto err;
  353. }
  354. ret = of_property_read_u32(dev->of_node, "qcom,mclk-clk-reg", &reg);
  355. if (ret < 0) {
  356. dev_dbg(dev, "%s: miss mclk reg\n", __func__);
  357. } else {
  358. pnctrl_info->base = ioremap(reg, sizeof(u32));
  359. if (pnctrl_info->base == NULL) {
  360. dev_err(dev, "%s ioremap failed\n", __func__);
  361. goto err;
  362. }
  363. }
  364. return 0;
  365. err:
  366. devm_pinctrl_put(pnctrl_info->pinctrl);
  367. return -EINVAL;
  368. }
  369. static int audio_put_pinctrl(struct platform_device *pdev)
  370. {
  371. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  372. struct pinctrl_info *pnctrl_info = NULL;
  373. pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  374. if (pnctrl_info && pnctrl_info->pinctrl) {
  375. devm_pinctrl_put(pnctrl_info->pinctrl);
  376. pnctrl_info->pinctrl = NULL;
  377. }
  378. return 0;
  379. }
  380. static int audio_get_clk_data(struct platform_device *pdev)
  381. {
  382. int ret;
  383. struct clk *audio_clk;
  384. struct clk_hw *clkhw;
  385. struct clk_onecell_data *clk_data;
  386. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  387. clk_data = devm_kzalloc(&pdev->dev, sizeof(*clk_data), GFP_KERNEL);
  388. if (!clk_data)
  389. return -ENOMEM;
  390. clk_data->clk_num = 1;
  391. clk_data->clks = devm_kzalloc(&pdev->dev,
  392. sizeof(struct clk *),
  393. GFP_KERNEL);
  394. if (!clk_data->clks)
  395. return -ENOMEM;
  396. clkhw = &clk_priv->audio_clk.fact.hw;
  397. audio_clk = devm_clk_register(&pdev->dev, clkhw);
  398. if (IS_ERR(audio_clk)) {
  399. dev_err(&pdev->dev,
  400. "%s: clock register failed for clk_src = %d\\n",
  401. __func__, clk_priv->clk_src);
  402. ret = PTR_ERR(audio_clk);
  403. return ret;
  404. }
  405. clk_data->clks[0] = audio_clk;
  406. ret = of_clk_add_provider(pdev->dev.of_node,
  407. of_clk_src_onecell_get, clk_data);
  408. if (ret)
  409. dev_err(&pdev->dev, "%s: clock add failed for clk_src = %d\n",
  410. __func__, clk_priv->clk_src);
  411. return ret;
  412. }
  413. static int audio_ref_clk_probe(struct platform_device *pdev)
  414. {
  415. int ret;
  416. struct audio_ext_clk_priv *clk_priv;
  417. u32 clk_freq = 0, clk_id = 0, clk_src = 0, use_pinctrl = 0;
  418. clk_priv = devm_kzalloc(&pdev->dev, sizeof(*clk_priv), GFP_KERNEL);
  419. if (!clk_priv)
  420. return -ENOMEM;
  421. ret = of_property_read_u32(pdev->dev.of_node,
  422. "qcom,codec-ext-clk-src",
  423. &clk_src);
  424. if (ret) {
  425. dev_err(&pdev->dev, "%s: could not get clk source, ret = %d\n",
  426. __func__, ret);
  427. return ret;
  428. }
  429. if (clk_src >= AUDIO_EXT_CLK_MAX) {
  430. dev_err(&pdev->dev, "%s: Invalid clk source = %d\n",
  431. __func__, clk_src);
  432. return -EINVAL;
  433. }
  434. clk_priv->clk_name = NULL;
  435. clk_priv->clk_src = clk_src;
  436. memcpy(&clk_priv->audio_clk, &audio_clk_array[clk_src],
  437. sizeof(struct audio_ext_clk));
  438. /* Init lpass clk default values */
  439. clk_priv->clk_cfg.clk_set_minor_version =
  440. Q6AFE_LPASS_CLK_CONFIG_API_VERSION;
  441. clk_priv->clk_cfg.clk_id = Q6AFE_LPASS_CLK_ID_SPEAKER_I2S_OSR;
  442. clk_priv->clk_cfg.clk_freq_in_hz = Q6AFE_LPASS_OSR_CLK_9_P600_MHZ;
  443. clk_priv->clk_cfg.clk_attri = Q6AFE_LPASS_CLK_ATTRIBUTE_COUPLE_NO;
  444. ret = of_property_read_u32(pdev->dev.of_node,
  445. "qcom,codec-lpass-ext-clk-freq",
  446. &clk_freq);
  447. if (!ret)
  448. clk_priv->clk_cfg.clk_freq_in_hz = clk_freq;
  449. ret = of_property_read_u32(pdev->dev.of_node,
  450. "qcom,codec-lpass-clk-id",
  451. &clk_id);
  452. if (!ret)
  453. clk_priv->clk_cfg.clk_id = clk_id;
  454. dev_dbg(&pdev->dev, "%s: ext-clk freq: %d, lpass clk_id: %d, clk_src: %d\n",
  455. __func__, clk_priv->clk_cfg.clk_freq_in_hz,
  456. clk_priv->clk_cfg.clk_id, clk_priv->clk_src);
  457. platform_set_drvdata(pdev, clk_priv);
  458. ret = of_property_read_string(pdev->dev.of_node, "pmic-clock-names",
  459. &clk_priv->clk_name);
  460. if (ret)
  461. dev_dbg(&pdev->dev, "%s: could not find pmic clock names\n",
  462. __func__);
  463. /*
  464. * property qcom,use-pinctrl to be defined in DTSI to val 1
  465. * for clock nodes using pinctrl
  466. */
  467. of_property_read_u32(pdev->dev.of_node, "qcom,use-pinctrl",
  468. &use_pinctrl);
  469. dev_dbg(&pdev->dev, "%s: use-pinctrl : %d\n",
  470. __func__, use_pinctrl);
  471. if (use_pinctrl) {
  472. ret = audio_get_pinctrl(pdev);
  473. if (ret) {
  474. dev_err(&pdev->dev, "%s: Parsing PMI pinctrl failed\n",
  475. __func__);
  476. return ret;
  477. }
  478. }
  479. ret = audio_get_clk_data(pdev);
  480. if (ret) {
  481. dev_err(&pdev->dev, "%s: clk_init is failed\n",
  482. __func__);
  483. audio_put_pinctrl(pdev);
  484. return ret;
  485. }
  486. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS_NPA_RSC_ISLAND) {
  487. if (q6core_get_avcs_api_version_per_service(
  488. APRV2_IDS_SERVICE_ID_ADSP_CORE_V) >= AVCS_API_VERSION_V4) {
  489. ret = q6core_create_lpass_npa_client(
  490. AVCS_SLEEP_NODE_ISLAND_TRANSITION_RESOURCE_ID,
  491. "lpass_npa_rsc_mgr",
  492. &clk_priv->npa_rsc_client_handle);
  493. if (ret) {
  494. dev_err(&pdev->dev, "%s: q6core_create_lpass_npa_client is failed %d\n",
  495. __func__, ret);
  496. audio_put_pinctrl(pdev);
  497. return ret;
  498. }
  499. }
  500. }
  501. return 0;
  502. }
  503. static int audio_ref_clk_remove(struct platform_device *pdev)
  504. {
  505. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  506. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS_NPA_RSC_ISLAND) {
  507. if (q6core_get_avcs_api_version_per_service(
  508. APRV2_IDS_SERVICE_ID_ADSP_CORE_V) >= AVCS_API_VERSION_V4)
  509. q6core_destroy_lpass_npa_client(
  510. clk_priv->npa_rsc_client_handle);
  511. }
  512. audio_put_pinctrl(pdev);
  513. return 0;
  514. }
  515. static const struct of_device_id audio_ref_clk_match[] = {
  516. {.compatible = "qcom,audio-ref-clk"},
  517. {}
  518. };
  519. MODULE_DEVICE_TABLE(of, audio_ref_clk_match);
  520. static struct platform_driver audio_ref_clk_driver = {
  521. .driver = {
  522. .name = "audio-ref-clk",
  523. .owner = THIS_MODULE,
  524. .of_match_table = audio_ref_clk_match,
  525. },
  526. .probe = audio_ref_clk_probe,
  527. .remove = audio_ref_clk_remove,
  528. };
  529. int audio_ref_clk_platform_init(void)
  530. {
  531. return platform_driver_register(&audio_ref_clk_driver);
  532. }
  533. void audio_ref_clk_platform_exit(void)
  534. {
  535. platform_driver_unregister(&audio_ref_clk_driver);
  536. }
  537. MODULE_DESCRIPTION("Audio Ref Up Clock module platform driver");
  538. MODULE_LICENSE("GPL v2");