]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/ppc/pmac.c
[ALSA] sound: fix caiaq section mismatches
[net-next-2.6.git] / sound / ppc / pmac.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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>
7bbd8277
BH
29#include <linux/pci.h>
30#include <linux/dma-mapping.h>
1da177e4
LT
31#include <sound/core.h>
32#include "pmac.h"
33#include <sound/pcm_params.h>
1da177e4 34#include <asm/pmac_feature.h>
7bbd8277 35#include <asm/pci-bridge.h>
1da177e4
LT
36
37
1da177e4
LT
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 * allocate DBDMA command arrays
49 */
65b29f50 50static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size)
1da177e4 51{
7bbd8277
BH
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);
1da177e4
LT
56 if (rec->space == NULL)
57 return -ENOMEM;
58 rec->size = size;
7bbd8277 59 memset(rec->space, 0, rsize);
1da177e4 60 rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space);
7bbd8277
BH
61 rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space);
62
1da177e4
LT
63 return 0;
64}
65
65b29f50 66static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec)
1da177e4 67{
367636e8 68 if (rec->space) {
7bbd8277
BH
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 }
1da177e4
LT
73}
74
75
76/*
77 * pcm stuff
78 */
79
80/*
81 * look up frequency table
82 */
83
65b29f50 84unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate)
1da177e4
LT
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 */
104static 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 */
65b29f50
TI
113static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs,
114 struct snd_pcm_hw_params *hw_params)
1da177e4
LT
115{
116 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
117}
118
119/*
120 * release buffers
121 */
65b29f50 122static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs)
1da177e4
LT
123{
124 snd_pcm_lib_free_pages(subs);
125 return 0;
126}
127
128/*
129 * get a stream of the opposite direction
130 */
65b29f50 131static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream)
1da177e4
LT
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 */
77933d72 147static inline void
65b29f50 148snd_pmac_wait_ack(struct pmac_stream *rec)
1da177e4
LT
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 */
65b29f50 159static void snd_pmac_pcm_set_format(struct snd_pmac *chip)
1da177e4
LT
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 */
65b29f50 171static inline void snd_pmac_dma_stop(struct pmac_stream *rec)
1da177e4
LT
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 */
65b29f50 180static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd)
1da177e4
LT
181{
182 out_le32(&rec->dma->cmdptr, cmd->addr);
183}
184
185/*
186 * start the DMA
187 */
65b29f50 188static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status)
1da177e4
LT
189{
190 out_le32(&rec->dma->control, status | (status << 16));
191}
192
193
194/*
195 * prepare playback/capture stream
196 */
65b29f50 197static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs)
1da177e4
LT
198{
199 int i;
200 volatile struct dbdma_cmd __iomem *cp;
65b29f50 201 struct snd_pcm_runtime *runtime = subs->runtime;
1da177e4
LT
202 int rate_index;
203 long offset;
65b29f50 204 struct pmac_stream *astr;
1da177e4
LT
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));
7c22f1aa
TI
214 if (! astr)
215 return -EINVAL;
1da177e4
LT
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 */
7bbd8277 238 offset = runtime->dma_addr;
1da177e4
LT
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 */
65b29f50
TI
261static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec,
262 struct snd_pcm_substream *subs, int cmd)
1da177e4
LT
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 */
307inline
65b29f50
TI
308static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip,
309 struct pmac_stream *rec,
310 struct snd_pcm_substream *subs)
1da177e4
LT
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
65b29f50 333static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs)
1da177e4 334{
65b29f50 335 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
336 return snd_pmac_pcm_prepare(chip, &chip->playback, subs);
337}
338
65b29f50 339static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs,
1da177e4
LT
340 int cmd)
341{
65b29f50 342 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
343 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd);
344}
345
65b29f50 346static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs)
1da177e4 347{
65b29f50 348 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
349 return snd_pmac_pcm_pointer(chip, &chip->playback, subs);
350}
351
352
353/*
354 * capture
355 */
356
65b29f50 357static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs)
1da177e4 358{
65b29f50 359 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
360 return snd_pmac_pcm_prepare(chip, &chip->capture, subs);
361}
362
65b29f50 363static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs,
1da177e4
LT
364 int cmd)
365{
65b29f50 366 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
367 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd);
368}
369
65b29f50 370static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs)
1da177e4 371{
65b29f50 372 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
373 return snd_pmac_pcm_pointer(chip, &chip->capture, subs);
374}
375
376
377/*
378 * update playback/capture pointer from interrupts
379 */
65b29f50 380static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec)
1da177e4
LT
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
65b29f50 416static struct snd_pcm_hardware snd_pmac_playback =
1da177e4
LT
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
65b29f50 435static struct snd_pcm_hardware snd_pmac_capture =
1da177e4
LT
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
65b29f50
TI
456static int snd_pmac_hw_rule_rate(struct snd_pcm_hw_params *params,
457 struct snd_pcm_hw_rule *rule)
1da177e4 458{
65b29f50
TI
459 struct snd_pmac *chip = rule->private;
460 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
1da177e4
LT
461 int i, freq_table[8], num_freqs;
462
7c22f1aa
TI
463 if (! rec)
464 return -EINVAL;
1da177e4
LT
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
65b29f50
TI
475static int snd_pmac_hw_rule_format(struct snd_pcm_hw_params *params,
476 struct snd_pcm_hw_rule *rule)
1da177e4 477{
65b29f50
TI
478 struct snd_pmac *chip = rule->private;
479 struct pmac_stream *rec = snd_pmac_get_stream(chip, rule->deps[0]);
1da177e4 480
7c22f1aa
TI
481 if (! rec)
482 return -EINVAL;
1da177e4
LT
483 return snd_mask_refine_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
484 rec->cur_formats);
485}
486#endif // NYI
487
65b29f50
TI
488static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec,
489 struct snd_pcm_substream *subs)
1da177e4 490{
65b29f50 491 struct snd_pcm_runtime *runtime = subs->runtime;
918f3a0e 492 int i;
1da177e4
LT
493
494 /* look up frequency table and fill bit mask */
495 runtime->hw.rates = 0;
918f3a0e
CL
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]);
1da177e4
LT
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
1da177e4
LT
532 /* constraints to fix choppy sound */
533 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
534 return 0;
535}
536
65b29f50
TI
537static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec,
538 struct snd_pcm_substream *subs)
1da177e4 539{
65b29f50 540 struct pmac_stream *astr;
1da177e4
LT
541
542 snd_pmac_dma_stop(rec);
543
544 astr = snd_pmac_get_stream(chip, another_stream(rec->stream));
7c22f1aa
TI
545 if (! astr)
546 return -EINVAL;
1da177e4
LT
547
548 /* reset constraints */
549 astr->cur_freqs = chip->freqs_ok;
550 astr->cur_formats = chip->formats_ok;
551
552 return 0;
553}
554
65b29f50 555static int snd_pmac_playback_open(struct snd_pcm_substream *subs)
1da177e4 556{
65b29f50 557 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
558
559 subs->runtime->hw = snd_pmac_playback;
560 return snd_pmac_pcm_open(chip, &chip->playback, subs);
561}
562
65b29f50 563static int snd_pmac_capture_open(struct snd_pcm_substream *subs)
1da177e4 564{
65b29f50 565 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
566
567 subs->runtime->hw = snd_pmac_capture;
568 return snd_pmac_pcm_open(chip, &chip->capture, subs);
569}
570
65b29f50 571static int snd_pmac_playback_close(struct snd_pcm_substream *subs)
1da177e4 572{
65b29f50 573 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
574
575 return snd_pmac_pcm_close(chip, &chip->playback, subs);
576}
577
65b29f50 578static int snd_pmac_capture_close(struct snd_pcm_substream *subs)
1da177e4 579{
65b29f50 580 struct snd_pmac *chip = snd_pcm_substream_chip(subs);
1da177e4
LT
581
582 return snd_pmac_pcm_close(chip, &chip->capture, subs);
583}
584
585/*
586 */
587
65b29f50 588static struct snd_pcm_ops snd_pmac_playback_ops = {
1da177e4
LT
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
65b29f50 599static struct snd_pcm_ops snd_pmac_capture_ops = {
1da177e4
LT
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
65b29f50 610int __init snd_pmac_pcm_new(struct snd_pmac *chip)
1da177e4 611{
65b29f50 612 struct snd_pcm *pcm;
1da177e4
LT
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;
1da177e4
LT
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 */
7bbd8277
BH
641 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
642 &chip->pdev->dev,
1da177e4
LT
643 64 * 1024, 64 * 1024);
644
645 return 0;
646}
647
648
65b29f50 649static void snd_pmac_dbdma_reset(struct snd_pmac *chip)
1da177e4
LT
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 */
65b29f50 661void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed)
1da177e4 662{
65b29f50 663 struct pmac_stream *rec = &chip->playback;
1da177e4
LT
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
65b29f50 679void snd_pmac_beep_dma_stop(struct snd_pmac *chip)
1da177e4
LT
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 */
690static irqreturn_t
7d12e780 691snd_pmac_tx_intr(int irq, void *devid)
1da177e4 692{
65b29f50 693 struct snd_pmac *chip = devid;
1da177e4
LT
694 snd_pmac_pcm_update(chip, &chip->playback);
695 return IRQ_HANDLED;
696}
697
698
699static irqreturn_t
7d12e780 700snd_pmac_rx_intr(int irq, void *devid)
1da177e4 701{
65b29f50 702 struct snd_pmac *chip = devid;
1da177e4
LT
703 snd_pmac_pcm_update(chip, &chip->capture);
704 return IRQ_HANDLED;
705}
706
707
708static irqreturn_t
7d12e780 709snd_pmac_ctrl_intr(int irq, void *devid)
1da177e4 710{
65b29f50 711 struct snd_pmac *chip = devid;
1da177e4
LT
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 */
65b29f50 734static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable)
1da177e4 735{
4e6a06ee
DW
736 if (ppc_md.feature_call)
737 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable);
1da177e4 738}
1da177e4
LT
739
740/*
741 * release resources
742 */
743
65b29f50 744static int snd_pmac_free(struct snd_pmac *chip)
1da177e4 745{
1da177e4
LT
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
41e904de
BH
753 if (chip->node)
754 snd_pmac_sound_feature(chip, 0);
1da177e4
LT
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);
7bbd8277
BH
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);
1da177e4
LT
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);
cc5d0189 782
1da177e4 783 if (chip->node) {
7bbd8277 784 int i;
1da177e4 785 for (i = 0; i < 3; i++) {
cc5d0189
BH
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);
1da177e4
LT
790 }
791 }
cc5d0189 792
7bbd8277
BH
793 if (chip->pdev)
794 pci_dev_put(chip->pdev);
30686ba6 795 of_node_put(chip->node);
1da177e4
LT
796 kfree(chip);
797 return 0;
798}
799
800
801/*
802 * free the device
803 */
65b29f50 804static int snd_pmac_dev_free(struct snd_device *device)
1da177e4 805{
65b29f50 806 struct snd_pmac *chip = device->device_data;
1da177e4
LT
807 return snd_pmac_free(chip);
808}
809
810
811/*
812 * check the machine support byteswap (little-endian)
813 */
814
65b29f50 815static void __init detect_byte_swap(struct snd_pmac *chip)
1da177e4
LT
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) {
55b61fec 822 if (of_device_is_compatible(mio, "Keylargo"))
1da177e4
LT
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 */
65b29f50 841static int __init snd_pmac_detect(struct snd_pmac *chip)
1da177e4 842{
30686ba6
SR
843 struct device_node *sound;
844 struct device_node *dn;
c4f55b39
SR
845 const unsigned int *prop;
846 unsigned int l;
7bbd8277
BH
847 struct macio_chip* macio;
848
e8222502 849 if (!machine_is(powermac))
1da177e4
LT
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;
367636e8 861 chip->pdev = NULL;
1da177e4
LT
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;
30686ba6
SR
872 chip->node = of_find_node_by_name(NULL, "awacs");
873 sound = of_node_get(chip->node);
1da177e4
LT
874
875 /*
876 * powermac G3 models have a node called "davbus"
877 * with a child called "sound".
878 */
5218064c 879 if (!chip->node)
30686ba6 880 chip->node = of_find_node_by_name(NULL, "davbus");
1da177e4
LT
881 /*
882 * if we didn't find a davbus device, try 'i2s-a' since
883 * this seems to be what iBooks have
884 */
7bbd8277 885 if (! chip->node) {
30686ba6 886 chip->node = of_find_node_by_name(NULL, "i2s-a");
5218064c
BH
887 if (chip->node && chip->node->parent &&
888 chip->node->parent->parent) {
55b61fec 889 if (of_device_is_compatible(chip->node->parent->parent,
7bbd8277
BH
890 "K2-Keylargo"))
891 chip->is_k2 = 1;
892 }
893 }
1da177e4
LT
894 if (! chip->node)
895 return -ENODEV;
7bbd8277 896
5218064c 897 if (!sound) {
30686ba6 898 sound = of_find_node_by_name(NULL, "sound");
5218064c 899 while (sound && sound->parent != chip->node)
30686ba6 900 sound = of_find_node_by_name(sound, "sound");
5218064c 901 }
30686ba6
SR
902 if (! sound) {
903 of_node_put(chip->node);
41e904de 904 chip->node = NULL;
1da177e4 905 return -ENODEV;
30686ba6 906 }
c4f55b39 907 prop = of_get_property(sound, "sub-frame", NULL);
1da177e4
LT
908 if (prop && *prop < 16)
909 chip->subframe = *prop;
c4f55b39 910 prop = of_get_property(sound, "layout-id", NULL);
55c385ad
JB
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");
41e904de 917 of_node_put(sound);
30686ba6 918 of_node_put(chip->node);
41e904de 919 chip->node = NULL;
55c385ad
JB
920 return -ENODEV;
921 }
1da177e4 922 /* This should be verified on older screamers */
55b61fec 923 if (of_device_is_compatible(sound, "screamer")) {
1da177e4
LT
924 chip->model = PMAC_SCREAMER;
925 // chip->can_byte_swap = 0; /* FIXME: check this */
926 }
55b61fec 927 if (of_device_is_compatible(sound, "burgundy")) {
1da177e4
LT
928 chip->model = PMAC_BURGUNDY;
929 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */
930 }
55b61fec 931 if (of_device_is_compatible(sound, "daca")) {
1da177e4
LT
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 }
55b61fec 938 if (of_device_is_compatible(sound, "tumbler")) {
1da177e4
LT
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 }
55b61fec 947 if (of_device_is_compatible(sound, "snapper")) {
1da177e4
LT
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 }
c4f55b39 954 prop = of_get_property(sound, "device-id", NULL);
1da177e4
LT
955 if (prop)
956 chip->device_id = *prop;
30686ba6
SR
957 dn = of_find_node_by_name(NULL, "perch");
958 chip->has_iic = (dn != NULL);
959 of_node_put(dn);
1da177e4 960
7bbd8277
BH
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)
5218064c
BH
979 printk(KERN_WARNING "snd-powermac: can't locate macio PCI"
980 " device !\n");
7bbd8277 981
1da177e4
LT
982 detect_byte_swap(chip);
983
984 /* look for a property saying what sample rates
985 are available */
c4f55b39 986 prop = of_get_property(sound, "sample-rates", &l);
1da177e4 987 if (! prop)
c4f55b39 988 prop = of_get_property(sound, "output-frame-rates", &l);
1da177e4
LT
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
30686ba6 1009 of_node_put(sound);
1da177e4
LT
1010 return 0;
1011}
1012
1da177e4
LT
1013#ifdef PMAC_SUPPORT_AUTOMUTE
1014/*
1015 * auto-mute
1016 */
65b29f50
TI
1017static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol,
1018 struct snd_ctl_elem_value *ucontrol)
1da177e4 1019{
65b29f50 1020 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
1021 ucontrol->value.integer.value[0] = chip->auto_mute;
1022 return 0;
1023}
1024
65b29f50
TI
1025static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol,
1026 struct snd_ctl_elem_value *ucontrol)
1da177e4 1027{
65b29f50 1028 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1da177e4 1029 if (ucontrol->value.integer.value[0] != chip->auto_mute) {
d4079ac4 1030 chip->auto_mute = !!ucontrol->value.integer.value[0];
1da177e4
LT
1031 if (chip->update_automute)
1032 chip->update_automute(chip, 1);
1033 return 1;
1034 }
1035 return 0;
1036}
1037
65b29f50
TI
1038static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol,
1039 struct snd_ctl_elem_value *ucontrol)
1da177e4 1040{
65b29f50 1041 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
1da177e4
LT
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
65b29f50 1049static struct snd_kcontrol_new auto_mute_controls[] __initdata = {
1da177e4
LT
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
65b29f50 1064int __init snd_pmac_add_automute(struct snd_pmac *chip)
1da177e4
LT
1065{
1066 int err;
1067 chip->auto_mute = 1;
1068 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip));
7bbd8277
BH
1069 if (err < 0) {
1070 printk(KERN_ERR "snd-powermac: Failed to add automute control\n");
1da177e4 1071 return err;
7bbd8277 1072 }
1da177e4
LT
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 */
65b29f50 1081int __init snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return)
1da177e4 1082{
65b29f50 1083 struct snd_pmac *chip;
1da177e4
LT
1084 struct device_node *np;
1085 int i, err;
0ebfff14 1086 unsigned int irq;
7bbd8277 1087 unsigned long ctrl_addr, txdma_addr, rxdma_addr;
65b29f50 1088 static struct snd_device_ops ops = {
1da177e4
LT
1089 .dev_free = snd_pmac_dev_free,
1090 };
1091
1da177e4
LT
1092 *chip_return = NULL;
1093
561b220a 1094 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1da177e4
LT
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
7bbd8277
BH
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) {
1da177e4
LT
1111 err = -ENOMEM;
1112 goto __error;
1113 }
1114
1115 np = chip->node;
cc5d0189 1116 chip->requested = 0;
7bbd8277 1117 if (chip->is_k2) {
cc5d0189
BH
1118 static char *rnames[] = {
1119 "Sound Control", "Sound DMA" };
cc5d0189
BH
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 "
d63fb6c5 1133 " %d (%s: 0x%016llx:%016llx)\n",
aa0a2ddc
GKH
1134 i, rnames[i],
1135 (unsigned long long)chip->rsrc[i].start,
1136 (unsigned long long)chip->rsrc[i].end);
7bbd8277
BH
1137 err = -ENODEV;
1138 goto __error;
1139 }
cc5d0189 1140 chip->requested |= (1 << i);
7bbd8277 1141 }
cc5d0189
BH
1142 ctrl_addr = chip->rsrc[0].start;
1143 txdma_addr = chip->rsrc[1].start;
1144 rxdma_addr = txdma_addr + 0x100;
7bbd8277 1145 } else {
cc5d0189
BH
1146 static char *rnames[] = {
1147 "Sound Control", "Sound Tx DMA", "Sound Rx DMA" };
cc5d0189 1148 for (i = 0; i < 3; i ++) {
88356e90 1149 if (of_address_to_resource(np, i,
cc5d0189
BH
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 "
aa0a2ddc
GKH
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);
7bbd8277
BH
1165 err = -ENODEV;
1166 goto __error;
1167 }
cc5d0189 1168 chip->requested |= (1 << i);
7bbd8277 1169 }
cc5d0189
BH
1170 ctrl_addr = chip->rsrc[0].start;
1171 txdma_addr = chip->rsrc[1].start;
1172 rxdma_addr = chip->rsrc[2].start;
1da177e4
LT
1173 }
1174
7bbd8277
BH
1175 chip->awacs = ioremap(ctrl_addr, 0x1000);
1176 chip->playback.dma = ioremap(txdma_addr, 0x100);
1177 chip->capture.dma = ioremap(rxdma_addr, 0x100);
1da177e4 1178 if (chip->model <= PMAC_BURGUNDY) {
0ebfff14
BH
1179 irq = irq_of_parse_and_map(np, 0);
1180 if (request_irq(irq, snd_pmac_ctrl_intr, 0,
1da177e4 1181 "PMac", (void*)chip)) {
0ebfff14
BH
1182 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n",
1183 irq);
1da177e4
LT
1184 err = -EBUSY;
1185 goto __error;
1186 }
0ebfff14 1187 chip->irq = irq;
1da177e4 1188 }
0ebfff14
BH
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);
1da177e4
LT
1192 err = -EBUSY;
1193 goto __error;
1194 }
0ebfff14
BH
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);
1da177e4
LT
1199 err = -EBUSY;
1200 goto __error;
1201 }
0ebfff14 1202 chip->rx_irq = irq;
1da177e4
LT
1203
1204 snd_pmac_sound_feature(chip, 1);
1205
1206 /* reset */
7bbd8277
BH
1207 if (chip->model == PMAC_AWACS)
1208 out_le32(&chip->awacs->control, 0x11);
1da177e4
LT
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) {
cc5d0189
BH
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);
1da177e4
LT
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
1da177e4
LT
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
8c870933 1265#ifdef CONFIG_PM
1da177e4
LT
1266
1267/*
1268 * Save state when going to sleep, restore it afterwards.
1269 */
1270
5e12bea0 1271void snd_pmac_suspend(struct snd_pmac *chip)
1da177e4 1272{
1da177e4
LT
1273 unsigned long flags;
1274
5e12bea0 1275 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1da177e4
LT
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);
1da177e4
LT
1289}
1290
5e12bea0 1291void snd_pmac_resume(struct snd_pmac *chip)
1da177e4 1292{
1da177e4
LT
1293 snd_pmac_sound_feature(chip, 1);
1294 if (chip->resume)
1295 chip->resume(chip);
1296 /* enable CD sound input */
5e12bea0 1297 if (chip->macio_base && chip->is_pbook_G3)
1da177e4 1298 out_8(chip->macio_base + 0x37, 3);
5e12bea0 1299 else if (chip->is_pbook_3400)
1da177e4 1300 in_8(chip->latch_base + 0x190);
1da177e4
LT
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
5e12bea0 1311 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1da177e4
LT
1312}
1313
8c870933
BH
1314#endif /* CONFIG_PM */
1315