axg-fifo.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. //
  3. // Copyright (c) 2018 BayLibre, SAS.
  4. // Author: Jerome Brunet <jbrunet@baylibre.com>
  5. #include <linux/clk.h>
  6. #include <linux/of_irq.h>
  7. #include <linux/of_platform.h>
  8. #include <linux/module.h>
  9. #include <linux/regmap.h>
  10. #include <linux/reset.h>
  11. #include <sound/pcm_params.h>
  12. #include <sound/soc.h>
  13. #include <sound/soc-dai.h>
  14. #include "axg-fifo.h"
  15. /*
  16. * This file implements the platform operations common to the playback and
  17. * capture frontend DAI. The logic behind this two types of fifo is very
  18. * similar but some difference exist.
  19. * These differences the respective DAI drivers
  20. */
  21. static struct snd_pcm_hardware axg_fifo_hw = {
  22. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  23. SNDRV_PCM_INFO_MMAP |
  24. SNDRV_PCM_INFO_MMAP_VALID |
  25. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  26. SNDRV_PCM_INFO_PAUSE),
  27. .formats = AXG_FIFO_FORMATS,
  28. .rate_min = 5512,
  29. .rate_max = 192000,
  30. .channels_min = 1,
  31. .channels_max = AXG_FIFO_CH_MAX,
  32. .period_bytes_min = AXG_FIFO_MIN_DEPTH,
  33. .period_bytes_max = UINT_MAX,
  34. .periods_min = 2,
  35. .periods_max = UINT_MAX,
  36. /* No real justification for this */
  37. .buffer_bytes_max = 1 * 1024 * 1024,
  38. };
  39. static struct snd_soc_dai *axg_fifo_dai(struct snd_pcm_substream *ss)
  40. {
  41. struct snd_soc_pcm_runtime *rtd = ss->private_data;
  42. return rtd->cpu_dai;
  43. }
  44. static struct axg_fifo *axg_fifo_data(struct snd_pcm_substream *ss)
  45. {
  46. struct snd_soc_dai *dai = axg_fifo_dai(ss);
  47. return snd_soc_dai_get_drvdata(dai);
  48. }
  49. static struct device *axg_fifo_dev(struct snd_pcm_substream *ss)
  50. {
  51. struct snd_soc_dai *dai = axg_fifo_dai(ss);
  52. return dai->dev;
  53. }
  54. static void __dma_enable(struct axg_fifo *fifo, bool enable)
  55. {
  56. regmap_update_bits(fifo->map, FIFO_CTRL0, CTRL0_DMA_EN,
  57. enable ? CTRL0_DMA_EN : 0);
  58. }
  59. static int axg_fifo_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
  60. {
  61. struct axg_fifo *fifo = axg_fifo_data(ss);
  62. switch (cmd) {
  63. case SNDRV_PCM_TRIGGER_START:
  64. case SNDRV_PCM_TRIGGER_RESUME:
  65. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  66. __dma_enable(fifo, true);
  67. break;
  68. case SNDRV_PCM_TRIGGER_SUSPEND:
  69. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  70. case SNDRV_PCM_TRIGGER_STOP:
  71. __dma_enable(fifo, false);
  72. break;
  73. default:
  74. return -EINVAL;
  75. }
  76. return 0;
  77. }
  78. static snd_pcm_uframes_t axg_fifo_pcm_pointer(struct snd_pcm_substream *ss)
  79. {
  80. struct axg_fifo *fifo = axg_fifo_data(ss);
  81. struct snd_pcm_runtime *runtime = ss->runtime;
  82. unsigned int addr;
  83. regmap_read(fifo->map, FIFO_STATUS2, &addr);
  84. return bytes_to_frames(runtime, addr - (unsigned int)runtime->dma_addr);
  85. }
  86. static int axg_fifo_pcm_hw_params(struct snd_pcm_substream *ss,
  87. struct snd_pcm_hw_params *params)
  88. {
  89. struct snd_pcm_runtime *runtime = ss->runtime;
  90. struct axg_fifo *fifo = axg_fifo_data(ss);
  91. dma_addr_t end_ptr;
  92. unsigned int burst_num;
  93. int ret;
  94. ret = snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(params));
  95. if (ret < 0)
  96. return ret;
  97. /* Setup dma memory pointers */
  98. end_ptr = runtime->dma_addr + runtime->dma_bytes - AXG_FIFO_BURST;
  99. regmap_write(fifo->map, FIFO_START_ADDR, runtime->dma_addr);
  100. regmap_write(fifo->map, FIFO_FINISH_ADDR, end_ptr);
  101. /* Setup interrupt periodicity */
  102. burst_num = params_period_bytes(params) / AXG_FIFO_BURST;
  103. regmap_write(fifo->map, FIFO_INT_ADDR, burst_num);
  104. /* Enable block count irq */
  105. regmap_update_bits(fifo->map, FIFO_CTRL0,
  106. CTRL0_INT_EN(FIFO_INT_COUNT_REPEAT),
  107. CTRL0_INT_EN(FIFO_INT_COUNT_REPEAT));
  108. return 0;
  109. }
  110. static int axg_fifo_pcm_hw_free(struct snd_pcm_substream *ss)
  111. {
  112. struct axg_fifo *fifo = axg_fifo_data(ss);
  113. /* Disable the block count irq */
  114. regmap_update_bits(fifo->map, FIFO_CTRL0,
  115. CTRL0_INT_EN(FIFO_INT_COUNT_REPEAT), 0);
  116. return snd_pcm_lib_free_pages(ss);
  117. }
  118. static void axg_fifo_ack_irq(struct axg_fifo *fifo, u8 mask)
  119. {
  120. regmap_update_bits(fifo->map, FIFO_CTRL1,
  121. CTRL1_INT_CLR(FIFO_INT_MASK),
  122. CTRL1_INT_CLR(mask));
  123. /* Clear must also be cleared */
  124. regmap_update_bits(fifo->map, FIFO_CTRL1,
  125. CTRL1_INT_CLR(FIFO_INT_MASK),
  126. 0);
  127. }
  128. static irqreturn_t axg_fifo_pcm_irq_block(int irq, void *dev_id)
  129. {
  130. struct snd_pcm_substream *ss = dev_id;
  131. struct axg_fifo *fifo = axg_fifo_data(ss);
  132. unsigned int status;
  133. regmap_read(fifo->map, FIFO_STATUS1, &status);
  134. status = STATUS1_INT_STS(status) & FIFO_INT_MASK;
  135. if (status & FIFO_INT_COUNT_REPEAT)
  136. snd_pcm_period_elapsed(ss);
  137. else
  138. dev_dbg(axg_fifo_dev(ss), "unexpected irq - STS 0x%02x\n",
  139. status);
  140. /* Ack irqs */
  141. axg_fifo_ack_irq(fifo, status);
  142. return !status ? IRQ_NONE : IRQ_HANDLED;
  143. }
  144. static int axg_fifo_pcm_open(struct snd_pcm_substream *ss)
  145. {
  146. struct axg_fifo *fifo = axg_fifo_data(ss);
  147. struct device *dev = axg_fifo_dev(ss);
  148. int ret;
  149. snd_soc_set_runtime_hwparams(ss, &axg_fifo_hw);
  150. /*
  151. * Make sure the buffer and period size are multiple of the FIFO
  152. * minimum depth size
  153. */
  154. ret = snd_pcm_hw_constraint_step(ss->runtime, 0,
  155. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  156. AXG_FIFO_MIN_DEPTH);
  157. if (ret)
  158. return ret;
  159. ret = snd_pcm_hw_constraint_step(ss->runtime, 0,
  160. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  161. AXG_FIFO_MIN_DEPTH);
  162. if (ret)
  163. return ret;
  164. ret = request_irq(fifo->irq, axg_fifo_pcm_irq_block, 0,
  165. dev_name(dev), ss);
  166. /* Enable pclk to access registers and clock the fifo ip */
  167. ret = clk_prepare_enable(fifo->pclk);
  168. if (ret)
  169. return ret;
  170. /* Setup status2 so it reports the memory pointer */
  171. regmap_update_bits(fifo->map, FIFO_CTRL1,
  172. CTRL1_STATUS2_SEL_MASK,
  173. CTRL1_STATUS2_SEL(STATUS2_SEL_DDR_READ));
  174. /* Make sure the dma is initially disabled */
  175. __dma_enable(fifo, false);
  176. /* Disable irqs until params are ready */
  177. regmap_update_bits(fifo->map, FIFO_CTRL0,
  178. CTRL0_INT_EN(FIFO_INT_MASK), 0);
  179. /* Clear any pending interrupt */
  180. axg_fifo_ack_irq(fifo, FIFO_INT_MASK);
  181. /* Take memory arbitror out of reset */
  182. ret = reset_control_deassert(fifo->arb);
  183. if (ret)
  184. clk_disable_unprepare(fifo->pclk);
  185. return ret;
  186. }
  187. static int axg_fifo_pcm_close(struct snd_pcm_substream *ss)
  188. {
  189. struct axg_fifo *fifo = axg_fifo_data(ss);
  190. int ret;
  191. /* Put the memory arbitror back in reset */
  192. ret = reset_control_assert(fifo->arb);
  193. /* Disable fifo ip and register access */
  194. clk_disable_unprepare(fifo->pclk);
  195. /* remove IRQ */
  196. free_irq(fifo->irq, ss);
  197. return ret;
  198. }
  199. const struct snd_pcm_ops axg_fifo_pcm_ops = {
  200. .open = axg_fifo_pcm_open,
  201. .close = axg_fifo_pcm_close,
  202. .ioctl = snd_pcm_lib_ioctl,
  203. .hw_params = axg_fifo_pcm_hw_params,
  204. .hw_free = axg_fifo_pcm_hw_free,
  205. .pointer = axg_fifo_pcm_pointer,
  206. .trigger = axg_fifo_pcm_trigger,
  207. };
  208. EXPORT_SYMBOL_GPL(axg_fifo_pcm_ops);
  209. int axg_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd, unsigned int type)
  210. {
  211. struct snd_card *card = rtd->card->snd_card;
  212. size_t size = axg_fifo_hw.buffer_bytes_max;
  213. return snd_pcm_lib_preallocate_pages(rtd->pcm->streams[type].substream,
  214. SNDRV_DMA_TYPE_DEV, card->dev,
  215. size, size);
  216. }
  217. EXPORT_SYMBOL_GPL(axg_fifo_pcm_new);
  218. static const struct regmap_config axg_fifo_regmap_cfg = {
  219. .reg_bits = 32,
  220. .val_bits = 32,
  221. .reg_stride = 4,
  222. .max_register = FIFO_STATUS2,
  223. };
  224. int axg_fifo_probe(struct platform_device *pdev)
  225. {
  226. struct device *dev = &pdev->dev;
  227. const struct axg_fifo_match_data *data;
  228. struct axg_fifo *fifo;
  229. struct resource *res;
  230. void __iomem *regs;
  231. data = of_device_get_match_data(dev);
  232. if (!data) {
  233. dev_err(dev, "failed to match device\n");
  234. return -ENODEV;
  235. }
  236. fifo = devm_kzalloc(dev, sizeof(*fifo), GFP_KERNEL);
  237. if (!fifo)
  238. return -ENOMEM;
  239. platform_set_drvdata(pdev, fifo);
  240. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  241. regs = devm_ioremap_resource(dev, res);
  242. if (IS_ERR(regs))
  243. return PTR_ERR(regs);
  244. fifo->map = devm_regmap_init_mmio(dev, regs, &axg_fifo_regmap_cfg);
  245. if (IS_ERR(fifo->map)) {
  246. dev_err(dev, "failed to init regmap: %ld\n",
  247. PTR_ERR(fifo->map));
  248. return PTR_ERR(fifo->map);
  249. }
  250. fifo->pclk = devm_clk_get(dev, NULL);
  251. if (IS_ERR(fifo->pclk)) {
  252. if (PTR_ERR(fifo->pclk) != -EPROBE_DEFER)
  253. dev_err(dev, "failed to get pclk: %ld\n",
  254. PTR_ERR(fifo->pclk));
  255. return PTR_ERR(fifo->pclk);
  256. }
  257. fifo->arb = devm_reset_control_get_exclusive(dev, NULL);
  258. if (IS_ERR(fifo->arb)) {
  259. if (PTR_ERR(fifo->arb) != -EPROBE_DEFER)
  260. dev_err(dev, "failed to get arb reset: %ld\n",
  261. PTR_ERR(fifo->arb));
  262. return PTR_ERR(fifo->arb);
  263. }
  264. fifo->irq = of_irq_get(dev->of_node, 0);
  265. if (fifo->irq <= 0) {
  266. dev_err(dev, "failed to get irq: %d\n", fifo->irq);
  267. return fifo->irq;
  268. }
  269. return devm_snd_soc_register_component(dev, data->component_drv,
  270. data->dai_drv, 1);
  271. }
  272. EXPORT_SYMBOL_GPL(axg_fifo_probe);
  273. MODULE_DESCRIPTION("Amlogic AXG fifo driver");
  274. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  275. MODULE_LICENSE("GPL v2");