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