spdif_in.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 const struct snd_soc_component_driver spdif_in_component = {
  145. .name = "spdif-in",
  146. };
  147. static irqreturn_t spdif_in_irq(int irq, void *arg)
  148. {
  149. struct spdif_in_dev *host = (struct spdif_in_dev *)arg;
  150. u32 irq_status = readl(host->io_base + SPDIF_IN_IRQ);
  151. if (!irq_status)
  152. return IRQ_NONE;
  153. if (irq_status & SPDIF_IRQ_FIFOWRITE)
  154. dev_err(host->dev, "spdif in: fifo write error");
  155. if (irq_status & SPDIF_IRQ_EMPTYFIFOREAD)
  156. dev_err(host->dev, "spdif in: empty fifo read error");
  157. if (irq_status & SPDIF_IRQ_FIFOFULL)
  158. dev_err(host->dev, "spdif in: fifo full error");
  159. if (irq_status & SPDIF_IRQ_OUTOFRANGE)
  160. dev_err(host->dev, "spdif in: out of range error");
  161. writel(0, host->io_base + SPDIF_IN_IRQ);
  162. return IRQ_HANDLED;
  163. }
  164. static int spdif_in_probe(struct platform_device *pdev)
  165. {
  166. struct spdif_in_dev *host;
  167. struct spear_spdif_platform_data *pdata;
  168. struct resource *res, *res_fifo;
  169. int ret;
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. if (!res)
  172. return -EINVAL;
  173. res_fifo = platform_get_resource(pdev, IORESOURCE_IO, 0);
  174. if (!res_fifo)
  175. return -EINVAL;
  176. if (!devm_request_mem_region(&pdev->dev, res->start,
  177. resource_size(res), pdev->name)) {
  178. dev_warn(&pdev->dev, "Failed to get memory resourse\n");
  179. return -ENOENT;
  180. }
  181. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  182. if (!host) {
  183. dev_warn(&pdev->dev, "kzalloc fail\n");
  184. return -ENOMEM;
  185. }
  186. host->io_base = devm_ioremap(&pdev->dev, res->start,
  187. resource_size(res));
  188. if (!host->io_base) {
  189. dev_warn(&pdev->dev, "ioremap failed\n");
  190. return -ENOMEM;
  191. }
  192. host->irq = platform_get_irq(pdev, 0);
  193. if (host->irq < 0)
  194. return -EINVAL;
  195. host->clk = clk_get(&pdev->dev, NULL);
  196. if (IS_ERR(host->clk))
  197. return PTR_ERR(host->clk);
  198. pdata = dev_get_platdata(&pdev->dev);
  199. if (!pdata)
  200. return -EINVAL;
  201. host->dma_params.data = pdata->dma_params;
  202. host->dma_params.addr = res_fifo->start;
  203. host->dma_params.max_burst = 16;
  204. host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  205. host->dma_params.filter = pdata->filter;
  206. host->reset_perip = pdata->reset_perip;
  207. host->dev = &pdev->dev;
  208. dev_set_drvdata(&pdev->dev, host);
  209. ret = devm_request_irq(&pdev->dev, host->irq, spdif_in_irq, 0,
  210. "spdif-in", host);
  211. if (ret) {
  212. clk_put(host->clk);
  213. dev_warn(&pdev->dev, "request_irq failed\n");
  214. return ret;
  215. }
  216. ret = snd_soc_register_component(&pdev->dev, &spdif_in_component,
  217. &spdif_in_dai, 1);
  218. if (ret != 0) {
  219. clk_put(host->clk);
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. static int spdif_in_remove(struct platform_device *pdev)
  225. {
  226. struct spdif_in_dev *host = dev_get_drvdata(&pdev->dev);
  227. snd_soc_unregister_component(&pdev->dev);
  228. dev_set_drvdata(&pdev->dev, NULL);
  229. clk_put(host->clk);
  230. return 0;
  231. }
  232. static struct platform_driver spdif_in_driver = {
  233. .probe = spdif_in_probe,
  234. .remove = spdif_in_remove,
  235. .driver = {
  236. .name = "spdif-in",
  237. .owner = THIS_MODULE,
  238. },
  239. };
  240. module_platform_driver(spdif_in_driver);
  241. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
  242. MODULE_DESCRIPTION("SPEAr SPDIF IN SoC Interface");
  243. MODULE_LICENSE("GPL");
  244. MODULE_ALIAS("platform:spdif_in");