sof_rt5682.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright(c) 2019-2020 Intel Corporation.
  3. /*
  4. * Intel SOF Machine Driver with Realtek rt5682 Codec
  5. * and speaker codec MAX98357A or RT1015.
  6. */
  7. #include <linux/i2c.h>
  8. #include <linux/input.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/clk.h>
  12. #include <linux/dmi.h>
  13. #include <sound/core.h>
  14. #include <sound/jack.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <sound/sof.h>
  19. #include <sound/rt5682.h>
  20. #include <sound/rt5682s.h>
  21. #include <sound/soc-acpi.h>
  22. #include "../../codecs/rt5682.h"
  23. #include "../../codecs/rt5682s.h"
  24. #include "../../codecs/rt5645.h"
  25. #include "../common/soc-intel-quirks.h"
  26. #include "sof_board_helpers.h"
  27. #include "sof_maxim_common.h"
  28. #include "sof_realtek_common.h"
  29. /* Driver-specific board quirks: from bit 0 to 7 */
  30. #define SOF_RT5682_MCLK_EN BIT(0)
  31. /* Default: MCLK on, MCLK 19.2M, SSP0 */
  32. static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
  33. SOF_SSP_PORT_CODEC(0);
  34. static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
  35. {
  36. sof_rt5682_quirk = (unsigned long)id->driver_data;
  37. return 1;
  38. }
  39. static const struct dmi_system_id sof_rt5682_quirk_table[] = {
  40. {
  41. .callback = sof_rt5682_quirk_cb,
  42. .matches = {
  43. DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
  44. DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max"),
  45. },
  46. .driver_data = (void *)(SOF_SSP_PORT_CODEC(2)),
  47. },
  48. {
  49. .callback = sof_rt5682_quirk_cb,
  50. .matches = {
  51. DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
  52. DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
  53. },
  54. .driver_data = (void *)(SOF_SSP_PORT_CODEC(2)),
  55. },
  56. {
  57. .callback = sof_rt5682_quirk_cb,
  58. .matches = {
  59. DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
  60. DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
  61. },
  62. .driver_data = (void *)(SOF_RT5682_MCLK_EN |
  63. SOF_SSP_PORT_CODEC(1)),
  64. },
  65. {
  66. .callback = sof_rt5682_quirk_cb,
  67. .matches = {
  68. DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"),
  69. DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"),
  70. },
  71. .driver_data = (void *)(SOF_RT5682_MCLK_EN |
  72. SOF_SSP_PORT_CODEC(0) |
  73. SOF_SSP_PORT_AMP(2) |
  74. SOF_NUM_IDISP_HDMI(4)),
  75. },
  76. {
  77. .callback = sof_rt5682_quirk_cb,
  78. .matches = {
  79. DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
  80. DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
  81. DMI_MATCH(DMI_OEM_STRING, "AUDIO-ADL_MAX98373_ALC5682I_I2S"),
  82. },
  83. .driver_data = (void *)(SOF_RT5682_MCLK_EN |
  84. SOF_SSP_PORT_CODEC(0) |
  85. SOF_SSP_PORT_AMP(2) |
  86. SOF_NUM_IDISP_HDMI(4)),
  87. },
  88. {
  89. .callback = sof_rt5682_quirk_cb,
  90. .matches = {
  91. DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
  92. DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S"),
  93. },
  94. .driver_data = (void *)(SOF_RT5682_MCLK_EN |
  95. SOF_SSP_PORT_CODEC(0) |
  96. SOF_SSP_PORT_AMP(2) |
  97. SOF_NUM_IDISP_HDMI(4)),
  98. },
  99. {
  100. .callback = sof_rt5682_quirk_cb,
  101. .matches = {
  102. DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
  103. DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98360_ALC5682I_I2S_AMP_SSP2"),
  104. },
  105. .driver_data = (void *)(SOF_RT5682_MCLK_EN |
  106. SOF_SSP_PORT_CODEC(0) |
  107. SOF_SSP_PORT_AMP(2) |
  108. SOF_NUM_IDISP_HDMI(4)),
  109. },
  110. {
  111. .callback = sof_rt5682_quirk_cb,
  112. .matches = {
  113. DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Rex"),
  114. },
  115. .driver_data = (void *)(SOF_RT5682_MCLK_EN |
  116. SOF_SSP_PORT_CODEC(2) |
  117. SOF_SSP_PORT_AMP(0) |
  118. SOF_SSP_PORT_BT_OFFLOAD(1) |
  119. SOF_BT_OFFLOAD_PRESENT
  120. ),
  121. },
  122. {}
  123. };
  124. static struct snd_soc_jack_pin jack_pins[] = {
  125. {
  126. .pin = "Headphone Jack",
  127. .mask = SND_JACK_HEADPHONE,
  128. },
  129. {
  130. .pin = "Headset Mic",
  131. .mask = SND_JACK_MICROPHONE,
  132. },
  133. };
  134. static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
  135. {
  136. struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
  137. struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
  138. struct snd_soc_jack *jack = &ctx->headset_jack;
  139. int extra_jack_data;
  140. int ret, mclk_freq;
  141. if (ctx->rt5682.mclk_en) {
  142. mclk_freq = sof_dai_get_mclk(rtd);
  143. if (mclk_freq <= 0) {
  144. dev_err(rtd->dev, "invalid mclk freq %d\n", mclk_freq);
  145. return -EINVAL;
  146. }
  147. /* need to enable ASRC function for 24MHz mclk rate */
  148. if (mclk_freq == 24000000) {
  149. dev_info(rtd->dev, "enable ASRC\n");
  150. switch (ctx->codec_type) {
  151. case CODEC_RT5650:
  152. rt5645_sel_asrc_clk_src(component,
  153. RT5645_DA_STEREO_FILTER |
  154. RT5645_AD_STEREO_FILTER,
  155. RT5645_CLK_SEL_I2S1_ASRC);
  156. rt5645_sel_asrc_clk_src(component,
  157. RT5645_DA_MONO_L_FILTER |
  158. RT5645_DA_MONO_R_FILTER,
  159. RT5645_CLK_SEL_I2S2_ASRC);
  160. break;
  161. case CODEC_RT5682:
  162. rt5682_sel_asrc_clk_src(component,
  163. RT5682_DA_STEREO1_FILTER |
  164. RT5682_AD_STEREO1_FILTER,
  165. RT5682_CLK_SEL_I2S1_ASRC);
  166. break;
  167. case CODEC_RT5682S:
  168. rt5682s_sel_asrc_clk_src(component,
  169. RT5682S_DA_STEREO1_FILTER |
  170. RT5682S_AD_STEREO1_FILTER,
  171. RT5682S_CLK_SEL_I2S1_ASRC);
  172. break;
  173. default:
  174. dev_err(rtd->dev, "invalid codec type %d\n",
  175. ctx->codec_type);
  176. return -EINVAL;
  177. }
  178. }
  179. if (ctx->rt5682.is_legacy_cpu) {
  180. /*
  181. * The firmware might enable the clock at
  182. * boot (this information may or may not
  183. * be reflected in the enable clock register).
  184. * To change the rate we must disable the clock
  185. * first to cover these cases. Due to common
  186. * clock framework restrictions that do not allow
  187. * to disable a clock that has not been enabled,
  188. * we need to enable the clock first.
  189. */
  190. ret = clk_prepare_enable(ctx->rt5682.mclk);
  191. if (!ret)
  192. clk_disable_unprepare(ctx->rt5682.mclk);
  193. ret = clk_set_rate(ctx->rt5682.mclk, 19200000);
  194. if (ret)
  195. dev_err(rtd->dev, "unable to set MCLK rate\n");
  196. }
  197. }
  198. /*
  199. * Headset buttons map to the google Reference headset.
  200. * These can be configured by userspace.
  201. */
  202. ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
  203. SND_JACK_HEADSET | SND_JACK_BTN_0 |
  204. SND_JACK_BTN_1 | SND_JACK_BTN_2 |
  205. SND_JACK_BTN_3,
  206. jack,
  207. jack_pins,
  208. ARRAY_SIZE(jack_pins));
  209. if (ret) {
  210. dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
  211. return ret;
  212. }
  213. snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  214. snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
  215. snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
  216. snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
  217. if (ctx->codec_type == CODEC_RT5650) {
  218. extra_jack_data = SND_JACK_MICROPHONE | SND_JACK_BTN_0;
  219. ret = snd_soc_component_set_jack(component, jack, &extra_jack_data);
  220. } else
  221. ret = snd_soc_component_set_jack(component, jack, NULL);
  222. if (ret) {
  223. dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
  224. return ret;
  225. }
  226. return ret;
  227. };
  228. static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
  229. {
  230. struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
  231. snd_soc_component_set_jack(component, NULL, NULL);
  232. }
  233. static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
  234. struct snd_pcm_hw_params *params)
  235. {
  236. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  237. struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
  238. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
  239. int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
  240. if (ctx->rt5682.mclk_en) {
  241. if (ctx->rt5682.is_legacy_cpu) {
  242. ret = clk_prepare_enable(ctx->rt5682.mclk);
  243. if (ret < 0) {
  244. dev_err(rtd->dev,
  245. "could not configure MCLK state");
  246. return ret;
  247. }
  248. }
  249. switch (ctx->codec_type) {
  250. case CODEC_RT5650:
  251. pll_source = RT5645_PLL1_S_MCLK;
  252. break;
  253. case CODEC_RT5682:
  254. pll_source = RT5682_PLL1_S_MCLK;
  255. break;
  256. case CODEC_RT5682S:
  257. pll_source = RT5682S_PLL_S_MCLK;
  258. break;
  259. default:
  260. dev_err(rtd->dev, "invalid codec type %d\n",
  261. ctx->codec_type);
  262. return -EINVAL;
  263. }
  264. /* get the tplg configured mclk. */
  265. pll_in = sof_dai_get_mclk(rtd);
  266. if (pll_in <= 0) {
  267. dev_err(rtd->dev, "invalid mclk freq %d\n", pll_in);
  268. return -EINVAL;
  269. }
  270. } else {
  271. switch (ctx->codec_type) {
  272. case CODEC_RT5650:
  273. pll_source = RT5645_PLL1_S_BCLK1;
  274. break;
  275. case CODEC_RT5682:
  276. pll_source = RT5682_PLL1_S_BCLK1;
  277. break;
  278. case CODEC_RT5682S:
  279. pll_source = RT5682S_PLL_S_BCLK1;
  280. break;
  281. default:
  282. dev_err(rtd->dev, "invalid codec type %d\n",
  283. ctx->codec_type);
  284. return -EINVAL;
  285. }
  286. /* get the tplg configured bclk. */
  287. pll_in = sof_dai_get_bclk(rtd);
  288. if (pll_in <= 0) {
  289. dev_err(rtd->dev, "invalid bclk freq %d\n", pll_in);
  290. return -EINVAL;
  291. }
  292. }
  293. pll_out = params_rate(params) * 512;
  294. /* when MCLK is 512FS, no need to set PLL configuration additionally. */
  295. if (pll_in == pll_out) {
  296. switch (ctx->codec_type) {
  297. case CODEC_RT5650:
  298. clk_id = RT5645_SCLK_S_MCLK;
  299. break;
  300. case CODEC_RT5682:
  301. clk_id = RT5682_SCLK_S_MCLK;
  302. break;
  303. case CODEC_RT5682S:
  304. clk_id = RT5682S_SCLK_S_MCLK;
  305. break;
  306. default:
  307. dev_err(rtd->dev, "invalid codec type %d\n",
  308. ctx->codec_type);
  309. return -EINVAL;
  310. }
  311. } else {
  312. switch (ctx->codec_type) {
  313. case CODEC_RT5650:
  314. pll_id = 0; /* not used in codec driver */
  315. clk_id = RT5645_SCLK_S_PLL1;
  316. break;
  317. case CODEC_RT5682:
  318. pll_id = RT5682_PLL1;
  319. clk_id = RT5682_SCLK_S_PLL1;
  320. break;
  321. case CODEC_RT5682S:
  322. /* check plla_table and pllb_table in rt5682s.c */
  323. switch (pll_in) {
  324. case 3072000:
  325. case 24576000:
  326. /*
  327. * For MCLK = 24.576MHz and sample rate = 96KHz case, use PLL1 We don't test
  328. * pll_out or params_rate() here since rt5682s PLL2 doesn't support 24.576MHz
  329. * input, so we have no choice but to use PLL1. Besides, we will not use PLL at
  330. * all if pll_in == pll_out. ex, MCLK = 24.576Mhz and sample rate = 48KHz
  331. */
  332. pll_id = RT5682S_PLL1;
  333. clk_id = RT5682S_SCLK_S_PLL1;
  334. break;
  335. default:
  336. pll_id = RT5682S_PLL2;
  337. clk_id = RT5682S_SCLK_S_PLL2;
  338. break;
  339. }
  340. break;
  341. default:
  342. dev_err(rtd->dev, "invalid codec type %d\n", ctx->codec_type);
  343. return -EINVAL;
  344. }
  345. /* Configure pll for codec */
  346. ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, pll_in,
  347. pll_out);
  348. if (ret < 0)
  349. dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
  350. }
  351. /* Configure sysclk for codec */
  352. ret = snd_soc_dai_set_sysclk(codec_dai, clk_id,
  353. pll_out, SND_SOC_CLOCK_IN);
  354. if (ret < 0)
  355. dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
  356. /*
  357. * slot_width should equal or large than data length, set them
  358. * be the same
  359. */
  360. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
  361. params_width(params));
  362. if (ret < 0) {
  363. dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
  364. return ret;
  365. }
  366. return ret;
  367. }
  368. static const struct snd_soc_ops sof_rt5682_ops = {
  369. .hw_params = sof_rt5682_hw_params,
  370. };
  371. static int sof_card_late_probe(struct snd_soc_card *card)
  372. {
  373. struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
  374. struct snd_soc_dapm_context *dapm = &card->dapm;
  375. int err;
  376. if (ctx->amp_type == CODEC_MAX98373) {
  377. /* Disable Left and Right Spk pin after boot */
  378. snd_soc_dapm_disable_pin(dapm, "Left Spk");
  379. snd_soc_dapm_disable_pin(dapm, "Right Spk");
  380. err = snd_soc_dapm_sync(dapm);
  381. if (err < 0)
  382. return err;
  383. }
  384. return sof_intel_board_card_late_probe(card);
  385. }
  386. static const struct snd_kcontrol_new sof_controls[] = {
  387. SOC_DAPM_PIN_SWITCH("Headphone Jack"),
  388. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  389. };
  390. static const struct snd_soc_dapm_widget sof_widgets[] = {
  391. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  392. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  393. };
  394. static const struct snd_soc_dapm_route sof_map[] = {
  395. /* HP jack connectors - unknown if we have jack detection */
  396. { "Headphone Jack", NULL, "HPOL" },
  397. { "Headphone Jack", NULL, "HPOR" },
  398. /* other jacks */
  399. { "IN1P", NULL, "Headset Mic" },
  400. };
  401. static const struct snd_kcontrol_new rt5650_spk_kcontrols[] = {
  402. SOC_DAPM_PIN_SWITCH("Left Spk"),
  403. SOC_DAPM_PIN_SWITCH("Right Spk"),
  404. };
  405. static const struct snd_soc_dapm_widget rt5650_spk_widgets[] = {
  406. SND_SOC_DAPM_SPK("Left Spk", NULL),
  407. SND_SOC_DAPM_SPK("Right Spk", NULL),
  408. };
  409. static const struct snd_soc_dapm_route rt5650_spk_dapm_routes[] = {
  410. /* speaker */
  411. { "Left Spk", NULL, "SPOL" },
  412. { "Right Spk", NULL, "SPOR" },
  413. };
  414. static int rt5650_spk_init(struct snd_soc_pcm_runtime *rtd)
  415. {
  416. struct snd_soc_card *card = rtd->card;
  417. int ret;
  418. ret = snd_soc_dapm_new_controls(&card->dapm, rt5650_spk_widgets,
  419. ARRAY_SIZE(rt5650_spk_widgets));
  420. if (ret) {
  421. dev_err(rtd->dev, "fail to add rt5650 spk widgets, ret %d\n",
  422. ret);
  423. return ret;
  424. }
  425. ret = snd_soc_add_card_controls(card, rt5650_spk_kcontrols,
  426. ARRAY_SIZE(rt5650_spk_kcontrols));
  427. if (ret) {
  428. dev_err(rtd->dev, "fail to add rt5650 spk kcontrols, ret %d\n",
  429. ret);
  430. return ret;
  431. }
  432. ret = snd_soc_dapm_add_routes(&card->dapm, rt5650_spk_dapm_routes,
  433. ARRAY_SIZE(rt5650_spk_dapm_routes));
  434. if (ret)
  435. dev_err(rtd->dev, "fail to add dapm routes, ret=%d\n", ret);
  436. return ret;
  437. }
  438. /* sof audio machine driver for rt5682 codec */
  439. static struct snd_soc_card sof_audio_card_rt5682 = {
  440. .name = "rt5682", /* the sof- prefix is added by the core */
  441. .owner = THIS_MODULE,
  442. .controls = sof_controls,
  443. .num_controls = ARRAY_SIZE(sof_controls),
  444. .dapm_widgets = sof_widgets,
  445. .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
  446. .dapm_routes = sof_map,
  447. .num_dapm_routes = ARRAY_SIZE(sof_map),
  448. .fully_routed = true,
  449. .late_probe = sof_card_late_probe,
  450. };
  451. static struct snd_soc_dai_link_component rt5682_component[] = {
  452. {
  453. .name = "i2c-10EC5682:00",
  454. .dai_name = "rt5682-aif1",
  455. }
  456. };
  457. static struct snd_soc_dai_link_component rt5682s_component[] = {
  458. {
  459. .name = "i2c-RTL5682:00",
  460. .dai_name = "rt5682s-aif1",
  461. }
  462. };
  463. static struct snd_soc_dai_link_component rt5650_components[] = {
  464. {
  465. .name = "i2c-10EC5650:00",
  466. .dai_name = "rt5645-aif1",
  467. },
  468. {
  469. .name = "i2c-10EC5650:00",
  470. .dai_name = "rt5645-aif2",
  471. }
  472. };
  473. static int
  474. sof_card_dai_links_create(struct device *dev, struct snd_soc_card *card,
  475. struct sof_card_private *ctx)
  476. {
  477. int ret;
  478. ret = sof_intel_board_set_dai_link(dev, card, ctx);
  479. if (ret)
  480. return ret;
  481. if (!ctx->codec_link) {
  482. dev_err(dev, "codec link not available");
  483. return -EINVAL;
  484. }
  485. /* codec-specific fields for headphone codec */
  486. switch (ctx->codec_type) {
  487. case CODEC_RT5650:
  488. ctx->codec_link->codecs = &rt5650_components[0];
  489. ctx->codec_link->num_codecs = 1;
  490. break;
  491. case CODEC_RT5682:
  492. ctx->codec_link->codecs = rt5682_component;
  493. ctx->codec_link->num_codecs = ARRAY_SIZE(rt5682_component);
  494. break;
  495. case CODEC_RT5682S:
  496. ctx->codec_link->codecs = rt5682s_component;
  497. ctx->codec_link->num_codecs = ARRAY_SIZE(rt5682s_component);
  498. break;
  499. default:
  500. dev_err(dev, "invalid codec type %d\n", ctx->codec_type);
  501. return -EINVAL;
  502. }
  503. ctx->codec_link->init = sof_rt5682_codec_init;
  504. ctx->codec_link->exit = sof_rt5682_codec_exit;
  505. ctx->codec_link->ops = &sof_rt5682_ops;
  506. if (!ctx->rt5682.is_legacy_cpu) {
  507. /*
  508. * Currently, On SKL+ platforms MCLK will be turned off in sof
  509. * runtime suspended, and it will go into runtime suspended
  510. * right after playback is stop. However, rt5682 will output
  511. * static noise if sysclk turns off during playback. Set
  512. * ignore_pmdown_time to power down rt5682 immediately and
  513. * avoid the noise.
  514. * It can be removed once we can control MCLK by driver.
  515. */
  516. ctx->codec_link->ignore_pmdown_time = 1;
  517. }
  518. if (ctx->amp_type == CODEC_NONE)
  519. return 0;
  520. if (!ctx->amp_link) {
  521. dev_err(dev, "amp link not available");
  522. return -EINVAL;
  523. }
  524. /* codec-specific fields for speaker amplifier */
  525. switch (ctx->amp_type) {
  526. case CODEC_MAX98357A:
  527. max_98357a_dai_link(ctx->amp_link);
  528. break;
  529. case CODEC_MAX98360A:
  530. max_98360a_dai_link(ctx->amp_link);
  531. break;
  532. case CODEC_MAX98373:
  533. max_98373_dai_link(dev, ctx->amp_link);
  534. break;
  535. case CODEC_MAX98390:
  536. max_98390_dai_link(dev, ctx->amp_link);
  537. break;
  538. case CODEC_RT1011:
  539. sof_rt1011_dai_link(dev, ctx->amp_link);
  540. break;
  541. case CODEC_RT1015:
  542. sof_rt1015_dai_link(ctx->amp_link);
  543. break;
  544. case CODEC_RT1015P:
  545. sof_rt1015p_dai_link(ctx->amp_link);
  546. break;
  547. case CODEC_RT1019P:
  548. sof_rt1019p_dai_link(ctx->amp_link);
  549. break;
  550. case CODEC_RT5650:
  551. /* use AIF2 to support speaker pipeline */
  552. ctx->amp_link->codecs = &rt5650_components[1];
  553. ctx->amp_link->num_codecs = 1;
  554. ctx->amp_link->init = rt5650_spk_init;
  555. ctx->amp_link->ops = &sof_rt5682_ops;
  556. break;
  557. default:
  558. dev_err(dev, "invalid amp type %d\n", ctx->amp_type);
  559. return -EINVAL;
  560. }
  561. return 0;
  562. }
  563. #define GLK_LINK_ORDER SOF_LINK_ORDER(SOF_LINK_AMP, \
  564. SOF_LINK_CODEC, \
  565. SOF_LINK_DMIC01, \
  566. SOF_LINK_IDISP_HDMI, \
  567. SOF_LINK_NONE, \
  568. SOF_LINK_NONE, \
  569. SOF_LINK_NONE)
  570. static int sof_audio_probe(struct platform_device *pdev)
  571. {
  572. struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
  573. struct sof_card_private *ctx;
  574. char *card_name;
  575. int ret;
  576. if (pdev->id_entry && pdev->id_entry->driver_data)
  577. sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
  578. dmi_check_system(sof_rt5682_quirk_table);
  579. dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
  580. /* initialize ctx with board quirk */
  581. ctx = sof_intel_board_get_ctx(&pdev->dev, sof_rt5682_quirk);
  582. if (!ctx)
  583. return -ENOMEM;
  584. if (ctx->codec_type == CODEC_RT5650) {
  585. card_name = devm_kstrdup(&pdev->dev, "rt5650", GFP_KERNEL);
  586. if (!card_name)
  587. return -ENOMEM;
  588. sof_audio_card_rt5682.name = card_name;
  589. /* create speaker dai link also */
  590. if (ctx->amp_type == CODEC_NONE)
  591. ctx->amp_type = CODEC_RT5650;
  592. }
  593. if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
  594. ctx->hdmi.idisp_codec = true;
  595. if (soc_intel_is_byt() || soc_intel_is_cht()) {
  596. ctx->rt5682.is_legacy_cpu = true;
  597. ctx->dmic_be_num = 0;
  598. /* HDMI is not supported by SOF on Baytrail/CherryTrail */
  599. ctx->hdmi_num = 0;
  600. } else if (soc_intel_is_glk()) {
  601. /* dmic16k not support */
  602. ctx->dmic_be_num = 1;
  603. /* overwrite the DAI link order for GLK boards */
  604. ctx->link_order_overwrite = GLK_LINK_ORDER;
  605. /* backward-compatible with existing devices */
  606. switch (ctx->amp_type) {
  607. case CODEC_MAX98357A:
  608. card_name = devm_kstrdup(&pdev->dev, "glkrt5682max",
  609. GFP_KERNEL);
  610. if (!card_name)
  611. return -ENOMEM;
  612. sof_audio_card_rt5682.name = card_name;
  613. break;
  614. default:
  615. break;
  616. }
  617. } else if (soc_intel_is_cml()) {
  618. /* backward-compatible with existing devices */
  619. switch (ctx->amp_type) {
  620. case CODEC_RT1011:
  621. card_name = devm_kstrdup(&pdev->dev, "cml_rt1011_rt5682",
  622. GFP_KERNEL);
  623. if (!card_name)
  624. return -ENOMEM;
  625. sof_audio_card_rt5682.name = card_name;
  626. break;
  627. default:
  628. break;
  629. }
  630. }
  631. if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
  632. ctx->rt5682.mclk_en = true;
  633. /* need to get main clock from pmc */
  634. if (ctx->rt5682.is_legacy_cpu) {
  635. ctx->rt5682.mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
  636. if (IS_ERR(ctx->rt5682.mclk)) {
  637. ret = PTR_ERR(ctx->rt5682.mclk);
  638. dev_err(&pdev->dev,
  639. "Failed to get MCLK from pmc_plt_clk_3: %d\n",
  640. ret);
  641. return ret;
  642. }
  643. ret = clk_prepare_enable(ctx->rt5682.mclk);
  644. if (ret < 0) {
  645. dev_err(&pdev->dev,
  646. "could not configure MCLK state");
  647. return ret;
  648. }
  649. }
  650. }
  651. /* update dai_link */
  652. ret = sof_card_dai_links_create(&pdev->dev, &sof_audio_card_rt5682, ctx);
  653. if (ret)
  654. return ret;
  655. /* update codec_conf */
  656. switch (ctx->amp_type) {
  657. case CODEC_MAX98373:
  658. max_98373_set_codec_conf(&sof_audio_card_rt5682);
  659. break;
  660. case CODEC_MAX98390:
  661. max_98390_set_codec_conf(&pdev->dev, &sof_audio_card_rt5682);
  662. break;
  663. case CODEC_RT1011:
  664. sof_rt1011_codec_conf(&pdev->dev, &sof_audio_card_rt5682);
  665. break;
  666. case CODEC_RT1015:
  667. sof_rt1015_codec_conf(&sof_audio_card_rt5682);
  668. break;
  669. case CODEC_RT1015P:
  670. sof_rt1015p_codec_conf(&sof_audio_card_rt5682);
  671. break;
  672. case CODEC_MAX98357A:
  673. case CODEC_MAX98360A:
  674. case CODEC_RT1019P:
  675. case CODEC_RT5650:
  676. case CODEC_NONE:
  677. /* no codec conf required */
  678. break;
  679. default:
  680. dev_err(&pdev->dev, "invalid amp type %d\n", ctx->amp_type);
  681. return -EINVAL;
  682. }
  683. sof_audio_card_rt5682.dev = &pdev->dev;
  684. /* set platform name for each dailink */
  685. ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
  686. mach->mach_params.platform);
  687. if (ret)
  688. return ret;
  689. snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
  690. return devm_snd_soc_register_card(&pdev->dev,
  691. &sof_audio_card_rt5682);
  692. }
  693. static const struct platform_device_id board_ids[] = {
  694. {
  695. .name = "sof_rt5682",
  696. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  697. SOF_SSP_PORT_CODEC(2)),
  698. },
  699. {
  700. .name = "glk_rt5682_def",
  701. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  702. SOF_SSP_PORT_CODEC(2) |
  703. SOF_SSP_PORT_AMP(1)),
  704. },
  705. {
  706. .name = "icl_rt5682_def",
  707. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  708. SOF_SSP_PORT_CODEC(0)),
  709. },
  710. {
  711. .name = "cml_rt5682_def",
  712. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  713. SOF_SSP_PORT_CODEC(0) |
  714. SOF_SSP_PORT_AMP(1)),
  715. },
  716. {
  717. .name = "jsl_rt5682_def",
  718. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  719. SOF_SSP_PORT_CODEC(0) |
  720. SOF_SSP_PORT_AMP(1)),
  721. },
  722. {
  723. .name = "tgl_rt5682_def",
  724. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  725. SOF_SSP_PORT_CODEC(0) |
  726. SOF_SSP_PORT_AMP(1) |
  727. SOF_NUM_IDISP_HDMI(4) |
  728. SOF_SSP_PORT_BT_OFFLOAD(2) |
  729. SOF_BT_OFFLOAD_PRESENT),
  730. },
  731. {
  732. .name = "adl_rt5682_def",
  733. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  734. SOF_SSP_PORT_CODEC(0) |
  735. SOF_SSP_PORT_AMP(1) |
  736. SOF_NUM_IDISP_HDMI(4) |
  737. SOF_SSP_PORT_BT_OFFLOAD(2) |
  738. SOF_BT_OFFLOAD_PRESENT),
  739. },
  740. {
  741. .name = "adl_mx98357_rt5682",
  742. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  743. SOF_SSP_PORT_CODEC(0) |
  744. SOF_SSP_PORT_AMP(2) |
  745. SOF_NUM_IDISP_HDMI(4)),
  746. },
  747. {
  748. .name = "adl_rt5682_c1_h02",
  749. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  750. SOF_SSP_PORT_CODEC(1) |
  751. /* SSP 0 and SSP 2 are used for HDMI IN */
  752. SOF_SSP_MASK_HDMI_CAPTURE(0x5)),
  753. },
  754. {
  755. .name = "rpl_mx98357_rt5682",
  756. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  757. SOF_SSP_PORT_CODEC(0) |
  758. SOF_SSP_PORT_AMP(2) |
  759. SOF_NUM_IDISP_HDMI(4)),
  760. },
  761. {
  762. .name = "rpl_rt5682_def",
  763. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  764. SOF_SSP_PORT_CODEC(0) |
  765. SOF_SSP_PORT_AMP(1) |
  766. SOF_NUM_IDISP_HDMI(4) |
  767. SOF_SSP_PORT_BT_OFFLOAD(2) |
  768. SOF_BT_OFFLOAD_PRESENT),
  769. },
  770. {
  771. .name = "rpl_rt5682_c1_h02",
  772. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  773. SOF_SSP_PORT_CODEC(1) |
  774. /* SSP 0 and SSP 2 are used for HDMI IN */
  775. SOF_SSP_MASK_HDMI_CAPTURE(0x5)),
  776. },
  777. {
  778. .name = "mtl_rt5682_def",
  779. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  780. SOF_SSP_PORT_CODEC(0) |
  781. SOF_SSP_PORT_AMP(1) |
  782. SOF_SSP_PORT_BT_OFFLOAD(2) |
  783. SOF_BT_OFFLOAD_PRESENT),
  784. },
  785. {
  786. .name = "mtl_rt5682_c1_h02",
  787. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  788. SOF_SSP_PORT_CODEC(1) |
  789. /* SSP 0 and SSP 2 are used for HDMI IN */
  790. SOF_SSP_MASK_HDMI_CAPTURE(0x5)),
  791. },
  792. {
  793. .name = "arl_rt5682_c1_h02",
  794. .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
  795. SOF_SSP_PORT_CODEC(1) |
  796. /* SSP 0 and SSP 2 are used for HDMI IN */
  797. SOF_SSP_MASK_HDMI_CAPTURE(0x5)),
  798. },
  799. { }
  800. };
  801. MODULE_DEVICE_TABLE(platform, board_ids);
  802. static struct platform_driver sof_audio = {
  803. .probe = sof_audio_probe,
  804. .driver = {
  805. .name = "sof_rt5682",
  806. .pm = &snd_soc_pm_ops,
  807. },
  808. .id_table = board_ids,
  809. };
  810. module_platform_driver(sof_audio)
  811. /* Module information */
  812. MODULE_DESCRIPTION("SOF Audio Machine driver");
  813. MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
  814. MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
  815. MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
  816. MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
  817. MODULE_LICENSE("GPL v2");
  818. MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_BOARD_HELPERS);
  819. MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
  820. MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);