Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver |
| 3 | * |
| 4 | * Author: Timur Tabi <timur@freescale.com> |
| 5 | * |
| 6 | * Copyright 2007-2008 Freescale Semiconductor, Inc. This file is licensed |
| 7 | * under the terms of the GNU General Public License version 2. This |
| 8 | * program is licensed "as is" without any warranty of any kind, whether |
| 9 | * express or implied. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/delay.h> |
| 17 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 18 | #include <sound/core.h> |
| 19 | #include <sound/pcm.h> |
| 20 | #include <sound/pcm_params.h> |
| 21 | #include <sound/initval.h> |
| 22 | #include <sound/soc.h> |
| 23 | |
| 24 | #include <asm/immap_86xx.h> |
| 25 | |
| 26 | #include "fsl_ssi.h" |
| 27 | |
| 28 | /** |
| 29 | * FSLSSI_I2S_RATES: sample rates supported by the I2S |
| 30 | * |
| 31 | * This driver currently only supports the SSI running in I2S slave mode, |
| 32 | * which means the codec determines the sample rate. Therefore, we tell |
| 33 | * ALSA that we support all rates and let the codec driver decide what rates |
| 34 | * are really supported. |
| 35 | */ |
| 36 | #define FSLSSI_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \ |
| 37 | SNDRV_PCM_RATE_CONTINUOUS) |
| 38 | |
| 39 | /** |
| 40 | * FSLSSI_I2S_FORMATS: audio formats supported by the SSI |
| 41 | * |
| 42 | * This driver currently only supports the SSI running in I2S slave mode. |
| 43 | * |
| 44 | * The SSI has a limitation in that the samples must be in the same byte |
| 45 | * order as the host CPU. This is because when multiple bytes are written |
| 46 | * to the STX register, the bytes and bits must be written in the same |
| 47 | * order. The STX is a shift register, so all the bits need to be aligned |
| 48 | * (bit-endianness must match byte-endianness). Processors typically write |
| 49 | * the bits within a byte in the same order that the bytes of a word are |
| 50 | * written in. So if the host CPU is big-endian, then only big-endian |
| 51 | * samples will be written to STX properly. |
| 52 | */ |
| 53 | #ifdef __BIG_ENDIAN |
| 54 | #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \ |
| 55 | SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \ |
| 56 | SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE) |
| 57 | #else |
| 58 | #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \ |
| 59 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 60 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE) |
| 61 | #endif |
| 62 | |
| 63 | /** |
| 64 | * fsl_ssi_private: per-SSI private data |
| 65 | * |
| 66 | * @name: short name for this device ("SSI0", "SSI1", etc) |
| 67 | * @ssi: pointer to the SSI's registers |
| 68 | * @ssi_phys: physical address of the SSI registers |
| 69 | * @irq: IRQ of this SSI |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 70 | * @first_stream: pointer to the stream that was opened first |
| 71 | * @second_stream: pointer to second stream |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 72 | * @dev: struct device pointer |
| 73 | * @playback: the number of playback streams opened |
| 74 | * @capture: the number of capture streams opened |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame^] | 75 | * @asynchronous: 0=synchronous mode, 1=asynchronous mode |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 76 | * @cpu_dai: the CPU DAI for this device |
| 77 | * @dev_attr: the sysfs device attribute structure |
| 78 | * @stats: SSI statistics |
| 79 | */ |
| 80 | struct fsl_ssi_private { |
| 81 | char name[8]; |
| 82 | struct ccsr_ssi __iomem *ssi; |
| 83 | dma_addr_t ssi_phys; |
| 84 | unsigned int irq; |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 85 | struct snd_pcm_substream *first_stream; |
| 86 | struct snd_pcm_substream *second_stream; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 87 | struct device *dev; |
| 88 | unsigned int playback; |
| 89 | unsigned int capture; |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame^] | 90 | int asynchronous; |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 91 | struct snd_soc_dai cpu_dai; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 92 | struct device_attribute dev_attr; |
| 93 | |
| 94 | struct { |
| 95 | unsigned int rfrc; |
| 96 | unsigned int tfrc; |
| 97 | unsigned int cmdau; |
| 98 | unsigned int cmddu; |
| 99 | unsigned int rxt; |
| 100 | unsigned int rdr1; |
| 101 | unsigned int rdr0; |
| 102 | unsigned int tde1; |
| 103 | unsigned int tde0; |
| 104 | unsigned int roe1; |
| 105 | unsigned int roe0; |
| 106 | unsigned int tue1; |
| 107 | unsigned int tue0; |
| 108 | unsigned int tfs; |
| 109 | unsigned int rfs; |
| 110 | unsigned int tls; |
| 111 | unsigned int rls; |
| 112 | unsigned int rff1; |
| 113 | unsigned int rff0; |
| 114 | unsigned int tfe1; |
| 115 | unsigned int tfe0; |
| 116 | } stats; |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * fsl_ssi_isr: SSI interrupt handler |
| 121 | * |
| 122 | * Although it's possible to use the interrupt handler to send and receive |
| 123 | * data to/from the SSI, we use the DMA instead. Programming is more |
| 124 | * complicated, but the performance is much better. |
| 125 | * |
| 126 | * This interrupt handler is used only to gather statistics. |
| 127 | * |
| 128 | * @irq: IRQ of the SSI device |
| 129 | * @dev_id: pointer to the ssi_private structure for this SSI device |
| 130 | */ |
| 131 | static irqreturn_t fsl_ssi_isr(int irq, void *dev_id) |
| 132 | { |
| 133 | struct fsl_ssi_private *ssi_private = dev_id; |
| 134 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 135 | irqreturn_t ret = IRQ_NONE; |
| 136 | __be32 sisr; |
| 137 | __be32 sisr2 = 0; |
| 138 | |
| 139 | /* We got an interrupt, so read the status register to see what we |
| 140 | were interrupted for. We mask it with the Interrupt Enable register |
| 141 | so that we only check for events that we're interested in. |
| 142 | */ |
| 143 | sisr = in_be32(&ssi->sisr) & in_be32(&ssi->sier); |
| 144 | |
| 145 | if (sisr & CCSR_SSI_SISR_RFRC) { |
| 146 | ssi_private->stats.rfrc++; |
| 147 | sisr2 |= CCSR_SSI_SISR_RFRC; |
| 148 | ret = IRQ_HANDLED; |
| 149 | } |
| 150 | |
| 151 | if (sisr & CCSR_SSI_SISR_TFRC) { |
| 152 | ssi_private->stats.tfrc++; |
| 153 | sisr2 |= CCSR_SSI_SISR_TFRC; |
| 154 | ret = IRQ_HANDLED; |
| 155 | } |
| 156 | |
| 157 | if (sisr & CCSR_SSI_SISR_CMDAU) { |
| 158 | ssi_private->stats.cmdau++; |
| 159 | ret = IRQ_HANDLED; |
| 160 | } |
| 161 | |
| 162 | if (sisr & CCSR_SSI_SISR_CMDDU) { |
| 163 | ssi_private->stats.cmddu++; |
| 164 | ret = IRQ_HANDLED; |
| 165 | } |
| 166 | |
| 167 | if (sisr & CCSR_SSI_SISR_RXT) { |
| 168 | ssi_private->stats.rxt++; |
| 169 | ret = IRQ_HANDLED; |
| 170 | } |
| 171 | |
| 172 | if (sisr & CCSR_SSI_SISR_RDR1) { |
| 173 | ssi_private->stats.rdr1++; |
| 174 | ret = IRQ_HANDLED; |
| 175 | } |
| 176 | |
| 177 | if (sisr & CCSR_SSI_SISR_RDR0) { |
| 178 | ssi_private->stats.rdr0++; |
| 179 | ret = IRQ_HANDLED; |
| 180 | } |
| 181 | |
| 182 | if (sisr & CCSR_SSI_SISR_TDE1) { |
| 183 | ssi_private->stats.tde1++; |
| 184 | ret = IRQ_HANDLED; |
| 185 | } |
| 186 | |
| 187 | if (sisr & CCSR_SSI_SISR_TDE0) { |
| 188 | ssi_private->stats.tde0++; |
| 189 | ret = IRQ_HANDLED; |
| 190 | } |
| 191 | |
| 192 | if (sisr & CCSR_SSI_SISR_ROE1) { |
| 193 | ssi_private->stats.roe1++; |
| 194 | sisr2 |= CCSR_SSI_SISR_ROE1; |
| 195 | ret = IRQ_HANDLED; |
| 196 | } |
| 197 | |
| 198 | if (sisr & CCSR_SSI_SISR_ROE0) { |
| 199 | ssi_private->stats.roe0++; |
| 200 | sisr2 |= CCSR_SSI_SISR_ROE0; |
| 201 | ret = IRQ_HANDLED; |
| 202 | } |
| 203 | |
| 204 | if (sisr & CCSR_SSI_SISR_TUE1) { |
| 205 | ssi_private->stats.tue1++; |
| 206 | sisr2 |= CCSR_SSI_SISR_TUE1; |
| 207 | ret = IRQ_HANDLED; |
| 208 | } |
| 209 | |
| 210 | if (sisr & CCSR_SSI_SISR_TUE0) { |
| 211 | ssi_private->stats.tue0++; |
| 212 | sisr2 |= CCSR_SSI_SISR_TUE0; |
| 213 | ret = IRQ_HANDLED; |
| 214 | } |
| 215 | |
| 216 | if (sisr & CCSR_SSI_SISR_TFS) { |
| 217 | ssi_private->stats.tfs++; |
| 218 | ret = IRQ_HANDLED; |
| 219 | } |
| 220 | |
| 221 | if (sisr & CCSR_SSI_SISR_RFS) { |
| 222 | ssi_private->stats.rfs++; |
| 223 | ret = IRQ_HANDLED; |
| 224 | } |
| 225 | |
| 226 | if (sisr & CCSR_SSI_SISR_TLS) { |
| 227 | ssi_private->stats.tls++; |
| 228 | ret = IRQ_HANDLED; |
| 229 | } |
| 230 | |
| 231 | if (sisr & CCSR_SSI_SISR_RLS) { |
| 232 | ssi_private->stats.rls++; |
| 233 | ret = IRQ_HANDLED; |
| 234 | } |
| 235 | |
| 236 | if (sisr & CCSR_SSI_SISR_RFF1) { |
| 237 | ssi_private->stats.rff1++; |
| 238 | ret = IRQ_HANDLED; |
| 239 | } |
| 240 | |
| 241 | if (sisr & CCSR_SSI_SISR_RFF0) { |
| 242 | ssi_private->stats.rff0++; |
| 243 | ret = IRQ_HANDLED; |
| 244 | } |
| 245 | |
| 246 | if (sisr & CCSR_SSI_SISR_TFE1) { |
| 247 | ssi_private->stats.tfe1++; |
| 248 | ret = IRQ_HANDLED; |
| 249 | } |
| 250 | |
| 251 | if (sisr & CCSR_SSI_SISR_TFE0) { |
| 252 | ssi_private->stats.tfe0++; |
| 253 | ret = IRQ_HANDLED; |
| 254 | } |
| 255 | |
| 256 | /* Clear the bits that we set */ |
| 257 | if (sisr2) |
| 258 | out_be32(&ssi->sisr, sisr2); |
| 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * fsl_ssi_startup: create a new substream |
| 265 | * |
| 266 | * This is the first function called when a stream is opened. |
| 267 | * |
| 268 | * If this is the first stream open, then grab the IRQ and program most of |
| 269 | * the SSI registers. |
| 270 | */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 271 | static int fsl_ssi_startup(struct snd_pcm_substream *substream, |
| 272 | struct snd_soc_dai *dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 273 | { |
| 274 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 275 | struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data; |
| 276 | |
| 277 | /* |
| 278 | * If this is the first stream opened, then request the IRQ |
| 279 | * and initialize the SSI registers. |
| 280 | */ |
| 281 | if (!ssi_private->playback && !ssi_private->capture) { |
| 282 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 283 | int ret; |
| 284 | |
| 285 | ret = request_irq(ssi_private->irq, fsl_ssi_isr, 0, |
| 286 | ssi_private->name, ssi_private); |
| 287 | if (ret < 0) { |
| 288 | dev_err(substream->pcm->card->dev, |
| 289 | "could not claim irq %u\n", ssi_private->irq); |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Section 16.5 of the MPC8610 reference manual says that the |
| 295 | * SSI needs to be disabled before updating the registers we set |
| 296 | * here. |
| 297 | */ |
| 298 | clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN); |
| 299 | |
| 300 | /* |
| 301 | * Program the SSI into I2S Slave Non-Network Synchronous mode. |
| 302 | * Also enable the transmit and receive FIFO. |
| 303 | * |
| 304 | * FIXME: Little-endian samples require a different shift dir |
| 305 | */ |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame^] | 306 | clrsetbits_be32(&ssi->scr, |
| 307 | CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN, |
| 308 | CCSR_SSI_SCR_TFR_CLK_DIS | CCSR_SSI_SCR_I2S_MODE_SLAVE |
| 309 | | (ssi_private->asynchronous ? 0 : CCSR_SSI_SCR_SYN)); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 310 | |
| 311 | out_be32(&ssi->stcr, |
| 312 | CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 | |
| 313 | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS | |
| 314 | CCSR_SSI_STCR_TSCKP); |
| 315 | |
| 316 | out_be32(&ssi->srcr, |
| 317 | CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 | |
| 318 | CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS | |
| 319 | CCSR_SSI_SRCR_RSCKP); |
| 320 | |
| 321 | /* |
| 322 | * The DC and PM bits are only used if the SSI is the clock |
| 323 | * master. |
| 324 | */ |
| 325 | |
| 326 | /* 4. Enable the interrupts and DMA requests */ |
| 327 | out_be32(&ssi->sier, |
| 328 | CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE | |
| 329 | CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN | |
| 330 | CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN | |
| 331 | CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE | |
| 332 | CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN); |
| 333 | |
| 334 | /* |
| 335 | * Set the watermark for transmit FIFI 0 and receive FIFO 0. We |
| 336 | * don't use FIFO 1. Since the SSI only supports stereo, the |
| 337 | * watermark should never be an odd number. |
| 338 | */ |
| 339 | out_be32(&ssi->sfcsr, |
| 340 | CCSR_SSI_SFCSR_TFWM0(6) | CCSR_SSI_SFCSR_RFWM0(2)); |
| 341 | |
| 342 | /* |
| 343 | * We keep the SSI disabled because if we enable it, then the |
| 344 | * DMA controller will start. It's not supposed to start until |
| 345 | * the SCR.TE (or SCR.RE) bit is set, but it does anyway. The |
| 346 | * DMA controller will transfer one "BWC" of data (i.e. the |
| 347 | * amount of data that the MR.BWC bits are set to). The reason |
| 348 | * this is bad is because at this point, the PCM driver has not |
| 349 | * finished initializing the DMA controller. |
| 350 | */ |
| 351 | } |
| 352 | |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 353 | if (!ssi_private->first_stream) |
| 354 | ssi_private->first_stream = substream; |
| 355 | else { |
| 356 | /* This is the second stream open, so we need to impose sample |
| 357 | * rate and maybe sample size constraints. Note that this can |
| 358 | * cause a race condition if the second stream is opened before |
| 359 | * the first stream is fully initialized. |
| 360 | * |
| 361 | * We provide some protection by checking to make sure the first |
| 362 | * stream is initialized, but it's not perfect. ALSA sometimes |
| 363 | * re-initializes the driver with a different sample rate or |
| 364 | * size. If the second stream is opened before the first stream |
| 365 | * has received its final parameters, then the second stream may |
| 366 | * be constrained to the wrong sample rate or size. |
| 367 | * |
| 368 | * FIXME: This code does not handle opening and closing streams |
| 369 | * repeatedly. If you open two streams and then close the first |
| 370 | * one, you may not be able to open another stream until you |
| 371 | * close the second one as well. |
| 372 | */ |
| 373 | struct snd_pcm_runtime *first_runtime = |
| 374 | ssi_private->first_stream->runtime; |
| 375 | |
| 376 | if (!first_runtime->rate || !first_runtime->sample_bits) { |
| 377 | dev_err(substream->pcm->card->dev, |
| 378 | "set sample rate and size in %s stream first\n", |
| 379 | substream->stream == SNDRV_PCM_STREAM_PLAYBACK |
| 380 | ? "capture" : "playback"); |
| 381 | return -EAGAIN; |
| 382 | } |
| 383 | |
| 384 | snd_pcm_hw_constraint_minmax(substream->runtime, |
| 385 | SNDRV_PCM_HW_PARAM_RATE, |
| 386 | first_runtime->rate, first_runtime->rate); |
| 387 | |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame^] | 388 | /* If we're in synchronous mode, then we need to constrain |
| 389 | * the sample size as well. We don't support independent sample |
| 390 | * rates in asynchronous mode. |
| 391 | */ |
| 392 | if (!ssi_private->asynchronous) |
| 393 | snd_pcm_hw_constraint_minmax(substream->runtime, |
| 394 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 395 | first_runtime->sample_bits, |
| 396 | first_runtime->sample_bits); |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 397 | |
| 398 | ssi_private->second_stream = substream; |
| 399 | } |
| 400 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 401 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 402 | ssi_private->playback++; |
| 403 | |
| 404 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 405 | ssi_private->capture++; |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /** |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 411 | * fsl_ssi_hw_params - program the sample size |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 412 | * |
| 413 | * Most of the SSI registers have been programmed in the startup function, |
| 414 | * but the word length must be programmed here. Unfortunately, programming |
| 415 | * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can |
| 416 | * cause a problem with supporting simultaneous playback and capture. If |
| 417 | * the SSI is already playing a stream, then that stream may be temporarily |
| 418 | * stopped when you start capture. |
| 419 | * |
| 420 | * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the |
| 421 | * clock master. |
| 422 | */ |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 423 | static int fsl_ssi_hw_params(struct snd_pcm_substream *substream, |
| 424 | struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 425 | { |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 426 | struct fsl_ssi_private *ssi_private = cpu_dai->private_data; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 427 | |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 428 | if (substream == ssi_private->first_stream) { |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 429 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 430 | unsigned int sample_size = |
| 431 | snd_pcm_format_width(params_format(hw_params)); |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame^] | 432 | u32 wl = CCSR_SSI_SxCCR_WL(sample_size); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 433 | |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 434 | /* The SSI should always be disabled at this points (SSIEN=0) */ |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 435 | |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 436 | /* In synchronous mode, the SSI uses STCCR for capture */ |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame^] | 437 | if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) || |
| 438 | !ssi_private->asynchronous) |
| 439 | clrsetbits_be32(&ssi->stccr, |
| 440 | CCSR_SSI_SxCCR_WL_MASK, wl); |
| 441 | else |
| 442 | clrsetbits_be32(&ssi->srccr, |
| 443 | CCSR_SSI_SxCCR_WL_MASK, wl); |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 444 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * fsl_ssi_trigger: start and stop the DMA transfer. |
| 451 | * |
| 452 | * This function is called by ALSA to start, stop, pause, and resume the DMA |
| 453 | * transfer of data. |
| 454 | * |
| 455 | * The DMA channel is in external master start and pause mode, which |
| 456 | * means the SSI completely controls the flow of data. |
| 457 | */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 458 | static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd, |
| 459 | struct snd_soc_dai *dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 460 | { |
| 461 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 462 | struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data; |
| 463 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 464 | |
| 465 | switch (cmd) { |
| 466 | case SNDRV_PCM_TRIGGER_START: |
| 467 | case SNDRV_PCM_TRIGGER_RESUME: |
| 468 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 469 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 470 | clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN); |
| 471 | setbits32(&ssi->scr, |
| 472 | CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 473 | } else { |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 474 | clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN); |
| 475 | setbits32(&ssi->scr, |
| 476 | CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * I think we need this delay to allow time for the SSI |
| 480 | * to put data into its FIFO. Without it, ALSA starts |
| 481 | * to complain about overruns. |
| 482 | */ |
Anton Vorontsov | 3a3bd96 | 2008-05-09 13:43:55 +0200 | [diff] [blame] | 483 | mdelay(1); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 484 | } |
| 485 | break; |
| 486 | |
| 487 | case SNDRV_PCM_TRIGGER_STOP: |
| 488 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 489 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 490 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 491 | clrbits32(&ssi->scr, CCSR_SSI_SCR_TE); |
| 492 | else |
| 493 | clrbits32(&ssi->scr, CCSR_SSI_SCR_RE); |
| 494 | break; |
| 495 | |
| 496 | default: |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * fsl_ssi_shutdown: shutdown the SSI |
| 505 | * |
| 506 | * Shutdown the SSI if there are no other substreams open. |
| 507 | */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 508 | static void fsl_ssi_shutdown(struct snd_pcm_substream *substream, |
| 509 | struct snd_soc_dai *dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 510 | { |
| 511 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 512 | struct fsl_ssi_private *ssi_private = rtd->dai->cpu_dai->private_data; |
| 513 | |
| 514 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 515 | ssi_private->playback--; |
| 516 | |
| 517 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 518 | ssi_private->capture--; |
| 519 | |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 520 | if (ssi_private->first_stream == substream) |
| 521 | ssi_private->first_stream = ssi_private->second_stream; |
| 522 | |
| 523 | ssi_private->second_stream = NULL; |
| 524 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 525 | /* |
| 526 | * If this is the last active substream, disable the SSI and release |
| 527 | * the IRQ. |
| 528 | */ |
| 529 | if (!ssi_private->playback && !ssi_private->capture) { |
| 530 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 531 | |
| 532 | clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN); |
| 533 | |
| 534 | free_irq(ssi_private->irq, ssi_private); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * fsl_ssi_set_sysclk: set the clock frequency and direction |
| 540 | * |
| 541 | * This function is called by the machine driver to tell us what the clock |
| 542 | * frequency and direction are. |
| 543 | * |
| 544 | * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN), |
| 545 | * and we don't care about the frequency. Return an error if the direction |
| 546 | * is not SND_SOC_CLOCK_IN. |
| 547 | * |
| 548 | * @clk_id: reserved, should be zero |
| 549 | * @freq: the frequency of the given clock ID, currently ignored |
| 550 | * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master) |
| 551 | */ |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 552 | static int fsl_ssi_set_sysclk(struct snd_soc_dai *cpu_dai, |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 553 | int clk_id, unsigned int freq, int dir) |
| 554 | { |
| 555 | |
| 556 | return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * fsl_ssi_set_fmt: set the serial format. |
| 561 | * |
| 562 | * This function is called by the machine driver to tell us what serial |
| 563 | * format to use. |
| 564 | * |
| 565 | * Currently, we only support I2S mode. Return an error if the format is |
| 566 | * not SND_SOC_DAIFMT_I2S. |
| 567 | * |
| 568 | * @format: one of SND_SOC_DAIFMT_xxx |
| 569 | */ |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 570 | static int fsl_ssi_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 571 | { |
| 572 | return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * fsl_ssi_dai_template: template CPU DAI for the SSI |
| 577 | */ |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 578 | static struct snd_soc_dai fsl_ssi_dai_template = { |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 579 | .playback = { |
| 580 | /* The SSI does not support monaural audio. */ |
| 581 | .channels_min = 2, |
| 582 | .channels_max = 2, |
| 583 | .rates = FSLSSI_I2S_RATES, |
| 584 | .formats = FSLSSI_I2S_FORMATS, |
| 585 | }, |
| 586 | .capture = { |
| 587 | .channels_min = 2, |
| 588 | .channels_max = 2, |
| 589 | .rates = FSLSSI_I2S_RATES, |
| 590 | .formats = FSLSSI_I2S_FORMATS, |
| 591 | }, |
| 592 | .ops = { |
| 593 | .startup = fsl_ssi_startup, |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 594 | .hw_params = fsl_ssi_hw_params, |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 595 | .shutdown = fsl_ssi_shutdown, |
| 596 | .trigger = fsl_ssi_trigger, |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 597 | .set_sysclk = fsl_ssi_set_sysclk, |
| 598 | .set_fmt = fsl_ssi_set_fmt, |
| 599 | }, |
| 600 | }; |
| 601 | |
| 602 | /** |
| 603 | * fsl_sysfs_ssi_show: display SSI statistics |
| 604 | * |
| 605 | * Display the statistics for the current SSI device. |
| 606 | */ |
| 607 | static ssize_t fsl_sysfs_ssi_show(struct device *dev, |
| 608 | struct device_attribute *attr, char *buf) |
| 609 | { |
| 610 | struct fsl_ssi_private *ssi_private = |
| 611 | container_of(attr, struct fsl_ssi_private, dev_attr); |
| 612 | ssize_t length; |
| 613 | |
| 614 | length = sprintf(buf, "rfrc=%u", ssi_private->stats.rfrc); |
| 615 | length += sprintf(buf + length, "\ttfrc=%u", ssi_private->stats.tfrc); |
| 616 | length += sprintf(buf + length, "\tcmdau=%u", ssi_private->stats.cmdau); |
| 617 | length += sprintf(buf + length, "\tcmddu=%u", ssi_private->stats.cmddu); |
| 618 | length += sprintf(buf + length, "\trxt=%u", ssi_private->stats.rxt); |
| 619 | length += sprintf(buf + length, "\trdr1=%u", ssi_private->stats.rdr1); |
| 620 | length += sprintf(buf + length, "\trdr0=%u", ssi_private->stats.rdr0); |
| 621 | length += sprintf(buf + length, "\ttde1=%u", ssi_private->stats.tde1); |
| 622 | length += sprintf(buf + length, "\ttde0=%u", ssi_private->stats.tde0); |
| 623 | length += sprintf(buf + length, "\troe1=%u", ssi_private->stats.roe1); |
| 624 | length += sprintf(buf + length, "\troe0=%u", ssi_private->stats.roe0); |
| 625 | length += sprintf(buf + length, "\ttue1=%u", ssi_private->stats.tue1); |
| 626 | length += sprintf(buf + length, "\ttue0=%u", ssi_private->stats.tue0); |
| 627 | length += sprintf(buf + length, "\ttfs=%u", ssi_private->stats.tfs); |
| 628 | length += sprintf(buf + length, "\trfs=%u", ssi_private->stats.rfs); |
| 629 | length += sprintf(buf + length, "\ttls=%u", ssi_private->stats.tls); |
| 630 | length += sprintf(buf + length, "\trls=%u", ssi_private->stats.rls); |
| 631 | length += sprintf(buf + length, "\trff1=%u", ssi_private->stats.rff1); |
| 632 | length += sprintf(buf + length, "\trff0=%u", ssi_private->stats.rff0); |
| 633 | length += sprintf(buf + length, "\ttfe1=%u", ssi_private->stats.tfe1); |
| 634 | length += sprintf(buf + length, "\ttfe0=%u\n", ssi_private->stats.tfe0); |
| 635 | |
| 636 | return length; |
| 637 | } |
| 638 | |
| 639 | /** |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 640 | * fsl_ssi_create_dai: create a snd_soc_dai structure |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 641 | * |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 642 | * This function is called by the machine driver to create a snd_soc_dai |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 643 | * structure. The function creates an ssi_private object, which contains |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 644 | * the snd_soc_dai. It also creates the sysfs statistics device. |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 645 | */ |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 646 | struct snd_soc_dai *fsl_ssi_create_dai(struct fsl_ssi_info *ssi_info) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 647 | { |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 648 | struct snd_soc_dai *fsl_ssi_dai; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 649 | struct fsl_ssi_private *ssi_private; |
| 650 | int ret = 0; |
| 651 | struct device_attribute *dev_attr; |
| 652 | |
| 653 | ssi_private = kzalloc(sizeof(struct fsl_ssi_private), GFP_KERNEL); |
| 654 | if (!ssi_private) { |
| 655 | dev_err(ssi_info->dev, "could not allocate DAI object\n"); |
| 656 | return NULL; |
| 657 | } |
| 658 | memcpy(&ssi_private->cpu_dai, &fsl_ssi_dai_template, |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 659 | sizeof(struct snd_soc_dai)); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 660 | |
| 661 | fsl_ssi_dai = &ssi_private->cpu_dai; |
| 662 | dev_attr = &ssi_private->dev_attr; |
| 663 | |
| 664 | sprintf(ssi_private->name, "ssi%u", (u8) ssi_info->id); |
| 665 | ssi_private->ssi = ssi_info->ssi; |
| 666 | ssi_private->ssi_phys = ssi_info->ssi_phys; |
| 667 | ssi_private->irq = ssi_info->irq; |
| 668 | ssi_private->dev = ssi_info->dev; |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame^] | 669 | ssi_private->asynchronous = ssi_info->asynchronous; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 670 | |
| 671 | ssi_private->dev->driver_data = fsl_ssi_dai; |
| 672 | |
| 673 | /* Initialize the the device_attribute structure */ |
| 674 | dev_attr->attr.name = "ssi-stats"; |
| 675 | dev_attr->attr.mode = S_IRUGO; |
| 676 | dev_attr->show = fsl_sysfs_ssi_show; |
| 677 | |
| 678 | ret = device_create_file(ssi_private->dev, dev_attr); |
| 679 | if (ret) { |
| 680 | dev_err(ssi_info->dev, "could not create sysfs %s file\n", |
| 681 | ssi_private->dev_attr.attr.name); |
| 682 | kfree(fsl_ssi_dai); |
| 683 | return NULL; |
| 684 | } |
| 685 | |
| 686 | fsl_ssi_dai->private_data = ssi_private; |
| 687 | fsl_ssi_dai->name = ssi_private->name; |
| 688 | fsl_ssi_dai->id = ssi_info->id; |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 689 | fsl_ssi_dai->dev = ssi_info->dev; |
| 690 | |
| 691 | ret = snd_soc_register_dai(fsl_ssi_dai); |
| 692 | if (ret != 0) { |
| 693 | dev_err(ssi_info->dev, "failed to register DAI: %d\n", ret); |
| 694 | kfree(fsl_ssi_dai); |
| 695 | return NULL; |
| 696 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 697 | |
| 698 | return fsl_ssi_dai; |
| 699 | } |
| 700 | EXPORT_SYMBOL_GPL(fsl_ssi_create_dai); |
| 701 | |
| 702 | /** |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 703 | * fsl_ssi_destroy_dai: destroy the snd_soc_dai object |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 704 | * |
| 705 | * This function undoes the operations of fsl_ssi_create_dai() |
| 706 | */ |
Liam Girdwood | 8cf7b2b | 2008-07-07 16:08:00 +0100 | [diff] [blame] | 707 | void fsl_ssi_destroy_dai(struct snd_soc_dai *fsl_ssi_dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 708 | { |
| 709 | struct fsl_ssi_private *ssi_private = |
| 710 | container_of(fsl_ssi_dai, struct fsl_ssi_private, cpu_dai); |
| 711 | |
| 712 | device_remove_file(ssi_private->dev, &ssi_private->dev_attr); |
| 713 | |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 714 | snd_soc_unregister_dai(&ssi_private->cpu_dai); |
| 715 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 716 | kfree(ssi_private); |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(fsl_ssi_destroy_dai); |
| 719 | |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame^] | 720 | static int __init fsl_ssi_init(void) |
| 721 | { |
| 722 | printk(KERN_INFO "Freescale Synchronous Serial Interface (SSI) ASoC Driver\n"); |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | module_init(fsl_ssi_init); |
| 727 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 728 | MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); |
| 729 | MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver"); |
| 730 | MODULE_LICENSE("GPL"); |