lpass-cdc.c 39 KB

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