cq93vc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/soc-dai.h>
  39. #include <sound/soc-dapm.h>
  40. #include <sound/initval.h>
  41. #include <mach/dm365.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. static struct snd_soc_dai_driver cq93vc_dai = {
  114. .name = "cq93vc-hifi",
  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. static int cq93vc_resume(struct snd_soc_codec *codec)
  130. {
  131. cq93vc_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  132. return 0;
  133. }
  134. static int cq93vc_probe(struct snd_soc_codec *codec)
  135. {
  136. struct davinci_vc *davinci_vc = codec->dev->platform_data;
  137. davinci_vc->cq93vc.codec = codec;
  138. codec->control_data = davinci_vc;
  139. /* Set controls */
  140. snd_soc_add_controls(codec, cq93vc_snd_controls,
  141. ARRAY_SIZE(cq93vc_snd_controls));
  142. /* Off, with power on */
  143. cq93vc_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  144. return 0;
  145. }
  146. static int cq93vc_remove(struct snd_soc_codec *codec)
  147. {
  148. cq93vc_set_bias_level(codec, SND_SOC_BIAS_OFF);
  149. return 0;
  150. }
  151. static struct snd_soc_codec_driver soc_codec_dev_cq93vc = {
  152. .read = cq93vc_read,
  153. .write = cq93vc_write,
  154. .set_bias_level = cq93vc_set_bias_level,
  155. .probe = cq93vc_probe,
  156. .remove = cq93vc_remove,
  157. .resume = cq93vc_resume,
  158. };
  159. static int cq93vc_platform_probe(struct platform_device *pdev)
  160. {
  161. return snd_soc_register_codec(&pdev->dev,
  162. &soc_codec_dev_cq93vc, &cq93vc_dai, 1);
  163. }
  164. static int cq93vc_platform_remove(struct platform_device *pdev)
  165. {
  166. snd_soc_unregister_codec(&pdev->dev);
  167. return 0;
  168. }
  169. static struct platform_driver cq93vc_codec_driver = {
  170. .driver = {
  171. .name = "cq93vc-codec",
  172. .owner = THIS_MODULE,
  173. },
  174. .probe = cq93vc_platform_probe,
  175. .remove = __devexit_p(cq93vc_platform_remove),
  176. };
  177. static int __init cq93vc_init(void)
  178. {
  179. return platform_driver_register(&cq93vc_codec_driver);
  180. }
  181. module_init(cq93vc_init);
  182. static void __exit cq93vc_exit(void)
  183. {
  184. platform_driver_unregister(&cq93vc_codec_driver);
  185. }
  186. module_exit(cq93vc_exit);
  187. MODULE_DESCRIPTION("Texas Instruments DaVinci ASoC CQ0093 Voice Codec Driver");
  188. MODULE_AUTHOR("Miguel Aguilar");
  189. MODULE_LICENSE("GPL");