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 | * |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 6 | * Copyright 2007-2010 Freescale Semiconductor, Inc. |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public License |
| 9 | * version 2. This program is licensed "as is" without any warranty of any |
| 10 | * kind, whether express or implied. |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 14 | #include <linux/io.h> |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 20 | #include <linux/of_address.h> |
| 21 | #include <linux/of_irq.h> |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 22 | #include <linux/of_platform.h> |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 23 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 24 | #include <sound/core.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/pcm_params.h> |
| 27 | #include <sound/initval.h> |
| 28 | #include <sound/soc.h> |
| 29 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 30 | #include "fsl_ssi.h" |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 31 | #include "imx-pcm.h" |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 32 | |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 33 | #ifdef PPC |
| 34 | #define read_ssi(addr) in_be32(addr) |
| 35 | #define write_ssi(val, addr) out_be32(addr, val) |
| 36 | #define write_ssi_mask(addr, clear, set) clrsetbits_be32(addr, clear, set) |
| 37 | #elif defined ARM |
| 38 | #define read_ssi(addr) readl(addr) |
| 39 | #define write_ssi(val, addr) writel(val, addr) |
| 40 | /* |
| 41 | * FIXME: Proper locking should be added at write_ssi_mask caller level |
| 42 | * to ensure this register read/modify/write sequence is race free. |
| 43 | */ |
| 44 | static inline void write_ssi_mask(u32 __iomem *addr, u32 clear, u32 set) |
| 45 | { |
| 46 | u32 val = readl(addr); |
| 47 | val = (val & ~clear) | set; |
| 48 | writel(val, addr); |
| 49 | } |
| 50 | #endif |
| 51 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 52 | /** |
| 53 | * FSLSSI_I2S_RATES: sample rates supported by the I2S |
| 54 | * |
| 55 | * This driver currently only supports the SSI running in I2S slave mode, |
| 56 | * which means the codec determines the sample rate. Therefore, we tell |
| 57 | * ALSA that we support all rates and let the codec driver decide what rates |
| 58 | * are really supported. |
| 59 | */ |
| 60 | #define FSLSSI_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \ |
| 61 | SNDRV_PCM_RATE_CONTINUOUS) |
| 62 | |
| 63 | /** |
| 64 | * FSLSSI_I2S_FORMATS: audio formats supported by the SSI |
| 65 | * |
| 66 | * This driver currently only supports the SSI running in I2S slave mode. |
| 67 | * |
| 68 | * The SSI has a limitation in that the samples must be in the same byte |
| 69 | * order as the host CPU. This is because when multiple bytes are written |
| 70 | * to the STX register, the bytes and bits must be written in the same |
| 71 | * order. The STX is a shift register, so all the bits need to be aligned |
| 72 | * (bit-endianness must match byte-endianness). Processors typically write |
| 73 | * the bits within a byte in the same order that the bytes of a word are |
| 74 | * written in. So if the host CPU is big-endian, then only big-endian |
| 75 | * samples will be written to STX properly. |
| 76 | */ |
| 77 | #ifdef __BIG_ENDIAN |
| 78 | #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \ |
| 79 | SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \ |
| 80 | SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE) |
| 81 | #else |
| 82 | #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \ |
| 83 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 84 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE) |
| 85 | #endif |
| 86 | |
Timur Tabi | d5a908b | 2009-03-26 11:42:38 -0500 | [diff] [blame] | 87 | /* SIER bitflag of interrupts to enable */ |
| 88 | #define SIER_FLAGS (CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE | \ |
| 89 | CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN | \ |
| 90 | CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN | \ |
| 91 | CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE | \ |
| 92 | CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN) |
| 93 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 94 | /** |
| 95 | * fsl_ssi_private: per-SSI private data |
| 96 | * |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 97 | * @ssi: pointer to the SSI's registers |
| 98 | * @ssi_phys: physical address of the SSI registers |
| 99 | * @irq: IRQ of this SSI |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 100 | * @first_stream: pointer to the stream that was opened first |
| 101 | * @second_stream: pointer to second stream |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 102 | * @playback: the number of playback streams opened |
| 103 | * @capture: the number of capture streams opened |
| 104 | * @cpu_dai: the CPU DAI for this device |
| 105 | * @dev_attr: the sysfs device attribute structure |
| 106 | * @stats: SSI statistics |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 107 | * @name: name for this device |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 108 | */ |
| 109 | struct fsl_ssi_private { |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 110 | struct ccsr_ssi __iomem *ssi; |
| 111 | dma_addr_t ssi_phys; |
| 112 | unsigned int irq; |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 113 | struct snd_pcm_substream *first_stream; |
| 114 | struct snd_pcm_substream *second_stream; |
Timur Tabi | 8e9d869 | 2010-08-06 12:16:12 -0500 | [diff] [blame] | 115 | unsigned int fifo_depth; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 116 | struct snd_soc_dai_driver cpu_dai_drv; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 117 | struct device_attribute dev_attr; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 118 | struct platform_device *pdev; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 119 | |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 120 | bool new_binding; |
| 121 | bool ssi_on_imx; |
| 122 | struct platform_device *imx_pcm_pdev; |
| 123 | struct imx_pcm_dma_params dma_params_tx; |
| 124 | struct imx_pcm_dma_params dma_params_rx; |
| 125 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 126 | struct { |
| 127 | unsigned int rfrc; |
| 128 | unsigned int tfrc; |
| 129 | unsigned int cmdau; |
| 130 | unsigned int cmddu; |
| 131 | unsigned int rxt; |
| 132 | unsigned int rdr1; |
| 133 | unsigned int rdr0; |
| 134 | unsigned int tde1; |
| 135 | unsigned int tde0; |
| 136 | unsigned int roe1; |
| 137 | unsigned int roe0; |
| 138 | unsigned int tue1; |
| 139 | unsigned int tue0; |
| 140 | unsigned int tfs; |
| 141 | unsigned int rfs; |
| 142 | unsigned int tls; |
| 143 | unsigned int rls; |
| 144 | unsigned int rff1; |
| 145 | unsigned int rff0; |
| 146 | unsigned int tfe1; |
| 147 | unsigned int tfe0; |
| 148 | } stats; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 149 | |
| 150 | char name[1]; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * fsl_ssi_isr: SSI interrupt handler |
| 155 | * |
| 156 | * Although it's possible to use the interrupt handler to send and receive |
| 157 | * data to/from the SSI, we use the DMA instead. Programming is more |
| 158 | * complicated, but the performance is much better. |
| 159 | * |
| 160 | * This interrupt handler is used only to gather statistics. |
| 161 | * |
| 162 | * @irq: IRQ of the SSI device |
| 163 | * @dev_id: pointer to the ssi_private structure for this SSI device |
| 164 | */ |
| 165 | static irqreturn_t fsl_ssi_isr(int irq, void *dev_id) |
| 166 | { |
| 167 | struct fsl_ssi_private *ssi_private = dev_id; |
| 168 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 169 | irqreturn_t ret = IRQ_NONE; |
| 170 | __be32 sisr; |
| 171 | __be32 sisr2 = 0; |
| 172 | |
| 173 | /* We got an interrupt, so read the status register to see what we |
| 174 | were interrupted for. We mask it with the Interrupt Enable register |
| 175 | so that we only check for events that we're interested in. |
| 176 | */ |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 177 | sisr = read_ssi(&ssi->sisr) & SIER_FLAGS; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 178 | |
| 179 | if (sisr & CCSR_SSI_SISR_RFRC) { |
| 180 | ssi_private->stats.rfrc++; |
| 181 | sisr2 |= CCSR_SSI_SISR_RFRC; |
| 182 | ret = IRQ_HANDLED; |
| 183 | } |
| 184 | |
| 185 | if (sisr & CCSR_SSI_SISR_TFRC) { |
| 186 | ssi_private->stats.tfrc++; |
| 187 | sisr2 |= CCSR_SSI_SISR_TFRC; |
| 188 | ret = IRQ_HANDLED; |
| 189 | } |
| 190 | |
| 191 | if (sisr & CCSR_SSI_SISR_CMDAU) { |
| 192 | ssi_private->stats.cmdau++; |
| 193 | ret = IRQ_HANDLED; |
| 194 | } |
| 195 | |
| 196 | if (sisr & CCSR_SSI_SISR_CMDDU) { |
| 197 | ssi_private->stats.cmddu++; |
| 198 | ret = IRQ_HANDLED; |
| 199 | } |
| 200 | |
| 201 | if (sisr & CCSR_SSI_SISR_RXT) { |
| 202 | ssi_private->stats.rxt++; |
| 203 | ret = IRQ_HANDLED; |
| 204 | } |
| 205 | |
| 206 | if (sisr & CCSR_SSI_SISR_RDR1) { |
| 207 | ssi_private->stats.rdr1++; |
| 208 | ret = IRQ_HANDLED; |
| 209 | } |
| 210 | |
| 211 | if (sisr & CCSR_SSI_SISR_RDR0) { |
| 212 | ssi_private->stats.rdr0++; |
| 213 | ret = IRQ_HANDLED; |
| 214 | } |
| 215 | |
| 216 | if (sisr & CCSR_SSI_SISR_TDE1) { |
| 217 | ssi_private->stats.tde1++; |
| 218 | ret = IRQ_HANDLED; |
| 219 | } |
| 220 | |
| 221 | if (sisr & CCSR_SSI_SISR_TDE0) { |
| 222 | ssi_private->stats.tde0++; |
| 223 | ret = IRQ_HANDLED; |
| 224 | } |
| 225 | |
| 226 | if (sisr & CCSR_SSI_SISR_ROE1) { |
| 227 | ssi_private->stats.roe1++; |
| 228 | sisr2 |= CCSR_SSI_SISR_ROE1; |
| 229 | ret = IRQ_HANDLED; |
| 230 | } |
| 231 | |
| 232 | if (sisr & CCSR_SSI_SISR_ROE0) { |
| 233 | ssi_private->stats.roe0++; |
| 234 | sisr2 |= CCSR_SSI_SISR_ROE0; |
| 235 | ret = IRQ_HANDLED; |
| 236 | } |
| 237 | |
| 238 | if (sisr & CCSR_SSI_SISR_TUE1) { |
| 239 | ssi_private->stats.tue1++; |
| 240 | sisr2 |= CCSR_SSI_SISR_TUE1; |
| 241 | ret = IRQ_HANDLED; |
| 242 | } |
| 243 | |
| 244 | if (sisr & CCSR_SSI_SISR_TUE0) { |
| 245 | ssi_private->stats.tue0++; |
| 246 | sisr2 |= CCSR_SSI_SISR_TUE0; |
| 247 | ret = IRQ_HANDLED; |
| 248 | } |
| 249 | |
| 250 | if (sisr & CCSR_SSI_SISR_TFS) { |
| 251 | ssi_private->stats.tfs++; |
| 252 | ret = IRQ_HANDLED; |
| 253 | } |
| 254 | |
| 255 | if (sisr & CCSR_SSI_SISR_RFS) { |
| 256 | ssi_private->stats.rfs++; |
| 257 | ret = IRQ_HANDLED; |
| 258 | } |
| 259 | |
| 260 | if (sisr & CCSR_SSI_SISR_TLS) { |
| 261 | ssi_private->stats.tls++; |
| 262 | ret = IRQ_HANDLED; |
| 263 | } |
| 264 | |
| 265 | if (sisr & CCSR_SSI_SISR_RLS) { |
| 266 | ssi_private->stats.rls++; |
| 267 | ret = IRQ_HANDLED; |
| 268 | } |
| 269 | |
| 270 | if (sisr & CCSR_SSI_SISR_RFF1) { |
| 271 | ssi_private->stats.rff1++; |
| 272 | ret = IRQ_HANDLED; |
| 273 | } |
| 274 | |
| 275 | if (sisr & CCSR_SSI_SISR_RFF0) { |
| 276 | ssi_private->stats.rff0++; |
| 277 | ret = IRQ_HANDLED; |
| 278 | } |
| 279 | |
| 280 | if (sisr & CCSR_SSI_SISR_TFE1) { |
| 281 | ssi_private->stats.tfe1++; |
| 282 | ret = IRQ_HANDLED; |
| 283 | } |
| 284 | |
| 285 | if (sisr & CCSR_SSI_SISR_TFE0) { |
| 286 | ssi_private->stats.tfe0++; |
| 287 | ret = IRQ_HANDLED; |
| 288 | } |
| 289 | |
| 290 | /* Clear the bits that we set */ |
| 291 | if (sisr2) |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 292 | write_ssi(sisr2, &ssi->sisr); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * fsl_ssi_startup: create a new substream |
| 299 | * |
| 300 | * This is the first function called when a stream is opened. |
| 301 | * |
| 302 | * If this is the first stream open, then grab the IRQ and program most of |
| 303 | * the SSI registers. |
| 304 | */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 305 | static int fsl_ssi_startup(struct snd_pcm_substream *substream, |
| 306 | struct snd_soc_dai *dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 307 | { |
| 308 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 309 | struct fsl_ssi_private *ssi_private = |
| 310 | snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 311 | int synchronous = ssi_private->cpu_dai_drv.symmetric_rates; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * If this is the first stream opened, then request the IRQ |
| 315 | * and initialize the SSI registers. |
| 316 | */ |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 317 | if (!ssi_private->first_stream) { |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 318 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 319 | |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 320 | ssi_private->first_stream = substream; |
| 321 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 322 | /* |
| 323 | * Section 16.5 of the MPC8610 reference manual says that the |
| 324 | * SSI needs to be disabled before updating the registers we set |
| 325 | * here. |
| 326 | */ |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 327 | write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, 0); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * Program the SSI into I2S Slave Non-Network Synchronous mode. |
| 331 | * Also enable the transmit and receive FIFO. |
| 332 | * |
| 333 | * FIXME: Little-endian samples require a different shift dir |
| 334 | */ |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 335 | write_ssi_mask(&ssi->scr, |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame] | 336 | CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN, |
| 337 | CCSR_SSI_SCR_TFR_CLK_DIS | CCSR_SSI_SCR_I2S_MODE_SLAVE |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 338 | | (synchronous ? CCSR_SSI_SCR_SYN : 0)); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 339 | |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 340 | write_ssi(CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 341 | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS | |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 342 | CCSR_SSI_STCR_TSCKP, &ssi->stcr); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 343 | |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 344 | write_ssi(CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 345 | CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS | |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 346 | CCSR_SSI_SRCR_RSCKP, &ssi->srcr); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * The DC and PM bits are only used if the SSI is the clock |
| 350 | * master. |
| 351 | */ |
| 352 | |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 353 | /* Enable the interrupts and DMA requests */ |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 354 | write_ssi(SIER_FLAGS, &ssi->sier); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 355 | |
| 356 | /* |
| 357 | * Set the watermark for transmit FIFI 0 and receive FIFO 0. We |
Timur Tabi | 8e9d869 | 2010-08-06 12:16:12 -0500 | [diff] [blame] | 358 | * don't use FIFO 1. We program the transmit water to signal a |
| 359 | * DMA transfer if there are only two (or fewer) elements left |
| 360 | * in the FIFO. Two elements equals one frame (left channel, |
| 361 | * right channel). This value, however, depends on the depth of |
| 362 | * the transmit buffer. |
| 363 | * |
| 364 | * We program the receive FIFO to notify us if at least two |
| 365 | * elements (one frame) have been written to the FIFO. We could |
| 366 | * make this value larger (and maybe we should), but this way |
| 367 | * data will be written to memory as soon as it's available. |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 368 | */ |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 369 | write_ssi(CCSR_SSI_SFCSR_TFWM0(ssi_private->fifo_depth - 2) | |
| 370 | CCSR_SSI_SFCSR_RFWM0(ssi_private->fifo_depth - 2), |
| 371 | &ssi->sfcsr); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 372 | |
| 373 | /* |
| 374 | * We keep the SSI disabled because if we enable it, then the |
| 375 | * DMA controller will start. It's not supposed to start until |
| 376 | * the SCR.TE (or SCR.RE) bit is set, but it does anyway. The |
| 377 | * DMA controller will transfer one "BWC" of data (i.e. the |
| 378 | * amount of data that the MR.BWC bits are set to). The reason |
| 379 | * this is bad is because at this point, the PCM driver has not |
| 380 | * finished initializing the DMA controller. |
| 381 | */ |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 382 | } else { |
| 383 | if (synchronous) { |
| 384 | struct snd_pcm_runtime *first_runtime = |
| 385 | ssi_private->first_stream->runtime; |
| 386 | /* |
| 387 | * This is the second stream open, and we're in |
| 388 | * synchronous mode, so we need to impose sample |
| 389 | * sample size constraints. This is because STCCR is |
| 390 | * used for playback and capture in synchronous mode, |
| 391 | * so there's no way to specify different word |
| 392 | * lengths. |
| 393 | * |
| 394 | * Note that this can cause a race condition if the |
| 395 | * second stream is opened before the first stream is |
| 396 | * fully initialized. We provide some protection by |
| 397 | * checking to make sure the first stream is |
| 398 | * initialized, but it's not perfect. ALSA sometimes |
| 399 | * re-initializes the driver with a different sample |
| 400 | * rate or size. If the second stream is opened |
| 401 | * before the first stream has received its final |
| 402 | * parameters, then the second stream may be |
| 403 | * constrained to the wrong sample rate or size. |
| 404 | */ |
| 405 | if (!first_runtime->sample_bits) { |
| 406 | dev_err(substream->pcm->card->dev, |
| 407 | "set sample size in %s stream first\n", |
| 408 | substream->stream == |
| 409 | SNDRV_PCM_STREAM_PLAYBACK |
| 410 | ? "capture" : "playback"); |
| 411 | return -EAGAIN; |
| 412 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 413 | |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame] | 414 | snd_pcm_hw_constraint_minmax(substream->runtime, |
| 415 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, |
| 416 | first_runtime->sample_bits, |
| 417 | first_runtime->sample_bits); |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 418 | } |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 419 | |
| 420 | ssi_private->second_stream = substream; |
| 421 | } |
| 422 | |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 423 | if (ssi_private->ssi_on_imx) |
| 424 | snd_soc_dai_set_dma_data(dai, substream, |
| 425 | (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? |
| 426 | &ssi_private->dma_params_tx : |
| 427 | &ssi_private->dma_params_rx); |
| 428 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /** |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 433 | * fsl_ssi_hw_params - program the sample size |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 434 | * |
| 435 | * Most of the SSI registers have been programmed in the startup function, |
| 436 | * but the word length must be programmed here. Unfortunately, programming |
| 437 | * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can |
| 438 | * cause a problem with supporting simultaneous playback and capture. If |
| 439 | * the SSI is already playing a stream, then that stream may be temporarily |
| 440 | * stopped when you start capture. |
| 441 | * |
| 442 | * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the |
| 443 | * clock master. |
| 444 | */ |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 445 | static int fsl_ssi_hw_params(struct snd_pcm_substream *substream, |
| 446 | 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] | 447 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 448 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai); |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 449 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 450 | unsigned int sample_size = |
| 451 | snd_pcm_format_width(params_format(hw_params)); |
| 452 | u32 wl = CCSR_SSI_SxCCR_WL(sample_size); |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 453 | int enabled = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 454 | |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 455 | /* |
| 456 | * If we're in synchronous mode, and the SSI is already enabled, |
| 457 | * then STCCR is already set properly. |
| 458 | */ |
| 459 | if (enabled && ssi_private->cpu_dai_drv.symmetric_rates) |
| 460 | return 0; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 461 | |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 462 | /* |
| 463 | * FIXME: The documentation says that SxCCR[WL] should not be |
| 464 | * modified while the SSI is enabled. The only time this can |
| 465 | * happen is if we're trying to do simultaneous playback and |
| 466 | * capture in asynchronous mode. Unfortunately, I have been enable |
| 467 | * to get that to work at all on the P1022DS. Therefore, we don't |
| 468 | * bother to disable/enable the SSI when setting SxCCR[WL], because |
| 469 | * the SSI will stop anyway. Maybe one day, this will get fixed. |
| 470 | */ |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 471 | |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 472 | /* In synchronous mode, the SSI uses STCCR for capture */ |
| 473 | if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) || |
| 474 | ssi_private->cpu_dai_drv.symmetric_rates) |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 475 | write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_WL_MASK, wl); |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 476 | else |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 477 | write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_WL_MASK, wl); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * fsl_ssi_trigger: start and stop the DMA transfer. |
| 484 | * |
| 485 | * This function is called by ALSA to start, stop, pause, and resume the DMA |
| 486 | * transfer of data. |
| 487 | * |
| 488 | * The DMA channel is in external master start and pause mode, which |
| 489 | * means the SSI completely controls the flow of data. |
| 490 | */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 491 | static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd, |
| 492 | struct snd_soc_dai *dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 493 | { |
| 494 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 495 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 496 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 497 | |
| 498 | switch (cmd) { |
| 499 | case SNDRV_PCM_TRIGGER_START: |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 500 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Timur Tabi | a4d11fe | 2009-03-25 18:20:37 -0500 | [diff] [blame] | 501 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 502 | write_ssi_mask(&ssi->scr, 0, |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 503 | CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE); |
Timur Tabi | a4d11fe | 2009-03-25 18:20:37 -0500 | [diff] [blame] | 504 | else |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 505 | write_ssi_mask(&ssi->scr, 0, |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 506 | CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 507 | break; |
| 508 | |
| 509 | case SNDRV_PCM_TRIGGER_STOP: |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 510 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 511 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 512 | write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_TE, 0); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 513 | else |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 514 | write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_RE, 0); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 515 | break; |
| 516 | |
| 517 | default: |
| 518 | return -EINVAL; |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * fsl_ssi_shutdown: shutdown the SSI |
| 526 | * |
| 527 | * Shutdown the SSI if there are no other substreams open. |
| 528 | */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 529 | static void fsl_ssi_shutdown(struct snd_pcm_substream *substream, |
| 530 | struct snd_soc_dai *dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 531 | { |
| 532 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 533 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 534 | |
Timur Tabi | be41e94 | 2008-07-28 17:04:39 -0500 | [diff] [blame] | 535 | if (ssi_private->first_stream == substream) |
| 536 | ssi_private->first_stream = ssi_private->second_stream; |
| 537 | |
| 538 | ssi_private->second_stream = NULL; |
| 539 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 540 | /* |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 541 | * If this is the last active substream, disable the SSI. |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 542 | */ |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 543 | if (!ssi_private->first_stream) { |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 544 | struct ccsr_ssi __iomem *ssi = ssi_private->ssi; |
| 545 | |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 546 | write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, 0); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 550 | static const struct snd_soc_dai_ops fsl_ssi_dai_ops = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 551 | .startup = fsl_ssi_startup, |
| 552 | .hw_params = fsl_ssi_hw_params, |
| 553 | .shutdown = fsl_ssi_shutdown, |
| 554 | .trigger = fsl_ssi_trigger, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 555 | }; |
| 556 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 557 | /* Template for the CPU dai driver structure */ |
| 558 | static struct snd_soc_dai_driver fsl_ssi_dai_template = { |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 559 | .playback = { |
| 560 | /* The SSI does not support monaural audio. */ |
| 561 | .channels_min = 2, |
| 562 | .channels_max = 2, |
| 563 | .rates = FSLSSI_I2S_RATES, |
| 564 | .formats = FSLSSI_I2S_FORMATS, |
| 565 | }, |
| 566 | .capture = { |
| 567 | .channels_min = 2, |
| 568 | .channels_max = 2, |
| 569 | .rates = FSLSSI_I2S_RATES, |
| 570 | .formats = FSLSSI_I2S_FORMATS, |
| 571 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 572 | .ops = &fsl_ssi_dai_ops, |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 573 | }; |
| 574 | |
Timur Tabi | d5a908b | 2009-03-26 11:42:38 -0500 | [diff] [blame] | 575 | /* Show the statistics of a flag only if its interrupt is enabled. The |
| 576 | * compiler will optimze this code to a no-op if the interrupt is not |
| 577 | * enabled. |
| 578 | */ |
| 579 | #define SIER_SHOW(flag, name) \ |
| 580 | do { \ |
| 581 | if (SIER_FLAGS & CCSR_SSI_SIER_##flag) \ |
| 582 | length += sprintf(buf + length, #name "=%u\n", \ |
| 583 | ssi_private->stats.name); \ |
| 584 | } while (0) |
| 585 | |
| 586 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 587 | /** |
| 588 | * fsl_sysfs_ssi_show: display SSI statistics |
| 589 | * |
Timur Tabi | d5a908b | 2009-03-26 11:42:38 -0500 | [diff] [blame] | 590 | * Display the statistics for the current SSI device. To avoid confusion, |
| 591 | * we only show those counts that are enabled. |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 592 | */ |
| 593 | static ssize_t fsl_sysfs_ssi_show(struct device *dev, |
| 594 | struct device_attribute *attr, char *buf) |
| 595 | { |
| 596 | struct fsl_ssi_private *ssi_private = |
Timur Tabi | d5a908b | 2009-03-26 11:42:38 -0500 | [diff] [blame] | 597 | container_of(attr, struct fsl_ssi_private, dev_attr); |
| 598 | ssize_t length = 0; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 599 | |
Timur Tabi | d5a908b | 2009-03-26 11:42:38 -0500 | [diff] [blame] | 600 | SIER_SHOW(RFRC_EN, rfrc); |
| 601 | SIER_SHOW(TFRC_EN, tfrc); |
| 602 | SIER_SHOW(CMDAU_EN, cmdau); |
| 603 | SIER_SHOW(CMDDU_EN, cmddu); |
| 604 | SIER_SHOW(RXT_EN, rxt); |
| 605 | SIER_SHOW(RDR1_EN, rdr1); |
| 606 | SIER_SHOW(RDR0_EN, rdr0); |
| 607 | SIER_SHOW(TDE1_EN, tde1); |
| 608 | SIER_SHOW(TDE0_EN, tde0); |
| 609 | SIER_SHOW(ROE1_EN, roe1); |
| 610 | SIER_SHOW(ROE0_EN, roe0); |
| 611 | SIER_SHOW(TUE1_EN, tue1); |
| 612 | SIER_SHOW(TUE0_EN, tue0); |
| 613 | SIER_SHOW(TFS_EN, tfs); |
| 614 | SIER_SHOW(RFS_EN, rfs); |
| 615 | SIER_SHOW(TLS_EN, tls); |
| 616 | SIER_SHOW(RLS_EN, rls); |
| 617 | SIER_SHOW(RFF1_EN, rff1); |
| 618 | SIER_SHOW(RFF0_EN, rff0); |
| 619 | SIER_SHOW(TFE1_EN, tfe1); |
| 620 | SIER_SHOW(TFE0_EN, tfe0); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 621 | |
| 622 | return length; |
| 623 | } |
| 624 | |
| 625 | /** |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 626 | * Make every character in a string lower-case |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 627 | */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 628 | static void make_lowercase(char *s) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 629 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 630 | char *p = s; |
| 631 | char c; |
| 632 | |
| 633 | while ((c = *p)) { |
| 634 | if ((c >= 'A') && (c <= 'Z')) |
| 635 | *p = c + ('a' - 'A'); |
| 636 | p++; |
| 637 | } |
| 638 | } |
| 639 | |
Grant Likely | f07eb22 | 2011-02-22 21:05:04 -0700 | [diff] [blame] | 640 | static int __devinit fsl_ssi_probe(struct platform_device *pdev) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 641 | { |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 642 | struct fsl_ssi_private *ssi_private; |
| 643 | int ret = 0; |
Timur Tabi | 87a0632 | 2010-08-03 17:55:28 -0500 | [diff] [blame] | 644 | struct device_attribute *dev_attr = NULL; |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 645 | struct device_node *np = pdev->dev.of_node; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 646 | const char *p, *sprop; |
Timur Tabi | 8e9d869 | 2010-08-06 12:16:12 -0500 | [diff] [blame] | 647 | const uint32_t *iprop; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 648 | struct resource res; |
| 649 | char name[64]; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 650 | |
Timur Tabi | ff71334 | 2010-08-04 17:51:08 -0500 | [diff] [blame] | 651 | /* SSIs that are not connected on the board should have a |
| 652 | * status = "disabled" |
| 653 | * property in their device tree nodes. |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 654 | */ |
Timur Tabi | ff71334 | 2010-08-04 17:51:08 -0500 | [diff] [blame] | 655 | if (!of_device_is_available(np)) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 656 | return -ENODEV; |
| 657 | |
| 658 | /* We only support the SSI in "I2S Slave" mode */ |
| 659 | sprop = of_get_property(np, "fsl,mode", NULL); |
| 660 | if (!sprop || strcmp(sprop, "i2s-slave")) { |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 661 | dev_notice(&pdev->dev, "mode %s is unsupported\n", sprop); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 662 | return -ENODEV; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 663 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 664 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 665 | /* The DAI name is the last part of the full name of the node. */ |
| 666 | p = strrchr(np->full_name, '/') + 1; |
| 667 | ssi_private = kzalloc(sizeof(struct fsl_ssi_private) + strlen(p), |
| 668 | GFP_KERNEL); |
| 669 | if (!ssi_private) { |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 670 | dev_err(&pdev->dev, "could not allocate DAI object\n"); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 671 | return -ENOMEM; |
| 672 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 673 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 674 | strcpy(ssi_private->name, p); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 675 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 676 | /* Initialize this copy of the CPU DAI driver structure */ |
| 677 | memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template, |
| 678 | sizeof(fsl_ssi_dai_template)); |
| 679 | ssi_private->cpu_dai_drv.name = ssi_private->name; |
| 680 | |
| 681 | /* Get the addresses and IRQ */ |
| 682 | ret = of_address_to_resource(np, 0, &res); |
| 683 | if (ret) { |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 684 | dev_err(&pdev->dev, "could not determine device resources\n"); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 685 | goto error_kmalloc; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 686 | } |
Timur Tabi | 147dfe9 | 2011-06-08 15:02:55 -0500 | [diff] [blame] | 687 | ssi_private->ssi = of_iomap(np, 0); |
| 688 | if (!ssi_private->ssi) { |
| 689 | dev_err(&pdev->dev, "could not map device resources\n"); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 690 | ret = -ENOMEM; |
| 691 | goto error_kmalloc; |
Timur Tabi | 147dfe9 | 2011-06-08 15:02:55 -0500 | [diff] [blame] | 692 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 693 | ssi_private->ssi_phys = res.start; |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 694 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 695 | ssi_private->irq = irq_of_parse_and_map(np, 0); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 696 | if (ssi_private->irq == NO_IRQ) { |
| 697 | dev_err(&pdev->dev, "no irq for node %s\n", np->full_name); |
| 698 | ret = -ENXIO; |
| 699 | goto error_iomap; |
| 700 | } |
| 701 | |
| 702 | /* The 'name' should not have any slashes in it. */ |
| 703 | ret = request_irq(ssi_private->irq, fsl_ssi_isr, 0, ssi_private->name, |
| 704 | ssi_private); |
| 705 | if (ret < 0) { |
| 706 | dev_err(&pdev->dev, "could not claim irq %u\n", ssi_private->irq); |
| 707 | goto error_irqmap; |
| 708 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 709 | |
| 710 | /* Are the RX and the TX clocks locked? */ |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 711 | if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 712 | ssi_private->cpu_dai_drv.symmetric_rates = 1; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 713 | |
Timur Tabi | 8e9d869 | 2010-08-06 12:16:12 -0500 | [diff] [blame] | 714 | /* Determine the FIFO depth. */ |
| 715 | iprop = of_get_property(np, "fsl,fifo-depth", NULL); |
| 716 | if (iprop) |
Timur Tabi | 147dfe9 | 2011-06-08 15:02:55 -0500 | [diff] [blame] | 717 | ssi_private->fifo_depth = be32_to_cpup(iprop); |
Timur Tabi | 8e9d869 | 2010-08-06 12:16:12 -0500 | [diff] [blame] | 718 | else |
| 719 | /* Older 8610 DTs didn't have the fifo-depth property */ |
| 720 | ssi_private->fifo_depth = 8; |
| 721 | |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 722 | if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx21-ssi")) { |
| 723 | u32 dma_events[2]; |
| 724 | ssi_private->ssi_on_imx = true; |
| 725 | /* |
| 726 | * We have burstsize be "fifo_depth - 2" to match the SSI |
| 727 | * watermark setting in fsl_ssi_startup(). |
| 728 | */ |
| 729 | ssi_private->dma_params_tx.burstsize = |
| 730 | ssi_private->fifo_depth - 2; |
| 731 | ssi_private->dma_params_rx.burstsize = |
| 732 | ssi_private->fifo_depth - 2; |
| 733 | ssi_private->dma_params_tx.dma_addr = |
| 734 | ssi_private->ssi_phys + offsetof(struct ccsr_ssi, stx0); |
| 735 | ssi_private->dma_params_rx.dma_addr = |
| 736 | ssi_private->ssi_phys + offsetof(struct ccsr_ssi, srx0); |
| 737 | /* |
| 738 | * TODO: This is a temporary solution and should be changed |
| 739 | * to use generic DMA binding later when the helplers get in. |
| 740 | */ |
| 741 | ret = of_property_read_u32_array(pdev->dev.of_node, |
| 742 | "fsl,ssi-dma-events", dma_events, 2); |
| 743 | if (ret) { |
| 744 | dev_err(&pdev->dev, "could not get dma events\n"); |
| 745 | goto error_irq; |
| 746 | } |
| 747 | ssi_private->dma_params_tx.dma = dma_events[0]; |
| 748 | ssi_private->dma_params_rx.dma = dma_events[1]; |
| 749 | } |
| 750 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 751 | /* Initialize the the device_attribute structure */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 752 | dev_attr = &ssi_private->dev_attr; |
Timur Tabi | 0f768a7 | 2011-11-14 16:35:26 -0600 | [diff] [blame] | 753 | sysfs_attr_init(&dev_attr->attr); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 754 | dev_attr->attr.name = "statistics"; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 755 | dev_attr->attr.mode = S_IRUGO; |
| 756 | dev_attr->show = fsl_sysfs_ssi_show; |
| 757 | |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 758 | ret = device_create_file(&pdev->dev, dev_attr); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 759 | if (ret) { |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 760 | dev_err(&pdev->dev, "could not create sysfs %s file\n", |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 761 | ssi_private->dev_attr.attr.name); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 762 | goto error_irq; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 765 | /* Register with ASoC */ |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 766 | dev_set_drvdata(&pdev->dev, ssi_private); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 767 | |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 768 | ret = snd_soc_register_dai(&pdev->dev, &ssi_private->cpu_dai_drv); |
Timur Tabi | 87a0632 | 2010-08-03 17:55:28 -0500 | [diff] [blame] | 769 | if (ret) { |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 770 | dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 771 | goto error_dev; |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 772 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 773 | |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 774 | if (ssi_private->ssi_on_imx) { |
| 775 | ssi_private->imx_pcm_pdev = |
| 776 | platform_device_register_simple("imx-pcm-audio", |
| 777 | -1, NULL, 0); |
| 778 | if (IS_ERR(ssi_private->imx_pcm_pdev)) { |
| 779 | ret = PTR_ERR(ssi_private->imx_pcm_pdev); |
| 780 | goto error_dev; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * If codec-handle property is missing from SSI node, we assume |
| 786 | * that the machine driver uses new binding which does not require |
| 787 | * SSI driver to trigger machine driver's probe. |
| 788 | */ |
| 789 | if (!of_get_property(np, "codec-handle", NULL)) { |
| 790 | ssi_private->new_binding = true; |
| 791 | goto done; |
| 792 | } |
| 793 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 794 | /* Trigger the machine driver's probe function. The platform driver |
Shawn Guo | 2b81ec6 | 2012-03-09 00:59:46 +0800 | [diff] [blame] | 795 | * name of the machine driver is taken from /compatible property of the |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 796 | * device tree. We also pass the address of the CPU DAI driver |
| 797 | * structure. |
| 798 | */ |
Shawn Guo | 2b81ec6 | 2012-03-09 00:59:46 +0800 | [diff] [blame] | 799 | sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL); |
| 800 | /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 801 | p = strrchr(sprop, ','); |
| 802 | if (p) |
| 803 | sprop = p + 1; |
| 804 | snprintf(name, sizeof(name), "snd-soc-%s", sprop); |
| 805 | make_lowercase(name); |
| 806 | |
| 807 | ssi_private->pdev = |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 808 | platform_device_register_data(&pdev->dev, name, 0, NULL, 0); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 809 | if (IS_ERR(ssi_private->pdev)) { |
| 810 | ret = PTR_ERR(ssi_private->pdev); |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 811 | dev_err(&pdev->dev, "failed to register platform: %d\n", ret); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 812 | goto error_dai; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 815 | done: |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 816 | return 0; |
Timur Tabi | 87a0632 | 2010-08-03 17:55:28 -0500 | [diff] [blame] | 817 | |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 818 | error_dai: |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 819 | if (ssi_private->ssi_on_imx) |
| 820 | platform_device_unregister(ssi_private->imx_pcm_pdev); |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 821 | snd_soc_unregister_dai(&pdev->dev); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 822 | |
| 823 | error_dev: |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 824 | dev_set_drvdata(&pdev->dev, NULL); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 825 | device_remove_file(&pdev->dev, dev_attr); |
| 826 | |
| 827 | error_irq: |
| 828 | free_irq(ssi_private->irq, ssi_private); |
| 829 | |
| 830 | error_irqmap: |
Timur Tabi | 87a0632 | 2010-08-03 17:55:28 -0500 | [diff] [blame] | 831 | irq_dispose_mapping(ssi_private->irq); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 832 | |
| 833 | error_iomap: |
Timur Tabi | 87a0632 | 2010-08-03 17:55:28 -0500 | [diff] [blame] | 834 | iounmap(ssi_private->ssi); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 835 | |
| 836 | error_kmalloc: |
Timur Tabi | 87a0632 | 2010-08-03 17:55:28 -0500 | [diff] [blame] | 837 | kfree(ssi_private); |
| 838 | |
| 839 | return ret; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 840 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 841 | |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 842 | static int fsl_ssi_remove(struct platform_device *pdev) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 843 | { |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 844 | struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 845 | |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 846 | if (!ssi_private->new_binding) |
| 847 | platform_device_unregister(ssi_private->pdev); |
| 848 | if (ssi_private->ssi_on_imx) |
| 849 | platform_device_unregister(ssi_private->imx_pcm_pdev); |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 850 | snd_soc_unregister_dai(&pdev->dev); |
| 851 | device_remove_file(&pdev->dev, &ssi_private->dev_attr); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 852 | |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 853 | free_irq(ssi_private->irq, ssi_private); |
| 854 | irq_dispose_mapping(ssi_private->irq); |
| 855 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 856 | kfree(ssi_private); |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 857 | dev_set_drvdata(&pdev->dev, NULL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 858 | |
| 859 | return 0; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 860 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 861 | |
| 862 | static const struct of_device_id fsl_ssi_ids[] = { |
| 863 | { .compatible = "fsl,mpc8610-ssi", }, |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame^] | 864 | { .compatible = "fsl,imx21-ssi", }, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 865 | {} |
| 866 | }; |
| 867 | MODULE_DEVICE_TABLE(of, fsl_ssi_ids); |
| 868 | |
Grant Likely | f07eb22 | 2011-02-22 21:05:04 -0700 | [diff] [blame] | 869 | static struct platform_driver fsl_ssi_driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 870 | .driver = { |
| 871 | .name = "fsl-ssi-dai", |
| 872 | .owner = THIS_MODULE, |
| 873 | .of_match_table = fsl_ssi_ids, |
| 874 | }, |
| 875 | .probe = fsl_ssi_probe, |
| 876 | .remove = fsl_ssi_remove, |
| 877 | }; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 878 | |
Axel Lin | ba0a7e0 | 2011-11-25 10:10:55 +0800 | [diff] [blame] | 879 | module_platform_driver(fsl_ssi_driver); |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame] | 880 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 881 | MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); |
| 882 | MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver"); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 883 | MODULE_LICENSE("GPL v2"); |