]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/soc/blackfin/bf5xx-i2s.c
[JFFS2] fix race condition in jffs2_lzo_compress()
[linux-2.6-omap-h63xx.git] / sound / soc / blackfin / bf5xx-i2s.c
1 /*
2  * File:         sound/soc/blackfin/bf5xx-i2s.c
3  * Author:       Cliff Cai <Cliff.Cai@analog.com>
4  *
5  * Created:      Tue June 06 2008
6  * Description:  Blackfin I2S CPU DAI driver
7  *
8  * Modified:
9  *               Copyright 2008 Analog Devices Inc.
10  *
11  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, see the file COPYING, or write
25  * to the Free Software Foundation, Inc.,
26  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27  */
28
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/device.h>
32 #include <linux/delay.h>
33 #include <sound/core.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/initval.h>
37 #include <sound/soc.h>
38
39 #include <asm/irq.h>
40 #include <asm/portmux.h>
41 #include <linux/mutex.h>
42 #include <linux/gpio.h>
43
44 #include "bf5xx-sport.h"
45 #include "bf5xx-i2s.h"
46
47 struct bf5xx_i2s_port {
48         u16 tcr1;
49         u16 rcr1;
50         u16 tcr2;
51         u16 rcr2;
52         int counter;
53 };
54
55 static struct bf5xx_i2s_port bf5xx_i2s;
56 static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
57
58 static struct sport_param sport_params[2] = {
59         {
60                 .dma_rx_chan    = CH_SPORT0_RX,
61                 .dma_tx_chan    = CH_SPORT0_TX,
62                 .err_irq        = IRQ_SPORT0_ERROR,
63                 .regs           = (struct sport_register *)SPORT0_TCR1,
64         },
65         {
66                 .dma_rx_chan    = CH_SPORT1_RX,
67                 .dma_tx_chan    = CH_SPORT1_TX,
68                 .err_irq        = IRQ_SPORT1_ERROR,
69                 .regs           = (struct sport_register *)SPORT1_TCR1,
70         }
71 };
72
73 static u16 sport_req[][7] = {
74                 { P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
75                   P_SPORT0_DRPRI, P_SPORT0_RSCLK, 0},
76                 { P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS,
77                   P_SPORT1_DRPRI, P_SPORT1_RSCLK, 0},
78 };
79
80 static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
81                 unsigned int fmt)
82 {
83         int ret = 0;
84
85         /* interface format:support I2S,slave mode */
86         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
87         case SND_SOC_DAIFMT_I2S:
88                 bf5xx_i2s.tcr1 |= TFSR | TCKFE;
89                 bf5xx_i2s.rcr1 |= RFSR | RCKFE;
90                 bf5xx_i2s.tcr2 |= TSFSE;
91                 bf5xx_i2s.rcr2 |= RSFSE;
92                 break;
93         case SND_SOC_DAIFMT_DSP_A:
94                 bf5xx_i2s.tcr1 |= TFSR;
95                 bf5xx_i2s.rcr1 |= RFSR;
96                 break;
97         case SND_SOC_DAIFMT_LEFT_J:
98                 ret = -EINVAL;
99                 break;
100         default:
101                 ret = -EINVAL;
102                 break;
103         }
104
105         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
106         case SND_SOC_DAIFMT_CBS_CFS:
107                 ret = -EINVAL;
108                 break;
109         case SND_SOC_DAIFMT_CBM_CFS:
110                 ret = -EINVAL;
111                 break;
112         case SND_SOC_DAIFMT_CBM_CFM:
113                 break;
114         case SND_SOC_DAIFMT_CBS_CFM:
115                 ret = -EINVAL;
116                 break;
117         default:
118                 ret = -EINVAL;
119                 break;
120         }
121
122         return ret;
123 }
124
125 static int bf5xx_i2s_startup(struct snd_pcm_substream *substream)
126 {
127         pr_debug("%s enter\n", __func__);
128
129         /*this counter is used for counting how many pcm streams are opened*/
130         bf5xx_i2s.counter++;
131         return 0;
132 }
133
134 static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
135                                 struct snd_pcm_hw_params *params)
136 {
137         int ret = 0;
138
139         bf5xx_i2s.tcr2 &= ~0x1f;
140         bf5xx_i2s.rcr2 &= ~0x1f;
141         switch (params_format(params)) {
142         case SNDRV_PCM_FORMAT_S16_LE:
143                 bf5xx_i2s.tcr2 |= 15;
144                 bf5xx_i2s.rcr2 |= 15;
145                 sport_handle->wdsize = 2;
146                 break;
147         case SNDRV_PCM_FORMAT_S24_LE:
148                 bf5xx_i2s.tcr2 |= 23;
149                 bf5xx_i2s.rcr2 |= 23;
150                 sport_handle->wdsize = 3;
151                 break;
152         case SNDRV_PCM_FORMAT_S32_LE:
153                 bf5xx_i2s.tcr2 |= 31;
154                 bf5xx_i2s.rcr2 |= 31;
155                 sport_handle->wdsize = 4;
156                 break;
157         }
158
159         if (bf5xx_i2s.counter == 1) {
160                 /*
161                  * TX and RX are not independent,they are enabled at the
162                  * same time, even if only one side is running. So, we
163                  * need to configure both of them at the time when the first
164                  * stream is opened.
165                  *
166                  * CPU DAI:slave mode.
167                  */
168                 ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
169                                       bf5xx_i2s.rcr2, 0, 0);
170                 if (ret) {
171                         pr_err("SPORT is busy!\n");
172                         return -EBUSY;
173                 }
174
175                 ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
176                                       bf5xx_i2s.tcr2, 0, 0);
177                 if (ret) {
178                         pr_err("SPORT is busy!\n");
179                         return -EBUSY;
180                 }
181         }
182
183         return 0;
184 }
185
186 static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream)
187 {
188         pr_debug("%s enter\n", __func__);
189         bf5xx_i2s.counter--;
190 }
191
192 static int bf5xx_i2s_probe(struct platform_device *pdev,
193                            struct snd_soc_dai *dai)
194 {
195         pr_debug("%s enter\n", __func__);
196         if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
197                 pr_err("Requesting Peripherals failed\n");
198                 return -EFAULT;
199         }
200
201         /* request DMA for SPORT */
202         sport_handle = sport_init(&sport_params[sport_num], 4, \
203                         2 * sizeof(u32), NULL);
204         if (!sport_handle) {
205                 peripheral_free_list(&sport_req[sport_num][0]);
206                 return -ENODEV;
207         }
208
209         return 0;
210 }
211
212 static void bf5xx_i2s_remove(struct platform_device *pdev,
213                            struct snd_soc_dai *dai)
214 {
215         pr_debug("%s enter\n", __func__);
216         peripheral_free_list(&sport_req[sport_num][0]);
217 }
218
219 #ifdef CONFIG_PM
220 static int bf5xx_i2s_suspend(struct platform_device *dev,
221                              struct snd_soc_dai *dai)
222 {
223         struct sport_device *sport =
224                 (struct sport_device *)dai->private_data;
225
226         pr_debug("%s : sport %d\n", __func__, dai->id);
227         if (!dai->active)
228                 return 0;
229         if (dai->capture.active)
230                 sport_rx_stop(sport);
231         if (dai->playback.active)
232                 sport_tx_stop(sport);
233         return 0;
234 }
235
236 static int bf5xx_i2s_resume(struct platform_device *pdev,
237                             struct snd_soc_dai *dai)
238 {
239         int ret;
240         struct sport_device *sport =
241                 (struct sport_device *)dai->private_data;
242
243         pr_debug("%s : sport %d\n", __func__, dai->id);
244         if (!dai->active)
245                 return 0;
246
247         ret = sport_config_rx(sport_handle, RFSR | RCKFE, RSFSE|0x1f, 0, 0);
248         if (ret) {
249                 pr_err("SPORT is busy!\n");
250                 return -EBUSY;
251         }
252
253         ret = sport_config_tx(sport_handle, TFSR | TCKFE, TSFSE|0x1f, 0, 0);
254         if (ret) {
255                 pr_err("SPORT is busy!\n");
256                 return -EBUSY;
257         }
258
259         if (dai->capture.active)
260                 sport_rx_start(sport);
261         if (dai->playback.active)
262                 sport_tx_start(sport);
263         return 0;
264 }
265
266 #else
267 #define bf5xx_i2s_suspend       NULL
268 #define bf5xx_i2s_resume        NULL
269 #endif
270
271 #define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
272                 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
273                 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
274                 SNDRV_PCM_RATE_96000)
275
276 #define BF5XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
277         SNDRV_PCM_FMTBIT_S32_LE)
278
279 struct snd_soc_dai bf5xx_i2s_dai = {
280         .name = "bf5xx-i2s",
281         .id = 0,
282         .type = SND_SOC_DAI_I2S,
283         .probe = bf5xx_i2s_probe,
284         .remove = bf5xx_i2s_remove,
285         .suspend = bf5xx_i2s_suspend,
286         .resume = bf5xx_i2s_resume,
287         .playback = {
288                 .channels_min = 1,
289                 .channels_max = 2,
290                 .rates = BF5XX_I2S_RATES,
291                 .formats = BF5XX_I2S_FORMATS,},
292         .capture = {
293                 .channels_min = 1,
294                 .channels_max = 2,
295                 .rates = BF5XX_I2S_RATES,
296                 .formats = BF5XX_I2S_FORMATS,},
297         .ops = {
298                 .startup   = bf5xx_i2s_startup,
299                 .shutdown  = bf5xx_i2s_shutdown,
300                 .hw_params = bf5xx_i2s_hw_params,},
301         .dai_ops = {
302                 .set_fmt = bf5xx_i2s_set_dai_fmt,
303         },
304 };
305 EXPORT_SYMBOL_GPL(bf5xx_i2s_dai);
306
307 /* Module information */
308 MODULE_AUTHOR("Cliff Cai");
309 MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
310 MODULE_LICENSE("GPL");
311