1/* 2 * ALSA SoC CQ0093 Voice Codec Driver for DaVinci platforms 3 * 4 * Copyright (C) 2010 Texas Instruments, Inc 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#include <linux/module.h> 23#include <linux/moduleparam.h> 24#include <linux/init.h> 25#include <linux/io.h> 26#include <linux/delay.h> 27#include <linux/pm.h> 28#include <linux/platform_device.h> 29#include <linux/device.h> 30#include <linux/slab.h> 31#include <linux/clk.h> 32#include <linux/mfd/davinci_voicecodec.h> 33 34#include <sound/core.h> 35#include <sound/pcm.h> 36#include <sound/pcm_params.h> 37#include <sound/soc.h> 38#include <sound/soc-dai.h> 39#include <sound/soc-dapm.h> 40#include <sound/initval.h> 41 42#include <mach/dm365.h> 43 44#include "cq93vc.h" 45 46static inline unsigned int cq93vc_read(struct snd_soc_codec *codec, 47 unsigned int reg) 48{ 49 struct davinci_vc *davinci_vc = codec->control_data; 50 51 return readl(davinci_vc->base + reg); 52} 53 54static inline int cq93vc_write(struct snd_soc_codec *codec, unsigned int reg, 55 unsigned int value) 56{ 57 struct davinci_vc *davinci_vc = codec->control_data; 58 59 writel(value, davinci_vc->base + reg); 60 61 return 0; 62} 63 64static const struct snd_kcontrol_new cq93vc_snd_controls[] = { 65 SOC_SINGLE("PGA Capture Volume", DAVINCI_VC_REG05, 0, 0x03, 0), 66 SOC_SINGLE("Mono DAC Playback Volume", DAVINCI_VC_REG09, 0, 0x3f, 0), 67}; 68 69static int cq93vc_mute(struct snd_soc_dai *dai, int mute) 70{ 71 struct snd_soc_codec *codec = dai->codec; 72 u8 reg = cq93vc_read(codec, DAVINCI_VC_REG09) & ~DAVINCI_VC_REG09_MUTE; 73 74 if (mute) 75 cq93vc_write(codec, DAVINCI_VC_REG09, 76 reg | DAVINCI_VC_REG09_MUTE); 77 else 78 cq93vc_write(codec, DAVINCI_VC_REG09, reg); 79 80 return 0; 81} 82 83static int cq93vc_set_dai_sysclk(struct snd_soc_dai *codec_dai, 84 int clk_id, unsigned int freq, int dir) 85{ 86 struct snd_soc_codec *codec = codec_dai->codec; 87 struct davinci_vc *davinci_vc = codec->control_data; 88 89 switch (freq) { 90 case 22579200: 91 case 27000000: 92 case 33868800: 93 davinci_vc->cq93vc.sysclk = freq; 94 return 0; 95 } 96 97 return -EINVAL; 98} 99 100static int cq93vc_set_bias_level(struct snd_soc_codec *codec, 101 enum snd_soc_bias_level level) 102{ 103 switch (level) { 104 case SND_SOC_BIAS_ON: 105 cq93vc_write(codec, DAVINCI_VC_REG12, 106 DAVINCI_VC_REG12_POWER_ALL_ON); 107 break; 108 case SND_SOC_BIAS_PREPARE: 109 break; 110 case SND_SOC_BIAS_STANDBY: 111 cq93vc_write(codec, DAVINCI_VC_REG12, 112 DAVINCI_VC_REG12_POWER_ALL_OFF); 113 break; 114 case SND_SOC_BIAS_OFF: 115 /* force all power off */ 116 cq93vc_write(codec, DAVINCI_VC_REG12, 117 DAVINCI_VC_REG12_POWER_ALL_OFF); 118 break; 119 } 120 codec->bias_level = level; 121 122 return 0; 123} 124 125#define CQ93VC_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000) 126#define CQ93VC_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE) 127 128static struct snd_soc_dai_ops cq93vc_dai_ops = { 129 .digital_mute = cq93vc_mute, 130 .set_sysclk = cq93vc_set_dai_sysclk, 131}; 132 133struct snd_soc_dai cq93vc_dai = { 134 .name = "CQ93VC", 135 .playback = { 136 .stream_name = "Playback", 137 .channels_min = 1, 138 .channels_max = 2, 139 .rates = CQ93VC_RATES, 140 .formats = CQ93VC_FORMATS,}, 141 .capture = { 142 .stream_name = "Capture", 143 .channels_min = 1, 144 .channels_max = 2, 145 .rates = CQ93VC_RATES, 146 .formats = CQ93VC_FORMATS,}, 147 .ops = &cq93vc_dai_ops, 148}; 149EXPORT_SYMBOL_GPL(cq93vc_dai); 150 151static int cq93vc_resume(struct platform_device *pdev) 152{ 153 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 154 struct snd_soc_codec *codec = socdev->card->codec; 155 156 cq93vc_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 157 158 return 0; 159} 160 161static struct snd_soc_codec *cq93vc_codec; 162 163static int cq93vc_probe(struct platform_device *pdev) 164{ 165 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 166 struct device *dev = &pdev->dev; 167 struct snd_soc_codec *codec; 168 int ret; 169 170 socdev->card->codec = cq93vc_codec; 171 codec = socdev->card->codec; 172 173 /* Register pcms */ 174 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 175 if (ret < 0) { 176 dev_err(dev, "%s: failed to create pcms\n", pdev->name); 177 return ret; 178 } 179 180 /* Set controls */ 181 snd_soc_add_controls(codec, cq93vc_snd_controls, 182 ARRAY_SIZE(cq93vc_snd_controls)); 183 184 /* Off, with power on */ 185 cq93vc_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 186 187 return 0; 188} 189 190static int cq93vc_remove(struct platform_device *pdev) 191{ 192 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 193 194 snd_soc_free_pcms(socdev); 195 snd_soc_dapm_free(socdev); 196 197 return 0; 198} 199 200struct snd_soc_codec_device soc_codec_dev_cq93vc = { 201 .probe = cq93vc_probe, 202 .remove = cq93vc_remove, 203 .resume = cq93vc_resume, 204}; 205EXPORT_SYMBOL_GPL(soc_codec_dev_cq93vc); 206 207static __init int cq93vc_codec_probe(struct platform_device *pdev) 208{ 209 struct davinci_vc *davinci_vc = platform_get_drvdata(pdev); 210 struct snd_soc_codec *codec; 211 int ret; 212 213 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 214 if (codec == NULL) { 215 dev_dbg(davinci_vc->dev, 216 "could not allocate memory for codec data\n"); 217 return -ENOMEM; 218 } 219 220 davinci_vc->cq93vc.codec = codec; 221 222 cq93vc_dai.dev = &pdev->dev; 223 224 mutex_init(&codec->mutex); 225 INIT_LIST_HEAD(&codec->dapm_widgets); 226 INIT_LIST_HEAD(&codec->dapm_paths); 227 codec->dev = &pdev->dev; 228 codec->name = "CQ93VC"; 229 codec->owner = THIS_MODULE; 230 codec->read = cq93vc_read; 231 codec->write = cq93vc_write; 232 codec->set_bias_level = cq93vc_set_bias_level; 233 codec->dai = &cq93vc_dai; 234 codec->num_dai = 1; 235 codec->control_data = davinci_vc; 236 237 cq93vc_codec = codec; 238 239 ret = snd_soc_register_codec(codec); 240 if (ret) { 241 dev_err(davinci_vc->dev, "failed to register codec\n"); 242 goto fail1; 243 } 244 245 ret = snd_soc_register_dai(&cq93vc_dai); 246 if (ret) { 247 dev_err(davinci_vc->dev, "could register dai\n"); 248 goto fail2; 249 } 250 return 0; 251 252fail2: 253 snd_soc_unregister_codec(codec); 254 255fail1: 256 kfree(codec); 257 cq93vc_codec = NULL; 258 259 return ret; 260} 261 262static int __devexit cq93vc_codec_remove(struct platform_device *pdev) 263{ 264 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 265 struct snd_soc_codec *codec = socdev->card->codec; 266 267 snd_soc_unregister_dai(&cq93vc_dai); 268 snd_soc_unregister_codec(&codec); 269 270 kfree(codec); 271 cq93vc_codec = NULL; 272 273 return 0; 274} 275 276static struct platform_driver cq93vc_codec_driver = { 277 .driver = { 278 .name = "cq93vc", 279 .owner = THIS_MODULE, 280 }, 281 .probe = cq93vc_codec_probe, 282 .remove = __devexit_p(cq93vc_codec_remove), 283}; 284 285static __init int cq93vc_init(void) 286{ 287 return platform_driver_probe(&cq93vc_codec_driver, cq93vc_codec_probe); 288} 289module_init(cq93vc_init); 290 291static __exit void cq93vc_exit(void) 292{ 293 platform_driver_unregister(&cq93vc_codec_driver); 294} 295module_exit(cq93vc_exit); 296 297MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC CQ0093 Voice Codec Driver"); 298MODULE_AUTHOR("Miguel Aguilar"); 299MODULE_LICENSE("GPL"); 300