blob: 734ffe925c4dbf3f70a398996657c7c6477904e1 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002/*
3 * ALSA SoC McASP Audio Layer for TI DAVINCI processor
4 *
5 * Multi-channel Audio Serial Port Driver
6 *
7 * Author: Nirmal Pandey <n-pandey@ti.com>,
8 * Suresh Rajashekara <suresh.r@ti.com>
9 * Steve Chen <schen@.mvista.com>
10 *
11 * Copyright: (C) 2009 MontaVista Software, Inc., <source@mvista.com>
12 * Copyright: (C) 2009 Texas Instruments, India
Chaithrika U Sb67f4482009-06-05 06:28:40 -040013 */
14
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040019#include <linux/delay.h>
20#include <linux/io.h>
Peter Ujfalusiae726e92013-11-14 11:35:35 +020021#include <linux/clk.h>
Hebbar, Gururaja10884342012-08-08 20:40:32 +053022#include <linux/pm_runtime.h>
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +053023#include <linux/of.h>
24#include <linux/of_platform.h>
25#include <linux/of_device.h>
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020026#include <linux/platform_data/davinci_asp.h>
Jyri Sarhaa75a0532015-03-20 13:31:08 +020027#include <linux/math64.h>
Peter Ujfalusica3d9432018-11-16 15:41:39 +020028#include <linux/bitmap.h>
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +020029#include <linux/gpio/driver.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040030
Daniel Mack64792852014-03-27 11:27:40 +010031#include <sound/asoundef.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040032#include <sound/core.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/initval.h>
36#include <sound/soc.h>
Peter Ujfalusi453c4992013-11-14 11:35:34 +020037#include <sound/dmaengine_pcm.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040038
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +030039#include "edma-pcm.h"
Peter Ujfalusif2055e12018-12-17 14:21:34 +020040#include "sdma-pcm.h"
Peter Ujfalusifb0c3c62020-02-10 16:09:50 +020041#include "udma-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 Bergmann8ca51042019-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 Ujfalusi540f1ba72019-01-03 16:05:52 +020057 DAVINCI_MCASP_PFUNC_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030058 DAVINCI_MCASP_RXMASK_REG,
59 DAVINCI_MCASP_TXMASK_REG,
60 DAVINCI_MCASP_RXTDM_REG,
61 DAVINCI_MCASP_TXTDM_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030062};
63
Peter Ujfalusi790bb942014-02-03 14:51:52 +020064struct davinci_mcasp_context {
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030065 u32 config_regs[ARRAY_SIZE(context_regs)];
Peter Ujfalusif114ce62014-10-01 16:02:12 +030066 u32 afifo_regs[2]; /* for read/write fifo control registers */
67 u32 *xrsr_regs; /* for serializer configuration */
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +020068 bool pm_state;
Peter Ujfalusi790bb942014-02-03 14:51:52 +020069};
Arnd Bergmann8ca51042019-03-07 11:11:30 +010070#endif
Peter Ujfalusi790bb942014-02-03 14:51:52 +020071
Jyri Sarhaa75a0532015-03-20 13:31:08 +020072struct davinci_mcasp_ruledata {
73 struct davinci_mcasp *mcasp;
74 int serializers;
75};
76
Peter Ujfalusi70091a32013-11-14 11:35:29 +020077struct davinci_mcasp {
Peter Ujfalusi453c4992013-11-14 11:35:34 +020078 struct snd_dmaengine_dai_dma_data dma_data[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020079 void __iomem *base;
Peter Ujfalusi487dce82013-11-14 11:35:31 +020080 u32 fifo_base;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020081 struct device *dev;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020082 struct snd_pcm_substream *substreams[2];
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +020083 unsigned int dai_fmt;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020084
85 /* McASP specific data */
86 int tdm_slots;
Jyri Sarhadd55ff82015-09-09 21:27:44 +030087 u32 tdm_mask[2];
88 int slot_width;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020089 u8 op_mode;
Peter Ujfalusibc184542018-11-16 15:41:41 +020090 u8 dismod;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020091 u8 num_serializer;
92 u8 *serial_dir;
93 u8 version;
Daniel Mack82675252014-07-16 14:04:41 +020094 u8 bclk_div;
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +020095 int streams;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020096 u32 irq_request[2];
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020097 int dma_request[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020098
Jyri Sarhaab8b14b2014-01-27 17:37:52 +020099 int sysclk_freq;
100 bool bclk_master;
Peter Ujfalusi764958f2019-06-11 15:29:41 +0300101 u32 auxclk_fs_ratio;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200102
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200103 unsigned long pdir; /* Pin direction bitfield */
104
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200105 /* McASP FIFO related */
106 u8 txnumevt;
107 u8 rxnumevt;
108
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200109 bool dat_port;
110
Peter Ujfalusi11277832014-11-10 12:32:16 +0200111 /* Used for comstraint setting on the second stream */
112 u32 channels;
Peter Ujfalusi2448c8132019-07-26 09:42:44 +0300113 int max_format_width;
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300114 u8 active_serializers[2];
Peter Ujfalusi11277832014-11-10 12:32:16 +0200115
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +0200116#ifdef CONFIG_GPIOLIB
117 struct gpio_chip gpio_chip;
118#endif
119
Peter Ujfalusi61754712019-01-03 16:05:50 +0200120#ifdef CONFIG_PM
Peter Ujfalusi790bb942014-02-03 14:51:52 +0200121 struct davinci_mcasp_context context;
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200122#endif
Jyri Sarhaa75a0532015-03-20 13:31:08 +0200123
124 struct davinci_mcasp_ruledata ruledata[2];
Jyri Sarha5935a052015-04-23 16:16:05 +0300125 struct snd_pcm_hw_constraint_list chconstr[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200126};
127
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200128static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
129 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400130{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200131 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400132 __raw_writel(__raw_readl(reg) | val, reg);
133}
134
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200135static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
136 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400137{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200138 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400139 __raw_writel((__raw_readl(reg) & ~(val)), reg);
140}
141
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200142static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
143 u32 val, u32 mask)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400144{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200145 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400146 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
147}
148
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200149static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
150 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400151{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200152 __raw_writel(val, mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400153}
154
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200155static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400156{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200157 return (u32)__raw_readl(mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400158}
159
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200160static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400161{
162 int i = 0;
163
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200164 mcasp_set_bits(mcasp, ctl_reg, val);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400165
166 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
167 /* loop count is to avoid the lock-up */
168 for (i = 0; i < 1000; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200169 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400170 break;
171 }
172
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200173 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400174 printk(KERN_ERR "GBLCTL write error\n");
175}
176
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200177static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
178{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200179 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
180 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200181
182 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
183}
184
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200185static inline void mcasp_set_clk_pdir(struct davinci_mcasp *mcasp, bool enable)
186{
187 u32 bit = PIN_BIT_AMUTE;
188
189 for_each_set_bit_from(bit, &mcasp->pdir, PIN_BIT_AFSR + 1) {
190 if (enable)
191 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
192 else
193 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
194 }
195}
196
197static inline void mcasp_set_axr_pdir(struct davinci_mcasp *mcasp, bool enable)
198{
199 u32 bit;
200
Peter Ujfalusi34a2a802019-07-25 11:34:23 +0300201 for_each_set_bit(bit, &mcasp->pdir, PIN_BIT_AMUTE) {
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200202 if (enable)
203 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
204 else
205 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
206 }
207}
208
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200209static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400210{
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200211 if (mcasp->rxnumevt) { /* enable FIFO */
212 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
213
214 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
215 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
216 }
217
Peter Ujfalusi44982732014-10-29 13:55:45 +0200218 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200219 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
220 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200221 /*
222 * When ASYNC == 0 the transmit and receive sections operate
223 * synchronously from the transmit clock and frame sync. We need to make
224 * sure that the TX signlas are enabled when starting reception.
225 */
226 if (mcasp_is_synchronous(mcasp)) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200227 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
228 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi34a2a802019-07-25 11:34:23 +0300229 mcasp_set_clk_pdir(mcasp, true);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200230 }
231
Peter Ujfalusi44982732014-10-29 13:55:45 +0200232 /* Activate serializer(s) */
Peter Ujfalusi1003c272018-11-16 15:41:38 +0200233 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200234 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200235 /* Release RX state machine */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200236 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200237 /* Release Frame Sync generator */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200238 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200239 if (mcasp_is_synchronous(mcasp))
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200240 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200241
242 /* enable receive IRQs */
243 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
244 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400245}
246
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200247static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400248{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400249 u32 cnt;
250
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200251 if (mcasp->txnumevt) { /* enable FIFO */
252 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
253
254 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
255 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
256 }
257
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200258 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200259 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
260 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200261 mcasp_set_clk_pdir(mcasp, true);
262
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200263 /* Activate serializer(s) */
Peter Ujfalusi1003c272018-11-16 15:41:38 +0200264 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200265 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400266
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200267 /* wait for XDATA to be cleared */
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400268 cnt = 0;
Peter Ujfalusie2a0c9f2015-12-11 13:06:24 +0200269 while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) &&
270 (cnt < 100000))
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400271 cnt++;
272
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200273 mcasp_set_axr_pdir(mcasp, true);
274
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200275 /* Release TX state machine */
276 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
277 /* Release Frame Sync generator */
278 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200279
280 /* enable transmit IRQs */
281 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
282 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400283}
284
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200285static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400286{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200287 mcasp->streams++;
288
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200289 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200290 mcasp_start_tx(mcasp);
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200291 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200292 mcasp_start_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400293}
294
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200295static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400296{
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200297 /* disable IRQ sources */
298 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
299 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
300
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200301 /*
302 * In synchronous mode stop the TX clocks if no other stream is
303 * running
304 */
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200305 if (mcasp_is_synchronous(mcasp) && !mcasp->streams) {
306 mcasp_set_clk_pdir(mcasp, false);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200307 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200308 }
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200309
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200310 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
311 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200312
313 if (mcasp->rxnumevt) { /* disable FIFO */
314 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
315
316 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
317 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400318}
319
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200320static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400321{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200322 u32 val = 0;
323
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200324 /* disable IRQ sources */
325 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
326 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
327
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200328 /*
329 * In synchronous mode keep TX clocks running if the capture stream is
330 * still running.
331 */
332 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
333 val = TXHCLKRST | TXCLKRST | TXFSRST;
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200334 else
335 mcasp_set_clk_pdir(mcasp, false);
336
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200337
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200338 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
339 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200340
341 if (mcasp->txnumevt) { /* disable FIFO */
342 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
343
344 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
345 }
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200346
347 mcasp_set_axr_pdir(mcasp, false);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400348}
349
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200350static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400351{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200352 mcasp->streams--;
353
Peter Ujfalusi03808662014-10-29 13:55:46 +0200354 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200355 mcasp_stop_tx(mcasp);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200356 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200357 mcasp_stop_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400358}
359
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200360static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
361{
362 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
363 struct snd_pcm_substream *substream;
364 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
365 u32 handled_mask = 0;
366 u32 stat;
367
368 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
369 if (stat & XUNDRN & irq_mask) {
370 dev_warn(mcasp->dev, "Transmit buffer underflow\n");
371 handled_mask |= XUNDRN;
372
373 substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
Takashi Iwaidae35d12018-07-04 16:01:43 +0200374 if (substream)
375 snd_pcm_stop_xrun(substream);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200376 }
377
378 if (!handled_mask)
379 dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
380 stat);
381
382 if (stat & XRERR)
383 handled_mask |= XRERR;
384
385 /* Ack the handled event only */
386 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
387
388 return IRQ_RETVAL(handled_mask);
389}
390
391static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
392{
393 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
394 struct snd_pcm_substream *substream;
395 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
396 u32 handled_mask = 0;
397 u32 stat;
398
399 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
400 if (stat & ROVRN & irq_mask) {
401 dev_warn(mcasp->dev, "Receive buffer overflow\n");
402 handled_mask |= ROVRN;
403
404 substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
Takashi Iwaidae35d12018-07-04 16:01:43 +0200405 if (substream)
406 snd_pcm_stop_xrun(substream);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200407 }
408
409 if (!handled_mask)
410 dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
411 stat);
412
413 if (stat & XRERR)
414 handled_mask |= XRERR;
415
416 /* Ack the handled event only */
417 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
418
419 return IRQ_RETVAL(handled_mask);
420}
421
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +0200422static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
423{
424 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
425 irqreturn_t ret = IRQ_NONE;
426
427 if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
428 ret = davinci_mcasp_tx_irq_handler(irq, data);
429
430 if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
431 ret |= davinci_mcasp_rx_irq_handler(irq, data);
432
433 return ret;
434}
435
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400436static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
437 unsigned int fmt)
438{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200439 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200440 int ret = 0;
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300441 u32 data_delay;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300442 bool fs_pol_rising;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300443 bool inv_fs = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400444
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200445 if (!fmt)
446 return 0;
447
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200448 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5296cf22012-10-04 15:08:42 +0200449 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300450 case SND_SOC_DAIFMT_DSP_A:
451 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
452 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300453 /* 1st data bit occur one ACLK cycle after the frame sync */
454 data_delay = 1;
455 break;
Daniel Mack5296cf22012-10-04 15:08:42 +0200456 case SND_SOC_DAIFMT_DSP_B:
457 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200458 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
459 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300460 /* No delay after FS */
461 data_delay = 0;
Daniel Mack5296cf22012-10-04 15:08:42 +0200462 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300463 case SND_SOC_DAIFMT_I2S:
Daniel Mack5296cf22012-10-04 15:08:42 +0200464 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200465 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
466 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300467 /* 1st data bit occur one ACLK cycle after the frame sync */
468 data_delay = 1;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300469 /* FS need to be inverted */
470 inv_fs = true;
Daniel Mack5296cf22012-10-04 15:08:42 +0200471 break;
Peter Ujfalusi816fe202019-07-25 11:34:11 +0300472 case SND_SOC_DAIFMT_RIGHT_J:
Peter Ujfalusi423761e2014-04-04 14:31:46 +0300473 case SND_SOC_DAIFMT_LEFT_J:
474 /* configure a full-word SYNC pulse (LRCLK) */
475 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
476 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
477 /* No delay after FS */
478 data_delay = 0;
479 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300480 default:
481 ret = -EINVAL;
482 goto out;
Daniel Mack5296cf22012-10-04 15:08:42 +0200483 }
484
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300485 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
486 FSXDLY(3));
487 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
488 FSRDLY(3));
489
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400490 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
491 case SND_SOC_DAIFMT_CBS_CFS:
492 /* codec is clock and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200493 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
494 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400495
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200496 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
497 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400498
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200499 /* BCLK */
500 set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
501 set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
502 /* Frame Sync */
503 set_bit(PIN_BIT_AFSX, &mcasp->pdir);
504 set_bit(PIN_BIT_AFSR, &mcasp->pdir);
505
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200506 mcasp->bclk_master = 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400507 break;
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200508 case SND_SOC_DAIFMT_CBS_CFM:
509 /* codec is clock slave and frame master */
510 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
511 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
512
513 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
514 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
515
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200516 /* BCLK */
517 set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
518 set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
519 /* Frame Sync */
520 clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
521 clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
522
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200523 mcasp->bclk_master = 1;
524 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400525 case SND_SOC_DAIFMT_CBM_CFS:
526 /* codec is clock master and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200527 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
528 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400529
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200530 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
531 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400532
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200533 /* BCLK */
534 clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
535 clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
536 /* Frame Sync */
537 set_bit(PIN_BIT_AFSX, &mcasp->pdir);
538 set_bit(PIN_BIT_AFSR, &mcasp->pdir);
539
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200540 mcasp->bclk_master = 0;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400541 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400542 case SND_SOC_DAIFMT_CBM_CFM:
543 /* codec is clock and frame master */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200544 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
545 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400546
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200547 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
548 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400549
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200550 /* BCLK */
551 clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
552 clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
553 /* Frame Sync */
554 clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
555 clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
556
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200557 mcasp->bclk_master = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400558 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400559 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200560 ret = -EINVAL;
561 goto out;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400562 }
563
564 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
565 case SND_SOC_DAIFMT_IB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200566 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300567 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300568 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400569 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400570 case SND_SOC_DAIFMT_NB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200571 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300572 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300573 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400574 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400575 case SND_SOC_DAIFMT_IB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200576 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300577 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300578 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400579 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400580 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200581 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200582 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300583 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400584 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400585 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200586 ret = -EINVAL;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300587 goto out;
588 }
589
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300590 if (inv_fs)
591 fs_pol_rising = !fs_pol_rising;
592
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300593 if (fs_pol_rising) {
594 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
595 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
596 } else {
597 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
598 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400599 }
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200600
601 mcasp->dai_fmt = fmt;
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200602out:
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200603 pm_runtime_put(mcasp->dev);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200604 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400605}
606
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300607static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp *mcasp, int div_id,
Jyri Sarha88135432014-08-06 16:47:16 +0300608 int div, bool explicit)
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200609{
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200610 pm_runtime_get_sync(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200611 switch (div_id) {
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300612 case MCASP_CLKDIV_AUXCLK: /* MCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200613 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200614 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200615 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200616 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
617 break;
618
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300619 case MCASP_CLKDIV_BCLK: /* BCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200620 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200621 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200622 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200623 ACLKRDIV(div - 1), ACLKRDIV_MASK);
Jyri Sarha88135432014-08-06 16:47:16 +0300624 if (explicit)
625 mcasp->bclk_div = div;
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200626 break;
627
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300628 case MCASP_CLKDIV_BCLK_FS_RATIO:
629 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300630 * BCLK/LRCLK ratio descries how many bit-clock cycles
631 * fit into one frame. The clock ratio is given for a
632 * full period of data (for I2S format both left and
633 * right channels), so it has to be divided by number
634 * of tdm-slots (for I2S - divided by 2).
635 * Instead of storing this ratio, we calculate a new
636 * tdm_slot width by dividing the the ratio by the
637 * number of configured tdm slots.
638 */
639 mcasp->slot_width = div / mcasp->tdm_slots;
640 if (div % mcasp->tdm_slots)
641 dev_warn(mcasp->dev,
642 "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
643 __func__, div, mcasp->tdm_slots);
Daniel Mack1b3bc062012-12-05 18:20:38 +0100644 break;
645
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200646 default:
647 return -EINVAL;
648 }
649
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200650 pm_runtime_put(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200651 return 0;
652}
653
Jyri Sarha88135432014-08-06 16:47:16 +0300654static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
655 int div)
656{
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300657 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
658
659 return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1);
Jyri Sarha88135432014-08-06 16:47:16 +0300660}
661
Daniel Mack5b66aa22012-10-04 15:08:41 +0200662static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
663 unsigned int freq, int dir)
664{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200665 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200666
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200667 pm_runtime_get_sync(mcasp->dev);
Peter Ujfalusi253f5842019-12-04 21:20:05 +0200668
669 if (dir == SND_SOC_CLOCK_IN) {
670 switch (clk_id) {
671 case MCASP_CLK_HCLK_AHCLK:
672 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
673 AHCLKXE);
674 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
675 AHCLKRE);
676 clear_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
677 break;
678 case MCASP_CLK_HCLK_AUXCLK:
679 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
680 AHCLKXE);
681 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
682 AHCLKRE);
683 set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
684 break;
685 default:
686 dev_err(mcasp->dev, "Invalid clk id: %d\n", clk_id);
687 goto out;
688 }
689 } else {
690 /* Select AUXCLK as HCLK */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200691 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
692 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200693 set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200694 }
Peter Ujfalusi253f5842019-12-04 21:20:05 +0200695 /*
696 * When AHCLK X/R is selected to be output it means that the HCLK is
697 * the same clock - coming via AUXCLK.
698 */
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200699 mcasp->sysclk_freq = freq;
Peter Ujfalusi253f5842019-12-04 21:20:05 +0200700out:
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200701 pm_runtime_put(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200702 return 0;
703}
704
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300705/* All serializers must have equal number of channels */
706static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream,
707 int serializers)
708{
709 struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream];
710 unsigned int *list = (unsigned int *) cl->list;
711 int slots = mcasp->tdm_slots;
712 int i, count = 0;
713
714 if (mcasp->tdm_mask[stream])
715 slots = hweight32(mcasp->tdm_mask[stream]);
716
Peter Ujfalusie4798d22017-05-11 09:58:22 +0300717 for (i = 1; i <= slots; i++)
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300718 list[count++] = i;
719
720 for (i = 2; i <= serializers; i++)
721 list[count++] = i*slots;
722
723 cl->count = count;
724
725 return 0;
726}
727
728static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp)
729{
730 int rx_serializers = 0, tx_serializers = 0, ret, i;
731
732 for (i = 0; i < mcasp->num_serializer; i++)
733 if (mcasp->serial_dir[i] == TX_MODE)
734 tx_serializers++;
735 else if (mcasp->serial_dir[i] == RX_MODE)
736 rx_serializers++;
737
738 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK,
739 tx_serializers);
740 if (ret)
741 return ret;
742
743 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE,
744 rx_serializers);
745
746 return ret;
747}
748
749
750static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai,
751 unsigned int tx_mask,
752 unsigned int rx_mask,
753 int slots, int slot_width)
754{
755 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
756
757 dev_dbg(mcasp->dev,
758 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
759 __func__, tx_mask, rx_mask, slots, slot_width);
760
761 if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
762 dev_err(mcasp->dev,
763 "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
764 tx_mask, rx_mask, slots);
765 return -EINVAL;
766 }
767
768 if (slot_width &&
769 (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) {
770 dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n",
771 __func__, slot_width);
772 return -EINVAL;
773 }
774
775 mcasp->tdm_slots = slots;
Andreas Dannenberg1bdd5932015-11-09 12:19:19 -0600776 mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
777 mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300778 mcasp->slot_width = slot_width;
779
780 return davinci_mcasp_set_ch_constraints(mcasp);
781}
782
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200783static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Jyri Sarha14a998b2015-09-17 10:39:05 +0300784 int sample_width)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400785{
Daniel Mackba764b32012-12-05 18:20:37 +0100786 u32 fmt;
Peter Ujfalusi816fe202019-07-25 11:34:11 +0300787 u32 tx_rotate, rx_rotate, slot_width;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300788 u32 mask = (1ULL << sample_width) - 1;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300789
Peter Ujfalusi816fe202019-07-25 11:34:11 +0300790 if (mcasp->slot_width)
Jyri Sarha14a998b2015-09-17 10:39:05 +0300791 slot_width = mcasp->slot_width;
Peter Ujfalusi2448c8132019-07-26 09:42:44 +0300792 else if (mcasp->max_format_width)
793 slot_width = mcasp->max_format_width;
Peter Ujfalusi816fe202019-07-25 11:34:11 +0300794 else
795 slot_width = sample_width;
796 /*
797 * TX rotation:
798 * right aligned formats: rotate w/ slot_width
799 * left aligned formats: rotate w/ sample_width
800 *
801 * RX rotation:
802 * right aligned formats: no rotation needed
803 * left aligned formats: rotate w/ (slot_width - sample_width)
804 */
805 if ((mcasp->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
806 SND_SOC_DAIFMT_RIGHT_J) {
807 tx_rotate = (slot_width / 4) & 0x7;
808 rx_rotate = 0;
809 } else {
810 tx_rotate = (sample_width / 4) & 0x7;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300811 rx_rotate = (slot_width - sample_width) / 4;
Peter Ujfalusid742b922014-11-10 12:32:19 +0200812 }
Daniel Mack1b3bc062012-12-05 18:20:38 +0100813
Daniel Mackba764b32012-12-05 18:20:37 +0100814 /* mapping of the XSSZ bit-field as described in the datasheet */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300815 fmt = (slot_width >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400816
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200817 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200818 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
819 RXSSZ(0x0F));
820 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
821 TXSSZ(0x0F));
822 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
823 TXROT(7));
824 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
825 RXROT(7));
826 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200827 }
828
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200829 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400830
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400831 return 0;
832}
833
Peter Ujfalusi662ffae2014-01-30 15:15:22 +0200834static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300835 int period_words, int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400836{
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300837 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400838 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400839 u8 tx_ser = 0;
840 u8 rx_ser = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200841 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100842 u8 max_active_serializers = (channels + slots - 1) / slots;
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300843 u8 max_rx_serializers, max_tx_serializers;
Peter Ujfalusi72383192015-09-14 16:06:48 +0300844 int active_serializers, numevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +0200845 u32 reg;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400846 /* Default configuration */
Peter Ujfalusi40448e52014-04-04 15:56:30 +0300847 if (mcasp->version < MCASP_VERSION_3)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200848 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400849
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400850 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200851 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
852 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300853 max_tx_serializers = max_active_serializers;
854 max_rx_serializers =
855 mcasp->active_serializers[SNDRV_PCM_STREAM_CAPTURE];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400856 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200857 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
858 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300859 max_tx_serializers =
860 mcasp->active_serializers[SNDRV_PCM_STREAM_PLAYBACK];
861 max_rx_serializers = max_active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400862 }
863
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200864 for (i = 0; i < mcasp->num_serializer; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200865 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
866 mcasp->serial_dir[i]);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200867 if (mcasp->serial_dir[i] == TX_MODE &&
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300868 tx_ser < max_tx_serializers) {
Misael Lopez Cruz19db62e2015-06-08 16:03:47 +0300869 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
Peter Ujfalusibc184542018-11-16 15:41:41 +0200870 mcasp->dismod, DISMOD_MASK);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200871 set_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400872 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200873 } else if (mcasp->serial_dir[i] == RX_MODE &&
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300874 rx_ser < max_rx_serializers) {
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200875 clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400876 rx_ser++;
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +0300877 } else {
878 /* Inactive or unused pin, set it to inactive */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200879 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
880 SRMOD_INACTIVE, SRMOD_MASK);
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +0300881 /* If unused, set DISMOD for the pin */
882 if (mcasp->serial_dir[i] != INACTIVE_MODE)
883 mcasp_mod_bits(mcasp,
884 DAVINCI_MCASP_XRSRCTL_REG(i),
885 mcasp->dismod, DISMOD_MASK);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200886 clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400887 }
888 }
889
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300890 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
891 active_serializers = tx_ser;
892 numevt = mcasp->txnumevt;
893 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
894 } else {
895 active_serializers = rx_ser;
896 numevt = mcasp->rxnumevt;
897 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
898 }
Daniel Mackecf327c2013-03-08 14:19:38 +0100899
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300900 if (active_serializers < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200901 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300902 "enabled in mcasp (%d)\n", channels,
903 active_serializers * slots);
Daniel Mackecf327c2013-03-08 14:19:38 +0100904 return -EINVAL;
905 }
906
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300907 /* AFIFO is not in use */
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300908 if (!numevt) {
909 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300910 if (active_serializers > 1) {
911 /*
912 * If more than one serializers are in use we have one
913 * DMA request to provide data for all serializers.
914 * For example if three serializers are enabled the DMA
915 * need to transfer three words per DMA request.
916 */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300917 dma_data->maxburst = active_serializers;
918 } else {
Peter Ujfalusi33445642014-04-01 15:55:12 +0300919 dma_data->maxburst = 0;
920 }
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300921
922 goto out;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300923 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400924
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300925 if (period_words % active_serializers) {
926 dev_err(mcasp->dev, "Invalid combination of period words and "
927 "active serializers: %d, %d\n", period_words,
928 active_serializers);
929 return -EINVAL;
930 }
931
932 /*
933 * Calculate the optimal AFIFO depth for platform side:
934 * The number of words for numevt need to be in steps of active
935 * serializers.
936 */
Peter Ujfalusi72383192015-09-14 16:06:48 +0300937 numevt = (numevt / active_serializers) * active_serializers;
938
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300939 while (period_words % numevt && numevt > 0)
940 numevt -= active_serializers;
941 if (numevt <= 0)
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300942 numevt = active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400943
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300944 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
945 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
Michal Bachraty2952b272013-02-28 16:07:08 +0100946
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300947 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300948 if (numevt == 1)
949 numevt = 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300950 dma_data->maxburst = numevt;
951
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300952out:
953 mcasp->active_serializers[stream] = active_serializers;
954
Michal Bachraty2952b272013-02-28 16:07:08 +0100955 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400956}
957
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200958static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
959 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400960{
961 int i, active_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200962 int total_slots;
963 int active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400964 u32 mask = 0;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200965 u32 busel = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400966
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200967 total_slots = mcasp->tdm_slots;
968
969 /*
970 * If more than one serializer is needed, then use them with
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300971 * all the specified tdm_slots. Otherwise, one serializer can
972 * cope with the transaction using just as many slots as there
973 * are channels in the stream.
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200974 */
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300975 if (mcasp->tdm_mask[stream]) {
976 active_slots = hweight32(mcasp->tdm_mask[stream]);
977 active_serializers = (channels + active_slots - 1) /
978 active_slots;
Peter Ujfalusifd14f442019-06-20 12:20:02 +0300979 if (active_serializers == 1)
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300980 active_slots = channels;
Peter Ujfalusifd14f442019-06-20 12:20:02 +0300981 for (i = 0; i < total_slots; i++) {
982 if ((1 << i) & mcasp->tdm_mask[stream]) {
983 mask |= (1 << i);
984 if (--active_slots <= 0)
985 break;
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300986 }
987 }
988 } else {
989 active_serializers = (channels + total_slots - 1) / total_slots;
990 if (active_serializers == 1)
991 active_slots = channels;
992 else
993 active_slots = total_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200994
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300995 for (i = 0; i < active_slots; i++)
996 mask |= (1 << i);
997 }
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +0300998
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200999 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -04001000
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001001 if (!mcasp->dat_port)
1002 busel = TXSEL;
1003
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001004 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1005 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
1006 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
1007 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
1008 FSXMOD(total_slots), FSXMOD(0x1FF));
1009 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
1010 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
1011 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
1012 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
1013 FSRMOD(total_slots), FSRMOD(0x1FF));
Peter Ujfalusi0ad7d3a2015-11-23 12:51:53 +02001014 /*
1015 * If McASP is set to be TX/RX synchronous and the playback is
1016 * not running already we need to configure the TX slots in
1017 * order to have correct FSX on the bus
1018 */
1019 if (mcasp_is_synchronous(mcasp) && !mcasp->channels)
1020 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
1021 FSXMOD(total_slots), FSXMOD(0x1FF));
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001022 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001023
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001024 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001025}
1026
1027/* S/PDIF */
Daniel Mack64792852014-03-27 11:27:40 +01001028static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
1029 unsigned int rate)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001030{
Daniel Mack64792852014-03-27 11:27:40 +01001031 u32 cs_value = 0;
1032 u8 *cs_bytes = (u8*) &cs_value;
1033
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001034 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
1035 and LSB first */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001036 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001037
1038 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001039 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001040
1041 /* Set the TX tdm : for all the slots */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001042 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001043
1044 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001045 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001046
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001047 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001048
1049 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001050 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001051
1052 /* Enable the DIT */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001053 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001054
Daniel Mack64792852014-03-27 11:27:40 +01001055 /* Set S/PDIF channel status bits */
1056 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
1057 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
1058
1059 switch (rate) {
1060 case 22050:
1061 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
1062 break;
1063 case 24000:
1064 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
1065 break;
1066 case 32000:
1067 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
1068 break;
1069 case 44100:
1070 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
1071 break;
1072 case 48000:
1073 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
1074 break;
1075 case 88200:
1076 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
1077 break;
1078 case 96000:
1079 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
1080 break;
1081 case 176400:
1082 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
1083 break;
1084 case 192000:
1085 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
1086 break;
1087 default:
1088 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
1089 return -EINVAL;
1090 }
1091
1092 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
1093 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
1094
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001095 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001096}
1097
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001098static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001099 unsigned int sysclk_freq,
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001100 unsigned int bclk_freq, bool set)
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001101{
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001102 u32 reg = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG);
1103 int div = sysclk_freq / bclk_freq;
1104 int rem = sysclk_freq % bclk_freq;
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001105 int error_ppm;
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001106 int aux_div = 1;
1107
1108 if (div > (ACLKXDIV_MASK + 1)) {
1109 if (reg & AHCLKXE) {
1110 aux_div = div / (ACLKXDIV_MASK + 1);
1111 if (div % (ACLKXDIV_MASK + 1))
1112 aux_div++;
1113
1114 sysclk_freq /= aux_div;
1115 div = sysclk_freq / bclk_freq;
1116 rem = sysclk_freq % bclk_freq;
1117 } else if (set) {
1118 dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
1119 sysclk_freq);
1120 }
1121 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001122
1123 if (rem != 0) {
1124 if (div == 0 ||
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001125 ((sysclk_freq / div) - bclk_freq) >
1126 (bclk_freq - (sysclk_freq / (div+1)))) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001127 div++;
1128 rem = rem - bclk_freq;
1129 }
1130 }
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001131 error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem,
1132 (int)bclk_freq)) / div - 1000000;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001133
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001134 if (set) {
1135 if (error_ppm)
1136 dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n",
1137 error_ppm);
1138
1139 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_BCLK, div, 0);
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001140 if (reg & AHCLKXE)
1141 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK,
1142 aux_div, 0);
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001143 }
1144
1145 return error_ppm;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001146}
1147
Peter Ujfalusi5fcb4572018-08-31 11:24:56 +03001148static inline u32 davinci_mcasp_tx_delay(struct davinci_mcasp *mcasp)
1149{
1150 if (!mcasp->txnumevt)
1151 return 0;
1152
1153 return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_WFIFOSTS_OFFSET);
1154}
1155
1156static inline u32 davinci_mcasp_rx_delay(struct davinci_mcasp *mcasp)
1157{
1158 if (!mcasp->rxnumevt)
1159 return 0;
1160
1161 return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_RFIFOSTS_OFFSET);
1162}
1163
1164static snd_pcm_sframes_t davinci_mcasp_delay(
1165 struct snd_pcm_substream *substream,
1166 struct snd_soc_dai *cpu_dai)
1167{
1168 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1169 u32 fifo_use;
1170
1171 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1172 fifo_use = davinci_mcasp_tx_delay(mcasp);
1173 else
1174 fifo_use = davinci_mcasp_rx_delay(mcasp);
1175
1176 /*
1177 * Divide the used locations with the channel count to get the
1178 * FIFO usage in samples (don't care about partial samples in the
1179 * buffer).
1180 */
1181 return fifo_use / substream->runtime->channels;
1182}
1183
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001184static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
1185 struct snd_pcm_hw_params *params,
1186 struct snd_soc_dai *cpu_dai)
1187{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001188 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001189 int word_length;
Peter Ujfalusia7e46bd2014-02-03 14:51:50 +02001190 int channels = params_channels(params);
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001191 int period_size = params_period_size(params);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001192 int ret;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001193
Peter Ujfalusib7989e22019-07-25 11:34:32 +03001194 switch (params_format(params)) {
1195 case SNDRV_PCM_FORMAT_U8:
1196 case SNDRV_PCM_FORMAT_S8:
1197 word_length = 8;
1198 break;
1199
1200 case SNDRV_PCM_FORMAT_U16_LE:
1201 case SNDRV_PCM_FORMAT_S16_LE:
1202 word_length = 16;
1203 break;
1204
1205 case SNDRV_PCM_FORMAT_U24_3LE:
1206 case SNDRV_PCM_FORMAT_S24_3LE:
1207 word_length = 24;
1208 break;
1209
1210 case SNDRV_PCM_FORMAT_U24_LE:
1211 case SNDRV_PCM_FORMAT_S24_LE:
1212 word_length = 24;
1213 break;
1214
1215 case SNDRV_PCM_FORMAT_U32_LE:
1216 case SNDRV_PCM_FORMAT_S32_LE:
1217 word_length = 32;
1218 break;
1219
1220 default:
1221 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
1222 return -EINVAL;
1223 }
1224
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +02001225 ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt);
1226 if (ret)
1227 return ret;
1228
Daniel Mack82675252014-07-16 14:04:41 +02001229 /*
1230 * If mcasp is BCLK master, and a BCLK divider was not provided by
1231 * the machine driver, we need to calculate the ratio.
1232 */
1233 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarha1f114f72015-04-23 16:16:04 +03001234 int slots = mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001235 int rate = params_rate(params);
1236 int sbits = params_width(params);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001237
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001238 if (mcasp->slot_width)
1239 sbits = mcasp->slot_width;
1240
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001241 davinci_mcasp_calc_clk_div(mcasp, mcasp->sysclk_freq,
1242 rate * sbits * slots, true);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001243 }
1244
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001245 ret = mcasp_common_hw_param(mcasp, substream->stream,
1246 period_size * channels, channels);
Peter Ujfalusi0f7d9a62014-01-30 15:15:24 +02001247 if (ret)
1248 return ret;
1249
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001250 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
Daniel Mack64792852014-03-27 11:27:40 +01001251 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001252 else
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +02001253 ret = mcasp_i2s_hw_param(mcasp, substream->stream,
1254 channels);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001255
1256 if (ret)
1257 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001258
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001259 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001260
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001261 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001262 mcasp->channels = channels;
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001263 if (!mcasp->max_format_width)
1264 mcasp->max_format_width = word_length;
1265 }
Peter Ujfalusi11277832014-11-10 12:32:16 +02001266
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001267 return 0;
1268}
1269
1270static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
1271 int cmd, struct snd_soc_dai *cpu_dai)
1272{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001273 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001274 int ret = 0;
1275
1276 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001277 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +05301278 case SNDRV_PCM_TRIGGER_START:
1279 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001280 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001281 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001282 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +05301283 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001284 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001285 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001286 break;
1287
1288 default:
1289 ret = -EINVAL;
1290 }
1291
1292 return ret;
1293}
1294
Peter Ujfalusi1e112c32019-07-26 09:42:43 +03001295static int davinci_mcasp_hw_rule_slot_width(struct snd_pcm_hw_params *params,
1296 struct snd_pcm_hw_rule *rule)
1297{
1298 struct davinci_mcasp_ruledata *rd = rule->private;
1299 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1300 struct snd_mask nfmt;
1301 int i, slot_width;
1302
1303 snd_mask_none(&nfmt);
1304 slot_width = rd->mcasp->slot_width;
1305
1306 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1307 if (snd_mask_test(fmt, i)) {
1308 if (snd_pcm_format_width(i) <= slot_width) {
1309 snd_mask_set(&nfmt, i);
1310 }
1311 }
1312 }
1313
1314 return snd_mask_refine(fmt, &nfmt);
1315}
1316
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001317static int davinci_mcasp_hw_rule_format_width(struct snd_pcm_hw_params *params,
1318 struct snd_pcm_hw_rule *rule)
1319{
1320 struct davinci_mcasp_ruledata *rd = rule->private;
1321 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1322 struct snd_mask nfmt;
1323 int i, format_width;
1324
1325 snd_mask_none(&nfmt);
1326 format_width = rd->mcasp->max_format_width;
1327
1328 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1329 if (snd_mask_test(fmt, i)) {
1330 if (snd_pcm_format_width(i) == format_width) {
1331 snd_mask_set(&nfmt, i);
1332 }
1333 }
1334 }
1335
1336 return snd_mask_refine(fmt, &nfmt);
1337}
1338
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001339static const unsigned int davinci_mcasp_dai_rates[] = {
1340 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1341 88200, 96000, 176400, 192000,
1342};
1343
1344#define DAVINCI_MAX_RATE_ERROR_PPM 1000
1345
1346static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params,
1347 struct snd_pcm_hw_rule *rule)
1348{
1349 struct davinci_mcasp_ruledata *rd = rule->private;
1350 struct snd_interval *ri =
1351 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1352 int sbits = params_width(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001353 int slots = rd->mcasp->tdm_slots;
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001354 struct snd_interval range;
1355 int i;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001356
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001357 if (rd->mcasp->slot_width)
1358 sbits = rd->mcasp->slot_width;
1359
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001360 snd_interval_any(&range);
1361 range.empty = 1;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001362
1363 for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) {
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001364 if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) {
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001365 uint bclk_freq = sbits * slots *
1366 davinci_mcasp_dai_rates[i];
1367 unsigned int sysclk_freq;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001368 int ppm;
1369
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001370 if (rd->mcasp->auxclk_fs_ratio)
1371 sysclk_freq = davinci_mcasp_dai_rates[i] *
1372 rd->mcasp->auxclk_fs_ratio;
1373 else
1374 sysclk_freq = rd->mcasp->sysclk_freq;
1375
1376 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
1377 bclk_freq, false);
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001378 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1379 if (range.empty) {
1380 range.min = davinci_mcasp_dai_rates[i];
1381 range.empty = 0;
1382 }
1383 range.max = davinci_mcasp_dai_rates[i];
1384 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001385 }
1386 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001387
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001388 dev_dbg(rd->mcasp->dev,
1389 "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1390 ri->min, ri->max, range.min, range.max, sbits, slots);
1391
1392 return snd_interval_refine(hw_param_interval(params, rule->var),
1393 &range);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001394}
1395
1396static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params,
1397 struct snd_pcm_hw_rule *rule)
1398{
1399 struct davinci_mcasp_ruledata *rd = rule->private;
1400 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1401 struct snd_mask nfmt;
1402 int rate = params_rate(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001403 int slots = rd->mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001404 int i, count = 0;
1405
1406 snd_mask_none(&nfmt);
1407
Peter Ujfalusi9be072a2016-09-01 10:05:12 +03001408 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001409 if (snd_mask_test(fmt, i)) {
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001410 uint sbits = snd_pcm_format_width(i);
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001411 unsigned int sysclk_freq;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001412 int ppm;
1413
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001414 if (rd->mcasp->auxclk_fs_ratio)
1415 sysclk_freq = rate *
1416 rd->mcasp->auxclk_fs_ratio;
1417 else
1418 sysclk_freq = rd->mcasp->sysclk_freq;
1419
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001420 if (rd->mcasp->slot_width)
1421 sbits = rd->mcasp->slot_width;
1422
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001423 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001424 sbits * slots * rate,
1425 false);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001426 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1427 snd_mask_set(&nfmt, i);
1428 count++;
1429 }
1430 }
1431 }
1432 dev_dbg(rd->mcasp->dev,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001433 "%d possible sample format for %d Hz and %d tdm slots\n",
1434 count, rate, slots);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001435
1436 return snd_mask_refine(fmt, &nfmt);
1437}
1438
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001439static int davinci_mcasp_hw_rule_min_periodsize(
1440 struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1441{
1442 struct snd_interval *period_size = hw_param_interval(params,
1443 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1444 struct snd_interval frames;
1445
1446 snd_interval_any(&frames);
1447 frames.min = 64;
1448 frames.integer = 1;
1449
1450 return snd_interval_refine(period_size, &frames);
1451}
1452
Peter Ujfalusi11277832014-11-10 12:32:16 +02001453static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
1454 struct snd_soc_dai *cpu_dai)
1455{
1456 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001457 struct davinci_mcasp_ruledata *ruledata =
1458 &mcasp->ruledata[substream->stream];
Peter Ujfalusi11277832014-11-10 12:32:16 +02001459 u32 max_channels = 0;
Peter Ujfalusi1e112c32019-07-26 09:42:43 +03001460 int i, dir, ret;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001461 int tdm_slots = mcasp->tdm_slots;
1462
Peter Ujfalusi19357362016-05-09 13:39:14 +03001463 /* Do not allow more then one stream per direction */
1464 if (mcasp->substreams[substream->stream])
1465 return -EBUSY;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001466
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001467 mcasp->substreams[substream->stream] = substream;
1468
Peter Ujfalusi19357362016-05-09 13:39:14 +03001469 if (mcasp->tdm_mask[substream->stream])
1470 tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]);
1471
Peter Ujfalusi11277832014-11-10 12:32:16 +02001472 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1473 return 0;
1474
1475 /*
1476 * Limit the maximum allowed channels for the first stream:
1477 * number of serializers for the direction * tdm slots per serializer
1478 */
1479 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1480 dir = TX_MODE;
1481 else
1482 dir = RX_MODE;
1483
1484 for (i = 0; i < mcasp->num_serializer; i++) {
1485 if (mcasp->serial_dir[i] == dir)
1486 max_channels++;
1487 }
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001488 ruledata->serializers = max_channels;
Peter Ujfalusi1e112c32019-07-26 09:42:43 +03001489 ruledata->mcasp = mcasp;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001490 max_channels *= tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001491 /*
1492 * If the already active stream has less channels than the calculated
Peter Ujfalusib7989e22019-07-25 11:34:32 +03001493 * limit based on the seirializers * tdm_slots, and only one serializer
1494 * is in use we need to use that as a constraint for the second stream.
1495 * Otherwise (first stream or less allowed channels or more than one
1496 * serializer in use) we use the calculated constraint.
Peter Ujfalusi11277832014-11-10 12:32:16 +02001497 */
Peter Ujfalusib7989e22019-07-25 11:34:32 +03001498 if (mcasp->channels && mcasp->channels < max_channels &&
1499 ruledata->serializers == 1)
Peter Ujfalusi11277832014-11-10 12:32:16 +02001500 max_channels = mcasp->channels;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001501 /*
1502 * But we can always allow channels upto the amount of
1503 * the available tdm_slots.
1504 */
1505 if (max_channels < tdm_slots)
1506 max_channels = tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001507
1508 snd_pcm_hw_constraint_minmax(substream->runtime,
1509 SNDRV_PCM_HW_PARAM_CHANNELS,
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001510 0, max_channels);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001511
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001512 snd_pcm_hw_constraint_list(substream->runtime,
1513 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1514 &mcasp->chconstr[substream->stream]);
1515
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001516 if (mcasp->max_format_width) {
1517 /*
1518 * Only allow formats which require same amount of bits on the
1519 * bus as the currently running stream
1520 */
1521 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1522 SNDRV_PCM_HW_PARAM_FORMAT,
1523 davinci_mcasp_hw_rule_format_width,
1524 ruledata,
1525 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1526 if (ret)
1527 return ret;
1528 }
1529 else if (mcasp->slot_width) {
Peter Ujfalusi1e112c32019-07-26 09:42:43 +03001530 /* Only allow formats require <= slot_width bits on the bus */
1531 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1532 SNDRV_PCM_HW_PARAM_FORMAT,
1533 davinci_mcasp_hw_rule_slot_width,
1534 ruledata,
1535 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1536 if (ret)
1537 return ret;
1538 }
Jyri Sarha5935a052015-04-23 16:16:05 +03001539
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001540 /*
1541 * If we rely on implicit BCLK divider setting we should
1542 * set constraints based on what we can provide.
1543 */
1544 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001545 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1546 SNDRV_PCM_HW_PARAM_RATE,
1547 davinci_mcasp_hw_rule_rate,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001548 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001549 SNDRV_PCM_HW_PARAM_FORMAT, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001550 if (ret)
1551 return ret;
1552 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1553 SNDRV_PCM_HW_PARAM_FORMAT,
1554 davinci_mcasp_hw_rule_format,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001555 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001556 SNDRV_PCM_HW_PARAM_RATE, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001557 if (ret)
1558 return ret;
1559 }
1560
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001561 snd_pcm_hw_rule_add(substream->runtime, 0,
1562 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1563 davinci_mcasp_hw_rule_min_periodsize, NULL,
1564 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1565
Peter Ujfalusi11277832014-11-10 12:32:16 +02001566 return 0;
1567}
1568
1569static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1570 struct snd_soc_dai *cpu_dai)
1571{
1572 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1573
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001574 mcasp->substreams[substream->stream] = NULL;
Peter Ujfalusib7989e22019-07-25 11:34:32 +03001575 mcasp->active_serializers[substream->stream] = 0;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001576
Peter Ujfalusi11277832014-11-10 12:32:16 +02001577 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1578 return;
1579
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001580 if (!cpu_dai->active) {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001581 mcasp->channels = 0;
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001582 mcasp->max_format_width = 0;
1583 }
Peter Ujfalusi11277832014-11-10 12:32:16 +02001584}
1585
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001586static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001587 .startup = davinci_mcasp_startup,
1588 .shutdown = davinci_mcasp_shutdown,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001589 .trigger = davinci_mcasp_trigger,
Peter Ujfalusi5fcb4572018-08-31 11:24:56 +03001590 .delay = davinci_mcasp_delay,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001591 .hw_params = davinci_mcasp_hw_params,
1592 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +02001593 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +02001594 .set_sysclk = davinci_mcasp_set_sysclk,
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001595 .set_tdm_slot = davinci_mcasp_set_tdm_slot,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001596};
1597
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001598static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1599{
1600 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1601
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001602 dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1603 dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001604
1605 return 0;
1606}
1607
Peter Ujfalusied29cd52013-11-14 11:35:22 +02001608#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1609
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001610#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1611 SNDRV_PCM_FMTBIT_U8 | \
1612 SNDRV_PCM_FMTBIT_S16_LE | \
1613 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +02001614 SNDRV_PCM_FMTBIT_S24_LE | \
1615 SNDRV_PCM_FMTBIT_U24_LE | \
1616 SNDRV_PCM_FMTBIT_S24_3LE | \
1617 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001618 SNDRV_PCM_FMTBIT_S32_LE | \
1619 SNDRV_PCM_FMTBIT_U32_LE)
1620
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001622 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001623 .name = "davinci-mcasp.0",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001624 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001625 .playback = {
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001626 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001627 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001628 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001629 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001630 },
1631 .capture = {
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001632 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001633 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001634 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001635 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001636 },
1637 .ops = &davinci_mcasp_dai_ops,
1638
Jyri Sarha295c3402015-09-09 21:27:42 +03001639 .symmetric_rates = 1,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001640 },
1641 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +02001642 .name = "davinci-mcasp.1",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001643 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001644 .playback = {
1645 .channels_min = 1,
1646 .channels_max = 384,
1647 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001648 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001649 },
1650 .ops = &davinci_mcasp_dai_ops,
1651 },
1652
1653};
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001654
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001655static const struct snd_soc_component_driver davinci_mcasp_component = {
1656 .name = "davinci-mcasp",
1657};
1658
Jyri Sarha256ba182013-10-18 18:37:42 +03001659/* Some HW specific values and defaults. The rest is filled in from DT. */
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001660static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001661 .tx_dma_offset = 0x400,
1662 .rx_dma_offset = 0x400,
Jyri Sarha256ba182013-10-18 18:37:42 +03001663 .version = MCASP_VERSION_1,
1664};
1665
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001666static struct davinci_mcasp_pdata da830_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001667 .tx_dma_offset = 0x2000,
1668 .rx_dma_offset = 0x2000,
Jyri Sarha256ba182013-10-18 18:37:42 +03001669 .version = MCASP_VERSION_2,
1670};
1671
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001672static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001673 .tx_dma_offset = 0,
1674 .rx_dma_offset = 0,
Jyri Sarha256ba182013-10-18 18:37:42 +03001675 .version = MCASP_VERSION_3,
1676};
1677
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001678static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001679 /* The CFG port offset will be calculated if it is needed */
1680 .tx_dma_offset = 0,
1681 .rx_dma_offset = 0,
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001682 .version = MCASP_VERSION_4,
1683};
1684
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301685static const struct of_device_id mcasp_dt_ids[] = {
1686 {
1687 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001688 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301689 },
1690 {
1691 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001692 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301693 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301694 {
Jyri Sarha3af9e032013-10-18 18:37:44 +03001695 .compatible = "ti,am33xx-mcasp-audio",
Peter Ujfalusib14899d2013-11-14 11:35:37 +02001696 .data = &am33xx_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301697 },
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001698 {
1699 .compatible = "ti,dra7-mcasp-audio",
1700 .data = &dra7_mcasp_pdata,
1701 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301702 { /* sentinel */ }
1703};
1704MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1705
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001706static int mcasp_reparent_fck(struct platform_device *pdev)
1707{
1708 struct device_node *node = pdev->dev.of_node;
1709 struct clk *gfclk, *parent_clk;
1710 const char *parent_name;
1711 int ret;
1712
1713 if (!node)
1714 return 0;
1715
1716 parent_name = of_get_property(node, "fck_parent", NULL);
1717 if (!parent_name)
1718 return 0;
1719
Peter Ujfalusic6702542016-01-27 15:02:49 +02001720 dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n");
1721
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001722 gfclk = clk_get(&pdev->dev, "fck");
1723 if (IS_ERR(gfclk)) {
1724 dev_err(&pdev->dev, "failed to get fck\n");
1725 return PTR_ERR(gfclk);
1726 }
1727
1728 parent_clk = clk_get(NULL, parent_name);
1729 if (IS_ERR(parent_clk)) {
1730 dev_err(&pdev->dev, "failed to get parent clock\n");
1731 ret = PTR_ERR(parent_clk);
1732 goto err1;
1733 }
1734
1735 ret = clk_set_parent(gfclk, parent_clk);
1736 if (ret) {
1737 dev_err(&pdev->dev, "failed to reparent fck\n");
1738 goto err2;
1739 }
1740
1741err2:
1742 clk_put(parent_clk);
1743err1:
1744 clk_put(gfclk);
1745 return ret;
1746}
1747
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001748static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301749 struct platform_device *pdev)
1750{
1751 struct device_node *np = pdev->dev.of_node;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001752 struct davinci_mcasp_pdata *pdata = NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301753 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +05301754 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001755 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301756
1757 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301758 u32 val;
1759 int i, ret = 0;
1760
1761 if (pdev->dev.platform_data) {
1762 pdata = pdev->dev.platform_data;
Peter Ujfalusibc184542018-11-16 15:41:41 +02001763 pdata->dismod = DISMOD_LOW;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301764 return pdata;
1765 } else if (match) {
Peter Ujfalusi272ee032016-06-02 12:55:24 +03001766 pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata),
1767 GFP_KERNEL);
Colin Ian Kingf4d95de2020-02-10 09:24:22 +00001768 if (!pdata)
1769 return NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301770 } else {
1771 /* control shouldn't reach here. something is wrong */
1772 ret = -EINVAL;
1773 goto nodata;
1774 }
1775
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301776 ret = of_property_read_u32(np, "op-mode", &val);
1777 if (ret >= 0)
1778 pdata->op_mode = val;
1779
1780 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +01001781 if (ret >= 0) {
1782 if (val < 2 || val > 32) {
1783 dev_err(&pdev->dev,
1784 "tdm-slots must be in rage [2-32]\n");
1785 ret = -EINVAL;
1786 goto nodata;
1787 }
1788
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301789 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +01001790 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301791
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301792 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1793 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301794 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001795 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1796 (sizeof(*of_serial_dir) * val),
1797 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301798 if (!of_serial_dir) {
1799 ret = -ENOMEM;
1800 goto nodata;
1801 }
1802
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001803 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301804 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1805
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001806 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301807 pdata->serial_dir = of_serial_dir;
1808 }
1809
Jyri Sarha4023fe62013-10-18 18:37:43 +03001810 ret = of_property_match_string(np, "dma-names", "tx");
1811 if (ret < 0)
1812 goto nodata;
1813
1814 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1815 &dma_spec);
1816 if (ret < 0)
1817 goto nodata;
1818
1819 pdata->tx_dma_channel = dma_spec.args[0];
1820
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001821 /* RX is not valid in DIT mode */
1822 if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) {
1823 ret = of_property_match_string(np, "dma-names", "rx");
1824 if (ret < 0)
1825 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001826
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001827 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1828 &dma_spec);
1829 if (ret < 0)
1830 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001831
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001832 pdata->rx_dma_channel = dma_spec.args[0];
1833 }
Jyri Sarha4023fe62013-10-18 18:37:43 +03001834
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301835 ret = of_property_read_u32(np, "tx-num-evt", &val);
1836 if (ret >= 0)
1837 pdata->txnumevt = val;
1838
1839 ret = of_property_read_u32(np, "rx-num-evt", &val);
1840 if (ret >= 0)
1841 pdata->rxnumevt = val;
1842
1843 ret = of_property_read_u32(np, "sram-size-playback", &val);
1844 if (ret >= 0)
1845 pdata->sram_size_playback = val;
1846
1847 ret = of_property_read_u32(np, "sram-size-capture", &val);
1848 if (ret >= 0)
1849 pdata->sram_size_capture = val;
1850
Peter Ujfalusibc184542018-11-16 15:41:41 +02001851 ret = of_property_read_u32(np, "dismod", &val);
1852 if (ret >= 0) {
1853 if (val == 0 || val == 2 || val == 3) {
1854 pdata->dismod = DISMOD_VAL(val);
1855 } else {
1856 dev_warn(&pdev->dev, "Invalid dismod value: %u\n", val);
1857 pdata->dismod = DISMOD_LOW;
1858 }
1859 } else {
1860 pdata->dismod = DISMOD_LOW;
1861 }
1862
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301863 return pdata;
1864
1865nodata:
1866 if (ret < 0) {
1867 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
1868 ret);
1869 pdata = NULL;
1870 }
1871 return pdata;
1872}
1873
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001874enum {
1875 PCM_EDMA,
1876 PCM_SDMA,
Peter Ujfalusifb0c3c62020-02-10 16:09:50 +02001877 PCM_UDMA,
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001878};
1879static const char *sdma_prefix = "ti,omap";
1880
1881static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp)
1882{
1883 struct dma_chan *chan;
1884 const char *tmp;
1885 int ret = PCM_EDMA;
1886
1887 if (!mcasp->dev->of_node)
1888 return PCM_EDMA;
1889
1890 tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data;
Peter Ujfalusif0c97132019-11-13 11:54:45 +02001891 chan = dma_request_chan(mcasp->dev, tmp);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001892 if (IS_ERR(chan)) {
1893 if (PTR_ERR(chan) != -EPROBE_DEFER)
1894 dev_err(mcasp->dev,
1895 "Can't verify DMA configuration (%ld)\n",
1896 PTR_ERR(chan));
1897 return PTR_ERR(chan);
1898 }
Takashi Iwaibefff4f2017-09-07 10:59:17 +02001899 if (WARN_ON(!chan->device || !chan->device->dev))
1900 return -EINVAL;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001901
1902 if (chan->device->dev->of_node)
1903 ret = of_property_read_string(chan->device->dev->of_node,
1904 "compatible", &tmp);
1905 else
1906 dev_dbg(mcasp->dev, "DMA controller has no of-node\n");
1907
1908 dma_release_channel(chan);
1909 if (ret)
1910 return ret;
1911
1912 dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp);
1913 if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix)))
1914 return PCM_SDMA;
Peter Ujfalusifb0c3c62020-02-10 16:09:50 +02001915 else if (strstr(tmp, "udmap"))
1916 return PCM_UDMA;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001917
1918 return PCM_EDMA;
1919}
1920
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001921static u32 davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata *pdata)
1922{
1923 int i;
1924 u32 offset = 0;
1925
1926 if (pdata->version != MCASP_VERSION_4)
1927 return pdata->tx_dma_offset;
1928
1929 for (i = 0; i < pdata->num_serializer; i++) {
1930 if (pdata->serial_dir[i] == TX_MODE) {
1931 if (!offset) {
1932 offset = DAVINCI_MCASP_TXBUF_REG(i);
1933 } else {
1934 pr_err("%s: Only one serializer allowed!\n",
1935 __func__);
1936 break;
1937 }
1938 }
1939 }
1940
1941 return offset;
1942}
1943
1944static u32 davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata *pdata)
1945{
1946 int i;
1947 u32 offset = 0;
1948
1949 if (pdata->version != MCASP_VERSION_4)
1950 return pdata->rx_dma_offset;
1951
1952 for (i = 0; i < pdata->num_serializer; i++) {
1953 if (pdata->serial_dir[i] == RX_MODE) {
1954 if (!offset) {
1955 offset = DAVINCI_MCASP_RXBUF_REG(i);
1956 } else {
1957 pr_err("%s: Only one serializer allowed!\n",
1958 __func__);
1959 break;
1960 }
1961 }
1962 }
1963
1964 return offset;
1965}
1966
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +02001967#ifdef CONFIG_GPIOLIB
1968static int davinci_mcasp_gpio_request(struct gpio_chip *chip, unsigned offset)
1969{
1970 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1971
1972 if (mcasp->num_serializer && offset < mcasp->num_serializer &&
1973 mcasp->serial_dir[offset] != INACTIVE_MODE) {
1974 dev_err(mcasp->dev, "AXR%u pin is used for audio\n", offset);
1975 return -EBUSY;
1976 }
1977
1978 /* Do not change the PIN yet */
1979
1980 return pm_runtime_get_sync(mcasp->dev);
1981}
1982
1983static void davinci_mcasp_gpio_free(struct gpio_chip *chip, unsigned offset)
1984{
1985 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1986
1987 /* Set the direction to input */
1988 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
1989
1990 /* Set the pin as McASP pin */
1991 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
1992
1993 pm_runtime_put_sync(mcasp->dev);
1994}
1995
1996static int davinci_mcasp_gpio_direction_out(struct gpio_chip *chip,
1997 unsigned offset, int value)
1998{
1999 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2000 u32 val;
2001
2002 if (value)
2003 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2004 else
2005 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2006
2007 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
2008 if (!(val & BIT(offset))) {
2009 /* Set the pin as GPIO pin */
2010 mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2011
2012 /* Set the direction to output */
2013 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2014 }
2015
2016 return 0;
2017}
2018
2019static void davinci_mcasp_gpio_set(struct gpio_chip *chip, unsigned offset,
2020 int value)
2021{
2022 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2023
2024 if (value)
2025 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2026 else
2027 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2028}
2029
2030static int davinci_mcasp_gpio_direction_in(struct gpio_chip *chip,
2031 unsigned offset)
2032{
2033 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2034 u32 val;
2035
2036 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
2037 if (!(val & BIT(offset))) {
2038 /* Set the direction to input */
2039 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2040
2041 /* Set the pin as GPIO pin */
2042 mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2043 }
2044
2045 return 0;
2046}
2047
2048static int davinci_mcasp_gpio_get(struct gpio_chip *chip, unsigned offset)
2049{
2050 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2051 u32 val;
2052
2053 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDSET_REG);
2054 if (val & BIT(offset))
2055 return 1;
2056
2057 return 0;
2058}
2059
2060static int davinci_mcasp_gpio_get_direction(struct gpio_chip *chip,
2061 unsigned offset)
2062{
2063 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2064 u32 val;
2065
2066 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDIR_REG);
2067 if (val & BIT(offset))
2068 return 0;
2069
2070 return 1;
2071}
2072
2073static const struct gpio_chip davinci_mcasp_template_chip = {
2074 .owner = THIS_MODULE,
2075 .request = davinci_mcasp_gpio_request,
2076 .free = davinci_mcasp_gpio_free,
2077 .direction_output = davinci_mcasp_gpio_direction_out,
2078 .set = davinci_mcasp_gpio_set,
2079 .direction_input = davinci_mcasp_gpio_direction_in,
2080 .get = davinci_mcasp_gpio_get,
2081 .get_direction = davinci_mcasp_gpio_get_direction,
2082 .base = -1,
2083 .ngpio = 32,
2084};
2085
2086static int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2087{
2088 if (!of_property_read_bool(mcasp->dev->of_node, "gpio-controller"))
2089 return 0;
2090
2091 mcasp->gpio_chip = davinci_mcasp_template_chip;
2092 mcasp->gpio_chip.label = dev_name(mcasp->dev);
2093 mcasp->gpio_chip.parent = mcasp->dev;
2094#ifdef CONFIG_OF_GPIO
2095 mcasp->gpio_chip.of_node = mcasp->dev->of_node;
2096#endif
2097
2098 return devm_gpiochip_add_data(mcasp->dev, &mcasp->gpio_chip, mcasp);
2099}
2100
2101#else /* CONFIG_GPIOLIB */
2102static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2103{
2104 return 0;
2105}
2106#endif /* CONFIG_GPIOLIB */
2107
Peter Ujfalusi764958f2019-06-11 15:29:41 +03002108static int davinci_mcasp_get_dt_params(struct davinci_mcasp *mcasp)
2109{
2110 struct device_node *np = mcasp->dev->of_node;
2111 int ret;
2112 u32 val;
2113
2114 if (!np)
2115 return 0;
2116
2117 ret = of_property_read_u32(np, "auxclk-fs-ratio", &val);
2118 if (ret >= 0)
2119 mcasp->auxclk_fs_ratio = val;
2120
2121 return 0;
2122}
2123
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002124static int davinci_mcasp_probe(struct platform_device *pdev)
2125{
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002126 struct snd_dmaengine_dai_dma_data *dma_data;
Axel Lin508a43f2015-08-24 16:47:36 +08002127 struct resource *mem, *res, *dat;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02002128 struct davinci_mcasp_pdata *pdata;
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002129 struct davinci_mcasp *mcasp;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002130 char *irq_name;
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002131 int *dma;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002132 int irq;
Julia Lawall96d31e22011-12-29 17:51:21 +01002133 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002134
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05302135 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
2136 dev_err(&pdev->dev, "No platform data supplied\n");
2137 return -EINVAL;
2138 }
2139
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002140 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +01002141 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002142 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002143 return -ENOMEM;
2144
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05302145 pdata = davinci_mcasp_set_pdata_from_of(pdev);
2146 if (!pdata) {
2147 dev_err(&pdev->dev, "no platform data\n");
2148 return -EINVAL;
2149 }
2150
Jyri Sarha256ba182013-10-18 18:37:42 +03002151 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002152 if (!mem) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002153 dev_warn(mcasp->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +03002154 "\"mpu\" mem resource not found, using index 0\n");
2155 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2156 if (!mem) {
2157 dev_err(&pdev->dev, "no mem resource?\n");
2158 return -ENODEV;
2159 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002160 }
2161
Axel Lin508a43f2015-08-24 16:47:36 +08002162 mcasp->base = devm_ioremap_resource(&pdev->dev, mem);
2163 if (IS_ERR(mcasp->base))
2164 return PTR_ERR(mcasp->base);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002165
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302166 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002167
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002168 mcasp->op_mode = pdata->op_mode;
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +02002169 /* sanity check for tdm slots parameter */
2170 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
2171 if (pdata->tdm_slots < 2) {
2172 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
2173 pdata->tdm_slots);
2174 mcasp->tdm_slots = 2;
2175 } else if (pdata->tdm_slots > 32) {
2176 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
2177 pdata->tdm_slots);
2178 mcasp->tdm_slots = 32;
2179 } else {
2180 mcasp->tdm_slots = pdata->tdm_slots;
2181 }
2182 }
2183
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002184 mcasp->num_serializer = pdata->num_serializer;
Peter Ujfalusi61754712019-01-03 16:05:50 +02002185#ifdef CONFIG_PM
Kees Cooka86854d2018-06-12 14:07:58 -07002186 mcasp->context.xrsr_regs = devm_kcalloc(&pdev->dev,
2187 mcasp->num_serializer, sizeof(u32),
Peter Ujfalusif114ce62014-10-01 16:02:12 +03002188 GFP_KERNEL);
Christophe Jaillet4243e042017-08-27 08:46:50 +02002189 if (!mcasp->context.xrsr_regs) {
2190 ret = -ENOMEM;
2191 goto err;
2192 }
Peter Ujfalusif114ce62014-10-01 16:02:12 +03002193#endif
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002194 mcasp->serial_dir = pdata->serial_dir;
2195 mcasp->version = pdata->version;
2196 mcasp->txnumevt = pdata->txnumevt;
2197 mcasp->rxnumevt = pdata->rxnumevt;
Peter Ujfalusibc184542018-11-16 15:41:41 +02002198 mcasp->dismod = pdata->dismod;
Peter Ujfalusi487dce82013-11-14 11:35:31 +02002199
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002200 mcasp->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002201
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002202 irq = platform_get_irq_byname(pdev, "common");
2203 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002204 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common",
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002205 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302206 if (!irq_name) {
2207 ret = -ENOMEM;
2208 goto err;
2209 }
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002210 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2211 davinci_mcasp_common_irq_handler,
Peter Ujfalusi8f511ff2015-02-02 14:38:32 +02002212 IRQF_ONESHOT | IRQF_SHARED,
2213 irq_name, mcasp);
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002214 if (ret) {
2215 dev_err(&pdev->dev, "common IRQ request failed\n");
2216 goto err;
2217 }
2218
2219 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2220 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2221 }
2222
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002223 irq = platform_get_irq_byname(pdev, "rx");
2224 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002225 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002226 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302227 if (!irq_name) {
2228 ret = -ENOMEM;
2229 goto err;
2230 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002231 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2232 davinci_mcasp_rx_irq_handler,
2233 IRQF_ONESHOT, irq_name, mcasp);
2234 if (ret) {
2235 dev_err(&pdev->dev, "RX IRQ request failed\n");
2236 goto err;
2237 }
2238
2239 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2240 }
2241
2242 irq = platform_get_irq_byname(pdev, "tx");
2243 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002244 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002245 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302246 if (!irq_name) {
2247 ret = -ENOMEM;
2248 goto err;
2249 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002250 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2251 davinci_mcasp_tx_irq_handler,
2252 IRQF_ONESHOT, irq_name, mcasp);
2253 if (ret) {
2254 dev_err(&pdev->dev, "TX IRQ request failed\n");
2255 goto err;
2256 }
2257
2258 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2259 }
2260
Jyri Sarha256ba182013-10-18 18:37:42 +03002261 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002262 if (dat)
2263 mcasp->dat_port = true;
Jyri Sarha256ba182013-10-18 18:37:42 +03002264
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002265 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002266 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002267 dma_data->addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002268 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03002269 dma_data->addr = mem->start + davinci_mcasp_txdma_offset(pdata);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002270
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002271 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002272 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +03002273 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002274 *dma = res->start;
Jyri Sarha4023fe62013-10-18 18:37:43 +03002275 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002276 *dma = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -07002277
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002278 /* dmaengine filter data for DT and non-DT boot */
2279 if (pdev->dev.of_node)
2280 dma_data->filter_data = "tx";
2281 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002282 dma_data->filter_data = dma;
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002283
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002284 /* RX is not valid in DIT mode */
2285 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002286 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002287 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002288 dma_data->addr = dat->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002289 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03002290 dma_data->addr =
2291 mem->start + davinci_mcasp_rxdma_offset(pdata);
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002292
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002293 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002294 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
2295 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002296 *dma = res->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002297 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002298 *dma = pdata->rx_dma_channel;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002299
2300 /* dmaengine filter data for DT and non-DT boot */
2301 if (pdev->dev.of_node)
2302 dma_data->filter_data = "rx";
2303 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002304 dma_data->filter_data = dma;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002305 }
Peter Ujfalusi453c4992013-11-14 11:35:34 +02002306
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002307 if (mcasp->version < MCASP_VERSION_3) {
2308 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02002309 /* dma_params->dma_addr is pointing to the data port address */
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002310 mcasp->dat_port = true;
2311 } else {
2312 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
2313 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002314
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002315 /* Allocate memory for long enough list for all possible
2316 * scenarios. Maximum number tdm slots is 32 and there cannot
2317 * be more serializers than given in the configuration. The
2318 * serializer directions could be taken into account, but it
2319 * would make code much more complex and save only couple of
2320 * bytes.
2321 */
2322 mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list =
Kees Cooka86854d2018-06-12 14:07:58 -07002323 devm_kcalloc(mcasp->dev,
2324 32 + mcasp->num_serializer - 1,
2325 sizeof(unsigned int),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002326 GFP_KERNEL);
2327
2328 mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list =
Kees Cooka86854d2018-06-12 14:07:58 -07002329 devm_kcalloc(mcasp->dev,
2330 32 + mcasp->num_serializer - 1,
2331 sizeof(unsigned int),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002332 GFP_KERNEL);
2333
2334 if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list ||
Christophe Jaillet1b8b68b2017-09-16 07:40:29 +02002335 !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) {
2336 ret = -ENOMEM;
2337 goto err;
2338 }
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002339
2340 ret = davinci_mcasp_set_ch_constraints(mcasp);
Jyri Sarha5935a052015-04-23 16:16:05 +03002341 if (ret)
2342 goto err;
2343
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002344 dev_set_drvdata(&pdev->dev, mcasp);
Peter Ujfalusiae726e92013-11-14 11:35:35 +02002345
2346 mcasp_reparent_fck(pdev);
2347
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +02002348 /* All PINS as McASP */
2349 pm_runtime_get_sync(mcasp->dev);
2350 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
2351 pm_runtime_put(mcasp->dev);
2352
2353 ret = davinci_mcasp_init_gpiochip(mcasp);
2354 if (ret)
2355 goto err;
2356
Peter Ujfalusi764958f2019-06-11 15:29:41 +03002357 ret = davinci_mcasp_get_dt_params(mcasp);
2358 if (ret)
2359 return -EINVAL;
2360
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002361 ret = devm_snd_soc_register_component(&pdev->dev,
2362 &davinci_mcasp_component,
2363 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002364
2365 if (ret != 0)
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002366 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302367
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002368 ret = davinci_mcasp_get_dma_type(mcasp);
2369 switch (ret) {
2370 case PCM_EDMA:
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002371 ret = edma_pcm_platform_register(&pdev->dev);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002372 break;
2373 case PCM_SDMA:
Janusz Krzysztofik3e802e92019-06-02 16:55:49 +02002374 ret = sdma_pcm_platform_register(&pdev->dev, "tx", "rx");
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002375 break;
Peter Ujfalusifb0c3c62020-02-10 16:09:50 +02002376 case PCM_UDMA:
2377 ret = udma_pcm_platform_register(&pdev->dev);
2378 break;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002379 default:
2380 dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret);
2381 case -EPROBE_DEFER:
2382 goto err;
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03002383 break;
2384 }
2385
2386 if (ret) {
2387 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002388 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302389 }
2390
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002391 return 0;
2392
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002393err:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302394 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002395 return ret;
2396}
2397
2398static int davinci_mcasp_remove(struct platform_device *pdev)
2399{
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302400 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002401
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002402 return 0;
2403}
2404
Peter Ujfalusi61754712019-01-03 16:05:50 +02002405#ifdef CONFIG_PM
2406static int davinci_mcasp_runtime_suspend(struct device *dev)
2407{
2408 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2409 struct davinci_mcasp_context *context = &mcasp->context;
2410 u32 reg;
2411 int i;
2412
2413 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2414 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
2415
2416 if (mcasp->txnumevt) {
2417 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2418 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
2419 }
2420 if (mcasp->rxnumevt) {
2421 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2422 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
2423 }
2424
2425 for (i = 0; i < mcasp->num_serializer; i++)
2426 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
2427 DAVINCI_MCASP_XRSRCTL_REG(i));
2428
2429 return 0;
2430}
2431
2432static int davinci_mcasp_runtime_resume(struct device *dev)
2433{
2434 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2435 struct davinci_mcasp_context *context = &mcasp->context;
2436 u32 reg;
2437 int i;
2438
2439 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2440 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
2441
2442 if (mcasp->txnumevt) {
2443 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2444 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
2445 }
2446 if (mcasp->rxnumevt) {
2447 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2448 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
2449 }
2450
2451 for (i = 0; i < mcasp->num_serializer; i++)
2452 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
2453 context->xrsr_regs[i]);
2454
2455 return 0;
2456}
2457
2458#endif
2459
2460static const struct dev_pm_ops davinci_mcasp_pm_ops = {
2461 SET_RUNTIME_PM_OPS(davinci_mcasp_runtime_suspend,
2462 davinci_mcasp_runtime_resume,
2463 NULL)
2464};
2465
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002466static struct platform_driver davinci_mcasp_driver = {
2467 .probe = davinci_mcasp_probe,
2468 .remove = davinci_mcasp_remove,
2469 .driver = {
2470 .name = "davinci-mcasp",
Peter Ujfalusi61754712019-01-03 16:05:50 +02002471 .pm = &davinci_mcasp_pm_ops,
Sachin Kamatea421eb2013-05-22 16:53:37 +05302472 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002473 },
2474};
2475
Axel Linf9b8a512011-11-25 10:09:27 +08002476module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002477
2478MODULE_AUTHOR("Steve Chen");
2479MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2480MODULE_LICENSE("GPL");