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