bytcr_rt5640.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
  3. *
  4. * Copyright (C) 2014 Intel Corp
  5. * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/acpi.h>
  23. #include <linux/device.h>
  24. #include <linux/dmi.h>
  25. #include <linux/slab.h>
  26. #include <asm/cpu_device_id.h>
  27. #include <asm/platform_sst_audio.h>
  28. #include <linux/clk.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/soc.h>
  32. #include <sound/jack.h>
  33. #include "../../codecs/rt5640.h"
  34. #include "../atom/sst-atom-controls.h"
  35. #include "../common/sst-acpi.h"
  36. #include "../common/sst-dsp.h"
  37. enum {
  38. BYT_RT5640_DMIC1_MAP,
  39. BYT_RT5640_DMIC2_MAP,
  40. BYT_RT5640_IN1_MAP,
  41. BYT_RT5640_IN3_MAP,
  42. };
  43. #define BYT_RT5640_MAP(quirk) ((quirk) & 0xff)
  44. #define BYT_RT5640_DMIC_EN BIT(16)
  45. #define BYT_RT5640_MONO_SPEAKER BIT(17)
  46. #define BYT_RT5640_DIFF_MIC BIT(18) /* defaut is single-ended */
  47. #define BYT_RT5640_SSP2_AIF2 BIT(19) /* default is using AIF1 */
  48. #define BYT_RT5640_SSP0_AIF1 BIT(20)
  49. #define BYT_RT5640_SSP0_AIF2 BIT(21)
  50. #define BYT_RT5640_MCLK_EN BIT(22)
  51. #define BYT_RT5640_MCLK_25MHZ BIT(23)
  52. struct byt_rt5640_private {
  53. struct clk *mclk;
  54. };
  55. static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
  56. static void log_quirks(struct device *dev)
  57. {
  58. if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_DMIC1_MAP)
  59. dev_info(dev, "quirk DMIC1_MAP enabled");
  60. if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_DMIC2_MAP)
  61. dev_info(dev, "quirk DMIC2_MAP enabled");
  62. if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_IN1_MAP)
  63. dev_info(dev, "quirk IN1_MAP enabled");
  64. if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_IN3_MAP)
  65. dev_info(dev, "quirk IN3_MAP enabled");
  66. if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN)
  67. dev_info(dev, "quirk DMIC enabled");
  68. if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
  69. dev_info(dev, "quirk MONO_SPEAKER enabled");
  70. if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
  71. dev_info(dev, "quirk DIFF_MIC enabled");
  72. if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2)
  73. dev_info(dev, "quirk SSP2_AIF2 enabled");
  74. if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1)
  75. dev_info(dev, "quirk SSP0_AIF1 enabled");
  76. if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)
  77. dev_info(dev, "quirk SSP0_AIF2 enabled");
  78. if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
  79. dev_info(dev, "quirk MCLK_EN enabled");
  80. if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
  81. dev_info(dev, "quirk MCLK_25MHZ enabled");
  82. }
  83. #define BYT_CODEC_DAI1 "rt5640-aif1"
  84. #define BYT_CODEC_DAI2 "rt5640-aif2"
  85. static inline struct snd_soc_dai *byt_get_codec_dai(struct snd_soc_card *card)
  86. {
  87. struct snd_soc_pcm_runtime *rtd;
  88. list_for_each_entry(rtd, &card->rtd_list, list) {
  89. if (!strncmp(rtd->codec_dai->name, BYT_CODEC_DAI1,
  90. strlen(BYT_CODEC_DAI1)))
  91. return rtd->codec_dai;
  92. if (!strncmp(rtd->codec_dai->name, BYT_CODEC_DAI2,
  93. strlen(BYT_CODEC_DAI2)))
  94. return rtd->codec_dai;
  95. }
  96. return NULL;
  97. }
  98. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  99. struct snd_kcontrol *k, int event)
  100. {
  101. struct snd_soc_dapm_context *dapm = w->dapm;
  102. struct snd_soc_card *card = dapm->card;
  103. struct snd_soc_dai *codec_dai;
  104. struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
  105. int ret;
  106. codec_dai = byt_get_codec_dai(card);
  107. if (!codec_dai) {
  108. dev_err(card->dev,
  109. "Codec dai not found; Unable to set platform clock\n");
  110. return -EIO;
  111. }
  112. if (SND_SOC_DAPM_EVENT_ON(event)) {
  113. if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk) {
  114. ret = clk_prepare_enable(priv->mclk);
  115. if (ret < 0) {
  116. dev_err(card->dev,
  117. "could not configure MCLK state");
  118. return ret;
  119. }
  120. }
  121. ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
  122. 48000 * 512,
  123. SND_SOC_CLOCK_IN);
  124. } else {
  125. /*
  126. * Set codec clock source to internal clock before
  127. * turning off the platform clock. Codec needs clock
  128. * for Jack detection and button press
  129. */
  130. ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
  131. 48000 * 512,
  132. SND_SOC_CLOCK_IN);
  133. if (!ret) {
  134. if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk)
  135. clk_disable_unprepare(priv->mclk);
  136. }
  137. }
  138. if (ret < 0) {
  139. dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
  145. SND_SOC_DAPM_HP("Headphone", NULL),
  146. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  147. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  148. SND_SOC_DAPM_SPK("Speaker", NULL),
  149. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  150. platform_clock_control, SND_SOC_DAPM_PRE_PMU |
  151. SND_SOC_DAPM_POST_PMD),
  152. };
  153. static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
  154. {"Headphone", NULL, "Platform Clock"},
  155. {"Headset Mic", NULL, "Platform Clock"},
  156. {"Internal Mic", NULL, "Platform Clock"},
  157. {"Speaker", NULL, "Platform Clock"},
  158. {"Headset Mic", NULL, "MICBIAS1"},
  159. {"IN2P", NULL, "Headset Mic"},
  160. {"Headphone", NULL, "HPOL"},
  161. {"Headphone", NULL, "HPOR"},
  162. };
  163. static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
  164. {"DMIC1", NULL, "Internal Mic"},
  165. };
  166. static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
  167. {"DMIC2", NULL, "Internal Mic"},
  168. };
  169. static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
  170. {"Internal Mic", NULL, "MICBIAS1"},
  171. {"IN1P", NULL, "Internal Mic"},
  172. };
  173. static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
  174. {"Internal Mic", NULL, "MICBIAS1"},
  175. {"IN3P", NULL, "Internal Mic"},
  176. };
  177. static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
  178. {"ssp2 Tx", NULL, "codec_out0"},
  179. {"ssp2 Tx", NULL, "codec_out1"},
  180. {"codec_in0", NULL, "ssp2 Rx"},
  181. {"codec_in1", NULL, "ssp2 Rx"},
  182. {"AIF1 Playback", NULL, "ssp2 Tx"},
  183. {"ssp2 Rx", NULL, "AIF1 Capture"},
  184. };
  185. static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
  186. {"ssp2 Tx", NULL, "codec_out0"},
  187. {"ssp2 Tx", NULL, "codec_out1"},
  188. {"codec_in0", NULL, "ssp2 Rx"},
  189. {"codec_in1", NULL, "ssp2 Rx"},
  190. {"AIF2 Playback", NULL, "ssp2 Tx"},
  191. {"ssp2 Rx", NULL, "AIF2 Capture"},
  192. };
  193. static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
  194. {"ssp0 Tx", NULL, "modem_out"},
  195. {"modem_in", NULL, "ssp0 Rx"},
  196. {"AIF1 Playback", NULL, "ssp0 Tx"},
  197. {"ssp0 Rx", NULL, "AIF1 Capture"},
  198. };
  199. static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
  200. {"ssp0 Tx", NULL, "modem_out"},
  201. {"modem_in", NULL, "ssp0 Rx"},
  202. {"AIF2 Playback", NULL, "ssp0 Tx"},
  203. {"ssp0 Rx", NULL, "AIF2 Capture"},
  204. };
  205. static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
  206. {"Speaker", NULL, "SPOLP"},
  207. {"Speaker", NULL, "SPOLN"},
  208. {"Speaker", NULL, "SPORP"},
  209. {"Speaker", NULL, "SPORN"},
  210. };
  211. static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
  212. {"Speaker", NULL, "SPOLP"},
  213. {"Speaker", NULL, "SPOLN"},
  214. };
  215. static const struct snd_kcontrol_new byt_rt5640_controls[] = {
  216. SOC_DAPM_PIN_SWITCH("Headphone"),
  217. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  218. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  219. SOC_DAPM_PIN_SWITCH("Speaker"),
  220. };
  221. static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
  222. struct snd_pcm_hw_params *params)
  223. {
  224. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  225. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  226. int ret;
  227. ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
  228. params_rate(params) * 512,
  229. SND_SOC_CLOCK_IN);
  230. if (ret < 0) {
  231. dev_err(rtd->dev, "can't set codec clock %d\n", ret);
  232. return ret;
  233. }
  234. if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
  235. /* use bitclock as PLL input */
  236. if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
  237. (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
  238. /* 2x16 bit slots on SSP0 */
  239. ret = snd_soc_dai_set_pll(codec_dai, 0,
  240. RT5640_PLL1_S_BCLK1,
  241. params_rate(params) * 32,
  242. params_rate(params) * 512);
  243. } else {
  244. /* 2x15 bit slots on SSP2 */
  245. ret = snd_soc_dai_set_pll(codec_dai, 0,
  246. RT5640_PLL1_S_BCLK1,
  247. params_rate(params) * 50,
  248. params_rate(params) * 512);
  249. }
  250. } else {
  251. if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
  252. ret = snd_soc_dai_set_pll(codec_dai, 0,
  253. RT5640_PLL1_S_MCLK,
  254. 25000000,
  255. params_rate(params) * 512);
  256. } else {
  257. ret = snd_soc_dai_set_pll(codec_dai, 0,
  258. RT5640_PLL1_S_MCLK,
  259. 19200000,
  260. params_rate(params) * 512);
  261. }
  262. }
  263. if (ret < 0) {
  264. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  265. return ret;
  266. }
  267. return 0;
  268. }
  269. static int byt_rt5640_quirk_cb(const struct dmi_system_id *id)
  270. {
  271. byt_rt5640_quirk = (unsigned long)id->driver_data;
  272. return 1;
  273. }
  274. static const struct dmi_system_id byt_rt5640_quirk_table[] = {
  275. {
  276. .callback = byt_rt5640_quirk_cb,
  277. .matches = {
  278. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  279. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
  280. },
  281. .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
  282. BYT_RT5640_MCLK_EN),
  283. },
  284. {
  285. .callback = byt_rt5640_quirk_cb,
  286. .matches = {
  287. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  288. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
  289. },
  290. .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
  291. BYT_RT5640_MONO_SPEAKER |
  292. BYT_RT5640_DIFF_MIC |
  293. BYT_RT5640_SSP0_AIF2 |
  294. BYT_RT5640_MCLK_EN
  295. ),
  296. },
  297. {
  298. .callback = byt_rt5640_quirk_cb,
  299. .matches = {
  300. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "DellInc."),
  301. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
  302. },
  303. .driver_data = (unsigned long *)(BYT_RT5640_DMIC2_MAP |
  304. BYT_RT5640_DMIC_EN |
  305. BYT_RT5640_MCLK_EN),
  306. },
  307. {
  308. .callback = byt_rt5640_quirk_cb,
  309. .matches = {
  310. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  311. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
  312. },
  313. .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
  314. BYT_RT5640_MCLK_EN),
  315. },
  316. {
  317. .callback = byt_rt5640_quirk_cb,
  318. .matches = {
  319. DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
  320. DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
  321. },
  322. .driver_data = (unsigned long *)(BYT_RT5640_DMIC1_MAP |
  323. BYT_RT5640_DMIC_EN),
  324. },
  325. {
  326. .callback = byt_rt5640_quirk_cb,
  327. .matches = {
  328. DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
  329. DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
  330. },
  331. .driver_data = (unsigned long *)(BYT_RT5640_IN3_MAP |
  332. BYT_RT5640_MCLK_EN |
  333. BYT_RT5640_SSP0_AIF1),
  334. },
  335. {
  336. .callback = byt_rt5640_quirk_cb,
  337. .matches = {
  338. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  339. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
  340. },
  341. .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
  342. BYT_RT5640_MCLK_EN |
  343. BYT_RT5640_SSP0_AIF1),
  344. },
  345. {
  346. .callback = byt_rt5640_quirk_cb,
  347. .matches = {
  348. DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
  349. },
  350. .driver_data = (unsigned long *)(BYT_RT5640_IN3_MAP |
  351. BYT_RT5640_MCLK_EN |
  352. BYT_RT5640_SSP0_AIF1),
  353. },
  354. {}
  355. };
  356. static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
  357. {
  358. int ret;
  359. struct snd_soc_codec *codec = runtime->codec;
  360. struct snd_soc_card *card = runtime->card;
  361. const struct snd_soc_dapm_route *custom_map;
  362. struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
  363. int num_routes;
  364. card->dapm.idle_bias_off = true;
  365. rt5640_sel_asrc_clk_src(codec,
  366. RT5640_DA_STEREO_FILTER |
  367. RT5640_DA_MONO_L_FILTER |
  368. RT5640_DA_MONO_R_FILTER |
  369. RT5640_AD_STEREO_FILTER |
  370. RT5640_AD_MONO_L_FILTER |
  371. RT5640_AD_MONO_R_FILTER,
  372. RT5640_CLK_SEL_ASRC);
  373. ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
  374. ARRAY_SIZE(byt_rt5640_controls));
  375. if (ret) {
  376. dev_err(card->dev, "unable to add card controls\n");
  377. return ret;
  378. }
  379. switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
  380. case BYT_RT5640_IN1_MAP:
  381. custom_map = byt_rt5640_intmic_in1_map;
  382. num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
  383. break;
  384. case BYT_RT5640_IN3_MAP:
  385. custom_map = byt_rt5640_intmic_in3_map;
  386. num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
  387. break;
  388. case BYT_RT5640_DMIC2_MAP:
  389. custom_map = byt_rt5640_intmic_dmic2_map;
  390. num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
  391. break;
  392. default:
  393. custom_map = byt_rt5640_intmic_dmic1_map;
  394. num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
  395. }
  396. ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
  397. if (ret)
  398. return ret;
  399. if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
  400. ret = snd_soc_dapm_add_routes(&card->dapm,
  401. byt_rt5640_ssp2_aif2_map,
  402. ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
  403. } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
  404. ret = snd_soc_dapm_add_routes(&card->dapm,
  405. byt_rt5640_ssp0_aif1_map,
  406. ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
  407. } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
  408. ret = snd_soc_dapm_add_routes(&card->dapm,
  409. byt_rt5640_ssp0_aif2_map,
  410. ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
  411. } else {
  412. ret = snd_soc_dapm_add_routes(&card->dapm,
  413. byt_rt5640_ssp2_aif1_map,
  414. ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
  415. }
  416. if (ret)
  417. return ret;
  418. if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
  419. ret = snd_soc_dapm_add_routes(&card->dapm,
  420. byt_rt5640_mono_spk_map,
  421. ARRAY_SIZE(byt_rt5640_mono_spk_map));
  422. } else {
  423. ret = snd_soc_dapm_add_routes(&card->dapm,
  424. byt_rt5640_stereo_spk_map,
  425. ARRAY_SIZE(byt_rt5640_stereo_spk_map));
  426. }
  427. if (ret)
  428. return ret;
  429. if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC) {
  430. snd_soc_update_bits(codec, RT5640_IN1_IN2, RT5640_IN_DF1,
  431. RT5640_IN_DF1);
  432. }
  433. if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN) {
  434. ret = rt5640_dmic_enable(codec, 0, 0);
  435. if (ret)
  436. return ret;
  437. }
  438. snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
  439. snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
  440. if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk) {
  441. /*
  442. * The firmware might enable the clock at
  443. * boot (this information may or may not
  444. * be reflected in the enable clock register).
  445. * To change the rate we must disable the clock
  446. * first to cover these cases. Due to common
  447. * clock framework restrictions that do not allow
  448. * to disable a clock that has not been enabled,
  449. * we need to enable the clock first.
  450. */
  451. ret = clk_prepare_enable(priv->mclk);
  452. if (!ret)
  453. clk_disable_unprepare(priv->mclk);
  454. if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
  455. ret = clk_set_rate(priv->mclk, 25000000);
  456. else
  457. ret = clk_set_rate(priv->mclk, 19200000);
  458. if (ret)
  459. dev_err(card->dev, "unable to set MCLK rate\n");
  460. }
  461. return ret;
  462. }
  463. static const struct snd_soc_pcm_stream byt_rt5640_dai_params = {
  464. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  465. .rate_min = 48000,
  466. .rate_max = 48000,
  467. .channels_min = 2,
  468. .channels_max = 2,
  469. };
  470. static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  471. struct snd_pcm_hw_params *params)
  472. {
  473. struct snd_interval *rate = hw_param_interval(params,
  474. SNDRV_PCM_HW_PARAM_RATE);
  475. struct snd_interval *channels = hw_param_interval(params,
  476. SNDRV_PCM_HW_PARAM_CHANNELS);
  477. int ret;
  478. /* The DSP will covert the FE rate to 48k, stereo */
  479. rate->min = rate->max = 48000;
  480. channels->min = channels->max = 2;
  481. if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
  482. (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
  483. /* set SSP0 to 16-bit */
  484. params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
  485. /*
  486. * Default mode for SSP configuration is TDM 4 slot, override config
  487. * with explicit setting to I2S 2ch 16-bit. The word length is set with
  488. * dai_set_tdm_slot() since there is no other API exposed
  489. */
  490. ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
  491. SND_SOC_DAIFMT_I2S |
  492. SND_SOC_DAIFMT_NB_IF |
  493. SND_SOC_DAIFMT_CBS_CFS
  494. );
  495. if (ret < 0) {
  496. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  497. return ret;
  498. }
  499. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
  500. if (ret < 0) {
  501. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  502. return ret;
  503. }
  504. } else {
  505. /* set SSP2 to 24-bit */
  506. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  507. /*
  508. * Default mode for SSP configuration is TDM 4 slot, override config
  509. * with explicit setting to I2S 2ch 24-bit. The word length is set with
  510. * dai_set_tdm_slot() since there is no other API exposed
  511. */
  512. ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
  513. SND_SOC_DAIFMT_I2S |
  514. SND_SOC_DAIFMT_NB_IF |
  515. SND_SOC_DAIFMT_CBS_CFS
  516. );
  517. if (ret < 0) {
  518. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  519. return ret;
  520. }
  521. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
  522. if (ret < 0) {
  523. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  524. return ret;
  525. }
  526. }
  527. return 0;
  528. }
  529. static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
  530. {
  531. return snd_pcm_hw_constraint_single(substream->runtime,
  532. SNDRV_PCM_HW_PARAM_RATE, 48000);
  533. }
  534. static struct snd_soc_ops byt_rt5640_aif1_ops = {
  535. .startup = byt_rt5640_aif1_startup,
  536. };
  537. static struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
  538. .hw_params = byt_rt5640_aif1_hw_params,
  539. };
  540. static struct snd_soc_dai_link byt_rt5640_dais[] = {
  541. [MERR_DPCM_AUDIO] = {
  542. .name = "Baytrail Audio Port",
  543. .stream_name = "Baytrail Audio",
  544. .cpu_dai_name = "media-cpu-dai",
  545. .codec_dai_name = "snd-soc-dummy-dai",
  546. .codec_name = "snd-soc-dummy",
  547. .platform_name = "sst-mfld-platform",
  548. .nonatomic = true,
  549. .dynamic = 1,
  550. .dpcm_playback = 1,
  551. .dpcm_capture = 1,
  552. .ops = &byt_rt5640_aif1_ops,
  553. },
  554. [MERR_DPCM_DEEP_BUFFER] = {
  555. .name = "Deep-Buffer Audio Port",
  556. .stream_name = "Deep-Buffer Audio",
  557. .cpu_dai_name = "deepbuffer-cpu-dai",
  558. .codec_dai_name = "snd-soc-dummy-dai",
  559. .codec_name = "snd-soc-dummy",
  560. .platform_name = "sst-mfld-platform",
  561. .nonatomic = true,
  562. .dynamic = 1,
  563. .dpcm_playback = 1,
  564. .ops = &byt_rt5640_aif1_ops,
  565. },
  566. [MERR_DPCM_COMPR] = {
  567. .name = "Baytrail Compressed Port",
  568. .stream_name = "Baytrail Compress",
  569. .cpu_dai_name = "compress-cpu-dai",
  570. .codec_dai_name = "snd-soc-dummy-dai",
  571. .codec_name = "snd-soc-dummy",
  572. .platform_name = "sst-mfld-platform",
  573. },
  574. /* back ends */
  575. {
  576. .name = "SSP2-Codec",
  577. .id = 1,
  578. .cpu_dai_name = "ssp2-port", /* overwritten for ssp0 routing */
  579. .platform_name = "sst-mfld-platform",
  580. .no_pcm = 1,
  581. .codec_dai_name = "rt5640-aif1", /* changed w/ quirk */
  582. .codec_name = "i2c-10EC5640:00", /* overwritten with HID */
  583. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  584. | SND_SOC_DAIFMT_CBS_CFS,
  585. .be_hw_params_fixup = byt_rt5640_codec_fixup,
  586. .ignore_suspend = 1,
  587. .nonatomic = true,
  588. .dpcm_playback = 1,
  589. .dpcm_capture = 1,
  590. .init = byt_rt5640_init,
  591. .ops = &byt_rt5640_be_ssp2_ops,
  592. },
  593. };
  594. /* SoC card */
  595. static struct snd_soc_card byt_rt5640_card = {
  596. .name = "bytcr-rt5640",
  597. .owner = THIS_MODULE,
  598. .dai_link = byt_rt5640_dais,
  599. .num_links = ARRAY_SIZE(byt_rt5640_dais),
  600. .dapm_widgets = byt_rt5640_widgets,
  601. .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
  602. .dapm_routes = byt_rt5640_audio_map,
  603. .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
  604. .fully_routed = true,
  605. };
  606. static char byt_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
  607. static char byt_rt5640_codec_aif_name[12]; /* = "rt5640-aif[1|2]" */
  608. static char byt_rt5640_cpu_dai_name[10]; /* = "ssp[0|2]-port" */
  609. static bool is_valleyview(void)
  610. {
  611. static const struct x86_cpu_id cpu_ids[] = {
  612. { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
  613. {}
  614. };
  615. if (!x86_match_cpu(cpu_ids))
  616. return false;
  617. return true;
  618. }
  619. static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
  620. {
  621. int ret_val = 0;
  622. struct sst_acpi_mach *mach;
  623. const char *i2c_name = NULL;
  624. int i;
  625. int dai_index;
  626. struct byt_rt5640_private *priv;
  627. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_ATOMIC);
  628. if (!priv)
  629. return -ENOMEM;
  630. /* register the soc card */
  631. byt_rt5640_card.dev = &pdev->dev;
  632. mach = byt_rt5640_card.dev->platform_data;
  633. snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
  634. /* fix index of codec dai */
  635. dai_index = MERR_DPCM_COMPR + 1;
  636. for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
  637. if (!strcmp(byt_rt5640_dais[i].codec_name, "i2c-10EC5640:00")) {
  638. dai_index = i;
  639. break;
  640. }
  641. }
  642. /* fixup codec name based on HID */
  643. i2c_name = sst_acpi_find_name_from_hid(mach->id);
  644. if (i2c_name != NULL) {
  645. snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
  646. "%s%s", "i2c-", i2c_name);
  647. byt_rt5640_dais[dai_index].codec_name = byt_rt5640_codec_name;
  648. }
  649. /*
  650. * swap SSP0 if bytcr is detected
  651. * (will be overridden if DMI quirk is detected)
  652. */
  653. if (is_valleyview()) {
  654. struct sst_platform_info *p_info = mach->pdata;
  655. const struct sst_res_info *res_info = p_info->res_info;
  656. /* TODO: use CHAN package info from BIOS to detect AIF1/AIF2 */
  657. if (res_info->acpi_ipc_irq_index == 0) {
  658. byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
  659. }
  660. /* change defaults for Baytrail-CR capture */
  661. byt_rt5640_quirk |= BYT_RT5640_IN1_MAP;
  662. byt_rt5640_quirk |= BYT_RT5640_DIFF_MIC;
  663. } else {
  664. byt_rt5640_quirk |= (BYT_RT5640_DMIC1_MAP |
  665. BYT_RT5640_DMIC_EN);
  666. }
  667. /* check quirks before creating card */
  668. dmi_check_system(byt_rt5640_quirk_table);
  669. log_quirks(&pdev->dev);
  670. if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
  671. (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
  672. /* fixup codec aif name */
  673. snprintf(byt_rt5640_codec_aif_name,
  674. sizeof(byt_rt5640_codec_aif_name),
  675. "%s", "rt5640-aif2");
  676. byt_rt5640_dais[dai_index].codec_dai_name =
  677. byt_rt5640_codec_aif_name;
  678. }
  679. if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
  680. (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
  681. /* fixup cpu dai name name */
  682. snprintf(byt_rt5640_cpu_dai_name,
  683. sizeof(byt_rt5640_cpu_dai_name),
  684. "%s", "ssp0-port");
  685. byt_rt5640_dais[dai_index].cpu_dai_name =
  686. byt_rt5640_cpu_dai_name;
  687. }
  688. if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && (is_valleyview())) {
  689. priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
  690. if (IS_ERR(priv->mclk)) {
  691. dev_err(&pdev->dev,
  692. "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
  693. PTR_ERR(priv->mclk));
  694. return PTR_ERR(priv->mclk);
  695. }
  696. }
  697. ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
  698. if (ret_val) {
  699. dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
  700. ret_val);
  701. return ret_val;
  702. }
  703. platform_set_drvdata(pdev, &byt_rt5640_card);
  704. return ret_val;
  705. }
  706. static struct platform_driver snd_byt_rt5640_mc_driver = {
  707. .driver = {
  708. .name = "bytcr_rt5640",
  709. },
  710. .probe = snd_byt_rt5640_mc_probe,
  711. };
  712. module_platform_driver(snd_byt_rt5640_mc_driver);
  713. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
  714. MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
  715. MODULE_LICENSE("GPL v2");
  716. MODULE_ALIAS("platform:bytcr_rt5640");