blob: 0eff33ca0f793742b97546deea5fa03b27dab220 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for Philips UDA1341TS on Compaq iPAQ H3600 soundcard
3 * Copyright (C) 2002 Tomas Kasparek <tomas.kasparek@seznam.cz>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License.
7 *
8 * History:
9 *
10 * 2002-03-13 Tomas Kasparek initial release - based on h3600-uda1341.c from OSS
11 * 2002-03-20 Tomas Kasparek playback over ALSA is working
12 * 2002-03-28 Tomas Kasparek playback over OSS emulation is working
13 * 2002-03-29 Tomas Kasparek basic capture is working (native ALSA)
14 * 2002-03-29 Tomas Kasparek capture is working (OSS emulation)
15 * 2002-04-04 Tomas Kasparek better rates handling (allow non-standard rates)
16 * 2003-02-14 Brian Avery fixed full duplex mode, other updates
17 * 2003-02-20 Tomas Kasparek merged updates by Brian (except HAL)
18 * 2003-04-19 Jaroslav Kysela recoded DMA stuff to follow 2.4.18rmk3-hh24 kernel
19 * working suspend and resume
20 * 2003-04-28 Tomas Kasparek updated work by Jaroslav to compile it under 2.5.x again
21 * merged HAL layer (patches from Brian)
22 */
23
Clemens Ladischf7a92752005-12-07 09:13:42 +010024/* $Id: sa11xx-uda1341.c,v 1.27 2005/12/07 09:13:42 cladisch Exp $ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/***************************************************************************************************
27*
28* To understand what Alsa Drivers should be doing look at "Writing an Alsa Driver" by Takashi Iwai
29* available in the Alsa doc section on the website
30*
31* A few notes to make things clearer. The UDA1341 is hooked up to Serial port 4 on the SA1100.
32* We are using SSP mode to talk to the UDA1341. The UDA1341 bit & wordselect clocks are generated
33* by this UART. Unfortunately, the clock only runs if the transmit buffer has something in it.
34* So, if we are just recording, we feed the transmit DMA stream a bunch of 0x0000 so that the
35* transmit buffer is full and the clock keeps going. The zeroes come from FLUSH_BASE_PHYS which
36* is a mem loc that always decodes to 0's w/ no off chip access.
37*
38* Some alsa terminology:
39* frame => num_channels * sample_size e.g stereo 16 bit is 2 * 16 = 32 bytes
40* period => the least number of bytes that will generate an interrupt e.g. we have a 1024 byte
41* buffer and 4 periods in the runtime structure this means we'll get an int every 256
42* bytes or 4 times per buffer.
43* A number of the sizes are in frames rather than bytes, use frames_to_bytes and
44* bytes_to_frames to convert. The easiest way to tell the units is to look at the
45* type i.e. runtime-> buffer_size is in frames and its type is snd_pcm_uframes_t
46*
47* Notes about the pointer fxn:
48* The pointer fxn needs to return the offset into the dma buffer in frames.
49* Interrupts must be blocked before calling the dma_get_pos fxn to avoid race with interrupts.
50*
51* Notes about pause/resume
52* Implementing this would be complicated so it's skipped. The problem case is:
53* A full duplex connection is going, then play is paused. At this point you need to start xmitting
54* 0's to keep the record active which means you cant just freeze the dma and resume it later you'd
55* need to save off the dma info, and restore it properly on a resume. Yeach!
56*
57* Notes about transfer methods:
58* The async write calls fail. I probably need to implement something else to support them?
59*
60***************************************************************************************************/
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/module.h>
63#include <linux/moduleparam.h>
64#include <linux/init.h>
Takashi Iwai2b3f5582005-11-17 17:19:50 +010065#include <linux/err.h>
66#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/errno.h>
68#include <linux/ioctl.h>
69#include <linux/delay.h>
70#include <linux/slab.h>
71
72#ifdef CONFIG_PM
73#include <linux/pm.h>
74#endif
75
76#include <asm/hardware.h>
77#include <asm/arch/h3600.h>
78#include <asm/mach-types.h>
79#include <asm/dma.h>
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <sound/core.h>
82#include <sound/pcm.h>
83#include <sound/initval.h>
84
85#include <linux/l3/l3.h>
86
87#undef DEBUG_MODE
88#undef DEBUG_FUNCTION_NAMES
89#include <sound/uda1341.h>
90
91/*
92 * FIXME: Is this enough as autodetection of 2.4.X-rmkY-hhZ kernels?
93 * We use DMA stuff from 2.4.18-rmk3-hh24 here to be able to compile this
94 * module for Familiar 0.6.1
95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* {{{ Type definitions */
98
99MODULE_AUTHOR("Tomas Kasparek <tomas.kasparek@seznam.cz>");
100MODULE_LICENSE("GPL");
101MODULE_DESCRIPTION("SA1100/SA1111 + UDA1341TS driver for ALSA");
102MODULE_SUPPORTED_DEVICE("{{UDA1341,iPAQ H3600 UDA1341TS}}");
103
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200104static char *id; /* ID for this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106module_param(id, charp, 0444);
107MODULE_PARM_DESC(id, "ID string for SA1100/SA1111 + UDA1341TS soundcard.");
108
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100109struct audio_stream {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 char *id; /* identification string */
111 int stream_id; /* numeric identification */
112 dma_device_t dma_dev; /* device identifier for DMA */
113#ifdef HH_VERSION
114 dmach_t dmach; /* dma channel identification */
115#else
116 dma_regs_t *dma_regs; /* points to our DMA registers */
117#endif
Mariusz Kozlowski940cc2d2006-12-06 20:38:04 -0800118 unsigned int active:1; /* we are using this stream for transfer now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int period; /* current transfer period */
120 int periods; /* current count of periods registerd in the DMA engine */
121 int tx_spin; /* are we recoding - flag used to do DMA trans. for sync */
122 unsigned int old_offset;
123 spinlock_t dma_lock; /* for locking in DMA operations (see dma-sa1100.c in the kernel) */
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100124 struct snd_pcm_substream *stream;
125};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100127struct sa11xx_uda1341 {
128 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct l3_client *uda1341;
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100130 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 long samplerate;
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100132 struct audio_stream s[2]; /* playback & capture */
133};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static unsigned int rates[] = {
136 8000, 10666, 10985, 14647,
137 16000, 21970, 22050, 24000,
138 29400, 32000, 44100, 48000,
139};
140
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100141static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .count = ARRAY_SIZE(rates),
143 .list = rates,
144 .mask = 0,
145};
146
Clemens Ladischf7a92752005-12-07 09:13:42 +0100147static struct platform_device *device;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* }}} */
150
151/* {{{ Clock and sample rate stuff */
152
153/*
154 * Stop-gap solution until rest of hh.org HAL stuff is merged.
155 */
156#define GPIO_H3600_CLK_SET0 GPIO_GPIO (12)
157#define GPIO_H3600_CLK_SET1 GPIO_GPIO (13)
158
159#ifdef CONFIG_SA1100_H3XXX
160#define clr_sa11xx_uda1341_egpio(x) clr_h3600_egpio(x)
161#define set_sa11xx_uda1341_egpio(x) set_h3600_egpio(x)
162#else
163#error This driver could serve H3x00 handhelds only!
164#endif
165
166static void sa11xx_uda1341_set_audio_clock(long val)
167{
168 switch (val) {
169 case 24000: case 32000: case 48000: /* 00: 12.288 MHz */
170 GPCR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
171 break;
172
173 case 22050: case 29400: case 44100: /* 01: 11.2896 MHz */
174 GPSR = GPIO_H3600_CLK_SET0;
175 GPCR = GPIO_H3600_CLK_SET1;
176 break;
177
178 case 8000: case 10666: case 16000: /* 10: 4.096 MHz */
179 GPCR = GPIO_H3600_CLK_SET0;
180 GPSR = GPIO_H3600_CLK_SET1;
181 break;
182
183 case 10985: case 14647: case 21970: /* 11: 5.6245 MHz */
184 GPSR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
185 break;
186 }
187}
188
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100189static void sa11xx_uda1341_set_samplerate(struct sa11xx_uda1341 *sa11xx_uda1341, long rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int clk_div = 0;
192 int clk=0;
193
194 /* We don't want to mess with clocks when frames are in flight */
195 Ser4SSCR0 &= ~SSCR0_SSE;
196 /* wait for any frame to complete */
197 udelay(125);
198
199 /*
200 * We have the following clock sources:
201 * 4.096 MHz, 5.6245 MHz, 11.2896 MHz, 12.288 MHz
202 * Those can be divided either by 256, 384 or 512.
203 * This makes up 12 combinations for the following samplerates...
204 */
205 if (rate >= 48000)
206 rate = 48000;
207 else if (rate >= 44100)
208 rate = 44100;
209 else if (rate >= 32000)
210 rate = 32000;
211 else if (rate >= 29400)
212 rate = 29400;
213 else if (rate >= 24000)
214 rate = 24000;
215 else if (rate >= 22050)
216 rate = 22050;
217 else if (rate >= 21970)
218 rate = 21970;
219 else if (rate >= 16000)
220 rate = 16000;
221 else if (rate >= 14647)
222 rate = 14647;
223 else if (rate >= 10985)
224 rate = 10985;
225 else if (rate >= 10666)
226 rate = 10666;
227 else
228 rate = 8000;
229
230 /* Set the external clock generator */
Michal Piotrowski350a29b2007-08-02 14:26:43 +0200231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sa11xx_uda1341_set_audio_clock(rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* Select the clock divisor */
235 switch (rate) {
236 case 8000:
237 case 10985:
238 case 22050:
239 case 24000:
240 clk = F512;
241 clk_div = SSCR0_SerClkDiv(16);
242 break;
243 case 16000:
244 case 21970:
245 case 44100:
246 case 48000:
247 clk = F256;
248 clk_div = SSCR0_SerClkDiv(8);
249 break;
250 case 10666:
251 case 14647:
252 case 29400:
253 case 32000:
254 clk = F384;
255 clk_div = SSCR0_SerClkDiv(12);
256 break;
257 }
258
259 /* FMT setting should be moved away when other FMTs are added (FIXME) */
260 l3_command(sa11xx_uda1341->uda1341, CMD_FORMAT, (void *)LSB16);
261
262 l3_command(sa11xx_uda1341->uda1341, CMD_FS, (void *)clk);
263 Ser4SSCR0 = (Ser4SSCR0 & ~0xff00) + clk_div + SSCR0_SSE;
264 sa11xx_uda1341->samplerate = rate;
265}
266
267/* }}} */
268
269/* {{{ HW init and shutdown */
270
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100271static void sa11xx_uda1341_audio_init(struct sa11xx_uda1341 *sa11xx_uda1341)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 unsigned long flags;
274
275 /* Setup DMA stuff */
276 sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].id = "UDA1341 out";
277 sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].stream_id = SNDRV_PCM_STREAM_PLAYBACK;
278 sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].dma_dev = DMA_Ser4SSPWr;
279
280 sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].id = "UDA1341 in";
281 sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].stream_id = SNDRV_PCM_STREAM_CAPTURE;
282 sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].dma_dev = DMA_Ser4SSPRd;
283
284 /* Initialize the UDA1341 internal state */
285
286 /* Setup the uarts */
287 local_irq_save(flags);
288 GAFR |= (GPIO_SSP_CLK);
289 GPDR &= ~(GPIO_SSP_CLK);
290 Ser4SSCR0 = 0;
291 Ser4SSCR0 = SSCR0_DataSize(16) + SSCR0_TI + SSCR0_SerClkDiv(8);
292 Ser4SSCR1 = SSCR1_SClkIactL + SSCR1_SClk1P + SSCR1_ExtClk;
293 Ser4SSCR0 |= SSCR0_SSE;
294 local_irq_restore(flags);
295
296 /* Enable the audio power */
Michal Piotrowski350a29b2007-08-02 14:26:43 +0200297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
299 set_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON);
300 set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /* Wait for the UDA1341 to wake up */
303 mdelay(1); //FIXME - was removed by Perex - Why?
304
305 /* Initialize the UDA1341 internal state */
306 l3_open(sa11xx_uda1341->uda1341);
307
308 /* external clock configuration (after l3_open - regs must be initialized */
309 sa11xx_uda1341_set_samplerate(sa11xx_uda1341, sa11xx_uda1341->samplerate);
310
311 /* Wait for the UDA1341 to wake up */
312 set_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
313 mdelay(1);
314
315 /* make the left and right channels unswapped (flip the WS latch) */
316 Ser4SSDR = 0;
317
Michal Piotrowski350a29b2007-08-02 14:26:43 +0200318 clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100321static void sa11xx_uda1341_audio_shutdown(struct sa11xx_uda1341 *sa11xx_uda1341)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 /* mute on */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* disable the audio power and all signals leading to the audio chip */
327 l3_close(sa11xx_uda1341->uda1341);
328 Ser4SSCR0 = 0;
329 clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
330
331 /* power off and mute off */
332 /* FIXME - is muting off necesary??? */
Michal Piotrowski350a29b2007-08-02 14:26:43 +0200333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON);
335 clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/* }}} */
339
340/* {{{ DMA staff */
341
342/*
343 * these are the address and sizes used to fill the xmit buffer
344 * so we can get a clock in record only mode
345 */
346#define FORCE_CLOCK_ADDR (dma_addr_t)FLUSH_BASE_PHYS
347#define FORCE_CLOCK_SIZE 4096 // was 2048
348
349// FIXME Why this value exactly - wrote comment
350#define DMA_BUF_SIZE 8176 /* <= MAX_DMA_SIZE from asm/arch-sa1100/dma.h */
351
352#ifdef HH_VERSION
353
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100354static int audio_dma_request(struct audio_stream *s, void (*callback)(void *, int))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 int ret;
357
358 ret = sa1100_request_dma(&s->dmach, s->id, s->dma_dev);
359 if (ret < 0) {
360 printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev);
361 return ret;
362 }
363 sa1100_dma_set_callback(s->dmach, callback);
364 return 0;
365}
366
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100367static inline void audio_dma_free(struct audio_stream *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 sa1100_free_dma(s->dmach);
370 s->dmach = -1;
371}
372
373#else
374
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100375static int audio_dma_request(struct audio_stream *s, void (*callback)(void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 int ret;
378
379 ret = sa1100_request_dma(s->dma_dev, s->id, callback, s, &s->dma_regs);
380 if (ret < 0)
381 printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev);
382 return ret;
383}
384
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100385static void audio_dma_free(struct audio_stream *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100387 sa1100_free_dma(s->dma_regs);
388 s->dma_regs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391#endif
392
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100393static u_int audio_get_dma_pos(struct audio_stream *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100395 struct snd_pcm_substream *substream = s->stream;
396 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 unsigned int offset;
398 unsigned long flags;
399 dma_addr_t addr;
400
401 // this must be called w/ interrupts locked out see dma-sa1100.c in the kernel
402 spin_lock_irqsave(&s->dma_lock, flags);
403#ifdef HH_VERSION
404 sa1100_dma_get_current(s->dmach, NULL, &addr);
405#else
406 addr = sa1100_get_dma_pos((s)->dma_regs);
407#endif
408 offset = addr - runtime->dma_addr;
409 spin_unlock_irqrestore(&s->dma_lock, flags);
410
411 offset = bytes_to_frames(runtime,offset);
412 if (offset >= runtime->buffer_size)
413 offset = 0;
414
415 return offset;
416}
417
418/*
419 * this stops the dma and clears the dma ptrs
420 */
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100421static void audio_stop_dma(struct audio_stream *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 unsigned long flags;
424
425 spin_lock_irqsave(&s->dma_lock, flags);
426 s->active = 0;
427 s->period = 0;
428 /* this stops the dma channel and clears the buffer ptrs */
429#ifdef HH_VERSION
430 sa1100_dma_flush_all(s->dmach);
431#else
432 sa1100_clear_dma(s->dma_regs);
433#endif
434 spin_unlock_irqrestore(&s->dma_lock, flags);
435}
436
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100437static void audio_process_dma(struct audio_stream *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100439 struct snd_pcm_substream *substream = s->stream;
440 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned int dma_size;
442 unsigned int offset;
443 int ret;
444
445 /* we are requested to process synchronization DMA transfer */
446 if (s->tx_spin) {
447 snd_assert(s->stream_id == SNDRV_PCM_STREAM_PLAYBACK, return);
448 /* fill the xmit dma buffers and return */
449#ifdef HH_VERSION
450 sa1100_dma_set_spin(s->dmach, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE);
451#else
452 while (1) {
453 ret = sa1100_start_dma(s->dma_regs, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE);
454 if (ret)
455 return;
456 }
457#endif
458 return;
459 }
460
461 /* must be set here - only valid for running streams, not for forced_clock dma fills */
462 runtime = substream->runtime;
463 while (s->active && s->periods < runtime->periods) {
464 dma_size = frames_to_bytes(runtime, runtime->period_size);
465 if (s->old_offset) {
466 /* a little trick, we need resume from old position */
467 offset = frames_to_bytes(runtime, s->old_offset - 1);
468 s->old_offset = 0;
469 s->periods = 0;
470 s->period = offset / dma_size;
471 offset %= dma_size;
472 dma_size = dma_size - offset;
473 if (!dma_size)
474 continue; /* special case */
475 } else {
476 offset = dma_size * s->period;
477 snd_assert(dma_size <= DMA_BUF_SIZE, );
478 }
479#ifdef HH_VERSION
480 ret = sa1100_dma_queue_buffer(s->dmach, s, runtime->dma_addr + offset, dma_size);
481 if (ret)
482 return; //FIXME
483#else
484 ret = sa1100_start_dma((s)->dma_regs, runtime->dma_addr + offset, dma_size);
485 if (ret) {
486 printk(KERN_ERR "audio_process_dma: cannot queue DMA buffer (%i)\n", ret);
487 return;
488 }
489#endif
490
491 s->period++;
492 s->period %= runtime->periods;
493 s->periods++;
494 }
495}
496
497#ifdef HH_VERSION
498static void audio_dma_callback(void *data, int size)
499#else
500static void audio_dma_callback(void *data)
501#endif
502{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100503 struct audio_stream *s = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /*
506 * If we are getting a callback for an active stream then we inform
507 * the PCM middle layer we've finished a period
508 */
509 if (s->active)
510 snd_pcm_period_elapsed(s->stream);
511
512 spin_lock(&s->dma_lock);
513 if (!s->tx_spin && s->periods > 0)
514 s->periods--;
515 audio_process_dma(s);
516 spin_unlock(&s->dma_lock);
517}
518
519/* }}} */
520
521/* {{{ PCM setting */
522
523/* {{{ trigger & timer */
524
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100525static int snd_sa11xx_uda1341_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100527 struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int stream_id = substream->pstr->stream;
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100529 struct audio_stream *s = &chip->s[stream_id];
530 struct audio_stream *s1 = &chip->s[stream_id ^ 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int err = 0;
532
533 /* note local interrupts are already disabled in the midlevel code */
534 spin_lock(&s->dma_lock);
535 switch (cmd) {
536 case SNDRV_PCM_TRIGGER_START:
537 /* now we need to make sure a record only stream has a clock */
538 if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
539 /* we need to force fill the xmit DMA with zeros */
540 s1->tx_spin = 1;
541 audio_process_dma(s1);
542 }
543 /* this case is when you were recording then you turn on a
544 * playback stream so we stop (also clears it) the dma first,
545 * clear the sync flag and then we let it turned on
546 */
547 else {
548 s->tx_spin = 0;
549 }
550
551 /* requested stream startup */
552 s->active = 1;
553 audio_process_dma(s);
554 break;
555 case SNDRV_PCM_TRIGGER_STOP:
556 /* requested stream shutdown */
557 audio_stop_dma(s);
558
559 /*
560 * now we need to make sure a record only stream has a clock
561 * so if we're stopping a playback with an active capture
562 * we need to turn the 0 fill dma on for the xmit side
563 */
564 if (stream_id == SNDRV_PCM_STREAM_PLAYBACK && s1->active) {
565 /* we need to force fill the xmit DMA with zeros */
566 s->tx_spin = 1;
567 audio_process_dma(s);
568 }
569 /*
570 * we killed a capture only stream, so we should also kill
571 * the zero fill transmit
572 */
573 else {
574 if (s1->tx_spin) {
575 s1->tx_spin = 0;
576 audio_stop_dma(s1);
577 }
578 }
579
580 break;
581 case SNDRV_PCM_TRIGGER_SUSPEND:
582 s->active = 0;
583#ifdef HH_VERSION
584 sa1100_dma_stop(s->dmach);
585#else
586 //FIXME - DMA API
587#endif
588 s->old_offset = audio_get_dma_pos(s) + 1;
589#ifdef HH_VERSION
590 sa1100_dma_flush_all(s->dmach);
591#else
592 //FIXME - DMA API
593#endif
594 s->periods = 0;
595 break;
596 case SNDRV_PCM_TRIGGER_RESUME:
597 s->active = 1;
598 s->tx_spin = 0;
599 audio_process_dma(s);
600 if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
601 s1->tx_spin = 1;
602 audio_process_dma(s1);
603 }
604 break;
605 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
606#ifdef HH_VERSION
607 sa1100_dma_stop(s->dmach);
608#else
609 //FIXME - DMA API
610#endif
611 s->active = 0;
612 if (stream_id == SNDRV_PCM_STREAM_PLAYBACK) {
613 if (s1->active) {
614 s->tx_spin = 1;
615 s->old_offset = audio_get_dma_pos(s) + 1;
616#ifdef HH_VERSION
617 sa1100_dma_flush_all(s->dmach);
618#else
619 //FIXME - DMA API
620#endif
621 audio_process_dma(s);
622 }
623 } else {
624 if (s1->tx_spin) {
625 s1->tx_spin = 0;
626#ifdef HH_VERSION
627 sa1100_dma_flush_all(s1->dmach);
628#else
629 //FIXME - DMA API
630#endif
631 }
632 }
633 break;
634 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
635 s->active = 1;
636 if (s->old_offset) {
637 s->tx_spin = 0;
638 audio_process_dma(s);
639 break;
640 }
641 if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
642 s1->tx_spin = 1;
643 audio_process_dma(s1);
644 }
645#ifdef HH_VERSION
646 sa1100_dma_resume(s->dmach);
647#else
648 //FIXME - DMA API
649#endif
650 break;
651 default:
652 err = -EINVAL;
653 break;
654 }
655 spin_unlock(&s->dma_lock);
656 return err;
657}
658
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100659static int snd_sa11xx_uda1341_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100661 struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
662 struct snd_pcm_runtime *runtime = substream->runtime;
663 struct audio_stream *s = &chip->s[substream->pstr->stream];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /* set requested samplerate */
666 sa11xx_uda1341_set_samplerate(chip, runtime->rate);
667
668 /* set requestd format when available */
669 /* set FMT here !!! FIXME */
670
671 s->period = 0;
672 s->periods = 0;
673
674 return 0;
675}
676
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100677static snd_pcm_uframes_t snd_sa11xx_uda1341_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100679 struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return audio_get_dma_pos(&chip->s[substream->pstr->stream]);
681}
682
683/* }}} */
684
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100685static struct snd_pcm_hardware snd_sa11xx_uda1341_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 .info = (SNDRV_PCM_INFO_INTERLEAVED |
688 SNDRV_PCM_INFO_BLOCK_TRANSFER |
689 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
690 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
691 .formats = SNDRV_PCM_FMTBIT_S16_LE,
692 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
693 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\
694 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
695 SNDRV_PCM_RATE_KNOT),
696 .rate_min = 8000,
697 .rate_max = 48000,
698 .channels_min = 2,
699 .channels_max = 2,
700 .buffer_bytes_max = 64*1024,
701 .period_bytes_min = 64,
702 .period_bytes_max = DMA_BUF_SIZE,
703 .periods_min = 2,
704 .periods_max = 255,
705 .fifo_size = 0,
706};
707
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100708static struct snd_pcm_hardware snd_sa11xx_uda1341_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 .info = (SNDRV_PCM_INFO_INTERLEAVED |
711 SNDRV_PCM_INFO_BLOCK_TRANSFER |
712 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
713 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
714 .formats = SNDRV_PCM_FMTBIT_S16_LE,
715 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
716 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\
717 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
718 SNDRV_PCM_RATE_KNOT),
719 .rate_min = 8000,
720 .rate_max = 48000,
721 .channels_min = 2,
722 .channels_max = 2,
723 .buffer_bytes_max = 64*1024,
724 .period_bytes_min = 64,
725 .period_bytes_max = DMA_BUF_SIZE,
726 .periods_min = 2,
727 .periods_max = 255,
728 .fifo_size = 0,
729};
730
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100731static int snd_card_sa11xx_uda1341_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100733 struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
734 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int stream_id = substream->pstr->stream;
736 int err;
737
738 chip->s[stream_id].stream = substream;
739
740 if (stream_id == SNDRV_PCM_STREAM_PLAYBACK)
741 runtime->hw = snd_sa11xx_uda1341_playback;
742 else
743 runtime->hw = snd_sa11xx_uda1341_capture;
744 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
745 return err;
746 if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0)
747 return err;
748
749 return 0;
750}
751
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100752static int snd_card_sa11xx_uda1341_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100754 struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 chip->s[substream->pstr->stream].stream = NULL;
757 return 0;
758}
759
760/* {{{ HW params & free */
761
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100762static int snd_sa11xx_uda1341_hw_params(struct snd_pcm_substream *substream,
763 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765
766 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
767}
768
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100769static int snd_sa11xx_uda1341_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 return snd_pcm_lib_free_pages(substream);
772}
773
774/* }}} */
775
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100776static struct snd_pcm_ops snd_card_sa11xx_uda1341_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 .open = snd_card_sa11xx_uda1341_open,
778 .close = snd_card_sa11xx_uda1341_close,
779 .ioctl = snd_pcm_lib_ioctl,
780 .hw_params = snd_sa11xx_uda1341_hw_params,
781 .hw_free = snd_sa11xx_uda1341_hw_free,
782 .prepare = snd_sa11xx_uda1341_prepare,
783 .trigger = snd_sa11xx_uda1341_trigger,
784 .pointer = snd_sa11xx_uda1341_pointer,
785};
786
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100787static struct snd_pcm_ops snd_card_sa11xx_uda1341_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .open = snd_card_sa11xx_uda1341_open,
789 .close = snd_card_sa11xx_uda1341_close,
790 .ioctl = snd_pcm_lib_ioctl,
791 .hw_params = snd_sa11xx_uda1341_hw_params,
792 .hw_free = snd_sa11xx_uda1341_hw_free,
793 .prepare = snd_sa11xx_uda1341_prepare,
794 .trigger = snd_sa11xx_uda1341_trigger,
795 .pointer = snd_sa11xx_uda1341_pointer,
796};
797
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100798static int __init snd_card_sa11xx_uda1341_pcm(struct sa11xx_uda1341 *sa11xx_uda1341, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100800 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int err;
802
803 if ((err = snd_pcm_new(sa11xx_uda1341->card, "UDA1341 PCM", device, 1, 1, &pcm)) < 0)
804 return err;
805
806 /*
807 * this sets up our initial buffers and sets the dma_type to isa.
808 * isa works but I'm not sure why (or if) it's the right choice
809 * this may be too large, trying it for now
810 */
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100811 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
812 snd_dma_isa_data(),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 64*1024, 64*1024);
814
815 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_sa11xx_uda1341_playback_ops);
816 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_sa11xx_uda1341_capture_ops);
817 pcm->private_data = sa11xx_uda1341;
818 pcm->info_flags = 0;
819 strcpy(pcm->name, "UDA1341 PCM");
820
821 sa11xx_uda1341_audio_init(sa11xx_uda1341);
822
823 /* setup DMA controller */
824 audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK], audio_dma_callback);
825 audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE], audio_dma_callback);
826
827 sa11xx_uda1341->pcm = pcm;
828
829 return 0;
830}
831
832/* }}} */
833
834/* {{{ module init & exit */
835
836#ifdef CONFIG_PM
837
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100838static int snd_sa11xx_uda1341_suspend(struct platform_device *devptr,
839 pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100841 struct snd_card *card = platform_get_drvdata(devptr);
842 struct sa11xx_uda1341 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100844 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 snd_pcm_suspend_all(chip->pcm);
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100846#ifdef HH_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach);
848 sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach);
849#else
850 //FIXME
851#endif
852 l3_command(chip->uda1341, CMD_SUSPEND, NULL);
853 sa11xx_uda1341_audio_shutdown(chip);
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return 0;
856}
857
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100858static int snd_sa11xx_uda1341_resume(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100860 struct snd_card *card = platform_get_drvdata(devptr);
861 struct sa11xx_uda1341 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 sa11xx_uda1341_audio_init(chip);
864 l3_command(chip->uda1341, CMD_RESUME, NULL);
865#ifdef HH_VERSION
866 sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach);
867 sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach);
868#else
869 //FIXME
870#endif
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100871 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 0;
873}
874#endif /* COMFIG_PM */
875
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100876void snd_sa11xx_uda1341_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100878 struct sa11xx_uda1341 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 audio_dma_free(&chip->s[SNDRV_PCM_STREAM_PLAYBACK]);
881 audio_dma_free(&chip->s[SNDRV_PCM_STREAM_CAPTURE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100884static int __init sa11xx_uda1341_probe(struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int err;
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100887 struct snd_card *card;
888 struct sa11xx_uda1341 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* register the soundcard */
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100891 card = snd_card_new(-1, id, THIS_MODULE, sizeof(struct sa11xx_uda1341));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (card == NULL)
893 return -ENOMEM;
894
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100895 chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 spin_lock_init(&chip->s[0].dma_lock);
897 spin_lock_init(&chip->s[1].dma_lock);
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100898
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100899 card->private_free = snd_sa11xx_uda1341_free;
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100900 chip->card = card;
901 chip->samplerate = AUDIO_RATE_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 // mixer
Takashi Iwaiaf0fbfb2005-11-17 15:10:58 +0100904 if ((err = snd_chip_uda1341_mixer_new(card, &chip->uda1341)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto nodev;
906
907 // PCM
Takashi Iwai0948e3c2005-11-17 10:25:22 +0100908 if ((err = snd_card_sa11xx_uda1341_pcm(chip, 0)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto nodev;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 strcpy(card->driver, "UDA1341");
912 strcpy(card->shortname, "H3600 UDA1341TS");
913 sprintf(card->longname, "Compaq iPAQ H3600 with Philips UDA1341TS");
914
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100915 snd_card_set_dev(card, &devptr->dev);
Takashi Iwai16dab542005-09-05 17:17:58 +0200916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if ((err = snd_card_register(card)) == 0) {
918 printk( KERN_INFO "iPAQ audio support initialized\n" );
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100919 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921 }
922
923 nodev:
924 snd_card_free(card);
925 return err;
926}
927
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100928static int __devexit sa11xx_uda1341_remove(struct platform_device *devptr)
929{
930 snd_card_free(platform_get_drvdata(devptr));
931 platform_set_drvdata(devptr, NULL);
932 return 0;
933}
934
935#define SA11XX_UDA1341_DRIVER "sa11xx_uda1341"
936
937static struct platform_driver sa11xx_uda1341_driver = {
938 .probe = sa11xx_uda1341_probe,
939 .remove = __devexit_p(sa11xx_uda1341_remove),
940#ifdef CONFIG_PM
941 .suspend = snd_sa11xx_uda1341_suspend,
942 .resume = snd_sa11xx_uda1341_resume,
943#endif
944 .driver = {
945 .name = SA11XX_UDA1341_DRIVER,
946 },
947};
948
949static int __init sa11xx_uda1341_init(void)
950{
951 int err;
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100952
953 if (!machine_is_h3xxx())
954 return -ENODEV;
955 if ((err = platform_driver_register(&sa11xx_uda1341_driver)) < 0)
956 return err;
957 device = platform_device_register_simple(SA11XX_UDA1341_DRIVER, -1, NULL, 0);
Rene Herman71524472006-04-13 12:58:06 +0200958 if (!IS_ERR(device)) {
959 if (platform_get_drvdata(device))
960 return 0;
961 platform_device_unregister(device);
Yoann Padioleau632155e2007-06-01 00:46:35 -0700962 err = -ENODEV;
Rene Herman71524472006-04-13 12:58:06 +0200963 } else
964 err = PTR_ERR(device);
965 platform_driver_unregister(&sa11xx_uda1341_driver);
966 return err;
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969static void __exit sa11xx_uda1341_exit(void)
970{
Clemens Ladischf7a92752005-12-07 09:13:42 +0100971 platform_device_unregister(device);
Takashi Iwai2b3f5582005-11-17 17:19:50 +0100972 platform_driver_unregister(&sa11xx_uda1341_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975module_init(sa11xx_uda1341_init);
976module_exit(sa11xx_uda1341_exit);
977
978/* }}} */
979
980/*
981 * Local variables:
982 * indent-tabs-mode: t
983 * End:
984 */