Barry Song | 01e2ab2 | 2009-07-27 18:06:39 +0800 | [diff] [blame] | 1 | /* |
| 2 | * File: sound/soc/blackfin/bf5xx-tdm.c |
| 3 | * Author: Barry Song <Barry.Song@analog.com> |
| 4 | * |
| 5 | * Created: Thurs June 04 2009 |
| 6 | * Description: Blackfin I2S(TDM) CPU DAI driver |
| 7 | * Even though TDM mode can be as part of I2S DAI, but there |
| 8 | * are so much difference in configuration and data flow, |
| 9 | * it's very ugly to integrate I2S and TDM into a module |
| 10 | * |
| 11 | * Modified: |
| 12 | * Copyright 2009 Analog Devices Inc. |
| 13 | * |
| 14 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, see the file COPYING, or write |
| 28 | * to the Free Software Foundation, Inc., |
| 29 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 | */ |
| 31 | |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/device.h> |
| 35 | #include <sound/core.h> |
| 36 | #include <sound/pcm.h> |
| 37 | #include <sound/pcm_params.h> |
| 38 | #include <sound/initval.h> |
| 39 | #include <sound/soc.h> |
| 40 | |
| 41 | #include <asm/irq.h> |
| 42 | #include <asm/portmux.h> |
| 43 | #include <linux/mutex.h> |
| 44 | #include <linux/gpio.h> |
| 45 | |
| 46 | #include "bf5xx-sport.h" |
| 47 | #include "bf5xx-tdm.h" |
| 48 | |
Barry Song | 01e2ab2 | 2009-07-27 18:06:39 +0800 | [diff] [blame] | 49 | static struct bf5xx_tdm_port bf5xx_tdm; |
| 50 | static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM; |
| 51 | |
| 52 | static struct sport_param sport_params[2] = { |
| 53 | { |
| 54 | .dma_rx_chan = CH_SPORT0_RX, |
| 55 | .dma_tx_chan = CH_SPORT0_TX, |
| 56 | .err_irq = IRQ_SPORT0_ERROR, |
| 57 | .regs = (struct sport_register *)SPORT0_TCR1, |
| 58 | }, |
| 59 | { |
| 60 | .dma_rx_chan = CH_SPORT1_RX, |
| 61 | .dma_tx_chan = CH_SPORT1_TX, |
| 62 | .err_irq = IRQ_SPORT1_ERROR, |
| 63 | .regs = (struct sport_register *)SPORT1_TCR1, |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * Setting the TFS pin selector for SPORT 0 based on whether the selected |
| 69 | * port id F or G. If the port is F then no conflict should exist for the |
| 70 | * TFS. When Port G is selected and EMAC then there is a conflict between |
| 71 | * the PHY interrupt line and TFS. Current settings prevent the conflict |
| 72 | * by ignoring the TFS pin when Port G is selected. This allows both |
Cliff Cai | df0fd5e | 2009-09-23 11:51:05 -0400 | [diff] [blame] | 73 | * codecs and EMAC using Port G concurrently. |
Barry Song | 01e2ab2 | 2009-07-27 18:06:39 +0800 | [diff] [blame] | 74 | */ |
Cliff Cai | df0fd5e | 2009-09-23 11:51:05 -0400 | [diff] [blame] | 75 | #ifdef CONFIG_BF527_SPORT0_PORTG |
Barry Song | 01e2ab2 | 2009-07-27 18:06:39 +0800 | [diff] [blame] | 76 | #define LOCAL_SPORT0_TFS (0) |
Cliff Cai | df0fd5e | 2009-09-23 11:51:05 -0400 | [diff] [blame] | 77 | #else |
| 78 | #define LOCAL_SPORT0_TFS (P_SPORT0_TFS) |
Barry Song | 01e2ab2 | 2009-07-27 18:06:39 +0800 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | static u16 sport_req[][7] = { {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS, |
| 82 | P_SPORT0_DRPRI, P_SPORT0_RSCLK, LOCAL_SPORT0_TFS, 0}, |
| 83 | {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, P_SPORT1_DRPRI, |
| 84 | P_SPORT1_RSCLK, P_SPORT1_TFS, 0} }; |
| 85 | |
| 86 | static int bf5xx_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
| 87 | unsigned int fmt) |
| 88 | { |
| 89 | int ret = 0; |
| 90 | |
| 91 | /* interface format:support TDM,slave mode */ |
| 92 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 93 | case SND_SOC_DAIFMT_DSP_A: |
| 94 | break; |
| 95 | default: |
| 96 | printk(KERN_ERR "%s: Unknown DAI format type\n", __func__); |
| 97 | ret = -EINVAL; |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 102 | case SND_SOC_DAIFMT_CBM_CFM: |
| 103 | break; |
| 104 | case SND_SOC_DAIFMT_CBS_CFS: |
| 105 | case SND_SOC_DAIFMT_CBM_CFS: |
| 106 | case SND_SOC_DAIFMT_CBS_CFM: |
| 107 | ret = -EINVAL; |
| 108 | break; |
| 109 | default: |
| 110 | printk(KERN_ERR "%s: Unknown DAI master type\n", __func__); |
| 111 | ret = -EINVAL; |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static int bf5xx_tdm_hw_params(struct snd_pcm_substream *substream, |
| 119 | struct snd_pcm_hw_params *params, |
| 120 | struct snd_soc_dai *dai) |
| 121 | { |
| 122 | int ret = 0; |
| 123 | |
| 124 | bf5xx_tdm.tcr2 &= ~0x1f; |
| 125 | bf5xx_tdm.rcr2 &= ~0x1f; |
| 126 | switch (params_format(params)) { |
| 127 | case SNDRV_PCM_FORMAT_S32_LE: |
| 128 | bf5xx_tdm.tcr2 |= 31; |
| 129 | bf5xx_tdm.rcr2 |= 31; |
| 130 | sport_handle->wdsize = 4; |
| 131 | break; |
| 132 | /* at present, we only support 32bit transfer */ |
| 133 | default: |
| 134 | pr_err("not supported PCM format yet\n"); |
| 135 | return -EINVAL; |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | if (!bf5xx_tdm.configured) { |
| 140 | /* |
| 141 | * TX and RX are not independent,they are enabled at the |
| 142 | * same time, even if only one side is running. So, we |
| 143 | * need to configure both of them at the time when the first |
| 144 | * stream is opened. |
| 145 | * |
| 146 | * CPU DAI:slave mode. |
| 147 | */ |
| 148 | ret = sport_config_rx(sport_handle, bf5xx_tdm.rcr1, |
| 149 | bf5xx_tdm.rcr2, 0, 0); |
| 150 | if (ret) { |
| 151 | pr_err("SPORT is busy!\n"); |
| 152 | return -EBUSY; |
| 153 | } |
| 154 | |
| 155 | ret = sport_config_tx(sport_handle, bf5xx_tdm.tcr1, |
| 156 | bf5xx_tdm.tcr2, 0, 0); |
| 157 | if (ret) { |
| 158 | pr_err("SPORT is busy!\n"); |
| 159 | return -EBUSY; |
| 160 | } |
| 161 | |
| 162 | bf5xx_tdm.configured = 1; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static void bf5xx_tdm_shutdown(struct snd_pcm_substream *substream, |
| 169 | struct snd_soc_dai *dai) |
| 170 | { |
| 171 | /* No active stream, SPORT is allowed to be configured again. */ |
| 172 | if (!dai->active) |
| 173 | bf5xx_tdm.configured = 0; |
| 174 | } |
| 175 | |
Barry Song | 08db48f | 2009-09-15 11:24:52 +0800 | [diff] [blame] | 176 | static int bf5xx_tdm_set_channel_map(struct snd_soc_dai *dai, |
| 177 | unsigned int tx_num, unsigned int *tx_slot, |
| 178 | unsigned int rx_num, unsigned int *rx_slot) |
| 179 | { |
| 180 | int i; |
| 181 | unsigned int slot; |
| 182 | unsigned int tx_mapped = 0, rx_mapped = 0; |
| 183 | |
| 184 | if ((tx_num > BFIN_TDM_DAI_MAX_SLOTS) || |
| 185 | (rx_num > BFIN_TDM_DAI_MAX_SLOTS)) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | for (i = 0; i < tx_num; i++) { |
| 189 | slot = tx_slot[i]; |
| 190 | if ((slot < BFIN_TDM_DAI_MAX_SLOTS) && |
| 191 | (!(tx_mapped & (1 << slot)))) { |
| 192 | bf5xx_tdm.tx_map[i] = slot; |
| 193 | tx_mapped |= 1 << slot; |
| 194 | } else |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | for (i = 0; i < rx_num; i++) { |
| 198 | slot = rx_slot[i]; |
| 199 | if ((slot < BFIN_TDM_DAI_MAX_SLOTS) && |
| 200 | (!(rx_mapped & (1 << slot)))) { |
| 201 | bf5xx_tdm.rx_map[i] = slot; |
| 202 | rx_mapped |= 1 << slot; |
| 203 | } else |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Barry Song | 01e2ab2 | 2009-07-27 18:06:39 +0800 | [diff] [blame] | 210 | #ifdef CONFIG_PM |
| 211 | static int bf5xx_tdm_suspend(struct snd_soc_dai *dai) |
| 212 | { |
| 213 | struct sport_device *sport = |
| 214 | (struct sport_device *)dai->private_data; |
| 215 | |
| 216 | if (!dai->active) |
| 217 | return 0; |
| 218 | if (dai->capture.active) |
| 219 | sport_rx_stop(sport); |
| 220 | if (dai->playback.active) |
| 221 | sport_tx_stop(sport); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int bf5xx_tdm_resume(struct snd_soc_dai *dai) |
| 226 | { |
| 227 | int ret; |
| 228 | struct sport_device *sport = |
| 229 | (struct sport_device *)dai->private_data; |
| 230 | |
| 231 | if (!dai->active) |
| 232 | return 0; |
| 233 | |
| 234 | ret = sport_set_multichannel(sport, 8, 0xFF, 1); |
| 235 | if (ret) { |
| 236 | pr_err("SPORT is busy!\n"); |
| 237 | ret = -EBUSY; |
| 238 | } |
| 239 | |
| 240 | ret = sport_config_rx(sport, IRFS, 0x1F, 0, 0); |
| 241 | if (ret) { |
| 242 | pr_err("SPORT is busy!\n"); |
| 243 | ret = -EBUSY; |
| 244 | } |
| 245 | |
| 246 | ret = sport_config_tx(sport, ITFS, 0x1F, 0, 0); |
| 247 | if (ret) { |
| 248 | pr_err("SPORT is busy!\n"); |
| 249 | ret = -EBUSY; |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | #else |
| 256 | #define bf5xx_tdm_suspend NULL |
| 257 | #define bf5xx_tdm_resume NULL |
| 258 | #endif |
| 259 | |
| 260 | static struct snd_soc_dai_ops bf5xx_tdm_dai_ops = { |
| 261 | .hw_params = bf5xx_tdm_hw_params, |
| 262 | .set_fmt = bf5xx_tdm_set_dai_fmt, |
| 263 | .shutdown = bf5xx_tdm_shutdown, |
Barry Song | 08db48f | 2009-09-15 11:24:52 +0800 | [diff] [blame] | 264 | .set_channel_map = bf5xx_tdm_set_channel_map, |
Barry Song | 01e2ab2 | 2009-07-27 18:06:39 +0800 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | struct snd_soc_dai bf5xx_tdm_dai = { |
| 268 | .name = "bf5xx-tdm", |
| 269 | .id = 0, |
| 270 | .suspend = bf5xx_tdm_suspend, |
| 271 | .resume = bf5xx_tdm_resume, |
| 272 | .playback = { |
| 273 | .channels_min = 2, |
| 274 | .channels_max = 8, |
| 275 | .rates = SNDRV_PCM_RATE_48000, |
| 276 | .formats = SNDRV_PCM_FMTBIT_S32_LE,}, |
| 277 | .capture = { |
| 278 | .channels_min = 2, |
| 279 | .channels_max = 8, |
| 280 | .rates = SNDRV_PCM_RATE_48000, |
| 281 | .formats = SNDRV_PCM_FMTBIT_S32_LE,}, |
| 282 | .ops = &bf5xx_tdm_dai_ops, |
| 283 | }; |
| 284 | EXPORT_SYMBOL_GPL(bf5xx_tdm_dai); |
| 285 | |
| 286 | static int __devinit bfin_tdm_probe(struct platform_device *pdev) |
| 287 | { |
| 288 | int ret = 0; |
| 289 | |
| 290 | if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) { |
| 291 | pr_err("Requesting Peripherals failed\n"); |
| 292 | return -EFAULT; |
| 293 | } |
| 294 | |
| 295 | /* request DMA for SPORT */ |
| 296 | sport_handle = sport_init(&sport_params[sport_num], 4, \ |
| 297 | 8 * sizeof(u32), NULL); |
| 298 | if (!sport_handle) { |
| 299 | peripheral_free_list(&sport_req[sport_num][0]); |
| 300 | return -ENODEV; |
| 301 | } |
| 302 | |
| 303 | /* SPORT works in TDM mode */ |
| 304 | ret = sport_set_multichannel(sport_handle, 8, 0xFF, 1); |
| 305 | if (ret) { |
| 306 | pr_err("SPORT is busy!\n"); |
| 307 | ret = -EBUSY; |
| 308 | goto sport_config_err; |
| 309 | } |
| 310 | |
| 311 | ret = sport_config_rx(sport_handle, IRFS, 0x1F, 0, 0); |
| 312 | if (ret) { |
| 313 | pr_err("SPORT is busy!\n"); |
| 314 | ret = -EBUSY; |
| 315 | goto sport_config_err; |
| 316 | } |
| 317 | |
| 318 | ret = sport_config_tx(sport_handle, ITFS, 0x1F, 0, 0); |
| 319 | if (ret) { |
| 320 | pr_err("SPORT is busy!\n"); |
| 321 | ret = -EBUSY; |
| 322 | goto sport_config_err; |
| 323 | } |
| 324 | |
| 325 | ret = snd_soc_register_dai(&bf5xx_tdm_dai); |
| 326 | if (ret) { |
| 327 | pr_err("Failed to register DAI: %d\n", ret); |
| 328 | goto sport_config_err; |
| 329 | } |
Barry Song | 08db48f | 2009-09-15 11:24:52 +0800 | [diff] [blame] | 330 | |
| 331 | sport_handle->private_data = &bf5xx_tdm; |
Barry Song | 01e2ab2 | 2009-07-27 18:06:39 +0800 | [diff] [blame] | 332 | return 0; |
| 333 | |
| 334 | sport_config_err: |
| 335 | peripheral_free_list(&sport_req[sport_num][0]); |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static int __devexit bfin_tdm_remove(struct platform_device *pdev) |
| 340 | { |
| 341 | peripheral_free_list(&sport_req[sport_num][0]); |
| 342 | snd_soc_unregister_dai(&bf5xx_tdm_dai); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static struct platform_driver bfin_tdm_driver = { |
| 348 | .probe = bfin_tdm_probe, |
| 349 | .remove = __devexit_p(bfin_tdm_remove), |
| 350 | .driver = { |
| 351 | .name = "bfin-tdm", |
| 352 | .owner = THIS_MODULE, |
| 353 | }, |
| 354 | }; |
| 355 | |
| 356 | static int __init bfin_tdm_init(void) |
| 357 | { |
| 358 | return platform_driver_register(&bfin_tdm_driver); |
| 359 | } |
| 360 | module_init(bfin_tdm_init); |
| 361 | |
| 362 | static void __exit bfin_tdm_exit(void) |
| 363 | { |
| 364 | platform_driver_unregister(&bfin_tdm_driver); |
| 365 | } |
| 366 | module_exit(bfin_tdm_exit); |
| 367 | |
| 368 | /* Module information */ |
| 369 | MODULE_AUTHOR("Barry Song"); |
| 370 | MODULE_DESCRIPTION("TDM driver for ADI Blackfin"); |
| 371 | MODULE_LICENSE("GPL"); |
| 372 | |