diff options
Diffstat (limited to 'firmware/drivers/audio')
-rw-r--r-- | firmware/drivers/audio/ak4537.c | 280 |
1 files changed, 280 insertions, 0 deletions
diff --git a/firmware/drivers/audio/ak4537.c b/firmware/drivers/audio/ak4537.c new file mode 100644 index 0000000000..83a040e177 --- /dev/null +++ b/firmware/drivers/audio/ak4537.c | |||
@@ -0,0 +1,280 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (c) 2009 Mark Arigo | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "config.h" | ||
22 | #include "system.h" | ||
23 | #include "string.h" | ||
24 | |||
25 | // #define LOGF_ENABLE | ||
26 | #include "logf.h" | ||
27 | |||
28 | #include "pcm_sampr.h" | ||
29 | #include "audio.h" | ||
30 | #include "akcodec.h" | ||
31 | #include "audiohw.h" | ||
32 | #include "sound.h" | ||
33 | |||
34 | const struct sound_settings_info audiohw_settings[] = { | ||
35 | [SOUND_VOLUME] = {"dB", 0, 1,-127, 0, -25}, | ||
36 | /* HAVE_SW_TONE_CONTROLS */ | ||
37 | [SOUND_BASS] = {"dB", 0, 1, -24, 24, 0}, | ||
38 | [SOUND_TREBLE] = {"dB", 0, 1, -24, 24, 0}, | ||
39 | [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0}, | ||
40 | [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0}, | ||
41 | [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100}, | ||
42 | #if defined(HAVE_RECORDING) | ||
43 | [SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 31, 23}, | ||
44 | [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 31, 23}, | ||
45 | [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 1, 0}, | ||
46 | #endif | ||
47 | }; | ||
48 | |||
49 | static unsigned char akc_regs[AKC_NUM_REGS]; | ||
50 | |||
51 | static void akc_write(int reg, unsigned val) | ||
52 | { | ||
53 | if ((unsigned)reg >= AKC_NUM_REGS) | ||
54 | return; | ||
55 | |||
56 | akc_regs[reg] = (unsigned char)val; | ||
57 | akcodec_write(reg, val); | ||
58 | } | ||
59 | |||
60 | static void akc_set(int reg, unsigned bits) | ||
61 | { | ||
62 | akc_write(reg, akc_regs[reg] | bits); | ||
63 | } | ||
64 | |||
65 | static void akc_clear(int reg, unsigned bits) | ||
66 | { | ||
67 | akc_write(reg, akc_regs[reg] & ~bits); | ||
68 | } | ||
69 | |||
70 | static void akc_write_masked(int reg, unsigned bits, unsigned mask) | ||
71 | { | ||
72 | akc_write(reg, (akc_regs[reg] & ~mask) | (bits & mask)); | ||
73 | } | ||
74 | |||
75 | #if 0 | ||
76 | static void codec_set_active(int active) | ||
77 | { | ||
78 | (void)active; | ||
79 | } | ||
80 | #endif | ||
81 | |||
82 | /* convert tenth of dB volume (-1270..0) to master volume register value */ | ||
83 | int tenthdb2master(int db) | ||
84 | { | ||
85 | if (db < VOLUME_MIN) | ||
86 | return 0xff; /* mute */ | ||
87 | else if (db >= VOLUME_MAX) | ||
88 | return 0x00; | ||
89 | else | ||
90 | return ((-db)/5); | ||
91 | } | ||
92 | |||
93 | int sound_val2phys(int setting, int value) | ||
94 | { | ||
95 | int result; | ||
96 | |||
97 | switch(setting) | ||
98 | { | ||
99 | #ifdef HAVE_RECORDING | ||
100 | case SOUND_LEFT_GAIN: | ||
101 | case SOUND_RIGHT_GAIN: | ||
102 | result = (value - 23) * 15; /* fix */ | ||
103 | break; | ||
104 | case SOUND_MIC_GAIN: | ||
105 | result = value * 200; /* fix */ | ||
106 | break; | ||
107 | #endif | ||
108 | default: | ||
109 | result = value; | ||
110 | break; | ||
111 | } | ||
112 | |||
113 | return result; | ||
114 | } | ||
115 | |||
116 | void audiohw_mute(bool mute) | ||
117 | { | ||
118 | if (mute) | ||
119 | { | ||
120 | akc_set(AK4537_DAC, SMUTE); | ||
121 | udelay(200000); | ||
122 | } | ||
123 | else | ||
124 | { | ||
125 | udelay(200000); | ||
126 | akc_clear(AK4537_DAC, SMUTE); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | void audiohw_preinit(void) | ||
131 | { | ||
132 | int i; | ||
133 | for (i = 0; i < AKC_NUM_REGS; i++) | ||
134 | akc_regs[i] = akcodec_read(i); | ||
135 | |||
136 | /* POWER UP SEQUENCE (from the datasheet) */ | ||
137 | /* Note: the delay length is what the OF uses, although the datasheet | ||
138 | suggests they can be shorter */ | ||
139 | |||
140 | /* power up VCOM */ | ||
141 | akc_set(AK4537_PM1, PMVCM); | ||
142 | udelay(100000); | ||
143 | |||
144 | /* setup AK4537_SIGSEL1 */ | ||
145 | akc_set(AK4537_SIGSEL1, ALCS | MOUT2); | ||
146 | udelay(100000); | ||
147 | |||
148 | /* setup AK4537_SIGSEL2 */ | ||
149 | akc_write_masked(AK4537_SIGSEL2, DAHS, (DAHS | HPL | HPR)); | ||
150 | udelay(100000); | ||
151 | |||
152 | /* setup AK4537_MODE1 */ | ||
153 | akc_write_masked(AK4537_MODE1, DIF_I2S | BICK_32FS | MCKI_PLL_12000KHZ, | ||
154 | (DIF_MASK | BICK_MASK | MCKI_MASK)); | ||
155 | udelay(100000); | ||
156 | |||
157 | /* CLOCK SETUP - X'tal used in PLL mode (master mode) */ | ||
158 | |||
159 | /* release the pull-down of the XTI pin and power-up the X'tal osc */ | ||
160 | akc_write_masked(AK4537_PM2, PMXTL, (MCLKPD | PMXTL)); | ||
161 | udelay(100000); | ||
162 | |||
163 | /* power-up the PLL */ | ||
164 | akc_set(AK4537_PM2, PMPLL); | ||
165 | udelay(100000); | ||
166 | |||
167 | /* enable MCKO output and setup MCKO output freq */ | ||
168 | akc_set(AK4537_MODE1, MCKO_EN); | ||
169 | udelay(100000); | ||
170 | |||
171 | /* ENABLE HEADPHONE AMP OUTPUT */ | ||
172 | |||
173 | /* setup the sampling freq if PLL mode is used */ | ||
174 | akc_write_masked(AK4537_MODE2, AKC_PLL_44100HZ, FS_MASK); | ||
175 | |||
176 | /* setup the low freq boost level */ | ||
177 | akc_write_masked(AK4537_DAC, BST_OFF, BST_MASK); | ||
178 | |||
179 | /* setup the digital volume */ | ||
180 | akc_write(AK4537_ATTL, 0x10); | ||
181 | akc_write(AK4537_ATTR, 0x10); | ||
182 | |||
183 | /* power up the DAC */ | ||
184 | akc_set(AK4537_PM2, PMDAC); | ||
185 | udelay(100000); | ||
186 | |||
187 | /* power up the headphone amp */ | ||
188 | akc_clear(AK4537_SIGSEL2, HPL | HPR); | ||
189 | udelay(100000); | ||
190 | |||
191 | /* power up the common voltage of headphone amp */ | ||
192 | akc_set(AK4537_PM2, PMHPL | PMHPR); | ||
193 | udelay(100000); | ||
194 | } | ||
195 | |||
196 | void audiohw_postinit(void) | ||
197 | { | ||
198 | /* nothing */ | ||
199 | } | ||
200 | |||
201 | void audiohw_close(void) | ||
202 | { | ||
203 | /* POWER DOWN SEQUENCE (from the datasheet) */ | ||
204 | |||
205 | /* mute */ | ||
206 | akc_write(AK4537_ATTL, 0xff); | ||
207 | akc_write(AK4537_ATTR, 0xff); | ||
208 | akc_set(AK4537_DAC, SMUTE); | ||
209 | udelay(100000); | ||
210 | |||
211 | /* power down the common voltage of headphone amp */ | ||
212 | akc_clear(AK4537_PM2, PMHPL | PMHPR); | ||
213 | |||
214 | /* power down the DAC */ | ||
215 | akc_clear(AK4537_PM2, PMDAC); | ||
216 | |||
217 | /* power down the headphone amp */ | ||
218 | akc_set(AK4537_SIGSEL2, HPL | HPR); | ||
219 | |||
220 | /* disable MCKO */ | ||
221 | akc_clear(AK4537_MODE1, MCKO_EN); | ||
222 | |||
223 | /* power down X'tal and PLL, pull down the XTI pin */ | ||
224 | akc_write_masked(AK4537_PM2, MCLKPD, (MCLKPD | PMXTL | PMPLL)); | ||
225 | |||
226 | /* power down VCOM */ | ||
227 | akc_clear(AK4537_PM1, PMVCM); | ||
228 | udelay(100000); | ||
229 | |||
230 | akcodec_close(); /* target-specific */ | ||
231 | } | ||
232 | |||
233 | void audiohw_set_master_vol(int vol_l, int vol_r) | ||
234 | { | ||
235 | akc_write(AK4537_ATTL, vol_l & 0xff); | ||
236 | akc_write(AK4537_ATTR, vol_r & 0xff); | ||
237 | } | ||
238 | |||
239 | void audiohw_set_frequency(int fsel) | ||
240 | { | ||
241 | static const unsigned char srctrl_table[HW_NUM_FREQ] = | ||
242 | { | ||
243 | HW_HAVE_8_([HW_FREQ_8] = AKC_PLL_8000HZ, ) | ||
244 | HW_HAVE_11_([HW_FREQ_11] = AKC_PLL_11025HZ,) | ||
245 | HW_HAVE_16_([HW_FREQ_16] = AKC_PLL_16000HZ,) | ||
246 | HW_HAVE_22_([HW_FREQ_22] = AKC_PLL_22050HZ,) | ||
247 | HW_HAVE_24_([HW_FREQ_24] = AKC_PLL_24000HZ,) | ||
248 | HW_HAVE_32_([HW_FREQ_32] = AKC_PLL_32000HZ,) | ||
249 | HW_HAVE_44_([HW_FREQ_44] = AKC_PLL_44100HZ,) | ||
250 | HW_HAVE_48_([HW_FREQ_48] = AKC_PLL_48000HZ,) | ||
251 | }; | ||
252 | |||
253 | if ((unsigned)fsel >= HW_NUM_FREQ) | ||
254 | fsel = HW_FREQ_DEFAULT; | ||
255 | |||
256 | akc_write_masked(AK4537_MODE2, srctrl_table[fsel], FS_MASK); | ||
257 | } | ||
258 | |||
259 | #if defined(HAVE_RECORDING) | ||
260 | void audiohw_enable_recording(bool source_mic) | ||
261 | { | ||
262 | (void)source_mic; | ||
263 | } | ||
264 | |||
265 | void audiohw_disable_recording(void) | ||
266 | { | ||
267 | } | ||
268 | |||
269 | void audiohw_set_recvol(int left, int right, int type) | ||
270 | { | ||
271 | (void)left; | ||
272 | (void)right; | ||
273 | (void)type; | ||
274 | } | ||
275 | |||
276 | void audiohw_set_monitor(bool enable) | ||
277 | { | ||
278 | (void)enable; | ||
279 | } | ||
280 | #endif /* HAVE_RECORDING */ | ||