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