bolero-cdc.c 40 KB

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