1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * i2sbus driver -- pcm routines 4 * 5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net> 6 */ 7 8 #include <linux/io.h> 9 #include <linux/delay.h> 10 #include <linux/slab.h> 11 #include <sound/core.h> 12 #include <asm/macio.h> 13 #include <linux/pci.h> 14 #include <linux/module.h> 15 #include "../soundbus.h" 16 #include "i2sbus.h" 17 18 static inline void get_pcm_info(struct i2sbus_dev *i2sdev, int in, 19 struct pcm_info **pi, struct pcm_info **other) 20 { 21 if (in) { 22 if (pi) 23 *pi = &i2sdev->in; 24 if (other) 25 *other = &i2sdev->out; 26 } else { 27 if (pi) 28 *pi = &i2sdev->out; 29 if (other) 30 *other = &i2sdev->in; 31 } 32 } 33 34 static int clock_and_divisors(int mclk, int sclk, int rate, int *out) 35 { 36 /* sclk must be derived from mclk! */ 37 if (mclk % sclk) 38 return -1; 39 /* derive sclk register value */ 40 if (i2s_sf_sclkdiv(mclk / sclk, out)) 41 return -1; 42 43 if (I2S_CLOCK_SPEED_18MHz % (rate * mclk) == 0) { 44 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_18MHz / (rate * mclk), out)) { 45 *out |= I2S_SF_CLOCK_SOURCE_18MHz; 46 return 0; 47 } 48 } 49 if (I2S_CLOCK_SPEED_45MHz % (rate * mclk) == 0) { 50 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_45MHz / (rate * mclk), out)) { 51 *out |= I2S_SF_CLOCK_SOURCE_45MHz; 52 return 0; 53 } 54 } 55 if (I2S_CLOCK_SPEED_49MHz % (rate * mclk) == 0) { 56 if (!i2s_sf_mclkdiv(I2S_CLOCK_SPEED_49MHz / (rate * mclk), out)) { 57 *out |= I2S_SF_CLOCK_SOURCE_49MHz; 58 return 0; 59 } 60 } 61 return -1; 62 } 63 64 #define CHECK_RATE(rate) \ 65 do { if (rates & SNDRV_PCM_RATE_ ##rate) { \ 66 int dummy; \ 67 if (clock_and_divisors(sysclock_factor, \ 68 bus_factor, rate, &dummy)) \ 69 rates &= ~SNDRV_PCM_RATE_ ##rate; \ 70 } } while (0) 71 72 static int i2sbus_pcm_open(struct i2sbus_dev *i2sdev, int in) 73 { 74 struct pcm_info *pi, *other; 75 struct soundbus_dev *sdev; 76 int masks_inited = 0, err; 77 struct codec_info_item *cii, *rev; 78 struct snd_pcm_hardware *hw; 79 u64 formats = 0; 80 unsigned int rates = 0; 81 struct transfer_info v; 82 int result = 0; 83 int bus_factor = 0, sysclock_factor = 0; 84 int found_this; 85 86 mutex_lock(&i2sdev->lock); 87 88 get_pcm_info(i2sdev, in, &pi, &other); 89 90 hw = &pi->substream->runtime->hw; 91 sdev = &i2sdev->sound; 92 93 if (pi->active) { 94 /* alsa messed up */ 95 result = -EBUSY; 96 goto out_unlock; 97 } 98 99 /* we now need to assign the hw */ 100 list_for_each_entry(cii, &sdev->codec_list, list) { 101 struct transfer_info *ti = cii->codec->transfers; 102 bus_factor = cii->codec->bus_factor; 103 sysclock_factor = cii->codec->sysclock_factor; 104 while (ti->formats && ti->rates) { 105 v = *ti; 106 if (ti->transfer_in == in 107 && cii->codec->usable(cii, ti, &v)) { 108 if (masks_inited) { 109 formats &= v.formats; 110 rates &= v.rates; 111 } else { 112 formats = v.formats; 113 rates = v.rates; 114 masks_inited = 1; 115 } 116 } 117 ti++; 118 } 119 } 120 if (!masks_inited || !bus_factor || !sysclock_factor) { 121 result = -ENODEV; 122 goto out_unlock; 123 } 124 /* bus dependent stuff */ 125 hw->info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | 126 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_RESUME | 127 SNDRV_PCM_INFO_JOINT_DUPLEX; 128 129 CHECK_RATE(5512); 130 CHECK_RATE(8000); 131 CHECK_RATE(11025); 132 CHECK_RATE(16000); 133 CHECK_RATE(22050); 134 CHECK_RATE(32000); 135 CHECK_RATE(44100); 136 CHECK_RATE(48000); 137 CHECK_RATE(64000); 138 CHECK_RATE(88200); 139 CHECK_RATE(96000); 140 CHECK_RATE(176400); 141 CHECK_RATE(192000); 142 hw->rates = rates; 143 144 /* well. the codec might want 24 bits only, and we'll 145 * ever only transfer 24 bits, but they are top-aligned! 146 * So for alsa, we claim that we're doing full 32 bit 147 * while in reality we'll ignore the lower 8 bits of 148 * that when doing playback (they're transferred as 0 149 * as far as I know, no codecs we have are 32-bit capable 150 * so I can't really test) and when doing recording we'll 151 * always have those lower 8 bits recorded as 0 */ 152 if (formats & SNDRV_PCM_FMTBIT_S24_BE) 153 formats |= SNDRV_PCM_FMTBIT_S32_BE; 154 if (formats & SNDRV_PCM_FMTBIT_U24_BE) 155 formats |= SNDRV_PCM_FMTBIT_U32_BE; 156 /* now mask off what we can support. I suppose we could 157 * also support S24_3LE and some similar formats, but I 158 * doubt there's a codec that would be able to use that, 159 * so we don't support it here. */ 160 hw->formats = formats & (SNDRV_PCM_FMTBIT_S16_BE | 161 SNDRV_PCM_FMTBIT_U16_BE | 162 SNDRV_PCM_FMTBIT_S32_BE | 163 SNDRV_PCM_FMTBIT_U32_BE); 164 165 /* we need to set the highest and lowest rate possible. 166 * These are the highest and lowest rates alsa can 167 * support properly in its bitfield. 168 * Below, we'll use that to restrict to the rate 169 * currently in use (if any). */ 170 hw->rate_min = 5512; 171 hw->rate_max = 192000; 172 /* if the other stream is active, then we can only 173 * support what it is currently using. 174 * FIXME: I lied. This comment is wrong. We can support 175 * anything that works with the same serial format, ie. 176 * when recording 24 bit sound we can well play 16 bit 177 * sound at the same time iff using the same transfer mode. 178 */ 179 if (other->active) { 180 /* FIXME: is this guaranteed by the alsa api? */ 181 hw->formats &= pcm_format_to_bits(i2sdev->format); 182 /* see above, restrict rates to the one we already have */ 183 hw->rate_min = i2sdev->rate; 184 hw->rate_max = i2sdev->rate; 185 } 186 187 hw->channels_min = 2; 188 hw->channels_max = 2; 189 /* these are somewhat arbitrary */ 190 hw->buffer_bytes_max = 131072; 191 hw->period_bytes_min = 256; 192 hw->period_bytes_max = 16384; 193 hw->periods_min = 3; 194 hw->periods_max = MAX_DBDMA_COMMANDS; 195 err = snd_pcm_hw_constraint_integer(pi->substream->runtime, 196 SNDRV_PCM_HW_PARAM_PERIODS); 197 if (err < 0) { 198 result = err; 199 goto out_unlock; 200 } 201 list_for_each_entry(cii, &sdev->codec_list, list) { 202 if (cii->codec->open) { 203 err = cii->codec->open(cii, pi->substream); 204 if (err) { 205 result = err; 206 /* unwind */ 207 found_this = 0; 208 list_for_each_entry_reverse(rev, 209 &sdev->codec_list, list) { 210 if (found_this && rev->codec->close) { 211 rev->codec->close(rev, 212 pi->substream); 213 } 214 if (rev == cii) 215 found_this = 1; 216 } 217 goto out_unlock; 218 } 219 } 220 } 221 222 out_unlock: 223 mutex_unlock(&i2sdev->lock); 224 return result; 225 } 226 227 #undef CHECK_RATE 228 229 static int i2sbus_pcm_close(struct i2sbus_dev *i2sdev, int in) 230 { 231 struct codec_info_item *cii; 232 struct pcm_info *pi; 233 int err = 0, tmp; 234 235 mutex_lock(&i2sdev->lock); 236 237 get_pcm_info(i2sdev, in, &pi, NULL); 238 239 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) { 240 if (cii->codec->close) { 241 tmp = cii->codec->close(cii, pi->substream); 242 if (tmp) 243 err = tmp; 244 } 245 } 246 247 pi->substream = NULL; 248 pi->active = 0; 249 mutex_unlock(&i2sdev->lock); 250 return err; 251 } 252 253 static void i2sbus_wait_for_stop(struct i2sbus_dev *i2sdev, 254 struct pcm_info *pi) 255 { 256 unsigned long flags; 257 struct completion done; 258 long timeout; 259 260 spin_lock_irqsave(&i2sdev->low_lock, flags); 261 if (pi->dbdma_ring.stopping) { 262 init_completion(&done); 263 pi->stop_completion = &done; 264 spin_unlock_irqrestore(&i2sdev->low_lock, flags); 265 timeout = wait_for_completion_timeout(&done, HZ); 266 spin_lock_irqsave(&i2sdev->low_lock, flags); 267 pi->stop_completion = NULL; 268 if (timeout == 0) { 269 /* timeout expired, stop dbdma forcefully */ 270 printk(KERN_ERR "i2sbus_wait_for_stop: timed out\n"); 271 /* make sure RUN, PAUSE and S0 bits are cleared */ 272 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16); 273 pi->dbdma_ring.stopping = 0; 274 timeout = 10; 275 while (in_le32(&pi->dbdma->status) & ACTIVE) { 276 if (--timeout <= 0) 277 break; 278 udelay(1); 279 } 280 } 281 } 282 spin_unlock_irqrestore(&i2sdev->low_lock, flags); 283 } 284 285 #ifdef CONFIG_PM 286 void i2sbus_wait_for_stop_both(struct i2sbus_dev *i2sdev) 287 { 288 struct pcm_info *pi; 289 290 get_pcm_info(i2sdev, 0, &pi, NULL); 291 i2sbus_wait_for_stop(i2sdev, pi); 292 get_pcm_info(i2sdev, 1, &pi, NULL); 293 i2sbus_wait_for_stop(i2sdev, pi); 294 } 295 #endif 296 297 static inline int i2sbus_hw_free(struct snd_pcm_substream *substream, int in) 298 { 299 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 300 struct pcm_info *pi; 301 302 get_pcm_info(i2sdev, in, &pi, NULL); 303 if (pi->dbdma_ring.stopping) 304 i2sbus_wait_for_stop(i2sdev, pi); 305 return 0; 306 } 307 308 static int i2sbus_playback_hw_free(struct snd_pcm_substream *substream) 309 { 310 return i2sbus_hw_free(substream, 0); 311 } 312 313 static int i2sbus_record_hw_free(struct snd_pcm_substream *substream) 314 { 315 return i2sbus_hw_free(substream, 1); 316 } 317 318 static int i2sbus_pcm_prepare(struct i2sbus_dev *i2sdev, int in) 319 { 320 /* whee. Hard work now. The user has selected a bitrate 321 * and bit format, so now we have to program our 322 * I2S controller appropriately. */ 323 struct snd_pcm_runtime *runtime; 324 struct dbdma_cmd *command; 325 int i, periodsize, nperiods; 326 dma_addr_t offset; 327 struct bus_info bi; 328 struct codec_info_item *cii; 329 int sfr = 0; /* serial format register */ 330 int dws = 0; /* data word sizes reg */ 331 int input_16bit; 332 struct pcm_info *pi, *other; 333 int cnt; 334 int result = 0; 335 unsigned int cmd, stopaddr; 336 337 mutex_lock(&i2sdev->lock); 338 339 get_pcm_info(i2sdev, in, &pi, &other); 340 341 if (pi->dbdma_ring.running) { 342 result = -EBUSY; 343 goto out_unlock; 344 } 345 if (pi->dbdma_ring.stopping) 346 i2sbus_wait_for_stop(i2sdev, pi); 347 348 if (!pi->substream || !pi->substream->runtime) { 349 result = -EINVAL; 350 goto out_unlock; 351 } 352 353 runtime = pi->substream->runtime; 354 pi->active = 1; 355 if (other->active && 356 ((i2sdev->format != runtime->format) 357 || (i2sdev->rate != runtime->rate))) { 358 result = -EINVAL; 359 goto out_unlock; 360 } 361 362 i2sdev->format = runtime->format; 363 i2sdev->rate = runtime->rate; 364 365 periodsize = snd_pcm_lib_period_bytes(pi->substream); 366 nperiods = pi->substream->runtime->periods; 367 pi->current_period = 0; 368 369 /* generate dbdma command ring first */ 370 command = pi->dbdma_ring.cmds; 371 memset(command, 0, (nperiods + 2) * sizeof(struct dbdma_cmd)); 372 373 /* commands to DMA to/from the ring */ 374 /* 375 * For input, we need to do a graceful stop; if we abort 376 * the DMA, we end up with leftover bytes that corrupt 377 * the next recording. To do this we set the S0 status 378 * bit and wait for the DMA controller to stop. Each 379 * command has a branch condition to 380 * make it branch to a stop command if S0 is set. 381 * On input we also need to wait for the S7 bit to be 382 * set before turning off the DMA controller. 383 * In fact we do the graceful stop for output as well. 384 */ 385 offset = runtime->dma_addr; 386 cmd = (in? INPUT_MORE: OUTPUT_MORE) | BR_IFSET | INTR_ALWAYS; 387 stopaddr = pi->dbdma_ring.bus_cmd_start + 388 (nperiods + 1) * sizeof(struct dbdma_cmd); 389 for (i = 0; i < nperiods; i++, command++, offset += periodsize) { 390 command->command = cpu_to_le16(cmd); 391 command->cmd_dep = cpu_to_le32(stopaddr); 392 command->phy_addr = cpu_to_le32(offset); 393 command->req_count = cpu_to_le16(periodsize); 394 } 395 396 /* branch back to beginning of ring */ 397 command->command = cpu_to_le16(DBDMA_NOP | BR_ALWAYS); 398 command->cmd_dep = cpu_to_le32(pi->dbdma_ring.bus_cmd_start); 399 command++; 400 401 /* set stop command */ 402 command->command = cpu_to_le16(DBDMA_STOP); 403 404 /* ok, let's set the serial format and stuff */ 405 switch (runtime->format) { 406 /* 16 bit formats */ 407 case SNDRV_PCM_FORMAT_S16_BE: 408 case SNDRV_PCM_FORMAT_U16_BE: 409 /* FIXME: if we add different bus factors we need to 410 * do more here!! */ 411 bi.bus_factor = 0; 412 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) { 413 bi.bus_factor = cii->codec->bus_factor; 414 break; 415 } 416 if (!bi.bus_factor) { 417 result = -ENODEV; 418 goto out_unlock; 419 } 420 input_16bit = 1; 421 break; 422 case SNDRV_PCM_FORMAT_S32_BE: 423 case SNDRV_PCM_FORMAT_U32_BE: 424 /* force 64x bus speed, otherwise the data cannot be 425 * transferred quickly enough! */ 426 bi.bus_factor = 64; 427 input_16bit = 0; 428 break; 429 default: 430 result = -EINVAL; 431 goto out_unlock; 432 } 433 /* we assume all sysclocks are the same! */ 434 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) { 435 bi.sysclock_factor = cii->codec->sysclock_factor; 436 break; 437 } 438 439 if (clock_and_divisors(bi.sysclock_factor, 440 bi.bus_factor, 441 runtime->rate, 442 &sfr) < 0) { 443 result = -EINVAL; 444 goto out_unlock; 445 } 446 switch (bi.bus_factor) { 447 case 32: 448 sfr |= I2S_SF_SERIAL_FORMAT_I2S_32X; 449 break; 450 case 64: 451 sfr |= I2S_SF_SERIAL_FORMAT_I2S_64X; 452 break; 453 } 454 /* FIXME: THIS ASSUMES MASTER ALL THE TIME */ 455 sfr |= I2S_SF_SCLK_MASTER; 456 457 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) { 458 int err = 0; 459 if (cii->codec->prepare) 460 err = cii->codec->prepare(cii, &bi, pi->substream); 461 if (err) { 462 result = err; 463 goto out_unlock; 464 } 465 } 466 /* codecs are fine with it, so set our clocks */ 467 if (input_16bit) 468 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) | 469 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) | 470 I2S_DWS_DATA_IN_16BIT | I2S_DWS_DATA_OUT_16BIT; 471 else 472 dws = (2 << I2S_DWS_NUM_CHANNELS_IN_SHIFT) | 473 (2 << I2S_DWS_NUM_CHANNELS_OUT_SHIFT) | 474 I2S_DWS_DATA_IN_24BIT | I2S_DWS_DATA_OUT_24BIT; 475 476 /* early exit if already programmed correctly */ 477 /* not locking these is fine since we touch them only in this function */ 478 if (in_le32(&i2sdev->intfregs->serial_format) == sfr 479 && in_le32(&i2sdev->intfregs->data_word_sizes) == dws) 480 goto out_unlock; 481 482 /* let's notify the codecs about clocks going away. 483 * For now we only do mastering on the i2s cell... */ 484 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) 485 if (cii->codec->switch_clock) 486 cii->codec->switch_clock(cii, CLOCK_SWITCH_PREPARE_SLAVE); 487 488 i2sbus_control_enable(i2sdev->control, i2sdev); 489 i2sbus_control_cell(i2sdev->control, i2sdev, 1); 490 491 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED); 492 493 i2sbus_control_clock(i2sdev->control, i2sdev, 0); 494 495 msleep(1); 496 497 /* wait for clock stopped. This can apparently take a while... */ 498 cnt = 100; 499 while (cnt-- && 500 !(in_le32(&i2sdev->intfregs->intr_ctl) & I2S_PENDING_CLOCKS_STOPPED)) { 501 msleep(5); 502 } 503 out_le32(&i2sdev->intfregs->intr_ctl, I2S_PENDING_CLOCKS_STOPPED); 504 505 /* not locking these is fine since we touch them only in this function */ 506 out_le32(&i2sdev->intfregs->serial_format, sfr); 507 out_le32(&i2sdev->intfregs->data_word_sizes, dws); 508 509 i2sbus_control_enable(i2sdev->control, i2sdev); 510 i2sbus_control_cell(i2sdev->control, i2sdev, 1); 511 i2sbus_control_clock(i2sdev->control, i2sdev, 1); 512 msleep(1); 513 514 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) 515 if (cii->codec->switch_clock) 516 cii->codec->switch_clock(cii, CLOCK_SWITCH_SLAVE); 517 518 out_unlock: 519 mutex_unlock(&i2sdev->lock); 520 return result; 521 } 522 523 #ifdef CONFIG_PM 524 void i2sbus_pcm_prepare_both(struct i2sbus_dev *i2sdev) 525 { 526 i2sbus_pcm_prepare(i2sdev, 0); 527 i2sbus_pcm_prepare(i2sdev, 1); 528 } 529 #endif 530 531 static int i2sbus_pcm_trigger(struct i2sbus_dev *i2sdev, int in, int cmd) 532 { 533 struct codec_info_item *cii; 534 struct pcm_info *pi; 535 int result = 0; 536 unsigned long flags; 537 538 spin_lock_irqsave(&i2sdev->low_lock, flags); 539 540 get_pcm_info(i2sdev, in, &pi, NULL); 541 542 switch (cmd) { 543 case SNDRV_PCM_TRIGGER_START: 544 case SNDRV_PCM_TRIGGER_RESUME: 545 if (pi->dbdma_ring.running) { 546 result = -EALREADY; 547 goto out_unlock; 548 } 549 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) 550 if (cii->codec->start) 551 cii->codec->start(cii, pi->substream); 552 pi->dbdma_ring.running = 1; 553 554 if (pi->dbdma_ring.stopping) { 555 /* Clear the S0 bit, then see if we stopped yet */ 556 out_le32(&pi->dbdma->control, 1 << 16); 557 if (in_le32(&pi->dbdma->status) & ACTIVE) { 558 /* possible race here? */ 559 udelay(10); 560 if (in_le32(&pi->dbdma->status) & ACTIVE) { 561 pi->dbdma_ring.stopping = 0; 562 goto out_unlock; /* keep running */ 563 } 564 } 565 } 566 567 /* make sure RUN, PAUSE and S0 bits are cleared */ 568 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16); 569 570 /* set branch condition select register */ 571 out_le32(&pi->dbdma->br_sel, (1 << 16) | 1); 572 573 /* write dma command buffer address to the dbdma chip */ 574 out_le32(&pi->dbdma->cmdptr, pi->dbdma_ring.bus_cmd_start); 575 576 /* initialize the frame count and current period */ 577 pi->current_period = 0; 578 pi->frame_count = in_le32(&i2sdev->intfregs->frame_count); 579 580 /* set the DMA controller running */ 581 out_le32(&pi->dbdma->control, (RUN << 16) | RUN); 582 583 /* off you go! */ 584 break; 585 586 case SNDRV_PCM_TRIGGER_STOP: 587 case SNDRV_PCM_TRIGGER_SUSPEND: 588 if (!pi->dbdma_ring.running) { 589 result = -EALREADY; 590 goto out_unlock; 591 } 592 pi->dbdma_ring.running = 0; 593 594 /* Set the S0 bit to make the DMA branch to the stop cmd */ 595 out_le32(&pi->dbdma->control, (1 << 16) | 1); 596 pi->dbdma_ring.stopping = 1; 597 598 list_for_each_entry(cii, &i2sdev->sound.codec_list, list) 599 if (cii->codec->stop) 600 cii->codec->stop(cii, pi->substream); 601 break; 602 default: 603 result = -EINVAL; 604 goto out_unlock; 605 } 606 607 out_unlock: 608 spin_unlock_irqrestore(&i2sdev->low_lock, flags); 609 return result; 610 } 611 612 static snd_pcm_uframes_t i2sbus_pcm_pointer(struct i2sbus_dev *i2sdev, int in) 613 { 614 struct pcm_info *pi; 615 u32 fc; 616 617 get_pcm_info(i2sdev, in, &pi, NULL); 618 619 fc = in_le32(&i2sdev->intfregs->frame_count); 620 fc = fc - pi->frame_count; 621 622 if (fc >= pi->substream->runtime->buffer_size) 623 fc %= pi->substream->runtime->buffer_size; 624 return fc; 625 } 626 627 static inline void handle_interrupt(struct i2sbus_dev *i2sdev, int in) 628 { 629 struct pcm_info *pi; 630 u32 fc, nframes; 631 u32 status; 632 int timeout, i; 633 int dma_stopped = 0; 634 struct snd_pcm_runtime *runtime; 635 636 spin_lock(&i2sdev->low_lock); 637 get_pcm_info(i2sdev, in, &pi, NULL); 638 if (!pi->dbdma_ring.running && !pi->dbdma_ring.stopping) 639 goto out_unlock; 640 641 i = pi->current_period; 642 runtime = pi->substream->runtime; 643 while (pi->dbdma_ring.cmds[i].xfer_status) { 644 if (le16_to_cpu(pi->dbdma_ring.cmds[i].xfer_status) & BT) 645 /* 646 * BT is the branch taken bit. If it took a branch 647 * it is because we set the S0 bit to make it 648 * branch to the stop command. 649 */ 650 dma_stopped = 1; 651 pi->dbdma_ring.cmds[i].xfer_status = 0; 652 653 if (++i >= runtime->periods) { 654 i = 0; 655 pi->frame_count += runtime->buffer_size; 656 } 657 pi->current_period = i; 658 659 /* 660 * Check the frame count. The DMA tends to get a bit 661 * ahead of the frame counter, which confuses the core. 662 */ 663 fc = in_le32(&i2sdev->intfregs->frame_count); 664 nframes = i * runtime->period_size; 665 if (fc < pi->frame_count + nframes) 666 pi->frame_count = fc - nframes; 667 } 668 669 if (dma_stopped) { 670 timeout = 1000; 671 for (;;) { 672 status = in_le32(&pi->dbdma->status); 673 if (!(status & ACTIVE) && (!in || (status & 0x80))) 674 break; 675 if (--timeout <= 0) { 676 printk(KERN_ERR "i2sbus: timed out " 677 "waiting for DMA to stop!\n"); 678 break; 679 } 680 udelay(1); 681 } 682 683 /* Turn off DMA controller, clear S0 bit */ 684 out_le32(&pi->dbdma->control, (RUN | PAUSE | 1) << 16); 685 686 pi->dbdma_ring.stopping = 0; 687 if (pi->stop_completion) 688 complete(pi->stop_completion); 689 } 690 691 if (!pi->dbdma_ring.running) 692 goto out_unlock; 693 spin_unlock(&i2sdev->low_lock); 694 /* may call _trigger again, hence needs to be unlocked */ 695 snd_pcm_period_elapsed(pi->substream); 696 return; 697 698 out_unlock: 699 spin_unlock(&i2sdev->low_lock); 700 } 701 702 irqreturn_t i2sbus_tx_intr(int irq, void *devid) 703 { 704 handle_interrupt((struct i2sbus_dev *)devid, 0); 705 return IRQ_HANDLED; 706 } 707 708 irqreturn_t i2sbus_rx_intr(int irq, void *devid) 709 { 710 handle_interrupt((struct i2sbus_dev *)devid, 1); 711 return IRQ_HANDLED; 712 } 713 714 static int i2sbus_playback_open(struct snd_pcm_substream *substream) 715 { 716 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 717 718 if (!i2sdev) 719 return -EINVAL; 720 i2sdev->out.substream = substream; 721 return i2sbus_pcm_open(i2sdev, 0); 722 } 723 724 static int i2sbus_playback_close(struct snd_pcm_substream *substream) 725 { 726 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 727 int err; 728 729 if (!i2sdev) 730 return -EINVAL; 731 if (i2sdev->out.substream != substream) 732 return -EINVAL; 733 err = i2sbus_pcm_close(i2sdev, 0); 734 if (!err) 735 i2sdev->out.substream = NULL; 736 return err; 737 } 738 739 static int i2sbus_playback_prepare(struct snd_pcm_substream *substream) 740 { 741 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 742 743 if (!i2sdev) 744 return -EINVAL; 745 if (i2sdev->out.substream != substream) 746 return -EINVAL; 747 return i2sbus_pcm_prepare(i2sdev, 0); 748 } 749 750 static int i2sbus_playback_trigger(struct snd_pcm_substream *substream, int cmd) 751 { 752 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 753 754 if (!i2sdev) 755 return -EINVAL; 756 if (i2sdev->out.substream != substream) 757 return -EINVAL; 758 return i2sbus_pcm_trigger(i2sdev, 0, cmd); 759 } 760 761 static snd_pcm_uframes_t i2sbus_playback_pointer(struct snd_pcm_substream 762 *substream) 763 { 764 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 765 766 if (!i2sdev) 767 return -EINVAL; 768 if (i2sdev->out.substream != substream) 769 return 0; 770 return i2sbus_pcm_pointer(i2sdev, 0); 771 } 772 773 static const struct snd_pcm_ops i2sbus_playback_ops = { 774 .open = i2sbus_playback_open, 775 .close = i2sbus_playback_close, 776 .ioctl = snd_pcm_lib_ioctl, 777 .hw_free = i2sbus_playback_hw_free, 778 .prepare = i2sbus_playback_prepare, 779 .trigger = i2sbus_playback_trigger, 780 .pointer = i2sbus_playback_pointer, 781 }; 782 783 static int i2sbus_record_open(struct snd_pcm_substream *substream) 784 { 785 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 786 787 if (!i2sdev) 788 return -EINVAL; 789 i2sdev->in.substream = substream; 790 return i2sbus_pcm_open(i2sdev, 1); 791 } 792 793 static int i2sbus_record_close(struct snd_pcm_substream *substream) 794 { 795 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 796 int err; 797 798 if (!i2sdev) 799 return -EINVAL; 800 if (i2sdev->in.substream != substream) 801 return -EINVAL; 802 err = i2sbus_pcm_close(i2sdev, 1); 803 if (!err) 804 i2sdev->in.substream = NULL; 805 return err; 806 } 807 808 static int i2sbus_record_prepare(struct snd_pcm_substream *substream) 809 { 810 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 811 812 if (!i2sdev) 813 return -EINVAL; 814 if (i2sdev->in.substream != substream) 815 return -EINVAL; 816 return i2sbus_pcm_prepare(i2sdev, 1); 817 } 818 819 static int i2sbus_record_trigger(struct snd_pcm_substream *substream, int cmd) 820 { 821 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 822 823 if (!i2sdev) 824 return -EINVAL; 825 if (i2sdev->in.substream != substream) 826 return -EINVAL; 827 return i2sbus_pcm_trigger(i2sdev, 1, cmd); 828 } 829 830 static snd_pcm_uframes_t i2sbus_record_pointer(struct snd_pcm_substream 831 *substream) 832 { 833 struct i2sbus_dev *i2sdev = snd_pcm_substream_chip(substream); 834 835 if (!i2sdev) 836 return -EINVAL; 837 if (i2sdev->in.substream != substream) 838 return 0; 839 return i2sbus_pcm_pointer(i2sdev, 1); 840 } 841 842 static const struct snd_pcm_ops i2sbus_record_ops = { 843 .open = i2sbus_record_open, 844 .close = i2sbus_record_close, 845 .ioctl = snd_pcm_lib_ioctl, 846 .hw_free = i2sbus_record_hw_free, 847 .prepare = i2sbus_record_prepare, 848 .trigger = i2sbus_record_trigger, 849 .pointer = i2sbus_record_pointer, 850 }; 851 852 static void i2sbus_private_free(struct snd_pcm *pcm) 853 { 854 struct i2sbus_dev *i2sdev = snd_pcm_chip(pcm); 855 struct codec_info_item *p, *tmp; 856 857 i2sdev->sound.pcm = NULL; 858 i2sdev->out.created = 0; 859 i2sdev->in.created = 0; 860 list_for_each_entry_safe(p, tmp, &i2sdev->sound.codec_list, list) { 861 printk(KERN_ERR "i2sbus: a codec didn't unregister!\n"); 862 list_del(&p->list); 863 module_put(p->codec->owner); 864 kfree(p); 865 } 866 soundbus_dev_put(&i2sdev->sound); 867 module_put(THIS_MODULE); 868 } 869 870 int 871 i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card, 872 struct codec_info *ci, void *data) 873 { 874 int err, in = 0, out = 0; 875 struct transfer_info *tmp; 876 struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev); 877 struct codec_info_item *cii; 878 879 if (!dev->pcmname || dev->pcmid == -1) { 880 printk(KERN_ERR "i2sbus: pcm name and id must be set!\n"); 881 return -EINVAL; 882 } 883 884 list_for_each_entry(cii, &dev->codec_list, list) { 885 if (cii->codec_data == data) 886 return -EALREADY; 887 } 888 889 if (!ci->transfers || !ci->transfers->formats 890 || !ci->transfers->rates || !ci->usable) 891 return -EINVAL; 892 893 /* we currently code the i2s transfer on the clock, and support only 894 * 32 and 64 */ 895 if (ci->bus_factor != 32 && ci->bus_factor != 64) 896 return -EINVAL; 897 898 /* If you want to fix this, you need to keep track of what transport infos 899 * are to be used, which codecs they belong to, and then fix all the 900 * sysclock/busclock stuff above to depend on which is usable */ 901 list_for_each_entry(cii, &dev->codec_list, list) { 902 if (cii->codec->sysclock_factor != ci->sysclock_factor) { 903 printk(KERN_DEBUG 904 "cannot yet handle multiple different sysclocks!\n"); 905 return -EINVAL; 906 } 907 if (cii->codec->bus_factor != ci->bus_factor) { 908 printk(KERN_DEBUG 909 "cannot yet handle multiple different bus clocks!\n"); 910 return -EINVAL; 911 } 912 } 913 914 tmp = ci->transfers; 915 while (tmp->formats && tmp->rates) { 916 if (tmp->transfer_in) 917 in = 1; 918 else 919 out = 1; 920 tmp++; 921 } 922 923 cii = kzalloc(sizeof(struct codec_info_item), GFP_KERNEL); 924 if (!cii) { 925 printk(KERN_DEBUG "i2sbus: failed to allocate cii\n"); 926 return -ENOMEM; 927 } 928 929 /* use the private data to point to the codec info */ 930 cii->sdev = soundbus_dev_get(dev); 931 cii->codec = ci; 932 cii->codec_data = data; 933 934 if (!cii->sdev) { 935 printk(KERN_DEBUG 936 "i2sbus: failed to get soundbus dev reference\n"); 937 err = -ENODEV; 938 goto out_free_cii; 939 } 940 941 if (!try_module_get(THIS_MODULE)) { 942 printk(KERN_DEBUG "i2sbus: failed to get module reference!\n"); 943 err = -EBUSY; 944 goto out_put_sdev; 945 } 946 947 if (!try_module_get(ci->owner)) { 948 printk(KERN_DEBUG 949 "i2sbus: failed to get module reference to codec owner!\n"); 950 err = -EBUSY; 951 goto out_put_this_module; 952 } 953 954 if (!dev->pcm) { 955 err = snd_pcm_new(card, dev->pcmname, dev->pcmid, 0, 0, 956 &dev->pcm); 957 if (err) { 958 printk(KERN_DEBUG "i2sbus: failed to create pcm\n"); 959 goto out_put_ci_module; 960 } 961 } 962 963 /* ALSA yet again sucks. 964 * If it is ever fixed, remove this line. See below. */ 965 out = in = 1; 966 967 if (!i2sdev->out.created && out) { 968 if (dev->pcm->card != card) { 969 /* eh? */ 970 printk(KERN_ERR 971 "Can't attach same bus to different cards!\n"); 972 err = -EINVAL; 973 goto out_put_ci_module; 974 } 975 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 1); 976 if (err) 977 goto out_put_ci_module; 978 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 979 &i2sbus_playback_ops); 980 dev->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].dev.parent = 981 &dev->ofdev.dev; 982 i2sdev->out.created = 1; 983 } 984 985 if (!i2sdev->in.created && in) { 986 if (dev->pcm->card != card) { 987 printk(KERN_ERR 988 "Can't attach same bus to different cards!\n"); 989 err = -EINVAL; 990 goto out_put_ci_module; 991 } 992 err = snd_pcm_new_stream(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 1); 993 if (err) 994 goto out_put_ci_module; 995 snd_pcm_set_ops(dev->pcm, SNDRV_PCM_STREAM_CAPTURE, 996 &i2sbus_record_ops); 997 dev->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].dev.parent = 998 &dev->ofdev.dev; 999 i2sdev->in.created = 1; 1000 } 1001 1002 /* so we have to register the pcm after adding any substream 1003 * to it because alsa doesn't create the devices for the 1004 * substreams when we add them later. 1005 * Therefore, force in and out on both busses (above) and 1006 * register the pcm now instead of just after creating it. 1007 */ 1008 err = snd_device_register(card, dev->pcm); 1009 if (err) { 1010 printk(KERN_ERR "i2sbus: error registering new pcm\n"); 1011 goto out_put_ci_module; 1012 } 1013 /* no errors any more, so let's add this to our list */ 1014 list_add(&cii->list, &dev->codec_list); 1015 1016 dev->pcm->private_data = i2sdev; 1017 dev->pcm->private_free = i2sbus_private_free; 1018 1019 /* well, we really should support scatter/gather DMA */ 1020 snd_pcm_set_managed_buffer_all( 1021 dev->pcm, SNDRV_DMA_TYPE_DEV, 1022 &macio_get_pci_dev(i2sdev->macio)->dev, 1023 64 * 1024, 64 * 1024); 1024 1025 return 0; 1026 out_put_ci_module: 1027 module_put(ci->owner); 1028 out_put_this_module: 1029 module_put(THIS_MODULE); 1030 out_put_sdev: 1031 soundbus_dev_put(dev); 1032 out_free_cii: 1033 kfree(cii); 1034 return err; 1035 } 1036 1037 void i2sbus_detach_codec(struct soundbus_dev *dev, void *data) 1038 { 1039 struct codec_info_item *cii = NULL, *i; 1040 1041 list_for_each_entry(i, &dev->codec_list, list) { 1042 if (i->codec_data == data) { 1043 cii = i; 1044 break; 1045 } 1046 } 1047 if (cii) { 1048 list_del(&cii->list); 1049 module_put(cii->codec->owner); 1050 kfree(cii); 1051 } 1052 /* no more codecs, but still a pcm? */ 1053 if (list_empty(&dev->codec_list) && dev->pcm) { 1054 /* the actual cleanup is done by the callback above! */ 1055 snd_device_free(dev->pcm->card, dev->pcm); 1056 } 1057 } 1058