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>
27 #include <linux/module.h>
29 #include <sound/asound.h>
30 #include <sound/control.h>
31 #include <sound/core.h>
32 #include <sound/initval.h>
33 #include <sound/memalloc.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
38 #include <asm/firmware.h>
39 #include <asm/lv1call.h>
41 #include <asm/ps3av.h>
44 #include "snd_ps3_reg.h"
50 static struct snd_ps3_card_info the_card;
52 static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY;
54 module_param_named(start_delay, snd_ps3_start_delay, uint, 0644);
55 MODULE_PARM_DESC(start_delay, "time to insert silent data in ms");
57 static int index = SNDRV_DEFAULT_IDX1;
58 static char *id = SNDRV_DEFAULT_STR1;
60 module_param(index, int, 0444);
61 MODULE_PARM_DESC(index, "Index value for PS3 soundchip.");
62 module_param(id, charp, 0444);
63 MODULE_PARM_DESC(id, "ID string for PS3 soundchip.");
67 * PS3 audio register access
69 static inline u32 read_reg(unsigned int reg)
71 return in_be32(the_card.mapped_mmio_vaddr + reg);
73 static inline void write_reg(unsigned int reg, u32 val)
75 out_be32(the_card.mapped_mmio_vaddr + reg, val);
77 static inline void update_reg(unsigned int reg, u32 or_val)
79 u32 newval = read_reg(reg) | or_val;
80 write_reg(reg, newval);
82 static inline void update_mask_reg(unsigned int reg, u32 mask, u32 or_val)
84 u32 newval = (read_reg(reg) & mask) | or_val;
85 write_reg(reg, newval);
91 static const struct snd_pcm_hardware snd_ps3_pcm_hw = {
92 .info = (SNDRV_PCM_INFO_MMAP |
93 SNDRV_PCM_INFO_NONINTERLEAVED |
94 SNDRV_PCM_INFO_MMAP_VALID),
95 .formats = (SNDRV_PCM_FMTBIT_S16_BE |
96 SNDRV_PCM_FMTBIT_S24_BE),
97 .rates = (SNDRV_PCM_RATE_44100 |
98 SNDRV_PCM_RATE_48000 |
99 SNDRV_PCM_RATE_88200 |
100 SNDRV_PCM_RATE_96000),
104 .channels_min = 2, /* stereo only */
107 .buffer_bytes_max = PS3_AUDIO_FIFO_SIZE * 64,
109 /* interrupt by four stages */
110 .period_bytes_min = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
111 .period_bytes_max = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
114 .periods_max = 32, /* buffer_size_max/ period_bytes_max */
116 .fifo_size = PS3_AUDIO_FIFO_SIZE
119 static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info *card,
120 int count, int force_stop)
122 int dma_ch, done, retries, stop_forced = 0;
125 for (dma_ch = 0; dma_ch < 8; dma_ch++) {
128 status = read_reg(PS3_AUDIO_KICK(dma_ch)) &
129 PS3_AUDIO_KICK_STATUS_MASK;
131 case PS3_AUDIO_KICK_STATUS_DONE:
132 case PS3_AUDIO_KICK_STATUS_NOTIFY:
133 case PS3_AUDIO_KICK_STATUS_CLEAR:
134 case PS3_AUDIO_KICK_STATUS_ERROR:
141 } while (!done && --retries);
142 if (!retries && force_stop) {
143 pr_info("%s: DMA ch %d is not stopped.",
145 /* last resort. force to stop dma.
146 * NOTE: this cause DMA done interrupts
148 update_reg(PS3_AUDIO_CONFIG, PS3_AUDIO_CONFIG_CLEAR);
156 * wait for all dma is done.
157 * NOTE: caller should reset card->running before call.
158 * If not, the interrupt handler will re-start DMA,
159 * then DMA is never stopped.
161 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card)
165 * wait for the last dma is done
169 * expected maximum DMA done time is 5.7ms + something (DMA itself).
170 * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
171 * DMA kick event would occur.
173 stop_forced = snd_ps3_verify_dma_stop(card, 700, 1);
176 * clear outstanding interrupts.
178 update_reg(PS3_AUDIO_INTR_0, 0);
179 update_reg(PS3_AUDIO_AX_IS, 0);
182 *revert CLEAR bit since it will not reset automatically after DMA stop
185 update_mask_reg(PS3_AUDIO_CONFIG, ~PS3_AUDIO_CONFIG_CLEAR, 0);
186 /* ensure the hardware sees changes */
190 static void snd_ps3_kick_dma(struct snd_ps3_card_info *card)
193 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST);
194 /* ensure the hardware sees the change */
199 * convert virtual addr to ioif bus addr.
201 static dma_addr_t v_to_bus(struct snd_ps3_card_info *card, void *paddr, int ch)
203 return card->dma_start_bus_addr[ch] +
204 (paddr - card->dma_start_vaddr[ch]);
209 * increment ring buffer pointer.
210 * NOTE: caller must hold write spinlock
212 static void snd_ps3_bump_buffer(struct snd_ps3_card_info *card,
213 enum snd_ps3_ch ch, size_t byte_count,
217 card->dma_last_transfer_vaddr[ch] =
218 card->dma_next_transfer_vaddr[ch];
219 card->dma_next_transfer_vaddr[ch] += byte_count;
220 if ((card->dma_start_vaddr[ch] + (card->dma_buffer_size / 2)) <=
221 card->dma_next_transfer_vaddr[ch]) {
222 card->dma_next_transfer_vaddr[ch] = card->dma_start_vaddr[ch];
226 * setup dmac to send data to audio and attenuate samples on the ring buffer
228 static int snd_ps3_program_dma(struct snd_ps3_card_info *card,
229 enum snd_ps3_dma_filltype filltype)
231 /* this dmac does not support over 4G */
233 int fill_stages, dma_ch, stage;
235 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 for (stage = 0; stage < fill_stages; stage++) {
260 dma_ch = stage * 2 + ch;
262 dma_addr = card->null_buffer_start_dma_addr;
266 card->dma_next_transfer_vaddr[ch],
269 write_reg(PS3_AUDIO_SOURCE(dma_ch),
270 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY |
273 /* dst: fixed to 3wire#0 */
275 write_reg(PS3_AUDIO_DEST(dma_ch),
276 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
277 PS3_AUDIO_AO_3W_LDATA(0)));
279 write_reg(PS3_AUDIO_DEST(dma_ch),
280 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
281 PS3_AUDIO_AO_3W_RDATA(0)));
283 /* count always 1 DMA block (1/2 stage = 128 bytes) */
284 write_reg(PS3_AUDIO_DMASIZE(dma_ch), 0);
285 /* bump pointer if needed */
287 snd_ps3_bump_buffer(card, ch,
288 PS3_AUDIO_DMAC_BLOCK_SIZE,
293 write_reg(PS3_AUDIO_KICK(dma_ch),
296 write_reg(PS3_AUDIO_KICK(dma_ch),
297 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
299 PS3_AUDIO_KICK_REQUEST);
302 /* ensure the hardware sees the change */
304 spin_unlock_irqrestore(&card->dma_lock, irqsave);
312 static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id)
316 int underflow_occured = 0;
317 struct snd_ps3_card_info *card = dev_id;
319 if (!card->running) {
320 update_reg(PS3_AUDIO_AX_IS, 0);
321 update_reg(PS3_AUDIO_INTR_0, 0);
325 port_intr = read_reg(PS3_AUDIO_AX_IS);
327 *serial buffer empty detected (every 4 times),
328 *program next dma and kick it
330 if (port_intr & PS3_AUDIO_AX_IE_ASOBEIE(0)) {
331 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBEIE(0));
332 if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
333 write_reg(PS3_AUDIO_AX_IS, port_intr);
334 underflow_occured = 1;
337 /* we are still in silent time */
338 snd_ps3_program_dma(card,
339 (underflow_occured) ?
340 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL :
341 SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
342 snd_ps3_kick_dma(card);
345 snd_ps3_program_dma(card,
346 (underflow_occured) ?
347 SND_PS3_DMA_FILLTYPE_FIRSTFILL :
348 SND_PS3_DMA_FILLTYPE_RUNNING);
349 snd_ps3_kick_dma(card);
350 snd_pcm_period_elapsed(card->substream);
352 } else if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
353 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBUIE(0));
355 * serial out underflow, but buffer empty not detected.
356 * in this case, fill fifo with 0 to recover. After
357 * filling dummy data, serial automatically start to
358 * consume them and then will generate normal buffer
360 * If both buffer underflow and buffer empty are occurred,
361 * it is better to do nomal data transfer than empty one
363 snd_ps3_program_dma(card,
364 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
365 snd_ps3_kick_dma(card);
366 snd_ps3_program_dma(card,
367 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
368 snd_ps3_kick_dma(card);
370 /* clear interrupt cause */
376 * mute_on : 0 output enabled
379 static int snd_ps3_mute(int mute_on)
381 return ps3av_audio_mute(mute_on);
386 * NOTE: calling this function may generate audio interrupt.
388 static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card)
391 pr_debug("%s: start\n", __func__);
393 ret = ps3av_set_audio_mode(card->avs.avs_audio_ch,
394 card->avs.avs_audio_rate,
395 card->avs.avs_audio_width,
396 card->avs.avs_audio_format,
397 card->avs.avs_audio_source);
399 * Reset the following unwanted settings:
402 /* disable all 3wire buffers */
403 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
404 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
405 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
406 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
407 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
409 wmb(); /* ensure the hardware sees the change */
410 /* wait for actually stopped */
412 while ((read_reg(PS3_AUDIO_AO_3WMCTRL) &
413 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
414 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
415 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
416 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
421 /* reset buffer pointer */
422 for (i = 0; i < 4; i++) {
423 update_reg(PS3_AUDIO_AO_3WCTRL(i),
424 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET);
427 wmb(); /* ensure the hardware actually start resetting */
429 /* enable 3wire#0 buffer */
430 update_reg(PS3_AUDIO_AO_3WMCTRL, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
433 /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
434 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
435 ~PS3_AUDIO_AO_3WCTRL_ASODF,
436 PS3_AUDIO_AO_3WCTRL_ASODF_LSB);
437 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
438 ~PS3_AUDIO_AO_SPDCTRL_SPODF,
439 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB);
440 /* ensure all the setting above is written back to register */
442 /* avsetting driver altered AX_IE, caller must reset it if you want */
443 pr_debug("%s: end\n", __func__);
448 * set sampling rate according to the substream
450 static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream)
452 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
453 struct snd_ps3_avsetting_info avs;
458 pr_debug("%s: called freq=%d width=%d\n", __func__,
459 substream->runtime->rate,
460 snd_pcm_format_width(substream->runtime->format));
462 pr_debug("%s: before freq=%d width=%d\n", __func__,
463 card->avs.avs_audio_rate, card->avs.avs_audio_width);
466 switch (substream->runtime->rate) {
468 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_44K;
471 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
474 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_88K;
477 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_96K;
480 pr_info("%s: invalid rate %d\n", __func__,
481 substream->runtime->rate);
486 switch (snd_pcm_format_width(substream->runtime->format)) {
488 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
491 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_24;
494 pr_info("%s: invalid width %d\n", __func__,
495 snd_pcm_format_width(substream->runtime->format));
499 memcpy(avs.avs_cs_info, ps3av_mode_cs_info, 8);
501 if (memcmp(&card->avs, &avs, sizeof(avs))) {
502 pr_debug("%s: after freq=%d width=%d\n", __func__,
503 card->avs.avs_audio_rate, card->avs.avs_audio_width);
506 snd_ps3_change_avsetting(card);
511 /* check CS non-audio bit and mute accordingly */
512 if (avs.avs_cs_info[0] & 0x02)
513 ps3av_audio_mute_analog(1); /* mute if non-audio */
515 ps3av_audio_mute_analog(0);
523 static int snd_ps3_pcm_open(struct snd_pcm_substream *substream)
525 struct snd_pcm_runtime *runtime = substream->runtime;
526 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
528 /* to retrieve substream/runtime in interrupt handler */
529 card->substream = substream;
531 runtime->hw = snd_ps3_pcm_hw;
533 card->start_delay = snd_ps3_start_delay;
536 snd_ps3_mute(0); /* this function sleep */
538 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
539 PS3_AUDIO_FIFO_STAGE_SIZE * 4 * 2);
543 static int snd_ps3_pcm_close(struct snd_pcm_substream *substream)
550 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
551 struct snd_pcm_hw_params *hw_params)
555 /* alloc transport buffer */
556 size = params_buffer_bytes(hw_params);
557 snd_pcm_lib_malloc_pages(substream, size);
561 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream)
563 return snd_pcm_lib_free_pages(substream);
566 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
567 unsigned int delay_ms)
572 rate = substream->runtime->rate;
573 ret = snd_pcm_format_size(substream->runtime->format,
574 rate * delay_ms / 1000)
575 * substream->runtime->channels;
577 pr_debug("%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
581 snd_pcm_format_size(substream->runtime->format, rate),
582 rate * delay_ms / 1000,
588 static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
590 struct snd_pcm_runtime *runtime = substream->runtime;
591 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
592 unsigned long irqsave;
594 if (!snd_ps3_set_avsetting(substream)) {
595 /* some parameter changed */
596 write_reg(PS3_AUDIO_AX_IE,
597 PS3_AUDIO_AX_IE_ASOBEIE(0) |
598 PS3_AUDIO_AX_IE_ASOBUIE(0));
600 * let SPDIF device re-lock with SPDIF signal,
601 * start with some silence
603 card->silent = snd_ps3_delay_to_bytes(substream,
605 (PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */
608 /* restart ring buffer pointer */
609 spin_lock_irqsave(&card->dma_lock, irqsave);
611 card->dma_buffer_size = runtime->dma_bytes;
613 card->dma_last_transfer_vaddr[SND_PS3_CH_L] =
614 card->dma_next_transfer_vaddr[SND_PS3_CH_L] =
615 card->dma_start_vaddr[SND_PS3_CH_L] =
617 card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr;
619 card->dma_last_transfer_vaddr[SND_PS3_CH_R] =
620 card->dma_next_transfer_vaddr[SND_PS3_CH_R] =
621 card->dma_start_vaddr[SND_PS3_CH_R] =
622 runtime->dma_area + (runtime->dma_bytes / 2);
623 card->dma_start_bus_addr[SND_PS3_CH_R] =
624 runtime->dma_addr + (runtime->dma_bytes / 2);
626 pr_debug("%s: vaddr=%p bus=%#llx\n", __func__,
627 card->dma_start_vaddr[SND_PS3_CH_L],
628 card->dma_start_bus_addr[SND_PS3_CH_L]);
631 spin_unlock_irqrestore(&card->dma_lock, irqsave);
633 /* ensure the hardware sees the change */
639 static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
642 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
646 case SNDRV_PCM_TRIGGER_START:
647 /* clear outstanding interrupts */
648 update_reg(PS3_AUDIO_AX_IS, 0);
650 spin_lock(&card->dma_lock);
654 spin_unlock(&card->dma_lock);
656 snd_ps3_program_dma(card,
657 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
658 snd_ps3_kick_dma(card);
659 while (read_reg(PS3_AUDIO_KICK(7)) &
660 PS3_AUDIO_KICK_STATUS_MASK) {
663 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
664 snd_ps3_kick_dma(card);
667 case SNDRV_PCM_TRIGGER_STOP:
668 spin_lock(&card->dma_lock);
672 spin_unlock(&card->dma_lock);
673 snd_ps3_wait_for_dma_stop(card);
684 * report current pointer
686 static snd_pcm_uframes_t snd_ps3_pcm_pointer(
687 struct snd_pcm_substream *substream)
689 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
691 snd_pcm_uframes_t ret;
693 spin_lock(&card->dma_lock);
695 bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
696 card->dma_start_vaddr[SND_PS3_CH_L]);
698 spin_unlock(&card->dma_lock);
700 ret = bytes_to_frames(substream->runtime, bytes * 2);
706 * SPDIF status bits controls
708 static int snd_ps3_spdif_mask_info(struct snd_kcontrol *kcontrol,
709 struct snd_ctl_elem_info *uinfo)
711 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
716 /* FIXME: ps3av_set_audio_mode() assumes only consumer mode */
717 static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol,
718 struct snd_ctl_elem_value *ucontrol)
720 memset(ucontrol->value.iec958.status, 0xff, 8);
724 static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol,
725 struct snd_ctl_elem_value *ucontrol)
730 static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol,
731 struct snd_ctl_elem_value *ucontrol)
733 memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8);
737 static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol,
738 struct snd_ctl_elem_value *ucontrol)
740 if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) {
741 memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8);
747 static struct snd_kcontrol_new spdif_ctls[] = {
749 .access = SNDRV_CTL_ELEM_ACCESS_READ,
750 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
751 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
752 .info = snd_ps3_spdif_mask_info,
753 .get = snd_ps3_spdif_cmask_get,
756 .access = SNDRV_CTL_ELEM_ACCESS_READ,
757 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
758 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
759 .info = snd_ps3_spdif_mask_info,
760 .get = snd_ps3_spdif_pmask_get,
763 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
764 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
765 .info = snd_ps3_spdif_mask_info,
766 .get = snd_ps3_spdif_default_get,
767 .put = snd_ps3_spdif_default_put,
771 static const struct snd_pcm_ops snd_ps3_pcm_spdif_ops = {
772 .open = snd_ps3_pcm_open,
773 .close = snd_ps3_pcm_close,
774 .ioctl = snd_pcm_lib_ioctl,
775 .hw_params = snd_ps3_pcm_hw_params,
776 .hw_free = snd_ps3_pcm_hw_free,
777 .prepare = snd_ps3_pcm_prepare,
778 .trigger = snd_ps3_pcm_trigger,
779 .pointer = snd_ps3_pcm_pointer,
783 static int snd_ps3_map_mmio(void)
785 the_card.mapped_mmio_vaddr =
786 ioremap(the_card.ps3_dev->m_region->bus_addr,
787 the_card.ps3_dev->m_region->len);
789 if (!the_card.mapped_mmio_vaddr) {
790 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
791 __func__, the_card.ps3_dev->m_region->lpar_addr,
792 the_card.ps3_dev->m_region->len);
799 static void snd_ps3_unmap_mmio(void)
801 iounmap(the_card.mapped_mmio_vaddr);
802 the_card.mapped_mmio_vaddr = NULL;
805 static int snd_ps3_allocate_irq(void)
808 u64 lpar_addr, lpar_size;
811 /* FIXME: move this to device_init (H/W probe) */
814 ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size);
816 pr_info("%s: device map 1 failed %d\n", __func__,
821 mapped = ioremap(lpar_addr, lpar_size);
823 pr_info("%s: ioremap 1 failed \n", __func__);
827 the_card.audio_irq_outlet = in_be64(mapped);
830 ret = lv1_gpu_device_unmap(1);
832 pr_info("%s: unmap 1 failed\n", __func__);
835 ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY,
836 the_card.audio_irq_outlet,
839 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret);
843 ret = request_irq(the_card.irq_no, snd_ps3_interrupt, 0,
844 SND_PS3_DRIVER_NAME, &the_card);
846 pr_info("%s: request_irq failed (%d)\n", __func__, ret);
853 ps3_irq_plug_destroy(the_card.irq_no);
857 static void snd_ps3_free_irq(void)
859 free_irq(the_card.irq_no, &the_card);
860 ps3_irq_plug_destroy(the_card.irq_no);
863 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
868 val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
873 ret = lv1_gpu_attribute(0x100, 0x007, val);
875 pr_info("%s: gpu_attribute failed %d\n", __func__,
879 static void snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
882 * avsetting driver seems to never change the following
883 * so, init them here once
886 /* no dma interrupt needed */
887 write_reg(PS3_AUDIO_INTR_EN_0, 0);
889 /* use every 4 buffer empty interrupt */
890 update_mask_reg(PS3_AUDIO_AX_IC,
891 PS3_AUDIO_AX_IC_AASOIMD_MASK,
892 PS3_AUDIO_AX_IC_AASOIMD_EVERY4);
894 /* enable 3wire clocks */
895 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
896 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED |
897 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED),
899 update_reg(PS3_AUDIO_AO_3WMCTRL,
900 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
903 static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
906 pr_debug("%s: start\n", __func__);
907 card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
908 card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
909 card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
910 card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM;
911 card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
912 memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8);
914 ret = snd_ps3_change_avsetting(card);
916 snd_ps3_audio_fixup(card);
918 /* to start to generate SPDIF signal, fill data */
919 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
920 snd_ps3_kick_dma(card);
921 pr_debug("%s: end\n", __func__);
925 static int snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
928 u64 lpar_addr, lpar_size;
929 static u64 dummy_mask;
931 if (WARN_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1)))
933 if (WARN_ON(dev->match_id != PS3_MATCH_ID_SOUND))
936 the_card.ps3_dev = dev;
938 ret = ps3_open_hv_device(dev);
944 ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size);
946 pr_info("%s: device map 2 failed %d\n", __func__, ret);
949 ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
952 ret = snd_ps3_map_mmio();
957 ps3_dma_region_init(dev, dev->d_region,
958 PAGE_SHIFT, /* use system page size */
959 0, /* dma type; not used */
961 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE));
962 dev->d_region->ioid = PS3_AUDIO_IOID;
964 ret = ps3_dma_region_create(dev->d_region);
966 pr_info("%s: region_create\n", __func__);
970 dummy_mask = DMA_BIT_MASK(32);
971 dev->core.dma_mask = &dummy_mask;
972 dma_set_coherent_mask(&dev->core, dummy_mask);
974 snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
976 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
977 the_card.start_delay = snd_ps3_start_delay;
980 if (snd_ps3_allocate_irq()) {
982 goto clean_dma_region;
985 /* create card instance */
986 ret = snd_card_new(&dev->core, index, id, THIS_MODULE,
991 strcpy(the_card.card->driver, "PS3");
992 strcpy(the_card.card->shortname, "PS3");
993 strcpy(the_card.card->longname, "PS3 sound");
995 /* create control elements */
996 for (i = 0; i < ARRAY_SIZE(spdif_ctls); i++) {
997 ret = snd_ctl_add(the_card.card,
998 snd_ctl_new1(&spdif_ctls[i], &the_card));
1003 /* create PCM devices instance */
1004 /* NOTE:this driver works assuming pcm:substream = 1:1 */
1005 ret = snd_pcm_new(the_card.card,
1007 0, /* instance index, will be stored pcm.device*/
1008 1, /* output substream */
1009 0, /* input substream */
1014 the_card.pcm->private_data = &the_card;
1015 strcpy(the_card.pcm->name, "SPDIF");
1018 snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK,
1019 &snd_ps3_pcm_spdif_ops);
1021 the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED;
1022 /* pre-alloc PCM DMA buffer*/
1023 snd_pcm_lib_preallocate_pages_for_all(the_card.pcm,
1026 SND_PS3_PCM_PREALLOC_SIZE,
1027 SND_PS3_PCM_PREALLOC_SIZE);
1030 * allocate null buffer
1031 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
1032 * PAGE_SIZE is enogh
1034 the_card.null_buffer_start_vaddr =
1035 dma_alloc_coherent(&the_card.ps3_dev->core,
1037 &the_card.null_buffer_start_dma_addr,
1039 if (!the_card.null_buffer_start_vaddr) {
1040 pr_info("%s: nullbuffer alloc failed\n", __func__);
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 ret = snd_card_register(the_card.card);
1055 pr_info("%s started. start_delay=%dms\n",
1056 the_card.card->longname, the_card.start_delay);
1060 dma_free_coherent(&the_card.ps3_dev->core,
1062 the_card.null_buffer_start_vaddr,
1063 the_card.null_buffer_start_dma_addr);
1065 snd_card_free(the_card.card);
1069 ps3_dma_region_free(dev->d_region);
1071 snd_ps3_unmap_mmio();
1073 lv1_gpu_device_unmap(2);
1075 ps3_close_hv_device(dev);
1077 * there is no destructor function to pcm.
1078 * midlayer automatically releases if the card removed
1081 }; /* snd_ps3_probe */
1083 /* called when module removal */
1084 static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev)
1087 pr_info("%s:start id=%d\n", __func__, dev->match_id);
1088 if (dev->match_id != PS3_MATCH_ID_SOUND)
1092 * ctl and preallocate buffer will be freed in
1095 ret = snd_card_free(the_card.card);
1097 pr_info("%s: ctl freecard=%d\n", __func__, ret);
1099 dma_free_coherent(&dev->core,
1101 the_card.null_buffer_start_vaddr,
1102 the_card.null_buffer_start_dma_addr);
1104 ps3_dma_region_free(dev->d_region);
1107 snd_ps3_unmap_mmio();
1109 lv1_gpu_device_unmap(2);
1110 ps3_close_hv_device(dev);
1111 pr_info("%s:end id=%d\n", __func__, dev->match_id);
1113 } /* snd_ps3_remove */
1115 static struct ps3_system_bus_driver snd_ps3_bus_driver_info = {
1116 .match_id = PS3_MATCH_ID_SOUND,
1117 .probe = snd_ps3_driver_probe,
1118 .remove = snd_ps3_driver_remove,
1119 .shutdown = snd_ps3_driver_remove,
1121 .name = SND_PS3_DRIVER_NAME,
1122 .owner = THIS_MODULE,
1128 * module/subsystem initialize/terminate
1130 static int __init snd_ps3_init(void)
1134 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1137 memset(&the_card, 0, sizeof(the_card));
1138 spin_lock_init(&the_card.dma_lock);
1140 /* register systembus DRIVER, this calls our probe() func */
1141 ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info);
1145 module_init(snd_ps3_init);
1147 static void __exit snd_ps3_exit(void)
1149 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info);
1151 module_exit(snd_ps3_exit);
1153 MODULE_LICENSE("GPL v2");
1154 MODULE_DESCRIPTION("PS3 sound driver");
1155 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1156 MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND);