drm/i915: fix port checks for MST support on gen >= 11
[linux/fpc-iii.git] / sound / soc / cirrus / ep93xx-i2s.c
blob0918c5da575aba1feedb4cf73f23cbdc7d380940
1 /*
2 * linux/sound/soc/ep93xx-i2s.c
3 * EP93xx I2S driver
5 * Copyright (C) 2010 Ryan Mallon
7 * Based on the original driver by:
8 * Copyright (C) 2007 Chase Douglas <chasedouglas@gmail>
9 * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/clk.h>
21 #include <linux/io.h>
23 #include <sound/core.h>
24 #include <sound/dmaengine_pcm.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/initval.h>
28 #include <sound/soc.h>
30 #include <mach/hardware.h>
31 #include <mach/ep93xx-regs.h>
32 #include <linux/platform_data/dma-ep93xx.h>
34 #include "ep93xx-pcm.h"
36 #define EP93XX_I2S_TXCLKCFG 0x00
37 #define EP93XX_I2S_RXCLKCFG 0x04
38 #define EP93XX_I2S_GLSTS 0x08
39 #define EP93XX_I2S_GLCTRL 0x0C
41 #define EP93XX_I2S_I2STX0LFT 0x10
42 #define EP93XX_I2S_I2STX0RT 0x14
44 #define EP93XX_I2S_TXLINCTRLDATA 0x28
45 #define EP93XX_I2S_TXCTRL 0x2C
46 #define EP93XX_I2S_TXWRDLEN 0x30
47 #define EP93XX_I2S_TX0EN 0x34
49 #define EP93XX_I2S_RXLINCTRLDATA 0x58
50 #define EP93XX_I2S_RXCTRL 0x5C
51 #define EP93XX_I2S_RXWRDLEN 0x60
52 #define EP93XX_I2S_RX0EN 0x64
54 #define EP93XX_I2S_WRDLEN_16 (0 << 0)
55 #define EP93XX_I2S_WRDLEN_24 (1 << 0)
56 #define EP93XX_I2S_WRDLEN_32 (2 << 0)
58 #define EP93XX_I2S_RXLINCTRLDATA_R_JUST BIT(1) /* Right justify */
60 #define EP93XX_I2S_TXLINCTRLDATA_R_JUST BIT(2) /* Right justify */
63 * Transmit empty interrupt level select:
64 * 0 - Generate interrupt when FIFO is half empty
65 * 1 - Generate interrupt when FIFO is empty
67 #define EP93XX_I2S_TXCTRL_TXEMPTY_LVL BIT(0)
68 #define EP93XX_I2S_TXCTRL_TXUFIE BIT(1) /* Transmit interrupt enable */
70 #define EP93XX_I2S_CLKCFG_LRS (1 << 0) /* lrclk polarity */
71 #define EP93XX_I2S_CLKCFG_CKP (1 << 1) /* Bit clock polarity */
72 #define EP93XX_I2S_CLKCFG_REL (1 << 2) /* First bit transition */
73 #define EP93XX_I2S_CLKCFG_MASTER (1 << 3) /* Master mode */
74 #define EP93XX_I2S_CLKCFG_NBCG (1 << 4) /* Not bit clock gating */
76 #define EP93XX_I2S_GLSTS_TX0_FIFO_FULL BIT(12)
78 struct ep93xx_i2s_info {
79 struct clk *mclk;
80 struct clk *sclk;
81 struct clk *lrclk;
82 void __iomem *regs;
83 struct snd_dmaengine_dai_dma_data dma_params_rx;
84 struct snd_dmaengine_dai_dma_data dma_params_tx;
87 static struct ep93xx_dma_data ep93xx_i2s_dma_data[] = {
88 [SNDRV_PCM_STREAM_PLAYBACK] = {
89 .name = "i2s-pcm-out",
90 .port = EP93XX_DMA_I2S1,
91 .direction = DMA_MEM_TO_DEV,
93 [SNDRV_PCM_STREAM_CAPTURE] = {
94 .name = "i2s-pcm-in",
95 .port = EP93XX_DMA_I2S1,
96 .direction = DMA_DEV_TO_MEM,
100 static inline void ep93xx_i2s_write_reg(struct ep93xx_i2s_info *info,
101 unsigned reg, unsigned val)
103 __raw_writel(val, info->regs + reg);
106 static inline unsigned ep93xx_i2s_read_reg(struct ep93xx_i2s_info *info,
107 unsigned reg)
109 return __raw_readl(info->regs + reg);
112 static void ep93xx_i2s_enable(struct ep93xx_i2s_info *info, int stream)
114 unsigned base_reg;
116 if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
117 (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
118 /* Enable clocks */
119 clk_enable(info->mclk);
120 clk_enable(info->sclk);
121 clk_enable(info->lrclk);
123 /* Enable i2s */
124 ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 1);
127 /* Enable fifo */
128 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
129 base_reg = EP93XX_I2S_TX0EN;
130 else
131 base_reg = EP93XX_I2S_RX0EN;
132 ep93xx_i2s_write_reg(info, base_reg, 1);
134 /* Enable TX IRQs (FIFO empty or underflow) */
135 if (IS_ENABLED(CONFIG_SND_EP93XX_SOC_I2S_WATCHDOG) &&
136 stream == SNDRV_PCM_STREAM_PLAYBACK)
137 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCTRL,
138 EP93XX_I2S_TXCTRL_TXEMPTY_LVL |
139 EP93XX_I2S_TXCTRL_TXUFIE);
142 static void ep93xx_i2s_disable(struct ep93xx_i2s_info *info, int stream)
144 unsigned base_reg;
146 /* Disable IRQs */
147 if (IS_ENABLED(CONFIG_SND_EP93XX_SOC_I2S_WATCHDOG) &&
148 stream == SNDRV_PCM_STREAM_PLAYBACK)
149 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCTRL, 0);
151 /* Disable fifo */
152 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
153 base_reg = EP93XX_I2S_TX0EN;
154 else
155 base_reg = EP93XX_I2S_RX0EN;
156 ep93xx_i2s_write_reg(info, base_reg, 0);
158 if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
159 (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
160 /* Disable i2s */
161 ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 0);
163 /* Disable clocks */
164 clk_disable(info->lrclk);
165 clk_disable(info->sclk);
166 clk_disable(info->mclk);
171 * According to documentation I2S controller can handle underflow conditions
172 * just fine, but in reality the state machine is sometimes confused so that
173 * the whole stream is shifted by one byte. The watchdog below disables the TX
174 * FIFO, fills the buffer with zeroes and re-enables the FIFO. State machine
175 * is being reset and by filling the buffer we get some time before next
176 * underflow happens.
178 static irqreturn_t ep93xx_i2s_interrupt(int irq, void *dev_id)
180 struct ep93xx_i2s_info *info = dev_id;
182 /* Disable FIFO */
183 ep93xx_i2s_write_reg(info, EP93XX_I2S_TX0EN, 0);
185 * Fill TX FIFO with zeroes, this way we can defer next IRQs as much as
186 * possible and get more time for DMA to catch up. Actually there are
187 * only 8 samples in this FIFO, so even on 8kHz maximum deferral here is
188 * 1ms.
190 while (!(ep93xx_i2s_read_reg(info, EP93XX_I2S_GLSTS) &
191 EP93XX_I2S_GLSTS_TX0_FIFO_FULL)) {
192 ep93xx_i2s_write_reg(info, EP93XX_I2S_I2STX0LFT, 0);
193 ep93xx_i2s_write_reg(info, EP93XX_I2S_I2STX0RT, 0);
195 /* Re-enable FIFO */
196 ep93xx_i2s_write_reg(info, EP93XX_I2S_TX0EN, 1);
198 return IRQ_HANDLED;
201 static int ep93xx_i2s_dai_probe(struct snd_soc_dai *dai)
203 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
205 info->dma_params_tx.filter_data =
206 &ep93xx_i2s_dma_data[SNDRV_PCM_STREAM_PLAYBACK];
207 info->dma_params_rx.filter_data =
208 &ep93xx_i2s_dma_data[SNDRV_PCM_STREAM_CAPTURE];
210 dai->playback_dma_data = &info->dma_params_tx;
211 dai->capture_dma_data = &info->dma_params_rx;
213 return 0;
216 static void ep93xx_i2s_shutdown(struct snd_pcm_substream *substream,
217 struct snd_soc_dai *dai)
219 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
221 ep93xx_i2s_disable(info, substream->stream);
224 static int ep93xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
225 unsigned int fmt)
227 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(cpu_dai);
228 unsigned int clk_cfg;
229 unsigned int txlin_ctrl = 0;
230 unsigned int rxlin_ctrl = 0;
232 clk_cfg = ep93xx_i2s_read_reg(info, EP93XX_I2S_RXCLKCFG);
234 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
235 case SND_SOC_DAIFMT_I2S:
236 clk_cfg |= EP93XX_I2S_CLKCFG_REL;
237 break;
239 case SND_SOC_DAIFMT_LEFT_J:
240 clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
241 break;
243 case SND_SOC_DAIFMT_RIGHT_J:
244 clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
245 rxlin_ctrl |= EP93XX_I2S_RXLINCTRLDATA_R_JUST;
246 txlin_ctrl |= EP93XX_I2S_TXLINCTRLDATA_R_JUST;
247 break;
249 default:
250 return -EINVAL;
253 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
254 case SND_SOC_DAIFMT_CBS_CFS:
255 /* CPU is master */
256 clk_cfg |= EP93XX_I2S_CLKCFG_MASTER;
257 break;
259 case SND_SOC_DAIFMT_CBM_CFM:
260 /* Codec is master */
261 clk_cfg &= ~EP93XX_I2S_CLKCFG_MASTER;
262 break;
264 default:
265 return -EINVAL;
268 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
269 case SND_SOC_DAIFMT_NB_NF:
270 /* Negative bit clock, lrclk low on left word */
271 clk_cfg &= ~(EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_LRS);
272 break;
274 case SND_SOC_DAIFMT_NB_IF:
275 /* Negative bit clock, lrclk low on right word */
276 clk_cfg &= ~EP93XX_I2S_CLKCFG_CKP;
277 clk_cfg |= EP93XX_I2S_CLKCFG_LRS;
278 break;
280 case SND_SOC_DAIFMT_IB_NF:
281 /* Positive bit clock, lrclk low on left word */
282 clk_cfg |= EP93XX_I2S_CLKCFG_CKP;
283 clk_cfg &= ~EP93XX_I2S_CLKCFG_LRS;
284 break;
286 case SND_SOC_DAIFMT_IB_IF:
287 /* Positive bit clock, lrclk low on right word */
288 clk_cfg |= EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_LRS;
289 break;
292 /* Write new register values */
293 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXCLKCFG, clk_cfg);
294 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCLKCFG, clk_cfg);
295 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXLINCTRLDATA, rxlin_ctrl);
296 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXLINCTRLDATA, txlin_ctrl);
297 return 0;
300 static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
301 struct snd_pcm_hw_params *params,
302 struct snd_soc_dai *dai)
304 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
305 unsigned word_len, div, sdiv, lrdiv;
306 int err;
308 switch (params_format(params)) {
309 case SNDRV_PCM_FORMAT_S16_LE:
310 word_len = EP93XX_I2S_WRDLEN_16;
311 break;
313 case SNDRV_PCM_FORMAT_S24_LE:
314 word_len = EP93XX_I2S_WRDLEN_24;
315 break;
317 case SNDRV_PCM_FORMAT_S32_LE:
318 word_len = EP93XX_I2S_WRDLEN_32;
319 break;
321 default:
322 return -EINVAL;
325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
326 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXWRDLEN, word_len);
327 else
328 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXWRDLEN, word_len);
331 * EP93xx I2S module can be setup so SCLK / LRCLK value can be
332 * 32, 64, 128. MCLK / SCLK value can be 2 and 4.
333 * We set LRCLK equal to `rate' and minimum SCLK / LRCLK
334 * value is 64, because our sample size is 32 bit * 2 channels.
335 * I2S standard permits us to transmit more bits than
336 * the codec uses.
338 div = clk_get_rate(info->mclk) / params_rate(params);
339 sdiv = 4;
340 if (div > (256 + 512) / 2) {
341 lrdiv = 128;
342 } else {
343 lrdiv = 64;
344 if (div < (128 + 256) / 2)
345 sdiv = 2;
348 err = clk_set_rate(info->sclk, clk_get_rate(info->mclk) / sdiv);
349 if (err)
350 return err;
352 err = clk_set_rate(info->lrclk, clk_get_rate(info->sclk) / lrdiv);
353 if (err)
354 return err;
356 ep93xx_i2s_enable(info, substream->stream);
357 return 0;
360 static int ep93xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
361 unsigned int freq, int dir)
363 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(cpu_dai);
365 if (dir == SND_SOC_CLOCK_IN || clk_id != 0)
366 return -EINVAL;
368 return clk_set_rate(info->mclk, freq);
371 #ifdef CONFIG_PM
372 static int ep93xx_i2s_suspend(struct snd_soc_dai *dai)
374 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
376 if (!dai->active)
377 return 0;
379 ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_PLAYBACK);
380 ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_CAPTURE);
382 return 0;
385 static int ep93xx_i2s_resume(struct snd_soc_dai *dai)
387 struct ep93xx_i2s_info *info = snd_soc_dai_get_drvdata(dai);
389 if (!dai->active)
390 return 0;
392 ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_PLAYBACK);
393 ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_CAPTURE);
395 return 0;
397 #else
398 #define ep93xx_i2s_suspend NULL
399 #define ep93xx_i2s_resume NULL
400 #endif
402 static const struct snd_soc_dai_ops ep93xx_i2s_dai_ops = {
403 .shutdown = ep93xx_i2s_shutdown,
404 .hw_params = ep93xx_i2s_hw_params,
405 .set_sysclk = ep93xx_i2s_set_sysclk,
406 .set_fmt = ep93xx_i2s_set_dai_fmt,
409 #define EP93XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
411 static struct snd_soc_dai_driver ep93xx_i2s_dai = {
412 .symmetric_rates= 1,
413 .probe = ep93xx_i2s_dai_probe,
414 .suspend = ep93xx_i2s_suspend,
415 .resume = ep93xx_i2s_resume,
416 .playback = {
417 .channels_min = 2,
418 .channels_max = 2,
419 .rates = SNDRV_PCM_RATE_8000_192000,
420 .formats = EP93XX_I2S_FORMATS,
422 .capture = {
423 .channels_min = 2,
424 .channels_max = 2,
425 .rates = SNDRV_PCM_RATE_8000_192000,
426 .formats = EP93XX_I2S_FORMATS,
428 .ops = &ep93xx_i2s_dai_ops,
431 static const struct snd_soc_component_driver ep93xx_i2s_component = {
432 .name = "ep93xx-i2s",
435 static int ep93xx_i2s_probe(struct platform_device *pdev)
437 struct ep93xx_i2s_info *info;
438 struct resource *res;
439 int err;
441 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
442 if (!info)
443 return -ENOMEM;
445 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
446 info->regs = devm_ioremap_resource(&pdev->dev, res);
447 if (IS_ERR(info->regs))
448 return PTR_ERR(info->regs);
450 if (IS_ENABLED(CONFIG_SND_EP93XX_SOC_I2S_WATCHDOG)) {
451 int irq = platform_get_irq(pdev, 0);
452 if (irq <= 0)
453 return irq < 0 ? irq : -ENODEV;
455 err = devm_request_irq(&pdev->dev, irq, ep93xx_i2s_interrupt, 0,
456 pdev->name, info);
457 if (err)
458 return err;
461 info->mclk = clk_get(&pdev->dev, "mclk");
462 if (IS_ERR(info->mclk)) {
463 err = PTR_ERR(info->mclk);
464 goto fail;
467 info->sclk = clk_get(&pdev->dev, "sclk");
468 if (IS_ERR(info->sclk)) {
469 err = PTR_ERR(info->sclk);
470 goto fail_put_mclk;
473 info->lrclk = clk_get(&pdev->dev, "lrclk");
474 if (IS_ERR(info->lrclk)) {
475 err = PTR_ERR(info->lrclk);
476 goto fail_put_sclk;
479 dev_set_drvdata(&pdev->dev, info);
481 err = snd_soc_register_component(&pdev->dev, &ep93xx_i2s_component,
482 &ep93xx_i2s_dai, 1);
483 if (err)
484 goto fail_put_lrclk;
486 err = devm_ep93xx_pcm_platform_register(&pdev->dev);
487 if (err)
488 goto fail_unregister;
490 return 0;
492 fail_unregister:
493 snd_soc_unregister_component(&pdev->dev);
494 fail_put_lrclk:
495 clk_put(info->lrclk);
496 fail_put_sclk:
497 clk_put(info->sclk);
498 fail_put_mclk:
499 clk_put(info->mclk);
500 fail:
501 return err;
504 static int ep93xx_i2s_remove(struct platform_device *pdev)
506 struct ep93xx_i2s_info *info = dev_get_drvdata(&pdev->dev);
508 snd_soc_unregister_component(&pdev->dev);
509 clk_put(info->lrclk);
510 clk_put(info->sclk);
511 clk_put(info->mclk);
512 return 0;
515 static struct platform_driver ep93xx_i2s_driver = {
516 .probe = ep93xx_i2s_probe,
517 .remove = ep93xx_i2s_remove,
518 .driver = {
519 .name = "ep93xx-i2s",
523 module_platform_driver(ep93xx_i2s_driver);
525 MODULE_ALIAS("platform:ep93xx-i2s");
526 MODULE_AUTHOR("Ryan Mallon");
527 MODULE_DESCRIPTION("EP93XX I2S driver");
528 MODULE_LICENSE("GPL");