ASoC: tegra: ensure clocks are enabled when touching registers
[linux-flexiantxendom0-3.2.10.git] / sound / soc / tegra / tegra_spdif.c
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
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>
36
37 #include "tegra_spdif.h"
38
39 #define DRV_NAME "tegra-spdif"
40
41 static inline void tegra_spdif_write(struct tegra_spdif *spdif, u32 reg,
42                                         u32 val)
43 {
44         __raw_writel(val, spdif->regs + reg);
45 }
46
47 static inline u32 tegra_spdif_read(struct tegra_spdif *spdif, u32 reg)
48 {
49         return __raw_readl(spdif->regs + reg);
50 }
51
52 #ifdef CONFIG_DEBUG_FS
53 static int tegra_spdif_show(struct seq_file *s, void *unused)
54 {
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),
76         };
77 #undef REG
78
79         struct tegra_spdif *spdif = s->private;
80         int i;
81
82         clk_enable(spdif->clk_spdif_out);
83
84         for (i = 0; i < ARRAY_SIZE(regs); i++) {
85                 u32 val = tegra_spdif_read(spdif, regs[i].offset);
86                 seq_printf(s, "%s = %08x\n", regs[i].name, val);
87         }
88
89         clk_disable(spdif->clk_spdif_out);
90
91         return 0;
92 }
93
94 static int tegra_spdif_debug_open(struct inode *inode, struct file *file)
95 {
96         return single_open(file, tegra_spdif_show, inode->i_private);
97 }
98
99 static const struct file_operations tegra_spdif_debug_fops = {
100         .open    = tegra_spdif_debug_open,
101         .read    = seq_read,
102         .llseek  = seq_lseek,
103         .release = single_release,
104 };
105
106 static void tegra_spdif_debug_add(struct tegra_spdif *spdif)
107 {
108         spdif->debug = debugfs_create_file(DRV_NAME, S_IRUGO,
109                                                 snd_soc_debugfs_root, spdif,
110                                                 &tegra_spdif_debug_fops);
111 }
112
113 static void tegra_spdif_debug_remove(struct tegra_spdif *spdif)
114 {
115         if (spdif->debug)
116                 debugfs_remove(spdif->debug);
117 }
118 #else
119 static inline void tegra_spdif_debug_add(struct tegra_spdif *spdif)
120 {
121 }
122
123 static inline void tegra_spdif_debug_remove(struct tegra_spdif *spdif)
124 {
125 }
126 #endif
127
128 static int tegra_spdif_hw_params(struct snd_pcm_substream *substream,
129                                 struct snd_pcm_hw_params *params,
130                                 struct snd_soc_dai *dai)
131 {
132         struct device *dev = substream->pcm->card->dev;
133         struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
134         int ret, spdifclock;
135
136         spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_PACK;
137         spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_BIT_MODE_MASK;
138         switch (params_format(params)) {
139         case SNDRV_PCM_FORMAT_S16_LE:
140                 spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_PACK;
141                 spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_BIT_MODE_16BIT;
142                 break;
143         default:
144                 return -EINVAL;
145         }
146
147         switch (params_rate(params)) {
148         case 32000:
149                 spdifclock = 4096000;
150                 break;
151         case 44100:
152                 spdifclock = 5644800;
153                 break;
154         case 48000:
155                 spdifclock = 6144000;
156                 break;
157         case 88200:
158                 spdifclock = 11289600;
159                 break;
160         case 96000:
161                 spdifclock = 12288000;
162                 break;
163         case 176400:
164                 spdifclock = 22579200;
165                 break;
166         case 192000:
167                 spdifclock = 24576000;
168                 break;
169         default:
170                 return -EINVAL;
171         }
172
173         ret = clk_set_rate(spdif->clk_spdif_out, spdifclock);
174         if (ret) {
175                 dev_err(dev, "Can't set SPDIF clock rate: %d\n", ret);
176                 return ret;
177         }
178
179         return 0;
180 }
181
182 static void tegra_spdif_start_playback(struct tegra_spdif *spdif)
183 {
184         spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_TX_EN;
185         tegra_spdif_write(spdif, TEGRA_SPDIF_CTRL, spdif->reg_ctrl);
186 }
187
188 static void tegra_spdif_stop_playback(struct tegra_spdif *spdif)
189 {
190         spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_TX_EN;
191         tegra_spdif_write(spdif, TEGRA_SPDIF_CTRL, spdif->reg_ctrl);
192 }
193
194 static int tegra_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
195                                 struct snd_soc_dai *dai)
196 {
197         struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
198
199         switch (cmd) {
200         case SNDRV_PCM_TRIGGER_START:
201         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
202         case SNDRV_PCM_TRIGGER_RESUME:
203                 if (!spdif->clk_refs)
204                         clk_enable(spdif->clk_spdif_out);
205                 spdif->clk_refs++;
206                 tegra_spdif_start_playback(spdif);
207                 break;
208         case SNDRV_PCM_TRIGGER_STOP:
209         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
210         case SNDRV_PCM_TRIGGER_SUSPEND:
211                 tegra_spdif_stop_playback(spdif);
212                 spdif->clk_refs--;
213                 if (!spdif->clk_refs)
214                         clk_disable(spdif->clk_spdif_out);
215                 break;
216         default:
217                 return -EINVAL;
218         }
219
220         return 0;
221 }
222
223 static int tegra_spdif_probe(struct snd_soc_dai *dai)
224 {
225         struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
226
227         dai->capture_dma_data = NULL;
228         dai->playback_dma_data = &spdif->playback_dma_data;
229
230         return 0;
231 }
232
233 static const struct snd_soc_dai_ops tegra_spdif_dai_ops = {
234         .hw_params      = tegra_spdif_hw_params,
235         .trigger        = tegra_spdif_trigger,
236 };
237
238 static struct snd_soc_dai_driver tegra_spdif_dai = {
239         .name = DRV_NAME,
240         .probe = tegra_spdif_probe,
241         .playback = {
242                 .channels_min = 2,
243                 .channels_max = 2,
244                 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
245                                 SNDRV_PCM_RATE_48000,
246                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
247         },
248         .ops = &tegra_spdif_dai_ops,
249 };
250
251 static __devinit int tegra_spdif_platform_probe(struct platform_device *pdev)
252 {
253         struct tegra_spdif *spdif;
254         struct resource *mem, *memregion, *dmareq;
255         int ret;
256
257         spdif = kzalloc(sizeof(struct tegra_spdif), GFP_KERNEL);
258         if (!spdif) {
259                 dev_err(&pdev->dev, "Can't allocate tegra_spdif\n");
260                 ret = -ENOMEM;
261                 goto exit;
262         }
263         dev_set_drvdata(&pdev->dev, spdif);
264
265         spdif->clk_spdif_out = clk_get(&pdev->dev, "spdif_out");
266         if (IS_ERR(spdif->clk_spdif_out)) {
267                 pr_err("Can't retrieve spdif clock\n");
268                 ret = PTR_ERR(spdif->clk_spdif_out);
269                 goto err_free;
270         }
271
272         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
273         if (!mem) {
274                 dev_err(&pdev->dev, "No memory resource\n");
275                 ret = -ENODEV;
276                 goto err_clk_put;
277         }
278
279         dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
280         if (!dmareq) {
281                 dev_err(&pdev->dev, "No DMA resource\n");
282                 ret = -ENODEV;
283                 goto err_clk_put;
284         }
285
286         memregion = request_mem_region(mem->start, resource_size(mem),
287                                         DRV_NAME);
288         if (!memregion) {
289                 dev_err(&pdev->dev, "Memory region already claimed\n");
290                 ret = -EBUSY;
291                 goto err_clk_put;
292         }
293
294         spdif->regs = ioremap(mem->start, resource_size(mem));
295         if (!spdif->regs) {
296                 dev_err(&pdev->dev, "ioremap failed\n");
297                 ret = -ENOMEM;
298                 goto err_release;
299         }
300
301         spdif->playback_dma_data.addr = mem->start + TEGRA_SPDIF_DATA_OUT;
302         spdif->playback_dma_data.wrap = 4;
303         spdif->playback_dma_data.width = 32;
304         spdif->playback_dma_data.req_sel = dmareq->start;
305
306         ret = snd_soc_register_dai(&pdev->dev, &tegra_spdif_dai);
307         if (ret) {
308                 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
309                 ret = -ENOMEM;
310                 goto err_unmap;
311         }
312
313         tegra_spdif_debug_add(spdif);
314
315         return 0;
316
317 err_unmap:
318         iounmap(spdif->regs);
319 err_release:
320         release_mem_region(mem->start, resource_size(mem));
321 err_clk_put:
322         clk_put(spdif->clk_spdif_out);
323 err_free:
324         kfree(spdif);
325 exit:
326         return ret;
327 }
328
329 static int __devexit tegra_spdif_platform_remove(struct platform_device *pdev)
330 {
331         struct tegra_spdif *spdif = dev_get_drvdata(&pdev->dev);
332         struct resource *res;
333
334         snd_soc_unregister_dai(&pdev->dev);
335
336         tegra_spdif_debug_remove(spdif);
337
338         iounmap(spdif->regs);
339
340         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
341         release_mem_region(res->start, resource_size(res));
342
343         clk_put(spdif->clk_spdif_out);
344
345         kfree(spdif);
346
347         return 0;
348 }
349
350 static struct platform_driver tegra_spdif_driver = {
351         .driver = {
352                 .name = DRV_NAME,
353                 .owner = THIS_MODULE,
354         },
355         .probe = tegra_spdif_platform_probe,
356         .remove = __devexit_p(tegra_spdif_platform_remove),
357 };
358
359 module_platform_driver(tegra_spdif_driver);
360
361 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
362 MODULE_DESCRIPTION("Tegra SPDIF ASoC driver");
363 MODULE_LICENSE("GPL");
364 MODULE_ALIAS("platform:" DRV_NAME);