aboutsummaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
Diffstat (limited to 'sound')
-rw-r--r--sound/arm/Kconfig14
-rw-r--r--sound/arm/Makefile8
-rw-r--r--sound/arm/pxa2xx-ac97.c410
-rw-r--r--sound/arm/pxa2xx-pcm.c367
-rw-r--r--sound/arm/pxa2xx-pcm.h29
5 files changed, 824 insertions, 4 deletions
diff --git a/sound/arm/Kconfig b/sound/arm/Kconfig
index 34c1740aa6e9..2e4a5e0d16db 100644
--- a/sound/arm/Kconfig
+++ b/sound/arm/Kconfig
@@ -20,5 +20,17 @@ config SND_ARMAACI
20 select SND_PCM 20 select SND_PCM
21 select SND_AC97_CODEC 21 select SND_AC97_CODEC
22 22
23endmenu 23config SND_PXA2XX_PCM
24 tristate
25 select SND_PCM
26
27config SND_PXA2XX_AC97
28 tristate "AC97 driver for the Intel PXA2xx chip"
29 depends on ARCH_PXA && SND
30 select SND_PXA2XX_PCM
31 select SND_AC97_CODEC
32 help
33 Say Y or M if you want to support any AC97 codec attached to
34 the PXA2xx AC97 interface.
24 35
36endmenu
diff --git a/sound/arm/Makefile b/sound/arm/Makefile
index f74ec28e1068..103f136926d9 100644
--- a/sound/arm/Makefile
+++ b/sound/arm/Makefile
@@ -3,9 +3,11 @@
3# 3#
4 4
5snd-sa11xx-uda1341-objs := sa11xx-uda1341.o 5snd-sa11xx-uda1341-objs := sa11xx-uda1341.o
6snd-aaci-objs := aaci.o devdma.o
7snd-pxa2xx-pcm-objs := pxa2xx-pcm.o
8snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
6 9
7# Toplevel Module Dependency
8obj-$(CONFIG_SND_SA11XX_UDA1341) += snd-sa11xx-uda1341.o 10obj-$(CONFIG_SND_SA11XX_UDA1341) += snd-sa11xx-uda1341.o
9
10obj-$(CONFIG_SND_ARMAACI) += snd-aaci.o 11obj-$(CONFIG_SND_ARMAACI) += snd-aaci.o
11snd-aaci-objs := aaci.o devdma.o 12obj-$(CONFIG_SND_PXA2XX_PCM) += snd-pxa2xx-pcm.o
13obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
diff --git a/sound/arm/pxa2xx-ac97.c b/sound/arm/pxa2xx-ac97.c
new file mode 100644
index 000000000000..46052304e230
--- /dev/null
+++ b/sound/arm/pxa2xx-ac97.c
@@ -0,0 +1,410 @@
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/module.h>
15#include <linux/kernel.h>
16#include <linux/device.h>
17#include <linux/interrupt.h>
18#include <linux/wait.h>
19#include <linux/delay.h>
20
21#include <sound/driver.h>
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/ac97_codec.h>
25#include <sound/initval.h>
26
27#include <asm/irq.h>
28#include <asm/semaphore.h>
29#include <asm/hardware.h>
30#include <asm/arch/pxa-regs.h>
31#include <asm/arch/audio.h>
32
33#include "pxa2xx-pcm.h"
34
35
36static DECLARE_MUTEX(car_mutex);
37static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
38static volatile long gsr_bits;
39
40static unsigned short pxa2xx_ac97_read(ac97_t *ac97, unsigned short reg)
41{
42 unsigned short val = -1;
43 volatile u32 *reg_addr;
44
45 down(&car_mutex);
46 if (CAR & CAR_CAIP) {
47 printk(KERN_CRIT"%s: CAR_CAIP already set\n", __FUNCTION__);
48 goto out;
49 }
50
51 /* set up primary or secondary codec space */
52 reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
53 reg_addr += (reg >> 1);
54
55 /* start read access across the ac97 link */
56 gsr_bits = 0;
57 val = *reg_addr;
58 if (reg == AC97_GPIO_STATUS)
59 goto out;
60 wait_event_timeout(gsr_wq, gsr_bits & GSR_SDONE, 1);
61 if (!gsr_bits & GSR_SDONE) {
62 printk(KERN_ERR "%s: read error (ac97_reg=%d GSR=%#lx)\n",
63 __FUNCTION__, reg, gsr_bits);
64 val = -1;
65 goto out;
66 }
67
68 /* valid data now */
69 gsr_bits = 0;
70 val = *reg_addr;
71 /* but we've just started another cycle... */
72 wait_event_timeout(gsr_wq, gsr_bits & GSR_SDONE, 1);
73
74out: up(&car_mutex);
75 return val;
76}
77
78static void pxa2xx_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
79{
80 volatile u32 *reg_addr;
81
82 down(&car_mutex);
83
84 if (CAR & CAR_CAIP) {
85 printk(KERN_CRIT "%s: CAR_CAIP already set\n", __FUNCTION__);
86 goto out;
87 }
88
89 /* set up primary or secondary codec space */
90 reg_addr = (ac97->num & 1) ? &SAC_REG_BASE : &PAC_REG_BASE;
91 reg_addr += (reg >> 1);
92 gsr_bits = 0;
93 *reg_addr = val;
94 wait_event_timeout(gsr_wq, gsr_bits & GSR_CDONE, 1);
95 if (!gsr_bits & GSR_SDONE)
96 printk(KERN_ERR "%s: write error (ac97_reg=%d GSR=%#lx)\n",
97 __FUNCTION__, reg, gsr_bits);
98
99out: up(&car_mutex);
100}
101
102static void pxa2xx_ac97_reset(ac97_t *ac97)
103{
104 /* First, try cold reset */
105 GCR &= GCR_COLD_RST; /* clear everything but nCRST */
106 GCR &= ~GCR_COLD_RST; /* then assert nCRST */
107
108 gsr_bits = 0;
109#ifdef CONFIG_PXA27x
110 /* PXA27x Developers Manual section 13.5.2.2.1 */
111 pxa_set_cken(1 << 31, 1);
112 udelay(5);
113 pxa_set_cken(1 << 31, 0);
114 GCR = GCR_COLD_RST;
115 udelay(50);
116#else
117 GCR = GCR_COLD_RST;
118 GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
119 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
120#endif
121
122 if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR))) {
123 printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
124 __FUNCTION__, gsr_bits);
125
126 /* let's try warm reset */
127 gsr_bits = 0;
128#ifdef CONFIG_PXA27x
129 /* warm reset broken on Bulverde,
130 so manually keep AC97 reset high */
131 pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
132 udelay(10);
133 GCR |= GCR_WARM_RST;
134 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
135 udelay(50);
136#else
137 GCR |= GCR_WARM_RST|GCR_PRIRDY_IEN|GCR_SECRDY_IEN;;
138 wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
139#endif
140
141 if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
142 printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
143 __FUNCTION__, gsr_bits);
144 }
145
146 GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
147 GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
148}
149
150static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id, struct pt_regs *regs)
151{
152 long status;
153
154 status = GSR;
155 if (status) {
156 GSR = status;
157 gsr_bits |= status;
158 wake_up(&gsr_wq);
159
160#ifdef CONFIG_PXA27x
161 /* Although we don't use those we still need to clear them
162 since they tend to spuriously trigger when MMC is used
163 (hardware bug? go figure)... */
164 MISR = MISR_EOC;
165 PISR = PISR_EOC;
166 MCSR = MCSR_EOC;
167#endif
168
169 return IRQ_HANDLED;
170 }
171
172 return IRQ_NONE;
173}
174
175static ac97_bus_ops_t pxa2xx_ac97_ops = {
176 .read = pxa2xx_ac97_read,
177 .write = pxa2xx_ac97_write,
178 .reset = pxa2xx_ac97_reset,
179};
180
181static pxa2xx_pcm_dma_params_t pxa2xx_ac97_pcm_out = {
182 .name = "AC97 PCM out",
183 .dev_addr = __PREG(PCDR),
184 .drcmr = &DRCMRTXPCDR,
185 .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
186 DCMD_BURST32 | DCMD_WIDTH4,
187};
188
189static pxa2xx_pcm_dma_params_t pxa2xx_ac97_pcm_in = {
190 .name = "AC97 PCM in",
191 .dev_addr = __PREG(PCDR),
192 .drcmr = &DRCMRRXPCDR,
193 .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
194 DCMD_BURST32 | DCMD_WIDTH4,
195};
196
197static snd_pcm_t *pxa2xx_ac97_pcm;
198static ac97_t *pxa2xx_ac97_ac97;
199
200static int pxa2xx_ac97_pcm_startup(snd_pcm_substream_t *substream)
201{
202 snd_pcm_runtime_t *runtime = substream->runtime;
203 pxa2xx_audio_ops_t *platform_ops;
204 int r;
205
206 runtime->hw.channels_min = 2;
207 runtime->hw.channels_max = 2;
208
209 r = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
210 AC97_RATES_FRONT_DAC : AC97_RATES_ADC;
211 runtime->hw.rates = pxa2xx_ac97_ac97->rates[r];
212 snd_pcm_limit_hw_rates(runtime);
213
214 platform_ops = substream->pcm->card->dev->platform_data;
215 if (platform_ops && platform_ops->startup)
216 return platform_ops->startup(substream, platform_ops->priv);
217 else
218 return 0;
219}
220
221static void pxa2xx_ac97_pcm_shutdown(snd_pcm_substream_t *substream)
222{
223 pxa2xx_audio_ops_t *platform_ops;
224
225 platform_ops = substream->pcm->card->dev->platform_data;
226 if (platform_ops && platform_ops->shutdown)
227 platform_ops->shutdown(substream, platform_ops->priv);
228}
229
230static int pxa2xx_ac97_pcm_prepare(snd_pcm_substream_t *substream)
231{
232 snd_pcm_runtime_t *runtime = substream->runtime;
233 int reg = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
234 AC97_PCM_FRONT_DAC_RATE : AC97_PCM_LR_ADC_RATE;
235 return snd_ac97_set_rate(pxa2xx_ac97_ac97, reg, runtime->rate);
236}
237
238static pxa2xx_pcm_client_t pxa2xx_ac97_pcm_client = {
239 .playback_params = &pxa2xx_ac97_pcm_out,
240 .capture_params = &pxa2xx_ac97_pcm_in,
241 .startup = pxa2xx_ac97_pcm_startup,
242 .shutdown = pxa2xx_ac97_pcm_shutdown,
243 .prepare = pxa2xx_ac97_pcm_prepare,
244};
245
246#ifdef CONFIG_PM
247
248static int pxa2xx_ac97_do_suspend(snd_card_t *card, unsigned int state)
249{
250 if (card->power_state != SNDRV_CTL_POWER_D3cold) {
251 pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
252 snd_pcm_suspend_all(pxa2xx_ac97_pcm);
253 snd_ac97_suspend(pxa2xx_ac97_ac97);
254 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
255 if (platform_ops && platform_ops->suspend)
256 platform_ops->suspend(platform_ops->priv);
257 GCR |= GCR_ACLINK_OFF;
258 pxa_set_cken(CKEN2_AC97, 0);
259 }
260
261 return 0;
262}
263
264static int pxa2xx_ac97_do_resume(snd_card_t *card, unsigned int state)
265{
266 if (card->power_state != SNDRV_CTL_POWER_D0) {
267 pxa2xx_audio_ops_t *platform_ops = card->dev->platform_data;
268 pxa_set_cken(CKEN2_AC97, 1);
269 if (platform_ops && platform_ops->resume)
270 platform_ops->resume(platform_ops->priv);
271 snd_ac97_resume(pxa2xx_ac97_ac97);
272 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
273 }
274
275 return 0;
276}
277
278static int pxa2xx_ac97_suspend(struct device *_dev, u32 state, u32 level)
279{
280 snd_card_t *card = dev_get_drvdata(_dev);
281 int ret = 0;
282
283 if (card && level == SUSPEND_DISABLE)
284 ret = pxa2xx_ac97_do_suspend(card, SNDRV_CTL_POWER_D3cold);
285
286 return ret;
287}
288
289static int pxa2xx_ac97_resume(struct device *_dev, u32 level)
290{
291 snd_card_t *card = dev_get_drvdata(_dev);
292 int ret = 0;
293
294 if (card && level == RESUME_ENABLE)
295 ret = pxa2xx_ac97_do_resume(card, SNDRV_CTL_POWER_D0);
296
297 return ret;
298}
299
300#else
301#define pxa2xx_ac97_suspend NULL
302#define pxa2xx_ac97_resume NULL
303#endif
304
305static int pxa2xx_ac97_probe(struct device *dev)
306{
307 snd_card_t *card;
308 ac97_bus_t *ac97_bus;
309 ac97_template_t ac97_template;
310 int ret;
311
312 ret = -ENOMEM;
313 card = snd_card_new(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
314 THIS_MODULE, 0);
315 if (!card)
316 goto err;
317
318 card->dev = dev;
319 strncpy(card->driver, dev->driver->name, sizeof(card->driver));
320
321 ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);
322 if (ret)
323 goto err;
324
325 ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, 0, "AC97", NULL);
326 if (ret < 0)
327 goto err;
328
329 pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
330 pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
331 pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
332 pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
333#ifdef CONFIG_PXA27x
334 /* Use GPIO 113 as AC97 Reset on Bulverde */
335 pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
336#endif
337 pxa_set_cken(CKEN2_AC97, 1);
338
339 ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);
340 if (ret)
341 goto err;
342 memset(&ac97_template, 0, sizeof(ac97_template));
343 ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);
344 if (ret)
345 goto err;
346
347 snprintf(card->shortname, sizeof(card->shortname),
348 "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));
349 snprintf(card->longname, sizeof(card->longname),
350 "%s (%s)", dev->driver->name, card->mixername);
351
352 snd_card_set_pm_callback(card, pxa2xx_ac97_do_suspend,
353 pxa2xx_ac97_do_resume, NULL);
354 ret = snd_card_register(card);
355 if (ret == 0) {
356 dev_set_drvdata(dev, card);
357 return 0;
358 }
359
360 err:
361 if (card)
362 snd_card_free(card);
363 if (CKEN & CKEN2_AC97) {
364 GCR |= GCR_ACLINK_OFF;
365 free_irq(IRQ_AC97, NULL);
366 pxa_set_cken(CKEN2_AC97, 0);
367 }
368 return ret;
369}
370
371static int pxa2xx_ac97_remove(struct device *dev)
372{
373 snd_card_t *card = dev_get_drvdata(dev);
374
375 if (card) {
376 snd_card_free(card);
377 dev_set_drvdata(dev, NULL);
378 GCR |= GCR_ACLINK_OFF;
379 free_irq(IRQ_AC97, NULL);
380 pxa_set_cken(CKEN2_AC97, 0);
381 }
382
383 return 0;
384}
385
386static struct device_driver pxa2xx_ac97_driver = {
387 .name = "pxa2xx-ac97",
388 .bus = &platform_bus_type,
389 .probe = pxa2xx_ac97_probe,
390 .remove = pxa2xx_ac97_remove,
391 .suspend = pxa2xx_ac97_suspend,
392 .resume = pxa2xx_ac97_resume,
393};
394
395static int __init pxa2xx_ac97_init(void)
396{
397 return driver_register(&pxa2xx_ac97_driver);
398}
399
400static void __exit pxa2xx_ac97_exit(void)
401{
402 driver_unregister(&pxa2xx_ac97_driver);
403}
404
405module_init(pxa2xx_ac97_init);
406module_exit(pxa2xx_ac97_exit);
407
408MODULE_AUTHOR("Nicolas Pitre");
409MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
410MODULE_LICENSE("GPL");
diff --git a/sound/arm/pxa2xx-pcm.c b/sound/arm/pxa2xx-pcm.c
new file mode 100644
index 000000000000..b1eb53b02eae
--- /dev/null
+++ b/sound/arm/pxa2xx-pcm.c
@@ -0,0 +1,367 @@
1/*
2 * linux/sound/arm/pxa2xx-pcm.c -- ALSA PCM interface for the Intel PXA2xx chip
3 *
4 * Author: Nicolas Pitre
5 * Created: Nov 30, 2004
6 * Copyright: (C) 2004 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/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/slab.h>
17#include <linux/dma-mapping.h>
18
19#include <sound/driver.h>
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23
24#include <asm/dma.h>
25#include <asm/hardware.h>
26#include <asm/arch/pxa-regs.h>
27
28#include "pxa2xx-pcm.h"
29
30
31static const snd_pcm_hardware_t pxa2xx_pcm_hardware = {
32 .info = SNDRV_PCM_INFO_MMAP |
33 SNDRV_PCM_INFO_MMAP_VALID |
34 SNDRV_PCM_INFO_INTERLEAVED |
35 SNDRV_PCM_INFO_PAUSE,
36 .formats = SNDRV_PCM_FMTBIT_S16_LE,
37 .period_bytes_min = 32,
38 .period_bytes_max = 8192 - 32,
39 .periods_min = 1,
40 .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc),
41 .buffer_bytes_max = 128 * 1024,
42 .fifo_size = 32,
43};
44
45struct pxa2xx_runtime_data {
46 int dma_ch;
47 pxa2xx_pcm_dma_params_t *params;
48 pxa_dma_desc *dma_desc_array;
49 dma_addr_t dma_desc_array_phys;
50};
51
52static int pxa2xx_pcm_hw_params(snd_pcm_substream_t *substream,
53 snd_pcm_hw_params_t *params)
54{
55 snd_pcm_runtime_t *runtime = substream->runtime;
56 struct pxa2xx_runtime_data *rtd = runtime->private_data;
57 size_t totsize = params_buffer_bytes(params);
58 size_t period = params_period_bytes(params);
59 pxa_dma_desc *dma_desc;
60 dma_addr_t dma_buff_phys, next_desc_phys;
61
62 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
63 runtime->dma_bytes = totsize;
64
65 dma_desc = rtd->dma_desc_array;
66 next_desc_phys = rtd->dma_desc_array_phys;
67 dma_buff_phys = runtime->dma_addr;
68 do {
69 next_desc_phys += sizeof(pxa_dma_desc);
70 dma_desc->ddadr = next_desc_phys;
71 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
72 dma_desc->dsadr = dma_buff_phys;
73 dma_desc->dtadr = rtd->params->dev_addr;
74 } else {
75 dma_desc->dsadr = rtd->params->dev_addr;
76 dma_desc->dtadr = dma_buff_phys;
77 }
78 if (period > totsize)
79 period = totsize;
80 dma_desc->dcmd = rtd->params->dcmd | period | DCMD_ENDIRQEN;
81 dma_desc++;
82 dma_buff_phys += period;
83 } while (totsize -= period);
84 dma_desc[-1].ddadr = rtd->dma_desc_array_phys;
85
86 return 0;
87}
88
89static int pxa2xx_pcm_hw_free(snd_pcm_substream_t *substream)
90{
91 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
92
93 *rtd->params->drcmr = 0;
94 snd_pcm_set_runtime_buffer(substream, NULL);
95 return 0;
96}
97
98static int pxa2xx_pcm_prepare(snd_pcm_substream_t *substream)
99{
100 pxa2xx_pcm_client_t *client = substream->private_data;
101 snd_pcm_runtime_t *runtime = substream->runtime;
102 struct pxa2xx_runtime_data *rtd = runtime->private_data;
103
104 DCSR(rtd->dma_ch) &= ~DCSR_RUN;
105 DCSR(rtd->dma_ch) = 0;
106 DCMD(rtd->dma_ch) = 0;
107 *rtd->params->drcmr = rtd->dma_ch | DRCMR_MAPVLD;
108
109 return client->prepare(substream);
110}
111
112static int pxa2xx_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
113{
114 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
115 int ret = 0;
116
117 switch (cmd) {
118 case SNDRV_PCM_TRIGGER_START:
119 DDADR(rtd->dma_ch) = rtd->dma_desc_array_phys;
120 DCSR(rtd->dma_ch) = DCSR_RUN;
121 break;
122
123 case SNDRV_PCM_TRIGGER_STOP:
124 case SNDRV_PCM_TRIGGER_SUSPEND:
125 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
126 DCSR(rtd->dma_ch) &= ~DCSR_RUN;
127 break;
128
129 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
130 DCSR(rtd->dma_ch) |= DCSR_RUN;
131 break;
132
133 default:
134 ret = -EINVAL;
135 }
136
137 return ret;
138}
139
140static void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id, struct pt_regs *regs)
141{
142 snd_pcm_substream_t *substream = dev_id;
143 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
144 int dcsr;
145
146 dcsr = DCSR(dma_ch);
147 DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
148
149 if (dcsr & DCSR_ENDINTR) {
150 snd_pcm_period_elapsed(substream);
151 } else {
152 printk( KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
153 rtd->params->name, dma_ch, dcsr );
154 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
155 }
156}
157
158static snd_pcm_uframes_t pxa2xx_pcm_pointer(snd_pcm_substream_t *substream)
159{
160 snd_pcm_runtime_t *runtime = substream->runtime;
161 struct pxa2xx_runtime_data *rtd = runtime->private_data;
162 dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
163 DSADR(rtd->dma_ch) : DTADR(rtd->dma_ch);
164 snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr);
165 if (x == runtime->buffer_size)
166 x = 0;
167 return x;
168}
169
170static int
171pxa2xx_pcm_hw_rule_mult32(snd_pcm_hw_params_t *params, snd_pcm_hw_rule_t *rule)
172{
173 snd_interval_t *i = hw_param_interval(params, rule->var);
174 int changed = 0;
175
176 if (i->min & 31) {
177 i->min = (i->min & ~31) + 32;
178 i->openmin = 0;
179 changed = 1;
180 }
181
182 if (i->max & 31) {
183 i->max &= ~31;
184 i->openmax = 0;
185 changed = 1;
186 }
187
188 return changed;
189}
190
191static int pxa2xx_pcm_open(snd_pcm_substream_t *substream)
192{
193 pxa2xx_pcm_client_t *client = substream->private_data;
194 snd_pcm_runtime_t *runtime = substream->runtime;
195 struct pxa2xx_runtime_data *rtd;
196 int ret;
197
198 runtime->hw = pxa2xx_pcm_hardware;
199
200 /*
201 * For mysterious reasons (and despite what the manual says)
202 * playback samples are lost if the DMA count is not a multiple
203 * of the DMA burst size. Let's add a rule to enforce that.
204 */
205 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
206 pxa2xx_pcm_hw_rule_mult32, NULL,
207 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, -1);
208 if (ret)
209 goto out;
210 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
211 pxa2xx_pcm_hw_rule_mult32, NULL,
212 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
213 if (ret)
214 goto out;
215
216 ret = -ENOMEM;
217 rtd = kmalloc(sizeof(*rtd), GFP_KERNEL);
218 if (!rtd)
219 goto out;
220 rtd->dma_desc_array =
221 dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
222 &rtd->dma_desc_array_phys, GFP_KERNEL);
223 if (!rtd->dma_desc_array)
224 goto err1;
225
226 rtd->params = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
227 client->playback_params : client->capture_params;
228 ret = pxa_request_dma(rtd->params->name, DMA_PRIO_LOW,
229 pxa2xx_pcm_dma_irq, substream);
230 if (ret < 0)
231 goto err2;
232 rtd->dma_ch = ret;
233
234 runtime->private_data = rtd;
235 ret = client->startup(substream);
236 if (!ret)
237 goto out;
238
239 pxa_free_dma(rtd->dma_ch);
240 err2:
241 dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
242 rtd->dma_desc_array, rtd->dma_desc_array_phys);
243 err1:
244 kfree(rtd);
245 out:
246 return ret;
247}
248
249static int pxa2xx_pcm_close(snd_pcm_substream_t *substream)
250{
251 pxa2xx_pcm_client_t *client = substream->private_data;
252 struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
253
254 pxa_free_dma(rtd->dma_ch);
255 client->shutdown(substream);
256 dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
257 rtd->dma_desc_array, rtd->dma_desc_array_phys);
258 kfree(rtd);
259 return 0;
260}
261
262static int
263pxa2xx_pcm_mmap(snd_pcm_substream_t *substream, struct vm_area_struct *vma)
264{
265 snd_pcm_runtime_t *runtime = substream->runtime;
266 return dma_mmap_writecombine(substream->pcm->card->dev, vma,
267 runtime->dma_area,
268 runtime->dma_addr,
269 runtime->dma_bytes);
270}
271
272static snd_pcm_ops_t pxa2xx_pcm_ops = {
273 .open = pxa2xx_pcm_open,
274 .close = pxa2xx_pcm_close,
275 .ioctl = snd_pcm_lib_ioctl,
276 .hw_params = pxa2xx_pcm_hw_params,
277 .hw_free = pxa2xx_pcm_hw_free,
278 .prepare = pxa2xx_pcm_prepare,
279 .trigger = pxa2xx_pcm_trigger,
280 .pointer = pxa2xx_pcm_pointer,
281 .mmap = pxa2xx_pcm_mmap,
282};
283
284static int pxa2xx_pcm_preallocate_dma_buffer(snd_pcm_t *pcm, int stream)
285{
286 snd_pcm_substream_t *substream = pcm->streams[stream].substream;
287 struct snd_dma_buffer *buf = &substream->dma_buffer;
288 size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
289 buf->dev.type = SNDRV_DMA_TYPE_DEV;
290 buf->dev.dev = pcm->card->dev;
291 buf->private_data = NULL;
292 buf->area = dma_alloc_writecombine(pcm->card->dev, size,
293 &buf->addr, GFP_KERNEL);
294 if (!buf->area)
295 return -ENOMEM;
296 buf->bytes = size;
297 return 0;
298}
299
300static void pxa2xx_pcm_free_dma_buffers(snd_pcm_t *pcm)
301{
302 snd_pcm_substream_t *substream;
303 struct snd_dma_buffer *buf;
304 int stream;
305
306 for (stream = 0; stream < 2; stream++) {
307 substream = pcm->streams[stream].substream;
308 if (!substream)
309 continue;
310 buf = &substream->dma_buffer;
311 if (!buf->area)
312 continue;
313 dma_free_writecombine(pcm->card->dev, buf->bytes,
314 buf->area, buf->addr);
315 buf->area = NULL;
316 }
317}
318
319static u64 pxa2xx_pcm_dmamask = 0xffffffff;
320
321int pxa2xx_pcm_new(snd_card_t *card, pxa2xx_pcm_client_t *client, snd_pcm_t **rpcm)
322{
323 snd_pcm_t *pcm;
324 int play = client->playback_params ? 1 : 0;
325 int capt = client->capture_params ? 1 : 0;
326 int ret;
327
328 ret = snd_pcm_new(card, "PXA2xx-PCM", 0, play, capt, &pcm);
329 if (ret)
330 goto out;
331
332 pcm->private_data = client;
333 pcm->private_free = pxa2xx_pcm_free_dma_buffers;
334
335 if (!card->dev->dma_mask)
336 card->dev->dma_mask = &pxa2xx_pcm_dmamask;
337 if (!card->dev->coherent_dma_mask)
338 card->dev->coherent_dma_mask = 0xffffffff;
339
340 if (play) {
341 int stream = SNDRV_PCM_STREAM_PLAYBACK;
342 snd_pcm_set_ops(pcm, stream, &pxa2xx_pcm_ops);
343 ret = pxa2xx_pcm_preallocate_dma_buffer(pcm, stream);
344 if (ret)
345 goto out;
346 }
347 if (capt) {
348 int stream = SNDRV_PCM_STREAM_CAPTURE;
349 snd_pcm_set_ops(pcm, stream, &pxa2xx_pcm_ops);
350 ret = pxa2xx_pcm_preallocate_dma_buffer(pcm, stream);
351 if (ret)
352 goto out;
353 }
354
355 if (rpcm)
356 *rpcm = pcm;
357 ret = 0;
358
359 out:
360 return ret;
361}
362
363EXPORT_SYMBOL(pxa2xx_pcm_new);
364
365MODULE_AUTHOR("Nicolas Pitre");
366MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
367MODULE_LICENSE("GPL");
diff --git a/sound/arm/pxa2xx-pcm.h b/sound/arm/pxa2xx-pcm.h
new file mode 100644
index 000000000000..43517597cab9
--- /dev/null
+++ b/sound/arm/pxa2xx-pcm.h
@@ -0,0 +1,29 @@
1/*
2 * linux/sound/arm/pxa2xx-pcm.h -- ALSA PCM interface for the Intel PXA2xx chip
3 *
4 * Author: Nicolas Pitre
5 * Created: Nov 30, 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
13typedef struct {
14 char *name; /* stream identifier */
15 u32 dcmd; /* DMA descriptor dcmd field */
16 volatile u32 *drcmr; /* the DMA request channel to use */
17 u32 dev_addr; /* device physical address for DMA */
18} pxa2xx_pcm_dma_params_t;
19
20typedef struct {
21 pxa2xx_pcm_dma_params_t *playback_params;
22 pxa2xx_pcm_dma_params_t *capture_params;
23 int (*startup)(snd_pcm_substream_t *);
24 void (*shutdown)(snd_pcm_substream_t *);
25 int (*prepare)(snd_pcm_substream_t *);
26} pxa2xx_pcm_client_t;
27
28extern int pxa2xx_pcm_new(snd_card_t *, pxa2xx_pcm_client_t *, snd_pcm_t **);
29