spdif_in.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * ALSA SoC SPDIF In Audio Layer for spear processors
  3. *
  4. * Copyright (C) 2012 ST Microelectronics
  5. * Vipin Kumar <vipin.kumar@st.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/ioport.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <sound/dmaengine_pcm.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/spear_dma.h>
  25. #include <sound/spear_spdif.h>
  26. #include "spdif_in_regs.h"
  27. #include "spear_pcm.h"
  28. struct spdif_in_params {
  29. u32 format;
  30. };
  31. struct spdif_in_dev {
  32. struct clk *clk;
  33. struct spear_dma_data dma_params;
  34. struct spdif_in_params saved_params;
  35. void *io_base;
  36. struct device *dev;
  37. void (*reset_perip)(void);
  38. int irq;
  39. struct snd_dmaengine_dai_dma_data dma_params_rx;
  40. struct snd_dmaengine_pcm_config config;
  41. };
  42. static void spdif_in_configure(struct spdif_in_dev *host)
  43. {
  44. u32 ctrl = SPDIF_IN_PRTYEN | SPDIF_IN_STATEN | SPDIF_IN_USREN |
  45. SPDIF_IN_VALEN | SPDIF_IN_BLKEN;
  46. ctrl |= SPDIF_MODE_16BIT | SPDIF_FIFO_THRES_16;
  47. writel(ctrl, host->io_base + SPDIF_IN_CTRL);
  48. writel(0xF, host->io_base + SPDIF_IN_IRQ_MASK);
  49. }
  50. static int spdif_in_dai_probe(struct snd_soc_dai *dai)
  51. {
  52. struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
  53. host->dma_params_rx.filter_data = &host->dma_params;
  54. dai->capture_dma_data = &host->dma_params_rx;
  55. return 0;
  56. }
  57. static void spdif_in_shutdown(struct snd_pcm_substream *substream,
  58. struct snd_soc_dai *dai)
  59. {
  60. struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
  61. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  62. return;
  63. writel(0x0, host->io_base + SPDIF_IN_IRQ_MASK);
  64. }
  65. static void spdif_in_format(struct spdif_in_dev *host, u32 format)
  66. {
  67. u32 ctrl = readl(host->io_base + SPDIF_IN_CTRL);
  68. switch (format) {
  69. case SNDRV_PCM_FORMAT_S16_LE:
  70. ctrl |= SPDIF_XTRACT_16BIT;
  71. break;
  72. case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
  73. ctrl &= ~SPDIF_XTRACT_16BIT;
  74. break;
  75. }
  76. writel(ctrl, host->io_base + SPDIF_IN_CTRL);
  77. }
  78. static int spdif_in_hw_params(struct snd_pcm_substream *substream,
  79. struct snd_pcm_hw_params *params,
  80. struct snd_soc_dai *dai)
  81. {
  82. struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
  83. u32 format;
  84. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  85. return -EINVAL;
  86. format = params_format(params);
  87. host->saved_params.format = format;
  88. return 0;
  89. }
  90. static int spdif_in_trigger(struct snd_pcm_substream *substream, int cmd,
  91. struct snd_soc_dai *dai)
  92. {
  93. struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
  94. u32 ctrl;
  95. int ret = 0;
  96. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  97. return -EINVAL;
  98. switch (cmd) {
  99. case SNDRV_PCM_TRIGGER_START:
  100. case SNDRV_PCM_TRIGGER_RESUME:
  101. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  102. clk_enable(host->clk);
  103. spdif_in_configure(host);
  104. spdif_in_format(host, host->saved_params.format);
  105. ctrl = readl(host->io_base + SPDIF_IN_CTRL);
  106. ctrl |= SPDIF_IN_SAMPLE | SPDIF_IN_ENB;
  107. writel(ctrl, host->io_base + SPDIF_IN_CTRL);
  108. writel(0xF, host->io_base + SPDIF_IN_IRQ_MASK);
  109. break;
  110. case SNDRV_PCM_TRIGGER_STOP:
  111. case SNDRV_PCM_TRIGGER_SUSPEND:
  112. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  113. ctrl = readl(host->io_base + SPDIF_IN_CTRL);
  114. ctrl &= ~(SPDIF_IN_SAMPLE | SPDIF_IN_ENB);
  115. writel(ctrl, host->io_base + SPDIF_IN_CTRL);
  116. writel(0x0, host->io_base + SPDIF_IN_IRQ_MASK);
  117. if (host->reset_perip)
  118. host->reset_perip();
  119. clk_disable(host->clk);
  120. break;
  121. default:
  122. ret = -EINVAL;
  123. break;
  124. }
  125. return ret;
  126. }
  127. static struct snd_soc_dai_ops spdif_in_dai_ops = {
  128. .shutdown = spdif_in_shutdown,
  129. .trigger = spdif_in_trigger,
  130. .hw_params = spdif_in_hw_params,
  131. };
  132. static struct snd_soc_dai_driver spdif_in_dai = {
  133. .probe = spdif_in_dai_probe,
  134. .capture = {
  135. .channels_min = 2,
  136. .channels_max = 2,
  137. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  138. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
  139. SNDRV_PCM_RATE_192000),
  140. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  141. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  142. },
  143. .ops = &spdif_in_dai_ops,
  144. };
  145. static const struct snd_soc_component_driver spdif_in_component = {
  146. .name = "spdif-in",
  147. };
  148. static irqreturn_t spdif_in_irq(int irq, void *arg)
  149. {
  150. struct spdif_in_dev *host = (struct spdif_in_dev *)arg;
  151. u32 irq_status = readl(host->io_base + SPDIF_IN_IRQ);
  152. if (!irq_status)
  153. return IRQ_NONE;
  154. if (irq_status & SPDIF_IRQ_FIFOWRITE)
  155. dev_err(host->dev, "spdif in: fifo write error");
  156. if (irq_status & SPDIF_IRQ_EMPTYFIFOREAD)
  157. dev_err(host->dev, "spdif in: empty fifo read error");
  158. if (irq_status & SPDIF_IRQ_FIFOFULL)
  159. dev_err(host->dev, "spdif in: fifo full error");
  160. if (irq_status & SPDIF_IRQ_OUTOFRANGE)
  161. dev_err(host->dev, "spdif in: out of range error");
  162. writel(0, host->io_base + SPDIF_IN_IRQ);
  163. return IRQ_HANDLED;
  164. }
  165. static int spdif_in_probe(struct platform_device *pdev)
  166. {
  167. struct spdif_in_dev *host;
  168. struct spear_spdif_platform_data *pdata;
  169. struct resource *res, *res_fifo;
  170. void __iomem *io_base;
  171. int ret;
  172. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  173. io_base = devm_ioremap_resource(&pdev->dev, res);
  174. if (IS_ERR(io_base))
  175. return PTR_ERR(io_base);
  176. res_fifo = platform_get_resource(pdev, IORESOURCE_IO, 0);
  177. if (!res_fifo)
  178. return -EINVAL;
  179. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  180. if (!host) {
  181. dev_warn(&pdev->dev, "kzalloc fail\n");
  182. return -ENOMEM;
  183. }
  184. host->io_base = io_base;
  185. host->irq = platform_get_irq(pdev, 0);
  186. if (host->irq < 0)
  187. return -EINVAL;
  188. host->clk = devm_clk_get(&pdev->dev, NULL);
  189. if (IS_ERR(host->clk))
  190. return PTR_ERR(host->clk);
  191. pdata = dev_get_platdata(&pdev->dev);
  192. if (!pdata)
  193. return -EINVAL;
  194. host->dma_params.data = pdata->dma_params;
  195. host->dma_params.addr = res_fifo->start;
  196. host->dma_params.max_burst = 16;
  197. host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  198. host->reset_perip = pdata->reset_perip;
  199. host->dev = &pdev->dev;
  200. dev_set_drvdata(&pdev->dev, host);
  201. ret = devm_request_irq(&pdev->dev, host->irq, spdif_in_irq, 0,
  202. "spdif-in", host);
  203. if (ret) {
  204. dev_warn(&pdev->dev, "request_irq failed\n");
  205. return ret;
  206. }
  207. ret = devm_snd_soc_register_component(&pdev->dev, &spdif_in_component,
  208. &spdif_in_dai, 1);
  209. if (ret)
  210. return ret;
  211. return devm_spear_pcm_platform_register(&pdev->dev, &host->config,
  212. pdata->filter);
  213. }
  214. static struct platform_driver spdif_in_driver = {
  215. .probe = spdif_in_probe,
  216. .driver = {
  217. .name = "spdif-in",
  218. },
  219. };
  220. module_platform_driver(spdif_in_driver);
  221. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
  222. MODULE_DESCRIPTION("SPEAr SPDIF IN SoC Interface");
  223. MODULE_LICENSE("GPL");
  224. MODULE_ALIAS("platform:spdif_in");