bolero-cdc.c 40 KB

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