summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/tlv320.c
blob: bafd77cd39bbf2b780b77051c0baf7658e0871be (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Christian Gmeiner
 *
 * 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 "i2c-coldfire.h"
#include "tlv320.h"

/* local functions and definations */
#define TLV320_ADDR 0x34

struct tlv320_info
{
    int vol_l;
    int vol_r;
} tlv320;

/* Definition of a playback configuration to start with */
#define NUM_DEFAULT_REGS 10
unsigned tlv320_defaults[2*NUM_DEFAULT_REGS] =
{
    REG_PC,         PC_ON | PC_OSC | PC_CLK | PC_DAC | ~PC_OUT,     /* do we need to enable osciliator and clock? */
    REG_LLIV,       LLIV_LIM,                                       /* mute adc input */
    REG_RLIV,       RLIV_RIM,                                       /* mute adc input */
    REG_LHV,        LHV_LHV(HEADPHONE_MUTE),                        /* mute headphone */
    REG_RHV,        RHV_RHV(HEADPHONE_MUTE),                        /* mute headphone */
    REG_AAP,        AAP_MICM,                                       /* mute microphone */
    REG_DAP,        DAP_DEEMP_DIS,                                  /* de-emphasis control: disabled */
    REG_DAIF,       DAIF_FOR_I2S | DAIF_IWL_24 | ~DAIF_MS,          /* i2s with 24 bit data len and slave mode */
    REG_SRC,        0,                                              /* ToDo */
    REG_DIA,        DIA_ACT,                                        /* activate digital interface */
};
unsigned tlv320_regs[0xf];

void tlv320_write_reg(unsigned reg, unsigned value)
{
    unsigned data[3];

    data[0] = TLV320_ADDR;
    data[1] = reg << 1;
    data[2] = value & 0xff; 

    if (i2c_write(1, data, 3) != 3)
    {
        DEBUGF("tlv320 error reg=0x%x", reg);
        return;
    } 

    tlv320_regs[reg] = value;
}

/* Returns 0 if successful or -1 if some register failed */
void tlv320_set_regs()
{
    int i;
    memset(tlv320_regs, 0, sizeof(tlv320_regs));

    /* Initialize all registers */
    for (i=0; i<NUM_DEFAULT_REGS; i++)
    {
        unsigned reg = tlv320_defaults[i*2+0];
        unsigned value = tlv320_defaults[i*2+1];

        tlv320_write_reg(reg, value);
    }
}

/* public functions */

/**
 * Init our tlv with default values
 */
void tlv320_init()
{
    tlv320_reset();
    tlv320_set_regs();
}

/**
 * Resets tlv320 to default values
 */
void tlv320_reset()
{
    tlv320_write_reg(REG_RR, RR_RESET):
}

void tlv320_enable_output(bool enable)
{
    unsigned value = tlv320regs[REG_PC];

    if (enable)
        value |= PC_OUT;
    else
        value &= ~PC_OUT;

    tlv320_write_reg(REG_PC, value);
}

/**
 * Sets left and right headphone volume  (127(max) to 48(muted))
 */
void tlv320_set_headphone_vol(int vol_l, int vol_r)
{
    unsigned value_l = tlv320_regs[REG_LHV];
    unsigned value_r = tlv320_regs[REG_RHV];

    /* keep track of current setting */
    tlv320.vol_l = vol_l;
    tlv320.vol_r = vol_r;

    /* set new values in local register holders */
    value_l |= LHV_LHV(vol_l);
    value_r |= LHV_LHV(vol_r);

    /* update */
    tlv320_write_reg(REG_LHV, value_l);
    tlv320_write_reg(REG_RHV, value_r);
}

/**
 * Sets left and right linein volume  (31(max) to 0(muted))
 */
void tlv320_set_linein_vol(int vol_l, int vol_r)
{
    unsigned value_l = tlv320regs[REG_LLIV];
    unsigned value_r = tlv320regs[REG_RLIV];

    value_l |= LLIV_LHV(vol_l);
    value_r |= RLIV_RHV(vol_r);

    tlv320_write_reg(REG_LLIV, value_l);
    tlv320_write_reg(REG_RLIV, value_r);
}

/**
 * Mute (mute=true) or enable sound (mute=false)
 *
 */
void tlv320_mute(bool mute)
{
    unsigned value_l = tlv320regs[REG_LHV];
    unsigned value_r = tlv320regs[REG_RHV];

    if (mute)
    {
        value_l |= LHV_LHV(HEADPHONE_MUTE);
        value_r |= RHV_RHV(HEADPHONE_MUTE);
    }
    else
    {
        value_l |= LHV_LHV(tlv320.vol_l);
        value_r |= RHV_RHV(tlv320.vol_r);
    }

    tlv320_write_reg(REG_LHV, value_r);
    tlv320_write_reg(REG_RHV, value_r);
}

void tlv320_close()
{
    /* todo */
}

void tlv320_enable_recording(bool source_mic)
{
    unsigned value_pc = tlv320regs[REG_PC];
    unsigned value_aap = tlv320regs[REG_AAP];

    /* select source*/
    if (source_mic)
    {
        value_aap &= ~AAP_INSEL;
        value_pc |= PC_MIC;
    }
    else
    {
        value_aap |= AAP_INSEL;
        value_pc |= PC_LINE;
    }

    /* poweron adc */
    value_pc |= PC_ADC;

    tlv320_write_reg(REG_AAP, value_aap);
    tlv320_write_reg(REG_PC, value_pc);
}

void tlv320_disable_recording()
{
    unsigned value = tlv320regs[REG_PC];

    /* powerdown mic, linein and adc */
    value &= ~(PC_MIC | PC_LINE | PC_ADC);

    /* powerdown mic, linein and adc */
    tlv320_write_reg(REG_PC, value);
}