bolero-cdc.c 41 KB

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