bolero-cdc.c 39 KB

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