bytcr_rt5651.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
  4. * (derived from bytcr_rt5640.c)
  5. *
  6. * Copyright (C) 2015 Intel Corp
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. */
  11. #include <linux/init.h>
  12. #include <linux/i2c.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. #include <linux/acpi.h>
  17. #include <linux/clk.h>
  18. #include <linux/device.h>
  19. #include <linux/dmi.h>
  20. #include <linux/input.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/gpio/machine.h>
  23. #include <linux/slab.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/jack.h>
  28. #include <sound/soc-acpi.h>
  29. #include "../../codecs/rt5651.h"
  30. #include "../atom/sst-atom-controls.h"
  31. #include "../common/soc-intel-quirks.h"
  32. enum {
  33. BYT_RT5651_DMIC_MAP,
  34. BYT_RT5651_IN1_MAP,
  35. BYT_RT5651_IN2_MAP,
  36. BYT_RT5651_IN1_IN2_MAP,
  37. };
  38. enum {
  39. BYT_RT5651_JD_NULL = (RT5651_JD_NULL << 4),
  40. BYT_RT5651_JD1_1 = (RT5651_JD1_1 << 4),
  41. BYT_RT5651_JD1_2 = (RT5651_JD1_2 << 4),
  42. BYT_RT5651_JD2 = (RT5651_JD2 << 4),
  43. };
  44. enum {
  45. BYT_RT5651_OVCD_TH_600UA = (6 << 8),
  46. BYT_RT5651_OVCD_TH_1500UA = (15 << 8),
  47. BYT_RT5651_OVCD_TH_2000UA = (20 << 8),
  48. };
  49. enum {
  50. BYT_RT5651_OVCD_SF_0P5 = (RT5651_OVCD_SF_0P5 << 13),
  51. BYT_RT5651_OVCD_SF_0P75 = (RT5651_OVCD_SF_0P75 << 13),
  52. BYT_RT5651_OVCD_SF_1P0 = (RT5651_OVCD_SF_1P0 << 13),
  53. BYT_RT5651_OVCD_SF_1P5 = (RT5651_OVCD_SF_1P5 << 13),
  54. };
  55. #define BYT_RT5651_MAP(quirk) ((quirk) & GENMASK(3, 0))
  56. #define BYT_RT5651_JDSRC(quirk) (((quirk) & GENMASK(7, 4)) >> 4)
  57. #define BYT_RT5651_OVCD_TH(quirk) (((quirk) & GENMASK(12, 8)) >> 8)
  58. #define BYT_RT5651_OVCD_SF(quirk) (((quirk) & GENMASK(14, 13)) >> 13)
  59. #define BYT_RT5651_DMIC_EN BIT(16)
  60. #define BYT_RT5651_MCLK_EN BIT(17)
  61. #define BYT_RT5651_MCLK_25MHZ BIT(18)
  62. #define BYT_RT5651_SSP2_AIF2 BIT(19) /* default is using AIF1 */
  63. #define BYT_RT5651_SSP0_AIF1 BIT(20)
  64. #define BYT_RT5651_SSP0_AIF2 BIT(21)
  65. #define BYT_RT5651_HP_LR_SWAPPED BIT(22)
  66. #define BYT_RT5651_MONO_SPEAKER BIT(23)
  67. #define BYT_RT5651_JD_NOT_INV BIT(24)
  68. #define BYT_RT5651_DEFAULT_QUIRKS (BYT_RT5651_MCLK_EN | \
  69. BYT_RT5651_JD1_1 | \
  70. BYT_RT5651_OVCD_TH_2000UA | \
  71. BYT_RT5651_OVCD_SF_0P75)
  72. /* jack-detect-source + inv + dmic-en + ovcd-th + -sf + terminating entry */
  73. #define MAX_NO_PROPS 6
  74. struct byt_rt5651_private {
  75. struct clk *mclk;
  76. struct gpio_desc *ext_amp_gpio;
  77. struct gpio_desc *hp_detect;
  78. struct snd_soc_jack jack;
  79. struct device *codec_dev;
  80. };
  81. static const struct acpi_gpio_mapping *byt_rt5651_gpios;
  82. /* Default: jack-detect on JD1_1, internal mic on in2, headsetmic on in3 */
  83. static unsigned long byt_rt5651_quirk = BYT_RT5651_DEFAULT_QUIRKS |
  84. BYT_RT5651_IN2_MAP;
  85. static int quirk_override = -1;
  86. module_param_named(quirk, quirk_override, int, 0444);
  87. MODULE_PARM_DESC(quirk, "Board-specific quirk override");
  88. static void log_quirks(struct device *dev)
  89. {
  90. if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_DMIC_MAP)
  91. dev_info(dev, "quirk DMIC_MAP enabled");
  92. if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_MAP)
  93. dev_info(dev, "quirk IN1_MAP enabled");
  94. if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_MAP)
  95. dev_info(dev, "quirk IN2_MAP enabled");
  96. if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_IN2_MAP)
  97. dev_info(dev, "quirk IN1_IN2_MAP enabled");
  98. if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) {
  99. dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
  100. BYT_RT5651_JDSRC(byt_rt5651_quirk));
  101. dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
  102. BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
  103. dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
  104. BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
  105. }
  106. if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
  107. dev_info(dev, "quirk DMIC enabled");
  108. if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
  109. dev_info(dev, "quirk MCLK_EN enabled");
  110. if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
  111. dev_info(dev, "quirk MCLK_25MHZ enabled");
  112. if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2)
  113. dev_info(dev, "quirk SSP2_AIF2 enabled\n");
  114. if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1)
  115. dev_info(dev, "quirk SSP0_AIF1 enabled\n");
  116. if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)
  117. dev_info(dev, "quirk SSP0_AIF2 enabled\n");
  118. if (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER)
  119. dev_info(dev, "quirk MONO_SPEAKER enabled\n");
  120. if (byt_rt5651_quirk & BYT_RT5651_JD_NOT_INV)
  121. dev_info(dev, "quirk JD_NOT_INV enabled\n");
  122. }
  123. #define BYT_CODEC_DAI1 "rt5651-aif1"
  124. #define BYT_CODEC_DAI2 "rt5651-aif2"
  125. static int byt_rt5651_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
  126. int rate, int bclk_ratio)
  127. {
  128. int clk_id, clk_freq, ret;
  129. /* Configure the PLL before selecting it */
  130. if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) {
  131. clk_id = RT5651_PLL1_S_BCLK1;
  132. clk_freq = rate * bclk_ratio;
  133. } else {
  134. clk_id = RT5651_PLL1_S_MCLK;
  135. if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
  136. clk_freq = 25000000;
  137. else
  138. clk_freq = 19200000;
  139. }
  140. ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, rate * 512);
  141. if (ret < 0) {
  142. dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
  143. return ret;
  144. }
  145. ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
  146. rate * 512, SND_SOC_CLOCK_IN);
  147. if (ret < 0) {
  148. dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  154. struct snd_kcontrol *k, int event)
  155. {
  156. struct snd_soc_dapm_context *dapm = w->dapm;
  157. struct snd_soc_card *card = dapm->card;
  158. struct snd_soc_dai *codec_dai;
  159. struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
  160. int ret;
  161. codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
  162. if (!codec_dai)
  163. codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
  164. if (!codec_dai) {
  165. dev_err(card->dev,
  166. "Codec dai not found; Unable to set platform clock\n");
  167. return -EIO;
  168. }
  169. if (SND_SOC_DAPM_EVENT_ON(event)) {
  170. ret = clk_prepare_enable(priv->mclk);
  171. if (ret < 0) {
  172. dev_err(card->dev, "could not configure MCLK state");
  173. return ret;
  174. }
  175. ret = byt_rt5651_prepare_and_enable_pll1(codec_dai, 48000, 50);
  176. } else {
  177. /*
  178. * Set codec clock source to internal clock before
  179. * turning off the platform clock. Codec needs clock
  180. * for Jack detection and button press
  181. */
  182. ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK,
  183. 48000 * 512,
  184. SND_SOC_CLOCK_IN);
  185. if (!ret)
  186. clk_disable_unprepare(priv->mclk);
  187. }
  188. if (ret < 0) {
  189. dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
  190. return ret;
  191. }
  192. return 0;
  193. }
  194. static int rt5651_ext_amp_power_event(struct snd_soc_dapm_widget *w,
  195. struct snd_kcontrol *kcontrol, int event)
  196. {
  197. struct snd_soc_card *card = w->dapm->card;
  198. struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
  199. if (SND_SOC_DAPM_EVENT_ON(event))
  200. gpiod_set_value_cansleep(priv->ext_amp_gpio, 1);
  201. else
  202. gpiod_set_value_cansleep(priv->ext_amp_gpio, 0);
  203. return 0;
  204. }
  205. static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
  206. SND_SOC_DAPM_HP("Headphone", NULL),
  207. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  208. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  209. SND_SOC_DAPM_SPK("Speaker", NULL),
  210. SND_SOC_DAPM_LINE("Line In", NULL),
  211. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  212. platform_clock_control, SND_SOC_DAPM_PRE_PMU |
  213. SND_SOC_DAPM_POST_PMD),
  214. SND_SOC_DAPM_SUPPLY("Ext Amp Power", SND_SOC_NOPM, 0, 0,
  215. rt5651_ext_amp_power_event,
  216. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  217. };
  218. static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
  219. {"Headphone", NULL, "Platform Clock"},
  220. {"Headset Mic", NULL, "Platform Clock"},
  221. {"Internal Mic", NULL, "Platform Clock"},
  222. {"Speaker", NULL, "Platform Clock"},
  223. {"Speaker", NULL, "Ext Amp Power"},
  224. {"Line In", NULL, "Platform Clock"},
  225. {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
  226. {"Headphone", NULL, "HPOL"},
  227. {"Headphone", NULL, "HPOR"},
  228. {"Speaker", NULL, "LOUTL"},
  229. {"Speaker", NULL, "LOUTR"},
  230. {"IN2P", NULL, "Line In"},
  231. {"IN2N", NULL, "Line In"},
  232. };
  233. static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = {
  234. {"DMIC L1", NULL, "Internal Mic"},
  235. {"DMIC R1", NULL, "Internal Mic"},
  236. {"IN2P", NULL, "Headset Mic"},
  237. };
  238. static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
  239. {"Internal Mic", NULL, "micbias1"},
  240. {"IN1P", NULL, "Internal Mic"},
  241. {"IN3P", NULL, "Headset Mic"},
  242. };
  243. static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = {
  244. {"Internal Mic", NULL, "micbias1"},
  245. {"IN2P", NULL, "Internal Mic"},
  246. {"IN3P", NULL, "Headset Mic"},
  247. };
  248. static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = {
  249. {"Internal Mic", NULL, "micbias1"},
  250. {"IN1P", NULL, "Internal Mic"},
  251. {"IN2P", NULL, "Internal Mic"},
  252. {"IN3P", NULL, "Headset Mic"},
  253. };
  254. static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif1_map[] = {
  255. {"ssp0 Tx", NULL, "modem_out"},
  256. {"modem_in", NULL, "ssp0 Rx"},
  257. {"AIF1 Playback", NULL, "ssp0 Tx"},
  258. {"ssp0 Rx", NULL, "AIF1 Capture"},
  259. };
  260. static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif2_map[] = {
  261. {"ssp0 Tx", NULL, "modem_out"},
  262. {"modem_in", NULL, "ssp0 Rx"},
  263. {"AIF2 Playback", NULL, "ssp0 Tx"},
  264. {"ssp0 Rx", NULL, "AIF2 Capture"},
  265. };
  266. static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif1_map[] = {
  267. {"ssp2 Tx", NULL, "codec_out0"},
  268. {"ssp2 Tx", NULL, "codec_out1"},
  269. {"codec_in0", NULL, "ssp2 Rx"},
  270. {"codec_in1", NULL, "ssp2 Rx"},
  271. {"AIF1 Playback", NULL, "ssp2 Tx"},
  272. {"ssp2 Rx", NULL, "AIF1 Capture"},
  273. };
  274. static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif2_map[] = {
  275. {"ssp2 Tx", NULL, "codec_out0"},
  276. {"ssp2 Tx", NULL, "codec_out1"},
  277. {"codec_in0", NULL, "ssp2 Rx"},
  278. {"codec_in1", NULL, "ssp2 Rx"},
  279. {"AIF2 Playback", NULL, "ssp2 Tx"},
  280. {"ssp2 Rx", NULL, "AIF2 Capture"},
  281. };
  282. static const struct snd_kcontrol_new byt_rt5651_controls[] = {
  283. SOC_DAPM_PIN_SWITCH("Headphone"),
  284. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  285. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  286. SOC_DAPM_PIN_SWITCH("Speaker"),
  287. SOC_DAPM_PIN_SWITCH("Line In"),
  288. };
  289. static struct snd_soc_jack_pin bytcr_jack_pins[] = {
  290. {
  291. .pin = "Headphone",
  292. .mask = SND_JACK_HEADPHONE,
  293. },
  294. {
  295. .pin = "Headset Mic",
  296. .mask = SND_JACK_MICROPHONE,
  297. },
  298. };
  299. static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
  300. struct snd_pcm_hw_params *params)
  301. {
  302. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  303. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
  304. snd_pcm_format_t format = params_format(params);
  305. int rate = params_rate(params);
  306. int bclk_ratio;
  307. if (format == SNDRV_PCM_FORMAT_S16_LE)
  308. bclk_ratio = 32;
  309. else
  310. bclk_ratio = 50;
  311. return byt_rt5651_prepare_and_enable_pll1(codec_dai, rate, bclk_ratio);
  312. }
  313. static const struct acpi_gpio_params pov_p1006w_hp_detect = { 1, 0, false };
  314. static const struct acpi_gpio_params pov_p1006w_ext_amp_en = { 2, 0, true };
  315. static const struct acpi_gpio_mapping byt_rt5651_pov_p1006w_gpios[] = {
  316. { "hp-detect-gpios", &pov_p1006w_hp_detect, 1, },
  317. { "ext-amp-enable-gpios", &pov_p1006w_ext_amp_en, 1, },
  318. { },
  319. };
  320. static int byt_rt5651_pov_p1006w_quirk_cb(const struct dmi_system_id *id)
  321. {
  322. byt_rt5651_quirk = (unsigned long)id->driver_data;
  323. byt_rt5651_gpios = byt_rt5651_pov_p1006w_gpios;
  324. return 1;
  325. }
  326. static int byt_rt5651_quirk_cb(const struct dmi_system_id *id)
  327. {
  328. byt_rt5651_quirk = (unsigned long)id->driver_data;
  329. return 1;
  330. }
  331. static const struct dmi_system_id byt_rt5651_quirk_table[] = {
  332. {
  333. /* Chuwi Hi8 Pro (CWI513) */
  334. .callback = byt_rt5651_quirk_cb,
  335. .matches = {
  336. DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
  337. DMI_MATCH(DMI_PRODUCT_NAME, "X1D3_C806N"),
  338. },
  339. .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
  340. BYT_RT5651_IN2_MAP |
  341. BYT_RT5651_HP_LR_SWAPPED |
  342. BYT_RT5651_MONO_SPEAKER),
  343. },
  344. {
  345. /* Chuwi Vi8 Plus (CWI519) */
  346. .callback = byt_rt5651_quirk_cb,
  347. .matches = {
  348. DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
  349. DMI_MATCH(DMI_PRODUCT_NAME, "D2D3_Vi8A1"),
  350. },
  351. .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
  352. BYT_RT5651_IN2_MAP |
  353. BYT_RT5651_HP_LR_SWAPPED |
  354. BYT_RT5651_MONO_SPEAKER),
  355. },
  356. {
  357. /* Complet Electro Serv MY8307 */
  358. .callback = byt_rt5651_quirk_cb,
  359. .matches = {
  360. DMI_MATCH(DMI_SYS_VENDOR, "Complet Electro Serv"),
  361. DMI_MATCH(DMI_PRODUCT_NAME, "MY8307"),
  362. },
  363. .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
  364. BYT_RT5651_IN2_MAP |
  365. BYT_RT5651_MONO_SPEAKER |
  366. BYT_RT5651_JD_NOT_INV),
  367. },
  368. {
  369. /* I.T.Works TW701, Ployer Momo7w and Trekstor ST70416-6
  370. * (these all use the same mainboard) */
  371. .callback = byt_rt5651_quirk_cb,
  372. .matches = {
  373. DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
  374. /* Partial match for all of itWORKS.G.WI71C.JGBMRBA,
  375. * TREK.G.WI71C.JGBMRBA0x and MOMO.G.WI71C.MABMRBA02 */
  376. DMI_MATCH(DMI_BIOS_VERSION, ".G.WI71C."),
  377. },
  378. .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
  379. BYT_RT5651_IN2_MAP |
  380. BYT_RT5651_SSP0_AIF1 |
  381. BYT_RT5651_MONO_SPEAKER),
  382. },
  383. {
  384. /* Jumper EZpad 7 */
  385. .callback = byt_rt5651_quirk_cb,
  386. .matches = {
  387. DMI_MATCH(DMI_SYS_VENDOR, "Jumper"),
  388. DMI_MATCH(DMI_PRODUCT_NAME, "EZpad"),
  389. /* Jumper12x.WJ2012.bsBKRCP05 with the version dropped */
  390. DMI_MATCH(DMI_BIOS_VERSION, "Jumper12x.WJ2012.bsBKRCP"),
  391. },
  392. .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
  393. BYT_RT5651_IN2_MAP |
  394. BYT_RT5651_JD_NOT_INV),
  395. },
  396. {
  397. /* KIANO SlimNote 14.2 */
  398. .callback = byt_rt5651_quirk_cb,
  399. .matches = {
  400. DMI_MATCH(DMI_SYS_VENDOR, "KIANO"),
  401. DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"),
  402. },
  403. .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
  404. BYT_RT5651_IN1_IN2_MAP),
  405. },
  406. {
  407. /* Minnowboard Max B3 */
  408. .callback = byt_rt5651_quirk_cb,
  409. .matches = {
  410. DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
  411. DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
  412. },
  413. .driver_data = (void *)(BYT_RT5651_IN1_MAP),
  414. },
  415. {
  416. /* Minnowboard Turbot */
  417. .callback = byt_rt5651_quirk_cb,
  418. .matches = {
  419. DMI_MATCH(DMI_SYS_VENDOR, "ADI"),
  420. DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Turbot"),
  421. },
  422. .driver_data = (void *)(BYT_RT5651_MCLK_EN |
  423. BYT_RT5651_IN1_MAP),
  424. },
  425. {
  426. /* Point of View mobii wintab p1006w (v1.0) */
  427. .callback = byt_rt5651_pov_p1006w_quirk_cb,
  428. .matches = {
  429. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
  430. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "BayTrail"),
  431. /* Note 105b is Foxcon's USB/PCI vendor id */
  432. DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "105B"),
  433. DMI_EXACT_MATCH(DMI_BOARD_NAME, "0E57"),
  434. },
  435. .driver_data = (void *)(BYT_RT5651_DMIC_MAP |
  436. BYT_RT5651_OVCD_TH_2000UA |
  437. BYT_RT5651_OVCD_SF_0P75 |
  438. BYT_RT5651_DMIC_EN |
  439. BYT_RT5651_MCLK_EN |
  440. BYT_RT5651_SSP0_AIF1),
  441. },
  442. {
  443. /* VIOS LTH17 */
  444. .callback = byt_rt5651_quirk_cb,
  445. .matches = {
  446. DMI_MATCH(DMI_SYS_VENDOR, "VIOS"),
  447. DMI_MATCH(DMI_PRODUCT_NAME, "LTH17"),
  448. },
  449. .driver_data = (void *)(BYT_RT5651_IN1_IN2_MAP |
  450. BYT_RT5651_JD1_1 |
  451. BYT_RT5651_OVCD_TH_2000UA |
  452. BYT_RT5651_OVCD_SF_1P0 |
  453. BYT_RT5651_MCLK_EN),
  454. },
  455. {
  456. /* Yours Y8W81 (and others using the same mainboard) */
  457. .callback = byt_rt5651_quirk_cb,
  458. .matches = {
  459. DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
  460. /* Partial match for all devs with a W86C mainboard */
  461. DMI_MATCH(DMI_BIOS_VERSION, ".F.W86C."),
  462. },
  463. .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
  464. BYT_RT5651_IN2_MAP |
  465. BYT_RT5651_SSP0_AIF1 |
  466. BYT_RT5651_MONO_SPEAKER),
  467. },
  468. {}
  469. };
  470. /*
  471. * Note this MUST be called before snd_soc_register_card(), so that the props
  472. * are in place before the codec component driver's probe function parses them.
  473. */
  474. static int byt_rt5651_add_codec_device_props(struct device *i2c_dev,
  475. struct byt_rt5651_private *priv)
  476. {
  477. struct property_entry props[MAX_NO_PROPS] = {};
  478. struct fwnode_handle *fwnode;
  479. int cnt = 0;
  480. int ret;
  481. props[cnt++] = PROPERTY_ENTRY_U32("realtek,jack-detect-source",
  482. BYT_RT5651_JDSRC(byt_rt5651_quirk));
  483. props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-threshold-microamp",
  484. BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
  485. props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-scale-factor",
  486. BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
  487. if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
  488. props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,dmic-en");
  489. if (byt_rt5651_quirk & BYT_RT5651_JD_NOT_INV)
  490. props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
  491. fwnode = fwnode_create_software_node(props, NULL);
  492. if (IS_ERR(fwnode)) {
  493. /* put_device(i2c_dev) is handled in caller */
  494. return PTR_ERR(fwnode);
  495. }
  496. ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
  497. fwnode_handle_put(fwnode);
  498. return ret;
  499. }
  500. static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
  501. {
  502. struct snd_soc_card *card = runtime->card;
  503. struct snd_soc_component *codec = snd_soc_rtd_to_codec(runtime, 0)->component;
  504. struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
  505. const struct snd_soc_dapm_route *custom_map;
  506. int num_routes;
  507. int report;
  508. int ret;
  509. card->dapm.idle_bias_off = true;
  510. /* Start with RC clk for jack-detect (we disable MCLK below) */
  511. if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
  512. snd_soc_component_update_bits(codec, RT5651_GLB_CLK,
  513. RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_RCCLK);
  514. switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
  515. case BYT_RT5651_IN1_MAP:
  516. custom_map = byt_rt5651_intmic_in1_map;
  517. num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
  518. break;
  519. case BYT_RT5651_IN2_MAP:
  520. custom_map = byt_rt5651_intmic_in2_map;
  521. num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map);
  522. break;
  523. case BYT_RT5651_IN1_IN2_MAP:
  524. custom_map = byt_rt5651_intmic_in1_in2_map;
  525. num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map);
  526. break;
  527. default:
  528. custom_map = byt_rt5651_intmic_dmic_map;
  529. num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map);
  530. }
  531. ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
  532. if (ret)
  533. return ret;
  534. if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) {
  535. ret = snd_soc_dapm_add_routes(&card->dapm,
  536. byt_rt5651_ssp2_aif2_map,
  537. ARRAY_SIZE(byt_rt5651_ssp2_aif2_map));
  538. } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) {
  539. ret = snd_soc_dapm_add_routes(&card->dapm,
  540. byt_rt5651_ssp0_aif1_map,
  541. ARRAY_SIZE(byt_rt5651_ssp0_aif1_map));
  542. } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) {
  543. ret = snd_soc_dapm_add_routes(&card->dapm,
  544. byt_rt5651_ssp0_aif2_map,
  545. ARRAY_SIZE(byt_rt5651_ssp0_aif2_map));
  546. } else {
  547. ret = snd_soc_dapm_add_routes(&card->dapm,
  548. byt_rt5651_ssp2_aif1_map,
  549. ARRAY_SIZE(byt_rt5651_ssp2_aif1_map));
  550. }
  551. if (ret)
  552. return ret;
  553. ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
  554. ARRAY_SIZE(byt_rt5651_controls));
  555. if (ret) {
  556. dev_err(card->dev, "unable to add card controls\n");
  557. return ret;
  558. }
  559. /*
  560. * The firmware might enable the clock at boot (this information
  561. * may or may not be reflected in the enable clock register).
  562. * To change the rate we must disable the clock first to cover
  563. * these cases. Due to common clock framework restrictions that
  564. * do not allow to disable a clock that has not been enabled,
  565. * we need to enable the clock first.
  566. */
  567. ret = clk_prepare_enable(priv->mclk);
  568. if (!ret)
  569. clk_disable_unprepare(priv->mclk);
  570. if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
  571. ret = clk_set_rate(priv->mclk, 25000000);
  572. else
  573. ret = clk_set_rate(priv->mclk, 19200000);
  574. if (ret)
  575. dev_err(card->dev, "unable to set MCLK rate\n");
  576. report = 0;
  577. if (BYT_RT5651_JDSRC(byt_rt5651_quirk))
  578. report = SND_JACK_HEADSET | SND_JACK_BTN_0;
  579. else if (priv->hp_detect)
  580. report = SND_JACK_HEADSET;
  581. if (report) {
  582. ret = snd_soc_card_jack_new_pins(runtime->card, "Headset",
  583. report, &priv->jack,
  584. bytcr_jack_pins,
  585. ARRAY_SIZE(bytcr_jack_pins));
  586. if (ret) {
  587. dev_err(runtime->dev, "jack creation failed %d\n", ret);
  588. return ret;
  589. }
  590. if (report & SND_JACK_BTN_0)
  591. snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
  592. KEY_PLAYPAUSE);
  593. ret = snd_soc_component_set_jack(codec, &priv->jack,
  594. priv->hp_detect);
  595. if (ret)
  596. return ret;
  597. }
  598. return 0;
  599. }
  600. static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  601. struct snd_pcm_hw_params *params)
  602. {
  603. struct snd_interval *rate = hw_param_interval(params,
  604. SNDRV_PCM_HW_PARAM_RATE);
  605. struct snd_interval *channels = hw_param_interval(params,
  606. SNDRV_PCM_HW_PARAM_CHANNELS);
  607. int ret, bits;
  608. /* The DSP will convert the FE rate to 48k, stereo */
  609. rate->min = rate->max = 48000;
  610. channels->min = channels->max = 2;
  611. if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
  612. (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
  613. /* set SSP0 to 16-bit */
  614. params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
  615. bits = 16;
  616. } else {
  617. /* set SSP2 to 24-bit */
  618. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  619. bits = 24;
  620. }
  621. /*
  622. * Default mode for SSP configuration is TDM 4 slot, override config
  623. * with explicit setting to I2S 2ch. The word length is set with
  624. * dai_set_tdm_slot() since there is no other API exposed
  625. */
  626. ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),
  627. SND_SOC_DAIFMT_I2S |
  628. SND_SOC_DAIFMT_NB_NF |
  629. SND_SOC_DAIFMT_BP_FP
  630. );
  631. if (ret < 0) {
  632. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  633. return ret;
  634. }
  635. ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
  636. if (ret < 0) {
  637. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  638. return ret;
  639. }
  640. return 0;
  641. }
  642. static const unsigned int rates_48000[] = {
  643. 48000,
  644. };
  645. static const struct snd_pcm_hw_constraint_list constraints_48000 = {
  646. .count = ARRAY_SIZE(rates_48000),
  647. .list = rates_48000,
  648. };
  649. static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
  650. {
  651. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  652. SNDRV_PCM_HW_PARAM_RATE,
  653. &constraints_48000);
  654. }
  655. static const struct snd_soc_ops byt_rt5651_aif1_ops = {
  656. .startup = byt_rt5651_aif1_startup,
  657. };
  658. static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
  659. .hw_params = byt_rt5651_aif1_hw_params,
  660. };
  661. SND_SOC_DAILINK_DEF(dummy,
  662. DAILINK_COMP_ARRAY(COMP_DUMMY()));
  663. SND_SOC_DAILINK_DEF(media,
  664. DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
  665. SND_SOC_DAILINK_DEF(deepbuffer,
  666. DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
  667. SND_SOC_DAILINK_DEF(ssp2_port,
  668. DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
  669. SND_SOC_DAILINK_DEF(ssp2_codec,
  670. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5651:00", "rt5651-aif1")));
  671. SND_SOC_DAILINK_DEF(platform,
  672. DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
  673. static struct snd_soc_dai_link byt_rt5651_dais[] = {
  674. [MERR_DPCM_AUDIO] = {
  675. .name = "Audio Port",
  676. .stream_name = "Audio",
  677. .nonatomic = true,
  678. .dynamic = 1,
  679. .dpcm_playback = 1,
  680. .dpcm_capture = 1,
  681. .ops = &byt_rt5651_aif1_ops,
  682. SND_SOC_DAILINK_REG(media, dummy, platform),
  683. },
  684. [MERR_DPCM_DEEP_BUFFER] = {
  685. .name = "Deep-Buffer Audio Port",
  686. .stream_name = "Deep-Buffer Audio",
  687. .nonatomic = true,
  688. .dynamic = 1,
  689. .dpcm_playback = 1,
  690. .ops = &byt_rt5651_aif1_ops,
  691. SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
  692. },
  693. /* CODEC<->CODEC link */
  694. /* back ends */
  695. {
  696. .name = "SSP2-Codec",
  697. .id = 0,
  698. .no_pcm = 1,
  699. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  700. | SND_SOC_DAIFMT_CBC_CFC,
  701. .be_hw_params_fixup = byt_rt5651_codec_fixup,
  702. .dpcm_playback = 1,
  703. .dpcm_capture = 1,
  704. .init = byt_rt5651_init,
  705. .ops = &byt_rt5651_be_ssp2_ops,
  706. SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
  707. },
  708. };
  709. /* SoC card */
  710. static char byt_rt5651_codec_name[SND_ACPI_I2C_ID_LEN];
  711. #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
  712. static char byt_rt5651_long_name[50]; /* = "bytcr-rt5651-*-spk-*-mic[-swapped-hp]" */
  713. #endif
  714. static char byt_rt5651_components[50]; /* = "cfg-spk:* cfg-mic:*" */
  715. static int byt_rt5651_suspend(struct snd_soc_card *card)
  716. {
  717. struct snd_soc_component *component;
  718. if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
  719. return 0;
  720. for_each_card_components(card, component) {
  721. if (!strcmp(component->name, byt_rt5651_codec_name)) {
  722. dev_dbg(component->dev, "disabling jack detect before suspend\n");
  723. snd_soc_component_set_jack(component, NULL, NULL);
  724. break;
  725. }
  726. }
  727. return 0;
  728. }
  729. static int byt_rt5651_resume(struct snd_soc_card *card)
  730. {
  731. struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
  732. struct snd_soc_component *component;
  733. if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
  734. return 0;
  735. for_each_card_components(card, component) {
  736. if (!strcmp(component->name, byt_rt5651_codec_name)) {
  737. dev_dbg(component->dev, "re-enabling jack detect after resume\n");
  738. snd_soc_component_set_jack(component, &priv->jack,
  739. priv->hp_detect);
  740. break;
  741. }
  742. }
  743. return 0;
  744. }
  745. /* use space before codec name to simplify card ID, and simplify driver name */
  746. #define SOF_CARD_NAME "bytcht rt5651" /* card name will be 'sof-bytcht rt5651' */
  747. #define SOF_DRIVER_NAME "SOF"
  748. #define CARD_NAME "bytcr-rt5651"
  749. #define DRIVER_NAME NULL /* card name will be used for driver name */
  750. static struct snd_soc_card byt_rt5651_card = {
  751. .name = CARD_NAME,
  752. .driver_name = DRIVER_NAME,
  753. .owner = THIS_MODULE,
  754. .dai_link = byt_rt5651_dais,
  755. .num_links = ARRAY_SIZE(byt_rt5651_dais),
  756. .dapm_widgets = byt_rt5651_widgets,
  757. .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
  758. .dapm_routes = byt_rt5651_audio_map,
  759. .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
  760. .fully_routed = true,
  761. .suspend_pre = byt_rt5651_suspend,
  762. .resume_post = byt_rt5651_resume,
  763. };
  764. static const struct acpi_gpio_params ext_amp_enable_gpios = { 0, 0, false };
  765. static const struct acpi_gpio_mapping cht_rt5651_gpios[] = {
  766. /*
  767. * Some boards have I2cSerialBusV2, GpioIo, GpioInt as ACPI resources,
  768. * other boards may have I2cSerialBusV2, GpioInt, GpioIo instead.
  769. * We want the GpioIo one for the ext-amp-enable-gpio.
  770. */
  771. { "ext-amp-enable-gpios", &ext_amp_enable_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
  772. { },
  773. };
  774. struct acpi_chan_package { /* ACPICA seems to require 64 bit integers */
  775. u64 aif_value; /* 1: AIF1, 2: AIF2 */
  776. u64 mclock_value; /* usually 25MHz (0x17d7940), ignored */
  777. };
  778. static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
  779. {
  780. struct device *dev = &pdev->dev;
  781. static const char * const mic_name[] = { "dmic", "in1", "in2", "in12" };
  782. struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
  783. struct byt_rt5651_private *priv;
  784. const char *platform_name;
  785. struct acpi_device *adev;
  786. struct device *codec_dev;
  787. bool sof_parent;
  788. bool is_bytcr = false;
  789. int ret_val = 0;
  790. int dai_index = 0;
  791. int i;
  792. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  793. if (!priv)
  794. return -ENOMEM;
  795. /* register the soc card */
  796. byt_rt5651_card.dev = dev;
  797. snd_soc_card_set_drvdata(&byt_rt5651_card, priv);
  798. /* fix index of codec dai */
  799. for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) {
  800. if (byt_rt5651_dais[i].num_codecs &&
  801. !strcmp(byt_rt5651_dais[i].codecs->name,
  802. "i2c-10EC5651:00")) {
  803. dai_index = i;
  804. break;
  805. }
  806. }
  807. /* fixup codec name based on HID */
  808. adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
  809. if (adev) {
  810. snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name),
  811. "i2c-%s", acpi_dev_name(adev));
  812. byt_rt5651_dais[dai_index].codecs->name = byt_rt5651_codec_name;
  813. } else {
  814. dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
  815. return -ENOENT;
  816. }
  817. codec_dev = acpi_get_first_physical_node(adev);
  818. acpi_dev_put(adev);
  819. if (!codec_dev)
  820. return -EPROBE_DEFER;
  821. priv->codec_dev = get_device(codec_dev);
  822. /*
  823. * swap SSP0 if bytcr is detected
  824. * (will be overridden if DMI quirk is detected)
  825. */
  826. if (soc_intel_is_byt()) {
  827. if (mach->mach_params.acpi_ipc_irq_index == 0)
  828. is_bytcr = true;
  829. }
  830. if (is_bytcr) {
  831. /*
  832. * Baytrail CR platforms may have CHAN package in BIOS, try
  833. * to find relevant routing quirk based as done on Windows
  834. * platforms. We have to read the information directly from the
  835. * BIOS, at this stage the card is not created and the links
  836. * with the codec driver/pdata are non-existent
  837. */
  838. struct acpi_chan_package chan_package = { 0 };
  839. /* format specified: 2 64-bit integers */
  840. struct acpi_buffer format = {sizeof("NN"), "NN"};
  841. struct acpi_buffer state = {0, NULL};
  842. struct snd_soc_acpi_package_context pkg_ctx;
  843. bool pkg_found = false;
  844. state.length = sizeof(chan_package);
  845. state.pointer = &chan_package;
  846. pkg_ctx.name = "CHAN";
  847. pkg_ctx.length = 2;
  848. pkg_ctx.format = &format;
  849. pkg_ctx.state = &state;
  850. pkg_ctx.data_valid = false;
  851. pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
  852. &pkg_ctx);
  853. if (pkg_found) {
  854. if (chan_package.aif_value == 1) {
  855. dev_info(dev, "BIOS Routing: AIF1 connected\n");
  856. byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF1;
  857. } else if (chan_package.aif_value == 2) {
  858. dev_info(dev, "BIOS Routing: AIF2 connected\n");
  859. byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
  860. } else {
  861. dev_info(dev, "BIOS Routing isn't valid, ignored\n");
  862. pkg_found = false;
  863. }
  864. }
  865. if (!pkg_found) {
  866. /* no BIOS indications, assume SSP0-AIF2 connection */
  867. byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
  868. }
  869. }
  870. /* check quirks before creating card */
  871. dmi_check_system(byt_rt5651_quirk_table);
  872. if (quirk_override != -1) {
  873. dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
  874. byt_rt5651_quirk, quirk_override);
  875. byt_rt5651_quirk = quirk_override;
  876. }
  877. /* Must be called before register_card, also see declaration comment. */
  878. ret_val = byt_rt5651_add_codec_device_props(codec_dev, priv);
  879. if (ret_val)
  880. goto err_device;
  881. /* Cherry Trail devices use an external amplifier enable gpio */
  882. if (soc_intel_is_cht() && !byt_rt5651_gpios)
  883. byt_rt5651_gpios = cht_rt5651_gpios;
  884. if (byt_rt5651_gpios) {
  885. devm_acpi_dev_add_driver_gpios(codec_dev, byt_rt5651_gpios);
  886. priv->ext_amp_gpio = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
  887. "ext-amp-enable",
  888. GPIOD_OUT_LOW,
  889. "speaker-amp");
  890. if (IS_ERR(priv->ext_amp_gpio)) {
  891. ret_val = PTR_ERR(priv->ext_amp_gpio);
  892. switch (ret_val) {
  893. case -ENOENT:
  894. priv->ext_amp_gpio = NULL;
  895. break;
  896. default:
  897. dev_err(dev, "Failed to get ext-amp-enable GPIO: %d\n", ret_val);
  898. fallthrough;
  899. case -EPROBE_DEFER:
  900. goto err;
  901. }
  902. }
  903. priv->hp_detect = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
  904. "hp-detect",
  905. GPIOD_IN,
  906. "hp-detect");
  907. if (IS_ERR(priv->hp_detect)) {
  908. ret_val = PTR_ERR(priv->hp_detect);
  909. switch (ret_val) {
  910. case -ENOENT:
  911. priv->hp_detect = NULL;
  912. break;
  913. default:
  914. dev_err(dev, "Failed to get hp-detect GPIO: %d\n", ret_val);
  915. fallthrough;
  916. case -EPROBE_DEFER:
  917. goto err;
  918. }
  919. }
  920. }
  921. log_quirks(dev);
  922. if ((byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) ||
  923. (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2))
  924. byt_rt5651_dais[dai_index].codecs->dai_name = "rt5651-aif2";
  925. if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
  926. (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2))
  927. byt_rt5651_dais[dai_index].cpus->dai_name = "ssp0-port";
  928. if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
  929. priv->mclk = devm_clk_get_optional(dev, "pmc_plt_clk_3");
  930. if (IS_ERR(priv->mclk)) {
  931. ret_val = dev_err_probe(dev, PTR_ERR(priv->mclk),
  932. "Failed to get MCLK from pmc_plt_clk_3\n");
  933. goto err;
  934. }
  935. /*
  936. * Fall back to bit clock usage when clock is not
  937. * available likely due to missing dependencies.
  938. */
  939. if (!priv->mclk)
  940. byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN;
  941. }
  942. snprintf(byt_rt5651_components, sizeof(byt_rt5651_components),
  943. "cfg-spk:%s cfg-mic:%s%s",
  944. (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ? "1" : "2",
  945. mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)],
  946. (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED) ?
  947. " cfg-hp:lrswap" : "");
  948. byt_rt5651_card.components = byt_rt5651_components;
  949. #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
  950. snprintf(byt_rt5651_long_name, sizeof(byt_rt5651_long_name),
  951. "bytcr-rt5651-%s-spk-%s-mic%s",
  952. (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ?
  953. "mono" : "stereo",
  954. mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)],
  955. (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED) ?
  956. "-hp-swapped" : "");
  957. byt_rt5651_card.long_name = byt_rt5651_long_name;
  958. #endif
  959. /* override platform name, if required */
  960. platform_name = mach->mach_params.platform;
  961. ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5651_card,
  962. platform_name);
  963. if (ret_val)
  964. goto err;
  965. sof_parent = snd_soc_acpi_sof_parent(dev);
  966. /* set card and driver name */
  967. if (sof_parent) {
  968. byt_rt5651_card.name = SOF_CARD_NAME;
  969. byt_rt5651_card.driver_name = SOF_DRIVER_NAME;
  970. } else {
  971. byt_rt5651_card.name = CARD_NAME;
  972. byt_rt5651_card.driver_name = DRIVER_NAME;
  973. }
  974. /* set pm ops */
  975. if (sof_parent)
  976. dev->driver->pm = &snd_soc_pm_ops;
  977. ret_val = devm_snd_soc_register_card(dev, &byt_rt5651_card);
  978. if (ret_val) {
  979. dev_err(dev, "devm_snd_soc_register_card failed %d\n", ret_val);
  980. goto err;
  981. }
  982. platform_set_drvdata(pdev, &byt_rt5651_card);
  983. return ret_val;
  984. err:
  985. device_remove_software_node(priv->codec_dev);
  986. err_device:
  987. put_device(priv->codec_dev);
  988. return ret_val;
  989. }
  990. static void snd_byt_rt5651_mc_remove(struct platform_device *pdev)
  991. {
  992. struct snd_soc_card *card = platform_get_drvdata(pdev);
  993. struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
  994. device_remove_software_node(priv->codec_dev);
  995. put_device(priv->codec_dev);
  996. }
  997. static struct platform_driver snd_byt_rt5651_mc_driver = {
  998. .driver = {
  999. .name = "bytcr_rt5651",
  1000. },
  1001. .probe = snd_byt_rt5651_mc_probe,
  1002. .remove = snd_byt_rt5651_mc_remove,
  1003. };
  1004. module_platform_driver(snd_byt_rt5651_mc_driver);
  1005. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
  1006. MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
  1007. MODULE_LICENSE("GPL v2");
  1008. MODULE_ALIAS("platform:bytcr_rt5651");