WIP FPC-III support
[linux/fpc-iii.git] / sound / soc / atmel / atmel_ssc_dai.c
blob6a63e8797a0b6b5ecd58076dd1cd751dd81a1afd
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * atmel_ssc_dai.c -- ALSA SoC ATMEL SSC Audio Layer Platform driver
5 * Copyright (C) 2005 SAN People
6 * Copyright (C) 2008 Atmel
8 * Author: Sedji Gaouaou <sedji.gaouaou@atmel.com>
9 * ATMEL CORP.
11 * Based on at91-ssc.c by
12 * Frank Mandarino <fmandarino@endrelia.com>
13 * Based on pxa2xx Platform drivers by
14 * Liam Girdwood <lrg@slimlogic.co.uk>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/device.h>
21 #include <linux/delay.h>
22 #include <linux/clk.h>
23 #include <linux/atmel_pdc.h>
25 #include <linux/atmel-ssc.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/initval.h>
30 #include <sound/soc.h>
32 #include "atmel-pcm.h"
33 #include "atmel_ssc_dai.h"
36 #define NUM_SSC_DEVICES 3
39 * SSC PDC registers required by the PCM DMA engine.
41 static struct atmel_pdc_regs pdc_tx_reg = {
42 .xpr = ATMEL_PDC_TPR,
43 .xcr = ATMEL_PDC_TCR,
44 .xnpr = ATMEL_PDC_TNPR,
45 .xncr = ATMEL_PDC_TNCR,
48 static struct atmel_pdc_regs pdc_rx_reg = {
49 .xpr = ATMEL_PDC_RPR,
50 .xcr = ATMEL_PDC_RCR,
51 .xnpr = ATMEL_PDC_RNPR,
52 .xncr = ATMEL_PDC_RNCR,
56 * SSC & PDC status bits for transmit and receive.
58 static struct atmel_ssc_mask ssc_tx_mask = {
59 .ssc_enable = SSC_BIT(CR_TXEN),
60 .ssc_disable = SSC_BIT(CR_TXDIS),
61 .ssc_endx = SSC_BIT(SR_ENDTX),
62 .ssc_endbuf = SSC_BIT(SR_TXBUFE),
63 .ssc_error = SSC_BIT(SR_OVRUN),
64 .pdc_enable = ATMEL_PDC_TXTEN,
65 .pdc_disable = ATMEL_PDC_TXTDIS,
68 static struct atmel_ssc_mask ssc_rx_mask = {
69 .ssc_enable = SSC_BIT(CR_RXEN),
70 .ssc_disable = SSC_BIT(CR_RXDIS),
71 .ssc_endx = SSC_BIT(SR_ENDRX),
72 .ssc_endbuf = SSC_BIT(SR_RXBUFF),
73 .ssc_error = SSC_BIT(SR_OVRUN),
74 .pdc_enable = ATMEL_PDC_RXTEN,
75 .pdc_disable = ATMEL_PDC_RXTDIS,
80 * DMA parameters.
82 static struct atmel_pcm_dma_params ssc_dma_params[NUM_SSC_DEVICES][2] = {
84 .name = "SSC0 PCM out",
85 .pdc = &pdc_tx_reg,
86 .mask = &ssc_tx_mask,
89 .name = "SSC0 PCM in",
90 .pdc = &pdc_rx_reg,
91 .mask = &ssc_rx_mask,
92 } },
94 .name = "SSC1 PCM out",
95 .pdc = &pdc_tx_reg,
96 .mask = &ssc_tx_mask,
99 .name = "SSC1 PCM in",
100 .pdc = &pdc_rx_reg,
101 .mask = &ssc_rx_mask,
102 } },
104 .name = "SSC2 PCM out",
105 .pdc = &pdc_tx_reg,
106 .mask = &ssc_tx_mask,
109 .name = "SSC2 PCM in",
110 .pdc = &pdc_rx_reg,
111 .mask = &ssc_rx_mask,
112 } },
116 static struct atmel_ssc_info ssc_info[NUM_SSC_DEVICES] = {
118 .name = "ssc0",
119 .dir_mask = SSC_DIR_MASK_UNUSED,
120 .initialized = 0,
123 .name = "ssc1",
124 .dir_mask = SSC_DIR_MASK_UNUSED,
125 .initialized = 0,
128 .name = "ssc2",
129 .dir_mask = SSC_DIR_MASK_UNUSED,
130 .initialized = 0,
136 * SSC interrupt handler. Passes PDC interrupts to the DMA
137 * interrupt handler in the PCM driver.
139 static irqreturn_t atmel_ssc_interrupt(int irq, void *dev_id)
141 struct atmel_ssc_info *ssc_p = dev_id;
142 struct atmel_pcm_dma_params *dma_params;
143 u32 ssc_sr;
144 u32 ssc_substream_mask;
145 int i;
147 ssc_sr = (unsigned long)ssc_readl(ssc_p->ssc->regs, SR)
148 & (unsigned long)ssc_readl(ssc_p->ssc->regs, IMR);
151 * Loop through the substreams attached to this SSC. If
152 * a DMA-related interrupt occurred on that substream, call
153 * the DMA interrupt handler function, if one has been
154 * registered in the dma_params structure by the PCM driver.
156 for (i = 0; i < ARRAY_SIZE(ssc_p->dma_params); i++) {
157 dma_params = ssc_p->dma_params[i];
159 if ((dma_params != NULL) &&
160 (dma_params->dma_intr_handler != NULL)) {
161 ssc_substream_mask = (dma_params->mask->ssc_endx |
162 dma_params->mask->ssc_endbuf);
163 if (ssc_sr & ssc_substream_mask) {
164 dma_params->dma_intr_handler(ssc_sr,
165 dma_params->
166 substream);
171 return IRQ_HANDLED;
175 * When the bit clock is input, limit the maximum rate according to the
176 * Serial Clock Ratio Considerations section from the SSC documentation:
178 * The Transmitter and the Receiver can be programmed to operate
179 * with the clock signals provided on either the TK or RK pins.
180 * This allows the SSC to support many slave-mode data transfers.
181 * In this case, the maximum clock speed allowed on the RK pin is:
182 * - Peripheral clock divided by 2 if Receiver Frame Synchro is input
183 * - Peripheral clock divided by 3 if Receiver Frame Synchro is output
184 * In addition, the maximum clock speed allowed on the TK pin is:
185 * - Peripheral clock divided by 6 if Transmit Frame Synchro is input
186 * - Peripheral clock divided by 2 if Transmit Frame Synchro is output
188 * When the bit clock is output, limit the rate according to the
189 * SSC divider restrictions.
191 static int atmel_ssc_hw_rule_rate(struct snd_pcm_hw_params *params,
192 struct snd_pcm_hw_rule *rule)
194 struct atmel_ssc_info *ssc_p = rule->private;
195 struct ssc_device *ssc = ssc_p->ssc;
196 struct snd_interval *i = hw_param_interval(params, rule->var);
197 struct snd_interval t;
198 struct snd_ratnum r = {
199 .den_min = 1,
200 .den_max = 4095,
201 .den_step = 1,
203 unsigned int num = 0, den = 0;
204 int frame_size;
205 int mck_div = 2;
206 int ret;
208 frame_size = snd_soc_params_to_frame_size(params);
209 if (frame_size < 0)
210 return frame_size;
212 switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) {
213 case SND_SOC_DAIFMT_CBM_CFS:
214 if ((ssc_p->dir_mask & SSC_DIR_MASK_CAPTURE)
215 && ssc->clk_from_rk_pin)
216 /* Receiver Frame Synchro (i.e. capture)
217 * is output (format is _CFS) and the RK pin
218 * is used for input (format is _CBM_).
220 mck_div = 3;
221 break;
223 case SND_SOC_DAIFMT_CBM_CFM:
224 if ((ssc_p->dir_mask & SSC_DIR_MASK_PLAYBACK)
225 && !ssc->clk_from_rk_pin)
226 /* Transmit Frame Synchro (i.e. playback)
227 * is input (format is _CFM) and the TK pin
228 * is used for input (format _CBM_ but not
229 * using the RK pin).
231 mck_div = 6;
232 break;
235 switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) {
236 case SND_SOC_DAIFMT_CBS_CFS:
237 r.num = ssc_p->mck_rate / mck_div / frame_size;
239 ret = snd_interval_ratnum(i, 1, &r, &num, &den);
240 if (ret >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
241 params->rate_num = num;
242 params->rate_den = den;
244 break;
246 case SND_SOC_DAIFMT_CBM_CFS:
247 case SND_SOC_DAIFMT_CBM_CFM:
248 t.min = 8000;
249 t.max = ssc_p->mck_rate / mck_div / frame_size;
250 t.openmin = t.openmax = 0;
251 t.integer = 0;
252 ret = snd_interval_refine(i, &t);
253 break;
255 default:
256 ret = -EINVAL;
257 break;
260 return ret;
263 /*-------------------------------------------------------------------------*\
264 * DAI functions
265 \*-------------------------------------------------------------------------*/
267 * Startup. Only that one substream allowed in each direction.
269 static int atmel_ssc_startup(struct snd_pcm_substream *substream,
270 struct snd_soc_dai *dai)
272 struct platform_device *pdev = to_platform_device(dai->dev);
273 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
274 struct atmel_pcm_dma_params *dma_params;
275 int dir, dir_mask;
276 int ret;
278 pr_debug("atmel_ssc_startup: SSC_SR=0x%x\n",
279 ssc_readl(ssc_p->ssc->regs, SR));
281 /* Enable PMC peripheral clock for this SSC */
282 pr_debug("atmel_ssc_dai: Starting clock\n");
283 clk_enable(ssc_p->ssc->clk);
284 ssc_p->mck_rate = clk_get_rate(ssc_p->ssc->clk);
286 /* Reset the SSC unless initialized to keep it in a clean state */
287 if (!ssc_p->initialized)
288 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
290 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
291 dir = 0;
292 dir_mask = SSC_DIR_MASK_PLAYBACK;
293 } else {
294 dir = 1;
295 dir_mask = SSC_DIR_MASK_CAPTURE;
298 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
299 SNDRV_PCM_HW_PARAM_RATE,
300 atmel_ssc_hw_rule_rate,
301 ssc_p,
302 SNDRV_PCM_HW_PARAM_FRAME_BITS,
303 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
304 if (ret < 0) {
305 dev_err(dai->dev, "Failed to specify rate rule: %d\n", ret);
306 return ret;
309 dma_params = &ssc_dma_params[pdev->id][dir];
310 dma_params->ssc = ssc_p->ssc;
311 dma_params->substream = substream;
313 ssc_p->dma_params[dir] = dma_params;
315 snd_soc_dai_set_dma_data(dai, substream, dma_params);
317 if (ssc_p->dir_mask & dir_mask)
318 return -EBUSY;
320 ssc_p->dir_mask |= dir_mask;
322 return 0;
326 * Shutdown. Clear DMA parameters and shutdown the SSC if there
327 * are no other substreams open.
329 static void atmel_ssc_shutdown(struct snd_pcm_substream *substream,
330 struct snd_soc_dai *dai)
332 struct platform_device *pdev = to_platform_device(dai->dev);
333 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
334 struct atmel_pcm_dma_params *dma_params;
335 int dir, dir_mask;
337 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
338 dir = 0;
339 else
340 dir = 1;
342 dma_params = ssc_p->dma_params[dir];
344 if (dma_params != NULL) {
345 dma_params->ssc = NULL;
346 dma_params->substream = NULL;
347 ssc_p->dma_params[dir] = NULL;
350 dir_mask = 1 << dir;
352 ssc_p->dir_mask &= ~dir_mask;
353 if (!ssc_p->dir_mask) {
354 if (ssc_p->initialized) {
355 free_irq(ssc_p->ssc->irq, ssc_p);
356 ssc_p->initialized = 0;
359 /* Reset the SSC */
360 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST));
361 /* Clear the SSC dividers */
362 ssc_p->cmr_div = ssc_p->tcmr_period = ssc_p->rcmr_period = 0;
363 ssc_p->forced_divider = 0;
366 /* Shutdown the SSC clock. */
367 pr_debug("atmel_ssc_dai: Stopping clock\n");
368 clk_disable(ssc_p->ssc->clk);
373 * Record the DAI format for use in hw_params().
375 static int atmel_ssc_set_dai_fmt(struct snd_soc_dai *cpu_dai,
376 unsigned int fmt)
378 struct platform_device *pdev = to_platform_device(cpu_dai->dev);
379 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
381 ssc_p->daifmt = fmt;
382 return 0;
386 * Record SSC clock dividers for use in hw_params().
388 static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
389 int div_id, int div)
391 struct platform_device *pdev = to_platform_device(cpu_dai->dev);
392 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
394 switch (div_id) {
395 case ATMEL_SSC_CMR_DIV:
397 * The same master clock divider is used for both
398 * transmit and receive, so if a value has already
399 * been set, it must match this value.
401 if (ssc_p->dir_mask !=
402 (SSC_DIR_MASK_PLAYBACK | SSC_DIR_MASK_CAPTURE))
403 ssc_p->cmr_div = div;
404 else if (ssc_p->cmr_div == 0)
405 ssc_p->cmr_div = div;
406 else
407 if (div != ssc_p->cmr_div)
408 return -EBUSY;
409 ssc_p->forced_divider |= BIT(ATMEL_SSC_CMR_DIV);
410 break;
412 case ATMEL_SSC_TCMR_PERIOD:
413 ssc_p->tcmr_period = div;
414 ssc_p->forced_divider |= BIT(ATMEL_SSC_TCMR_PERIOD);
415 break;
417 case ATMEL_SSC_RCMR_PERIOD:
418 ssc_p->rcmr_period = div;
419 ssc_p->forced_divider |= BIT(ATMEL_SSC_RCMR_PERIOD);
420 break;
422 default:
423 return -EINVAL;
426 return 0;
429 /* Is the cpu-dai master of the frame clock? */
430 static int atmel_ssc_cfs(struct atmel_ssc_info *ssc_p)
432 switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) {
433 case SND_SOC_DAIFMT_CBM_CFS:
434 case SND_SOC_DAIFMT_CBS_CFS:
435 return 1;
437 return 0;
440 /* Is the cpu-dai master of the bit clock? */
441 static int atmel_ssc_cbs(struct atmel_ssc_info *ssc_p)
443 switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) {
444 case SND_SOC_DAIFMT_CBS_CFM:
445 case SND_SOC_DAIFMT_CBS_CFS:
446 return 1;
448 return 0;
452 * Configure the SSC.
454 static int atmel_ssc_hw_params(struct snd_pcm_substream *substream,
455 struct snd_pcm_hw_params *params,
456 struct snd_soc_dai *dai)
458 struct platform_device *pdev = to_platform_device(dai->dev);
459 int id = pdev->id;
460 struct atmel_ssc_info *ssc_p = &ssc_info[id];
461 struct ssc_device *ssc = ssc_p->ssc;
462 struct atmel_pcm_dma_params *dma_params;
463 int dir, channels, bits;
464 u32 tfmr, rfmr, tcmr, rcmr;
465 int ret;
466 int fslen, fslen_ext, fs_osync, fs_edge;
467 u32 cmr_div;
468 u32 tcmr_period;
469 u32 rcmr_period;
472 * Currently, there is only one set of dma params for
473 * each direction. If more are added, this code will
474 * have to be changed to select the proper set.
476 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
477 dir = 0;
478 else
479 dir = 1;
482 * If the cpu dai should provide BCLK, but noone has provided the
483 * divider needed for that to work, fall back to something sensible.
485 cmr_div = ssc_p->cmr_div;
486 if (!(ssc_p->forced_divider & BIT(ATMEL_SSC_CMR_DIV)) &&
487 atmel_ssc_cbs(ssc_p)) {
488 int bclk_rate = snd_soc_params_to_bclk(params);
490 if (bclk_rate < 0) {
491 dev_err(dai->dev, "unable to calculate cmr_div: %d\n",
492 bclk_rate);
493 return bclk_rate;
496 cmr_div = DIV_ROUND_CLOSEST(ssc_p->mck_rate, 2 * bclk_rate);
500 * If the cpu dai should provide LRCLK, but noone has provided the
501 * dividers needed for that to work, fall back to something sensible.
503 tcmr_period = ssc_p->tcmr_period;
504 rcmr_period = ssc_p->rcmr_period;
505 if (atmel_ssc_cfs(ssc_p)) {
506 int frame_size = snd_soc_params_to_frame_size(params);
508 if (frame_size < 0) {
509 dev_err(dai->dev,
510 "unable to calculate tx/rx cmr_period: %d\n",
511 frame_size);
512 return frame_size;
515 if (!(ssc_p->forced_divider & BIT(ATMEL_SSC_TCMR_PERIOD)))
516 tcmr_period = frame_size / 2 - 1;
517 if (!(ssc_p->forced_divider & BIT(ATMEL_SSC_RCMR_PERIOD)))
518 rcmr_period = frame_size / 2 - 1;
521 dma_params = ssc_p->dma_params[dir];
523 channels = params_channels(params);
526 * Determine sample size in bits and the PDC increment.
528 switch (params_format(params)) {
529 case SNDRV_PCM_FORMAT_S8:
530 bits = 8;
531 dma_params->pdc_xfer_size = 1;
532 break;
533 case SNDRV_PCM_FORMAT_S16_LE:
534 bits = 16;
535 dma_params->pdc_xfer_size = 2;
536 break;
537 case SNDRV_PCM_FORMAT_S24_LE:
538 bits = 24;
539 dma_params->pdc_xfer_size = 4;
540 break;
541 case SNDRV_PCM_FORMAT_S32_LE:
542 bits = 32;
543 dma_params->pdc_xfer_size = 4;
544 break;
545 default:
546 printk(KERN_WARNING "atmel_ssc_dai: unsupported PCM format");
547 return -EINVAL;
551 * Compute SSC register settings.
554 fslen_ext = (bits - 1) / 16;
555 fslen = (bits - 1) % 16;
557 switch (ssc_p->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
559 case SND_SOC_DAIFMT_LEFT_J:
560 fs_osync = SSC_FSOS_POSITIVE;
561 fs_edge = SSC_START_RISING_RF;
563 rcmr = SSC_BF(RCMR_STTDLY, 0);
564 tcmr = SSC_BF(TCMR_STTDLY, 0);
566 break;
568 case SND_SOC_DAIFMT_I2S:
569 fs_osync = SSC_FSOS_NEGATIVE;
570 fs_edge = SSC_START_FALLING_RF;
572 rcmr = SSC_BF(RCMR_STTDLY, 1);
573 tcmr = SSC_BF(TCMR_STTDLY, 1);
575 break;
577 case SND_SOC_DAIFMT_DSP_A:
579 * DSP/PCM Mode A format
581 * Data is transferred on first BCLK after LRC pulse rising
582 * edge.If stereo, the right channel data is contiguous with
583 * the left channel data.
585 fs_osync = SSC_FSOS_POSITIVE;
586 fs_edge = SSC_START_RISING_RF;
587 fslen = fslen_ext = 0;
589 rcmr = SSC_BF(RCMR_STTDLY, 1);
590 tcmr = SSC_BF(TCMR_STTDLY, 1);
592 break;
594 default:
595 printk(KERN_WARNING "atmel_ssc_dai: unsupported DAI format 0x%x\n",
596 ssc_p->daifmt);
597 return -EINVAL;
600 if (!atmel_ssc_cfs(ssc_p)) {
601 fslen = fslen_ext = 0;
602 rcmr_period = tcmr_period = 0;
603 fs_osync = SSC_FSOS_NONE;
606 rcmr |= SSC_BF(RCMR_START, fs_edge);
607 tcmr |= SSC_BF(TCMR_START, fs_edge);
609 if (atmel_ssc_cbs(ssc_p)) {
611 * SSC provides BCLK
613 * The SSC transmit and receive clocks are generated from the
614 * MCK divider, and the BCLK signal is output
615 * on the SSC TK line.
617 rcmr |= SSC_BF(RCMR_CKS, SSC_CKS_DIV)
618 | SSC_BF(RCMR_CKO, SSC_CKO_NONE);
620 tcmr |= SSC_BF(TCMR_CKS, SSC_CKS_DIV)
621 | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS);
622 } else {
623 rcmr |= SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ?
624 SSC_CKS_PIN : SSC_CKS_CLOCK)
625 | SSC_BF(RCMR_CKO, SSC_CKO_NONE);
627 tcmr |= SSC_BF(TCMR_CKS, ssc->clk_from_rk_pin ?
628 SSC_CKS_CLOCK : SSC_CKS_PIN)
629 | SSC_BF(TCMR_CKO, SSC_CKO_NONE);
632 rcmr |= SSC_BF(RCMR_PERIOD, rcmr_period)
633 | SSC_BF(RCMR_CKI, SSC_CKI_RISING);
635 tcmr |= SSC_BF(TCMR_PERIOD, tcmr_period)
636 | SSC_BF(TCMR_CKI, SSC_CKI_FALLING);
638 rfmr = SSC_BF(RFMR_FSLEN_EXT, fslen_ext)
639 | SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
640 | SSC_BF(RFMR_FSOS, fs_osync)
641 | SSC_BF(RFMR_FSLEN, fslen)
642 | SSC_BF(RFMR_DATNB, (channels - 1))
643 | SSC_BIT(RFMR_MSBF)
644 | SSC_BF(RFMR_LOOP, 0)
645 | SSC_BF(RFMR_DATLEN, (bits - 1));
647 tfmr = SSC_BF(TFMR_FSLEN_EXT, fslen_ext)
648 | SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE)
649 | SSC_BF(TFMR_FSDEN, 0)
650 | SSC_BF(TFMR_FSOS, fs_osync)
651 | SSC_BF(TFMR_FSLEN, fslen)
652 | SSC_BF(TFMR_DATNB, (channels - 1))
653 | SSC_BIT(TFMR_MSBF)
654 | SSC_BF(TFMR_DATDEF, 0)
655 | SSC_BF(TFMR_DATLEN, (bits - 1));
657 if (fslen_ext && !ssc->pdata->has_fslen_ext) {
658 dev_err(dai->dev, "sample size %d is too large for SSC device\n",
659 bits);
660 return -EINVAL;
663 pr_debug("atmel_ssc_hw_params: "
664 "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n",
665 rcmr, rfmr, tcmr, tfmr);
667 if (!ssc_p->initialized) {
668 if (!ssc_p->ssc->pdata->use_dma) {
669 ssc_writel(ssc_p->ssc->regs, PDC_RPR, 0);
670 ssc_writel(ssc_p->ssc->regs, PDC_RCR, 0);
671 ssc_writel(ssc_p->ssc->regs, PDC_RNPR, 0);
672 ssc_writel(ssc_p->ssc->regs, PDC_RNCR, 0);
674 ssc_writel(ssc_p->ssc->regs, PDC_TPR, 0);
675 ssc_writel(ssc_p->ssc->regs, PDC_TCR, 0);
676 ssc_writel(ssc_p->ssc->regs, PDC_TNPR, 0);
677 ssc_writel(ssc_p->ssc->regs, PDC_TNCR, 0);
680 ret = request_irq(ssc_p->ssc->irq, atmel_ssc_interrupt, 0,
681 ssc_p->name, ssc_p);
682 if (ret < 0) {
683 printk(KERN_WARNING
684 "atmel_ssc_dai: request_irq failure\n");
685 pr_debug("Atmel_ssc_dai: Stopping clock\n");
686 clk_disable(ssc_p->ssc->clk);
687 return ret;
690 ssc_p->initialized = 1;
693 /* set SSC clock mode register */
694 ssc_writel(ssc_p->ssc->regs, CMR, cmr_div);
696 /* set receive clock mode and format */
697 ssc_writel(ssc_p->ssc->regs, RCMR, rcmr);
698 ssc_writel(ssc_p->ssc->regs, RFMR, rfmr);
700 /* set transmit clock mode and format */
701 ssc_writel(ssc_p->ssc->regs, TCMR, tcmr);
702 ssc_writel(ssc_p->ssc->regs, TFMR, tfmr);
704 pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n");
705 return 0;
709 static int atmel_ssc_prepare(struct snd_pcm_substream *substream,
710 struct snd_soc_dai *dai)
712 struct platform_device *pdev = to_platform_device(dai->dev);
713 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
714 struct atmel_pcm_dma_params *dma_params;
715 int dir;
717 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
718 dir = 0;
719 else
720 dir = 1;
722 dma_params = ssc_p->dma_params[dir];
724 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
725 ssc_writel(ssc_p->ssc->regs, IDR, dma_params->mask->ssc_error);
727 pr_debug("%s enabled SSC_SR=0x%08x\n",
728 dir ? "receive" : "transmit",
729 ssc_readl(ssc_p->ssc->regs, SR));
730 return 0;
733 static int atmel_ssc_trigger(struct snd_pcm_substream *substream,
734 int cmd, struct snd_soc_dai *dai)
736 struct platform_device *pdev = to_platform_device(dai->dev);
737 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id];
738 struct atmel_pcm_dma_params *dma_params;
739 int dir;
741 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
742 dir = 0;
743 else
744 dir = 1;
746 dma_params = ssc_p->dma_params[dir];
748 switch (cmd) {
749 case SNDRV_PCM_TRIGGER_START:
750 case SNDRV_PCM_TRIGGER_RESUME:
751 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
752 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_enable);
753 break;
754 default:
755 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable);
756 break;
759 return 0;
762 #ifdef CONFIG_PM
763 static int atmel_ssc_suspend(struct snd_soc_component *component)
765 struct atmel_ssc_info *ssc_p;
766 struct platform_device *pdev = to_platform_device(component->dev);
768 if (!snd_soc_component_active(component))
769 return 0;
771 ssc_p = &ssc_info[pdev->id];
773 /* Save the status register before disabling transmit and receive */
774 ssc_p->ssc_state.ssc_sr = ssc_readl(ssc_p->ssc->regs, SR);
775 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_TXDIS) | SSC_BIT(CR_RXDIS));
777 /* Save the current interrupt mask, then disable unmasked interrupts */
778 ssc_p->ssc_state.ssc_imr = ssc_readl(ssc_p->ssc->regs, IMR);
779 ssc_writel(ssc_p->ssc->regs, IDR, ssc_p->ssc_state.ssc_imr);
781 ssc_p->ssc_state.ssc_cmr = ssc_readl(ssc_p->ssc->regs, CMR);
782 ssc_p->ssc_state.ssc_rcmr = ssc_readl(ssc_p->ssc->regs, RCMR);
783 ssc_p->ssc_state.ssc_rfmr = ssc_readl(ssc_p->ssc->regs, RFMR);
784 ssc_p->ssc_state.ssc_tcmr = ssc_readl(ssc_p->ssc->regs, TCMR);
785 ssc_p->ssc_state.ssc_tfmr = ssc_readl(ssc_p->ssc->regs, TFMR);
787 return 0;
790 static int atmel_ssc_resume(struct snd_soc_component *component)
792 struct atmel_ssc_info *ssc_p;
793 struct platform_device *pdev = to_platform_device(component->dev);
794 u32 cr;
796 if (!snd_soc_component_active(component))
797 return 0;
799 ssc_p = &ssc_info[pdev->id];
801 /* restore SSC register settings */
802 ssc_writel(ssc_p->ssc->regs, TFMR, ssc_p->ssc_state.ssc_tfmr);
803 ssc_writel(ssc_p->ssc->regs, TCMR, ssc_p->ssc_state.ssc_tcmr);
804 ssc_writel(ssc_p->ssc->regs, RFMR, ssc_p->ssc_state.ssc_rfmr);
805 ssc_writel(ssc_p->ssc->regs, RCMR, ssc_p->ssc_state.ssc_rcmr);
806 ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->ssc_state.ssc_cmr);
808 /* re-enable interrupts */
809 ssc_writel(ssc_p->ssc->regs, IER, ssc_p->ssc_state.ssc_imr);
811 /* Re-enable receive and transmit as appropriate */
812 cr = 0;
813 cr |=
814 (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_RXEN)) ? SSC_BIT(CR_RXEN) : 0;
815 cr |=
816 (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_TXEN)) ? SSC_BIT(CR_TXEN) : 0;
817 ssc_writel(ssc_p->ssc->regs, CR, cr);
819 return 0;
821 #else /* CONFIG_PM */
822 # define atmel_ssc_suspend NULL
823 # define atmel_ssc_resume NULL
824 #endif /* CONFIG_PM */
826 #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
827 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
829 static const struct snd_soc_dai_ops atmel_ssc_dai_ops = {
830 .startup = atmel_ssc_startup,
831 .shutdown = atmel_ssc_shutdown,
832 .prepare = atmel_ssc_prepare,
833 .trigger = atmel_ssc_trigger,
834 .hw_params = atmel_ssc_hw_params,
835 .set_fmt = atmel_ssc_set_dai_fmt,
836 .set_clkdiv = atmel_ssc_set_dai_clkdiv,
839 static struct snd_soc_dai_driver atmel_ssc_dai = {
840 .playback = {
841 .channels_min = 1,
842 .channels_max = 2,
843 .rates = SNDRV_PCM_RATE_CONTINUOUS,
844 .rate_min = 8000,
845 .rate_max = 384000,
846 .formats = ATMEL_SSC_FORMATS,},
847 .capture = {
848 .channels_min = 1,
849 .channels_max = 2,
850 .rates = SNDRV_PCM_RATE_CONTINUOUS,
851 .rate_min = 8000,
852 .rate_max = 384000,
853 .formats = ATMEL_SSC_FORMATS,},
854 .ops = &atmel_ssc_dai_ops,
857 static const struct snd_soc_component_driver atmel_ssc_component = {
858 .name = "atmel-ssc",
859 .suspend = atmel_ssc_suspend,
860 .resume = atmel_ssc_resume,
863 static int asoc_ssc_init(struct device *dev)
865 struct ssc_device *ssc = dev_get_drvdata(dev);
866 int ret;
868 ret = devm_snd_soc_register_component(dev, &atmel_ssc_component,
869 &atmel_ssc_dai, 1);
870 if (ret) {
871 dev_err(dev, "Could not register DAI: %d\n", ret);
872 return ret;
875 if (ssc->pdata->use_dma)
876 ret = atmel_pcm_dma_platform_register(dev);
877 else
878 ret = atmel_pcm_pdc_platform_register(dev);
880 if (ret) {
881 dev_err(dev, "Could not register PCM: %d\n", ret);
882 return ret;
885 return 0;
889 * atmel_ssc_set_audio - Allocate the specified SSC for audio use.
890 * @ssc_id: SSD ID in [0, NUM_SSC_DEVICES[
892 int atmel_ssc_set_audio(int ssc_id)
894 struct ssc_device *ssc;
895 int ret;
897 /* If we can grab the SSC briefly to parent the DAI device off it */
898 ssc = ssc_request(ssc_id);
899 if (IS_ERR(ssc)) {
900 pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n",
901 PTR_ERR(ssc));
902 return PTR_ERR(ssc);
903 } else {
904 ssc_info[ssc_id].ssc = ssc;
907 ret = asoc_ssc_init(&ssc->pdev->dev);
909 return ret;
911 EXPORT_SYMBOL_GPL(atmel_ssc_set_audio);
913 void atmel_ssc_put_audio(int ssc_id)
915 struct ssc_device *ssc = ssc_info[ssc_id].ssc;
917 ssc_free(ssc);
919 EXPORT_SYMBOL_GPL(atmel_ssc_put_audio);
921 /* Module information */
922 MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com");
923 MODULE_DESCRIPTION("ATMEL SSC ASoC Interface");
924 MODULE_LICENSE("GPL");