tegra_spdif.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * tegra_spdif.c - Tegra SPDIF driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2011 - NVIDIA, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/module.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/slab.h>
  29. #include <linux/io.h>
  30. #include <mach/iomap.h>
  31. #include <sound/core.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include "tegra_spdif.h"
  36. #define DRV_NAME "tegra-spdif"
  37. static inline void tegra_spdif_write(struct tegra_spdif *spdif, u32 reg,
  38. u32 val)
  39. {
  40. __raw_writel(val, spdif->regs + reg);
  41. }
  42. static inline u32 tegra_spdif_read(struct tegra_spdif *spdif, u32 reg)
  43. {
  44. return __raw_readl(spdif->regs + reg);
  45. }
  46. #ifdef CONFIG_DEBUG_FS
  47. static int tegra_spdif_show(struct seq_file *s, void *unused)
  48. {
  49. #define REG(r) { r, #r }
  50. static const struct {
  51. int offset;
  52. const char *name;
  53. } regs[] = {
  54. REG(TEGRA_SPDIF_CTRL),
  55. REG(TEGRA_SPDIF_STATUS),
  56. REG(TEGRA_SPDIF_STROBE_CTRL),
  57. REG(TEGRA_SPDIF_DATA_FIFO_CSR),
  58. REG(TEGRA_SPDIF_CH_STA_RX_A),
  59. REG(TEGRA_SPDIF_CH_STA_RX_B),
  60. REG(TEGRA_SPDIF_CH_STA_RX_C),
  61. REG(TEGRA_SPDIF_CH_STA_RX_D),
  62. REG(TEGRA_SPDIF_CH_STA_RX_E),
  63. REG(TEGRA_SPDIF_CH_STA_RX_F),
  64. REG(TEGRA_SPDIF_CH_STA_TX_A),
  65. REG(TEGRA_SPDIF_CH_STA_TX_B),
  66. REG(TEGRA_SPDIF_CH_STA_TX_C),
  67. REG(TEGRA_SPDIF_CH_STA_TX_D),
  68. REG(TEGRA_SPDIF_CH_STA_TX_E),
  69. REG(TEGRA_SPDIF_CH_STA_TX_F),
  70. };
  71. #undef REG
  72. struct tegra_spdif *spdif = s->private;
  73. int i;
  74. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  75. u32 val = tegra_spdif_read(spdif, regs[i].offset);
  76. seq_printf(s, "%s = %08x\n", regs[i].name, val);
  77. }
  78. return 0;
  79. }
  80. static int tegra_spdif_debug_open(struct inode *inode, struct file *file)
  81. {
  82. return single_open(file, tegra_spdif_show, inode->i_private);
  83. }
  84. static const struct file_operations tegra_spdif_debug_fops = {
  85. .open = tegra_spdif_debug_open,
  86. .read = seq_read,
  87. .llseek = seq_lseek,
  88. .release = single_release,
  89. };
  90. static void tegra_spdif_debug_add(struct tegra_spdif *spdif)
  91. {
  92. spdif->debug = debugfs_create_file(DRV_NAME, S_IRUGO,
  93. snd_soc_debugfs_root, spdif,
  94. &tegra_spdif_debug_fops);
  95. }
  96. static void tegra_spdif_debug_remove(struct tegra_spdif *spdif)
  97. {
  98. if (spdif->debug)
  99. debugfs_remove(spdif->debug);
  100. }
  101. #else
  102. static inline void tegra_spdif_debug_add(struct tegra_spdif *spdif)
  103. {
  104. }
  105. static inline void tegra_spdif_debug_remove(struct tegra_spdif *spdif)
  106. {
  107. }
  108. #endif
  109. static int tegra_spdif_hw_params(struct snd_pcm_substream *substream,
  110. struct snd_pcm_hw_params *params,
  111. struct snd_soc_dai *dai)
  112. {
  113. struct device *dev = substream->pcm->card->dev;
  114. struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
  115. int ret, spdifclock;
  116. spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_PACK;
  117. spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_BIT_MODE_MASK;
  118. switch (params_format(params)) {
  119. case SNDRV_PCM_FORMAT_S16_LE:
  120. spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_PACK;
  121. spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_BIT_MODE_16BIT;
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. switch (params_rate(params)) {
  127. case 32000:
  128. spdifclock = 4096000;
  129. break;
  130. case 44100:
  131. spdifclock = 5644800;
  132. break;
  133. case 48000:
  134. spdifclock = 6144000;
  135. break;
  136. case 88200:
  137. spdifclock = 11289600;
  138. break;
  139. case 96000:
  140. spdifclock = 12288000;
  141. break;
  142. case 176400:
  143. spdifclock = 22579200;
  144. break;
  145. case 192000:
  146. spdifclock = 24576000;
  147. break;
  148. default:
  149. return -EINVAL;
  150. }
  151. ret = clk_set_rate(spdif->clk_spdif_out, spdifclock);
  152. if (ret) {
  153. dev_err(dev, "Can't set SPDIF clock rate: %d\n", ret);
  154. return ret;
  155. }
  156. return 0;
  157. }
  158. static void tegra_spdif_start_playback(struct tegra_spdif *spdif)
  159. {
  160. spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_TX_EN;
  161. tegra_spdif_write(spdif, TEGRA_SPDIF_CTRL, spdif->reg_ctrl);
  162. }
  163. static void tegra_spdif_stop_playback(struct tegra_spdif *spdif)
  164. {
  165. spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_TX_EN;
  166. tegra_spdif_write(spdif, TEGRA_SPDIF_CTRL, spdif->reg_ctrl);
  167. }
  168. static int tegra_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
  169. struct snd_soc_dai *dai)
  170. {
  171. struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
  172. switch (cmd) {
  173. case SNDRV_PCM_TRIGGER_START:
  174. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  175. case SNDRV_PCM_TRIGGER_RESUME:
  176. if (!spdif->clk_refs)
  177. clk_enable(spdif->clk_spdif_out);
  178. spdif->clk_refs++;
  179. tegra_spdif_start_playback(spdif);
  180. break;
  181. case SNDRV_PCM_TRIGGER_STOP:
  182. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  183. case SNDRV_PCM_TRIGGER_SUSPEND:
  184. tegra_spdif_stop_playback(spdif);
  185. spdif->clk_refs--;
  186. if (!spdif->clk_refs)
  187. clk_disable(spdif->clk_spdif_out);
  188. break;
  189. default:
  190. return -EINVAL;
  191. }
  192. return 0;
  193. }
  194. static int tegra_spdif_probe(struct snd_soc_dai *dai)
  195. {
  196. struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
  197. dai->capture_dma_data = NULL;
  198. dai->playback_dma_data = &spdif->playback_dma_data;
  199. return 0;
  200. }
  201. static const struct snd_soc_dai_ops tegra_spdif_dai_ops = {
  202. .hw_params = tegra_spdif_hw_params,
  203. .trigger = tegra_spdif_trigger,
  204. };
  205. static struct snd_soc_dai_driver tegra_spdif_dai = {
  206. .name = DRV_NAME,
  207. .probe = tegra_spdif_probe,
  208. .playback = {
  209. .channels_min = 2,
  210. .channels_max = 2,
  211. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  212. SNDRV_PCM_RATE_48000,
  213. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  214. },
  215. .ops = &tegra_spdif_dai_ops,
  216. };
  217. static __devinit int tegra_spdif_platform_probe(struct platform_device *pdev)
  218. {
  219. struct tegra_spdif *spdif;
  220. struct resource *mem, *memregion, *dmareq;
  221. int ret;
  222. spdif = kzalloc(sizeof(struct tegra_spdif), GFP_KERNEL);
  223. if (!spdif) {
  224. dev_err(&pdev->dev, "Can't allocate tegra_spdif\n");
  225. ret = -ENOMEM;
  226. goto exit;
  227. }
  228. dev_set_drvdata(&pdev->dev, spdif);
  229. spdif->clk_spdif_out = clk_get(&pdev->dev, "spdif_out");
  230. if (IS_ERR(spdif->clk_spdif_out)) {
  231. pr_err("Can't retrieve spdif clock\n");
  232. ret = PTR_ERR(spdif->clk_spdif_out);
  233. goto err_free;
  234. }
  235. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  236. if (!mem) {
  237. dev_err(&pdev->dev, "No memory resource\n");
  238. ret = -ENODEV;
  239. goto err_clk_put;
  240. }
  241. dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  242. if (!dmareq) {
  243. dev_err(&pdev->dev, "No DMA resource\n");
  244. ret = -ENODEV;
  245. goto err_clk_put;
  246. }
  247. memregion = request_mem_region(mem->start, resource_size(mem),
  248. DRV_NAME);
  249. if (!memregion) {
  250. dev_err(&pdev->dev, "Memory region already claimed\n");
  251. ret = -EBUSY;
  252. goto err_clk_put;
  253. }
  254. spdif->regs = ioremap(mem->start, resource_size(mem));
  255. if (!spdif->regs) {
  256. dev_err(&pdev->dev, "ioremap failed\n");
  257. ret = -ENOMEM;
  258. goto err_release;
  259. }
  260. spdif->playback_dma_data.addr = mem->start + TEGRA_SPDIF_DATA_OUT;
  261. spdif->playback_dma_data.wrap = 4;
  262. spdif->playback_dma_data.width = 32;
  263. spdif->playback_dma_data.req_sel = dmareq->start;
  264. ret = snd_soc_register_dai(&pdev->dev, &tegra_spdif_dai);
  265. if (ret) {
  266. dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  267. ret = -ENOMEM;
  268. goto err_unmap;
  269. }
  270. tegra_spdif_debug_add(spdif);
  271. return 0;
  272. err_unmap:
  273. iounmap(spdif->regs);
  274. err_release:
  275. release_mem_region(mem->start, resource_size(mem));
  276. err_clk_put:
  277. clk_put(spdif->clk_spdif_out);
  278. err_free:
  279. kfree(spdif);
  280. exit:
  281. return ret;
  282. }
  283. static int __devexit tegra_spdif_platform_remove(struct platform_device *pdev)
  284. {
  285. struct tegra_spdif *spdif = dev_get_drvdata(&pdev->dev);
  286. struct resource *res;
  287. snd_soc_unregister_dai(&pdev->dev);
  288. tegra_spdif_debug_remove(spdif);
  289. iounmap(spdif->regs);
  290. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  291. release_mem_region(res->start, resource_size(res));
  292. clk_put(spdif->clk_spdif_out);
  293. kfree(spdif);
  294. return 0;
  295. }
  296. static struct platform_driver tegra_spdif_driver = {
  297. .driver = {
  298. .name = DRV_NAME,
  299. .owner = THIS_MODULE,
  300. },
  301. .probe = tegra_spdif_platform_probe,
  302. .remove = __devexit_p(tegra_spdif_platform_remove),
  303. };
  304. module_platform_driver(tegra_spdif_driver);
  305. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  306. MODULE_DESCRIPTION("Tegra SPDIF ASoC driver");
  307. MODULE_LICENSE("GPL");
  308. MODULE_ALIAS("platform:" DRV_NAME);