1/*
2 * i2sbus driver -- pcm routines
3 *
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 */
8
9#include <asm/io.h>
10#include <linux/delay.h>
11/* So apparently there's a reason for requiring driver.h
12 * to be included first, even if I don't know it... */
13#include <sound/driver.h>
14#include <sound/core.h>
15#include <asm/macio.h>
16#include <linux/pci.h>
17#include "../soundbus.h"
18#include "i2sbus.h"
19
20static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in,
21				struct pcm_info **pi, struct pcm_info **other)
22{
23	if (in) {
24		if (pi)
25			*pi = &i2sdev->in;
26		if (other)
27			*other = &i2sdev->out;
28	} else {
29		if (pi)
30			*pi = &i2sdev->out;
31		if (other)
32			*other = &i2sdev->in;
33	}
34}
35
36static int clock_and_divisors(int mclk, int sclk, int rate, int *out)
37{
38	/* sclk must be derived from mclk! */
39	if (mclk % sclk)
40		return -1;
41	/* derive sclk register value */
42	if (i2s_sf_sclkdiv(mclk / sclk, out))
43		return -1;
44
45	if (I2S_CLOCK_SPEED_18MHz % (rate * mclk) == 0) {
46		if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz / (rate * mclk), out)) {
47			*out |= I2S_SF_CLOCK_SOURCE_18MHz;
48			return 0;
49		}
50	}
51	if (I2S_CLOCK_SPEED_45MHz % (rate * mclk) == 0) {
52		if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz / (rate * mclk), out)) {
53			*out |= I2S_SF_CLOCK_SOURCE_45MHz;
54			return 0;
55		}
56	}
57	if (I2S_CLOCK_SPEED_49MHz % (rate * mclk) == 0) {
58		if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz / (rate * mclk), out)) {
59			*out |= I2S_SF_CLOCK_SOURCE_49MHz;
60			return 0;
61		}
62	}
63	return -1;
64}
65
66#define CHECK_RATE(rate)						\
67	do { if (rates & SNDRV_PCM_RATE_ ##rate) {			\
68		int dummy;						\
69		if (clock_and_divisors(sysclock_factor,			\
70				       bus_factor, rate, &dummy))	\
71			rates &= ~SNDRV_PCM_RATE_ ##rate;		\
72	} } while (0)
73
74static int i2sbus_pcm_open(struct i2sbus_dev *i2sdev, int in)
75{
76	struct pcm_info *pi, *other;
77	struct soundbus_dev *sdev;
78	int masks_inited = 0, err;
79	struct codec_info_item *cii, *rev;
80	struct snd_pcm_hardware *hw;
81	u64 formats = 0;
82	unsigned int rates = 0;
83	struct transfer_info v;
84	int result = 0;
85	int bus_factor = 0, sysclock_factor = 0;
86	int found_this;
87
88	mutex_lock(&i2sdev->lock);
89
90	get_pcm_info(i2sdev, in, &pi, &other);
91
92	hw = &pi->substream->runtime->hw;
93	sdev = &i2sdev->sound;
94
95	if (pi->active) {
96		/* alsa messed up */
97		result = -EBUSY;
98		goto out_unlock;
99	}
100
101	/* we now need to assign the hw */
102	list_for_each_entry(cii, &sdev->codec_list, list) {
103		struct transfer_info *ti = cii->codec->transfers;
104		bus_factor = cii->codec->bus_factor;
105		sysclock_factor = cii->codec->sysclock_factor;
106		while (ti->formats && ti->rates) {
107			v = *ti;
108			if (ti->transfer_in == in
109			    && cii->codec->usable(cii, ti, &v)) {
110				if (masks_inited) {
111					formats &= v.formats;
112					rates &= v.rates;
113				} else {
114					formats = v.formats;
115					rates = v.rates;
116					masks_inited = 1;
117				}
118			}
119			ti++;
120		}
121	}
122	if (!masks_inited || !bus_factor || !sysclock_factor) {
123		result = -ENODEV;
124		goto out_unlock;
125	}
126	/* bus dependent stuff */
127	hw->info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
128		   SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME |
129		   SNDRV_PCM_INFO_JOINT_DUPLEX;
130
131	CHECK_RATE(5512);
132	CHECK_RATE(8000);
133	CHECK_RATE(11025);
134	CHECK_RATE(16000);
135	CHECK_RATE(22050);
136	CHECK_RATE(32000);
137	CHECK_RATE(44100);
138	CHECK_RATE(48000);
139	CHECK_RATE(64000);
140	CHECK_RATE(88200);
141	CHECK_RATE(96000);
142	CHECK_RATE(176400);
143	CHECK_RATE(192000);
144	hw->rates = rates;
145
146	/* well. the codec might want 24 bits only, and we'll
147	 * ever only transfer 24 bits, but they are top-aligned!
148	 * So for alsa, we claim that we're doing full 32 bit
149	 * while in reality we'll ignore the lower 8 bits of
150	 * that when doing playback (they're transferred as 0
151	 * as far as I know, no codecs we have are 32-bit capable
152	 * so I can't really test) and when doing recording we'll
153	 * always have those lower 8 bits recorded as 0 */
154	if (formats & SNDRV_PCM_FMTBIT_S24_BE)
155		formats |= SNDRV_PCM_FMTBIT_S32_BE;
156	if (formats & SNDRV_PCM_FMTBIT_U24_BE)
157		formats |= SNDRV_PCM_FMTBIT_U32_BE;
158	/* now mask off what we can support. I suppose we could
159	 * also support S24_3LE and some similar formats, but I
160	 * doubt there's a codec that would be able to use that,
161	 * so we don't support it here. */
162	hw->formats = formats & (SNDRV_PCM_FMTBIT_S16_BE |
163				 SNDRV_PCM_FMTBIT_U16_BE |
164				 SNDRV_PCM_FMTBIT_S32_BE |
165				 SNDRV_PCM_FMTBIT_U32_BE);
166
167	/* we need to set the highest and lowest rate possible.
168	 * These are the highest and lowest rates alsa can
169	 * support properly in its bitfield.
170	 * Below, we'll use that to restrict to the rate
171	 * currently in use (if any). */
172	hw->rate_min = 5512;
173	hw->rate_max = 192000;
174	if (other->active) {
175		hw->formats &= (1ULL << i2sdev->format);
176		/* see above, restrict rates to the one we already have */
177		hw->rate_min = i2sdev->rate;
178		hw->rate_max = i2sdev->rate;
179	}
180
181	hw->channels_min = 2;
182	hw->channels_max = 2;
183	/* these are somewhat arbitrary */
184	hw->buffer_bytes_max = 131072;
185	hw->period_bytes_min = 256;
186	hw->period_bytes_max = 16384;
187	hw->periods_min = 3;
188	hw->periods_max = MAX_DBDMA_COMMANDS;
189	list_for_each_entry(cii, &sdev->codec_list, list) {
190		if (cii->codec->open) {
191			err = cii->codec->open(cii, pi->substream);
192			if (err) {
193				result = err;
194				/* unwind */
195				found_this = 0;
196				list_for_each_entry_reverse(rev,
197				    &sdev->codec_list, list) {
198					if (found_this && rev->codec->close) {
199						rev->codec->close(rev,
200								pi->substream);
201					}
202					if (rev == cii)
203						found_this = 1;
204				}
205				goto out_unlock;
206			}
207		}
208	}
209
210 out_unlock:
211	mutex_unlock(&i2sdev->lock);
212	return result;
213}
214
215#undef CHECK_RATE
216
217static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in)
218{
219	struct codec_info_item *cii;
220	struct pcm_info *pi;
221	int err = 0, tmp;
222
223	mutex_lock(&i2sdev->lock);
224
225	get_pcm_info(i2sdev, in, &pi, NULL);
226
227	list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
228		if (cii->codec->close) {
229			tmp = cii->codec->close(cii, pi->substream);
230			if (tmp)
231				err = tmp;
232		}
233	}
234
235	pi->substream = NULL;
236	pi->active = 0;
237	mutex_unlock(&i2sdev->lock);
238	return err;
239}
240
241static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev,
242				 struct pcm_info *pi)
243{
244	unsigned long flags;
245	struct completion done;
246	long timeout;
247
248	spin_lock_irqsave(&i2sdev->low_lock, flags);
249	if (pi->dbdma_ring.stopping) {
250		init_completion(&done);
251		pi->stop_completion = &done;
252		spin_unlock_irqrestore(&i2sdev->low_lock, flags);
253		timeout = wait_for_completion_timeout(&done, HZ);
254		spin_lock_irqsave(&i2sdev->low_lock, flags);
255		pi->stop_completion = NULL;
256		if (timeout == 0) {
257			/* timeout expired, stop dbdma forcefully */
258			printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n");
259			/* make sure RUN, PAUSE and S0 bits are cleared */
260			out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
261			pi->dbdma_ring.stopping = 0;
262			timeout = 10;
263			while (in_le32(&pi->dbdma->status) & ACTIVE) {
264				if (--timeout <= 0)
265					break;
266				udelay(1);
267			}
268		}
269	}
270	spin_unlock_irqrestore(&i2sdev->low_lock, flags);
271}
272
273#ifdef CONFIG_PM
274void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev)
275{
276	struct pcm_info *pi;
277
278	get_pcm_info(i2sdev, 0, &pi, NULL);
279	i2sbus_wait_for_stop(i2sdev, pi);
280	get_pcm_info(i2sdev, 1, &pi, NULL);
281	i2sbus_wait_for_stop(i2sdev, pi);
282}
283#endif
284
285static int i2sbus_hw_params(struct snd_pcm_substream *substream,
286			    struct snd_pcm_hw_params *params)
287{
288	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
289}
290
291static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in)
292{
293	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
294	struct pcm_info *pi;
295
296	get_pcm_info(i2sdev, in, &pi, NULL);
297	if (pi->dbdma_ring.stopping)
298		i2sbus_wait_for_stop(i2sdev, pi);
299	snd_pcm_lib_free_pages(substream);
300	return 0;
301}
302
303static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream)
304{
305	return i2sbus_hw_free(substream, 0);
306}
307
308static int i2sbus_record_hw_free(struct snd_pcm_substream *substream)
309{
310	return i2sbus_hw_free(substream, 1);
311}
312
313static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in)
314{
315	/* whee. Hard work now. The user has selected a bitrate
316	 * and bit format, so now we have to program our
317	 * I2S controller appropriately. */
318	struct snd_pcm_runtime *runtime;
319	struct dbdma_cmd *command;
320	int i, periodsize, nperiods;
321	dma_addr_t offset;
322	struct bus_info bi;
323	struct codec_info_item *cii;
324	int sfr = 0;		/* serial format register */
325	int dws = 0;		/* data word sizes reg */
326	int input_16bit;
327	struct pcm_info *pi, *other;
328	int cnt;
329	int result = 0;
330	unsigned int cmd, stopaddr;
331
332	mutex_lock(&i2sdev->lock);
333
334	get_pcm_info(i2sdev, in, &pi, &other);
335
336	if (pi->dbdma_ring.running) {
337		result = -EBUSY;
338		goto out_unlock;
339	}
340	if (pi->dbdma_ring.stopping)
341		i2sbus_wait_for_stop(i2sdev, pi);
342
343	if (!pi->substream || !pi->substream->runtime) {
344		result = -EINVAL;
345		goto out_unlock;
346	}
347
348	runtime = pi->substream->runtime;
349	pi->active = 1;
350	if (other->active &&
351	    ((i2sdev->format != runtime->format)
352	     || (i2sdev->rate != runtime->rate))) {
353		result = -EINVAL;
354		goto out_unlock;
355	}
356
357	i2sdev->format = runtime->format;
358	i2sdev->rate = runtime->rate;
359
360	periodsize = snd_pcm_lib_period_bytes(pi->substream);
361	nperiods = pi->substream->runtime->periods;
362	pi->current_period = 0;
363
364	/* generate dbdma command ring first */
365	command = pi->dbdma_ring.cmds;
366	memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd));
367
368	/* commands to DMA to/from the ring */
369	/*
370	 * For input, we need to do a graceful stop; if we abort
371	 * the DMA, we end up with leftover bytes that corrupt
372	 * the next recording.  To do this we set the S0 status
373	 * bit and wait for the DMA controller to stop.  Each
374	 * command has a branch condition to
375	 * make it branch to a stop command if S0 is set.
376	 * On input we also need to wait for the S7 bit to be
377	 * set before turning off the DMA controller.
378	 * In fact we do the graceful stop for output as well.
379	 */
380	offset = runtime->dma_addr;
381	cmd = (in? INPUT_MORE: OUTPUT_MORE) | BR_IFSET | INTR_ALWAYS;
382	stopaddr = pi->dbdma_ring.bus_cmd_start +
383		(nperiods + 1) * sizeof(struct dbdma_cmd);
384	for (i = 0; i < nperiods; i++, command++, offset += periodsize) {
385		command->command = cpu_to_le16(cmd);
386		command->cmd_dep = cpu_to_le32(stopaddr);
387		command->phy_addr = cpu_to_le32(offset);
388		command->req_count = cpu_to_le16(periodsize);
389	}
390
391	/* branch back to beginning of ring */
392	command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
393	command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start);
394	command++;
395
396	/* set stop command */
397	command->command = cpu_to_le16(DBDMA_STOP);
398
399	/* ok, let's set the serial format and stuff */
400	switch (runtime->format) {
401	/* 16 bit formats */
402	case SNDRV_PCM_FORMAT_S16_BE:
403	case SNDRV_PCM_FORMAT_U16_BE:
404		bi.bus_factor = 0;
405		list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
406			bi.bus_factor = cii->codec->bus_factor;
407			break;
408		}
409		if (!bi.bus_factor) {
410			result = -ENODEV;
411			goto out_unlock;
412		}
413		input_16bit = 1;
414		break;
415	case SNDRV_PCM_FORMAT_S32_BE:
416	case SNDRV_PCM_FORMAT_U32_BE:
417		/* force 64x bus speed, otherwise the data cannot be
418		 * transferred quickly enough! */
419		bi.bus_factor = 64;
420		input_16bit = 0;
421		break;
422	default:
423		result = -EINVAL;
424		goto out_unlock;
425	}
426	/* we assume all sysclocks are the same! */
427	list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
428		bi.sysclock_factor = cii->codec->sysclock_factor;
429		break;
430	}
431
432	if (clock_and_divisors(bi.sysclock_factor,
433			       bi.bus_factor,
434			       runtime->rate,
435			       &sfr) < 0) {
436		result = -EINVAL;
437		goto out_unlock;
438	}
439	switch (bi.bus_factor) {
440	case 32:
441		sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X;
442		break;
443	case 64:
444		sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X;
445		break;
446	}
447	sfr |= I2S_SF_SCLK_MASTER;
448
449	list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
450		int err = 0;
451		if (cii->codec->prepare)
452			err = cii->codec->prepare(cii, &bi, pi->substream);
453		if (err) {
454			result = err;
455			goto out_unlock;
456		}
457	}
458	/* codecs are fine with it, so set our clocks */
459	if (input_16bit)
460		dws =	(2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
461			(2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
462			I2S_DWS_DATA_IN_16BIT | I2S_DWS_DATA_OUT_16BIT;
463	else
464		dws =	(2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) |
465			(2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) |
466			I2S_DWS_DATA_IN_24BIT | I2S_DWS_DATA_OUT_24BIT;
467
468	/* early exit if already programmed correctly */
469	/* not locking these is fine since we touch them only in this function */
470	if (in_le32(&i2sdev->intfregs->serial_format) == sfr
471	 && in_le32(&i2sdev->intfregs->data_word_sizes) == dws)
472		goto out_unlock;
473
474	/* let's notify the codecs about clocks going away.
475	 * For now we only do mastering on the i2s cell... */
476	list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
477		if (cii->codec->switch_clock)
478			cii->codec->switch_clock(cii, CLOCK_SWITCH_PREPARE_SLAVE);
479
480	i2sbus_control_enable(i2sdev->control, i2sdev);
481	i2sbus_control_cell(i2sdev->control, i2sdev, 1);
482
483	out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
484
485	i2sbus_control_clock(i2sdev->control, i2sdev, 0);
486
487	msleep(1);
488
489	/* wait for clock stopped. This can apparently take a while... */
490	cnt = 100;
491	while (cnt-- &&
492	    !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) {
493		msleep(5);
494	}
495	out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED);
496
497	/* not locking these is fine since we touch them only in this function */
498	out_le32(&i2sdev->intfregs->serial_format, sfr);
499	out_le32(&i2sdev->intfregs->data_word_sizes, dws);
500
501        i2sbus_control_enable(i2sdev->control, i2sdev);
502        i2sbus_control_cell(i2sdev->control, i2sdev, 1);
503        i2sbus_control_clock(i2sdev->control, i2sdev, 1);
504	msleep(1);
505
506	list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
507		if (cii->codec->switch_clock)
508			cii->codec->switch_clock(cii, CLOCK_SWITCH_SLAVE);
509
510 out_unlock:
511	mutex_unlock(&i2sdev->lock);
512	return result;
513}
514
515#ifdef CONFIG_PM
516void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev)
517{
518	i2sbus_pcm_prepare(i2sdev, 0);
519	i2sbus_pcm_prepare(i2sdev, 1);
520}
521#endif
522
523static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd)
524{
525	struct codec_info_item *cii;
526	struct pcm_info *pi;
527	int result = 0;
528	unsigned long flags;
529
530	spin_lock_irqsave(&i2sdev->low_lock, flags);
531
532	get_pcm_info(i2sdev, in, &pi, NULL);
533
534	switch (cmd) {
535	case SNDRV_PCM_TRIGGER_START:
536	case SNDRV_PCM_TRIGGER_RESUME:
537		if (pi->dbdma_ring.running) {
538			result = -EALREADY;
539			goto out_unlock;
540		}
541		list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
542			if (cii->codec->start)
543				cii->codec->start(cii, pi->substream);
544		pi->dbdma_ring.running = 1;
545
546		if (pi->dbdma_ring.stopping) {
547			/* Clear the S0 bit, then see if we stopped yet */
548			out_le32(&pi->dbdma->control, 1 << 16);
549			if (in_le32(&pi->dbdma->status) & ACTIVE) {
550				/* possible race here? */
551				udelay(10);
552				if (in_le32(&pi->dbdma->status) & ACTIVE) {
553					pi->dbdma_ring.stopping = 0;
554					goto out_unlock; /* keep running */
555				}
556			}
557		}
558
559		/* make sure RUN, PAUSE and S0 bits are cleared */
560		out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
561
562		/* set branch condition select register */
563		out_le32(&pi->dbdma->br_sel, (1 << 16) | 1);
564
565		/* write dma command buffer address to the dbdma chip */
566		out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start);
567
568		/* initialize the frame count and current period */
569		pi->current_period = 0;
570		pi->frame_count = in_le32(&i2sdev->intfregs->frame_count);
571
572		/* set the DMA controller running */
573		out_le32(&pi->dbdma->control, (RUN << 16) | RUN);
574
575		/* off you go! */
576		break;
577
578	case SNDRV_PCM_TRIGGER_STOP:
579	case SNDRV_PCM_TRIGGER_SUSPEND:
580		if (!pi->dbdma_ring.running) {
581			result = -EALREADY;
582			goto out_unlock;
583		}
584		pi->dbdma_ring.running = 0;
585
586		/* Set the S0 bit to make the DMA branch to the stop cmd */
587		out_le32(&pi->dbdma->control, (1 << 16) | 1);
588		pi->dbdma_ring.stopping = 1;
589
590		list_for_each_entry(cii, &i2sdev->sound.codec_list, list)
591			if (cii->codec->stop)
592				cii->codec->stop(cii, pi->substream);
593		break;
594	default:
595		result = -EINVAL;
596		goto out_unlock;
597	}
598
599 out_unlock:
600	spin_unlock_irqrestore(&i2sdev->low_lock, flags);
601	return result;
602}
603
604static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in)
605{
606	struct pcm_info *pi;
607	u32 fc;
608
609	get_pcm_info(i2sdev, in, &pi, NULL);
610
611	fc = in_le32(&i2sdev->intfregs->frame_count);
612	fc = fc - pi->frame_count;
613
614	if (fc >= pi->substream->runtime->buffer_size)
615		fc %= pi->substream->runtime->buffer_size;
616	return fc;
617}
618
619static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in)
620{
621	struct pcm_info *pi;
622	u32 fc, nframes;
623	u32 status;
624	int timeout, i;
625	int dma_stopped = 0;
626	struct snd_pcm_runtime *runtime;
627
628	spin_lock(&i2sdev->low_lock);
629	get_pcm_info(i2sdev, in, &pi, NULL);
630	if (!pi->dbdma_ring.running && !pi->dbdma_ring.stopping)
631		goto out_unlock;
632
633	i = pi->current_period;
634	runtime = pi->substream->runtime;
635	while (pi->dbdma_ring.cmds[i].xfer_status) {
636		if (le16_to_cpu(pi->dbdma_ring.cmds[i].xfer_status) & BT)
637			/*
638			 * BT is the branch taken bit.  If it took a branch
639			 * it is because we set the S0 bit to make it
640			 * branch to the stop command.
641			 */
642			dma_stopped = 1;
643		pi->dbdma_ring.cmds[i].xfer_status = 0;
644
645		if (++i >= runtime->periods) {
646			i = 0;
647			pi->frame_count += runtime->buffer_size;
648		}
649		pi->current_period = i;
650
651		/*
652		 * Check the frame count.  The DMA tends to get a bit
653		 * ahead of the frame counter, which confuses the core.
654		 */
655		fc = in_le32(&i2sdev->intfregs->frame_count);
656		nframes = i * runtime->period_size;
657		if (fc < pi->frame_count + nframes)
658			pi->frame_count = fc - nframes;
659	}
660
661	if (dma_stopped) {
662		timeout = 1000;
663		for (;;) {
664			status = in_le32(&pi->dbdma->status);
665			if (!(status & ACTIVE) && (!in || (status & 0x80)))
666				break;
667			if (--timeout <= 0) {
668				printk(KERN_ERR "i2sbus: timed out "
669				       "waiting for DMA to stop!\n");
670				break;
671			}
672			udelay(1);
673		}
674
675		/* Turn off DMA controller, clear S0 bit */
676		out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16);
677
678		pi->dbdma_ring.stopping = 0;
679		if (pi->stop_completion)
680			complete(pi->stop_completion);
681	}
682
683	if (!pi->dbdma_ring.running)
684		goto out_unlock;
685	spin_unlock(&i2sdev->low_lock);
686	/* may call _trigger again, hence needs to be unlocked */
687	snd_pcm_period_elapsed(pi->substream);
688	return;
689
690 out_unlock:
691	spin_unlock(&i2sdev->low_lock);
692}
693
694irqreturn_t i2sbus_tx_intr(int irq, void *devid)
695{
696	handle_interrupt((struct i2sbus_dev *)devid, 0);
697	return IRQ_HANDLED;
698}
699
700irqreturn_t i2sbus_rx_intr(int irq, void *devid)
701{
702	handle_interrupt((struct i2sbus_dev *)devid, 1);
703	return IRQ_HANDLED;
704}
705
706static int i2sbus_playback_open(struct snd_pcm_substream *substream)
707{
708	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
709
710	if (!i2sdev)
711		return -EINVAL;
712	i2sdev->out.substream = substream;
713	return i2sbus_pcm_open(i2sdev, 0);
714}
715
716static int i2sbus_playback_close(struct snd_pcm_substream *substream)
717{
718	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
719	int err;
720
721	if (!i2sdev)
722		return -EINVAL;
723	if (i2sdev->out.substream != substream)
724		return -EINVAL;
725	err = i2sbus_pcm_close(i2sdev, 0);
726	if (!err)
727		i2sdev->out.substream = NULL;
728	return err;
729}
730
731static int i2sbus_playback_prepare(struct snd_pcm_substream *substream)
732{
733	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
734
735	if (!i2sdev)
736		return -EINVAL;
737	if (i2sdev->out.substream != substream)
738		return -EINVAL;
739	return i2sbus_pcm_prepare(i2sdev, 0);
740}
741
742static int i2sbus_playback_trigger(struct snd_pcm_substream *substream, int cmd)
743{
744	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
745
746	if (!i2sdev)
747		return -EINVAL;
748	if (i2sdev->out.substream != substream)
749		return -EINVAL;
750	return i2sbus_pcm_trigger(i2sdev, 0, cmd);
751}
752
753static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream
754						 *substream)
755{
756	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
757
758	if (!i2sdev)
759		return -EINVAL;
760	if (i2sdev->out.substream != substream)
761		return 0;
762	return i2sbus_pcm_pointer(i2sdev, 0);
763}
764
765static struct snd_pcm_ops i2sbus_playback_ops = {
766	.open =		i2sbus_playback_open,
767	.close =	i2sbus_playback_close,
768	.ioctl =	snd_pcm_lib_ioctl,
769	.hw_params =	i2sbus_hw_params,
770	.hw_free =	i2sbus_playback_hw_free,
771	.prepare =	i2sbus_playback_prepare,
772	.trigger =	i2sbus_playback_trigger,
773	.pointer =	i2sbus_playback_pointer,
774};
775
776static int i2sbus_record_open(struct snd_pcm_substream *substream)
777{
778	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
779
780	if (!i2sdev)
781		return -EINVAL;
782	i2sdev->in.substream = substream;
783	return i2sbus_pcm_open(i2sdev, 1);
784}
785
786static int i2sbus_record_close(struct snd_pcm_substream *substream)
787{
788	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
789	int err;
790
791	if (!i2sdev)
792		return -EINVAL;
793	if (i2sdev->in.substream != substream)
794		return -EINVAL;
795	err = i2sbus_pcm_close(i2sdev, 1);
796	if (!err)
797		i2sdev->in.substream = NULL;
798	return err;
799}
800
801static int i2sbus_record_prepare(struct snd_pcm_substream *substream)
802{
803	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
804
805	if (!i2sdev)
806		return -EINVAL;
807	if (i2sdev->in.substream != substream)
808		return -EINVAL;
809	return i2sbus_pcm_prepare(i2sdev, 1);
810}
811
812static int i2sbus_record_trigger(struct snd_pcm_substream *substream, int cmd)
813{
814	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
815
816	if (!i2sdev)
817		return -EINVAL;
818	if (i2sdev->in.substream != substream)
819		return -EINVAL;
820	return i2sbus_pcm_trigger(i2sdev, 1, cmd);
821}
822
823static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream
824					       *substream)
825{
826	struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream);
827
828	if (!i2sdev)
829		return -EINVAL;
830	if (i2sdev->in.substream != substream)
831		return 0;
832	return i2sbus_pcm_pointer(i2sdev, 1);
833}
834
835static struct snd_pcm_ops i2sbus_record_ops = {
836	.open =		i2sbus_record_open,
837	.close =	i2sbus_record_close,
838	.ioctl =	snd_pcm_lib_ioctl,
839	.hw_params =	i2sbus_hw_params,
840	.hw_free =	i2sbus_record_hw_free,
841	.prepare =	i2sbus_record_prepare,
842	.trigger =	i2sbus_record_trigger,
843	.pointer =	i2sbus_record_pointer,
844};
845
846static void i2sbus_private_free(struct snd_pcm *pcm)
847{
848	struct i2sbus_dev *i2sdev = snd_pcm_chip(pcm);
849	struct codec_info_item *p, *tmp;
850
851	i2sdev->sound.pcm = NULL;
852	i2sdev->out.created = 0;
853	i2sdev->in.created = 0;
854	list_for_each_entry_safe(p, tmp, &i2sdev->sound.codec_list, list) {
855		printk(KERN_ERR "i2sbus: a codec didn't unregister!\n");
856		list_del(&p->list);
857		module_put(p->codec->owner);
858		kfree(p);
859	}
860	soundbus_dev_put(&i2sdev->sound);
861	module_put(THIS_MODULE);
862}
863
864int
865i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
866		    struct codec_info *ci, void *data)
867{
868	int err, in = 0, out = 0;
869	struct transfer_info *tmp;
870	struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
871	struct codec_info_item *cii;
872
873	if (!dev->pcmname || dev->pcmid == -1) {
874		printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
875		return -EINVAL;
876	}
877
878	list_for_each_entry(cii, &dev->codec_list, list) {
879		if (cii->codec_data == data)
880			return -EALREADY;
881	}
882
883	if (!ci->transfers || !ci->transfers->formats
884	    || !ci->transfers->rates || !ci->usable)
885		return -EINVAL;
886
887	/* we currently code the i2s transfer on the clock, and support only
888	 * 32 and 64 */
889	if (ci->bus_factor != 32 && ci->bus_factor != 64)
890		return -EINVAL;
891
892	/* If you want to fix this, you need to keep track of what transport infos
893	 * are to be used, which codecs they belong to, and then fix all the
894	 * sysclock/busclock stuff above to depend on which is usable */
895	list_for_each_entry(cii, &dev->codec_list, list) {
896		if (cii->codec->sysclock_factor != ci->sysclock_factor) {
897			printk(KERN_DEBUG
898			       "cannot yet handle multiple different sysclocks!\n");
899			return -EINVAL;
900		}
901		if (cii->codec->bus_factor != ci->bus_factor) {
902			printk(KERN_DEBUG
903			       "cannot yet handle multiple different bus clocks!\n");
904			return -EINVAL;
905		}
906	}
907
908	tmp = ci->transfers;
909	while (tmp->formats && tmp->rates) {
910		if (tmp->transfer_in)
911			in = 1;
912		else
913			out = 1;
914		tmp++;
915	}
916
917	cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL);
918	if (!cii) {
919		printk(KERN_DEBUG "i2sbus: failed to allocate cii\n");
920		return -ENOMEM;
921	}
922
923	/* use the private data to point to the codec info */
924	cii->sdev = soundbus_dev_get(dev);
925	cii->codec = ci;
926	cii->codec_data = data;
927
928	if (!cii->sdev) {
929		printk(KERN_DEBUG
930		       "i2sbus: failed to get soundbus dev reference\n");
931		err = -ENODEV;
932		goto out_free_cii;
933	}
934
935	if (!try_module_get(THIS_MODULE)) {
936		printk(KERN_DEBUG "i2sbus: failed to get module reference!\n");
937		err = -EBUSY;
938		goto out_put_sdev;
939	}
940
941	if (!try_module_get(ci->owner)) {
942		printk(KERN_DEBUG
943		       "i2sbus: failed to get module reference to codec owner!\n");
944		err = -EBUSY;
945		goto out_put_this_module;
946	}
947
948	if (!dev->pcm) {
949		err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0,
950				  &dev->pcm);
951		if (err) {
952			printk(KERN_DEBUG "i2sbus: failed to create pcm\n");
953			goto out_put_ci_module;
954		}
955		dev->pcm->dev = &dev->ofdev.dev;
956	}
957
958	/* ALSA yet again sucks.
959	 * If it is ever fixed, remove this line. See below. */
960	out = in = 1;
961
962	if (!i2sdev->out.created && out) {
963		if (dev->pcm->card != card) {
964			/* eh? */
965			printk(KERN_ERR
966			       "Can't attach same bus to different cards!\n");
967			err = -EINVAL;
968			goto out_put_ci_module;
969		}
970		err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
971		if (err)
972			goto out_put_ci_module;
973		snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
974				&i2sbus_playback_ops);
975		i2sdev->out.created = 1;
976	}
977
978	if (!i2sdev->in.created && in) {
979		if (dev->pcm->card != card) {
980			printk(KERN_ERR
981			       "Can't attach same bus to different cards!\n");
982			goto out_put_ci_module;
983		}
984		err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
985		if (err)
986			goto out_put_ci_module;
987		snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE,
988				&i2sbus_record_ops);
989		i2sdev->in.created = 1;
990	}
991
992	/* so we have to register the pcm after adding any substream
993	 * to it because alsa doesn't create the devices for the
994	 * substreams when we add them later.
995	 * Therefore, force in and out on both busses (above) and
996	 * register the pcm now instead of just after creating it.
997	 */
998	err = snd_device_register(card, dev->pcm);
999	if (err) {
1000		printk(KERN_ERR "i2sbus: error registering new pcm\n");
1001		goto out_put_ci_module;
1002	}
1003	/* no errors any more, so let's add this to our list */
1004	list_add(&cii->list, &dev->codec_list);
1005
1006	dev->pcm->private_data = i2sdev;
1007	dev->pcm->private_free = i2sbus_private_free;
1008
1009	/* well, we really should support scatter/gather DMA */
1010	snd_pcm_lib_preallocate_pages_for_all(
1011		dev->pcm, SNDRV_DMA_TYPE_DEV,
1012		snd_dma_pci_data(macio_get_pci_dev(i2sdev->macio)),
1013		64 * 1024, 64 * 1024);
1014
1015	return 0;
1016 out_put_ci_module:
1017	module_put(ci->owner);
1018 out_put_this_module:
1019	module_put(THIS_MODULE);
1020 out_put_sdev:
1021	soundbus_dev_put(dev);
1022 out_free_cii:
1023	kfree(cii);
1024	return err;
1025}
1026
1027void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
1028{
1029	struct codec_info_item *cii = NULL, *i;
1030
1031	list_for_each_entry(i, &dev->codec_list, list) {
1032		if (i->codec_data == data) {
1033			cii = i;
1034			break;
1035		}
1036	}
1037	if (cii) {
1038		list_del(&cii->list);
1039		module_put(cii->codec->owner);
1040		kfree(cii);
1041	}
1042	/* no more codecs, but still a pcm? */
1043	if (list_empty(&dev->codec_list) && dev->pcm) {
1044		/* the actual cleanup is done by the callback above! */
1045		snd_device_free(dev->pcm->card, dev->pcm);
1046	}
1047}
1048