blob: dacd86c2ead4bb13e6e4066e39b0fab871c329f8 [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"
Cliff Cai912c2ac2008-09-05 18:21:39 +080045
46struct bf5xx_i2s_port {
47 u16 tcr1;
48 u16 rcr1;
49 u16 tcr2;
50 u16 rcr2;
Cliff Cai895c9c02009-06-20 11:29:05 -040051 int configured;
Cliff Cai912c2ac2008-09-05 18:21:39 +080052};
53
54static struct bf5xx_i2s_port bf5xx_i2s;
55static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
56
57static struct sport_param sport_params[2] = {
58 {
59 .dma_rx_chan = CH_SPORT0_RX,
60 .dma_tx_chan = CH_SPORT0_TX,
61 .err_irq = IRQ_SPORT0_ERROR,
62 .regs = (struct sport_register *)SPORT0_TCR1,
63 },
64 {
65 .dma_rx_chan = CH_SPORT1_RX,
66 .dma_tx_chan = CH_SPORT1_TX,
67 .err_irq = IRQ_SPORT1_ERROR,
68 .regs = (struct sport_register *)SPORT1_TCR1,
69 }
70};
71
Cliff Caic3e52032008-10-27 17:09:25 +080072/*
73 * Setting the TFS pin selector for SPORT 0 based on whether the selected
74 * port id F or G. If the port is F then no conflict should exist for the
75 * TFS. When Port G is selected and EMAC then there is a conflict between
76 * the PHY interrupt line and TFS. Current settings prevent the conflict
77 * by ignoring the TFS pin when Port G is selected. This allows both
Cliff Caidf0fd5e2009-09-23 11:51:05 -040078 * codecs and EMAC using Port G concurrently.
Cliff Caic3e52032008-10-27 17:09:25 +080079 */
Cliff Caidf0fd5e2009-09-23 11:51:05 -040080#ifdef CONFIG_BF527_SPORT0_PORTG
Cliff Caic3e52032008-10-27 17:09:25 +080081#define LOCAL_SPORT0_TFS (0)
Cliff Caidf0fd5e2009-09-23 11:51:05 -040082#else
83#define LOCAL_SPORT0_TFS (P_SPORT0_TFS)
Cliff Caic3e52032008-10-27 17:09:25 +080084#endif
85
86static u16 sport_req[][7] = { {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
87 P_SPORT0_DRPRI, P_SPORT0_RSCLK, LOCAL_SPORT0_TFS, 0},
88 {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, P_SPORT1_DRPRI,
89 P_SPORT1_RSCLK, P_SPORT1_TFS, 0} };
Cliff Cai33392682008-09-27 22:30:15 +080090
Cliff Cai912c2ac2008-09-05 18:21:39 +080091static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
92 unsigned int fmt)
93{
94 int ret = 0;
95
96 /* interface format:support I2S,slave mode */
97 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
98 case SND_SOC_DAIFMT_I2S:
Cliff Cai33392682008-09-27 22:30:15 +080099 bf5xx_i2s.tcr1 |= TFSR | TCKFE;
100 bf5xx_i2s.rcr1 |= RFSR | RCKFE;
101 bf5xx_i2s.tcr2 |= TSFSE;
102 bf5xx_i2s.rcr2 |= RSFSE;
103 break;
104 case SND_SOC_DAIFMT_DSP_A:
105 bf5xx_i2s.tcr1 |= TFSR;
106 bf5xx_i2s.rcr1 |= RFSR;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800107 break;
108 case SND_SOC_DAIFMT_LEFT_J:
109 ret = -EINVAL;
110 break;
111 default:
Cliff Caic3e52032008-10-27 17:09:25 +0800112 printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800113 ret = -EINVAL;
114 break;
115 }
116
117 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Cliff Cai912c2ac2008-09-05 18:21:39 +0800118 case SND_SOC_DAIFMT_CBM_CFM:
119 break;
Cliff Caic3e52032008-10-27 17:09:25 +0800120 case SND_SOC_DAIFMT_CBS_CFS:
121 case SND_SOC_DAIFMT_CBM_CFS:
Cliff Cai912c2ac2008-09-05 18:21:39 +0800122 case SND_SOC_DAIFMT_CBS_CFM:
123 ret = -EINVAL;
124 break;
125 default:
Cliff Caic3e52032008-10-27 17:09:25 +0800126 printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800127 ret = -EINVAL;
128 break;
129 }
130
131 return ret;
132}
133
Cliff Cai912c2ac2008-09-05 18:21:39 +0800134static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000135 struct snd_pcm_hw_params *params,
136 struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800137{
138 int ret = 0;
139
140 bf5xx_i2s.tcr2 &= ~0x1f;
141 bf5xx_i2s.rcr2 &= ~0x1f;
142 switch (params_format(params)) {
Cliff Cai4b2ffc22011-03-26 03:05:08 -0400143 case SNDRV_PCM_FORMAT_S8:
144 bf5xx_i2s->tcr2 |= 7;
145 bf5xx_i2s->rcr2 |= 7;
146 sport_handle->wdsize = 1;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800147 case SNDRV_PCM_FORMAT_S16_LE:
148 bf5xx_i2s.tcr2 |= 15;
149 bf5xx_i2s.rcr2 |= 15;
Cliff Cai33392682008-09-27 22:30:15 +0800150 sport_handle->wdsize = 2;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800151 break;
152 case SNDRV_PCM_FORMAT_S24_LE:
153 bf5xx_i2s.tcr2 |= 23;
154 bf5xx_i2s.rcr2 |= 23;
Cliff Cai33392682008-09-27 22:30:15 +0800155 sport_handle->wdsize = 3;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800156 break;
157 case SNDRV_PCM_FORMAT_S32_LE:
158 bf5xx_i2s.tcr2 |= 31;
159 bf5xx_i2s.rcr2 |= 31;
Cliff Cai33392682008-09-27 22:30:15 +0800160 sport_handle->wdsize = 4;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800161 break;
162 }
163
Cliff Cai895c9c02009-06-20 11:29:05 -0400164 if (!bf5xx_i2s.configured) {
Cliff Cai912c2ac2008-09-05 18:21:39 +0800165 /*
166 * TX and RX are not independent,they are enabled at the
167 * same time, even if only one side is running. So, we
168 * need to configure both of them at the time when the first
169 * stream is opened.
170 *
Cliff Cai33392682008-09-27 22:30:15 +0800171 * CPU DAI:slave mode.
Cliff Cai912c2ac2008-09-05 18:21:39 +0800172 */
Cliff Cai895c9c02009-06-20 11:29:05 -0400173 bf5xx_i2s.configured = 1;
Cliff Cai33392682008-09-27 22:30:15 +0800174 ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
175 bf5xx_i2s.rcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800176 if (ret) {
177 pr_err("SPORT is busy!\n");
178 return -EBUSY;
179 }
180
Cliff Cai33392682008-09-27 22:30:15 +0800181 ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
182 bf5xx_i2s.tcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800183 if (ret) {
184 pr_err("SPORT is busy!\n");
185 return -EBUSY;
186 }
187 }
188
189 return 0;
190}
191
Mark Browndee89c42008-11-18 22:11:38 +0000192static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
193 struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800194{
195 pr_debug("%s enter\n", __func__);
Cliff Cai895c9c02009-06-20 11:29:05 -0400196 /* No active stream, SPORT is allowed to be configured again. */
Barry Song766df6d2009-09-23 11:51:04 -0400197 if (!dai->active)
Cliff Cai895c9c02009-06-20 11:29:05 -0400198 bf5xx_i2s.configured = 0;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800199}
200
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000201static int bf5xx_i2s_probe(struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800202{
Cliff Cai912c2ac2008-09-05 18:21:39 +0800203 pr_debug("%s enter\n", __func__);
204 if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
205 pr_err("Requesting Peripherals failed\n");
206 return -EFAULT;
207 }
208
209 /* request DMA for SPORT */
210 sport_handle = sport_init(&sport_params[sport_num], 4, \
211 2 * sizeof(u32), NULL);
212 if (!sport_handle) {
213 peripheral_free_list(&sport_req[sport_num][0]);
214 return -ENODEV;
215 }
216
217 return 0;
218}
219
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000220static int bf5xx_i2s_remove(struct snd_soc_dai *dai)
Cliff Cai33392682008-09-27 22:30:15 +0800221{
222 pr_debug("%s enter\n", __func__);
223 peripheral_free_list(&sport_req[sport_num][0]);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000224 return 0;
Cliff Cai33392682008-09-27 22:30:15 +0800225}
226
Cliff Cai912c2ac2008-09-05 18:21:39 +0800227#ifdef CONFIG_PM
Mark Browndc7d7b82008-12-03 18:21:52 +0000228static int bf5xx_i2s_suspend(struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800229{
Cliff Cai912c2ac2008-09-05 18:21:39 +0800230
231 pr_debug("%s : sport %d\n", __func__, dai->id);
Cliff Caiad80efc2009-09-16 20:25:12 -0400232
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000233 if (dai->capture_active)
Cliff Caiad80efc2009-09-16 20:25:12 -0400234 sport_rx_stop(sport_handle);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000235 if (dai->playback_active)
Cliff Caiad80efc2009-09-16 20:25:12 -0400236 sport_tx_stop(sport_handle);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800237 return 0;
238}
239
Barry Song92a6ad32009-06-20 11:29:57 -0400240static int bf5xx_i2s_resume(struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800241{
242 int ret;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800243
244 pr_debug("%s : sport %d\n", __func__, dai->id);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800245
Cliff Caiad80efc2009-09-16 20:25:12 -0400246 ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
247 bf5xx_i2s.rcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800248 if (ret) {
249 pr_err("SPORT is busy!\n");
250 return -EBUSY;
251 }
252
Cliff Caiad80efc2009-09-16 20:25:12 -0400253 ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
254 bf5xx_i2s.tcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800255 if (ret) {
256 pr_err("SPORT is busy!\n");
257 return -EBUSY;
258 }
259
Cliff Cai912c2ac2008-09-05 18:21:39 +0800260 return 0;
261}
262
263#else
264#define bf5xx_i2s_suspend NULL
265#define bf5xx_i2s_resume NULL
266#endif
267
268#define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
269 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
270 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
271 SNDRV_PCM_RATE_96000)
272
Cliff Cai4b2ffc22011-03-26 03:05:08 -0400273#define BF5XX_I2S_FORMATS \
274 (SNDRV_PCM_FMTBIT_S8 | \
275 SNDRV_PCM_FMTBIT_S16_LE | \
276 SNDRV_PCM_FMTBIT_S24_LE | \
277 SNDRV_PCM_FMTBIT_S32_LE)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800278
Eric Miao6335d052009-03-03 09:41:00 +0800279static struct snd_soc_dai_ops bf5xx_i2s_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800280 .shutdown = bf5xx_i2s_shutdown,
281 .hw_params = bf5xx_i2s_hw_params,
282 .set_fmt = bf5xx_i2s_set_dai_fmt,
283};
284
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000285static struct snd_soc_dai_driver bf5xx_i2s_dai = {
Cliff Cai912c2ac2008-09-05 18:21:39 +0800286 .probe = bf5xx_i2s_probe,
Cliff Cai33392682008-09-27 22:30:15 +0800287 .remove = bf5xx_i2s_remove,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800288 .suspend = bf5xx_i2s_suspend,
289 .resume = bf5xx_i2s_resume,
290 .playback = {
Cliff Cai33392682008-09-27 22:30:15 +0800291 .channels_min = 1,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800292 .channels_max = 2,
293 .rates = BF5XX_I2S_RATES,
294 .formats = BF5XX_I2S_FORMATS,},
295 .capture = {
Cliff Cai33392682008-09-27 22:30:15 +0800296 .channels_min = 1,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800297 .channels_max = 2,
298 .rates = BF5XX_I2S_RATES,
299 .formats = BF5XX_I2S_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800300 .ops = &bf5xx_i2s_dai_ops,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800301};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000302
303static int bfin_i2s_drv_probe(struct platform_device *pdev)
304{
305 return snd_soc_register_dai(&pdev->dev, &bf5xx_i2s_dai);
306}
307
308static int __devexit bfin_i2s_drv_remove(struct platform_device *pdev)
309{
310 snd_soc_unregister_dai(&pdev->dev);
311 return 0;
312}
313
314static struct platform_driver bfin_i2s_driver = {
315 .probe = bfin_i2s_drv_probe,
316 .remove = __devexit_p(bfin_i2s_drv_remove),
317
318 .driver = {
Mike Frysingerbfe4ee02011-03-28 01:45:09 -0400319 .name = "bfin-i2s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320 .owner = THIS_MODULE,
321 },
322};
Cliff Cai912c2ac2008-09-05 18:21:39 +0800323
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100324static int __init bfin_i2s_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000325{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000326 return platform_driver_register(&bfin_i2s_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000327}
Mark Brown3f4b7832008-12-03 19:26:35 +0000328
329static void __exit bfin_i2s_exit(void)
330{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000331 platform_driver_unregister(&bfin_i2s_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000332}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000333
334module_init(bfin_i2s_init);
Mark Brown3f4b7832008-12-03 19:26:35 +0000335module_exit(bfin_i2s_exit);
336
Cliff Cai912c2ac2008-09-05 18:21:39 +0800337/* Module information */
338MODULE_AUTHOR("Cliff Cai");
339MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
340MODULE_LICENSE("GPL");
341