xref: /openbmc/linux/sound/soc/pxa/pxa2xx-pcm.c (revision c9bd5e69)
1f11a96d5SLiam Girdwood /*
2f11a96d5SLiam Girdwood  * linux/sound/arm/pxa2xx-pcm.c -- ALSA PCM interface for the Intel PXA2xx chip
3f11a96d5SLiam Girdwood  *
4f11a96d5SLiam Girdwood  * Author:	Nicolas Pitre
5f11a96d5SLiam Girdwood  * Created:	Nov 30, 2004
6f11a96d5SLiam Girdwood  * Copyright:	(C) 2004 MontaVista Software, Inc.
7f11a96d5SLiam Girdwood  *
8f11a96d5SLiam Girdwood  * This program is free software; you can redistribute it and/or modify
9f11a96d5SLiam Girdwood  * it under the terms of the GNU General Public License version 2 as
10f11a96d5SLiam Girdwood  * published by the Free Software Foundation.
11f11a96d5SLiam Girdwood  */
12f11a96d5SLiam Girdwood 
13f11a96d5SLiam Girdwood #include <linux/dma-mapping.h>
14da155d5bSPaul Gortmaker #include <linux/module.h>
15d65a1458SDaniel Mack #include <linux/dmaengine.h>
16c529ca4aSDaniel Mack #include <linux/of.h>
17f11a96d5SLiam Girdwood 
18f11a96d5SLiam Girdwood #include <sound/core.h>
19f11a96d5SLiam Girdwood #include <sound/soc.h>
20a6d77317SDmitry Baryshkov #include <sound/pxa2xx-lib.h>
21d65a1458SDaniel Mack #include <sound/dmaengine_pcm.h>
22f11a96d5SLiam Girdwood 
23a6d77317SDmitry Baryshkov #include "../../arm/pxa2xx-pcm.h"
24f11a96d5SLiam Girdwood 
25f11a96d5SLiam Girdwood static int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
26f11a96d5SLiam Girdwood 	struct snd_pcm_hw_params *params)
27f11a96d5SLiam Girdwood {
28f11a96d5SLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
29f11a96d5SLiam Girdwood 	struct pxa2xx_runtime_data *prtd = runtime->private_data;
30f11a96d5SLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
31d65a1458SDaniel Mack 	struct snd_dmaengine_dai_dma_data *dma;
32f11a96d5SLiam Girdwood 	int ret;
33f11a96d5SLiam Girdwood 
34f0fba2adSLiam Girdwood 	dma = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
355f712b2bSDaniel Mack 
36a8f5d0a5SAndrew Johnson 	/* return if this is a bufferless transfer e.g.
37a8f5d0a5SAndrew Johnson 	 * codec <--> BT codec or GSM modem -- lg FIXME */
38a8f5d0a5SAndrew Johnson 	if (!dma)
39a8f5d0a5SAndrew Johnson 		return 0;
40a8f5d0a5SAndrew Johnson 
41f11a96d5SLiam Girdwood 	/* this may get called several times by oss emulation
42f11a96d5SLiam Girdwood 	 * with different params */
43f11a96d5SLiam Girdwood 	if (prtd->params == NULL) {
44f11a96d5SLiam Girdwood 		prtd->params = dma;
45d65a1458SDaniel Mack 		ret = pxa_request_dma("name", DMA_PRIO_LOW,
46f11a96d5SLiam Girdwood 			      pxa2xx_pcm_dma_irq, substream);
47f11a96d5SLiam Girdwood 		if (ret < 0)
48f11a96d5SLiam Girdwood 			return ret;
49f11a96d5SLiam Girdwood 		prtd->dma_ch = ret;
50f11a96d5SLiam Girdwood 	} else if (prtd->params != dma) {
51f11a96d5SLiam Girdwood 		pxa_free_dma(prtd->dma_ch);
52f11a96d5SLiam Girdwood 		prtd->params = dma;
53d65a1458SDaniel Mack 		ret = pxa_request_dma("name", DMA_PRIO_LOW,
54f11a96d5SLiam Girdwood 			      pxa2xx_pcm_dma_irq, substream);
55f11a96d5SLiam Girdwood 		if (ret < 0)
56f11a96d5SLiam Girdwood 			return ret;
57f11a96d5SLiam Girdwood 		prtd->dma_ch = ret;
58f11a96d5SLiam Girdwood 	}
59f11a96d5SLiam Girdwood 
60a6d77317SDmitry Baryshkov 	return __pxa2xx_pcm_hw_params(substream, params);
61f11a96d5SLiam Girdwood }
62f11a96d5SLiam Girdwood 
63f11a96d5SLiam Girdwood static int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
64f11a96d5SLiam Girdwood {
65f11a96d5SLiam Girdwood 	struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
66f11a96d5SLiam Girdwood 
67a6d77317SDmitry Baryshkov 	__pxa2xx_pcm_hw_free(substream);
68f11a96d5SLiam Girdwood 
699bef6489SStephen Ware 	if (prtd->dma_ch >= 0) {
70f11a96d5SLiam Girdwood 		pxa_free_dma(prtd->dma_ch);
719bef6489SStephen Ware 		prtd->dma_ch = -1;
7297c96c07SVasily Khoruzhick 		prtd->params = NULL;
73f11a96d5SLiam Girdwood 	}
74f11a96d5SLiam Girdwood 
75f11a96d5SLiam Girdwood 	return 0;
76f11a96d5SLiam Girdwood }
77f11a96d5SLiam Girdwood 
780e734ad5SMark Brown static struct snd_pcm_ops pxa2xx_pcm_ops = {
79a6d77317SDmitry Baryshkov 	.open		= __pxa2xx_pcm_open,
80a6d77317SDmitry Baryshkov 	.close		= __pxa2xx_pcm_close,
81f11a96d5SLiam Girdwood 	.ioctl		= snd_pcm_lib_ioctl,
82f11a96d5SLiam Girdwood 	.hw_params	= pxa2xx_pcm_hw_params,
83f11a96d5SLiam Girdwood 	.hw_free	= pxa2xx_pcm_hw_free,
84a6d77317SDmitry Baryshkov 	.prepare	= __pxa2xx_pcm_prepare,
85f11a96d5SLiam Girdwood 	.trigger	= pxa2xx_pcm_trigger,
86f11a96d5SLiam Girdwood 	.pointer	= pxa2xx_pcm_pointer,
87f11a96d5SLiam Girdwood 	.mmap		= pxa2xx_pcm_mmap,
88f11a96d5SLiam Girdwood };
89f11a96d5SLiam Girdwood 
90552d1ef6SLiam Girdwood static int pxa2xx_soc_pcm_new(struct snd_soc_pcm_runtime *rtd)
91f11a96d5SLiam Girdwood {
92552d1ef6SLiam Girdwood 	struct snd_card *card = rtd->card->snd_card;
93552d1ef6SLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
94c9bd5e69SRussell King 	int ret;
95f11a96d5SLiam Girdwood 
96c9bd5e69SRussell King 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
97c9bd5e69SRussell King 	if (ret)
98c9bd5e69SRussell King 		return ret;
99f11a96d5SLiam Girdwood 
10053dea36cSDaniel Mack 	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
101f11a96d5SLiam Girdwood 		ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
102f11a96d5SLiam Girdwood 			SNDRV_PCM_STREAM_PLAYBACK);
103f11a96d5SLiam Girdwood 		if (ret)
104f11a96d5SLiam Girdwood 			goto out;
105f11a96d5SLiam Girdwood 	}
106f11a96d5SLiam Girdwood 
10753dea36cSDaniel Mack 	if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
108f11a96d5SLiam Girdwood 		ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
109f11a96d5SLiam Girdwood 			SNDRV_PCM_STREAM_CAPTURE);
110f11a96d5SLiam Girdwood 		if (ret)
111f11a96d5SLiam Girdwood 			goto out;
112f11a96d5SLiam Girdwood 	}
113f11a96d5SLiam Girdwood  out:
114f11a96d5SLiam Girdwood 	return ret;
115f11a96d5SLiam Girdwood }
116f11a96d5SLiam Girdwood 
117f0fba2adSLiam Girdwood static struct snd_soc_platform_driver pxa2xx_soc_platform = {
118f0fba2adSLiam Girdwood 	.ops 	= &pxa2xx_pcm_ops,
1198a33de9eSDmitry Baryshkov 	.pcm_new	= pxa2xx_soc_pcm_new,
120f11a96d5SLiam Girdwood 	.pcm_free	= pxa2xx_pcm_free_dma_buffers,
121f11a96d5SLiam Girdwood };
122f11a96d5SLiam Girdwood 
123570f6fe1SBill Pemberton static int pxa2xx_soc_platform_probe(struct platform_device *pdev)
124958e792cSMark Brown {
125f0fba2adSLiam Girdwood 	return snd_soc_register_platform(&pdev->dev, &pxa2xx_soc_platform);
126958e792cSMark Brown }
127958e792cSMark Brown 
128570f6fe1SBill Pemberton static int pxa2xx_soc_platform_remove(struct platform_device *pdev)
129958e792cSMark Brown {
130f0fba2adSLiam Girdwood 	snd_soc_unregister_platform(&pdev->dev);
131f0fba2adSLiam Girdwood 	return 0;
132958e792cSMark Brown }
133f0fba2adSLiam Girdwood 
134c529ca4aSDaniel Mack #ifdef CONFIG_OF
135c529ca4aSDaniel Mack static const struct of_device_id snd_soc_pxa_audio_match[] = {
136c529ca4aSDaniel Mack 	{ .compatible   = "mrvl,pxa-pcm-audio" },
137c529ca4aSDaniel Mack 	{ }
138c529ca4aSDaniel Mack };
139c529ca4aSDaniel Mack #endif
140c529ca4aSDaniel Mack 
141f0fba2adSLiam Girdwood static struct platform_driver pxa_pcm_driver = {
142f0fba2adSLiam Girdwood 	.driver = {
143f0fba2adSLiam Girdwood 		.name = "pxa-pcm-audio",
144f0fba2adSLiam Girdwood 		.owner = THIS_MODULE,
145c529ca4aSDaniel Mack 		.of_match_table = of_match_ptr(snd_soc_pxa_audio_match),
146f0fba2adSLiam Girdwood 	},
147f0fba2adSLiam Girdwood 
148f0fba2adSLiam Girdwood 	.probe = pxa2xx_soc_platform_probe,
149570f6fe1SBill Pemberton 	.remove = pxa2xx_soc_platform_remove,
150f0fba2adSLiam Girdwood };
151f0fba2adSLiam Girdwood 
1522f702a19SAxel Lin module_platform_driver(pxa_pcm_driver);
153958e792cSMark Brown 
154f11a96d5SLiam Girdwood MODULE_AUTHOR("Nicolas Pitre");
155f11a96d5SLiam Girdwood MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
156f11a96d5SLiam Girdwood MODULE_LICENSE("GPL");
157