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