File Index
Files and directories
Sign in
FILE

pcm.c

File preview with syntax highlighting.

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