blob: 1203de8aab9909502d28e5da292cb0b2fb95ba63 [file] [log] [blame]
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001/*
2 * ALSA SoC McASP Audio Layer for TI DAVINCI processor
3 *
4 * Multi-channel Audio Serial Port Driver
5 *
6 * Author: Nirmal Pandey <n-pandey@ti.com>,
7 * Suresh Rajashekara <suresh.r@ti.com>
8 * Steve Chen <schen@.mvista.com>
9 *
10 * Copyright: (C) 2009 MontaVista Software, Inc., <source@mvista.com>
11 * Copyright: (C) 2009 Texas Instruments, India
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040022#include <linux/delay.h>
23#include <linux/io.h>
Peter Ujfalusiae726e92013-11-14 11:35:35 +020024#include <linux/clk.h>
Hebbar, Gururaja10884342012-08-08 20:40:32 +053025#include <linux/pm_runtime.h>
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +053026#include <linux/of.h>
27#include <linux/of_platform.h>
28#include <linux/of_device.h>
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020029#include <linux/platform_data/davinci_asp.h>
Jyri Sarhaa75a0532015-03-20 13:31:08 +020030#include <linux/math64.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040031
Daniel Mack64792852014-03-27 11:27:40 +010032#include <sound/asoundef.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040033#include <sound/core.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/initval.h>
37#include <sound/soc.h>
Peter Ujfalusi453c4992013-11-14 11:35:34 +020038#include <sound/dmaengine_pcm.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040039
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +030040#include "edma-pcm.h"
Peter Ujfalusi077a4032018-05-09 14:03:55 +030041#include "../omap/sdma-pcm.h"
Chaithrika U Sb67f4482009-06-05 06:28:40 -040042#include "davinci-mcasp.h"
43
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +030044#define MCASP_MAX_AFIFO_DEPTH 64
45
Arnd Bergmannb9c8f862019-03-07 11:11:30 +010046#ifdef CONFIG_PM
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030047static u32 context_regs[] = {
48 DAVINCI_MCASP_TXFMCTL_REG,
49 DAVINCI_MCASP_RXFMCTL_REG,
50 DAVINCI_MCASP_TXFMT_REG,
51 DAVINCI_MCASP_RXFMT_REG,
52 DAVINCI_MCASP_ACLKXCTL_REG,
53 DAVINCI_MCASP_ACLKRCTL_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030054 DAVINCI_MCASP_AHCLKXCTL_REG,
55 DAVINCI_MCASP_AHCLKRCTL_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030056 DAVINCI_MCASP_PDIR_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030057 DAVINCI_MCASP_RXMASK_REG,
58 DAVINCI_MCASP_TXMASK_REG,
59 DAVINCI_MCASP_RXTDM_REG,
60 DAVINCI_MCASP_TXTDM_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030061};
62
Peter Ujfalusi790bb942014-02-03 14:51:52 +020063struct davinci_mcasp_context {
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030064 u32 config_regs[ARRAY_SIZE(context_regs)];
Peter Ujfalusif114ce62014-10-01 16:02:12 +030065 u32 afifo_regs[2]; /* for read/write fifo control registers */
66 u32 *xrsr_regs; /* for serializer configuration */
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +020067 bool pm_state;
Peter Ujfalusi790bb942014-02-03 14:51:52 +020068};
Arnd Bergmannb9c8f862019-03-07 11:11:30 +010069#endif
Peter Ujfalusi790bb942014-02-03 14:51:52 +020070
Jyri Sarhaa75a0532015-03-20 13:31:08 +020071struct davinci_mcasp_ruledata {
72 struct davinci_mcasp *mcasp;
73 int serializers;
74};
75
Peter Ujfalusi70091a32013-11-14 11:35:29 +020076struct davinci_mcasp {
Peter Ujfalusi453c4992013-11-14 11:35:34 +020077 struct snd_dmaengine_dai_dma_data dma_data[2];
Peter Ujfalusi21400a722013-11-14 11:35:26 +020078 void __iomem *base;
Peter Ujfalusi487dce82013-11-14 11:35:31 +020079 u32 fifo_base;
Peter Ujfalusi21400a722013-11-14 11:35:26 +020080 struct device *dev;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020081 struct snd_pcm_substream *substreams[2];
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +020082 unsigned int dai_fmt;
Peter Ujfalusi21400a722013-11-14 11:35:26 +020083
84 /* McASP specific data */
85 int tdm_slots;
Jyri Sarhadd55ff82015-09-09 21:27:44 +030086 u32 tdm_mask[2];
87 int slot_width;
Peter Ujfalusi21400a722013-11-14 11:35:26 +020088 u8 op_mode;
89 u8 num_serializer;
90 u8 *serial_dir;
91 u8 version;
Daniel Mack82675252014-07-16 14:04:41 +020092 u8 bclk_div;
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +020093 int streams;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020094 u32 irq_request[2];
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020095 int dma_request[2];
Peter Ujfalusi21400a722013-11-14 11:35:26 +020096
Jyri Sarhaab8b14b2014-01-27 17:37:52 +020097 int sysclk_freq;
98 bool bclk_master;
99
Peter Ujfalusi21400a722013-11-14 11:35:26 +0200100 /* McASP FIFO related */
101 u8 txnumevt;
102 u8 rxnumevt;
103
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200104 bool dat_port;
105
Peter Ujfalusi11277832014-11-10 12:32:16 +0200106 /* Used for comstraint setting on the second stream */
107 u32 channels;
108
Peter Ujfalusi21400a722013-11-14 11:35:26 +0200109#ifdef CONFIG_PM_SLEEP
Peter Ujfalusi790bb942014-02-03 14:51:52 +0200110 struct davinci_mcasp_context context;
Peter Ujfalusi21400a722013-11-14 11:35:26 +0200111#endif
Jyri Sarhaa75a0532015-03-20 13:31:08 +0200112
113 struct davinci_mcasp_ruledata ruledata[2];
Jyri Sarha5935a052015-04-23 16:16:05 +0300114 struct snd_pcm_hw_constraint_list chconstr[2];
Peter Ujfalusi21400a722013-11-14 11:35:26 +0200115};
116
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200117static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
118 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400119{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200120 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400121 __raw_writel(__raw_readl(reg) | val, reg);
122}
123
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200124static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
125 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400126{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200127 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400128 __raw_writel((__raw_readl(reg) & ~(val)), reg);
129}
130
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200131static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
132 u32 val, u32 mask)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400133{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200134 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400135 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
136}
137
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200138static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
139 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400140{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200141 __raw_writel(val, mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400142}
143
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200144static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400145{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200146 return (u32)__raw_readl(mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400147}
148
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200149static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400150{
151 int i = 0;
152
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200153 mcasp_set_bits(mcasp, ctl_reg, val);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400154
155 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
156 /* loop count is to avoid the lock-up */
157 for (i = 0; i < 1000; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200158 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400159 break;
160 }
161
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200162 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400163 printk(KERN_ERR "GBLCTL write error\n");
164}
165
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200166static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
167{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200168 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
169 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200170
171 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
172}
173
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200174static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400175{
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200176 if (mcasp->rxnumevt) { /* enable FIFO */
177 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
178
179 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
180 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
181 }
182
Peter Ujfalusi44982732014-10-29 13:55:45 +0200183 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200184 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
185 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200186 /*
187 * When ASYNC == 0 the transmit and receive sections operate
188 * synchronously from the transmit clock and frame sync. We need to make
189 * sure that the TX signlas are enabled when starting reception.
190 */
191 if (mcasp_is_synchronous(mcasp)) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200192 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
193 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200194 }
195
Peter Ujfalusi44982732014-10-29 13:55:45 +0200196 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200197 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200198 /* Release RX state machine */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200199 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200200 /* Release Frame Sync generator */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200201 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200202 if (mcasp_is_synchronous(mcasp))
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200203 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200204
205 /* enable receive IRQs */
206 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
207 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400208}
209
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200210static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400211{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400212 u32 cnt;
213
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200214 if (mcasp->txnumevt) { /* enable FIFO */
215 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
216
217 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
218 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
219 }
220
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200221 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200222 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
223 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200224 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200225 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400226
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200227 /* wait for XDATA to be cleared */
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400228 cnt = 0;
Peter Ujfalusie2a0c9f2015-12-11 13:06:24 +0200229 while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) &&
230 (cnt < 100000))
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400231 cnt++;
232
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200233 /* Release TX state machine */
234 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
235 /* Release Frame Sync generator */
236 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200237
238 /* enable transmit IRQs */
239 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
240 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400241}
242
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200243static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400244{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200245 mcasp->streams++;
246
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200247 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200248 mcasp_start_tx(mcasp);
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200249 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200250 mcasp_start_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400251}
252
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200253static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400254{
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200255 /* disable IRQ sources */
256 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
257 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
258
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200259 /*
260 * In synchronous mode stop the TX clocks if no other stream is
261 * running
262 */
263 if (mcasp_is_synchronous(mcasp) && !mcasp->streams)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200264 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200265
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200266 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
267 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200268
269 if (mcasp->rxnumevt) { /* disable FIFO */
270 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
271
272 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
273 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400274}
275
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200276static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400277{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200278 u32 val = 0;
279
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200280 /* disable IRQ sources */
281 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
282 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
283
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200284 /*
285 * In synchronous mode keep TX clocks running if the capture stream is
286 * still running.
287 */
288 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
289 val = TXHCLKRST | TXCLKRST | TXFSRST;
290
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200291 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
292 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200293
294 if (mcasp->txnumevt) { /* disable FIFO */
295 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
296
297 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
298 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400299}
300
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200301static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400302{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200303 mcasp->streams--;
304
Peter Ujfalusi03808662014-10-29 13:55:46 +0200305 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200306 mcasp_stop_tx(mcasp);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200307 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200308 mcasp_stop_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400309}
310
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200311static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
312{
313 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
314 struct snd_pcm_substream *substream;
315 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
316 u32 handled_mask = 0;
317 u32 stat;
318
319 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
320 if (stat & XUNDRN & irq_mask) {
321 dev_warn(mcasp->dev, "Transmit buffer underflow\n");
322 handled_mask |= XUNDRN;
323
324 substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
Takashi Iwaidae35d12018-07-04 16:01:43 +0200325 if (substream)
326 snd_pcm_stop_xrun(substream);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200327 }
328
329 if (!handled_mask)
330 dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
331 stat);
332
333 if (stat & XRERR)
334 handled_mask |= XRERR;
335
336 /* Ack the handled event only */
337 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
338
339 return IRQ_RETVAL(handled_mask);
340}
341
342static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
343{
344 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
345 struct snd_pcm_substream *substream;
346 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
347 u32 handled_mask = 0;
348 u32 stat;
349
350 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
351 if (stat & ROVRN & irq_mask) {
352 dev_warn(mcasp->dev, "Receive buffer overflow\n");
353 handled_mask |= ROVRN;
354
355 substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
Takashi Iwaidae35d12018-07-04 16:01:43 +0200356 if (substream)
357 snd_pcm_stop_xrun(substream);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200358 }
359
360 if (!handled_mask)
361 dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
362 stat);
363
364 if (stat & XRERR)
365 handled_mask |= XRERR;
366
367 /* Ack the handled event only */
368 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
369
370 return IRQ_RETVAL(handled_mask);
371}
372
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +0200373static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
374{
375 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
376 irqreturn_t ret = IRQ_NONE;
377
378 if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
379 ret = davinci_mcasp_tx_irq_handler(irq, data);
380
381 if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
382 ret |= davinci_mcasp_rx_irq_handler(irq, data);
383
384 return ret;
385}
386
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400387static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
388 unsigned int fmt)
389{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200390 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200391 int ret = 0;
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300392 u32 data_delay;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300393 bool fs_pol_rising;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300394 bool inv_fs = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400395
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200396 if (!fmt)
397 return 0;
398
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200399 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5296cf22012-10-04 15:08:42 +0200400 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300401 case SND_SOC_DAIFMT_DSP_A:
402 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
403 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300404 /* 1st data bit occur one ACLK cycle after the frame sync */
405 data_delay = 1;
406 break;
Daniel Mack5296cf22012-10-04 15:08:42 +0200407 case SND_SOC_DAIFMT_DSP_B:
408 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200409 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
410 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300411 /* No delay after FS */
412 data_delay = 0;
Daniel Mack5296cf22012-10-04 15:08:42 +0200413 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300414 case SND_SOC_DAIFMT_I2S:
Daniel Mack5296cf22012-10-04 15:08:42 +0200415 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200416 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
417 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300418 /* 1st data bit occur one ACLK cycle after the frame sync */
419 data_delay = 1;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300420 /* FS need to be inverted */
421 inv_fs = true;
Daniel Mack5296cf22012-10-04 15:08:42 +0200422 break;
Peter Ujfalusi423761e2014-04-04 14:31:46 +0300423 case SND_SOC_DAIFMT_LEFT_J:
424 /* configure a full-word SYNC pulse (LRCLK) */
425 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
426 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
427 /* No delay after FS */
428 data_delay = 0;
429 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300430 default:
431 ret = -EINVAL;
432 goto out;
Daniel Mack5296cf22012-10-04 15:08:42 +0200433 }
434
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300435 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
436 FSXDLY(3));
437 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
438 FSRDLY(3));
439
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400440 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
441 case SND_SOC_DAIFMT_CBS_CFS:
442 /* codec is clock and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200443 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
444 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400445
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200446 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
447 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400448
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200449 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
450 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200451 mcasp->bclk_master = 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400452 break;
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200453 case SND_SOC_DAIFMT_CBS_CFM:
454 /* codec is clock slave and frame master */
455 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
456 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
457
458 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
459 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
460
461 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
462 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
463 mcasp->bclk_master = 1;
464 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400465 case SND_SOC_DAIFMT_CBM_CFS:
466 /* codec is clock master and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200467 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
468 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400469
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200470 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
471 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400472
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200473 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
474 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200475 mcasp->bclk_master = 0;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400476 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400477 case SND_SOC_DAIFMT_CBM_CFM:
478 /* codec is clock and frame master */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200479 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
480 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400481
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200482 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
483 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400484
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200485 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG,
Jim Lodes823ecdd2016-04-25 11:08:10 -0500486 ACLKX | AFSX | ACLKR | AHCLKR | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200487 mcasp->bclk_master = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400488 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400489 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200490 ret = -EINVAL;
491 goto out;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400492 }
493
494 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
495 case SND_SOC_DAIFMT_IB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200496 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300497 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300498 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400499 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400500 case SND_SOC_DAIFMT_NB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200501 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300502 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300503 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400504 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400505 case SND_SOC_DAIFMT_IB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200506 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300507 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300508 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400509 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400510 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200511 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200512 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300513 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400514 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400515 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200516 ret = -EINVAL;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300517 goto out;
518 }
519
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300520 if (inv_fs)
521 fs_pol_rising = !fs_pol_rising;
522
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300523 if (fs_pol_rising) {
524 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
525 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
526 } else {
527 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
528 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400529 }
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200530
531 mcasp->dai_fmt = fmt;
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200532out:
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200533 pm_runtime_put(mcasp->dev);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200534 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400535}
536
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300537static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp *mcasp, int div_id,
Jyri Sarha88135432014-08-06 16:47:16 +0300538 int div, bool explicit)
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200539{
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200540 pm_runtime_get_sync(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200541 switch (div_id) {
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300542 case MCASP_CLKDIV_AUXCLK: /* MCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200543 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200544 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200545 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200546 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
547 break;
548
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300549 case MCASP_CLKDIV_BCLK: /* BCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200550 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200551 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200552 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200553 ACLKRDIV(div - 1), ACLKRDIV_MASK);
Jyri Sarha88135432014-08-06 16:47:16 +0300554 if (explicit)
555 mcasp->bclk_div = div;
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200556 break;
557
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300558 case MCASP_CLKDIV_BCLK_FS_RATIO:
559 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300560 * BCLK/LRCLK ratio descries how many bit-clock cycles
561 * fit into one frame. The clock ratio is given for a
562 * full period of data (for I2S format both left and
563 * right channels), so it has to be divided by number
564 * of tdm-slots (for I2S - divided by 2).
565 * Instead of storing this ratio, we calculate a new
566 * tdm_slot width by dividing the the ratio by the
567 * number of configured tdm slots.
568 */
569 mcasp->slot_width = div / mcasp->tdm_slots;
570 if (div % mcasp->tdm_slots)
571 dev_warn(mcasp->dev,
572 "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
573 __func__, div, mcasp->tdm_slots);
Daniel Mack1b3bc062012-12-05 18:20:38 +0100574 break;
575
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200576 default:
577 return -EINVAL;
578 }
579
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200580 pm_runtime_put(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200581 return 0;
582}
583
Jyri Sarha88135432014-08-06 16:47:16 +0300584static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
585 int div)
586{
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300587 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
588
589 return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1);
Jyri Sarha88135432014-08-06 16:47:16 +0300590}
591
Daniel Mack5b66aa22012-10-04 15:08:41 +0200592static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
593 unsigned int freq, int dir)
594{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200595 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200596
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200597 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200598 if (dir == SND_SOC_CLOCK_OUT) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200599 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
600 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
601 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200602 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200603 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
604 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
605 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200606 }
607
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200608 mcasp->sysclk_freq = freq;
609
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200610 pm_runtime_put(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200611 return 0;
612}
613
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300614/* All serializers must have equal number of channels */
615static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream,
616 int serializers)
617{
618 struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream];
619 unsigned int *list = (unsigned int *) cl->list;
620 int slots = mcasp->tdm_slots;
621 int i, count = 0;
622
623 if (mcasp->tdm_mask[stream])
624 slots = hweight32(mcasp->tdm_mask[stream]);
625
Peter Ujfalusie4798d22017-05-11 09:58:22 +0300626 for (i = 1; i <= slots; i++)
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300627 list[count++] = i;
628
629 for (i = 2; i <= serializers; i++)
630 list[count++] = i*slots;
631
632 cl->count = count;
633
634 return 0;
635}
636
637static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp)
638{
639 int rx_serializers = 0, tx_serializers = 0, ret, i;
640
641 for (i = 0; i < mcasp->num_serializer; i++)
642 if (mcasp->serial_dir[i] == TX_MODE)
643 tx_serializers++;
644 else if (mcasp->serial_dir[i] == RX_MODE)
645 rx_serializers++;
646
647 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK,
648 tx_serializers);
649 if (ret)
650 return ret;
651
652 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE,
653 rx_serializers);
654
655 return ret;
656}
657
658
659static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai,
660 unsigned int tx_mask,
661 unsigned int rx_mask,
662 int slots, int slot_width)
663{
664 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
665
666 dev_dbg(mcasp->dev,
667 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
668 __func__, tx_mask, rx_mask, slots, slot_width);
669
670 if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
671 dev_err(mcasp->dev,
672 "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
673 tx_mask, rx_mask, slots);
674 return -EINVAL;
675 }
676
677 if (slot_width &&
678 (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) {
679 dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n",
680 __func__, slot_width);
681 return -EINVAL;
682 }
683
684 mcasp->tdm_slots = slots;
Andreas Dannenberg1bdd5932015-11-09 12:19:19 -0600685 mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
686 mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300687 mcasp->slot_width = slot_width;
688
689 return davinci_mcasp_set_ch_constraints(mcasp);
690}
691
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200692static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Jyri Sarha14a998b2015-09-17 10:39:05 +0300693 int sample_width)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400694{
Daniel Mackba764b32012-12-05 18:20:37 +0100695 u32 fmt;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300696 u32 tx_rotate = (sample_width / 4) & 0x7;
697 u32 mask = (1ULL << sample_width) - 1;
698 u32 slot_width = sample_width;
699
Peter Ujfalusife0a29e2014-09-04 10:52:53 +0300700 /*
701 * For captured data we should not rotate, inversion and masking is
702 * enoguh to get the data to the right position:
703 * Format data from bus after reverse (XRBUF)
704 * S16_LE: |LSB|MSB|xxx|xxx| |xxx|xxx|MSB|LSB|
705 * S24_3LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
706 * S24_LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
707 * S32_LE: |LSB|DAT|DAT|MSB| |MSB|DAT|DAT|LSB|
708 */
709 u32 rx_rotate = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400710
Daniel Mack1b3bc062012-12-05 18:20:38 +0100711 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300712 * Setting the tdm slot width either with set_clkdiv() or
713 * set_tdm_slot() allows us to for example send 32 bits per
714 * channel to the codec, while only 16 of them carry audio
715 * payload.
Daniel Mack1b3bc062012-12-05 18:20:38 +0100716 */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300717 if (mcasp->slot_width) {
Peter Ujfalusid742b922014-11-10 12:32:19 +0200718 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300719 * When we have more bclk then it is needed for the
720 * data, we need to use the rotation to move the
721 * received samples to have correct alignment.
Peter Ujfalusid742b922014-11-10 12:32:19 +0200722 */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300723 slot_width = mcasp->slot_width;
724 rx_rotate = (slot_width - sample_width) / 4;
Peter Ujfalusid742b922014-11-10 12:32:19 +0200725 }
Daniel Mack1b3bc062012-12-05 18:20:38 +0100726
Daniel Mackba764b32012-12-05 18:20:37 +0100727 /* mapping of the XSSZ bit-field as described in the datasheet */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300728 fmt = (slot_width >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400729
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200730 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200731 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
732 RXSSZ(0x0F));
733 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
734 TXSSZ(0x0F));
735 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
736 TXROT(7));
737 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
738 RXROT(7));
739 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200740 }
741
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200742 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400743
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400744 return 0;
745}
746
Peter Ujfalusi662ffae2014-01-30 15:15:22 +0200747static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300748 int period_words, int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400749{
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300750 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400751 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400752 u8 tx_ser = 0;
753 u8 rx_ser = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200754 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100755 u8 max_active_serializers = (channels + slots - 1) / slots;
Peter Ujfalusi72383192015-09-14 16:06:48 +0300756 int active_serializers, numevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +0200757 u32 reg;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400758 /* Default configuration */
Peter Ujfalusi40448e52014-04-04 15:56:30 +0300759 if (mcasp->version < MCASP_VERSION_3)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200760 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400761
762 /* All PINS as McASP */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200763 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400764
765 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200766 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
767 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400768 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200769 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
770 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400771 }
772
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200773 for (i = 0; i < mcasp->num_serializer; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200774 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
775 mcasp->serial_dir[i]);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200776 if (mcasp->serial_dir[i] == TX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100777 tx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200778 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Misael Lopez Cruz19db62e2015-06-08 16:03:47 +0300779 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
780 DISMOD_LOW, DISMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400781 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200782 } else if (mcasp->serial_dir[i] == RX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100783 rx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200784 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400785 rx_ser++;
Vishal Thanki096a8f82018-05-11 14:33:37 +0200786 } else if (mcasp->serial_dir[i] == INACTIVE_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200787 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
788 SRMOD_INACTIVE, SRMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400789 }
790 }
791
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300792 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
793 active_serializers = tx_ser;
794 numevt = mcasp->txnumevt;
795 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
796 } else {
797 active_serializers = rx_ser;
798 numevt = mcasp->rxnumevt;
799 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
800 }
Daniel Mackecf327c2013-03-08 14:19:38 +0100801
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300802 if (active_serializers < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200803 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300804 "enabled in mcasp (%d)\n", channels,
805 active_serializers * slots);
Daniel Mackecf327c2013-03-08 14:19:38 +0100806 return -EINVAL;
807 }
808
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300809 /* AFIFO is not in use */
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300810 if (!numevt) {
811 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300812 if (active_serializers > 1) {
813 /*
814 * If more than one serializers are in use we have one
815 * DMA request to provide data for all serializers.
816 * For example if three serializers are enabled the DMA
817 * need to transfer three words per DMA request.
818 */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300819 dma_data->maxburst = active_serializers;
820 } else {
Peter Ujfalusi33445642014-04-01 15:55:12 +0300821 dma_data->maxburst = 0;
822 }
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300823 return 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300824 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400825
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300826 if (period_words % active_serializers) {
827 dev_err(mcasp->dev, "Invalid combination of period words and "
828 "active serializers: %d, %d\n", period_words,
829 active_serializers);
830 return -EINVAL;
831 }
832
833 /*
834 * Calculate the optimal AFIFO depth for platform side:
835 * The number of words for numevt need to be in steps of active
836 * serializers.
837 */
Peter Ujfalusi72383192015-09-14 16:06:48 +0300838 numevt = (numevt / active_serializers) * active_serializers;
839
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300840 while (period_words % numevt && numevt > 0)
841 numevt -= active_serializers;
842 if (numevt <= 0)
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300843 numevt = active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400844
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300845 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
846 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
Michal Bachraty2952b272013-02-28 16:07:08 +0100847
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300848 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300849 if (numevt == 1)
850 numevt = 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300851 dma_data->maxburst = numevt;
852
Michal Bachraty2952b272013-02-28 16:07:08 +0100853 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400854}
855
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200856static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
857 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400858{
859 int i, active_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200860 int total_slots;
861 int active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400862 u32 mask = 0;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200863 u32 busel = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400864
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200865 total_slots = mcasp->tdm_slots;
866
867 /*
868 * If more than one serializer is needed, then use them with
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300869 * all the specified tdm_slots. Otherwise, one serializer can
870 * cope with the transaction using just as many slots as there
871 * are channels in the stream.
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200872 */
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300873 if (mcasp->tdm_mask[stream]) {
874 active_slots = hweight32(mcasp->tdm_mask[stream]);
875 active_serializers = (channels + active_slots - 1) /
876 active_slots;
Peter Ujfalusi63f47d22019-06-20 12:20:02 +0300877 if (active_serializers == 1)
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300878 active_slots = channels;
Peter Ujfalusi63f47d22019-06-20 12:20:02 +0300879 for (i = 0; i < total_slots; i++) {
880 if ((1 << i) & mcasp->tdm_mask[stream]) {
881 mask |= (1 << i);
882 if (--active_slots <= 0)
883 break;
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300884 }
885 }
886 } else {
887 active_serializers = (channels + total_slots - 1) / total_slots;
888 if (active_serializers == 1)
889 active_slots = channels;
890 else
891 active_slots = total_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200892
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300893 for (i = 0; i < active_slots; i++)
894 mask |= (1 << i);
895 }
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200896 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400897
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200898 if (!mcasp->dat_port)
899 busel = TXSEL;
900
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300901 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
902 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
903 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
904 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
905 FSXMOD(total_slots), FSXMOD(0x1FF));
906 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
907 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
908 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
909 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
910 FSRMOD(total_slots), FSRMOD(0x1FF));
Peter Ujfalusi0ad7d3a2015-11-23 12:51:53 +0200911 /*
912 * If McASP is set to be TX/RX synchronous and the playback is
913 * not running already we need to configure the TX slots in
914 * order to have correct FSX on the bus
915 */
916 if (mcasp_is_synchronous(mcasp) && !mcasp->channels)
917 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
918 FSXMOD(total_slots), FSXMOD(0x1FF));
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300919 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400920
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200921 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400922}
923
924/* S/PDIF */
Daniel Mack64792852014-03-27 11:27:40 +0100925static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
926 unsigned int rate)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400927{
Daniel Mack64792852014-03-27 11:27:40 +0100928 u32 cs_value = 0;
929 u8 *cs_bytes = (u8*) &cs_value;
930
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400931 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
932 and LSB first */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200933 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400934
935 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200936 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400937
938 /* Set the TX tdm : for all the slots */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200939 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400940
941 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200942 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400943
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200944 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400945
946 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200947 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400948
949 /* Enable the DIT */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200950 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200951
Daniel Mack64792852014-03-27 11:27:40 +0100952 /* Set S/PDIF channel status bits */
953 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
954 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
955
956 switch (rate) {
957 case 22050:
958 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
959 break;
960 case 24000:
961 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
962 break;
963 case 32000:
964 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
965 break;
966 case 44100:
967 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
968 break;
969 case 48000:
970 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
971 break;
972 case 88200:
973 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
974 break;
975 case 96000:
976 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
977 break;
978 case 176400:
979 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
980 break;
981 case 192000:
982 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
983 break;
984 default:
985 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
986 return -EINVAL;
987 }
988
989 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
990 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
991
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200992 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400993}
994
Jyri Sarhaa75a0532015-03-20 13:31:08 +0200995static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +0300996 unsigned int bclk_freq, bool set)
Jyri Sarhaa75a0532015-03-20 13:31:08 +0200997{
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +0300998 int error_ppm;
Peter Ujfalusiddecd142016-05-09 13:42:32 +0300999 unsigned int sysclk_freq = mcasp->sysclk_freq;
1000 u32 reg = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG);
1001 int div = sysclk_freq / bclk_freq;
1002 int rem = sysclk_freq % bclk_freq;
1003 int aux_div = 1;
1004
1005 if (div > (ACLKXDIV_MASK + 1)) {
1006 if (reg & AHCLKXE) {
1007 aux_div = div / (ACLKXDIV_MASK + 1);
1008 if (div % (ACLKXDIV_MASK + 1))
1009 aux_div++;
1010
1011 sysclk_freq /= aux_div;
1012 div = sysclk_freq / bclk_freq;
1013 rem = sysclk_freq % bclk_freq;
1014 } else if (set) {
1015 dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
1016 sysclk_freq);
1017 }
1018 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001019
1020 if (rem != 0) {
1021 if (div == 0 ||
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001022 ((sysclk_freq / div) - bclk_freq) >
1023 (bclk_freq - (sysclk_freq / (div+1)))) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001024 div++;
1025 rem = rem - bclk_freq;
1026 }
1027 }
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001028 error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem,
1029 (int)bclk_freq)) / div - 1000000;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001030
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001031 if (set) {
1032 if (error_ppm)
1033 dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n",
1034 error_ppm);
1035
1036 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_BCLK, div, 0);
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001037 if (reg & AHCLKXE)
1038 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK,
1039 aux_div, 0);
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001040 }
1041
1042 return error_ppm;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001043}
1044
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001045static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
1046 struct snd_pcm_hw_params *params,
1047 struct snd_soc_dai *cpu_dai)
1048{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001049 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001050 int word_length;
Peter Ujfalusia7e46bd2014-02-03 14:51:50 +02001051 int channels = params_channels(params);
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001052 int period_size = params_period_size(params);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001053 int ret;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001054
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +02001055 ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt);
1056 if (ret)
1057 return ret;
1058
Daniel Mack82675252014-07-16 14:04:41 +02001059 /*
1060 * If mcasp is BCLK master, and a BCLK divider was not provided by
1061 * the machine driver, we need to calculate the ratio.
1062 */
1063 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarha1f114f72015-04-23 16:16:04 +03001064 int slots = mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001065 int rate = params_rate(params);
1066 int sbits = params_width(params);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001067
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001068 if (mcasp->slot_width)
1069 sbits = mcasp->slot_width;
1070
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001071 davinci_mcasp_calc_clk_div(mcasp, rate * sbits * slots, true);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001072 }
1073
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001074 ret = mcasp_common_hw_param(mcasp, substream->stream,
1075 period_size * channels, channels);
Peter Ujfalusi0f7d9a62014-01-30 15:15:24 +02001076 if (ret)
1077 return ret;
1078
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001079 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
Daniel Mack64792852014-03-27 11:27:40 +01001080 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001081 else
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +02001082 ret = mcasp_i2s_hw_param(mcasp, substream->stream,
1083 channels);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001084
1085 if (ret)
1086 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001087
1088 switch (params_format(params)) {
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001089 case SNDRV_PCM_FORMAT_U8:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001090 case SNDRV_PCM_FORMAT_S8:
Daniel Mackba764b32012-12-05 18:20:37 +01001091 word_length = 8;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001092 break;
1093
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001094 case SNDRV_PCM_FORMAT_U16_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001095 case SNDRV_PCM_FORMAT_S16_LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001096 word_length = 16;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001097 break;
1098
Daniel Mack21eb24d2012-10-09 09:35:16 +02001099 case SNDRV_PCM_FORMAT_U24_3LE:
1100 case SNDRV_PCM_FORMAT_S24_3LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001101 word_length = 24;
Daniel Mack21eb24d2012-10-09 09:35:16 +02001102 break;
1103
Daniel Mack6b7fa012012-10-09 11:56:40 +02001104 case SNDRV_PCM_FORMAT_U24_LE:
1105 case SNDRV_PCM_FORMAT_S24_LE:
Peter Ujfalusi182bef82014-06-26 08:09:24 +03001106 word_length = 24;
1107 break;
1108
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001109 case SNDRV_PCM_FORMAT_U32_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001110 case SNDRV_PCM_FORMAT_S32_LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001111 word_length = 32;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001112 break;
1113
1114 default:
1115 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
1116 return -EINVAL;
1117 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -04001118
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001119 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001120
Peter Ujfalusi11277832014-11-10 12:32:16 +02001121 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE)
1122 mcasp->channels = channels;
1123
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001124 return 0;
1125}
1126
1127static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
1128 int cmd, struct snd_soc_dai *cpu_dai)
1129{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001130 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001131 int ret = 0;
1132
1133 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001134 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +05301135 case SNDRV_PCM_TRIGGER_START:
1136 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001137 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001138 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001139 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +05301140 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001141 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001142 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001143 break;
1144
1145 default:
1146 ret = -EINVAL;
1147 }
1148
1149 return ret;
1150}
1151
Peter Ujfalusibfa713f2019-07-26 09:42:43 +03001152static int davinci_mcasp_hw_rule_slot_width(struct snd_pcm_hw_params *params,
1153 struct snd_pcm_hw_rule *rule)
1154{
1155 struct davinci_mcasp_ruledata *rd = rule->private;
1156 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1157 struct snd_mask nfmt;
1158 int i, slot_width;
1159
1160 snd_mask_none(&nfmt);
1161 slot_width = rd->mcasp->slot_width;
1162
1163 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1164 if (snd_mask_test(fmt, i)) {
1165 if (snd_pcm_format_width(i) <= slot_width) {
1166 snd_mask_set(&nfmt, i);
1167 }
1168 }
1169 }
1170
1171 return snd_mask_refine(fmt, &nfmt);
1172}
1173
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001174static const unsigned int davinci_mcasp_dai_rates[] = {
1175 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1176 88200, 96000, 176400, 192000,
1177};
1178
1179#define DAVINCI_MAX_RATE_ERROR_PPM 1000
1180
1181static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params,
1182 struct snd_pcm_hw_rule *rule)
1183{
1184 struct davinci_mcasp_ruledata *rd = rule->private;
1185 struct snd_interval *ri =
1186 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1187 int sbits = params_width(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001188 int slots = rd->mcasp->tdm_slots;
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001189 struct snd_interval range;
1190 int i;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001191
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001192 if (rd->mcasp->slot_width)
1193 sbits = rd->mcasp->slot_width;
1194
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001195 snd_interval_any(&range);
1196 range.empty = 1;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001197
1198 for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) {
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001199 if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) {
Jyri Sarha1f114f72015-04-23 16:16:04 +03001200 uint bclk_freq = sbits*slots*
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001201 davinci_mcasp_dai_rates[i];
1202 int ppm;
1203
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001204 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, bclk_freq,
1205 false);
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001206 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1207 if (range.empty) {
1208 range.min = davinci_mcasp_dai_rates[i];
1209 range.empty = 0;
1210 }
1211 range.max = davinci_mcasp_dai_rates[i];
1212 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001213 }
1214 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001215
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001216 dev_dbg(rd->mcasp->dev,
1217 "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1218 ri->min, ri->max, range.min, range.max, sbits, slots);
1219
1220 return snd_interval_refine(hw_param_interval(params, rule->var),
1221 &range);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001222}
1223
1224static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params,
1225 struct snd_pcm_hw_rule *rule)
1226{
1227 struct davinci_mcasp_ruledata *rd = rule->private;
1228 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1229 struct snd_mask nfmt;
1230 int rate = params_rate(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001231 int slots = rd->mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001232 int i, count = 0;
1233
1234 snd_mask_none(&nfmt);
1235
Peter Ujfalusi9be072a2016-09-01 10:05:12 +03001236 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001237 if (snd_mask_test(fmt, i)) {
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001238 uint sbits = snd_pcm_format_width(i);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001239 int ppm;
1240
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001241 if (rd->mcasp->slot_width)
1242 sbits = rd->mcasp->slot_width;
1243
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001244 ppm = davinci_mcasp_calc_clk_div(rd->mcasp,
1245 sbits * slots * rate,
1246 false);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001247 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1248 snd_mask_set(&nfmt, i);
1249 count++;
1250 }
1251 }
1252 }
1253 dev_dbg(rd->mcasp->dev,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001254 "%d possible sample format for %d Hz and %d tdm slots\n",
1255 count, rate, slots);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001256
1257 return snd_mask_refine(fmt, &nfmt);
1258}
1259
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001260static int davinci_mcasp_hw_rule_min_periodsize(
1261 struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1262{
1263 struct snd_interval *period_size = hw_param_interval(params,
1264 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1265 struct snd_interval frames;
1266
1267 snd_interval_any(&frames);
1268 frames.min = 64;
1269 frames.integer = 1;
1270
1271 return snd_interval_refine(period_size, &frames);
1272}
1273
Peter Ujfalusi11277832014-11-10 12:32:16 +02001274static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
1275 struct snd_soc_dai *cpu_dai)
1276{
1277 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001278 struct davinci_mcasp_ruledata *ruledata =
1279 &mcasp->ruledata[substream->stream];
Peter Ujfalusi11277832014-11-10 12:32:16 +02001280 u32 max_channels = 0;
Peter Ujfalusibfa713f2019-07-26 09:42:43 +03001281 int i, dir, ret;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001282 int tdm_slots = mcasp->tdm_slots;
1283
Peter Ujfalusi19357362016-05-09 13:39:14 +03001284 /* Do not allow more then one stream per direction */
1285 if (mcasp->substreams[substream->stream])
1286 return -EBUSY;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001287
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001288 mcasp->substreams[substream->stream] = substream;
1289
Peter Ujfalusi19357362016-05-09 13:39:14 +03001290 if (mcasp->tdm_mask[substream->stream])
1291 tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]);
1292
Peter Ujfalusi11277832014-11-10 12:32:16 +02001293 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1294 return 0;
1295
1296 /*
1297 * Limit the maximum allowed channels for the first stream:
1298 * number of serializers for the direction * tdm slots per serializer
1299 */
1300 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1301 dir = TX_MODE;
1302 else
1303 dir = RX_MODE;
1304
1305 for (i = 0; i < mcasp->num_serializer; i++) {
1306 if (mcasp->serial_dir[i] == dir)
1307 max_channels++;
1308 }
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001309 ruledata->serializers = max_channels;
Peter Ujfalusibfa713f2019-07-26 09:42:43 +03001310 ruledata->mcasp = mcasp;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001311 max_channels *= tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001312 /*
1313 * If the already active stream has less channels than the calculated
1314 * limnit based on the seirializers * tdm_slots, we need to use that as
1315 * a constraint for the second stream.
1316 * Otherwise (first stream or less allowed channels) we use the
1317 * calculated constraint.
1318 */
1319 if (mcasp->channels && mcasp->channels < max_channels)
1320 max_channels = mcasp->channels;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001321 /*
1322 * But we can always allow channels upto the amount of
1323 * the available tdm_slots.
1324 */
1325 if (max_channels < tdm_slots)
1326 max_channels = tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001327
1328 snd_pcm_hw_constraint_minmax(substream->runtime,
1329 SNDRV_PCM_HW_PARAM_CHANNELS,
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001330 0, max_channels);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001331
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001332 snd_pcm_hw_constraint_list(substream->runtime,
1333 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1334 &mcasp->chconstr[substream->stream]);
1335
Peter Ujfalusibfa713f2019-07-26 09:42:43 +03001336 if (mcasp->slot_width) {
1337 /* Only allow formats require <= slot_width bits on the bus */
1338 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1339 SNDRV_PCM_HW_PARAM_FORMAT,
1340 davinci_mcasp_hw_rule_slot_width,
1341 ruledata,
1342 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1343 if (ret)
1344 return ret;
1345 }
Jyri Sarha5935a052015-04-23 16:16:05 +03001346
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001347 /*
1348 * If we rely on implicit BCLK divider setting we should
1349 * set constraints based on what we can provide.
1350 */
1351 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001352 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1353 SNDRV_PCM_HW_PARAM_RATE,
1354 davinci_mcasp_hw_rule_rate,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001355 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001356 SNDRV_PCM_HW_PARAM_FORMAT, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001357 if (ret)
1358 return ret;
1359 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1360 SNDRV_PCM_HW_PARAM_FORMAT,
1361 davinci_mcasp_hw_rule_format,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001362 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001363 SNDRV_PCM_HW_PARAM_RATE, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001364 if (ret)
1365 return ret;
1366 }
1367
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001368 snd_pcm_hw_rule_add(substream->runtime, 0,
1369 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1370 davinci_mcasp_hw_rule_min_periodsize, NULL,
1371 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1372
Peter Ujfalusi11277832014-11-10 12:32:16 +02001373 return 0;
1374}
1375
1376static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1377 struct snd_soc_dai *cpu_dai)
1378{
1379 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1380
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001381 mcasp->substreams[substream->stream] = NULL;
1382
Peter Ujfalusi11277832014-11-10 12:32:16 +02001383 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1384 return;
1385
1386 if (!cpu_dai->active)
1387 mcasp->channels = 0;
1388}
1389
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001390static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001391 .startup = davinci_mcasp_startup,
1392 .shutdown = davinci_mcasp_shutdown,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001393 .trigger = davinci_mcasp_trigger,
1394 .hw_params = davinci_mcasp_hw_params,
1395 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +02001396 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +02001397 .set_sysclk = davinci_mcasp_set_sysclk,
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001398 .set_tdm_slot = davinci_mcasp_set_tdm_slot,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001399};
1400
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001401static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1402{
1403 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1404
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001405 dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1406 dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001407
1408 return 0;
1409}
1410
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001411#ifdef CONFIG_PM_SLEEP
1412static int davinci_mcasp_suspend(struct snd_soc_dai *dai)
1413{
1414 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +02001415 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001416 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001417 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001418
Peter Ujfalusi27796e72015-04-30 11:57:41 +03001419 context->pm_state = pm_runtime_active(mcasp->dev);
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001420 if (!context->pm_state)
1421 pm_runtime_get_sync(mcasp->dev);
1422
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001423 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
1424 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001425
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001426 if (mcasp->txnumevt) {
1427 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
1428 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
1429 }
1430 if (mcasp->rxnumevt) {
1431 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
1432 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
1433 }
1434
1435 for (i = 0; i < mcasp->num_serializer; i++)
1436 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
1437 DAVINCI_MCASP_XRSRCTL_REG(i));
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001438
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001439 pm_runtime_put_sync(mcasp->dev);
1440
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001441 return 0;
1442}
1443
1444static int davinci_mcasp_resume(struct snd_soc_dai *dai)
1445{
1446 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +02001447 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001448 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001449 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001450
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001451 pm_runtime_get_sync(mcasp->dev);
1452
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001453 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
1454 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001455
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001456 if (mcasp->txnumevt) {
1457 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
1458 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
1459 }
1460 if (mcasp->rxnumevt) {
1461 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
1462 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
1463 }
1464
1465 for (i = 0; i < mcasp->num_serializer; i++)
1466 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
1467 context->xrsr_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001468
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001469 if (!context->pm_state)
1470 pm_runtime_put_sync(mcasp->dev);
1471
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001472 return 0;
1473}
1474#else
1475#define davinci_mcasp_suspend NULL
1476#define davinci_mcasp_resume NULL
1477#endif
1478
Peter Ujfalusied29cd52013-11-14 11:35:22 +02001479#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1480
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001481#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1482 SNDRV_PCM_FMTBIT_U8 | \
1483 SNDRV_PCM_FMTBIT_S16_LE | \
1484 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +02001485 SNDRV_PCM_FMTBIT_S24_LE | \
1486 SNDRV_PCM_FMTBIT_U24_LE | \
1487 SNDRV_PCM_FMTBIT_S24_3LE | \
1488 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001489 SNDRV_PCM_FMTBIT_S32_LE | \
1490 SNDRV_PCM_FMTBIT_U32_LE)
1491
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001492static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001493 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001494 .name = "davinci-mcasp.0",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001495 .probe = davinci_mcasp_dai_probe,
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001496 .suspend = davinci_mcasp_suspend,
1497 .resume = davinci_mcasp_resume,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001498 .playback = {
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001499 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001500 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001501 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001502 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001503 },
1504 .capture = {
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001505 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001506 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001507 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001508 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001509 },
1510 .ops = &davinci_mcasp_dai_ops,
1511
Peter Ujfalusid75249f2014-11-10 12:32:18 +02001512 .symmetric_samplebits = 1,
Jyri Sarha295c3402015-09-09 21:27:42 +03001513 .symmetric_rates = 1,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001514 },
1515 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +02001516 .name = "davinci-mcasp.1",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001517 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001518 .playback = {
1519 .channels_min = 1,
1520 .channels_max = 384,
1521 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001522 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001523 },
1524 .ops = &davinci_mcasp_dai_ops,
1525 },
1526
1527};
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001528
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001529static const struct snd_soc_component_driver davinci_mcasp_component = {
1530 .name = "davinci-mcasp",
1531};
1532
Jyri Sarha256ba182013-10-18 18:37:42 +03001533/* Some HW specific values and defaults. The rest is filled in from DT. */
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001534static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001535 .tx_dma_offset = 0x400,
1536 .rx_dma_offset = 0x400,
Jyri Sarha256ba182013-10-18 18:37:42 +03001537 .version = MCASP_VERSION_1,
1538};
1539
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001540static struct davinci_mcasp_pdata da830_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001541 .tx_dma_offset = 0x2000,
1542 .rx_dma_offset = 0x2000,
Jyri Sarha256ba182013-10-18 18:37:42 +03001543 .version = MCASP_VERSION_2,
1544};
1545
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001546static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001547 .tx_dma_offset = 0,
1548 .rx_dma_offset = 0,
Jyri Sarha256ba182013-10-18 18:37:42 +03001549 .version = MCASP_VERSION_3,
1550};
1551
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001552static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001553 /* The CFG port offset will be calculated if it is needed */
1554 .tx_dma_offset = 0,
1555 .rx_dma_offset = 0,
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001556 .version = MCASP_VERSION_4,
1557};
1558
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301559static const struct of_device_id mcasp_dt_ids[] = {
1560 {
1561 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001562 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301563 },
1564 {
1565 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001566 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301567 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301568 {
Jyri Sarha3af9e032013-10-18 18:37:44 +03001569 .compatible = "ti,am33xx-mcasp-audio",
Peter Ujfalusib14899d2013-11-14 11:35:37 +02001570 .data = &am33xx_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301571 },
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001572 {
1573 .compatible = "ti,dra7-mcasp-audio",
1574 .data = &dra7_mcasp_pdata,
1575 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301576 { /* sentinel */ }
1577};
1578MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1579
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001580static int mcasp_reparent_fck(struct platform_device *pdev)
1581{
1582 struct device_node *node = pdev->dev.of_node;
1583 struct clk *gfclk, *parent_clk;
1584 const char *parent_name;
1585 int ret;
1586
1587 if (!node)
1588 return 0;
1589
1590 parent_name = of_get_property(node, "fck_parent", NULL);
1591 if (!parent_name)
1592 return 0;
1593
Peter Ujfalusic6702542016-01-27 15:02:49 +02001594 dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n");
1595
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001596 gfclk = clk_get(&pdev->dev, "fck");
1597 if (IS_ERR(gfclk)) {
1598 dev_err(&pdev->dev, "failed to get fck\n");
1599 return PTR_ERR(gfclk);
1600 }
1601
1602 parent_clk = clk_get(NULL, parent_name);
1603 if (IS_ERR(parent_clk)) {
1604 dev_err(&pdev->dev, "failed to get parent clock\n");
1605 ret = PTR_ERR(parent_clk);
1606 goto err1;
1607 }
1608
1609 ret = clk_set_parent(gfclk, parent_clk);
1610 if (ret) {
1611 dev_err(&pdev->dev, "failed to reparent fck\n");
1612 goto err2;
1613 }
1614
1615err2:
1616 clk_put(parent_clk);
1617err1:
1618 clk_put(gfclk);
1619 return ret;
1620}
1621
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001622static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301623 struct platform_device *pdev)
1624{
1625 struct device_node *np = pdev->dev.of_node;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001626 struct davinci_mcasp_pdata *pdata = NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301627 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +05301628 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001629 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301630
1631 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301632 u32 val;
1633 int i, ret = 0;
1634
1635 if (pdev->dev.platform_data) {
1636 pdata = pdev->dev.platform_data;
1637 return pdata;
1638 } else if (match) {
Peter Ujfalusi272ee032016-06-02 12:55:24 +03001639 pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata),
1640 GFP_KERNEL);
1641 if (!pdata) {
Peter Ujfalusi272ee032016-06-02 12:55:24 +03001642 ret = -ENOMEM;
1643 return pdata;
1644 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301645 } else {
1646 /* control shouldn't reach here. something is wrong */
1647 ret = -EINVAL;
1648 goto nodata;
1649 }
1650
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301651 ret = of_property_read_u32(np, "op-mode", &val);
1652 if (ret >= 0)
1653 pdata->op_mode = val;
1654
1655 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +01001656 if (ret >= 0) {
1657 if (val < 2 || val > 32) {
1658 dev_err(&pdev->dev,
1659 "tdm-slots must be in rage [2-32]\n");
1660 ret = -EINVAL;
1661 goto nodata;
1662 }
1663
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301664 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +01001665 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301666
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301667 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1668 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301669 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001670 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1671 (sizeof(*of_serial_dir) * val),
1672 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301673 if (!of_serial_dir) {
1674 ret = -ENOMEM;
1675 goto nodata;
1676 }
1677
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001678 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301679 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1680
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001681 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301682 pdata->serial_dir = of_serial_dir;
1683 }
1684
Jyri Sarha4023fe62013-10-18 18:37:43 +03001685 ret = of_property_match_string(np, "dma-names", "tx");
1686 if (ret < 0)
1687 goto nodata;
1688
1689 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1690 &dma_spec);
1691 if (ret < 0)
1692 goto nodata;
1693
1694 pdata->tx_dma_channel = dma_spec.args[0];
1695
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02001696 /* RX is not valid in DIT mode */
1697 if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) {
1698 ret = of_property_match_string(np, "dma-names", "rx");
1699 if (ret < 0)
1700 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001701
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02001702 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1703 &dma_spec);
1704 if (ret < 0)
1705 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001706
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02001707 pdata->rx_dma_channel = dma_spec.args[0];
1708 }
Jyri Sarha4023fe62013-10-18 18:37:43 +03001709
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301710 ret = of_property_read_u32(np, "tx-num-evt", &val);
1711 if (ret >= 0)
1712 pdata->txnumevt = val;
1713
1714 ret = of_property_read_u32(np, "rx-num-evt", &val);
1715 if (ret >= 0)
1716 pdata->rxnumevt = val;
1717
1718 ret = of_property_read_u32(np, "sram-size-playback", &val);
1719 if (ret >= 0)
1720 pdata->sram_size_playback = val;
1721
1722 ret = of_property_read_u32(np, "sram-size-capture", &val);
1723 if (ret >= 0)
1724 pdata->sram_size_capture = val;
1725
1726 return pdata;
1727
1728nodata:
1729 if (ret < 0) {
1730 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
1731 ret);
1732 pdata = NULL;
1733 }
1734 return pdata;
1735}
1736
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001737enum {
1738 PCM_EDMA,
1739 PCM_SDMA,
1740};
1741static const char *sdma_prefix = "ti,omap";
1742
1743static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp)
1744{
1745 struct dma_chan *chan;
1746 const char *tmp;
1747 int ret = PCM_EDMA;
1748
1749 if (!mcasp->dev->of_node)
1750 return PCM_EDMA;
1751
1752 tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data;
1753 chan = dma_request_slave_channel_reason(mcasp->dev, tmp);
1754 if (IS_ERR(chan)) {
1755 if (PTR_ERR(chan) != -EPROBE_DEFER)
1756 dev_err(mcasp->dev,
1757 "Can't verify DMA configuration (%ld)\n",
1758 PTR_ERR(chan));
1759 return PTR_ERR(chan);
1760 }
Xiyu Yang49aa5532020-04-25 20:48:35 +08001761 if (WARN_ON(!chan->device || !chan->device->dev)) {
1762 dma_release_channel(chan);
Takashi Iwaibefff4f2017-09-07 10:59:17 +02001763 return -EINVAL;
Xiyu Yang49aa5532020-04-25 20:48:35 +08001764 }
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001765
1766 if (chan->device->dev->of_node)
1767 ret = of_property_read_string(chan->device->dev->of_node,
1768 "compatible", &tmp);
1769 else
1770 dev_dbg(mcasp->dev, "DMA controller has no of-node\n");
1771
1772 dma_release_channel(chan);
1773 if (ret)
1774 return ret;
1775
1776 dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp);
1777 if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix)))
1778 return PCM_SDMA;
1779
1780 return PCM_EDMA;
1781}
1782
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001783static u32 davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata *pdata)
1784{
1785 int i;
1786 u32 offset = 0;
1787
1788 if (pdata->version != MCASP_VERSION_4)
1789 return pdata->tx_dma_offset;
1790
1791 for (i = 0; i < pdata->num_serializer; i++) {
1792 if (pdata->serial_dir[i] == TX_MODE) {
1793 if (!offset) {
1794 offset = DAVINCI_MCASP_TXBUF_REG(i);
1795 } else {
1796 pr_err("%s: Only one serializer allowed!\n",
1797 __func__);
1798 break;
1799 }
1800 }
1801 }
1802
1803 return offset;
1804}
1805
1806static u32 davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata *pdata)
1807{
1808 int i;
1809 u32 offset = 0;
1810
1811 if (pdata->version != MCASP_VERSION_4)
1812 return pdata->rx_dma_offset;
1813
1814 for (i = 0; i < pdata->num_serializer; i++) {
1815 if (pdata->serial_dir[i] == RX_MODE) {
1816 if (!offset) {
1817 offset = DAVINCI_MCASP_RXBUF_REG(i);
1818 } else {
1819 pr_err("%s: Only one serializer allowed!\n",
1820 __func__);
1821 break;
1822 }
1823 }
1824 }
1825
1826 return offset;
1827}
1828
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001829static int davinci_mcasp_probe(struct platform_device *pdev)
1830{
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001831 struct snd_dmaengine_dai_dma_data *dma_data;
Axel Lin508a43f2015-08-24 16:47:36 +08001832 struct resource *mem, *res, *dat;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001833 struct davinci_mcasp_pdata *pdata;
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001834 struct davinci_mcasp *mcasp;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001835 char *irq_name;
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001836 int *dma;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001837 int irq;
Julia Lawall96d31e22011-12-29 17:51:21 +01001838 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001839
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301840 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
1841 dev_err(&pdev->dev, "No platform data supplied\n");
1842 return -EINVAL;
1843 }
1844
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001845 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +01001846 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001847 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001848 return -ENOMEM;
1849
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301850 pdata = davinci_mcasp_set_pdata_from_of(pdev);
1851 if (!pdata) {
1852 dev_err(&pdev->dev, "no platform data\n");
1853 return -EINVAL;
1854 }
1855
Jyri Sarha256ba182013-10-18 18:37:42 +03001856 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001857 if (!mem) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001858 dev_warn(mcasp->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +03001859 "\"mpu\" mem resource not found, using index 0\n");
1860 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1861 if (!mem) {
1862 dev_err(&pdev->dev, "no mem resource?\n");
1863 return -ENODEV;
1864 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001865 }
1866
Axel Lin508a43f2015-08-24 16:47:36 +08001867 mcasp->base = devm_ioremap_resource(&pdev->dev, mem);
1868 if (IS_ERR(mcasp->base))
1869 return PTR_ERR(mcasp->base);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001870
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301871 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001872
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001873 mcasp->op_mode = pdata->op_mode;
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +02001874 /* sanity check for tdm slots parameter */
1875 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1876 if (pdata->tdm_slots < 2) {
1877 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1878 pdata->tdm_slots);
1879 mcasp->tdm_slots = 2;
1880 } else if (pdata->tdm_slots > 32) {
1881 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1882 pdata->tdm_slots);
1883 mcasp->tdm_slots = 32;
1884 } else {
1885 mcasp->tdm_slots = pdata->tdm_slots;
1886 }
1887 }
1888
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001889 mcasp->num_serializer = pdata->num_serializer;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001890#ifdef CONFIG_PM_SLEEP
Kees Cooka86854d2018-06-12 14:07:58 -07001891 mcasp->context.xrsr_regs = devm_kcalloc(&pdev->dev,
1892 mcasp->num_serializer, sizeof(u32),
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001893 GFP_KERNEL);
Christophe Jaillet4243e042017-08-27 08:46:50 +02001894 if (!mcasp->context.xrsr_regs) {
1895 ret = -ENOMEM;
1896 goto err;
1897 }
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001898#endif
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001899 mcasp->serial_dir = pdata->serial_dir;
1900 mcasp->version = pdata->version;
1901 mcasp->txnumevt = pdata->txnumevt;
1902 mcasp->rxnumevt = pdata->rxnumevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +02001903
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001904 mcasp->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001905
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001906 irq = platform_get_irq_byname(pdev, "common");
1907 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03001908 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common",
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001909 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05301910 if (!irq_name) {
1911 ret = -ENOMEM;
1912 goto err;
1913 }
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001914 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1915 davinci_mcasp_common_irq_handler,
Peter Ujfalusi8f511ff2015-02-02 14:38:32 +02001916 IRQF_ONESHOT | IRQF_SHARED,
1917 irq_name, mcasp);
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001918 if (ret) {
1919 dev_err(&pdev->dev, "common IRQ request failed\n");
1920 goto err;
1921 }
1922
1923 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
1924 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
1925 }
1926
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001927 irq = platform_get_irq_byname(pdev, "rx");
1928 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03001929 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001930 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05301931 if (!irq_name) {
1932 ret = -ENOMEM;
1933 goto err;
1934 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001935 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1936 davinci_mcasp_rx_irq_handler,
1937 IRQF_ONESHOT, irq_name, mcasp);
1938 if (ret) {
1939 dev_err(&pdev->dev, "RX IRQ request failed\n");
1940 goto err;
1941 }
1942
1943 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
1944 }
1945
1946 irq = platform_get_irq_byname(pdev, "tx");
1947 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03001948 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001949 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05301950 if (!irq_name) {
1951 ret = -ENOMEM;
1952 goto err;
1953 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001954 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1955 davinci_mcasp_tx_irq_handler,
1956 IRQF_ONESHOT, irq_name, mcasp);
1957 if (ret) {
1958 dev_err(&pdev->dev, "TX IRQ request failed\n");
1959 goto err;
1960 }
1961
1962 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
1963 }
1964
Jyri Sarha256ba182013-10-18 18:37:42 +03001965 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001966 if (dat)
1967 mcasp->dat_port = true;
Jyri Sarha256ba182013-10-18 18:37:42 +03001968
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001969 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001970 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001971 dma_data->addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001972 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001973 dma_data->addr = mem->start + davinci_mcasp_txdma_offset(pdata);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001974
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001975 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001976 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001977 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001978 *dma = res->start;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001979 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001980 *dma = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -07001981
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001982 /* dmaengine filter data for DT and non-DT boot */
1983 if (pdev->dev.of_node)
1984 dma_data->filter_data = "tx";
1985 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001986 dma_data->filter_data = dma;
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001987
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02001988 /* RX is not valid in DIT mode */
1989 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02001990 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02001991 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001992 dma_data->addr = dat->start;
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02001993 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001994 dma_data->addr =
1995 mem->start + davinci_mcasp_rxdma_offset(pdata);
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001996
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001997 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02001998 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1999 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002000 *dma = res->start;
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02002001 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002002 *dma = pdata->rx_dma_channel;
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02002003
2004 /* dmaengine filter data for DT and non-DT boot */
2005 if (pdev->dev.of_node)
2006 dma_data->filter_data = "rx";
2007 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002008 dma_data->filter_data = dma;
Peter Ujfalusicaa1d7942015-02-02 14:38:33 +02002009 }
Peter Ujfalusi453c4992013-11-14 11:35:34 +02002010
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002011 if (mcasp->version < MCASP_VERSION_3) {
2012 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02002013 /* dma_params->dma_addr is pointing to the data port address */
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002014 mcasp->dat_port = true;
2015 } else {
2016 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
2017 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002018
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002019 /* Allocate memory for long enough list for all possible
2020 * scenarios. Maximum number tdm slots is 32 and there cannot
2021 * be more serializers than given in the configuration. The
2022 * serializer directions could be taken into account, but it
2023 * would make code much more complex and save only couple of
2024 * bytes.
2025 */
2026 mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list =
Kees Cooka86854d2018-06-12 14:07:58 -07002027 devm_kcalloc(mcasp->dev,
2028 32 + mcasp->num_serializer - 1,
2029 sizeof(unsigned int),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002030 GFP_KERNEL);
2031
2032 mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list =
Kees Cooka86854d2018-06-12 14:07:58 -07002033 devm_kcalloc(mcasp->dev,
2034 32 + mcasp->num_serializer - 1,
2035 sizeof(unsigned int),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002036 GFP_KERNEL);
2037
2038 if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list ||
Christophe Jaillet1b8b68b2017-09-16 07:40:29 +02002039 !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) {
2040 ret = -ENOMEM;
2041 goto err;
2042 }
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002043
2044 ret = davinci_mcasp_set_ch_constraints(mcasp);
Jyri Sarha5935a052015-04-23 16:16:05 +03002045 if (ret)
2046 goto err;
2047
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002048 dev_set_drvdata(&pdev->dev, mcasp);
Peter Ujfalusiae726e92013-11-14 11:35:35 +02002049
2050 mcasp_reparent_fck(pdev);
2051
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002052 ret = devm_snd_soc_register_component(&pdev->dev,
2053 &davinci_mcasp_component,
2054 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002055
2056 if (ret != 0)
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002057 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302058
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002059 ret = davinci_mcasp_get_dma_type(mcasp);
2060 switch (ret) {
2061 case PCM_EDMA:
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002062#if IS_BUILTIN(CONFIG_SND_EDMA_SOC) || \
2063 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
2064 IS_MODULE(CONFIG_SND_EDMA_SOC))
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002065 ret = edma_pcm_platform_register(&pdev->dev);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002066#else
2067 dev_err(&pdev->dev, "Missing SND_EDMA_SOC\n");
2068 ret = -EINVAL;
2069 goto err;
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002070#endif
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002071 break;
2072 case PCM_SDMA:
Peter Ujfalusi077a4032018-05-09 14:03:55 +03002073#if IS_BUILTIN(CONFIG_SND_SDMA_SOC) || \
Jyri Sarha7f28f352014-06-13 12:49:59 +03002074 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
Peter Ujfalusi077a4032018-05-09 14:03:55 +03002075 IS_MODULE(CONFIG_SND_SDMA_SOC))
2076 ret = sdma_pcm_platform_register(&pdev->dev, NULL, NULL);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002077#else
2078 dev_err(&pdev->dev, "Missing SND_SDMA_SOC\n");
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03002079 ret = -EINVAL;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002080 goto err;
2081#endif
2082 break;
2083 default:
2084 dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret);
2085 case -EPROBE_DEFER:
2086 goto err;
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03002087 break;
2088 }
2089
2090 if (ret) {
2091 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002092 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302093 }
2094
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002095 return 0;
2096
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002097err:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302098 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002099 return ret;
2100}
2101
2102static int davinci_mcasp_remove(struct platform_device *pdev)
2103{
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302104 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002105
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002106 return 0;
2107}
2108
2109static struct platform_driver davinci_mcasp_driver = {
2110 .probe = davinci_mcasp_probe,
2111 .remove = davinci_mcasp_remove,
2112 .driver = {
2113 .name = "davinci-mcasp",
Sachin Kamatea421eb2013-05-22 16:53:37 +05302114 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002115 },
2116};
2117
Axel Linf9b8a512011-11-25 10:09:27 +08002118module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002119
2120MODULE_AUTHOR("Steve Chen");
2121MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2122MODULE_LICENSE("GPL");