blob: 4e675b55b1829a55811843ce95b5abefa087533a [file] [log] [blame]
Cliff Cai912c2ac2008-09-05 18:21:39 +08001/*
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
47struct bf5xx_i2s_port {
48 u16 tcr1;
49 u16 rcr1;
50 u16 tcr2;
51 u16 rcr2;
52 int counter;
53};
54
55static struct bf5xx_i2s_port bf5xx_i2s;
56static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
57
58static 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
Cliff Caic3e52032008-10-27 17:09:25 +080073/*
74 * Setting the TFS pin selector for SPORT 0 based on whether the selected
75 * port id F or G. If the port is F then no conflict should exist for the
76 * TFS. When Port G is selected and EMAC then there is a conflict between
77 * the PHY interrupt line and TFS. Current settings prevent the conflict
78 * by ignoring the TFS pin when Port G is selected. This allows both
79 * ssm2602 using Port G and EMAC concurrently.
80 */
81#ifdef CONFIG_BF527_SPORT0_PORTF
82#define LOCAL_SPORT0_TFS (P_SPORT0_TFS)
83#else
84#define LOCAL_SPORT0_TFS (0)
85#endif
86
87static u16 sport_req[][7] = { {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
88 P_SPORT0_DRPRI, P_SPORT0_RSCLK, LOCAL_SPORT0_TFS, 0},
89 {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, P_SPORT1_DRPRI,
90 P_SPORT1_RSCLK, P_SPORT1_TFS, 0} };
Cliff Cai33392682008-09-27 22:30:15 +080091
Cliff Cai912c2ac2008-09-05 18:21:39 +080092static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
93 unsigned int fmt)
94{
95 int ret = 0;
96
97 /* interface format:support I2S,slave mode */
98 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
99 case SND_SOC_DAIFMT_I2S:
Cliff Cai33392682008-09-27 22:30:15 +0800100 bf5xx_i2s.tcr1 |= TFSR | TCKFE;
101 bf5xx_i2s.rcr1 |= RFSR | RCKFE;
102 bf5xx_i2s.tcr2 |= TSFSE;
103 bf5xx_i2s.rcr2 |= RSFSE;
104 break;
105 case SND_SOC_DAIFMT_DSP_A:
106 bf5xx_i2s.tcr1 |= TFSR;
107 bf5xx_i2s.rcr1 |= RFSR;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800108 break;
109 case SND_SOC_DAIFMT_LEFT_J:
110 ret = -EINVAL;
111 break;
112 default:
Cliff Caic3e52032008-10-27 17:09:25 +0800113 printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800114 ret = -EINVAL;
115 break;
116 }
117
118 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Cliff Cai912c2ac2008-09-05 18:21:39 +0800119 case SND_SOC_DAIFMT_CBM_CFM:
120 break;
Cliff Caic3e52032008-10-27 17:09:25 +0800121 case SND_SOC_DAIFMT_CBS_CFS:
122 case SND_SOC_DAIFMT_CBM_CFS:
Cliff Cai912c2ac2008-09-05 18:21:39 +0800123 case SND_SOC_DAIFMT_CBS_CFM:
124 ret = -EINVAL;
125 break;
126 default:
Cliff Caic3e52032008-10-27 17:09:25 +0800127 printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800128 ret = -EINVAL;
129 break;
130 }
131
132 return ret;
133}
134
Mark Browndee89c42008-11-18 22:11:38 +0000135static int bf5xx_i2s_startup(struct snd_pcm_substream *substream,
136 struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800137{
138 pr_debug("%s enter\n", __func__);
139
140 /*this counter is used for counting how many pcm streams are opened*/
141 bf5xx_i2s.counter++;
142 return 0;
143}
144
145static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000146 struct snd_pcm_hw_params *params,
147 struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800148{
149 int ret = 0;
150
151 bf5xx_i2s.tcr2 &= ~0x1f;
152 bf5xx_i2s.rcr2 &= ~0x1f;
153 switch (params_format(params)) {
154 case SNDRV_PCM_FORMAT_S16_LE:
155 bf5xx_i2s.tcr2 |= 15;
156 bf5xx_i2s.rcr2 |= 15;
Cliff Cai33392682008-09-27 22:30:15 +0800157 sport_handle->wdsize = 2;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800158 break;
159 case SNDRV_PCM_FORMAT_S24_LE:
160 bf5xx_i2s.tcr2 |= 23;
161 bf5xx_i2s.rcr2 |= 23;
Cliff Cai33392682008-09-27 22:30:15 +0800162 sport_handle->wdsize = 3;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800163 break;
164 case SNDRV_PCM_FORMAT_S32_LE:
165 bf5xx_i2s.tcr2 |= 31;
166 bf5xx_i2s.rcr2 |= 31;
Cliff Cai33392682008-09-27 22:30:15 +0800167 sport_handle->wdsize = 4;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800168 break;
169 }
170
171 if (bf5xx_i2s.counter == 1) {
172 /*
173 * TX and RX are not independent,they are enabled at the
174 * same time, even if only one side is running. So, we
175 * need to configure both of them at the time when the first
176 * stream is opened.
177 *
Cliff Cai33392682008-09-27 22:30:15 +0800178 * CPU DAI:slave mode.
Cliff Cai912c2ac2008-09-05 18:21:39 +0800179 */
Cliff Cai33392682008-09-27 22:30:15 +0800180 ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
181 bf5xx_i2s.rcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800182 if (ret) {
183 pr_err("SPORT is busy!\n");
184 return -EBUSY;
185 }
186
Cliff Cai33392682008-09-27 22:30:15 +0800187 ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
188 bf5xx_i2s.tcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800189 if (ret) {
190 pr_err("SPORT is busy!\n");
191 return -EBUSY;
192 }
193 }
194
195 return 0;
196}
197
Mark Browndee89c42008-11-18 22:11:38 +0000198static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
199 struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800200{
201 pr_debug("%s enter\n", __func__);
202 bf5xx_i2s.counter--;
203}
204
205static int bf5xx_i2s_probe(struct platform_device *pdev,
206 struct snd_soc_dai *dai)
207{
Cliff Cai912c2ac2008-09-05 18:21:39 +0800208 pr_debug("%s enter\n", __func__);
209 if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
210 pr_err("Requesting Peripherals failed\n");
211 return -EFAULT;
212 }
213
214 /* request DMA for SPORT */
215 sport_handle = sport_init(&sport_params[sport_num], 4, \
216 2 * sizeof(u32), NULL);
217 if (!sport_handle) {
218 peripheral_free_list(&sport_req[sport_num][0]);
219 return -ENODEV;
220 }
221
222 return 0;
223}
224
Cliff Cai33392682008-09-27 22:30:15 +0800225static void bf5xx_i2s_remove(struct platform_device *pdev,
226 struct snd_soc_dai *dai)
227{
228 pr_debug("%s enter\n", __func__);
229 peripheral_free_list(&sport_req[sport_num][0]);
230}
231
Cliff Cai912c2ac2008-09-05 18:21:39 +0800232#ifdef CONFIG_PM
233static int bf5xx_i2s_suspend(struct platform_device *dev,
234 struct snd_soc_dai *dai)
235{
236 struct sport_device *sport =
237 (struct sport_device *)dai->private_data;
238
239 pr_debug("%s : sport %d\n", __func__, dai->id);
240 if (!dai->active)
241 return 0;
242 if (dai->capture.active)
243 sport_rx_stop(sport);
244 if (dai->playback.active)
245 sport_tx_stop(sport);
246 return 0;
247}
248
249static int bf5xx_i2s_resume(struct platform_device *pdev,
250 struct snd_soc_dai *dai)
251{
252 int ret;
253 struct sport_device *sport =
254 (struct sport_device *)dai->private_data;
255
256 pr_debug("%s : sport %d\n", __func__, dai->id);
257 if (!dai->active)
258 return 0;
259
260 ret = sport_config_rx(sport_handle, RFSR | RCKFE, RSFSE|0x1f, 0, 0);
261 if (ret) {
262 pr_err("SPORT is busy!\n");
263 return -EBUSY;
264 }
265
266 ret = sport_config_tx(sport_handle, TFSR | TCKFE, TSFSE|0x1f, 0, 0);
267 if (ret) {
268 pr_err("SPORT is busy!\n");
269 return -EBUSY;
270 }
271
272 if (dai->capture.active)
273 sport_rx_start(sport);
274 if (dai->playback.active)
275 sport_tx_start(sport);
276 return 0;
277}
278
279#else
280#define bf5xx_i2s_suspend NULL
281#define bf5xx_i2s_resume NULL
282#endif
283
284#define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
285 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
286 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
287 SNDRV_PCM_RATE_96000)
288
289#define BF5XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
290 SNDRV_PCM_FMTBIT_S32_LE)
291
292struct snd_soc_dai bf5xx_i2s_dai = {
293 .name = "bf5xx-i2s",
294 .id = 0,
295 .type = SND_SOC_DAI_I2S,
296 .probe = bf5xx_i2s_probe,
Cliff Cai33392682008-09-27 22:30:15 +0800297 .remove = bf5xx_i2s_remove,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800298 .suspend = bf5xx_i2s_suspend,
299 .resume = bf5xx_i2s_resume,
300 .playback = {
Cliff Cai33392682008-09-27 22:30:15 +0800301 .channels_min = 1,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800302 .channels_max = 2,
303 .rates = BF5XX_I2S_RATES,
304 .formats = BF5XX_I2S_FORMATS,},
305 .capture = {
Cliff Cai33392682008-09-27 22:30:15 +0800306 .channels_min = 1,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800307 .channels_max = 2,
308 .rates = BF5XX_I2S_RATES,
309 .formats = BF5XX_I2S_FORMATS,},
310 .ops = {
311 .startup = bf5xx_i2s_startup,
312 .shutdown = bf5xx_i2s_shutdown,
Mark Browndee89c42008-11-18 22:11:38 +0000313 .hw_params = bf5xx_i2s_hw_params,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800314 .set_fmt = bf5xx_i2s_set_dai_fmt,
315 },
316};
317EXPORT_SYMBOL_GPL(bf5xx_i2s_dai);
318
319/* Module information */
320MODULE_AUTHOR("Cliff Cai");
321MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
322MODULE_LICENSE("GPL");
323