cq93vc.c 5.3 KB

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