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
);
192 struct snd_sg_buf
*sgbuf
;
195 // Alloc buffer memory.
197 snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
199 printk(KERN_ERR
"Vortex: pcm page alloc failed!\n");
202 //sgbuf = (struct snd_sg_buf *) substream->runtime->dma_private;
203 sgbuf
= snd_pcm_substream_sgbuf(substream
);
205 printk(KERN_INFO "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
206 params_period_bytes(hw_params), params_channels(hw_params));
208 spin_lock_irq(&chip
->lock
);
209 // Make audio routes and config buffer DMA.
210 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
211 int dma
, type
= VORTEX_PCM_TYPE(substream
->pcm
);
212 /* Dealloc any routes. */
214 vortex_adb_allocroute(chip
, stream
->dma
,
215 stream
->nr_ch
, stream
->dir
,
219 vortex_adb_allocroute(chip
, -1,
220 params_channels(hw_params
),
221 substream
->stream
, type
);
223 spin_unlock_irq(&chip
->lock
);
226 stream
= substream
->runtime
->private_data
= &chip
->dma_adb
[dma
];
227 stream
->substream
= substream
;
229 vortex_adbdma_setbuffers(chip
, dma
, sgbuf
,
230 params_period_bytes(hw_params
),
231 params_periods(hw_params
));
235 /* if (stream != NULL)
236 vortex_wt_allocroute(chip, substream->number, 0); */
237 vortex_wt_allocroute(chip
, substream
->number
,
238 params_channels(hw_params
));
239 stream
= substream
->runtime
->private_data
=
240 &chip
->dma_wt
[substream
->number
];
241 stream
->dma
= substream
->number
;
242 stream
->substream
= substream
;
243 vortex_wtdma_setbuffers(chip
, substream
->number
, sgbuf
,
244 params_period_bytes(hw_params
),
245 params_periods(hw_params
));
248 spin_unlock_irq(&chip
->lock
);
252 /* hw_free callback */
253 static int snd_vortex_pcm_hw_free(struct snd_pcm_substream
*substream
)
255 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
256 stream_t
*stream
= (stream_t
*) (substream
->runtime
->private_data
);
258 spin_lock_irq(&chip
->lock
);
259 // Delete audio routes.
260 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
262 vortex_adb_allocroute(chip
, stream
->dma
,
263 stream
->nr_ch
, stream
->dir
,
269 vortex_wt_allocroute(chip
, stream
->dma
, 0);
272 substream
->runtime
->private_data
= NULL
;
273 spin_unlock_irq(&chip
->lock
);
275 return snd_pcm_lib_free_pages(substream
);
278 /* prepare callback */
279 static int snd_vortex_pcm_prepare(struct snd_pcm_substream
*substream
)
281 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
282 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
283 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
284 int dma
= stream
->dma
, fmt
, dir
;
286 // set up the hardware with the current configuration.
287 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
291 fmt
= vortex_alsafmt_aspfmt(runtime
->format
);
292 spin_lock_irq(&chip
->lock
);
293 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
294 vortex_adbdma_setmode(chip
, dma
, 1, dir
, fmt
, 0 /*? */ ,
296 vortex_adbdma_setstartbuffer(chip
, dma
, 0);
297 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_SPDIF
)
298 vortex_adb_setsrc(chip
, dma
, runtime
->rate
, dir
);
302 vortex_wtdma_setmode(chip
, dma
, 1, fmt
, 0, 0);
303 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
304 vortex_wtdma_setstartbuffer(chip
, dma
, 0);
307 spin_unlock_irq(&chip
->lock
);
311 /* trigger callback */
312 static int snd_vortex_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
314 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
315 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
316 int dma
= stream
->dma
;
318 spin_lock(&chip
->lock
);
320 case SNDRV_PCM_TRIGGER_START
:
321 // do something to start the PCM engine
322 //printk(KERN_INFO "vortex: start %d\n", dma);
323 stream
->fifo_enabled
= 1;
324 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
) {
325 vortex_adbdma_resetup(chip
, dma
);
326 vortex_adbdma_startfifo(chip
, dma
);
330 printk(KERN_INFO
"vortex: wt start %d\n", dma
);
331 vortex_wtdma_startfifo(chip
, dma
);
335 case SNDRV_PCM_TRIGGER_STOP
:
336 // do something to stop the PCM engine
337 //printk(KERN_INFO "vortex: stop %d\n", dma);
338 stream
->fifo_enabled
= 0;
339 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
340 vortex_adbdma_pausefifo(chip
, dma
);
341 //vortex_adbdma_stopfifo(chip, dma);
344 printk(KERN_INFO
"vortex: wt stop %d\n", dma
);
345 vortex_wtdma_stopfifo(chip
, dma
);
349 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
350 //printk(KERN_INFO "vortex: pause %d\n", dma);
351 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
352 vortex_adbdma_pausefifo(chip
, dma
);
355 vortex_wtdma_pausefifo(chip
, dma
);
358 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
359 //printk(KERN_INFO "vortex: resume %d\n", dma);
360 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
361 vortex_adbdma_resumefifo(chip
, dma
);
364 vortex_wtdma_resumefifo(chip
, dma
);
368 spin_unlock(&chip
->lock
);
371 spin_unlock(&chip
->lock
);
375 /* pointer callback */
376 static snd_pcm_uframes_t
snd_vortex_pcm_pointer(struct snd_pcm_substream
*substream
)
378 vortex_t
*chip
= snd_pcm_substream_chip(substream
);
379 stream_t
*stream
= (stream_t
*) substream
->runtime
->private_data
;
380 int dma
= stream
->dma
;
381 snd_pcm_uframes_t current_ptr
= 0;
383 spin_lock(&chip
->lock
);
384 if (VORTEX_PCM_TYPE(substream
->pcm
) != VORTEX_PCM_WT
)
385 current_ptr
= vortex_adbdma_getlinearpos(chip
, dma
);
388 current_ptr
= vortex_wtdma_getlinearpos(chip
, dma
);
390 //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
391 spin_unlock(&chip
->lock
);
392 return (bytes_to_frames(substream
->runtime
, current_ptr
));
397 static struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream, unsigned long offset) {
403 static struct snd_pcm_ops snd_vortex_playback_ops
= {
404 .open
= snd_vortex_pcm_open
,
405 .close
= snd_vortex_pcm_close
,
406 .ioctl
= snd_pcm_lib_ioctl
,
407 .hw_params
= snd_vortex_pcm_hw_params
,
408 .hw_free
= snd_vortex_pcm_hw_free
,
409 .prepare
= snd_vortex_pcm_prepare
,
410 .trigger
= snd_vortex_pcm_trigger
,
411 .pointer
= snd_vortex_pcm_pointer
,
412 .page
= snd_pcm_sgbuf_ops_page
,
416 * definitions of capture are omitted here...
419 static char *vortex_pcm_prettyname
[VORTEX_PCM_LAST
] = {
426 static char *vortex_pcm_name
[VORTEX_PCM_LAST
] = {
436 static int snd_vortex_spdif_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
438 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
443 static int snd_vortex_spdif_mask_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
445 ucontrol
->value
.iec958
.status
[0] = 0xff;
446 ucontrol
->value
.iec958
.status
[1] = 0xff;
447 ucontrol
->value
.iec958
.status
[2] = 0xff;
448 ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS
;
452 static int snd_vortex_spdif_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
454 vortex_t
*vortex
= snd_kcontrol_chip(kcontrol
);
455 ucontrol
->value
.iec958
.status
[0] = 0x00;
456 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_ORIGINAL
|IEC958_AES1_CON_DIGDIGCONV_ID
;
457 ucontrol
->value
.iec958
.status
[2] = 0x00;
458 switch (vortex
->spdif_sr
) {
459 case 32000: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_32000
; break;
460 case 44100: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_44100
; break;
461 case 48000: ucontrol
->value
.iec958
.status
[3] = IEC958_AES3_CON_FS_48000
; break;
466 static int snd_vortex_spdif_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
468 vortex_t
*vortex
= snd_kcontrol_chip(kcontrol
);
469 int spdif_sr
= 48000;
470 switch (ucontrol
->value
.iec958
.status
[3] & IEC958_AES3_CON_FS
) {
471 case IEC958_AES3_CON_FS_32000
: spdif_sr
= 32000; break;
472 case IEC958_AES3_CON_FS_44100
: spdif_sr
= 44100; break;
473 case IEC958_AES3_CON_FS_48000
: spdif_sr
= 48000; break;
475 if (spdif_sr
== vortex
->spdif_sr
)
477 vortex
->spdif_sr
= spdif_sr
;
478 vortex_spdif_init(vortex
, vortex
->spdif_sr
, 1);
483 static struct snd_kcontrol_new snd_vortex_mixer_spdif
[] __devinitdata
= {
485 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
486 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,DEFAULT
),
487 .info
= snd_vortex_spdif_info
,
488 .get
= snd_vortex_spdif_get
,
489 .put
= snd_vortex_spdif_put
,
492 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
493 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
494 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,CON_MASK
),
495 .info
= snd_vortex_spdif_info
,
496 .get
= snd_vortex_spdif_mask_get
500 /* create a pcm device */
501 static int __devinit
snd_vortex_new_pcm(vortex_t
* chip
, int idx
, int nr
)
504 struct snd_kcontrol
*kctl
;
508 if ((chip
== 0) || (idx
< 0) || (idx
>= VORTEX_PCM_LAST
))
511 /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
512 * same dma engine. WT uses it own separate dma engine whcih cant capture. */
513 if (idx
== VORTEX_PCM_ADB
)
518 snd_pcm_new(chip
->card
, vortex_pcm_prettyname
[idx
], idx
, nr
,
521 strcpy(pcm
->name
, vortex_pcm_name
[idx
]);
522 chip
->pcm
[idx
] = pcm
;
523 // This is an evil hack, but it saves a lot of duplicated code.
524 VORTEX_PCM_TYPE(pcm
) = idx
;
525 pcm
->private_data
= chip
;
527 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
528 &snd_vortex_playback_ops
);
529 if (idx
== VORTEX_PCM_ADB
)
530 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
531 &snd_vortex_playback_ops
);
533 /* pre-allocation of Scatter-Gather buffers */
535 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV_SG
,
536 snd_dma_pci_data(chip
->pci_dev
),
539 if (VORTEX_PCM_TYPE(pcm
) == VORTEX_PCM_SPDIF
) {
540 for (i
= 0; i
< ARRAY_SIZE(snd_vortex_mixer_spdif
); i
++) {
541 kctl
= snd_ctl_new1(&snd_vortex_mixer_spdif
[i
], chip
);
544 if ((err
= snd_ctl_add(chip
->card
, kctl
)) < 0)