1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 int configured;
54};
55
56static struct bf5xx_i2s_port bf5xx_i2s;
57static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
58
59static struct sport_param sport_params[2] = {
60 {
61 .dma_rx_chan = CH_SPORT0_RX,
62 .dma_tx_chan = CH_SPORT0_TX,
63 .err_irq = IRQ_SPORT0_ERROR,
64 .regs = (struct sport_register *)SPORT0_TCR1,
65 },
66 {
67 .dma_rx_chan = CH_SPORT1_RX,
68 .dma_tx_chan = CH_SPORT1_TX,
69 .err_irq = IRQ_SPORT1_ERROR,
70 .regs = (struct sport_register *)SPORT1_TCR1,
71 }
72};
73
74
75
76
77
78
79
80
81
82#ifdef CONFIG_BF527_SPORT0_PORTG
83#define LOCAL_SPORT0_TFS (0)
84#else
85#define LOCAL_SPORT0_TFS (P_SPORT0_TFS)
86#endif
87
88static u16 sport_req[][7] = { {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
89 P_SPORT0_DRPRI, P_SPORT0_RSCLK, LOCAL_SPORT0_TFS, 0},
90 {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, P_SPORT1_DRPRI,
91 P_SPORT1_RSCLK, P_SPORT1_TFS, 0} };
92
93static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
94 unsigned int fmt)
95{
96 int ret = 0;
97
98
99 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
100 case SND_SOC_DAIFMT_I2S:
101 bf5xx_i2s.tcr1 |= TFSR | TCKFE;
102 bf5xx_i2s.rcr1 |= RFSR | RCKFE;
103 bf5xx_i2s.tcr2 |= TSFSE;
104 bf5xx_i2s.rcr2 |= RSFSE;
105 break;
106 case SND_SOC_DAIFMT_DSP_A:
107 bf5xx_i2s.tcr1 |= TFSR;
108 bf5xx_i2s.rcr1 |= RFSR;
109 break;
110 case SND_SOC_DAIFMT_LEFT_J:
111 ret = -EINVAL;
112 break;
113 default:
114 printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
115 ret = -EINVAL;
116 break;
117 }
118
119 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
120 case SND_SOC_DAIFMT_CBM_CFM:
121 break;
122 case SND_SOC_DAIFMT_CBS_CFS:
123 case SND_SOC_DAIFMT_CBM_CFS:
124 case SND_SOC_DAIFMT_CBS_CFM:
125 ret = -EINVAL;
126 break;
127 default:
128 printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
129 ret = -EINVAL;
130 break;
131 }
132
133 return ret;
134}
135
136static int bf5xx_i2s_startup(struct snd_pcm_substream *substream,
137 struct snd_soc_dai *dai)
138{
139 pr_debug("%s enter\n", __func__);
140
141
142 bf5xx_i2s.counter++;
143 return 0;
144}
145
146static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
147 struct snd_pcm_hw_params *params,
148 struct snd_soc_dai *dai)
149{
150 int ret = 0;
151
152 bf5xx_i2s.tcr2 &= ~0x1f;
153 bf5xx_i2s.rcr2 &= ~0x1f;
154 switch (params_format(params)) {
155 case SNDRV_PCM_FORMAT_S16_LE:
156 bf5xx_i2s.tcr2 |= 15;
157 bf5xx_i2s.rcr2 |= 15;
158 sport_handle->wdsize = 2;
159 break;
160 case SNDRV_PCM_FORMAT_S24_LE:
161 bf5xx_i2s.tcr2 |= 23;
162 bf5xx_i2s.rcr2 |= 23;
163 sport_handle->wdsize = 3;
164 break;
165 case SNDRV_PCM_FORMAT_S32_LE:
166 bf5xx_i2s.tcr2 |= 31;
167 bf5xx_i2s.rcr2 |= 31;
168 sport_handle->wdsize = 4;
169 break;
170 }
171
172 if (!bf5xx_i2s.configured) {
173
174
175
176
177
178
179
180
181 bf5xx_i2s.configured = 1;
182 ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
183 bf5xx_i2s.rcr2, 0, 0);
184 if (ret) {
185 pr_err("SPORT is busy!\n");
186 return -EBUSY;
187 }
188
189 ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
190 bf5xx_i2s.tcr2, 0, 0);
191 if (ret) {
192 pr_err("SPORT is busy!\n");
193 return -EBUSY;
194 }
195 }
196
197 return 0;
198}
199
200static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
201 struct snd_soc_dai *dai)
202{
203 pr_debug("%s enter\n", __func__);
204 bf5xx_i2s.counter--;
205
206 if (!bf5xx_i2s.counter)
207 bf5xx_i2s.configured = 0;
208}
209
210static int bf5xx_i2s_probe(struct platform_device *pdev,
211 struct snd_soc_dai *dai)
212{
213 pr_debug("%s enter\n", __func__);
214 if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
215 pr_err("Requesting Peripherals failed\n");
216 return -EFAULT;
217 }
218
219
220 sport_handle = sport_init(&sport_params[sport_num], 4, \
221 2 * sizeof(u32), NULL);
222 if (!sport_handle) {
223 peripheral_free_list(&sport_req[sport_num][0]);
224 return -ENODEV;
225 }
226
227 return 0;
228}
229
230static void bf5xx_i2s_remove(struct platform_device *pdev,
231 struct snd_soc_dai *dai)
232{
233 pr_debug("%s enter\n", __func__);
234 peripheral_free_list(&sport_req[sport_num][0]);
235}
236
237#ifdef CONFIG_PM
238static int bf5xx_i2s_suspend(struct snd_soc_dai *dai)
239{
240
241 pr_debug("%s : sport %d\n", __func__, dai->id);
242
243 if (dai->capture.active)
244 sport_rx_stop(sport_handle);
245 if (dai->playback.active)
246 sport_tx_stop(sport_handle);
247 return 0;
248}
249
250static int bf5xx_i2s_resume(struct snd_soc_dai *dai)
251{
252 int ret;
253
254 pr_debug("%s : sport %d\n", __func__, dai->id);
255
256 ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
257 bf5xx_i2s.rcr2, 0, 0);
258 if (ret) {
259 pr_err("SPORT is busy!\n");
260 return -EBUSY;
261 }
262
263 ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
264 bf5xx_i2s.tcr2, 0, 0);
265 if (ret) {
266 pr_err("SPORT is busy!\n");
267 return -EBUSY;
268 }
269
270 return 0;
271}
272
273#else
274#define bf5xx_i2s_suspend NULL
275#define bf5xx_i2s_resume NULL
276#endif
277
278#define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
279 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
280 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
281 SNDRV_PCM_RATE_96000)
282
283#define BF5XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
284 SNDRV_PCM_FMTBIT_S32_LE)
285
286static struct snd_soc_dai_ops bf5xx_i2s_dai_ops = {
287 .startup = bf5xx_i2s_startup,
288 .shutdown = bf5xx_i2s_shutdown,
289 .hw_params = bf5xx_i2s_hw_params,
290 .set_fmt = bf5xx_i2s_set_dai_fmt,
291};
292
293struct snd_soc_dai bf5xx_i2s_dai = {
294 .name = "bf5xx-i2s",
295 .id = 0,
296 .probe = bf5xx_i2s_probe,
297 .remove = bf5xx_i2s_remove,
298 .suspend = bf5xx_i2s_suspend,
299 .resume = bf5xx_i2s_resume,
300 .playback = {
301 .channels_min = 1,
302 .channels_max = 2,
303 .rates = BF5XX_I2S_RATES,
304 .formats = BF5XX_I2S_FORMATS,},
305 .capture = {
306 .channels_min = 1,
307 .channels_max = 2,
308 .rates = BF5XX_I2S_RATES,
309 .formats = BF5XX_I2S_FORMATS,},
310 .ops = &bf5xx_i2s_dai_ops,
311};
312EXPORT_SYMBOL_GPL(bf5xx_i2s_dai);
313
314static int __init bfin_i2s_init(void)
315{
316 return snd_soc_register_dai(&bf5xx_i2s_dai);
317}
318module_init(bfin_i2s_init);
319
320static void __exit bfin_i2s_exit(void)
321{
322 snd_soc_unregister_dai(&bf5xx_i2s_dai);
323}
324module_exit(bfin_i2s_exit);
325
326
327MODULE_AUTHOR("Cliff Cai");
328MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
329MODULE_LICENSE("GPL");
330
331