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 */
237 unsigned long irqsave;
241 case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL:
243 /* intentionally fall thru */
244 case SND_PS3_DMA_FILLTYPE_FIRSTFILL:
245 ch0_kick_event = PS3_AUDIO_KICK_EVENT_ALWAYS;
248 case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING:
250 /* intentionally fall thru */
251 case SND_PS3_DMA_FILLTYPE_RUNNING:
252 ch0_kick_event = PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY;
256 snd_ps3_verify_dma_stop(card, 700, 0);
258 spin_lock_irqsave(&card->dma_lock, irqsave);
259 for (ch = 0; ch < 2; ch++) {
260 start_vaddr = card->dma_next_transfer_vaddr[0];
261 for (stage = 0; stage < fill_stages; stage++) {
262 dma_ch = stage * 2 + ch;
264 dma_addr = card->null_buffer_start_dma_addr;
268 card->dma_next_transfer_vaddr[ch],
271 write_reg(PS3_AUDIO_SOURCE(dma_ch),
272 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY |
275 /* dst: fixed to 3wire#0 */
277 write_reg(PS3_AUDIO_DEST(dma_ch),
278 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
279 PS3_AUDIO_AO_3W_LDATA(0)));
281 write_reg(PS3_AUDIO_DEST(dma_ch),
282 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
283 PS3_AUDIO_AO_3W_RDATA(0)));
285 /* count always 1 DMA block (1/2 stage = 128 bytes) */
286 write_reg(PS3_AUDIO_DMASIZE(dma_ch), 0);
287 /* bump pointer if needed */
289 snd_ps3_bump_buffer(card, ch,
290 PS3_AUDIO_DMAC_BLOCK_SIZE,
295 write_reg(PS3_AUDIO_KICK(dma_ch),
298 write_reg(PS3_AUDIO_KICK(dma_ch),
299 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
301 PS3_AUDIO_KICK_REQUEST);
304 /* ensure the hardware sees the change */
306 spin_unlock_irqrestore(&card->dma_lock, irqsave);
314 static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id)
318 int underflow_occured = 0;
319 struct snd_ps3_card_info *card = dev_id;
321 if (!card->running) {
322 update_reg(PS3_AUDIO_AX_IS, 0);
323 update_reg(PS3_AUDIO_INTR_0, 0);
327 port_intr = read_reg(PS3_AUDIO_AX_IS);
329 *serial buffer empty detected (every 4 times),
330 *program next dma and kick it
332 if (port_intr & PS3_AUDIO_AX_IE_ASOBEIE(0)) {
333 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBEIE(0));
334 if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
335 write_reg(PS3_AUDIO_AX_IS, port_intr);
336 underflow_occured = 1;
339 /* we are still in silent time */
340 snd_ps3_program_dma(card,
341 (underflow_occured) ?
342 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL :
343 SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
344 snd_ps3_kick_dma(card);
347 snd_ps3_program_dma(card,
348 (underflow_occured) ?
349 SND_PS3_DMA_FILLTYPE_FIRSTFILL :
350 SND_PS3_DMA_FILLTYPE_RUNNING);
351 snd_ps3_kick_dma(card);
352 snd_pcm_period_elapsed(card->substream);
354 } else if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
355 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBUIE(0));
357 * serial out underflow, but buffer empty not detected.
358 * in this case, fill fifo with 0 to recover. After
359 * filling dummy data, serial automatically start to
360 * consume them and then will generate normal buffer
362 * If both buffer underflow and buffer empty are occurred,
363 * it is better to do nomal data transfer than empty one
365 snd_ps3_program_dma(card,
366 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
367 snd_ps3_kick_dma(card);
368 snd_ps3_program_dma(card,
369 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
370 snd_ps3_kick_dma(card);
372 /* clear interrupt cause */
378 * mute_on : 0 output enabled
381 static int snd_ps3_mute(int mute_on)
383 return ps3av_audio_mute(mute_on);
388 * NOTE: calling this function may generate audio interrupt.
390 static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card)
393 pr_debug("%s: start\n", __func__);
395 ret = ps3av_set_audio_mode(card->avs.avs_audio_ch,
396 card->avs.avs_audio_rate,
397 card->avs.avs_audio_width,
398 card->avs.avs_audio_format,
399 card->avs.avs_audio_source);
401 * Reset the following unwanted settings:
404 /* disable all 3wire buffers */
405 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
406 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
407 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
408 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
409 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
411 wmb(); /* ensure the hardware sees the change */
412 /* wait for actually stopped */
414 while ((read_reg(PS3_AUDIO_AO_3WMCTRL) &
415 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
416 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
417 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
418 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
423 /* reset buffer pointer */
424 for (i = 0; i < 4; i++) {
425 update_reg(PS3_AUDIO_AO_3WCTRL(i),
426 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET);
429 wmb(); /* ensure the hardware actually start resetting */
431 /* enable 3wire#0 buffer */
432 update_reg(PS3_AUDIO_AO_3WMCTRL, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
435 /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
436 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
437 ~PS3_AUDIO_AO_3WCTRL_ASODF,
438 PS3_AUDIO_AO_3WCTRL_ASODF_LSB);
439 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
440 ~PS3_AUDIO_AO_SPDCTRL_SPODF,
441 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB);
442 /* ensure all the setting above is written back to register */
444 /* avsetting driver altered AX_IE, caller must reset it if you want */
445 pr_debug("%s: end\n", __func__);
450 * set sampling rate according to the substream
452 static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream)
454 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
455 struct snd_ps3_avsetting_info avs;
460 pr_debug("%s: called freq=%d width=%d\n", __func__,
461 substream->runtime->rate,
462 snd_pcm_format_width(substream->runtime->format));
464 pr_debug("%s: before freq=%d width=%d\n", __func__,
465 card->avs.avs_audio_rate, card->avs.avs_audio_width);
468 switch (substream->runtime->rate) {
470 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_44K;
473 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
476 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_88K;
479 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_96K;
482 pr_info("%s: invalid rate %d\n", __func__,
483 substream->runtime->rate);
488 switch (snd_pcm_format_width(substream->runtime->format)) {
490 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
493 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_24;
496 pr_info("%s: invalid width %d\n", __func__,
497 snd_pcm_format_width(substream->runtime->format));
501 memcpy(avs.avs_cs_info, ps3av_mode_cs_info, 8);
503 if (memcmp(&card->avs, &avs, sizeof(avs))) {
504 pr_debug("%s: after freq=%d width=%d\n", __func__,
505 card->avs.avs_audio_rate, card->avs.avs_audio_width);
508 snd_ps3_change_avsetting(card);
513 /* check CS non-audio bit and mute accordingly */
514 if (avs.avs_cs_info[0] & 0x02)
515 ps3av_audio_mute_analog(1); /* mute if non-audio */
517 ps3av_audio_mute_analog(0);
525 static int snd_ps3_pcm_open(struct snd_pcm_substream *substream)
527 struct snd_pcm_runtime *runtime = substream->runtime;
528 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
531 pcm_index = substream->pcm->device;
532 /* to retrieve substream/runtime in interrupt handler */
533 card->substream = substream;
535 runtime->hw = snd_ps3_pcm_hw;
537 card->start_delay = snd_ps3_start_delay;
540 snd_ps3_mute(0); /* this function sleep */
542 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
543 PS3_AUDIO_FIFO_STAGE_SIZE * 4 * 2);
547 static int snd_ps3_pcm_close(struct snd_pcm_substream *substream)
554 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
555 struct snd_pcm_hw_params *hw_params)
559 /* alloc transport buffer */
560 size = params_buffer_bytes(hw_params);
561 snd_pcm_lib_malloc_pages(substream, size);
565 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream)
568 ret = snd_pcm_lib_free_pages(substream);
572 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
573 unsigned int delay_ms)
578 rate = substream->runtime->rate;
579 ret = snd_pcm_format_size(substream->runtime->format,
580 rate * delay_ms / 1000)
581 * substream->runtime->channels;
583 pr_debug("%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
587 snd_pcm_format_size(substream->runtime->format, rate),
588 rate * delay_ms / 1000,
594 static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
596 struct snd_pcm_runtime *runtime = substream->runtime;
597 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
598 unsigned long irqsave;
600 if (!snd_ps3_set_avsetting(substream)) {
601 /* some parameter changed */
602 write_reg(PS3_AUDIO_AX_IE,
603 PS3_AUDIO_AX_IE_ASOBEIE(0) |
604 PS3_AUDIO_AX_IE_ASOBUIE(0));
606 * let SPDIF device re-lock with SPDIF signal,
607 * start with some silence
609 card->silent = snd_ps3_delay_to_bytes(substream,
611 (PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */
614 /* restart ring buffer pointer */
615 spin_lock_irqsave(&card->dma_lock, irqsave);
617 card->dma_buffer_size = runtime->dma_bytes;
619 card->dma_last_transfer_vaddr[SND_PS3_CH_L] =
620 card->dma_next_transfer_vaddr[SND_PS3_CH_L] =
621 card->dma_start_vaddr[SND_PS3_CH_L] =
623 card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr;
625 card->dma_last_transfer_vaddr[SND_PS3_CH_R] =
626 card->dma_next_transfer_vaddr[SND_PS3_CH_R] =
627 card->dma_start_vaddr[SND_PS3_CH_R] =
628 runtime->dma_area + (runtime->dma_bytes / 2);
629 card->dma_start_bus_addr[SND_PS3_CH_R] =
630 runtime->dma_addr + (runtime->dma_bytes / 2);
632 pr_debug("%s: vaddr=%p bus=%#llx\n", __func__,
633 card->dma_start_vaddr[SND_PS3_CH_L],
634 card->dma_start_bus_addr[SND_PS3_CH_L]);
637 spin_unlock_irqrestore(&card->dma_lock, irqsave);
639 /* ensure the hardware sees the change */
645 static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
648 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
652 case SNDRV_PCM_TRIGGER_START:
653 /* clear outstanding interrupts */
654 update_reg(PS3_AUDIO_AX_IS, 0);
656 spin_lock(&card->dma_lock);
660 spin_unlock(&card->dma_lock);
662 snd_ps3_program_dma(card,
663 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
664 snd_ps3_kick_dma(card);
665 while (read_reg(PS3_AUDIO_KICK(7)) &
666 PS3_AUDIO_KICK_STATUS_MASK) {
669 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
670 snd_ps3_kick_dma(card);
673 case SNDRV_PCM_TRIGGER_STOP:
674 spin_lock(&card->dma_lock);
678 spin_unlock(&card->dma_lock);
679 snd_ps3_wait_for_dma_stop(card);
690 * report current pointer
692 static snd_pcm_uframes_t snd_ps3_pcm_pointer(
693 struct snd_pcm_substream *substream)
695 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
697 snd_pcm_uframes_t ret;
699 spin_lock(&card->dma_lock);
701 bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
702 card->dma_start_vaddr[SND_PS3_CH_L]);
704 spin_unlock(&card->dma_lock);
706 ret = bytes_to_frames(substream->runtime, bytes * 2);
712 * SPDIF status bits controls
714 static int snd_ps3_spdif_mask_info(struct snd_kcontrol *kcontrol,
715 struct snd_ctl_elem_info *uinfo)
717 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
722 /* FIXME: ps3av_set_audio_mode() assumes only consumer mode */
723 static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol,
724 struct snd_ctl_elem_value *ucontrol)
726 memset(ucontrol->value.iec958.status, 0xff, 8);
730 static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol,
731 struct snd_ctl_elem_value *ucontrol)
736 static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol,
737 struct snd_ctl_elem_value *ucontrol)
739 memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8);
743 static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol,
744 struct snd_ctl_elem_value *ucontrol)
746 if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) {
747 memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8);
753 static struct snd_kcontrol_new spdif_ctls[] = {
755 .access = SNDRV_CTL_ELEM_ACCESS_READ,
756 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
757 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
758 .info = snd_ps3_spdif_mask_info,
759 .get = snd_ps3_spdif_cmask_get,
762 .access = SNDRV_CTL_ELEM_ACCESS_READ,
763 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
764 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
765 .info = snd_ps3_spdif_mask_info,
766 .get = snd_ps3_spdif_pmask_get,
769 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
770 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
771 .info = snd_ps3_spdif_mask_info,
772 .get = snd_ps3_spdif_default_get,
773 .put = snd_ps3_spdif_default_put,
777 static struct snd_pcm_ops snd_ps3_pcm_spdif_ops = {
778 .open = snd_ps3_pcm_open,
779 .close = snd_ps3_pcm_close,
780 .ioctl = snd_pcm_lib_ioctl,
781 .hw_params = snd_ps3_pcm_hw_params,
782 .hw_free = snd_ps3_pcm_hw_free,
783 .prepare = snd_ps3_pcm_prepare,
784 .trigger = snd_ps3_pcm_trigger,
785 .pointer = snd_ps3_pcm_pointer,
789 static int snd_ps3_map_mmio(void)
791 the_card.mapped_mmio_vaddr =
792 ioremap(the_card.ps3_dev->m_region->bus_addr,
793 the_card.ps3_dev->m_region->len);
795 if (!the_card.mapped_mmio_vaddr) {
796 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
797 __func__, the_card.ps3_dev->m_region->lpar_addr,
798 the_card.ps3_dev->m_region->len);
805 static void snd_ps3_unmap_mmio(void)
807 iounmap(the_card.mapped_mmio_vaddr);
808 the_card.mapped_mmio_vaddr = NULL;
811 static int snd_ps3_allocate_irq(void)
814 u64 lpar_addr, lpar_size;
817 /* FIXME: move this to device_init (H/W probe) */
820 ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size);
822 pr_info("%s: device map 1 failed %d\n", __func__,
827 mapped = ioremap(lpar_addr, lpar_size);
829 pr_info("%s: ioremap 1 failed \n", __func__);
833 the_card.audio_irq_outlet = in_be64(mapped);
836 ret = lv1_gpu_device_unmap(1);
838 pr_info("%s: unmap 1 failed\n", __func__);
841 ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY,
842 the_card.audio_irq_outlet,
845 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret);
849 ret = request_irq(the_card.irq_no, snd_ps3_interrupt, 0,
850 SND_PS3_DRIVER_NAME, &the_card);
852 pr_info("%s: request_irq failed (%d)\n", __func__, ret);
859 ps3_irq_plug_destroy(the_card.irq_no);
863 static void snd_ps3_free_irq(void)
865 free_irq(the_card.irq_no, &the_card);
866 ps3_irq_plug_destroy(the_card.irq_no);
869 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
874 val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
879 ret = lv1_gpu_attribute(0x100, 0x007, val);
881 pr_info("%s: gpu_attribute failed %d\n", __func__,
885 static void snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
888 * avsetting driver seems to never change the followings
889 * so, init them here once
892 /* no dma interrupt needed */
893 write_reg(PS3_AUDIO_INTR_EN_0, 0);
895 /* use every 4 buffer empty interrupt */
896 update_mask_reg(PS3_AUDIO_AX_IC,
897 PS3_AUDIO_AX_IC_AASOIMD_MASK,
898 PS3_AUDIO_AX_IC_AASOIMD_EVERY4);
900 /* enable 3wire clocks */
901 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
902 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED |
903 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED),
905 update_reg(PS3_AUDIO_AO_3WMCTRL,
906 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
909 static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
912 pr_debug("%s: start\n", __func__);
913 card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
914 card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
915 card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
916 card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM;
917 card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
918 memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8);
920 ret = snd_ps3_change_avsetting(card);
922 snd_ps3_audio_fixup(card);
924 /* to start to generate SPDIF signal, fill data */
925 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
926 snd_ps3_kick_dma(card);
927 pr_debug("%s: end\n", __func__);
931 static int snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
934 u64 lpar_addr, lpar_size;
936 if (WARN_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1)))
938 if (WARN_ON(dev->match_id != PS3_MATCH_ID_SOUND))
941 the_card.ps3_dev = dev;
943 ret = ps3_open_hv_device(dev);
949 ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size);
951 pr_info("%s: device map 2 failed %d\n", __func__, ret);
954 ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
957 ret = snd_ps3_map_mmio();
962 ps3_dma_region_init(dev, dev->d_region,
963 PAGE_SHIFT, /* use system page size */
964 0, /* dma type; not used */
966 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE));
967 dev->d_region->ioid = PS3_AUDIO_IOID;
969 ret = ps3_dma_region_create(dev->d_region);
971 pr_info("%s: region_create\n", __func__);
975 snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
977 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
978 the_card.start_delay = snd_ps3_start_delay;
981 if (snd_ps3_allocate_irq()) {
983 goto clean_dma_region;
986 /* create card instance */
987 ret = snd_card_create(index, id, THIS_MODULE, 0, &the_card.card);
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 ret = snd_pcm_lib_preallocate_pages_for_all(the_card.pcm,
1026 SND_PS3_PCM_PREALLOC_SIZE,
1027 SND_PS3_PCM_PREALLOC_SIZE);
1029 pr_info("%s: prealloc failed\n", __func__);
1034 * allocate null buffer
1035 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
1036 * PAGE_SIZE is enogh
1038 the_card.null_buffer_start_vaddr =
1039 dma_alloc_coherent(&the_card.ps3_dev->core,
1041 &the_card.null_buffer_start_dma_addr,
1043 if (!the_card.null_buffer_start_vaddr) {
1044 pr_info("%s: nullbuffer alloc failed\n", __func__);
1046 goto clean_preallocate;
1048 pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__,
1049 the_card.null_buffer_start_vaddr,
1050 the_card.null_buffer_start_dma_addr);
1051 /* set default sample rate/word width */
1052 snd_ps3_init_avsetting(&the_card);
1054 /* register the card */
1055 snd_card_set_dev(the_card.card, &dev->core);
1056 ret = snd_card_register(the_card.card);
1060 pr_info("%s started. start_delay=%dms\n",
1061 the_card.card->longname, the_card.start_delay);
1065 dma_free_coherent(&the_card.ps3_dev->core,
1067 the_card.null_buffer_start_vaddr,
1068 the_card.null_buffer_start_dma_addr);
1070 snd_pcm_lib_preallocate_free_for_all(the_card.pcm);
1072 snd_card_free(the_card.card);
1076 ps3_dma_region_free(dev->d_region);
1078 snd_ps3_unmap_mmio();
1080 lv1_gpu_device_unmap(2);
1082 ps3_close_hv_device(dev);
1084 * there is no destructor function to pcm.
1085 * midlayer automatically releases if the card removed
1088 }; /* snd_ps3_probe */
1090 /* called when module removal */
1091 static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev)
1094 pr_info("%s:start id=%d\n", __func__, dev->match_id);
1095 if (dev->match_id != PS3_MATCH_ID_SOUND)
1099 * ctl and preallocate buffer will be freed in
1102 ret = snd_card_free(the_card.card);
1104 pr_info("%s: ctl freecard=%d\n", __func__, ret);
1106 dma_free_coherent(&dev->core,
1108 the_card.null_buffer_start_vaddr,
1109 the_card.null_buffer_start_dma_addr);
1111 ps3_dma_region_free(dev->d_region);
1114 snd_ps3_unmap_mmio();
1116 lv1_gpu_device_unmap(2);
1117 ps3_close_hv_device(dev);
1118 pr_info("%s:end id=%d\n", __func__, dev->match_id);
1120 } /* snd_ps3_remove */
1122 static struct ps3_system_bus_driver snd_ps3_bus_driver_info = {
1123 .match_id = PS3_MATCH_ID_SOUND,
1124 .probe = snd_ps3_driver_probe,
1125 .remove = snd_ps3_driver_remove,
1126 .shutdown = snd_ps3_driver_remove,
1128 .name = SND_PS3_DRIVER_NAME,
1129 .owner = THIS_MODULE,
1135 * module/subsystem initialize/terminate
1137 static int __init snd_ps3_init(void)
1141 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1144 memset(&the_card, 0, sizeof(the_card));
1145 spin_lock_init(&the_card.dma_lock);
1147 /* register systembus DRIVER, this calls our probe() func */
1148 ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info);
1152 module_init(snd_ps3_init);
1154 static void __exit snd_ps3_exit(void)
1156 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info);
1158 module_exit(snd_ps3_exit);
1160 MODULE_LICENSE("GPL v2");
1161 MODULE_DESCRIPTION("PS3 sound driver");
1162 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1163 MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND);