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