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