convert to unix line endings.
[Rockbox.git] / firmware / drivers / audio / as3514.c
blob940cc36c5f834105341d2f2e8227f9c0b7bcce11
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Driver for AS3514 audio codec
12 * Copyright (c) 2007 Daniel Ankers
13 * Copyright (c) 2007 Christian Gmeiner
15 * All files in this archive are subject to the GNU General Public License.
16 * See the file COPYING in the source tree root for full license agreement.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "cpu.h"
23 #include "debug.h"
24 #include "system.h"
25 #include "audio.h"
26 #include "sound.h"
28 #include "audiohw.h"
29 #include "i2s.h"
30 #include "i2c-pp.h"
32 const struct sound_settings_info audiohw_settings[] = {
33 [SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25},
34 /* HAVE_SW_TONE_CONTROLS */
35 [SOUND_BASS] = {"dB", 0, 1, -24, 24, 0},
36 [SOUND_TREBLE] = {"dB", 0, 1, -24, 24, 0},
37 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
38 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
39 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
40 [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 39, 23},
41 [SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 31, 23},
42 [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 31, 23},
45 /* Shadow registers */
46 static struct as3514_info
48 int vol_r; /* Cached volume level (R) */
49 int vol_l; /* Cached volume level (L) */
50 unsigned int regs[0x1e]; /* last audio register: PLLMODE 0x1d */
51 } as3514;
53 enum
55 SOURCE_DAC = 0,
56 SOURCE_MIC1,
57 SOURCE_LINE_IN1,
58 SOURCE_LINE_IN1_ANALOG
61 static unsigned int source = SOURCE_DAC;
64 * little helper method to set register values.
65 * With the help of as3514.regs, we minimize i2c
66 * traffic.
68 static void as3514_write(unsigned int reg, unsigned int value)
70 if (pp_i2c_send(AS3514_I2C_ADDR, reg, value) != 2)
72 DEBUGF("as3514 error reg=0x%02x", reg);
75 if (reg < ARRAYLEN(as3514.regs))
77 as3514.regs[reg] = value;
79 else
81 DEBUGF("as3514 error reg=0x%02x", reg);
85 /* Helpers to set/clear bits */
86 static void as3514_write_or(unsigned int reg, unsigned int bits)
88 as3514_write(reg, as3514.regs[reg] | bits);
91 static void as3514_write_and(unsigned int reg, unsigned int bits)
93 as3514_write(reg, as3514.regs[reg] & bits);
96 /* convert tenth of dB volume to master volume register value */
97 int tenthdb2master(int db)
99 /* +6 to -73.5dB in 1.5dB steps == 53 levels */
100 if (db < VOLUME_MIN) {
101 return 0x0;
102 } else if (db >= VOLUME_MAX) {
103 return 0x35;
104 } else {
105 return((db-VOLUME_MIN)/15); /* VOLUME_MIN is negative */
109 int sound_val2phys(int setting, int value)
111 int result;
113 switch(setting)
115 case SOUND_LEFT_GAIN:
116 case SOUND_RIGHT_GAIN:
117 case SOUND_MIC_GAIN:
118 result = (value - 23) * 15;
119 break;
121 default:
122 result = value;
123 break;
126 return result;
130 * Initialise the PP I2C and I2S.
132 void audiohw_init(void)
134 unsigned int i;
136 /* normal outputs for CDI and I2S pin groups */
137 DEV_INIT2 &= ~0x300;
139 /*mini2?*/
140 DEV_INIT1 &=~0x3000000;
141 /*mini2?*/
143 /* device reset */
144 DEV_RS |= DEV_I2S;
145 DEV_RS &=~DEV_I2S;
147 /* I2S device reset */
148 DEV_RS |= DEV_I2S;
149 DEV_RS &=~DEV_I2S;
151 /* I2S device enable */
152 DEV_EN |= DEV_I2S;
154 /* enable external dev clock clocks */
155 DEV_EN |= DEV_EXTCLOCKS;
157 /* external dev clock to 24MHz */
158 outl(inl(0x70000018) & ~0xc, 0x70000018);
160 i2s_reset();
162 /* Set ADC off, mixer on, DAC on, line out off, line in off, mic off */
164 /* Turn on SUM, DAC */
165 as3514_write(AS3514_AUDIOSET1, (1 << 6) | (1 << 5));
167 /* Set BIAS on, DITH on, AGC on, IBR_DAC max, LSP_LP on, IBR_LSP min */
168 as3514_write(AS3514_AUDIOSET2, (1 << 2) | (3 << 0));
170 /* Set HPCM off, ZCU off*/
171 as3514_write(AS3514_AUDIOSET3, (1 << 2) | (1 << 0));
173 /* Mute and disable speaker */
174 as3514_write(AS3514_LSP_OUT_R, 0);
175 as3514_write(AS3514_LSP_OUT_L, (1 << 7));
177 /* set vol and set headphone over-current to 0 */
178 as3514_write(AS3514_HPH_OUT_R, (0x3 << 6) | 0x16);
179 /* set default vol for headphone */
180 as3514_write(AS3514_HPH_OUT_L, 0x16);
182 /* LRCK 24-48kHz */
183 as3514_write(AS3514_PLLMODE, 0x00);
185 /* DAC_Mute_off */
186 as3514_write_or(AS3514_DAC_L, (1 << 6));
188 /* M1_Sup_off */
189 as3514_write_or(AS3514_MIC1_L, (1 << 7));
190 /* M2_Sup_off */
191 as3514_write_or(AS3514_MIC2_L, (1 << 7));
193 /* read all reg values */
194 for (i = 0; i < ARRAYLEN(as3514.regs); i++)
196 as3514.regs[i] = i2c_readbyte(AS3514_I2C_ADDR, i);
200 void audiohw_postinit(void)
204 /* Silently enable / disable audio output */
205 void audiohw_enable_output(bool enable)
207 if (enable) {
208 /* reset the I2S controller into known state */
209 i2s_reset();
211 as3514_write_or(AS3514_HPH_OUT_L, (1 << 6)); /* power on */
212 audiohw_mute(0);
213 } else {
214 audiohw_mute(1);
215 as3514_write_and(AS3514_HPH_OUT_L, ~(1 << 6)); /* power off */
219 void audiohw_set_master_vol(int vol_l, int vol_r)
221 unsigned int hph_r = as3514.regs[AS3514_HPH_OUT_R] & ~0x1f;
222 unsigned int hph_l = as3514.regs[AS3514_HPH_OUT_L] & ~0x1f;
223 unsigned int mix_l, mix_r;
224 unsigned int mix_reg_r, mix_reg_l;
226 /* keep track of current setting */
227 as3514.vol_l = vol_l;
228 as3514.vol_r = vol_r;
230 if (source == SOURCE_LINE_IN1_ANALOG) {
231 mix_reg_r = AS3514_LINE_IN1_R;
232 mix_reg_l = AS3514_LINE_IN1_L;
233 } else {
234 mix_reg_r = AS3514_DAC_R;
235 mix_reg_l = AS3514_DAC_L;
238 mix_r = as3514.regs[mix_reg_r] & ~0x1f;
239 mix_l = as3514.regs[mix_reg_l] & ~0x1f;
241 /* we combine the mixer channel volume range with the headphone volume
242 range */
243 if (vol_r <= 0x16) {
244 mix_r |= vol_r;
245 /* hph_r - set 0 */
246 } else {
247 mix_r |= 0x16;
248 hph_r += vol_r - 0x16;
251 if (vol_l <= 0x16) {
252 mix_l |= vol_l;
253 /* hph_l - set 0 */
254 } else {
255 mix_l |= 0x16;
256 hph_l += vol_l - 0x16;
259 as3514_write(mix_reg_r, mix_r);
260 as3514_write(mix_reg_l, mix_l);
261 as3514_write(AS3514_HPH_OUT_R, hph_r);
262 as3514_write(AS3514_HPH_OUT_L, hph_l);
265 void audiohw_set_lineout_vol(int vol_l, int vol_r)
267 as3514_write(AS3514_LINE_OUT_R, vol_r);
268 as3514_write(AS3514_LINE_OUT_L, (1 << 6) | vol_l);
271 void audiohw_mute(bool mute)
273 if (mute) {
274 as3514_write_or(AS3514_HPH_OUT_L, (1 << 7));
275 } else {
276 as3514_write_and(AS3514_HPH_OUT_L, ~(1 << 7));
280 /* Nice shutdown of WM8758 codec */
281 void audiohw_close(void)
283 /* mute headphones */
284 audiohw_mute(true);
286 /* turn off everything */
287 as3514_write(AS3514_AUDIOSET1, 0x0);
290 void audiohw_set_sample_rate(int sampling_control)
292 (void)sampling_control;
295 void audiohw_enable_recording(bool source_mic)
297 if (source_mic) {
298 source = SOURCE_MIC1;
300 /* Sync mixer volumes before switching inputs */
301 audiohw_set_master_vol(as3514.vol_l, as3514.vol_r);
303 /* ADCmux = Stereo Microphone */
304 as3514_write_and(AS3514_ADC_R, ~(0x3 << 6));
305 /* MIC1_on, LIN1_off */
306 as3514_write(AS3514_AUDIOSET1,
307 (as3514.regs[AS3514_AUDIOSET1] & ~(1 << 2)) | (1 << 0));
308 /* M1_AGC_off */
309 as3514_write_and(AS3514_MIC1_R, ~(1 << 7));
310 } else {
311 source = SOURCE_LINE_IN1;
313 audiohw_set_master_vol(as3514.vol_l, as3514.vol_r);
315 /* ADCmux = Line_IN1 */
316 as3514_write(AS3514_ADC_R,
317 (as3514.regs[AS3514_ADC_R] & ~(0x3 << 6)) | (0x1 << 6));
318 /* MIC1_off, LIN1_on */
319 as3514_write(AS3514_AUDIOSET1,
320 (as3514.regs[AS3514_AUDIOSET1] & ~(1 << 0)) | (1 << 2));
323 /* ADC_Mute_off */
324 as3514_write_or(AS3514_ADC_L, (1 << 6));
325 /* ADC_on */
326 as3514_write_or(AS3514_AUDIOSET1, (1 << 7));
329 void audiohw_disable_recording(void)
331 source = SOURCE_DAC;
333 /* ADC_Mute_on */
334 as3514_write_and(AS3514_ADC_L, ~(1 << 6));
336 /* ADC_off, LIN1_off, MIC_off */
337 as3514_write_and(AS3514_AUDIOSET1, ~((1 << 7) | (1 << 2) | (1 << 0)));
339 audiohw_set_master_vol(as3514.vol_l, as3514.vol_r);
343 * Set recording volume
345 * Line in : 0 .. 23 .. 31 =>
346 Volume -34.5 .. +00.0 .. +12.0 dB
347 * Mic (left): 0 .. 23 .. 39 =>
348 * Volume -34.5 .. +00.0 .. +24.0 dB
351 void audiohw_set_recvol(int left, int right, int type)
353 switch (type)
355 case AUDIO_GAIN_MIC:
357 /* Combine MIC gains seamlessly with ADC levels */
358 unsigned int mic1_r = as3514.regs[AS3514_MIC1_R] & ~(0x3 << 5);
360 if (left >= 36) {
361 /* M1_Gain = +40db, ADR_Vol = +7.5dB .. +12.0 dB =>
362 +19.5 dB .. +24.0 dB */
363 left -= 8;
364 mic1_r |= (0x2 << 5);
365 } else if (left >= 32) {
366 /* M1_Gain = +34db, ADR_Vol = +7.5dB .. +12.0 dB =>
367 +13.5 dB .. +18.0 dB */
368 left -= 4;
369 mic1_r |= (0x1 << 5);
371 /* M1_Gain = +28db, ADR_Vol = -34.5dB .. +12.0 dB =>
372 -34.5 dB .. +12.0 dB */
374 right = left;
376 as3514_write(AS3514_MIC1_R, mic1_r);
377 break;
379 case AUDIO_GAIN_LINEIN:
380 break;
381 default:
382 return;
385 as3514_write(AS3514_ADC_R, (as3514.regs[AS3514_ADC_R] & ~0x1f) | right);
386 as3514_write(AS3514_ADC_L, (as3514.regs[AS3514_ADC_L] & ~0x1f) | left);
390 * Enable line in 1 analog monitoring
393 void audiohw_set_monitor(bool enable)
395 /* LI1R_Mute_on - default */
396 unsigned int line_in1_r = as3514.regs[AS3514_LINE_IN1_R] & ~(1 << 5);
397 /* LI1L_Mute_on - default */
398 unsigned int line_in1_l = as3514.regs[AS3514_LINE_IN1_L] & ~(1 << 5);
399 /* LIN1_off - default */
400 unsigned int audioset1 = as3514.regs[AS3514_AUDIOSET1] & ~(1 << 2);
402 if (enable) {
403 source = SOURCE_LINE_IN1_ANALOG;
405 /* LI1R_Mute_off */
406 line_in1_r |= (1 << 5);
407 /* LI1L_Mute_off */
408 line_in1_l |= (1 << 5);
409 /* LIN1_on */
410 audioset1 |= (1 << 2);
413 as3514_write(AS3514_AUDIOSET1, audioset1);
414 as3514_write(AS3514_LINE_IN1_R, line_in1_r);
415 as3514_write(AS3514_LINE_IN1_L, line_in1_l);
417 /* Sync mixer volume */
418 audiohw_set_master_vol(as3514.vol_l, as3514.vol_r);