acp-pcm-dma.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. /*
  2. * AMD ALSA SoC PCM Driver for ACP 2.x
  3. *
  4. * Copyright 2014-2015 Advanced Micro Devices, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/delay.h>
  17. #include <linux/io.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/sizes.h>
  20. #include <linux/pm_runtime.h>
  21. #include <sound/soc.h>
  22. #include <drm/amd_asic_type.h>
  23. #include "acp.h"
  24. #define DRV_NAME "acp_audio_dma"
  25. #define PLAYBACK_MIN_NUM_PERIODS 2
  26. #define PLAYBACK_MAX_NUM_PERIODS 2
  27. #define PLAYBACK_MAX_PERIOD_SIZE 16384
  28. #define PLAYBACK_MIN_PERIOD_SIZE 1024
  29. #define CAPTURE_MIN_NUM_PERIODS 2
  30. #define CAPTURE_MAX_NUM_PERIODS 2
  31. #define CAPTURE_MAX_PERIOD_SIZE 16384
  32. #define CAPTURE_MIN_PERIOD_SIZE 1024
  33. #define MAX_BUFFER (PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
  34. #define MIN_BUFFER MAX_BUFFER
  35. #define ST_PLAYBACK_MAX_PERIOD_SIZE 4096
  36. #define ST_CAPTURE_MAX_PERIOD_SIZE ST_PLAYBACK_MAX_PERIOD_SIZE
  37. #define ST_MAX_BUFFER (ST_PLAYBACK_MAX_PERIOD_SIZE * PLAYBACK_MAX_NUM_PERIODS)
  38. #define ST_MIN_BUFFER ST_MAX_BUFFER
  39. #define DRV_NAME "acp_audio_dma"
  40. bool bt_uart_enable = true;
  41. EXPORT_SYMBOL(bt_uart_enable);
  42. static const struct snd_pcm_hardware acp_pcm_hardware_playback = {
  43. .info = SNDRV_PCM_INFO_INTERLEAVED |
  44. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
  45. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
  46. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  47. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  48. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  49. .channels_min = 1,
  50. .channels_max = 8,
  51. .rates = SNDRV_PCM_RATE_8000_96000,
  52. .rate_min = 8000,
  53. .rate_max = 96000,
  54. .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE,
  55. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  56. .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE,
  57. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  58. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  59. };
  60. static const struct snd_pcm_hardware acp_pcm_hardware_capture = {
  61. .info = SNDRV_PCM_INFO_INTERLEAVED |
  62. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
  63. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
  64. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  65. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  66. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  67. .channels_min = 1,
  68. .channels_max = 2,
  69. .rates = SNDRV_PCM_RATE_8000_48000,
  70. .rate_min = 8000,
  71. .rate_max = 48000,
  72. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE,
  73. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  74. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  75. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  76. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  77. };
  78. static const struct snd_pcm_hardware acp_st_pcm_hardware_playback = {
  79. .info = SNDRV_PCM_INFO_INTERLEAVED |
  80. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
  81. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
  82. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  83. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  84. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  85. .channels_min = 1,
  86. .channels_max = 8,
  87. .rates = SNDRV_PCM_RATE_8000_96000,
  88. .rate_min = 8000,
  89. .rate_max = 96000,
  90. .buffer_bytes_max = ST_MAX_BUFFER,
  91. .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE,
  92. .period_bytes_max = ST_PLAYBACK_MAX_PERIOD_SIZE,
  93. .periods_min = PLAYBACK_MIN_NUM_PERIODS,
  94. .periods_max = PLAYBACK_MAX_NUM_PERIODS,
  95. };
  96. static const struct snd_pcm_hardware acp_st_pcm_hardware_capture = {
  97. .info = SNDRV_PCM_INFO_INTERLEAVED |
  98. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
  99. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BATCH |
  100. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME,
  101. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  102. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  103. .channels_min = 1,
  104. .channels_max = 2,
  105. .rates = SNDRV_PCM_RATE_8000_48000,
  106. .rate_min = 8000,
  107. .rate_max = 48000,
  108. .buffer_bytes_max = ST_MAX_BUFFER,
  109. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  110. .period_bytes_max = ST_CAPTURE_MAX_PERIOD_SIZE,
  111. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  112. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  113. };
  114. static u32 acp_reg_read(void __iomem *acp_mmio, u32 reg)
  115. {
  116. return readl(acp_mmio + (reg * 4));
  117. }
  118. static void acp_reg_write(u32 val, void __iomem *acp_mmio, u32 reg)
  119. {
  120. writel(val, acp_mmio + (reg * 4));
  121. }
  122. /*
  123. * Configure a given dma channel parameters - enable/disable,
  124. * number of descriptors, priority
  125. */
  126. static void config_acp_dma_channel(void __iomem *acp_mmio, u8 ch_num,
  127. u16 dscr_strt_idx, u16 num_dscrs,
  128. enum acp_dma_priority_level priority_level)
  129. {
  130. u32 dma_ctrl;
  131. /* disable the channel run field */
  132. dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  133. dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
  134. acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  135. /* program a DMA channel with first descriptor to be processed. */
  136. acp_reg_write((ACP_DMA_DSCR_STRT_IDX_0__DMAChDscrStrtIdx_MASK
  137. & dscr_strt_idx),
  138. acp_mmio, mmACP_DMA_DSCR_STRT_IDX_0 + ch_num);
  139. /*
  140. * program a DMA channel with the number of descriptors to be
  141. * processed in the transfer
  142. */
  143. acp_reg_write(ACP_DMA_DSCR_CNT_0__DMAChDscrCnt_MASK & num_dscrs,
  144. acp_mmio, mmACP_DMA_DSCR_CNT_0 + ch_num);
  145. /* set DMA channel priority */
  146. acp_reg_write(priority_level, acp_mmio, mmACP_DMA_PRIO_0 + ch_num);
  147. }
  148. /* Initialize a dma descriptor in SRAM based on descritor information passed */
  149. static void config_dma_descriptor_in_sram(void __iomem *acp_mmio,
  150. u16 descr_idx,
  151. acp_dma_dscr_transfer_t *descr_info)
  152. {
  153. u32 sram_offset;
  154. sram_offset = (descr_idx * sizeof(acp_dma_dscr_transfer_t));
  155. /* program the source base address. */
  156. acp_reg_write(sram_offset, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
  157. acp_reg_write(descr_info->src, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
  158. /* program the destination base address. */
  159. acp_reg_write(sram_offset + 4, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
  160. acp_reg_write(descr_info->dest, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
  161. /* program the number of bytes to be transferred for this descriptor. */
  162. acp_reg_write(sram_offset + 8, acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
  163. acp_reg_write(descr_info->xfer_val, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
  164. }
  165. static void pre_config_reset(void __iomem *acp_mmio, u16 ch_num)
  166. {
  167. u32 dma_ctrl;
  168. int ret;
  169. /* clear the reset bit */
  170. dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  171. dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;
  172. acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  173. /* check the reset bit before programming configuration registers */
  174. ret = readl_poll_timeout(acp_mmio + ((mmACP_DMA_CNTL_0 + ch_num) * 4),
  175. dma_ctrl,
  176. !(dma_ctrl & ACP_DMA_CNTL_0__DMAChRst_MASK),
  177. 100, ACP_DMA_RESET_TIME);
  178. if (ret < 0)
  179. pr_err("Failed to clear reset of channel : %d\n", ch_num);
  180. }
  181. /*
  182. * Initialize the DMA descriptor information for transfer between
  183. * system memory <-> ACP SRAM
  184. */
  185. static void set_acp_sysmem_dma_descriptors(void __iomem *acp_mmio,
  186. u32 size, int direction,
  187. u32 pte_offset, u16 ch,
  188. u32 sram_bank, u16 dma_dscr_idx,
  189. u32 asic_type)
  190. {
  191. u16 i;
  192. acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
  193. for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
  194. dmadscr[i].xfer_val = 0;
  195. if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
  196. dma_dscr_idx = dma_dscr_idx + i;
  197. dmadscr[i].dest = sram_bank + (i * (size / 2));
  198. dmadscr[i].src = ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS
  199. + (pte_offset * SZ_4K) + (i * (size / 2));
  200. switch (asic_type) {
  201. case CHIP_STONEY:
  202. dmadscr[i].xfer_val |=
  203. (ACP_DMA_ATTR_DAGB_GARLIC_TO_SHAREDMEM << 16) |
  204. (size / 2);
  205. break;
  206. default:
  207. dmadscr[i].xfer_val |=
  208. (ACP_DMA_ATTR_DAGB_ONION_TO_SHAREDMEM << 16) |
  209. (size / 2);
  210. }
  211. } else {
  212. dma_dscr_idx = dma_dscr_idx + i;
  213. dmadscr[i].src = sram_bank + (i * (size / 2));
  214. dmadscr[i].dest =
  215. ACP_INTERNAL_APERTURE_WINDOW_0_ADDRESS +
  216. (pte_offset * SZ_4K) + (i * (size / 2));
  217. switch (asic_type) {
  218. case CHIP_STONEY:
  219. dmadscr[i].xfer_val |=
  220. (ACP_DMA_ATTR_SHARED_MEM_TO_DAGB_GARLIC << 16) |
  221. (size / 2);
  222. break;
  223. default:
  224. dmadscr[i].xfer_val |=
  225. (ACP_DMA_ATTR_SHAREDMEM_TO_DAGB_ONION << 16) |
  226. (size / 2);
  227. }
  228. }
  229. config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
  230. &dmadscr[i]);
  231. }
  232. pre_config_reset(acp_mmio, ch);
  233. config_acp_dma_channel(acp_mmio, ch,
  234. dma_dscr_idx - 1,
  235. NUM_DSCRS_PER_CHANNEL,
  236. ACP_DMA_PRIORITY_LEVEL_NORMAL);
  237. }
  238. /*
  239. * Initialize the DMA descriptor information for transfer between
  240. * ACP SRAM <-> I2S
  241. */
  242. static void set_acp_to_i2s_dma_descriptors(void __iomem *acp_mmio, u32 size,
  243. int direction, u32 sram_bank,
  244. u16 destination, u16 ch,
  245. u16 dma_dscr_idx, u32 asic_type)
  246. {
  247. u16 i;
  248. acp_dma_dscr_transfer_t dmadscr[NUM_DSCRS_PER_CHANNEL];
  249. for (i = 0; i < NUM_DSCRS_PER_CHANNEL; i++) {
  250. dmadscr[i].xfer_val = 0;
  251. if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
  252. dma_dscr_idx = dma_dscr_idx + i;
  253. dmadscr[i].src = sram_bank + (i * (size / 2));
  254. /* dmadscr[i].dest is unused by hardware. */
  255. dmadscr[i].dest = 0;
  256. dmadscr[i].xfer_val |= BIT(22) | (destination << 16) |
  257. (size / 2);
  258. } else {
  259. dma_dscr_idx = dma_dscr_idx + i;
  260. /* dmadscr[i].src is unused by hardware. */
  261. dmadscr[i].src = 0;
  262. dmadscr[i].dest =
  263. sram_bank + (i * (size / 2));
  264. dmadscr[i].xfer_val |= BIT(22) |
  265. (destination << 16) | (size / 2);
  266. }
  267. config_dma_descriptor_in_sram(acp_mmio, dma_dscr_idx,
  268. &dmadscr[i]);
  269. }
  270. pre_config_reset(acp_mmio, ch);
  271. /* Configure the DMA channel with the above descriptore */
  272. config_acp_dma_channel(acp_mmio, ch, dma_dscr_idx - 1,
  273. NUM_DSCRS_PER_CHANNEL,
  274. ACP_DMA_PRIORITY_LEVEL_NORMAL);
  275. }
  276. /* Create page table entries in ACP SRAM for the allocated memory */
  277. static void acp_pte_config(void __iomem *acp_mmio, struct page *pg,
  278. u16 num_of_pages, u32 pte_offset)
  279. {
  280. u16 page_idx;
  281. u64 addr;
  282. u32 low;
  283. u32 high;
  284. u32 offset;
  285. offset = ACP_DAGB_GRP_SRBM_SRAM_BASE_OFFSET + (pte_offset * 8);
  286. for (page_idx = 0; page_idx < (num_of_pages); page_idx++) {
  287. /* Load the low address of page int ACP SRAM through SRBM */
  288. acp_reg_write((offset + (page_idx * 8)),
  289. acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
  290. addr = page_to_phys(pg);
  291. low = lower_32_bits(addr);
  292. high = upper_32_bits(addr);
  293. acp_reg_write(low, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
  294. /* Load the High address of page int ACP SRAM through SRBM */
  295. acp_reg_write((offset + (page_idx * 8) + 4),
  296. acp_mmio, mmACP_SRBM_Targ_Idx_Addr);
  297. /* page enable in ACP */
  298. high |= BIT(31);
  299. acp_reg_write(high, acp_mmio, mmACP_SRBM_Targ_Idx_Data);
  300. /* Move to next physically contiguos page */
  301. pg++;
  302. }
  303. }
  304. static void config_acp_dma(void __iomem *acp_mmio,
  305. struct audio_substream_data *rtd,
  306. u32 asic_type)
  307. {
  308. u16 ch_acp_sysmem, ch_acp_i2s;
  309. acp_pte_config(acp_mmio, rtd->pg, rtd->num_of_pages,
  310. rtd->pte_offset);
  311. if (rtd->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  312. ch_acp_sysmem = rtd->ch1;
  313. ch_acp_i2s = rtd->ch2;
  314. } else {
  315. ch_acp_i2s = rtd->ch1;
  316. ch_acp_sysmem = rtd->ch2;
  317. }
  318. /* Configure System memory <-> ACP SRAM DMA descriptors */
  319. set_acp_sysmem_dma_descriptors(acp_mmio, rtd->size,
  320. rtd->direction, rtd->pte_offset,
  321. ch_acp_sysmem, rtd->sram_bank,
  322. rtd->dma_dscr_idx_1, asic_type);
  323. /* Configure ACP SRAM <-> I2S DMA descriptors */
  324. set_acp_to_i2s_dma_descriptors(acp_mmio, rtd->size,
  325. rtd->direction, rtd->sram_bank,
  326. rtd->destination, ch_acp_i2s,
  327. rtd->dma_dscr_idx_2, asic_type);
  328. }
  329. static void acp_dma_cap_channel_enable(void __iomem *acp_mmio,
  330. u16 cap_channel)
  331. {
  332. u32 val, ch_reg, imr_reg, res_reg;
  333. switch (cap_channel) {
  334. case CAP_CHANNEL1:
  335. ch_reg = mmACP_I2SMICSP_RER1;
  336. res_reg = mmACP_I2SMICSP_RCR1;
  337. imr_reg = mmACP_I2SMICSP_IMR1;
  338. break;
  339. case CAP_CHANNEL0:
  340. default:
  341. ch_reg = mmACP_I2SMICSP_RER0;
  342. res_reg = mmACP_I2SMICSP_RCR0;
  343. imr_reg = mmACP_I2SMICSP_IMR0;
  344. break;
  345. }
  346. val = acp_reg_read(acp_mmio,
  347. mmACP_I2S_16BIT_RESOLUTION_EN);
  348. if (val & ACP_I2S_MIC_16BIT_RESOLUTION_EN) {
  349. acp_reg_write(0x0, acp_mmio, ch_reg);
  350. /* Set 16bit resolution on capture */
  351. acp_reg_write(0x2, acp_mmio, res_reg);
  352. }
  353. val = acp_reg_read(acp_mmio, imr_reg);
  354. val &= ~ACP_I2SMICSP_IMR1__I2SMICSP_RXDAM_MASK;
  355. val &= ~ACP_I2SMICSP_IMR1__I2SMICSP_RXFOM_MASK;
  356. acp_reg_write(val, acp_mmio, imr_reg);
  357. acp_reg_write(0x1, acp_mmio, ch_reg);
  358. }
  359. static void acp_dma_cap_channel_disable(void __iomem *acp_mmio,
  360. u16 cap_channel)
  361. {
  362. u32 val, ch_reg, imr_reg;
  363. switch (cap_channel) {
  364. case CAP_CHANNEL1:
  365. imr_reg = mmACP_I2SMICSP_IMR1;
  366. ch_reg = mmACP_I2SMICSP_RER1;
  367. break;
  368. case CAP_CHANNEL0:
  369. default:
  370. imr_reg = mmACP_I2SMICSP_IMR0;
  371. ch_reg = mmACP_I2SMICSP_RER0;
  372. break;
  373. }
  374. val = acp_reg_read(acp_mmio, imr_reg);
  375. val |= ACP_I2SMICSP_IMR1__I2SMICSP_RXDAM_MASK;
  376. val |= ACP_I2SMICSP_IMR1__I2SMICSP_RXFOM_MASK;
  377. acp_reg_write(val, acp_mmio, imr_reg);
  378. acp_reg_write(0x0, acp_mmio, ch_reg);
  379. }
  380. /* Start a given DMA channel transfer */
  381. static void acp_dma_start(void __iomem *acp_mmio, u16 ch_num, bool is_circular)
  382. {
  383. u32 dma_ctrl;
  384. /* read the dma control register and disable the channel run field */
  385. dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  386. /* Invalidating the DAGB cache */
  387. acp_reg_write(1, acp_mmio, mmACP_DAGB_ATU_CTRL);
  388. /*
  389. * configure the DMA channel and start the DMA transfer
  390. * set dmachrun bit to start the transfer and enable the
  391. * interrupt on completion of the dma transfer
  392. */
  393. dma_ctrl |= ACP_DMA_CNTL_0__DMAChRun_MASK;
  394. switch (ch_num) {
  395. case ACP_TO_I2S_DMA_CH_NUM:
  396. case I2S_TO_ACP_DMA_CH_NUM:
  397. case ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM:
  398. case I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM:
  399. dma_ctrl |= ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
  400. break;
  401. default:
  402. dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
  403. break;
  404. }
  405. /* enable for ACP to SRAM DMA channel */
  406. if (is_circular == true)
  407. dma_ctrl |= ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
  408. else
  409. dma_ctrl &= ~ACP_DMA_CNTL_0__Circular_DMA_En_MASK;
  410. acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  411. }
  412. /* Stop a given DMA channel transfer */
  413. static int acp_dma_stop(void __iomem *acp_mmio, u8 ch_num)
  414. {
  415. u32 dma_ctrl;
  416. u32 dma_ch_sts;
  417. u32 count = ACP_DMA_RESET_TIME;
  418. dma_ctrl = acp_reg_read(acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  419. /*
  420. * clear the dma control register fields before writing zero
  421. * in reset bit
  422. */
  423. dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRun_MASK;
  424. dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChIOCEn_MASK;
  425. acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  426. dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
  427. if (dma_ch_sts & BIT(ch_num)) {
  428. /*
  429. * set the reset bit for this channel to stop the dma
  430. * transfer
  431. */
  432. dma_ctrl |= ACP_DMA_CNTL_0__DMAChRst_MASK;
  433. acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0 + ch_num);
  434. }
  435. /* check the channel status bit for some time and return the status */
  436. while (true) {
  437. dma_ch_sts = acp_reg_read(acp_mmio, mmACP_DMA_CH_STS);
  438. if (!(dma_ch_sts & BIT(ch_num))) {
  439. /*
  440. * clear the reset flag after successfully stopping
  441. * the dma transfer and break from the loop
  442. */
  443. dma_ctrl &= ~ACP_DMA_CNTL_0__DMAChRst_MASK;
  444. acp_reg_write(dma_ctrl, acp_mmio, mmACP_DMA_CNTL_0
  445. + ch_num);
  446. break;
  447. }
  448. if (--count == 0) {
  449. pr_err("Failed to stop ACP DMA channel : %d\n", ch_num);
  450. return -ETIMEDOUT;
  451. }
  452. udelay(100);
  453. }
  454. return 0;
  455. }
  456. static void acp_set_sram_bank_state(void __iomem *acp_mmio, u16 bank,
  457. bool power_on)
  458. {
  459. u32 val, req_reg, sts_reg, sts_reg_mask;
  460. u32 loops = 1000;
  461. if (bank < 32) {
  462. req_reg = mmACP_MEM_SHUT_DOWN_REQ_LO;
  463. sts_reg = mmACP_MEM_SHUT_DOWN_STS_LO;
  464. sts_reg_mask = 0xFFFFFFFF;
  465. } else {
  466. bank -= 32;
  467. req_reg = mmACP_MEM_SHUT_DOWN_REQ_HI;
  468. sts_reg = mmACP_MEM_SHUT_DOWN_STS_HI;
  469. sts_reg_mask = 0x0000FFFF;
  470. }
  471. val = acp_reg_read(acp_mmio, req_reg);
  472. if (val & (1 << bank)) {
  473. /* bank is in off state */
  474. if (power_on == true)
  475. /* request to on */
  476. val &= ~(1 << bank);
  477. else
  478. /* request to off */
  479. return;
  480. } else {
  481. /* bank is in on state */
  482. if (power_on == false)
  483. /* request to off */
  484. val |= 1 << bank;
  485. else
  486. /* request to on */
  487. return;
  488. }
  489. acp_reg_write(val, acp_mmio, req_reg);
  490. while (acp_reg_read(acp_mmio, sts_reg) != sts_reg_mask) {
  491. if (!loops--) {
  492. pr_err("ACP SRAM bank %d state change failed\n", bank);
  493. break;
  494. }
  495. cpu_relax();
  496. }
  497. }
  498. /* Initialize and bring ACP hardware to default state. */
  499. static int acp_init(void __iomem *acp_mmio, u32 asic_type)
  500. {
  501. u16 bank;
  502. u32 val, count, sram_pte_offset;
  503. /* Assert Soft reset of ACP */
  504. val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
  505. val |= ACP_SOFT_RESET__SoftResetAud_MASK;
  506. acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
  507. count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
  508. while (true) {
  509. val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
  510. if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
  511. (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
  512. break;
  513. if (--count == 0) {
  514. pr_err("Failed to reset ACP\n");
  515. return -ETIMEDOUT;
  516. }
  517. udelay(100);
  518. }
  519. /* Enable clock to ACP and wait until the clock is enabled */
  520. val = acp_reg_read(acp_mmio, mmACP_CONTROL);
  521. val = val | ACP_CONTROL__ClkEn_MASK;
  522. acp_reg_write(val, acp_mmio, mmACP_CONTROL);
  523. count = ACP_CLOCK_EN_TIME_OUT_VALUE;
  524. while (true) {
  525. val = acp_reg_read(acp_mmio, mmACP_STATUS);
  526. if (val & (u32)0x1)
  527. break;
  528. if (--count == 0) {
  529. pr_err("Failed to reset ACP\n");
  530. return -ETIMEDOUT;
  531. }
  532. udelay(100);
  533. }
  534. /* Deassert the SOFT RESET flags */
  535. val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
  536. val &= ~ACP_SOFT_RESET__SoftResetAud_MASK;
  537. acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
  538. /* For BT instance change pins from UART to BT */
  539. if (!bt_uart_enable) {
  540. val = acp_reg_read(acp_mmio, mmACP_BT_UART_PAD_SEL);
  541. val |= ACP_BT_UART_PAD_SELECT_MASK;
  542. acp_reg_write(val, acp_mmio, mmACP_BT_UART_PAD_SEL);
  543. }
  544. /* initiailize Onion control DAGB register */
  545. acp_reg_write(ACP_ONION_CNTL_DEFAULT, acp_mmio,
  546. mmACP_AXI2DAGB_ONION_CNTL);
  547. /* initiailize Garlic control DAGB registers */
  548. acp_reg_write(ACP_GARLIC_CNTL_DEFAULT, acp_mmio,
  549. mmACP_AXI2DAGB_GARLIC_CNTL);
  550. sram_pte_offset = ACP_DAGB_GRP_SRAM_BASE_ADDRESS |
  551. ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBSnoopSel_MASK |
  552. ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBTargetMemSel_MASK |
  553. ACP_DAGB_BASE_ADDR_GRP_1__AXI2DAGBGrpEnable_MASK;
  554. acp_reg_write(sram_pte_offset, acp_mmio, mmACP_DAGB_BASE_ADDR_GRP_1);
  555. acp_reg_write(ACP_PAGE_SIZE_4K_ENABLE, acp_mmio,
  556. mmACP_DAGB_PAGE_SIZE_GRP_1);
  557. acp_reg_write(ACP_SRAM_BASE_ADDRESS, acp_mmio,
  558. mmACP_DMA_DESC_BASE_ADDR);
  559. /* Num of descriptiors in SRAM 0x4, means 256 descriptors;(64 * 4) */
  560. acp_reg_write(0x4, acp_mmio, mmACP_DMA_DESC_MAX_NUM_DSCR);
  561. acp_reg_write(ACP_EXTERNAL_INTR_CNTL__DMAIOCMask_MASK,
  562. acp_mmio, mmACP_EXTERNAL_INTR_CNTL);
  563. /*
  564. * When ACP_TILE_P1 is turned on, all SRAM banks get turned on.
  565. * Now, turn off all of them. This can't be done in 'poweron' of
  566. * ACP pm domain, as this requires ACP to be initialized.
  567. * For Stoney, Memory gating is disabled,i.e SRAM Banks
  568. * won't be turned off. The default state for SRAM banks is ON.
  569. * Setting SRAM bank state code skipped for STONEY platform.
  570. */
  571. if (asic_type != CHIP_STONEY) {
  572. for (bank = 1; bank < 48; bank++)
  573. acp_set_sram_bank_state(acp_mmio, bank, false);
  574. }
  575. return 0;
  576. }
  577. /* Deinitialize ACP */
  578. static int acp_deinit(void __iomem *acp_mmio)
  579. {
  580. u32 val;
  581. u32 count;
  582. /* Assert Soft reset of ACP */
  583. val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
  584. val |= ACP_SOFT_RESET__SoftResetAud_MASK;
  585. acp_reg_write(val, acp_mmio, mmACP_SOFT_RESET);
  586. count = ACP_SOFT_RESET_DONE_TIME_OUT_VALUE;
  587. while (true) {
  588. val = acp_reg_read(acp_mmio, mmACP_SOFT_RESET);
  589. if (ACP_SOFT_RESET__SoftResetAudDone_MASK ==
  590. (val & ACP_SOFT_RESET__SoftResetAudDone_MASK))
  591. break;
  592. if (--count == 0) {
  593. pr_err("Failed to reset ACP\n");
  594. return -ETIMEDOUT;
  595. }
  596. udelay(100);
  597. }
  598. /* Disable ACP clock */
  599. val = acp_reg_read(acp_mmio, mmACP_CONTROL);
  600. val &= ~ACP_CONTROL__ClkEn_MASK;
  601. acp_reg_write(val, acp_mmio, mmACP_CONTROL);
  602. count = ACP_CLOCK_EN_TIME_OUT_VALUE;
  603. while (true) {
  604. val = acp_reg_read(acp_mmio, mmACP_STATUS);
  605. if (!(val & (u32)0x1))
  606. break;
  607. if (--count == 0) {
  608. pr_err("Failed to reset ACP\n");
  609. return -ETIMEDOUT;
  610. }
  611. udelay(100);
  612. }
  613. return 0;
  614. }
  615. /* ACP DMA irq handler routine for playback, capture usecases */
  616. static irqreturn_t dma_irq_handler(int irq, void *arg)
  617. {
  618. u16 dscr_idx;
  619. u32 intr_flag, ext_intr_status;
  620. struct audio_drv_data *irq_data;
  621. void __iomem *acp_mmio;
  622. struct device *dev = arg;
  623. bool valid_irq = false;
  624. irq_data = dev_get_drvdata(dev);
  625. acp_mmio = irq_data->acp_mmio;
  626. ext_intr_status = acp_reg_read(acp_mmio, mmACP_EXTERNAL_INTR_STAT);
  627. intr_flag = (((ext_intr_status &
  628. ACP_EXTERNAL_INTR_STAT__DMAIOCStat_MASK) >>
  629. ACP_EXTERNAL_INTR_STAT__DMAIOCStat__SHIFT));
  630. if ((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) != 0) {
  631. valid_irq = true;
  632. snd_pcm_period_elapsed(irq_data->play_i2ssp_stream);
  633. acp_reg_write((intr_flag & BIT(ACP_TO_I2S_DMA_CH_NUM)) << 16,
  634. acp_mmio, mmACP_EXTERNAL_INTR_STAT);
  635. }
  636. if ((intr_flag & BIT(ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM)) != 0) {
  637. valid_irq = true;
  638. snd_pcm_period_elapsed(irq_data->play_i2sbt_stream);
  639. acp_reg_write((intr_flag &
  640. BIT(ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM)) << 16,
  641. acp_mmio, mmACP_EXTERNAL_INTR_STAT);
  642. }
  643. if ((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) != 0) {
  644. valid_irq = true;
  645. if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_14) ==
  646. CAPTURE_START_DMA_DESCR_CH15)
  647. dscr_idx = CAPTURE_END_DMA_DESCR_CH14;
  648. else
  649. dscr_idx = CAPTURE_START_DMA_DESCR_CH14;
  650. config_acp_dma_channel(acp_mmio, ACP_TO_SYSRAM_CH_NUM, dscr_idx,
  651. 1, 0);
  652. acp_dma_start(acp_mmio, ACP_TO_SYSRAM_CH_NUM, false);
  653. snd_pcm_period_elapsed(irq_data->capture_i2ssp_stream);
  654. acp_reg_write((intr_flag & BIT(I2S_TO_ACP_DMA_CH_NUM)) << 16,
  655. acp_mmio, mmACP_EXTERNAL_INTR_STAT);
  656. }
  657. if ((intr_flag & BIT(I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM)) != 0) {
  658. valid_irq = true;
  659. if (acp_reg_read(acp_mmio, mmACP_DMA_CUR_DSCR_10) ==
  660. CAPTURE_START_DMA_DESCR_CH11)
  661. dscr_idx = CAPTURE_END_DMA_DESCR_CH10;
  662. else
  663. dscr_idx = CAPTURE_START_DMA_DESCR_CH10;
  664. config_acp_dma_channel(acp_mmio,
  665. ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM,
  666. dscr_idx, 1, 0);
  667. acp_dma_start(acp_mmio, ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM,
  668. false);
  669. snd_pcm_period_elapsed(irq_data->capture_i2sbt_stream);
  670. acp_reg_write((intr_flag &
  671. BIT(I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM)) << 16,
  672. acp_mmio, mmACP_EXTERNAL_INTR_STAT);
  673. }
  674. if (valid_irq)
  675. return IRQ_HANDLED;
  676. else
  677. return IRQ_NONE;
  678. }
  679. static int acp_dma_open(struct snd_pcm_substream *substream)
  680. {
  681. u16 bank;
  682. int ret = 0;
  683. struct snd_pcm_runtime *runtime = substream->runtime;
  684. struct snd_soc_pcm_runtime *prtd = substream->private_data;
  685. struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
  686. DRV_NAME);
  687. struct audio_drv_data *intr_data = dev_get_drvdata(component->dev);
  688. struct audio_substream_data *adata =
  689. kzalloc(sizeof(struct audio_substream_data), GFP_KERNEL);
  690. if (!adata)
  691. return -ENOMEM;
  692. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  693. switch (intr_data->asic_type) {
  694. case CHIP_STONEY:
  695. runtime->hw = acp_st_pcm_hardware_playback;
  696. break;
  697. default:
  698. runtime->hw = acp_pcm_hardware_playback;
  699. }
  700. } else {
  701. switch (intr_data->asic_type) {
  702. case CHIP_STONEY:
  703. runtime->hw = acp_st_pcm_hardware_capture;
  704. break;
  705. default:
  706. runtime->hw = acp_pcm_hardware_capture;
  707. }
  708. }
  709. ret = snd_pcm_hw_constraint_integer(runtime,
  710. SNDRV_PCM_HW_PARAM_PERIODS);
  711. if (ret < 0) {
  712. dev_err(component->dev, "set integer constraint failed\n");
  713. kfree(adata);
  714. return ret;
  715. }
  716. adata->acp_mmio = intr_data->acp_mmio;
  717. runtime->private_data = adata;
  718. /*
  719. * Enable ACP irq, when neither playback or capture streams are
  720. * active by the time when a new stream is being opened.
  721. * This enablement is not required for another stream, if current
  722. * stream is not closed
  723. */
  724. if (!intr_data->play_i2ssp_stream && !intr_data->capture_i2ssp_stream &&
  725. !intr_data->play_i2sbt_stream && !intr_data->capture_i2sbt_stream)
  726. acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
  727. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  728. /*
  729. * For Stoney, Memory gating is disabled,i.e SRAM Banks
  730. * won't be turned off. The default state for SRAM banks is ON.
  731. * Setting SRAM bank state code skipped for STONEY platform.
  732. */
  733. if (intr_data->asic_type != CHIP_STONEY) {
  734. for (bank = 1; bank <= 4; bank++)
  735. acp_set_sram_bank_state(intr_data->acp_mmio,
  736. bank, true);
  737. }
  738. } else {
  739. if (intr_data->asic_type != CHIP_STONEY) {
  740. for (bank = 5; bank <= 8; bank++)
  741. acp_set_sram_bank_state(intr_data->acp_mmio,
  742. bank, true);
  743. }
  744. }
  745. return 0;
  746. }
  747. static int acp_dma_hw_params(struct snd_pcm_substream *substream,
  748. struct snd_pcm_hw_params *params)
  749. {
  750. int status;
  751. uint64_t size;
  752. u32 val = 0;
  753. struct page *pg;
  754. struct snd_pcm_runtime *runtime;
  755. struct audio_substream_data *rtd;
  756. struct snd_soc_pcm_runtime *prtd = substream->private_data;
  757. struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
  758. DRV_NAME);
  759. struct audio_drv_data *adata = dev_get_drvdata(component->dev);
  760. struct snd_soc_card *card = prtd->card;
  761. struct acp_platform_info *pinfo = snd_soc_card_get_drvdata(card);
  762. runtime = substream->runtime;
  763. rtd = runtime->private_data;
  764. if (WARN_ON(!rtd))
  765. return -EINVAL;
  766. if (pinfo) {
  767. rtd->i2s_instance = pinfo->i2s_instance;
  768. rtd->capture_channel = pinfo->capture_channel;
  769. }
  770. if (adata->asic_type == CHIP_STONEY) {
  771. val = acp_reg_read(adata->acp_mmio,
  772. mmACP_I2S_16BIT_RESOLUTION_EN);
  773. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  774. switch (rtd->i2s_instance) {
  775. case I2S_BT_INSTANCE:
  776. val |= ACP_I2S_BT_16BIT_RESOLUTION_EN;
  777. break;
  778. case I2S_SP_INSTANCE:
  779. default:
  780. val |= ACP_I2S_SP_16BIT_RESOLUTION_EN;
  781. }
  782. } else {
  783. switch (rtd->i2s_instance) {
  784. case I2S_BT_INSTANCE:
  785. val |= ACP_I2S_BT_16BIT_RESOLUTION_EN;
  786. break;
  787. case I2S_SP_INSTANCE:
  788. default:
  789. val |= ACP_I2S_MIC_16BIT_RESOLUTION_EN;
  790. }
  791. }
  792. acp_reg_write(val, adata->acp_mmio,
  793. mmACP_I2S_16BIT_RESOLUTION_EN);
  794. }
  795. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  796. switch (rtd->i2s_instance) {
  797. case I2S_BT_INSTANCE:
  798. rtd->pte_offset = ACP_ST_BT_PLAYBACK_PTE_OFFSET;
  799. rtd->ch1 = SYSRAM_TO_ACP_BT_INSTANCE_CH_NUM;
  800. rtd->ch2 = ACP_TO_I2S_DMA_BT_INSTANCE_CH_NUM;
  801. rtd->sram_bank = ACP_SRAM_BANK_3_ADDRESS;
  802. rtd->destination = TO_BLUETOOTH;
  803. rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH8;
  804. rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH9;
  805. rtd->byte_cnt_high_reg_offset =
  806. mmACP_I2S_BT_TRANSMIT_BYTE_CNT_HIGH;
  807. rtd->byte_cnt_low_reg_offset =
  808. mmACP_I2S_BT_TRANSMIT_BYTE_CNT_LOW;
  809. adata->play_i2sbt_stream = substream;
  810. break;
  811. case I2S_SP_INSTANCE:
  812. default:
  813. switch (adata->asic_type) {
  814. case CHIP_STONEY:
  815. rtd->pte_offset = ACP_ST_PLAYBACK_PTE_OFFSET;
  816. break;
  817. default:
  818. rtd->pte_offset = ACP_PLAYBACK_PTE_OFFSET;
  819. }
  820. rtd->ch1 = SYSRAM_TO_ACP_CH_NUM;
  821. rtd->ch2 = ACP_TO_I2S_DMA_CH_NUM;
  822. rtd->sram_bank = ACP_SRAM_BANK_1_ADDRESS;
  823. rtd->destination = TO_ACP_I2S_1;
  824. rtd->dma_dscr_idx_1 = PLAYBACK_START_DMA_DESCR_CH12;
  825. rtd->dma_dscr_idx_2 = PLAYBACK_START_DMA_DESCR_CH13;
  826. rtd->byte_cnt_high_reg_offset =
  827. mmACP_I2S_TRANSMIT_BYTE_CNT_HIGH;
  828. rtd->byte_cnt_low_reg_offset =
  829. mmACP_I2S_TRANSMIT_BYTE_CNT_LOW;
  830. adata->play_i2ssp_stream = substream;
  831. }
  832. } else {
  833. switch (rtd->i2s_instance) {
  834. case I2S_BT_INSTANCE:
  835. rtd->pte_offset = ACP_ST_BT_CAPTURE_PTE_OFFSET;
  836. rtd->ch1 = I2S_TO_ACP_DMA_BT_INSTANCE_CH_NUM;
  837. rtd->ch2 = ACP_TO_SYSRAM_BT_INSTANCE_CH_NUM;
  838. rtd->sram_bank = ACP_SRAM_BANK_4_ADDRESS;
  839. rtd->destination = FROM_BLUETOOTH;
  840. rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH10;
  841. rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH11;
  842. rtd->byte_cnt_high_reg_offset =
  843. mmACP_I2S_BT_RECEIVE_BYTE_CNT_HIGH;
  844. rtd->byte_cnt_low_reg_offset =
  845. mmACP_I2S_BT_RECEIVE_BYTE_CNT_LOW;
  846. rtd->dma_curr_dscr = mmACP_DMA_CUR_DSCR_11;
  847. adata->capture_i2sbt_stream = substream;
  848. break;
  849. case I2S_SP_INSTANCE:
  850. default:
  851. rtd->pte_offset = ACP_CAPTURE_PTE_OFFSET;
  852. rtd->ch1 = I2S_TO_ACP_DMA_CH_NUM;
  853. rtd->ch2 = ACP_TO_SYSRAM_CH_NUM;
  854. switch (adata->asic_type) {
  855. case CHIP_STONEY:
  856. rtd->pte_offset = ACP_ST_CAPTURE_PTE_OFFSET;
  857. rtd->sram_bank = ACP_SRAM_BANK_2_ADDRESS;
  858. break;
  859. default:
  860. rtd->pte_offset = ACP_CAPTURE_PTE_OFFSET;
  861. rtd->sram_bank = ACP_SRAM_BANK_5_ADDRESS;
  862. }
  863. rtd->destination = FROM_ACP_I2S_1;
  864. rtd->dma_dscr_idx_1 = CAPTURE_START_DMA_DESCR_CH14;
  865. rtd->dma_dscr_idx_2 = CAPTURE_START_DMA_DESCR_CH15;
  866. rtd->byte_cnt_high_reg_offset =
  867. mmACP_I2S_RECEIVED_BYTE_CNT_HIGH;
  868. rtd->byte_cnt_low_reg_offset =
  869. mmACP_I2S_RECEIVED_BYTE_CNT_LOW;
  870. rtd->dma_curr_dscr = mmACP_DMA_CUR_DSCR_15;
  871. adata->capture_i2ssp_stream = substream;
  872. }
  873. }
  874. size = params_buffer_bytes(params);
  875. status = snd_pcm_lib_malloc_pages(substream, size);
  876. if (status < 0)
  877. return status;
  878. memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
  879. pg = virt_to_page(substream->dma_buffer.area);
  880. if (pg) {
  881. acp_set_sram_bank_state(rtd->acp_mmio, 0, true);
  882. /* Save for runtime private data */
  883. rtd->pg = pg;
  884. rtd->order = get_order(size);
  885. /* Fill the page table entries in ACP SRAM */
  886. rtd->pg = pg;
  887. rtd->size = size;
  888. rtd->num_of_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  889. rtd->direction = substream->stream;
  890. config_acp_dma(rtd->acp_mmio, rtd, adata->asic_type);
  891. status = 0;
  892. } else {
  893. status = -ENOMEM;
  894. }
  895. return status;
  896. }
  897. static int acp_dma_hw_free(struct snd_pcm_substream *substream)
  898. {
  899. return snd_pcm_lib_free_pages(substream);
  900. }
  901. static u64 acp_get_byte_count(struct audio_substream_data *rtd)
  902. {
  903. union acp_dma_count byte_count;
  904. byte_count.bcount.high = acp_reg_read(rtd->acp_mmio,
  905. rtd->byte_cnt_high_reg_offset);
  906. byte_count.bcount.low = acp_reg_read(rtd->acp_mmio,
  907. rtd->byte_cnt_low_reg_offset);
  908. return byte_count.bytescount;
  909. }
  910. static snd_pcm_uframes_t acp_dma_pointer(struct snd_pcm_substream *substream)
  911. {
  912. u32 buffersize;
  913. u32 pos = 0;
  914. u64 bytescount = 0;
  915. u16 dscr;
  916. u32 period_bytes, delay;
  917. struct snd_pcm_runtime *runtime = substream->runtime;
  918. struct audio_substream_data *rtd = runtime->private_data;
  919. if (!rtd)
  920. return -EINVAL;
  921. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  922. period_bytes = frames_to_bytes(runtime, runtime->period_size);
  923. bytescount = acp_get_byte_count(rtd);
  924. if (bytescount >= rtd->bytescount)
  925. bytescount -= rtd->bytescount;
  926. if (bytescount < period_bytes) {
  927. pos = 0;
  928. } else {
  929. dscr = acp_reg_read(rtd->acp_mmio, rtd->dma_curr_dscr);
  930. if (dscr == rtd->dma_dscr_idx_1)
  931. pos = period_bytes;
  932. else
  933. pos = 0;
  934. }
  935. if (bytescount > 0) {
  936. delay = do_div(bytescount, period_bytes);
  937. runtime->delay = bytes_to_frames(runtime, delay);
  938. }
  939. } else {
  940. buffersize = frames_to_bytes(runtime, runtime->buffer_size);
  941. bytescount = acp_get_byte_count(rtd);
  942. if (bytescount > rtd->bytescount)
  943. bytescount -= rtd->bytescount;
  944. pos = do_div(bytescount, buffersize);
  945. }
  946. return bytes_to_frames(runtime, pos);
  947. }
  948. static int acp_dma_mmap(struct snd_pcm_substream *substream,
  949. struct vm_area_struct *vma)
  950. {
  951. return snd_pcm_lib_default_mmap(substream, vma);
  952. }
  953. static int acp_dma_prepare(struct snd_pcm_substream *substream)
  954. {
  955. struct snd_pcm_runtime *runtime = substream->runtime;
  956. struct audio_substream_data *rtd = runtime->private_data;
  957. u16 ch_acp_sysmem, ch_acp_i2s;
  958. if (!rtd)
  959. return -EINVAL;
  960. if (rtd->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  961. ch_acp_sysmem = rtd->ch1;
  962. ch_acp_i2s = rtd->ch2;
  963. } else {
  964. ch_acp_i2s = rtd->ch1;
  965. ch_acp_sysmem = rtd->ch2;
  966. }
  967. config_acp_dma_channel(rtd->acp_mmio,
  968. ch_acp_sysmem,
  969. rtd->dma_dscr_idx_1,
  970. NUM_DSCRS_PER_CHANNEL, 0);
  971. config_acp_dma_channel(rtd->acp_mmio,
  972. ch_acp_i2s,
  973. rtd->dma_dscr_idx_2,
  974. NUM_DSCRS_PER_CHANNEL, 0);
  975. return 0;
  976. }
  977. static int acp_dma_trigger(struct snd_pcm_substream *substream, int cmd)
  978. {
  979. int ret;
  980. struct snd_pcm_runtime *runtime = substream->runtime;
  981. struct audio_substream_data *rtd = runtime->private_data;
  982. if (!rtd)
  983. return -EINVAL;
  984. switch (cmd) {
  985. case SNDRV_PCM_TRIGGER_START:
  986. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  987. case SNDRV_PCM_TRIGGER_RESUME:
  988. rtd->bytescount = acp_get_byte_count(rtd);
  989. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  990. if (rtd->capture_channel == CAP_CHANNEL0) {
  991. acp_dma_cap_channel_disable(rtd->acp_mmio,
  992. CAP_CHANNEL1);
  993. acp_dma_cap_channel_enable(rtd->acp_mmio,
  994. CAP_CHANNEL0);
  995. }
  996. if (rtd->capture_channel == CAP_CHANNEL1) {
  997. acp_dma_cap_channel_disable(rtd->acp_mmio,
  998. CAP_CHANNEL0);
  999. acp_dma_cap_channel_enable(rtd->acp_mmio,
  1000. CAP_CHANNEL1);
  1001. }
  1002. acp_dma_start(rtd->acp_mmio, rtd->ch1, true);
  1003. } else {
  1004. acp_dma_start(rtd->acp_mmio, rtd->ch1, true);
  1005. acp_dma_start(rtd->acp_mmio, rtd->ch2, true);
  1006. }
  1007. ret = 0;
  1008. break;
  1009. case SNDRV_PCM_TRIGGER_STOP:
  1010. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1011. case SNDRV_PCM_TRIGGER_SUSPEND:
  1012. acp_dma_stop(rtd->acp_mmio, rtd->ch2);
  1013. ret = acp_dma_stop(rtd->acp_mmio, rtd->ch1);
  1014. break;
  1015. default:
  1016. ret = -EINVAL;
  1017. }
  1018. return ret;
  1019. }
  1020. static int acp_dma_new(struct snd_soc_pcm_runtime *rtd)
  1021. {
  1022. int ret;
  1023. struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd,
  1024. DRV_NAME);
  1025. struct audio_drv_data *adata = dev_get_drvdata(component->dev);
  1026. struct device *parent = component->dev->parent;
  1027. switch (adata->asic_type) {
  1028. case CHIP_STONEY:
  1029. ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
  1030. SNDRV_DMA_TYPE_DEV,
  1031. parent,
  1032. ST_MIN_BUFFER,
  1033. ST_MAX_BUFFER);
  1034. break;
  1035. default:
  1036. ret = snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
  1037. SNDRV_DMA_TYPE_DEV,
  1038. parent,
  1039. MIN_BUFFER,
  1040. MAX_BUFFER);
  1041. break;
  1042. }
  1043. if (ret < 0)
  1044. dev_err(component->dev,
  1045. "buffer preallocation failure error:%d\n", ret);
  1046. return ret;
  1047. }
  1048. static int acp_dma_close(struct snd_pcm_substream *substream)
  1049. {
  1050. u16 bank;
  1051. struct snd_pcm_runtime *runtime = substream->runtime;
  1052. struct audio_substream_data *rtd = runtime->private_data;
  1053. struct snd_soc_pcm_runtime *prtd = substream->private_data;
  1054. struct snd_soc_component *component = snd_soc_rtdcom_lookup(prtd,
  1055. DRV_NAME);
  1056. struct audio_drv_data *adata = dev_get_drvdata(component->dev);
  1057. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1058. switch (rtd->i2s_instance) {
  1059. case I2S_BT_INSTANCE:
  1060. adata->play_i2sbt_stream = NULL;
  1061. break;
  1062. case I2S_SP_INSTANCE:
  1063. default:
  1064. adata->play_i2ssp_stream = NULL;
  1065. /*
  1066. * For Stoney, Memory gating is disabled,i.e SRAM Banks
  1067. * won't be turned off. The default state for SRAM banks
  1068. * is ON.Setting SRAM bank state code skipped for STONEY
  1069. * platform. Added condition checks for Carrizo platform
  1070. * only.
  1071. */
  1072. if (adata->asic_type != CHIP_STONEY) {
  1073. for (bank = 1; bank <= 4; bank++)
  1074. acp_set_sram_bank_state(adata->acp_mmio,
  1075. bank, false);
  1076. }
  1077. }
  1078. } else {
  1079. switch (rtd->i2s_instance) {
  1080. case I2S_BT_INSTANCE:
  1081. adata->capture_i2sbt_stream = NULL;
  1082. break;
  1083. case I2S_SP_INSTANCE:
  1084. default:
  1085. adata->capture_i2ssp_stream = NULL;
  1086. if (adata->asic_type != CHIP_STONEY) {
  1087. for (bank = 5; bank <= 8; bank++)
  1088. acp_set_sram_bank_state(adata->acp_mmio,
  1089. bank, false);
  1090. }
  1091. }
  1092. }
  1093. /*
  1094. * Disable ACP irq, when the current stream is being closed and
  1095. * another stream is also not active.
  1096. */
  1097. if (!adata->play_i2ssp_stream && !adata->capture_i2ssp_stream &&
  1098. !adata->play_i2sbt_stream && !adata->capture_i2sbt_stream)
  1099. acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
  1100. kfree(rtd);
  1101. return 0;
  1102. }
  1103. static const struct snd_pcm_ops acp_dma_ops = {
  1104. .open = acp_dma_open,
  1105. .close = acp_dma_close,
  1106. .ioctl = snd_pcm_lib_ioctl,
  1107. .hw_params = acp_dma_hw_params,
  1108. .hw_free = acp_dma_hw_free,
  1109. .trigger = acp_dma_trigger,
  1110. .pointer = acp_dma_pointer,
  1111. .mmap = acp_dma_mmap,
  1112. .prepare = acp_dma_prepare,
  1113. };
  1114. static const struct snd_soc_component_driver acp_asoc_platform = {
  1115. .name = DRV_NAME,
  1116. .ops = &acp_dma_ops,
  1117. .pcm_new = acp_dma_new,
  1118. };
  1119. static int acp_audio_probe(struct platform_device *pdev)
  1120. {
  1121. int status;
  1122. struct audio_drv_data *audio_drv_data;
  1123. struct resource *res;
  1124. const u32 *pdata = pdev->dev.platform_data;
  1125. if (!pdata) {
  1126. dev_err(&pdev->dev, "Missing platform data\n");
  1127. return -ENODEV;
  1128. }
  1129. audio_drv_data = devm_kzalloc(&pdev->dev, sizeof(struct audio_drv_data),
  1130. GFP_KERNEL);
  1131. if (!audio_drv_data)
  1132. return -ENOMEM;
  1133. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1134. audio_drv_data->acp_mmio = devm_ioremap_resource(&pdev->dev, res);
  1135. if (IS_ERR(audio_drv_data->acp_mmio))
  1136. return PTR_ERR(audio_drv_data->acp_mmio);
  1137. /*
  1138. * The following members gets populated in device 'open'
  1139. * function. Till then interrupts are disabled in 'acp_init'
  1140. * and device doesn't generate any interrupts.
  1141. */
  1142. audio_drv_data->play_i2ssp_stream = NULL;
  1143. audio_drv_data->capture_i2ssp_stream = NULL;
  1144. audio_drv_data->play_i2sbt_stream = NULL;
  1145. audio_drv_data->capture_i2sbt_stream = NULL;
  1146. audio_drv_data->asic_type = *pdata;
  1147. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1148. if (!res) {
  1149. dev_err(&pdev->dev, "IORESOURCE_IRQ FAILED\n");
  1150. return -ENODEV;
  1151. }
  1152. status = devm_request_irq(&pdev->dev, res->start, dma_irq_handler,
  1153. 0, "ACP_IRQ", &pdev->dev);
  1154. if (status) {
  1155. dev_err(&pdev->dev, "ACP IRQ request failed\n");
  1156. return status;
  1157. }
  1158. dev_set_drvdata(&pdev->dev, audio_drv_data);
  1159. /* Initialize the ACP */
  1160. status = acp_init(audio_drv_data->acp_mmio, audio_drv_data->asic_type);
  1161. if (status) {
  1162. dev_err(&pdev->dev, "ACP Init failed status:%d\n", status);
  1163. return status;
  1164. }
  1165. status = devm_snd_soc_register_component(&pdev->dev,
  1166. &acp_asoc_platform, NULL, 0);
  1167. if (status != 0) {
  1168. dev_err(&pdev->dev, "Fail to register ALSA platform device\n");
  1169. return status;
  1170. }
  1171. pm_runtime_set_autosuspend_delay(&pdev->dev, 10000);
  1172. pm_runtime_use_autosuspend(&pdev->dev);
  1173. pm_runtime_enable(&pdev->dev);
  1174. return status;
  1175. }
  1176. static int acp_audio_remove(struct platform_device *pdev)
  1177. {
  1178. int status;
  1179. struct audio_drv_data *adata = dev_get_drvdata(&pdev->dev);
  1180. status = acp_deinit(adata->acp_mmio);
  1181. if (status)
  1182. dev_err(&pdev->dev, "ACP Deinit failed status:%d\n", status);
  1183. pm_runtime_disable(&pdev->dev);
  1184. return 0;
  1185. }
  1186. static int acp_pcm_resume(struct device *dev)
  1187. {
  1188. u16 bank;
  1189. int status;
  1190. struct audio_substream_data *rtd;
  1191. struct audio_drv_data *adata = dev_get_drvdata(dev);
  1192. status = acp_init(adata->acp_mmio, adata->asic_type);
  1193. if (status) {
  1194. dev_err(dev, "ACP Init failed status:%d\n", status);
  1195. return status;
  1196. }
  1197. if (adata->play_i2ssp_stream && adata->play_i2ssp_stream->runtime) {
  1198. /*
  1199. * For Stoney, Memory gating is disabled,i.e SRAM Banks
  1200. * won't be turned off. The default state for SRAM banks is ON.
  1201. * Setting SRAM bank state code skipped for STONEY platform.
  1202. */
  1203. if (adata->asic_type != CHIP_STONEY) {
  1204. for (bank = 1; bank <= 4; bank++)
  1205. acp_set_sram_bank_state(adata->acp_mmio, bank,
  1206. true);
  1207. }
  1208. rtd = adata->play_i2ssp_stream->runtime->private_data;
  1209. config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
  1210. }
  1211. if (adata->capture_i2ssp_stream &&
  1212. adata->capture_i2ssp_stream->runtime) {
  1213. if (adata->asic_type != CHIP_STONEY) {
  1214. for (bank = 5; bank <= 8; bank++)
  1215. acp_set_sram_bank_state(adata->acp_mmio, bank,
  1216. true);
  1217. }
  1218. rtd = adata->capture_i2ssp_stream->runtime->private_data;
  1219. config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
  1220. }
  1221. if (adata->asic_type != CHIP_CARRIZO) {
  1222. if (adata->play_i2sbt_stream &&
  1223. adata->play_i2sbt_stream->runtime) {
  1224. rtd = adata->play_i2sbt_stream->runtime->private_data;
  1225. config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
  1226. }
  1227. if (adata->capture_i2sbt_stream &&
  1228. adata->capture_i2sbt_stream->runtime) {
  1229. rtd = adata->capture_i2sbt_stream->runtime->private_data;
  1230. config_acp_dma(adata->acp_mmio, rtd, adata->asic_type);
  1231. }
  1232. }
  1233. acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
  1234. return 0;
  1235. }
  1236. static int acp_pcm_runtime_suspend(struct device *dev)
  1237. {
  1238. int status;
  1239. struct audio_drv_data *adata = dev_get_drvdata(dev);
  1240. status = acp_deinit(adata->acp_mmio);
  1241. if (status)
  1242. dev_err(dev, "ACP Deinit failed status:%d\n", status);
  1243. acp_reg_write(0, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
  1244. return 0;
  1245. }
  1246. static int acp_pcm_runtime_resume(struct device *dev)
  1247. {
  1248. int status;
  1249. struct audio_drv_data *adata = dev_get_drvdata(dev);
  1250. status = acp_init(adata->acp_mmio, adata->asic_type);
  1251. if (status) {
  1252. dev_err(dev, "ACP Init failed status:%d\n", status);
  1253. return status;
  1254. }
  1255. acp_reg_write(1, adata->acp_mmio, mmACP_EXTERNAL_INTR_ENB);
  1256. return 0;
  1257. }
  1258. static const struct dev_pm_ops acp_pm_ops = {
  1259. .resume = acp_pcm_resume,
  1260. .runtime_suspend = acp_pcm_runtime_suspend,
  1261. .runtime_resume = acp_pcm_runtime_resume,
  1262. };
  1263. static struct platform_driver acp_dma_driver = {
  1264. .probe = acp_audio_probe,
  1265. .remove = acp_audio_remove,
  1266. .driver = {
  1267. .name = DRV_NAME,
  1268. .pm = &acp_pm_ops,
  1269. },
  1270. };
  1271. module_platform_driver(acp_dma_driver);
  1272. MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
  1273. MODULE_AUTHOR("Maruthi.Bayyavarapu@amd.com");
  1274. MODULE_DESCRIPTION("AMD ACP PCM Driver");
  1275. MODULE_LICENSE("GPL v2");
  1276. MODULE_ALIAS("platform:"DRV_NAME);