aiu-fifo.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2020 BayLibre, SAS.
  4. // Author: Jerome Brunet <[email protected]>
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/dma-mapping.h>
  8. #include <sound/pcm_params.h>
  9. #include <sound/soc.h>
  10. #include <sound/soc-dai.h>
  11. #include "aiu-fifo.h"
  12. #define AIU_MEM_START 0x00
  13. #define AIU_MEM_RD 0x04
  14. #define AIU_MEM_END 0x08
  15. #define AIU_MEM_MASKS 0x0c
  16. #define AIU_MEM_MASK_CH_RD GENMASK(7, 0)
  17. #define AIU_MEM_MASK_CH_MEM GENMASK(15, 8)
  18. #define AIU_MEM_CONTROL 0x10
  19. #define AIU_MEM_CONTROL_INIT BIT(0)
  20. #define AIU_MEM_CONTROL_FILL_EN BIT(1)
  21. #define AIU_MEM_CONTROL_EMPTY_EN BIT(2)
  22. static struct snd_soc_dai *aiu_fifo_dai(struct snd_pcm_substream *ss)
  23. {
  24. struct snd_soc_pcm_runtime *rtd = ss->private_data;
  25. return asoc_rtd_to_cpu(rtd, 0);
  26. }
  27. snd_pcm_uframes_t aiu_fifo_pointer(struct snd_soc_component *component,
  28. struct snd_pcm_substream *substream)
  29. {
  30. struct snd_soc_dai *dai = aiu_fifo_dai(substream);
  31. struct aiu_fifo *fifo = dai->playback_dma_data;
  32. struct snd_pcm_runtime *runtime = substream->runtime;
  33. unsigned int addr;
  34. addr = snd_soc_component_read(component, fifo->mem_offset + AIU_MEM_RD);
  35. return bytes_to_frames(runtime, addr - (unsigned int)runtime->dma_addr);
  36. }
  37. static void aiu_fifo_enable(struct snd_soc_dai *dai, bool enable)
  38. {
  39. struct snd_soc_component *component = dai->component;
  40. struct aiu_fifo *fifo = dai->playback_dma_data;
  41. unsigned int en_mask = (AIU_MEM_CONTROL_FILL_EN |
  42. AIU_MEM_CONTROL_EMPTY_EN);
  43. snd_soc_component_update_bits(component,
  44. fifo->mem_offset + AIU_MEM_CONTROL,
  45. en_mask, enable ? en_mask : 0);
  46. }
  47. int aiu_fifo_trigger(struct snd_pcm_substream *substream, int cmd,
  48. struct snd_soc_dai *dai)
  49. {
  50. switch (cmd) {
  51. case SNDRV_PCM_TRIGGER_START:
  52. case SNDRV_PCM_TRIGGER_RESUME:
  53. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  54. aiu_fifo_enable(dai, true);
  55. break;
  56. case SNDRV_PCM_TRIGGER_SUSPEND:
  57. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  58. case SNDRV_PCM_TRIGGER_STOP:
  59. aiu_fifo_enable(dai, false);
  60. break;
  61. default:
  62. return -EINVAL;
  63. }
  64. return 0;
  65. }
  66. int aiu_fifo_prepare(struct snd_pcm_substream *substream,
  67. struct snd_soc_dai *dai)
  68. {
  69. struct snd_soc_component *component = dai->component;
  70. struct aiu_fifo *fifo = dai->playback_dma_data;
  71. snd_soc_component_update_bits(component,
  72. fifo->mem_offset + AIU_MEM_CONTROL,
  73. AIU_MEM_CONTROL_INIT,
  74. AIU_MEM_CONTROL_INIT);
  75. snd_soc_component_update_bits(component,
  76. fifo->mem_offset + AIU_MEM_CONTROL,
  77. AIU_MEM_CONTROL_INIT, 0);
  78. return 0;
  79. }
  80. int aiu_fifo_hw_params(struct snd_pcm_substream *substream,
  81. struct snd_pcm_hw_params *params,
  82. struct snd_soc_dai *dai)
  83. {
  84. struct snd_pcm_runtime *runtime = substream->runtime;
  85. struct snd_soc_component *component = dai->component;
  86. struct aiu_fifo *fifo = dai->playback_dma_data;
  87. dma_addr_t end;
  88. /* Setup the fifo boundaries */
  89. end = runtime->dma_addr + runtime->dma_bytes - fifo->fifo_block;
  90. snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_START,
  91. runtime->dma_addr);
  92. snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_RD,
  93. runtime->dma_addr);
  94. snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_END,
  95. end);
  96. /* Setup the fifo to read all the memory - no skip */
  97. snd_soc_component_update_bits(component,
  98. fifo->mem_offset + AIU_MEM_MASKS,
  99. AIU_MEM_MASK_CH_RD | AIU_MEM_MASK_CH_MEM,
  100. FIELD_PREP(AIU_MEM_MASK_CH_RD, 0xff) |
  101. FIELD_PREP(AIU_MEM_MASK_CH_MEM, 0xff));
  102. return 0;
  103. }
  104. static irqreturn_t aiu_fifo_isr(int irq, void *dev_id)
  105. {
  106. struct snd_pcm_substream *playback = dev_id;
  107. snd_pcm_period_elapsed(playback);
  108. return IRQ_HANDLED;
  109. }
  110. int aiu_fifo_startup(struct snd_pcm_substream *substream,
  111. struct snd_soc_dai *dai)
  112. {
  113. struct aiu_fifo *fifo = dai->playback_dma_data;
  114. int ret;
  115. snd_soc_set_runtime_hwparams(substream, fifo->pcm);
  116. /*
  117. * Make sure the buffer and period size are multiple of the fifo burst
  118. * size
  119. */
  120. ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
  121. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  122. fifo->fifo_block);
  123. if (ret)
  124. return ret;
  125. ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
  126. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  127. fifo->fifo_block);
  128. if (ret)
  129. return ret;
  130. ret = clk_prepare_enable(fifo->pclk);
  131. if (ret)
  132. return ret;
  133. ret = request_irq(fifo->irq, aiu_fifo_isr, 0, dev_name(dai->dev),
  134. substream);
  135. if (ret)
  136. clk_disable_unprepare(fifo->pclk);
  137. return ret;
  138. }
  139. void aiu_fifo_shutdown(struct snd_pcm_substream *substream,
  140. struct snd_soc_dai *dai)
  141. {
  142. struct aiu_fifo *fifo = dai->playback_dma_data;
  143. free_irq(fifo->irq, substream);
  144. clk_disable_unprepare(fifo->pclk);
  145. }
  146. int aiu_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd,
  147. struct snd_soc_dai *dai)
  148. {
  149. struct snd_card *card = rtd->card->snd_card;
  150. struct aiu_fifo *fifo = dai->playback_dma_data;
  151. size_t size = fifo->pcm->buffer_bytes_max;
  152. int ret;
  153. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  154. if (ret)
  155. return ret;
  156. snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
  157. card->dev, size, size);
  158. return 0;
  159. }
  160. int aiu_fifo_dai_probe(struct snd_soc_dai *dai)
  161. {
  162. struct aiu_fifo *fifo;
  163. fifo = kzalloc(sizeof(*fifo), GFP_KERNEL);
  164. if (!fifo)
  165. return -ENOMEM;
  166. dai->playback_dma_data = fifo;
  167. return 0;
  168. }
  169. int aiu_fifo_dai_remove(struct snd_soc_dai *dai)
  170. {
  171. kfree(dai->playback_dma_data);
  172. return 0;
  173. }