bdw-rt5650.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ASoC machine driver for Intel Broadwell platforms with RT5650 codec
  4. *
  5. * Copyright 2019, The Chromium OS Authors. All rights reserved.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <sound/core.h>
  12. #include <sound/jack.h>
  13. #include <sound/pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include <sound/soc.h>
  16. #include <sound/soc-acpi.h>
  17. #include "../../codecs/rt5645.h"
  18. struct bdw_rt5650_priv {
  19. struct gpio_desc *gpio_hp_en;
  20. struct snd_soc_component *component;
  21. };
  22. static const struct snd_soc_dapm_widget bdw_rt5650_widgets[] = {
  23. SND_SOC_DAPM_HP("Headphone", NULL),
  24. SND_SOC_DAPM_SPK("Speaker", NULL),
  25. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  26. SND_SOC_DAPM_MIC("DMIC Pair1", NULL),
  27. SND_SOC_DAPM_MIC("DMIC Pair2", NULL),
  28. };
  29. static const struct snd_soc_dapm_route bdw_rt5650_map[] = {
  30. /* Speakers */
  31. {"Speaker", NULL, "SPOL"},
  32. {"Speaker", NULL, "SPOR"},
  33. /* Headset jack connectors */
  34. {"Headphone", NULL, "HPOL"},
  35. {"Headphone", NULL, "HPOR"},
  36. {"IN1P", NULL, "Headset Mic"},
  37. {"IN1N", NULL, "Headset Mic"},
  38. /* Digital MICs
  39. * DMIC Pair1 are the two DMICs connected on the DMICN1 connector.
  40. * DMIC Pair2 are the two DMICs connected on the DMICN2 connector.
  41. * Facing the camera, DMIC Pair1 are on the left side, DMIC Pair2
  42. * are on the right side.
  43. */
  44. {"DMIC L1", NULL, "DMIC Pair1"},
  45. {"DMIC R1", NULL, "DMIC Pair1"},
  46. {"DMIC L2", NULL, "DMIC Pair2"},
  47. {"DMIC R2", NULL, "DMIC Pair2"},
  48. /* CODEC BE connections */
  49. {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
  50. {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
  51. };
  52. static const struct snd_kcontrol_new bdw_rt5650_controls[] = {
  53. SOC_DAPM_PIN_SWITCH("Speaker"),
  54. SOC_DAPM_PIN_SWITCH("Headphone"),
  55. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  56. SOC_DAPM_PIN_SWITCH("DMIC Pair1"),
  57. SOC_DAPM_PIN_SWITCH("DMIC Pair2"),
  58. };
  59. static struct snd_soc_jack headphone_jack;
  60. static struct snd_soc_jack mic_jack;
  61. static struct snd_soc_jack_pin headphone_jack_pin = {
  62. .pin = "Headphone",
  63. .mask = SND_JACK_HEADPHONE,
  64. };
  65. static struct snd_soc_jack_pin mic_jack_pin = {
  66. .pin = "Headset Mic",
  67. .mask = SND_JACK_MICROPHONE,
  68. };
  69. static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
  70. struct snd_pcm_hw_params *params)
  71. {
  72. struct snd_interval *rate = hw_param_interval(params,
  73. SNDRV_PCM_HW_PARAM_RATE);
  74. struct snd_interval *chan = hw_param_interval(params,
  75. SNDRV_PCM_HW_PARAM_CHANNELS);
  76. /* The ADSP will covert the FE rate to 48k, max 4-channels */
  77. rate->min = rate->max = 48000;
  78. chan->min = 2;
  79. chan->max = 4;
  80. /* set SSP0 to 24 bit */
  81. snd_mask_set_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
  82. SNDRV_PCM_FORMAT_S24_LE);
  83. return 0;
  84. }
  85. static int bdw_rt5650_hw_params(struct snd_pcm_substream *substream,
  86. struct snd_pcm_hw_params *params)
  87. {
  88. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  89. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  90. int ret;
  91. /* Workaround: set codec PLL to 19.2MHz that PLL source is
  92. * from MCLK(24MHz) to conform 2.4MHz DMIC clock.
  93. */
  94. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
  95. 24000000, 19200000);
  96. if (ret < 0) {
  97. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  98. return ret;
  99. }
  100. /* The actual MCLK freq is 24MHz. The codec is told that MCLK is
  101. * 24.576MHz to satisfy the requirement of rl6231_get_clk_info.
  102. * ASRC is enabled on AD and DA filters to ensure good audio quality.
  103. */
  104. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1, 24576000,
  105. SND_SOC_CLOCK_IN);
  106. if (ret < 0) {
  107. dev_err(rtd->dev, "can't set codec sysclk configuration\n");
  108. return ret;
  109. }
  110. return ret;
  111. }
  112. static struct snd_soc_ops bdw_rt5650_ops = {
  113. .hw_params = bdw_rt5650_hw_params,
  114. };
  115. static const unsigned int channels[] = {
  116. 2, 4,
  117. };
  118. static const struct snd_pcm_hw_constraint_list constraints_channels = {
  119. .count = ARRAY_SIZE(channels),
  120. .list = channels,
  121. .mask = 0,
  122. };
  123. static int bdw_rt5650_fe_startup(struct snd_pcm_substream *substream)
  124. {
  125. struct snd_pcm_runtime *runtime = substream->runtime;
  126. /* Board supports stereo and quad configurations for capture */
  127. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  128. return 0;
  129. runtime->hw.channels_max = 4;
  130. return snd_pcm_hw_constraint_list(runtime, 0,
  131. SNDRV_PCM_HW_PARAM_CHANNELS,
  132. &constraints_channels);
  133. }
  134. static const struct snd_soc_ops bdw_rt5650_fe_ops = {
  135. .startup = bdw_rt5650_fe_startup,
  136. };
  137. static int bdw_rt5650_init(struct snd_soc_pcm_runtime *rtd)
  138. {
  139. struct bdw_rt5650_priv *bdw_rt5650 =
  140. snd_soc_card_get_drvdata(rtd->card);
  141. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  142. struct snd_soc_component *component = codec_dai->component;
  143. int ret;
  144. /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
  145. * The ASRC clock source is clk_i2s1_asrc.
  146. */
  147. rt5645_sel_asrc_clk_src(component,
  148. RT5645_DA_STEREO_FILTER |
  149. RT5645_DA_MONO_L_FILTER |
  150. RT5645_DA_MONO_R_FILTER |
  151. RT5645_AD_STEREO_FILTER |
  152. RT5645_AD_MONO_L_FILTER |
  153. RT5645_AD_MONO_R_FILTER,
  154. RT5645_CLK_SEL_I2S1_ASRC);
  155. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  156. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
  157. if (ret < 0) {
  158. dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret);
  159. return ret;
  160. }
  161. /* Create and initialize headphone jack */
  162. if (snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
  163. SND_JACK_HEADPHONE, &headphone_jack,
  164. &headphone_jack_pin, 1)) {
  165. dev_err(component->dev, "Can't create headphone jack\n");
  166. }
  167. /* Create and initialize mic jack */
  168. if (snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
  169. SND_JACK_MICROPHONE, &mic_jack, &mic_jack_pin, 1)) {
  170. dev_err(component->dev, "Can't create mic jack\n");
  171. }
  172. rt5645_set_jack_detect(component, &headphone_jack, &mic_jack, NULL);
  173. bdw_rt5650->component = component;
  174. return 0;
  175. }
  176. /* broadwell digital audio interface glue - connects codec <--> CPU */
  177. SND_SOC_DAILINK_DEF(dummy,
  178. DAILINK_COMP_ARRAY(COMP_DUMMY()));
  179. SND_SOC_DAILINK_DEF(fe,
  180. DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
  181. SND_SOC_DAILINK_DEF(platform,
  182. DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
  183. SND_SOC_DAILINK_DEF(be,
  184. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5650:00", "rt5645-aif1")));
  185. SND_SOC_DAILINK_DEF(ssp0_port,
  186. DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
  187. static struct snd_soc_dai_link bdw_rt5650_dais[] = {
  188. /* Front End DAI links */
  189. {
  190. .name = "System PCM",
  191. .stream_name = "System Playback",
  192. .nonatomic = 1,
  193. .dynamic = 1,
  194. .ops = &bdw_rt5650_fe_ops,
  195. .trigger = {
  196. SND_SOC_DPCM_TRIGGER_POST,
  197. SND_SOC_DPCM_TRIGGER_POST
  198. },
  199. .dpcm_playback = 1,
  200. .dpcm_capture = 1,
  201. SND_SOC_DAILINK_REG(fe, dummy, platform),
  202. },
  203. /* Back End DAI links */
  204. {
  205. /* SSP0 - Codec */
  206. .name = "Codec",
  207. .id = 0,
  208. .nonatomic = 1,
  209. .no_pcm = 1,
  210. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF |
  211. SND_SOC_DAIFMT_CBC_CFC,
  212. .ignore_pmdown_time = 1,
  213. .be_hw_params_fixup = broadwell_ssp0_fixup,
  214. .ops = &bdw_rt5650_ops,
  215. .dpcm_playback = 1,
  216. .dpcm_capture = 1,
  217. .init = bdw_rt5650_init,
  218. SND_SOC_DAILINK_REG(ssp0_port, be, platform),
  219. },
  220. };
  221. /* use space before codec name to simplify card ID, and simplify driver name */
  222. #define SOF_CARD_NAME "bdw rt5650" /* card name will be 'sof-bdw rt5650' */
  223. #define SOF_DRIVER_NAME "SOF"
  224. #define CARD_NAME "bdw-rt5650"
  225. #define DRIVER_NAME NULL /* card name will be used for driver name */
  226. /* ASoC machine driver for Broadwell DSP + RT5650 */
  227. static struct snd_soc_card bdw_rt5650_card = {
  228. .name = CARD_NAME,
  229. .driver_name = DRIVER_NAME,
  230. .owner = THIS_MODULE,
  231. .dai_link = bdw_rt5650_dais,
  232. .num_links = ARRAY_SIZE(bdw_rt5650_dais),
  233. .dapm_widgets = bdw_rt5650_widgets,
  234. .num_dapm_widgets = ARRAY_SIZE(bdw_rt5650_widgets),
  235. .dapm_routes = bdw_rt5650_map,
  236. .num_dapm_routes = ARRAY_SIZE(bdw_rt5650_map),
  237. .controls = bdw_rt5650_controls,
  238. .num_controls = ARRAY_SIZE(bdw_rt5650_controls),
  239. .fully_routed = true,
  240. };
  241. static int bdw_rt5650_probe(struct platform_device *pdev)
  242. {
  243. struct bdw_rt5650_priv *bdw_rt5650;
  244. struct snd_soc_acpi_mach *mach;
  245. int ret;
  246. bdw_rt5650_card.dev = &pdev->dev;
  247. /* Allocate driver private struct */
  248. bdw_rt5650 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5650_priv),
  249. GFP_KERNEL);
  250. if (!bdw_rt5650)
  251. return -ENOMEM;
  252. /* override platform name, if required */
  253. mach = pdev->dev.platform_data;
  254. ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5650_card,
  255. mach->mach_params.platform);
  256. if (ret)
  257. return ret;
  258. /* set card and driver name */
  259. if (snd_soc_acpi_sof_parent(&pdev->dev)) {
  260. bdw_rt5650_card.name = SOF_CARD_NAME;
  261. bdw_rt5650_card.driver_name = SOF_DRIVER_NAME;
  262. } else {
  263. bdw_rt5650_card.name = CARD_NAME;
  264. bdw_rt5650_card.driver_name = DRIVER_NAME;
  265. }
  266. snd_soc_card_set_drvdata(&bdw_rt5650_card, bdw_rt5650);
  267. return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5650_card);
  268. }
  269. static struct platform_driver bdw_rt5650_audio = {
  270. .probe = bdw_rt5650_probe,
  271. .driver = {
  272. .name = "bdw-rt5650",
  273. .pm = &snd_soc_pm_ops,
  274. },
  275. };
  276. module_platform_driver(bdw_rt5650_audio)
  277. /* Module information */
  278. MODULE_AUTHOR("Ben Zhang <[email protected]>");
  279. MODULE_DESCRIPTION("Intel Broadwell RT5650 machine driver");
  280. MODULE_LICENSE("GPL v2");
  281. MODULE_ALIAS("platform:bdw-rt5650");