Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC
0004  *
0005  * Copyright (C) 2006-2007 Atmel Norway
0006  */
0007 
0008 /*#define DEBUG*/
0009 
0010 #include <linux/clk.h>
0011 #include <linux/err.h>
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/dma-mapping.h>
0015 #include <linux/init.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/module.h>
0018 #include <linux/mutex.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/io.h>
0021 
0022 #include <sound/initval.h>
0023 #include <sound/control.h>
0024 #include <sound/core.h>
0025 #include <sound/pcm.h>
0026 
0027 #include <linux/atmel-ssc.h>
0028 
0029 #include <linux/spi/spi.h>
0030 #include <linux/spi/at73c213.h>
0031 
0032 #include "at73c213.h"
0033 
0034 #define BITRATE_MIN  8000 /* Hardware limit? */
0035 #define BITRATE_TARGET  CONFIG_SND_AT73C213_TARGET_BITRATE
0036 #define BITRATE_MAX 50000 /* Hardware limit. */
0037 
0038 /* Initial (hardware reset) AT73C213 register values. */
0039 static const u8 snd_at73c213_original_image[18] =
0040 {
0041     0x00,   /* 00 - CTRL    */
0042     0x05,   /* 01 - LLIG    */
0043     0x05,   /* 02 - RLIG    */
0044     0x08,   /* 03 - LPMG    */
0045     0x08,   /* 04 - RPMG    */
0046     0x00,   /* 05 - LLOG    */
0047     0x00,   /* 06 - RLOG    */
0048     0x22,   /* 07 - OLC     */
0049     0x09,   /* 08 - MC      */
0050     0x00,   /* 09 - CSFC    */
0051     0x00,   /* 0A - MISC    */
0052     0x00,   /* 0B -         */
0053     0x00,   /* 0C - PRECH   */
0054     0x05,   /* 0D - AUXG    */
0055     0x00,   /* 0E -         */
0056     0x00,   /* 0F -         */
0057     0x00,   /* 10 - RST     */
0058     0x00,   /* 11 - PA_CTRL */
0059 };
0060 
0061 struct snd_at73c213 {
0062     struct snd_card         *card;
0063     struct snd_pcm          *pcm;
0064     struct snd_pcm_substream    *substream;
0065     struct at73c213_board_info  *board;
0066     int             irq;
0067     int             period;
0068     unsigned long           bitrate;
0069     struct ssc_device       *ssc;
0070     struct spi_device       *spi;
0071     u8              spi_wbuffer[2];
0072     u8              spi_rbuffer[2];
0073     /* Image of the SPI registers in AT73C213. */
0074     u8              reg_image[18];
0075     /* Protect SSC registers against concurrent access. */
0076     spinlock_t          lock;
0077     /* Protect mixer registers against concurrent access. */
0078     struct mutex            mixer_lock;
0079 };
0080 
0081 #define get_chip(card) ((struct snd_at73c213 *)card->private_data)
0082 
0083 static int
0084 snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val)
0085 {
0086     struct spi_message msg;
0087     struct spi_transfer msg_xfer = {
0088         .len        = 2,
0089         .cs_change  = 0,
0090     };
0091     int retval;
0092 
0093     spi_message_init(&msg);
0094 
0095     chip->spi_wbuffer[0] = reg;
0096     chip->spi_wbuffer[1] = val;
0097 
0098     msg_xfer.tx_buf = chip->spi_wbuffer;
0099     msg_xfer.rx_buf = chip->spi_rbuffer;
0100     spi_message_add_tail(&msg_xfer, &msg);
0101 
0102     retval = spi_sync(chip->spi, &msg);
0103 
0104     if (!retval)
0105         chip->reg_image[reg] = val;
0106 
0107     return retval;
0108 }
0109 
0110 static struct snd_pcm_hardware snd_at73c213_playback_hw = {
0111     .info       = SNDRV_PCM_INFO_INTERLEAVED |
0112               SNDRV_PCM_INFO_BLOCK_TRANSFER,
0113     .formats    = SNDRV_PCM_FMTBIT_S16_BE,
0114     .rates      = SNDRV_PCM_RATE_CONTINUOUS,
0115     .rate_min   = 8000,  /* Replaced by chip->bitrate later. */
0116     .rate_max   = 50000, /* Replaced by chip->bitrate later. */
0117     .channels_min   = 1,
0118     .channels_max   = 2,
0119     .buffer_bytes_max = 64 * 1024 - 1,
0120     .period_bytes_min = 512,
0121     .period_bytes_max = 64 * 1024 - 1,
0122     .periods_min    = 4,
0123     .periods_max    = 1024,
0124 };
0125 
0126 /*
0127  * Calculate and set bitrate and divisions.
0128  */
0129 static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip)
0130 {
0131     unsigned long ssc_rate = clk_get_rate(chip->ssc->clk);
0132     unsigned long dac_rate_new, ssc_div;
0133     int status;
0134     unsigned long ssc_div_max, ssc_div_min;
0135     int max_tries;
0136 
0137     /*
0138      * We connect two clocks here, picking divisors so the I2S clocks
0139      * out data at the same rate the DAC clocks it in ... and as close
0140      * as practical to the desired target rate.
0141      *
0142      * The DAC master clock (MCLK) is programmable, and is either 256
0143      * or (not here) 384 times the I2S output clock (BCLK).
0144      */
0145 
0146     /* SSC clock / (bitrate * stereo * 16-bit). */
0147     ssc_div = ssc_rate / (BITRATE_TARGET * 2 * 16);
0148     ssc_div_min = ssc_rate / (BITRATE_MAX * 2 * 16);
0149     ssc_div_max = ssc_rate / (BITRATE_MIN * 2 * 16);
0150     max_tries = (ssc_div_max - ssc_div_min) / 2;
0151 
0152     if (max_tries < 1)
0153         max_tries = 1;
0154 
0155     /* ssc_div must be even. */
0156     ssc_div = (ssc_div + 1) & ~1UL;
0157 
0158     if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) {
0159         ssc_div -= 2;
0160         if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX)
0161             return -ENXIO;
0162     }
0163 
0164     /* Search for a possible bitrate. */
0165     do {
0166         /* SSC clock / (ssc divider * 16-bit * stereo). */
0167         if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN)
0168             return -ENXIO;
0169 
0170         /* 256 / (2 * 16) = 8 */
0171         dac_rate_new = 8 * (ssc_rate / ssc_div);
0172 
0173         status = clk_round_rate(chip->board->dac_clk, dac_rate_new);
0174         if (status <= 0)
0175             return status;
0176 
0177         /* Ignore difference smaller than 256 Hz. */
0178         if ((status/256) == (dac_rate_new/256))
0179             goto set_rate;
0180 
0181         ssc_div += 2;
0182     } while (--max_tries);
0183 
0184     /* Not able to find a valid bitrate. */
0185     return -ENXIO;
0186 
0187 set_rate:
0188     status = clk_set_rate(chip->board->dac_clk, status);
0189     if (status < 0)
0190         return status;
0191 
0192     /* Set divider in SSC device. */
0193     ssc_writel(chip->ssc->regs, CMR, ssc_div/2);
0194 
0195     /* SSC clock / (ssc divider * 16-bit * stereo). */
0196     chip->bitrate = ssc_rate / (ssc_div * 16 * 2);
0197 
0198     dev_info(&chip->spi->dev,
0199             "at73c213: supported bitrate is %lu (%lu divider)\n",
0200             chip->bitrate, ssc_div);
0201 
0202     return 0;
0203 }
0204 
0205 static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream)
0206 {
0207     struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
0208     struct snd_pcm_runtime *runtime = substream->runtime;
0209     int err;
0210 
0211     /* ensure buffer_size is a multiple of period_size */
0212     err = snd_pcm_hw_constraint_integer(runtime,
0213                     SNDRV_PCM_HW_PARAM_PERIODS);
0214     if (err < 0)
0215         return err;
0216     snd_at73c213_playback_hw.rate_min = chip->bitrate;
0217     snd_at73c213_playback_hw.rate_max = chip->bitrate;
0218     runtime->hw = snd_at73c213_playback_hw;
0219     chip->substream = substream;
0220 
0221     err = clk_enable(chip->ssc->clk);
0222     if (err)
0223         return err;
0224 
0225     return 0;
0226 }
0227 
0228 static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream)
0229 {
0230     struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
0231     chip->substream = NULL;
0232     clk_disable(chip->ssc->clk);
0233     return 0;
0234 }
0235 
0236 static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream,
0237                  struct snd_pcm_hw_params *hw_params)
0238 {
0239     struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
0240     int channels = params_channels(hw_params);
0241     int val;
0242 
0243     val = ssc_readl(chip->ssc->regs, TFMR);
0244     val = SSC_BFINS(TFMR_DATNB, channels - 1, val);
0245     ssc_writel(chip->ssc->regs, TFMR, val);
0246 
0247     return 0;
0248 }
0249 
0250 static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream)
0251 {
0252     struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
0253     struct snd_pcm_runtime *runtime = substream->runtime;
0254     int block_size;
0255 
0256     block_size = frames_to_bytes(runtime, runtime->period_size);
0257 
0258     chip->period = 0;
0259 
0260     ssc_writel(chip->ssc->regs, PDC_TPR,
0261             (long)runtime->dma_addr);
0262     ssc_writel(chip->ssc->regs, PDC_TCR,
0263             runtime->period_size * runtime->channels);
0264     ssc_writel(chip->ssc->regs, PDC_TNPR,
0265             (long)runtime->dma_addr + block_size);
0266     ssc_writel(chip->ssc->regs, PDC_TNCR,
0267             runtime->period_size * runtime->channels);
0268 
0269     return 0;
0270 }
0271 
0272 static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream,
0273                    int cmd)
0274 {
0275     struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
0276     int retval = 0;
0277 
0278     spin_lock(&chip->lock);
0279 
0280     switch (cmd) {
0281     case SNDRV_PCM_TRIGGER_START:
0282         ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX));
0283         ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN));
0284         break;
0285     case SNDRV_PCM_TRIGGER_STOP:
0286         ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS));
0287         ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX));
0288         break;
0289     default:
0290         dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd);
0291         retval = -EINVAL;
0292         break;
0293     }
0294 
0295     spin_unlock(&chip->lock);
0296 
0297     return retval;
0298 }
0299 
0300 static snd_pcm_uframes_t
0301 snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream)
0302 {
0303     struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
0304     struct snd_pcm_runtime *runtime = substream->runtime;
0305     snd_pcm_uframes_t pos;
0306     unsigned long bytes;
0307 
0308     bytes = ssc_readl(chip->ssc->regs, PDC_TPR)
0309         - (unsigned long)runtime->dma_addr;
0310 
0311     pos = bytes_to_frames(runtime, bytes);
0312     if (pos >= runtime->buffer_size)
0313         pos -= runtime->buffer_size;
0314 
0315     return pos;
0316 }
0317 
0318 static const struct snd_pcm_ops at73c213_playback_ops = {
0319     .open       = snd_at73c213_pcm_open,
0320     .close      = snd_at73c213_pcm_close,
0321     .hw_params  = snd_at73c213_pcm_hw_params,
0322     .prepare    = snd_at73c213_pcm_prepare,
0323     .trigger    = snd_at73c213_pcm_trigger,
0324     .pointer    = snd_at73c213_pcm_pointer,
0325 };
0326 
0327 static int snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device)
0328 {
0329     struct snd_pcm *pcm;
0330     int retval;
0331 
0332     retval = snd_pcm_new(chip->card, chip->card->shortname,
0333             device, 1, 0, &pcm);
0334     if (retval < 0)
0335         goto out;
0336 
0337     pcm->private_data = chip;
0338     pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER;
0339     strcpy(pcm->name, "at73c213");
0340     chip->pcm = pcm;
0341 
0342     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops);
0343 
0344     snd_pcm_set_managed_buffer_all(chip->pcm,
0345             SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev,
0346             64 * 1024, 64 * 1024);
0347 out:
0348     return retval;
0349 }
0350 
0351 static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id)
0352 {
0353     struct snd_at73c213 *chip = dev_id;
0354     struct snd_pcm_runtime *runtime = chip->substream->runtime;
0355     u32 status;
0356     int offset;
0357     int block_size;
0358     int next_period;
0359     int retval = IRQ_NONE;
0360 
0361     spin_lock(&chip->lock);
0362 
0363     block_size = frames_to_bytes(runtime, runtime->period_size);
0364     status = ssc_readl(chip->ssc->regs, IMR);
0365 
0366     if (status & SSC_BIT(IMR_ENDTX)) {
0367         chip->period++;
0368         if (chip->period == runtime->periods)
0369             chip->period = 0;
0370         next_period = chip->period + 1;
0371         if (next_period == runtime->periods)
0372             next_period = 0;
0373 
0374         offset = block_size * next_period;
0375 
0376         ssc_writel(chip->ssc->regs, PDC_TNPR,
0377                 (long)runtime->dma_addr + offset);
0378         ssc_writel(chip->ssc->regs, PDC_TNCR,
0379                 runtime->period_size * runtime->channels);
0380         retval = IRQ_HANDLED;
0381     }
0382 
0383     ssc_readl(chip->ssc->regs, IMR);
0384     spin_unlock(&chip->lock);
0385 
0386     if (status & SSC_BIT(IMR_ENDTX))
0387         snd_pcm_period_elapsed(chip->substream);
0388 
0389     return retval;
0390 }
0391 
0392 /*
0393  * Mixer functions.
0394  */
0395 static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol,
0396                  struct snd_ctl_elem_value *ucontrol)
0397 {
0398     struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
0399     int reg = kcontrol->private_value & 0xff;
0400     int shift = (kcontrol->private_value >> 8) & 0xff;
0401     int mask = (kcontrol->private_value >> 16) & 0xff;
0402     int invert = (kcontrol->private_value >> 24) & 0xff;
0403 
0404     mutex_lock(&chip->mixer_lock);
0405 
0406     ucontrol->value.integer.value[0] =
0407         (chip->reg_image[reg] >> shift) & mask;
0408 
0409     if (invert)
0410         ucontrol->value.integer.value[0] =
0411             mask - ucontrol->value.integer.value[0];
0412 
0413     mutex_unlock(&chip->mixer_lock);
0414 
0415     return 0;
0416 }
0417 
0418 static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol,
0419                  struct snd_ctl_elem_value *ucontrol)
0420 {
0421     struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
0422     int reg = kcontrol->private_value & 0xff;
0423     int shift = (kcontrol->private_value >> 8) & 0xff;
0424     int mask = (kcontrol->private_value >> 16) & 0xff;
0425     int invert = (kcontrol->private_value >> 24) & 0xff;
0426     int change, retval;
0427     unsigned short val;
0428 
0429     val = (ucontrol->value.integer.value[0] & mask);
0430     if (invert)
0431         val = mask - val;
0432     val <<= shift;
0433 
0434     mutex_lock(&chip->mixer_lock);
0435 
0436     val = (chip->reg_image[reg] & ~(mask << shift)) | val;
0437     change = val != chip->reg_image[reg];
0438     retval = snd_at73c213_write_reg(chip, reg, val);
0439 
0440     mutex_unlock(&chip->mixer_lock);
0441 
0442     if (retval)
0443         return retval;
0444 
0445     return change;
0446 }
0447 
0448 static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol,
0449                   struct snd_ctl_elem_info *uinfo)
0450 {
0451     int mask = (kcontrol->private_value >> 24) & 0xff;
0452 
0453     if (mask == 1)
0454         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
0455     else
0456         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0457 
0458     uinfo->count = 2;
0459     uinfo->value.integer.min = 0;
0460     uinfo->value.integer.max = mask;
0461 
0462     return 0;
0463 }
0464 
0465 static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol,
0466                  struct snd_ctl_elem_value *ucontrol)
0467 {
0468     struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
0469     int left_reg = kcontrol->private_value & 0xff;
0470     int right_reg = (kcontrol->private_value >> 8) & 0xff;
0471     int shift_left = (kcontrol->private_value >> 16) & 0x07;
0472     int shift_right = (kcontrol->private_value >> 19) & 0x07;
0473     int mask = (kcontrol->private_value >> 24) & 0xff;
0474     int invert = (kcontrol->private_value >> 22) & 1;
0475 
0476     mutex_lock(&chip->mixer_lock);
0477 
0478     ucontrol->value.integer.value[0] =
0479         (chip->reg_image[left_reg] >> shift_left) & mask;
0480     ucontrol->value.integer.value[1] =
0481         (chip->reg_image[right_reg] >> shift_right) & mask;
0482 
0483     if (invert) {
0484         ucontrol->value.integer.value[0] =
0485             mask - ucontrol->value.integer.value[0];
0486         ucontrol->value.integer.value[1] =
0487             mask - ucontrol->value.integer.value[1];
0488     }
0489 
0490     mutex_unlock(&chip->mixer_lock);
0491 
0492     return 0;
0493 }
0494 
0495 static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol,
0496                  struct snd_ctl_elem_value *ucontrol)
0497 {
0498     struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
0499     int left_reg = kcontrol->private_value & 0xff;
0500     int right_reg = (kcontrol->private_value >> 8) & 0xff;
0501     int shift_left = (kcontrol->private_value >> 16) & 0x07;
0502     int shift_right = (kcontrol->private_value >> 19) & 0x07;
0503     int mask = (kcontrol->private_value >> 24) & 0xff;
0504     int invert = (kcontrol->private_value >> 22) & 1;
0505     int change, retval;
0506     unsigned short val1, val2;
0507 
0508     val1 = ucontrol->value.integer.value[0] & mask;
0509     val2 = ucontrol->value.integer.value[1] & mask;
0510     if (invert) {
0511         val1 = mask - val1;
0512         val2 = mask - val2;
0513     }
0514     val1 <<= shift_left;
0515     val2 <<= shift_right;
0516 
0517     mutex_lock(&chip->mixer_lock);
0518 
0519     val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1;
0520     val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2;
0521     change = val1 != chip->reg_image[left_reg]
0522         || val2 != chip->reg_image[right_reg];
0523     retval = snd_at73c213_write_reg(chip, left_reg, val1);
0524     if (retval) {
0525         mutex_unlock(&chip->mixer_lock);
0526         goto out;
0527     }
0528     retval = snd_at73c213_write_reg(chip, right_reg, val2);
0529     if (retval) {
0530         mutex_unlock(&chip->mixer_lock);
0531         goto out;
0532     }
0533 
0534     mutex_unlock(&chip->mixer_lock);
0535 
0536     return change;
0537 
0538 out:
0539     return retval;
0540 }
0541 
0542 #define snd_at73c213_mono_switch_info   snd_ctl_boolean_mono_info
0543 
0544 static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol,
0545                  struct snd_ctl_elem_value *ucontrol)
0546 {
0547     struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
0548     int reg = kcontrol->private_value & 0xff;
0549     int shift = (kcontrol->private_value >> 8) & 0xff;
0550     int invert = (kcontrol->private_value >> 24) & 0xff;
0551 
0552     mutex_lock(&chip->mixer_lock);
0553 
0554     ucontrol->value.integer.value[0] =
0555         (chip->reg_image[reg] >> shift) & 0x01;
0556 
0557     if (invert)
0558         ucontrol->value.integer.value[0] =
0559             0x01 - ucontrol->value.integer.value[0];
0560 
0561     mutex_unlock(&chip->mixer_lock);
0562 
0563     return 0;
0564 }
0565 
0566 static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol,
0567                  struct snd_ctl_elem_value *ucontrol)
0568 {
0569     struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
0570     int reg = kcontrol->private_value & 0xff;
0571     int shift = (kcontrol->private_value >> 8) & 0xff;
0572     int mask = (kcontrol->private_value >> 16) & 0xff;
0573     int invert = (kcontrol->private_value >> 24) & 0xff;
0574     int change, retval;
0575     unsigned short val;
0576 
0577     if (ucontrol->value.integer.value[0])
0578         val = mask;
0579     else
0580         val = 0;
0581 
0582     if (invert)
0583         val = mask - val;
0584     val <<= shift;
0585 
0586     mutex_lock(&chip->mixer_lock);
0587 
0588     val |= (chip->reg_image[reg] & ~(mask << shift));
0589     change = val != chip->reg_image[reg];
0590 
0591     retval = snd_at73c213_write_reg(chip, reg, val);
0592 
0593     mutex_unlock(&chip->mixer_lock);
0594 
0595     if (retval)
0596         return retval;
0597 
0598     return change;
0599 }
0600 
0601 static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol,
0602                   struct snd_ctl_elem_info *uinfo)
0603 {
0604     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0605     uinfo->count = 1;
0606     uinfo->value.integer.min = 0;
0607     uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1;
0608 
0609     return 0;
0610 }
0611 
0612 static int snd_at73c213_line_capture_volume_info(
0613         struct snd_kcontrol *kcontrol,
0614         struct snd_ctl_elem_info *uinfo)
0615 {
0616     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0617     uinfo->count = 2;
0618     /* When inverted will give values 0x10001 => 0. */
0619     uinfo->value.integer.min = 14;
0620     uinfo->value.integer.max = 31;
0621 
0622     return 0;
0623 }
0624 
0625 static int snd_at73c213_aux_capture_volume_info(
0626         struct snd_kcontrol *kcontrol,
0627         struct snd_ctl_elem_info *uinfo)
0628 {
0629     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0630     uinfo->count = 1;
0631     /* When inverted will give values 0x10001 => 0. */
0632     uinfo->value.integer.min = 14;
0633     uinfo->value.integer.max = 31;
0634 
0635     return 0;
0636 }
0637 
0638 #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert)   \
0639 {                                   \
0640     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,                \
0641     .name = xname,                          \
0642     .index = xindex,                        \
0643     .info = snd_at73c213_mono_switch_info,              \
0644     .get = snd_at73c213_mono_switch_get,                \
0645     .put = snd_at73c213_mono_switch_put,                \
0646     .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
0647 }
0648 
0649 #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
0650 {                                   \
0651     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,                \
0652     .name = xname,                          \
0653     .index = xindex,                        \
0654     .info = snd_at73c213_stereo_info,               \
0655     .get = snd_at73c213_stereo_get,                 \
0656     .put = snd_at73c213_stereo_put,                 \
0657     .private_value = (left_reg | (right_reg << 8)           \
0658             | (shift_left << 16) | (shift_right << 19)  \
0659             | (mask << 24) | (invert << 22))        \
0660 }
0661 
0662 static const struct snd_kcontrol_new snd_at73c213_controls[] = {
0663 AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1),
0664 AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1),
0665 AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1),
0666 AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1),
0667 AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV,
0668              0x01, 0),
0669 {
0670     .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
0671     .name   = "PA Playback Volume",
0672     .index  = 0,
0673     .info   = snd_at73c213_pa_volume_info,
0674     .get    = snd_at73c213_mono_get,
0675     .put    = snd_at73c213_mono_put,
0676     .private_value  = PA_CTRL | (PA_CTRL_APAGAIN << 8) | \
0677         (0x0f << 16) | (1 << 24),
0678 },
0679 AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP,
0680              0x01, 1),
0681 AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0),
0682 {
0683     .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
0684     .name   = "Aux Capture Volume",
0685     .index  = 0,
0686     .info   = snd_at73c213_aux_capture_volume_info,
0687     .get    = snd_at73c213_mono_get,
0688     .put    = snd_at73c213_mono_put,
0689     .private_value  = DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24),
0690 },
0691 AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN,
0692              0x01, 0),
0693 {
0694     .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
0695     .name   = "Line Capture Volume",
0696     .index  = 0,
0697     .info   = snd_at73c213_line_capture_volume_info,
0698     .get    = snd_at73c213_stereo_get,
0699     .put    = snd_at73c213_stereo_put,
0700     .private_value  = DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19)
0701         | (0x1f << 24) | (1 << 22),
0702 },
0703 AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0),
0704 };
0705 
0706 static int snd_at73c213_mixer(struct snd_at73c213 *chip)
0707 {
0708     struct snd_card *card;
0709     int errval, idx;
0710 
0711     if (chip == NULL || chip->pcm == NULL)
0712         return -EINVAL;
0713 
0714     card = chip->card;
0715 
0716     strcpy(card->mixername, chip->pcm->name);
0717 
0718     for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) {
0719         errval = snd_ctl_add(card,
0720                 snd_ctl_new1(&snd_at73c213_controls[idx],
0721                     chip));
0722         if (errval < 0)
0723             goto cleanup;
0724     }
0725 
0726     return 0;
0727 
0728 cleanup:
0729     for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) {
0730         struct snd_kcontrol *kctl;
0731         kctl = snd_ctl_find_numid(card, idx);
0732         if (kctl)
0733             snd_ctl_remove(card, kctl);
0734     }
0735     return errval;
0736 }
0737 
0738 /*
0739  * Device functions
0740  */
0741 static int snd_at73c213_ssc_init(struct snd_at73c213 *chip)
0742 {
0743     /*
0744      * Continuous clock output.
0745      * Starts on falling TF.
0746      * Delay 1 cycle (1 bit).
0747      * Periode is 16 bit (16 - 1).
0748      */
0749     ssc_writel(chip->ssc->regs, TCMR,
0750             SSC_BF(TCMR_CKO, 1)
0751             | SSC_BF(TCMR_START, 4)
0752             | SSC_BF(TCMR_STTDLY, 1)
0753             | SSC_BF(TCMR_PERIOD, 16 - 1));
0754     /*
0755      * Data length is 16 bit (16 - 1).
0756      * Transmit MSB first.
0757      * Transmit 2 words each transfer.
0758      * Frame sync length is 16 bit (16 - 1).
0759      * Frame starts on negative pulse.
0760      */
0761     ssc_writel(chip->ssc->regs, TFMR,
0762             SSC_BF(TFMR_DATLEN, 16 - 1)
0763             | SSC_BIT(TFMR_MSBF)
0764             | SSC_BF(TFMR_DATNB, 1)
0765             | SSC_BF(TFMR_FSLEN, 16 - 1)
0766             | SSC_BF(TFMR_FSOS, 1));
0767 
0768     return 0;
0769 }
0770 
0771 static int snd_at73c213_chip_init(struct snd_at73c213 *chip)
0772 {
0773     int retval;
0774     unsigned char dac_ctrl = 0;
0775 
0776     retval = snd_at73c213_set_bitrate(chip);
0777     if (retval)
0778         goto out;
0779 
0780     /* Enable DAC master clock. */
0781     retval = clk_enable(chip->board->dac_clk);
0782     if (retval)
0783         goto out;
0784 
0785     /* Initialize at73c213 on SPI bus. */
0786     retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04);
0787     if (retval)
0788         goto out_clk;
0789     msleep(1);
0790     retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03);
0791     if (retval)
0792         goto out_clk;
0793 
0794     /* Precharge everything. */
0795     retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff);
0796     if (retval)
0797         goto out_clk;
0798     retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH));
0799     if (retval)
0800         goto out_clk;
0801     retval = snd_at73c213_write_reg(chip, DAC_CTRL,
0802             (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR));
0803     if (retval)
0804         goto out_clk;
0805 
0806     msleep(50);
0807 
0808     /* Stop precharging PA. */
0809     retval = snd_at73c213_write_reg(chip, PA_CTRL,
0810             (1<<PA_CTRL_APALP) | 0x0f);
0811     if (retval)
0812         goto out_clk;
0813 
0814     msleep(450);
0815 
0816     /* Stop precharging DAC, turn on master power. */
0817     retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR));
0818     if (retval)
0819         goto out_clk;
0820 
0821     msleep(1);
0822 
0823     /* Turn on DAC. */
0824     dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR)
0825         | (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR);
0826 
0827     retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl);
0828     if (retval)
0829         goto out_clk;
0830 
0831     /* Mute sound. */
0832     retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
0833     if (retval)
0834         goto out_clk;
0835     retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
0836     if (retval)
0837         goto out_clk;
0838     retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
0839     if (retval)
0840         goto out_clk;
0841     retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
0842     if (retval)
0843         goto out_clk;
0844     retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
0845     if (retval)
0846         goto out_clk;
0847     retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
0848     if (retval)
0849         goto out_clk;
0850     retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
0851     if (retval)
0852         goto out_clk;
0853 
0854     /* Enable I2S device, i.e. clock output. */
0855     ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
0856 
0857     goto out;
0858 
0859 out_clk:
0860     clk_disable(chip->board->dac_clk);
0861 out:
0862     return retval;
0863 }
0864 
0865 static int snd_at73c213_dev_free(struct snd_device *device)
0866 {
0867     struct snd_at73c213 *chip = device->device_data;
0868 
0869     ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
0870     if (chip->irq >= 0) {
0871         free_irq(chip->irq, chip);
0872         chip->irq = -1;
0873     }
0874 
0875     return 0;
0876 }
0877 
0878 static int snd_at73c213_dev_init(struct snd_card *card,
0879                  struct spi_device *spi)
0880 {
0881     static const struct snd_device_ops ops = {
0882         .dev_free   = snd_at73c213_dev_free,
0883     };
0884     struct snd_at73c213 *chip = get_chip(card);
0885     int irq, retval;
0886 
0887     irq = chip->ssc->irq;
0888     if (irq < 0)
0889         return irq;
0890 
0891     spin_lock_init(&chip->lock);
0892     mutex_init(&chip->mixer_lock);
0893     chip->card = card;
0894     chip->irq = -1;
0895 
0896     retval = clk_enable(chip->ssc->clk);
0897     if (retval)
0898         return retval;
0899 
0900     retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip);
0901     if (retval) {
0902         dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq);
0903         goto out;
0904     }
0905     chip->irq = irq;
0906 
0907     memcpy(&chip->reg_image, &snd_at73c213_original_image,
0908             sizeof(snd_at73c213_original_image));
0909 
0910     retval = snd_at73c213_ssc_init(chip);
0911     if (retval)
0912         goto out_irq;
0913 
0914     retval = snd_at73c213_chip_init(chip);
0915     if (retval)
0916         goto out_irq;
0917 
0918     retval = snd_at73c213_pcm_new(chip, 0);
0919     if (retval)
0920         goto out_irq;
0921 
0922     retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
0923     if (retval)
0924         goto out_irq;
0925 
0926     retval = snd_at73c213_mixer(chip);
0927     if (retval)
0928         goto out_snd_dev;
0929 
0930     goto out;
0931 
0932 out_snd_dev:
0933     snd_device_free(card, chip);
0934 out_irq:
0935     free_irq(chip->irq, chip);
0936     chip->irq = -1;
0937 out:
0938     clk_disable(chip->ssc->clk);
0939 
0940     return retval;
0941 }
0942 
0943 static int snd_at73c213_probe(struct spi_device *spi)
0944 {
0945     struct snd_card         *card;
0946     struct snd_at73c213     *chip;
0947     struct at73c213_board_info  *board;
0948     int             retval;
0949     char                id[16];
0950 
0951     board = spi->dev.platform_data;
0952     if (!board) {
0953         dev_dbg(&spi->dev, "no platform_data\n");
0954         return -ENXIO;
0955     }
0956 
0957     if (!board->dac_clk) {
0958         dev_dbg(&spi->dev, "no DAC clk\n");
0959         return -ENXIO;
0960     }
0961 
0962     if (IS_ERR(board->dac_clk)) {
0963         dev_dbg(&spi->dev, "no DAC clk\n");
0964         return PTR_ERR(board->dac_clk);
0965     }
0966 
0967     /* Allocate "card" using some unused identifiers. */
0968     snprintf(id, sizeof id, "at73c213_%d", board->ssc_id);
0969     retval = snd_card_new(&spi->dev, -1, id, THIS_MODULE,
0970                   sizeof(struct snd_at73c213), &card);
0971     if (retval < 0)
0972         goto out;
0973 
0974     chip = card->private_data;
0975     chip->spi = spi;
0976     chip->board = board;
0977 
0978     chip->ssc = ssc_request(board->ssc_id);
0979     if (IS_ERR(chip->ssc)) {
0980         dev_dbg(&spi->dev, "could not get ssc%d device\n",
0981                 board->ssc_id);
0982         retval = PTR_ERR(chip->ssc);
0983         goto out_card;
0984     }
0985 
0986     retval = snd_at73c213_dev_init(card, spi);
0987     if (retval)
0988         goto out_ssc;
0989 
0990     strcpy(card->driver, "at73c213");
0991     strcpy(card->shortname, board->shortname);
0992     sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq);
0993 
0994     retval = snd_card_register(card);
0995     if (retval)
0996         goto out_ssc;
0997 
0998     dev_set_drvdata(&spi->dev, card);
0999 
1000     goto out;
1001 
1002 out_ssc:
1003     ssc_free(chip->ssc);
1004 out_card:
1005     snd_card_free(card);
1006 out:
1007     return retval;
1008 }
1009 
1010 static void snd_at73c213_remove(struct spi_device *spi)
1011 {
1012     struct snd_card *card = dev_get_drvdata(&spi->dev);
1013     struct snd_at73c213 *chip = card->private_data;
1014     int retval;
1015 
1016     /* Stop playback. */
1017     retval = clk_enable(chip->ssc->clk);
1018     if (retval)
1019         goto out;
1020     ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1021     clk_disable(chip->ssc->clk);
1022 
1023     /* Mute sound. */
1024     retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
1025     if (retval)
1026         goto out;
1027     retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
1028     if (retval)
1029         goto out;
1030     retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
1031     if (retval)
1032         goto out;
1033     retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
1034     if (retval)
1035         goto out;
1036     retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
1037     if (retval)
1038         goto out;
1039     retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
1040     if (retval)
1041         goto out;
1042     retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
1043     if (retval)
1044         goto out;
1045 
1046     /* Turn off PA. */
1047     retval = snd_at73c213_write_reg(chip, PA_CTRL,
1048                     chip->reg_image[PA_CTRL] | 0x0f);
1049     if (retval)
1050         goto out;
1051     msleep(10);
1052     retval = snd_at73c213_write_reg(chip, PA_CTRL,
1053                     (1 << PA_CTRL_APALP) | 0x0f);
1054     if (retval)
1055         goto out;
1056 
1057     /* Turn off external DAC. */
1058     retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c);
1059     if (retval)
1060         goto out;
1061     msleep(2);
1062     retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00);
1063     if (retval)
1064         goto out;
1065 
1066     /* Turn off master power. */
1067     retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00);
1068     if (retval)
1069         goto out;
1070 
1071 out:
1072     /* Stop DAC master clock. */
1073     clk_disable(chip->board->dac_clk);
1074 
1075     ssc_free(chip->ssc);
1076     snd_card_free(card);
1077 }
1078 
1079 #ifdef CONFIG_PM_SLEEP
1080 
1081 static int snd_at73c213_suspend(struct device *dev)
1082 {
1083     struct snd_card *card = dev_get_drvdata(dev);
1084     struct snd_at73c213 *chip = card->private_data;
1085 
1086     ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
1087     clk_disable(chip->ssc->clk);
1088     clk_disable(chip->board->dac_clk);
1089 
1090     return 0;
1091 }
1092 
1093 static int snd_at73c213_resume(struct device *dev)
1094 {
1095     struct snd_card *card = dev_get_drvdata(dev);
1096     struct snd_at73c213 *chip = card->private_data;
1097     int retval;
1098 
1099     retval = clk_enable(chip->board->dac_clk);
1100     if (retval)
1101         return retval;
1102     retval = clk_enable(chip->ssc->clk);
1103     if (retval) {
1104         clk_disable(chip->board->dac_clk);
1105         return retval;
1106     }
1107     ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
1108 
1109     return 0;
1110 }
1111 
1112 static SIMPLE_DEV_PM_OPS(at73c213_pm_ops, snd_at73c213_suspend,
1113         snd_at73c213_resume);
1114 #define AT73C213_PM_OPS (&at73c213_pm_ops)
1115 
1116 #else
1117 #define AT73C213_PM_OPS NULL
1118 #endif
1119 
1120 static struct spi_driver at73c213_driver = {
1121     .driver     = {
1122         .name   = "at73c213",
1123         .pm = AT73C213_PM_OPS,
1124     },
1125     .probe      = snd_at73c213_probe,
1126     .remove     = snd_at73c213_remove,
1127 };
1128 
1129 module_spi_driver(at73c213_driver);
1130 
1131 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
1132 MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
1133 MODULE_LICENSE("GPL");