sof_es8336.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 = snd_soc_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 = snd_soc_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 = snd_soc_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 = snd_soc_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 = snd_soc_substream_to_rtd(substream);
  295. struct snd_soc_dai *codec_dai = snd_soc_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 const 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 int sof_es8336_late_probe(struct snd_soc_card *card)
  326. {
  327. struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
  328. struct sof_hdmi_pcm *pcm;
  329. if (list_empty(&priv->hdmi_pcm_list))
  330. return -ENOENT;
  331. pcm = list_first_entry(&priv->hdmi_pcm_list, struct sof_hdmi_pcm, head);
  332. return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
  333. }
  334. /* SoC card */
  335. static struct snd_soc_card sof_es8336_card = {
  336. .name = "essx8336", /* sof- prefix added automatically */
  337. .owner = THIS_MODULE,
  338. .dapm_widgets = sof_es8316_widgets,
  339. .num_dapm_widgets = ARRAY_SIZE(sof_es8316_widgets),
  340. .dapm_routes = sof_es8316_audio_map,
  341. .num_dapm_routes = ARRAY_SIZE(sof_es8316_audio_map),
  342. .controls = sof_es8316_controls,
  343. .num_controls = ARRAY_SIZE(sof_es8316_controls),
  344. .fully_routed = true,
  345. .late_probe = sof_es8336_late_probe,
  346. .num_links = 1,
  347. };
  348. static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
  349. int ssp_codec,
  350. int dmic_be_num,
  351. int hdmi_num)
  352. {
  353. struct snd_soc_dai_link_component *cpus;
  354. struct snd_soc_dai_link *links;
  355. struct snd_soc_dai_link_component *idisp_components;
  356. int hdmi_id_offset = 0;
  357. int id = 0;
  358. int i;
  359. links = devm_kcalloc(dev, sof_es8336_card.num_links,
  360. sizeof(struct snd_soc_dai_link), GFP_KERNEL);
  361. cpus = devm_kcalloc(dev, sof_es8336_card.num_links,
  362. sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
  363. if (!links || !cpus)
  364. goto devm_err;
  365. /* codec SSP */
  366. links[id].name = devm_kasprintf(dev, GFP_KERNEL,
  367. "SSP%d-Codec", ssp_codec);
  368. if (!links[id].name)
  369. goto devm_err;
  370. links[id].id = id;
  371. links[id].codecs = es8336_codec;
  372. links[id].num_codecs = ARRAY_SIZE(es8336_codec);
  373. links[id].platforms = platform_component;
  374. links[id].num_platforms = ARRAY_SIZE(platform_component);
  375. links[id].init = sof_es8316_init;
  376. links[id].exit = sof_es8316_exit;
  377. links[id].ops = &sof_es8336_ops;
  378. links[id].nonatomic = true;
  379. links[id].dpcm_playback = 1;
  380. links[id].dpcm_capture = 1;
  381. links[id].no_pcm = 1;
  382. links[id].cpus = &cpus[id];
  383. links[id].num_cpus = 1;
  384. links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  385. "SSP%d Pin",
  386. ssp_codec);
  387. if (!links[id].cpus->dai_name)
  388. goto devm_err;
  389. id++;
  390. /* dmic */
  391. if (dmic_be_num > 0) {
  392. /* at least we have dmic01 */
  393. links[id].name = "dmic01";
  394. links[id].cpus = &cpus[id];
  395. links[id].cpus->dai_name = "DMIC01 Pin";
  396. links[id].init = dmic_init;
  397. if (dmic_be_num > 1) {
  398. /* set up 2 BE links at most */
  399. links[id + 1].name = "dmic16k";
  400. links[id + 1].cpus = &cpus[id + 1];
  401. links[id + 1].cpus->dai_name = "DMIC16k Pin";
  402. dmic_be_num = 2;
  403. }
  404. } else {
  405. /* HDMI dai link starts at 3 according to current topology settings */
  406. hdmi_id_offset = 2;
  407. }
  408. for (i = 0; i < dmic_be_num; i++) {
  409. links[id].id = id;
  410. links[id].num_cpus = 1;
  411. links[id].codecs = dmic_component;
  412. links[id].num_codecs = ARRAY_SIZE(dmic_component);
  413. links[id].platforms = platform_component;
  414. links[id].num_platforms = ARRAY_SIZE(platform_component);
  415. links[id].ignore_suspend = 1;
  416. links[id].dpcm_capture = 1;
  417. links[id].no_pcm = 1;
  418. id++;
  419. }
  420. /* HDMI */
  421. if (hdmi_num > 0) {
  422. idisp_components = devm_kcalloc(dev,
  423. hdmi_num,
  424. sizeof(struct snd_soc_dai_link_component),
  425. GFP_KERNEL);
  426. if (!idisp_components)
  427. goto devm_err;
  428. }
  429. for (i = 1; i <= hdmi_num; i++) {
  430. links[id].name = devm_kasprintf(dev, GFP_KERNEL,
  431. "iDisp%d", i);
  432. if (!links[id].name)
  433. goto devm_err;
  434. links[id].id = id + hdmi_id_offset;
  435. links[id].cpus = &cpus[id];
  436. links[id].num_cpus = 1;
  437. links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  438. "iDisp%d Pin", i);
  439. if (!links[id].cpus->dai_name)
  440. goto devm_err;
  441. idisp_components[i - 1].name = "ehdaudio0D2";
  442. idisp_components[i - 1].dai_name = devm_kasprintf(dev,
  443. GFP_KERNEL,
  444. "intel-hdmi-hifi%d",
  445. i);
  446. if (!idisp_components[i - 1].dai_name)
  447. goto devm_err;
  448. links[id].codecs = &idisp_components[i - 1];
  449. links[id].num_codecs = 1;
  450. links[id].platforms = platform_component;
  451. links[id].num_platforms = ARRAY_SIZE(platform_component);
  452. links[id].init = sof_hdmi_init;
  453. links[id].dpcm_playback = 1;
  454. links[id].no_pcm = 1;
  455. id++;
  456. }
  457. /* HDMI-In SSP */
  458. if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
  459. int num_of_hdmi_ssp = (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
  460. SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
  461. for (i = 1; i <= num_of_hdmi_ssp; i++) {
  462. int port = (i == 1 ? (quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
  463. SOF_HDMI_CAPTURE_1_SSP_SHIFT :
  464. (quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
  465. SOF_HDMI_CAPTURE_2_SSP_SHIFT);
  466. links[id].cpus = &cpus[id];
  467. links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
  468. "SSP%d Pin", port);
  469. if (!links[id].cpus->dai_name)
  470. return NULL;
  471. links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
  472. if (!links[id].name)
  473. return NULL;
  474. links[id].id = id + hdmi_id_offset;
  475. links[id].codecs = &snd_soc_dummy_dlc;
  476. links[id].num_codecs = 1;
  477. links[id].platforms = platform_component;
  478. links[id].num_platforms = ARRAY_SIZE(platform_component);
  479. links[id].dpcm_capture = 1;
  480. links[id].no_pcm = 1;
  481. links[id].num_cpus = 1;
  482. id++;
  483. }
  484. }
  485. return links;
  486. devm_err:
  487. return NULL;
  488. }
  489. static char soc_components[30];
  490. /* i2c-<HID>:00 with HID being 8 chars */
  491. static char codec_name[SND_ACPI_I2C_ID_LEN];
  492. static int sof_es8336_probe(struct platform_device *pdev)
  493. {
  494. struct device *dev = &pdev->dev;
  495. struct snd_soc_card *card;
  496. struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
  497. struct property_entry props[MAX_NO_PROPS] = {};
  498. struct sof_es8336_private *priv;
  499. struct fwnode_handle *fwnode;
  500. struct acpi_device *adev;
  501. struct snd_soc_dai_link *dai_links;
  502. struct device *codec_dev;
  503. const struct acpi_gpio_mapping *gpio_mapping;
  504. unsigned int cnt = 0;
  505. int dmic_be_num = 0;
  506. int hdmi_num = 3;
  507. int ret;
  508. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  509. if (!priv)
  510. return -ENOMEM;
  511. card = &sof_es8336_card;
  512. card->dev = dev;
  513. if (pdev->id_entry && pdev->id_entry->driver_data)
  514. quirk = (unsigned long)pdev->id_entry->driver_data;
  515. /* check GPIO DMI quirks */
  516. dmi_check_system(sof_es8336_quirk_table);
  517. /* Use NHLT configuration only for Non-HDMI capture use case.
  518. * Because more than one SSP will be enabled for HDMI capture hence wrong codec
  519. * SSP will be set.
  520. */
  521. if (mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER) {
  522. if (!mach->mach_params.i2s_link_mask) {
  523. dev_warn(dev, "No I2S link information provided, using SSP0. This may need to be modified with the quirk module parameter\n");
  524. } else {
  525. /*
  526. * Set configuration based on platform NHLT.
  527. * In this machine driver, we can only support one SSP for the
  528. * ES8336 link.
  529. * In some cases multiple SSPs can be reported by NHLT, starting MSB-first
  530. * seems to pick the right connection.
  531. */
  532. unsigned long ssp;
  533. /* fls returns 1-based results, SSPs indices are 0-based */
  534. ssp = fls(mach->mach_params.i2s_link_mask) - 1;
  535. quirk |= ssp;
  536. }
  537. }
  538. if (mach->mach_params.dmic_num)
  539. quirk |= SOF_ES8336_ENABLE_DMIC;
  540. if (quirk_override != -1) {
  541. dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
  542. quirk, quirk_override);
  543. quirk = quirk_override;
  544. }
  545. log_quirks(dev);
  546. if (quirk & SOF_ES8336_ENABLE_DMIC)
  547. dmic_be_num = 2;
  548. /* compute number of dai links */
  549. sof_es8336_card.num_links = 1 + dmic_be_num + hdmi_num;
  550. if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
  551. sof_es8336_card.num_links += (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
  552. SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
  553. dai_links = sof_card_dai_links_create(dev,
  554. SOF_ES8336_SSP_CODEC(quirk),
  555. dmic_be_num, hdmi_num);
  556. if (!dai_links)
  557. return -ENOMEM;
  558. sof_es8336_card.dai_link = dai_links;
  559. /* fixup codec name based on HID */
  560. adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
  561. if (adev) {
  562. snprintf(codec_name, sizeof(codec_name),
  563. "i2c-%s", acpi_dev_name(adev));
  564. dai_links[0].codecs->name = codec_name;
  565. /* also fixup codec dai name if relevant */
  566. if (!strncmp(mach->id, "ESSX8326", SND_ACPI_I2C_ID_LEN))
  567. dai_links[0].codecs->dai_name = "ES8326 HiFi";
  568. } else {
  569. dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
  570. return -ENOENT;
  571. }
  572. codec_dev = acpi_get_first_physical_node(adev);
  573. acpi_dev_put(adev);
  574. if (!codec_dev)
  575. return -EPROBE_DEFER;
  576. priv->codec_dev = get_device(codec_dev);
  577. ret = snd_soc_fixup_dai_links_platform_name(&sof_es8336_card,
  578. mach->mach_params.platform);
  579. if (ret) {
  580. put_device(codec_dev);
  581. return ret;
  582. }
  583. if (quirk & SOF_ES8336_JD_INVERTED)
  584. props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
  585. if (cnt) {
  586. fwnode = fwnode_create_software_node(props, NULL);
  587. if (IS_ERR(fwnode)) {
  588. put_device(codec_dev);
  589. return PTR_ERR(fwnode);
  590. }
  591. ret = device_add_software_node(codec_dev, to_software_node(fwnode));
  592. fwnode_handle_put(fwnode);
  593. if (ret) {
  594. put_device(codec_dev);
  595. return ret;
  596. }
  597. }
  598. /* get speaker enable GPIO */
  599. if (quirk & SOF_ES8336_HEADPHONE_GPIO) {
  600. if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
  601. gpio_mapping = acpi_enable_both_gpios;
  602. else
  603. gpio_mapping = acpi_enable_both_gpios_rev_order;
  604. } else if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK) {
  605. gpio_mapping = acpi_speakers_enable_gpio1;
  606. } else {
  607. gpio_mapping = acpi_speakers_enable_gpio0;
  608. }
  609. ret = devm_acpi_dev_add_driver_gpios(codec_dev, gpio_mapping);
  610. if (ret)
  611. dev_warn(codec_dev, "unable to add GPIO mapping table\n");
  612. priv->gpio_speakers = gpiod_get_optional(codec_dev, "speakers-enable", GPIOD_OUT_LOW);
  613. if (IS_ERR(priv->gpio_speakers)) {
  614. ret = dev_err_probe(dev, PTR_ERR(priv->gpio_speakers),
  615. "could not get speakers-enable GPIO\n");
  616. goto err_put_codec;
  617. }
  618. priv->gpio_headphone = gpiod_get_optional(codec_dev, "headphone-enable", GPIOD_OUT_LOW);
  619. if (IS_ERR(priv->gpio_headphone)) {
  620. ret = dev_err_probe(dev, PTR_ERR(priv->gpio_headphone),
  621. "could not get headphone-enable GPIO\n");
  622. goto err_put_codec;
  623. }
  624. INIT_LIST_HEAD(&priv->hdmi_pcm_list);
  625. INIT_DELAYED_WORK(&priv->pcm_pop_work,
  626. pcm_pop_work_events);
  627. snd_soc_card_set_drvdata(card, priv);
  628. if (mach->mach_params.dmic_num > 0) {
  629. snprintf(soc_components, sizeof(soc_components),
  630. "cfg-dmics:%d", mach->mach_params.dmic_num);
  631. card->components = soc_components;
  632. }
  633. ret = devm_snd_soc_register_card(dev, card);
  634. if (ret) {
  635. gpiod_put(priv->gpio_speakers);
  636. dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
  637. goto err_put_codec;
  638. }
  639. platform_set_drvdata(pdev, &sof_es8336_card);
  640. return 0;
  641. err_put_codec:
  642. device_remove_software_node(priv->codec_dev);
  643. put_device(codec_dev);
  644. return ret;
  645. }
  646. static void sof_es8336_remove(struct platform_device *pdev)
  647. {
  648. struct snd_soc_card *card = platform_get_drvdata(pdev);
  649. struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
  650. cancel_delayed_work_sync(&priv->pcm_pop_work);
  651. gpiod_put(priv->gpio_speakers);
  652. device_remove_software_node(priv->codec_dev);
  653. put_device(priv->codec_dev);
  654. }
  655. static const struct platform_device_id board_ids[] = {
  656. {
  657. .name = "sof-essx8336", /* default quirk == 0 */
  658. },
  659. {
  660. .name = "adl_es83x6_c1_h02",
  661. .driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
  662. SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
  663. SOF_HDMI_CAPTURE_1_SSP(0) |
  664. SOF_HDMI_CAPTURE_2_SSP(2) |
  665. SOF_SSP_HDMI_CAPTURE_PRESENT |
  666. SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
  667. SOF_ES8336_JD_INVERTED),
  668. },
  669. {
  670. .name = "rpl_es83x6_c1_h02",
  671. .driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
  672. SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
  673. SOF_HDMI_CAPTURE_1_SSP(0) |
  674. SOF_HDMI_CAPTURE_2_SSP(2) |
  675. SOF_SSP_HDMI_CAPTURE_PRESENT |
  676. SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
  677. SOF_ES8336_JD_INVERTED),
  678. },
  679. {
  680. .name = "mtl_es83x6_c1_h02",
  681. .driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
  682. SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
  683. SOF_HDMI_CAPTURE_1_SSP(0) |
  684. SOF_HDMI_CAPTURE_2_SSP(2) |
  685. SOF_SSP_HDMI_CAPTURE_PRESENT |
  686. SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
  687. SOF_ES8336_JD_INVERTED),
  688. },
  689. {
  690. .name = "arl_es83x6_c1_h02",
  691. .driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
  692. SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
  693. SOF_HDMI_CAPTURE_1_SSP(0) |
  694. SOF_HDMI_CAPTURE_2_SSP(2) |
  695. SOF_SSP_HDMI_CAPTURE_PRESENT |
  696. SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
  697. SOF_ES8336_JD_INVERTED),
  698. },
  699. { }
  700. };
  701. MODULE_DEVICE_TABLE(platform, board_ids);
  702. static struct platform_driver sof_es8336_driver = {
  703. .driver = {
  704. .name = "sof-essx8336",
  705. .pm = &snd_soc_pm_ops,
  706. },
  707. .probe = sof_es8336_probe,
  708. .remove = sof_es8336_remove,
  709. .id_table = board_ids,
  710. };
  711. module_platform_driver(sof_es8336_driver);
  712. MODULE_DESCRIPTION("ASoC Intel(R) SOF + ES8336 Machine driver");
  713. MODULE_LICENSE("GPL");
  714. MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);