Path: blob/master/sound/soc/blackfin/bf5xx-tdm-pcm.c
10817 views
/*1* File: sound/soc/blackfin/bf5xx-tdm-pcm.c2* Author: Barry Song <[email protected]>3*4* Created: Tue June 06 20095* Description: DMA driver for tdm codec6*7* Modified:8* Copyright 2009 Analog Devices Inc.9*10* Bugs: Enter bugs at http://blackfin.uclinux.org/11*12* This program is free software; you can redistribute it and/or modify13* it under the terms of the GNU General Public License as published by14* the Free Software Foundation; either version 2 of the License, or15* (at your option) any later version.16*17* This program is distributed in the hope that it will be useful,18* but WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20* GNU General Public License for more details.21*22* You should have received a copy of the GNU General Public License23* along with this program; if not, see the file COPYING, or write24* to the Free Software Foundation, Inc.,25* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA26*/2728#include <linux/module.h>29#include <linux/init.h>30#include <linux/platform_device.h>31#include <linux/dma-mapping.h>32#include <linux/gfp.h>3334#include <sound/core.h>35#include <sound/pcm.h>36#include <sound/pcm_params.h>37#include <sound/soc.h>3839#include <asm/dma.h>4041#include "bf5xx-tdm-pcm.h"42#include "bf5xx-tdm.h"43#include "bf5xx-sport.h"4445#define PCM_BUFFER_MAX 0x800046#define FRAGMENT_SIZE_MIN (4*1024)47#define FRAGMENTS_MIN 248#define FRAGMENTS_MAX 324950static void bf5xx_dma_irq(void *data)51{52struct snd_pcm_substream *pcm = data;53snd_pcm_period_elapsed(pcm);54}5556static const struct snd_pcm_hardware bf5xx_pcm_hardware = {57.info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |58SNDRV_PCM_INFO_RESUME),59.formats = SNDRV_PCM_FMTBIT_S32_LE,60.rates = SNDRV_PCM_RATE_48000,61.channels_min = 2,62.channels_max = 8,63.buffer_bytes_max = PCM_BUFFER_MAX,64.period_bytes_min = FRAGMENT_SIZE_MIN,65.period_bytes_max = PCM_BUFFER_MAX/2,66.periods_min = FRAGMENTS_MIN,67.periods_max = FRAGMENTS_MAX,68};6970static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,71struct snd_pcm_hw_params *params)72{73size_t size = bf5xx_pcm_hardware.buffer_bytes_max;74snd_pcm_lib_malloc_pages(substream, size * 4);7576return 0;77}7879static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)80{81snd_pcm_lib_free_pages(substream);8283return 0;84}8586static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)87{88struct snd_pcm_runtime *runtime = substream->runtime;89struct sport_device *sport = runtime->private_data;90int fragsize_bytes = frames_to_bytes(runtime, runtime->period_size);9192fragsize_bytes /= runtime->channels;93/* inflate the fragsize to match the dma width of SPORT */94fragsize_bytes *= 8;9596if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {97sport_set_tx_callback(sport, bf5xx_dma_irq, substream);98sport_config_tx_dma(sport, runtime->dma_area,99runtime->periods, fragsize_bytes);100} else {101sport_set_rx_callback(sport, bf5xx_dma_irq, substream);102sport_config_rx_dma(sport, runtime->dma_area,103runtime->periods, fragsize_bytes);104}105106return 0;107}108109static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)110{111struct snd_pcm_runtime *runtime = substream->runtime;112struct sport_device *sport = runtime->private_data;113int ret = 0;114115switch (cmd) {116case SNDRV_PCM_TRIGGER_START:117if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)118sport_tx_start(sport);119else120sport_rx_start(sport);121break;122case SNDRV_PCM_TRIGGER_STOP:123case SNDRV_PCM_TRIGGER_SUSPEND:124case SNDRV_PCM_TRIGGER_PAUSE_PUSH:125if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)126sport_tx_stop(sport);127else128sport_rx_stop(sport);129break;130default:131ret = -EINVAL;132}133134return ret;135}136137static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)138{139struct snd_pcm_runtime *runtime = substream->runtime;140struct sport_device *sport = runtime->private_data;141unsigned int diff;142snd_pcm_uframes_t frames;143144if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {145diff = sport_curr_offset_tx(sport);146frames = diff / (8*4); /* 32 bytes per frame */147} else {148diff = sport_curr_offset_rx(sport);149frames = diff / (8*4);150}151return frames;152}153154static int bf5xx_pcm_open(struct snd_pcm_substream *substream)155{156struct snd_soc_pcm_runtime *rtd = substream->private_data;157struct snd_soc_dai *cpu_dai = rtd->cpu_dai;158struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);159struct snd_pcm_runtime *runtime = substream->runtime;160struct snd_dma_buffer *buf = &substream->dma_buffer;161162int ret = 0;163164snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);165166ret = snd_pcm_hw_constraint_integer(runtime,167SNDRV_PCM_HW_PARAM_PERIODS);168if (ret < 0)169goto out;170171if (sport_handle != NULL) {172if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)173sport_handle->tx_buf = buf->area;174else175sport_handle->rx_buf = buf->area;176177runtime->private_data = sport_handle;178} else {179pr_err("sport_handle is NULL\n");180ret = -ENODEV;181}182out:183return ret;184}185186static int bf5xx_pcm_copy(struct snd_pcm_substream *substream, int channel,187snd_pcm_uframes_t pos, void *buf, snd_pcm_uframes_t count)188{189struct snd_pcm_runtime *runtime = substream->runtime;190struct sport_device *sport = runtime->private_data;191struct bf5xx_tdm_port *tdm_port = sport->private_data;192unsigned int *src;193unsigned int *dst;194int i;195196if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {197src = buf;198dst = (unsigned int *)substream->runtime->dma_area;199200dst += pos * 8;201while (count--) {202for (i = 0; i < substream->runtime->channels; i++)203*(dst + tdm_port->tx_map[i]) = *src++;204dst += 8;205}206} else {207src = (unsigned int *)substream->runtime->dma_area;208dst = buf;209210src += pos * 8;211while (count--) {212for (i = 0; i < substream->runtime->channels; i++)213*dst++ = *(src + tdm_port->rx_map[i]);214src += 8;215}216}217218return 0;219}220221static int bf5xx_pcm_silence(struct snd_pcm_substream *substream,222int channel, snd_pcm_uframes_t pos, snd_pcm_uframes_t count)223{224unsigned char *buf = substream->runtime->dma_area;225buf += pos * 8 * 4;226memset(buf, '\0', count * 8 * 4);227228return 0;229}230231232struct snd_pcm_ops bf5xx_pcm_tdm_ops = {233.open = bf5xx_pcm_open,234.ioctl = snd_pcm_lib_ioctl,235.hw_params = bf5xx_pcm_hw_params,236.hw_free = bf5xx_pcm_hw_free,237.prepare = bf5xx_pcm_prepare,238.trigger = bf5xx_pcm_trigger,239.pointer = bf5xx_pcm_pointer,240.copy = bf5xx_pcm_copy,241.silence = bf5xx_pcm_silence,242};243244static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)245{246struct snd_pcm_substream *substream = pcm->streams[stream].substream;247struct snd_dma_buffer *buf = &substream->dma_buffer;248size_t size = bf5xx_pcm_hardware.buffer_bytes_max;249250buf->dev.type = SNDRV_DMA_TYPE_DEV;251buf->dev.dev = pcm->card->dev;252buf->private_data = NULL;253buf->area = dma_alloc_coherent(pcm->card->dev, size * 4,254&buf->addr, GFP_KERNEL);255if (!buf->area) {256pr_err("Failed to allocate dma memory - Please increase uncached DMA memory region\n");257return -ENOMEM;258}259buf->bytes = size;260261return 0;262}263264static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)265{266struct snd_pcm_substream *substream;267struct snd_dma_buffer *buf;268int stream;269270for (stream = 0; stream < 2; stream++) {271substream = pcm->streams[stream].substream;272if (!substream)273continue;274275buf = &substream->dma_buffer;276if (!buf->area)277continue;278dma_free_coherent(NULL, buf->bytes, buf->area, 0);279buf->area = NULL;280}281}282283static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);284285static int bf5xx_pcm_tdm_new(struct snd_card *card, struct snd_soc_dai *dai,286struct snd_pcm *pcm)287{288int ret = 0;289290if (!card->dev->dma_mask)291card->dev->dma_mask = &bf5xx_pcm_dmamask;292if (!card->dev->coherent_dma_mask)293card->dev->coherent_dma_mask = DMA_BIT_MASK(32);294295if (dai->driver->playback.channels_min) {296ret = bf5xx_pcm_preallocate_dma_buffer(pcm,297SNDRV_PCM_STREAM_PLAYBACK);298if (ret)299goto out;300}301302if (dai->driver->capture.channels_min) {303ret = bf5xx_pcm_preallocate_dma_buffer(pcm,304SNDRV_PCM_STREAM_CAPTURE);305if (ret)306goto out;307}308out:309return ret;310}311312static struct snd_soc_platform_driver bf5xx_tdm_soc_platform = {313.ops = &bf5xx_pcm_tdm_ops,314.pcm_new = bf5xx_pcm_tdm_new,315.pcm_free = bf5xx_pcm_free_dma_buffers,316};317318static int __devinit bf5xx_soc_platform_probe(struct platform_device *pdev)319{320return snd_soc_register_platform(&pdev->dev, &bf5xx_tdm_soc_platform);321}322323static int __devexit bf5xx_soc_platform_remove(struct platform_device *pdev)324{325snd_soc_unregister_platform(&pdev->dev);326return 0;327}328329static struct platform_driver bfin_tdm_driver = {330.driver = {331.name = "bfin-tdm-pcm-audio",332.owner = THIS_MODULE,333},334335.probe = bf5xx_soc_platform_probe,336.remove = __devexit_p(bf5xx_soc_platform_remove),337};338339static int __init snd_bfin_tdm_init(void)340{341return platform_driver_register(&bfin_tdm_driver);342}343module_init(snd_bfin_tdm_init);344345static void __exit snd_bfin_tdm_exit(void)346{347platform_driver_unregister(&bfin_tdm_driver);348}349module_exit(snd_bfin_tdm_exit);350351MODULE_AUTHOR("Barry Song");352MODULE_DESCRIPTION("ADI Blackfin TDM PCM DMA module");353MODULE_LICENSE("GPL");354355356