summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/audio/fiiolinux_codec.c
blob: d23e023564d2929edd20da3a95382cb5d655d7eb (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 *
 * Copyright (c) 2018 Marcin Bukat
 * Copyright (c) 2019 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 "config.h"
#include "audio.h"
#include "audiohw.h"
#include "system.h"
#include "kernel.h"
#include "panic.h"
#include "sysfs.h"
#include "alsa-controls.h"
#include "pcm-alsa.h"
#include <sys/ioctl.h>

static int fd_hw = -1;
static int ak_hw = -1;

static      int vol_sw[2] = {0};
static long int vol_hw[2] = {0};

static void hw_open(void)
{
    fd_hw = open("/dev/snd/controlC0", O_RDWR);
    if(fd_hw < 0)
        panicf("Cannot open '/dev/snd/controlC0'");

    ak_hw = open("/dev/ak4376", O_RDWR);
    if(ak_hw < 0)
        panicf("Cannot open '/dev/ak4376'");

    if(ioctl(ak_hw, 0x20003424, 0) < 0)
    {
        panicf("Call cmd AK4376_POWER_ON fail");
    }
}

static void hw_close(void)
{
    if(ioctl(ak_hw, 0x20003425, 0) < 0)
    {
        panicf("Call cmd AK4376_POWER_OFF fail");
    }
    close(ak_hw);
    close(fd_hw);
    ak_hw = fd_hw = -1;
}

void audiohw_preinit(void)
{
    alsa_controls_init();
    hw_open();
    // NOTE:
    // Of the exported controls, only these do anything:
    // 10 DACL Playback Volume
    // 11 DACR Playback Volume
    // 12 Low Mode Switch  (see table 25 in datasheet, not simple..)
}

void audiohw_postinit(void)
{
}

void audiohw_close(void)
{
    hw_close();
    alsa_controls_close();
}

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

static int muted = -1;

void audiohw_set_volume(int vol_l, int vol_r)
{
    int vol[2];

    if (fd_hw < 0)
       return;

    vol[0] = vol_l / 20;
    vol[1] = vol_r / 20;

    for (int i = 0; i < 2; i++)
    {
        if (vol[i] > -56)
        {
            if (vol[i] < -12)
            {
                vol_hw[i] = 1;
                vol_sw[i] = vol[i] + 12;
            }
            else
            {
                vol_hw[i] = 25 - (-vol[i] * 2);
                vol_sw[i] = 0;
            }
        }
        else
        {
            // Mute
            vol_hw[i] = 0;
            vol_sw[i] = 0;
        }
    }

    if (!muted) {
       alsa_controls_set_ints("DACL Playback Volume", 1, &vol_hw[0]);
       alsa_controls_set_ints("DACR Playback Volume", 1, &vol_hw[1]);
       pcm_alsa_set_digital_volume(vol_sw[0], vol_sw[1]);
    }
}

void audiohw_mute(int mute)
{
    long int vol0 = 0;

    if (fd_hw < 0 || muted == mute)
       return;

    if(mute)
    {
        alsa_controls_set_ints("DACL Playback Volume", 1, &vol0);
        alsa_controls_set_ints("DACR Playback Volume", 1, &vol0);
        pcm_alsa_set_digital_volume(0, 0);
    }
    else
    {
        alsa_controls_set_ints("DACL Playback Volume", 1, &vol_hw[0]);
        alsa_controls_set_ints("DACR Playback Volume", 1, &vol_hw[1]);
        pcm_alsa_set_digital_volume(vol_sw[0], vol_sw[1]);
    }
}

void audiohw_set_filter_roll_off(int value)
{
    if (fd_hw < 0)
       return;

    /* 0 = Sharp;
       1 = Slow;
       2 = Short Sharp
       3 = Short Slow */
#if 0 //  defined(FIIO_M3K)
    // AK4376 supports this but the control isn't wired into ALSA!
    long int value_hw = value;
    alsa_controls_set_ints("AK4376 Digital Filter", 1, &value_hw);
#endif
    (void)value;
}