2 * Freescale MPC5200 PSC in I2S mode
3 * ALSA SoC Digital Audio Interface (DAI) driver
5 * Copyright (C) 2008 Secret Lab Technologies Ltd.
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>
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>
24 #include <sysdev/bestcomm/bestcomm.h>
25 #include <sysdev/bestcomm/gen_bd.h>
26 #include <asm/mpc52xx_psc.h>
28 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
29 MODULE_DESCRIPTION("Freescale MPC5200 PSC in I2S mode ASoC Driver");
30 MODULE_LICENSE("GPL");
33 * PSC_I2S_RATES: sample rates supported by the I2S
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.
40 #define PSC_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
41 SNDRV_PCM_RATE_CONTINUOUS)
44 * PSC_I2S_FORMATS: audio formats supported by the PSC I2S mode
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)
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
61 struct psc_i2s_stream
{
63 struct psc_i2s
*psc_i2s
;
64 struct bcom_task
*bcom_task
;
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
;
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
84 * @playback: Playback stream context data
85 * @capture: Capture stream context data
89 struct mpc52xx_psc __iomem
*psc_regs
;
90 struct mpc52xx_psc_fifo __iomem
*fifo_regs
;
93 struct snd_soc_dai dai
;
98 struct psc_i2s_stream playback
;
99 struct psc_i2s_stream capture
;
111 static irqreturn_t
psc_i2s_status_irq(int irq
, void *_psc_i2s
)
113 struct psc_i2s
*psc_i2s
= _psc_i2s
;
114 struct mpc52xx_psc __iomem
*regs
= psc_i2s
->psc_regs
;
117 isr
= in_be16(®s
->mpc52xx_psc_isr
);
119 /* Playback underrun error */
120 if (psc_i2s
->playback
.active
&& (isr
& MPC52xx_PSC_IMR_TXEMP
))
121 psc_i2s
->stats
.underrun_count
++;
123 /* Capture overrun error */
124 if (psc_i2s
->capture
.active
&& (isr
& MPC52xx_PSC_IMR_ORERR
))
125 psc_i2s
->stats
.overrun_count
++;
127 out_8(®s
->command
, 4 << 4); /* reset the error status */
133 * psc_i2s_bcom_enqueue_next_buffer - Enqueue another audio buffer
134 * @s: pointer to stream private data structure
136 * Enqueues another audio period buffer into the bestcomm queue.
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
142 static void psc_i2s_bcom_enqueue_next_buffer(struct psc_i2s_stream
*s
)
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
);
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
;
158 /* Bestcomm DMA irq handler */
159 static irqreturn_t
psc_i2s_bcom_irq(int irq
, void *_psc_i2s_stream
)
161 struct psc_i2s_stream
*s
= _psc_i2s_stream
;
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
);
174 /* If the stream is active, then also inform the PCM middle layer
175 * of the period finished event. */
177 snd_pcm_period_elapsed(s
->stream
);
183 * psc_i2s_startup: create a new substream
185 * This is the first function called when a stream is opened.
187 * If this is the first stream open, then grab the IRQ and program most of
190 static int psc_i2s_startup(struct snd_pcm_substream
*substream
,
191 struct snd_soc_dai
*dai
)
193 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
194 struct psc_i2s
*psc_i2s
= rtd
->dai
->cpu_dai
->private_data
;
197 dev_dbg(psc_i2s
->dev
, "psc_i2s_startup(substream=%p)\n", substream
);
199 if (!psc_i2s
->playback
.active
&&
200 !psc_i2s
->capture
.active
) {
202 rc
= request_irq(psc_i2s
->irq
, &psc_i2s_status_irq
, IRQF_SHARED
,
203 "psc-i2s-status", psc_i2s
);
204 rc
|= request_irq(psc_i2s
->capture
.irq
,
205 &psc_i2s_bcom_irq
, IRQF_SHARED
,
206 "psc-i2s-capture", &psc_i2s
->capture
);
207 rc
|= request_irq(psc_i2s
->playback
.irq
,
208 &psc_i2s_bcom_irq
, IRQF_SHARED
,
209 "psc-i2s-playback", &psc_i2s
->playback
);
211 free_irq(psc_i2s
->irq
, psc_i2s
);
212 free_irq(psc_i2s
->capture
.irq
,
214 free_irq(psc_i2s
->playback
.irq
,
223 static int psc_i2s_hw_params(struct snd_pcm_substream
*substream
,
224 struct snd_pcm_hw_params
*params
,
225 struct snd_soc_dai
*dai
)
227 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
228 struct psc_i2s
*psc_i2s
= rtd
->dai
->cpu_dai
->private_data
;
231 dev_dbg(psc_i2s
->dev
, "%s(substream=%p) p_size=%i p_bytes=%i"
232 " periods=%i buffer_size=%i buffer_bytes=%i\n",
233 __func__
, substream
, params_period_size(params
),
234 params_period_bytes(params
), params_periods(params
),
235 params_buffer_size(params
), params_buffer_bytes(params
));
237 switch (params_format(params
)) {
238 case SNDRV_PCM_FORMAT_S8
:
239 mode
= MPC52xx_PSC_SICR_SIM_CODEC_8
;
241 case SNDRV_PCM_FORMAT_S16_BE
:
242 mode
= MPC52xx_PSC_SICR_SIM_CODEC_16
;
244 case SNDRV_PCM_FORMAT_S24_BE
:
245 mode
= MPC52xx_PSC_SICR_SIM_CODEC_24
;
247 case SNDRV_PCM_FORMAT_S32_BE
:
248 mode
= MPC52xx_PSC_SICR_SIM_CODEC_32
;
251 dev_dbg(psc_i2s
->dev
, "invalid format\n");
254 out_be32(&psc_i2s
->psc_regs
->sicr
, psc_i2s
->sicr
| mode
);
256 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
261 static int psc_i2s_hw_free(struct snd_pcm_substream
*substream
,
262 struct snd_soc_dai
*dai
)
264 snd_pcm_set_runtime_buffer(substream
, NULL
);
269 * psc_i2s_trigger: start and stop the DMA transfer.
271 * This function is called by ALSA to start, stop, pause, and resume the DMA
274 static int psc_i2s_trigger(struct snd_pcm_substream
*substream
, int cmd
,
275 struct snd_soc_dai
*dai
)
277 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
278 struct psc_i2s
*psc_i2s
= rtd
->dai
->cpu_dai
->private_data
;
279 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
280 struct psc_i2s_stream
*s
;
281 struct mpc52xx_psc __iomem
*regs
= psc_i2s
->psc_regs
;
286 if (substream
->pstr
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
287 s
= &psc_i2s
->capture
;
289 s
= &psc_i2s
->playback
;
291 dev_dbg(psc_i2s
->dev
, "psc_i2s_trigger(substream=%p, cmd=%i)"
293 substream
, cmd
, substream
->pstr
->stream
);
296 case SNDRV_PCM_TRIGGER_START
:
297 s
->period_bytes
= frames_to_bytes(runtime
,
298 runtime
->period_size
);
299 s
->period_start
= virt_to_phys(runtime
->dma_area
);
300 s
->period_end
= s
->period_start
+
301 (s
->period_bytes
* runtime
->periods
);
302 s
->period_next_pt
= s
->period_start
;
303 s
->period_current_pt
= s
->period_start
;
306 /* First; reset everything */
307 if (substream
->pstr
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
308 out_8(®s
->command
, MPC52xx_PSC_RST_RX
);
309 out_8(®s
->command
, MPC52xx_PSC_RST_ERR_STAT
);
311 out_8(®s
->command
, MPC52xx_PSC_RST_TX
);
312 out_8(®s
->command
, MPC52xx_PSC_RST_ERR_STAT
);
315 /* Next, fill up the bestcomm bd queue and enable DMA.
316 * This will begin filling the PSC's fifo. */
317 if (substream
->pstr
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
318 bcom_gen_bd_rx_reset(s
->bcom_task
);
320 bcom_gen_bd_tx_reset(s
->bcom_task
);
321 while (!bcom_queue_full(s
->bcom_task
))
322 psc_i2s_bcom_enqueue_next_buffer(s
);
323 bcom_enable(s
->bcom_task
);
325 /* Due to errata in the i2s mode; need to line up enabling
326 * the transmitter with a transition on the frame sync
329 spin_lock_irqsave(&psc_i2s
->lock
, flags
);
330 /* first make sure it is low */
331 while ((in_8(®s
->ipcr_acr
.ipcr
) & 0x80) != 0)
333 /* then wait for the transition to high */
334 while ((in_8(®s
->ipcr_acr
.ipcr
) & 0x80) == 0)
336 /* Finally, enable the PSC.
337 * Receiver must always be enabled; even when we only want
338 * transmit. (see 15.3.2.3 of MPC5200B User's Guide) */
339 psc_cmd
= MPC52xx_PSC_RX_ENABLE
;
340 if (substream
->pstr
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
341 psc_cmd
|= MPC52xx_PSC_TX_ENABLE
;
342 out_8(®s
->command
, psc_cmd
);
343 spin_unlock_irqrestore(&psc_i2s
->lock
, flags
);
347 case SNDRV_PCM_TRIGGER_STOP
:
348 /* Turn off the PSC */
350 if (substream
->pstr
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
351 if (!psc_i2s
->playback
.active
) {
352 out_8(®s
->command
, 2 << 4); /* reset rx */
353 out_8(®s
->command
, 3 << 4); /* reset tx */
354 out_8(®s
->command
, 4 << 4); /* reset err */
357 out_8(®s
->command
, 3 << 4); /* reset tx */
358 out_8(®s
->command
, 4 << 4); /* reset err */
359 if (!psc_i2s
->capture
.active
)
360 out_8(®s
->command
, 2 << 4); /* reset rx */
363 bcom_disable(s
->bcom_task
);
364 while (!bcom_queue_empty(s
->bcom_task
))
365 bcom_retrieve_buffer(s
->bcom_task
, NULL
, NULL
);
370 dev_dbg(psc_i2s
->dev
, "invalid command\n");
374 /* Update interrupt enable settings */
376 if (psc_i2s
->playback
.active
)
377 imr
|= MPC52xx_PSC_IMR_TXEMP
;
378 if (psc_i2s
->capture
.active
)
379 imr
|= MPC52xx_PSC_IMR_ORERR
;
380 out_be16(®s
->isr_imr
.imr
, imr
);
386 * psc_i2s_shutdown: shutdown the data transfer on a stream
388 * Shutdown the PSC if there are no other substreams open.
390 static void psc_i2s_shutdown(struct snd_pcm_substream
*substream
,
391 struct snd_soc_dai
*dai
)
393 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
394 struct psc_i2s
*psc_i2s
= rtd
->dai
->cpu_dai
->private_data
;
396 dev_dbg(psc_i2s
->dev
, "psc_i2s_shutdown(substream=%p)\n", substream
);
399 * If this is the last active substream, disable the PSC and release
402 if (!psc_i2s
->playback
.active
&&
403 !psc_i2s
->capture
.active
) {
405 /* Disable all interrupts and reset the PSC */
406 out_be16(&psc_i2s
->psc_regs
->isr_imr
.imr
, 0);
407 out_8(&psc_i2s
->psc_regs
->command
, 3 << 4); /* reset tx */
408 out_8(&psc_i2s
->psc_regs
->command
, 2 << 4); /* reset rx */
409 out_8(&psc_i2s
->psc_regs
->command
, 1 << 4); /* reset mode */
410 out_8(&psc_i2s
->psc_regs
->command
, 4 << 4); /* reset error */
413 free_irq(psc_i2s
->irq
, psc_i2s
);
414 free_irq(psc_i2s
->capture
.irq
, &psc_i2s
->capture
);
415 free_irq(psc_i2s
->playback
.irq
, &psc_i2s
->playback
);
420 * psc_i2s_set_sysclk: set the clock frequency and direction
422 * This function is called by the machine driver to tell us what the clock
423 * frequency and direction are.
425 * Currently, we only support operating as a clock slave (SND_SOC_CLOCK_IN),
426 * and we don't care about the frequency. Return an error if the direction
427 * is not SND_SOC_CLOCK_IN.
429 * @clk_id: reserved, should be zero
430 * @freq: the frequency of the given clock ID, currently ignored
431 * @dir: SND_SOC_CLOCK_IN (clock slave) or SND_SOC_CLOCK_OUT (clock master)
433 static int psc_i2s_set_sysclk(struct snd_soc_dai
*cpu_dai
,
434 int clk_id
, unsigned int freq
, int dir
)
436 struct psc_i2s
*psc_i2s
= cpu_dai
->private_data
;
437 dev_dbg(psc_i2s
->dev
, "psc_i2s_set_sysclk(cpu_dai=%p, dir=%i)\n",
439 return (dir
== SND_SOC_CLOCK_IN
) ? 0 : -EINVAL
;
443 * psc_i2s_set_fmt: set the serial format.
445 * This function is called by the machine driver to tell us what serial
448 * This driver only supports I2S mode. Return an error if the format is
449 * not SND_SOC_DAIFMT_I2S.
451 * @format: one of SND_SOC_DAIFMT_xxx
453 static int psc_i2s_set_fmt(struct snd_soc_dai
*cpu_dai
, unsigned int format
)
455 struct psc_i2s
*psc_i2s
= cpu_dai
->private_data
;
456 dev_dbg(psc_i2s
->dev
, "psc_i2s_set_fmt(cpu_dai=%p, format=%i)\n",
458 return (format
== SND_SOC_DAIFMT_I2S
) ? 0 : -EINVAL
;
461 /* ---------------------------------------------------------------------
464 * - Digital Audio Interface (DAI) template
465 * - create/destroy dai hooks
469 * psc_i2s_dai_template: template CPU Digital Audio Interface
471 static struct snd_soc_dai_ops psc_i2s_dai_ops
= {
472 .startup
= psc_i2s_startup
,
473 .hw_params
= psc_i2s_hw_params
,
474 .hw_free
= psc_i2s_hw_free
,
475 .shutdown
= psc_i2s_shutdown
,
476 .trigger
= psc_i2s_trigger
,
477 .set_sysclk
= psc_i2s_set_sysclk
,
478 .set_fmt
= psc_i2s_set_fmt
,
481 static struct snd_soc_dai psc_i2s_dai_template
= {
485 .rates
= PSC_I2S_RATES
,
486 .formats
= PSC_I2S_FORMATS
,
491 .rates
= PSC_I2S_RATES
,
492 .formats
= PSC_I2S_FORMATS
,
494 .ops
= &psc_i2s_dai_ops
,
497 /* ---------------------------------------------------------------------
498 * The PSC I2S 'ASoC platform' driver
500 * Can be referenced by an 'ASoC machine' driver
501 * This driver only deals with the audio bus; it doesn't have any
502 * interaction with the attached codec
505 static const struct snd_pcm_hardware psc_i2s_pcm_hardware
= {
506 .info
= SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_MMAP_VALID
|
507 SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_BLOCK_TRANSFER
,
508 .formats
= SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_S16_BE
|
509 SNDRV_PCM_FMTBIT_S24_BE
| SNDRV_PCM_FMTBIT_S32_BE
,
514 .period_bytes_max
= 1024 * 1024,
515 .period_bytes_min
= 32,
518 .buffer_bytes_max
= 2 * 1024 * 1024,
522 static int psc_i2s_pcm_open(struct snd_pcm_substream
*substream
)
524 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
525 struct psc_i2s
*psc_i2s
= rtd
->dai
->cpu_dai
->private_data
;
526 struct psc_i2s_stream
*s
;
528 dev_dbg(psc_i2s
->dev
, "psc_i2s_pcm_open(substream=%p)\n", substream
);
530 if (substream
->pstr
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
531 s
= &psc_i2s
->capture
;
533 s
= &psc_i2s
->playback
;
535 snd_soc_set_runtime_hwparams(substream
, &psc_i2s_pcm_hardware
);
537 s
->stream
= substream
;
541 static int psc_i2s_pcm_close(struct snd_pcm_substream
*substream
)
543 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
544 struct psc_i2s
*psc_i2s
= rtd
->dai
->cpu_dai
->private_data
;
545 struct psc_i2s_stream
*s
;
547 dev_dbg(psc_i2s
->dev
, "psc_i2s_pcm_close(substream=%p)\n", substream
);
549 if (substream
->pstr
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
550 s
= &psc_i2s
->capture
;
552 s
= &psc_i2s
->playback
;
558 static snd_pcm_uframes_t
559 psc_i2s_pcm_pointer(struct snd_pcm_substream
*substream
)
561 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
562 struct psc_i2s
*psc_i2s
= rtd
->dai
->cpu_dai
->private_data
;
563 struct psc_i2s_stream
*s
;
566 if (substream
->pstr
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
567 s
= &psc_i2s
->capture
;
569 s
= &psc_i2s
->playback
;
571 count
= s
->period_current_pt
- s
->period_start
;
573 return bytes_to_frames(substream
->runtime
, count
);
576 static struct snd_pcm_ops psc_i2s_pcm_ops
= {
577 .open
= psc_i2s_pcm_open
,
578 .close
= psc_i2s_pcm_close
,
579 .ioctl
= snd_pcm_lib_ioctl
,
580 .pointer
= psc_i2s_pcm_pointer
,
583 static u64 psc_i2s_pcm_dmamask
= 0xffffffff;
584 static int psc_i2s_pcm_new(struct snd_card
*card
, struct snd_soc_dai
*dai
,
587 struct snd_soc_pcm_runtime
*rtd
= pcm
->private_data
;
588 size_t size
= psc_i2s_pcm_hardware
.buffer_bytes_max
;
591 dev_dbg(rtd
->socdev
->dev
, "psc_i2s_pcm_new(card=%p, dai=%p, pcm=%p)\n",
594 if (!card
->dev
->dma_mask
)
595 card
->dev
->dma_mask
= &psc_i2s_pcm_dmamask
;
596 if (!card
->dev
->coherent_dma_mask
)
597 card
->dev
->coherent_dma_mask
= 0xffffffff;
599 if (pcm
->streams
[0].substream
) {
600 rc
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, pcm
->dev
, size
,
601 &pcm
->streams
[0].substream
->dma_buffer
);
603 goto playback_alloc_err
;
606 if (pcm
->streams
[1].substream
) {
607 rc
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, pcm
->dev
, size
,
608 &pcm
->streams
[1].substream
->dma_buffer
);
610 goto capture_alloc_err
;
616 if (pcm
->streams
[0].substream
)
617 snd_dma_free_pages(&pcm
->streams
[0].substream
->dma_buffer
);
619 dev_err(card
->dev
, "Cannot allocate buffer(s)\n");
623 static void psc_i2s_pcm_free(struct snd_pcm
*pcm
)
625 struct snd_soc_pcm_runtime
*rtd
= pcm
->private_data
;
626 struct snd_pcm_substream
*substream
;
629 dev_dbg(rtd
->socdev
->dev
, "psc_i2s_pcm_free(pcm=%p)\n", pcm
);
631 for (stream
= 0; stream
< 2; stream
++) {
632 substream
= pcm
->streams
[stream
].substream
;
634 snd_dma_free_pages(&substream
->dma_buffer
);
635 substream
->dma_buffer
.area
= NULL
;
636 substream
->dma_buffer
.addr
= 0;
641 struct snd_soc_platform psc_i2s_pcm_soc_platform
= {
642 .name
= "mpc5200-psc-audio",
643 .pcm_ops
= &psc_i2s_pcm_ops
,
644 .pcm_new
= &psc_i2s_pcm_new
,
645 .pcm_free
= &psc_i2s_pcm_free
,
648 /* ---------------------------------------------------------------------
649 * Sysfs attributes for debugging
652 static ssize_t
psc_i2s_status_show(struct device
*dev
,
653 struct device_attribute
*attr
, char *buf
)
655 struct psc_i2s
*psc_i2s
= dev_get_drvdata(dev
);
657 return sprintf(buf
, "status=%.4x sicr=%.8x rfnum=%i rfstat=0x%.4x "
658 "tfnum=%i tfstat=0x%.4x\n",
659 in_be16(&psc_i2s
->psc_regs
->sr_csr
.status
),
660 in_be32(&psc_i2s
->psc_regs
->sicr
),
661 in_be16(&psc_i2s
->fifo_regs
->rfnum
) & 0x1ff,
662 in_be16(&psc_i2s
->fifo_regs
->rfstat
),
663 in_be16(&psc_i2s
->fifo_regs
->tfnum
) & 0x1ff,
664 in_be16(&psc_i2s
->fifo_regs
->tfstat
));
667 static int *psc_i2s_get_stat_attr(struct psc_i2s
*psc_i2s
, const char *name
)
669 if (strcmp(name
, "playback_underrun") == 0)
670 return &psc_i2s
->stats
.underrun_count
;
671 if (strcmp(name
, "capture_overrun") == 0)
672 return &psc_i2s
->stats
.overrun_count
;
677 static ssize_t
psc_i2s_stat_show(struct device
*dev
,
678 struct device_attribute
*attr
, char *buf
)
680 struct psc_i2s
*psc_i2s
= dev_get_drvdata(dev
);
683 attrib
= psc_i2s_get_stat_attr(psc_i2s
, attr
->attr
.name
);
687 return sprintf(buf
, "%i\n", *attrib
);
690 static ssize_t
psc_i2s_stat_store(struct device
*dev
,
691 struct device_attribute
*attr
,
695 struct psc_i2s
*psc_i2s
= dev_get_drvdata(dev
);
698 attrib
= psc_i2s_get_stat_attr(psc_i2s
, attr
->attr
.name
);
702 *attrib
= simple_strtoul(buf
, NULL
, 0);
706 static DEVICE_ATTR(status
, 0644, psc_i2s_status_show
, NULL
);
707 static DEVICE_ATTR(playback_underrun
, 0644, psc_i2s_stat_show
,
709 static DEVICE_ATTR(capture_overrun
, 0644, psc_i2s_stat_show
,
712 /* ---------------------------------------------------------------------
713 * OF platform bus binding code:
714 * - Probe/remove operations
715 * - OF device match table
717 static int __devinit
psc_i2s_of_probe(struct of_device
*op
,
718 const struct of_device_id
*match
)
721 struct psc_i2s
*psc_i2s
;
723 int size
, psc_id
, irq
, rc
;
727 dev_dbg(&op
->dev
, "probing psc i2s device\n");
730 prop
= of_get_property(op
->node
, "cell-index", &size
);
731 if (!prop
|| size
< sizeof *prop
)
733 psc_id
= be32_to_cpu(*prop
);
735 /* Fetch the registers and IRQ of the PSC */
736 irq
= irq_of_parse_and_map(op
->node
, 0);
737 if (of_address_to_resource(op
->node
, 0, &res
)) {
738 dev_err(&op
->dev
, "Missing reg property\n");
741 regs
= ioremap(res
.start
, 1 + res
.end
- res
.start
);
743 dev_err(&op
->dev
, "Could not map registers\n");
747 /* Allocate and initialize the driver private data */
748 psc_i2s
= kzalloc(sizeof *psc_i2s
, GFP_KERNEL
);
753 spin_lock_init(&psc_i2s
->lock
);
755 psc_i2s
->psc_regs
= regs
;
756 psc_i2s
->fifo_regs
= regs
+ sizeof *psc_i2s
->psc_regs
;
757 psc_i2s
->dev
= &op
->dev
;
758 psc_i2s
->playback
.psc_i2s
= psc_i2s
;
759 psc_i2s
->capture
.psc_i2s
= psc_i2s
;
760 snprintf(psc_i2s
->name
, sizeof psc_i2s
->name
, "PSC%u", psc_id
+1);
762 /* Fill out the CPU DAI structure */
763 memcpy(&psc_i2s
->dai
, &psc_i2s_dai_template
, sizeof psc_i2s
->dai
);
764 psc_i2s
->dai
.private_data
= psc_i2s
;
765 psc_i2s
->dai
.name
= psc_i2s
->name
;
766 psc_i2s
->dai
.id
= psc_id
;
768 /* Find the address of the fifo data registers and setup the
770 fifo
= res
.start
+ offsetof(struct mpc52xx_psc
, buffer
.buffer_32
);
771 psc_i2s
->capture
.bcom_task
=
772 bcom_psc_gen_bd_rx_init(psc_id
, 10, fifo
, 512);
773 psc_i2s
->playback
.bcom_task
=
774 bcom_psc_gen_bd_tx_init(psc_id
, 10, fifo
);
775 if (!psc_i2s
->capture
.bcom_task
||
776 !psc_i2s
->playback
.bcom_task
) {
777 dev_err(&op
->dev
, "Could not allocate bestcomm tasks\n");
783 /* Disable all interrupts and reset the PSC */
784 out_be16(&psc_i2s
->psc_regs
->isr_imr
.imr
, 0);
785 out_8(&psc_i2s
->psc_regs
->command
, 3 << 4); /* reset transmitter */
786 out_8(&psc_i2s
->psc_regs
->command
, 2 << 4); /* reset receiver */
787 out_8(&psc_i2s
->psc_regs
->command
, 1 << 4); /* reset mode */
788 out_8(&psc_i2s
->psc_regs
->command
, 4 << 4); /* reset error */
790 /* Configure the serial interface mode; defaulting to CODEC8 mode */
791 psc_i2s
->sicr
= MPC52xx_PSC_SICR_DTS1
| MPC52xx_PSC_SICR_I2S
|
792 MPC52xx_PSC_SICR_CLKPOL
;
793 if (of_get_property(op
->node
, "fsl,cellslave", NULL
))
794 psc_i2s
->sicr
|= MPC52xx_PSC_SICR_CELLSLAVE
|
795 MPC52xx_PSC_SICR_GENCLK
;
796 out_be32(&psc_i2s
->psc_regs
->sicr
,
797 psc_i2s
->sicr
| MPC52xx_PSC_SICR_SIM_CODEC_8
);
799 /* Check for the codec handle. If it is not present then we
801 if (!of_get_property(op
->node
, "codec-handle", NULL
))
804 /* Set up mode register;
805 * First write: RxRdy (FIFO Alarm) generates rx FIFO irq
806 * Second write: register Normal mode for non loopback
808 out_8(&psc_i2s
->psc_regs
->mode
, 0);
809 out_8(&psc_i2s
->psc_regs
->mode
, 0);
811 /* Set the TX and RX fifo alarm thresholds */
812 out_be16(&psc_i2s
->fifo_regs
->rfalarm
, 0x100);
813 out_8(&psc_i2s
->fifo_regs
->rfcntl
, 0x4);
814 out_be16(&psc_i2s
->fifo_regs
->tfalarm
, 0x100);
815 out_8(&psc_i2s
->fifo_regs
->tfcntl
, 0x7);
817 /* Lookup the IRQ numbers */
818 psc_i2s
->playback
.irq
=
819 bcom_get_task_irq(psc_i2s
->playback
.bcom_task
);
820 psc_i2s
->capture
.irq
=
821 bcom_get_task_irq(psc_i2s
->capture
.bcom_task
);
823 /* Save what we've done so it can be found again later */
824 dev_set_drvdata(&op
->dev
, psc_i2s
);
826 /* Register the SYSFS files */
827 rc
= device_create_file(psc_i2s
->dev
, &dev_attr_status
);
828 rc
|= device_create_file(psc_i2s
->dev
, &dev_attr_capture_overrun
);
829 rc
|= device_create_file(psc_i2s
->dev
, &dev_attr_playback_underrun
);
831 dev_info(psc_i2s
->dev
, "error creating sysfs files\n");
833 snd_soc_register_platform(&psc_i2s_pcm_soc_platform
);
835 /* Tell the ASoC OF helpers about it */
836 of_snd_soc_register_platform(&psc_i2s_pcm_soc_platform
, op
->node
,
842 static int __devexit
psc_i2s_of_remove(struct of_device
*op
)
844 struct psc_i2s
*psc_i2s
= dev_get_drvdata(&op
->dev
);
846 dev_dbg(&op
->dev
, "psc_i2s_remove()\n");
848 snd_soc_unregister_platform(&psc_i2s_pcm_soc_platform
);
850 bcom_gen_bd_rx_release(psc_i2s
->capture
.bcom_task
);
851 bcom_gen_bd_tx_release(psc_i2s
->playback
.bcom_task
);
853 iounmap(psc_i2s
->psc_regs
);
854 iounmap(psc_i2s
->fifo_regs
);
856 dev_set_drvdata(&op
->dev
, NULL
);
861 /* Match table for of_platform binding */
862 static struct of_device_id psc_i2s_match
[] __devinitdata
= {
863 { .compatible
= "fsl,mpc5200-psc-i2s", },
866 MODULE_DEVICE_TABLE(of
, psc_i2s_match
);
868 static struct of_platform_driver psc_i2s_driver
= {
869 .match_table
= psc_i2s_match
,
870 .probe
= psc_i2s_of_probe
,
871 .remove
= __devexit_p(psc_i2s_of_remove
),
873 .name
= "mpc5200-psc-i2s",
874 .owner
= THIS_MODULE
,
878 /* ---------------------------------------------------------------------
879 * Module setup and teardown; simply register the of_platform driver
880 * for the PSC in I2S mode.
882 static int __init
psc_i2s_init(void)
884 return of_register_platform_driver(&psc_i2s_driver
);
886 module_init(psc_i2s_init
);
888 static void __exit
psc_i2s_exit(void)
890 of_unregister_platform_driver(&psc_i2s_driver
);
892 module_exit(psc_i2s_exit
);