Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 1 | /* |
| 2 | * ALSA SoC Voice Codec Interface for TI DAVINCI processor |
| 3 | * |
| 4 | * Copyright (C) 2010 Texas Instruments. |
| 5 | * |
| 6 | * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/delay.h> |
Tejun Heo | 923a004 | 2010-03-30 02:52:29 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 28 | #include <linux/io.h> |
| 29 | #include <linux/mfd/davinci_voicecodec.h> |
| 30 | |
| 31 | #include <sound/core.h> |
| 32 | #include <sound/pcm.h> |
| 33 | #include <sound/pcm_params.h> |
| 34 | #include <sound/initval.h> |
| 35 | #include <sound/soc.h> |
Peter Ujfalusi | 62731d3 | 2015-03-03 16:45:19 +0200 | [diff] [blame] | 36 | #include <sound/dmaengine_pcm.h> |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 37 | |
Peter Ujfalusi | 62731d3 | 2015-03-03 16:45:19 +0200 | [diff] [blame] | 38 | #include "edma-pcm.h" |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 39 | #include "davinci-i2s.h" |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 40 | |
| 41 | #define MOD_REG_BIT(val, mask, set) do { \ |
| 42 | if (set) { \ |
| 43 | val |= mask; \ |
| 44 | } else { \ |
| 45 | val &= ~mask; \ |
| 46 | } \ |
| 47 | } while (0) |
| 48 | |
| 49 | struct davinci_vcif_dev { |
| 50 | struct davinci_vc *davinci_vc; |
Peter Ujfalusi | 62731d3 | 2015-03-03 16:45:19 +0200 | [diff] [blame] | 51 | struct snd_dmaengine_dai_dma_data dma_data[2]; |
| 52 | int dma_request[2]; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static void davinci_vcif_start(struct snd_pcm_substream *substream) |
| 56 | { |
| 57 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 58 | struct davinci_vcif_dev *davinci_vcif_dev = |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 59 | snd_soc_dai_get_drvdata(rtd->cpu_dai); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 60 | struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc; |
| 61 | u32 w; |
| 62 | |
| 63 | /* Start the sample generator and enable transmitter/receiver */ |
| 64 | w = readl(davinci_vc->base + DAVINCI_VC_CTRL); |
| 65 | |
| 66 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Rajashekhara, Sudhakar | 3012f43 | 2011-07-20 17:36:04 +0530 | [diff] [blame] | 67 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 0); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 68 | else |
Rajashekhara, Sudhakar | 3012f43 | 2011-07-20 17:36:04 +0530 | [diff] [blame] | 69 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 0); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 70 | |
| 71 | writel(w, davinci_vc->base + DAVINCI_VC_CTRL); |
| 72 | } |
| 73 | |
| 74 | static void davinci_vcif_stop(struct snd_pcm_substream *substream) |
| 75 | { |
| 76 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 77 | struct davinci_vcif_dev *davinci_vcif_dev = |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 78 | snd_soc_dai_get_drvdata(rtd->cpu_dai); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 79 | struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc; |
| 80 | u32 w; |
| 81 | |
| 82 | /* Reset transmitter/receiver and sample rate/frame sync generators */ |
| 83 | w = readl(davinci_vc->base + DAVINCI_VC_CTRL); |
| 84 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Rajashekhara, Sudhakar | 3012f43 | 2011-07-20 17:36:04 +0530 | [diff] [blame] | 85 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 1); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 86 | else |
Rajashekhara, Sudhakar | 3012f43 | 2011-07-20 17:36:04 +0530 | [diff] [blame] | 87 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 1); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 88 | |
| 89 | writel(w, davinci_vc->base + DAVINCI_VC_CTRL); |
| 90 | } |
| 91 | |
| 92 | static int davinci_vcif_hw_params(struct snd_pcm_substream *substream, |
| 93 | struct snd_pcm_hw_params *params, |
| 94 | struct snd_soc_dai *dai) |
| 95 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 96 | struct davinci_vcif_dev *davinci_vcif_dev = snd_soc_dai_get_drvdata(dai); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 97 | struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 98 | u32 w; |
| 99 | |
| 100 | /* Restart the codec before setup */ |
| 101 | davinci_vcif_stop(substream); |
| 102 | davinci_vcif_start(substream); |
| 103 | |
| 104 | /* General line settings */ |
| 105 | writel(DAVINCI_VC_CTRL_MASK, davinci_vc->base + DAVINCI_VC_CTRL); |
| 106 | |
| 107 | writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTCLR); |
| 108 | |
| 109 | writel(DAVINCI_VC_INT_MASK, davinci_vc->base + DAVINCI_VC_INTEN); |
| 110 | |
| 111 | w = readl(davinci_vc->base + DAVINCI_VC_CTRL); |
| 112 | |
| 113 | /* Determine xfer data type */ |
| 114 | switch (params_format(params)) { |
| 115 | case SNDRV_PCM_FORMAT_U8: |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 116 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 | |
| 117 | DAVINCI_VC_CTRL_RD_UNSIGNED | |
| 118 | DAVINCI_VC_CTRL_WD_BITS_8 | |
| 119 | DAVINCI_VC_CTRL_WD_UNSIGNED, 1); |
| 120 | break; |
| 121 | case SNDRV_PCM_FORMAT_S8: |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 122 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 | |
| 123 | DAVINCI_VC_CTRL_WD_BITS_8, 1); |
| 124 | |
| 125 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_UNSIGNED | |
| 126 | DAVINCI_VC_CTRL_WD_UNSIGNED, 0); |
| 127 | break; |
| 128 | case SNDRV_PCM_FORMAT_S16_LE: |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 129 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 | |
| 130 | DAVINCI_VC_CTRL_RD_UNSIGNED | |
| 131 | DAVINCI_VC_CTRL_WD_BITS_8 | |
| 132 | DAVINCI_VC_CTRL_WD_UNSIGNED, 0); |
| 133 | break; |
| 134 | default: |
| 135 | printk(KERN_WARNING "davinci-vcif: unsupported PCM format"); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 139 | writel(w, davinci_vc->base + DAVINCI_VC_CTRL); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int davinci_vcif_trigger(struct snd_pcm_substream *substream, int cmd, |
| 145 | struct snd_soc_dai *dai) |
| 146 | { |
| 147 | int ret = 0; |
| 148 | |
| 149 | switch (cmd) { |
| 150 | case SNDRV_PCM_TRIGGER_START: |
| 151 | case SNDRV_PCM_TRIGGER_RESUME: |
| 152 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 153 | davinci_vcif_start(substream); |
Rajashekhara, Sudhakar | 82d1d52 | 2011-07-20 17:37:18 +0530 | [diff] [blame] | 154 | break; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 155 | case SNDRV_PCM_TRIGGER_STOP: |
| 156 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 157 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 158 | davinci_vcif_stop(substream); |
| 159 | break; |
| 160 | default: |
| 161 | ret = -EINVAL; |
| 162 | } |
| 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | #define DAVINCI_VCIF_RATES SNDRV_PCM_RATE_8000_48000 |
| 168 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 169 | static const struct snd_soc_dai_ops davinci_vcif_dai_ops = { |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 170 | .trigger = davinci_vcif_trigger, |
| 171 | .hw_params = davinci_vcif_hw_params, |
| 172 | }; |
| 173 | |
Peter Ujfalusi | 62731d3 | 2015-03-03 16:45:19 +0200 | [diff] [blame] | 174 | static int davinci_vcif_dai_probe(struct snd_soc_dai *dai) |
| 175 | { |
| 176 | struct davinci_vcif_dev *dev = snd_soc_dai_get_drvdata(dai); |
| 177 | |
| 178 | dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK]; |
| 179 | dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE]; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 184 | static struct snd_soc_dai_driver davinci_vcif_dai = { |
Peter Ujfalusi | 62731d3 | 2015-03-03 16:45:19 +0200 | [diff] [blame] | 185 | .probe = davinci_vcif_dai_probe, |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 186 | .playback = { |
| 187 | .channels_min = 1, |
| 188 | .channels_max = 2, |
| 189 | .rates = DAVINCI_VCIF_RATES, |
| 190 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 191 | .capture = { |
| 192 | .channels_min = 1, |
| 193 | .channels_max = 2, |
| 194 | .rates = DAVINCI_VCIF_RATES, |
| 195 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 196 | .ops = &davinci_vcif_dai_ops, |
| 197 | |
| 198 | }; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 199 | |
Kuninori Morimoto | ee226ce | 2013-03-21 03:31:07 -0700 | [diff] [blame] | 200 | static const struct snd_soc_component_driver davinci_vcif_component = { |
| 201 | .name = "davinci-vcif", |
| 202 | }; |
| 203 | |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 204 | static int davinci_vcif_probe(struct platform_device *pdev) |
| 205 | { |
Samuel Ortiz | cb5811c | 2011-04-06 16:39:45 +0200 | [diff] [blame] | 206 | struct davinci_vc *davinci_vc = pdev->dev.platform_data; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 207 | struct davinci_vcif_dev *davinci_vcif_dev; |
| 208 | int ret; |
| 209 | |
Julia Lawall | a3bb8f3 | 2011-12-29 17:51:20 +0100 | [diff] [blame] | 210 | davinci_vcif_dev = devm_kzalloc(&pdev->dev, |
| 211 | sizeof(struct davinci_vcif_dev), |
| 212 | GFP_KERNEL); |
Kulikov Vasiliy | 55938b1 | 2010-07-16 20:16:17 +0400 | [diff] [blame] | 213 | if (!davinci_vcif_dev) { |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 214 | dev_dbg(&pdev->dev, |
| 215 | "could not allocate memory for private data\n"); |
| 216 | return -ENOMEM; |
| 217 | } |
| 218 | |
| 219 | /* DMA tx params */ |
| 220 | davinci_vcif_dev->davinci_vc = davinci_vc; |
Peter Ujfalusi | 62731d3 | 2015-03-03 16:45:19 +0200 | [diff] [blame] | 221 | davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data = |
| 222 | &davinci_vc->davinci_vcif.dma_tx_channel; |
| 223 | davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr = |
| 224 | davinci_vc->davinci_vcif.dma_tx_addr; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 225 | |
| 226 | /* DMA rx params */ |
Peter Ujfalusi | 62731d3 | 2015-03-03 16:45:19 +0200 | [diff] [blame] | 227 | davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].filter_data = |
| 228 | &davinci_vc->davinci_vcif.dma_rx_channel; |
| 229 | davinci_vcif_dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr = |
| 230 | davinci_vc->davinci_vcif.dma_rx_addr; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 231 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 232 | dev_set_drvdata(&pdev->dev, davinci_vcif_dev); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 233 | |
Kuninori Morimoto | ee226ce | 2013-03-21 03:31:07 -0700 | [diff] [blame] | 234 | ret = snd_soc_register_component(&pdev->dev, &davinci_vcif_component, |
| 235 | &davinci_vcif_dai, 1); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 236 | if (ret != 0) { |
| 237 | dev_err(&pdev->dev, "could not register dai\n"); |
Julia Lawall | a3bb8f3 | 2011-12-29 17:51:20 +0100 | [diff] [blame] | 238 | return ret; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 239 | } |
| 240 | |
Peter Ujfalusi | 62731d3 | 2015-03-03 16:45:19 +0200 | [diff] [blame] | 241 | ret = edma_pcm_platform_register(&pdev->dev); |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 242 | if (ret) { |
| 243 | dev_err(&pdev->dev, "register PCM failed: %d\n", ret); |
Kuninori Morimoto | ee226ce | 2013-03-21 03:31:07 -0700 | [diff] [blame] | 244 | snd_soc_unregister_component(&pdev->dev); |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 245 | return ret; |
| 246 | } |
| 247 | |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 248 | return 0; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static int davinci_vcif_remove(struct platform_device *pdev) |
| 252 | { |
Kuninori Morimoto | ee226ce | 2013-03-21 03:31:07 -0700 | [diff] [blame] | 253 | snd_soc_unregister_component(&pdev->dev); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static struct platform_driver davinci_vcif_driver = { |
| 259 | .probe = davinci_vcif_probe, |
| 260 | .remove = davinci_vcif_remove, |
| 261 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 262 | .name = "davinci-vcif", |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 263 | }, |
| 264 | }; |
| 265 | |
Axel Lin | f9b8a51 | 2011-11-25 10:09:27 +0800 | [diff] [blame] | 266 | module_platform_driver(davinci_vcif_driver); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 267 | |
| 268 | MODULE_AUTHOR("Miguel Aguilar"); |
| 269 | MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface"); |
| 270 | MODULE_LICENSE("GPL"); |