blob: 9a174fc47d39b38b0bf3ddfbd18f8367136744c4 [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"
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +020045#include "bf5xx-i2s-pcm.h"
Cliff Cai912c2ac2008-09-05 18:21:39 +080046
47struct bf5xx_i2s_port {
48 u16 tcr1;
49 u16 rcr1;
50 u16 tcr2;
51 u16 rcr2;
Cliff Cai895c9c02009-06-20 11:29:05 -040052 int configured;
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +020053
54 unsigned int slots;
55 unsigned int tx_mask;
56 unsigned int rx_mask;
57
58 struct bf5xx_i2s_pcm_data tx_dma_data;
59 struct bf5xx_i2s_pcm_data rx_dma_data;
Cliff Cai912c2ac2008-09-05 18:21:39 +080060};
61
Cliff Cai912c2ac2008-09-05 18:21:39 +080062static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
63 unsigned int fmt)
64{
Barry Song2c66cb92011-03-28 01:45:10 -040065 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
66 struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
Cliff Cai912c2ac2008-09-05 18:21:39 +080067 int ret = 0;
68
69 /* interface format:support I2S,slave mode */
70 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
71 case SND_SOC_DAIFMT_I2S:
Barry Song2c66cb92011-03-28 01:45:10 -040072 bf5xx_i2s->tcr1 |= TFSR | TCKFE;
73 bf5xx_i2s->rcr1 |= RFSR | RCKFE;
74 bf5xx_i2s->tcr2 |= TSFSE;
75 bf5xx_i2s->rcr2 |= RSFSE;
Cliff Cai33392682008-09-27 22:30:15 +080076 break;
77 case SND_SOC_DAIFMT_DSP_A:
Barry Song2c66cb92011-03-28 01:45:10 -040078 bf5xx_i2s->tcr1 |= TFSR;
79 bf5xx_i2s->rcr1 |= RFSR;
Cliff Cai912c2ac2008-09-05 18:21:39 +080080 break;
81 case SND_SOC_DAIFMT_LEFT_J:
82 ret = -EINVAL;
83 break;
84 default:
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +020085 dev_err(cpu_dai->dev, "%s: Unknown DAI format type\n",
86 __func__);
Cliff Cai912c2ac2008-09-05 18:21:39 +080087 ret = -EINVAL;
88 break;
89 }
90
91 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Cliff Cai912c2ac2008-09-05 18:21:39 +080092 case SND_SOC_DAIFMT_CBM_CFM:
93 break;
Cliff Caic3e52032008-10-27 17:09:25 +080094 case SND_SOC_DAIFMT_CBS_CFS:
95 case SND_SOC_DAIFMT_CBM_CFS:
Cliff Cai912c2ac2008-09-05 18:21:39 +080096 case SND_SOC_DAIFMT_CBS_CFM:
97 ret = -EINVAL;
98 break;
99 default:
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200100 dev_err(cpu_dai->dev, "%s: Unknown DAI master type\n",
101 __func__);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800102 ret = -EINVAL;
103 break;
104 }
105
106 return ret;
107}
108
Cliff Cai912c2ac2008-09-05 18:21:39 +0800109static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000110 struct snd_pcm_hw_params *params,
111 struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800112{
Barry Song2c66cb92011-03-28 01:45:10 -0400113 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
114 struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800115 int ret = 0;
116
Barry Song2c66cb92011-03-28 01:45:10 -0400117 bf5xx_i2s->tcr2 &= ~0x1f;
118 bf5xx_i2s->rcr2 &= ~0x1f;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800119 switch (params_format(params)) {
Cliff Cai4b2ffc22011-03-26 03:05:08 -0400120 case SNDRV_PCM_FORMAT_S8:
121 bf5xx_i2s->tcr2 |= 7;
122 bf5xx_i2s->rcr2 |= 7;
123 sport_handle->wdsize = 1;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800124 case SNDRV_PCM_FORMAT_S16_LE:
Barry Song2c66cb92011-03-28 01:45:10 -0400125 bf5xx_i2s->tcr2 |= 15;
126 bf5xx_i2s->rcr2 |= 15;
Cliff Cai33392682008-09-27 22:30:15 +0800127 sport_handle->wdsize = 2;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800128 break;
129 case SNDRV_PCM_FORMAT_S24_LE:
Barry Song2c66cb92011-03-28 01:45:10 -0400130 bf5xx_i2s->tcr2 |= 23;
131 bf5xx_i2s->rcr2 |= 23;
Cliff Cai33392682008-09-27 22:30:15 +0800132 sport_handle->wdsize = 3;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800133 break;
134 case SNDRV_PCM_FORMAT_S32_LE:
Barry Song2c66cb92011-03-28 01:45:10 -0400135 bf5xx_i2s->tcr2 |= 31;
136 bf5xx_i2s->rcr2 |= 31;
Cliff Cai33392682008-09-27 22:30:15 +0800137 sport_handle->wdsize = 4;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800138 break;
139 }
140
Barry Song2c66cb92011-03-28 01:45:10 -0400141 if (!bf5xx_i2s->configured) {
Cliff Cai912c2ac2008-09-05 18:21:39 +0800142 /*
143 * TX and RX are not independent,they are enabled at the
144 * same time, even if only one side is running. So, we
145 * need to configure both of them at the time when the first
146 * stream is opened.
147 *
Cliff Cai33392682008-09-27 22:30:15 +0800148 * CPU DAI:slave mode.
Cliff Cai912c2ac2008-09-05 18:21:39 +0800149 */
Barry Song2c66cb92011-03-28 01:45:10 -0400150 bf5xx_i2s->configured = 1;
151 ret = sport_config_rx(sport_handle, bf5xx_i2s->rcr1,
152 bf5xx_i2s->rcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800153 if (ret) {
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200154 dev_err(dai->dev, "SPORT is busy!\n");
Cliff Cai912c2ac2008-09-05 18:21:39 +0800155 return -EBUSY;
156 }
157
Barry Song2c66cb92011-03-28 01:45:10 -0400158 ret = sport_config_tx(sport_handle, bf5xx_i2s->tcr1,
159 bf5xx_i2s->tcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800160 if (ret) {
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200161 dev_err(dai->dev, "SPORT is busy!\n");
Cliff Cai912c2ac2008-09-05 18:21:39 +0800162 return -EBUSY;
163 }
164 }
165
166 return 0;
167}
168
Mark Browndee89c42008-11-18 22:11:38 +0000169static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
170 struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800171{
Barry Song2c66cb92011-03-28 01:45:10 -0400172 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
173 struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
174
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200175 dev_dbg(dai->dev, "%s enter\n", __func__);
Cliff Cai895c9c02009-06-20 11:29:05 -0400176 /* No active stream, SPORT is allowed to be configured again. */
Barry Song766df6d2009-09-23 11:51:04 -0400177 if (!dai->active)
Barry Song2c66cb92011-03-28 01:45:10 -0400178 bf5xx_i2s->configured = 0;
Cliff Cai33392682008-09-27 22:30:15 +0800179}
180
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +0200181static int bf5xx_i2s_set_channel_map(struct snd_soc_dai *dai,
182 unsigned int tx_num, unsigned int *tx_slot,
183 unsigned int rx_num, unsigned int *rx_slot)
184{
185 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
186 struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
187 unsigned int tx_mapped = 0, rx_mapped = 0;
188 unsigned int slot;
189 int i;
190
191 if ((tx_num > BFIN_TDM_DAI_MAX_SLOTS) ||
192 (rx_num > BFIN_TDM_DAI_MAX_SLOTS))
193 return -EINVAL;
194
195 for (i = 0; i < tx_num; i++) {
196 slot = tx_slot[i];
197 if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
198 (!(tx_mapped & (1 << slot)))) {
199 bf5xx_i2s->tx_dma_data.map[i] = slot;
200 tx_mapped |= 1 << slot;
201 } else
202 return -EINVAL;
203 }
204 for (i = 0; i < rx_num; i++) {
205 slot = rx_slot[i];
206 if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
207 (!(rx_mapped & (1 << slot)))) {
208 bf5xx_i2s->rx_dma_data.map[i] = slot;
209 rx_mapped |= 1 << slot;
210 } else
211 return -EINVAL;
212 }
213
214 return 0;
215}
216
217static int bf5xx_i2s_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
218 unsigned int rx_mask, int slots, int width)
219{
220 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
221 struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
222
223 if (slots % 8 != 0 || slots > 8)
224 return -EINVAL;
225
226 if (width != 32)
227 return -EINVAL;
228
229 bf5xx_i2s->slots = slots;
230 bf5xx_i2s->tx_mask = tx_mask;
231 bf5xx_i2s->rx_mask = rx_mask;
232
233 bf5xx_i2s->tx_dma_data.tdm_mode = slots != 0;
234 bf5xx_i2s->rx_dma_data.tdm_mode = slots != 0;
235
236 return sport_set_multichannel(sport_handle, slots, tx_mask, rx_mask, 0);
237}
238
Cliff Cai912c2ac2008-09-05 18:21:39 +0800239#ifdef CONFIG_PM
Mark Browndc7d7b82008-12-03 18:21:52 +0000240static int bf5xx_i2s_suspend(struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800241{
Barry Song2c66cb92011-03-28 01:45:10 -0400242 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800243
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200244 dev_dbg(dai->dev, "%s : sport %d\n", __func__, dai->id);
Cliff Caiad80efc2009-09-16 20:25:12 -0400245
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000246 if (dai->capture_active)
Cliff Caiad80efc2009-09-16 20:25:12 -0400247 sport_rx_stop(sport_handle);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000248 if (dai->playback_active)
Cliff Caiad80efc2009-09-16 20:25:12 -0400249 sport_tx_stop(sport_handle);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800250 return 0;
251}
252
Barry Song92a6ad32009-06-20 11:29:57 -0400253static int bf5xx_i2s_resume(struct snd_soc_dai *dai)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800254{
Barry Song2c66cb92011-03-28 01:45:10 -0400255 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
256 struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800257 int ret;
Cliff Cai912c2ac2008-09-05 18:21:39 +0800258
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200259 dev_dbg(dai->dev, "%s : sport %d\n", __func__, dai->id);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800260
Barry Song2c66cb92011-03-28 01:45:10 -0400261 ret = sport_config_rx(sport_handle, bf5xx_i2s->rcr1,
262 bf5xx_i2s->rcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800263 if (ret) {
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200264 dev_err(dai->dev, "SPORT is busy!\n");
Cliff Cai912c2ac2008-09-05 18:21:39 +0800265 return -EBUSY;
266 }
267
Barry Song2c66cb92011-03-28 01:45:10 -0400268 ret = sport_config_tx(sport_handle, bf5xx_i2s->tcr1,
269 bf5xx_i2s->tcr2, 0, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800270 if (ret) {
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200271 dev_err(dai->dev, "SPORT is busy!\n");
Cliff Cai912c2ac2008-09-05 18:21:39 +0800272 return -EBUSY;
273 }
274
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +0200275 return sport_set_multichannel(sport_handle, bf5xx_i2s->slots,
276 bf5xx_i2s->tx_mask, bf5xx_i2s->rx_mask, 0);
Cliff Cai912c2ac2008-09-05 18:21:39 +0800277}
278
279#else
280#define bf5xx_i2s_suspend NULL
281#define bf5xx_i2s_resume NULL
282#endif
283
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +0200284static int bf5xx_i2s_dai_probe(struct snd_soc_dai *dai)
285{
286 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
287 struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
288 unsigned int i;
289
290 for (i = 0; i < BFIN_TDM_DAI_MAX_SLOTS; i++) {
291 bf5xx_i2s->tx_dma_data.map[i] = i;
292 bf5xx_i2s->rx_dma_data.map[i] = i;
293 }
294
295 dai->playback_dma_data = &bf5xx_i2s->tx_dma_data;
296 dai->capture_dma_data = &bf5xx_i2s->rx_dma_data;
297
298 return 0;
299}
300
Cliff Cai912c2ac2008-09-05 18:21:39 +0800301#define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
302 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
303 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
304 SNDRV_PCM_RATE_96000)
305
Cliff Cai4b2ffc22011-03-26 03:05:08 -0400306#define BF5XX_I2S_FORMATS \
307 (SNDRV_PCM_FMTBIT_S8 | \
308 SNDRV_PCM_FMTBIT_S16_LE | \
309 SNDRV_PCM_FMTBIT_S24_LE | \
310 SNDRV_PCM_FMTBIT_S32_LE)
Cliff Cai912c2ac2008-09-05 18:21:39 +0800311
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100312static const struct snd_soc_dai_ops bf5xx_i2s_dai_ops = {
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +0200313 .shutdown = bf5xx_i2s_shutdown,
314 .hw_params = bf5xx_i2s_hw_params,
315 .set_fmt = bf5xx_i2s_set_dai_fmt,
316 .set_tdm_slot = bf5xx_i2s_set_tdm_slot,
317 .set_channel_map = bf5xx_i2s_set_channel_map,
Eric Miao6335d052009-03-03 09:41:00 +0800318};
319
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320static struct snd_soc_dai_driver bf5xx_i2s_dai = {
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +0200321 .probe = bf5xx_i2s_dai_probe,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800322 .suspend = bf5xx_i2s_suspend,
323 .resume = bf5xx_i2s_resume,
324 .playback = {
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +0200325 .channels_min = 2,
326 .channels_max = 8,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800327 .rates = BF5XX_I2S_RATES,
328 .formats = BF5XX_I2S_FORMATS,},
329 .capture = {
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +0200330 .channels_min = 2,
331 .channels_max = 8,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800332 .rates = BF5XX_I2S_RATES,
333 .formats = BF5XX_I2S_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800334 .ops = &bf5xx_i2s_dai_ops,
Cliff Cai912c2ac2008-09-05 18:21:39 +0800335};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000336
Kuninori Morimotob56733b2013-03-21 03:30:08 -0700337static const struct snd_soc_component_driver bf5xx_i2s_component = {
338 .name = "bf5xx-i2s",
339};
340
Bill Pembertondca66da2012-12-07 09:26:13 -0500341static int bf5xx_i2s_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000342{
Barry Song2c66cb92011-03-28 01:45:10 -0400343 struct sport_device *sport_handle;
344 int ret;
345
346 /* configure SPORT for I2S */
Lars-Peter Clausen8b5e2e32013-05-28 19:22:14 +0200347 sport_handle = sport_init(pdev, 4, 8 * sizeof(u32),
Barry Song2c66cb92011-03-28 01:45:10 -0400348 sizeof(struct bf5xx_i2s_port));
349 if (!sport_handle)
350 return -ENODEV;
351
352 /* register with the ASoC layers */
Kuninori Morimotob56733b2013-03-21 03:30:08 -0700353 ret = snd_soc_register_component(&pdev->dev, &bf5xx_i2s_component,
354 &bf5xx_i2s_dai, 1);
Barry Song2c66cb92011-03-28 01:45:10 -0400355 if (ret) {
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200356 dev_err(&pdev->dev, "Failed to register DAI: %d\n", ret);
Barry Song2c66cb92011-03-28 01:45:10 -0400357 sport_done(sport_handle);
358 return ret;
359 }
360
361 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000362}
363
Bill Pembertondca66da2012-12-07 09:26:13 -0500364static int bf5xx_i2s_remove(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000365{
Barry Song2c66cb92011-03-28 01:45:10 -0400366 struct sport_device *sport_handle = platform_get_drvdata(pdev);
367
Lars-Peter Clausenb7ede5d2013-05-28 19:22:10 +0200368 dev_dbg(&pdev->dev, "%s enter\n", __func__);
Barry Song2c66cb92011-03-28 01:45:10 -0400369
Kuninori Morimotob56733b2013-03-21 03:30:08 -0700370 snd_soc_unregister_component(&pdev->dev);
Barry Song2c66cb92011-03-28 01:45:10 -0400371 sport_done(sport_handle);
372
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000373 return 0;
374}
375
376static struct platform_driver bfin_i2s_driver = {
Barry Song2c66cb92011-03-28 01:45:10 -0400377 .probe = bf5xx_i2s_probe,
Bill Pembertondca66da2012-12-07 09:26:13 -0500378 .remove = bf5xx_i2s_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000379 .driver = {
Mike Frysingerbfe4ee02011-03-28 01:45:09 -0400380 .name = "bfin-i2s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000381 .owner = THIS_MODULE,
382 },
383};
Cliff Cai912c2ac2008-09-05 18:21:39 +0800384
Axel Linfb802972011-11-24 14:44:52 +0800385module_platform_driver(bfin_i2s_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000386
Cliff Cai912c2ac2008-09-05 18:21:39 +0800387/* Module information */
388MODULE_AUTHOR("Cliff Cai");
389MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
390MODULE_LICENSE("GPL");
391