omap-abe-twl6040.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * omap-abe-twl6040.c -- SoC audio for TI OMAP based boards with ABE and
  3. * twl6040 codec
  4. *
  5. * Author: Misael Lopez Cruz <misael.lopez@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mfd/twl6040.h>
  25. #include <linux/platform_data/omap-abe-twl6040.h>
  26. #include <linux/module.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/soc.h>
  30. #include <sound/jack.h>
  31. #include <asm/mach-types.h>
  32. #include <plat/hardware.h>
  33. #include <plat/mux.h>
  34. #include "omap-dmic.h"
  35. #include "omap-mcpdm.h"
  36. #include "omap-pcm.h"
  37. #include "../codecs/twl6040.h"
  38. static int omap_abe_hw_params(struct snd_pcm_substream *substream,
  39. struct snd_pcm_hw_params *params)
  40. {
  41. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  42. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  43. int clk_id, freq;
  44. int ret;
  45. clk_id = twl6040_get_clk_id(rtd->codec);
  46. if (clk_id == TWL6040_SYSCLK_SEL_HPPLL)
  47. freq = 38400000;
  48. else if (clk_id == TWL6040_SYSCLK_SEL_LPPLL)
  49. freq = 32768;
  50. else
  51. return -EINVAL;
  52. /* set the codec mclk */
  53. ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, freq,
  54. SND_SOC_CLOCK_IN);
  55. if (ret) {
  56. printk(KERN_ERR "can't set codec system clock\n");
  57. return ret;
  58. }
  59. return ret;
  60. }
  61. static struct snd_soc_ops omap_abe_ops = {
  62. .hw_params = omap_abe_hw_params,
  63. };
  64. static int omap_abe_dmic_hw_params(struct snd_pcm_substream *substream,
  65. struct snd_pcm_hw_params *params)
  66. {
  67. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  68. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  69. int ret = 0;
  70. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_SYSCLK_PAD_CLKS,
  71. 19200000, SND_SOC_CLOCK_IN);
  72. if (ret < 0) {
  73. printk(KERN_ERR "can't set DMIC cpu system clock\n");
  74. return ret;
  75. }
  76. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_ABE_DMIC_CLK, 2400000,
  77. SND_SOC_CLOCK_OUT);
  78. if (ret < 0) {
  79. printk(KERN_ERR "can't set DMIC output clock\n");
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. static struct snd_soc_ops omap_abe_dmic_ops = {
  85. .hw_params = omap_abe_dmic_hw_params,
  86. };
  87. /* Headset jack */
  88. static struct snd_soc_jack hs_jack;
  89. /*Headset jack detection DAPM pins */
  90. static struct snd_soc_jack_pin hs_jack_pins[] = {
  91. {
  92. .pin = "Headset Mic",
  93. .mask = SND_JACK_MICROPHONE,
  94. },
  95. {
  96. .pin = "Headset Stereophone",
  97. .mask = SND_JACK_HEADPHONE,
  98. },
  99. };
  100. /* SDP4430 machine DAPM */
  101. static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
  102. SND_SOC_DAPM_MIC("Ext Mic", NULL),
  103. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  104. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  105. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  106. SND_SOC_DAPM_SPK("Earphone Spk", NULL),
  107. SND_SOC_DAPM_INPUT("FM Stereo In"),
  108. };
  109. static const struct snd_soc_dapm_route audio_map[] = {
  110. /* External Mics: MAINMIC, SUBMIC with bias*/
  111. {"MAINMIC", NULL, "Main Mic Bias"},
  112. {"SUBMIC", NULL, "Main Mic Bias"},
  113. {"Main Mic Bias", NULL, "Ext Mic"},
  114. /* External Speakers: HFL, HFR */
  115. {"Ext Spk", NULL, "HFL"},
  116. {"Ext Spk", NULL, "HFR"},
  117. /* Headset Mic: HSMIC with bias */
  118. {"HSMIC", NULL, "Headset Mic Bias"},
  119. {"Headset Mic Bias", NULL, "Headset Mic"},
  120. /* Headset Stereophone (Headphone): HSOL, HSOR */
  121. {"Headset Stereophone", NULL, "HSOL"},
  122. {"Headset Stereophone", NULL, "HSOR"},
  123. /* Earphone speaker */
  124. {"Earphone Spk", NULL, "EP"},
  125. /* Aux/FM Stereo In: AFML, AFMR */
  126. {"AFML", NULL, "FM Stereo In"},
  127. {"AFMR", NULL, "FM Stereo In"},
  128. };
  129. static int omap_abe_twl6040_init(struct snd_soc_pcm_runtime *rtd)
  130. {
  131. struct snd_soc_codec *codec = rtd->codec;
  132. int ret, hs_trim;
  133. /*
  134. * Configure McPDM offset cancellation based on the HSOTRIM value from
  135. * twl6040.
  136. */
  137. hs_trim = twl6040_get_trim_value(codec, TWL6040_TRIM_HSOTRIM);
  138. omap_mcpdm_configure_dn_offsets(rtd, TWL6040_HSF_TRIM_LEFT(hs_trim),
  139. TWL6040_HSF_TRIM_RIGHT(hs_trim));
  140. /* Headset jack detection */
  141. ret = snd_soc_jack_new(codec, "Headset Jack",
  142. SND_JACK_HEADSET, &hs_jack);
  143. if (ret)
  144. return ret;
  145. ret = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pins),
  146. hs_jack_pins);
  147. if (machine_is_omap_4430sdp())
  148. twl6040_hs_jack_detect(codec, &hs_jack, SND_JACK_HEADSET);
  149. else
  150. snd_soc_jack_report(&hs_jack, SND_JACK_HEADSET, SND_JACK_HEADSET);
  151. return ret;
  152. }
  153. static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
  154. SND_SOC_DAPM_MIC("Digital Mic", NULL),
  155. };
  156. static const struct snd_soc_dapm_route dmic_audio_map[] = {
  157. {"DMic", NULL, "Digital Mic1 Bias"},
  158. {"Digital Mic1 Bias", NULL, "Digital Mic"},
  159. };
  160. static int omap_abe_dmic_init(struct snd_soc_pcm_runtime *rtd)
  161. {
  162. struct snd_soc_codec *codec = rtd->codec;
  163. struct snd_soc_dapm_context *dapm = &codec->dapm;
  164. int ret;
  165. ret = snd_soc_dapm_new_controls(dapm, dmic_dapm_widgets,
  166. ARRAY_SIZE(dmic_dapm_widgets));
  167. if (ret)
  168. return ret;
  169. return snd_soc_dapm_add_routes(dapm, dmic_audio_map,
  170. ARRAY_SIZE(dmic_audio_map));
  171. }
  172. /* Digital audio interface glue - connects codec <--> CPU */
  173. static struct snd_soc_dai_link sdp4430_dai[] = {
  174. {
  175. .name = "TWL6040",
  176. .stream_name = "TWL6040",
  177. .cpu_dai_name = "omap-mcpdm",
  178. .codec_dai_name = "twl6040-legacy",
  179. .platform_name = "omap-pcm-audio",
  180. .codec_name = "twl6040-codec",
  181. .init = omap_abe_twl6040_init,
  182. .ops = &omap_abe_ops,
  183. },
  184. {
  185. .name = "DMIC",
  186. .stream_name = "DMIC Capture",
  187. .cpu_dai_name = "omap-dmic",
  188. .codec_dai_name = "dmic-hifi",
  189. .platform_name = "omap-pcm-audio",
  190. .codec_name = "dmic-codec",
  191. .init = omap_abe_dmic_init,
  192. .ops = &omap_abe_dmic_ops,
  193. },
  194. };
  195. /* Audio machine driver */
  196. static struct snd_soc_card omap_abe_card = {
  197. .owner = THIS_MODULE,
  198. .dai_link = sdp4430_dai,
  199. .num_links = ARRAY_SIZE(sdp4430_dai),
  200. .dapm_widgets = twl6040_dapm_widgets,
  201. .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
  202. .dapm_routes = audio_map,
  203. .num_dapm_routes = ARRAY_SIZE(audio_map),
  204. };
  205. static __devinit int omap_abe_probe(struct platform_device *pdev)
  206. {
  207. struct omap_abe_twl6040_data *pdata = dev_get_platdata(&pdev->dev);
  208. struct snd_soc_card *card = &omap_abe_card;
  209. int ret;
  210. card->dev = &pdev->dev;
  211. if (!pdata) {
  212. dev_err(&pdev->dev, "Missing pdata\n");
  213. return -ENODEV;
  214. }
  215. if (pdata->card_name) {
  216. card->name = pdata->card_name;
  217. } else {
  218. dev_err(&pdev->dev, "Card name is not provided\n");
  219. return -ENODEV;
  220. }
  221. ret = snd_soc_register_card(card);
  222. if (ret)
  223. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  224. ret);
  225. return ret;
  226. }
  227. static int __devexit omap_abe_remove(struct platform_device *pdev)
  228. {
  229. struct snd_soc_card *card = platform_get_drvdata(pdev);
  230. snd_soc_unregister_card(card);
  231. return 0;
  232. }
  233. static struct platform_driver omap_abe_driver = {
  234. .driver = {
  235. .name = "omap-abe-twl6040",
  236. .owner = THIS_MODULE,
  237. .pm = &snd_soc_pm_ops,
  238. },
  239. .probe = omap_abe_probe,
  240. .remove = __devexit_p(omap_abe_remove),
  241. };
  242. module_platform_driver(omap_abe_driver);
  243. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
  244. MODULE_DESCRIPTION("ALSA SoC for OMAP boards with ABE and twl6040 codec");
  245. MODULE_LICENSE("GPL");
  246. MODULE_ALIAS("platform:omap-abe-twl6040");