cht_bsw_rt5672.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms
  3. * Cherrytrail and Braswell, with RT5672 codec.
  4. *
  5. * Copyright (C) 2014 Intel Corp
  6. * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
  7. * Mengdong Lin <mengdong.lin@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/clk.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/jack.h>
  26. #include <sound/soc-acpi.h>
  27. #include "../../codecs/rt5670.h"
  28. #include "../atom/sst-atom-controls.h"
  29. /* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */
  30. #define CHT_PLAT_CLK_3_HZ 19200000
  31. #define CHT_CODEC_DAI "rt5670-aif1"
  32. struct cht_mc_private {
  33. struct snd_soc_jack headset;
  34. char codec_name[SND_ACPI_I2C_ID_LEN];
  35. struct clk *mclk;
  36. };
  37. /* Headset jack detection DAPM pins */
  38. static struct snd_soc_jack_pin cht_bsw_headset_pins[] = {
  39. {
  40. .pin = "Headset Mic",
  41. .mask = SND_JACK_MICROPHONE,
  42. },
  43. {
  44. .pin = "Headphone",
  45. .mask = SND_JACK_HEADPHONE,
  46. },
  47. };
  48. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  49. struct snd_kcontrol *k, int event)
  50. {
  51. struct snd_soc_dapm_context *dapm = w->dapm;
  52. struct snd_soc_card *card = dapm->card;
  53. struct snd_soc_dai *codec_dai;
  54. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
  55. int ret;
  56. codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI);
  57. if (!codec_dai) {
  58. dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
  59. return -EIO;
  60. }
  61. if (SND_SOC_DAPM_EVENT_ON(event)) {
  62. if (ctx->mclk) {
  63. ret = clk_prepare_enable(ctx->mclk);
  64. if (ret < 0) {
  65. dev_err(card->dev,
  66. "could not configure MCLK state");
  67. return ret;
  68. }
  69. }
  70. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  71. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
  72. CHT_PLAT_CLK_3_HZ, 48000 * 512);
  73. if (ret < 0) {
  74. dev_err(card->dev, "can't set codec pll: %d\n", ret);
  75. return ret;
  76. }
  77. /* set codec sysclk source to PLL */
  78. ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
  79. 48000 * 512, SND_SOC_CLOCK_IN);
  80. if (ret < 0) {
  81. dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
  82. return ret;
  83. }
  84. } else {
  85. /* Set codec sysclk source to its internal clock because codec
  86. * PLL will be off when idle and MCLK will also be off by ACPI
  87. * when codec is runtime suspended. Codec needs clock for jack
  88. * detection and button press.
  89. */
  90. snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK,
  91. 48000 * 512, SND_SOC_CLOCK_IN);
  92. if (ctx->mclk)
  93. clk_disable_unprepare(ctx->mclk);
  94. }
  95. return 0;
  96. }
  97. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  98. SND_SOC_DAPM_HP("Headphone", NULL),
  99. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  100. SND_SOC_DAPM_MIC("Int Mic", NULL),
  101. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  102. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  103. platform_clock_control, SND_SOC_DAPM_PRE_PMU |
  104. SND_SOC_DAPM_POST_PMD),
  105. };
  106. static const struct snd_soc_dapm_route cht_audio_map[] = {
  107. {"IN1P", NULL, "Headset Mic"},
  108. {"IN1N", NULL, "Headset Mic"},
  109. {"DMIC L1", NULL, "Int Mic"},
  110. {"DMIC R1", NULL, "Int Mic"},
  111. {"Headphone", NULL, "HPOL"},
  112. {"Headphone", NULL, "HPOR"},
  113. {"Ext Spk", NULL, "SPOLP"},
  114. {"Ext Spk", NULL, "SPOLN"},
  115. {"Ext Spk", NULL, "SPORP"},
  116. {"Ext Spk", NULL, "SPORN"},
  117. {"AIF1 Playback", NULL, "ssp2 Tx"},
  118. {"ssp2 Tx", NULL, "codec_out0"},
  119. {"ssp2 Tx", NULL, "codec_out1"},
  120. {"codec_in0", NULL, "ssp2 Rx"},
  121. {"codec_in1", NULL, "ssp2 Rx"},
  122. {"ssp2 Rx", NULL, "AIF1 Capture"},
  123. {"Headphone", NULL, "Platform Clock"},
  124. {"Headset Mic", NULL, "Platform Clock"},
  125. {"Int Mic", NULL, "Platform Clock"},
  126. {"Ext Spk", NULL, "Platform Clock"},
  127. };
  128. static const struct snd_kcontrol_new cht_mc_controls[] = {
  129. SOC_DAPM_PIN_SWITCH("Headphone"),
  130. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  131. SOC_DAPM_PIN_SWITCH("Int Mic"),
  132. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  133. };
  134. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  135. struct snd_pcm_hw_params *params)
  136. {
  137. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  138. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  139. int ret;
  140. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  141. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
  142. CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
  143. if (ret < 0) {
  144. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  145. return ret;
  146. }
  147. /* set codec sysclk source to PLL */
  148. ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
  149. params_rate(params) * 512,
  150. SND_SOC_CLOCK_IN);
  151. if (ret < 0) {
  152. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. static const struct acpi_gpio_params headset_gpios = { 0, 0, false };
  158. static const struct acpi_gpio_mapping cht_rt5672_gpios[] = {
  159. { "headset-gpios", &headset_gpios, 1 },
  160. {},
  161. };
  162. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  163. {
  164. int ret;
  165. struct snd_soc_dai *codec_dai = runtime->codec_dai;
  166. struct snd_soc_component *component = codec_dai->component;
  167. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
  168. if (devm_acpi_dev_add_driver_gpios(component->dev, cht_rt5672_gpios))
  169. dev_warn(runtime->dev, "Unable to add GPIO mapping table\n");
  170. /* Select codec ASRC clock source to track I2S1 clock, because codec
  171. * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot
  172. * be supported by RT5672. Otherwise, ASRC will be disabled and cause
  173. * noise.
  174. */
  175. rt5670_sel_asrc_clk_src(component,
  176. RT5670_DA_STEREO_FILTER
  177. | RT5670_DA_MONO_L_FILTER
  178. | RT5670_DA_MONO_R_FILTER
  179. | RT5670_AD_STEREO_FILTER
  180. | RT5670_AD_MONO_L_FILTER
  181. | RT5670_AD_MONO_R_FILTER,
  182. RT5670_CLK_SEL_I2S1_ASRC);
  183. ret = snd_soc_card_jack_new(runtime->card, "Headset",
  184. SND_JACK_HEADSET | SND_JACK_BTN_0 |
  185. SND_JACK_BTN_1 | SND_JACK_BTN_2,
  186. &ctx->headset,
  187. cht_bsw_headset_pins,
  188. ARRAY_SIZE(cht_bsw_headset_pins));
  189. if (ret)
  190. return ret;
  191. rt5670_set_jack_detect(component, &ctx->headset);
  192. if (ctx->mclk) {
  193. /*
  194. * The firmware might enable the clock at
  195. * boot (this information may or may not
  196. * be reflected in the enable clock register).
  197. * To change the rate we must disable the clock
  198. * first to cover these cases. Due to common
  199. * clock framework restrictions that do not allow
  200. * to disable a clock that has not been enabled,
  201. * we need to enable the clock first.
  202. */
  203. ret = clk_prepare_enable(ctx->mclk);
  204. if (!ret)
  205. clk_disable_unprepare(ctx->mclk);
  206. ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
  207. if (ret) {
  208. dev_err(runtime->dev, "unable to set MCLK rate\n");
  209. return ret;
  210. }
  211. }
  212. return 0;
  213. }
  214. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  215. struct snd_pcm_hw_params *params)
  216. {
  217. struct snd_interval *rate = hw_param_interval(params,
  218. SNDRV_PCM_HW_PARAM_RATE);
  219. struct snd_interval *channels = hw_param_interval(params,
  220. SNDRV_PCM_HW_PARAM_CHANNELS);
  221. int ret;
  222. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  223. rate->min = rate->max = 48000;
  224. channels->min = channels->max = 2;
  225. /* set SSP2 to 24-bit */
  226. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  227. /*
  228. * Default mode for SSP configuration is TDM 4 slot
  229. */
  230. ret = snd_soc_dai_set_fmt(rtd->codec_dai,
  231. SND_SOC_DAIFMT_DSP_B |
  232. SND_SOC_DAIFMT_IB_NF |
  233. SND_SOC_DAIFMT_CBS_CFS);
  234. if (ret < 0) {
  235. dev_err(rtd->dev, "can't set format to TDM %d\n", ret);
  236. return ret;
  237. }
  238. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  239. ret = snd_soc_dai_set_tdm_slot(rtd->codec_dai, 0xF, 0xF, 4, 24);
  240. if (ret < 0) {
  241. dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret);
  242. return ret;
  243. }
  244. return 0;
  245. }
  246. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  247. {
  248. return snd_pcm_hw_constraint_single(substream->runtime,
  249. SNDRV_PCM_HW_PARAM_RATE, 48000);
  250. }
  251. static const struct snd_soc_ops cht_aif1_ops = {
  252. .startup = cht_aif1_startup,
  253. };
  254. static const struct snd_soc_ops cht_be_ssp2_ops = {
  255. .hw_params = cht_aif1_hw_params,
  256. };
  257. static struct snd_soc_dai_link cht_dailink[] = {
  258. /* Front End DAI links */
  259. [MERR_DPCM_AUDIO] = {
  260. .name = "Audio Port",
  261. .stream_name = "Audio",
  262. .cpu_dai_name = "media-cpu-dai",
  263. .codec_dai_name = "snd-soc-dummy-dai",
  264. .codec_name = "snd-soc-dummy",
  265. .platform_name = "sst-mfld-platform",
  266. .nonatomic = true,
  267. .dynamic = 1,
  268. .dpcm_playback = 1,
  269. .dpcm_capture = 1,
  270. .ops = &cht_aif1_ops,
  271. },
  272. [MERR_DPCM_DEEP_BUFFER] = {
  273. .name = "Deep-Buffer Audio Port",
  274. .stream_name = "Deep-Buffer Audio",
  275. .cpu_dai_name = "deepbuffer-cpu-dai",
  276. .codec_dai_name = "snd-soc-dummy-dai",
  277. .codec_name = "snd-soc-dummy",
  278. .platform_name = "sst-mfld-platform",
  279. .nonatomic = true,
  280. .dynamic = 1,
  281. .dpcm_playback = 1,
  282. .ops = &cht_aif1_ops,
  283. },
  284. /* Back End DAI links */
  285. {
  286. /* SSP2 - Codec */
  287. .name = "SSP2-Codec",
  288. .id = 0,
  289. .cpu_dai_name = "ssp2-port",
  290. .platform_name = "sst-mfld-platform",
  291. .no_pcm = 1,
  292. .nonatomic = true,
  293. .codec_dai_name = "rt5670-aif1",
  294. .codec_name = "i2c-10EC5670:00",
  295. .init = cht_codec_init,
  296. .be_hw_params_fixup = cht_codec_fixup,
  297. .dpcm_playback = 1,
  298. .dpcm_capture = 1,
  299. .ops = &cht_be_ssp2_ops,
  300. },
  301. };
  302. static int cht_suspend_pre(struct snd_soc_card *card)
  303. {
  304. struct snd_soc_component *component;
  305. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
  306. list_for_each_entry(component, &card->component_dev_list, card_list) {
  307. if (!strncmp(component->name,
  308. ctx->codec_name, sizeof(ctx->codec_name))) {
  309. dev_dbg(component->dev, "disabling jack detect before going to suspend.\n");
  310. rt5670_jack_suspend(component);
  311. break;
  312. }
  313. }
  314. return 0;
  315. }
  316. static int cht_resume_post(struct snd_soc_card *card)
  317. {
  318. struct snd_soc_component *component;
  319. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
  320. list_for_each_entry(component, &card->component_dev_list, card_list) {
  321. if (!strncmp(component->name,
  322. ctx->codec_name, sizeof(ctx->codec_name))) {
  323. dev_dbg(component->dev, "enabling jack detect for resume.\n");
  324. rt5670_jack_resume(component);
  325. break;
  326. }
  327. }
  328. return 0;
  329. }
  330. /* SoC card */
  331. static struct snd_soc_card snd_soc_card_cht = {
  332. .name = "cht-bsw-rt5672",
  333. .owner = THIS_MODULE,
  334. .dai_link = cht_dailink,
  335. .num_links = ARRAY_SIZE(cht_dailink),
  336. .dapm_widgets = cht_dapm_widgets,
  337. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  338. .dapm_routes = cht_audio_map,
  339. .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
  340. .controls = cht_mc_controls,
  341. .num_controls = ARRAY_SIZE(cht_mc_controls),
  342. .suspend_pre = cht_suspend_pre,
  343. .resume_post = cht_resume_post,
  344. };
  345. #define RT5672_I2C_DEFAULT "i2c-10EC5670:00"
  346. static int snd_cht_mc_probe(struct platform_device *pdev)
  347. {
  348. int ret_val = 0;
  349. struct cht_mc_private *drv;
  350. struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
  351. const char *i2c_name;
  352. int i;
  353. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
  354. if (!drv)
  355. return -ENOMEM;
  356. strcpy(drv->codec_name, RT5672_I2C_DEFAULT);
  357. /* fixup codec name based on HID */
  358. if (mach) {
  359. i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
  360. if (i2c_name) {
  361. snprintf(drv->codec_name, sizeof(drv->codec_name),
  362. "i2c-%s", i2c_name);
  363. for (i = 0; i < ARRAY_SIZE(cht_dailink); i++) {
  364. if (!strcmp(cht_dailink[i].codec_name,
  365. RT5672_I2C_DEFAULT)) {
  366. cht_dailink[i].codec_name =
  367. drv->codec_name;
  368. break;
  369. }
  370. }
  371. }
  372. }
  373. drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
  374. if (IS_ERR(drv->mclk)) {
  375. dev_err(&pdev->dev,
  376. "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
  377. PTR_ERR(drv->mclk));
  378. return PTR_ERR(drv->mclk);
  379. }
  380. snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
  381. /* register the soc card */
  382. snd_soc_card_cht.dev = &pdev->dev;
  383. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
  384. if (ret_val) {
  385. dev_err(&pdev->dev,
  386. "snd_soc_register_card failed %d\n", ret_val);
  387. return ret_val;
  388. }
  389. platform_set_drvdata(pdev, &snd_soc_card_cht);
  390. return ret_val;
  391. }
  392. static struct platform_driver snd_cht_mc_driver = {
  393. .driver = {
  394. .name = "cht-bsw-rt5672",
  395. },
  396. .probe = snd_cht_mc_probe,
  397. };
  398. module_platform_driver(snd_cht_mc_driver);
  399. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
  400. MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
  401. MODULE_LICENSE("GPL v2");
  402. MODULE_ALIAS("platform:cht-bsw-rt5672");