bolero-cdc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/of_platform.h>
  5. #include <linux/module.h>
  6. #include <linux/io.h>
  7. #include <linux/init.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/printk.h>
  10. #include <linux/delay.h>
  11. #include <linux/kernel.h>
  12. #include <linux/clk.h>
  13. #include <soc/snd_event.h>
  14. #include <linux/pm_runtime.h>
  15. #include <soc/swr-common.h>
  16. #include "bolero-cdc.h"
  17. #include "internal.h"
  18. #include "bolero-clk-rsc.h"
  19. #define DRV_NAME "bolero_codec"
  20. #define BOLERO_VERSION_ENTRY_SIZE 32
  21. #define BOLERO_CDC_STRING_LEN 80
  22. static const struct snd_soc_component_driver bolero;
  23. /* pm runtime auto suspend timer in msecs */
  24. #define BOLERO_AUTO_SUSPEND_DELAY 100 /* delay in msec */
  25. /* MCLK_MUX table for all macros */
  26. static u16 bolero_mclk_mux_tbl[MAX_MACRO][MCLK_MUX_MAX] = {
  27. {TX_MACRO, VA_MACRO},
  28. {TX_MACRO, RX_MACRO},
  29. {TX_MACRO, WSA_MACRO},
  30. {TX_MACRO, VA_MACRO},
  31. };
  32. static bool bolero_is_valid_codec_dev(struct device *dev);
  33. int bolero_set_port_map(struct snd_soc_component *component,
  34. u32 size, void *data)
  35. {
  36. struct bolero_priv *priv = NULL;
  37. struct swr_mstr_port_map *map = NULL;
  38. u16 idx;
  39. if (!component || (size == 0) || !data)
  40. return -EINVAL;
  41. priv = snd_soc_component_get_drvdata(component);
  42. if (!priv)
  43. return -EINVAL;
  44. if (!bolero_is_valid_codec_dev(priv->dev)) {
  45. dev_err(priv->dev, "%s: invalid codec\n", __func__);
  46. return -EINVAL;
  47. }
  48. map = (struct swr_mstr_port_map *)data;
  49. for (idx = 0; idx < size; idx++) {
  50. if (priv->macro_params[map->id].set_port_map)
  51. priv->macro_params[map->id].set_port_map(component,
  52. map->uc,
  53. SWR_MSTR_PORT_LEN,
  54. map->swr_port_params);
  55. map += 1;
  56. }
  57. return 0;
  58. }
  59. EXPORT_SYMBOL(bolero_set_port_map);
  60. static void bolero_ahb_write_device(char __iomem *io_base,
  61. u16 reg, u8 value)
  62. {
  63. u32 temp = (u32)(value) & 0x000000FF;
  64. iowrite32(temp, io_base + reg);
  65. }
  66. static void bolero_ahb_read_device(char __iomem *io_base,
  67. u16 reg, u8 *value)
  68. {
  69. u32 temp;
  70. temp = ioread32(io_base + reg);
  71. *value = (u8)temp;
  72. }
  73. static int __bolero_reg_read(struct bolero_priv *priv,
  74. u16 macro_id, u16 reg, u8 *val)
  75. {
  76. int ret = 0;
  77. mutex_lock(&priv->clk_lock);
  78. if (!priv->dev_up) {
  79. dev_dbg_ratelimited(priv->dev,
  80. "%s: SSR in progress, exit\n", __func__);
  81. ret = -EINVAL;
  82. goto ssr_err;
  83. }
  84. if (priv->macro_params[VA_MACRO].dev) {
  85. pm_runtime_get_sync(priv->macro_params[VA_MACRO].dev);
  86. if (!bolero_check_core_votes(priv->macro_params[VA_MACRO].dev))
  87. goto ssr_err;
  88. }
  89. if (priv->version < BOLERO_VERSION_2_0) {
  90. /* Request Clk before register access */
  91. ret = bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  92. priv->macro_params[macro_id].default_clk_id,
  93. priv->macro_params[macro_id].clk_id_req,
  94. true);
  95. if (ret < 0) {
  96. dev_err_ratelimited(priv->dev,
  97. "%s: Failed to enable clock, ret:%d\n",
  98. __func__, ret);
  99. goto err;
  100. }
  101. }
  102. bolero_ahb_read_device(
  103. priv->macro_params[macro_id].io_base, reg, val);
  104. if (priv->version < BOLERO_VERSION_2_0)
  105. bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  106. priv->macro_params[macro_id].default_clk_id,
  107. priv->macro_params[macro_id].clk_id_req,
  108. false);
  109. err:
  110. if (priv->macro_params[VA_MACRO].dev) {
  111. pm_runtime_mark_last_busy(priv->macro_params[VA_MACRO].dev);
  112. pm_runtime_put_autosuspend(priv->macro_params[VA_MACRO].dev);
  113. }
  114. ssr_err:
  115. mutex_unlock(&priv->clk_lock);
  116. return ret;
  117. }
  118. static int __bolero_reg_write(struct bolero_priv *priv,
  119. u16 macro_id, u16 reg, u8 val)
  120. {
  121. int ret = 0;
  122. mutex_lock(&priv->clk_lock);
  123. if (!priv->dev_up) {
  124. dev_dbg_ratelimited(priv->dev,
  125. "%s: SSR in progress, exit\n", __func__);
  126. ret = -EINVAL;
  127. goto ssr_err;
  128. }
  129. if (priv->macro_params[VA_MACRO].dev) {
  130. pm_runtime_get_sync(priv->macro_params[VA_MACRO].dev);
  131. if (!bolero_check_core_votes(priv->macro_params[VA_MACRO].dev))
  132. goto ssr_err;
  133. }
  134. if (priv->version < BOLERO_VERSION_2_0) {
  135. /* Request Clk before register access */
  136. ret = bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  137. priv->macro_params[macro_id].default_clk_id,
  138. priv->macro_params[macro_id].clk_id_req,
  139. true);
  140. if (ret < 0) {
  141. dev_err_ratelimited(priv->dev,
  142. "%s: Failed to enable clock, ret:%d\n",
  143. __func__, ret);
  144. goto err;
  145. }
  146. }
  147. bolero_ahb_write_device(
  148. priv->macro_params[macro_id].io_base, reg, val);
  149. if (priv->version < BOLERO_VERSION_2_0)
  150. bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  151. priv->macro_params[macro_id].default_clk_id,
  152. priv->macro_params[macro_id].clk_id_req,
  153. false);
  154. err:
  155. if (priv->macro_params[VA_MACRO].dev) {
  156. pm_runtime_mark_last_busy(priv->macro_params[VA_MACRO].dev);
  157. pm_runtime_put_autosuspend(priv->macro_params[VA_MACRO].dev);
  158. }
  159. ssr_err:
  160. mutex_unlock(&priv->clk_lock);
  161. return ret;
  162. }
  163. static int bolero_cdc_update_wcd_event(void *handle, u16 event, u32 data)
  164. {
  165. struct bolero_priv *priv = (struct bolero_priv *)handle;
  166. if (!priv) {
  167. pr_err("%s:Invalid bolero priv handle\n", __func__);
  168. return -EINVAL;
  169. }
  170. switch (event) {
  171. case WCD_BOLERO_EVT_RX_MUTE:
  172. if (priv->macro_params[RX_MACRO].event_handler)
  173. priv->macro_params[RX_MACRO].event_handler(
  174. priv->component,
  175. BOLERO_MACRO_EVT_RX_MUTE, data);
  176. break;
  177. case WCD_BOLERO_EVT_IMPED_TRUE:
  178. if (priv->macro_params[RX_MACRO].event_handler)
  179. priv->macro_params[RX_MACRO].event_handler(
  180. priv->component,
  181. BOLERO_MACRO_EVT_IMPED_TRUE, data);
  182. break;
  183. case WCD_BOLERO_EVT_IMPED_FALSE:
  184. if (priv->macro_params[RX_MACRO].event_handler)
  185. priv->macro_params[RX_MACRO].event_handler(
  186. priv->component,
  187. BOLERO_MACRO_EVT_IMPED_FALSE, data);
  188. break;
  189. case WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST:
  190. if (priv->macro_params[RX_MACRO].event_handler)
  191. priv->macro_params[RX_MACRO].event_handler(
  192. priv->component,
  193. BOLERO_MACRO_EVT_RX_COMPANDER_SOFT_RST, data);
  194. break;
  195. case WCD_BOLERO_EVT_BCS_CLK_OFF:
  196. if (priv->macro_params[TX_MACRO].event_handler)
  197. priv->macro_params[TX_MACRO].event_handler(
  198. priv->component,
  199. BOLERO_MACRO_EVT_BCS_CLK_OFF, data);
  200. break;
  201. default:
  202. dev_err(priv->dev, "%s: Invalid event %d trigger from wcd\n",
  203. __func__, event);
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. static int bolero_cdc_register_notifier(void *handle,
  209. struct notifier_block *nblock,
  210. bool enable)
  211. {
  212. struct bolero_priv *priv = (struct bolero_priv *)handle;
  213. if (!priv) {
  214. pr_err("%s: bolero priv is null\n", __func__);
  215. return -EINVAL;
  216. }
  217. if (enable)
  218. return blocking_notifier_chain_register(&priv->notifier,
  219. nblock);
  220. return blocking_notifier_chain_unregister(&priv->notifier,
  221. nblock);
  222. }
  223. static void bolero_cdc_notifier_call(struct bolero_priv *priv,
  224. u32 data)
  225. {
  226. dev_dbg(priv->dev, "%s: notifier call, data:%d\n", __func__, data);
  227. blocking_notifier_call_chain(&priv->notifier,
  228. data, (void *)priv->wcd_dev);
  229. }
  230. static bool bolero_is_valid_child_dev(struct device *dev)
  231. {
  232. if (of_device_is_compatible(dev->parent->of_node, "qcom,bolero-codec"))
  233. return true;
  234. return false;
  235. }
  236. static bool bolero_is_valid_codec_dev(struct device *dev)
  237. {
  238. if (of_device_is_compatible(dev->of_node, "qcom,bolero-codec"))
  239. return true;
  240. return false;
  241. }
  242. /**
  243. * bolero_clear_amic_tx_hold - clears AMIC register on analog codec
  244. *
  245. * @dev: bolero device ptr.
  246. *
  247. */
  248. void bolero_clear_amic_tx_hold(struct device *dev, u16 adc_n)
  249. {
  250. struct bolero_priv *priv;
  251. u16 event;
  252. u16 amic = 0;
  253. if (!dev) {
  254. pr_err("%s: dev is null\n", __func__);
  255. return;
  256. }
  257. if (!bolero_is_valid_codec_dev(dev)) {
  258. pr_err("%s: invalid codec\n", __func__);
  259. return;
  260. }
  261. priv = dev_get_drvdata(dev);
  262. if (!priv) {
  263. dev_err(dev, "%s: priv is null\n", __func__);
  264. return;
  265. }
  266. event = BOLERO_WCD_EVT_TX_CH_HOLD_CLEAR;
  267. if (adc_n == BOLERO_ADC0)
  268. amic = 0x1;
  269. else if (adc_n == BOLERO_ADC1)
  270. amic = 0x2;
  271. else if (adc_n == BOLERO_ADC2)
  272. amic = 0x2;
  273. else if (adc_n == BOLERO_ADC3)
  274. amic = 0x3;
  275. else
  276. return;
  277. bolero_cdc_notifier_call(priv, (amic << 0x10 | event));
  278. }
  279. EXPORT_SYMBOL(bolero_clear_amic_tx_hold);
  280. /**
  281. * bolero_get_device_ptr - Get child or macro device ptr
  282. *
  283. * @dev: bolero device ptr.
  284. * @macro_id: ID of macro calling this API.
  285. *
  286. * Returns dev ptr on success or NULL on error.
  287. */
  288. struct device *bolero_get_device_ptr(struct device *dev, u16 macro_id)
  289. {
  290. struct bolero_priv *priv;
  291. if (!dev) {
  292. pr_err("%s: dev is null\n", __func__);
  293. return NULL;
  294. }
  295. if (!bolero_is_valid_codec_dev(dev)) {
  296. pr_err("%s: invalid codec\n", __func__);
  297. return NULL;
  298. }
  299. priv = dev_get_drvdata(dev);
  300. if (!priv || (macro_id >= MAX_MACRO)) {
  301. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  302. return NULL;
  303. }
  304. return priv->macro_params[macro_id].dev;
  305. }
  306. EXPORT_SYMBOL(bolero_get_device_ptr);
  307. /**
  308. * bolero_get_rsc_clk_device_ptr - Get rsc clk device ptr
  309. *
  310. * @dev: bolero device ptr.
  311. *
  312. * Returns dev ptr on success or NULL on error.
  313. */
  314. struct device *bolero_get_rsc_clk_device_ptr(struct device *dev)
  315. {
  316. struct bolero_priv *priv;
  317. if (!dev) {
  318. pr_err("%s: dev is null\n", __func__);
  319. return NULL;
  320. }
  321. if (!bolero_is_valid_codec_dev(dev)) {
  322. pr_err("%s: invalid codec\n", __func__);
  323. return NULL;
  324. }
  325. priv = dev_get_drvdata(dev);
  326. if (!priv) {
  327. dev_err(dev, "%s: priv is null\n", __func__);
  328. return NULL;
  329. }
  330. return priv->clk_dev;
  331. }
  332. EXPORT_SYMBOL(bolero_get_rsc_clk_device_ptr);
  333. static int bolero_copy_dais_from_macro(struct bolero_priv *priv)
  334. {
  335. struct snd_soc_dai_driver *dai_ptr;
  336. u16 macro_idx;
  337. /* memcpy into bolero_dais all macro dais */
  338. if (!priv->bolero_dais)
  339. priv->bolero_dais = devm_kzalloc(priv->dev,
  340. priv->num_dais *
  341. sizeof(
  342. struct snd_soc_dai_driver),
  343. GFP_KERNEL);
  344. if (!priv->bolero_dais)
  345. return -ENOMEM;
  346. dai_ptr = priv->bolero_dais;
  347. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  348. if (priv->macro_params[macro_idx].dai_ptr) {
  349. memcpy(dai_ptr,
  350. priv->macro_params[macro_idx].dai_ptr,
  351. priv->macro_params[macro_idx].num_dais *
  352. sizeof(struct snd_soc_dai_driver));
  353. dai_ptr += priv->macro_params[macro_idx].num_dais;
  354. }
  355. }
  356. return 0;
  357. }
  358. /**
  359. * bolero_register_res_clk - Registers rsc clk driver to bolero
  360. *
  361. * @dev: rsc clk device ptr.
  362. * @rsc_clk_cb: event handler callback for notifications like SSR
  363. *
  364. * Returns 0 on success or -EINVAL on error.
  365. */
  366. int bolero_register_res_clk(struct device *dev, rsc_clk_cb_t rsc_clk_cb)
  367. {
  368. struct bolero_priv *priv;
  369. if (!dev || !rsc_clk_cb) {
  370. pr_err("%s: dev or rsc_clk_cb is null\n", __func__);
  371. return -EINVAL;
  372. }
  373. if (!bolero_is_valid_child_dev(dev)) {
  374. dev_err(dev, "%s: child device :%pK not added yet\n",
  375. __func__, dev);
  376. return -EINVAL;
  377. }
  378. priv = dev_get_drvdata(dev->parent);
  379. if (!priv) {
  380. dev_err(dev, "%s: priv is null\n", __func__);
  381. return -EINVAL;
  382. }
  383. priv->clk_dev = dev;
  384. priv->rsc_clk_cb = rsc_clk_cb;
  385. return 0;
  386. }
  387. EXPORT_SYMBOL(bolero_register_res_clk);
  388. /**
  389. * bolero_unregister_res_clk - Unregisters rsc clk driver from bolero
  390. *
  391. * @dev: resource clk device ptr.
  392. */
  393. void bolero_unregister_res_clk(struct device *dev)
  394. {
  395. struct bolero_priv *priv;
  396. if (!dev) {
  397. pr_err("%s: dev is NULL\n", __func__);
  398. return;
  399. }
  400. if (!bolero_is_valid_child_dev(dev)) {
  401. dev_err(dev, "%s: child device :%pK not added\n",
  402. __func__, dev);
  403. return;
  404. }
  405. priv = dev_get_drvdata(dev->parent);
  406. if (!priv) {
  407. dev_err(dev, "%s: priv is null\n", __func__);
  408. return;
  409. }
  410. priv->clk_dev = NULL;
  411. priv->rsc_clk_cb = NULL;
  412. }
  413. EXPORT_SYMBOL(bolero_unregister_res_clk);
  414. static u8 bolero_dmic_clk_div_get(struct snd_soc_component *component,
  415. int mode)
  416. {
  417. struct bolero_priv* priv = snd_soc_component_get_drvdata(component);
  418. int macro = (mode ? VA_MACRO : TX_MACRO);
  419. int ret = 0;
  420. if (priv->macro_params[macro].clk_div_get) {
  421. ret = priv->macro_params[macro].clk_div_get(component);
  422. if (ret > 0)
  423. return ret;
  424. }
  425. return 1;
  426. }
  427. int bolero_dmic_clk_enable(struct snd_soc_component *component,
  428. u32 dmic, u32 tx_mode, bool enable)
  429. {
  430. struct bolero_priv* priv = snd_soc_component_get_drvdata(component);
  431. u8 dmic_clk_en = 0x01;
  432. u16 dmic_clk_reg = 0;
  433. s32 *dmic_clk_cnt = NULL;
  434. u8 *dmic_clk_div = NULL;
  435. u8 freq_change_mask = 0;
  436. u8 clk_div = 0;
  437. dev_dbg(component->dev, "%s: enable: %d, tx_mode:%d, dmic: %d\n",
  438. __func__, enable, tx_mode, dmic);
  439. switch (dmic) {
  440. case 0:
  441. case 1:
  442. dmic_clk_cnt = &(priv->dmic_0_1_clk_cnt);
  443. dmic_clk_div = &(priv->dmic_0_1_clk_div);
  444. dmic_clk_reg = BOLERO_CDC_VA_TOP_CSR_DMIC0_CTL;
  445. freq_change_mask = 0x01;
  446. break;
  447. case 2:
  448. case 3:
  449. dmic_clk_cnt = &(priv->dmic_2_3_clk_cnt);
  450. dmic_clk_div = &(priv->dmic_2_3_clk_div);
  451. dmic_clk_reg = BOLERO_CDC_VA_TOP_CSR_DMIC1_CTL;
  452. freq_change_mask = 0x02;
  453. break;
  454. case 4:
  455. case 5:
  456. dmic_clk_cnt = &(priv->dmic_4_5_clk_cnt);
  457. dmic_clk_div = &(priv->dmic_4_5_clk_div);
  458. dmic_clk_reg = BOLERO_CDC_VA_TOP_CSR_DMIC2_CTL;
  459. freq_change_mask = 0x04;
  460. break;
  461. case 6:
  462. case 7:
  463. dmic_clk_cnt = &(priv->dmic_6_7_clk_cnt);
  464. dmic_clk_div = &(priv->dmic_6_7_clk_div);
  465. dmic_clk_reg = BOLERO_CDC_VA_TOP_CSR_DMIC3_CTL;
  466. freq_change_mask = 0x08;
  467. break;
  468. default:
  469. dev_err(component->dev, "%s: Invalid DMIC Selection\n",
  470. __func__);
  471. return -EINVAL;
  472. }
  473. dev_dbg(component->dev, "%s: DMIC%d dmic_clk_cnt %d\n",
  474. __func__, dmic, *dmic_clk_cnt);
  475. if (enable) {
  476. clk_div = bolero_dmic_clk_div_get(component, tx_mode);
  477. (*dmic_clk_cnt)++;
  478. if (*dmic_clk_cnt == 1) {
  479. snd_soc_component_update_bits(component,
  480. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  481. 0x80, 0x00);
  482. snd_soc_component_update_bits(component, dmic_clk_reg,
  483. 0x0E, clk_div << 0x1);
  484. snd_soc_component_update_bits(component, dmic_clk_reg,
  485. dmic_clk_en, dmic_clk_en);
  486. } else {
  487. if (*dmic_clk_div > clk_div) {
  488. snd_soc_component_update_bits(component,
  489. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  490. freq_change_mask, freq_change_mask);
  491. snd_soc_component_update_bits(component, dmic_clk_reg,
  492. 0x0E, clk_div << 0x1);
  493. snd_soc_component_update_bits(component,
  494. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  495. freq_change_mask, 0x00);
  496. } else {
  497. clk_div = *dmic_clk_div;
  498. }
  499. }
  500. *dmic_clk_div = clk_div;
  501. } else {
  502. (*dmic_clk_cnt)--;
  503. if (*dmic_clk_cnt == 0) {
  504. snd_soc_component_update_bits(component, dmic_clk_reg,
  505. dmic_clk_en, 0);
  506. clk_div = 0;
  507. snd_soc_component_update_bits(component, dmic_clk_reg,
  508. 0x0E, clk_div << 0x1);
  509. } else {
  510. clk_div = bolero_dmic_clk_div_get(component, tx_mode);
  511. if (*dmic_clk_div > clk_div) {
  512. clk_div = bolero_dmic_clk_div_get(component, !tx_mode);
  513. snd_soc_component_update_bits(component,
  514. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  515. freq_change_mask, freq_change_mask);
  516. snd_soc_component_update_bits(component, dmic_clk_reg,
  517. 0x0E, clk_div << 0x1);
  518. snd_soc_component_update_bits(component,
  519. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  520. freq_change_mask, 0x00);
  521. } else {
  522. clk_div = *dmic_clk_div;
  523. }
  524. }
  525. *dmic_clk_div = clk_div;
  526. }
  527. return 0;
  528. }
  529. EXPORT_SYMBOL(bolero_dmic_clk_enable);
  530. /**
  531. * bolero_register_macro - Registers macro to bolero
  532. *
  533. * @dev: macro device ptr.
  534. * @macro_id: ID of macro calling this API.
  535. * @ops: macro params to register.
  536. *
  537. * Returns 0 on success or -EINVAL on error.
  538. */
  539. int bolero_register_macro(struct device *dev, u16 macro_id,
  540. struct macro_ops *ops)
  541. {
  542. struct bolero_priv *priv;
  543. int ret = -EINVAL;
  544. if (!dev || !ops) {
  545. pr_err("%s: dev or ops is null\n", __func__);
  546. return -EINVAL;
  547. }
  548. if (!bolero_is_valid_child_dev(dev)) {
  549. dev_err(dev, "%s: child device for macro:%d not added yet\n",
  550. __func__, macro_id);
  551. return -EINVAL;
  552. }
  553. priv = dev_get_drvdata(dev->parent);
  554. if (!priv || (macro_id >= MAX_MACRO)) {
  555. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  556. return -EINVAL;
  557. }
  558. priv->macro_params[macro_id].clk_id_req = ops->clk_id_req;
  559. priv->macro_params[macro_id].default_clk_id = ops->default_clk_id;
  560. priv->macro_params[macro_id].init = ops->init;
  561. priv->macro_params[macro_id].exit = ops->exit;
  562. priv->macro_params[macro_id].io_base = ops->io_base;
  563. priv->macro_params[macro_id].num_dais = ops->num_dais;
  564. priv->macro_params[macro_id].dai_ptr = ops->dai_ptr;
  565. priv->macro_params[macro_id].event_handler = ops->event_handler;
  566. priv->macro_params[macro_id].set_port_map = ops->set_port_map;
  567. priv->macro_params[macro_id].dev = dev;
  568. priv->current_mclk_mux_macro[macro_id] =
  569. bolero_mclk_mux_tbl[macro_id][MCLK_MUX0];
  570. if (macro_id == TX_MACRO) {
  571. priv->macro_params[macro_id].reg_wake_irq = ops->reg_wake_irq;
  572. priv->macro_params[macro_id].clk_switch = ops->clk_switch;
  573. priv->macro_params[macro_id].reg_evt_listener =
  574. ops->reg_evt_listener;
  575. }
  576. if (macro_id == TX_MACRO || macro_id == VA_MACRO)
  577. priv->macro_params[macro_id].clk_div_get = ops->clk_div_get;
  578. if (priv->version == BOLERO_VERSION_2_1) {
  579. if (macro_id == VA_MACRO)
  580. priv->macro_params[macro_id].reg_wake_irq =
  581. ops->reg_wake_irq;
  582. }
  583. priv->num_dais += ops->num_dais;
  584. priv->num_macros_registered++;
  585. priv->macros_supported[macro_id] = true;
  586. dev_dbg(dev, "%s: register macro successful:%d\n", macro_id);
  587. if (priv->num_macros_registered == priv->num_macros) {
  588. ret = bolero_copy_dais_from_macro(priv);
  589. if (ret < 0) {
  590. dev_err(dev, "%s: copy_dais failed\n", __func__);
  591. return ret;
  592. }
  593. if (priv->macros_supported[TX_MACRO] == false) {
  594. bolero_mclk_mux_tbl[WSA_MACRO][MCLK_MUX0] = WSA_MACRO;
  595. priv->current_mclk_mux_macro[WSA_MACRO] = WSA_MACRO;
  596. bolero_mclk_mux_tbl[VA_MACRO][MCLK_MUX0] = VA_MACRO;
  597. priv->current_mclk_mux_macro[VA_MACRO] = VA_MACRO;
  598. }
  599. ret = snd_soc_register_component(dev->parent, &bolero,
  600. priv->bolero_dais, priv->num_dais);
  601. if (ret < 0) {
  602. dev_err(dev, "%s: register codec failed\n", __func__);
  603. return ret;
  604. }
  605. }
  606. return 0;
  607. }
  608. EXPORT_SYMBOL(bolero_register_macro);
  609. /**
  610. * bolero_unregister_macro - De-Register macro from bolero
  611. *
  612. * @dev: macro device ptr.
  613. * @macro_id: ID of macro calling this API.
  614. *
  615. */
  616. void bolero_unregister_macro(struct device *dev, u16 macro_id)
  617. {
  618. struct bolero_priv *priv;
  619. if (!dev) {
  620. pr_err("%s: dev is null\n", __func__);
  621. return;
  622. }
  623. if (!bolero_is_valid_child_dev(dev)) {
  624. dev_err(dev, "%s: macro:%d not in valid registered macro-list\n",
  625. __func__, macro_id);
  626. return;
  627. }
  628. priv = dev_get_drvdata(dev->parent);
  629. if (!priv || (macro_id >= MAX_MACRO)) {
  630. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  631. return;
  632. }
  633. priv->macro_params[macro_id].init = NULL;
  634. priv->macro_params[macro_id].num_dais = 0;
  635. priv->macro_params[macro_id].dai_ptr = NULL;
  636. priv->macro_params[macro_id].event_handler = NULL;
  637. priv->macro_params[macro_id].dev = NULL;
  638. if (macro_id == TX_MACRO) {
  639. priv->macro_params[macro_id].reg_wake_irq = NULL;
  640. priv->macro_params[macro_id].clk_switch = NULL;
  641. priv->macro_params[macro_id].reg_evt_listener = NULL;
  642. }
  643. if (macro_id == TX_MACRO || macro_id == VA_MACRO)
  644. priv->macro_params[macro_id].clk_div_get = NULL;
  645. priv->num_dais -= priv->macro_params[macro_id].num_dais;
  646. priv->num_macros_registered--;
  647. /* UNREGISTER CODEC HERE */
  648. if (priv->num_macros - 1 == priv->num_macros_registered)
  649. snd_soc_unregister_component(dev->parent);
  650. }
  651. EXPORT_SYMBOL(bolero_unregister_macro);
  652. void bolero_wsa_pa_on(struct device *dev)
  653. {
  654. struct bolero_priv *priv;
  655. if (!dev) {
  656. pr_err("%s: dev is null\n", __func__);
  657. return;
  658. }
  659. if (!bolero_is_valid_child_dev(dev)) {
  660. dev_err(dev, "%s: not a valid child dev\n",
  661. __func__);
  662. return;
  663. }
  664. priv = dev_get_drvdata(dev->parent);
  665. if (!priv) {
  666. dev_err(dev, "%s: priv is null\n", __func__);
  667. return;
  668. }
  669. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_PA_ON_POST_FSCLK);
  670. }
  671. EXPORT_SYMBOL(bolero_wsa_pa_on);
  672. int bolero_get_version(struct device *dev)
  673. {
  674. struct bolero_priv *priv;
  675. if (!dev) {
  676. pr_err("%s: dev is null\n", __func__);
  677. return -EINVAL;
  678. }
  679. if (!bolero_is_valid_child_dev(dev)) {
  680. dev_err(dev, "%s: child device for macro not added yet\n",
  681. __func__);
  682. return -EINVAL;
  683. }
  684. priv = dev_get_drvdata(dev->parent);
  685. if (!priv) {
  686. dev_err(dev, "%s: priv is null\n", __func__);
  687. return -EINVAL;
  688. }
  689. return priv->version;
  690. }
  691. EXPORT_SYMBOL(bolero_get_version);
  692. static ssize_t bolero_version_read(struct snd_info_entry *entry,
  693. void *file_private_data,
  694. struct file *file,
  695. char __user *buf, size_t count,
  696. loff_t pos)
  697. {
  698. struct bolero_priv *priv;
  699. char buffer[BOLERO_VERSION_ENTRY_SIZE];
  700. int len = 0;
  701. priv = (struct bolero_priv *) entry->private_data;
  702. if (!priv) {
  703. pr_err("%s: bolero priv is null\n", __func__);
  704. return -EINVAL;
  705. }
  706. switch (priv->version) {
  707. case BOLERO_VERSION_1_0:
  708. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_0\n");
  709. break;
  710. case BOLERO_VERSION_1_1:
  711. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_1\n");
  712. break;
  713. case BOLERO_VERSION_1_2:
  714. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_2\n");
  715. break;
  716. case BOLERO_VERSION_2_1:
  717. len = snprintf(buffer, sizeof(buffer), "BOLERO_2_1\n");
  718. break;
  719. default:
  720. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  721. }
  722. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  723. }
  724. static int bolero_ssr_enable(struct device *dev, void *data)
  725. {
  726. struct bolero_priv *priv = data;
  727. int macro_idx;
  728. if (priv->initial_boot) {
  729. priv->initial_boot = false;
  730. return 0;
  731. }
  732. if (priv->rsc_clk_cb)
  733. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_UP);
  734. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  735. if (priv->macro_params[macro_idx].event_handler)
  736. priv->macro_params[macro_idx].event_handler(
  737. priv->component,
  738. BOLERO_MACRO_EVT_CLK_RESET, 0x0);
  739. }
  740. regcache_cache_only(priv->regmap, false);
  741. mutex_lock(&priv->clk_lock);
  742. priv->dev_up = true;
  743. mutex_unlock(&priv->clk_lock);
  744. regcache_mark_dirty(priv->regmap);
  745. bolero_clk_rsc_enable_all_clocks(priv->clk_dev, true);
  746. regcache_sync(priv->regmap);
  747. /* Add a 100usec sleep to ensure last register write is done */
  748. usleep_range(100,110);
  749. bolero_clk_rsc_enable_all_clocks(priv->clk_dev, false);
  750. /* call ssr event for supported macros */
  751. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  752. if (!priv->macro_params[macro_idx].event_handler)
  753. continue;
  754. priv->macro_params[macro_idx].event_handler(
  755. priv->component,
  756. BOLERO_MACRO_EVT_SSR_UP, 0x0);
  757. }
  758. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_UP);
  759. return 0;
  760. }
  761. static void bolero_ssr_disable(struct device *dev, void *data)
  762. {
  763. struct bolero_priv *priv = data;
  764. int macro_idx;
  765. if (!priv->dev_up) {
  766. dev_err_ratelimited(priv->dev,
  767. "%s: already disabled\n", __func__);
  768. return;
  769. }
  770. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_PA_OFF_PRE_SSR);
  771. regcache_cache_only(priv->regmap, true);
  772. mutex_lock(&priv->clk_lock);
  773. priv->dev_up = false;
  774. mutex_unlock(&priv->clk_lock);
  775. if (priv->rsc_clk_cb)
  776. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_DOWN);
  777. /* call ssr event for supported macros */
  778. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  779. if (!priv->macro_params[macro_idx].event_handler)
  780. continue;
  781. priv->macro_params[macro_idx].event_handler(
  782. priv->component,
  783. BOLERO_MACRO_EVT_SSR_DOWN, 0x0);
  784. }
  785. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_DOWN);
  786. }
  787. static struct snd_info_entry_ops bolero_info_ops = {
  788. .read = bolero_version_read,
  789. };
  790. static const struct snd_event_ops bolero_ssr_ops = {
  791. .enable = bolero_ssr_enable,
  792. .disable = bolero_ssr_disable,
  793. };
  794. /*
  795. * bolero_info_create_codec_entry - creates bolero module
  796. * @codec_root: The parent directory
  797. * @component: Codec component instance
  798. *
  799. * Creates bolero module and version entry under the given
  800. * parent directory.
  801. *
  802. * Return: 0 on success or negative error code on failure.
  803. */
  804. int bolero_info_create_codec_entry(struct snd_info_entry *codec_root,
  805. struct snd_soc_component *component)
  806. {
  807. struct snd_info_entry *version_entry;
  808. struct bolero_priv *priv;
  809. struct snd_soc_card *card;
  810. if (!codec_root || !component)
  811. return -EINVAL;
  812. priv = snd_soc_component_get_drvdata(component);
  813. if (priv->entry) {
  814. dev_dbg(priv->dev,
  815. "%s:bolero module already created\n", __func__);
  816. return 0;
  817. }
  818. card = component->card;
  819. priv->entry = snd_info_create_module_entry(codec_root->module,
  820. "bolero", codec_root);
  821. if (!priv->entry) {
  822. dev_dbg(component->dev, "%s: failed to create bolero entry\n",
  823. __func__);
  824. return -ENOMEM;
  825. }
  826. priv->entry->mode = S_IFDIR | 0555;
  827. if (snd_info_register(priv->entry) < 0) {
  828. snd_info_free_entry(priv->entry);
  829. return -ENOMEM;
  830. }
  831. version_entry = snd_info_create_card_entry(card->snd_card,
  832. "version",
  833. priv->entry);
  834. if (!version_entry) {
  835. dev_err(component->dev, "%s: failed to create bolero version entry\n",
  836. __func__);
  837. snd_info_free_entry(priv->entry);
  838. return -ENOMEM;
  839. }
  840. version_entry->private_data = priv;
  841. version_entry->size = BOLERO_VERSION_ENTRY_SIZE;
  842. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  843. version_entry->c.ops = &bolero_info_ops;
  844. if (snd_info_register(version_entry) < 0) {
  845. snd_info_free_entry(version_entry);
  846. snd_info_free_entry(priv->entry);
  847. return -ENOMEM;
  848. }
  849. priv->version_entry = version_entry;
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(bolero_info_create_codec_entry);
  853. /**
  854. * bolero_register_wake_irq - Register wake irq of Tx macro
  855. *
  856. * @component: codec component ptr.
  857. * @ipc_wakeup: bool to identify ipc_wakeup to be used or HW interrupt line.
  858. *
  859. * Return: 0 on success or negative error code on failure.
  860. */
  861. int bolero_register_wake_irq(struct snd_soc_component *component,
  862. u32 ipc_wakeup)
  863. {
  864. struct bolero_priv *priv = NULL;
  865. if (!component)
  866. return -EINVAL;
  867. priv = snd_soc_component_get_drvdata(component);
  868. if (!priv)
  869. return -EINVAL;
  870. if (!bolero_is_valid_codec_dev(priv->dev)) {
  871. dev_err(component->dev, "%s: invalid codec\n", __func__);
  872. return -EINVAL;
  873. }
  874. if (priv->version == BOLERO_VERSION_2_1) {
  875. if (priv->macro_params[VA_MACRO].reg_wake_irq)
  876. priv->macro_params[VA_MACRO].reg_wake_irq(
  877. component, ipc_wakeup);
  878. } else {
  879. if (priv->macro_params[TX_MACRO].reg_wake_irq)
  880. priv->macro_params[TX_MACRO].reg_wake_irq(
  881. component, ipc_wakeup);
  882. }
  883. return 0;
  884. }
  885. EXPORT_SYMBOL(bolero_register_wake_irq);
  886. /**
  887. * bolero_tx_clk_switch - Switch tx macro clock
  888. *
  889. * @component: pointer to codec component instance.
  890. *
  891. * Returns 0 on success or -EINVAL on error.
  892. */
  893. int bolero_tx_clk_switch(struct snd_soc_component *component)
  894. {
  895. struct bolero_priv *priv = NULL;
  896. int ret = 0;
  897. if (!component)
  898. return -EINVAL;
  899. priv = snd_soc_component_get_drvdata(component);
  900. if (!priv)
  901. return -EINVAL;
  902. if (!bolero_is_valid_codec_dev(priv->dev)) {
  903. dev_err(component->dev, "%s: invalid codec\n", __func__);
  904. return -EINVAL;
  905. }
  906. if (priv->macro_params[TX_MACRO].clk_switch)
  907. ret = priv->macro_params[TX_MACRO].clk_switch(component);
  908. return ret;
  909. }
  910. EXPORT_SYMBOL(bolero_tx_clk_switch);
  911. /**
  912. * bolero_register_event_listener - Register/Deregister to event listener
  913. *
  914. * @component: pointer to codec component instance.
  915. * @enable: when set to 1 registers to event listener otherwise, derigisters
  916. * from the event listener
  917. *
  918. * Returns 0 on success or -EINVAL on error.
  919. */
  920. int bolero_register_event_listener(struct snd_soc_component *component,
  921. bool enable)
  922. {
  923. struct bolero_priv *priv = NULL;
  924. int ret = 0;
  925. if (!component)
  926. return -EINVAL;
  927. priv = snd_soc_component_get_drvdata(component);
  928. if (!priv)
  929. return -EINVAL;
  930. if (!bolero_is_valid_codec_dev(priv->dev)) {
  931. dev_err(component->dev, "%s: invalid codec\n", __func__);
  932. return -EINVAL;
  933. }
  934. if (priv->macro_params[TX_MACRO].reg_evt_listener)
  935. ret = priv->macro_params[TX_MACRO].reg_evt_listener(component,
  936. enable);
  937. return ret;
  938. }
  939. EXPORT_SYMBOL(bolero_register_event_listener);
  940. static int bolero_soc_codec_probe(struct snd_soc_component *component)
  941. {
  942. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  943. int macro_idx, ret = 0;
  944. u8 core_id_0 = 0, core_id_1 = 0;
  945. snd_soc_component_init_regmap(component, priv->regmap);
  946. if (!priv->version) {
  947. /*
  948. * In order for the ADIE RTC to differentiate between targets
  949. * version info is used.
  950. * Assign 1.0 for target with only one macro
  951. * Assign 1.1 for target with two macros
  952. * Assign 1.2 for target with more than two macros
  953. */
  954. if (priv->num_macros_registered == 1)
  955. priv->version = BOLERO_VERSION_1_0;
  956. else if (priv->num_macros_registered == 2)
  957. priv->version = BOLERO_VERSION_1_1;
  958. else if (priv->num_macros_registered > 2)
  959. priv->version = BOLERO_VERSION_1_2;
  960. }
  961. /* Assign bolero version */
  962. core_id_0 = snd_soc_component_read32(component,
  963. BOLERO_CDC_VA_TOP_CSR_CORE_ID_0);
  964. core_id_1 = snd_soc_component_read32(component,
  965. BOLERO_CDC_VA_TOP_CSR_CORE_ID_1);
  966. if ((core_id_0 == 0x01) && (core_id_1 == 0x0F))
  967. priv->version = BOLERO_VERSION_2_0;
  968. if ((core_id_0 == 0x02) && (core_id_1 == 0x0E))
  969. priv->version = BOLERO_VERSION_2_1;
  970. /* call init for supported macros */
  971. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  972. if (priv->macro_params[macro_idx].init) {
  973. ret = priv->macro_params[macro_idx].init(component);
  974. if (ret < 0) {
  975. dev_err(component->dev,
  976. "%s: init for macro %d failed\n",
  977. __func__, macro_idx);
  978. goto err;
  979. }
  980. }
  981. }
  982. priv->component = component;
  983. ret = snd_event_client_register(priv->dev, &bolero_ssr_ops, priv);
  984. if (!ret) {
  985. snd_event_notify(priv->dev, SND_EVENT_UP);
  986. } else {
  987. dev_err(component->dev,
  988. "%s: Registration with SND event FWK failed ret = %d\n",
  989. __func__, ret);
  990. goto err;
  991. }
  992. dev_dbg(component->dev, "%s: bolero soc codec probe success\n",
  993. __func__);
  994. err:
  995. return ret;
  996. }
  997. static void bolero_soc_codec_remove(struct snd_soc_component *component)
  998. {
  999. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  1000. int macro_idx;
  1001. snd_event_client_deregister(priv->dev);
  1002. /* call exit for supported macros */
  1003. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++)
  1004. if (priv->macro_params[macro_idx].exit)
  1005. priv->macro_params[macro_idx].exit(component);
  1006. return;
  1007. }
  1008. static const struct snd_soc_component_driver bolero = {
  1009. .name = DRV_NAME,
  1010. .probe = bolero_soc_codec_probe,
  1011. .remove = bolero_soc_codec_remove,
  1012. };
  1013. static void bolero_add_child_devices(struct work_struct *work)
  1014. {
  1015. struct bolero_priv *priv;
  1016. bool split_codec = false;
  1017. struct platform_device *pdev;
  1018. struct device_node *node;
  1019. int ret = 0, count = 0;
  1020. struct wcd_ctrl_platform_data *platdata = NULL;
  1021. char plat_dev_name[BOLERO_CDC_STRING_LEN] = "";
  1022. priv = container_of(work, struct bolero_priv,
  1023. bolero_add_child_devices_work);
  1024. if (!priv) {
  1025. pr_err("%s: Memory for bolero priv does not exist\n",
  1026. __func__);
  1027. return;
  1028. }
  1029. if (!priv->dev || !priv->dev->of_node) {
  1030. dev_err(priv->dev, "%s: DT node for bolero does not exist\n",
  1031. __func__);
  1032. return;
  1033. }
  1034. platdata = &priv->plat_data;
  1035. priv->child_count = 0;
  1036. for_each_available_child_of_node(priv->dev->of_node, node) {
  1037. split_codec = false;
  1038. if (of_find_property(node, "qcom,split-codec", NULL)) {
  1039. split_codec = true;
  1040. dev_dbg(priv->dev, "%s: split codec slave exists\n",
  1041. __func__);
  1042. }
  1043. strlcpy(plat_dev_name, node->name,
  1044. (BOLERO_CDC_STRING_LEN - 1));
  1045. pdev = platform_device_alloc(plat_dev_name, -1);
  1046. if (!pdev) {
  1047. dev_err(priv->dev, "%s: pdev memory alloc failed\n",
  1048. __func__);
  1049. ret = -ENOMEM;
  1050. goto err;
  1051. }
  1052. pdev->dev.parent = priv->dev;
  1053. pdev->dev.of_node = node;
  1054. priv->dev->platform_data = platdata;
  1055. if (split_codec)
  1056. priv->wcd_dev = &pdev->dev;
  1057. ret = platform_device_add(pdev);
  1058. if (ret) {
  1059. dev_err(&pdev->dev,
  1060. "%s: Cannot add platform device\n",
  1061. __func__);
  1062. platform_device_put(pdev);
  1063. goto fail_pdev_add;
  1064. }
  1065. if (priv->child_count < BOLERO_CDC_CHILD_DEVICES_MAX)
  1066. priv->pdev_child_devices[priv->child_count++] = pdev;
  1067. else
  1068. goto err;
  1069. }
  1070. return;
  1071. fail_pdev_add:
  1072. for (count = 0; count < priv->child_count; count++)
  1073. platform_device_put(priv->pdev_child_devices[count]);
  1074. err:
  1075. return;
  1076. }
  1077. static int bolero_probe(struct platform_device *pdev)
  1078. {
  1079. struct bolero_priv *priv;
  1080. u32 num_macros = 0;
  1081. int ret;
  1082. struct clk *lpass_core_hw_vote = NULL;
  1083. struct clk *lpass_audio_hw_vote = NULL;
  1084. priv = devm_kzalloc(&pdev->dev, sizeof(struct bolero_priv),
  1085. GFP_KERNEL);
  1086. if (!priv)
  1087. return -ENOMEM;
  1088. ret = of_property_read_u32(pdev->dev.of_node, "qcom,num-macros",
  1089. &num_macros);
  1090. if (ret) {
  1091. dev_err(&pdev->dev,
  1092. "%s:num-macros property not found\n",
  1093. __func__);
  1094. return ret;
  1095. }
  1096. priv->num_macros = num_macros;
  1097. if (priv->num_macros > MAX_MACRO) {
  1098. dev_err(&pdev->dev,
  1099. "%s:num_macros(%d) > MAX_MACRO(%d) than supported\n",
  1100. __func__, priv->num_macros, MAX_MACRO);
  1101. return -EINVAL;
  1102. }
  1103. priv->va_without_decimation = of_property_read_bool(pdev->dev.of_node,
  1104. "qcom,va-without-decimation");
  1105. if (priv->va_without_decimation)
  1106. bolero_reg_access[VA_MACRO] = bolero_va_top_reg_access;
  1107. ret = of_property_read_u32(pdev->dev.of_node,
  1108. "qcom,bolero-version", &priv->version);
  1109. if (ret) {
  1110. dev_dbg(&pdev->dev, "%s:bolero version not specified\n",
  1111. __func__);
  1112. ret = 0;
  1113. }
  1114. if (priv->version == BOLERO_VERSION_2_1) {
  1115. bolero_reg_access[TX_MACRO] = bolero_tx_reg_access_v2;
  1116. bolero_reg_access[VA_MACRO] = bolero_va_reg_access_v2;
  1117. } else if (priv->version == BOLERO_VERSION_2_0) {
  1118. bolero_reg_access[VA_MACRO] = bolero_va_reg_access_v3;
  1119. }
  1120. priv->dev = &pdev->dev;
  1121. priv->dev_up = true;
  1122. priv->initial_boot = true;
  1123. priv->regmap = bolero_regmap_init(priv->dev,
  1124. &bolero_regmap_config);
  1125. if (IS_ERR_OR_NULL((void *)(priv->regmap))) {
  1126. dev_err(&pdev->dev, "%s:regmap init failed\n", __func__);
  1127. return -EINVAL;
  1128. }
  1129. priv->read_dev = __bolero_reg_read;
  1130. priv->write_dev = __bolero_reg_write;
  1131. priv->plat_data.handle = (void *) priv;
  1132. priv->plat_data.update_wcd_event = bolero_cdc_update_wcd_event;
  1133. priv->plat_data.register_notifier = bolero_cdc_register_notifier;
  1134. priv->core_hw_vote_count = 0;
  1135. priv->core_audio_vote_count = 0;
  1136. dev_set_drvdata(&pdev->dev, priv);
  1137. mutex_init(&priv->io_lock);
  1138. mutex_init(&priv->clk_lock);
  1139. mutex_init(&priv->vote_lock);
  1140. INIT_WORK(&priv->bolero_add_child_devices_work,
  1141. bolero_add_child_devices);
  1142. schedule_work(&priv->bolero_add_child_devices_work);
  1143. /* Register LPASS core hw vote */
  1144. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  1145. if (IS_ERR(lpass_core_hw_vote)) {
  1146. ret = PTR_ERR(lpass_core_hw_vote);
  1147. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  1148. __func__, "lpass_core_hw_vote", ret);
  1149. lpass_core_hw_vote = NULL;
  1150. ret = 0;
  1151. }
  1152. priv->lpass_core_hw_vote = lpass_core_hw_vote;
  1153. /* Register LPASS audio hw vote */
  1154. lpass_audio_hw_vote = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  1155. if (IS_ERR(lpass_audio_hw_vote)) {
  1156. ret = PTR_ERR(lpass_audio_hw_vote);
  1157. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  1158. __func__, "lpass_audio_hw_vote", ret);
  1159. lpass_audio_hw_vote = NULL;
  1160. ret = 0;
  1161. }
  1162. priv->lpass_audio_hw_vote = lpass_audio_hw_vote;
  1163. return 0;
  1164. }
  1165. static int bolero_remove(struct platform_device *pdev)
  1166. {
  1167. struct bolero_priv *priv = dev_get_drvdata(&pdev->dev);
  1168. if (!priv)
  1169. return -EINVAL;
  1170. of_platform_depopulate(&pdev->dev);
  1171. mutex_destroy(&priv->io_lock);
  1172. mutex_destroy(&priv->clk_lock);
  1173. mutex_destroy(&priv->vote_lock);
  1174. return 0;
  1175. }
  1176. #ifdef CONFIG_PM
  1177. int bolero_runtime_resume(struct device *dev)
  1178. {
  1179. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1180. int ret = 0;
  1181. mutex_lock(&priv->vote_lock);
  1182. if (priv->lpass_core_hw_vote == NULL) {
  1183. dev_dbg(dev, "%s: Invalid lpass core hw node\n", __func__);
  1184. goto audio_vote;
  1185. }
  1186. if (priv->core_hw_vote_count == 0) {
  1187. ret = clk_prepare_enable(priv->lpass_core_hw_vote);
  1188. if (ret < 0) {
  1189. dev_err(dev, "%s:lpass core hw enable failed\n",
  1190. __func__);
  1191. goto audio_vote;
  1192. }
  1193. }
  1194. priv->core_hw_vote_count++;
  1195. audio_vote:
  1196. if (priv->lpass_audio_hw_vote == NULL) {
  1197. dev_dbg(dev, "%s: Invalid lpass audio hw node\n", __func__);
  1198. goto done;
  1199. }
  1200. if (priv->core_audio_vote_count == 0) {
  1201. ret = clk_prepare_enable(priv->lpass_audio_hw_vote);
  1202. if (ret < 0) {
  1203. dev_err(dev, "%s:lpass audio hw enable failed\n",
  1204. __func__);
  1205. goto done;
  1206. }
  1207. }
  1208. priv->core_audio_vote_count++;
  1209. done:
  1210. mutex_unlock(&priv->vote_lock);
  1211. pm_runtime_set_autosuspend_delay(priv->dev, BOLERO_AUTO_SUSPEND_DELAY);
  1212. return 0;
  1213. }
  1214. EXPORT_SYMBOL(bolero_runtime_resume);
  1215. int bolero_runtime_suspend(struct device *dev)
  1216. {
  1217. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1218. mutex_lock(&priv->vote_lock);
  1219. if (priv->lpass_core_hw_vote != NULL) {
  1220. if (--priv->core_hw_vote_count == 0)
  1221. clk_disable_unprepare(priv->lpass_core_hw_vote);
  1222. if (priv->core_hw_vote_count < 0)
  1223. priv->core_hw_vote_count = 0;
  1224. } else {
  1225. dev_dbg(dev, "%s: Invalid lpass core hw node\n",
  1226. __func__);
  1227. }
  1228. if (priv->lpass_audio_hw_vote != NULL) {
  1229. if (--priv->core_audio_vote_count == 0)
  1230. clk_disable_unprepare(priv->lpass_audio_hw_vote);
  1231. if (priv->core_audio_vote_count < 0)
  1232. priv->core_audio_vote_count = 0;
  1233. } else {
  1234. dev_dbg(dev, "%s: Invalid lpass audio hw node\n",
  1235. __func__);
  1236. }
  1237. mutex_unlock(&priv->vote_lock);
  1238. return 0;
  1239. }
  1240. EXPORT_SYMBOL(bolero_runtime_suspend);
  1241. #endif /* CONFIG_PM */
  1242. bool bolero_check_core_votes(struct device *dev)
  1243. {
  1244. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1245. bool ret = true;
  1246. mutex_lock(&priv->vote_lock);
  1247. if ((priv->lpass_core_hw_vote && !priv->core_hw_vote_count) ||
  1248. (priv->lpass_audio_hw_vote && !priv->core_audio_vote_count))
  1249. ret = false;
  1250. mutex_unlock(&priv->vote_lock);
  1251. return ret;
  1252. }
  1253. EXPORT_SYMBOL(bolero_check_core_votes);
  1254. static const struct of_device_id bolero_dt_match[] = {
  1255. {.compatible = "qcom,bolero-codec"},
  1256. {}
  1257. };
  1258. MODULE_DEVICE_TABLE(of, bolero_dt_match);
  1259. static struct platform_driver bolero_drv = {
  1260. .driver = {
  1261. .name = "bolero-codec",
  1262. .owner = THIS_MODULE,
  1263. .of_match_table = bolero_dt_match,
  1264. .suppress_bind_attrs = true,
  1265. },
  1266. .probe = bolero_probe,
  1267. .remove = bolero_remove,
  1268. };
  1269. static int bolero_drv_init(void)
  1270. {
  1271. return platform_driver_register(&bolero_drv);
  1272. }
  1273. static void bolero_drv_exit(void)
  1274. {
  1275. platform_driver_unregister(&bolero_drv);
  1276. }
  1277. static int __init bolero_init(void)
  1278. {
  1279. bolero_drv_init();
  1280. bolero_clk_rsc_mgr_init();
  1281. return 0;
  1282. }
  1283. module_init(bolero_init);
  1284. static void __exit bolero_exit(void)
  1285. {
  1286. bolero_clk_rsc_mgr_exit();
  1287. bolero_drv_exit();
  1288. }
  1289. module_exit(bolero_exit);
  1290. MODULE_DESCRIPTION("Bolero driver");
  1291. MODULE_LICENSE("GPL v2");