2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Vortex PCM ALSA driver.
20 * Supports ADB and WT DMA. Unfortunately, WT channels do not run yet.
21 * It remains stuck,and DMA transfers do not happen.
23 #include <sound/asoundef.h>
24 #include <linux/time.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
30 #define VORTEX_PCM_TYPE(x) (x->name[40])
32 /* hardware definition */
33 static struct snd_pcm_hardware snd_vortex_playback_hw_adb
= {
35 (SNDRV_PCM_INFO_MMAP
| /* SNDRV_PCM_INFO_RESUME | */
36 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_INTERLEAVED
|
37 SNDRV_PCM_INFO_MMAP_VALID
),
39 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_U8
|
40 SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
,
41 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
50 .buffer_bytes_max
= 0x10000,
51 .period_bytes_min
= 0x1,
52 .period_bytes_max
= 0x1000,
58 static struct snd_pcm_hardware snd_vortex_playback_hw_a3d
= {
60 (SNDRV_PCM_INFO_MMAP
| /* SNDRV_PCM_INFO_RESUME | */
61 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_INTERLEAVED
|
62 SNDRV_PCM_INFO_MMAP_VALID
),
64 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_U8
|
65 SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
,
66 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
71 .buffer_bytes_max
= 0x10000,
72 .period_bytes_min
= 0x100,
73 .period_bytes_max
= 0x1000,
78 static struct snd_pcm_hardware snd_vortex_playback_hw_spdif
= {
80 (SNDRV_PCM_INFO_MMAP
| /* SNDRV_PCM_INFO_RESUME | */
81 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_INTERLEAVED
|
82 SNDRV_PCM_INFO_MMAP_VALID
),
84 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_U8
|
85 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
| SNDRV_PCM_FMTBIT_MU_LAW
|
86 SNDRV_PCM_FMTBIT_A_LAW
,
88 SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
93 .buffer_bytes_max
= 0x10000,
94 .period_bytes_min
= 0x100,
95 .period_bytes_max
= 0x1000,
101 static struct snd_pcm_hardware snd_vortex_playback_hw_wt
= {
102 .info
= (SNDRV_PCM_INFO_MMAP
|
103 SNDRV_PCM_INFO_INTERLEAVED
|
104 SNDRV_PCM_INFO_BLOCK_TRANSFER
| SNDRV_PCM_INFO_MMAP_VALID
),
105 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
106 .rates
= SNDRV_PCM_RATE_8000_48000
| SNDRV_PCM_RATE_CONTINUOUS
, // SNDRV_PCM_RATE_48000,
111 .buffer_bytes_max
= 0x10000,
112 .period_bytes_min
= 0x0400,
113 .period_bytes_max
= 0x1000,
119 static int snd_vortex_pcm_open(struct snd_pcm_substream
*substream
)
121 vortex_t
*vortex
= snd_pcm_substream_chip(substream
);
122 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
125 /* Force equal size periods */
127 snd_pcm_hw_constraint_integer(runtime
,
128 SNDRV_PCM_HW_PARAM_PERIODS
)) < 0)
130 /* Avoid PAGE_SIZE boundary to fall inside of a period. */
132 snd_pcm_hw_constraint_pow2(runtime
, 0,
133 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
)) < 0)
136 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
138 if (VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_A3D
) {
139 runtime
->hw
= snd_vortex_playback_hw_a3d
;
142 if (VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_SPDIF
) {
143 runtime
->hw
= snd_vortex_playback_hw_spdif
;
144 switch (vortex
->spdif_sr
) {
146 runtime
->hw
.rates
= SNDRV_PCM_RATE_32000
;
149 runtime
->hw
.rates
= SNDRV_PCM_RATE_44100
;
152 runtime
->hw
.rates
= SNDRV_PCM_RATE_48000
;
156 if (VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_ADB
157 || VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_I2S
)
158 runtime
->hw
= snd_vortex_playback_hw_adb
;
159 substream
->runtime
->private_data
= NULL
;
163 runtime
->hw
= snd_vortex_playback_hw_wt
;
164 substream
->runtime
->private_data
= NULL
;
171 static int snd_vortex_pcm_close(struct snd_pcm_substream
*substream
)
173 //vortex_t *chip = snd_pcm_substream_chip(substream);
174 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
176 // the hardware-specific codes will be here
177 if (stream
!= NULL
) {
178 stream
->substream
= NULL
;
181 substream
->runtime
->private_data
= NULL
;
185 /* hw_params callback */
187 snd_vortex_pcm_hw_params(struct snd_pcm_substream
*substream
,
188 struct snd_pcm_hw_params
*hw_params
)
190 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
191 stream_t
*stream
= (stream_t
*) (substream
->runtime
->private_data
);
194 // Alloc buffer memory.
196 snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
198 printk(KERN_ERR
"Vortex: pcm page alloc failed!\n");
202 printk(KERN_INFO "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
203 params_period_bytes(hw_params), params_channels(hw_params));
205 spin_lock_irq(&chip
->lock
);
206 // Make audio routes and config buffer DMA.
207 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
208 int dma
, type
= VORTEX_PCM_TYPE(substream
->pcm
);
209 /* Dealloc any routes. */
211 vortex_adb_allocroute(chip
, stream
->dma
,
212 stream
->nr_ch
, stream
->dir
,
216 vortex_adb_allocroute(chip
, -1,
217 params_channels(hw_params
),
218 substream
->stream
, type
);
220 spin_unlock_irq(&chip
->lock
);
223 stream
= substream
->runtime
->private_data
= &chip
->dma_adb
[dma
];
224 stream
->substream
= substream
;
226 vortex_adbdma_setbuffers(chip
, dma
,
227 params_period_bytes(hw_params
),
228 params_periods(hw_params
));
232 /* if (stream != NULL)
233 vortex_wt_allocroute(chip, substream->number, 0); */
234 vortex_wt_allocroute(chip
, substream
->number
,
235 params_channels(hw_params
));
236 stream
= substream
->runtime
->private_data
=
237 &chip
->dma_wt
[substream
->number
];
238 stream
->dma
= substream
->number
;
239 stream
->substream
= substream
;
240 vortex_wtdma_setbuffers(chip
, substream
->number
,
241 params_period_bytes(hw_params
),
242 params_periods(hw_params
));
245 spin_unlock_irq(&chip
->lock
);
249 /* hw_free callback */
250 static int snd_vortex_pcm_hw_free(struct snd_pcm_substream
*substream
)
252 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
253 stream_t
*stream
= (stream_t
*) (substream
->runtime
->private_data
);
255 spin_lock_irq(&chip
->lock
);
256 // Delete audio routes.
257 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
259 vortex_adb_allocroute(chip
, stream
->dma
,
260 stream
->nr_ch
, stream
->dir
,
266 vortex_wt_allocroute(chip
, stream
->dma
, 0);
269 substream
->runtime
->private_data
= NULL
;
270 spin_unlock_irq(&chip
->lock
);
272 return snd_pcm_lib_free_pages(substream
);
275 /* prepare callback */
276 static int snd_vortex_pcm_prepare(struct snd_pcm_substream
*substream
)
278 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
279 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
280 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
281 int dma
= stream
->dma
, fmt
, dir
;
283 // set up the hardware with the current configuration.
284 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
288 fmt
= vortex_alsafmt_aspfmt(runtime
->format
);
289 spin_lock_irq(&chip
->lock
);
290 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
291 vortex_adbdma_setmode(chip
, dma
, 1, dir
, fmt
, 0 /*? */ ,
293 vortex_adbdma_setstartbuffer(chip
, dma
, 0);
294 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_SPDIF
)
295 vortex_adb_setsrc(chip
, dma
, runtime
->rate
, dir
);
299 vortex_wtdma_setmode(chip
, dma
, 1, fmt
, 0, 0);
300 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
301 vortex_wtdma_setstartbuffer(chip
, dma
, 0);
304 spin_unlock_irq(&chip
->lock
);
308 /* trigger callback */
309 static int snd_vortex_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
311 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
312 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
313 int dma
= stream
->dma
;
315 spin_lock(&chip
->lock
);
317 case SNDRV_PCM_TRIGGER_START
:
318 // do something to start the PCM engine
319 //printk(KERN_INFO "vortex: start %d\n", dma);
320 stream
->fifo_enabled
= 1;
321 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
322 vortex_adbdma_resetup(chip
, dma
);
323 vortex_adbdma_startfifo(chip
, dma
);
327 printk(KERN_INFO
"vortex: wt start %d\n", dma
);
328 vortex_wtdma_startfifo(chip
, dma
);
332 case SNDRV_PCM_TRIGGER_STOP
:
333 // do something to stop the PCM engine
334 //printk(KERN_INFO "vortex: stop %d\n", dma);
335 stream
->fifo_enabled
= 0;
336 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
337 vortex_adbdma_pausefifo(chip
, dma
);
338 //vortex_adbdma_stopfifo(chip, dma);
341 printk(KERN_INFO
"vortex: wt stop %d\n", dma
);
342 vortex_wtdma_stopfifo(chip
, dma
);
346 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
347 //printk(KERN_INFO "vortex: pause %d\n", dma);
348 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
349 vortex_adbdma_pausefifo(chip
, dma
);
352 vortex_wtdma_pausefifo(chip
, dma
);
355 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
356 //printk(KERN_INFO "vortex: resume %d\n", dma);
357 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
358 vortex_adbdma_resumefifo(chip
, dma
);
361 vortex_wtdma_resumefifo(chip
, dma
);
365 spin_unlock(&chip
->lock
);
368 spin_unlock(&chip
->lock
);
372 /* pointer callback */
373 static snd_pcm_uframes_t
snd_vortex_pcm_pointer(struct snd_pcm_substream
*substream
)
375 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
376 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
377 int dma
= stream
->dma
;
378 snd_pcm_uframes_t current_ptr
= 0;
380 spin_lock(&chip
->lock
);
381 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
382 current_ptr
= vortex_adbdma_getlinearpos(chip
, dma
);
385 current_ptr
= vortex_wtdma_getlinearpos(chip
, dma
);
387 //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
388 spin_unlock(&chip
->lock
);
389 return (bytes_to_frames(substream
->runtime
, current_ptr
));
393 static struct snd_pcm_ops snd_vortex_playback_ops
= {
394 .open
= snd_vortex_pcm_open
,
395 .close
= snd_vortex_pcm_close
,
396 .ioctl
= snd_pcm_lib_ioctl
,
397 .hw_params
= snd_vortex_pcm_hw_params
,
398 .hw_free
= snd_vortex_pcm_hw_free
,
399 .prepare
= snd_vortex_pcm_prepare
,
400 .trigger
= snd_vortex_pcm_trigger
,
401 .pointer
= snd_vortex_pcm_pointer
,
402 .page
= snd_pcm_sgbuf_ops_page
,
406 * definitions of capture are omitted here...
409 static char *vortex_pcm_prettyname
[VORTEX_PCM_LAST
] = {
416 static char *vortex_pcm_name
[VORTEX_PCM_LAST
] = {
426 static int snd_vortex_spdif_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
428 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
433 static int snd_vortex_spdif_mask_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
435 ucontrol
->value
.iec958
.status
[0] = 0xff;
436 ucontrol
->value
.iec958
.status
[1] = 0xff;
437 ucontrol
->value
.iec958
.status
[2] = 0xff;
438 ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS
;
442 static int snd_vortex_spdif_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
444 vortex_t
*vortex
= snd_kcontrol_chip(kcontrol
);
445 ucontrol
->value
.iec958
.status
[0] = 0x00;
446 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_ORIGINAL
|IEC958_AES1_CON_DIGDIGCONV_ID
;
447 ucontrol
->value
.iec958
.status
[2] = 0x00;
448 switch (vortex
->spdif_sr
) {
449 case 32000: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_32000
; break;
450 case 44100: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_44100
; break;
451 case 48000: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_48000
; break;
456 static int snd_vortex_spdif_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
458 vortex_t
*vortex
= snd_kcontrol_chip(kcontrol
);
459 int spdif_sr
= 48000;
460 switch (ucontrol
->value
.iec958
.status
[3] & IEC958_AES3_CON_FS
) {
461 case IEC958_AES3_CON_FS_32000
: spdif_sr
= 32000; break;
462 case IEC958_AES3_CON_FS_44100
: spdif_sr
= 44100; break;
463 case IEC958_AES3_CON_FS_48000
: spdif_sr
= 48000; break;
465 if (spdif_sr
== vortex
->spdif_sr
)
467 vortex
->spdif_sr
= spdif_sr
;
468 vortex_spdif_init(vortex
, vortex
->spdif_sr
, 1);
473 static struct snd_kcontrol_new snd_vortex_mixer_spdif
[] __devinitdata
= {
475 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
476 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,DEFAULT
),
477 .info
= snd_vortex_spdif_info
,
478 .get
= snd_vortex_spdif_get
,
479 .put
= snd_vortex_spdif_put
,
482 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
483 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
484 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,CON_MASK
),
485 .info
= snd_vortex_spdif_info
,
486 .get
= snd_vortex_spdif_mask_get
490 /* create a pcm device */
491 static int __devinit
snd_vortex_new_pcm(vortex_t
*chip
, int idx
, int nr
)
494 struct snd_kcontrol
*kctl
;
498 if (!chip
|| idx
< 0 || idx
>= VORTEX_PCM_LAST
)
501 /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
502 * same dma engine. WT uses it own separate dma engine whcih cant capture. */
503 if (idx
== VORTEX_PCM_ADB
)
507 err
= snd_pcm_new(chip
->card
, vortex_pcm_prettyname
[idx
], idx
, nr
,
511 strcpy(pcm
->name
, vortex_pcm_name
[idx
]);
512 chip
->pcm
[idx
] = pcm
;
513 // This is an evil hack, but it saves a lot of duplicated code.
514 VORTEX_PCM_TYPE(pcm
) = idx
;
515 pcm
->private_data
= chip
;
517 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
518 &snd_vortex_playback_ops
);
519 if (idx
== VORTEX_PCM_ADB
)
520 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
521 &snd_vortex_playback_ops
);
523 /* pre-allocation of Scatter-Gather buffers */
525 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV_SG
,
526 snd_dma_pci_data(chip
->pci_dev
),
529 if (VORTEX_PCM_TYPE(pcm
) == VORTEX_PCM_SPDIF
) {
530 for (i
= 0; i
< ARRAY_SIZE(snd_vortex_mixer_spdif
); i
++) {
531 kctl
= snd_ctl_new1(&snd_vortex_mixer_spdif
[i
], chip
);
534 if ((err
= snd_ctl_add(chip
->card
, kctl
)) < 0)