spdif_out.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. ret = clk_enable(host->clk);
  57. if (ret)
  58. return ret;
  59. host->running = true;
  60. spdif_out_configure(host);
  61. return 0;
  62. }
  63. static void spdif_out_shutdown(struct snd_pcm_substream *substream,
  64. struct snd_soc_dai *dai)
  65. {
  66. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  67. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  68. return;
  69. clk_disable(host->clk);
  70. host->running = false;
  71. }
  72. static void spdif_out_clock(struct spdif_out_dev *host, u32 core_freq,
  73. u32 rate)
  74. {
  75. u32 divider, ctrl;
  76. clk_set_rate(host->clk, core_freq);
  77. divider = DIV_ROUND_CLOSEST(clk_get_rate(host->clk), (rate * 128));
  78. ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
  79. ctrl &= ~SPDIF_DIVIDER_MASK;
  80. ctrl |= (divider << SPDIF_DIVIDER_SHIFT) & SPDIF_DIVIDER_MASK;
  81. writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
  82. }
  83. static int spdif_out_hw_params(struct snd_pcm_substream *substream,
  84. struct snd_pcm_hw_params *params,
  85. struct snd_soc_dai *dai)
  86. {
  87. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  88. u32 rate, core_freq;
  89. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  90. return -EINVAL;
  91. rate = params_rate(params);
  92. switch (rate) {
  93. case 8000:
  94. case 16000:
  95. case 32000:
  96. case 64000:
  97. /*
  98. * The clock is multiplied by 10 to bring it to feasible range
  99. * of frequencies for sscg
  100. */
  101. core_freq = 64000 * 128 * 10; /* 81.92 MHz */
  102. break;
  103. case 5512:
  104. case 11025:
  105. case 22050:
  106. case 44100:
  107. case 88200:
  108. case 176400:
  109. core_freq = 176400 * 128; /* 22.5792 MHz */
  110. break;
  111. case 48000:
  112. case 96000:
  113. case 192000:
  114. default:
  115. core_freq = 192000 * 128; /* 24.576 MHz */
  116. break;
  117. }
  118. spdif_out_clock(host, core_freq, rate);
  119. host->saved_params.core_freq = core_freq;
  120. host->saved_params.rate = rate;
  121. return 0;
  122. }
  123. static int spdif_out_trigger(struct snd_pcm_substream *substream, int cmd,
  124. struct snd_soc_dai *dai)
  125. {
  126. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  127. u32 ctrl;
  128. int ret = 0;
  129. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  130. return -EINVAL;
  131. switch (cmd) {
  132. case SNDRV_PCM_TRIGGER_START:
  133. case SNDRV_PCM_TRIGGER_RESUME:
  134. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  135. ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
  136. ctrl &= ~SPDIF_OPMODE_MASK;
  137. if (!host->saved_params.mute)
  138. ctrl |= SPDIF_OPMODE_AUD_DATA |
  139. SPDIF_STATE_NORMAL;
  140. else
  141. ctrl |= SPDIF_OPMODE_MUTE_PCM;
  142. writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
  143. break;
  144. case SNDRV_PCM_TRIGGER_STOP:
  145. case SNDRV_PCM_TRIGGER_SUSPEND:
  146. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  147. ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
  148. ctrl &= ~SPDIF_OPMODE_MASK;
  149. ctrl |= SPDIF_OPMODE_OFF;
  150. writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
  151. break;
  152. default:
  153. ret = -EINVAL;
  154. break;
  155. }
  156. return ret;
  157. }
  158. static int spdif_digital_mute(struct snd_soc_dai *dai, int mute)
  159. {
  160. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  161. u32 val;
  162. host->saved_params.mute = mute;
  163. val = readl(host->io_base + SPDIF_OUT_CTRL);
  164. val &= ~SPDIF_OPMODE_MASK;
  165. if (mute)
  166. val |= SPDIF_OPMODE_MUTE_PCM;
  167. else {
  168. if (host->running)
  169. val |= SPDIF_OPMODE_AUD_DATA | SPDIF_STATE_NORMAL;
  170. else
  171. val |= SPDIF_OPMODE_OFF;
  172. }
  173. writel(val, host->io_base + SPDIF_OUT_CTRL);
  174. return 0;
  175. }
  176. static int spdif_mute_get(struct snd_kcontrol *kcontrol,
  177. struct snd_ctl_elem_value *ucontrol)
  178. {
  179. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  180. struct snd_soc_card *card = codec->card;
  181. struct snd_soc_pcm_runtime *rtd = card->rtd;
  182. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  183. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  184. ucontrol->value.integer.value[0] = host->saved_params.mute;
  185. return 0;
  186. }
  187. static int spdif_mute_put(struct snd_kcontrol *kcontrol,
  188. struct snd_ctl_elem_value *ucontrol)
  189. {
  190. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  191. struct snd_soc_card *card = codec->card;
  192. struct snd_soc_pcm_runtime *rtd = card->rtd;
  193. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  194. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
  195. if (host->saved_params.mute == ucontrol->value.integer.value[0])
  196. return 0;
  197. spdif_digital_mute(cpu_dai, ucontrol->value.integer.value[0]);
  198. return 1;
  199. }
  200. static const struct snd_kcontrol_new spdif_out_controls[] = {
  201. SOC_SINGLE_BOOL_EXT("IEC958 Playback Switch", 0,
  202. spdif_mute_get, spdif_mute_put),
  203. };
  204. static int spdif_soc_dai_probe(struct snd_soc_dai *dai)
  205. {
  206. struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
  207. dai->playback_dma_data = &host->dma_params;
  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 const struct snd_soc_component_driver spdif_out_component = {
  231. .name = "spdif-out",
  232. };
  233. static int spdif_out_probe(struct platform_device *pdev)
  234. {
  235. struct spdif_out_dev *host;
  236. struct spear_spdif_platform_data *pdata;
  237. struct resource *res;
  238. int ret;
  239. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  240. if (!host) {
  241. dev_warn(&pdev->dev, "kzalloc fail\n");
  242. return -ENOMEM;
  243. }
  244. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  245. host->io_base = devm_ioremap_resource(&pdev->dev, res);
  246. if (IS_ERR(host->io_base))
  247. return PTR_ERR(host->io_base);
  248. host->clk = devm_clk_get(&pdev->dev, NULL);
  249. if (IS_ERR(host->clk))
  250. return PTR_ERR(host->clk);
  251. pdata = dev_get_platdata(&pdev->dev);
  252. host->dma_params.data = pdata->dma_params;
  253. host->dma_params.addr = res->start + SPDIF_OUT_FIFO_DATA;
  254. host->dma_params.max_burst = 16;
  255. host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  256. host->dma_params.filter = pdata->filter;
  257. dev_set_drvdata(&pdev->dev, host);
  258. ret = snd_soc_register_component(&pdev->dev, &spdif_out_component,
  259. &spdif_out_dai, 1);
  260. return ret;
  261. }
  262. static int spdif_out_remove(struct platform_device *pdev)
  263. {
  264. snd_soc_unregister_component(&pdev->dev);
  265. return 0;
  266. }
  267. #ifdef CONFIG_PM
  268. static int spdif_out_suspend(struct device *dev)
  269. {
  270. struct platform_device *pdev = to_platform_device(dev);
  271. struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
  272. if (host->running)
  273. clk_disable(host->clk);
  274. return 0;
  275. }
  276. static int spdif_out_resume(struct device *dev)
  277. {
  278. struct platform_device *pdev = to_platform_device(dev);
  279. struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
  280. if (host->running) {
  281. clk_enable(host->clk);
  282. spdif_out_configure(host);
  283. spdif_out_clock(host, host->saved_params.core_freq,
  284. host->saved_params.rate);
  285. }
  286. return 0;
  287. }
  288. static SIMPLE_DEV_PM_OPS(spdif_out_dev_pm_ops, spdif_out_suspend, \
  289. spdif_out_resume);
  290. #define SPDIF_OUT_DEV_PM_OPS (&spdif_out_dev_pm_ops)
  291. #else
  292. #define SPDIF_OUT_DEV_PM_OPS NULL
  293. #endif
  294. static struct platform_driver spdif_out_driver = {
  295. .probe = spdif_out_probe,
  296. .remove = spdif_out_remove,
  297. .driver = {
  298. .name = "spdif-out",
  299. .owner = THIS_MODULE,
  300. .pm = SPDIF_OUT_DEV_PM_OPS,
  301. },
  302. };
  303. module_platform_driver(spdif_out_driver);
  304. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
  305. MODULE_DESCRIPTION("SPEAr SPDIF OUT SoC Interface");
  306. MODULE_LICENSE("GPL");
  307. MODULE_ALIAS("platform:spdif_out");