]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/soc/au1x/psc-i2s.c
Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jlbec...
[mirror_ubuntu-bionic-kernel.git] / sound / soc / au1x / psc-i2s.c
1 /*
2 * Au12x0/Au1550 PSC ALSA ASoC audio support.
3 *
4 * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
5 * Manuel Lauss <manuel.lauss@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Au1xxx-PSC I2S glue.
12 *
13 * NOTE: all of these drivers can only work with a SINGLE instance
14 * of a PSC. Multiple independent audio devices are impossible
15 * with ASoC v1.
16 * NOTE: so far only PSC slave mode (bit- and frameclock) is supported.
17 */
18
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/suspend.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/initval.h>
25 #include <sound/soc.h>
26 #include <asm/mach-au1x00/au1000.h>
27 #include <asm/mach-au1x00/au1xxx_psc.h>
28
29 #include "psc.h"
30
31 /* supported I2S DAI hardware formats */
32 #define AU1XPSC_I2S_DAIFMT \
33 (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | \
34 SND_SOC_DAIFMT_NB_NF)
35
36 /* supported I2S direction */
37 #define AU1XPSC_I2S_DIR \
38 (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
39
40 #define AU1XPSC_I2S_RATES \
41 SNDRV_PCM_RATE_8000_192000
42
43 #define AU1XPSC_I2S_FMTS \
44 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
45
46 #define I2SSTAT_BUSY(stype) \
47 ((stype) == PCM_TX ? PSC_I2SSTAT_TB : PSC_I2SSTAT_RB)
48 #define I2SPCR_START(stype) \
49 ((stype) == PCM_TX ? PSC_I2SPCR_TS : PSC_I2SPCR_RS)
50 #define I2SPCR_STOP(stype) \
51 ((stype) == PCM_TX ? PSC_I2SPCR_TP : PSC_I2SPCR_RP)
52 #define I2SPCR_CLRFIFO(stype) \
53 ((stype) == PCM_TX ? PSC_I2SPCR_TC : PSC_I2SPCR_RC)
54
55
56 /* instance data. There can be only one, MacLeod!!!! */
57 static struct au1xpsc_audio_data *au1xpsc_i2s_workdata;
58
59 static int au1xpsc_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
60 unsigned int fmt)
61 {
62 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
63 unsigned long ct;
64 int ret;
65
66 ret = -EINVAL;
67
68 ct = pscdata->cfg;
69
70 ct &= ~(PSC_I2SCFG_XM | PSC_I2SCFG_MLJ); /* left-justified */
71 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
72 case SND_SOC_DAIFMT_I2S:
73 ct |= PSC_I2SCFG_XM; /* enable I2S mode */
74 break;
75 case SND_SOC_DAIFMT_MSB:
76 break;
77 case SND_SOC_DAIFMT_LSB:
78 ct |= PSC_I2SCFG_MLJ; /* LSB (right-) justified */
79 break;
80 default:
81 goto out;
82 }
83
84 ct &= ~(PSC_I2SCFG_BI | PSC_I2SCFG_WI); /* IB-IF */
85 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
86 case SND_SOC_DAIFMT_NB_NF:
87 ct |= PSC_I2SCFG_BI | PSC_I2SCFG_WI;
88 break;
89 case SND_SOC_DAIFMT_NB_IF:
90 ct |= PSC_I2SCFG_BI;
91 break;
92 case SND_SOC_DAIFMT_IB_NF:
93 ct |= PSC_I2SCFG_WI;
94 break;
95 case SND_SOC_DAIFMT_IB_IF:
96 break;
97 default:
98 goto out;
99 }
100
101 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
102 case SND_SOC_DAIFMT_CBM_CFM: /* CODEC master */
103 ct |= PSC_I2SCFG_MS; /* PSC I2S slave mode */
104 break;
105 case SND_SOC_DAIFMT_CBS_CFS: /* CODEC slave */
106 ct &= ~PSC_I2SCFG_MS; /* PSC I2S Master mode */
107 break;
108 default:
109 goto out;
110 }
111
112 pscdata->cfg = ct;
113 ret = 0;
114 out:
115 return ret;
116 }
117
118 static int au1xpsc_i2s_hw_params(struct snd_pcm_substream *substream,
119 struct snd_pcm_hw_params *params,
120 struct snd_soc_dai *dai)
121 {
122 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
123
124 int cfgbits;
125 unsigned long stat;
126
127 /* check if the PSC is already streaming data */
128 stat = au_readl(I2S_STAT(pscdata));
129 if (stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB)) {
130 /* reject parameters not currently set up in hardware */
131 cfgbits = au_readl(I2S_CFG(pscdata));
132 if ((PSC_I2SCFG_GET_LEN(cfgbits) != params->msbits) ||
133 (params_rate(params) != pscdata->rate))
134 return -EINVAL;
135 } else {
136 /* set sample bitdepth */
137 pscdata->cfg &= ~(0x1f << 4);
138 pscdata->cfg |= PSC_I2SCFG_SET_LEN(params->msbits);
139 /* remember current rate for other stream */
140 pscdata->rate = params_rate(params);
141 }
142 return 0;
143 }
144
145 /* Configure PSC late: on my devel systems the codec is I2S master and
146 * supplies the i2sbitclock __AND__ i2sMclk (!) to the PSC unit. ASoC
147 * uses aggressive PM and switches the codec off when it is not in use
148 * which also means the PSC unit doesn't get any clocks and is therefore
149 * dead. That's why this chunk here gets called from the trigger callback
150 * because I can be reasonably certain the codec is driving the clocks.
151 */
152 static int au1xpsc_i2s_configure(struct au1xpsc_audio_data *pscdata)
153 {
154 unsigned long tmo;
155
156 /* bring PSC out of sleep, and configure I2S unit */
157 au_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
158 au_sync();
159
160 tmo = 1000000;
161 while (!(au_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_SR) && tmo)
162 tmo--;
163
164 if (!tmo)
165 goto psc_err;
166
167 au_writel(0, I2S_CFG(pscdata));
168 au_sync();
169 au_writel(pscdata->cfg | PSC_I2SCFG_DE_ENABLE, I2S_CFG(pscdata));
170 au_sync();
171
172 /* wait for I2S controller to become ready */
173 tmo = 1000000;
174 while (!(au_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_DR) && tmo)
175 tmo--;
176
177 if (tmo)
178 return 0;
179
180 psc_err:
181 au_writel(0, I2S_CFG(pscdata));
182 au_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
183 au_sync();
184 return -ETIMEDOUT;
185 }
186
187 static int au1xpsc_i2s_start(struct au1xpsc_audio_data *pscdata, int stype)
188 {
189 unsigned long tmo, stat;
190 int ret;
191
192 ret = 0;
193
194 /* if both TX and RX are idle, configure the PSC */
195 stat = au_readl(I2S_STAT(pscdata));
196 if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
197 ret = au1xpsc_i2s_configure(pscdata);
198 if (ret)
199 goto out;
200 }
201
202 au_writel(I2SPCR_CLRFIFO(stype), I2S_PCR(pscdata));
203 au_sync();
204 au_writel(I2SPCR_START(stype), I2S_PCR(pscdata));
205 au_sync();
206
207 /* wait for start confirmation */
208 tmo = 1000000;
209 while (!(au_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
210 tmo--;
211
212 if (!tmo) {
213 au_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
214 au_sync();
215 ret = -ETIMEDOUT;
216 }
217 out:
218 return ret;
219 }
220
221 static int au1xpsc_i2s_stop(struct au1xpsc_audio_data *pscdata, int stype)
222 {
223 unsigned long tmo, stat;
224
225 au_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
226 au_sync();
227
228 /* wait for stop confirmation */
229 tmo = 1000000;
230 while ((au_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
231 tmo--;
232
233 /* if both TX and RX are idle, disable PSC */
234 stat = au_readl(I2S_STAT(pscdata));
235 if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
236 au_writel(0, I2S_CFG(pscdata));
237 au_sync();
238 au_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
239 au_sync();
240 }
241 return 0;
242 }
243
244 static int au1xpsc_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
245 struct snd_soc_dai *dai)
246 {
247 struct au1xpsc_audio_data *pscdata = au1xpsc_i2s_workdata;
248 int ret, stype = SUBSTREAM_TYPE(substream);
249
250 switch (cmd) {
251 case SNDRV_PCM_TRIGGER_START:
252 case SNDRV_PCM_TRIGGER_RESUME:
253 ret = au1xpsc_i2s_start(pscdata, stype);
254 break;
255 case SNDRV_PCM_TRIGGER_STOP:
256 case SNDRV_PCM_TRIGGER_SUSPEND:
257 ret = au1xpsc_i2s_stop(pscdata, stype);
258 break;
259 default:
260 ret = -EINVAL;
261 }
262 return ret;
263 }
264
265 static int au1xpsc_i2s_probe(struct platform_device *pdev,
266 struct snd_soc_dai *dai)
267 {
268 return au1xpsc_i2s_workdata ? 0 : -ENODEV;
269 }
270
271 static void au1xpsc_i2s_remove(struct platform_device *pdev,
272 struct snd_soc_dai *dai)
273 {
274 }
275
276 static struct snd_soc_dai_ops au1xpsc_i2s_dai_ops = {
277 .trigger = au1xpsc_i2s_trigger,
278 .hw_params = au1xpsc_i2s_hw_params,
279 .set_fmt = au1xpsc_i2s_set_fmt,
280 };
281
282 struct snd_soc_dai au1xpsc_i2s_dai = {
283 .name = "au1xpsc_i2s",
284 .probe = au1xpsc_i2s_probe,
285 .remove = au1xpsc_i2s_remove,
286 .playback = {
287 .rates = AU1XPSC_I2S_RATES,
288 .formats = AU1XPSC_I2S_FMTS,
289 .channels_min = 2,
290 .channels_max = 8, /* 2 without external help */
291 },
292 .capture = {
293 .rates = AU1XPSC_I2S_RATES,
294 .formats = AU1XPSC_I2S_FMTS,
295 .channels_min = 2,
296 .channels_max = 8, /* 2 without external help */
297 },
298 .ops = &au1xpsc_i2s_dai_ops,
299 };
300 EXPORT_SYMBOL(au1xpsc_i2s_dai);
301
302 static int __init au1xpsc_i2s_drvprobe(struct platform_device *pdev)
303 {
304 struct resource *r;
305 unsigned long sel;
306 int ret;
307 struct au1xpsc_audio_data *wd;
308
309 if (au1xpsc_i2s_workdata)
310 return -EBUSY;
311
312 wd = kzalloc(sizeof(struct au1xpsc_audio_data), GFP_KERNEL);
313 if (!wd)
314 return -ENOMEM;
315
316 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
317 if (!r) {
318 ret = -ENODEV;
319 goto out0;
320 }
321
322 ret = -EBUSY;
323 wd->ioarea = request_mem_region(r->start, r->end - r->start + 1,
324 "au1xpsc_i2s");
325 if (!wd->ioarea)
326 goto out0;
327
328 wd->mmio = ioremap(r->start, 0xffff);
329 if (!wd->mmio)
330 goto out1;
331
332 /* preserve PSC clock source set up by platform (dev.platform_data
333 * is already occupied by soc layer)
334 */
335 sel = au_readl(PSC_SEL(wd)) & PSC_SEL_CLK_MASK;
336 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
337 au_sync();
338 au_writel(PSC_SEL_PS_I2SMODE | sel, PSC_SEL(wd));
339 au_writel(0, I2S_CFG(wd));
340 au_sync();
341
342 /* preconfigure: set max rx/tx fifo depths */
343 wd->cfg |= PSC_I2SCFG_RT_FIFO8 | PSC_I2SCFG_TT_FIFO8;
344
345 /* don't wait for I2S core to become ready now; clocks may not
346 * be running yet; depending on clock input for PSC a wait might
347 * time out.
348 */
349
350 ret = snd_soc_register_dai(&au1xpsc_i2s_dai);
351 if (ret)
352 goto out1;
353
354 /* finally add the DMA device for this PSC */
355 wd->dmapd = au1xpsc_pcm_add(pdev);
356 if (wd->dmapd) {
357 platform_set_drvdata(pdev, wd);
358 au1xpsc_i2s_workdata = wd;
359 return 0;
360 }
361
362 snd_soc_unregister_dai(&au1xpsc_i2s_dai);
363 out1:
364 release_resource(wd->ioarea);
365 kfree(wd->ioarea);
366 out0:
367 kfree(wd);
368 return ret;
369 }
370
371 static int __devexit au1xpsc_i2s_drvremove(struct platform_device *pdev)
372 {
373 struct au1xpsc_audio_data *wd = platform_get_drvdata(pdev);
374
375 if (wd->dmapd)
376 au1xpsc_pcm_destroy(wd->dmapd);
377
378 snd_soc_unregister_dai(&au1xpsc_i2s_dai);
379
380 au_writel(0, I2S_CFG(wd));
381 au_sync();
382 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
383 au_sync();
384
385 iounmap(wd->mmio);
386 release_resource(wd->ioarea);
387 kfree(wd->ioarea);
388 kfree(wd);
389
390 au1xpsc_i2s_workdata = NULL; /* MDEV */
391
392 return 0;
393 }
394
395 #ifdef CONFIG_PM
396 static int au1xpsc_i2s_drvsuspend(struct device *dev)
397 {
398 struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
399
400 /* save interesting register and disable PSC */
401 wd->pm[0] = au_readl(PSC_SEL(wd));
402
403 au_writel(0, I2S_CFG(wd));
404 au_sync();
405 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
406 au_sync();
407
408 return 0;
409 }
410
411 static int au1xpsc_i2s_drvresume(struct device *dev)
412 {
413 struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
414
415 /* select I2S mode and PSC clock */
416 au_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
417 au_sync();
418 au_writel(0, PSC_SEL(wd));
419 au_sync();
420 au_writel(wd->pm[0], PSC_SEL(wd));
421 au_sync();
422
423 return 0;
424 }
425
426 static struct dev_pm_ops au1xpsci2s_pmops = {
427 .suspend = au1xpsc_i2s_drvsuspend,
428 .resume = au1xpsc_i2s_drvresume,
429 };
430
431 #define AU1XPSCI2S_PMOPS &au1xpsci2s_pmops
432
433 #else
434
435 #define AU1XPSCI2S_PMOPS NULL
436
437 #endif
438
439 static struct platform_driver au1xpsc_i2s_driver = {
440 .driver = {
441 .name = "au1xpsc_i2s",
442 .owner = THIS_MODULE,
443 .pm = AU1XPSCI2S_PMOPS,
444 },
445 .probe = au1xpsc_i2s_drvprobe,
446 .remove = __devexit_p(au1xpsc_i2s_drvremove),
447 };
448
449 static int __init au1xpsc_i2s_load(void)
450 {
451 au1xpsc_i2s_workdata = NULL;
452 return platform_driver_register(&au1xpsc_i2s_driver);
453 }
454
455 static void __exit au1xpsc_i2s_unload(void)
456 {
457 platform_driver_unregister(&au1xpsc_i2s_driver);
458 }
459
460 module_init(au1xpsc_i2s_load);
461 module_exit(au1xpsc_i2s_unload);
462
463 MODULE_LICENSE("GPL");
464 MODULE_DESCRIPTION("Au12x0/Au1550 PSC I2S ALSA ASoC audio driver");
465 MODULE_AUTHOR("Manuel Lauss");