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