Part of FS#11367 by me: e200v2 enable line out
[kugel-rb.git] / firmware / drivers / audio / as3514.c
blob9b1359df3d0485b0cef5961906373c7ddcfb818e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Driver for AS3514 and compatible audio codec
12 * Copyright (c) 2007 Daniel Ankers
13 * Copyright (c) 2007 Christian Gmeiner
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "cpu.h"
25 #include "debug.h"
26 #include "system.h"
27 #include "audio.h"
28 #include "sound.h"
30 #include "audiohw.h"
31 #include "i2s.h"
32 #include "ascodec.h"
35 * This drivers supports:
36 * as3514 , as used in the PP targets
37 * as3515 , as used in the as3525 targets
38 * as3543 , as used in the as3525v2 targets
41 #if CONFIG_CPU == AS3525
42 /* AMS Sansas based on the AS3525 use the LINE2 input for the analog radio
43 signal instead of LINE1 */
44 #define AS3514_LINE_IN_R AS3514_LINE_IN2_R
45 #define AS3514_LINE_IN_L AS3514_LINE_IN2_L
46 #define ADC_R_ADCMUX_LINE_IN ADC_R_ADCMUX_LINE_IN2
47 #define AUDIOSET1_LIN_on AUDIOSET1_LIN2_on
49 #elif CONFIG_CPU == AS3525v2
50 /* There is only 1 pair of registers on AS3543, the line input is selectable in
51 LINE_IN_R register */
52 #define AS3514_LINE_IN_R AS3514_LINE_IN1_R
53 #define AS3514_LINE_IN_L AS3514_LINE_IN1_L
54 #define ADC_R_ADCMUX_LINE_IN ADC_R_ADCMUX_LINE_IN2
55 #define AUDIOSET1_LIN_on AUDIOSET1_LIN1_on
57 #else /* PP use line1 */
59 #define AS3514_LINE_IN_R AS3514_LINE_IN1_R
60 #define AS3514_LINE_IN_L AS3514_LINE_IN1_L
61 #define ADC_R_ADCMUX_LINE_IN ADC_R_ADCMUX_LINE_IN1
62 #define AUDIOSET1_LIN_on AUDIOSET1_LIN1_on
64 #endif
66 const struct sound_settings_info audiohw_settings[] = {
67 [SOUND_VOLUME] = {"dB", 0, 1, VOLUME_MIN/10, 6, -25},
68 /* HAVE_SW_TONE_CONTROLS */
69 [SOUND_BASS] = {"dB", 0, 1, -24, 24, 0},
70 [SOUND_TREBLE] = {"dB", 0, 1, -24, 24, 0},
71 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
72 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
73 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
74 #ifdef HAVE_RECORDING
75 [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 39, 23},
76 [SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 31, 23},
77 [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 31, 23},
78 #endif
81 /* Shadow registers */
82 static uint8_t as3514_regs[AS3514_NUM_AUDIO_REGS]; /* 8-bit registers */
85 * little helper method to set register values.
86 * With the help of as3514_regs, we minimize i2c
87 * traffic.
89 static void as3514_write(unsigned int reg, unsigned int value)
91 ascodec_write(reg, value);
93 if (reg < AS3514_NUM_AUDIO_REGS)
94 as3514_regs[reg] = value;
97 /* Helpers to set/clear bits */
98 static void as3514_set(unsigned int reg, unsigned int bits)
100 as3514_write(reg, as3514_regs[reg] | bits);
103 static void as3514_clear(unsigned int reg, unsigned int bits)
105 as3514_write(reg, as3514_regs[reg] & ~bits);
108 static void as3514_write_masked(unsigned int reg, unsigned int bits,
109 unsigned int mask)
111 as3514_write(reg, (as3514_regs[reg] & ~mask) | (bits & mask));
114 /* convert tenth of dB volume to master volume register value */
115 int tenthdb2master(int db)
117 /* +6 to -73.5dB (or -81.0 dB) in 1.5dB steps == 53 (or 58) levels */
118 if (db < VOLUME_MIN) {
119 return 0x0;
120 } else if (db > VOLUME_MAX) {
121 return (VOLUME_MAX-VOLUME_MIN)/15;
122 } else {
123 return((db-VOLUME_MIN)/15); /* VOLUME_MIN is negative */
127 int sound_val2phys(int setting, int value)
129 switch(setting)
131 #if defined(HAVE_RECORDING)
132 case SOUND_LEFT_GAIN:
133 case SOUND_RIGHT_GAIN:
134 case SOUND_MIC_GAIN:
135 return (value - 23) * 15;
136 #endif
138 default:
139 return value;
144 * Initialise the PP I2C and I2S.
146 void audiohw_preinit(void)
148 /* read all reg values */
149 ascodec_readbytes(0x0, AS3514_NUM_AUDIO_REGS, as3514_regs);
151 #ifdef HAVE_AS3543
153 as3514_write(AS3514_AUDIOSET1, AUDIOSET1_DAC_on | AUDIOSET1_DAC_GAIN_on);
154 as3514_write(AS3514_AUDIOSET2, AUDIOSET2_HPH_QUALITY_LOW_POWER);
155 /* common ground on, delay playback unmuting when inserting headphones */
156 as3514_write(AS3514_AUDIOSET3, AUDIOSET3_HPCM_on | AUDIOSET3_HP_LONGSTART);
158 as3514_write(AS3543_DAC_IF, AS3543_DAC_INT_PLL);
159 /* Select Line 2 for FM radio */
160 as3514_set(AS3514_LINE_IN1_R, LINE_IN_R_LINE_SELECT);
161 /* Output SUM of microphone/line/DAC */
162 as3514_write(AS3514_HPH_OUT_R, HPH_OUT_R_HEADPHONES | HPH_OUT_R_HP_OUT_SUM);
164 #else
165 /* as3514/as3515 */
167 #if defined(SANSA_E200V2) || defined(SANSA_FUZE)
168 /* Set ADC off, mixer on, DAC on, line out on, line in off, mic off */
169 /* Turn on SUM, DAC */
170 as3514_write(AS3514_AUDIOSET1, AUDIOSET1_DAC_on | AUDIOSET1_LOUT_on |
171 AUDIOSET1_SUM_on);
172 #else
173 /* Set ADC off, mixer on, DAC on, line out off, line in off, mic off */
174 /* Turn on SUM, DAC */
175 as3514_write(AS3514_AUDIOSET1, AUDIOSET1_DAC_on | AUDIOSET1_SUM_on);
176 #endif /* SANSA_E200V2 || SANSA_FUZE */
178 /* Set BIAS on, DITH off, AGC off, IBR_DAC max reduction, LSP_LP on,
179 IBR_LSP max reduction (50%), taken from c200v2 OF
181 as3514_write(AS3514_AUDIOSET2, AUDIOSET2_IBR_LSP_50 | AUDIOSET2_LSP_LP |
182 AUDIOSET2_IBR_DAC_50 | AUDIOSET2_AGC_off | AUDIOSET2_DITH_off );
184 /* Mute and disable speaker */
185 as3514_write(AS3514_LSP_OUT_R, LSP_OUT_R_SP_OVC_TO_256MS | 0x00);
186 as3514_write(AS3514_LSP_OUT_L, LSP_OUT_L_SP_MUTE | 0x00);
188 #ifdef PHILIPS_SA9200
189 /* LRCK 8-23kHz (there are audible clicks while reading the ADC otherwise) */
190 as3514_write(AS3514_PLLMODE, PLLMODE_LRCK_8_23);
191 #else
192 /* LRCK 24-48kHz */
193 as3514_write(AS3514_PLLMODE, PLLMODE_LRCK_24_48);
194 #endif /* PHILIPS_SA9200 */
196 /* Set headphone over-current to 0, Min volume */
197 as3514_write(AS3514_HPH_OUT_R, HPH_OUT_R_HP_OVC_TO_0MS | 0x00);
199 /* AMS Sansas based on the AS3525 need HPCM enabled, otherwise they output the
200 L-R signal on both L and R headphone outputs instead of normal stereo.
201 Turning it off saves a little power on targets that don't need it. */
202 #if (CONFIG_CPU == AS3525)
203 /* Set HPCM on, ZCU off, reduce bias current, settings taken from c200v2 OF
205 as3514_write(AS3514_AUDIOSET3, AUDIOSET3_IBR_HPH | AUDIOSET3_ZCU_off);
206 #else
207 /* TODO: check if AS3525 settings save power on e200v1 or as3525v2 */
208 /* Set HPCM off, ZCU on */
209 as3514_write(AS3514_AUDIOSET3, AUDIOSET3_HPCM_off);
210 #endif /* CONFIG_CPU == AS3525 */
212 /* M2_Sup_off */
213 as3514_set(AS3514_MIC2_L, MIC2_L_M2_SUP_off);
215 #endif /* HAVE_AS3543 */
217 /* registers identical on as3514/as3515 and as3543 */
219 /* M1_Sup_off */
220 as3514_set(AS3514_MIC1_L, MIC1_L_M1_SUP_off);
222 /* Headphone ON, MUTE, Min volume */
223 as3514_write(AS3514_HPH_OUT_L, HPH_OUT_L_HP_ON | HPH_OUT_L_HP_MUTE | 0x00);
225 #if defined(SANSA_E200V2) || defined(SANSA_FUZE)
226 /* Line Out Stereo, MUTE, Min volume */
227 as3514_write(AS3514_LINE_OUT_L, LINE_OUT_L_LO_SES_DM_SE_ST |
228 LINE_OUT_L_LO_SES_DM_MUTE | 0x00);
229 #endif /* SANSA_E200V2 || SANSA_FUZE */
231 /* DAC_Mute_off */
232 as3514_set(AS3514_DAC_L, DAC_L_DAC_MUTE_off);
235 static void audiohw_mute(bool mute)
237 if (mute) {
238 as3514_set(AS3514_HPH_OUT_L, HPH_OUT_L_HP_MUTE);
239 #if defined(SANSA_E200V2) || defined(SANSA_FUZE)
240 as3514_set(AS3514_LINE_OUT_L, LINE_OUT_L_LO_SES_DM_MUTE);
241 #endif /* SANSA_E200V2 || SANSA_FUZE */
242 } else {
243 as3514_clear(AS3514_HPH_OUT_L, HPH_OUT_L_HP_MUTE);
244 #if defined(SANSA_E200V2) || defined(SANSA_FUZE)
245 as3514_clear(AS3514_LINE_OUT_L, LINE_OUT_L_LO_SES_DM_MUTE);
246 #endif /* SANSA_E200V2 || SANSA_FUZE */
250 void audiohw_postinit(void)
252 /* wait until outputs have stabilized */
253 sleep(HZ/4);
255 #ifdef CPU_PP
256 ascodec_suppressor_on(false);
257 #endif
259 #if defined(SANSA_E200V2) || defined(SANSA_FUZE)
260 /* Set line out volume to 0dB */
261 as3514_write_masked(AS3514_LINE_OUT_R, 0x1b, AS3514_VOL_MASK);
262 as3514_write_masked(AS3514_LINE_OUT_L, 0x1b, AS3514_VOL_MASK);
263 #endif /* SANSA_E200V2 || SANSA_FUZE */
265 audiohw_mute(false);
268 void audiohw_set_master_vol(int vol_l, int vol_r)
270 unsigned int hph_r, hph_l;
271 unsigned int mix_l, mix_r;
273 if (vol_l == 0 && vol_r == 0) {
274 audiohw_mute(true);
275 return;
278 /* We combine the mixer/DAC channel volume range with the headphone volume
279 range - keep first stage as loud as possible */
281 /*AS3543 mixer can go a little louder then the as3514, although
282 * it might be possible to go louder on the as3514 as well */
284 #if CONFIG_CPU == AS3525v2
285 #define MIXER_MAX_VOLUME 0x1b
286 #else /* lets leave the AS3514 alone until its better tested*/
287 #define MIXER_MAX_VOLUME 0x16
288 #endif
290 if (vol_r <= MIXER_MAX_VOLUME) {
291 mix_r = vol_r;
292 hph_r = 0;
293 } else {
294 mix_r = MIXER_MAX_VOLUME;
295 hph_r = vol_r - MIXER_MAX_VOLUME;
298 if (vol_l <= MIXER_MAX_VOLUME) {
299 mix_l = vol_l;
300 hph_l = 0;
301 } else {
302 mix_l = MIXER_MAX_VOLUME;
303 hph_l = vol_l - MIXER_MAX_VOLUME;
307 as3514_write_masked(AS3514_DAC_R, mix_r, AS3514_VOL_MASK);
308 as3514_write_masked(AS3514_DAC_L, mix_l, AS3514_VOL_MASK);
309 #if defined(HAVE_RECORDING) || defined(HAVE_FMRADIO_IN)
310 as3514_write_masked(AS3514_LINE_IN_R, mix_r, AS3514_VOL_MASK);
311 as3514_write_masked(AS3514_LINE_IN_L, mix_l, AS3514_VOL_MASK);
312 #endif
313 as3514_write_masked(AS3514_HPH_OUT_R, hph_r, AS3514_VOL_MASK);
314 as3514_write_masked(AS3514_HPH_OUT_L, hph_l, AS3514_VOL_MASK);
316 audiohw_mute(false);
319 #if 0 /* unused */
320 void audiohw_set_lineout_vol(int vol_l, int vol_r)
322 #ifdef HAVE_AS3543
323 /* line out volume is set in the same registers */
324 audiohw_set_master_vol(vol_l, vol_r);
325 #else
326 as3514_write_masked(AS3514_LINE_OUT_R, vol_r, AS3514_VOL_MASK);
327 as3514_write_masked(AS3514_LINE_OUT_L, vol_l, AS3514_VOL_MASK);
328 #endif
330 #endif
332 /* Nice shutdown of AS3514 audio codec */
333 void audiohw_close(void)
335 /* mute headphones */
336 audiohw_mute(true);
338 #ifdef CPU_PP
339 ascodec_suppressor_on(true);
340 #endif
342 /* turn on common */
343 as3514_clear(AS3514_AUDIOSET3, AUDIOSET3_HPCM_off);
345 /* turn off everything */
346 as3514_clear(AS3514_HPH_OUT_L, HPH_OUT_L_HP_ON);
347 as3514_write(AS3514_AUDIOSET1, 0x0);
349 /* Allow caps to discharge */
350 sleep(HZ/4);
353 void audiohw_set_frequency(int fsel)
355 #if defined(SANSA_E200) || defined(SANSA_C200)
356 if ((unsigned)fsel >= HW_NUM_FREQ)
357 fsel = HW_FREQ_DEFAULT;
359 as3514_write(AS3514_PLLMODE, hw_freq_sampr[fsel] < 24000 ?
360 PLLMODE_LRCK_8_23 : PLLMODE_LRCK_24_48);
362 audiohw_set_sampr_dividers(fsel);
363 #endif
364 (void)fsel;
367 #if defined(HAVE_RECORDING)
368 void audiohw_enable_recording(bool source_mic)
370 if (source_mic) {
371 /* ADCmux = Stereo Microphone */
372 as3514_write_masked(AS3514_ADC_R, ADC_R_ADCMUX_ST_MIC,
373 ADC_R_ADCMUX);
375 /* MIC1_on, others off */
376 as3514_write_masked(AS3514_AUDIOSET1, AUDIOSET1_MIC1_on,
377 AUDIOSET1_INPUT_MASK);
379 #if CONFIG_CPU == AS3525v2
380 /* XXX: why is the microphone supply not needed on other models ?? */
381 /* Enable supply */
382 as3514_clear(AS3514_MIC1_L, MIC1_L_M1_SUP_off);
383 #endif
385 /* M1_AGC_off */
386 as3514_clear(AS3514_MIC1_R, MIC1_R_M1_AGC_off);
387 } else {
388 /* ADCmux = Line_IN1 or Line_IN2 */
389 as3514_write_masked(AS3514_ADC_R, ADC_R_ADCMUX_LINE_IN,
390 ADC_R_ADCMUX);
392 /* LIN1_or LIN2 on, rest off */
393 as3514_write_masked(AS3514_AUDIOSET1, AUDIOSET1_LIN_on,
394 AUDIOSET1_INPUT_MASK);
396 #if CONFIG_CPU == AS3525v2
397 /* Disable supply */
398 as3514_set(AS3514_MIC1_L, MIC1_L_M1_SUP_off);
399 #endif
402 /* ADC_Mute_off */
403 as3514_set(AS3514_ADC_L, ADC_L_ADC_MUTE_off);
404 /* ADC_on */
405 as3514_set(AS3514_AUDIOSET1, AUDIOSET1_ADC_on);
408 void audiohw_disable_recording(void)
410 /* ADC_Mute_on */
411 as3514_clear(AS3514_ADC_L, ADC_L_ADC_MUTE_off);
413 /* ADC_off, all input sources off */
414 as3514_clear(AS3514_AUDIOSET1, AUDIOSET1_ADC_on | AUDIOSET1_INPUT_MASK);
418 * Set recording volume
420 * Line in : 0 .. 23 .. 31 =>
421 Volume -34.5 .. +00.0 .. +12.0 dB
422 * Mic (left): 0 .. 23 .. 39 =>
423 * Volume -34.5 .. +00.0 .. +24.0 dB
426 void audiohw_set_recvol(int left, int right, int type)
428 switch (type)
430 case AUDIO_GAIN_MIC:
432 /* Combine MIC gains seamlessly with ADC levels */
433 unsigned int mic1_r;
435 if (left >= 36) {
436 /* M1_Gain = +40db, ADR_Vol = +7.5dB .. +12.0 dB =>
437 +19.5 dB .. +24.0 dB */
438 left -= 8;
439 mic1_r = MIC1_R_M1_GAIN_40DB;
440 } else if (left >= 32) {
441 /* M1_Gain = +34db, ADR_Vol = +7.5dB .. +12.0 dB =>
442 +13.5 dB .. +18.0 dB */
443 left -= 4;
444 mic1_r = MIC1_R_M1_GAIN_34DB;
445 } else {
446 /* M1_Gain = +28db, ADR_Vol = -34.5dB .. +12.0 dB =>
447 -34.5 dB .. +12.0 dB */
448 mic1_r = MIC1_R_M1_GAIN_28DB;
451 right = left;
453 as3514_write_masked(AS3514_MIC1_R, mic1_r, MIC1_R_M1_GAIN);
454 break;
456 case AUDIO_GAIN_LINEIN:
457 break;
458 default:
459 return;
462 as3514_write_masked(AS3514_ADC_R, right, AS3514_VOL_MASK);
463 as3514_write_masked(AS3514_ADC_L, left, AS3514_VOL_MASK);
465 #endif /* HAVE_RECORDING */
467 #if defined(HAVE_RECORDING) || defined(HAVE_FMRADIO_IN)
469 * Enable line in analog monitoring
472 void audiohw_set_monitor(bool enable)
474 if (enable) {
475 /* select either LIN1 or LIN2 */
476 as3514_write_masked(AS3514_AUDIOSET1, AUDIOSET1_LIN_on,
477 AUDIOSET1_LIN1_on | AUDIOSET1_LIN2_on);
478 as3514_set(AS3514_LINE_IN_R, LINE_IN1_R_LI1R_MUTE_off);
479 as3514_set(AS3514_LINE_IN_L, LINE_IN1_L_LI1L_MUTE_off);
481 else {
482 /* turn off both LIN1 and LIN2 (if present) */
483 as3514_clear(AS3514_LINE_IN1_R, LINE_IN1_R_LI1R_MUTE_off);
484 as3514_clear(AS3514_LINE_IN1_L, LINE_IN1_L_LI1L_MUTE_off);
485 #ifndef HAVE_AS3543
486 as3514_clear(AS3514_LINE_IN2_R, LINE_IN2_R_LI2R_MUTE_off);
487 as3514_clear(AS3514_LINE_IN2_L, LINE_IN2_L_LI2L_MUTE_off);
488 #endif
489 as3514_clear(AS3514_AUDIOSET1, AUDIOSET1_LIN1_on | AUDIOSET1_LIN2_on);
492 #endif /* HAVE_RECORDING || HAVE_FMRADIO_IN */