x86: Fix S4 regression
[linux-btrfs-devel.git] / sound / soc / tegra / tegra_spdif.c
blobabe606b0a29ea6e2ef5ab44c4d1a2c0b4dd42e04
1 /*
2 * tegra_spdif.c - Tegra SPDIF driver
4 * Author: Stephen Warren <swarren@nvidia.com>
5 * Copyright (C) 2011 - NVIDIA, Inc.
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.
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.
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
23 #include <linux/clk.h>
24 #include <linux/module.h>
25 #include <linux/debugfs.h>
26 #include <linux/device.h>
27 #include <linux/platform_device.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
30 #include <linux/io.h>
31 #include <mach/iomap.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
37 #include "tegra_spdif.h"
39 #define DRV_NAME "tegra-spdif"
41 static inline void tegra_spdif_write(struct tegra_spdif *spdif, u32 reg,
42 u32 val)
44 __raw_writel(val, spdif->regs + reg);
47 static inline u32 tegra_spdif_read(struct tegra_spdif *spdif, u32 reg)
49 return __raw_readl(spdif->regs + reg);
52 #ifdef CONFIG_DEBUG_FS
53 static int tegra_spdif_show(struct seq_file *s, void *unused)
55 #define REG(r) { r, #r }
56 static const struct {
57 int offset;
58 const char *name;
59 } regs[] = {
60 REG(TEGRA_SPDIF_CTRL),
61 REG(TEGRA_SPDIF_STATUS),
62 REG(TEGRA_SPDIF_STROBE_CTRL),
63 REG(TEGRA_SPDIF_DATA_FIFO_CSR),
64 REG(TEGRA_SPDIF_CH_STA_RX_A),
65 REG(TEGRA_SPDIF_CH_STA_RX_B),
66 REG(TEGRA_SPDIF_CH_STA_RX_C),
67 REG(TEGRA_SPDIF_CH_STA_RX_D),
68 REG(TEGRA_SPDIF_CH_STA_RX_E),
69 REG(TEGRA_SPDIF_CH_STA_RX_F),
70 REG(TEGRA_SPDIF_CH_STA_TX_A),
71 REG(TEGRA_SPDIF_CH_STA_TX_B),
72 REG(TEGRA_SPDIF_CH_STA_TX_C),
73 REG(TEGRA_SPDIF_CH_STA_TX_D),
74 REG(TEGRA_SPDIF_CH_STA_TX_E),
75 REG(TEGRA_SPDIF_CH_STA_TX_F),
77 #undef REG
79 struct tegra_spdif *spdif = s->private;
80 int i;
82 for (i = 0; i < ARRAY_SIZE(regs); i++) {
83 u32 val = tegra_spdif_read(spdif, regs[i].offset);
84 seq_printf(s, "%s = %08x\n", regs[i].name, val);
87 return 0;
90 static int tegra_spdif_debug_open(struct inode *inode, struct file *file)
92 return single_open(file, tegra_spdif_show, inode->i_private);
95 static const struct file_operations tegra_spdif_debug_fops = {
96 .open = tegra_spdif_debug_open,
97 .read = seq_read,
98 .llseek = seq_lseek,
99 .release = single_release,
102 static void tegra_spdif_debug_add(struct tegra_spdif *spdif)
104 spdif->debug = debugfs_create_file(DRV_NAME, S_IRUGO,
105 snd_soc_debugfs_root, spdif,
106 &tegra_spdif_debug_fops);
109 static void tegra_spdif_debug_remove(struct tegra_spdif *spdif)
111 if (spdif->debug)
112 debugfs_remove(spdif->debug);
114 #else
115 static inline void tegra_spdif_debug_add(struct tegra_spdif *spdif)
119 static inline void tegra_spdif_debug_remove(struct tegra_spdif *spdif)
122 #endif
124 static int tegra_spdif_hw_params(struct snd_pcm_substream *substream,
125 struct snd_pcm_hw_params *params,
126 struct snd_soc_dai *dai)
128 struct device *dev = substream->pcm->card->dev;
129 struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
130 int ret, srate, spdifclock;
132 spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_PACK;
133 spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_BIT_MODE_MASK;
134 switch (params_format(params)) {
135 case SNDRV_PCM_FORMAT_S16_LE:
136 spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_PACK;
137 spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_BIT_MODE_16BIT;
138 break;
139 default:
140 return -EINVAL;
143 srate = params_rate(params);
144 switch (params_rate(params)) {
145 case 32000:
146 spdifclock = 4096000;
147 break;
148 case 44100:
149 spdifclock = 5644800;
150 break;
151 case 48000:
152 spdifclock = 6144000;
153 break;
154 case 88200:
155 spdifclock = 11289600;
156 break;
157 case 96000:
158 spdifclock = 12288000;
159 break;
160 case 176400:
161 spdifclock = 22579200;
162 break;
163 case 192000:
164 spdifclock = 24576000;
165 break;
166 default:
167 return -EINVAL;
170 ret = clk_set_rate(spdif->clk_spdif_out, spdifclock);
171 if (ret) {
172 dev_err(dev, "Can't set SPDIF clock rate: %d\n", ret);
173 return ret;
176 return 0;
179 static void tegra_spdif_start_playback(struct tegra_spdif *spdif)
181 spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_TX_EN;
182 tegra_spdif_write(spdif, TEGRA_SPDIF_CTRL, spdif->reg_ctrl);
185 static void tegra_spdif_stop_playback(struct tegra_spdif *spdif)
187 spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_TX_EN;
188 tegra_spdif_write(spdif, TEGRA_SPDIF_CTRL, spdif->reg_ctrl);
191 static int tegra_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
192 struct snd_soc_dai *dai)
194 struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
196 switch (cmd) {
197 case SNDRV_PCM_TRIGGER_START:
198 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
199 case SNDRV_PCM_TRIGGER_RESUME:
200 if (!spdif->clk_refs)
201 clk_enable(spdif->clk_spdif_out);
202 spdif->clk_refs++;
203 tegra_spdif_start_playback(spdif);
204 break;
205 case SNDRV_PCM_TRIGGER_STOP:
206 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
207 case SNDRV_PCM_TRIGGER_SUSPEND:
208 tegra_spdif_stop_playback(spdif);
209 spdif->clk_refs--;
210 if (!spdif->clk_refs)
211 clk_disable(spdif->clk_spdif_out);
212 break;
213 default:
214 return -EINVAL;
217 return 0;
220 static int tegra_spdif_probe(struct snd_soc_dai *dai)
222 struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
224 dai->capture_dma_data = NULL;
225 dai->playback_dma_data = &spdif->playback_dma_data;
227 return 0;
230 static struct snd_soc_dai_ops tegra_spdif_dai_ops = {
231 .hw_params = tegra_spdif_hw_params,
232 .trigger = tegra_spdif_trigger,
235 struct snd_soc_dai_driver tegra_spdif_dai = {
236 .name = DRV_NAME,
237 .probe = tegra_spdif_probe,
238 .playback = {
239 .channels_min = 2,
240 .channels_max = 2,
241 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
242 SNDRV_PCM_RATE_48000,
243 .formats = SNDRV_PCM_FMTBIT_S16_LE,
245 .ops = &tegra_spdif_dai_ops,
248 static __devinit int tegra_spdif_platform_probe(struct platform_device *pdev)
250 struct tegra_spdif *spdif;
251 struct resource *mem, *memregion, *dmareq;
252 int ret;
254 spdif = kzalloc(sizeof(struct tegra_spdif), GFP_KERNEL);
255 if (!spdif) {
256 dev_err(&pdev->dev, "Can't allocate tegra_spdif\n");
257 ret = -ENOMEM;
258 goto exit;
260 dev_set_drvdata(&pdev->dev, spdif);
262 spdif->clk_spdif_out = clk_get(&pdev->dev, "spdif_out");
263 if (IS_ERR(spdif->clk_spdif_out)) {
264 pr_err("Can't retrieve spdif clock\n");
265 ret = PTR_ERR(spdif->clk_spdif_out);
266 goto err_free;
269 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
270 if (!mem) {
271 dev_err(&pdev->dev, "No memory resource\n");
272 ret = -ENODEV;
273 goto err_clk_put;
276 dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
277 if (!dmareq) {
278 dev_err(&pdev->dev, "No DMA resource\n");
279 ret = -ENODEV;
280 goto err_clk_put;
283 memregion = request_mem_region(mem->start, resource_size(mem),
284 DRV_NAME);
285 if (!memregion) {
286 dev_err(&pdev->dev, "Memory region already claimed\n");
287 ret = -EBUSY;
288 goto err_clk_put;
291 spdif->regs = ioremap(mem->start, resource_size(mem));
292 if (!spdif->regs) {
293 dev_err(&pdev->dev, "ioremap failed\n");
294 ret = -ENOMEM;
295 goto err_release;
298 spdif->playback_dma_data.addr = mem->start + TEGRA_SPDIF_DATA_OUT;
299 spdif->playback_dma_data.wrap = 4;
300 spdif->playback_dma_data.width = 32;
301 spdif->playback_dma_data.req_sel = dmareq->start;
303 ret = snd_soc_register_dai(&pdev->dev, &tegra_spdif_dai);
304 if (ret) {
305 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
306 ret = -ENOMEM;
307 goto err_unmap;
310 tegra_spdif_debug_add(spdif);
312 return 0;
314 err_unmap:
315 iounmap(spdif->regs);
316 err_release:
317 release_mem_region(mem->start, resource_size(mem));
318 err_clk_put:
319 clk_put(spdif->clk_spdif_out);
320 err_free:
321 kfree(spdif);
322 exit:
323 return ret;
326 static int __devexit tegra_spdif_platform_remove(struct platform_device *pdev)
328 struct tegra_spdif *spdif = dev_get_drvdata(&pdev->dev);
329 struct resource *res;
331 snd_soc_unregister_dai(&pdev->dev);
333 tegra_spdif_debug_remove(spdif);
335 iounmap(spdif->regs);
337 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
338 release_mem_region(res->start, resource_size(res));
340 clk_put(spdif->clk_spdif_out);
342 kfree(spdif);
344 return 0;
347 static struct platform_driver tegra_spdif_driver = {
348 .driver = {
349 .name = DRV_NAME,
350 .owner = THIS_MODULE,
352 .probe = tegra_spdif_platform_probe,
353 .remove = __devexit_p(tegra_spdif_platform_remove),
356 static int __init snd_tegra_spdif_init(void)
358 return platform_driver_register(&tegra_spdif_driver);
360 module_init(snd_tegra_spdif_init);
362 static void __exit snd_tegra_spdif_exit(void)
364 platform_driver_unregister(&tegra_spdif_driver);
366 module_exit(snd_tegra_spdif_exit);
368 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
369 MODULE_DESCRIPTION("Tegra SPDIF ASoC driver");
370 MODULE_LICENSE("GPL");
371 MODULE_ALIAS("platform:" DRV_NAME);