1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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>
27#include <linux/module.h>
28
29#include <sound/asound.h>
30#include <sound/control.h>
31#include <sound/core.h>
32#include <sound/initval.h>
33#include <sound/memalloc.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36
37#include <asm/dma.h>
38#include <asm/firmware.h>
39#include <asm/lv1call.h>
40#include <asm/ps3.h>
41#include <asm/ps3av.h>
42
43#include "snd_ps3.h"
44#include "snd_ps3_reg.h"
45
46
47
48
49
50static struct snd_ps3_card_info the_card;
51
52static int snd_ps3_start_delay = CONFIG_SND_PS3_DEFAULT_START_DELAY;
53
54module_param_named(start_delay, snd_ps3_start_delay, uint, 0644);
55MODULE_PARM_DESC(start_delay, "time to insert silent data in ms");
56
57static int index = SNDRV_DEFAULT_IDX1;
58static char *id = SNDRV_DEFAULT_STR1;
59
60module_param(index, int, 0444);
61MODULE_PARM_DESC(index, "Index value for PS3 soundchip.");
62module_param(id, charp, 0444);
63MODULE_PARM_DESC(id, "ID string for PS3 soundchip.");
64
65
66
67
68
69static inline u32 read_reg(unsigned int reg)
70{
71 return in_be32(the_card.mapped_mmio_vaddr + reg);
72}
73static inline void write_reg(unsigned int reg, u32 val)
74{
75 out_be32(the_card.mapped_mmio_vaddr + reg, val);
76}
77static inline void update_reg(unsigned int reg, u32 or_val)
78{
79 u32 newval = read_reg(reg) | or_val;
80 write_reg(reg, newval);
81}
82static inline void update_mask_reg(unsigned int reg, u32 mask, u32 or_val)
83{
84 u32 newval = (read_reg(reg) & mask) | or_val;
85 write_reg(reg, newval);
86}
87
88
89
90
91static const struct snd_pcm_hardware snd_ps3_pcm_hw = {
92 .info = (SNDRV_PCM_INFO_MMAP |
93 SNDRV_PCM_INFO_NONINTERLEAVED |
94 SNDRV_PCM_INFO_MMAP_VALID),
95 .formats = (SNDRV_PCM_FMTBIT_S16_BE |
96 SNDRV_PCM_FMTBIT_S24_BE),
97 .rates = (SNDRV_PCM_RATE_44100 |
98 SNDRV_PCM_RATE_48000 |
99 SNDRV_PCM_RATE_88200 |
100 SNDRV_PCM_RATE_96000),
101 .rate_min = 44100,
102 .rate_max = 96000,
103
104 .channels_min = 2,
105 .channels_max = 2,
106
107 .buffer_bytes_max = PS3_AUDIO_FIFO_SIZE * 64,
108
109
110 .period_bytes_min = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
111 .period_bytes_max = PS3_AUDIO_FIFO_STAGE_SIZE * 4,
112
113 .periods_min = 16,
114 .periods_max = 32,
115
116 .fifo_size = PS3_AUDIO_FIFO_SIZE
117};
118
119static int snd_ps3_verify_dma_stop(struct snd_ps3_card_info *card,
120 int count, int force_stop)
121{
122 int dma_ch, done, retries, stop_forced = 0;
123 uint32_t status;
124
125 for (dma_ch = 0; dma_ch < 8; dma_ch++) {
126 retries = count;
127 do {
128 status = read_reg(PS3_AUDIO_KICK(dma_ch)) &
129 PS3_AUDIO_KICK_STATUS_MASK;
130 switch (status) {
131 case PS3_AUDIO_KICK_STATUS_DONE:
132 case PS3_AUDIO_KICK_STATUS_NOTIFY:
133 case PS3_AUDIO_KICK_STATUS_CLEAR:
134 case PS3_AUDIO_KICK_STATUS_ERROR:
135 done = 1;
136 break;
137 default:
138 done = 0;
139 udelay(10);
140 }
141 } while (!done && --retries);
142 if (!retries && force_stop) {
143 pr_info("%s: DMA ch %d is not stopped.",
144 __func__, dma_ch);
145
146
147
148 update_reg(PS3_AUDIO_CONFIG, PS3_AUDIO_CONFIG_CLEAR);
149 stop_forced = 1;
150 }
151 }
152 return stop_forced;
153}
154
155
156
157
158
159
160
161static void snd_ps3_wait_for_dma_stop(struct snd_ps3_card_info *card)
162{
163 int stop_forced;
164
165
166
167
168
169
170
171
172
173 stop_forced = snd_ps3_verify_dma_stop(card, 700, 1);
174
175
176
177
178 update_reg(PS3_AUDIO_INTR_0, 0);
179 update_reg(PS3_AUDIO_AX_IS, 0);
180
181
182
183
184 if (stop_forced)
185 update_mask_reg(PS3_AUDIO_CONFIG, ~PS3_AUDIO_CONFIG_CLEAR, 0);
186
187 wmb();
188}
189
190static void snd_ps3_kick_dma(struct snd_ps3_card_info *card)
191{
192
193 update_reg(PS3_AUDIO_KICK(0), PS3_AUDIO_KICK_REQUEST);
194
195 wmb();
196}
197
198
199
200
201static dma_addr_t v_to_bus(struct snd_ps3_card_info *card, void *paddr, int ch)
202{
203 return card->dma_start_bus_addr[ch] +
204 (paddr - card->dma_start_vaddr[ch]);
205};
206
207
208
209
210
211
212static void snd_ps3_bump_buffer(struct snd_ps3_card_info *card,
213 enum snd_ps3_ch ch, size_t byte_count,
214 int stage)
215{
216 if (!stage)
217 card->dma_last_transfer_vaddr[ch] =
218 card->dma_next_transfer_vaddr[ch];
219 card->dma_next_transfer_vaddr[ch] += byte_count;
220 if ((card->dma_start_vaddr[ch] + (card->dma_buffer_size / 2)) <=
221 card->dma_next_transfer_vaddr[ch]) {
222 card->dma_next_transfer_vaddr[ch] = card->dma_start_vaddr[ch];
223 }
224}
225
226
227
228static int snd_ps3_program_dma(struct snd_ps3_card_info *card,
229 enum snd_ps3_dma_filltype filltype)
230{
231
232 uint32_t dma_addr;
233 int fill_stages, dma_ch, stage;
234 enum snd_ps3_ch ch;
235 uint32_t ch0_kick_event = 0;
236 void *start_vaddr;
237 unsigned long irqsave;
238 int silent = 0;
239
240 switch (filltype) {
241 case SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL:
242 silent = 1;
243
244 case SND_PS3_DMA_FILLTYPE_FIRSTFILL:
245 ch0_kick_event = PS3_AUDIO_KICK_EVENT_ALWAYS;
246 break;
247
248 case SND_PS3_DMA_FILLTYPE_SILENT_RUNNING:
249 silent = 1;
250
251 case SND_PS3_DMA_FILLTYPE_RUNNING:
252 ch0_kick_event = PS3_AUDIO_KICK_EVENT_SERIALOUT0_EMPTY;
253 break;
254 }
255
256 snd_ps3_verify_dma_stop(card, 700, 0);
257 fill_stages = 4;
258 spin_lock_irqsave(&card->dma_lock, irqsave);
259 for (ch = 0; ch < 2; ch++) {
260 start_vaddr = card->dma_next_transfer_vaddr[0];
261 for (stage = 0; stage < fill_stages; stage++) {
262 dma_ch = stage * 2 + ch;
263 if (silent)
264 dma_addr = card->null_buffer_start_dma_addr;
265 else
266 dma_addr =
267 v_to_bus(card,
268 card->dma_next_transfer_vaddr[ch],
269 ch);
270
271 write_reg(PS3_AUDIO_SOURCE(dma_ch),
272 (PS3_AUDIO_SOURCE_TARGET_SYSTEM_MEMORY |
273 dma_addr));
274
275
276 if (ch == 0)
277 write_reg(PS3_AUDIO_DEST(dma_ch),
278 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
279 PS3_AUDIO_AO_3W_LDATA(0)));
280 else
281 write_reg(PS3_AUDIO_DEST(dma_ch),
282 (PS3_AUDIO_DEST_TARGET_AUDIOFIFO |
283 PS3_AUDIO_AO_3W_RDATA(0)));
284
285
286 write_reg(PS3_AUDIO_DMASIZE(dma_ch), 0);
287
288 if (!silent)
289 snd_ps3_bump_buffer(card, ch,
290 PS3_AUDIO_DMAC_BLOCK_SIZE,
291 stage);
292
293
294 if (dma_ch == 0)
295 write_reg(PS3_AUDIO_KICK(dma_ch),
296 ch0_kick_event);
297 else
298 write_reg(PS3_AUDIO_KICK(dma_ch),
299 PS3_AUDIO_KICK_EVENT_AUDIO_DMA(dma_ch
300 - 1) |
301 PS3_AUDIO_KICK_REQUEST);
302 }
303 }
304
305 wmb();
306 spin_unlock_irqrestore(&card->dma_lock, irqsave);
307
308 return 0;
309}
310
311
312
313
314static irqreturn_t snd_ps3_interrupt(int irq, void *dev_id)
315{
316
317 uint32_t port_intr;
318 int underflow_occured = 0;
319 struct snd_ps3_card_info *card = dev_id;
320
321 if (!card->running) {
322 update_reg(PS3_AUDIO_AX_IS, 0);
323 update_reg(PS3_AUDIO_INTR_0, 0);
324 return IRQ_HANDLED;
325 }
326
327 port_intr = read_reg(PS3_AUDIO_AX_IS);
328
329
330
331
332 if (port_intr & PS3_AUDIO_AX_IE_ASOBEIE(0)) {
333 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBEIE(0));
334 if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
335 write_reg(PS3_AUDIO_AX_IS, port_intr);
336 underflow_occured = 1;
337 }
338 if (card->silent) {
339
340 snd_ps3_program_dma(card,
341 (underflow_occured) ?
342 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL :
343 SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
344 snd_ps3_kick_dma(card);
345 card->silent--;
346 } else {
347 snd_ps3_program_dma(card,
348 (underflow_occured) ?
349 SND_PS3_DMA_FILLTYPE_FIRSTFILL :
350 SND_PS3_DMA_FILLTYPE_RUNNING);
351 snd_ps3_kick_dma(card);
352 snd_pcm_period_elapsed(card->substream);
353 }
354 } else if (port_intr & PS3_AUDIO_AX_IE_ASOBUIE(0)) {
355 write_reg(PS3_AUDIO_AX_IS, PS3_AUDIO_AX_IE_ASOBUIE(0));
356
357
358
359
360
361
362
363
364
365 snd_ps3_program_dma(card,
366 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
367 snd_ps3_kick_dma(card);
368 snd_ps3_program_dma(card,
369 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
370 snd_ps3_kick_dma(card);
371 }
372
373 return IRQ_HANDLED;
374};
375
376
377
378
379
380
381static int snd_ps3_mute(int mute_on)
382{
383 return ps3av_audio_mute(mute_on);
384}
385
386
387
388
389
390static int snd_ps3_change_avsetting(struct snd_ps3_card_info *card)
391{
392 int ret, retries, i;
393 pr_debug("%s: start\n", __func__);
394
395 ret = ps3av_set_audio_mode(card->avs.avs_audio_ch,
396 card->avs.avs_audio_rate,
397 card->avs.avs_audio_width,
398 card->avs.avs_audio_format,
399 card->avs.avs_audio_source);
400
401
402
403
404
405 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
406 ~(PS3_AUDIO_AO_3WMCTRL_ASOEN(0) |
407 PS3_AUDIO_AO_3WMCTRL_ASOEN(1) |
408 PS3_AUDIO_AO_3WMCTRL_ASOEN(2) |
409 PS3_AUDIO_AO_3WMCTRL_ASOEN(3)),
410 0);
411 wmb();
412
413 retries = 1000;
414 while ((read_reg(PS3_AUDIO_AO_3WMCTRL) &
415 (PS3_AUDIO_AO_3WMCTRL_ASORUN(0) |
416 PS3_AUDIO_AO_3WMCTRL_ASORUN(1) |
417 PS3_AUDIO_AO_3WMCTRL_ASORUN(2) |
418 PS3_AUDIO_AO_3WMCTRL_ASORUN(3))) &&
419 --retries) {
420 udelay(1);
421 }
422
423
424 for (i = 0; i < 4; i++) {
425 update_reg(PS3_AUDIO_AO_3WCTRL(i),
426 PS3_AUDIO_AO_3WCTRL_ASOBRST_RESET);
427 udelay(10);
428 }
429 wmb();
430
431
432 update_reg(PS3_AUDIO_AO_3WMCTRL, PS3_AUDIO_AO_3WMCTRL_ASOEN(0));
433
434
435
436 update_mask_reg(PS3_AUDIO_AO_3WCTRL(0),
437 ~PS3_AUDIO_AO_3WCTRL_ASODF,
438 PS3_AUDIO_AO_3WCTRL_ASODF_LSB);
439 update_mask_reg(PS3_AUDIO_AO_SPDCTRL(0),
440 ~PS3_AUDIO_AO_SPDCTRL_SPODF,
441 PS3_AUDIO_AO_SPDCTRL_SPODF_LSB);
442
443 wmb();
444
445 pr_debug("%s: end\n", __func__);
446 return ret;
447}
448
449
450
451
452static int snd_ps3_set_avsetting(struct snd_pcm_substream *substream)
453{
454 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
455 struct snd_ps3_avsetting_info avs;
456 int ret;
457
458 avs = card->avs;
459
460 pr_debug("%s: called freq=%d width=%d\n", __func__,
461 substream->runtime->rate,
462 snd_pcm_format_width(substream->runtime->format));
463
464 pr_debug("%s: before freq=%d width=%d\n", __func__,
465 card->avs.avs_audio_rate, card->avs.avs_audio_width);
466
467
468 switch (substream->runtime->rate) {
469 case 44100:
470 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_44K;
471 break;
472 case 48000:
473 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
474 break;
475 case 88200:
476 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_88K;
477 break;
478 case 96000:
479 avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_96K;
480 break;
481 default:
482 pr_info("%s: invalid rate %d\n", __func__,
483 substream->runtime->rate);
484 return 1;
485 }
486
487
488 switch (snd_pcm_format_width(substream->runtime->format)) {
489 case 16:
490 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
491 break;
492 case 24:
493 avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_24;
494 break;
495 default:
496 pr_info("%s: invalid width %d\n", __func__,
497 snd_pcm_format_width(substream->runtime->format));
498 return 1;
499 }
500
501 memcpy(avs.avs_cs_info, ps3av_mode_cs_info, 8);
502
503 if (memcmp(&card->avs, &avs, sizeof(avs))) {
504 pr_debug("%s: after freq=%d width=%d\n", __func__,
505 card->avs.avs_audio_rate, card->avs.avs_audio_width);
506
507 card->avs = avs;
508 snd_ps3_change_avsetting(card);
509 ret = 0;
510 } else
511 ret = 1;
512
513
514 if (avs.avs_cs_info[0] & 0x02)
515 ps3av_audio_mute_analog(1);
516 else
517 ps3av_audio_mute_analog(0);
518
519 return ret;
520}
521
522
523
524
525static int snd_ps3_pcm_open(struct snd_pcm_substream *substream)
526{
527 struct snd_pcm_runtime *runtime = substream->runtime;
528 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
529 int pcm_index;
530
531 pcm_index = substream->pcm->device;
532
533 card->substream = substream;
534
535 runtime->hw = snd_ps3_pcm_hw;
536
537 card->start_delay = snd_ps3_start_delay;
538
539
540 snd_ps3_mute(0);
541
542 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
543 PS3_AUDIO_FIFO_STAGE_SIZE * 4 * 2);
544 return 0;
545};
546
547static int snd_ps3_pcm_close(struct snd_pcm_substream *substream)
548{
549
550 snd_ps3_mute(1);
551 return 0;
552};
553
554static int snd_ps3_pcm_hw_params(struct snd_pcm_substream *substream,
555 struct snd_pcm_hw_params *hw_params)
556{
557 size_t size;
558
559
560 size = params_buffer_bytes(hw_params);
561 snd_pcm_lib_malloc_pages(substream, size);
562 return 0;
563};
564
565static int snd_ps3_pcm_hw_free(struct snd_pcm_substream *substream)
566{
567 return snd_pcm_lib_free_pages(substream);
568};
569
570static int snd_ps3_delay_to_bytes(struct snd_pcm_substream *substream,
571 unsigned int delay_ms)
572{
573 int ret;
574 int rate ;
575
576 rate = substream->runtime->rate;
577 ret = snd_pcm_format_size(substream->runtime->format,
578 rate * delay_ms / 1000)
579 * substream->runtime->channels;
580
581 pr_debug("%s: time=%d rate=%d bytes=%ld, frames=%d, ret=%d\n",
582 __func__,
583 delay_ms,
584 rate,
585 snd_pcm_format_size(substream->runtime->format, rate),
586 rate * delay_ms / 1000,
587 ret);
588
589 return ret;
590};
591
592static int snd_ps3_pcm_prepare(struct snd_pcm_substream *substream)
593{
594 struct snd_pcm_runtime *runtime = substream->runtime;
595 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
596 unsigned long irqsave;
597
598 if (!snd_ps3_set_avsetting(substream)) {
599
600 write_reg(PS3_AUDIO_AX_IE,
601 PS3_AUDIO_AX_IE_ASOBEIE(0) |
602 PS3_AUDIO_AX_IE_ASOBUIE(0));
603
604
605
606
607 card->silent = snd_ps3_delay_to_bytes(substream,
608 card->start_delay) /
609 (PS3_AUDIO_FIFO_STAGE_SIZE * 4);
610 }
611
612
613 spin_lock_irqsave(&card->dma_lock, irqsave);
614 {
615 card->dma_buffer_size = runtime->dma_bytes;
616
617 card->dma_last_transfer_vaddr[SND_PS3_CH_L] =
618 card->dma_next_transfer_vaddr[SND_PS3_CH_L] =
619 card->dma_start_vaddr[SND_PS3_CH_L] =
620 runtime->dma_area;
621 card->dma_start_bus_addr[SND_PS3_CH_L] = runtime->dma_addr;
622
623 card->dma_last_transfer_vaddr[SND_PS3_CH_R] =
624 card->dma_next_transfer_vaddr[SND_PS3_CH_R] =
625 card->dma_start_vaddr[SND_PS3_CH_R] =
626 runtime->dma_area + (runtime->dma_bytes / 2);
627 card->dma_start_bus_addr[SND_PS3_CH_R] =
628 runtime->dma_addr + (runtime->dma_bytes / 2);
629
630 pr_debug("%s: vaddr=%p bus=%#llx\n", __func__,
631 card->dma_start_vaddr[SND_PS3_CH_L],
632 card->dma_start_bus_addr[SND_PS3_CH_L]);
633
634 }
635 spin_unlock_irqrestore(&card->dma_lock, irqsave);
636
637
638 mb();
639
640 return 0;
641};
642
643static int snd_ps3_pcm_trigger(struct snd_pcm_substream *substream,
644 int cmd)
645{
646 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
647 int ret = 0;
648
649 switch (cmd) {
650 case SNDRV_PCM_TRIGGER_START:
651
652 update_reg(PS3_AUDIO_AX_IS, 0);
653
654 spin_lock(&card->dma_lock);
655 {
656 card->running = 1;
657 }
658 spin_unlock(&card->dma_lock);
659
660 snd_ps3_program_dma(card,
661 SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
662 snd_ps3_kick_dma(card);
663 while (read_reg(PS3_AUDIO_KICK(7)) &
664 PS3_AUDIO_KICK_STATUS_MASK) {
665 udelay(1);
666 }
667 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_RUNNING);
668 snd_ps3_kick_dma(card);
669 break;
670
671 case SNDRV_PCM_TRIGGER_STOP:
672 spin_lock(&card->dma_lock);
673 {
674 card->running = 0;
675 }
676 spin_unlock(&card->dma_lock);
677 snd_ps3_wait_for_dma_stop(card);
678 break;
679 default:
680 break;
681
682 }
683
684 return ret;
685};
686
687
688
689
690static snd_pcm_uframes_t snd_ps3_pcm_pointer(
691 struct snd_pcm_substream *substream)
692{
693 struct snd_ps3_card_info *card = snd_pcm_substream_chip(substream);
694 size_t bytes;
695 snd_pcm_uframes_t ret;
696
697 spin_lock(&card->dma_lock);
698 {
699 bytes = (size_t)(card->dma_last_transfer_vaddr[SND_PS3_CH_L] -
700 card->dma_start_vaddr[SND_PS3_CH_L]);
701 }
702 spin_unlock(&card->dma_lock);
703
704 ret = bytes_to_frames(substream->runtime, bytes * 2);
705
706 return ret;
707};
708
709
710
711
712static int snd_ps3_spdif_mask_info(struct snd_kcontrol *kcontrol,
713 struct snd_ctl_elem_info *uinfo)
714{
715 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
716 uinfo->count = 1;
717 return 0;
718}
719
720
721static int snd_ps3_spdif_cmask_get(struct snd_kcontrol *kcontrol,
722 struct snd_ctl_elem_value *ucontrol)
723{
724 memset(ucontrol->value.iec958.status, 0xff, 8);
725 return 0;
726}
727
728static int snd_ps3_spdif_pmask_get(struct snd_kcontrol *kcontrol,
729 struct snd_ctl_elem_value *ucontrol)
730{
731 return 0;
732}
733
734static int snd_ps3_spdif_default_get(struct snd_kcontrol *kcontrol,
735 struct snd_ctl_elem_value *ucontrol)
736{
737 memcpy(ucontrol->value.iec958.status, ps3av_mode_cs_info, 8);
738 return 0;
739}
740
741static int snd_ps3_spdif_default_put(struct snd_kcontrol *kcontrol,
742 struct snd_ctl_elem_value *ucontrol)
743{
744 if (memcmp(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8)) {
745 memcpy(ps3av_mode_cs_info, ucontrol->value.iec958.status, 8);
746 return 1;
747 }
748 return 0;
749}
750
751static struct snd_kcontrol_new spdif_ctls[] = {
752 {
753 .access = SNDRV_CTL_ELEM_ACCESS_READ,
754 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
755 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
756 .info = snd_ps3_spdif_mask_info,
757 .get = snd_ps3_spdif_cmask_get,
758 },
759 {
760 .access = SNDRV_CTL_ELEM_ACCESS_READ,
761 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
762 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
763 .info = snd_ps3_spdif_mask_info,
764 .get = snd_ps3_spdif_pmask_get,
765 },
766 {
767 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
768 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
769 .info = snd_ps3_spdif_mask_info,
770 .get = snd_ps3_spdif_default_get,
771 .put = snd_ps3_spdif_default_put,
772 },
773};
774
775static struct snd_pcm_ops snd_ps3_pcm_spdif_ops = {
776 .open = snd_ps3_pcm_open,
777 .close = snd_ps3_pcm_close,
778 .ioctl = snd_pcm_lib_ioctl,
779 .hw_params = snd_ps3_pcm_hw_params,
780 .hw_free = snd_ps3_pcm_hw_free,
781 .prepare = snd_ps3_pcm_prepare,
782 .trigger = snd_ps3_pcm_trigger,
783 .pointer = snd_ps3_pcm_pointer,
784};
785
786
787static int snd_ps3_map_mmio(void)
788{
789 the_card.mapped_mmio_vaddr =
790 ioremap(the_card.ps3_dev->m_region->bus_addr,
791 the_card.ps3_dev->m_region->len);
792
793 if (!the_card.mapped_mmio_vaddr) {
794 pr_info("%s: ioremap 0 failed p=%#lx l=%#lx \n",
795 __func__, the_card.ps3_dev->m_region->lpar_addr,
796 the_card.ps3_dev->m_region->len);
797 return -ENXIO;
798 }
799
800 return 0;
801};
802
803static void snd_ps3_unmap_mmio(void)
804{
805 iounmap(the_card.mapped_mmio_vaddr);
806 the_card.mapped_mmio_vaddr = NULL;
807}
808
809static int snd_ps3_allocate_irq(void)
810{
811 int ret;
812 u64 lpar_addr, lpar_size;
813 u64 __iomem *mapped;
814
815
816
817
818 ret = lv1_gpu_device_map(1, &lpar_addr, &lpar_size);
819 if (ret) {
820 pr_info("%s: device map 1 failed %d\n", __func__,
821 ret);
822 return -ENXIO;
823 }
824
825 mapped = ioremap(lpar_addr, lpar_size);
826 if (!mapped) {
827 pr_info("%s: ioremap 1 failed \n", __func__);
828 return -ENXIO;
829 }
830
831 the_card.audio_irq_outlet = in_be64(mapped);
832
833 iounmap(mapped);
834 ret = lv1_gpu_device_unmap(1);
835 if (ret)
836 pr_info("%s: unmap 1 failed\n", __func__);
837
838
839 ret = ps3_irq_plug_setup(PS3_BINDING_CPU_ANY,
840 the_card.audio_irq_outlet,
841 &the_card.irq_no);
842 if (ret) {
843 pr_info("%s:ps3_alloc_irq failed (%d)\n", __func__, ret);
844 return ret;
845 }
846
847 ret = request_irq(the_card.irq_no, snd_ps3_interrupt, 0,
848 SND_PS3_DRIVER_NAME, &the_card);
849 if (ret) {
850 pr_info("%s: request_irq failed (%d)\n", __func__, ret);
851 goto cleanup_irq;
852 }
853
854 return 0;
855
856 cleanup_irq:
857 ps3_irq_plug_destroy(the_card.irq_no);
858 return ret;
859};
860
861static void snd_ps3_free_irq(void)
862{
863 free_irq(the_card.irq_no, &the_card);
864 ps3_irq_plug_destroy(the_card.irq_no);
865}
866
867static void snd_ps3_audio_set_base_addr(uint64_t ioaddr_start)
868{
869 uint64_t val;
870 int ret;
871
872 val = (ioaddr_start & (0x0fUL << 32)) >> (32 - 20) |
873 (0x03UL << 24) |
874 (0x0fUL << 12) |
875 (PS3_AUDIO_IOID);
876
877 ret = lv1_gpu_attribute(0x100, 0x007, val);
878 if (ret)
879 pr_info("%s: gpu_attribute failed %d\n", __func__,
880 ret);
881}
882
883static void snd_ps3_audio_fixup(struct snd_ps3_card_info *card)
884{
885
886
887
888
889
890
891 write_reg(PS3_AUDIO_INTR_EN_0, 0);
892
893
894 update_mask_reg(PS3_AUDIO_AX_IC,
895 PS3_AUDIO_AX_IC_AASOIMD_MASK,
896 PS3_AUDIO_AX_IC_AASOIMD_EVERY4);
897
898
899 update_mask_reg(PS3_AUDIO_AO_3WMCTRL,
900 ~(PS3_AUDIO_AO_3WMCTRL_ASOBCLKD_DISABLED |
901 PS3_AUDIO_AO_3WMCTRL_ASOLRCKD_DISABLED),
902 0);
903 update_reg(PS3_AUDIO_AO_3WMCTRL,
904 PS3_AUDIO_AO_3WMCTRL_ASOPLRCK_DEFAULT);
905}
906
907static int snd_ps3_init_avsetting(struct snd_ps3_card_info *card)
908{
909 int ret;
910 pr_debug("%s: start\n", __func__);
911 card->avs.avs_audio_ch = PS3AV_CMD_AUDIO_NUM_OF_CH_2;
912 card->avs.avs_audio_rate = PS3AV_CMD_AUDIO_FS_48K;
913 card->avs.avs_audio_width = PS3AV_CMD_AUDIO_WORD_BITS_16;
914 card->avs.avs_audio_format = PS3AV_CMD_AUDIO_FORMAT_PCM;
915 card->avs.avs_audio_source = PS3AV_CMD_AUDIO_SOURCE_SERIAL;
916 memcpy(card->avs.avs_cs_info, ps3av_mode_cs_info, 8);
917
918 ret = snd_ps3_change_avsetting(card);
919
920 snd_ps3_audio_fixup(card);
921
922
923 snd_ps3_program_dma(card, SND_PS3_DMA_FILLTYPE_SILENT_FIRSTFILL);
924 snd_ps3_kick_dma(card);
925 pr_debug("%s: end\n", __func__);
926 return ret;
927}
928
929static int snd_ps3_driver_probe(struct ps3_system_bus_device *dev)
930{
931 int i, ret;
932 u64 lpar_addr, lpar_size;
933
934 if (WARN_ON(!firmware_has_feature(FW_FEATURE_PS3_LV1)))
935 return -ENODEV;
936 if (WARN_ON(dev->match_id != PS3_MATCH_ID_SOUND))
937 return -ENODEV;
938
939 the_card.ps3_dev = dev;
940
941 ret = ps3_open_hv_device(dev);
942
943 if (ret)
944 return -ENXIO;
945
946
947 ret = lv1_gpu_device_map(2, &lpar_addr, &lpar_size);
948 if (ret) {
949 pr_info("%s: device map 2 failed %d\n", __func__, ret);
950 goto clean_open;
951 }
952 ps3_mmio_region_init(dev, dev->m_region, lpar_addr, lpar_size,
953 PAGE_SHIFT);
954
955 ret = snd_ps3_map_mmio();
956 if (ret)
957 goto clean_dev_map;
958
959
960 ps3_dma_region_init(dev, dev->d_region,
961 PAGE_SHIFT,
962 0,
963 NULL,
964 _ALIGN_UP(SND_PS3_DMA_REGION_SIZE, PAGE_SIZE));
965 dev->d_region->ioid = PS3_AUDIO_IOID;
966
967 ret = ps3_dma_region_create(dev->d_region);
968 if (ret) {
969 pr_info("%s: region_create\n", __func__);
970 goto clean_mmio;
971 }
972
973 snd_ps3_audio_set_base_addr(dev->d_region->bus_addr);
974
975
976 the_card.start_delay = snd_ps3_start_delay;
977
978
979 if (snd_ps3_allocate_irq()) {
980 ret = -ENXIO;
981 goto clean_dma_region;
982 }
983
984
985 ret = snd_card_new(&dev->core, index, id, THIS_MODULE,
986 0, &the_card.card);
987 if (ret < 0)
988 goto clean_irq;
989
990 strcpy(the_card.card->driver, "PS3");
991 strcpy(the_card.card->shortname, "PS3");
992 strcpy(the_card.card->longname, "PS3 sound");
993
994
995 for (i = 0; i < ARRAY_SIZE(spdif_ctls); i++) {
996 ret = snd_ctl_add(the_card.card,
997 snd_ctl_new1(&spdif_ctls[i], &the_card));
998 if (ret < 0)
999 goto clean_card;
1000 }
1001
1002
1003
1004 ret = snd_pcm_new(the_card.card,
1005 "SPDIF",
1006 0,
1007 1,
1008 0,
1009 &(the_card.pcm));
1010 if (ret)
1011 goto clean_card;
1012
1013 the_card.pcm->private_data = &the_card;
1014 strcpy(the_card.pcm->name, "SPDIF");
1015
1016
1017 snd_pcm_set_ops(the_card.pcm, SNDRV_PCM_STREAM_PLAYBACK,
1018 &snd_ps3_pcm_spdif_ops);
1019
1020 the_card.pcm->info_flags = SNDRV_PCM_INFO_NONINTERLEAVED;
1021
1022 ret = snd_pcm_lib_preallocate_pages_for_all(the_card.pcm,
1023 SNDRV_DMA_TYPE_DEV,
1024 &dev->core,
1025 SND_PS3_PCM_PREALLOC_SIZE,
1026 SND_PS3_PCM_PREALLOC_SIZE);
1027 if (ret < 0) {
1028 pr_info("%s: prealloc failed\n", __func__);
1029 goto clean_card;
1030 }
1031
1032
1033
1034
1035
1036
1037 the_card.null_buffer_start_vaddr =
1038 dma_alloc_coherent(&the_card.ps3_dev->core,
1039 PAGE_SIZE,
1040 &the_card.null_buffer_start_dma_addr,
1041 GFP_KERNEL);
1042 if (!the_card.null_buffer_start_vaddr) {
1043 pr_info("%s: nullbuffer alloc failed\n", __func__);
1044 ret = -ENOMEM;
1045 goto clean_card;
1046 }
1047 pr_debug("%s: null vaddr=%p dma=%#llx\n", __func__,
1048 the_card.null_buffer_start_vaddr,
1049 the_card.null_buffer_start_dma_addr);
1050
1051 snd_ps3_init_avsetting(&the_card);
1052
1053
1054 ret = snd_card_register(the_card.card);
1055 if (ret < 0)
1056 goto clean_dma_map;
1057
1058 pr_info("%s started. start_delay=%dms\n",
1059 the_card.card->longname, the_card.start_delay);
1060 return 0;
1061
1062clean_dma_map:
1063 dma_free_coherent(&the_card.ps3_dev->core,
1064 PAGE_SIZE,
1065 the_card.null_buffer_start_vaddr,
1066 the_card.null_buffer_start_dma_addr);
1067clean_card:
1068 snd_card_free(the_card.card);
1069clean_irq:
1070 snd_ps3_free_irq();
1071clean_dma_region:
1072 ps3_dma_region_free(dev->d_region);
1073clean_mmio:
1074 snd_ps3_unmap_mmio();
1075clean_dev_map:
1076 lv1_gpu_device_unmap(2);
1077clean_open:
1078 ps3_close_hv_device(dev);
1079
1080
1081
1082
1083 return ret;
1084};
1085
1086
1087static int snd_ps3_driver_remove(struct ps3_system_bus_device *dev)
1088{
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;
1093
1094
1095
1096
1097
1098 ret = snd_card_free(the_card.card);
1099 if (ret)
1100 pr_info("%s: ctl freecard=%d\n", __func__, ret);
1101
1102 dma_free_coherent(&dev->core,
1103 PAGE_SIZE,
1104 the_card.null_buffer_start_vaddr,
1105 the_card.null_buffer_start_dma_addr);
1106
1107 ps3_dma_region_free(dev->d_region);
1108
1109 snd_ps3_free_irq();
1110 snd_ps3_unmap_mmio();
1111
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}
1117
1118static 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,
1126 },
1127};
1128
1129
1130
1131
1132
1133static int __init snd_ps3_init(void)
1134{
1135 int ret;
1136
1137 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
1138 return -ENXIO;
1139
1140 memset(&the_card, 0, sizeof(the_card));
1141 spin_lock_init(&the_card.dma_lock);
1142
1143
1144 ret = ps3_system_bus_driver_register(&snd_ps3_bus_driver_info);
1145
1146 return ret;
1147}
1148module_init(snd_ps3_init);
1149
1150static void __exit snd_ps3_exit(void)
1151{
1152 ps3_system_bus_driver_unregister(&snd_ps3_bus_driver_info);
1153}
1154module_exit(snd_ps3_exit);
1155
1156MODULE_LICENSE("GPL v2");
1157MODULE_DESCRIPTION("PS3 sound driver");
1158MODULE_AUTHOR("Sony Computer Entertainment Inc.");
1159MODULE_ALIAS(PS3_MODULE_ALIAS_SOUND);
1160