[ALSA] Clean up duplicate includes in sound/ppc/
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / ppc / snd_ps3.c
blob27b61899fe84d6cae6f86fbaa2923ba05343a596
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/init.h>
22 #include <linux/slab.h>
23 #include <linux/io.h>
24 #include <linux/interrupt.h>
25 #include <sound/driver.h>
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/pcm.h>
29 #include <sound/asound.h>
30 #include <sound/memalloc.h>
31 #include <sound/pcm_params.h>
32 #include <sound/control.h>
33 #include <linux/dmapool.h>
34 #include <linux/dma-mapping.h>
35 #include <asm/firmware.h>
36 #include <asm/dma.h>
37 #include <asm/lv1call.h>
38 #include <asm/ps3.h>
39 #include <asm/ps3av.h>
41 #include "snd_ps3_reg.h"
42 #include "snd_ps3.h"
44 MODULE_LICENSE("GPL v2");
45 MODULE_DESCRIPTION("PS3 sound driver");
46 MODULE_AUTHOR("Sony Computer Entertainment Inc.");
48 /* module entries */
49 static int __init snd_ps3_init(void);
50 static void __exit snd_ps3_exit(void);
52 /* ALSA snd driver ops */
53 static int snd_ps3_pcm_open(struct snd_pcm_substream *substream);
54 static int snd_ps3_pcm_close(struct snd_pcm_substream *substream);
55 static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream);
56 static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
57 int cmd);
58 static snd_pcm_uframes_t snd_ps3_pcm_pointer(struct snd_pcm_substream
59 *substream);
60 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
61 struct snd_pcm_hw_params *hw_params);
62 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream);
65 /* ps3_system_bus_driver entries */
66 static int __init snd_ps3_driver_probe(struct ps3_system_bus_device *dev);
67 static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev);
69 /* address setup */
70 static int snd_ps3_map_mmio(void);
71 static void snd_ps3_unmap_mmio(void);
72 static int snd_ps3_allocate_irq(void);
73 static void snd_ps3_free_irq(void);
74 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start);
76 /* interrupt handler */
77 static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id);
80 /* set sampling rate/format */
81 static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream);
82 /* take effect parameter change */
83 static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card);
84 /* initialize avsetting and take it effect */
85 static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card);
86 /* setup dma */
87 static int snd_ps3_program_dma(struct snd_ps3_card_info *card,
88 enum snd_ps3_dma_filltype filltype);
89 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card);
91 static dma_addr_t v_to_bus(struct snd_ps3_card_info *, void *vaddr, int ch);
94 module_init(snd_ps3_init);
95 module_exit(snd_ps3_exit);
98 * global
100 static struct snd_ps3_card_info the_card;
102 static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY;
104 module_param_named(start_delay, snd_ps3_start_delay, uint, 0644);
105 MODULE_PARM_DESC(start_delay, "time to insert silent data in milisec");
107 static int index = SNDRV_DEFAULT_IDX1;
108 static char *id = SNDRV_DEFAULT_STR1;
110 module_param(index, int, 0444);
111 MODULE_PARM_DESC(index, "Index value for PS3 soundchip.");
112 module_param(id, charp, 0444);
113 MODULE_PARM_DESC(id, "ID string for PS3 soundchip.");
117 * PS3 audio register access
119 static inline u32 read_reg(unsigned int reg)
121 return in_be32(the_card.mapped_mmio_vaddr + reg);
123 static inline void write_reg(unsigned int reg, u32 val)
125 out_be32(the_card.mapped_mmio_vaddr + reg, val);
127 static inline void update_reg(unsigned int reg, u32 or_val)
129 u32 newval = read_reg(reg) | or_val;
130 write_reg(reg, newval);
132 static inline void update_mask_reg(unsigned int reg, u32 mask, u32 or_val)
134 u32 newval = (read_reg(reg) & mask) | or_val;
135 write_reg(reg, newval);
139 * ALSA defs
141 const static struct snd_pcm_hardware snd_ps3_pcm_hw = {
142 .info = (SNDRV_PCM_INFO_MMAP |
143 SNDRV_PCM_INFO_NONINTERLEAVED |
144 SNDRV_PCM_INFO_MMAP_VALID),
145 .formats = (SNDRV_PCM_FMTBIT_S16_BE |
146 SNDRV_PCM_FMTBIT_S24_BE),
147 .rates = (SNDRV_PCM_RATE_44100 |
148 SNDRV_PCM_RATE_48000 |
149 SNDRV_PCM_RATE_88200 |
150 SNDRV_PCM_RATE_96000),
151 .rate_min = 44100,
152 .rate_max = 96000,
154 .channels_min = 2, /* stereo only */
155 .channels_max = 2,
157 .buffer_bytes_max = PS3_AUDIO_FIFO_SIZE * 64,
159 /* interrupt by four stages */
160 .period_bytes_min = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
161 .period_bytes_max = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
163 .periods_min = 16,
164 .periods_max = 32, /* buffer_size_max/ period_bytes_max */
166 .fifo_size = PS3_AUDIO_FIFO_SIZE
169 static struct snd_pcm_ops snd_ps3_pcm_spdif_ops =
171 .open = snd_ps3_pcm_open,
172 .close = snd_ps3_pcm_close,
173 .prepare = snd_ps3_pcm_prepare,
174 .ioctl = snd_pcm_lib_ioctl,
175 .trigger = snd_ps3_pcm_trigger,
176 .pointer = snd_ps3_pcm_pointer,
177 .hw_params = snd_ps3_pcm_hw_params,
178 .hw_free = snd_ps3_pcm_hw_free
181 static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info *card,
182 int count, int force_stop)
184 int dma_ch, done, retries, stop_forced = 0;
185 uint32_t status;
187 for (dma_ch = 0; dma_ch < 8; dma_ch ++) {
188 retries = count;
189 do {
190 status = read_reg(PS3_AUDIO_KICK(dma_ch)) &
191 PS3_AUDIO_KICK_STATUS_MASK;
192 switch (status) {
193 case PS3_AUDIO_KICK_STATUS_DONE:
194 case PS3_AUDIO_KICK_STATUS_NOTIFY:
195 case PS3_AUDIO_KICK_STATUS_CLEAR:
196 case PS3_AUDIO_KICK_STATUS_ERROR:
197 done = 1;
198 break;
199 default:
200 done = 0;
201 udelay(10);
203 } while (!done && --retries);
204 if (!retries && force_stop) {
205 pr_info("%s: DMA ch %d is not stopped.",
206 __func__, dma_ch);
207 /* last resort. force to stop dma.
208 * NOTE: this cause DMA done interrupts
210 update_reg(PS3_AUDIO_CONFIG, PS3_AUDIO_CONFIG_CLEAR);
211 stop_forced = 1;
214 return stop_forced;
218 * wait for all dma is done.
219 * NOTE: caller should reset card->running before call.
220 * If not, the interrupt handler will re-start DMA,
221 * then DMA is never stopped.
223 static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card)
225 int stop_forced;
227 * wait for the last dma is done
231 * expected maximum DMA done time is 5.7ms + something (DMA itself).
232 * 5.7ms is from 16bit/sample 2ch 44.1Khz; the time next
233 * DMA kick event would occur.
235 stop_forced = snd_ps3_verify_dma_stop(card, 700, 1);
238 * clear outstanding interrupts.
240 update_reg(PS3_AUDIO_INTR_0, 0);
241 update_reg(PS3_AUDIO_AX_IS, 0);
244 *revert CLEAR bit since it will not reset automatically after DMA stop
246 if (stop_forced)
247 update_mask_reg(PS3_AUDIO_CONFIG, ~PS3_AUDIO_CONFIG_CLEAR, 0);
248 /* ensure the hardware sees changes */
249 wmb();
252 static void snd_ps3_kick_dma(struct snd_ps3_card_info *card)
255 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST);
256 /* ensure the hardware sees the change */
257 wmb();
261 * convert virtual addr to ioif bus addr.
263 static dma_addr_t v_to_bus(struct snd_ps3_card_info *card,
264 void * paddr,
265 int ch)
267 return card->dma_start_bus_addr[ch] +
268 (paddr - card->dma_start_vaddr[ch]);
273 * increment ring buffer pointer.
274 * NOTE: caller must hold write spinlock
276 static void snd_ps3_bump_buffer(struct snd_ps3_card_info *card,
277 enum snd_ps3_ch ch, size_t byte_count,
278 int stage)
280 if (!stage)
281 card->dma_last_transfer_vaddr[ch] =
282 card->dma_next_transfer_vaddr[ch];
283 card->dma_next_transfer_vaddr[ch] += byte_count;
284 if ((card->dma_start_vaddr[ch] + (card->dma_buffer_size / 2)) <=
285 card->dma_next_transfer_vaddr[ch]) {
286 card->dma_next_transfer_vaddr[ch] = card->dma_start_vaddr[ch];
290 * setup dmac to send data to audio and attenuate samples on the ring buffer
292 static int snd_ps3_program_dma(struct snd_ps3_card_info *card,
293 enum snd_ps3_dma_filltype filltype)
295 /* this dmac does not support over 4G */
296 uint32_t dma_addr;
297 int fill_stages, dma_ch, stage;
298 enum snd_ps3_ch ch;
299 uint32_t ch0_kick_event = 0; /* initialize to mute gcc */
300 void *start_vaddr;
301 unsigned long irqsave;
302 int silent = 0;
304 switch (filltype) {
305 case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL:
306 silent = 1;
307 /* intentionally fall thru */
308 case SND_PS3_DMA_FILLTYPE_FIRSTFILL:
309 ch0_kick_event = PS3_AUDIO_KICK_EVENT_ALWAYS;
310 break;
312 case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING:
313 silent = 1;
314 /* intentionally fall thru */
315 case SND_PS3_DMA_FILLTYPE_RUNNING:
316 ch0_kick_event = PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY;
317 break;
320 snd_ps3_verify_dma_stop(card, 700, 0);
321 fill_stages = 4;
322 spin_lock_irqsave(&card->dma_lock, irqsave);
323 for (ch = 0; ch < 2; ch++) {
324 start_vaddr = card->dma_next_transfer_vaddr[0];
325 for (stage = 0; stage < fill_stages; stage ++) {
326 dma_ch = stage * 2 + ch;
327 if (silent)
328 dma_addr = card->null_buffer_start_dma_addr;
329 else
330 dma_addr =
331 v_to_bus(card,
332 card->dma_next_transfer_vaddr[ch],
333 ch);
335 write_reg(PS3_AUDIO_SOURCE(dma_ch),
336 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY |
337 dma_addr));
339 /* dst: fixed to 3wire#0 */
340 if (ch == 0)
341 write_reg(PS3_AUDIO_DEST(dma_ch),
342 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
343 PS3_AUDIO_AO_3W_LDATA(0)));
344 else
345 write_reg(PS3_AUDIO_DEST(dma_ch),
346 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
347 PS3_AUDIO_AO_3W_RDATA(0)));
349 /* count always 1 DMA block (1/2 stage = 128 bytes) */
350 write_reg(PS3_AUDIO_DMASIZE(dma_ch), 0);
351 /* bump pointer if needed */
352 if (!silent)
353 snd_ps3_bump_buffer(card, ch,
354 PS3_AUDIO_DMAC_BLOCK_SIZE,
355 stage);
357 /* kick event */
358 if (dma_ch == 0)
359 write_reg(PS3_AUDIO_KICK(dma_ch),
360 ch0_kick_event);
361 else
362 write_reg(PS3_AUDIO_KICK(dma_ch),
363 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
364 - 1) |
365 PS3_AUDIO_KICK_REQUEST);
368 /* ensure the hardware sees the change */
369 wmb();
370 spin_unlock_irqrestore(&card->dma_lock, irqsave);
372 return 0;
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 * PCM operators
388 static int snd_ps3_pcm_open(struct snd_pcm_substream *substream)
390 struct snd_pcm_runtime *runtime = substream->runtime;
391 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
392 int pcm_index;
394 pcm_index = substream->pcm->device;
395 /* to retrieve substream/runtime in interrupt handler */
396 card->substream = substream;
398 runtime->hw = snd_ps3_pcm_hw;
400 card->start_delay = snd_ps3_start_delay;
402 /* mute off */
403 snd_ps3_mute(0); /* this function sleep */
405 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
406 PS3_AUDIO_FIFO_STAGE_SIZE * 4 * 2);
407 return 0;
410 static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
411 struct snd_pcm_hw_params *hw_params)
413 size_t size;
415 /* alloc transport buffer */
416 size = params_buffer_bytes(hw_params);
417 snd_pcm_lib_malloc_pages(substream, size);
418 return 0;
421 static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
422 unsigned int delay_ms)
424 int ret;
425 int rate ;
427 rate = substream->runtime->rate;
428 ret = snd_pcm_format_size(substream->runtime->format,
429 rate * delay_ms / 1000)
430 * substream->runtime->channels;
432 pr_debug(KERN_ERR "%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
433 __func__,
434 delay_ms,
435 rate,
436 snd_pcm_format_size(substream->runtime->format, rate),
437 rate * delay_ms / 1000,
438 ret);
440 return ret;
443 static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
445 struct snd_pcm_runtime *runtime = substream->runtime;
446 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
447 unsigned long irqsave;
449 if (!snd_ps3_set_avsetting(substream)) {
450 /* some parameter changed */
451 write_reg(PS3_AUDIO_AX_IE,
452 PS3_AUDIO_AX_IE_ASOBEIE(0) |
453 PS3_AUDIO_AX_IE_ASOBUIE(0));
455 * let SPDIF device re-lock with SPDIF signal,
456 * start with some silence
458 card->silent = snd_ps3_delay_to_bytes(substream,
459 card->start_delay) /
460 (PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */
463 /* restart ring buffer pointer */
464 spin_lock_irqsave(&card->dma_lock, irqsave);
466 card->dma_buffer_size = runtime->dma_bytes;
468 card->dma_last_transfer_vaddr[SND_PS3_CH_L] =
469 card->dma_next_transfer_vaddr[SND_PS3_CH_L] =
470 card->dma_start_vaddr[SND_PS3_CH_L] =
471 runtime->dma_area;
472 card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr;
474 card->dma_last_transfer_vaddr[SND_PS3_CH_R] =
475 card->dma_next_transfer_vaddr[SND_PS3_CH_R] =
476 card->dma_start_vaddr[SND_PS3_CH_R] =
477 runtime->dma_area + (runtime->dma_bytes / 2);
478 card->dma_start_bus_addr[SND_PS3_CH_R] =
479 runtime->dma_addr + (runtime->dma_bytes / 2);
481 pr_debug("%s: vaddr=%p bus=%#lx\n", __func__,
482 card->dma_start_vaddr[SND_PS3_CH_L],
483 card->dma_start_bus_addr[SND_PS3_CH_L]);
486 spin_unlock_irqrestore(&card->dma_lock, irqsave);
488 /* ensure the hardware sees the change */
489 mb();
491 return 0;
494 static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
495 int cmd)
497 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
498 int ret = 0;
500 switch (cmd) {
501 case SNDRV_PCM_TRIGGER_START:
502 /* clear outstanding interrupts */
503 update_reg(PS3_AUDIO_AX_IS, 0);
505 spin_lock(&card->dma_lock);
507 card->running = 1;
509 spin_unlock(&card->dma_lock);
511 snd_ps3_program_dma(card,
512 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
513 snd_ps3_kick_dma(card);
514 while (read_reg(PS3_AUDIO_KICK(7)) &
515 PS3_AUDIO_KICK_STATUS_MASK) {
516 udelay(1);
518 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
519 snd_ps3_kick_dma(card);
520 break;
522 case SNDRV_PCM_TRIGGER_STOP:
523 spin_lock(&card->dma_lock);
525 card->running = 0;
527 spin_unlock(&card->dma_lock);
528 snd_ps3_wait_for_dma_stop(card);
529 break;
530 default:
531 break;
535 return ret;
539 * report current pointer
541 static snd_pcm_uframes_t snd_ps3_pcm_pointer(
542 struct snd_pcm_substream *substream)
544 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
545 size_t bytes;
546 snd_pcm_uframes_t ret;
548 spin_lock(&card->dma_lock);
550 bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
551 card->dma_start_vaddr[SND_PS3_CH_L]);
553 spin_unlock(&card->dma_lock);
555 ret = bytes_to_frames(substream->runtime, bytes * 2);
557 return ret;
560 static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream)
562 int ret;
563 ret = snd_pcm_lib_free_pages(substream);
564 return ret;
567 static int snd_ps3_pcm_close(struct snd_pcm_substream *substream)
569 /* mute on */
570 snd_ps3_mute(1);
571 return 0;
574 static void snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
577 * avsetting driver seems to never change the followings
578 * so, init them here once
581 /* no dma interrupt needed */
582 write_reg(PS3_AUDIO_INTR_EN_0, 0);
584 /* use every 4 buffer empty interrupt */
585 update_mask_reg(PS3_AUDIO_AX_IC,
586 PS3_AUDIO_AX_IC_AASOIMD_MASK,
587 PS3_AUDIO_AX_IC_AASOIMD_EVERY4);
589 /* enable 3wire clocks */
590 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
591 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED |
592 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED),
594 update_reg(PS3_AUDIO_AO_3WMCTRL,
595 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
599 * av setting
600 * NOTE: calling this function may generate audio interrupt.
602 static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card)
604 int ret, retries, i;
605 pr_debug("%s: start\n", __func__);
607 ret = ps3av_set_audio_mode(card->avs.avs_audio_ch,
608 card->avs.avs_audio_rate,
609 card->avs.avs_audio_width,
610 card->avs.avs_audio_format,
611 card->avs.avs_audio_source);
613 * Reset the following unwanted settings:
616 /* disable all 3wire buffers */
617 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
618 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
619 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
620 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
621 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
623 wmb(); /* ensure the hardware sees the change */
624 /* wait for actually stopped */
625 retries = 1000;
626 while ((read_reg(PS3_AUDIO_AO_3WMCTRL) &
627 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
628 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
629 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
630 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
631 --retries) {
632 udelay(1);
635 /* reset buffer pointer */
636 for (i = 0; i < 4; i++) {
637 update_reg(PS3_AUDIO_AO_3WCTRL(i),
638 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET);
639 udelay(10);
641 wmb(); /* ensure the hardware actually start resetting */
643 /* enable 3wire#0 buffer */
644 update_reg(PS3_AUDIO_AO_3WMCTRL, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
647 /* In 24bit mode,ALSA inserts a zero byte at first byte of per sample */
648 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
649 ~PS3_AUDIO_AO_3WCTRL_ASODF,
650 PS3_AUDIO_AO_3WCTRL_ASODF_LSB);
651 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
652 ~PS3_AUDIO_AO_SPDCTRL_SPODF,
653 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB);
654 /* ensure all the setting above is written back to register */
655 wmb();
656 /* avsetting driver altered AX_IE, caller must reset it if you want */
657 pr_debug("%s: end\n", __func__);
658 return ret;
661 static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
663 int ret;
664 pr_debug("%s: start\n", __func__);
665 card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
666 card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
667 card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
668 card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM;
669 card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
671 ret = snd_ps3_change_avsetting(card);
673 snd_ps3_audio_fixup(card);
675 /* to start to generate SPDIF signal, fill data */
676 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
677 snd_ps3_kick_dma(card);
678 pr_debug("%s: end\n", __func__);
679 return ret;
683 * set sampling rate according to the substream
685 static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream)
687 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
688 struct snd_ps3_avsetting_info avs;
690 avs = card->avs;
692 pr_debug("%s: called freq=%d width=%d\n", __func__,
693 substream->runtime->rate,
694 snd_pcm_format_width(substream->runtime->format));
696 pr_debug("%s: before freq=%d width=%d\n", __func__,
697 card->avs.avs_audio_rate, card->avs.avs_audio_width);
699 /* sample rate */
700 switch (substream->runtime->rate) {
701 case 44100:
702 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_44K;
703 break;
704 case 48000:
705 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
706 break;
707 case 88200:
708 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_88K;
709 break;
710 case 96000:
711 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_96K;
712 break;
713 default:
714 pr_info("%s: invalid rate %d\n", __func__,
715 substream->runtime->rate);
716 return 1;
719 /* width */
720 switch (snd_pcm_format_width(substream->runtime->format)) {
721 case 16:
722 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
723 break;
724 case 24:
725 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_24;
726 break;
727 default:
728 pr_info("%s: invalid width %d\n", __func__,
729 snd_pcm_format_width(substream->runtime->format));
730 return 1;
733 if ((card->avs.avs_audio_width != avs.avs_audio_width) ||
734 (card->avs.avs_audio_rate != avs.avs_audio_rate)) {
735 card->avs = avs;
736 snd_ps3_change_avsetting(card);
738 pr_debug("%s: after freq=%d width=%d\n", __func__,
739 card->avs.avs_audio_rate, card->avs.avs_audio_width);
741 return 0;
742 } else
743 return 1;
748 static int snd_ps3_map_mmio(void)
750 the_card.mapped_mmio_vaddr =
751 ioremap(the_card.ps3_dev->m_region->bus_addr,
752 the_card.ps3_dev->m_region->len);
754 if (!the_card.mapped_mmio_vaddr) {
755 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
756 __func__, the_card.ps3_dev->m_region->lpar_addr,
757 the_card.ps3_dev->m_region->len);
758 return -ENXIO;
761 return 0;
764 static void snd_ps3_unmap_mmio(void)
766 iounmap(the_card.mapped_mmio_vaddr);
767 the_card.mapped_mmio_vaddr = NULL;
770 static int snd_ps3_allocate_irq(void)
772 int ret;
773 u64 lpar_addr, lpar_size;
774 u64 __iomem *mapped;
776 /* FIXME: move this to device_init (H/W probe) */
778 /* get irq outlet */
779 ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size);
780 if (ret) {
781 pr_info("%s: device map 1 failed %d\n", __func__,
782 ret);
783 return -ENXIO;
786 mapped = ioremap(lpar_addr, lpar_size);
787 if (!mapped) {
788 pr_info("%s: ioremap 1 failed \n", __func__);
789 return -ENXIO;
792 the_card.audio_irq_outlet = in_be64(mapped);
794 iounmap(mapped);
795 ret = lv1_gpu_device_unmap(1);
796 if (ret)
797 pr_info("%s: unmap 1 failed\n", __func__);
799 /* irq */
800 ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY,
801 the_card.audio_irq_outlet,
802 &the_card.irq_no);
803 if (ret) {
804 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret);
805 return ret;
808 ret = request_irq(the_card.irq_no, snd_ps3_interrupt, IRQF_DISABLED,
809 SND_PS3_DRIVER_NAME, &the_card);
810 if (ret) {
811 pr_info("%s: request_irq failed (%d)\n", __func__, ret);
812 goto cleanup_irq;
815 return 0;
817 cleanup_irq:
818 ps3_irq_plug_destroy(the_card.irq_no);
819 return ret;
822 static void snd_ps3_free_irq(void)
824 free_irq(the_card.irq_no, &the_card);
825 ps3_irq_plug_destroy(the_card.irq_no);
828 static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
830 uint64_t val;
831 int ret;
833 val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
834 (0x03UL << 24) |
835 (0x0fUL << 12) |
836 (PS3_AUDIO_IOID);
838 ret = lv1_gpu_attribute(0x100, 0x007, val, 0, 0);
839 if (ret)
840 pr_info("%s: gpu_attribute failed %d\n", __func__,
841 ret);
844 static int __init snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
846 int ret;
847 u64 lpar_addr, lpar_size;
849 BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1));
850 BUG_ON(dev->match_id != PS3_MATCH_ID_SOUND);
852 the_card.ps3_dev = dev;
854 ret = ps3_open_hv_device(dev);
856 if (ret)
857 return -ENXIO;
859 /* setup MMIO */
860 ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size);
861 if (ret) {
862 pr_info("%s: device map 2 failed %d\n", __func__, ret);
863 goto clean_open;
865 ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
866 PAGE_SHIFT);
868 ret = snd_ps3_map_mmio();
869 if (ret)
870 goto clean_dev_map;
872 /* setup DMA area */
873 ps3_dma_region_init(dev, dev->d_region,
874 PAGE_SHIFT, /* use system page size */
875 0, /* dma type; not used */
876 NULL,
877 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE));
878 dev->d_region->ioid = PS3_AUDIO_IOID;
880 ret = ps3_dma_region_create(dev->d_region);
881 if (ret) {
882 pr_info("%s: region_create\n", __func__);
883 goto clean_mmio;
886 snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
888 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
889 the_card.start_delay = snd_ps3_start_delay;
891 /* irq */
892 if (snd_ps3_allocate_irq()) {
893 ret = -ENXIO;
894 goto clean_dma_region;
897 /* create card instance */
898 the_card.card = snd_card_new(index, id, THIS_MODULE, 0);
899 if (!the_card.card) {
900 ret = -ENXIO;
901 goto clean_irq;
904 strcpy(the_card.card->driver, "PS3");
905 strcpy(the_card.card->shortname, "PS3");
906 strcpy(the_card.card->longname, "PS3 sound");
907 /* create PCM devices instance */
908 /* NOTE:this driver works assuming pcm:substream = 1:1 */
909 ret = snd_pcm_new(the_card.card,
910 "SPDIF",
911 0, /* instance index, will be stored pcm.device*/
912 1, /* output substream */
913 0, /* input substream */
914 &(the_card.pcm));
915 if (ret)
916 goto clean_card;
918 the_card.pcm->private_data = &the_card;
919 strcpy(the_card.pcm->name, "SPDIF");
921 /* set pcm ops */
922 snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK,
923 &snd_ps3_pcm_spdif_ops);
925 the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED;
926 /* pre-alloc PCM DMA buffer*/
927 ret = snd_pcm_lib_preallocate_pages_for_all(the_card.pcm,
928 SNDRV_DMA_TYPE_DEV,
929 &dev->core,
930 SND_PS3_PCM_PREALLOC_SIZE,
931 SND_PS3_PCM_PREALLOC_SIZE);
932 if (ret < 0) {
933 pr_info("%s: prealloc failed\n", __func__);
934 goto clean_card;
938 * allocate null buffer
939 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
940 * PAGE_SIZE is enogh
942 if (!(the_card.null_buffer_start_vaddr =
943 dma_alloc_coherent(&the_card.ps3_dev->core,
944 PAGE_SIZE,
945 &the_card.null_buffer_start_dma_addr,
946 GFP_KERNEL))) {
947 pr_info("%s: nullbuffer alloc failed\n", __func__);
948 goto clean_preallocate;
950 pr_debug("%s: null vaddr=%p dma=%#lx\n", __func__,
951 the_card.null_buffer_start_vaddr,
952 the_card.null_buffer_start_dma_addr);
953 /* set default sample rate/word width */
954 snd_ps3_init_avsetting(&the_card);
956 /* register the card */
957 ret = snd_card_register(the_card.card);
958 if (ret < 0)
959 goto clean_dma_map;
961 pr_info("%s started. start_delay=%dms\n",
962 the_card.card->longname, the_card.start_delay);
963 return 0;
965 clean_dma_map:
966 dma_free_coherent(&the_card.ps3_dev->core,
967 PAGE_SIZE,
968 the_card.null_buffer_start_vaddr,
969 the_card.null_buffer_start_dma_addr);
970 clean_preallocate:
971 snd_pcm_lib_preallocate_free_for_all(the_card.pcm);
972 clean_card:
973 snd_card_free(the_card.card);
974 clean_irq:
975 snd_ps3_free_irq();
976 clean_dma_region:
977 ps3_dma_region_free(dev->d_region);
978 clean_mmio:
979 snd_ps3_unmap_mmio();
980 clean_dev_map:
981 lv1_gpu_device_unmap(2);
982 clean_open:
983 ps3_close_hv_device(dev);
985 * there is no destructor function to pcm.
986 * midlayer automatically releases if the card removed
988 return ret;
989 }; /* snd_ps3_probe */
991 /* called when module removal */
992 static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev)
994 int ret;
995 pr_info("%s:start id=%d\n", __func__, dev->match_id);
996 if (dev->match_id != PS3_MATCH_ID_SOUND)
997 return -ENXIO;
1000 * ctl and preallocate buffer will be freed in
1001 * snd_card_free
1003 ret = snd_card_free(the_card.card);
1004 if (ret)
1005 pr_info("%s: ctl freecard=%d\n", __func__, ret);
1007 dma_free_coherent(&dev->core,
1008 PAGE_SIZE,
1009 the_card.null_buffer_start_vaddr,
1010 the_card.null_buffer_start_dma_addr);
1012 ps3_dma_region_free(dev->d_region);
1014 snd_ps3_free_irq();
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);
1020 return 0;
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,
1028 .core = {
1029 .name = SND_PS3_DRIVER_NAME,
1030 .owner = THIS_MODULE,
1036 * Interrupt handler
1038 static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id)
1041 uint32_t port_intr;
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);
1048 return IRQ_HANDLED;
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;
1062 if (card->silent) {
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);
1069 card->silent --;
1070 } else {
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
1085 * empty interrupts.
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 */
1097 return IRQ_HANDLED;
1101 * module/subsystem initialize/terminate
1103 static int __init snd_ps3_init(void)
1105 int ret;
1107 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1108 return -ENXIO;
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);
1116 return ret;
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);