1 // SPDX-License-Identifier: GPL-2.0-only
3 * Audio support for PS3
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
6 * Copyright 2006, 2007 Sony Corporation
9 #include <linux/dma-mapping.h>
10 #include <linux/dmapool.h>
11 #include <linux/gfp.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
15 #include <linux/module.h>
17 #include <sound/asound.h>
18 #include <sound/control.h>
19 #include <sound/core.h>
20 #include <sound/initval.h>
21 #include <sound/memalloc.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
26 #include <asm/firmware.h>
27 #include <asm/lv1call.h>
29 #include <asm/ps3av.h>
32 #include "snd_ps3_reg.h"
38 static struct snd_ps3_card_info the_card
;
40 static int snd_ps3_start_delay
= CONFIG_SND_PS3_DEFAULT_START_DELAY
;
42 module_param_named(start_delay
, snd_ps3_start_delay
, uint
, 0644);
43 MODULE_PARM_DESC(start_delay
, "time to insert silent data in ms");
45 static int index
= SNDRV_DEFAULT_IDX1
;
46 static char *id
= SNDRV_DEFAULT_STR1
;
48 module_param(index
, int, 0444);
49 MODULE_PARM_DESC(index
, "Index value for PS3 soundchip.");
50 module_param(id
, charp
, 0444);
51 MODULE_PARM_DESC(id
, "ID string for PS3 soundchip.");
55 * PS3 audio register access
57 static inline u32
read_reg(unsigned int reg
)
59 return in_be32(the_card
.mapped_mmio_vaddr
+ reg
);
61 static inline void write_reg(unsigned int reg
, u32 val
)
63 out_be32(the_card
.mapped_mmio_vaddr
+ reg
, val
);
65 static inline void update_reg(unsigned int reg
, u32 or_val
)
67 u32 newval
= read_reg(reg
) | or_val
;
68 write_reg(reg
, newval
);
70 static inline void update_mask_reg(unsigned int reg
, u32 mask
, u32 or_val
)
72 u32 newval
= (read_reg(reg
) & mask
) | or_val
;
73 write_reg(reg
, newval
);
79 static const struct snd_pcm_hardware snd_ps3_pcm_hw
= {
80 .info
= (SNDRV_PCM_INFO_MMAP
|
81 SNDRV_PCM_INFO_NONINTERLEAVED
|
82 SNDRV_PCM_INFO_MMAP_VALID
),
83 .formats
= (SNDRV_PCM_FMTBIT_S16_BE
|
84 SNDRV_PCM_FMTBIT_S24_BE
),
85 .rates
= (SNDRV_PCM_RATE_44100
|
86 SNDRV_PCM_RATE_48000
|
87 SNDRV_PCM_RATE_88200
|
88 SNDRV_PCM_RATE_96000
),
92 .channels_min
= 2, /* stereo only */
95 .buffer_bytes_max
= PS3_AUDIO_FIFO_SIZE
* 64,
97 /* interrupt by four stages */
98 .period_bytes_min
= PS3_AUDIO_FIFO_STAGE_SIZE
* 4,
99 .period_bytes_max
= PS3_AUDIO_FIFO_STAGE_SIZE
* 4,
102 .periods_max
= 32, /* buffer_size_max/ period_bytes_max */
104 .fifo_size
= PS3_AUDIO_FIFO_SIZE
107 static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info
*card
,
108 int count
, int force_stop
)
110 int dma_ch
, done
, retries
, stop_forced
= 0;
113 for (dma_ch
= 0; dma_ch
< 8; dma_ch
++) {
116 status
= read_reg(PS3_AUDIO_KICK(dma_ch
)) &
117 PS3_AUDIO_KICK_STATUS_MASK
;
119 case PS3_AUDIO_KICK_STATUS_DONE
:
120 case PS3_AUDIO_KICK_STATUS_NOTIFY
:
121 case PS3_AUDIO_KICK_STATUS_CLEAR
:
122 case PS3_AUDIO_KICK_STATUS_ERROR
:
129 } while (!done
&& --retries
);
130 if (!retries
&& force_stop
) {
131 pr_info("%s: DMA ch %d is not stopped.",
133 /* last resort. force to stop dma.
134 * NOTE: this cause DMA done interrupts
136 update_reg(PS3_AUDIO_CONFIG
, PS3_AUDIO_CONFIG_CLEAR
);
144 * wait for all dma is done.
145 * NOTE: caller should reset card->running before call.
146 * If not, the interrupt handler will re-start DMA,
147 * then DMA is never stopped.
149 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info
*card
)
153 * wait for the last dma is done
157 * expected maximum DMA done time is 5.7ms + something (DMA itself).
158 * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
159 * DMA kick event would occur.
161 stop_forced
= snd_ps3_verify_dma_stop(card
, 700, 1);
164 * clear outstanding interrupts.
166 update_reg(PS3_AUDIO_INTR_0
, 0);
167 update_reg(PS3_AUDIO_AX_IS
, 0);
170 *revert CLEAR bit since it will not reset automatically after DMA stop
173 update_mask_reg(PS3_AUDIO_CONFIG
, ~PS3_AUDIO_CONFIG_CLEAR
, 0);
174 /* ensure the hardware sees changes */
178 static void snd_ps3_kick_dma(struct snd_ps3_card_info
*card
)
181 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST
);
182 /* ensure the hardware sees the change */
187 * convert virtual addr to ioif bus addr.
189 static dma_addr_t
v_to_bus(struct snd_ps3_card_info
*card
, void *paddr
, int ch
)
191 return card
->dma_start_bus_addr
[ch
] +
192 (paddr
- card
->dma_start_vaddr
[ch
]);
197 * increment ring buffer pointer.
198 * NOTE: caller must hold write spinlock
200 static void snd_ps3_bump_buffer(struct snd_ps3_card_info
*card
,
201 enum snd_ps3_ch ch
, size_t byte_count
,
205 card
->dma_last_transfer_vaddr
[ch
] =
206 card
->dma_next_transfer_vaddr
[ch
];
207 card
->dma_next_transfer_vaddr
[ch
] += byte_count
;
208 if ((card
->dma_start_vaddr
[ch
] + (card
->dma_buffer_size
/ 2)) <=
209 card
->dma_next_transfer_vaddr
[ch
]) {
210 card
->dma_next_transfer_vaddr
[ch
] = card
->dma_start_vaddr
[ch
];
214 * setup dmac to send data to audio and attenuate samples on the ring buffer
216 static int snd_ps3_program_dma(struct snd_ps3_card_info
*card
,
217 enum snd_ps3_dma_filltype filltype
)
219 /* this dmac does not support over 4G */
221 int fill_stages
, dma_ch
, stage
;
223 uint32_t ch0_kick_event
= 0; /* initialize to mute gcc */
224 unsigned long irqsave
;
228 case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
:
231 case SND_PS3_DMA_FILLTYPE_FIRSTFILL
:
232 ch0_kick_event
= PS3_AUDIO_KICK_EVENT_ALWAYS
;
235 case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
:
238 case SND_PS3_DMA_FILLTYPE_RUNNING
:
239 ch0_kick_event
= PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY
;
243 snd_ps3_verify_dma_stop(card
, 700, 0);
245 spin_lock_irqsave(&card
->dma_lock
, irqsave
);
246 for (ch
= 0; ch
< 2; ch
++) {
247 for (stage
= 0; stage
< fill_stages
; stage
++) {
248 dma_ch
= stage
* 2 + ch
;
250 dma_addr
= card
->null_buffer_start_dma_addr
;
254 card
->dma_next_transfer_vaddr
[ch
],
257 write_reg(PS3_AUDIO_SOURCE(dma_ch
),
258 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY
|
261 /* dst: fixed to 3wire#0 */
263 write_reg(PS3_AUDIO_DEST(dma_ch
),
264 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO
|
265 PS3_AUDIO_AO_3W_LDATA(0)));
267 write_reg(PS3_AUDIO_DEST(dma_ch
),
268 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO
|
269 PS3_AUDIO_AO_3W_RDATA(0)));
271 /* count always 1 DMA block (1/2 stage = 128 bytes) */
272 write_reg(PS3_AUDIO_DMASIZE(dma_ch
), 0);
273 /* bump pointer if needed */
275 snd_ps3_bump_buffer(card
, ch
,
276 PS3_AUDIO_DMAC_BLOCK_SIZE
,
281 write_reg(PS3_AUDIO_KICK(dma_ch
),
284 write_reg(PS3_AUDIO_KICK(dma_ch
),
285 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
287 PS3_AUDIO_KICK_REQUEST
);
290 /* ensure the hardware sees the change */
292 spin_unlock_irqrestore(&card
->dma_lock
, irqsave
);
300 static irqreturn_t
snd_ps3_interrupt(int irq
, void *dev_id
)
304 int underflow_occured
= 0;
305 struct snd_ps3_card_info
*card
= dev_id
;
307 if (!card
->running
) {
308 update_reg(PS3_AUDIO_AX_IS
, 0);
309 update_reg(PS3_AUDIO_INTR_0
, 0);
313 port_intr
= read_reg(PS3_AUDIO_AX_IS
);
315 *serial buffer empty detected (every 4 times),
316 *program next dma and kick it
318 if (port_intr
& PS3_AUDIO_AX_IE_ASOBEIE(0)) {
319 write_reg(PS3_AUDIO_AX_IS
, PS3_AUDIO_AX_IE_ASOBEIE(0));
320 if (port_intr
& PS3_AUDIO_AX_IE_ASOBUIE(0)) {
321 write_reg(PS3_AUDIO_AX_IS
, port_intr
);
322 underflow_occured
= 1;
325 /* we are still in silent time */
326 snd_ps3_program_dma(card
,
327 (underflow_occured
) ?
328 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
:
329 SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
);
330 snd_ps3_kick_dma(card
);
333 snd_ps3_program_dma(card
,
334 (underflow_occured
) ?
335 SND_PS3_DMA_FILLTYPE_FIRSTFILL
:
336 SND_PS3_DMA_FILLTYPE_RUNNING
);
337 snd_ps3_kick_dma(card
);
338 snd_pcm_period_elapsed(card
->substream
);
340 } else if (port_intr
& PS3_AUDIO_AX_IE_ASOBUIE(0)) {
341 write_reg(PS3_AUDIO_AX_IS
, PS3_AUDIO_AX_IE_ASOBUIE(0));
343 * serial out underflow, but buffer empty not detected.
344 * in this case, fill fifo with 0 to recover. After
345 * filling dummy data, serial automatically start to
346 * consume them and then will generate normal buffer
348 * If both buffer underflow and buffer empty are occurred,
349 * it is better to do nomal data transfer than empty one
351 snd_ps3_program_dma(card
,
352 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
353 snd_ps3_kick_dma(card
);
354 snd_ps3_program_dma(card
,
355 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
356 snd_ps3_kick_dma(card
);
358 /* clear interrupt cause */
364 * mute_on : 0 output enabled
367 static int snd_ps3_mute(int mute_on
)
369 return ps3av_audio_mute(mute_on
);
374 * NOTE: calling this function may generate audio interrupt.
376 static int snd_ps3_change_avsetting(struct snd_ps3_card_info
*card
)
379 pr_debug("%s: start\n", __func__
);
381 ret
= ps3av_set_audio_mode(card
->avs
.avs_audio_ch
,
382 card
->avs
.avs_audio_rate
,
383 card
->avs
.avs_audio_width
,
384 card
->avs
.avs_audio_format
,
385 card
->avs
.avs_audio_source
);
387 * Reset the following unwanted settings:
390 /* disable all 3wire buffers */
391 update_mask_reg(PS3_AUDIO_AO_3WMCTRL
,
392 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
393 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
394 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
395 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
397 wmb(); /* ensure the hardware sees the change */
398 /* wait for actually stopped */
400 while ((read_reg(PS3_AUDIO_AO_3WMCTRL
) &
401 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
402 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
403 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
404 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
409 /* reset buffer pointer */
410 for (i
= 0; i
< 4; i
++) {
411 update_reg(PS3_AUDIO_AO_3WCTRL(i
),
412 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET
);
415 wmb(); /* ensure the hardware actually start resetting */
417 /* enable 3wire#0 buffer */
418 update_reg(PS3_AUDIO_AO_3WMCTRL
, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
421 /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
422 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
423 ~PS3_AUDIO_AO_3WCTRL_ASODF
,
424 PS3_AUDIO_AO_3WCTRL_ASODF_LSB
);
425 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
426 ~PS3_AUDIO_AO_SPDCTRL_SPODF
,
427 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB
);
428 /* ensure all the setting above is written back to register */
430 /* avsetting driver altered AX_IE, caller must reset it if you want */
431 pr_debug("%s: end\n", __func__
);
436 * set sampling rate according to the substream
438 static int snd_ps3_set_avsetting(struct snd_pcm_substream
*substream
)
440 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
441 struct snd_ps3_avsetting_info avs
;
446 pr_debug("%s: called freq=%d width=%d\n", __func__
,
447 substream
->runtime
->rate
,
448 snd_pcm_format_width(substream
->runtime
->format
));
450 pr_debug("%s: before freq=%d width=%d\n", __func__
,
451 card
->avs
.avs_audio_rate
, card
->avs
.avs_audio_width
);
454 switch (substream
->runtime
->rate
) {
456 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_44K
;
459 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_48K
;
462 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_88K
;
465 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_96K
;
468 pr_info("%s: invalid rate %d\n", __func__
,
469 substream
->runtime
->rate
);
474 switch (snd_pcm_format_width(substream
->runtime
->format
)) {
476 avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_16
;
479 avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_24
;
482 pr_info("%s: invalid width %d\n", __func__
,
483 snd_pcm_format_width(substream
->runtime
->format
));
487 memcpy(avs
.avs_cs_info
, ps3av_mode_cs_info
, 8);
489 if (memcmp(&card
->avs
, &avs
, sizeof(avs
))) {
490 pr_debug("%s: after freq=%d width=%d\n", __func__
,
491 card
->avs
.avs_audio_rate
, card
->avs
.avs_audio_width
);
494 snd_ps3_change_avsetting(card
);
499 /* check CS non-audio bit and mute accordingly */
500 if (avs
.avs_cs_info
[0] & 0x02)
501 ps3av_audio_mute_analog(1); /* mute if non-audio */
503 ps3av_audio_mute_analog(0);
511 static int snd_ps3_pcm_open(struct snd_pcm_substream
*substream
)
513 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
514 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
516 /* to retrieve substream/runtime in interrupt handler */
517 card
->substream
= substream
;
519 runtime
->hw
= snd_ps3_pcm_hw
;
521 card
->start_delay
= snd_ps3_start_delay
;
524 snd_ps3_mute(0); /* this function sleep */
526 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
527 PS3_AUDIO_FIFO_STAGE_SIZE
* 4 * 2);
531 static int snd_ps3_pcm_close(struct snd_pcm_substream
*substream
)
538 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream
*substream
,
539 unsigned int delay_ms
)
544 rate
= substream
->runtime
->rate
;
545 ret
= snd_pcm_format_size(substream
->runtime
->format
,
546 rate
* delay_ms
/ 1000)
547 * substream
->runtime
->channels
;
549 pr_debug("%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
553 snd_pcm_format_size(substream
->runtime
->format
, rate
),
554 rate
* delay_ms
/ 1000,
560 static int snd_ps3_pcm_prepare(struct snd_pcm_substream
*substream
)
562 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
563 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
564 unsigned long irqsave
;
566 if (!snd_ps3_set_avsetting(substream
)) {
567 /* some parameter changed */
568 write_reg(PS3_AUDIO_AX_IE
,
569 PS3_AUDIO_AX_IE_ASOBEIE(0) |
570 PS3_AUDIO_AX_IE_ASOBUIE(0));
572 * let SPDIF device re-lock with SPDIF signal,
573 * start with some silence
575 card
->silent
= snd_ps3_delay_to_bytes(substream
,
577 (PS3_AUDIO_FIFO_STAGE_SIZE
* 4); /* every 4 times */
580 /* restart ring buffer pointer */
581 spin_lock_irqsave(&card
->dma_lock
, irqsave
);
583 card
->dma_buffer_size
= runtime
->dma_bytes
;
585 card
->dma_last_transfer_vaddr
[SND_PS3_CH_L
] =
586 card
->dma_next_transfer_vaddr
[SND_PS3_CH_L
] =
587 card
->dma_start_vaddr
[SND_PS3_CH_L
] =
589 card
->dma_start_bus_addr
[SND_PS3_CH_L
] = runtime
->dma_addr
;
591 card
->dma_last_transfer_vaddr
[SND_PS3_CH_R
] =
592 card
->dma_next_transfer_vaddr
[SND_PS3_CH_R
] =
593 card
->dma_start_vaddr
[SND_PS3_CH_R
] =
594 runtime
->dma_area
+ (runtime
->dma_bytes
/ 2);
595 card
->dma_start_bus_addr
[SND_PS3_CH_R
] =
596 runtime
->dma_addr
+ (runtime
->dma_bytes
/ 2);
598 pr_debug("%s: vaddr=%p bus=%#llx\n", __func__
,
599 card
->dma_start_vaddr
[SND_PS3_CH_L
],
600 card
->dma_start_bus_addr
[SND_PS3_CH_L
]);
603 spin_unlock_irqrestore(&card
->dma_lock
, irqsave
);
605 /* ensure the hardware sees the change */
611 static int snd_ps3_pcm_trigger(struct snd_pcm_substream
*substream
,
614 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
617 case SNDRV_PCM_TRIGGER_START
:
618 /* clear outstanding interrupts */
619 update_reg(PS3_AUDIO_AX_IS
, 0);
621 spin_lock(&card
->dma_lock
);
625 spin_unlock(&card
->dma_lock
);
627 snd_ps3_program_dma(card
,
628 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
629 snd_ps3_kick_dma(card
);
630 while (read_reg(PS3_AUDIO_KICK(7)) &
631 PS3_AUDIO_KICK_STATUS_MASK
) {
634 snd_ps3_program_dma(card
, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
);
635 snd_ps3_kick_dma(card
);
638 case SNDRV_PCM_TRIGGER_STOP
:
639 spin_lock(&card
->dma_lock
);
643 spin_unlock(&card
->dma_lock
);
644 snd_ps3_wait_for_dma_stop(card
);
655 * report current pointer
657 static snd_pcm_uframes_t
snd_ps3_pcm_pointer(
658 struct snd_pcm_substream
*substream
)
660 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
662 snd_pcm_uframes_t ret
;
664 spin_lock(&card
->dma_lock
);
666 bytes
= (size_t)(card
->dma_last_transfer_vaddr
[SND_PS3_CH_L
] -
667 card
->dma_start_vaddr
[SND_PS3_CH_L
]);
669 spin_unlock(&card
->dma_lock
);
671 ret
= bytes_to_frames(substream
->runtime
, bytes
* 2);
677 * SPDIF status bits controls
679 static int snd_ps3_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
680 struct snd_ctl_elem_info
*uinfo
)
682 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
687 /* FIXME: ps3av_set_audio_mode() assumes only consumer mode */
688 static int snd_ps3_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
689 struct snd_ctl_elem_value
*ucontrol
)
691 memset(ucontrol
->value
.iec958
.status
, 0xff, 8);
695 static int snd_ps3_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
696 struct snd_ctl_elem_value
*ucontrol
)
701 static int snd_ps3_spdif_default_get(struct snd_kcontrol
*kcontrol
,
702 struct snd_ctl_elem_value
*ucontrol
)
704 memcpy(ucontrol
->value
.iec958
.status
, ps3av_mode_cs_info
, 8);
708 static int snd_ps3_spdif_default_put(struct snd_kcontrol
*kcontrol
,
709 struct snd_ctl_elem_value
*ucontrol
)
711 if (memcmp(ps3av_mode_cs_info
, ucontrol
->value
.iec958
.status
, 8)) {
712 memcpy(ps3av_mode_cs_info
, ucontrol
->value
.iec958
.status
, 8);
718 static const struct snd_kcontrol_new spdif_ctls
[] = {
720 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
721 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
722 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
723 .info
= snd_ps3_spdif_mask_info
,
724 .get
= snd_ps3_spdif_cmask_get
,
727 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
728 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
729 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
730 .info
= snd_ps3_spdif_mask_info
,
731 .get
= snd_ps3_spdif_pmask_get
,
734 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
735 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
736 .info
= snd_ps3_spdif_mask_info
,
737 .get
= snd_ps3_spdif_default_get
,
738 .put
= snd_ps3_spdif_default_put
,
742 static const struct snd_pcm_ops snd_ps3_pcm_spdif_ops
= {
743 .open
= snd_ps3_pcm_open
,
744 .close
= snd_ps3_pcm_close
,
745 .prepare
= snd_ps3_pcm_prepare
,
746 .trigger
= snd_ps3_pcm_trigger
,
747 .pointer
= snd_ps3_pcm_pointer
,
751 static int snd_ps3_map_mmio(void)
753 the_card
.mapped_mmio_vaddr
=
754 ioremap(the_card
.ps3_dev
->m_region
->bus_addr
,
755 the_card
.ps3_dev
->m_region
->len
);
757 if (!the_card
.mapped_mmio_vaddr
) {
758 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
759 __func__
, the_card
.ps3_dev
->m_region
->lpar_addr
,
760 the_card
.ps3_dev
->m_region
->len
);
767 static void snd_ps3_unmap_mmio(void)
769 iounmap(the_card
.mapped_mmio_vaddr
);
770 the_card
.mapped_mmio_vaddr
= NULL
;
773 static int snd_ps3_allocate_irq(void)
776 u64 lpar_addr
, lpar_size
;
779 /* FIXME: move this to device_init (H/W probe) */
782 ret
= lv1_gpu_device_map(1, &lpar_addr
, &lpar_size
);
784 pr_info("%s: device map 1 failed %d\n", __func__
,
789 mapped
= ioremap(lpar_addr
, lpar_size
);
791 pr_info("%s: ioremap 1 failed \n", __func__
);
795 the_card
.audio_irq_outlet
= in_be64(mapped
);
798 ret
= lv1_gpu_device_unmap(1);
800 pr_info("%s: unmap 1 failed\n", __func__
);
803 ret
= ps3_irq_plug_setup(PS3_BINDING_CPU_ANY
,
804 the_card
.audio_irq_outlet
,
807 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__
, ret
);
811 ret
= request_irq(the_card
.irq_no
, snd_ps3_interrupt
, 0,
812 SND_PS3_DRIVER_NAME
, &the_card
);
814 pr_info("%s: request_irq failed (%d)\n", __func__
, ret
);
821 ps3_irq_plug_destroy(the_card
.irq_no
);
825 static void snd_ps3_free_irq(void)
827 free_irq(the_card
.irq_no
, &the_card
);
828 ps3_irq_plug_destroy(the_card
.irq_no
);
831 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start
)
836 val
= (ioaddr_start
& (0x0fUL
<< 32)) >> (32 - 20) |
841 ret
= lv1_gpu_attribute(0x100, 0x007, val
);
843 pr_info("%s: gpu_attribute failed %d\n", __func__
,
847 static void snd_ps3_audio_fixup(struct snd_ps3_card_info
*card
)
850 * avsetting driver seems to never change the following
851 * so, init them here once
854 /* no dma interrupt needed */
855 write_reg(PS3_AUDIO_INTR_EN_0
, 0);
857 /* use every 4 buffer empty interrupt */
858 update_mask_reg(PS3_AUDIO_AX_IC
,
859 PS3_AUDIO_AX_IC_AASOIMD_MASK
,
860 PS3_AUDIO_AX_IC_AASOIMD_EVERY4
);
862 /* enable 3wire clocks */
863 update_mask_reg(PS3_AUDIO_AO_3WMCTRL
,
864 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED
|
865 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED
),
867 update_reg(PS3_AUDIO_AO_3WMCTRL
,
868 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT
);
871 static int snd_ps3_init_avsetting(struct snd_ps3_card_info
*card
)
874 pr_debug("%s: start\n", __func__
);
875 card
->avs
.avs_audio_ch
= PS3AV_CMD_AUDIO_NUM_OF_CH_2
;
876 card
->avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_48K
;
877 card
->avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_16
;
878 card
->avs
.avs_audio_format
= PS3AV_CMD_AUDIO_FORMAT_PCM
;
879 card
->avs
.avs_audio_source
= PS3AV_CMD_AUDIO_SOURCE_SERIAL
;
880 memcpy(card
->avs
.avs_cs_info
, ps3av_mode_cs_info
, 8);
882 ret
= snd_ps3_change_avsetting(card
);
884 snd_ps3_audio_fixup(card
);
886 /* to start to generate SPDIF signal, fill data */
887 snd_ps3_program_dma(card
, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
888 snd_ps3_kick_dma(card
);
889 pr_debug("%s: end\n", __func__
);
893 static int snd_ps3_driver_probe(struct ps3_system_bus_device
*dev
)
896 u64 lpar_addr
, lpar_size
;
897 static u64 dummy_mask
;
899 the_card
.ps3_dev
= dev
;
901 ret
= ps3_open_hv_device(dev
);
907 ret
= lv1_gpu_device_map(2, &lpar_addr
, &lpar_size
);
909 pr_info("%s: device map 2 failed %d\n", __func__
, ret
);
912 ps3_mmio_region_init(dev
, dev
->m_region
, lpar_addr
, lpar_size
,
915 ret
= snd_ps3_map_mmio();
920 ps3_dma_region_init(dev
, dev
->d_region
,
921 PAGE_SHIFT
, /* use system page size */
922 0, /* dma type; not used */
924 ALIGN(SND_PS3_DMA_REGION_SIZE
, PAGE_SIZE
));
925 dev
->d_region
->ioid
= PS3_AUDIO_IOID
;
927 ret
= ps3_dma_region_create(dev
->d_region
);
929 pr_info("%s: region_create\n", __func__
);
933 dummy_mask
= DMA_BIT_MASK(32);
934 dev
->core
.dma_mask
= &dummy_mask
;
935 dma_set_coherent_mask(&dev
->core
, dummy_mask
);
937 snd_ps3_audio_set_base_addr(dev
->d_region
->bus_addr
);
939 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
940 the_card
.start_delay
= snd_ps3_start_delay
;
943 if (snd_ps3_allocate_irq()) {
945 goto clean_dma_region
;
948 /* create card instance */
949 ret
= snd_card_new(&dev
->core
, index
, id
, THIS_MODULE
,
954 strcpy(the_card
.card
->driver
, "PS3");
955 strcpy(the_card
.card
->shortname
, "PS3");
956 strcpy(the_card
.card
->longname
, "PS3 sound");
958 /* create control elements */
959 for (i
= 0; i
< ARRAY_SIZE(spdif_ctls
); i
++) {
960 ret
= snd_ctl_add(the_card
.card
,
961 snd_ctl_new1(&spdif_ctls
[i
], &the_card
));
966 /* create PCM devices instance */
967 /* NOTE:this driver works assuming pcm:substream = 1:1 */
968 ret
= snd_pcm_new(the_card
.card
,
970 0, /* instance index, will be stored pcm.device*/
971 1, /* output substream */
972 0, /* input substream */
977 the_card
.pcm
->private_data
= &the_card
;
978 strcpy(the_card
.pcm
->name
, "SPDIF");
981 snd_pcm_set_ops(the_card
.pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
982 &snd_ps3_pcm_spdif_ops
);
984 the_card
.pcm
->info_flags
= SNDRV_PCM_INFO_NONINTERLEAVED
;
985 /* pre-alloc PCM DMA buffer*/
986 snd_pcm_set_managed_buffer_all(the_card
.pcm
,
989 SND_PS3_PCM_PREALLOC_SIZE
,
990 SND_PS3_PCM_PREALLOC_SIZE
);
993 * allocate null buffer
994 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
997 the_card
.null_buffer_start_vaddr
=
998 dma_alloc_coherent(&the_card
.ps3_dev
->core
,
1000 &the_card
.null_buffer_start_dma_addr
,
1002 if (!the_card
.null_buffer_start_vaddr
) {
1003 pr_info("%s: nullbuffer alloc failed\n", __func__
);
1007 pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__
,
1008 the_card
.null_buffer_start_vaddr
,
1009 the_card
.null_buffer_start_dma_addr
);
1010 /* set default sample rate/word width */
1011 snd_ps3_init_avsetting(&the_card
);
1013 /* register the card */
1014 ret
= snd_card_register(the_card
.card
);
1018 pr_info("%s started. start_delay=%dms\n",
1019 the_card
.card
->longname
, the_card
.start_delay
);
1023 dma_free_coherent(&the_card
.ps3_dev
->core
,
1025 the_card
.null_buffer_start_vaddr
,
1026 the_card
.null_buffer_start_dma_addr
);
1028 snd_card_free(the_card
.card
);
1032 ps3_dma_region_free(dev
->d_region
);
1034 snd_ps3_unmap_mmio();
1036 lv1_gpu_device_unmap(2);
1038 ps3_close_hv_device(dev
);
1040 * there is no destructor function to pcm.
1041 * midlayer automatically releases if the card removed
1044 }; /* snd_ps3_probe */
1046 /* called when module removal */
1047 static void snd_ps3_driver_remove(struct ps3_system_bus_device
*dev
)
1050 pr_info("%s:start id=%d\n", __func__
, dev
->match_id
);
1053 * ctl and preallocate buffer will be freed in
1056 ret
= snd_card_free(the_card
.card
);
1058 pr_info("%s: ctl freecard=%d\n", __func__
, ret
);
1060 dma_free_coherent(&dev
->core
,
1062 the_card
.null_buffer_start_vaddr
,
1063 the_card
.null_buffer_start_dma_addr
);
1065 ps3_dma_region_free(dev
->d_region
);
1068 snd_ps3_unmap_mmio();
1070 lv1_gpu_device_unmap(2);
1071 ps3_close_hv_device(dev
);
1072 pr_info("%s:end id=%d\n", __func__
, dev
->match_id
);
1073 } /* snd_ps3_remove */
1075 static struct ps3_system_bus_driver snd_ps3_bus_driver_info
= {
1076 .match_id
= PS3_MATCH_ID_SOUND
,
1077 .probe
= snd_ps3_driver_probe
,
1078 .remove
= snd_ps3_driver_remove
,
1079 .shutdown
= snd_ps3_driver_remove
,
1081 .name
= SND_PS3_DRIVER_NAME
,
1082 .owner
= THIS_MODULE
,
1088 * module/subsystem initialize/terminate
1090 static int __init
snd_ps3_init(void)
1094 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
1097 memset(&the_card
, 0, sizeof(the_card
));
1098 spin_lock_init(&the_card
.dma_lock
);
1100 /* register systembus DRIVER, this calls our probe() func */
1101 ret
= ps3_system_bus_driver_register(&snd_ps3_bus_driver_info
);
1105 module_init(snd_ps3_init
);
1107 static void __exit
snd_ps3_exit(void)
1109 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info
);
1111 module_exit(snd_ps3_exit
);
1113 MODULE_LICENSE("GPL v2");
1114 MODULE_DESCRIPTION("PS3 sound driver");
1115 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1116 MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND
);