1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC 4 * 5 * Copyright (C) 2006-2007 Atmel Norway 6 */ 7 8 /*#define DEBUG*/ 9 10 #include <linux/clk.h> 11 #include <linux/err.h> 12 #include <linux/delay.h> 13 #include <linux/device.h> 14 #include <linux/dma-mapping.h> 15 #include <linux/init.h> 16 #include <linux/interrupt.h> 17 #include <linux/module.h> 18 #include <linux/mutex.h> 19 #include <linux/platform_device.h> 20 #include <linux/io.h> 21 22 #include <sound/initval.h> 23 #include <sound/control.h> 24 #include <sound/core.h> 25 #include <sound/pcm.h> 26 27 #include <linux/atmel-ssc.h> 28 29 #include <linux/spi/spi.h> 30 #include <linux/spi/at73c213.h> 31 32 #include "at73c213.h" 33 34 #define BITRATE_MIN 8000 /* Hardware limit? */ 35 #define BITRATE_TARGET CONFIG_SND_AT73C213_TARGET_BITRATE 36 #define BITRATE_MAX 50000 /* Hardware limit. */ 37 38 /* Initial (hardware reset) AT73C213 register values. */ 39 static u8 snd_at73c213_original_image[18] = 40 { 41 0x00, /* 00 - CTRL */ 42 0x05, /* 01 - LLIG */ 43 0x05, /* 02 - RLIG */ 44 0x08, /* 03 - LPMG */ 45 0x08, /* 04 - RPMG */ 46 0x00, /* 05 - LLOG */ 47 0x00, /* 06 - RLOG */ 48 0x22, /* 07 - OLC */ 49 0x09, /* 08 - MC */ 50 0x00, /* 09 - CSFC */ 51 0x00, /* 0A - MISC */ 52 0x00, /* 0B - */ 53 0x00, /* 0C - PRECH */ 54 0x05, /* 0D - AUXG */ 55 0x00, /* 0E - */ 56 0x00, /* 0F - */ 57 0x00, /* 10 - RST */ 58 0x00, /* 11 - PA_CTRL */ 59 }; 60 61 struct snd_at73c213 { 62 struct snd_card *card; 63 struct snd_pcm *pcm; 64 struct snd_pcm_substream *substream; 65 struct at73c213_board_info *board; 66 int irq; 67 int period; 68 unsigned long bitrate; 69 struct ssc_device *ssc; 70 struct spi_device *spi; 71 u8 spi_wbuffer[2]; 72 u8 spi_rbuffer[2]; 73 /* Image of the SPI registers in AT73C213. */ 74 u8 reg_image[18]; 75 /* Protect SSC registers against concurrent access. */ 76 spinlock_t lock; 77 /* Protect mixer registers against concurrent access. */ 78 struct mutex mixer_lock; 79 }; 80 81 #define get_chip(card) ((struct snd_at73c213 *)card->private_data) 82 83 static int 84 snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val) 85 { 86 struct spi_message msg; 87 struct spi_transfer msg_xfer = { 88 .len = 2, 89 .cs_change = 0, 90 }; 91 int retval; 92 93 spi_message_init(&msg); 94 95 chip->spi_wbuffer[0] = reg; 96 chip->spi_wbuffer[1] = val; 97 98 msg_xfer.tx_buf = chip->spi_wbuffer; 99 msg_xfer.rx_buf = chip->spi_rbuffer; 100 spi_message_add_tail(&msg_xfer, &msg); 101 102 retval = spi_sync(chip->spi, &msg); 103 104 if (!retval) 105 chip->reg_image[reg] = val; 106 107 return retval; 108 } 109 110 static struct snd_pcm_hardware snd_at73c213_playback_hw = { 111 .info = SNDRV_PCM_INFO_INTERLEAVED | 112 SNDRV_PCM_INFO_BLOCK_TRANSFER, 113 .formats = SNDRV_PCM_FMTBIT_S16_BE, 114 .rates = SNDRV_PCM_RATE_CONTINUOUS, 115 .rate_min = 8000, /* Replaced by chip->bitrate later. */ 116 .rate_max = 50000, /* Replaced by chip->bitrate later. */ 117 .channels_min = 1, 118 .channels_max = 2, 119 .buffer_bytes_max = 64 * 1024 - 1, 120 .period_bytes_min = 512, 121 .period_bytes_max = 64 * 1024 - 1, 122 .periods_min = 4, 123 .periods_max = 1024, 124 }; 125 126 /* 127 * Calculate and set bitrate and divisions. 128 */ 129 static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip) 130 { 131 unsigned long ssc_rate = clk_get_rate(chip->ssc->clk); 132 unsigned long dac_rate_new, ssc_div; 133 int status; 134 unsigned long ssc_div_max, ssc_div_min; 135 int max_tries; 136 137 /* 138 * We connect two clocks here, picking divisors so the I2S clocks 139 * out data at the same rate the DAC clocks it in ... and as close 140 * as practical to the desired target rate. 141 * 142 * The DAC master clock (MCLK) is programmable, and is either 256 143 * or (not here) 384 times the I2S output clock (BCLK). 144 */ 145 146 /* SSC clock / (bitrate * stereo * 16-bit). */ 147 ssc_div = ssc_rate / (BITRATE_TARGET * 2 * 16); 148 ssc_div_min = ssc_rate / (BITRATE_MAX * 2 * 16); 149 ssc_div_max = ssc_rate / (BITRATE_MIN * 2 * 16); 150 max_tries = (ssc_div_max - ssc_div_min) / 2; 151 152 if (max_tries < 1) 153 max_tries = 1; 154 155 /* ssc_div must be even. */ 156 ssc_div = (ssc_div + 1) & ~1UL; 157 158 if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) { 159 ssc_div -= 2; 160 if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX) 161 return -ENXIO; 162 } 163 164 /* Search for a possible bitrate. */ 165 do { 166 /* SSC clock / (ssc divider * 16-bit * stereo). */ 167 if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) 168 return -ENXIO; 169 170 /* 256 / (2 * 16) = 8 */ 171 dac_rate_new = 8 * (ssc_rate / ssc_div); 172 173 status = clk_round_rate(chip->board->dac_clk, dac_rate_new); 174 if (status <= 0) 175 return status; 176 177 /* Ignore difference smaller than 256 Hz. */ 178 if ((status/256) == (dac_rate_new/256)) 179 goto set_rate; 180 181 ssc_div += 2; 182 } while (--max_tries); 183 184 /* Not able to find a valid bitrate. */ 185 return -ENXIO; 186 187 set_rate: 188 status = clk_set_rate(chip->board->dac_clk, status); 189 if (status < 0) 190 return status; 191 192 /* Set divider in SSC device. */ 193 ssc_writel(chip->ssc->regs, CMR, ssc_div/2); 194 195 /* SSC clock / (ssc divider * 16-bit * stereo). */ 196 chip->bitrate = ssc_rate / (ssc_div * 16 * 2); 197 198 dev_info(&chip->spi->dev, 199 "at73c213: supported bitrate is %lu (%lu divider)\n", 200 chip->bitrate, ssc_div); 201 202 return 0; 203 } 204 205 static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream) 206 { 207 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 208 struct snd_pcm_runtime *runtime = substream->runtime; 209 int err; 210 211 /* ensure buffer_size is a multiple of period_size */ 212 err = snd_pcm_hw_constraint_integer(runtime, 213 SNDRV_PCM_HW_PARAM_PERIODS); 214 if (err < 0) 215 return err; 216 snd_at73c213_playback_hw.rate_min = chip->bitrate; 217 snd_at73c213_playback_hw.rate_max = chip->bitrate; 218 runtime->hw = snd_at73c213_playback_hw; 219 chip->substream = substream; 220 221 clk_enable(chip->ssc->clk); 222 223 return 0; 224 } 225 226 static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream) 227 { 228 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 229 chip->substream = NULL; 230 clk_disable(chip->ssc->clk); 231 return 0; 232 } 233 234 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream, 235 struct snd_pcm_hw_params *hw_params) 236 { 237 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 238 int channels = params_channels(hw_params); 239 int val; 240 241 val = ssc_readl(chip->ssc->regs, TFMR); 242 val = SSC_BFINS(TFMR_DATNB, channels - 1, val); 243 ssc_writel(chip->ssc->regs, TFMR, val); 244 245 return 0; 246 } 247 248 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream) 249 { 250 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 251 struct snd_pcm_runtime *runtime = substream->runtime; 252 int block_size; 253 254 block_size = frames_to_bytes(runtime, runtime->period_size); 255 256 chip->period = 0; 257 258 ssc_writel(chip->ssc->regs, PDC_TPR, 259 (long)runtime->dma_addr); 260 ssc_writel(chip->ssc->regs, PDC_TCR, 261 runtime->period_size * runtime->channels); 262 ssc_writel(chip->ssc->regs, PDC_TNPR, 263 (long)runtime->dma_addr + block_size); 264 ssc_writel(chip->ssc->regs, PDC_TNCR, 265 runtime->period_size * runtime->channels); 266 267 return 0; 268 } 269 270 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream, 271 int cmd) 272 { 273 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 274 int retval = 0; 275 276 spin_lock(&chip->lock); 277 278 switch (cmd) { 279 case SNDRV_PCM_TRIGGER_START: 280 ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX)); 281 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN)); 282 break; 283 case SNDRV_PCM_TRIGGER_STOP: 284 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS)); 285 ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX)); 286 break; 287 default: 288 dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd); 289 retval = -EINVAL; 290 break; 291 } 292 293 spin_unlock(&chip->lock); 294 295 return retval; 296 } 297 298 static snd_pcm_uframes_t 299 snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream) 300 { 301 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 302 struct snd_pcm_runtime *runtime = substream->runtime; 303 snd_pcm_uframes_t pos; 304 unsigned long bytes; 305 306 bytes = ssc_readl(chip->ssc->regs, PDC_TPR) 307 - (unsigned long)runtime->dma_addr; 308 309 pos = bytes_to_frames(runtime, bytes); 310 if (pos >= runtime->buffer_size) 311 pos -= runtime->buffer_size; 312 313 return pos; 314 } 315 316 static const struct snd_pcm_ops at73c213_playback_ops = { 317 .open = snd_at73c213_pcm_open, 318 .close = snd_at73c213_pcm_close, 319 .ioctl = snd_pcm_lib_ioctl, 320 .hw_params = snd_at73c213_pcm_hw_params, 321 .prepare = snd_at73c213_pcm_prepare, 322 .trigger = snd_at73c213_pcm_trigger, 323 .pointer = snd_at73c213_pcm_pointer, 324 }; 325 326 static int snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device) 327 { 328 struct snd_pcm *pcm; 329 int retval; 330 331 retval = snd_pcm_new(chip->card, chip->card->shortname, 332 device, 1, 0, &pcm); 333 if (retval < 0) 334 goto out; 335 336 pcm->private_data = chip; 337 pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER; 338 strcpy(pcm->name, "at73c213"); 339 chip->pcm = pcm; 340 341 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops); 342 343 snd_pcm_set_managed_buffer_all(chip->pcm, 344 SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev, 345 64 * 1024, 64 * 1024); 346 out: 347 return retval; 348 } 349 350 static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id) 351 { 352 struct snd_at73c213 *chip = dev_id; 353 struct snd_pcm_runtime *runtime = chip->substream->runtime; 354 u32 status; 355 int offset; 356 int block_size; 357 int next_period; 358 int retval = IRQ_NONE; 359 360 spin_lock(&chip->lock); 361 362 block_size = frames_to_bytes(runtime, runtime->period_size); 363 status = ssc_readl(chip->ssc->regs, IMR); 364 365 if (status & SSC_BIT(IMR_ENDTX)) { 366 chip->period++; 367 if (chip->period == runtime->periods) 368 chip->period = 0; 369 next_period = chip->period + 1; 370 if (next_period == runtime->periods) 371 next_period = 0; 372 373 offset = block_size * next_period; 374 375 ssc_writel(chip->ssc->regs, PDC_TNPR, 376 (long)runtime->dma_addr + offset); 377 ssc_writel(chip->ssc->regs, PDC_TNCR, 378 runtime->period_size * runtime->channels); 379 retval = IRQ_HANDLED; 380 } 381 382 ssc_readl(chip->ssc->regs, IMR); 383 spin_unlock(&chip->lock); 384 385 if (status & SSC_BIT(IMR_ENDTX)) 386 snd_pcm_period_elapsed(chip->substream); 387 388 return retval; 389 } 390 391 /* 392 * Mixer functions. 393 */ 394 static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol, 395 struct snd_ctl_elem_value *ucontrol) 396 { 397 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 398 int reg = kcontrol->private_value & 0xff; 399 int shift = (kcontrol->private_value >> 8) & 0xff; 400 int mask = (kcontrol->private_value >> 16) & 0xff; 401 int invert = (kcontrol->private_value >> 24) & 0xff; 402 403 mutex_lock(&chip->mixer_lock); 404 405 ucontrol->value.integer.value[0] = 406 (chip->reg_image[reg] >> shift) & mask; 407 408 if (invert) 409 ucontrol->value.integer.value[0] = 410 mask - ucontrol->value.integer.value[0]; 411 412 mutex_unlock(&chip->mixer_lock); 413 414 return 0; 415 } 416 417 static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol, 418 struct snd_ctl_elem_value *ucontrol) 419 { 420 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 421 int reg = kcontrol->private_value & 0xff; 422 int shift = (kcontrol->private_value >> 8) & 0xff; 423 int mask = (kcontrol->private_value >> 16) & 0xff; 424 int invert = (kcontrol->private_value >> 24) & 0xff; 425 int change, retval; 426 unsigned short val; 427 428 val = (ucontrol->value.integer.value[0] & mask); 429 if (invert) 430 val = mask - val; 431 val <<= shift; 432 433 mutex_lock(&chip->mixer_lock); 434 435 val = (chip->reg_image[reg] & ~(mask << shift)) | val; 436 change = val != chip->reg_image[reg]; 437 retval = snd_at73c213_write_reg(chip, reg, val); 438 439 mutex_unlock(&chip->mixer_lock); 440 441 if (retval) 442 return retval; 443 444 return change; 445 } 446 447 static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol, 448 struct snd_ctl_elem_info *uinfo) 449 { 450 int mask = (kcontrol->private_value >> 24) & 0xff; 451 452 if (mask == 1) 453 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 454 else 455 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 456 457 uinfo->count = 2; 458 uinfo->value.integer.min = 0; 459 uinfo->value.integer.max = mask; 460 461 return 0; 462 } 463 464 static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol, 465 struct snd_ctl_elem_value *ucontrol) 466 { 467 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 468 int left_reg = kcontrol->private_value & 0xff; 469 int right_reg = (kcontrol->private_value >> 8) & 0xff; 470 int shift_left = (kcontrol->private_value >> 16) & 0x07; 471 int shift_right = (kcontrol->private_value >> 19) & 0x07; 472 int mask = (kcontrol->private_value >> 24) & 0xff; 473 int invert = (kcontrol->private_value >> 22) & 1; 474 475 mutex_lock(&chip->mixer_lock); 476 477 ucontrol->value.integer.value[0] = 478 (chip->reg_image[left_reg] >> shift_left) & mask; 479 ucontrol->value.integer.value[1] = 480 (chip->reg_image[right_reg] >> shift_right) & mask; 481 482 if (invert) { 483 ucontrol->value.integer.value[0] = 484 mask - ucontrol->value.integer.value[0]; 485 ucontrol->value.integer.value[1] = 486 mask - ucontrol->value.integer.value[1]; 487 } 488 489 mutex_unlock(&chip->mixer_lock); 490 491 return 0; 492 } 493 494 static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol, 495 struct snd_ctl_elem_value *ucontrol) 496 { 497 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 498 int left_reg = kcontrol->private_value & 0xff; 499 int right_reg = (kcontrol->private_value >> 8) & 0xff; 500 int shift_left = (kcontrol->private_value >> 16) & 0x07; 501 int shift_right = (kcontrol->private_value >> 19) & 0x07; 502 int mask = (kcontrol->private_value >> 24) & 0xff; 503 int invert = (kcontrol->private_value >> 22) & 1; 504 int change, retval; 505 unsigned short val1, val2; 506 507 val1 = ucontrol->value.integer.value[0] & mask; 508 val2 = ucontrol->value.integer.value[1] & mask; 509 if (invert) { 510 val1 = mask - val1; 511 val2 = mask - val2; 512 } 513 val1 <<= shift_left; 514 val2 <<= shift_right; 515 516 mutex_lock(&chip->mixer_lock); 517 518 val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1; 519 val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2; 520 change = val1 != chip->reg_image[left_reg] 521 || val2 != chip->reg_image[right_reg]; 522 retval = snd_at73c213_write_reg(chip, left_reg, val1); 523 if (retval) { 524 mutex_unlock(&chip->mixer_lock); 525 goto out; 526 } 527 retval = snd_at73c213_write_reg(chip, right_reg, val2); 528 if (retval) { 529 mutex_unlock(&chip->mixer_lock); 530 goto out; 531 } 532 533 mutex_unlock(&chip->mixer_lock); 534 535 return change; 536 537 out: 538 return retval; 539 } 540 541 #define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info 542 543 static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol, 544 struct snd_ctl_elem_value *ucontrol) 545 { 546 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 547 int reg = kcontrol->private_value & 0xff; 548 int shift = (kcontrol->private_value >> 8) & 0xff; 549 int invert = (kcontrol->private_value >> 24) & 0xff; 550 551 mutex_lock(&chip->mixer_lock); 552 553 ucontrol->value.integer.value[0] = 554 (chip->reg_image[reg] >> shift) & 0x01; 555 556 if (invert) 557 ucontrol->value.integer.value[0] = 558 0x01 - ucontrol->value.integer.value[0]; 559 560 mutex_unlock(&chip->mixer_lock); 561 562 return 0; 563 } 564 565 static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol, 566 struct snd_ctl_elem_value *ucontrol) 567 { 568 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 569 int reg = kcontrol->private_value & 0xff; 570 int shift = (kcontrol->private_value >> 8) & 0xff; 571 int mask = (kcontrol->private_value >> 16) & 0xff; 572 int invert = (kcontrol->private_value >> 24) & 0xff; 573 int change, retval; 574 unsigned short val; 575 576 if (ucontrol->value.integer.value[0]) 577 val = mask; 578 else 579 val = 0; 580 581 if (invert) 582 val = mask - val; 583 val <<= shift; 584 585 mutex_lock(&chip->mixer_lock); 586 587 val |= (chip->reg_image[reg] & ~(mask << shift)); 588 change = val != chip->reg_image[reg]; 589 590 retval = snd_at73c213_write_reg(chip, reg, val); 591 592 mutex_unlock(&chip->mixer_lock); 593 594 if (retval) 595 return retval; 596 597 return change; 598 } 599 600 static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol, 601 struct snd_ctl_elem_info *uinfo) 602 { 603 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 604 uinfo->count = 1; 605 uinfo->value.integer.min = 0; 606 uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1; 607 608 return 0; 609 } 610 611 static int snd_at73c213_line_capture_volume_info( 612 struct snd_kcontrol *kcontrol, 613 struct snd_ctl_elem_info *uinfo) 614 { 615 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 616 uinfo->count = 2; 617 /* When inverted will give values 0x10001 => 0. */ 618 uinfo->value.integer.min = 14; 619 uinfo->value.integer.max = 31; 620 621 return 0; 622 } 623 624 static int snd_at73c213_aux_capture_volume_info( 625 struct snd_kcontrol *kcontrol, 626 struct snd_ctl_elem_info *uinfo) 627 { 628 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 629 uinfo->count = 1; 630 /* When inverted will give values 0x10001 => 0. */ 631 uinfo->value.integer.min = 14; 632 uinfo->value.integer.max = 31; 633 634 return 0; 635 } 636 637 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \ 638 { \ 639 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 640 .name = xname, \ 641 .index = xindex, \ 642 .info = snd_at73c213_mono_switch_info, \ 643 .get = snd_at73c213_mono_switch_get, \ 644 .put = snd_at73c213_mono_switch_put, \ 645 .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \ 646 } 647 648 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \ 649 { \ 650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 651 .name = xname, \ 652 .index = xindex, \ 653 .info = snd_at73c213_stereo_info, \ 654 .get = snd_at73c213_stereo_get, \ 655 .put = snd_at73c213_stereo_put, \ 656 .private_value = (left_reg | (right_reg << 8) \ 657 | (shift_left << 16) | (shift_right << 19) \ 658 | (mask << 24) | (invert << 22)) \ 659 } 660 661 static struct snd_kcontrol_new snd_at73c213_controls[] = { 662 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1), 663 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1), 664 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1), 665 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1), 666 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV, 667 0x01, 0), 668 { 669 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 670 .name = "PA Playback Volume", 671 .index = 0, 672 .info = snd_at73c213_pa_volume_info, 673 .get = snd_at73c213_mono_get, 674 .put = snd_at73c213_mono_put, 675 .private_value = PA_CTRL | (PA_CTRL_APAGAIN << 8) | \ 676 (0x0f << 16) | (1 << 24), 677 }, 678 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP, 679 0x01, 1), 680 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0), 681 { 682 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 683 .name = "Aux Capture Volume", 684 .index = 0, 685 .info = snd_at73c213_aux_capture_volume_info, 686 .get = snd_at73c213_mono_get, 687 .put = snd_at73c213_mono_put, 688 .private_value = DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24), 689 }, 690 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN, 691 0x01, 0), 692 { 693 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 694 .name = "Line Capture Volume", 695 .index = 0, 696 .info = snd_at73c213_line_capture_volume_info, 697 .get = snd_at73c213_stereo_get, 698 .put = snd_at73c213_stereo_put, 699 .private_value = DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19) 700 | (0x1f << 24) | (1 << 22), 701 }, 702 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0), 703 }; 704 705 static int snd_at73c213_mixer(struct snd_at73c213 *chip) 706 { 707 struct snd_card *card; 708 int errval, idx; 709 710 if (chip == NULL || chip->pcm == NULL) 711 return -EINVAL; 712 713 card = chip->card; 714 715 strcpy(card->mixername, chip->pcm->name); 716 717 for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) { 718 errval = snd_ctl_add(card, 719 snd_ctl_new1(&snd_at73c213_controls[idx], 720 chip)); 721 if (errval < 0) 722 goto cleanup; 723 } 724 725 return 0; 726 727 cleanup: 728 for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) { 729 struct snd_kcontrol *kctl; 730 kctl = snd_ctl_find_numid(card, idx); 731 if (kctl) 732 snd_ctl_remove(card, kctl); 733 } 734 return errval; 735 } 736 737 /* 738 * Device functions 739 */ 740 static int snd_at73c213_ssc_init(struct snd_at73c213 *chip) 741 { 742 /* 743 * Continuous clock output. 744 * Starts on falling TF. 745 * Delay 1 cycle (1 bit). 746 * Periode is 16 bit (16 - 1). 747 */ 748 ssc_writel(chip->ssc->regs, TCMR, 749 SSC_BF(TCMR_CKO, 1) 750 | SSC_BF(TCMR_START, 4) 751 | SSC_BF(TCMR_STTDLY, 1) 752 | SSC_BF(TCMR_PERIOD, 16 - 1)); 753 /* 754 * Data length is 16 bit (16 - 1). 755 * Transmit MSB first. 756 * Transmit 2 words each transfer. 757 * Frame sync length is 16 bit (16 - 1). 758 * Frame starts on negative pulse. 759 */ 760 ssc_writel(chip->ssc->regs, TFMR, 761 SSC_BF(TFMR_DATLEN, 16 - 1) 762 | SSC_BIT(TFMR_MSBF) 763 | SSC_BF(TFMR_DATNB, 1) 764 | SSC_BF(TFMR_FSLEN, 16 - 1) 765 | SSC_BF(TFMR_FSOS, 1)); 766 767 return 0; 768 } 769 770 static int snd_at73c213_chip_init(struct snd_at73c213 *chip) 771 { 772 int retval; 773 unsigned char dac_ctrl = 0; 774 775 retval = snd_at73c213_set_bitrate(chip); 776 if (retval) 777 goto out; 778 779 /* Enable DAC master clock. */ 780 clk_enable(chip->board->dac_clk); 781 782 /* Initialize at73c213 on SPI bus. */ 783 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04); 784 if (retval) 785 goto out_clk; 786 msleep(1); 787 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03); 788 if (retval) 789 goto out_clk; 790 791 /* Precharge everything. */ 792 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff); 793 if (retval) 794 goto out_clk; 795 retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH)); 796 if (retval) 797 goto out_clk; 798 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 799 (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR)); 800 if (retval) 801 goto out_clk; 802 803 msleep(50); 804 805 /* Stop precharging PA. */ 806 retval = snd_at73c213_write_reg(chip, PA_CTRL, 807 (1<<PA_CTRL_APALP) | 0x0f); 808 if (retval) 809 goto out_clk; 810 811 msleep(450); 812 813 /* Stop precharging DAC, turn on master power. */ 814 retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR)); 815 if (retval) 816 goto out_clk; 817 818 msleep(1); 819 820 /* Turn on DAC. */ 821 dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR) 822 | (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR); 823 824 retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl); 825 if (retval) 826 goto out_clk; 827 828 /* Mute sound. */ 829 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f); 830 if (retval) 831 goto out_clk; 832 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f); 833 if (retval) 834 goto out_clk; 835 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f); 836 if (retval) 837 goto out_clk; 838 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f); 839 if (retval) 840 goto out_clk; 841 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11); 842 if (retval) 843 goto out_clk; 844 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11); 845 if (retval) 846 goto out_clk; 847 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11); 848 if (retval) 849 goto out_clk; 850 851 /* Enable I2S device, i.e. clock output. */ 852 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN)); 853 854 goto out; 855 856 out_clk: 857 clk_disable(chip->board->dac_clk); 858 out: 859 return retval; 860 } 861 862 static int snd_at73c213_dev_free(struct snd_device *device) 863 { 864 struct snd_at73c213 *chip = device->device_data; 865 866 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS)); 867 if (chip->irq >= 0) { 868 free_irq(chip->irq, chip); 869 chip->irq = -1; 870 } 871 872 return 0; 873 } 874 875 static int snd_at73c213_dev_init(struct snd_card *card, 876 struct spi_device *spi) 877 { 878 static struct snd_device_ops ops = { 879 .dev_free = snd_at73c213_dev_free, 880 }; 881 struct snd_at73c213 *chip = get_chip(card); 882 int irq, retval; 883 884 irq = chip->ssc->irq; 885 if (irq < 0) 886 return irq; 887 888 spin_lock_init(&chip->lock); 889 mutex_init(&chip->mixer_lock); 890 chip->card = card; 891 chip->irq = -1; 892 893 clk_enable(chip->ssc->clk); 894 895 retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip); 896 if (retval) { 897 dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq); 898 goto out; 899 } 900 chip->irq = irq; 901 902 memcpy(&chip->reg_image, &snd_at73c213_original_image, 903 sizeof(snd_at73c213_original_image)); 904 905 retval = snd_at73c213_ssc_init(chip); 906 if (retval) 907 goto out_irq; 908 909 retval = snd_at73c213_chip_init(chip); 910 if (retval) 911 goto out_irq; 912 913 retval = snd_at73c213_pcm_new(chip, 0); 914 if (retval) 915 goto out_irq; 916 917 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); 918 if (retval) 919 goto out_irq; 920 921 retval = snd_at73c213_mixer(chip); 922 if (retval) 923 goto out_snd_dev; 924 925 goto out; 926 927 out_snd_dev: 928 snd_device_free(card, chip); 929 out_irq: 930 free_irq(chip->irq, chip); 931 chip->irq = -1; 932 out: 933 clk_disable(chip->ssc->clk); 934 935 return retval; 936 } 937 938 static int snd_at73c213_probe(struct spi_device *spi) 939 { 940 struct snd_card *card; 941 struct snd_at73c213 *chip; 942 struct at73c213_board_info *board; 943 int retval; 944 char id[16]; 945 946 board = spi->dev.platform_data; 947 if (!board) { 948 dev_dbg(&spi->dev, "no platform_data\n"); 949 return -ENXIO; 950 } 951 952 if (!board->dac_clk) { 953 dev_dbg(&spi->dev, "no DAC clk\n"); 954 return -ENXIO; 955 } 956 957 if (IS_ERR(board->dac_clk)) { 958 dev_dbg(&spi->dev, "no DAC clk\n"); 959 return PTR_ERR(board->dac_clk); 960 } 961 962 /* Allocate "card" using some unused identifiers. */ 963 snprintf(id, sizeof id, "at73c213_%d", board->ssc_id); 964 retval = snd_card_new(&spi->dev, -1, id, THIS_MODULE, 965 sizeof(struct snd_at73c213), &card); 966 if (retval < 0) 967 goto out; 968 969 chip = card->private_data; 970 chip->spi = spi; 971 chip->board = board; 972 973 chip->ssc = ssc_request(board->ssc_id); 974 if (IS_ERR(chip->ssc)) { 975 dev_dbg(&spi->dev, "could not get ssc%d device\n", 976 board->ssc_id); 977 retval = PTR_ERR(chip->ssc); 978 goto out_card; 979 } 980 981 retval = snd_at73c213_dev_init(card, spi); 982 if (retval) 983 goto out_ssc; 984 985 strcpy(card->driver, "at73c213"); 986 strcpy(card->shortname, board->shortname); 987 sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq); 988 989 retval = snd_card_register(card); 990 if (retval) 991 goto out_ssc; 992 993 dev_set_drvdata(&spi->dev, card); 994 995 goto out; 996 997 out_ssc: 998 ssc_free(chip->ssc); 999 out_card: 1000 snd_card_free(card); 1001 out: 1002 return retval; 1003 } 1004 1005 static int snd_at73c213_remove(struct spi_device *spi) 1006 { 1007 struct snd_card *card = dev_get_drvdata(&spi->dev); 1008 struct snd_at73c213 *chip = card->private_data; 1009 int retval; 1010 1011 /* Stop playback. */ 1012 clk_enable(chip->ssc->clk); 1013 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS)); 1014 clk_disable(chip->ssc->clk); 1015 1016 /* Mute sound. */ 1017 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f); 1018 if (retval) 1019 goto out; 1020 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f); 1021 if (retval) 1022 goto out; 1023 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f); 1024 if (retval) 1025 goto out; 1026 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f); 1027 if (retval) 1028 goto out; 1029 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11); 1030 if (retval) 1031 goto out; 1032 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11); 1033 if (retval) 1034 goto out; 1035 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11); 1036 if (retval) 1037 goto out; 1038 1039 /* Turn off PA. */ 1040 retval = snd_at73c213_write_reg(chip, PA_CTRL, 1041 chip->reg_image[PA_CTRL] | 0x0f); 1042 if (retval) 1043 goto out; 1044 msleep(10); 1045 retval = snd_at73c213_write_reg(chip, PA_CTRL, 1046 (1 << PA_CTRL_APALP) | 0x0f); 1047 if (retval) 1048 goto out; 1049 1050 /* Turn off external DAC. */ 1051 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c); 1052 if (retval) 1053 goto out; 1054 msleep(2); 1055 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00); 1056 if (retval) 1057 goto out; 1058 1059 /* Turn off master power. */ 1060 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00); 1061 if (retval) 1062 goto out; 1063 1064 out: 1065 /* Stop DAC master clock. */ 1066 clk_disable(chip->board->dac_clk); 1067 1068 ssc_free(chip->ssc); 1069 snd_card_free(card); 1070 1071 return 0; 1072 } 1073 1074 #ifdef CONFIG_PM_SLEEP 1075 1076 static int snd_at73c213_suspend(struct device *dev) 1077 { 1078 struct snd_card *card = dev_get_drvdata(dev); 1079 struct snd_at73c213 *chip = card->private_data; 1080 1081 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS)); 1082 clk_disable(chip->ssc->clk); 1083 clk_disable(chip->board->dac_clk); 1084 1085 return 0; 1086 } 1087 1088 static int snd_at73c213_resume(struct device *dev) 1089 { 1090 struct snd_card *card = dev_get_drvdata(dev); 1091 struct snd_at73c213 *chip = card->private_data; 1092 1093 clk_enable(chip->board->dac_clk); 1094 clk_enable(chip->ssc->clk); 1095 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN)); 1096 1097 return 0; 1098 } 1099 1100 static SIMPLE_DEV_PM_OPS(at73c213_pm_ops, snd_at73c213_suspend, 1101 snd_at73c213_resume); 1102 #define AT73C213_PM_OPS (&at73c213_pm_ops) 1103 1104 #else 1105 #define AT73C213_PM_OPS NULL 1106 #endif 1107 1108 static struct spi_driver at73c213_driver = { 1109 .driver = { 1110 .name = "at73c213", 1111 .pm = AT73C213_PM_OPS, 1112 }, 1113 .probe = snd_at73c213_probe, 1114 .remove = snd_at73c213_remove, 1115 }; 1116 1117 module_spi_driver(at73c213_driver); 1118 1119 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>"); 1120 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC"); 1121 MODULE_LICENSE("GPL"); 1122