1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC
5 * Copyright (C) 2006-2007 Atmel Norway
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>
22 #include <sound/initval.h>
23 #include <sound/control.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
27 #include <linux/atmel-ssc.h>
29 #include <linux/spi/spi.h>
30 #include <linux/spi/at73c213.h>
34 #define BITRATE_MIN 8000 /* Hardware limit? */
35 #define BITRATE_TARGET CONFIG_SND_AT73C213_TARGET_BITRATE
36 #define BITRATE_MAX 50000 /* Hardware limit. */
38 /* Initial (hardware reset) AT73C213 register values. */
39 static const u8 snd_at73c213_original_image[18] =
53 0x00, /* 0C - PRECH */
58 0x00, /* 11 - PA_CTRL */
62 struct snd_card *card;
64 struct snd_pcm_substream *substream;
65 struct at73c213_board_info *board;
68 unsigned long bitrate;
69 struct ssc_device *ssc;
70 struct spi_device *spi;
73 /* Image of the SPI registers in AT73C213. */
75 /* Protect SSC registers against concurrent access. */
77 /* Protect mixer registers against concurrent access. */
78 struct mutex mixer_lock;
81 #define get_chip(card) ((struct snd_at73c213 *)card->private_data)
84 snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val)
86 struct spi_message msg;
87 struct spi_transfer msg_xfer = {
93 spi_message_init(&msg);
95 chip->spi_wbuffer[0] = reg;
96 chip->spi_wbuffer[1] = val;
98 msg_xfer.tx_buf = chip->spi_wbuffer;
99 msg_xfer.rx_buf = chip->spi_rbuffer;
100 spi_message_add_tail(&msg_xfer, &msg);
102 retval = spi_sync(chip->spi, &msg);
105 chip->reg_image[reg] = val;
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. */
119 .buffer_bytes_max = 64 * 1024 - 1,
120 .period_bytes_min = 512,
121 .period_bytes_max = 64 * 1024 - 1,
127 * Calculate and set bitrate and divisions.
129 static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip)
131 unsigned long ssc_rate = clk_get_rate(chip->ssc->clk);
132 unsigned long dac_rate_new, ssc_div;
134 unsigned long ssc_div_max, ssc_div_min;
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.
142 * The DAC master clock (MCLK) is programmable, and is either 256
143 * or (not here) 384 times the I2S output clock (BCLK).
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;
155 /* ssc_div must be even. */
156 ssc_div = (ssc_div + 1) & ~1UL;
158 if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) {
160 if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX)
164 /* Search for a possible bitrate. */
166 /* SSC clock / (ssc divider * 16-bit * stereo). */
167 if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN)
170 /* 256 / (2 * 16) = 8 */
171 dac_rate_new = 8 * (ssc_rate / ssc_div);
173 status = clk_round_rate(chip->board->dac_clk, dac_rate_new);
177 /* Ignore difference smaller than 256 Hz. */
178 if ((status/256) == (dac_rate_new/256))
182 } while (--max_tries);
184 /* Not able to find a valid bitrate. */
188 status = clk_set_rate(chip->board->dac_clk, status);
192 /* Set divider in SSC device. */
193 ssc_writel(chip->ssc->regs, CMR, ssc_div/2);
195 /* SSC clock / (ssc divider * 16-bit * stereo). */
196 chip->bitrate = ssc_rate / (ssc_div * 16 * 2);
198 dev_info(&chip->spi->dev,
199 "at73c213: supported bitrate is %lu (%lu divider)\n",
200 chip->bitrate, ssc_div);
205 static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream)
207 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
208 struct snd_pcm_runtime *runtime = substream->runtime;
211 /* ensure buffer_size is a multiple of period_size */
212 err = snd_pcm_hw_constraint_integer(runtime,
213 SNDRV_PCM_HW_PARAM_PERIODS);
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;
221 err = clk_enable(chip->ssc->clk);
228 static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream)
230 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
231 chip->substream = NULL;
232 clk_disable(chip->ssc->clk);
236 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream,
237 struct snd_pcm_hw_params *hw_params)
239 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
240 int channels = params_channels(hw_params);
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);
250 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream)
252 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
253 struct snd_pcm_runtime *runtime = substream->runtime;
256 block_size = frames_to_bytes(runtime, runtime->period_size);
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);
272 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream,
275 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
278 spin_lock(&chip->lock);
281 case SNDRV_PCM_TRIGGER_START:
282 ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX));
283 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN));
285 case SNDRV_PCM_TRIGGER_STOP:
286 ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS));
287 ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX));
290 dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd);
295 spin_unlock(&chip->lock);
300 static snd_pcm_uframes_t
301 snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream)
303 struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
304 struct snd_pcm_runtime *runtime = substream->runtime;
305 snd_pcm_uframes_t pos;
308 bytes = ssc_readl(chip->ssc->regs, PDC_TPR)
309 - (unsigned long)runtime->dma_addr;
311 pos = bytes_to_frames(runtime, bytes);
312 if (pos >= runtime->buffer_size)
313 pos -= runtime->buffer_size;
318 static const struct snd_pcm_ops at73c213_playback_ops = {
319 .open = snd_at73c213_pcm_open,
320 .close = snd_at73c213_pcm_close,
321 .hw_params = snd_at73c213_pcm_hw_params,
322 .prepare = snd_at73c213_pcm_prepare,
323 .trigger = snd_at73c213_pcm_trigger,
324 .pointer = snd_at73c213_pcm_pointer,
327 static int snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device)
332 retval = snd_pcm_new(chip->card, chip->card->shortname,
337 pcm->private_data = chip;
338 pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER;
339 strcpy(pcm->name, "at73c213");
342 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops);
344 snd_pcm_set_managed_buffer_all(chip->pcm,
345 SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev,
346 64 * 1024, 64 * 1024);
351 static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id)
353 struct snd_at73c213 *chip = dev_id;
354 struct snd_pcm_runtime *runtime = chip->substream->runtime;
359 int retval = IRQ_NONE;
361 spin_lock(&chip->lock);
363 block_size = frames_to_bytes(runtime, runtime->period_size);
364 status = ssc_readl(chip->ssc->regs, IMR);
366 if (status & SSC_BIT(IMR_ENDTX)) {
368 if (chip->period == runtime->periods)
370 next_period = chip->period + 1;
371 if (next_period == runtime->periods)
374 offset = block_size * next_period;
376 ssc_writel(chip->ssc->regs, PDC_TNPR,
377 (long)runtime->dma_addr + offset);
378 ssc_writel(chip->ssc->regs, PDC_TNCR,
379 runtime->period_size * runtime->channels);
380 retval = IRQ_HANDLED;
383 ssc_readl(chip->ssc->regs, IMR);
384 spin_unlock(&chip->lock);
386 if (status & SSC_BIT(IMR_ENDTX))
387 snd_pcm_period_elapsed(chip->substream);
395 static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol,
396 struct snd_ctl_elem_value *ucontrol)
398 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
399 int reg = kcontrol->private_value & 0xff;
400 int shift = (kcontrol->private_value >> 8) & 0xff;
401 int mask = (kcontrol->private_value >> 16) & 0xff;
402 int invert = (kcontrol->private_value >> 24) & 0xff;
404 mutex_lock(&chip->mixer_lock);
406 ucontrol->value.integer.value[0] =
407 (chip->reg_image[reg] >> shift) & mask;
410 ucontrol->value.integer.value[0] =
411 mask - ucontrol->value.integer.value[0];
413 mutex_unlock(&chip->mixer_lock);
418 static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol,
419 struct snd_ctl_elem_value *ucontrol)
421 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
422 int reg = kcontrol->private_value & 0xff;
423 int shift = (kcontrol->private_value >> 8) & 0xff;
424 int mask = (kcontrol->private_value >> 16) & 0xff;
425 int invert = (kcontrol->private_value >> 24) & 0xff;
429 val = (ucontrol->value.integer.value[0] & mask);
434 mutex_lock(&chip->mixer_lock);
436 val = (chip->reg_image[reg] & ~(mask << shift)) | val;
437 change = val != chip->reg_image[reg];
438 retval = snd_at73c213_write_reg(chip, reg, val);
440 mutex_unlock(&chip->mixer_lock);
448 static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol,
449 struct snd_ctl_elem_info *uinfo)
451 int mask = (kcontrol->private_value >> 24) & 0xff;
454 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
456 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
459 uinfo->value.integer.min = 0;
460 uinfo->value.integer.max = mask;
465 static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol,
466 struct snd_ctl_elem_value *ucontrol)
468 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
469 int left_reg = kcontrol->private_value & 0xff;
470 int right_reg = (kcontrol->private_value >> 8) & 0xff;
471 int shift_left = (kcontrol->private_value >> 16) & 0x07;
472 int shift_right = (kcontrol->private_value >> 19) & 0x07;
473 int mask = (kcontrol->private_value >> 24) & 0xff;
474 int invert = (kcontrol->private_value >> 22) & 1;
476 mutex_lock(&chip->mixer_lock);
478 ucontrol->value.integer.value[0] =
479 (chip->reg_image[left_reg] >> shift_left) & mask;
480 ucontrol->value.integer.value[1] =
481 (chip->reg_image[right_reg] >> shift_right) & mask;
484 ucontrol->value.integer.value[0] =
485 mask - ucontrol->value.integer.value[0];
486 ucontrol->value.integer.value[1] =
487 mask - ucontrol->value.integer.value[1];
490 mutex_unlock(&chip->mixer_lock);
495 static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol,
496 struct snd_ctl_elem_value *ucontrol)
498 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
499 int left_reg = kcontrol->private_value & 0xff;
500 int right_reg = (kcontrol->private_value >> 8) & 0xff;
501 int shift_left = (kcontrol->private_value >> 16) & 0x07;
502 int shift_right = (kcontrol->private_value >> 19) & 0x07;
503 int mask = (kcontrol->private_value >> 24) & 0xff;
504 int invert = (kcontrol->private_value >> 22) & 1;
506 unsigned short val1, val2;
508 val1 = ucontrol->value.integer.value[0] & mask;
509 val2 = ucontrol->value.integer.value[1] & mask;
515 val2 <<= shift_right;
517 mutex_lock(&chip->mixer_lock);
519 val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1;
520 val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2;
521 change = val1 != chip->reg_image[left_reg]
522 || val2 != chip->reg_image[right_reg];
523 retval = snd_at73c213_write_reg(chip, left_reg, val1);
525 mutex_unlock(&chip->mixer_lock);
528 retval = snd_at73c213_write_reg(chip, right_reg, val2);
530 mutex_unlock(&chip->mixer_lock);
534 mutex_unlock(&chip->mixer_lock);
542 #define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info
544 static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol,
545 struct snd_ctl_elem_value *ucontrol)
547 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
548 int reg = kcontrol->private_value & 0xff;
549 int shift = (kcontrol->private_value >> 8) & 0xff;
550 int invert = (kcontrol->private_value >> 24) & 0xff;
552 mutex_lock(&chip->mixer_lock);
554 ucontrol->value.integer.value[0] =
555 (chip->reg_image[reg] >> shift) & 0x01;
558 ucontrol->value.integer.value[0] =
559 0x01 - ucontrol->value.integer.value[0];
561 mutex_unlock(&chip->mixer_lock);
566 static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol,
567 struct snd_ctl_elem_value *ucontrol)
569 struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
570 int reg = kcontrol->private_value & 0xff;
571 int shift = (kcontrol->private_value >> 8) & 0xff;
572 int mask = (kcontrol->private_value >> 16) & 0xff;
573 int invert = (kcontrol->private_value >> 24) & 0xff;
577 if (ucontrol->value.integer.value[0])
586 mutex_lock(&chip->mixer_lock);
588 val |= (chip->reg_image[reg] & ~(mask << shift));
589 change = val != chip->reg_image[reg];
591 retval = snd_at73c213_write_reg(chip, reg, val);
593 mutex_unlock(&chip->mixer_lock);
601 static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol,
602 struct snd_ctl_elem_info *uinfo)
604 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
606 uinfo->value.integer.min = 0;
607 uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1;
612 static int snd_at73c213_line_capture_volume_info(
613 struct snd_kcontrol *kcontrol,
614 struct snd_ctl_elem_info *uinfo)
616 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
618 /* When inverted will give values 0x10001 => 0. */
619 uinfo->value.integer.min = 14;
620 uinfo->value.integer.max = 31;
625 static int snd_at73c213_aux_capture_volume_info(
626 struct snd_kcontrol *kcontrol,
627 struct snd_ctl_elem_info *uinfo)
629 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
631 /* When inverted will give values 0x10001 => 0. */
632 uinfo->value.integer.min = 14;
633 uinfo->value.integer.max = 31;
638 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \
640 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
643 .info = snd_at73c213_mono_switch_info, \
644 .get = snd_at73c213_mono_switch_get, \
645 .put = snd_at73c213_mono_switch_put, \
646 .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
649 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
651 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
654 .info = snd_at73c213_stereo_info, \
655 .get = snd_at73c213_stereo_get, \
656 .put = snd_at73c213_stereo_put, \
657 .private_value = (left_reg | (right_reg << 8) \
658 | (shift_left << 16) | (shift_right << 19) \
659 | (mask << 24) | (invert << 22)) \
662 static const struct snd_kcontrol_new snd_at73c213_controls[] = {
663 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1),
664 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1),
665 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1),
666 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1),
667 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV,
670 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
671 .name = "PA Playback Volume",
673 .info = snd_at73c213_pa_volume_info,
674 .get = snd_at73c213_mono_get,
675 .put = snd_at73c213_mono_put,
676 .private_value = PA_CTRL | (PA_CTRL_APAGAIN << 8) | \
677 (0x0f << 16) | (1 << 24),
679 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP,
681 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0),
683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
684 .name = "Aux Capture Volume",
686 .info = snd_at73c213_aux_capture_volume_info,
687 .get = snd_at73c213_mono_get,
688 .put = snd_at73c213_mono_put,
689 .private_value = DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24),
691 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN,
694 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
695 .name = "Line Capture Volume",
697 .info = snd_at73c213_line_capture_volume_info,
698 .get = snd_at73c213_stereo_get,
699 .put = snd_at73c213_stereo_put,
700 .private_value = DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19)
701 | (0x1f << 24) | (1 << 22),
703 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0),
706 static int snd_at73c213_mixer(struct snd_at73c213 *chip)
708 struct snd_card *card;
711 if (chip == NULL || chip->pcm == NULL)
716 strcpy(card->mixername, chip->pcm->name);
718 for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) {
719 errval = snd_ctl_add(card,
720 snd_ctl_new1(&snd_at73c213_controls[idx],
729 for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) {
730 struct snd_kcontrol *kctl;
731 kctl = snd_ctl_find_numid(card, idx);
733 snd_ctl_remove(card, kctl);
741 static int snd_at73c213_ssc_init(struct snd_at73c213 *chip)
744 * Continuous clock output.
745 * Starts on falling TF.
746 * Delay 1 cycle (1 bit).
747 * Periode is 16 bit (16 - 1).
749 ssc_writel(chip->ssc->regs, TCMR,
751 | SSC_BF(TCMR_START, 4)
752 | SSC_BF(TCMR_STTDLY, 1)
753 | SSC_BF(TCMR_PERIOD, 16 - 1));
755 * Data length is 16 bit (16 - 1).
756 * Transmit MSB first.
757 * Transmit 2 words each transfer.
758 * Frame sync length is 16 bit (16 - 1).
759 * Frame starts on negative pulse.
761 ssc_writel(chip->ssc->regs, TFMR,
762 SSC_BF(TFMR_DATLEN, 16 - 1)
764 | SSC_BF(TFMR_DATNB, 1)
765 | SSC_BF(TFMR_FSLEN, 16 - 1)
766 | SSC_BF(TFMR_FSOS, 1));
771 static int snd_at73c213_chip_init(struct snd_at73c213 *chip)
774 unsigned char dac_ctrl = 0;
776 retval = snd_at73c213_set_bitrate(chip);
780 /* Enable DAC master clock. */
781 retval = clk_enable(chip->board->dac_clk);
785 /* Initialize at73c213 on SPI bus. */
786 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04);
790 retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03);
794 /* Precharge everything. */
795 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff);
798 retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH));
801 retval = snd_at73c213_write_reg(chip, DAC_CTRL,
802 (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR));
808 /* Stop precharging PA. */
809 retval = snd_at73c213_write_reg(chip, PA_CTRL,
810 (1<<PA_CTRL_APALP) | 0x0f);
816 /* Stop precharging DAC, turn on master power. */
817 retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR));
824 dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR)
825 | (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR);
827 retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl);
832 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
835 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
838 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
841 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
844 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
847 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
850 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
854 /* Enable I2S device, i.e. clock output. */
855 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
860 clk_disable(chip->board->dac_clk);
865 static int snd_at73c213_dev_free(struct snd_device *device)
867 struct snd_at73c213 *chip = device->device_data;
869 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
870 if (chip->irq >= 0) {
871 free_irq(chip->irq, chip);
878 static int snd_at73c213_dev_init(struct snd_card *card,
879 struct spi_device *spi)
881 static const struct snd_device_ops ops = {
882 .dev_free = snd_at73c213_dev_free,
884 struct snd_at73c213 *chip = get_chip(card);
887 irq = chip->ssc->irq;
891 spin_lock_init(&chip->lock);
892 mutex_init(&chip->mixer_lock);
896 retval = clk_enable(chip->ssc->clk);
900 retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip);
902 dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq);
907 memcpy(&chip->reg_image, &snd_at73c213_original_image,
908 sizeof(snd_at73c213_original_image));
910 retval = snd_at73c213_ssc_init(chip);
914 retval = snd_at73c213_chip_init(chip);
918 retval = snd_at73c213_pcm_new(chip, 0);
922 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
926 retval = snd_at73c213_mixer(chip);
933 snd_device_free(card, chip);
935 free_irq(chip->irq, chip);
938 clk_disable(chip->ssc->clk);
943 static int snd_at73c213_probe(struct spi_device *spi)
945 struct snd_card *card;
946 struct snd_at73c213 *chip;
947 struct at73c213_board_info *board;
951 board = spi->dev.platform_data;
953 dev_dbg(&spi->dev, "no platform_data\n");
957 if (!board->dac_clk) {
958 dev_dbg(&spi->dev, "no DAC clk\n");
962 if (IS_ERR(board->dac_clk)) {
963 dev_dbg(&spi->dev, "no DAC clk\n");
964 return PTR_ERR(board->dac_clk);
967 /* Allocate "card" using some unused identifiers. */
968 snprintf(id, sizeof id, "at73c213_%d", board->ssc_id);
969 retval = snd_card_new(&spi->dev, -1, id, THIS_MODULE,
970 sizeof(struct snd_at73c213), &card);
974 chip = card->private_data;
978 chip->ssc = ssc_request(board->ssc_id);
979 if (IS_ERR(chip->ssc)) {
980 dev_dbg(&spi->dev, "could not get ssc%d device\n",
982 retval = PTR_ERR(chip->ssc);
986 retval = snd_at73c213_dev_init(card, spi);
990 strcpy(card->driver, "at73c213");
991 strcpy(card->shortname, board->shortname);
992 sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq);
994 retval = snd_card_register(card);
998 dev_set_drvdata(&spi->dev, card);
1003 ssc_free(chip->ssc);
1005 snd_card_free(card);
1010 static void snd_at73c213_remove(struct spi_device *spi)
1012 struct snd_card *card = dev_get_drvdata(&spi->dev);
1013 struct snd_at73c213 *chip = card->private_data;
1016 /* Stop playback. */
1017 retval = clk_enable(chip->ssc->clk);
1020 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1021 clk_disable(chip->ssc->clk);
1024 retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
1027 retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
1030 retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
1033 retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
1036 retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
1039 retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
1042 retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
1047 retval = snd_at73c213_write_reg(chip, PA_CTRL,
1048 chip->reg_image[PA_CTRL] | 0x0f);
1052 retval = snd_at73c213_write_reg(chip, PA_CTRL,
1053 (1 << PA_CTRL_APALP) | 0x0f);
1057 /* Turn off external DAC. */
1058 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c);
1062 retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00);
1066 /* Turn off master power. */
1067 retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00);
1072 /* Stop DAC master clock. */
1073 clk_disable(chip->board->dac_clk);
1075 ssc_free(chip->ssc);
1076 snd_card_free(card);
1079 #ifdef CONFIG_PM_SLEEP
1081 static int snd_at73c213_suspend(struct device *dev)
1083 struct snd_card *card = dev_get_drvdata(dev);
1084 struct snd_at73c213 *chip = card->private_data;
1086 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1087 clk_disable(chip->ssc->clk);
1088 clk_disable(chip->board->dac_clk);
1093 static int snd_at73c213_resume(struct device *dev)
1095 struct snd_card *card = dev_get_drvdata(dev);
1096 struct snd_at73c213 *chip = card->private_data;
1099 retval = clk_enable(chip->board->dac_clk);
1102 retval = clk_enable(chip->ssc->clk);
1104 clk_disable(chip->board->dac_clk);
1107 ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
1112 static SIMPLE_DEV_PM_OPS(at73c213_pm_ops, snd_at73c213_suspend,
1113 snd_at73c213_resume);
1114 #define AT73C213_PM_OPS (&at73c213_pm_ops)
1117 #define AT73C213_PM_OPS NULL
1120 static struct spi_driver at73c213_driver = {
1123 .pm = AT73C213_PM_OPS,
1125 .probe = snd_at73c213_probe,
1126 .remove = snd_at73c213_remove,
1129 module_spi_driver(at73c213_driver);
1131 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
1132 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1133 MODULE_LICENSE("GPL");