omap-twl4030.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * omap-twl4030.c -- SoC audio for TI SoC based boards with twl4030 codec
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
  5. * All rights reserved.
  6. *
  7. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  8. *
  9. * This driver replaces the following machine drivers:
  10. * omap3beagle (Author: Steve Sakoman <steve@sakoman.com>)
  11. * omap3evm (Author: Anuj Aggarwal <anuj.aggarwal@ti.com>)
  12. * overo (Author: Steve Sakoman <steve@sakoman.com>)
  13. * igep0020 (Author: Enric Balletbo i Serra <eballetbo@iseebcn.com>)
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  27. * 02110-1301 USA
  28. *
  29. */
  30. #include <linux/platform_device.h>
  31. #include <linux/platform_data/omap-twl4030.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/soc.h>
  37. #include "omap-mcbsp.h"
  38. #include "omap-pcm.h"
  39. static int omap_twl4030_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_dai *cpu_dai = rtd->cpu_dai;
  45. struct snd_soc_codec *codec = rtd->codec;
  46. struct snd_soc_card *card = codec->card;
  47. unsigned int fmt;
  48. int ret;
  49. switch (params_channels(params)) {
  50. case 2: /* Stereo I2S mode */
  51. fmt = SND_SOC_DAIFMT_I2S |
  52. SND_SOC_DAIFMT_NB_NF |
  53. SND_SOC_DAIFMT_CBM_CFM;
  54. break;
  55. case 4: /* Four channel TDM mode */
  56. fmt = SND_SOC_DAIFMT_DSP_A |
  57. SND_SOC_DAIFMT_IB_NF |
  58. SND_SOC_DAIFMT_CBM_CFM;
  59. break;
  60. default:
  61. return -EINVAL;
  62. }
  63. /* Set codec DAI configuration */
  64. ret = snd_soc_dai_set_fmt(codec_dai, fmt);
  65. if (ret < 0) {
  66. dev_err(card->dev, "can't set codec DAI configuration\n");
  67. return ret;
  68. }
  69. /* Set cpu DAI configuration */
  70. ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
  71. if (ret < 0) {
  72. dev_err(card->dev, "can't set cpu DAI configuration\n");
  73. return ret;
  74. }
  75. return 0;
  76. }
  77. static struct snd_soc_ops omap_twl4030_ops = {
  78. .hw_params = omap_twl4030_hw_params,
  79. };
  80. /* Digital audio interface glue - connects codec <--> CPU */
  81. static struct snd_soc_dai_link omap_twl4030_dai_links[] = {
  82. {
  83. .name = "TWL4030",
  84. .stream_name = "TWL4030",
  85. .cpu_dai_name = "omap-mcbsp.2",
  86. .codec_dai_name = "twl4030-hifi",
  87. .platform_name = "omap-pcm-audio",
  88. .codec_name = "twl4030-codec",
  89. .ops = &omap_twl4030_ops,
  90. },
  91. };
  92. /* Audio machine driver */
  93. static struct snd_soc_card omap_twl4030_card = {
  94. .owner = THIS_MODULE,
  95. .dai_link = omap_twl4030_dai_links,
  96. .num_links = ARRAY_SIZE(omap_twl4030_dai_links),
  97. };
  98. static int omap_twl4030_probe(struct platform_device *pdev)
  99. {
  100. struct omap_tw4030_pdata *pdata = dev_get_platdata(&pdev->dev);
  101. struct device_node *node = pdev->dev.of_node;
  102. struct snd_soc_card *card = &omap_twl4030_card;
  103. int ret = 0;
  104. card->dev = &pdev->dev;
  105. if (node) {
  106. struct device_node *dai_node;
  107. if (snd_soc_of_parse_card_name(card, "ti,model")) {
  108. dev_err(&pdev->dev, "Card name is not provided\n");
  109. return -ENODEV;
  110. }
  111. dai_node = of_parse_phandle(node, "ti,mcbsp", 0);
  112. if (!dai_node) {
  113. dev_err(&pdev->dev, "McBSP node is not provided\n");
  114. return -EINVAL;
  115. }
  116. omap_twl4030_dai_links[0].cpu_dai_name = NULL;
  117. omap_twl4030_dai_links[0].cpu_of_node = dai_node;
  118. } else if (pdata) {
  119. if (pdata->card_name) {
  120. card->name = pdata->card_name;
  121. } else {
  122. dev_err(&pdev->dev, "Card name is not provided\n");
  123. return -ENODEV;
  124. }
  125. } else {
  126. dev_err(&pdev->dev, "Missing pdata\n");
  127. return -ENODEV;
  128. }
  129. ret = snd_soc_register_card(card);
  130. if (ret) {
  131. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  132. ret);
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. static int omap_twl4030_remove(struct platform_device *pdev)
  138. {
  139. struct snd_soc_card *card = platform_get_drvdata(pdev);
  140. snd_soc_unregister_card(card);
  141. return 0;
  142. }
  143. static const struct of_device_id omap_twl4030_of_match[] = {
  144. {.compatible = "ti,omap-twl4030", },
  145. { },
  146. };
  147. MODULE_DEVICE_TABLE(of, omap_twl4030_of_match);
  148. static struct platform_driver omap_twl4030_driver = {
  149. .driver = {
  150. .name = "omap-twl4030",
  151. .owner = THIS_MODULE,
  152. .pm = &snd_soc_pm_ops,
  153. .of_match_table = omap_twl4030_of_match,
  154. },
  155. .probe = omap_twl4030_probe,
  156. .remove = omap_twl4030_remove,
  157. };
  158. module_platform_driver(omap_twl4030_driver);
  159. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  160. MODULE_DESCRIPTION("ALSA SoC for TI SoC based boards with twl4030 codec");
  161. MODULE_LICENSE("GPL");
  162. MODULE_ALIAS("platform:omap-twl4030");