axg-fifo.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 IRQ_RETVAL(status);
  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. if (ret)
  167. return ret;
  168. /* Enable pclk to access registers and clock the fifo ip */
  169. ret = clk_prepare_enable(fifo->pclk);
  170. if (ret)
  171. return ret;
  172. /* Setup status2 so it reports the memory pointer */
  173. regmap_update_bits(fifo->map, FIFO_CTRL1,
  174. CTRL1_STATUS2_SEL_MASK,
  175. CTRL1_STATUS2_SEL(STATUS2_SEL_DDR_READ));
  176. /* Make sure the dma is initially disabled */
  177. __dma_enable(fifo, false);
  178. /* Disable irqs until params are ready */
  179. regmap_update_bits(fifo->map, FIFO_CTRL0,
  180. CTRL0_INT_EN(FIFO_INT_MASK), 0);
  181. /* Clear any pending interrupt */
  182. axg_fifo_ack_irq(fifo, FIFO_INT_MASK);
  183. /* Take memory arbitror out of reset */
  184. ret = reset_control_deassert(fifo->arb);
  185. if (ret)
  186. clk_disable_unprepare(fifo->pclk);
  187. return ret;
  188. }
  189. static int axg_fifo_pcm_close(struct snd_pcm_substream *ss)
  190. {
  191. struct axg_fifo *fifo = axg_fifo_data(ss);
  192. int ret;
  193. /* Put the memory arbitror back in reset */
  194. ret = reset_control_assert(fifo->arb);
  195. /* Disable fifo ip and register access */
  196. clk_disable_unprepare(fifo->pclk);
  197. /* remove IRQ */
  198. free_irq(fifo->irq, ss);
  199. return ret;
  200. }
  201. const struct snd_pcm_ops axg_fifo_pcm_ops = {
  202. .open = axg_fifo_pcm_open,
  203. .close = axg_fifo_pcm_close,
  204. .ioctl = snd_pcm_lib_ioctl,
  205. .hw_params = axg_fifo_pcm_hw_params,
  206. .hw_free = axg_fifo_pcm_hw_free,
  207. .pointer = axg_fifo_pcm_pointer,
  208. .trigger = axg_fifo_pcm_trigger,
  209. };
  210. EXPORT_SYMBOL_GPL(axg_fifo_pcm_ops);
  211. int axg_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd, unsigned int type)
  212. {
  213. struct snd_card *card = rtd->card->snd_card;
  214. size_t size = axg_fifo_hw.buffer_bytes_max;
  215. return snd_pcm_lib_preallocate_pages(rtd->pcm->streams[type].substream,
  216. SNDRV_DMA_TYPE_DEV, card->dev,
  217. size, size);
  218. }
  219. EXPORT_SYMBOL_GPL(axg_fifo_pcm_new);
  220. static const struct regmap_config axg_fifo_regmap_cfg = {
  221. .reg_bits = 32,
  222. .val_bits = 32,
  223. .reg_stride = 4,
  224. .max_register = FIFO_STATUS2,
  225. };
  226. int axg_fifo_probe(struct platform_device *pdev)
  227. {
  228. struct device *dev = &pdev->dev;
  229. const struct axg_fifo_match_data *data;
  230. struct axg_fifo *fifo;
  231. struct resource *res;
  232. void __iomem *regs;
  233. data = of_device_get_match_data(dev);
  234. if (!data) {
  235. dev_err(dev, "failed to match device\n");
  236. return -ENODEV;
  237. }
  238. fifo = devm_kzalloc(dev, sizeof(*fifo), GFP_KERNEL);
  239. if (!fifo)
  240. return -ENOMEM;
  241. platform_set_drvdata(pdev, fifo);
  242. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  243. regs = devm_ioremap_resource(dev, res);
  244. if (IS_ERR(regs))
  245. return PTR_ERR(regs);
  246. fifo->map = devm_regmap_init_mmio(dev, regs, &axg_fifo_regmap_cfg);
  247. if (IS_ERR(fifo->map)) {
  248. dev_err(dev, "failed to init regmap: %ld\n",
  249. PTR_ERR(fifo->map));
  250. return PTR_ERR(fifo->map);
  251. }
  252. fifo->pclk = devm_clk_get(dev, NULL);
  253. if (IS_ERR(fifo->pclk)) {
  254. if (PTR_ERR(fifo->pclk) != -EPROBE_DEFER)
  255. dev_err(dev, "failed to get pclk: %ld\n",
  256. PTR_ERR(fifo->pclk));
  257. return PTR_ERR(fifo->pclk);
  258. }
  259. fifo->arb = devm_reset_control_get_exclusive(dev, NULL);
  260. if (IS_ERR(fifo->arb)) {
  261. if (PTR_ERR(fifo->arb) != -EPROBE_DEFER)
  262. dev_err(dev, "failed to get arb reset: %ld\n",
  263. PTR_ERR(fifo->arb));
  264. return PTR_ERR(fifo->arb);
  265. }
  266. fifo->irq = of_irq_get(dev->of_node, 0);
  267. if (fifo->irq <= 0) {
  268. dev_err(dev, "failed to get irq: %d\n", fifo->irq);
  269. return fifo->irq;
  270. }
  271. return devm_snd_soc_register_component(dev, data->component_drv,
  272. data->dai_drv, 1);
  273. }
  274. EXPORT_SYMBOL_GPL(axg_fifo_probe);
  275. MODULE_DESCRIPTION("Amlogic AXG fifo driver");
  276. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  277. MODULE_LICENSE("GPL v2");