blob: 3d4e32cff75e5d94f9a436ea3cdb20548f627302 [file] [log] [blame]
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +01001/*
Frank Mandarino36b8a8b2007-04-16 17:18:52 +02002 * at91-ssc.c -- ALSA SoC AT91 SSC Audio Layer Platform driver
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +01003 *
4 * Author: Frank Mandarino <fmandarino@endrelia.com>
5 * Endrelia Technologies Inc.
6 *
7 * Based on pxa2xx Platform drivers by
8 * Liam Girdwood <liam.girdwood@wolfsonmicro.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010015 */
16
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>
Frank Mandarinofa8f8782007-02-12 14:06:22 +010023#include <linux/atmel_pdc.h>
24
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010025#include <sound/driver.h>
26#include <sound/core.h>
27#include <sound/pcm.h>
Frank Mandarino36b8a8b2007-04-16 17:18:52 +020028#include <sound/pcm_params.h>
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010029#include <sound/initval.h>
30#include <sound/soc.h>
31
32#include <asm/arch/hardware.h>
33#include <asm/arch/at91_pmc.h>
34#include <asm/arch/at91_ssc.h>
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010035
36#include "at91-pcm.h"
Frank Mandarino36b8a8b2007-04-16 17:18:52 +020037#include "at91-ssc.h"
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010038
39#if 0
Frank Mandarino36b8a8b2007-04-16 17:18:52 +020040#define DBG(x...) printk(KERN_DEBUG "at91-ssc:" x)
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010041#else
42#define DBG(x...)
43#endif
44
45#if defined(CONFIG_ARCH_AT91SAM9260)
46#define NUM_SSC_DEVICES 1
47#else
48#define NUM_SSC_DEVICES 3
49#endif
50
51
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010052/*
53 * SSC PDC registers required by the PCM DMA engine.
54 */
55static struct at91_pdc_regs pdc_tx_reg = {
Frank Mandarinofa8f8782007-02-12 14:06:22 +010056 .xpr = ATMEL_PDC_TPR,
57 .xcr = ATMEL_PDC_TCR,
58 .xnpr = ATMEL_PDC_TNPR,
59 .xncr = ATMEL_PDC_TNCR,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010060};
61
62static struct at91_pdc_regs pdc_rx_reg = {
Frank Mandarinofa8f8782007-02-12 14:06:22 +010063 .xpr = ATMEL_PDC_RPR,
64 .xcr = ATMEL_PDC_RCR,
65 .xnpr = ATMEL_PDC_RNPR,
66 .xncr = ATMEL_PDC_RNCR,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010067};
68
69/*
70 * SSC & PDC status bits for transmit and receive.
71 */
72static struct at91_ssc_mask ssc_tx_mask = {
73 .ssc_enable = AT91_SSC_TXEN,
74 .ssc_disable = AT91_SSC_TXDIS,
75 .ssc_endx = AT91_SSC_ENDTX,
76 .ssc_endbuf = AT91_SSC_TXBUFE,
Frank Mandarinofa8f8782007-02-12 14:06:22 +010077 .pdc_enable = ATMEL_PDC_TXTEN,
78 .pdc_disable = ATMEL_PDC_TXTDIS,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010079};
80
81static struct at91_ssc_mask ssc_rx_mask = {
82 .ssc_enable = AT91_SSC_RXEN,
83 .ssc_disable = AT91_SSC_RXDIS,
84 .ssc_endx = AT91_SSC_ENDRX,
85 .ssc_endbuf = AT91_SSC_RXBUFF,
Frank Mandarinofa8f8782007-02-12 14:06:22 +010086 .pdc_enable = ATMEL_PDC_RXTEN,
87 .pdc_disable = ATMEL_PDC_RXTDIS,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010088};
89
90
91/*
92 * DMA parameters.
93 */
94static struct at91_pcm_dma_params ssc_dma_params[NUM_SSC_DEVICES][2] = {
95 {{
Frank Mandarino36b8a8b2007-04-16 17:18:52 +020096 .name = "SSC0 PCM out",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +010097 .pdc = &pdc_tx_reg,
98 .mask = &ssc_tx_mask,
99 },
100 {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200101 .name = "SSC0 PCM in",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100102 .pdc = &pdc_rx_reg,
103 .mask = &ssc_rx_mask,
104 }},
105#if NUM_SSC_DEVICES == 3
106 {{
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200107 .name = "SSC1 PCM out",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100108 .pdc = &pdc_tx_reg,
109 .mask = &ssc_tx_mask,
110 },
111 {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200112 .name = "SSC1 PCM in",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100113 .pdc = &pdc_rx_reg,
114 .mask = &ssc_rx_mask,
115 }},
116 {{
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200117 .name = "SSC2 PCM out",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100118 .pdc = &pdc_tx_reg,
119 .mask = &ssc_tx_mask,
120 },
121 {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200122 .name = "SSC2 PCM in",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100123 .pdc = &pdc_rx_reg,
124 .mask = &ssc_rx_mask,
125 }},
126#endif
127};
128
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100129struct at91_ssc_state {
130 u32 ssc_cmr;
131 u32 ssc_rcmr;
132 u32 ssc_rfmr;
133 u32 ssc_tcmr;
134 u32 ssc_tfmr;
135 u32 ssc_sr;
136 u32 ssc_imr;
137};
138
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100139static struct at91_ssc_info {
140 char *name;
141 struct at91_ssc_periph ssc;
142 spinlock_t lock; /* lock for dir_mask */
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100143 unsigned short dir_mask; /* 0=unused, 1=playback, 2=capture */
144 unsigned short initialized; /* 1=SSC has been initialized */
145 unsigned short daifmt;
146 unsigned short cmr_div;
147 unsigned short tcmr_period;
148 unsigned short rcmr_period;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100149 struct at91_pcm_dma_params *dma_params[2];
150 struct at91_ssc_state ssc_state;
151
152} ssc_info[NUM_SSC_DEVICES] = {
153 {
154 .name = "ssc0",
Milind Arun Choudharyc9758b22007-04-19 15:22:56 +0200155 .lock = __SPIN_LOCK_UNLOCKED(ssc_info[0].lock),
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100156 .dir_mask = 0,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100157 .initialized = 0,
158 },
159#if NUM_SSC_DEVICES == 3
160 {
161 .name = "ssc1",
Milind Arun Choudharyc9758b22007-04-19 15:22:56 +0200162 .lock = __SPIN_LOCK_UNLOCKED(ssc_info[1].lock),
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100163 .dir_mask = 0,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100164 .initialized = 0,
165 },
166 {
167 .name = "ssc2",
Milind Arun Choudharyc9758b22007-04-19 15:22:56 +0200168 .lock = __SPIN_LOCK_UNLOCKED(ssc_info[2].lock),
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100169 .dir_mask = 0,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100170 .initialized = 0,
171 },
172#endif
173};
174
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200175static unsigned int at91_ssc_sysclk;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100176
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100177/*
178 * SSC interrupt handler. Passes PDC interrupts to the DMA
179 * interrupt handler in the PCM driver.
180 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200181static irqreturn_t at91_ssc_interrupt(int irq, void *dev_id)
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100182{
183 struct at91_ssc_info *ssc_p = dev_id;
184 struct at91_pcm_dma_params *dma_params;
185 u32 ssc_sr;
186 int i;
187
188 ssc_sr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_SR)
189 & at91_ssc_read(ssc_p->ssc.base + AT91_SSC_IMR);
190
191 /*
192 * Loop through the substreams attached to this SSC. If
193 * a DMA-related interrupt occurred on that substream, call
194 * the DMA interrupt handler function, if one has been
195 * registered in the dma_params structure by the PCM driver.
196 */
197 for (i = 0; i < ARRAY_SIZE(ssc_p->dma_params); i++) {
198 dma_params = ssc_p->dma_params[i];
199
200 if (dma_params != NULL && dma_params->dma_intr_handler != NULL &&
201 (ssc_sr &
202 (dma_params->mask->ssc_endx | dma_params->mask->ssc_endbuf)))
203
204 dma_params->dma_intr_handler(ssc_sr, dma_params->substream);
205 }
206
207 return IRQ_HANDLED;
208}
209
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100210/*
211 * Startup. Only that one substream allowed in each direction.
212 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200213static int at91_ssc_startup(struct snd_pcm_substream *substream)
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100214{
215 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100216 struct at91_ssc_info *ssc_p = &ssc_info[rtd->dai->cpu_dai->id];
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100217 int dir_mask;
218
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200219 DBG("ssc_startup: SSC_SR=0x%08lx\n",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100220 at91_ssc_read(ssc_p->ssc.base + AT91_SSC_SR));
221 dir_mask = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0x1 : 0x2;
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
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100234/*
235 * Shutdown. Clear DMA parameters and shutdown the SSC if there
236 * are no other substreams open.
237 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200238static void at91_ssc_shutdown(struct snd_pcm_substream *substream)
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100239{
240 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100241 struct at91_ssc_info *ssc_p = &ssc_info[rtd->dai->cpu_dai->id];
242 struct at91_pcm_dma_params *dma_params;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100243 int dir, dir_mask;
244
245 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100246 dma_params = ssc_p->dma_params[dir];
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100247
248 if (dma_params != NULL) {
249 at91_ssc_write(dma_params->ssc_base + AT91_SSC_CR,
250 dma_params->mask->ssc_disable);
251 DBG("%s disabled SSC_SR=0x%08lx\n", (dir ? "receive" : "transmit"),
252 at91_ssc_read(ssc_p->ssc.base + AT91_SSC_SR));
253
254 dma_params->ssc_base = NULL;
255 dma_params->substream = NULL;
256 ssc_p->dma_params[dir] = NULL;
257 }
258
259 dir_mask = 1 << dir;
260
261 spin_lock_irq(&ssc_p->lock);
262 ssc_p->dir_mask &= ~dir_mask;
263 if (!ssc_p->dir_mask) {
264 /* Shutdown the SSC clock. */
265 DBG("Stopping pid %d clock\n", ssc_p->ssc.pid);
266 at91_sys_write(AT91_PMC_PCDR, 1<<ssc_p->ssc.pid);
267
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100268 if (ssc_p->initialized) {
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100269 free_irq(ssc_p->ssc.pid, ssc_p);
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100270 ssc_p->initialized = 0;
271 }
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100272
273 /* Reset the SSC */
274 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CR, AT91_SSC_SWRST);
275
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100276 /* Clear the SSC dividers */
277 ssc_p->cmr_div = ssc_p->tcmr_period = ssc_p->rcmr_period = 0;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100278 }
279 spin_unlock_irq(&ssc_p->lock);
280}
281
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100282/*
283 * Record the SSC system clock rate.
284 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200285static int at91_ssc_set_dai_sysclk(struct snd_soc_cpu_dai *cpu_dai,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100286 int clk_id, unsigned int freq, int dir)
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100287{
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100288 /*
289 * The only clock supplied to the SSC is the AT91 master clock,
290 * which is only used if the SSC is generating BCLK and/or
291 * LRC clocks.
292 */
293 switch (clk_id) {
294 case AT91_SYSCLK_MCK:
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200295 at91_ssc_sysclk = freq;
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100296 break;
297 default:
298 return -EINVAL;
299 }
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100300
301 return 0;
302}
303
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100304/*
305 * Record the DAI format for use in hw_params().
306 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200307static int at91_ssc_set_dai_fmt(struct snd_soc_cpu_dai *cpu_dai,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100308 unsigned int fmt)
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100309{
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100310 struct at91_ssc_info *ssc_p = &ssc_info[cpu_dai->id];
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100311
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100312 ssc_p->daifmt = fmt;
313 return 0;
314}
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100315
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100316/*
317 * Record SSC clock dividers for use in hw_params().
318 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200319static int at91_ssc_set_dai_clkdiv(struct snd_soc_cpu_dai *cpu_dai,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100320 int div_id, int div)
321{
322 struct at91_ssc_info *ssc_p = &ssc_info[cpu_dai->id];
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100323
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100324 switch (div_id) {
325 case AT91SSC_CMR_DIV:
326 /*
327 * The same master clock divider is used for both
328 * transmit and receive, so if a value has already
329 * been set, it must match this value.
330 */
331 if (ssc_p->cmr_div == 0)
332 ssc_p->cmr_div = div;
333 else
334 if (div != ssc_p->cmr_div)
335 return -EBUSY;
336 break;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100337
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100338 case AT91SSC_TCMR_PERIOD:
339 ssc_p->tcmr_period = div;
340 break;
341
342 case AT91SSC_RCMR_PERIOD:
343 ssc_p->rcmr_period = div;
344 break;
345
346 default:
347 return -EINVAL;
348 }
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100349
350 return 0;
351}
352
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100353/*
354 * Configure the SSC.
355 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200356static int at91_ssc_hw_params(struct snd_pcm_substream *substream,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100357 struct snd_pcm_hw_params *params)
358{
359 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100360 int id = rtd->dai->cpu_dai->id;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100361 struct at91_ssc_info *ssc_p = &ssc_info[id];
362 struct at91_pcm_dma_params *dma_params;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100363 int dir, channels, bits;
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100364 u32 tfmr, rfmr, tcmr, rcmr;
365 int start_event;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100366 int ret;
367
368 /*
369 * Currently, there is only one set of dma params for
370 * each direction. If more are added, this code will
371 * have to be changed to select the proper set.
372 */
373 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
374
375 dma_params = &ssc_dma_params[id][dir];
376 dma_params->ssc_base = ssc_p->ssc.base;
377 dma_params->substream = substream;
378
379 ssc_p->dma_params[dir] = dma_params;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100380
381 /*
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100382 * The cpu_dai->dma_data field is only used to communicate the
383 * appropriate DMA parameters to the pcm driver hw_params()
384 * function. It should not be used for other purposes
385 * as it is common to all substreams.
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100386 */
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100387 rtd->dai->cpu_dai->dma_data = dma_params;
388
389 channels = params_channels(params);
390
391 /*
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200392 * Determine sample size in bits and the PDC increment.
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100393 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200394 switch(params_format(params)) {
395 case SNDRV_PCM_FORMAT_S8:
396 bits = 8;
397 dma_params->pdc_xfer_size = 1;
398 break;
399 case SNDRV_PCM_FORMAT_S16_LE:
400 bits = 16;
401 dma_params->pdc_xfer_size = 2;
402 break;
403 case SNDRV_PCM_FORMAT_S24_LE:
404 bits = 24;
405 dma_params->pdc_xfer_size = 4;
406 break;
407 case SNDRV_PCM_FORMAT_S32_LE:
408 bits = 32;
409 dma_params->pdc_xfer_size = 4;
410 break;
411 default:
412 printk(KERN_WARNING "at91-ssc: unsupported PCM format");
413 return -EINVAL;
414 }
415
416 /*
417 * The SSC only supports up to 16-bit samples in I2S format, due
418 * to the size of the Frame Mode Register FSLEN field.
419 */
420 if ((ssc_p->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_I2S
421 && bits > 16) {
422 printk(KERN_WARNING
423 "at91-ssc: sample size %d is too large for I2S\n", bits);
424 return -EINVAL;
425 }
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100426
427 /*
428 * Compute SSC register settings.
429 */
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200430 switch (ssc_p->daifmt
431 & (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_MASTER_MASK)) {
432
433 case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS:
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100434 /*
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200435 * I2S format, SSC provides BCLK and LRC clocks.
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100436 *
437 * The SSC transmit and receive clocks are generated from the
438 * MCK divider, and the BCLK signal is output on the SSC TK line.
439 */
440 rcmr = (( ssc_p->rcmr_period << 24) & AT91_SSC_PERIOD)
441 | (( 1 << 16) & AT91_SSC_STTDLY)
442 | (( AT91_SSC_START_FALLING_RF ) & AT91_SSC_START)
443 | (( AT91_SSC_CK_RISING ) & AT91_SSC_CKI)
444 | (( AT91_SSC_CKO_NONE ) & AT91_SSC_CKO)
445 | (( AT91_SSC_CKS_DIV ) & AT91_SSC_CKS);
446
447 rfmr = (( AT91_SSC_FSEDGE_POSITIVE ) & AT91_SSC_FSEDGE)
448 | (( AT91_SSC_FSOS_NEGATIVE ) & AT91_SSC_FSOS)
449 | (((bits - 1) << 16) & AT91_SSC_FSLEN)
450 | (((channels - 1) << 8) & AT91_SSC_DATNB)
451 | (( 1 << 7) & AT91_SSC_MSBF)
452 | (( 0 << 5) & AT91_SSC_LOOP)
453 | (((bits - 1) << 0) & AT91_SSC_DATALEN);
454
455 tcmr = (( ssc_p->tcmr_period << 24) & AT91_SSC_PERIOD)
456 | (( 1 << 16) & AT91_SSC_STTDLY)
457 | (( AT91_SSC_START_FALLING_RF ) & AT91_SSC_START)
458 | (( AT91_SSC_CKI_FALLING ) & AT91_SSC_CKI)
459 | (( AT91_SSC_CKO_CONTINUOUS ) & AT91_SSC_CKO)
460 | (( AT91_SSC_CKS_DIV ) & AT91_SSC_CKS);
461
462 tfmr = (( AT91_SSC_FSEDGE_POSITIVE ) & AT91_SSC_FSEDGE)
463 | (( 0 << 23) & AT91_SSC_FSDEN)
464 | (( AT91_SSC_FSOS_NEGATIVE ) & AT91_SSC_FSOS)
465 | (((bits - 1) << 16) & AT91_SSC_FSLEN)
466 | (((channels - 1) << 8) & AT91_SSC_DATNB)
467 | (( 1 << 7) & AT91_SSC_MSBF)
468 | (( 0 << 5) & AT91_SSC_DATDEF)
469 | (((bits - 1) << 0) & AT91_SSC_DATALEN);
470 break;
471
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200472 case SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM:
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100473 /*
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200474 * I2S format, CODEC supplies BCLK and LRC clocks.
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100475 *
476 * The SSC transmit clock is obtained from the BCLK signal on
477 * on the TK line, and the SSC receive clock is generated from the
478 * transmit clock.
479 *
480 * For single channel data, one sample is transferred on the falling
481 * edge of the LRC clock. For two channel data, one sample is
482 * transferred on both edges of the LRC clock.
483 */
484 start_event = channels == 1
485 ? AT91_SSC_START_FALLING_RF
486 : AT91_SSC_START_EDGE_RF;
487
488 rcmr = (( 0 << 24) & AT91_SSC_PERIOD)
489 | (( 1 << 16) & AT91_SSC_STTDLY)
490 | (( start_event ) & AT91_SSC_START)
491 | (( AT91_SSC_CK_RISING ) & AT91_SSC_CKI)
492 | (( AT91_SSC_CKO_NONE ) & AT91_SSC_CKO)
493 | (( AT91_SSC_CKS_CLOCK ) & AT91_SSC_CKS);
494
495 rfmr = (( AT91_SSC_FSEDGE_POSITIVE ) & AT91_SSC_FSEDGE)
496 | (( AT91_SSC_FSOS_NONE ) & AT91_SSC_FSOS)
497 | (( 0 << 16) & AT91_SSC_FSLEN)
498 | (( 0 << 8) & AT91_SSC_DATNB)
499 | (( 1 << 7) & AT91_SSC_MSBF)
500 | (( 0 << 5) & AT91_SSC_LOOP)
501 | (((bits - 1) << 0) & AT91_SSC_DATALEN);
502
503 tcmr = (( 0 << 24) & AT91_SSC_PERIOD)
504 | (( 1 << 16) & AT91_SSC_STTDLY)
505 | (( start_event ) & AT91_SSC_START)
506 | (( AT91_SSC_CKI_FALLING ) & AT91_SSC_CKI)
507 | (( AT91_SSC_CKO_NONE ) & AT91_SSC_CKO)
508 | (( AT91_SSC_CKS_PIN ) & AT91_SSC_CKS);
509
510 tfmr = (( AT91_SSC_FSEDGE_POSITIVE ) & AT91_SSC_FSEDGE)
511 | (( 0 << 23) & AT91_SSC_FSDEN)
512 | (( AT91_SSC_FSOS_NONE ) & AT91_SSC_FSOS)
513 | (( 0 << 16) & AT91_SSC_FSLEN)
514 | (( 0 << 8) & AT91_SSC_DATNB)
515 | (( 1 << 7) & AT91_SSC_MSBF)
516 | (( 0 << 5) & AT91_SSC_DATDEF)
517 | (((bits - 1) << 0) & AT91_SSC_DATALEN);
518 break;
519
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200520 case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS:
521 /*
522 * DSP/PCM Mode A format, SSC provides BCLK and LRC clocks.
523 *
524 * The SSC transmit and receive clocks are generated from the
525 * MCK divider, and the BCLK signal is output on the SSC TK line.
526 */
527 rcmr = (( ssc_p->rcmr_period << 24) & AT91_SSC_PERIOD)
528 | (( 1 << 16) & AT91_SSC_STTDLY)
529 | (( AT91_SSC_START_RISING_RF ) & AT91_SSC_START)
530 | (( AT91_SSC_CK_RISING ) & AT91_SSC_CKI)
531 | (( AT91_SSC_CKO_NONE ) & AT91_SSC_CKO)
532 | (( AT91_SSC_CKS_DIV ) & AT91_SSC_CKS);
533
534 rfmr = (( AT91_SSC_FSEDGE_POSITIVE ) & AT91_SSC_FSEDGE)
535 | (( AT91_SSC_FSOS_POSITIVE ) & AT91_SSC_FSOS)
536 | (( 0 << 16) & AT91_SSC_FSLEN)
537 | (((channels - 1) << 8) & AT91_SSC_DATNB)
538 | (( 1 << 7) & AT91_SSC_MSBF)
539 | (( 0 << 5) & AT91_SSC_LOOP)
540 | (((bits - 1) << 0) & AT91_SSC_DATALEN);
541
542 tcmr = (( ssc_p->tcmr_period << 24) & AT91_SSC_PERIOD)
543 | (( 1 << 16) & AT91_SSC_STTDLY)
544 | (( AT91_SSC_START_RISING_RF ) & AT91_SSC_START)
545 | (( AT91_SSC_CK_RISING ) & AT91_SSC_CKI)
546 | (( AT91_SSC_CKO_CONTINUOUS ) & AT91_SSC_CKO)
547 | (( AT91_SSC_CKS_DIV ) & AT91_SSC_CKS);
548
549 tfmr = (( AT91_SSC_FSEDGE_POSITIVE ) & AT91_SSC_FSEDGE)
550 | (( 0 << 23) & AT91_SSC_FSDEN)
551 | (( AT91_SSC_FSOS_POSITIVE ) & AT91_SSC_FSOS)
552 | (( 0 << 16) & AT91_SSC_FSLEN)
553 | (((channels - 1) << 8) & AT91_SSC_DATNB)
554 | (( 1 << 7) & AT91_SSC_MSBF)
555 | (( 0 << 5) & AT91_SSC_DATDEF)
556 | (((bits - 1) << 0) & AT91_SSC_DATALEN);
557
558
559
560 break;
561
562 case SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM:
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100563 default:
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200564 printk(KERN_WARNING "at91-ssc: unsupported DAI format 0x%x.\n",
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100565 ssc_p->daifmt);
566 return -EINVAL;
567 break;
568 }
569 DBG("RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n", rcmr, rfmr, tcmr, tfmr);
570
571 if (!ssc_p->initialized) {
572
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100573 /* Enable PMC peripheral clock for this SSC */
574 DBG("Starting pid %d clock\n", ssc_p->ssc.pid);
575 at91_sys_write(AT91_PMC_PCER, 1<<ssc_p->ssc.pid);
576
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100577 /* Reset the SSC and its PDC registers */
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100578 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CR, AT91_SSC_SWRST);
579
Frank Mandarinofa8f8782007-02-12 14:06:22 +0100580 at91_ssc_write(ssc_p->ssc.base + ATMEL_PDC_RPR, 0);
581 at91_ssc_write(ssc_p->ssc.base + ATMEL_PDC_RCR, 0);
582 at91_ssc_write(ssc_p->ssc.base + ATMEL_PDC_RNPR, 0);
583 at91_ssc_write(ssc_p->ssc.base + ATMEL_PDC_RNCR, 0);
584 at91_ssc_write(ssc_p->ssc.base + ATMEL_PDC_TPR, 0);
585 at91_ssc_write(ssc_p->ssc.base + ATMEL_PDC_TCR, 0);
586 at91_ssc_write(ssc_p->ssc.base + ATMEL_PDC_TNPR, 0);
587 at91_ssc_write(ssc_p->ssc.base + ATMEL_PDC_TNCR, 0);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100588
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200589 if ((ret = request_irq(ssc_p->ssc.pid, at91_ssc_interrupt,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100590 0, ssc_p->name, ssc_p)) < 0) {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200591 printk(KERN_WARNING "at91-ssc: request_irq failure\n");
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100592
593 DBG("Stopping pid %d clock\n", ssc_p->ssc.pid);
594 at91_sys_write(AT91_PMC_PCER, 1<<ssc_p->ssc.pid);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100595 return ret;
596 }
597
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100598 ssc_p->initialized = 1;
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100599 }
600
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100601 /* set SSC clock mode register */
602 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CMR, ssc_p->cmr_div);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100603
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100604 /* set receive clock mode and format */
605 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RCMR, rcmr);
606 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RFMR, rfmr);
607
608 /* set transmit clock mode and format */
609 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_TCMR, tcmr);
610 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_TFMR, tfmr);
611
612 DBG("hw_params: SSC initialized\n");
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100613 return 0;
614}
615
616
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200617static int at91_ssc_prepare(struct snd_pcm_substream *substream)
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100618{
619 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100620 struct at91_ssc_info *ssc_p = &ssc_info[rtd->dai->cpu_dai->id];
621 struct at91_pcm_dma_params *dma_params;
622 int dir;
623
624 dir = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
625 dma_params = ssc_p->dma_params[dir];
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100626
627 at91_ssc_write(dma_params->ssc_base + AT91_SSC_CR,
628 dma_params->mask->ssc_enable);
629
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100630 DBG("%s enabled SSC_SR=0x%08lx\n", dir ? "receive" : "transmit",
631 at91_ssc_read(dma_params->ssc_base + AT91_SSC_SR));
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100632 return 0;
633}
634
635
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100636#ifdef CONFIG_PM
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200637static int at91_ssc_suspend(struct platform_device *pdev,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100638 struct snd_soc_cpu_dai *cpu_dai)
639{
640 struct at91_ssc_info *ssc_p;
641
642 if(!cpu_dai->active)
643 return 0;
644
645 ssc_p = &ssc_info[cpu_dai->id];
646
647 /* Save the status register before disabling transmit and receive. */
648 ssc_p->ssc_state.ssc_sr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_SR);
649 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CR,
650 AT91_SSC_TXDIS | AT91_SSC_RXDIS);
651
652 /* Save the current interrupt mask, then disable unmasked interrupts. */
653 ssc_p->ssc_state.ssc_imr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_IMR);
654 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_IDR, ssc_p->ssc_state.ssc_imr);
655
656 ssc_p->ssc_state.ssc_cmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_CMR);
657 ssc_p->ssc_state.ssc_rcmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_RCMR);
658 ssc_p->ssc_state.ssc_rfmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_RFMR);
659 ssc_p->ssc_state.ssc_tcmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_TCMR);
660 ssc_p->ssc_state.ssc_tfmr = at91_ssc_read(ssc_p->ssc.base + AT91_SSC_TFMR);
661
662 return 0;
663}
664
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200665static int at91_ssc_resume(struct platform_device *pdev,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100666 struct snd_soc_cpu_dai *cpu_dai)
667{
668 struct at91_ssc_info *ssc_p;
669
670 if(!cpu_dai->active)
671 return 0;
672
673 ssc_p = &ssc_info[cpu_dai->id];
674
675 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_TFMR, ssc_p->ssc_state.ssc_tfmr);
676 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_TCMR, ssc_p->ssc_state.ssc_tcmr);
677 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RFMR, ssc_p->ssc_state.ssc_rfmr);
678 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_RCMR, ssc_p->ssc_state.ssc_rcmr);
679 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CMR, ssc_p->ssc_state.ssc_cmr);
680
681 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_IER, ssc_p->ssc_state.ssc_imr);
682
683 at91_ssc_write(ssc_p->ssc.base + AT91_SSC_CR,
684 ((ssc_p->ssc_state.ssc_sr & AT91_SSC_RXENA) ? AT91_SSC_RXEN : 0) |
685 ((ssc_p->ssc_state.ssc_sr & AT91_SSC_TXENA) ? AT91_SSC_TXEN : 0));
686
687 return 0;
688}
689
690#else
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200691#define at91_ssc_suspend NULL
692#define at91_ssc_resume NULL
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100693#endif
694
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200695#define AT91_SSC_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100696 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
697 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
698 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
699 SNDRV_PCM_RATE_96000)
700
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200701#define AT91_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
702 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
703
704struct snd_soc_cpu_dai at91_ssc_dai[NUM_SSC_DEVICES] = {
705 { .name = "at91-ssc0",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100706 .id = 0,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200707 .type = SND_SOC_DAI_PCM,
708 .suspend = at91_ssc_suspend,
709 .resume = at91_ssc_resume,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100710 .playback = {
711 .channels_min = 1,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100712 .channels_max = 2,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200713 .rates = AT91_SSC_RATES,
714 .formats = AT91_SSC_FORMATS,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100715 .capture = {
716 .channels_min = 1,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100717 .channels_max = 2,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200718 .rates = AT91_SSC_RATES,
719 .formats = AT91_SSC_FORMATS,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100720 .ops = {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200721 .startup = at91_ssc_startup,
722 .shutdown = at91_ssc_shutdown,
723 .prepare = at91_ssc_prepare,
724 .hw_params = at91_ssc_hw_params,},
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100725 .dai_ops = {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200726 .set_sysclk = at91_ssc_set_dai_sysclk,
727 .set_fmt = at91_ssc_set_dai_fmt,
728 .set_clkdiv = at91_ssc_set_dai_clkdiv,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100729 .private_data = &ssc_info[0].ssc,
730 },
731#if NUM_SSC_DEVICES == 3
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200732 { .name = "at91-ssc1",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100733 .id = 1,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200734 .type = SND_SOC_DAI_PCM,
735 .suspend = at91_ssc_suspend,
736 .resume = at91_ssc_resume,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100737 .playback = {
738 .channels_min = 1,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100739 .channels_max = 2,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200740 .rates = AT91_SSC_RATES,
741 .formats = AT91_SSC_FORMATS,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100742 .capture = {
743 .channels_min = 1,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100744 .channels_max = 2,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200745 .rates = AT91_SSC_RATES,
746 .formats = AT91_SSC_FORMATS,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100747 .ops = {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200748 .startup = at91_ssc_startup,
749 .shutdown = at91_ssc_shutdown,
750 .prepare = at91_ssc_prepare,
751 .hw_params = at91_ssc_hw_params,},
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100752 .dai_ops = {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200753 .set_sysclk = at91_ssc_set_dai_sysclk,
754 .set_fmt = at91_ssc_set_dai_fmt,
755 .set_clkdiv = at91_ssc_set_dai_clkdiv,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100756 .private_data = &ssc_info[1].ssc,
757 },
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200758 { .name = "at91-ssc2",
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100759 .id = 2,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200760 .type = SND_SOC_DAI_PCM,
761 .suspend = at91_ssc_suspend,
762 .resume = at91_ssc_resume,
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100763 .playback = {
764 .channels_min = 1,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100765 .channels_max = 2,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200766 .rates = AT91_SSC_RATES,
767 .formats = AT91_SSC_FORMATS,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100768 .capture = {
769 .channels_min = 1,
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100770 .channels_max = 2,
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200771 .rates = AT91_SSC_RATES,
772 .formats = AT91_SSC_FORMATS,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100773 .ops = {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200774 .startup = at91_ssc_startup,
775 .shutdown = at91_ssc_shutdown,
776 .prepare = at91_ssc_prepare,
777 .hw_params = at91_ssc_hw_params,},
Frank Mandarino171eb8f2007-02-02 17:18:38 +0100778 .dai_ops = {
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200779 .set_sysclk = at91_ssc_set_dai_sysclk,
780 .set_fmt = at91_ssc_set_dai_fmt,
781 .set_clkdiv = at91_ssc_set_dai_clkdiv,},
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100782 .private_data = &ssc_info[2].ssc,
783 },
784#endif
785};
786
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200787EXPORT_SYMBOL_GPL(at91_ssc_dai);
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100788
789/* Module information */
790MODULE_AUTHOR("Frank Mandarino, fmandarino@endrelia.com, www.endrelia.com");
Frank Mandarino36b8a8b2007-04-16 17:18:52 +0200791MODULE_DESCRIPTION("AT91 SSC ASoC Interface");
Frank Mandarino9f0ac6e2006-11-24 15:49:39 +0100792MODULE_LICENSE("GPL");