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