diff mbox

greybus: gb-audio: First pass at integrated greybus audio driver

Message ID 1426205450-22521-1-git-send-email-john.stultz@linaro.org
State New
Headers show

Commit Message

John Stultz March 13, 2015, 12:10 a.m. UTC
So this is my first attempt at merging my dummy driver
into the greybus driver code. Its not yet very interesting.

This patch requires the kernel config include the
SND_GB_AUDIO patch I also submitted since it requires
the simple card infrastructure and codec support.

Since folks may not test this on a tegra, I added spdif
codec support, which is baiscally an empty codec. We can
switch back to the rt56xx codecs when we start talking
to actual modules.

I've not yet really gotten it tied into the greybus
infrastructure as tightly as I'd like, but that's mostly
due to the difficulty of testing any of this w/o hardware,
so I'm currently working on getting gbsim up and running
to see if I can get the basic probing and connection
initialzation done.

This patch was generated on top of the queue MarkG sent
me, and while I don't think it depends on anything he
added, it may not apply cleanly.

Signed-off-by: John Stultz <john.stultz@linaro.org>
---
 Makefile |   2 +
 audio.c  | 365 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 367 insertions(+)
 create mode 100644 audio.c
diff mbox

Patch

diff --git a/Makefile b/Makefile
index a22ad69..77b1d88 100644
--- a/Makefile
+++ b/Makefile
@@ -23,6 +23,7 @@  gb-vibrator-y := vibrator.o
 gb-battery-y := battery.o
 gb-es1-y := es1.o
 gb-es2-y := es2.o
+gb-audio-y := audio.o
 
 obj-m += greybus.o
 obj-m += gb-phy.o
@@ -30,6 +31,7 @@  obj-m += gb-vibrator.o
 obj-m += gb-battery.o
 obj-m += gb-es1.o
 obj-m += gb-es2.o
+obj-m += gb-audio.o
 
 KERNELVER		?= $(shell uname -r)
 KERNELDIR 		?= /lib/modules/$(KERNELVER)/build
diff --git a/audio.c b/audio.c
new file mode 100644
index 0000000..b6579fb
--- /dev/null
+++ b/audio.c
@@ -0,0 +1,365 @@ 
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <sound/core.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <sound/dmaengine_pcm.h>
+#include <sound/simple_card.h>
+#include "greybus.h"
+
+#define GB_AUDIO_DRIVER_NAME			"gb_audio"
+
+#define GB_RATES				SNDRV_PCM_RATE_8000_48000
+#define GB_FMTS					SNDRV_PCM_FMTBIT_S16_LE
+#define GB_MAX_LENGTH				256L
+#define PREALLOC_BUFFER				(32 * 1024)
+#define PREALLOC_BUFFER_MAX			(32 * 1024)
+
+/* Switch between dummy spdif and jetson rt5639 codec */
+#define USE_RT5639 0
+
+
+/* XXX remove this once MarkG's code lands */
+#ifndef GREYBUS_PROTOCOL_I2S_RECEIVER
+#define GREYBUS_PROTOCOL_I2S_RECEIVER 0x11
+#endif
+
+/***************************************************************
+ * This is the gb_pcm_timer logic which fakes DMA interrupts
+ * via a timer.
+ ***************************************************************/
+
+struct gb_pcm_timer {
+	struct snd_pcm_substream	*substream;
+	struct gb_connection		*connection;
+	struct timer_list		timer;
+	int				hwptr_done;
+};
+
+static void dummy_timer_start(struct gb_pcm_timer *pcmt)
+{
+	pcmt->timer.expires = jiffies + HZ/8;
+	add_timer(&pcmt->timer);
+}
+
+static void dummy_timer_stop(struct gb_pcm_timer *pcmt)
+{
+	del_timer(&pcmt->timer);
+	pcmt->timer.expires = 0;
+}
+
+static void dummy_timer_function(unsigned long data)
+{
+	struct gb_pcm_timer *pcmt = (struct gb_pcm_timer *)data;
+	struct snd_pcm_substream *substream = pcmt->substream;
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	char* address;
+	long len;
+	int ret;
+
+	address = runtime->dma_area + pcmt->hwptr_done;
+	len = frames_to_bytes(runtime, runtime->buffer_size) - pcmt->hwptr_done;
+	len = min(len, GB_MAX_LENGTH);
+
+#if 0
+	ret = gb_operation_sync(pcmt->connection, GB_I2S_DATA_TYPE_SEND_DATA,
+							address, len, NULL, 0);
+#endif
+	pcmt->hwptr_done += len;
+
+	dummy_timer_start(pcmt);
+	snd_pcm_period_elapsed(substream);
+}
+
+
+/******************************************
+ * dai op functions)
+ ******************************************/
+
+static int gb_dai_startup(struct snd_pcm_substream *substream,
+				struct snd_soc_dai *dai)
+{
+	return 0;
+}
+
+static void gb_dai_shutdown(struct snd_pcm_substream *substream,
+				struct snd_soc_dai *dai)
+{
+}
+
+static int gb_dai_trigger(struct snd_pcm_substream *substream, int cmd,
+				struct snd_soc_dai *dai)
+{
+	struct gb_pcm_timer *pcmt = substream->runtime->private_data;
+
+	switch (cmd) {
+	case SNDRV_PCM_TRIGGER_START:
+		dummy_timer_start(pcmt);
+		break;
+	case SNDRV_PCM_TRIGGER_STOP:
+		dummy_timer_stop(pcmt);
+		break;
+	default:
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int gb_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
+{
+	return 0;
+}
+
+static int gb_dai_hw_params(struct snd_pcm_substream *substream,
+				struct snd_pcm_hw_params *params,
+				struct snd_soc_dai *dai)
+{
+	return 0;
+}
+
+
+static const struct snd_soc_dai_ops gb_dai_ops = {
+	.startup        = gb_dai_startup,
+	.shutdown       = gb_dai_shutdown,
+	.trigger        = gb_dai_trigger,
+	.set_fmt        = gb_dai_set_fmt,
+	.hw_params      = gb_dai_hw_params,
+};
+
+static struct snd_soc_dai_driver gb_cpu_dai = {
+	/*XXX WTF, this doesn't seem to be the name of the dai that gets registered! */
+	.name                   = "gb-cpu-dai",
+	.playback = {
+		.rates          = GB_RATES,
+		.formats        = GB_FMTS,
+		.channels_min   = 2,
+		.channels_max   = 2,
+	},
+	.ops = &gb_dai_ops,
+};
+
+/******************************************************
+ * gb pcm logic
+ *****************************************************/
+static struct snd_pcm_hardware gb_plat_pcm_hardware = {
+	.info =         SNDRV_PCM_INFO_INTERLEAVED,
+	.formats                = GB_FMTS,
+	.rates                  = GB_RATES,
+	.rate_min               = 8000,
+	.rate_max               = 48000,
+	.channels_min           = 2,
+	.channels_max           = 2,
+	/* XXX - All the values below are junk */
+	.buffer_bytes_max       = 64 * 1024,
+	.period_bytes_min       = 32,
+	.period_bytes_max       = 8192,
+	.periods_min            = 1,
+	.periods_max            = 32,
+	.fifo_size              = 256,
+};
+
+static snd_pcm_uframes_t gb_pcm_pointer(struct snd_pcm_substream *substream)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct gb_pcm_timer *pcmt = substream->runtime->private_data;
+	int hwptr_done;
+
+	hwptr_done = pcmt->hwptr_done;
+
+	return hwptr_done;
+}
+
+static int gb_pcm_prepare(struct snd_pcm_substream *substream)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct gb_pcm_timer *pcmt = substream->runtime->private_data;
+
+	pcmt->hwptr_done = 0;
+
+	return 0;
+}
+
+static int gb_pcm_open(struct snd_pcm_substream *substream)
+{
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct gb_pcm_timer *pcmt;
+
+	pcmt = kzalloc(sizeof(*pcmt), GFP_KERNEL);
+	if (!pcmt)
+		return -ENOMEM;
+	pcmt->substream = substream;
+	setup_timer(&pcmt->timer, dummy_timer_function, (unsigned long)pcmt);
+	runtime->private_data = pcmt;
+
+	snd_soc_set_runtime_hwparams(substream, &gb_plat_pcm_hardware);
+	return snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
+}
+
+static int gb_pcm_close(struct snd_pcm_substream *substream)
+{
+	if (substream->runtime->private_data)
+		kfree(substream->runtime->private_data);
+	substream->runtime->private_data = NULL;
+	return 0;
+}
+
+static int gb_pcm_hw_params(struct snd_pcm_substream *substream,
+				struct snd_pcm_hw_params *hw_params)
+{
+	return snd_pcm_lib_malloc_pages(substream,
+					params_buffer_bytes(hw_params));
+}
+
+static int gb_pcm_hw_free(struct snd_pcm_substream *substream)
+{
+	return snd_pcm_lib_free_pages(substream);
+}
+
+static struct snd_pcm_ops gb_pcm_ops = {
+	.open           = gb_pcm_open,
+	.close		= gb_pcm_close,
+	.ioctl          = snd_pcm_lib_ioctl,
+	.hw_params      = gb_pcm_hw_params,
+	.hw_free        = gb_pcm_hw_free,
+	.prepare	= gb_pcm_prepare,
+	.pointer        = gb_pcm_pointer,
+};
+
+static void gb_pcm_free(struct snd_pcm *pcm)
+{
+	snd_pcm_lib_preallocate_free_for_all(pcm);
+}
+
+static int gb_pcm_new(struct snd_soc_pcm_runtime *rtd)
+{
+	struct snd_pcm *pcm = rtd->pcm;
+
+	return snd_pcm_lib_preallocate_pages_for_all(
+			pcm,
+			SNDRV_DMA_TYPE_CONTINUOUS,
+			snd_dma_continuous_data(GFP_KERNEL),
+			PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
+}
+
+static struct snd_soc_platform_driver gb_soc_platform = {
+	.ops            = &gb_pcm_ops,
+	.pcm_new        = gb_pcm_new,
+	.pcm_free       = gb_pcm_free,
+};
+
+
+/************************************************************
+ * This is the aosc simple card junk which binds the platform
+ * codec, cpu and codec-dais etc togheter, also all the
+ * nested gross platfrom driver/device junk is here.
+ ************************************************************/
+static struct asoc_simple_card_info gb_card_info = {
+	.name           = "Greybus Audio Module",
+	.card           = "gb-card",
+#if USE_RT5639
+	.codec          = "rt5639.0-001c", /* XXX this will need to be dynamic*/
+	.daifmt         = GB_FMTS,
+#else
+	.codec          = "spdif-dit", /* XXX this will need to be dynamic*/
+#endif
+	.platform       = "gb-pcm-audio.0",
+	.cpu_dai = {
+		.name   = "gb-pcm-audio.0",
+		.fmt    = GB_FMTS,
+	},
+	.codec_dai = {
+#if USE_RT5639
+		.name   = "rt5639-aif1",
+		.fmt    = SND_SOC_DAIFMT_CBM_CFM,
+		.sysclk = 11289600,
+#else
+		.name   = "dit-hifi",
+#endif
+	}
+};
+
+static struct platform_device gb_snd_plat_device = {
+	.name   = "asoc-simple-card",
+	.dev    = {
+		.platform_data  = &gb_card_info,
+	},
+};
+
+static struct platform_device gb_device = {
+	.name           = "gb-pcm-audio",
+};
+
+static struct platform_device *gb_devices[] = {
+	&gb_snd_plat_device,
+	&gb_device,
+};
+
+static const struct snd_soc_component_driver gb_soc_component = {
+	.name           = "gb-component",
+};
+
+static int gb_plat_probe(struct platform_device *pdev)
+{
+	int ret;
+
+	ret = snd_soc_register_platform(&pdev->dev, &gb_soc_platform);
+	ret = snd_soc_register_component(&pdev->dev, &gb_soc_component,
+							&gb_cpu_dai, 1);
+	return ret;
+}
+
+static struct platform_driver gb_plat_driver = {
+	.driver         = {
+		.name   = "gb-pcm-audio",
+	},
+	.probe          = gb_plat_probe,
+};
+
+
+
+/****************************************************************
+ * GB hooks
+ ****************************************************************/
+static int gb_i2s_transmitter_connection_init(struct gb_connection *connection)
+{
+	printk("JDB: gb_i2s_transmitter_connection_init!\n");
+	return 0;
+}
+static void gb_i2s_transmitter_connection_exit(struct gb_connection *connection)
+{
+
+}
+
+static struct gb_protocol gb_audio_protocol = {
+	.name			= GB_AUDIO_DRIVER_NAME,
+	.id			= GREYBUS_PROTOCOL_I2S_RECEIVER,
+	.major			= 0,
+	.minor			= 1,
+	.connection_init	= gb_i2s_transmitter_connection_init,
+	.connection_exit	= gb_i2s_transmitter_connection_exit,
+	.request_recv		= NULL,
+};
+
+
+/******************************************************************
+ * This is the basic hook to let me get things initialized
+ ******************************************************************/
+static int __init devices_setup(void)
+{
+	int err;
+	struct platform_device *device;
+
+	gb_protocol_register(&gb_audio_protocol);
+
+	err = platform_driver_register(&gb_plat_driver);
+
+	device = platform_device_register_simple("spdif-dit", -1, NULL, 0);
+	platform_add_devices(gb_devices, ARRAY_SIZE(gb_devices));
+
+	return err;
+}
+device_initcall(devices_setup);
+MODULE_LICENSE("GPL");