2 * Freescale DMA ALSA SoC PCM driver
4 * Author: Timur Tabi <timur@freescale.com>
6 * Copyright 2007-2008 Freescale Semiconductor, Inc. This file is licensed
7 * under the terms of the GNU General Public License version 2. This
8 * program is licensed "as is" without any warranty of any kind, whether
11 * This driver implements ASoC support for the Elo DMA controller, which is
12 * the DMA controller on Freescale 83xx, 85xx, and 86xx SOCs. In ALSA terms,
13 * the PCM driver is what handles the DMA buffer.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
33 * The formats that the DMA controller supports, which is anything
34 * that is 8, 16, or 32 bits.
36 #define FSLDMA_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
37 SNDRV_PCM_FMTBIT_U8 | \
38 SNDRV_PCM_FMTBIT_S16_LE | \
39 SNDRV_PCM_FMTBIT_S16_BE | \
40 SNDRV_PCM_FMTBIT_U16_LE | \
41 SNDRV_PCM_FMTBIT_U16_BE | \
42 SNDRV_PCM_FMTBIT_S24_LE | \
43 SNDRV_PCM_FMTBIT_S24_BE | \
44 SNDRV_PCM_FMTBIT_U24_LE | \
45 SNDRV_PCM_FMTBIT_U24_BE | \
46 SNDRV_PCM_FMTBIT_S32_LE | \
47 SNDRV_PCM_FMTBIT_S32_BE | \
48 SNDRV_PCM_FMTBIT_U32_LE | \
49 SNDRV_PCM_FMTBIT_U32_BE)
51 #define FSLDMA_PCM_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
52 SNDRV_PCM_RATE_CONTINUOUS)
54 /* DMA global data. This structure is used by fsl_dma_open() to determine
55 * which DMA channels to assign to a substream. Unfortunately, ASoC V1 does
56 * not allow the machine driver to provide this information to the PCM
57 * driver in advance, and there's no way to differentiate between the two
58 * DMA controllers. So for now, this driver only supports one SSI device
59 * using two DMA channels. We cannot support multiple DMA devices.
61 * ssi_stx_phys: bus address of SSI STX register
62 * ssi_srx_phys: bus address of SSI SRX register
63 * dma_channel: pointer to the DMA channel's registers
64 * irq: IRQ for this DMA channel
65 * assigned: set to 1 if that DMA channel is assigned to a substream
68 dma_addr_t ssi_stx_phys
;
69 dma_addr_t ssi_srx_phys
;
70 struct ccsr_dma_channel __iomem
*dma_channel
[2];
72 unsigned int assigned
[2];
76 * The number of DMA links to use. Two is the bare minimum, but if you
77 * have really small links you might need more.
79 #define NUM_DMA_LINKS 2
81 /** fsl_dma_private: p-substream DMA data
83 * Each substream has a 1-to-1 association with a DMA channel.
85 * The link[] array is first because it needs to be aligned on a 32-byte
86 * boundary, so putting it first will ensure alignment without padding the
89 * @link[]: array of link descriptors
90 * @controller_id: which DMA controller (0, 1, ...)
91 * @channel_id: which DMA channel on the controller (0, 1, 2, ...)
92 * @dma_channel: pointer to the DMA channel's registers
93 * @irq: IRQ for this DMA channel
94 * @substream: pointer to the substream object, needed by the ISR
95 * @ssi_sxx_phys: bus address of the STX or SRX register to use
96 * @ld_buf_phys: physical address of the LD buffer
97 * @current_link: index into link[] of the link currently being processed
98 * @dma_buf_phys: physical address of the DMA buffer
99 * @dma_buf_next: physical address of the next period to process
100 * @dma_buf_end: physical address of the byte after the end of the DMA
101 * @buffer period_size: the size of a single period
102 * @num_periods: the number of periods in the DMA buffer
104 struct fsl_dma_private
{
105 struct fsl_dma_link_descriptor link
[NUM_DMA_LINKS
];
106 unsigned int controller_id
;
107 unsigned int channel_id
;
108 struct ccsr_dma_channel __iomem
*dma_channel
;
110 struct snd_pcm_substream
*substream
;
111 dma_addr_t ssi_sxx_phys
;
112 dma_addr_t ld_buf_phys
;
113 unsigned int current_link
;
114 dma_addr_t dma_buf_phys
;
115 dma_addr_t dma_buf_next
;
116 dma_addr_t dma_buf_end
;
118 unsigned int num_periods
;
122 * fsl_dma_hardare: define characteristics of the PCM hardware.
124 * The PCM hardware is the Freescale DMA controller. This structure defines
125 * the capabilities of that hardware.
127 * Since the sampling rate and data format are not controlled by the DMA
128 * controller, we specify no limits for those values. The only exception is
129 * period_bytes_min, which is set to a reasonably low value to prevent the
130 * DMA controller from generating too many interrupts per second.
132 * Since each link descriptor has a 32-bit byte count field, we set
133 * period_bytes_max to the largest 32-bit number. We also have no maximum
136 static const struct snd_pcm_hardware fsl_dma_hardware
= {
138 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
139 SNDRV_PCM_INFO_MMAP
|
140 SNDRV_PCM_INFO_MMAP_VALID
,
141 .formats
= FSLDMA_PCM_FORMATS
,
142 .rates
= FSLDMA_PCM_RATES
,
145 .period_bytes_min
= 512, /* A reasonable limit */
146 .period_bytes_max
= (u32
) -1,
147 .periods_min
= NUM_DMA_LINKS
,
148 .periods_max
= (unsigned int) -1,
149 .buffer_bytes_max
= 128 * 1024, /* A reasonable limit */
153 * fsl_dma_abort_stream: tell ALSA that the DMA transfer has aborted
155 * This function should be called by the ISR whenever the DMA controller
156 * halts data transfer.
158 static void fsl_dma_abort_stream(struct snd_pcm_substream
*substream
)
162 snd_pcm_stream_lock_irqsave(substream
, flags
);
164 if (snd_pcm_running(substream
))
165 snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
167 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
171 * fsl_dma_update_pointers - update LD pointers to point to the next period
173 * As each period is completed, this function changes the the link
174 * descriptor pointers for that period to point to the next period.
176 static void fsl_dma_update_pointers(struct fsl_dma_private
*dma_private
)
178 struct fsl_dma_link_descriptor
*link
=
179 &dma_private
->link
[dma_private
->current_link
];
181 /* Update our link descriptors to point to the next period */
182 if (dma_private
->substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
184 cpu_to_be32(dma_private
->dma_buf_next
);
187 cpu_to_be32(dma_private
->dma_buf_next
);
189 /* Update our variables for next time */
190 dma_private
->dma_buf_next
+= dma_private
->period_size
;
192 if (dma_private
->dma_buf_next
>= dma_private
->dma_buf_end
)
193 dma_private
->dma_buf_next
= dma_private
->dma_buf_phys
;
195 if (++dma_private
->current_link
>= NUM_DMA_LINKS
)
196 dma_private
->current_link
= 0;
200 * fsl_dma_isr: interrupt handler for the DMA controller
202 * @irq: IRQ of the DMA channel
203 * @dev_id: pointer to the dma_private structure for this DMA channel
205 static irqreturn_t
fsl_dma_isr(int irq
, void *dev_id
)
207 struct fsl_dma_private
*dma_private
= dev_id
;
208 struct ccsr_dma_channel __iomem
*dma_channel
= dma_private
->dma_channel
;
209 irqreturn_t ret
= IRQ_NONE
;
212 /* We got an interrupt, so read the status register to see what we
213 were interrupted for.
215 sr
= in_be32(&dma_channel
->sr
);
217 if (sr
& CCSR_DMA_SR_TE
) {
218 dev_err(dma_private
->substream
->pcm
->card
->dev
,
219 "DMA transmit error (controller=%u channel=%u irq=%u\n",
220 dma_private
->controller_id
,
221 dma_private
->channel_id
, irq
);
222 fsl_dma_abort_stream(dma_private
->substream
);
223 sr2
|= CCSR_DMA_SR_TE
;
227 if (sr
& CCSR_DMA_SR_CH
)
230 if (sr
& CCSR_DMA_SR_PE
) {
231 dev_err(dma_private
->substream
->pcm
->card
->dev
,
232 "DMA%u programming error (channel=%u irq=%u)\n",
233 dma_private
->controller_id
,
234 dma_private
->channel_id
, irq
);
235 fsl_dma_abort_stream(dma_private
->substream
);
236 sr2
|= CCSR_DMA_SR_PE
;
240 if (sr
& CCSR_DMA_SR_EOLNI
) {
241 sr2
|= CCSR_DMA_SR_EOLNI
;
245 if (sr
& CCSR_DMA_SR_CB
)
248 if (sr
& CCSR_DMA_SR_EOSI
) {
249 struct snd_pcm_substream
*substream
= dma_private
->substream
;
251 /* Tell ALSA we completed a period. */
252 snd_pcm_period_elapsed(substream
);
255 * Update our link descriptors to point to the next period. We
256 * only need to do this if the number of periods is not equal to
257 * the number of links.
259 if (dma_private
->num_periods
!= NUM_DMA_LINKS
)
260 fsl_dma_update_pointers(dma_private
);
262 sr2
|= CCSR_DMA_SR_EOSI
;
266 if (sr
& CCSR_DMA_SR_EOLSI
) {
267 sr2
|= CCSR_DMA_SR_EOLSI
;
271 /* Clear the bits that we set */
273 out_be32(&dma_channel
->sr
, sr2
);
279 * fsl_dma_new: initialize this PCM driver.
281 * This function is called when the codec driver calls snd_soc_new_pcms(),
282 * once for each .dai_link in the machine driver's snd_soc_machine
285 static int fsl_dma_new(struct snd_card
*card
, struct snd_soc_codec_dai
*dai
,
288 static u64 fsl_dma_dmamask
= DMA_BIT_MASK(32);
291 if (!card
->dev
->dma_mask
)
292 card
->dev
->dma_mask
= &fsl_dma_dmamask
;
294 if (!card
->dev
->coherent_dma_mask
)
295 card
->dev
->coherent_dma_mask
= fsl_dma_dmamask
;
297 ret
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, pcm
->dev
,
298 fsl_dma_hardware
.buffer_bytes_max
,
299 &pcm
->streams
[0].substream
->dma_buffer
);
302 "Can't allocate playback DMA buffer (size=%u)\n",
303 fsl_dma_hardware
.buffer_bytes_max
);
307 ret
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
, pcm
->dev
,
308 fsl_dma_hardware
.buffer_bytes_max
,
309 &pcm
->streams
[1].substream
->dma_buffer
);
311 snd_dma_free_pages(&pcm
->streams
[0].substream
->dma_buffer
);
313 "Can't allocate capture DMA buffer (size=%u)\n",
314 fsl_dma_hardware
.buffer_bytes_max
);
322 * fsl_dma_open: open a new substream.
324 * Each substream has its own DMA buffer.
326 static int fsl_dma_open(struct snd_pcm_substream
*substream
)
328 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
329 struct fsl_dma_private
*dma_private
;
330 dma_addr_t ld_buf_phys
;
331 unsigned int channel
;
335 * Reject any DMA buffer whose size is not a multiple of the period
336 * size. We need to make sure that the DMA buffer can be evenly divided
339 ret
= snd_pcm_hw_constraint_integer(runtime
,
340 SNDRV_PCM_HW_PARAM_PERIODS
);
342 dev_err(substream
->pcm
->card
->dev
, "invalid buffer size\n");
346 channel
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
? 0 : 1;
348 if (dma_global_data
.assigned
[channel
]) {
349 dev_err(substream
->pcm
->card
->dev
,
350 "DMA channel already assigned\n");
354 dma_private
= dma_alloc_coherent(substream
->pcm
->dev
,
355 sizeof(struct fsl_dma_private
), &ld_buf_phys
, GFP_KERNEL
);
357 dev_err(substream
->pcm
->card
->dev
,
358 "can't allocate DMA private data\n");
361 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
362 dma_private
->ssi_sxx_phys
= dma_global_data
.ssi_stx_phys
;
364 dma_private
->ssi_sxx_phys
= dma_global_data
.ssi_srx_phys
;
366 dma_private
->dma_channel
= dma_global_data
.dma_channel
[channel
];
367 dma_private
->irq
= dma_global_data
.irq
[channel
];
368 dma_private
->substream
= substream
;
369 dma_private
->ld_buf_phys
= ld_buf_phys
;
370 dma_private
->dma_buf_phys
= substream
->dma_buffer
.addr
;
372 /* We only support one DMA controller for now */
373 dma_private
->controller_id
= 0;
374 dma_private
->channel_id
= channel
;
376 ret
= request_irq(dma_private
->irq
, fsl_dma_isr
, 0, "DMA", dma_private
);
378 dev_err(substream
->pcm
->card
->dev
,
379 "can't register ISR for IRQ %u (ret=%i)\n",
380 dma_private
->irq
, ret
);
381 dma_free_coherent(substream
->pcm
->dev
,
382 sizeof(struct fsl_dma_private
),
383 dma_private
, dma_private
->ld_buf_phys
);
387 dma_global_data
.assigned
[channel
] = 1;
389 snd_pcm_set_runtime_buffer(substream
, &substream
->dma_buffer
);
390 snd_soc_set_runtime_hwparams(substream
, &fsl_dma_hardware
);
391 runtime
->private_data
= dma_private
;
397 * fsl_dma_hw_params: allocate the DMA buffer and the DMA link descriptors.
399 * ALSA divides the DMA buffer into N periods. We create NUM_DMA_LINKS link
400 * descriptors that ping-pong from one period to the next. For example, if
401 * there are six periods and two link descriptors, this is how they look
402 * before playback starts:
404 * The last link descriptor
405 * ____________ points back to the first
414 * _________________________________________
415 * | | | | | | | The DMA buffer is
416 * | | | | | | | divided into 6 parts
417 * |______|______|______|______|______|______|
419 * and here's how they look after the first period is finished playing:
431 * _________________________________________
434 * |______|______|______|______|______|______|
436 * The first link descriptor now points to the third period. The DMA
437 * controller is currently playing the second period. When it finishes, it
438 * will jump back to the first descriptor and play the third period.
440 * There are four reasons we do this:
442 * 1. The only way to get the DMA controller to automatically restart the
443 * transfer when it gets to the end of the buffer is to use chaining
444 * mode. Basic direct mode doesn't offer that feature.
445 * 2. We need to receive an interrupt at the end of every period. The DMA
446 * controller can generate an interrupt at the end of every link transfer
447 * (aka segment). Making each period into a DMA segment will give us the
448 * interrupts we need.
449 * 3. By creating only two link descriptors, regardless of the number of
450 * periods, we do not need to reallocate the link descriptors if the
451 * number of periods changes.
452 * 4. All of the audio data is still stored in a single, contiguous DMA
453 * buffer, which is what ALSA expects. We're just dividing it into
454 * contiguous parts, and creating a link descriptor for each one.
456 * Note that due to a quirk of the SSI's STX register, the target address
457 * for the DMA operations depends on the sample size. So we don't program
458 * the dest_addr (for playback -- source_addr for capture) fields in the
459 * link descriptors here. We do that in fsl_dma_prepare()
461 static int fsl_dma_hw_params(struct snd_pcm_substream
*substream
,
462 struct snd_pcm_hw_params
*hw_params
)
464 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
465 struct fsl_dma_private
*dma_private
= runtime
->private_data
;
466 struct ccsr_dma_channel __iomem
*dma_channel
= dma_private
->dma_channel
;
468 dma_addr_t temp_addr
; /* Pointer to next period */
469 u64 temp_link
; /* Pointer to next link descriptor */
470 u32 mr
; /* Temporary variable for MR register */
474 /* Get all the parameters we need */
475 size_t buffer_size
= params_buffer_bytes(hw_params
);
476 size_t period_size
= params_period_bytes(hw_params
);
478 /* Initialize our DMA tracking variables */
479 dma_private
->period_size
= period_size
;
480 dma_private
->num_periods
= params_periods(hw_params
);
481 dma_private
->dma_buf_end
= dma_private
->dma_buf_phys
+ buffer_size
;
482 dma_private
->dma_buf_next
= dma_private
->dma_buf_phys
+
483 (NUM_DMA_LINKS
* period_size
);
484 if (dma_private
->dma_buf_next
>= dma_private
->dma_buf_end
)
485 dma_private
->dma_buf_next
= dma_private
->dma_buf_phys
;
488 * Initialize each link descriptor.
490 * The actual address in STX0 (destination for playback, source for
491 * capture) is based on the sample size, but we don't know the sample
492 * size in this function, so we'll have to adjust that later. See
493 * comments in fsl_dma_prepare().
495 * The DMA controller does not have a cache, so the CPU does not
496 * need to tell it to flush its cache. However, the DMA
497 * controller does need to tell the CPU to flush its cache.
498 * That's what the SNOOP bit does.
500 * Also, even though the DMA controller supports 36-bit addressing, for
501 * simplicity we currently support only 32-bit addresses for the audio
504 temp_addr
= substream
->dma_buffer
.addr
;
505 temp_link
= dma_private
->ld_buf_phys
+
506 sizeof(struct fsl_dma_link_descriptor
);
508 for (i
= 0; i
< NUM_DMA_LINKS
; i
++) {
509 struct fsl_dma_link_descriptor
*link
= &dma_private
->link
[i
];
511 link
->count
= cpu_to_be32(period_size
);
512 link
->source_attr
= cpu_to_be32(CCSR_DMA_ATR_SNOOP
);
513 link
->dest_attr
= cpu_to_be32(CCSR_DMA_ATR_SNOOP
);
514 link
->next
= cpu_to_be64(temp_link
);
516 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
517 link
->source_addr
= cpu_to_be32(temp_addr
);
519 link
->dest_addr
= cpu_to_be32(temp_addr
);
521 temp_addr
+= period_size
;
522 temp_link
+= sizeof(struct fsl_dma_link_descriptor
);
524 /* The last link descriptor points to the first */
525 dma_private
->link
[i
- 1].next
= cpu_to_be64(dma_private
->ld_buf_phys
);
527 /* Tell the DMA controller where the first link descriptor is */
528 out_be32(&dma_channel
->clndar
,
529 CCSR_DMA_CLNDAR_ADDR(dma_private
->ld_buf_phys
));
530 out_be32(&dma_channel
->eclndar
,
531 CCSR_DMA_ECLNDAR_ADDR(dma_private
->ld_buf_phys
));
533 /* The manual says the BCR must be clear before enabling EMP */
534 out_be32(&dma_channel
->bcr
, 0);
537 * Program the mode register for interrupts, external master control,
538 * and source/destination hold. Also clear the Channel Abort bit.
540 mr
= in_be32(&dma_channel
->mr
) &
541 ~(CCSR_DMA_MR_CA
| CCSR_DMA_MR_DAHE
| CCSR_DMA_MR_SAHE
);
544 * We want External Master Start and External Master Pause enabled,
545 * because the SSI is controlling the DMA controller. We want the DMA
546 * controller to be set up in advance, and then we signal only the SSI
547 * to start transfering.
549 * We want End-Of-Segment Interrupts enabled, because this will generate
550 * an interrupt at the end of each segment (each link descriptor
551 * represents one segment). Each DMA segment is the same thing as an
552 * ALSA period, so this is how we get an interrupt at the end of every
555 * We want Error Interrupt enabled, so that we can get an error if
556 * the DMA controller is mis-programmed somehow.
558 mr
|= CCSR_DMA_MR_EOSIE
| CCSR_DMA_MR_EIE
| CCSR_DMA_MR_EMP_EN
|
561 /* For playback, we want the destination address to be held. For
562 capture, set the source address to be held. */
563 mr
|= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) ?
564 CCSR_DMA_MR_DAHE
: CCSR_DMA_MR_SAHE
;
566 out_be32(&dma_channel
->mr
, mr
);
572 * fsl_dma_prepare - prepare the DMA registers for playback.
574 * This function is called after the specifics of the audio data are known,
575 * i.e. snd_pcm_runtime is initialized.
577 * In this function, we finish programming the registers of the DMA
578 * controller that are dependent on the sample size.
580 * One of the drawbacks with big-endian is that when copying integers of
581 * different sizes to a fixed-sized register, the address to which the
582 * integer must be copied is dependent on the size of the integer.
584 * For example, if P is the address of a 32-bit register, and X is a 32-bit
585 * integer, then X should be copied to address P. However, if X is a 16-bit
586 * integer, then it should be copied to P+2. If X is an 8-bit register,
587 * then it should be copied to P+3.
589 * So for playback of 8-bit samples, the DMA controller must transfer single
590 * bytes from the DMA buffer to the last byte of the STX0 register, i.e.
591 * offset by 3 bytes. For 16-bit samples, the offset is two bytes.
593 * For 24-bit samples, the offset is 1 byte. However, the DMA controller
594 * does not support 3-byte copies (the DAHTS register supports only 1, 2, 4,
595 * and 8 bytes at a time). So we do not support packed 24-bit samples.
596 * 24-bit data must be padded to 32 bits.
598 static int fsl_dma_prepare(struct snd_pcm_substream
*substream
)
600 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
601 struct fsl_dma_private
*dma_private
= runtime
->private_data
;
602 struct ccsr_dma_channel __iomem
*dma_channel
= dma_private
->dma_channel
;
605 dma_addr_t ssi_sxx_phys
; /* Bus address of SSI STX register */
606 unsigned int frame_size
; /* Number of bytes per frame */
608 ssi_sxx_phys
= dma_private
->ssi_sxx_phys
;
610 mr
= in_be32(&dma_channel
->mr
) & ~(CCSR_DMA_MR_BWC_MASK
|
611 CCSR_DMA_MR_SAHTS_MASK
| CCSR_DMA_MR_DAHTS_MASK
);
613 switch (runtime
->sample_bits
) {
615 mr
|= CCSR_DMA_MR_DAHTS_1
| CCSR_DMA_MR_SAHTS_1
;
619 mr
|= CCSR_DMA_MR_DAHTS_2
| CCSR_DMA_MR_SAHTS_2
;
623 mr
|= CCSR_DMA_MR_DAHTS_4
| CCSR_DMA_MR_SAHTS_4
;
626 dev_err(substream
->pcm
->card
->dev
,
627 "unsupported sample size %u\n", runtime
->sample_bits
);
631 frame_size
= runtime
->frame_bits
/ 8;
633 * BWC should always be a multiple of the frame size. BWC determines
634 * how many bytes are sent/received before the DMA controller checks the
635 * SSI to see if it needs to stop. For playback, the transmit FIFO can
636 * hold three frames, so we want to send two frames at a time. For
637 * capture, the receive FIFO is triggered when it contains one frame, so
638 * we want to receive one frame at a time.
641 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
642 mr
|= CCSR_DMA_MR_BWC(2 * frame_size
);
644 mr
|= CCSR_DMA_MR_BWC(frame_size
);
646 out_be32(&dma_channel
->mr
, mr
);
649 * Program the address of the DMA transfer to/from the SSI.
651 for (i
= 0; i
< NUM_DMA_LINKS
; i
++) {
652 struct fsl_dma_link_descriptor
*link
= &dma_private
->link
[i
];
654 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
655 link
->dest_addr
= cpu_to_be32(ssi_sxx_phys
);
657 link
->source_addr
= cpu_to_be32(ssi_sxx_phys
);
664 * fsl_dma_pointer: determine the current position of the DMA transfer
666 * This function is called by ALSA when ALSA wants to know where in the
667 * stream buffer the hardware currently is.
669 * For playback, the SAR register contains the physical address of the most
670 * recent DMA transfer. For capture, the value is in the DAR register.
672 * The base address of the buffer is stored in the source_addr field of the
673 * first link descriptor.
675 static snd_pcm_uframes_t
fsl_dma_pointer(struct snd_pcm_substream
*substream
)
677 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
678 struct fsl_dma_private
*dma_private
= runtime
->private_data
;
679 struct ccsr_dma_channel __iomem
*dma_channel
= dma_private
->dma_channel
;
681 snd_pcm_uframes_t frames
;
683 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
684 position
= in_be32(&dma_channel
->sar
);
686 position
= in_be32(&dma_channel
->dar
);
688 frames
= bytes_to_frames(runtime
, position
- dma_private
->dma_buf_phys
);
691 * If the current address is just past the end of the buffer, wrap it
694 if (frames
== runtime
->buffer_size
)
701 * fsl_dma_hw_free: release resources allocated in fsl_dma_hw_params()
703 * Release the resources allocated in fsl_dma_hw_params() and de-program the
706 * This function can be called multiple times.
708 static int fsl_dma_hw_free(struct snd_pcm_substream
*substream
)
710 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
711 struct fsl_dma_private
*dma_private
= runtime
->private_data
;
714 struct ccsr_dma_channel __iomem
*dma_channel
;
716 dma_channel
= dma_private
->dma_channel
;
719 out_be32(&dma_channel
->mr
, CCSR_DMA_MR_CA
);
720 out_be32(&dma_channel
->mr
, 0);
722 /* Reset all the other registers */
723 out_be32(&dma_channel
->sr
, -1);
724 out_be32(&dma_channel
->clndar
, 0);
725 out_be32(&dma_channel
->eclndar
, 0);
726 out_be32(&dma_channel
->satr
, 0);
727 out_be32(&dma_channel
->sar
, 0);
728 out_be32(&dma_channel
->datr
, 0);
729 out_be32(&dma_channel
->dar
, 0);
730 out_be32(&dma_channel
->bcr
, 0);
731 out_be32(&dma_channel
->nlndar
, 0);
732 out_be32(&dma_channel
->enlndar
, 0);
739 * fsl_dma_close: close the stream.
741 static int fsl_dma_close(struct snd_pcm_substream
*substream
)
743 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
744 struct fsl_dma_private
*dma_private
= runtime
->private_data
;
745 int dir
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
? 0 : 1;
748 if (dma_private
->irq
)
749 free_irq(dma_private
->irq
, dma_private
);
751 if (dma_private
->ld_buf_phys
) {
752 dma_unmap_single(substream
->pcm
->dev
,
753 dma_private
->ld_buf_phys
,
754 sizeof(dma_private
->link
), DMA_TO_DEVICE
);
757 /* Deallocate the fsl_dma_private structure */
758 dma_free_coherent(substream
->pcm
->dev
,
759 sizeof(struct fsl_dma_private
),
760 dma_private
, dma_private
->ld_buf_phys
);
761 substream
->runtime
->private_data
= NULL
;
764 dma_global_data
.assigned
[dir
] = 0;
770 * Remove this PCM driver.
772 static void fsl_dma_free_dma_buffers(struct snd_pcm
*pcm
)
774 struct snd_pcm_substream
*substream
;
777 for (i
= 0; i
< ARRAY_SIZE(pcm
->streams
); i
++) {
778 substream
= pcm
->streams
[i
].substream
;
780 snd_dma_free_pages(&substream
->dma_buffer
);
781 substream
->dma_buffer
.area
= NULL
;
782 substream
->dma_buffer
.addr
= 0;
787 static struct snd_pcm_ops fsl_dma_ops
= {
788 .open
= fsl_dma_open
,
789 .close
= fsl_dma_close
,
790 .ioctl
= snd_pcm_lib_ioctl
,
791 .hw_params
= fsl_dma_hw_params
,
792 .hw_free
= fsl_dma_hw_free
,
793 .prepare
= fsl_dma_prepare
,
794 .pointer
= fsl_dma_pointer
,
797 struct snd_soc_platform fsl_soc_platform
= {
799 .pcm_ops
= &fsl_dma_ops
,
800 .pcm_new
= fsl_dma_new
,
801 .pcm_free
= fsl_dma_free_dma_buffers
,
803 EXPORT_SYMBOL_GPL(fsl_soc_platform
);
806 * fsl_dma_configure: store the DMA parameters from the fabric driver.
808 * This function is called by the ASoC fabric driver to give us the DMA and
809 * SSI channel information.
811 * Unfortunately, ASoC V1 does make it possible to determine the DMA/SSI
812 * data when a substream is created, so for now we need to store this data
813 * into a global variable. This means that we can only support one DMA
814 * controller, and hence only one SSI.
816 int fsl_dma_configure(struct fsl_dma_info
*dma_info
)
818 static int initialized
;
820 /* We only support one DMA controller for now */
824 dma_global_data
.ssi_stx_phys
= dma_info
->ssi_stx_phys
;
825 dma_global_data
.ssi_srx_phys
= dma_info
->ssi_srx_phys
;
826 dma_global_data
.dma_channel
[0] = dma_info
->dma_channel
[0];
827 dma_global_data
.dma_channel
[1] = dma_info
->dma_channel
[1];
828 dma_global_data
.irq
[0] = dma_info
->dma_irq
[0];
829 dma_global_data
.irq
[1] = dma_info
->dma_irq
[1];
830 dma_global_data
.assigned
[0] = 0;
831 dma_global_data
.assigned
[1] = 0;
836 EXPORT_SYMBOL_GPL(fsl_dma_configure
);
838 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
839 MODULE_DESCRIPTION("Freescale Elo DMA ASoC PCM module");
840 MODULE_LICENSE("GPL");