audio-ext-clk-up.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/init.h>
  6. #include <linux/io.h>
  7. #include <linux/err.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include "../../../drivers/clk/qcom/common.h"
  13. #include <linux/pinctrl/consumer.h>
  14. #include <linux/platform_device.h>
  15. #include <dsp/apr_audio-v2.h>
  16. #include <bindings/qcom,audio-ext-clk.h>
  17. #include <linux/ratelimit.h>
  18. #ifdef CONFIG_AUDIO_PRM
  19. #include <dsp/audio_prm.h>
  20. #else
  21. #include "audio-ext-clk-up.h"
  22. #endif
  23. enum {
  24. AUDIO_EXT_CLK_PMI,
  25. AUDIO_EXT_CLK_LNBB2,
  26. AUDIO_EXT_CLK_LPASS,
  27. AUDIO_EXT_CLK_LPASS2,
  28. AUDIO_EXT_CLK_LPASS3,
  29. AUDIO_EXT_CLK_LPASS4,
  30. AUDIO_EXT_CLK_LPASS5,
  31. AUDIO_EXT_CLK_LPASS6,
  32. AUDIO_EXT_CLK_LPASS7,
  33. AUDIO_EXT_CLK_LPASS_CORE_HW_VOTE,
  34. AUDIO_EXT_CLK_LPASS8,
  35. AUDIO_EXT_CLK_LPASS_AUDIO_HW_VOTE,
  36. AUDIO_EXT_CLK_LPASS9,
  37. AUDIO_EXT_CLK_LPASS10,
  38. AUDIO_EXT_CLK_LPASS11,
  39. AUDIO_EXT_CLK_LPASS12,
  40. AUDIO_EXT_CLK_LPASS13,
  41. AUDIO_EXT_CLK_LPASS_MAX,
  42. AUDIO_EXT_CLK_EXTERNAL_PLL = AUDIO_EXT_CLK_LPASS_MAX,
  43. AUDIO_EXT_CLK_MAX,
  44. };
  45. struct pinctrl_info {
  46. struct pinctrl *pinctrl;
  47. struct pinctrl_state *sleep;
  48. struct pinctrl_state *active;
  49. char __iomem *base;
  50. };
  51. struct audio_ext_clk {
  52. struct pinctrl_info pnctrl_info;
  53. struct clk_fixed_factor fact;
  54. };
  55. struct audio_ext_clk_priv {
  56. struct device *dev;
  57. int clk_src;
  58. struct afe_clk_set clk_cfg;
  59. #ifdef CONFIG_AUDIO_PRM
  60. struct clk_cfg prm_clk_cfg;
  61. #endif
  62. struct audio_ext_clk audio_clk;
  63. const char *clk_name;
  64. uint32_t lpass_core_hwvote_client_handle;
  65. uint32_t lpass_audio_hwvote_client_handle;
  66. };
  67. static inline struct audio_ext_clk_priv *to_audio_clk(struct clk_hw *hw)
  68. {
  69. return container_of(hw, struct audio_ext_clk_priv, audio_clk.fact.hw);
  70. }
  71. static int audio_ext_clk_prepare(struct clk_hw *hw)
  72. {
  73. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  74. struct pinctrl_info *pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  75. int ret;
  76. static DEFINE_RATELIMIT_STATE(rtl, 1 * HZ, 1);
  77. if ((clk_priv->clk_src >= AUDIO_EXT_CLK_LPASS) &&
  78. (clk_priv->clk_src < AUDIO_EXT_CLK_LPASS_MAX) && !clk_priv->clk_cfg.enable) {
  79. #ifdef CONFIG_AUDIO_PRM
  80. pr_debug("%s: clk_id %x ",__func__, clk_priv->prm_clk_cfg.clk_id);
  81. trace_printk("%s: clk_id %x \n", __func__, clk_priv->prm_clk_cfg.clk_id);
  82. ret = audio_prm_set_lpass_clk_cfg(&clk_priv->prm_clk_cfg,1);
  83. #else
  84. ret = afe_set_lpass_clk_cfg(IDX_RSVD_3, &clk_priv->clk_cfg);
  85. #endif
  86. if (ret < 0) {
  87. if (__ratelimit(&rtl))
  88. pr_err_ratelimited("%s afe_set_digital_codec_core_clock failed\n",
  89. __func__);
  90. return ret;
  91. }
  92. clk_priv->clk_cfg.enable = 1;
  93. }
  94. if (pnctrl_info->pinctrl) {
  95. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  96. pnctrl_info->active);
  97. if (ret) {
  98. pr_err("%s: active state select failed with %d\n",
  99. __func__, ret);
  100. return -EIO;
  101. }
  102. }
  103. if (pnctrl_info->base)
  104. iowrite32(1, pnctrl_info->base);
  105. return 0;
  106. }
  107. static void audio_ext_clk_unprepare(struct clk_hw *hw)
  108. {
  109. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  110. struct pinctrl_info *pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  111. int ret;
  112. static DEFINE_RATELIMIT_STATE(rtl, 1 * HZ, 1);
  113. if (pnctrl_info->pinctrl) {
  114. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  115. pnctrl_info->sleep);
  116. if (ret) {
  117. pr_err("%s: active state select failed with %d\n",
  118. __func__, ret);
  119. return;
  120. }
  121. }
  122. if ((clk_priv->clk_src >= AUDIO_EXT_CLK_LPASS) &&
  123. (clk_priv->clk_src < AUDIO_EXT_CLK_LPASS_MAX)) {
  124. clk_priv->clk_cfg.enable = 0;
  125. #ifdef CONFIG_AUDIO_PRM
  126. pr_debug("%s: clk_id %x",__func__,
  127. clk_priv->prm_clk_cfg.clk_id);
  128. ret = audio_prm_set_lpass_clk_cfg(&clk_priv->prm_clk_cfg,0);
  129. trace_printk("%s: clk_id %x \n", __func__, clk_priv->prm_clk_cfg.clk_id);
  130. #else
  131. ret = afe_set_lpass_clk_cfg(IDX_RSVD_3, &clk_priv->clk_cfg);
  132. #endif
  133. if (ret < 0) {
  134. if (__ratelimit(&rtl))
  135. pr_err_ratelimited("%s: afe_set_lpass_clk_cfg failed, ret = %d\n",
  136. __func__, ret);
  137. }
  138. }
  139. if (pnctrl_info->base)
  140. iowrite32(0, pnctrl_info->base);
  141. }
  142. static u8 audio_ext_clk_get_parent(struct clk_hw *hw)
  143. {
  144. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  145. int num_parents = clk_hw_get_num_parents(hw);
  146. const char * const *parent_names = hw->init->parent_names;
  147. u8 i = 0, ret = hw->init->num_parents + 1;
  148. if ((clk_priv->clk_src == AUDIO_EXT_CLK_PMI) && clk_priv->clk_name) {
  149. for (i = 0; i < num_parents; i++) {
  150. if (!strcmp(parent_names[i], clk_priv->clk_name))
  151. ret = i;
  152. }
  153. pr_debug("%s: parent index = %u\n", __func__, ret);
  154. return ret;
  155. } else
  156. return 0;
  157. }
  158. static int lpass_hw_vote_prepare(struct clk_hw *hw)
  159. {
  160. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  161. int ret;
  162. static DEFINE_RATELIMIT_STATE(rtl, 1 * HZ, 1);
  163. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS_CORE_HW_VOTE) {
  164. #ifdef CONFIG_AUDIO_PRM
  165. pr_debug("%s: core vote clk_id %x \n",__func__, clk_priv->prm_clk_cfg.clk_id);
  166. ret = audio_prm_set_lpass_hw_core_req(&clk_priv->prm_clk_cfg,
  167. HW_CORE_ID_LPASS, 1);
  168. trace_printk("%s: core vote clk_id %x \n", __func__, clk_priv->prm_clk_cfg.clk_id);
  169. #else
  170. ret = afe_vote_lpass_core_hw(AFE_LPASS_CORE_HW_MACRO_BLOCK,
  171. "LPASS_HW_MACRO",
  172. &clk_priv->lpass_core_hwvote_client_handle);
  173. #endif
  174. if (ret < 0) {
  175. pr_err("%s lpass core hw vote failed %d\n",
  176. __func__, ret);
  177. return ret;
  178. }
  179. }
  180. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS_AUDIO_HW_VOTE) {
  181. #ifdef CONFIG_AUDIO_PRM
  182. pr_debug("%s: audio vote clk_id %x \n",__func__, clk_priv->prm_clk_cfg.clk_id);
  183. ret = audio_prm_set_lpass_hw_core_req(&clk_priv->prm_clk_cfg,
  184. HW_CORE_ID_DCODEC, 1);
  185. trace_printk("%s: audio vote clk_id %x \n", __func__, clk_priv->prm_clk_cfg.clk_id);
  186. #else
  187. ret = afe_vote_lpass_core_hw(AFE_LPASS_CORE_HW_DCODEC_BLOCK,
  188. "LPASS_HW_DCODEC",
  189. &clk_priv->lpass_audio_hwvote_client_handle);
  190. #endif
  191. if (ret < 0) {
  192. if (__ratelimit(&rtl))
  193. pr_err("%s lpass audio hw vote failed %d\n",
  194. __func__, ret);
  195. return ret;
  196. }
  197. }
  198. return 0;
  199. }
  200. static void lpass_hw_vote_unprepare(struct clk_hw *hw)
  201. {
  202. struct audio_ext_clk_priv *clk_priv = to_audio_clk(hw);
  203. int ret = 0;
  204. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS_CORE_HW_VOTE) {
  205. #ifdef CONFIG_AUDIO_PRM
  206. pr_debug("%s: core vote clk_id %x \n",__func__, clk_priv->prm_clk_cfg.clk_id);
  207. ret = audio_prm_set_lpass_hw_core_req(&clk_priv->prm_clk_cfg,
  208. HW_CORE_ID_LPASS, 0);
  209. trace_printk("%s: core vote clk_id %x \n", __func__, clk_priv->prm_clk_cfg.clk_id);
  210. #else
  211. ret = afe_unvote_lpass_core_hw(
  212. AFE_LPASS_CORE_HW_MACRO_BLOCK,
  213. clk_priv->lpass_core_hwvote_client_handle);
  214. #endif
  215. if (ret < 0) {
  216. pr_err("%s lpass core hw vote failed %d\n",
  217. __func__, ret);
  218. }
  219. }
  220. if (clk_priv->clk_src == AUDIO_EXT_CLK_LPASS_AUDIO_HW_VOTE) {
  221. #ifdef CONFIG_AUDIO_PRM
  222. pr_debug("%s: audio vote clk_id %x \n",__func__, clk_priv->prm_clk_cfg.clk_id);
  223. ret = audio_prm_set_lpass_hw_core_req(&clk_priv->prm_clk_cfg,
  224. HW_CORE_ID_DCODEC, 0);
  225. trace_printk("%s: audio vote clk_id %x \n", __func__, clk_priv->prm_clk_cfg.clk_id);
  226. #else
  227. ret = afe_unvote_lpass_core_hw(
  228. AFE_LPASS_CORE_HW_DCODEC_BLOCK,
  229. clk_priv->lpass_audio_hwvote_client_handle);
  230. #endif
  231. if (ret < 0) {
  232. pr_err("%s lpass audio hw unvote failed %d\n",
  233. __func__, ret);
  234. }
  235. }
  236. }
  237. static const struct clk_ops audio_ext_clk_ops = {
  238. .prepare = audio_ext_clk_prepare,
  239. .unprepare = audio_ext_clk_unprepare,
  240. .get_parent = audio_ext_clk_get_parent,
  241. };
  242. static const struct clk_ops lpass_hw_vote_ops = {
  243. .prepare = lpass_hw_vote_prepare,
  244. .unprepare = lpass_hw_vote_unprepare,
  245. };
  246. static const char * const audio_ext_pmi_div_clk[] = {
  247. "qpnp_clkdiv_1",
  248. "pms405_div_clk1",
  249. "pm6150_div_clk1",
  250. "pm6125_div_clk1",
  251. };
  252. static int audio_ext_clk_dummy_prepare(struct clk_hw *hw)
  253. {
  254. return 0;
  255. }
  256. static void audio_ext_clk_dummy_unprepare(struct clk_hw *hw)
  257. {
  258. }
  259. static const struct clk_ops audio_ext_clk_dummy_ops = {
  260. .prepare = audio_ext_clk_dummy_prepare,
  261. .unprepare = audio_ext_clk_dummy_unprepare,
  262. };
  263. static struct audio_ext_clk audio_clk_array[] = {
  264. {
  265. .pnctrl_info = {NULL},
  266. .fact = {
  267. .mult = 1,
  268. .div = 1,
  269. .hw.init = &(struct clk_init_data){
  270. .name = "audio_ext_pmi_clk",
  271. .parent_names = audio_ext_pmi_div_clk,
  272. .num_parents =
  273. ARRAY_SIZE(audio_ext_pmi_div_clk),
  274. .ops = &audio_ext_clk_ops,
  275. },
  276. },
  277. },
  278. {
  279. .pnctrl_info = {NULL},
  280. .fact = {
  281. .mult = 1,
  282. .div = 1,
  283. .hw.init = &(struct clk_init_data){
  284. .name = "audio_ext_pmi_lnbb_clk",
  285. .parent_names = (const char *[])
  286. { "ln_bb_clk2" },
  287. .num_parents = 1,
  288. .ops = &audio_ext_clk_dummy_ops,
  289. },
  290. },
  291. },
  292. {
  293. .pnctrl_info = {NULL},
  294. .fact = {
  295. .mult = 1,
  296. .div = 1,
  297. .hw.init = &(struct clk_init_data){
  298. .name = "audio_lpass_mclk",
  299. .ops = &audio_ext_clk_ops,
  300. },
  301. },
  302. },
  303. {
  304. .pnctrl_info = {NULL},
  305. .fact = {
  306. .mult = 1,
  307. .div = 1,
  308. .hw.init = &(struct clk_init_data){
  309. .name = "audio_lpass_mclk2",
  310. .ops = &audio_ext_clk_ops,
  311. },
  312. },
  313. },
  314. {
  315. .pnctrl_info = {NULL},
  316. .fact = {
  317. .mult = 1,
  318. .div = 1,
  319. .hw.init = &(struct clk_init_data){
  320. .name = "audio_lpass_mclk3",
  321. .ops = &audio_ext_clk_ops,
  322. },
  323. },
  324. },
  325. {
  326. .pnctrl_info = {NULL},
  327. .fact = {
  328. .mult = 1,
  329. .div = 1,
  330. .hw.init = &(struct clk_init_data){
  331. .name = "audio_lpass_mclk4",
  332. .ops = &audio_ext_clk_ops,
  333. },
  334. },
  335. },
  336. {
  337. .pnctrl_info = {NULL},
  338. .fact = {
  339. .mult = 1,
  340. .div = 1,
  341. .hw.init = &(struct clk_init_data){
  342. .name = "audio_lpass_mclk5",
  343. .ops = &audio_ext_clk_ops,
  344. },
  345. },
  346. },
  347. {
  348. .pnctrl_info = {NULL},
  349. .fact = {
  350. .mult = 1,
  351. .div = 1,
  352. .hw.init = &(struct clk_init_data){
  353. .name = "audio_lpass_mclk6",
  354. .ops = &audio_ext_clk_ops,
  355. },
  356. },
  357. },
  358. {
  359. .pnctrl_info = {NULL},
  360. .fact = {
  361. .mult = 1,
  362. .div = 1,
  363. .hw.init = &(struct clk_init_data){
  364. .name = "audio_lpass_mclk7",
  365. .ops = &audio_ext_clk_ops,
  366. },
  367. },
  368. },
  369. {
  370. .pnctrl_info = {NULL},
  371. .fact = {
  372. .hw.init = &(struct clk_init_data){
  373. .name = "lpass_hw_vote_clk",
  374. .ops = &lpass_hw_vote_ops,
  375. },
  376. },
  377. },
  378. {
  379. .pnctrl_info = {NULL},
  380. .fact = {
  381. .mult = 1,
  382. .div = 1,
  383. .hw.init = &(struct clk_init_data){
  384. .name = "audio_lpass_mclk8",
  385. .ops = &audio_ext_clk_ops,
  386. },
  387. },
  388. },
  389. {
  390. .pnctrl_info = {NULL},
  391. .fact = {
  392. .hw.init = &(struct clk_init_data){
  393. .name = "lpass_audio_hw_vote_clk",
  394. .ops = &lpass_hw_vote_ops,
  395. },
  396. },
  397. },
  398. {
  399. .pnctrl_info = {NULL},
  400. .fact = {
  401. .mult = 1,
  402. .div = 1,
  403. .hw.init = &(struct clk_init_data){
  404. .name = "audio_lpass_mclk9",
  405. .ops = &audio_ext_clk_ops,
  406. },
  407. },
  408. },
  409. {
  410. .pnctrl_info = {NULL},
  411. .fact = {
  412. .mult = 1,
  413. .div = 1,
  414. .hw.init = &(struct clk_init_data){
  415. .name = "audio_lpass_mclk10",
  416. .ops = &audio_ext_clk_ops,
  417. },
  418. },
  419. },
  420. {
  421. .pnctrl_info = {NULL},
  422. .fact = {
  423. .mult = 1,
  424. .div = 1,
  425. .hw.init = &(struct clk_init_data){
  426. .name = "audio_lpass_mclk11",
  427. .ops = &audio_ext_clk_ops,
  428. },
  429. },
  430. },
  431. {
  432. .pnctrl_info = {NULL},
  433. .fact = {
  434. .mult = 1,
  435. .div = 1,
  436. .hw.init = &(struct clk_init_data){
  437. .name = "audio_lpass_mclk12",
  438. .ops = &audio_ext_clk_ops,
  439. },
  440. },
  441. },
  442. {
  443. .pnctrl_info = {NULL},
  444. .fact = {
  445. .mult = 1,
  446. .div = 1,
  447. .hw.init = &(struct clk_init_data){
  448. .name = "audio_lpass_mclk13",
  449. .ops = &audio_ext_clk_ops,
  450. },
  451. },
  452. },
  453. {
  454. .pnctrl_info = {NULL},
  455. .fact = {
  456. .mult = 1,
  457. .div = 1,
  458. .hw.init = &(struct clk_init_data){
  459. .name = "audio_external_pll_clk",
  460. .ops = &audio_ext_clk_ops,
  461. },
  462. },
  463. },
  464. };
  465. static int audio_get_pinctrl(struct platform_device *pdev)
  466. {
  467. struct device *dev = &pdev->dev;
  468. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  469. struct pinctrl_info *pnctrl_info;
  470. struct pinctrl *pinctrl;
  471. int ret;
  472. u32 reg;
  473. pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  474. if (pnctrl_info->pinctrl) {
  475. dev_err(dev, "%s: already requested before\n",
  476. __func__);
  477. return -EINVAL;
  478. }
  479. pinctrl = devm_pinctrl_get(dev);
  480. if (IS_ERR_OR_NULL(pinctrl)) {
  481. dev_err(dev, "%s: Unable to get pinctrl handle\n",
  482. __func__);
  483. return -EINVAL;
  484. }
  485. pnctrl_info->pinctrl = pinctrl;
  486. /* get all state handles from Device Tree */
  487. pnctrl_info->sleep = pinctrl_lookup_state(pinctrl, "sleep");
  488. if (IS_ERR(pnctrl_info->sleep)) {
  489. dev_err(dev, "%s: could not get sleep pinstate\n",
  490. __func__);
  491. goto err;
  492. }
  493. pnctrl_info->active = pinctrl_lookup_state(pinctrl, "active");
  494. if (IS_ERR(pnctrl_info->active)) {
  495. dev_err(dev, "%s: could not get active pinstate\n",
  496. __func__);
  497. goto err;
  498. }
  499. /* Reset the TLMM pins to a default state */
  500. ret = pinctrl_select_state(pnctrl_info->pinctrl,
  501. pnctrl_info->sleep);
  502. if (ret) {
  503. dev_err(dev, "%s: Disable TLMM pins failed with %d\n",
  504. __func__, ret);
  505. goto err;
  506. }
  507. ret = of_property_read_u32(dev->of_node, "qcom,mclk-clk-reg", &reg);
  508. if (ret < 0) {
  509. dev_dbg(dev, "%s: miss mclk reg\n", __func__);
  510. } else {
  511. pnctrl_info->base = ioremap(reg, sizeof(u32));
  512. if (pnctrl_info->base == NULL) {
  513. dev_err(dev, "%s ioremap failed\n", __func__);
  514. goto err;
  515. }
  516. }
  517. return 0;
  518. err:
  519. devm_pinctrl_put(pnctrl_info->pinctrl);
  520. return -EINVAL;
  521. }
  522. static int audio_put_pinctrl(struct platform_device *pdev)
  523. {
  524. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  525. struct pinctrl_info *pnctrl_info = NULL;
  526. pnctrl_info = &clk_priv->audio_clk.pnctrl_info;
  527. if (pnctrl_info && pnctrl_info->pinctrl) {
  528. devm_pinctrl_put(pnctrl_info->pinctrl);
  529. pnctrl_info->pinctrl = NULL;
  530. }
  531. return 0;
  532. }
  533. static int audio_get_clk_data(struct platform_device *pdev)
  534. {
  535. int ret;
  536. struct clk *audio_clk;
  537. struct clk_hw *clkhw;
  538. struct clk_onecell_data *clk_data;
  539. struct audio_ext_clk_priv *clk_priv = platform_get_drvdata(pdev);
  540. clk_data = devm_kzalloc(&pdev->dev, sizeof(*clk_data), GFP_KERNEL);
  541. if (!clk_data)
  542. return -ENOMEM;
  543. clk_data->clk_num = 1;
  544. clk_data->clks = devm_kzalloc(&pdev->dev,
  545. sizeof(struct clk *),
  546. GFP_KERNEL);
  547. if (!clk_data->clks)
  548. return -ENOMEM;
  549. clkhw = &clk_priv->audio_clk.fact.hw;
  550. audio_clk = devm_clk_register(&pdev->dev, clkhw);
  551. if (IS_ERR(audio_clk)) {
  552. dev_err(&pdev->dev,
  553. "%s: clock register failed for clk_src = %d\\n",
  554. __func__, clk_priv->clk_src);
  555. ret = PTR_ERR(audio_clk);
  556. return ret;
  557. }
  558. clk_data->clks[0] = audio_clk;
  559. ret = of_clk_add_provider(pdev->dev.of_node,
  560. of_clk_src_onecell_get, clk_data);
  561. if (ret)
  562. dev_err(&pdev->dev, "%s: clock add failed for clk_src = %d\n",
  563. __func__, clk_priv->clk_src);
  564. return ret;
  565. }
  566. static int audio_ref_clk_probe(struct platform_device *pdev)
  567. {
  568. int ret;
  569. struct audio_ext_clk_priv *clk_priv;
  570. u32 clk_freq = 0, clk_id = 0, clk_src = 0, use_pinctrl = 0;
  571. clk_priv = devm_kzalloc(&pdev->dev, sizeof(*clk_priv), GFP_KERNEL);
  572. if (!clk_priv)
  573. return -ENOMEM;
  574. ret = of_property_read_u32(pdev->dev.of_node,
  575. "qcom,codec-ext-clk-src",
  576. &clk_src);
  577. if (ret) {
  578. dev_err(&pdev->dev, "%s: could not get clk source, ret = %d\n",
  579. __func__, ret);
  580. return ret;
  581. }
  582. if (clk_src >= AUDIO_EXT_CLK_MAX) {
  583. dev_err(&pdev->dev, "%s: Invalid clk source = %d\n",
  584. __func__, clk_src);
  585. return -EINVAL;
  586. }
  587. clk_priv->clk_name = NULL;
  588. clk_priv->clk_src = clk_src;
  589. memcpy(&clk_priv->audio_clk, &audio_clk_array[clk_src],
  590. sizeof(struct audio_ext_clk));
  591. /* Init lpass clk default values */
  592. clk_priv->clk_cfg.clk_set_minor_version =
  593. Q6AFE_LPASS_CLK_CONFIG_API_VERSION;
  594. clk_priv->clk_cfg.clk_id = Q6AFE_LPASS_CLK_ID_SPEAKER_I2S_OSR;
  595. clk_priv->clk_cfg.clk_freq_in_hz = Q6AFE_LPASS_OSR_CLK_9_P600_MHZ;
  596. clk_priv->clk_cfg.clk_attri = Q6AFE_LPASS_CLK_ATTRIBUTE_COUPLE_NO;
  597. #ifdef CONFIG_AUDIO_PRM
  598. /* Init prm clk cfg default values */
  599. clk_priv->prm_clk_cfg.clk_id = Q6AFE_LPASS_CLK_ID_SPEAKER_I2S_OSR;
  600. clk_priv->prm_clk_cfg.clk_freq_in_hz = Q6AFE_LPASS_OSR_CLK_9_P600_MHZ;
  601. clk_priv->prm_clk_cfg.clk_attri = Q6AFE_LPASS_CLK_ATTRIBUTE_COUPLE_NO;
  602. clk_priv->prm_clk_cfg.clk_root = 0;
  603. #endif
  604. ret = of_property_read_u32(pdev->dev.of_node,
  605. "qcom,codec-lpass-ext-clk-freq",
  606. &clk_freq);
  607. if (!ret) {
  608. clk_priv->clk_cfg.clk_freq_in_hz = clk_freq;
  609. #ifdef CONFIG_AUDIO_PRM
  610. clk_priv->prm_clk_cfg.clk_freq_in_hz = clk_freq;
  611. #endif
  612. }
  613. ret = of_property_read_u32(pdev->dev.of_node,
  614. "qcom,codec-lpass-clk-id",
  615. &clk_id);
  616. if (!ret) {
  617. clk_priv->clk_cfg.clk_id = clk_id;
  618. #ifdef CONFIG_AUDIO_PRM
  619. clk_priv->prm_clk_cfg.clk_id = clk_id;
  620. dev_dbg(&pdev->dev, "%s: PRM ext-clk freq: %d, lpass clk_id: %d, clk_src: %d\n",
  621. __func__, clk_priv->prm_clk_cfg.clk_freq_in_hz,
  622. clk_priv->prm_clk_cfg.clk_id, clk_priv->clk_src);
  623. #endif
  624. }
  625. dev_dbg(&pdev->dev, "%s: ext-clk freq: %d, lpass clk_id: %d, clk_src: %d\n",
  626. __func__, clk_priv->clk_cfg.clk_freq_in_hz,
  627. clk_priv->clk_cfg.clk_id, clk_priv->clk_src);
  628. dev_dbg(&pdev->dev, "%s: PRM2 ext-clk freq: %d, lpass clk_id: %d, clk_src: %d\n",
  629. __func__, clk_priv->prm_clk_cfg.clk_freq_in_hz,
  630. clk_priv->prm_clk_cfg.clk_id, clk_priv->clk_src);
  631. platform_set_drvdata(pdev, clk_priv);
  632. ret = of_property_read_string(pdev->dev.of_node, "pmic-clock-names",
  633. &clk_priv->clk_name);
  634. if (ret)
  635. dev_dbg(&pdev->dev, "%s: could not find pmic clock names\n",
  636. __func__);
  637. /*
  638. * property qcom,use-pinctrl to be defined in DTSI to val 1
  639. * for clock nodes using pinctrl
  640. */
  641. of_property_read_u32(pdev->dev.of_node, "qcom,use-pinctrl",
  642. &use_pinctrl);
  643. dev_dbg(&pdev->dev, "%s: use-pinctrl : %d\n",
  644. __func__, use_pinctrl);
  645. if (use_pinctrl) {
  646. ret = audio_get_pinctrl(pdev);
  647. if (ret) {
  648. dev_err(&pdev->dev, "%s: Parsing PMI pinctrl failed\n",
  649. __func__);
  650. return ret;
  651. }
  652. }
  653. ret = audio_get_clk_data(pdev);
  654. if (ret) {
  655. dev_err(&pdev->dev, "%s: clk_init is failed\n",
  656. __func__);
  657. audio_put_pinctrl(pdev);
  658. return ret;
  659. }
  660. return 0;
  661. }
  662. static int audio_ref_clk_remove(struct platform_device *pdev)
  663. {
  664. audio_put_pinctrl(pdev);
  665. return 0;
  666. }
  667. static const struct of_device_id audio_ref_clk_match[] = {
  668. {.compatible = "qcom,audio-ref-clk"},
  669. {}
  670. };
  671. MODULE_DEVICE_TABLE(of, audio_ref_clk_match);
  672. static struct platform_driver audio_ref_clk_driver = {
  673. .driver = {
  674. .name = "audio-ref-clk",
  675. .owner = THIS_MODULE,
  676. .of_match_table = audio_ref_clk_match,
  677. .suppress_bind_attrs = true,
  678. },
  679. .probe = audio_ref_clk_probe,
  680. .remove = audio_ref_clk_remove,
  681. };
  682. int audio_ref_clk_platform_init(void)
  683. {
  684. return platform_driver_register(&audio_ref_clk_driver);
  685. }
  686. void audio_ref_clk_platform_exit(void)
  687. {
  688. platform_driver_unregister(&audio_ref_clk_driver);
  689. }
  690. MODULE_DESCRIPTION("Audio Ref Up Clock module platform driver");
  691. MODULE_LICENSE("GPL v2");