Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * omap-mcbsp.c -- OMAP ALSA SoC DAI driver using McBSP port |
| 3 | * |
| 4 | * Copyright (C) 2008 Nokia Corporation |
| 5 | * |
Jarkko Nikula | b08f7a6 | 2009-04-17 14:42:26 +0300 | [diff] [blame] | 6 | * Contact: Jarkko Nikula <jhnikula@gmail.com> |
| 7 | * Peter Ujfalusi <peter.ujfalusi@nokia.com> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/pcm.h> |
| 30 | #include <sound/pcm_params.h> |
| 31 | #include <sound/initval.h> |
| 32 | #include <sound/soc.h> |
| 33 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 34 | #include <mach/control.h> |
| 35 | #include <mach/dma.h> |
| 36 | #include <mach/mcbsp.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 37 | #include "omap-mcbsp.h" |
| 38 | #include "omap-pcm.h" |
| 39 | |
Jarkko Nikula | 0b60485 | 2008-11-12 17:05:51 +0200 | [diff] [blame] | 40 | #define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 41 | |
| 42 | struct omap_mcbsp_data { |
| 43 | unsigned int bus_id; |
| 44 | struct omap_mcbsp_reg_cfg regs; |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 45 | unsigned int fmt; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 46 | /* |
| 47 | * Flags indicating is the bus already activated and configured by |
| 48 | * another substream |
| 49 | */ |
| 50 | int active; |
| 51 | int configured; |
| 52 | }; |
| 53 | |
| 54 | #define to_mcbsp(priv) container_of((priv), struct omap_mcbsp_data, bus_id) |
| 55 | |
| 56 | static struct omap_mcbsp_data mcbsp_data[NUM_LINKS]; |
| 57 | |
| 58 | /* |
| 59 | * Stream DMA parameters. DMA request line and port address are set runtime |
| 60 | * since they are different between OMAP1 and later OMAPs |
| 61 | */ |
Jarkko Nikula | 2e89713 | 2008-10-09 15:57:21 +0300 | [diff] [blame] | 62 | static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2]; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 63 | |
| 64 | #if defined(CONFIG_ARCH_OMAP15XX) || defined(CONFIG_ARCH_OMAP16XX) |
| 65 | static const int omap1_dma_reqs[][2] = { |
| 66 | { OMAP_DMA_MCBSP1_TX, OMAP_DMA_MCBSP1_RX }, |
| 67 | { OMAP_DMA_MCBSP2_TX, OMAP_DMA_MCBSP2_RX }, |
| 68 | { OMAP_DMA_MCBSP3_TX, OMAP_DMA_MCBSP3_RX }, |
| 69 | }; |
| 70 | static const unsigned long omap1_mcbsp_port[][2] = { |
| 71 | { OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1, |
| 72 | OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 73 | { OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1, |
| 74 | OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 75 | { OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DXR1, |
| 76 | OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 77 | }; |
| 78 | #else |
| 79 | static const int omap1_dma_reqs[][2] = {}; |
| 80 | static const unsigned long omap1_mcbsp_port[][2] = {}; |
| 81 | #endif |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 82 | |
| 83 | #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) |
| 84 | static const int omap24xx_dma_reqs[][2] = { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 85 | { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX }, |
| 86 | { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX }, |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 87 | #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX) |
| 88 | { OMAP24XX_DMA_MCBSP3_TX, OMAP24XX_DMA_MCBSP3_RX }, |
| 89 | { OMAP24XX_DMA_MCBSP4_TX, OMAP24XX_DMA_MCBSP4_RX }, |
| 90 | { OMAP24XX_DMA_MCBSP5_TX, OMAP24XX_DMA_MCBSP5_RX }, |
| 91 | #endif |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 92 | }; |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 93 | #else |
| 94 | static const int omap24xx_dma_reqs[][2] = {}; |
| 95 | #endif |
| 96 | |
| 97 | #if defined(CONFIG_ARCH_OMAP2420) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 98 | static const unsigned long omap2420_mcbsp_port[][2] = { |
| 99 | { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1, |
| 100 | OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 101 | { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1, |
| 102 | OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 103 | }; |
| 104 | #else |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 105 | static const unsigned long omap2420_mcbsp_port[][2] = {}; |
| 106 | #endif |
| 107 | |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 108 | #if defined(CONFIG_ARCH_OMAP2430) |
| 109 | static const unsigned long omap2430_mcbsp_port[][2] = { |
| 110 | { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR, |
| 111 | OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR }, |
| 112 | { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR, |
| 113 | OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR }, |
| 114 | { OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DXR, |
| 115 | OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DRR }, |
| 116 | { OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DXR, |
| 117 | OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DRR }, |
| 118 | { OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DXR, |
| 119 | OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DRR }, |
| 120 | }; |
| 121 | #else |
| 122 | static const unsigned long omap2430_mcbsp_port[][2] = {}; |
| 123 | #endif |
| 124 | |
| 125 | #if defined(CONFIG_ARCH_OMAP34XX) |
| 126 | static const unsigned long omap34xx_mcbsp_port[][2] = { |
| 127 | { OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR, |
| 128 | OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR }, |
| 129 | { OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR, |
| 130 | OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR }, |
| 131 | { OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DXR, |
| 132 | OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DRR }, |
| 133 | { OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DXR, |
| 134 | OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DRR }, |
| 135 | { OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DXR, |
| 136 | OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DRR }, |
| 137 | }; |
| 138 | #else |
| 139 | static const unsigned long omap34xx_mcbsp_port[][2] = {}; |
| 140 | #endif |
| 141 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 142 | static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream, |
| 143 | struct snd_soc_dai *dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 144 | { |
| 145 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 146 | struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 147 | struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data); |
| 148 | int err = 0; |
| 149 | |
Jarkko Nikula | 6984992 | 2009-03-27 15:32:01 +0200 | [diff] [blame] | 150 | if (cpu_is_omap343x() && mcbsp_data->bus_id == 1) { |
| 151 | /* |
| 152 | * McBSP2 in OMAP3 has 1024 * 32-bit internal audio buffer. |
| 153 | * Set constraint for minimum buffer size to the same than FIFO |
| 154 | * size in order to avoid underruns in playback startup because |
| 155 | * HW is keeping the DMA request active until FIFO is filled. |
| 156 | */ |
| 157 | snd_pcm_hw_constraint_minmax(substream->runtime, |
| 158 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4096, UINT_MAX); |
| 159 | } |
| 160 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 161 | if (!cpu_dai->active) |
| 162 | err = omap_mcbsp_request(mcbsp_data->bus_id); |
| 163 | |
| 164 | return err; |
| 165 | } |
| 166 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 167 | static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream, |
| 168 | struct snd_soc_dai *dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 169 | { |
| 170 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 171 | struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 172 | struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data); |
| 173 | |
| 174 | if (!cpu_dai->active) { |
| 175 | omap_mcbsp_free(mcbsp_data->bus_id); |
| 176 | mcbsp_data->configured = 0; |
| 177 | } |
| 178 | } |
| 179 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 180 | static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd, |
| 181 | struct snd_soc_dai *dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 182 | { |
| 183 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 184 | struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 185 | struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data); |
Jarkko Nikula | c12abc0 | 2009-08-07 09:59:47 +0300 | [diff] [blame] | 186 | int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 187 | |
| 188 | switch (cmd) { |
| 189 | case SNDRV_PCM_TRIGGER_START: |
| 190 | case SNDRV_PCM_TRIGGER_RESUME: |
| 191 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Jarkko Nikula | c12abc0 | 2009-08-07 09:59:47 +0300 | [diff] [blame] | 192 | mcbsp_data->active++; |
| 193 | omap_mcbsp_start(mcbsp_data->bus_id, play, !play); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 194 | break; |
| 195 | |
| 196 | case SNDRV_PCM_TRIGGER_STOP: |
| 197 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 198 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Jarkko Nikula | c12abc0 | 2009-08-07 09:59:47 +0300 | [diff] [blame] | 199 | omap_mcbsp_stop(mcbsp_data->bus_id, play, !play); |
| 200 | mcbsp_data->active--; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 201 | break; |
| 202 | default: |
| 203 | err = -EINVAL; |
| 204 | } |
| 205 | |
| 206 | return err; |
| 207 | } |
| 208 | |
| 209 | static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 210 | struct snd_pcm_hw_params *params, |
| 211 | struct snd_soc_dai *dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 212 | { |
| 213 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 214 | struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 215 | struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data); |
| 216 | struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs; |
| 217 | int dma, bus_id = mcbsp_data->bus_id, id = cpu_dai->id; |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 218 | int wlen, channels, wpf; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 219 | unsigned long port; |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 220 | unsigned int format; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 221 | |
| 222 | if (cpu_class_is_omap1()) { |
| 223 | dma = omap1_dma_reqs[bus_id][substream->stream]; |
| 224 | port = omap1_mcbsp_port[bus_id][substream->stream]; |
| 225 | } else if (cpu_is_omap2420()) { |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 226 | dma = omap24xx_dma_reqs[bus_id][substream->stream]; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 227 | port = omap2420_mcbsp_port[bus_id][substream->stream]; |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 228 | } else if (cpu_is_omap2430()) { |
| 229 | dma = omap24xx_dma_reqs[bus_id][substream->stream]; |
| 230 | port = omap2430_mcbsp_port[bus_id][substream->stream]; |
| 231 | } else if (cpu_is_omap343x()) { |
| 232 | dma = omap24xx_dma_reqs[bus_id][substream->stream]; |
| 233 | port = omap34xx_mcbsp_port[bus_id][substream->stream]; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 234 | } else { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 235 | return -ENODEV; |
| 236 | } |
Jarkko Nikula | 2e89713 | 2008-10-09 15:57:21 +0300 | [diff] [blame] | 237 | omap_mcbsp_dai_dma_params[id][substream->stream].name = |
| 238 | substream->stream ? "Audio Capture" : "Audio Playback"; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 239 | omap_mcbsp_dai_dma_params[id][substream->stream].dma_req = dma; |
| 240 | omap_mcbsp_dai_dma_params[id][substream->stream].port_addr = port; |
| 241 | cpu_dai->dma_data = &omap_mcbsp_dai_dma_params[id][substream->stream]; |
| 242 | |
| 243 | if (mcbsp_data->configured) { |
| 244 | /* McBSP already configured by another stream */ |
| 245 | return 0; |
| 246 | } |
| 247 | |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 248 | format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK; |
| 249 | wpf = channels = params_channels(params); |
Jarkko Nikula | 375e8a7 | 2008-11-25 12:45:09 +0200 | [diff] [blame] | 250 | switch (channels) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 251 | case 2: |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 252 | if (format == SND_SOC_DAIFMT_I2S) { |
| 253 | /* Use dual-phase frames */ |
| 254 | regs->rcr2 |= RPHASE; |
| 255 | regs->xcr2 |= XPHASE; |
| 256 | /* Set 1 word per (McBSP) frame for phase1 and phase2 */ |
| 257 | wpf--; |
| 258 | regs->rcr2 |= RFRLEN2(wpf - 1); |
| 259 | regs->xcr2 |= XFRLEN2(wpf - 1); |
| 260 | } |
Jarkko Nikula | 375e8a7 | 2008-11-25 12:45:09 +0200 | [diff] [blame] | 261 | case 1: |
Peter Ujfalusi | 31a00c6 | 2009-04-23 14:36:48 +0300 | [diff] [blame] | 262 | case 4: |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 263 | /* Set word per (McBSP) frame for phase1 */ |
| 264 | regs->rcr1 |= RFRLEN1(wpf - 1); |
| 265 | regs->xcr1 |= XFRLEN1(wpf - 1); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 266 | break; |
| 267 | default: |
| 268 | /* Unsupported number of channels */ |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
| 272 | switch (params_format(params)) { |
| 273 | case SNDRV_PCM_FORMAT_S16_LE: |
| 274 | /* Set word lengths */ |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 275 | wlen = 16; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 276 | regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16); |
| 277 | regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16); |
| 278 | regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16); |
| 279 | regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 280 | break; |
| 281 | default: |
| 282 | /* Unsupported PCM format */ |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 286 | /* Set FS period and length in terms of bit clock periods */ |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 287 | switch (format) { |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 288 | case SND_SOC_DAIFMT_I2S: |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 289 | regs->srgr2 |= FPER(wlen * channels - 1); |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 290 | regs->srgr1 |= FWID(wlen - 1); |
| 291 | break; |
Peter Ujfalusi | 3ba191c | 2009-04-15 15:38:56 +0300 | [diff] [blame] | 292 | case SND_SOC_DAIFMT_DSP_A: |
Jarkko Nikula | bd25867 | 2008-12-22 10:21:36 +0200 | [diff] [blame] | 293 | case SND_SOC_DAIFMT_DSP_B: |
Jarkko Nikula | 375e8a7 | 2008-11-25 12:45:09 +0200 | [diff] [blame] | 294 | regs->srgr2 |= FPER(wlen * channels - 1); |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 295 | regs->srgr1 |= FWID(0); |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 299 | omap_mcbsp_config(bus_id, &mcbsp_data->regs); |
| 300 | mcbsp_data->configured = 1; |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * This must be called before _set_clkdiv and _set_sysclk since McBSP register |
| 307 | * cache is initialized here |
| 308 | */ |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 309 | static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 310 | unsigned int fmt) |
| 311 | { |
| 312 | struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data); |
| 313 | struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs; |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 314 | unsigned int temp_fmt = fmt; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 315 | |
| 316 | if (mcbsp_data->configured) |
| 317 | return 0; |
| 318 | |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 319 | mcbsp_data->fmt = fmt; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 320 | memset(regs, 0, sizeof(*regs)); |
| 321 | /* Generic McBSP register settings */ |
| 322 | regs->spcr2 |= XINTM(3) | FREE; |
| 323 | regs->spcr1 |= RINTM(3); |
Eero Nurkkala | c721bbd | 2009-08-20 16:18:23 +0300 | [diff] [blame^] | 324 | /* RFIG and XFIG are not defined in 34xx */ |
| 325 | if (!cpu_is_omap34xx()) { |
| 326 | regs->rcr2 |= RFIG; |
| 327 | regs->xcr2 |= XFIG; |
| 328 | } |
Misael Lopez Cruz | ef390c0 | 2009-01-29 13:29:46 +0200 | [diff] [blame] | 329 | if (cpu_is_omap2430() || cpu_is_omap34xx()) { |
| 330 | regs->xccr = DXENDLY(1) | XDMAEN; |
| 331 | regs->rccr = RFULL_CYCLE | RDMAEN; |
| 332 | } |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 333 | |
| 334 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 335 | case SND_SOC_DAIFMT_I2S: |
| 336 | /* 1-bit data delay */ |
| 337 | regs->rcr2 |= RDATDLY(1); |
| 338 | regs->xcr2 |= XDATDLY(1); |
| 339 | break; |
Peter Ujfalusi | 3ba191c | 2009-04-15 15:38:56 +0300 | [diff] [blame] | 340 | case SND_SOC_DAIFMT_DSP_A: |
| 341 | /* 1-bit data delay */ |
| 342 | regs->rcr2 |= RDATDLY(1); |
| 343 | regs->xcr2 |= XDATDLY(1); |
| 344 | /* Invert FS polarity configuration */ |
| 345 | temp_fmt ^= SND_SOC_DAIFMT_NB_IF; |
| 346 | break; |
Jarkko Nikula | bd25867 | 2008-12-22 10:21:36 +0200 | [diff] [blame] | 347 | case SND_SOC_DAIFMT_DSP_B: |
Arun KS | 3336c5b | 2008-10-02 15:07:06 +0530 | [diff] [blame] | 348 | /* 0-bit data delay */ |
| 349 | regs->rcr2 |= RDATDLY(0); |
| 350 | regs->xcr2 |= XDATDLY(0); |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 351 | /* Invert FS polarity configuration */ |
| 352 | temp_fmt ^= SND_SOC_DAIFMT_NB_IF; |
Arun KS | 3336c5b | 2008-10-02 15:07:06 +0530 | [diff] [blame] | 353 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 354 | default: |
| 355 | /* Unsupported data format */ |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | |
| 359 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 360 | case SND_SOC_DAIFMT_CBS_CFS: |
| 361 | /* McBSP master. Set FS and bit clocks as outputs */ |
| 362 | regs->pcr0 |= FSXM | FSRM | |
| 363 | CLKXM | CLKRM; |
| 364 | /* Sample rate generator drives the FS */ |
| 365 | regs->srgr2 |= FSGM; |
| 366 | break; |
| 367 | case SND_SOC_DAIFMT_CBM_CFM: |
| 368 | /* McBSP slave */ |
| 369 | break; |
| 370 | default: |
| 371 | /* Unsupported master/slave configuration */ |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | |
| 375 | /* Set bit clock (CLKX/CLKR) and FS polarities */ |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 376 | switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 377 | case SND_SOC_DAIFMT_NB_NF: |
| 378 | /* |
| 379 | * Normal BCLK + FS. |
| 380 | * FS active low. TX data driven on falling edge of bit clock |
| 381 | * and RX data sampled on rising edge of bit clock. |
| 382 | */ |
| 383 | regs->pcr0 |= FSXP | FSRP | |
| 384 | CLKXP | CLKRP; |
| 385 | break; |
| 386 | case SND_SOC_DAIFMT_NB_IF: |
| 387 | regs->pcr0 |= CLKXP | CLKRP; |
| 388 | break; |
| 389 | case SND_SOC_DAIFMT_IB_NF: |
| 390 | regs->pcr0 |= FSXP | FSRP; |
| 391 | break; |
| 392 | case SND_SOC_DAIFMT_IB_IF: |
| 393 | break; |
| 394 | default: |
| 395 | return -EINVAL; |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 401 | static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 402 | int div_id, int div) |
| 403 | { |
| 404 | struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data); |
| 405 | struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs; |
| 406 | |
| 407 | if (div_id != OMAP_MCBSP_CLKGDV) |
| 408 | return -ENODEV; |
| 409 | |
| 410 | regs->srgr1 |= CLKGDV(div - 1); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int omap_mcbsp_dai_set_clks_src(struct omap_mcbsp_data *mcbsp_data, |
| 416 | int clk_id) |
| 417 | { |
| 418 | int sel_bit; |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 419 | u16 reg, reg_devconf1 = OMAP243X_CONTROL_DEVCONF1; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 420 | |
| 421 | if (cpu_class_is_omap1()) { |
| 422 | /* OMAP1's can use only external source clock */ |
| 423 | if (unlikely(clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK)) |
| 424 | return -EINVAL; |
| 425 | else |
| 426 | return 0; |
| 427 | } |
| 428 | |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 429 | if (cpu_is_omap2420() && mcbsp_data->bus_id > 1) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | if (cpu_is_omap343x()) |
| 433 | reg_devconf1 = OMAP343X_CONTROL_DEVCONF1; |
| 434 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 435 | switch (mcbsp_data->bus_id) { |
| 436 | case 0: |
| 437 | reg = OMAP2_CONTROL_DEVCONF0; |
| 438 | sel_bit = 2; |
| 439 | break; |
| 440 | case 1: |
| 441 | reg = OMAP2_CONTROL_DEVCONF0; |
| 442 | sel_bit = 6; |
| 443 | break; |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 444 | case 2: |
| 445 | reg = reg_devconf1; |
| 446 | sel_bit = 0; |
| 447 | break; |
| 448 | case 3: |
| 449 | reg = reg_devconf1; |
| 450 | sel_bit = 2; |
| 451 | break; |
| 452 | case 4: |
| 453 | reg = reg_devconf1; |
| 454 | sel_bit = 4; |
| 455 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 456 | default: |
| 457 | return -EINVAL; |
| 458 | } |
| 459 | |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 460 | if (clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK) |
| 461 | omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg); |
| 462 | else |
| 463 | omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 468 | static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 469 | int clk_id, unsigned int freq, |
| 470 | int dir) |
| 471 | { |
| 472 | struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data); |
| 473 | struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs; |
| 474 | int err = 0; |
| 475 | |
| 476 | switch (clk_id) { |
| 477 | case OMAP_MCBSP_SYSCLK_CLK: |
| 478 | regs->srgr2 |= CLKSM; |
| 479 | break; |
| 480 | case OMAP_MCBSP_SYSCLK_CLKS_FCLK: |
| 481 | case OMAP_MCBSP_SYSCLK_CLKS_EXT: |
| 482 | err = omap_mcbsp_dai_set_clks_src(mcbsp_data, clk_id); |
| 483 | break; |
| 484 | |
| 485 | case OMAP_MCBSP_SYSCLK_CLKX_EXT: |
| 486 | regs->srgr2 |= CLKSM; |
| 487 | case OMAP_MCBSP_SYSCLK_CLKR_EXT: |
| 488 | regs->pcr0 |= SCLKME; |
| 489 | break; |
| 490 | default: |
| 491 | err = -ENODEV; |
| 492 | } |
| 493 | |
| 494 | return err; |
| 495 | } |
| 496 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 497 | static struct snd_soc_dai_ops omap_mcbsp_dai_ops = { |
| 498 | .startup = omap_mcbsp_dai_startup, |
| 499 | .shutdown = omap_mcbsp_dai_shutdown, |
| 500 | .trigger = omap_mcbsp_dai_trigger, |
| 501 | .hw_params = omap_mcbsp_dai_hw_params, |
| 502 | .set_fmt = omap_mcbsp_dai_set_dai_fmt, |
| 503 | .set_clkdiv = omap_mcbsp_dai_set_clkdiv, |
| 504 | .set_sysclk = omap_mcbsp_dai_set_dai_sysclk, |
| 505 | }; |
| 506 | |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 507 | #define OMAP_MCBSP_DAI_BUILDER(link_id) \ |
| 508 | { \ |
Jarkko Nikula | 0c758bd | 2008-11-21 14:31:33 +0200 | [diff] [blame] | 509 | .name = "omap-mcbsp-dai-"#link_id, \ |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 510 | .id = (link_id), \ |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 511 | .playback = { \ |
Jarkko Nikula | 375e8a7 | 2008-11-25 12:45:09 +0200 | [diff] [blame] | 512 | .channels_min = 1, \ |
Peter Ujfalusi | 31a00c6 | 2009-04-23 14:36:48 +0300 | [diff] [blame] | 513 | .channels_max = 4, \ |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 514 | .rates = OMAP_MCBSP_RATES, \ |
| 515 | .formats = SNDRV_PCM_FMTBIT_S16_LE, \ |
| 516 | }, \ |
| 517 | .capture = { \ |
Jarkko Nikula | 375e8a7 | 2008-11-25 12:45:09 +0200 | [diff] [blame] | 518 | .channels_min = 1, \ |
Peter Ujfalusi | 31a00c6 | 2009-04-23 14:36:48 +0300 | [diff] [blame] | 519 | .channels_max = 4, \ |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 520 | .rates = OMAP_MCBSP_RATES, \ |
| 521 | .formats = SNDRV_PCM_FMTBIT_S16_LE, \ |
| 522 | }, \ |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 523 | .ops = &omap_mcbsp_dai_ops, \ |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 524 | .private_data = &mcbsp_data[(link_id)].bus_id, \ |
| 525 | } |
| 526 | |
| 527 | struct snd_soc_dai omap_mcbsp_dai[] = { |
| 528 | OMAP_MCBSP_DAI_BUILDER(0), |
| 529 | OMAP_MCBSP_DAI_BUILDER(1), |
| 530 | #if NUM_LINKS >= 3 |
| 531 | OMAP_MCBSP_DAI_BUILDER(2), |
| 532 | #endif |
| 533 | #if NUM_LINKS == 5 |
| 534 | OMAP_MCBSP_DAI_BUILDER(3), |
| 535 | OMAP_MCBSP_DAI_BUILDER(4), |
| 536 | #endif |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 537 | }; |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 538 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 539 | EXPORT_SYMBOL_GPL(omap_mcbsp_dai); |
| 540 | |
Takashi Iwai | f73f2a6 | 2008-12-10 07:59:33 +0100 | [diff] [blame] | 541 | static int __init snd_omap_mcbsp_init(void) |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 542 | { |
| 543 | return snd_soc_register_dais(omap_mcbsp_dai, |
| 544 | ARRAY_SIZE(omap_mcbsp_dai)); |
| 545 | } |
Takashi Iwai | f73f2a6 | 2008-12-10 07:59:33 +0100 | [diff] [blame] | 546 | module_init(snd_omap_mcbsp_init); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 547 | |
Takashi Iwai | f73f2a6 | 2008-12-10 07:59:33 +0100 | [diff] [blame] | 548 | static void __exit snd_omap_mcbsp_exit(void) |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 549 | { |
| 550 | snd_soc_unregister_dais(omap_mcbsp_dai, ARRAY_SIZE(omap_mcbsp_dai)); |
| 551 | } |
Takashi Iwai | f73f2a6 | 2008-12-10 07:59:33 +0100 | [diff] [blame] | 552 | module_exit(snd_omap_mcbsp_exit); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 553 | |
Jarkko Nikula | b08f7a6 | 2009-04-17 14:42:26 +0300 | [diff] [blame] | 554 | MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>"); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 555 | MODULE_DESCRIPTION("OMAP I2S SoC Interface"); |
| 556 | MODULE_LICENSE("GPL"); |