1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Sound driver for Silicon Graphics O2 Workstations A/V board audio. 4 * 5 * Copyright 2003 Vivien Chappelier <vivien.chappelier@linux-mips.org> 6 * Copyright 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de> 7 * Mxier part taken from mace_audio.c: 8 * Copyright 2007 Thorben Jändling <tj.trevelyan@gmail.com> 9 */ 10 11 #include <linux/init.h> 12 #include <linux/delay.h> 13 #include <linux/spinlock.h> 14 #include <linux/interrupt.h> 15 #include <linux/dma-mapping.h> 16 #include <linux/platform_device.h> 17 #include <linux/io.h> 18 #include <linux/slab.h> 19 #include <linux/string.h> 20 #include <linux/module.h> 21 22 #include <asm/ip32/ip32_ints.h> 23 #include <asm/ip32/mace.h> 24 25 #include <sound/core.h> 26 #include <sound/control.h> 27 #include <sound/pcm.h> 28 #define SNDRV_GET_ID 29 #include <sound/initval.h> 30 #include <sound/ad1843.h> 31 32 33 MODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org>"); 34 MODULE_DESCRIPTION("SGI O2 Audio"); 35 MODULE_LICENSE("GPL"); 36 37 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ 38 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ 39 40 module_param(index, int, 0444); 41 MODULE_PARM_DESC(index, "Index value for SGI O2 soundcard."); 42 module_param(id, charp, 0444); 43 MODULE_PARM_DESC(id, "ID string for SGI O2 soundcard."); 44 45 46 #define AUDIO_CONTROL_RESET BIT(0) /* 1: reset audio interface */ 47 #define AUDIO_CONTROL_CODEC_PRESENT BIT(1) /* 1: codec detected */ 48 49 #define CODEC_CONTROL_WORD_SHIFT 0 50 #define CODEC_CONTROL_READ BIT(16) 51 #define CODEC_CONTROL_ADDRESS_SHIFT 17 52 53 #define CHANNEL_CONTROL_RESET BIT(10) /* 1: reset channel */ 54 #define CHANNEL_DMA_ENABLE BIT(9) /* 1: enable DMA transfer */ 55 #define CHANNEL_INT_THRESHOLD_DISABLED (0 << 5) /* interrupt disabled */ 56 #define CHANNEL_INT_THRESHOLD_25 (1 << 5) /* int on buffer >25% full */ 57 #define CHANNEL_INT_THRESHOLD_50 (2 << 5) /* int on buffer >50% full */ 58 #define CHANNEL_INT_THRESHOLD_75 (3 << 5) /* int on buffer >75% full */ 59 #define CHANNEL_INT_THRESHOLD_EMPTY (4 << 5) /* int on buffer empty */ 60 #define CHANNEL_INT_THRESHOLD_NOT_EMPTY (5 << 5) /* int on buffer !empty */ 61 #define CHANNEL_INT_THRESHOLD_FULL (6 << 5) /* int on buffer empty */ 62 #define CHANNEL_INT_THRESHOLD_NOT_FULL (7 << 5) /* int on buffer !empty */ 63 64 #define CHANNEL_RING_SHIFT 12 65 #define CHANNEL_RING_SIZE (1 << CHANNEL_RING_SHIFT) 66 #define CHANNEL_RING_MASK (CHANNEL_RING_SIZE - 1) 67 68 #define CHANNEL_LEFT_SHIFT 40 69 #define CHANNEL_RIGHT_SHIFT 8 70 71 struct snd_sgio2audio_chan { 72 int idx; 73 struct snd_pcm_substream *substream; 74 int pos; 75 snd_pcm_uframes_t size; 76 spinlock_t lock; 77 }; 78 79 /* definition of the chip-specific record */ 80 struct snd_sgio2audio { 81 struct snd_card *card; 82 83 /* codec */ 84 struct snd_ad1843 ad1843; 85 spinlock_t ad1843_lock; 86 87 /* channels */ 88 struct snd_sgio2audio_chan channel[3]; 89 90 /* resources */ 91 void *ring_base; 92 dma_addr_t ring_base_dma; 93 }; 94 95 /* AD1843 access */ 96 97 /* 98 * read_ad1843_reg returns the current contents of a 16 bit AD1843 register. 99 * 100 * Returns unsigned register value on success, -errno on failure. 101 */ 102 static int read_ad1843_reg(void *priv, int reg) 103 { 104 struct snd_sgio2audio *chip = priv; 105 int val; 106 107 guard(spinlock_irqsave)(&chip->ad1843_lock); 108 109 writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) | 110 CODEC_CONTROL_READ, &mace->perif.audio.codec_control); 111 wmb(); 112 val = readq(&mace->perif.audio.codec_control); /* flush bus */ 113 udelay(200); 114 115 val = readq(&mace->perif.audio.codec_read); 116 117 return val; 118 } 119 120 /* 121 * write_ad1843_reg writes the specified value to a 16 bit AD1843 register. 122 */ 123 static int write_ad1843_reg(void *priv, int reg, int word) 124 { 125 struct snd_sgio2audio *chip = priv; 126 int val; 127 128 guard(spinlock_irqsave)(&chip->ad1843_lock); 129 130 writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) | 131 (word << CODEC_CONTROL_WORD_SHIFT), 132 &mace->perif.audio.codec_control); 133 wmb(); 134 val = readq(&mace->perif.audio.codec_control); /* flush bus */ 135 udelay(200); 136 137 return 0; 138 } 139 140 static int sgio2audio_gain_info(struct snd_kcontrol *kcontrol, 141 struct snd_ctl_elem_info *uinfo) 142 { 143 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol); 144 145 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 146 uinfo->count = 2; 147 uinfo->value.integer.min = 0; 148 uinfo->value.integer.max = ad1843_get_gain_max(&chip->ad1843, 149 (int)kcontrol->private_value); 150 return 0; 151 } 152 153 static int sgio2audio_gain_get(struct snd_kcontrol *kcontrol, 154 struct snd_ctl_elem_value *ucontrol) 155 { 156 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol); 157 int vol; 158 159 vol = ad1843_get_gain(&chip->ad1843, (int)kcontrol->private_value); 160 161 ucontrol->value.integer.value[0] = (vol >> 8) & 0xFF; 162 ucontrol->value.integer.value[1] = vol & 0xFF; 163 164 return 0; 165 } 166 167 static int sgio2audio_gain_put(struct snd_kcontrol *kcontrol, 168 struct snd_ctl_elem_value *ucontrol) 169 { 170 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol); 171 int newvol, oldvol; 172 173 oldvol = ad1843_get_gain(&chip->ad1843, kcontrol->private_value); 174 newvol = (ucontrol->value.integer.value[0] << 8) | 175 ucontrol->value.integer.value[1]; 176 177 newvol = ad1843_set_gain(&chip->ad1843, kcontrol->private_value, 178 newvol); 179 180 return newvol != oldvol; 181 } 182 183 static int sgio2audio_source_info(struct snd_kcontrol *kcontrol, 184 struct snd_ctl_elem_info *uinfo) 185 { 186 static const char * const texts[3] = { 187 "Cam Mic", "Mic", "Line" 188 }; 189 return snd_ctl_enum_info(uinfo, 1, 3, texts); 190 } 191 192 static int sgio2audio_source_get(struct snd_kcontrol *kcontrol, 193 struct snd_ctl_elem_value *ucontrol) 194 { 195 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol); 196 197 ucontrol->value.enumerated.item[0] = ad1843_get_recsrc(&chip->ad1843); 198 return 0; 199 } 200 201 static int sgio2audio_source_put(struct snd_kcontrol *kcontrol, 202 struct snd_ctl_elem_value *ucontrol) 203 { 204 struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol); 205 int newsrc, oldsrc; 206 207 oldsrc = ad1843_get_recsrc(&chip->ad1843); 208 newsrc = ad1843_set_recsrc(&chip->ad1843, 209 ucontrol->value.enumerated.item[0]); 210 211 return newsrc != oldsrc; 212 } 213 214 /* dac1/pcm0 mixer control */ 215 static const struct snd_kcontrol_new sgio2audio_ctrl_pcm0 = { 216 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 217 .name = "PCM Playback Volume", 218 .index = 0, 219 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 220 .private_value = AD1843_GAIN_PCM_0, 221 .info = sgio2audio_gain_info, 222 .get = sgio2audio_gain_get, 223 .put = sgio2audio_gain_put, 224 }; 225 226 /* dac2/pcm1 mixer control */ 227 static const struct snd_kcontrol_new sgio2audio_ctrl_pcm1 = { 228 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 229 .name = "PCM Playback Volume", 230 .index = 1, 231 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 232 .private_value = AD1843_GAIN_PCM_1, 233 .info = sgio2audio_gain_info, 234 .get = sgio2audio_gain_get, 235 .put = sgio2audio_gain_put, 236 }; 237 238 /* record level mixer control */ 239 static const struct snd_kcontrol_new sgio2audio_ctrl_reclevel = { 240 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 241 .name = "Capture Volume", 242 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 243 .private_value = AD1843_GAIN_RECLEV, 244 .info = sgio2audio_gain_info, 245 .get = sgio2audio_gain_get, 246 .put = sgio2audio_gain_put, 247 }; 248 249 /* record level source control */ 250 static const struct snd_kcontrol_new sgio2audio_ctrl_recsource = { 251 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 252 .name = "Capture Source", 253 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 254 .info = sgio2audio_source_info, 255 .get = sgio2audio_source_get, 256 .put = sgio2audio_source_put, 257 }; 258 259 /* line mixer control */ 260 static const struct snd_kcontrol_new sgio2audio_ctrl_line = { 261 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 262 .name = "Line Playback Volume", 263 .index = 0, 264 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 265 .private_value = AD1843_GAIN_LINE, 266 .info = sgio2audio_gain_info, 267 .get = sgio2audio_gain_get, 268 .put = sgio2audio_gain_put, 269 }; 270 271 /* cd mixer control */ 272 static const struct snd_kcontrol_new sgio2audio_ctrl_cd = { 273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 274 .name = "Line Playback Volume", 275 .index = 1, 276 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 277 .private_value = AD1843_GAIN_LINE_2, 278 .info = sgio2audio_gain_info, 279 .get = sgio2audio_gain_get, 280 .put = sgio2audio_gain_put, 281 }; 282 283 /* mic mixer control */ 284 static const struct snd_kcontrol_new sgio2audio_ctrl_mic = { 285 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 286 .name = "Mic Playback Volume", 287 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 288 .private_value = AD1843_GAIN_MIC, 289 .info = sgio2audio_gain_info, 290 .get = sgio2audio_gain_get, 291 .put = sgio2audio_gain_put, 292 }; 293 294 295 static int snd_sgio2audio_new_mixer(struct snd_sgio2audio *chip) 296 { 297 int err; 298 299 err = snd_ctl_add(chip->card, 300 snd_ctl_new1(&sgio2audio_ctrl_pcm0, chip)); 301 if (err < 0) 302 return err; 303 304 err = snd_ctl_add(chip->card, 305 snd_ctl_new1(&sgio2audio_ctrl_pcm1, chip)); 306 if (err < 0) 307 return err; 308 309 err = snd_ctl_add(chip->card, 310 snd_ctl_new1(&sgio2audio_ctrl_reclevel, chip)); 311 if (err < 0) 312 return err; 313 314 err = snd_ctl_add(chip->card, 315 snd_ctl_new1(&sgio2audio_ctrl_recsource, chip)); 316 if (err < 0) 317 return err; 318 err = snd_ctl_add(chip->card, 319 snd_ctl_new1(&sgio2audio_ctrl_line, chip)); 320 if (err < 0) 321 return err; 322 323 err = snd_ctl_add(chip->card, 324 snd_ctl_new1(&sgio2audio_ctrl_cd, chip)); 325 if (err < 0) 326 return err; 327 328 err = snd_ctl_add(chip->card, 329 snd_ctl_new1(&sgio2audio_ctrl_mic, chip)); 330 if (err < 0) 331 return err; 332 333 return 0; 334 } 335 336 /* low-level audio interface DMA */ 337 338 /* get data out of bounce buffer, count must be a multiple of 32 */ 339 /* returns 1 if a period has elapsed */ 340 static int snd_sgio2audio_dma_pull_frag(struct snd_sgio2audio *chip, 341 unsigned int ch, unsigned int count) 342 { 343 int ret; 344 unsigned long src_base, src_pos, dst_mask; 345 unsigned char *dst_base; 346 int dst_pos; 347 u64 *src; 348 s16 *dst; 349 u64 x; 350 struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime; 351 352 guard(spinlock_irqsave)(&chip->channel[ch].lock); 353 354 src_base = (unsigned long) chip->ring_base | (ch << CHANNEL_RING_SHIFT); 355 src_pos = readq(&mace->perif.audio.chan[ch].read_ptr); 356 dst_base = runtime->dma_area; 357 dst_pos = chip->channel[ch].pos; 358 dst_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1; 359 360 /* check if a period has elapsed */ 361 chip->channel[ch].size += (count >> 3); /* in frames */ 362 ret = chip->channel[ch].size >= runtime->period_size; 363 chip->channel[ch].size %= runtime->period_size; 364 365 while (count) { 366 src = (u64 *)(src_base + src_pos); 367 dst = (s16 *)(dst_base + dst_pos); 368 369 x = *src; 370 dst[0] = (x >> CHANNEL_LEFT_SHIFT) & 0xffff; 371 dst[1] = (x >> CHANNEL_RIGHT_SHIFT) & 0xffff; 372 373 src_pos = (src_pos + sizeof(u64)) & CHANNEL_RING_MASK; 374 dst_pos = (dst_pos + 2 * sizeof(s16)) & dst_mask; 375 count -= sizeof(u64); 376 } 377 378 writeq(src_pos, &mace->perif.audio.chan[ch].read_ptr); /* in bytes */ 379 chip->channel[ch].pos = dst_pos; 380 381 return ret; 382 } 383 384 /* put some DMA data in bounce buffer, count must be a multiple of 32 */ 385 /* returns 1 if a period has elapsed */ 386 static int snd_sgio2audio_dma_push_frag(struct snd_sgio2audio *chip, 387 unsigned int ch, unsigned int count) 388 { 389 int ret; 390 s64 l, r; 391 unsigned long dst_base, dst_pos, src_mask; 392 unsigned char *src_base; 393 int src_pos; 394 u64 *dst; 395 s16 *src; 396 struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime; 397 398 guard(spinlock_irqsave)(&chip->channel[ch].lock); 399 400 dst_base = (unsigned long)chip->ring_base | (ch << CHANNEL_RING_SHIFT); 401 dst_pos = readq(&mace->perif.audio.chan[ch].write_ptr); 402 src_base = runtime->dma_area; 403 src_pos = chip->channel[ch].pos; 404 src_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1; 405 406 /* check if a period has elapsed */ 407 chip->channel[ch].size += (count >> 3); /* in frames */ 408 ret = chip->channel[ch].size >= runtime->period_size; 409 chip->channel[ch].size %= runtime->period_size; 410 411 while (count) { 412 src = (s16 *)(src_base + src_pos); 413 dst = (u64 *)(dst_base + dst_pos); 414 415 l = src[0]; /* sign extend */ 416 r = src[1]; /* sign extend */ 417 418 *dst = ((l & 0x00ffffff) << CHANNEL_LEFT_SHIFT) | 419 ((r & 0x00ffffff) << CHANNEL_RIGHT_SHIFT); 420 421 dst_pos = (dst_pos + sizeof(u64)) & CHANNEL_RING_MASK; 422 src_pos = (src_pos + 2 * sizeof(s16)) & src_mask; 423 count -= sizeof(u64); 424 } 425 426 writeq(dst_pos, &mace->perif.audio.chan[ch].write_ptr); /* in bytes */ 427 chip->channel[ch].pos = src_pos; 428 429 return ret; 430 } 431 432 static int snd_sgio2audio_dma_start(struct snd_pcm_substream *substream) 433 { 434 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream); 435 struct snd_sgio2audio_chan *chan = substream->runtime->private_data; 436 int ch = chan->idx; 437 438 /* reset DMA channel */ 439 writeq(CHANNEL_CONTROL_RESET, &mace->perif.audio.chan[ch].control); 440 udelay(10); 441 writeq(0, &mace->perif.audio.chan[ch].control); 442 443 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 444 /* push a full buffer */ 445 snd_sgio2audio_dma_push_frag(chip, ch, CHANNEL_RING_SIZE - 32); 446 } 447 /* set DMA to wake on 50% empty and enable interrupt */ 448 writeq(CHANNEL_DMA_ENABLE | CHANNEL_INT_THRESHOLD_50, 449 &mace->perif.audio.chan[ch].control); 450 return 0; 451 } 452 453 static int snd_sgio2audio_dma_stop(struct snd_pcm_substream *substream) 454 { 455 struct snd_sgio2audio_chan *chan = substream->runtime->private_data; 456 457 writeq(0, &mace->perif.audio.chan[chan->idx].control); 458 return 0; 459 } 460 461 static irqreturn_t snd_sgio2audio_dma_in_isr(int irq, void *dev_id) 462 { 463 struct snd_sgio2audio_chan *chan = dev_id; 464 struct snd_pcm_substream *substream; 465 struct snd_sgio2audio *chip; 466 int count, ch; 467 468 substream = chan->substream; 469 chip = snd_pcm_substream_chip(substream); 470 ch = chan->idx; 471 472 /* empty the ring */ 473 count = CHANNEL_RING_SIZE - 474 readq(&mace->perif.audio.chan[ch].depth) - 32; 475 if (snd_sgio2audio_dma_pull_frag(chip, ch, count)) 476 snd_pcm_period_elapsed(substream); 477 478 return IRQ_HANDLED; 479 } 480 481 static irqreturn_t snd_sgio2audio_dma_out_isr(int irq, void *dev_id) 482 { 483 struct snd_sgio2audio_chan *chan = dev_id; 484 struct snd_pcm_substream *substream; 485 struct snd_sgio2audio *chip; 486 int count, ch; 487 488 substream = chan->substream; 489 chip = snd_pcm_substream_chip(substream); 490 ch = chan->idx; 491 /* fill the ring */ 492 count = CHANNEL_RING_SIZE - 493 readq(&mace->perif.audio.chan[ch].depth) - 32; 494 if (snd_sgio2audio_dma_push_frag(chip, ch, count)) 495 snd_pcm_period_elapsed(substream); 496 497 return IRQ_HANDLED; 498 } 499 500 static irqreturn_t snd_sgio2audio_error_isr(int irq, void *dev_id) 501 { 502 struct snd_sgio2audio_chan *chan = dev_id; 503 struct snd_pcm_substream *substream; 504 505 substream = chan->substream; 506 snd_sgio2audio_dma_stop(substream); 507 snd_sgio2audio_dma_start(substream); 508 return IRQ_HANDLED; 509 } 510 511 /* PCM part */ 512 /* PCM hardware definition */ 513 static const struct snd_pcm_hardware snd_sgio2audio_pcm_hw = { 514 .info = (SNDRV_PCM_INFO_MMAP | 515 SNDRV_PCM_INFO_MMAP_VALID | 516 SNDRV_PCM_INFO_INTERLEAVED | 517 SNDRV_PCM_INFO_BLOCK_TRANSFER), 518 .formats = SNDRV_PCM_FMTBIT_S16_BE, 519 .rates = SNDRV_PCM_RATE_8000_48000, 520 .rate_min = 8000, 521 .rate_max = 48000, 522 .channels_min = 2, 523 .channels_max = 2, 524 .buffer_bytes_max = 65536, 525 .period_bytes_min = 32768, 526 .period_bytes_max = 65536, 527 .periods_min = 1, 528 .periods_max = 1024, 529 }; 530 531 /* PCM playback open callback */ 532 static int snd_sgio2audio_playback1_open(struct snd_pcm_substream *substream) 533 { 534 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream); 535 struct snd_pcm_runtime *runtime = substream->runtime; 536 537 runtime->hw = snd_sgio2audio_pcm_hw; 538 runtime->private_data = &chip->channel[1]; 539 return 0; 540 } 541 542 static int snd_sgio2audio_playback2_open(struct snd_pcm_substream *substream) 543 { 544 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream); 545 struct snd_pcm_runtime *runtime = substream->runtime; 546 547 runtime->hw = snd_sgio2audio_pcm_hw; 548 runtime->private_data = &chip->channel[2]; 549 return 0; 550 } 551 552 /* PCM capture open callback */ 553 static int snd_sgio2audio_capture_open(struct snd_pcm_substream *substream) 554 { 555 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream); 556 struct snd_pcm_runtime *runtime = substream->runtime; 557 558 runtime->hw = snd_sgio2audio_pcm_hw; 559 runtime->private_data = &chip->channel[0]; 560 return 0; 561 } 562 563 /* PCM close callback */ 564 static int snd_sgio2audio_pcm_close(struct snd_pcm_substream *substream) 565 { 566 struct snd_pcm_runtime *runtime = substream->runtime; 567 568 runtime->private_data = NULL; 569 return 0; 570 } 571 572 /* prepare callback */ 573 static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream *substream) 574 { 575 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream); 576 struct snd_pcm_runtime *runtime = substream->runtime; 577 struct snd_sgio2audio_chan *chan = substream->runtime->private_data; 578 int ch = chan->idx; 579 580 guard(spinlock_irqsave)(&chip->channel[ch].lock); 581 582 /* Setup the pseudo-dma transfer pointers. */ 583 chip->channel[ch].pos = 0; 584 chip->channel[ch].size = 0; 585 chip->channel[ch].substream = substream; 586 587 /* set AD1843 format */ 588 /* hardware format is always S16_LE */ 589 switch (substream->stream) { 590 case SNDRV_PCM_STREAM_PLAYBACK: 591 ad1843_setup_dac(&chip->ad1843, 592 ch - 1, 593 runtime->rate, 594 SNDRV_PCM_FORMAT_S16_LE, 595 runtime->channels); 596 break; 597 case SNDRV_PCM_STREAM_CAPTURE: 598 ad1843_setup_adc(&chip->ad1843, 599 runtime->rate, 600 SNDRV_PCM_FORMAT_S16_LE, 601 runtime->channels); 602 break; 603 } 604 return 0; 605 } 606 607 /* trigger callback */ 608 static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream *substream, 609 int cmd) 610 { 611 switch (cmd) { 612 case SNDRV_PCM_TRIGGER_START: 613 /* start the PCM engine */ 614 snd_sgio2audio_dma_start(substream); 615 break; 616 case SNDRV_PCM_TRIGGER_STOP: 617 /* stop the PCM engine */ 618 snd_sgio2audio_dma_stop(substream); 619 break; 620 default: 621 return -EINVAL; 622 } 623 return 0; 624 } 625 626 /* pointer callback */ 627 static snd_pcm_uframes_t 628 snd_sgio2audio_pcm_pointer(struct snd_pcm_substream *substream) 629 { 630 struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream); 631 struct snd_sgio2audio_chan *chan = substream->runtime->private_data; 632 633 /* get the current hardware pointer */ 634 return bytes_to_frames(substream->runtime, 635 chip->channel[chan->idx].pos); 636 } 637 638 /* operators */ 639 static const struct snd_pcm_ops snd_sgio2audio_playback1_ops = { 640 .open = snd_sgio2audio_playback1_open, 641 .close = snd_sgio2audio_pcm_close, 642 .prepare = snd_sgio2audio_pcm_prepare, 643 .trigger = snd_sgio2audio_pcm_trigger, 644 .pointer = snd_sgio2audio_pcm_pointer, 645 }; 646 647 static const struct snd_pcm_ops snd_sgio2audio_playback2_ops = { 648 .open = snd_sgio2audio_playback2_open, 649 .close = snd_sgio2audio_pcm_close, 650 .prepare = snd_sgio2audio_pcm_prepare, 651 .trigger = snd_sgio2audio_pcm_trigger, 652 .pointer = snd_sgio2audio_pcm_pointer, 653 }; 654 655 static const struct snd_pcm_ops snd_sgio2audio_capture_ops = { 656 .open = snd_sgio2audio_capture_open, 657 .close = snd_sgio2audio_pcm_close, 658 .prepare = snd_sgio2audio_pcm_prepare, 659 .trigger = snd_sgio2audio_pcm_trigger, 660 .pointer = snd_sgio2audio_pcm_pointer, 661 }; 662 663 /* 664 * definitions of capture are omitted here... 665 */ 666 667 /* create a pcm device */ 668 static int snd_sgio2audio_new_pcm(struct snd_sgio2audio *chip) 669 { 670 struct snd_pcm *pcm; 671 int err; 672 673 /* create first pcm device with one outputs and one input */ 674 err = snd_pcm_new(chip->card, "SGI O2 Audio", 0, 1, 1, &pcm); 675 if (err < 0) 676 return err; 677 678 pcm->private_data = chip; 679 strscpy(pcm->name, "SGI O2 DAC1"); 680 681 /* set operators */ 682 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 683 &snd_sgio2audio_playback1_ops); 684 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, 685 &snd_sgio2audio_capture_ops); 686 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); 687 688 /* create second pcm device with one outputs and no input */ 689 err = snd_pcm_new(chip->card, "SGI O2 Audio", 1, 1, 0, &pcm); 690 if (err < 0) 691 return err; 692 693 pcm->private_data = chip; 694 strscpy(pcm->name, "SGI O2 DAC2"); 695 696 /* set operators */ 697 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 698 &snd_sgio2audio_playback2_ops); 699 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0); 700 701 return 0; 702 } 703 704 static struct { 705 int idx; 706 int irq; 707 irqreturn_t (*isr)(int, void *); 708 const char *desc; 709 } snd_sgio2_isr_table[] = { 710 { 711 .idx = 0, 712 .irq = MACEISA_AUDIO1_DMAT_IRQ, 713 .isr = snd_sgio2audio_dma_in_isr, 714 .desc = "Capture DMA Channel 0" 715 }, { 716 .idx = 0, 717 .irq = MACEISA_AUDIO1_OF_IRQ, 718 .isr = snd_sgio2audio_error_isr, 719 .desc = "Capture Overflow" 720 }, { 721 .idx = 1, 722 .irq = MACEISA_AUDIO2_DMAT_IRQ, 723 .isr = snd_sgio2audio_dma_out_isr, 724 .desc = "Playback DMA Channel 1" 725 }, { 726 .idx = 1, 727 .irq = MACEISA_AUDIO2_MERR_IRQ, 728 .isr = snd_sgio2audio_error_isr, 729 .desc = "Memory Error Channel 1" 730 }, { 731 .idx = 2, 732 .irq = MACEISA_AUDIO3_DMAT_IRQ, 733 .isr = snd_sgio2audio_dma_out_isr, 734 .desc = "Playback DMA Channel 2" 735 }, { 736 .idx = 2, 737 .irq = MACEISA_AUDIO3_MERR_IRQ, 738 .isr = snd_sgio2audio_error_isr, 739 .desc = "Memory Error Channel 2" 740 } 741 }; 742 743 /* ALSA driver */ 744 745 static int snd_sgio2audio_free(struct snd_sgio2audio *chip) 746 { 747 int i; 748 749 /* reset interface */ 750 writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control); 751 udelay(1); 752 writeq(0, &mace->perif.audio.control); 753 754 /* release IRQ's */ 755 for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) 756 free_irq(snd_sgio2_isr_table[i].irq, 757 &chip->channel[snd_sgio2_isr_table[i].idx]); 758 759 dma_free_coherent(chip->card->dev, MACEISA_RINGBUFFERS_SIZE, 760 chip->ring_base, chip->ring_base_dma); 761 762 /* release card data */ 763 kfree(chip); 764 return 0; 765 } 766 767 static int snd_sgio2audio_dev_free(struct snd_device *device) 768 { 769 struct snd_sgio2audio *chip = device->device_data; 770 771 return snd_sgio2audio_free(chip); 772 } 773 774 static const struct snd_device_ops ops = { 775 .dev_free = snd_sgio2audio_dev_free, 776 }; 777 778 static int snd_sgio2audio_create(struct snd_card *card, 779 struct snd_sgio2audio **rchip) 780 { 781 struct snd_sgio2audio *chip; 782 int i, err; 783 784 *rchip = NULL; 785 786 /* check if a codec is attached to the interface */ 787 /* (Audio or Audio/Video board present) */ 788 if (!(readq(&mace->perif.audio.control) & AUDIO_CONTROL_CODEC_PRESENT)) 789 return -ENOENT; 790 791 chip = kzalloc(sizeof(*chip), GFP_KERNEL); 792 if (chip == NULL) 793 return -ENOMEM; 794 795 chip->card = card; 796 797 chip->ring_base = dma_alloc_coherent(card->dev, 798 MACEISA_RINGBUFFERS_SIZE, 799 &chip->ring_base_dma, GFP_KERNEL); 800 if (chip->ring_base == NULL) { 801 printk(KERN_ERR 802 "sgio2audio: could not allocate ring buffers\n"); 803 kfree(chip); 804 return -ENOMEM; 805 } 806 807 spin_lock_init(&chip->ad1843_lock); 808 809 /* initialize channels */ 810 for (i = 0; i < 3; i++) { 811 spin_lock_init(&chip->channel[i].lock); 812 chip->channel[i].idx = i; 813 } 814 815 /* allocate IRQs */ 816 for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) { 817 if (request_irq(snd_sgio2_isr_table[i].irq, 818 snd_sgio2_isr_table[i].isr, 819 0, 820 snd_sgio2_isr_table[i].desc, 821 &chip->channel[snd_sgio2_isr_table[i].idx])) { 822 snd_sgio2audio_free(chip); 823 printk(KERN_ERR "sgio2audio: cannot allocate irq %d\n", 824 snd_sgio2_isr_table[i].irq); 825 return -EBUSY; 826 } 827 } 828 829 /* reset the interface */ 830 writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control); 831 udelay(1); 832 writeq(0, &mace->perif.audio.control); 833 msleep_interruptible(1); /* give time to recover */ 834 835 /* set ring base */ 836 writeq(chip->ring_base_dma, &mace->perif.ctrl.ringbase); 837 838 /* attach the AD1843 codec */ 839 chip->ad1843.read = read_ad1843_reg; 840 chip->ad1843.write = write_ad1843_reg; 841 chip->ad1843.chip = chip; 842 843 /* initialize the AD1843 codec */ 844 err = ad1843_init(&chip->ad1843); 845 if (err < 0) { 846 snd_sgio2audio_free(chip); 847 return err; 848 } 849 850 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); 851 if (err < 0) { 852 snd_sgio2audio_free(chip); 853 return err; 854 } 855 *rchip = chip; 856 return 0; 857 } 858 859 static int snd_sgio2audio_probe(struct platform_device *pdev) 860 { 861 struct snd_card *card; 862 struct snd_sgio2audio *chip; 863 int err; 864 865 err = snd_card_new(&pdev->dev, index, id, THIS_MODULE, 0, &card); 866 if (err < 0) 867 return err; 868 869 err = snd_sgio2audio_create(card, &chip); 870 if (err < 0) { 871 snd_card_free(card); 872 return err; 873 } 874 875 err = snd_sgio2audio_new_pcm(chip); 876 if (err < 0) { 877 snd_card_free(card); 878 return err; 879 } 880 err = snd_sgio2audio_new_mixer(chip); 881 if (err < 0) { 882 snd_card_free(card); 883 return err; 884 } 885 886 strscpy(card->driver, "SGI O2 Audio"); 887 strscpy(card->shortname, "SGI O2 Audio"); 888 sprintf(card->longname, "%s irq %i-%i", 889 card->shortname, 890 MACEISA_AUDIO1_DMAT_IRQ, 891 MACEISA_AUDIO3_MERR_IRQ); 892 893 err = snd_card_register(card); 894 if (err < 0) { 895 snd_card_free(card); 896 return err; 897 } 898 platform_set_drvdata(pdev, card); 899 return 0; 900 } 901 902 static void snd_sgio2audio_remove(struct platform_device *pdev) 903 { 904 struct snd_card *card = platform_get_drvdata(pdev); 905 906 snd_card_free(card); 907 } 908 909 static struct platform_driver sgio2audio_driver = { 910 .probe = snd_sgio2audio_probe, 911 .remove = snd_sgio2audio_remove, 912 .driver = { 913 .name = "sgio2audio", 914 } 915 }; 916 917 module_platform_driver(sgio2audio_driver); 918