summaryrefslogtreecommitdiff
path: root/bsp/nxp/lpc/lpc54114-lite/drivers/audio/drv_sound.c
blob: 4e49a619ade7b1d779b4efedf4deb18ab6461ee3 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-11-17     LiWeiHao     First implementation
 */

#include "drv_sound.h"
#include "fsl_common.h"
#include "fsl_iocon.h"
#include "fsl_dma.h"
#include "fsl_i2s.h"
#include "fsl_i2s_dma.h"
#include "fsl_wm8904.h"
#include "fsl_i2c.h"

#define TX_FIFO_SIZE (4096)

#define I2S_TX I2S1
#define I2S_RX I2S0

#define I2S_DMA_TX 15
#define I2S_DMA_RX 12

#ifndef CODEC_I2C_NAME
    #define CODEC_I2C_NAME "i2c4"
#endif

struct sound_device
{
    wm8904_handle_t wm8904_handle;
    dma_handle_t tx_dma_handle;
    i2s_dma_handle_t tx_i2s_dma_handle;
    struct rt_audio_device audio;
    struct rt_audio_configure replay_config;
    rt_uint8_t volume;
    rt_uint8_t *tx_fifo;
};

const pll_setup_t pll_setup =
{
    .syspllctrl = SYSCON_SYSPLLCTRL_BANDSEL_MASK | SYSCON_SYSPLLCTRL_SELP(0x1FU) | SYSCON_SYSPLLCTRL_SELI(0x8U),
    .syspllndec = SYSCON_SYSPLLNDEC_NDEC(0x2DU),
    .syspllpdec = SYSCON_SYSPLLPDEC_PDEC(0x42U),
    .syspllssctrl = {SYSCON_SYSPLLSSCTRL0_MDEC(0x34D3U) | SYSCON_SYSPLLSSCTRL0_SEL_EXT_MASK, 0x00000000U},
    .pllRate = 24576000U, /* 16 bits * 2 channels * 44.1 kHz * 16 */
    .flags = PLL_SETUPFLAG_WAITLOCK
};

static struct sound_device snd_dev;

void i2s_tx_transfer_callback(I2S_Type *base,
                              i2s_dma_handle_t *handle,
                              status_t completionStatus,
                              void *userData)
{
    struct sound_device *snd_dev = (struct sound_device *)userData;
    rt_audio_tx_complete(&snd_dev->audio);
}

static rt_err_t lpc_audio_init(struct rt_audio_device *audio)
{
    i2s_config_t tx_i2s_config;
    wm8904_config_t wm8904_config;

    CLOCK_EnableClock(kCLOCK_Iocon);
    CLOCK_EnableClock(kCLOCK_InputMux);
    CLOCK_EnableClock(kCLOCK_Gpio0);
    CLOCK_EnableClock(kCLOCK_Gpio1);

    CLOCK_AttachClk(kFRO12M_to_SYS_PLL);
    CLOCK_AttachClk(kSYS_PLL_to_FLEXCOMM7);

    RESET_PeripheralReset(kFC7_RST_SHIFT_RSTn);

    CLOCK_SetPLLFreq(&pll_setup);
    CLOCK_AttachClk(kSYS_PLL_to_MCLK);
    SYSCON->MCLKDIV = SYSCON_MCLKDIV_DIV(0U);

    // Flexcomm 7 I2S Tx
    IOCON_PinMuxSet(IOCON, 1, 12, IOCON_FUNC4 | IOCON_DIGITAL_EN); /* Flexcomm 7 / SCK */
    IOCON_PinMuxSet(IOCON, 1, 13, IOCON_FUNC4 | IOCON_DIGITAL_EN);  /* Flexcomm 7 / SDA */
    IOCON_PinMuxSet(IOCON, 1, 14, IOCON_FUNC4 | IOCON_DIGITAL_EN);  /* Flexcomm 7 / WS */

    /* MCLK output for I2S */
    IOCON_PinMuxSet(IOCON, 1, 17, IOCON_FUNC4 | IOCON_MODE_INACT | IOCON_DIGITAL_EN);
    SYSCON->MCLKIO = 1U;

    WM8904_GetDefaultConfig(&wm8904_config);
    snd_dev.wm8904_handle.i2c = (struct rt_i2c_bus_device *)rt_device_find(CODEC_I2C_NAME);
    if (WM8904_Init(&snd_dev.wm8904_handle, &wm8904_config) != kStatus_Success)
    {
        rt_kprintf("wm8904 init failed\n");
        return -RT_ERROR;
    }

    WM8904_SetMute(&snd_dev.wm8904_handle, RT_TRUE, RT_TRUE);

    I2S_TxGetDefaultConfig(&tx_i2s_config);
    tx_i2s_config.divider = CLOCK_GetPllOutFreq() / 48000U / 16 / 2;
    I2S_TxInit(I2S_TX, &tx_i2s_config);

    DMA_Init(DMA0);

    DMA_EnableChannel(DMA0, I2S_DMA_TX);
    DMA_SetChannelPriority(DMA0, I2S_DMA_TX, kDMA_ChannelPriority3);
    DMA_CreateHandle(&snd_dev.tx_dma_handle, DMA0, I2S_DMA_TX);

    I2S_TxTransferCreateHandleDMA(I2S_TX,
                                  &snd_dev.tx_i2s_dma_handle,
                                  &snd_dev.tx_dma_handle,
                                  i2s_tx_transfer_callback,
                                  (void *)&snd_dev);

    return RT_EOK;
}

static rt_err_t lpc_audio_start(struct rt_audio_device *audio, int stream)
{
    RT_ASSERT(audio != RT_NULL);

    if (stream == AUDIO_STREAM_REPLAY)
    {
        struct rt_audio_caps caps;
        caps.main_type = AUDIO_TYPE_MIXER;
        caps.sub_type = AUDIO_MIXER_VOLUME;
        audio->ops->getcaps(audio, &caps);
        audio->ops->configure(audio, &caps);
        rt_audio_tx_complete(audio);
    }
    return RT_EOK;
}

static rt_err_t lpc_audio_stop(struct rt_audio_device *audio, int stream)
{
    if (stream == AUDIO_STREAM_REPLAY)
    {
        WM8904_SetMute(&snd_dev.wm8904_handle, RT_TRUE, RT_TRUE);
        I2S_TransferAbortDMA(I2S_TX, &snd_dev.tx_i2s_dma_handle);
    }
    return RT_EOK;
}

static rt_err_t lpc_audio_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
{
    rt_err_t result = RT_EOK;
    struct sound_device *snd_dev;

    RT_ASSERT(audio != RT_NULL);
    snd_dev = (struct sound_device *)audio->parent.user_data;

    switch (caps->main_type)
    {
    case AUDIO_TYPE_QUERY: /* qurey the types of hw_codec device */
    {
        switch (caps->sub_type)
        {
        case AUDIO_TYPE_QUERY:
            caps->udata.mask = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_MIXER;
            break;

        default:
            result = -RT_ERROR;
            break;
        }

        break;
    }

    case AUDIO_TYPE_OUTPUT: /* Provide capabilities of OUTPUT unit */
    {
        switch (caps->sub_type)
        {
        case AUDIO_DSP_PARAM:
            caps->udata.config.samplerate   = snd_dev->replay_config.samplerate;
            caps->udata.config.channels     = snd_dev->replay_config.channels;
            caps->udata.config.samplebits   = snd_dev->replay_config.samplebits;
            break;

        case AUDIO_DSP_SAMPLERATE:
            caps->udata.config.samplerate   = snd_dev->replay_config.samplerate;
            break;

        case AUDIO_DSP_CHANNELS:
            caps->udata.config.channels     = snd_dev->replay_config.channels;
            break;

        case AUDIO_DSP_SAMPLEBITS:
            caps->udata.config.samplebits   = snd_dev->replay_config.samplebits;
            break;

        default:
            result = -RT_ERROR;
            break;
        }

        break;
    }

    case AUDIO_TYPE_MIXER: /* report the Mixer Units */
    {
        switch (caps->sub_type)
        {
        case AUDIO_MIXER_QUERY:
            caps->udata.mask = AUDIO_MIXER_VOLUME;
            break;

        case AUDIO_MIXER_VOLUME:
            caps->udata.value = snd_dev->volume;
            break;

        default:
            result = -RT_ERROR;
            break;
        }

        break;
    }

    default:
        result = -RT_ERROR;
        break;
    }

    return result;
}

static rt_err_t lpc_audio_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
{
    rt_err_t result = RT_EOK;
    struct sound_device *snd_dev = audio->parent.user_data;

    switch (caps->main_type)
    {
    case AUDIO_TYPE_MIXER:
    {
        switch (caps->sub_type)
        {
        case AUDIO_MIXER_MUTE:
        {
            WM8904_SetMute(&snd_dev->wm8904_handle, RT_TRUE, RT_TRUE);
            snd_dev->volume = 0;
            break;
        }

        case AUDIO_MIXER_VOLUME:
        {
            int volume = caps->udata.value / 2;
            WM8904_SetMute(&snd_dev->wm8904_handle, RT_FALSE, RT_FALSE);
            WM8904_SetVolume(&snd_dev->wm8904_handle, volume, volume);
            snd_dev->volume = volume;
            break;
        }
        }

        break;
    }
    case AUDIO_TYPE_OUTPUT:
    {
        switch (caps->sub_type)
        {
        case AUDIO_DSP_PARAM:
        {
            struct rt_audio_configure config = caps->udata.config;
            i2s_config_t tx_i2s_config;
            snd_dev->replay_config.channels = config.channels;
            snd_dev->replay_config.samplebits = config.samplebits;
            snd_dev->replay_config.samplerate = config.samplerate;
            I2S_TxGetDefaultConfig(&tx_i2s_config);
            tx_i2s_config.divider = CLOCK_GetPllOutFreq() / config.samplerate / 16 / 2;
            I2S_TxInit(I2S_TX, &tx_i2s_config);
            break;
        }

        case AUDIO_DSP_SAMPLERATE:
        {
            struct rt_audio_configure config = caps->udata.config;
            i2s_config_t tx_i2s_config;
            snd_dev->replay_config.samplerate = config.samplerate;
            I2S_TxGetDefaultConfig(&tx_i2s_config);
            tx_i2s_config.divider = CLOCK_GetPllOutFreq() / config.samplerate / 16 / 2;
            I2S_TxInit(I2S_TX, &tx_i2s_config);
            break;
        }

        default:
            result = -RT_ERROR;
            break;
        }
        break;
    }
    }

    return result;
}

static rt_ssize_t lpc_audio_transmit(struct rt_audio_device *audio, const void *writeBuf, void *readBuf, rt_size_t size)
{
    RT_ASSERT(audio != RT_NULL);
    i2s_transfer_t transfer;
    transfer.data = (uint8_t *)writeBuf;
    transfer.dataSize = size;
    I2S_TxTransferSendDMA(I2S_TX, &snd_dev.tx_i2s_dma_handle, transfer);

    return RT_EOK;
}

static void lpc_audio_buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info)
{
    RT_ASSERT(audio != RT_NULL);
    /**
     *               TX_FIFO
     * +----------------+----------------+
     * |     block1     |     block2     |
     * +----------------+----------------+
     *  \  block_size  /
     */
    info->buffer      = snd_dev.tx_fifo;
    info->total_size  = TX_FIFO_SIZE;
    info->block_size  = TX_FIFO_SIZE / 2;
    info->block_count = 2;
}

static struct rt_audio_ops audio_ops =
{
    .getcaps = lpc_audio_getcaps,
    .configure = lpc_audio_configure,
    .init = lpc_audio_init,
    .start = lpc_audio_start,
    .stop = lpc_audio_stop,
    .transmit = lpc_audio_transmit,
    .buffer_info = lpc_audio_buffer_info,
};

int rt_hw_sound_init(void)
{
    rt_uint8_t *tx_fifo = RT_NULL;

    tx_fifo = rt_malloc(TX_FIFO_SIZE);
    if (tx_fifo == NULL)
    {
        return -RT_ENOMEM;
    }
    snd_dev.tx_fifo = tx_fifo;

    /* init default configuration */
    {
        snd_dev.replay_config.samplerate = 44100;
        snd_dev.replay_config.channels   = 2;
        snd_dev.replay_config.samplebits = 16;
        snd_dev.volume                   = 30;
    }

    snd_dev.audio.ops = &audio_ops;
    rt_audio_register(&snd_dev.audio, "sound0", RT_DEVICE_FLAG_WRONLY, &snd_dev);
    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_sound_init);