bolero-cdc.c 39 KB

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