summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/audio/wm8740.c
blob: d88f53bd7501730f3094bf8f9c88b8871e01a358 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 by Roman Stolyarov
 *
 * 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 "wm8740.h"
#include "config.h"
#include "audio.h"
#include "audiohw.h"

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

    for (i = (1<<15); i; i >>= 1) {
        udelay(1);
        wm8740_set_mc(0);
        if ((reg|value) & i) {
            wm8740_set_md(1);
        } else {
            wm8740_set_md(0);
        }
        udelay(1);
        wm8740_set_mc(1);
    }
    udelay(1);
    wm8740_set_ml(0);
    udelay(1);
    wm8740_set_mc(0);
    udelay(1);
    wm8740_set_ml(1);
    udelay(1);
}

static int vol_tenthdb2hw(const int tdb)
{
    if (tdb < WM8740_VOLUME_MIN) {
        return 0x00;
    } else if (tdb > WM8740_VOLUME_MAX) {
        return 0xff;
    } else {
        return ((tdb / 5 + 0xff) & 0xff);
    }
}

void audiohw_set_volume(int vol_l, int vol_r)
{
    wm8740_write_reg(WM8740_REG0, vol_tenthdb2hw(vol_l));
    wm8740_write_reg(WM8740_REG1, vol_tenthdb2hw(vol_r) | WM8740_LDR);
}

void audiohw_mute(void)
{
    wm8740_write_reg(WM8740_REG2, WM8740_MUT);
}

void audiohw_unmute(void)
{
    wm8740_write_reg(WM8740_REG2, 0x00);
}

void audiohw_init(void)
{
    wm8740_write_reg(WM8740_REG0, 0x00);
    wm8740_write_reg(WM8740_REG1, 0x00);
    wm8740_write_reg(WM8740_REG2, WM8740_MUT);
    wm8740_write_reg(WM8740_REG3, WM8740_I2S);
    wm8740_write_reg(WM8740_REG4, 0x00);
}

void audiohw_preinit(void)
{
}

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

void audiohw_set_filter_roll_off(int value)
{
    /* 0 = fast (sharp); 
       1 = slow */
    if (value == 0) {
        wm8740_write_reg(WM8740_REG3, WM8740_I2S);
    } else {
        wm8740_write_reg(WM8740_REG3, WM8740_I2S | WM8740_SR0);
    }
}