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