summaryrefslogtreecommitdiff
path: root/firmware/drivers/audio/df1704.c
blob: 917cecb76713dfcd63c2fb67b71703eed9e32deb (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2013 Andrew Ryabinin
 *
 * 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 "system.h"
#include "df1704.h"
#include "config.h"
#include "audio.h"
#include "audiohw.h"

static void df1704_write_reg(const int reg, const unsigned int value)
{
    int i;

    df1704_set_ml_dir(0);
    df1704_set_ml(1);
    df1704_set_md(1);
    df1704_set_mc(1);

    for (i = (1<<15); i; i >>= 1) {
        udelay(40);
        df1704_set_mc(0);

        if ((reg|value) & i) {
            df1704_set_md(1);
        } else {
            df1704_set_md(0);
        }

        udelay(40);
        df1704_set_mc(1);
    }

    df1704_set_ml(0);
    udelay(130);
    df1704_set_ml(1);
    udelay(130);
    df1704_set_ml_dir(1);
}

static int vol_tenthdb2hw(const int tdb)
{
    if (tdb < DF1704_VOLUME_MIN) {
        return 0;
    } else if (tdb > DF1704_VOLUME_MAX) {
        return 0xff;
    } else {
        return (tdb/5+0xff);
    }
}


void audiohw_init(void)
{
    df1704_write_reg(DF1704_MODE(2),
                     DF1704_OW_24     |
                     DF1704_IW_16_I2S |
                     DF1704_DEM_OFF   |
                     DF1704_MUTE_OFF);
    df1704_write_reg(DF1704_MODE(3),
                     DF1704_CKO_HALF |
                     DF1704_SRO_SHARP|
                     DF1704_LRP_H    |
                     DF1704_I2S_ON);
}

void audiohw_mute(void)
{
    df1704_write_reg(DF1704_MODE(2),
                     DF1704_OW_24     |
                     DF1704_IW_16_I2S |
                     DF1704_DEM_OFF   |
                     DF1704_MUTE_ON);
}

void audiohw_unmute(void)
{
    df1704_write_reg(DF1704_MODE(2),
                     DF1704_OW_24     |
                     DF1704_IW_16_I2S |
                     DF1704_DEM_OFF   |
                     DF1704_MUTE_OFF);
}

void audiohw_preinit(void)
{
}

void audiohw_set_frequency(int fsel)
{
    (void)fsel;
}

void audiohw_set_volume(int vol_l, int vol_r)
{
    df1704_write_reg(DF1704_MODE(0), DF1704_LDL_ON|vol_tenthdb2hw(vol_l));
    df1704_write_reg(DF1704_MODE(1), DF1704_LDR_ON|vol_tenthdb2hw(vol_r));
}

void audiohw_set_filter_roll_off(int value)
{
    int mode3_val = DF1704_CKO_HALF |
                    DF1704_LRP_H    |
                    DF1704_I2S_ON;

    if (value == 0) {
        mode3_val |= DF1704_SRO_SHARP;
    } else {
        mode3_val |= DF1704_SRO_SLOW;
    }
    df1704_write_reg(DF1704_MODE(3), mode3_val);
}