dm: sound: Create a uclass for audio codecs

An audio codec provides a way to convert digital data to sound and vice
versa. Add a simple uclass which just supports setting the parameters for
the codec.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2018-12-10 10:37:33 -07:00
parent 45863db5a9
commit ce6d99a056
9 changed files with 182 additions and 1 deletions

View File

@ -42,7 +42,12 @@
osd0 = "/osd";
};
cros_ec: cros-ec {
audio: audio-codec {
compatible = "sandbox,audio-codec";
#sound-dai-cells = <1>;
};
cros_ec: cros-ec {
reg = <0 0>;
compatible = "google,cros-ec-sandbox";

View File

@ -121,4 +121,14 @@ int sandbox_pwm_get_config(struct udevice *dev, uint channel, uint *period_nsp,
*/
void sandbox_sf_set_block_protect(struct udevice *dev, int bp_mask);
/**
* sandbox_get_codec_params() - Read back codec parameters
*
* This reads back the parameters set by audio_codec_set_params() for the
* sandbox audio driver. Arguments are as for that function.
*/
void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
int *mclk_freqp, int *bits_per_samplep,
uint *channelsp);
#endif

View File

@ -4,6 +4,7 @@
# R. Chandrasekar <rcsekar@samsung.com>
obj-$(CONFIG_SOUND) += sound.o
obj-$(CONFIG_DM_SOUND) += codec-uclass.o
obj-$(CONFIG_I2S) += sound-i2s.o
obj-$(CONFIG_I2S_SAMSUNG) += samsung-i2s.o
obj-$(CONFIG_SOUND_SANDBOX) += sandbox.o

View File

@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2018 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <dm.h>
#include <audio_codec.h>
int audio_codec_set_params(struct udevice *dev, int interface, int rate,
int mclk_freq, int bits_per_sample, uint channels)
{
struct audio_codec_ops *ops = audio_codec_get_ops(dev);
if (!ops->set_params)
return -ENOSYS;
return ops->set_params(dev, interface, rate, mclk_freq, bits_per_sample,
channels);
}
UCLASS_DRIVER(audio_codec) = {
.id = UCLASS_AUDIO_CODEC,
.name = "audio-codec",
};

View File

@ -4,9 +4,19 @@
*/
#include <common.h>
#include <dm.h>
#include <audio_codec.h>
#include <asm/sound.h>
#include <asm/sdl.h>
struct sandbox_codec_priv {
int interface;
int rate;
int mclk_freq;
int bits_per_sample;
uint channels;
};
int sound_play(uint32_t msec, uint32_t frequency)
{
sandbox_sdl_sound_start(frequency);
@ -20,3 +30,48 @@ int sound_init(const void *blob)
{
return sandbox_sdl_sound_init();
}
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;
}
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;
}
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,
.priv_auto_alloc_size = sizeof(struct sandbox_codec_priv),
};

48
include/audio_codec.h Normal file
View File

@ -0,0 +1,48 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright 2018 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#ifndef __AUDIO_CODEC_H__
#define __AUDIO_CODEC_H__
/*
* An audio codec turns digital data into sound with various parameters to
* control its operation.
*/
/* Operations for sound */
struct audio_codec_ops {
/**
* set_params() - Set audio codec parameters
*
* @dev: Sound device
* @inteface: Interface number to use on codec
* @rate: Sampling rate in Hz
* @mclk_freq: Codec clock frequency in Hz
* @bits_per_sample: Must be 16 or 24
* @channels: Number of channels to use (1=mono, 2=stereo)
* @return 0 if OK, -ve on error
*/
int (*set_params)(struct udevice *dev, int interface, int rate,
int mclk_freq, int bits_per_sample, uint channels);
};
#define audio_codec_get_ops(dev) ((struct audio_codec_ops *)(dev)->driver->ops)
/**
* audio_codec_set_params() - Set audio codec parameters
*
* @dev: Sound device
* @inteface: Interface number to use on codec
* @rate: Sampling rate in Hz
* @mclk_freq: Codec clock frequency in Hz
* @bits_per_sample: Must be 16 or 24
* @channels: Number of channels to use (1=mono, 2=stereo)
* @return 0 if OK, -ve on error
*/
int audio_codec_set_params(struct udevice *dev, int interface, int rate,
int mclk_freq, int bits_per_sample, uint channels);
#endif /* __AUDIO_CODEC_H__ */

View File

@ -29,6 +29,7 @@ enum uclass_id {
/* U-Boot uclasses start here - in alphabetical order */
UCLASS_ADC, /* Analog-to-digital converter */
UCLASS_AHCI, /* SATA disk controller */
UCLASS_AUDIO_CODEC, /* Audio codec with control and data path */
UCLASS_AXI, /* AXI bus */
UCLASS_BLK, /* Block device */
UCLASS_BOARD, /* Device information from hardware */

View File

@ -13,6 +13,7 @@ obj-$(CONFIG_UT_DM) += test-uclass.o
# subsystem you must add sandbox tests here.
obj-$(CONFIG_UT_DM) += core.o
ifneq ($(CONFIG_SANDBOX),)
obj-$(CONFIG_DM_SOUND) += audio.o
obj-$(CONFIG_BLK) += blk.o
obj-$(CONFIG_BOARD) += board.o
obj-$(CONFIG_CLK) += clk.o

34
test/dm/audio.c Normal file
View File

@ -0,0 +1,34 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2018 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <audio_codec.h>
#include <dm.h>
#include <dm/test.h>
#include <test/ut.h>
#include <asm/test.h>
/* Basic test of the audio codec uclass */
static int dm_test_audio(struct unit_test_state *uts)
{
int interface, rate, mclk_freq, bits_per_sample;
struct udevice *dev;
uint channels;
/* check probe success */
ut_assertok(uclass_first_device_err(UCLASS_AUDIO_CODEC, &dev));
ut_assertok(audio_codec_set_params(dev, 1, 2, 3, 4, 5));
sandbox_get_codec_params(dev, &interface, &rate, &mclk_freq,
&bits_per_sample, &channels);
ut_asserteq(1, interface);
ut_asserteq(2, rate);
ut_asserteq(3, mclk_freq);
ut_asserteq(4, bits_per_sample);
ut_asserteq(5, channels);
return 0;
}
DM_TEST(dm_test_audio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);