blob: 0db69b7e96170e471fe3101982a1cfeaa50dfb60 [file] [log] [blame]
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +05301/*
2 * ALSA SoC Synopsys I2S Audio Layer
3 *
Rajeev Kumar22a4adf2013-06-11 09:29:08 +05304 * sound/soc/dwc/designware_i2s.c
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +05305 *
6 * Copyright (C) 2010 ST Microelectronics
Rajeev Kumar9a302c32014-09-05 16:47:04 +05307 * Rajeev Kumar <rajeevkumar.linux@gmail.com>
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +05308 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
14#include <linux/clk.h>
15#include <linux/device.h>
16#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/slab.h>
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -050021#include <linux/pm_runtime.h>
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +053022#include <sound/designware_i2s.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
Andrew Jackson0d274542014-12-30 10:55:48 +000026#include <sound/dmaengine_pcm.h>
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +053027
28/* common register for all channel */
29#define IER 0x000
30#define IRER 0x004
31#define ITER 0x008
32#define CER 0x00C
33#define CCR 0x010
34#define RXFFR 0x014
35#define TXFFR 0x018
36
37/* I2STxRxRegisters for all channels */
38#define LRBR_LTHR(x) (0x40 * x + 0x020)
39#define RRBR_RTHR(x) (0x40 * x + 0x024)
40#define RER(x) (0x40 * x + 0x028)
41#define TER(x) (0x40 * x + 0x02C)
42#define RCR(x) (0x40 * x + 0x030)
43#define TCR(x) (0x40 * x + 0x034)
44#define ISR(x) (0x40 * x + 0x038)
45#define IMR(x) (0x40 * x + 0x03C)
46#define ROR(x) (0x40 * x + 0x040)
47#define TOR(x) (0x40 * x + 0x044)
48#define RFCR(x) (0x40 * x + 0x048)
49#define TFCR(x) (0x40 * x + 0x04C)
50#define RFF(x) (0x40 * x + 0x050)
51#define TFF(x) (0x40 * x + 0x054)
52
53/* I2SCOMPRegisters */
54#define I2S_COMP_PARAM_2 0x01F0
55#define I2S_COMP_PARAM_1 0x01F4
56#define I2S_COMP_VERSION 0x01F8
57#define I2S_COMP_TYPE 0x01FC
58
Andrew Jacksonb226efe2014-12-30 10:55:45 +000059/*
60 * Component parameter register fields - define the I2S block's
61 * configuration.
62 */
63#define COMP1_TX_WORDSIZE_3(r) (((r) & GENMASK(27, 25)) >> 25)
64#define COMP1_TX_WORDSIZE_2(r) (((r) & GENMASK(24, 22)) >> 22)
65#define COMP1_TX_WORDSIZE_1(r) (((r) & GENMASK(21, 19)) >> 19)
66#define COMP1_TX_WORDSIZE_0(r) (((r) & GENMASK(18, 16)) >> 16)
67#define COMP1_TX_CHANNELS(r) (((r) & GENMASK(10, 9)) >> 9)
68#define COMP1_RX_CHANNELS(r) (((r) & GENMASK(8, 7)) >> 7)
69#define COMP1_RX_ENABLED(r) (((r) & BIT(6)) >> 6)
70#define COMP1_TX_ENABLED(r) (((r) & BIT(5)) >> 5)
71#define COMP1_MODE_EN(r) (((r) & BIT(4)) >> 4)
72#define COMP1_FIFO_DEPTH_GLOBAL(r) (((r) & GENMASK(3, 2)) >> 2)
73#define COMP1_APB_DATA_WIDTH(r) (((r) & GENMASK(1, 0)) >> 0)
74
75#define COMP2_RX_WORDSIZE_3(r) (((r) & GENMASK(12, 10)) >> 10)
76#define COMP2_RX_WORDSIZE_2(r) (((r) & GENMASK(9, 7)) >> 7)
77#define COMP2_RX_WORDSIZE_1(r) (((r) & GENMASK(5, 3)) >> 3)
78#define COMP2_RX_WORDSIZE_0(r) (((r) & GENMASK(2, 0)) >> 0)
79
80/* Number of entries in WORDSIZE and DATA_WIDTH parameter registers */
81#define COMP_MAX_WORDSIZE (1 << 3)
82#define COMP_MAX_DATA_WIDTH (1 << 2)
83
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +053084#define MAX_CHANNEL_NUM 8
85#define MIN_CHANNEL_NUM 2
86
Andrew Jackson0d274542014-12-30 10:55:48 +000087union dw_i2s_snd_dma_data {
88 struct i2s_dma_data pd;
89 struct snd_dmaengine_dai_dma_data dt;
90};
91
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +053092struct dw_i2s_dev {
93 void __iomem *i2s_base;
94 struct clk *clk;
95 int active;
96 unsigned int capability;
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -050097 unsigned int quirks;
98 unsigned int i2s_reg_comp1;
99 unsigned int i2s_reg_comp2;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530100 struct device *dev;
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500101 u32 ccr;
102 u32 xfer_resolution;
Jose Abreu3fafd142016-04-07 17:53:57 +0100103 u32 fifo_th;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530104
105 /* data related to DMA transfers b/w i2s and DMAC */
Andrew Jackson0d274542014-12-30 10:55:48 +0000106 union dw_i2s_snd_dma_data play_dma_data;
107 union dw_i2s_snd_dma_data capture_dma_data;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530108 struct i2s_clk_config_data config;
109 int (*i2s_clk_cfg)(struct i2s_clk_config_data *config);
110};
111
Mark Brown6b4a21b2012-06-28 13:11:47 +0100112static inline void i2s_write_reg(void __iomem *io_base, int reg, u32 val)
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530113{
114 writel(val, io_base + reg);
115}
116
Mark Brown6b4a21b2012-06-28 13:11:47 +0100117static inline u32 i2s_read_reg(void __iomem *io_base, int reg)
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530118{
119 return readl(io_base + reg);
120}
121
122static inline void i2s_disable_channels(struct dw_i2s_dev *dev, u32 stream)
123{
124 u32 i = 0;
125
126 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
127 for (i = 0; i < 4; i++)
128 i2s_write_reg(dev->i2s_base, TER(i), 0);
129 } else {
130 for (i = 0; i < 4; i++)
131 i2s_write_reg(dev->i2s_base, RER(i), 0);
132 }
133}
134
135static inline void i2s_clear_irqs(struct dw_i2s_dev *dev, u32 stream)
136{
137 u32 i = 0;
138
139 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
140 for (i = 0; i < 4; i++)
Yitian Bu48738672015-10-02 15:18:41 +0800141 i2s_read_reg(dev->i2s_base, TOR(i));
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530142 } else {
143 for (i = 0; i < 4; i++)
Yitian Bu48738672015-10-02 15:18:41 +0800144 i2s_read_reg(dev->i2s_base, ROR(i));
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530145 }
146}
147
Mark Brown1520ffd2012-07-04 19:04:11 +0100148static void i2s_start(struct dw_i2s_dev *dev,
149 struct snd_pcm_substream *substream)
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530150{
Jose Abreu613c7c42016-04-05 18:08:02 +0100151 struct i2s_clk_config_data *config = &dev->config;
yitian924eb472015-09-29 22:43:17 +0800152 u32 i, irq;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530153 i2s_write_reg(dev->i2s_base, IER, 1);
154
yitian924eb472015-09-29 22:43:17 +0800155 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Jose Abreu613c7c42016-04-05 18:08:02 +0100156 for (i = 0; i < (config->chan_nr / 2); i++) {
yitian924eb472015-09-29 22:43:17 +0800157 irq = i2s_read_reg(dev->i2s_base, IMR(i));
158 i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x30);
159 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530160 i2s_write_reg(dev->i2s_base, ITER, 1);
yitian924eb472015-09-29 22:43:17 +0800161 } else {
Jose Abreu613c7c42016-04-05 18:08:02 +0100162 for (i = 0; i < (config->chan_nr / 2); i++) {
yitian924eb472015-09-29 22:43:17 +0800163 irq = i2s_read_reg(dev->i2s_base, IMR(i));
164 i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x03);
165 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530166 i2s_write_reg(dev->i2s_base, IRER, 1);
yitian924eb472015-09-29 22:43:17 +0800167 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530168
169 i2s_write_reg(dev->i2s_base, CER, 1);
170}
171
172static void i2s_stop(struct dw_i2s_dev *dev,
173 struct snd_pcm_substream *substream)
174{
175 u32 i = 0, irq;
176
177 i2s_clear_irqs(dev, substream->stream);
178 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
179 i2s_write_reg(dev->i2s_base, ITER, 0);
180
181 for (i = 0; i < 4; i++) {
182 irq = i2s_read_reg(dev->i2s_base, IMR(i));
183 i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x30);
184 }
185 } else {
186 i2s_write_reg(dev->i2s_base, IRER, 0);
187
188 for (i = 0; i < 4; i++) {
189 irq = i2s_read_reg(dev->i2s_base, IMR(i));
190 i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x03);
191 }
192 }
193
194 if (!dev->active) {
195 i2s_write_reg(dev->i2s_base, CER, 0);
196 i2s_write_reg(dev->i2s_base, IER, 0);
197 }
198}
199
200static int dw_i2s_startup(struct snd_pcm_substream *substream,
201 struct snd_soc_dai *cpu_dai)
202{
203 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Andrew Jackson0d274542014-12-30 10:55:48 +0000204 union dw_i2s_snd_dma_data *dma_data = NULL;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530205
206 if (!(dev->capability & DWC_I2S_RECORD) &&
207 (substream->stream == SNDRV_PCM_STREAM_CAPTURE))
208 return -EINVAL;
209
210 if (!(dev->capability & DWC_I2S_PLAY) &&
211 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
212 return -EINVAL;
213
214 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
215 dma_data = &dev->play_dma_data;
216 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
217 dma_data = &dev->capture_dma_data;
218
219 snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)dma_data);
220
221 return 0;
222}
223
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500224static void dw_i2s_config(struct dw_i2s_dev *dev, int stream)
225{
226 u32 ch_reg, irq;
227 struct i2s_clk_config_data *config = &dev->config;
228
229
230 i2s_disable_channels(dev, stream);
231
232 for (ch_reg = 0; ch_reg < (config->chan_nr / 2); ch_reg++) {
233 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
234 i2s_write_reg(dev->i2s_base, TCR(ch_reg),
235 dev->xfer_resolution);
Jose Abreu3fafd142016-04-07 17:53:57 +0100236 i2s_write_reg(dev->i2s_base, TFCR(ch_reg),
237 dev->fifo_th - 1);
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500238 irq = i2s_read_reg(dev->i2s_base, IMR(ch_reg));
239 i2s_write_reg(dev->i2s_base, IMR(ch_reg), irq & ~0x30);
240 i2s_write_reg(dev->i2s_base, TER(ch_reg), 1);
241 } else {
242 i2s_write_reg(dev->i2s_base, RCR(ch_reg),
243 dev->xfer_resolution);
Jose Abreu3fafd142016-04-07 17:53:57 +0100244 i2s_write_reg(dev->i2s_base, RFCR(ch_reg),
245 dev->fifo_th - 1);
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500246 irq = i2s_read_reg(dev->i2s_base, IMR(ch_reg));
247 i2s_write_reg(dev->i2s_base, IMR(ch_reg), irq & ~0x03);
248 i2s_write_reg(dev->i2s_base, RER(ch_reg), 1);
249 }
250
251 }
252}
253
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530254static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
255 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
256{
257 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
258 struct i2s_clk_config_data *config = &dev->config;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530259 int ret;
260
261 switch (params_format(params)) {
262 case SNDRV_PCM_FORMAT_S16_LE:
263 config->data_width = 16;
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500264 dev->ccr = 0x00;
265 dev->xfer_resolution = 0x02;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530266 break;
267
268 case SNDRV_PCM_FORMAT_S24_LE:
269 config->data_width = 24;
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500270 dev->ccr = 0x08;
271 dev->xfer_resolution = 0x04;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530272 break;
273
274 case SNDRV_PCM_FORMAT_S32_LE:
275 config->data_width = 32;
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500276 dev->ccr = 0x10;
277 dev->xfer_resolution = 0x05;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530278 break;
279
280 default:
281 dev_err(dev->dev, "designware-i2s: unsuppted PCM fmt");
282 return -EINVAL;
283 }
284
285 config->chan_nr = params_channels(params);
286
287 switch (config->chan_nr) {
288 case EIGHT_CHANNEL_SUPPORT:
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530289 case SIX_CHANNEL_SUPPORT:
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530290 case FOUR_CHANNEL_SUPPORT:
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530291 case TWO_CHANNEL_SUPPORT:
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530292 break;
293 default:
294 dev_err(dev->dev, "channel not supported\n");
Dan Carpenter0099d242013-01-25 09:43:43 +0300295 return -EINVAL;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530296 }
297
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500298 dw_i2s_config(dev, substream->stream);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530299
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500300 i2s_write_reg(dev->i2s_base, CCR, dev->ccr);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530301
302 config->sample_rate = params_rate(params);
303
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400304 if (dev->capability & DW_I2S_MASTER) {
305 if (dev->i2s_clk_cfg) {
306 ret = dev->i2s_clk_cfg(config);
307 if (ret < 0) {
308 dev_err(dev->dev, "runtime audio clk config fail\n");
309 return ret;
310 }
311 } else {
312 u32 bitclk = config->sample_rate *
313 config->data_width * 2;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530314
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400315 ret = clk_set_rate(dev->clk, bitclk);
316 if (ret) {
317 dev_err(dev->dev, "Can't set I2S clock rate: %d\n",
318 ret);
319 return ret;
320 }
Andrew Jackson0d274542014-12-30 10:55:48 +0000321 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530322 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530323 return 0;
324}
325
326static void dw_i2s_shutdown(struct snd_pcm_substream *substream,
327 struct snd_soc_dai *dai)
328{
329 snd_soc_dai_set_dma_data(dai, substream, NULL);
330}
331
Andrew Jackson3475c3d2014-12-19 16:18:05 +0000332static int dw_i2s_prepare(struct snd_pcm_substream *substream,
333 struct snd_soc_dai *dai)
334{
335 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
336
337 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
338 i2s_write_reg(dev->i2s_base, TXFFR, 1);
339 else
340 i2s_write_reg(dev->i2s_base, RXFFR, 1);
341
342 return 0;
343}
344
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530345static int dw_i2s_trigger(struct snd_pcm_substream *substream,
346 int cmd, struct snd_soc_dai *dai)
347{
348 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
349 int ret = 0;
350
351 switch (cmd) {
352 case SNDRV_PCM_TRIGGER_START:
353 case SNDRV_PCM_TRIGGER_RESUME:
354 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
355 dev->active++;
356 i2s_start(dev, substream);
357 break;
358
359 case SNDRV_PCM_TRIGGER_STOP:
360 case SNDRV_PCM_TRIGGER_SUSPEND:
361 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
362 dev->active--;
363 i2s_stop(dev, substream);
364 break;
365 default:
366 ret = -EINVAL;
367 break;
368 }
369 return ret;
370}
371
Maruthi Srinivas Bayyavarapuab57b8e2015-10-23 17:15:41 -0400372static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
373{
374 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
375 int ret = 0;
376
377 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
378 case SND_SOC_DAIFMT_CBM_CFM:
379 if (dev->capability & DW_I2S_SLAVE)
380 ret = 0;
381 else
382 ret = -EINVAL;
383 break;
384 case SND_SOC_DAIFMT_CBS_CFS:
385 if (dev->capability & DW_I2S_MASTER)
386 ret = 0;
387 else
388 ret = -EINVAL;
389 break;
390 case SND_SOC_DAIFMT_CBM_CFS:
391 case SND_SOC_DAIFMT_CBS_CFM:
392 ret = -EINVAL;
393 break;
394 default:
395 dev_dbg(dev->dev, "dwc : Invalid master/slave format\n");
396 ret = -EINVAL;
397 break;
398 }
399 return ret;
400}
401
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530402static struct snd_soc_dai_ops dw_i2s_dai_ops = {
403 .startup = dw_i2s_startup,
404 .shutdown = dw_i2s_shutdown,
405 .hw_params = dw_i2s_hw_params,
Andrew Jackson3475c3d2014-12-19 16:18:05 +0000406 .prepare = dw_i2s_prepare,
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530407 .trigger = dw_i2s_trigger,
Maruthi Srinivas Bayyavarapuab57b8e2015-10-23 17:15:41 -0400408 .set_fmt = dw_i2s_set_fmt,
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530409};
410
Kuninori Morimoto92eaa322013-03-21 03:31:30 -0700411static const struct snd_soc_component_driver dw_i2s_component = {
412 .name = "dw-i2s",
413};
414
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530415#ifdef CONFIG_PM
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500416static int dw_i2s_runtime_suspend(struct device *dev)
417{
418 struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
419
420 if (dw_dev->capability & DW_I2S_MASTER)
421 clk_disable(dw_dev->clk);
422 return 0;
423}
424
425static int dw_i2s_runtime_resume(struct device *dev)
426{
427 struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
428
429 if (dw_dev->capability & DW_I2S_MASTER)
430 clk_enable(dw_dev->clk);
431 return 0;
432}
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530433
434static int dw_i2s_suspend(struct snd_soc_dai *dai)
435{
436 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
437
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400438 if (dev->capability & DW_I2S_MASTER)
439 clk_disable(dev->clk);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530440 return 0;
441}
442
443static int dw_i2s_resume(struct snd_soc_dai *dai)
444{
445 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
446
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400447 if (dev->capability & DW_I2S_MASTER)
448 clk_enable(dev->clk);
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500449
450 if (dai->playback_active)
451 dw_i2s_config(dev, SNDRV_PCM_STREAM_PLAYBACK);
452 if (dai->capture_active)
453 dw_i2s_config(dev, SNDRV_PCM_STREAM_CAPTURE);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530454 return 0;
455}
456
457#else
458#define dw_i2s_suspend NULL
459#define dw_i2s_resume NULL
460#endif
461
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000462/*
463 * The following tables allow a direct lookup of various parameters
464 * defined in the I2S block's configuration in terms of sound system
465 * parameters. Each table is sized to the number of entries possible
466 * according to the number of configuration bits describing an I2S
467 * block parameter.
468 */
469
Andrew Jackson0d274542014-12-30 10:55:48 +0000470/* Maximum bit resolution of a channel - not uniformly spaced */
471static const u32 fifo_width[COMP_MAX_WORDSIZE] = {
472 12, 16, 20, 24, 32, 0, 0, 0
473};
474
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000475/* Width of (DMA) bus */
476static const u32 bus_widths[COMP_MAX_DATA_WIDTH] = {
477 DMA_SLAVE_BUSWIDTH_1_BYTE,
478 DMA_SLAVE_BUSWIDTH_2_BYTES,
479 DMA_SLAVE_BUSWIDTH_4_BYTES,
480 DMA_SLAVE_BUSWIDTH_UNDEFINED
481};
482
483/* PCM format to support channel resolution */
484static const u32 formats[COMP_MAX_WORDSIZE] = {
485 SNDRV_PCM_FMTBIT_S16_LE,
486 SNDRV_PCM_FMTBIT_S16_LE,
487 SNDRV_PCM_FMTBIT_S24_LE,
488 SNDRV_PCM_FMTBIT_S24_LE,
489 SNDRV_PCM_FMTBIT_S32_LE,
490 0,
491 0,
492 0
493};
494
Andrew Jackson0d274542014-12-30 10:55:48 +0000495static int dw_configure_dai(struct dw_i2s_dev *dev,
Andrew Jacksonafa86032014-12-19 16:18:07 +0000496 struct snd_soc_dai_driver *dw_i2s_dai,
Andrew Jackson0d274542014-12-30 10:55:48 +0000497 unsigned int rates)
Andrew Jacksonafa86032014-12-19 16:18:07 +0000498{
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000499 /*
500 * Read component parameter registers to extract
501 * the I2S block's configuration.
502 */
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -0500503 u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
504 u32 comp2 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp2);
Jose Abreu3fafd142016-04-07 17:53:57 +0100505 u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
Andrew Jackson0d274542014-12-30 10:55:48 +0000506 u32 idx;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000507
Maruthi Srinivas Bayyavarapua242cac2016-01-08 18:22:05 -0500508 if (dev->capability & DWC_I2S_RECORD &&
509 dev->quirks & DW_I2S_QUIRK_COMP_PARAM1)
510 comp1 = comp1 & ~BIT(5);
511
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000512 if (COMP1_TX_ENABLED(comp1)) {
Andrew Jacksonafa86032014-12-19 16:18:07 +0000513 dev_dbg(dev->dev, " designware: play supported\n");
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000514 idx = COMP1_TX_WORDSIZE_0(comp1);
515 if (WARN_ON(idx >= ARRAY_SIZE(formats)))
516 return -EINVAL;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000517 dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000518 dw_i2s_dai->playback.channels_max =
519 1 << (COMP1_TX_CHANNELS(comp1) + 1);
520 dw_i2s_dai->playback.formats = formats[idx];
Andrew Jackson0d274542014-12-30 10:55:48 +0000521 dw_i2s_dai->playback.rates = rates;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000522 }
523
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000524 if (COMP1_RX_ENABLED(comp1)) {
Andrew Jacksonafa86032014-12-19 16:18:07 +0000525 dev_dbg(dev->dev, "designware: record supported\n");
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000526 idx = COMP2_RX_WORDSIZE_0(comp2);
527 if (WARN_ON(idx >= ARRAY_SIZE(formats)))
528 return -EINVAL;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000529 dw_i2s_dai->capture.channels_min = MIN_CHANNEL_NUM;
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000530 dw_i2s_dai->capture.channels_max =
531 1 << (COMP1_RX_CHANNELS(comp1) + 1);
532 dw_i2s_dai->capture.formats = formats[idx];
Andrew Jackson0d274542014-12-30 10:55:48 +0000533 dw_i2s_dai->capture.rates = rates;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000534 }
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000535
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400536 if (COMP1_MODE_EN(comp1)) {
537 dev_dbg(dev->dev, "designware: i2s master mode supported\n");
538 dev->capability |= DW_I2S_MASTER;
539 } else {
540 dev_dbg(dev->dev, "designware: i2s slave mode supported\n");
541 dev->capability |= DW_I2S_SLAVE;
542 }
543
Jose Abreu3fafd142016-04-07 17:53:57 +0100544 dev->fifo_th = fifo_depth / 2;
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000545 return 0;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000546}
547
Andrew Jackson0d274542014-12-30 10:55:48 +0000548static int dw_configure_dai_by_pd(struct dw_i2s_dev *dev,
549 struct snd_soc_dai_driver *dw_i2s_dai,
550 struct resource *res,
551 const struct i2s_platform_data *pdata)
552{
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -0500553 u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
Andrew Jackson0d274542014-12-30 10:55:48 +0000554 u32 idx = COMP1_APB_DATA_WIDTH(comp1);
555 int ret;
556
557 if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
558 return -EINVAL;
559
560 ret = dw_configure_dai(dev, dw_i2s_dai, pdata->snd_rates);
561 if (ret < 0)
562 return ret;
563
564 /* Set DMA slaves info */
565 dev->play_dma_data.pd.data = pdata->play_dma_data;
566 dev->capture_dma_data.pd.data = pdata->capture_dma_data;
567 dev->play_dma_data.pd.addr = res->start + I2S_TXDMA;
568 dev->capture_dma_data.pd.addr = res->start + I2S_RXDMA;
569 dev->play_dma_data.pd.max_burst = 16;
570 dev->capture_dma_data.pd.max_burst = 16;
571 dev->play_dma_data.pd.addr_width = bus_widths[idx];
572 dev->capture_dma_data.pd.addr_width = bus_widths[idx];
573 dev->play_dma_data.pd.filter = pdata->filter;
574 dev->capture_dma_data.pd.filter = pdata->filter;
575
576 return 0;
577}
578
579static int dw_configure_dai_by_dt(struct dw_i2s_dev *dev,
580 struct snd_soc_dai_driver *dw_i2s_dai,
581 struct resource *res)
582{
583 u32 comp1 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_1);
584 u32 comp2 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_2);
585 u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
586 u32 idx = COMP1_APB_DATA_WIDTH(comp1);
587 u32 idx2;
588 int ret;
589
590 if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
591 return -EINVAL;
592
593 ret = dw_configure_dai(dev, dw_i2s_dai, SNDRV_PCM_RATE_8000_192000);
594 if (ret < 0)
595 return ret;
596
597 if (COMP1_TX_ENABLED(comp1)) {
598 idx2 = COMP1_TX_WORDSIZE_0(comp1);
599
600 dev->capability |= DWC_I2S_PLAY;
601 dev->play_dma_data.dt.addr = res->start + I2S_TXDMA;
602 dev->play_dma_data.dt.addr_width = bus_widths[idx];
603 dev->play_dma_data.dt.chan_name = "TX";
604 dev->play_dma_data.dt.fifo_size = fifo_depth *
605 (fifo_width[idx2]) >> 8;
606 dev->play_dma_data.dt.maxburst = 16;
607 }
608 if (COMP1_RX_ENABLED(comp1)) {
609 idx2 = COMP2_RX_WORDSIZE_0(comp2);
610
611 dev->capability |= DWC_I2S_RECORD;
612 dev->capture_dma_data.dt.addr = res->start + I2S_RXDMA;
613 dev->capture_dma_data.dt.addr_width = bus_widths[idx];
614 dev->capture_dma_data.dt.chan_name = "RX";
615 dev->capture_dma_data.dt.fifo_size = fifo_depth *
616 (fifo_width[idx2] >> 8);
617 dev->capture_dma_data.dt.maxburst = 16;
618 }
619
620 return 0;
621
622}
623
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530624static int dw_i2s_probe(struct platform_device *pdev)
625{
626 const struct i2s_platform_data *pdata = pdev->dev.platform_data;
627 struct dw_i2s_dev *dev;
628 struct resource *res;
629 int ret;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530630 struct snd_soc_dai_driver *dw_i2s_dai;
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400631 const char *clk_id;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530632
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530633 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
634 if (!dev) {
635 dev_warn(&pdev->dev, "kzalloc fail\n");
636 return -ENOMEM;
637 }
638
Andrew Jacksonb163be42014-12-03 16:38:46 +0000639 dw_i2s_dai = devm_kzalloc(&pdev->dev, sizeof(*dw_i2s_dai), GFP_KERNEL);
Andrew Jacksonbe334652014-12-12 09:25:00 +0000640 if (!dw_i2s_dai)
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530641 return -ENOMEM;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530642
Andrew Jacksonb163be42014-12-03 16:38:46 +0000643 dw_i2s_dai->ops = &dw_i2s_dai_ops;
644 dw_i2s_dai->suspend = dw_i2s_suspend;
645 dw_i2s_dai->resume = dw_i2s_resume;
646
647 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Andrew Jacksonb163be42014-12-03 16:38:46 +0000648 dev->i2s_base = devm_ioremap_resource(&pdev->dev, res);
Andrew Jacksonbe334652014-12-12 09:25:00 +0000649 if (IS_ERR(dev->i2s_base))
Andrew Jacksonb163be42014-12-03 16:38:46 +0000650 return PTR_ERR(dev->i2s_base);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530651
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530652 dev->dev = &pdev->dev;
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400653
Jon Medhurst (Tixy)d2f916a2016-02-01 15:54:37 +0000654 dev->i2s_reg_comp1 = I2S_COMP_PARAM_1;
655 dev->i2s_reg_comp2 = I2S_COMP_PARAM_2;
Andrew Jackson0d274542014-12-30 10:55:48 +0000656 if (pdata) {
Andrew Jackson0d274542014-12-30 10:55:48 +0000657 dev->capability = pdata->cap;
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400658 clk_id = NULL;
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -0500659 dev->quirks = pdata->quirks;
660 if (dev->quirks & DW_I2S_QUIRK_COMP_REG_OFFSET) {
661 dev->i2s_reg_comp1 = pdata->i2s_reg_comp1;
662 dev->i2s_reg_comp2 = pdata->i2s_reg_comp2;
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -0500663 }
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400664 ret = dw_configure_dai_by_pd(dev, dw_i2s_dai, res, pdata);
Andrew Jackson0d274542014-12-30 10:55:48 +0000665 } else {
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400666 clk_id = "i2sclk";
Andrew Jackson0d274542014-12-30 10:55:48 +0000667 ret = dw_configure_dai_by_dt(dev, dw_i2s_dai, res);
Andrew Jackson0d274542014-12-30 10:55:48 +0000668 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530669 if (ret < 0)
Andrew Jacksona56257c62014-12-30 10:55:43 +0000670 return ret;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530671
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400672 if (dev->capability & DW_I2S_MASTER) {
673 if (pdata) {
674 dev->i2s_clk_cfg = pdata->i2s_clk_cfg;
675 if (!dev->i2s_clk_cfg) {
676 dev_err(&pdev->dev, "no clock configure method\n");
677 return -ENODEV;
678 }
679 }
680 dev->clk = devm_clk_get(&pdev->dev, clk_id);
681
682 if (IS_ERR(dev->clk))
683 return PTR_ERR(dev->clk);
684
685 ret = clk_prepare_enable(dev->clk);
686 if (ret < 0)
687 return ret;
688 }
689
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530690 dev_set_drvdata(&pdev->dev, dev);
Andrew Jackson758c2de2014-12-30 10:55:46 +0000691 ret = devm_snd_soc_register_component(&pdev->dev, &dw_i2s_component,
Kuninori Morimoto92eaa322013-03-21 03:31:30 -0700692 dw_i2s_dai, 1);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530693 if (ret != 0) {
694 dev_err(&pdev->dev, "not able to register dai\n");
Fabio Estevame925a6b12013-08-26 09:25:15 -0300695 goto err_clk_disable;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530696 }
697
Andrew Jackson0d274542014-12-30 10:55:48 +0000698 if (!pdata) {
699 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
700 if (ret) {
701 dev_err(&pdev->dev,
702 "Could not register PCM: %d\n", ret);
703 goto err_clk_disable;
704 }
705 }
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500706 pm_runtime_enable(&pdev->dev);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530707 return 0;
708
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530709err_clk_disable:
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400710 if (dev->capability & DW_I2S_MASTER)
711 clk_disable_unprepare(dev->clk);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530712 return ret;
713}
714
715static int dw_i2s_remove(struct platform_device *pdev)
716{
717 struct dw_i2s_dev *dev = dev_get_drvdata(&pdev->dev);
718
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400719 if (dev->capability & DW_I2S_MASTER)
720 clk_disable_unprepare(dev->clk);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530721
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500722 pm_runtime_disable(&pdev->dev);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530723 return 0;
724}
725
Andrew Jackson0d274542014-12-30 10:55:48 +0000726#ifdef CONFIG_OF
727static const struct of_device_id dw_i2s_of_match[] = {
728 { .compatible = "snps,designware-i2s", },
729 {},
730};
731
732MODULE_DEVICE_TABLE(of, dw_i2s_of_match);
733#endif
734
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500735static const struct dev_pm_ops dwc_pm_ops = {
736 SET_RUNTIME_PM_OPS(dw_i2s_runtime_suspend, dw_i2s_runtime_resume, NULL)
737};
738
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530739static struct platform_driver dw_i2s_driver = {
740 .probe = dw_i2s_probe,
741 .remove = dw_i2s_remove,
742 .driver = {
743 .name = "designware-i2s",
Andrew Jackson0d274542014-12-30 10:55:48 +0000744 .of_match_table = of_match_ptr(dw_i2s_of_match),
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500745 .pm = &dwc_pm_ops,
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530746 },
747};
748
749module_platform_driver(dw_i2s_driver);
750
Rajeev Kumarb794dbc2014-09-09 12:27:19 +0530751MODULE_AUTHOR("Rajeev Kumar <rajeevkumar.linux@gmail.com>");
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530752MODULE_DESCRIPTION("DESIGNWARE I2S SoC Interface");
753MODULE_LICENSE("GPL");
754MODULE_ALIAS("platform:designware_i2s");