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