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 | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 79 | void __iomem *base; |
Peter Ujfalusi | 487dce8 | 2013-11-14 11:35:31 +0200 | [diff] [blame] | 80 | u32 fifo_base; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 81 | struct device *dev; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 82 | struct snd_pcm_substream *substreams[2]; |
Peter Ujfalusi | 4a11ff2 | 2016-03-11 13:18:51 +0200 | [diff] [blame] | 83 | unsigned int dai_fmt; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 84 | |
| 85 | /* McASP specific data */ |
| 86 | int tdm_slots; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 87 | u32 tdm_mask[2]; |
| 88 | int slot_width; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 89 | u8 op_mode; |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 90 | u8 dismod; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 91 | u8 num_serializer; |
| 92 | u8 *serial_dir; |
| 93 | u8 version; |
Daniel Mack | 8267525 | 2014-07-16 14:04:41 +0200 | [diff] [blame] | 94 | u8 bclk_div; |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 95 | int streams; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 96 | u32 irq_request[2]; |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 97 | int dma_request[2]; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 98 | |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 99 | int sysclk_freq; |
| 100 | bool bclk_master; |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 101 | u32 auxclk_fs_ratio; |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 102 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 103 | unsigned long pdir; /* Pin direction bitfield */ |
| 104 | |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 105 | /* McASP FIFO related */ |
| 106 | u8 txnumevt; |
| 107 | u8 rxnumevt; |
| 108 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 109 | bool dat_port; |
| 110 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 111 | /* Used for comstraint setting on the second stream */ |
| 112 | u32 channels; |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 113 | int max_format_width; |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 114 | u8 active_serializers[2]; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 115 | |
Peter Ujfalusi | 540f1ba7 | 2019-01-03 16:05:52 +0200 | [diff] [blame] | 116 | #ifdef CONFIG_GPIOLIB |
| 117 | struct gpio_chip gpio_chip; |
| 118 | #endif |
| 119 | |
Peter Ujfalusi | 6175471 | 2019-01-03 16:05:50 +0200 | [diff] [blame] | 120 | #ifdef CONFIG_PM |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 121 | struct davinci_mcasp_context context; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 122 | #endif |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 123 | |
| 124 | struct davinci_mcasp_ruledata ruledata[2]; |
Jyri Sarha | 5935a05 | 2015-04-23 16:16:05 +0300 | [diff] [blame] | 125 | struct snd_pcm_hw_constraint_list chconstr[2]; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 128 | static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 129 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 130 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 131 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 132 | __raw_writel(__raw_readl(reg) | val, reg); |
| 133 | } |
| 134 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 135 | static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 136 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 137 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 138 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 139 | __raw_writel((__raw_readl(reg) & ~(val)), reg); |
| 140 | } |
| 141 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 142 | static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 143 | u32 val, u32 mask) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 144 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 145 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 146 | __raw_writel((__raw_readl(reg) & ~mask) | val, reg); |
| 147 | } |
| 148 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 149 | static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset, |
| 150 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 151 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 152 | __raw_writel(val, mcasp->base + offset); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 155 | 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] | 156 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 157 | return (u32)__raw_readl(mcasp->base + offset); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 160 | 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] | 161 | { |
| 162 | int i = 0; |
| 163 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 164 | mcasp_set_bits(mcasp, ctl_reg, val); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 165 | |
| 166 | /* programming GBLCTL needs to read back from GBLCTL and verfiy */ |
| 167 | /* loop count is to avoid the lock-up */ |
| 168 | for (i = 0; i < 1000; i++) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 169 | if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 173 | if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val)) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 174 | printk(KERN_ERR "GBLCTL write error\n"); |
| 175 | } |
| 176 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 177 | static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp) |
| 178 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 179 | u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG); |
| 180 | u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 181 | |
| 182 | return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE; |
| 183 | } |
| 184 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 185 | static inline void mcasp_set_clk_pdir(struct davinci_mcasp *mcasp, bool enable) |
| 186 | { |
| 187 | u32 bit = PIN_BIT_AMUTE; |
| 188 | |
| 189 | for_each_set_bit_from(bit, &mcasp->pdir, PIN_BIT_AFSR + 1) { |
| 190 | if (enable) |
| 191 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit)); |
| 192 | else |
| 193 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit)); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static inline void mcasp_set_axr_pdir(struct davinci_mcasp *mcasp, bool enable) |
| 198 | { |
| 199 | u32 bit; |
| 200 | |
Peter Ujfalusi | 34a2a80 | 2019-07-25 11:34:23 +0300 | [diff] [blame] | 201 | for_each_set_bit(bit, &mcasp->pdir, PIN_BIT_AMUTE) { |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 202 | if (enable) |
| 203 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit)); |
| 204 | else |
| 205 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit)); |
| 206 | } |
| 207 | } |
| 208 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 209 | static void mcasp_start_rx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 210 | { |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 211 | if (mcasp->rxnumevt) { /* enable FIFO */ |
| 212 | u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 213 | |
| 214 | mcasp_clr_bits(mcasp, reg, FIFO_ENABLE); |
| 215 | mcasp_set_bits(mcasp, reg, FIFO_ENABLE); |
| 216 | } |
| 217 | |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 218 | /* Start clocks */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 219 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST); |
| 220 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 221 | /* |
| 222 | * When ASYNC == 0 the transmit and receive sections operate |
| 223 | * synchronously from the transmit clock and frame sync. We need to make |
| 224 | * sure that the TX signlas are enabled when starting reception. |
| 225 | */ |
| 226 | if (mcasp_is_synchronous(mcasp)) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 227 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST); |
| 228 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST); |
Peter Ujfalusi | 34a2a80 | 2019-07-25 11:34:23 +0300 | [diff] [blame] | 229 | mcasp_set_clk_pdir(mcasp, true); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 232 | /* Activate serializer(s) */ |
Peter Ujfalusi | 1003c27 | 2018-11-16 15:41:38 +0200 | [diff] [blame] | 233 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 234 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR); |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 235 | /* Release RX state machine */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 236 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST); |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 237 | /* Release Frame Sync generator */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 238 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 239 | if (mcasp_is_synchronous(mcasp)) |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 240 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 241 | |
| 242 | /* enable receive IRQs */ |
| 243 | mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG, |
| 244 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 247 | static void mcasp_start_tx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 248 | { |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 249 | u32 cnt; |
| 250 | |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 251 | if (mcasp->txnumevt) { /* enable FIFO */ |
| 252 | u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 253 | |
| 254 | mcasp_clr_bits(mcasp, reg, FIFO_ENABLE); |
| 255 | mcasp_set_bits(mcasp, reg, FIFO_ENABLE); |
| 256 | } |
| 257 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 258 | /* Start clocks */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 259 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST); |
| 260 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 261 | mcasp_set_clk_pdir(mcasp, true); |
| 262 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 263 | /* Activate serializer(s) */ |
Peter Ujfalusi | 1003c27 | 2018-11-16 15:41:38 +0200 | [diff] [blame] | 264 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 265 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 266 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 267 | /* wait for XDATA to be cleared */ |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 268 | cnt = 0; |
Peter Ujfalusi | e2a0c9f | 2015-12-11 13:06:24 +0200 | [diff] [blame] | 269 | while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) && |
| 270 | (cnt < 100000)) |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 271 | cnt++; |
| 272 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 273 | mcasp_set_axr_pdir(mcasp, true); |
| 274 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 275 | /* Release TX state machine */ |
| 276 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST); |
| 277 | /* Release Frame Sync generator */ |
| 278 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 279 | |
| 280 | /* enable transmit IRQs */ |
| 281 | mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG, |
| 282 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 285 | static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 286 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 287 | mcasp->streams++; |
| 288 | |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 289 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 290 | mcasp_start_tx(mcasp); |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 291 | else |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 292 | mcasp_start_rx(mcasp); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 295 | static void mcasp_stop_rx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 296 | { |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 297 | /* disable IRQ sources */ |
| 298 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG, |
| 299 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]); |
| 300 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 301 | /* |
| 302 | * In synchronous mode stop the TX clocks if no other stream is |
| 303 | * running |
| 304 | */ |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 305 | if (mcasp_is_synchronous(mcasp) && !mcasp->streams) { |
| 306 | mcasp_set_clk_pdir(mcasp, false); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 307 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 308 | } |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 309 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 310 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0); |
| 311 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 312 | |
| 313 | if (mcasp->rxnumevt) { /* disable FIFO */ |
| 314 | u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 315 | |
| 316 | mcasp_clr_bits(mcasp, reg, FIFO_ENABLE); |
| 317 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 320 | static void mcasp_stop_tx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 321 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 322 | u32 val = 0; |
| 323 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 324 | /* disable IRQ sources */ |
| 325 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG, |
| 326 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]); |
| 327 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 328 | /* |
| 329 | * In synchronous mode keep TX clocks running if the capture stream is |
| 330 | * still running. |
| 331 | */ |
| 332 | if (mcasp_is_synchronous(mcasp) && mcasp->streams) |
| 333 | val = TXHCLKRST | TXCLKRST | TXFSRST; |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 334 | else |
| 335 | mcasp_set_clk_pdir(mcasp, false); |
| 336 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 337 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 338 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val); |
| 339 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 340 | |
| 341 | if (mcasp->txnumevt) { /* disable FIFO */ |
| 342 | u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 343 | |
| 344 | mcasp_clr_bits(mcasp, reg, FIFO_ENABLE); |
| 345 | } |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 346 | |
| 347 | mcasp_set_axr_pdir(mcasp, false); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 348 | } |
| 349 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 350 | static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 351 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 352 | mcasp->streams--; |
| 353 | |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 354 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 355 | mcasp_stop_tx(mcasp); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 356 | else |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 357 | mcasp_stop_rx(mcasp); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 358 | } |
| 359 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 360 | static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data) |
| 361 | { |
| 362 | struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data; |
| 363 | struct snd_pcm_substream *substream; |
| 364 | u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]; |
| 365 | u32 handled_mask = 0; |
| 366 | u32 stat; |
| 367 | |
| 368 | stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG); |
| 369 | if (stat & XUNDRN & irq_mask) { |
| 370 | dev_warn(mcasp->dev, "Transmit buffer underflow\n"); |
| 371 | handled_mask |= XUNDRN; |
| 372 | |
| 373 | substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK]; |
Takashi Iwai | dae35d1 | 2018-07-04 16:01:43 +0200 | [diff] [blame] | 374 | if (substream) |
| 375 | snd_pcm_stop_xrun(substream); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | if (!handled_mask) |
| 379 | dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n", |
| 380 | stat); |
| 381 | |
| 382 | if (stat & XRERR) |
| 383 | handled_mask |= XRERR; |
| 384 | |
| 385 | /* Ack the handled event only */ |
| 386 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask); |
| 387 | |
| 388 | return IRQ_RETVAL(handled_mask); |
| 389 | } |
| 390 | |
| 391 | static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data) |
| 392 | { |
| 393 | struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data; |
| 394 | struct snd_pcm_substream *substream; |
| 395 | u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]; |
| 396 | u32 handled_mask = 0; |
| 397 | u32 stat; |
| 398 | |
| 399 | stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG); |
| 400 | if (stat & ROVRN & irq_mask) { |
| 401 | dev_warn(mcasp->dev, "Receive buffer overflow\n"); |
| 402 | handled_mask |= ROVRN; |
| 403 | |
| 404 | substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE]; |
Takashi Iwai | dae35d1 | 2018-07-04 16:01:43 +0200 | [diff] [blame] | 405 | if (substream) |
| 406 | snd_pcm_stop_xrun(substream); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (!handled_mask) |
| 410 | dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n", |
| 411 | stat); |
| 412 | |
| 413 | if (stat & XRERR) |
| 414 | handled_mask |= XRERR; |
| 415 | |
| 416 | /* Ack the handled event only */ |
| 417 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask); |
| 418 | |
| 419 | return IRQ_RETVAL(handled_mask); |
| 420 | } |
| 421 | |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 422 | static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data) |
| 423 | { |
| 424 | struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data; |
| 425 | irqreturn_t ret = IRQ_NONE; |
| 426 | |
| 427 | if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK]) |
| 428 | ret = davinci_mcasp_tx_irq_handler(irq, data); |
| 429 | |
| 430 | if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE]) |
| 431 | ret |= davinci_mcasp_rx_irq_handler(irq, data); |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 436 | static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
| 437 | unsigned int fmt) |
| 438 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 439 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 440 | int ret = 0; |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 441 | u32 data_delay; |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 442 | bool fs_pol_rising; |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 443 | bool inv_fs = false; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 444 | |
Peter Ujfalusi | 4a11ff2 | 2016-03-11 13:18:51 +0200 | [diff] [blame] | 445 | if (!fmt) |
| 446 | return 0; |
| 447 | |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 448 | pm_runtime_get_sync(mcasp->dev); |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 449 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
Peter Ujfalusi | 188edc5 | 2014-04-04 14:31:43 +0300 | [diff] [blame] | 450 | case SND_SOC_DAIFMT_DSP_A: |
| 451 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR); |
| 452 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR); |
Peter Ujfalusi | 188edc5 | 2014-04-04 14:31:43 +0300 | [diff] [blame] | 453 | /* 1st data bit occur one ACLK cycle after the frame sync */ |
| 454 | data_delay = 1; |
| 455 | break; |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 456 | case SND_SOC_DAIFMT_DSP_B: |
| 457 | case SND_SOC_DAIFMT_AC97: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 458 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR); |
| 459 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR); |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 460 | /* No delay after FS */ |
| 461 | data_delay = 0; |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 462 | break; |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 463 | case SND_SOC_DAIFMT_I2S: |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 464 | /* configure a full-word SYNC pulse (LRCLK) */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 465 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR); |
| 466 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR); |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 467 | /* 1st data bit occur one ACLK cycle after the frame sync */ |
| 468 | data_delay = 1; |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 469 | /* FS need to be inverted */ |
| 470 | inv_fs = true; |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 471 | break; |
Peter Ujfalusi | 816fe20 | 2019-07-25 11:34:11 +0300 | [diff] [blame] | 472 | case SND_SOC_DAIFMT_RIGHT_J: |
Peter Ujfalusi | 423761e | 2014-04-04 14:31:46 +0300 | [diff] [blame] | 473 | case SND_SOC_DAIFMT_LEFT_J: |
| 474 | /* configure a full-word SYNC pulse (LRCLK) */ |
| 475 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR); |
| 476 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR); |
| 477 | /* No delay after FS */ |
| 478 | data_delay = 0; |
| 479 | break; |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 480 | default: |
| 481 | ret = -EINVAL; |
| 482 | goto out; |
Daniel Mack | 5296cf2 | 2012-10-04 15:08:42 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 485 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay), |
| 486 | FSXDLY(3)); |
| 487 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay), |
| 488 | FSRDLY(3)); |
| 489 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 490 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 491 | case SND_SOC_DAIFMT_CBS_CFS: |
| 492 | /* codec is clock and frame slave */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 493 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE); |
| 494 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 495 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 496 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE); |
| 497 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 498 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 499 | /* BCLK */ |
| 500 | set_bit(PIN_BIT_ACLKX, &mcasp->pdir); |
| 501 | set_bit(PIN_BIT_ACLKR, &mcasp->pdir); |
| 502 | /* Frame Sync */ |
| 503 | set_bit(PIN_BIT_AFSX, &mcasp->pdir); |
| 504 | set_bit(PIN_BIT_AFSR, &mcasp->pdir); |
| 505 | |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 506 | mcasp->bclk_master = 1; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 507 | break; |
Peter Ujfalusi | 226e2f1 | 2015-02-12 16:41:26 +0200 | [diff] [blame] | 508 | case SND_SOC_DAIFMT_CBS_CFM: |
| 509 | /* codec is clock slave and frame master */ |
| 510 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE); |
| 511 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE); |
| 512 | |
| 513 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE); |
| 514 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE); |
| 515 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 516 | /* BCLK */ |
| 517 | set_bit(PIN_BIT_ACLKX, &mcasp->pdir); |
| 518 | set_bit(PIN_BIT_ACLKR, &mcasp->pdir); |
| 519 | /* Frame Sync */ |
| 520 | clear_bit(PIN_BIT_AFSX, &mcasp->pdir); |
| 521 | clear_bit(PIN_BIT_AFSR, &mcasp->pdir); |
| 522 | |
Peter Ujfalusi | 226e2f1 | 2015-02-12 16:41:26 +0200 | [diff] [blame] | 523 | mcasp->bclk_master = 1; |
| 524 | break; |
Chaithrika U S | 517ee6c | 2009-08-11 16:59:12 -0400 | [diff] [blame] | 525 | case SND_SOC_DAIFMT_CBM_CFS: |
| 526 | /* codec is clock master and frame slave */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 527 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE); |
| 528 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE); |
Chaithrika U S | 517ee6c | 2009-08-11 16:59:12 -0400 | [diff] [blame] | 529 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 530 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE); |
| 531 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE); |
Chaithrika U S | 517ee6c | 2009-08-11 16:59:12 -0400 | [diff] [blame] | 532 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 533 | /* BCLK */ |
| 534 | clear_bit(PIN_BIT_ACLKX, &mcasp->pdir); |
| 535 | clear_bit(PIN_BIT_ACLKR, &mcasp->pdir); |
| 536 | /* Frame Sync */ |
| 537 | set_bit(PIN_BIT_AFSX, &mcasp->pdir); |
| 538 | set_bit(PIN_BIT_AFSR, &mcasp->pdir); |
| 539 | |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 540 | mcasp->bclk_master = 0; |
Chaithrika U S | 517ee6c | 2009-08-11 16:59:12 -0400 | [diff] [blame] | 541 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 542 | case SND_SOC_DAIFMT_CBM_CFM: |
| 543 | /* codec is clock and frame master */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 544 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE); |
| 545 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 546 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 547 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE); |
| 548 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 549 | |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 550 | /* BCLK */ |
| 551 | clear_bit(PIN_BIT_ACLKX, &mcasp->pdir); |
| 552 | clear_bit(PIN_BIT_ACLKR, &mcasp->pdir); |
| 553 | /* Frame Sync */ |
| 554 | clear_bit(PIN_BIT_AFSX, &mcasp->pdir); |
| 555 | clear_bit(PIN_BIT_AFSR, &mcasp->pdir); |
| 556 | |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 557 | mcasp->bclk_master = 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 558 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 559 | default: |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 560 | ret = -EINVAL; |
| 561 | goto out; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 565 | case SND_SOC_DAIFMT_IB_NF: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 566 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL); |
Peter Ujfalusi | 74ddd8c | 2014-04-04 14:31:41 +0300 | [diff] [blame] | 567 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL); |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 568 | fs_pol_rising = true; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 569 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 570 | case SND_SOC_DAIFMT_NB_IF: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 571 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL); |
Peter Ujfalusi | 74ddd8c | 2014-04-04 14:31:41 +0300 | [diff] [blame] | 572 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL); |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 573 | fs_pol_rising = false; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 574 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 575 | case SND_SOC_DAIFMT_IB_IF: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 576 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL); |
Peter Ujfalusi | 74ddd8c | 2014-04-04 14:31:41 +0300 | [diff] [blame] | 577 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL); |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 578 | fs_pol_rising = false; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 579 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 580 | case SND_SOC_DAIFMT_NB_NF: |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 581 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 582 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL); |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 583 | fs_pol_rising = true; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 584 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 585 | default: |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 586 | ret = -EINVAL; |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 587 | goto out; |
| 588 | } |
| 589 | |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 590 | if (inv_fs) |
| 591 | fs_pol_rising = !fs_pol_rising; |
| 592 | |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 593 | if (fs_pol_rising) { |
| 594 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL); |
| 595 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL); |
| 596 | } else { |
| 597 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL); |
| 598 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 599 | } |
Peter Ujfalusi | 4a11ff2 | 2016-03-11 13:18:51 +0200 | [diff] [blame] | 600 | |
| 601 | mcasp->dai_fmt = fmt; |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 602 | out: |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 603 | pm_runtime_put(mcasp->dev); |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 604 | return ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Peter Ujfalusi | 226e73e | 2016-05-09 13:42:30 +0300 | [diff] [blame] | 607 | 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] | 608 | int div, bool explicit) |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 609 | { |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 610 | pm_runtime_get_sync(mcasp->dev); |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 611 | switch (div_id) { |
Peter Ujfalusi | 20d4b10 | 2016-05-09 13:42:29 +0300 | [diff] [blame] | 612 | case MCASP_CLKDIV_AUXCLK: /* MCLK divider */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 613 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 614 | AHCLKXDIV(div - 1), AHCLKXDIV_MASK); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 615 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 616 | AHCLKRDIV(div - 1), AHCLKRDIV_MASK); |
| 617 | break; |
| 618 | |
Peter Ujfalusi | 20d4b10 | 2016-05-09 13:42:29 +0300 | [diff] [blame] | 619 | case MCASP_CLKDIV_BCLK: /* BCLK divider */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 620 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 621 | ACLKXDIV(div - 1), ACLKXDIV_MASK); |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 622 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 623 | ACLKRDIV(div - 1), ACLKRDIV_MASK); |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 624 | if (explicit) |
| 625 | mcasp->bclk_div = div; |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 626 | break; |
| 627 | |
Peter Ujfalusi | 20d4b10 | 2016-05-09 13:42:29 +0300 | [diff] [blame] | 628 | case MCASP_CLKDIV_BCLK_FS_RATIO: |
| 629 | /* |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 630 | * BCLK/LRCLK ratio descries how many bit-clock cycles |
| 631 | * fit into one frame. The clock ratio is given for a |
| 632 | * full period of data (for I2S format both left and |
| 633 | * right channels), so it has to be divided by number |
| 634 | * of tdm-slots (for I2S - divided by 2). |
| 635 | * Instead of storing this ratio, we calculate a new |
| 636 | * tdm_slot width by dividing the the ratio by the |
| 637 | * number of configured tdm slots. |
| 638 | */ |
| 639 | mcasp->slot_width = div / mcasp->tdm_slots; |
| 640 | if (div % mcasp->tdm_slots) |
| 641 | dev_warn(mcasp->dev, |
| 642 | "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots", |
| 643 | __func__, div, mcasp->tdm_slots); |
Daniel Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 644 | break; |
| 645 | |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 646 | default: |
| 647 | return -EINVAL; |
| 648 | } |
| 649 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 650 | pm_runtime_put(mcasp->dev); |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 654 | static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id, |
| 655 | int div) |
| 656 | { |
Peter Ujfalusi | 226e73e | 2016-05-09 13:42:30 +0300 | [diff] [blame] | 657 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
| 658 | |
| 659 | return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1); |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 660 | } |
| 661 | |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 662 | static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 663 | unsigned int freq, int dir) |
| 664 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 665 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 666 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 667 | pm_runtime_get_sync(mcasp->dev); |
Peter Ujfalusi | 253f584 | 2019-12-04 21:20:05 +0200 | [diff] [blame] | 668 | |
| 669 | if (dir == SND_SOC_CLOCK_IN) { |
| 670 | switch (clk_id) { |
| 671 | case MCASP_CLK_HCLK_AHCLK: |
| 672 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, |
| 673 | AHCLKXE); |
| 674 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, |
| 675 | AHCLKRE); |
| 676 | clear_bit(PIN_BIT_AHCLKX, &mcasp->pdir); |
| 677 | break; |
| 678 | case MCASP_CLK_HCLK_AUXCLK: |
| 679 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, |
| 680 | AHCLKXE); |
| 681 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, |
| 682 | AHCLKRE); |
| 683 | set_bit(PIN_BIT_AHCLKX, &mcasp->pdir); |
| 684 | break; |
| 685 | default: |
| 686 | dev_err(mcasp->dev, "Invalid clk id: %d\n", clk_id); |
| 687 | goto out; |
| 688 | } |
| 689 | } else { |
| 690 | /* Select AUXCLK as HCLK */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 691 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE); |
| 692 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 693 | set_bit(PIN_BIT_AHCLKX, &mcasp->pdir); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 694 | } |
Peter Ujfalusi | 253f584 | 2019-12-04 21:20:05 +0200 | [diff] [blame] | 695 | /* |
| 696 | * When AHCLK X/R is selected to be output it means that the HCLK is |
| 697 | * the same clock - coming via AUXCLK. |
| 698 | */ |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 699 | mcasp->sysclk_freq = freq; |
Peter Ujfalusi | 253f584 | 2019-12-04 21:20:05 +0200 | [diff] [blame] | 700 | out: |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 701 | pm_runtime_put(mcasp->dev); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 705 | /* All serializers must have equal number of channels */ |
| 706 | static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream, |
| 707 | int serializers) |
| 708 | { |
| 709 | struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream]; |
| 710 | unsigned int *list = (unsigned int *) cl->list; |
| 711 | int slots = mcasp->tdm_slots; |
| 712 | int i, count = 0; |
| 713 | |
| 714 | if (mcasp->tdm_mask[stream]) |
| 715 | slots = hweight32(mcasp->tdm_mask[stream]); |
| 716 | |
Peter Ujfalusi | e4798d2 | 2017-05-11 09:58:22 +0300 | [diff] [blame] | 717 | for (i = 1; i <= slots; i++) |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 718 | list[count++] = i; |
| 719 | |
| 720 | for (i = 2; i <= serializers; i++) |
| 721 | list[count++] = i*slots; |
| 722 | |
| 723 | cl->count = count; |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp) |
| 729 | { |
| 730 | int rx_serializers = 0, tx_serializers = 0, ret, i; |
| 731 | |
| 732 | for (i = 0; i < mcasp->num_serializer; i++) |
| 733 | if (mcasp->serial_dir[i] == TX_MODE) |
| 734 | tx_serializers++; |
| 735 | else if (mcasp->serial_dir[i] == RX_MODE) |
| 736 | rx_serializers++; |
| 737 | |
| 738 | ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK, |
| 739 | tx_serializers); |
| 740 | if (ret) |
| 741 | return ret; |
| 742 | |
| 743 | ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE, |
| 744 | rx_serializers); |
| 745 | |
| 746 | return ret; |
| 747 | } |
| 748 | |
| 749 | |
| 750 | static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai, |
| 751 | unsigned int tx_mask, |
| 752 | unsigned int rx_mask, |
| 753 | int slots, int slot_width) |
| 754 | { |
| 755 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
| 756 | |
| 757 | dev_dbg(mcasp->dev, |
| 758 | "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n", |
| 759 | __func__, tx_mask, rx_mask, slots, slot_width); |
| 760 | |
| 761 | if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) { |
| 762 | dev_err(mcasp->dev, |
| 763 | "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n", |
| 764 | tx_mask, rx_mask, slots); |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | |
| 768 | if (slot_width && |
| 769 | (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) { |
| 770 | dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n", |
| 771 | __func__, slot_width); |
| 772 | return -EINVAL; |
| 773 | } |
| 774 | |
| 775 | mcasp->tdm_slots = slots; |
Andreas Dannenberg | 1bdd593 | 2015-11-09 12:19:19 -0600 | [diff] [blame] | 776 | mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask; |
| 777 | mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 778 | mcasp->slot_width = slot_width; |
| 779 | |
| 780 | return davinci_mcasp_set_ch_constraints(mcasp); |
| 781 | } |
| 782 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 783 | static int davinci_config_channel_size(struct davinci_mcasp *mcasp, |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 784 | int sample_width) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 785 | { |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 786 | u32 fmt; |
Peter Ujfalusi | 816fe20 | 2019-07-25 11:34:11 +0300 | [diff] [blame] | 787 | u32 tx_rotate, rx_rotate, slot_width; |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 788 | u32 mask = (1ULL << sample_width) - 1; |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 789 | |
Peter Ujfalusi | 816fe20 | 2019-07-25 11:34:11 +0300 | [diff] [blame] | 790 | if (mcasp->slot_width) |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 791 | slot_width = mcasp->slot_width; |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 792 | else if (mcasp->max_format_width) |
| 793 | slot_width = mcasp->max_format_width; |
Peter Ujfalusi | 816fe20 | 2019-07-25 11:34:11 +0300 | [diff] [blame] | 794 | else |
| 795 | slot_width = sample_width; |
| 796 | /* |
| 797 | * TX rotation: |
| 798 | * right aligned formats: rotate w/ slot_width |
| 799 | * left aligned formats: rotate w/ sample_width |
| 800 | * |
| 801 | * RX rotation: |
| 802 | * right aligned formats: no rotation needed |
| 803 | * left aligned formats: rotate w/ (slot_width - sample_width) |
| 804 | */ |
| 805 | if ((mcasp->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) == |
| 806 | SND_SOC_DAIFMT_RIGHT_J) { |
| 807 | tx_rotate = (slot_width / 4) & 0x7; |
| 808 | rx_rotate = 0; |
| 809 | } else { |
| 810 | tx_rotate = (sample_width / 4) & 0x7; |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 811 | rx_rotate = (slot_width - sample_width) / 4; |
Peter Ujfalusi | d742b92 | 2014-11-10 12:32:19 +0200 | [diff] [blame] | 812 | } |
Daniel Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 813 | |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 814 | /* mapping of the XSSZ bit-field as described in the datasheet */ |
Jyri Sarha | 14a998b | 2015-09-17 10:39:05 +0300 | [diff] [blame] | 815 | fmt = (slot_width >> 1) - 1; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 816 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 817 | if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 818 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt), |
| 819 | RXSSZ(0x0F)); |
| 820 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt), |
| 821 | TXSSZ(0x0F)); |
| 822 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate), |
| 823 | TXROT(7)); |
| 824 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate), |
| 825 | RXROT(7)); |
| 826 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask); |
Yegor Yefremov | f5023af | 2013-04-04 16:13:20 +0200 | [diff] [blame] | 827 | } |
| 828 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 829 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask); |
Chaithrika U S | 0c31cf3 | 2009-09-15 18:13:29 -0400 | [diff] [blame] | 830 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | |
Peter Ujfalusi | 662ffae | 2014-01-30 15:15:22 +0200 | [diff] [blame] | 834 | static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream, |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 835 | int period_words, int channels) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 836 | { |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 837 | 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] | 838 | int i; |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 839 | u8 tx_ser = 0; |
| 840 | u8 rx_ser = 0; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 841 | u8 slots = mcasp->tdm_slots; |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 842 | u8 max_active_serializers = (channels + slots - 1) / slots; |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 843 | u8 max_rx_serializers, max_tx_serializers; |
Peter Ujfalusi | 7238319 | 2015-09-14 16:06:48 +0300 | [diff] [blame] | 844 | int active_serializers, numevt; |
Peter Ujfalusi | 487dce8 | 2013-11-14 11:35:31 +0200 | [diff] [blame] | 845 | u32 reg; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 846 | /* Default configuration */ |
Peter Ujfalusi | 40448e5 | 2014-04-04 15:56:30 +0300 | [diff] [blame] | 847 | if (mcasp->version < MCASP_VERSION_3) |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 848 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 849 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 850 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 851 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF); |
| 852 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS); |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 853 | max_tx_serializers = max_active_serializers; |
| 854 | max_rx_serializers = |
| 855 | mcasp->active_serializers[SNDRV_PCM_STREAM_CAPTURE]; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 856 | } else { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 857 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF); |
| 858 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS); |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 859 | max_tx_serializers = |
| 860 | mcasp->active_serializers[SNDRV_PCM_STREAM_PLAYBACK]; |
| 861 | max_rx_serializers = max_active_serializers; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 862 | } |
| 863 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 864 | for (i = 0; i < mcasp->num_serializer; i++) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 865 | mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
| 866 | mcasp->serial_dir[i]); |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 867 | if (mcasp->serial_dir[i] == TX_MODE && |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 868 | tx_ser < max_tx_serializers) { |
Misael Lopez Cruz | 19db62e | 2015-06-08 16:03:47 +0300 | [diff] [blame] | 869 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 870 | mcasp->dismod, DISMOD_MASK); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 871 | set_bit(PIN_BIT_AXR(i), &mcasp->pdir); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 872 | tx_ser++; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 873 | } else if (mcasp->serial_dir[i] == RX_MODE && |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 874 | rx_ser < max_rx_serializers) { |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 875 | clear_bit(PIN_BIT_AXR(i), &mcasp->pdir); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 876 | rx_ser++; |
Peter Ujfalusi | 5dd17a3 | 2019-06-20 12:20:01 +0300 | [diff] [blame] | 877 | } else { |
| 878 | /* Inactive or unused pin, set it to inactive */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 879 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
| 880 | SRMOD_INACTIVE, SRMOD_MASK); |
Peter Ujfalusi | 5dd17a3 | 2019-06-20 12:20:01 +0300 | [diff] [blame] | 881 | /* If unused, set DISMOD for the pin */ |
| 882 | if (mcasp->serial_dir[i] != INACTIVE_MODE) |
| 883 | mcasp_mod_bits(mcasp, |
| 884 | DAVINCI_MCASP_XRSRCTL_REG(i), |
| 885 | mcasp->dismod, DISMOD_MASK); |
Peter Ujfalusi | ca3d943 | 2018-11-16 15:41:39 +0200 | [diff] [blame] | 886 | clear_bit(PIN_BIT_AXR(i), &mcasp->pdir); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 890 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 891 | active_serializers = tx_ser; |
| 892 | numevt = mcasp->txnumevt; |
| 893 | reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 894 | } else { |
| 895 | active_serializers = rx_ser; |
| 896 | numevt = mcasp->rxnumevt; |
| 897 | reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 898 | } |
Daniel Mack | ecf327c | 2013-03-08 14:19:38 +0100 | [diff] [blame] | 899 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 900 | if (active_serializers < max_active_serializers) { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 901 | dev_warn(mcasp->dev, "stream has more channels (%d) than are " |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 902 | "enabled in mcasp (%d)\n", channels, |
| 903 | active_serializers * slots); |
Daniel Mack | ecf327c | 2013-03-08 14:19:38 +0100 | [diff] [blame] | 904 | return -EINVAL; |
| 905 | } |
| 906 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 907 | /* AFIFO is not in use */ |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 908 | if (!numevt) { |
| 909 | /* Configure the burst size for platform drivers */ |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 910 | if (active_serializers > 1) { |
| 911 | /* |
| 912 | * If more than one serializers are in use we have one |
| 913 | * DMA request to provide data for all serializers. |
| 914 | * For example if three serializers are enabled the DMA |
| 915 | * need to transfer three words per DMA request. |
| 916 | */ |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 917 | dma_data->maxburst = active_serializers; |
| 918 | } else { |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 919 | dma_data->maxburst = 0; |
| 920 | } |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 921 | |
| 922 | goto out; |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 923 | } |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 924 | |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 925 | if (period_words % active_serializers) { |
| 926 | dev_err(mcasp->dev, "Invalid combination of period words and " |
| 927 | "active serializers: %d, %d\n", period_words, |
| 928 | active_serializers); |
| 929 | return -EINVAL; |
| 930 | } |
| 931 | |
| 932 | /* |
| 933 | * Calculate the optimal AFIFO depth for platform side: |
| 934 | * The number of words for numevt need to be in steps of active |
| 935 | * serializers. |
| 936 | */ |
Peter Ujfalusi | 7238319 | 2015-09-14 16:06:48 +0300 | [diff] [blame] | 937 | numevt = (numevt / active_serializers) * active_serializers; |
| 938 | |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 939 | while (period_words % numevt && numevt > 0) |
| 940 | numevt -= active_serializers; |
| 941 | if (numevt <= 0) |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 942 | numevt = active_serializers; |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 943 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 944 | mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK); |
| 945 | mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK); |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 946 | |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 947 | /* Configure the burst size for platform drivers */ |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 948 | if (numevt == 1) |
| 949 | numevt = 0; |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 950 | dma_data->maxburst = numevt; |
| 951 | |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 952 | out: |
| 953 | mcasp->active_serializers[stream] = active_serializers; |
| 954 | |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 955 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 956 | } |
| 957 | |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 958 | static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream, |
| 959 | int channels) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 960 | { |
| 961 | int i, active_slots; |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 962 | int total_slots; |
| 963 | int active_serializers; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 964 | u32 mask = 0; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 965 | u32 busel = 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 966 | |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 967 | total_slots = mcasp->tdm_slots; |
| 968 | |
| 969 | /* |
| 970 | * If more than one serializer is needed, then use them with |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 971 | * all the specified tdm_slots. Otherwise, one serializer can |
| 972 | * cope with the transaction using just as many slots as there |
| 973 | * are channels in the stream. |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 974 | */ |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 975 | if (mcasp->tdm_mask[stream]) { |
| 976 | active_slots = hweight32(mcasp->tdm_mask[stream]); |
| 977 | active_serializers = (channels + active_slots - 1) / |
| 978 | active_slots; |
Peter Ujfalusi | fd14f44 | 2019-06-20 12:20:02 +0300 | [diff] [blame] | 979 | if (active_serializers == 1) |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 980 | active_slots = channels; |
Peter Ujfalusi | fd14f44 | 2019-06-20 12:20:02 +0300 | [diff] [blame] | 981 | for (i = 0; i < total_slots; i++) { |
| 982 | if ((1 << i) & mcasp->tdm_mask[stream]) { |
| 983 | mask |= (1 << i); |
| 984 | if (--active_slots <= 0) |
| 985 | break; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | } else { |
| 989 | active_serializers = (channels + total_slots - 1) / total_slots; |
| 990 | if (active_serializers == 1) |
| 991 | active_slots = channels; |
| 992 | else |
| 993 | active_slots = total_slots; |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 994 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 995 | for (i = 0; i < active_slots; i++) |
| 996 | mask |= (1 << i); |
| 997 | } |
Peter Ujfalusi | 5dd17a3 | 2019-06-20 12:20:01 +0300 | [diff] [blame] | 998 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 999 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 1000 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 1001 | if (!mcasp->dat_port) |
| 1002 | busel = TXSEL; |
| 1003 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1004 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 1005 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask); |
| 1006 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD); |
| 1007 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, |
| 1008 | FSXMOD(total_slots), FSXMOD(0x1FF)); |
| 1009 | } else if (stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 1010 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask); |
| 1011 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD); |
| 1012 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, |
| 1013 | FSRMOD(total_slots), FSRMOD(0x1FF)); |
Peter Ujfalusi | 0ad7d3a | 2015-11-23 12:51:53 +0200 | [diff] [blame] | 1014 | /* |
| 1015 | * If McASP is set to be TX/RX synchronous and the playback is |
| 1016 | * not running already we need to configure the TX slots in |
| 1017 | * order to have correct FSX on the bus |
| 1018 | */ |
| 1019 | if (mcasp_is_synchronous(mcasp) && !mcasp->channels) |
| 1020 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, |
| 1021 | FSXMOD(total_slots), FSXMOD(0x1FF)); |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1022 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1023 | |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1024 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* S/PDIF */ |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 1028 | static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp, |
| 1029 | unsigned int rate) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1030 | { |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 1031 | u32 cs_value = 0; |
| 1032 | u8 *cs_bytes = (u8*) &cs_value; |
| 1033 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1034 | /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0 |
| 1035 | and LSB first */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1036 | 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] | 1037 | |
| 1038 | /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1039 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1040 | |
| 1041 | /* Set the TX tdm : for all the slots */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1042 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1043 | |
| 1044 | /* Set the TX clock controls : div = 1 and internal */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1045 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1046 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1047 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1048 | |
| 1049 | /* Only 44100 and 48000 are valid, both have the same setting */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1050 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1051 | |
| 1052 | /* Enable the DIT */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 1053 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1054 | |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 1055 | /* Set S/PDIF channel status bits */ |
| 1056 | cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT; |
| 1057 | cs_bytes[1] = IEC958_AES1_CON_PCM_CODER; |
| 1058 | |
| 1059 | switch (rate) { |
| 1060 | case 22050: |
| 1061 | cs_bytes[3] |= IEC958_AES3_CON_FS_22050; |
| 1062 | break; |
| 1063 | case 24000: |
| 1064 | cs_bytes[3] |= IEC958_AES3_CON_FS_24000; |
| 1065 | break; |
| 1066 | case 32000: |
| 1067 | cs_bytes[3] |= IEC958_AES3_CON_FS_32000; |
| 1068 | break; |
| 1069 | case 44100: |
| 1070 | cs_bytes[3] |= IEC958_AES3_CON_FS_44100; |
| 1071 | break; |
| 1072 | case 48000: |
| 1073 | cs_bytes[3] |= IEC958_AES3_CON_FS_48000; |
| 1074 | break; |
| 1075 | case 88200: |
| 1076 | cs_bytes[3] |= IEC958_AES3_CON_FS_88200; |
| 1077 | break; |
| 1078 | case 96000: |
| 1079 | cs_bytes[3] |= IEC958_AES3_CON_FS_96000; |
| 1080 | break; |
| 1081 | case 176400: |
| 1082 | cs_bytes[3] |= IEC958_AES3_CON_FS_176400; |
| 1083 | break; |
| 1084 | case 192000: |
| 1085 | cs_bytes[3] |= IEC958_AES3_CON_FS_192000; |
| 1086 | break; |
| 1087 | default: |
| 1088 | printk(KERN_WARNING "unsupported sampling rate: %d\n", rate); |
| 1089 | return -EINVAL; |
| 1090 | } |
| 1091 | |
| 1092 | mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value); |
| 1093 | mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value); |
| 1094 | |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1095 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1096 | } |
| 1097 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1098 | static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp, |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1099 | unsigned int sysclk_freq, |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1100 | unsigned int bclk_freq, bool set) |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1101 | { |
Peter Ujfalusi | ddecd14 | 2016-05-09 13:42:32 +0300 | [diff] [blame] | 1102 | u32 reg = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG); |
| 1103 | int div = sysclk_freq / bclk_freq; |
| 1104 | int rem = sysclk_freq % bclk_freq; |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1105 | int error_ppm; |
Peter Ujfalusi | ddecd14 | 2016-05-09 13:42:32 +0300 | [diff] [blame] | 1106 | int aux_div = 1; |
| 1107 | |
| 1108 | if (div > (ACLKXDIV_MASK + 1)) { |
| 1109 | if (reg & AHCLKXE) { |
| 1110 | aux_div = div / (ACLKXDIV_MASK + 1); |
| 1111 | if (div % (ACLKXDIV_MASK + 1)) |
| 1112 | aux_div++; |
| 1113 | |
| 1114 | sysclk_freq /= aux_div; |
| 1115 | div = sysclk_freq / bclk_freq; |
| 1116 | rem = sysclk_freq % bclk_freq; |
| 1117 | } else if (set) { |
| 1118 | dev_warn(mcasp->dev, "Too fast reference clock (%u)\n", |
| 1119 | sysclk_freq); |
| 1120 | } |
| 1121 | } |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1122 | |
| 1123 | if (rem != 0) { |
| 1124 | if (div == 0 || |
Peter Ujfalusi | ddecd14 | 2016-05-09 13:42:32 +0300 | [diff] [blame] | 1125 | ((sysclk_freq / div) - bclk_freq) > |
| 1126 | (bclk_freq - (sysclk_freq / (div+1)))) { |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1127 | div++; |
| 1128 | rem = rem - bclk_freq; |
| 1129 | } |
| 1130 | } |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1131 | error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem, |
| 1132 | (int)bclk_freq)) / div - 1000000; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1133 | |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1134 | if (set) { |
| 1135 | if (error_ppm) |
| 1136 | dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n", |
| 1137 | error_ppm); |
| 1138 | |
| 1139 | __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_BCLK, div, 0); |
Peter Ujfalusi | ddecd14 | 2016-05-09 13:42:32 +0300 | [diff] [blame] | 1140 | if (reg & AHCLKXE) |
| 1141 | __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK, |
| 1142 | aux_div, 0); |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | return error_ppm; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Peter Ujfalusi | 5fcb457 | 2018-08-31 11:24:56 +0300 | [diff] [blame] | 1148 | static inline u32 davinci_mcasp_tx_delay(struct davinci_mcasp *mcasp) |
| 1149 | { |
| 1150 | if (!mcasp->txnumevt) |
| 1151 | return 0; |
| 1152 | |
| 1153 | return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_WFIFOSTS_OFFSET); |
| 1154 | } |
| 1155 | |
| 1156 | static inline u32 davinci_mcasp_rx_delay(struct davinci_mcasp *mcasp) |
| 1157 | { |
| 1158 | if (!mcasp->rxnumevt) |
| 1159 | return 0; |
| 1160 | |
| 1161 | return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_RFIFOSTS_OFFSET); |
| 1162 | } |
| 1163 | |
| 1164 | static snd_pcm_sframes_t davinci_mcasp_delay( |
| 1165 | struct snd_pcm_substream *substream, |
| 1166 | struct snd_soc_dai *cpu_dai) |
| 1167 | { |
| 1168 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
| 1169 | u32 fifo_use; |
| 1170 | |
| 1171 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 1172 | fifo_use = davinci_mcasp_tx_delay(mcasp); |
| 1173 | else |
| 1174 | fifo_use = davinci_mcasp_rx_delay(mcasp); |
| 1175 | |
| 1176 | /* |
| 1177 | * Divide the used locations with the channel count to get the |
| 1178 | * FIFO usage in samples (don't care about partial samples in the |
| 1179 | * buffer). |
| 1180 | */ |
| 1181 | return fifo_use / substream->runtime->channels; |
| 1182 | } |
| 1183 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1184 | static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream, |
| 1185 | struct snd_pcm_hw_params *params, |
| 1186 | struct snd_soc_dai *cpu_dai) |
| 1187 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1188 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1189 | int word_length; |
Peter Ujfalusi | a7e46bd | 2014-02-03 14:51:50 +0200 | [diff] [blame] | 1190 | int channels = params_channels(params); |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 1191 | int period_size = params_period_size(params); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1192 | int ret; |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 1193 | |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 1194 | switch (params_format(params)) { |
| 1195 | case SNDRV_PCM_FORMAT_U8: |
| 1196 | case SNDRV_PCM_FORMAT_S8: |
| 1197 | word_length = 8; |
| 1198 | break; |
| 1199 | |
| 1200 | case SNDRV_PCM_FORMAT_U16_LE: |
| 1201 | case SNDRV_PCM_FORMAT_S16_LE: |
| 1202 | word_length = 16; |
| 1203 | break; |
| 1204 | |
| 1205 | case SNDRV_PCM_FORMAT_U24_3LE: |
| 1206 | case SNDRV_PCM_FORMAT_S24_3LE: |
| 1207 | word_length = 24; |
| 1208 | break; |
| 1209 | |
| 1210 | case SNDRV_PCM_FORMAT_U24_LE: |
| 1211 | case SNDRV_PCM_FORMAT_S24_LE: |
| 1212 | word_length = 24; |
| 1213 | break; |
| 1214 | |
| 1215 | case SNDRV_PCM_FORMAT_U32_LE: |
| 1216 | case SNDRV_PCM_FORMAT_S32_LE: |
| 1217 | word_length = 32; |
| 1218 | break; |
| 1219 | |
| 1220 | default: |
| 1221 | printk(KERN_WARNING "davinci-mcasp: unsupported PCM format"); |
| 1222 | return -EINVAL; |
| 1223 | } |
| 1224 | |
Peter Ujfalusi | 4a11ff2 | 2016-03-11 13:18:51 +0200 | [diff] [blame] | 1225 | ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt); |
| 1226 | if (ret) |
| 1227 | return ret; |
| 1228 | |
Daniel Mack | 8267525 | 2014-07-16 14:04:41 +0200 | [diff] [blame] | 1229 | /* |
| 1230 | * If mcasp is BCLK master, and a BCLK divider was not provided by |
| 1231 | * the machine driver, we need to calculate the ratio. |
| 1232 | */ |
| 1233 | if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) { |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1234 | int slots = mcasp->tdm_slots; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1235 | int rate = params_rate(params); |
| 1236 | int sbits = params_width(params); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1237 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1238 | if (mcasp->slot_width) |
| 1239 | sbits = mcasp->slot_width; |
| 1240 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1241 | davinci_mcasp_calc_clk_div(mcasp, mcasp->sysclk_freq, |
| 1242 | rate * sbits * slots, true); |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 1245 | ret = mcasp_common_hw_param(mcasp, substream->stream, |
| 1246 | period_size * channels, channels); |
Peter Ujfalusi | 0f7d9a6 | 2014-01-30 15:15:24 +0200 | [diff] [blame] | 1247 | if (ret) |
| 1248 | return ret; |
| 1249 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1250 | if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE) |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 1251 | ret = mcasp_dit_hw_param(mcasp, params_rate(params)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1252 | else |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 1253 | ret = mcasp_i2s_hw_param(mcasp, substream->stream, |
| 1254 | channels); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 1255 | |
| 1256 | if (ret) |
| 1257 | return ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1258 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1259 | davinci_config_channel_size(mcasp, word_length); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1260 | |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1261 | if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) { |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1262 | mcasp->channels = channels; |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1263 | if (!mcasp->max_format_width) |
| 1264 | mcasp->max_format_width = word_length; |
| 1265 | } |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1266 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | static int davinci_mcasp_trigger(struct snd_pcm_substream *substream, |
| 1271 | int cmd, struct snd_soc_dai *cpu_dai) |
| 1272 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1273 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1274 | int ret = 0; |
| 1275 | |
| 1276 | switch (cmd) { |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1277 | case SNDRV_PCM_TRIGGER_RESUME: |
Chaithrika U S | e473b84 | 2010-01-20 17:06:33 +0530 | [diff] [blame] | 1278 | case SNDRV_PCM_TRIGGER_START: |
| 1279 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1280 | davinci_mcasp_start(mcasp, substream->stream); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1281 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1282 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Chaithrika U S | a47979b | 2009-12-03 18:56:56 +0530 | [diff] [blame] | 1283 | case SNDRV_PCM_TRIGGER_STOP: |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1284 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1285 | davinci_mcasp_stop(mcasp, substream->stream); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1286 | break; |
| 1287 | |
| 1288 | default: |
| 1289 | ret = -EINVAL; |
| 1290 | } |
| 1291 | |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
Peter Ujfalusi | 1e112c3 | 2019-07-26 09:42:43 +0300 | [diff] [blame] | 1295 | static int davinci_mcasp_hw_rule_slot_width(struct snd_pcm_hw_params *params, |
| 1296 | struct snd_pcm_hw_rule *rule) |
| 1297 | { |
| 1298 | struct davinci_mcasp_ruledata *rd = rule->private; |
| 1299 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 1300 | struct snd_mask nfmt; |
| 1301 | int i, slot_width; |
| 1302 | |
| 1303 | snd_mask_none(&nfmt); |
| 1304 | slot_width = rd->mcasp->slot_width; |
| 1305 | |
| 1306 | for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) { |
| 1307 | if (snd_mask_test(fmt, i)) { |
| 1308 | if (snd_pcm_format_width(i) <= slot_width) { |
| 1309 | snd_mask_set(&nfmt, i); |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | return snd_mask_refine(fmt, &nfmt); |
| 1315 | } |
| 1316 | |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1317 | static int davinci_mcasp_hw_rule_format_width(struct snd_pcm_hw_params *params, |
| 1318 | struct snd_pcm_hw_rule *rule) |
| 1319 | { |
| 1320 | struct davinci_mcasp_ruledata *rd = rule->private; |
| 1321 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 1322 | struct snd_mask nfmt; |
| 1323 | int i, format_width; |
| 1324 | |
| 1325 | snd_mask_none(&nfmt); |
| 1326 | format_width = rd->mcasp->max_format_width; |
| 1327 | |
| 1328 | for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) { |
| 1329 | if (snd_mask_test(fmt, i)) { |
| 1330 | if (snd_pcm_format_width(i) == format_width) { |
| 1331 | snd_mask_set(&nfmt, i); |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | return snd_mask_refine(fmt, &nfmt); |
| 1337 | } |
| 1338 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1339 | static const unsigned int davinci_mcasp_dai_rates[] = { |
| 1340 | 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, |
| 1341 | 88200, 96000, 176400, 192000, |
| 1342 | }; |
| 1343 | |
| 1344 | #define DAVINCI_MAX_RATE_ERROR_PPM 1000 |
| 1345 | |
| 1346 | static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params, |
| 1347 | struct snd_pcm_hw_rule *rule) |
| 1348 | { |
| 1349 | struct davinci_mcasp_ruledata *rd = rule->private; |
| 1350 | struct snd_interval *ri = |
| 1351 | hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 1352 | int sbits = params_width(params); |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1353 | int slots = rd->mcasp->tdm_slots; |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1354 | struct snd_interval range; |
| 1355 | int i; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1356 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1357 | if (rd->mcasp->slot_width) |
| 1358 | sbits = rd->mcasp->slot_width; |
| 1359 | |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1360 | snd_interval_any(&range); |
| 1361 | range.empty = 1; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1362 | |
| 1363 | for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) { |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1364 | if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) { |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1365 | uint bclk_freq = sbits * slots * |
| 1366 | davinci_mcasp_dai_rates[i]; |
| 1367 | unsigned int sysclk_freq; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1368 | int ppm; |
| 1369 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1370 | if (rd->mcasp->auxclk_fs_ratio) |
| 1371 | sysclk_freq = davinci_mcasp_dai_rates[i] * |
| 1372 | rd->mcasp->auxclk_fs_ratio; |
| 1373 | else |
| 1374 | sysclk_freq = rd->mcasp->sysclk_freq; |
| 1375 | |
| 1376 | ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq, |
| 1377 | bclk_freq, false); |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1378 | if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) { |
| 1379 | if (range.empty) { |
| 1380 | range.min = davinci_mcasp_dai_rates[i]; |
| 1381 | range.empty = 0; |
| 1382 | } |
| 1383 | range.max = davinci_mcasp_dai_rates[i]; |
| 1384 | } |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1385 | } |
| 1386 | } |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1387 | |
Jyri Sarha | 518f6ba | 2015-04-23 16:16:06 +0300 | [diff] [blame] | 1388 | dev_dbg(rd->mcasp->dev, |
| 1389 | "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n", |
| 1390 | ri->min, ri->max, range.min, range.max, sbits, slots); |
| 1391 | |
| 1392 | return snd_interval_refine(hw_param_interval(params, rule->var), |
| 1393 | &range); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params, |
| 1397 | struct snd_pcm_hw_rule *rule) |
| 1398 | { |
| 1399 | struct davinci_mcasp_ruledata *rd = rule->private; |
| 1400 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 1401 | struct snd_mask nfmt; |
| 1402 | int rate = params_rate(params); |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1403 | int slots = rd->mcasp->tdm_slots; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1404 | int i, count = 0; |
| 1405 | |
| 1406 | snd_mask_none(&nfmt); |
| 1407 | |
Peter Ujfalusi | 9be072a | 2016-09-01 10:05:12 +0300 | [diff] [blame] | 1408 | for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) { |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1409 | if (snd_mask_test(fmt, i)) { |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1410 | uint sbits = snd_pcm_format_width(i); |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1411 | unsigned int sysclk_freq; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1412 | int ppm; |
| 1413 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1414 | if (rd->mcasp->auxclk_fs_ratio) |
| 1415 | sysclk_freq = rate * |
| 1416 | rd->mcasp->auxclk_fs_ratio; |
| 1417 | else |
| 1418 | sysclk_freq = rd->mcasp->sysclk_freq; |
| 1419 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1420 | if (rd->mcasp->slot_width) |
| 1421 | sbits = rd->mcasp->slot_width; |
| 1422 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 1423 | ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq, |
Peter Ujfalusi | 3e9bee1 | 2016-05-09 13:42:31 +0300 | [diff] [blame] | 1424 | sbits * slots * rate, |
| 1425 | false); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1426 | if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) { |
| 1427 | snd_mask_set(&nfmt, i); |
| 1428 | count++; |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | dev_dbg(rd->mcasp->dev, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1433 | "%d possible sample format for %d Hz and %d tdm slots\n", |
| 1434 | count, rate, slots); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1435 | |
| 1436 | return snd_mask_refine(fmt, &nfmt); |
| 1437 | } |
| 1438 | |
Peter Ujfalusi | d43c17d | 2018-01-05 12:18:07 +0200 | [diff] [blame] | 1439 | static int davinci_mcasp_hw_rule_min_periodsize( |
| 1440 | struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) |
| 1441 | { |
| 1442 | struct snd_interval *period_size = hw_param_interval(params, |
| 1443 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
| 1444 | struct snd_interval frames; |
| 1445 | |
| 1446 | snd_interval_any(&frames); |
| 1447 | frames.min = 64; |
| 1448 | frames.integer = 1; |
| 1449 | |
| 1450 | return snd_interval_refine(period_size, &frames); |
| 1451 | } |
| 1452 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1453 | static int davinci_mcasp_startup(struct snd_pcm_substream *substream, |
| 1454 | struct snd_soc_dai *cpu_dai) |
| 1455 | { |
| 1456 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1457 | struct davinci_mcasp_ruledata *ruledata = |
| 1458 | &mcasp->ruledata[substream->stream]; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1459 | u32 max_channels = 0; |
Peter Ujfalusi | 1e112c3 | 2019-07-26 09:42:43 +0300 | [diff] [blame] | 1460 | int i, dir, ret; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1461 | int tdm_slots = mcasp->tdm_slots; |
| 1462 | |
Peter Ujfalusi | 1935736 | 2016-05-09 13:39:14 +0300 | [diff] [blame] | 1463 | /* Do not allow more then one stream per direction */ |
| 1464 | if (mcasp->substreams[substream->stream]) |
| 1465 | return -EBUSY; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1466 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1467 | mcasp->substreams[substream->stream] = substream; |
| 1468 | |
Peter Ujfalusi | 1935736 | 2016-05-09 13:39:14 +0300 | [diff] [blame] | 1469 | if (mcasp->tdm_mask[substream->stream]) |
| 1470 | tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]); |
| 1471 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1472 | if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE) |
| 1473 | return 0; |
| 1474 | |
| 1475 | /* |
| 1476 | * Limit the maximum allowed channels for the first stream: |
| 1477 | * number of serializers for the direction * tdm slots per serializer |
| 1478 | */ |
| 1479 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 1480 | dir = TX_MODE; |
| 1481 | else |
| 1482 | dir = RX_MODE; |
| 1483 | |
| 1484 | for (i = 0; i < mcasp->num_serializer; i++) { |
| 1485 | if (mcasp->serial_dir[i] == dir) |
| 1486 | max_channels++; |
| 1487 | } |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1488 | ruledata->serializers = max_channels; |
Peter Ujfalusi | 1e112c3 | 2019-07-26 09:42:43 +0300 | [diff] [blame] | 1489 | ruledata->mcasp = mcasp; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1490 | max_channels *= tdm_slots; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1491 | /* |
| 1492 | * If the already active stream has less channels than the calculated |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 1493 | * limit based on the seirializers * tdm_slots, and only one serializer |
| 1494 | * is in use we need to use that as a constraint for the second stream. |
| 1495 | * Otherwise (first stream or less allowed channels or more than one |
| 1496 | * serializer in use) we use the calculated constraint. |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1497 | */ |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 1498 | if (mcasp->channels && mcasp->channels < max_channels && |
| 1499 | ruledata->serializers == 1) |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1500 | max_channels = mcasp->channels; |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1501 | /* |
| 1502 | * But we can always allow channels upto the amount of |
| 1503 | * the available tdm_slots. |
| 1504 | */ |
| 1505 | if (max_channels < tdm_slots) |
| 1506 | max_channels = tdm_slots; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1507 | |
| 1508 | snd_pcm_hw_constraint_minmax(substream->runtime, |
| 1509 | SNDRV_PCM_HW_PARAM_CHANNELS, |
Peter Ujfalusi | e4798d2 | 2017-05-11 09:58:22 +0300 | [diff] [blame] | 1510 | 0, max_channels); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1511 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1512 | snd_pcm_hw_constraint_list(substream->runtime, |
| 1513 | 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1514 | &mcasp->chconstr[substream->stream]); |
| 1515 | |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1516 | if (mcasp->max_format_width) { |
| 1517 | /* |
| 1518 | * Only allow formats which require same amount of bits on the |
| 1519 | * bus as the currently running stream |
| 1520 | */ |
| 1521 | ret = snd_pcm_hw_rule_add(substream->runtime, 0, |
| 1522 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1523 | davinci_mcasp_hw_rule_format_width, |
| 1524 | ruledata, |
| 1525 | SNDRV_PCM_HW_PARAM_FORMAT, -1); |
| 1526 | if (ret) |
| 1527 | return ret; |
| 1528 | } |
| 1529 | else if (mcasp->slot_width) { |
Peter Ujfalusi | 1e112c3 | 2019-07-26 09:42:43 +0300 | [diff] [blame] | 1530 | /* Only allow formats require <= slot_width bits on the bus */ |
| 1531 | ret = snd_pcm_hw_rule_add(substream->runtime, 0, |
| 1532 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1533 | davinci_mcasp_hw_rule_slot_width, |
| 1534 | ruledata, |
| 1535 | SNDRV_PCM_HW_PARAM_FORMAT, -1); |
| 1536 | if (ret) |
| 1537 | return ret; |
| 1538 | } |
Jyri Sarha | 5935a05 | 2015-04-23 16:16:05 +0300 | [diff] [blame] | 1539 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1540 | /* |
| 1541 | * If we rely on implicit BCLK divider setting we should |
| 1542 | * set constraints based on what we can provide. |
| 1543 | */ |
| 1544 | if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) { |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1545 | ret = snd_pcm_hw_rule_add(substream->runtime, 0, |
| 1546 | SNDRV_PCM_HW_PARAM_RATE, |
| 1547 | davinci_mcasp_hw_rule_rate, |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1548 | ruledata, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1549 | SNDRV_PCM_HW_PARAM_FORMAT, -1); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1550 | if (ret) |
| 1551 | return ret; |
| 1552 | ret = snd_pcm_hw_rule_add(substream->runtime, 0, |
| 1553 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1554 | davinci_mcasp_hw_rule_format, |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1555 | ruledata, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame] | 1556 | SNDRV_PCM_HW_PARAM_RATE, -1); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1557 | if (ret) |
| 1558 | return ret; |
| 1559 | } |
| 1560 | |
Peter Ujfalusi | d43c17d | 2018-01-05 12:18:07 +0200 | [diff] [blame] | 1561 | snd_pcm_hw_rule_add(substream->runtime, 0, |
| 1562 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1563 | davinci_mcasp_hw_rule_min_periodsize, NULL, |
| 1564 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); |
| 1565 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream, |
| 1570 | struct snd_soc_dai *cpu_dai) |
| 1571 | { |
| 1572 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
| 1573 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1574 | mcasp->substreams[substream->stream] = NULL; |
Peter Ujfalusi | b7989e2 | 2019-07-25 11:34:32 +0300 | [diff] [blame] | 1575 | mcasp->active_serializers[substream->stream] = 0; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1576 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1577 | if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE) |
| 1578 | return; |
| 1579 | |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1580 | if (!cpu_dai->active) { |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1581 | mcasp->channels = 0; |
Peter Ujfalusi | 2448c813 | 2019-07-26 09:42:44 +0300 | [diff] [blame] | 1582 | mcasp->max_format_width = 0; |
| 1583 | } |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 1586 | static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = { |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1587 | .startup = davinci_mcasp_startup, |
| 1588 | .shutdown = davinci_mcasp_shutdown, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1589 | .trigger = davinci_mcasp_trigger, |
Peter Ujfalusi | 5fcb457 | 2018-08-31 11:24:56 +0300 | [diff] [blame] | 1590 | .delay = davinci_mcasp_delay, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1591 | .hw_params = davinci_mcasp_hw_params, |
| 1592 | .set_fmt = davinci_mcasp_set_dai_fmt, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 1593 | .set_clkdiv = davinci_mcasp_set_clkdiv, |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 1594 | .set_sysclk = davinci_mcasp_set_sysclk, |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 1595 | .set_tdm_slot = davinci_mcasp_set_tdm_slot, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1596 | }; |
| 1597 | |
Peter Ujfalusi | d5902f69 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1598 | static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai) |
| 1599 | { |
| 1600 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
| 1601 | |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1602 | dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK]; |
| 1603 | dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE]; |
Peter Ujfalusi | d5902f69 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1604 | |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
Peter Ujfalusi | ed29cd5 | 2013-11-14 11:35:22 +0200 | [diff] [blame] | 1608 | #define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000 |
| 1609 | |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1610 | #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \ |
| 1611 | SNDRV_PCM_FMTBIT_U8 | \ |
| 1612 | SNDRV_PCM_FMTBIT_S16_LE | \ |
| 1613 | SNDRV_PCM_FMTBIT_U16_LE | \ |
Daniel Mack | 21eb24d | 2012-10-09 09:35:16 +0200 | [diff] [blame] | 1614 | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 1615 | SNDRV_PCM_FMTBIT_U24_LE | \ |
| 1616 | SNDRV_PCM_FMTBIT_S24_3LE | \ |
| 1617 | SNDRV_PCM_FMTBIT_U24_3LE | \ |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1618 | SNDRV_PCM_FMTBIT_S32_LE | \ |
| 1619 | SNDRV_PCM_FMTBIT_U32_LE) |
| 1620 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1621 | static struct snd_soc_dai_driver davinci_mcasp_dai[] = { |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1622 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1623 | .name = "davinci-mcasp.0", |
Peter Ujfalusi | d5902f69 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1624 | .probe = davinci_mcasp_dai_probe, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1625 | .playback = { |
Peter Ujfalusi | e4798d2 | 2017-05-11 09:58:22 +0300 | [diff] [blame] | 1626 | .channels_min = 1, |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1627 | .channels_max = 32 * 16, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1628 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1629 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1630 | }, |
| 1631 | .capture = { |
Peter Ujfalusi | e4798d2 | 2017-05-11 09:58:22 +0300 | [diff] [blame] | 1632 | .channels_min = 1, |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1633 | .channels_max = 32 * 16, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1634 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1635 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1636 | }, |
| 1637 | .ops = &davinci_mcasp_dai_ops, |
| 1638 | |
Jyri Sarha | 295c340 | 2015-09-09 21:27:42 +0300 | [diff] [blame] | 1639 | .symmetric_rates = 1, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1640 | }, |
| 1641 | { |
Peter Ujfalusi | 58e48d9 | 2013-11-14 11:35:24 +0200 | [diff] [blame] | 1642 | .name = "davinci-mcasp.1", |
Peter Ujfalusi | d5902f69 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1643 | .probe = davinci_mcasp_dai_probe, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1644 | .playback = { |
| 1645 | .channels_min = 1, |
| 1646 | .channels_max = 384, |
| 1647 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1648 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1649 | }, |
| 1650 | .ops = &davinci_mcasp_dai_ops, |
| 1651 | }, |
| 1652 | |
| 1653 | }; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1654 | |
Kuninori Morimoto | eeef0ed | 2013-03-21 03:31:19 -0700 | [diff] [blame] | 1655 | static const struct snd_soc_component_driver davinci_mcasp_component = { |
| 1656 | .name = "davinci-mcasp", |
| 1657 | }; |
| 1658 | |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1659 | /* 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] | 1660 | static struct davinci_mcasp_pdata dm646x_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1661 | .tx_dma_offset = 0x400, |
| 1662 | .rx_dma_offset = 0x400, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1663 | .version = MCASP_VERSION_1, |
| 1664 | }; |
| 1665 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1666 | static struct davinci_mcasp_pdata da830_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1667 | .tx_dma_offset = 0x2000, |
| 1668 | .rx_dma_offset = 0x2000, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1669 | .version = MCASP_VERSION_2, |
| 1670 | }; |
| 1671 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1672 | static struct davinci_mcasp_pdata am33xx_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1673 | .tx_dma_offset = 0, |
| 1674 | .rx_dma_offset = 0, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1675 | .version = MCASP_VERSION_3, |
| 1676 | }; |
| 1677 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1678 | static struct davinci_mcasp_pdata dra7_mcasp_pdata = { |
Peter Ujfalusi | 9ac0013 | 2016-06-02 12:55:05 +0300 | [diff] [blame] | 1679 | /* The CFG port offset will be calculated if it is needed */ |
| 1680 | .tx_dma_offset = 0, |
| 1681 | .rx_dma_offset = 0, |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 1682 | .version = MCASP_VERSION_4, |
| 1683 | }; |
| 1684 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1685 | static const struct of_device_id mcasp_dt_ids[] = { |
| 1686 | { |
| 1687 | .compatible = "ti,dm646x-mcasp-audio", |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1688 | .data = &dm646x_mcasp_pdata, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1689 | }, |
| 1690 | { |
| 1691 | .compatible = "ti,da830-mcasp-audio", |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1692 | .data = &da830_mcasp_pdata, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1693 | }, |
Hebbar, Gururaja | e5ec69d | 2012-09-03 13:40:40 +0530 | [diff] [blame] | 1694 | { |
Jyri Sarha | 3af9e03 | 2013-10-18 18:37:44 +0300 | [diff] [blame] | 1695 | .compatible = "ti,am33xx-mcasp-audio", |
Peter Ujfalusi | b14899d | 2013-11-14 11:35:37 +0200 | [diff] [blame] | 1696 | .data = &am33xx_mcasp_pdata, |
Hebbar, Gururaja | e5ec69d | 2012-09-03 13:40:40 +0530 | [diff] [blame] | 1697 | }, |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 1698 | { |
| 1699 | .compatible = "ti,dra7-mcasp-audio", |
| 1700 | .data = &dra7_mcasp_pdata, |
| 1701 | }, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1702 | { /* sentinel */ } |
| 1703 | }; |
| 1704 | MODULE_DEVICE_TABLE(of, mcasp_dt_ids); |
| 1705 | |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 1706 | static int mcasp_reparent_fck(struct platform_device *pdev) |
| 1707 | { |
| 1708 | struct device_node *node = pdev->dev.of_node; |
| 1709 | struct clk *gfclk, *parent_clk; |
| 1710 | const char *parent_name; |
| 1711 | int ret; |
| 1712 | |
| 1713 | if (!node) |
| 1714 | return 0; |
| 1715 | |
| 1716 | parent_name = of_get_property(node, "fck_parent", NULL); |
| 1717 | if (!parent_name) |
| 1718 | return 0; |
| 1719 | |
Peter Ujfalusi | c670254 | 2016-01-27 15:02:49 +0200 | [diff] [blame] | 1720 | dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n"); |
| 1721 | |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 1722 | gfclk = clk_get(&pdev->dev, "fck"); |
| 1723 | if (IS_ERR(gfclk)) { |
| 1724 | dev_err(&pdev->dev, "failed to get fck\n"); |
| 1725 | return PTR_ERR(gfclk); |
| 1726 | } |
| 1727 | |
| 1728 | parent_clk = clk_get(NULL, parent_name); |
| 1729 | if (IS_ERR(parent_clk)) { |
| 1730 | dev_err(&pdev->dev, "failed to get parent clock\n"); |
| 1731 | ret = PTR_ERR(parent_clk); |
| 1732 | goto err1; |
| 1733 | } |
| 1734 | |
| 1735 | ret = clk_set_parent(gfclk, parent_clk); |
| 1736 | if (ret) { |
| 1737 | dev_err(&pdev->dev, "failed to reparent fck\n"); |
| 1738 | goto err2; |
| 1739 | } |
| 1740 | |
| 1741 | err2: |
| 1742 | clk_put(parent_clk); |
| 1743 | err1: |
| 1744 | clk_put(gfclk); |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1748 | static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of( |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1749 | struct platform_device *pdev) |
| 1750 | { |
| 1751 | struct device_node *np = pdev->dev.of_node; |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1752 | struct davinci_mcasp_pdata *pdata = NULL; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1753 | const struct of_device_id *match = |
Sachin Kamat | ea421eb | 2013-05-22 16:53:37 +0530 | [diff] [blame] | 1754 | of_match_device(mcasp_dt_ids, &pdev->dev); |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1755 | struct of_phandle_args dma_spec; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1756 | |
| 1757 | const u32 *of_serial_dir32; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1758 | u32 val; |
| 1759 | int i, ret = 0; |
| 1760 | |
| 1761 | if (pdev->dev.platform_data) { |
| 1762 | pdata = pdev->dev.platform_data; |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 1763 | pdata->dismod = DISMOD_LOW; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1764 | return pdata; |
| 1765 | } else if (match) { |
Peter Ujfalusi | 272ee03 | 2016-06-02 12:55:24 +0300 | [diff] [blame] | 1766 | pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata), |
| 1767 | GFP_KERNEL); |
Colin Ian King | f4d95de | 2020-02-10 09:24:22 +0000 | [diff] [blame^] | 1768 | if (!pdata) |
| 1769 | return NULL; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1770 | } else { |
| 1771 | /* control shouldn't reach here. something is wrong */ |
| 1772 | ret = -EINVAL; |
| 1773 | goto nodata; |
| 1774 | } |
| 1775 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1776 | ret = of_property_read_u32(np, "op-mode", &val); |
| 1777 | if (ret >= 0) |
| 1778 | pdata->op_mode = val; |
| 1779 | |
| 1780 | ret = of_property_read_u32(np, "tdm-slots", &val); |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1781 | if (ret >= 0) { |
| 1782 | if (val < 2 || val > 32) { |
| 1783 | dev_err(&pdev->dev, |
| 1784 | "tdm-slots must be in rage [2-32]\n"); |
| 1785 | ret = -EINVAL; |
| 1786 | goto nodata; |
| 1787 | } |
| 1788 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1789 | pdata->tdm_slots = val; |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1790 | } |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1791 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1792 | of_serial_dir32 = of_get_property(np, "serial-dir", &val); |
| 1793 | val /= sizeof(u32); |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1794 | if (of_serial_dir32) { |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1795 | u8 *of_serial_dir = devm_kzalloc(&pdev->dev, |
| 1796 | (sizeof(*of_serial_dir) * val), |
| 1797 | GFP_KERNEL); |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1798 | if (!of_serial_dir) { |
| 1799 | ret = -ENOMEM; |
| 1800 | goto nodata; |
| 1801 | } |
| 1802 | |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1803 | for (i = 0; i < val; i++) |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1804 | of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]); |
| 1805 | |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1806 | pdata->num_serializer = val; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1807 | pdata->serial_dir = of_serial_dir; |
| 1808 | } |
| 1809 | |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1810 | ret = of_property_match_string(np, "dma-names", "tx"); |
| 1811 | if (ret < 0) |
| 1812 | goto nodata; |
| 1813 | |
| 1814 | ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret, |
| 1815 | &dma_spec); |
| 1816 | if (ret < 0) |
| 1817 | goto nodata; |
| 1818 | |
| 1819 | pdata->tx_dma_channel = dma_spec.args[0]; |
| 1820 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1821 | /* RX is not valid in DIT mode */ |
| 1822 | if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) { |
| 1823 | ret = of_property_match_string(np, "dma-names", "rx"); |
| 1824 | if (ret < 0) |
| 1825 | goto nodata; |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1826 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1827 | ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret, |
| 1828 | &dma_spec); |
| 1829 | if (ret < 0) |
| 1830 | goto nodata; |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1831 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1832 | pdata->rx_dma_channel = dma_spec.args[0]; |
| 1833 | } |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1834 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1835 | ret = of_property_read_u32(np, "tx-num-evt", &val); |
| 1836 | if (ret >= 0) |
| 1837 | pdata->txnumevt = val; |
| 1838 | |
| 1839 | ret = of_property_read_u32(np, "rx-num-evt", &val); |
| 1840 | if (ret >= 0) |
| 1841 | pdata->rxnumevt = val; |
| 1842 | |
| 1843 | ret = of_property_read_u32(np, "sram-size-playback", &val); |
| 1844 | if (ret >= 0) |
| 1845 | pdata->sram_size_playback = val; |
| 1846 | |
| 1847 | ret = of_property_read_u32(np, "sram-size-capture", &val); |
| 1848 | if (ret >= 0) |
| 1849 | pdata->sram_size_capture = val; |
| 1850 | |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 1851 | ret = of_property_read_u32(np, "dismod", &val); |
| 1852 | if (ret >= 0) { |
| 1853 | if (val == 0 || val == 2 || val == 3) { |
| 1854 | pdata->dismod = DISMOD_VAL(val); |
| 1855 | } else { |
| 1856 | dev_warn(&pdev->dev, "Invalid dismod value: %u\n", val); |
| 1857 | pdata->dismod = DISMOD_LOW; |
| 1858 | } |
| 1859 | } else { |
| 1860 | pdata->dismod = DISMOD_LOW; |
| 1861 | } |
| 1862 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1863 | return pdata; |
| 1864 | |
| 1865 | nodata: |
| 1866 | if (ret < 0) { |
| 1867 | dev_err(&pdev->dev, "Error populating platform data, err %d\n", |
| 1868 | ret); |
| 1869 | pdata = NULL; |
| 1870 | } |
| 1871 | return pdata; |
| 1872 | } |
| 1873 | |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1874 | enum { |
| 1875 | PCM_EDMA, |
| 1876 | PCM_SDMA, |
Peter Ujfalusi | fb0c3c6 | 2020-02-10 16:09:50 +0200 | [diff] [blame] | 1877 | PCM_UDMA, |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1878 | }; |
| 1879 | static const char *sdma_prefix = "ti,omap"; |
| 1880 | |
| 1881 | static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp) |
| 1882 | { |
| 1883 | struct dma_chan *chan; |
| 1884 | const char *tmp; |
| 1885 | int ret = PCM_EDMA; |
| 1886 | |
| 1887 | if (!mcasp->dev->of_node) |
| 1888 | return PCM_EDMA; |
| 1889 | |
| 1890 | tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data; |
Peter Ujfalusi | f0c9713 | 2019-11-13 11:54:45 +0200 | [diff] [blame] | 1891 | chan = dma_request_chan(mcasp->dev, tmp); |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1892 | if (IS_ERR(chan)) { |
| 1893 | if (PTR_ERR(chan) != -EPROBE_DEFER) |
| 1894 | dev_err(mcasp->dev, |
| 1895 | "Can't verify DMA configuration (%ld)\n", |
| 1896 | PTR_ERR(chan)); |
| 1897 | return PTR_ERR(chan); |
| 1898 | } |
Takashi Iwai | befff4f | 2017-09-07 10:59:17 +0200 | [diff] [blame] | 1899 | if (WARN_ON(!chan->device || !chan->device->dev)) |
| 1900 | return -EINVAL; |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1901 | |
| 1902 | if (chan->device->dev->of_node) |
| 1903 | ret = of_property_read_string(chan->device->dev->of_node, |
| 1904 | "compatible", &tmp); |
| 1905 | else |
| 1906 | dev_dbg(mcasp->dev, "DMA controller has no of-node\n"); |
| 1907 | |
| 1908 | dma_release_channel(chan); |
| 1909 | if (ret) |
| 1910 | return ret; |
| 1911 | |
| 1912 | dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp); |
| 1913 | if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix))) |
| 1914 | return PCM_SDMA; |
Peter Ujfalusi | fb0c3c6 | 2020-02-10 16:09:50 +0200 | [diff] [blame] | 1915 | else if (strstr(tmp, "udmap")) |
| 1916 | return PCM_UDMA; |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 1917 | |
| 1918 | return PCM_EDMA; |
| 1919 | } |
| 1920 | |
Peter Ujfalusi | 9ac0013 | 2016-06-02 12:55:05 +0300 | [diff] [blame] | 1921 | static u32 davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata *pdata) |
| 1922 | { |
| 1923 | int i; |
| 1924 | u32 offset = 0; |
| 1925 | |
| 1926 | if (pdata->version != MCASP_VERSION_4) |
| 1927 | return pdata->tx_dma_offset; |
| 1928 | |
| 1929 | for (i = 0; i < pdata->num_serializer; i++) { |
| 1930 | if (pdata->serial_dir[i] == TX_MODE) { |
| 1931 | if (!offset) { |
| 1932 | offset = DAVINCI_MCASP_TXBUF_REG(i); |
| 1933 | } else { |
| 1934 | pr_err("%s: Only one serializer allowed!\n", |
| 1935 | __func__); |
| 1936 | break; |
| 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | return offset; |
| 1942 | } |
| 1943 | |
| 1944 | static u32 davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata *pdata) |
| 1945 | { |
| 1946 | int i; |
| 1947 | u32 offset = 0; |
| 1948 | |
| 1949 | if (pdata->version != MCASP_VERSION_4) |
| 1950 | return pdata->rx_dma_offset; |
| 1951 | |
| 1952 | for (i = 0; i < pdata->num_serializer; i++) { |
| 1953 | if (pdata->serial_dir[i] == RX_MODE) { |
| 1954 | if (!offset) { |
| 1955 | offset = DAVINCI_MCASP_RXBUF_REG(i); |
| 1956 | } else { |
| 1957 | pr_err("%s: Only one serializer allowed!\n", |
| 1958 | __func__); |
| 1959 | break; |
| 1960 | } |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | return offset; |
| 1965 | } |
| 1966 | |
Peter Ujfalusi | 540f1ba7 | 2019-01-03 16:05:52 +0200 | [diff] [blame] | 1967 | #ifdef CONFIG_GPIOLIB |
| 1968 | static int davinci_mcasp_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 1969 | { |
| 1970 | struct davinci_mcasp *mcasp = gpiochip_get_data(chip); |
| 1971 | |
| 1972 | if (mcasp->num_serializer && offset < mcasp->num_serializer && |
| 1973 | mcasp->serial_dir[offset] != INACTIVE_MODE) { |
| 1974 | dev_err(mcasp->dev, "AXR%u pin is used for audio\n", offset); |
| 1975 | return -EBUSY; |
| 1976 | } |
| 1977 | |
| 1978 | /* Do not change the PIN yet */ |
| 1979 | |
| 1980 | return pm_runtime_get_sync(mcasp->dev); |
| 1981 | } |
| 1982 | |
| 1983 | static void davinci_mcasp_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 1984 | { |
| 1985 | struct davinci_mcasp *mcasp = gpiochip_get_data(chip); |
| 1986 | |
| 1987 | /* Set the direction to input */ |
| 1988 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset)); |
| 1989 | |
| 1990 | /* Set the pin as McASP pin */ |
| 1991 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset)); |
| 1992 | |
| 1993 | pm_runtime_put_sync(mcasp->dev); |
| 1994 | } |
| 1995 | |
| 1996 | static int davinci_mcasp_gpio_direction_out(struct gpio_chip *chip, |
| 1997 | unsigned offset, int value) |
| 1998 | { |
| 1999 | struct davinci_mcasp *mcasp = gpiochip_get_data(chip); |
| 2000 | u32 val; |
| 2001 | |
| 2002 | if (value) |
| 2003 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset)); |
| 2004 | else |
| 2005 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset)); |
| 2006 | |
| 2007 | val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG); |
| 2008 | if (!(val & BIT(offset))) { |
| 2009 | /* Set the pin as GPIO pin */ |
| 2010 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset)); |
| 2011 | |
| 2012 | /* Set the direction to output */ |
| 2013 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset)); |
| 2014 | } |
| 2015 | |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
| 2019 | static void davinci_mcasp_gpio_set(struct gpio_chip *chip, unsigned offset, |
| 2020 | int value) |
| 2021 | { |
| 2022 | struct davinci_mcasp *mcasp = gpiochip_get_data(chip); |
| 2023 | |
| 2024 | if (value) |
| 2025 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset)); |
| 2026 | else |
| 2027 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset)); |
| 2028 | } |
| 2029 | |
| 2030 | static int davinci_mcasp_gpio_direction_in(struct gpio_chip *chip, |
| 2031 | unsigned offset) |
| 2032 | { |
| 2033 | struct davinci_mcasp *mcasp = gpiochip_get_data(chip); |
| 2034 | u32 val; |
| 2035 | |
| 2036 | val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG); |
| 2037 | if (!(val & BIT(offset))) { |
| 2038 | /* Set the direction to input */ |
| 2039 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset)); |
| 2040 | |
| 2041 | /* Set the pin as GPIO pin */ |
| 2042 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset)); |
| 2043 | } |
| 2044 | |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
| 2048 | static int davinci_mcasp_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 2049 | { |
| 2050 | struct davinci_mcasp *mcasp = gpiochip_get_data(chip); |
| 2051 | u32 val; |
| 2052 | |
| 2053 | val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDSET_REG); |
| 2054 | if (val & BIT(offset)) |
| 2055 | return 1; |
| 2056 | |
| 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | static int davinci_mcasp_gpio_get_direction(struct gpio_chip *chip, |
| 2061 | unsigned offset) |
| 2062 | { |
| 2063 | struct davinci_mcasp *mcasp = gpiochip_get_data(chip); |
| 2064 | u32 val; |
| 2065 | |
| 2066 | val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDIR_REG); |
| 2067 | if (val & BIT(offset)) |
| 2068 | return 0; |
| 2069 | |
| 2070 | return 1; |
| 2071 | } |
| 2072 | |
| 2073 | static const struct gpio_chip davinci_mcasp_template_chip = { |
| 2074 | .owner = THIS_MODULE, |
| 2075 | .request = davinci_mcasp_gpio_request, |
| 2076 | .free = davinci_mcasp_gpio_free, |
| 2077 | .direction_output = davinci_mcasp_gpio_direction_out, |
| 2078 | .set = davinci_mcasp_gpio_set, |
| 2079 | .direction_input = davinci_mcasp_gpio_direction_in, |
| 2080 | .get = davinci_mcasp_gpio_get, |
| 2081 | .get_direction = davinci_mcasp_gpio_get_direction, |
| 2082 | .base = -1, |
| 2083 | .ngpio = 32, |
| 2084 | }; |
| 2085 | |
| 2086 | static int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp) |
| 2087 | { |
| 2088 | if (!of_property_read_bool(mcasp->dev->of_node, "gpio-controller")) |
| 2089 | return 0; |
| 2090 | |
| 2091 | mcasp->gpio_chip = davinci_mcasp_template_chip; |
| 2092 | mcasp->gpio_chip.label = dev_name(mcasp->dev); |
| 2093 | mcasp->gpio_chip.parent = mcasp->dev; |
| 2094 | #ifdef CONFIG_OF_GPIO |
| 2095 | mcasp->gpio_chip.of_node = mcasp->dev->of_node; |
| 2096 | #endif |
| 2097 | |
| 2098 | return devm_gpiochip_add_data(mcasp->dev, &mcasp->gpio_chip, mcasp); |
| 2099 | } |
| 2100 | |
| 2101 | #else /* CONFIG_GPIOLIB */ |
| 2102 | static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp) |
| 2103 | { |
| 2104 | return 0; |
| 2105 | } |
| 2106 | #endif /* CONFIG_GPIOLIB */ |
| 2107 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 2108 | static int davinci_mcasp_get_dt_params(struct davinci_mcasp *mcasp) |
| 2109 | { |
| 2110 | struct device_node *np = mcasp->dev->of_node; |
| 2111 | int ret; |
| 2112 | u32 val; |
| 2113 | |
| 2114 | if (!np) |
| 2115 | return 0; |
| 2116 | |
| 2117 | ret = of_property_read_u32(np, "auxclk-fs-ratio", &val); |
| 2118 | if (ret >= 0) |
| 2119 | mcasp->auxclk_fs_ratio = val; |
| 2120 | |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2124 | static int davinci_mcasp_probe(struct platform_device *pdev) |
| 2125 | { |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 2126 | struct snd_dmaengine_dai_dma_data *dma_data; |
Axel Lin | 508a43f | 2015-08-24 16:47:36 +0800 | [diff] [blame] | 2127 | struct resource *mem, *res, *dat; |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 2128 | struct davinci_mcasp_pdata *pdata; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2129 | struct davinci_mcasp *mcasp; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2130 | char *irq_name; |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2131 | int *dma; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2132 | int irq; |
Julia Lawall | 96d31e2 | 2011-12-29 17:51:21 +0100 | [diff] [blame] | 2133 | int ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2134 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 2135 | if (!pdev->dev.platform_data && !pdev->dev.of_node) { |
| 2136 | dev_err(&pdev->dev, "No platform data supplied\n"); |
| 2137 | return -EINVAL; |
| 2138 | } |
| 2139 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2140 | mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp), |
Julia Lawall | 96d31e2 | 2011-12-29 17:51:21 +0100 | [diff] [blame] | 2141 | GFP_KERNEL); |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2142 | if (!mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2143 | return -ENOMEM; |
| 2144 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 2145 | pdata = davinci_mcasp_set_pdata_from_of(pdev); |
| 2146 | if (!pdata) { |
| 2147 | dev_err(&pdev->dev, "no platform data\n"); |
| 2148 | return -EINVAL; |
| 2149 | } |
| 2150 | |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 2151 | mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu"); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2152 | if (!mem) { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2153 | dev_warn(mcasp->dev, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 2154 | "\"mpu\" mem resource not found, using index 0\n"); |
| 2155 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2156 | if (!mem) { |
| 2157 | dev_err(&pdev->dev, "no mem resource?\n"); |
| 2158 | return -ENODEV; |
| 2159 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2160 | } |
| 2161 | |
Axel Lin | 508a43f | 2015-08-24 16:47:36 +0800 | [diff] [blame] | 2162 | mcasp->base = devm_ioremap_resource(&pdev->dev, mem); |
| 2163 | if (IS_ERR(mcasp->base)) |
| 2164 | return PTR_ERR(mcasp->base); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2165 | |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 2166 | pm_runtime_enable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2167 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2168 | mcasp->op_mode = pdata->op_mode; |
Peter Ujfalusi | 1a5923d | 2014-11-10 12:32:15 +0200 | [diff] [blame] | 2169 | /* sanity check for tdm slots parameter */ |
| 2170 | if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) { |
| 2171 | if (pdata->tdm_slots < 2) { |
| 2172 | dev_err(&pdev->dev, "invalid tdm slots: %d\n", |
| 2173 | pdata->tdm_slots); |
| 2174 | mcasp->tdm_slots = 2; |
| 2175 | } else if (pdata->tdm_slots > 32) { |
| 2176 | dev_err(&pdev->dev, "invalid tdm slots: %d\n", |
| 2177 | pdata->tdm_slots); |
| 2178 | mcasp->tdm_slots = 32; |
| 2179 | } else { |
| 2180 | mcasp->tdm_slots = pdata->tdm_slots; |
| 2181 | } |
| 2182 | } |
| 2183 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2184 | mcasp->num_serializer = pdata->num_serializer; |
Peter Ujfalusi | 6175471 | 2019-01-03 16:05:50 +0200 | [diff] [blame] | 2185 | #ifdef CONFIG_PM |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2186 | mcasp->context.xrsr_regs = devm_kcalloc(&pdev->dev, |
| 2187 | mcasp->num_serializer, sizeof(u32), |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 2188 | GFP_KERNEL); |
Christophe Jaillet | 4243e04 | 2017-08-27 08:46:50 +0200 | [diff] [blame] | 2189 | if (!mcasp->context.xrsr_regs) { |
| 2190 | ret = -ENOMEM; |
| 2191 | goto err; |
| 2192 | } |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 2193 | #endif |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2194 | mcasp->serial_dir = pdata->serial_dir; |
| 2195 | mcasp->version = pdata->version; |
| 2196 | mcasp->txnumevt = pdata->txnumevt; |
| 2197 | mcasp->rxnumevt = pdata->rxnumevt; |
Peter Ujfalusi | bc18454 | 2018-11-16 15:41:41 +0200 | [diff] [blame] | 2198 | mcasp->dismod = pdata->dismod; |
Peter Ujfalusi | 487dce8 | 2013-11-14 11:35:31 +0200 | [diff] [blame] | 2199 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2200 | mcasp->dev = &pdev->dev; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2201 | |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 2202 | irq = platform_get_irq_byname(pdev, "common"); |
| 2203 | if (irq >= 0) { |
Peter Ujfalusi | ab1fffe | 2015-09-18 15:02:50 +0300 | [diff] [blame] | 2204 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common", |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 2205 | dev_name(&pdev->dev)); |
Arvind Yadav | 0c8b794 | 2017-09-20 15:36:09 +0530 | [diff] [blame] | 2206 | if (!irq_name) { |
| 2207 | ret = -ENOMEM; |
| 2208 | goto err; |
| 2209 | } |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 2210 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 2211 | davinci_mcasp_common_irq_handler, |
Peter Ujfalusi | 8f511ff | 2015-02-02 14:38:32 +0200 | [diff] [blame] | 2212 | IRQF_ONESHOT | IRQF_SHARED, |
| 2213 | irq_name, mcasp); |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 2214 | if (ret) { |
| 2215 | dev_err(&pdev->dev, "common IRQ request failed\n"); |
| 2216 | goto err; |
| 2217 | } |
| 2218 | |
| 2219 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN; |
| 2220 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN; |
| 2221 | } |
| 2222 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2223 | irq = platform_get_irq_byname(pdev, "rx"); |
| 2224 | if (irq >= 0) { |
Peter Ujfalusi | ab1fffe | 2015-09-18 15:02:50 +0300 | [diff] [blame] | 2225 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx", |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2226 | dev_name(&pdev->dev)); |
Arvind Yadav | 0c8b794 | 2017-09-20 15:36:09 +0530 | [diff] [blame] | 2227 | if (!irq_name) { |
| 2228 | ret = -ENOMEM; |
| 2229 | goto err; |
| 2230 | } |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2231 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 2232 | davinci_mcasp_rx_irq_handler, |
| 2233 | IRQF_ONESHOT, irq_name, mcasp); |
| 2234 | if (ret) { |
| 2235 | dev_err(&pdev->dev, "RX IRQ request failed\n"); |
| 2236 | goto err; |
| 2237 | } |
| 2238 | |
| 2239 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN; |
| 2240 | } |
| 2241 | |
| 2242 | irq = platform_get_irq_byname(pdev, "tx"); |
| 2243 | if (irq >= 0) { |
Peter Ujfalusi | ab1fffe | 2015-09-18 15:02:50 +0300 | [diff] [blame] | 2244 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx", |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2245 | dev_name(&pdev->dev)); |
Arvind Yadav | 0c8b794 | 2017-09-20 15:36:09 +0530 | [diff] [blame] | 2246 | if (!irq_name) { |
| 2247 | ret = -ENOMEM; |
| 2248 | goto err; |
| 2249 | } |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 2250 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 2251 | davinci_mcasp_tx_irq_handler, |
| 2252 | IRQF_ONESHOT, irq_name, mcasp); |
| 2253 | if (ret) { |
| 2254 | dev_err(&pdev->dev, "TX IRQ request failed\n"); |
| 2255 | goto err; |
| 2256 | } |
| 2257 | |
| 2258 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN; |
| 2259 | } |
| 2260 | |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 2261 | dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat"); |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2262 | if (dat) |
| 2263 | mcasp->dat_port = true; |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 2264 | |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 2265 | dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK]; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2266 | if (dat) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2267 | dma_data->addr = dat->start; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2268 | else |
Peter Ujfalusi | 9ac0013 | 2016-06-02 12:55:05 +0300 | [diff] [blame] | 2269 | dma_data->addr = mem->start + davinci_mcasp_txdma_offset(pdata); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2270 | |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2271 | dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK]; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2272 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 2273 | if (res) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2274 | *dma = res->start; |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 2275 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2276 | *dma = pdata->tx_dma_channel; |
Troy Kisky | 92e2a6f | 2009-09-11 14:29:03 -0700 | [diff] [blame] | 2277 | |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 2278 | /* dmaengine filter data for DT and non-DT boot */ |
| 2279 | if (pdev->dev.of_node) |
| 2280 | dma_data->filter_data = "tx"; |
| 2281 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2282 | dma_data->filter_data = dma; |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 2283 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2284 | /* RX is not valid in DIT mode */ |
| 2285 | if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) { |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2286 | dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE]; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2287 | if (dat) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2288 | dma_data->addr = dat->start; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2289 | else |
Peter Ujfalusi | 9ac0013 | 2016-06-02 12:55:05 +0300 | [diff] [blame] | 2290 | dma_data->addr = |
| 2291 | mem->start + davinci_mcasp_rxdma_offset(pdata); |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2292 | |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2293 | dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE]; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2294 | res = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 2295 | if (res) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2296 | *dma = res->start; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2297 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2298 | *dma = pdata->rx_dma_channel; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2299 | |
| 2300 | /* dmaengine filter data for DT and non-DT boot */ |
| 2301 | if (pdev->dev.of_node) |
| 2302 | dma_data->filter_data = "rx"; |
| 2303 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 2304 | dma_data->filter_data = dma; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 2305 | } |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 2306 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2307 | if (mcasp->version < MCASP_VERSION_3) { |
| 2308 | mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE; |
Peter Ujfalusi | 64ebdec | 2014-03-07 15:03:55 +0200 | [diff] [blame] | 2309 | /* dma_params->dma_addr is pointing to the data port address */ |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 2310 | mcasp->dat_port = true; |
| 2311 | } else { |
| 2312 | mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE; |
| 2313 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2314 | |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 2315 | /* Allocate memory for long enough list for all possible |
| 2316 | * scenarios. Maximum number tdm slots is 32 and there cannot |
| 2317 | * be more serializers than given in the configuration. The |
| 2318 | * serializer directions could be taken into account, but it |
| 2319 | * would make code much more complex and save only couple of |
| 2320 | * bytes. |
| 2321 | */ |
| 2322 | mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list = |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2323 | devm_kcalloc(mcasp->dev, |
| 2324 | 32 + mcasp->num_serializer - 1, |
| 2325 | sizeof(unsigned int), |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 2326 | GFP_KERNEL); |
| 2327 | |
| 2328 | mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list = |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2329 | devm_kcalloc(mcasp->dev, |
| 2330 | 32 + mcasp->num_serializer - 1, |
| 2331 | sizeof(unsigned int), |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 2332 | GFP_KERNEL); |
| 2333 | |
| 2334 | if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list || |
Christophe Jaillet | 1b8b68b | 2017-09-16 07:40:29 +0200 | [diff] [blame] | 2335 | !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) { |
| 2336 | ret = -ENOMEM; |
| 2337 | goto err; |
| 2338 | } |
Jyri Sarha | dd55ff8 | 2015-09-09 21:27:44 +0300 | [diff] [blame] | 2339 | |
| 2340 | ret = davinci_mcasp_set_ch_constraints(mcasp); |
Jyri Sarha | 5935a05 | 2015-04-23 16:16:05 +0300 | [diff] [blame] | 2341 | if (ret) |
| 2342 | goto err; |
| 2343 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 2344 | dev_set_drvdata(&pdev->dev, mcasp); |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 2345 | |
| 2346 | mcasp_reparent_fck(pdev); |
| 2347 | |
Peter Ujfalusi | 540f1ba7 | 2019-01-03 16:05:52 +0200 | [diff] [blame] | 2348 | /* All PINS as McASP */ |
| 2349 | pm_runtime_get_sync(mcasp->dev); |
| 2350 | mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000); |
| 2351 | pm_runtime_put(mcasp->dev); |
| 2352 | |
| 2353 | ret = davinci_mcasp_init_gpiochip(mcasp); |
| 2354 | if (ret) |
| 2355 | goto err; |
| 2356 | |
Peter Ujfalusi | 764958f | 2019-06-11 15:29:41 +0300 | [diff] [blame] | 2357 | ret = davinci_mcasp_get_dt_params(mcasp); |
| 2358 | if (ret) |
| 2359 | return -EINVAL; |
| 2360 | |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 2361 | ret = devm_snd_soc_register_component(&pdev->dev, |
| 2362 | &davinci_mcasp_component, |
| 2363 | &davinci_mcasp_dai[pdata->op_mode], 1); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2364 | |
| 2365 | if (ret != 0) |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 2366 | goto err; |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 2367 | |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 2368 | ret = davinci_mcasp_get_dma_type(mcasp); |
| 2369 | switch (ret) { |
| 2370 | case PCM_EDMA: |
Peter Ujfalusi | f3f9cfa | 2014-07-16 15:12:04 +0300 | [diff] [blame] | 2371 | ret = edma_pcm_platform_register(&pdev->dev); |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 2372 | break; |
| 2373 | case PCM_SDMA: |
Janusz Krzysztofik | 3e802e9 | 2019-06-02 16:55:49 +0200 | [diff] [blame] | 2374 | ret = sdma_pcm_platform_register(&pdev->dev, "tx", "rx"); |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 2375 | break; |
Peter Ujfalusi | fb0c3c6 | 2020-02-10 16:09:50 +0200 | [diff] [blame] | 2376 | case PCM_UDMA: |
| 2377 | ret = udma_pcm_platform_register(&pdev->dev); |
| 2378 | break; |
Jyri Sarha | 9fbd58c | 2015-06-02 23:09:34 +0300 | [diff] [blame] | 2379 | default: |
| 2380 | dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret); |
| 2381 | case -EPROBE_DEFER: |
| 2382 | goto err; |
Peter Ujfalusi | d5c6c59 | 2014-04-16 15:46:20 +0300 | [diff] [blame] | 2383 | break; |
| 2384 | } |
| 2385 | |
| 2386 | if (ret) { |
| 2387 | dev_err(&pdev->dev, "register PCM failed: %d\n", ret); |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 2388 | goto err; |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 2389 | } |
| 2390 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2391 | return 0; |
| 2392 | |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 2393 | err: |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 2394 | pm_runtime_disable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2395 | return ret; |
| 2396 | } |
| 2397 | |
| 2398 | static int davinci_mcasp_remove(struct platform_device *pdev) |
| 2399 | { |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 2400 | pm_runtime_disable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2401 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2402 | return 0; |
| 2403 | } |
| 2404 | |
Peter Ujfalusi | 6175471 | 2019-01-03 16:05:50 +0200 | [diff] [blame] | 2405 | #ifdef CONFIG_PM |
| 2406 | static int davinci_mcasp_runtime_suspend(struct device *dev) |
| 2407 | { |
| 2408 | struct davinci_mcasp *mcasp = dev_get_drvdata(dev); |
| 2409 | struct davinci_mcasp_context *context = &mcasp->context; |
| 2410 | u32 reg; |
| 2411 | int i; |
| 2412 | |
| 2413 | for (i = 0; i < ARRAY_SIZE(context_regs); i++) |
| 2414 | context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]); |
| 2415 | |
| 2416 | if (mcasp->txnumevt) { |
| 2417 | reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 2418 | context->afifo_regs[0] = mcasp_get_reg(mcasp, reg); |
| 2419 | } |
| 2420 | if (mcasp->rxnumevt) { |
| 2421 | reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 2422 | context->afifo_regs[1] = mcasp_get_reg(mcasp, reg); |
| 2423 | } |
| 2424 | |
| 2425 | for (i = 0; i < mcasp->num_serializer; i++) |
| 2426 | context->xrsr_regs[i] = mcasp_get_reg(mcasp, |
| 2427 | DAVINCI_MCASP_XRSRCTL_REG(i)); |
| 2428 | |
| 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | static int davinci_mcasp_runtime_resume(struct device *dev) |
| 2433 | { |
| 2434 | struct davinci_mcasp *mcasp = dev_get_drvdata(dev); |
| 2435 | struct davinci_mcasp_context *context = &mcasp->context; |
| 2436 | u32 reg; |
| 2437 | int i; |
| 2438 | |
| 2439 | for (i = 0; i < ARRAY_SIZE(context_regs); i++) |
| 2440 | mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]); |
| 2441 | |
| 2442 | if (mcasp->txnumevt) { |
| 2443 | reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 2444 | mcasp_set_reg(mcasp, reg, context->afifo_regs[0]); |
| 2445 | } |
| 2446 | if (mcasp->rxnumevt) { |
| 2447 | reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 2448 | mcasp_set_reg(mcasp, reg, context->afifo_regs[1]); |
| 2449 | } |
| 2450 | |
| 2451 | for (i = 0; i < mcasp->num_serializer; i++) |
| 2452 | mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
| 2453 | context->xrsr_regs[i]); |
| 2454 | |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
| 2458 | #endif |
| 2459 | |
| 2460 | static const struct dev_pm_ops davinci_mcasp_pm_ops = { |
| 2461 | SET_RUNTIME_PM_OPS(davinci_mcasp_runtime_suspend, |
| 2462 | davinci_mcasp_runtime_resume, |
| 2463 | NULL) |
| 2464 | }; |
| 2465 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2466 | static struct platform_driver davinci_mcasp_driver = { |
| 2467 | .probe = davinci_mcasp_probe, |
| 2468 | .remove = davinci_mcasp_remove, |
| 2469 | .driver = { |
| 2470 | .name = "davinci-mcasp", |
Peter Ujfalusi | 6175471 | 2019-01-03 16:05:50 +0200 | [diff] [blame] | 2471 | .pm = &davinci_mcasp_pm_ops, |
Sachin Kamat | ea421eb | 2013-05-22 16:53:37 +0530 | [diff] [blame] | 2472 | .of_match_table = mcasp_dt_ids, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2473 | }, |
| 2474 | }; |
| 2475 | |
Axel Lin | f9b8a51 | 2011-11-25 10:09:27 +0800 | [diff] [blame] | 2476 | module_platform_driver(davinci_mcasp_driver); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 2477 | |
| 2478 | MODULE_AUTHOR("Steve Chen"); |
| 2479 | MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface"); |
| 2480 | MODULE_LICENSE("GPL"); |