]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/arm/sa11xx-uda1341.c
[ALSA] Remove xxx_t typedefs: ARM SA11xx-UDA1341
[linux-2.6-omap-h63xx.git] / sound / arm / sa11xx-uda1341.c
1 /*
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
24 /* $Id: sa11xx-uda1341.c,v 1.25 2005/11/17 15:10:58 tiwai Exp $ */
25
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
62 #include <linux/config.h>
63 #include <sound/driver.h>
64 #include <linux/module.h>
65 #include <linux/moduleparam.h>
66 #include <linux/init.h>
67 #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
81 #ifdef CONFIG_H3600_HAL
82 #include <asm/semaphore.h>
83 #include <asm/uaccess.h>
84 #include <asm/arch/h3600_hal.h>
85 #endif
86
87 #include <sound/core.h>
88 #include <sound/pcm.h>
89 #include <sound/initval.h>
90
91 #include <linux/l3/l3.h>
92
93 #undef DEBUG_MODE
94 #undef DEBUG_FUNCTION_NAMES
95 #include <sound/uda1341.h>
96
97 /*
98  * FIXME: Is this enough as autodetection of 2.4.X-rmkY-hhZ kernels?
99  * We use DMA stuff from 2.4.18-rmk3-hh24 here to be able to compile this
100  * module for Familiar 0.6.1
101  */
102 #ifdef CONFIG_H3600_HAL
103 #define HH_VERSION 1
104 #endif
105
106 /* {{{ Type definitions */
107
108 MODULE_AUTHOR("Tomas Kasparek <tomas.kasparek@seznam.cz>");
109 MODULE_LICENSE("GPL");
110 MODULE_DESCRIPTION("SA1100/SA1111 + UDA1341TS driver for ALSA");
111 MODULE_SUPPORTED_DEVICE("{{UDA1341,iPAQ H3600 UDA1341TS}}");
112
113 static char *id = NULL; /* ID for this card */
114
115 module_param(id, charp, 0444);
116 MODULE_PARM_DESC(id, "ID string for SA1100/SA1111 + UDA1341TS soundcard.");
117
118 struct audio_stream {
119         char *id;               /* identification string */
120         int stream_id;          /* numeric identification */    
121         dma_device_t dma_dev;   /* device identifier for DMA */
122 #ifdef HH_VERSION
123         dmach_t dmach;          /* dma channel identification */
124 #else
125         dma_regs_t *dma_regs;   /* points to our DMA registers */
126 #endif
127         int active:1;           /* we are using this stream for transfer now */
128         int period;             /* current transfer period */
129         int periods;            /* current count of periods registerd in the DMA engine */
130         int tx_spin;            /* are we recoding - flag used to do DMA trans. for sync */
131         unsigned int old_offset;
132         spinlock_t dma_lock;    /* for locking in DMA operations (see dma-sa1100.c in the kernel) */
133         struct snd_pcm_substream *stream;
134 };
135
136 struct sa11xx_uda1341 {
137         struct snd_card *card;
138         struct l3_client *uda1341;
139         struct snd_pcm *pcm;
140         long samplerate;
141         struct audio_stream s[2];       /* playback & capture */
142 };
143
144 static unsigned int rates[] = {
145         8000,  10666, 10985, 14647,
146         16000, 21970, 22050, 24000,
147         29400, 32000, 44100, 48000,
148 };
149
150 static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
151         .count  = ARRAY_SIZE(rates),
152         .list   = rates,
153         .mask   = 0,
154 };
155
156 /* }}} */
157
158 /* {{{ Clock and sample rate stuff */
159
160 /*
161  * Stop-gap solution until rest of hh.org HAL stuff is merged.
162  */
163 #define GPIO_H3600_CLK_SET0             GPIO_GPIO (12)
164 #define GPIO_H3600_CLK_SET1             GPIO_GPIO (13)
165
166 #ifdef CONFIG_SA1100_H3XXX
167 #define clr_sa11xx_uda1341_egpio(x)     clr_h3600_egpio(x)
168 #define set_sa11xx_uda1341_egpio(x)     set_h3600_egpio(x)
169 #else
170 #error This driver could serve H3x00 handhelds only!
171 #endif
172
173 static void sa11xx_uda1341_set_audio_clock(long val)
174 {
175         switch (val) {
176         case 24000: case 32000: case 48000:     /* 00: 12.288 MHz */
177                 GPCR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
178                 break;
179
180         case 22050: case 29400: case 44100:     /* 01: 11.2896 MHz */
181                 GPSR = GPIO_H3600_CLK_SET0;
182                 GPCR = GPIO_H3600_CLK_SET1;
183                 break;
184
185         case 8000: case 10666: case 16000:      /* 10: 4.096 MHz */
186                 GPCR = GPIO_H3600_CLK_SET0;
187                 GPSR = GPIO_H3600_CLK_SET1;
188                 break;
189
190         case 10985: case 14647: case 21970:     /* 11: 5.6245 MHz */
191                 GPSR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
192                 break;
193         }
194 }
195
196 static void sa11xx_uda1341_set_samplerate(struct sa11xx_uda1341 *sa11xx_uda1341, long rate)
197 {
198         int clk_div = 0;
199         int clk=0;
200
201         /* We don't want to mess with clocks when frames are in flight */
202         Ser4SSCR0 &= ~SSCR0_SSE;
203         /* wait for any frame to complete */
204         udelay(125);
205
206         /*
207          * We have the following clock sources:
208          * 4.096 MHz, 5.6245 MHz, 11.2896 MHz, 12.288 MHz
209          * Those can be divided either by 256, 384 or 512.
210          * This makes up 12 combinations for the following samplerates...
211          */
212         if (rate >= 48000)
213                 rate = 48000;
214         else if (rate >= 44100)
215                 rate = 44100;
216         else if (rate >= 32000)
217                 rate = 32000;
218         else if (rate >= 29400)
219                 rate = 29400;
220         else if (rate >= 24000)
221                 rate = 24000;
222         else if (rate >= 22050)
223                 rate = 22050;
224         else if (rate >= 21970)
225                 rate = 21970;
226         else if (rate >= 16000)
227                 rate = 16000;
228         else if (rate >= 14647)
229                 rate = 14647;
230         else if (rate >= 10985)
231                 rate = 10985;
232         else if (rate >= 10666)
233                 rate = 10666;
234         else
235                 rate = 8000;
236
237         /* Set the external clock generator */
238 #ifdef CONFIG_H3600_HAL
239         h3600_audio_clock(rate);
240 #else   
241         sa11xx_uda1341_set_audio_clock(rate);
242 #endif
243
244         /* Select the clock divisor */
245         switch (rate) {
246         case 8000:
247         case 10985:
248         case 22050:
249         case 24000:
250                 clk = F512;
251                 clk_div = SSCR0_SerClkDiv(16);
252                 break;
253         case 16000:
254         case 21970:
255         case 44100:
256         case 48000:
257                 clk = F256;
258                 clk_div = SSCR0_SerClkDiv(8);
259                 break;
260         case 10666:
261         case 14647:
262         case 29400:
263         case 32000:
264                 clk = F384;
265                 clk_div = SSCR0_SerClkDiv(12);
266                 break;
267         }
268
269         /* FMT setting should be moved away when other FMTs are added (FIXME) */
270         l3_command(sa11xx_uda1341->uda1341, CMD_FORMAT, (void *)LSB16);
271         
272         l3_command(sa11xx_uda1341->uda1341, CMD_FS, (void *)clk);        
273         Ser4SSCR0 = (Ser4SSCR0 & ~0xff00) + clk_div + SSCR0_SSE;
274         sa11xx_uda1341->samplerate = rate;
275 }
276
277 /* }}} */
278
279 /* {{{ HW init and shutdown */
280
281 static void sa11xx_uda1341_audio_init(struct sa11xx_uda1341 *sa11xx_uda1341)
282 {
283         unsigned long flags;
284
285         /* Setup DMA stuff */
286         sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].id = "UDA1341 out";
287         sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].stream_id = SNDRV_PCM_STREAM_PLAYBACK;
288         sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].dma_dev = DMA_Ser4SSPWr;
289
290         sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].id = "UDA1341 in";
291         sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].stream_id = SNDRV_PCM_STREAM_CAPTURE;
292         sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].dma_dev = DMA_Ser4SSPRd;
293
294         /* Initialize the UDA1341 internal state */
295        
296         /* Setup the uarts */
297         local_irq_save(flags);
298         GAFR |= (GPIO_SSP_CLK);
299         GPDR &= ~(GPIO_SSP_CLK);
300         Ser4SSCR0 = 0;
301         Ser4SSCR0 = SSCR0_DataSize(16) + SSCR0_TI + SSCR0_SerClkDiv(8);
302         Ser4SSCR1 = SSCR1_SClkIactL + SSCR1_SClk1P + SSCR1_ExtClk;
303         Ser4SSCR0 |= SSCR0_SSE;
304         local_irq_restore(flags);
305
306         /* Enable the audio power */
307 #ifdef CONFIG_H3600_HAL
308         h3600_audio_power(AUDIO_RATE_DEFAULT);
309 #else
310         clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
311         set_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON);
312         set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
313 #endif
314  
315         /* Wait for the UDA1341 to wake up */
316         mdelay(1); //FIXME - was removed by Perex - Why?
317
318         /* Initialize the UDA1341 internal state */
319         l3_open(sa11xx_uda1341->uda1341);
320         
321         /* external clock configuration (after l3_open - regs must be initialized */
322         sa11xx_uda1341_set_samplerate(sa11xx_uda1341, sa11xx_uda1341->samplerate);
323
324         /* Wait for the UDA1341 to wake up */
325         set_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
326         mdelay(1);      
327
328         /* make the left and right channels unswapped (flip the WS latch) */
329         Ser4SSDR = 0;
330
331 #ifdef CONFIG_H3600_HAL
332         h3600_audio_mute(0);
333 #else   
334         clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);        
335 #endif     
336 }
337
338 static void sa11xx_uda1341_audio_shutdown(struct sa11xx_uda1341 *sa11xx_uda1341)
339 {
340         /* mute on */
341 #ifdef CONFIG_H3600_HAL
342         h3600_audio_mute(1);
343 #else   
344         set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
345 #endif
346         
347         /* disable the audio power and all signals leading to the audio chip */
348         l3_close(sa11xx_uda1341->uda1341);
349         Ser4SSCR0 = 0;
350         clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
351
352         /* power off and mute off */
353         /* FIXME - is muting off necesary??? */
354 #ifdef CONFIG_H3600_HAL
355         h3600_audio_power(0);
356         h3600_audio_mute(0);
357 #else   
358         clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON);
359         clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
360 #endif  
361 }
362
363 /* }}} */
364
365 /* {{{ DMA staff */
366
367 /*
368  * these are the address and sizes used to fill the xmit buffer
369  * so we can get a clock in record only mode
370  */
371 #define FORCE_CLOCK_ADDR                (dma_addr_t)FLUSH_BASE_PHYS
372 #define FORCE_CLOCK_SIZE                4096 // was 2048
373
374 // FIXME Why this value exactly - wrote comment
375 #define DMA_BUF_SIZE    8176    /* <= MAX_DMA_SIZE from asm/arch-sa1100/dma.h */
376
377 #ifdef HH_VERSION
378
379 static int audio_dma_request(struct audio_stream *s, void (*callback)(void *, int))
380 {
381         int ret;
382
383         ret = sa1100_request_dma(&s->dmach, s->id, s->dma_dev);
384         if (ret < 0) {
385                 printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev);
386                 return ret;
387         }
388         sa1100_dma_set_callback(s->dmach, callback);
389         return 0;
390 }
391
392 static inline void audio_dma_free(struct audio_stream *s)
393 {
394         sa1100_free_dma(s->dmach);
395         s->dmach = -1;
396 }
397
398 #else
399
400 static int audio_dma_request(struct audio_stream *s, void (*callback)(void *))
401 {
402         int ret;
403
404         ret = sa1100_request_dma(s->dma_dev, s->id, callback, s, &s->dma_regs);
405         if (ret < 0)
406                 printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev);
407         return ret;
408 }
409
410 static void audio_dma_free(struct audio_stream *s)
411 {
412         sa1100_free_dma(s->dma_regs);
413         s->dma_regs = 0;
414 }
415
416 #endif
417
418 static u_int audio_get_dma_pos(struct audio_stream *s)
419 {
420         struct snd_pcm_substream *substream = s->stream;
421         struct snd_pcm_runtime *runtime = substream->runtime;
422         unsigned int offset;
423         unsigned long flags;
424         dma_addr_t addr;
425         
426         // this must be called w/ interrupts locked out see dma-sa1100.c in the kernel
427         spin_lock_irqsave(&s->dma_lock, flags);
428 #ifdef HH_VERSION       
429         sa1100_dma_get_current(s->dmach, NULL, &addr);
430 #else
431         addr = sa1100_get_dma_pos((s)->dma_regs);
432 #endif
433         offset = addr - runtime->dma_addr;
434         spin_unlock_irqrestore(&s->dma_lock, flags);
435         
436         offset = bytes_to_frames(runtime,offset);
437         if (offset >= runtime->buffer_size)
438                 offset = 0;
439
440         return offset;
441 }
442
443 /*
444  * this stops the dma and clears the dma ptrs
445  */
446 static void audio_stop_dma(struct audio_stream *s)
447 {
448         unsigned long flags;
449
450         spin_lock_irqsave(&s->dma_lock, flags); 
451         s->active = 0;
452         s->period = 0;
453         /* this stops the dma channel and clears the buffer ptrs */
454 #ifdef HH_VERSION
455         sa1100_dma_flush_all(s->dmach);
456 #else
457         sa1100_clear_dma(s->dma_regs);  
458 #endif
459         spin_unlock_irqrestore(&s->dma_lock, flags);
460 }
461
462 static void audio_process_dma(struct audio_stream *s)
463 {
464         struct snd_pcm_substream *substream = s->stream;
465         struct snd_pcm_runtime *runtime;
466         unsigned int dma_size;          
467         unsigned int offset;
468         int ret;
469                 
470         /* we are requested to process synchronization DMA transfer */
471         if (s->tx_spin) {
472                 snd_assert(s->stream_id == SNDRV_PCM_STREAM_PLAYBACK, return);
473                 /* fill the xmit dma buffers and return */
474 #ifdef HH_VERSION
475                 sa1100_dma_set_spin(s->dmach, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE);
476 #else
477                 while (1) {
478                         ret = sa1100_start_dma(s->dma_regs, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE);
479                         if (ret)
480                                 return;   
481                 }
482 #endif
483                 return;
484         }
485
486         /* must be set here - only valid for running streams, not for forced_clock dma fills  */
487         runtime = substream->runtime;
488         while (s->active && s->periods < runtime->periods) {
489                 dma_size = frames_to_bytes(runtime, runtime->period_size);
490                 if (s->old_offset) {
491                         /* a little trick, we need resume from old position */
492                         offset = frames_to_bytes(runtime, s->old_offset - 1);
493                         s->old_offset = 0;
494                         s->periods = 0;
495                         s->period = offset / dma_size;
496                         offset %= dma_size;
497                         dma_size = dma_size - offset;
498                         if (!dma_size)
499                                 continue;               /* special case */
500                 } else {
501                         offset = dma_size * s->period;
502                         snd_assert(dma_size <= DMA_BUF_SIZE, );
503                 }
504 #ifdef HH_VERSION
505                 ret = sa1100_dma_queue_buffer(s->dmach, s, runtime->dma_addr + offset, dma_size);
506                 if (ret)
507                         return; //FIXME
508 #else
509                 ret = sa1100_start_dma((s)->dma_regs, runtime->dma_addr + offset, dma_size);
510                 if (ret) {
511                         printk(KERN_ERR "audio_process_dma: cannot queue DMA buffer (%i)\n", ret);
512                         return;
513                 }
514 #endif
515
516                 s->period++;
517                 s->period %= runtime->periods;
518                 s->periods++;
519         }
520 }
521
522 #ifdef HH_VERSION
523 static void audio_dma_callback(void *data, int size)
524 #else
525 static void audio_dma_callback(void *data)
526 #endif
527 {
528         struct audio_stream *s = data;
529         
530         /* 
531          * If we are getting a callback for an active stream then we inform
532          * the PCM middle layer we've finished a period
533          */
534         if (s->active)
535                 snd_pcm_period_elapsed(s->stream);
536
537         spin_lock(&s->dma_lock);
538         if (!s->tx_spin && s->periods > 0)
539                 s->periods--;
540         audio_process_dma(s);
541         spin_unlock(&s->dma_lock);
542 }
543
544 /* }}} */
545
546 /* {{{ PCM setting */
547
548 /* {{{ trigger & timer */
549
550 static int snd_sa11xx_uda1341_trigger(struct snd_pcm_substream *substream, int cmd)
551 {
552         struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
553         int stream_id = substream->pstr->stream;
554         struct audio_stream *s = &chip->s[stream_id];
555         struct audio_stream *s1 = &chip->s[stream_id ^ 1];
556         int err = 0;
557
558         /* note local interrupts are already disabled in the midlevel code */
559         spin_lock(&s->dma_lock);
560         switch (cmd) {
561         case SNDRV_PCM_TRIGGER_START:
562                 /* now we need to make sure a record only stream has a clock */
563                 if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
564                         /* we need to force fill the xmit DMA with zeros */
565                         s1->tx_spin = 1;
566                         audio_process_dma(s1);
567                 }
568                 /* this case is when you were recording then you turn on a
569                  * playback stream so we stop (also clears it) the dma first,
570                  * clear the sync flag and then we let it turned on
571                  */             
572                 else {
573                         s->tx_spin = 0;
574                 }
575
576                 /* requested stream startup */
577                 s->active = 1;
578                 audio_process_dma(s);
579                 break;
580         case SNDRV_PCM_TRIGGER_STOP:
581                 /* requested stream shutdown */
582                 audio_stop_dma(s);
583                 
584                 /*
585                  * now we need to make sure a record only stream has a clock
586                  * so if we're stopping a playback with an active capture
587                  * we need to turn the 0 fill dma on for the xmit side
588                  */
589                 if (stream_id == SNDRV_PCM_STREAM_PLAYBACK && s1->active) {
590                         /* we need to force fill the xmit DMA with zeros */
591                         s->tx_spin = 1;
592                         audio_process_dma(s);
593                 }
594                 /*
595                  * we killed a capture only stream, so we should also kill
596                  * the zero fill transmit
597                  */
598                 else {
599                         if (s1->tx_spin) {
600                                 s1->tx_spin = 0;
601                                 audio_stop_dma(s1);
602                         }
603                 }
604                 
605                 break;
606         case SNDRV_PCM_TRIGGER_SUSPEND:
607                 s->active = 0;
608 #ifdef HH_VERSION               
609                 sa1100_dma_stop(s->dmach);
610 #else
611                 //FIXME - DMA API
612 #endif          
613                 s->old_offset = audio_get_dma_pos(s) + 1;
614 #ifdef HH_VERSION               
615                 sa1100_dma_flush_all(s->dmach);
616 #else
617                 //FIXME - DMA API
618 #endif          
619                 s->periods = 0;
620                 break;
621         case SNDRV_PCM_TRIGGER_RESUME:
622                 s->active = 1;
623                 s->tx_spin = 0;
624                 audio_process_dma(s);
625                 if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
626                         s1->tx_spin = 1;
627                         audio_process_dma(s1);
628                 }
629                 break;
630         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
631 #ifdef HH_VERSION               
632                 sa1100_dma_stop(s->dmach);
633 #else
634                 //FIXME - DMA API
635 #endif
636                 s->active = 0;
637                 if (stream_id == SNDRV_PCM_STREAM_PLAYBACK) {
638                         if (s1->active) {
639                                 s->tx_spin = 1;
640                                 s->old_offset = audio_get_dma_pos(s) + 1;
641 #ifdef HH_VERSION                               
642                                 sa1100_dma_flush_all(s->dmach);
643 #else
644                                 //FIXME - DMA API
645 #endif                          
646                                 audio_process_dma(s);
647                         }
648                 } else {
649                         if (s1->tx_spin) {
650                                 s1->tx_spin = 0;
651 #ifdef HH_VERSION                               
652                                 sa1100_dma_flush_all(s1->dmach);
653 #else
654                                 //FIXME - DMA API
655 #endif                          
656                         }
657                 }
658                 break;
659         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
660                 s->active = 1;
661                 if (s->old_offset) {
662                         s->tx_spin = 0;
663                         audio_process_dma(s);
664                         break;
665                 }
666                 if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
667                         s1->tx_spin = 1;
668                         audio_process_dma(s1);
669                 }
670 #ifdef HH_VERSION               
671                 sa1100_dma_resume(s->dmach);
672 #else
673                 //FIXME - DMA API
674 #endif
675                 break;
676         default:
677                 err = -EINVAL;
678                 break;
679         }
680         spin_unlock(&s->dma_lock);      
681         return err;
682 }
683
684 static int snd_sa11xx_uda1341_prepare(struct snd_pcm_substream *substream)
685 {
686         struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
687         struct snd_pcm_runtime *runtime = substream->runtime;
688         struct audio_stream *s = &chip->s[substream->pstr->stream];
689         
690         /* set requested samplerate */
691         sa11xx_uda1341_set_samplerate(chip, runtime->rate);
692
693         /* set requestd format when available */
694         /* set FMT here !!! FIXME */
695
696         s->period = 0;
697         s->periods = 0;
698         
699         return 0;
700 }
701
702 static snd_pcm_uframes_t snd_sa11xx_uda1341_pointer(struct snd_pcm_substream *substream)
703 {
704         struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
705         return audio_get_dma_pos(&chip->s[substream->pstr->stream]);
706 }
707
708 /* }}} */
709
710 static struct snd_pcm_hardware snd_sa11xx_uda1341_capture =
711 {
712         .info                   = (SNDRV_PCM_INFO_INTERLEAVED |
713                                    SNDRV_PCM_INFO_BLOCK_TRANSFER |
714                                    SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
715                                    SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
716         .formats                = SNDRV_PCM_FMTBIT_S16_LE,
717         .rates                  = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
718                                    SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\
719                                    SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
720                                    SNDRV_PCM_RATE_KNOT),
721         .rate_min               = 8000,
722         .rate_max               = 48000,
723         .channels_min           = 2,
724         .channels_max           = 2,
725         .buffer_bytes_max       = 64*1024,
726         .period_bytes_min       = 64,
727         .period_bytes_max       = DMA_BUF_SIZE,
728         .periods_min            = 2,
729         .periods_max            = 255,
730         .fifo_size              = 0,
731 };
732
733 static struct snd_pcm_hardware snd_sa11xx_uda1341_playback =
734 {
735         .info                   = (SNDRV_PCM_INFO_INTERLEAVED |
736                                    SNDRV_PCM_INFO_BLOCK_TRANSFER |
737                                    SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
738                                    SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
739         .formats                = SNDRV_PCM_FMTBIT_S16_LE,
740         .rates                  = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
741                                    SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\
742                                    SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
743                                    SNDRV_PCM_RATE_KNOT),
744         .rate_min               = 8000,
745         .rate_max               = 48000,
746         .channels_min           = 2,
747         .channels_max           = 2,
748         .buffer_bytes_max       = 64*1024,
749         .period_bytes_min       = 64,
750         .period_bytes_max       = DMA_BUF_SIZE,
751         .periods_min            = 2,
752         .periods_max            = 255,
753         .fifo_size              = 0,
754 };
755
756 static int snd_card_sa11xx_uda1341_open(struct snd_pcm_substream *substream)
757 {
758         struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
759         struct snd_pcm_runtime *runtime = substream->runtime;
760         int stream_id = substream->pstr->stream;
761         int err;
762
763         chip->s[stream_id].stream = substream;
764
765         if (stream_id == SNDRV_PCM_STREAM_PLAYBACK)
766                 runtime->hw = snd_sa11xx_uda1341_playback;
767         else
768                 runtime->hw = snd_sa11xx_uda1341_capture;
769         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
770                 return err;
771         if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0)
772                 return err;
773         
774         return 0;
775 }
776
777 static int snd_card_sa11xx_uda1341_close(struct snd_pcm_substream *substream)
778 {
779         struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
780
781         chip->s[substream->pstr->stream].stream = NULL;
782         return 0;
783 }
784
785 /* {{{ HW params & free */
786
787 static int snd_sa11xx_uda1341_hw_params(struct snd_pcm_substream *substream,
788                                         struct snd_pcm_hw_params *hw_params)
789 {
790         
791         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
792 }
793
794 static int snd_sa11xx_uda1341_hw_free(struct snd_pcm_substream *substream)
795 {
796         return snd_pcm_lib_free_pages(substream);
797 }
798
799 /* }}} */
800
801 static struct snd_pcm_ops snd_card_sa11xx_uda1341_playback_ops = {
802         .open                   = snd_card_sa11xx_uda1341_open,
803         .close                  = snd_card_sa11xx_uda1341_close,
804         .ioctl                  = snd_pcm_lib_ioctl,
805         .hw_params              = snd_sa11xx_uda1341_hw_params,
806         .hw_free                = snd_sa11xx_uda1341_hw_free,
807         .prepare                = snd_sa11xx_uda1341_prepare,
808         .trigger                = snd_sa11xx_uda1341_trigger,
809         .pointer                = snd_sa11xx_uda1341_pointer,
810 };
811
812 static struct snd_pcm_ops snd_card_sa11xx_uda1341_capture_ops = {
813         .open                   = snd_card_sa11xx_uda1341_open,
814         .close                  = snd_card_sa11xx_uda1341_close,
815         .ioctl                  = snd_pcm_lib_ioctl,
816         .hw_params              = snd_sa11xx_uda1341_hw_params,
817         .hw_free                = snd_sa11xx_uda1341_hw_free,
818         .prepare                = snd_sa11xx_uda1341_prepare,
819         .trigger                = snd_sa11xx_uda1341_trigger,
820         .pointer                = snd_sa11xx_uda1341_pointer,
821 };
822
823 static int __init snd_card_sa11xx_uda1341_pcm(struct sa11xx_uda1341 *sa11xx_uda1341, int device)
824 {
825         struct snd_pcm *pcm;
826         int err;
827
828         if ((err = snd_pcm_new(sa11xx_uda1341->card, "UDA1341 PCM", device, 1, 1, &pcm)) < 0)
829                 return err;
830
831         /*
832          * this sets up our initial buffers and sets the dma_type to isa.
833          * isa works but I'm not sure why (or if) it's the right choice
834          * this may be too large, trying it for now
835          */
836         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 
837                                               snd_dma_isa_data(),
838                                               64*1024, 64*1024);
839
840         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_sa11xx_uda1341_playback_ops);
841         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_sa11xx_uda1341_capture_ops);
842         pcm->private_data = sa11xx_uda1341;
843         pcm->info_flags = 0;
844         strcpy(pcm->name, "UDA1341 PCM");
845
846         sa11xx_uda1341_audio_init(sa11xx_uda1341);
847
848         /* setup DMA controller */
849         audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK], audio_dma_callback);
850         audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE], audio_dma_callback);
851
852         sa11xx_uda1341->pcm = pcm;
853
854         return 0;
855 }
856
857 /* }}} */
858
859 /* {{{ module init & exit */
860
861 #ifdef CONFIG_PM
862
863 static int snd_sa11xx_uda1341_suspend(struct snd_card *card, pm_message_t state)
864 {
865         struct sa11xx_uda1341 *chip = card->pm_private_data;
866
867         snd_pcm_suspend_all(chip->pcm);
868 #ifdef HH_VERSION       
869         sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach);
870         sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach);
871 #else
872         //FIXME
873 #endif
874         l3_command(chip->uda1341, CMD_SUSPEND, NULL);
875         sa11xx_uda1341_audio_shutdown(chip);
876         return 0;
877 }
878
879 static int snd_sa11xx_uda1341_resume(struct snd_card *card)
880 {
881         struct sa11xx_uda1341 *chip = card->pm_private_data;
882
883         sa11xx_uda1341_audio_init(chip);
884         l3_command(chip->uda1341, CMD_RESUME, NULL);
885 #ifdef HH_VERSION       
886         sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach);
887         sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach);
888 #else
889         //FIXME
890 #endif
891         return 0;
892 }
893 #endif /* COMFIG_PM */
894
895 void snd_sa11xx_uda1341_free(struct snd_card *card)
896 {
897         struct sa11xx_uda1341 *chip = card->private_data;
898
899         audio_dma_free(&chip->s[SNDRV_PCM_STREAM_PLAYBACK]);
900         audio_dma_free(&chip->s[SNDRV_PCM_STREAM_CAPTURE]);
901 }
902
903 static struct snd_card *sa11xx_uda1341_card;
904
905 static int __init sa11xx_uda1341_init(void)
906 {
907         int err;
908         struct snd_card *card;
909         struct sa11xx_uda1341 *chip;
910
911         if (!machine_is_h3xxx())
912                 return -ENODEV;
913
914         /* register the soundcard */
915         card = snd_card_new(-1, id, THIS_MODULE, sizeof(struct sa11xx_uda1341));
916         if (card == NULL)
917                 return -ENOMEM;
918
919         card->private_free = snd_sa11xx_uda1341_free;
920         chip = card->private_data;
921         spin_lock_init(&chip->s[0].dma_lock);
922         spin_lock_init(&chip->s[1].dma_lock);
923
924         chip->card = card;
925         chip->samplerate = AUDIO_RATE_DEFAULT;
926
927         // mixer
928         if ((err = snd_chip_uda1341_mixer_new(card, &chip->uda1341)))
929                 goto nodev;
930
931         // PCM
932         if ((err = snd_card_sa11xx_uda1341_pcm(chip, 0)) < 0)
933                 goto nodev;
934         
935         snd_card_set_generic_pm_callback(card,
936                                          snd_sa11xx_uda1341_suspend, snd_sa11_uda1341_resume,
937                                          chip);
938
939         strcpy(card->driver, "UDA1341");
940         strcpy(card->shortname, "H3600 UDA1341TS");
941         sprintf(card->longname, "Compaq iPAQ H3600 with Philips UDA1341TS");
942         
943         if ((err = snd_card_set_generic_dev(card)) < 0)
944                 goto nodev;
945
946         if ((err = snd_card_register(card)) == 0) {
947                 printk( KERN_INFO "iPAQ audio support initialized\n" );
948                 sa11xx_uda1341_card = card;
949                 return 0;
950         }
951         
952  nodev:
953         snd_card_free(card);
954         return err;
955 }
956
957 static void __exit sa11xx_uda1341_exit(void)
958 {
959         snd_card_free(sa11xx_uda1341_card);
960 }
961
962 module_init(sa11xx_uda1341_init);
963 module_exit(sa11xx_uda1341_exit);
964
965 /* }}} */
966
967 /*
968  * Local variables:
969  * indent-tabs-mode: t
970  * End:
971  */