Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.git] / sound / soc / pxa / pxa-ssp.c
1 /*
2  * pxa-ssp.c  --  ALSA Soc Audio Layer
3  *
4  * Copyright 2005,2008 Wolfson Microelectronics PLC.
5  * Author: Liam Girdwood
6  *         Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  * TODO:
14  *  o Test network mode for > 16bit sample size
15  */
16
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/clk.h>
21 #include <linux/io.h>
22
23 #include <asm/irq.h>
24
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/initval.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/pxa2xx-lib.h>
31
32 #include <mach/hardware.h>
33 #include <mach/dma.h>
34 #include <mach/regs-ssp.h>
35 #include <mach/audio.h>
36 #include <mach/ssp.h>
37
38 #include "pxa2xx-pcm.h"
39 #include "pxa-ssp.h"
40
41 /*
42  * SSP audio private data
43  */
44 struct ssp_priv {
45         struct ssp_device *ssp;
46         unsigned int sysclk;
47         int dai_fmt;
48 #ifdef CONFIG_PM
49         uint32_t        cr0;
50         uint32_t        cr1;
51         uint32_t        to;
52         uint32_t        psp;
53 #endif
54 };
55
56 static void dump_registers(struct ssp_device *ssp)
57 {
58         dev_dbg(&ssp->pdev->dev, "SSCR0 0x%08x SSCR1 0x%08x SSTO 0x%08x\n",
59                  ssp_read_reg(ssp, SSCR0), ssp_read_reg(ssp, SSCR1),
60                  ssp_read_reg(ssp, SSTO));
61
62         dev_dbg(&ssp->pdev->dev, "SSPSP 0x%08x SSSR 0x%08x SSACD 0x%08x\n",
63                  ssp_read_reg(ssp, SSPSP), ssp_read_reg(ssp, SSSR),
64                  ssp_read_reg(ssp, SSACD));
65 }
66
67 static void ssp_enable(struct ssp_device *ssp)
68 {
69         uint32_t sscr0;
70
71         sscr0 = __raw_readl(ssp->mmio_base + SSCR0) | SSCR0_SSE;
72         __raw_writel(sscr0, ssp->mmio_base + SSCR0);
73 }
74
75 static void ssp_disable(struct ssp_device *ssp)
76 {
77         uint32_t sscr0;
78
79         sscr0 = __raw_readl(ssp->mmio_base + SSCR0) & ~SSCR0_SSE;
80         __raw_writel(sscr0, ssp->mmio_base + SSCR0);
81 }
82
83 struct pxa2xx_pcm_dma_data {
84         struct pxa2xx_pcm_dma_params params;
85         char name[20];
86 };
87
88 static struct pxa2xx_pcm_dma_params *
89 ssp_get_dma_params(struct ssp_device *ssp, int width4, int out)
90 {
91         struct pxa2xx_pcm_dma_data *dma;
92
93         dma = kzalloc(sizeof(struct pxa2xx_pcm_dma_data), GFP_KERNEL);
94         if (dma == NULL)
95                 return NULL;
96
97         snprintf(dma->name, 20, "SSP%d PCM %s %s", ssp->port_id,
98                         width4 ? "32-bit" : "16-bit", out ? "out" : "in");
99
100         dma->params.name = dma->name;
101         dma->params.drcmr = &DRCMR(out ? ssp->drcmr_tx : ssp->drcmr_rx);
102         dma->params.dcmd = (out ? (DCMD_INCSRCADDR | DCMD_FLOWTRG) :
103                                   (DCMD_INCTRGADDR | DCMD_FLOWSRC)) |
104                         (width4 ? DCMD_WIDTH4 : DCMD_WIDTH2) | DCMD_BURST16;
105         dma->params.dev_addr = ssp->phys_base + SSDR;
106
107         return &dma->params;
108 }
109
110 static int pxa_ssp_startup(struct snd_pcm_substream *substream,
111                            struct snd_soc_dai *dai)
112 {
113         struct snd_soc_pcm_runtime *rtd = substream->private_data;
114         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
115         struct ssp_priv *priv = cpu_dai->private_data;
116         struct ssp_device *ssp = priv->ssp;
117         int ret = 0;
118
119         if (!cpu_dai->active) {
120                 clk_enable(ssp->clk);
121                 ssp_disable(ssp);
122         }
123
124         if (cpu_dai->dma_data) {
125                 kfree(cpu_dai->dma_data);
126                 cpu_dai->dma_data = NULL;
127         }
128         return ret;
129 }
130
131 static void pxa_ssp_shutdown(struct snd_pcm_substream *substream,
132                              struct snd_soc_dai *dai)
133 {
134         struct snd_soc_pcm_runtime *rtd = substream->private_data;
135         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
136         struct ssp_priv *priv = cpu_dai->private_data;
137         struct ssp_device *ssp = priv->ssp;
138
139         if (!cpu_dai->active) {
140                 ssp_disable(ssp);
141                 clk_disable(ssp->clk);
142         }
143
144         if (cpu_dai->dma_data) {
145                 kfree(cpu_dai->dma_data);
146                 cpu_dai->dma_data = NULL;
147         }
148 }
149
150 #ifdef CONFIG_PM
151
152 static int pxa_ssp_suspend(struct snd_soc_dai *cpu_dai)
153 {
154         struct ssp_priv *priv = cpu_dai->private_data;
155         struct ssp_device *ssp = priv->ssp;
156
157         if (!cpu_dai->active)
158                 clk_enable(ssp->clk);
159
160         priv->cr0 = __raw_readl(ssp->mmio_base + SSCR0);
161         priv->cr1 = __raw_readl(ssp->mmio_base + SSCR1);
162         priv->to  = __raw_readl(ssp->mmio_base + SSTO);
163         priv->psp = __raw_readl(ssp->mmio_base + SSPSP);
164
165         ssp_disable(ssp);
166         clk_disable(ssp->clk);
167         return 0;
168 }
169
170 static int pxa_ssp_resume(struct snd_soc_dai *cpu_dai)
171 {
172         struct ssp_priv *priv = cpu_dai->private_data;
173         struct ssp_device *ssp = priv->ssp;
174         uint32_t sssr = SSSR_ROR | SSSR_TUR | SSSR_BCE;
175
176         clk_enable(ssp->clk);
177
178         __raw_writel(sssr, ssp->mmio_base + SSSR);
179         __raw_writel(priv->cr0 & ~SSCR0_SSE, ssp->mmio_base + SSCR0);
180         __raw_writel(priv->cr1, ssp->mmio_base + SSCR1);
181         __raw_writel(priv->to,  ssp->mmio_base + SSTO);
182         __raw_writel(priv->psp, ssp->mmio_base + SSPSP);
183
184         if (cpu_dai->active)
185                 ssp_enable(ssp);
186         else
187                 clk_disable(ssp->clk);
188
189         return 0;
190 }
191
192 #else
193 #define pxa_ssp_suspend NULL
194 #define pxa_ssp_resume  NULL
195 #endif
196
197 /**
198  * ssp_set_clkdiv - set SSP clock divider
199  * @div: serial clock rate divider
200  */
201 static void ssp_set_scr(struct ssp_device *ssp, u32 div)
202 {
203         u32 sscr0 = ssp_read_reg(ssp, SSCR0);
204
205         if (cpu_is_pxa25x() && ssp->type == PXA25x_SSP) {
206                 sscr0 &= ~0x0000ff00;
207                 sscr0 |= ((div - 2)/2) << 8; /* 2..512 */
208         } else {
209                 sscr0 &= ~0x000fff00;
210                 sscr0 |= (div - 1) << 8;     /* 1..4096 */
211         }
212         ssp_write_reg(ssp, SSCR0, sscr0);
213 }
214
215 /**
216  * ssp_get_clkdiv - get SSP clock divider
217  */
218 static u32 ssp_get_scr(struct ssp_device *ssp)
219 {
220         u32 sscr0 = ssp_read_reg(ssp, SSCR0);
221         u32 div;
222
223         if (cpu_is_pxa25x() && ssp->type == PXA25x_SSP)
224                 div = ((sscr0 >> 8) & 0xff) * 2 + 2;
225         else
226                 div = ((sscr0 >> 8) & 0xfff) + 1;
227         return div;
228 }
229
230 /*
231  * Set the SSP ports SYSCLK.
232  */
233 static int pxa_ssp_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
234         int clk_id, unsigned int freq, int dir)
235 {
236         struct ssp_priv *priv = cpu_dai->private_data;
237         struct ssp_device *ssp = priv->ssp;
238         int val;
239
240         u32 sscr0 = ssp_read_reg(ssp, SSCR0) &
241                 ~(SSCR0_ECS |  SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
242
243         dev_dbg(&ssp->pdev->dev,
244                 "pxa_ssp_set_dai_sysclk id: %d, clk_id %d, freq %u\n",
245                 cpu_dai->id, clk_id, freq);
246
247         switch (clk_id) {
248         case PXA_SSP_CLK_NET_PLL:
249                 sscr0 |= SSCR0_MOD;
250                 break;
251         case PXA_SSP_CLK_PLL:
252                 /* Internal PLL is fixed */
253                 if (cpu_is_pxa25x())
254                         priv->sysclk = 1843200;
255                 else
256                         priv->sysclk = 13000000;
257                 break;
258         case PXA_SSP_CLK_EXT:
259                 priv->sysclk = freq;
260                 sscr0 |= SSCR0_ECS;
261                 break;
262         case PXA_SSP_CLK_NET:
263                 priv->sysclk = freq;
264                 sscr0 |= SSCR0_NCS | SSCR0_MOD;
265                 break;
266         case PXA_SSP_CLK_AUDIO:
267                 priv->sysclk = 0;
268                 ssp_set_scr(ssp, 1);
269                 sscr0 |= SSCR0_ACS;
270                 break;
271         default:
272                 return -ENODEV;
273         }
274
275         /* The SSP clock must be disabled when changing SSP clock mode
276          * on PXA2xx.  On PXA3xx it must be enabled when doing so. */
277         if (!cpu_is_pxa3xx())
278                 clk_disable(ssp->clk);
279         val = ssp_read_reg(ssp, SSCR0) | sscr0;
280         ssp_write_reg(ssp, SSCR0, val);
281         if (!cpu_is_pxa3xx())
282                 clk_enable(ssp->clk);
283
284         return 0;
285 }
286
287 /*
288  * Set the SSP clock dividers.
289  */
290 static int pxa_ssp_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
291         int div_id, int div)
292 {
293         struct ssp_priv *priv = cpu_dai->private_data;
294         struct ssp_device *ssp = priv->ssp;
295         int val;
296
297         switch (div_id) {
298         case PXA_SSP_AUDIO_DIV_ACDS:
299                 val = (ssp_read_reg(ssp, SSACD) & ~0x7) | SSACD_ACDS(div);
300                 ssp_write_reg(ssp, SSACD, val);
301                 break;
302         case PXA_SSP_AUDIO_DIV_SCDB:
303                 val = ssp_read_reg(ssp, SSACD);
304                 val &= ~SSACD_SCDB;
305 #if defined(CONFIG_PXA3xx)
306                 if (cpu_is_pxa3xx())
307                         val &= ~SSACD_SCDX8;
308 #endif
309                 switch (div) {
310                 case PXA_SSP_CLK_SCDB_1:
311                         val |= SSACD_SCDB;
312                         break;
313                 case PXA_SSP_CLK_SCDB_4:
314                         break;
315 #if defined(CONFIG_PXA3xx)
316                 case PXA_SSP_CLK_SCDB_8:
317                         if (cpu_is_pxa3xx())
318                                 val |= SSACD_SCDX8;
319                         else
320                                 return -EINVAL;
321                         break;
322 #endif
323                 default:
324                         return -EINVAL;
325                 }
326                 ssp_write_reg(ssp, SSACD, val);
327                 break;
328         case PXA_SSP_DIV_SCR:
329                 ssp_set_scr(ssp, div);
330                 break;
331         default:
332                 return -ENODEV;
333         }
334
335         return 0;
336 }
337
338 /*
339  * Configure the PLL frequency pxa27x and (afaik - pxa320 only)
340  */
341 static int pxa_ssp_set_dai_pll(struct snd_soc_dai *cpu_dai, int pll_id,
342         int source, unsigned int freq_in, unsigned int freq_out)
343 {
344         struct ssp_priv *priv = cpu_dai->private_data;
345         struct ssp_device *ssp = priv->ssp;
346         u32 ssacd = ssp_read_reg(ssp, SSACD) & ~0x70;
347
348 #if defined(CONFIG_PXA3xx)
349         if (cpu_is_pxa3xx())
350                 ssp_write_reg(ssp, SSACDD, 0);
351 #endif
352
353         switch (freq_out) {
354         case 5622000:
355                 break;
356         case 11345000:
357                 ssacd |= (0x1 << 4);
358                 break;
359         case 12235000:
360                 ssacd |= (0x2 << 4);
361                 break;
362         case 14857000:
363                 ssacd |= (0x3 << 4);
364                 break;
365         case 32842000:
366                 ssacd |= (0x4 << 4);
367                 break;
368         case 48000000:
369                 ssacd |= (0x5 << 4);
370                 break;
371         case 0:
372                 /* Disable */
373                 break;
374
375         default:
376 #ifdef CONFIG_PXA3xx
377                 /* PXA3xx has a clock ditherer which can be used to generate
378                  * a wider range of frequencies - calculate a value for it.
379                  */
380                 if (cpu_is_pxa3xx()) {
381                         u32 val;
382                         u64 tmp = 19968;
383                         tmp *= 1000000;
384                         do_div(tmp, freq_out);
385                         val = tmp;
386
387                         val = (val << 16) | 64;
388                         ssp_write_reg(ssp, SSACDD, val);
389
390                         ssacd |= (0x6 << 4);
391
392                         dev_dbg(&ssp->pdev->dev,
393                                 "Using SSACDD %x to supply %uHz\n",
394                                 val, freq_out);
395                         break;
396                 }
397 #endif
398
399                 return -EINVAL;
400         }
401
402         ssp_write_reg(ssp, SSACD, ssacd);
403
404         return 0;
405 }
406
407 /*
408  * Set the active slots in TDM/Network mode
409  */
410 static int pxa_ssp_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
411         unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
412 {
413         struct ssp_priv *priv = cpu_dai->private_data;
414         struct ssp_device *ssp = priv->ssp;
415         u32 sscr0;
416
417         sscr0 = ssp_read_reg(ssp, SSCR0);
418         sscr0 &= ~(SSCR0_MOD | SSCR0_SlotsPerFrm(8) | SSCR0_EDSS | SSCR0_DSS);
419
420         /* set slot width */
421         if (slot_width > 16)
422                 sscr0 |= SSCR0_EDSS | SSCR0_DataSize(slot_width - 16);
423         else
424                 sscr0 |= SSCR0_DataSize(slot_width);
425
426         if (slots > 1) {
427                 /* enable network mode */
428                 sscr0 |= SSCR0_MOD;
429
430                 /* set number of active slots */
431                 sscr0 |= SSCR0_SlotsPerFrm(slots);
432
433                 /* set active slot mask */
434                 ssp_write_reg(ssp, SSTSA, tx_mask);
435                 ssp_write_reg(ssp, SSRSA, rx_mask);
436         }
437         ssp_write_reg(ssp, SSCR0, sscr0);
438
439         return 0;
440 }
441
442 /*
443  * Tristate the SSP DAI lines
444  */
445 static int pxa_ssp_set_dai_tristate(struct snd_soc_dai *cpu_dai,
446         int tristate)
447 {
448         struct ssp_priv *priv = cpu_dai->private_data;
449         struct ssp_device *ssp = priv->ssp;
450         u32 sscr1;
451
452         sscr1 = ssp_read_reg(ssp, SSCR1);
453         if (tristate)
454                 sscr1 &= ~SSCR1_TTE;
455         else
456                 sscr1 |= SSCR1_TTE;
457         ssp_write_reg(ssp, SSCR1, sscr1);
458
459         return 0;
460 }
461
462 /*
463  * Set up the SSP DAI format.
464  * The SSP Port must be inactive before calling this function as the
465  * physical interface format is changed.
466  */
467 static int pxa_ssp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
468                 unsigned int fmt)
469 {
470         struct ssp_priv *priv = cpu_dai->private_data;
471         struct ssp_device *ssp = priv->ssp;
472         u32 sscr0;
473         u32 sscr1;
474         u32 sspsp;
475
476         /* check if we need to change anything at all */
477         if (priv->dai_fmt == fmt)
478                 return 0;
479
480         /* we can only change the settings if the port is not in use */
481         if (ssp_read_reg(ssp, SSCR0) & SSCR0_SSE) {
482                 dev_err(&ssp->pdev->dev,
483                         "can't change hardware dai format: stream is in use");
484                 return -EINVAL;
485         }
486
487         /* reset port settings */
488         sscr0 = ssp_read_reg(ssp, SSCR0) &
489                 (SSCR0_ECS |  SSCR0_NCS | SSCR0_MOD | SSCR0_ACS);
490         sscr1 = SSCR1_RxTresh(8) | SSCR1_TxTresh(7);
491         sspsp = 0;
492
493         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
494         case SND_SOC_DAIFMT_CBM_CFM:
495                 sscr1 |= SSCR1_SCLKDIR | SSCR1_SFRMDIR;
496                 break;
497         case SND_SOC_DAIFMT_CBM_CFS:
498                 sscr1 |= SSCR1_SCLKDIR;
499                 break;
500         case SND_SOC_DAIFMT_CBS_CFS:
501                 break;
502         default:
503                 return -EINVAL;
504         }
505
506         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
507         case SND_SOC_DAIFMT_NB_NF:
508                 sspsp |= SSPSP_SFRMP;
509                 break;
510         case SND_SOC_DAIFMT_NB_IF:
511                 break;
512         case SND_SOC_DAIFMT_IB_IF:
513                 sspsp |= SSPSP_SCMODE(2);
514                 break;
515         case SND_SOC_DAIFMT_IB_NF:
516                 sspsp |= SSPSP_SCMODE(2) | SSPSP_SFRMP;
517                 break;
518         default:
519                 return -EINVAL;
520         }
521
522         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
523         case SND_SOC_DAIFMT_I2S:
524                 sscr0 |= SSCR0_PSP;
525                 sscr1 |= SSCR1_RWOT | SSCR1_TRAIL;
526                 /* See hw_params() */
527                 break;
528
529         case SND_SOC_DAIFMT_DSP_A:
530                 sspsp |= SSPSP_FSRT;
531         case SND_SOC_DAIFMT_DSP_B:
532                 sscr0 |= SSCR0_MOD | SSCR0_PSP;
533                 sscr1 |= SSCR1_TRAIL | SSCR1_RWOT;
534                 break;
535
536         default:
537                 return -EINVAL;
538         }
539
540         ssp_write_reg(ssp, SSCR0, sscr0);
541         ssp_write_reg(ssp, SSCR1, sscr1);
542         ssp_write_reg(ssp, SSPSP, sspsp);
543
544         dump_registers(ssp);
545
546         /* Since we are configuring the timings for the format by hand
547          * we have to defer some things until hw_params() where we
548          * know parameters like the sample size.
549          */
550         priv->dai_fmt = fmt;
551
552         return 0;
553 }
554
555 /*
556  * Set the SSP audio DMA parameters and sample size.
557  * Can be called multiple times by oss emulation.
558  */
559 static int pxa_ssp_hw_params(struct snd_pcm_substream *substream,
560                                 struct snd_pcm_hw_params *params,
561                                 struct snd_soc_dai *dai)
562 {
563         struct snd_soc_pcm_runtime *rtd = substream->private_data;
564         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
565         struct ssp_priv *priv = cpu_dai->private_data;
566         struct ssp_device *ssp = priv->ssp;
567         int chn = params_channels(params);
568         u32 sscr0;
569         u32 sspsp;
570         int width = snd_pcm_format_physical_width(params_format(params));
571         int ttsa = ssp_read_reg(ssp, SSTSA) & 0xf;
572
573         /* generate correct DMA params */
574         if (cpu_dai->dma_data)
575                 kfree(cpu_dai->dma_data);
576
577         /* Network mode with one active slot (ttsa == 1) can be used
578          * to force 16-bit frame width on the wire (for S16_LE), even
579          * with two channels. Use 16-bit DMA transfers for this case.
580          */
581         cpu_dai->dma_data = ssp_get_dma_params(ssp,
582                         ((chn == 2) && (ttsa != 1)) || (width == 32),
583                         substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
584
585         /* we can only change the settings if the port is not in use */
586         if (ssp_read_reg(ssp, SSCR0) & SSCR0_SSE)
587                 return 0;
588
589         /* clear selected SSP bits */
590         sscr0 = ssp_read_reg(ssp, SSCR0) & ~(SSCR0_DSS | SSCR0_EDSS);
591         ssp_write_reg(ssp, SSCR0, sscr0);
592
593         /* bit size */
594         sscr0 = ssp_read_reg(ssp, SSCR0);
595         switch (params_format(params)) {
596         case SNDRV_PCM_FORMAT_S16_LE:
597 #ifdef CONFIG_PXA3xx
598                 if (cpu_is_pxa3xx())
599                         sscr0 |= SSCR0_FPCKE;
600 #endif
601                 sscr0 |= SSCR0_DataSize(16);
602                 break;
603         case SNDRV_PCM_FORMAT_S24_LE:
604                 sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(8));
605                 break;
606         case SNDRV_PCM_FORMAT_S32_LE:
607                 sscr0 |= (SSCR0_EDSS | SSCR0_DataSize(16));
608                 break;
609         }
610         ssp_write_reg(ssp, SSCR0, sscr0);
611
612         switch (priv->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
613         case SND_SOC_DAIFMT_I2S:
614                sspsp = ssp_read_reg(ssp, SSPSP);
615
616                 if ((ssp_get_scr(ssp) == 4) && (width == 16)) {
617                         /* This is a special case where the bitclk is 64fs
618                         * and we're not dealing with 2*32 bits of audio
619                         * samples.
620                         *
621                         * The SSP values used for that are all found out by
622                         * trying and failing a lot; some of the registers
623                         * needed for that mode are only available on PXA3xx.
624                         */
625
626 #ifdef CONFIG_PXA3xx
627                         if (!cpu_is_pxa3xx())
628                                 return -EINVAL;
629
630                         sspsp |= SSPSP_SFRMWDTH(width * 2);
631                         sspsp |= SSPSP_SFRMDLY(width * 4);
632                         sspsp |= SSPSP_EDMYSTOP(3);
633                         sspsp |= SSPSP_DMYSTOP(3);
634                         sspsp |= SSPSP_DMYSTRT(1);
635 #else
636                         return -EINVAL;
637 #endif
638                 } else {
639                         /* The frame width is the width the LRCLK is
640                          * asserted for; the delay is expressed in
641                          * half cycle units.  We need the extra cycle
642                          * because the data starts clocking out one BCLK
643                          * after LRCLK changes polarity.
644                          */
645                         sspsp |= SSPSP_SFRMWDTH(width + 1);
646                         sspsp |= SSPSP_SFRMDLY((width + 1) * 2);
647                         sspsp |= SSPSP_DMYSTRT(1);
648                 }
649
650                 ssp_write_reg(ssp, SSPSP, sspsp);
651                 break;
652         default:
653                 break;
654         }
655
656         /* When we use a network mode, we always require TDM slots
657          * - complain loudly and fail if they've not been set up yet.
658          */
659         if ((sscr0 & SSCR0_MOD) && !ttsa) {
660                 dev_err(&ssp->pdev->dev, "No TDM timeslot configured\n");
661                 return -EINVAL;
662         }
663
664         dump_registers(ssp);
665
666         return 0;
667 }
668
669 static int pxa_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
670                            struct snd_soc_dai *dai)
671 {
672         struct snd_soc_pcm_runtime *rtd = substream->private_data;
673         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
674         int ret = 0;
675         struct ssp_priv *priv = cpu_dai->private_data;
676         struct ssp_device *ssp = priv->ssp;
677         int val;
678
679         switch (cmd) {
680         case SNDRV_PCM_TRIGGER_RESUME:
681                 ssp_enable(ssp);
682                 break;
683         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
684                 val = ssp_read_reg(ssp, SSCR1);
685                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
686                         val |= SSCR1_TSRE;
687                 else
688                         val |= SSCR1_RSRE;
689                 ssp_write_reg(ssp, SSCR1, val);
690                 val = ssp_read_reg(ssp, SSSR);
691                 ssp_write_reg(ssp, SSSR, val);
692                 break;
693         case SNDRV_PCM_TRIGGER_START:
694                 val = ssp_read_reg(ssp, SSCR1);
695                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
696                         val |= SSCR1_TSRE;
697                 else
698                         val |= SSCR1_RSRE;
699                 ssp_write_reg(ssp, SSCR1, val);
700                 ssp_enable(ssp);
701                 break;
702         case SNDRV_PCM_TRIGGER_STOP:
703                 val = ssp_read_reg(ssp, SSCR1);
704                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
705                         val &= ~SSCR1_TSRE;
706                 else
707                         val &= ~SSCR1_RSRE;
708                 ssp_write_reg(ssp, SSCR1, val);
709                 break;
710         case SNDRV_PCM_TRIGGER_SUSPEND:
711                 ssp_disable(ssp);
712                 break;
713         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
714                 val = ssp_read_reg(ssp, SSCR1);
715                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
716                         val &= ~SSCR1_TSRE;
717                 else
718                         val &= ~SSCR1_RSRE;
719                 ssp_write_reg(ssp, SSCR1, val);
720                 break;
721
722         default:
723                 ret = -EINVAL;
724         }
725
726         dump_registers(ssp);
727
728         return ret;
729 }
730
731 static int pxa_ssp_probe(struct platform_device *pdev,
732                             struct snd_soc_dai *dai)
733 {
734         struct ssp_priv *priv;
735         int ret;
736
737         priv = kzalloc(sizeof(struct ssp_priv), GFP_KERNEL);
738         if (!priv)
739                 return -ENOMEM;
740
741         priv->ssp = ssp_request(dai->id + 1, "SoC audio");
742         if (priv->ssp == NULL) {
743                 ret = -ENODEV;
744                 goto err_priv;
745         }
746
747         priv->dai_fmt = (unsigned int) -1;
748         dai->private_data = priv;
749
750         return 0;
751
752 err_priv:
753         kfree(priv);
754         return ret;
755 }
756
757 static void pxa_ssp_remove(struct platform_device *pdev,
758                               struct snd_soc_dai *dai)
759 {
760         struct ssp_priv *priv = dai->private_data;
761         ssp_free(priv->ssp);
762 }
763
764 #define PXA_SSP_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
765                           SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
766                           SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
767                           SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
768
769 #define PXA_SSP_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
770                             SNDRV_PCM_FMTBIT_S24_LE |   \
771                             SNDRV_PCM_FMTBIT_S32_LE)
772
773 static struct snd_soc_dai_ops pxa_ssp_dai_ops = {
774         .startup        = pxa_ssp_startup,
775         .shutdown       = pxa_ssp_shutdown,
776         .trigger        = pxa_ssp_trigger,
777         .hw_params      = pxa_ssp_hw_params,
778         .set_sysclk     = pxa_ssp_set_dai_sysclk,
779         .set_clkdiv     = pxa_ssp_set_dai_clkdiv,
780         .set_pll        = pxa_ssp_set_dai_pll,
781         .set_fmt        = pxa_ssp_set_dai_fmt,
782         .set_tdm_slot   = pxa_ssp_set_dai_tdm_slot,
783         .set_tristate   = pxa_ssp_set_dai_tristate,
784 };
785
786 struct snd_soc_dai pxa_ssp_dai[] = {
787         {
788                 .name = "pxa2xx-ssp1",
789                 .id = 0,
790                 .probe = pxa_ssp_probe,
791                 .remove = pxa_ssp_remove,
792                 .suspend = pxa_ssp_suspend,
793                 .resume = pxa_ssp_resume,
794                 .playback = {
795                         .channels_min = 1,
796                         .channels_max = 8,
797                         .rates = PXA_SSP_RATES,
798                         .formats = PXA_SSP_FORMATS,
799                 },
800                 .capture = {
801                          .channels_min = 1,
802                          .channels_max = 8,
803                         .rates = PXA_SSP_RATES,
804                         .formats = PXA_SSP_FORMATS,
805                  },
806                 .ops = &pxa_ssp_dai_ops,
807         },
808         {       .name = "pxa2xx-ssp2",
809                 .id = 1,
810                 .probe = pxa_ssp_probe,
811                 .remove = pxa_ssp_remove,
812                 .suspend = pxa_ssp_suspend,
813                 .resume = pxa_ssp_resume,
814                 .playback = {
815                         .channels_min = 1,
816                         .channels_max = 8,
817                         .rates = PXA_SSP_RATES,
818                         .formats = PXA_SSP_FORMATS,
819                 },
820                 .capture = {
821                         .channels_min = 1,
822                         .channels_max = 8,
823                         .rates = PXA_SSP_RATES,
824                         .formats = PXA_SSP_FORMATS,
825                  },
826                 .ops = &pxa_ssp_dai_ops,
827         },
828         {
829                 .name = "pxa2xx-ssp3",
830                 .id = 2,
831                 .probe = pxa_ssp_probe,
832                 .remove = pxa_ssp_remove,
833                 .suspend = pxa_ssp_suspend,
834                 .resume = pxa_ssp_resume,
835                 .playback = {
836                         .channels_min = 1,
837                         .channels_max = 8,
838                         .rates = PXA_SSP_RATES,
839                         .formats = PXA_SSP_FORMATS,
840                 },
841                 .capture = {
842                         .channels_min = 1,
843                         .channels_max = 8,
844                         .rates = PXA_SSP_RATES,
845                         .formats = PXA_SSP_FORMATS,
846                  },
847                 .ops = &pxa_ssp_dai_ops,
848         },
849         {
850                 .name = "pxa2xx-ssp4",
851                 .id = 3,
852                 .probe = pxa_ssp_probe,
853                 .remove = pxa_ssp_remove,
854                 .suspend = pxa_ssp_suspend,
855                 .resume = pxa_ssp_resume,
856                 .playback = {
857                         .channels_min = 1,
858                         .channels_max = 8,
859                         .rates = PXA_SSP_RATES,
860                         .formats = PXA_SSP_FORMATS,
861                 },
862                 .capture = {
863                         .channels_min = 1,
864                         .channels_max = 8,
865                         .rates = PXA_SSP_RATES,
866                         .formats = PXA_SSP_FORMATS,
867                  },
868                 .ops = &pxa_ssp_dai_ops,
869         },
870 };
871 EXPORT_SYMBOL_GPL(pxa_ssp_dai);
872
873 static int __init pxa_ssp_init(void)
874 {
875         return snd_soc_register_dais(pxa_ssp_dai, ARRAY_SIZE(pxa_ssp_dai));
876 }
877 module_init(pxa_ssp_init);
878
879 static void __exit pxa_ssp_exit(void)
880 {
881         snd_soc_unregister_dais(pxa_ssp_dai, ARRAY_SIZE(pxa_ssp_dai));
882 }
883 module_exit(pxa_ssp_exit);
884
885 /* Module information */
886 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
887 MODULE_DESCRIPTION("PXA SSP/PCM SoC Interface");
888 MODULE_LICENSE("GPL");