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> |
Jyri Sarha | 87c1936 | 2014-05-26 11:51:14 +0300 | [diff] [blame] | 39 | #include <sound/omap-pcm.h> |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 40 | |
Peter Ujfalusi | f3f9cfa | 2014-07-16 15:12:04 +0300 | [diff] [blame] | 41 | #include "edma-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 | |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 46 | static u32 context_regs[] = { |
| 47 | DAVINCI_MCASP_TXFMCTL_REG, |
| 48 | DAVINCI_MCASP_RXFMCTL_REG, |
| 49 | DAVINCI_MCASP_TXFMT_REG, |
| 50 | DAVINCI_MCASP_RXFMT_REG, |
| 51 | DAVINCI_MCASP_ACLKXCTL_REG, |
| 52 | DAVINCI_MCASP_ACLKRCTL_REG, |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 53 | DAVINCI_MCASP_AHCLKXCTL_REG, |
| 54 | DAVINCI_MCASP_AHCLKRCTL_REG, |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 55 | DAVINCI_MCASP_PDIR_REG, |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 56 | DAVINCI_MCASP_RXMASK_REG, |
| 57 | DAVINCI_MCASP_TXMASK_REG, |
| 58 | DAVINCI_MCASP_RXTDM_REG, |
| 59 | DAVINCI_MCASP_TXTDM_REG, |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 60 | }; |
| 61 | |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 62 | struct davinci_mcasp_context { |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 63 | u32 config_regs[ARRAY_SIZE(context_regs)]; |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 64 | u32 afifo_regs[2]; /* for read/write fifo control registers */ |
| 65 | u32 *xrsr_regs; /* for serializer configuration */ |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 66 | bool pm_state; |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 69 | struct davinci_mcasp_ruledata { |
| 70 | struct davinci_mcasp *mcasp; |
| 71 | int serializers; |
| 72 | }; |
| 73 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 74 | struct davinci_mcasp { |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 75 | struct snd_dmaengine_dai_dma_data dma_data[2]; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 76 | void __iomem *base; |
Peter Ujfalusi | 487dce8 | 2013-11-14 11:35:31 +0200 | [diff] [blame] | 77 | u32 fifo_base; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 78 | struct device *dev; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 79 | struct snd_pcm_substream *substreams[2]; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 80 | |
| 81 | /* McASP specific data */ |
| 82 | int tdm_slots; |
| 83 | u8 op_mode; |
| 84 | u8 num_serializer; |
| 85 | u8 *serial_dir; |
| 86 | u8 version; |
Daniel Mack | 8267525 | 2014-07-16 14:04:41 +0200 | [diff] [blame] | 87 | u8 bclk_div; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 88 | u16 bclk_lrclk_ratio; |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 89 | int streams; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 90 | u32 irq_request[2]; |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 91 | int dma_request[2]; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 92 | |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 93 | int sysclk_freq; |
| 94 | bool bclk_master; |
| 95 | |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 96 | /* McASP FIFO related */ |
| 97 | u8 txnumevt; |
| 98 | u8 rxnumevt; |
| 99 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 100 | bool dat_port; |
| 101 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 102 | /* Used for comstraint setting on the second stream */ |
| 103 | u32 channels; |
| 104 | |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 105 | #ifdef CONFIG_PM_SLEEP |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 106 | struct davinci_mcasp_context context; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 107 | #endif |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 108 | |
| 109 | struct davinci_mcasp_ruledata ruledata[2]; |
Peter Ujfalusi | 21400a7 | 2013-11-14 11:35:26 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 112 | static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 113 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 114 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 115 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 116 | __raw_writel(__raw_readl(reg) | val, reg); |
| 117 | } |
| 118 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 119 | static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 120 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 121 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 122 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 123 | __raw_writel((__raw_readl(reg) & ~(val)), reg); |
| 124 | } |
| 125 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 126 | static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset, |
| 127 | u32 val, u32 mask) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 128 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 129 | void __iomem *reg = mcasp->base + offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 130 | __raw_writel((__raw_readl(reg) & ~mask) | val, reg); |
| 131 | } |
| 132 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 133 | static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset, |
| 134 | u32 val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 135 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 136 | __raw_writel(val, mcasp->base + offset); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 139 | 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] | 140 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 141 | return (u32)__raw_readl(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 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] | 145 | { |
| 146 | int i = 0; |
| 147 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 148 | mcasp_set_bits(mcasp, ctl_reg, val); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 149 | |
| 150 | /* programming GBLCTL needs to read back from GBLCTL and verfiy */ |
| 151 | /* loop count is to avoid the lock-up */ |
| 152 | for (i = 0; i < 1000; i++) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 153 | if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 157 | if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val)) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 158 | printk(KERN_ERR "GBLCTL write error\n"); |
| 159 | } |
| 160 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 161 | static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp) |
| 162 | { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 163 | u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG); |
| 164 | u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 165 | |
| 166 | return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE; |
| 167 | } |
| 168 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 169 | static void mcasp_start_rx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 170 | { |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 171 | if (mcasp->rxnumevt) { /* enable FIFO */ |
| 172 | u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 173 | |
| 174 | mcasp_clr_bits(mcasp, reg, FIFO_ENABLE); |
| 175 | mcasp_set_bits(mcasp, reg, FIFO_ENABLE); |
| 176 | } |
| 177 | |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 178 | /* Start clocks */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 179 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST); |
| 180 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 181 | /* |
| 182 | * When ASYNC == 0 the transmit and receive sections operate |
| 183 | * synchronously from the transmit clock and frame sync. We need to make |
| 184 | * sure that the TX signlas are enabled when starting reception. |
| 185 | */ |
| 186 | if (mcasp_is_synchronous(mcasp)) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 187 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST); |
| 188 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 191 | /* Activate serializer(s) */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 192 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR); |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 193 | /* Release RX state machine */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 194 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST); |
Peter Ujfalusi | 4498273 | 2014-10-29 13:55:45 +0200 | [diff] [blame] | 195 | /* Release Frame Sync generator */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 196 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 197 | if (mcasp_is_synchronous(mcasp)) |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 198 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 199 | |
| 200 | /* enable receive IRQs */ |
| 201 | mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG, |
| 202 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 205 | static void mcasp_start_tx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 206 | { |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 207 | u32 cnt; |
| 208 | |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 209 | if (mcasp->txnumevt) { /* enable FIFO */ |
| 210 | u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 211 | |
| 212 | mcasp_clr_bits(mcasp, reg, FIFO_ENABLE); |
| 213 | mcasp_set_bits(mcasp, reg, FIFO_ENABLE); |
| 214 | } |
| 215 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 216 | /* Start clocks */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 217 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST); |
| 218 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST); |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 219 | /* Activate serializer(s) */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 220 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 221 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 222 | /* wait for XDATA to be cleared */ |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 223 | cnt = 0; |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 224 | while (!(mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & |
| 225 | ~XRDATA) && (cnt < 100000)) |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 226 | cnt++; |
| 227 | |
Peter Ujfalusi | 36bcecd | 2014-10-29 13:55:44 +0200 | [diff] [blame] | 228 | /* Release TX state machine */ |
| 229 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST); |
| 230 | /* Release Frame Sync generator */ |
| 231 | mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST); |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 232 | |
| 233 | /* enable transmit IRQs */ |
| 234 | mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG, |
| 235 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 238 | static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 239 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 240 | mcasp->streams++; |
| 241 | |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 242 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 243 | mcasp_start_tx(mcasp); |
Peter Ujfalusi | bb372af | 2014-10-29 13:55:47 +0200 | [diff] [blame] | 244 | else |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 245 | mcasp_start_rx(mcasp); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 248 | static void mcasp_stop_rx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 249 | { |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 250 | /* disable IRQ sources */ |
| 251 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG, |
| 252 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]); |
| 253 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 254 | /* |
| 255 | * In synchronous mode stop the TX clocks if no other stream is |
| 256 | * running |
| 257 | */ |
| 258 | if (mcasp_is_synchronous(mcasp) && !mcasp->streams) |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 259 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0); |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 260 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 261 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0); |
| 262 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 263 | |
| 264 | if (mcasp->rxnumevt) { /* disable FIFO */ |
| 265 | u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 266 | |
| 267 | mcasp_clr_bits(mcasp, reg, FIFO_ENABLE); |
| 268 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 271 | static void mcasp_stop_tx(struct davinci_mcasp *mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 272 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 273 | u32 val = 0; |
| 274 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 275 | /* disable IRQ sources */ |
| 276 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG, |
| 277 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]); |
| 278 | |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 279 | /* |
| 280 | * In synchronous mode keep TX clocks running if the capture stream is |
| 281 | * still running. |
| 282 | */ |
| 283 | if (mcasp_is_synchronous(mcasp) && mcasp->streams) |
| 284 | val = TXHCLKRST | TXCLKRST | TXFSRST; |
| 285 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 286 | mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val); |
| 287 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 288 | |
| 289 | if (mcasp->txnumevt) { /* disable FIFO */ |
| 290 | u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 291 | |
| 292 | mcasp_clr_bits(mcasp, reg, FIFO_ENABLE); |
| 293 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 294 | } |
| 295 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 296 | static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 297 | { |
Peter Ujfalusi | 4dcb5a0 | 2013-11-14 11:35:33 +0200 | [diff] [blame] | 298 | mcasp->streams--; |
| 299 | |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 300 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 301 | mcasp_stop_tx(mcasp); |
Peter Ujfalusi | 0380866 | 2014-10-29 13:55:46 +0200 | [diff] [blame] | 302 | else |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 303 | mcasp_stop_rx(mcasp); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 306 | static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data) |
| 307 | { |
| 308 | struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data; |
| 309 | struct snd_pcm_substream *substream; |
| 310 | u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]; |
| 311 | u32 handled_mask = 0; |
| 312 | u32 stat; |
| 313 | |
| 314 | stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG); |
| 315 | if (stat & XUNDRN & irq_mask) { |
| 316 | dev_warn(mcasp->dev, "Transmit buffer underflow\n"); |
| 317 | handled_mask |= XUNDRN; |
| 318 | |
| 319 | substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK]; |
| 320 | if (substream) { |
| 321 | snd_pcm_stream_lock_irq(substream); |
| 322 | if (snd_pcm_running(substream)) |
| 323 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 324 | snd_pcm_stream_unlock_irq(substream); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (!handled_mask) |
| 329 | dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n", |
| 330 | stat); |
| 331 | |
| 332 | if (stat & XRERR) |
| 333 | handled_mask |= XRERR; |
| 334 | |
| 335 | /* Ack the handled event only */ |
| 336 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask); |
| 337 | |
| 338 | return IRQ_RETVAL(handled_mask); |
| 339 | } |
| 340 | |
| 341 | static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data) |
| 342 | { |
| 343 | struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data; |
| 344 | struct snd_pcm_substream *substream; |
| 345 | u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]; |
| 346 | u32 handled_mask = 0; |
| 347 | u32 stat; |
| 348 | |
| 349 | stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG); |
| 350 | if (stat & ROVRN & irq_mask) { |
| 351 | dev_warn(mcasp->dev, "Receive buffer overflow\n"); |
| 352 | handled_mask |= ROVRN; |
| 353 | |
| 354 | substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE]; |
| 355 | if (substream) { |
| 356 | snd_pcm_stream_lock_irq(substream); |
| 357 | if (snd_pcm_running(substream)) |
| 358 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 359 | snd_pcm_stream_unlock_irq(substream); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if (!handled_mask) |
| 364 | dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n", |
| 365 | stat); |
| 366 | |
| 367 | if (stat & XRERR) |
| 368 | handled_mask |= XRERR; |
| 369 | |
| 370 | /* Ack the handled event only */ |
| 371 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask); |
| 372 | |
| 373 | return IRQ_RETVAL(handled_mask); |
| 374 | } |
| 375 | |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 376 | static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data) |
| 377 | { |
| 378 | struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data; |
| 379 | irqreturn_t ret = IRQ_NONE; |
| 380 | |
| 381 | if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK]) |
| 382 | ret = davinci_mcasp_tx_irq_handler(irq, data); |
| 383 | |
| 384 | if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE]) |
| 385 | ret |= davinci_mcasp_rx_irq_handler(irq, data); |
| 386 | |
| 387 | return ret; |
| 388 | } |
| 389 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 390 | static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
| 391 | unsigned int fmt) |
| 392 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 393 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 394 | int ret = 0; |
Peter Ujfalusi | 6dfa9a4 | 2014-04-04 14:31:42 +0300 | [diff] [blame] | 395 | u32 data_delay; |
Peter Ujfalusi | 83f1250 | 2014-04-04 14:31:44 +0300 | [diff] [blame] | 396 | bool fs_pol_rising; |
Peter Ujfalusi | ffd950f | 2014-04-04 14:31:45 +0300 | [diff] [blame] | 397 | bool inv_fs = false; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 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, |
| 486 | ACLKX | AHCLKX | 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 | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 530 | out: |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 531 | pm_runtime_put(mcasp->dev); |
Peter Ujfalusi | 1d17a04 | 2014-01-30 15:21:30 +0200 | [diff] [blame] | 532 | return ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 533 | } |
| 534 | |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 535 | static int __davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id, |
| 536 | int div, bool explicit) |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 537 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 538 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
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) { |
| 542 | case 0: /* 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 | |
| 549 | case 1: /* 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 | |
Daniel Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 558 | case 2: /* BCLK/LRCLK ratio */ |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 559 | mcasp->bclk_lrclk_ratio = div; |
Daniel Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 560 | break; |
| 561 | |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 562 | default: |
| 563 | return -EINVAL; |
| 564 | } |
| 565 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 566 | pm_runtime_put(mcasp->dev); |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 570 | static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id, |
| 571 | int div) |
| 572 | { |
| 573 | return __davinci_mcasp_set_clkdiv(dai, div_id, div, 1); |
| 574 | } |
| 575 | |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 576 | static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id, |
| 577 | unsigned int freq, int dir) |
| 578 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 579 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 580 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 581 | pm_runtime_get_sync(mcasp->dev); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 582 | if (dir == SND_SOC_CLOCK_OUT) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 583 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE); |
| 584 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE); |
| 585 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 586 | } else { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 587 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE); |
| 588 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE); |
| 589 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 592 | mcasp->sysclk_freq = freq; |
| 593 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 594 | pm_runtime_put(mcasp->dev); |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 598 | static int davinci_config_channel_size(struct davinci_mcasp *mcasp, |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 599 | int word_length) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 600 | { |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 601 | u32 fmt; |
Daniel Mack | 7967189 | 2013-05-16 15:25:01 +0200 | [diff] [blame] | 602 | u32 tx_rotate = (word_length / 4) & 0x7; |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 603 | u32 mask = (1ULL << word_length) - 1; |
Peter Ujfalusi | fe0a29e | 2014-09-04 10:52:53 +0300 | [diff] [blame] | 604 | /* |
| 605 | * For captured data we should not rotate, inversion and masking is |
| 606 | * enoguh to get the data to the right position: |
| 607 | * Format data from bus after reverse (XRBUF) |
| 608 | * S16_LE: |LSB|MSB|xxx|xxx| |xxx|xxx|MSB|LSB| |
| 609 | * S24_3LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB| |
| 610 | * S24_LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB| |
| 611 | * S32_LE: |LSB|DAT|DAT|MSB| |MSB|DAT|DAT|LSB| |
| 612 | */ |
| 613 | u32 rx_rotate = 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 614 | |
Daniel Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 615 | /* |
| 616 | * if s BCLK-to-LRCLK ratio has been configured via the set_clkdiv() |
| 617 | * callback, take it into account here. That allows us to for example |
| 618 | * send 32 bits per channel to the codec, while only 16 of them carry |
| 619 | * audio payload. |
Michal Bachraty | d486fea | 2013-04-19 15:28:44 +0200 | [diff] [blame] | 620 | * The clock ratio is given for a full period of data (for I2S format |
| 621 | * both left and right channels), so it has to be divided by number of |
| 622 | * tdm-slots (for I2S - divided by 2). |
Daniel Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 623 | */ |
Peter Ujfalusi | d742b92 | 2014-11-10 12:32:19 +0200 | [diff] [blame] | 624 | if (mcasp->bclk_lrclk_ratio) { |
| 625 | u32 slot_length = mcasp->bclk_lrclk_ratio / mcasp->tdm_slots; |
| 626 | |
| 627 | /* |
| 628 | * When we have more bclk then it is needed for the data, we |
| 629 | * need to use the rotation to move the received samples to have |
| 630 | * correct alignment. |
| 631 | */ |
| 632 | rx_rotate = (slot_length - word_length) / 4; |
| 633 | word_length = slot_length; |
| 634 | } |
Daniel Mack | 1b3bc06 | 2012-12-05 18:20:38 +0100 | [diff] [blame] | 635 | |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 636 | /* mapping of the XSSZ bit-field as described in the datasheet */ |
| 637 | fmt = (word_length >> 1) - 1; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 638 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 639 | if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 640 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt), |
| 641 | RXSSZ(0x0F)); |
| 642 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt), |
| 643 | TXSSZ(0x0F)); |
| 644 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate), |
| 645 | TXROT(7)); |
| 646 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate), |
| 647 | RXROT(7)); |
| 648 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask); |
Yegor Yefremov | f5023af | 2013-04-04 16:13:20 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 651 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask); |
Chaithrika U S | 0c31cf3 | 2009-09-15 18:13:29 -0400 | [diff] [blame] | 652 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 653 | return 0; |
| 654 | } |
| 655 | |
Peter Ujfalusi | 662ffae | 2014-01-30 15:15:22 +0200 | [diff] [blame] | 656 | static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream, |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 657 | int period_words, int channels) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 658 | { |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 659 | 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] | 660 | int i; |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 661 | u8 tx_ser = 0; |
| 662 | u8 rx_ser = 0; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 663 | u8 slots = mcasp->tdm_slots; |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 664 | u8 max_active_serializers = (channels + slots - 1) / slots; |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 665 | int active_serializers, numevt, n; |
Peter Ujfalusi | 487dce8 | 2013-11-14 11:35:31 +0200 | [diff] [blame] | 666 | u32 reg; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 667 | /* Default configuration */ |
Peter Ujfalusi | 40448e5 | 2014-04-04 15:56:30 +0300 | [diff] [blame] | 668 | if (mcasp->version < MCASP_VERSION_3) |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 669 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 670 | |
| 671 | /* All PINS as McASP */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 672 | mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 673 | |
| 674 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 675 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF); |
| 676 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 677 | } else { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 678 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF); |
| 679 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 682 | for (i = 0; i < mcasp->num_serializer; i++) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 683 | mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
| 684 | mcasp->serial_dir[i]); |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 685 | if (mcasp->serial_dir[i] == TX_MODE && |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 686 | tx_ser < max_active_serializers) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 687 | mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i)); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 688 | tx_ser++; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 689 | } else if (mcasp->serial_dir[i] == RX_MODE && |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 690 | rx_ser < max_active_serializers) { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 691 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i)); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 692 | rx_ser++; |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 693 | } else { |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 694 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
| 695 | SRMOD_INACTIVE, SRMOD_MASK); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 699 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 700 | active_serializers = tx_ser; |
| 701 | numevt = mcasp->txnumevt; |
| 702 | reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 703 | } else { |
| 704 | active_serializers = rx_ser; |
| 705 | numevt = mcasp->rxnumevt; |
| 706 | reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 707 | } |
Daniel Mack | ecf327c | 2013-03-08 14:19:38 +0100 | [diff] [blame] | 708 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 709 | if (active_serializers < max_active_serializers) { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 710 | dev_warn(mcasp->dev, "stream has more channels (%d) than are " |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 711 | "enabled in mcasp (%d)\n", channels, |
| 712 | active_serializers * slots); |
Daniel Mack | ecf327c | 2013-03-08 14:19:38 +0100 | [diff] [blame] | 713 | return -EINVAL; |
| 714 | } |
| 715 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 716 | /* AFIFO is not in use */ |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 717 | if (!numevt) { |
| 718 | /* Configure the burst size for platform drivers */ |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 719 | if (active_serializers > 1) { |
| 720 | /* |
| 721 | * If more than one serializers are in use we have one |
| 722 | * DMA request to provide data for all serializers. |
| 723 | * For example if three serializers are enabled the DMA |
| 724 | * need to transfer three words per DMA request. |
| 725 | */ |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 726 | dma_data->maxburst = active_serializers; |
| 727 | } else { |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 728 | dma_data->maxburst = 0; |
| 729 | } |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 730 | return 0; |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 731 | } |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 732 | |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 733 | if (period_words % active_serializers) { |
| 734 | dev_err(mcasp->dev, "Invalid combination of period words and " |
| 735 | "active serializers: %d, %d\n", period_words, |
| 736 | active_serializers); |
| 737 | return -EINVAL; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Calculate the optimal AFIFO depth for platform side: |
| 742 | * The number of words for numevt need to be in steps of active |
| 743 | * serializers. |
| 744 | */ |
| 745 | n = numevt % active_serializers; |
| 746 | if (n) |
| 747 | numevt += (active_serializers - n); |
| 748 | while (period_words % numevt && numevt > 0) |
| 749 | numevt -= active_serializers; |
| 750 | if (numevt <= 0) |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 751 | numevt = active_serializers; |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 752 | |
Peter Ujfalusi | 0bf0e8a | 2014-04-01 15:55:09 +0300 | [diff] [blame] | 753 | mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK); |
| 754 | mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK); |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 755 | |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 756 | /* Configure the burst size for platform drivers */ |
Peter Ujfalusi | 3344564 | 2014-04-01 15:55:12 +0300 | [diff] [blame] | 757 | if (numevt == 1) |
| 758 | numevt = 0; |
Peter Ujfalusi | 5f04c60 | 2014-04-01 15:55:10 +0300 | [diff] [blame] | 759 | dma_data->maxburst = numevt; |
| 760 | |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 761 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 762 | } |
| 763 | |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 764 | static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream, |
| 765 | int channels) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 766 | { |
| 767 | int i, active_slots; |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 768 | int total_slots; |
| 769 | int active_serializers; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 770 | u32 mask = 0; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 771 | u32 busel = 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 772 | |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 773 | total_slots = mcasp->tdm_slots; |
| 774 | |
| 775 | /* |
| 776 | * If more than one serializer is needed, then use them with |
| 777 | * their specified tdm_slots count. Otherwise, one serializer |
| 778 | * can cope with the transaction using as many slots as channels |
| 779 | * in the stream, requires channels symmetry |
| 780 | */ |
| 781 | active_serializers = (channels + total_slots - 1) / total_slots; |
| 782 | if (active_serializers == 1) |
| 783 | active_slots = channels; |
| 784 | else |
| 785 | active_slots = total_slots; |
| 786 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 787 | for (i = 0; i < active_slots; i++) |
| 788 | mask |= (1 << i); |
| 789 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 790 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC); |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 791 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 792 | if (!mcasp->dat_port) |
| 793 | busel = TXSEL; |
| 794 | |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 795 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask); |
| 796 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD); |
| 797 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 798 | FSXMOD(total_slots), FSXMOD(0x1FF)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 799 | |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 800 | mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask); |
| 801 | mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD); |
| 802 | mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 803 | FSRMOD(total_slots), FSRMOD(0x1FF)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 804 | |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 805 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /* S/PDIF */ |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 809 | static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp, |
| 810 | unsigned int rate) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 811 | { |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 812 | u32 cs_value = 0; |
| 813 | u8 *cs_bytes = (u8*) &cs_value; |
| 814 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 815 | /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0 |
| 816 | and LSB first */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 817 | 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] | 818 | |
| 819 | /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 820 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 821 | |
| 822 | /* Set the TX tdm : for all the slots */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 823 | mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 824 | |
| 825 | /* Set the TX clock controls : div = 1 and internal */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 826 | mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 827 | |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 828 | mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 829 | |
| 830 | /* Only 44100 and 48000 are valid, both have the same setting */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 831 | mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 832 | |
| 833 | /* Enable the DIT */ |
Peter Ujfalusi | f68205a | 2013-11-14 11:35:36 +0200 | [diff] [blame] | 834 | mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 835 | |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 836 | /* Set S/PDIF channel status bits */ |
| 837 | cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT; |
| 838 | cs_bytes[1] = IEC958_AES1_CON_PCM_CODER; |
| 839 | |
| 840 | switch (rate) { |
| 841 | case 22050: |
| 842 | cs_bytes[3] |= IEC958_AES3_CON_FS_22050; |
| 843 | break; |
| 844 | case 24000: |
| 845 | cs_bytes[3] |= IEC958_AES3_CON_FS_24000; |
| 846 | break; |
| 847 | case 32000: |
| 848 | cs_bytes[3] |= IEC958_AES3_CON_FS_32000; |
| 849 | break; |
| 850 | case 44100: |
| 851 | cs_bytes[3] |= IEC958_AES3_CON_FS_44100; |
| 852 | break; |
| 853 | case 48000: |
| 854 | cs_bytes[3] |= IEC958_AES3_CON_FS_48000; |
| 855 | break; |
| 856 | case 88200: |
| 857 | cs_bytes[3] |= IEC958_AES3_CON_FS_88200; |
| 858 | break; |
| 859 | case 96000: |
| 860 | cs_bytes[3] |= IEC958_AES3_CON_FS_96000; |
| 861 | break; |
| 862 | case 176400: |
| 863 | cs_bytes[3] |= IEC958_AES3_CON_FS_176400; |
| 864 | break; |
| 865 | case 192000: |
| 866 | cs_bytes[3] |= IEC958_AES3_CON_FS_192000; |
| 867 | break; |
| 868 | default: |
| 869 | printk(KERN_WARNING "unsupported sampling rate: %d\n", rate); |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | |
| 873 | mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value); |
| 874 | mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value); |
| 875 | |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 876 | return 0; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 877 | } |
| 878 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 879 | static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp, |
| 880 | unsigned int bclk_freq, |
| 881 | int *error_ppm) |
| 882 | { |
| 883 | int div = mcasp->sysclk_freq / bclk_freq; |
| 884 | int rem = mcasp->sysclk_freq % bclk_freq; |
| 885 | |
| 886 | if (rem != 0) { |
| 887 | if (div == 0 || |
| 888 | ((mcasp->sysclk_freq / div) - bclk_freq) > |
| 889 | (bclk_freq - (mcasp->sysclk_freq / (div+1)))) { |
| 890 | div++; |
| 891 | rem = rem - bclk_freq; |
| 892 | } |
| 893 | } |
| 894 | if (error_ppm) |
| 895 | *error_ppm = |
| 896 | (div*1000000 + (int)div64_long(1000000LL*rem, |
| 897 | (int)bclk_freq)) |
| 898 | /div - 1000000; |
| 899 | |
| 900 | return div; |
| 901 | } |
| 902 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 903 | static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream, |
| 904 | struct snd_pcm_hw_params *params, |
| 905 | struct snd_soc_dai *cpu_dai) |
| 906 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 907 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 908 | int word_length; |
Peter Ujfalusi | a7e46bd | 2014-02-03 14:51:50 +0200 | [diff] [blame] | 909 | int channels = params_channels(params); |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 910 | int period_size = params_period_size(params); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 911 | int ret; |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 912 | |
Daniel Mack | 8267525 | 2014-07-16 14:04:41 +0200 | [diff] [blame] | 913 | /* |
| 914 | * If mcasp is BCLK master, and a BCLK divider was not provided by |
| 915 | * the machine driver, we need to calculate the ratio. |
| 916 | */ |
| 917 | if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) { |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 918 | int slots = mcasp->tdm_slots; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 919 | int rate = params_rate(params); |
| 920 | int sbits = params_width(params); |
| 921 | int ppm, div; |
| 922 | |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 923 | div = davinci_mcasp_calc_clk_div(mcasp, rate*sbits*slots, |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 924 | &ppm); |
| 925 | if (ppm) |
| 926 | dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n", |
| 927 | ppm); |
| 928 | |
Jyri Sarha | 8813543 | 2014-08-06 16:47:16 +0300 | [diff] [blame] | 929 | __davinci_mcasp_set_clkdiv(cpu_dai, 1, div, 0); |
Jyri Sarha | ab8b14b | 2014-01-27 17:37:52 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Peter Ujfalusi | dd093a0 | 2014-04-01 15:55:11 +0300 | [diff] [blame] | 932 | ret = mcasp_common_hw_param(mcasp, substream->stream, |
| 933 | period_size * channels, channels); |
Peter Ujfalusi | 0f7d9a6 | 2014-01-30 15:15:24 +0200 | [diff] [blame] | 934 | if (ret) |
| 935 | return ret; |
| 936 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 937 | if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE) |
Daniel Mack | 6479285 | 2014-03-27 11:27:40 +0100 | [diff] [blame] | 938 | ret = mcasp_dit_hw_param(mcasp, params_rate(params)); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 939 | else |
Misael Lopez Cruz | 18a4f55 | 2014-11-10 12:32:17 +0200 | [diff] [blame] | 940 | ret = mcasp_i2s_hw_param(mcasp, substream->stream, |
| 941 | channels); |
Peter Ujfalusi | 2c56c4c | 2014-01-30 15:15:23 +0200 | [diff] [blame] | 942 | |
| 943 | if (ret) |
| 944 | return ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 945 | |
| 946 | switch (params_format(params)) { |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 947 | case SNDRV_PCM_FORMAT_U8: |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 948 | case SNDRV_PCM_FORMAT_S8: |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 949 | word_length = 8; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 950 | break; |
| 951 | |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 952 | case SNDRV_PCM_FORMAT_U16_LE: |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 953 | case SNDRV_PCM_FORMAT_S16_LE: |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 954 | word_length = 16; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 955 | break; |
| 956 | |
Daniel Mack | 21eb24d | 2012-10-09 09:35:16 +0200 | [diff] [blame] | 957 | case SNDRV_PCM_FORMAT_U24_3LE: |
| 958 | case SNDRV_PCM_FORMAT_S24_3LE: |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 959 | word_length = 24; |
Daniel Mack | 21eb24d | 2012-10-09 09:35:16 +0200 | [diff] [blame] | 960 | break; |
| 961 | |
Daniel Mack | 6b7fa01 | 2012-10-09 11:56:40 +0200 | [diff] [blame] | 962 | case SNDRV_PCM_FORMAT_U24_LE: |
| 963 | case SNDRV_PCM_FORMAT_S24_LE: |
Peter Ujfalusi | 182bef8 | 2014-06-26 08:09:24 +0300 | [diff] [blame] | 964 | word_length = 24; |
| 965 | break; |
| 966 | |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 967 | case SNDRV_PCM_FORMAT_U32_LE: |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 968 | case SNDRV_PCM_FORMAT_S32_LE: |
Daniel Mack | ba764b3 | 2012-12-05 18:20:37 +0100 | [diff] [blame] | 969 | word_length = 32; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 970 | break; |
| 971 | |
| 972 | default: |
| 973 | printk(KERN_WARNING "davinci-mcasp: unsupported PCM format"); |
| 974 | return -EINVAL; |
| 975 | } |
Chaithrika U S | 6a99fb5 | 2009-08-11 16:58:52 -0400 | [diff] [blame] | 976 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 977 | davinci_config_channel_size(mcasp, word_length); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 978 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 979 | if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) |
| 980 | mcasp->channels = channels; |
| 981 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | static int davinci_mcasp_trigger(struct snd_pcm_substream *substream, |
| 986 | int cmd, struct snd_soc_dai *cpu_dai) |
| 987 | { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 988 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 989 | int ret = 0; |
| 990 | |
| 991 | switch (cmd) { |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 992 | case SNDRV_PCM_TRIGGER_RESUME: |
Chaithrika U S | e473b84 | 2010-01-20 17:06:33 +0530 | [diff] [blame] | 993 | case SNDRV_PCM_TRIGGER_START: |
| 994 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 995 | davinci_mcasp_start(mcasp, substream->stream); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 996 | break; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 997 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Chaithrika U S | a47979b | 2009-12-03 18:56:56 +0530 | [diff] [blame] | 998 | case SNDRV_PCM_TRIGGER_STOP: |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 999 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1000 | davinci_mcasp_stop(mcasp, substream->stream); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1001 | break; |
| 1002 | |
| 1003 | default: |
| 1004 | ret = -EINVAL; |
| 1005 | } |
| 1006 | |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1010 | static const unsigned int davinci_mcasp_dai_rates[] = { |
| 1011 | 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, |
| 1012 | 88200, 96000, 176400, 192000, |
| 1013 | }; |
| 1014 | |
| 1015 | #define DAVINCI_MAX_RATE_ERROR_PPM 1000 |
| 1016 | |
| 1017 | static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params, |
| 1018 | struct snd_pcm_hw_rule *rule) |
| 1019 | { |
| 1020 | struct davinci_mcasp_ruledata *rd = rule->private; |
| 1021 | struct snd_interval *ri = |
| 1022 | hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 1023 | int sbits = params_width(params); |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 1024 | int slots = rd->mcasp->tdm_slots; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1025 | unsigned int list[ARRAY_SIZE(davinci_mcasp_dai_rates)]; |
| 1026 | int i, count = 0; |
| 1027 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1028 | for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) { |
| 1029 | if (ri->min <= davinci_mcasp_dai_rates[i] && |
| 1030 | ri->max >= davinci_mcasp_dai_rates[i]) { |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 1031 | uint bclk_freq = sbits*slots* |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1032 | davinci_mcasp_dai_rates[i]; |
| 1033 | int ppm; |
| 1034 | |
| 1035 | davinci_mcasp_calc_clk_div(rd->mcasp, bclk_freq, &ppm); |
| 1036 | if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) |
| 1037 | list[count++] = davinci_mcasp_dai_rates[i]; |
| 1038 | } |
| 1039 | } |
| 1040 | dev_dbg(rd->mcasp->dev, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 1041 | "%d frequencies (%d-%d) for %d sbits and %d tdm slots\n", |
| 1042 | count, ri->min, ri->max, sbits, slots); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1043 | |
| 1044 | return snd_interval_list(hw_param_interval(params, rule->var), |
| 1045 | count, list, 0); |
| 1046 | } |
| 1047 | |
| 1048 | static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params, |
| 1049 | struct snd_pcm_hw_rule *rule) |
| 1050 | { |
| 1051 | struct davinci_mcasp_ruledata *rd = rule->private; |
| 1052 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 1053 | struct snd_mask nfmt; |
| 1054 | int rate = params_rate(params); |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 1055 | int slots = rd->mcasp->tdm_slots; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1056 | int i, count = 0; |
| 1057 | |
| 1058 | snd_mask_none(&nfmt); |
| 1059 | |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1060 | for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) { |
| 1061 | if (snd_mask_test(fmt, i)) { |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 1062 | uint bclk_freq = snd_pcm_format_width(i)*slots*rate; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1063 | int ppm; |
| 1064 | |
| 1065 | davinci_mcasp_calc_clk_div(rd->mcasp, bclk_freq, &ppm); |
| 1066 | if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) { |
| 1067 | snd_mask_set(&nfmt, i); |
| 1068 | count++; |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | dev_dbg(rd->mcasp->dev, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 1073 | "%d possible sample format for %d Hz and %d tdm slots\n", |
| 1074 | count, rate, slots); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1075 | |
| 1076 | return snd_mask_refine(fmt, &nfmt); |
| 1077 | } |
| 1078 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1079 | static int davinci_mcasp_startup(struct snd_pcm_substream *substream, |
| 1080 | struct snd_soc_dai *cpu_dai) |
| 1081 | { |
| 1082 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1083 | struct davinci_mcasp_ruledata *ruledata = |
| 1084 | &mcasp->ruledata[substream->stream]; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1085 | u32 max_channels = 0; |
| 1086 | int i, dir; |
| 1087 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1088 | mcasp->substreams[substream->stream] = substream; |
| 1089 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1090 | if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE) |
| 1091 | return 0; |
| 1092 | |
| 1093 | /* |
| 1094 | * Limit the maximum allowed channels for the first stream: |
| 1095 | * number of serializers for the direction * tdm slots per serializer |
| 1096 | */ |
| 1097 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 1098 | dir = TX_MODE; |
| 1099 | else |
| 1100 | dir = RX_MODE; |
| 1101 | |
| 1102 | for (i = 0; i < mcasp->num_serializer; i++) { |
| 1103 | if (mcasp->serial_dir[i] == dir) |
| 1104 | max_channels++; |
| 1105 | } |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1106 | ruledata->serializers = max_channels; |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1107 | max_channels *= mcasp->tdm_slots; |
| 1108 | /* |
| 1109 | * If the already active stream has less channels than the calculated |
| 1110 | * limnit based on the seirializers * tdm_slots, we need to use that as |
| 1111 | * a constraint for the second stream. |
| 1112 | * Otherwise (first stream or less allowed channels) we use the |
| 1113 | * calculated constraint. |
| 1114 | */ |
| 1115 | if (mcasp->channels && mcasp->channels < max_channels) |
| 1116 | max_channels = mcasp->channels; |
| 1117 | |
| 1118 | snd_pcm_hw_constraint_minmax(substream->runtime, |
| 1119 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1120 | 2, max_channels); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1121 | |
| 1122 | /* |
| 1123 | * If we rely on implicit BCLK divider setting we should |
| 1124 | * set constraints based on what we can provide. |
| 1125 | */ |
| 1126 | if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) { |
| 1127 | int ret; |
| 1128 | |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1129 | ruledata->mcasp = mcasp; |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1130 | |
| 1131 | ret = snd_pcm_hw_rule_add(substream->runtime, 0, |
| 1132 | SNDRV_PCM_HW_PARAM_RATE, |
| 1133 | davinci_mcasp_hw_rule_rate, |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1134 | ruledata, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 1135 | SNDRV_PCM_HW_PARAM_FORMAT, -1); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1136 | if (ret) |
| 1137 | return ret; |
| 1138 | ret = snd_pcm_hw_rule_add(substream->runtime, 0, |
| 1139 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1140 | davinci_mcasp_hw_rule_format, |
Peter Ujfalusi | 4cd9db0 | 2015-04-07 14:03:53 +0300 | [diff] [blame] | 1141 | ruledata, |
Jyri Sarha | 1f114f7 | 2015-04-23 16:16:04 +0300 | [diff] [blame^] | 1142 | SNDRV_PCM_HW_PARAM_RATE, -1); |
Jyri Sarha | a75a053 | 2015-03-20 13:31:08 +0200 | [diff] [blame] | 1143 | if (ret) |
| 1144 | return ret; |
| 1145 | } |
| 1146 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream, |
| 1151 | struct snd_soc_dai *cpu_dai) |
| 1152 | { |
| 1153 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai); |
| 1154 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1155 | mcasp->substreams[substream->stream] = NULL; |
| 1156 | |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1157 | if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE) |
| 1158 | return; |
| 1159 | |
| 1160 | if (!cpu_dai->active) |
| 1161 | mcasp->channels = 0; |
| 1162 | } |
| 1163 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 1164 | static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = { |
Peter Ujfalusi | 1127783 | 2014-11-10 12:32:16 +0200 | [diff] [blame] | 1165 | .startup = davinci_mcasp_startup, |
| 1166 | .shutdown = davinci_mcasp_shutdown, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1167 | .trigger = davinci_mcasp_trigger, |
| 1168 | .hw_params = davinci_mcasp_hw_params, |
| 1169 | .set_fmt = davinci_mcasp_set_dai_fmt, |
Daniel Mack | 4ed8c9b | 2012-10-04 15:08:39 +0200 | [diff] [blame] | 1170 | .set_clkdiv = davinci_mcasp_set_clkdiv, |
Daniel Mack | 5b66aa2 | 2012-10-04 15:08:41 +0200 | [diff] [blame] | 1171 | .set_sysclk = davinci_mcasp_set_sysclk, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1172 | }; |
| 1173 | |
Peter Ujfalusi | d5902f6 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1174 | static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai) |
| 1175 | { |
| 1176 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
| 1177 | |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1178 | dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK]; |
| 1179 | dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE]; |
Peter Ujfalusi | d5902f6 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1184 | #ifdef CONFIG_PM_SLEEP |
| 1185 | static int davinci_mcasp_suspend(struct snd_soc_dai *dai) |
| 1186 | { |
| 1187 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 1188 | struct davinci_mcasp_context *context = &mcasp->context; |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 1189 | u32 reg; |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 1190 | int i; |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1191 | |
Peter Ujfalusi | 66e6188 | 2015-03-06 09:07:32 +0200 | [diff] [blame] | 1192 | context->pm_state = pm_runtime_enabled(mcasp->dev); |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 1193 | if (!context->pm_state) |
| 1194 | pm_runtime_get_sync(mcasp->dev); |
| 1195 | |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 1196 | for (i = 0; i < ARRAY_SIZE(context_regs); i++) |
| 1197 | context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]); |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1198 | |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 1199 | if (mcasp->txnumevt) { |
| 1200 | reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 1201 | context->afifo_regs[0] = mcasp_get_reg(mcasp, reg); |
| 1202 | } |
| 1203 | if (mcasp->rxnumevt) { |
| 1204 | reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 1205 | context->afifo_regs[1] = mcasp_get_reg(mcasp, reg); |
| 1206 | } |
| 1207 | |
| 1208 | for (i = 0; i < mcasp->num_serializer; i++) |
| 1209 | context->xrsr_regs[i] = mcasp_get_reg(mcasp, |
| 1210 | DAVINCI_MCASP_XRSRCTL_REG(i)); |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1211 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 1212 | pm_runtime_put_sync(mcasp->dev); |
| 1213 | |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | static int davinci_mcasp_resume(struct snd_soc_dai *dai) |
| 1218 | { |
| 1219 | struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai); |
Peter Ujfalusi | 790bb94 | 2014-02-03 14:51:52 +0200 | [diff] [blame] | 1220 | struct davinci_mcasp_context *context = &mcasp->context; |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 1221 | u32 reg; |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 1222 | int i; |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1223 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 1224 | pm_runtime_get_sync(mcasp->dev); |
| 1225 | |
Peter Ujfalusi | 1cc0c05 | 2014-10-01 16:02:11 +0300 | [diff] [blame] | 1226 | for (i = 0; i < ARRAY_SIZE(context_regs); i++) |
| 1227 | mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]); |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1228 | |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 1229 | if (mcasp->txnumevt) { |
| 1230 | reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET; |
| 1231 | mcasp_set_reg(mcasp, reg, context->afifo_regs[0]); |
| 1232 | } |
| 1233 | if (mcasp->rxnumevt) { |
| 1234 | reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET; |
| 1235 | mcasp_set_reg(mcasp, reg, context->afifo_regs[1]); |
| 1236 | } |
| 1237 | |
| 1238 | for (i = 0; i < mcasp->num_serializer; i++) |
| 1239 | mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i), |
| 1240 | context->xrsr_regs[i]); |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1241 | |
Peter Ujfalusi | 6afda7f | 2015-03-05 16:55:21 +0200 | [diff] [blame] | 1242 | if (!context->pm_state) |
| 1243 | pm_runtime_put_sync(mcasp->dev); |
| 1244 | |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1245 | return 0; |
| 1246 | } |
| 1247 | #else |
| 1248 | #define davinci_mcasp_suspend NULL |
| 1249 | #define davinci_mcasp_resume NULL |
| 1250 | #endif |
| 1251 | |
Peter Ujfalusi | ed29cd5 | 2013-11-14 11:35:22 +0200 | [diff] [blame] | 1252 | #define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000 |
| 1253 | |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1254 | #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \ |
| 1255 | SNDRV_PCM_FMTBIT_U8 | \ |
| 1256 | SNDRV_PCM_FMTBIT_S16_LE | \ |
| 1257 | SNDRV_PCM_FMTBIT_U16_LE | \ |
Daniel Mack | 21eb24d | 2012-10-09 09:35:16 +0200 | [diff] [blame] | 1258 | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 1259 | SNDRV_PCM_FMTBIT_U24_LE | \ |
| 1260 | SNDRV_PCM_FMTBIT_S24_3LE | \ |
| 1261 | SNDRV_PCM_FMTBIT_U24_3LE | \ |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1262 | SNDRV_PCM_FMTBIT_S32_LE | \ |
| 1263 | SNDRV_PCM_FMTBIT_U32_LE) |
| 1264 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1265 | static struct snd_soc_dai_driver davinci_mcasp_dai[] = { |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1266 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1267 | .name = "davinci-mcasp.0", |
Peter Ujfalusi | d5902f6 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1268 | .probe = davinci_mcasp_dai_probe, |
Peter Ujfalusi | 135014a | 2014-01-30 15:21:32 +0200 | [diff] [blame] | 1269 | .suspend = davinci_mcasp_suspend, |
| 1270 | .resume = davinci_mcasp_resume, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1271 | .playback = { |
| 1272 | .channels_min = 2, |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1273 | .channels_max = 32 * 16, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1274 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1275 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1276 | }, |
| 1277 | .capture = { |
| 1278 | .channels_min = 2, |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1279 | .channels_max = 32 * 16, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1280 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1281 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1282 | }, |
| 1283 | .ops = &davinci_mcasp_dai_ops, |
| 1284 | |
Peter Ujfalusi | d75249f | 2014-11-10 12:32:18 +0200 | [diff] [blame] | 1285 | .symmetric_samplebits = 1, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1286 | }, |
| 1287 | { |
Peter Ujfalusi | 58e48d9 | 2013-11-14 11:35:24 +0200 | [diff] [blame] | 1288 | .name = "davinci-mcasp.1", |
Peter Ujfalusi | d5902f6 | 2014-04-01 15:55:07 +0300 | [diff] [blame] | 1289 | .probe = davinci_mcasp_dai_probe, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1290 | .playback = { |
| 1291 | .channels_min = 1, |
| 1292 | .channels_max = 384, |
| 1293 | .rates = DAVINCI_MCASP_RATES, |
Ben Gardiner | 0a9d138 | 2011-08-26 12:02:44 -0400 | [diff] [blame] | 1294 | .formats = DAVINCI_MCASP_PCM_FMTS, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1295 | }, |
| 1296 | .ops = &davinci_mcasp_dai_ops, |
| 1297 | }, |
| 1298 | |
| 1299 | }; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1300 | |
Kuninori Morimoto | eeef0ed | 2013-03-21 03:31:19 -0700 | [diff] [blame] | 1301 | static const struct snd_soc_component_driver davinci_mcasp_component = { |
| 1302 | .name = "davinci-mcasp", |
| 1303 | }; |
| 1304 | |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1305 | /* 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] | 1306 | static struct davinci_mcasp_pdata dm646x_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1307 | .tx_dma_offset = 0x400, |
| 1308 | .rx_dma_offset = 0x400, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1309 | .version = MCASP_VERSION_1, |
| 1310 | }; |
| 1311 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1312 | static struct davinci_mcasp_pdata da830_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1313 | .tx_dma_offset = 0x2000, |
| 1314 | .rx_dma_offset = 0x2000, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1315 | .version = MCASP_VERSION_2, |
| 1316 | }; |
| 1317 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1318 | static struct davinci_mcasp_pdata am33xx_mcasp_pdata = { |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1319 | .tx_dma_offset = 0, |
| 1320 | .rx_dma_offset = 0, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1321 | .version = MCASP_VERSION_3, |
| 1322 | }; |
| 1323 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1324 | static struct davinci_mcasp_pdata dra7_mcasp_pdata = { |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 1325 | .tx_dma_offset = 0x200, |
| 1326 | .rx_dma_offset = 0x284, |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 1327 | .version = MCASP_VERSION_4, |
| 1328 | }; |
| 1329 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1330 | static const struct of_device_id mcasp_dt_ids[] = { |
| 1331 | { |
| 1332 | .compatible = "ti,dm646x-mcasp-audio", |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1333 | .data = &dm646x_mcasp_pdata, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1334 | }, |
| 1335 | { |
| 1336 | .compatible = "ti,da830-mcasp-audio", |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1337 | .data = &da830_mcasp_pdata, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1338 | }, |
Hebbar, Gururaja | e5ec69d | 2012-09-03 13:40:40 +0530 | [diff] [blame] | 1339 | { |
Jyri Sarha | 3af9e03 | 2013-10-18 18:37:44 +0300 | [diff] [blame] | 1340 | .compatible = "ti,am33xx-mcasp-audio", |
Peter Ujfalusi | b14899d | 2013-11-14 11:35:37 +0200 | [diff] [blame] | 1341 | .data = &am33xx_mcasp_pdata, |
Hebbar, Gururaja | e5ec69d | 2012-09-03 13:40:40 +0530 | [diff] [blame] | 1342 | }, |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 1343 | { |
| 1344 | .compatible = "ti,dra7-mcasp-audio", |
| 1345 | .data = &dra7_mcasp_pdata, |
| 1346 | }, |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1347 | { /* sentinel */ } |
| 1348 | }; |
| 1349 | MODULE_DEVICE_TABLE(of, mcasp_dt_ids); |
| 1350 | |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 1351 | static int mcasp_reparent_fck(struct platform_device *pdev) |
| 1352 | { |
| 1353 | struct device_node *node = pdev->dev.of_node; |
| 1354 | struct clk *gfclk, *parent_clk; |
| 1355 | const char *parent_name; |
| 1356 | int ret; |
| 1357 | |
| 1358 | if (!node) |
| 1359 | return 0; |
| 1360 | |
| 1361 | parent_name = of_get_property(node, "fck_parent", NULL); |
| 1362 | if (!parent_name) |
| 1363 | return 0; |
| 1364 | |
| 1365 | gfclk = clk_get(&pdev->dev, "fck"); |
| 1366 | if (IS_ERR(gfclk)) { |
| 1367 | dev_err(&pdev->dev, "failed to get fck\n"); |
| 1368 | return PTR_ERR(gfclk); |
| 1369 | } |
| 1370 | |
| 1371 | parent_clk = clk_get(NULL, parent_name); |
| 1372 | if (IS_ERR(parent_clk)) { |
| 1373 | dev_err(&pdev->dev, "failed to get parent clock\n"); |
| 1374 | ret = PTR_ERR(parent_clk); |
| 1375 | goto err1; |
| 1376 | } |
| 1377 | |
| 1378 | ret = clk_set_parent(gfclk, parent_clk); |
| 1379 | if (ret) { |
| 1380 | dev_err(&pdev->dev, "failed to reparent fck\n"); |
| 1381 | goto err2; |
| 1382 | } |
| 1383 | |
| 1384 | err2: |
| 1385 | clk_put(parent_clk); |
| 1386 | err1: |
| 1387 | clk_put(gfclk); |
| 1388 | return ret; |
| 1389 | } |
| 1390 | |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1391 | static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of( |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1392 | struct platform_device *pdev) |
| 1393 | { |
| 1394 | struct device_node *np = pdev->dev.of_node; |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1395 | struct davinci_mcasp_pdata *pdata = NULL; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1396 | const struct of_device_id *match = |
Sachin Kamat | ea421eb | 2013-05-22 16:53:37 +0530 | [diff] [blame] | 1397 | of_match_device(mcasp_dt_ids, &pdev->dev); |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1398 | struct of_phandle_args dma_spec; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1399 | |
| 1400 | const u32 *of_serial_dir32; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1401 | u32 val; |
| 1402 | int i, ret = 0; |
| 1403 | |
| 1404 | if (pdev->dev.platform_data) { |
| 1405 | pdata = pdev->dev.platform_data; |
| 1406 | return pdata; |
| 1407 | } else if (match) { |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1408 | pdata = (struct davinci_mcasp_pdata*) match->data; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1409 | } else { |
| 1410 | /* control shouldn't reach here. something is wrong */ |
| 1411 | ret = -EINVAL; |
| 1412 | goto nodata; |
| 1413 | } |
| 1414 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1415 | ret = of_property_read_u32(np, "op-mode", &val); |
| 1416 | if (ret >= 0) |
| 1417 | pdata->op_mode = val; |
| 1418 | |
| 1419 | ret = of_property_read_u32(np, "tdm-slots", &val); |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1420 | if (ret >= 0) { |
| 1421 | if (val < 2 || val > 32) { |
| 1422 | dev_err(&pdev->dev, |
| 1423 | "tdm-slots must be in rage [2-32]\n"); |
| 1424 | ret = -EINVAL; |
| 1425 | goto nodata; |
| 1426 | } |
| 1427 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1428 | pdata->tdm_slots = val; |
Michal Bachraty | 2952b27 | 2013-02-28 16:07:08 +0100 | [diff] [blame] | 1429 | } |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1430 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1431 | of_serial_dir32 = of_get_property(np, "serial-dir", &val); |
| 1432 | val /= sizeof(u32); |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1433 | if (of_serial_dir32) { |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1434 | u8 *of_serial_dir = devm_kzalloc(&pdev->dev, |
| 1435 | (sizeof(*of_serial_dir) * val), |
| 1436 | GFP_KERNEL); |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1437 | if (!of_serial_dir) { |
| 1438 | ret = -ENOMEM; |
| 1439 | goto nodata; |
| 1440 | } |
| 1441 | |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1442 | for (i = 0; i < val; i++) |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1443 | of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]); |
| 1444 | |
Peter Ujfalusi | 1427e66 | 2013-10-18 18:37:46 +0300 | [diff] [blame] | 1445 | pdata->num_serializer = val; |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1446 | pdata->serial_dir = of_serial_dir; |
| 1447 | } |
| 1448 | |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1449 | ret = of_property_match_string(np, "dma-names", "tx"); |
| 1450 | if (ret < 0) |
| 1451 | goto nodata; |
| 1452 | |
| 1453 | ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret, |
| 1454 | &dma_spec); |
| 1455 | if (ret < 0) |
| 1456 | goto nodata; |
| 1457 | |
| 1458 | pdata->tx_dma_channel = dma_spec.args[0]; |
| 1459 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1460 | /* RX is not valid in DIT mode */ |
| 1461 | if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) { |
| 1462 | ret = of_property_match_string(np, "dma-names", "rx"); |
| 1463 | if (ret < 0) |
| 1464 | goto nodata; |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1465 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1466 | ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret, |
| 1467 | &dma_spec); |
| 1468 | if (ret < 0) |
| 1469 | goto nodata; |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1470 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1471 | pdata->rx_dma_channel = dma_spec.args[0]; |
| 1472 | } |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1473 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1474 | ret = of_property_read_u32(np, "tx-num-evt", &val); |
| 1475 | if (ret >= 0) |
| 1476 | pdata->txnumevt = val; |
| 1477 | |
| 1478 | ret = of_property_read_u32(np, "rx-num-evt", &val); |
| 1479 | if (ret >= 0) |
| 1480 | pdata->rxnumevt = val; |
| 1481 | |
| 1482 | ret = of_property_read_u32(np, "sram-size-playback", &val); |
| 1483 | if (ret >= 0) |
| 1484 | pdata->sram_size_playback = val; |
| 1485 | |
| 1486 | ret = of_property_read_u32(np, "sram-size-capture", &val); |
| 1487 | if (ret >= 0) |
| 1488 | pdata->sram_size_capture = val; |
| 1489 | |
| 1490 | return pdata; |
| 1491 | |
| 1492 | nodata: |
| 1493 | if (ret < 0) { |
| 1494 | dev_err(&pdev->dev, "Error populating platform data, err %d\n", |
| 1495 | ret); |
| 1496 | pdata = NULL; |
| 1497 | } |
| 1498 | return pdata; |
| 1499 | } |
| 1500 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1501 | static int davinci_mcasp_probe(struct platform_device *pdev) |
| 1502 | { |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 1503 | struct snd_dmaengine_dai_dma_data *dma_data; |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1504 | struct resource *mem, *ioarea, *res, *dat; |
Peter Ujfalusi | d1debaf | 2014-02-03 14:51:51 +0200 | [diff] [blame] | 1505 | struct davinci_mcasp_pdata *pdata; |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1506 | struct davinci_mcasp *mcasp; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1507 | char *irq_name; |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1508 | int *dma; |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1509 | int irq; |
Julia Lawall | 96d31e2 | 2011-12-29 17:51:21 +0100 | [diff] [blame] | 1510 | int ret; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1511 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1512 | if (!pdev->dev.platform_data && !pdev->dev.of_node) { |
| 1513 | dev_err(&pdev->dev, "No platform data supplied\n"); |
| 1514 | return -EINVAL; |
| 1515 | } |
| 1516 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1517 | mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp), |
Julia Lawall | 96d31e2 | 2011-12-29 17:51:21 +0100 | [diff] [blame] | 1518 | GFP_KERNEL); |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1519 | if (!mcasp) |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1520 | return -ENOMEM; |
| 1521 | |
Hebbar, Gururaja | 3e3b8c3 | 2012-08-27 18:56:42 +0530 | [diff] [blame] | 1522 | pdata = davinci_mcasp_set_pdata_from_of(pdev); |
| 1523 | if (!pdata) { |
| 1524 | dev_err(&pdev->dev, "no platform data\n"); |
| 1525 | return -EINVAL; |
| 1526 | } |
| 1527 | |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1528 | mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu"); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1529 | if (!mem) { |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1530 | dev_warn(mcasp->dev, |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1531 | "\"mpu\" mem resource not found, using index 0\n"); |
| 1532 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1533 | if (!mem) { |
| 1534 | dev_err(&pdev->dev, "no mem resource?\n"); |
| 1535 | return -ENODEV; |
| 1536 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1537 | } |
| 1538 | |
Julia Lawall | 96d31e2 | 2011-12-29 17:51:21 +0100 | [diff] [blame] | 1539 | ioarea = devm_request_mem_region(&pdev->dev, mem->start, |
Vaibhav Bedia | d852f446 | 2011-02-09 18:39:52 +0530 | [diff] [blame] | 1540 | resource_size(mem), pdev->name); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1541 | if (!ioarea) { |
| 1542 | dev_err(&pdev->dev, "Audio region already claimed\n"); |
Julia Lawall | 96d31e2 | 2011-12-29 17:51:21 +0100 | [diff] [blame] | 1543 | return -EBUSY; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1544 | } |
| 1545 | |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 1546 | pm_runtime_enable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1547 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1548 | mcasp->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem)); |
| 1549 | if (!mcasp->base) { |
Vaibhav Bedia | 4f82f02 | 2011-02-09 18:39:54 +0530 | [diff] [blame] | 1550 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 1551 | ret = -ENOMEM; |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 1552 | goto err; |
Vaibhav Bedia | 4f82f02 | 2011-02-09 18:39:54 +0530 | [diff] [blame] | 1553 | } |
| 1554 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1555 | mcasp->op_mode = pdata->op_mode; |
Peter Ujfalusi | 1a5923d | 2014-11-10 12:32:15 +0200 | [diff] [blame] | 1556 | /* sanity check for tdm slots parameter */ |
| 1557 | if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) { |
| 1558 | if (pdata->tdm_slots < 2) { |
| 1559 | dev_err(&pdev->dev, "invalid tdm slots: %d\n", |
| 1560 | pdata->tdm_slots); |
| 1561 | mcasp->tdm_slots = 2; |
| 1562 | } else if (pdata->tdm_slots > 32) { |
| 1563 | dev_err(&pdev->dev, "invalid tdm slots: %d\n", |
| 1564 | pdata->tdm_slots); |
| 1565 | mcasp->tdm_slots = 32; |
| 1566 | } else { |
| 1567 | mcasp->tdm_slots = pdata->tdm_slots; |
| 1568 | } |
| 1569 | } |
| 1570 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1571 | mcasp->num_serializer = pdata->num_serializer; |
Peter Ujfalusi | f114ce6 | 2014-10-01 16:02:12 +0300 | [diff] [blame] | 1572 | #ifdef CONFIG_PM_SLEEP |
| 1573 | mcasp->context.xrsr_regs = devm_kzalloc(&pdev->dev, |
| 1574 | sizeof(u32) * mcasp->num_serializer, |
| 1575 | GFP_KERNEL); |
| 1576 | #endif |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1577 | mcasp->serial_dir = pdata->serial_dir; |
| 1578 | mcasp->version = pdata->version; |
| 1579 | mcasp->txnumevt = pdata->txnumevt; |
| 1580 | mcasp->rxnumevt = pdata->rxnumevt; |
Peter Ujfalusi | 487dce8 | 2013-11-14 11:35:31 +0200 | [diff] [blame] | 1581 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1582 | mcasp->dev = &pdev->dev; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1583 | |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 1584 | irq = platform_get_irq_byname(pdev, "common"); |
| 1585 | if (irq >= 0) { |
| 1586 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common\n", |
| 1587 | dev_name(&pdev->dev)); |
| 1588 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 1589 | davinci_mcasp_common_irq_handler, |
Peter Ujfalusi | 8f511ff | 2015-02-02 14:38:32 +0200 | [diff] [blame] | 1590 | IRQF_ONESHOT | IRQF_SHARED, |
| 1591 | irq_name, mcasp); |
Peter Ujfalusi | 5a1b8a8 | 2014-12-30 16:10:32 +0200 | [diff] [blame] | 1592 | if (ret) { |
| 1593 | dev_err(&pdev->dev, "common IRQ request failed\n"); |
| 1594 | goto err; |
| 1595 | } |
| 1596 | |
| 1597 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN; |
| 1598 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN; |
| 1599 | } |
| 1600 | |
Misael Lopez Cruz | a7a3324 | 2014-11-12 16:38:05 +0200 | [diff] [blame] | 1601 | irq = platform_get_irq_byname(pdev, "rx"); |
| 1602 | if (irq >= 0) { |
| 1603 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx\n", |
| 1604 | dev_name(&pdev->dev)); |
| 1605 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 1606 | davinci_mcasp_rx_irq_handler, |
| 1607 | IRQF_ONESHOT, irq_name, mcasp); |
| 1608 | if (ret) { |
| 1609 | dev_err(&pdev->dev, "RX IRQ request failed\n"); |
| 1610 | goto err; |
| 1611 | } |
| 1612 | |
| 1613 | mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN; |
| 1614 | } |
| 1615 | |
| 1616 | irq = platform_get_irq_byname(pdev, "tx"); |
| 1617 | if (irq >= 0) { |
| 1618 | irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx\n", |
| 1619 | dev_name(&pdev->dev)); |
| 1620 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 1621 | davinci_mcasp_tx_irq_handler, |
| 1622 | IRQF_ONESHOT, irq_name, mcasp); |
| 1623 | if (ret) { |
| 1624 | dev_err(&pdev->dev, "TX IRQ request failed\n"); |
| 1625 | goto err; |
| 1626 | } |
| 1627 | |
| 1628 | mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN; |
| 1629 | } |
| 1630 | |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1631 | dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat"); |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 1632 | if (dat) |
| 1633 | mcasp->dat_port = true; |
Jyri Sarha | 256ba18 | 2013-10-18 18:37:42 +0300 | [diff] [blame] | 1634 | |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 1635 | dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK]; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 1636 | if (dat) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1637 | dma_data->addr = dat->start; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 1638 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1639 | dma_data->addr = mem->start + pdata->tx_dma_offset; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1640 | |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1641 | dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK]; |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1642 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1643 | if (res) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1644 | *dma = res->start; |
Jyri Sarha | 4023fe6 | 2013-10-18 18:37:43 +0300 | [diff] [blame] | 1645 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1646 | *dma = pdata->tx_dma_channel; |
Troy Kisky | 92e2a6f | 2009-09-11 14:29:03 -0700 | [diff] [blame] | 1647 | |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 1648 | /* dmaengine filter data for DT and non-DT boot */ |
| 1649 | if (pdev->dev.of_node) |
| 1650 | dma_data->filter_data = "tx"; |
| 1651 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1652 | dma_data->filter_data = dma; |
Peter Ujfalusi | 8de131f | 2014-03-14 16:42:46 +0200 | [diff] [blame] | 1653 | |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1654 | /* RX is not valid in DIT mode */ |
| 1655 | if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) { |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1656 | dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE]; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1657 | if (dat) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1658 | dma_data->addr = dat->start; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1659 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1660 | dma_data->addr = mem->start + pdata->rx_dma_offset; |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 1661 | |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1662 | dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE]; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1663 | res = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 1664 | if (res) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1665 | *dma = res->start; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1666 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1667 | *dma = pdata->rx_dma_channel; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1668 | |
| 1669 | /* dmaengine filter data for DT and non-DT boot */ |
| 1670 | if (pdev->dev.of_node) |
| 1671 | dma_data->filter_data = "rx"; |
| 1672 | else |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1673 | dma_data->filter_data = dma; |
Peter Ujfalusi | caa1d79 | 2015-02-02 14:38:33 +0200 | [diff] [blame] | 1674 | } |
Peter Ujfalusi | 453c499 | 2013-11-14 11:35:34 +0200 | [diff] [blame] | 1675 | |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 1676 | if (mcasp->version < MCASP_VERSION_3) { |
| 1677 | mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE; |
Peter Ujfalusi | 64ebdec | 2014-03-07 15:03:55 +0200 | [diff] [blame] | 1678 | /* dma_params->dma_addr is pointing to the data port address */ |
Peter Ujfalusi | cbc7956c | 2013-11-14 11:35:32 +0200 | [diff] [blame] | 1679 | mcasp->dat_port = true; |
| 1680 | } else { |
| 1681 | mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE; |
| 1682 | } |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1683 | |
Peter Ujfalusi | 70091a3 | 2013-11-14 11:35:29 +0200 | [diff] [blame] | 1684 | dev_set_drvdata(&pdev->dev, mcasp); |
Peter Ujfalusi | ae726e9 | 2013-11-14 11:35:35 +0200 | [diff] [blame] | 1685 | |
| 1686 | mcasp_reparent_fck(pdev); |
| 1687 | |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 1688 | ret = devm_snd_soc_register_component(&pdev->dev, |
| 1689 | &davinci_mcasp_component, |
| 1690 | &davinci_mcasp_dai[pdata->op_mode], 1); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1691 | |
| 1692 | if (ret != 0) |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 1693 | goto err; |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 1694 | |
Peter Ujfalusi | d5c6c59 | 2014-04-16 15:46:20 +0300 | [diff] [blame] | 1695 | switch (mcasp->version) { |
Peter Ujfalusi | f3f9cfa | 2014-07-16 15:12:04 +0300 | [diff] [blame] | 1696 | #if IS_BUILTIN(CONFIG_SND_EDMA_SOC) || \ |
| 1697 | (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \ |
| 1698 | IS_MODULE(CONFIG_SND_EDMA_SOC)) |
Peter Ujfalusi | 9759e7e | 2015-03-03 16:45:20 +0200 | [diff] [blame] | 1699 | case MCASP_VERSION_1: |
| 1700 | case MCASP_VERSION_2: |
Peter Ujfalusi | f3f9cfa | 2014-07-16 15:12:04 +0300 | [diff] [blame] | 1701 | case MCASP_VERSION_3: |
| 1702 | ret = edma_pcm_platform_register(&pdev->dev); |
| 1703 | break; |
| 1704 | #endif |
Jyri Sarha | 7f28f35 | 2014-06-13 12:49:59 +0300 | [diff] [blame] | 1705 | #if IS_BUILTIN(CONFIG_SND_OMAP_SOC) || \ |
| 1706 | (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \ |
| 1707 | IS_MODULE(CONFIG_SND_OMAP_SOC)) |
Peter Ujfalusi | d5c6c59 | 2014-04-16 15:46:20 +0300 | [diff] [blame] | 1708 | case MCASP_VERSION_4: |
| 1709 | ret = omap_pcm_platform_register(&pdev->dev); |
| 1710 | break; |
Jyri Sarha | 7f28f35 | 2014-06-13 12:49:59 +0300 | [diff] [blame] | 1711 | #endif |
Peter Ujfalusi | d5c6c59 | 2014-04-16 15:46:20 +0300 | [diff] [blame] | 1712 | default: |
| 1713 | dev_err(&pdev->dev, "Invalid McASP version: %d\n", |
| 1714 | mcasp->version); |
| 1715 | ret = -EINVAL; |
| 1716 | break; |
| 1717 | } |
| 1718 | |
| 1719 | if (ret) { |
| 1720 | dev_err(&pdev->dev, "register PCM failed: %d\n", ret); |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 1721 | goto err; |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 1722 | } |
| 1723 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1724 | return 0; |
| 1725 | |
Peter Ujfalusi | b6bb370 | 2014-04-22 14:03:13 +0300 | [diff] [blame] | 1726 | err: |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 1727 | pm_runtime_disable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1728 | return ret; |
| 1729 | } |
| 1730 | |
| 1731 | static int davinci_mcasp_remove(struct platform_device *pdev) |
| 1732 | { |
Hebbar, Gururaja | 1088434 | 2012-08-08 20:40:32 +0530 | [diff] [blame] | 1733 | pm_runtime_disable(&pdev->dev); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1734 | |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | static struct platform_driver davinci_mcasp_driver = { |
| 1739 | .probe = davinci_mcasp_probe, |
| 1740 | .remove = davinci_mcasp_remove, |
| 1741 | .driver = { |
| 1742 | .name = "davinci-mcasp", |
Sachin Kamat | ea421eb | 2013-05-22 16:53:37 +0530 | [diff] [blame] | 1743 | .of_match_table = mcasp_dt_ids, |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1744 | }, |
| 1745 | }; |
| 1746 | |
Axel Lin | f9b8a51 | 2011-11-25 10:09:27 +0800 | [diff] [blame] | 1747 | module_platform_driver(davinci_mcasp_driver); |
Chaithrika U S | b67f448 | 2009-06-05 06:28:40 -0400 | [diff] [blame] | 1748 | |
| 1749 | MODULE_AUTHOR("Steve Chen"); |
| 1750 | MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface"); |
| 1751 | MODULE_LICENSE("GPL"); |