axg-fifo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/of_irq.h>
  8. #include <linux/of_platform.h>
  9. #include <linux/module.h>
  10. #include <linux/regmap.h>
  11. #include <linux/reset.h>
  12. #include <sound/pcm_params.h>
  13. #include <sound/soc.h>
  14. #include <sound/soc-dai.h>
  15. #include "axg-fifo.h"
  16. /*
  17. * This file implements the platform operations common to the playback and
  18. * capture frontend DAI. The logic behind this two types of fifo is very
  19. * similar but some difference exist.
  20. * These differences are handled in the respective DAI drivers
  21. */
  22. static const struct snd_pcm_hardware axg_fifo_hw = {
  23. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  24. SNDRV_PCM_INFO_MMAP |
  25. SNDRV_PCM_INFO_MMAP_VALID |
  26. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  27. SNDRV_PCM_INFO_PAUSE |
  28. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
  29. .formats = AXG_FIFO_FORMATS,
  30. .rate_min = 5512,
  31. .rate_max = 768000,
  32. .channels_min = 1,
  33. .channels_max = AXG_FIFO_CH_MAX,
  34. .period_bytes_min = AXG_FIFO_BURST,
  35. .period_bytes_max = UINT_MAX,
  36. .periods_min = 2,
  37. .periods_max = UINT_MAX,
  38. /* No real justification for this */
  39. .buffer_bytes_max = 1 * 1024 * 1024,
  40. };
  41. static struct snd_soc_dai *axg_fifo_dai(struct snd_pcm_substream *ss)
  42. {
  43. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(ss);
  44. return snd_soc_rtd_to_cpu(rtd, 0);
  45. }
  46. static struct axg_fifo *axg_fifo_data(struct snd_pcm_substream *ss)
  47. {
  48. struct snd_soc_dai *dai = axg_fifo_dai(ss);
  49. return snd_soc_dai_get_drvdata(dai);
  50. }
  51. static struct device *axg_fifo_dev(struct snd_pcm_substream *ss)
  52. {
  53. struct snd_soc_dai *dai = axg_fifo_dai(ss);
  54. return dai->dev;
  55. }
  56. static void __dma_enable(struct axg_fifo *fifo, bool enable)
  57. {
  58. regmap_update_bits(fifo->map, FIFO_CTRL0, CTRL0_DMA_EN,
  59. enable ? CTRL0_DMA_EN : 0);
  60. }
  61. int axg_fifo_pcm_trigger(struct snd_soc_component *component,
  62. struct snd_pcm_substream *ss, int cmd)
  63. {
  64. struct axg_fifo *fifo = axg_fifo_data(ss);
  65. switch (cmd) {
  66. case SNDRV_PCM_TRIGGER_START:
  67. case SNDRV_PCM_TRIGGER_RESUME:
  68. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  69. __dma_enable(fifo, true);
  70. break;
  71. case SNDRV_PCM_TRIGGER_SUSPEND:
  72. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  73. case SNDRV_PCM_TRIGGER_STOP:
  74. __dma_enable(fifo, false);
  75. break;
  76. default:
  77. return -EINVAL;
  78. }
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(axg_fifo_pcm_trigger);
  82. snd_pcm_uframes_t axg_fifo_pcm_pointer(struct snd_soc_component *component,
  83. struct snd_pcm_substream *ss)
  84. {
  85. struct axg_fifo *fifo = axg_fifo_data(ss);
  86. struct snd_pcm_runtime *runtime = ss->runtime;
  87. unsigned int addr;
  88. regmap_read(fifo->map, FIFO_STATUS2, &addr);
  89. return bytes_to_frames(runtime, addr - (unsigned int)runtime->dma_addr);
  90. }
  91. EXPORT_SYMBOL_GPL(axg_fifo_pcm_pointer);
  92. int axg_fifo_pcm_hw_params(struct snd_soc_component *component,
  93. struct snd_pcm_substream *ss,
  94. struct snd_pcm_hw_params *params)
  95. {
  96. struct snd_pcm_runtime *runtime = ss->runtime;
  97. struct axg_fifo *fifo = axg_fifo_data(ss);
  98. unsigned int burst_num, period, threshold, irq_en;
  99. dma_addr_t end_ptr;
  100. period = params_period_bytes(params);
  101. /* Setup dma memory pointers */
  102. end_ptr = runtime->dma_addr + runtime->dma_bytes - AXG_FIFO_BURST;
  103. regmap_write(fifo->map, FIFO_START_ADDR, runtime->dma_addr);
  104. regmap_write(fifo->map, FIFO_FINISH_ADDR, end_ptr);
  105. /* Setup interrupt periodicity */
  106. burst_num = period / AXG_FIFO_BURST;
  107. regmap_write(fifo->map, FIFO_INT_ADDR, burst_num);
  108. /*
  109. * Start the fifo request on the smallest of the following:
  110. * - Half the fifo size
  111. * - Half the period size
  112. */
  113. threshold = min(period / 2, fifo->depth / 2);
  114. /*
  115. * With the threshold in bytes, register value is:
  116. * V = (threshold / burst) - 1
  117. */
  118. threshold /= AXG_FIFO_BURST;
  119. regmap_field_write(fifo->field_threshold,
  120. threshold ? threshold - 1 : 0);
  121. /* Enable irq if necessary */
  122. irq_en = runtime->no_period_wakeup ? 0 : FIFO_INT_COUNT_REPEAT;
  123. regmap_update_bits(fifo->map, FIFO_CTRL0,
  124. CTRL0_INT_EN,
  125. FIELD_PREP(CTRL0_INT_EN, irq_en));
  126. return 0;
  127. }
  128. EXPORT_SYMBOL_GPL(axg_fifo_pcm_hw_params);
  129. int g12a_fifo_pcm_hw_params(struct snd_soc_component *component,
  130. struct snd_pcm_substream *ss,
  131. struct snd_pcm_hw_params *params)
  132. {
  133. struct axg_fifo *fifo = axg_fifo_data(ss);
  134. struct snd_pcm_runtime *runtime = ss->runtime;
  135. int ret;
  136. ret = axg_fifo_pcm_hw_params(component, ss, params);
  137. if (ret)
  138. return ret;
  139. /* Set the initial memory address of the DMA */
  140. regmap_write(fifo->map, FIFO_INIT_ADDR, runtime->dma_addr);
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(g12a_fifo_pcm_hw_params);
  144. int axg_fifo_pcm_hw_free(struct snd_soc_component *component,
  145. struct snd_pcm_substream *ss)
  146. {
  147. struct axg_fifo *fifo = axg_fifo_data(ss);
  148. /* Disable irqs */
  149. regmap_update_bits(fifo->map, FIFO_CTRL0,
  150. CTRL0_INT_EN, 0);
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(axg_fifo_pcm_hw_free);
  154. static void axg_fifo_ack_irq(struct axg_fifo *fifo, u8 mask)
  155. {
  156. regmap_update_bits(fifo->map, FIFO_CTRL1,
  157. CTRL1_INT_CLR,
  158. FIELD_PREP(CTRL1_INT_CLR, mask));
  159. /* Clear must also be cleared */
  160. regmap_update_bits(fifo->map, FIFO_CTRL1,
  161. CTRL1_INT_CLR,
  162. FIELD_PREP(CTRL1_INT_CLR, 0));
  163. }
  164. static irqreturn_t axg_fifo_pcm_irq_block(int irq, void *dev_id)
  165. {
  166. struct snd_pcm_substream *ss = dev_id;
  167. struct axg_fifo *fifo = axg_fifo_data(ss);
  168. unsigned int status;
  169. regmap_read(fifo->map, FIFO_STATUS1, &status);
  170. status = FIELD_GET(STATUS1_INT_STS, status);
  171. axg_fifo_ack_irq(fifo, status);
  172. if (status & ~FIFO_INT_COUNT_REPEAT)
  173. dev_dbg(axg_fifo_dev(ss), "unexpected irq - STS 0x%02x\n",
  174. status);
  175. if (status & FIFO_INT_COUNT_REPEAT) {
  176. snd_pcm_period_elapsed(ss);
  177. return IRQ_HANDLED;
  178. }
  179. return IRQ_NONE;
  180. }
  181. int axg_fifo_pcm_open(struct snd_soc_component *component,
  182. struct snd_pcm_substream *ss)
  183. {
  184. struct axg_fifo *fifo = axg_fifo_data(ss);
  185. struct device *dev = axg_fifo_dev(ss);
  186. int ret;
  187. snd_soc_set_runtime_hwparams(ss, &axg_fifo_hw);
  188. /*
  189. * Make sure the buffer and period size are multiple of the FIFO
  190. * burst
  191. */
  192. ret = snd_pcm_hw_constraint_step(ss->runtime, 0,
  193. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  194. AXG_FIFO_BURST);
  195. if (ret)
  196. return ret;
  197. ret = snd_pcm_hw_constraint_step(ss->runtime, 0,
  198. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  199. AXG_FIFO_BURST);
  200. if (ret)
  201. return ret;
  202. /* Use the threaded irq handler only with non-atomic links */
  203. ret = request_threaded_irq(fifo->irq, NULL,
  204. axg_fifo_pcm_irq_block,
  205. IRQF_ONESHOT, dev_name(dev), ss);
  206. if (ret)
  207. return ret;
  208. /* Enable pclk to access registers and clock the fifo ip */
  209. ret = clk_prepare_enable(fifo->pclk);
  210. if (ret)
  211. goto free_irq;
  212. /* Setup status2 so it reports the memory pointer */
  213. regmap_update_bits(fifo->map, FIFO_CTRL1,
  214. CTRL1_STATUS2_SEL,
  215. FIELD_PREP(CTRL1_STATUS2_SEL, STATUS2_SEL_DDR_READ));
  216. /* Make sure the dma is initially disabled */
  217. __dma_enable(fifo, false);
  218. /* Disable irqs until params are ready */
  219. regmap_update_bits(fifo->map, FIFO_CTRL0,
  220. CTRL0_INT_EN, 0);
  221. /* Clear any pending interrupt */
  222. axg_fifo_ack_irq(fifo, FIFO_INT_MASK);
  223. /* Take memory arbitror out of reset */
  224. ret = reset_control_deassert(fifo->arb);
  225. if (ret)
  226. goto free_clk;
  227. return 0;
  228. free_clk:
  229. clk_disable_unprepare(fifo->pclk);
  230. free_irq:
  231. free_irq(fifo->irq, ss);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL_GPL(axg_fifo_pcm_open);
  235. int axg_fifo_pcm_close(struct snd_soc_component *component,
  236. struct snd_pcm_substream *ss)
  237. {
  238. struct axg_fifo *fifo = axg_fifo_data(ss);
  239. int ret;
  240. /* Put the memory arbitror back in reset */
  241. ret = reset_control_assert(fifo->arb);
  242. /* Disable fifo ip and register access */
  243. clk_disable_unprepare(fifo->pclk);
  244. /* remove IRQ */
  245. free_irq(fifo->irq, ss);
  246. return ret;
  247. }
  248. EXPORT_SYMBOL_GPL(axg_fifo_pcm_close);
  249. int axg_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd, unsigned int type)
  250. {
  251. struct snd_card *card = rtd->card->snd_card;
  252. size_t size = axg_fifo_hw.buffer_bytes_max;
  253. snd_pcm_set_managed_buffer(rtd->pcm->streams[type].substream,
  254. SNDRV_DMA_TYPE_DEV, card->dev,
  255. size, size);
  256. return 0;
  257. }
  258. EXPORT_SYMBOL_GPL(axg_fifo_pcm_new);
  259. static const struct regmap_config axg_fifo_regmap_cfg = {
  260. .reg_bits = 32,
  261. .val_bits = 32,
  262. .reg_stride = 4,
  263. .max_register = FIFO_CTRL2,
  264. };
  265. int axg_fifo_probe(struct platform_device *pdev)
  266. {
  267. struct device *dev = &pdev->dev;
  268. const struct axg_fifo_match_data *data;
  269. struct axg_fifo *fifo;
  270. void __iomem *regs;
  271. int ret;
  272. data = of_device_get_match_data(dev);
  273. if (!data) {
  274. dev_err(dev, "failed to match device\n");
  275. return -ENODEV;
  276. }
  277. fifo = devm_kzalloc(dev, sizeof(*fifo), GFP_KERNEL);
  278. if (!fifo)
  279. return -ENOMEM;
  280. platform_set_drvdata(pdev, fifo);
  281. regs = devm_platform_ioremap_resource(pdev, 0);
  282. if (IS_ERR(regs))
  283. return PTR_ERR(regs);
  284. fifo->map = devm_regmap_init_mmio(dev, regs, &axg_fifo_regmap_cfg);
  285. if (IS_ERR(fifo->map)) {
  286. dev_err(dev, "failed to init regmap: %ld\n",
  287. PTR_ERR(fifo->map));
  288. return PTR_ERR(fifo->map);
  289. }
  290. fifo->pclk = devm_clk_get(dev, NULL);
  291. if (IS_ERR(fifo->pclk))
  292. return dev_err_probe(dev, PTR_ERR(fifo->pclk), "failed to get pclk\n");
  293. fifo->arb = devm_reset_control_get_exclusive(dev, NULL);
  294. if (IS_ERR(fifo->arb))
  295. return dev_err_probe(dev, PTR_ERR(fifo->arb), "failed to get arb reset\n");
  296. fifo->irq = of_irq_get(dev->of_node, 0);
  297. if (fifo->irq <= 0) {
  298. dev_err(dev, "failed to get irq: %d\n", fifo->irq);
  299. return fifo->irq;
  300. }
  301. fifo->field_threshold =
  302. devm_regmap_field_alloc(dev, fifo->map, data->field_threshold);
  303. if (IS_ERR(fifo->field_threshold))
  304. return PTR_ERR(fifo->field_threshold);
  305. ret = of_property_read_u32(dev->of_node, "amlogic,fifo-depth",
  306. &fifo->depth);
  307. if (ret) {
  308. /* Error out for anything but a missing property */
  309. if (ret != -EINVAL)
  310. return ret;
  311. /*
  312. * If the property is missing, it might be because of an old
  313. * DT. In such case, assume the smallest known fifo depth
  314. */
  315. fifo->depth = 256;
  316. dev_warn(dev, "fifo depth not found, assume %u bytes\n",
  317. fifo->depth);
  318. }
  319. return devm_snd_soc_register_component(dev, data->component_drv,
  320. data->dai_drv, 1);
  321. }
  322. EXPORT_SYMBOL_GPL(axg_fifo_probe);
  323. MODULE_DESCRIPTION("Amlogic AXG/G12A fifo driver");
  324. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  325. MODULE_LICENSE("GPL v2");