cht_bsw_rt5645.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * cht-bsw-rt5645.c - ASoc Machine driver for Intel Cherryview-based platforms
  3. * Cherrytrail and Braswell, with RT5645 codec.
  4. *
  5. * Copyright (C) 2015 Intel Corp
  6. * Author: Fang, Yang A <yang.a.fang@intel.com>
  7. * N,Harshapriya <harshapriya.n@intel.com>
  8. * This file is modified from cht_bsw_rt5672.c
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. */
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/acpi.h>
  25. #include <linux/clk.h>
  26. #include <linux/dmi.h>
  27. #include <linux/slab.h>
  28. #include <asm/cpu_device_id.h>
  29. #include <asm/platform_sst_audio.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include <sound/jack.h>
  34. #include <sound/soc-acpi.h>
  35. #include "../../codecs/rt5645.h"
  36. #include "../atom/sst-atom-controls.h"
  37. #define CHT_PLAT_CLK_3_HZ 19200000
  38. #define CHT_CODEC_DAI1 "rt5645-aif1"
  39. #define CHT_CODEC_DAI2 "rt5645-aif2"
  40. struct cht_acpi_card {
  41. char *codec_id;
  42. int codec_type;
  43. struct snd_soc_card *soc_card;
  44. };
  45. struct cht_mc_private {
  46. struct snd_soc_jack jack;
  47. struct cht_acpi_card *acpi_card;
  48. char codec_name[SND_ACPI_I2C_ID_LEN];
  49. struct clk *mclk;
  50. };
  51. #define CHT_RT5645_MAP(quirk) ((quirk) & GENMASK(7, 0))
  52. #define CHT_RT5645_SSP2_AIF2 BIT(16) /* default is using AIF1 */
  53. #define CHT_RT5645_SSP0_AIF1 BIT(17)
  54. #define CHT_RT5645_SSP0_AIF2 BIT(18)
  55. static unsigned long cht_rt5645_quirk = 0;
  56. static void log_quirks(struct device *dev)
  57. {
  58. if (cht_rt5645_quirk & CHT_RT5645_SSP2_AIF2)
  59. dev_info(dev, "quirk SSP2_AIF2 enabled");
  60. if (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF1)
  61. dev_info(dev, "quirk SSP0_AIF1 enabled");
  62. if (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)
  63. dev_info(dev, "quirk SSP0_AIF2 enabled");
  64. }
  65. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  66. struct snd_kcontrol *k, int event)
  67. {
  68. struct snd_soc_dapm_context *dapm = w->dapm;
  69. struct snd_soc_card *card = dapm->card;
  70. struct snd_soc_dai *codec_dai;
  71. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
  72. int ret;
  73. codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI1);
  74. if (!codec_dai)
  75. codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI2);
  76. if (!codec_dai) {
  77. dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
  78. return -EIO;
  79. }
  80. if (SND_SOC_DAPM_EVENT_ON(event)) {
  81. ret = clk_prepare_enable(ctx->mclk);
  82. if (ret < 0) {
  83. dev_err(card->dev,
  84. "could not configure MCLK state");
  85. return ret;
  86. }
  87. } else {
  88. /* Set codec sysclk source to its internal clock because codec PLL will
  89. * be off when idle and MCLK will also be off when codec is
  90. * runtime suspended. Codec needs clock for jack detection and button
  91. * press. MCLK is turned off with clock framework or ACPI.
  92. */
  93. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
  94. 48000 * 512, SND_SOC_CLOCK_IN);
  95. if (ret < 0) {
  96. dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
  97. return ret;
  98. }
  99. clk_disable_unprepare(ctx->mclk);
  100. }
  101. return 0;
  102. }
  103. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  104. SND_SOC_DAPM_HP("Headphone", NULL),
  105. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  106. SND_SOC_DAPM_MIC("Int Mic", NULL),
  107. SND_SOC_DAPM_MIC("Int Analog Mic", NULL),
  108. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  109. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  110. platform_clock_control, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  111. };
  112. static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
  113. {"IN1P", NULL, "Headset Mic"},
  114. {"IN1N", NULL, "Headset Mic"},
  115. {"DMIC L1", NULL, "Int Mic"},
  116. {"DMIC R1", NULL, "Int Mic"},
  117. {"IN2P", NULL, "Int Analog Mic"},
  118. {"IN2N", NULL, "Int Analog Mic"},
  119. {"Headphone", NULL, "HPOL"},
  120. {"Headphone", NULL, "HPOR"},
  121. {"Ext Spk", NULL, "SPOL"},
  122. {"Ext Spk", NULL, "SPOR"},
  123. {"Headphone", NULL, "Platform Clock"},
  124. {"Headset Mic", NULL, "Platform Clock"},
  125. {"Int Mic", NULL, "Platform Clock"},
  126. {"Int Analog Mic", NULL, "Platform Clock"},
  127. {"Int Analog Mic", NULL, "micbias1"},
  128. {"Int Analog Mic", NULL, "micbias2"},
  129. {"Ext Spk", NULL, "Platform Clock"},
  130. };
  131. static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
  132. {"IN1P", NULL, "Headset Mic"},
  133. {"IN1N", NULL, "Headset Mic"},
  134. {"DMIC L2", NULL, "Int Mic"},
  135. {"DMIC R2", NULL, "Int Mic"},
  136. {"Headphone", NULL, "HPOL"},
  137. {"Headphone", NULL, "HPOR"},
  138. {"Ext Spk", NULL, "SPOL"},
  139. {"Ext Spk", NULL, "SPOR"},
  140. {"Headphone", NULL, "Platform Clock"},
  141. {"Headset Mic", NULL, "Platform Clock"},
  142. {"Int Mic", NULL, "Platform Clock"},
  143. {"Ext Spk", NULL, "Platform Clock"},
  144. };
  145. static const struct snd_soc_dapm_route cht_rt5645_ssp2_aif1_map[] = {
  146. {"AIF1 Playback", NULL, "ssp2 Tx"},
  147. {"ssp2 Tx", NULL, "codec_out0"},
  148. {"ssp2 Tx", NULL, "codec_out1"},
  149. {"codec_in0", NULL, "ssp2 Rx" },
  150. {"codec_in1", NULL, "ssp2 Rx" },
  151. {"ssp2 Rx", NULL, "AIF1 Capture"},
  152. };
  153. static const struct snd_soc_dapm_route cht_rt5645_ssp2_aif2_map[] = {
  154. {"AIF2 Playback", NULL, "ssp2 Tx"},
  155. {"ssp2 Tx", NULL, "codec_out0"},
  156. {"ssp2 Tx", NULL, "codec_out1"},
  157. {"codec_in0", NULL, "ssp2 Rx" },
  158. {"codec_in1", NULL, "ssp2 Rx" },
  159. {"ssp2 Rx", NULL, "AIF2 Capture"},
  160. };
  161. static const struct snd_soc_dapm_route cht_rt5645_ssp0_aif1_map[] = {
  162. {"AIF1 Playback", NULL, "ssp0 Tx"},
  163. {"ssp0 Tx", NULL, "modem_out"},
  164. {"modem_in", NULL, "ssp0 Rx" },
  165. {"ssp0 Rx", NULL, "AIF1 Capture"},
  166. };
  167. static const struct snd_soc_dapm_route cht_rt5645_ssp0_aif2_map[] = {
  168. {"AIF2 Playback", NULL, "ssp0 Tx"},
  169. {"ssp0 Tx", NULL, "modem_out"},
  170. {"modem_in", NULL, "ssp0 Rx" },
  171. {"ssp0 Rx", NULL, "AIF2 Capture"},
  172. };
  173. static const struct snd_kcontrol_new cht_mc_controls[] = {
  174. SOC_DAPM_PIN_SWITCH("Headphone"),
  175. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  176. SOC_DAPM_PIN_SWITCH("Int Mic"),
  177. SOC_DAPM_PIN_SWITCH("Int Analog Mic"),
  178. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  179. };
  180. static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
  181. {
  182. .pin = "Headphone",
  183. .mask = SND_JACK_HEADPHONE,
  184. },
  185. {
  186. .pin = "Headset Mic",
  187. .mask = SND_JACK_MICROPHONE,
  188. },
  189. };
  190. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  191. struct snd_pcm_hw_params *params)
  192. {
  193. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  194. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  195. int ret;
  196. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  197. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
  198. CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
  199. if (ret < 0) {
  200. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  201. return ret;
  202. }
  203. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
  204. params_rate(params) * 512, SND_SOC_CLOCK_IN);
  205. if (ret < 0) {
  206. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  207. return ret;
  208. }
  209. return 0;
  210. }
  211. /* uncomment when we have a real quirk
  212. static int cht_rt5645_quirk_cb(const struct dmi_system_id *id)
  213. {
  214. cht_rt5645_quirk = (unsigned long)id->driver_data;
  215. return 1;
  216. }
  217. */
  218. static const struct dmi_system_id cht_rt5645_quirk_table[] = {
  219. {
  220. },
  221. };
  222. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  223. {
  224. struct snd_soc_card *card = runtime->card;
  225. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
  226. struct snd_soc_component *component = runtime->codec_dai->component;
  227. int jack_type;
  228. int ret;
  229. if ((cht_rt5645_quirk & CHT_RT5645_SSP2_AIF2) ||
  230. (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)) {
  231. /* Select clk_i2s2_asrc as ASRC clock source */
  232. rt5645_sel_asrc_clk_src(component,
  233. RT5645_DA_STEREO_FILTER |
  234. RT5645_DA_MONO_L_FILTER |
  235. RT5645_DA_MONO_R_FILTER |
  236. RT5645_AD_STEREO_FILTER,
  237. RT5645_CLK_SEL_I2S2_ASRC);
  238. } else {
  239. /* Select clk_i2s1_asrc as ASRC clock source */
  240. rt5645_sel_asrc_clk_src(component,
  241. RT5645_DA_STEREO_FILTER |
  242. RT5645_DA_MONO_L_FILTER |
  243. RT5645_DA_MONO_R_FILTER |
  244. RT5645_AD_STEREO_FILTER,
  245. RT5645_CLK_SEL_I2S1_ASRC);
  246. }
  247. if (cht_rt5645_quirk & CHT_RT5645_SSP2_AIF2) {
  248. ret = snd_soc_dapm_add_routes(&card->dapm,
  249. cht_rt5645_ssp2_aif2_map,
  250. ARRAY_SIZE(cht_rt5645_ssp2_aif2_map));
  251. } else if (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF1) {
  252. ret = snd_soc_dapm_add_routes(&card->dapm,
  253. cht_rt5645_ssp0_aif1_map,
  254. ARRAY_SIZE(cht_rt5645_ssp0_aif1_map));
  255. } else if (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2) {
  256. ret = snd_soc_dapm_add_routes(&card->dapm,
  257. cht_rt5645_ssp0_aif2_map,
  258. ARRAY_SIZE(cht_rt5645_ssp0_aif2_map));
  259. } else {
  260. ret = snd_soc_dapm_add_routes(&card->dapm,
  261. cht_rt5645_ssp2_aif1_map,
  262. ARRAY_SIZE(cht_rt5645_ssp2_aif1_map));
  263. }
  264. if (ret)
  265. return ret;
  266. if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
  267. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  268. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  269. SND_JACK_BTN_2 | SND_JACK_BTN_3;
  270. else
  271. jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
  272. ret = snd_soc_card_jack_new(runtime->card, "Headset",
  273. jack_type, &ctx->jack,
  274. cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
  275. if (ret) {
  276. dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
  277. return ret;
  278. }
  279. rt5645_set_jack_detect(component, &ctx->jack, &ctx->jack, &ctx->jack);
  280. /*
  281. * The firmware might enable the clock at
  282. * boot (this information may or may not
  283. * be reflected in the enable clock register).
  284. * To change the rate we must disable the clock
  285. * first to cover these cases. Due to common
  286. * clock framework restrictions that do not allow
  287. * to disable a clock that has not been enabled,
  288. * we need to enable the clock first.
  289. */
  290. ret = clk_prepare_enable(ctx->mclk);
  291. if (!ret)
  292. clk_disable_unprepare(ctx->mclk);
  293. ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
  294. if (ret)
  295. dev_err(runtime->dev, "unable to set MCLK rate\n");
  296. return ret;
  297. }
  298. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  299. struct snd_pcm_hw_params *params)
  300. {
  301. int ret;
  302. struct snd_interval *rate = hw_param_interval(params,
  303. SNDRV_PCM_HW_PARAM_RATE);
  304. struct snd_interval *channels = hw_param_interval(params,
  305. SNDRV_PCM_HW_PARAM_CHANNELS);
  306. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  307. rate->min = rate->max = 48000;
  308. channels->min = channels->max = 2;
  309. if ((cht_rt5645_quirk & CHT_RT5645_SSP0_AIF1) ||
  310. (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)) {
  311. /* set SSP0 to 16-bit */
  312. params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
  313. /*
  314. * Default mode for SSP configuration is TDM 4 slot, override config
  315. * with explicit setting to I2S 2ch 16-bit. The word length is set with
  316. * dai_set_tdm_slot() since there is no other API exposed
  317. */
  318. ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
  319. SND_SOC_DAIFMT_I2S |
  320. SND_SOC_DAIFMT_NB_NF |
  321. SND_SOC_DAIFMT_CBS_CFS
  322. );
  323. if (ret < 0) {
  324. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  325. return ret;
  326. }
  327. ret = snd_soc_dai_set_fmt(rtd->codec_dai,
  328. SND_SOC_DAIFMT_I2S |
  329. SND_SOC_DAIFMT_NB_NF |
  330. SND_SOC_DAIFMT_CBS_CFS
  331. );
  332. if (ret < 0) {
  333. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  334. return ret;
  335. }
  336. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
  337. if (ret < 0) {
  338. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  339. return ret;
  340. }
  341. } else {
  342. /* set SSP2 to 24-bit */
  343. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  344. /*
  345. * Default mode for SSP configuration is TDM 4 slot
  346. */
  347. ret = snd_soc_dai_set_fmt(rtd->codec_dai,
  348. SND_SOC_DAIFMT_DSP_B |
  349. SND_SOC_DAIFMT_IB_NF |
  350. SND_SOC_DAIFMT_CBS_CFS);
  351. if (ret < 0) {
  352. dev_err(rtd->dev, "can't set format to TDM %d\n", ret);
  353. return ret;
  354. }
  355. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  356. ret = snd_soc_dai_set_tdm_slot(rtd->codec_dai, 0xF, 0xF, 4, 24);
  357. if (ret < 0) {
  358. dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret);
  359. return ret;
  360. }
  361. }
  362. return 0;
  363. }
  364. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  365. {
  366. return snd_pcm_hw_constraint_single(substream->runtime,
  367. SNDRV_PCM_HW_PARAM_RATE, 48000);
  368. }
  369. static const struct snd_soc_ops cht_aif1_ops = {
  370. .startup = cht_aif1_startup,
  371. };
  372. static const struct snd_soc_ops cht_be_ssp2_ops = {
  373. .hw_params = cht_aif1_hw_params,
  374. };
  375. static struct snd_soc_dai_link cht_dailink[] = {
  376. [MERR_DPCM_AUDIO] = {
  377. .name = "Audio Port",
  378. .stream_name = "Audio",
  379. .cpu_dai_name = "media-cpu-dai",
  380. .codec_dai_name = "snd-soc-dummy-dai",
  381. .codec_name = "snd-soc-dummy",
  382. .platform_name = "sst-mfld-platform",
  383. .nonatomic = true,
  384. .dynamic = 1,
  385. .dpcm_playback = 1,
  386. .dpcm_capture = 1,
  387. .ops = &cht_aif1_ops,
  388. },
  389. [MERR_DPCM_DEEP_BUFFER] = {
  390. .name = "Deep-Buffer Audio Port",
  391. .stream_name = "Deep-Buffer Audio",
  392. .cpu_dai_name = "deepbuffer-cpu-dai",
  393. .codec_dai_name = "snd-soc-dummy-dai",
  394. .codec_name = "snd-soc-dummy",
  395. .platform_name = "sst-mfld-platform",
  396. .nonatomic = true,
  397. .dynamic = 1,
  398. .dpcm_playback = 1,
  399. .ops = &cht_aif1_ops,
  400. },
  401. /* CODEC<->CODEC link */
  402. /* back ends */
  403. {
  404. .name = "SSP2-Codec",
  405. .id = 0,
  406. .cpu_dai_name = "ssp2-port",
  407. .platform_name = "sst-mfld-platform",
  408. .no_pcm = 1,
  409. .codec_dai_name = "rt5645-aif1",
  410. .codec_name = "i2c-10EC5645:00",
  411. .init = cht_codec_init,
  412. .be_hw_params_fixup = cht_codec_fixup,
  413. .nonatomic = true,
  414. .dpcm_playback = 1,
  415. .dpcm_capture = 1,
  416. .ops = &cht_be_ssp2_ops,
  417. },
  418. };
  419. /* SoC card */
  420. static struct snd_soc_card snd_soc_card_chtrt5645 = {
  421. .name = "chtrt5645",
  422. .owner = THIS_MODULE,
  423. .dai_link = cht_dailink,
  424. .num_links = ARRAY_SIZE(cht_dailink),
  425. .dapm_widgets = cht_dapm_widgets,
  426. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  427. .dapm_routes = cht_rt5645_audio_map,
  428. .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
  429. .controls = cht_mc_controls,
  430. .num_controls = ARRAY_SIZE(cht_mc_controls),
  431. };
  432. static struct snd_soc_card snd_soc_card_chtrt5650 = {
  433. .name = "chtrt5650",
  434. .owner = THIS_MODULE,
  435. .dai_link = cht_dailink,
  436. .num_links = ARRAY_SIZE(cht_dailink),
  437. .dapm_widgets = cht_dapm_widgets,
  438. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  439. .dapm_routes = cht_rt5650_audio_map,
  440. .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
  441. .controls = cht_mc_controls,
  442. .num_controls = ARRAY_SIZE(cht_mc_controls),
  443. };
  444. static struct cht_acpi_card snd_soc_cards[] = {
  445. {"10EC5640", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
  446. {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
  447. {"10EC5648", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
  448. {"10EC3270", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
  449. {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
  450. };
  451. static char cht_rt5645_codec_name[SND_ACPI_I2C_ID_LEN];
  452. static char cht_rt5645_codec_aif_name[12]; /* = "rt5645-aif[1|2]" */
  453. static char cht_rt5645_cpu_dai_name[10]; /* = "ssp[0|2]-port" */
  454. static bool is_valleyview(void)
  455. {
  456. static const struct x86_cpu_id cpu_ids[] = {
  457. { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
  458. {}
  459. };
  460. if (!x86_match_cpu(cpu_ids))
  461. return false;
  462. return true;
  463. }
  464. struct acpi_chan_package { /* ACPICA seems to require 64 bit integers */
  465. u64 aif_value; /* 1: AIF1, 2: AIF2 */
  466. u64 mclock_value; /* usually 25MHz (0x17d7940), ignored */
  467. };
  468. static int snd_cht_mc_probe(struct platform_device *pdev)
  469. {
  470. struct snd_soc_card *card = snd_soc_cards[0].soc_card;
  471. struct snd_soc_acpi_mach *mach;
  472. struct cht_mc_private *drv;
  473. const char *i2c_name = NULL;
  474. bool found = false;
  475. bool is_bytcr = false;
  476. int dai_index = 0;
  477. int ret_val = 0;
  478. int i;
  479. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
  480. if (!drv)
  481. return -ENOMEM;
  482. mach = (&pdev->dev)->platform_data;
  483. for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
  484. if (acpi_dev_found(snd_soc_cards[i].codec_id) &&
  485. (!strncmp(snd_soc_cards[i].codec_id, mach->id, 8))) {
  486. dev_dbg(&pdev->dev,
  487. "found codec %s\n", snd_soc_cards[i].codec_id);
  488. card = snd_soc_cards[i].soc_card;
  489. drv->acpi_card = &snd_soc_cards[i];
  490. found = true;
  491. break;
  492. }
  493. }
  494. if (!found) {
  495. dev_err(&pdev->dev, "No matching HID found in supported list\n");
  496. return -ENODEV;
  497. }
  498. card->dev = &pdev->dev;
  499. sprintf(drv->codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
  500. /* set correct codec name */
  501. for (i = 0; i < ARRAY_SIZE(cht_dailink); i++)
  502. if (!strcmp(card->dai_link[i].codec_name, "i2c-10EC5645:00")) {
  503. card->dai_link[i].codec_name = drv->codec_name;
  504. dai_index = i;
  505. }
  506. /* fixup codec name based on HID */
  507. i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
  508. if (i2c_name) {
  509. snprintf(cht_rt5645_codec_name, sizeof(cht_rt5645_codec_name),
  510. "%s%s", "i2c-", i2c_name);
  511. cht_dailink[dai_index].codec_name = cht_rt5645_codec_name;
  512. }
  513. /*
  514. * swap SSP0 if bytcr is detected
  515. * (will be overridden if DMI quirk is detected)
  516. */
  517. if (is_valleyview()) {
  518. struct sst_platform_info *p_info = mach->pdata;
  519. const struct sst_res_info *res_info = p_info->res_info;
  520. if (res_info->acpi_ipc_irq_index == 0)
  521. is_bytcr = true;
  522. }
  523. if (is_bytcr) {
  524. /*
  525. * Baytrail CR platforms may have CHAN package in BIOS, try
  526. * to find relevant routing quirk based as done on Windows
  527. * platforms. We have to read the information directly from the
  528. * BIOS, at this stage the card is not created and the links
  529. * with the codec driver/pdata are non-existent
  530. */
  531. struct acpi_chan_package chan_package;
  532. /* format specified: 2 64-bit integers */
  533. struct acpi_buffer format = {sizeof("NN"), "NN"};
  534. struct acpi_buffer state = {0, NULL};
  535. struct snd_soc_acpi_package_context pkg_ctx;
  536. bool pkg_found = false;
  537. state.length = sizeof(chan_package);
  538. state.pointer = &chan_package;
  539. pkg_ctx.name = "CHAN";
  540. pkg_ctx.length = 2;
  541. pkg_ctx.format = &format;
  542. pkg_ctx.state = &state;
  543. pkg_ctx.data_valid = false;
  544. pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
  545. &pkg_ctx);
  546. if (pkg_found) {
  547. if (chan_package.aif_value == 1) {
  548. dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
  549. cht_rt5645_quirk |= CHT_RT5645_SSP0_AIF1;
  550. } else if (chan_package.aif_value == 2) {
  551. dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
  552. cht_rt5645_quirk |= CHT_RT5645_SSP0_AIF2;
  553. } else {
  554. dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
  555. pkg_found = false;
  556. }
  557. }
  558. if (!pkg_found) {
  559. /* no BIOS indications, assume SSP0-AIF2 connection */
  560. cht_rt5645_quirk |= CHT_RT5645_SSP0_AIF2;
  561. }
  562. }
  563. /* check quirks before creating card */
  564. dmi_check_system(cht_rt5645_quirk_table);
  565. log_quirks(&pdev->dev);
  566. if ((cht_rt5645_quirk & CHT_RT5645_SSP2_AIF2) ||
  567. (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)) {
  568. /* fixup codec aif name */
  569. snprintf(cht_rt5645_codec_aif_name,
  570. sizeof(cht_rt5645_codec_aif_name),
  571. "%s", "rt5645-aif2");
  572. cht_dailink[dai_index].codec_dai_name =
  573. cht_rt5645_codec_aif_name;
  574. }
  575. if ((cht_rt5645_quirk & CHT_RT5645_SSP0_AIF1) ||
  576. (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)) {
  577. /* fixup cpu dai name name */
  578. snprintf(cht_rt5645_cpu_dai_name,
  579. sizeof(cht_rt5645_cpu_dai_name),
  580. "%s", "ssp0-port");
  581. cht_dailink[dai_index].cpu_dai_name =
  582. cht_rt5645_cpu_dai_name;
  583. }
  584. drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
  585. if (IS_ERR(drv->mclk)) {
  586. dev_err(&pdev->dev,
  587. "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
  588. PTR_ERR(drv->mclk));
  589. return PTR_ERR(drv->mclk);
  590. }
  591. snd_soc_card_set_drvdata(card, drv);
  592. ret_val = devm_snd_soc_register_card(&pdev->dev, card);
  593. if (ret_val) {
  594. dev_err(&pdev->dev,
  595. "snd_soc_register_card failed %d\n", ret_val);
  596. return ret_val;
  597. }
  598. platform_set_drvdata(pdev, card);
  599. return ret_val;
  600. }
  601. static struct platform_driver snd_cht_mc_driver = {
  602. .driver = {
  603. .name = "cht-bsw-rt5645",
  604. },
  605. .probe = snd_cht_mc_probe,
  606. };
  607. module_platform_driver(snd_cht_mc_driver)
  608. MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
  609. MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
  610. MODULE_LICENSE("GPL v2");
  611. MODULE_ALIAS("platform:cht-bsw-rt5645");