2 * Copyright (c) 2010 Nuvoton technology corporation.
4 * Wan ZongShun <mcuos.com@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation;version 2 of the License.
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/dma-mapping.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
24 #include <mach/hardware.h>
26 #include "nuc900-audio.h"
28 static const struct snd_pcm_hardware nuc900_pcm_hardware
= {
29 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
30 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
32 SNDRV_PCM_INFO_MMAP_VALID
|
33 SNDRV_PCM_INFO_PAUSE
|
34 SNDRV_PCM_INFO_RESUME
,
35 .buffer_bytes_max
= 4*1024,
36 .period_bytes_min
= 1*1024,
37 .period_bytes_max
= 4*1024,
42 static int nuc900_dma_hw_params(struct snd_pcm_substream
*substream
,
43 struct snd_pcm_hw_params
*params
)
45 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
46 struct nuc900_audio
*nuc900_audio
= runtime
->private_data
;
50 ret
= snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
54 spin_lock_irqsave(&nuc900_audio
->lock
, flags
);
56 nuc900_audio
->substream
= substream
;
57 nuc900_audio
->dma_addr
[substream
->stream
] = runtime
->dma_addr
;
58 nuc900_audio
->buffersize
[substream
->stream
] =
59 params_buffer_bytes(params
);
61 spin_unlock_irqrestore(&nuc900_audio
->lock
, flags
);
66 static void nuc900_update_dma_register(struct snd_pcm_substream
*substream
,
67 dma_addr_t dma_addr
, size_t count
)
69 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
70 struct nuc900_audio
*nuc900_audio
= runtime
->private_data
;
71 void __iomem
*mmio_addr
, *mmio_len
;
73 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
74 mmio_addr
= nuc900_audio
->mmio
+ ACTL_PDSTB
;
75 mmio_len
= nuc900_audio
->mmio
+ ACTL_PDST_LENGTH
;
77 mmio_addr
= nuc900_audio
->mmio
+ ACTL_RDSTB
;
78 mmio_len
= nuc900_audio
->mmio
+ ACTL_RDST_LENGTH
;
81 AUDIO_WRITE(mmio_addr
, dma_addr
);
82 AUDIO_WRITE(mmio_len
, count
);
85 static void nuc900_dma_start(struct snd_pcm_substream
*substream
)
87 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
88 struct nuc900_audio
*nuc900_audio
= runtime
->private_data
;
91 val
= AUDIO_READ(nuc900_audio
->mmio
+ ACTL_CON
);
92 val
|= (T_DMA_IRQ
| R_DMA_IRQ
);
93 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_CON
, val
);
96 static void nuc900_dma_stop(struct snd_pcm_substream
*substream
)
98 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
99 struct nuc900_audio
*nuc900_audio
= runtime
->private_data
;
102 val
= AUDIO_READ(nuc900_audio
->mmio
+ ACTL_CON
);
103 val
&= ~(T_DMA_IRQ
| R_DMA_IRQ
);
104 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_CON
, val
);
107 static irqreturn_t
nuc900_dma_interrupt(int irq
, void *dev_id
)
109 struct snd_pcm_substream
*substream
= dev_id
;
110 struct nuc900_audio
*nuc900_audio
= substream
->runtime
->private_data
;
113 spin_lock(&nuc900_audio
->lock
);
115 val
= AUDIO_READ(nuc900_audio
->mmio
+ ACTL_CON
);
117 if (val
& R_DMA_IRQ
) {
118 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_CON
, val
| R_DMA_IRQ
);
120 val
= AUDIO_READ(nuc900_audio
->mmio
+ ACTL_RSR
);
122 if (val
& R_DMA_MIDDLE_IRQ
) {
123 val
|= R_DMA_MIDDLE_IRQ
;
124 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_RSR
, val
);
127 if (val
& R_DMA_END_IRQ
) {
128 val
|= R_DMA_END_IRQ
;
129 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_RSR
, val
);
131 } else if (val
& T_DMA_IRQ
) {
132 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_CON
, val
| T_DMA_IRQ
);
134 val
= AUDIO_READ(nuc900_audio
->mmio
+ ACTL_PSR
);
136 if (val
& P_DMA_MIDDLE_IRQ
) {
137 val
|= P_DMA_MIDDLE_IRQ
;
138 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_PSR
, val
);
141 if (val
& P_DMA_END_IRQ
) {
142 val
|= P_DMA_END_IRQ
;
143 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_PSR
, val
);
146 dev_err(nuc900_audio
->dev
, "Wrong DMA interrupt status!\n");
147 spin_unlock(&nuc900_audio
->lock
);
151 spin_unlock(&nuc900_audio
->lock
);
153 snd_pcm_period_elapsed(substream
);
158 static int nuc900_dma_hw_free(struct snd_pcm_substream
*substream
)
160 snd_pcm_lib_free_pages(substream
);
164 static int nuc900_dma_prepare(struct snd_pcm_substream
*substream
)
166 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
167 struct nuc900_audio
*nuc900_audio
= runtime
->private_data
;
168 unsigned long flags
, val
;
171 spin_lock_irqsave(&nuc900_audio
->lock
, flags
);
173 nuc900_update_dma_register(substream
,
174 nuc900_audio
->dma_addr
[substream
->stream
],
175 nuc900_audio
->buffersize
[substream
->stream
]);
177 val
= AUDIO_READ(nuc900_audio
->mmio
+ ACTL_RESET
);
179 switch (runtime
->channels
) {
181 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
182 val
&= ~(PLAY_LEFT_CHNNEL
| PLAY_RIGHT_CHNNEL
);
183 val
|= PLAY_RIGHT_CHNNEL
;
185 val
&= ~(RECORD_LEFT_CHNNEL
| RECORD_RIGHT_CHNNEL
);
186 val
|= RECORD_RIGHT_CHNNEL
;
188 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_RESET
, val
);
191 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
192 val
|= (PLAY_LEFT_CHNNEL
| PLAY_RIGHT_CHNNEL
);
194 val
|= (RECORD_LEFT_CHNNEL
| RECORD_RIGHT_CHNNEL
);
195 AUDIO_WRITE(nuc900_audio
->mmio
+ ACTL_RESET
, val
);
200 spin_unlock_irqrestore(&nuc900_audio
->lock
, flags
);
204 static int nuc900_dma_trigger(struct snd_pcm_substream
*substream
, int cmd
)
209 case SNDRV_PCM_TRIGGER_START
:
210 case SNDRV_PCM_TRIGGER_RESUME
:
211 nuc900_dma_start(substream
);
214 case SNDRV_PCM_TRIGGER_STOP
:
215 case SNDRV_PCM_TRIGGER_SUSPEND
:
216 nuc900_dma_stop(substream
);
227 static int nuc900_dma_getposition(struct snd_pcm_substream
*substream
,
228 dma_addr_t
*src
, dma_addr_t
*dst
)
230 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
231 struct nuc900_audio
*nuc900_audio
= runtime
->private_data
;
234 *src
= AUDIO_READ(nuc900_audio
->mmio
+ ACTL_PDSTC
);
237 *dst
= AUDIO_READ(nuc900_audio
->mmio
+ ACTL_RDSTC
);
242 static snd_pcm_uframes_t
nuc900_dma_pointer(struct snd_pcm_substream
*substream
)
244 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
248 nuc900_dma_getposition(substream
, &src
, &dst
);
250 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
251 res
= dst
- runtime
->dma_addr
;
253 res
= src
- runtime
->dma_addr
;
255 return bytes_to_frames(substream
->runtime
, res
);
258 static int nuc900_dma_open(struct snd_pcm_substream
*substream
)
260 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
261 struct nuc900_audio
*nuc900_audio
;
263 snd_soc_set_runtime_hwparams(substream
, &nuc900_pcm_hardware
);
265 nuc900_audio
= nuc900_ac97_data
;
267 if (request_irq(nuc900_audio
->irq_num
, nuc900_dma_interrupt
,
268 0, "nuc900-dma", substream
))
271 runtime
->private_data
= nuc900_audio
;
276 static int nuc900_dma_close(struct snd_pcm_substream
*substream
)
278 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
279 struct nuc900_audio
*nuc900_audio
= runtime
->private_data
;
281 free_irq(nuc900_audio
->irq_num
, substream
);
286 static int nuc900_dma_mmap(struct snd_pcm_substream
*substream
,
287 struct vm_area_struct
*vma
)
289 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
291 return dma_mmap_writecombine(substream
->pcm
->card
->dev
, vma
,
297 static struct snd_pcm_ops nuc900_dma_ops
= {
298 .open
= nuc900_dma_open
,
299 .close
= nuc900_dma_close
,
300 .ioctl
= snd_pcm_lib_ioctl
,
301 .hw_params
= nuc900_dma_hw_params
,
302 .hw_free
= nuc900_dma_hw_free
,
303 .prepare
= nuc900_dma_prepare
,
304 .trigger
= nuc900_dma_trigger
,
305 .pointer
= nuc900_dma_pointer
,
306 .mmap
= nuc900_dma_mmap
,
309 static void nuc900_dma_free_dma_buffers(struct snd_pcm
*pcm
)
311 snd_pcm_lib_preallocate_free_for_all(pcm
);
314 static int nuc900_dma_new(struct snd_soc_pcm_runtime
*rtd
)
316 struct snd_card
*card
= rtd
->card
->snd_card
;
317 struct snd_pcm
*pcm
= rtd
->pcm
;
320 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
324 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
325 card
->dev
, 4 * 1024, (4 * 1024) - 1);
330 static struct snd_soc_platform_driver nuc900_soc_platform
= {
331 .ops
= &nuc900_dma_ops
,
332 .pcm_new
= nuc900_dma_new
,
333 .pcm_free
= nuc900_dma_free_dma_buffers
,
336 static int nuc900_soc_platform_probe(struct platform_device
*pdev
)
338 return snd_soc_register_platform(&pdev
->dev
, &nuc900_soc_platform
);
341 static int nuc900_soc_platform_remove(struct platform_device
*pdev
)
343 snd_soc_unregister_platform(&pdev
->dev
);
347 static struct platform_driver nuc900_pcm_driver
= {
349 .name
= "nuc900-pcm-audio",
350 .owner
= THIS_MODULE
,
353 .probe
= nuc900_soc_platform_probe
,
354 .remove
= nuc900_soc_platform_remove
,
357 module_platform_driver(nuc900_pcm_driver
);
359 MODULE_AUTHOR("Wan ZongShun, <mcuos.com@gmail.com>");
360 MODULE_DESCRIPTION("nuc900 Audio DMA module");
361 MODULE_LICENSE("GPL");