cq93vc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <linux/spi/spi.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include <sound/initval.h>
  39. #include <mach/dm365.h>
  40. static inline unsigned int cq93vc_read(struct snd_soc_codec *codec,
  41. unsigned int reg)
  42. {
  43. struct davinci_vc *davinci_vc = codec->control_data;
  44. return readl(davinci_vc->base + reg);
  45. }
  46. static inline int cq93vc_write(struct snd_soc_codec *codec, unsigned int reg,
  47. unsigned int value)
  48. {
  49. struct davinci_vc *davinci_vc = codec->control_data;
  50. writel(value, davinci_vc->base + reg);
  51. return 0;
  52. }
  53. static const struct snd_kcontrol_new cq93vc_snd_controls[] = {
  54. SOC_SINGLE("PGA Capture Volume", DAVINCI_VC_REG05, 0, 0x03, 0),
  55. SOC_SINGLE("Mono DAC Playback Volume", DAVINCI_VC_REG09, 0, 0x3f, 0),
  56. };
  57. static int cq93vc_mute(struct snd_soc_dai *dai, int mute)
  58. {
  59. struct snd_soc_codec *codec = dai->codec;
  60. u8 reg = cq93vc_read(codec, DAVINCI_VC_REG09) & ~DAVINCI_VC_REG09_MUTE;
  61. if (mute)
  62. cq93vc_write(codec, DAVINCI_VC_REG09,
  63. reg | DAVINCI_VC_REG09_MUTE);
  64. else
  65. cq93vc_write(codec, DAVINCI_VC_REG09, reg);
  66. return 0;
  67. }
  68. static int cq93vc_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  69. int clk_id, unsigned int freq, int dir)
  70. {
  71. struct snd_soc_codec *codec = codec_dai->codec;
  72. struct davinci_vc *davinci_vc = codec->control_data;
  73. switch (freq) {
  74. case 22579200:
  75. case 27000000:
  76. case 33868800:
  77. davinci_vc->cq93vc.sysclk = freq;
  78. return 0;
  79. }
  80. return -EINVAL;
  81. }
  82. static int cq93vc_set_bias_level(struct snd_soc_codec *codec,
  83. enum snd_soc_bias_level level)
  84. {
  85. switch (level) {
  86. case SND_SOC_BIAS_ON:
  87. cq93vc_write(codec, DAVINCI_VC_REG12,
  88. DAVINCI_VC_REG12_POWER_ALL_ON);
  89. break;
  90. case SND_SOC_BIAS_PREPARE:
  91. break;
  92. case SND_SOC_BIAS_STANDBY:
  93. cq93vc_write(codec, DAVINCI_VC_REG12,
  94. DAVINCI_VC_REG12_POWER_ALL_OFF);
  95. break;
  96. case SND_SOC_BIAS_OFF:
  97. /* force all power off */
  98. cq93vc_write(codec, DAVINCI_VC_REG12,
  99. DAVINCI_VC_REG12_POWER_ALL_OFF);
  100. break;
  101. }
  102. codec->dapm.bias_level = level;
  103. return 0;
  104. }
  105. #define CQ93VC_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000)
  106. #define CQ93VC_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE)
  107. static struct snd_soc_dai_ops cq93vc_dai_ops = {
  108. .digital_mute = cq93vc_mute,
  109. .set_sysclk = cq93vc_set_dai_sysclk,
  110. };
  111. static struct snd_soc_dai_driver cq93vc_dai = {
  112. .name = "cq93vc-hifi",
  113. .playback = {
  114. .stream_name = "Playback",
  115. .channels_min = 1,
  116. .channels_max = 2,
  117. .rates = CQ93VC_RATES,
  118. .formats = CQ93VC_FORMATS,},
  119. .capture = {
  120. .stream_name = "Capture",
  121. .channels_min = 1,
  122. .channels_max = 2,
  123. .rates = CQ93VC_RATES,
  124. .formats = CQ93VC_FORMATS,},
  125. .ops = &cq93vc_dai_ops,
  126. };
  127. static int cq93vc_resume(struct snd_soc_codec *codec)
  128. {
  129. cq93vc_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  130. return 0;
  131. }
  132. static int cq93vc_probe(struct snd_soc_codec *codec)
  133. {
  134. struct davinci_vc *davinci_vc = codec->dev->platform_data;
  135. davinci_vc->cq93vc.codec = codec;
  136. codec->control_data = davinci_vc;
  137. /* Set controls */
  138. snd_soc_add_controls(codec, cq93vc_snd_controls,
  139. ARRAY_SIZE(cq93vc_snd_controls));
  140. /* Off, with power on */
  141. cq93vc_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  142. return 0;
  143. }
  144. static int cq93vc_remove(struct snd_soc_codec *codec)
  145. {
  146. cq93vc_set_bias_level(codec, SND_SOC_BIAS_OFF);
  147. return 0;
  148. }
  149. static struct snd_soc_codec_driver soc_codec_dev_cq93vc = {
  150. .read = cq93vc_read,
  151. .write = cq93vc_write,
  152. .set_bias_level = cq93vc_set_bias_level,
  153. .probe = cq93vc_probe,
  154. .remove = cq93vc_remove,
  155. .resume = cq93vc_resume,
  156. };
  157. static int cq93vc_platform_probe(struct platform_device *pdev)
  158. {
  159. return snd_soc_register_codec(&pdev->dev,
  160. &soc_codec_dev_cq93vc, &cq93vc_dai, 1);
  161. }
  162. static int cq93vc_platform_remove(struct platform_device *pdev)
  163. {
  164. snd_soc_unregister_codec(&pdev->dev);
  165. return 0;
  166. }
  167. static struct platform_driver cq93vc_codec_driver = {
  168. .driver = {
  169. .name = "cq93vc-codec",
  170. .owner = THIS_MODULE,
  171. },
  172. .probe = cq93vc_platform_probe,
  173. .remove = __devexit_p(cq93vc_platform_remove),
  174. };
  175. static int __init cq93vc_init(void)
  176. {
  177. return platform_driver_register(&cq93vc_codec_driver);
  178. }
  179. module_init(cq93vc_init);
  180. static void __exit cq93vc_exit(void)
  181. {
  182. platform_driver_unregister(&cq93vc_codec_driver);
  183. }
  184. module_exit(cq93vc_exit);
  185. MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC CQ0093 Voice Codec Driver");
  186. MODULE_AUTHOR("Miguel Aguilar");
  187. MODULE_LICENSE("GPL");