Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * PMac DBDMA lowlevel functions
   4 *
   5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
   6 * code based on dmasound.c.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   7 */
   8
   9
  10#include <linux/io.h>
  11#include <asm/irq.h>
  12#include <linux/init.h>
  13#include <linux/delay.h>
  14#include <linux/slab.h>
  15#include <linux/interrupt.h>
  16#include <linux/pci.h>
  17#include <linux/dma-mapping.h>
  18#include <linux/of_address.h>
  19#include <linux/of_irq.h>
  20#include <sound/core.h>
  21#include "pmac.h"
  22#include <sound/pcm_params.h>
  23#include <asm/pmac_feature.h>
 
  24
  25
  26/* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
  27static const int awacs_freqs[8] = {
  28	44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
  29};
  30/* fixed frequency table for tumbler */
  31static const int tumbler_freqs[1] = {
  32	44100
  33};
  34
  35
  36/*
  37 * we will allocate a single 'emergency' dbdma cmd block to use if the
  38 * tx status comes up "DEAD".  This happens on some PowerComputing Pmac
  39 * clones, either owing to a bug in dbdma or some interaction between
  40 * IDE and sound.  However, this measure would deal with DEAD status if
  41 * it appeared elsewhere.
  42 */
  43static struct pmac_dbdma emergency_dbdma;
  44static int emergency_in_use;
  45
  46
  47/*
  48 * allocate DBDMA command arrays
  49 */
  50static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
  51{
  52	unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
  53
  54	rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
  55					&rec->dma_base, GFP_KERNEL);
  56	if (rec->space == NULL)
  57		return -ENOMEM;
  58	rec->size = size;
  59	memset(rec->space, 0, rsize);
  60	rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
  61	rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
  62
  63	return 0;
  64}
  65
  66static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
  67{
  68	if (rec->space) {
  69		unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
  70
  71		dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
  72	}
  73}
  74
  75
  76/*
  77 * pcm stuff
  78 */
  79
  80/*
  81 * look up frequency table
  82 */
  83
  84unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
  85{
  86	int i, ok, found;
  87
  88	ok = rec->cur_freqs;
  89	if (rate > chip->freq_table[0])
  90		return 0;
  91	found = 0;
  92	for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
  93		if (! (ok & 1)) continue;
  94		found = i;
  95		if (rate >= chip->freq_table[i])
  96			break;
  97	}
  98	return found;
  99}
 100
 101/*
 102 * check whether another stream is active
 103 */
 104static inline int another_stream(int stream)
 105{
 106	return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
 107		SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
 108}
 109
 110/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 111 * get a stream of the opposite direction
 112 */
 113static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
 114{
 115	switch (stream) {
 116	case SNDRV_PCM_STREAM_PLAYBACK:
 117		return &chip->playback;
 118	case SNDRV_PCM_STREAM_CAPTURE:
 119		return &chip->capture;
 120	default:
 121		snd_BUG();
 122		return NULL;
 123	}
 124}
 125
 126/*
 127 * wait while run status is on
 128 */
 129static inline void
 130snd_pmac_wait_ack(struct pmac_stream *rec)
 131{
 132	int timeout = 50000;
 133	while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
 134		udelay(1);
 135}
 136
 137/*
 138 * set the format and rate to the chip.
 139 * call the lowlevel function if defined (e.g. for AWACS).
 140 */
 141static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
 142{
 143	/* set up frequency and format */
 144	out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
 145	out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
 146	if (chip->set_format)
 147		chip->set_format(chip);
 148}
 149
 150/*
 151 * stop the DMA transfer
 152 */
 153static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
 154{
 155	out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
 156	snd_pmac_wait_ack(rec);
 157}
 158
 159/*
 160 * set the command pointer address
 161 */
 162static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
 163{
 164	out_le32(&rec->dma->cmdptr, cmd->addr);
 165}
 166
 167/*
 168 * start the DMA
 169 */
 170static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
 171{
 172	out_le32(&rec->dma->control, status | (status << 16));
 173}
 174
 175
 176/*
 177 * prepare playback/capture stream
 178 */
 179static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
 180{
 181	int i;
 182	volatile struct dbdma_cmd __iomem *cp;
 183	struct snd_pcm_runtime *runtime = subs->runtime;
 184	int rate_index;
 185	long offset;
 186	struct pmac_stream *astr;
 187
 188	rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
 189	rec->period_size = snd_pcm_lib_period_bytes(subs);
 190	rec->nperiods = rec->dma_size / rec->period_size;
 191	rec->cur_period = 0;
 192	rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
 193
 194	/* set up constraints */
 195	astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
 196	if (! astr)
 197		return -EINVAL;
 198	astr->cur_freqs = 1 << rate_index;
 199	astr->cur_formats = 1 << runtime->format;
 200	chip->rate_index = rate_index;
 201	chip->format = runtime->format;
 202
 203	/* We really want to execute a DMA stop command, after the AWACS
 204	 * is initialized.
 205	 * For reasons I don't understand, it stops the hissing noise
 206	 * common to many PowerBook G3 systems and random noise otherwise
 207	 * captured on iBook2's about every third time. -ReneR
 208	 */
 209	spin_lock_irq(&chip->reg_lock);
 210	snd_pmac_dma_stop(rec);
 211	chip->extra_dma.cmds->command = cpu_to_le16(DBDMA_STOP);
 212	snd_pmac_dma_set_command(rec, &chip->extra_dma);
 213	snd_pmac_dma_run(rec, RUN);
 214	spin_unlock_irq(&chip->reg_lock);
 215	mdelay(5);
 216	spin_lock_irq(&chip->reg_lock);
 217	/* continuous DMA memory type doesn't provide the physical address,
 218	 * so we need to resolve the address here...
 219	 */
 220	offset = runtime->dma_addr;
 221	for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
 222		cp->phy_addr = cpu_to_le32(offset);
 223		cp->req_count = cpu_to_le16(rec->period_size);
 224		/*cp->res_count = cpu_to_le16(0);*/
 225		cp->xfer_status = cpu_to_le16(0);
 226		offset += rec->period_size;
 227	}
 228	/* make loop */
 229	cp->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS);
 230	cp->cmd_dep = cpu_to_le32(rec->cmd.addr);
 231
 232	snd_pmac_dma_stop(rec);
 233	snd_pmac_dma_set_command(rec, &rec->cmd);
 234	spin_unlock_irq(&chip->reg_lock);
 235
 236	return 0;
 237}
 238
 239
 240/*
 241 * PCM trigger/stop
 242 */
 243static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
 244				struct snd_pcm_substream *subs, int cmd)
 245{
 246	volatile struct dbdma_cmd __iomem *cp;
 247	int i, command;
 248
 249	switch (cmd) {
 250	case SNDRV_PCM_TRIGGER_START:
 251	case SNDRV_PCM_TRIGGER_RESUME:
 252		if (rec->running)
 253			return -EBUSY;
 254		command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
 255			   OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
 256		spin_lock(&chip->reg_lock);
 257		snd_pmac_beep_stop(chip);
 258		snd_pmac_pcm_set_format(chip);
 259		for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
 260			out_le16(&cp->command, command);
 261		snd_pmac_dma_set_command(rec, &rec->cmd);
 262		(void)in_le32(&rec->dma->status);
 263		snd_pmac_dma_run(rec, RUN|WAKE);
 264		rec->running = 1;
 265		spin_unlock(&chip->reg_lock);
 266		break;
 267
 268	case SNDRV_PCM_TRIGGER_STOP:
 269	case SNDRV_PCM_TRIGGER_SUSPEND:
 270		spin_lock(&chip->reg_lock);
 271		rec->running = 0;
 
 272		snd_pmac_dma_stop(rec);
 273		for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
 274			out_le16(&cp->command, DBDMA_STOP);
 275		spin_unlock(&chip->reg_lock);
 276		break;
 277
 278	default:
 279		return -EINVAL;
 280	}
 281
 282	return 0;
 283}
 284
 285/*
 286 * return the current pointer
 287 */
 288inline
 289static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
 290					      struct pmac_stream *rec,
 291					      struct snd_pcm_substream *subs)
 292{
 293	int count = 0;
 294
 295#if 1 /* hmm.. how can we get the current dma pointer?? */
 296	int stat;
 297	volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
 298	stat = le16_to_cpu(cp->xfer_status);
 299	if (stat & (ACTIVE|DEAD)) {
 300		count = in_le16(&cp->res_count);
 301		if (count)
 302			count = rec->period_size - count;
 303	}
 304#endif
 305	count += rec->cur_period * rec->period_size;
 
 306	return bytes_to_frames(subs->runtime, count);
 307}
 308
 309/*
 310 * playback
 311 */
 312
 313static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
 314{
 315	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 316	return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
 317}
 318
 319static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
 320				     int cmd)
 321{
 322	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 323	return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
 324}
 325
 326static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
 327{
 328	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 329	return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
 330}
 331
 332
 333/*
 334 * capture
 335 */
 336
 337static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
 338{
 339	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 340	return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
 341}
 342
 343static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
 344				    int cmd)
 345{
 346	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 347	return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
 348}
 349
 350static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
 351{
 352	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 353	return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
 354}
 355
 356
 357/*
 358 * Handle DEAD DMA transfers:
 359 * if the TX status comes up "DEAD" - reported on some Power Computing machines
 360 * we need to re-start the dbdma - but from a different physical start address
 361 * and with a different transfer length.  It would get very messy to do this
 362 * with the normal dbdma_cmd blocks - we would have to re-write the buffer start
 363 * addresses each time.  So, we will keep a single dbdma_cmd block which can be
 364 * fiddled with.
 365 * When DEAD status is first reported the content of the faulted dbdma block is
 366 * copied into the emergency buffer and we note that the buffer is in use.
 367 * we then bump the start physical address by the amount that was successfully
 368 * output before it died.
 369 * On any subsequent DEAD result we just do the bump-ups (we know that we are
 370 * already using the emergency dbdma_cmd).
 371 * CHECK: this just tries to "do it".  It is possible that we should abandon
 372 * xfers when the number of residual bytes gets below a certain value - I can
 373 * see that this might cause a loop-forever if a too small transfer causes
 374 * DEAD status.  However this is a TODO for now - we'll see what gets reported.
 375 * When we get a successful transfer result with the emergency buffer we just
 376 * pretend that it completed using the original dmdma_cmd and carry on.  The
 377 * 'next_cmd' field will already point back to the original loop of blocks.
 378 */
 379static inline void snd_pmac_pcm_dead_xfer(struct pmac_stream *rec,
 380					  volatile struct dbdma_cmd __iomem *cp)
 381{
 382	unsigned short req, res ;
 383	unsigned int phy ;
 384
 
 
 385	/* to clear DEAD status we must first clear RUN
 386	   set it to quiescent to be on the safe side */
 387	(void)in_le32(&rec->dma->status);
 388	out_le32(&rec->dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
 389
 390	if (!emergency_in_use) { /* new problem */
 391		memcpy((void *)emergency_dbdma.cmds, (void *)cp,
 392		       sizeof(struct dbdma_cmd));
 393		emergency_in_use = 1;
 394		cp->xfer_status = cpu_to_le16(0);
 395		cp->req_count = cpu_to_le16(rec->period_size);
 396		cp = emergency_dbdma.cmds;
 397	}
 398
 399	/* now bump the values to reflect the amount
 400	   we haven't yet shifted */
 401	req = le16_to_cpu(cp->req_count);
 402	res = le16_to_cpu(cp->res_count);
 403	phy = le32_to_cpu(cp->phy_addr);
 404	phy += (req - res);
 405	cp->req_count = cpu_to_le16(res);
 406	cp->res_count = cpu_to_le16(0);
 407	cp->xfer_status = cpu_to_le16(0);
 408	cp->phy_addr = cpu_to_le32(phy);
 409
 410	cp->cmd_dep = cpu_to_le32(rec->cmd.addr
 411		+ sizeof(struct dbdma_cmd)*((rec->cur_period+1)%rec->nperiods));
 412
 413	cp->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS | INTR_ALWAYS);
 414
 415	/* point at our patched up command block */
 416	out_le32(&rec->dma->cmdptr, emergency_dbdma.addr);
 417
 418	/* we must re-start the controller */
 419	(void)in_le32(&rec->dma->status);
 420	/* should complete clearing the DEAD status */
 421	out_le32(&rec->dma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
 422}
 423
 424/*
 425 * update playback/capture pointer from interrupts
 426 */
 427static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
 428{
 429	volatile struct dbdma_cmd __iomem *cp;
 430	int c;
 431	int stat;
 432
 433	spin_lock(&chip->reg_lock);
 434	if (rec->running) {
 435		for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
 436
 437			if (emergency_in_use)   /* already using DEAD xfer? */
 438				cp = emergency_dbdma.cmds;
 439			else
 440				cp = &rec->cmd.cmds[rec->cur_period];
 441
 442			stat = le16_to_cpu(cp->xfer_status);
 443
 444			if (stat & DEAD) {
 445				snd_pmac_pcm_dead_xfer(rec, cp);
 446				break; /* this block is still going */
 447			}
 448
 449			if (emergency_in_use)
 450				emergency_in_use = 0 ; /* done that */
 451
 452			if (! (stat & ACTIVE))
 453				break;
 454
 455			cp->xfer_status = cpu_to_le16(0);
 456			cp->req_count = cpu_to_le16(rec->period_size);
 457			/*cp->res_count = cpu_to_le16(0);*/
 
 458			rec->cur_period++;
 459			if (rec->cur_period >= rec->nperiods) {
 460				rec->cur_period = 0;
 461			}
 462
 463			spin_unlock(&chip->reg_lock);
 464			snd_pcm_period_elapsed(rec->substream);
 465			spin_lock(&chip->reg_lock);
 466		}
 467	}
 468	spin_unlock(&chip->reg_lock);
 469}
 470
 471
 472/*
 473 * hw info
 474 */
 475
 476static const struct snd_pcm_hardware snd_pmac_playback =
 477{
 478	.info =			(SNDRV_PCM_INFO_INTERLEAVED |
 479				 SNDRV_PCM_INFO_MMAP |
 480				 SNDRV_PCM_INFO_MMAP_VALID |
 481				 SNDRV_PCM_INFO_RESUME),
 482	.formats =		SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
 483	.rates =		SNDRV_PCM_RATE_8000_44100,
 484	.rate_min =		7350,
 485	.rate_max =		44100,
 486	.channels_min =		2,
 487	.channels_max =		2,
 488	.buffer_bytes_max =	131072,
 489	.period_bytes_min =	256,
 490	.period_bytes_max =	16384,
 491	.periods_min =		3,
 492	.periods_max =		PMAC_MAX_FRAGS,
 493};
 494
 495static const struct snd_pcm_hardware snd_pmac_capture =
 496{
 497	.info =			(SNDRV_PCM_INFO_INTERLEAVED |
 498				 SNDRV_PCM_INFO_MMAP |
 499				 SNDRV_PCM_INFO_MMAP_VALID |
 500				 SNDRV_PCM_INFO_RESUME),
 501	.formats =		SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
 502	.rates =		SNDRV_PCM_RATE_8000_44100,
 503	.rate_min =		7350,
 504	.rate_max =		44100,
 505	.channels_min =		2,
 506	.channels_max =		2,
 507	.buffer_bytes_max =	131072,
 508	.period_bytes_min =	256,
 509	.period_bytes_max =	16384,
 510	.periods_min =		3,
 511	.periods_max =		PMAC_MAX_FRAGS,
 512};
 513
 514
 515#if 0 // NYI
 516static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
 517				 struct snd_pcm_hw_rule *rule)
 518{
 519	struct snd_pmac *chip = rule->private;
 520	struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
 521	int i, freq_table[8], num_freqs;
 522
 523	if (! rec)
 524		return -EINVAL;
 525	num_freqs = 0;
 526	for (i = chip->num_freqs - 1; i >= 0; i--) {
 527		if (rec->cur_freqs & (1 << i))
 528			freq_table[num_freqs++] = chip->freq_table[i];
 529	}
 530
 531	return snd_interval_list(hw_param_interval(params, rule->var),
 532				 num_freqs, freq_table, 0);
 533}
 534
 535static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
 536				   struct snd_pcm_hw_rule *rule)
 537{
 538	struct snd_pmac *chip = rule->private;
 539	struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
 540
 541	if (! rec)
 542		return -EINVAL;
 543	return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
 544				   rec->cur_formats);
 545}
 546#endif // NYI
 547
 548static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
 549			     struct snd_pcm_substream *subs)
 550{
 551	struct snd_pcm_runtime *runtime = subs->runtime;
 552	int i;
 553
 554	/* look up frequency table and fill bit mask */
 555	runtime->hw.rates = 0;
 556	for (i = 0; i < chip->num_freqs; i++)
 557		if (chip->freqs_ok & (1 << i))
 558			runtime->hw.rates |=
 559				snd_pcm_rate_to_rate_bit(chip->freq_table[i]);
 560
 561	/* check for minimum and maximum rates */
 562	for (i = 0; i < chip->num_freqs; i++) {
 563		if (chip->freqs_ok & (1 << i)) {
 564			runtime->hw.rate_max = chip->freq_table[i];
 565			break;
 566		}
 567	}
 568	for (i = chip->num_freqs - 1; i >= 0; i--) {
 569		if (chip->freqs_ok & (1 << i)) {
 570			runtime->hw.rate_min = chip->freq_table[i];
 571			break;
 572		}
 573	}
 574	runtime->hw.formats = chip->formats_ok;
 575	if (chip->can_capture) {
 576		if (! chip->can_duplex)
 577			runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
 578		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
 579	}
 580	runtime->private_data = rec;
 581	rec->substream = subs;
 582
 583#if 0 /* FIXME: still under development.. */
 584	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
 585			    snd_pmac_hw_rule_rate, chip, rec->stream, -1);
 586	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
 587			    snd_pmac_hw_rule_format, chip, rec->stream, -1);
 588#endif
 589
 590	runtime->hw.periods_max = rec->cmd.size - 1;
 591
 592	/* constraints to fix choppy sound */
 593	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
 594	return 0;
 595}
 596
 597static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
 598			      struct snd_pcm_substream *subs)
 599{
 600	struct pmac_stream *astr;
 601
 602	snd_pmac_dma_stop(rec);
 603
 604	astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
 605	if (! astr)
 606		return -EINVAL;
 607
 608	/* reset constraints */
 609	astr->cur_freqs = chip->freqs_ok;
 610	astr->cur_formats = chip->formats_ok;
 611
 612	return 0;
 613}
 614
 615static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
 616{
 617	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 618
 619	subs->runtime->hw = snd_pmac_playback;
 620	return snd_pmac_pcm_open(chip, &chip->playback, subs);
 621}
 622
 623static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
 624{
 625	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 626
 627	subs->runtime->hw = snd_pmac_capture;
 628	return snd_pmac_pcm_open(chip, &chip->capture, subs);
 629}
 630
 631static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
 632{
 633	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 634
 635	return snd_pmac_pcm_close(chip, &chip->playback, subs);
 636}
 637
 638static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
 639{
 640	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 641
 642	return snd_pmac_pcm_close(chip, &chip->capture, subs);
 643}
 644
 645/*
 646 */
 647
 648static const struct snd_pcm_ops snd_pmac_playback_ops = {
 649	.open =		snd_pmac_playback_open,
 650	.close =	snd_pmac_playback_close,
 
 
 
 651	.prepare =	snd_pmac_playback_prepare,
 652	.trigger =	snd_pmac_playback_trigger,
 653	.pointer =	snd_pmac_playback_pointer,
 654};
 655
 656static const struct snd_pcm_ops snd_pmac_capture_ops = {
 657	.open =		snd_pmac_capture_open,
 658	.close =	snd_pmac_capture_close,
 
 
 
 659	.prepare =	snd_pmac_capture_prepare,
 660	.trigger =	snd_pmac_capture_trigger,
 661	.pointer =	snd_pmac_capture_pointer,
 662};
 663
 664int snd_pmac_pcm_new(struct snd_pmac *chip)
 665{
 666	struct snd_pcm *pcm;
 667	int err;
 668	int num_captures = 1;
 669
 670	if (! chip->can_capture)
 671		num_captures = 0;
 672	err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
 673	if (err < 0)
 674		return err;
 675
 676	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
 677	if (chip->can_capture)
 678		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
 679
 680	pcm->private_data = chip;
 681	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
 682	strcpy(pcm->name, chip->card->shortname);
 683	chip->pcm = pcm;
 684
 685	chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
 686	if (chip->can_byte_swap)
 687		chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
 688
 689	chip->playback.cur_formats = chip->formats_ok;
 690	chip->capture.cur_formats = chip->formats_ok;
 691	chip->playback.cur_freqs = chip->freqs_ok;
 692	chip->capture.cur_freqs = chip->freqs_ok;
 693
 694	/* preallocate 64k buffer */
 695	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
 696				       &chip->pdev->dev,
 697				       64 * 1024, 64 * 1024);
 698
 699	return 0;
 700}
 701
 702
 703static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
 704{
 705	out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
 706	snd_pmac_wait_ack(&chip->playback);
 707	out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
 708	snd_pmac_wait_ack(&chip->capture);
 709}
 710
 711
 712/*
 713 * handling beep
 714 */
 715void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
 716{
 717	struct pmac_stream *rec = &chip->playback;
 718
 719	snd_pmac_dma_stop(rec);
 720	chip->extra_dma.cmds->req_count = cpu_to_le16(bytes);
 721	chip->extra_dma.cmds->xfer_status = cpu_to_le16(0);
 722	chip->extra_dma.cmds->cmd_dep = cpu_to_le32(chip->extra_dma.addr);
 723	chip->extra_dma.cmds->phy_addr = cpu_to_le32(addr);
 724	chip->extra_dma.cmds->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS);
 725	out_le32(&chip->awacs->control,
 726		 (in_le32(&chip->awacs->control) & ~0x1f00)
 727		 | (speed << 8));
 728	out_le32(&chip->awacs->byteswap, 0);
 729	snd_pmac_dma_set_command(rec, &chip->extra_dma);
 730	snd_pmac_dma_run(rec, RUN);
 731}
 732
 733void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
 734{
 735	snd_pmac_dma_stop(&chip->playback);
 736	chip->extra_dma.cmds->command = cpu_to_le16(DBDMA_STOP);
 737	snd_pmac_pcm_set_format(chip); /* reset format */
 738}
 739
 740
 741/*
 742 * interrupt handlers
 743 */
 744static irqreturn_t
 745snd_pmac_tx_intr(int irq, void *devid)
 746{
 747	struct snd_pmac *chip = devid;
 748	snd_pmac_pcm_update(chip, &chip->playback);
 749	return IRQ_HANDLED;
 750}
 751
 752
 753static irqreturn_t
 754snd_pmac_rx_intr(int irq, void *devid)
 755{
 756	struct snd_pmac *chip = devid;
 757	snd_pmac_pcm_update(chip, &chip->capture);
 758	return IRQ_HANDLED;
 759}
 760
 761
 762static irqreturn_t
 763snd_pmac_ctrl_intr(int irq, void *devid)
 764{
 765	struct snd_pmac *chip = devid;
 766	int ctrl = in_le32(&chip->awacs->control);
 767
 
 768	if (ctrl & MASK_PORTCHG) {
 769		/* do something when headphone is plugged/unplugged? */
 770		if (chip->update_automute)
 771			chip->update_automute(chip, 1);
 772	}
 773	if (ctrl & MASK_CNTLERR) {
 774		int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
 775		if (err && chip->model <= PMAC_SCREAMER)
 776			dev_dbg(chip->card->dev, "%s: error %x\n", __func__, err);
 777	}
 778	/* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
 779	out_le32(&chip->awacs->control, ctrl);
 780	return IRQ_HANDLED;
 781}
 782
 783
 784/*
 785 * a wrapper to feature call for compatibility
 786 */
 787static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
 788{
 789	if (ppc_md.feature_call)
 790		ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
 791}
 792
 793/*
 794 * release resources
 795 */
 796
 797static int snd_pmac_free(struct snd_pmac *chip)
 798{
 799	/* stop sounds */
 800	if (chip->initialized) {
 801		snd_pmac_dbdma_reset(chip);
 802		/* disable interrupts from awacs interface */
 803		out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
 804	}
 805
 806	if (chip->node)
 807		snd_pmac_sound_feature(chip, 0);
 808
 809	/* clean up mixer if any */
 810	if (chip->mixer_free)
 811		chip->mixer_free(chip);
 812
 813	snd_pmac_detach_beep(chip);
 814
 815	/* release resources */
 816	if (chip->irq >= 0)
 817		free_irq(chip->irq, (void*)chip);
 818	if (chip->tx_irq >= 0)
 819		free_irq(chip->tx_irq, (void*)chip);
 820	if (chip->rx_irq >= 0)
 821		free_irq(chip->rx_irq, (void*)chip);
 822	snd_pmac_dbdma_free(chip, &chip->playback.cmd);
 823	snd_pmac_dbdma_free(chip, &chip->capture.cmd);
 824	snd_pmac_dbdma_free(chip, &chip->extra_dma);
 825	snd_pmac_dbdma_free(chip, &emergency_dbdma);
 826	iounmap(chip->macio_base);
 827	iounmap(chip->latch_base);
 828	iounmap(chip->awacs);
 829	iounmap(chip->playback.dma);
 830	iounmap(chip->capture.dma);
 
 
 
 
 
 831
 832	if (chip->node) {
 833		int i;
 834		for (i = 0; i < 3; i++) {
 835			if (chip->requested & (1 << i))
 836				release_mem_region(chip->rsrc[i].start,
 837						   resource_size(&chip->rsrc[i]));
 838		}
 839	}
 840
 841	pci_dev_put(chip->pdev);
 
 842	of_node_put(chip->node);
 843	kfree(chip);
 844	return 0;
 845}
 846
 847
 848/*
 849 * free the device
 850 */
 851static int snd_pmac_dev_free(struct snd_device *device)
 852{
 853	struct snd_pmac *chip = device->device_data;
 854	return snd_pmac_free(chip);
 855}
 856
 857
 858/*
 859 * check the machine support byteswap (little-endian)
 860 */
 861
 862static void detect_byte_swap(struct snd_pmac *chip)
 863{
 864	struct device_node *mio;
 865
 866	/* if seems that Keylargo can't byte-swap  */
 867	for (mio = chip->node->parent; mio; mio = mio->parent) {
 868		if (of_node_name_eq(mio, "mac-io")) {
 869			if (of_device_is_compatible(mio, "Keylargo"))
 870				chip->can_byte_swap = 0;
 871			break;
 872		}
 873	}
 874
 875	/* it seems the Pismo & iBook can't byte-swap in hardware. */
 876	if (of_machine_is_compatible("PowerBook3,1") ||
 877	    of_machine_is_compatible("PowerBook2,1"))
 878		chip->can_byte_swap = 0 ;
 879
 880	if (of_machine_is_compatible("PowerBook2,1"))
 881		chip->can_duplex = 0;
 882}
 883
 884
 885/*
 886 * detect a sound chip
 887 */
 888static int snd_pmac_detect(struct snd_pmac *chip)
 889{
 890	struct device_node *sound;
 891	struct device_node *dn;
 892	const unsigned int *prop;
 893	unsigned int l;
 894	struct macio_chip* macio;
 895
 896	if (!machine_is(powermac))
 897		return -ENODEV;
 898
 899	chip->subframe = 0;
 900	chip->revision = 0;
 901	chip->freqs_ok = 0xff; /* all ok */
 902	chip->model = PMAC_AWACS;
 903	chip->can_byte_swap = 1;
 904	chip->can_duplex = 1;
 905	chip->can_capture = 1;
 906	chip->num_freqs = ARRAY_SIZE(awacs_freqs);
 907	chip->freq_table = awacs_freqs;
 908	chip->pdev = NULL;
 909
 910	chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
 911
 912	/* check machine type */
 913	if (of_machine_is_compatible("AAPL,3400/2400")
 914	    || of_machine_is_compatible("AAPL,3500"))
 915		chip->is_pbook_3400 = 1;
 916	else if (of_machine_is_compatible("PowerBook1,1")
 917		 || of_machine_is_compatible("AAPL,PowerBook1998"))
 918		chip->is_pbook_G3 = 1;
 919	chip->node = of_find_node_by_name(NULL, "awacs");
 920	sound = of_node_get(chip->node);
 921
 922	/*
 923	 * powermac G3 models have a node called "davbus"
 924	 * with a child called "sound".
 925	 */
 926	if (!chip->node)
 927		chip->node = of_find_node_by_name(NULL, "davbus");
 928	/*
 929	 * if we didn't find a davbus device, try 'i2s-a' since
 930	 * this seems to be what iBooks have
 931	 */
 932	if (! chip->node) {
 933		chip->node = of_find_node_by_name(NULL, "i2s-a");
 934		if (chip->node && chip->node->parent &&
 935		    chip->node->parent->parent) {
 936			if (of_device_is_compatible(chip->node->parent->parent,
 937						 "K2-Keylargo"))
 938				chip->is_k2 = 1;
 939		}
 940	}
 941	if (! chip->node)
 942		return -ENODEV;
 943
 944	if (!sound) {
 945		for_each_node_by_name(sound, "sound")
 946			if (sound->parent == chip->node)
 947				break;
 948	}
 949	if (! sound) {
 950		of_node_put(chip->node);
 951		chip->node = NULL;
 952		return -ENODEV;
 953	}
 954	prop = of_get_property(sound, "sub-frame", NULL);
 955	if (prop && *prop < 16)
 956		chip->subframe = *prop;
 957	prop = of_get_property(sound, "layout-id", NULL);
 958	if (prop) {
 959		/* partly deprecate snd-powermac, for those machines
 960		 * that have a layout-id property for now */
 961		dev_info(chip->card->dev,
 962			 "snd-powermac no longer handles any machines with a layout-id property in the device-tree, use snd-aoa.\n");
 
 963		of_node_put(sound);
 964		of_node_put(chip->node);
 965		chip->node = NULL;
 966		return -ENODEV;
 967	}
 968	/* This should be verified on older screamers */
 969	if (of_device_is_compatible(sound, "screamer")) {
 970		chip->model = PMAC_SCREAMER;
 971		// chip->can_byte_swap = 0; /* FIXME: check this */
 972	}
 973	if (of_device_is_compatible(sound, "burgundy")) {
 974		chip->model = PMAC_BURGUNDY;
 975		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
 976	}
 977	if (of_device_is_compatible(sound, "daca")) {
 978		chip->model = PMAC_DACA;
 979		chip->can_capture = 0;  /* no capture */
 980		chip->can_duplex = 0;
 981		// chip->can_byte_swap = 0; /* FIXME: check this */
 982		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
 983	}
 984	if (of_device_is_compatible(sound, "tumbler")) {
 985		chip->model = PMAC_TUMBLER;
 986		chip->can_capture = of_machine_is_compatible("PowerMac4,2")
 987				|| of_machine_is_compatible("PowerBook3,2")
 988				|| of_machine_is_compatible("PowerBook3,3")
 989				|| of_machine_is_compatible("PowerBook4,1")
 990				|| of_machine_is_compatible("PowerBook4,2")
 991				|| of_machine_is_compatible("PowerBook4,3");
 992		chip->can_duplex = 0;
 993		// chip->can_byte_swap = 0; /* FIXME: check this */
 994		chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
 995		chip->freq_table = tumbler_freqs;
 996		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
 997	}
 998	if (of_device_is_compatible(sound, "snapper")) {
 999		chip->model = PMAC_SNAPPER;
1000		// chip->can_byte_swap = 0; /* FIXME: check this */
1001		chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
1002		chip->freq_table = tumbler_freqs;
1003		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1004	}
1005	prop = of_get_property(sound, "device-id", NULL);
1006	if (prop)
1007		chip->device_id = *prop;
1008	dn = of_find_node_by_name(NULL, "perch");
1009	chip->has_iic = (dn != NULL);
1010	of_node_put(dn);
1011
1012	/* We need the PCI device for DMA allocations, let's use a crude method
1013	 * for now ...
1014	 */
1015	macio = macio_find(chip->node, macio_unknown);
1016	if (macio == NULL)
1017		dev_warn(chip->card->dev, "snd-powermac: can't locate macio !\n");
1018	else {
1019		struct pci_dev *pdev = NULL;
1020
1021		for_each_pci_dev(pdev) {
1022			struct device_node *np = pci_device_to_OF_node(pdev);
1023			if (np && np == macio->of_node) {
1024				chip->pdev = pdev;
1025				break;
1026			}
1027		}
1028	}
1029	if (chip->pdev == NULL)
1030		dev_warn(chip->card->dev,
1031			 "snd-powermac: can't locate macio PCI device !\n");
1032
1033	detect_byte_swap(chip);
1034
1035	/* look for a property saying what sample rates
1036	   are available */
1037	prop = of_get_property(sound, "sample-rates", &l);
1038	if (! prop)
1039		prop = of_get_property(sound, "output-frame-rates", &l);
1040	if (prop) {
1041		int i;
1042		chip->freqs_ok = 0;
1043		for (l /= sizeof(int); l > 0; --l) {
1044			unsigned int r = *prop++;
1045			/* Apple 'Fixed' format */
1046			if (r >= 0x10000)
1047				r >>= 16;
1048			for (i = 0; i < chip->num_freqs; ++i) {
1049				if (r == chip->freq_table[i]) {
1050					chip->freqs_ok |= (1 << i);
1051					break;
1052				}
1053			}
1054		}
1055	} else {
1056		/* assume only 44.1khz */
1057		chip->freqs_ok = 1;
1058	}
1059
1060	of_node_put(sound);
1061	return 0;
1062}
1063
1064#ifdef PMAC_SUPPORT_AUTOMUTE
1065/*
1066 * auto-mute
1067 */
1068static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1069			      struct snd_ctl_elem_value *ucontrol)
1070{
1071	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1072	ucontrol->value.integer.value[0] = chip->auto_mute;
1073	return 0;
1074}
1075
1076static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1077			      struct snd_ctl_elem_value *ucontrol)
1078{
1079	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1080	if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1081		chip->auto_mute = !!ucontrol->value.integer.value[0];
1082		if (chip->update_automute)
1083			chip->update_automute(chip, 1);
1084		return 1;
1085	}
1086	return 0;
1087}
1088
1089static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1090			      struct snd_ctl_elem_value *ucontrol)
1091{
1092	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1093	if (chip->detect_headphone)
1094		ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1095	else
1096		ucontrol->value.integer.value[0] = 0;
1097	return 0;
1098}
1099
1100static const struct snd_kcontrol_new auto_mute_controls[] = {
1101	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1102	  .name = "Auto Mute Switch",
1103	  .info = snd_pmac_boolean_mono_info,
1104	  .get = pmac_auto_mute_get,
1105	  .put = pmac_auto_mute_put,
1106	},
1107	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1108	  .name = "Headphone Detection",
1109	  .access = SNDRV_CTL_ELEM_ACCESS_READ,
1110	  .info = snd_pmac_boolean_mono_info,
1111	  .get = pmac_hp_detect_get,
1112	},
1113};
1114
1115int snd_pmac_add_automute(struct snd_pmac *chip)
1116{
1117	int err;
1118	chip->auto_mute = 1;
1119	err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1120	if (err < 0) {
1121		dev_err(chip->card->dev,
1122			"snd-powermac: Failed to add automute control\n");
1123		return err;
1124	}
1125	chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1126	return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1127}
1128#endif /* PMAC_SUPPORT_AUTOMUTE */
1129
1130/*
1131 * create and detect a pmac chip record
1132 */
1133int snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
1134{
1135	struct snd_pmac *chip;
1136	struct device_node *np;
1137	int i, err;
1138	unsigned int irq;
1139	unsigned long ctrl_addr, txdma_addr, rxdma_addr;
1140	static const struct snd_device_ops ops = {
1141		.dev_free =	snd_pmac_dev_free,
1142	};
1143
1144	*chip_return = NULL;
1145
1146	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1147	if (chip == NULL)
1148		return -ENOMEM;
1149	chip->card = card;
1150
1151	spin_lock_init(&chip->reg_lock);
1152	chip->irq = chip->tx_irq = chip->rx_irq = -1;
1153
1154	chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1155	chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1156
1157	err = snd_pmac_detect(chip);
1158	if (err < 0)
1159		goto __error;
1160
1161	if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1162	    snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1163	    snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0 ||
1164	    snd_pmac_dbdma_alloc(chip, &emergency_dbdma, 2) < 0) {
1165		err = -ENOMEM;
1166		goto __error;
1167	}
1168
1169	np = chip->node;
1170	chip->requested = 0;
1171	if (chip->is_k2) {
1172		static const char * const rnames[] = {
1173			"Sound Control", "Sound DMA" };
1174		for (i = 0; i < 2; i ++) {
1175			if (of_address_to_resource(np->parent, i,
1176						   &chip->rsrc[i])) {
1177				dev_err(chip->card->dev,
1178					"snd: can't translate rsrc %d (%s)\n",
1179					i, rnames[i]);
1180				err = -ENODEV;
1181				goto __error;
1182			}
1183			if (request_mem_region(chip->rsrc[i].start,
1184					       resource_size(&chip->rsrc[i]),
1185					       rnames[i]) == NULL) {
1186				dev_err(chip->card->dev,
1187					"snd: can't request rsrc %d (%s: %pR)\n",
1188					i, rnames[i], &chip->rsrc[i]);
1189				err = -ENODEV;
1190				goto __error;
1191			}
1192			chip->requested |= (1 << i);
1193		}
1194		ctrl_addr = chip->rsrc[0].start;
1195		txdma_addr = chip->rsrc[1].start;
1196		rxdma_addr = txdma_addr + 0x100;
1197	} else {
1198		static const char * const rnames[] = {
1199			"Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
1200		for (i = 0; i < 3; i ++) {
1201			if (of_address_to_resource(np, i,
1202						   &chip->rsrc[i])) {
1203				dev_err(chip->card->dev,
1204					"snd: can't translate rsrc %d (%s)\n",
1205					i, rnames[i]);
1206				err = -ENODEV;
1207				goto __error;
1208			}
1209			if (request_mem_region(chip->rsrc[i].start,
1210					       resource_size(&chip->rsrc[i]),
1211					       rnames[i]) == NULL) {
1212				dev_err(chip->card->dev,
1213					"snd: can't request rsrc %d (%s: %pR)\n",
1214					i, rnames[i], &chip->rsrc[i]);
1215				err = -ENODEV;
1216				goto __error;
1217			}
1218			chip->requested |= (1 << i);
1219		}
1220		ctrl_addr = chip->rsrc[0].start;
1221		txdma_addr = chip->rsrc[1].start;
1222		rxdma_addr = chip->rsrc[2].start;
1223	}
1224
1225	chip->awacs = ioremap(ctrl_addr, 0x1000);
1226	chip->playback.dma = ioremap(txdma_addr, 0x100);
1227	chip->capture.dma = ioremap(rxdma_addr, 0x100);
1228	if (chip->model <= PMAC_BURGUNDY) {
1229		irq = irq_of_parse_and_map(np, 0);
1230		if (request_irq(irq, snd_pmac_ctrl_intr, 0,
1231				"PMac", (void*)chip)) {
1232			dev_err(chip->card->dev,
1233				"pmac: unable to grab IRQ %d\n", irq);
1234			err = -EBUSY;
1235			goto __error;
1236		}
1237		chip->irq = irq;
1238	}
1239	irq = irq_of_parse_and_map(np, 1);
1240	if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
1241		dev_err(chip->card->dev, "pmac: unable to grab IRQ %d\n", irq);
1242		err = -EBUSY;
1243		goto __error;
1244	}
1245	chip->tx_irq = irq;
1246	irq = irq_of_parse_and_map(np, 2);
1247	if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
1248		dev_err(chip->card->dev, "pmac: unable to grab IRQ %d\n", irq);
1249		err = -EBUSY;
1250		goto __error;
1251	}
1252	chip->rx_irq = irq;
1253
1254	snd_pmac_sound_feature(chip, 1);
1255
1256	/* reset & enable interrupts */
1257	if (chip->model <= PMAC_BURGUNDY)
1258		out_le32(&chip->awacs->control, chip->control_mask);
1259
1260	/* Powerbooks have odd ways of enabling inputs such as
1261	   an expansion-bay CD or sound from an internal modem
1262	   or a PC-card modem. */
1263	if (chip->is_pbook_3400) {
1264		/* Enable CD and PC-card sound inputs. */
1265		/* This is done by reading from address
1266		 * f301a000, + 0x10 to enable the expansion-bay
1267		 * CD sound input, + 0x80 to enable the PC-card
1268		 * sound input.  The 0x100 enables the SCSI bus
1269		 * terminator power.
1270		 */
1271		chip->latch_base = ioremap (0xf301a000, 0x1000);
1272		in_8(chip->latch_base + 0x190);
1273	} else if (chip->is_pbook_G3) {
1274		struct device_node* mio;
1275		for (mio = chip->node->parent; mio; mio = mio->parent) {
1276			if (of_node_name_eq(mio, "mac-io")) {
1277				struct resource r;
1278				if (of_address_to_resource(mio, 0, &r) == 0)
1279					chip->macio_base =
1280						ioremap(r.start, 0x40);
1281				break;
1282			}
1283		}
1284		/* Enable CD sound input. */
1285		/* The relevant bits for writing to this byte are 0x8f.
1286		 * I haven't found out what the 0x80 bit does.
1287		 * For the 0xf bits, writing 3 or 7 enables the CD
1288		 * input, any other value disables it.  Values
1289		 * 1, 3, 5, 7 enable the microphone.  Values 0, 2,
1290		 * 4, 6, 8 - f enable the input from the modem.
1291		 */
1292		if (chip->macio_base)
1293			out_8(chip->macio_base + 0x37, 3);
1294	}
1295
1296	/* Reset dbdma channels */
1297	snd_pmac_dbdma_reset(chip);
1298
1299	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1300	if (err < 0)
1301		goto __error;
1302
1303	*chip_return = chip;
1304	return 0;
1305
1306 __error:
1307	snd_pmac_free(chip);
1308	return err;
1309}
1310
1311
1312/*
1313 * sleep notify for powerbook
1314 */
1315
1316#ifdef CONFIG_PM
1317
1318/*
1319 * Save state when going to sleep, restore it afterwards.
1320 */
1321
1322void snd_pmac_suspend(struct snd_pmac *chip)
1323{
1324	unsigned long flags;
1325
1326	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1327	if (chip->suspend)
1328		chip->suspend(chip);
 
1329	spin_lock_irqsave(&chip->reg_lock, flags);
1330	snd_pmac_beep_stop(chip);
1331	spin_unlock_irqrestore(&chip->reg_lock, flags);
1332	if (chip->irq >= 0)
1333		disable_irq(chip->irq);
1334	if (chip->tx_irq >= 0)
1335		disable_irq(chip->tx_irq);
1336	if (chip->rx_irq >= 0)
1337		disable_irq(chip->rx_irq);
1338	snd_pmac_sound_feature(chip, 0);
1339}
1340
1341void snd_pmac_resume(struct snd_pmac *chip)
1342{
1343	snd_pmac_sound_feature(chip, 1);
1344	if (chip->resume)
1345		chip->resume(chip);
1346	/* enable CD sound input */
1347	if (chip->macio_base && chip->is_pbook_G3)
1348		out_8(chip->macio_base + 0x37, 3);
1349	else if (chip->is_pbook_3400)
1350		in_8(chip->latch_base + 0x190);
1351
1352	snd_pmac_pcm_set_format(chip);
1353
1354	if (chip->irq >= 0)
1355		enable_irq(chip->irq);
1356	if (chip->tx_irq >= 0)
1357		enable_irq(chip->tx_irq);
1358	if (chip->rx_irq >= 0)
1359		enable_irq(chip->rx_irq);
1360
1361	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1362}
1363
1364#endif /* CONFIG_PM */
1365
v3.1
 
   1/*
   2 * PMac DBDMA lowlevel functions
   3 *
   4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
   5 * code based on dmasound.c.
   6 *
   7 *   This program is free software; you can redistribute it and/or modify
   8 *   it under the terms of the GNU General Public License as published by
   9 *   the Free Software Foundation; either version 2 of the License, or
  10 *   (at your option) any later version.
  11 *
  12 *   This program is distributed in the hope that it will be useful,
  13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 *   GNU General Public License for more details.
  16 *
  17 *   You should have received a copy of the GNU General Public License
  18 *   along with this program; if not, write to the Free Software
  19 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20 */
  21
  22
  23#include <asm/io.h>
  24#include <asm/irq.h>
  25#include <linux/init.h>
  26#include <linux/delay.h>
  27#include <linux/slab.h>
  28#include <linux/interrupt.h>
  29#include <linux/pci.h>
  30#include <linux/dma-mapping.h>
 
 
  31#include <sound/core.h>
  32#include "pmac.h"
  33#include <sound/pcm_params.h>
  34#include <asm/pmac_feature.h>
  35#include <asm/pci-bridge.h>
  36
  37
  38/* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
  39static int awacs_freqs[8] = {
  40	44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
  41};
  42/* fixed frequency table for tumbler */
  43static int tumbler_freqs[1] = {
  44	44100
  45};
  46
  47
  48/*
  49 * we will allocate a single 'emergency' dbdma cmd block to use if the
  50 * tx status comes up "DEAD".  This happens on some PowerComputing Pmac
  51 * clones, either owing to a bug in dbdma or some interaction between
  52 * IDE and sound.  However, this measure would deal with DEAD status if
  53 * it appeared elsewhere.
  54 */
  55static struct pmac_dbdma emergency_dbdma;
  56static int emergency_in_use;
  57
  58
  59/*
  60 * allocate DBDMA command arrays
  61 */
  62static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
  63{
  64	unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);
  65
  66	rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
  67					&rec->dma_base, GFP_KERNEL);
  68	if (rec->space == NULL)
  69		return -ENOMEM;
  70	rec->size = size;
  71	memset(rec->space, 0, rsize);
  72	rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
  73	rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
  74
  75	return 0;
  76}
  77
  78static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
  79{
  80	if (rec->space) {
  81		unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);
  82
  83		dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
  84	}
  85}
  86
  87
  88/*
  89 * pcm stuff
  90 */
  91
  92/*
  93 * look up frequency table
  94 */
  95
  96unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
  97{
  98	int i, ok, found;
  99
 100	ok = rec->cur_freqs;
 101	if (rate > chip->freq_table[0])
 102		return 0;
 103	found = 0;
 104	for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
 105		if (! (ok & 1)) continue;
 106		found = i;
 107		if (rate >= chip->freq_table[i])
 108			break;
 109	}
 110	return found;
 111}
 112
 113/*
 114 * check whether another stream is active
 115 */
 116static inline int another_stream(int stream)
 117{
 118	return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
 119		SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
 120}
 121
 122/*
 123 * allocate buffers
 124 */
 125static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs,
 126				  struct snd_pcm_hw_params *hw_params)
 127{
 128	return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
 129}
 130
 131/*
 132 * release buffers
 133 */
 134static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs)
 135{
 136	snd_pcm_lib_free_pages(subs);
 137	return 0;
 138}
 139
 140/*
 141 * get a stream of the opposite direction
 142 */
 143static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
 144{
 145	switch (stream) {
 146	case SNDRV_PCM_STREAM_PLAYBACK:
 147		return &chip->playback;
 148	case SNDRV_PCM_STREAM_CAPTURE:
 149		return &chip->capture;
 150	default:
 151		snd_BUG();
 152		return NULL;
 153	}
 154}
 155
 156/*
 157 * wait while run status is on
 158 */
 159static inline void
 160snd_pmac_wait_ack(struct pmac_stream *rec)
 161{
 162	int timeout = 50000;
 163	while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
 164		udelay(1);
 165}
 166
 167/*
 168 * set the format and rate to the chip.
 169 * call the lowlevel function if defined (e.g. for AWACS).
 170 */
 171static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
 172{
 173	/* set up frequency and format */
 174	out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
 175	out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
 176	if (chip->set_format)
 177		chip->set_format(chip);
 178}
 179
 180/*
 181 * stop the DMA transfer
 182 */
 183static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
 184{
 185	out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
 186	snd_pmac_wait_ack(rec);
 187}
 188
 189/*
 190 * set the command pointer address
 191 */
 192static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
 193{
 194	out_le32(&rec->dma->cmdptr, cmd->addr);
 195}
 196
 197/*
 198 * start the DMA
 199 */
 200static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
 201{
 202	out_le32(&rec->dma->control, status | (status << 16));
 203}
 204
 205
 206/*
 207 * prepare playback/capture stream
 208 */
 209static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
 210{
 211	int i;
 212	volatile struct dbdma_cmd __iomem *cp;
 213	struct snd_pcm_runtime *runtime = subs->runtime;
 214	int rate_index;
 215	long offset;
 216	struct pmac_stream *astr;
 217
 218	rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
 219	rec->period_size = snd_pcm_lib_period_bytes(subs);
 220	rec->nperiods = rec->dma_size / rec->period_size;
 221	rec->cur_period = 0;
 222	rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);
 223
 224	/* set up constraints */
 225	astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
 226	if (! astr)
 227		return -EINVAL;
 228	astr->cur_freqs = 1 << rate_index;
 229	astr->cur_formats = 1 << runtime->format;
 230	chip->rate_index = rate_index;
 231	chip->format = runtime->format;
 232
 233	/* We really want to execute a DMA stop command, after the AWACS
 234	 * is initialized.
 235	 * For reasons I don't understand, it stops the hissing noise
 236	 * common to many PowerBook G3 systems and random noise otherwise
 237	 * captured on iBook2's about every third time. -ReneR
 238	 */
 239	spin_lock_irq(&chip->reg_lock);
 240	snd_pmac_dma_stop(rec);
 241	st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
 242	snd_pmac_dma_set_command(rec, &chip->extra_dma);
 243	snd_pmac_dma_run(rec, RUN);
 244	spin_unlock_irq(&chip->reg_lock);
 245	mdelay(5);
 246	spin_lock_irq(&chip->reg_lock);
 247	/* continuous DMA memory type doesn't provide the physical address,
 248	 * so we need to resolve the address here...
 249	 */
 250	offset = runtime->dma_addr;
 251	for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
 252		st_le32(&cp->phy_addr, offset);
 253		st_le16(&cp->req_count, rec->period_size);
 254		/*st_le16(&cp->res_count, 0);*/
 255		st_le16(&cp->xfer_status, 0);
 256		offset += rec->period_size;
 257	}
 258	/* make loop */
 259	st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
 260	st_le32(&cp->cmd_dep, rec->cmd.addr);
 261
 262	snd_pmac_dma_stop(rec);
 263	snd_pmac_dma_set_command(rec, &rec->cmd);
 264	spin_unlock_irq(&chip->reg_lock);
 265
 266	return 0;
 267}
 268
 269
 270/*
 271 * PCM trigger/stop
 272 */
 273static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
 274				struct snd_pcm_substream *subs, int cmd)
 275{
 276	volatile struct dbdma_cmd __iomem *cp;
 277	int i, command;
 278
 279	switch (cmd) {
 280	case SNDRV_PCM_TRIGGER_START:
 281	case SNDRV_PCM_TRIGGER_RESUME:
 282		if (rec->running)
 283			return -EBUSY;
 284		command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
 285			   OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
 286		spin_lock(&chip->reg_lock);
 287		snd_pmac_beep_stop(chip);
 288		snd_pmac_pcm_set_format(chip);
 289		for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
 290			out_le16(&cp->command, command);
 291		snd_pmac_dma_set_command(rec, &rec->cmd);
 292		(void)in_le32(&rec->dma->status);
 293		snd_pmac_dma_run(rec, RUN|WAKE);
 294		rec->running = 1;
 295		spin_unlock(&chip->reg_lock);
 296		break;
 297
 298	case SNDRV_PCM_TRIGGER_STOP:
 299	case SNDRV_PCM_TRIGGER_SUSPEND:
 300		spin_lock(&chip->reg_lock);
 301		rec->running = 0;
 302		/*printk(KERN_DEBUG "stopped!!\n");*/
 303		snd_pmac_dma_stop(rec);
 304		for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
 305			out_le16(&cp->command, DBDMA_STOP);
 306		spin_unlock(&chip->reg_lock);
 307		break;
 308
 309	default:
 310		return -EINVAL;
 311	}
 312
 313	return 0;
 314}
 315
 316/*
 317 * return the current pointer
 318 */
 319inline
 320static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
 321					      struct pmac_stream *rec,
 322					      struct snd_pcm_substream *subs)
 323{
 324	int count = 0;
 325
 326#if 1 /* hmm.. how can we get the current dma pointer?? */
 327	int stat;
 328	volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
 329	stat = ld_le16(&cp->xfer_status);
 330	if (stat & (ACTIVE|DEAD)) {
 331		count = in_le16(&cp->res_count);
 332		if (count)
 333			count = rec->period_size - count;
 334	}
 335#endif
 336	count += rec->cur_period * rec->period_size;
 337	/*printk(KERN_DEBUG "pointer=%d\n", count);*/
 338	return bytes_to_frames(subs->runtime, count);
 339}
 340
 341/*
 342 * playback
 343 */
 344
 345static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
 346{
 347	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 348	return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
 349}
 350
 351static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
 352				     int cmd)
 353{
 354	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 355	return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
 356}
 357
 358static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
 359{
 360	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 361	return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
 362}
 363
 364
 365/*
 366 * capture
 367 */
 368
 369static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
 370{
 371	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 372	return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
 373}
 374
 375static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
 376				    int cmd)
 377{
 378	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 379	return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
 380}
 381
 382static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
 383{
 384	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 385	return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
 386}
 387
 388
 389/*
 390 * Handle DEAD DMA transfers:
 391 * if the TX status comes up "DEAD" - reported on some Power Computing machines
 392 * we need to re-start the dbdma - but from a different physical start address
 393 * and with a different transfer length.  It would get very messy to do this
 394 * with the normal dbdma_cmd blocks - we would have to re-write the buffer start
 395 * addresses each time.  So, we will keep a single dbdma_cmd block which can be
 396 * fiddled with.
 397 * When DEAD status is first reported the content of the faulted dbdma block is
 398 * copied into the emergency buffer and we note that the buffer is in use.
 399 * we then bump the start physical address by the amount that was successfully
 400 * output before it died.
 401 * On any subsequent DEAD result we just do the bump-ups (we know that we are
 402 * already using the emergency dbdma_cmd).
 403 * CHECK: this just tries to "do it".  It is possible that we should abandon
 404 * xfers when the number of residual bytes gets below a certain value - I can
 405 * see that this might cause a loop-forever if a too small transfer causes
 406 * DEAD status.  However this is a TODO for now - we'll see what gets reported.
 407 * When we get a successful transfer result with the emergency buffer we just
 408 * pretend that it completed using the original dmdma_cmd and carry on.  The
 409 * 'next_cmd' field will already point back to the original loop of blocks.
 410 */
 411static inline void snd_pmac_pcm_dead_xfer(struct pmac_stream *rec,
 412					  volatile struct dbdma_cmd __iomem *cp)
 413{
 414	unsigned short req, res ;
 415	unsigned int phy ;
 416
 417	/* printk(KERN_WARNING "snd-powermac: DMA died - patching it up!\n"); */
 418
 419	/* to clear DEAD status we must first clear RUN
 420	   set it to quiescent to be on the safe side */
 421	(void)in_le32(&rec->dma->status);
 422	out_le32(&rec->dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
 423
 424	if (!emergency_in_use) { /* new problem */
 425		memcpy((void *)emergency_dbdma.cmds, (void *)cp,
 426		       sizeof(struct dbdma_cmd));
 427		emergency_in_use = 1;
 428		st_le16(&cp->xfer_status, 0);
 429		st_le16(&cp->req_count, rec->period_size);
 430		cp = emergency_dbdma.cmds;
 431	}
 432
 433	/* now bump the values to reflect the amount
 434	   we haven't yet shifted */
 435	req = ld_le16(&cp->req_count);
 436	res = ld_le16(&cp->res_count);
 437	phy = ld_le32(&cp->phy_addr);
 438	phy += (req - res);
 439	st_le16(&cp->req_count, res);
 440	st_le16(&cp->res_count, 0);
 441	st_le16(&cp->xfer_status, 0);
 442	st_le32(&cp->phy_addr, phy);
 443
 444	st_le32(&cp->cmd_dep, rec->cmd.addr
 445		+ sizeof(struct dbdma_cmd)*((rec->cur_period+1)%rec->nperiods));
 446
 447	st_le16(&cp->command, OUTPUT_MORE | BR_ALWAYS | INTR_ALWAYS);
 448
 449	/* point at our patched up command block */
 450	out_le32(&rec->dma->cmdptr, emergency_dbdma.addr);
 451
 452	/* we must re-start the controller */
 453	(void)in_le32(&rec->dma->status);
 454	/* should complete clearing the DEAD status */
 455	out_le32(&rec->dma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
 456}
 457
 458/*
 459 * update playback/capture pointer from interrupts
 460 */
 461static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
 462{
 463	volatile struct dbdma_cmd __iomem *cp;
 464	int c;
 465	int stat;
 466
 467	spin_lock(&chip->reg_lock);
 468	if (rec->running) {
 469		for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
 470
 471			if (emergency_in_use)   /* already using DEAD xfer? */
 472				cp = emergency_dbdma.cmds;
 473			else
 474				cp = &rec->cmd.cmds[rec->cur_period];
 475
 476			stat = ld_le16(&cp->xfer_status);
 477
 478			if (stat & DEAD) {
 479				snd_pmac_pcm_dead_xfer(rec, cp);
 480				break; /* this block is still going */
 481			}
 482
 483			if (emergency_in_use)
 484				emergency_in_use = 0 ; /* done that */
 485
 486			if (! (stat & ACTIVE))
 487				break;
 488
 489			/*printk(KERN_DEBUG "update frag %d\n", rec->cur_period);*/
 490			st_le16(&cp->xfer_status, 0);
 491			st_le16(&cp->req_count, rec->period_size);
 492			/*st_le16(&cp->res_count, 0);*/
 493			rec->cur_period++;
 494			if (rec->cur_period >= rec->nperiods) {
 495				rec->cur_period = 0;
 496			}
 497
 498			spin_unlock(&chip->reg_lock);
 499			snd_pcm_period_elapsed(rec->substream);
 500			spin_lock(&chip->reg_lock);
 501		}
 502	}
 503	spin_unlock(&chip->reg_lock);
 504}
 505
 506
 507/*
 508 * hw info
 509 */
 510
 511static struct snd_pcm_hardware snd_pmac_playback =
 512{
 513	.info =			(SNDRV_PCM_INFO_INTERLEAVED |
 514				 SNDRV_PCM_INFO_MMAP |
 515				 SNDRV_PCM_INFO_MMAP_VALID |
 516				 SNDRV_PCM_INFO_RESUME),
 517	.formats =		SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
 518	.rates =		SNDRV_PCM_RATE_8000_44100,
 519	.rate_min =		7350,
 520	.rate_max =		44100,
 521	.channels_min =		2,
 522	.channels_max =		2,
 523	.buffer_bytes_max =	131072,
 524	.period_bytes_min =	256,
 525	.period_bytes_max =	16384,
 526	.periods_min =		3,
 527	.periods_max =		PMAC_MAX_FRAGS,
 528};
 529
 530static struct snd_pcm_hardware snd_pmac_capture =
 531{
 532	.info =			(SNDRV_PCM_INFO_INTERLEAVED |
 533				 SNDRV_PCM_INFO_MMAP |
 534				 SNDRV_PCM_INFO_MMAP_VALID |
 535				 SNDRV_PCM_INFO_RESUME),
 536	.formats =		SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
 537	.rates =		SNDRV_PCM_RATE_8000_44100,
 538	.rate_min =		7350,
 539	.rate_max =		44100,
 540	.channels_min =		2,
 541	.channels_max =		2,
 542	.buffer_bytes_max =	131072,
 543	.period_bytes_min =	256,
 544	.period_bytes_max =	16384,
 545	.periods_min =		3,
 546	.periods_max =		PMAC_MAX_FRAGS,
 547};
 548
 549
 550#if 0 // NYI
 551static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
 552				 struct snd_pcm_hw_rule *rule)
 553{
 554	struct snd_pmac *chip = rule->private;
 555	struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
 556	int i, freq_table[8], num_freqs;
 557
 558	if (! rec)
 559		return -EINVAL;
 560	num_freqs = 0;
 561	for (i = chip->num_freqs - 1; i >= 0; i--) {
 562		if (rec->cur_freqs & (1 << i))
 563			freq_table[num_freqs++] = chip->freq_table[i];
 564	}
 565
 566	return snd_interval_list(hw_param_interval(params, rule->var),
 567				 num_freqs, freq_table, 0);
 568}
 569
 570static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
 571				   struct snd_pcm_hw_rule *rule)
 572{
 573	struct snd_pmac *chip = rule->private;
 574	struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
 575
 576	if (! rec)
 577		return -EINVAL;
 578	return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
 579				   rec->cur_formats);
 580}
 581#endif // NYI
 582
 583static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
 584			     struct snd_pcm_substream *subs)
 585{
 586	struct snd_pcm_runtime *runtime = subs->runtime;
 587	int i;
 588
 589	/* look up frequency table and fill bit mask */
 590	runtime->hw.rates = 0;
 591	for (i = 0; i < chip->num_freqs; i++)
 592		if (chip->freqs_ok & (1 << i))
 593			runtime->hw.rates |=
 594				snd_pcm_rate_to_rate_bit(chip->freq_table[i]);
 595
 596	/* check for minimum and maximum rates */
 597	for (i = 0; i < chip->num_freqs; i++) {
 598		if (chip->freqs_ok & (1 << i)) {
 599			runtime->hw.rate_max = chip->freq_table[i];
 600			break;
 601		}
 602	}
 603	for (i = chip->num_freqs - 1; i >= 0; i--) {
 604		if (chip->freqs_ok & (1 << i)) {
 605			runtime->hw.rate_min = chip->freq_table[i];
 606			break;
 607		}
 608	}
 609	runtime->hw.formats = chip->formats_ok;
 610	if (chip->can_capture) {
 611		if (! chip->can_duplex)
 612			runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
 613		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
 614	}
 615	runtime->private_data = rec;
 616	rec->substream = subs;
 617
 618#if 0 /* FIXME: still under development.. */
 619	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
 620			    snd_pmac_hw_rule_rate, chip, rec->stream, -1);
 621	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
 622			    snd_pmac_hw_rule_format, chip, rec->stream, -1);
 623#endif
 624
 625	runtime->hw.periods_max = rec->cmd.size - 1;
 626
 627	/* constraints to fix choppy sound */
 628	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
 629	return 0;
 630}
 631
 632static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
 633			      struct snd_pcm_substream *subs)
 634{
 635	struct pmac_stream *astr;
 636
 637	snd_pmac_dma_stop(rec);
 638
 639	astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
 640	if (! astr)
 641		return -EINVAL;
 642
 643	/* reset constraints */
 644	astr->cur_freqs = chip->freqs_ok;
 645	astr->cur_formats = chip->formats_ok;
 646
 647	return 0;
 648}
 649
 650static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
 651{
 652	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 653
 654	subs->runtime->hw = snd_pmac_playback;
 655	return snd_pmac_pcm_open(chip, &chip->playback, subs);
 656}
 657
 658static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
 659{
 660	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 661
 662	subs->runtime->hw = snd_pmac_capture;
 663	return snd_pmac_pcm_open(chip, &chip->capture, subs);
 664}
 665
 666static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
 667{
 668	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 669
 670	return snd_pmac_pcm_close(chip, &chip->playback, subs);
 671}
 672
 673static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
 674{
 675	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
 676
 677	return snd_pmac_pcm_close(chip, &chip->capture, subs);
 678}
 679
 680/*
 681 */
 682
 683static struct snd_pcm_ops snd_pmac_playback_ops = {
 684	.open =		snd_pmac_playback_open,
 685	.close =	snd_pmac_playback_close,
 686	.ioctl =	snd_pcm_lib_ioctl,
 687	.hw_params =	snd_pmac_pcm_hw_params,
 688	.hw_free =	snd_pmac_pcm_hw_free,
 689	.prepare =	snd_pmac_playback_prepare,
 690	.trigger =	snd_pmac_playback_trigger,
 691	.pointer =	snd_pmac_playback_pointer,
 692};
 693
 694static struct snd_pcm_ops snd_pmac_capture_ops = {
 695	.open =		snd_pmac_capture_open,
 696	.close =	snd_pmac_capture_close,
 697	.ioctl =	snd_pcm_lib_ioctl,
 698	.hw_params =	snd_pmac_pcm_hw_params,
 699	.hw_free =	snd_pmac_pcm_hw_free,
 700	.prepare =	snd_pmac_capture_prepare,
 701	.trigger =	snd_pmac_capture_trigger,
 702	.pointer =	snd_pmac_capture_pointer,
 703};
 704
 705int __devinit snd_pmac_pcm_new(struct snd_pmac *chip)
 706{
 707	struct snd_pcm *pcm;
 708	int err;
 709	int num_captures = 1;
 710
 711	if (! chip->can_capture)
 712		num_captures = 0;
 713	err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
 714	if (err < 0)
 715		return err;
 716
 717	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
 718	if (chip->can_capture)
 719		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);
 720
 721	pcm->private_data = chip;
 722	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
 723	strcpy(pcm->name, chip->card->shortname);
 724	chip->pcm = pcm;
 725
 726	chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
 727	if (chip->can_byte_swap)
 728		chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;
 729
 730	chip->playback.cur_formats = chip->formats_ok;
 731	chip->capture.cur_formats = chip->formats_ok;
 732	chip->playback.cur_freqs = chip->freqs_ok;
 733	chip->capture.cur_freqs = chip->freqs_ok;
 734
 735	/* preallocate 64k buffer */
 736	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
 737					      &chip->pdev->dev,
 738					      64 * 1024, 64 * 1024);
 739
 740	return 0;
 741}
 742
 743
 744static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
 745{
 746	out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
 747	snd_pmac_wait_ack(&chip->playback);
 748	out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
 749	snd_pmac_wait_ack(&chip->capture);
 750}
 751
 752
 753/*
 754 * handling beep
 755 */
 756void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
 757{
 758	struct pmac_stream *rec = &chip->playback;
 759
 760	snd_pmac_dma_stop(rec);
 761	st_le16(&chip->extra_dma.cmds->req_count, bytes);
 762	st_le16(&chip->extra_dma.cmds->xfer_status, 0);
 763	st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
 764	st_le32(&chip->extra_dma.cmds->phy_addr, addr);
 765	st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
 766	out_le32(&chip->awacs->control,
 767		 (in_le32(&chip->awacs->control) & ~0x1f00)
 768		 | (speed << 8));
 769	out_le32(&chip->awacs->byteswap, 0);
 770	snd_pmac_dma_set_command(rec, &chip->extra_dma);
 771	snd_pmac_dma_run(rec, RUN);
 772}
 773
 774void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
 775{
 776	snd_pmac_dma_stop(&chip->playback);
 777	st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
 778	snd_pmac_pcm_set_format(chip); /* reset format */
 779}
 780
 781
 782/*
 783 * interrupt handlers
 784 */
 785static irqreturn_t
 786snd_pmac_tx_intr(int irq, void *devid)
 787{
 788	struct snd_pmac *chip = devid;
 789	snd_pmac_pcm_update(chip, &chip->playback);
 790	return IRQ_HANDLED;
 791}
 792
 793
 794static irqreturn_t
 795snd_pmac_rx_intr(int irq, void *devid)
 796{
 797	struct snd_pmac *chip = devid;
 798	snd_pmac_pcm_update(chip, &chip->capture);
 799	return IRQ_HANDLED;
 800}
 801
 802
 803static irqreturn_t
 804snd_pmac_ctrl_intr(int irq, void *devid)
 805{
 806	struct snd_pmac *chip = devid;
 807	int ctrl = in_le32(&chip->awacs->control);
 808
 809	/*printk(KERN_DEBUG "pmac: control interrupt.. 0x%x\n", ctrl);*/
 810	if (ctrl & MASK_PORTCHG) {
 811		/* do something when headphone is plugged/unplugged? */
 812		if (chip->update_automute)
 813			chip->update_automute(chip, 1);
 814	}
 815	if (ctrl & MASK_CNTLERR) {
 816		int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
 817		if (err && chip->model <= PMAC_SCREAMER)
 818			snd_printk(KERN_DEBUG "error %x\n", err);
 819	}
 820	/* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
 821	out_le32(&chip->awacs->control, ctrl);
 822	return IRQ_HANDLED;
 823}
 824
 825
 826/*
 827 * a wrapper to feature call for compatibility
 828 */
 829static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
 830{
 831	if (ppc_md.feature_call)
 832		ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
 833}
 834
 835/*
 836 * release resources
 837 */
 838
 839static int snd_pmac_free(struct snd_pmac *chip)
 840{
 841	/* stop sounds */
 842	if (chip->initialized) {
 843		snd_pmac_dbdma_reset(chip);
 844		/* disable interrupts from awacs interface */
 845		out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
 846	}
 847
 848	if (chip->node)
 849		snd_pmac_sound_feature(chip, 0);
 850
 851	/* clean up mixer if any */
 852	if (chip->mixer_free)
 853		chip->mixer_free(chip);
 854
 855	snd_pmac_detach_beep(chip);
 856
 857	/* release resources */
 858	if (chip->irq >= 0)
 859		free_irq(chip->irq, (void*)chip);
 860	if (chip->tx_irq >= 0)
 861		free_irq(chip->tx_irq, (void*)chip);
 862	if (chip->rx_irq >= 0)
 863		free_irq(chip->rx_irq, (void*)chip);
 864	snd_pmac_dbdma_free(chip, &chip->playback.cmd);
 865	snd_pmac_dbdma_free(chip, &chip->capture.cmd);
 866	snd_pmac_dbdma_free(chip, &chip->extra_dma);
 867	snd_pmac_dbdma_free(chip, &emergency_dbdma);
 868	if (chip->macio_base)
 869		iounmap(chip->macio_base);
 870	if (chip->latch_base)
 871		iounmap(chip->latch_base);
 872	if (chip->awacs)
 873		iounmap(chip->awacs);
 874	if (chip->playback.dma)
 875		iounmap(chip->playback.dma);
 876	if (chip->capture.dma)
 877		iounmap(chip->capture.dma);
 878
 879	if (chip->node) {
 880		int i;
 881		for (i = 0; i < 3; i++) {
 882			if (chip->requested & (1 << i))
 883				release_mem_region(chip->rsrc[i].start,
 884						   resource_size(&chip->rsrc[i]));
 885		}
 886	}
 887
 888	if (chip->pdev)
 889		pci_dev_put(chip->pdev);
 890	of_node_put(chip->node);
 891	kfree(chip);
 892	return 0;
 893}
 894
 895
 896/*
 897 * free the device
 898 */
 899static int snd_pmac_dev_free(struct snd_device *device)
 900{
 901	struct snd_pmac *chip = device->device_data;
 902	return snd_pmac_free(chip);
 903}
 904
 905
 906/*
 907 * check the machine support byteswap (little-endian)
 908 */
 909
 910static void __devinit detect_byte_swap(struct snd_pmac *chip)
 911{
 912	struct device_node *mio;
 913
 914	/* if seems that Keylargo can't byte-swap  */
 915	for (mio = chip->node->parent; mio; mio = mio->parent) {
 916		if (strcmp(mio->name, "mac-io") == 0) {
 917			if (of_device_is_compatible(mio, "Keylargo"))
 918				chip->can_byte_swap = 0;
 919			break;
 920		}
 921	}
 922
 923	/* it seems the Pismo & iBook can't byte-swap in hardware. */
 924	if (of_machine_is_compatible("PowerBook3,1") ||
 925	    of_machine_is_compatible("PowerBook2,1"))
 926		chip->can_byte_swap = 0 ;
 927
 928	if (of_machine_is_compatible("PowerBook2,1"))
 929		chip->can_duplex = 0;
 930}
 931
 932
 933/*
 934 * detect a sound chip
 935 */
 936static int __devinit snd_pmac_detect(struct snd_pmac *chip)
 937{
 938	struct device_node *sound;
 939	struct device_node *dn;
 940	const unsigned int *prop;
 941	unsigned int l;
 942	struct macio_chip* macio;
 943
 944	if (!machine_is(powermac))
 945		return -ENODEV;
 946
 947	chip->subframe = 0;
 948	chip->revision = 0;
 949	chip->freqs_ok = 0xff; /* all ok */
 950	chip->model = PMAC_AWACS;
 951	chip->can_byte_swap = 1;
 952	chip->can_duplex = 1;
 953	chip->can_capture = 1;
 954	chip->num_freqs = ARRAY_SIZE(awacs_freqs);
 955	chip->freq_table = awacs_freqs;
 956	chip->pdev = NULL;
 957
 958	chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */
 959
 960	/* check machine type */
 961	if (of_machine_is_compatible("AAPL,3400/2400")
 962	    || of_machine_is_compatible("AAPL,3500"))
 963		chip->is_pbook_3400 = 1;
 964	else if (of_machine_is_compatible("PowerBook1,1")
 965		 || of_machine_is_compatible("AAPL,PowerBook1998"))
 966		chip->is_pbook_G3 = 1;
 967	chip->node = of_find_node_by_name(NULL, "awacs");
 968	sound = of_node_get(chip->node);
 969
 970	/*
 971	 * powermac G3 models have a node called "davbus"
 972	 * with a child called "sound".
 973	 */
 974	if (!chip->node)
 975		chip->node = of_find_node_by_name(NULL, "davbus");
 976	/*
 977	 * if we didn't find a davbus device, try 'i2s-a' since
 978	 * this seems to be what iBooks have
 979	 */
 980	if (! chip->node) {
 981		chip->node = of_find_node_by_name(NULL, "i2s-a");
 982		if (chip->node && chip->node->parent &&
 983		    chip->node->parent->parent) {
 984			if (of_device_is_compatible(chip->node->parent->parent,
 985						 "K2-Keylargo"))
 986				chip->is_k2 = 1;
 987		}
 988	}
 989	if (! chip->node)
 990		return -ENODEV;
 991
 992	if (!sound) {
 993		sound = of_find_node_by_name(NULL, "sound");
 994		while (sound && sound->parent != chip->node)
 995			sound = of_find_node_by_name(sound, "sound");
 996	}
 997	if (! sound) {
 998		of_node_put(chip->node);
 999		chip->node = NULL;
1000		return -ENODEV;
1001	}
1002	prop = of_get_property(sound, "sub-frame", NULL);
1003	if (prop && *prop < 16)
1004		chip->subframe = *prop;
1005	prop = of_get_property(sound, "layout-id", NULL);
1006	if (prop) {
1007		/* partly deprecate snd-powermac, for those machines
1008		 * that have a layout-id property for now */
1009		printk(KERN_INFO "snd-powermac no longer handles any "
1010				 "machines with a layout-id property "
1011				 "in the device-tree, use snd-aoa.\n");
1012		of_node_put(sound);
1013		of_node_put(chip->node);
1014		chip->node = NULL;
1015		return -ENODEV;
1016	}
1017	/* This should be verified on older screamers */
1018	if (of_device_is_compatible(sound, "screamer")) {
1019		chip->model = PMAC_SCREAMER;
1020		// chip->can_byte_swap = 0; /* FIXME: check this */
1021	}
1022	if (of_device_is_compatible(sound, "burgundy")) {
1023		chip->model = PMAC_BURGUNDY;
1024		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1025	}
1026	if (of_device_is_compatible(sound, "daca")) {
1027		chip->model = PMAC_DACA;
1028		chip->can_capture = 0;  /* no capture */
1029		chip->can_duplex = 0;
1030		// chip->can_byte_swap = 0; /* FIXME: check this */
1031		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1032	}
1033	if (of_device_is_compatible(sound, "tumbler")) {
1034		chip->model = PMAC_TUMBLER;
1035		chip->can_capture = of_machine_is_compatible("PowerMac4,2")
1036				|| of_machine_is_compatible("PowerBook3,2")
1037				|| of_machine_is_compatible("PowerBook3,3")
1038				|| of_machine_is_compatible("PowerBook4,1")
1039				|| of_machine_is_compatible("PowerBook4,2")
1040				|| of_machine_is_compatible("PowerBook4,3");
1041		chip->can_duplex = 0;
1042		// chip->can_byte_swap = 0; /* FIXME: check this */
1043		chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
1044		chip->freq_table = tumbler_freqs;
1045		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1046	}
1047	if (of_device_is_compatible(sound, "snapper")) {
1048		chip->model = PMAC_SNAPPER;
1049		// chip->can_byte_swap = 0; /* FIXME: check this */
1050		chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
1051		chip->freq_table = tumbler_freqs;
1052		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
1053	}
1054	prop = of_get_property(sound, "device-id", NULL);
1055	if (prop)
1056		chip->device_id = *prop;
1057	dn = of_find_node_by_name(NULL, "perch");
1058	chip->has_iic = (dn != NULL);
1059	of_node_put(dn);
1060
1061	/* We need the PCI device for DMA allocations, let's use a crude method
1062	 * for now ...
1063	 */
1064	macio = macio_find(chip->node, macio_unknown);
1065	if (macio == NULL)
1066		printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
1067	else {
1068		struct pci_dev *pdev = NULL;
1069
1070		for_each_pci_dev(pdev) {
1071			struct device_node *np = pci_device_to_OF_node(pdev);
1072			if (np && np == macio->of_node) {
1073				chip->pdev = pdev;
1074				break;
1075			}
1076		}
1077	}
1078	if (chip->pdev == NULL)
1079		printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
1080		       " device !\n");
1081
1082	detect_byte_swap(chip);
1083
1084	/* look for a property saying what sample rates
1085	   are available */
1086	prop = of_get_property(sound, "sample-rates", &l);
1087	if (! prop)
1088		prop = of_get_property(sound, "output-frame-rates", &l);
1089	if (prop) {
1090		int i;
1091		chip->freqs_ok = 0;
1092		for (l /= sizeof(int); l > 0; --l) {
1093			unsigned int r = *prop++;
1094			/* Apple 'Fixed' format */
1095			if (r >= 0x10000)
1096				r >>= 16;
1097			for (i = 0; i < chip->num_freqs; ++i) {
1098				if (r == chip->freq_table[i]) {
1099					chip->freqs_ok |= (1 << i);
1100					break;
1101				}
1102			}
1103		}
1104	} else {
1105		/* assume only 44.1khz */
1106		chip->freqs_ok = 1;
1107	}
1108
1109	of_node_put(sound);
1110	return 0;
1111}
1112
1113#ifdef PMAC_SUPPORT_AUTOMUTE
1114/*
1115 * auto-mute
1116 */
1117static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1118			      struct snd_ctl_elem_value *ucontrol)
1119{
1120	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1121	ucontrol->value.integer.value[0] = chip->auto_mute;
1122	return 0;
1123}
1124
1125static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1126			      struct snd_ctl_elem_value *ucontrol)
1127{
1128	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1129	if (ucontrol->value.integer.value[0] != chip->auto_mute) {
1130		chip->auto_mute = !!ucontrol->value.integer.value[0];
1131		if (chip->update_automute)
1132			chip->update_automute(chip, 1);
1133		return 1;
1134	}
1135	return 0;
1136}
1137
1138static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1139			      struct snd_ctl_elem_value *ucontrol)
1140{
1141	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1142	if (chip->detect_headphone)
1143		ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
1144	else
1145		ucontrol->value.integer.value[0] = 0;
1146	return 0;
1147}
1148
1149static struct snd_kcontrol_new auto_mute_controls[] __devinitdata = {
1150	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1151	  .name = "Auto Mute Switch",
1152	  .info = snd_pmac_boolean_mono_info,
1153	  .get = pmac_auto_mute_get,
1154	  .put = pmac_auto_mute_put,
1155	},
1156	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1157	  .name = "Headphone Detection",
1158	  .access = SNDRV_CTL_ELEM_ACCESS_READ,
1159	  .info = snd_pmac_boolean_mono_info,
1160	  .get = pmac_hp_detect_get,
1161	},
1162};
1163
1164int __devinit snd_pmac_add_automute(struct snd_pmac *chip)
1165{
1166	int err;
1167	chip->auto_mute = 1;
1168	err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1169	if (err < 0) {
1170		printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
 
1171		return err;
1172	}
1173	chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
1174	return snd_ctl_add(chip->card, chip->hp_detect_ctl);
1175}
1176#endif /* PMAC_SUPPORT_AUTOMUTE */
1177
1178/*
1179 * create and detect a pmac chip record
1180 */
1181int __devinit snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
1182{
1183	struct snd_pmac *chip;
1184	struct device_node *np;
1185	int i, err;
1186	unsigned int irq;
1187	unsigned long ctrl_addr, txdma_addr, rxdma_addr;
1188	static struct snd_device_ops ops = {
1189		.dev_free =	snd_pmac_dev_free,
1190	};
1191
1192	*chip_return = NULL;
1193
1194	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1195	if (chip == NULL)
1196		return -ENOMEM;
1197	chip->card = card;
1198
1199	spin_lock_init(&chip->reg_lock);
1200	chip->irq = chip->tx_irq = chip->rx_irq = -1;
1201
1202	chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
1203	chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;
1204
1205	if ((err = snd_pmac_detect(chip)) < 0)
 
1206		goto __error;
1207
1208	if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1209	    snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
1210	    snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0 ||
1211	    snd_pmac_dbdma_alloc(chip, &emergency_dbdma, 2) < 0) {
1212		err = -ENOMEM;
1213		goto __error;
1214	}
1215
1216	np = chip->node;
1217	chip->requested = 0;
1218	if (chip->is_k2) {
1219		static char *rnames[] = {
1220			"Sound Control", "Sound DMA" };
1221		for (i = 0; i < 2; i ++) {
1222			if (of_address_to_resource(np->parent, i,
1223						   &chip->rsrc[i])) {
1224				printk(KERN_ERR "snd: can't translate rsrc "
1225				       " %d (%s)\n", i, rnames[i]);
 
1226				err = -ENODEV;
1227				goto __error;
1228			}
1229			if (request_mem_region(chip->rsrc[i].start,
1230					       resource_size(&chip->rsrc[i]),
1231					       rnames[i]) == NULL) {
1232				printk(KERN_ERR "snd: can't request rsrc "
1233				       " %d (%s: %pR)\n",
1234				       i, rnames[i], &chip->rsrc[i]);
1235				err = -ENODEV;
1236				goto __error;
1237			}
1238			chip->requested |= (1 << i);
1239		}
1240		ctrl_addr = chip->rsrc[0].start;
1241		txdma_addr = chip->rsrc[1].start;
1242		rxdma_addr = txdma_addr + 0x100;
1243	} else {
1244		static char *rnames[] = {
1245			"Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
1246		for (i = 0; i < 3; i ++) {
1247			if (of_address_to_resource(np, i,
1248						   &chip->rsrc[i])) {
1249				printk(KERN_ERR "snd: can't translate rsrc "
1250				       " %d (%s)\n", i, rnames[i]);
 
1251				err = -ENODEV;
1252				goto __error;
1253			}
1254			if (request_mem_region(chip->rsrc[i].start,
1255					       resource_size(&chip->rsrc[i]),
1256					       rnames[i]) == NULL) {
1257				printk(KERN_ERR "snd: can't request rsrc "
1258				       " %d (%s: %pR)\n",
1259				       i, rnames[i], &chip->rsrc[i]);
1260				err = -ENODEV;
1261				goto __error;
1262			}
1263			chip->requested |= (1 << i);
1264		}
1265		ctrl_addr = chip->rsrc[0].start;
1266		txdma_addr = chip->rsrc[1].start;
1267		rxdma_addr = chip->rsrc[2].start;
1268	}
1269
1270	chip->awacs = ioremap(ctrl_addr, 0x1000);
1271	chip->playback.dma = ioremap(txdma_addr, 0x100);
1272	chip->capture.dma = ioremap(rxdma_addr, 0x100);
1273	if (chip->model <= PMAC_BURGUNDY) {
1274		irq = irq_of_parse_and_map(np, 0);
1275		if (request_irq(irq, snd_pmac_ctrl_intr, 0,
1276				"PMac", (void*)chip)) {
1277			snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
1278				   irq);
1279			err = -EBUSY;
1280			goto __error;
1281		}
1282		chip->irq = irq;
1283	}
1284	irq = irq_of_parse_and_map(np, 1);
1285	if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){
1286		snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1287		err = -EBUSY;
1288		goto __error;
1289	}
1290	chip->tx_irq = irq;
1291	irq = irq_of_parse_and_map(np, 2);
1292	if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) {
1293		snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq);
1294		err = -EBUSY;
1295		goto __error;
1296	}
1297	chip->rx_irq = irq;
1298
1299	snd_pmac_sound_feature(chip, 1);
1300
1301	/* reset & enable interrupts */
1302	if (chip->model <= PMAC_BURGUNDY)
1303		out_le32(&chip->awacs->control, chip->control_mask);
1304
1305	/* Powerbooks have odd ways of enabling inputs such as
1306	   an expansion-bay CD or sound from an internal modem
1307	   or a PC-card modem. */
1308	if (chip->is_pbook_3400) {
1309		/* Enable CD and PC-card sound inputs. */
1310		/* This is done by reading from address
1311		 * f301a000, + 0x10 to enable the expansion-bay
1312		 * CD sound input, + 0x80 to enable the PC-card
1313		 * sound input.  The 0x100 enables the SCSI bus
1314		 * terminator power.
1315		 */
1316		chip->latch_base = ioremap (0xf301a000, 0x1000);
1317		in_8(chip->latch_base + 0x190);
1318	} else if (chip->is_pbook_G3) {
1319		struct device_node* mio;
1320		for (mio = chip->node->parent; mio; mio = mio->parent) {
1321			if (strcmp(mio->name, "mac-io") == 0) {
1322				struct resource r;
1323				if (of_address_to_resource(mio, 0, &r) == 0)
1324					chip->macio_base =
1325						ioremap(r.start, 0x40);
1326				break;
1327			}
1328		}
1329		/* Enable CD sound input. */
1330		/* The relevant bits for writing to this byte are 0x8f.
1331		 * I haven't found out what the 0x80 bit does.
1332		 * For the 0xf bits, writing 3 or 7 enables the CD
1333		 * input, any other value disables it.  Values
1334		 * 1, 3, 5, 7 enable the microphone.  Values 0, 2,
1335		 * 4, 6, 8 - f enable the input from the modem.
1336		 */
1337		if (chip->macio_base)
1338			out_8(chip->macio_base + 0x37, 3);
1339	}
1340
1341	/* Reset dbdma channels */
1342	snd_pmac_dbdma_reset(chip);
1343
1344	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
 
1345		goto __error;
1346
1347	*chip_return = chip;
1348	return 0;
1349
1350 __error:
1351	snd_pmac_free(chip);
1352	return err;
1353}
1354
1355
1356/*
1357 * sleep notify for powerbook
1358 */
1359
1360#ifdef CONFIG_PM
1361
1362/*
1363 * Save state when going to sleep, restore it afterwards.
1364 */
1365
1366void snd_pmac_suspend(struct snd_pmac *chip)
1367{
1368	unsigned long flags;
1369
1370	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1371	if (chip->suspend)
1372		chip->suspend(chip);
1373	snd_pcm_suspend_all(chip->pcm);
1374	spin_lock_irqsave(&chip->reg_lock, flags);
1375	snd_pmac_beep_stop(chip);
1376	spin_unlock_irqrestore(&chip->reg_lock, flags);
1377	if (chip->irq >= 0)
1378		disable_irq(chip->irq);
1379	if (chip->tx_irq >= 0)
1380		disable_irq(chip->tx_irq);
1381	if (chip->rx_irq >= 0)
1382		disable_irq(chip->rx_irq);
1383	snd_pmac_sound_feature(chip, 0);
1384}
1385
1386void snd_pmac_resume(struct snd_pmac *chip)
1387{
1388	snd_pmac_sound_feature(chip, 1);
1389	if (chip->resume)
1390		chip->resume(chip);
1391	/* enable CD sound input */
1392	if (chip->macio_base && chip->is_pbook_G3)
1393		out_8(chip->macio_base + 0x37, 3);
1394	else if (chip->is_pbook_3400)
1395		in_8(chip->latch_base + 0x190);
1396
1397	snd_pmac_pcm_set_format(chip);
1398
1399	if (chip->irq >= 0)
1400		enable_irq(chip->irq);
1401	if (chip->tx_irq >= 0)
1402		enable_irq(chip->tx_irq);
1403	if (chip->rx_irq >= 0)
1404		enable_irq(chip->rx_irq);
1405
1406	snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1407}
1408
1409#endif /* CONFIG_PM */
1410