bolero-cdc.c 42 KB

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