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 const 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 err = clk_enable(chip->ssc->clk); 222 if (err) 223 return err; 224 225 return 0; 226 } 227 228 static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream) 229 { 230 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 231 chip->substream = NULL; 232 clk_disable(chip->ssc->clk); 233 return 0; 234 } 235 236 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream, 237 struct snd_pcm_hw_params *hw_params) 238 { 239 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 240 int channels = params_channels(hw_params); 241 int val; 242 243 val = ssc_readl(chip->ssc->regs, TFMR); 244 val = SSC_BFINS(TFMR_DATNB, channels - 1, val); 245 ssc_writel(chip->ssc->regs, TFMR, val); 246 247 return 0; 248 } 249 250 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream) 251 { 252 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 253 struct snd_pcm_runtime *runtime = substream->runtime; 254 int block_size; 255 256 block_size = frames_to_bytes(runtime, runtime->period_size); 257 258 chip->period = 0; 259 260 ssc_writel(chip->ssc->regs, PDC_TPR, 261 (long)runtime->dma_addr); 262 ssc_writel(chip->ssc->regs, PDC_TCR, 263 runtime->period_size * runtime->channels); 264 ssc_writel(chip->ssc->regs, PDC_TNPR, 265 (long)runtime->dma_addr + block_size); 266 ssc_writel(chip->ssc->regs, PDC_TNCR, 267 runtime->period_size * runtime->channels); 268 269 return 0; 270 } 271 272 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream, 273 int cmd) 274 { 275 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 276 277 guard(spinlock)(&chip->lock); 278 279 switch (cmd) { 280 case SNDRV_PCM_TRIGGER_START: 281 ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX)); 282 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN)); 283 break; 284 case SNDRV_PCM_TRIGGER_STOP: 285 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS)); 286 ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX)); 287 break; 288 default: 289 dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd); 290 return -EINVAL; 291 break; 292 } 293 294 return 0; 295 } 296 297 static snd_pcm_uframes_t 298 snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream) 299 { 300 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream); 301 struct snd_pcm_runtime *runtime = substream->runtime; 302 snd_pcm_uframes_t pos; 303 unsigned long bytes; 304 305 bytes = ssc_readl(chip->ssc->regs, PDC_TPR) 306 - (unsigned long)runtime->dma_addr; 307 308 pos = bytes_to_frames(runtime, bytes); 309 if (pos >= runtime->buffer_size) 310 pos -= runtime->buffer_size; 311 312 return pos; 313 } 314 315 static const struct snd_pcm_ops at73c213_playback_ops = { 316 .open = snd_at73c213_pcm_open, 317 .close = snd_at73c213_pcm_close, 318 .hw_params = snd_at73c213_pcm_hw_params, 319 .prepare = snd_at73c213_pcm_prepare, 320 .trigger = snd_at73c213_pcm_trigger, 321 .pointer = snd_at73c213_pcm_pointer, 322 }; 323 324 static int snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device) 325 { 326 struct snd_pcm *pcm; 327 int retval; 328 329 retval = snd_pcm_new(chip->card, chip->card->shortname, 330 device, 1, 0, &pcm); 331 if (retval < 0) 332 goto out; 333 334 pcm->private_data = chip; 335 pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER; 336 strscpy(pcm->name, "at73c213"); 337 chip->pcm = pcm; 338 339 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops); 340 341 snd_pcm_set_managed_buffer_all(chip->pcm, 342 SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev, 343 64 * 1024, 64 * 1024); 344 out: 345 return retval; 346 } 347 348 static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id) 349 { 350 struct snd_at73c213 *chip = dev_id; 351 struct snd_pcm_runtime *runtime = chip->substream->runtime; 352 u32 status; 353 int offset; 354 int block_size; 355 int next_period; 356 int retval = IRQ_NONE; 357 358 scoped_guard(spinlock, &chip->lock) { 359 block_size = frames_to_bytes(runtime, runtime->period_size); 360 status = ssc_readl(chip->ssc->regs, IMR); 361 362 if (status & SSC_BIT(IMR_ENDTX)) { 363 chip->period++; 364 if (chip->period == runtime->periods) 365 chip->period = 0; 366 next_period = chip->period + 1; 367 if (next_period == runtime->periods) 368 next_period = 0; 369 370 offset = block_size * next_period; 371 372 ssc_writel(chip->ssc->regs, PDC_TNPR, 373 (long)runtime->dma_addr + offset); 374 ssc_writel(chip->ssc->regs, PDC_TNCR, 375 runtime->period_size * runtime->channels); 376 retval = IRQ_HANDLED; 377 } 378 379 ssc_readl(chip->ssc->regs, IMR); 380 } 381 382 if (status & SSC_BIT(IMR_ENDTX)) 383 snd_pcm_period_elapsed(chip->substream); 384 385 return retval; 386 } 387 388 /* 389 * Mixer functions. 390 */ 391 static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol, 392 struct snd_ctl_elem_value *ucontrol) 393 { 394 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 395 int reg = kcontrol->private_value & 0xff; 396 int shift = (kcontrol->private_value >> 8) & 0xff; 397 int mask = (kcontrol->private_value >> 16) & 0xff; 398 int invert = (kcontrol->private_value >> 24) & 0xff; 399 400 guard(mutex)(&chip->mixer_lock); 401 402 ucontrol->value.integer.value[0] = 403 (chip->reg_image[reg] >> shift) & mask; 404 405 if (invert) 406 ucontrol->value.integer.value[0] = 407 mask - ucontrol->value.integer.value[0]; 408 409 return 0; 410 } 411 412 static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol, 413 struct snd_ctl_elem_value *ucontrol) 414 { 415 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 416 int reg = kcontrol->private_value & 0xff; 417 int shift = (kcontrol->private_value >> 8) & 0xff; 418 int mask = (kcontrol->private_value >> 16) & 0xff; 419 int invert = (kcontrol->private_value >> 24) & 0xff; 420 int change, retval; 421 unsigned short val; 422 423 val = (ucontrol->value.integer.value[0] & mask); 424 if (invert) 425 val = mask - val; 426 val <<= shift; 427 428 guard(mutex)(&chip->mixer_lock); 429 430 val = (chip->reg_image[reg] & ~(mask << shift)) | val; 431 change = val != chip->reg_image[reg]; 432 retval = snd_at73c213_write_reg(chip, reg, val); 433 434 if (retval) 435 return retval; 436 437 return change; 438 } 439 440 static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol, 441 struct snd_ctl_elem_info *uinfo) 442 { 443 int mask = (kcontrol->private_value >> 24) & 0xff; 444 445 if (mask == 1) 446 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 447 else 448 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 449 450 uinfo->count = 2; 451 uinfo->value.integer.min = 0; 452 uinfo->value.integer.max = mask; 453 454 return 0; 455 } 456 457 static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol, 458 struct snd_ctl_elem_value *ucontrol) 459 { 460 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 461 int left_reg = kcontrol->private_value & 0xff; 462 int right_reg = (kcontrol->private_value >> 8) & 0xff; 463 int shift_left = (kcontrol->private_value >> 16) & 0x07; 464 int shift_right = (kcontrol->private_value >> 19) & 0x07; 465 int mask = (kcontrol->private_value >> 24) & 0xff; 466 int invert = (kcontrol->private_value >> 22) & 1; 467 468 guard(mutex)(&chip->mixer_lock); 469 470 ucontrol->value.integer.value[0] = 471 (chip->reg_image[left_reg] >> shift_left) & mask; 472 ucontrol->value.integer.value[1] = 473 (chip->reg_image[right_reg] >> shift_right) & mask; 474 475 if (invert) { 476 ucontrol->value.integer.value[0] = 477 mask - ucontrol->value.integer.value[0]; 478 ucontrol->value.integer.value[1] = 479 mask - ucontrol->value.integer.value[1]; 480 } 481 482 return 0; 483 } 484 485 static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol, 486 struct snd_ctl_elem_value *ucontrol) 487 { 488 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 489 int left_reg = kcontrol->private_value & 0xff; 490 int right_reg = (kcontrol->private_value >> 8) & 0xff; 491 int shift_left = (kcontrol->private_value >> 16) & 0x07; 492 int shift_right = (kcontrol->private_value >> 19) & 0x07; 493 int mask = (kcontrol->private_value >> 24) & 0xff; 494 int invert = (kcontrol->private_value >> 22) & 1; 495 int change, retval; 496 unsigned short val1, val2; 497 498 val1 = ucontrol->value.integer.value[0] & mask; 499 val2 = ucontrol->value.integer.value[1] & mask; 500 if (invert) { 501 val1 = mask - val1; 502 val2 = mask - val2; 503 } 504 val1 <<= shift_left; 505 val2 <<= shift_right; 506 507 guard(mutex)(&chip->mixer_lock); 508 509 val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1; 510 val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2; 511 change = val1 != chip->reg_image[left_reg] 512 || val2 != chip->reg_image[right_reg]; 513 retval = snd_at73c213_write_reg(chip, left_reg, val1); 514 if (retval) 515 return retval; 516 retval = snd_at73c213_write_reg(chip, right_reg, val2); 517 if (retval) 518 return retval; 519 520 return change; 521 } 522 523 #define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info 524 525 static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol, 526 struct snd_ctl_elem_value *ucontrol) 527 { 528 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 529 int reg = kcontrol->private_value & 0xff; 530 int shift = (kcontrol->private_value >> 8) & 0xff; 531 int invert = (kcontrol->private_value >> 24) & 0xff; 532 533 guard(mutex)(&chip->mixer_lock); 534 535 ucontrol->value.integer.value[0] = 536 (chip->reg_image[reg] >> shift) & 0x01; 537 538 if (invert) 539 ucontrol->value.integer.value[0] = 540 0x01 - ucontrol->value.integer.value[0]; 541 542 return 0; 543 } 544 545 static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol, 546 struct snd_ctl_elem_value *ucontrol) 547 { 548 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol); 549 int reg = kcontrol->private_value & 0xff; 550 int shift = (kcontrol->private_value >> 8) & 0xff; 551 int mask = (kcontrol->private_value >> 16) & 0xff; 552 int invert = (kcontrol->private_value >> 24) & 0xff; 553 int change, retval; 554 unsigned short val; 555 556 if (ucontrol->value.integer.value[0]) 557 val = mask; 558 else 559 val = 0; 560 561 if (invert) 562 val = mask - val; 563 val <<= shift; 564 565 guard(mutex)(&chip->mixer_lock); 566 567 val |= (chip->reg_image[reg] & ~(mask << shift)); 568 change = val != chip->reg_image[reg]; 569 570 retval = snd_at73c213_write_reg(chip, reg, val); 571 572 if (retval) 573 return retval; 574 575 return change; 576 } 577 578 static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol, 579 struct snd_ctl_elem_info *uinfo) 580 { 581 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 582 uinfo->count = 1; 583 uinfo->value.integer.min = 0; 584 uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1; 585 586 return 0; 587 } 588 589 static int snd_at73c213_line_capture_volume_info( 590 struct snd_kcontrol *kcontrol, 591 struct snd_ctl_elem_info *uinfo) 592 { 593 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 594 uinfo->count = 2; 595 /* When inverted will give values 0x10001 => 0. */ 596 uinfo->value.integer.min = 14; 597 uinfo->value.integer.max = 31; 598 599 return 0; 600 } 601 602 static int snd_at73c213_aux_capture_volume_info( 603 struct snd_kcontrol *kcontrol, 604 struct snd_ctl_elem_info *uinfo) 605 { 606 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 607 uinfo->count = 1; 608 /* When inverted will give values 0x10001 => 0. */ 609 uinfo->value.integer.min = 14; 610 uinfo->value.integer.max = 31; 611 612 return 0; 613 } 614 615 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \ 616 { \ 617 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 618 .name = xname, \ 619 .index = xindex, \ 620 .info = snd_at73c213_mono_switch_info, \ 621 .get = snd_at73c213_mono_switch_get, \ 622 .put = snd_at73c213_mono_switch_put, \ 623 .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \ 624 } 625 626 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \ 627 { \ 628 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 629 .name = xname, \ 630 .index = xindex, \ 631 .info = snd_at73c213_stereo_info, \ 632 .get = snd_at73c213_stereo_get, \ 633 .put = snd_at73c213_stereo_put, \ 634 .private_value = (left_reg | (right_reg << 8) \ 635 | (shift_left << 16) | (shift_right << 19) \ 636 | (mask << 24) | (invert << 22)) \ 637 } 638 639 static const struct snd_kcontrol_new snd_at73c213_controls[] = { 640 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1), 641 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1), 642 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1), 643 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1), 644 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV, 645 0x01, 0), 646 { 647 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 648 .name = "PA Playback Volume", 649 .index = 0, 650 .info = snd_at73c213_pa_volume_info, 651 .get = snd_at73c213_mono_get, 652 .put = snd_at73c213_mono_put, 653 .private_value = PA_CTRL | (PA_CTRL_APAGAIN << 8) | \ 654 (0x0f << 16) | (1 << 24), 655 }, 656 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP, 657 0x01, 1), 658 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0), 659 { 660 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 661 .name = "Aux Capture Volume", 662 .index = 0, 663 .info = snd_at73c213_aux_capture_volume_info, 664 .get = snd_at73c213_mono_get, 665 .put = snd_at73c213_mono_put, 666 .private_value = DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24), 667 }, 668 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN, 669 0x01, 0), 670 { 671 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 672 .name = "Line Capture Volume", 673 .index = 0, 674 .info = snd_at73c213_line_capture_volume_info, 675 .get = snd_at73c213_stereo_get, 676 .put = snd_at73c213_stereo_put, 677 .private_value = DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19) 678 | (0x1f << 24) | (1 << 22), 679 }, 680 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0), 681 }; 682 683 static int snd_at73c213_mixer(struct snd_at73c213 *chip) 684 { 685 struct snd_card *card; 686 int errval, idx; 687 688 if (chip == NULL || chip->pcm == NULL) 689 return -EINVAL; 690 691 card = chip->card; 692 693 strscpy(card->mixername, chip->pcm->name); 694 695 for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) { 696 errval = snd_ctl_add(card, 697 snd_ctl_new1(&snd_at73c213_controls[idx], 698 chip)); 699 if (errval < 0) 700 goto cleanup; 701 } 702 703 return 0; 704 705 cleanup: 706 for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) 707 snd_ctl_remove(card, snd_ctl_find_numid(card, idx)); 708 return errval; 709 } 710 711 /* 712 * Device functions 713 */ 714 static int snd_at73c213_ssc_init(struct snd_at73c213 *chip) 715 { 716 /* 717 * Continuous clock output. 718 * Starts on falling TF. 719 * Delay 1 cycle (1 bit). 720 * Periode is 16 bit (16 - 1). 721 */ 722 ssc_writel(chip->ssc->regs, TCMR, 723 SSC_BF(TCMR_CKO, 1) 724 | SSC_BF(TCMR_START, 4) 725 | SSC_BF(TCMR_STTDLY, 1) 726 | SSC_BF(TCMR_PERIOD, 16 - 1)); 727 /* 728 * Data length is 16 bit (16 - 1). 729 * Transmit MSB first. 730 * Transmit 2 words each transfer. 731 * Frame sync length is 16 bit (16 - 1). 732 * Frame starts on negative pulse. 733 */ 734 ssc_writel(chip->ssc->regs, TFMR, 735 SSC_BF(TFMR_DATLEN, 16 - 1) 736 | SSC_BIT(TFMR_MSBF) 737 | SSC_BF(TFMR_DATNB, 1) 738 | SSC_BF(TFMR_FSLEN, 16 - 1) 739 | SSC_BF(TFMR_FSOS, 1)); 740 741 return 0; 742 } 743 744 static int snd_at73c213_chip_init(struct snd_at73c213 *chip) 745 { 746 int retval; 747 unsigned char dac_ctrl = 0; 748 749 retval = snd_at73c213_set_bitrate(chip); 750 if (retval) 751 goto out; 752 753 /* Enable DAC master clock. */ 754 retval = clk_enable(chip->board->dac_clk); 755 if (retval) 756 goto out; 757 758 /* Initialize at73c213 on SPI bus. */ 759 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04); 760 if (retval) 761 goto out_clk; 762 msleep(1); 763 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03); 764 if (retval) 765 goto out_clk; 766 767 /* Precharge everything. */ 768 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff); 769 if (retval) 770 goto out_clk; 771 retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH)); 772 if (retval) 773 goto out_clk; 774 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 775 (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR)); 776 if (retval) 777 goto out_clk; 778 779 msleep(50); 780 781 /* Stop precharging PA. */ 782 retval = snd_at73c213_write_reg(chip, PA_CTRL, 783 (1<<PA_CTRL_APALP) | 0x0f); 784 if (retval) 785 goto out_clk; 786 787 msleep(450); 788 789 /* Stop precharging DAC, turn on master power. */ 790 retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR)); 791 if (retval) 792 goto out_clk; 793 794 msleep(1); 795 796 /* Turn on DAC. */ 797 dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR) 798 | (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR); 799 800 retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl); 801 if (retval) 802 goto out_clk; 803 804 /* Mute sound. */ 805 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f); 806 if (retval) 807 goto out_clk; 808 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f); 809 if (retval) 810 goto out_clk; 811 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f); 812 if (retval) 813 goto out_clk; 814 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f); 815 if (retval) 816 goto out_clk; 817 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11); 818 if (retval) 819 goto out_clk; 820 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11); 821 if (retval) 822 goto out_clk; 823 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11); 824 if (retval) 825 goto out_clk; 826 827 /* Enable I2S device, i.e. clock output. */ 828 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN)); 829 830 goto out; 831 832 out_clk: 833 clk_disable(chip->board->dac_clk); 834 out: 835 return retval; 836 } 837 838 static int snd_at73c213_dev_free(struct snd_device *device) 839 { 840 struct snd_at73c213 *chip = device->device_data; 841 842 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS)); 843 if (chip->irq >= 0) { 844 free_irq(chip->irq, chip); 845 chip->irq = -1; 846 } 847 848 return 0; 849 } 850 851 static int snd_at73c213_dev_init(struct snd_card *card, 852 struct spi_device *spi) 853 { 854 static const struct snd_device_ops ops = { 855 .dev_free = snd_at73c213_dev_free, 856 }; 857 struct snd_at73c213 *chip = get_chip(card); 858 int irq, retval; 859 860 irq = chip->ssc->irq; 861 if (irq < 0) 862 return irq; 863 864 spin_lock_init(&chip->lock); 865 mutex_init(&chip->mixer_lock); 866 chip->card = card; 867 chip->irq = -1; 868 869 retval = clk_enable(chip->ssc->clk); 870 if (retval) 871 return retval; 872 873 retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip); 874 if (retval) { 875 dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq); 876 goto out; 877 } 878 chip->irq = irq; 879 880 memcpy(&chip->reg_image, &snd_at73c213_original_image, 881 sizeof(snd_at73c213_original_image)); 882 883 retval = snd_at73c213_ssc_init(chip); 884 if (retval) 885 goto out_irq; 886 887 retval = snd_at73c213_chip_init(chip); 888 if (retval) 889 goto out_irq; 890 891 retval = snd_at73c213_pcm_new(chip, 0); 892 if (retval) 893 goto out_irq; 894 895 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); 896 if (retval) 897 goto out_irq; 898 899 retval = snd_at73c213_mixer(chip); 900 if (retval) 901 goto out_snd_dev; 902 903 goto out; 904 905 out_snd_dev: 906 snd_device_free(card, chip); 907 out_irq: 908 free_irq(chip->irq, chip); 909 chip->irq = -1; 910 out: 911 clk_disable(chip->ssc->clk); 912 913 return retval; 914 } 915 916 static int snd_at73c213_probe(struct spi_device *spi) 917 { 918 struct snd_card *card; 919 struct snd_at73c213 *chip; 920 struct at73c213_board_info *board; 921 int retval; 922 char id[16]; 923 924 board = spi->dev.platform_data; 925 if (!board) { 926 dev_dbg(&spi->dev, "no platform_data\n"); 927 return -ENXIO; 928 } 929 930 if (!board->dac_clk) { 931 dev_dbg(&spi->dev, "no DAC clk\n"); 932 return -ENXIO; 933 } 934 935 if (IS_ERR(board->dac_clk)) { 936 dev_dbg(&spi->dev, "no DAC clk\n"); 937 return PTR_ERR(board->dac_clk); 938 } 939 940 /* Allocate "card" using some unused identifiers. */ 941 snprintf(id, sizeof id, "at73c213_%d", board->ssc_id); 942 retval = snd_card_new(&spi->dev, -1, id, THIS_MODULE, 943 sizeof(struct snd_at73c213), &card); 944 if (retval < 0) 945 goto out; 946 947 chip = card->private_data; 948 chip->spi = spi; 949 chip->board = board; 950 951 chip->ssc = ssc_request(board->ssc_id); 952 if (IS_ERR(chip->ssc)) { 953 dev_dbg(&spi->dev, "could not get ssc%d device\n", 954 board->ssc_id); 955 retval = PTR_ERR(chip->ssc); 956 goto out_card; 957 } 958 959 retval = snd_at73c213_dev_init(card, spi); 960 if (retval) 961 goto out_ssc; 962 963 strscpy(card->driver, "at73c213"); 964 strscpy(card->shortname, board->shortname); 965 sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq); 966 967 retval = snd_card_register(card); 968 if (retval) 969 goto out_ssc; 970 971 dev_set_drvdata(&spi->dev, card); 972 973 goto out; 974 975 out_ssc: 976 ssc_free(chip->ssc); 977 out_card: 978 snd_card_free(card); 979 out: 980 return retval; 981 } 982 983 static void snd_at73c213_remove(struct spi_device *spi) 984 { 985 struct snd_card *card = dev_get_drvdata(&spi->dev); 986 struct snd_at73c213 *chip = card->private_data; 987 int retval; 988 989 /* Stop playback. */ 990 retval = clk_enable(chip->ssc->clk); 991 if (retval) 992 goto out; 993 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS)); 994 clk_disable(chip->ssc->clk); 995 996 /* Mute sound. */ 997 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f); 998 if (retval) 999 goto out; 1000 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f); 1001 if (retval) 1002 goto out; 1003 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f); 1004 if (retval) 1005 goto out; 1006 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f); 1007 if (retval) 1008 goto out; 1009 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11); 1010 if (retval) 1011 goto out; 1012 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11); 1013 if (retval) 1014 goto out; 1015 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11); 1016 if (retval) 1017 goto out; 1018 1019 /* Turn off PA. */ 1020 retval = snd_at73c213_write_reg(chip, PA_CTRL, 1021 chip->reg_image[PA_CTRL] | 0x0f); 1022 if (retval) 1023 goto out; 1024 msleep(10); 1025 retval = snd_at73c213_write_reg(chip, PA_CTRL, 1026 (1 << PA_CTRL_APALP) | 0x0f); 1027 if (retval) 1028 goto out; 1029 1030 /* Turn off external DAC. */ 1031 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c); 1032 if (retval) 1033 goto out; 1034 msleep(2); 1035 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00); 1036 if (retval) 1037 goto out; 1038 1039 /* Turn off master power. */ 1040 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00); 1041 if (retval) 1042 goto out; 1043 1044 out: 1045 /* Stop DAC master clock. */ 1046 clk_disable(chip->board->dac_clk); 1047 1048 ssc_free(chip->ssc); 1049 snd_card_free(card); 1050 } 1051 1052 static int snd_at73c213_suspend(struct device *dev) 1053 { 1054 struct snd_card *card = dev_get_drvdata(dev); 1055 struct snd_at73c213 *chip = card->private_data; 1056 1057 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS)); 1058 clk_disable(chip->ssc->clk); 1059 clk_disable(chip->board->dac_clk); 1060 1061 return 0; 1062 } 1063 1064 static int snd_at73c213_resume(struct device *dev) 1065 { 1066 struct snd_card *card = dev_get_drvdata(dev); 1067 struct snd_at73c213 *chip = card->private_data; 1068 int retval; 1069 1070 retval = clk_enable(chip->board->dac_clk); 1071 if (retval) 1072 return retval; 1073 retval = clk_enable(chip->ssc->clk); 1074 if (retval) { 1075 clk_disable(chip->board->dac_clk); 1076 return retval; 1077 } 1078 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN)); 1079 1080 return 0; 1081 } 1082 1083 static DEFINE_SIMPLE_DEV_PM_OPS(at73c213_pm_ops, snd_at73c213_suspend, 1084 snd_at73c213_resume); 1085 1086 static struct spi_driver at73c213_driver = { 1087 .driver = { 1088 .name = "at73c213", 1089 .pm = &at73c213_pm_ops, 1090 }, 1091 .probe = snd_at73c213_probe, 1092 .remove = snd_at73c213_remove, 1093 }; 1094 1095 module_spi_driver(at73c213_driver); 1096 1097 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>"); 1098 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC"); 1099 MODULE_LICENSE("GPL"); 1100