omap-abe-twl6040.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. struct abe_twl6040 {
  39. int jack_detection; /* board can detect jack events */
  40. int mclk_freq; /* MCLK frequency speed for twl6040 */
  41. };
  42. static int omap_abe_hw_params(struct snd_pcm_substream *substream,
  43. struct snd_pcm_hw_params *params)
  44. {
  45. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  46. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  47. struct snd_soc_codec *codec = rtd->codec;
  48. struct snd_soc_card *card = codec->card;
  49. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  50. int clk_id, freq;
  51. int ret;
  52. clk_id = twl6040_get_clk_id(rtd->codec);
  53. if (clk_id == TWL6040_SYSCLK_SEL_HPPLL)
  54. freq = priv->mclk_freq;
  55. else if (clk_id == TWL6040_SYSCLK_SEL_LPPLL)
  56. freq = 32768;
  57. else
  58. return -EINVAL;
  59. /* set the codec mclk */
  60. ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, freq,
  61. SND_SOC_CLOCK_IN);
  62. if (ret) {
  63. printk(KERN_ERR "can't set codec system clock\n");
  64. return ret;
  65. }
  66. return ret;
  67. }
  68. static struct snd_soc_ops omap_abe_ops = {
  69. .hw_params = omap_abe_hw_params,
  70. };
  71. static int omap_abe_dmic_hw_params(struct snd_pcm_substream *substream,
  72. struct snd_pcm_hw_params *params)
  73. {
  74. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  75. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  76. int ret = 0;
  77. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_SYSCLK_PAD_CLKS,
  78. 19200000, SND_SOC_CLOCK_IN);
  79. if (ret < 0) {
  80. printk(KERN_ERR "can't set DMIC cpu system clock\n");
  81. return ret;
  82. }
  83. ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_ABE_DMIC_CLK, 2400000,
  84. SND_SOC_CLOCK_OUT);
  85. if (ret < 0) {
  86. printk(KERN_ERR "can't set DMIC output clock\n");
  87. return ret;
  88. }
  89. return 0;
  90. }
  91. static struct snd_soc_ops omap_abe_dmic_ops = {
  92. .hw_params = omap_abe_dmic_hw_params,
  93. };
  94. /* Headset jack */
  95. static struct snd_soc_jack hs_jack;
  96. /*Headset jack detection DAPM pins */
  97. static struct snd_soc_jack_pin hs_jack_pins[] = {
  98. {
  99. .pin = "Headset Mic",
  100. .mask = SND_JACK_MICROPHONE,
  101. },
  102. {
  103. .pin = "Headset Stereophone",
  104. .mask = SND_JACK_HEADPHONE,
  105. },
  106. };
  107. /* SDP4430 machine DAPM */
  108. static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
  109. /* Outputs */
  110. SND_SOC_DAPM_HP("Headset Stereophone", NULL),
  111. SND_SOC_DAPM_SPK("Earphone Spk", NULL),
  112. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  113. SND_SOC_DAPM_LINE("Line Out", NULL),
  114. SND_SOC_DAPM_SPK("Vibrator", NULL),
  115. /* Inputs */
  116. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  117. SND_SOC_DAPM_MIC("Main Handset Mic", NULL),
  118. SND_SOC_DAPM_MIC("Sub Handset Mic", NULL),
  119. SND_SOC_DAPM_LINE("Line In", NULL),
  120. /* Digital microphones */
  121. SND_SOC_DAPM_MIC("Digital Mic", NULL),
  122. };
  123. static const struct snd_soc_dapm_route audio_map[] = {
  124. /* Routings for outputs */
  125. {"Headset Stereophone", NULL, "HSOL"},
  126. {"Headset Stereophone", NULL, "HSOR"},
  127. {"Earphone Spk", NULL, "EP"},
  128. {"Ext Spk", NULL, "HFL"},
  129. {"Ext Spk", NULL, "HFR"},
  130. {"Line Out", NULL, "AUXL"},
  131. {"Line Out", NULL, "AUXR"},
  132. {"Vibrator", NULL, "VIBRAL"},
  133. {"Vibrator", NULL, "VIBRAR"},
  134. /* Routings for inputs */
  135. {"HSMIC", NULL, "Headset Mic"},
  136. {"Headset Mic", NULL, "Headset Mic Bias"},
  137. {"MAINMIC", NULL, "Main Handset Mic"},
  138. {"Main Handset Mic", NULL, "Main Mic Bias"},
  139. {"SUBMIC", NULL, "Sub Handset Mic"},
  140. {"Sub Handset Mic", NULL, "Main Mic Bias"},
  141. {"AFML", NULL, "Line In"},
  142. {"AFMR", NULL, "Line In"},
  143. };
  144. static inline void twl6040_disconnect_pin(struct snd_soc_dapm_context *dapm,
  145. int connected, char *pin)
  146. {
  147. if (!connected)
  148. snd_soc_dapm_disable_pin(dapm, pin);
  149. }
  150. static int omap_abe_twl6040_init(struct snd_soc_pcm_runtime *rtd)
  151. {
  152. struct snd_soc_codec *codec = rtd->codec;
  153. struct snd_soc_card *card = codec->card;
  154. struct snd_soc_dapm_context *dapm = &codec->dapm;
  155. struct omap_abe_twl6040_data *pdata = dev_get_platdata(card->dev);
  156. struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
  157. int hs_trim;
  158. int ret = 0;
  159. /* Disable not connected paths if not used */
  160. twl6040_disconnect_pin(dapm, pdata->has_hs, "Headset Stereophone");
  161. twl6040_disconnect_pin(dapm, pdata->has_hf, "Ext Spk");
  162. twl6040_disconnect_pin(dapm, pdata->has_ep, "Earphone Spk");
  163. twl6040_disconnect_pin(dapm, pdata->has_aux, "Line Out");
  164. twl6040_disconnect_pin(dapm, pdata->has_vibra, "Vinrator");
  165. twl6040_disconnect_pin(dapm, pdata->has_hsmic, "Headset Mic");
  166. twl6040_disconnect_pin(dapm, pdata->has_mainmic, "Main Handset Mic");
  167. twl6040_disconnect_pin(dapm, pdata->has_submic, "Sub Handset Mic");
  168. twl6040_disconnect_pin(dapm, pdata->has_afm, "Line In");
  169. /*
  170. * Configure McPDM offset cancellation based on the HSOTRIM value from
  171. * twl6040.
  172. */
  173. hs_trim = twl6040_get_trim_value(codec, TWL6040_TRIM_HSOTRIM);
  174. omap_mcpdm_configure_dn_offsets(rtd, TWL6040_HSF_TRIM_LEFT(hs_trim),
  175. TWL6040_HSF_TRIM_RIGHT(hs_trim));
  176. /* Headset jack detection only if it is supported */
  177. if (priv->jack_detection) {
  178. ret = snd_soc_jack_new(codec, "Headset Jack",
  179. SND_JACK_HEADSET, &hs_jack);
  180. if (ret)
  181. return ret;
  182. ret = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pins),
  183. hs_jack_pins);
  184. twl6040_hs_jack_detect(codec, &hs_jack, SND_JACK_HEADSET);
  185. }
  186. return ret;
  187. }
  188. static const struct snd_soc_dapm_route dmic_audio_map[] = {
  189. {"DMic", NULL, "Digital Mic"},
  190. {"Digital Mic", NULL, "Digital Mic1 Bias"},
  191. };
  192. static int omap_abe_dmic_init(struct snd_soc_pcm_runtime *rtd)
  193. {
  194. struct snd_soc_codec *codec = rtd->codec;
  195. struct snd_soc_dapm_context *dapm = &codec->dapm;
  196. return snd_soc_dapm_add_routes(dapm, dmic_audio_map,
  197. ARRAY_SIZE(dmic_audio_map));
  198. }
  199. /* Digital audio interface glue - connects codec <--> CPU */
  200. static struct snd_soc_dai_link abe_twl6040_dai_links[] = {
  201. {
  202. .name = "TWL6040",
  203. .stream_name = "TWL6040",
  204. .cpu_dai_name = "omap-mcpdm",
  205. .codec_dai_name = "twl6040-legacy",
  206. .platform_name = "omap-pcm-audio",
  207. .codec_name = "twl6040-codec",
  208. .init = omap_abe_twl6040_init,
  209. .ops = &omap_abe_ops,
  210. },
  211. {
  212. .name = "DMIC",
  213. .stream_name = "DMIC Capture",
  214. .cpu_dai_name = "omap-dmic",
  215. .codec_dai_name = "dmic-hifi",
  216. .platform_name = "omap-pcm-audio",
  217. .codec_name = "dmic-codec",
  218. .init = omap_abe_dmic_init,
  219. .ops = &omap_abe_dmic_ops,
  220. },
  221. };
  222. /* Audio machine driver */
  223. static struct snd_soc_card omap_abe_card = {
  224. .owner = THIS_MODULE,
  225. .dapm_widgets = twl6040_dapm_widgets,
  226. .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
  227. .dapm_routes = audio_map,
  228. .num_dapm_routes = ARRAY_SIZE(audio_map),
  229. };
  230. static __devinit int omap_abe_probe(struct platform_device *pdev)
  231. {
  232. struct omap_abe_twl6040_data *pdata = dev_get_platdata(&pdev->dev);
  233. struct snd_soc_card *card = &omap_abe_card;
  234. struct abe_twl6040 *priv;
  235. int num_links = 0;
  236. int ret;
  237. card->dev = &pdev->dev;
  238. if (!pdata) {
  239. dev_err(&pdev->dev, "Missing pdata\n");
  240. return -ENODEV;
  241. }
  242. priv = devm_kzalloc(&pdev->dev, sizeof(struct abe_twl6040), GFP_KERNEL);
  243. if (priv == NULL)
  244. return -ENOMEM;
  245. if (pdata->card_name) {
  246. card->name = pdata->card_name;
  247. } else {
  248. dev_err(&pdev->dev, "Card name is not provided\n");
  249. return -ENODEV;
  250. }
  251. priv->jack_detection = pdata->jack_detection;
  252. priv->mclk_freq = pdata->mclk_freq;
  253. if (!priv->mclk_freq) {
  254. dev_err(&pdev->dev, "MCLK frequency missing\n");
  255. return -ENODEV;
  256. }
  257. if (pdata->has_dmic)
  258. num_links = 2;
  259. else
  260. num_links = 1;
  261. card->dai_link = abe_twl6040_dai_links;
  262. card->num_links = num_links;
  263. snd_soc_card_set_drvdata(card, priv);
  264. ret = snd_soc_register_card(card);
  265. if (ret)
  266. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  267. ret);
  268. return ret;
  269. }
  270. static int __devexit omap_abe_remove(struct platform_device *pdev)
  271. {
  272. struct snd_soc_card *card = platform_get_drvdata(pdev);
  273. snd_soc_unregister_card(card);
  274. return 0;
  275. }
  276. static struct platform_driver omap_abe_driver = {
  277. .driver = {
  278. .name = "omap-abe-twl6040",
  279. .owner = THIS_MODULE,
  280. .pm = &snd_soc_pm_ops,
  281. },
  282. .probe = omap_abe_probe,
  283. .remove = __devexit_p(omap_abe_remove),
  284. };
  285. module_platform_driver(omap_abe_driver);
  286. MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
  287. MODULE_DESCRIPTION("ALSA SoC for OMAP boards with ABE and twl6040 codec");
  288. MODULE_LICENSE("GPL");
  289. MODULE_ALIAS("platform:omap-abe-twl6040");