omap-abe-twl6040.c 9.0 KB

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