summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/as3514.c
blob: f7eb0bd0ecc1728e5341115663e68dfce44bec17 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Driver for AS3514 audio codec
 *
 * Copyright (c) 2007 Daniel Ankers
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "lcd.h"
#include "cpu.h"
#include "kernel.h"
#include "thread.h"
#include "power.h"
#include "debug.h"
#include "system.h"
#include "sprintf.h"
#include "button.h"
#include "string.h"
#include "file.h"
#include "buffer.h"
#include "audio.h"
#include "backlight.h"

#include "as3514.h"
#include "i2s.h"
#include "i2c-pp.h"

/* convert tenth of dB volume to master volume register value */
int tenthdb2master(int db)
{
    /* +1..07 to -45.43dB in 1.5dB steps == 32 levels = 5 bits */
    /* 11111 == +1.07dB  (0x1f) = 31) */
    /* 11110 == -0.43dB  (0x1e) = 30) */
    /* 00001 == -43.93dB (0x01) */
    /* 00000 == -45.43dB (0x00) */

    if (db < VOLUME_MIN) {
        return 0x0;
    } else if (db >= VOLUME_MAX) {
        return 0x1f;
    } else {
        return((db-VOLUME_MIN)/15); /* VOLUME_MIN is negative */
    }
}

/* convert tenth of dB volume (-405..60) to mixer volume register value */
int tenthdb2mixer(int db)
{
    /* FIXME: Make this sensible */
    if (db < -405) {
        return 0x0;
    } else if (db >= 60) {
        return 0x1f;
    } else {
        return((db+405)/15);
    }
}

void audiohw_reset(void);

/*
 * Initialise the PP I2C and I2S.
 */
int audiohw_init(void) {
    /* reset I2C */
    i2c_init();

    /* normal outputs for CDI and I2S pin groups */
    DEV_INIT &= ~0x300;

    /*mini2?*/
    outl(inl(0x70000010) & ~0x3000000, 0x70000010);
    /*mini2?*/

    /* device reset */
    DEV_RS |= 0x800;
    DEV_RS &=~0x800;

    /* device enable */
    DEV_EN |= 0x807;

    /* enable external dev clock clocks */
    DEV_EN |= 0x2;

    /* external dev clock to 24MHz */
    outl(inl(0x70000018) & ~0xc, 0x70000018);

    i2s_reset();

    /* Set ADC off, mixer on, DAC on, line out off, line in off, mic off */
    pp_i2c_send(AS3514_I2C_ADDR, AUDIOSET1, 0x60); /* Turn on DAC and mixer */
    pp_i2c_send(AS3514_I2C_ADDR, PLLMODE, 0x04);
    pp_i2c_send(AS3514_I2C_ADDR, DAC_L, 0x50); /* DAC mute off, -16.5dB gain */
    pp_i2c_send(AS3514_I2C_ADDR, DAC_R, 0x10); /* DAC -16.5dB gain to prevent overloading
                                       the headphone amp */
    return 0;
}

void audiohw_postinit(void)
{
}

/* Silently enable / disable audio output */
void audiohw_enable_output(bool enable)
{
    if (enable)
    {
        /* reset the I2S controller into known state */
        i2s_reset();

        pp_i2c_send(AS3514_I2C_ADDR, HPH_OUT_L, 0xc0); /* Mute on, power on */
        audiohw_mute(0);
    } else {
        audiohw_mute(1);
        pp_i2c_send(AS3514_I2C_ADDR, HPH_OUT_L, 0x80); /* Mute on, power off */
    }
}

int audiohw_set_master_vol(int vol_l, int vol_r)
{
    vol_l &= 0x1f;
    vol_r &= 0x1f;
    pp_i2c_send(AS3514_I2C_ADDR, HPH_OUT_R, vol_r);
    pp_i2c_send(AS3514_I2C_ADDR, HPH_OUT_L, 0x40 | vol_l);

    return 0;
}

int audiohw_set_lineout_vol(int vol_l, int vol_r)
{
    pp_i2c_send(AS3514_I2C_ADDR, LINE_OUT_R, vol_r);
    pp_i2c_send(AS3514_I2C_ADDR, LINE_OUT_L, 0x40 | vol_l);

    return 0;
}

int audiohw_set_mixer_vol(int channel1, int channel2)
{
    (void)channel1;
    (void)channel2;

    return 0;
}

/* We are using Linear bass control */
void audiohw_set_bass(int value)
{
    (void)value;
}

void audiohw_set_treble(int value)
{
    (void)value;
}

int audiohw_mute(int mute)
{
    int curr;

    curr = i2c_readbyte(AS3514_I2C_ADDR, HPH_OUT_L);
    if (mute)
    {
        pp_i2c_send(AS3514_I2C_ADDR, HPH_OUT_L, curr | 0x80);
    } else {
        pp_i2c_send(AS3514_I2C_ADDR, HPH_OUT_L, curr & ~(0x80));
    }

    return 0;
}

/* Nice shutdown of WM8758 codec */
void audiohw_close(void)
{
    audiohw_mute(1);
}

/* Change the order of the noise shaper, 5th order is recommended above 32kHz */
void audiohw_set_nsorder(int order)
{
    (void)order;
}

void audiohw_set_sample_rate(int sampling_control)
{
    (void)sampling_control;
    /* TODO: Implement this by adjusting the I2S Master clock */
}

void audiohw_enable_recording(bool source_mic)
{
    (void)source_mic;
    /* TODO */
}

void audiohw_disable_recording(void) {
    /* TODO */
}

void audiohw_set_recvol(int left, int right, int type) {

    (void)left;
    (void)right;
    (void)type;
}

void audiohw_set_monitor(int enable) {

    (void)enable;
}

void audiohw_set_equalizer_band(int band, int freq, int bw, int gain)
{
    (void)band;
    (void)freq;
    (void)bw;
    (void)gain;
}