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