bdw-rt5677.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ASoC machine driver for Intel Broadwell platforms with RT5677 codec
  4. *
  5. * Copyright (c) 2014, The Chromium OS Authors. All rights reserved.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/delay.h>
  12. #include <sound/core.h>
  13. #include <sound/pcm.h>
  14. #include <sound/soc.h>
  15. #include <sound/pcm_params.h>
  16. #include <sound/jack.h>
  17. #include <sound/soc-acpi.h>
  18. #include "../../codecs/rt5677.h"
  19. struct bdw_rt5677_priv {
  20. struct gpio_desc *gpio_hp_en;
  21. struct snd_soc_component *component;
  22. };
  23. static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
  24. struct snd_kcontrol *k, int event)
  25. {
  26. struct snd_soc_dapm_context *dapm = w->dapm;
  27. struct snd_soc_card *card = dapm->card;
  28. struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
  29. if (SND_SOC_DAPM_EVENT_ON(event))
  30. msleep(70);
  31. gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
  32. SND_SOC_DAPM_EVENT_ON(event));
  33. return 0;
  34. }
  35. static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = {
  36. SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp),
  37. SND_SOC_DAPM_SPK("Speaker", NULL),
  38. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  39. SND_SOC_DAPM_MIC("Local DMICs", NULL),
  40. SND_SOC_DAPM_MIC("Remote DMICs", NULL),
  41. };
  42. static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
  43. /* Speakers */
  44. {"Speaker", NULL, "PDM1L"},
  45. {"Speaker", NULL, "PDM1R"},
  46. /* Headset jack connectors */
  47. {"Headphone", NULL, "LOUT1"},
  48. {"Headphone", NULL, "LOUT2"},
  49. {"IN1P", NULL, "Headset Mic"},
  50. {"IN1N", NULL, "Headset Mic"},
  51. /* Digital MICs
  52. * Local DMICs: the two DMICs on the mainboard
  53. * Remote DMICs: the two DMICs on the camera module
  54. */
  55. {"DMIC L1", NULL, "Remote DMICs"},
  56. {"DMIC R1", NULL, "Remote DMICs"},
  57. {"DMIC L2", NULL, "Local DMICs"},
  58. {"DMIC R2", NULL, "Local DMICs"},
  59. /* CODEC BE connections */
  60. {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
  61. {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
  62. {"DSP Capture", NULL, "DSP Buffer"},
  63. /* DSP Clock Connections */
  64. { "DSP Buffer", NULL, "SSP0 CODEC IN" },
  65. { "SSP0 CODEC IN", NULL, "DSPTX" },
  66. };
  67. static const struct snd_kcontrol_new bdw_rt5677_controls[] = {
  68. SOC_DAPM_PIN_SWITCH("Speaker"),
  69. SOC_DAPM_PIN_SWITCH("Headphone"),
  70. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  71. SOC_DAPM_PIN_SWITCH("Local DMICs"),
  72. SOC_DAPM_PIN_SWITCH("Remote DMICs"),
  73. };
  74. static struct snd_soc_jack headphone_jack;
  75. static struct snd_soc_jack mic_jack;
  76. static struct snd_soc_jack_pin headphone_jack_pin = {
  77. .pin = "Headphone",
  78. .mask = SND_JACK_HEADPHONE,
  79. };
  80. static struct snd_soc_jack_pin mic_jack_pin = {
  81. .pin = "Headset Mic",
  82. .mask = SND_JACK_MICROPHONE,
  83. };
  84. static struct snd_soc_jack_gpio headphone_jack_gpio = {
  85. .name = "plug-det",
  86. .report = SND_JACK_HEADPHONE,
  87. .debounce_time = 200,
  88. };
  89. static struct snd_soc_jack_gpio mic_jack_gpio = {
  90. .name = "mic-present",
  91. .report = SND_JACK_MICROPHONE,
  92. .debounce_time = 200,
  93. .invert = 1,
  94. };
  95. /* GPIO indexes defined by ACPI */
  96. enum {
  97. RT5677_GPIO_PLUG_DET = 0,
  98. RT5677_GPIO_MIC_PRESENT_L = 1,
  99. RT5677_GPIO_HOTWORD_DET_L = 2,
  100. RT5677_GPIO_DSP_INT = 3,
  101. RT5677_GPIO_HP_AMP_SHDN_L = 4,
  102. };
  103. static const struct acpi_gpio_params plug_det_gpio = { RT5677_GPIO_PLUG_DET, 0, false };
  104. static const struct acpi_gpio_params mic_present_gpio = { RT5677_GPIO_MIC_PRESENT_L, 0, false };
  105. static const struct acpi_gpio_params headphone_enable_gpio = { RT5677_GPIO_HP_AMP_SHDN_L, 0, false };
  106. static const struct acpi_gpio_mapping bdw_rt5677_gpios[] = {
  107. { "plug-det-gpios", &plug_det_gpio, 1 },
  108. { "mic-present-gpios", &mic_present_gpio, 1 },
  109. { "headphone-enable-gpios", &headphone_enable_gpio, 1 },
  110. { NULL },
  111. };
  112. static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
  113. struct snd_pcm_hw_params *params)
  114. {
  115. struct snd_interval *rate = hw_param_interval(params,
  116. SNDRV_PCM_HW_PARAM_RATE);
  117. struct snd_interval *chan = hw_param_interval(params,
  118. SNDRV_PCM_HW_PARAM_CHANNELS);
  119. /* The ADSP will covert the FE rate to 48k, stereo */
  120. rate->min = rate->max = 48000;
  121. chan->min = chan->max = 2;
  122. /* set SSP0 to 16 bit */
  123. params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
  124. return 0;
  125. }
  126. static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
  127. struct snd_pcm_hw_params *params)
  128. {
  129. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  130. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  131. int ret;
  132. ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000,
  133. SND_SOC_CLOCK_IN);
  134. if (ret < 0) {
  135. dev_err(rtd->dev, "can't set codec sysclk configuration\n");
  136. return ret;
  137. }
  138. return ret;
  139. }
  140. static int bdw_rt5677_dsp_hw_params(struct snd_pcm_substream *substream,
  141. struct snd_pcm_hw_params *params)
  142. {
  143. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  144. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  145. int ret;
  146. ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_PLL1, 24576000,
  147. SND_SOC_CLOCK_IN);
  148. if (ret < 0) {
  149. dev_err(rtd->dev, "can't set codec sysclk configuration\n");
  150. return ret;
  151. }
  152. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5677_PLL1_S_MCLK,
  153. 24000000, 24576000);
  154. if (ret < 0) {
  155. dev_err(rtd->dev, "can't set codec pll configuration\n");
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. static const struct snd_soc_ops bdw_rt5677_ops = {
  161. .hw_params = bdw_rt5677_hw_params,
  162. };
  163. static const struct snd_soc_ops bdw_rt5677_dsp_ops = {
  164. .hw_params = bdw_rt5677_dsp_hw_params,
  165. };
  166. static const unsigned int channels[] = {
  167. 2,
  168. };
  169. static const struct snd_pcm_hw_constraint_list constraints_channels = {
  170. .count = ARRAY_SIZE(channels),
  171. .list = channels,
  172. .mask = 0,
  173. };
  174. static int bdw_rt5677_fe_startup(struct snd_pcm_substream *substream)
  175. {
  176. struct snd_pcm_runtime *runtime = substream->runtime;
  177. /* Board supports stereo configuration only */
  178. runtime->hw.channels_max = 2;
  179. return snd_pcm_hw_constraint_list(runtime, 0,
  180. SNDRV_PCM_HW_PARAM_CHANNELS,
  181. &constraints_channels);
  182. }
  183. static const struct snd_soc_ops bdw_rt5677_fe_ops = {
  184. .startup = bdw_rt5677_fe_startup,
  185. };
  186. static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
  187. {
  188. struct bdw_rt5677_priv *bdw_rt5677 =
  189. snd_soc_card_get_drvdata(rtd->card);
  190. struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
  191. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  192. int ret;
  193. ret = devm_acpi_dev_add_driver_gpios(component->dev, bdw_rt5677_gpios);
  194. if (ret)
  195. dev_warn(component->dev, "Failed to add driver gpios\n");
  196. /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
  197. * The ASRC clock source is clk_i2s1_asrc.
  198. */
  199. rt5677_sel_asrc_clk_src(component, RT5677_DA_STEREO_FILTER |
  200. RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
  201. RT5677_CLK_SEL_I2S1_ASRC);
  202. /* Enable codec ASRC function for Mono ADC L.
  203. * The ASRC clock source is clk_sys2_asrc.
  204. */
  205. rt5677_sel_asrc_clk_src(component, RT5677_AD_MONO_L_FILTER,
  206. RT5677_CLK_SEL_SYS2);
  207. /* Request rt5677 GPIO for headphone amp control */
  208. bdw_rt5677->gpio_hp_en = gpiod_get(component->dev, "headphone-enable",
  209. GPIOD_OUT_LOW);
  210. if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
  211. dev_err(component->dev, "Can't find HP_AMP_SHDN_L gpio\n");
  212. return PTR_ERR(bdw_rt5677->gpio_hp_en);
  213. }
  214. /* Create and initialize headphone jack */
  215. if (!snd_soc_card_jack_new_pins(rtd->card, "Headphone Jack",
  216. SND_JACK_HEADPHONE, &headphone_jack,
  217. &headphone_jack_pin, 1)) {
  218. headphone_jack_gpio.gpiod_dev = component->dev;
  219. if (snd_soc_jack_add_gpios(&headphone_jack, 1,
  220. &headphone_jack_gpio))
  221. dev_err(component->dev, "Can't add headphone jack gpio\n");
  222. } else {
  223. dev_err(component->dev, "Can't create headphone jack\n");
  224. }
  225. /* Create and initialize mic jack */
  226. if (!snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
  227. SND_JACK_MICROPHONE, &mic_jack,
  228. &mic_jack_pin, 1)) {
  229. mic_jack_gpio.gpiod_dev = component->dev;
  230. if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
  231. dev_err(component->dev, "Can't add mic jack gpio\n");
  232. } else {
  233. dev_err(component->dev, "Can't create mic jack\n");
  234. }
  235. bdw_rt5677->component = component;
  236. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
  237. return 0;
  238. }
  239. static void bdw_rt5677_exit(struct snd_soc_pcm_runtime *rtd)
  240. {
  241. struct bdw_rt5677_priv *bdw_rt5677 =
  242. snd_soc_card_get_drvdata(rtd->card);
  243. /*
  244. * The .exit() can be reached without going through the .init()
  245. * so explicitly test if the gpiod is valid
  246. */
  247. if (!IS_ERR_OR_NULL(bdw_rt5677->gpio_hp_en))
  248. gpiod_put(bdw_rt5677->gpio_hp_en);
  249. }
  250. /* broadwell digital audio interface glue - connects codec <--> CPU */
  251. SND_SOC_DAILINK_DEF(dummy,
  252. DAILINK_COMP_ARRAY(COMP_DUMMY()));
  253. SND_SOC_DAILINK_DEF(fe,
  254. DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
  255. SND_SOC_DAILINK_DEF(platform,
  256. DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
  257. SND_SOC_DAILINK_DEF(be,
  258. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-aif1")));
  259. SND_SOC_DAILINK_DEF(ssp0_port,
  260. DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
  261. /* Wake on voice interface */
  262. SND_SOC_DAILINK_DEFS(dsp,
  263. DAILINK_COMP_ARRAY(COMP_CPU("spi-RT5677AA:00")),
  264. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-dspbuffer")),
  265. DAILINK_COMP_ARRAY(COMP_PLATFORM("spi-RT5677AA:00")));
  266. static struct snd_soc_dai_link bdw_rt5677_dais[] = {
  267. /* Front End DAI links */
  268. {
  269. .name = "System PCM",
  270. .stream_name = "System Playback/Capture",
  271. .nonatomic = 1,
  272. .dynamic = 1,
  273. .trigger = {
  274. SND_SOC_DPCM_TRIGGER_POST,
  275. SND_SOC_DPCM_TRIGGER_POST
  276. },
  277. .dpcm_capture = 1,
  278. .dpcm_playback = 1,
  279. .ops = &bdw_rt5677_fe_ops,
  280. SND_SOC_DAILINK_REG(fe, dummy, platform),
  281. },
  282. /* Non-DPCM links */
  283. {
  284. .name = "Codec DSP",
  285. .stream_name = "Wake on Voice",
  286. .capture_only = 1,
  287. .ops = &bdw_rt5677_dsp_ops,
  288. SND_SOC_DAILINK_REG(dsp),
  289. },
  290. /* Back End DAI links */
  291. {
  292. /* SSP0 - Codec */
  293. .name = "Codec",
  294. .id = 0,
  295. .nonatomic = 1,
  296. .no_pcm = 1,
  297. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  298. SND_SOC_DAIFMT_CBC_CFC,
  299. .ignore_pmdown_time = 1,
  300. .be_hw_params_fixup = broadwell_ssp0_fixup,
  301. .ops = &bdw_rt5677_ops,
  302. .dpcm_playback = 1,
  303. .dpcm_capture = 1,
  304. .init = bdw_rt5677_init,
  305. .exit = bdw_rt5677_exit,
  306. SND_SOC_DAILINK_REG(ssp0_port, be, platform),
  307. },
  308. };
  309. static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
  310. {
  311. struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
  312. struct snd_soc_dapm_context *dapm;
  313. if (bdw_rt5677->component) {
  314. dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
  315. snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
  316. }
  317. return 0;
  318. }
  319. static int bdw_rt5677_resume_post(struct snd_soc_card *card)
  320. {
  321. struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
  322. struct snd_soc_dapm_context *dapm;
  323. if (bdw_rt5677->component) {
  324. dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
  325. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
  326. }
  327. return 0;
  328. }
  329. /* use space before codec name to simplify card ID, and simplify driver name */
  330. #define SOF_CARD_NAME "bdw rt5677" /* card name will be 'sof-bdw rt5677' */
  331. #define SOF_DRIVER_NAME "SOF"
  332. #define CARD_NAME "bdw-rt5677"
  333. #define DRIVER_NAME NULL /* card name will be used for driver name */
  334. /* ASoC machine driver for Broadwell DSP + RT5677 */
  335. static struct snd_soc_card bdw_rt5677_card = {
  336. .name = CARD_NAME,
  337. .driver_name = DRIVER_NAME,
  338. .owner = THIS_MODULE,
  339. .dai_link = bdw_rt5677_dais,
  340. .num_links = ARRAY_SIZE(bdw_rt5677_dais),
  341. .dapm_widgets = bdw_rt5677_widgets,
  342. .num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
  343. .dapm_routes = bdw_rt5677_map,
  344. .num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
  345. .controls = bdw_rt5677_controls,
  346. .num_controls = ARRAY_SIZE(bdw_rt5677_controls),
  347. .fully_routed = true,
  348. .suspend_pre = bdw_rt5677_suspend_pre,
  349. .resume_post = bdw_rt5677_resume_post,
  350. };
  351. static int bdw_rt5677_probe(struct platform_device *pdev)
  352. {
  353. struct bdw_rt5677_priv *bdw_rt5677;
  354. struct snd_soc_acpi_mach *mach;
  355. int ret;
  356. bdw_rt5677_card.dev = &pdev->dev;
  357. /* Allocate driver private struct */
  358. bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
  359. GFP_KERNEL);
  360. if (!bdw_rt5677)
  361. return -ENOMEM;
  362. /* override platform name, if required */
  363. mach = pdev->dev.platform_data;
  364. ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5677_card,
  365. mach->mach_params.platform);
  366. if (ret)
  367. return ret;
  368. /* set card and driver name */
  369. if (snd_soc_acpi_sof_parent(&pdev->dev)) {
  370. bdw_rt5677_card.name = SOF_CARD_NAME;
  371. bdw_rt5677_card.driver_name = SOF_DRIVER_NAME;
  372. } else {
  373. bdw_rt5677_card.name = CARD_NAME;
  374. bdw_rt5677_card.driver_name = DRIVER_NAME;
  375. }
  376. snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
  377. return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
  378. }
  379. static struct platform_driver bdw_rt5677_audio = {
  380. .probe = bdw_rt5677_probe,
  381. .driver = {
  382. .name = "bdw-rt5677",
  383. .pm = &snd_soc_pm_ops
  384. },
  385. };
  386. module_platform_driver(bdw_rt5677_audio)
  387. /* Module information */
  388. MODULE_AUTHOR("Ben Zhang");
  389. MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
  390. MODULE_LICENSE("GPL v2");
  391. MODULE_ALIAS("platform:bdw-rt5677");