summaryrefslogtreecommitdiff
path: root/firmware/drivers/audio/ak4537.c
blob: a42b1346d2f260c57bcc66aa62a651132f07fd25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (c) 2009 Mark Arigo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "system.h"
#include "string.h"

/*#define LOGF_ENABLE*/
#include "logf.h"

#include "pcm_sampr.h"
#include "audio.h"
#include "akcodec.h"
#include "audiohw.h"
#include "sound.h"

static unsigned char akc_regs[AKC_NUM_REGS];

static void akc_write(int reg, unsigned val)
{
    if ((unsigned)reg >= AKC_NUM_REGS)
        return;

    akc_regs[reg] = (unsigned char)val;
    akcodec_write(reg, val);
}

static void akc_set(int reg, unsigned bits)
{
    akc_write(reg, akc_regs[reg] | bits);
}

static void akc_clear(int reg, unsigned bits)
{
    akc_write(reg, akc_regs[reg] & ~bits);
}

static void akc_write_masked(int reg, unsigned bits, unsigned mask)
{
    akc_write(reg, (akc_regs[reg] & ~mask) | (bits & mask));
}

#if 0
static void codec_set_active(int active)
{
    (void)active;
}
#endif

/* convert tenth of dB volume (-1270..0) to master volume register value */
static int vol_tenthdb2hw(int db)
{
    if (db <= -1280)
        return 0xff; /* mute */
    else if (db >= 0)
        return 0x00;
    else
        return ((-db)/5);
}

/*static void audiohw_mute(bool mute)
{
    if (mute)
    {
        akc_set(AK4537_DAC, SMUTE);
        udelay(200000);
    }
    else
    {
        udelay(200000);
        akc_clear(AK4537_DAC, SMUTE);
    }
}*/

void audiohw_preinit(void)
{
    int i;
    for (i = 0; i < AKC_NUM_REGS; i++)
        akc_regs[i] = akcodec_read(i);

    /* POWER UP SEQUENCE (from the datasheet) */
    /* Note: the delay length is what the OF uses, although the datasheet
       suggests they can be shorter */

    /* power up VCOM */
    akc_set(AK4537_PM1, PMVCM);
    udelay(100000);

    /* setup AK4537_SIGSEL1 */
    akc_set(AK4537_SIGSEL1, ALCS | MOUT2);        
    udelay(100000);

    /* setup AK4537_SIGSEL2 */
    akc_write_masked(AK4537_SIGSEL2, DAHS, (DAHS | HPL | HPR));
    udelay(100000);

    /* setup AK4537_MODE1 */
    akc_write_masked(AK4537_MODE1, DIF_I2S | BICK_32FS | MCKI_PLL_12000KHZ,
                     (DIF_MASK | BICK_MASK | MCKI_MASK));
    udelay(100000);

    /* CLOCK SETUP - X'tal used in PLL mode (master mode) */

    /* release the pull-down of the XTI pin and power-up the X'tal osc */
    akc_write_masked(AK4537_PM2, PMXTL, (MCLKPD | PMXTL));
    udelay(100000);

    /* power-up the PLL */
    akc_set(AK4537_PM2, PMPLL);
    udelay(100000);

    /* enable MCKO output and setup MCKO output freq */
    akc_set(AK4537_MODE1, MCKO_EN);
    udelay(100000);

    /* ENABLE HEADPHONE AMP OUTPUT */

    /* setup the sampling freq if PLL mode is used */
    akc_write_masked(AK4537_MODE2, AKC_PLL_44100HZ, FS_MASK);

    /* setup the low freq boost level */
    akc_write_masked(AK4537_DAC, BST_OFF, BST_MASK);

    /* setup the digital volume */
    akc_write(AK4537_ATTL, 0x10);
    akc_write(AK4537_ATTR, 0x10);

    /* power up the DAC */
    akc_set(AK4537_PM2, PMDAC);
    udelay(100000);

    /* power up the headphone amp */
    akc_clear(AK4537_SIGSEL2, HPL | HPR);
    udelay(100000);

    /* power up the common voltage of headphone amp */
    akc_set(AK4537_PM2, PMHPL | PMHPR);
    udelay(100000);
}

void audiohw_postinit(void)
{
    /* nothing */
}

void audiohw_close(void)
{
    /* POWER DOWN SEQUENCE (from the datasheet) */

    /* mute */
    akc_write(AK4537_ATTL, 0xff);
    akc_write(AK4537_ATTR, 0xff);
    akc_set(AK4537_DAC, SMUTE);
    udelay(100000);

    /* power down the common voltage of headphone amp */
    akc_clear(AK4537_PM2, PMHPL | PMHPR);

    /* power down the DAC */
    akc_clear(AK4537_PM2, PMDAC);

    /* power down the headphone amp */
    akc_set(AK4537_SIGSEL2, HPL | HPR);

    /* disable MCKO */
    akc_clear(AK4537_MODE1, MCKO_EN);

    /* power down X'tal and PLL, pull down the XTI pin */
    akc_write_masked(AK4537_PM2, MCLKPD, (MCLKPD | PMXTL | PMPLL));

    /* power down VCOM */
    akc_clear(AK4537_PM1, PMVCM);
    udelay(100000);

    akcodec_close(); /* target-specific */
}

void audiohw_set_volume(int vol_l, int vol_r)
{
    vol_l = vol_tenthdb2hw(vol_l);
    vol_r = vol_tenthdb2hw(vol_r);
    akc_write(AK4537_ATTL, vol_l & 0xff);
    akc_write(AK4537_ATTR, vol_r & 0xff);
}

void audiohw_set_frequency(int fsel)
{
    static const unsigned char srctrl_table[HW_NUM_FREQ] =
    {
        HW_HAVE_8_([HW_FREQ_8]   = AKC_PLL_8000HZ, )
        HW_HAVE_11_([HW_FREQ_11] = AKC_PLL_11025HZ,)
        HW_HAVE_16_([HW_FREQ_16] = AKC_PLL_16000HZ,)
        HW_HAVE_22_([HW_FREQ_22] = AKC_PLL_22050HZ,)
        HW_HAVE_24_([HW_FREQ_24] = AKC_PLL_24000HZ,)
        HW_HAVE_32_([HW_FREQ_32] = AKC_PLL_32000HZ,)
        HW_HAVE_44_([HW_FREQ_44] = AKC_PLL_44100HZ,)
        HW_HAVE_48_([HW_FREQ_48] = AKC_PLL_48000HZ,)
    };

    if ((unsigned)fsel >= HW_NUM_FREQ)
        fsel = HW_FREQ_DEFAULT;

    akc_write_masked(AK4537_MODE2, srctrl_table[fsel], FS_MASK);
}

#if defined(HAVE_RECORDING)
void audiohw_set_recsrc(int source)
{
    switch(source)
    {
    case AUDIO_SRC_PLAYBACK:
        /* disable mic power supply */
#if defined(SAMSUNG_YH920) || defined(SAMSUNG_YH925)
        akc_clear(AK4537_MIC, MPWRE);
#else
        akc_clear(AK4537_MIC, MPWRI);
#endif
        /* power down ADC, mic preamp and line amp */
        akc_clear(AK4537_PM1, PMADL | PMMICL | PMIPGL);
        akc_clear(AK4537_PM3, PMADR | PMMICR | PMIPGR);

        /* break ADC -> DAC connection */
        akc_clear(AK4537_MODE2, LOOP);

        break;

#if (INPUT_SRC_CAPS & SRC_CAP_FMRADIO)
    case AUDIO_SRC_FMRADIO:
        /* disable mic power supply */
#if defined(SAMSUNG_YH920) || defined(SAMSUNG_YH925)
        akc_clear(AK4537_MIC, MPWRE);
#else
        akc_clear(AK4537_MIC, MPWRI);
#endif
        /* disable mic preamp */
        akc_clear(AK4537_PM1, PMMICL);

        /* Select line2 input: Radio */
        akc_set(AK4537_PM3, INL | INR);
        /* route ALC output to ADC input */
        akc_set(AK4537_MIC, MICAD);
        /* set ALC (automatic level control) to manual mode */
        akc_clear(AK4537_ALC1, ALC1);

        /* set gain control to independent left & right gain */
        akc_set(AK4537_MIC, IPGAC);

        /* power up left channel input and ADC */
        akc_set(AK4537_PM1, PMADL | PMIPGL);
        /* power up right channel input and ADC */
        akc_set(AK4537_PM3, PMADR | PMIPGR);

        /* set line in vol = 0 dB */
        akc_write(AK4537_IPGAL, 0x2f);
        akc_write(AK4537_IPGAR, 0x2f);

        /* ADC -> DAC, external data to DAC ignored */
        akc_set(AK4537_MODE2, LOOP);

        break;
#endif /* INPUT_SRC_CAPS & SRC_CAP_FMRADIO */

#if (INPUT_SRC_CAPS & SRC_CAP_LINEIN)
    case AUDIO_SRC_LINEIN:
        /* disable mic power supply */
#if defined(SAMSUNG_YH920) || defined(SAMSUNG_YH925)
        akc_clear(AK4537_MIC, MPWRE);
#else
        akc_clear(AK4537_MIC, MPWRI);
#endif
        /* disable mic preamp */
        akc_clear(AK4537_PM1, PMMICL);

        /* Select line1 input */
        akc_clear(AK4537_PM3, INL | INR);
        /* route ALC output to ADC input */
        akc_set(AK4537_MIC, MICAD);
        /* set ALC (automatic level control) to manual mode */
        akc_clear(AK4537_ALC1, ALC1);

        /* set gain control to independent left & right gain */
        akc_set(AK4537_MIC, IPGAC);

        /* power up left channel input and ADC */
        akc_set(AK4537_PM1, PMADL | PMIPGL);
        /* power up right channel input and ADC */
        akc_set(AK4537_PM3, PMADR | PMIPGR);

        /* ADC -> DAC, external data to DAC ignored */
        akc_set(AK4537_MODE2, LOOP);

        break;
#endif /* INPUT_SRC_CAPS & SRC_CAP_LINEIN */

#if (INPUT_SRC_CAPS & SRC_CAP_MIC)
    case AUDIO_SRC_MIC:
        /* enable mic power supply */
#if defined(SAMSUNG_YH920) || defined(SAMSUNG_YH925)
        /* additionally select external mic */
        akc_set(AK4537_MIC, MPWRE | MSEL);
#else
        akc_set(AK4537_MIC, MPWRI);
#endif

        /* mic out is connected to line1 input */
        akc_clear(AK4537_PM3, INL | INR);

        /* route ALC output to ADC input */
        akc_set(AK4537_MIC, MICAD);
        /* set ALC (automatic level control) to manual mode */
        akc_clear(AK4537_ALC1, ALC1);
        /* set gain control to 'dependent' (left&right at the same time) */
        akc_clear(AK4537_MIC, IPGAC);
        /* power up mic preamp, left channel ADC and line in */
        akc_set(AK4537_PM1, PMMICL | PMIPGL | PMADL);
        /* power up right channel ADC and line in */
        akc_set(AK4537_PM3, PMADR | PMIPGR);

        /* ADC -> DAC, external data to DAC ignored */
        akc_set(AK4537_MODE2, LOOP);

        break;
#endif /* INPUT_SRC_CAPS & SRC_CAP_MIC) */

    } /* switch(source) */
}

void audiohw_set_recvol(int left, int right, int type)
{
    switch (type)
    {
    case AUDIO_GAIN_MIC:
        /* the mic preamp has a fixed gain of +15 dB. There's an additional
         * activatable +20dB mic gain stage. The signal is then routed to
         * the Line1 input, where you find the line attenuator with a range
         * from -23.5 to +12dB, so we have a total gain range of -8.0 .. +47dB.
         * NOTE: the datasheet state's different attenuator levels for mic and
         * line input, but that's not precise. The +15dB difference result only
         * from the mic stage.
         * NOTE2: the mic is connected to the line1 input (via mic preamp),
         * so if a line signal is present, you will always record a mixup.
         */
        /* If gain is > 20 dB we use the additional gain stage */
        if (left > 20) {
            akc_set(AK4537_MIC, MGAIN);
            left -= 20;
        }
        else {
            akc_clear(AK4537_MIC, MGAIN);
        }
        /* the remains is done by the line input amp */
        left = (left+8)*2;
        akc_write(AK4537_IPGAL, left);
        break;
    case AUDIO_GAIN_LINEIN:
        /* convert dB to register value */
        left = (left+23)*2+1;
        right = (right+23)*2+1;
        akc_write(AK4537_IPGAL, left);
        akc_write(AK4537_IPGAR, right);
        break;
    default:
        return;
    }
}
#endif /* HAVE_RECORDING */