Path: blob/master/sound/soc/blackfin/bf5xx-i2s-pcm.c
10817 views
/*1* File: sound/soc/blackfin/bf5xx-i2s-pcm.c2* Author: Cliff Cai <[email protected]>3*4* Created: Tue June 06 20085* Description: DMA driver for i2s codec6*7* Modified:8* Copyright 2008 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-i2s-pcm.h"42#include "bf5xx-sport.h"4344static void bf5xx_dma_irq(void *data)45{46struct snd_pcm_substream *pcm = data;47snd_pcm_period_elapsed(pcm);48}4950static const struct snd_pcm_hardware bf5xx_pcm_hardware = {51.info = SNDRV_PCM_INFO_INTERLEAVED |52SNDRV_PCM_INFO_MMAP |53SNDRV_PCM_INFO_MMAP_VALID |54SNDRV_PCM_INFO_BLOCK_TRANSFER,55.formats = SNDRV_PCM_FMTBIT_S16_LE |56SNDRV_PCM_FMTBIT_S24_LE |57SNDRV_PCM_FMTBIT_S32_LE,58.period_bytes_min = 32,59.period_bytes_max = 0x10000,60.periods_min = 1,61.periods_max = PAGE_SIZE/32,62.buffer_bytes_max = 0x20000, /* 128 kbytes */63.fifo_size = 16,64};6566static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,67struct snd_pcm_hw_params *params)68{69size_t size = bf5xx_pcm_hardware.buffer_bytes_max;70snd_pcm_lib_malloc_pages(substream, size);7172return 0;73}7475static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)76{77snd_pcm_lib_free_pages(substream);7879return 0;80}8182static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)83{84struct snd_pcm_runtime *runtime = substream->runtime;85struct sport_device *sport = runtime->private_data;86int period_bytes = frames_to_bytes(runtime, runtime->period_size);8788pr_debug("%s enter\n", __func__);89if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {90sport_set_tx_callback(sport, bf5xx_dma_irq, substream);91sport_config_tx_dma(sport, runtime->dma_area,92runtime->periods, period_bytes);93} else {94sport_set_rx_callback(sport, bf5xx_dma_irq, substream);95sport_config_rx_dma(sport, runtime->dma_area,96runtime->periods, period_bytes);97}9899return 0;100}101102static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)103{104struct snd_pcm_runtime *runtime = substream->runtime;105struct sport_device *sport = runtime->private_data;106int ret = 0;107108pr_debug("%s enter\n", __func__);109switch (cmd) {110case SNDRV_PCM_TRIGGER_START:111if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)112sport_tx_start(sport);113else114sport_rx_start(sport);115break;116case SNDRV_PCM_TRIGGER_STOP:117case SNDRV_PCM_TRIGGER_SUSPEND:118case SNDRV_PCM_TRIGGER_PAUSE_PUSH:119if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)120sport_tx_stop(sport);121else122sport_rx_stop(sport);123break;124default:125ret = -EINVAL;126}127128return ret;129}130131static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)132{133struct snd_pcm_runtime *runtime = substream->runtime;134struct sport_device *sport = runtime->private_data;135unsigned int diff;136snd_pcm_uframes_t frames;137pr_debug("%s enter\n", __func__);138if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {139diff = sport_curr_offset_tx(sport);140} else {141diff = sport_curr_offset_rx(sport);142}143144/*145* TX at least can report one frame beyond the end of the146* buffer if we hit the wraparound case - clamp to within the147* buffer as the ALSA APIs require.148*/149if (diff == snd_pcm_lib_buffer_bytes(substream))150diff = 0;151152frames = bytes_to_frames(substream->runtime, diff);153154return frames;155}156157static int bf5xx_pcm_open(struct snd_pcm_substream *substream)158{159struct snd_soc_pcm_runtime *rtd = substream->private_data;160struct snd_soc_dai *cpu_dai = rtd->cpu_dai;161struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);162struct snd_pcm_runtime *runtime = substream->runtime;163struct snd_dma_buffer *buf = &substream->dma_buffer;164int ret;165166pr_debug("%s enter\n", __func__);167168snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);169170ret = snd_pcm_hw_constraint_integer(runtime, \171SNDRV_PCM_HW_PARAM_PERIODS);172if (ret < 0)173goto out;174175if (sport_handle != NULL) {176if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)177sport_handle->tx_buf = buf->area;178else179sport_handle->rx_buf = buf->area;180181runtime->private_data = sport_handle;182} else {183pr_err("sport_handle is NULL\n");184return -1;185}186return 0;187188out:189return ret;190}191192static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,193struct vm_area_struct *vma)194{195struct snd_pcm_runtime *runtime = substream->runtime;196size_t size = vma->vm_end - vma->vm_start;197vma->vm_start = (unsigned long)runtime->dma_area;198vma->vm_end = vma->vm_start + size;199vma->vm_flags |= VM_SHARED;200201return 0 ;202}203204static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {205.open = bf5xx_pcm_open,206.ioctl = snd_pcm_lib_ioctl,207.hw_params = bf5xx_pcm_hw_params,208.hw_free = bf5xx_pcm_hw_free,209.prepare = bf5xx_pcm_prepare,210.trigger = bf5xx_pcm_trigger,211.pointer = bf5xx_pcm_pointer,212.mmap = bf5xx_pcm_mmap,213};214215static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)216{217struct snd_pcm_substream *substream = pcm->streams[stream].substream;218struct snd_dma_buffer *buf = &substream->dma_buffer;219size_t size = bf5xx_pcm_hardware.buffer_bytes_max;220221buf->dev.type = SNDRV_DMA_TYPE_DEV;222buf->dev.dev = pcm->card->dev;223buf->private_data = NULL;224buf->area = dma_alloc_coherent(pcm->card->dev, size,225&buf->addr, GFP_KERNEL);226if (!buf->area) {227pr_err("Failed to allocate dma memory - Please increase uncached DMA memory region\n");228return -ENOMEM;229}230buf->bytes = size;231232pr_debug("%s, area:%p, size:0x%08lx\n", __func__,233buf->area, buf->bytes);234235return 0;236}237238static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)239{240struct snd_pcm_substream *substream;241struct snd_dma_buffer *buf;242int stream;243244for (stream = 0; stream < 2; stream++) {245substream = pcm->streams[stream].substream;246if (!substream)247continue;248249buf = &substream->dma_buffer;250if (!buf->area)251continue;252dma_free_coherent(NULL, buf->bytes, buf->area, 0);253buf->area = NULL;254}255}256257static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);258259int bf5xx_pcm_i2s_new(struct snd_card *card, struct snd_soc_dai *dai,260struct snd_pcm *pcm)261{262int ret = 0;263264pr_debug("%s enter\n", __func__);265if (!card->dev->dma_mask)266card->dev->dma_mask = &bf5xx_pcm_dmamask;267if (!card->dev->coherent_dma_mask)268card->dev->coherent_dma_mask = DMA_BIT_MASK(32);269270if (dai->driver->playback.channels_min) {271ret = bf5xx_pcm_preallocate_dma_buffer(pcm,272SNDRV_PCM_STREAM_PLAYBACK);273if (ret)274goto out;275}276277if (dai->driver->capture.channels_min) {278ret = bf5xx_pcm_preallocate_dma_buffer(pcm,279SNDRV_PCM_STREAM_CAPTURE);280if (ret)281goto out;282}283out:284return ret;285}286287static struct snd_soc_platform_driver bf5xx_i2s_soc_platform = {288.ops = &bf5xx_pcm_i2s_ops,289.pcm_new = bf5xx_pcm_i2s_new,290.pcm_free = bf5xx_pcm_free_dma_buffers,291};292293static int __devinit bfin_i2s_soc_platform_probe(struct platform_device *pdev)294{295return snd_soc_register_platform(&pdev->dev, &bf5xx_i2s_soc_platform);296}297298static int __devexit bfin_i2s_soc_platform_remove(struct platform_device *pdev)299{300snd_soc_unregister_platform(&pdev->dev);301return 0;302}303304static struct platform_driver bfin_i2s_pcm_driver = {305.driver = {306.name = "bfin-i2s-pcm-audio",307.owner = THIS_MODULE,308},309310.probe = bfin_i2s_soc_platform_probe,311.remove = __devexit_p(bfin_i2s_soc_platform_remove),312};313314static int __init snd_bfin_i2s_pcm_init(void)315{316return platform_driver_register(&bfin_i2s_pcm_driver);317}318module_init(snd_bfin_i2s_pcm_init);319320static void __exit snd_bfin_i2s_pcm_exit(void)321{322platform_driver_unregister(&bfin_i2s_pcm_driver);323}324module_exit(snd_bfin_i2s_pcm_exit);325326MODULE_AUTHOR("Cliff Cai");327MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");328MODULE_LICENSE("GPL");329330331