bytcr_rt5640.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
  3. *
  4. * Copyright (C) 2014 Intel Corp
  5. * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  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; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/device.h>
  23. #include <linux/slab.h>
  24. #include <linux/input.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include "../../codecs/rt5640.h"
  29. #include "../atom/sst-atom-controls.h"
  30. static const struct snd_soc_dapm_widget byt_dapm_widgets[] = {
  31. SND_SOC_DAPM_HP("Headphone", NULL),
  32. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  33. SND_SOC_DAPM_MIC("Int Mic", NULL),
  34. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  35. };
  36. static const struct snd_soc_dapm_route byt_audio_map[] = {
  37. {"IN2P", NULL, "Headset Mic"},
  38. {"IN2N", NULL, "Headset Mic"},
  39. {"Headset Mic", NULL, "MICBIAS1"},
  40. {"IN1P", NULL, "MICBIAS1"},
  41. {"LDO2", NULL, "Int Mic"},
  42. {"Headphone", NULL, "HPOL"},
  43. {"Headphone", NULL, "HPOR"},
  44. {"Ext Spk", NULL, "SPOLP"},
  45. {"Ext Spk", NULL, "SPOLN"},
  46. {"Ext Spk", NULL, "SPORP"},
  47. {"Ext Spk", NULL, "SPORN"},
  48. {"AIF1 Playback", NULL, "ssp2 Tx"},
  49. {"ssp2 Tx", NULL, "codec_out0"},
  50. {"ssp2 Tx", NULL, "codec_out1"},
  51. {"codec_in0", NULL, "ssp2 Rx"},
  52. {"codec_in1", NULL, "ssp2 Rx"},
  53. {"ssp2 Rx", NULL, "AIF1 Capture"},
  54. };
  55. static const struct snd_kcontrol_new byt_mc_controls[] = {
  56. SOC_DAPM_PIN_SWITCH("Headphone"),
  57. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  58. SOC_DAPM_PIN_SWITCH("Int Mic"),
  59. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  60. };
  61. static int byt_aif1_hw_params(struct snd_pcm_substream *substream,
  62. struct snd_pcm_hw_params *params)
  63. {
  64. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  65. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  66. int ret;
  67. snd_soc_dai_set_bclk_ratio(codec_dai, 50);
  68. ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
  69. params_rate(params) * 512,
  70. SND_SOC_CLOCK_IN);
  71. if (ret < 0) {
  72. dev_err(rtd->dev, "can't set codec clock %d\n", ret);
  73. return ret;
  74. }
  75. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_BCLK1,
  76. params_rate(params) * 50,
  77. params_rate(params) * 512);
  78. if (ret < 0) {
  79. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. static const struct snd_soc_pcm_stream byt_dai_params = {
  85. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  86. .rate_min = 48000,
  87. .rate_max = 48000,
  88. .channels_min = 2,
  89. .channels_max = 2,
  90. };
  91. static int byt_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  92. struct snd_pcm_hw_params *params)
  93. {
  94. struct snd_interval *rate = hw_param_interval(params,
  95. SNDRV_PCM_HW_PARAM_RATE);
  96. struct snd_interval *channels = hw_param_interval(params,
  97. SNDRV_PCM_HW_PARAM_CHANNELS);
  98. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  99. rate->min = rate->max = 48000;
  100. channels->min = channels->max = 2;
  101. /* set SSP2 to 24-bit */
  102. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  103. return 0;
  104. }
  105. static int byt_aif1_startup(struct snd_pcm_substream *substream)
  106. {
  107. return snd_pcm_hw_constraint_single(substream->runtime,
  108. SNDRV_PCM_HW_PARAM_RATE, 48000);
  109. }
  110. static struct snd_soc_ops byt_aif1_ops = {
  111. .startup = byt_aif1_startup,
  112. };
  113. static struct snd_soc_ops byt_be_ssp2_ops = {
  114. .hw_params = byt_aif1_hw_params,
  115. };
  116. static struct snd_soc_dai_link byt_dailink[] = {
  117. [MERR_DPCM_AUDIO] = {
  118. .name = "Baytrail Audio Port",
  119. .stream_name = "Baytrail Audio",
  120. .cpu_dai_name = "media-cpu-dai",
  121. .codec_dai_name = "snd-soc-dummy-dai",
  122. .codec_name = "snd-soc-dummy",
  123. .platform_name = "sst-mfld-platform",
  124. .nonatomic = true,
  125. .dynamic = 1,
  126. .dpcm_playback = 1,
  127. .dpcm_capture = 1,
  128. .ops = &byt_aif1_ops,
  129. },
  130. [MERR_DPCM_COMPR] = {
  131. .name = "Baytrail Compressed Port",
  132. .stream_name = "Baytrail Compress",
  133. .cpu_dai_name = "compress-cpu-dai",
  134. .codec_dai_name = "snd-soc-dummy-dai",
  135. .codec_name = "snd-soc-dummy",
  136. .platform_name = "sst-mfld-platform",
  137. },
  138. /* back ends */
  139. {
  140. .name = "SSP2-Codec",
  141. .be_id = 1,
  142. .cpu_dai_name = "ssp2-port",
  143. .platform_name = "sst-mfld-platform",
  144. .no_pcm = 1,
  145. .codec_dai_name = "rt5640-aif1",
  146. .codec_name = "i2c-10EC5640:00",
  147. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  148. | SND_SOC_DAIFMT_CBS_CFS,
  149. .be_hw_params_fixup = byt_codec_fixup,
  150. .ignore_suspend = 1,
  151. .nonatomic = true,
  152. .dpcm_playback = 1,
  153. .dpcm_capture = 1,
  154. .ops = &byt_be_ssp2_ops,
  155. },
  156. };
  157. /* SoC card */
  158. static struct snd_soc_card snd_soc_card_byt = {
  159. .name = "baytrailcraudio",
  160. .owner = THIS_MODULE,
  161. .dai_link = byt_dailink,
  162. .num_links = ARRAY_SIZE(byt_dailink),
  163. .dapm_widgets = byt_dapm_widgets,
  164. .num_dapm_widgets = ARRAY_SIZE(byt_dapm_widgets),
  165. .dapm_routes = byt_audio_map,
  166. .num_dapm_routes = ARRAY_SIZE(byt_audio_map),
  167. .controls = byt_mc_controls,
  168. .num_controls = ARRAY_SIZE(byt_mc_controls),
  169. };
  170. static int snd_byt_mc_probe(struct platform_device *pdev)
  171. {
  172. int ret_val = 0;
  173. /* register the soc card */
  174. snd_soc_card_byt.dev = &pdev->dev;
  175. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_byt);
  176. if (ret_val) {
  177. dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n", ret_val);
  178. return ret_val;
  179. }
  180. platform_set_drvdata(pdev, &snd_soc_card_byt);
  181. return ret_val;
  182. }
  183. static struct platform_driver snd_byt_mc_driver = {
  184. .driver = {
  185. .name = "bytt100_rt5640",
  186. .pm = &snd_soc_pm_ops,
  187. },
  188. .probe = snd_byt_mc_probe,
  189. };
  190. module_platform_driver(snd_byt_mc_driver);
  191. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
  192. MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
  193. MODULE_LICENSE("GPL v2");
  194. MODULE_ALIAS("platform:bytt100_rt5640");