blob: 9931a18c962e1aa21ac873bf8f2fa8af3f0f9e42 [file] [log] [blame]
Cliff Cai88740da2008-09-05 18:21:38 +08001/*
2 * File: sound/soc/blackfin/bf5xx-i2s-pcm.c
3 * Author: Cliff Cai <Cliff.Cai@analog.com>
4 *
5 * Created: Tue June 06 2008
6 * Description: DMA driver for i2s codec
7 *
8 * Modified:
9 * Copyright 2008 Analog Devices Inc.
10 *
11 * Bugs: Enter bugs at http://blackfin.uclinux.org/
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see the file COPYING, or write
25 * to the Free Software Foundation, Inc.,
26 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/platform_device.h>
Cliff Cai88740da2008-09-05 18:21:38 +080032#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/gfp.h>
Cliff Cai88740da2008-09-05 18:21:38 +080034
35#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
39
40#include <asm/dma.h>
41
Cliff Cai88740da2008-09-05 18:21:38 +080042#include "bf5xx-sport.h"
43
44static void bf5xx_dma_irq(void *data)
45{
46 struct snd_pcm_substream *pcm = data;
47 snd_pcm_period_elapsed(pcm);
48}
49
50static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
51 .info = SNDRV_PCM_INFO_INTERLEAVED |
52 SNDRV_PCM_INFO_MMAP |
53 SNDRV_PCM_INFO_MMAP_VALID |
54 SNDRV_PCM_INFO_BLOCK_TRANSFER,
55 .formats = SNDRV_PCM_FMTBIT_S16_LE |
56 SNDRV_PCM_FMTBIT_S24_LE |
57 SNDRV_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};
65
66static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params *params)
68{
Lars-Peter Clausen569ef652013-05-28 19:22:12 +020069 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
Cliff Cai88740da2008-09-05 18:21:38 +080070}
71
72static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
73{
74 snd_pcm_lib_free_pages(substream);
75
76 return 0;
77}
78
79static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
80{
81 struct snd_pcm_runtime *runtime = substream->runtime;
82 struct sport_device *sport = runtime->private_data;
83 int period_bytes = frames_to_bytes(runtime, runtime->period_size);
84
85 pr_debug("%s enter\n", __func__);
86 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
87 sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
88 sport_config_tx_dma(sport, runtime->dma_area,
89 runtime->periods, period_bytes);
90 } else {
91 sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
92 sport_config_rx_dma(sport, runtime->dma_area,
93 runtime->periods, period_bytes);
94 }
95
96 return 0;
97}
98
99static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
100{
101 struct snd_pcm_runtime *runtime = substream->runtime;
102 struct sport_device *sport = runtime->private_data;
103 int ret = 0;
104
105 pr_debug("%s enter\n", __func__);
106 switch (cmd) {
107 case SNDRV_PCM_TRIGGER_START:
108 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
109 sport_tx_start(sport);
110 else
111 sport_rx_start(sport);
112 break;
113 case SNDRV_PCM_TRIGGER_STOP:
114 case SNDRV_PCM_TRIGGER_SUSPEND:
115 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
116 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
117 sport_tx_stop(sport);
118 else
119 sport_rx_stop(sport);
120 break;
121 default:
122 ret = -EINVAL;
123 }
124
125 return ret;
126}
127
128static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
129{
130 struct snd_pcm_runtime *runtime = substream->runtime;
131 struct sport_device *sport = runtime->private_data;
132 unsigned int diff;
133 snd_pcm_uframes_t frames;
134 pr_debug("%s enter\n", __func__);
135 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
136 diff = sport_curr_offset_tx(sport);
Cliff Cai88740da2008-09-05 18:21:38 +0800137 } else {
138 diff = sport_curr_offset_rx(sport);
Cliff Cai88740da2008-09-05 18:21:38 +0800139 }
Mark Browne999dc52011-06-13 12:14:07 +0100140
141 /*
142 * TX at least can report one frame beyond the end of the
143 * buffer if we hit the wraparound case - clamp to within the
144 * buffer as the ALSA APIs require.
145 */
146 if (diff == snd_pcm_lib_buffer_bytes(substream))
147 diff = 0;
148
149 frames = bytes_to_frames(substream->runtime, diff);
150
Cliff Cai88740da2008-09-05 18:21:38 +0800151 return frames;
152}
153
154static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
155{
Barry Song2c66cb92011-03-28 01:45:10 -0400156 struct snd_soc_pcm_runtime *rtd = substream->private_data;
157 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
158 struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
Cliff Cai88740da2008-09-05 18:21:38 +0800159 struct snd_pcm_runtime *runtime = substream->runtime;
Barry Song2c66cb92011-03-28 01:45:10 -0400160 struct snd_dma_buffer *buf = &substream->dma_buffer;
Cliff Cai88740da2008-09-05 18:21:38 +0800161 int ret;
162
163 pr_debug("%s enter\n", __func__);
Barry Song2c66cb92011-03-28 01:45:10 -0400164
Cliff Cai88740da2008-09-05 18:21:38 +0800165 snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
166
Mark Brownb83e60c2011-06-13 17:50:18 +0100167 ret = snd_pcm_hw_constraint_integer(runtime,
Cliff Cai88740da2008-09-05 18:21:38 +0800168 SNDRV_PCM_HW_PARAM_PERIODS);
169 if (ret < 0)
170 goto out;
171
Barry Song2c66cb92011-03-28 01:45:10 -0400172 if (sport_handle != NULL) {
173 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
174 sport_handle->tx_buf = buf->area;
175 else
176 sport_handle->rx_buf = buf->area;
177
Cliff Cai88740da2008-09-05 18:21:38 +0800178 runtime->private_data = sport_handle;
Barry Song2c66cb92011-03-28 01:45:10 -0400179 } else {
Cliff Cai88740da2008-09-05 18:21:38 +0800180 pr_err("sport_handle is NULL\n");
181 return -1;
182 }
183 return 0;
184
185 out:
186 return ret;
187}
188
189static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
190 struct vm_area_struct *vma)
191{
192 struct snd_pcm_runtime *runtime = substream->runtime;
193 size_t size = vma->vm_end - vma->vm_start;
194 vma->vm_start = (unsigned long)runtime->dma_area;
195 vma->vm_end = vma->vm_start + size;
196 vma->vm_flags |= VM_SHARED;
197
198 return 0 ;
199}
200
Mark Brownb2a19d02009-01-17 19:14:26 +0000201static struct snd_pcm_ops bf5xx_pcm_i2s_ops = {
Cliff Cai88740da2008-09-05 18:21:38 +0800202 .open = bf5xx_pcm_open,
203 .ioctl = snd_pcm_lib_ioctl,
204 .hw_params = bf5xx_pcm_hw_params,
205 .hw_free = bf5xx_pcm_hw_free,
206 .prepare = bf5xx_pcm_prepare,
207 .trigger = bf5xx_pcm_trigger,
208 .pointer = bf5xx_pcm_pointer,
209 .mmap = bf5xx_pcm_mmap,
210};
211
Yang Hongyang284901a2009-04-06 19:01:15 -0700212static u64 bf5xx_pcm_dmamask = DMA_BIT_MASK(32);
Cliff Cai88740da2008-09-05 18:21:38 +0800213
Axel Lin3b70e3b2011-09-21 14:40:58 +0800214static int bf5xx_pcm_i2s_new(struct snd_soc_pcm_runtime *rtd)
Cliff Cai88740da2008-09-05 18:21:38 +0800215{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100216 struct snd_card *card = rtd->card->snd_card;
Lars-Peter Clausena3935a22013-05-28 19:22:13 +0200217 size_t size = bf5xx_pcm_hardware.buffer_bytes_max;
Cliff Cai88740da2008-09-05 18:21:38 +0800218
219 pr_debug("%s enter\n", __func__);
220 if (!card->dev->dma_mask)
221 card->dev->dma_mask = &bf5xx_pcm_dmamask;
222 if (!card->dev->coherent_dma_mask)
Yang Hongyang284901a2009-04-06 19:01:15 -0700223 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
Cliff Cai88740da2008-09-05 18:21:38 +0800224
Lars-Peter Clausena3935a22013-05-28 19:22:13 +0200225 return snd_pcm_lib_preallocate_pages_for_all(rtd->pcm,
226 SNDRV_DMA_TYPE_DEV, card->dev, size, size);
Cliff Cai88740da2008-09-05 18:21:38 +0800227}
228
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000229static struct snd_soc_platform_driver bf5xx_i2s_soc_platform = {
230 .ops = &bf5xx_pcm_i2s_ops,
Cliff Cai88740da2008-09-05 18:21:38 +0800231 .pcm_new = bf5xx_pcm_i2s_new,
Cliff Cai88740da2008-09-05 18:21:38 +0800232};
Cliff Cai88740da2008-09-05 18:21:38 +0800233
Bill Pembertondca66da2012-12-07 09:26:13 -0500234static int bfin_i2s_soc_platform_probe(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000235{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000236 return snd_soc_register_platform(&pdev->dev, &bf5xx_i2s_soc_platform);
Mark Brown958e7922008-12-03 19:58:17 +0000237}
Mark Brown958e7922008-12-03 19:58:17 +0000238
Bill Pembertondca66da2012-12-07 09:26:13 -0500239static int bfin_i2s_soc_platform_remove(struct platform_device *pdev)
Mark Brown958e7922008-12-03 19:58:17 +0000240{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000241 snd_soc_unregister_platform(&pdev->dev);
242 return 0;
Mark Brown958e7922008-12-03 19:58:17 +0000243}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000244
245static struct platform_driver bfin_i2s_pcm_driver = {
246 .driver = {
Mark Brownb83e60c2011-06-13 17:50:18 +0100247 .name = "bfin-i2s-pcm-audio",
248 .owner = THIS_MODULE,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000249 },
250
251 .probe = bfin_i2s_soc_platform_probe,
Bill Pembertondca66da2012-12-07 09:26:13 -0500252 .remove = bfin_i2s_soc_platform_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000253};
254
Axel Linfb802972011-11-24 14:44:52 +0800255module_platform_driver(bfin_i2s_pcm_driver);
Mark Brown958e7922008-12-03 19:58:17 +0000256
Cliff Cai88740da2008-09-05 18:21:38 +0800257MODULE_AUTHOR("Cliff Cai");
258MODULE_DESCRIPTION("ADI Blackfin I2S PCM DMA module");
259MODULE_LICENSE("GPL");