Merge branch 'nfs-for-2.6.32'
[pandora-kernel.git] / sound / soc / blackfin / bf5xx-tdm.c
1 /*
2  * File:         sound/soc/blackfin/bf5xx-tdm.c
3  * Author:       Barry Song <Barry.Song@analog.com>
4  *
5  * Created:      Thurs June 04 2009
6  * Description:  Blackfin I2S(TDM) CPU DAI driver
7  *              Even though TDM mode can be as part of I2S DAI, but there
8  *              are so much difference in configuration and data flow,
9  *              it's very ugly to integrate I2S and TDM into a module
10  *
11  * Modified:
12  *               Copyright 2009 Analog Devices Inc.
13  *
14  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, see the file COPYING, or write
28  * to the Free Software Foundation, Inc.,
29  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30  */
31
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/device.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/initval.h>
39 #include <sound/soc.h>
40
41 #include <asm/irq.h>
42 #include <asm/portmux.h>
43 #include <linux/mutex.h>
44 #include <linux/gpio.h>
45
46 #include "bf5xx-sport.h"
47 #include "bf5xx-tdm.h"
48
49 struct bf5xx_tdm_port {
50         u16 tcr1;
51         u16 rcr1;
52         u16 tcr2;
53         u16 rcr2;
54         int configured;
55 };
56
57 static struct bf5xx_tdm_port bf5xx_tdm;
58 static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
59
60 static struct sport_param sport_params[2] = {
61         {
62                 .dma_rx_chan    = CH_SPORT0_RX,
63                 .dma_tx_chan    = CH_SPORT0_TX,
64                 .err_irq        = IRQ_SPORT0_ERROR,
65                 .regs           = (struct sport_register *)SPORT0_TCR1,
66         },
67         {
68                 .dma_rx_chan    = CH_SPORT1_RX,
69                 .dma_tx_chan    = CH_SPORT1_TX,
70                 .err_irq        = IRQ_SPORT1_ERROR,
71                 .regs           = (struct sport_register *)SPORT1_TCR1,
72         }
73 };
74
75 /*
76  * Setting the TFS pin selector for SPORT 0 based on whether the selected
77  * port id F or G. If the port is F then no conflict should exist for the
78  * TFS. When Port G is selected and EMAC then there is a conflict between
79  * the PHY interrupt line and TFS.  Current settings prevent the conflict
80  * by ignoring the TFS pin when Port G is selected. This allows both
81  * ssm2602 using Port G and EMAC concurrently.
82  */
83 #ifdef CONFIG_BF527_SPORT0_PORTF
84 #define LOCAL_SPORT0_TFS (P_SPORT0_TFS)
85 #else
86 #define LOCAL_SPORT0_TFS (0)
87 #endif
88
89 static u16 sport_req[][7] = { {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
90         P_SPORT0_DRPRI, P_SPORT0_RSCLK, LOCAL_SPORT0_TFS, 0},
91            {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, P_SPORT1_DRPRI,
92                    P_SPORT1_RSCLK, P_SPORT1_TFS, 0} };
93
94 static int bf5xx_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
95         unsigned int fmt)
96 {
97         int ret = 0;
98
99         /* interface format:support TDM,slave mode */
100         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
101         case SND_SOC_DAIFMT_DSP_A:
102                 break;
103         default:
104                 printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
105                 ret = -EINVAL;
106                 break;
107         }
108
109         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
110         case SND_SOC_DAIFMT_CBM_CFM:
111                 break;
112         case SND_SOC_DAIFMT_CBS_CFS:
113         case SND_SOC_DAIFMT_CBM_CFS:
114         case SND_SOC_DAIFMT_CBS_CFM:
115                 ret = -EINVAL;
116                 break;
117         default:
118                 printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
119                 ret = -EINVAL;
120                 break;
121         }
122
123         return ret;
124 }
125
126 static int bf5xx_tdm_hw_params(struct snd_pcm_substream *substream,
127         struct snd_pcm_hw_params *params,
128         struct snd_soc_dai *dai)
129 {
130         int ret = 0;
131
132         bf5xx_tdm.tcr2 &= ~0x1f;
133         bf5xx_tdm.rcr2 &= ~0x1f;
134         switch (params_format(params)) {
135         case SNDRV_PCM_FORMAT_S32_LE:
136                 bf5xx_tdm.tcr2 |= 31;
137                 bf5xx_tdm.rcr2 |= 31;
138                 sport_handle->wdsize = 4;
139                 break;
140                 /* at present, we only support 32bit transfer */
141         default:
142                 pr_err("not supported PCM format yet\n");
143                 return -EINVAL;
144                 break;
145         }
146
147         if (!bf5xx_tdm.configured) {
148                 /*
149                  * TX and RX are not independent,they are enabled at the
150                  * same time, even if only one side is running. So, we
151                  * need to configure both of them at the time when the first
152                  * stream is opened.
153                  *
154                  * CPU DAI:slave mode.
155                  */
156                 ret = sport_config_rx(sport_handle, bf5xx_tdm.rcr1,
157                         bf5xx_tdm.rcr2, 0, 0);
158                 if (ret) {
159                         pr_err("SPORT is busy!\n");
160                         return -EBUSY;
161                 }
162
163                 ret = sport_config_tx(sport_handle, bf5xx_tdm.tcr1,
164                         bf5xx_tdm.tcr2, 0, 0);
165                 if (ret) {
166                         pr_err("SPORT is busy!\n");
167                         return -EBUSY;
168                 }
169
170                 bf5xx_tdm.configured = 1;
171         }
172
173         return 0;
174 }
175
176 static void bf5xx_tdm_shutdown(struct snd_pcm_substream *substream,
177         struct snd_soc_dai *dai)
178 {
179         /* No active stream, SPORT is allowed to be configured again. */
180         if (!dai->active)
181                 bf5xx_tdm.configured = 0;
182 }
183
184 #ifdef CONFIG_PM
185 static int bf5xx_tdm_suspend(struct snd_soc_dai *dai)
186 {
187         struct sport_device *sport =
188                 (struct sport_device *)dai->private_data;
189
190         if (!dai->active)
191                 return 0;
192         if (dai->capture.active)
193                 sport_rx_stop(sport);
194         if (dai->playback.active)
195                 sport_tx_stop(sport);
196         return 0;
197 }
198
199 static int bf5xx_tdm_resume(struct snd_soc_dai *dai)
200 {
201         int ret;
202         struct sport_device *sport =
203                 (struct sport_device *)dai->private_data;
204
205         if (!dai->active)
206                 return 0;
207
208         ret = sport_set_multichannel(sport, 8, 0xFF, 1);
209         if (ret) {
210                 pr_err("SPORT is busy!\n");
211                 ret = -EBUSY;
212         }
213
214         ret = sport_config_rx(sport, IRFS, 0x1F, 0, 0);
215         if (ret) {
216                 pr_err("SPORT is busy!\n");
217                 ret = -EBUSY;
218         }
219
220         ret = sport_config_tx(sport, ITFS, 0x1F, 0, 0);
221         if (ret) {
222                 pr_err("SPORT is busy!\n");
223                 ret = -EBUSY;
224         }
225
226         return 0;
227 }
228
229 #else
230 #define bf5xx_tdm_suspend      NULL
231 #define bf5xx_tdm_resume       NULL
232 #endif
233
234 static struct snd_soc_dai_ops bf5xx_tdm_dai_ops = {
235         .hw_params      = bf5xx_tdm_hw_params,
236         .set_fmt        = bf5xx_tdm_set_dai_fmt,
237         .shutdown       = bf5xx_tdm_shutdown,
238 };
239
240 struct snd_soc_dai bf5xx_tdm_dai = {
241         .name = "bf5xx-tdm",
242         .id = 0,
243         .suspend = bf5xx_tdm_suspend,
244         .resume = bf5xx_tdm_resume,
245         .playback = {
246                 .channels_min = 2,
247                 .channels_max = 8,
248                 .rates = SNDRV_PCM_RATE_48000,
249                 .formats = SNDRV_PCM_FMTBIT_S32_LE,},
250         .capture = {
251                 .channels_min = 2,
252                 .channels_max = 8,
253                 .rates = SNDRV_PCM_RATE_48000,
254                 .formats = SNDRV_PCM_FMTBIT_S32_LE,},
255         .ops = &bf5xx_tdm_dai_ops,
256 };
257 EXPORT_SYMBOL_GPL(bf5xx_tdm_dai);
258
259 static int __devinit bfin_tdm_probe(struct platform_device *pdev)
260 {
261         int ret = 0;
262
263         if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
264                 pr_err("Requesting Peripherals failed\n");
265                 return -EFAULT;
266         }
267
268         /* request DMA for SPORT */
269         sport_handle = sport_init(&sport_params[sport_num], 4, \
270                 8 * sizeof(u32), NULL);
271         if (!sport_handle) {
272                 peripheral_free_list(&sport_req[sport_num][0]);
273                 return -ENODEV;
274         }
275
276         /* SPORT works in TDM mode */
277         ret = sport_set_multichannel(sport_handle, 8, 0xFF, 1);
278         if (ret) {
279                 pr_err("SPORT is busy!\n");
280                 ret = -EBUSY;
281                 goto sport_config_err;
282         }
283
284         ret = sport_config_rx(sport_handle, IRFS, 0x1F, 0, 0);
285         if (ret) {
286                 pr_err("SPORT is busy!\n");
287                 ret = -EBUSY;
288                 goto sport_config_err;
289         }
290
291         ret = sport_config_tx(sport_handle, ITFS, 0x1F, 0, 0);
292         if (ret) {
293                 pr_err("SPORT is busy!\n");
294                 ret = -EBUSY;
295                 goto sport_config_err;
296         }
297
298         ret = snd_soc_register_dai(&bf5xx_tdm_dai);
299         if (ret) {
300                 pr_err("Failed to register DAI: %d\n", ret);
301                 goto sport_config_err;
302         }
303         return 0;
304
305 sport_config_err:
306         peripheral_free_list(&sport_req[sport_num][0]);
307         return ret;
308 }
309
310 static int __devexit bfin_tdm_remove(struct platform_device *pdev)
311 {
312         peripheral_free_list(&sport_req[sport_num][0]);
313         snd_soc_unregister_dai(&bf5xx_tdm_dai);
314
315         return 0;
316 }
317
318 static struct platform_driver bfin_tdm_driver = {
319         .probe  = bfin_tdm_probe,
320         .remove = __devexit_p(bfin_tdm_remove),
321         .driver = {
322                 .name   = "bfin-tdm",
323                 .owner  = THIS_MODULE,
324         },
325 };
326
327 static int __init bfin_tdm_init(void)
328 {
329         return platform_driver_register(&bfin_tdm_driver);
330 }
331 module_init(bfin_tdm_init);
332
333 static void __exit bfin_tdm_exit(void)
334 {
335         platform_driver_unregister(&bfin_tdm_driver);
336 }
337 module_exit(bfin_tdm_exit);
338
339 /* Module information */
340 MODULE_AUTHOR("Barry Song");
341 MODULE_DESCRIPTION("TDM driver for ADI Blackfin");
342 MODULE_LICENSE("GPL");
343