2 * Audio support for PS3
3 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2 of the Licence.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/dma-mapping.h>
22 #include <linux/dmapool.h>
23 #include <linux/gfp.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
28 #include <sound/asound.h>
29 #include <sound/control.h>
30 #include <sound/core.h>
31 #include <sound/initval.h>
32 #include <sound/memalloc.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
37 #include <asm/firmware.h>
38 #include <asm/lv1call.h>
40 #include <asm/ps3av.h>
43 #include "snd_ps3_reg.h"
49 static struct snd_ps3_card_info the_card
;
51 static int snd_ps3_start_delay
= CONFIG_SND_PS3_DEFAULT_START_DELAY
;
53 module_param_named(start_delay
, snd_ps3_start_delay
, uint
, 0644);
54 MODULE_PARM_DESC(start_delay
, "time to insert silent data in ms");
56 static int index
= SNDRV_DEFAULT_IDX1
;
57 static char *id
= SNDRV_DEFAULT_STR1
;
59 module_param(index
, int, 0444);
60 MODULE_PARM_DESC(index
, "Index value for PS3 soundchip.");
61 module_param(id
, charp
, 0444);
62 MODULE_PARM_DESC(id
, "ID string for PS3 soundchip.");
66 * PS3 audio register access
68 static inline u32
read_reg(unsigned int reg
)
70 return in_be32(the_card
.mapped_mmio_vaddr
+ reg
);
72 static inline void write_reg(unsigned int reg
, u32 val
)
74 out_be32(the_card
.mapped_mmio_vaddr
+ reg
, val
);
76 static inline void update_reg(unsigned int reg
, u32 or_val
)
78 u32 newval
= read_reg(reg
) | or_val
;
79 write_reg(reg
, newval
);
81 static inline void update_mask_reg(unsigned int reg
, u32 mask
, u32 or_val
)
83 u32 newval
= (read_reg(reg
) & mask
) | or_val
;
84 write_reg(reg
, newval
);
90 static const struct snd_pcm_hardware snd_ps3_pcm_hw
= {
91 .info
= (SNDRV_PCM_INFO_MMAP
|
92 SNDRV_PCM_INFO_NONINTERLEAVED
|
93 SNDRV_PCM_INFO_MMAP_VALID
),
94 .formats
= (SNDRV_PCM_FMTBIT_S16_BE
|
95 SNDRV_PCM_FMTBIT_S24_BE
),
96 .rates
= (SNDRV_PCM_RATE_44100
|
97 SNDRV_PCM_RATE_48000
|
98 SNDRV_PCM_RATE_88200
|
99 SNDRV_PCM_RATE_96000
),
103 .channels_min
= 2, /* stereo only */
106 .buffer_bytes_max
= PS3_AUDIO_FIFO_SIZE
* 64,
108 /* interrupt by four stages */
109 .period_bytes_min
= PS3_AUDIO_FIFO_STAGE_SIZE
* 4,
110 .period_bytes_max
= PS3_AUDIO_FIFO_STAGE_SIZE
* 4,
113 .periods_max
= 32, /* buffer_size_max/ period_bytes_max */
115 .fifo_size
= PS3_AUDIO_FIFO_SIZE
118 static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info
*card
,
119 int count
, int force_stop
)
121 int dma_ch
, done
, retries
, stop_forced
= 0;
124 for (dma_ch
= 0; dma_ch
< 8; dma_ch
++) {
127 status
= read_reg(PS3_AUDIO_KICK(dma_ch
)) &
128 PS3_AUDIO_KICK_STATUS_MASK
;
130 case PS3_AUDIO_KICK_STATUS_DONE
:
131 case PS3_AUDIO_KICK_STATUS_NOTIFY
:
132 case PS3_AUDIO_KICK_STATUS_CLEAR
:
133 case PS3_AUDIO_KICK_STATUS_ERROR
:
140 } while (!done
&& --retries
);
141 if (!retries
&& force_stop
) {
142 pr_info("%s: DMA ch %d is not stopped.",
144 /* last resort. force to stop dma.
145 * NOTE: this cause DMA done interrupts
147 update_reg(PS3_AUDIO_CONFIG
, PS3_AUDIO_CONFIG_CLEAR
);
155 * wait for all dma is done.
156 * NOTE: caller should reset card->running before call.
157 * If not, the interrupt handler will re-start DMA,
158 * then DMA is never stopped.
160 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info
*card
)
164 * wait for the last dma is done
168 * expected maximum DMA done time is 5.7ms + something (DMA itself).
169 * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
170 * DMA kick event would occur.
172 stop_forced
= snd_ps3_verify_dma_stop(card
, 700, 1);
175 * clear outstanding interrupts.
177 update_reg(PS3_AUDIO_INTR_0
, 0);
178 update_reg(PS3_AUDIO_AX_IS
, 0);
181 *revert CLEAR bit since it will not reset automatically after DMA stop
184 update_mask_reg(PS3_AUDIO_CONFIG
, ~PS3_AUDIO_CONFIG_CLEAR
, 0);
185 /* ensure the hardware sees changes */
189 static void snd_ps3_kick_dma(struct snd_ps3_card_info
*card
)
192 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST
);
193 /* ensure the hardware sees the change */
198 * convert virtual addr to ioif bus addr.
200 static dma_addr_t
v_to_bus(struct snd_ps3_card_info
*card
, void *paddr
, int ch
)
202 return card
->dma_start_bus_addr
[ch
] +
203 (paddr
- card
->dma_start_vaddr
[ch
]);
208 * increment ring buffer pointer.
209 * NOTE: caller must hold write spinlock
211 static void snd_ps3_bump_buffer(struct snd_ps3_card_info
*card
,
212 enum snd_ps3_ch ch
, size_t byte_count
,
216 card
->dma_last_transfer_vaddr
[ch
] =
217 card
->dma_next_transfer_vaddr
[ch
];
218 card
->dma_next_transfer_vaddr
[ch
] += byte_count
;
219 if ((card
->dma_start_vaddr
[ch
] + (card
->dma_buffer_size
/ 2)) <=
220 card
->dma_next_transfer_vaddr
[ch
]) {
221 card
->dma_next_transfer_vaddr
[ch
] = card
->dma_start_vaddr
[ch
];
225 * setup dmac to send data to audio and attenuate samples on the ring buffer
227 static int snd_ps3_program_dma(struct snd_ps3_card_info
*card
,
228 enum snd_ps3_dma_filltype filltype
)
230 /* this dmac does not support over 4G */
232 int fill_stages
, dma_ch
, stage
;
234 uint32_t ch0_kick_event
= 0; /* initialize to mute gcc */
236 unsigned long irqsave
;
240 case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
:
242 /* intentionally fall thru */
243 case SND_PS3_DMA_FILLTYPE_FIRSTFILL
:
244 ch0_kick_event
= PS3_AUDIO_KICK_EVENT_ALWAYS
;
247 case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
:
249 /* intentionally fall thru */
250 case SND_PS3_DMA_FILLTYPE_RUNNING
:
251 ch0_kick_event
= PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY
;
255 snd_ps3_verify_dma_stop(card
, 700, 0);
257 spin_lock_irqsave(&card
->dma_lock
, irqsave
);
258 for (ch
= 0; ch
< 2; ch
++) {
259 start_vaddr
= card
->dma_next_transfer_vaddr
[0];
260 for (stage
= 0; stage
< fill_stages
; stage
++) {
261 dma_ch
= stage
* 2 + ch
;
263 dma_addr
= card
->null_buffer_start_dma_addr
;
267 card
->dma_next_transfer_vaddr
[ch
],
270 write_reg(PS3_AUDIO_SOURCE(dma_ch
),
271 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY
|
274 /* dst: fixed to 3wire#0 */
276 write_reg(PS3_AUDIO_DEST(dma_ch
),
277 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO
|
278 PS3_AUDIO_AO_3W_LDATA(0)));
280 write_reg(PS3_AUDIO_DEST(dma_ch
),
281 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO
|
282 PS3_AUDIO_AO_3W_RDATA(0)));
284 /* count always 1 DMA block (1/2 stage = 128 bytes) */
285 write_reg(PS3_AUDIO_DMASIZE(dma_ch
), 0);
286 /* bump pointer if needed */
288 snd_ps3_bump_buffer(card
, ch
,
289 PS3_AUDIO_DMAC_BLOCK_SIZE
,
294 write_reg(PS3_AUDIO_KICK(dma_ch
),
297 write_reg(PS3_AUDIO_KICK(dma_ch
),
298 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
300 PS3_AUDIO_KICK_REQUEST
);
303 /* ensure the hardware sees the change */
305 spin_unlock_irqrestore(&card
->dma_lock
, irqsave
);
313 static irqreturn_t
snd_ps3_interrupt(int irq
, void *dev_id
)
317 int underflow_occured
= 0;
318 struct snd_ps3_card_info
*card
= dev_id
;
320 if (!card
->running
) {
321 update_reg(PS3_AUDIO_AX_IS
, 0);
322 update_reg(PS3_AUDIO_INTR_0
, 0);
326 port_intr
= read_reg(PS3_AUDIO_AX_IS
);
328 *serial buffer empty detected (every 4 times),
329 *program next dma and kick it
331 if (port_intr
& PS3_AUDIO_AX_IE_ASOBEIE(0)) {
332 write_reg(PS3_AUDIO_AX_IS
, PS3_AUDIO_AX_IE_ASOBEIE(0));
333 if (port_intr
& PS3_AUDIO_AX_IE_ASOBUIE(0)) {
334 write_reg(PS3_AUDIO_AX_IS
, port_intr
);
335 underflow_occured
= 1;
338 /* we are still in silent time */
339 snd_ps3_program_dma(card
,
340 (underflow_occured
) ?
341 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
:
342 SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
);
343 snd_ps3_kick_dma(card
);
346 snd_ps3_program_dma(card
,
347 (underflow_occured
) ?
348 SND_PS3_DMA_FILLTYPE_FIRSTFILL
:
349 SND_PS3_DMA_FILLTYPE_RUNNING
);
350 snd_ps3_kick_dma(card
);
351 snd_pcm_period_elapsed(card
->substream
);
353 } else if (port_intr
& PS3_AUDIO_AX_IE_ASOBUIE(0)) {
354 write_reg(PS3_AUDIO_AX_IS
, PS3_AUDIO_AX_IE_ASOBUIE(0));
356 * serial out underflow, but buffer empty not detected.
357 * in this case, fill fifo with 0 to recover. After
358 * filling dummy data, serial automatically start to
359 * consume them and then will generate normal buffer
361 * If both buffer underflow and buffer empty are occured,
362 * it is better to do nomal data transfer than empty one
364 snd_ps3_program_dma(card
,
365 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
366 snd_ps3_kick_dma(card
);
367 snd_ps3_program_dma(card
,
368 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
369 snd_ps3_kick_dma(card
);
371 /* clear interrupt cause */
377 * mute_on : 0 output enabled
380 static int snd_ps3_mute(int mute_on
)
382 return ps3av_audio_mute(mute_on
);
387 * NOTE: calling this function may generate audio interrupt.
389 static int snd_ps3_change_avsetting(struct snd_ps3_card_info
*card
)
392 pr_debug("%s: start\n", __func__
);
394 ret
= ps3av_set_audio_mode(card
->avs
.avs_audio_ch
,
395 card
->avs
.avs_audio_rate
,
396 card
->avs
.avs_audio_width
,
397 card
->avs
.avs_audio_format
,
398 card
->avs
.avs_audio_source
);
400 * Reset the following unwanted settings:
403 /* disable all 3wire buffers */
404 update_mask_reg(PS3_AUDIO_AO_3WMCTRL
,
405 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
406 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
407 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
408 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
410 wmb(); /* ensure the hardware sees the change */
411 /* wait for actually stopped */
413 while ((read_reg(PS3_AUDIO_AO_3WMCTRL
) &
414 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
415 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
416 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
417 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
422 /* reset buffer pointer */
423 for (i
= 0; i
< 4; i
++) {
424 update_reg(PS3_AUDIO_AO_3WCTRL(i
),
425 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET
);
428 wmb(); /* ensure the hardware actually start resetting */
430 /* enable 3wire#0 buffer */
431 update_reg(PS3_AUDIO_AO_3WMCTRL
, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
434 /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
435 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
436 ~PS3_AUDIO_AO_3WCTRL_ASODF
,
437 PS3_AUDIO_AO_3WCTRL_ASODF_LSB
);
438 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
439 ~PS3_AUDIO_AO_SPDCTRL_SPODF
,
440 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB
);
441 /* ensure all the setting above is written back to register */
443 /* avsetting driver altered AX_IE, caller must reset it if you want */
444 pr_debug("%s: end\n", __func__
);
449 * set sampling rate according to the substream
451 static int snd_ps3_set_avsetting(struct snd_pcm_substream
*substream
)
453 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
454 struct snd_ps3_avsetting_info avs
;
459 pr_debug("%s: called freq=%d width=%d\n", __func__
,
460 substream
->runtime
->rate
,
461 snd_pcm_format_width(substream
->runtime
->format
));
463 pr_debug("%s: before freq=%d width=%d\n", __func__
,
464 card
->avs
.avs_audio_rate
, card
->avs
.avs_audio_width
);
467 switch (substream
->runtime
->rate
) {
469 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_44K
;
472 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_48K
;
475 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_88K
;
478 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_96K
;
481 pr_info("%s: invalid rate %d\n", __func__
,
482 substream
->runtime
->rate
);
487 switch (snd_pcm_format_width(substream
->runtime
->format
)) {
489 avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_16
;
492 avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_24
;
495 pr_info("%s: invalid width %d\n", __func__
,
496 snd_pcm_format_width(substream
->runtime
->format
));
500 memcpy(avs
.avs_cs_info
, ps3av_mode_cs_info
, 8);
502 if (memcmp(&card
->avs
, &avs
, sizeof(avs
))) {
503 pr_debug("%s: after freq=%d width=%d\n", __func__
,
504 card
->avs
.avs_audio_rate
, card
->avs
.avs_audio_width
);
507 snd_ps3_change_avsetting(card
);
512 /* check CS non-audio bit and mute accordingly */
513 if (avs
.avs_cs_info
[0] & 0x02)
514 ps3av_audio_mute_analog(1); /* mute if non-audio */
516 ps3av_audio_mute_analog(0);
524 static int snd_ps3_pcm_open(struct snd_pcm_substream
*substream
)
526 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
527 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
530 pcm_index
= substream
->pcm
->device
;
531 /* to retrieve substream/runtime in interrupt handler */
532 card
->substream
= substream
;
534 runtime
->hw
= snd_ps3_pcm_hw
;
536 card
->start_delay
= snd_ps3_start_delay
;
539 snd_ps3_mute(0); /* this function sleep */
541 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
542 PS3_AUDIO_FIFO_STAGE_SIZE
* 4 * 2);
546 static int snd_ps3_pcm_close(struct snd_pcm_substream
*substream
)
553 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream
*substream
,
554 struct snd_pcm_hw_params
*hw_params
)
558 /* alloc transport buffer */
559 size
= params_buffer_bytes(hw_params
);
560 snd_pcm_lib_malloc_pages(substream
, size
);
564 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream
*substream
)
567 ret
= snd_pcm_lib_free_pages(substream
);
571 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream
*substream
,
572 unsigned int delay_ms
)
577 rate
= substream
->runtime
->rate
;
578 ret
= snd_pcm_format_size(substream
->runtime
->format
,
579 rate
* delay_ms
/ 1000)
580 * substream
->runtime
->channels
;
582 pr_debug("%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
586 snd_pcm_format_size(substream
->runtime
->format
, rate
),
587 rate
* delay_ms
/ 1000,
593 static int snd_ps3_pcm_prepare(struct snd_pcm_substream
*substream
)
595 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
596 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
597 unsigned long irqsave
;
599 if (!snd_ps3_set_avsetting(substream
)) {
600 /* some parameter changed */
601 write_reg(PS3_AUDIO_AX_IE
,
602 PS3_AUDIO_AX_IE_ASOBEIE(0) |
603 PS3_AUDIO_AX_IE_ASOBUIE(0));
605 * let SPDIF device re-lock with SPDIF signal,
606 * start with some silence
608 card
->silent
= snd_ps3_delay_to_bytes(substream
,
610 (PS3_AUDIO_FIFO_STAGE_SIZE
* 4); /* every 4 times */
613 /* restart ring buffer pointer */
614 spin_lock_irqsave(&card
->dma_lock
, irqsave
);
616 card
->dma_buffer_size
= runtime
->dma_bytes
;
618 card
->dma_last_transfer_vaddr
[SND_PS3_CH_L
] =
619 card
->dma_next_transfer_vaddr
[SND_PS3_CH_L
] =
620 card
->dma_start_vaddr
[SND_PS3_CH_L
] =
622 card
->dma_start_bus_addr
[SND_PS3_CH_L
] = runtime
->dma_addr
;
624 card
->dma_last_transfer_vaddr
[SND_PS3_CH_R
] =
625 card
->dma_next_transfer_vaddr
[SND_PS3_CH_R
] =
626 card
->dma_start_vaddr
[SND_PS3_CH_R
] =
627 runtime
->dma_area
+ (runtime
->dma_bytes
/ 2);
628 card
->dma_start_bus_addr
[SND_PS3_CH_R
] =
629 runtime
->dma_addr
+ (runtime
->dma_bytes
/ 2);
631 pr_debug("%s: vaddr=%p bus=%#llx\n", __func__
,
632 card
->dma_start_vaddr
[SND_PS3_CH_L
],
633 card
->dma_start_bus_addr
[SND_PS3_CH_L
]);
636 spin_unlock_irqrestore(&card
->dma_lock
, irqsave
);
638 /* ensure the hardware sees the change */
644 static int snd_ps3_pcm_trigger(struct snd_pcm_substream
*substream
,
647 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
651 case SNDRV_PCM_TRIGGER_START
:
652 /* clear outstanding interrupts */
653 update_reg(PS3_AUDIO_AX_IS
, 0);
655 spin_lock(&card
->dma_lock
);
659 spin_unlock(&card
->dma_lock
);
661 snd_ps3_program_dma(card
,
662 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
663 snd_ps3_kick_dma(card
);
664 while (read_reg(PS3_AUDIO_KICK(7)) &
665 PS3_AUDIO_KICK_STATUS_MASK
) {
668 snd_ps3_program_dma(card
, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
);
669 snd_ps3_kick_dma(card
);
672 case SNDRV_PCM_TRIGGER_STOP
:
673 spin_lock(&card
->dma_lock
);
677 spin_unlock(&card
->dma_lock
);
678 snd_ps3_wait_for_dma_stop(card
);
689 * report current pointer
691 static snd_pcm_uframes_t
snd_ps3_pcm_pointer(
692 struct snd_pcm_substream
*substream
)
694 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
696 snd_pcm_uframes_t ret
;
698 spin_lock(&card
->dma_lock
);
700 bytes
= (size_t)(card
->dma_last_transfer_vaddr
[SND_PS3_CH_L
] -
701 card
->dma_start_vaddr
[SND_PS3_CH_L
]);
703 spin_unlock(&card
->dma_lock
);
705 ret
= bytes_to_frames(substream
->runtime
, bytes
* 2);
711 * SPDIF status bits controls
713 static int snd_ps3_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
714 struct snd_ctl_elem_info
*uinfo
)
716 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
721 /* FIXME: ps3av_set_audio_mode() assumes only consumer mode */
722 static int snd_ps3_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
723 struct snd_ctl_elem_value
*ucontrol
)
725 memset(ucontrol
->value
.iec958
.status
, 0xff, 8);
729 static int snd_ps3_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
730 struct snd_ctl_elem_value
*ucontrol
)
735 static int snd_ps3_spdif_default_get(struct snd_kcontrol
*kcontrol
,
736 struct snd_ctl_elem_value
*ucontrol
)
738 memcpy(ucontrol
->value
.iec958
.status
, ps3av_mode_cs_info
, 8);
742 static int snd_ps3_spdif_default_put(struct snd_kcontrol
*kcontrol
,
743 struct snd_ctl_elem_value
*ucontrol
)
745 if (memcmp(ps3av_mode_cs_info
, ucontrol
->value
.iec958
.status
, 8)) {
746 memcpy(ps3av_mode_cs_info
, ucontrol
->value
.iec958
.status
, 8);
752 static struct snd_kcontrol_new spdif_ctls
[] = {
754 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
755 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
756 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
757 .info
= snd_ps3_spdif_mask_info
,
758 .get
= snd_ps3_spdif_cmask_get
,
761 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
762 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
763 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
764 .info
= snd_ps3_spdif_mask_info
,
765 .get
= snd_ps3_spdif_pmask_get
,
768 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
769 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
770 .info
= snd_ps3_spdif_mask_info
,
771 .get
= snd_ps3_spdif_default_get
,
772 .put
= snd_ps3_spdif_default_put
,
776 static struct snd_pcm_ops snd_ps3_pcm_spdif_ops
= {
777 .open
= snd_ps3_pcm_open
,
778 .close
= snd_ps3_pcm_close
,
779 .ioctl
= snd_pcm_lib_ioctl
,
780 .hw_params
= snd_ps3_pcm_hw_params
,
781 .hw_free
= snd_ps3_pcm_hw_free
,
782 .prepare
= snd_ps3_pcm_prepare
,
783 .trigger
= snd_ps3_pcm_trigger
,
784 .pointer
= snd_ps3_pcm_pointer
,
788 static int __devinit
snd_ps3_map_mmio(void)
790 the_card
.mapped_mmio_vaddr
=
791 ioremap(the_card
.ps3_dev
->m_region
->bus_addr
,
792 the_card
.ps3_dev
->m_region
->len
);
794 if (!the_card
.mapped_mmio_vaddr
) {
795 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
796 __func__
, the_card
.ps3_dev
->m_region
->lpar_addr
,
797 the_card
.ps3_dev
->m_region
->len
);
804 static void snd_ps3_unmap_mmio(void)
806 iounmap(the_card
.mapped_mmio_vaddr
);
807 the_card
.mapped_mmio_vaddr
= NULL
;
810 static int __devinit
snd_ps3_allocate_irq(void)
813 u64 lpar_addr
, lpar_size
;
816 /* FIXME: move this to device_init (H/W probe) */
819 ret
= lv1_gpu_device_map(1, &lpar_addr
, &lpar_size
);
821 pr_info("%s: device map 1 failed %d\n", __func__
,
826 mapped
= ioremap(lpar_addr
, lpar_size
);
828 pr_info("%s: ioremap 1 failed \n", __func__
);
832 the_card
.audio_irq_outlet
= in_be64(mapped
);
835 ret
= lv1_gpu_device_unmap(1);
837 pr_info("%s: unmap 1 failed\n", __func__
);
840 ret
= ps3_irq_plug_setup(PS3_BINDING_CPU_ANY
,
841 the_card
.audio_irq_outlet
,
844 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__
, ret
);
848 ret
= request_irq(the_card
.irq_no
, snd_ps3_interrupt
, IRQF_DISABLED
,
849 SND_PS3_DRIVER_NAME
, &the_card
);
851 pr_info("%s: request_irq failed (%d)\n", __func__
, ret
);
858 ps3_irq_plug_destroy(the_card
.irq_no
);
862 static void snd_ps3_free_irq(void)
864 free_irq(the_card
.irq_no
, &the_card
);
865 ps3_irq_plug_destroy(the_card
.irq_no
);
868 static void __devinit
snd_ps3_audio_set_base_addr(uint64_t ioaddr_start
)
873 val
= (ioaddr_start
& (0x0fUL
<< 32)) >> (32 - 20) |
878 ret
= lv1_gpu_attribute(0x100, 0x007, val
, 0, 0);
880 pr_info("%s: gpu_attribute failed %d\n", __func__
,
884 static void __devinit
snd_ps3_audio_fixup(struct snd_ps3_card_info
*card
)
887 * avsetting driver seems to never change the followings
888 * so, init them here once
891 /* no dma interrupt needed */
892 write_reg(PS3_AUDIO_INTR_EN_0
, 0);
894 /* use every 4 buffer empty interrupt */
895 update_mask_reg(PS3_AUDIO_AX_IC
,
896 PS3_AUDIO_AX_IC_AASOIMD_MASK
,
897 PS3_AUDIO_AX_IC_AASOIMD_EVERY4
);
899 /* enable 3wire clocks */
900 update_mask_reg(PS3_AUDIO_AO_3WMCTRL
,
901 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED
|
902 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED
),
904 update_reg(PS3_AUDIO_AO_3WMCTRL
,
905 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT
);
908 static int __devinit
snd_ps3_init_avsetting(struct snd_ps3_card_info
*card
)
911 pr_debug("%s: start\n", __func__
);
912 card
->avs
.avs_audio_ch
= PS3AV_CMD_AUDIO_NUM_OF_CH_2
;
913 card
->avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_48K
;
914 card
->avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_16
;
915 card
->avs
.avs_audio_format
= PS3AV_CMD_AUDIO_FORMAT_PCM
;
916 card
->avs
.avs_audio_source
= PS3AV_CMD_AUDIO_SOURCE_SERIAL
;
917 memcpy(card
->avs
.avs_cs_info
, ps3av_mode_cs_info
, 8);
919 ret
= snd_ps3_change_avsetting(card
);
921 snd_ps3_audio_fixup(card
);
923 /* to start to generate SPDIF signal, fill data */
924 snd_ps3_program_dma(card
, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
925 snd_ps3_kick_dma(card
);
926 pr_debug("%s: end\n", __func__
);
930 static int __devinit
snd_ps3_driver_probe(struct ps3_system_bus_device
*dev
)
933 u64 lpar_addr
, lpar_size
;
935 BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1
));
936 BUG_ON(dev
->match_id
!= PS3_MATCH_ID_SOUND
);
938 the_card
.ps3_dev
= dev
;
940 ret
= ps3_open_hv_device(dev
);
946 ret
= lv1_gpu_device_map(2, &lpar_addr
, &lpar_size
);
948 pr_info("%s: device map 2 failed %d\n", __func__
, ret
);
951 ps3_mmio_region_init(dev
, dev
->m_region
, lpar_addr
, lpar_size
,
954 ret
= snd_ps3_map_mmio();
959 ps3_dma_region_init(dev
, dev
->d_region
,
960 PAGE_SHIFT
, /* use system page size */
961 0, /* dma type; not used */
963 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE
, PAGE_SIZE
));
964 dev
->d_region
->ioid
= PS3_AUDIO_IOID
;
966 ret
= ps3_dma_region_create(dev
->d_region
);
968 pr_info("%s: region_create\n", __func__
);
972 snd_ps3_audio_set_base_addr(dev
->d_region
->bus_addr
);
974 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
975 the_card
.start_delay
= snd_ps3_start_delay
;
978 if (snd_ps3_allocate_irq()) {
980 goto clean_dma_region
;
983 /* create card instance */
984 ret
= snd_card_create(index
, id
, THIS_MODULE
, 0, &the_card
.card
);
988 strcpy(the_card
.card
->driver
, "PS3");
989 strcpy(the_card
.card
->shortname
, "PS3");
990 strcpy(the_card
.card
->longname
, "PS3 sound");
992 /* create control elements */
993 for (i
= 0; i
< ARRAY_SIZE(spdif_ctls
); i
++) {
994 ret
= snd_ctl_add(the_card
.card
,
995 snd_ctl_new1(&spdif_ctls
[i
], &the_card
));
1000 /* create PCM devices instance */
1001 /* NOTE:this driver works assuming pcm:substream = 1:1 */
1002 ret
= snd_pcm_new(the_card
.card
,
1004 0, /* instance index, will be stored pcm.device*/
1005 1, /* output substream */
1006 0, /* input substream */
1011 the_card
.pcm
->private_data
= &the_card
;
1012 strcpy(the_card
.pcm
->name
, "SPDIF");
1015 snd_pcm_set_ops(the_card
.pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
1016 &snd_ps3_pcm_spdif_ops
);
1018 the_card
.pcm
->info_flags
= SNDRV_PCM_INFO_NONINTERLEAVED
;
1019 /* pre-alloc PCM DMA buffer*/
1020 ret
= snd_pcm_lib_preallocate_pages_for_all(the_card
.pcm
,
1023 SND_PS3_PCM_PREALLOC_SIZE
,
1024 SND_PS3_PCM_PREALLOC_SIZE
);
1026 pr_info("%s: prealloc failed\n", __func__
);
1031 * allocate null buffer
1032 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
1033 * PAGE_SIZE is enogh
1035 the_card
.null_buffer_start_vaddr
=
1036 dma_alloc_coherent(&the_card
.ps3_dev
->core
,
1038 &the_card
.null_buffer_start_dma_addr
,
1040 if (!the_card
.null_buffer_start_vaddr
) {
1041 pr_info("%s: nullbuffer alloc failed\n", __func__
);
1042 goto clean_preallocate
;
1044 pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__
,
1045 the_card
.null_buffer_start_vaddr
,
1046 the_card
.null_buffer_start_dma_addr
);
1047 /* set default sample rate/word width */
1048 snd_ps3_init_avsetting(&the_card
);
1050 /* register the card */
1051 snd_card_set_dev(the_card
.card
, &dev
->core
);
1052 ret
= snd_card_register(the_card
.card
);
1056 pr_info("%s started. start_delay=%dms\n",
1057 the_card
.card
->longname
, the_card
.start_delay
);
1061 dma_free_coherent(&the_card
.ps3_dev
->core
,
1063 the_card
.null_buffer_start_vaddr
,
1064 the_card
.null_buffer_start_dma_addr
);
1066 snd_pcm_lib_preallocate_free_for_all(the_card
.pcm
);
1068 snd_card_free(the_card
.card
);
1072 ps3_dma_region_free(dev
->d_region
);
1074 snd_ps3_unmap_mmio();
1076 lv1_gpu_device_unmap(2);
1078 ps3_close_hv_device(dev
);
1080 * there is no destructor function to pcm.
1081 * midlayer automatically releases if the card removed
1084 }; /* snd_ps3_probe */
1086 /* called when module removal */
1087 static int snd_ps3_driver_remove(struct ps3_system_bus_device
*dev
)
1090 pr_info("%s:start id=%d\n", __func__
, dev
->match_id
);
1091 if (dev
->match_id
!= PS3_MATCH_ID_SOUND
)
1095 * ctl and preallocate buffer will be freed in
1098 ret
= snd_card_free(the_card
.card
);
1100 pr_info("%s: ctl freecard=%d\n", __func__
, ret
);
1102 dma_free_coherent(&dev
->core
,
1104 the_card
.null_buffer_start_vaddr
,
1105 the_card
.null_buffer_start_dma_addr
);
1107 ps3_dma_region_free(dev
->d_region
);
1110 snd_ps3_unmap_mmio();
1112 lv1_gpu_device_unmap(2);
1113 ps3_close_hv_device(dev
);
1114 pr_info("%s:end id=%d\n", __func__
, dev
->match_id
);
1116 } /* snd_ps3_remove */
1118 static struct ps3_system_bus_driver snd_ps3_bus_driver_info
= {
1119 .match_id
= PS3_MATCH_ID_SOUND
,
1120 .probe
= snd_ps3_driver_probe
,
1121 .remove
= snd_ps3_driver_remove
,
1122 .shutdown
= snd_ps3_driver_remove
,
1124 .name
= SND_PS3_DRIVER_NAME
,
1125 .owner
= THIS_MODULE
,
1131 * module/subsystem initialize/terminate
1133 static int __init
snd_ps3_init(void)
1137 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
1140 memset(&the_card
, 0, sizeof(the_card
));
1141 spin_lock_init(&the_card
.dma_lock
);
1143 /* register systembus DRIVER, this calls our probe() func */
1144 ret
= ps3_system_bus_driver_register(&snd_ps3_bus_driver_info
);
1148 module_init(snd_ps3_init
);
1150 static void __exit
snd_ps3_exit(void)
1152 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info
);
1154 module_exit(snd_ps3_exit
);
1156 MODULE_LICENSE("GPL v2");
1157 MODULE_DESCRIPTION("PS3 sound driver");
1158 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1159 MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND
);