xref: /openbmc/linux/sound/soc/pxa/pxa2xx-ac97.c (revision a4519526)
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();
33 
34 	pxa2xx_ac97_finish_reset();
35 }
36 
37 static void pxa2xx_ac97_cold_reset(struct snd_ac97 *ac97)
38 {
39 	pxa2xx_ac97_try_cold_reset();
40 
41 	pxa2xx_ac97_finish_reset();
42 }
43 
44 static unsigned short pxa2xx_ac97_legacy_read(struct snd_ac97 *ac97,
45 					      unsigned short reg)
46 {
47 	int ret;
48 
49 	ret = pxa2xx_ac97_read(ac97->num, reg);
50 	if (ret < 0)
51 		return 0;
52 	else
53 		return (unsigned short)(ret & 0xffff);
54 }
55 
56 static void pxa2xx_ac97_legacy_write(struct snd_ac97 *ac97,
57 				     unsigned short reg, unsigned short val)
58 {
59 	int ret;
60 
61 	ret = pxa2xx_ac97_write(ac97->num, reg, val);
62 }
63 
64 static struct snd_ac97_bus_ops pxa2xx_ac97_ops = {
65 	.read	= pxa2xx_ac97_legacy_read,
66 	.write	= pxa2xx_ac97_legacy_write,
67 	.warm_reset	= pxa2xx_ac97_warm_reset,
68 	.reset	= pxa2xx_ac97_cold_reset,
69 };
70 
71 static struct pxad_param pxa2xx_ac97_pcm_stereo_in_req = {
72 	.prio = PXAD_PRIO_LOWEST,
73 	.drcmr = 11,
74 };
75 
76 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_stereo_in = {
77 	.addr		= __PREG(PCDR),
78 	.addr_width	= DMA_SLAVE_BUSWIDTH_4_BYTES,
79 	.maxburst	= 32,
80 	.filter_data	= &pxa2xx_ac97_pcm_stereo_in_req,
81 };
82 
83 static struct pxad_param pxa2xx_ac97_pcm_stereo_out_req = {
84 	.prio = PXAD_PRIO_LOWEST,
85 	.drcmr = 12,
86 };
87 
88 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_stereo_out = {
89 	.addr		= __PREG(PCDR),
90 	.addr_width	= DMA_SLAVE_BUSWIDTH_4_BYTES,
91 	.maxburst	= 32,
92 	.filter_data	= &pxa2xx_ac97_pcm_stereo_out_req,
93 };
94 
95 static struct pxad_param pxa2xx_ac97_pcm_aux_mono_out_req = {
96 	.prio = PXAD_PRIO_LOWEST,
97 	.drcmr = 10,
98 };
99 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_aux_mono_out = {
100 	.addr		= __PREG(MODR),
101 	.addr_width	= DMA_SLAVE_BUSWIDTH_2_BYTES,
102 	.maxburst	= 16,
103 	.filter_data	= &pxa2xx_ac97_pcm_aux_mono_out_req,
104 };
105 
106 static struct pxad_param pxa2xx_ac97_pcm_aux_mono_in_req = {
107 	.prio = PXAD_PRIO_LOWEST,
108 	.drcmr = 9,
109 };
110 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_aux_mono_in = {
111 	.addr		= __PREG(MODR),
112 	.addr_width	= DMA_SLAVE_BUSWIDTH_2_BYTES,
113 	.maxburst	= 16,
114 	.filter_data	= &pxa2xx_ac97_pcm_aux_mono_in_req,
115 };
116 
117 static struct pxad_param pxa2xx_ac97_pcm_aux_mic_mono_req = {
118 	.prio = PXAD_PRIO_LOWEST,
119 	.drcmr = 8,
120 };
121 static struct snd_dmaengine_dai_dma_data pxa2xx_ac97_pcm_mic_mono_in = {
122 	.addr		= __PREG(MCDR),
123 	.addr_width	= DMA_SLAVE_BUSWIDTH_2_BYTES,
124 	.maxburst	= 16,
125 	.filter_data	= &pxa2xx_ac97_pcm_aux_mic_mono_req,
126 };
127 
128 static int pxa2xx_ac97_hifi_startup(struct snd_pcm_substream *substream,
129 				    struct snd_soc_dai *cpu_dai)
130 {
131 	struct snd_dmaengine_dai_dma_data *dma_data;
132 
133 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
134 		dma_data = &pxa2xx_ac97_pcm_stereo_out;
135 	else
136 		dma_data = &pxa2xx_ac97_pcm_stereo_in;
137 
138 	snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
139 
140 	return 0;
141 }
142 
143 static int pxa2xx_ac97_aux_startup(struct snd_pcm_substream *substream,
144 				   struct snd_soc_dai *cpu_dai)
145 {
146 	struct snd_dmaengine_dai_dma_data *dma_data;
147 
148 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
149 		dma_data = &pxa2xx_ac97_pcm_aux_mono_out;
150 	else
151 		dma_data = &pxa2xx_ac97_pcm_aux_mono_in;
152 
153 	snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
154 
155 	return 0;
156 }
157 
158 static int pxa2xx_ac97_mic_startup(struct snd_pcm_substream *substream,
159 				   struct snd_soc_dai *cpu_dai)
160 {
161 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
162 		return -ENODEV;
163 	snd_soc_dai_set_dma_data(cpu_dai, substream,
164 				 &pxa2xx_ac97_pcm_mic_mono_in);
165 
166 	return 0;
167 }
168 
169 #define PXA2XX_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
170 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
171 		SNDRV_PCM_RATE_48000)
172 
173 static const struct snd_soc_dai_ops pxa_ac97_hifi_dai_ops = {
174 	.startup	= pxa2xx_ac97_hifi_startup,
175 };
176 
177 static const struct snd_soc_dai_ops pxa_ac97_aux_dai_ops = {
178 	.startup	= pxa2xx_ac97_aux_startup,
179 };
180 
181 static const struct snd_soc_dai_ops pxa_ac97_mic_dai_ops = {
182 	.startup	= pxa2xx_ac97_mic_startup,
183 };
184 
185 /*
186  * There is only 1 physical AC97 interface for pxa2xx, but it
187  * has extra fifo's that can be used for aux DACs and ADCs.
188  */
189 static struct snd_soc_dai_driver pxa_ac97_dai_driver[] = {
190 {
191 	.name = "pxa2xx-ac97",
192 	.bus_control = true,
193 	.playback = {
194 		.stream_name = "AC97 Playback",
195 		.channels_min = 2,
196 		.channels_max = 2,
197 		.rates = PXA2XX_AC97_RATES,
198 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
199 	.capture = {
200 		.stream_name = "AC97 Capture",
201 		.channels_min = 2,
202 		.channels_max = 2,
203 		.rates = PXA2XX_AC97_RATES,
204 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
205 	.ops = &pxa_ac97_hifi_dai_ops,
206 },
207 {
208 	.name = "pxa2xx-ac97-aux",
209 	.bus_control = true,
210 	.playback = {
211 		.stream_name = "AC97 Aux Playback",
212 		.channels_min = 1,
213 		.channels_max = 1,
214 		.rates = PXA2XX_AC97_RATES,
215 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
216 	.capture = {
217 		.stream_name = "AC97 Aux Capture",
218 		.channels_min = 1,
219 		.channels_max = 1,
220 		.rates = PXA2XX_AC97_RATES,
221 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
222 	.ops = &pxa_ac97_aux_dai_ops,
223 },
224 {
225 	.name = "pxa2xx-ac97-mic",
226 	.bus_control = true,
227 	.capture = {
228 		.stream_name = "AC97 Mic Capture",
229 		.channels_min = 1,
230 		.channels_max = 1,
231 		.rates = PXA2XX_AC97_RATES,
232 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
233 	.ops = &pxa_ac97_mic_dai_ops,
234 },
235 };
236 
237 static const struct snd_soc_component_driver pxa_ac97_component = {
238 	.name		= "pxa-ac97",
239 };
240 
241 #ifdef CONFIG_OF
242 static const struct of_device_id pxa2xx_ac97_dt_ids[] = {
243 	{ .compatible = "marvell,pxa250-ac97", },
244 	{ .compatible = "marvell,pxa270-ac97", },
245 	{ .compatible = "marvell,pxa300-ac97", },
246 	{ }
247 };
248 MODULE_DEVICE_TABLE(of, pxa2xx_ac97_dt_ids);
249 
250 #endif
251 
252 static int pxa2xx_ac97_dev_probe(struct platform_device *pdev)
253 {
254 	int ret;
255 
256 	if (pdev->id != -1) {
257 		dev_err(&pdev->dev, "PXA2xx has only one AC97 port.\n");
258 		return -ENXIO;
259 	}
260 
261 	ret = pxa2xx_ac97_hw_probe(pdev);
262 	if (ret) {
263 		dev_err(&pdev->dev, "PXA2xx AC97 hw probe error (%d)\n", ret);
264 		return ret;
265 	}
266 
267 	ret = snd_soc_set_ac97_ops(&pxa2xx_ac97_ops);
268 	if (ret != 0)
269 		return ret;
270 
271 	/* Punt most of the init to the SoC probe; we may need the machine
272 	 * driver to do interesting things with the clocking to get us up
273 	 * and running.
274 	 */
275 	return snd_soc_register_component(&pdev->dev, &pxa_ac97_component,
276 					  pxa_ac97_dai_driver, ARRAY_SIZE(pxa_ac97_dai_driver));
277 }
278 
279 static int pxa2xx_ac97_dev_remove(struct platform_device *pdev)
280 {
281 	snd_soc_unregister_component(&pdev->dev);
282 	snd_soc_set_ac97_ops(NULL);
283 	pxa2xx_ac97_hw_remove(pdev);
284 	return 0;
285 }
286 
287 #ifdef CONFIG_PM_SLEEP
288 static int pxa2xx_ac97_dev_suspend(struct device *dev)
289 {
290 	return pxa2xx_ac97_hw_suspend();
291 }
292 
293 static int pxa2xx_ac97_dev_resume(struct device *dev)
294 {
295 	return pxa2xx_ac97_hw_resume();
296 }
297 
298 static SIMPLE_DEV_PM_OPS(pxa2xx_ac97_pm_ops,
299 		pxa2xx_ac97_dev_suspend, pxa2xx_ac97_dev_resume);
300 #endif
301 
302 static struct platform_driver pxa2xx_ac97_driver = {
303 	.probe		= pxa2xx_ac97_dev_probe,
304 	.remove		= pxa2xx_ac97_dev_remove,
305 	.driver		= {
306 		.name	= "pxa2xx-ac97",
307 #ifdef CONFIG_PM_SLEEP
308 		.pm	= &pxa2xx_ac97_pm_ops,
309 #endif
310 		.of_match_table = of_match_ptr(pxa2xx_ac97_dt_ids),
311 	},
312 };
313 
314 module_platform_driver(pxa2xx_ac97_driver);
315 
316 MODULE_AUTHOR("Nicolas Pitre");
317 MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
318 MODULE_LICENSE("GPL");
319 MODULE_ALIAS("platform:pxa2xx-ac97");
320