bytcht_es8316.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * bytcht_es8316.c - ASoc Machine driver for Intel Baytrail/Cherrytrail
  4. * platforms with Everest ES8316 SoC
  5. *
  6. * Copyright (C) 2017 Endless Mobile, Inc.
  7. * Authors: David Yang <yangxiaohua@everest-semi.com>,
  8. * Daniel Drake <drake@endlessm.com>
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/clk.h>
  16. #include <linux/device.h>
  17. #include <linux/dmi.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/i2c.h>
  20. #include <linux/init.h>
  21. #include <linux/input.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <sound/jack.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/soc-acpi.h>
  30. #include "../../codecs/es83xx-dsm-common.h"
  31. #include "../atom/sst-atom-controls.h"
  32. #include "../common/soc-intel-quirks.h"
  33. /* jd-inv + terminating entry */
  34. #define MAX_NO_PROPS 2
  35. struct byt_cht_es8316_private {
  36. struct clk *mclk;
  37. struct snd_soc_jack jack;
  38. struct gpio_desc *speaker_en_gpio;
  39. struct device *codec_dev;
  40. bool speaker_en;
  41. };
  42. enum {
  43. BYT_CHT_ES8316_INTMIC_IN1_MAP,
  44. BYT_CHT_ES8316_INTMIC_IN2_MAP,
  45. };
  46. #define BYT_CHT_ES8316_MAP(quirk) ((quirk) & GENMASK(3, 0))
  47. #define BYT_CHT_ES8316_SSP0 BIT(16)
  48. #define BYT_CHT_ES8316_MONO_SPEAKER BIT(17)
  49. #define BYT_CHT_ES8316_JD_INVERTED BIT(18)
  50. static unsigned long quirk;
  51. static int quirk_override = -1;
  52. module_param_named(quirk, quirk_override, int, 0444);
  53. MODULE_PARM_DESC(quirk, "Board-specific quirk override");
  54. static void log_quirks(struct device *dev)
  55. {
  56. if (BYT_CHT_ES8316_MAP(quirk) == BYT_CHT_ES8316_INTMIC_IN1_MAP)
  57. dev_info(dev, "quirk IN1_MAP enabled");
  58. if (BYT_CHT_ES8316_MAP(quirk) == BYT_CHT_ES8316_INTMIC_IN2_MAP)
  59. dev_info(dev, "quirk IN2_MAP enabled");
  60. if (quirk & BYT_CHT_ES8316_SSP0)
  61. dev_info(dev, "quirk SSP0 enabled");
  62. if (quirk & BYT_CHT_ES8316_MONO_SPEAKER)
  63. dev_info(dev, "quirk MONO_SPEAKER enabled\n");
  64. if (quirk & BYT_CHT_ES8316_JD_INVERTED)
  65. dev_info(dev, "quirk JD_INVERTED enabled\n");
  66. }
  67. static int byt_cht_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
  68. struct snd_kcontrol *kcontrol, int event)
  69. {
  70. struct snd_soc_card *card = w->dapm->card;
  71. struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
  72. if (SND_SOC_DAPM_EVENT_ON(event))
  73. priv->speaker_en = true;
  74. else
  75. priv->speaker_en = false;
  76. gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
  77. return 0;
  78. }
  79. static const struct snd_soc_dapm_widget byt_cht_es8316_widgets[] = {
  80. SND_SOC_DAPM_SPK("Speaker", NULL),
  81. SND_SOC_DAPM_HP("Headphone", NULL),
  82. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  83. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  84. SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
  85. byt_cht_es8316_speaker_power_event,
  86. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  87. };
  88. static const struct snd_soc_dapm_route byt_cht_es8316_audio_map[] = {
  89. {"Headphone", NULL, "HPOL"},
  90. {"Headphone", NULL, "HPOR"},
  91. /*
  92. * There is no separate speaker output instead the speakers are muxed to
  93. * the HP outputs. The mux is controlled by the "Speaker Power" supply.
  94. */
  95. {"Speaker", NULL, "HPOL"},
  96. {"Speaker", NULL, "HPOR"},
  97. {"Speaker", NULL, "Speaker Power"},
  98. };
  99. static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in1_map[] = {
  100. {"MIC1", NULL, "Internal Mic"},
  101. {"MIC2", NULL, "Headset Mic"},
  102. };
  103. static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in2_map[] = {
  104. {"MIC2", NULL, "Internal Mic"},
  105. {"MIC1", NULL, "Headset Mic"},
  106. };
  107. static const struct snd_soc_dapm_route byt_cht_es8316_ssp0_map[] = {
  108. {"Playback", NULL, "ssp0 Tx"},
  109. {"ssp0 Tx", NULL, "modem_out"},
  110. {"modem_in", NULL, "ssp0 Rx"},
  111. {"ssp0 Rx", NULL, "Capture"},
  112. };
  113. static const struct snd_soc_dapm_route byt_cht_es8316_ssp2_map[] = {
  114. {"Playback", NULL, "ssp2 Tx"},
  115. {"ssp2 Tx", NULL, "codec_out0"},
  116. {"ssp2 Tx", NULL, "codec_out1"},
  117. {"codec_in0", NULL, "ssp2 Rx" },
  118. {"codec_in1", NULL, "ssp2 Rx" },
  119. {"ssp2 Rx", NULL, "Capture"},
  120. };
  121. static const struct snd_kcontrol_new byt_cht_es8316_controls[] = {
  122. SOC_DAPM_PIN_SWITCH("Speaker"),
  123. SOC_DAPM_PIN_SWITCH("Headphone"),
  124. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  125. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  126. };
  127. static struct snd_soc_jack_pin byt_cht_es8316_jack_pins[] = {
  128. {
  129. .pin = "Headphone",
  130. .mask = SND_JACK_HEADPHONE,
  131. },
  132. {
  133. .pin = "Headset Mic",
  134. .mask = SND_JACK_MICROPHONE,
  135. },
  136. };
  137. static int byt_cht_es8316_init(struct snd_soc_pcm_runtime *runtime)
  138. {
  139. struct snd_soc_component *codec = snd_soc_rtd_to_codec(runtime, 0)->component;
  140. struct snd_soc_card *card = runtime->card;
  141. struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
  142. const struct snd_soc_dapm_route *custom_map;
  143. int num_routes;
  144. int ret;
  145. card->dapm.idle_bias_off = true;
  146. switch (BYT_CHT_ES8316_MAP(quirk)) {
  147. case BYT_CHT_ES8316_INTMIC_IN1_MAP:
  148. default:
  149. custom_map = byt_cht_es8316_intmic_in1_map;
  150. num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in1_map);
  151. break;
  152. case BYT_CHT_ES8316_INTMIC_IN2_MAP:
  153. custom_map = byt_cht_es8316_intmic_in2_map;
  154. num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in2_map);
  155. break;
  156. }
  157. ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
  158. if (ret)
  159. return ret;
  160. if (quirk & BYT_CHT_ES8316_SSP0) {
  161. custom_map = byt_cht_es8316_ssp0_map;
  162. num_routes = ARRAY_SIZE(byt_cht_es8316_ssp0_map);
  163. } else {
  164. custom_map = byt_cht_es8316_ssp2_map;
  165. num_routes = ARRAY_SIZE(byt_cht_es8316_ssp2_map);
  166. }
  167. ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
  168. if (ret)
  169. return ret;
  170. /*
  171. * The firmware might enable the clock at boot (this information
  172. * may or may not be reflected in the enable clock register).
  173. * To change the rate we must disable the clock first to cover these
  174. * cases. Due to common clock framework restrictions that do not allow
  175. * to disable a clock that has not been enabled, we need to enable
  176. * the clock first.
  177. */
  178. ret = clk_prepare_enable(priv->mclk);
  179. if (!ret)
  180. clk_disable_unprepare(priv->mclk);
  181. ret = clk_set_rate(priv->mclk, 19200000);
  182. if (ret)
  183. dev_err(card->dev, "unable to set MCLK rate\n");
  184. ret = clk_prepare_enable(priv->mclk);
  185. if (ret)
  186. dev_err(card->dev, "unable to enable MCLK\n");
  187. ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_codec(runtime, 0), 0, 19200000,
  188. SND_SOC_CLOCK_IN);
  189. if (ret < 0) {
  190. dev_err(card->dev, "can't set codec clock %d\n", ret);
  191. return ret;
  192. }
  193. ret = snd_soc_card_jack_new_pins(card, "Headset",
  194. SND_JACK_HEADSET | SND_JACK_BTN_0,
  195. &priv->jack, byt_cht_es8316_jack_pins,
  196. ARRAY_SIZE(byt_cht_es8316_jack_pins));
  197. if (ret) {
  198. dev_err(card->dev, "jack creation failed %d\n", ret);
  199. return ret;
  200. }
  201. snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  202. snd_soc_component_set_jack(codec, &priv->jack, NULL);
  203. return 0;
  204. }
  205. static int byt_cht_es8316_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  206. struct snd_pcm_hw_params *params)
  207. {
  208. struct snd_interval *rate = hw_param_interval(params,
  209. SNDRV_PCM_HW_PARAM_RATE);
  210. struct snd_interval *channels = hw_param_interval(params,
  211. SNDRV_PCM_HW_PARAM_CHANNELS);
  212. int ret, bits;
  213. /* The DSP will convert the FE rate to 48k, stereo */
  214. rate->min = rate->max = 48000;
  215. channels->min = channels->max = 2;
  216. if (quirk & BYT_CHT_ES8316_SSP0) {
  217. /* set SSP0 to 16-bit */
  218. params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
  219. bits = 16;
  220. } else {
  221. /* set SSP2 to 24-bit */
  222. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  223. bits = 24;
  224. }
  225. /*
  226. * Default mode for SSP configuration is TDM 4 slot, override config
  227. * with explicit setting to I2S 2ch 24-bit. The word length is set with
  228. * dai_set_tdm_slot() since there is no other API exposed
  229. */
  230. ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),
  231. SND_SOC_DAIFMT_I2S |
  232. SND_SOC_DAIFMT_NB_NF |
  233. SND_SOC_DAIFMT_BP_FP
  234. );
  235. if (ret < 0) {
  236. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  237. return ret;
  238. }
  239. ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
  240. if (ret < 0) {
  241. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  242. return ret;
  243. }
  244. return 0;
  245. }
  246. static int byt_cht_es8316_aif1_startup(struct snd_pcm_substream *substream)
  247. {
  248. return snd_pcm_hw_constraint_single(substream->runtime,
  249. SNDRV_PCM_HW_PARAM_RATE, 48000);
  250. }
  251. static const struct snd_soc_ops byt_cht_es8316_aif1_ops = {
  252. .startup = byt_cht_es8316_aif1_startup,
  253. };
  254. SND_SOC_DAILINK_DEF(dummy,
  255. DAILINK_COMP_ARRAY(COMP_DUMMY()));
  256. SND_SOC_DAILINK_DEF(media,
  257. DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
  258. SND_SOC_DAILINK_DEF(deepbuffer,
  259. DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
  260. SND_SOC_DAILINK_DEF(ssp2_port,
  261. DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
  262. SND_SOC_DAILINK_DEF(ssp2_codec,
  263. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8316:00", "ES8316 HiFi")));
  264. SND_SOC_DAILINK_DEF(platform,
  265. DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
  266. static struct snd_soc_dai_link byt_cht_es8316_dais[] = {
  267. [MERR_DPCM_AUDIO] = {
  268. .name = "Audio Port",
  269. .stream_name = "Audio",
  270. .nonatomic = true,
  271. .dynamic = 1,
  272. .dpcm_playback = 1,
  273. .dpcm_capture = 1,
  274. .ops = &byt_cht_es8316_aif1_ops,
  275. SND_SOC_DAILINK_REG(media, dummy, platform),
  276. },
  277. [MERR_DPCM_DEEP_BUFFER] = {
  278. .name = "Deep-Buffer Audio Port",
  279. .stream_name = "Deep-Buffer Audio",
  280. .nonatomic = true,
  281. .dynamic = 1,
  282. .dpcm_playback = 1,
  283. .ops = &byt_cht_es8316_aif1_ops,
  284. SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
  285. },
  286. /* back ends */
  287. {
  288. .name = "SSP2-Codec",
  289. .id = 0,
  290. .no_pcm = 1,
  291. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  292. | SND_SOC_DAIFMT_CBC_CFC,
  293. .be_hw_params_fixup = byt_cht_es8316_codec_fixup,
  294. .dpcm_playback = 1,
  295. .dpcm_capture = 1,
  296. .init = byt_cht_es8316_init,
  297. SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
  298. },
  299. };
  300. /* SoC card */
  301. static char codec_name[SND_ACPI_I2C_ID_LEN];
  302. #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
  303. static char long_name[50]; /* = "bytcht-es8316-*-spk-*-mic" */
  304. #endif
  305. static char components_string[32]; /* = "cfg-spk:* cfg-mic:* */
  306. static int byt_cht_es8316_suspend(struct snd_soc_card *card)
  307. {
  308. struct snd_soc_component *component;
  309. for_each_card_components(card, component) {
  310. if (!strcmp(component->name, codec_name)) {
  311. dev_dbg(component->dev, "disabling jack detect before suspend\n");
  312. snd_soc_component_set_jack(component, NULL, NULL);
  313. break;
  314. }
  315. }
  316. return 0;
  317. }
  318. static int byt_cht_es8316_resume(struct snd_soc_card *card)
  319. {
  320. struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
  321. struct snd_soc_component *component;
  322. for_each_card_components(card, component) {
  323. if (!strcmp(component->name, codec_name)) {
  324. dev_dbg(component->dev, "re-enabling jack detect after resume\n");
  325. snd_soc_component_set_jack(component, &priv->jack, NULL);
  326. break;
  327. }
  328. }
  329. /*
  330. * Some Cherry Trail boards with an ES8316 codec have a bug in their
  331. * ACPI tables where the MSSL1680 touchscreen's _PS0 and _PS3 methods
  332. * wrongly also set the speaker-enable GPIO to 1/0. Testing has shown
  333. * that this really is a bug and the GPIO has no influence on the
  334. * touchscreen at all.
  335. *
  336. * The silead.c touchscreen driver does not support runtime suspend, so
  337. * the GPIO can only be changed underneath us during a system suspend.
  338. * This resume() function runs from a pm complete() callback, and thus
  339. * is guaranteed to run after the touchscreen driver/ACPI-subsys has
  340. * brought the touchscreen back up again (and thus changed the GPIO).
  341. *
  342. * So to work around this we pass GPIOD_FLAGS_BIT_NONEXCLUSIVE when
  343. * requesting the GPIO and we set its value here to undo any changes
  344. * done by the touchscreen's broken _PS0 ACPI method.
  345. */
  346. gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
  347. return 0;
  348. }
  349. /* use space before codec name to simplify card ID, and simplify driver name */
  350. #define SOF_CARD_NAME "bytcht es8316" /* card name will be 'sof-bytcht es8316' */
  351. #define SOF_DRIVER_NAME "SOF"
  352. #define CARD_NAME "bytcht-es8316"
  353. #define DRIVER_NAME NULL /* card name will be used for driver name */
  354. static struct snd_soc_card byt_cht_es8316_card = {
  355. .owner = THIS_MODULE,
  356. .dai_link = byt_cht_es8316_dais,
  357. .num_links = ARRAY_SIZE(byt_cht_es8316_dais),
  358. .dapm_widgets = byt_cht_es8316_widgets,
  359. .num_dapm_widgets = ARRAY_SIZE(byt_cht_es8316_widgets),
  360. .dapm_routes = byt_cht_es8316_audio_map,
  361. .num_dapm_routes = ARRAY_SIZE(byt_cht_es8316_audio_map),
  362. .controls = byt_cht_es8316_controls,
  363. .num_controls = ARRAY_SIZE(byt_cht_es8316_controls),
  364. .fully_routed = true,
  365. .suspend_pre = byt_cht_es8316_suspend,
  366. .resume_post = byt_cht_es8316_resume,
  367. };
  368. static const struct acpi_gpio_params first_gpio = { 0, 0, false };
  369. static const struct acpi_gpio_mapping byt_cht_es8316_gpios[] = {
  370. { "speaker-enable-gpios", &first_gpio, 1 },
  371. { },
  372. };
  373. /* Please keep this list alphabetically sorted */
  374. static const struct dmi_system_id byt_cht_es8316_quirk_table[] = {
  375. { /* Irbis NB41 */
  376. .matches = {
  377. DMI_MATCH(DMI_SYS_VENDOR, "IRBIS"),
  378. DMI_MATCH(DMI_PRODUCT_NAME, "NB41"),
  379. },
  380. .driver_data = (void *)(BYT_CHT_ES8316_SSP0
  381. | BYT_CHT_ES8316_INTMIC_IN2_MAP
  382. | BYT_CHT_ES8316_JD_INVERTED),
  383. },
  384. { /* Nanote UMPC-01 */
  385. .matches = {
  386. DMI_MATCH(DMI_SYS_VENDOR, "RWC CO.,LTD"),
  387. DMI_MATCH(DMI_PRODUCT_NAME, "UMPC-01"),
  388. },
  389. .driver_data = (void *)BYT_CHT_ES8316_INTMIC_IN1_MAP,
  390. },
  391. { /* Teclast X98 Plus II */
  392. .matches = {
  393. DMI_MATCH(DMI_SYS_VENDOR, "TECLAST"),
  394. DMI_MATCH(DMI_PRODUCT_NAME, "X98 Plus II"),
  395. },
  396. .driver_data = (void *)(BYT_CHT_ES8316_INTMIC_IN1_MAP
  397. | BYT_CHT_ES8316_JD_INVERTED),
  398. },
  399. {}
  400. };
  401. static int byt_cht_es8316_get_quirks_from_dsm(struct byt_cht_es8316_private *priv,
  402. bool is_bytcr)
  403. {
  404. int ret, val1, val2, dsm_quirk = 0;
  405. if (is_bytcr)
  406. dsm_quirk |= BYT_CHT_ES8316_SSP0;
  407. ret = es83xx_dsm(priv->codec_dev, PLATFORM_MAINMIC_TYPE_ARG, &val1);
  408. if (ret < 0)
  409. return ret;
  410. ret = es83xx_dsm(priv->codec_dev, PLATFORM_HPMIC_TYPE_ARG, &val2);
  411. if (ret < 0)
  412. return ret;
  413. if (val1 == PLATFORM_MIC_AMIC_LIN1RIN1 && val2 == PLATFORM_MIC_AMIC_LIN2RIN2) {
  414. dsm_quirk |= BYT_CHT_ES8316_INTMIC_IN1_MAP;
  415. } else if (val1 == PLATFORM_MIC_AMIC_LIN2RIN2 && val2 == PLATFORM_MIC_AMIC_LIN1RIN1) {
  416. dsm_quirk |= BYT_CHT_ES8316_INTMIC_IN2_MAP;
  417. } else {
  418. dev_warn(priv->codec_dev, "Unknown mic settings mainmic 0x%02x hpmic 0x%02x\n",
  419. val1, val2);
  420. return -EINVAL;
  421. }
  422. ret = es83xx_dsm(priv->codec_dev, PLATFORM_SPK_TYPE_ARG, &val1);
  423. if (ret < 0)
  424. return ret;
  425. switch (val1) {
  426. case PLATFORM_SPK_MONO:
  427. dsm_quirk |= BYT_CHT_ES8316_MONO_SPEAKER;
  428. break;
  429. case PLATFORM_SPK_STEREO:
  430. break;
  431. default:
  432. dev_warn(priv->codec_dev, "Unknown speaker setting 0x%02x\n", val1);
  433. return -EINVAL;
  434. }
  435. ret = es83xx_dsm(priv->codec_dev, PLATFORM_HPDET_INV_ARG, &val1);
  436. if (ret < 0)
  437. return ret;
  438. switch (val1) {
  439. case PLATFORM_HPDET_NORMAL:
  440. break;
  441. case PLATFORM_HPDET_INVERTED:
  442. dsm_quirk |= BYT_CHT_ES8316_JD_INVERTED;
  443. break;
  444. default:
  445. dev_warn(priv->codec_dev, "Unknown hpdet-inv setting 0x%02x\n", val1);
  446. return -EINVAL;
  447. }
  448. quirk = dsm_quirk;
  449. return 0;
  450. }
  451. static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
  452. {
  453. struct device *dev = &pdev->dev;
  454. static const char * const mic_name[] = { "in1", "in2" };
  455. struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
  456. struct property_entry props[MAX_NO_PROPS] = {};
  457. struct byt_cht_es8316_private *priv;
  458. const struct dmi_system_id *dmi_id;
  459. struct fwnode_handle *fwnode;
  460. bool sof_parent, is_bytcr;
  461. const char *platform_name;
  462. struct acpi_device *adev;
  463. struct device *codec_dev;
  464. unsigned int cnt = 0;
  465. int dai_index = 0;
  466. int i;
  467. int ret = 0;
  468. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  469. if (!priv)
  470. return -ENOMEM;
  471. /* fix index of codec dai */
  472. for (i = 0; i < ARRAY_SIZE(byt_cht_es8316_dais); i++) {
  473. if (byt_cht_es8316_dais[i].num_codecs &&
  474. !strcmp(byt_cht_es8316_dais[i].codecs->name,
  475. "i2c-ESSX8316:00")) {
  476. dai_index = i;
  477. break;
  478. }
  479. }
  480. /* fixup codec name based on HID */
  481. adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
  482. if (adev) {
  483. snprintf(codec_name, sizeof(codec_name),
  484. "i2c-%s", acpi_dev_name(adev));
  485. byt_cht_es8316_dais[dai_index].codecs->name = codec_name;
  486. } else {
  487. dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
  488. return -ENOENT;
  489. }
  490. codec_dev = acpi_get_first_physical_node(adev);
  491. acpi_dev_put(adev);
  492. if (!codec_dev)
  493. return -EPROBE_DEFER;
  494. priv->codec_dev = get_device(codec_dev);
  495. /* override platform name, if required */
  496. byt_cht_es8316_card.dev = dev;
  497. platform_name = mach->mach_params.platform;
  498. ret = snd_soc_fixup_dai_links_platform_name(&byt_cht_es8316_card,
  499. platform_name);
  500. if (ret) {
  501. put_device(codec_dev);
  502. return ret;
  503. }
  504. es83xx_dsm_dump(priv->codec_dev);
  505. /* Check for BYTCR or other platform and setup quirks */
  506. is_bytcr = soc_intel_is_byt() && mach->mach_params.acpi_ipc_irq_index == 0;
  507. dmi_id = dmi_first_match(byt_cht_es8316_quirk_table);
  508. if (dmi_id) {
  509. quirk = (unsigned long)dmi_id->driver_data;
  510. } else if (!byt_cht_es8316_get_quirks_from_dsm(priv, is_bytcr)) {
  511. dev_info(dev, "Using ACPI DSM info for quirks\n");
  512. } else if (is_bytcr) {
  513. /* On BYTCR default to SSP0, internal-mic-in2-map, mono-spk */
  514. quirk = BYT_CHT_ES8316_SSP0 | BYT_CHT_ES8316_INTMIC_IN2_MAP |
  515. BYT_CHT_ES8316_MONO_SPEAKER;
  516. } else {
  517. /* Others default to internal-mic-in1-map, mono-speaker */
  518. quirk = BYT_CHT_ES8316_INTMIC_IN1_MAP |
  519. BYT_CHT_ES8316_MONO_SPEAKER;
  520. }
  521. if (quirk_override != -1) {
  522. dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
  523. quirk, quirk_override);
  524. quirk = quirk_override;
  525. }
  526. log_quirks(dev);
  527. if (quirk & BYT_CHT_ES8316_SSP0)
  528. byt_cht_es8316_dais[dai_index].cpus->dai_name = "ssp0-port";
  529. /* get the clock */
  530. priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
  531. if (IS_ERR(priv->mclk)) {
  532. put_device(codec_dev);
  533. return dev_err_probe(dev, PTR_ERR(priv->mclk), "clk_get pmc_plt_clk_3 failed\n");
  534. }
  535. if (quirk & BYT_CHT_ES8316_JD_INVERTED)
  536. props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
  537. if (cnt) {
  538. fwnode = fwnode_create_software_node(props, NULL);
  539. if (IS_ERR(fwnode)) {
  540. put_device(codec_dev);
  541. return PTR_ERR(fwnode);
  542. }
  543. ret = device_add_software_node(codec_dev, to_software_node(fwnode));
  544. fwnode_handle_put(fwnode);
  545. if (ret) {
  546. put_device(codec_dev);
  547. return ret;
  548. }
  549. }
  550. /* get speaker enable GPIO */
  551. devm_acpi_dev_add_driver_gpios(codec_dev, byt_cht_es8316_gpios);
  552. priv->speaker_en_gpio =
  553. gpiod_get_optional(codec_dev, "speaker-enable",
  554. /* see comment in byt_cht_es8316_resume() */
  555. GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  556. if (IS_ERR(priv->speaker_en_gpio)) {
  557. ret = dev_err_probe(dev, PTR_ERR(priv->speaker_en_gpio),
  558. "get speaker GPIO failed\n");
  559. goto err_put_codec;
  560. }
  561. snprintf(components_string, sizeof(components_string),
  562. "cfg-spk:%s cfg-mic:%s",
  563. (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "1" : "2",
  564. mic_name[BYT_CHT_ES8316_MAP(quirk)]);
  565. byt_cht_es8316_card.components = components_string;
  566. #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
  567. snprintf(long_name, sizeof(long_name), "bytcht-es8316-%s-spk-%s-mic",
  568. (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "mono" : "stereo",
  569. mic_name[BYT_CHT_ES8316_MAP(quirk)]);
  570. byt_cht_es8316_card.long_name = long_name;
  571. #endif
  572. sof_parent = snd_soc_acpi_sof_parent(dev);
  573. /* set card and driver name */
  574. if (sof_parent) {
  575. byt_cht_es8316_card.name = SOF_CARD_NAME;
  576. byt_cht_es8316_card.driver_name = SOF_DRIVER_NAME;
  577. } else {
  578. byt_cht_es8316_card.name = CARD_NAME;
  579. byt_cht_es8316_card.driver_name = DRIVER_NAME;
  580. }
  581. /* set pm ops */
  582. if (sof_parent)
  583. dev->driver->pm = &snd_soc_pm_ops;
  584. /* register the soc card */
  585. snd_soc_card_set_drvdata(&byt_cht_es8316_card, priv);
  586. ret = devm_snd_soc_register_card(dev, &byt_cht_es8316_card);
  587. if (ret) {
  588. gpiod_put(priv->speaker_en_gpio);
  589. dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
  590. goto err_put_codec;
  591. }
  592. platform_set_drvdata(pdev, &byt_cht_es8316_card);
  593. return 0;
  594. err_put_codec:
  595. device_remove_software_node(priv->codec_dev);
  596. put_device(priv->codec_dev);
  597. return ret;
  598. }
  599. static void snd_byt_cht_es8316_mc_remove(struct platform_device *pdev)
  600. {
  601. struct snd_soc_card *card = platform_get_drvdata(pdev);
  602. struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
  603. gpiod_put(priv->speaker_en_gpio);
  604. device_remove_software_node(priv->codec_dev);
  605. put_device(priv->codec_dev);
  606. }
  607. static struct platform_driver snd_byt_cht_es8316_mc_driver = {
  608. .driver = {
  609. .name = "bytcht_es8316",
  610. },
  611. .probe = snd_byt_cht_es8316_mc_probe,
  612. .remove = snd_byt_cht_es8316_mc_remove,
  613. };
  614. module_platform_driver(snd_byt_cht_es8316_mc_driver);
  615. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail Machine driver");
  616. MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
  617. MODULE_LICENSE("GPL v2");
  618. MODULE_ALIAS("platform:bytcht_es8316");