From patchwork Tue Mar 17 20:22:50 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 45901 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-we0-f197.google.com (mail-we0-f197.google.com [74.125.82.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id D72222153C for ; Tue, 17 Mar 2015 20:22:57 +0000 (UTC) Received: by wesp10 with SMTP id p10sf3795691wes.3 for ; Tue, 17 Mar 2015 13:22:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=q0z5tjpn9rm7veuRXxuxKybvQSFC4U6e81hreDVm5+o=; b=d7AE9A8ZWHJ/oCLzWh53lid3DK8Y2HC3AddruzNDqqHNcXOOP7FTmp559tWEdVnQSi LL9mDIBYTzw4auGW+JIqsWgiOYiqf9fyAlrf8DA2AT//dYbk9rDZW2zjBhU3FlMNZV1g t785RDpZAV1lcPvSH70brROQmNtLpM7cx7DHJR5UrRDpBbZlFZpEkCw0HBcOKbVcFMXd lkVkHLikfuUlzwTSgoWwR0v4EpcOcJ9C7hbDHY9JBKo/WKFVHU66n+teVivanKen0gDV wxN2rLLJyrEGEk5RduxrCTtd3lwMYqyZzUfq+QhX7eJ2d5bDZ8F2GPiIbRBOztvDgctH Fjlg== X-Gm-Message-State: ALoCoQkZ0YHk1eNLAvYCO1y0zRalf64eKqXh2eKtaZDjyKGHkxywNyLjVOrDxigMvR+4v6QSlQub X-Received: by 10.194.201.10 with SMTP id jw10mr10441911wjc.3.1426623777179; Tue, 17 Mar 2015 13:22:57 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.205.4 with SMTP id lc4ls81010lac.104.gmail; Tue, 17 Mar 2015 13:22:57 -0700 (PDT) X-Received: by 10.112.17.98 with SMTP id n2mr28979025lbd.40.1426623777013; Tue, 17 Mar 2015 13:22:57 -0700 (PDT) Received: from mail-la0-f45.google.com (mail-la0-f45.google.com. [209.85.215.45]) by mx.google.com with ESMTPS id ba5si11341072lbc.128.2015.03.17.13.22.56 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Mar 2015 13:22:57 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.45 as permitted sender) client-ip=209.85.215.45; Received: by ladw1 with SMTP id w1so18982765lad.0 for ; Tue, 17 Mar 2015 13:22:56 -0700 (PDT) X-Received: by 10.152.26.201 with SMTP id n9mr60724788lag.29.1426623776882; Tue, 17 Mar 2015 13:22:56 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.112.35.133 with SMTP id h5csp743053lbj; Tue, 17 Mar 2015 13:22:55 -0700 (PDT) X-Received: by 10.66.221.8 with SMTP id qa8mr20177748pac.62.1426623774780; Tue, 17 Mar 2015 13:22:54 -0700 (PDT) Received: from mail-pd0-f176.google.com (mail-pd0-f176.google.com. [209.85.192.176]) by mx.google.com with ESMTPS id fe2si31367896pab.161.2015.03.17.13.22.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Mar 2015 13:22:54 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.192.176 as permitted sender) client-ip=209.85.192.176; Received: by pdbcz9 with SMTP id cz9so19485539pdb.3 for ; Tue, 17 Mar 2015 13:22:53 -0700 (PDT) X-Received: by 10.70.47.193 with SMTP id f1mr136310262pdn.158.1426623773881; Tue, 17 Mar 2015 13:22:53 -0700 (PDT) Received: from localhost.localdomain (c-67-170-153-23.hsd1.or.comcast.net. [67.170.153.23]) by mx.google.com with ESMTPSA id z6sm23817792pdm.78.2015.03.17.13.22.52 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 Mar 2015 13:22:53 -0700 (PDT) From: John Stultz To: projectara-software-dev@linaro.org Cc: John Stultz Subject: [PATCH] greybus: gb-audio: Add integrated greybus audio driver Date: Tue, 17 Mar 2015 13:22:50 -0700 Message-Id: <1426623770-25116-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.45 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , So this is still an early attempt at merging my dummy driver into the greybus driver code. Things are coming along, but its not ready for merging yet. 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 gotten things tied in with the greybus initialization logic, and somewhat tested via gbsim, although there are still bugs with module removal. The patch still doesn't handle audio transmission properly, currently using a fixed rate timer to send chunks of audio. This will need to be tweaked so we're sending the right chunks of audio at the right interval for the given bitrate. 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 w/o. Signed-off-by: John Stultz --- Makefile | 3 +- audio.c | 410 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 412 insertions(+), 1 deletion(-) create mode 100644 audio.c diff --git a/Makefile b/Makefile index 9aebfbe..719e134 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,6 @@ gb-phy-y := gpb.o \ i2c.o \ spi.o \ i2s_mgmt.o \ - i2s_transmitter.o \ usb.o # Prefix all modules with gb- @@ -25,6 +24,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 @@ -32,6 +32,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..ab98f6c --- /dev/null +++ b/audio.c @@ -0,0 +1,410 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 GB_I2S_DATA_TYPE_SEND_DATA +#define GB_I2S_DATA_TYPE_SEND_DATA 0x02 +#endif + +/*************************************************************** + * This is the gb_snd structure which ties everything together + * and fakes DMA interrupts via a timer. + ***************************************************************/ +struct gb_snd { + struct platform_device *card; + struct platform_device *cpu_dai; + struct gb_connection *connection; + + struct snd_pcm_substream *substream; + struct timer_list timer; + int hwptr_done; +}; + +static void dummy_timer_start(struct gb_snd *snd_dev) +{ + snd_dev->timer.expires = jiffies + HZ/8; + add_timer(&snd_dev->timer); +} + +static void dummy_timer_stop(struct gb_snd *snd_dev) +{ + del_timer(&snd_dev->timer); + snd_dev->timer.expires = 0; +} + +static void dummy_timer_function(unsigned long data) +{ + struct gb_snd *snd_dev = (struct gb_snd *)data; + struct snd_pcm_substream *substream = snd_dev->substream; + struct snd_pcm_runtime *runtime = substream->runtime; + char* address; + long len; + int ret; + + address = runtime->dma_area + snd_dev->hwptr_done; + len = frames_to_bytes(runtime, runtime->buffer_size) - snd_dev->hwptr_done; + len = min(len, GB_MAX_LENGTH); + + ret = gb_operation_sync(snd_dev->connection, GB_I2S_DATA_TYPE_SEND_DATA, + address, len, NULL, 0); + snd_dev->hwptr_done += len; + + dummy_timer_start(snd_dev); + 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 snd_soc_pcm_runtime *rtd = substream->private_data; + struct gb_snd *snd_dev; + + + snd_dev = snd_soc_dai_get_drvdata(rtd->cpu_dai); + + switch (cmd) { + case SNDRV_PCM_TRIGGER_START: + dummy_timer_start(snd_dev); + break; + case SNDRV_PCM_TRIGGER_STOP: + dummy_timer_stop(snd_dev); + 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 = { + .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_soc_pcm_runtime *rtd = substream->private_data; + struct gb_snd *snd_dev; + + snd_dev = snd_soc_dai_get_drvdata(rtd->cpu_dai); + + return snd_dev->hwptr_done; +} + +static int gb_pcm_prepare(struct snd_pcm_substream *substream) +{ + struct snd_soc_pcm_runtime *rtd = substream->private_data; + struct gb_snd *snd_dev; + + snd_dev = snd_soc_dai_get_drvdata(rtd->cpu_dai); + snd_dev->hwptr_done = 0; + + return 0; +} + +static int gb_pcm_open(struct snd_pcm_substream *substream) +{ + struct snd_pcm_runtime *runtime = substream->runtime; + struct snd_soc_pcm_runtime *rtd = substream->private_data; + struct gb_snd *snd_dev; + + + snd_dev = snd_soc_dai_get_drvdata(rtd->cpu_dai); + + snd_dev->substream = substream; + setup_timer(&snd_dev->timer, dummy_timer_function, (unsigned long)snd_dev); + runtime->private_data = snd_dev; + + 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 const struct snd_soc_component_driver gb_soc_component = { + .name = "gb-component", +}; + +static int gb_plat_probe(struct platform_device *pdev) +{ + struct gb_snd *snd_dev; + int ret; + + snd_dev = (struct gb_snd *)pdev->dev.platform_data; + dev_set_drvdata(&pdev->dev, snd_dev); + + 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, +}; + + +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 + } +}; + + +int device_count; + +/**************************************************************** + * GB hooks + ****************************************************************/ +static int gb_i2s_transmitter_connection_init(struct gb_connection *connection) +{ + struct gb_snd *snd_dev; + int ret; + + printk("JDB: gb_i2s_transmitter_connection_init!\n"); + + snd_dev = kzalloc(sizeof(*snd_dev), GFP_KERNEL); + if(!snd_dev) + return -ENOMEM; + + snd_dev->cpu_dai = platform_device_alloc("gb-pcm-audio", device_count); + if (!snd_dev->cpu_dai) { + ret = ENOMEM; + goto out; + } + + snd_dev->card = platform_device_alloc("asoc-simple-card", device_count); + if (!snd_dev->card) { + ret = ENOMEM; + goto out_dai; + } + + snd_dev->connection = connection; + + /* XXX there's got to be a better way to handle device counts.... */ + device_count++; + + snd_dev->card->dev.platform_data = &gb_card_info; /*XXX - prob should generate this dynamically*/ + snd_dev->cpu_dai->dev.platform_data = snd_dev; + snd_dev->connection->private = snd_dev; + + ret = platform_device_add(snd_dev->cpu_dai); + if (ret) { + goto out_dai; + } + ret = platform_device_add(snd_dev->card); + if (ret) { + /* XXX errrr.. figure out the right thing here... */ + //platform_device_unregister(snd_dev->cpu_dai); + goto out_card; + } + return 0; + +out_card: + platform_device_put(snd_dev->card); +out_dai: + platform_device_put(snd_dev->cpu_dai); +out: + kfree(snd_dev); + return ret; +} + +static void gb_i2s_transmitter_connection_exit(struct gb_connection *connection) +{ + struct gb_snd *snd_dev; + + snd_dev = (struct gb_snd *)connection->private; + + platform_device_unregister(snd_dev->card); + platform_device_unregister(snd_dev->cpu_dai); + kfree(snd_dev); +} + +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); + + return err; +} +device_initcall(devices_setup); +MODULE_LICENSE("GPL");