Philips GoGear SA9200 port. Working bootloader and normal builds, including sound...
[Rockbox.git] / firmware / drivers / audio / as3514.c
blobb801375846b7a9f3ab172d15a0a73efa7777c12c
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 #if defined(HAVE_RECORDING)
41 [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 39, 23},
42 [SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 31, 23},
43 [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 31, 23},
44 #endif
47 /* Shadow registers */
48 static struct as3514_info
50 int vol_r; /* Cached volume level (R) */
51 int vol_l; /* Cached volume level (L) */
52 unsigned int regs[0x1e]; /* last audio register: PLLMODE 0x1d */
53 } as3514;
55 enum
57 SOURCE_DAC = 0,
58 SOURCE_MIC1,
59 SOURCE_LINE_IN1,
60 SOURCE_LINE_IN1_ANALOG
63 static unsigned int source = SOURCE_DAC;
66 * little helper method to set register values.
67 * With the help of as3514.regs, we minimize i2c
68 * traffic.
70 static void as3514_write(unsigned int reg, unsigned int value)
72 if (pp_i2c_send(AS3514_I2C_ADDR, reg, value) != 2)
74 DEBUGF("as3514 error reg=0x%02x", reg);
77 if (reg < ARRAYLEN(as3514.regs))
79 as3514.regs[reg] = value;
81 else
83 DEBUGF("as3514 error reg=0x%02x", reg);
87 /* Helpers to set/clear bits */
88 static void as3514_write_or(unsigned int reg, unsigned int bits)
90 as3514_write(reg, as3514.regs[reg] | bits);
93 static void as3514_write_and(unsigned int reg, unsigned int bits)
95 as3514_write(reg, as3514.regs[reg] & bits);
98 /* convert tenth of dB volume to master volume register value */
99 int tenthdb2master(int db)
101 /* +6 to -73.5dB in 1.5dB steps == 53 levels */
102 if (db < VOLUME_MIN) {
103 return 0x0;
104 } else if (db >= VOLUME_MAX) {
105 return 0x35;
106 } else {
107 return((db-VOLUME_MIN)/15); /* VOLUME_MIN is negative */
111 int sound_val2phys(int setting, int value)
113 int result;
115 switch(setting)
117 #if defined(HAVE_RECORDING)
118 case SOUND_LEFT_GAIN:
119 case SOUND_RIGHT_GAIN:
120 case SOUND_MIC_GAIN:
121 result = (value - 23) * 15;
122 break;
123 #endif
125 default:
126 result = value;
127 break;
130 return result;
134 * Initialise the PP I2C and I2S.
136 void audiohw_init(void)
138 unsigned int i;
140 /* normal outputs for CDI and I2S pin groups */
141 DEV_INIT2 &= ~0x300;
143 /*mini2?*/
144 DEV_INIT1 &=~0x3000000;
145 /*mini2?*/
147 /* device reset */
148 DEV_RS |= DEV_I2S;
149 DEV_RS &=~DEV_I2S;
151 /* I2S device reset */
152 DEV_RS |= DEV_I2S;
153 DEV_RS &=~DEV_I2S;
155 /* I2S device enable */
156 DEV_EN |= DEV_I2S;
158 /* enable external dev clock clocks */
159 DEV_EN |= DEV_EXTCLOCKS;
161 /* external dev clock to 24MHz */
162 outl(inl(0x70000018) & ~0xc, 0x70000018);
164 i2s_reset();
166 /* Set ADC off, mixer on, DAC on, line out off, line in off, mic off */
168 /* Turn on SUM, DAC */
169 as3514_write(AS3514_AUDIOSET1, (1 << 6) | (1 << 5));
171 /* Set BIAS on, DITH on, AGC on, IBR_DAC max, LSP_LP on, IBR_LSP min */
172 as3514_write(AS3514_AUDIOSET2, (1 << 2) | (3 << 0));
174 /* Set HPCM off, ZCU off*/
175 as3514_write(AS3514_AUDIOSET3, (1 << 2) | (1 << 0));
177 /* Mute and disable speaker */
178 as3514_write(AS3514_LSP_OUT_R, 0);
179 as3514_write(AS3514_LSP_OUT_L, (1 << 7));
181 /* set vol and set headphone over-current to 0 */
182 as3514_write(AS3514_HPH_OUT_R, (0x3 << 6) | 0x16);
183 /* set default vol for headphone */
184 as3514_write(AS3514_HPH_OUT_L, 0x16);
186 /* LRCK 24-48kHz */
187 as3514_write(AS3514_PLLMODE, 0x00);
189 /* DAC_Mute_off */
190 as3514_write_or(AS3514_DAC_L, (1 << 6));
192 /* M1_Sup_off */
193 as3514_write_or(AS3514_MIC1_L, (1 << 7));
194 /* M2_Sup_off */
195 as3514_write_or(AS3514_MIC2_L, (1 << 7));
197 /* read all reg values */
198 for (i = 0; i < ARRAYLEN(as3514.regs); i++)
200 as3514.regs[i] = i2c_readbyte(AS3514_I2C_ADDR, i);
204 void audiohw_postinit(void)
208 /* Silently enable / disable audio output */
209 void audiohw_enable_output(bool enable)
211 if (enable) {
212 /* reset the I2S controller into known state */
213 i2s_reset();
215 as3514_write_or(AS3514_HPH_OUT_L, (1 << 6)); /* power on */
216 audiohw_mute(0);
217 } else {
218 audiohw_mute(1);
219 as3514_write_and(AS3514_HPH_OUT_L, ~(1 << 6)); /* power off */
223 void audiohw_set_master_vol(int vol_l, int vol_r)
225 unsigned int hph_r = as3514.regs[AS3514_HPH_OUT_R] & ~0x1f;
226 unsigned int hph_l = as3514.regs[AS3514_HPH_OUT_L] & ~0x1f;
227 unsigned int mix_l, mix_r;
228 unsigned int mix_reg_r, mix_reg_l;
230 /* keep track of current setting */
231 as3514.vol_l = vol_l;
232 as3514.vol_r = vol_r;
234 if (source == SOURCE_LINE_IN1_ANALOG) {
235 mix_reg_r = AS3514_LINE_IN1_R;
236 mix_reg_l = AS3514_LINE_IN1_L;
237 } else {
238 mix_reg_r = AS3514_DAC_R;
239 mix_reg_l = AS3514_DAC_L;
242 mix_r = as3514.regs[mix_reg_r] & ~0x1f;
243 mix_l = as3514.regs[mix_reg_l] & ~0x1f;
245 /* we combine the mixer channel volume range with the headphone volume
246 range */
247 if (vol_r <= 0x16) {
248 mix_r |= vol_r;
249 /* hph_r - set 0 */
250 } else {
251 mix_r |= 0x16;
252 hph_r += vol_r - 0x16;
255 if (vol_l <= 0x16) {
256 mix_l |= vol_l;
257 /* hph_l - set 0 */
258 } else {
259 mix_l |= 0x16;
260 hph_l += vol_l - 0x16;
263 as3514_write(mix_reg_r, mix_r);
264 as3514_write(mix_reg_l, mix_l);
265 as3514_write(AS3514_HPH_OUT_R, hph_r);
266 as3514_write(AS3514_HPH_OUT_L, hph_l);
269 void audiohw_set_lineout_vol(int vol_l, int vol_r)
271 as3514_write(AS3514_LINE_OUT_R, vol_r);
272 as3514_write(AS3514_LINE_OUT_L, (1 << 6) | vol_l);
275 void audiohw_mute(bool mute)
277 if (mute) {
278 as3514_write_or(AS3514_HPH_OUT_L, (1 << 7));
279 } else {
280 as3514_write_and(AS3514_HPH_OUT_L, ~(1 << 7));
284 /* Nice shutdown of AS3514 audio codec */
285 void audiohw_close(void)
287 /* mute headphones */
288 audiohw_mute(true);
290 /* turn off everything */
291 as3514_write(AS3514_AUDIOSET1, 0x0);
294 void audiohw_set_sample_rate(int sampling_control)
296 (void)sampling_control;
299 #if defined(HAVE_RECORDING)
300 void audiohw_enable_recording(bool source_mic)
302 if (source_mic) {
303 source = SOURCE_MIC1;
305 /* Sync mixer volumes before switching inputs */
306 audiohw_set_master_vol(as3514.vol_l, as3514.vol_r);
308 /* ADCmux = Stereo Microphone */
309 as3514_write_and(AS3514_ADC_R, ~(0x3 << 6));
310 /* MIC1_on, LIN1_off */
311 as3514_write(AS3514_AUDIOSET1,
312 (as3514.regs[AS3514_AUDIOSET1] & ~(1 << 2)) | (1 << 0));
313 /* M1_AGC_off */
314 as3514_write_and(AS3514_MIC1_R, ~(1 << 7));
315 } else {
316 source = SOURCE_LINE_IN1;
318 audiohw_set_master_vol(as3514.vol_l, as3514.vol_r);
320 /* ADCmux = Line_IN1 */
321 as3514_write(AS3514_ADC_R,
322 (as3514.regs[AS3514_ADC_R] & ~(0x3 << 6)) | (0x1 << 6));
323 /* MIC1_off, LIN1_on */
324 as3514_write(AS3514_AUDIOSET1,
325 (as3514.regs[AS3514_AUDIOSET1] & ~(1 << 0)) | (1 << 2));
328 /* ADC_Mute_off */
329 as3514_write_or(AS3514_ADC_L, (1 << 6));
330 /* ADC_on */
331 as3514_write_or(AS3514_AUDIOSET1, (1 << 7));
334 void audiohw_disable_recording(void)
336 source = SOURCE_DAC;
338 /* ADC_Mute_on */
339 as3514_write_and(AS3514_ADC_L, ~(1 << 6));
341 /* ADC_off, LIN1_off, MIC_off */
342 as3514_write_and(AS3514_AUDIOSET1, ~((1 << 7) | (1 << 2) | (1 << 0)));
344 audiohw_set_master_vol(as3514.vol_l, as3514.vol_r);
348 * Set recording volume
350 * Line in : 0 .. 23 .. 31 =>
351 Volume -34.5 .. +00.0 .. +12.0 dB
352 * Mic (left): 0 .. 23 .. 39 =>
353 * Volume -34.5 .. +00.0 .. +24.0 dB
356 void audiohw_set_recvol(int left, int right, int type)
358 switch (type)
360 case AUDIO_GAIN_MIC:
362 /* Combine MIC gains seamlessly with ADC levels */
363 unsigned int mic1_r = as3514.regs[AS3514_MIC1_R] & ~(0x3 << 5);
365 if (left >= 36) {
366 /* M1_Gain = +40db, ADR_Vol = +7.5dB .. +12.0 dB =>
367 +19.5 dB .. +24.0 dB */
368 left -= 8;
369 mic1_r |= (0x2 << 5);
370 } else if (left >= 32) {
371 /* M1_Gain = +34db, ADR_Vol = +7.5dB .. +12.0 dB =>
372 +13.5 dB .. +18.0 dB */
373 left -= 4;
374 mic1_r |= (0x1 << 5);
376 /* M1_Gain = +28db, ADR_Vol = -34.5dB .. +12.0 dB =>
377 -34.5 dB .. +12.0 dB */
379 right = left;
381 as3514_write(AS3514_MIC1_R, mic1_r);
382 break;
384 case AUDIO_GAIN_LINEIN:
385 break;
386 default:
387 return;
390 as3514_write(AS3514_ADC_R, (as3514.regs[AS3514_ADC_R] & ~0x1f) | right);
391 as3514_write(AS3514_ADC_L, (as3514.regs[AS3514_ADC_L] & ~0x1f) | left);
395 * Enable line in 1 analog monitoring
398 void audiohw_set_monitor(bool enable)
400 /* LI1R_Mute_on - default */
401 unsigned int line_in1_r = as3514.regs[AS3514_LINE_IN1_R] & ~(1 << 5);
402 /* LI1L_Mute_on - default */
403 unsigned int line_in1_l = as3514.regs[AS3514_LINE_IN1_L] & ~(1 << 5);
404 /* LIN1_off - default */
405 unsigned int audioset1 = as3514.regs[AS3514_AUDIOSET1] & ~(1 << 2);
407 if (enable) {
408 source = SOURCE_LINE_IN1_ANALOG;
410 /* LI1R_Mute_off */
411 line_in1_r |= (1 << 5);
412 /* LI1L_Mute_off */
413 line_in1_l |= (1 << 5);
414 /* LIN1_on */
415 audioset1 |= (1 << 2);
418 as3514_write(AS3514_AUDIOSET1, audioset1);
419 as3514_write(AS3514_LINE_IN1_R, line_in1_r);
420 as3514_write(AS3514_LINE_IN1_L, line_in1_l);
422 /* Sync mixer volume */
423 audiohw_set_master_vol(as3514.vol_l, as3514.vol_r);
425 #endif /* HAVE_RECORDING */