blob: 4c4f0dc24f10c938470f2e8ef51dd8e612682388 [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
Jose Abreub1d32fe2016-05-23 11:02:22 +0100148static inline void i2s_disable_irqs(struct dw_i2s_dev *dev, u32 stream,
149 int chan_nr)
150{
151 u32 i, irq;
152
153 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
154 for (i = 0; i < (chan_nr / 2); i++) {
155 irq = i2s_read_reg(dev->i2s_base, IMR(i));
156 i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x30);
157 }
158 } else {
159 for (i = 0; i < (chan_nr / 2); i++) {
160 irq = i2s_read_reg(dev->i2s_base, IMR(i));
161 i2s_write_reg(dev->i2s_base, IMR(i), irq | 0x03);
162 }
163 }
164}
165
166static inline void i2s_enable_irqs(struct dw_i2s_dev *dev, u32 stream,
167 int chan_nr)
168{
169 u32 i, irq;
170
171 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
172 for (i = 0; i < (chan_nr / 2); i++) {
173 irq = i2s_read_reg(dev->i2s_base, IMR(i));
174 i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x30);
175 }
176 } else {
177 for (i = 0; i < (chan_nr / 2); i++) {
178 irq = i2s_read_reg(dev->i2s_base, IMR(i));
179 i2s_write_reg(dev->i2s_base, IMR(i), irq & ~0x03);
180 }
181 }
182}
183
Mark Brown1520ffd2012-07-04 19:04:11 +0100184static void i2s_start(struct dw_i2s_dev *dev,
185 struct snd_pcm_substream *substream)
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530186{
Jose Abreu613c7c42016-04-05 18:08:02 +0100187 struct i2s_clk_config_data *config = &dev->config;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530188
Jose Abreub1d32fe2016-05-23 11:02:22 +0100189 i2s_write_reg(dev->i2s_base, IER, 1);
190 i2s_enable_irqs(dev, substream->stream, config->chan_nr);
191
192 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530193 i2s_write_reg(dev->i2s_base, ITER, 1);
Jose Abreub1d32fe2016-05-23 11:02:22 +0100194 else
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530195 i2s_write_reg(dev->i2s_base, IRER, 1);
196
197 i2s_write_reg(dev->i2s_base, CER, 1);
198}
199
200static void i2s_stop(struct dw_i2s_dev *dev,
201 struct snd_pcm_substream *substream)
202{
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530203
204 i2s_clear_irqs(dev, substream->stream);
Jose Abreub1d32fe2016-05-23 11:02:22 +0100205 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530206 i2s_write_reg(dev->i2s_base, ITER, 0);
Jose Abreub1d32fe2016-05-23 11:02:22 +0100207 else
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530208 i2s_write_reg(dev->i2s_base, IRER, 0);
209
Jose Abreub1d32fe2016-05-23 11:02:22 +0100210 i2s_disable_irqs(dev, substream->stream, 8);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530211
212 if (!dev->active) {
213 i2s_write_reg(dev->i2s_base, CER, 0);
214 i2s_write_reg(dev->i2s_base, IER, 0);
215 }
216}
217
218static int dw_i2s_startup(struct snd_pcm_substream *substream,
219 struct snd_soc_dai *cpu_dai)
220{
221 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Andrew Jackson0d274542014-12-30 10:55:48 +0000222 union dw_i2s_snd_dma_data *dma_data = NULL;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530223
224 if (!(dev->capability & DWC_I2S_RECORD) &&
225 (substream->stream == SNDRV_PCM_STREAM_CAPTURE))
226 return -EINVAL;
227
228 if (!(dev->capability & DWC_I2S_PLAY) &&
229 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK))
230 return -EINVAL;
231
232 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
233 dma_data = &dev->play_dma_data;
234 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
235 dma_data = &dev->capture_dma_data;
236
237 snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)dma_data);
238
239 return 0;
240}
241
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500242static void dw_i2s_config(struct dw_i2s_dev *dev, int stream)
243{
Jose Abreub1d32fe2016-05-23 11:02:22 +0100244 u32 ch_reg;
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500245 struct i2s_clk_config_data *config = &dev->config;
246
247
248 i2s_disable_channels(dev, stream);
249
250 for (ch_reg = 0; ch_reg < (config->chan_nr / 2); ch_reg++) {
251 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
252 i2s_write_reg(dev->i2s_base, TCR(ch_reg),
253 dev->xfer_resolution);
Jose Abreu3fafd142016-04-07 17:53:57 +0100254 i2s_write_reg(dev->i2s_base, TFCR(ch_reg),
255 dev->fifo_th - 1);
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500256 i2s_write_reg(dev->i2s_base, TER(ch_reg), 1);
257 } else {
258 i2s_write_reg(dev->i2s_base, RCR(ch_reg),
259 dev->xfer_resolution);
Jose Abreu3fafd142016-04-07 17:53:57 +0100260 i2s_write_reg(dev->i2s_base, RFCR(ch_reg),
261 dev->fifo_th - 1);
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500262 i2s_write_reg(dev->i2s_base, RER(ch_reg), 1);
263 }
264
265 }
266}
267
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530268static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
269 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
270{
271 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
272 struct i2s_clk_config_data *config = &dev->config;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530273 int ret;
274
275 switch (params_format(params)) {
276 case SNDRV_PCM_FORMAT_S16_LE:
277 config->data_width = 16;
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500278 dev->ccr = 0x00;
279 dev->xfer_resolution = 0x02;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530280 break;
281
282 case SNDRV_PCM_FORMAT_S24_LE:
283 config->data_width = 24;
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500284 dev->ccr = 0x08;
285 dev->xfer_resolution = 0x04;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530286 break;
287
288 case SNDRV_PCM_FORMAT_S32_LE:
289 config->data_width = 32;
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500290 dev->ccr = 0x10;
291 dev->xfer_resolution = 0x05;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530292 break;
293
294 default:
295 dev_err(dev->dev, "designware-i2s: unsuppted PCM fmt");
296 return -EINVAL;
297 }
298
299 config->chan_nr = params_channels(params);
300
301 switch (config->chan_nr) {
302 case EIGHT_CHANNEL_SUPPORT:
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530303 case SIX_CHANNEL_SUPPORT:
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530304 case FOUR_CHANNEL_SUPPORT:
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530305 case TWO_CHANNEL_SUPPORT:
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530306 break;
307 default:
308 dev_err(dev->dev, "channel not supported\n");
Dan Carpenter0099d242013-01-25 09:43:43 +0300309 return -EINVAL;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530310 }
311
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500312 dw_i2s_config(dev, substream->stream);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530313
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500314 i2s_write_reg(dev->i2s_base, CCR, dev->ccr);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530315
316 config->sample_rate = params_rate(params);
317
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400318 if (dev->capability & DW_I2S_MASTER) {
319 if (dev->i2s_clk_cfg) {
320 ret = dev->i2s_clk_cfg(config);
321 if (ret < 0) {
322 dev_err(dev->dev, "runtime audio clk config fail\n");
323 return ret;
324 }
325 } else {
326 u32 bitclk = config->sample_rate *
327 config->data_width * 2;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530328
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400329 ret = clk_set_rate(dev->clk, bitclk);
330 if (ret) {
331 dev_err(dev->dev, "Can't set I2S clock rate: %d\n",
332 ret);
333 return ret;
334 }
Andrew Jackson0d274542014-12-30 10:55:48 +0000335 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530336 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530337 return 0;
338}
339
340static void dw_i2s_shutdown(struct snd_pcm_substream *substream,
341 struct snd_soc_dai *dai)
342{
343 snd_soc_dai_set_dma_data(dai, substream, NULL);
344}
345
Andrew Jackson3475c3d2014-12-19 16:18:05 +0000346static int dw_i2s_prepare(struct snd_pcm_substream *substream,
347 struct snd_soc_dai *dai)
348{
349 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
350
351 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
352 i2s_write_reg(dev->i2s_base, TXFFR, 1);
353 else
354 i2s_write_reg(dev->i2s_base, RXFFR, 1);
355
356 return 0;
357}
358
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530359static int dw_i2s_trigger(struct snd_pcm_substream *substream,
360 int cmd, struct snd_soc_dai *dai)
361{
362 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
363 int ret = 0;
364
365 switch (cmd) {
366 case SNDRV_PCM_TRIGGER_START:
367 case SNDRV_PCM_TRIGGER_RESUME:
368 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
369 dev->active++;
370 i2s_start(dev, substream);
371 break;
372
373 case SNDRV_PCM_TRIGGER_STOP:
374 case SNDRV_PCM_TRIGGER_SUSPEND:
375 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
376 dev->active--;
377 i2s_stop(dev, substream);
378 break;
379 default:
380 ret = -EINVAL;
381 break;
382 }
383 return ret;
384}
385
Maruthi Srinivas Bayyavarapuab57b8e2015-10-23 17:15:41 -0400386static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
387{
388 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
389 int ret = 0;
390
391 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
392 case SND_SOC_DAIFMT_CBM_CFM:
393 if (dev->capability & DW_I2S_SLAVE)
394 ret = 0;
395 else
396 ret = -EINVAL;
397 break;
398 case SND_SOC_DAIFMT_CBS_CFS:
399 if (dev->capability & DW_I2S_MASTER)
400 ret = 0;
401 else
402 ret = -EINVAL;
403 break;
404 case SND_SOC_DAIFMT_CBM_CFS:
405 case SND_SOC_DAIFMT_CBS_CFM:
406 ret = -EINVAL;
407 break;
408 default:
409 dev_dbg(dev->dev, "dwc : Invalid master/slave format\n");
410 ret = -EINVAL;
411 break;
412 }
413 return ret;
414}
415
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530416static struct snd_soc_dai_ops dw_i2s_dai_ops = {
417 .startup = dw_i2s_startup,
418 .shutdown = dw_i2s_shutdown,
419 .hw_params = dw_i2s_hw_params,
Andrew Jackson3475c3d2014-12-19 16:18:05 +0000420 .prepare = dw_i2s_prepare,
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530421 .trigger = dw_i2s_trigger,
Maruthi Srinivas Bayyavarapuab57b8e2015-10-23 17:15:41 -0400422 .set_fmt = dw_i2s_set_fmt,
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530423};
424
Kuninori Morimoto92eaa322013-03-21 03:31:30 -0700425static const struct snd_soc_component_driver dw_i2s_component = {
426 .name = "dw-i2s",
427};
428
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530429#ifdef CONFIG_PM
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500430static int dw_i2s_runtime_suspend(struct device *dev)
431{
432 struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
433
434 if (dw_dev->capability & DW_I2S_MASTER)
435 clk_disable(dw_dev->clk);
436 return 0;
437}
438
439static int dw_i2s_runtime_resume(struct device *dev)
440{
441 struct dw_i2s_dev *dw_dev = dev_get_drvdata(dev);
442
443 if (dw_dev->capability & DW_I2S_MASTER)
444 clk_enable(dw_dev->clk);
445 return 0;
446}
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530447
448static int dw_i2s_suspend(struct snd_soc_dai *dai)
449{
450 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
451
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400452 if (dev->capability & DW_I2S_MASTER)
453 clk_disable(dev->clk);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530454 return 0;
455}
456
457static int dw_i2s_resume(struct snd_soc_dai *dai)
458{
459 struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
460
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400461 if (dev->capability & DW_I2S_MASTER)
462 clk_enable(dev->clk);
Maruthi Srinivas Bayyavarapu0032e9d2015-12-04 18:40:33 -0500463
464 if (dai->playback_active)
465 dw_i2s_config(dev, SNDRV_PCM_STREAM_PLAYBACK);
466 if (dai->capture_active)
467 dw_i2s_config(dev, SNDRV_PCM_STREAM_CAPTURE);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530468 return 0;
469}
470
471#else
472#define dw_i2s_suspend NULL
473#define dw_i2s_resume NULL
474#endif
475
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000476/*
477 * The following tables allow a direct lookup of various parameters
478 * defined in the I2S block's configuration in terms of sound system
479 * parameters. Each table is sized to the number of entries possible
480 * according to the number of configuration bits describing an I2S
481 * block parameter.
482 */
483
Andrew Jackson0d274542014-12-30 10:55:48 +0000484/* Maximum bit resolution of a channel - not uniformly spaced */
485static const u32 fifo_width[COMP_MAX_WORDSIZE] = {
486 12, 16, 20, 24, 32, 0, 0, 0
487};
488
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000489/* Width of (DMA) bus */
490static const u32 bus_widths[COMP_MAX_DATA_WIDTH] = {
491 DMA_SLAVE_BUSWIDTH_1_BYTE,
492 DMA_SLAVE_BUSWIDTH_2_BYTES,
493 DMA_SLAVE_BUSWIDTH_4_BYTES,
494 DMA_SLAVE_BUSWIDTH_UNDEFINED
495};
496
497/* PCM format to support channel resolution */
498static const u32 formats[COMP_MAX_WORDSIZE] = {
499 SNDRV_PCM_FMTBIT_S16_LE,
500 SNDRV_PCM_FMTBIT_S16_LE,
501 SNDRV_PCM_FMTBIT_S24_LE,
502 SNDRV_PCM_FMTBIT_S24_LE,
503 SNDRV_PCM_FMTBIT_S32_LE,
504 0,
505 0,
506 0
507};
508
Andrew Jackson0d274542014-12-30 10:55:48 +0000509static int dw_configure_dai(struct dw_i2s_dev *dev,
Andrew Jacksonafa86032014-12-19 16:18:07 +0000510 struct snd_soc_dai_driver *dw_i2s_dai,
Andrew Jackson0d274542014-12-30 10:55:48 +0000511 unsigned int rates)
Andrew Jacksonafa86032014-12-19 16:18:07 +0000512{
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000513 /*
514 * Read component parameter registers to extract
515 * the I2S block's configuration.
516 */
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -0500517 u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
518 u32 comp2 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp2);
Jose Abreu3fafd142016-04-07 17:53:57 +0100519 u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
Andrew Jackson0d274542014-12-30 10:55:48 +0000520 u32 idx;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000521
Maruthi Srinivas Bayyavarapua242cac2016-01-08 18:22:05 -0500522 if (dev->capability & DWC_I2S_RECORD &&
523 dev->quirks & DW_I2S_QUIRK_COMP_PARAM1)
524 comp1 = comp1 & ~BIT(5);
525
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000526 if (COMP1_TX_ENABLED(comp1)) {
Andrew Jacksonafa86032014-12-19 16:18:07 +0000527 dev_dbg(dev->dev, " designware: play supported\n");
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000528 idx = COMP1_TX_WORDSIZE_0(comp1);
529 if (WARN_ON(idx >= ARRAY_SIZE(formats)))
530 return -EINVAL;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000531 dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000532 dw_i2s_dai->playback.channels_max =
533 1 << (COMP1_TX_CHANNELS(comp1) + 1);
534 dw_i2s_dai->playback.formats = formats[idx];
Andrew Jackson0d274542014-12-30 10:55:48 +0000535 dw_i2s_dai->playback.rates = rates;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000536 }
537
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000538 if (COMP1_RX_ENABLED(comp1)) {
Andrew Jacksonafa86032014-12-19 16:18:07 +0000539 dev_dbg(dev->dev, "designware: record supported\n");
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000540 idx = COMP2_RX_WORDSIZE_0(comp2);
541 if (WARN_ON(idx >= ARRAY_SIZE(formats)))
542 return -EINVAL;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000543 dw_i2s_dai->capture.channels_min = MIN_CHANNEL_NUM;
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000544 dw_i2s_dai->capture.channels_max =
545 1 << (COMP1_RX_CHANNELS(comp1) + 1);
546 dw_i2s_dai->capture.formats = formats[idx];
Andrew Jackson0d274542014-12-30 10:55:48 +0000547 dw_i2s_dai->capture.rates = rates;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000548 }
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000549
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400550 if (COMP1_MODE_EN(comp1)) {
551 dev_dbg(dev->dev, "designware: i2s master mode supported\n");
552 dev->capability |= DW_I2S_MASTER;
553 } else {
554 dev_dbg(dev->dev, "designware: i2s slave mode supported\n");
555 dev->capability |= DW_I2S_SLAVE;
556 }
557
Jose Abreu3fafd142016-04-07 17:53:57 +0100558 dev->fifo_th = fifo_depth / 2;
Andrew Jacksonb226efe2014-12-30 10:55:45 +0000559 return 0;
Andrew Jacksonafa86032014-12-19 16:18:07 +0000560}
561
Andrew Jackson0d274542014-12-30 10:55:48 +0000562static int dw_configure_dai_by_pd(struct dw_i2s_dev *dev,
563 struct snd_soc_dai_driver *dw_i2s_dai,
564 struct resource *res,
565 const struct i2s_platform_data *pdata)
566{
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -0500567 u32 comp1 = i2s_read_reg(dev->i2s_base, dev->i2s_reg_comp1);
Andrew Jackson0d274542014-12-30 10:55:48 +0000568 u32 idx = COMP1_APB_DATA_WIDTH(comp1);
569 int ret;
570
571 if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
572 return -EINVAL;
573
574 ret = dw_configure_dai(dev, dw_i2s_dai, pdata->snd_rates);
575 if (ret < 0)
576 return ret;
577
578 /* Set DMA slaves info */
579 dev->play_dma_data.pd.data = pdata->play_dma_data;
580 dev->capture_dma_data.pd.data = pdata->capture_dma_data;
581 dev->play_dma_data.pd.addr = res->start + I2S_TXDMA;
582 dev->capture_dma_data.pd.addr = res->start + I2S_RXDMA;
583 dev->play_dma_data.pd.max_burst = 16;
584 dev->capture_dma_data.pd.max_burst = 16;
585 dev->play_dma_data.pd.addr_width = bus_widths[idx];
586 dev->capture_dma_data.pd.addr_width = bus_widths[idx];
587 dev->play_dma_data.pd.filter = pdata->filter;
588 dev->capture_dma_data.pd.filter = pdata->filter;
589
590 return 0;
591}
592
593static int dw_configure_dai_by_dt(struct dw_i2s_dev *dev,
594 struct snd_soc_dai_driver *dw_i2s_dai,
595 struct resource *res)
596{
597 u32 comp1 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_1);
598 u32 comp2 = i2s_read_reg(dev->i2s_base, I2S_COMP_PARAM_2);
599 u32 fifo_depth = 1 << (1 + COMP1_FIFO_DEPTH_GLOBAL(comp1));
600 u32 idx = COMP1_APB_DATA_WIDTH(comp1);
601 u32 idx2;
602 int ret;
603
604 if (WARN_ON(idx >= ARRAY_SIZE(bus_widths)))
605 return -EINVAL;
606
607 ret = dw_configure_dai(dev, dw_i2s_dai, SNDRV_PCM_RATE_8000_192000);
608 if (ret < 0)
609 return ret;
610
611 if (COMP1_TX_ENABLED(comp1)) {
612 idx2 = COMP1_TX_WORDSIZE_0(comp1);
613
614 dev->capability |= DWC_I2S_PLAY;
615 dev->play_dma_data.dt.addr = res->start + I2S_TXDMA;
616 dev->play_dma_data.dt.addr_width = bus_widths[idx];
617 dev->play_dma_data.dt.chan_name = "TX";
618 dev->play_dma_data.dt.fifo_size = fifo_depth *
619 (fifo_width[idx2]) >> 8;
620 dev->play_dma_data.dt.maxburst = 16;
621 }
622 if (COMP1_RX_ENABLED(comp1)) {
623 idx2 = COMP2_RX_WORDSIZE_0(comp2);
624
625 dev->capability |= DWC_I2S_RECORD;
626 dev->capture_dma_data.dt.addr = res->start + I2S_RXDMA;
627 dev->capture_dma_data.dt.addr_width = bus_widths[idx];
628 dev->capture_dma_data.dt.chan_name = "RX";
629 dev->capture_dma_data.dt.fifo_size = fifo_depth *
630 (fifo_width[idx2] >> 8);
631 dev->capture_dma_data.dt.maxburst = 16;
632 }
633
634 return 0;
635
636}
637
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530638static int dw_i2s_probe(struct platform_device *pdev)
639{
640 const struct i2s_platform_data *pdata = pdev->dev.platform_data;
641 struct dw_i2s_dev *dev;
642 struct resource *res;
643 int ret;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530644 struct snd_soc_dai_driver *dw_i2s_dai;
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400645 const char *clk_id;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530646
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530647 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
648 if (!dev) {
649 dev_warn(&pdev->dev, "kzalloc fail\n");
650 return -ENOMEM;
651 }
652
Andrew Jacksonb163be42014-12-03 16:38:46 +0000653 dw_i2s_dai = devm_kzalloc(&pdev->dev, sizeof(*dw_i2s_dai), GFP_KERNEL);
Andrew Jacksonbe334652014-12-12 09:25:00 +0000654 if (!dw_i2s_dai)
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530655 return -ENOMEM;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530656
Andrew Jacksonb163be42014-12-03 16:38:46 +0000657 dw_i2s_dai->ops = &dw_i2s_dai_ops;
658 dw_i2s_dai->suspend = dw_i2s_suspend;
659 dw_i2s_dai->resume = dw_i2s_resume;
660
661 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Andrew Jacksonb163be42014-12-03 16:38:46 +0000662 dev->i2s_base = devm_ioremap_resource(&pdev->dev, res);
Andrew Jacksonbe334652014-12-12 09:25:00 +0000663 if (IS_ERR(dev->i2s_base))
Andrew Jacksonb163be42014-12-03 16:38:46 +0000664 return PTR_ERR(dev->i2s_base);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530665
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530666 dev->dev = &pdev->dev;
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400667
Jon Medhurst (Tixy)d2f916a2016-02-01 15:54:37 +0000668 dev->i2s_reg_comp1 = I2S_COMP_PARAM_1;
669 dev->i2s_reg_comp2 = I2S_COMP_PARAM_2;
Andrew Jackson0d274542014-12-30 10:55:48 +0000670 if (pdata) {
Andrew Jackson0d274542014-12-30 10:55:48 +0000671 dev->capability = pdata->cap;
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400672 clk_id = NULL;
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -0500673 dev->quirks = pdata->quirks;
674 if (dev->quirks & DW_I2S_QUIRK_COMP_REG_OFFSET) {
675 dev->i2s_reg_comp1 = pdata->i2s_reg_comp1;
676 dev->i2s_reg_comp2 = pdata->i2s_reg_comp2;
Maruthi Srinivas Bayyavarapue1648352015-12-04 18:40:32 -0500677 }
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400678 ret = dw_configure_dai_by_pd(dev, dw_i2s_dai, res, pdata);
Andrew Jackson0d274542014-12-30 10:55:48 +0000679 } else {
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400680 clk_id = "i2sclk";
Andrew Jackson0d274542014-12-30 10:55:48 +0000681 ret = dw_configure_dai_by_dt(dev, dw_i2s_dai, res);
Andrew Jackson0d274542014-12-30 10:55:48 +0000682 }
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530683 if (ret < 0)
Andrew Jacksona56257c62014-12-30 10:55:43 +0000684 return ret;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530685
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400686 if (dev->capability & DW_I2S_MASTER) {
687 if (pdata) {
688 dev->i2s_clk_cfg = pdata->i2s_clk_cfg;
689 if (!dev->i2s_clk_cfg) {
690 dev_err(&pdev->dev, "no clock configure method\n");
691 return -ENODEV;
692 }
693 }
694 dev->clk = devm_clk_get(&pdev->dev, clk_id);
695
696 if (IS_ERR(dev->clk))
697 return PTR_ERR(dev->clk);
698
699 ret = clk_prepare_enable(dev->clk);
700 if (ret < 0)
701 return ret;
702 }
703
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530704 dev_set_drvdata(&pdev->dev, dev);
Andrew Jackson758c2de2014-12-30 10:55:46 +0000705 ret = devm_snd_soc_register_component(&pdev->dev, &dw_i2s_component,
Kuninori Morimoto92eaa322013-03-21 03:31:30 -0700706 dw_i2s_dai, 1);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530707 if (ret != 0) {
708 dev_err(&pdev->dev, "not able to register dai\n");
Fabio Estevame925a6b12013-08-26 09:25:15 -0300709 goto err_clk_disable;
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530710 }
711
Andrew Jackson0d274542014-12-30 10:55:48 +0000712 if (!pdata) {
713 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
714 if (ret) {
715 dev_err(&pdev->dev,
716 "Could not register PCM: %d\n", ret);
717 goto err_clk_disable;
718 }
719 }
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500720 pm_runtime_enable(&pdev->dev);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530721 return 0;
722
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530723err_clk_disable:
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400724 if (dev->capability & DW_I2S_MASTER)
725 clk_disable_unprepare(dev->clk);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530726 return ret;
727}
728
729static int dw_i2s_remove(struct platform_device *pdev)
730{
731 struct dw_i2s_dev *dev = dev_get_drvdata(&pdev->dev);
732
Maruthi Srinivas Bayyavarapu1d957d82015-09-25 17:48:22 -0400733 if (dev->capability & DW_I2S_MASTER)
734 clk_disable_unprepare(dev->clk);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530735
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500736 pm_runtime_disable(&pdev->dev);
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530737 return 0;
738}
739
Andrew Jackson0d274542014-12-30 10:55:48 +0000740#ifdef CONFIG_OF
741static const struct of_device_id dw_i2s_of_match[] = {
742 { .compatible = "snps,designware-i2s", },
743 {},
744};
745
746MODULE_DEVICE_TABLE(of, dw_i2s_of_match);
747#endif
748
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500749static const struct dev_pm_ops dwc_pm_ops = {
750 SET_RUNTIME_PM_OPS(dw_i2s_runtime_suspend, dw_i2s_runtime_resume, NULL)
751};
752
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530753static struct platform_driver dw_i2s_driver = {
754 .probe = dw_i2s_probe,
755 .remove = dw_i2s_remove,
756 .driver = {
757 .name = "designware-i2s",
Andrew Jackson0d274542014-12-30 10:55:48 +0000758 .of_match_table = of_match_ptr(dw_i2s_of_match),
Maruthi Srinivas Bayyavarapuf4830312015-12-04 18:40:31 -0500759 .pm = &dwc_pm_ops,
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530760 },
761};
762
763module_platform_driver(dw_i2s_driver);
764
Rajeev Kumarb794dbc2014-09-09 12:27:19 +0530765MODULE_AUTHOR("Rajeev Kumar <rajeevkumar.linux@gmail.com>");
Rajeev Kumar3a9cf8e2012-06-21 15:54:51 +0530766MODULE_DESCRIPTION("DESIGNWARE I2S SoC Interface");
767MODULE_LICENSE("GPL");
768MODULE_ALIAS("platform:designware_i2s");