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