2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-02-27 20:26:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Google, Inc
|
|
|
|
*/
|
|
|
|
|
2019-02-17 03:24:56 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_SOUND
|
|
|
|
|
2014-02-27 20:26:18 +00:00
|
|
|
#include <common.h>
|
2018-12-10 17:37:33 +00:00
|
|
|
#include <audio_codec.h>
|
2018-12-10 17:37:34 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <i2s.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2018-12-10 17:37:36 +00:00
|
|
|
#include <sound.h>
|
2014-02-27 20:26:18 +00:00
|
|
|
#include <asm/sdl.h>
|
|
|
|
|
2018-12-10 17:37:33 +00:00
|
|
|
struct sandbox_codec_priv {
|
|
|
|
int interface;
|
|
|
|
int rate;
|
|
|
|
int mclk_freq;
|
|
|
|
int bits_per_sample;
|
|
|
|
uint channels;
|
|
|
|
};
|
|
|
|
|
2018-12-10 17:37:34 +00:00
|
|
|
struct sandbox_i2s_priv {
|
|
|
|
int sum; /* Use to sum the provided audio data */
|
2019-02-17 03:24:56 +00:00
|
|
|
bool silent; /* Sound is silent, don't use SDL */
|
2018-12-10 17:37:34 +00:00
|
|
|
};
|
|
|
|
|
2018-12-10 17:37:36 +00:00
|
|
|
struct sandbox_sound_priv {
|
2020-02-03 14:36:06 +00:00
|
|
|
int setup_called; /* Incremented when setup() method is called */
|
|
|
|
bool active; /* TX data is being sent */
|
2019-02-17 03:24:54 +00:00
|
|
|
int sum; /* Use to sum the provided audio data */
|
|
|
|
bool allow_beep; /* true to allow the start_beep() interface */
|
|
|
|
int frequency_hz; /* Beep frequency if active, else 0 */
|
2018-12-10 17:37:36 +00:00
|
|
|
};
|
|
|
|
|
2018-12-10 17:37:33 +00:00
|
|
|
void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
|
|
|
|
int *mclk_freqp, int *bits_per_samplep,
|
|
|
|
uint *channelsp)
|
|
|
|
{
|
|
|
|
struct sandbox_codec_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
*interfacep = priv->interface;
|
|
|
|
*ratep = priv->rate;
|
|
|
|
*mclk_freqp = priv->mclk_freq;
|
|
|
|
*bits_per_samplep = priv->bits_per_sample;
|
|
|
|
*channelsp = priv->channels;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:37:34 +00:00
|
|
|
int sandbox_get_i2s_sum(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_i2s_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->sum;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:37:36 +00:00
|
|
|
int sandbox_get_setup_called(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->setup_called;
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:36:06 +00:00
|
|
|
int sandbox_get_sound_active(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->active;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:37:36 +00:00
|
|
|
int sandbox_get_sound_sum(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->sum;
|
|
|
|
}
|
|
|
|
|
2019-02-17 03:24:54 +00:00
|
|
|
void sandbox_set_allow_beep(struct udevice *dev, bool allow)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
priv->allow_beep = allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sandbox_get_beep_frequency(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return priv->frequency_hz;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:37:33 +00:00
|
|
|
static int sandbox_codec_set_params(struct udevice *dev, int interface,
|
|
|
|
int rate, int mclk_freq,
|
|
|
|
int bits_per_sample, uint channels)
|
|
|
|
{
|
|
|
|
struct sandbox_codec_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
priv->interface = interface;
|
|
|
|
priv->rate = rate;
|
|
|
|
priv->mclk_freq = mclk_freq;
|
|
|
|
priv->bits_per_sample = bits_per_sample;
|
|
|
|
priv->channels = channels;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:37:34 +00:00
|
|
|
static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
|
|
|
|
uint data_size)
|
|
|
|
{
|
|
|
|
struct sandbox_i2s_priv *priv = dev_get_priv(dev);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < data_size; i++)
|
|
|
|
priv->sum += ((uint8_t *)data)[i];
|
|
|
|
|
2019-02-17 03:24:56 +00:00
|
|
|
if (!priv->silent) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sandbox_sdl_sound_play(data, data_size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-12-10 17:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_i2s_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
|
2019-02-17 03:24:56 +00:00
|
|
|
struct sandbox_i2s_priv *priv = dev_get_priv(dev);
|
2018-12-10 17:37:34 +00:00
|
|
|
|
|
|
|
/* Use hard-coded values here */
|
|
|
|
uc_priv->rfs = 256;
|
|
|
|
uc_priv->bfs = 32;
|
|
|
|
uc_priv->audio_pll_clk = 192000000;
|
|
|
|
uc_priv->samplingrate = 48000;
|
|
|
|
uc_priv->bitspersample = 16;
|
|
|
|
uc_priv->channels = 2;
|
|
|
|
uc_priv->id = 1;
|
|
|
|
|
2019-02-17 03:24:56 +00:00
|
|
|
priv->silent = dev_read_bool(dev, "sandbox,silent");
|
|
|
|
|
|
|
|
if (priv->silent) {
|
|
|
|
log_warning("Sound is silenced\n");
|
|
|
|
} else if (sandbox_sdl_sound_init(uc_priv->samplingrate,
|
|
|
|
uc_priv->channels)) {
|
|
|
|
/* Ignore any error here - we'll just have no sound */
|
|
|
|
priv->silent = true;
|
|
|
|
}
|
2018-12-10 17:37:39 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-12-10 17:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_sound_setup(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
priv->setup_called++;
|
|
|
|
|
2018-12-10 17:37:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:37:36 +00:00
|
|
|
static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
|
|
|
|
{
|
|
|
|
struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < data_size; i++)
|
|
|
|
priv->sum += ((uint8_t *)data)[i];
|
|
|
|
|
|
|
|
return i2s_tx_data(uc_priv->i2s, data, data_size);
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:36:06 +00:00
|
|
|
static int sandbox_sound_stop_play(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
sandbox_sdl_sound_stop();
|
|
|
|
priv->active = false;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-17 03:24:54 +00:00
|
|
|
int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
if (!priv->allow_beep)
|
|
|
|
return -ENOSYS;
|
|
|
|
priv->frequency_hz = frequency_hz;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sandbox_sound_stop_beep(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_sound_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
if (!priv->allow_beep)
|
|
|
|
return -ENOSYS;
|
|
|
|
priv->frequency_hz = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:37:36 +00:00
|
|
|
static int sandbox_sound_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return sound_find_codec_i2s(dev);
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:37:33 +00:00
|
|
|
static const struct audio_codec_ops sandbox_codec_ops = {
|
|
|
|
.set_params = sandbox_codec_set_params,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_codec_ids[] = {
|
|
|
|
{ .compatible = "sandbox,audio-codec" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(sandbox_codec) = {
|
|
|
|
.name = "sandbox_codec",
|
|
|
|
.id = UCLASS_AUDIO_CODEC,
|
|
|
|
.of_match = sandbox_codec_ids,
|
|
|
|
.ops = &sandbox_codec_ops,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct sandbox_codec_priv),
|
2018-12-10 17:37:33 +00:00
|
|
|
};
|
2018-12-10 17:37:34 +00:00
|
|
|
|
|
|
|
static const struct i2s_ops sandbox_i2s_ops = {
|
|
|
|
.tx_data = sandbox_i2s_tx_data,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_i2s_ids[] = {
|
|
|
|
{ .compatible = "sandbox,i2s" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(sandbox_i2s) = {
|
|
|
|
.name = "sandbox_i2s",
|
|
|
|
.id = UCLASS_I2S,
|
|
|
|
.of_match = sandbox_i2s_ids,
|
|
|
|
.ops = &sandbox_i2s_ops,
|
|
|
|
.probe = sandbox_i2s_probe,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct sandbox_i2s_priv),
|
2018-12-10 17:37:34 +00:00
|
|
|
};
|
2018-12-10 17:37:36 +00:00
|
|
|
|
|
|
|
static const struct sound_ops sandbox_sound_ops = {
|
2019-02-17 03:24:54 +00:00
|
|
|
.setup = sandbox_sound_setup,
|
|
|
|
.play = sandbox_sound_play,
|
2020-02-03 14:36:06 +00:00
|
|
|
.stop_play = sandbox_sound_stop_play,
|
2019-02-17 03:24:54 +00:00
|
|
|
.start_beep = sandbox_sound_start_beep,
|
|
|
|
.stop_beep = sandbox_sound_stop_beep,
|
2018-12-10 17:37:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_sound_ids[] = {
|
|
|
|
{ .compatible = "sandbox,sound" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(sandbox_sound) = {
|
|
|
|
.name = "sandbox_sound",
|
|
|
|
.id = UCLASS_SOUND,
|
|
|
|
.of_match = sandbox_sound_ids,
|
|
|
|
.ops = &sandbox_sound_ops,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct sandbox_sound_priv),
|
2018-12-10 17:37:36 +00:00
|
|
|
.probe = sandbox_sound_probe,
|
|
|
|
};
|