bytcr_rt5651.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
  3. * (derived from bytcr_rt5640.c)
  4. *
  5. * Copyright (C) 2015 Intel Corp
  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/acpi.h>
  23. #include <linux/device.h>
  24. #include <linux/dmi.h>
  25. #include <linux/slab.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/jack.h>
  30. #include "../../codecs/rt5651.h"
  31. #include "../atom/sst-atom-controls.h"
  32. static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
  33. SND_SOC_DAPM_HP("Headphone", NULL),
  34. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  35. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  36. SND_SOC_DAPM_SPK("Speaker", NULL),
  37. };
  38. static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
  39. {"AIF1 Playback", NULL, "ssp2 Tx"},
  40. {"ssp2 Tx", NULL, "codec_out0"},
  41. {"ssp2 Tx", NULL, "codec_out1"},
  42. {"codec_in0", NULL, "ssp2 Rx"},
  43. {"codec_in1", NULL, "ssp2 Rx"},
  44. {"ssp2 Rx", NULL, "AIF1 Capture"},
  45. {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
  46. {"IN2P", NULL, "Headset Mic"},
  47. {"Headphone", NULL, "HPOL"},
  48. {"Headphone", NULL, "HPOR"},
  49. {"Speaker", NULL, "LOUTL"},
  50. {"Speaker", NULL, "LOUTR"},
  51. };
  52. static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic1_map[] = {
  53. {"DMIC1", NULL, "Internal Mic"},
  54. };
  55. static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic2_map[] = {
  56. {"DMIC2", NULL, "Internal Mic"},
  57. };
  58. static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
  59. {"Internal Mic", NULL, "micbias1"},
  60. {"IN1P", NULL, "Internal Mic"},
  61. };
  62. enum {
  63. BYT_RT5651_DMIC1_MAP,
  64. BYT_RT5651_DMIC2_MAP,
  65. BYT_RT5651_IN1_MAP,
  66. };
  67. #define BYT_RT5651_MAP(quirk) ((quirk) & 0xff)
  68. #define BYT_RT5651_DMIC_EN BIT(16)
  69. static unsigned long byt_rt5651_quirk = BYT_RT5651_DMIC1_MAP |
  70. BYT_RT5651_DMIC_EN;
  71. static const struct snd_kcontrol_new byt_rt5651_controls[] = {
  72. SOC_DAPM_PIN_SWITCH("Headphone"),
  73. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  74. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  75. SOC_DAPM_PIN_SWITCH("Speaker"),
  76. };
  77. static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
  78. struct snd_pcm_hw_params *params)
  79. {
  80. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  81. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  82. int ret;
  83. snd_soc_dai_set_bclk_ratio(codec_dai, 50);
  84. ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
  85. params_rate(params) * 512,
  86. SND_SOC_CLOCK_IN);
  87. if (ret < 0) {
  88. dev_err(rtd->dev, "can't set codec clock %d\n", ret);
  89. return ret;
  90. }
  91. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5651_PLL1_S_BCLK1,
  92. params_rate(params) * 50,
  93. params_rate(params) * 512);
  94. if (ret < 0) {
  95. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. static const struct dmi_system_id byt_rt5651_quirk_table[] = {
  101. {}
  102. };
  103. static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
  104. {
  105. int ret;
  106. struct snd_soc_card *card = runtime->card;
  107. const struct snd_soc_dapm_route *custom_map;
  108. int num_routes;
  109. card->dapm.idle_bias_off = true;
  110. dmi_check_system(byt_rt5651_quirk_table);
  111. switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
  112. case BYT_RT5651_IN1_MAP:
  113. custom_map = byt_rt5651_intmic_in1_map;
  114. num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
  115. break;
  116. case BYT_RT5651_DMIC2_MAP:
  117. custom_map = byt_rt5651_intmic_dmic2_map;
  118. num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic2_map);
  119. break;
  120. default:
  121. custom_map = byt_rt5651_intmic_dmic1_map;
  122. num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic1_map);
  123. }
  124. ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
  125. ARRAY_SIZE(byt_rt5651_controls));
  126. if (ret) {
  127. dev_err(card->dev, "unable to add card controls\n");
  128. return ret;
  129. }
  130. snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
  131. snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
  132. return ret;
  133. }
  134. static const struct snd_soc_pcm_stream byt_rt5651_dai_params = {
  135. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  136. .rate_min = 48000,
  137. .rate_max = 48000,
  138. .channels_min = 2,
  139. .channels_max = 2,
  140. };
  141. static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  142. struct snd_pcm_hw_params *params)
  143. {
  144. struct snd_interval *rate = hw_param_interval(params,
  145. SNDRV_PCM_HW_PARAM_RATE);
  146. struct snd_interval *channels = hw_param_interval(params,
  147. SNDRV_PCM_HW_PARAM_CHANNELS);
  148. int ret;
  149. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  150. rate->min = rate->max = 48000;
  151. channels->min = channels->max = 2;
  152. /* set SSP2 to 24-bit */
  153. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  154. /*
  155. * Default mode for SSP configuration is TDM 4 slot, override config
  156. * with explicit setting to I2S 2ch 24-bit. The word length is set with
  157. * dai_set_tdm_slot() since there is no other API exposed
  158. */
  159. ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
  160. SND_SOC_DAIFMT_I2S |
  161. SND_SOC_DAIFMT_NB_IF |
  162. SND_SOC_DAIFMT_CBS_CFS
  163. );
  164. if (ret < 0) {
  165. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  166. return ret;
  167. }
  168. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
  169. if (ret < 0) {
  170. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  171. return ret;
  172. }
  173. return 0;
  174. }
  175. static unsigned int rates_48000[] = {
  176. 48000,
  177. };
  178. static struct snd_pcm_hw_constraint_list constraints_48000 = {
  179. .count = ARRAY_SIZE(rates_48000),
  180. .list = rates_48000,
  181. };
  182. static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
  183. {
  184. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  185. SNDRV_PCM_HW_PARAM_RATE,
  186. &constraints_48000);
  187. }
  188. static struct snd_soc_ops byt_rt5651_aif1_ops = {
  189. .startup = byt_rt5651_aif1_startup,
  190. };
  191. static struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
  192. .hw_params = byt_rt5651_aif1_hw_params,
  193. };
  194. static struct snd_soc_dai_link byt_rt5651_dais[] = {
  195. [MERR_DPCM_AUDIO] = {
  196. .name = "Audio Port",
  197. .stream_name = "Audio",
  198. .cpu_dai_name = "media-cpu-dai",
  199. .codec_dai_name = "snd-soc-dummy-dai",
  200. .codec_name = "snd-soc-dummy",
  201. .platform_name = "sst-mfld-platform",
  202. .nonatomic = true,
  203. .dynamic = 1,
  204. .dpcm_playback = 1,
  205. .dpcm_capture = 1,
  206. .ops = &byt_rt5651_aif1_ops,
  207. },
  208. [MERR_DPCM_DEEP_BUFFER] = {
  209. .name = "Deep-Buffer Audio Port",
  210. .stream_name = "Deep-Buffer Audio",
  211. .cpu_dai_name = "deepbuffer-cpu-dai",
  212. .codec_dai_name = "snd-soc-dummy-dai",
  213. .codec_name = "snd-soc-dummy",
  214. .platform_name = "sst-mfld-platform",
  215. .nonatomic = true,
  216. .dynamic = 1,
  217. .dpcm_playback = 1,
  218. .ops = &byt_rt5651_aif1_ops,
  219. },
  220. [MERR_DPCM_COMPR] = {
  221. .name = "Compressed Port",
  222. .stream_name = "Compress",
  223. .cpu_dai_name = "compress-cpu-dai",
  224. .codec_dai_name = "snd-soc-dummy-dai",
  225. .codec_name = "snd-soc-dummy",
  226. .platform_name = "sst-mfld-platform",
  227. },
  228. /* CODEC<->CODEC link */
  229. /* back ends */
  230. {
  231. .name = "SSP2-Codec",
  232. .id = 1,
  233. .cpu_dai_name = "ssp2-port",
  234. .platform_name = "sst-mfld-platform",
  235. .no_pcm = 1,
  236. .codec_dai_name = "rt5651-aif1",
  237. .codec_name = "i2c-10EC5651:00",
  238. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  239. | SND_SOC_DAIFMT_CBS_CFS,
  240. .be_hw_params_fixup = byt_rt5651_codec_fixup,
  241. .ignore_suspend = 1,
  242. .nonatomic = true,
  243. .dpcm_playback = 1,
  244. .dpcm_capture = 1,
  245. .init = byt_rt5651_init,
  246. .ops = &byt_rt5651_be_ssp2_ops,
  247. },
  248. };
  249. /* SoC card */
  250. static struct snd_soc_card byt_rt5651_card = {
  251. .name = "bytcr-rt5651",
  252. .owner = THIS_MODULE,
  253. .dai_link = byt_rt5651_dais,
  254. .num_links = ARRAY_SIZE(byt_rt5651_dais),
  255. .dapm_widgets = byt_rt5651_widgets,
  256. .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
  257. .dapm_routes = byt_rt5651_audio_map,
  258. .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
  259. .fully_routed = true,
  260. };
  261. static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
  262. {
  263. int ret_val = 0;
  264. /* register the soc card */
  265. byt_rt5651_card.dev = &pdev->dev;
  266. ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5651_card);
  267. if (ret_val) {
  268. dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
  269. ret_val);
  270. return ret_val;
  271. }
  272. platform_set_drvdata(pdev, &byt_rt5651_card);
  273. return ret_val;
  274. }
  275. static struct platform_driver snd_byt_rt5651_mc_driver = {
  276. .driver = {
  277. .name = "bytcr_rt5651",
  278. },
  279. .probe = snd_byt_rt5651_mc_probe,
  280. };
  281. module_platform_driver(snd_byt_rt5651_mc_driver);
  282. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
  283. MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
  284. MODULE_LICENSE("GPL v2");
  285. MODULE_ALIAS("platform:bytcr_rt5651");