bolero-cdc.c 41 KB

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