cq93vc.c 7.2 KB

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