xref: /openbmc/linux/sound/pci/echoaudio/echoaudio_dsp.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1dd7b254dSGiuliano Pochini /****************************************************************************
2dd7b254dSGiuliano Pochini 
3dd7b254dSGiuliano Pochini    Copyright Echo Digital Audio Corporation (c) 1998 - 2004
4dd7b254dSGiuliano Pochini    All rights reserved
5dd7b254dSGiuliano Pochini    www.echoaudio.com
6dd7b254dSGiuliano Pochini 
7dd7b254dSGiuliano Pochini    This file is part of Echo Digital Audio's generic driver library.
8dd7b254dSGiuliano Pochini 
9dd7b254dSGiuliano Pochini    Echo Digital Audio's generic driver library is free software;
10dd7b254dSGiuliano Pochini    you can redistribute it and/or modify it under the terms of
11dd7b254dSGiuliano Pochini    the GNU General Public License as published by the Free Software
12dd7b254dSGiuliano Pochini    Foundation.
13dd7b254dSGiuliano Pochini 
14dd7b254dSGiuliano Pochini    This program is distributed in the hope that it will be useful,
15dd7b254dSGiuliano Pochini    but WITHOUT ANY WARRANTY; without even the implied warranty of
16dd7b254dSGiuliano Pochini    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17dd7b254dSGiuliano Pochini    GNU General Public License for more details.
18dd7b254dSGiuliano Pochini 
19dd7b254dSGiuliano Pochini    You should have received a copy of the GNU General Public License
20dd7b254dSGiuliano Pochini    along with this program; if not, write to the Free Software
21dd7b254dSGiuliano Pochini    Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22dd7b254dSGiuliano Pochini    MA  02111-1307, USA.
23dd7b254dSGiuliano Pochini 
24dd7b254dSGiuliano Pochini    *************************************************************************
25dd7b254dSGiuliano Pochini 
26dd7b254dSGiuliano Pochini  Translation from C++ and adaptation for use in ALSA-Driver
27dd7b254dSGiuliano Pochini  were made by Giuliano Pochini <pochini@shiny.it>
28dd7b254dSGiuliano Pochini 
29dd7b254dSGiuliano Pochini ****************************************************************************/
30dd7b254dSGiuliano Pochini 
31dd7b254dSGiuliano Pochini #if PAGE_SIZE < 4096
32dd7b254dSGiuliano Pochini #error PAGE_SIZE is < 4k
33dd7b254dSGiuliano Pochini #endif
34dd7b254dSGiuliano Pochini 
35dd7b254dSGiuliano Pochini static int restore_dsp_rettings(struct echoaudio *chip);
36dd7b254dSGiuliano Pochini 
37dd7b254dSGiuliano Pochini 
38dd7b254dSGiuliano Pochini /* Some vector commands involve the DSP reading or writing data to and from the
39dd7b254dSGiuliano Pochini comm page; if you send one of these commands to the DSP, it will complete the
40dd7b254dSGiuliano Pochini command and then write a non-zero value to the Handshake field in the
41dd7b254dSGiuliano Pochini comm page.  This function waits for the handshake to show up. */
wait_handshake(struct echoaudio * chip)42dd7b254dSGiuliano Pochini static int wait_handshake(struct echoaudio *chip)
43dd7b254dSGiuliano Pochini {
44dd7b254dSGiuliano Pochini 	int i;
45dd7b254dSGiuliano Pochini 
4622d3a200SGiuliano Pochini 	/* Wait up to 20ms for the handshake from the DSP */
47dd7b254dSGiuliano Pochini 	for (i = 0; i < HANDSHAKE_TIMEOUT; i++) {
48dd7b254dSGiuliano Pochini 		/* Look for the handshake value */
4922d3a200SGiuliano Pochini 		barrier();
50dd7b254dSGiuliano Pochini 		if (chip->comm_page->handshake) {
51dd7b254dSGiuliano Pochini 			return 0;
52dd7b254dSGiuliano Pochini 		}
53dd7b254dSGiuliano Pochini 		udelay(1);
54dd7b254dSGiuliano Pochini 	}
55dd7b254dSGiuliano Pochini 
56ece7a36dSTakashi Iwai 	dev_err(chip->card->dev, "wait_handshake(): Timeout waiting for DSP\n");
57dd7b254dSGiuliano Pochini 	return -EBUSY;
58dd7b254dSGiuliano Pochini }
59dd7b254dSGiuliano Pochini 
60dd7b254dSGiuliano Pochini 
61dd7b254dSGiuliano Pochini 
62dd7b254dSGiuliano Pochini /* Much of the interaction between the DSP and the driver is done via vector
63dd7b254dSGiuliano Pochini commands; send_vector writes a vector command to the DSP.  Typically, this
64dd7b254dSGiuliano Pochini causes the DSP to read or write fields in the comm page.
65dd7b254dSGiuliano Pochini PCI posting is not required thanks to the handshake logic. */
send_vector(struct echoaudio * chip,u32 command)66dd7b254dSGiuliano Pochini static int send_vector(struct echoaudio *chip, u32 command)
67dd7b254dSGiuliano Pochini {
68dd7b254dSGiuliano Pochini 	int i;
69dd7b254dSGiuliano Pochini 
70dd7b254dSGiuliano Pochini 	wmb();	/* Flush all pending writes before sending the command */
71dd7b254dSGiuliano Pochini 
72dd7b254dSGiuliano Pochini 	/* Wait up to 100ms for the "vector busy" bit to be off */
73dd7b254dSGiuliano Pochini 	for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) {
74dd7b254dSGiuliano Pochini 		if (!(get_dsp_register(chip, CHI32_VECTOR_REG) &
75dd7b254dSGiuliano Pochini 		      CHI32_VECTOR_BUSY)) {
76dd7b254dSGiuliano Pochini 			set_dsp_register(chip, CHI32_VECTOR_REG, command);
77dd7b254dSGiuliano Pochini 			/*if (i)  DE_ACT(("send_vector time: %d\n", i));*/
78dd7b254dSGiuliano Pochini 			return 0;
79dd7b254dSGiuliano Pochini 		}
80dd7b254dSGiuliano Pochini 		udelay(1);
81dd7b254dSGiuliano Pochini 	}
82dd7b254dSGiuliano Pochini 
83b5b4a41bSSudip Mukherjee 	dev_err(chip->card->dev, "timeout on send_vector\n");
84dd7b254dSGiuliano Pochini 	return -EBUSY;
85dd7b254dSGiuliano Pochini }
86dd7b254dSGiuliano Pochini 
87dd7b254dSGiuliano Pochini 
88dd7b254dSGiuliano Pochini 
89dd7b254dSGiuliano Pochini /* write_dsp writes a 32-bit value to the DSP; this is used almost
90dd7b254dSGiuliano Pochini exclusively for loading the DSP. */
write_dsp(struct echoaudio * chip,u32 data)91dd7b254dSGiuliano Pochini static int write_dsp(struct echoaudio *chip, u32 data)
92dd7b254dSGiuliano Pochini {
93dd7b254dSGiuliano Pochini 	u32 status, i;
94dd7b254dSGiuliano Pochini 
95dd7b254dSGiuliano Pochini 	for (i = 0; i < 10000000; i++) {	/* timeout = 10s */
96dd7b254dSGiuliano Pochini 		status = get_dsp_register(chip, CHI32_STATUS_REG);
97dd7b254dSGiuliano Pochini 		if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) {
98dd7b254dSGiuliano Pochini 			set_dsp_register(chip, CHI32_DATA_REG, data);
99dd7b254dSGiuliano Pochini 			wmb();			/* write it immediately */
100dd7b254dSGiuliano Pochini 			return 0;
101dd7b254dSGiuliano Pochini 		}
102dd7b254dSGiuliano Pochini 		udelay(1);
103dd7b254dSGiuliano Pochini 		cond_resched();
104dd7b254dSGiuliano Pochini 	}
105dd7b254dSGiuliano Pochini 
1063f6175ecSMark Brown 	chip->bad_board = true;		/* Set true until DSP re-loaded */
1073f6175ecSMark Brown 	dev_dbg(chip->card->dev, "write_dsp: Set bad_board to true\n");
108dd7b254dSGiuliano Pochini 	return -EIO;
109dd7b254dSGiuliano Pochini }
110dd7b254dSGiuliano Pochini 
111dd7b254dSGiuliano Pochini 
112dd7b254dSGiuliano Pochini 
113dd7b254dSGiuliano Pochini /* read_dsp reads a 32-bit value from the DSP; this is used almost
114dd7b254dSGiuliano Pochini exclusively for loading the DSP and checking the status of the ASIC. */
read_dsp(struct echoaudio * chip,u32 * data)115dd7b254dSGiuliano Pochini static int read_dsp(struct echoaudio *chip, u32 *data)
116dd7b254dSGiuliano Pochini {
117dd7b254dSGiuliano Pochini 	u32 status, i;
118dd7b254dSGiuliano Pochini 
119dd7b254dSGiuliano Pochini 	for (i = 0; i < READ_DSP_TIMEOUT; i++) {
120dd7b254dSGiuliano Pochini 		status = get_dsp_register(chip, CHI32_STATUS_REG);
121dd7b254dSGiuliano Pochini 		if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) {
122dd7b254dSGiuliano Pochini 			*data = get_dsp_register(chip, CHI32_DATA_REG);
123dd7b254dSGiuliano Pochini 			return 0;
124dd7b254dSGiuliano Pochini 		}
125dd7b254dSGiuliano Pochini 		udelay(1);
126dd7b254dSGiuliano Pochini 		cond_resched();
127dd7b254dSGiuliano Pochini 	}
128dd7b254dSGiuliano Pochini 
1293f6175ecSMark Brown 	chip->bad_board = true;		/* Set true until DSP re-loaded */
1303f6175ecSMark Brown 	dev_err(chip->card->dev, "read_dsp: Set bad_board to true\n");
131dd7b254dSGiuliano Pochini 	return -EIO;
132dd7b254dSGiuliano Pochini }
133dd7b254dSGiuliano Pochini 
134dd7b254dSGiuliano Pochini 
135dd7b254dSGiuliano Pochini 
136dd7b254dSGiuliano Pochini /****************************************************************************
137dd7b254dSGiuliano Pochini 	Firmware loading functions
138dd7b254dSGiuliano Pochini  ****************************************************************************/
139dd7b254dSGiuliano Pochini 
140dd7b254dSGiuliano Pochini /* This function is used to read back the serial number from the DSP;
141dd7b254dSGiuliano Pochini this is triggered by the SET_COMMPAGE_ADDR command.
142dd7b254dSGiuliano Pochini Only some early Echogals products have serial numbers in the ROM;
143dd7b254dSGiuliano Pochini the serial number is not used, but you still need to do this as
144dd7b254dSGiuliano Pochini part of the DSP load process. */
read_sn(struct echoaudio * chip)145dd7b254dSGiuliano Pochini static int read_sn(struct echoaudio *chip)
146dd7b254dSGiuliano Pochini {
147dd7b254dSGiuliano Pochini 	int i;
148dd7b254dSGiuliano Pochini 	u32 sn[6];
149dd7b254dSGiuliano Pochini 
150dd7b254dSGiuliano Pochini 	for (i = 0; i < 5; i++) {
151dd7b254dSGiuliano Pochini 		if (read_dsp(chip, &sn[i])) {
152ece7a36dSTakashi Iwai 			dev_err(chip->card->dev,
153ece7a36dSTakashi Iwai 				"Failed to read serial number\n");
154dd7b254dSGiuliano Pochini 			return -EIO;
155dd7b254dSGiuliano Pochini 		}
156dd7b254dSGiuliano Pochini 	}
157b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev,
158b5b4a41bSSudip Mukherjee 		"Read serial number %08x %08x %08x %08x %08x\n",
159b5b4a41bSSudip Mukherjee 		 sn[0], sn[1], sn[2], sn[3], sn[4]);
160dd7b254dSGiuliano Pochini 	return 0;
161dd7b254dSGiuliano Pochini }
162dd7b254dSGiuliano Pochini 
163dd7b254dSGiuliano Pochini 
164dd7b254dSGiuliano Pochini 
165dd7b254dSGiuliano Pochini #ifndef ECHOCARD_HAS_ASIC
166dd7b254dSGiuliano Pochini /* This card has no ASIC, just return ok */
check_asic_status(struct echoaudio * chip)167dd7b254dSGiuliano Pochini static inline int check_asic_status(struct echoaudio *chip)
168dd7b254dSGiuliano Pochini {
1693f6175ecSMark Brown 	chip->asic_loaded = true;
170dd7b254dSGiuliano Pochini 	return 0;
171dd7b254dSGiuliano Pochini }
172dd7b254dSGiuliano Pochini 
173dd7b254dSGiuliano Pochini #endif /* !ECHOCARD_HAS_ASIC */
174dd7b254dSGiuliano Pochini 
175dd7b254dSGiuliano Pochini 
176dd7b254dSGiuliano Pochini 
177dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_ASIC
178dd7b254dSGiuliano Pochini 
179dd7b254dSGiuliano Pochini /* Load ASIC code - done after the DSP is loaded */
load_asic_generic(struct echoaudio * chip,u32 cmd,short asic)18019b50063SGiuliano Pochini static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic)
181dd7b254dSGiuliano Pochini {
182dd7b254dSGiuliano Pochini 	const struct firmware *fw;
183dd7b254dSGiuliano Pochini 	int err;
184dd7b254dSGiuliano Pochini 	u32 i, size;
185dd7b254dSGiuliano Pochini 	u8 *code;
186dd7b254dSGiuliano Pochini 
18719b50063SGiuliano Pochini 	err = get_firmware(&fw, chip, asic);
18819b50063SGiuliano Pochini 	if (err < 0) {
189ece7a36dSTakashi Iwai 		dev_warn(chip->card->dev, "Firmware not found !\n");
190dd7b254dSGiuliano Pochini 		return err;
191dd7b254dSGiuliano Pochini 	}
192dd7b254dSGiuliano Pochini 
193dd7b254dSGiuliano Pochini 	code = (u8 *)fw->data;
194dd7b254dSGiuliano Pochini 	size = fw->size;
195dd7b254dSGiuliano Pochini 
196dd7b254dSGiuliano Pochini 	/* Send the "Here comes the ASIC" command */
197dd7b254dSGiuliano Pochini 	if (write_dsp(chip, cmd) < 0)
198dd7b254dSGiuliano Pochini 		goto la_error;
199dd7b254dSGiuliano Pochini 
200dd7b254dSGiuliano Pochini 	/* Write length of ASIC file in bytes */
201dd7b254dSGiuliano Pochini 	if (write_dsp(chip, size) < 0)
202dd7b254dSGiuliano Pochini 		goto la_error;
203dd7b254dSGiuliano Pochini 
204dd7b254dSGiuliano Pochini 	for (i = 0; i < size; i++) {
205dd7b254dSGiuliano Pochini 		if (write_dsp(chip, code[i]) < 0)
206dd7b254dSGiuliano Pochini 			goto la_error;
207dd7b254dSGiuliano Pochini 	}
208dd7b254dSGiuliano Pochini 
209e3690869SSudip Mukherjee 	free_firmware(fw, chip);
210dd7b254dSGiuliano Pochini 	return 0;
211dd7b254dSGiuliano Pochini 
212dd7b254dSGiuliano Pochini la_error:
213b5b4a41bSSudip Mukherjee 	dev_err(chip->card->dev, "failed on write_dsp\n");
214e3690869SSudip Mukherjee 	free_firmware(fw, chip);
215dd7b254dSGiuliano Pochini 	return -EIO;
216dd7b254dSGiuliano Pochini }
217dd7b254dSGiuliano Pochini 
218dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_ASIC */
219dd7b254dSGiuliano Pochini 
220dd7b254dSGiuliano Pochini 
221dd7b254dSGiuliano Pochini 
222dd7b254dSGiuliano Pochini #ifdef DSP_56361
223dd7b254dSGiuliano Pochini 
224dd7b254dSGiuliano Pochini /* Install the resident loader for 56361 DSPs;  The resident loader is on
225dd7b254dSGiuliano Pochini the EPROM on the board for 56301 DSP. The resident loader is a tiny little
226dd7b254dSGiuliano Pochini program that is used to load the real DSP code. */
install_resident_loader(struct echoaudio * chip)227dd7b254dSGiuliano Pochini static int install_resident_loader(struct echoaudio *chip)
228dd7b254dSGiuliano Pochini {
229dd7b254dSGiuliano Pochini 	u32 address;
230dd7b254dSGiuliano Pochini 	int index, words, i;
231dd7b254dSGiuliano Pochini 	u16 *code;
232dd7b254dSGiuliano Pochini 	u32 status;
233dd7b254dSGiuliano Pochini 	const struct firmware *fw;
234dd7b254dSGiuliano Pochini 
235dd7b254dSGiuliano Pochini 	/* 56361 cards only!  This check is required by the old 56301-based
236dd7b254dSGiuliano Pochini 	Mona and Gina24 */
237dd7b254dSGiuliano Pochini 	if (chip->device_id != DEVICE_ID_56361)
238dd7b254dSGiuliano Pochini 		return 0;
239dd7b254dSGiuliano Pochini 
240dd7b254dSGiuliano Pochini 	/* Look to see if the resident loader is present.  If the resident
241dd7b254dSGiuliano Pochini 	loader is already installed, host flag 5 will be on. */
242dd7b254dSGiuliano Pochini 	status = get_dsp_register(chip, CHI32_STATUS_REG);
243dd7b254dSGiuliano Pochini 	if (status & CHI32_STATUS_REG_HF5) {
244b5b4a41bSSudip Mukherjee 		dev_dbg(chip->card->dev,
245b5b4a41bSSudip Mukherjee 			"Resident loader already installed; status is 0x%x\n",
246b5b4a41bSSudip Mukherjee 			 status);
247dd7b254dSGiuliano Pochini 		return 0;
248dd7b254dSGiuliano Pochini 	}
249dd7b254dSGiuliano Pochini 
25019b50063SGiuliano Pochini 	i = get_firmware(&fw, chip, FW_361_LOADER);
25119b50063SGiuliano Pochini 	if (i < 0) {
252ece7a36dSTakashi Iwai 		dev_warn(chip->card->dev, "Firmware not found !\n");
253dd7b254dSGiuliano Pochini 		return i;
254dd7b254dSGiuliano Pochini 	}
255dd7b254dSGiuliano Pochini 
256dd7b254dSGiuliano Pochini 	/* The DSP code is an array of 16 bit words.  The array is divided up
257dd7b254dSGiuliano Pochini 	into sections.  The first word of each section is the size in words,
258dd7b254dSGiuliano Pochini 	followed by the section type.
259dd7b254dSGiuliano Pochini 	Since DSP addresses and data are 24 bits wide, they each take up two
260dd7b254dSGiuliano Pochini 	16 bit words in the array.
261dd7b254dSGiuliano Pochini 	This is a lot like the other loader loop, but it's not a loop, you
262dd7b254dSGiuliano Pochini 	don't write the memory type, and you don't write a zero at the end. */
263dd7b254dSGiuliano Pochini 
264dd7b254dSGiuliano Pochini 	/* Set DSP format bits for 24 bit mode */
265dd7b254dSGiuliano Pochini 	set_dsp_register(chip, CHI32_CONTROL_REG,
266dd7b254dSGiuliano Pochini 			 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
267dd7b254dSGiuliano Pochini 
268dd7b254dSGiuliano Pochini 	code = (u16 *)fw->data;
269dd7b254dSGiuliano Pochini 
270dd7b254dSGiuliano Pochini 	/* Skip the header section; the first word in the array is the size
271dd7b254dSGiuliano Pochini 	of the first section, so the first real section of code is pointed
272dd7b254dSGiuliano Pochini 	to by Code[0]. */
273dd7b254dSGiuliano Pochini 	index = code[0];
274dd7b254dSGiuliano Pochini 
275dd7b254dSGiuliano Pochini 	/* Skip the section size, LRS block type, and DSP memory type */
276dd7b254dSGiuliano Pochini 	index += 3;
277dd7b254dSGiuliano Pochini 
278dd7b254dSGiuliano Pochini 	/* Get the number of DSP words to write */
279dd7b254dSGiuliano Pochini 	words = code[index++];
280dd7b254dSGiuliano Pochini 
281dd7b254dSGiuliano Pochini 	/* Get the DSP address for this block; 24 bits, so build from two words */
282dd7b254dSGiuliano Pochini 	address = ((u32)code[index] << 16) + code[index + 1];
283dd7b254dSGiuliano Pochini 	index += 2;
284dd7b254dSGiuliano Pochini 
285dd7b254dSGiuliano Pochini 	/* Write the count to the DSP */
286dd7b254dSGiuliano Pochini 	if (write_dsp(chip, words)) {
287b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
288b5b4a41bSSudip Mukherjee 			"install_resident_loader: Failed to write word count!\n");
289dd7b254dSGiuliano Pochini 		goto irl_error;
290dd7b254dSGiuliano Pochini 	}
291dd7b254dSGiuliano Pochini 	/* Write the DSP address */
292dd7b254dSGiuliano Pochini 	if (write_dsp(chip, address)) {
293b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
294b5b4a41bSSudip Mukherjee 			"install_resident_loader: Failed to write DSP address!\n");
295dd7b254dSGiuliano Pochini 		goto irl_error;
296dd7b254dSGiuliano Pochini 	}
297dd7b254dSGiuliano Pochini 	/* Write out this block of code to the DSP */
298dd7b254dSGiuliano Pochini 	for (i = 0; i < words; i++) {
299dd7b254dSGiuliano Pochini 		u32 data;
300dd7b254dSGiuliano Pochini 
301dd7b254dSGiuliano Pochini 		data = ((u32)code[index] << 16) + code[index + 1];
302dd7b254dSGiuliano Pochini 		if (write_dsp(chip, data)) {
303b5b4a41bSSudip Mukherjee 			dev_err(chip->card->dev,
304b5b4a41bSSudip Mukherjee 				"install_resident_loader: Failed to write DSP code\n");
305dd7b254dSGiuliano Pochini 			goto irl_error;
306dd7b254dSGiuliano Pochini 		}
307dd7b254dSGiuliano Pochini 		index += 2;
308dd7b254dSGiuliano Pochini 	}
309dd7b254dSGiuliano Pochini 
310dd7b254dSGiuliano Pochini 	/* Wait for flag 5 to come up */
311dd7b254dSGiuliano Pochini 	for (i = 0; i < 200; i++) {	/* Timeout is 50us * 200 = 10ms */
312dd7b254dSGiuliano Pochini 		udelay(50);
313dd7b254dSGiuliano Pochini 		status = get_dsp_register(chip, CHI32_STATUS_REG);
314dd7b254dSGiuliano Pochini 		if (status & CHI32_STATUS_REG_HF5)
315dd7b254dSGiuliano Pochini 			break;
316dd7b254dSGiuliano Pochini 	}
317dd7b254dSGiuliano Pochini 
318dd7b254dSGiuliano Pochini 	if (i == 200) {
319b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev, "Resident loader failed to set HF5\n");
320dd7b254dSGiuliano Pochini 		goto irl_error;
321dd7b254dSGiuliano Pochini 	}
322dd7b254dSGiuliano Pochini 
323b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev, "Resident loader successfully installed\n");
324e3690869SSudip Mukherjee 	free_firmware(fw, chip);
325dd7b254dSGiuliano Pochini 	return 0;
326dd7b254dSGiuliano Pochini 
327dd7b254dSGiuliano Pochini irl_error:
328e3690869SSudip Mukherjee 	free_firmware(fw, chip);
329dd7b254dSGiuliano Pochini 	return -EIO;
330dd7b254dSGiuliano Pochini }
331dd7b254dSGiuliano Pochini 
332dd7b254dSGiuliano Pochini #endif /* DSP_56361 */
333dd7b254dSGiuliano Pochini 
334dd7b254dSGiuliano Pochini 
load_dsp(struct echoaudio * chip,u16 * code)335dd7b254dSGiuliano Pochini static int load_dsp(struct echoaudio *chip, u16 *code)
336dd7b254dSGiuliano Pochini {
337dd7b254dSGiuliano Pochini 	u32 address, data;
338dd7b254dSGiuliano Pochini 	int index, words, i;
339dd7b254dSGiuliano Pochini 
340dd7b254dSGiuliano Pochini 	if (chip->dsp_code == code) {
341b5b4a41bSSudip Mukherjee 		dev_warn(chip->card->dev, "DSP is already loaded!\n");
342dd7b254dSGiuliano Pochini 		return 0;
343dd7b254dSGiuliano Pochini 	}
3443f6175ecSMark Brown 	chip->bad_board = true;		/* Set true until DSP loaded */
345dd7b254dSGiuliano Pochini 	chip->dsp_code = NULL;		/* Current DSP code not loaded */
3463f6175ecSMark Brown 	chip->asic_loaded = false;	/* Loading the DSP code will reset the ASIC */
347dd7b254dSGiuliano Pochini 
3483f6175ecSMark Brown 	dev_dbg(chip->card->dev, "load_dsp: Set bad_board to true\n");
349dd7b254dSGiuliano Pochini 
350dd7b254dSGiuliano Pochini 	/* If this board requires a resident loader, install it. */
351dd7b254dSGiuliano Pochini #ifdef DSP_56361
352*549717fcSTakashi Iwai 	i = install_resident_loader(chip);
353*549717fcSTakashi Iwai 	if (i < 0)
354dd7b254dSGiuliano Pochini 		return i;
355dd7b254dSGiuliano Pochini #endif
356dd7b254dSGiuliano Pochini 
357dd7b254dSGiuliano Pochini 	/* Send software reset command */
358dd7b254dSGiuliano Pochini 	if (send_vector(chip, DSP_VC_RESET) < 0) {
359b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
360b5b4a41bSSudip Mukherjee 			"LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n");
361dd7b254dSGiuliano Pochini 		return -EIO;
362dd7b254dSGiuliano Pochini 	}
363dd7b254dSGiuliano Pochini 	/* Delay 10us */
364dd7b254dSGiuliano Pochini 	udelay(10);
365dd7b254dSGiuliano Pochini 
366dd7b254dSGiuliano Pochini 	/* Wait 10ms for HF3 to indicate that software reset is complete */
367dd7b254dSGiuliano Pochini 	for (i = 0; i < 1000; i++) {	/* Timeout is 10us * 1000 = 10ms */
368dd7b254dSGiuliano Pochini 		if (get_dsp_register(chip, CHI32_STATUS_REG) &
369dd7b254dSGiuliano Pochini 		    CHI32_STATUS_REG_HF3)
370dd7b254dSGiuliano Pochini 			break;
371dd7b254dSGiuliano Pochini 		udelay(10);
372dd7b254dSGiuliano Pochini 	}
373dd7b254dSGiuliano Pochini 
374dd7b254dSGiuliano Pochini 	if (i == 1000) {
375b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
376b5b4a41bSSudip Mukherjee 			"load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n");
377dd7b254dSGiuliano Pochini 		return -EIO;
378dd7b254dSGiuliano Pochini 	}
379dd7b254dSGiuliano Pochini 
380dd7b254dSGiuliano Pochini 	/* Set DSP format bits for 24 bit mode now that soft reset is done */
381dd7b254dSGiuliano Pochini 	set_dsp_register(chip, CHI32_CONTROL_REG,
382dd7b254dSGiuliano Pochini 			 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
383dd7b254dSGiuliano Pochini 
384dd7b254dSGiuliano Pochini 	/* Main loader loop */
385dd7b254dSGiuliano Pochini 
386dd7b254dSGiuliano Pochini 	index = code[0];
387dd7b254dSGiuliano Pochini 	for (;;) {
388dd7b254dSGiuliano Pochini 		int block_type, mem_type;
389dd7b254dSGiuliano Pochini 
390dd7b254dSGiuliano Pochini 		/* Total Block Size */
391dd7b254dSGiuliano Pochini 		index++;
392dd7b254dSGiuliano Pochini 
393dd7b254dSGiuliano Pochini 		/* Block Type */
394dd7b254dSGiuliano Pochini 		block_type = code[index];
395dd7b254dSGiuliano Pochini 		if (block_type == 4)	/* We're finished */
396dd7b254dSGiuliano Pochini 			break;
397dd7b254dSGiuliano Pochini 
398dd7b254dSGiuliano Pochini 		index++;
399dd7b254dSGiuliano Pochini 
400dd7b254dSGiuliano Pochini 		/* Memory Type  P=0,X=1,Y=2 */
401dd7b254dSGiuliano Pochini 		mem_type = code[index++];
402dd7b254dSGiuliano Pochini 
403dd7b254dSGiuliano Pochini 		/* Block Code Size */
404dd7b254dSGiuliano Pochini 		words = code[index++];
405dd7b254dSGiuliano Pochini 		if (words == 0)		/* We're finished */
406dd7b254dSGiuliano Pochini 			break;
407dd7b254dSGiuliano Pochini 
408dd7b254dSGiuliano Pochini 		/* Start Address */
409dd7b254dSGiuliano Pochini 		address = ((u32)code[index] << 16) + code[index + 1];
410dd7b254dSGiuliano Pochini 		index += 2;
411dd7b254dSGiuliano Pochini 
412dd7b254dSGiuliano Pochini 		if (write_dsp(chip, words) < 0) {
413b5b4a41bSSudip Mukherjee 			dev_err(chip->card->dev,
414b5b4a41bSSudip Mukherjee 				"load_dsp: failed to write number of DSP words\n");
415dd7b254dSGiuliano Pochini 			return -EIO;
416dd7b254dSGiuliano Pochini 		}
417dd7b254dSGiuliano Pochini 		if (write_dsp(chip, address) < 0) {
418b5b4a41bSSudip Mukherjee 			dev_err(chip->card->dev,
419b5b4a41bSSudip Mukherjee 				"load_dsp: failed to write DSP address\n");
420dd7b254dSGiuliano Pochini 			return -EIO;
421dd7b254dSGiuliano Pochini 		}
422dd7b254dSGiuliano Pochini 		if (write_dsp(chip, mem_type) < 0) {
423b5b4a41bSSudip Mukherjee 			dev_err(chip->card->dev,
424b5b4a41bSSudip Mukherjee 				"load_dsp: failed to write DSP memory type\n");
425dd7b254dSGiuliano Pochini 			return -EIO;
426dd7b254dSGiuliano Pochini 		}
427dd7b254dSGiuliano Pochini 		/* Code */
428dd7b254dSGiuliano Pochini 		for (i = 0; i < words; i++, index+=2) {
429dd7b254dSGiuliano Pochini 			data = ((u32)code[index] << 16) + code[index + 1];
430dd7b254dSGiuliano Pochini 			if (write_dsp(chip, data) < 0) {
431b5b4a41bSSudip Mukherjee 				dev_err(chip->card->dev,
432b5b4a41bSSudip Mukherjee 					"load_dsp: failed to write DSP data\n");
433dd7b254dSGiuliano Pochini 				return -EIO;
434dd7b254dSGiuliano Pochini 			}
435dd7b254dSGiuliano Pochini 		}
436dd7b254dSGiuliano Pochini 	}
437dd7b254dSGiuliano Pochini 
438dd7b254dSGiuliano Pochini 	if (write_dsp(chip, 0) < 0) {	/* We're done!!! */
439b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
440b5b4a41bSSudip Mukherjee 			"load_dsp: Failed to write final zero\n");
441dd7b254dSGiuliano Pochini 		return -EIO;
442dd7b254dSGiuliano Pochini 	}
443dd7b254dSGiuliano Pochini 	udelay(10);
444dd7b254dSGiuliano Pochini 
445dd7b254dSGiuliano Pochini 	for (i = 0; i < 5000; i++) {	/* Timeout is 100us * 5000 = 500ms */
446dd7b254dSGiuliano Pochini 		/* Wait for flag 4 - indicates that the DSP loaded OK */
447dd7b254dSGiuliano Pochini 		if (get_dsp_register(chip, CHI32_STATUS_REG) &
448dd7b254dSGiuliano Pochini 		    CHI32_STATUS_REG_HF4) {
449dd7b254dSGiuliano Pochini 			set_dsp_register(chip, CHI32_CONTROL_REG,
450dd7b254dSGiuliano Pochini 					 get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00);
451dd7b254dSGiuliano Pochini 
452dd7b254dSGiuliano Pochini 			if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) {
453b5b4a41bSSudip Mukherjee 				dev_err(chip->card->dev,
454b5b4a41bSSudip Mukherjee 					"load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n");
455dd7b254dSGiuliano Pochini 				return -EIO;
456dd7b254dSGiuliano Pochini 			}
457dd7b254dSGiuliano Pochini 
458dd7b254dSGiuliano Pochini 			if (write_dsp(chip, chip->comm_page_phys) < 0) {
459b5b4a41bSSudip Mukherjee 				dev_err(chip->card->dev,
460b5b4a41bSSudip Mukherjee 					"load_dsp: Failed to write comm page address\n");
461dd7b254dSGiuliano Pochini 				return -EIO;
462dd7b254dSGiuliano Pochini 			}
463dd7b254dSGiuliano Pochini 
464dd7b254dSGiuliano Pochini 			/* Get the serial number via slave mode.
465dd7b254dSGiuliano Pochini 			This is triggered by the SET_COMMPAGE_ADDR command.
466dd7b254dSGiuliano Pochini 			We don't actually use the serial number but we have to
467dd7b254dSGiuliano Pochini 			get it as part of the DSP init voodoo. */
468dd7b254dSGiuliano Pochini 			if (read_sn(chip) < 0) {
469b5b4a41bSSudip Mukherjee 				dev_err(chip->card->dev,
470b5b4a41bSSudip Mukherjee 					"load_dsp: Failed to read serial number\n");
471dd7b254dSGiuliano Pochini 				return -EIO;
472dd7b254dSGiuliano Pochini 			}
473dd7b254dSGiuliano Pochini 
474dd7b254dSGiuliano Pochini 			chip->dsp_code = code;		/* Show which DSP code loaded */
4753f6175ecSMark Brown 			chip->bad_board = false;	/* DSP OK */
476dd7b254dSGiuliano Pochini 			return 0;
477dd7b254dSGiuliano Pochini 		}
478dd7b254dSGiuliano Pochini 		udelay(100);
479dd7b254dSGiuliano Pochini 	}
480dd7b254dSGiuliano Pochini 
481b5b4a41bSSudip Mukherjee 	dev_err(chip->card->dev,
482b5b4a41bSSudip Mukherjee 		"load_dsp: DSP load timed out waiting for HF4\n");
483dd7b254dSGiuliano Pochini 	return -EIO;
484dd7b254dSGiuliano Pochini }
485dd7b254dSGiuliano Pochini 
486dd7b254dSGiuliano Pochini 
487dd7b254dSGiuliano Pochini 
488dd7b254dSGiuliano Pochini /* load_firmware takes care of loading the DSP and any ASIC code. */
load_firmware(struct echoaudio * chip)489dd7b254dSGiuliano Pochini static int load_firmware(struct echoaudio *chip)
490dd7b254dSGiuliano Pochini {
491dd7b254dSGiuliano Pochini 	const struct firmware *fw;
492dd7b254dSGiuliano Pochini 	int box_type, err;
493dd7b254dSGiuliano Pochini 
494c914f55fSMark Hills 	if (snd_BUG_ON(!chip->comm_page))
495da3cec35STakashi Iwai 		return -EPERM;
496dd7b254dSGiuliano Pochini 
497dd7b254dSGiuliano Pochini 	/* See if the ASIC is present and working - only if the DSP is already loaded */
498dd7b254dSGiuliano Pochini 	if (chip->dsp_code) {
499*549717fcSTakashi Iwai 		box_type = check_asic_status(chip);
500*549717fcSTakashi Iwai 		if (box_type >= 0)
501dd7b254dSGiuliano Pochini 			return box_type;
502dd7b254dSGiuliano Pochini 		/* ASIC check failed; force the DSP to reload */
503dd7b254dSGiuliano Pochini 		chip->dsp_code = NULL;
504dd7b254dSGiuliano Pochini 	}
505dd7b254dSGiuliano Pochini 
50619b50063SGiuliano Pochini 	err = get_firmware(&fw, chip, chip->dsp_code_to_load);
50719b50063SGiuliano Pochini 	if (err < 0)
508dd7b254dSGiuliano Pochini 		return err;
509dd7b254dSGiuliano Pochini 	err = load_dsp(chip, (u16 *)fw->data);
510e3690869SSudip Mukherjee 	free_firmware(fw, chip);
511dd7b254dSGiuliano Pochini 	if (err < 0)
512dd7b254dSGiuliano Pochini 		return err;
513dd7b254dSGiuliano Pochini 
514*549717fcSTakashi Iwai 	box_type = load_asic(chip);
515*549717fcSTakashi Iwai 	if (box_type < 0)
516dd7b254dSGiuliano Pochini 		return box_type;	/* error */
517dd7b254dSGiuliano Pochini 
518dd7b254dSGiuliano Pochini 	return box_type;
519dd7b254dSGiuliano Pochini }
520dd7b254dSGiuliano Pochini 
521dd7b254dSGiuliano Pochini 
522dd7b254dSGiuliano Pochini 
523dd7b254dSGiuliano Pochini /****************************************************************************
524dd7b254dSGiuliano Pochini 	Mixer functions
525dd7b254dSGiuliano Pochini  ****************************************************************************/
526dd7b254dSGiuliano Pochini 
527dd7b254dSGiuliano Pochini #if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
528dd7b254dSGiuliano Pochini 	defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
529dd7b254dSGiuliano Pochini 
530dd7b254dSGiuliano Pochini /* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
set_nominal_level(struct echoaudio * chip,u16 index,char consumer)531dd7b254dSGiuliano Pochini static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer)
532dd7b254dSGiuliano Pochini {
533da3cec35STakashi Iwai 	if (snd_BUG_ON(index >= num_busses_out(chip) + num_busses_in(chip)))
534da3cec35STakashi Iwai 		return -EINVAL;
535dd7b254dSGiuliano Pochini 
536dd7b254dSGiuliano Pochini 	/* Wait for the handshake (OK even if ASIC is not loaded) */
537dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
538dd7b254dSGiuliano Pochini 		return -EIO;
539dd7b254dSGiuliano Pochini 
540dd7b254dSGiuliano Pochini 	chip->nominal_level[index] = consumer;
541dd7b254dSGiuliano Pochini 
542dd7b254dSGiuliano Pochini 	if (consumer)
543dd7b254dSGiuliano Pochini 		chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index);
544dd7b254dSGiuliano Pochini 	else
545dd7b254dSGiuliano Pochini 		chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index);
546dd7b254dSGiuliano Pochini 
547dd7b254dSGiuliano Pochini 	return 0;
548dd7b254dSGiuliano Pochini }
549dd7b254dSGiuliano Pochini 
550dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
551dd7b254dSGiuliano Pochini 
552dd7b254dSGiuliano Pochini 
553dd7b254dSGiuliano Pochini 
554dd7b254dSGiuliano Pochini /* Set the gain for a single physical output channel (dB). */
set_output_gain(struct echoaudio * chip,u16 channel,s8 gain)555dd7b254dSGiuliano Pochini static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain)
556dd7b254dSGiuliano Pochini {
557da3cec35STakashi Iwai 	if (snd_BUG_ON(channel >= num_busses_out(chip)))
558da3cec35STakashi Iwai 		return -EINVAL;
559dd7b254dSGiuliano Pochini 
560dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
561dd7b254dSGiuliano Pochini 		return -EIO;
562dd7b254dSGiuliano Pochini 
563dd7b254dSGiuliano Pochini 	/* Save the new value */
564dd7b254dSGiuliano Pochini 	chip->output_gain[channel] = gain;
565dd7b254dSGiuliano Pochini 	chip->comm_page->line_out_level[channel] = gain;
566dd7b254dSGiuliano Pochini 	return 0;
567dd7b254dSGiuliano Pochini }
568dd7b254dSGiuliano Pochini 
569dd7b254dSGiuliano Pochini 
570dd7b254dSGiuliano Pochini 
571dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MONITOR
572dd7b254dSGiuliano Pochini /* Set the monitor level from an input bus to an output bus. */
set_monitor_gain(struct echoaudio * chip,u16 output,u16 input,s8 gain)573dd7b254dSGiuliano Pochini static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input,
574dd7b254dSGiuliano Pochini 			    s8 gain)
575dd7b254dSGiuliano Pochini {
576da3cec35STakashi Iwai 	if (snd_BUG_ON(output >= num_busses_out(chip) ||
577da3cec35STakashi Iwai 		    input >= num_busses_in(chip)))
578da3cec35STakashi Iwai 		return -EINVAL;
579dd7b254dSGiuliano Pochini 
580dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
581dd7b254dSGiuliano Pochini 		return -EIO;
582dd7b254dSGiuliano Pochini 
583dd7b254dSGiuliano Pochini 	chip->monitor_gain[output][input] = gain;
584dd7b254dSGiuliano Pochini 	chip->comm_page->monitors[monitor_index(chip, output, input)] = gain;
585dd7b254dSGiuliano Pochini 	return 0;
586dd7b254dSGiuliano Pochini }
587dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_MONITOR */
588dd7b254dSGiuliano Pochini 
589dd7b254dSGiuliano Pochini 
590dd7b254dSGiuliano Pochini /* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
update_output_line_level(struct echoaudio * chip)591dd7b254dSGiuliano Pochini static int update_output_line_level(struct echoaudio *chip)
592dd7b254dSGiuliano Pochini {
593dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
594dd7b254dSGiuliano Pochini 		return -EIO;
595dd7b254dSGiuliano Pochini 	clear_handshake(chip);
596dd7b254dSGiuliano Pochini 	return send_vector(chip, DSP_VC_UPDATE_OUTVOL);
597dd7b254dSGiuliano Pochini }
598dd7b254dSGiuliano Pochini 
599dd7b254dSGiuliano Pochini 
600dd7b254dSGiuliano Pochini 
601dd7b254dSGiuliano Pochini /* Tell the DSP to read and update input levels in comm page */
update_input_line_level(struct echoaudio * chip)602dd7b254dSGiuliano Pochini static int update_input_line_level(struct echoaudio *chip)
603dd7b254dSGiuliano Pochini {
604dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
605dd7b254dSGiuliano Pochini 		return -EIO;
606dd7b254dSGiuliano Pochini 	clear_handshake(chip);
607dd7b254dSGiuliano Pochini 	return send_vector(chip, DSP_VC_UPDATE_INGAIN);
608dd7b254dSGiuliano Pochini }
609dd7b254dSGiuliano Pochini 
610dd7b254dSGiuliano Pochini 
611dd7b254dSGiuliano Pochini 
612dd7b254dSGiuliano Pochini /* set_meters_on turns the meters on or off.  If meters are turned on, the DSP
613dd7b254dSGiuliano Pochini will write the meter and clock detect values to the comm page at about 30Hz */
set_meters_on(struct echoaudio * chip,char on)614dd7b254dSGiuliano Pochini static void set_meters_on(struct echoaudio *chip, char on)
615dd7b254dSGiuliano Pochini {
616dd7b254dSGiuliano Pochini 	if (on && !chip->meters_enabled) {
617dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_METERS_ON);
618dd7b254dSGiuliano Pochini 		chip->meters_enabled = 1;
619dd7b254dSGiuliano Pochini 	} else if (!on && chip->meters_enabled) {
620dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_METERS_OFF);
621dd7b254dSGiuliano Pochini 		chip->meters_enabled = 0;
622dd7b254dSGiuliano Pochini 		memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED,
623dd7b254dSGiuliano Pochini 		       DSP_MAXPIPES);
624dd7b254dSGiuliano Pochini 		memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED,
625dd7b254dSGiuliano Pochini 		       DSP_MAXPIPES);
626dd7b254dSGiuliano Pochini 	}
627dd7b254dSGiuliano Pochini }
628dd7b254dSGiuliano Pochini 
629dd7b254dSGiuliano Pochini 
630dd7b254dSGiuliano Pochini 
631dd7b254dSGiuliano Pochini /* Fill out an the given array using the current values in the comm page.
632dd7b254dSGiuliano Pochini Meters are written in the comm page by the DSP in this order:
633dd7b254dSGiuliano Pochini  Output busses
634dd7b254dSGiuliano Pochini  Input busses
635dd7b254dSGiuliano Pochini  Output pipes (vmixer cards only)
636dd7b254dSGiuliano Pochini 
637dd7b254dSGiuliano Pochini This function assumes there are no more than 16 in/out busses or pipes
638dd7b254dSGiuliano Pochini Meters is an array [3][16][2] of long. */
get_audio_meters(struct echoaudio * chip,long * meters)639dd7b254dSGiuliano Pochini static void get_audio_meters(struct echoaudio *chip, long *meters)
640dd7b254dSGiuliano Pochini {
641c08f0a92SOlof Johansson 	unsigned int i, m, n;
642dd7b254dSGiuliano Pochini 
643c08f0a92SOlof Johansson 	for (i = 0 ; i < 96; i++)
644c08f0a92SOlof Johansson 		meters[i] = 0;
645c08f0a92SOlof Johansson 
646c08f0a92SOlof Johansson 	for (m = 0, n = 0, i = 0; i < num_busses_out(chip); i++, m++) {
647dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->vu_meter[m];
648dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->peak_meter[m];
649dd7b254dSGiuliano Pochini 	}
650dd7b254dSGiuliano Pochini 
651dd7b254dSGiuliano Pochini #ifdef ECHOCARD_ECHO3G
652dd7b254dSGiuliano Pochini 	m = E3G_MAX_OUTPUTS;	/* Skip unused meters */
653dd7b254dSGiuliano Pochini #endif
654dd7b254dSGiuliano Pochini 
655c08f0a92SOlof Johansson 	for (n = 32, i = 0; i < num_busses_in(chip); i++, m++) {
656dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->vu_meter[m];
657dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->peak_meter[m];
658dd7b254dSGiuliano Pochini 	}
659dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_VMIXER
660c08f0a92SOlof Johansson 	for (n = 64, i = 0; i < num_pipes_out(chip); i++, m++) {
661dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->vu_meter[m];
662dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->peak_meter[m];
663dd7b254dSGiuliano Pochini 	}
664dd7b254dSGiuliano Pochini #endif
665dd7b254dSGiuliano Pochini }
666dd7b254dSGiuliano Pochini 
667dd7b254dSGiuliano Pochini 
668dd7b254dSGiuliano Pochini 
restore_dsp_rettings(struct echoaudio * chip)669dd7b254dSGiuliano Pochini static int restore_dsp_rettings(struct echoaudio *chip)
670dd7b254dSGiuliano Pochini {
67147b5d028SGiuliano Pochini 	int i, o, err;
672dd7b254dSGiuliano Pochini 
673*549717fcSTakashi Iwai 	err = check_asic_status(chip);
674*549717fcSTakashi Iwai 	if (err < 0)
675dd7b254dSGiuliano Pochini 		return err;
676dd7b254dSGiuliano Pochini 
67747b5d028SGiuliano Pochini 	/* Gina20/Darla20 only. Should be harmless for other cards. */
678dd7b254dSGiuliano Pochini 	chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF;
679dd7b254dSGiuliano Pochini 	chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF;
6802a833a02STakashi Iwai 	chip->comm_page->handshake = cpu_to_le32(0xffffffff);
681dd7b254dSGiuliano Pochini 
68247b5d028SGiuliano Pochini 	/* Restore output busses */
68347b5d028SGiuliano Pochini 	for (i = 0; i < num_busses_out(chip); i++) {
68447b5d028SGiuliano Pochini 		err = set_output_gain(chip, i, chip->output_gain[i]);
68547b5d028SGiuliano Pochini 		if (err < 0)
68647b5d028SGiuliano Pochini 			return err;
68747b5d028SGiuliano Pochini 	}
68847b5d028SGiuliano Pochini 
68947b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_VMIXER
69047b5d028SGiuliano Pochini 	for (i = 0; i < num_pipes_out(chip); i++)
69147b5d028SGiuliano Pochini 		for (o = 0; o < num_busses_out(chip); o++) {
69247b5d028SGiuliano Pochini 			err = set_vmixer_gain(chip, o, i,
69347b5d028SGiuliano Pochini 						chip->vmixer_gain[o][i]);
69447b5d028SGiuliano Pochini 			if (err < 0)
69547b5d028SGiuliano Pochini 				return err;
69647b5d028SGiuliano Pochini 		}
69747b5d028SGiuliano Pochini 	if (update_vmixer_level(chip) < 0)
69847b5d028SGiuliano Pochini 		return -EIO;
69947b5d028SGiuliano Pochini #endif /* ECHOCARD_HAS_VMIXER */
70047b5d028SGiuliano Pochini 
70147b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_MONITOR
70247b5d028SGiuliano Pochini 	for (o = 0; o < num_busses_out(chip); o++)
70347b5d028SGiuliano Pochini 		for (i = 0; i < num_busses_in(chip); i++) {
70447b5d028SGiuliano Pochini 			err = set_monitor_gain(chip, o, i,
70547b5d028SGiuliano Pochini 						chip->monitor_gain[o][i]);
70647b5d028SGiuliano Pochini 			if (err < 0)
70747b5d028SGiuliano Pochini 				return err;
70847b5d028SGiuliano Pochini 		}
70947b5d028SGiuliano Pochini #endif /* ECHOCARD_HAS_MONITOR */
71047b5d028SGiuliano Pochini 
71147b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_INPUT_GAIN
71247b5d028SGiuliano Pochini 	for (i = 0; i < num_busses_in(chip); i++) {
71347b5d028SGiuliano Pochini 		err = set_input_gain(chip, i, chip->input_gain[i]);
71447b5d028SGiuliano Pochini 		if (err < 0)
71547b5d028SGiuliano Pochini 			return err;
71647b5d028SGiuliano Pochini 	}
71747b5d028SGiuliano Pochini #endif /* ECHOCARD_HAS_INPUT_GAIN */
71847b5d028SGiuliano Pochini 
71947b5d028SGiuliano Pochini 	err = update_output_line_level(chip);
72047b5d028SGiuliano Pochini 	if (err < 0)
721dd7b254dSGiuliano Pochini 		return err;
722dd7b254dSGiuliano Pochini 
72347b5d028SGiuliano Pochini 	err = update_input_line_level(chip);
72447b5d028SGiuliano Pochini 	if (err < 0)
72547b5d028SGiuliano Pochini 		return err;
72647b5d028SGiuliano Pochini 
72747b5d028SGiuliano Pochini 	err = set_sample_rate(chip, chip->sample_rate);
72847b5d028SGiuliano Pochini 	if (err < 0)
72947b5d028SGiuliano Pochini 		return err;
73047b5d028SGiuliano Pochini 
73147b5d028SGiuliano Pochini 	if (chip->meters_enabled) {
73247b5d028SGiuliano Pochini 		err = send_vector(chip, DSP_VC_METERS_ON);
73347b5d028SGiuliano Pochini 		if (err < 0)
73447b5d028SGiuliano Pochini 			return err;
73547b5d028SGiuliano Pochini 	}
73647b5d028SGiuliano Pochini 
73747b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
73847b5d028SGiuliano Pochini 	if (set_digital_mode(chip, chip->digital_mode) < 0)
739dd7b254dSGiuliano Pochini 		return -EIO;
74047b5d028SGiuliano Pochini #endif
74147b5d028SGiuliano Pochini 
74247b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
74347b5d028SGiuliano Pochini 	if (set_professional_spdif(chip, chip->professional_spdif) < 0)
74447b5d028SGiuliano Pochini 		return -EIO;
74547b5d028SGiuliano Pochini #endif
74647b5d028SGiuliano Pochini 
74747b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_PHANTOM_POWER
74847b5d028SGiuliano Pochini 	if (set_phantom_power(chip, chip->phantom_power) < 0)
74947b5d028SGiuliano Pochini 		return -EIO;
75047b5d028SGiuliano Pochini #endif
751dd7b254dSGiuliano Pochini 
752dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
75347b5d028SGiuliano Pochini 	/* set_input_clock() also restores automute setting */
754dd7b254dSGiuliano Pochini 	if (set_input_clock(chip, chip->input_clock) < 0)
755dd7b254dSGiuliano Pochini 		return -EIO;
756dd7b254dSGiuliano Pochini #endif
757dd7b254dSGiuliano Pochini 
758dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
759dd7b254dSGiuliano Pochini 	if (set_output_clock(chip, chip->output_clock) < 0)
760dd7b254dSGiuliano Pochini 		return -EIO;
761dd7b254dSGiuliano Pochini #endif
762dd7b254dSGiuliano Pochini 
763dd7b254dSGiuliano Pochini 	if (wait_handshake(chip) < 0)
764dd7b254dSGiuliano Pochini 		return -EIO;
765dd7b254dSGiuliano Pochini 	clear_handshake(chip);
76647b5d028SGiuliano Pochini 	if (send_vector(chip, DSP_VC_UPDATE_FLAGS) < 0)
76747b5d028SGiuliano Pochini 		return -EIO;
768dd7b254dSGiuliano Pochini 
76947b5d028SGiuliano Pochini 	return 0;
770dd7b254dSGiuliano Pochini }
771dd7b254dSGiuliano Pochini 
772dd7b254dSGiuliano Pochini 
773dd7b254dSGiuliano Pochini 
774dd7b254dSGiuliano Pochini /****************************************************************************
775dd7b254dSGiuliano Pochini 	Transport functions
776dd7b254dSGiuliano Pochini  ****************************************************************************/
777dd7b254dSGiuliano Pochini 
778dd7b254dSGiuliano Pochini /* set_audio_format() sets the format of the audio data in host memory for
779dd7b254dSGiuliano Pochini this pipe.  Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
780dd7b254dSGiuliano Pochini but they are here because they are just mono while capturing */
set_audio_format(struct echoaudio * chip,u16 pipe_index,const struct audioformat * format)781dd7b254dSGiuliano Pochini static void set_audio_format(struct echoaudio *chip, u16 pipe_index,
782dd7b254dSGiuliano Pochini 			     const struct audioformat *format)
783dd7b254dSGiuliano Pochini {
784dd7b254dSGiuliano Pochini 	u16 dsp_format;
785dd7b254dSGiuliano Pochini 
786dd7b254dSGiuliano Pochini 	dsp_format = DSP_AUDIOFORM_SS_16LE;
787dd7b254dSGiuliano Pochini 
788dd7b254dSGiuliano Pochini 	/* Look for super-interleave (no big-endian and 8 bits) */
789dd7b254dSGiuliano Pochini 	if (format->interleave > 2) {
790dd7b254dSGiuliano Pochini 		switch (format->bits_per_sample) {
791dd7b254dSGiuliano Pochini 		case 16:
792dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE;
793dd7b254dSGiuliano Pochini 			break;
794dd7b254dSGiuliano Pochini 		case 24:
795dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE;
796dd7b254dSGiuliano Pochini 			break;
797dd7b254dSGiuliano Pochini 		case 32:
798dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE;
799dd7b254dSGiuliano Pochini 			break;
800dd7b254dSGiuliano Pochini 		}
801dd7b254dSGiuliano Pochini 		dsp_format |= format->interleave;
802dd7b254dSGiuliano Pochini 	} else if (format->data_are_bigendian) {
803dd7b254dSGiuliano Pochini 		/* For big-endian data, only 32 bit samples are supported */
804dd7b254dSGiuliano Pochini 		switch (format->interleave) {
805dd7b254dSGiuliano Pochini 		case 1:
806dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_MM_32BE;
807dd7b254dSGiuliano Pochini 			break;
808dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
809dd7b254dSGiuliano Pochini 		case 2:
810dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_SS_32BE;
811dd7b254dSGiuliano Pochini 			break;
812dd7b254dSGiuliano Pochini #endif
813dd7b254dSGiuliano Pochini 		}
814dd7b254dSGiuliano Pochini 	} else if (format->interleave == 1 &&
815dd7b254dSGiuliano Pochini 		   format->bits_per_sample == 32 && !format->mono_to_stereo) {
816dd7b254dSGiuliano Pochini 		/* 32 bit little-endian mono->mono case */
817dd7b254dSGiuliano Pochini 		dsp_format = DSP_AUDIOFORM_MM_32LE;
818dd7b254dSGiuliano Pochini 	} else {
819dd7b254dSGiuliano Pochini 		/* Handle the other little-endian formats */
820dd7b254dSGiuliano Pochini 		switch (format->bits_per_sample) {
821dd7b254dSGiuliano Pochini 		case 8:
822dd7b254dSGiuliano Pochini 			if (format->interleave == 2)
823dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_SS_8;
824dd7b254dSGiuliano Pochini 			else
825dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_MS_8;
826dd7b254dSGiuliano Pochini 			break;
827dd7b254dSGiuliano Pochini 		default:
828dd7b254dSGiuliano Pochini 		case 16:
829dd7b254dSGiuliano Pochini 			if (format->interleave == 2)
830dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_SS_16LE;
831dd7b254dSGiuliano Pochini 			else
832dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_MS_16LE;
833dd7b254dSGiuliano Pochini 			break;
834dd7b254dSGiuliano Pochini 		case 24:
835dd7b254dSGiuliano Pochini 			if (format->interleave == 2)
836dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_SS_24LE;
837dd7b254dSGiuliano Pochini 			else
838dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_MS_24LE;
839dd7b254dSGiuliano Pochini 			break;
840dd7b254dSGiuliano Pochini 		case 32:
841dd7b254dSGiuliano Pochini 			if (format->interleave == 2)
842dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_SS_32LE;
843dd7b254dSGiuliano Pochini 			else
844dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_MS_32LE;
845dd7b254dSGiuliano Pochini 			break;
846dd7b254dSGiuliano Pochini 		}
847dd7b254dSGiuliano Pochini 	}
848b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev,
849b5b4a41bSSudip Mukherjee 		 "set_audio_format[%d] = %x\n", pipe_index, dsp_format);
850dd7b254dSGiuliano Pochini 	chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format);
851dd7b254dSGiuliano Pochini }
852dd7b254dSGiuliano Pochini 
853dd7b254dSGiuliano Pochini 
854dd7b254dSGiuliano Pochini 
855dd7b254dSGiuliano Pochini /* start_transport starts transport for a set of pipes.
856dd7b254dSGiuliano Pochini The bits 1 in channel_mask specify what pipes to start. Only the bit of the
857dd7b254dSGiuliano Pochini first channel must be set, regardless its interleave.
858dd7b254dSGiuliano Pochini Same thing for pause_ and stop_ -trasport below. */
start_transport(struct echoaudio * chip,u32 channel_mask,u32 cyclic_mask)859dd7b254dSGiuliano Pochini static int start_transport(struct echoaudio *chip, u32 channel_mask,
860dd7b254dSGiuliano Pochini 			   u32 cyclic_mask)
861dd7b254dSGiuliano Pochini {
862dd7b254dSGiuliano Pochini 
863dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
864dd7b254dSGiuliano Pochini 		return -EIO;
865dd7b254dSGiuliano Pochini 
866dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_start |= cpu_to_le32(channel_mask);
867dd7b254dSGiuliano Pochini 
868dd7b254dSGiuliano Pochini 	if (chip->comm_page->cmd_start) {
869dd7b254dSGiuliano Pochini 		clear_handshake(chip);
870dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_START_TRANSFER);
871dd7b254dSGiuliano Pochini 		if (wait_handshake(chip))
872dd7b254dSGiuliano Pochini 			return -EIO;
873dd7b254dSGiuliano Pochini 		/* Keep track of which pipes are transporting */
874dd7b254dSGiuliano Pochini 		chip->active_mask |= channel_mask;
875dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_start = 0;
876dd7b254dSGiuliano Pochini 		return 0;
877dd7b254dSGiuliano Pochini 	}
878dd7b254dSGiuliano Pochini 
879b5b4a41bSSudip Mukherjee 	dev_err(chip->card->dev, "start_transport: No pipes to start!\n");
880dd7b254dSGiuliano Pochini 	return -EINVAL;
881dd7b254dSGiuliano Pochini }
882dd7b254dSGiuliano Pochini 
883dd7b254dSGiuliano Pochini 
884dd7b254dSGiuliano Pochini 
pause_transport(struct echoaudio * chip,u32 channel_mask)885dd7b254dSGiuliano Pochini static int pause_transport(struct echoaudio *chip, u32 channel_mask)
886dd7b254dSGiuliano Pochini {
887dd7b254dSGiuliano Pochini 
888dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
889dd7b254dSGiuliano Pochini 		return -EIO;
890dd7b254dSGiuliano Pochini 
891dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
892dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_reset = 0;
893dd7b254dSGiuliano Pochini 	if (chip->comm_page->cmd_stop) {
894dd7b254dSGiuliano Pochini 		clear_handshake(chip);
895dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_STOP_TRANSFER);
896dd7b254dSGiuliano Pochini 		if (wait_handshake(chip))
897dd7b254dSGiuliano Pochini 			return -EIO;
898dd7b254dSGiuliano Pochini 		/* Keep track of which pipes are transporting */
899dd7b254dSGiuliano Pochini 		chip->active_mask &= ~channel_mask;
900dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_stop = 0;
901dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_reset = 0;
902dd7b254dSGiuliano Pochini 		return 0;
903dd7b254dSGiuliano Pochini 	}
904dd7b254dSGiuliano Pochini 
905f688a0dfSMark Hills 	dev_dbg(chip->card->dev, "pause_transport: No pipes to stop!\n");
906dd7b254dSGiuliano Pochini 	return 0;
907dd7b254dSGiuliano Pochini }
908dd7b254dSGiuliano Pochini 
909dd7b254dSGiuliano Pochini 
910dd7b254dSGiuliano Pochini 
stop_transport(struct echoaudio * chip,u32 channel_mask)911dd7b254dSGiuliano Pochini static int stop_transport(struct echoaudio *chip, u32 channel_mask)
912dd7b254dSGiuliano Pochini {
913dd7b254dSGiuliano Pochini 
914dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
915dd7b254dSGiuliano Pochini 		return -EIO;
916dd7b254dSGiuliano Pochini 
917dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
918dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask);
919dd7b254dSGiuliano Pochini 	if (chip->comm_page->cmd_reset) {
920dd7b254dSGiuliano Pochini 		clear_handshake(chip);
921dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_STOP_TRANSFER);
922dd7b254dSGiuliano Pochini 		if (wait_handshake(chip))
923dd7b254dSGiuliano Pochini 			return -EIO;
924dd7b254dSGiuliano Pochini 		/* Keep track of which pipes are transporting */
925dd7b254dSGiuliano Pochini 		chip->active_mask &= ~channel_mask;
926dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_stop = 0;
927dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_reset = 0;
928dd7b254dSGiuliano Pochini 		return 0;
929dd7b254dSGiuliano Pochini 	}
930dd7b254dSGiuliano Pochini 
931f688a0dfSMark Hills 	dev_dbg(chip->card->dev, "stop_transport: No pipes to stop!\n");
932dd7b254dSGiuliano Pochini 	return 0;
933dd7b254dSGiuliano Pochini }
934dd7b254dSGiuliano Pochini 
935dd7b254dSGiuliano Pochini 
936dd7b254dSGiuliano Pochini 
is_pipe_allocated(struct echoaudio * chip,u16 pipe_index)937dd7b254dSGiuliano Pochini static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index)
938dd7b254dSGiuliano Pochini {
939dd7b254dSGiuliano Pochini 	return (chip->pipe_alloc_mask & (1 << pipe_index));
940dd7b254dSGiuliano Pochini }
941dd7b254dSGiuliano Pochini 
942dd7b254dSGiuliano Pochini 
943dd7b254dSGiuliano Pochini 
944dd7b254dSGiuliano Pochini /* Stops everything and turns off the DSP. All pipes should be already
945dd7b254dSGiuliano Pochini stopped and unallocated. */
rest_in_peace(struct echoaudio * chip)946dd7b254dSGiuliano Pochini static int rest_in_peace(struct echoaudio *chip)
947dd7b254dSGiuliano Pochini {
948dd7b254dSGiuliano Pochini 
949dd7b254dSGiuliano Pochini 	/* Stops all active pipes (just to be sure) */
950dd7b254dSGiuliano Pochini 	stop_transport(chip, chip->active_mask);
951dd7b254dSGiuliano Pochini 
9523f6175ecSMark Brown 	set_meters_on(chip, false);
953dd7b254dSGiuliano Pochini 
954dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MIDI
9553f6175ecSMark Brown 	enable_midi_input(chip, false);
956dd7b254dSGiuliano Pochini #endif
957dd7b254dSGiuliano Pochini 
958dd7b254dSGiuliano Pochini 	/* Go to sleep */
959dd7b254dSGiuliano Pochini 	if (chip->dsp_code) {
960dd7b254dSGiuliano Pochini 		/* Make load_firmware do a complete reload */
961dd7b254dSGiuliano Pochini 		chip->dsp_code = NULL;
962dd7b254dSGiuliano Pochini 		/* Put the DSP to sleep */
963dd7b254dSGiuliano Pochini 		return send_vector(chip, DSP_VC_GO_COMATOSE);
964dd7b254dSGiuliano Pochini 	}
965dd7b254dSGiuliano Pochini 	return 0;
966dd7b254dSGiuliano Pochini }
967dd7b254dSGiuliano Pochini 
968dd7b254dSGiuliano Pochini 
969dd7b254dSGiuliano Pochini 
970dd7b254dSGiuliano Pochini /* Fills the comm page with default values */
init_dsp_comm_page(struct echoaudio * chip)971dd7b254dSGiuliano Pochini static int init_dsp_comm_page(struct echoaudio *chip)
972dd7b254dSGiuliano Pochini {
973dd7b254dSGiuliano Pochini 	/* Check if the compiler added extra padding inside the structure */
974dd7b254dSGiuliano Pochini 	if (offsetof(struct comm_page, midi_output) != 0xbe0) {
975b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
976b5b4a41bSSudip Mukherjee 			"init_dsp_comm_page() - Invalid struct comm_page structure\n");
977dd7b254dSGiuliano Pochini 		return -EPERM;
978dd7b254dSGiuliano Pochini 	}
979dd7b254dSGiuliano Pochini 
980dd7b254dSGiuliano Pochini 	/* Init all the basic stuff */
981dd7b254dSGiuliano Pochini 	chip->card_name = ECHOCARD_NAME;
9823f6175ecSMark Brown 	chip->bad_board = true;	/* Set true until DSP loaded */
983dd7b254dSGiuliano Pochini 	chip->dsp_code = NULL;	/* Current DSP code not loaded */
9843f6175ecSMark Brown 	chip->asic_loaded = false;
985dd7b254dSGiuliano Pochini 	memset(chip->comm_page, 0, sizeof(struct comm_page));
986dd7b254dSGiuliano Pochini 
987dd7b254dSGiuliano Pochini 	/* Init the comm page */
988dd7b254dSGiuliano Pochini 	chip->comm_page->comm_size =
989e930e995SHarvey Harrison 		cpu_to_le32(sizeof(struct comm_page));
9902a833a02STakashi Iwai 	chip->comm_page->handshake = cpu_to_le32(0xffffffff);
991dd7b254dSGiuliano Pochini 	chip->comm_page->midi_out_free_count =
992e930e995SHarvey Harrison 		cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE);
993e930e995SHarvey Harrison 	chip->comm_page->sample_rate = cpu_to_le32(44100);
994dd7b254dSGiuliano Pochini 
995dd7b254dSGiuliano Pochini 	/* Set line levels so we don't blast any inputs on startup */
996dd7b254dSGiuliano Pochini 	memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE);
997dd7b254dSGiuliano Pochini 	memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE);
998dd7b254dSGiuliano Pochini 
999dd7b254dSGiuliano Pochini 	return 0;
1000dd7b254dSGiuliano Pochini }
1001dd7b254dSGiuliano Pochini 
1002dd7b254dSGiuliano Pochini 
1003dd7b254dSGiuliano Pochini 
100447b5d028SGiuliano Pochini /* This function initializes the chip structure with default values, ie. all
100547b5d028SGiuliano Pochini  * muted and internal clock source. Then it copies the settings to the DSP.
100647b5d028SGiuliano Pochini  * This MUST be called after the DSP is up and running !
100747b5d028SGiuliano Pochini  */
init_line_levels(struct echoaudio * chip)1008dd7b254dSGiuliano Pochini static int init_line_levels(struct echoaudio *chip)
1009dd7b254dSGiuliano Pochini {
101047b5d028SGiuliano Pochini 	memset(chip->output_gain, ECHOGAIN_MUTED, sizeof(chip->output_gain));
101147b5d028SGiuliano Pochini 	memset(chip->input_gain, ECHOGAIN_MUTED, sizeof(chip->input_gain));
101247b5d028SGiuliano Pochini 	memset(chip->monitor_gain, ECHOGAIN_MUTED, sizeof(chip->monitor_gain));
101347b5d028SGiuliano Pochini 	memset(chip->vmixer_gain, ECHOGAIN_MUTED, sizeof(chip->vmixer_gain));
101447b5d028SGiuliano Pochini 	chip->input_clock = ECHO_CLOCK_INTERNAL;
101547b5d028SGiuliano Pochini 	chip->output_clock = ECHO_CLOCK_WORD;
101647b5d028SGiuliano Pochini 	chip->sample_rate = 44100;
101747b5d028SGiuliano Pochini 	return restore_dsp_rettings(chip);
1018dd7b254dSGiuliano Pochini }
1019dd7b254dSGiuliano Pochini 
1020dd7b254dSGiuliano Pochini 
1021dd7b254dSGiuliano Pochini 
1022dd7b254dSGiuliano Pochini /* This is low level part of the interrupt handler.
1023dd7b254dSGiuliano Pochini It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
1024dd7b254dSGiuliano Pochini of midi data in the input queue. */
service_irq(struct echoaudio * chip)1025dd7b254dSGiuliano Pochini static int service_irq(struct echoaudio *chip)
1026dd7b254dSGiuliano Pochini {
1027dd7b254dSGiuliano Pochini 	int st;
1028dd7b254dSGiuliano Pochini 
1029dd7b254dSGiuliano Pochini 	/* Read the DSP status register and see if this DSP generated this interrupt */
1030dd7b254dSGiuliano Pochini 	if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) {
1031dd7b254dSGiuliano Pochini 		st = 0;
1032dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MIDI
1033dd7b254dSGiuliano Pochini 		/* Get and parse midi data if present */
1034dd7b254dSGiuliano Pochini 		if (chip->comm_page->midi_input[0])	/* The count is at index 0 */
1035dd7b254dSGiuliano Pochini 			st = midi_service_irq(chip);	/* Returns how many midi bytes we received */
1036dd7b254dSGiuliano Pochini #endif
1037dd7b254dSGiuliano Pochini 		/* Clear the hardware interrupt */
1038dd7b254dSGiuliano Pochini 		chip->comm_page->midi_input[0] = 0;
1039dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_ACK_INT);
1040dd7b254dSGiuliano Pochini 		return st;
1041dd7b254dSGiuliano Pochini 	}
1042dd7b254dSGiuliano Pochini 	return -1;
1043dd7b254dSGiuliano Pochini }
1044dd7b254dSGiuliano Pochini 
1045dd7b254dSGiuliano Pochini 
1046dd7b254dSGiuliano Pochini 
1047dd7b254dSGiuliano Pochini 
1048dd7b254dSGiuliano Pochini /******************************************************************************
1049dd7b254dSGiuliano Pochini 	Functions for opening and closing pipes
1050dd7b254dSGiuliano Pochini  ******************************************************************************/
1051dd7b254dSGiuliano Pochini 
1052dd7b254dSGiuliano Pochini /* allocate_pipes is used to reserve audio pipes for your exclusive use.
1053dd7b254dSGiuliano Pochini The call will fail if some pipes are already allocated. */
allocate_pipes(struct echoaudio * chip,struct audiopipe * pipe,int pipe_index,int interleave)1054dd7b254dSGiuliano Pochini static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe,
1055dd7b254dSGiuliano Pochini 			  int pipe_index, int interleave)
1056dd7b254dSGiuliano Pochini {
1057dd7b254dSGiuliano Pochini 	int i;
1058dd7b254dSGiuliano Pochini 	u32 channel_mask;
1059dd7b254dSGiuliano Pochini 
1060b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev,
1061b5b4a41bSSudip Mukherjee 		"allocate_pipes: ch=%d int=%d\n", pipe_index, interleave);
1062dd7b254dSGiuliano Pochini 
1063dd7b254dSGiuliano Pochini 	if (chip->bad_board)
1064dd7b254dSGiuliano Pochini 		return -EIO;
1065dd7b254dSGiuliano Pochini 
1066dd7b254dSGiuliano Pochini 	for (channel_mask = i = 0; i < interleave; i++)
1067dd7b254dSGiuliano Pochini 		channel_mask |= 1 << (pipe_index + i);
1068dd7b254dSGiuliano Pochini 	if (chip->pipe_alloc_mask & channel_mask) {
1069b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
1070b5b4a41bSSudip Mukherjee 			"allocate_pipes: channel already open\n");
1071dd7b254dSGiuliano Pochini 		return -EAGAIN;
1072dd7b254dSGiuliano Pochini 	}
1073dd7b254dSGiuliano Pochini 
1074dd7b254dSGiuliano Pochini 	chip->comm_page->position[pipe_index] = 0;
1075dd7b254dSGiuliano Pochini 	chip->pipe_alloc_mask |= channel_mask;
10769aa892d2SWolfram Sang 	/* This driver uses cyclic buffers only */
1077dd7b254dSGiuliano Pochini 	chip->pipe_cyclic_mask |= channel_mask;
1078dd7b254dSGiuliano Pochini 	pipe->index = pipe_index;
1079dd7b254dSGiuliano Pochini 	pipe->interleave = interleave;
1080dd7b254dSGiuliano Pochini 	pipe->state = PIPE_STATE_STOPPED;
1081dd7b254dSGiuliano Pochini 
1082dd7b254dSGiuliano Pochini 	/* The counter register is where the DSP writes the 32 bit DMA
1083dd7b254dSGiuliano Pochini 	position for a pipe.  The DSP is constantly updating this value as
1084dd7b254dSGiuliano Pochini 	it moves data. The DMA counter is in units of bytes, not samples. */
10852a833a02STakashi Iwai 	pipe->dma_counter = (__le32 *)&chip->comm_page->position[pipe_index];
1086dd7b254dSGiuliano Pochini 	*pipe->dma_counter = 0;
1087dd7b254dSGiuliano Pochini 	return pipe_index;
1088dd7b254dSGiuliano Pochini }
1089dd7b254dSGiuliano Pochini 
1090dd7b254dSGiuliano Pochini 
1091dd7b254dSGiuliano Pochini 
free_pipes(struct echoaudio * chip,struct audiopipe * pipe)1092dd7b254dSGiuliano Pochini static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe)
1093dd7b254dSGiuliano Pochini {
1094dd7b254dSGiuliano Pochini 	u32 channel_mask;
1095dd7b254dSGiuliano Pochini 	int i;
1096dd7b254dSGiuliano Pochini 
1097da3cec35STakashi Iwai 	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe->index)))
1098da3cec35STakashi Iwai 		return -EINVAL;
1099da3cec35STakashi Iwai 	if (snd_BUG_ON(pipe->state != PIPE_STATE_STOPPED))
1100da3cec35STakashi Iwai 		return -EINVAL;
1101dd7b254dSGiuliano Pochini 
1102dd7b254dSGiuliano Pochini 	for (channel_mask = i = 0; i < pipe->interleave; i++)
1103dd7b254dSGiuliano Pochini 		channel_mask |= 1 << (pipe->index + i);
1104dd7b254dSGiuliano Pochini 
1105dd7b254dSGiuliano Pochini 	chip->pipe_alloc_mask &= ~channel_mask;
1106dd7b254dSGiuliano Pochini 	chip->pipe_cyclic_mask &= ~channel_mask;
1107dd7b254dSGiuliano Pochini 	return 0;
1108dd7b254dSGiuliano Pochini }
1109dd7b254dSGiuliano Pochini 
1110dd7b254dSGiuliano Pochini 
1111dd7b254dSGiuliano Pochini 
1112dd7b254dSGiuliano Pochini /******************************************************************************
1113dd7b254dSGiuliano Pochini 	Functions for managing the scatter-gather list
1114dd7b254dSGiuliano Pochini ******************************************************************************/
1115dd7b254dSGiuliano Pochini 
sglist_init(struct echoaudio * chip,struct audiopipe * pipe)1116dd7b254dSGiuliano Pochini static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe)
1117dd7b254dSGiuliano Pochini {
1118dd7b254dSGiuliano Pochini 	pipe->sglist_head = 0;
1119dd7b254dSGiuliano Pochini 	memset(pipe->sgpage.area, 0, PAGE_SIZE);
1120dd7b254dSGiuliano Pochini 	chip->comm_page->sglist_addr[pipe->index].addr =
1121dd7b254dSGiuliano Pochini 		cpu_to_le32(pipe->sgpage.addr);
1122dd7b254dSGiuliano Pochini 	return 0;
1123dd7b254dSGiuliano Pochini }
1124dd7b254dSGiuliano Pochini 
1125dd7b254dSGiuliano Pochini 
1126dd7b254dSGiuliano Pochini 
sglist_add_mapping(struct echoaudio * chip,struct audiopipe * pipe,dma_addr_t address,size_t length)1127dd7b254dSGiuliano Pochini static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe,
1128dd7b254dSGiuliano Pochini 				dma_addr_t address, size_t length)
1129dd7b254dSGiuliano Pochini {
1130dd7b254dSGiuliano Pochini 	int head = pipe->sglist_head;
1131dd7b254dSGiuliano Pochini 	struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area;
1132dd7b254dSGiuliano Pochini 
1133dd7b254dSGiuliano Pochini 	if (head < MAX_SGLIST_ENTRIES - 1) {
1134dd7b254dSGiuliano Pochini 		list[head].addr = cpu_to_le32(address);
1135dd7b254dSGiuliano Pochini 		list[head].size = cpu_to_le32(length);
1136dd7b254dSGiuliano Pochini 		pipe->sglist_head++;
1137dd7b254dSGiuliano Pochini 	} else {
1138b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev, "SGlist: too many fragments\n");
1139dd7b254dSGiuliano Pochini 		return -ENOMEM;
1140dd7b254dSGiuliano Pochini 	}
1141dd7b254dSGiuliano Pochini 	return 0;
1142dd7b254dSGiuliano Pochini }
1143dd7b254dSGiuliano Pochini 
1144dd7b254dSGiuliano Pochini 
1145dd7b254dSGiuliano Pochini 
sglist_add_irq(struct echoaudio * chip,struct audiopipe * pipe)1146dd7b254dSGiuliano Pochini static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe)
1147dd7b254dSGiuliano Pochini {
1148dd7b254dSGiuliano Pochini 	return sglist_add_mapping(chip, pipe, 0, 0);
1149dd7b254dSGiuliano Pochini }
1150dd7b254dSGiuliano Pochini 
1151dd7b254dSGiuliano Pochini 
1152dd7b254dSGiuliano Pochini 
sglist_wrap(struct echoaudio * chip,struct audiopipe * pipe)1153dd7b254dSGiuliano Pochini static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe)
1154dd7b254dSGiuliano Pochini {
1155dd7b254dSGiuliano Pochini 	return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0);
1156dd7b254dSGiuliano Pochini }
1157