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