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