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