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