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