lpass-cdc.c 40 KB

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