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