RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / soc / sh / dma-sh7760.c
blob84340d5d7fbbf1a45fcf82d4a42f0e6725555f95
3 #include <linux/module.h>
4 #include <linux/gfp.h>
5 #include <linux/init.h>
6 #include <linux/platform_device.h>
7 #include <linux/dma-mapping.h>
8 #include <sound/core.h>
9 #include <sound/pcm.h>
10 #include <sound/pcm_params.h>
11 #include <sound/soc.h>
12 #include <asm/dmabrg.h>
15 /* registers and bits */
16 #define BRGATXSAR 0x00
17 #define BRGARXDAR 0x04
18 #define BRGATXTCR 0x08
19 #define BRGARXTCR 0x0C
20 #define BRGACR 0x10
21 #define BRGATXTCNT 0x14
22 #define BRGARXTCNT 0x18
24 #define ACR_RAR (1 << 18)
25 #define ACR_RDS (1 << 17)
26 #define ACR_RDE (1 << 16)
27 #define ACR_TAR (1 << 2)
28 #define ACR_TDS (1 << 1)
29 #define ACR_TDE (1 << 0)
31 /* receiver/transmitter data alignment */
32 #define ACR_RAM_NONE (0 << 24)
33 #define ACR_RAM_4BYTE (1 << 24)
34 #define ACR_RAM_2WORD (2 << 24)
35 #define ACR_TAM_NONE (0 << 8)
36 #define ACR_TAM_4BYTE (1 << 8)
37 #define ACR_TAM_2WORD (2 << 8)
40 struct camelot_pcm {
41 unsigned long mmio; /* DMABRG audio channel control reg MMIO */
42 unsigned int txid; /* ID of first DMABRG IRQ for this unit */
44 struct snd_pcm_substream *tx_ss;
45 unsigned long tx_period_size;
46 unsigned int tx_period;
48 struct snd_pcm_substream *rx_ss;
49 unsigned long rx_period_size;
50 unsigned int rx_period;
52 } cam_pcm_data[2] = {
54 .mmio = 0xFE3C0040,
55 .txid = DMABRGIRQ_A0TXF,
58 .mmio = 0xFE3C0060,
59 .txid = DMABRGIRQ_A1TXF,
63 #define BRGREG(x) (*(unsigned long *)(cam->mmio + (x)))
66 * set a minimum of 16kb per period, to avoid interrupt-"storm" and
67 * resulting skipping. In general, the bigger the minimum size, the
68 * better for overall system performance. (The SH7760 is a puny CPU
69 * with a slow SDRAM interface and poor internal bus bandwidth,
70 * *especially* when the LCDC is active). The minimum for the DMAC
71 * is 8 bytes; 16kbytes are enough to get skip-free playback of a
72 * 44kHz/16bit/stereo MP3 on a lightly loaded system, and maintain
73 * reasonable responsiveness in MPlayer.
75 #define DMABRG_PERIOD_MIN 16 * 1024
76 #define DMABRG_PERIOD_MAX 0x03fffffc
77 #define DMABRG_PREALLOC_BUFFER 32 * 1024
78 #define DMABRG_PREALLOC_BUFFER_MAX 32 * 1024
80 /* support everything the SSI supports */
81 #define DMABRG_RATES \
82 SNDRV_PCM_RATE_8000_192000
84 #define DMABRG_FMTS \
85 (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | \
86 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE | \
87 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_U20_3LE | \
88 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3LE | \
89 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_U32_LE)
91 static struct snd_pcm_hardware camelot_pcm_hardware = {
92 .info = (SNDRV_PCM_INFO_MMAP |
93 SNDRV_PCM_INFO_INTERLEAVED |
94 SNDRV_PCM_INFO_BLOCK_TRANSFER |
95 SNDRV_PCM_INFO_MMAP_VALID |
96 SNDRV_PCM_INFO_BATCH),
97 .formats = DMABRG_FMTS,
98 .rates = DMABRG_RATES,
99 .rate_min = 8000,
100 .rate_max = 192000,
101 .channels_min = 2,
102 .channels_max = 8, /* max of the SSI */
103 .buffer_bytes_max = DMABRG_PERIOD_MAX,
104 .period_bytes_min = DMABRG_PERIOD_MIN,
105 .period_bytes_max = DMABRG_PERIOD_MAX / 2,
106 .periods_min = 2,
107 .periods_max = 2,
108 .fifo_size = 128,
111 static void camelot_txdma(void *data)
113 struct camelot_pcm *cam = data;
114 cam->tx_period ^= 1;
115 snd_pcm_period_elapsed(cam->tx_ss);
118 static void camelot_rxdma(void *data)
120 struct camelot_pcm *cam = data;
121 cam->rx_period ^= 1;
122 snd_pcm_period_elapsed(cam->rx_ss);
125 static int camelot_pcm_open(struct snd_pcm_substream *substream)
127 struct snd_soc_pcm_runtime *rtd = substream->private_data;
128 struct camelot_pcm *cam = &cam_pcm_data[rtd->dai->cpu_dai->id];
129 int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
130 int ret, dmairq;
132 snd_soc_set_runtime_hwparams(substream, &camelot_pcm_hardware);
134 /* DMABRG buffer half/full events */
135 dmairq = (recv) ? cam->txid + 2 : cam->txid;
136 if (recv) {
137 cam->rx_ss = substream;
138 ret = dmabrg_request_irq(dmairq, camelot_rxdma, cam);
139 if (unlikely(ret)) {
140 pr_debug("audio unit %d irqs already taken!\n",
141 rtd->dai->cpu_dai->id);
142 return -EBUSY;
144 (void)dmabrg_request_irq(dmairq + 1,camelot_rxdma, cam);
145 } else {
146 cam->tx_ss = substream;
147 ret = dmabrg_request_irq(dmairq, camelot_txdma, cam);
148 if (unlikely(ret)) {
149 pr_debug("audio unit %d irqs already taken!\n",
150 rtd->dai->cpu_dai->id);
151 return -EBUSY;
153 (void)dmabrg_request_irq(dmairq + 1, camelot_txdma, cam);
155 return 0;
158 static int camelot_pcm_close(struct snd_pcm_substream *substream)
160 struct snd_soc_pcm_runtime *rtd = substream->private_data;
161 struct camelot_pcm *cam = &cam_pcm_data[rtd->dai->cpu_dai->id];
162 int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
163 int dmairq;
165 dmairq = (recv) ? cam->txid + 2 : cam->txid;
167 if (recv)
168 cam->rx_ss = NULL;
169 else
170 cam->tx_ss = NULL;
172 dmabrg_free_irq(dmairq + 1);
173 dmabrg_free_irq(dmairq);
175 return 0;
178 static int camelot_hw_params(struct snd_pcm_substream *substream,
179 struct snd_pcm_hw_params *hw_params)
181 struct snd_soc_pcm_runtime *rtd = substream->private_data;
182 struct camelot_pcm *cam = &cam_pcm_data[rtd->dai->cpu_dai->id];
183 int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
184 int ret;
186 ret = snd_pcm_lib_malloc_pages(substream,
187 params_buffer_bytes(hw_params));
188 if (ret < 0)
189 return ret;
191 if (recv) {
192 cam->rx_period_size = params_period_bytes(hw_params);
193 cam->rx_period = 0;
194 } else {
195 cam->tx_period_size = params_period_bytes(hw_params);
196 cam->tx_period = 0;
198 return 0;
201 static int camelot_hw_free(struct snd_pcm_substream *substream)
203 return snd_pcm_lib_free_pages(substream);
206 static int camelot_prepare(struct snd_pcm_substream *substream)
208 struct snd_pcm_runtime *runtime = substream->runtime;
209 struct snd_soc_pcm_runtime *rtd = substream->private_data;
210 struct camelot_pcm *cam = &cam_pcm_data[rtd->dai->cpu_dai->id];
212 pr_debug("PCM data: addr 0x%08ulx len %d\n",
213 (u32)runtime->dma_addr, runtime->dma_bytes);
215 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
216 BRGREG(BRGATXSAR) = (unsigned long)runtime->dma_area;
217 BRGREG(BRGATXTCR) = runtime->dma_bytes;
218 } else {
219 BRGREG(BRGARXDAR) = (unsigned long)runtime->dma_area;
220 BRGREG(BRGARXTCR) = runtime->dma_bytes;
223 return 0;
226 static inline void dmabrg_play_dma_start(struct camelot_pcm *cam)
228 unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
229 /* start DMABRG engine: XFER start, auto-addr-reload */
230 BRGREG(BRGACR) = acr | ACR_TDE | ACR_TAR | ACR_TAM_2WORD;
233 static inline void dmabrg_play_dma_stop(struct camelot_pcm *cam)
235 unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
236 /* forcibly terminate data transmission */
237 BRGREG(BRGACR) = acr | ACR_TDS;
240 static inline void dmabrg_rec_dma_start(struct camelot_pcm *cam)
242 unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
243 /* start DMABRG engine: recv start, auto-reload */
244 BRGREG(BRGACR) = acr | ACR_RDE | ACR_RAR | ACR_RAM_2WORD;
247 static inline void dmabrg_rec_dma_stop(struct camelot_pcm *cam)
249 unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
250 /* forcibly terminate data receiver */
251 BRGREG(BRGACR) = acr | ACR_RDS;
254 static int camelot_trigger(struct snd_pcm_substream *substream, int cmd)
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct camelot_pcm *cam = &cam_pcm_data[rtd->dai->cpu_dai->id];
258 int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
260 switch (cmd) {
261 case SNDRV_PCM_TRIGGER_START:
262 if (recv)
263 dmabrg_rec_dma_start(cam);
264 else
265 dmabrg_play_dma_start(cam);
266 break;
267 case SNDRV_PCM_TRIGGER_STOP:
268 if (recv)
269 dmabrg_rec_dma_stop(cam);
270 else
271 dmabrg_play_dma_stop(cam);
272 break;
273 default:
274 return -EINVAL;
277 return 0;
280 static snd_pcm_uframes_t camelot_pos(struct snd_pcm_substream *substream)
282 struct snd_pcm_runtime *runtime = substream->runtime;
283 struct snd_soc_pcm_runtime *rtd = substream->private_data;
284 struct camelot_pcm *cam = &cam_pcm_data[rtd->dai->cpu_dai->id];
285 int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
286 unsigned long pos;
288 /* cannot use the DMABRG pointer register: under load, by the
289 * time ALSA comes around to read the register, it is already
290 * far ahead (or worse, already done with the fragment) of the
291 * position at the time the IRQ was triggered, which results in
292 * fast-playback sound in my test application (ScummVM)
294 if (recv)
295 pos = cam->rx_period ? cam->rx_period_size : 0;
296 else
297 pos = cam->tx_period ? cam->tx_period_size : 0;
299 return bytes_to_frames(runtime, pos);
302 static struct snd_pcm_ops camelot_pcm_ops = {
303 .open = camelot_pcm_open,
304 .close = camelot_pcm_close,
305 .ioctl = snd_pcm_lib_ioctl,
306 .hw_params = camelot_hw_params,
307 .hw_free = camelot_hw_free,
308 .prepare = camelot_prepare,
309 .trigger = camelot_trigger,
310 .pointer = camelot_pos,
313 static void camelot_pcm_free(struct snd_pcm *pcm)
315 snd_pcm_lib_preallocate_free_for_all(pcm);
318 static int camelot_pcm_new(struct snd_card *card,
319 struct snd_soc_dai *dai,
320 struct snd_pcm *pcm)
322 /* dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
323 * in MMAP mode (i.e. aplay -M)
325 snd_pcm_lib_preallocate_pages_for_all(pcm,
326 SNDRV_DMA_TYPE_CONTINUOUS,
327 snd_dma_continuous_data(GFP_KERNEL),
328 DMABRG_PREALLOC_BUFFER, DMABRG_PREALLOC_BUFFER_MAX);
330 return 0;
333 struct snd_soc_platform sh7760_soc_platform = {
334 .name = "sh7760-pcm",
335 .pcm_ops = &camelot_pcm_ops,
336 .pcm_new = camelot_pcm_new,
337 .pcm_free = camelot_pcm_free,
339 EXPORT_SYMBOL_GPL(sh7760_soc_platform);
341 static int __init sh7760_soc_platform_init(void)
343 return snd_soc_register_platform(&sh7760_soc_platform);
345 module_init(sh7760_soc_platform_init);
347 static void __exit sh7760_soc_platform_exit(void)
349 snd_soc_unregister_platform(&sh7760_soc_platform);
351 module_exit(sh7760_soc_platform_exit);
353 MODULE_LICENSE("GPL");
354 MODULE_DESCRIPTION("SH7760 Audio DMA (DMABRG) driver");
355 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");