sof_es8336.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright(c) 2021 Intel Corporation.
  3. /*
  4. * Intel SOF Machine Driver with es8336 Codec
  5. */
  6. #include <linux/device.h>
  7. #include <linux/dmi.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/gpio/machine.h>
  10. #include <linux/i2c.h>
  11. #include <linux/input.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <sound/jack.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include <sound/soc-acpi.h>
  20. #include "hda_dsp_common.h"
  21. /* jd-inv + terminating entry */
  22. #define MAX_NO_PROPS 2
  23. #define SOF_ES8336_SSP_CODEC(quirk) ((quirk) & GENMASK(3, 0))
  24. #define SOF_ES8336_SSP_CODEC_MASK (GENMASK(3, 0))
  25. #define SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK BIT(4)
  26. /* HDMI capture*/
  27. #define SOF_SSP_HDMI_CAPTURE_PRESENT BIT(14)
  28. #define SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT 15
  29. #define SOF_NO_OF_HDMI_CAPTURE_SSP_MASK (GENMASK(16, 15))
  30. #define SOF_NO_OF_HDMI_CAPTURE_SSP(quirk) \
  31. (((quirk) << SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT) & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK)
  32. #define SOF_HDMI_CAPTURE_1_SSP_SHIFT 7
  33. #define SOF_HDMI_CAPTURE_1_SSP_MASK (GENMASK(9, 7))
  34. #define SOF_HDMI_CAPTURE_1_SSP(quirk) \
  35. (((quirk) << SOF_HDMI_CAPTURE_1_SSP_SHIFT) & SOF_HDMI_CAPTURE_1_SSP_MASK)
  36. #define SOF_HDMI_CAPTURE_2_SSP_SHIFT 10
  37. #define SOF_HDMI_CAPTURE_2_SSP_MASK (GENMASK(12, 10))
  38. #define SOF_HDMI_CAPTURE_2_SSP(quirk) \
  39. (((quirk) << SOF_HDMI_CAPTURE_2_SSP_SHIFT) & SOF_HDMI_CAPTURE_2_SSP_MASK)
  40. #define SOF_ES8336_ENABLE_DMIC BIT(5)
  41. #define SOF_ES8336_JD_INVERTED BIT(6)
  42. #define SOF_ES8336_HEADPHONE_GPIO BIT(7)
  43. #define SOC_ES8336_HEADSET_MIC1 BIT(8)
  44. static unsigned long quirk;
  45. static int quirk_override = -1;
  46. module_param_named(quirk, quirk_override, int, 0444);
  47. MODULE_PARM_DESC(quirk, "Board-specific quirk override");
  48. struct sof_es8336_private {
  49. struct device *codec_dev;
  50. struct gpio_desc *gpio_speakers, *gpio_headphone;
  51. struct snd_soc_jack jack;
  52. struct list_head hdmi_pcm_list;
  53. bool speaker_en;
  54. struct delayed_work pcm_pop_work;
  55. };
  56. struct sof_hdmi_pcm {
  57. struct list_head head;
  58. struct snd_soc_dai *codec_dai;
  59. int device;
  60. };
  61. static const struct acpi_gpio_params enable_gpio0 = { 0, 0, true };
  62. static const struct acpi_gpio_params enable_gpio1 = { 1, 0, true };
  63. static const struct acpi_gpio_mapping acpi_speakers_enable_gpio0[] = {
  64. { "speakers-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  65. { }
  66. };
  67. static const struct acpi_gpio_mapping acpi_speakers_enable_gpio1[] = {
  68. { "speakers-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  69. };
  70. static const struct acpi_gpio_mapping acpi_enable_both_gpios[] = {
  71. { "speakers-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  72. { "headphone-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  73. { }
  74. };
  75. static const struct acpi_gpio_mapping acpi_enable_both_gpios_rev_order[] = {
  76. { "speakers-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  77. { "headphone-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  78. { }
  79. };
  80. static void log_quirks(struct device *dev)
  81. {
  82. dev_info(dev, "quirk mask %#lx\n", quirk);
  83. dev_info(dev, "quirk SSP%ld\n", SOF_ES8336_SSP_CODEC(quirk));
  84. if (quirk & SOF_ES8336_ENABLE_DMIC)
  85. dev_info(dev, "quirk DMIC enabled\n");
  86. if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
  87. dev_info(dev, "Speakers GPIO1 quirk enabled\n");
  88. if (quirk & SOF_ES8336_HEADPHONE_GPIO)
  89. dev_info(dev, "quirk headphone GPIO enabled\n");
  90. if (quirk & SOF_ES8336_JD_INVERTED)
  91. dev_info(dev, "quirk JD inverted enabled\n");
  92. if (quirk & SOC_ES8336_HEADSET_MIC1)
  93. dev_info(dev, "quirk headset at mic1 port enabled\n");
  94. }
  95. static void pcm_pop_work_events(struct work_struct *work)
  96. {
  97. struct sof_es8336_private *priv =
  98. container_of(work, struct sof_es8336_private, pcm_pop_work.work);
  99. gpiod_set_value_cansleep(priv->gpio_speakers, priv->speaker_en);
  100. if (quirk & SOF_ES8336_HEADPHONE_GPIO)
  101. gpiod_set_value_cansleep(priv->gpio_headphone, priv->speaker_en);
  102. }
  103. static int sof_8336_trigger(struct snd_pcm_substream *substream, int cmd)
  104. {
  105. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  106. struct snd_soc_card *card = rtd->card;
  107. struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
  108. switch (cmd) {
  109. case SNDRV_PCM_TRIGGER_START:
  110. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  111. case SNDRV_PCM_TRIGGER_RESUME:
  112. break;
  113. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  114. case SNDRV_PCM_TRIGGER_SUSPEND:
  115. case SNDRV_PCM_TRIGGER_STOP:
  116. if (priv->speaker_en == false)
  117. if (substream->stream == 0) {
  118. cancel_delayed_work(&priv->pcm_pop_work);
  119. gpiod_set_value_cansleep(priv->gpio_speakers, true);
  120. }
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. return 0;
  126. }
  127. static int sof_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
  128. struct snd_kcontrol *kcontrol, int event)
  129. {
  130. struct snd_soc_card *card = w->dapm->card;
  131. struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
  132. if (priv->speaker_en == !SND_SOC_DAPM_EVENT_ON(event))
  133. return 0;
  134. priv->speaker_en = !SND_SOC_DAPM_EVENT_ON(event);
  135. queue_delayed_work(system_wq, &priv->pcm_pop_work, msecs_to_jiffies(70));
  136. return 0;
  137. }
  138. static const struct snd_soc_dapm_widget sof_es8316_widgets[] = {
  139. SND_SOC_DAPM_SPK("Speaker", NULL),
  140. SND_SOC_DAPM_HP("Headphone", NULL),
  141. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  142. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  143. SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
  144. sof_es8316_speaker_power_event,
  145. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  146. };
  147. static const struct snd_soc_dapm_widget dmic_widgets[] = {
  148. SND_SOC_DAPM_MIC("SoC DMIC", NULL),
  149. };
  150. static const struct snd_soc_dapm_route sof_es8316_audio_map[] = {
  151. {"Headphone", NULL, "HPOL"},
  152. {"Headphone", NULL, "HPOR"},
  153. /*
  154. * There is no separate speaker output instead the speakers are muxed to
  155. * the HP outputs. The mux is controlled Speaker and/or headphone switch.
  156. */
  157. {"Speaker", NULL, "HPOL"},
  158. {"Speaker", NULL, "HPOR"},
  159. {"Speaker", NULL, "Speaker Power"},
  160. };
  161. static const struct snd_soc_dapm_route sof_es8316_headset_mic2_map[] = {
  162. {"MIC1", NULL, "Internal Mic"},
  163. {"MIC2", NULL, "Headset Mic"},
  164. };
  165. static const struct snd_soc_dapm_route sof_es8316_headset_mic1_map[] = {
  166. {"MIC2", NULL, "Internal Mic"},
  167. {"MIC1", NULL, "Headset Mic"},
  168. };
  169. static const struct snd_soc_dapm_route dmic_map[] = {
  170. /* digital mics */
  171. {"DMic", NULL, "SoC DMIC"},
  172. };
  173. static const struct snd_kcontrol_new sof_es8316_controls[] = {
  174. SOC_DAPM_PIN_SWITCH("Speaker"),
  175. SOC_DAPM_PIN_SWITCH("Headphone"),
  176. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  177. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  178. };
  179. static struct snd_soc_jack_pin sof_es8316_jack_pins[] = {
  180. {
  181. .pin = "Headphone",
  182. .mask = SND_JACK_HEADPHONE,
  183. },
  184. {
  185. .pin = "Headset Mic",
  186. .mask = SND_JACK_MICROPHONE,
  187. },
  188. };
  189. static int dmic_init(struct snd_soc_pcm_runtime *runtime)
  190. {
  191. struct snd_soc_card *card = runtime->card;
  192. int ret;
  193. ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
  194. ARRAY_SIZE(dmic_widgets));
  195. if (ret) {
  196. dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
  197. return ret;
  198. }
  199. ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
  200. ARRAY_SIZE(dmic_map));
  201. if (ret)
  202. dev_err(card->dev, "DMic map addition failed: %d\n", ret);
  203. return ret;
  204. }
  205. static int sof_hdmi_init(struct snd_soc_pcm_runtime *runtime)
  206. {
  207. struct sof_es8336_private *priv = snd_soc_card_get_drvdata(runtime->card);
  208. struct snd_soc_dai *dai = asoc_rtd_to_codec(runtime, 0);
  209. struct sof_hdmi_pcm *pcm;
  210. pcm = devm_kzalloc(runtime->card->dev, sizeof(*pcm), GFP_KERNEL);
  211. if (!pcm)
  212. return -ENOMEM;
  213. /* dai_link id is 1:1 mapped to the PCM device */
  214. pcm->device = runtime->dai_link->id;
  215. pcm->codec_dai = dai;
  216. list_add_tail(&pcm->head, &priv->hdmi_pcm_list);
  217. return 0;
  218. }
  219. static int sof_es8316_init(struct snd_soc_pcm_runtime *runtime)
  220. {
  221. struct snd_soc_component *codec = asoc_rtd_to_codec(runtime, 0)->component;
  222. struct snd_soc_card *card = runtime->card;
  223. struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
  224. const struct snd_soc_dapm_route *custom_map;
  225. int num_routes;
  226. int ret;
  227. card->dapm.idle_bias_off = true;
  228. if (quirk & SOC_ES8336_HEADSET_MIC1) {
  229. custom_map = sof_es8316_headset_mic1_map;
  230. num_routes = ARRAY_SIZE(sof_es8316_headset_mic1_map);
  231. } else {
  232. custom_map = sof_es8316_headset_mic2_map;
  233. num_routes = ARRAY_SIZE(sof_es8316_headset_mic2_map);
  234. }
  235. ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
  236. if (ret)
  237. return ret;
  238. ret = snd_soc_card_jack_new_pins(card, "Headset",
  239. SND_JACK_HEADSET | SND_JACK_BTN_0,
  240. &priv->jack, sof_es8316_jack_pins,
  241. ARRAY_SIZE(sof_es8316_jack_pins));
  242. if (ret) {
  243. dev_err(card->dev, "jack creation failed %d\n", ret);
  244. return ret;
  245. }
  246. snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  247. snd_soc_component_set_jack(codec, &priv->jack, NULL);
  248. return 0;
  249. }
  250. static void sof_es8316_exit(struct snd_soc_pcm_runtime *rtd)
  251. {
  252. struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
  253. snd_soc_component_set_jack(component, NULL, NULL);
  254. }
  255. static int sof_es8336_quirk_cb(const struct dmi_system_id *id)
  256. {
  257. quirk = (unsigned long)id->driver_data;
  258. return 1;
  259. }
  260. /*
  261. * this table should only be used to add GPIO or jack-detection quirks
  262. * that cannot be detected from ACPI tables. The SSP and DMIC
  263. * information are providing by the platform driver and are aligned
  264. * with the topology used.
  265. *
  266. * If the GPIO support is missing, the quirk parameter can be used to
  267. * enable speakers. In that case it's recommended to keep the SSP and DMIC
  268. * information consistent, overriding the SSP and DMIC can only be done
  269. * if the topology file is modified as well.
  270. */
  271. static const struct dmi_system_id sof_es8336_quirk_table[] = {
  272. {
  273. .callback = sof_es8336_quirk_cb,
  274. .matches = {
  275. DMI_MATCH(DMI_SYS_VENDOR, "IP3 tech"),
  276. DMI_MATCH(DMI_BOARD_NAME, "WN1"),
  277. },
  278. .driver_data = (void *)(SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
  279. },
  280. {
  281. .callback = sof_es8336_quirk_cb,
  282. .matches = {
  283. DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
  284. DMI_MATCH(DMI_BOARD_NAME, "BOHB-WAX9-PCB-B2"),
  285. },
  286. .driver_data = (void *)(SOF_ES8336_HEADPHONE_GPIO |
  287. SOC_ES8336_HEADSET_MIC1)
  288. },
  289. {}
  290. };
  291. static int sof_es8336_hw_params(struct snd_pcm_substream *substream,
  292. struct snd_pcm_hw_params *params)
  293. {
  294. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  295. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  296. const int sysclk = 19200000;
  297. int ret;
  298. ret = snd_soc_dai_set_sysclk(codec_dai, 1, sysclk, SND_SOC_CLOCK_OUT);
  299. if (ret < 0) {
  300. dev_err(rtd->dev, "%s, Failed to set ES8336 SYSCLK: %d\n",
  301. __func__, ret);
  302. return ret;
  303. }
  304. return 0;
  305. }
  306. /* machine stream operations */
  307. static struct snd_soc_ops sof_es8336_ops = {
  308. .hw_params = sof_es8336_hw_params,
  309. .trigger = sof_8336_trigger,
  310. };
  311. static struct snd_soc_dai_link_component platform_component[] = {
  312. {
  313. /* name might be overridden during probe */
  314. .name = "0000:00:1f.3"
  315. }
  316. };
  317. SND_SOC_DAILINK_DEF(es8336_codec,
  318. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8336:00", "ES8316 HiFi")));
  319. static struct snd_soc_dai_link_component dmic_component[] = {
  320. {
  321. .name = "dmic-codec",
  322. .dai_name = "dmic-hifi",
  323. }
  324. };
  325. static struct snd_soc_dai_link_component dummy_component[] = {
  326. {
  327. .name = "snd-soc-dummy",
  328. .dai_name = "snd-soc-dummy-dai",
  329. }
  330. };
  331. static int sof_es8336_late_probe(struct snd_soc_card *card)
  332. {
  333. struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
  334. struct sof_hdmi_pcm *pcm;
  335. if (list_empty(&priv->hdmi_pcm_list))
  336. return -ENOENT;
  337. pcm = list_first_entry(&priv->hdmi_pcm_list, struct sof_hdmi_pcm, head);
  338. return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
  339. }
  340. /* SoC card */
  341. static struct snd_soc_card sof_es8336_card = {
  342. .name = "essx8336", /* sof- prefix added automatically */
  343. .owner = THIS_MODULE,
  344. .dapm_widgets = sof_es8316_widgets,
  345. .num_dapm_widgets = ARRAY_SIZE(sof_es8316_widgets),
  346. .dapm_routes = sof_es8316_audio_map,
  347. .num_dapm_routes = ARRAY_SIZE(sof_es8316_audio_map),
  348. .controls = sof_es8316_controls,
  349. .num_controls = ARRAY_SIZE(sof_es8316_controls),
  350. .fully_routed = true,
  351. .late_probe = sof_es8336_late_probe,
  352. .num_links = 1,
  353. };
  354. static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
  355. int ssp_codec,
  356. int dmic_be_num,
  357. int hdmi_num)
  358. {
  359. struct snd_soc_dai_link_component *cpus;
  360. struct snd_soc_dai_link *links;
  361. struct snd_soc_dai_link_component *idisp_components;
  362. int hdmi_id_offset = 0;
  363. int id = 0;
  364. int i;
  365. links = devm_kcalloc(dev, sof_es8336_card.num_links,
  366. sizeof(struct snd_soc_dai_link), GFP_KERNEL);
  367. cpus = devm_kcalloc(dev, sof_es8336_card.num_links,
  368. sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
  369. if (!links || !cpus)
  370. goto devm_err;
  371. /* codec SSP */
  372. links[id].name = devm_kasprintf(dev, GFP_KERNEL,
  373. "SSP%d-Codec", ssp_codec);
  374. if (!links[id].name)
  375. goto devm_err;
  376. links[id].id = id;
  377. links[id].codecs = es8336_codec;
  378. links[id].num_codecs = ARRAY_SIZE(es8336_codec);
  379. links[id].platforms = platform_component;
  380. links[id].num_platforms = ARRAY_SIZE(platform_component);
  381. links[id].init = sof_es8316_init;
  382. links[id].exit = sof_es8316_exit;
  383. links[id].ops = &sof_es8336_ops;
  384. links[id].nonatomic = true;
  385. links[id].dpcm_playback = 1;
  386. links[id].dpcm_capture = 1;
  387. links[id].no_pcm = 1;
  388. links[id].cpus = &cpus[id];
  389. links[id].num_cpus = 1;
  390. links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  391. "SSP%d Pin",
  392. ssp_codec);
  393. if (!links[id].cpus->dai_name)
  394. goto devm_err;
  395. id++;
  396. /* dmic */
  397. if (dmic_be_num > 0) {
  398. /* at least we have dmic01 */
  399. links[id].name = "dmic01";
  400. links[id].cpus = &cpus[id];
  401. links[id].cpus->dai_name = "DMIC01 Pin";
  402. links[id].init = dmic_init;
  403. if (dmic_be_num > 1) {
  404. /* set up 2 BE links at most */
  405. links[id + 1].name = "dmic16k";
  406. links[id + 1].cpus = &cpus[id + 1];
  407. links[id + 1].cpus->dai_name = "DMIC16k Pin";
  408. dmic_be_num = 2;
  409. }
  410. } else {
  411. /* HDMI dai link starts at 3 according to current topology settings */
  412. hdmi_id_offset = 2;
  413. }
  414. for (i = 0; i < dmic_be_num; i++) {
  415. links[id].id = id;
  416. links[id].num_cpus = 1;
  417. links[id].codecs = dmic_component;
  418. links[id].num_codecs = ARRAY_SIZE(dmic_component);
  419. links[id].platforms = platform_component;
  420. links[id].num_platforms = ARRAY_SIZE(platform_component);
  421. links[id].ignore_suspend = 1;
  422. links[id].dpcm_capture = 1;
  423. links[id].no_pcm = 1;
  424. id++;
  425. }
  426. /* HDMI */
  427. if (hdmi_num > 0) {
  428. idisp_components = devm_kcalloc(dev,
  429. hdmi_num,
  430. sizeof(struct snd_soc_dai_link_component),
  431. GFP_KERNEL);
  432. if (!idisp_components)
  433. goto devm_err;
  434. }
  435. for (i = 1; i <= hdmi_num; i++) {
  436. links[id].name = devm_kasprintf(dev, GFP_KERNEL,
  437. "iDisp%d", i);
  438. if (!links[id].name)
  439. goto devm_err;
  440. links[id].id = id + hdmi_id_offset;
  441. links[id].cpus = &cpus[id];
  442. links[id].num_cpus = 1;
  443. links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  444. "iDisp%d Pin", i);
  445. if (!links[id].cpus->dai_name)
  446. goto devm_err;
  447. idisp_components[i - 1].name = "ehdaudio0D2";
  448. idisp_components[i - 1].dai_name = devm_kasprintf(dev,
  449. GFP_KERNEL,
  450. "intel-hdmi-hifi%d",
  451. i);
  452. if (!idisp_components[i - 1].dai_name)
  453. goto devm_err;
  454. links[id].codecs = &idisp_components[i - 1];
  455. links[id].num_codecs = 1;
  456. links[id].platforms = platform_component;
  457. links[id].num_platforms = ARRAY_SIZE(platform_component);
  458. links[id].init = sof_hdmi_init;
  459. links[id].dpcm_playback = 1;
  460. links[id].no_pcm = 1;
  461. id++;
  462. }
  463. /* HDMI-In SSP */
  464. if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
  465. int num_of_hdmi_ssp = (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
  466. SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
  467. for (i = 1; i <= num_of_hdmi_ssp; i++) {
  468. int port = (i == 1 ? (quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
  469. SOF_HDMI_CAPTURE_1_SSP_SHIFT :
  470. (quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
  471. SOF_HDMI_CAPTURE_2_SSP_SHIFT);
  472. links[id].cpus = &cpus[id];
  473. links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  474. "SSP%d Pin", port);
  475. if (!links[id].cpus->dai_name)
  476. return NULL;
  477. links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
  478. if (!links[id].name)
  479. return NULL;
  480. links[id].id = id + hdmi_id_offset;
  481. links[id].codecs = dummy_component;
  482. links[id].num_codecs = ARRAY_SIZE(dummy_component);
  483. links[id].platforms = platform_component;
  484. links[id].num_platforms = ARRAY_SIZE(platform_component);
  485. links[id].dpcm_capture = 1;
  486. links[id].no_pcm = 1;
  487. links[id].num_cpus = 1;
  488. id++;
  489. }
  490. }
  491. return links;
  492. devm_err:
  493. return NULL;
  494. }
  495. static char soc_components[30];
  496. /* i2c-<HID>:00 with HID being 8 chars */
  497. static char codec_name[SND_ACPI_I2C_ID_LEN];
  498. static int sof_es8336_probe(struct platform_device *pdev)
  499. {
  500. struct device *dev = &pdev->dev;
  501. struct snd_soc_card *card;
  502. struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
  503. struct property_entry props[MAX_NO_PROPS] = {};
  504. struct sof_es8336_private *priv;
  505. struct fwnode_handle *fwnode;
  506. struct acpi_device *adev;
  507. struct snd_soc_dai_link *dai_links;
  508. struct device *codec_dev;
  509. const struct acpi_gpio_mapping *gpio_mapping;
  510. unsigned int cnt = 0;
  511. int dmic_be_num = 0;
  512. int hdmi_num = 3;
  513. int ret;
  514. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  515. if (!priv)
  516. return -ENOMEM;
  517. card = &sof_es8336_card;
  518. card->dev = dev;
  519. if (pdev->id_entry && pdev->id_entry->driver_data)
  520. quirk = (unsigned long)pdev->id_entry->driver_data;
  521. /* check GPIO DMI quirks */
  522. dmi_check_system(sof_es8336_quirk_table);
  523. /* Use NHLT configuration only for Non-HDMI capture use case.
  524. * Because more than one SSP will be enabled for HDMI capture hence wrong codec
  525. * SSP will be set.
  526. */
  527. if (mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER) {
  528. if (!mach->mach_params.i2s_link_mask) {
  529. dev_warn(dev, "No I2S link information provided, using SSP0. This may need to be modified with the quirk module parameter\n");
  530. } else {
  531. /*
  532. * Set configuration based on platform NHLT.
  533. * In this machine driver, we can only support one SSP for the
  534. * ES8336 link.
  535. * In some cases multiple SSPs can be reported by NHLT, starting MSB-first
  536. * seems to pick the right connection.
  537. */
  538. unsigned long ssp;
  539. /* fls returns 1-based results, SSPs indices are 0-based */
  540. ssp = fls(mach->mach_params.i2s_link_mask) - 1;
  541. quirk |= ssp;
  542. }
  543. }
  544. if (mach->mach_params.dmic_num)
  545. quirk |= SOF_ES8336_ENABLE_DMIC;
  546. if (quirk_override != -1) {
  547. dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
  548. quirk, quirk_override);
  549. quirk = quirk_override;
  550. }
  551. log_quirks(dev);
  552. if (quirk & SOF_ES8336_ENABLE_DMIC)
  553. dmic_be_num = 2;
  554. /* compute number of dai links */
  555. sof_es8336_card.num_links = 1 + dmic_be_num + hdmi_num;
  556. if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
  557. sof_es8336_card.num_links += (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
  558. SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
  559. dai_links = sof_card_dai_links_create(dev,
  560. SOF_ES8336_SSP_CODEC(quirk),
  561. dmic_be_num, hdmi_num);
  562. if (!dai_links)
  563. return -ENOMEM;
  564. sof_es8336_card.dai_link = dai_links;
  565. /* fixup codec name based on HID */
  566. adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
  567. if (adev) {
  568. snprintf(codec_name, sizeof(codec_name),
  569. "i2c-%s", acpi_dev_name(adev));
  570. dai_links[0].codecs->name = codec_name;
  571. /* also fixup codec dai name if relevant */
  572. if (!strncmp(mach->id, "ESSX8326", SND_ACPI_I2C_ID_LEN))
  573. dai_links[0].codecs->dai_name = "ES8326 HiFi";
  574. } else {
  575. dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
  576. return -ENXIO;
  577. }
  578. codec_dev = acpi_get_first_physical_node(adev);
  579. acpi_dev_put(adev);
  580. if (!codec_dev)
  581. return -EPROBE_DEFER;
  582. priv->codec_dev = get_device(codec_dev);
  583. ret = snd_soc_fixup_dai_links_platform_name(&sof_es8336_card,
  584. mach->mach_params.platform);
  585. if (ret) {
  586. put_device(codec_dev);
  587. return ret;
  588. }
  589. if (quirk & SOF_ES8336_JD_INVERTED)
  590. props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
  591. if (cnt) {
  592. fwnode = fwnode_create_software_node(props, NULL);
  593. if (IS_ERR(fwnode)) {
  594. put_device(codec_dev);
  595. return PTR_ERR(fwnode);
  596. }
  597. ret = device_add_software_node(codec_dev, to_software_node(fwnode));
  598. fwnode_handle_put(fwnode);
  599. if (ret) {
  600. put_device(codec_dev);
  601. return ret;
  602. }
  603. }
  604. /* get speaker enable GPIO */
  605. if (quirk & SOF_ES8336_HEADPHONE_GPIO) {
  606. if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
  607. gpio_mapping = acpi_enable_both_gpios;
  608. else
  609. gpio_mapping = acpi_enable_both_gpios_rev_order;
  610. } else if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK) {
  611. gpio_mapping = acpi_speakers_enable_gpio1;
  612. } else {
  613. gpio_mapping = acpi_speakers_enable_gpio0;
  614. }
  615. ret = devm_acpi_dev_add_driver_gpios(codec_dev, gpio_mapping);
  616. if (ret)
  617. dev_warn(codec_dev, "unable to add GPIO mapping table\n");
  618. priv->gpio_speakers = gpiod_get_optional(codec_dev, "speakers-enable", GPIOD_OUT_LOW);
  619. if (IS_ERR(priv->gpio_speakers)) {
  620. ret = dev_err_probe(dev, PTR_ERR(priv->gpio_speakers),
  621. "could not get speakers-enable GPIO\n");
  622. goto err_put_codec;
  623. }
  624. priv->gpio_headphone = gpiod_get_optional(codec_dev, "headphone-enable", GPIOD_OUT_LOW);
  625. if (IS_ERR(priv->gpio_headphone)) {
  626. ret = dev_err_probe(dev, PTR_ERR(priv->gpio_headphone),
  627. "could not get headphone-enable GPIO\n");
  628. goto err_put_codec;
  629. }
  630. INIT_LIST_HEAD(&priv->hdmi_pcm_list);
  631. INIT_DELAYED_WORK(&priv->pcm_pop_work,
  632. pcm_pop_work_events);
  633. snd_soc_card_set_drvdata(card, priv);
  634. if (mach->mach_params.dmic_num > 0) {
  635. snprintf(soc_components, sizeof(soc_components),
  636. "cfg-dmics:%d", mach->mach_params.dmic_num);
  637. card->components = soc_components;
  638. }
  639. ret = devm_snd_soc_register_card(dev, card);
  640. if (ret) {
  641. gpiod_put(priv->gpio_speakers);
  642. dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
  643. goto err_put_codec;
  644. }
  645. platform_set_drvdata(pdev, &sof_es8336_card);
  646. return 0;
  647. err_put_codec:
  648. device_remove_software_node(priv->codec_dev);
  649. put_device(codec_dev);
  650. return ret;
  651. }
  652. static int sof_es8336_remove(struct platform_device *pdev)
  653. {
  654. struct snd_soc_card *card = platform_get_drvdata(pdev);
  655. struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
  656. cancel_delayed_work_sync(&priv->pcm_pop_work);
  657. gpiod_put(priv->gpio_speakers);
  658. device_remove_software_node(priv->codec_dev);
  659. put_device(priv->codec_dev);
  660. return 0;
  661. }
  662. static const struct platform_device_id board_ids[] = {
  663. {
  664. .name = "sof-essx8336", /* default quirk == 0 */
  665. },
  666. {
  667. .name = "adl_es83x6_c1_h02",
  668. .driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
  669. SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
  670. SOF_HDMI_CAPTURE_1_SSP(0) |
  671. SOF_HDMI_CAPTURE_2_SSP(2) |
  672. SOF_SSP_HDMI_CAPTURE_PRESENT |
  673. SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
  674. SOF_ES8336_JD_INVERTED),
  675. },
  676. {
  677. .name = "mtl_es83x6_c1_h02",
  678. .driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
  679. SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
  680. SOF_HDMI_CAPTURE_1_SSP(0) |
  681. SOF_HDMI_CAPTURE_2_SSP(2) |
  682. SOF_SSP_HDMI_CAPTURE_PRESENT |
  683. SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
  684. SOF_ES8336_JD_INVERTED),
  685. },
  686. { }
  687. };
  688. MODULE_DEVICE_TABLE(platform, board_ids);
  689. static struct platform_driver sof_es8336_driver = {
  690. .driver = {
  691. .name = "sof-essx8336",
  692. .pm = &snd_soc_pm_ops,
  693. },
  694. .probe = sof_es8336_probe,
  695. .remove = sof_es8336_remove,
  696. .id_table = board_ids,
  697. };
  698. module_platform_driver(sof_es8336_driver);
  699. MODULE_DESCRIPTION("ASoC Intel(R) SOF + ES8336 Machine driver");
  700. MODULE_LICENSE("GPL");
  701. MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);