aiu-fifo.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2020 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  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. int ret;
  89. ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  90. if (ret < 0)
  91. return ret;
  92. /* Setup the fifo boundaries */
  93. end = runtime->dma_addr + runtime->dma_bytes - fifo->fifo_block;
  94. snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_START,
  95. runtime->dma_addr);
  96. snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_RD,
  97. runtime->dma_addr);
  98. snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_END,
  99. end);
  100. /* Setup the fifo to read all the memory - no skip */
  101. snd_soc_component_update_bits(component,
  102. fifo->mem_offset + AIU_MEM_MASKS,
  103. AIU_MEM_MASK_CH_RD | AIU_MEM_MASK_CH_MEM,
  104. FIELD_PREP(AIU_MEM_MASK_CH_RD, 0xff) |
  105. FIELD_PREP(AIU_MEM_MASK_CH_MEM, 0xff));
  106. return 0;
  107. }
  108. int aiu_fifo_hw_free(struct snd_pcm_substream *substream,
  109. struct snd_soc_dai *dai)
  110. {
  111. return snd_pcm_lib_free_pages(substream);
  112. }
  113. static irqreturn_t aiu_fifo_isr(int irq, void *dev_id)
  114. {
  115. struct snd_pcm_substream *playback = dev_id;
  116. snd_pcm_period_elapsed(playback);
  117. return IRQ_HANDLED;
  118. }
  119. int aiu_fifo_startup(struct snd_pcm_substream *substream,
  120. struct snd_soc_dai *dai)
  121. {
  122. struct aiu_fifo *fifo = dai->playback_dma_data;
  123. int ret;
  124. snd_soc_set_runtime_hwparams(substream, fifo->pcm);
  125. /*
  126. * Make sure the buffer and period size are multiple of the fifo burst
  127. * size
  128. */
  129. ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
  130. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  131. fifo->fifo_block);
  132. if (ret)
  133. return ret;
  134. ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
  135. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  136. fifo->fifo_block);
  137. if (ret)
  138. return ret;
  139. ret = clk_prepare_enable(fifo->pclk);
  140. if (ret)
  141. return ret;
  142. ret = request_irq(fifo->irq, aiu_fifo_isr, 0, dev_name(dai->dev),
  143. substream);
  144. if (ret)
  145. clk_disable_unprepare(fifo->pclk);
  146. return ret;
  147. }
  148. void aiu_fifo_shutdown(struct snd_pcm_substream *substream,
  149. struct snd_soc_dai *dai)
  150. {
  151. struct aiu_fifo *fifo = dai->playback_dma_data;
  152. free_irq(fifo->irq, substream);
  153. clk_disable_unprepare(fifo->pclk);
  154. }
  155. int aiu_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd,
  156. struct snd_soc_dai *dai)
  157. {
  158. struct snd_pcm_substream *substream =
  159. rtd->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  160. struct snd_card *card = rtd->card->snd_card;
  161. struct aiu_fifo *fifo = dai->playback_dma_data;
  162. size_t size = fifo->pcm->buffer_bytes_max;
  163. int ret;
  164. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  165. if (ret)
  166. return ret;
  167. snd_pcm_lib_preallocate_pages(substream,
  168. SNDRV_DMA_TYPE_DEV,
  169. card->dev, size, size);
  170. return 0;
  171. }
  172. int aiu_fifo_dai_probe(struct snd_soc_dai *dai)
  173. {
  174. struct aiu_fifo *fifo;
  175. fifo = kzalloc(sizeof(*fifo), GFP_KERNEL);
  176. if (!fifo)
  177. return -ENOMEM;
  178. dai->playback_dma_data = fifo;
  179. return 0;
  180. }
  181. int aiu_fifo_dai_remove(struct snd_soc_dai *dai)
  182. {
  183. kfree(dai->playback_dma_data);
  184. return 0;
  185. }