xref: /openbmc/linux/sound/soc/pxa/pxa2xx-pcm.c (revision 570f6fe1)
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>
15f11a96d5SLiam Girdwood 
16f11a96d5SLiam Girdwood #include <sound/core.h>
17f11a96d5SLiam Girdwood #include <sound/soc.h>
18a6d77317SDmitry Baryshkov #include <sound/pxa2xx-lib.h>
19f11a96d5SLiam Girdwood 
20a6d77317SDmitry Baryshkov #include "../../arm/pxa2xx-pcm.h"
21f11a96d5SLiam Girdwood 
22f11a96d5SLiam Girdwood static int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
23f11a96d5SLiam Girdwood 	struct snd_pcm_hw_params *params)
24f11a96d5SLiam Girdwood {
25f11a96d5SLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
26f11a96d5SLiam Girdwood 	struct pxa2xx_runtime_data *prtd = runtime->private_data;
27f11a96d5SLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
285f712b2bSDaniel Mack 	struct pxa2xx_pcm_dma_params *dma;
29f11a96d5SLiam Girdwood 	int ret;
30f11a96d5SLiam Girdwood 
31f0fba2adSLiam Girdwood 	dma = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
325f712b2bSDaniel Mack 
33a8f5d0a5SAndrew Johnson 	/* return if this is a bufferless transfer e.g.
34a8f5d0a5SAndrew Johnson 	 * codec <--> BT codec or GSM modem -- lg FIXME */
35a8f5d0a5SAndrew Johnson 	if (!dma)
36a8f5d0a5SAndrew Johnson 		return 0;
37a8f5d0a5SAndrew Johnson 
38f11a96d5SLiam Girdwood 	/* this may get called several times by oss emulation
39f11a96d5SLiam Girdwood 	 * with different params */
40f11a96d5SLiam Girdwood 	if (prtd->params == NULL) {
41f11a96d5SLiam Girdwood 		prtd->params = dma;
42f11a96d5SLiam Girdwood 		ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW,
43f11a96d5SLiam Girdwood 			      pxa2xx_pcm_dma_irq, substream);
44f11a96d5SLiam Girdwood 		if (ret < 0)
45f11a96d5SLiam Girdwood 			return ret;
46f11a96d5SLiam Girdwood 		prtd->dma_ch = ret;
47f11a96d5SLiam Girdwood 	} else if (prtd->params != dma) {
48f11a96d5SLiam Girdwood 		pxa_free_dma(prtd->dma_ch);
49f11a96d5SLiam Girdwood 		prtd->params = dma;
50f11a96d5SLiam Girdwood 		ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW,
51f11a96d5SLiam Girdwood 			      pxa2xx_pcm_dma_irq, substream);
52f11a96d5SLiam Girdwood 		if (ret < 0)
53f11a96d5SLiam Girdwood 			return ret;
54f11a96d5SLiam Girdwood 		prtd->dma_ch = ret;
55f11a96d5SLiam Girdwood 	}
56f11a96d5SLiam Girdwood 
57a6d77317SDmitry Baryshkov 	return __pxa2xx_pcm_hw_params(substream, params);
58f11a96d5SLiam Girdwood }
59f11a96d5SLiam Girdwood 
60f11a96d5SLiam Girdwood static int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
61f11a96d5SLiam Girdwood {
62f11a96d5SLiam Girdwood 	struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
63f11a96d5SLiam Girdwood 
64a6d77317SDmitry Baryshkov 	__pxa2xx_pcm_hw_free(substream);
65f11a96d5SLiam Girdwood 
669bef6489SStephen Ware 	if (prtd->dma_ch >= 0) {
67f11a96d5SLiam Girdwood 		pxa_free_dma(prtd->dma_ch);
689bef6489SStephen Ware 		prtd->dma_ch = -1;
6997c96c07SVasily Khoruzhick 		prtd->params = NULL;
70f11a96d5SLiam Girdwood 	}
71f11a96d5SLiam Girdwood 
72f11a96d5SLiam Girdwood 	return 0;
73f11a96d5SLiam Girdwood }
74f11a96d5SLiam Girdwood 
750e734ad5SMark Brown static struct snd_pcm_ops pxa2xx_pcm_ops = {
76a6d77317SDmitry Baryshkov 	.open		= __pxa2xx_pcm_open,
77a6d77317SDmitry Baryshkov 	.close		= __pxa2xx_pcm_close,
78f11a96d5SLiam Girdwood 	.ioctl		= snd_pcm_lib_ioctl,
79f11a96d5SLiam Girdwood 	.hw_params	= pxa2xx_pcm_hw_params,
80f11a96d5SLiam Girdwood 	.hw_free	= pxa2xx_pcm_hw_free,
81a6d77317SDmitry Baryshkov 	.prepare	= __pxa2xx_pcm_prepare,
82f11a96d5SLiam Girdwood 	.trigger	= pxa2xx_pcm_trigger,
83f11a96d5SLiam Girdwood 	.pointer	= pxa2xx_pcm_pointer,
84f11a96d5SLiam Girdwood 	.mmap		= pxa2xx_pcm_mmap,
85f11a96d5SLiam Girdwood };
86f11a96d5SLiam Girdwood 
87284901a9SYang Hongyang static u64 pxa2xx_pcm_dmamask = DMA_BIT_MASK(32);
88f11a96d5SLiam Girdwood 
89552d1ef6SLiam Girdwood static int pxa2xx_soc_pcm_new(struct snd_soc_pcm_runtime *rtd)
90f11a96d5SLiam Girdwood {
91552d1ef6SLiam Girdwood 	struct snd_card *card = rtd->card->snd_card;
92552d1ef6SLiam Girdwood 	struct snd_pcm *pcm = rtd->pcm;
93f11a96d5SLiam Girdwood 	int ret = 0;
94f11a96d5SLiam Girdwood 
95f11a96d5SLiam Girdwood 	if (!card->dev->dma_mask)
96f11a96d5SLiam Girdwood 		card->dev->dma_mask = &pxa2xx_pcm_dmamask;
97f11a96d5SLiam Girdwood 	if (!card->dev->coherent_dma_mask)
98284901a9SYang Hongyang 		card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
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 
134f0fba2adSLiam Girdwood static struct platform_driver pxa_pcm_driver = {
135f0fba2adSLiam Girdwood 	.driver = {
136f0fba2adSLiam Girdwood 			.name = "pxa-pcm-audio",
137f0fba2adSLiam Girdwood 			.owner = THIS_MODULE,
138f0fba2adSLiam Girdwood 	},
139f0fba2adSLiam Girdwood 
140f0fba2adSLiam Girdwood 	.probe = pxa2xx_soc_platform_probe,
141570f6fe1SBill Pemberton 	.remove = pxa2xx_soc_platform_remove,
142f0fba2adSLiam Girdwood };
143f0fba2adSLiam Girdwood 
1442f702a19SAxel Lin module_platform_driver(pxa_pcm_driver);
145958e792cSMark Brown 
146f11a96d5SLiam Girdwood MODULE_AUTHOR("Nicolas Pitre");
147f11a96d5SLiam Girdwood MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
148f11a96d5SLiam Girdwood MODULE_LICENSE("GPL");
149