bytcr_rt5640.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 unsigned int rates_48000[] = {
  106. 48000,
  107. };
  108. static struct snd_pcm_hw_constraint_list constraints_48000 = {
  109. .count = ARRAY_SIZE(rates_48000),
  110. .list = rates_48000,
  111. };
  112. static int byt_aif1_startup(struct snd_pcm_substream *substream)
  113. {
  114. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  115. SNDRV_PCM_HW_PARAM_RATE,
  116. &constraints_48000);
  117. }
  118. static struct snd_soc_ops byt_aif1_ops = {
  119. .startup = byt_aif1_startup,
  120. };
  121. static struct snd_soc_ops byt_be_ssp2_ops = {
  122. .hw_params = byt_aif1_hw_params,
  123. };
  124. static struct snd_soc_dai_link byt_dailink[] = {
  125. [MERR_DPCM_AUDIO] = {
  126. .name = "Baytrail Audio Port",
  127. .stream_name = "Baytrail Audio",
  128. .cpu_dai_name = "media-cpu-dai",
  129. .codec_dai_name = "snd-soc-dummy-dai",
  130. .codec_name = "snd-soc-dummy",
  131. .platform_name = "sst-mfld-platform",
  132. .ignore_suspend = 1,
  133. .dynamic = 1,
  134. .dpcm_playback = 1,
  135. .dpcm_capture = 1,
  136. .ops = &byt_aif1_ops,
  137. },
  138. [MERR_DPCM_COMPR] = {
  139. .name = "Baytrail Compressed Port",
  140. .stream_name = "Baytrail Compress",
  141. .cpu_dai_name = "compress-cpu-dai",
  142. .codec_dai_name = "snd-soc-dummy-dai",
  143. .codec_name = "snd-soc-dummy",
  144. .platform_name = "sst-mfld-platform",
  145. },
  146. /* back ends */
  147. {
  148. .name = "SSP2-Codec",
  149. .be_id = 1,
  150. .cpu_dai_name = "ssp2-port",
  151. .platform_name = "sst-mfld-platform",
  152. .no_pcm = 1,
  153. .codec_dai_name = "rt5640-aif1",
  154. .codec_name = "i2c-10EC5640:00",
  155. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  156. | SND_SOC_DAIFMT_CBS_CFS,
  157. .be_hw_params_fixup = byt_codec_fixup,
  158. .ignore_suspend = 1,
  159. .dpcm_playback = 1,
  160. .dpcm_capture = 1,
  161. .ops = &byt_be_ssp2_ops,
  162. },
  163. };
  164. /* SoC card */
  165. static struct snd_soc_card snd_soc_card_byt = {
  166. .name = "baytrailcraudio",
  167. .owner = THIS_MODULE,
  168. .dai_link = byt_dailink,
  169. .num_links = ARRAY_SIZE(byt_dailink),
  170. .dapm_widgets = byt_dapm_widgets,
  171. .num_dapm_widgets = ARRAY_SIZE(byt_dapm_widgets),
  172. .dapm_routes = byt_audio_map,
  173. .num_dapm_routes = ARRAY_SIZE(byt_audio_map),
  174. .controls = byt_mc_controls,
  175. .num_controls = ARRAY_SIZE(byt_mc_controls),
  176. };
  177. static int snd_byt_mc_probe(struct platform_device *pdev)
  178. {
  179. int ret_val = 0;
  180. /* register the soc card */
  181. snd_soc_card_byt.dev = &pdev->dev;
  182. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_byt);
  183. if (ret_val) {
  184. dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n", ret_val);
  185. return ret_val;
  186. }
  187. platform_set_drvdata(pdev, &snd_soc_card_byt);
  188. return ret_val;
  189. }
  190. static struct platform_driver snd_byt_mc_driver = {
  191. .driver = {
  192. .name = "bytt100_rt5640",
  193. .pm = &snd_soc_pm_ops,
  194. },
  195. .probe = snd_byt_mc_probe,
  196. };
  197. module_platform_driver(snd_byt_mc_driver);
  198. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
  199. MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
  200. MODULE_LICENSE("GPL v2");
  201. MODULE_ALIAS("platform:bytt100_rt5640");