summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/imx233/audio-imx233.c
blob: 40218f4aa0f7fa14a82168c3477d1fd24012a2d3 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2013 by Amaury Pouly
 *
 * 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 "audiohw.h"
#include "audio-imx233.h"
#include "pinctrl-imx233.h"

void imx233_audio_preinit(void)
{
#ifdef IMX233_AUDIO_HP_GATE_BANK
    imx233_pinctrl_acquire(IMX233_AUDIO_HP_GATE_BANK, IMX233_AUDIO_HP_GATE_PIN, "hp_gate");
    imx233_pinctrl_set_function(IMX233_AUDIO_HP_GATE_BANK, IMX233_AUDIO_HP_GATE_PIN, PINCTRL_FUNCTION_GPIO);
    imx233_pinctrl_enable_gpio(IMX233_AUDIO_HP_GATE_BANK, IMX233_AUDIO_HP_GATE_PIN, true);
    imx233_audio_enable_hp(false);
#endif
#ifdef IMX233_AUDIO_SPKR_GATE_BANK
    imx233_pinctrl_acquire(IMX233_AUDIO_SPKR_GATE_BANK, IMX233_AUDIO_SPKR_GATE_PIN, "spkr_gate");
    imx233_pinctrl_set_function(IMX233_AUDIO_SPKR_GATE_BANK, IMX233_AUDIO_SPKR_GATE_PIN, PINCTRL_FUNCTION_GPIO);
    imx233_pinctrl_enable_gpio(IMX233_AUDIO_SPKR_GATE_BANK, IMX233_AUDIO_SPKR_GATE_PIN, true);
    imx233_audio_enable_spkr(false);
#endif
}

void imx233_audio_postinit(void)
{
    imx233_audio_enable_hp(true);
}
// enable/disable the HP audio gate (typically using a GPIO)
void imx233_audio_enable_hp(bool en)
{
#ifdef IMX233_AUDIO_HP_GATE_BANK
# ifdef IMX233_AUDIO_HP_GATE_INVERTED
    en = !en;
# endif
    imx233_pinctrl_set_gpio(IMX233_AUDIO_HP_GATE_BANK, IMX233_AUDIO_HP_GATE_PIN, en);
#else
    (void) en;
#endif
}
// enable/disable the speaker audio gate (typically using a GPIO)
void imx233_audio_enable_spkr(bool en)
{
#ifdef IMX233_AUDIO_SPKR_GATE_BANK
# ifdef IMX233_AUDIO_SPKR_GATE_INVERTED
    en = !en;
# endif
    imx233_pinctrl_set_gpio(IMX233_AUDIO_SPKR_GATE_BANK, IMX233_AUDIO_SPKR_GATE_PIN, en);
#else
    (void) en;
#endif
}

static int input_source = AUDIO_SRC_PLAYBACK;
static unsigned input_flags = 0;
static int output_source = AUDIO_SRC_PLAYBACK;

static void select_audio_path(void)
{
    const bool recording = input_flags & SRCF_RECORDING;
    (void) recording; // it is not always used

    switch(input_source)
    {
        default:
            /* playback and no recording */
            input_source = AUDIO_SRC_PLAYBACK;
            /* fallthrough */
        case AUDIO_SRC_PLAYBACK:
            audiohw_set_monitor(false);
#if defined(HAVE_RECORDING)
            audiohw_disable_recording();
#endif
            break;

#if defined(HAVE_RECORDING) && (INPUT_SRC_CAPS & SRC_CAP_MIC)
        /* recording only */
        case AUDIO_SRC_MIC:
            audiohw_set_monitor(false);
            audiohw_enable_recording(true);
            break;
#endif

#if (INPUT_SRC_CAPS & SRC_CAP_FMRADIO)
        /* recording and playback */
        case AUDIO_SRC_FMRADIO:
            audiohw_set_monitor(true);
#if defined(HAVE_RECORDING)
            if(recording)
                audiohw_enable_recording(false);
            else
                audiohw_disable_recording();
#endif
            break;
#endif /* (INPUT_SRC_CAPS & SRC_CAP_FMRADIO) */
    }
}

void audio_input_mux(int source, unsigned flags)
{
    input_source = source;
    input_flags = flags;
    select_audio_path();
}

void audio_set_output_source(int source)
{
    output_source = source;
    select_audio_path();
}