blob: 6247ec3d3a096c23ec0396ba308ca2d7a1a92558 [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 Ujfalusi1125d922020-11-06 09:25:50 +020079 struct davinci_mcasp_pdata *pdata;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020080 void __iomem *base;
Peter Ujfalusi487dce82013-11-14 11:35:31 +020081 u32 fifo_base;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020082 struct device *dev;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020083 struct snd_pcm_substream *substreams[2];
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +020084 unsigned int dai_fmt;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020085
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +020086 /* Audio can not be enabled due to missing parameter(s) */
87 bool missing_audio_param;
88
Peter Ujfalusi21400a72013-11-14 11:35:26 +020089 /* McASP specific data */
90 int tdm_slots;
Jyri Sarhadd55ff82015-09-09 21:27:44 +030091 u32 tdm_mask[2];
92 int slot_width;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020093 u8 op_mode;
Peter Ujfalusibc184542018-11-16 15:41:41 +020094 u8 dismod;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020095 u8 num_serializer;
96 u8 *serial_dir;
97 u8 version;
Daniel Mack82675252014-07-16 14:04:41 +020098 u8 bclk_div;
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +020099 int streams;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200100 u32 irq_request[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200101
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200102 int sysclk_freq;
103 bool bclk_master;
Peter Ujfalusi764958f2019-06-11 15:29:41 +0300104 u32 auxclk_fs_ratio;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200105
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200106 unsigned long pdir; /* Pin direction bitfield */
107
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200108 /* McASP FIFO related */
109 u8 txnumevt;
110 u8 rxnumevt;
111
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200112 bool dat_port;
113
Peter Ujfalusi11277832014-11-10 12:32:16 +0200114 /* Used for comstraint setting on the second stream */
115 u32 channels;
Peter Ujfalusi2448c8132019-07-26 09:42:44 +0300116 int max_format_width;
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300117 u8 active_serializers[2];
Peter Ujfalusi11277832014-11-10 12:32:16 +0200118
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +0200119#ifdef CONFIG_GPIOLIB
120 struct gpio_chip gpio_chip;
121#endif
122
Peter Ujfalusi61754712019-01-03 16:05:50 +0200123#ifdef CONFIG_PM
Peter Ujfalusi790bb942014-02-03 14:51:52 +0200124 struct davinci_mcasp_context context;
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200125#endif
Jyri Sarhaa75a0532015-03-20 13:31:08 +0200126
127 struct davinci_mcasp_ruledata ruledata[2];
Jyri Sarha5935a052015-04-23 16:16:05 +0300128 struct snd_pcm_hw_constraint_list chconstr[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200129};
130
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200131static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
132 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400133{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200134 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400135 __raw_writel(__raw_readl(reg) | val, reg);
136}
137
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200138static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
139 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400140{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200141 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400142 __raw_writel((__raw_readl(reg) & ~(val)), reg);
143}
144
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200145static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
146 u32 val, u32 mask)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400147{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200148 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400149 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
150}
151
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200152static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
153 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400154{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200155 __raw_writel(val, mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400156}
157
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200158static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400159{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200160 return (u32)__raw_readl(mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400161}
162
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200163static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400164{
165 int i = 0;
166
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200167 mcasp_set_bits(mcasp, ctl_reg, val);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400168
169 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
170 /* loop count is to avoid the lock-up */
171 for (i = 0; i < 1000; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200172 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400173 break;
174 }
175
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200176 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400177 printk(KERN_ERR "GBLCTL write error\n");
178}
179
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200180static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
181{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200182 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
183 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200184
185 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
186}
187
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200188static inline void mcasp_set_clk_pdir(struct davinci_mcasp *mcasp, bool enable)
189{
190 u32 bit = PIN_BIT_AMUTE;
191
192 for_each_set_bit_from(bit, &mcasp->pdir, PIN_BIT_AFSR + 1) {
193 if (enable)
194 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
195 else
196 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
197 }
198}
199
200static inline void mcasp_set_axr_pdir(struct davinci_mcasp *mcasp, bool enable)
201{
202 u32 bit;
203
Peter Ujfalusi34a2a802019-07-25 11:34:23 +0300204 for_each_set_bit(bit, &mcasp->pdir, PIN_BIT_AMUTE) {
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200205 if (enable)
206 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
207 else
208 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
209 }
210}
211
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200212static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400213{
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200214 if (mcasp->rxnumevt) { /* enable FIFO */
215 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
216
217 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
218 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
219 }
220
Peter Ujfalusi44982732014-10-29 13:55:45 +0200221 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200222 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
223 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200224 /*
225 * When ASYNC == 0 the transmit and receive sections operate
226 * synchronously from the transmit clock and frame sync. We need to make
227 * sure that the TX signlas are enabled when starting reception.
228 */
229 if (mcasp_is_synchronous(mcasp)) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200230 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
231 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi34a2a802019-07-25 11:34:23 +0300232 mcasp_set_clk_pdir(mcasp, true);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200233 }
234
Peter Ujfalusi44982732014-10-29 13:55:45 +0200235 /* Activate serializer(s) */
Peter Ujfalusi1003c272018-11-16 15:41:38 +0200236 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200237 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200238 /* Release RX state machine */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200239 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200240 /* Release Frame Sync generator */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200241 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200242 if (mcasp_is_synchronous(mcasp))
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200243 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200244
245 /* enable receive IRQs */
246 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
247 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400248}
249
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200250static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400251{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400252 u32 cnt;
253
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200254 if (mcasp->txnumevt) { /* enable FIFO */
255 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
256
257 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
258 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
259 }
260
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200261 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200262 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
263 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200264 mcasp_set_clk_pdir(mcasp, true);
265
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200266 /* Activate serializer(s) */
Peter Ujfalusi1003c272018-11-16 15:41:38 +0200267 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200268 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400269
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200270 /* wait for XDATA to be cleared */
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400271 cnt = 0;
Peter Ujfalusie2a0c9f2015-12-11 13:06:24 +0200272 while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) &&
273 (cnt < 100000))
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400274 cnt++;
275
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200276 mcasp_set_axr_pdir(mcasp, true);
277
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200278 /* Release TX state machine */
279 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
280 /* Release Frame Sync generator */
281 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200282
283 /* enable transmit IRQs */
284 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
285 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400286}
287
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200288static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400289{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200290 mcasp->streams++;
291
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200292 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200293 mcasp_start_tx(mcasp);
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200294 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200295 mcasp_start_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400296}
297
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200298static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400299{
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200300 /* disable IRQ sources */
301 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
302 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
303
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200304 /*
305 * In synchronous mode stop the TX clocks if no other stream is
306 * running
307 */
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200308 if (mcasp_is_synchronous(mcasp) && !mcasp->streams) {
309 mcasp_set_clk_pdir(mcasp, false);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200310 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200311 }
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200312
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200313 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
314 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200315
316 if (mcasp->rxnumevt) { /* disable FIFO */
317 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
318
319 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
320 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400321}
322
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200323static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400324{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200325 u32 val = 0;
326
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200327 /* disable IRQ sources */
328 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
329 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
330
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200331 /*
332 * In synchronous mode keep TX clocks running if the capture stream is
333 * still running.
334 */
335 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
336 val = TXHCLKRST | TXCLKRST | TXFSRST;
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200337 else
338 mcasp_set_clk_pdir(mcasp, false);
339
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200340
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200341 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
342 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200343
344 if (mcasp->txnumevt) { /* disable FIFO */
345 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
346
347 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
348 }
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200349
350 mcasp_set_axr_pdir(mcasp, false);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400351}
352
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200353static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400354{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200355 mcasp->streams--;
356
Peter Ujfalusi03808662014-10-29 13:55:46 +0200357 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200358 mcasp_stop_tx(mcasp);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200359 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200360 mcasp_stop_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400361}
362
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200363static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
364{
365 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
366 struct snd_pcm_substream *substream;
367 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
368 u32 handled_mask = 0;
369 u32 stat;
370
371 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
372 if (stat & XUNDRN & irq_mask) {
373 dev_warn(mcasp->dev, "Transmit buffer underflow\n");
374 handled_mask |= XUNDRN;
375
376 substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
Takashi Iwaidae35d12018-07-04 16:01:43 +0200377 if (substream)
378 snd_pcm_stop_xrun(substream);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200379 }
380
381 if (!handled_mask)
382 dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
383 stat);
384
385 if (stat & XRERR)
386 handled_mask |= XRERR;
387
388 /* Ack the handled event only */
389 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
390
391 return IRQ_RETVAL(handled_mask);
392}
393
394static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
395{
396 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
397 struct snd_pcm_substream *substream;
398 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
399 u32 handled_mask = 0;
400 u32 stat;
401
402 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
403 if (stat & ROVRN & irq_mask) {
404 dev_warn(mcasp->dev, "Receive buffer overflow\n");
405 handled_mask |= ROVRN;
406
407 substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
Takashi Iwaidae35d12018-07-04 16:01:43 +0200408 if (substream)
409 snd_pcm_stop_xrun(substream);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200410 }
411
412 if (!handled_mask)
413 dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
414 stat);
415
416 if (stat & XRERR)
417 handled_mask |= XRERR;
418
419 /* Ack the handled event only */
420 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
421
422 return IRQ_RETVAL(handled_mask);
423}
424
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +0200425static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
426{
427 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
428 irqreturn_t ret = IRQ_NONE;
429
430 if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
431 ret = davinci_mcasp_tx_irq_handler(irq, data);
432
433 if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
434 ret |= davinci_mcasp_rx_irq_handler(irq, data);
435
436 return ret;
437}
438
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400439static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
440 unsigned int fmt)
441{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200442 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200443 int ret = 0;
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300444 u32 data_delay;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300445 bool fs_pol_rising;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300446 bool inv_fs = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400447
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200448 if (!fmt)
449 return 0;
450
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200451 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5296cf22012-10-04 15:08:42 +0200452 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300453 case SND_SOC_DAIFMT_DSP_A:
454 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
455 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300456 /* 1st data bit occur one ACLK cycle after the frame sync */
457 data_delay = 1;
458 break;
Daniel Mack5296cf22012-10-04 15:08:42 +0200459 case SND_SOC_DAIFMT_DSP_B:
460 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200461 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
462 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300463 /* No delay after FS */
464 data_delay = 0;
Daniel Mack5296cf22012-10-04 15:08:42 +0200465 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300466 case SND_SOC_DAIFMT_I2S:
Daniel Mack5296cf22012-10-04 15:08:42 +0200467 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200468 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
469 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300470 /* 1st data bit occur one ACLK cycle after the frame sync */
471 data_delay = 1;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300472 /* FS need to be inverted */
473 inv_fs = true;
Daniel Mack5296cf22012-10-04 15:08:42 +0200474 break;
Peter Ujfalusi816fe202019-07-25 11:34:11 +0300475 case SND_SOC_DAIFMT_RIGHT_J:
Peter Ujfalusi423761e2014-04-04 14:31:46 +0300476 case SND_SOC_DAIFMT_LEFT_J:
477 /* configure a full-word SYNC pulse (LRCLK) */
478 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
479 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
480 /* No delay after FS */
481 data_delay = 0;
482 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300483 default:
484 ret = -EINVAL;
485 goto out;
Daniel Mack5296cf22012-10-04 15:08:42 +0200486 }
487
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300488 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
489 FSXDLY(3));
490 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
491 FSRDLY(3));
492
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400493 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
494 case SND_SOC_DAIFMT_CBS_CFS:
495 /* codec is clock and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200496 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
497 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400498
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200499 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
500 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400501
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200502 /* BCLK */
503 set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
504 set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
505 /* Frame Sync */
506 set_bit(PIN_BIT_AFSX, &mcasp->pdir);
507 set_bit(PIN_BIT_AFSR, &mcasp->pdir);
508
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200509 mcasp->bclk_master = 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400510 break;
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200511 case SND_SOC_DAIFMT_CBS_CFM:
512 /* codec is clock slave and frame master */
513 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
514 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
515
516 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
517 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
518
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200519 /* BCLK */
520 set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
521 set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
522 /* Frame Sync */
523 clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
524 clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
525
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200526 mcasp->bclk_master = 1;
527 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400528 case SND_SOC_DAIFMT_CBM_CFS:
529 /* codec is clock master and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200530 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
531 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400532
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200533 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
534 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400535
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200536 /* BCLK */
537 clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
538 clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
539 /* Frame Sync */
540 set_bit(PIN_BIT_AFSX, &mcasp->pdir);
541 set_bit(PIN_BIT_AFSR, &mcasp->pdir);
542
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200543 mcasp->bclk_master = 0;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400544 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400545 case SND_SOC_DAIFMT_CBM_CFM:
546 /* codec is clock and frame master */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200547 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
548 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400549
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200550 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
551 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400552
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200553 /* BCLK */
554 clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
555 clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
556 /* Frame Sync */
557 clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
558 clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
559
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200560 mcasp->bclk_master = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400561 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400562 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200563 ret = -EINVAL;
564 goto out;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400565 }
566
567 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
568 case SND_SOC_DAIFMT_IB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200569 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300570 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300571 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400572 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400573 case SND_SOC_DAIFMT_NB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200574 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300575 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300576 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400577 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400578 case SND_SOC_DAIFMT_IB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200579 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300580 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300581 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400582 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400583 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200584 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200585 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300586 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400587 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400588 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200589 ret = -EINVAL;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300590 goto out;
591 }
592
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300593 if (inv_fs)
594 fs_pol_rising = !fs_pol_rising;
595
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300596 if (fs_pol_rising) {
597 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
598 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
599 } else {
600 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
601 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400602 }
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200603
604 mcasp->dai_fmt = fmt;
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200605out:
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200606 pm_runtime_put(mcasp->dev);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200607 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400608}
609
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300610static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp *mcasp, int div_id,
Jyri Sarha88135432014-08-06 16:47:16 +0300611 int div, bool explicit)
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200612{
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200613 pm_runtime_get_sync(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200614 switch (div_id) {
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300615 case MCASP_CLKDIV_AUXCLK: /* MCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200616 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200617 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200618 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200619 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
620 break;
621
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300622 case MCASP_CLKDIV_BCLK: /* BCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200623 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200624 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200625 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200626 ACLKRDIV(div - 1), ACLKRDIV_MASK);
Jyri Sarha88135432014-08-06 16:47:16 +0300627 if (explicit)
628 mcasp->bclk_div = div;
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200629 break;
630
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300631 case MCASP_CLKDIV_BCLK_FS_RATIO:
632 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300633 * BCLK/LRCLK ratio descries how many bit-clock cycles
634 * fit into one frame. The clock ratio is given for a
635 * full period of data (for I2S format both left and
636 * right channels), so it has to be divided by number
637 * of tdm-slots (for I2S - divided by 2).
638 * Instead of storing this ratio, we calculate a new
Randy Dunlap0d8aa2c2020-08-07 18:22:09 -0700639 * tdm_slot width by dividing the ratio by the
Jyri Sarha14a998b2015-09-17 10:39:05 +0300640 * number of configured tdm slots.
641 */
642 mcasp->slot_width = div / mcasp->tdm_slots;
643 if (div % mcasp->tdm_slots)
644 dev_warn(mcasp->dev,
645 "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
646 __func__, div, mcasp->tdm_slots);
Daniel Mack1b3bc062012-12-05 18:20:38 +0100647 break;
648
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200649 default:
650 return -EINVAL;
651 }
652
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200653 pm_runtime_put(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200654 return 0;
655}
656
Jyri Sarha88135432014-08-06 16:47:16 +0300657static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
658 int div)
659{
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300660 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
661
662 return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1);
Jyri Sarha88135432014-08-06 16:47:16 +0300663}
664
Daniel Mack5b66aa22012-10-04 15:08:41 +0200665static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
666 unsigned int freq, int dir)
667{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200668 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200669
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200670 pm_runtime_get_sync(mcasp->dev);
Peter Ujfalusi253f5842019-12-04 21:20:05 +0200671
672 if (dir == SND_SOC_CLOCK_IN) {
673 switch (clk_id) {
674 case MCASP_CLK_HCLK_AHCLK:
675 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
676 AHCLKXE);
677 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
678 AHCLKRE);
679 clear_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
680 break;
681 case MCASP_CLK_HCLK_AUXCLK:
682 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
683 AHCLKXE);
684 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
685 AHCLKRE);
686 set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
687 break;
688 default:
689 dev_err(mcasp->dev, "Invalid clk id: %d\n", clk_id);
690 goto out;
691 }
692 } else {
693 /* Select AUXCLK as HCLK */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200694 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
695 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200696 set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200697 }
Peter Ujfalusi253f5842019-12-04 21:20:05 +0200698 /*
699 * When AHCLK X/R is selected to be output it means that the HCLK is
700 * the same clock - coming via AUXCLK.
701 */
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200702 mcasp->sysclk_freq = freq;
Peter Ujfalusi253f5842019-12-04 21:20:05 +0200703out:
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200704 pm_runtime_put(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200705 return 0;
706}
707
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300708/* All serializers must have equal number of channels */
709static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream,
710 int serializers)
711{
712 struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream];
713 unsigned int *list = (unsigned int *) cl->list;
714 int slots = mcasp->tdm_slots;
715 int i, count = 0;
716
717 if (mcasp->tdm_mask[stream])
718 slots = hweight32(mcasp->tdm_mask[stream]);
719
Peter Ujfalusie4798d22017-05-11 09:58:22 +0300720 for (i = 1; i <= slots; i++)
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300721 list[count++] = i;
722
723 for (i = 2; i <= serializers; i++)
724 list[count++] = i*slots;
725
726 cl->count = count;
727
728 return 0;
729}
730
731static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp)
732{
733 int rx_serializers = 0, tx_serializers = 0, ret, i;
734
735 for (i = 0; i < mcasp->num_serializer; i++)
736 if (mcasp->serial_dir[i] == TX_MODE)
737 tx_serializers++;
738 else if (mcasp->serial_dir[i] == RX_MODE)
739 rx_serializers++;
740
741 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK,
742 tx_serializers);
743 if (ret)
744 return ret;
745
746 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE,
747 rx_serializers);
748
749 return ret;
750}
751
752
753static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai,
754 unsigned int tx_mask,
755 unsigned int rx_mask,
756 int slots, int slot_width)
757{
758 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
759
760 dev_dbg(mcasp->dev,
761 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
762 __func__, tx_mask, rx_mask, slots, slot_width);
763
764 if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
765 dev_err(mcasp->dev,
766 "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
767 tx_mask, rx_mask, slots);
768 return -EINVAL;
769 }
770
771 if (slot_width &&
772 (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) {
773 dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n",
774 __func__, slot_width);
775 return -EINVAL;
776 }
777
778 mcasp->tdm_slots = slots;
Andreas Dannenberg1bdd5932015-11-09 12:19:19 -0600779 mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
780 mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300781 mcasp->slot_width = slot_width;
782
783 return davinci_mcasp_set_ch_constraints(mcasp);
784}
785
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200786static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Jyri Sarha14a998b2015-09-17 10:39:05 +0300787 int sample_width)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400788{
Daniel Mackba764b32012-12-05 18:20:37 +0100789 u32 fmt;
Peter Ujfalusi816fe202019-07-25 11:34:11 +0300790 u32 tx_rotate, rx_rotate, slot_width;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300791 u32 mask = (1ULL << sample_width) - 1;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300792
Peter Ujfalusi816fe202019-07-25 11:34:11 +0300793 if (mcasp->slot_width)
Jyri Sarha14a998b2015-09-17 10:39:05 +0300794 slot_width = mcasp->slot_width;
Peter Ujfalusi2448c8132019-07-26 09:42:44 +0300795 else if (mcasp->max_format_width)
796 slot_width = mcasp->max_format_width;
Peter Ujfalusi816fe202019-07-25 11:34:11 +0300797 else
798 slot_width = sample_width;
799 /*
800 * TX rotation:
801 * right aligned formats: rotate w/ slot_width
802 * left aligned formats: rotate w/ sample_width
803 *
804 * RX rotation:
805 * right aligned formats: no rotation needed
806 * left aligned formats: rotate w/ (slot_width - sample_width)
807 */
808 if ((mcasp->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
809 SND_SOC_DAIFMT_RIGHT_J) {
810 tx_rotate = (slot_width / 4) & 0x7;
811 rx_rotate = 0;
812 } else {
813 tx_rotate = (sample_width / 4) & 0x7;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300814 rx_rotate = (slot_width - sample_width) / 4;
Peter Ujfalusid742b922014-11-10 12:32:19 +0200815 }
Daniel Mack1b3bc062012-12-05 18:20:38 +0100816
Daniel Mackba764b32012-12-05 18:20:37 +0100817 /* mapping of the XSSZ bit-field as described in the datasheet */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300818 fmt = (slot_width >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400819
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200820 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200821 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
822 RXSSZ(0x0F));
823 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
824 TXSSZ(0x0F));
825 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
826 TXROT(7));
827 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
828 RXROT(7));
829 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200830 }
831
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200832 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400833
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400834 return 0;
835}
836
Peter Ujfalusi662ffae2014-01-30 15:15:22 +0200837static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300838 int period_words, int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400839{
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300840 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400841 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400842 u8 tx_ser = 0;
843 u8 rx_ser = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200844 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100845 u8 max_active_serializers = (channels + slots - 1) / slots;
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300846 u8 max_rx_serializers, max_tx_serializers;
Peter Ujfalusi72383192015-09-14 16:06:48 +0300847 int active_serializers, numevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +0200848 u32 reg;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400849 /* Default configuration */
Peter Ujfalusi40448e52014-04-04 15:56:30 +0300850 if (mcasp->version < MCASP_VERSION_3)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200851 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400852
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400853 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200854 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
855 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300856 max_tx_serializers = max_active_serializers;
857 max_rx_serializers =
858 mcasp->active_serializers[SNDRV_PCM_STREAM_CAPTURE];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400859 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200860 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
861 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300862 max_tx_serializers =
863 mcasp->active_serializers[SNDRV_PCM_STREAM_PLAYBACK];
864 max_rx_serializers = max_active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400865 }
866
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200867 for (i = 0; i < mcasp->num_serializer; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200868 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
869 mcasp->serial_dir[i]);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200870 if (mcasp->serial_dir[i] == TX_MODE &&
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300871 tx_ser < max_tx_serializers) {
Misael Lopez Cruz19db62e2015-06-08 16:03:47 +0300872 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
Peter Ujfalusibc184542018-11-16 15:41:41 +0200873 mcasp->dismod, DISMOD_MASK);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200874 set_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400875 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200876 } else if (mcasp->serial_dir[i] == RX_MODE &&
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300877 rx_ser < max_rx_serializers) {
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200878 clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400879 rx_ser++;
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +0300880 } else {
881 /* Inactive or unused pin, set it to inactive */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200882 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
883 SRMOD_INACTIVE, SRMOD_MASK);
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +0300884 /* If unused, set DISMOD for the pin */
885 if (mcasp->serial_dir[i] != INACTIVE_MODE)
886 mcasp_mod_bits(mcasp,
887 DAVINCI_MCASP_XRSRCTL_REG(i),
888 mcasp->dismod, DISMOD_MASK);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200889 clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400890 }
891 }
892
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300893 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
894 active_serializers = tx_ser;
895 numevt = mcasp->txnumevt;
896 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
897 } else {
898 active_serializers = rx_ser;
899 numevt = mcasp->rxnumevt;
900 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
901 }
Daniel Mackecf327c2013-03-08 14:19:38 +0100902
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300903 if (active_serializers < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200904 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300905 "enabled in mcasp (%d)\n", channels,
906 active_serializers * slots);
Daniel Mackecf327c2013-03-08 14:19:38 +0100907 return -EINVAL;
908 }
909
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300910 /* AFIFO is not in use */
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300911 if (!numevt) {
912 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300913 if (active_serializers > 1) {
914 /*
915 * If more than one serializers are in use we have one
916 * DMA request to provide data for all serializers.
917 * For example if three serializers are enabled the DMA
918 * need to transfer three words per DMA request.
919 */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300920 dma_data->maxburst = active_serializers;
921 } else {
Peter Ujfalusi33445642014-04-01 15:55:12 +0300922 dma_data->maxburst = 0;
923 }
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300924
925 goto out;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300926 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400927
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300928 if (period_words % active_serializers) {
929 dev_err(mcasp->dev, "Invalid combination of period words and "
930 "active serializers: %d, %d\n", period_words,
931 active_serializers);
932 return -EINVAL;
933 }
934
935 /*
936 * Calculate the optimal AFIFO depth for platform side:
937 * The number of words for numevt need to be in steps of active
938 * serializers.
939 */
Peter Ujfalusi72383192015-09-14 16:06:48 +0300940 numevt = (numevt / active_serializers) * active_serializers;
941
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300942 while (period_words % numevt && numevt > 0)
943 numevt -= active_serializers;
944 if (numevt <= 0)
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300945 numevt = active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400946
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300947 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
948 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
Michal Bachraty2952b272013-02-28 16:07:08 +0100949
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300950 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300951 if (numevt == 1)
952 numevt = 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300953 dma_data->maxburst = numevt;
954
Peter Ujfalusib7989e22019-07-25 11:34:32 +0300955out:
956 mcasp->active_serializers[stream] = active_serializers;
957
Michal Bachraty2952b272013-02-28 16:07:08 +0100958 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400959}
960
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200961static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
962 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400963{
964 int i, active_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200965 int total_slots;
966 int active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400967 u32 mask = 0;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200968 u32 busel = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400969
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200970 total_slots = mcasp->tdm_slots;
971
972 /*
973 * If more than one serializer is needed, then use them with
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300974 * all the specified tdm_slots. Otherwise, one serializer can
975 * cope with the transaction using just as many slots as there
976 * are channels in the stream.
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200977 */
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300978 if (mcasp->tdm_mask[stream]) {
979 active_slots = hweight32(mcasp->tdm_mask[stream]);
980 active_serializers = (channels + active_slots - 1) /
981 active_slots;
Peter Ujfalusifd14f442019-06-20 12:20:02 +0300982 if (active_serializers == 1)
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300983 active_slots = channels;
Peter Ujfalusifd14f442019-06-20 12:20:02 +0300984 for (i = 0; i < total_slots; i++) {
985 if ((1 << i) & mcasp->tdm_mask[stream]) {
986 mask |= (1 << i);
987 if (--active_slots <= 0)
988 break;
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300989 }
990 }
991 } else {
992 active_serializers = (channels + total_slots - 1) / total_slots;
993 if (active_serializers == 1)
994 active_slots = channels;
995 else
996 active_slots = total_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200997
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300998 for (i = 0; i < active_slots; i++)
999 mask |= (1 << i);
1000 }
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +03001001
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001002 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -04001003
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001004 if (!mcasp->dat_port)
1005 busel = TXSEL;
1006
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001007 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1008 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
1009 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
1010 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
1011 FSXMOD(total_slots), FSXMOD(0x1FF));
1012 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
1013 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
1014 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
1015 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
1016 FSRMOD(total_slots), FSRMOD(0x1FF));
Peter Ujfalusi0ad7d3a2015-11-23 12:51:53 +02001017 /*
1018 * If McASP is set to be TX/RX synchronous and the playback is
1019 * not running already we need to configure the TX slots in
1020 * order to have correct FSX on the bus
1021 */
1022 if (mcasp_is_synchronous(mcasp) && !mcasp->channels)
1023 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
1024 FSXMOD(total_slots), FSXMOD(0x1FF));
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001025 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001026
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001027 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001028}
1029
1030/* S/PDIF */
Daniel Mack64792852014-03-27 11:27:40 +01001031static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
1032 unsigned int rate)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001033{
Daniel Mack64792852014-03-27 11:27:40 +01001034 u32 cs_value = 0;
1035 u8 *cs_bytes = (u8*) &cs_value;
1036
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001037 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
1038 and LSB first */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001039 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001040
1041 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001042 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001043
1044 /* Set the TX tdm : for all the slots */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001045 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001046
1047 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001048 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001049
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001050 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001051
1052 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001053 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001054
1055 /* Enable the DIT */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001056 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001057
Daniel Mack64792852014-03-27 11:27:40 +01001058 /* Set S/PDIF channel status bits */
1059 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
1060 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
1061
1062 switch (rate) {
1063 case 22050:
1064 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
1065 break;
1066 case 24000:
1067 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
1068 break;
1069 case 32000:
1070 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
1071 break;
1072 case 44100:
1073 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
1074 break;
1075 case 48000:
1076 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
1077 break;
1078 case 88200:
1079 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
1080 break;
1081 case 96000:
1082 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
1083 break;
1084 case 176400:
1085 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
1086 break;
1087 case 192000:
1088 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
1089 break;
1090 default:
1091 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
1092 return -EINVAL;
1093 }
1094
1095 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
1096 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
1097
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001098 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001099}
1100
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001101static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001102 unsigned int sysclk_freq,
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001103 unsigned int bclk_freq, bool set)
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001104{
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001105 u32 reg = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG);
1106 int div = sysclk_freq / bclk_freq;
1107 int rem = sysclk_freq % bclk_freq;
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001108 int error_ppm;
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001109 int aux_div = 1;
1110
1111 if (div > (ACLKXDIV_MASK + 1)) {
1112 if (reg & AHCLKXE) {
1113 aux_div = div / (ACLKXDIV_MASK + 1);
1114 if (div % (ACLKXDIV_MASK + 1))
1115 aux_div++;
1116
1117 sysclk_freq /= aux_div;
1118 div = sysclk_freq / bclk_freq;
1119 rem = sysclk_freq % bclk_freq;
1120 } else if (set) {
1121 dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
1122 sysclk_freq);
1123 }
1124 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001125
1126 if (rem != 0) {
1127 if (div == 0 ||
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001128 ((sysclk_freq / div) - bclk_freq) >
1129 (bclk_freq - (sysclk_freq / (div+1)))) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001130 div++;
1131 rem = rem - bclk_freq;
1132 }
1133 }
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001134 error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem,
1135 (int)bclk_freq)) / div - 1000000;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001136
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001137 if (set) {
1138 if (error_ppm)
1139 dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n",
1140 error_ppm);
1141
1142 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_BCLK, div, 0);
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001143 if (reg & AHCLKXE)
1144 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK,
1145 aux_div, 0);
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001146 }
1147
1148 return error_ppm;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001149}
1150
Peter Ujfalusi5fcb4572018-08-31 11:24:56 +03001151static inline u32 davinci_mcasp_tx_delay(struct davinci_mcasp *mcasp)
1152{
1153 if (!mcasp->txnumevt)
1154 return 0;
1155
1156 return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_WFIFOSTS_OFFSET);
1157}
1158
1159static inline u32 davinci_mcasp_rx_delay(struct davinci_mcasp *mcasp)
1160{
1161 if (!mcasp->rxnumevt)
1162 return 0;
1163
1164 return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_RFIFOSTS_OFFSET);
1165}
1166
1167static snd_pcm_sframes_t davinci_mcasp_delay(
1168 struct snd_pcm_substream *substream,
1169 struct snd_soc_dai *cpu_dai)
1170{
1171 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1172 u32 fifo_use;
1173
1174 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1175 fifo_use = davinci_mcasp_tx_delay(mcasp);
1176 else
1177 fifo_use = davinci_mcasp_rx_delay(mcasp);
1178
1179 /*
1180 * Divide the used locations with the channel count to get the
1181 * FIFO usage in samples (don't care about partial samples in the
1182 * buffer).
1183 */
1184 return fifo_use / substream->runtime->channels;
1185}
1186
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001187static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
1188 struct snd_pcm_hw_params *params,
1189 struct snd_soc_dai *cpu_dai)
1190{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001191 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001192 int word_length;
Peter Ujfalusia7e46bd2014-02-03 14:51:50 +02001193 int channels = params_channels(params);
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001194 int period_size = params_period_size(params);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001195 int ret;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001196
Peter Ujfalusib7989e22019-07-25 11:34:32 +03001197 switch (params_format(params)) {
1198 case SNDRV_PCM_FORMAT_U8:
1199 case SNDRV_PCM_FORMAT_S8:
1200 word_length = 8;
1201 break;
1202
1203 case SNDRV_PCM_FORMAT_U16_LE:
1204 case SNDRV_PCM_FORMAT_S16_LE:
1205 word_length = 16;
1206 break;
1207
1208 case SNDRV_PCM_FORMAT_U24_3LE:
1209 case SNDRV_PCM_FORMAT_S24_3LE:
1210 word_length = 24;
1211 break;
1212
1213 case SNDRV_PCM_FORMAT_U24_LE:
1214 case SNDRV_PCM_FORMAT_S24_LE:
1215 word_length = 24;
1216 break;
1217
1218 case SNDRV_PCM_FORMAT_U32_LE:
1219 case SNDRV_PCM_FORMAT_S32_LE:
1220 word_length = 32;
1221 break;
1222
1223 default:
1224 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
1225 return -EINVAL;
1226 }
1227
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +02001228 ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt);
1229 if (ret)
1230 return ret;
1231
Daniel Mack82675252014-07-16 14:04:41 +02001232 /*
1233 * If mcasp is BCLK master, and a BCLK divider was not provided by
1234 * the machine driver, we need to calculate the ratio.
1235 */
1236 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarha1f114f72015-04-23 16:16:04 +03001237 int slots = mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001238 int rate = params_rate(params);
1239 int sbits = params_width(params);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001240
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001241 if (mcasp->slot_width)
1242 sbits = mcasp->slot_width;
1243
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001244 davinci_mcasp_calc_clk_div(mcasp, mcasp->sysclk_freq,
1245 rate * sbits * slots, true);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001246 }
1247
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001248 ret = mcasp_common_hw_param(mcasp, substream->stream,
1249 period_size * channels, channels);
Peter Ujfalusi0f7d9a62014-01-30 15:15:24 +02001250 if (ret)
1251 return ret;
1252
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001253 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
Daniel Mack64792852014-03-27 11:27:40 +01001254 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001255 else
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +02001256 ret = mcasp_i2s_hw_param(mcasp, substream->stream,
1257 channels);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001258
1259 if (ret)
1260 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001261
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001262 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001263
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001264 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001265 mcasp->channels = channels;
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001266 if (!mcasp->max_format_width)
1267 mcasp->max_format_width = word_length;
1268 }
Peter Ujfalusi11277832014-11-10 12:32:16 +02001269
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001270 return 0;
1271}
1272
1273static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
1274 int cmd, struct snd_soc_dai *cpu_dai)
1275{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001276 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001277 int ret = 0;
1278
1279 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001280 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +05301281 case SNDRV_PCM_TRIGGER_START:
1282 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001283 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001284 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001285 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +05301286 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001287 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001288 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001289 break;
1290
1291 default:
1292 ret = -EINVAL;
1293 }
1294
1295 return ret;
1296}
1297
Peter Ujfalusi1e112c32019-07-26 09:42:43 +03001298static int davinci_mcasp_hw_rule_slot_width(struct snd_pcm_hw_params *params,
1299 struct snd_pcm_hw_rule *rule)
1300{
1301 struct davinci_mcasp_ruledata *rd = rule->private;
1302 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1303 struct snd_mask nfmt;
1304 int i, slot_width;
1305
1306 snd_mask_none(&nfmt);
1307 slot_width = rd->mcasp->slot_width;
1308
1309 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1310 if (snd_mask_test(fmt, i)) {
1311 if (snd_pcm_format_width(i) <= slot_width) {
1312 snd_mask_set(&nfmt, i);
1313 }
1314 }
1315 }
1316
1317 return snd_mask_refine(fmt, &nfmt);
1318}
1319
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001320static int davinci_mcasp_hw_rule_format_width(struct snd_pcm_hw_params *params,
1321 struct snd_pcm_hw_rule *rule)
1322{
1323 struct davinci_mcasp_ruledata *rd = rule->private;
1324 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1325 struct snd_mask nfmt;
1326 int i, format_width;
1327
1328 snd_mask_none(&nfmt);
1329 format_width = rd->mcasp->max_format_width;
1330
1331 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1332 if (snd_mask_test(fmt, i)) {
1333 if (snd_pcm_format_width(i) == format_width) {
1334 snd_mask_set(&nfmt, i);
1335 }
1336 }
1337 }
1338
1339 return snd_mask_refine(fmt, &nfmt);
1340}
1341
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001342static const unsigned int davinci_mcasp_dai_rates[] = {
1343 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1344 88200, 96000, 176400, 192000,
1345};
1346
1347#define DAVINCI_MAX_RATE_ERROR_PPM 1000
1348
1349static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params,
1350 struct snd_pcm_hw_rule *rule)
1351{
1352 struct davinci_mcasp_ruledata *rd = rule->private;
1353 struct snd_interval *ri =
1354 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1355 int sbits = params_width(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001356 int slots = rd->mcasp->tdm_slots;
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001357 struct snd_interval range;
1358 int i;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001359
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001360 if (rd->mcasp->slot_width)
1361 sbits = rd->mcasp->slot_width;
1362
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001363 snd_interval_any(&range);
1364 range.empty = 1;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001365
1366 for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) {
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001367 if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) {
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001368 uint bclk_freq = sbits * slots *
1369 davinci_mcasp_dai_rates[i];
1370 unsigned int sysclk_freq;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001371 int ppm;
1372
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001373 if (rd->mcasp->auxclk_fs_ratio)
1374 sysclk_freq = davinci_mcasp_dai_rates[i] *
1375 rd->mcasp->auxclk_fs_ratio;
1376 else
1377 sysclk_freq = rd->mcasp->sysclk_freq;
1378
1379 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
1380 bclk_freq, false);
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001381 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1382 if (range.empty) {
1383 range.min = davinci_mcasp_dai_rates[i];
1384 range.empty = 0;
1385 }
1386 range.max = davinci_mcasp_dai_rates[i];
1387 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001388 }
1389 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001390
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001391 dev_dbg(rd->mcasp->dev,
1392 "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1393 ri->min, ri->max, range.min, range.max, sbits, slots);
1394
1395 return snd_interval_refine(hw_param_interval(params, rule->var),
1396 &range);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001397}
1398
1399static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params,
1400 struct snd_pcm_hw_rule *rule)
1401{
1402 struct davinci_mcasp_ruledata *rd = rule->private;
1403 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1404 struct snd_mask nfmt;
1405 int rate = params_rate(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001406 int slots = rd->mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001407 int i, count = 0;
1408
1409 snd_mask_none(&nfmt);
1410
Peter Ujfalusi9be072a2016-09-01 10:05:12 +03001411 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001412 if (snd_mask_test(fmt, i)) {
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001413 uint sbits = snd_pcm_format_width(i);
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001414 unsigned int sysclk_freq;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001415 int ppm;
1416
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001417 if (rd->mcasp->auxclk_fs_ratio)
1418 sysclk_freq = rate *
1419 rd->mcasp->auxclk_fs_ratio;
1420 else
1421 sysclk_freq = rd->mcasp->sysclk_freq;
1422
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001423 if (rd->mcasp->slot_width)
1424 sbits = rd->mcasp->slot_width;
1425
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001426 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001427 sbits * slots * rate,
1428 false);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001429 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1430 snd_mask_set(&nfmt, i);
1431 count++;
1432 }
1433 }
1434 }
1435 dev_dbg(rd->mcasp->dev,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001436 "%d possible sample format for %d Hz and %d tdm slots\n",
1437 count, rate, slots);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001438
1439 return snd_mask_refine(fmt, &nfmt);
1440}
1441
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001442static int davinci_mcasp_hw_rule_min_periodsize(
1443 struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1444{
1445 struct snd_interval *period_size = hw_param_interval(params,
1446 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1447 struct snd_interval frames;
1448
1449 snd_interval_any(&frames);
1450 frames.min = 64;
1451 frames.integer = 1;
1452
1453 return snd_interval_refine(period_size, &frames);
1454}
1455
Peter Ujfalusi11277832014-11-10 12:32:16 +02001456static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
1457 struct snd_soc_dai *cpu_dai)
1458{
1459 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001460 struct davinci_mcasp_ruledata *ruledata =
1461 &mcasp->ruledata[substream->stream];
Peter Ujfalusi11277832014-11-10 12:32:16 +02001462 u32 max_channels = 0;
Peter Ujfalusi1e112c32019-07-26 09:42:43 +03001463 int i, dir, ret;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001464 int tdm_slots = mcasp->tdm_slots;
1465
Peter Ujfalusi19357362016-05-09 13:39:14 +03001466 /* Do not allow more then one stream per direction */
1467 if (mcasp->substreams[substream->stream])
1468 return -EBUSY;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001469
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001470 mcasp->substreams[substream->stream] = substream;
1471
Peter Ujfalusi19357362016-05-09 13:39:14 +03001472 if (mcasp->tdm_mask[substream->stream])
1473 tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]);
1474
Peter Ujfalusi11277832014-11-10 12:32:16 +02001475 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1476 return 0;
1477
1478 /*
1479 * Limit the maximum allowed channels for the first stream:
1480 * number of serializers for the direction * tdm slots per serializer
1481 */
1482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1483 dir = TX_MODE;
1484 else
1485 dir = RX_MODE;
1486
1487 for (i = 0; i < mcasp->num_serializer; i++) {
1488 if (mcasp->serial_dir[i] == dir)
1489 max_channels++;
1490 }
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001491 ruledata->serializers = max_channels;
Peter Ujfalusi1e112c32019-07-26 09:42:43 +03001492 ruledata->mcasp = mcasp;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001493 max_channels *= tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001494 /*
1495 * If the already active stream has less channels than the calculated
Peter Ujfalusib7989e22019-07-25 11:34:32 +03001496 * limit based on the seirializers * tdm_slots, and only one serializer
1497 * is in use we need to use that as a constraint for the second stream.
1498 * Otherwise (first stream or less allowed channels or more than one
1499 * serializer in use) we use the calculated constraint.
Peter Ujfalusi11277832014-11-10 12:32:16 +02001500 */
Peter Ujfalusib7989e22019-07-25 11:34:32 +03001501 if (mcasp->channels && mcasp->channels < max_channels &&
1502 ruledata->serializers == 1)
Peter Ujfalusi11277832014-11-10 12:32:16 +02001503 max_channels = mcasp->channels;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001504 /*
1505 * But we can always allow channels upto the amount of
1506 * the available tdm_slots.
1507 */
1508 if (max_channels < tdm_slots)
1509 max_channels = tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001510
1511 snd_pcm_hw_constraint_minmax(substream->runtime,
1512 SNDRV_PCM_HW_PARAM_CHANNELS,
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001513 0, max_channels);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001514
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001515 snd_pcm_hw_constraint_list(substream->runtime,
1516 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1517 &mcasp->chconstr[substream->stream]);
1518
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001519 if (mcasp->max_format_width) {
1520 /*
1521 * Only allow formats which require same amount of bits on the
1522 * bus as the currently running stream
1523 */
1524 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1525 SNDRV_PCM_HW_PARAM_FORMAT,
1526 davinci_mcasp_hw_rule_format_width,
1527 ruledata,
1528 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1529 if (ret)
1530 return ret;
1531 }
1532 else if (mcasp->slot_width) {
Peter Ujfalusi1e112c32019-07-26 09:42:43 +03001533 /* Only allow formats require <= slot_width bits on the bus */
1534 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1535 SNDRV_PCM_HW_PARAM_FORMAT,
1536 davinci_mcasp_hw_rule_slot_width,
1537 ruledata,
1538 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1539 if (ret)
1540 return ret;
1541 }
Jyri Sarha5935a052015-04-23 16:16:05 +03001542
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001543 /*
1544 * If we rely on implicit BCLK divider setting we should
1545 * set constraints based on what we can provide.
1546 */
1547 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001548 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1549 SNDRV_PCM_HW_PARAM_RATE,
1550 davinci_mcasp_hw_rule_rate,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001551 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001552 SNDRV_PCM_HW_PARAM_FORMAT, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001553 if (ret)
1554 return ret;
1555 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1556 SNDRV_PCM_HW_PARAM_FORMAT,
1557 davinci_mcasp_hw_rule_format,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001558 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001559 SNDRV_PCM_HW_PARAM_RATE, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001560 if (ret)
1561 return ret;
1562 }
1563
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001564 snd_pcm_hw_rule_add(substream->runtime, 0,
1565 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1566 davinci_mcasp_hw_rule_min_periodsize, NULL,
1567 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1568
Peter Ujfalusi11277832014-11-10 12:32:16 +02001569 return 0;
1570}
1571
1572static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1573 struct snd_soc_dai *cpu_dai)
1574{
1575 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1576
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001577 mcasp->substreams[substream->stream] = NULL;
Peter Ujfalusib7989e22019-07-25 11:34:32 +03001578 mcasp->active_serializers[substream->stream] = 0;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001579
Peter Ujfalusi11277832014-11-10 12:32:16 +02001580 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1581 return;
1582
Kuninori Morimoto36ad1a82020-05-15 09:47:48 +09001583 if (!snd_soc_dai_active(cpu_dai)) {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001584 mcasp->channels = 0;
Peter Ujfalusi2448c8132019-07-26 09:42:44 +03001585 mcasp->max_format_width = 0;
1586 }
Peter Ujfalusi11277832014-11-10 12:32:16 +02001587}
1588
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001589static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001590 .startup = davinci_mcasp_startup,
1591 .shutdown = davinci_mcasp_shutdown,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001592 .trigger = davinci_mcasp_trigger,
Peter Ujfalusi5fcb4572018-08-31 11:24:56 +03001593 .delay = davinci_mcasp_delay,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001594 .hw_params = davinci_mcasp_hw_params,
1595 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +02001596 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +02001597 .set_sysclk = davinci_mcasp_set_sysclk,
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001598 .set_tdm_slot = davinci_mcasp_set_tdm_slot,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001599};
1600
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001601static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1602{
1603 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1604
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001605 dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1606 dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001607
1608 return 0;
1609}
1610
Peter Ujfalusied29cd52013-11-14 11:35:22 +02001611#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1612
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001613#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1614 SNDRV_PCM_FMTBIT_U8 | \
1615 SNDRV_PCM_FMTBIT_S16_LE | \
1616 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +02001617 SNDRV_PCM_FMTBIT_S24_LE | \
1618 SNDRV_PCM_FMTBIT_U24_LE | \
1619 SNDRV_PCM_FMTBIT_S24_3LE | \
1620 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001621 SNDRV_PCM_FMTBIT_S32_LE | \
1622 SNDRV_PCM_FMTBIT_U32_LE)
1623
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001624static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001625 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001626 .name = "davinci-mcasp.0",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001627 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001628 .playback = {
Peter Ujfalusief3ab252020-06-30 15:58:41 +03001629 .stream_name = "IIS Playback",
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001630 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001631 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001632 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001633 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001634 },
1635 .capture = {
Peter Ujfalusief3ab252020-06-30 15:58:41 +03001636 .stream_name = "IIS Capture",
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001637 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001638 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001639 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001640 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001641 },
1642 .ops = &davinci_mcasp_dai_ops,
1643
Jyri Sarha295c3402015-09-09 21:27:42 +03001644 .symmetric_rates = 1,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001645 },
1646 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +02001647 .name = "davinci-mcasp.1",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001648 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001649 .playback = {
Peter Ujfalusief3ab252020-06-30 15:58:41 +03001650 .stream_name = "DIT Playback",
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001651 .channels_min = 1,
1652 .channels_max = 384,
1653 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001654 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001655 },
1656 .ops = &davinci_mcasp_dai_ops,
1657 },
1658
1659};
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001660
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001661static const struct snd_soc_component_driver davinci_mcasp_component = {
1662 .name = "davinci-mcasp",
1663};
1664
Jyri Sarha256ba182013-10-18 18:37:42 +03001665/* Some HW specific values and defaults. The rest is filled in from DT. */
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001666static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001667 .tx_dma_offset = 0x400,
1668 .rx_dma_offset = 0x400,
Jyri Sarha256ba182013-10-18 18:37:42 +03001669 .version = MCASP_VERSION_1,
1670};
1671
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001672static struct davinci_mcasp_pdata da830_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001673 .tx_dma_offset = 0x2000,
1674 .rx_dma_offset = 0x2000,
Jyri Sarha256ba182013-10-18 18:37:42 +03001675 .version = MCASP_VERSION_2,
1676};
1677
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001678static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001679 .tx_dma_offset = 0,
1680 .rx_dma_offset = 0,
Jyri Sarha256ba182013-10-18 18:37:42 +03001681 .version = MCASP_VERSION_3,
1682};
1683
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001684static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001685 /* The CFG port offset will be calculated if it is needed */
1686 .tx_dma_offset = 0,
1687 .rx_dma_offset = 0,
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001688 .version = MCASP_VERSION_4,
1689};
1690
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301691static const struct of_device_id mcasp_dt_ids[] = {
1692 {
1693 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001694 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301695 },
1696 {
1697 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001698 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301699 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301700 {
Jyri Sarha3af9e032013-10-18 18:37:44 +03001701 .compatible = "ti,am33xx-mcasp-audio",
Peter Ujfalusib14899d2013-11-14 11:35:37 +02001702 .data = &am33xx_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301703 },
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001704 {
1705 .compatible = "ti,dra7-mcasp-audio",
1706 .data = &dra7_mcasp_pdata,
1707 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301708 { /* sentinel */ }
1709};
1710MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1711
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001712static int mcasp_reparent_fck(struct platform_device *pdev)
1713{
1714 struct device_node *node = pdev->dev.of_node;
1715 struct clk *gfclk, *parent_clk;
1716 const char *parent_name;
1717 int ret;
1718
1719 if (!node)
1720 return 0;
1721
1722 parent_name = of_get_property(node, "fck_parent", NULL);
1723 if (!parent_name)
1724 return 0;
1725
Peter Ujfalusic6702542016-01-27 15:02:49 +02001726 dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n");
1727
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001728 gfclk = clk_get(&pdev->dev, "fck");
1729 if (IS_ERR(gfclk)) {
1730 dev_err(&pdev->dev, "failed to get fck\n");
1731 return PTR_ERR(gfclk);
1732 }
1733
1734 parent_clk = clk_get(NULL, parent_name);
1735 if (IS_ERR(parent_clk)) {
1736 dev_err(&pdev->dev, "failed to get parent clock\n");
1737 ret = PTR_ERR(parent_clk);
1738 goto err1;
1739 }
1740
1741 ret = clk_set_parent(gfclk, parent_clk);
1742 if (ret) {
1743 dev_err(&pdev->dev, "failed to reparent fck\n");
1744 goto err2;
1745 }
1746
1747err2:
1748 clk_put(parent_clk);
1749err1:
1750 clk_put(gfclk);
1751 return ret;
1752}
1753
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02001754static bool davinci_mcasp_have_gpiochip(struct davinci_mcasp *mcasp)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301755{
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02001756#ifdef CONFIG_OF_GPIO
1757 if (mcasp->dev->of_node &&
1758 of_property_read_bool(mcasp->dev->of_node, "gpio-controller"))
1759 return true;
1760#endif
1761
1762 return false;
1763}
1764
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001765static int davinci_mcasp_get_config(struct davinci_mcasp *mcasp,
1766 struct platform_device *pdev)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301767{
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001768 const struct of_device_id *match = of_match_device(mcasp_dt_ids, &pdev->dev);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301769 struct device_node *np = pdev->dev.of_node;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001770 struct davinci_mcasp_pdata *pdata = NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301771 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301772 u32 val;
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001773 int i;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301774
1775 if (pdev->dev.platform_data) {
1776 pdata = pdev->dev.platform_data;
Peter Ujfalusibc184542018-11-16 15:41:41 +02001777 pdata->dismod = DISMOD_LOW;
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001778 goto out;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301779 } else if (match) {
Peter Ujfalusi272ee032016-06-02 12:55:24 +03001780 pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata),
1781 GFP_KERNEL);
Colin Ian Kingf4d95de2020-02-10 09:24:22 +00001782 if (!pdata)
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001783 return -ENOMEM;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301784 } else {
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001785 dev_err(&pdev->dev, "No compatible match found\n");
1786 return -EINVAL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301787 }
1788
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02001789 if (of_property_read_u32(np, "op-mode", &val) == 0) {
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301790 pdata->op_mode = val;
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02001791 } else {
1792 mcasp->missing_audio_param = true;
1793 goto out;
1794 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301795
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001796 if (of_property_read_u32(np, "tdm-slots", &val) == 0) {
Michal Bachraty2952b272013-02-28 16:07:08 +01001797 if (val < 2 || val > 32) {
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001798 dev_err(&pdev->dev, "tdm-slots must be in rage [2-32]\n");
1799 return -EINVAL;
Michal Bachraty2952b272013-02-28 16:07:08 +01001800 }
1801
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301802 pdata->tdm_slots = val;
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02001803 } else if (pdata->op_mode == DAVINCI_MCASP_IIS_MODE) {
1804 mcasp->missing_audio_param = true;
1805 goto out;
Michal Bachraty2952b272013-02-28 16:07:08 +01001806 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301807
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301808 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1809 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301810 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001811 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1812 (sizeof(*of_serial_dir) * val),
1813 GFP_KERNEL);
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001814 if (!of_serial_dir)
1815 return -ENOMEM;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301816
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001817 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301818 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1819
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001820 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301821 pdata->serial_dir = of_serial_dir;
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02001822 } else {
1823 mcasp->missing_audio_param = true;
1824 goto out;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301825 }
1826
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001827 if (of_property_read_u32(np, "tx-num-evt", &val) == 0)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301828 pdata->txnumevt = val;
1829
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001830 if (of_property_read_u32(np, "rx-num-evt", &val) == 0)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301831 pdata->rxnumevt = val;
1832
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001833 if (of_property_read_u32(np, "auxclk-fs-ratio", &val) == 0)
1834 mcasp->auxclk_fs_ratio = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301835
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001836 if (of_property_read_u32(np, "dismod", &val) == 0) {
Peter Ujfalusibc184542018-11-16 15:41:41 +02001837 if (val == 0 || val == 2 || val == 3) {
1838 pdata->dismod = DISMOD_VAL(val);
1839 } else {
1840 dev_warn(&pdev->dev, "Invalid dismod value: %u\n", val);
1841 pdata->dismod = DISMOD_LOW;
1842 }
1843 } else {
1844 pdata->dismod = DISMOD_LOW;
1845 }
1846
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001847out:
1848 mcasp->pdata = pdata;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301849
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02001850 if (mcasp->missing_audio_param) {
1851 if (davinci_mcasp_have_gpiochip(mcasp)) {
1852 dev_dbg(&pdev->dev, "Missing DT parameter(s) for audio\n");
1853 return 0;
1854 }
1855
1856 dev_err(&pdev->dev, "Insufficient DT parameter(s)\n");
1857 return -ENODEV;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301858 }
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02001859
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001860 mcasp->op_mode = pdata->op_mode;
1861 /* sanity check for tdm slots parameter */
1862 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1863 if (pdata->tdm_slots < 2) {
1864 dev_warn(&pdev->dev, "invalid tdm slots: %d\n",
1865 pdata->tdm_slots);
1866 mcasp->tdm_slots = 2;
1867 } else if (pdata->tdm_slots > 32) {
1868 dev_warn(&pdev->dev, "invalid tdm slots: %d\n",
1869 pdata->tdm_slots);
1870 mcasp->tdm_slots = 32;
1871 } else {
1872 mcasp->tdm_slots = pdata->tdm_slots;
1873 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301874 }
Peter Ujfalusi1125d922020-11-06 09:25:50 +02001875
1876 mcasp->num_serializer = pdata->num_serializer;
1877#ifdef CONFIG_PM
1878 mcasp->context.xrsr_regs = devm_kcalloc(&pdev->dev,
1879 mcasp->num_serializer, sizeof(u32),
1880 GFP_KERNEL);
1881 if (!mcasp->context.xrsr_regs)
1882 return -ENOMEM;
1883#endif
1884 mcasp->serial_dir = pdata->serial_dir;
1885 mcasp->version = pdata->version;
1886 mcasp->txnumevt = pdata->txnumevt;
1887 mcasp->rxnumevt = pdata->rxnumevt;
1888 mcasp->dismod = pdata->dismod;
1889
1890 return 0;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301891}
1892
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001893enum {
1894 PCM_EDMA,
1895 PCM_SDMA,
Peter Ujfalusifb0c3c62020-02-10 16:09:50 +02001896 PCM_UDMA,
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001897};
1898static const char *sdma_prefix = "ti,omap";
1899
1900static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp)
1901{
1902 struct dma_chan *chan;
1903 const char *tmp;
1904 int ret = PCM_EDMA;
1905
1906 if (!mcasp->dev->of_node)
1907 return PCM_EDMA;
1908
1909 tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data;
Peter Ujfalusif0c97132019-11-13 11:54:45 +02001910 chan = dma_request_chan(mcasp->dev, tmp);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001911 if (IS_ERR(chan)) {
1912 if (PTR_ERR(chan) != -EPROBE_DEFER)
1913 dev_err(mcasp->dev,
1914 "Can't verify DMA configuration (%ld)\n",
1915 PTR_ERR(chan));
1916 return PTR_ERR(chan);
1917 }
Xiyu Yanga697ae62020-04-25 20:48:35 +08001918 if (WARN_ON(!chan->device || !chan->device->dev)) {
1919 dma_release_channel(chan);
Takashi Iwaibefff4f2017-09-07 10:59:17 +02001920 return -EINVAL;
Xiyu Yanga697ae62020-04-25 20:48:35 +08001921 }
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001922
1923 if (chan->device->dev->of_node)
1924 ret = of_property_read_string(chan->device->dev->of_node,
1925 "compatible", &tmp);
1926 else
1927 dev_dbg(mcasp->dev, "DMA controller has no of-node\n");
1928
1929 dma_release_channel(chan);
1930 if (ret)
1931 return ret;
1932
1933 dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp);
1934 if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix)))
1935 return PCM_SDMA;
Peter Ujfalusifb0c3c62020-02-10 16:09:50 +02001936 else if (strstr(tmp, "udmap"))
1937 return PCM_UDMA;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001938
1939 return PCM_EDMA;
1940}
1941
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001942static u32 davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata *pdata)
1943{
1944 int i;
1945 u32 offset = 0;
1946
1947 if (pdata->version != MCASP_VERSION_4)
1948 return pdata->tx_dma_offset;
1949
1950 for (i = 0; i < pdata->num_serializer; i++) {
1951 if (pdata->serial_dir[i] == TX_MODE) {
1952 if (!offset) {
1953 offset = DAVINCI_MCASP_TXBUF_REG(i);
1954 } else {
1955 pr_err("%s: Only one serializer allowed!\n",
1956 __func__);
1957 break;
1958 }
1959 }
1960 }
1961
1962 return offset;
1963}
1964
1965static u32 davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata *pdata)
1966{
1967 int i;
1968 u32 offset = 0;
1969
1970 if (pdata->version != MCASP_VERSION_4)
1971 return pdata->rx_dma_offset;
1972
1973 for (i = 0; i < pdata->num_serializer; i++) {
1974 if (pdata->serial_dir[i] == RX_MODE) {
1975 if (!offset) {
1976 offset = DAVINCI_MCASP_RXBUF_REG(i);
1977 } else {
1978 pr_err("%s: Only one serializer allowed!\n",
1979 __func__);
1980 break;
1981 }
1982 }
1983 }
1984
1985 return offset;
1986}
1987
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +02001988#ifdef CONFIG_GPIOLIB
1989static int davinci_mcasp_gpio_request(struct gpio_chip *chip, unsigned offset)
1990{
1991 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1992
1993 if (mcasp->num_serializer && offset < mcasp->num_serializer &&
1994 mcasp->serial_dir[offset] != INACTIVE_MODE) {
1995 dev_err(mcasp->dev, "AXR%u pin is used for audio\n", offset);
1996 return -EBUSY;
1997 }
1998
1999 /* Do not change the PIN yet */
2000
2001 return pm_runtime_get_sync(mcasp->dev);
2002}
2003
2004static void davinci_mcasp_gpio_free(struct gpio_chip *chip, unsigned offset)
2005{
2006 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2007
2008 /* Set the direction to input */
2009 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2010
2011 /* Set the pin as McASP pin */
2012 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2013
2014 pm_runtime_put_sync(mcasp->dev);
2015}
2016
2017static int davinci_mcasp_gpio_direction_out(struct gpio_chip *chip,
2018 unsigned offset, int value)
2019{
2020 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2021 u32 val;
2022
2023 if (value)
2024 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2025 else
2026 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2027
2028 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
2029 if (!(val & BIT(offset))) {
2030 /* Set the pin as GPIO pin */
2031 mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2032
2033 /* Set the direction to output */
2034 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2035 }
2036
2037 return 0;
2038}
2039
2040static void davinci_mcasp_gpio_set(struct gpio_chip *chip, unsigned offset,
2041 int value)
2042{
2043 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2044
2045 if (value)
2046 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2047 else
2048 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2049}
2050
2051static int davinci_mcasp_gpio_direction_in(struct gpio_chip *chip,
2052 unsigned offset)
2053{
2054 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2055 u32 val;
2056
2057 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
2058 if (!(val & BIT(offset))) {
2059 /* Set the direction to input */
2060 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2061
2062 /* Set the pin as GPIO pin */
2063 mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2064 }
2065
2066 return 0;
2067}
2068
2069static int davinci_mcasp_gpio_get(struct gpio_chip *chip, unsigned offset)
2070{
2071 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2072 u32 val;
2073
2074 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDSET_REG);
2075 if (val & BIT(offset))
2076 return 1;
2077
2078 return 0;
2079}
2080
2081static int davinci_mcasp_gpio_get_direction(struct gpio_chip *chip,
2082 unsigned offset)
2083{
2084 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2085 u32 val;
2086
2087 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDIR_REG);
2088 if (val & BIT(offset))
2089 return 0;
2090
2091 return 1;
2092}
2093
2094static const struct gpio_chip davinci_mcasp_template_chip = {
2095 .owner = THIS_MODULE,
2096 .request = davinci_mcasp_gpio_request,
2097 .free = davinci_mcasp_gpio_free,
2098 .direction_output = davinci_mcasp_gpio_direction_out,
2099 .set = davinci_mcasp_gpio_set,
2100 .direction_input = davinci_mcasp_gpio_direction_in,
2101 .get = davinci_mcasp_gpio_get,
2102 .get_direction = davinci_mcasp_gpio_get_direction,
2103 .base = -1,
2104 .ngpio = 32,
2105};
2106
2107static int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2108{
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02002109 if (!davinci_mcasp_have_gpiochip(mcasp))
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +02002110 return 0;
2111
2112 mcasp->gpio_chip = davinci_mcasp_template_chip;
2113 mcasp->gpio_chip.label = dev_name(mcasp->dev);
2114 mcasp->gpio_chip.parent = mcasp->dev;
2115#ifdef CONFIG_OF_GPIO
2116 mcasp->gpio_chip.of_node = mcasp->dev->of_node;
2117#endif
2118
2119 return devm_gpiochip_add_data(mcasp->dev, &mcasp->gpio_chip, mcasp);
2120}
2121
2122#else /* CONFIG_GPIOLIB */
2123static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2124{
2125 return 0;
2126}
2127#endif /* CONFIG_GPIOLIB */
2128
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002129static int davinci_mcasp_probe(struct platform_device *pdev)
2130{
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002131 struct snd_dmaengine_dai_dma_data *dma_data;
Peter Ujfalusidb8793a2020-11-06 09:25:49 +02002132 struct resource *mem, *dat;
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002133 struct davinci_mcasp *mcasp;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002134 char *irq_name;
2135 int irq;
Julia Lawall96d31e22011-12-29 17:51:21 +01002136 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002137
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05302138 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
2139 dev_err(&pdev->dev, "No platform data supplied\n");
2140 return -EINVAL;
2141 }
2142
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002143 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +01002144 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002145 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002146 return -ENOMEM;
2147
Jyri Sarha256ba182013-10-18 18:37:42 +03002148 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002149 if (!mem) {
Peter Ujfalusi18096cb2020-10-08 11:54:00 +03002150 dev_warn(&pdev->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +03002151 "\"mpu\" mem resource not found, using index 0\n");
2152 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2153 if (!mem) {
2154 dev_err(&pdev->dev, "no mem resource?\n");
2155 return -ENODEV;
2156 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002157 }
2158
Axel Lin508a43f2015-08-24 16:47:36 +08002159 mcasp->base = devm_ioremap_resource(&pdev->dev, mem);
2160 if (IS_ERR(mcasp->base))
2161 return PTR_ERR(mcasp->base);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002162
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02002163 dev_set_drvdata(&pdev->dev, mcasp);
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302164 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002165
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002166 mcasp->dev = &pdev->dev;
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02002167 ret = davinci_mcasp_get_config(mcasp, pdev);
2168 if (ret)
2169 goto err;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002170
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02002171 /* All PINS as McASP */
2172 pm_runtime_get_sync(mcasp->dev);
2173 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
2174 pm_runtime_put(mcasp->dev);
2175
2176 /* Skip audio related setup code if the configuration is not adequat */
2177 if (mcasp->missing_audio_param)
2178 goto no_audio;
2179
Peter Ujfalusi372c4bd2020-11-06 09:25:48 +02002180 irq = platform_get_irq_byname_optional(pdev, "common");
2181 if (irq > 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002182 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common",
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002183 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302184 if (!irq_name) {
2185 ret = -ENOMEM;
2186 goto err;
2187 }
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002188 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2189 davinci_mcasp_common_irq_handler,
Peter Ujfalusi8f511ff2015-02-02 14:38:32 +02002190 IRQF_ONESHOT | IRQF_SHARED,
2191 irq_name, mcasp);
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002192 if (ret) {
2193 dev_err(&pdev->dev, "common IRQ request failed\n");
2194 goto err;
2195 }
2196
2197 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2198 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2199 }
2200
Peter Ujfalusi372c4bd2020-11-06 09:25:48 +02002201 irq = platform_get_irq_byname_optional(pdev, "rx");
2202 if (irq > 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002203 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002204 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302205 if (!irq_name) {
2206 ret = -ENOMEM;
2207 goto err;
2208 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002209 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2210 davinci_mcasp_rx_irq_handler,
2211 IRQF_ONESHOT, irq_name, mcasp);
2212 if (ret) {
2213 dev_err(&pdev->dev, "RX IRQ request failed\n");
2214 goto err;
2215 }
2216
2217 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2218 }
2219
Peter Ujfalusi372c4bd2020-11-06 09:25:48 +02002220 irq = platform_get_irq_byname_optional(pdev, "tx");
2221 if (irq > 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002222 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002223 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302224 if (!irq_name) {
2225 ret = -ENOMEM;
2226 goto err;
2227 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002228 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2229 davinci_mcasp_tx_irq_handler,
2230 IRQF_ONESHOT, irq_name, mcasp);
2231 if (ret) {
2232 dev_err(&pdev->dev, "TX IRQ request failed\n");
2233 goto err;
2234 }
2235
2236 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2237 }
2238
Jyri Sarha256ba182013-10-18 18:37:42 +03002239 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002240 if (dat)
2241 mcasp->dat_port = true;
Jyri Sarha256ba182013-10-18 18:37:42 +03002242
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002243 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusidb8793a2020-11-06 09:25:49 +02002244 dma_data->filter_data = "tx";
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002245 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002246 dma_data->addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002247 else
Peter Ujfalusi1125d922020-11-06 09:25:50 +02002248 dma_data->addr = mem->start + davinci_mcasp_txdma_offset(mcasp->pdata);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002249
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002250
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002251 /* RX is not valid in DIT mode */
2252 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002253 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusidb8793a2020-11-06 09:25:49 +02002254 dma_data->filter_data = "rx";
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002255 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002256 dma_data->addr = dat->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002257 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03002258 dma_data->addr =
Peter Ujfalusi1125d922020-11-06 09:25:50 +02002259 mem->start + davinci_mcasp_rxdma_offset(mcasp->pdata);
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002260 }
Peter Ujfalusi453c4992013-11-14 11:35:34 +02002261
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002262 if (mcasp->version < MCASP_VERSION_3) {
2263 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02002264 /* dma_params->dma_addr is pointing to the data port address */
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002265 mcasp->dat_port = true;
2266 } else {
2267 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
2268 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002269
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002270 /* Allocate memory for long enough list for all possible
2271 * scenarios. Maximum number tdm slots is 32 and there cannot
2272 * be more serializers than given in the configuration. The
2273 * serializer directions could be taken into account, but it
2274 * would make code much more complex and save only couple of
2275 * bytes.
2276 */
2277 mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list =
Kees Cooka86854d2018-06-12 14:07:58 -07002278 devm_kcalloc(mcasp->dev,
2279 32 + mcasp->num_serializer - 1,
2280 sizeof(unsigned int),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002281 GFP_KERNEL);
2282
2283 mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list =
Kees Cooka86854d2018-06-12 14:07:58 -07002284 devm_kcalloc(mcasp->dev,
2285 32 + mcasp->num_serializer - 1,
2286 sizeof(unsigned int),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002287 GFP_KERNEL);
2288
2289 if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list ||
Christophe Jaillet1b8b68b2017-09-16 07:40:29 +02002290 !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) {
2291 ret = -ENOMEM;
2292 goto err;
2293 }
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002294
2295 ret = davinci_mcasp_set_ch_constraints(mcasp);
Jyri Sarha5935a052015-04-23 16:16:05 +03002296 if (ret)
2297 goto err;
2298
Peter Ujfalusiae726e92013-11-14 11:35:35 +02002299 mcasp_reparent_fck(pdev);
2300
Peter Ujfalusi1125d922020-11-06 09:25:50 +02002301 ret = devm_snd_soc_register_component(&pdev->dev, &davinci_mcasp_component,
2302 &davinci_mcasp_dai[mcasp->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002303
2304 if (ret != 0)
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002305 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302306
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002307 ret = davinci_mcasp_get_dma_type(mcasp);
2308 switch (ret) {
2309 case PCM_EDMA:
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002310 ret = edma_pcm_platform_register(&pdev->dev);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002311 break;
2312 case PCM_SDMA:
Janusz Krzysztofik3e802e92019-06-02 16:55:49 +02002313 ret = sdma_pcm_platform_register(&pdev->dev, "tx", "rx");
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002314 break;
Peter Ujfalusifb0c3c62020-02-10 16:09:50 +02002315 case PCM_UDMA:
2316 ret = udma_pcm_platform_register(&pdev->dev);
2317 break;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002318 default:
2319 dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret);
2320 case -EPROBE_DEFER:
2321 goto err;
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03002322 }
2323
2324 if (ret) {
2325 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002326 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302327 }
2328
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02002329no_audio:
2330 ret = davinci_mcasp_init_gpiochip(mcasp);
2331 if (ret) {
2332 dev_err(&pdev->dev, "gpiochip registration failed: %d\n", ret);
2333 goto err;
2334 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002335
Peter Ujfalusi1b4fb702020-11-06 09:25:51 +02002336 return 0;
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002337err:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302338 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002339 return ret;
2340}
2341
2342static int davinci_mcasp_remove(struct platform_device *pdev)
2343{
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302344 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002345
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002346 return 0;
2347}
2348
Peter Ujfalusi61754712019-01-03 16:05:50 +02002349#ifdef CONFIG_PM
2350static int davinci_mcasp_runtime_suspend(struct device *dev)
2351{
2352 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2353 struct davinci_mcasp_context *context = &mcasp->context;
2354 u32 reg;
2355 int i;
2356
2357 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2358 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
2359
2360 if (mcasp->txnumevt) {
2361 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2362 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
2363 }
2364 if (mcasp->rxnumevt) {
2365 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2366 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
2367 }
2368
2369 for (i = 0; i < mcasp->num_serializer; i++)
2370 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
2371 DAVINCI_MCASP_XRSRCTL_REG(i));
2372
2373 return 0;
2374}
2375
2376static int davinci_mcasp_runtime_resume(struct device *dev)
2377{
2378 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2379 struct davinci_mcasp_context *context = &mcasp->context;
2380 u32 reg;
2381 int i;
2382
2383 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2384 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
2385
2386 if (mcasp->txnumevt) {
2387 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2388 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
2389 }
2390 if (mcasp->rxnumevt) {
2391 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2392 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
2393 }
2394
2395 for (i = 0; i < mcasp->num_serializer; i++)
2396 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
2397 context->xrsr_regs[i]);
2398
2399 return 0;
2400}
2401
2402#endif
2403
2404static const struct dev_pm_ops davinci_mcasp_pm_ops = {
2405 SET_RUNTIME_PM_OPS(davinci_mcasp_runtime_suspend,
2406 davinci_mcasp_runtime_resume,
2407 NULL)
2408};
2409
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002410static struct platform_driver davinci_mcasp_driver = {
2411 .probe = davinci_mcasp_probe,
2412 .remove = davinci_mcasp_remove,
2413 .driver = {
2414 .name = "davinci-mcasp",
Peter Ujfalusi61754712019-01-03 16:05:50 +02002415 .pm = &davinci_mcasp_pm_ops,
Sachin Kamatea421eb2013-05-22 16:53:37 +05302416 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002417 },
2418};
2419
Axel Linf9b8a512011-11-25 10:09:27 +08002420module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002421
2422MODULE_AUTHOR("Steve Chen");
2423MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2424MODULE_LICENSE("GPL");