spdif_in.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <sound/spear_dma.h>
  24. #include <sound/spear_spdif.h>
  25. #include "spdif_in_regs.h"
  26. struct spdif_in_params {
  27. u32 format;
  28. };
  29. struct spdif_in_dev {
  30. struct clk *clk;
  31. struct spear_dma_data dma_params;
  32. struct spdif_in_params saved_params;
  33. void *io_base;
  34. struct device *dev;
  35. void (*reset_perip)(void);
  36. int irq;
  37. };
  38. static void spdif_in_configure(struct spdif_in_dev *host)
  39. {
  40. u32 ctrl = SPDIF_IN_PRTYEN | SPDIF_IN_STATEN | SPDIF_IN_USREN |
  41. SPDIF_IN_VALEN | SPDIF_IN_BLKEN;
  42. ctrl |= SPDIF_MODE_16BIT | SPDIF_FIFO_THRES_16;
  43. writel(ctrl, host->io_base + SPDIF_IN_CTRL);
  44. writel(0xF, host->io_base + SPDIF_IN_IRQ_MASK);
  45. }
  46. static int spdif_in_startup(struct snd_pcm_substream *substream,
  47. struct snd_soc_dai *cpu_dai)
  48. {
  49. struct spdif_in_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  50. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  51. return -EINVAL;
  52. snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)&host->dma_params);
  53. return 0;
  54. }
  55. static void spdif_in_shutdown(struct snd_pcm_substream *substream,
  56. struct snd_soc_dai *dai)
  57. {
  58. struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
  59. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  60. return;
  61. writel(0x0, host->io_base + SPDIF_IN_IRQ_MASK);
  62. snd_soc_dai_set_dma_data(dai, substream, NULL);
  63. }
  64. static void spdif_in_format(struct spdif_in_dev *host, u32 format)
  65. {
  66. u32 ctrl = readl(host->io_base + SPDIF_IN_CTRL);
  67. switch (format) {
  68. case SNDRV_PCM_FORMAT_S16_LE:
  69. ctrl |= SPDIF_XTRACT_16BIT;
  70. break;
  71. case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
  72. ctrl &= ~SPDIF_XTRACT_16BIT;
  73. break;
  74. }
  75. writel(ctrl, host->io_base + SPDIF_IN_CTRL);
  76. }
  77. static int spdif_in_hw_params(struct snd_pcm_substream *substream,
  78. struct snd_pcm_hw_params *params,
  79. struct snd_soc_dai *dai)
  80. {
  81. struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
  82. u32 format;
  83. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  84. return -EINVAL;
  85. format = params_format(params);
  86. host->saved_params.format = format;
  87. return 0;
  88. }
  89. static int spdif_in_trigger(struct snd_pcm_substream *substream, int cmd,
  90. struct snd_soc_dai *dai)
  91. {
  92. struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
  93. u32 ctrl;
  94. int ret = 0;
  95. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  96. return -EINVAL;
  97. switch (cmd) {
  98. case SNDRV_PCM_TRIGGER_START:
  99. case SNDRV_PCM_TRIGGER_RESUME:
  100. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  101. clk_enable(host->clk);
  102. spdif_in_configure(host);
  103. spdif_in_format(host, host->saved_params.format);
  104. ctrl = readl(host->io_base + SPDIF_IN_CTRL);
  105. ctrl |= SPDIF_IN_SAMPLE | SPDIF_IN_ENB;
  106. writel(ctrl, host->io_base + SPDIF_IN_CTRL);
  107. writel(0xF, host->io_base + SPDIF_IN_IRQ_MASK);
  108. break;
  109. case SNDRV_PCM_TRIGGER_STOP:
  110. case SNDRV_PCM_TRIGGER_SUSPEND:
  111. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  112. ctrl = readl(host->io_base + SPDIF_IN_CTRL);
  113. ctrl &= ~(SPDIF_IN_SAMPLE | SPDIF_IN_ENB);
  114. writel(ctrl, host->io_base + SPDIF_IN_CTRL);
  115. writel(0x0, host->io_base + SPDIF_IN_IRQ_MASK);
  116. if (host->reset_perip)
  117. host->reset_perip();
  118. clk_disable(host->clk);
  119. break;
  120. default:
  121. ret = -EINVAL;
  122. break;
  123. }
  124. return ret;
  125. }
  126. static struct snd_soc_dai_ops spdif_in_dai_ops = {
  127. .startup = spdif_in_startup,
  128. .shutdown = spdif_in_shutdown,
  129. .trigger = spdif_in_trigger,
  130. .hw_params = spdif_in_hw_params,
  131. };
  132. struct snd_soc_dai_driver spdif_in_dai = {
  133. .capture = {
  134. .channels_min = 2,
  135. .channels_max = 2,
  136. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  137. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
  138. SNDRV_PCM_RATE_192000),
  139. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  140. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  141. },
  142. .ops = &spdif_in_dai_ops,
  143. };
  144. static irqreturn_t spdif_in_irq(int irq, void *arg)
  145. {
  146. struct spdif_in_dev *host = (struct spdif_in_dev *)arg;
  147. u32 irq_status = readl(host->io_base + SPDIF_IN_IRQ);
  148. if (!irq_status)
  149. return IRQ_NONE;
  150. if (irq_status & SPDIF_IRQ_FIFOWRITE)
  151. dev_err(host->dev, "spdif in: fifo write error");
  152. if (irq_status & SPDIF_IRQ_EMPTYFIFOREAD)
  153. dev_err(host->dev, "spdif in: empty fifo read error");
  154. if (irq_status & SPDIF_IRQ_FIFOFULL)
  155. dev_err(host->dev, "spdif in: fifo full error");
  156. if (irq_status & SPDIF_IRQ_OUTOFRANGE)
  157. dev_err(host->dev, "spdif in: out of range error");
  158. writel(0, host->io_base + SPDIF_IN_IRQ);
  159. return IRQ_HANDLED;
  160. }
  161. static int spdif_in_probe(struct platform_device *pdev)
  162. {
  163. struct spdif_in_dev *host;
  164. struct spear_spdif_platform_data *pdata;
  165. struct resource *res, *res_fifo;
  166. int ret;
  167. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  168. if (!res)
  169. return -EINVAL;
  170. res_fifo = platform_get_resource(pdev, IORESOURCE_IO, 0);
  171. if (!res_fifo)
  172. return -EINVAL;
  173. if (!devm_request_mem_region(&pdev->dev, res->start,
  174. resource_size(res), pdev->name)) {
  175. dev_warn(&pdev->dev, "Failed to get memory resourse\n");
  176. return -ENOENT;
  177. }
  178. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  179. if (!host) {
  180. dev_warn(&pdev->dev, "kzalloc fail\n");
  181. return -ENOMEM;
  182. }
  183. host->io_base = devm_ioremap(&pdev->dev, res->start,
  184. resource_size(res));
  185. if (!host->io_base) {
  186. dev_warn(&pdev->dev, "ioremap failed\n");
  187. return -ENOMEM;
  188. }
  189. host->irq = platform_get_irq(pdev, 0);
  190. if (host->irq < 0)
  191. return -EINVAL;
  192. host->clk = clk_get(&pdev->dev, NULL);
  193. if (IS_ERR(host->clk))
  194. return PTR_ERR(host->clk);
  195. pdata = dev_get_platdata(&pdev->dev);
  196. if (!pdata)
  197. return -EINVAL;
  198. host->dma_params.data = pdata->dma_params;
  199. host->dma_params.addr = res_fifo->start;
  200. host->dma_params.max_burst = 16;
  201. host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  202. host->dma_params.filter = pdata->filter;
  203. host->reset_perip = pdata->reset_perip;
  204. host->dev = &pdev->dev;
  205. dev_set_drvdata(&pdev->dev, host);
  206. ret = devm_request_irq(&pdev->dev, host->irq, spdif_in_irq, 0,
  207. "spdif-in", host);
  208. if (ret) {
  209. clk_put(host->clk);
  210. dev_warn(&pdev->dev, "request_irq failed\n");
  211. return ret;
  212. }
  213. ret = snd_soc_register_dai(&pdev->dev, &spdif_in_dai);
  214. if (ret != 0) {
  215. clk_put(host->clk);
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. static int spdif_in_remove(struct platform_device *pdev)
  221. {
  222. struct spdif_in_dev *host = dev_get_drvdata(&pdev->dev);
  223. snd_soc_unregister_dai(&pdev->dev);
  224. dev_set_drvdata(&pdev->dev, NULL);
  225. clk_put(host->clk);
  226. return 0;
  227. }
  228. static struct platform_driver spdif_in_driver = {
  229. .probe = spdif_in_probe,
  230. .remove = spdif_in_remove,
  231. .driver = {
  232. .name = "spdif-in",
  233. .owner = THIS_MODULE,
  234. },
  235. };
  236. module_platform_driver(spdif_in_driver);
  237. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
  238. MODULE_DESCRIPTION("SPEAr SPDIF IN SoC Interface");
  239. MODULE_LICENSE("GPL");
  240. MODULE_ALIAS("platform:spdif_in");