Grant Likely | dc64137 | 2008-07-29 11:42:30 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Freescale MPC5200 PSC in I2S mode |
| 3 | * ALSA SoC Digital Audio Interface (DAI) driver |
| 4 | * |
| 5 | * Copyright (C) 2008 Secret Lab Technologies Ltd. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/of_device.h> |
| 14 | #include <linux/of_platform.h> |
| 15 | #include <linux/dma-mapping.h> |
| 16 | |
| 17 | #include <sound/core.h> |
| 18 | #include <sound/pcm.h> |
| 19 | #include <sound/pcm_params.h> |
| 20 | #include <sound/initval.h> |
| 21 | #include <sound/soc.h> |
| 22 | #include <sound/soc-of-simple.h> |
| 23 | |
| 24 | #include <sysdev/bestcomm/bestcomm.h> |
| 25 | #include <sysdev/bestcomm/gen_bd.h> |
| 26 | #include <asm/mpc52xx_psc.h> |
| 27 | |
| 28 | MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); |
| 29 | MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver"); |
| 30 | MODULE_LICENSE("GPL"); |
| 31 | |
| 32 | /** |
| 33 | * PSC_I2S_RATES: sample rates supported by the I2S |
| 34 | * |
| 35 | * This driver currently only supports the PSC running in I2S slave mode, |
| 36 | * which means the codec determines the sample rate. Therefore, we tell |
| 37 | * ALSA that we support all rates and let the codec driver decide what rates |
| 38 | * are really supported. |
| 39 | */ |
| 40 | #define PSC_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \ |
| 41 | SNDRV_PCM_RATE_CONTINUOUS) |
| 42 | |
| 43 | /** |
| 44 | * PSC_I2S_FORMATS: audio formats supported by the PSC I2S mode |
| 45 | */ |
| 46 | #define PSC_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \ |
| 47 | SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE | \ |
| 48 | SNDRV_PCM_FMTBIT_S32_BE) |
| 49 | |
| 50 | /** |
| 51 | * psc_i2s_stream - Data specific to a single stream (playback or capture) |
| 52 | * @active: flag indicating if the stream is active |
| 53 | * @psc_i2s: pointer back to parent psc_i2s data structure |
| 54 | * @bcom_task: bestcomm task structure |
| 55 | * @irq: irq number for bestcomm task |
| 56 | * @period_start: physical address of start of DMA region |
| 57 | * @period_end: physical address of end of DMA region |
| 58 | * @period_next_pt: physical address of next DMA buffer to enqueue |
| 59 | * @period_bytes: size of DMA period in bytes |
| 60 | */ |
| 61 | struct psc_i2s_stream { |
| 62 | int active; |
| 63 | struct psc_i2s *psc_i2s; |
| 64 | struct bcom_task *bcom_task; |
| 65 | int irq; |
| 66 | struct snd_pcm_substream *stream; |
| 67 | dma_addr_t period_start; |
| 68 | dma_addr_t period_end; |
| 69 | dma_addr_t period_next_pt; |
| 70 | dma_addr_t period_current_pt; |
| 71 | int period_bytes; |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * psc_i2s - Private driver data |
| 76 | * @name: short name for this device ("PSC0", "PSC1", etc) |
| 77 | * @psc_regs: pointer to the PSC's registers |
| 78 | * @fifo_regs: pointer to the PSC's FIFO registers |
| 79 | * @irq: IRQ of this PSC |
| 80 | * @dev: struct device pointer |
| 81 | * @dai: the CPU DAI for this device |
| 82 | * @sicr: Base value used in serial interface control register; mode is ORed |
| 83 | * with this value. |
| 84 | * @playback: Playback stream context data |
| 85 | * @capture: Capture stream context data |
| 86 | */ |
| 87 | struct psc_i2s { |
| 88 | char name[32]; |
| 89 | struct mpc52xx_psc __iomem *psc_regs; |
| 90 | struct mpc52xx_psc_fifo __iomem *fifo_regs; |
| 91 | unsigned int irq; |
| 92 | struct device *dev; |
| 93 | struct snd_soc_dai dai; |
| 94 | spinlock_t lock; |
| 95 | u32 sicr; |
| 96 | |
| 97 | /* per-stream data */ |
| 98 | struct psc_i2s_stream playback; |
| 99 | struct psc_i2s_stream capture; |
| 100 | |
| 101 | /* Statistics */ |
| 102 | struct { |
| 103 | int overrun_count; |
| 104 | int underrun_count; |
| 105 | } stats; |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * Interrupt handlers |
| 110 | */ |
| 111 | static irqreturn_t psc_i2s_status_irq(int irq, void *_psc_i2s) |
| 112 | { |
| 113 | struct psc_i2s *psc_i2s = _psc_i2s; |
| 114 | struct mpc52xx_psc __iomem *regs = psc_i2s->psc_regs; |
| 115 | u16 isr; |
| 116 | |
| 117 | isr = in_be16(®s->mpc52xx_psc_isr); |
| 118 | |
| 119 | /* Playback underrun error */ |
| 120 | if (psc_i2s->playback.active && (isr & MPC52xx_PSC_IMR_TXEMP)) |
| 121 | psc_i2s->stats.underrun_count++; |
| 122 | |
| 123 | /* Capture overrun error */ |
| 124 | if (psc_i2s->capture.active && (isr & MPC52xx_PSC_IMR_ORERR)) |
| 125 | psc_i2s->stats.overrun_count++; |
| 126 | |
| 127 | out_8(®s->command, 4 << 4); /* reset the error status */ |
| 128 | |
| 129 | return IRQ_HANDLED; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * psc_i2s_bcom_enqueue_next_buffer - Enqueue another audio buffer |
| 134 | * @s: pointer to stream private data structure |
| 135 | * |
| 136 | * Enqueues another audio period buffer into the bestcomm queue. |
| 137 | * |
| 138 | * Note: The routine must only be called when there is space available in |
| 139 | * the queue. Otherwise the enqueue will fail and the audio ring buffer |
| 140 | * will get out of sync |
| 141 | */ |
| 142 | static void psc_i2s_bcom_enqueue_next_buffer(struct psc_i2s_stream *s) |
| 143 | { |
| 144 | struct bcom_bd *bd; |
| 145 | |
| 146 | /* Prepare and enqueue the next buffer descriptor */ |
| 147 | bd = bcom_prepare_next_buffer(s->bcom_task); |
| 148 | bd->status = s->period_bytes; |
| 149 | bd->data[0] = s->period_next_pt; |
| 150 | bcom_submit_next_buffer(s->bcom_task, NULL); |
| 151 | |
| 152 | /* Update for next period */ |
| 153 | s->period_next_pt += s->period_bytes; |
| 154 | if (s->period_next_pt >= s->period_end) |
| 155 | s->period_next_pt = s->period_start; |
| 156 | } |
| 157 | |
| 158 | /* Bestcomm DMA irq handler */ |
| 159 | static irqreturn_t psc_i2s_bcom_irq(int irq, void *_psc_i2s_stream) |
| 160 | { |
| 161 | struct psc_i2s_stream *s = _psc_i2s_stream; |
| 162 | |
| 163 | /* For each finished period, dequeue the completed period buffer |
| 164 | * and enqueue a new one in it's place. */ |
| 165 | while (bcom_buffer_done(s->bcom_task)) { |
| 166 | bcom_retrieve_buffer(s->bcom_task, NULL, NULL); |
| 167 | s->period_current_pt += s->period_bytes; |
| 168 | if (s->period_current_pt >= s->period_end) |
| 169 | s->period_current_pt = s->period_start; |
| 170 | psc_i2s_bcom_enqueue_next_buffer(s); |
| 171 | bcom_enable(s->bcom_task); |
| 172 | } |
| 173 | |
| 174 | /* If the stream is active, then also inform the PCM middle layer |
| 175 | * of the period finished event. */ |
| 176 | if (s->active) |
| 177 | snd_pcm_period_elapsed(s->stream); |
| 178 | |
| 179 | return IRQ_HANDLED; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * psc_i2s_startup: create a new substream |
| 184 | * |
| 185 | * This is the first function called when a stream is opened. |
| 186 | * |
| 187 | * If this is the first stream open, then grab the IRQ and program most of |
| 188 | * the PSC registers. |
| 189 | */ |
| 190 | static int psc_i2s_startup(struct snd_pcm_substream *substream) |
| 191 | { |
| 192 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 193 | struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data; |
| 194 | int rc; |
| 195 | |
| 196 | dev_dbg(psc_i2s->dev, "psc_i2s_startup(substream=%p)\n", substream); |
| 197 | |
| 198 | if (!psc_i2s->playback.active && |
| 199 | !psc_i2s->capture.active) { |
| 200 | /* Setup the IRQs */ |
| 201 | rc = request_irq(psc_i2s->irq, &psc_i2s_status_irq, IRQF_SHARED, |
| 202 | "psc-i2s-status", psc_i2s); |
| 203 | rc |= request_irq(psc_i2s->capture.irq, |
| 204 | &psc_i2s_bcom_irq, IRQF_SHARED, |
| 205 | "psc-i2s-capture", &psc_i2s->capture); |
| 206 | rc |= request_irq(psc_i2s->playback.irq, |
| 207 | &psc_i2s_bcom_irq, IRQF_SHARED, |
| 208 | "psc-i2s-playback", &psc_i2s->playback); |
| 209 | if (rc) { |
| 210 | free_irq(psc_i2s->irq, psc_i2s); |
| 211 | free_irq(psc_i2s->capture.irq, |
| 212 | &psc_i2s->capture); |
| 213 | free_irq(psc_i2s->playback.irq, |
| 214 | &psc_i2s->playback); |
| 215 | return -ENODEV; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int psc_i2s_hw_params(struct snd_pcm_substream *substream, |
| 223 | struct snd_pcm_hw_params *params) |
| 224 | { |
| 225 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 226 | struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data; |
| 227 | u32 mode; |
| 228 | |
| 229 | dev_dbg(psc_i2s->dev, "%s(substream=%p) p_size=%i p_bytes=%i" |
| 230 | " periods=%i buffer_size=%i buffer_bytes=%i\n", |
| 231 | __func__, substream, params_period_size(params), |
| 232 | params_period_bytes(params), params_periods(params), |
| 233 | params_buffer_size(params), params_buffer_bytes(params)); |
| 234 | |
| 235 | switch (params_format(params)) { |
| 236 | case SNDRV_PCM_FORMAT_S8: |
| 237 | mode = MPC52xx_PSC_SICR_SIM_CODEC_8; |
| 238 | break; |
| 239 | case SNDRV_PCM_FORMAT_S16_BE: |
| 240 | mode = MPC52xx_PSC_SICR_SIM_CODEC_16; |
| 241 | break; |
| 242 | case SNDRV_PCM_FORMAT_S24_BE: |
| 243 | mode = MPC52xx_PSC_SICR_SIM_CODEC_24; |
| 244 | break; |
| 245 | case SNDRV_PCM_FORMAT_S32_BE: |
| 246 | mode = MPC52xx_PSC_SICR_SIM_CODEC_32; |
| 247 | break; |
| 248 | default: |
| 249 | dev_dbg(psc_i2s->dev, "invalid format\n"); |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | out_be32(&psc_i2s->psc_regs->sicr, psc_i2s->sicr | mode); |
| 253 | |
| 254 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int psc_i2s_hw_free(struct snd_pcm_substream *substream) |
| 260 | { |
| 261 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * psc_i2s_trigger: start and stop the DMA transfer. |
| 267 | * |
| 268 | * This function is called by ALSA to start, stop, pause, and resume the DMA |
| 269 | * transfer of data. |
| 270 | */ |
| 271 | static int psc_i2s_trigger(struct snd_pcm_substream *substream, int cmd) |
| 272 | { |
| 273 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 274 | struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data; |
| 275 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 276 | struct psc_i2s_stream *s; |
| 277 | struct mpc52xx_psc __iomem *regs = psc_i2s->psc_regs; |
| 278 | u16 imr; |
| 279 | u8 psc_cmd; |
| 280 | long flags; |
| 281 | |
| 282 | if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 283 | s = &psc_i2s->capture; |
| 284 | else |
| 285 | s = &psc_i2s->playback; |
| 286 | |
| 287 | dev_dbg(psc_i2s->dev, "psc_i2s_trigger(substream=%p, cmd=%i)" |
| 288 | " stream_id=%i\n", |
| 289 | substream, cmd, substream->pstr->stream); |
| 290 | |
| 291 | switch (cmd) { |
| 292 | case SNDRV_PCM_TRIGGER_START: |
| 293 | s->period_bytes = frames_to_bytes(runtime, |
| 294 | runtime->period_size); |
| 295 | s->period_start = virt_to_phys(runtime->dma_area); |
| 296 | s->period_end = s->period_start + |
| 297 | (s->period_bytes * runtime->periods); |
| 298 | s->period_next_pt = s->period_start; |
| 299 | s->period_current_pt = s->period_start; |
| 300 | s->active = 1; |
| 301 | |
| 302 | /* First; reset everything */ |
| 303 | if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 304 | out_8(®s->command, MPC52xx_PSC_RST_RX); |
| 305 | out_8(®s->command, MPC52xx_PSC_RST_ERR_STAT); |
| 306 | } else { |
| 307 | out_8(®s->command, MPC52xx_PSC_RST_TX); |
| 308 | out_8(®s->command, MPC52xx_PSC_RST_ERR_STAT); |
| 309 | } |
| 310 | |
| 311 | /* Next, fill up the bestcomm bd queue and enable DMA. |
| 312 | * This will begin filling the PSC's fifo. */ |
| 313 | if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 314 | bcom_gen_bd_rx_reset(s->bcom_task); |
| 315 | else |
| 316 | bcom_gen_bd_tx_reset(s->bcom_task); |
| 317 | while (!bcom_queue_full(s->bcom_task)) |
| 318 | psc_i2s_bcom_enqueue_next_buffer(s); |
| 319 | bcom_enable(s->bcom_task); |
| 320 | |
| 321 | /* Due to errata in the i2s mode; need to line up enabling |
| 322 | * the transmitter with a transition on the frame sync |
| 323 | * line */ |
| 324 | |
| 325 | spin_lock_irqsave(&psc_i2s->lock, flags); |
| 326 | /* first make sure it is low */ |
| 327 | while ((in_8(®s->ipcr_acr.ipcr) & 0x80) != 0) |
| 328 | ; |
| 329 | /* then wait for the transition to high */ |
| 330 | while ((in_8(®s->ipcr_acr.ipcr) & 0x80) == 0) |
| 331 | ; |
| 332 | /* Finally, enable the PSC. |
| 333 | * Receiver must always be enabled; even when we only want |
| 334 | * transmit. (see 15.3.2.3 of MPC5200B User's Guide) */ |
| 335 | psc_cmd = MPC52xx_PSC_RX_ENABLE; |
| 336 | if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 337 | psc_cmd |= MPC52xx_PSC_TX_ENABLE; |
| 338 | out_8(®s->command, psc_cmd); |
| 339 | spin_unlock_irqrestore(&psc_i2s->lock, flags); |
| 340 | |
| 341 | break; |
| 342 | |
| 343 | case SNDRV_PCM_TRIGGER_STOP: |
| 344 | /* Turn off the PSC */ |
| 345 | s->active = 0; |
| 346 | if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 347 | if (!psc_i2s->playback.active) { |
| 348 | out_8(®s->command, 2 << 4); /* reset rx */ |
| 349 | out_8(®s->command, 3 << 4); /* reset tx */ |
| 350 | out_8(®s->command, 4 << 4); /* reset err */ |
| 351 | } |
| 352 | } else { |
| 353 | out_8(®s->command, 3 << 4); /* reset tx */ |
| 354 | out_8(®s->command, 4 << 4); /* reset err */ |
| 355 | if (!psc_i2s->capture.active) |
| 356 | out_8(®s->command, 2 << 4); /* reset rx */ |
| 357 | } |
| 358 | |
| 359 | bcom_disable(s->bcom_task); |
| 360 | while (!bcom_queue_empty(s->bcom_task)) |
| 361 | bcom_retrieve_buffer(s->bcom_task, NULL, NULL); |
| 362 | |
| 363 | break; |
| 364 | |
| 365 | default: |
| 366 | dev_dbg(psc_i2s->dev, "invalid command\n"); |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | |
| 370 | /* Update interrupt enable settings */ |
| 371 | imr = 0; |
| 372 | if (psc_i2s->playback.active) |
| 373 | imr |= MPC52xx_PSC_IMR_TXEMP; |
| 374 | if (psc_i2s->capture.active) |
| 375 | imr |= MPC52xx_PSC_IMR_ORERR; |
| 376 | out_be16(®s->isr_imr.imr, imr); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * psc_i2s_shutdown: shutdown the data transfer on a stream |
| 383 | * |
| 384 | * Shutdown the PSC if there are no other substreams open. |
| 385 | */ |
| 386 | static void psc_i2s_shutdown(struct snd_pcm_substream *substream) |
| 387 | { |
| 388 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 389 | struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data; |
| 390 | |
| 391 | dev_dbg(psc_i2s->dev, "psc_i2s_shutdown(substream=%p)\n", substream); |
| 392 | |
| 393 | /* |
| 394 | * If this is the last active substream, disable the PSC and release |
| 395 | * the IRQ. |
| 396 | */ |
| 397 | if (!psc_i2s->playback.active && |
| 398 | !psc_i2s->capture.active) { |
| 399 | |
| 400 | /* Disable all interrupts and reset the PSC */ |
| 401 | out_be16(&psc_i2s->psc_regs->isr_imr.imr, 0); |
| 402 | out_8(&psc_i2s->psc_regs->command, 3 << 4); /* reset tx */ |
| 403 | out_8(&psc_i2s->psc_regs->command, 2 << 4); /* reset rx */ |
| 404 | out_8(&psc_i2s->psc_regs->command, 1 << 4); /* reset mode */ |
| 405 | out_8(&psc_i2s->psc_regs->command, 4 << 4); /* reset error */ |
| 406 | |
| 407 | /* Release irqs */ |
| 408 | free_irq(psc_i2s->irq, psc_i2s); |
| 409 | free_irq(psc_i2s->capture.irq, &psc_i2s->capture); |
| 410 | free_irq(psc_i2s->playback.irq, &psc_i2s->playback); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * psc_i2s_set_sysclk: set the clock frequency and direction |
| 416 | * |
| 417 | * This function is called by the machine driver to tell us what the clock |
| 418 | * frequency and direction are. |
| 419 | * |
| 420 | * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN), |
| 421 | * and we don't care about the frequency. Return an error if the direction |
| 422 | * is not SND_SOC_CLOCK_IN. |
| 423 | * |
| 424 | * @clk_id: reserved, should be zero |
| 425 | * @freq: the frequency of the given clock ID, currently ignored |
| 426 | * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master) |
| 427 | */ |
| 428 | static int psc_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, |
| 429 | int clk_id, unsigned int freq, int dir) |
| 430 | { |
| 431 | struct psc_i2s *psc_i2s = cpu_dai->private_data; |
| 432 | dev_dbg(psc_i2s->dev, "psc_i2s_set_sysclk(cpu_dai=%p, dir=%i)\n", |
| 433 | cpu_dai, dir); |
| 434 | return (dir == SND_SOC_CLOCK_IN) ? 0 : -EINVAL; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * psc_i2s_set_fmt: set the serial format. |
| 439 | * |
| 440 | * This function is called by the machine driver to tell us what serial |
| 441 | * format to use. |
| 442 | * |
| 443 | * This driver only supports I2S mode. Return an error if the format is |
| 444 | * not SND_SOC_DAIFMT_I2S. |
| 445 | * |
| 446 | * @format: one of SND_SOC_DAIFMT_xxx |
| 447 | */ |
| 448 | static int psc_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int format) |
| 449 | { |
| 450 | struct psc_i2s *psc_i2s = cpu_dai->private_data; |
| 451 | dev_dbg(psc_i2s->dev, "psc_i2s_set_fmt(cpu_dai=%p, format=%i)\n", |
| 452 | cpu_dai, format); |
| 453 | return (format == SND_SOC_DAIFMT_I2S) ? 0 : -EINVAL; |
| 454 | } |
| 455 | |
| 456 | /* --------------------------------------------------------------------- |
| 457 | * ALSA SoC Bindings |
| 458 | * |
| 459 | * - Digital Audio Interface (DAI) template |
| 460 | * - create/destroy dai hooks |
| 461 | */ |
| 462 | |
| 463 | /** |
| 464 | * psc_i2s_dai_template: template CPU Digital Audio Interface |
| 465 | */ |
| 466 | static struct snd_soc_dai psc_i2s_dai_template = { |
| 467 | .type = SND_SOC_DAI_I2S, |
| 468 | .playback = { |
| 469 | .channels_min = 2, |
| 470 | .channels_max = 2, |
| 471 | .rates = PSC_I2S_RATES, |
| 472 | .formats = PSC_I2S_FORMATS, |
| 473 | }, |
| 474 | .capture = { |
| 475 | .channels_min = 2, |
| 476 | .channels_max = 2, |
| 477 | .rates = PSC_I2S_RATES, |
| 478 | .formats = PSC_I2S_FORMATS, |
| 479 | }, |
| 480 | .ops = { |
| 481 | .startup = psc_i2s_startup, |
| 482 | .hw_params = psc_i2s_hw_params, |
| 483 | .hw_free = psc_i2s_hw_free, |
| 484 | .shutdown = psc_i2s_shutdown, |
| 485 | .trigger = psc_i2s_trigger, |
| 486 | }, |
| 487 | .dai_ops = { |
| 488 | .set_sysclk = psc_i2s_set_sysclk, |
| 489 | .set_fmt = psc_i2s_set_fmt, |
| 490 | }, |
| 491 | }; |
| 492 | |
| 493 | /* --------------------------------------------------------------------- |
| 494 | * The PSC I2S 'ASoC platform' driver |
| 495 | * |
| 496 | * Can be referenced by an 'ASoC machine' driver |
| 497 | * This driver only deals with the audio bus; it doesn't have any |
| 498 | * interaction with the attached codec |
| 499 | */ |
| 500 | |
| 501 | static const struct snd_pcm_hardware psc_i2s_pcm_hardware = { |
| 502 | .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | |
| 503 | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER, |
| 504 | .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | |
| 505 | SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE, |
| 506 | .rate_min = 8000, |
| 507 | .rate_max = 48000, |
| 508 | .channels_min = 2, |
| 509 | .channels_max = 2, |
| 510 | .period_bytes_max = 1024 * 1024, |
| 511 | .period_bytes_min = 32, |
| 512 | .periods_min = 2, |
| 513 | .periods_max = 256, |
| 514 | .buffer_bytes_max = 2 * 1024 * 1024, |
| 515 | .fifo_size = 0, |
| 516 | }; |
| 517 | |
| 518 | static int psc_i2s_pcm_open(struct snd_pcm_substream *substream) |
| 519 | { |
| 520 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 521 | struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data; |
| 522 | struct psc_i2s_stream *s; |
| 523 | |
| 524 | dev_dbg(psc_i2s->dev, "psc_i2s_pcm_open(substream=%p)\n", substream); |
| 525 | |
| 526 | if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 527 | s = &psc_i2s->capture; |
| 528 | else |
| 529 | s = &psc_i2s->playback; |
| 530 | |
| 531 | snd_soc_set_runtime_hwparams(substream, &psc_i2s_pcm_hardware); |
| 532 | |
| 533 | s->stream = substream; |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int psc_i2s_pcm_close(struct snd_pcm_substream *substream) |
| 538 | { |
| 539 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 540 | struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data; |
| 541 | struct psc_i2s_stream *s; |
| 542 | |
| 543 | dev_dbg(psc_i2s->dev, "psc_i2s_pcm_close(substream=%p)\n", substream); |
| 544 | |
| 545 | if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 546 | s = &psc_i2s->capture; |
| 547 | else |
| 548 | s = &psc_i2s->playback; |
| 549 | |
| 550 | s->stream = NULL; |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static snd_pcm_uframes_t |
| 555 | psc_i2s_pcm_pointer(struct snd_pcm_substream *substream) |
| 556 | { |
| 557 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 558 | struct psc_i2s *psc_i2s = rtd->dai->cpu_dai->private_data; |
| 559 | struct psc_i2s_stream *s; |
| 560 | dma_addr_t count; |
| 561 | |
| 562 | if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 563 | s = &psc_i2s->capture; |
| 564 | else |
| 565 | s = &psc_i2s->playback; |
| 566 | |
| 567 | count = s->period_current_pt - s->period_start; |
| 568 | |
| 569 | return bytes_to_frames(substream->runtime, count); |
| 570 | } |
| 571 | |
| 572 | static struct snd_pcm_ops psc_i2s_pcm_ops = { |
| 573 | .open = psc_i2s_pcm_open, |
| 574 | .close = psc_i2s_pcm_close, |
| 575 | .ioctl = snd_pcm_lib_ioctl, |
| 576 | .pointer = psc_i2s_pcm_pointer, |
| 577 | }; |
| 578 | |
| 579 | static u64 psc_i2s_pcm_dmamask = 0xffffffff; |
| 580 | static int psc_i2s_pcm_new(struct snd_card *card, struct snd_soc_dai *dai, |
| 581 | struct snd_pcm *pcm) |
| 582 | { |
| 583 | struct snd_soc_pcm_runtime *rtd = pcm->private_data; |
| 584 | size_t size = psc_i2s_pcm_hardware.buffer_bytes_max; |
| 585 | int rc = 0; |
| 586 | |
| 587 | dev_dbg(rtd->socdev->dev, "psc_i2s_pcm_new(card=%p, dai=%p, pcm=%p)\n", |
| 588 | card, dai, pcm); |
| 589 | |
| 590 | if (!card->dev->dma_mask) |
| 591 | card->dev->dma_mask = &psc_i2s_pcm_dmamask; |
| 592 | if (!card->dev->coherent_dma_mask) |
| 593 | card->dev->coherent_dma_mask = 0xffffffff; |
| 594 | |
| 595 | if (pcm->streams[0].substream) { |
| 596 | rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->dev, size, |
| 597 | &pcm->streams[0].substream->dma_buffer); |
| 598 | if (rc) |
| 599 | goto playback_alloc_err; |
| 600 | } |
| 601 | |
| 602 | if (pcm->streams[1].substream) { |
| 603 | rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->dev, size, |
| 604 | &pcm->streams[1].substream->dma_buffer); |
| 605 | if (rc) |
| 606 | goto capture_alloc_err; |
| 607 | } |
| 608 | |
| 609 | return 0; |
| 610 | |
| 611 | capture_alloc_err: |
| 612 | if (pcm->streams[0].substream) |
| 613 | snd_dma_free_pages(&pcm->streams[0].substream->dma_buffer); |
| 614 | playback_alloc_err: |
| 615 | dev_err(card->dev, "Cannot allocate buffer(s)\n"); |
| 616 | return -ENOMEM; |
| 617 | } |
| 618 | |
| 619 | static void psc_i2s_pcm_free(struct snd_pcm *pcm) |
| 620 | { |
| 621 | struct snd_soc_pcm_runtime *rtd = pcm->private_data; |
| 622 | struct snd_pcm_substream *substream; |
| 623 | int stream; |
| 624 | |
| 625 | dev_dbg(rtd->socdev->dev, "psc_i2s_pcm_free(pcm=%p)\n", pcm); |
| 626 | |
| 627 | for (stream = 0; stream < 2; stream++) { |
| 628 | substream = pcm->streams[stream].substream; |
| 629 | if (substream) { |
| 630 | snd_dma_free_pages(&substream->dma_buffer); |
| 631 | substream->dma_buffer.area = NULL; |
| 632 | substream->dma_buffer.addr = 0; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | struct snd_soc_platform psc_i2s_pcm_soc_platform = { |
| 638 | .name = "mpc5200-psc-audio", |
| 639 | .pcm_ops = &psc_i2s_pcm_ops, |
| 640 | .pcm_new = &psc_i2s_pcm_new, |
| 641 | .pcm_free = &psc_i2s_pcm_free, |
| 642 | }; |
| 643 | |
| 644 | /* --------------------------------------------------------------------- |
| 645 | * Sysfs attributes for debugging |
| 646 | */ |
| 647 | |
| 648 | static ssize_t psc_i2s_status_show(struct device *dev, |
| 649 | struct device_attribute *attr, char *buf) |
| 650 | { |
| 651 | struct psc_i2s *psc_i2s = dev_get_drvdata(dev); |
| 652 | |
| 653 | return sprintf(buf, "status=%.4x sicr=%.8x rfnum=%i rfstat=0x%.4x " |
| 654 | "tfnum=%i tfstat=0x%.4x\n", |
| 655 | in_be16(&psc_i2s->psc_regs->sr_csr.status), |
| 656 | in_be32(&psc_i2s->psc_regs->sicr), |
| 657 | in_be16(&psc_i2s->fifo_regs->rfnum) & 0x1ff, |
| 658 | in_be16(&psc_i2s->fifo_regs->rfstat), |
| 659 | in_be16(&psc_i2s->fifo_regs->tfnum) & 0x1ff, |
| 660 | in_be16(&psc_i2s->fifo_regs->tfstat)); |
| 661 | } |
| 662 | |
| 663 | static int *psc_i2s_get_stat_attr(struct psc_i2s *psc_i2s, const char *name) |
| 664 | { |
| 665 | if (strcmp(name, "playback_underrun") == 0) |
| 666 | return &psc_i2s->stats.underrun_count; |
| 667 | if (strcmp(name, "capture_overrun") == 0) |
| 668 | return &psc_i2s->stats.overrun_count; |
| 669 | |
| 670 | return NULL; |
| 671 | } |
| 672 | |
| 673 | static ssize_t psc_i2s_stat_show(struct device *dev, |
| 674 | struct device_attribute *attr, char *buf) |
| 675 | { |
| 676 | struct psc_i2s *psc_i2s = dev_get_drvdata(dev); |
| 677 | int *attrib; |
| 678 | |
| 679 | attrib = psc_i2s_get_stat_attr(psc_i2s, attr->attr.name); |
| 680 | if (!attrib) |
| 681 | return 0; |
| 682 | |
| 683 | return sprintf(buf, "%i\n", *attrib); |
| 684 | } |
| 685 | |
| 686 | static ssize_t psc_i2s_stat_store(struct device *dev, |
| 687 | struct device_attribute *attr, |
| 688 | const char *buf, |
| 689 | size_t count) |
| 690 | { |
| 691 | struct psc_i2s *psc_i2s = dev_get_drvdata(dev); |
| 692 | int *attrib; |
| 693 | |
| 694 | attrib = psc_i2s_get_stat_attr(psc_i2s, attr->attr.name); |
| 695 | if (!attrib) |
| 696 | return 0; |
| 697 | |
| 698 | *attrib = simple_strtoul(buf, NULL, 0); |
| 699 | return count; |
| 700 | } |
| 701 | |
| 702 | DEVICE_ATTR(status, 0644, psc_i2s_status_show, NULL); |
| 703 | DEVICE_ATTR(playback_underrun, 0644, psc_i2s_stat_show, psc_i2s_stat_store); |
| 704 | DEVICE_ATTR(capture_overrun, 0644, psc_i2s_stat_show, psc_i2s_stat_store); |
| 705 | |
| 706 | /* --------------------------------------------------------------------- |
| 707 | * OF platform bus binding code: |
| 708 | * - Probe/remove operations |
| 709 | * - OF device match table |
| 710 | */ |
| 711 | static int __devinit psc_i2s_of_probe(struct of_device *op, |
| 712 | const struct of_device_id *match) |
| 713 | { |
| 714 | phys_addr_t fifo; |
| 715 | struct psc_i2s *psc_i2s; |
| 716 | struct resource res; |
| 717 | int size, psc_id, irq, rc; |
| 718 | const __be32 *prop; |
| 719 | void __iomem *regs; |
| 720 | |
| 721 | dev_dbg(&op->dev, "probing psc i2s device\n"); |
| 722 | |
| 723 | /* Get the PSC ID */ |
| 724 | prop = of_get_property(op->node, "cell-index", &size); |
| 725 | if (!prop || size < sizeof *prop) |
| 726 | return -ENODEV; |
| 727 | psc_id = be32_to_cpu(*prop); |
| 728 | |
| 729 | /* Fetch the registers and IRQ of the PSC */ |
| 730 | irq = irq_of_parse_and_map(op->node, 0); |
| 731 | if (of_address_to_resource(op->node, 0, &res)) { |
| 732 | dev_err(&op->dev, "Missing reg property\n"); |
| 733 | return -ENODEV; |
| 734 | } |
| 735 | regs = ioremap(res.start, 1 + res.end - res.start); |
| 736 | if (!regs) { |
| 737 | dev_err(&op->dev, "Could not map registers\n"); |
| 738 | return -ENODEV; |
| 739 | } |
| 740 | |
| 741 | /* Allocate and initialize the driver private data */ |
| 742 | psc_i2s = kzalloc(sizeof *psc_i2s, GFP_KERNEL); |
| 743 | if (!psc_i2s) { |
| 744 | iounmap(regs); |
| 745 | return -ENOMEM; |
| 746 | } |
| 747 | spin_lock_init(&psc_i2s->lock); |
| 748 | psc_i2s->irq = irq; |
| 749 | psc_i2s->psc_regs = regs; |
| 750 | psc_i2s->fifo_regs = regs + sizeof *psc_i2s->psc_regs; |
| 751 | psc_i2s->dev = &op->dev; |
| 752 | psc_i2s->playback.psc_i2s = psc_i2s; |
| 753 | psc_i2s->capture.psc_i2s = psc_i2s; |
| 754 | snprintf(psc_i2s->name, sizeof psc_i2s->name, "PSC%u", psc_id+1); |
| 755 | |
| 756 | /* Fill out the CPU DAI structure */ |
| 757 | memcpy(&psc_i2s->dai, &psc_i2s_dai_template, sizeof psc_i2s->dai); |
| 758 | psc_i2s->dai.private_data = psc_i2s; |
| 759 | psc_i2s->dai.name = psc_i2s->name; |
| 760 | psc_i2s->dai.id = psc_id; |
| 761 | |
| 762 | /* Find the address of the fifo data registers and setup the |
| 763 | * DMA tasks */ |
| 764 | fifo = res.start + offsetof(struct mpc52xx_psc, buffer.buffer_32); |
| 765 | psc_i2s->capture.bcom_task = |
| 766 | bcom_psc_gen_bd_rx_init(psc_id, 10, fifo, 512); |
| 767 | psc_i2s->playback.bcom_task = |
| 768 | bcom_psc_gen_bd_tx_init(psc_id, 10, fifo); |
| 769 | if (!psc_i2s->capture.bcom_task || |
| 770 | !psc_i2s->playback.bcom_task) { |
| 771 | dev_err(&op->dev, "Could not allocate bestcomm tasks\n"); |
| 772 | iounmap(regs); |
| 773 | kfree(psc_i2s); |
| 774 | return -ENODEV; |
| 775 | } |
| 776 | |
| 777 | /* Disable all interrupts and reset the PSC */ |
| 778 | out_be16(&psc_i2s->psc_regs->isr_imr.imr, 0); |
| 779 | out_8(&psc_i2s->psc_regs->command, 3 << 4); /* reset transmitter */ |
| 780 | out_8(&psc_i2s->psc_regs->command, 2 << 4); /* reset receiver */ |
| 781 | out_8(&psc_i2s->psc_regs->command, 1 << 4); /* reset mode */ |
| 782 | out_8(&psc_i2s->psc_regs->command, 4 << 4); /* reset error */ |
| 783 | |
| 784 | /* Configure the serial interface mode; defaulting to CODEC8 mode */ |
| 785 | psc_i2s->sicr = MPC52xx_PSC_SICR_DTS1 | MPC52xx_PSC_SICR_I2S | |
| 786 | MPC52xx_PSC_SICR_CLKPOL; |
| 787 | if (of_get_property(op->node, "fsl,cellslave", NULL)) |
| 788 | psc_i2s->sicr |= MPC52xx_PSC_SICR_CELLSLAVE | |
| 789 | MPC52xx_PSC_SICR_GENCLK; |
| 790 | out_be32(&psc_i2s->psc_regs->sicr, |
| 791 | psc_i2s->sicr | MPC52xx_PSC_SICR_SIM_CODEC_8); |
| 792 | |
| 793 | /* Check for the codec handle. If it is not present then we |
| 794 | * are done */ |
| 795 | if (!of_get_property(op->node, "codec-handle", NULL)) |
| 796 | return 0; |
| 797 | |
| 798 | /* Set up mode register; |
| 799 | * First write: RxRdy (FIFO Alarm) generates rx FIFO irq |
| 800 | * Second write: register Normal mode for non loopback |
| 801 | */ |
| 802 | out_8(&psc_i2s->psc_regs->mode, 0); |
| 803 | out_8(&psc_i2s->psc_regs->mode, 0); |
| 804 | |
| 805 | /* Set the TX and RX fifo alarm thresholds */ |
| 806 | out_be16(&psc_i2s->fifo_regs->rfalarm, 0x100); |
| 807 | out_8(&psc_i2s->fifo_regs->rfcntl, 0x4); |
| 808 | out_be16(&psc_i2s->fifo_regs->tfalarm, 0x100); |
| 809 | out_8(&psc_i2s->fifo_regs->tfcntl, 0x7); |
| 810 | |
| 811 | /* Lookup the IRQ numbers */ |
| 812 | psc_i2s->playback.irq = |
| 813 | bcom_get_task_irq(psc_i2s->playback.bcom_task); |
| 814 | psc_i2s->capture.irq = |
| 815 | bcom_get_task_irq(psc_i2s->capture.bcom_task); |
| 816 | |
| 817 | /* Save what we've done so it can be found again later */ |
| 818 | dev_set_drvdata(&op->dev, psc_i2s); |
| 819 | |
| 820 | /* Register the SYSFS files */ |
| 821 | rc = device_create_file(psc_i2s->dev, &dev_attr_status); |
| 822 | rc = device_create_file(psc_i2s->dev, &dev_attr_capture_overrun); |
| 823 | rc = device_create_file(psc_i2s->dev, &dev_attr_playback_underrun); |
| 824 | if (rc) |
| 825 | dev_info(psc_i2s->dev, "error creating sysfs files\n"); |
| 826 | |
| 827 | /* Tell the ASoC OF helpers about it */ |
| 828 | of_snd_soc_register_platform(&psc_i2s_pcm_soc_platform, op->node, |
| 829 | &psc_i2s->dai); |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int __devexit psc_i2s_of_remove(struct of_device *op) |
| 835 | { |
| 836 | struct psc_i2s *psc_i2s = dev_get_drvdata(&op->dev); |
| 837 | |
| 838 | dev_dbg(&op->dev, "psc_i2s_remove()\n"); |
| 839 | |
| 840 | bcom_gen_bd_rx_release(psc_i2s->capture.bcom_task); |
| 841 | bcom_gen_bd_tx_release(psc_i2s->playback.bcom_task); |
| 842 | |
| 843 | iounmap(psc_i2s->psc_regs); |
| 844 | iounmap(psc_i2s->fifo_regs); |
| 845 | kfree(psc_i2s); |
| 846 | dev_set_drvdata(&op->dev, NULL); |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | /* Match table for of_platform binding */ |
| 852 | static struct of_device_id psc_i2s_match[] __devinitdata = { |
| 853 | { .compatible = "fsl,mpc5200-psc-i2s", }, |
| 854 | {} |
| 855 | }; |
| 856 | MODULE_DEVICE_TABLE(of, psc_i2s_match); |
| 857 | |
| 858 | static struct of_platform_driver psc_i2s_driver = { |
| 859 | .match_table = psc_i2s_match, |
| 860 | .probe = psc_i2s_of_probe, |
| 861 | .remove = __devexit_p(psc_i2s_of_remove), |
| 862 | .driver = { |
| 863 | .name = "mpc5200-psc-i2s", |
| 864 | .owner = THIS_MODULE, |
| 865 | }, |
| 866 | }; |
| 867 | |
| 868 | /* --------------------------------------------------------------------- |
| 869 | * Module setup and teardown; simply register the of_platform driver |
| 870 | * for the PSC in I2S mode. |
| 871 | */ |
| 872 | static int __init psc_i2s_init(void) |
| 873 | { |
| 874 | return of_register_platform_driver(&psc_i2s_driver); |
| 875 | } |
| 876 | module_init(psc_i2s_init); |
| 877 | |
| 878 | static void __exit psc_i2s_exit(void) |
| 879 | { |
| 880 | of_unregister_platform_driver(&psc_i2s_driver); |
| 881 | } |
| 882 | module_exit(psc_i2s_exit); |
| 883 | |
| 884 | |