bolero-cdc.c 42 KB

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