xref: /linux/sound/soc/pxa/pxa2xx-ac97.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
3  *
4  * Author:	Nicolas Pitre
5  * Created:	Dec 02, 2004
6  * Copyright:	MontaVista Software Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/init.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/dmaengine.h>
18 #include <linux/dma/pxa-dma.h>
19 
20 #include <sound/core.h>
21 #include <sound/ac97_codec.h>
22 #include <sound/soc.h>
23 #include <sound/pxa2xx-lib.h>
24 #include <sound/dmaengine_pcm.h>
25 
26 #include <mach/hardware.h>
27 #include <mach/regs-ac97.h>
28 #include <mach/audio.h>
29 
30 static void pxa2xx_ac97_warm_reset(struct snd_ac97 *ac97)
31 {
32 	pxa2xx_ac97_try_warm_reset(ac97);
33 
34 	pxa2xx_ac97_finish_reset(ac97);
35 }
36 
37 static void pxa2xx_ac97_cold_reset(struct snd_ac97 *ac97)
38 {
39 	pxa2xx_ac97_try_cold_reset(ac97);
40 
41 	pxa2xx_ac97_finish_reset(ac97);
42 }
43 
44 static struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
45 	.read	= pxa2xx_ac97_read,
46 	.write	= pxa2xx_ac97_write,
47 	.warm_reset	= pxa2xx_ac97_warm_reset,
48 	.reset	= pxa2xx_ac97_cold_reset,
49 };
50 
51 static struct pxad_param pxa2xx_ac97_pcm_stereo_in_req = {
52 	.prio = PXAD_PRIO_LOWEST,
53 	.drcmr = 11,
54 };
55 
56 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_stereo_in = {
57 	.addr		= __PREG(PCDR),
58 	.addr_width	= DMA_SLAVE_BUSWIDTH_4_BYTES,
59 	.maxburst	= 32,
60 	.filter_data	= &pxa2xx_ac97_pcm_stereo_in_req,
61 };
62 
63 static struct pxad_param pxa2xx_ac97_pcm_stereo_out_req = {
64 	.prio = PXAD_PRIO_LOWEST,
65 	.drcmr = 12,
66 };
67 
68 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_stereo_out = {
69 	.addr		= __PREG(PCDR),
70 	.addr_width	= DMA_SLAVE_BUSWIDTH_4_BYTES,
71 	.maxburst	= 32,
72 	.filter_data	= &pxa2xx_ac97_pcm_stereo_out_req,
73 };
74 
75 static struct pxad_param pxa2xx_ac97_pcm_aux_mono_out_req = {
76 	.prio = PXAD_PRIO_LOWEST,
77 	.drcmr = 10,
78 };
79 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_aux_mono_out = {
80 	.addr		= __PREG(MODR),
81 	.addr_width	= DMA_SLAVE_BUSWIDTH_2_BYTES,
82 	.maxburst	= 16,
83 	.filter_data	= &pxa2xx_ac97_pcm_aux_mono_out_req,
84 };
85 
86 static struct pxad_param pxa2xx_ac97_pcm_aux_mono_in_req = {
87 	.prio = PXAD_PRIO_LOWEST,
88 	.drcmr = 9,
89 };
90 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_aux_mono_in = {
91 	.addr		= __PREG(MODR),
92 	.addr_width	= DMA_SLAVE_BUSWIDTH_2_BYTES,
93 	.maxburst	= 16,
94 	.filter_data	= &pxa2xx_ac97_pcm_aux_mono_in_req,
95 };
96 
97 static struct pxad_param pxa2xx_ac97_pcm_aux_mic_mono_req = {
98 	.prio = PXAD_PRIO_LOWEST,
99 	.drcmr = 8,
100 };
101 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_mic_mono_in = {
102 	.addr		= __PREG(MCDR),
103 	.addr_width	= DMA_SLAVE_BUSWIDTH_2_BYTES,
104 	.maxburst	= 16,
105 	.filter_data	= &pxa2xx_ac97_pcm_aux_mic_mono_req,
106 };
107 
108 static int pxa2xx_ac97_hifi_startup(struct snd_pcm_substream *substream,
109 				    struct snd_soc_dai *cpu_dai)
110 {
111 	struct snd_dmaengine_dai_dma_data *dma_data;
112 
113 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
114 		dma_data = &pxa2xx_ac97_pcm_stereo_out;
115 	else
116 		dma_data = &pxa2xx_ac97_pcm_stereo_in;
117 
118 	snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
119 
120 	return 0;
121 }
122 
123 static int pxa2xx_ac97_aux_startup(struct snd_pcm_substream *substream,
124 				   struct snd_soc_dai *cpu_dai)
125 {
126 	struct snd_dmaengine_dai_dma_data *dma_data;
127 
128 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
129 		dma_data = &pxa2xx_ac97_pcm_aux_mono_out;
130 	else
131 		dma_data = &pxa2xx_ac97_pcm_aux_mono_in;
132 
133 	snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
134 
135 	return 0;
136 }
137 
138 static int pxa2xx_ac97_mic_startup(struct snd_pcm_substream *substream,
139 				   struct snd_soc_dai *cpu_dai)
140 {
141 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
142 		return -ENODEV;
143 	snd_soc_dai_set_dma_data(cpu_dai, substream,
144 				 &pxa2xx_ac97_pcm_mic_mono_in);
145 
146 	return 0;
147 }
148 
149 #define PXA2XX_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
150 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
151 		SNDRV_PCM_RATE_48000)
152 
153 static const struct snd_soc_dai_ops pxa_ac97_hifi_dai_ops = {
154 	.startup	= pxa2xx_ac97_hifi_startup,
155 };
156 
157 static const struct snd_soc_dai_ops pxa_ac97_aux_dai_ops = {
158 	.startup	= pxa2xx_ac97_aux_startup,
159 };
160 
161 static const struct snd_soc_dai_ops pxa_ac97_mic_dai_ops = {
162 	.startup	= pxa2xx_ac97_mic_startup,
163 };
164 
165 /*
166  * There is only 1 physical AC97 interface for pxa2xx, but it
167  * has extra fifo's that can be used for aux DACs and ADCs.
168  */
169 static struct snd_soc_dai_driver pxa_ac97_dai_driver[] = {
170 {
171 	.name = "pxa2xx-ac97",
172 	.bus_control = true,
173 	.playback = {
174 		.stream_name = "AC97 Playback",
175 		.channels_min = 2,
176 		.channels_max = 2,
177 		.rates = PXA2XX_AC97_RATES,
178 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
179 	.capture = {
180 		.stream_name = "AC97 Capture",
181 		.channels_min = 2,
182 		.channels_max = 2,
183 		.rates = PXA2XX_AC97_RATES,
184 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
185 	.ops = &pxa_ac97_hifi_dai_ops,
186 },
187 {
188 	.name = "pxa2xx-ac97-aux",
189 	.bus_control = true,
190 	.playback = {
191 		.stream_name = "AC97 Aux Playback",
192 		.channels_min = 1,
193 		.channels_max = 1,
194 		.rates = PXA2XX_AC97_RATES,
195 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
196 	.capture = {
197 		.stream_name = "AC97 Aux Capture",
198 		.channels_min = 1,
199 		.channels_max = 1,
200 		.rates = PXA2XX_AC97_RATES,
201 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
202 	.ops = &pxa_ac97_aux_dai_ops,
203 },
204 {
205 	.name = "pxa2xx-ac97-mic",
206 	.bus_control = true,
207 	.capture = {
208 		.stream_name = "AC97 Mic Capture",
209 		.channels_min = 1,
210 		.channels_max = 1,
211 		.rates = PXA2XX_AC97_RATES,
212 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
213 	.ops = &pxa_ac97_mic_dai_ops,
214 },
215 };
216 
217 static const struct snd_soc_component_driver pxa_ac97_component = {
218 	.name		= "pxa-ac97",
219 };
220 
221 static int pxa2xx_ac97_dev_probe(struct platform_device *pdev)
222 {
223 	int ret;
224 
225 	if (pdev->id != -1) {
226 		dev_err(&pdev->dev, "PXA2xx has only one AC97 port.\n");
227 		return -ENXIO;
228 	}
229 
230 	ret = pxa2xx_ac97_hw_probe(pdev);
231 	if (ret) {
232 		dev_err(&pdev->dev, "PXA2xx AC97 hw probe error (%d)\n", ret);
233 		return ret;
234 	}
235 
236 	ret = snd_soc_set_ac97_ops(&pxa2xx_ac97_ops);
237 	if (ret != 0)
238 		return ret;
239 
240 	/* Punt most of the init to the SoC probe; we may need the machine
241 	 * driver to do interesting things with the clocking to get us up
242 	 * and running.
243 	 */
244 	return snd_soc_register_component(&pdev->dev, &pxa_ac97_component,
245 					  pxa_ac97_dai_driver, ARRAY_SIZE(pxa_ac97_dai_driver));
246 }
247 
248 static int pxa2xx_ac97_dev_remove(struct platform_device *pdev)
249 {
250 	snd_soc_unregister_component(&pdev->dev);
251 	snd_soc_set_ac97_ops(NULL);
252 	pxa2xx_ac97_hw_remove(pdev);
253 	return 0;
254 }
255 
256 #ifdef CONFIG_PM_SLEEP
257 static int pxa2xx_ac97_dev_suspend(struct device *dev)
258 {
259 	return pxa2xx_ac97_hw_suspend();
260 }
261 
262 static int pxa2xx_ac97_dev_resume(struct device *dev)
263 {
264 	return pxa2xx_ac97_hw_resume();
265 }
266 
267 static SIMPLE_DEV_PM_OPS(pxa2xx_ac97_pm_ops,
268 		pxa2xx_ac97_dev_suspend, pxa2xx_ac97_dev_resume);
269 #endif
270 
271 static struct platform_driver pxa2xx_ac97_driver = {
272 	.probe		= pxa2xx_ac97_dev_probe,
273 	.remove		= pxa2xx_ac97_dev_remove,
274 	.driver		= {
275 		.name	= "pxa2xx-ac97",
276 #ifdef CONFIG_PM_SLEEP
277 		.pm	= &pxa2xx_ac97_pm_ops,
278 #endif
279 	},
280 };
281 
282 module_platform_driver(pxa2xx_ac97_driver);
283 
284 MODULE_AUTHOR("Nicolas Pitre");
285 MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
286 MODULE_LICENSE("GPL");
287 MODULE_ALIAS("platform:pxa2xx-ac97");
288