bolero-cdc.c 40 KB

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