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