pmac.c 36.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * PMac DBDMA lowlevel functions
 *
 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
 * code based on dmasound.c.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */


#include <sound/driver.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
30 31
#include <linux/pci.h>
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
32 33 34 35
#include <sound/core.h>
#include "pmac.h"
#include <sound/pcm_params.h>
#include <asm/pmac_feature.h>
36
#include <asm/pci-bridge.h>
L
Linus Torvalds 已提交
37 38


39
#ifdef CONFIG_PM
40 41 42 43
static int snd_pmac_register_sleep_notifier(struct snd_pmac *chip);
static int snd_pmac_unregister_sleep_notifier(struct snd_pmac *chip);
static int snd_pmac_suspend(struct snd_card *card, pm_message_t state);
static int snd_pmac_resume(struct snd_card *card);
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#endif


/* fixed frequency table for awacs, screamer, burgundy, DACA (44100 max) */
static int awacs_freqs[8] = {
	44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
};
/* fixed frequency table for tumbler */
static int tumbler_freqs[1] = {
	44100
};

/*
 * allocate DBDMA command arrays
 */
59
static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
L
Linus Torvalds 已提交
60
{
61 62 63 64
	unsigned int rsize = sizeof(struct dbdma_cmd) * (size + 1);

	rec->space = dma_alloc_coherent(&chip->pdev->dev, rsize,
					&rec->dma_base, GFP_KERNEL);
L
Linus Torvalds 已提交
65 66 67
	if (rec->space == NULL)
		return -ENOMEM;
	rec->size = size;
68
	memset(rec->space, 0, rsize);
L
Linus Torvalds 已提交
69
	rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
70 71
	rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);

L
Linus Torvalds 已提交
72 73 74
	return 0;
}

75
static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
L
Linus Torvalds 已提交
76
{
77 78 79 80 81
	if (rec) {
		unsigned int rsize = sizeof(struct dbdma_cmd) * (rec->size + 1);

		dma_free_coherent(&chip->pdev->dev, rsize, rec->space, rec->dma_base);
	}
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92
}


/*
 * pcm stuff
 */

/*
 * look up frequency table
 */

93
unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
{
	int i, ok, found;

	ok = rec->cur_freqs;
	if (rate > chip->freq_table[0])
		return 0;
	found = 0;
	for (i = 0; i < chip->num_freqs; i++, ok >>= 1) {
		if (! (ok & 1)) continue;
		found = i;
		if (rate >= chip->freq_table[i])
			break;
	}
	return found;
}

/*
 * check whether another stream is active
 */
static inline int another_stream(int stream)
{
	return (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
		SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
}

/*
 * allocate buffers
 */
122 123
static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs,
				  struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
124 125 126 127 128 129 130
{
	return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
}

/*
 * release buffers
 */
131
static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139
{
	snd_pcm_lib_free_pages(subs);
	return 0;
}

/*
 * get a stream of the opposite direction
 */
140
static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
{
	switch (stream) {
	case SNDRV_PCM_STREAM_PLAYBACK:
		return &chip->playback;
	case SNDRV_PCM_STREAM_CAPTURE:
		return &chip->capture;
	default:
		snd_BUG();
		return NULL;
	}
}

/*
 * wait while run status is on
 */
156
static inline void
157
snd_pmac_wait_ack(struct pmac_stream *rec)
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167
{
	int timeout = 50000;
	while ((in_le32(&rec->dma->status) & RUN) && timeout-- > 0)
		udelay(1);
}

/*
 * set the format and rate to the chip.
 * call the lowlevel function if defined (e.g. for AWACS).
 */
168
static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179
{
	/* set up frequency and format */
	out_le32(&chip->awacs->control, chip->control_mask | (chip->rate_index << 8));
	out_le32(&chip->awacs->byteswap, chip->format == SNDRV_PCM_FORMAT_S16_LE ? 1 : 0);
	if (chip->set_format)
		chip->set_format(chip);
}

/*
 * stop the DMA transfer
 */
180
static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
L
Linus Torvalds 已提交
181 182 183 184 185 186 187 188
{
	out_le32(&rec->dma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
	snd_pmac_wait_ack(rec);
}

/*
 * set the command pointer address
 */
189
static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
L
Linus Torvalds 已提交
190 191 192 193 194 195 196
{
	out_le32(&rec->dma->cmdptr, cmd->addr);
}

/*
 * start the DMA
 */
197
static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205
{
	out_le32(&rec->dma->control, status | (status << 16));
}


/*
 * prepare playback/capture stream
 */
206
static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
207 208 209
{
	int i;
	volatile struct dbdma_cmd __iomem *cp;
210
	struct snd_pcm_runtime *runtime = subs->runtime;
L
Linus Torvalds 已提交
211 212
	int rate_index;
	long offset;
213
	struct pmac_stream *astr;
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221 222
	
	rec->dma_size = snd_pcm_lib_buffer_bytes(subs);
	rec->period_size = snd_pcm_lib_period_bytes(subs);
	rec->nperiods = rec->dma_size / rec->period_size;
	rec->cur_period = 0;
	rate_index = snd_pmac_rate_index(chip, rec, runtime->rate);

	/* set up constraints */
	astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
223 224
	if (! astr)
		return -EINVAL;
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	astr->cur_freqs = 1 << rate_index;
	astr->cur_formats = 1 << runtime->format;
	chip->rate_index = rate_index;
	chip->format = runtime->format;

	/* We really want to execute a DMA stop command, after the AWACS
	 * is initialized.
	 * For reasons I don't understand, it stops the hissing noise
	 * common to many PowerBook G3 systems and random noise otherwise
	 * captured on iBook2's about every third time. -ReneR
	 */
	spin_lock_irq(&chip->reg_lock);
	snd_pmac_dma_stop(rec);
	st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
	snd_pmac_dma_set_command(rec, &chip->extra_dma);
	snd_pmac_dma_run(rec, RUN);
	spin_unlock_irq(&chip->reg_lock);
	mdelay(5);
	spin_lock_irq(&chip->reg_lock);
	/* continuous DMA memory type doesn't provide the physical address,
	 * so we need to resolve the address here...
	 */
247
	offset = runtime->dma_addr;
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++) {
		st_le32(&cp->phy_addr, offset);
		st_le16(&cp->req_count, rec->period_size);
		/*st_le16(&cp->res_count, 0);*/
		st_le16(&cp->xfer_status, 0);
		offset += rec->period_size;
	}
	/* make loop */
	st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
	st_le32(&cp->cmd_dep, rec->cmd.addr);

	snd_pmac_dma_stop(rec);
	snd_pmac_dma_set_command(rec, &rec->cmd);
	spin_unlock_irq(&chip->reg_lock);

	return 0;
}


/*
 * PCM trigger/stop
 */
270 271
static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
				struct snd_pcm_substream *subs, int cmd)
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
{
	volatile struct dbdma_cmd __iomem *cp;
	int i, command;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
		if (rec->running)
			return -EBUSY;
		command = (subs->stream == SNDRV_PCM_STREAM_PLAYBACK ?
			   OUTPUT_MORE : INPUT_MORE) + INTR_ALWAYS;
		spin_lock(&chip->reg_lock);
		snd_pmac_beep_stop(chip);
		snd_pmac_pcm_set_format(chip);
		for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
			out_le16(&cp->command, command);
		snd_pmac_dma_set_command(rec, &rec->cmd);
		(void)in_le32(&rec->dma->status);
		snd_pmac_dma_run(rec, RUN|WAKE);
		rec->running = 1;
		spin_unlock(&chip->reg_lock);
		break;

	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		spin_lock(&chip->reg_lock);
		rec->running = 0;
		/*printk("stopped!!\n");*/
		snd_pmac_dma_stop(rec);
		for (i = 0, cp = rec->cmd.cmds; i < rec->nperiods; i++, cp++)
			out_le16(&cp->command, DBDMA_STOP);
		spin_unlock(&chip->reg_lock);
		break;

	default:
		return -EINVAL;
	}

	return 0;
}

/*
 * return the current pointer
 */
inline
317 318 319
static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
					      struct pmac_stream *rec,
					      struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
{
	int count = 0;

#if 1 /* hmm.. how can we get the current dma pointer?? */
	int stat;
	volatile struct dbdma_cmd __iomem *cp = &rec->cmd.cmds[rec->cur_period];
	stat = ld_le16(&cp->xfer_status);
	if (stat & (ACTIVE|DEAD)) {
		count = in_le16(&cp->res_count);
		if (count)
			count = rec->period_size - count;
	}
#endif
	count += rec->cur_period * rec->period_size;
	/*printk("pointer=%d\n", count);*/
	return bytes_to_frames(subs->runtime, count);
}

/*
 * playback
 */

342
static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
343
{
344
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
345 346 347
	return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
}

348
static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
L
Linus Torvalds 已提交
349 350
				     int cmd)
{
351
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
352 353 354
	return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
}

355
static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
356
{
357
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365
	return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
}


/*
 * capture
 */

366
static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
367
{
368
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
369 370 371
	return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
}

372
static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
L
Linus Torvalds 已提交
373 374
				    int cmd)
{
375
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
376 377 378
	return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
}

379
static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
380
{
381
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
382 383 384 385 386 387 388
	return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
}


/*
 * update playback/capture pointer from interrupts
 */
389
static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
L
Linus Torvalds 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
{
	volatile struct dbdma_cmd __iomem *cp;
	int c;
	int stat;

	spin_lock(&chip->reg_lock);
	if (rec->running) {
		cp = &rec->cmd.cmds[rec->cur_period];
		for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */
			stat = ld_le16(&cp->xfer_status);
			if (! (stat & ACTIVE))
				break;
			/*printk("update frag %d\n", rec->cur_period);*/
			st_le16(&cp->xfer_status, 0);
			st_le16(&cp->req_count, rec->period_size);
			/*st_le16(&cp->res_count, 0);*/
			rec->cur_period++;
			if (rec->cur_period >= rec->nperiods) {
				rec->cur_period = 0;
				cp = rec->cmd.cmds;
			} else
				cp++;
			spin_unlock(&chip->reg_lock);
			snd_pcm_period_elapsed(rec->substream);
			spin_lock(&chip->reg_lock);
		}
	}
	spin_unlock(&chip->reg_lock);
}


/*
 * hw info
 */

425
static struct snd_pcm_hardware snd_pmac_playback =
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
{
	.info =			(SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_RESUME),
	.formats =		SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_8000_44100,
	.rate_min =		7350,
	.rate_max =		44100,
	.channels_min =		2,
	.channels_max =		2,
	.buffer_bytes_max =	131072,
	.period_bytes_min =	256,
	.period_bytes_max =	16384,
	.periods_min =		3,
	.periods_max =		PMAC_MAX_FRAGS,
};

444
static struct snd_pcm_hardware snd_pmac_capture =
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
{
	.info =			(SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_RESUME),
	.formats =		SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_8000_44100,
	.rate_min =		7350,
	.rate_max =		44100,
	.channels_min =		2,
	.channels_max =		2,
	.buffer_bytes_max =	131072,
	.period_bytes_min =	256,
	.period_bytes_max =	16384,
	.periods_min =		3,
	.periods_max =		PMAC_MAX_FRAGS,
};


#if 0 // NYI
465 466
static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
				 struct snd_pcm_hw_rule *rule)
L
Linus Torvalds 已提交
467
{
468 469
	struct snd_pmac *chip = rule->private;
	struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
L
Linus Torvalds 已提交
470 471
	int i, freq_table[8], num_freqs;

472 473
	if (! rec)
		return -EINVAL;
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482 483
	num_freqs = 0;
	for (i = chip->num_freqs - 1; i >= 0; i--) {
		if (rec->cur_freqs & (1 << i))
			freq_table[num_freqs++] = chip->freq_table[i];
	}

	return snd_interval_list(hw_param_interval(params, rule->var),
				 num_freqs, freq_table, 0);
}

484 485
static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
				   struct snd_pcm_hw_rule *rule)
L
Linus Torvalds 已提交
486
{
487 488
	struct snd_pmac *chip = rule->private;
	struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
L
Linus Torvalds 已提交
489

490 491
	if (! rec)
		return -EINVAL;
L
Linus Torvalds 已提交
492 493 494 495 496
	return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
				   rec->cur_formats);
}
#endif // NYI

497 498
static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
			     struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
499
{
500
	struct snd_pcm_runtime *runtime = subs->runtime;
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	int i, j, fflags;
	static int typical_freqs[] = {
		44100,
		22050,
		11025,
		0,
	};
	static int typical_freq_flags[] = {
		SNDRV_PCM_RATE_44100,
		SNDRV_PCM_RATE_22050,
		SNDRV_PCM_RATE_11025,
		0,
	};

	/* look up frequency table and fill bit mask */
	runtime->hw.rates = 0;
	fflags = chip->freqs_ok;
	for (i = 0; typical_freqs[i]; i++) {
		for (j = 0; j < chip->num_freqs; j++) {
			if ((chip->freqs_ok & (1 << j)) &&
			    chip->freq_table[j] == typical_freqs[i]) {
				runtime->hw.rates |= typical_freq_flags[i];
				fflags &= ~(1 << j);
				break;
			}
		}
	}
	if (fflags) /* rest */
		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;

	/* check for minimum and maximum rates */
	for (i = 0; i < chip->num_freqs; i++) {
		if (chip->freqs_ok & (1 << i)) {
			runtime->hw.rate_max = chip->freq_table[i];
			break;
		}
	}
	for (i = chip->num_freqs - 1; i >= 0; i--) {
		if (chip->freqs_ok & (1 << i)) {
			runtime->hw.rate_min = chip->freq_table[i];
			break;
		}
	}
	runtime->hw.formats = chip->formats_ok;
	if (chip->can_capture) {
		if (! chip->can_duplex)
			runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX;
		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
	}
	runtime->private_data = rec;
	rec->substream = subs;

#if 0 /* FIXME: still under development.. */
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
			    snd_pmac_hw_rule_rate, chip, rec->stream, -1);
	snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
			    snd_pmac_hw_rule_format, chip, rec->stream, -1);
#endif

	runtime->hw.periods_max = rec->cmd.size - 1;

	if (chip->can_duplex)
		snd_pcm_set_sync(subs);

	/* constraints to fix choppy sound */
	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
	return 0;
}

570 571
static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
			      struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
572
{
573
	struct pmac_stream *astr;
L
Linus Torvalds 已提交
574 575 576 577

	snd_pmac_dma_stop(rec);

	astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
578 579
	if (! astr)
		return -EINVAL;
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587

	/* reset constraints */
	astr->cur_freqs = chip->freqs_ok;
	astr->cur_formats = chip->formats_ok;
	
	return 0;
}

588
static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
589
{
590
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
591 592 593 594 595

	subs->runtime->hw = snd_pmac_playback;
	return snd_pmac_pcm_open(chip, &chip->playback, subs);
}

596
static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
597
{
598
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
599 600 601 602 603

	subs->runtime->hw = snd_pmac_capture;
	return snd_pmac_pcm_open(chip, &chip->capture, subs);
}

604
static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
605
{
606
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
607 608 609 610

	return snd_pmac_pcm_close(chip, &chip->playback, subs);
}

611
static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
L
Linus Torvalds 已提交
612
{
613
	struct snd_pmac *chip = snd_pcm_substream_chip(subs);
L
Linus Torvalds 已提交
614 615 616 617 618 619 620

	return snd_pmac_pcm_close(chip, &chip->capture, subs);
}

/*
 */

621
static struct snd_pcm_ops snd_pmac_playback_ops = {
L
Linus Torvalds 已提交
622 623 624 625 626 627 628 629 630 631
	.open =		snd_pmac_playback_open,
	.close =	snd_pmac_playback_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_pmac_pcm_hw_params,
	.hw_free =	snd_pmac_pcm_hw_free,
	.prepare =	snd_pmac_playback_prepare,
	.trigger =	snd_pmac_playback_trigger,
	.pointer =	snd_pmac_playback_pointer,
};

632
static struct snd_pcm_ops snd_pmac_capture_ops = {
L
Linus Torvalds 已提交
633 634 635 636 637 638 639 640 641 642
	.open =		snd_pmac_capture_open,
	.close =	snd_pmac_capture_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_pmac_pcm_hw_params,
	.hw_free =	snd_pmac_pcm_hw_free,
	.prepare =	snd_pmac_capture_prepare,
	.trigger =	snd_pmac_capture_trigger,
	.pointer =	snd_pmac_capture_pointer,
};

643
int __init snd_pmac_pcm_new(struct snd_pmac *chip)
L
Linus Torvalds 已提交
644
{
645
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	int err;
	int num_captures = 1;

	if (! chip->can_capture)
		num_captures = 0;
	err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm);
	if (err < 0)
		return err;

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops);
	if (chip->can_capture)
		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops);

	pcm->private_data = chip;
	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
	strcpy(pcm->name, chip->card->shortname);
	chip->pcm = pcm;

	chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE;
	if (chip->can_byte_swap)
		chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE;

	chip->playback.cur_formats = chip->formats_ok;
	chip->capture.cur_formats = chip->formats_ok;
	chip->playback.cur_freqs = chip->freqs_ok;
	chip->capture.cur_freqs = chip->freqs_ok;

	/* preallocate 64k buffer */
674 675
	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      &chip->pdev->dev,
L
Linus Torvalds 已提交
676 677 678 679 680 681
					      64 * 1024, 64 * 1024);

	return 0;
}


682
static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690 691 692 693
{
	out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
	snd_pmac_wait_ack(&chip->playback);
	out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
	snd_pmac_wait_ack(&chip->capture);
}


/*
 * handling beep
 */
694
void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
L
Linus Torvalds 已提交
695
{
696
	struct pmac_stream *rec = &chip->playback;
L
Linus Torvalds 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

	snd_pmac_dma_stop(rec);
	st_le16(&chip->extra_dma.cmds->req_count, bytes);
	st_le16(&chip->extra_dma.cmds->xfer_status, 0);
	st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr);
	st_le32(&chip->extra_dma.cmds->phy_addr, addr);
	st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS);
	out_le32(&chip->awacs->control,
		 (in_le32(&chip->awacs->control) & ~0x1f00)
		 | (speed << 8));
	out_le32(&chip->awacs->byteswap, 0);
	snd_pmac_dma_set_command(rec, &chip->extra_dma);
	snd_pmac_dma_run(rec, RUN);
}

712
void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
L
Linus Torvalds 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725
{
	snd_pmac_dma_stop(&chip->playback);
	st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP);
	snd_pmac_pcm_set_format(chip); /* reset format */
}


/*
 * interrupt handlers
 */
static irqreturn_t
snd_pmac_tx_intr(int irq, void *devid, struct pt_regs *regs)
{
726
	struct snd_pmac *chip = devid;
L
Linus Torvalds 已提交
727 728 729 730 731 732 733 734
	snd_pmac_pcm_update(chip, &chip->playback);
	return IRQ_HANDLED;
}


static irqreturn_t
snd_pmac_rx_intr(int irq, void *devid, struct pt_regs *regs)
{
735
	struct snd_pmac *chip = devid;
L
Linus Torvalds 已提交
736 737 738 739 740 741 742 743
	snd_pmac_pcm_update(chip, &chip->capture);
	return IRQ_HANDLED;
}


static irqreturn_t
snd_pmac_ctrl_intr(int irq, void *devid, struct pt_regs *regs)
{
744
	struct snd_pmac *chip = devid;
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	int ctrl = in_le32(&chip->awacs->control);

	/*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/
	if (ctrl & MASK_PORTCHG) {
		/* do something when headphone is plugged/unplugged? */
		if (chip->update_automute)
			chip->update_automute(chip, 1);
	}
	if (ctrl & MASK_CNTLERR) {
		int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16;
		if (err && chip->model <= PMAC_SCREAMER)
			snd_printk(KERN_DEBUG "error %x\n", err);
	}
	/* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
	out_le32(&chip->awacs->control, ctrl);
	return IRQ_HANDLED;
}


/*
 * a wrapper to feature call for compatibility
 */
767
static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
L
Linus Torvalds 已提交
768
{
769 770
	if (ppc_md.feature_call)
		ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
L
Linus Torvalds 已提交
771 772 773 774 775 776
}

/*
 * release resources
 */

777
static int snd_pmac_free(struct snd_pmac *chip)
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786
{
	/* stop sounds */
	if (chip->initialized) {
		snd_pmac_dbdma_reset(chip);
		/* disable interrupts from awacs interface */
		out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff);
	}

	snd_pmac_sound_feature(chip, 0);
787
#ifdef CONFIG_PM
L
Linus Torvalds 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
	snd_pmac_unregister_sleep_notifier(chip);
#endif

	/* clean up mixer if any */
	if (chip->mixer_free)
		chip->mixer_free(chip);

	snd_pmac_detach_beep(chip);

	/* release resources */
	if (chip->irq >= 0)
		free_irq(chip->irq, (void*)chip);
	if (chip->tx_irq >= 0)
		free_irq(chip->tx_irq, (void*)chip);
	if (chip->rx_irq >= 0)
		free_irq(chip->rx_irq, (void*)chip);
804 805 806
	snd_pmac_dbdma_free(chip, &chip->playback.cmd);
	snd_pmac_dbdma_free(chip, &chip->capture.cmd);
	snd_pmac_dbdma_free(chip, &chip->extra_dma);
L
Linus Torvalds 已提交
807 808 809 810 811 812 813 814 815 816
	if (chip->macio_base)
		iounmap(chip->macio_base);
	if (chip->latch_base)
		iounmap(chip->latch_base);
	if (chip->awacs)
		iounmap(chip->awacs);
	if (chip->playback.dma)
		iounmap(chip->playback.dma);
	if (chip->capture.dma)
		iounmap(chip->capture.dma);
817
#ifndef CONFIG_PPC64
L
Linus Torvalds 已提交
818
	if (chip->node) {
819 820
		int i;

L
Linus Torvalds 已提交
821
		for (i = 0; i < 3; i++) {
822 823 824 825 826 827 828
			if (chip->of_requested & (1 << i)) {
				if (chip->is_k2)
					release_OF_resource(chip->node->parent,
							    i);
				else
					release_OF_resource(chip->node, i);
			}
L
Linus Torvalds 已提交
829 830
		}
	}
831 832 833
#endif /* CONFIG_PPC64 */
	if (chip->pdev)
		pci_dev_put(chip->pdev);
L
Linus Torvalds 已提交
834 835 836 837 838 839 840 841
	kfree(chip);
	return 0;
}


/*
 * free the device
 */
842
static int snd_pmac_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
843
{
844
	struct snd_pmac *chip = device->device_data;
L
Linus Torvalds 已提交
845 846 847 848 849 850 851 852
	return snd_pmac_free(chip);
}


/*
 * check the machine support byteswap (little-endian)
 */

853
static void __init detect_byte_swap(struct snd_pmac *chip)
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
{
	struct device_node *mio;

	/* if seems that Keylargo can't byte-swap  */
	for (mio = chip->node->parent; mio; mio = mio->parent) {
		if (strcmp(mio->name, "mac-io") == 0) {
			if (device_is_compatible(mio, "Keylargo"))
				chip->can_byte_swap = 0;
			break;
		}
	}

	/* it seems the Pismo & iBook can't byte-swap in hardware. */
	if (machine_is_compatible("PowerBook3,1") ||
	    machine_is_compatible("PowerBook2,1"))
		chip->can_byte_swap = 0 ;

	if (machine_is_compatible("PowerBook2,1"))
		chip->can_duplex = 0;
}


/*
 * detect a sound chip
 */
879
static int __init snd_pmac_detect(struct snd_pmac *chip)
L
Linus Torvalds 已提交
880
{
881
	struct device_node *sound = NULL;
L
Linus Torvalds 已提交
882
	unsigned int *prop, l;
883 884
	struct macio_chip* macio;

885
	u32 layout_id = 0;
L
Linus Torvalds 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910

	if (_machine != _MACH_Pmac)
		return -ENODEV;

	chip->subframe = 0;
	chip->revision = 0;
	chip->freqs_ok = 0xff; /* all ok */
	chip->model = PMAC_AWACS;
	chip->can_byte_swap = 1;
	chip->can_duplex = 1;
	chip->can_capture = 1;
	chip->num_freqs = ARRAY_SIZE(awacs_freqs);
	chip->freq_table = awacs_freqs;

	chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */

	/* check machine type */
	if (machine_is_compatible("AAPL,3400/2400")
	    || machine_is_compatible("AAPL,3500"))
		chip->is_pbook_3400 = 1;
	else if (machine_is_compatible("PowerBook1,1")
		 || machine_is_compatible("AAPL,PowerBook1998"))
		chip->is_pbook_G3 = 1;
	chip->node = find_devices("awacs");
	if (chip->node)
911
		sound = chip->node;
L
Linus Torvalds 已提交
912 913 914 915 916

	/*
	 * powermac G3 models have a node called "davbus"
	 * with a child called "sound".
	 */
917 918
	if (!chip->node)
		chip->node = find_devices("davbus");
L
Linus Torvalds 已提交
919 920 921 922
	/*
	 * if we didn't find a davbus device, try 'i2s-a' since
	 * this seems to be what iBooks have
	 */
923
	if (! chip->node) {
L
Linus Torvalds 已提交
924
		chip->node = find_devices("i2s-a");
925 926
		if (chip->node && chip->node->parent &&
		    chip->node->parent->parent) {
927 928 929 930 931
			if (device_is_compatible(chip->node->parent->parent,
						 "K2-Keylargo"))
				chip->is_k2 = 1;
		}
	}
L
Linus Torvalds 已提交
932 933
	if (! chip->node)
		return -ENODEV;
934

935 936 937 938 939
	if (!sound) {
		sound = find_devices("sound");
		while (sound && sound->parent != chip->node)
			sound = sound->next;
	}
L
Linus Torvalds 已提交
940 941 942 943 944
	if (! sound)
		return -ENODEV;
	prop = (unsigned int *) get_property(sound, "sub-frame", NULL);
	if (prop && *prop < 16)
		chip->subframe = *prop;
945 946 947
	prop = (unsigned int *) get_property(sound, "layout-id", NULL);
	if (prop)
		layout_id = *prop;
L
Linus Torvalds 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	/* This should be verified on older screamers */
	if (device_is_compatible(sound, "screamer")) {
		chip->model = PMAC_SCREAMER;
		// chip->can_byte_swap = 0; /* FIXME: check this */
	}
	if (device_is_compatible(sound, "burgundy")) {
		chip->model = PMAC_BURGUNDY;
		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
	}
	if (device_is_compatible(sound, "daca")) {
		chip->model = PMAC_DACA;
		chip->can_capture = 0;  /* no capture */
		chip->can_duplex = 0;
		// chip->can_byte_swap = 0; /* FIXME: check this */
		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
	}
	if (device_is_compatible(sound, "tumbler")) {
		chip->model = PMAC_TUMBLER;
		chip->can_capture = 0;  /* no capture */
		chip->can_duplex = 0;
		// chip->can_byte_swap = 0; /* FIXME: check this */
		chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
		chip->freq_table = tumbler_freqs;
		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
	}
	if (device_is_compatible(sound, "snapper")) {
		chip->model = PMAC_SNAPPER;
		// chip->can_byte_swap = 0; /* FIXME: check this */
		chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
		chip->freq_table = tumbler_freqs;
		chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
	}
980
	if (device_is_compatible(sound, "AOAKeylargo") ||
981 982
	    device_is_compatible(sound, "AOAbase") ||
	    device_is_compatible(sound, "AOAK2")) {
983 984 985 986 987 988 989 990
		/* For now, only support very basic TAS3004 based machines with
		 * single frequency until proper i2s control is implemented
		 */
		switch(layout_id) {
		case 0x48:
		case 0x46:
		case 0x33:
		case 0x29:
991
		case 0x24:
T
Takashi Iwai 已提交
992
		case 0x50:
993
		case 0x5c:
994 995 996
			chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
			chip->model = PMAC_SNAPPER;
			chip->can_byte_swap = 0; /* FIXME: check this */
997 998 999 1000 1001 1002 1003
			chip->control_mask = MASK_IEPC | 0x11;/* disable IEE */
			break;
		case 0x3a:
			chip->num_freqs = ARRAY_SIZE(tumbler_freqs);
			chip->model = PMAC_TOONIE;
			chip->can_byte_swap = 0; /* FIXME: check this */
			chip->control_mask = MASK_IEPC | 0x11;/* disable IEE */
1004 1005
			break;
		}
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011
	}
	prop = (unsigned int *)get_property(sound, "device-id", NULL);
	if (prop)
		chip->device_id = *prop;
	chip->has_iic = (find_devices("perch") != NULL);

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	/* We need the PCI device for DMA allocations, let's use a crude method
	 * for now ...
	 */
	macio = macio_find(chip->node, macio_unknown);
	if (macio == NULL)
		printk(KERN_WARNING "snd-powermac: can't locate macio !\n");
	else {
		struct pci_dev *pdev = NULL;

		for_each_pci_dev(pdev) {
			struct device_node *np = pci_device_to_OF_node(pdev);
			if (np && np == macio->of_node) {
				chip->pdev = pdev;
				break;
			}
		}
	}
	if (chip->pdev == NULL)
1030 1031
		printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
		       " device !\n");
1032

L
Linus Torvalds 已提交
1033 1034 1035 1036 1037 1038
	detect_byte_swap(chip);

	/* look for a property saying what sample rates
	   are available */
	prop = (unsigned int *) get_property(sound, "sample-rates", &l);
	if (! prop)
1039 1040
		prop = (unsigned int *) get_property(sound,
						     "output-frame-rates", &l);
L
Linus Torvalds 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	if (prop) {
		int i;
		chip->freqs_ok = 0;
		for (l /= sizeof(int); l > 0; --l) {
			unsigned int r = *prop++;
			/* Apple 'Fixed' format */
			if (r >= 0x10000)
				r >>= 16;
			for (i = 0; i < chip->num_freqs; ++i) {
				if (r == chip->freq_table[i]) {
					chip->freqs_ok |= (1 << i);
					break;
				}
			}
		}
	} else {
		/* assume only 44.1khz */
		chip->freqs_ok = 1;
	}

	return 0;
}

/*
 * exported - boolean info callbacks for ease of programming
 */
1067 1068
int snd_pmac_boolean_stereo_info(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073 1074 1075 1076
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;
	return 0;
}

1077 1078
int snd_pmac_boolean_mono_info(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;
	return 0;
}

#ifdef PMAC_SUPPORT_AUTOMUTE
/*
 * auto-mute
 */
1091 1092
static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1093
{
1094
	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1095 1096 1097 1098
	ucontrol->value.integer.value[0] = chip->auto_mute;
	return 0;
}

1099 1100
static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1101
{
1102
	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111
	if (ucontrol->value.integer.value[0] != chip->auto_mute) {
		chip->auto_mute = ucontrol->value.integer.value[0];
		if (chip->update_automute)
			chip->update_automute(chip, 1);
		return 1;
	}
	return 0;
}

1112 1113
static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1114
{
1115
	struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120 1121 1122
	if (chip->detect_headphone)
		ucontrol->value.integer.value[0] = chip->detect_headphone(chip);
	else
		ucontrol->value.integer.value[0] = 0;
	return 0;
}

1123
static struct snd_kcontrol_new auto_mute_controls[] __initdata = {
L
Linus Torvalds 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	  .name = "Auto Mute Switch",
	  .info = snd_pmac_boolean_mono_info,
	  .get = pmac_auto_mute_get,
	  .put = pmac_auto_mute_put,
	},
	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	  .name = "Headphone Detection",
	  .access = SNDRV_CTL_ELEM_ACCESS_READ,
	  .info = snd_pmac_boolean_mono_info,
	  .get = pmac_hp_detect_get,
	},
};

1138
int __init snd_pmac_add_automute(struct snd_pmac *chip)
L
Linus Torvalds 已提交
1139 1140 1141 1142
{
	int err;
	chip->auto_mute = 1;
	err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
1143 1144
	if (err < 0) {
		printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
L
Linus Torvalds 已提交
1145
		return err;
1146
	}
L
Linus Torvalds 已提交
1147 1148 1149 1150 1151 1152 1153 1154
	chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip);
	return snd_ctl_add(chip->card, chip->hp_detect_ctl);
}
#endif /* PMAC_SUPPORT_AUTOMUTE */

/*
 * create and detect a pmac chip record
 */
1155
int __init snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
L
Linus Torvalds 已提交
1156
{
1157
	struct snd_pmac *chip;
L
Linus Torvalds 已提交
1158 1159
	struct device_node *np;
	int i, err;
1160
	unsigned long ctrl_addr, txdma_addr, rxdma_addr;
1161
	static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
1162 1163 1164 1165 1166
		.dev_free =	snd_pmac_dev_free,
	};

	*chip_return = NULL;

1167
	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
L
Linus Torvalds 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	if (chip == NULL)
		return -ENOMEM;
	chip->card = card;

	spin_lock_init(&chip->reg_lock);
	chip->irq = chip->tx_irq = chip->rx_irq = -1;

	chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK;
	chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE;

	if ((err = snd_pmac_detect(chip)) < 0)
		goto __error;

1181 1182 1183
	if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
	    snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 ||
	    snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) {
L
Linus Torvalds 已提交
1184 1185 1186 1187 1188
		err = -ENOMEM;
		goto __error;
	}

	np = chip->node;
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	if (chip->is_k2) {
		if (np->parent->n_addrs < 2 || np->n_intrs < 3) {
			err = -ENODEV;
			goto __error;
		}
		for (i = 0; i < 2; i++) {
#ifndef CONFIG_PPC64
			static char *name[2] = { "- Control", "- DMA" };
			if (! request_OF_resource(np->parent, i, name[i])) {
				snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
				err = -ENODEV;
				goto __error;
			}
			chip->of_requested |= (1 << i);
#endif /* CONFIG_PPC64 */
			ctrl_addr = np->parent->addrs[0].address;
			txdma_addr = np->parent->addrs[1].address;
			rxdma_addr = txdma_addr + 0x100;
		}
L
Linus Torvalds 已提交
1208

1209 1210
	} else {
		if (np->n_addrs < 3 || np->n_intrs < 3) {
L
Linus Torvalds 已提交
1211 1212 1213
			err = -ENODEV;
			goto __error;
		}
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

		for (i = 0; i < 3; i++) {
#ifndef CONFIG_PPC64
			static char *name[3] = { "- Control", "- Tx DMA", "- Rx DMA" };
			if (! request_OF_resource(np, i, name[i])) {
				snd_printk(KERN_ERR "pmac: can't request resource %d!\n", i);
				err = -ENODEV;
				goto __error;
			}
			chip->of_requested |= (1 << i);
#endif /* CONFIG_PPC64 */
			ctrl_addr = np->addrs[0].address;
			txdma_addr = np->addrs[1].address;
			rxdma_addr = np->addrs[2].address;
		}
L
Linus Torvalds 已提交
1229 1230
	}

1231 1232 1233
	chip->awacs = ioremap(ctrl_addr, 0x1000);
	chip->playback.dma = ioremap(txdma_addr, 0x100);
	chip->capture.dma = ioremap(rxdma_addr, 0x100);
L
Linus Torvalds 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
	if (chip->model <= PMAC_BURGUNDY) {
		if (request_irq(np->intrs[0].line, snd_pmac_ctrl_intr, 0,
				"PMac", (void*)chip)) {
			snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[0].line);
			err = -EBUSY;
			goto __error;
		}
		chip->irq = np->intrs[0].line;
	}
	if (request_irq(np->intrs[1].line, snd_pmac_tx_intr, 0,
			"PMac Output", (void*)chip)) {
		snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[1].line);
		err = -EBUSY;
		goto __error;
	}
	chip->tx_irq = np->intrs[1].line;
	if (request_irq(np->intrs[2].line, snd_pmac_rx_intr, 0,
			"PMac Input", (void*)chip)) {
		snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", np->intrs[2].line);
		err = -EBUSY;
		goto __error;
	}
	chip->rx_irq = np->intrs[2].line;

	snd_pmac_sound_feature(chip, 1);

	/* reset */
1261 1262
	if (chip->model == PMAC_AWACS)
		out_le32(&chip->awacs->control, 0x11);
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

	/* Powerbooks have odd ways of enabling inputs such as
	   an expansion-bay CD or sound from an internal modem
	   or a PC-card modem. */
	if (chip->is_pbook_3400) {
		/* Enable CD and PC-card sound inputs. */
		/* This is done by reading from address
		 * f301a000, + 0x10 to enable the expansion-bay
		 * CD sound input, + 0x80 to enable the PC-card
		 * sound input.  The 0x100 enables the SCSI bus
		 * terminator power.
		 */
		chip->latch_base = ioremap (0xf301a000, 0x1000);
		in_8(chip->latch_base + 0x190);
	} else if (chip->is_pbook_G3) {
		struct device_node* mio;
		for (mio = chip->node->parent; mio; mio = mio->parent) {
			if (strcmp(mio->name, "mac-io") == 0
			    && mio->n_addrs > 0) {
				chip->macio_base = ioremap(mio->addrs[0].address, 0x40);
				break;
			}
		}
		/* Enable CD sound input. */
		/* The relevant bits for writing to this byte are 0x8f.
		 * I haven't found out what the 0x80 bit does.
		 * For the 0xf bits, writing 3 or 7 enables the CD
		 * input, any other value disables it.  Values
		 * 1, 3, 5, 7 enable the microphone.  Values 0, 2,
		 * 4, 6, 8 - f enable the input from the modem.
		 */
		if (chip->macio_base)
			out_8(chip->macio_base + 0x37, 3);
	}

	/* Reset dbdma channels */
	snd_pmac_dbdma_reset(chip);

1301
#ifdef CONFIG_PM
L
Linus Torvalds 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	/* add sleep notifier */
	if (! snd_pmac_register_sleep_notifier(chip))
		snd_card_set_pm_callback(chip->card, snd_pmac_suspend, snd_pmac_resume, chip);
#endif

	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
		goto __error;

	*chip_return = chip;
	return 0;

 __error:
1314 1315
	if (chip->pdev)
		pci_dev_put(chip->pdev);
L
Linus Torvalds 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324
	snd_pmac_free(chip);
	return err;
}


/*
 * sleep notify for powerbook
 */

1325
#ifdef CONFIG_PM
L
Linus Torvalds 已提交
1326 1327 1328 1329 1330

/*
 * Save state when going to sleep, restore it afterwards.
 */

1331
static int snd_pmac_suspend(struct snd_card *card, pm_message_t state)
L
Linus Torvalds 已提交
1332
{
1333
	struct snd_pmac *chip = card->pm_private_data;
L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
	unsigned long flags;

	if (chip->suspend)
		chip->suspend(chip);
	snd_pcm_suspend_all(chip->pcm);
	spin_lock_irqsave(&chip->reg_lock, flags);
	snd_pmac_beep_stop(chip);
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	if (chip->irq >= 0)
		disable_irq(chip->irq);
	if (chip->tx_irq >= 0)
		disable_irq(chip->tx_irq);
	if (chip->rx_irq >= 0)
		disable_irq(chip->rx_irq);
	snd_pmac_sound_feature(chip, 0);
	return 0;
}

1352
static int snd_pmac_resume(struct snd_card *card)
L
Linus Torvalds 已提交
1353
{
1354
	struct snd_pmac *chip = card->pm_private_data;
L
Linus Torvalds 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

	snd_pmac_sound_feature(chip, 1);
	if (chip->resume)
		chip->resume(chip);
	/* enable CD sound input */
	if (chip->macio_base && chip->is_pbook_G3) {
		out_8(chip->macio_base + 0x37, 3);
	} else if (chip->is_pbook_3400) {
		in_8(chip->latch_base + 0x190);
	}

	snd_pmac_pcm_set_format(chip);

	if (chip->irq >= 0)
		enable_irq(chip->irq);
	if (chip->tx_irq >= 0)
		enable_irq(chip->tx_irq);
	if (chip->rx_irq >= 0)
		enable_irq(chip->rx_irq);

	return 0;
}

/* the chip is stored statically by snd_pmac_register_sleep_notifier
 * because we can't have any private data for notify callback.
 */
1381
static struct snd_pmac *sleeping_pmac = NULL;
L
Linus Torvalds 已提交
1382 1383 1384

static int snd_pmac_sleep_notify(struct pmu_sleep_notifier *self, int when)
{
1385
	struct snd_pmac *chip;
L
Linus Torvalds 已提交
1386 1387

	chip = sleeping_pmac;
1388 1389
	if (! chip)
		return 0;
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

	switch (when) {
	case PBOOK_SLEEP_NOW:
		snd_pmac_suspend(chip->card, PMSG_SUSPEND);
		break;
	case PBOOK_WAKE:
		snd_pmac_resume(chip->card);
		break;
	}
	return PBOOK_SLEEP_OK;
}

static struct pmu_sleep_notifier snd_pmac_sleep_notifier = {
	snd_pmac_sleep_notify, SLEEP_LEVEL_SOUND,
};

1406
static int __init snd_pmac_register_sleep_notifier(struct snd_pmac *chip)
L
Linus Torvalds 已提交
1407 1408 1409 1410 1411 1412 1413 1414
{
	/* should be protected here.. */
	snd_assert(! sleeping_pmac, return -EBUSY);
	sleeping_pmac = chip;
	pmu_register_sleep_notifier(&snd_pmac_sleep_notifier);
	return 0;
}
						    
1415
static int snd_pmac_unregister_sleep_notifier(struct snd_pmac *chip)
L
Linus Torvalds 已提交
1416 1417 1418 1419 1420 1421 1422 1423
{
	/* should be protected here.. */
	snd_assert(sleeping_pmac == chip, return -ENODEV);
	pmu_unregister_sleep_notifier(&snd_pmac_sleep_notifier);
	sleeping_pmac = NULL;
	return 0;
}

1424 1425
#endif /* CONFIG_PM */