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