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
,
46 .buffer_bytes_max
= 0x10000,
47 .period_bytes_min
= 0x20,
48 .period_bytes_max
= 0x1000,
54 static struct snd_pcm_hardware snd_vortex_playback_hw_a3d
= {
56 (SNDRV_PCM_INFO_MMAP
| /* SNDRV_PCM_INFO_RESUME | */
57 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_INTERLEAVED
|
58 SNDRV_PCM_INFO_MMAP_VALID
),
60 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_U8
|
61 SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
,
62 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
67 .buffer_bytes_max
= 0x10000,
68 .period_bytes_min
= 0x100,
69 .period_bytes_max
= 0x1000,
74 static struct snd_pcm_hardware snd_vortex_playback_hw_spdif
= {
76 (SNDRV_PCM_INFO_MMAP
| /* SNDRV_PCM_INFO_RESUME | */
77 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_INTERLEAVED
|
78 SNDRV_PCM_INFO_MMAP_VALID
),
80 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_U8
|
81 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
| SNDRV_PCM_FMTBIT_MU_LAW
|
82 SNDRV_PCM_FMTBIT_A_LAW
,
84 SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
89 .buffer_bytes_max
= 0x10000,
90 .period_bytes_min
= 0x100,
91 .period_bytes_max
= 0x1000,
97 static struct snd_pcm_hardware snd_vortex_playback_hw_wt
= {
98 .info
= (SNDRV_PCM_INFO_MMAP
|
99 SNDRV_PCM_INFO_INTERLEAVED
|
100 SNDRV_PCM_INFO_BLOCK_TRANSFER
| SNDRV_PCM_INFO_MMAP_VALID
),
101 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
102 .rates
= SNDRV_PCM_RATE_8000_48000
| SNDRV_PCM_RATE_CONTINUOUS
, // SNDRV_PCM_RATE_48000,
107 .buffer_bytes_max
= 0x10000,
108 .period_bytes_min
= 0x0400,
109 .period_bytes_max
= 0x1000,
115 static unsigned int au8830_channels
[3] = {
119 static struct snd_pcm_hw_constraint_list hw_constraints_au8830_channels
= {
120 .count
= ARRAY_SIZE(au8830_channels
),
121 .list
= au8830_channels
,
126 static int snd_vortex_pcm_open(struct snd_pcm_substream
*substream
)
128 vortex_t
*vortex
= snd_pcm_substream_chip(substream
);
129 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
132 /* Force equal size periods */
134 snd_pcm_hw_constraint_integer(runtime
,
135 SNDRV_PCM_HW_PARAM_PERIODS
)) < 0)
137 /* Avoid PAGE_SIZE boundary to fall inside of a period. */
139 snd_pcm_hw_constraint_pow2(runtime
, 0,
140 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
)) < 0)
143 snd_pcm_hw_constraint_step(runtime
, 0,
144 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 64);
146 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
148 if (VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_A3D
) {
149 runtime
->hw
= snd_vortex_playback_hw_a3d
;
152 if (VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_SPDIF
) {
153 runtime
->hw
= snd_vortex_playback_hw_spdif
;
154 switch (vortex
->spdif_sr
) {
156 runtime
->hw
.rates
= SNDRV_PCM_RATE_32000
;
159 runtime
->hw
.rates
= SNDRV_PCM_RATE_44100
;
162 runtime
->hw
.rates
= SNDRV_PCM_RATE_48000
;
166 if (VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_ADB
167 || VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_I2S
)
168 runtime
->hw
= snd_vortex_playback_hw_adb
;
170 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
171 VORTEX_IS_QUAD(vortex
) &&
172 VORTEX_PCM_TYPE(substream
->pcm
) == VORTEX_PCM_ADB
) {
173 runtime
->hw
.channels_max
= 4;
174 snd_pcm_hw_constraint_list(runtime
, 0,
175 SNDRV_PCM_HW_PARAM_CHANNELS
,
176 &hw_constraints_au8830_channels
);
179 substream
->runtime
->private_data
= NULL
;
183 runtime
->hw
= snd_vortex_playback_hw_wt
;
184 substream
->runtime
->private_data
= NULL
;
191 static int snd_vortex_pcm_close(struct snd_pcm_substream
*substream
)
193 //vortex_t *chip = snd_pcm_substream_chip(substream);
194 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
196 // the hardware-specific codes will be here
197 if (stream
!= NULL
) {
198 stream
->substream
= NULL
;
201 substream
->runtime
->private_data
= NULL
;
205 /* hw_params callback */
207 snd_vortex_pcm_hw_params(struct snd_pcm_substream
*substream
,
208 struct snd_pcm_hw_params
*hw_params
)
210 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
211 stream_t
*stream
= (stream_t
*) (substream
->runtime
->private_data
);
214 // Alloc buffer memory.
216 snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
218 printk(KERN_ERR
"Vortex: pcm page alloc failed!\n");
222 printk(KERN_INFO "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
223 params_period_bytes(hw_params), params_channels(hw_params));
225 spin_lock_irq(&chip
->lock
);
226 // Make audio routes and config buffer DMA.
227 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
228 int dma
, type
= VORTEX_PCM_TYPE(substream
->pcm
);
229 /* Dealloc any routes. */
231 vortex_adb_allocroute(chip
, stream
->dma
,
232 stream
->nr_ch
, stream
->dir
,
236 vortex_adb_allocroute(chip
, -1,
237 params_channels(hw_params
),
238 substream
->stream
, type
);
240 spin_unlock_irq(&chip
->lock
);
243 stream
= substream
->runtime
->private_data
= &chip
->dma_adb
[dma
];
244 stream
->substream
= substream
;
246 vortex_adbdma_setbuffers(chip
, dma
,
247 params_period_bytes(hw_params
),
248 params_periods(hw_params
));
252 /* if (stream != NULL)
253 vortex_wt_allocroute(chip, substream->number, 0); */
254 vortex_wt_allocroute(chip
, substream
->number
,
255 params_channels(hw_params
));
256 stream
= substream
->runtime
->private_data
=
257 &chip
->dma_wt
[substream
->number
];
258 stream
->dma
= substream
->number
;
259 stream
->substream
= substream
;
260 vortex_wtdma_setbuffers(chip
, substream
->number
,
261 params_period_bytes(hw_params
),
262 params_periods(hw_params
));
265 spin_unlock_irq(&chip
->lock
);
269 /* hw_free callback */
270 static int snd_vortex_pcm_hw_free(struct snd_pcm_substream
*substream
)
272 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
273 stream_t
*stream
= (stream_t
*) (substream
->runtime
->private_data
);
275 spin_lock_irq(&chip
->lock
);
276 // Delete audio routes.
277 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
279 vortex_adb_allocroute(chip
, stream
->dma
,
280 stream
->nr_ch
, stream
->dir
,
286 vortex_wt_allocroute(chip
, stream
->dma
, 0);
289 substream
->runtime
->private_data
= NULL
;
290 spin_unlock_irq(&chip
->lock
);
292 return snd_pcm_lib_free_pages(substream
);
295 /* prepare callback */
296 static int snd_vortex_pcm_prepare(struct snd_pcm_substream
*substream
)
298 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
299 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
300 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
301 int dma
= stream
->dma
, fmt
, dir
;
303 // set up the hardware with the current configuration.
304 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
308 fmt
= vortex_alsafmt_aspfmt(runtime
->format
);
309 spin_lock_irq(&chip
->lock
);
310 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
311 vortex_adbdma_setmode(chip
, dma
, 1, dir
, fmt
,
312 runtime
->channels
== 1 ? 0 : 1, 0);
313 vortex_adbdma_setstartbuffer(chip
, dma
, 0);
314 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_SPDIF
)
315 vortex_adb_setsrc(chip
, dma
, runtime
->rate
, dir
);
319 vortex_wtdma_setmode(chip
, dma
, 1, fmt
, 0, 0);
320 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
321 vortex_wtdma_setstartbuffer(chip
, dma
, 0);
324 spin_unlock_irq(&chip
->lock
);
328 /* trigger callback */
329 static int snd_vortex_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
331 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
332 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
333 int dma
= stream
->dma
;
335 spin_lock(&chip
->lock
);
337 case SNDRV_PCM_TRIGGER_START
:
338 // do something to start the PCM engine
339 //printk(KERN_INFO "vortex: start %d\n", dma);
340 stream
->fifo_enabled
= 1;
341 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
342 vortex_adbdma_resetup(chip
, dma
);
343 vortex_adbdma_startfifo(chip
, dma
);
347 printk(KERN_INFO
"vortex: wt start %d\n", dma
);
348 vortex_wtdma_startfifo(chip
, dma
);
352 case SNDRV_PCM_TRIGGER_STOP
:
353 // do something to stop the PCM engine
354 //printk(KERN_INFO "vortex: stop %d\n", dma);
355 stream
->fifo_enabled
= 0;
356 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
357 vortex_adbdma_stopfifo(chip
, dma
);
360 printk(KERN_INFO
"vortex: wt stop %d\n", dma
);
361 vortex_wtdma_stopfifo(chip
, dma
);
365 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
366 //printk(KERN_INFO "vortex: pause %d\n", dma);
367 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
368 vortex_adbdma_pausefifo(chip
, dma
);
371 vortex_wtdma_pausefifo(chip
, dma
);
374 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
375 //printk(KERN_INFO "vortex: resume %d\n", dma);
376 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
377 vortex_adbdma_resumefifo(chip
, dma
);
380 vortex_wtdma_resumefifo(chip
, dma
);
384 spin_unlock(&chip
->lock
);
387 spin_unlock(&chip
->lock
);
391 /* pointer callback */
392 static snd_pcm_uframes_t
snd_vortex_pcm_pointer(struct snd_pcm_substream
*substream
)
394 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
395 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
396 int dma
= stream
->dma
;
397 snd_pcm_uframes_t current_ptr
= 0;
399 spin_lock(&chip
->lock
);
400 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
401 current_ptr
= vortex_adbdma_getlinearpos(chip
, dma
);
404 current_ptr
= vortex_wtdma_getlinearpos(chip
, dma
);
406 //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
407 spin_unlock(&chip
->lock
);
408 return (bytes_to_frames(substream
->runtime
, current_ptr
));
412 static struct snd_pcm_ops snd_vortex_playback_ops
= {
413 .open
= snd_vortex_pcm_open
,
414 .close
= snd_vortex_pcm_close
,
415 .ioctl
= snd_pcm_lib_ioctl
,
416 .hw_params
= snd_vortex_pcm_hw_params
,
417 .hw_free
= snd_vortex_pcm_hw_free
,
418 .prepare
= snd_vortex_pcm_prepare
,
419 .trigger
= snd_vortex_pcm_trigger
,
420 .pointer
= snd_vortex_pcm_pointer
,
421 .page
= snd_pcm_sgbuf_ops_page
,
425 * definitions of capture are omitted here...
428 static char *vortex_pcm_prettyname
[VORTEX_PCM_LAST
] = {
435 static char *vortex_pcm_name
[VORTEX_PCM_LAST
] = {
445 static int snd_vortex_spdif_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
447 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
452 static int snd_vortex_spdif_mask_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
454 ucontrol
->value
.iec958
.status
[0] = 0xff;
455 ucontrol
->value
.iec958
.status
[1] = 0xff;
456 ucontrol
->value
.iec958
.status
[2] = 0xff;
457 ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS
;
461 static int snd_vortex_spdif_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
463 vortex_t
*vortex
= snd_kcontrol_chip(kcontrol
);
464 ucontrol
->value
.iec958
.status
[0] = 0x00;
465 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_ORIGINAL
|IEC958_AES1_CON_DIGDIGCONV_ID
;
466 ucontrol
->value
.iec958
.status
[2] = 0x00;
467 switch (vortex
->spdif_sr
) {
468 case 32000: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_32000
; break;
469 case 44100: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_44100
; break;
470 case 48000: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_48000
; break;
475 static int snd_vortex_spdif_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
477 vortex_t
*vortex
= snd_kcontrol_chip(kcontrol
);
478 int spdif_sr
= 48000;
479 switch (ucontrol
->value
.iec958
.status
[3] & IEC958_AES3_CON_FS
) {
480 case IEC958_AES3_CON_FS_32000
: spdif_sr
= 32000; break;
481 case IEC958_AES3_CON_FS_44100
: spdif_sr
= 44100; break;
482 case IEC958_AES3_CON_FS_48000
: spdif_sr
= 48000; break;
484 if (spdif_sr
== vortex
->spdif_sr
)
486 vortex
->spdif_sr
= spdif_sr
;
487 vortex_spdif_init(vortex
, vortex
->spdif_sr
, 1);
492 static struct snd_kcontrol_new snd_vortex_mixer_spdif
[] __devinitdata
= {
494 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
495 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,DEFAULT
),
496 .info
= snd_vortex_spdif_info
,
497 .get
= snd_vortex_spdif_get
,
498 .put
= snd_vortex_spdif_put
,
501 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
502 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
503 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,CON_MASK
),
504 .info
= snd_vortex_spdif_info
,
505 .get
= snd_vortex_spdif_mask_get
509 /* create a pcm device */
510 static int __devinit
snd_vortex_new_pcm(vortex_t
*chip
, int idx
, int nr
)
513 struct snd_kcontrol
*kctl
;
517 if (!chip
|| idx
< 0 || idx
>= VORTEX_PCM_LAST
)
520 /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
521 * same dma engine. WT uses it own separate dma engine which can't capture. */
522 if (idx
== VORTEX_PCM_ADB
)
526 err
= snd_pcm_new(chip
->card
, vortex_pcm_prettyname
[idx
], idx
, nr
,
530 snprintf(pcm
->name
, sizeof(pcm
->name
),
531 "%s %s", CARD_NAME_SHORT
, vortex_pcm_name
[idx
]);
532 chip
->pcm
[idx
] = pcm
;
533 // This is an evil hack, but it saves a lot of duplicated code.
534 VORTEX_PCM_TYPE(pcm
) = idx
;
535 pcm
->private_data
= chip
;
537 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
538 &snd_vortex_playback_ops
);
539 if (idx
== VORTEX_PCM_ADB
)
540 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
541 &snd_vortex_playback_ops
);
543 /* pre-allocation of Scatter-Gather buffers */
545 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV_SG
,
546 snd_dma_pci_data(chip
->pci_dev
),
549 if (VORTEX_PCM_TYPE(pcm
) == VORTEX_PCM_SPDIF
) {
550 for (i
= 0; i
< ARRAY_SIZE(snd_vortex_mixer_spdif
); i
++) {
551 kctl
= snd_ctl_new1(&snd_vortex_mixer_spdif
[i
], chip
);
554 if ((err
= snd_ctl_add(chip
->card
, kctl
)) < 0)