16f6c3c36SMaruthi Srinivas Bayyavarapu // SPDX-License-Identifier: GPL-2.0
26f6c3c36SMaruthi Srinivas Bayyavarapu //
36f6c3c36SMaruthi Srinivas Bayyavarapu // Xilinx ASoC audio formatter support
46f6c3c36SMaruthi Srinivas Bayyavarapu //
56f6c3c36SMaruthi Srinivas Bayyavarapu // Copyright (C) 2018 Xilinx, Inc.
66f6c3c36SMaruthi Srinivas Bayyavarapu //
76f6c3c36SMaruthi Srinivas Bayyavarapu // Author: Maruthi Srinivas Bayyavarapu <maruthis@xilinx.com>
86f6c3c36SMaruthi Srinivas Bayyavarapu
96f6c3c36SMaruthi Srinivas Bayyavarapu #include <linux/clk.h>
106f6c3c36SMaruthi Srinivas Bayyavarapu #include <linux/io.h>
116f6c3c36SMaruthi Srinivas Bayyavarapu #include <linux/module.h>
126f6c3c36SMaruthi Srinivas Bayyavarapu #include <linux/of_address.h>
136f6c3c36SMaruthi Srinivas Bayyavarapu #include <linux/of_irq.h>
146f6c3c36SMaruthi Srinivas Bayyavarapu #include <linux/sizes.h>
156f6c3c36SMaruthi Srinivas Bayyavarapu
16c284d4e3SMaruthi Srinivas Bayyavarapu #include <sound/asoundef.h>
176f6c3c36SMaruthi Srinivas Bayyavarapu #include <sound/soc.h>
186f6c3c36SMaruthi Srinivas Bayyavarapu #include <sound/pcm_params.h>
196f6c3c36SMaruthi Srinivas Bayyavarapu
206f6c3c36SMaruthi Srinivas Bayyavarapu #define DRV_NAME "xlnx_formatter_pcm"
216f6c3c36SMaruthi Srinivas Bayyavarapu
226f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_S2MM_OFFSET 0
236f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_MM2S_OFFSET 0x100
246f6c3c36SMaruthi Srinivas Bayyavarapu
256f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_CORE_CONFIG 0x4
266f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_CTRL 0x10
276f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_STS 0x14
286f6c3c36SMaruthi Srinivas Bayyavarapu
296f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_CTRL_RESET_MASK BIT(1)
306f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_CFG_MM2S_MASK BIT(15)
316f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_CFG_S2MM_MASK BIT(31)
326f6c3c36SMaruthi Srinivas Bayyavarapu
336f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_FS_MULTIPLIER 0x18
346f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_PERIOD_CONFIG 0x1C
356f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_BUFF_ADDR_LSB 0x20
366f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_BUFF_ADDR_MSB 0x24
376f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_XFER_COUNT 0x28
386f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_AUD_CH_STS_START 0x2C
396f6c3c36SMaruthi Srinivas Bayyavarapu #define XLNX_BYTES_PER_CH 0x44
40e958b588SRobert Hancock #define XLNX_AUD_ALIGN_BYTES 64
416f6c3c36SMaruthi Srinivas Bayyavarapu
426f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_STS_IOC_IRQ_MASK BIT(31)
436f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_STS_CH_STS_MASK BIT(29)
446f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_CTRL_IOC_IRQ_MASK BIT(13)
456f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_CTRL_TOUT_IRQ_MASK BIT(14)
466f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_CTRL_DMA_EN_MASK BIT(0)
476f6c3c36SMaruthi Srinivas Bayyavarapu
486f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_MM2S_CH_MASK GENMASK(11, 8)
496f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_MM2S_CH_SHIFT 8
506f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_MM2S_XFER_MASK GENMASK(14, 13)
516f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_MM2S_XFER_SHIFT 13
526f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_MM2S_PKG_MASK BIT(12)
536f6c3c36SMaruthi Srinivas Bayyavarapu
546f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_S2MM_CH_MASK GENMASK(27, 24)
556f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_S2MM_CH_SHIFT 24
566f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_S2MM_XFER_MASK GENMASK(30, 29)
576f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_S2MM_XFER_SHIFT 29
586f6c3c36SMaruthi Srinivas Bayyavarapu #define CFG_S2MM_PKG_MASK BIT(28)
596f6c3c36SMaruthi Srinivas Bayyavarapu
606f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_CTRL_DATA_WIDTH_SHIFT 16
616f6c3c36SMaruthi Srinivas Bayyavarapu #define AUD_CTRL_ACTIVE_CH_SHIFT 19
626f6c3c36SMaruthi Srinivas Bayyavarapu #define PERIOD_CFG_PERIODS_SHIFT 16
636f6c3c36SMaruthi Srinivas Bayyavarapu
646f6c3c36SMaruthi Srinivas Bayyavarapu #define PERIODS_MIN 2
656f6c3c36SMaruthi Srinivas Bayyavarapu #define PERIODS_MAX 6
666f6c3c36SMaruthi Srinivas Bayyavarapu #define PERIOD_BYTES_MIN 192
676f6c3c36SMaruthi Srinivas Bayyavarapu #define PERIOD_BYTES_MAX (50 * 1024)
68c284d4e3SMaruthi Srinivas Bayyavarapu #define XLNX_PARAM_UNKNOWN 0
696f6c3c36SMaruthi Srinivas Bayyavarapu
706f6c3c36SMaruthi Srinivas Bayyavarapu enum bit_depth {
716f6c3c36SMaruthi Srinivas Bayyavarapu BIT_DEPTH_8,
726f6c3c36SMaruthi Srinivas Bayyavarapu BIT_DEPTH_16,
736f6c3c36SMaruthi Srinivas Bayyavarapu BIT_DEPTH_20,
746f6c3c36SMaruthi Srinivas Bayyavarapu BIT_DEPTH_24,
756f6c3c36SMaruthi Srinivas Bayyavarapu BIT_DEPTH_32,
766f6c3c36SMaruthi Srinivas Bayyavarapu };
776f6c3c36SMaruthi Srinivas Bayyavarapu
786f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_drv_data {
796f6c3c36SMaruthi Srinivas Bayyavarapu void __iomem *mmio;
806f6c3c36SMaruthi Srinivas Bayyavarapu bool s2mm_presence;
816f6c3c36SMaruthi Srinivas Bayyavarapu bool mm2s_presence;
821d38b4e9SGustavo A. R. Silva int s2mm_irq;
831d38b4e9SGustavo A. R. Silva int mm2s_irq;
846f6c3c36SMaruthi Srinivas Bayyavarapu struct snd_pcm_substream *play_stream;
856f6c3c36SMaruthi Srinivas Bayyavarapu struct snd_pcm_substream *capture_stream;
866f6c3c36SMaruthi Srinivas Bayyavarapu struct clk *axi_clk;
871c5091fbSRobert Hancock unsigned int sysclk;
886f6c3c36SMaruthi Srinivas Bayyavarapu };
896f6c3c36SMaruthi Srinivas Bayyavarapu
906f6c3c36SMaruthi Srinivas Bayyavarapu /*
916f6c3c36SMaruthi Srinivas Bayyavarapu * struct xlnx_pcm_stream_param - stream configuration
926f6c3c36SMaruthi Srinivas Bayyavarapu * @mmio: base address offset
936f6c3c36SMaruthi Srinivas Bayyavarapu * @interleaved: audio channels arrangement in buffer
946f6c3c36SMaruthi Srinivas Bayyavarapu * @xfer_mode: data formatting mode during transfer
956f6c3c36SMaruthi Srinivas Bayyavarapu * @ch_limit: Maximum channels supported
966f6c3c36SMaruthi Srinivas Bayyavarapu * @buffer_size: stream ring buffer size
976f6c3c36SMaruthi Srinivas Bayyavarapu */
986f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_stream_param {
996f6c3c36SMaruthi Srinivas Bayyavarapu void __iomem *mmio;
1006f6c3c36SMaruthi Srinivas Bayyavarapu bool interleaved;
1016f6c3c36SMaruthi Srinivas Bayyavarapu u32 xfer_mode;
1026f6c3c36SMaruthi Srinivas Bayyavarapu u32 ch_limit;
1036f6c3c36SMaruthi Srinivas Bayyavarapu u64 buffer_size;
1046f6c3c36SMaruthi Srinivas Bayyavarapu };
1056f6c3c36SMaruthi Srinivas Bayyavarapu
1066f6c3c36SMaruthi Srinivas Bayyavarapu static const struct snd_pcm_hardware xlnx_pcm_hardware = {
1076f6c3c36SMaruthi Srinivas Bayyavarapu .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
1086f6c3c36SMaruthi Srinivas Bayyavarapu SNDRV_PCM_INFO_BATCH | SNDRV_PCM_INFO_PAUSE |
1096f6c3c36SMaruthi Srinivas Bayyavarapu SNDRV_PCM_INFO_RESUME,
1106f6c3c36SMaruthi Srinivas Bayyavarapu .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |
1116f6c3c36SMaruthi Srinivas Bayyavarapu SNDRV_PCM_FMTBIT_S24_LE,
1126f6c3c36SMaruthi Srinivas Bayyavarapu .channels_min = 2,
1136f6c3c36SMaruthi Srinivas Bayyavarapu .channels_max = 2,
1146f6c3c36SMaruthi Srinivas Bayyavarapu .rates = SNDRV_PCM_RATE_8000_192000,
1156f6c3c36SMaruthi Srinivas Bayyavarapu .rate_min = 8000,
1166f6c3c36SMaruthi Srinivas Bayyavarapu .rate_max = 192000,
1176f6c3c36SMaruthi Srinivas Bayyavarapu .buffer_bytes_max = PERIODS_MAX * PERIOD_BYTES_MAX,
1186f6c3c36SMaruthi Srinivas Bayyavarapu .period_bytes_min = PERIOD_BYTES_MIN,
1196f6c3c36SMaruthi Srinivas Bayyavarapu .period_bytes_max = PERIOD_BYTES_MAX,
1206f6c3c36SMaruthi Srinivas Bayyavarapu .periods_min = PERIODS_MIN,
1216f6c3c36SMaruthi Srinivas Bayyavarapu .periods_max = PERIODS_MAX,
1226f6c3c36SMaruthi Srinivas Bayyavarapu };
1236f6c3c36SMaruthi Srinivas Bayyavarapu
124c284d4e3SMaruthi Srinivas Bayyavarapu enum {
125c284d4e3SMaruthi Srinivas Bayyavarapu AES_TO_AES,
126c284d4e3SMaruthi Srinivas Bayyavarapu AES_TO_PCM,
127c284d4e3SMaruthi Srinivas Bayyavarapu PCM_TO_PCM,
128c284d4e3SMaruthi Srinivas Bayyavarapu PCM_TO_AES
129c284d4e3SMaruthi Srinivas Bayyavarapu };
130c284d4e3SMaruthi Srinivas Bayyavarapu
xlnx_parse_aes_params(u32 chsts_reg1_val,u32 chsts_reg2_val,struct device * dev)131c284d4e3SMaruthi Srinivas Bayyavarapu static void xlnx_parse_aes_params(u32 chsts_reg1_val, u32 chsts_reg2_val,
132c284d4e3SMaruthi Srinivas Bayyavarapu struct device *dev)
133c284d4e3SMaruthi Srinivas Bayyavarapu {
134c284d4e3SMaruthi Srinivas Bayyavarapu u32 padded, srate, bit_depth, status[2];
135c284d4e3SMaruthi Srinivas Bayyavarapu
136c284d4e3SMaruthi Srinivas Bayyavarapu if (chsts_reg1_val & IEC958_AES0_PROFESSIONAL) {
137c284d4e3SMaruthi Srinivas Bayyavarapu status[0] = chsts_reg1_val & 0xff;
138c284d4e3SMaruthi Srinivas Bayyavarapu status[1] = (chsts_reg1_val >> 16) & 0xff;
139c284d4e3SMaruthi Srinivas Bayyavarapu
140c284d4e3SMaruthi Srinivas Bayyavarapu switch (status[0] & IEC958_AES0_PRO_FS) {
141c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES0_PRO_FS_44100:
142c284d4e3SMaruthi Srinivas Bayyavarapu srate = 44100;
143c284d4e3SMaruthi Srinivas Bayyavarapu break;
144c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES0_PRO_FS_48000:
145c284d4e3SMaruthi Srinivas Bayyavarapu srate = 48000;
146c284d4e3SMaruthi Srinivas Bayyavarapu break;
147c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES0_PRO_FS_32000:
148c284d4e3SMaruthi Srinivas Bayyavarapu srate = 32000;
149c284d4e3SMaruthi Srinivas Bayyavarapu break;
150c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES0_PRO_FS_NOTID:
151c284d4e3SMaruthi Srinivas Bayyavarapu default:
152c284d4e3SMaruthi Srinivas Bayyavarapu srate = XLNX_PARAM_UNKNOWN;
153c284d4e3SMaruthi Srinivas Bayyavarapu break;
154c284d4e3SMaruthi Srinivas Bayyavarapu }
155c284d4e3SMaruthi Srinivas Bayyavarapu
156c284d4e3SMaruthi Srinivas Bayyavarapu switch (status[1] & IEC958_AES2_PRO_SBITS) {
157c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES2_PRO_WORDLEN_NOTID:
158c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES2_PRO_SBITS_20:
159c284d4e3SMaruthi Srinivas Bayyavarapu padded = 0;
160c284d4e3SMaruthi Srinivas Bayyavarapu break;
161c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES2_PRO_SBITS_24:
162c284d4e3SMaruthi Srinivas Bayyavarapu padded = 4;
163c284d4e3SMaruthi Srinivas Bayyavarapu break;
164c284d4e3SMaruthi Srinivas Bayyavarapu default:
165c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = XLNX_PARAM_UNKNOWN;
166c284d4e3SMaruthi Srinivas Bayyavarapu goto log_params;
167c284d4e3SMaruthi Srinivas Bayyavarapu }
168c284d4e3SMaruthi Srinivas Bayyavarapu
169c284d4e3SMaruthi Srinivas Bayyavarapu switch (status[1] & IEC958_AES2_PRO_WORDLEN) {
170c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES2_PRO_WORDLEN_20_16:
171c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 16 + padded;
172c284d4e3SMaruthi Srinivas Bayyavarapu break;
173c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES2_PRO_WORDLEN_22_18:
174c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 18 + padded;
175c284d4e3SMaruthi Srinivas Bayyavarapu break;
176c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES2_PRO_WORDLEN_23_19:
177c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 19 + padded;
178c284d4e3SMaruthi Srinivas Bayyavarapu break;
179c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES2_PRO_WORDLEN_24_20:
180c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 20 + padded;
181c284d4e3SMaruthi Srinivas Bayyavarapu break;
182c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES2_PRO_WORDLEN_NOTID:
183c284d4e3SMaruthi Srinivas Bayyavarapu default:
184c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = XLNX_PARAM_UNKNOWN;
185c284d4e3SMaruthi Srinivas Bayyavarapu break;
186c284d4e3SMaruthi Srinivas Bayyavarapu }
187c284d4e3SMaruthi Srinivas Bayyavarapu
188c284d4e3SMaruthi Srinivas Bayyavarapu } else {
189c284d4e3SMaruthi Srinivas Bayyavarapu status[0] = (chsts_reg1_val >> 24) & 0xff;
190c284d4e3SMaruthi Srinivas Bayyavarapu status[1] = chsts_reg2_val & 0xff;
191c284d4e3SMaruthi Srinivas Bayyavarapu
192c284d4e3SMaruthi Srinivas Bayyavarapu switch (status[0] & IEC958_AES3_CON_FS) {
193c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES3_CON_FS_44100:
194c284d4e3SMaruthi Srinivas Bayyavarapu srate = 44100;
195c284d4e3SMaruthi Srinivas Bayyavarapu break;
196c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES3_CON_FS_48000:
197c284d4e3SMaruthi Srinivas Bayyavarapu srate = 48000;
198c284d4e3SMaruthi Srinivas Bayyavarapu break;
199c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES3_CON_FS_32000:
200c284d4e3SMaruthi Srinivas Bayyavarapu srate = 32000;
201c284d4e3SMaruthi Srinivas Bayyavarapu break;
202c284d4e3SMaruthi Srinivas Bayyavarapu default:
203c284d4e3SMaruthi Srinivas Bayyavarapu srate = XLNX_PARAM_UNKNOWN;
204c284d4e3SMaruthi Srinivas Bayyavarapu break;
205c284d4e3SMaruthi Srinivas Bayyavarapu }
206c284d4e3SMaruthi Srinivas Bayyavarapu
207c284d4e3SMaruthi Srinivas Bayyavarapu if (status[1] & IEC958_AES4_CON_MAX_WORDLEN_24)
208c284d4e3SMaruthi Srinivas Bayyavarapu padded = 4;
209c284d4e3SMaruthi Srinivas Bayyavarapu else
210c284d4e3SMaruthi Srinivas Bayyavarapu padded = 0;
211c284d4e3SMaruthi Srinivas Bayyavarapu
212c284d4e3SMaruthi Srinivas Bayyavarapu switch (status[1] & IEC958_AES4_CON_WORDLEN) {
213c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES4_CON_WORDLEN_20_16:
214c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 16 + padded;
215c284d4e3SMaruthi Srinivas Bayyavarapu break;
216c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES4_CON_WORDLEN_22_18:
217c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 18 + padded;
218c284d4e3SMaruthi Srinivas Bayyavarapu break;
219c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES4_CON_WORDLEN_23_19:
220c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 19 + padded;
221c284d4e3SMaruthi Srinivas Bayyavarapu break;
222c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES4_CON_WORDLEN_24_20:
223c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 20 + padded;
224c284d4e3SMaruthi Srinivas Bayyavarapu break;
225c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES4_CON_WORDLEN_21_17:
226c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = 17 + padded;
227c284d4e3SMaruthi Srinivas Bayyavarapu break;
228c284d4e3SMaruthi Srinivas Bayyavarapu case IEC958_AES4_CON_WORDLEN_NOTID:
229c284d4e3SMaruthi Srinivas Bayyavarapu default:
230c284d4e3SMaruthi Srinivas Bayyavarapu bit_depth = XLNX_PARAM_UNKNOWN;
231c284d4e3SMaruthi Srinivas Bayyavarapu break;
232c284d4e3SMaruthi Srinivas Bayyavarapu }
233c284d4e3SMaruthi Srinivas Bayyavarapu }
234c284d4e3SMaruthi Srinivas Bayyavarapu
235c284d4e3SMaruthi Srinivas Bayyavarapu log_params:
236c284d4e3SMaruthi Srinivas Bayyavarapu if (srate != XLNX_PARAM_UNKNOWN)
237c284d4e3SMaruthi Srinivas Bayyavarapu dev_info(dev, "sample rate = %d\n", srate);
238c284d4e3SMaruthi Srinivas Bayyavarapu else
239c284d4e3SMaruthi Srinivas Bayyavarapu dev_info(dev, "sample rate = unknown\n");
240c284d4e3SMaruthi Srinivas Bayyavarapu
241c284d4e3SMaruthi Srinivas Bayyavarapu if (bit_depth != XLNX_PARAM_UNKNOWN)
242c284d4e3SMaruthi Srinivas Bayyavarapu dev_info(dev, "bit_depth = %d\n", bit_depth);
243c284d4e3SMaruthi Srinivas Bayyavarapu else
244c284d4e3SMaruthi Srinivas Bayyavarapu dev_info(dev, "bit_depth = unknown\n");
245c284d4e3SMaruthi Srinivas Bayyavarapu }
246c284d4e3SMaruthi Srinivas Bayyavarapu
xlnx_formatter_pcm_reset(void __iomem * mmio_base)2476f6c3c36SMaruthi Srinivas Bayyavarapu static int xlnx_formatter_pcm_reset(void __iomem *mmio_base)
2486f6c3c36SMaruthi Srinivas Bayyavarapu {
2496f6c3c36SMaruthi Srinivas Bayyavarapu u32 val, retries = 0;
2506f6c3c36SMaruthi Srinivas Bayyavarapu
2516f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(mmio_base + XLNX_AUD_CTRL);
2526f6c3c36SMaruthi Srinivas Bayyavarapu val |= AUD_CTRL_RESET_MASK;
2536f6c3c36SMaruthi Srinivas Bayyavarapu writel(val, mmio_base + XLNX_AUD_CTRL);
2546f6c3c36SMaruthi Srinivas Bayyavarapu
2556f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(mmio_base + XLNX_AUD_CTRL);
2566f6c3c36SMaruthi Srinivas Bayyavarapu /* Poll for maximum timeout of approximately 100ms (1 * 100)*/
2576f6c3c36SMaruthi Srinivas Bayyavarapu while ((val & AUD_CTRL_RESET_MASK) && (retries < 100)) {
2586f6c3c36SMaruthi Srinivas Bayyavarapu mdelay(1);
2596f6c3c36SMaruthi Srinivas Bayyavarapu retries++;
2606f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(mmio_base + XLNX_AUD_CTRL);
2616f6c3c36SMaruthi Srinivas Bayyavarapu }
2626f6c3c36SMaruthi Srinivas Bayyavarapu if (val & AUD_CTRL_RESET_MASK)
2636f6c3c36SMaruthi Srinivas Bayyavarapu return -ENODEV;
2646f6c3c36SMaruthi Srinivas Bayyavarapu
2656f6c3c36SMaruthi Srinivas Bayyavarapu return 0;
2666f6c3c36SMaruthi Srinivas Bayyavarapu }
2676f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_formatter_disable_irqs(void __iomem * mmio_base,int stream)2686f6c3c36SMaruthi Srinivas Bayyavarapu static void xlnx_formatter_disable_irqs(void __iomem *mmio_base, int stream)
2696f6c3c36SMaruthi Srinivas Bayyavarapu {
2706f6c3c36SMaruthi Srinivas Bayyavarapu u32 val;
2716f6c3c36SMaruthi Srinivas Bayyavarapu
2726f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(mmio_base + XLNX_AUD_CTRL);
2736f6c3c36SMaruthi Srinivas Bayyavarapu val &= ~AUD_CTRL_IOC_IRQ_MASK;
2746f6c3c36SMaruthi Srinivas Bayyavarapu if (stream == SNDRV_PCM_STREAM_CAPTURE)
2756f6c3c36SMaruthi Srinivas Bayyavarapu val &= ~AUD_CTRL_TOUT_IRQ_MASK;
2766f6c3c36SMaruthi Srinivas Bayyavarapu
2776f6c3c36SMaruthi Srinivas Bayyavarapu writel(val, mmio_base + XLNX_AUD_CTRL);
2786f6c3c36SMaruthi Srinivas Bayyavarapu }
2796f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_mm2s_irq_handler(int irq,void * arg)2806f6c3c36SMaruthi Srinivas Bayyavarapu static irqreturn_t xlnx_mm2s_irq_handler(int irq, void *arg)
2816f6c3c36SMaruthi Srinivas Bayyavarapu {
2826f6c3c36SMaruthi Srinivas Bayyavarapu u32 val;
2836f6c3c36SMaruthi Srinivas Bayyavarapu void __iomem *reg;
2846f6c3c36SMaruthi Srinivas Bayyavarapu struct device *dev = arg;
2856f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_drv_data *adata = dev_get_drvdata(dev);
2866f6c3c36SMaruthi Srinivas Bayyavarapu
2876f6c3c36SMaruthi Srinivas Bayyavarapu reg = adata->mmio + XLNX_MM2S_OFFSET + XLNX_AUD_STS;
2886f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(reg);
2896f6c3c36SMaruthi Srinivas Bayyavarapu if (val & AUD_STS_IOC_IRQ_MASK) {
2906f6c3c36SMaruthi Srinivas Bayyavarapu writel(val & AUD_STS_IOC_IRQ_MASK, reg);
2916f6c3c36SMaruthi Srinivas Bayyavarapu if (adata->play_stream)
2926f6c3c36SMaruthi Srinivas Bayyavarapu snd_pcm_period_elapsed(adata->play_stream);
2936f6c3c36SMaruthi Srinivas Bayyavarapu return IRQ_HANDLED;
2946f6c3c36SMaruthi Srinivas Bayyavarapu }
2956f6c3c36SMaruthi Srinivas Bayyavarapu
2966f6c3c36SMaruthi Srinivas Bayyavarapu return IRQ_NONE;
2976f6c3c36SMaruthi Srinivas Bayyavarapu }
2986f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_s2mm_irq_handler(int irq,void * arg)2996f6c3c36SMaruthi Srinivas Bayyavarapu static irqreturn_t xlnx_s2mm_irq_handler(int irq, void *arg)
3006f6c3c36SMaruthi Srinivas Bayyavarapu {
3016f6c3c36SMaruthi Srinivas Bayyavarapu u32 val;
3026f6c3c36SMaruthi Srinivas Bayyavarapu void __iomem *reg;
3036f6c3c36SMaruthi Srinivas Bayyavarapu struct device *dev = arg;
3046f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_drv_data *adata = dev_get_drvdata(dev);
3056f6c3c36SMaruthi Srinivas Bayyavarapu
3066f6c3c36SMaruthi Srinivas Bayyavarapu reg = adata->mmio + XLNX_S2MM_OFFSET + XLNX_AUD_STS;
3076f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(reg);
3086f6c3c36SMaruthi Srinivas Bayyavarapu if (val & AUD_STS_IOC_IRQ_MASK) {
3096f6c3c36SMaruthi Srinivas Bayyavarapu writel(val & AUD_STS_IOC_IRQ_MASK, reg);
3106f6c3c36SMaruthi Srinivas Bayyavarapu if (adata->capture_stream)
3116f6c3c36SMaruthi Srinivas Bayyavarapu snd_pcm_period_elapsed(adata->capture_stream);
3126f6c3c36SMaruthi Srinivas Bayyavarapu return IRQ_HANDLED;
3136f6c3c36SMaruthi Srinivas Bayyavarapu }
3146f6c3c36SMaruthi Srinivas Bayyavarapu
3156f6c3c36SMaruthi Srinivas Bayyavarapu return IRQ_NONE;
3166f6c3c36SMaruthi Srinivas Bayyavarapu }
3176f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_formatter_set_sysclk(struct snd_soc_component * component,int clk_id,int source,unsigned int freq,int dir)3181c5091fbSRobert Hancock static int xlnx_formatter_set_sysclk(struct snd_soc_component *component,
3191c5091fbSRobert Hancock int clk_id, int source, unsigned int freq, int dir)
3201c5091fbSRobert Hancock {
3211c5091fbSRobert Hancock struct xlnx_pcm_drv_data *adata = dev_get_drvdata(component->dev);
3221c5091fbSRobert Hancock
3231c5091fbSRobert Hancock adata->sysclk = freq;
3241c5091fbSRobert Hancock return 0;
3251c5091fbSRobert Hancock }
3261c5091fbSRobert Hancock
xlnx_formatter_pcm_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)3272bcba424SKuninori Morimoto static int xlnx_formatter_pcm_open(struct snd_soc_component *component,
3282bcba424SKuninori Morimoto struct snd_pcm_substream *substream)
3296f6c3c36SMaruthi Srinivas Bayyavarapu {
3306f6c3c36SMaruthi Srinivas Bayyavarapu int err;
3316f6c3c36SMaruthi Srinivas Bayyavarapu u32 val, data_format_mode;
3326f6c3c36SMaruthi Srinivas Bayyavarapu u32 ch_count_mask, ch_count_shift, data_xfer_mode, data_xfer_shift;
3336f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_stream_param *stream_data;
3346f6c3c36SMaruthi Srinivas Bayyavarapu struct snd_pcm_runtime *runtime = substream->runtime;
3356f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_drv_data *adata = dev_get_drvdata(component->dev);
3366f6c3c36SMaruthi Srinivas Bayyavarapu
3376f6c3c36SMaruthi Srinivas Bayyavarapu if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
3386f6c3c36SMaruthi Srinivas Bayyavarapu !adata->mm2s_presence)
3396f6c3c36SMaruthi Srinivas Bayyavarapu return -ENODEV;
3406f6c3c36SMaruthi Srinivas Bayyavarapu else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
3416f6c3c36SMaruthi Srinivas Bayyavarapu !adata->s2mm_presence)
3426f6c3c36SMaruthi Srinivas Bayyavarapu return -ENODEV;
3436f6c3c36SMaruthi Srinivas Bayyavarapu
3446f6c3c36SMaruthi Srinivas Bayyavarapu stream_data = kzalloc(sizeof(*stream_data), GFP_KERNEL);
3456f6c3c36SMaruthi Srinivas Bayyavarapu if (!stream_data)
3466f6c3c36SMaruthi Srinivas Bayyavarapu return -ENOMEM;
3476f6c3c36SMaruthi Srinivas Bayyavarapu
3486f6c3c36SMaruthi Srinivas Bayyavarapu if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3496f6c3c36SMaruthi Srinivas Bayyavarapu ch_count_mask = CFG_MM2S_CH_MASK;
3506f6c3c36SMaruthi Srinivas Bayyavarapu ch_count_shift = CFG_MM2S_CH_SHIFT;
3516f6c3c36SMaruthi Srinivas Bayyavarapu data_xfer_mode = CFG_MM2S_XFER_MASK;
3526f6c3c36SMaruthi Srinivas Bayyavarapu data_xfer_shift = CFG_MM2S_XFER_SHIFT;
3536f6c3c36SMaruthi Srinivas Bayyavarapu data_format_mode = CFG_MM2S_PKG_MASK;
3546f6c3c36SMaruthi Srinivas Bayyavarapu stream_data->mmio = adata->mmio + XLNX_MM2S_OFFSET;
3556f6c3c36SMaruthi Srinivas Bayyavarapu adata->play_stream = substream;
3566f6c3c36SMaruthi Srinivas Bayyavarapu
3576f6c3c36SMaruthi Srinivas Bayyavarapu } else {
3586f6c3c36SMaruthi Srinivas Bayyavarapu ch_count_mask = CFG_S2MM_CH_MASK;
3596f6c3c36SMaruthi Srinivas Bayyavarapu ch_count_shift = CFG_S2MM_CH_SHIFT;
3606f6c3c36SMaruthi Srinivas Bayyavarapu data_xfer_mode = CFG_S2MM_XFER_MASK;
3616f6c3c36SMaruthi Srinivas Bayyavarapu data_xfer_shift = CFG_S2MM_XFER_SHIFT;
3626f6c3c36SMaruthi Srinivas Bayyavarapu data_format_mode = CFG_S2MM_PKG_MASK;
3636f6c3c36SMaruthi Srinivas Bayyavarapu stream_data->mmio = adata->mmio + XLNX_S2MM_OFFSET;
3646f6c3c36SMaruthi Srinivas Bayyavarapu adata->capture_stream = substream;
3656f6c3c36SMaruthi Srinivas Bayyavarapu }
3666f6c3c36SMaruthi Srinivas Bayyavarapu
3676f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(adata->mmio + XLNX_AUD_CORE_CONFIG);
3686f6c3c36SMaruthi Srinivas Bayyavarapu
3696f6c3c36SMaruthi Srinivas Bayyavarapu if (!(val & data_format_mode))
3706f6c3c36SMaruthi Srinivas Bayyavarapu stream_data->interleaved = true;
3716f6c3c36SMaruthi Srinivas Bayyavarapu
3726f6c3c36SMaruthi Srinivas Bayyavarapu stream_data->xfer_mode = (val & data_xfer_mode) >> data_xfer_shift;
3736f6c3c36SMaruthi Srinivas Bayyavarapu stream_data->ch_limit = (val & ch_count_mask) >> ch_count_shift;
3746f6c3c36SMaruthi Srinivas Bayyavarapu dev_info(component->dev,
3756f6c3c36SMaruthi Srinivas Bayyavarapu "stream %d : format = %d mode = %d ch_limit = %d\n",
3766f6c3c36SMaruthi Srinivas Bayyavarapu substream->stream, stream_data->interleaved,
3776f6c3c36SMaruthi Srinivas Bayyavarapu stream_data->xfer_mode, stream_data->ch_limit);
3786f6c3c36SMaruthi Srinivas Bayyavarapu
3796f6c3c36SMaruthi Srinivas Bayyavarapu snd_soc_set_runtime_hwparams(substream, &xlnx_pcm_hardware);
3806f6c3c36SMaruthi Srinivas Bayyavarapu runtime->private_data = stream_data;
3816f6c3c36SMaruthi Srinivas Bayyavarapu
382e958b588SRobert Hancock /* Resize the period bytes as divisible by 64 */
3836f6c3c36SMaruthi Srinivas Bayyavarapu err = snd_pcm_hw_constraint_step(runtime, 0,
384e958b588SRobert Hancock SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
385e958b588SRobert Hancock XLNX_AUD_ALIGN_BYTES);
3866f6c3c36SMaruthi Srinivas Bayyavarapu if (err) {
3876f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(component->dev,
388e958b588SRobert Hancock "Unable to set constraint on period bytes\n");
389e958b588SRobert Hancock return err;
390e958b588SRobert Hancock }
391e958b588SRobert Hancock
392e958b588SRobert Hancock /* Resize the buffer bytes as divisible by 64 */
393e958b588SRobert Hancock err = snd_pcm_hw_constraint_step(runtime, 0,
394e958b588SRobert Hancock SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
395e958b588SRobert Hancock XLNX_AUD_ALIGN_BYTES);
396e958b588SRobert Hancock if (err) {
397e958b588SRobert Hancock dev_err(component->dev,
398e958b588SRobert Hancock "Unable to set constraint on buffer bytes\n");
399e958b588SRobert Hancock return err;
400e958b588SRobert Hancock }
401e958b588SRobert Hancock
402e958b588SRobert Hancock /* Set periods as integer multiple */
403e958b588SRobert Hancock err = snd_pcm_hw_constraint_integer(runtime,
404e958b588SRobert Hancock SNDRV_PCM_HW_PARAM_PERIODS);
405e958b588SRobert Hancock if (err < 0) {
406e958b588SRobert Hancock dev_err(component->dev,
407e958b588SRobert Hancock "Unable to set constraint on periods to be integer\n");
4086f6c3c36SMaruthi Srinivas Bayyavarapu return err;
4096f6c3c36SMaruthi Srinivas Bayyavarapu }
4106f6c3c36SMaruthi Srinivas Bayyavarapu
4116f6c3c36SMaruthi Srinivas Bayyavarapu /* enable DMA IOC irq */
4126f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(stream_data->mmio + XLNX_AUD_CTRL);
4136f6c3c36SMaruthi Srinivas Bayyavarapu val |= AUD_CTRL_IOC_IRQ_MASK;
4146f6c3c36SMaruthi Srinivas Bayyavarapu writel(val, stream_data->mmio + XLNX_AUD_CTRL);
4156f6c3c36SMaruthi Srinivas Bayyavarapu
4166f6c3c36SMaruthi Srinivas Bayyavarapu return 0;
4176f6c3c36SMaruthi Srinivas Bayyavarapu }
4186f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_formatter_pcm_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)4192bcba424SKuninori Morimoto static int xlnx_formatter_pcm_close(struct snd_soc_component *component,
4202bcba424SKuninori Morimoto struct snd_pcm_substream *substream)
4216f6c3c36SMaruthi Srinivas Bayyavarapu {
4226f6c3c36SMaruthi Srinivas Bayyavarapu int ret;
4236f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_stream_param *stream_data =
4246f6c3c36SMaruthi Srinivas Bayyavarapu substream->runtime->private_data;
4256f6c3c36SMaruthi Srinivas Bayyavarapu
4266f6c3c36SMaruthi Srinivas Bayyavarapu ret = xlnx_formatter_pcm_reset(stream_data->mmio);
4276f6c3c36SMaruthi Srinivas Bayyavarapu if (ret) {
4286f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(component->dev, "audio formatter reset failed\n");
4296f6c3c36SMaruthi Srinivas Bayyavarapu goto err_reset;
4306f6c3c36SMaruthi Srinivas Bayyavarapu }
4316f6c3c36SMaruthi Srinivas Bayyavarapu xlnx_formatter_disable_irqs(stream_data->mmio, substream->stream);
4326f6c3c36SMaruthi Srinivas Bayyavarapu
4336f6c3c36SMaruthi Srinivas Bayyavarapu err_reset:
4346f6c3c36SMaruthi Srinivas Bayyavarapu kfree(stream_data);
4356f6c3c36SMaruthi Srinivas Bayyavarapu return 0;
4366f6c3c36SMaruthi Srinivas Bayyavarapu }
4376f6c3c36SMaruthi Srinivas Bayyavarapu
4386f6c3c36SMaruthi Srinivas Bayyavarapu static snd_pcm_uframes_t
xlnx_formatter_pcm_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)4392bcba424SKuninori Morimoto xlnx_formatter_pcm_pointer(struct snd_soc_component *component,
4402bcba424SKuninori Morimoto struct snd_pcm_substream *substream)
4416f6c3c36SMaruthi Srinivas Bayyavarapu {
4426f6c3c36SMaruthi Srinivas Bayyavarapu u32 pos;
4436f6c3c36SMaruthi Srinivas Bayyavarapu struct snd_pcm_runtime *runtime = substream->runtime;
4446f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_stream_param *stream_data = runtime->private_data;
4456f6c3c36SMaruthi Srinivas Bayyavarapu
4466f6c3c36SMaruthi Srinivas Bayyavarapu pos = readl(stream_data->mmio + XLNX_AUD_XFER_COUNT);
4476f6c3c36SMaruthi Srinivas Bayyavarapu
4486f6c3c36SMaruthi Srinivas Bayyavarapu if (pos >= stream_data->buffer_size)
4496f6c3c36SMaruthi Srinivas Bayyavarapu pos = 0;
4506f6c3c36SMaruthi Srinivas Bayyavarapu
4516f6c3c36SMaruthi Srinivas Bayyavarapu return bytes_to_frames(runtime, pos);
4526f6c3c36SMaruthi Srinivas Bayyavarapu }
4536f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_formatter_pcm_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)4542bcba424SKuninori Morimoto static int xlnx_formatter_pcm_hw_params(struct snd_soc_component *component,
4552bcba424SKuninori Morimoto struct snd_pcm_substream *substream,
4566f6c3c36SMaruthi Srinivas Bayyavarapu struct snd_pcm_hw_params *params)
4576f6c3c36SMaruthi Srinivas Bayyavarapu {
4586f6c3c36SMaruthi Srinivas Bayyavarapu u32 low, high, active_ch, val, bytes_per_ch, bits_per_sample;
459c284d4e3SMaruthi Srinivas Bayyavarapu u32 aes_reg1_val, aes_reg2_val;
4606f6c3c36SMaruthi Srinivas Bayyavarapu u64 size;
4616f6c3c36SMaruthi Srinivas Bayyavarapu struct snd_pcm_runtime *runtime = substream->runtime;
4626f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_stream_param *stream_data = runtime->private_data;
4631c5091fbSRobert Hancock struct xlnx_pcm_drv_data *adata = dev_get_drvdata(component->dev);
4646f6c3c36SMaruthi Srinivas Bayyavarapu
4656f6c3c36SMaruthi Srinivas Bayyavarapu active_ch = params_channels(params);
4666f6c3c36SMaruthi Srinivas Bayyavarapu if (active_ch > stream_data->ch_limit)
4676f6c3c36SMaruthi Srinivas Bayyavarapu return -EINVAL;
4686f6c3c36SMaruthi Srinivas Bayyavarapu
4691c5091fbSRobert Hancock if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
4701c5091fbSRobert Hancock adata->sysclk) {
4711c5091fbSRobert Hancock unsigned int mclk_fs = adata->sysclk / params_rate(params);
4721c5091fbSRobert Hancock
4731c5091fbSRobert Hancock if (adata->sysclk % params_rate(params) != 0) {
4741c5091fbSRobert Hancock dev_warn(component->dev, "sysclk %u not divisible by rate %u\n",
4751c5091fbSRobert Hancock adata->sysclk, params_rate(params));
4761c5091fbSRobert Hancock return -EINVAL;
4771c5091fbSRobert Hancock }
4781c5091fbSRobert Hancock
4791c5091fbSRobert Hancock writel(mclk_fs, stream_data->mmio + XLNX_AUD_FS_MULTIPLIER);
4801c5091fbSRobert Hancock }
4811c5091fbSRobert Hancock
482c284d4e3SMaruthi Srinivas Bayyavarapu if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
483c284d4e3SMaruthi Srinivas Bayyavarapu stream_data->xfer_mode == AES_TO_PCM) {
484c284d4e3SMaruthi Srinivas Bayyavarapu val = readl(stream_data->mmio + XLNX_AUD_STS);
485c284d4e3SMaruthi Srinivas Bayyavarapu if (val & AUD_STS_CH_STS_MASK) {
486c284d4e3SMaruthi Srinivas Bayyavarapu aes_reg1_val = readl(stream_data->mmio +
487c284d4e3SMaruthi Srinivas Bayyavarapu XLNX_AUD_CH_STS_START);
488c284d4e3SMaruthi Srinivas Bayyavarapu aes_reg2_val = readl(stream_data->mmio +
489c284d4e3SMaruthi Srinivas Bayyavarapu XLNX_AUD_CH_STS_START + 0x4);
490c284d4e3SMaruthi Srinivas Bayyavarapu
491c284d4e3SMaruthi Srinivas Bayyavarapu xlnx_parse_aes_params(aes_reg1_val, aes_reg2_val,
492c284d4e3SMaruthi Srinivas Bayyavarapu component->dev);
493c284d4e3SMaruthi Srinivas Bayyavarapu }
494c284d4e3SMaruthi Srinivas Bayyavarapu }
495c284d4e3SMaruthi Srinivas Bayyavarapu
4966f6c3c36SMaruthi Srinivas Bayyavarapu size = params_buffer_bytes(params);
4976f6c3c36SMaruthi Srinivas Bayyavarapu
4986f6c3c36SMaruthi Srinivas Bayyavarapu stream_data->buffer_size = size;
4996f6c3c36SMaruthi Srinivas Bayyavarapu
50042bc62c9STakashi Iwai low = lower_32_bits(runtime->dma_addr);
50142bc62c9STakashi Iwai high = upper_32_bits(runtime->dma_addr);
5026f6c3c36SMaruthi Srinivas Bayyavarapu writel(low, stream_data->mmio + XLNX_AUD_BUFF_ADDR_LSB);
5036f6c3c36SMaruthi Srinivas Bayyavarapu writel(high, stream_data->mmio + XLNX_AUD_BUFF_ADDR_MSB);
5046f6c3c36SMaruthi Srinivas Bayyavarapu
5056f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(stream_data->mmio + XLNX_AUD_CTRL);
5066f6c3c36SMaruthi Srinivas Bayyavarapu bits_per_sample = params_width(params);
5076f6c3c36SMaruthi Srinivas Bayyavarapu switch (bits_per_sample) {
5086f6c3c36SMaruthi Srinivas Bayyavarapu case 8:
5096f6c3c36SMaruthi Srinivas Bayyavarapu val |= (BIT_DEPTH_8 << AUD_CTRL_DATA_WIDTH_SHIFT);
5106f6c3c36SMaruthi Srinivas Bayyavarapu break;
5116f6c3c36SMaruthi Srinivas Bayyavarapu case 16:
5126f6c3c36SMaruthi Srinivas Bayyavarapu val |= (BIT_DEPTH_16 << AUD_CTRL_DATA_WIDTH_SHIFT);
5136f6c3c36SMaruthi Srinivas Bayyavarapu break;
5146f6c3c36SMaruthi Srinivas Bayyavarapu case 20:
5156f6c3c36SMaruthi Srinivas Bayyavarapu val |= (BIT_DEPTH_20 << AUD_CTRL_DATA_WIDTH_SHIFT);
5166f6c3c36SMaruthi Srinivas Bayyavarapu break;
5176f6c3c36SMaruthi Srinivas Bayyavarapu case 24:
5186f6c3c36SMaruthi Srinivas Bayyavarapu val |= (BIT_DEPTH_24 << AUD_CTRL_DATA_WIDTH_SHIFT);
5196f6c3c36SMaruthi Srinivas Bayyavarapu break;
5206f6c3c36SMaruthi Srinivas Bayyavarapu case 32:
5216f6c3c36SMaruthi Srinivas Bayyavarapu val |= (BIT_DEPTH_32 << AUD_CTRL_DATA_WIDTH_SHIFT);
5226f6c3c36SMaruthi Srinivas Bayyavarapu break;
5236f6c3c36SMaruthi Srinivas Bayyavarapu default:
5246f6c3c36SMaruthi Srinivas Bayyavarapu return -EINVAL;
5256f6c3c36SMaruthi Srinivas Bayyavarapu }
5266f6c3c36SMaruthi Srinivas Bayyavarapu
5276f6c3c36SMaruthi Srinivas Bayyavarapu val |= active_ch << AUD_CTRL_ACTIVE_CH_SHIFT;
5286f6c3c36SMaruthi Srinivas Bayyavarapu writel(val, stream_data->mmio + XLNX_AUD_CTRL);
5296f6c3c36SMaruthi Srinivas Bayyavarapu
5306f6c3c36SMaruthi Srinivas Bayyavarapu val = (params_periods(params) << PERIOD_CFG_PERIODS_SHIFT)
5316f6c3c36SMaruthi Srinivas Bayyavarapu | params_period_bytes(params);
5326f6c3c36SMaruthi Srinivas Bayyavarapu writel(val, stream_data->mmio + XLNX_AUD_PERIOD_CONFIG);
5336f6c3c36SMaruthi Srinivas Bayyavarapu bytes_per_ch = DIV_ROUND_UP(params_period_bytes(params), active_ch);
5346f6c3c36SMaruthi Srinivas Bayyavarapu writel(bytes_per_ch, stream_data->mmio + XLNX_BYTES_PER_CH);
5356f6c3c36SMaruthi Srinivas Bayyavarapu
5366f6c3c36SMaruthi Srinivas Bayyavarapu return 0;
5376f6c3c36SMaruthi Srinivas Bayyavarapu }
5386f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_formatter_pcm_trigger(struct snd_soc_component * component,struct snd_pcm_substream * substream,int cmd)5392bcba424SKuninori Morimoto static int xlnx_formatter_pcm_trigger(struct snd_soc_component *component,
5402bcba424SKuninori Morimoto struct snd_pcm_substream *substream,
5416f6c3c36SMaruthi Srinivas Bayyavarapu int cmd)
5426f6c3c36SMaruthi Srinivas Bayyavarapu {
5436f6c3c36SMaruthi Srinivas Bayyavarapu u32 val;
5446f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_stream_param *stream_data =
5456f6c3c36SMaruthi Srinivas Bayyavarapu substream->runtime->private_data;
5466f6c3c36SMaruthi Srinivas Bayyavarapu
5476f6c3c36SMaruthi Srinivas Bayyavarapu switch (cmd) {
5486f6c3c36SMaruthi Srinivas Bayyavarapu case SNDRV_PCM_TRIGGER_START:
5496f6c3c36SMaruthi Srinivas Bayyavarapu case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
5506f6c3c36SMaruthi Srinivas Bayyavarapu case SNDRV_PCM_TRIGGER_RESUME:
5516f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(stream_data->mmio + XLNX_AUD_CTRL);
5526f6c3c36SMaruthi Srinivas Bayyavarapu val |= AUD_CTRL_DMA_EN_MASK;
5536f6c3c36SMaruthi Srinivas Bayyavarapu writel(val, stream_data->mmio + XLNX_AUD_CTRL);
5546f6c3c36SMaruthi Srinivas Bayyavarapu break;
5556f6c3c36SMaruthi Srinivas Bayyavarapu case SNDRV_PCM_TRIGGER_STOP:
5566f6c3c36SMaruthi Srinivas Bayyavarapu case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
5576f6c3c36SMaruthi Srinivas Bayyavarapu case SNDRV_PCM_TRIGGER_SUSPEND:
5586f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(stream_data->mmio + XLNX_AUD_CTRL);
5596f6c3c36SMaruthi Srinivas Bayyavarapu val &= ~AUD_CTRL_DMA_EN_MASK;
5606f6c3c36SMaruthi Srinivas Bayyavarapu writel(val, stream_data->mmio + XLNX_AUD_CTRL);
5616f6c3c36SMaruthi Srinivas Bayyavarapu break;
5626f6c3c36SMaruthi Srinivas Bayyavarapu }
5636f6c3c36SMaruthi Srinivas Bayyavarapu
5646f6c3c36SMaruthi Srinivas Bayyavarapu return 0;
5656f6c3c36SMaruthi Srinivas Bayyavarapu }
5666f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_formatter_pcm_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)5672bcba424SKuninori Morimoto static int xlnx_formatter_pcm_new(struct snd_soc_component *component,
5682bcba424SKuninori Morimoto struct snd_soc_pcm_runtime *rtd)
5696f6c3c36SMaruthi Srinivas Bayyavarapu {
57052f0ac15STakashi Iwai snd_pcm_set_managed_buffer_all(rtd->pcm,
5716f6c3c36SMaruthi Srinivas Bayyavarapu SNDRV_DMA_TYPE_DEV, component->dev,
5726f6c3c36SMaruthi Srinivas Bayyavarapu xlnx_pcm_hardware.buffer_bytes_max,
5736f6c3c36SMaruthi Srinivas Bayyavarapu xlnx_pcm_hardware.buffer_bytes_max);
57495d14640SStephen Rothwell return 0;
5756f6c3c36SMaruthi Srinivas Bayyavarapu }
5766f6c3c36SMaruthi Srinivas Bayyavarapu
5772bcba424SKuninori Morimoto static const struct snd_soc_component_driver xlnx_asoc_component = {
5782bcba424SKuninori Morimoto .name = DRV_NAME,
5791c5091fbSRobert Hancock .set_sysclk = xlnx_formatter_set_sysclk,
5806f6c3c36SMaruthi Srinivas Bayyavarapu .open = xlnx_formatter_pcm_open,
5816f6c3c36SMaruthi Srinivas Bayyavarapu .close = xlnx_formatter_pcm_close,
5826f6c3c36SMaruthi Srinivas Bayyavarapu .hw_params = xlnx_formatter_pcm_hw_params,
5836f6c3c36SMaruthi Srinivas Bayyavarapu .trigger = xlnx_formatter_pcm_trigger,
5846f6c3c36SMaruthi Srinivas Bayyavarapu .pointer = xlnx_formatter_pcm_pointer,
5852bcba424SKuninori Morimoto .pcm_construct = xlnx_formatter_pcm_new,
5866f6c3c36SMaruthi Srinivas Bayyavarapu };
5876f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_formatter_pcm_probe(struct platform_device * pdev)5886f6c3c36SMaruthi Srinivas Bayyavarapu static int xlnx_formatter_pcm_probe(struct platform_device *pdev)
5896f6c3c36SMaruthi Srinivas Bayyavarapu {
5906f6c3c36SMaruthi Srinivas Bayyavarapu int ret;
5916f6c3c36SMaruthi Srinivas Bayyavarapu u32 val;
5926f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_drv_data *aud_drv_data;
5936f6c3c36SMaruthi Srinivas Bayyavarapu struct device *dev = &pdev->dev;
5946f6c3c36SMaruthi Srinivas Bayyavarapu
5956f6c3c36SMaruthi Srinivas Bayyavarapu aud_drv_data = devm_kzalloc(dev, sizeof(*aud_drv_data), GFP_KERNEL);
5966f6c3c36SMaruthi Srinivas Bayyavarapu if (!aud_drv_data)
5976f6c3c36SMaruthi Srinivas Bayyavarapu return -ENOMEM;
5986f6c3c36SMaruthi Srinivas Bayyavarapu
5996f6c3c36SMaruthi Srinivas Bayyavarapu aud_drv_data->axi_clk = devm_clk_get(dev, "s_axi_lite_aclk");
6006f6c3c36SMaruthi Srinivas Bayyavarapu if (IS_ERR(aud_drv_data->axi_clk)) {
6016f6c3c36SMaruthi Srinivas Bayyavarapu ret = PTR_ERR(aud_drv_data->axi_clk);
6026f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(dev, "failed to get s_axi_lite_aclk(%d)\n", ret);
6036f6c3c36SMaruthi Srinivas Bayyavarapu return ret;
6046f6c3c36SMaruthi Srinivas Bayyavarapu }
6056f6c3c36SMaruthi Srinivas Bayyavarapu ret = clk_prepare_enable(aud_drv_data->axi_clk);
6066f6c3c36SMaruthi Srinivas Bayyavarapu if (ret) {
6076f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(dev,
6086f6c3c36SMaruthi Srinivas Bayyavarapu "failed to enable s_axi_lite_aclk(%d)\n", ret);
6096f6c3c36SMaruthi Srinivas Bayyavarapu return ret;
6106f6c3c36SMaruthi Srinivas Bayyavarapu }
6116f6c3c36SMaruthi Srinivas Bayyavarapu
6126cfb1cd6SMarkus Elfring aud_drv_data->mmio = devm_platform_ioremap_resource(pdev, 0);
6136f6c3c36SMaruthi Srinivas Bayyavarapu if (IS_ERR(aud_drv_data->mmio)) {
6146f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(dev, "audio formatter ioremap failed\n");
6156f6c3c36SMaruthi Srinivas Bayyavarapu ret = PTR_ERR(aud_drv_data->mmio);
6166f6c3c36SMaruthi Srinivas Bayyavarapu goto clk_err;
6176f6c3c36SMaruthi Srinivas Bayyavarapu }
6186f6c3c36SMaruthi Srinivas Bayyavarapu
6196f6c3c36SMaruthi Srinivas Bayyavarapu val = readl(aud_drv_data->mmio + XLNX_AUD_CORE_CONFIG);
6206f6c3c36SMaruthi Srinivas Bayyavarapu if (val & AUD_CFG_MM2S_MASK) {
6216f6c3c36SMaruthi Srinivas Bayyavarapu aud_drv_data->mm2s_presence = true;
6226f6c3c36SMaruthi Srinivas Bayyavarapu ret = xlnx_formatter_pcm_reset(aud_drv_data->mmio +
6236f6c3c36SMaruthi Srinivas Bayyavarapu XLNX_MM2S_OFFSET);
6246f6c3c36SMaruthi Srinivas Bayyavarapu if (ret) {
6256f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(dev, "audio formatter reset failed\n");
6266f6c3c36SMaruthi Srinivas Bayyavarapu goto clk_err;
6276f6c3c36SMaruthi Srinivas Bayyavarapu }
6286f6c3c36SMaruthi Srinivas Bayyavarapu xlnx_formatter_disable_irqs(aud_drv_data->mmio +
6296f6c3c36SMaruthi Srinivas Bayyavarapu XLNX_MM2S_OFFSET,
6306f6c3c36SMaruthi Srinivas Bayyavarapu SNDRV_PCM_STREAM_PLAYBACK);
6316f6c3c36SMaruthi Srinivas Bayyavarapu
6326f6c3c36SMaruthi Srinivas Bayyavarapu aud_drv_data->mm2s_irq = platform_get_irq_byname(pdev,
6336f6c3c36SMaruthi Srinivas Bayyavarapu "irq_mm2s");
6346f6c3c36SMaruthi Srinivas Bayyavarapu if (aud_drv_data->mm2s_irq < 0) {
6356f6c3c36SMaruthi Srinivas Bayyavarapu ret = aud_drv_data->mm2s_irq;
6366f6c3c36SMaruthi Srinivas Bayyavarapu goto clk_err;
6376f6c3c36SMaruthi Srinivas Bayyavarapu }
6386f6c3c36SMaruthi Srinivas Bayyavarapu ret = devm_request_irq(dev, aud_drv_data->mm2s_irq,
6396f6c3c36SMaruthi Srinivas Bayyavarapu xlnx_mm2s_irq_handler, 0,
6406f6c3c36SMaruthi Srinivas Bayyavarapu "xlnx_formatter_pcm_mm2s_irq", dev);
6416f6c3c36SMaruthi Srinivas Bayyavarapu if (ret) {
6426f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(dev, "xlnx audio mm2s irq request failed\n");
6436f6c3c36SMaruthi Srinivas Bayyavarapu goto clk_err;
6446f6c3c36SMaruthi Srinivas Bayyavarapu }
6456f6c3c36SMaruthi Srinivas Bayyavarapu }
6466f6c3c36SMaruthi Srinivas Bayyavarapu if (val & AUD_CFG_S2MM_MASK) {
6476f6c3c36SMaruthi Srinivas Bayyavarapu aud_drv_data->s2mm_presence = true;
6486f6c3c36SMaruthi Srinivas Bayyavarapu ret = xlnx_formatter_pcm_reset(aud_drv_data->mmio +
6496f6c3c36SMaruthi Srinivas Bayyavarapu XLNX_S2MM_OFFSET);
6506f6c3c36SMaruthi Srinivas Bayyavarapu if (ret) {
6516f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(dev, "audio formatter reset failed\n");
6526f6c3c36SMaruthi Srinivas Bayyavarapu goto clk_err;
6536f6c3c36SMaruthi Srinivas Bayyavarapu }
6546f6c3c36SMaruthi Srinivas Bayyavarapu xlnx_formatter_disable_irqs(aud_drv_data->mmio +
6556f6c3c36SMaruthi Srinivas Bayyavarapu XLNX_S2MM_OFFSET,
6566f6c3c36SMaruthi Srinivas Bayyavarapu SNDRV_PCM_STREAM_CAPTURE);
6576f6c3c36SMaruthi Srinivas Bayyavarapu
6586f6c3c36SMaruthi Srinivas Bayyavarapu aud_drv_data->s2mm_irq = platform_get_irq_byname(pdev,
6596f6c3c36SMaruthi Srinivas Bayyavarapu "irq_s2mm");
6606f6c3c36SMaruthi Srinivas Bayyavarapu if (aud_drv_data->s2mm_irq < 0) {
6616f6c3c36SMaruthi Srinivas Bayyavarapu ret = aud_drv_data->s2mm_irq;
6626f6c3c36SMaruthi Srinivas Bayyavarapu goto clk_err;
6636f6c3c36SMaruthi Srinivas Bayyavarapu }
6646f6c3c36SMaruthi Srinivas Bayyavarapu ret = devm_request_irq(dev, aud_drv_data->s2mm_irq,
6656f6c3c36SMaruthi Srinivas Bayyavarapu xlnx_s2mm_irq_handler, 0,
6666f6c3c36SMaruthi Srinivas Bayyavarapu "xlnx_formatter_pcm_s2mm_irq",
6676f6c3c36SMaruthi Srinivas Bayyavarapu dev);
6686f6c3c36SMaruthi Srinivas Bayyavarapu if (ret) {
6696f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(dev, "xlnx audio s2mm irq request failed\n");
6706f6c3c36SMaruthi Srinivas Bayyavarapu goto clk_err;
6716f6c3c36SMaruthi Srinivas Bayyavarapu }
6726f6c3c36SMaruthi Srinivas Bayyavarapu }
6736f6c3c36SMaruthi Srinivas Bayyavarapu
6746f6c3c36SMaruthi Srinivas Bayyavarapu dev_set_drvdata(dev, aud_drv_data);
6756f6c3c36SMaruthi Srinivas Bayyavarapu
6766f6c3c36SMaruthi Srinivas Bayyavarapu ret = devm_snd_soc_register_component(dev, &xlnx_asoc_component,
6776f6c3c36SMaruthi Srinivas Bayyavarapu NULL, 0);
6786f6c3c36SMaruthi Srinivas Bayyavarapu if (ret) {
6796f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(dev, "pcm platform device register failed\n");
6806f6c3c36SMaruthi Srinivas Bayyavarapu goto clk_err;
6816f6c3c36SMaruthi Srinivas Bayyavarapu }
6826f6c3c36SMaruthi Srinivas Bayyavarapu
6836f6c3c36SMaruthi Srinivas Bayyavarapu return 0;
6846f6c3c36SMaruthi Srinivas Bayyavarapu
6856f6c3c36SMaruthi Srinivas Bayyavarapu clk_err:
6866f6c3c36SMaruthi Srinivas Bayyavarapu clk_disable_unprepare(aud_drv_data->axi_clk);
6876f6c3c36SMaruthi Srinivas Bayyavarapu return ret;
6886f6c3c36SMaruthi Srinivas Bayyavarapu }
6896f6c3c36SMaruthi Srinivas Bayyavarapu
xlnx_formatter_pcm_remove(struct platform_device * pdev)690*bf6b5cedSUwe Kleine-König static void xlnx_formatter_pcm_remove(struct platform_device *pdev)
6916f6c3c36SMaruthi Srinivas Bayyavarapu {
6926f6c3c36SMaruthi Srinivas Bayyavarapu int ret = 0;
6936f6c3c36SMaruthi Srinivas Bayyavarapu struct xlnx_pcm_drv_data *adata = dev_get_drvdata(&pdev->dev);
6946f6c3c36SMaruthi Srinivas Bayyavarapu
6956f6c3c36SMaruthi Srinivas Bayyavarapu if (adata->s2mm_presence)
6966f6c3c36SMaruthi Srinivas Bayyavarapu ret = xlnx_formatter_pcm_reset(adata->mmio + XLNX_S2MM_OFFSET);
6976f6c3c36SMaruthi Srinivas Bayyavarapu
6986f6c3c36SMaruthi Srinivas Bayyavarapu /* Try MM2S reset, even if S2MM reset fails */
6996f6c3c36SMaruthi Srinivas Bayyavarapu if (adata->mm2s_presence)
7006f6c3c36SMaruthi Srinivas Bayyavarapu ret = xlnx_formatter_pcm_reset(adata->mmio + XLNX_MM2S_OFFSET);
7016f6c3c36SMaruthi Srinivas Bayyavarapu
7026f6c3c36SMaruthi Srinivas Bayyavarapu if (ret)
7036f6c3c36SMaruthi Srinivas Bayyavarapu dev_err(&pdev->dev, "audio formatter reset failed\n");
7046f6c3c36SMaruthi Srinivas Bayyavarapu
7056f6c3c36SMaruthi Srinivas Bayyavarapu clk_disable_unprepare(adata->axi_clk);
7066f6c3c36SMaruthi Srinivas Bayyavarapu }
7076f6c3c36SMaruthi Srinivas Bayyavarapu
7086f6c3c36SMaruthi Srinivas Bayyavarapu static const struct of_device_id xlnx_formatter_pcm_of_match[] = {
7096f6c3c36SMaruthi Srinivas Bayyavarapu { .compatible = "xlnx,audio-formatter-1.0"},
7106f6c3c36SMaruthi Srinivas Bayyavarapu {},
7116f6c3c36SMaruthi Srinivas Bayyavarapu };
7126f6c3c36SMaruthi Srinivas Bayyavarapu MODULE_DEVICE_TABLE(of, xlnx_formatter_pcm_of_match);
7136f6c3c36SMaruthi Srinivas Bayyavarapu
7146f6c3c36SMaruthi Srinivas Bayyavarapu static struct platform_driver xlnx_formatter_pcm_driver = {
7156f6c3c36SMaruthi Srinivas Bayyavarapu .probe = xlnx_formatter_pcm_probe,
716*bf6b5cedSUwe Kleine-König .remove_new = xlnx_formatter_pcm_remove,
7176f6c3c36SMaruthi Srinivas Bayyavarapu .driver = {
7186f6c3c36SMaruthi Srinivas Bayyavarapu .name = DRV_NAME,
7196f6c3c36SMaruthi Srinivas Bayyavarapu .of_match_table = xlnx_formatter_pcm_of_match,
7206f6c3c36SMaruthi Srinivas Bayyavarapu },
7216f6c3c36SMaruthi Srinivas Bayyavarapu };
7226f6c3c36SMaruthi Srinivas Bayyavarapu
7236f6c3c36SMaruthi Srinivas Bayyavarapu module_platform_driver(xlnx_formatter_pcm_driver);
7246f6c3c36SMaruthi Srinivas Bayyavarapu MODULE_AUTHOR("Maruthi Srinivas Bayyavarapu <maruthis@xilinx.com>");
7256f6c3c36SMaruthi Srinivas Bayyavarapu MODULE_LICENSE("GPL v2");
726