spdif_out.c 9.3 KB

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