spdif_in.c 6.8 KB

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