1/* 2 * PMac DBDMA lowlevel functions 3 * 4 * Copyright (c) by Takashi Iwai <tiwai@suse.de> 5 * code based on dmasound.c. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 23#include <sound/driver.h> 24#include <asm/io.h> 25#include <asm/irq.h> 26#include <linux/init.h> 27#include <linux/delay.h> 28#include <linux/slab.h> 29#include <linux/interrupt.h> 30#include <linux/pci.h> 31#include <linux/dma-mapping.h> 32#include <sound/core.h> 33#include "pmac.h" 34#include <sound/pcm_params.h> 35#include <asm/pmac_feature.h> 36#include <asm/pci-bridge.h> 37 38 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 */ 51static int snd_pmac_dbdma_alloc(struct snd_pmac *chip, struct pmac_dbdma *rec, int size) 52{ 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); 57 if (rec->space == NULL) 58 return -ENOMEM; 59 rec->size = size; 60 memset(rec->space, 0, rsize); 61 rec->cmds = (void __iomem *)DBDMA_ALIGN(rec->space); 62 rec->addr = rec->dma_base + (unsigned long)((char *)rec->cmds - (char *)rec->space); 63 64 return 0; 65} 66 67static void snd_pmac_dbdma_free(struct snd_pmac *chip, struct pmac_dbdma *rec) 68{ 69 if (rec->space) { 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 } 74} 75 76 77/* 78 * pcm stuff 79 */ 80 81/* 82 * look up frequency table 83 */ 84 85unsigned int snd_pmac_rate_index(struct snd_pmac *chip, struct pmac_stream *rec, unsigned int rate) 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 */ 114static int snd_pmac_pcm_hw_params(struct snd_pcm_substream *subs, 115 struct snd_pcm_hw_params *hw_params) 116{ 117 return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params)); 118} 119 120/* 121 * release buffers 122 */ 123static int snd_pmac_pcm_hw_free(struct snd_pcm_substream *subs) 124{ 125 snd_pcm_lib_free_pages(subs); 126 return 0; 127} 128 129/* 130 * get a stream of the opposite direction 131 */ 132static struct pmac_stream *snd_pmac_get_stream(struct snd_pmac *chip, int stream) 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 */ 148static inline void 149snd_pmac_wait_ack(struct pmac_stream *rec) 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 */ 160static void snd_pmac_pcm_set_format(struct snd_pmac *chip) 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 */ 172static inline void snd_pmac_dma_stop(struct pmac_stream *rec) 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 */ 181static inline void snd_pmac_dma_set_command(struct pmac_stream *rec, struct pmac_dbdma *cmd) 182{ 183 out_le32(&rec->dma->cmdptr, cmd->addr); 184} 185 186/* 187 * start the DMA 188 */ 189static inline void snd_pmac_dma_run(struct pmac_stream *rec, int status) 190{ 191 out_le32(&rec->dma->control, status | (status << 16)); 192} 193 194 195/* 196 * prepare playback/capture stream 197 */ 198static int snd_pmac_pcm_prepare(struct snd_pmac *chip, struct pmac_stream *rec, struct snd_pcm_substream *subs) 199{ 200 int i; 201 volatile struct dbdma_cmd __iomem *cp; 202 struct snd_pcm_runtime *runtime = subs->runtime; 203 int rate_index; 204 long offset; 205 struct pmac_stream *astr; 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)); 215 if (! astr) 216 return -EINVAL; 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 */ 239 offset = runtime->dma_addr; 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 */ 262static int snd_pmac_pcm_trigger(struct snd_pmac *chip, struct pmac_stream *rec, 263 struct snd_pcm_substream *subs, int cmd) 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 309static snd_pcm_uframes_t snd_pmac_pcm_pointer(struct snd_pmac *chip, 310 struct pmac_stream *rec, 311 struct snd_pcm_substream *subs) 312{ 313 int count = 0; 314 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 count += rec->cur_period * rec->period_size; 324 /*printk("pointer=%d\n", count);*/ 325 return bytes_to_frames(subs->runtime, count); 326} 327 328/* 329 * playback 330 */ 331 332static int snd_pmac_playback_prepare(struct snd_pcm_substream *subs) 333{ 334 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 335 return snd_pmac_pcm_prepare(chip, &chip->playback, subs); 336} 337 338static int snd_pmac_playback_trigger(struct snd_pcm_substream *subs, 339 int cmd) 340{ 341 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 342 return snd_pmac_pcm_trigger(chip, &chip->playback, subs, cmd); 343} 344 345static snd_pcm_uframes_t snd_pmac_playback_pointer(struct snd_pcm_substream *subs) 346{ 347 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 348 return snd_pmac_pcm_pointer(chip, &chip->playback, subs); 349} 350 351 352/* 353 * capture 354 */ 355 356static int snd_pmac_capture_prepare(struct snd_pcm_substream *subs) 357{ 358 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 359 return snd_pmac_pcm_prepare(chip, &chip->capture, subs); 360} 361 362static int snd_pmac_capture_trigger(struct snd_pcm_substream *subs, 363 int cmd) 364{ 365 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 366 return snd_pmac_pcm_trigger(chip, &chip->capture, subs, cmd); 367} 368 369static snd_pcm_uframes_t snd_pmac_capture_pointer(struct snd_pcm_substream *subs) 370{ 371 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 372 return snd_pmac_pcm_pointer(chip, &chip->capture, subs); 373} 374 375 376/* 377 * update playback/capture pointer from interrupts 378 */ 379static void snd_pmac_pcm_update(struct snd_pmac *chip, struct pmac_stream *rec) 380{ 381 volatile struct dbdma_cmd __iomem *cp; 382 int c; 383 int stat; 384 385 spin_lock(&chip->reg_lock); 386 if (rec->running) { 387 cp = &rec->cmd.cmds[rec->cur_period]; 388 for (c = 0; c < rec->nperiods; c++) { /* at most all fragments */ 389 stat = ld_le16(&cp->xfer_status); 390 if (! (stat & ACTIVE)) 391 break; 392 /*printk("update frag %d\n", rec->cur_period);*/ 393 st_le16(&cp->xfer_status, 0); 394 st_le16(&cp->req_count, rec->period_size); 395 /*st_le16(&cp->res_count, 0);*/ 396 rec->cur_period++; 397 if (rec->cur_period >= rec->nperiods) { 398 rec->cur_period = 0; 399 cp = rec->cmd.cmds; 400 } else 401 cp++; 402 spin_unlock(&chip->reg_lock); 403 snd_pcm_period_elapsed(rec->substream); 404 spin_lock(&chip->reg_lock); 405 } 406 } 407 spin_unlock(&chip->reg_lock); 408} 409 410 411/* 412 * hw info 413 */ 414 415static struct snd_pcm_hardware snd_pmac_playback = 416{ 417 .info = (SNDRV_PCM_INFO_INTERLEAVED | 418 SNDRV_PCM_INFO_MMAP | 419 SNDRV_PCM_INFO_MMAP_VALID | 420 SNDRV_PCM_INFO_RESUME), 421 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE, 422 .rates = SNDRV_PCM_RATE_8000_44100, 423 .rate_min = 7350, 424 .rate_max = 44100, 425 .channels_min = 2, 426 .channels_max = 2, 427 .buffer_bytes_max = 131072, 428 .period_bytes_min = 256, 429 .period_bytes_max = 16384, 430 .periods_min = 3, 431 .periods_max = PMAC_MAX_FRAGS, 432}; 433 434static struct snd_pcm_hardware snd_pmac_capture = 435{ 436 .info = (SNDRV_PCM_INFO_INTERLEAVED | 437 SNDRV_PCM_INFO_MMAP | 438 SNDRV_PCM_INFO_MMAP_VALID | 439 SNDRV_PCM_INFO_RESUME), 440 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_LE, 441 .rates = SNDRV_PCM_RATE_8000_44100, 442 .rate_min = 7350, 443 .rate_max = 44100, 444 .channels_min = 2, 445 .channels_max = 2, 446 .buffer_bytes_max = 131072, 447 .period_bytes_min = 256, 448 .period_bytes_max = 16384, 449 .periods_min = 3, 450 .periods_max = PMAC_MAX_FRAGS, 451}; 452 453 454 455static int snd_pmac_pcm_open(struct snd_pmac *chip, struct pmac_stream *rec, 456 struct snd_pcm_substream *subs) 457{ 458 struct snd_pcm_runtime *runtime = subs->runtime; 459 int i, j, fflags; 460 static int typical_freqs[] = { 461 44100, 462 22050, 463 11025, 464 0, 465 }; 466 static int typical_freq_flags[] = { 467 SNDRV_PCM_RATE_44100, 468 SNDRV_PCM_RATE_22050, 469 SNDRV_PCM_RATE_11025, 470 0, 471 }; 472 473 /* look up frequency table and fill bit mask */ 474 runtime->hw.rates = 0; 475 fflags = chip->freqs_ok; 476 for (i = 0; typical_freqs[i]; i++) { 477 for (j = 0; j < chip->num_freqs; j++) { 478 if ((chip->freqs_ok & (1 << j)) && 479 chip->freq_table[j] == typical_freqs[i]) { 480 runtime->hw.rates |= typical_freq_flags[i]; 481 fflags &= ~(1 << j); 482 break; 483 } 484 } 485 } 486 if (fflags) /* rest */ 487 runtime->hw.rates |= SNDRV_PCM_RATE_KNOT; 488 489 /* check for minimum and maximum rates */ 490 for (i = 0; i < chip->num_freqs; i++) { 491 if (chip->freqs_ok & (1 << i)) { 492 runtime->hw.rate_max = chip->freq_table[i]; 493 break; 494 } 495 } 496 for (i = chip->num_freqs - 1; i >= 0; i--) { 497 if (chip->freqs_ok & (1 << i)) { 498 runtime->hw.rate_min = chip->freq_table[i]; 499 break; 500 } 501 } 502 runtime->hw.formats = chip->formats_ok; 503 if (chip->can_capture) { 504 if (! chip->can_duplex) 505 runtime->hw.info |= SNDRV_PCM_INFO_HALF_DUPLEX; 506 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX; 507 } 508 runtime->private_data = rec; 509 rec->substream = subs; 510 511 512 runtime->hw.periods_max = rec->cmd.size - 1; 513 514 if (chip->can_duplex) 515 snd_pcm_set_sync(subs); 516 517 /* constraints to fix choppy sound */ 518 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 519 return 0; 520} 521 522static int snd_pmac_pcm_close(struct snd_pmac *chip, struct pmac_stream *rec, 523 struct snd_pcm_substream *subs) 524{ 525 struct pmac_stream *astr; 526 527 snd_pmac_dma_stop(rec); 528 529 astr = snd_pmac_get_stream(chip, another_stream(rec->stream)); 530 if (! astr) 531 return -EINVAL; 532 533 /* reset constraints */ 534 astr->cur_freqs = chip->freqs_ok; 535 astr->cur_formats = chip->formats_ok; 536 537 return 0; 538} 539 540static int snd_pmac_playback_open(struct snd_pcm_substream *subs) 541{ 542 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 543 544 subs->runtime->hw = snd_pmac_playback; 545 return snd_pmac_pcm_open(chip, &chip->playback, subs); 546} 547 548static int snd_pmac_capture_open(struct snd_pcm_substream *subs) 549{ 550 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 551 552 subs->runtime->hw = snd_pmac_capture; 553 return snd_pmac_pcm_open(chip, &chip->capture, subs); 554} 555 556static int snd_pmac_playback_close(struct snd_pcm_substream *subs) 557{ 558 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 559 560 return snd_pmac_pcm_close(chip, &chip->playback, subs); 561} 562 563static int snd_pmac_capture_close(struct snd_pcm_substream *subs) 564{ 565 struct snd_pmac *chip = snd_pcm_substream_chip(subs); 566 567 return snd_pmac_pcm_close(chip, &chip->capture, subs); 568} 569 570/* 571 */ 572 573static struct snd_pcm_ops snd_pmac_playback_ops = { 574 .open = snd_pmac_playback_open, 575 .close = snd_pmac_playback_close, 576 .ioctl = snd_pcm_lib_ioctl, 577 .hw_params = snd_pmac_pcm_hw_params, 578 .hw_free = snd_pmac_pcm_hw_free, 579 .prepare = snd_pmac_playback_prepare, 580 .trigger = snd_pmac_playback_trigger, 581 .pointer = snd_pmac_playback_pointer, 582}; 583 584static struct snd_pcm_ops snd_pmac_capture_ops = { 585 .open = snd_pmac_capture_open, 586 .close = snd_pmac_capture_close, 587 .ioctl = snd_pcm_lib_ioctl, 588 .hw_params = snd_pmac_pcm_hw_params, 589 .hw_free = snd_pmac_pcm_hw_free, 590 .prepare = snd_pmac_capture_prepare, 591 .trigger = snd_pmac_capture_trigger, 592 .pointer = snd_pmac_capture_pointer, 593}; 594 595int __init snd_pmac_pcm_new(struct snd_pmac *chip) 596{ 597 struct snd_pcm *pcm; 598 int err; 599 int num_captures = 1; 600 601 if (! chip->can_capture) 602 num_captures = 0; 603 err = snd_pcm_new(chip->card, chip->card->driver, 0, 1, num_captures, &pcm); 604 if (err < 0) 605 return err; 606 607 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_pmac_playback_ops); 608 if (chip->can_capture) 609 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_pmac_capture_ops); 610 611 pcm->private_data = chip; 612 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX; 613 strcpy(pcm->name, chip->card->shortname); 614 chip->pcm = pcm; 615 616 chip->formats_ok = SNDRV_PCM_FMTBIT_S16_BE; 617 if (chip->can_byte_swap) 618 chip->formats_ok |= SNDRV_PCM_FMTBIT_S16_LE; 619 620 chip->playback.cur_formats = chip->formats_ok; 621 chip->capture.cur_formats = chip->formats_ok; 622 chip->playback.cur_freqs = chip->freqs_ok; 623 chip->capture.cur_freqs = chip->freqs_ok; 624 625 /* preallocate 64k buffer */ 626 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 627 &chip->pdev->dev, 628 64 * 1024, 64 * 1024); 629 630 return 0; 631} 632 633 634static void snd_pmac_dbdma_reset(struct snd_pmac *chip) 635{ 636 out_le32(&chip->playback.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16); 637 snd_pmac_wait_ack(&chip->playback); 638 out_le32(&chip->capture.dma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16); 639 snd_pmac_wait_ack(&chip->capture); 640} 641 642 643/* 644 * handling beep 645 */ 646void snd_pmac_beep_dma_start(struct snd_pmac *chip, int bytes, unsigned long addr, int speed) 647{ 648 struct pmac_stream *rec = &chip->playback; 649 650 snd_pmac_dma_stop(rec); 651 st_le16(&chip->extra_dma.cmds->req_count, bytes); 652 st_le16(&chip->extra_dma.cmds->xfer_status, 0); 653 st_le32(&chip->extra_dma.cmds->cmd_dep, chip->extra_dma.addr); 654 st_le32(&chip->extra_dma.cmds->phy_addr, addr); 655 st_le16(&chip->extra_dma.cmds->command, OUTPUT_MORE + BR_ALWAYS); 656 out_le32(&chip->awacs->control, 657 (in_le32(&chip->awacs->control) & ~0x1f00) 658 | (speed << 8)); 659 out_le32(&chip->awacs->byteswap, 0); 660 snd_pmac_dma_set_command(rec, &chip->extra_dma); 661 snd_pmac_dma_run(rec, RUN); 662} 663 664void snd_pmac_beep_dma_stop(struct snd_pmac *chip) 665{ 666 snd_pmac_dma_stop(&chip->playback); 667 st_le16(&chip->extra_dma.cmds->command, DBDMA_STOP); 668 snd_pmac_pcm_set_format(chip); /* reset format */ 669} 670 671 672/* 673 * interrupt handlers 674 */ 675static irqreturn_t 676snd_pmac_tx_intr(int irq, void *devid) 677{ 678 struct snd_pmac *chip = devid; 679 snd_pmac_pcm_update(chip, &chip->playback); 680 return IRQ_HANDLED; 681} 682 683 684static irqreturn_t 685snd_pmac_rx_intr(int irq, void *devid) 686{ 687 struct snd_pmac *chip = devid; 688 snd_pmac_pcm_update(chip, &chip->capture); 689 return IRQ_HANDLED; 690} 691 692 693static irqreturn_t 694snd_pmac_ctrl_intr(int irq, void *devid) 695{ 696 struct snd_pmac *chip = devid; 697 int ctrl = in_le32(&chip->awacs->control); 698 699 /*printk("pmac: control interrupt.. 0x%x\n", ctrl);*/ 700 if (ctrl & MASK_PORTCHG) { 701 /* do something when headphone is plugged/unplugged? */ 702 if (chip->update_automute) 703 chip->update_automute(chip, 1); 704 } 705 if (ctrl & MASK_CNTLERR) { 706 int err = (in_le32(&chip->awacs->codec_stat) & MASK_ERRCODE) >> 16; 707 if (err && chip->model <= PMAC_SCREAMER) 708 snd_printk(KERN_DEBUG "error %x\n", err); 709 } 710 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */ 711 out_le32(&chip->awacs->control, ctrl); 712 return IRQ_HANDLED; 713} 714 715 716/* 717 * a wrapper to feature call for compatibility 718 */ 719static void snd_pmac_sound_feature(struct snd_pmac *chip, int enable) 720{ 721 if (ppc_md.feature_call) 722 ppc_md.feature_call(PMAC_FTR_SOUND_CHIP_ENABLE, chip->node, 0, enable); 723} 724 725/* 726 * release resources 727 */ 728 729static int snd_pmac_free(struct snd_pmac *chip) 730{ 731 /* stop sounds */ 732 if (chip->initialized) { 733 snd_pmac_dbdma_reset(chip); 734 /* disable interrupts from awacs interface */ 735 out_le32(&chip->awacs->control, in_le32(&chip->awacs->control) & 0xfff); 736 } 737 738 if (chip->node) 739 snd_pmac_sound_feature(chip, 0); 740 741 /* clean up mixer if any */ 742 if (chip->mixer_free) 743 chip->mixer_free(chip); 744 745 snd_pmac_detach_beep(chip); 746 747 /* release resources */ 748 if (chip->irq >= 0) 749 free_irq(chip->irq, (void*)chip); 750 if (chip->tx_irq >= 0) 751 free_irq(chip->tx_irq, (void*)chip); 752 if (chip->rx_irq >= 0) 753 free_irq(chip->rx_irq, (void*)chip); 754 snd_pmac_dbdma_free(chip, &chip->playback.cmd); 755 snd_pmac_dbdma_free(chip, &chip->capture.cmd); 756 snd_pmac_dbdma_free(chip, &chip->extra_dma); 757 if (chip->macio_base) 758 iounmap(chip->macio_base); 759 if (chip->latch_base) 760 iounmap(chip->latch_base); 761 if (chip->awacs) 762 iounmap(chip->awacs); 763 if (chip->playback.dma) 764 iounmap(chip->playback.dma); 765 if (chip->capture.dma) 766 iounmap(chip->capture.dma); 767 768 if (chip->node) { 769 int i; 770 for (i = 0; i < 3; i++) { 771 if (chip->requested & (1 << i)) 772 release_mem_region(chip->rsrc[i].start, 773 chip->rsrc[i].end - 774 chip->rsrc[i].start + 1); 775 } 776 } 777 778 if (chip->pdev) 779 pci_dev_put(chip->pdev); 780 of_node_put(chip->node); 781 kfree(chip); 782 return 0; 783} 784 785 786/* 787 * free the device 788 */ 789static int snd_pmac_dev_free(struct snd_device *device) 790{ 791 struct snd_pmac *chip = device->device_data; 792 return snd_pmac_free(chip); 793} 794 795 796/* 797 * check the machine support byteswap (little-endian) 798 */ 799 800static void __init detect_byte_swap(struct snd_pmac *chip) 801{ 802 struct device_node *mio; 803 804 /* if seems that Keylargo can't byte-swap */ 805 for (mio = chip->node->parent; mio; mio = mio->parent) { 806 if (strcmp(mio->name, "mac-io") == 0) { 807 if (of_device_is_compatible(mio, "Keylargo")) 808 chip->can_byte_swap = 0; 809 break; 810 } 811 } 812 813 /* it seems the Pismo & iBook can't byte-swap in hardware. */ 814 if (machine_is_compatible("PowerBook3,1") || 815 machine_is_compatible("PowerBook2,1")) 816 chip->can_byte_swap = 0 ; 817 818 if (machine_is_compatible("PowerBook2,1")) 819 chip->can_duplex = 0; 820} 821 822 823/* 824 * detect a sound chip 825 */ 826static int __init snd_pmac_detect(struct snd_pmac *chip) 827{ 828 struct device_node *sound; 829 struct device_node *dn; 830 const unsigned int *prop; 831 unsigned int l; 832 struct macio_chip* macio; 833 834 if (!machine_is(powermac)) 835 return -ENODEV; 836 837 chip->subframe = 0; 838 chip->revision = 0; 839 chip->freqs_ok = 0xff; /* all ok */ 840 chip->model = PMAC_AWACS; 841 chip->can_byte_swap = 1; 842 chip->can_duplex = 1; 843 chip->can_capture = 1; 844 chip->num_freqs = ARRAY_SIZE(awacs_freqs); 845 chip->freq_table = awacs_freqs; 846 chip->pdev = NULL; 847 848 chip->control_mask = MASK_IEPC | MASK_IEE | 0x11; /* default */ 849 850 /* check machine type */ 851 if (machine_is_compatible("AAPL,3400/2400") 852 || machine_is_compatible("AAPL,3500")) 853 chip->is_pbook_3400 = 1; 854 else if (machine_is_compatible("PowerBook1,1") 855 || machine_is_compatible("AAPL,PowerBook1998")) 856 chip->is_pbook_G3 = 1; 857 chip->node = of_find_node_by_name(NULL, "awacs"); 858 sound = of_node_get(chip->node); 859 860 /* 861 * powermac G3 models have a node called "davbus" 862 * with a child called "sound". 863 */ 864 if (!chip->node) 865 chip->node = of_find_node_by_name(NULL, "davbus"); 866 /* 867 * if we didn't find a davbus device, try 'i2s-a' since 868 * this seems to be what iBooks have 869 */ 870 if (! chip->node) { 871 chip->node = of_find_node_by_name(NULL, "i2s-a"); 872 if (chip->node && chip->node->parent && 873 chip->node->parent->parent) { 874 if (of_device_is_compatible(chip->node->parent->parent, 875 "K2-Keylargo")) 876 chip->is_k2 = 1; 877 } 878 } 879 if (! chip->node) 880 return -ENODEV; 881 882 if (!sound) { 883 sound = of_find_node_by_name(NULL, "sound"); 884 while (sound && sound->parent != chip->node) 885 sound = of_find_node_by_name(sound, "sound"); 886 } 887 if (! sound) { 888 of_node_put(chip->node); 889 chip->node = NULL; 890 return -ENODEV; 891 } 892 prop = of_get_property(sound, "sub-frame", NULL); 893 if (prop && *prop < 16) 894 chip->subframe = *prop; 895 prop = of_get_property(sound, "layout-id", NULL); 896 if (prop) { 897 /* partly deprecate snd-powermac, for those machines 898 * that have a layout-id property for now */ 899 printk(KERN_INFO "snd-powermac no longer handles any " 900 "machines with a layout-id property " 901 "in the device-tree, use snd-aoa.\n"); 902 of_node_put(sound); 903 of_node_put(chip->node); 904 chip->node = NULL; 905 return -ENODEV; 906 } 907 /* This should be verified on older screamers */ 908 if (of_device_is_compatible(sound, "screamer")) { 909 chip->model = PMAC_SCREAMER; 910 // chip->can_byte_swap = 0; 911 } 912 if (of_device_is_compatible(sound, "burgundy")) { 913 chip->model = PMAC_BURGUNDY; 914 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */ 915 } 916 if (of_device_is_compatible(sound, "daca")) { 917 chip->model = PMAC_DACA; 918 chip->can_capture = 0; /* no capture */ 919 chip->can_duplex = 0; 920 // chip->can_byte_swap = 0; 921 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */ 922 } 923 if (of_device_is_compatible(sound, "tumbler")) { 924 chip->model = PMAC_TUMBLER; 925 chip->can_capture = 0; /* no capture */ 926 chip->can_duplex = 0; 927 // chip->can_byte_swap = 0; 928 chip->num_freqs = ARRAY_SIZE(tumbler_freqs); 929 chip->freq_table = tumbler_freqs; 930 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */ 931 } 932 if (of_device_is_compatible(sound, "snapper")) { 933 chip->model = PMAC_SNAPPER; 934 // chip->can_byte_swap = 0; 935 chip->num_freqs = ARRAY_SIZE(tumbler_freqs); 936 chip->freq_table = tumbler_freqs; 937 chip->control_mask = MASK_IEPC | 0x11; /* disable IEE */ 938 } 939 prop = of_get_property(sound, "device-id", NULL); 940 if (prop) 941 chip->device_id = *prop; 942 dn = of_find_node_by_name(NULL, "perch"); 943 chip->has_iic = (dn != NULL); 944 of_node_put(dn); 945 946 /* We need the PCI device for DMA allocations, let's use a crude method 947 * for now ... 948 */ 949 macio = macio_find(chip->node, macio_unknown); 950 if (macio == NULL) 951 printk(KERN_WARNING "snd-powermac: can't locate macio !\n"); 952 else { 953 struct pci_dev *pdev = NULL; 954 955 for_each_pci_dev(pdev) { 956 struct device_node *np = pci_device_to_OF_node(pdev); 957 if (np && np == macio->of_node) { 958 chip->pdev = pdev; 959 break; 960 } 961 } 962 } 963 if (chip->pdev == NULL) 964 printk(KERN_WARNING "snd-powermac: can't locate macio PCI" 965 " device !\n"); 966 967 detect_byte_swap(chip); 968 969 /* look for a property saying what sample rates 970 are available */ 971 prop = of_get_property(sound, "sample-rates", &l); 972 if (! prop) 973 prop = of_get_property(sound, "output-frame-rates", &l); 974 if (prop) { 975 int i; 976 chip->freqs_ok = 0; 977 for (l /= sizeof(int); l > 0; --l) { 978 unsigned int r = *prop++; 979 /* Apple 'Fixed' format */ 980 if (r >= 0x10000) 981 r >>= 16; 982 for (i = 0; i < chip->num_freqs; ++i) { 983 if (r == chip->freq_table[i]) { 984 chip->freqs_ok |= (1 << i); 985 break; 986 } 987 } 988 } 989 } else { 990 /* assume only 44.1khz */ 991 chip->freqs_ok = 1; 992 } 993 994 of_node_put(sound); 995 return 0; 996} 997 998/* 999 * exported - boolean info callbacks for ease of programming 1000 */ 1001int snd_pmac_boolean_stereo_info(struct snd_kcontrol *kcontrol, 1002 struct snd_ctl_elem_info *uinfo) 1003{ 1004 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1005 uinfo->count = 2; 1006 uinfo->value.integer.min = 0; 1007 uinfo->value.integer.max = 1; 1008 return 0; 1009} 1010 1011int snd_pmac_boolean_mono_info(struct snd_kcontrol *kcontrol, 1012 struct snd_ctl_elem_info *uinfo) 1013{ 1014 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 1015 uinfo->count = 1; 1016 uinfo->value.integer.min = 0; 1017 uinfo->value.integer.max = 1; 1018 return 0; 1019} 1020 1021#ifdef PMAC_SUPPORT_AUTOMUTE 1022/* 1023 * auto-mute 1024 */ 1025static int pmac_auto_mute_get(struct snd_kcontrol *kcontrol, 1026 struct snd_ctl_elem_value *ucontrol) 1027{ 1028 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol); 1029 ucontrol->value.integer.value[0] = chip->auto_mute; 1030 return 0; 1031} 1032 1033static int pmac_auto_mute_put(struct snd_kcontrol *kcontrol, 1034 struct snd_ctl_elem_value *ucontrol) 1035{ 1036 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol); 1037 if (ucontrol->value.integer.value[0] != chip->auto_mute) { 1038 chip->auto_mute = ucontrol->value.integer.value[0]; 1039 if (chip->update_automute) 1040 chip->update_automute(chip, 1); 1041 return 1; 1042 } 1043 return 0; 1044} 1045 1046static int pmac_hp_detect_get(struct snd_kcontrol *kcontrol, 1047 struct snd_ctl_elem_value *ucontrol) 1048{ 1049 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol); 1050 if (chip->detect_headphone) 1051 ucontrol->value.integer.value[0] = chip->detect_headphone(chip); 1052 else 1053 ucontrol->value.integer.value[0] = 0; 1054 return 0; 1055} 1056 1057static struct snd_kcontrol_new auto_mute_controls[] __initdata = { 1058 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1059 .name = "Auto Mute Switch", 1060 .info = snd_pmac_boolean_mono_info, 1061 .get = pmac_auto_mute_get, 1062 .put = pmac_auto_mute_put, 1063 }, 1064 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1065 .name = "Headphone Detection", 1066 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1067 .info = snd_pmac_boolean_mono_info, 1068 .get = pmac_hp_detect_get, 1069 }, 1070}; 1071 1072int __init snd_pmac_add_automute(struct snd_pmac *chip) 1073{ 1074 int err; 1075 chip->auto_mute = 1; 1076 err = snd_ctl_add(chip->card, snd_ctl_new1(&auto_mute_controls[0], chip)); 1077 if (err < 0) { 1078 printk(KERN_ERR "snd-powermac: Failed to add automute control\n"); 1079 return err; 1080 } 1081 chip->hp_detect_ctl = snd_ctl_new1(&auto_mute_controls[1], chip); 1082 return snd_ctl_add(chip->card, chip->hp_detect_ctl); 1083} 1084#endif /* PMAC_SUPPORT_AUTOMUTE */ 1085 1086/* 1087 * create and detect a pmac chip record 1088 */ 1089int __init snd_pmac_new(struct snd_card *card, struct snd_pmac **chip_return) 1090{ 1091 struct snd_pmac *chip; 1092 struct device_node *np; 1093 int i, err; 1094 unsigned int irq; 1095 unsigned long ctrl_addr, txdma_addr, rxdma_addr; 1096 static struct snd_device_ops ops = { 1097 .dev_free = snd_pmac_dev_free, 1098 }; 1099 1100 *chip_return = NULL; 1101 1102 chip = kzalloc(sizeof(*chip), GFP_KERNEL); 1103 if (chip == NULL) 1104 return -ENOMEM; 1105 chip->card = card; 1106 1107 spin_lock_init(&chip->reg_lock); 1108 chip->irq = chip->tx_irq = chip->rx_irq = -1; 1109 1110 chip->playback.stream = SNDRV_PCM_STREAM_PLAYBACK; 1111 chip->capture.stream = SNDRV_PCM_STREAM_CAPTURE; 1112 1113 if ((err = snd_pmac_detect(chip)) < 0) 1114 goto __error; 1115 1116 if (snd_pmac_dbdma_alloc(chip, &chip->playback.cmd, PMAC_MAX_FRAGS + 1) < 0 || 1117 snd_pmac_dbdma_alloc(chip, &chip->capture.cmd, PMAC_MAX_FRAGS + 1) < 0 || 1118 snd_pmac_dbdma_alloc(chip, &chip->extra_dma, 2) < 0) { 1119 err = -ENOMEM; 1120 goto __error; 1121 } 1122 1123 np = chip->node; 1124 chip->requested = 0; 1125 if (chip->is_k2) { 1126 static char *rnames[] = { 1127 "Sound Control", "Sound DMA" }; 1128 for (i = 0; i < 2; i ++) { 1129 if (of_address_to_resource(np->parent, i, 1130 &chip->rsrc[i])) { 1131 printk(KERN_ERR "snd: can't translate rsrc " 1132 " %d (%s)\n", i, rnames[i]); 1133 err = -ENODEV; 1134 goto __error; 1135 } 1136 if (request_mem_region(chip->rsrc[i].start, 1137 chip->rsrc[i].end - 1138 chip->rsrc[i].start + 1, 1139 rnames[i]) == NULL) { 1140 printk(KERN_ERR "snd: can't request rsrc " 1141 " %d (%s: 0x%016llx:%016llx)\n", 1142 i, rnames[i], 1143 (unsigned long long)chip->rsrc[i].start, 1144 (unsigned long long)chip->rsrc[i].end); 1145 err = -ENODEV; 1146 goto __error; 1147 } 1148 chip->requested |= (1 << i); 1149 } 1150 ctrl_addr = chip->rsrc[0].start; 1151 txdma_addr = chip->rsrc[1].start; 1152 rxdma_addr = txdma_addr + 0x100; 1153 } else { 1154 static char *rnames[] = { 1155 "Sound Control", "Sound Tx DMA", "Sound Rx DMA" }; 1156 for (i = 0; i < 3; i ++) { 1157 if (of_address_to_resource(np, i, 1158 &chip->rsrc[i])) { 1159 printk(KERN_ERR "snd: can't translate rsrc " 1160 " %d (%s)\n", i, rnames[i]); 1161 err = -ENODEV; 1162 goto __error; 1163 } 1164 if (request_mem_region(chip->rsrc[i].start, 1165 chip->rsrc[i].end - 1166 chip->rsrc[i].start + 1, 1167 rnames[i]) == NULL) { 1168 printk(KERN_ERR "snd: can't request rsrc " 1169 " %d (%s: 0x%016llx:%016llx)\n", 1170 i, rnames[i], 1171 (unsigned long long)chip->rsrc[i].start, 1172 (unsigned long long)chip->rsrc[i].end); 1173 err = -ENODEV; 1174 goto __error; 1175 } 1176 chip->requested |= (1 << i); 1177 } 1178 ctrl_addr = chip->rsrc[0].start; 1179 txdma_addr = chip->rsrc[1].start; 1180 rxdma_addr = chip->rsrc[2].start; 1181 } 1182 1183 chip->awacs = ioremap(ctrl_addr, 0x1000); 1184 chip->playback.dma = ioremap(txdma_addr, 0x100); 1185 chip->capture.dma = ioremap(rxdma_addr, 0x100); 1186 if (chip->model <= PMAC_BURGUNDY) { 1187 irq = irq_of_parse_and_map(np, 0); 1188 if (request_irq(irq, snd_pmac_ctrl_intr, 0, 1189 "PMac", (void*)chip)) { 1190 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", 1191 irq); 1192 err = -EBUSY; 1193 goto __error; 1194 } 1195 chip->irq = irq; 1196 } 1197 irq = irq_of_parse_and_map(np, 1); 1198 if (request_irq(irq, snd_pmac_tx_intr, 0, "PMac Output", (void*)chip)){ 1199 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq); 1200 err = -EBUSY; 1201 goto __error; 1202 } 1203 chip->tx_irq = irq; 1204 irq = irq_of_parse_and_map(np, 2); 1205 if (request_irq(irq, snd_pmac_rx_intr, 0, "PMac Input", (void*)chip)) { 1206 snd_printk(KERN_ERR "pmac: unable to grab IRQ %d\n", irq); 1207 err = -EBUSY; 1208 goto __error; 1209 } 1210 chip->rx_irq = irq; 1211 1212 snd_pmac_sound_feature(chip, 1); 1213 1214 /* reset */ 1215 if (chip->model == PMAC_AWACS) 1216 out_le32(&chip->awacs->control, 0x11); 1217 1218 /* Powerbooks have odd ways of enabling inputs such as 1219 an expansion-bay CD or sound from an internal modem 1220 or a PC-card modem. */ 1221 if (chip->is_pbook_3400) { 1222 /* Enable CD and PC-card sound inputs. */ 1223 /* This is done by reading from address 1224 * f301a000, + 0x10 to enable the expansion-bay 1225 * CD sound input, + 0x80 to enable the PC-card 1226 * sound input. The 0x100 enables the SCSI bus 1227 * terminator power. 1228 */ 1229 chip->latch_base = ioremap (0xf301a000, 0x1000); 1230 in_8(chip->latch_base + 0x190); 1231 } else if (chip->is_pbook_G3) { 1232 struct device_node* mio; 1233 for (mio = chip->node->parent; mio; mio = mio->parent) { 1234 if (strcmp(mio->name, "mac-io") == 0) { 1235 struct resource r; 1236 if (of_address_to_resource(mio, 0, &r) == 0) 1237 chip->macio_base = 1238 ioremap(r.start, 0x40); 1239 break; 1240 } 1241 } 1242 /* Enable CD sound input. */ 1243 /* The relevant bits for writing to this byte are 0x8f. 1244 * I haven't found out what the 0x80 bit does. 1245 * For the 0xf bits, writing 3 or 7 enables the CD 1246 * input, any other value disables it. Values 1247 * 1, 3, 5, 7 enable the microphone. Values 0, 2, 1248 * 4, 6, 8 - f enable the input from the modem. 1249 */ 1250 if (chip->macio_base) 1251 out_8(chip->macio_base + 0x37, 3); 1252 } 1253 1254 /* Reset dbdma channels */ 1255 snd_pmac_dbdma_reset(chip); 1256 1257 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) 1258 goto __error; 1259 1260 *chip_return = chip; 1261 return 0; 1262 1263 __error: 1264 snd_pmac_free(chip); 1265 return err; 1266} 1267 1268 1269/* 1270 * sleep notify for powerbook 1271 */ 1272 1273#ifdef CONFIG_PM 1274 1275/* 1276 * Save state when going to sleep, restore it afterwards. 1277 */ 1278 1279void snd_pmac_suspend(struct snd_pmac *chip) 1280{ 1281 unsigned long flags; 1282 1283 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); 1284 if (chip->suspend) 1285 chip->suspend(chip); 1286 snd_pcm_suspend_all(chip->pcm); 1287 spin_lock_irqsave(&chip->reg_lock, flags); 1288 snd_pmac_beep_stop(chip); 1289 spin_unlock_irqrestore(&chip->reg_lock, flags); 1290 if (chip->irq >= 0) 1291 disable_irq(chip->irq); 1292 if (chip->tx_irq >= 0) 1293 disable_irq(chip->tx_irq); 1294 if (chip->rx_irq >= 0) 1295 disable_irq(chip->rx_irq); 1296 snd_pmac_sound_feature(chip, 0); 1297} 1298 1299void snd_pmac_resume(struct snd_pmac *chip) 1300{ 1301 snd_pmac_sound_feature(chip, 1); 1302 if (chip->resume) 1303 chip->resume(chip); 1304 /* enable CD sound input */ 1305 if (chip->macio_base && chip->is_pbook_G3) 1306 out_8(chip->macio_base + 0x37, 3); 1307 else if (chip->is_pbook_3400) 1308 in_8(chip->latch_base + 0x190); 1309 1310 snd_pmac_pcm_set_format(chip); 1311 1312 if (chip->irq >= 0) 1313 enable_irq(chip->irq); 1314 if (chip->tx_irq >= 0) 1315 enable_irq(chip->tx_irq); 1316 if (chip->rx_irq >= 0) 1317 enable_irq(chip->rx_irq); 1318 1319 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); 1320} 1321 1322#endif /* CONFIG_PM */ 1323