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/core.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/asound.h>
29 #include <sound/memalloc.h>
30 #include <sound/pcm_params.h>
31 #include <sound/control.h>
32 #include <linux/dmapool.h>
33 #include <linux/dma-mapping.h>
34 #include <asm/firmware.h>
36 #include <asm/lv1call.h>
38 #include <asm/ps3av.h>
40 #include "snd_ps3_reg.h"
43 MODULE_LICENSE("GPL v2");
44 MODULE_DESCRIPTION("PS3 sound driver");
45 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
48 static int __init
snd_ps3_init(void);
49 static void __exit
snd_ps3_exit(void);
51 /* ALSA snd driver ops */
52 static int snd_ps3_pcm_open(struct snd_pcm_substream
*substream
);
53 static int snd_ps3_pcm_close(struct snd_pcm_substream
*substream
);
54 static int snd_ps3_pcm_prepare(struct snd_pcm_substream
*substream
);
55 static int snd_ps3_pcm_trigger(struct snd_pcm_substream
*substream
,
57 static snd_pcm_uframes_t
snd_ps3_pcm_pointer(struct snd_pcm_substream
59 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream
*substream
,
60 struct snd_pcm_hw_params
*hw_params
);
61 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream
*substream
);
64 /* ps3_system_bus_driver entries */
65 static int __init
snd_ps3_driver_probe(struct ps3_system_bus_device
*dev
);
66 static int snd_ps3_driver_remove(struct ps3_system_bus_device
*dev
);
69 static int snd_ps3_map_mmio(void);
70 static void snd_ps3_unmap_mmio(void);
71 static int snd_ps3_allocate_irq(void);
72 static void snd_ps3_free_irq(void);
73 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start
);
75 /* interrupt handler */
76 static irqreturn_t
snd_ps3_interrupt(int irq
, void *dev_id
);
79 /* set sampling rate/format */
80 static int snd_ps3_set_avsetting(struct snd_pcm_substream
*substream
);
81 /* take effect parameter change */
82 static int snd_ps3_change_avsetting(struct snd_ps3_card_info
*card
);
83 /* initialize avsetting and take it effect */
84 static int snd_ps3_init_avsetting(struct snd_ps3_card_info
*card
);
86 static int snd_ps3_program_dma(struct snd_ps3_card_info
*card
,
87 enum snd_ps3_dma_filltype filltype
);
88 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info
*card
);
90 static dma_addr_t
v_to_bus(struct snd_ps3_card_info
*, void *vaddr
, int ch
);
93 module_init(snd_ps3_init
);
94 module_exit(snd_ps3_exit
);
99 static struct snd_ps3_card_info the_card
;
101 static int snd_ps3_start_delay
= CONFIG_SND_PS3_DEFAULT_START_DELAY
;
103 module_param_named(start_delay
, snd_ps3_start_delay
, uint
, 0644);
104 MODULE_PARM_DESC(start_delay
, "time to insert silent data in milisec");
106 static int index
= SNDRV_DEFAULT_IDX1
;
107 static char *id
= SNDRV_DEFAULT_STR1
;
109 module_param(index
, int, 0444);
110 MODULE_PARM_DESC(index
, "Index value for PS3 soundchip.");
111 module_param(id
, charp
, 0444);
112 MODULE_PARM_DESC(id
, "ID string for PS3 soundchip.");
116 * PS3 audio register access
118 static inline u32
read_reg(unsigned int reg
)
120 return in_be32(the_card
.mapped_mmio_vaddr
+ reg
);
122 static inline void write_reg(unsigned int reg
, u32 val
)
124 out_be32(the_card
.mapped_mmio_vaddr
+ reg
, val
);
126 static inline void update_reg(unsigned int reg
, u32 or_val
)
128 u32 newval
= read_reg(reg
) | or_val
;
129 write_reg(reg
, newval
);
131 static inline void update_mask_reg(unsigned int reg
, u32 mask
, u32 or_val
)
133 u32 newval
= (read_reg(reg
) & mask
) | or_val
;
134 write_reg(reg
, newval
);
140 const static struct snd_pcm_hardware snd_ps3_pcm_hw
= {
141 .info
= (SNDRV_PCM_INFO_MMAP
|
142 SNDRV_PCM_INFO_NONINTERLEAVED
|
143 SNDRV_PCM_INFO_MMAP_VALID
),
144 .formats
= (SNDRV_PCM_FMTBIT_S16_BE
|
145 SNDRV_PCM_FMTBIT_S24_BE
),
146 .rates
= (SNDRV_PCM_RATE_44100
|
147 SNDRV_PCM_RATE_48000
|
148 SNDRV_PCM_RATE_88200
|
149 SNDRV_PCM_RATE_96000
),
153 .channels_min
= 2, /* stereo only */
156 .buffer_bytes_max
= PS3_AUDIO_FIFO_SIZE
* 64,
158 /* interrupt by four stages */
159 .period_bytes_min
= PS3_AUDIO_FIFO_STAGE_SIZE
* 4,
160 .period_bytes_max
= PS3_AUDIO_FIFO_STAGE_SIZE
* 4,
163 .periods_max
= 32, /* buffer_size_max/ period_bytes_max */
165 .fifo_size
= PS3_AUDIO_FIFO_SIZE
168 static struct snd_pcm_ops snd_ps3_pcm_spdif_ops
=
170 .open
= snd_ps3_pcm_open
,
171 .close
= snd_ps3_pcm_close
,
172 .prepare
= snd_ps3_pcm_prepare
,
173 .ioctl
= snd_pcm_lib_ioctl
,
174 .trigger
= snd_ps3_pcm_trigger
,
175 .pointer
= snd_ps3_pcm_pointer
,
176 .hw_params
= snd_ps3_pcm_hw_params
,
177 .hw_free
= snd_ps3_pcm_hw_free
180 static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info
*card
,
181 int count
, int force_stop
)
183 int dma_ch
, done
, retries
, stop_forced
= 0;
186 for (dma_ch
= 0; dma_ch
< 8; dma_ch
++) {
189 status
= read_reg(PS3_AUDIO_KICK(dma_ch
)) &
190 PS3_AUDIO_KICK_STATUS_MASK
;
192 case PS3_AUDIO_KICK_STATUS_DONE
:
193 case PS3_AUDIO_KICK_STATUS_NOTIFY
:
194 case PS3_AUDIO_KICK_STATUS_CLEAR
:
195 case PS3_AUDIO_KICK_STATUS_ERROR
:
202 } while (!done
&& --retries
);
203 if (!retries
&& force_stop
) {
204 pr_info("%s: DMA ch %d is not stopped.",
206 /* last resort. force to stop dma.
207 * NOTE: this cause DMA done interrupts
209 update_reg(PS3_AUDIO_CONFIG
, PS3_AUDIO_CONFIG_CLEAR
);
217 * wait for all dma is done.
218 * NOTE: caller should reset card->running before call.
219 * If not, the interrupt handler will re-start DMA,
220 * then DMA is never stopped.
222 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info
*card
)
226 * wait for the last dma is done
230 * expected maximum DMA done time is 5.7ms + something (DMA itself).
231 * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
232 * DMA kick event would occur.
234 stop_forced
= snd_ps3_verify_dma_stop(card
, 700, 1);
237 * clear outstanding interrupts.
239 update_reg(PS3_AUDIO_INTR_0
, 0);
240 update_reg(PS3_AUDIO_AX_IS
, 0);
243 *revert CLEAR bit since it will not reset automatically after DMA stop
246 update_mask_reg(PS3_AUDIO_CONFIG
, ~PS3_AUDIO_CONFIG_CLEAR
, 0);
247 /* ensure the hardware sees changes */
251 static void snd_ps3_kick_dma(struct snd_ps3_card_info
*card
)
254 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST
);
255 /* ensure the hardware sees the change */
260 * convert virtual addr to ioif bus addr.
262 static dma_addr_t
v_to_bus(struct snd_ps3_card_info
*card
,
266 return card
->dma_start_bus_addr
[ch
] +
267 (paddr
- card
->dma_start_vaddr
[ch
]);
272 * increment ring buffer pointer.
273 * NOTE: caller must hold write spinlock
275 static void snd_ps3_bump_buffer(struct snd_ps3_card_info
*card
,
276 enum snd_ps3_ch ch
, size_t byte_count
,
280 card
->dma_last_transfer_vaddr
[ch
] =
281 card
->dma_next_transfer_vaddr
[ch
];
282 card
->dma_next_transfer_vaddr
[ch
] += byte_count
;
283 if ((card
->dma_start_vaddr
[ch
] + (card
->dma_buffer_size
/ 2)) <=
284 card
->dma_next_transfer_vaddr
[ch
]) {
285 card
->dma_next_transfer_vaddr
[ch
] = card
->dma_start_vaddr
[ch
];
289 * setup dmac to send data to audio and attenuate samples on the ring buffer
291 static int snd_ps3_program_dma(struct snd_ps3_card_info
*card
,
292 enum snd_ps3_dma_filltype filltype
)
294 /* this dmac does not support over 4G */
296 int fill_stages
, dma_ch
, stage
;
298 uint32_t ch0_kick_event
= 0; /* initialize to mute gcc */
300 unsigned long irqsave
;
304 case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
:
306 /* intentionally fall thru */
307 case SND_PS3_DMA_FILLTYPE_FIRSTFILL
:
308 ch0_kick_event
= PS3_AUDIO_KICK_EVENT_ALWAYS
;
311 case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
:
313 /* intentionally fall thru */
314 case SND_PS3_DMA_FILLTYPE_RUNNING
:
315 ch0_kick_event
= PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY
;
319 snd_ps3_verify_dma_stop(card
, 700, 0);
321 spin_lock_irqsave(&card
->dma_lock
, irqsave
);
322 for (ch
= 0; ch
< 2; ch
++) {
323 start_vaddr
= card
->dma_next_transfer_vaddr
[0];
324 for (stage
= 0; stage
< fill_stages
; stage
++) {
325 dma_ch
= stage
* 2 + ch
;
327 dma_addr
= card
->null_buffer_start_dma_addr
;
331 card
->dma_next_transfer_vaddr
[ch
],
334 write_reg(PS3_AUDIO_SOURCE(dma_ch
),
335 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY
|
338 /* dst: fixed to 3wire#0 */
340 write_reg(PS3_AUDIO_DEST(dma_ch
),
341 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO
|
342 PS3_AUDIO_AO_3W_LDATA(0)));
344 write_reg(PS3_AUDIO_DEST(dma_ch
),
345 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO
|
346 PS3_AUDIO_AO_3W_RDATA(0)));
348 /* count always 1 DMA block (1/2 stage = 128 bytes) */
349 write_reg(PS3_AUDIO_DMASIZE(dma_ch
), 0);
350 /* bump pointer if needed */
352 snd_ps3_bump_buffer(card
, ch
,
353 PS3_AUDIO_DMAC_BLOCK_SIZE
,
358 write_reg(PS3_AUDIO_KICK(dma_ch
),
361 write_reg(PS3_AUDIO_KICK(dma_ch
),
362 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
364 PS3_AUDIO_KICK_REQUEST
);
367 /* ensure the hardware sees the change */
369 spin_unlock_irqrestore(&card
->dma_lock
, irqsave
);
376 * mute_on : 0 output enabled
379 static int snd_ps3_mute(int mute_on
)
381 return ps3av_audio_mute(mute_on
);
387 static int snd_ps3_pcm_open(struct snd_pcm_substream
*substream
)
389 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
390 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
393 pcm_index
= substream
->pcm
->device
;
394 /* to retrieve substream/runtime in interrupt handler */
395 card
->substream
= substream
;
397 runtime
->hw
= snd_ps3_pcm_hw
;
399 card
->start_delay
= snd_ps3_start_delay
;
402 snd_ps3_mute(0); /* this function sleep */
404 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
405 PS3_AUDIO_FIFO_STAGE_SIZE
* 4 * 2);
409 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream
*substream
,
410 struct snd_pcm_hw_params
*hw_params
)
414 /* alloc transport buffer */
415 size
= params_buffer_bytes(hw_params
);
416 snd_pcm_lib_malloc_pages(substream
, size
);
420 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream
*substream
,
421 unsigned int delay_ms
)
426 rate
= substream
->runtime
->rate
;
427 ret
= snd_pcm_format_size(substream
->runtime
->format
,
428 rate
* delay_ms
/ 1000)
429 * substream
->runtime
->channels
;
431 pr_debug(KERN_ERR
"%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
435 snd_pcm_format_size(substream
->runtime
->format
, rate
),
436 rate
* delay_ms
/ 1000,
442 static int snd_ps3_pcm_prepare(struct snd_pcm_substream
*substream
)
444 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
445 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
446 unsigned long irqsave
;
448 if (!snd_ps3_set_avsetting(substream
)) {
449 /* some parameter changed */
450 write_reg(PS3_AUDIO_AX_IE
,
451 PS3_AUDIO_AX_IE_ASOBEIE(0) |
452 PS3_AUDIO_AX_IE_ASOBUIE(0));
454 * let SPDIF device re-lock with SPDIF signal,
455 * start with some silence
457 card
->silent
= snd_ps3_delay_to_bytes(substream
,
459 (PS3_AUDIO_FIFO_STAGE_SIZE
* 4); /* every 4 times */
462 /* restart ring buffer pointer */
463 spin_lock_irqsave(&card
->dma_lock
, irqsave
);
465 card
->dma_buffer_size
= runtime
->dma_bytes
;
467 card
->dma_last_transfer_vaddr
[SND_PS3_CH_L
] =
468 card
->dma_next_transfer_vaddr
[SND_PS3_CH_L
] =
469 card
->dma_start_vaddr
[SND_PS3_CH_L
] =
471 card
->dma_start_bus_addr
[SND_PS3_CH_L
] = runtime
->dma_addr
;
473 card
->dma_last_transfer_vaddr
[SND_PS3_CH_R
] =
474 card
->dma_next_transfer_vaddr
[SND_PS3_CH_R
] =
475 card
->dma_start_vaddr
[SND_PS3_CH_R
] =
476 runtime
->dma_area
+ (runtime
->dma_bytes
/ 2);
477 card
->dma_start_bus_addr
[SND_PS3_CH_R
] =
478 runtime
->dma_addr
+ (runtime
->dma_bytes
/ 2);
480 pr_debug("%s: vaddr=%p bus=%#lx\n", __func__
,
481 card
->dma_start_vaddr
[SND_PS3_CH_L
],
482 card
->dma_start_bus_addr
[SND_PS3_CH_L
]);
485 spin_unlock_irqrestore(&card
->dma_lock
, irqsave
);
487 /* ensure the hardware sees the change */
493 static int snd_ps3_pcm_trigger(struct snd_pcm_substream
*substream
,
496 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
500 case SNDRV_PCM_TRIGGER_START
:
501 /* clear outstanding interrupts */
502 update_reg(PS3_AUDIO_AX_IS
, 0);
504 spin_lock(&card
->dma_lock
);
508 spin_unlock(&card
->dma_lock
);
510 snd_ps3_program_dma(card
,
511 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
512 snd_ps3_kick_dma(card
);
513 while (read_reg(PS3_AUDIO_KICK(7)) &
514 PS3_AUDIO_KICK_STATUS_MASK
) {
517 snd_ps3_program_dma(card
, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING
);
518 snd_ps3_kick_dma(card
);
521 case SNDRV_PCM_TRIGGER_STOP
:
522 spin_lock(&card
->dma_lock
);
526 spin_unlock(&card
->dma_lock
);
527 snd_ps3_wait_for_dma_stop(card
);
538 * report current pointer
540 static snd_pcm_uframes_t
snd_ps3_pcm_pointer(
541 struct snd_pcm_substream
*substream
)
543 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
545 snd_pcm_uframes_t ret
;
547 spin_lock(&card
->dma_lock
);
549 bytes
= (size_t)(card
->dma_last_transfer_vaddr
[SND_PS3_CH_L
] -
550 card
->dma_start_vaddr
[SND_PS3_CH_L
]);
552 spin_unlock(&card
->dma_lock
);
554 ret
= bytes_to_frames(substream
->runtime
, bytes
* 2);
559 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream
*substream
)
562 ret
= snd_pcm_lib_free_pages(substream
);
566 static int snd_ps3_pcm_close(struct snd_pcm_substream
*substream
)
573 static void snd_ps3_audio_fixup(struct snd_ps3_card_info
*card
)
576 * avsetting driver seems to never change the followings
577 * so, init them here once
580 /* no dma interrupt needed */
581 write_reg(PS3_AUDIO_INTR_EN_0
, 0);
583 /* use every 4 buffer empty interrupt */
584 update_mask_reg(PS3_AUDIO_AX_IC
,
585 PS3_AUDIO_AX_IC_AASOIMD_MASK
,
586 PS3_AUDIO_AX_IC_AASOIMD_EVERY4
);
588 /* enable 3wire clocks */
589 update_mask_reg(PS3_AUDIO_AO_3WMCTRL
,
590 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED
|
591 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED
),
593 update_reg(PS3_AUDIO_AO_3WMCTRL
,
594 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT
);
599 * NOTE: calling this function may generate audio interrupt.
601 static int snd_ps3_change_avsetting(struct snd_ps3_card_info
*card
)
604 pr_debug("%s: start\n", __func__
);
606 ret
= ps3av_set_audio_mode(card
->avs
.avs_audio_ch
,
607 card
->avs
.avs_audio_rate
,
608 card
->avs
.avs_audio_width
,
609 card
->avs
.avs_audio_format
,
610 card
->avs
.avs_audio_source
);
612 * Reset the following unwanted settings:
615 /* disable all 3wire buffers */
616 update_mask_reg(PS3_AUDIO_AO_3WMCTRL
,
617 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
618 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
619 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
620 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
622 wmb(); /* ensure the hardware sees the change */
623 /* wait for actually stopped */
625 while ((read_reg(PS3_AUDIO_AO_3WMCTRL
) &
626 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
627 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
628 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
629 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
634 /* reset buffer pointer */
635 for (i
= 0; i
< 4; i
++) {
636 update_reg(PS3_AUDIO_AO_3WCTRL(i
),
637 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET
);
640 wmb(); /* ensure the hardware actually start resetting */
642 /* enable 3wire#0 buffer */
643 update_reg(PS3_AUDIO_AO_3WMCTRL
, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
646 /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
647 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
648 ~PS3_AUDIO_AO_3WCTRL_ASODF
,
649 PS3_AUDIO_AO_3WCTRL_ASODF_LSB
);
650 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
651 ~PS3_AUDIO_AO_SPDCTRL_SPODF
,
652 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB
);
653 /* ensure all the setting above is written back to register */
655 /* avsetting driver altered AX_IE, caller must reset it if you want */
656 pr_debug("%s: end\n", __func__
);
660 static int snd_ps3_init_avsetting(struct snd_ps3_card_info
*card
)
663 pr_debug("%s: start\n", __func__
);
664 card
->avs
.avs_audio_ch
= PS3AV_CMD_AUDIO_NUM_OF_CH_2
;
665 card
->avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_48K
;
666 card
->avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_16
;
667 card
->avs
.avs_audio_format
= PS3AV_CMD_AUDIO_FORMAT_PCM
;
668 card
->avs
.avs_audio_source
= PS3AV_CMD_AUDIO_SOURCE_SERIAL
;
670 ret
= snd_ps3_change_avsetting(card
);
672 snd_ps3_audio_fixup(card
);
674 /* to start to generate SPDIF signal, fill data */
675 snd_ps3_program_dma(card
, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL
);
676 snd_ps3_kick_dma(card
);
677 pr_debug("%s: end\n", __func__
);
682 * set sampling rate according to the substream
684 static int snd_ps3_set_avsetting(struct snd_pcm_substream
*substream
)
686 struct snd_ps3_card_info
*card
= snd_pcm_substream_chip(substream
);
687 struct snd_ps3_avsetting_info avs
;
691 pr_debug("%s: called freq=%d width=%d\n", __func__
,
692 substream
->runtime
->rate
,
693 snd_pcm_format_width(substream
->runtime
->format
));
695 pr_debug("%s: before freq=%d width=%d\n", __func__
,
696 card
->avs
.avs_audio_rate
, card
->avs
.avs_audio_width
);
699 switch (substream
->runtime
->rate
) {
701 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_44K
;
704 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_48K
;
707 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_88K
;
710 avs
.avs_audio_rate
= PS3AV_CMD_AUDIO_FS_96K
;
713 pr_info("%s: invalid rate %d\n", __func__
,
714 substream
->runtime
->rate
);
719 switch (snd_pcm_format_width(substream
->runtime
->format
)) {
721 avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_16
;
724 avs
.avs_audio_width
= PS3AV_CMD_AUDIO_WORD_BITS_24
;
727 pr_info("%s: invalid width %d\n", __func__
,
728 snd_pcm_format_width(substream
->runtime
->format
));
732 if ((card
->avs
.avs_audio_width
!= avs
.avs_audio_width
) ||
733 (card
->avs
.avs_audio_rate
!= avs
.avs_audio_rate
)) {
735 snd_ps3_change_avsetting(card
);
737 pr_debug("%s: after freq=%d width=%d\n", __func__
,
738 card
->avs
.avs_audio_rate
, card
->avs
.avs_audio_width
);
747 static int snd_ps3_map_mmio(void)
749 the_card
.mapped_mmio_vaddr
=
750 ioremap(the_card
.ps3_dev
->m_region
->bus_addr
,
751 the_card
.ps3_dev
->m_region
->len
);
753 if (!the_card
.mapped_mmio_vaddr
) {
754 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
755 __func__
, the_card
.ps3_dev
->m_region
->lpar_addr
,
756 the_card
.ps3_dev
->m_region
->len
);
763 static void snd_ps3_unmap_mmio(void)
765 iounmap(the_card
.mapped_mmio_vaddr
);
766 the_card
.mapped_mmio_vaddr
= NULL
;
769 static int snd_ps3_allocate_irq(void)
772 u64 lpar_addr
, lpar_size
;
775 /* FIXME: move this to device_init (H/W probe) */
778 ret
= lv1_gpu_device_map(1, &lpar_addr
, &lpar_size
);
780 pr_info("%s: device map 1 failed %d\n", __func__
,
785 mapped
= ioremap(lpar_addr
, lpar_size
);
787 pr_info("%s: ioremap 1 failed \n", __func__
);
791 the_card
.audio_irq_outlet
= in_be64(mapped
);
794 ret
= lv1_gpu_device_unmap(1);
796 pr_info("%s: unmap 1 failed\n", __func__
);
799 ret
= ps3_irq_plug_setup(PS3_BINDING_CPU_ANY
,
800 the_card
.audio_irq_outlet
,
803 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__
, ret
);
807 ret
= request_irq(the_card
.irq_no
, snd_ps3_interrupt
, IRQF_DISABLED
,
808 SND_PS3_DRIVER_NAME
, &the_card
);
810 pr_info("%s: request_irq failed (%d)\n", __func__
, ret
);
817 ps3_irq_plug_destroy(the_card
.irq_no
);
821 static void snd_ps3_free_irq(void)
823 free_irq(the_card
.irq_no
, &the_card
);
824 ps3_irq_plug_destroy(the_card
.irq_no
);
827 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start
)
832 val
= (ioaddr_start
& (0x0fUL
<< 32)) >> (32 - 20) |
837 ret
= lv1_gpu_attribute(0x100, 0x007, val
, 0, 0);
839 pr_info("%s: gpu_attribute failed %d\n", __func__
,
843 static int __init
snd_ps3_driver_probe(struct ps3_system_bus_device
*dev
)
846 u64 lpar_addr
, lpar_size
;
848 BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1
));
849 BUG_ON(dev
->match_id
!= PS3_MATCH_ID_SOUND
);
851 the_card
.ps3_dev
= dev
;
853 ret
= ps3_open_hv_device(dev
);
859 ret
= lv1_gpu_device_map(2, &lpar_addr
, &lpar_size
);
861 pr_info("%s: device map 2 failed %d\n", __func__
, ret
);
864 ps3_mmio_region_init(dev
, dev
->m_region
, lpar_addr
, lpar_size
,
867 ret
= snd_ps3_map_mmio();
872 ps3_dma_region_init(dev
, dev
->d_region
,
873 PAGE_SHIFT
, /* use system page size */
874 0, /* dma type; not used */
876 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE
, PAGE_SIZE
));
877 dev
->d_region
->ioid
= PS3_AUDIO_IOID
;
879 ret
= ps3_dma_region_create(dev
->d_region
);
881 pr_info("%s: region_create\n", __func__
);
885 snd_ps3_audio_set_base_addr(dev
->d_region
->bus_addr
);
887 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
888 the_card
.start_delay
= snd_ps3_start_delay
;
891 if (snd_ps3_allocate_irq()) {
893 goto clean_dma_region
;
896 /* create card instance */
897 the_card
.card
= snd_card_new(index
, id
, THIS_MODULE
, 0);
898 if (!the_card
.card
) {
903 strcpy(the_card
.card
->driver
, "PS3");
904 strcpy(the_card
.card
->shortname
, "PS3");
905 strcpy(the_card
.card
->longname
, "PS3 sound");
906 /* create PCM devices instance */
907 /* NOTE:this driver works assuming pcm:substream = 1:1 */
908 ret
= snd_pcm_new(the_card
.card
,
910 0, /* instance index, will be stored pcm.device*/
911 1, /* output substream */
912 0, /* input substream */
917 the_card
.pcm
->private_data
= &the_card
;
918 strcpy(the_card
.pcm
->name
, "SPDIF");
921 snd_pcm_set_ops(the_card
.pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
922 &snd_ps3_pcm_spdif_ops
);
924 the_card
.pcm
->info_flags
= SNDRV_PCM_INFO_NONINTERLEAVED
;
925 /* pre-alloc PCM DMA buffer*/
926 ret
= snd_pcm_lib_preallocate_pages_for_all(the_card
.pcm
,
929 SND_PS3_PCM_PREALLOC_SIZE
,
930 SND_PS3_PCM_PREALLOC_SIZE
);
932 pr_info("%s: prealloc failed\n", __func__
);
937 * allocate null buffer
938 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
941 if (!(the_card
.null_buffer_start_vaddr
=
942 dma_alloc_coherent(&the_card
.ps3_dev
->core
,
944 &the_card
.null_buffer_start_dma_addr
,
946 pr_info("%s: nullbuffer alloc failed\n", __func__
);
947 goto clean_preallocate
;
949 pr_debug("%s: null vaddr=%p dma=%#lx\n", __func__
,
950 the_card
.null_buffer_start_vaddr
,
951 the_card
.null_buffer_start_dma_addr
);
952 /* set default sample rate/word width */
953 snd_ps3_init_avsetting(&the_card
);
955 /* register the card */
956 snd_card_set_dev(the_card
.card
, &dev
->core
);
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
);