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