bolero-clk-rsc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/of_platform.h>
  6. #include <linux/module.h>
  7. #include <linux/io.h>
  8. #include <linux/init.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include "bolero-cdc.h"
  14. #include "bolero-clk-rsc.h"
  15. #define DRV_NAME "bolero-clk-rsc"
  16. #define BOLERO_CLK_NAME_LENGTH 30
  17. #define NPL_CLK_OFFSET (TX_NPL_CLK - TX_CORE_CLK)
  18. static char clk_src_name[MAX_CLK][BOLERO_CLK_NAME_LENGTH] = {
  19. "tx_core_clk",
  20. "rx_core_clk",
  21. "wsa_core_clk",
  22. "va_core_clk",
  23. "tx_npl_clk",
  24. "rx_npl_clk",
  25. "wsa_npl_clk",
  26. "va_npl_clk",
  27. };
  28. struct bolero_clk_rsc {
  29. struct device *dev;
  30. struct mutex rsc_clk_lock;
  31. struct mutex fs_gen_lock;
  32. struct clk *clk[MAX_CLK];
  33. int clk_cnt[MAX_CLK];
  34. int reg_seq_en_cnt;
  35. int va_tx_clk_cnt;
  36. bool dev_up;
  37. u32 num_fs_reg;
  38. u32 *fs_gen_seq;
  39. int default_clk_id[MAX_CLK];
  40. struct regmap *regmap;
  41. char __iomem *rx_clk_muxsel;
  42. char __iomem *wsa_clk_muxsel;
  43. char __iomem *va_clk_muxsel;
  44. };
  45. static int bolero_clk_rsc_cb(struct device *dev, u16 event)
  46. {
  47. struct bolero_clk_rsc *priv;
  48. if (!dev) {
  49. pr_err("%s: Invalid device pointer\n",
  50. __func__);
  51. return -EINVAL;
  52. }
  53. priv = dev_get_drvdata(dev);
  54. if (!priv) {
  55. pr_err("%s: Invalid clk rsc priviate data\n",
  56. __func__);
  57. return -EINVAL;
  58. }
  59. mutex_lock(&priv->rsc_clk_lock);
  60. if (event == BOLERO_MACRO_EVT_SSR_UP)
  61. priv->dev_up = true;
  62. else if (event == BOLERO_MACRO_EVT_SSR_DOWN)
  63. priv->dev_up = false;
  64. mutex_unlock(&priv->rsc_clk_lock);
  65. return 0;
  66. }
  67. static char __iomem *bolero_clk_rsc_get_clk_muxsel(struct bolero_clk_rsc *priv,
  68. int clk_id)
  69. {
  70. switch (clk_id) {
  71. case RX_CORE_CLK:
  72. return priv->rx_clk_muxsel;
  73. case WSA_CORE_CLK:
  74. return priv->wsa_clk_muxsel;
  75. case VA_CORE_CLK:
  76. return priv->va_clk_muxsel;
  77. case TX_CORE_CLK:
  78. default:
  79. dev_err_ratelimited(priv->dev, "%s: Invalid case\n", __func__);
  80. break;
  81. }
  82. return NULL;
  83. }
  84. int bolero_rsc_clk_reset(struct device *dev, int clk_id)
  85. {
  86. struct device *clk_dev = NULL;
  87. struct bolero_clk_rsc *priv = NULL;
  88. int count = 0;
  89. if (!dev) {
  90. pr_err("%s: dev is null %d\n", __func__);
  91. return -EINVAL;
  92. }
  93. if (clk_id < 0 || clk_id >= MAX_CLK - NPL_CLK_OFFSET) {
  94. pr_err("%s: Invalid clk_id: %d\n",
  95. __func__, clk_id);
  96. return -EINVAL;
  97. }
  98. clk_dev = bolero_get_rsc_clk_device_ptr(dev->parent);
  99. if (!clk_dev) {
  100. pr_err("%s: Invalid rsc clk device\n", __func__);
  101. return -EINVAL;
  102. }
  103. priv = dev_get_drvdata(clk_dev);
  104. if (!priv) {
  105. pr_err("%s: Invalid rsc clk priviate data\n", __func__);
  106. return -EINVAL;
  107. }
  108. mutex_lock(&priv->rsc_clk_lock);
  109. while (__clk_is_enabled(priv->clk[clk_id])) {
  110. clk_disable_unprepare(priv->clk[clk_id + NPL_CLK_OFFSET]);
  111. clk_disable_unprepare(priv->clk[clk_id]);
  112. count++;
  113. }
  114. dev_dbg(priv->dev,
  115. "%s: clock reset after ssr, count %d\n", __func__, count);
  116. while (count--) {
  117. clk_prepare_enable(priv->clk[clk_id]);
  118. clk_prepare_enable(priv->clk[clk_id + NPL_CLK_OFFSET]);
  119. }
  120. mutex_unlock(&priv->rsc_clk_lock);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(bolero_rsc_clk_reset);
  124. void bolero_clk_rsc_enable_all_clocks(struct device *dev, bool enable)
  125. {
  126. struct device *clk_dev = NULL;
  127. struct bolero_clk_rsc *priv = NULL;
  128. int i = 0;
  129. if (!dev) {
  130. pr_err("%s: dev is null %d\n", __func__);
  131. return;
  132. }
  133. clk_dev = bolero_get_rsc_clk_device_ptr(dev->parent);
  134. if (!clk_dev) {
  135. pr_err("%s: Invalid rsc clk device\n", __func__);
  136. return;
  137. }
  138. priv = dev_get_drvdata(clk_dev);
  139. if (!priv) {
  140. pr_err("%s: Invalid rsc clk private data\n", __func__);
  141. return;
  142. }
  143. mutex_lock(&priv->rsc_clk_lock);
  144. for (i = 0; i < MAX_CLK - NPL_CLK_OFFSET; i++) {
  145. if (enable) {
  146. if (priv->clk[i])
  147. clk_prepare_enable(priv->clk[i]);
  148. if (priv->clk[i + NPL_CLK_OFFSET])
  149. clk_prepare_enable(
  150. priv->clk[i + NPL_CLK_OFFSET]);
  151. } else {
  152. if (priv->clk[i + NPL_CLK_OFFSET])
  153. clk_disable_unprepare(
  154. priv->clk[i + NPL_CLK_OFFSET]);
  155. if (priv->clk[i])
  156. clk_disable_unprepare(priv->clk[i]);
  157. }
  158. }
  159. mutex_unlock(&priv->rsc_clk_lock);
  160. return;
  161. }
  162. EXPORT_SYMBOL(bolero_clk_rsc_enable_all_clocks);
  163. static int bolero_clk_rsc_mux0_clk_request(struct bolero_clk_rsc *priv,
  164. int clk_id,
  165. bool enable)
  166. {
  167. int ret = 0;
  168. if (enable) {
  169. /* Enable Requested Core clk */
  170. if (priv->clk_cnt[clk_id] == 0) {
  171. ret = clk_prepare_enable(priv->clk[clk_id]);
  172. if (ret < 0) {
  173. dev_err_ratelimited(priv->dev, "%s:clk_id %d enable failed\n",
  174. __func__, clk_id);
  175. goto done;
  176. }
  177. if (priv->clk[clk_id + NPL_CLK_OFFSET]) {
  178. ret = clk_prepare_enable(
  179. priv->clk[clk_id + NPL_CLK_OFFSET]);
  180. if (ret < 0) {
  181. dev_err_ratelimited(priv->dev, "%s:clk_id %d enable failed\n",
  182. __func__,
  183. clk_id + NPL_CLK_OFFSET);
  184. goto err;
  185. }
  186. }
  187. }
  188. priv->clk_cnt[clk_id]++;
  189. } else {
  190. if (priv->clk_cnt[clk_id] <= 0) {
  191. dev_err_ratelimited(priv->dev, "%s: clk_id: %d is already disabled\n",
  192. __func__, clk_id);
  193. priv->clk_cnt[clk_id] = 0;
  194. goto done;
  195. }
  196. priv->clk_cnt[clk_id]--;
  197. if (priv->clk_cnt[clk_id] == 0) {
  198. if (priv->clk[clk_id + NPL_CLK_OFFSET])
  199. clk_disable_unprepare(
  200. priv->clk[clk_id + NPL_CLK_OFFSET]);
  201. clk_disable_unprepare(priv->clk[clk_id]);
  202. }
  203. }
  204. return ret;
  205. err:
  206. clk_disable_unprepare(priv->clk[clk_id]);
  207. done:
  208. return ret;
  209. }
  210. static int bolero_clk_rsc_mux1_clk_request(struct bolero_clk_rsc *priv,
  211. int clk_id,
  212. bool enable)
  213. {
  214. char __iomem *clk_muxsel = NULL;
  215. int ret = 0;
  216. int default_clk_id = priv->default_clk_id[clk_id];
  217. clk_muxsel = bolero_clk_rsc_get_clk_muxsel(priv, clk_id);
  218. if (!clk_muxsel) {
  219. ret = -EINVAL;
  220. goto done;
  221. }
  222. if (enable) {
  223. if (priv->clk_cnt[clk_id] == 0) {
  224. if (clk_id != VA_CORE_CLK) {
  225. ret = bolero_clk_rsc_mux0_clk_request(priv,
  226. default_clk_id,
  227. true);
  228. if (ret < 0)
  229. goto done;
  230. }
  231. ret = clk_prepare_enable(priv->clk[clk_id]);
  232. if (ret < 0) {
  233. dev_err_ratelimited(priv->dev, "%s:clk_id %d enable failed\n",
  234. __func__, clk_id);
  235. goto err_clk;
  236. }
  237. if (priv->clk[clk_id + NPL_CLK_OFFSET]) {
  238. ret = clk_prepare_enable(
  239. priv->clk[clk_id + NPL_CLK_OFFSET]);
  240. if (ret < 0) {
  241. dev_err_ratelimited(priv->dev, "%s:clk_id %d enable failed\n",
  242. __func__,
  243. clk_id + NPL_CLK_OFFSET);
  244. goto err_npl_clk;
  245. }
  246. }
  247. /*
  248. * Temp SW workaround to address a glitch issue of
  249. * VA GFMux instance responsible for switching from
  250. * TX MCLK to VA MCLK. This configuration would be taken
  251. * care in DSP itself
  252. */
  253. if (clk_id != VA_CORE_CLK) {
  254. iowrite32(0x1, clk_muxsel);
  255. bolero_clk_rsc_mux0_clk_request(priv,
  256. default_clk_id,
  257. false);
  258. }
  259. }
  260. priv->clk_cnt[clk_id]++;
  261. } else {
  262. if (priv->clk_cnt[clk_id] <= 0) {
  263. dev_err_ratelimited(priv->dev, "%s: clk_id: %d is already disabled\n",
  264. __func__, clk_id);
  265. priv->clk_cnt[clk_id] = 0;
  266. goto done;
  267. }
  268. priv->clk_cnt[clk_id]--;
  269. if (priv->clk_cnt[clk_id] == 0) {
  270. if (clk_id != VA_CORE_CLK) {
  271. ret = bolero_clk_rsc_mux0_clk_request(priv,
  272. default_clk_id, true);
  273. if (!ret) {
  274. /*
  275. * Temp SW workaround to address a glitch issue
  276. * of VA GFMux instance responsible for
  277. * switching from TX MCLK to VA MCLK.
  278. * This configuration would be taken
  279. * care in DSP itself.
  280. */
  281. iowrite32(0x0, clk_muxsel);
  282. }
  283. }
  284. if (priv->clk[clk_id + NPL_CLK_OFFSET])
  285. clk_disable_unprepare(
  286. priv->clk[clk_id + NPL_CLK_OFFSET]);
  287. clk_disable_unprepare(priv->clk[clk_id]);
  288. if (clk_id != VA_CORE_CLK) {
  289. if (!ret)
  290. bolero_clk_rsc_mux0_clk_request(priv,
  291. default_clk_id, false);
  292. }
  293. }
  294. }
  295. return ret;
  296. err_npl_clk:
  297. clk_disable_unprepare(priv->clk[clk_id]);
  298. err_clk:
  299. if (clk_id != VA_CORE_CLK)
  300. bolero_clk_rsc_mux0_clk_request(priv, default_clk_id, false);
  301. done:
  302. return ret;
  303. }
  304. static int bolero_clk_rsc_check_and_update_va_clk(struct bolero_clk_rsc *priv,
  305. bool mux_switch,
  306. int clk_id,
  307. bool enable)
  308. {
  309. int ret = 0;
  310. if (enable) {
  311. if (clk_id == VA_CORE_CLK && mux_switch) {
  312. /*
  313. * Handle the following usecase scenarios during enable
  314. * 1. VA only, Active clk is VA_CORE_CLK
  315. * 2. record -> record + VA, Active clk is TX_CORE_CLK
  316. */
  317. if (priv->clk_cnt[TX_CORE_CLK] == 0) {
  318. ret = bolero_clk_rsc_mux1_clk_request(priv,
  319. VA_CORE_CLK, enable);
  320. if (ret < 0)
  321. goto err;
  322. } else {
  323. ret = bolero_clk_rsc_mux0_clk_request(priv,
  324. TX_CORE_CLK, enable);
  325. if (ret < 0)
  326. goto err;
  327. priv->va_tx_clk_cnt++;
  328. }
  329. } else if ((priv->clk_cnt[TX_CORE_CLK] > 0) &&
  330. (priv->clk_cnt[VA_CORE_CLK] > 0)) {
  331. /*
  332. * Handle following concurrency scenario during enable
  333. * 1. VA-> Record+VA, Increment TX CLK and Disable VA
  334. * 2. VA-> Playback+VA, Increment TX CLK and Disable VA
  335. */
  336. while (priv->clk_cnt[VA_CORE_CLK] > 0) {
  337. ret = bolero_clk_rsc_mux0_clk_request(priv,
  338. TX_CORE_CLK, true);
  339. if (ret < 0)
  340. goto err;
  341. bolero_clk_rsc_mux1_clk_request(priv,
  342. VA_CORE_CLK, false);
  343. priv->va_tx_clk_cnt++;
  344. }
  345. }
  346. } else {
  347. if (clk_id == VA_CORE_CLK && mux_switch) {
  348. /*
  349. * Handle the following usecase scenarios during disable
  350. * 1. VA only, disable VA_CORE_CLK
  351. * 2. Record + VA -> Record, decrement TX CLK count
  352. */
  353. if (priv->clk_cnt[VA_CORE_CLK]) {
  354. bolero_clk_rsc_mux1_clk_request(priv,
  355. VA_CORE_CLK, enable);
  356. } else if (priv->va_tx_clk_cnt) {
  357. bolero_clk_rsc_mux0_clk_request(priv,
  358. TX_CORE_CLK, enable);
  359. priv->va_tx_clk_cnt--;
  360. }
  361. } else if (priv->va_tx_clk_cnt == priv->clk_cnt[TX_CORE_CLK]) {
  362. /*
  363. * Handle the following usecase scenarios during disable
  364. * Record+VA-> VA: enable VA CLK, decrement TX CLK count
  365. */
  366. while (priv->va_tx_clk_cnt) {
  367. ret = bolero_clk_rsc_mux1_clk_request(priv,
  368. VA_CORE_CLK, true);
  369. if (ret < 0)
  370. goto err;
  371. bolero_clk_rsc_mux0_clk_request(priv,
  372. TX_CORE_CLK, false);
  373. priv->va_tx_clk_cnt--;
  374. }
  375. }
  376. }
  377. err:
  378. return ret;
  379. }
  380. /**
  381. * bolero_clk_rsc_fs_gen_request - request to enable/disable fs generation
  382. * sequence
  383. *
  384. * @dev: Macro device pointer
  385. * @enable: enable or disable flag
  386. */
  387. void bolero_clk_rsc_fs_gen_request(struct device *dev, bool enable)
  388. {
  389. int i;
  390. struct regmap *regmap;
  391. struct device *clk_dev = NULL;
  392. struct bolero_clk_rsc *priv = NULL;
  393. if (!dev) {
  394. pr_err("%s: dev is null %d\n", __func__);
  395. return;
  396. }
  397. clk_dev = bolero_get_rsc_clk_device_ptr(dev->parent);
  398. if (!clk_dev) {
  399. pr_err("%s: Invalid rsc clk device\n", __func__);
  400. return;
  401. }
  402. priv = dev_get_drvdata(clk_dev);
  403. if (!priv) {
  404. pr_err("%s: Invalid rsc clk priviate data\n", __func__);
  405. return;
  406. }
  407. regmap = dev_get_regmap(priv->dev->parent, NULL);
  408. if (!regmap) {
  409. pr_err("%s: regmap is null\n", __func__);
  410. return;
  411. }
  412. mutex_lock(&priv->fs_gen_lock);
  413. if (enable) {
  414. if (priv->reg_seq_en_cnt++ == 0) {
  415. for (i = 0; i < (priv->num_fs_reg * 2); i += 2) {
  416. dev_dbg(priv->dev, "%s: Register: %d, value: %d\n",
  417. __func__, priv->fs_gen_seq[i],
  418. priv->fs_gen_seq[i + 1]);
  419. regmap_update_bits(regmap,
  420. priv->fs_gen_seq[i],
  421. priv->fs_gen_seq[i + 1],
  422. priv->fs_gen_seq[i + 1]);
  423. }
  424. }
  425. } else {
  426. if (priv->reg_seq_en_cnt <= 0) {
  427. dev_err_ratelimited(priv->dev, "%s: req_seq_cnt: %d is already disabled\n",
  428. __func__, priv->reg_seq_en_cnt);
  429. priv->reg_seq_en_cnt = 0;
  430. mutex_unlock(&priv->fs_gen_lock);
  431. return;
  432. }
  433. if (--priv->reg_seq_en_cnt == 0) {
  434. for (i = ((priv->num_fs_reg - 1) * 2); i >= 0; i -= 2) {
  435. dev_dbg(priv->dev, "%s: Register: %d, value: %d\n",
  436. __func__, priv->fs_gen_seq[i],
  437. priv->fs_gen_seq[i + 1]);
  438. regmap_update_bits(regmap, priv->fs_gen_seq[i],
  439. priv->fs_gen_seq[i + 1], 0x0);
  440. }
  441. }
  442. }
  443. mutex_unlock(&priv->fs_gen_lock);
  444. }
  445. EXPORT_SYMBOL(bolero_clk_rsc_fs_gen_request);
  446. /**
  447. * bolero_clk_rsc_request_clock - request for clock to
  448. * enable/disable
  449. *
  450. * @dev: Macro device pointer.
  451. * @default_clk_id: mux0 Core clock ID input.
  452. * @clk_id_req: Core clock ID requested to enable/disable
  453. * @enable: enable or disable clock flag
  454. *
  455. * Returns 0 on success or -EINVAL on error.
  456. */
  457. int bolero_clk_rsc_request_clock(struct device *dev,
  458. int default_clk_id,
  459. int clk_id_req,
  460. bool enable)
  461. {
  462. int ret = 0;
  463. struct device *clk_dev = NULL;
  464. struct bolero_clk_rsc *priv = NULL;
  465. bool mux_switch = false;
  466. if (!dev) {
  467. pr_err("%s: dev is null %d\n", __func__);
  468. return -EINVAL;
  469. }
  470. if ((clk_id_req < 0 || clk_id_req >= MAX_CLK) &&
  471. (default_clk_id < 0 || default_clk_id >= MAX_CLK)) {
  472. pr_err("%s: Invalid clk_id_req: %d or default_clk_id: %d\n",
  473. __func__, clk_id_req, default_clk_id);
  474. return -EINVAL;
  475. }
  476. clk_dev = bolero_get_rsc_clk_device_ptr(dev->parent);
  477. if (!clk_dev) {
  478. pr_err("%s: Invalid rsc clk device\n", __func__);
  479. return -EINVAL;
  480. }
  481. priv = dev_get_drvdata(clk_dev);
  482. if (!priv) {
  483. pr_err("%s: Invalid rsc clk priviate data\n", __func__);
  484. return -EINVAL;
  485. }
  486. mutex_lock(&priv->rsc_clk_lock);
  487. if (!priv->dev_up && enable) {
  488. dev_err_ratelimited(priv->dev, "%s: SSR is in progress..\n",
  489. __func__);
  490. ret = -EINVAL;
  491. goto err;
  492. }
  493. priv->default_clk_id[clk_id_req] = default_clk_id;
  494. if (default_clk_id != clk_id_req)
  495. mux_switch = true;
  496. if (mux_switch) {
  497. if (clk_id_req != VA_CORE_CLK) {
  498. ret = bolero_clk_rsc_mux1_clk_request(priv, clk_id_req,
  499. enable);
  500. if (ret < 0)
  501. goto err;
  502. }
  503. } else {
  504. ret = bolero_clk_rsc_mux0_clk_request(priv, clk_id_req, enable);
  505. if (ret < 0)
  506. goto err;
  507. }
  508. ret = bolero_clk_rsc_check_and_update_va_clk(priv, mux_switch,
  509. clk_id_req,
  510. enable);
  511. if (ret < 0)
  512. goto err;
  513. dev_dbg(priv->dev, "%s: clk_cnt: %d for requested clk: %d, enable: %d\n",
  514. __func__, priv->clk_cnt[clk_id_req], clk_id_req,
  515. enable);
  516. mutex_unlock(&priv->rsc_clk_lock);
  517. return 0;
  518. err:
  519. mutex_unlock(&priv->rsc_clk_lock);
  520. return ret;
  521. }
  522. EXPORT_SYMBOL(bolero_clk_rsc_request_clock);
  523. static int bolero_clk_rsc_probe(struct platform_device *pdev)
  524. {
  525. int ret = 0, fs_gen_size, i, j;
  526. const char **clk_name_array;
  527. int clk_cnt;
  528. struct clk *clk;
  529. struct bolero_clk_rsc *priv = NULL;
  530. u32 muxsel = 0;
  531. priv = devm_kzalloc(&pdev->dev, sizeof(struct bolero_clk_rsc),
  532. GFP_KERNEL);
  533. if (!priv)
  534. return -ENOMEM;
  535. /* Get clk fs gen sequence from device tree */
  536. if (!of_find_property(pdev->dev.of_node, "qcom,fs-gen-sequence",
  537. &fs_gen_size)) {
  538. dev_err(&pdev->dev, "%s: unable to find qcom,fs-gen-sequence property\n",
  539. __func__);
  540. ret = -EINVAL;
  541. goto err;
  542. }
  543. priv->num_fs_reg = fs_gen_size/(2 * sizeof(u32));
  544. priv->fs_gen_seq = devm_kzalloc(&pdev->dev, fs_gen_size, GFP_KERNEL);
  545. if (!priv->fs_gen_seq) {
  546. ret = -ENOMEM;
  547. goto err;
  548. }
  549. dev_dbg(&pdev->dev, "%s: num_fs_reg %d\n", __func__, priv->num_fs_reg);
  550. /* Parse fs-gen-sequence */
  551. ret = of_property_read_u32_array(pdev->dev.of_node,
  552. "qcom,fs-gen-sequence",
  553. priv->fs_gen_seq,
  554. priv->num_fs_reg * 2);
  555. if (ret < 0) {
  556. dev_err(&pdev->dev, "%s: unable to parse fs-gen-sequence, ret = %d\n",
  557. __func__, ret);
  558. goto err;
  559. }
  560. /* Get clk details from device tree */
  561. clk_cnt = of_property_count_strings(pdev->dev.of_node, "clock-names");
  562. if (clk_cnt <= 0 || clk_cnt > MAX_CLK) {
  563. dev_err(&pdev->dev, "%s: Invalid number of clocks %d",
  564. __func__, clk_cnt);
  565. ret = -EINVAL;
  566. goto err;
  567. }
  568. clk_name_array = devm_kzalloc(&pdev->dev, clk_cnt * sizeof(char *),
  569. GFP_KERNEL);
  570. if (!clk_name_array) {
  571. ret = -ENOMEM;
  572. goto err;
  573. }
  574. ret = of_property_read_string_array(pdev->dev.of_node, "clock-names",
  575. clk_name_array, clk_cnt);
  576. for (i = 0; i < MAX_CLK; i++) {
  577. priv->clk[i] = NULL;
  578. for (j = 0; j < clk_cnt; j++) {
  579. if (!strcmp(clk_src_name[i], clk_name_array[j])) {
  580. clk = devm_clk_get(&pdev->dev, clk_src_name[i]);
  581. if (IS_ERR(clk)) {
  582. ret = PTR_ERR(clk);
  583. dev_err(&pdev->dev, "%s: clk get failed for %s with ret %d\n",
  584. __func__, clk_src_name[i], ret);
  585. goto err;
  586. }
  587. priv->clk[i] = clk;
  588. dev_dbg(&pdev->dev, "%s: clk get success for clk name %s\n",
  589. __func__, clk_src_name[i]);
  590. }
  591. }
  592. }
  593. ret = of_property_read_u32(pdev->dev.of_node,
  594. "qcom,rx_mclk_mode_muxsel", &muxsel);
  595. if (ret) {
  596. dev_dbg(&pdev->dev, "%s: could not find qcom,rx_mclk_mode_muxsel entry in dt\n",
  597. __func__);
  598. } else {
  599. priv->rx_clk_muxsel = devm_ioremap(&pdev->dev, muxsel, 0x4);
  600. if (!priv->rx_clk_muxsel) {
  601. dev_err(&pdev->dev, "%s: ioremap failed for rx muxsel\n",
  602. __func__);
  603. return -ENOMEM;
  604. }
  605. }
  606. ret = of_property_read_u32(pdev->dev.of_node,
  607. "qcom,wsa_mclk_mode_muxsel", &muxsel);
  608. if (ret) {
  609. dev_dbg(&pdev->dev, "%s: could not find qcom,wsa_mclk_mode_muxsel entry in dt\n",
  610. __func__);
  611. } else {
  612. priv->wsa_clk_muxsel = devm_ioremap(&pdev->dev, muxsel, 0x4);
  613. if (!priv->wsa_clk_muxsel) {
  614. dev_err(&pdev->dev, "%s: ioremap failed for wsa muxsel\n",
  615. __func__);
  616. return -ENOMEM;
  617. }
  618. }
  619. ret = of_property_read_u32(pdev->dev.of_node,
  620. "qcom,va_mclk_mode_muxsel", &muxsel);
  621. if (ret) {
  622. dev_dbg(&pdev->dev, "%s: could not find qcom,va_mclk_mode_muxsel entry in dt\n",
  623. __func__);
  624. } else {
  625. priv->va_clk_muxsel = devm_ioremap(&pdev->dev, muxsel, 0x4);
  626. if (!priv->va_clk_muxsel) {
  627. dev_err(&pdev->dev, "%s: ioremap failed for va muxsel\n",
  628. __func__);
  629. return -ENOMEM;
  630. }
  631. }
  632. ret = bolero_register_res_clk(&pdev->dev, bolero_clk_rsc_cb);
  633. if (ret < 0) {
  634. dev_err(&pdev->dev, "%s: Failed to register cb %d",
  635. __func__, ret);
  636. goto err;
  637. }
  638. priv->dev = &pdev->dev;
  639. priv->dev_up = true;
  640. mutex_init(&priv->rsc_clk_lock);
  641. mutex_init(&priv->fs_gen_lock);
  642. dev_set_drvdata(&pdev->dev, priv);
  643. err:
  644. return ret;
  645. }
  646. static int bolero_clk_rsc_remove(struct platform_device *pdev)
  647. {
  648. struct bolero_clk_rsc *priv = dev_get_drvdata(&pdev->dev);
  649. bolero_unregister_res_clk(&pdev->dev);
  650. of_platform_depopulate(&pdev->dev);
  651. if (!priv)
  652. return -EINVAL;
  653. mutex_destroy(&priv->rsc_clk_lock);
  654. mutex_destroy(&priv->fs_gen_lock);
  655. return 0;
  656. }
  657. static const struct of_device_id bolero_clk_rsc_dt_match[] = {
  658. {.compatible = "qcom,bolero-clk-rsc-mngr"},
  659. {}
  660. };
  661. MODULE_DEVICE_TABLE(of, bolero_clk_rsc_dt_match);
  662. static struct platform_driver bolero_clk_rsc_mgr = {
  663. .driver = {
  664. .name = "bolero-clk-rsc-mngr",
  665. .owner = THIS_MODULE,
  666. .of_match_table = bolero_clk_rsc_dt_match,
  667. .suppress_bind_attrs = true,
  668. },
  669. .probe = bolero_clk_rsc_probe,
  670. .remove = bolero_clk_rsc_remove,
  671. };
  672. int bolero_clk_rsc_mgr_init(void)
  673. {
  674. return platform_driver_register(&bolero_clk_rsc_mgr);
  675. }
  676. void bolero_clk_rsc_mgr_exit(void)
  677. {
  678. platform_driver_unregister(&bolero_clk_rsc_mgr);
  679. }
  680. MODULE_DESCRIPTION("Bolero clock resource manager driver");
  681. MODULE_LICENSE("GPL v2");