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/init.h>
22 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <sound/driver.h>
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/pcm.h>
29 #include <sound/asound.h>
30 #include <sound/memalloc.h>
31 #include <sound/pcm_params.h>
32 #include <sound/control.h>
33 #include <linux/dmapool.h>
34 #include <linux/dma-mapping.h>
35 #include <asm/firmware.h>
37 #include <asm/lv1call.h>
39 #include <asm/ps3av.h>
41 #include "snd_ps3_reg.h"
44 MODULE_LICENSE("GPL v2");
45 MODULE_DESCRIPTION("PS3 sound driver");
46 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
49 static int __init
snd_ps3_init(void);
50 static void __exit
snd_ps3_exit(void);
52 /* ALSA snd driver ops */
53 static int snd_ps3_pcm_open(struct snd_pcm_substream
*substream
);
54 static int snd_ps3_pcm_close(struct snd_pcm_substream
*substream
);
55 static int snd_ps3_pcm_prepare(struct snd_pcm_substream
*substream
);
56 static int snd_ps3_pcm_trigger(struct snd_pcm_substream
*substream
,
58 static snd_pcm_uframes_t
snd_ps3_pcm_pointer(struct snd_pcm_substream
60 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream
*substream
,
61 struct snd_pcm_hw_params
*hw_params
);
62 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream
*substream
);
65 /* ps3_system_bus_driver entries */
66 static int __init
snd_ps3_driver_probe(struct ps3_system_bus_device
*dev
);
67 static int snd_ps3_driver_remove(struct ps3_system_bus_device
*dev
);
70 static int snd_ps3_map_mmio(void);
71 static void snd_ps3_unmap_mmio(void);
72 static int snd_ps3_allocate_irq(void);
73 static void snd_ps3_free_irq(void);
74 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start
);
76 /* interrupt handler */
77 static irqreturn_t
snd_ps3_interrupt(int irq
, void *dev_id
);
80 /* set sampling rate/format */
81 static int snd_ps3_set_avsetting(struct snd_pcm_substream
*substream
);
82 /* take effect parameter change */
83 static int snd_ps3_change_avsetting(struct snd_ps3_card_info
*card
);
84 /* initialize avsetting and take it effect */
85 static int snd_ps3_init_avsetting(struct snd_ps3_card_info
*card
);
87 static int snd_ps3_program_dma(struct snd_ps3_card_info
*card
,
88 enum snd_ps3_dma_filltype filltype
);
89 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info
*card
);
91 static dma_addr_t
v_to_bus(struct snd_ps3_card_info
*, void *vaddr
, int ch
);
94 module_init(snd_ps3_init
);
95 module_exit(snd_ps3_exit
);
100 static struct snd_ps3_card_info the_card
;
102 static int snd_ps3_start_delay
= CONFIG_SND_PS3_DEFAULT_START_DELAY
;
104 module_param_named(start_delay
, snd_ps3_start_delay
, uint
, 0644);
105 MODULE_PARM_DESC(start_delay
, "time to insert silent data in milisec");
107 static int index
= SNDRV_DEFAULT_IDX1
;
108 static char *id
= SNDRV_DEFAULT_STR1
;
110 module_param(index
, int, 0444);
111 MODULE_PARM_DESC(index
, "Index value for PS3 soundchip.");
112 module_param(id
, charp
, 0444);
113 MODULE_PARM_DESC(id
, "ID string for PS3 soundchip.");
117 * PS3 audio register access
119 static inline u32
read_reg(unsigned int reg
)
121 return in_be32(the_card
.mapped_mmio_vaddr
+ reg
);
123 static inline void write_reg(unsigned int reg
, u32 val
)
125 out_be32(the_card
.mapped_mmio_vaddr
+ reg
, val
);
127 static inline void update_reg(unsigned int reg
, u32 or_val
)
129 u32 newval
= read_reg(reg
) | or_val
;
130 write_reg(reg
, newval
);
132 static inline void update_mask_reg(unsigned int reg
, u32 mask
, u32 or_val
)
134 u32 newval
= (read_reg(reg
) & mask
) | or_val
;
135 write_reg(reg
, newval
);
141 const static struct snd_pcm_hardware snd_ps3_pcm_hw
= {
142 .info
= (SNDRV_PCM_INFO_MMAP
|
143 SNDRV_PCM_INFO_NONINTERLEAVED
|
144 SNDRV_PCM_INFO_MMAP_VALID
),
145 .formats
= (SNDRV_PCM_FMTBIT_S16_BE
|
146 SNDRV_PCM_FMTBIT_S24_BE
),
147 .rates
= (SNDRV_PCM_RATE_44100
|
148 SNDRV_PCM_RATE_48000
|
149 SNDRV_PCM_RATE_88200
|
150 SNDRV_PCM_RATE_96000
),
154 .channels_min
= 2, /* stereo only */
157 .buffer_bytes_max
= PS3_AUDIO_FIFO_SIZE
* 64,
159 /* interrupt by four stages */
160 .period_bytes_min
= PS3_AUDIO_FIFO_STAGE_SIZE
* 4,
161 .period_bytes_max
= PS3_AUDIO_FIFO_STAGE_SIZE
* 4,
164 .periods_max
= 32, /* buffer_size_max/ period_bytes_max */
166 .fifo_size
= PS3_AUDIO_FIFO_SIZE
169 static struct snd_pcm_ops snd_ps3_pcm_spdif_ops
=
171 .open
= snd_ps3_pcm_open
,
172 .close
= snd_ps3_pcm_close
,
173 .prepare
= snd_ps3_pcm_prepare
,
174 .ioctl
= snd_pcm_lib_ioctl
,
175 .trigger
= snd_ps3_pcm_trigger
,
176 .pointer
= snd_ps3_pcm_pointer
,
177 .hw_params
= snd_ps3_pcm_hw_params
,
178 .hw_free
= snd_ps3_pcm_hw_free
181 static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info
*card
,
182 int count
, int force_stop
)
184 int dma_ch
, done
, retries
, stop_forced
= 0;
187 for (dma_ch
= 0; dma_ch
< 8; dma_ch
++) {
190 status
= read_reg(PS3_AUDIO_KICK(dma_ch
)) &
191 PS3_AUDIO_KICK_STATUS_MASK
;
193 case PS3_AUDIO_KICK_STATUS_DONE
:
194 case PS3_AUDIO_KICK_STATUS_NOTIFY
:
195 case PS3_AUDIO_KICK_STATUS_CLEAR
:
196 case PS3_AUDIO_KICK_STATUS_ERROR
:
203 } while (!done
&& --retries
);
204 if (!retries
&& force_stop
) {
205 pr_info("%s: DMA ch %d is not stopped.",
207 /* last resort. force to stop dma.
208 * NOTE: this cause DMA done interrupts
210 update_reg(PS3_AUDIO_CONFIG
, PS3_AUDIO_CONFIG_CLEAR
);
218 * wait for all dma is done.
219 * NOTE: caller should reset card->running before call.
220 * If not, the interrupt handler will re-start DMA,
221 * then DMA is never stopped.
223 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info
*card
)
227 * wait for the last dma is done
231 * expected maximum DMA done time is 5.7ms + something (DMA itself).
232 * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
233 * DMA kick event would occur.
235 stop_forced
= snd_ps3_verify_dma_stop(card
, 700, 1);
238 * clear outstanding interrupts.
240 update_reg(PS3_AUDIO_INTR_0
, 0);
241 update_reg(PS3_AUDIO_AX_IS
, 0);
244 *revert CLEAR bit since it will not reset automatically after DMA stop
247 update_mask_reg(PS3_AUDIO_CONFIG
, ~PS3_AUDIO_CONFIG_CLEAR
, 0);
248 /* ensure the hardware sees changes */
252 static void snd_ps3_kick_dma(struct snd_ps3_card_info
*card
)
255 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST
);
256 /* ensure the hardware sees the change */
261 * convert virtual addr to ioif bus addr.
263 static dma_addr_t
v_to_bus(struct snd_ps3_card_info
*card
,
267 return card
->dma_start_bus_addr
[ch
] +
268 (paddr
- card
->dma_start_vaddr
[ch
]);
273 * increment ring buffer pointer.
274 * NOTE: caller must hold write spinlock
276 static void snd_ps3_bump_buffer(struct snd_ps3_card_info
*card
,
277 enum snd_ps3_ch ch
, size_t byte_count
,
281 card
->dma_last_transfer_vaddr
[ch
] =
282 card
->dma_next_transfer_vaddr
[ch
];
283 card
->dma_next_transfer_vaddr
[ch
] += byte_count
;
284 if ((card
->dma_start_vaddr
[ch
] + (card
->dma_buffer_size
/ 2)) <=
285 card
->dma_next_transfer_vaddr
[ch
]) {
286 card
->dma_next_transfer_vaddr
[ch
] = card
->dma_start_vaddr
[ch
];
290 * setup dmac to send data to audio and attenuate samples on the ring buffer
292 static int snd_ps3_program_dma(struct snd_ps3_card_info
*card
,
293 enum snd_ps3_dma_filltype filltype
)
295 /* this dmac does not support over 4G */
297 int fill_stages
, dma_ch
, stage
;
299 uint32_t ch0_kick_event
= 0; /* initialize to mute gcc */
301 unsigned long irqsave
;
305 case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
:
307 /* intentionally fall thru */
308 case SND_PS3_DMA_FILLTYPE_FIRSTFILL
:
309 ch0_kick_event
= PS3_AUDIO_KICK_EVENT_ALWAYS
;
312 case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
:
314 /* intentionally fall thru */
315 case SND_PS3_DMA_FILLTYPE_RUNNING
:
316 ch0_kick_event
= PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY
;
320 snd_ps3_verify_dma_stop(card
, 700, 0);
322 spin_lock_irqsave(&card
->dma_lock
, irqsave
);
323 for (ch
= 0; ch
< 2; ch
++) {
324 start_vaddr
= card
->dma_next_transfer_vaddr
[0];
325 for (stage
= 0; stage
< fill_stages
; stage
++) {
326 dma_ch
= stage
* 2 + ch
;
328 dma_addr
= card
->null_buffer_start_dma_addr
;
332 card
->dma_next_transfer_vaddr
[ch
],
335 write_reg(PS3_AUDIO_SOURCE(dma_ch
),
336 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY
|
339 /* dst: fixed to 3wire#0 */
341 write_reg(PS3_AUDIO_DEST(dma_ch
),
342 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO
|
343 PS3_AUDIO_AO_3W_LDATA(0)));
345 write_reg(PS3_AUDIO_DEST(dma_ch
),
346 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO
|
347 PS3_AUDIO_AO_3W_RDATA(0)));
349 /* count always 1 DMA block (1/2 stage = 128 bytes) */
350 write_reg(PS3_AUDIO_DMASIZE(dma_ch
), 0);
351 /* bump pointer if needed */
353 snd_ps3_bump_buffer(card
, ch
,
354 PS3_AUDIO_DMAC_BLOCK_SIZE
,
359 write_reg(PS3_AUDIO_KICK(dma_ch
),
362 write_reg(PS3_AUDIO_KICK(dma_ch
),
363 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
365 PS3_AUDIO_KICK_REQUEST
);
368 /* ensure the hardware sees the change */
370 spin_unlock_irqrestore(&card
->dma_lock
, irqsave
);
377 * mute_on : 0 output enabled
380 static int snd_ps3_mute(int mute_on
)
382 return ps3av_audio_mute(mute_on
);
388 static int snd_ps3_pcm_open(struct snd_pcm_substream
*substream
)
390 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
391 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
394 pcm_index
= substream
->pcm
->device
;
395 /* to retrieve substream/runtime in interrupt handler */
396 card
->substream
= substream
;
398 runtime
->hw
= snd_ps3_pcm_hw
;
400 card
->start_delay
= snd_ps3_start_delay
;
403 snd_ps3_mute(0); /* this function sleep */
405 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
406 PS3_AUDIO_FIFO_STAGE_SIZE
* 4 * 2);
410 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream
*substream
,
411 struct snd_pcm_hw_params
*hw_params
)
415 /* alloc transport buffer */
416 size
= params_buffer_bytes(hw_params
);
417 snd_pcm_lib_malloc_pages(substream
, size
);
421 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream
*substream
,
422 unsigned int delay_ms
)
427 rate
= substream
->runtime
->rate
;
428 ret
= snd_pcm_format_size(substream
->runtime
->format
,
429 rate
* delay_ms
/ 1000)
430 * substream
->runtime
->channels
;
432 pr_debug(KERN_ERR
"%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
436 snd_pcm_format_size(substream
->runtime
->format
, rate
),
437 rate
* delay_ms
/ 1000,
443 static int snd_ps3_pcm_prepare(struct snd_pcm_substream
*substream
)
445 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
446 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
447 unsigned long irqsave
;
449 if (!snd_ps3_set_avsetting(substream
)) {
450 /* some parameter changed */
451 write_reg(PS3_AUDIO_AX_IE
,
452 PS3_AUDIO_AX_IE_ASOBEIE(0) |
453 PS3_AUDIO_AX_IE_ASOBUIE(0));
455 * let SPDIF device re-lock with SPDIF signal,
456 * start with some silence
458 card
->silent
= snd_ps3_delay_to_bytes(substream
,
460 (PS3_AUDIO_FIFO_STAGE_SIZE
* 4); /* every 4 times */
463 /* restart ring buffer pointer */
464 spin_lock_irqsave(&card
->dma_lock
, irqsave
);
466 card
->dma_buffer_size
= runtime
->dma_bytes
;
468 card
->dma_last_transfer_vaddr
[SND_PS3_CH_L
] =
469 card
->dma_next_transfer_vaddr
[SND_PS3_CH_L
] =
470 card
->dma_start_vaddr
[SND_PS3_CH_L
] =
472 card
->dma_start_bus_addr
[SND_PS3_CH_L
] = runtime
->dma_addr
;
474 card
->dma_last_transfer_vaddr
[SND_PS3_CH_R
] =
475 card
->dma_next_transfer_vaddr
[SND_PS3_CH_R
] =
476 card
->dma_start_vaddr
[SND_PS3_CH_R
] =
477 runtime
->dma_area
+ (runtime
->dma_bytes
/ 2);
478 card
->dma_start_bus_addr
[SND_PS3_CH_R
] =
479 runtime
->dma_addr
+ (runtime
->dma_bytes
/ 2);
481 pr_debug("%s: vaddr=%p bus=%#lx\n", __func__
,
482 card
->dma_start_vaddr
[SND_PS3_CH_L
],
483 card
->dma_start_bus_addr
[SND_PS3_CH_L
]);
486 spin_unlock_irqrestore(&card
->dma_lock
, irqsave
);
488 /* ensure the hardware sees the change */
494 static int snd_ps3_pcm_trigger(struct snd_pcm_substream
*substream
,
497 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
501 case SNDRV_PCM_TRIGGER_START
:
502 /* clear outstanding interrupts */
503 update_reg(PS3_AUDIO_AX_IS
, 0);
505 spin_lock(&card
->dma_lock
);
509 spin_unlock(&card
->dma_lock
);
511 snd_ps3_program_dma(card
,
512 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
513 snd_ps3_kick_dma(card
);
514 while (read_reg(PS3_AUDIO_KICK(7)) &
515 PS3_AUDIO_KICK_STATUS_MASK
) {
518 snd_ps3_program_dma(card
, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
);
519 snd_ps3_kick_dma(card
);
522 case SNDRV_PCM_TRIGGER_STOP
:
523 spin_lock(&card
->dma_lock
);
527 spin_unlock(&card
->dma_lock
);
528 snd_ps3_wait_for_dma_stop(card
);
539 * report current pointer
541 static snd_pcm_uframes_t
snd_ps3_pcm_pointer(
542 struct snd_pcm_substream
*substream
)
544 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
546 snd_pcm_uframes_t ret
;
548 spin_lock(&card
->dma_lock
);
550 bytes
= (size_t)(card
->dma_last_transfer_vaddr
[SND_PS3_CH_L
] -
551 card
->dma_start_vaddr
[SND_PS3_CH_L
]);
553 spin_unlock(&card
->dma_lock
);
555 ret
= bytes_to_frames(substream
->runtime
, bytes
* 2);
560 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream
*substream
)
563 ret
= snd_pcm_lib_free_pages(substream
);
567 static int snd_ps3_pcm_close(struct snd_pcm_substream
*substream
)
574 static void snd_ps3_audio_fixup(struct snd_ps3_card_info
*card
)
577 * avsetting driver seems to never change the followings
578 * so, init them here once
581 /* no dma interrupt needed */
582 write_reg(PS3_AUDIO_INTR_EN_0
, 0);
584 /* use every 4 buffer empty interrupt */
585 update_mask_reg(PS3_AUDIO_AX_IC
,
586 PS3_AUDIO_AX_IC_AASOIMD_MASK
,
587 PS3_AUDIO_AX_IC_AASOIMD_EVERY4
);
589 /* enable 3wire clocks */
590 update_mask_reg(PS3_AUDIO_AO_3WMCTRL
,
591 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED
|
592 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED
),
594 update_reg(PS3_AUDIO_AO_3WMCTRL
,
595 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT
);
600 * NOTE: calling this function may generate audio interrupt.
602 static int snd_ps3_change_avsetting(struct snd_ps3_card_info
*card
)
605 pr_debug("%s: start\n", __func__
);
607 ret
= ps3av_set_audio_mode(card
->avs
.avs_audio_ch
,
608 card
->avs
.avs_audio_rate
,
609 card
->avs
.avs_audio_width
,
610 card
->avs
.avs_audio_format
,
611 card
->avs
.avs_audio_source
);
613 * Reset the following unwanted settings:
616 /* disable all 3wire buffers */
617 update_mask_reg(PS3_AUDIO_AO_3WMCTRL
,
618 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
619 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
620 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
621 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
623 wmb(); /* ensure the hardware sees the change */
624 /* wait for actually stopped */
626 while ((read_reg(PS3_AUDIO_AO_3WMCTRL
) &
627 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
628 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
629 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
630 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
635 /* reset buffer pointer */
636 for (i
= 0; i
< 4; i
++) {
637 update_reg(PS3_AUDIO_AO_3WCTRL(i
),
638 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET
);
641 wmb(); /* ensure the hardware actually start resetting */
643 /* enable 3wire#0 buffer */
644 update_reg(PS3_AUDIO_AO_3WMCTRL
, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
647 /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
648 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
649 ~PS3_AUDIO_AO_3WCTRL_ASODF
,
650 PS3_AUDIO_AO_3WCTRL_ASODF_LSB
);
651 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
652 ~PS3_AUDIO_AO_SPDCTRL_SPODF
,
653 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB
);
654 /* ensure all the setting above is written back to register */
656 /* avsetting driver altered AX_IE, caller must reset it if you want */
657 pr_debug("%s: end\n", __func__
);
661 static int snd_ps3_init_avsetting(struct snd_ps3_card_info
*card
)
664 pr_debug("%s: start\n", __func__
);
665 card
->avs
.avs_audio_ch
= PS3AV_CMD_AUDIO_NUM_OF_CH_2
;
666 card
->avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_48K
;
667 card
->avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_16
;
668 card
->avs
.avs_audio_format
= PS3AV_CMD_AUDIO_FORMAT_PCM
;
669 card
->avs
.avs_audio_source
= PS3AV_CMD_AUDIO_SOURCE_SERIAL
;
671 ret
= snd_ps3_change_avsetting(card
);
673 snd_ps3_audio_fixup(card
);
675 /* to start to generate SPDIF signal, fill data */
676 snd_ps3_program_dma(card
, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
677 snd_ps3_kick_dma(card
);
678 pr_debug("%s: end\n", __func__
);
683 * set sampling rate according to the substream
685 static int snd_ps3_set_avsetting(struct snd_pcm_substream
*substream
)
687 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
688 struct snd_ps3_avsetting_info avs
;
692 pr_debug("%s: called freq=%d width=%d\n", __func__
,
693 substream
->runtime
->rate
,
694 snd_pcm_format_width(substream
->runtime
->format
));
696 pr_debug("%s: before freq=%d width=%d\n", __func__
,
697 card
->avs
.avs_audio_rate
, card
->avs
.avs_audio_width
);
700 switch (substream
->runtime
->rate
) {
702 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_44K
;
705 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_48K
;
708 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_88K
;
711 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_96K
;
714 pr_info("%s: invalid rate %d\n", __func__
,
715 substream
->runtime
->rate
);
720 switch (snd_pcm_format_width(substream
->runtime
->format
)) {
722 avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_16
;
725 avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_24
;
728 pr_info("%s: invalid width %d\n", __func__
,
729 snd_pcm_format_width(substream
->runtime
->format
));
733 if ((card
->avs
.avs_audio_width
!= avs
.avs_audio_width
) ||
734 (card
->avs
.avs_audio_rate
!= avs
.avs_audio_rate
)) {
736 snd_ps3_change_avsetting(card
);
738 pr_debug("%s: after freq=%d width=%d\n", __func__
,
739 card
->avs
.avs_audio_rate
, card
->avs
.avs_audio_width
);
748 static int snd_ps3_map_mmio(void)
750 the_card
.mapped_mmio_vaddr
=
751 ioremap(the_card
.ps3_dev
->m_region
->bus_addr
,
752 the_card
.ps3_dev
->m_region
->len
);
754 if (!the_card
.mapped_mmio_vaddr
) {
755 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
756 __func__
, the_card
.ps3_dev
->m_region
->lpar_addr
,
757 the_card
.ps3_dev
->m_region
->len
);
764 static void snd_ps3_unmap_mmio(void)
766 iounmap(the_card
.mapped_mmio_vaddr
);
767 the_card
.mapped_mmio_vaddr
= NULL
;
770 static int snd_ps3_allocate_irq(void)
773 u64 lpar_addr
, lpar_size
;
776 /* FIXME: move this to device_init (H/W probe) */
779 ret
= lv1_gpu_device_map(1, &lpar_addr
, &lpar_size
);
781 pr_info("%s: device map 1 failed %d\n", __func__
,
786 mapped
= ioremap(lpar_addr
, lpar_size
);
788 pr_info("%s: ioremap 1 failed \n", __func__
);
792 the_card
.audio_irq_outlet
= in_be64(mapped
);
795 ret
= lv1_gpu_device_unmap(1);
797 pr_info("%s: unmap 1 failed\n", __func__
);
800 ret
= ps3_irq_plug_setup(PS3_BINDING_CPU_ANY
,
801 the_card
.audio_irq_outlet
,
804 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__
, ret
);
808 ret
= request_irq(the_card
.irq_no
, snd_ps3_interrupt
, IRQF_DISABLED
,
809 SND_PS3_DRIVER_NAME
, &the_card
);
811 pr_info("%s: request_irq failed (%d)\n", __func__
, ret
);
818 ps3_irq_plug_destroy(the_card
.irq_no
);
822 static void snd_ps3_free_irq(void)
824 free_irq(the_card
.irq_no
, &the_card
);
825 ps3_irq_plug_destroy(the_card
.irq_no
);
828 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start
)
833 val
= (ioaddr_start
& (0x0fUL
<< 32)) >> (32 - 20) |
838 ret
= lv1_gpu_attribute(0x100, 0x007, val
, 0, 0);
840 pr_info("%s: gpu_attribute failed %d\n", __func__
,
844 static int __init
snd_ps3_driver_probe(struct ps3_system_bus_device
*dev
)
847 u64 lpar_addr
, lpar_size
;
849 BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1
));
850 BUG_ON(dev
->match_id
!= PS3_MATCH_ID_SOUND
);
852 the_card
.ps3_dev
= dev
;
854 ret
= ps3_open_hv_device(dev
);
860 ret
= lv1_gpu_device_map(2, &lpar_addr
, &lpar_size
);
862 pr_info("%s: device map 2 failed %d\n", __func__
, ret
);
865 ps3_mmio_region_init(dev
, dev
->m_region
, lpar_addr
, lpar_size
,
868 ret
= snd_ps3_map_mmio();
873 ps3_dma_region_init(dev
, dev
->d_region
,
874 PAGE_SHIFT
, /* use system page size */
875 0, /* dma type; not used */
877 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE
, PAGE_SIZE
));
878 dev
->d_region
->ioid
= PS3_AUDIO_IOID
;
880 ret
= ps3_dma_region_create(dev
->d_region
);
882 pr_info("%s: region_create\n", __func__
);
886 snd_ps3_audio_set_base_addr(dev
->d_region
->bus_addr
);
888 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
889 the_card
.start_delay
= snd_ps3_start_delay
;
892 if (snd_ps3_allocate_irq()) {
894 goto clean_dma_region
;
897 /* create card instance */
898 the_card
.card
= snd_card_new(index
, id
, THIS_MODULE
, 0);
899 if (!the_card
.card
) {
904 strcpy(the_card
.card
->driver
, "PS3");
905 strcpy(the_card
.card
->shortname
, "PS3");
906 strcpy(the_card
.card
->longname
, "PS3 sound");
907 /* create PCM devices instance */
908 /* NOTE:this driver works assuming pcm:substream = 1:1 */
909 ret
= snd_pcm_new(the_card
.card
,
911 0, /* instance index, will be stored pcm.device*/
912 1, /* output substream */
913 0, /* input substream */
918 the_card
.pcm
->private_data
= &the_card
;
919 strcpy(the_card
.pcm
->name
, "SPDIF");
922 snd_pcm_set_ops(the_card
.pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
923 &snd_ps3_pcm_spdif_ops
);
925 the_card
.pcm
->info_flags
= SNDRV_PCM_INFO_NONINTERLEAVED
;
926 /* pre-alloc PCM DMA buffer*/
927 ret
= snd_pcm_lib_preallocate_pages_for_all(the_card
.pcm
,
930 SND_PS3_PCM_PREALLOC_SIZE
,
931 SND_PS3_PCM_PREALLOC_SIZE
);
933 pr_info("%s: prealloc failed\n", __func__
);
938 * allocate null buffer
939 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
942 if (!(the_card
.null_buffer_start_vaddr
=
943 dma_alloc_coherent(&the_card
.ps3_dev
->core
,
945 &the_card
.null_buffer_start_dma_addr
,
947 pr_info("%s: nullbuffer alloc failed\n", __func__
);
948 goto clean_preallocate
;
950 pr_debug("%s: null vaddr=%p dma=%#lx\n", __func__
,
951 the_card
.null_buffer_start_vaddr
,
952 the_card
.null_buffer_start_dma_addr
);
953 /* set default sample rate/word width */
954 snd_ps3_init_avsetting(&the_card
);
956 /* register the card */
957 ret
= snd_card_register(the_card
.card
);
961 pr_info("%s started. start_delay=%dms\n",
962 the_card
.card
->longname
, the_card
.start_delay
);
966 dma_free_coherent(&the_card
.ps3_dev
->core
,
968 the_card
.null_buffer_start_vaddr
,
969 the_card
.null_buffer_start_dma_addr
);
971 snd_pcm_lib_preallocate_free_for_all(the_card
.pcm
);
973 snd_card_free(the_card
.card
);
977 ps3_dma_region_free(dev
->d_region
);
979 snd_ps3_unmap_mmio();
981 lv1_gpu_device_unmap(2);
983 ps3_close_hv_device(dev
);
985 * there is no destructor function to pcm.
986 * midlayer automatically releases if the card removed
989 }; /* snd_ps3_probe */
991 /* called when module removal */
992 static int snd_ps3_driver_remove(struct ps3_system_bus_device
*dev
)
995 pr_info("%s:start id=%d\n", __func__
, dev
->match_id
);
996 if (dev
->match_id
!= PS3_MATCH_ID_SOUND
)
1000 * ctl and preallocate buffer will be freed in
1003 ret
= snd_card_free(the_card
.card
);
1005 pr_info("%s: ctl freecard=%d\n", __func__
, ret
);
1007 dma_free_coherent(&dev
->core
,
1009 the_card
.null_buffer_start_vaddr
,
1010 the_card
.null_buffer_start_dma_addr
);
1012 ps3_dma_region_free(dev
->d_region
);
1015 snd_ps3_unmap_mmio();
1017 lv1_gpu_device_unmap(2);
1018 ps3_close_hv_device(dev
);
1019 pr_info("%s:end id=%d\n", __func__
, dev
->match_id
);
1021 } /* snd_ps3_remove */
1023 static struct ps3_system_bus_driver snd_ps3_bus_driver_info
= {
1024 .match_id
= PS3_MATCH_ID_SOUND
,
1025 .probe
= snd_ps3_driver_probe
,
1026 .remove
= snd_ps3_driver_remove
,
1027 .shutdown
= snd_ps3_driver_remove
,
1029 .name
= SND_PS3_DRIVER_NAME
,
1030 .owner
= THIS_MODULE
,
1038 static irqreturn_t
snd_ps3_interrupt(int irq
, void *dev_id
)
1042 int underflow_occured
= 0;
1043 struct snd_ps3_card_info
*card
= dev_id
;
1045 if (!card
->running
) {
1046 update_reg(PS3_AUDIO_AX_IS
, 0);
1047 update_reg(PS3_AUDIO_INTR_0
, 0);
1051 port_intr
= read_reg(PS3_AUDIO_AX_IS
);
1053 *serial buffer empty detected (every 4 times),
1054 *program next dma and kick it
1056 if (port_intr
& PS3_AUDIO_AX_IE_ASOBEIE(0)) {
1057 write_reg(PS3_AUDIO_AX_IS
, PS3_AUDIO_AX_IE_ASOBEIE(0));
1058 if (port_intr
& PS3_AUDIO_AX_IE_ASOBUIE(0)) {
1059 write_reg(PS3_AUDIO_AX_IS
, port_intr
);
1060 underflow_occured
= 1;
1063 /* we are still in silent time */
1064 snd_ps3_program_dma(card
,
1065 (underflow_occured
) ?
1066 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
:
1067 SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
);
1068 snd_ps3_kick_dma(card
);
1071 snd_ps3_program_dma(card
,
1072 (underflow_occured
) ?
1073 SND_PS3_DMA_FILLTYPE_FIRSTFILL
:
1074 SND_PS3_DMA_FILLTYPE_RUNNING
);
1075 snd_ps3_kick_dma(card
);
1076 snd_pcm_period_elapsed(card
->substream
);
1078 } else if (port_intr
& PS3_AUDIO_AX_IE_ASOBUIE(0)) {
1079 write_reg(PS3_AUDIO_AX_IS
, PS3_AUDIO_AX_IE_ASOBUIE(0));
1081 * serial out underflow, but buffer empty not detected.
1082 * in this case, fill fifo with 0 to recover. After
1083 * filling dummy data, serial automatically start to
1084 * consume them and then will generate normal buffer
1086 * If both buffer underflow and buffer empty are occured,
1087 * it is better to do nomal data transfer than empty one
1089 snd_ps3_program_dma(card
,
1090 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
1091 snd_ps3_kick_dma(card
);
1092 snd_ps3_program_dma(card
,
1093 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
1094 snd_ps3_kick_dma(card
);
1096 /* clear interrupt cause */
1101 * module/subsystem initialize/terminate
1103 static int __init
snd_ps3_init(void)
1107 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
1110 memset(&the_card
, 0, sizeof(the_card
));
1111 spin_lock_init(&the_card
.dma_lock
);
1113 /* register systembus DRIVER, this calls our probe() func */
1114 ret
= ps3_system_bus_driver_register(&snd_ps3_bus_driver_info
);
1119 static void __exit
snd_ps3_exit(void)
1121 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info
);
1124 MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND
);