~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/atmel/atmel_ssc_dai.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * atmel_ssc_dai.c  --  ALSA SoC ATMEL SSC Audio Layer Platform driver
  3  *
  4  * Copyright (C) 2005 SAN People
  5  * Copyright (C) 2008 Atmel
  6  *
  7  * Author: Sedji Gaouaou <sedji.gaouaou@atmel.com>
  8  *         ATMEL CORP.
  9  *
 10  * Based on at91-ssc.c by
 11  * Frank Mandarino <fmandarino@endrelia.com>
 12  * Based on pxa2xx Platform drivers by
 13  * Liam Girdwood <lrg@slimlogic.co.uk>
 14  *
 15  * This program is free software; you can redistribute it and/or modify
 16  * it under the terms of the GNU General Public License as published by
 17  * the Free Software Foundation; either version 2 of the License, or
 18  * (at your option) any later version.
 19  *
 20  * This program is distributed in the hope that it will be useful,
 21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 23  * GNU General Public License for more details.
 24  *
 25  * You should have received a copy of the GNU General Public License
 26  * along with this program; if not, write to the Free Software
 27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 28  */
 29 
 30 #include <linux/init.h>
 31 #include <linux/module.h>
 32 #include <linux/interrupt.h>
 33 #include <linux/device.h>
 34 #include <linux/delay.h>
 35 #include <linux/clk.h>
 36 #include <linux/atmel_pdc.h>
 37 
 38 #include <linux/atmel-ssc.h>
 39 #include <sound/core.h>
 40 #include <sound/pcm.h>
 41 #include <sound/pcm_params.h>
 42 #include <sound/initval.h>
 43 #include <sound/soc.h>
 44 
 45 #include "atmel-pcm.h"
 46 #include "atmel_ssc_dai.h"
 47 
 48 
 49 #define NUM_SSC_DEVICES         3
 50 
 51 /*
 52  * SSC PDC registers required by the PCM DMA engine.
 53  */
 54 static struct atmel_pdc_regs pdc_tx_reg = {
 55         .xpr            = ATMEL_PDC_TPR,
 56         .xcr            = ATMEL_PDC_TCR,
 57         .xnpr           = ATMEL_PDC_TNPR,
 58         .xncr           = ATMEL_PDC_TNCR,
 59 };
 60 
 61 static struct atmel_pdc_regs pdc_rx_reg = {
 62         .xpr            = ATMEL_PDC_RPR,
 63         .xcr            = ATMEL_PDC_RCR,
 64         .xnpr           = ATMEL_PDC_RNPR,
 65         .xncr           = ATMEL_PDC_RNCR,
 66 };
 67 
 68 /*
 69  * SSC & PDC status bits for transmit and receive.
 70  */
 71 static struct atmel_ssc_mask ssc_tx_mask = {
 72         .ssc_enable     = SSC_BIT(CR_TXEN),
 73         .ssc_disable    = SSC_BIT(CR_TXDIS),
 74         .ssc_endx       = SSC_BIT(SR_ENDTX),
 75         .ssc_endbuf     = SSC_BIT(SR_TXBUFE),
 76         .ssc_error      = SSC_BIT(SR_OVRUN),
 77         .pdc_enable     = ATMEL_PDC_TXTEN,
 78         .pdc_disable    = ATMEL_PDC_TXTDIS,
 79 };
 80 
 81 static struct atmel_ssc_mask ssc_rx_mask = {
 82         .ssc_enable     = SSC_BIT(CR_RXEN),
 83         .ssc_disable    = SSC_BIT(CR_RXDIS),
 84         .ssc_endx       = SSC_BIT(SR_ENDRX),
 85         .ssc_endbuf     = SSC_BIT(SR_RXBUFF),
 86         .ssc_error      = SSC_BIT(SR_OVRUN),
 87         .pdc_enable     = ATMEL_PDC_RXTEN,
 88         .pdc_disable    = ATMEL_PDC_RXTDIS,
 89 };
 90 
 91 
 92 /*
 93  * DMA parameters.
 94  */
 95 static struct atmel_pcm_dma_params ssc_dma_params[NUM_SSC_DEVICES][2] = {
 96         {{
 97         .name           = "SSC0 PCM out",
 98         .pdc            = &pdc_tx_reg,
 99         .mask           = &ssc_tx_mask,
100         },
101         {
102         .name           = "SSC0 PCM in",
103         .pdc            = &pdc_rx_reg,
104         .mask           = &ssc_rx_mask,
105         } },
106         {{
107         .name           = "SSC1 PCM out",
108         .pdc            = &pdc_tx_reg,
109         .mask           = &ssc_tx_mask,
110         },
111         {
112         .name           = "SSC1 PCM in",
113         .pdc            = &pdc_rx_reg,
114         .mask           = &ssc_rx_mask,
115         } },
116         {{
117         .name           = "SSC2 PCM out",
118         .pdc            = &pdc_tx_reg,
119         .mask           = &ssc_tx_mask,
120         },
121         {
122         .name           = "SSC2 PCM in",
123         .pdc            = &pdc_rx_reg,
124         .mask           = &ssc_rx_mask,
125         } },
126 };
127 
128 
129 static struct atmel_ssc_info ssc_info[NUM_SSC_DEVICES] = {
130         {
131         .name           = "ssc0",
132         .lock           = __SPIN_LOCK_UNLOCKED(ssc_info[0].lock),
133         .dir_mask       = SSC_DIR_MASK_UNUSED,
134         .initialized    = 0,
135         },
136         {
137         .name           = "ssc1",
138         .lock           = __SPIN_LOCK_UNLOCKED(ssc_info[1].lock),
139         .dir_mask       = SSC_DIR_MASK_UNUSED,
140         .initialized    = 0,
141         },
142         {
143         .name           = "ssc2",
144         .lock           = __SPIN_LOCK_UNLOCKED(ssc_info[2].lock),
145         .dir_mask       = SSC_DIR_MASK_UNUSED,
146         .initialized    = 0,
147         },
148 };
149 
150 
151 /*
152  * SSC interrupt handler.  Passes PDC interrupts to the DMA
153  * interrupt handler in the PCM driver.
154  */
155 static irqreturn_t atmel_ssc_interrupt(int irq, void *dev_id)
156 {
157         struct atmel_ssc_info *ssc_p = dev_id;
158         struct atmel_pcm_dma_params *dma_params;
159         u32 ssc_sr;
160         u32 ssc_substream_mask;
161         int i;
162 
163         ssc_sr = (unsigned long)ssc_readl(ssc_p->ssc->regs, SR)
164                         & (unsigned long)ssc_readl(ssc_p->ssc->regs, IMR);
165 
166         /*
167          * Loop through the substreams attached to this SSC.  If
168          * a DMA-related interrupt occurred on that substream, call
169          * the DMA interrupt handler function, if one has been
170          * registered in the dma_params structure by the PCM driver.
171          */
172         for (i = 0; i < ARRAY_SIZE(ssc_p->dma_params); i++) {
173                 dma_params = ssc_p->dma_params[i];
174 
175                 if ((dma_params != NULL) &&
176                         (dma_params->dma_intr_handler != NULL)) {
177                         ssc_substream_mask = (dma_params->mask->ssc_endx |
178                                         dma_params->mask->ssc_endbuf);
179                         if (ssc_sr & ssc_substream_mask) {
180                                 dma_params->dma_intr_handler(ssc_sr,
181                                                 dma_params->
182                                                 substream);
183                         }
184                 }
185         }
186 
187         return IRQ_HANDLED;
188 }
189 
190 
191 /*-------------------------------------------------------------------------*\
192  * DAI functions
193 \*-------------------------------------------------------------------------*/
194 /*
195  * Startup.  Only that one substream allowed in each direction.
196  */
197 static int atmel_ssc_startup(struct snd_pcm_substream *substream,
198                              struct snd_soc_dai *dai)
199 {
200         struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
201         struct atmel_pcm_dma_params *dma_params;
202         int dir, dir_mask;
203 
204         pr_debug("atmel_ssc_startup: SSC_SR=0x%u\n",
205                 ssc_readl(ssc_p->ssc->regs, SR));
206 
207         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
208                 dir = 0;
209                 dir_mask = SSC_DIR_MASK_PLAYBACK;
210         } else {
211                 dir = 1;
212                 dir_mask = SSC_DIR_MASK_CAPTURE;
213         }
214 
215         dma_params = &ssc_dma_params[dai->id][dir];
216         dma_params->ssc = ssc_p->ssc;
217         dma_params->substream = substream;
218 
219         ssc_p->dma_params[dir] = dma_params;
220 
221         snd_soc_dai_set_dma_data(dai, substream, dma_params);
222 
223         spin_lock_irq(&ssc_p->lock);
224         if (ssc_p->dir_mask & dir_mask) {
225                 spin_unlock_irq(&ssc_p->lock);
226                 return -EBUSY;
227         }
228         ssc_p->dir_mask |= dir_mask;
229         spin_unlock_irq(&ssc_p->lock);
230 
231         return 0;
232 }
233 
234 /*
235  * Shutdown.  Clear DMA parameters and shutdown the SSC if there
236  * are no other substreams open.
237  */
238 static void atmel_ssc_shutdown(struct snd_pcm_substream *substream,
239                                struct snd_soc_dai *dai)
240 {
241         struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
242         struct atmel_pcm_dma_params *dma_params;
243         int dir, dir_mask;
244 
245         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
246                 dir = 0;
247         else
248                 dir = 1;
249 
250         dma_params = ssc_p->dma_params[dir];
251 
252         if (dma_params != NULL) {
253                 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
254                 pr_debug("atmel_ssc_shutdown: %s disabled SSC_SR=0x%08x\n",
255                         (dir ? "receive" : "transmit"),
256                         ssc_readl(ssc_p->ssc->regs, SR));
257 
258                 dma_params->ssc = NULL;
259                 dma_params->substream = NULL;
260                 ssc_p->dma_params[dir] = NULL;
261         }
262 
263         dir_mask = 1 << dir;
264 
265         spin_lock_irq(&ssc_p->lock);
266         ssc_p->dir_mask &= ~dir_mask;
267         if (!ssc_p->dir_mask) {
268                 if (ssc_p->initialized) {
269                         /* Shutdown the SSC clock. */
270                         pr_debug("atmel_ssc_dau: Stopping clock\n");
271                         clk_disable(ssc_p->ssc->clk);
272 
273                         free_irq(ssc_p->ssc->irq, ssc_p);
274                         ssc_p->initialized = 0;
275                 }
276 
277                 /* Reset the SSC */
278                 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
279                 /* Clear the SSC dividers */
280                 ssc_p->cmr_div = ssc_p->tcmr_period = ssc_p->rcmr_period = 0;
281         }
282         spin_unlock_irq(&ssc_p->lock);
283 }
284 
285 
286 /*
287  * Record the DAI format for use in hw_params().
288  */
289 static int atmel_ssc_set_dai_fmt(struct snd_soc_dai *cpu_dai,
290                 unsigned int fmt)
291 {
292         struct atmel_ssc_info *ssc_p = &ssc_info[cpu_dai->id];
293 
294         ssc_p->daifmt = fmt;
295         return 0;
296 }
297 
298 /*
299  * Record SSC clock dividers for use in hw_params().
300  */
301 static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
302         int div_id, int div)
303 {
304         struct atmel_ssc_info *ssc_p = &ssc_info[cpu_dai->id];
305 
306         switch (div_id) {
307         case ATMEL_SSC_CMR_DIV:
308                 /*
309                  * The same master clock divider is used for both
310                  * transmit and receive, so if a value has already
311                  * been set, it must match this value.
312                  */
313                 if (ssc_p->cmr_div == 0)
314                         ssc_p->cmr_div = div;
315                 else
316                         if (div != ssc_p->cmr_div)
317                                 return -EBUSY;
318                 break;
319 
320         case ATMEL_SSC_TCMR_PERIOD:
321                 ssc_p->tcmr_period = div;
322                 break;
323 
324         case ATMEL_SSC_RCMR_PERIOD:
325                 ssc_p->rcmr_period = div;
326                 break;
327 
328         default:
329                 return -EINVAL;
330         }
331 
332         return 0;
333 }
334 
335 /*
336  * Configure the SSC.
337  */
338 static int atmel_ssc_hw_params(struct snd_pcm_substream *substream,
339         struct snd_pcm_hw_params *params,
340         struct snd_soc_dai *dai)
341 {
342         int id = dai->id;
343         struct atmel_ssc_info *ssc_p = &ssc_info[id];
344         struct ssc_device *ssc = ssc_p->ssc;
345         struct atmel_pcm_dma_params *dma_params;
346         int dir, channels, bits;
347         u32 tfmr, rfmr, tcmr, rcmr;
348         int ret;
349         int fslen, fslen_ext;
350 
351         /*
352          * Currently, there is only one set of dma params for
353          * each direction.  If more are added, this code will
354          * have to be changed to select the proper set.
355          */
356         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
357                 dir = 0;
358         else
359                 dir = 1;
360 
361         dma_params = ssc_p->dma_params[dir];
362 
363         channels = params_channels(params);
364 
365         /*
366          * Determine sample size in bits and the PDC increment.
367          */
368         switch (params_format(params)) {
369         case SNDRV_PCM_FORMAT_S8:
370                 bits = 8;
371                 dma_params->pdc_xfer_size = 1;
372                 break;
373         case SNDRV_PCM_FORMAT_S16_LE:
374                 bits = 16;
375                 dma_params->pdc_xfer_size = 2;
376                 break;
377         case SNDRV_PCM_FORMAT_S24_LE:
378                 bits = 24;
379                 dma_params->pdc_xfer_size = 4;
380                 break;
381         case SNDRV_PCM_FORMAT_S32_LE:
382                 bits = 32;
383                 dma_params->pdc_xfer_size = 4;
384                 break;
385         default:
386                 printk(KERN_WARNING "atmel_ssc_dai: unsupported PCM format");
387                 return -EINVAL;
388         }
389 
390         /*
391          * Compute SSC register settings.
392          */
393         switch (ssc_p->daifmt
394                 & (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
395 
396         case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
397                 /*
398                  * I2S format, SSC provides BCLK and LRC clocks.
399                  *
400                  * The SSC transmit and receive clocks are generated
401                  * from the MCK divider, and the BCLK signal
402                  * is output on the SSC TK line.
403                  */
404 
405                 if (bits > 16 && !ssc->pdata->has_fslen_ext) {
406                         dev_err(dai->dev,
407                                 "sample size %d is too large for SSC device\n",
408                                 bits);
409                         return -EINVAL;
410                 }
411 
412                 fslen_ext = (bits - 1) / 16;
413                 fslen = (bits - 1) % 16;
414 
415                 rcmr =    SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
416                         | SSC_BF(RCMR_STTDLY, START_DELAY)
417                         | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
418                         | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
419                         | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
420                         | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
421 
422                 rfmr =    SSC_BF(RFMR_FSLEN_EXT, fslen_ext)
423                         | SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
424                         | SSC_BF(RFMR_FSOS, SSC_FSOS_NEGATIVE)
425                         | SSC_BF(RFMR_FSLEN, fslen)
426                         | SSC_BF(RFMR_DATNB, (channels - 1))
427                         | SSC_BIT(RFMR_MSBF)
428                         | SSC_BF(RFMR_LOOP, 0)
429                         | SSC_BF(RFMR_DATLEN, (bits - 1));
430 
431                 tcmr =    SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
432                         | SSC_BF(TCMR_STTDLY, START_DELAY)
433                         | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
434                         | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
435                         | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
436                         | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
437 
438                 tfmr =    SSC_BF(TFMR_FSLEN_EXT, fslen_ext)
439                         | SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
440                         | SSC_BF(TFMR_FSDEN, 0)
441                         | SSC_BF(TFMR_FSOS, SSC_FSOS_NEGATIVE)
442                         | SSC_BF(TFMR_FSLEN, fslen)
443                         | SSC_BF(TFMR_DATNB, (channels - 1))
444                         | SSC_BIT(TFMR_MSBF)
445                         | SSC_BF(TFMR_DATDEF, 0)
446                         | SSC_BF(TFMR_DATLEN, (bits - 1));
447                 break;
448 
449         case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
450                 /*
451                  * I2S format, CODEC supplies BCLK and LRC clocks.
452                  *
453                  * The SSC transmit clock is obtained from the BCLK signal on
454                  * on the TK line, and the SSC receive clock is
455                  * generated from the transmit clock.
456                  */
457                 rcmr =    SSC_BF(RCMR_PERIOD, 0)
458                         | SSC_BF(RCMR_STTDLY, START_DELAY)
459                         | SSC_BF(RCMR_START, SSC_START_FALLING_RF)
460                         | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
461                         | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
462                         | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
463                                            SSC_CKS_PIN : SSC_CKS_CLOCK);
464 
465                 rfmr =    SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
466                         | SSC_BF(RFMR_FSOS, SSC_FSOS_NONE)
467                         | SSC_BF(RFMR_FSLEN, 0)
468                         | SSC_BF(RFMR_DATNB, (channels - 1))
469                         | SSC_BIT(RFMR_MSBF)
470                         | SSC_BF(RFMR_LOOP, 0)
471                         | SSC_BF(RFMR_DATLEN, (bits - 1));
472 
473                 tcmr =    SSC_BF(TCMR_PERIOD, 0)
474                         | SSC_BF(TCMR_STTDLY, START_DELAY)
475                         | SSC_BF(TCMR_START, SSC_START_FALLING_RF)
476                         | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
477                         | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
478                         | SSC_BF(TCMR_CKS, ssc->clk_from_rk_pin ?
479                                            SSC_CKS_CLOCK : SSC_CKS_PIN);
480 
481                 tfmr =    SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
482                         | SSC_BF(TFMR_FSDEN, 0)
483                         | SSC_BF(TFMR_FSOS, SSC_FSOS_NONE)
484                         | SSC_BF(TFMR_FSLEN, 0)
485                         | SSC_BF(TFMR_DATNB, (channels - 1))
486                         | SSC_BIT(TFMR_MSBF)
487                         | SSC_BF(TFMR_DATDEF, 0)
488                         | SSC_BF(TFMR_DATLEN, (bits - 1));
489                 break;
490 
491         case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
492                 /*
493                  * DSP/PCM Mode A format, SSC provides BCLK and LRC clocks.
494                  *
495                  * The SSC transmit and receive clocks are generated from the
496                  * MCK divider, and the BCLK signal is output
497                  * on the SSC TK line.
498                  */
499                 rcmr =    SSC_BF(RCMR_PERIOD, ssc_p->rcmr_period)
500                         | SSC_BF(RCMR_STTDLY, 1)
501                         | SSC_BF(RCMR_START, SSC_START_RISING_RF)
502                         | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
503                         | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
504                         | SSC_BF(RCMR_CKS, SSC_CKS_DIV);
505 
506                 rfmr =    SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
507                         | SSC_BF(RFMR_FSOS, SSC_FSOS_POSITIVE)
508                         | SSC_BF(RFMR_FSLEN, 0)
509                         | SSC_BF(RFMR_DATNB, (channels - 1))
510                         | SSC_BIT(RFMR_MSBF)
511                         | SSC_BF(RFMR_LOOP, 0)
512                         | SSC_BF(RFMR_DATLEN, (bits - 1));
513 
514                 tcmr =    SSC_BF(TCMR_PERIOD, ssc_p->tcmr_period)
515                         | SSC_BF(TCMR_STTDLY, 1)
516                         | SSC_BF(TCMR_START, SSC_START_RISING_RF)
517                         | SSC_BF(TCMR_CKI, SSC_CKI_RISING)
518                         | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS)
519                         | SSC_BF(TCMR_CKS, SSC_CKS_DIV);
520 
521                 tfmr =    SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
522                         | SSC_BF(TFMR_FSDEN, 0)
523                         | SSC_BF(TFMR_FSOS, SSC_FSOS_POSITIVE)
524                         | SSC_BF(TFMR_FSLEN, 0)
525                         | SSC_BF(TFMR_DATNB, (channels - 1))
526                         | SSC_BIT(TFMR_MSBF)
527                         | SSC_BF(TFMR_DATDEF, 0)
528                         | SSC_BF(TFMR_DATLEN, (bits - 1));
529                 break;
530 
531         case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
532                 /*
533                  * DSP/PCM Mode A format, CODEC supplies BCLK and LRC clocks.
534                  *
535                  * The SSC transmit clock is obtained from the BCLK signal on
536                  * on the TK line, and the SSC receive clock is
537                  * generated from the transmit clock.
538                  *
539                  * Data is transferred on first BCLK after LRC pulse rising
540                  * edge.If stereo, the right channel data is contiguous with
541                  * the left channel data.
542                  */
543                 rcmr =    SSC_BF(RCMR_PERIOD, 0)
544                         | SSC_BF(RCMR_STTDLY, START_DELAY)
545                         | SSC_BF(RCMR_START, SSC_START_RISING_RF)
546                         | SSC_BF(RCMR_CKI, SSC_CKI_RISING)
547                         | SSC_BF(RCMR_CKO, SSC_CKO_NONE)
548                         | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
549                                            SSC_CKS_PIN : SSC_CKS_CLOCK);
550 
551                 rfmr =    SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
552                         | SSC_BF(RFMR_FSOS, SSC_FSOS_NONE)
553                         | SSC_BF(RFMR_FSLEN, 0)
554                         | SSC_BF(RFMR_DATNB, (channels - 1))
555                         | SSC_BIT(RFMR_MSBF)
556                         | SSC_BF(RFMR_LOOP, 0)
557                         | SSC_BF(RFMR_DATLEN, (bits - 1));
558 
559                 tcmr =    SSC_BF(TCMR_PERIOD, 0)
560                         | SSC_BF(TCMR_STTDLY, START_DELAY)
561                         | SSC_BF(TCMR_START, SSC_START_RISING_RF)
562                         | SSC_BF(TCMR_CKI, SSC_CKI_FALLING)
563                         | SSC_BF(TCMR_CKO, SSC_CKO_NONE)
564                         | SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
565                                            SSC_CKS_CLOCK : SSC_CKS_PIN);
566 
567                 tfmr =    SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
568                         | SSC_BF(TFMR_FSDEN, 0)
569                         | SSC_BF(TFMR_FSOS, SSC_FSOS_NONE)
570                         | SSC_BF(TFMR_FSLEN, 0)
571                         | SSC_BF(TFMR_DATNB, (channels - 1))
572                         | SSC_BIT(TFMR_MSBF)
573                         | SSC_BF(TFMR_DATDEF, 0)
574                         | SSC_BF(TFMR_DATLEN, (bits - 1));
575                 break;
576 
577         default:
578                 printk(KERN_WARNING "atmel_ssc_dai: unsupported DAI format 0x%x\n",
579                         ssc_p->daifmt);
580                 return -EINVAL;
581         }
582         pr_debug("atmel_ssc_hw_params: "
583                         "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
584                         rcmr, rfmr, tcmr, tfmr);
585 
586         if (!ssc_p->initialized) {
587 
588                 /* Enable PMC peripheral clock for this SSC */
589                 pr_debug("atmel_ssc_dai: Starting clock\n");
590                 clk_enable(ssc_p->ssc->clk);
591 
592                 /* Reset the SSC and its PDC registers */
593                 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
594 
595                 ssc_writel(ssc_p->ssc->regs, PDC_RPR, 0);
596                 ssc_writel(ssc_p->ssc->regs, PDC_RCR, 0);
597                 ssc_writel(ssc_p->ssc->regs, PDC_RNPR, 0);
598                 ssc_writel(ssc_p->ssc->regs, PDC_RNCR, 0);
599 
600                 ssc_writel(ssc_p->ssc->regs, PDC_TPR, 0);
601                 ssc_writel(ssc_p->ssc->regs, PDC_TCR, 0);
602                 ssc_writel(ssc_p->ssc->regs, PDC_TNPR, 0);
603                 ssc_writel(ssc_p->ssc->regs, PDC_TNCR, 0);
604 
605                 ret = request_irq(ssc_p->ssc->irq, atmel_ssc_interrupt, 0,
606                                 ssc_p->name, ssc_p);
607                 if (ret < 0) {
608                         printk(KERN_WARNING
609                                         "atmel_ssc_dai: request_irq failure\n");
610                         pr_debug("Atmel_ssc_dai: Stoping clock\n");
611                         clk_disable(ssc_p->ssc->clk);
612                         return ret;
613                 }
614 
615                 ssc_p->initialized = 1;
616         }
617 
618         /* set SSC clock mode register */
619         ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->cmr_div);
620 
621         /* set receive clock mode and format */
622         ssc_writel(ssc_p->ssc->regs, RCMR, rcmr);
623         ssc_writel(ssc_p->ssc->regs, RFMR, rfmr);
624 
625         /* set transmit clock mode and format */
626         ssc_writel(ssc_p->ssc->regs, TCMR, tcmr);
627         ssc_writel(ssc_p->ssc->regs, TFMR, tfmr);
628 
629         pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
630         return 0;
631 }
632 
633 
634 static int atmel_ssc_prepare(struct snd_pcm_substream *substream,
635                              struct snd_soc_dai *dai)
636 {
637         struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
638         struct atmel_pcm_dma_params *dma_params;
639         int dir;
640 
641         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
642                 dir = 0;
643         else
644                 dir = 1;
645 
646         dma_params = ssc_p->dma_params[dir];
647 
648         ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
649         ssc_writel(ssc_p->ssc->regs, IDR, dma_params->mask->ssc_error);
650 
651         pr_debug("%s enabled SSC_SR=0x%08x\n",
652                         dir ? "receive" : "transmit",
653                         ssc_readl(ssc_p->ssc->regs, SR));
654         return 0;
655 }
656 
657 static int atmel_ssc_trigger(struct snd_pcm_substream *substream,
658                              int cmd, struct snd_soc_dai *dai)
659 {
660         struct atmel_ssc_info *ssc_p = &ssc_info[dai->id];
661         struct atmel_pcm_dma_params *dma_params;
662         int dir;
663 
664         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
665                 dir = 0;
666         else
667                 dir = 1;
668 
669         dma_params = ssc_p->dma_params[dir];
670 
671         switch (cmd) {
672         case SNDRV_PCM_TRIGGER_START:
673         case SNDRV_PCM_TRIGGER_RESUME:
674         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
675                 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_enable);
676                 break;
677         default:
678                 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
679                 break;
680         }
681 
682         return 0;
683 }
684 
685 #ifdef CONFIG_PM
686 static int atmel_ssc_suspend(struct snd_soc_dai *cpu_dai)
687 {
688         struct atmel_ssc_info *ssc_p;
689 
690         if (!cpu_dai->active)
691                 return 0;
692 
693         ssc_p = &ssc_info[cpu_dai->id];
694 
695         /* Save the status register before disabling transmit and receive */
696         ssc_p->ssc_state.ssc_sr = ssc_readl(ssc_p->ssc->regs, SR);
697         ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_TXDIS) | SSC_BIT(CR_RXDIS));
698 
699         /* Save the current interrupt mask, then disable unmasked interrupts */
700         ssc_p->ssc_state.ssc_imr = ssc_readl(ssc_p->ssc->regs, IMR);
701         ssc_writel(ssc_p->ssc->regs, IDR, ssc_p->ssc_state.ssc_imr);
702 
703         ssc_p->ssc_state.ssc_cmr = ssc_readl(ssc_p->ssc->regs, CMR);
704         ssc_p->ssc_state.ssc_rcmr = ssc_readl(ssc_p->ssc->regs, RCMR);
705         ssc_p->ssc_state.ssc_rfmr = ssc_readl(ssc_p->ssc->regs, RFMR);
706         ssc_p->ssc_state.ssc_tcmr = ssc_readl(ssc_p->ssc->regs, TCMR);
707         ssc_p->ssc_state.ssc_tfmr = ssc_readl(ssc_p->ssc->regs, TFMR);
708 
709         return 0;
710 }
711 
712 
713 
714 static int atmel_ssc_resume(struct snd_soc_dai *cpu_dai)
715 {
716         struct atmel_ssc_info *ssc_p;
717         u32 cr;
718 
719         if (!cpu_dai->active)
720                 return 0;
721 
722         ssc_p = &ssc_info[cpu_dai->id];
723 
724         /* restore SSC register settings */
725         ssc_writel(ssc_p->ssc->regs, TFMR, ssc_p->ssc_state.ssc_tfmr);
726         ssc_writel(ssc_p->ssc->regs, TCMR, ssc_p->ssc_state.ssc_tcmr);
727         ssc_writel(ssc_p->ssc->regs, RFMR, ssc_p->ssc_state.ssc_rfmr);
728         ssc_writel(ssc_p->ssc->regs, RCMR, ssc_p->ssc_state.ssc_rcmr);
729         ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->ssc_state.ssc_cmr);
730 
731         /* re-enable interrupts */
732         ssc_writel(ssc_p->ssc->regs, IER, ssc_p->ssc_state.ssc_imr);
733 
734         /* Re-enable receive and transmit as appropriate */
735         cr = 0;
736         cr |=
737             (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_RXEN)) ? SSC_BIT(CR_RXEN) : 0;
738         cr |=
739             (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_TXEN)) ? SSC_BIT(CR_TXEN) : 0;
740         ssc_writel(ssc_p->ssc->regs, CR, cr);
741 
742         return 0;
743 }
744 #else /* CONFIG_PM */
745 #  define atmel_ssc_suspend     NULL
746 #  define atmel_ssc_resume      NULL
747 #endif /* CONFIG_PM */
748 
749 #define ATMEL_SSC_RATES (SNDRV_PCM_RATE_8000_96000)
750 
751 #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8     | SNDRV_PCM_FMTBIT_S16_LE |\
752                           SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
753 
754 static const struct snd_soc_dai_ops atmel_ssc_dai_ops = {
755         .startup        = atmel_ssc_startup,
756         .shutdown       = atmel_ssc_shutdown,
757         .prepare        = atmel_ssc_prepare,
758         .trigger        = atmel_ssc_trigger,
759         .hw_params      = atmel_ssc_hw_params,
760         .set_fmt        = atmel_ssc_set_dai_fmt,
761         .set_clkdiv     = atmel_ssc_set_dai_clkdiv,
762 };
763 
764 static struct snd_soc_dai_driver atmel_ssc_dai = {
765                 .suspend = atmel_ssc_suspend,
766                 .resume = atmel_ssc_resume,
767                 .playback = {
768                         .channels_min = 1,
769                         .channels_max = 2,
770                         .rates = ATMEL_SSC_RATES,
771                         .formats = ATMEL_SSC_FORMATS,},
772                 .capture = {
773                         .channels_min = 1,
774                         .channels_max = 2,
775                         .rates = ATMEL_SSC_RATES,
776                         .formats = ATMEL_SSC_FORMATS,},
777                 .ops = &atmel_ssc_dai_ops,
778 };
779 
780 static const struct snd_soc_component_driver atmel_ssc_component = {
781         .name           = "atmel-ssc",
782 };
783 
784 static int asoc_ssc_init(struct device *dev)
785 {
786         struct platform_device *pdev = to_platform_device(dev);
787         struct ssc_device *ssc = platform_get_drvdata(pdev);
788         int ret;
789 
790         ret = snd_soc_register_component(dev, &atmel_ssc_component,
791                                          &atmel_ssc_dai, 1);
792         if (ret) {
793                 dev_err(dev, "Could not register DAI: %d\n", ret);
794                 goto err;
795         }
796 
797         if (ssc->pdata->use_dma)
798                 ret = atmel_pcm_dma_platform_register(dev);
799         else
800                 ret = atmel_pcm_pdc_platform_register(dev);
801 
802         if (ret) {
803                 dev_err(dev, "Could not register PCM: %d\n", ret);
804                 goto err_unregister_dai;
805         }
806 
807         return 0;
808 
809 err_unregister_dai:
810         snd_soc_unregister_component(dev);
811 err:
812         return ret;
813 }
814 
815 static void asoc_ssc_exit(struct device *dev)
816 {
817         struct platform_device *pdev = to_platform_device(dev);
818         struct ssc_device *ssc = platform_get_drvdata(pdev);
819 
820         if (ssc->pdata->use_dma)
821                 atmel_pcm_dma_platform_unregister(dev);
822         else
823                 atmel_pcm_pdc_platform_unregister(dev);
824 
825         snd_soc_unregister_component(dev);
826 }
827 
828 /**
829  * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
830  */
831 int atmel_ssc_set_audio(int ssc_id)
832 {
833         struct ssc_device *ssc;
834         int ret;
835 
836         /* If we can grab the SSC briefly to parent the DAI device off it */
837         ssc = ssc_request(ssc_id);
838         if (IS_ERR(ssc)) {
839                 pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n",
840                         PTR_ERR(ssc));
841                 return PTR_ERR(ssc);
842         } else {
843                 ssc_info[ssc_id].ssc = ssc;
844         }
845 
846         ret = asoc_ssc_init(&ssc->pdev->dev);
847 
848         return ret;
849 }
850 EXPORT_SYMBOL_GPL(atmel_ssc_set_audio);
851 
852 void atmel_ssc_put_audio(int ssc_id)
853 {
854         struct ssc_device *ssc = ssc_info[ssc_id].ssc;
855 
856         asoc_ssc_exit(&ssc->pdev->dev);
857         ssc_free(ssc);
858 }
859 EXPORT_SYMBOL_GPL(atmel_ssc_put_audio);
860 
861 /* Module information */
862 MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
863 MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
864 MODULE_LICENSE("GPL");
865 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp