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