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> |
| 36 | |
| 37 | #include "davinci-pcm.h" |
| 38 | #include "davinci-i2s.h" |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 39 | |
| 40 | #define MOD_REG_BIT(val, mask, set) do { \ |
| 41 | if (set) { \ |
| 42 | val |= mask; \ |
| 43 | } else { \ |
| 44 | val &= ~mask; \ |
| 45 | } \ |
| 46 | } while (0) |
| 47 | |
| 48 | struct davinci_vcif_dev { |
| 49 | struct davinci_vc *davinci_vc; |
| 50 | struct davinci_pcm_dma_params dma_params[2]; |
| 51 | }; |
| 52 | |
| 53 | static void davinci_vcif_start(struct snd_pcm_substream *substream) |
| 54 | { |
| 55 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 56 | struct davinci_vcif_dev *davinci_vcif_dev = |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 57 | snd_soc_dai_get_drvdata(rtd->cpu_dai); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 58 | struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc; |
| 59 | u32 w; |
| 60 | |
| 61 | /* Start the sample generator and enable transmitter/receiver */ |
| 62 | w = readl(davinci_vc->base + DAVINCI_VC_CTRL); |
| 63 | |
| 64 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Rajashekhara, Sudhakar | 3012f43 | 2011-07-20 17:36:04 +0530 | [diff] [blame] | 65 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 0); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 66 | else |
Rajashekhara, Sudhakar | 3012f43 | 2011-07-20 17:36:04 +0530 | [diff] [blame] | 67 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 0); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 68 | |
| 69 | writel(w, davinci_vc->base + DAVINCI_VC_CTRL); |
| 70 | } |
| 71 | |
| 72 | static void davinci_vcif_stop(struct snd_pcm_substream *substream) |
| 73 | { |
| 74 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 75 | struct davinci_vcif_dev *davinci_vcif_dev = |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 76 | snd_soc_dai_get_drvdata(rtd->cpu_dai); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 77 | struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc; |
| 78 | u32 w; |
| 79 | |
| 80 | /* Reset transmitter/receiver and sample rate/frame sync generators */ |
| 81 | w = readl(davinci_vc->base + DAVINCI_VC_CTRL); |
| 82 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Rajashekhara, Sudhakar | 3012f43 | 2011-07-20 17:36:04 +0530 | [diff] [blame] | 83 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTDAC, 1); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 84 | else |
Rajashekhara, Sudhakar | 3012f43 | 2011-07-20 17:36:04 +0530 | [diff] [blame] | 85 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RSTADC, 1); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 86 | |
| 87 | writel(w, davinci_vc->base + DAVINCI_VC_CTRL); |
| 88 | } |
| 89 | |
| 90 | static int davinci_vcif_hw_params(struct snd_pcm_substream *substream, |
| 91 | struct snd_pcm_hw_params *params, |
| 92 | struct snd_soc_dai *dai) |
| 93 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 94 | 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] | 95 | struct davinci_vc *davinci_vc = davinci_vcif_dev->davinci_vc; |
| 96 | struct davinci_pcm_dma_params *dma_params = |
| 97 | &davinci_vcif_dev->dma_params[substream->stream]; |
| 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: |
| 116 | dma_params->data_type = 0; |
| 117 | |
| 118 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 | |
| 119 | DAVINCI_VC_CTRL_RD_UNSIGNED | |
| 120 | DAVINCI_VC_CTRL_WD_BITS_8 | |
| 121 | DAVINCI_VC_CTRL_WD_UNSIGNED, 1); |
| 122 | break; |
| 123 | case SNDRV_PCM_FORMAT_S8: |
| 124 | dma_params->data_type = 1; |
| 125 | |
| 126 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 | |
| 127 | DAVINCI_VC_CTRL_WD_BITS_8, 1); |
| 128 | |
| 129 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_UNSIGNED | |
| 130 | DAVINCI_VC_CTRL_WD_UNSIGNED, 0); |
| 131 | break; |
| 132 | case SNDRV_PCM_FORMAT_S16_LE: |
| 133 | dma_params->data_type = 2; |
| 134 | |
| 135 | MOD_REG_BIT(w, DAVINCI_VC_CTRL_RD_BITS_8 | |
| 136 | DAVINCI_VC_CTRL_RD_UNSIGNED | |
| 137 | DAVINCI_VC_CTRL_WD_BITS_8 | |
| 138 | DAVINCI_VC_CTRL_WD_UNSIGNED, 0); |
| 139 | break; |
| 140 | default: |
| 141 | printk(KERN_WARNING "davinci-vcif: unsupported PCM format"); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | dma_params->acnt = dma_params->data_type; |
| 146 | |
| 147 | writel(w, davinci_vc->base + DAVINCI_VC_CTRL); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int davinci_vcif_trigger(struct snd_pcm_substream *substream, int cmd, |
| 153 | struct snd_soc_dai *dai) |
| 154 | { |
| 155 | int ret = 0; |
| 156 | |
| 157 | switch (cmd) { |
| 158 | case SNDRV_PCM_TRIGGER_START: |
| 159 | case SNDRV_PCM_TRIGGER_RESUME: |
| 160 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 161 | davinci_vcif_start(substream); |
Rajashekhara, Sudhakar | 82d1d52 | 2011-07-20 17:37:18 +0530 | [diff] [blame] | 162 | break; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 163 | case SNDRV_PCM_TRIGGER_STOP: |
| 164 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 165 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 166 | davinci_vcif_stop(substream); |
| 167 | break; |
| 168 | default: |
| 169 | ret = -EINVAL; |
| 170 | } |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
Chris Paulson-Ellis | bedad0c | 2010-11-16 12:27:09 +0000 | [diff] [blame] | 175 | static int davinci_vcif_startup(struct snd_pcm_substream *substream, |
| 176 | struct snd_soc_dai *dai) |
| 177 | { |
| 178 | struct davinci_vcif_dev *dev = snd_soc_dai_get_drvdata(dai); |
| 179 | |
| 180 | snd_soc_dai_set_dma_data(dai, substream, dev->dma_params); |
| 181 | return 0; |
| 182 | } |
| 183 | |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 184 | #define DAVINCI_VCIF_RATES SNDRV_PCM_RATE_8000_48000 |
| 185 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 186 | static const struct snd_soc_dai_ops davinci_vcif_dai_ops = { |
Chris Paulson-Ellis | bedad0c | 2010-11-16 12:27:09 +0000 | [diff] [blame] | 187 | .startup = davinci_vcif_startup, |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 188 | .trigger = davinci_vcif_trigger, |
| 189 | .hw_params = davinci_vcif_hw_params, |
| 190 | }; |
| 191 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 192 | static struct snd_soc_dai_driver davinci_vcif_dai = { |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 193 | .playback = { |
| 194 | .channels_min = 1, |
| 195 | .channels_max = 2, |
| 196 | .rates = DAVINCI_VCIF_RATES, |
| 197 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 198 | .capture = { |
| 199 | .channels_min = 1, |
| 200 | .channels_max = 2, |
| 201 | .rates = DAVINCI_VCIF_RATES, |
| 202 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 203 | .ops = &davinci_vcif_dai_ops, |
| 204 | |
| 205 | }; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 206 | |
Kuninori Morimoto | ee226ce | 2013-03-21 03:31:07 -0700 | [diff] [blame] | 207 | static const struct snd_soc_component_driver davinci_vcif_component = { |
| 208 | .name = "davinci-vcif", |
| 209 | }; |
| 210 | |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 211 | static int davinci_vcif_probe(struct platform_device *pdev) |
| 212 | { |
Samuel Ortiz | cb5811c | 2011-04-06 16:39:45 +0200 | [diff] [blame] | 213 | struct davinci_vc *davinci_vc = pdev->dev.platform_data; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 214 | struct davinci_vcif_dev *davinci_vcif_dev; |
| 215 | int ret; |
| 216 | |
Julia Lawall | a3bb8f3 | 2011-12-29 17:51:20 +0100 | [diff] [blame] | 217 | davinci_vcif_dev = devm_kzalloc(&pdev->dev, |
| 218 | sizeof(struct davinci_vcif_dev), |
| 219 | GFP_KERNEL); |
Kulikov Vasiliy | 55938b1 | 2010-07-16 20:16:17 +0400 | [diff] [blame] | 220 | if (!davinci_vcif_dev) { |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 221 | dev_dbg(&pdev->dev, |
| 222 | "could not allocate memory for private data\n"); |
| 223 | return -ENOMEM; |
| 224 | } |
| 225 | |
| 226 | /* DMA tx params */ |
| 227 | davinci_vcif_dev->davinci_vc = davinci_vc; |
| 228 | davinci_vcif_dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = |
| 229 | davinci_vc->davinci_vcif.dma_tx_channel; |
| 230 | davinci_vcif_dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr = |
| 231 | davinci_vc->davinci_vcif.dma_tx_addr; |
| 232 | |
| 233 | /* DMA rx params */ |
| 234 | davinci_vcif_dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = |
| 235 | davinci_vc->davinci_vcif.dma_rx_channel; |
| 236 | davinci_vcif_dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr = |
| 237 | davinci_vc->davinci_vcif.dma_rx_addr; |
| 238 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 239 | dev_set_drvdata(&pdev->dev, davinci_vcif_dev); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 240 | |
Kuninori Morimoto | ee226ce | 2013-03-21 03:31:07 -0700 | [diff] [blame] | 241 | ret = snd_soc_register_component(&pdev->dev, &davinci_vcif_component, |
| 242 | &davinci_vcif_dai, 1); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 243 | if (ret != 0) { |
| 244 | dev_err(&pdev->dev, "could not register dai\n"); |
Julia Lawall | a3bb8f3 | 2011-12-29 17:51:20 +0100 | [diff] [blame] | 245 | return ret; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 246 | } |
| 247 | |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 248 | ret = davinci_soc_platform_register(&pdev->dev); |
| 249 | if (ret) { |
| 250 | dev_err(&pdev->dev, "register PCM failed: %d\n", ret); |
Kuninori Morimoto | ee226ce | 2013-03-21 03:31:07 -0700 | [diff] [blame] | 251 | snd_soc_unregister_component(&pdev->dev); |
Hebbar, Gururaja | f08095a | 2012-08-27 18:56:39 +0530 | [diff] [blame] | 252 | return ret; |
| 253 | } |
| 254 | |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 255 | return 0; |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int davinci_vcif_remove(struct platform_device *pdev) |
| 259 | { |
Kuninori Morimoto | ee226ce | 2013-03-21 03:31:07 -0700 | [diff] [blame] | 260 | snd_soc_unregister_component(&pdev->dev); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static struct platform_driver davinci_vcif_driver = { |
| 266 | .probe = davinci_vcif_probe, |
| 267 | .remove = davinci_vcif_remove, |
| 268 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 269 | .name = "davinci-vcif", |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 270 | .owner = THIS_MODULE, |
| 271 | }, |
| 272 | }; |
| 273 | |
Axel Lin | f9b8a51 | 2011-11-25 10:09:27 +0800 | [diff] [blame] | 274 | module_platform_driver(davinci_vcif_driver); |
Miguel Aguilar | e155fcc | 2010-03-11 09:32:42 -0600 | [diff] [blame] | 275 | |
| 276 | MODULE_AUTHOR("Miguel Aguilar"); |
| 277 | MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC Voice Codec Interface"); |
| 278 | MODULE_LICENSE("GPL"); |