spdif_out.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * ALSA SoC SPDIF Out 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/soc.h>
  22. #include <sound/spear_dma.h>
  23. #include <sound/spear_spdif.h>
  24. #include "spdif_out_regs.h"
  25. #include "spear_pcm.h"
  26. struct spdif_out_params {
  27. u32 rate;
  28. u32 core_freq;
  29. u32 mute;
  30. };
  31. struct spdif_out_dev {
  32. struct clk *clk;
  33. struct spear_dma_data dma_params;
  34. struct spdif_out_params saved_params;
  35. u32 running;
  36. void __iomem *io_base;
  37. struct snd_dmaengine_dai_dma_data dma_params_tx;
  38. struct snd_dmaengine_pcm_config config;
  39. };
  40. static void spdif_out_configure(struct spdif_out_dev *host)
  41. {
  42. writel(SPDIF_OUT_RESET, host->io_base + SPDIF_OUT_SOFT_RST);
  43. mdelay(1);
  44. writel(readl(host->io_base + SPDIF_OUT_SOFT_RST) & ~SPDIF_OUT_RESET,
  45. host->io_base + SPDIF_OUT_SOFT_RST);
  46. writel(SPDIF_OUT_FDMA_TRIG_16 | SPDIF_OUT_MEMFMT_16_16 |
  47. SPDIF_OUT_VALID_HW | SPDIF_OUT_USER_HW |
  48. SPDIF_OUT_CHNLSTA_HW | SPDIF_OUT_PARITY_HW,
  49. host->io_base + SPDIF_OUT_CFG);
  50. writel(0x7F, host->io_base + SPDIF_OUT_INT_STA_CLR);
  51. writel(0x7F, host->io_base + SPDIF_OUT_INT_EN_CLR);
  52. }
  53. static int spdif_out_startup(struct snd_pcm_substream *substream,
  54. struct snd_soc_dai *cpu_dai)
  55. {
  56. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  57. int ret;
  58. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  59. return -EINVAL;
  60. ret = clk_enable(host->clk);
  61. if (ret)
  62. return ret;
  63. host->running = true;
  64. spdif_out_configure(host);
  65. return 0;
  66. }
  67. static void spdif_out_shutdown(struct snd_pcm_substream *substream,
  68. struct snd_soc_dai *dai)
  69. {
  70. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  71. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  72. return;
  73. clk_disable(host->clk);
  74. host->running = false;
  75. }
  76. static void spdif_out_clock(struct spdif_out_dev *host, u32 core_freq,
  77. u32 rate)
  78. {
  79. u32 divider, ctrl;
  80. clk_set_rate(host->clk, core_freq);
  81. divider = DIV_ROUND_CLOSEST(clk_get_rate(host->clk), (rate * 128));
  82. ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
  83. ctrl &= ~SPDIF_DIVIDER_MASK;
  84. ctrl |= (divider << SPDIF_DIVIDER_SHIFT) & SPDIF_DIVIDER_MASK;
  85. writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
  86. }
  87. static int spdif_out_hw_params(struct snd_pcm_substream *substream,
  88. struct snd_pcm_hw_params *params,
  89. struct snd_soc_dai *dai)
  90. {
  91. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  92. u32 rate, core_freq;
  93. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  94. return -EINVAL;
  95. rate = params_rate(params);
  96. switch (rate) {
  97. case 8000:
  98. case 16000:
  99. case 32000:
  100. case 64000:
  101. /*
  102. * The clock is multiplied by 10 to bring it to feasible range
  103. * of frequencies for sscg
  104. */
  105. core_freq = 64000 * 128 * 10; /* 81.92 MHz */
  106. break;
  107. case 5512:
  108. case 11025:
  109. case 22050:
  110. case 44100:
  111. case 88200:
  112. case 176400:
  113. core_freq = 176400 * 128; /* 22.5792 MHz */
  114. break;
  115. case 48000:
  116. case 96000:
  117. case 192000:
  118. default:
  119. core_freq = 192000 * 128; /* 24.576 MHz */
  120. break;
  121. }
  122. spdif_out_clock(host, core_freq, rate);
  123. host->saved_params.core_freq = core_freq;
  124. host->saved_params.rate = rate;
  125. return 0;
  126. }
  127. static int spdif_out_trigger(struct snd_pcm_substream *substream, int cmd,
  128. struct snd_soc_dai *dai)
  129. {
  130. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  131. u32 ctrl;
  132. int ret = 0;
  133. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  134. return -EINVAL;
  135. switch (cmd) {
  136. case SNDRV_PCM_TRIGGER_START:
  137. case SNDRV_PCM_TRIGGER_RESUME:
  138. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  139. ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
  140. ctrl &= ~SPDIF_OPMODE_MASK;
  141. if (!host->saved_params.mute)
  142. ctrl |= SPDIF_OPMODE_AUD_DATA |
  143. SPDIF_STATE_NORMAL;
  144. else
  145. ctrl |= SPDIF_OPMODE_MUTE_PCM;
  146. writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
  147. break;
  148. case SNDRV_PCM_TRIGGER_STOP:
  149. case SNDRV_PCM_TRIGGER_SUSPEND:
  150. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  151. ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
  152. ctrl &= ~SPDIF_OPMODE_MASK;
  153. ctrl |= SPDIF_OPMODE_OFF;
  154. writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
  155. break;
  156. default:
  157. ret = -EINVAL;
  158. break;
  159. }
  160. return ret;
  161. }
  162. static int spdif_digital_mute(struct snd_soc_dai *dai, int mute)
  163. {
  164. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  165. u32 val;
  166. host->saved_params.mute = mute;
  167. val = readl(host->io_base + SPDIF_OUT_CTRL);
  168. val &= ~SPDIF_OPMODE_MASK;
  169. if (mute)
  170. val |= SPDIF_OPMODE_MUTE_PCM;
  171. else {
  172. if (host->running)
  173. val |= SPDIF_OPMODE_AUD_DATA | SPDIF_STATE_NORMAL;
  174. else
  175. val |= SPDIF_OPMODE_OFF;
  176. }
  177. writel(val, host->io_base + SPDIF_OUT_CTRL);
  178. return 0;
  179. }
  180. static int spdif_mute_get(struct snd_kcontrol *kcontrol,
  181. struct snd_ctl_elem_value *ucontrol)
  182. {
  183. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  184. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  185. ucontrol->value.integer.value[0] = host->saved_params.mute;
  186. return 0;
  187. }
  188. static int spdif_mute_put(struct snd_kcontrol *kcontrol,
  189. struct snd_ctl_elem_value *ucontrol)
  190. {
  191. struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
  192. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  193. if (host->saved_params.mute == ucontrol->value.integer.value[0])
  194. return 0;
  195. spdif_digital_mute(cpu_dai, ucontrol->value.integer.value[0]);
  196. return 1;
  197. }
  198. static const struct snd_kcontrol_new spdif_out_controls[] = {
  199. SOC_SINGLE_BOOL_EXT("IEC958 Playback Switch", 0,
  200. spdif_mute_get, spdif_mute_put),
  201. };
  202. static int spdif_soc_dai_probe(struct snd_soc_dai *dai)
  203. {
  204. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  205. host->dma_params_tx.filter_data = &host->dma_params;
  206. dai->playback_dma_data = &host->dma_params_tx;
  207. return snd_soc_add_dai_controls(dai, spdif_out_controls,
  208. ARRAY_SIZE(spdif_out_controls));
  209. }
  210. static const struct snd_soc_dai_ops spdif_out_dai_ops = {
  211. .digital_mute = spdif_digital_mute,
  212. .startup = spdif_out_startup,
  213. .shutdown = spdif_out_shutdown,
  214. .trigger = spdif_out_trigger,
  215. .hw_params = spdif_out_hw_params,
  216. };
  217. static struct snd_soc_dai_driver spdif_out_dai = {
  218. .playback = {
  219. .channels_min = 2,
  220. .channels_max = 2,
  221. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  222. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
  223. SNDRV_PCM_RATE_192000),
  224. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  225. },
  226. .probe = spdif_soc_dai_probe,
  227. .ops = &spdif_out_dai_ops,
  228. };
  229. static const struct snd_soc_component_driver spdif_out_component = {
  230. .name = "spdif-out",
  231. };
  232. static int spdif_out_probe(struct platform_device *pdev)
  233. {
  234. struct spdif_out_dev *host;
  235. struct spear_spdif_platform_data *pdata;
  236. struct resource *res;
  237. int ret;
  238. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  239. if (!host) {
  240. dev_warn(&pdev->dev, "kzalloc fail\n");
  241. return -ENOMEM;
  242. }
  243. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  244. host->io_base = devm_ioremap_resource(&pdev->dev, res);
  245. if (IS_ERR(host->io_base))
  246. return PTR_ERR(host->io_base);
  247. host->clk = devm_clk_get(&pdev->dev, NULL);
  248. if (IS_ERR(host->clk))
  249. return PTR_ERR(host->clk);
  250. pdata = dev_get_platdata(&pdev->dev);
  251. host->dma_params.data = pdata->dma_params;
  252. host->dma_params.addr = res->start + SPDIF_OUT_FIFO_DATA;
  253. host->dma_params.max_burst = 16;
  254. host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  255. dev_set_drvdata(&pdev->dev, host);
  256. ret = devm_snd_soc_register_component(&pdev->dev, &spdif_out_component,
  257. &spdif_out_dai, 1);
  258. if (ret)
  259. return ret;
  260. return devm_spear_pcm_platform_register(&pdev->dev, &host->config,
  261. pdata->filter);
  262. }
  263. #ifdef CONFIG_PM
  264. static int spdif_out_suspend(struct device *dev)
  265. {
  266. struct platform_device *pdev = to_platform_device(dev);
  267. struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
  268. if (host->running)
  269. clk_disable(host->clk);
  270. return 0;
  271. }
  272. static int spdif_out_resume(struct device *dev)
  273. {
  274. struct platform_device *pdev = to_platform_device(dev);
  275. struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
  276. if (host->running) {
  277. clk_enable(host->clk);
  278. spdif_out_configure(host);
  279. spdif_out_clock(host, host->saved_params.core_freq,
  280. host->saved_params.rate);
  281. }
  282. return 0;
  283. }
  284. static SIMPLE_DEV_PM_OPS(spdif_out_dev_pm_ops, spdif_out_suspend, \
  285. spdif_out_resume);
  286. #define SPDIF_OUT_DEV_PM_OPS (&spdif_out_dev_pm_ops)
  287. #else
  288. #define SPDIF_OUT_DEV_PM_OPS NULL
  289. #endif
  290. static struct platform_driver spdif_out_driver = {
  291. .probe = spdif_out_probe,
  292. .driver = {
  293. .name = "spdif-out",
  294. .pm = SPDIF_OUT_DEV_PM_OPS,
  295. },
  296. };
  297. module_platform_driver(spdif_out_driver);
  298. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
  299. MODULE_DESCRIPTION("SPEAr SPDIF OUT SoC Interface");
  300. MODULE_LICENSE("GPL");
  301. MODULE_ALIAS("platform:spdif_out");