gma500: Set the correct bits according to the pipe
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / ppc / snd_ps3.c
blobbc823a547550c740ab9d7871fc2e401a09d7afbd
1 /*
2 * Audio support for PS3
3 * Copyright (C) 2007 Sony Computer Entertainment Inc.
4 * All rights reserved.
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>
26 #include <linux/io.h>
28 #include <sound/asound.h>
29 #include <sound/control.h>
30 #include <sound/core.h>
31 #include <sound/initval.h>
32 #include <sound/memalloc.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
36 #include <asm/dma.h>
37 #include <asm/firmware.h>
38 #include <asm/lv1call.h>
39 #include <asm/ps3.h>
40 #include <asm/ps3av.h>
42 #include "snd_ps3.h"
43 #include "snd_ps3_reg.h"
47 * global
49 static struct snd_ps3_card_info the_card;
51 static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY;
53 module_param_named(start_delay, snd_ps3_start_delay, uint, 0644);
54 MODULE_PARM_DESC(start_delay, "time to insert silent data in ms");
56 static int index = SNDRV_DEFAULT_IDX1;
57 static char *id = SNDRV_DEFAULT_STR1;
59 module_param(index, int, 0444);
60 MODULE_PARM_DESC(index, "Index value for PS3 soundchip.");
61 module_param(id, charp, 0444);
62 MODULE_PARM_DESC(id, "ID string for PS3 soundchip.");
66 * PS3 audio register access
68 static inline u32 read_reg(unsigned int reg)
70 return in_be32(the_card.mapped_mmio_vaddr + reg);
72 static inline void write_reg(unsigned int reg, u32 val)
74 out_be32(the_card.mapped_mmio_vaddr + reg, val);
76 static inline void update_reg(unsigned int reg, u32 or_val)
78 u32 newval = read_reg(reg) | or_val;
79 write_reg(reg, newval);
81 static inline void update_mask_reg(unsigned int reg, u32 mask, u32 or_val)
83 u32 newval = (read_reg(reg) & mask) | or_val;
84 write_reg(reg, newval);
88 * ALSA defs
90 static const struct snd_pcm_hardware snd_ps3_pcm_hw = {
91 .info = (SNDRV_PCM_INFO_MMAP |
92 SNDRV_PCM_INFO_NONINTERLEAVED |
93 SNDRV_PCM_INFO_MMAP_VALID),
94 .formats = (SNDRV_PCM_FMTBIT_S16_BE |
95 SNDRV_PCM_FMTBIT_S24_BE),
96 .rates = (SNDRV_PCM_RATE_44100 |
97 SNDRV_PCM_RATE_48000 |
98 SNDRV_PCM_RATE_88200 |
99 SNDRV_PCM_RATE_96000),
100 .rate_min = 44100,
101 .rate_max = 96000,
103 .channels_min = 2, /* stereo only */
104 .channels_max = 2,
106 .buffer_bytes_max = PS3_AUDIO_FIFO_SIZE * 64,
108 /* interrupt by four stages */
109 .period_bytes_min = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
110 .period_bytes_max = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
112 .periods_min = 16,
113 .periods_max = 32, /* buffer_size_max/ period_bytes_max */
115 .fifo_size = PS3_AUDIO_FIFO_SIZE
118 static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info *card,
119 int count, int force_stop)
121 int dma_ch, done, retries, stop_forced = 0;
122 uint32_t status;
124 for (dma_ch = 0; dma_ch < 8; dma_ch++) {
125 retries = count;
126 do {
127 status = read_reg(PS3_AUDIO_KICK(dma_ch)) &
128 PS3_AUDIO_KICK_STATUS_MASK;
129 switch (status) {
130 case PS3_AUDIO_KICK_STATUS_DONE:
131 case PS3_AUDIO_KICK_STATUS_NOTIFY:
132 case PS3_AUDIO_KICK_STATUS_CLEAR:
133 case PS3_AUDIO_KICK_STATUS_ERROR:
134 done = 1;
135 break;
136 default:
137 done = 0;
138 udelay(10);
140 } while (!done && --retries);
141 if (!retries && force_stop) {
142 pr_info("%s: DMA ch %d is not stopped.",
143 __func__, dma_ch);
144 /* last resort. force to stop dma.
145 * NOTE: this cause DMA done interrupts
147 update_reg(PS3_AUDIO_CONFIG, PS3_AUDIO_CONFIG_CLEAR);
148 stop_forced = 1;
151 return stop_forced;
155 * wait for all dma is done.
156 * NOTE: caller should reset card->running before call.
157 * If not, the interrupt handler will re-start DMA,
158 * then DMA is never stopped.
160 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card)
162 int stop_forced;
164 * wait for the last dma is done
168 * expected maximum DMA done time is 5.7ms + something (DMA itself).
169 * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
170 * DMA kick event would occur.
172 stop_forced = snd_ps3_verify_dma_stop(card, 700, 1);
175 * clear outstanding interrupts.
177 update_reg(PS3_AUDIO_INTR_0, 0);
178 update_reg(PS3_AUDIO_AX_IS, 0);
181 *revert CLEAR bit since it will not reset automatically after DMA stop
183 if (stop_forced)
184 update_mask_reg(PS3_AUDIO_CONFIG, ~PS3_AUDIO_CONFIG_CLEAR, 0);
185 /* ensure the hardware sees changes */
186 wmb();
189 static void snd_ps3_kick_dma(struct snd_ps3_card_info *card)
192 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST);
193 /* ensure the hardware sees the change */
194 wmb();
198 * convert virtual addr to ioif bus addr.
200 static dma_addr_t v_to_bus(struct snd_ps3_card_info *card, void *paddr, int ch)
202 return card->dma_start_bus_addr[ch] +
203 (paddr - card->dma_start_vaddr[ch]);
208 * increment ring buffer pointer.
209 * NOTE: caller must hold write spinlock
211 static void snd_ps3_bump_buffer(struct snd_ps3_card_info *card,
212 enum snd_ps3_ch ch, size_t byte_count,
213 int stage)
215 if (!stage)
216 card->dma_last_transfer_vaddr[ch] =
217 card->dma_next_transfer_vaddr[ch];
218 card->dma_next_transfer_vaddr[ch] += byte_count;
219 if ((card->dma_start_vaddr[ch] + (card->dma_buffer_size / 2)) <=
220 card->dma_next_transfer_vaddr[ch]) {
221 card->dma_next_transfer_vaddr[ch] = card->dma_start_vaddr[ch];
225 * setup dmac to send data to audio and attenuate samples on the ring buffer
227 static int snd_ps3_program_dma(struct snd_ps3_card_info *card,
228 enum snd_ps3_dma_filltype filltype)
230 /* this dmac does not support over 4G */
231 uint32_t dma_addr;
232 int fill_stages, dma_ch, stage;
233 enum snd_ps3_ch ch;
234 uint32_t ch0_kick_event = 0; /* initialize to mute gcc */
235 void *start_vaddr;
236 unsigned long irqsave;
237 int silent = 0;
239 switch (filltype) {
240 case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL:
241 silent = 1;
242 /* intentionally fall thru */
243 case SND_PS3_DMA_FILLTYPE_FIRSTFILL:
244 ch0_kick_event = PS3_AUDIO_KICK_EVENT_ALWAYS;
245 break;
247 case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING:
248 silent = 1;
249 /* intentionally fall thru */
250 case SND_PS3_DMA_FILLTYPE_RUNNING:
251 ch0_kick_event = PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY;
252 break;
255 snd_ps3_verify_dma_stop(card, 700, 0);
256 fill_stages = 4;
257 spin_lock_irqsave(&card->dma_lock, irqsave);
258 for (ch = 0; ch < 2; ch++) {
259 start_vaddr = card->dma_next_transfer_vaddr[0];
260 for (stage = 0; stage < fill_stages; stage++) {
261 dma_ch = stage * 2 + ch;
262 if (silent)
263 dma_addr = card->null_buffer_start_dma_addr;
264 else
265 dma_addr =
266 v_to_bus(card,
267 card->dma_next_transfer_vaddr[ch],
268 ch);
270 write_reg(PS3_AUDIO_SOURCE(dma_ch),
271 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY |
272 dma_addr));
274 /* dst: fixed to 3wire#0 */
275 if (ch == 0)
276 write_reg(PS3_AUDIO_DEST(dma_ch),
277 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
278 PS3_AUDIO_AO_3W_LDATA(0)));
279 else
280 write_reg(PS3_AUDIO_DEST(dma_ch),
281 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
282 PS3_AUDIO_AO_3W_RDATA(0)));
284 /* count always 1 DMA block (1/2 stage = 128 bytes) */
285 write_reg(PS3_AUDIO_DMASIZE(dma_ch), 0);
286 /* bump pointer if needed */
287 if (!silent)
288 snd_ps3_bump_buffer(card, ch,
289 PS3_AUDIO_DMAC_BLOCK_SIZE,
290 stage);
292 /* kick event */
293 if (dma_ch == 0)
294 write_reg(PS3_AUDIO_KICK(dma_ch),
295 ch0_kick_event);
296 else
297 write_reg(PS3_AUDIO_KICK(dma_ch),
298 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
299 - 1) |
300 PS3_AUDIO_KICK_REQUEST);
303 /* ensure the hardware sees the change */
304 wmb();
305 spin_unlock_irqrestore(&card->dma_lock, irqsave);
307 return 0;
311 * Interrupt handler
313 static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id)
316 uint32_t port_intr;
317 int underflow_occured = 0;
318 struct snd_ps3_card_info *card = dev_id;
320 if (!card->running) {
321 update_reg(PS3_AUDIO_AX_IS, 0);
322 update_reg(PS3_AUDIO_INTR_0, 0);
323 return IRQ_HANDLED;
326 port_intr = read_reg(PS3_AUDIO_AX_IS);
328 *serial buffer empty detected (every 4 times),
329 *program next dma and kick it
331 if (port_intr & PS3_AUDIO_AX_IE_ASOBEIE(0)) {
332 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBEIE(0));
333 if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
334 write_reg(PS3_AUDIO_AX_IS, port_intr);
335 underflow_occured = 1;
337 if (card->silent) {
338 /* we are still in silent time */
339 snd_ps3_program_dma(card,
340 (underflow_occured) ?
341 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL :
342 SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
343 snd_ps3_kick_dma(card);
344 card->silent--;
345 } else {
346 snd_ps3_program_dma(card,
347 (underflow_occured) ?
348 SND_PS3_DMA_FILLTYPE_FIRSTFILL :
349 SND_PS3_DMA_FILLTYPE_RUNNING);
350 snd_ps3_kick_dma(card);
351 snd_pcm_period_elapsed(card->substream);
353 } else if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
354 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBUIE(0));
356 * serial out underflow, but buffer empty not detected.
357 * in this case, fill fifo with 0 to recover. After
358 * filling dummy data, serial automatically start to
359 * consume them and then will generate normal buffer
360 * empty interrupts.
361 * If both buffer underflow and buffer empty are occurred,
362 * it is better to do nomal data transfer than empty one
364 snd_ps3_program_dma(card,
365 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
366 snd_ps3_kick_dma(card);
367 snd_ps3_program_dma(card,
368 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
369 snd_ps3_kick_dma(card);
371 /* clear interrupt cause */
372 return IRQ_HANDLED;
376 * audio mute on/off
377 * mute_on : 0 output enabled
378 * 1 mute
380 static int snd_ps3_mute(int mute_on)
382 return ps3av_audio_mute(mute_on);
386 * av setting
387 * NOTE: calling this function may generate audio interrupt.
389 static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card)
391 int ret, retries, i;
392 pr_debug("%s: start\n", __func__);
394 ret = ps3av_set_audio_mode(card->avs.avs_audio_ch,
395 card->avs.avs_audio_rate,
396 card->avs.avs_audio_width,
397 card->avs.avs_audio_format,
398 card->avs.avs_audio_source);
400 * Reset the following unwanted settings:
403 /* disable all 3wire buffers */
404 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
405 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
406 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
407 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
408 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
410 wmb(); /* ensure the hardware sees the change */
411 /* wait for actually stopped */
412 retries = 1000;
413 while ((read_reg(PS3_AUDIO_AO_3WMCTRL) &
414 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
415 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
416 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
417 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
418 --retries) {
419 udelay(1);
422 /* reset buffer pointer */
423 for (i = 0; i < 4; i++) {
424 update_reg(PS3_AUDIO_AO_3WCTRL(i),
425 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET);
426 udelay(10);
428 wmb(); /* ensure the hardware actually start resetting */
430 /* enable 3wire#0 buffer */
431 update_reg(PS3_AUDIO_AO_3WMCTRL, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
434 /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
435 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
436 ~PS3_AUDIO_AO_3WCTRL_ASODF,
437 PS3_AUDIO_AO_3WCTRL_ASODF_LSB);
438 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
439 ~PS3_AUDIO_AO_SPDCTRL_SPODF,
440 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB);
441 /* ensure all the setting above is written back to register */
442 wmb();
443 /* avsetting driver altered AX_IE, caller must reset it if you want */
444 pr_debug("%s: end\n", __func__);
445 return ret;
449 * set sampling rate according to the substream
451 static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream)
453 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
454 struct snd_ps3_avsetting_info avs;
455 int ret;
457 avs = card->avs;
459 pr_debug("%s: called freq=%d width=%d\n", __func__,
460 substream->runtime->rate,
461 snd_pcm_format_width(substream->runtime->format));
463 pr_debug("%s: before freq=%d width=%d\n", __func__,
464 card->avs.avs_audio_rate, card->avs.avs_audio_width);
466 /* sample rate */
467 switch (substream->runtime->rate) {
468 case 44100:
469 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_44K;
470 break;
471 case 48000:
472 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
473 break;
474 case 88200:
475 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_88K;
476 break;
477 case 96000:
478 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_96K;
479 break;
480 default:
481 pr_info("%s: invalid rate %d\n", __func__,
482 substream->runtime->rate);
483 return 1;
486 /* width */
487 switch (snd_pcm_format_width(substream->runtime->format)) {
488 case 16:
489 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
490 break;
491 case 24:
492 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_24;
493 break;
494 default:
495 pr_info("%s: invalid width %d\n", __func__,
496 snd_pcm_format_width(substream->runtime->format));
497 return 1;
500 memcpy(avs.avs_cs_info, ps3av_mode_cs_info, 8);
502 if (memcmp(&card->avs, &avs, sizeof(avs))) {
503 pr_debug("%s: after freq=%d width=%d\n", __func__,
504 card->avs.avs_audio_rate, card->avs.avs_audio_width);
506 card->avs = avs;
507 snd_ps3_change_avsetting(card);
508 ret = 0;
509 } else
510 ret = 1;
512 /* check CS non-audio bit and mute accordingly */
513 if (avs.avs_cs_info[0] & 0x02)
514 ps3av_audio_mute_analog(1); /* mute if non-audio */
515 else
516 ps3av_audio_mute_analog(0);
518 return ret;
522 * PCM operators
524 static int snd_ps3_pcm_open(struct snd_pcm_substream *substream)
526 struct snd_pcm_runtime *runtime = substream->runtime;
527 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
528 int pcm_index;
530 pcm_index = substream->pcm->device;
531 /* to retrieve substream/runtime in interrupt handler */
532 card->substream = substream;
534 runtime->hw = snd_ps3_pcm_hw;
536 card->start_delay = snd_ps3_start_delay;
538 /* mute off */
539 snd_ps3_mute(0); /* this function sleep */
541 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
542 PS3_AUDIO_FIFO_STAGE_SIZE * 4 * 2);
543 return 0;
546 static int snd_ps3_pcm_close(struct snd_pcm_substream *substream)
548 /* mute on */
549 snd_ps3_mute(1);
550 return 0;
553 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
554 struct snd_pcm_hw_params *hw_params)
556 size_t size;
558 /* alloc transport buffer */
559 size = params_buffer_bytes(hw_params);
560 snd_pcm_lib_malloc_pages(substream, size);
561 return 0;
564 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream)
566 int ret;
567 ret = snd_pcm_lib_free_pages(substream);
568 return ret;
571 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
572 unsigned int delay_ms)
574 int ret;
575 int rate ;
577 rate = substream->runtime->rate;
578 ret = snd_pcm_format_size(substream->runtime->format,
579 rate * delay_ms / 1000)
580 * substream->runtime->channels;
582 pr_debug("%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
583 __func__,
584 delay_ms,
585 rate,
586 snd_pcm_format_size(substream->runtime->format, rate),
587 rate * delay_ms / 1000,
588 ret);
590 return ret;
593 static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
595 struct snd_pcm_runtime *runtime = substream->runtime;
596 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
597 unsigned long irqsave;
599 if (!snd_ps3_set_avsetting(substream)) {
600 /* some parameter changed */
601 write_reg(PS3_AUDIO_AX_IE,
602 PS3_AUDIO_AX_IE_ASOBEIE(0) |
603 PS3_AUDIO_AX_IE_ASOBUIE(0));
605 * let SPDIF device re-lock with SPDIF signal,
606 * start with some silence
608 card->silent = snd_ps3_delay_to_bytes(substream,
609 card->start_delay) /
610 (PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */
613 /* restart ring buffer pointer */
614 spin_lock_irqsave(&card->dma_lock, irqsave);
616 card->dma_buffer_size = runtime->dma_bytes;
618 card->dma_last_transfer_vaddr[SND_PS3_CH_L] =
619 card->dma_next_transfer_vaddr[SND_PS3_CH_L] =
620 card->dma_start_vaddr[SND_PS3_CH_L] =
621 runtime->dma_area;
622 card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr;
624 card->dma_last_transfer_vaddr[SND_PS3_CH_R] =
625 card->dma_next_transfer_vaddr[SND_PS3_CH_R] =
626 card->dma_start_vaddr[SND_PS3_CH_R] =
627 runtime->dma_area + (runtime->dma_bytes / 2);
628 card->dma_start_bus_addr[SND_PS3_CH_R] =
629 runtime->dma_addr + (runtime->dma_bytes / 2);
631 pr_debug("%s: vaddr=%p bus=%#llx\n", __func__,
632 card->dma_start_vaddr[SND_PS3_CH_L],
633 card->dma_start_bus_addr[SND_PS3_CH_L]);
636 spin_unlock_irqrestore(&card->dma_lock, irqsave);
638 /* ensure the hardware sees the change */
639 mb();
641 return 0;
644 static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
645 int cmd)
647 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
648 int ret = 0;
650 switch (cmd) {
651 case SNDRV_PCM_TRIGGER_START:
652 /* clear outstanding interrupts */
653 update_reg(PS3_AUDIO_AX_IS, 0);
655 spin_lock(&card->dma_lock);
657 card->running = 1;
659 spin_unlock(&card->dma_lock);
661 snd_ps3_program_dma(card,
662 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
663 snd_ps3_kick_dma(card);
664 while (read_reg(PS3_AUDIO_KICK(7)) &
665 PS3_AUDIO_KICK_STATUS_MASK) {
666 udelay(1);
668 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
669 snd_ps3_kick_dma(card);
670 break;
672 case SNDRV_PCM_TRIGGER_STOP:
673 spin_lock(&card->dma_lock);
675 card->running = 0;
677 spin_unlock(&card->dma_lock);
678 snd_ps3_wait_for_dma_stop(card);
679 break;
680 default:
681 break;
685 return ret;
689 * report current pointer
691 static snd_pcm_uframes_t snd_ps3_pcm_pointer(
692 struct snd_pcm_substream *substream)
694 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
695 size_t bytes;
696 snd_pcm_uframes_t ret;
698 spin_lock(&card->dma_lock);
700 bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
701 card->dma_start_vaddr[SND_PS3_CH_L]);
703 spin_unlock(&card->dma_lock);
705 ret = bytes_to_frames(substream->runtime, bytes * 2);
707 return ret;
711 * SPDIF status bits controls
713 static int snd_ps3_spdif_mask_info(struct snd_kcontrol *kcontrol,
714 struct snd_ctl_elem_info *uinfo)
716 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
717 uinfo->count = 1;
718 return 0;
721 /* FIXME: ps3av_set_audio_mode() assumes only consumer mode */
722 static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol,
723 struct snd_ctl_elem_value *ucontrol)
725 memset(ucontrol->value.iec958.status, 0xff, 8);
726 return 0;
729 static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol,
730 struct snd_ctl_elem_value *ucontrol)
732 return 0;
735 static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol,
736 struct snd_ctl_elem_value *ucontrol)
738 memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8);
739 return 0;
742 static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol,
743 struct snd_ctl_elem_value *ucontrol)
745 if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) {
746 memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8);
747 return 1;
749 return 0;
752 static struct snd_kcontrol_new spdif_ctls[] = {
754 .access = SNDRV_CTL_ELEM_ACCESS_READ,
755 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
756 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
757 .info = snd_ps3_spdif_mask_info,
758 .get = snd_ps3_spdif_cmask_get,
761 .access = SNDRV_CTL_ELEM_ACCESS_READ,
762 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
763 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
764 .info = snd_ps3_spdif_mask_info,
765 .get = snd_ps3_spdif_pmask_get,
768 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
769 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
770 .info = snd_ps3_spdif_mask_info,
771 .get = snd_ps3_spdif_default_get,
772 .put = snd_ps3_spdif_default_put,
776 static struct snd_pcm_ops snd_ps3_pcm_spdif_ops = {
777 .open = snd_ps3_pcm_open,
778 .close = snd_ps3_pcm_close,
779 .ioctl = snd_pcm_lib_ioctl,
780 .hw_params = snd_ps3_pcm_hw_params,
781 .hw_free = snd_ps3_pcm_hw_free,
782 .prepare = snd_ps3_pcm_prepare,
783 .trigger = snd_ps3_pcm_trigger,
784 .pointer = snd_ps3_pcm_pointer,
788 static int __devinit snd_ps3_map_mmio(void)
790 the_card.mapped_mmio_vaddr =
791 ioremap(the_card.ps3_dev->m_region->bus_addr,
792 the_card.ps3_dev->m_region->len);
794 if (!the_card.mapped_mmio_vaddr) {
795 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
796 __func__, the_card.ps3_dev->m_region->lpar_addr,
797 the_card.ps3_dev->m_region->len);
798 return -ENXIO;
801 return 0;
804 static void snd_ps3_unmap_mmio(void)
806 iounmap(the_card.mapped_mmio_vaddr);
807 the_card.mapped_mmio_vaddr = NULL;
810 static int __devinit snd_ps3_allocate_irq(void)
812 int ret;
813 u64 lpar_addr, lpar_size;
814 u64 __iomem *mapped;
816 /* FIXME: move this to device_init (H/W probe) */
818 /* get irq outlet */
819 ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size);
820 if (ret) {
821 pr_info("%s: device map 1 failed %d\n", __func__,
822 ret);
823 return -ENXIO;
826 mapped = ioremap(lpar_addr, lpar_size);
827 if (!mapped) {
828 pr_info("%s: ioremap 1 failed \n", __func__);
829 return -ENXIO;
832 the_card.audio_irq_outlet = in_be64(mapped);
834 iounmap(mapped);
835 ret = lv1_gpu_device_unmap(1);
836 if (ret)
837 pr_info("%s: unmap 1 failed\n", __func__);
839 /* irq */
840 ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY,
841 the_card.audio_irq_outlet,
842 &the_card.irq_no);
843 if (ret) {
844 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret);
845 return ret;
848 ret = request_irq(the_card.irq_no, snd_ps3_interrupt, IRQF_DISABLED,
849 SND_PS3_DRIVER_NAME, &the_card);
850 if (ret) {
851 pr_info("%s: request_irq failed (%d)\n", __func__, ret);
852 goto cleanup_irq;
855 return 0;
857 cleanup_irq:
858 ps3_irq_plug_destroy(the_card.irq_no);
859 return ret;
862 static void snd_ps3_free_irq(void)
864 free_irq(the_card.irq_no, &the_card);
865 ps3_irq_plug_destroy(the_card.irq_no);
868 static void __devinit snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
870 uint64_t val;
871 int ret;
873 val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
874 (0x03UL << 24) |
875 (0x0fUL << 12) |
876 (PS3_AUDIO_IOID);
878 ret = lv1_gpu_attribute(0x100, 0x007, val, 0, 0);
879 if (ret)
880 pr_info("%s: gpu_attribute failed %d\n", __func__,
881 ret);
884 static void __devinit snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
887 * avsetting driver seems to never change the followings
888 * so, init them here once
891 /* no dma interrupt needed */
892 write_reg(PS3_AUDIO_INTR_EN_0, 0);
894 /* use every 4 buffer empty interrupt */
895 update_mask_reg(PS3_AUDIO_AX_IC,
896 PS3_AUDIO_AX_IC_AASOIMD_MASK,
897 PS3_AUDIO_AX_IC_AASOIMD_EVERY4);
899 /* enable 3wire clocks */
900 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
901 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED |
902 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED),
904 update_reg(PS3_AUDIO_AO_3WMCTRL,
905 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
908 static int __devinit snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
910 int ret;
911 pr_debug("%s: start\n", __func__);
912 card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
913 card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
914 card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
915 card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM;
916 card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
917 memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8);
919 ret = snd_ps3_change_avsetting(card);
921 snd_ps3_audio_fixup(card);
923 /* to start to generate SPDIF signal, fill data */
924 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
925 snd_ps3_kick_dma(card);
926 pr_debug("%s: end\n", __func__);
927 return ret;
930 static int __devinit snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
932 int i, ret;
933 u64 lpar_addr, lpar_size;
935 BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1));
936 BUG_ON(dev->match_id != PS3_MATCH_ID_SOUND);
938 the_card.ps3_dev = dev;
940 ret = ps3_open_hv_device(dev);
942 if (ret)
943 return -ENXIO;
945 /* setup MMIO */
946 ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size);
947 if (ret) {
948 pr_info("%s: device map 2 failed %d\n", __func__, ret);
949 goto clean_open;
951 ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
952 PAGE_SHIFT);
954 ret = snd_ps3_map_mmio();
955 if (ret)
956 goto clean_dev_map;
958 /* setup DMA area */
959 ps3_dma_region_init(dev, dev->d_region,
960 PAGE_SHIFT, /* use system page size */
961 0, /* dma type; not used */
962 NULL,
963 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE));
964 dev->d_region->ioid = PS3_AUDIO_IOID;
966 ret = ps3_dma_region_create(dev->d_region);
967 if (ret) {
968 pr_info("%s: region_create\n", __func__);
969 goto clean_mmio;
972 snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
974 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
975 the_card.start_delay = snd_ps3_start_delay;
977 /* irq */
978 if (snd_ps3_allocate_irq()) {
979 ret = -ENXIO;
980 goto clean_dma_region;
983 /* create card instance */
984 ret = snd_card_create(index, id, THIS_MODULE, 0, &the_card.card);
985 if (ret < 0)
986 goto clean_irq;
988 strcpy(the_card.card->driver, "PS3");
989 strcpy(the_card.card->shortname, "PS3");
990 strcpy(the_card.card->longname, "PS3 sound");
992 /* create control elements */
993 for (i = 0; i < ARRAY_SIZE(spdif_ctls); i++) {
994 ret = snd_ctl_add(the_card.card,
995 snd_ctl_new1(&spdif_ctls[i], &the_card));
996 if (ret < 0)
997 goto clean_card;
1000 /* create PCM devices instance */
1001 /* NOTE:this driver works assuming pcm:substream = 1:1 */
1002 ret = snd_pcm_new(the_card.card,
1003 "SPDIF",
1004 0, /* instance index, will be stored pcm.device*/
1005 1, /* output substream */
1006 0, /* input substream */
1007 &(the_card.pcm));
1008 if (ret)
1009 goto clean_card;
1011 the_card.pcm->private_data = &the_card;
1012 strcpy(the_card.pcm->name, "SPDIF");
1014 /* set pcm ops */
1015 snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK,
1016 &snd_ps3_pcm_spdif_ops);
1018 the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED;
1019 /* pre-alloc PCM DMA buffer*/
1020 ret = snd_pcm_lib_preallocate_pages_for_all(the_card.pcm,
1021 SNDRV_DMA_TYPE_DEV,
1022 &dev->core,
1023 SND_PS3_PCM_PREALLOC_SIZE,
1024 SND_PS3_PCM_PREALLOC_SIZE);
1025 if (ret < 0) {
1026 pr_info("%s: prealloc failed\n", __func__);
1027 goto clean_card;
1031 * allocate null buffer
1032 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
1033 * PAGE_SIZE is enogh
1035 the_card.null_buffer_start_vaddr =
1036 dma_alloc_coherent(&the_card.ps3_dev->core,
1037 PAGE_SIZE,
1038 &the_card.null_buffer_start_dma_addr,
1039 GFP_KERNEL);
1040 if (!the_card.null_buffer_start_vaddr) {
1041 pr_info("%s: nullbuffer alloc failed\n", __func__);
1042 goto clean_preallocate;
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 snd_card_set_dev(the_card.card, &dev->core);
1052 ret = snd_card_register(the_card.card);
1053 if (ret < 0)
1054 goto clean_dma_map;
1056 pr_info("%s started. start_delay=%dms\n",
1057 the_card.card->longname, the_card.start_delay);
1058 return 0;
1060 clean_dma_map:
1061 dma_free_coherent(&the_card.ps3_dev->core,
1062 PAGE_SIZE,
1063 the_card.null_buffer_start_vaddr,
1064 the_card.null_buffer_start_dma_addr);
1065 clean_preallocate:
1066 snd_pcm_lib_preallocate_free_for_all(the_card.pcm);
1067 clean_card:
1068 snd_card_free(the_card.card);
1069 clean_irq:
1070 snd_ps3_free_irq();
1071 clean_dma_region:
1072 ps3_dma_region_free(dev->d_region);
1073 clean_mmio:
1074 snd_ps3_unmap_mmio();
1075 clean_dev_map:
1076 lv1_gpu_device_unmap(2);
1077 clean_open:
1078 ps3_close_hv_device(dev);
1080 * there is no destructor function to pcm.
1081 * midlayer automatically releases if the card removed
1083 return ret;
1084 }; /* snd_ps3_probe */
1086 /* called when module removal */
1087 static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev)
1089 int ret;
1090 pr_info("%s:start id=%d\n", __func__, dev->match_id);
1091 if (dev->match_id != PS3_MATCH_ID_SOUND)
1092 return -ENXIO;
1095 * ctl and preallocate buffer will be freed in
1096 * snd_card_free
1098 ret = snd_card_free(the_card.card);
1099 if (ret)
1100 pr_info("%s: ctl freecard=%d\n", __func__, ret);
1102 dma_free_coherent(&dev->core,
1103 PAGE_SIZE,
1104 the_card.null_buffer_start_vaddr,
1105 the_card.null_buffer_start_dma_addr);
1107 ps3_dma_region_free(dev->d_region);
1109 snd_ps3_free_irq();
1110 snd_ps3_unmap_mmio();
1112 lv1_gpu_device_unmap(2);
1113 ps3_close_hv_device(dev);
1114 pr_info("%s:end id=%d\n", __func__, dev->match_id);
1115 return 0;
1116 } /* snd_ps3_remove */
1118 static struct ps3_system_bus_driver snd_ps3_bus_driver_info = {
1119 .match_id = PS3_MATCH_ID_SOUND,
1120 .probe = snd_ps3_driver_probe,
1121 .remove = snd_ps3_driver_remove,
1122 .shutdown = snd_ps3_driver_remove,
1123 .core = {
1124 .name = SND_PS3_DRIVER_NAME,
1125 .owner = THIS_MODULE,
1131 * module/subsystem initialize/terminate
1133 static int __init snd_ps3_init(void)
1135 int ret;
1137 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1138 return -ENXIO;
1140 memset(&the_card, 0, sizeof(the_card));
1141 spin_lock_init(&the_card.dma_lock);
1143 /* register systembus DRIVER, this calls our probe() func */
1144 ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info);
1146 return ret;
1148 module_init(snd_ps3_init);
1150 static void __exit snd_ps3_exit(void)
1152 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info);
1154 module_exit(snd_ps3_exit);
1156 MODULE_LICENSE("GPL v2");
1157 MODULE_DESCRIPTION("PS3 sound driver");
1158 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1159 MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND);