blob: af812dc69ece990ce032800b396b2476f6d80a4c [file] [log] [blame]
Masakazu Mokunoc454fd42007-07-03 12:54:49 +02001/*
2 * Audio support for PS3
3 * Copyright (C) 2007 Sony Computer Entertainment Inc.
4 * All rights reserved.
5 * Copyright 2006, 2007 Sony Corporation
6 *
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.
10 *
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.
15 *
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
19 */
20
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>
Masakazu Mokunoc454fd42007-07-03 12:54:49 +020036#include <asm/dma.h>
37#include <asm/lv1call.h>
38#include <asm/ps3.h>
39#include <asm/ps3av.h>
40
41#include "snd_ps3_reg.h"
42#include "snd_ps3.h"
43
44MODULE_LICENSE("GPL v2");
45MODULE_DESCRIPTION("PS3 sound driver");
46MODULE_AUTHOR("Sony Computer Entertainment Inc.");
47
48/* module entries */
49static int __init snd_ps3_init(void);
50static void __exit snd_ps3_exit(void);
51
52/* ALSA snd driver ops */
53static int snd_ps3_pcm_open(struct snd_pcm_substream *substream);
54static int snd_ps3_pcm_close(struct snd_pcm_substream *substream);
55static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream);
56static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
57 int cmd);
58static snd_pcm_uframes_t snd_ps3_pcm_pointer(struct snd_pcm_substream
59 *substream);
60static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
61 struct snd_pcm_hw_params *hw_params);
62static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream);
63
64
65/* ps3_system_bus_driver entries */
66static int __init snd_ps3_driver_probe(struct ps3_system_bus_device *dev);
67static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev);
68
69/* address setup */
70static int snd_ps3_map_mmio(void);
71static void snd_ps3_unmap_mmio(void);
72static int snd_ps3_allocate_irq(void);
73static void snd_ps3_free_irq(void);
74static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start);
75
76/* interrupt handler */
77static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id);
78
79
80/* set sampling rate/format */
81static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream);
82/* take effect parameter change */
83static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card);
84/* initialize avsetting and take it effect */
85static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card);
86/* setup dma */
87static int snd_ps3_program_dma(struct snd_ps3_card_info *card,
88 enum snd_ps3_dma_filltype filltype);
89static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card);
90
91static dma_addr_t v_to_bus(struct snd_ps3_card_info *, void *vaddr, int ch);
92
93
94module_init(snd_ps3_init);
95module_exit(snd_ps3_exit);
96
97/*
98 * global
99 */
100static struct snd_ps3_card_info the_card;
101
102static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY;
103
104module_param_named(start_delay, snd_ps3_start_delay, uint, 0644);
105MODULE_PARM_DESC(start_delay, "time to insert silent data in milisec");
106
107static int index = SNDRV_DEFAULT_IDX1;
108static char *id = SNDRV_DEFAULT_STR1;
109
110module_param(index, int, 0444);
111MODULE_PARM_DESC(index, "Index value for PS3 soundchip.");
112module_param(id, charp, 0444);
113MODULE_PARM_DESC(id, "ID string for PS3 soundchip.");
114
115
116/*
117 * PS3 audio register access
118 */
119static inline u32 read_reg(unsigned int reg)
120{
121 return in_be32(the_card.mapped_mmio_vaddr + reg);
122}
123static inline void write_reg(unsigned int reg, u32 val)
124{
125 out_be32(the_card.mapped_mmio_vaddr + reg, val);
126}
127static inline void update_reg(unsigned int reg, u32 or_val)
128{
129 u32 newval = read_reg(reg) | or_val;
130 write_reg(reg, newval);
131}
132static inline void update_mask_reg(unsigned int reg, u32 mask, u32 or_val)
133{
134 u32 newval = (read_reg(reg) & mask) | or_val;
135 write_reg(reg, newval);
136}
137
138/*
139 * ALSA defs
140 */
141const 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,
153
154 .channels_min = 2, /* stereo only */
155 .channels_max = 2,
156
157 .buffer_bytes_max = PS3_AUDIO_FIFO_SIZE * 64,
158
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,
162
163 .periods_min = 16,
164 .periods_max = 32, /* buffer_size_max/ period_bytes_max */
165
166 .fifo_size = PS3_AUDIO_FIFO_SIZE
167};
168
169static struct snd_pcm_ops snd_ps3_pcm_spdif_ops =
170{
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
179};
180
181static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info *card,
182 int count, int force_stop)
183{
184 int dma_ch, done, retries, stop_forced = 0;
185 uint32_t status;
186
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);
202 }
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
209 */
210 update_reg(PS3_AUDIO_CONFIG, PS3_AUDIO_CONFIG_CLEAR);
211 stop_forced = 1;
212 }
213 }
214 return stop_forced;
215}
216
217/*
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.
222 */
223static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card)
224{
225 int stop_forced;
226 /*
227 * wait for the last dma is done
228 */
229
230 /*
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.
234 */
235 stop_forced = snd_ps3_verify_dma_stop(card, 700, 1);
236
237 /*
238 * clear outstanding interrupts.
239 */
240 update_reg(PS3_AUDIO_INTR_0, 0);
241 update_reg(PS3_AUDIO_AX_IS, 0);
242
243 /*
244 *revert CLEAR bit since it will not reset automatically after DMA stop
245 */
246 if (stop_forced)
247 update_mask_reg(PS3_AUDIO_CONFIG, ~PS3_AUDIO_CONFIG_CLEAR, 0);
248 /* ensure the hardware sees changes */
249 wmb();
250}
251
252static void snd_ps3_kick_dma(struct snd_ps3_card_info *card)
253{
254
255 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST);
256 /* ensure the hardware sees the change */
257 wmb();
258}
259
260/*
261 * convert virtual addr to ioif bus addr.
262 */
263static dma_addr_t v_to_bus(struct snd_ps3_card_info *card,
264 void * paddr,
265 int ch)
266{
267 return card->dma_start_bus_addr[ch] +
268 (paddr - card->dma_start_vaddr[ch]);
269};
270
271
272/*
273 * increment ring buffer pointer.
274 * NOTE: caller must hold write spinlock
275 */
276static void snd_ps3_bump_buffer(struct snd_ps3_card_info *card,
277 enum snd_ps3_ch ch, size_t byte_count,
278 int stage)
279{
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];
287 }
288}
289/*
290 * setup dmac to send data to audio and attenuate samples on the ring buffer
291 */
292static int snd_ps3_program_dma(struct snd_ps3_card_info *card,
293 enum snd_ps3_dma_filltype filltype)
294{
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;
303
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;
311
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;
318 }
319
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);
334
335 write_reg(PS3_AUDIO_SOURCE(dma_ch),
336 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY |
337 dma_addr));
338
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)));
348
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);
356
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);
366 }
367 }
368 /* ensure the hardware sees the change */
369 wmb();
370 spin_unlock_irqrestore(&card->dma_lock, irqsave);
371
372 return 0;
373}
374
375/*
376 * audio mute on/off
377 * mute_on : 0 output enabled
378 * 1 mute
379 */
380static int snd_ps3_mute(int mute_on)
381{
382 return ps3av_audio_mute(mute_on);
383}
384
385/*
386 * PCM operators
387 */
388static int snd_ps3_pcm_open(struct snd_pcm_substream *substream)
389{
390 struct snd_pcm_runtime *runtime = substream->runtime;
391 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
392 int pcm_index;
393
394 pcm_index = substream->pcm->device;
395 /* to retrieve substream/runtime in interrupt handler */
396 card->substream = substream;
397
398 runtime->hw = snd_ps3_pcm_hw;
399
400 card->start_delay = snd_ps3_start_delay;
401
402 /* mute off */
403 snd_ps3_mute(0); /* this function sleep */
404
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;
408};
409
410static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
411 struct snd_pcm_hw_params *hw_params)
412{
413 size_t size;
414
415 /* alloc transport buffer */
416 size = params_buffer_bytes(hw_params);
417 snd_pcm_lib_malloc_pages(substream, size);
418 return 0;
419};
420
421static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
422 unsigned int delay_ms)
423{
424 int ret;
425 int rate ;
426
427 rate = substream->runtime->rate;
428 ret = snd_pcm_format_size(substream->runtime->format,
429 rate * delay_ms / 1000)
430 * substream->runtime->channels;
431
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);
439
440 return ret;
441};
442
443static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
444{
445 struct snd_pcm_runtime *runtime = substream->runtime;
446 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
447 unsigned long irqsave;
448
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));
454 /*
455 * let SPDIF device re-lock with SPDIF signal,
456 * start with some silence
457 */
458 card->silent = snd_ps3_delay_to_bytes(substream,
459 card->start_delay) /
460 (PS3_AUDIO_FIFO_STAGE_SIZE * 4); /* every 4 times */
461 }
462
463 /* restart ring buffer pointer */
464 spin_lock_irqsave(&card->dma_lock, irqsave);
465 {
466 card->dma_buffer_size = runtime->dma_bytes;
467
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;
473
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);
480
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]);
484
485 }
486 spin_unlock_irqrestore(&card->dma_lock, irqsave);
487
488 /* ensure the hardware sees the change */
489 mb();
490
491 return 0;
492};
493
494static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
495 int cmd)
496{
497 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
498 int ret = 0;
499
500 switch (cmd) {
501 case SNDRV_PCM_TRIGGER_START:
502 /* clear outstanding interrupts */
503 update_reg(PS3_AUDIO_AX_IS, 0);
504
505 spin_lock(&card->dma_lock);
506 {
507 card->running = 1;
508 }
509 spin_unlock(&card->dma_lock);
510
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);
517 }
518 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
519 snd_ps3_kick_dma(card);
520 break;
521
522 case SNDRV_PCM_TRIGGER_STOP:
523 spin_lock(&card->dma_lock);
524 {
525 card->running = 0;
526 }
527 spin_unlock(&card->dma_lock);
528 snd_ps3_wait_for_dma_stop(card);
529 break;
530 default:
531 break;
532
533 }
534
535 return ret;
536};
537
538/*
539 * report current pointer
540 */
541static snd_pcm_uframes_t snd_ps3_pcm_pointer(
542 struct snd_pcm_substream *substream)
543{
544 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
545 size_t bytes;
546 snd_pcm_uframes_t ret;
547
548 spin_lock(&card->dma_lock);
549 {
550 bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
551 card->dma_start_vaddr[SND_PS3_CH_L]);
552 }
553 spin_unlock(&card->dma_lock);
554
555 ret = bytes_to_frames(substream->runtime, bytes * 2);
556
557 return ret;
558};
559
560static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream)
561{
562 int ret;
563 ret = snd_pcm_lib_free_pages(substream);
564 return ret;
565};
566
567static int snd_ps3_pcm_close(struct snd_pcm_substream *substream)
568{
569 /* mute on */
570 snd_ps3_mute(1);
571 return 0;
572};
573
574static void snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
575{
576 /*
577 * avsetting driver seems to never change the followings
578 * so, init them here once
579 */
580
581 /* no dma interrupt needed */
582 write_reg(PS3_AUDIO_INTR_EN_0, 0);
583
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);
588
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),
593 0);
594 update_reg(PS3_AUDIO_AO_3WMCTRL,
595 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
596}
597
598/*
599 * av setting
600 * NOTE: calling this function may generate audio interrupt.
601 */
602static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card)
603{
604 int ret, retries, i;
605 pr_debug("%s: start\n", __func__);
606
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);
612 /*
613 * Reset the following unwanted settings:
614 */
615
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)),
622 0);
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);
633 }
634
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);
640 }
641 wmb(); /* ensure the hardware actually start resetting */
642
643 /* enable 3wire#0 buffer */
644 update_reg(PS3_AUDIO_AO_3WMCTRL, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
645
646
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;
659}
660
661static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
662{
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;
670
671 ret = snd_ps3_change_avsetting(card);
672
673 snd_ps3_audio_fixup(card);
674
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;
680}
681
682/*
683 * set sampling rate according to the substream
684 */
685static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream)
686{
687 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
688 struct snd_ps3_avsetting_info avs;
689
690 avs = card->avs;
691
692 pr_debug("%s: called freq=%d width=%d\n", __func__,
693 substream->runtime->rate,
694 snd_pcm_format_width(substream->runtime->format));
695
696 pr_debug("%s: before freq=%d width=%d\n", __func__,
697 card->avs.avs_audio_rate, card->avs.avs_audio_width);
698
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;
717 }
718
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;
731 }
732
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);
737
738 pr_debug("%s: after freq=%d width=%d\n", __func__,
739 card->avs.avs_audio_rate, card->avs.avs_audio_width);
740
741 return 0;
742 } else
743 return 1;
744}
745
746
747
748static int snd_ps3_map_mmio(void)
749{
750 the_card.mapped_mmio_vaddr =
751 ioremap(the_card.ps3_dev->m_region->bus_addr,
752 the_card.ps3_dev->m_region->len);
753
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;
759 }
760
761 return 0;
762};
763
764static void snd_ps3_unmap_mmio(void)
765{
766 iounmap(the_card.mapped_mmio_vaddr);
767 the_card.mapped_mmio_vaddr = NULL;
768}
769
770static int snd_ps3_allocate_irq(void)
771{
772 int ret;
773 u64 lpar_addr, lpar_size;
774 u64 __iomem *mapped;
775
776 /* FIXME: move this to device_init (H/W probe) */
777
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;
784 }
785
786 mapped = ioremap(lpar_addr, lpar_size);
787 if (!mapped) {
788 pr_info("%s: ioremap 1 failed \n", __func__);
789 return -ENXIO;
790 }
791
792 the_card.audio_irq_outlet = in_be64(mapped);
793
794 iounmap(mapped);
795 ret = lv1_gpu_device_unmap(1);
796 if (ret)
797 pr_info("%s: unmap 1 failed\n", __func__);
798
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;
806 }
807
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;
813 }
814
815 return 0;
816
817 cleanup_irq:
818 ps3_irq_plug_destroy(the_card.irq_no);
819 return ret;
820};
821
822static void snd_ps3_free_irq(void)
823{
824 free_irq(the_card.irq_no, &the_card);
825 ps3_irq_plug_destroy(the_card.irq_no);
826}
827
828static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
829{
830 uint64_t val;
831 int ret;
832
833 val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
834 (0x03UL << 24) |
835 (0x0fUL << 12) |
836 (PS3_AUDIO_IOID);
837
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);
842}
843
844static int __init snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
845{
846 int ret;
847 u64 lpar_addr, lpar_size;
848
849 BUG_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1));
850 BUG_ON(dev->match_id != PS3_MATCH_ID_SOUND);
851
852 the_card.ps3_dev = dev;
853
854 ret = ps3_open_hv_device(dev);
855
856 if (ret)
857 return -ENXIO;
858
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;
864 }
865 ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
866 PAGE_SHIFT);
867
868 ret = snd_ps3_map_mmio();
869 if (ret)
870 goto clean_dev_map;
871
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;
879
880 ret = ps3_dma_region_create(dev->d_region);
881 if (ret) {
882 pr_info("%s: region_create\n", __func__);
883 goto clean_mmio;
884 }
885
886 snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
887
888 /* CONFIG_SND_PS3_DEFAULT_START_DELAY */
889 the_card.start_delay = snd_ps3_start_delay;
890
891 /* irq */
892 if (snd_ps3_allocate_irq()) {
893 ret = -ENXIO;
894 goto clean_dma_region;
895 }
896
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;
902 }
903
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;
917
918 the_card.pcm->private_data = &the_card;
919 strcpy(the_card.pcm->name, "SPDIF");
920
921 /* set pcm ops */
922 snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK,
923 &snd_ps3_pcm_spdif_ops);
924
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;
935 }
936
937 /*
938 * allocate null buffer
939 * its size should be lager than PS3_AUDIO_FIFO_STAGE_SIZE * 2
940 * PAGE_SIZE is enogh
941 */
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;
949 }
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);
955
956 /* register the card */
Takashi Iwaif78dfac2007-12-17 16:24:04 +0100957 snd_card_set_dev(the_card.card, &dev->core);
Masakazu Mokunoc454fd42007-07-03 12:54:49 +0200958 ret = snd_card_register(the_card.card);
959 if (ret < 0)
960 goto clean_dma_map;
961
962 pr_info("%s started. start_delay=%dms\n",
963 the_card.card->longname, the_card.start_delay);
964 return 0;
965
966clean_dma_map:
967 dma_free_coherent(&the_card.ps3_dev->core,
968 PAGE_SIZE,
969 the_card.null_buffer_start_vaddr,
970 the_card.null_buffer_start_dma_addr);
971clean_preallocate:
972 snd_pcm_lib_preallocate_free_for_all(the_card.pcm);
973clean_card:
974 snd_card_free(the_card.card);
975clean_irq:
976 snd_ps3_free_irq();
977clean_dma_region:
978 ps3_dma_region_free(dev->d_region);
979clean_mmio:
980 snd_ps3_unmap_mmio();
981clean_dev_map:
982 lv1_gpu_device_unmap(2);
983clean_open:
984 ps3_close_hv_device(dev);
985 /*
986 * there is no destructor function to pcm.
987 * midlayer automatically releases if the card removed
988 */
989 return ret;
990}; /* snd_ps3_probe */
991
992/* called when module removal */
993static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev)
994{
995 int ret;
996 pr_info("%s:start id=%d\n", __func__, dev->match_id);
997 if (dev->match_id != PS3_MATCH_ID_SOUND)
998 return -ENXIO;
999
1000 /*
1001 * ctl and preallocate buffer will be freed in
1002 * snd_card_free
1003 */
1004 ret = snd_card_free(the_card.card);
1005 if (ret)
1006 pr_info("%s: ctl freecard=%d\n", __func__, ret);
1007
1008 dma_free_coherent(&dev->core,
1009 PAGE_SIZE,
1010 the_card.null_buffer_start_vaddr,
1011 the_card.null_buffer_start_dma_addr);
1012
1013 ps3_dma_region_free(dev->d_region);
1014
1015 snd_ps3_free_irq();
1016 snd_ps3_unmap_mmio();
1017
1018 lv1_gpu_device_unmap(2);
1019 ps3_close_hv_device(dev);
1020 pr_info("%s:end id=%d\n", __func__, dev->match_id);
1021 return 0;
1022} /* snd_ps3_remove */
1023
1024static struct ps3_system_bus_driver snd_ps3_bus_driver_info = {
1025 .match_id = PS3_MATCH_ID_SOUND,
1026 .probe = snd_ps3_driver_probe,
1027 .remove = snd_ps3_driver_remove,
1028 .shutdown = snd_ps3_driver_remove,
1029 .core = {
1030 .name = SND_PS3_DRIVER_NAME,
1031 .owner = THIS_MODULE,
1032 },
1033};
1034
1035
1036/*
1037 * Interrupt handler
1038 */
1039static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id)
1040{
1041
1042 uint32_t port_intr;
1043 int underflow_occured = 0;
1044 struct snd_ps3_card_info *card = dev_id;
1045
1046 if (!card->running) {
1047 update_reg(PS3_AUDIO_AX_IS, 0);
1048 update_reg(PS3_AUDIO_INTR_0, 0);
1049 return IRQ_HANDLED;
1050 }
1051
1052 port_intr = read_reg(PS3_AUDIO_AX_IS);
1053 /*
1054 *serial buffer empty detected (every 4 times),
1055 *program next dma and kick it
1056 */
1057 if (port_intr & PS3_AUDIO_AX_IE_ASOBEIE(0)) {
1058 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBEIE(0));
1059 if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
1060 write_reg(PS3_AUDIO_AX_IS, port_intr);
1061 underflow_occured = 1;
1062 }
1063 if (card->silent) {
1064 /* we are still in silent time */
1065 snd_ps3_program_dma(card,
1066 (underflow_occured) ?
1067 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL :
1068 SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
1069 snd_ps3_kick_dma(card);
1070 card->silent --;
1071 } else {
1072 snd_ps3_program_dma(card,
1073 (underflow_occured) ?
1074 SND_PS3_DMA_FILLTYPE_FIRSTFILL :
1075 SND_PS3_DMA_FILLTYPE_RUNNING);
1076 snd_ps3_kick_dma(card);
1077 snd_pcm_period_elapsed(card->substream);
1078 }
1079 } else if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
1080 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBUIE(0));
1081 /*
1082 * serial out underflow, but buffer empty not detected.
1083 * in this case, fill fifo with 0 to recover. After
1084 * filling dummy data, serial automatically start to
1085 * consume them and then will generate normal buffer
1086 * empty interrupts.
1087 * If both buffer underflow and buffer empty are occured,
1088 * it is better to do nomal data transfer than empty one
1089 */
1090 snd_ps3_program_dma(card,
1091 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
1092 snd_ps3_kick_dma(card);
1093 snd_ps3_program_dma(card,
1094 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
1095 snd_ps3_kick_dma(card);
1096 }
1097 /* clear interrupt cause */
1098 return IRQ_HANDLED;
1099};
1100
1101/*
1102 * module/subsystem initialize/terminate
1103 */
1104static int __init snd_ps3_init(void)
1105{
1106 int ret;
1107
1108 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1109 return -ENXIO;
1110
1111 memset(&the_card, 0, sizeof(the_card));
1112 spin_lock_init(&the_card.dma_lock);
1113
1114 /* register systembus DRIVER, this calls our probe() func */
1115 ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info);
1116
1117 return ret;
1118}
1119
1120static void __exit snd_ps3_exit(void)
1121{
1122 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info);
1123}
1124
1125MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND);