Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2 | /* |
| 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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 19 | #include <linux/delay.h> |
| 20 | #include <linux/io.h> |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 21 | #include <linux/clk.h> |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 22 | #include <linux/pm_runtime.h> |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_platform.h> |
| 25 | #include <linux/of_device.h> |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 26 | #include <linux/platform_data/davinci_asp.h> |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 27 | #include <linux/math64.h> |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 28 | #include <linux/bitmap.h> |
Peter Ujfalusi | 540f1ba7 | 2019-01-03 16:05:52 +0200 | [diff] [blame] | 29 | #include <linux/gpio/driver.h> |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 30 | |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 31 | #include <sound/asoundef.h> |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 32 | #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 Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 37 | #include <sound/dmaengine_pcm.h> |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 38 | |
Peter Ujfalusi | f3f9cfa | 2014-07-16 15:12:04 +0300 | [diff] [blame] | 39 | #include "edma-pcm.h" |
Peter Ujfalusi | f2055e1 | 2018-12-17 14:21:34 +0200 | [diff] [blame] | 40 | #include "sdma-pcm.h" |
Peter Ujfalusi | fb0c3c6 | 2020-02-10 16:09:50 +0200 | [diff] [blame] | 41 | #include "udma-pcm.h" |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 42 | #include "davinci-mcasp.h" |
| 43 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 44 | #define MCASP_MAX_AFIFO_DEPTH 64 |
| 45 | |
Arnd Bergmann | 8ca5104 | 2019-03-07 11:11:30 +0100 | [diff] [blame] | 46 | #ifdef CONFIG_PM |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 47 | static 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 Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 54 | DAVINCI_MCASP_AHCLKXCTL_REG, |
| 55 | DAVINCI_MCASP_AHCLKRCTL_REG, |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 56 | DAVINCI_MCASP_PDIR_REG, |
Peter Ujfalusi | 540f1ba7 | 2019-01-03 16:05:52 +0200 | [diff] [blame] | 57 | DAVINCI_MCASP_PFUNC_REG, |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 58 | DAVINCI_MCASP_RXMASK_REG, |
| 59 | DAVINCI_MCASP_TXMASK_REG, |
| 60 | DAVINCI_MCASP_RXTDM_REG, |
| 61 | DAVINCI_MCASP_TXTDM_REG, |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 62 | }; |
| 63 | |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 64 | struct davinci_mcasp_context { |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 65 | u32 config_regs[ARRAY_SIZE(context_regs)]; |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 66 | u32 afifo_regs[2]; /* for read/write fifo control registers */ |
| 67 | u32 *xrsr_regs; /* for serializer configuration */ |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 68 | bool pm_state; |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 69 | }; |
Arnd Bergmann | 8ca5104 | 2019-03-07 11:11:30 +0100 | [diff] [blame] | 70 | #endif |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 71 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 72 | struct davinci_mcasp_ruledata { |
| 73 | struct davinci_mcasp *mcasp; |
| 74 | int serializers; |
| 75 | }; |
| 76 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 77 | struct davinci_mcasp { |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 78 | struct snd_dmaengine_dai_dma_data dma_data[2]; |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 79 | struct davinci_mcasp_pdata *pdata; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 80 | void __iomem *base; |
Peter Ujfalusi | 487dce8 | 2013-11-14 11:35:31 +0200 | [diff] [blame] | 81 | u32 fifo_base; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 82 | struct device *dev; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 83 | struct snd_pcm_substream *substreams[2]; |
Peter Ujfalusi | 4a11ff2 | 2016-03-11 13:18:51 +0200 | [diff] [blame] | 84 | unsigned int dai_fmt; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 85 | |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 86 | /* Audio can not be enabled due to missing parameter(s) */ |
| 87 | bool missing_audio_param; |
| 88 | |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 89 | /* McASP specific data */ |
| 90 | int tdm_slots; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 91 | u32 tdm_mask[2]; |
| 92 | int slot_width; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 93 | u8 op_mode; |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 94 | u8 dismod; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 95 | u8 num_serializer; |
| 96 | u8 *serial_dir; |
| 97 | u8 version; |
Daniel Mack | 8267525 | 2014-07-16 14:04:41 +0200 | [diff] [blame] | 98 | u8 bclk_div; |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 99 | int streams; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 100 | u32 irq_request[2]; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 101 | |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 102 | int sysclk_freq; |
| 103 | bool bclk_master; |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 104 | u32 auxclk_fs_ratio; |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 105 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 106 | unsigned long pdir; /* Pin direction bitfield */ |
| 107 | |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 108 | /* McASP FIFO related */ |
| 109 | u8 txnumevt; |
| 110 | u8 rxnumevt; |
| 111 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 112 | bool dat_port; |
| 113 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 114 | /* Used for comstraint setting on the second stream */ |
| 115 | u32 channels; |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 116 | int max_format_width; |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 117 | u8 active_serializers[2]; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 118 | |
Peter Ujfalusi | 540f1ba7 | 2019-01-03 16:05:52 +0200 | [diff] [blame] | 119 | #ifdef CONFIG_GPIOLIB |
| 120 | struct gpio_chip gpio_chip; |
| 121 | #endif |
| 122 | |
Peter Ujfalusi | 6175471 | 2019-01-03 16:05:50 +0200 | [diff] [blame] | 123 | #ifdef CONFIG_PM |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 124 | struct davinci_mcasp_context context; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 125 | #endif |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 126 | |
| 127 | struct davinci_mcasp_ruledata ruledata[2]; |
Jyri Sarha | 5935a05 | 2015-04-23 16:16:05 +0300 | [diff] [blame] | 128 | struct snd_pcm_hw_constraint_list chconstr[2]; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 131 | static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 132 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 133 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 134 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 135 | __raw_writel(__raw_readl(reg) | val, reg); |
| 136 | } |
| 137 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 138 | static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 139 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 140 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 141 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 142 | __raw_writel((__raw_readl(reg) & ~(val)), reg); |
| 143 | } |
| 144 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 145 | static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 146 | u32 val, u32 mask) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 147 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 148 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 149 | __raw_writel((__raw_readl(reg) & ~mask) | val, reg); |
| 150 | } |
| 151 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 152 | static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset, |
| 153 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 154 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 155 | __raw_writel(val, mcasp->base + offset); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 158 | static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 159 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 160 | return (u32)__raw_readl(mcasp->base + offset); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 163 | static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 164 | { |
| 165 | int i = 0; |
| 166 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 167 | mcasp_set_bits(mcasp, ctl_reg, val); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 168 | |
| 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 Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 172 | if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 176 | if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val)) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 177 | printk(KERN_ERR "GBLCTL write error\n"); |
| 178 | } |
| 179 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 180 | static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp) |
| 181 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 182 | u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG); |
| 183 | u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 184 | |
| 185 | return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE; |
| 186 | } |
| 187 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 188 | static 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 | |
| 200 | static inline void mcasp_set_axr_pdir(struct davinci_mcasp *mcasp, bool enable) |
| 201 | { |
| 202 | u32 bit; |
| 203 | |
Peter Ujfalusi | 34a2a80 | 2019-07-25 11:34:23 +0300 | [diff] [blame] | 204 | for_each_set_bit(bit, &mcasp->pdir, PIN_BIT_AMUTE) { |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 205 | 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 Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 212 | static void mcasp_start_rx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 213 | { |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 214 | 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 Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 221 | /* Start clocks */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 222 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST); |
| 223 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 224 | /* |
| 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 Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 230 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST); |
| 231 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST); |
Peter Ujfalusi | 34a2a80 | 2019-07-25 11:34:23 +0300 | [diff] [blame] | 232 | mcasp_set_clk_pdir(mcasp, true); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 235 | /* Activate serializer(s) */ |
Peter Ujfalusi | 1003c27 | 2018-11-16 15:41:38 +0200 | [diff] [blame] | 236 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 237 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR); |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 238 | /* Release RX state machine */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 239 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST); |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 240 | /* Release Frame Sync generator */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 241 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 242 | if (mcasp_is_synchronous(mcasp)) |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 243 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 244 | |
| 245 | /* enable receive IRQs */ |
| 246 | mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG, |
| 247 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 250 | static void mcasp_start_tx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 251 | { |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 252 | u32 cnt; |
| 253 | |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 254 | 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 Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 261 | /* Start clocks */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 262 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST); |
| 263 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 264 | mcasp_set_clk_pdir(mcasp, true); |
| 265 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 266 | /* Activate serializer(s) */ |
Peter Ujfalusi | 1003c27 | 2018-11-16 15:41:38 +0200 | [diff] [blame] | 267 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 268 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 269 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 270 | /* wait for XDATA to be cleared */ |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 271 | cnt = 0; |
Peter Ujfalusi | e2a0c9f | 2015-12-11 13:06:24 +0200 | [diff] [blame] | 272 | while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) && |
| 273 | (cnt < 100000)) |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 274 | cnt++; |
| 275 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 276 | mcasp_set_axr_pdir(mcasp, true); |
| 277 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 278 | /* 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 Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 282 | |
| 283 | /* enable transmit IRQs */ |
| 284 | mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG, |
| 285 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 288 | static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 289 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 290 | mcasp->streams++; |
| 291 | |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 292 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 293 | mcasp_start_tx(mcasp); |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 294 | else |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 295 | mcasp_start_rx(mcasp); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 298 | static void mcasp_stop_rx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 299 | { |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 300 | /* disable IRQ sources */ |
| 301 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG, |
| 302 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]); |
| 303 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 304 | /* |
| 305 | * In synchronous mode stop the TX clocks if no other stream is |
| 306 | * running |
| 307 | */ |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 308 | if (mcasp_is_synchronous(mcasp) && !mcasp->streams) { |
| 309 | mcasp_set_clk_pdir(mcasp, false); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 310 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 311 | } |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 312 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 313 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0); |
| 314 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 315 | |
| 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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 323 | static void mcasp_stop_tx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 324 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 325 | u32 val = 0; |
| 326 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 327 | /* disable IRQ sources */ |
| 328 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG, |
| 329 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]); |
| 330 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 331 | /* |
| 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 Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 337 | else |
| 338 | mcasp_set_clk_pdir(mcasp, false); |
| 339 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 340 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 341 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val); |
| 342 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 343 | |
| 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 Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 349 | |
| 350 | mcasp_set_axr_pdir(mcasp, false); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 353 | static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 354 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 355 | mcasp->streams--; |
| 356 | |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 357 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 358 | mcasp_stop_tx(mcasp); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 359 | else |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 360 | mcasp_stop_rx(mcasp); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 361 | } |
| 362 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 363 | static 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 Iwai | dae35d1 | 2018-07-04 16:01:43 +0200 | [diff] [blame] | 377 | if (substream) |
| 378 | snd_pcm_stop_xrun(substream); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 379 | } |
| 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 | |
| 394 | static 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 Iwai | dae35d1 | 2018-07-04 16:01:43 +0200 | [diff] [blame] | 408 | if (substream) |
| 409 | snd_pcm_stop_xrun(substream); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 410 | } |
| 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 Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 425 | static 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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 439 | static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
| 440 | unsigned int fmt) |
| 441 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 442 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 443 | int ret = 0; |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 444 | u32 data_delay; |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 445 | bool fs_pol_rising; |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 446 | bool inv_fs = false; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 447 | |
Peter Ujfalusi | 4a11ff2 | 2016-03-11 13:18:51 +0200 | [diff] [blame] | 448 | if (!fmt) |
| 449 | return 0; |
| 450 | |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 451 | pm_runtime_get_sync(mcasp->dev); |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 452 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
Peter Ujfalusi | 188edc5 | 2014-04-04 14:31:43 +0300 | [diff] [blame] | 453 | 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 Ujfalusi | 188edc5 | 2014-04-04 14:31:43 +0300 | [diff] [blame] | 456 | /* 1st data bit occur one ACLK cycle after the frame sync */ |
| 457 | data_delay = 1; |
| 458 | break; |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 459 | case SND_SOC_DAIFMT_DSP_B: |
| 460 | case SND_SOC_DAIFMT_AC97: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 461 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR); |
| 462 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR); |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 463 | /* No delay after FS */ |
| 464 | data_delay = 0; |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 465 | break; |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 466 | case SND_SOC_DAIFMT_I2S: |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 467 | /* configure a full-word SYNC pulse (LRCLK) */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 468 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR); |
| 469 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR); |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 470 | /* 1st data bit occur one ACLK cycle after the frame sync */ |
| 471 | data_delay = 1; |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 472 | /* FS need to be inverted */ |
| 473 | inv_fs = true; |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 474 | break; |
Peter Ujfalusi | 816fe20 | 2019-07-25 11:34:11 +0300 | [diff] [blame] | 475 | case SND_SOC_DAIFMT_RIGHT_J: |
Peter Ujfalusi | 423761e | 2014-04-04 14:31:46 +0300 | [diff] [blame] | 476 | 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 Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 483 | default: |
| 484 | ret = -EINVAL; |
| 485 | goto out; |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 486 | } |
| 487 | |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 488 | 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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 493 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 494 | case SND_SOC_DAIFMT_CBS_CFS: |
| 495 | /* codec is clock and frame slave */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 496 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE); |
| 497 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 498 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 499 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE); |
| 500 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 501 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 502 | /* 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 Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 509 | mcasp->bclk_master = 1; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 510 | break; |
Peter Ujfalusi | 226e2f1 | 2015-02-12 16:41:26 +0200 | [diff] [blame] | 511 | 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 Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 519 | /* 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 Ujfalusi | 226e2f1 | 2015-02-12 16:41:26 +0200 | [diff] [blame] | 526 | mcasp->bclk_master = 1; |
| 527 | break; |
Chaithrika U S | 517ee6c | 2009-08-11 16:59:12 -0400 | [diff] [blame] | 528 | case SND_SOC_DAIFMT_CBM_CFS: |
| 529 | /* codec is clock master and frame slave */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 530 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE); |
| 531 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE); |
Chaithrika U S | 517ee6c | 2009-08-11 16:59:12 -0400 | [diff] [blame] | 532 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 533 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE); |
| 534 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE); |
Chaithrika U S | 517ee6c | 2009-08-11 16:59:12 -0400 | [diff] [blame] | 535 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 536 | /* 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 Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 543 | mcasp->bclk_master = 0; |
Chaithrika U S | 517ee6c | 2009-08-11 16:59:12 -0400 | [diff] [blame] | 544 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 545 | case SND_SOC_DAIFMT_CBM_CFM: |
| 546 | /* codec is clock and frame master */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 547 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE); |
| 548 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 549 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 550 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE); |
| 551 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 552 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 553 | /* 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 Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 560 | mcasp->bclk_master = 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 561 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 562 | default: |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 563 | ret = -EINVAL; |
| 564 | goto out; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 568 | case SND_SOC_DAIFMT_IB_NF: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 569 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL); |
Peter Ujfalusi | 74ddd8c | 2014-04-04 14:31:41 +0300 | [diff] [blame] | 570 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL); |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 571 | fs_pol_rising = true; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 572 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 573 | case SND_SOC_DAIFMT_NB_IF: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 574 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL); |
Peter Ujfalusi | 74ddd8c | 2014-04-04 14:31:41 +0300 | [diff] [blame] | 575 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL); |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 576 | fs_pol_rising = false; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 577 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 578 | case SND_SOC_DAIFMT_IB_IF: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 579 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL); |
Peter Ujfalusi | 74ddd8c | 2014-04-04 14:31:41 +0300 | [diff] [blame] | 580 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL); |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 581 | fs_pol_rising = false; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 582 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 583 | case SND_SOC_DAIFMT_NB_NF: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 584 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 585 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL); |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 586 | fs_pol_rising = true; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 587 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 588 | default: |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 589 | ret = -EINVAL; |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 590 | goto out; |
| 591 | } |
| 592 | |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 593 | if (inv_fs) |
| 594 | fs_pol_rising = !fs_pol_rising; |
| 595 | |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 596 | 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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 602 | } |
Peter Ujfalusi | 4a11ff2 | 2016-03-11 13:18:51 +0200 | [diff] [blame] | 603 | |
| 604 | mcasp->dai_fmt = fmt; |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 605 | out: |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 606 | pm_runtime_put(mcasp->dev); |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 607 | return ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 608 | } |
| 609 | |
Peter Ujfalusi | 226e73e | 2016-05-09 13:42:30 +0300 | [diff] [blame] | 610 | static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp *mcasp, int div_id, |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 611 | int div, bool explicit) |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 612 | { |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 613 | pm_runtime_get_sync(mcasp->dev); |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 614 | switch (div_id) { |
Peter Ujfalusi | 20d4b10 | 2016-05-09 13:42:29 +0300 | [diff] [blame] | 615 | case MCASP_CLKDIV_AUXCLK: /* MCLK divider */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 616 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 617 | AHCLKXDIV(div - 1), AHCLKXDIV_MASK); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 618 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 619 | AHCLKRDIV(div - 1), AHCLKRDIV_MASK); |
| 620 | break; |
| 621 | |
Peter Ujfalusi | 20d4b10 | 2016-05-09 13:42:29 +0300 | [diff] [blame] | 622 | case MCASP_CLKDIV_BCLK: /* BCLK divider */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 623 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 624 | ACLKXDIV(div - 1), ACLKXDIV_MASK); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 625 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 626 | ACLKRDIV(div - 1), ACLKRDIV_MASK); |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 627 | if (explicit) |
| 628 | mcasp->bclk_div = div; |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 629 | break; |
| 630 | |
Peter Ujfalusi | 20d4b10 | 2016-05-09 13:42:29 +0300 | [diff] [blame] | 631 | case MCASP_CLKDIV_BCLK_FS_RATIO: |
| 632 | /* |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 633 | * 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 Dunlap | 0d8aa2c | 2020-08-07 18:22:09 -0700 | [diff] [blame] | 639 | * tdm_slot width by dividing the ratio by the |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 640 | * 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 Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 647 | break; |
| 648 | |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 649 | default: |
| 650 | return -EINVAL; |
| 651 | } |
| 652 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 653 | pm_runtime_put(mcasp->dev); |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 654 | return 0; |
| 655 | } |
| 656 | |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 657 | static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id, |
| 658 | int div) |
| 659 | { |
Peter Ujfalusi | 226e73e | 2016-05-09 13:42:30 +0300 | [diff] [blame] | 660 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
| 661 | |
| 662 | return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1); |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 663 | } |
| 664 | |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 665 | static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 666 | unsigned int freq, int dir) |
| 667 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 668 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 669 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 670 | pm_runtime_get_sync(mcasp->dev); |
Peter Ujfalusi | 253f584 | 2019-12-04 21:20:05 +0200 | [diff] [blame] | 671 | |
| 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 Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 694 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE); |
| 695 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 696 | set_bit(PIN_BIT_AHCLKX, &mcasp->pdir); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 697 | } |
Peter Ujfalusi | 253f584 | 2019-12-04 21:20:05 +0200 | [diff] [blame] | 698 | /* |
| 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 Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 702 | mcasp->sysclk_freq = freq; |
Peter Ujfalusi | 253f584 | 2019-12-04 21:20:05 +0200 | [diff] [blame] | 703 | out: |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 704 | pm_runtime_put(mcasp->dev); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 708 | /* All serializers must have equal number of channels */ |
| 709 | static 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 Ujfalusi | e4798d2 | 2017-05-11 09:58:22 +0300 | [diff] [blame] | 720 | for (i = 1; i <= slots; i++) |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 721 | 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 | |
| 731 | static 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 | |
| 753 | static 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 Dannenberg | 1bdd593 | 2015-11-09 12:19:19 -0600 | [diff] [blame] | 779 | mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask; |
| 780 | mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 781 | mcasp->slot_width = slot_width; |
| 782 | |
| 783 | return davinci_mcasp_set_ch_constraints(mcasp); |
| 784 | } |
| 785 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 786 | static int davinci_config_channel_size(struct davinci_mcasp *mcasp, |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 787 | int sample_width) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 788 | { |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 789 | u32 fmt; |
Peter Ujfalusi | 816fe20 | 2019-07-25 11:34:11 +0300 | [diff] [blame] | 790 | u32 tx_rotate, rx_rotate, slot_width; |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 791 | u32 mask = (1ULL << sample_width) - 1; |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 792 | |
Peter Ujfalusi | 816fe20 | 2019-07-25 11:34:11 +0300 | [diff] [blame] | 793 | if (mcasp->slot_width) |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 794 | slot_width = mcasp->slot_width; |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 795 | else if (mcasp->max_format_width) |
| 796 | slot_width = mcasp->max_format_width; |
Peter Ujfalusi | 816fe20 | 2019-07-25 11:34:11 +0300 | [diff] [blame] | 797 | 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 Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 814 | rx_rotate = (slot_width - sample_width) / 4; |
Peter Ujfalusi | d742b92 | 2014-11-10 12:32:19 +0200 | [diff] [blame] | 815 | } |
Daniel Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 816 | |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 817 | /* mapping of the XSSZ bit-field as described in the datasheet */ |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 818 | fmt = (slot_width >> 1) - 1; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 819 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 820 | if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 821 | 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 Yefremov | f5023af | 2013-04-04 16:13:20 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 832 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask); |
Chaithrika U S | 0c31cf3 | 2009-09-15 18:13:29 -0400 | [diff] [blame] | 833 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
Peter Ujfalusi | 662ffae | 2014-01-30 15:15:22 +0200 | [diff] [blame] | 837 | static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream, |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 838 | int period_words, int channels) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 839 | { |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 840 | struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream]; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 841 | int i; |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 842 | u8 tx_ser = 0; |
| 843 | u8 rx_ser = 0; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 844 | u8 slots = mcasp->tdm_slots; |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 845 | u8 max_active_serializers = (channels + slots - 1) / slots; |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 846 | u8 max_rx_serializers, max_tx_serializers; |
Peter Ujfalusi | 7238319 | 2015-09-14 16:06:48 +0300 | [diff] [blame] | 847 | int active_serializers, numevt; |
Peter Ujfalusi | 487dce8 | 2013-11-14 11:35:31 +0200 | [diff] [blame] | 848 | u32 reg; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 849 | /* Default configuration */ |
Peter Ujfalusi | 40448e5 | 2014-04-04 15:56:30 +0300 | [diff] [blame] | 850 | if (mcasp->version < MCASP_VERSION_3) |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 851 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 852 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 853 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 854 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF); |
| 855 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS); |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 856 | max_tx_serializers = max_active_serializers; |
| 857 | max_rx_serializers = |
| 858 | mcasp->active_serializers[SNDRV_PCM_STREAM_CAPTURE]; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 859 | } else { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 860 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF); |
| 861 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS); |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 862 | max_tx_serializers = |
| 863 | mcasp->active_serializers[SNDRV_PCM_STREAM_PLAYBACK]; |
| 864 | max_rx_serializers = max_active_serializers; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 865 | } |
| 866 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 867 | for (i = 0; i < mcasp->num_serializer; i++) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 868 | mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
| 869 | mcasp->serial_dir[i]); |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 870 | if (mcasp->serial_dir[i] == TX_MODE && |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 871 | tx_ser < max_tx_serializers) { |
Misael Lopez Cruz | 19db62e | 2015-06-08 16:03:47 +0300 | [diff] [blame] | 872 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 873 | mcasp->dismod, DISMOD_MASK); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 874 | set_bit(PIN_BIT_AXR(i), &mcasp->pdir); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 875 | tx_ser++; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 876 | } else if (mcasp->serial_dir[i] == RX_MODE && |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 877 | rx_ser < max_rx_serializers) { |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 878 | clear_bit(PIN_BIT_AXR(i), &mcasp->pdir); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 879 | rx_ser++; |
Peter Ujfalusi | 5dd17a3 | 2019-06-20 12:20:01 +0300 | [diff] [blame] | 880 | } else { |
| 881 | /* Inactive or unused pin, set it to inactive */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 882 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
| 883 | SRMOD_INACTIVE, SRMOD_MASK); |
Peter Ujfalusi | 5dd17a3 | 2019-06-20 12:20:01 +0300 | [diff] [blame] | 884 | /* 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 Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 889 | clear_bit(PIN_BIT_AXR(i), &mcasp->pdir); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 893 | 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 Mack | ecf327c | 2013-03-08 14:19:38 +0100 | [diff] [blame] | 902 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 903 | if (active_serializers < max_active_serializers) { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 904 | dev_warn(mcasp->dev, "stream has more channels (%d) than are " |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 905 | "enabled in mcasp (%d)\n", channels, |
| 906 | active_serializers * slots); |
Daniel Mack | ecf327c | 2013-03-08 14:19:38 +0100 | [diff] [blame] | 907 | return -EINVAL; |
| 908 | } |
| 909 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 910 | /* AFIFO is not in use */ |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 911 | if (!numevt) { |
| 912 | /* Configure the burst size for platform drivers */ |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 913 | 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 Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 920 | dma_data->maxburst = active_serializers; |
| 921 | } else { |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 922 | dma_data->maxburst = 0; |
| 923 | } |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 924 | |
| 925 | goto out; |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 926 | } |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 927 | |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 928 | 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 Ujfalusi | 7238319 | 2015-09-14 16:06:48 +0300 | [diff] [blame] | 940 | numevt = (numevt / active_serializers) * active_serializers; |
| 941 | |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 942 | while (period_words % numevt && numevt > 0) |
| 943 | numevt -= active_serializers; |
| 944 | if (numevt <= 0) |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 945 | numevt = active_serializers; |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 946 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 947 | mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK); |
| 948 | mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK); |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 949 | |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 950 | /* Configure the burst size for platform drivers */ |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 951 | if (numevt == 1) |
| 952 | numevt = 0; |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 953 | dma_data->maxburst = numevt; |
| 954 | |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 955 | out: |
| 956 | mcasp->active_serializers[stream] = active_serializers; |
| 957 | |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 958 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 959 | } |
| 960 | |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 961 | static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream, |
| 962 | int channels) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 963 | { |
| 964 | int i, active_slots; |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 965 | int total_slots; |
| 966 | int active_serializers; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 967 | u32 mask = 0; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 968 | u32 busel = 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 969 | |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 970 | total_slots = mcasp->tdm_slots; |
| 971 | |
| 972 | /* |
| 973 | * If more than one serializer is needed, then use them with |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 974 | * 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 Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 977 | */ |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 978 | if (mcasp->tdm_mask[stream]) { |
| 979 | active_slots = hweight32(mcasp->tdm_mask[stream]); |
| 980 | active_serializers = (channels + active_slots - 1) / |
| 981 | active_slots; |
Peter Ujfalusi | fd14f44 | 2019-06-20 12:20:02 +0300 | [diff] [blame] | 982 | if (active_serializers == 1) |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 983 | active_slots = channels; |
Peter Ujfalusi | fd14f44 | 2019-06-20 12:20:02 +0300 | [diff] [blame] | 984 | 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 Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 989 | } |
| 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 Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 997 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 998 | for (i = 0; i < active_slots; i++) |
| 999 | mask |= (1 << i); |
| 1000 | } |
Peter Ujfalusi | 5dd17a3 | 2019-06-20 12:20:01 +0300 | [diff] [blame] | 1001 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1002 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 1003 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 1004 | if (!mcasp->dat_port) |
| 1005 | busel = TXSEL; |
| 1006 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1007 | 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 Ujfalusi | 0ad7d3a | 2015-11-23 12:51:53 +0200 | [diff] [blame] | 1017 | /* |
| 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 Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1025 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1026 | |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1027 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* S/PDIF */ |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 1031 | static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp, |
| 1032 | unsigned int rate) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1033 | { |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 1034 | u32 cs_value = 0; |
| 1035 | u8 *cs_bytes = (u8*) &cs_value; |
| 1036 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1037 | /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0 |
| 1038 | and LSB first */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1039 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1040 | |
| 1041 | /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1042 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1043 | |
| 1044 | /* Set the TX tdm : for all the slots */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1045 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1046 | |
| 1047 | /* Set the TX clock controls : div = 1 and internal */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1048 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1049 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1050 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1051 | |
| 1052 | /* Only 44100 and 48000 are valid, both have the same setting */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1053 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1054 | |
| 1055 | /* Enable the DIT */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1056 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1057 | |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 1058 | /* 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 Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1098 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1099 | } |
| 1100 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1101 | static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp, |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1102 | unsigned int sysclk_freq, |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1103 | unsigned int bclk_freq, bool set) |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1104 | { |
Peter Ujfalusi | ddecd14 | 2016-05-09 13:42:32 +0300 | [diff] [blame] | 1105 | 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 Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1108 | int error_ppm; |
Peter Ujfalusi | ddecd14 | 2016-05-09 13:42:32 +0300 | [diff] [blame] | 1109 | 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 Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1125 | |
| 1126 | if (rem != 0) { |
| 1127 | if (div == 0 || |
Peter Ujfalusi | ddecd14 | 2016-05-09 13:42:32 +0300 | [diff] [blame] | 1128 | ((sysclk_freq / div) - bclk_freq) > |
| 1129 | (bclk_freq - (sysclk_freq / (div+1)))) { |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1130 | div++; |
| 1131 | rem = rem - bclk_freq; |
| 1132 | } |
| 1133 | } |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1134 | error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem, |
| 1135 | (int)bclk_freq)) / div - 1000000; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1136 | |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1137 | 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 Ujfalusi | ddecd14 | 2016-05-09 13:42:32 +0300 | [diff] [blame] | 1143 | if (reg & AHCLKXE) |
| 1144 | __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK, |
| 1145 | aux_div, 0); |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | return error_ppm; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Peter Ujfalusi | 5fcb457 | 2018-08-31 11:24:56 +0300 | [diff] [blame] | 1151 | static 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 | |
| 1159 | static 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 | |
| 1167 | static 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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1187 | static 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 Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1191 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1192 | int word_length; |
Peter Ujfalusi | a7e46bd | 2014-02-03 14:51:50 +0200 | [diff] [blame] | 1193 | int channels = params_channels(params); |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 1194 | int period_size = params_period_size(params); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1195 | int ret; |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 1196 | |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 1197 | 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 Ujfalusi | 4a11ff2 | 2016-03-11 13:18:51 +0200 | [diff] [blame] | 1228 | ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt); |
| 1229 | if (ret) |
| 1230 | return ret; |
| 1231 | |
Daniel Mack | 8267525 | 2014-07-16 14:04:41 +0200 | [diff] [blame] | 1232 | /* |
| 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 Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1237 | int slots = mcasp->tdm_slots; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1238 | int rate = params_rate(params); |
| 1239 | int sbits = params_width(params); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1240 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1241 | if (mcasp->slot_width) |
| 1242 | sbits = mcasp->slot_width; |
| 1243 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1244 | davinci_mcasp_calc_clk_div(mcasp, mcasp->sysclk_freq, |
| 1245 | rate * sbits * slots, true); |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 1248 | ret = mcasp_common_hw_param(mcasp, substream->stream, |
| 1249 | period_size * channels, channels); |
Peter Ujfalusi | 0f7d9a6 | 2014-01-30 15:15:24 +0200 | [diff] [blame] | 1250 | if (ret) |
| 1251 | return ret; |
| 1252 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1253 | if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE) |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 1254 | ret = mcasp_dit_hw_param(mcasp, params_rate(params)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1255 | else |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 1256 | ret = mcasp_i2s_hw_param(mcasp, substream->stream, |
| 1257 | channels); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1258 | |
| 1259 | if (ret) |
| 1260 | return ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1261 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1262 | davinci_config_channel_size(mcasp, word_length); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1263 | |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1264 | if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) { |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1265 | mcasp->channels = channels; |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1266 | if (!mcasp->max_format_width) |
| 1267 | mcasp->max_format_width = word_length; |
| 1268 | } |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1269 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | static int davinci_mcasp_trigger(struct snd_pcm_substream *substream, |
| 1274 | int cmd, struct snd_soc_dai *cpu_dai) |
| 1275 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1276 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1277 | int ret = 0; |
| 1278 | |
| 1279 | switch (cmd) { |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1280 | case SNDRV_PCM_TRIGGER_RESUME: |
Chaithrika U S | e473b84 | 2010-01-20 17:06:33 +0530 | [diff] [blame] | 1281 | case SNDRV_PCM_TRIGGER_START: |
| 1282 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1283 | davinci_mcasp_start(mcasp, substream->stream); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1284 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1285 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Chaithrika U S | a47979b | 2009-12-03 18:56:56 +0530 | [diff] [blame] | 1286 | case SNDRV_PCM_TRIGGER_STOP: |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1287 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1288 | davinci_mcasp_stop(mcasp, substream->stream); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1289 | break; |
| 1290 | |
| 1291 | default: |
| 1292 | ret = -EINVAL; |
| 1293 | } |
| 1294 | |
| 1295 | return ret; |
| 1296 | } |
| 1297 | |
Peter Ujfalusi | 1e112c3 | 2019-07-26 09:42:43 +0300 | [diff] [blame] | 1298 | static 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 Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1320 | static 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 Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1342 | static 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 | |
| 1349 | static 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 Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1356 | int slots = rd->mcasp->tdm_slots; |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1357 | struct snd_interval range; |
| 1358 | int i; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1359 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1360 | if (rd->mcasp->slot_width) |
| 1361 | sbits = rd->mcasp->slot_width; |
| 1362 | |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1363 | snd_interval_any(&range); |
| 1364 | range.empty = 1; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1365 | |
| 1366 | for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) { |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1367 | if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) { |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1368 | uint bclk_freq = sbits * slots * |
| 1369 | davinci_mcasp_dai_rates[i]; |
| 1370 | unsigned int sysclk_freq; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1371 | int ppm; |
| 1372 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1373 | 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 Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1381 | 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 Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1388 | } |
| 1389 | } |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1390 | |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1391 | 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 Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static 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 Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1406 | int slots = rd->mcasp->tdm_slots; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1407 | int i, count = 0; |
| 1408 | |
| 1409 | snd_mask_none(&nfmt); |
| 1410 | |
Peter Ujfalusi | 9be072a | 2016-09-01 10:05:12 +0300 | [diff] [blame] | 1411 | for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) { |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1412 | if (snd_mask_test(fmt, i)) { |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1413 | uint sbits = snd_pcm_format_width(i); |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1414 | unsigned int sysclk_freq; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1415 | int ppm; |
| 1416 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1417 | 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 Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1423 | if (rd->mcasp->slot_width) |
| 1424 | sbits = rd->mcasp->slot_width; |
| 1425 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1426 | ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq, |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1427 | sbits * slots * rate, |
| 1428 | false); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1429 | 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 Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1436 | "%d possible sample format for %d Hz and %d tdm slots\n", |
| 1437 | count, rate, slots); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1438 | |
| 1439 | return snd_mask_refine(fmt, &nfmt); |
| 1440 | } |
| 1441 | |
Peter Ujfalusi | d43c17d | 2018-01-05 12:18:07 +0200 | [diff] [blame] | 1442 | static 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 Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1456 | static 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 Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1460 | struct davinci_mcasp_ruledata *ruledata = |
| 1461 | &mcasp->ruledata[substream->stream]; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1462 | u32 max_channels = 0; |
Peter Ujfalusi | 1e112c3 | 2019-07-26 09:42:43 +0300 | [diff] [blame] | 1463 | int i, dir, ret; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1464 | int tdm_slots = mcasp->tdm_slots; |
| 1465 | |
Peter Ujfalusi | 1935736 | 2016-05-09 13:39:14 +0300 | [diff] [blame] | 1466 | /* Do not allow more then one stream per direction */ |
| 1467 | if (mcasp->substreams[substream->stream]) |
| 1468 | return -EBUSY; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1469 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1470 | mcasp->substreams[substream->stream] = substream; |
| 1471 | |
Peter Ujfalusi | 1935736 | 2016-05-09 13:39:14 +0300 | [diff] [blame] | 1472 | if (mcasp->tdm_mask[substream->stream]) |
| 1473 | tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]); |
| 1474 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1475 | 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 Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1491 | ruledata->serializers = max_channels; |
Peter Ujfalusi | 1e112c3 | 2019-07-26 09:42:43 +0300 | [diff] [blame] | 1492 | ruledata->mcasp = mcasp; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1493 | max_channels *= tdm_slots; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1494 | /* |
| 1495 | * If the already active stream has less channels than the calculated |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 1496 | * 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 Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1500 | */ |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 1501 | if (mcasp->channels && mcasp->channels < max_channels && |
| 1502 | ruledata->serializers == 1) |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1503 | max_channels = mcasp->channels; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1504 | /* |
| 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 Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1510 | |
| 1511 | snd_pcm_hw_constraint_minmax(substream->runtime, |
| 1512 | SNDRV_PCM_HW_PARAM_CHANNELS, |
Peter Ujfalusi | e4798d2 | 2017-05-11 09:58:22 +0300 | [diff] [blame] | 1513 | 0, max_channels); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1514 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1515 | snd_pcm_hw_constraint_list(substream->runtime, |
| 1516 | 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1517 | &mcasp->chconstr[substream->stream]); |
| 1518 | |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1519 | 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 Ujfalusi | 1e112c3 | 2019-07-26 09:42:43 +0300 | [diff] [blame] | 1533 | /* 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 Sarha | 5935a05 | 2015-04-23 16:16:05 +0300 | [diff] [blame] | 1542 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1543 | /* |
| 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 Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1548 | ret = snd_pcm_hw_rule_add(substream->runtime, 0, |
| 1549 | SNDRV_PCM_HW_PARAM_RATE, |
| 1550 | davinci_mcasp_hw_rule_rate, |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1551 | ruledata, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1552 | SNDRV_PCM_HW_PARAM_FORMAT, -1); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1553 | 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 Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1558 | ruledata, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1559 | SNDRV_PCM_HW_PARAM_RATE, -1); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1560 | if (ret) |
| 1561 | return ret; |
| 1562 | } |
| 1563 | |
Peter Ujfalusi | d43c17d | 2018-01-05 12:18:07 +0200 | [diff] [blame] | 1564 | 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 Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | static 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 Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1577 | mcasp->substreams[substream->stream] = NULL; |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 1578 | mcasp->active_serializers[substream->stream] = 0; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1579 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1580 | if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE) |
| 1581 | return; |
| 1582 | |
Kuninori Morimoto | 36ad1a8 | 2020-05-15 09:47:48 +0900 | [diff] [blame] | 1583 | if (!snd_soc_dai_active(cpu_dai)) { |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1584 | mcasp->channels = 0; |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1585 | mcasp->max_format_width = 0; |
| 1586 | } |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1587 | } |
| 1588 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 1589 | static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = { |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1590 | .startup = davinci_mcasp_startup, |
| 1591 | .shutdown = davinci_mcasp_shutdown, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1592 | .trigger = davinci_mcasp_trigger, |
Peter Ujfalusi | 5fcb457 | 2018-08-31 11:24:56 +0300 | [diff] [blame] | 1593 | .delay = davinci_mcasp_delay, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1594 | .hw_params = davinci_mcasp_hw_params, |
| 1595 | .set_fmt = davinci_mcasp_set_dai_fmt, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 1596 | .set_clkdiv = davinci_mcasp_set_clkdiv, |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 1597 | .set_sysclk = davinci_mcasp_set_sysclk, |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1598 | .set_tdm_slot = davinci_mcasp_set_tdm_slot, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1599 | }; |
| 1600 | |
Peter Ujfalusi | d5902f69 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1601 | static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai) |
| 1602 | { |
| 1603 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
| 1604 | |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1605 | dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK]; |
| 1606 | dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE]; |
Peter Ujfalusi | d5902f69 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1607 | |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
Peter Ujfalusi | ed29cd5 | 2013-11-14 11:35:22 +0200 | [diff] [blame] | 1611 | #define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000 |
| 1612 | |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1613 | #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 Mack | 21eb24d | 2012-10-09 09:35:16 +0200 | [diff] [blame] | 1617 | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 1618 | SNDRV_PCM_FMTBIT_U24_LE | \ |
| 1619 | SNDRV_PCM_FMTBIT_S24_3LE | \ |
| 1620 | SNDRV_PCM_FMTBIT_U24_3LE | \ |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1621 | SNDRV_PCM_FMTBIT_S32_LE | \ |
| 1622 | SNDRV_PCM_FMTBIT_U32_LE) |
| 1623 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1624 | static struct snd_soc_dai_driver davinci_mcasp_dai[] = { |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1625 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1626 | .name = "davinci-mcasp.0", |
Peter Ujfalusi | d5902f69 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1627 | .probe = davinci_mcasp_dai_probe, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1628 | .playback = { |
Peter Ujfalusi | ef3ab25 | 2020-06-30 15:58:41 +0300 | [diff] [blame] | 1629 | .stream_name = "IIS Playback", |
Peter Ujfalusi | e4798d2 | 2017-05-11 09:58:22 +0300 | [diff] [blame] | 1630 | .channels_min = 1, |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1631 | .channels_max = 32 * 16, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1632 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1633 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1634 | }, |
| 1635 | .capture = { |
Peter Ujfalusi | ef3ab25 | 2020-06-30 15:58:41 +0300 | [diff] [blame] | 1636 | .stream_name = "IIS Capture", |
Peter Ujfalusi | e4798d2 | 2017-05-11 09:58:22 +0300 | [diff] [blame] | 1637 | .channels_min = 1, |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1638 | .channels_max = 32 * 16, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1639 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1640 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1641 | }, |
| 1642 | .ops = &davinci_mcasp_dai_ops, |
| 1643 | |
Jyri Sarha | 295c340 | 2015-09-09 21:27:42 +0300 | [diff] [blame] | 1644 | .symmetric_rates = 1, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1645 | }, |
| 1646 | { |
Peter Ujfalusi | 58e48d9 | 2013-11-14 11:35:24 +0200 | [diff] [blame] | 1647 | .name = "davinci-mcasp.1", |
Peter Ujfalusi | d5902f69 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1648 | .probe = davinci_mcasp_dai_probe, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1649 | .playback = { |
Peter Ujfalusi | ef3ab25 | 2020-06-30 15:58:41 +0300 | [diff] [blame] | 1650 | .stream_name = "DIT Playback", |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1651 | .channels_min = 1, |
| 1652 | .channels_max = 384, |
| 1653 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1654 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1655 | }, |
| 1656 | .ops = &davinci_mcasp_dai_ops, |
| 1657 | }, |
| 1658 | |
| 1659 | }; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1660 | |
Kuninori Morimoto | eeef0ed | 2013-03-21 03:31:19 -0700 | [diff] [blame] | 1661 | static const struct snd_soc_component_driver davinci_mcasp_component = { |
| 1662 | .name = "davinci-mcasp", |
| 1663 | }; |
| 1664 | |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1665 | /* Some HW specific values and defaults. The rest is filled in from DT. */ |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1666 | static struct davinci_mcasp_pdata dm646x_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1667 | .tx_dma_offset = 0x400, |
| 1668 | .rx_dma_offset = 0x400, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1669 | .version = MCASP_VERSION_1, |
| 1670 | }; |
| 1671 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1672 | static struct davinci_mcasp_pdata da830_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1673 | .tx_dma_offset = 0x2000, |
| 1674 | .rx_dma_offset = 0x2000, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1675 | .version = MCASP_VERSION_2, |
| 1676 | }; |
| 1677 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1678 | static struct davinci_mcasp_pdata am33xx_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1679 | .tx_dma_offset = 0, |
| 1680 | .rx_dma_offset = 0, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1681 | .version = MCASP_VERSION_3, |
| 1682 | }; |
| 1683 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1684 | static struct davinci_mcasp_pdata dra7_mcasp_pdata = { |
Peter Ujfalusi | 9ac0013 | 2016-06-02 12:55:05 +0300 | [diff] [blame] | 1685 | /* The CFG port offset will be calculated if it is needed */ |
| 1686 | .tx_dma_offset = 0, |
| 1687 | .rx_dma_offset = 0, |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 1688 | .version = MCASP_VERSION_4, |
| 1689 | }; |
| 1690 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1691 | static const struct of_device_id mcasp_dt_ids[] = { |
| 1692 | { |
| 1693 | .compatible = "ti,dm646x-mcasp-audio", |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1694 | .data = &dm646x_mcasp_pdata, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1695 | }, |
| 1696 | { |
| 1697 | .compatible = "ti,da830-mcasp-audio", |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1698 | .data = &da830_mcasp_pdata, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1699 | }, |
Hebbar, Gururaja | e5ec69d | 2012-09-03 13:40:40 +0530 | [diff] [blame] | 1700 | { |
Jyri Sarha | 3af9e03 | 2013-10-18 18:37:44 +0300 | [diff] [blame] | 1701 | .compatible = "ti,am33xx-mcasp-audio", |
Peter Ujfalusi | b14899d | 2013-11-14 11:35:37 +0200 | [diff] [blame] | 1702 | .data = &am33xx_mcasp_pdata, |
Hebbar, Gururaja | e5ec69d | 2012-09-03 13:40:40 +0530 | [diff] [blame] | 1703 | }, |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 1704 | { |
| 1705 | .compatible = "ti,dra7-mcasp-audio", |
| 1706 | .data = &dra7_mcasp_pdata, |
| 1707 | }, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1708 | { /* sentinel */ } |
| 1709 | }; |
| 1710 | MODULE_DEVICE_TABLE(of, mcasp_dt_ids); |
| 1711 | |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 1712 | static 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 Ujfalusi | c670254 | 2016-01-27 15:02:49 +0200 | [diff] [blame] | 1726 | dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n"); |
| 1727 | |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 1728 | 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 | |
| 1747 | err2: |
| 1748 | clk_put(parent_clk); |
| 1749 | err1: |
| 1750 | clk_put(gfclk); |
| 1751 | return ret; |
| 1752 | } |
| 1753 | |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 1754 | static bool davinci_mcasp_have_gpiochip(struct davinci_mcasp *mcasp) |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1755 | { |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 1756 | #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 Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1765 | static int davinci_mcasp_get_config(struct davinci_mcasp *mcasp, |
| 1766 | struct platform_device *pdev) |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1767 | { |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1768 | const struct of_device_id *match = of_match_device(mcasp_dt_ids, &pdev->dev); |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1769 | struct device_node *np = pdev->dev.of_node; |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1770 | struct davinci_mcasp_pdata *pdata = NULL; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1771 | const u32 *of_serial_dir32; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1772 | u32 val; |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1773 | int i; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1774 | |
| 1775 | if (pdev->dev.platform_data) { |
| 1776 | pdata = pdev->dev.platform_data; |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 1777 | pdata->dismod = DISMOD_LOW; |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1778 | goto out; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1779 | } else if (match) { |
Peter Ujfalusi | 272ee03 | 2016-06-02 12:55:24 +0300 | [diff] [blame] | 1780 | pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata), |
| 1781 | GFP_KERNEL); |
Colin Ian King | f4d95de | 2020-02-10 09:24:22 +0000 | [diff] [blame] | 1782 | if (!pdata) |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1783 | return -ENOMEM; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1784 | } else { |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1785 | dev_err(&pdev->dev, "No compatible match found\n"); |
| 1786 | return -EINVAL; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1787 | } |
| 1788 | |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 1789 | if (of_property_read_u32(np, "op-mode", &val) == 0) { |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1790 | pdata->op_mode = val; |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 1791 | } else { |
| 1792 | mcasp->missing_audio_param = true; |
| 1793 | goto out; |
| 1794 | } |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1795 | |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1796 | if (of_property_read_u32(np, "tdm-slots", &val) == 0) { |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1797 | if (val < 2 || val > 32) { |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1798 | dev_err(&pdev->dev, "tdm-slots must be in rage [2-32]\n"); |
| 1799 | return -EINVAL; |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1800 | } |
| 1801 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1802 | pdata->tdm_slots = val; |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 1803 | } else if (pdata->op_mode == DAVINCI_MCASP_IIS_MODE) { |
| 1804 | mcasp->missing_audio_param = true; |
| 1805 | goto out; |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1806 | } |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1807 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1808 | of_serial_dir32 = of_get_property(np, "serial-dir", &val); |
| 1809 | val /= sizeof(u32); |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1810 | if (of_serial_dir32) { |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1811 | u8 *of_serial_dir = devm_kzalloc(&pdev->dev, |
| 1812 | (sizeof(*of_serial_dir) * val), |
| 1813 | GFP_KERNEL); |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1814 | if (!of_serial_dir) |
| 1815 | return -ENOMEM; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1816 | |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1817 | for (i = 0; i < val; i++) |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1818 | of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]); |
| 1819 | |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1820 | pdata->num_serializer = val; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1821 | pdata->serial_dir = of_serial_dir; |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 1822 | } else { |
| 1823 | mcasp->missing_audio_param = true; |
| 1824 | goto out; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1825 | } |
| 1826 | |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1827 | if (of_property_read_u32(np, "tx-num-evt", &val) == 0) |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1828 | pdata->txnumevt = val; |
| 1829 | |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1830 | if (of_property_read_u32(np, "rx-num-evt", &val) == 0) |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1831 | pdata->rxnumevt = val; |
| 1832 | |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1833 | if (of_property_read_u32(np, "auxclk-fs-ratio", &val) == 0) |
| 1834 | mcasp->auxclk_fs_ratio = val; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1835 | |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1836 | if (of_property_read_u32(np, "dismod", &val) == 0) { |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 1837 | 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 Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1847 | out: |
| 1848 | mcasp->pdata = pdata; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1849 | |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 1850 | 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, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1858 | } |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 1859 | |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1860 | 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, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1874 | } |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 1875 | |
| 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, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1891 | } |
| 1892 | |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1893 | enum { |
| 1894 | PCM_EDMA, |
| 1895 | PCM_SDMA, |
Peter Ujfalusi | fb0c3c6 | 2020-02-10 16:09:50 +0200 | [diff] [blame] | 1896 | PCM_UDMA, |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1897 | }; |
| 1898 | static const char *sdma_prefix = "ti,omap"; |
| 1899 | |
| 1900 | static 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 Ujfalusi | f0c9713 | 2019-11-13 11:54:45 +0200 | [diff] [blame] | 1910 | chan = dma_request_chan(mcasp->dev, tmp); |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1911 | 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 Yang | a697ae6 | 2020-04-25 20:48:35 +0800 | [diff] [blame] | 1918 | if (WARN_ON(!chan->device || !chan->device->dev)) { |
| 1919 | dma_release_channel(chan); |
Takashi Iwai | befff4f | 2017-09-07 10:59:17 +0200 | [diff] [blame] | 1920 | return -EINVAL; |
Xiyu Yang | a697ae6 | 2020-04-25 20:48:35 +0800 | [diff] [blame] | 1921 | } |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1922 | |
| 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 Ujfalusi | fb0c3c6 | 2020-02-10 16:09:50 +0200 | [diff] [blame] | 1936 | else if (strstr(tmp, "udmap")) |
| 1937 | return PCM_UDMA; |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1938 | |
| 1939 | return PCM_EDMA; |
| 1940 | } |
| 1941 | |
Peter Ujfalusi | 9ac0013 | 2016-06-02 12:55:05 +0300 | [diff] [blame] | 1942 | static 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 | |
| 1965 | static 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 Ujfalusi | 540f1ba7 | 2019-01-03 16:05:52 +0200 | [diff] [blame] | 1988 | #ifdef CONFIG_GPIOLIB |
| 1989 | static 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 | |
| 2004 | static 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 | |
| 2017 | static 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 | |
| 2040 | static 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 | |
| 2051 | static 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 | |
| 2069 | static 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 | |
| 2081 | static 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 | |
| 2094 | static 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 | |
| 2107 | static int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp) |
| 2108 | { |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 2109 | if (!davinci_mcasp_have_gpiochip(mcasp)) |
Peter Ujfalusi | 540f1ba7 | 2019-01-03 16:05:52 +0200 | [diff] [blame] | 2110 | 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 */ |
| 2123 | static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp) |
| 2124 | { |
| 2125 | return 0; |
| 2126 | } |
| 2127 | #endif /* CONFIG_GPIOLIB */ |
| 2128 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2129 | static int davinci_mcasp_probe(struct platform_device *pdev) |
| 2130 | { |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 2131 | struct snd_dmaengine_dai_dma_data *dma_data; |
Peter Ujfalusi | db8793a | 2020-11-06 09:25:49 +0200 | [diff] [blame] | 2132 | struct resource *mem, *dat; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2133 | struct davinci_mcasp *mcasp; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2134 | char *irq_name; |
| 2135 | int irq; |
Julia Lawall | 96d31e2 | 2011-12-29 17:51:21 +0100 | [diff] [blame] | 2136 | int ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2137 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 2138 | 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 Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2143 | mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp), |
Julia Lawall | 96d31e2 | 2011-12-29 17:51:21 +0100 | [diff] [blame] | 2144 | GFP_KERNEL); |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2145 | if (!mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2146 | return -ENOMEM; |
| 2147 | |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 2148 | mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu"); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2149 | if (!mem) { |
Peter Ujfalusi | 18096cb | 2020-10-08 11:54:00 +0300 | [diff] [blame] | 2150 | dev_warn(&pdev->dev, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 2151 | "\"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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2157 | } |
| 2158 | |
Axel Lin | 508a43f | 2015-08-24 16:47:36 +0800 | [diff] [blame] | 2159 | mcasp->base = devm_ioremap_resource(&pdev->dev, mem); |
| 2160 | if (IS_ERR(mcasp->base)) |
| 2161 | return PTR_ERR(mcasp->base); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2162 | |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 2163 | dev_set_drvdata(&pdev->dev, mcasp); |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 2164 | pm_runtime_enable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2165 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2166 | mcasp->dev = &pdev->dev; |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 2167 | ret = davinci_mcasp_get_config(mcasp, pdev); |
| 2168 | if (ret) |
| 2169 | goto err; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2170 | |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 2171 | /* 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 Ujfalusi | 372c4bd | 2020-11-06 09:25:48 +0200 | [diff] [blame] | 2180 | irq = platform_get_irq_byname_optional(pdev, "common"); |
| 2181 | if (irq > 0) { |
Peter Ujfalusi | ab1fffe | 2015-09-18 15:02:50 +0300 | [diff] [blame] | 2182 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common", |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 2183 | dev_name(&pdev->dev)); |
Arvind Yadav | 0c8b794 | 2017-09-20 15:36:09 +0530 | [diff] [blame] | 2184 | if (!irq_name) { |
| 2185 | ret = -ENOMEM; |
| 2186 | goto err; |
| 2187 | } |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 2188 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 2189 | davinci_mcasp_common_irq_handler, |
Peter Ujfalusi | 8f511ff | 2015-02-02 14:38:32 +0200 | [diff] [blame] | 2190 | IRQF_ONESHOT | IRQF_SHARED, |
| 2191 | irq_name, mcasp); |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 2192 | 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 Ujfalusi | 372c4bd | 2020-11-06 09:25:48 +0200 | [diff] [blame] | 2201 | irq = platform_get_irq_byname_optional(pdev, "rx"); |
| 2202 | if (irq > 0) { |
Peter Ujfalusi | ab1fffe | 2015-09-18 15:02:50 +0300 | [diff] [blame] | 2203 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx", |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2204 | dev_name(&pdev->dev)); |
Arvind Yadav | 0c8b794 | 2017-09-20 15:36:09 +0530 | [diff] [blame] | 2205 | if (!irq_name) { |
| 2206 | ret = -ENOMEM; |
| 2207 | goto err; |
| 2208 | } |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2209 | 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 Ujfalusi | 372c4bd | 2020-11-06 09:25:48 +0200 | [diff] [blame] | 2220 | irq = platform_get_irq_byname_optional(pdev, "tx"); |
| 2221 | if (irq > 0) { |
Peter Ujfalusi | ab1fffe | 2015-09-18 15:02:50 +0300 | [diff] [blame] | 2222 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx", |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2223 | dev_name(&pdev->dev)); |
Arvind Yadav | 0c8b794 | 2017-09-20 15:36:09 +0530 | [diff] [blame] | 2224 | if (!irq_name) { |
| 2225 | ret = -ENOMEM; |
| 2226 | goto err; |
| 2227 | } |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2228 | 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 Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 2239 | dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat"); |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2240 | if (dat) |
| 2241 | mcasp->dat_port = true; |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 2242 | |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 2243 | dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK]; |
Peter Ujfalusi | db8793a | 2020-11-06 09:25:49 +0200 | [diff] [blame] | 2244 | dma_data->filter_data = "tx"; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2245 | if (dat) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2246 | dma_data->addr = dat->start; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2247 | else |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 2248 | dma_data->addr = mem->start + davinci_mcasp_txdma_offset(mcasp->pdata); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2249 | |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 2250 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2251 | /* RX is not valid in DIT mode */ |
| 2252 | if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) { |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2253 | dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE]; |
Peter Ujfalusi | db8793a | 2020-11-06 09:25:49 +0200 | [diff] [blame] | 2254 | dma_data->filter_data = "rx"; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2255 | if (dat) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2256 | dma_data->addr = dat->start; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2257 | else |
Peter Ujfalusi | 9ac0013 | 2016-06-02 12:55:05 +0300 | [diff] [blame] | 2258 | dma_data->addr = |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 2259 | mem->start + davinci_mcasp_rxdma_offset(mcasp->pdata); |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2260 | } |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 2261 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2262 | if (mcasp->version < MCASP_VERSION_3) { |
| 2263 | mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE; |
Peter Ujfalusi | 64ebdec | 2014-03-07 15:03:55 +0200 | [diff] [blame] | 2264 | /* dma_params->dma_addr is pointing to the data port address */ |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2265 | mcasp->dat_port = true; |
| 2266 | } else { |
| 2267 | mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE; |
| 2268 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2269 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 2270 | /* 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 Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2278 | devm_kcalloc(mcasp->dev, |
| 2279 | 32 + mcasp->num_serializer - 1, |
| 2280 | sizeof(unsigned int), |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 2281 | GFP_KERNEL); |
| 2282 | |
| 2283 | mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list = |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2284 | devm_kcalloc(mcasp->dev, |
| 2285 | 32 + mcasp->num_serializer - 1, |
| 2286 | sizeof(unsigned int), |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 2287 | GFP_KERNEL); |
| 2288 | |
| 2289 | if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list || |
Christophe Jaillet | 1b8b68b | 2017-09-16 07:40:29 +0200 | [diff] [blame] | 2290 | !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) { |
| 2291 | ret = -ENOMEM; |
| 2292 | goto err; |
| 2293 | } |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 2294 | |
| 2295 | ret = davinci_mcasp_set_ch_constraints(mcasp); |
Jyri Sarha | 5935a05 | 2015-04-23 16:16:05 +0300 | [diff] [blame] | 2296 | if (ret) |
| 2297 | goto err; |
| 2298 | |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 2299 | mcasp_reparent_fck(pdev); |
| 2300 | |
Peter Ujfalusi | 1125d92 | 2020-11-06 09:25:50 +0200 | [diff] [blame] | 2301 | ret = devm_snd_soc_register_component(&pdev->dev, &davinci_mcasp_component, |
| 2302 | &davinci_mcasp_dai[mcasp->op_mode], 1); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2303 | |
| 2304 | if (ret != 0) |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 2305 | goto err; |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 2306 | |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 2307 | ret = davinci_mcasp_get_dma_type(mcasp); |
| 2308 | switch (ret) { |
| 2309 | case PCM_EDMA: |
Peter Ujfalusi | f3f9cfa | 2014-07-16 15:12:04 +0300 | [diff] [blame] | 2310 | ret = edma_pcm_platform_register(&pdev->dev); |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 2311 | break; |
| 2312 | case PCM_SDMA: |
Janusz Krzysztofik | 3e802e9 | 2019-06-02 16:55:49 +0200 | [diff] [blame] | 2313 | ret = sdma_pcm_platform_register(&pdev->dev, "tx", "rx"); |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 2314 | break; |
Peter Ujfalusi | fb0c3c6 | 2020-02-10 16:09:50 +0200 | [diff] [blame] | 2315 | case PCM_UDMA: |
| 2316 | ret = udma_pcm_platform_register(&pdev->dev); |
| 2317 | break; |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 2318 | default: |
| 2319 | dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret); |
| 2320 | case -EPROBE_DEFER: |
| 2321 | goto err; |
Peter Ujfalusi | d5c6c59 | 2014-04-16 15:46:20 +0300 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | if (ret) { |
| 2325 | dev_err(&pdev->dev, "register PCM failed: %d\n", ret); |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 2326 | goto err; |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 2327 | } |
| 2328 | |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 2329 | no_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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2335 | |
Peter Ujfalusi | 1b4fb70 | 2020-11-06 09:25:51 +0200 | [diff] [blame] | 2336 | return 0; |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 2337 | err: |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 2338 | pm_runtime_disable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2339 | return ret; |
| 2340 | } |
| 2341 | |
| 2342 | static int davinci_mcasp_remove(struct platform_device *pdev) |
| 2343 | { |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 2344 | pm_runtime_disable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2345 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2346 | return 0; |
| 2347 | } |
| 2348 | |
Peter Ujfalusi | 6175471 | 2019-01-03 16:05:50 +0200 | [diff] [blame] | 2349 | #ifdef CONFIG_PM |
| 2350 | static 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 | |
| 2376 | static 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 | |
| 2404 | static 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 S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2410 | static struct platform_driver davinci_mcasp_driver = { |
| 2411 | .probe = davinci_mcasp_probe, |
| 2412 | .remove = davinci_mcasp_remove, |
| 2413 | .driver = { |
| 2414 | .name = "davinci-mcasp", |
Peter Ujfalusi | 6175471 | 2019-01-03 16:05:50 +0200 | [diff] [blame] | 2415 | .pm = &davinci_mcasp_pm_ops, |
Sachin Kamat | ea421eb | 2013-05-22 16:53:37 +0530 | [diff] [blame] | 2416 | .of_match_table = mcasp_dt_ids, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2417 | }, |
| 2418 | }; |
| 2419 | |
Axel Lin | f9b8a51 | 2011-11-25 10:09:27 +0800 | [diff] [blame] | 2420 | module_platform_driver(davinci_mcasp_driver); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2421 | |
| 2422 | MODULE_AUTHOR("Steve Chen"); |
| 2423 | MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface"); |
| 2424 | MODULE_LICENSE("GPL"); |