Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 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; |
Cliff Cai | 895c9c0 | 2009-06-20 11:29:05 -0400 | [diff] [blame] | 53 | int configured; |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static struct bf5xx_i2s_port bf5xx_i2s; |
| 57 | static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM; |
| 58 | |
| 59 | static 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 | |
Cliff Cai | c3e5203 | 2008-10-27 17:09:25 +0800 | [diff] [blame] | 74 | /* |
| 75 | * Setting the TFS pin selector for SPORT 0 based on whether the selected |
| 76 | * port id F or G. If the port is F then no conflict should exist for the |
| 77 | * TFS. When Port G is selected and EMAC then there is a conflict between |
| 78 | * the PHY interrupt line and TFS. Current settings prevent the conflict |
| 79 | * by ignoring the TFS pin when Port G is selected. This allows both |
| 80 | * ssm2602 using Port G and EMAC concurrently. |
| 81 | */ |
| 82 | #ifdef CONFIG_BF527_SPORT0_PORTF |
| 83 | #define LOCAL_SPORT0_TFS (P_SPORT0_TFS) |
| 84 | #else |
| 85 | #define LOCAL_SPORT0_TFS (0) |
| 86 | #endif |
| 87 | |
| 88 | static 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} }; |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 92 | |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 93 | static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
| 94 | unsigned int fmt) |
| 95 | { |
| 96 | int ret = 0; |
| 97 | |
| 98 | /* interface format:support I2S,slave mode */ |
| 99 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 100 | case SND_SOC_DAIFMT_I2S: |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 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; |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 109 | break; |
| 110 | case SND_SOC_DAIFMT_LEFT_J: |
| 111 | ret = -EINVAL; |
| 112 | break; |
| 113 | default: |
Cliff Cai | c3e5203 | 2008-10-27 17:09:25 +0800 | [diff] [blame] | 114 | printk(KERN_ERR "%s: Unknown DAI format type\n", __func__); |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 115 | ret = -EINVAL; |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 120 | case SND_SOC_DAIFMT_CBM_CFM: |
| 121 | break; |
Cliff Cai | c3e5203 | 2008-10-27 17:09:25 +0800 | [diff] [blame] | 122 | case SND_SOC_DAIFMT_CBS_CFS: |
| 123 | case SND_SOC_DAIFMT_CBM_CFS: |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 124 | case SND_SOC_DAIFMT_CBS_CFM: |
| 125 | ret = -EINVAL; |
| 126 | break; |
| 127 | default: |
Cliff Cai | c3e5203 | 2008-10-27 17:09:25 +0800 | [diff] [blame] | 128 | printk(KERN_ERR "%s: Unknown DAI master type\n", __func__); |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 129 | ret = -EINVAL; |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 136 | static int bf5xx_i2s_startup(struct snd_pcm_substream *substream, |
| 137 | struct snd_soc_dai *dai) |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 138 | { |
| 139 | pr_debug("%s enter\n", __func__); |
| 140 | |
| 141 | /*this counter is used for counting how many pcm streams are opened*/ |
| 142 | bf5xx_i2s.counter++; |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 147 | struct snd_pcm_hw_params *params, |
| 148 | struct snd_soc_dai *dai) |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 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; |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 158 | sport_handle->wdsize = 2; |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 159 | break; |
| 160 | case SNDRV_PCM_FORMAT_S24_LE: |
| 161 | bf5xx_i2s.tcr2 |= 23; |
| 162 | bf5xx_i2s.rcr2 |= 23; |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 163 | sport_handle->wdsize = 3; |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 164 | break; |
| 165 | case SNDRV_PCM_FORMAT_S32_LE: |
| 166 | bf5xx_i2s.tcr2 |= 31; |
| 167 | bf5xx_i2s.rcr2 |= 31; |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 168 | sport_handle->wdsize = 4; |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | |
Cliff Cai | 895c9c0 | 2009-06-20 11:29:05 -0400 | [diff] [blame] | 172 | if (!bf5xx_i2s.configured) { |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 173 | /* |
| 174 | * TX and RX are not independent,they are enabled at the |
| 175 | * same time, even if only one side is running. So, we |
| 176 | * need to configure both of them at the time when the first |
| 177 | * stream is opened. |
| 178 | * |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 179 | * CPU DAI:slave mode. |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 180 | */ |
Cliff Cai | 895c9c0 | 2009-06-20 11:29:05 -0400 | [diff] [blame] | 181 | bf5xx_i2s.configured = 1; |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 182 | ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1, |
| 183 | bf5xx_i2s.rcr2, 0, 0); |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 184 | if (ret) { |
| 185 | pr_err("SPORT is busy!\n"); |
| 186 | return -EBUSY; |
| 187 | } |
| 188 | |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 189 | ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1, |
| 190 | bf5xx_i2s.tcr2, 0, 0); |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 191 | if (ret) { |
| 192 | pr_err("SPORT is busy!\n"); |
| 193 | return -EBUSY; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 200 | static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream, |
| 201 | struct snd_soc_dai *dai) |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 202 | { |
| 203 | pr_debug("%s enter\n", __func__); |
| 204 | bf5xx_i2s.counter--; |
Cliff Cai | 895c9c0 | 2009-06-20 11:29:05 -0400 | [diff] [blame] | 205 | /* No active stream, SPORT is allowed to be configured again. */ |
| 206 | if (!bf5xx_i2s.counter) |
| 207 | bf5xx_i2s.configured = 0; |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static int bf5xx_i2s_probe(struct platform_device *pdev, |
| 211 | struct snd_soc_dai *dai) |
| 212 | { |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 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 | /* request DMA for SPORT */ |
| 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 | |
Mark Brown | dc7d7b8 | 2008-12-03 18:21:52 +0000 | [diff] [blame] | 230 | static void bf5xx_i2s_remove(struct snd_soc_dai *dai) |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 231 | { |
| 232 | pr_debug("%s enter\n", __func__); |
| 233 | peripheral_free_list(&sport_req[sport_num][0]); |
| 234 | } |
| 235 | |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 236 | #ifdef CONFIG_PM |
Mark Brown | dc7d7b8 | 2008-12-03 18:21:52 +0000 | [diff] [blame] | 237 | static int bf5xx_i2s_suspend(struct snd_soc_dai *dai) |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 238 | { |
| 239 | struct sport_device *sport = |
| 240 | (struct sport_device *)dai->private_data; |
| 241 | |
| 242 | pr_debug("%s : sport %d\n", __func__, dai->id); |
| 243 | if (!dai->active) |
| 244 | return 0; |
| 245 | if (dai->capture.active) |
| 246 | sport_rx_stop(sport); |
| 247 | if (dai->playback.active) |
| 248 | sport_tx_stop(sport); |
| 249 | return 0; |
| 250 | } |
| 251 | |
Barry Song | 92a6ad3 | 2009-06-20 11:29:57 -0400 | [diff] [blame] | 252 | static int bf5xx_i2s_resume(struct snd_soc_dai *dai) |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 253 | { |
| 254 | int ret; |
| 255 | struct sport_device *sport = |
| 256 | (struct sport_device *)dai->private_data; |
| 257 | |
| 258 | pr_debug("%s : sport %d\n", __func__, dai->id); |
| 259 | if (!dai->active) |
| 260 | return 0; |
| 261 | |
| 262 | ret = sport_config_rx(sport_handle, RFSR | RCKFE, RSFSE|0x1f, 0, 0); |
| 263 | if (ret) { |
| 264 | pr_err("SPORT is busy!\n"); |
| 265 | return -EBUSY; |
| 266 | } |
| 267 | |
| 268 | ret = sport_config_tx(sport_handle, TFSR | TCKFE, TSFSE|0x1f, 0, 0); |
| 269 | if (ret) { |
| 270 | pr_err("SPORT is busy!\n"); |
| 271 | return -EBUSY; |
| 272 | } |
| 273 | |
| 274 | if (dai->capture.active) |
| 275 | sport_rx_start(sport); |
| 276 | if (dai->playback.active) |
| 277 | sport_tx_start(sport); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | #else |
| 282 | #define bf5xx_i2s_suspend NULL |
| 283 | #define bf5xx_i2s_resume NULL |
| 284 | #endif |
| 285 | |
| 286 | #define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 287 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \ |
| 288 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \ |
| 289 | SNDRV_PCM_RATE_96000) |
| 290 | |
| 291 | #define BF5XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\ |
| 292 | SNDRV_PCM_FMTBIT_S32_LE) |
| 293 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 294 | static struct snd_soc_dai_ops bf5xx_i2s_dai_ops = { |
| 295 | .startup = bf5xx_i2s_startup, |
| 296 | .shutdown = bf5xx_i2s_shutdown, |
| 297 | .hw_params = bf5xx_i2s_hw_params, |
| 298 | .set_fmt = bf5xx_i2s_set_dai_fmt, |
| 299 | }; |
| 300 | |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 301 | struct snd_soc_dai bf5xx_i2s_dai = { |
| 302 | .name = "bf5xx-i2s", |
| 303 | .id = 0, |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 304 | .probe = bf5xx_i2s_probe, |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 305 | .remove = bf5xx_i2s_remove, |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 306 | .suspend = bf5xx_i2s_suspend, |
| 307 | .resume = bf5xx_i2s_resume, |
| 308 | .playback = { |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 309 | .channels_min = 1, |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 310 | .channels_max = 2, |
| 311 | .rates = BF5XX_I2S_RATES, |
| 312 | .formats = BF5XX_I2S_FORMATS,}, |
| 313 | .capture = { |
Cliff Cai | 3339268 | 2008-09-27 22:30:15 +0800 | [diff] [blame] | 314 | .channels_min = 1, |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 315 | .channels_max = 2, |
| 316 | .rates = BF5XX_I2S_RATES, |
| 317 | .formats = BF5XX_I2S_FORMATS,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 318 | .ops = &bf5xx_i2s_dai_ops, |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 319 | }; |
| 320 | EXPORT_SYMBOL_GPL(bf5xx_i2s_dai); |
| 321 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 322 | static int __init bfin_i2s_init(void) |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 323 | { |
Mark Brown | 4544f8a | 2008-12-11 16:11:38 +0000 | [diff] [blame] | 324 | return snd_soc_register_dai(&bf5xx_i2s_dai); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 325 | } |
| 326 | module_init(bfin_i2s_init); |
| 327 | |
| 328 | static void __exit bfin_i2s_exit(void) |
| 329 | { |
Mark Brown | 4544f8a | 2008-12-11 16:11:38 +0000 | [diff] [blame] | 330 | snd_soc_unregister_dai(&bf5xx_i2s_dai); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 331 | } |
| 332 | module_exit(bfin_i2s_exit); |
| 333 | |
Cliff Cai | 912c2ac | 2008-09-05 18:21:39 +0800 | [diff] [blame] | 334 | /* Module information */ |
| 335 | MODULE_AUTHOR("Cliff Cai"); |
| 336 | MODULE_DESCRIPTION("I2S driver for ADI Blackfin"); |
| 337 | MODULE_LICENSE("GPL"); |
| 338 | |