1198090Srdivacky/****************************************************************************
2198090Srdivacky
3198090Srdivacky   Copyright Echo Digital Audio Corporation (c) 1998 - 2004
4198090Srdivacky   All rights reserved
5198090Srdivacky   www.echoaudio.com
6198090Srdivacky
7198090Srdivacky   This file is part of Echo Digital Audio's generic driver library.
8198090Srdivacky
9198090Srdivacky   Echo Digital Audio's generic driver library is free software;
10198090Srdivacky   you can redistribute it and/or modify it under the terms of
11198090Srdivacky   the GNU General Public License as published by the Free Software
12198090Srdivacky   Foundation.
13198090Srdivacky
14198090Srdivacky   This program is distributed in the hope that it will be useful,
15198090Srdivacky   but WITHOUT ANY WARRANTY; without even the implied warranty of
16198090Srdivacky   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17198090Srdivacky   GNU General Public License for more details.
18198090Srdivacky
19198090Srdivacky   You should have received a copy of the GNU General Public License
20198090Srdivacky   along with this program; if not, write to the Free Software
21198090Srdivacky   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22198090Srdivacky   MA  02111-1307, USA.
23198090Srdivacky
24198090Srdivacky   *************************************************************************
25198090Srdivacky
26198090Srdivacky Translation from C++ and adaptation for use in ALSA-Driver
27198090Srdivacky were made by Giuliano Pochini <pochini@shiny.it>
28198090Srdivacky
29198090Srdivacky****************************************************************************/
30198090Srdivacky
31198090Srdivacky
32198090Srdivacky/* These functions are common for Gina24, Layla24 and Mona cards */
33198090Srdivacky
34198090Srdivacky
35198090Srdivacky/* ASIC status check - some cards have one or two ASICs that need to be
36198090Srdivackyloaded.  Once that load is complete, this function is called to see if
37198090Srdivackythe load was successful.
38198090SrdivackyIf this load fails, it does not necessarily mean that the hardware is
39198090Srdivackydefective - the external box may be disconnected or turned off. */
40198090Srdivackystatic int check_asic_status(struct echoaudio *chip)
41198090Srdivacky{
42198090Srdivacky	u32 asic_status;
43198090Srdivacky
44198090Srdivacky	send_vector(chip, DSP_VC_TEST_ASIC);
45198090Srdivacky
46198090Srdivacky	/* The DSP will return a value to indicate whether or not the
47198090Srdivacky	   ASIC is currently loaded */
48198090Srdivacky	if (read_dsp(chip, &asic_status) < 0) {
49198090Srdivacky		dev_err(chip->card->dev,
50198090Srdivacky			"check_asic_status: failed on read_dsp\n");
51198090Srdivacky		chip->asic_loaded = false;
52198090Srdivacky		return -EIO;
53198090Srdivacky	}
54198090Srdivacky
55198090Srdivacky	chip->asic_loaded = (asic_status == ASIC_ALREADY_LOADED);
56198090Srdivacky	return chip->asic_loaded ? 0 : -EIO;
57198090Srdivacky}
58198090Srdivacky
59198090Srdivacky
60198090Srdivacky
61198090Srdivacky/* Most configuration of Gina24, Layla24, or Mona is accomplished by writing
62198090Srdivackythe control register.  write_control_reg sends the new control register
63198090Srdivackyvalue to the DSP. */
64198090Srdivackystatic int write_control_reg(struct echoaudio *chip, u32 value, char force)
65198090Srdivacky{
66198090Srdivacky	__le32 reg_value;
67198090Srdivacky
68198090Srdivacky	/* Handle the digital input auto-mute */
69198090Srdivacky	if (chip->digital_in_automute)
70198090Srdivacky		value |= GML_DIGITAL_IN_AUTO_MUTE;
71198090Srdivacky	else
72198090Srdivacky		value &= ~GML_DIGITAL_IN_AUTO_MUTE;
73198090Srdivacky
74198090Srdivacky	dev_dbg(chip->card->dev, "write_control_reg: 0x%x\n", value);
75198090Srdivacky
76198090Srdivacky	/* Write the control register */
77198090Srdivacky	reg_value = cpu_to_le32(value);
78198090Srdivacky	if (reg_value != chip->comm_page->control_register || force) {
79198090Srdivacky		if (wait_handshake(chip))
80198090Srdivacky			return -EIO;
81198090Srdivacky		chip->comm_page->control_register = reg_value;
82198090Srdivacky		clear_handshake(chip);
83198090Srdivacky		return send_vector(chip, DSP_VC_WRITE_CONTROL_REG);
84198090Srdivacky	}
85198090Srdivacky	return 0;
86198090Srdivacky}
87198090Srdivacky
88198090Srdivacky
89198090Srdivacky
90198090Srdivacky/* Gina24, Layla24, and Mona support digital input auto-mute.  If the digital
91198090Srdivackyinput auto-mute is enabled, the DSP will only enable the digital inputs if
92198090Srdivackythe card is syncing to a valid clock on the ADAT or S/PDIF inputs.
93198090SrdivackyIf the auto-mute is disabled, the digital inputs are enabled regardless of
94198090Srdivackywhat the input clock is set or what is connected. */
95198090Srdivackystatic int set_input_auto_mute(struct echoaudio *chip, int automute)
96198090Srdivacky{
97198090Srdivacky	dev_dbg(chip->card->dev, "set_input_auto_mute %d\n", automute);
98198090Srdivacky
99198090Srdivacky	chip->digital_in_automute = automute;
100198090Srdivacky
101198090Srdivacky	/* Re-set the input clock to the current value - indirectly causes
102198090Srdivacky	the auto-mute flag to be sent to the DSP */
103198090Srdivacky	return set_input_clock(chip, chip->input_clock);
104198090Srdivacky}
105198090Srdivacky
106198090Srdivacky
107198090Srdivacky
108198090Srdivacky/* S/PDIF coax / S/PDIF optical / ADAT - switch */
109static int set_digital_mode(struct echoaudio *chip, u8 mode)
110{
111	u8 previous_mode;
112	int err, i, o;
113
114	if (chip->bad_board)
115		return -EIO;
116
117	/* All audio channels must be closed before changing the digital mode */
118	if (snd_BUG_ON(chip->pipe_alloc_mask))
119		return -EAGAIN;
120
121	if (snd_BUG_ON(!(chip->digital_modes & (1 << mode))))
122		return -EINVAL;
123
124	previous_mode = chip->digital_mode;
125	err = dsp_set_digital_mode(chip, mode);
126
127	/* If we successfully changed the digital mode from or to ADAT,
128	   then make sure all output, input and monitor levels are
129	   updated by the DSP comm object. */
130	if (err >= 0 && previous_mode != mode &&
131	    (previous_mode == DIGITAL_MODE_ADAT || mode == DIGITAL_MODE_ADAT)) {
132		spin_lock_irq(&chip->lock);
133		for (o = 0; o < num_busses_out(chip); o++)
134			for (i = 0; i < num_busses_in(chip); i++)
135				set_monitor_gain(chip, o, i,
136						 chip->monitor_gain[o][i]);
137
138#ifdef ECHOCARD_HAS_INPUT_GAIN
139		for (i = 0; i < num_busses_in(chip); i++)
140			set_input_gain(chip, i, chip->input_gain[i]);
141		update_input_line_level(chip);
142#endif
143
144		for (o = 0; o < num_busses_out(chip); o++)
145			set_output_gain(chip, o, chip->output_gain[o]);
146		update_output_line_level(chip);
147		spin_unlock_irq(&chip->lock);
148	}
149
150	return err;
151}
152
153
154
155/* Set the S/PDIF output format */
156static int set_professional_spdif(struct echoaudio *chip, char prof)
157{
158	u32 control_reg;
159	int err;
160
161	/* Clear the current S/PDIF flags */
162	control_reg = le32_to_cpu(chip->comm_page->control_register);
163	control_reg &= GML_SPDIF_FORMAT_CLEAR_MASK;
164
165	/* Set the new S/PDIF flags depending on the mode */
166	control_reg |= GML_SPDIF_TWO_CHANNEL | GML_SPDIF_24_BIT |
167		GML_SPDIF_COPY_PERMIT;
168	if (prof) {
169		/* Professional mode */
170		control_reg |= GML_SPDIF_PRO_MODE;
171
172		switch (chip->sample_rate) {
173		case 32000:
174			control_reg |= GML_SPDIF_SAMPLE_RATE0 |
175				GML_SPDIF_SAMPLE_RATE1;
176			break;
177		case 44100:
178			control_reg |= GML_SPDIF_SAMPLE_RATE0;
179			break;
180		case 48000:
181			control_reg |= GML_SPDIF_SAMPLE_RATE1;
182			break;
183		}
184	} else {
185		/* Consumer mode */
186		switch (chip->sample_rate) {
187		case 32000:
188			control_reg |= GML_SPDIF_SAMPLE_RATE0 |
189				GML_SPDIF_SAMPLE_RATE1;
190			break;
191		case 48000:
192			control_reg |= GML_SPDIF_SAMPLE_RATE1;
193			break;
194		}
195	}
196
197	err = write_control_reg(chip, control_reg, false);
198	if (err)
199		return err;
200	chip->professional_spdif = prof;
201	dev_dbg(chip->card->dev, "set_professional_spdif to %s\n",
202		prof ? "Professional" : "Consumer");
203	return 0;
204}
205