cht_bsw_rt5672.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/jack.h>
  25. #include "../../codecs/rt5670.h"
  26. #include "../atom/sst-atom-controls.h"
  27. /* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */
  28. #define CHT_PLAT_CLK_3_HZ 19200000
  29. #define CHT_CODEC_DAI "rt5670-aif1"
  30. static struct snd_soc_jack cht_bsw_headset;
  31. /* Headset jack detection DAPM pins */
  32. static struct snd_soc_jack_pin cht_bsw_headset_pins[] = {
  33. {
  34. .pin = "Headset Mic",
  35. .mask = SND_JACK_MICROPHONE,
  36. },
  37. {
  38. .pin = "Headphone",
  39. .mask = SND_JACK_HEADPHONE,
  40. },
  41. };
  42. static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
  43. {
  44. int i;
  45. for (i = 0; i < card->num_rtd; i++) {
  46. struct snd_soc_pcm_runtime *rtd;
  47. rtd = card->rtd + i;
  48. if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
  49. strlen(CHT_CODEC_DAI)))
  50. return rtd->codec_dai;
  51. }
  52. return NULL;
  53. }
  54. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  55. struct snd_kcontrol *k, int event)
  56. {
  57. struct snd_soc_dapm_context *dapm = w->dapm;
  58. struct snd_soc_card *card = dapm->card;
  59. struct snd_soc_dai *codec_dai;
  60. int ret;
  61. codec_dai = cht_get_codec_dai(card);
  62. if (!codec_dai) {
  63. dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
  64. return -EIO;
  65. }
  66. if (SND_SOC_DAPM_EVENT_ON(event)) {
  67. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  68. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
  69. CHT_PLAT_CLK_3_HZ, 48000 * 512);
  70. if (ret < 0) {
  71. dev_err(card->dev, "can't set codec pll: %d\n", ret);
  72. return ret;
  73. }
  74. /* set codec sysclk source to PLL */
  75. ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
  76. 48000 * 512, SND_SOC_CLOCK_IN);
  77. if (ret < 0) {
  78. dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
  79. return ret;
  80. }
  81. } else {
  82. /* Set codec sysclk source to its internal clock because codec
  83. * PLL will be off when idle and MCLK will also be off by ACPI
  84. * when codec is runtime suspended. Codec needs clock for jack
  85. * detection and button press.
  86. */
  87. snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK,
  88. 48000 * 512, SND_SOC_CLOCK_IN);
  89. }
  90. return 0;
  91. }
  92. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  93. SND_SOC_DAPM_HP("Headphone", NULL),
  94. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  95. SND_SOC_DAPM_MIC("Int Mic", NULL),
  96. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  97. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  98. platform_clock_control, SND_SOC_DAPM_PRE_PMU |
  99. SND_SOC_DAPM_POST_PMD),
  100. };
  101. static const struct snd_soc_dapm_route cht_audio_map[] = {
  102. {"IN1P", NULL, "Headset Mic"},
  103. {"IN1N", NULL, "Headset Mic"},
  104. {"DMIC L1", NULL, "Int Mic"},
  105. {"DMIC R1", NULL, "Int Mic"},
  106. {"Headphone", NULL, "HPOL"},
  107. {"Headphone", NULL, "HPOR"},
  108. {"Ext Spk", NULL, "SPOLP"},
  109. {"Ext Spk", NULL, "SPOLN"},
  110. {"Ext Spk", NULL, "SPORP"},
  111. {"Ext Spk", NULL, "SPORN"},
  112. {"AIF1 Playback", NULL, "ssp2 Tx"},
  113. {"ssp2 Tx", NULL, "codec_out0"},
  114. {"ssp2 Tx", NULL, "codec_out1"},
  115. {"codec_in0", NULL, "ssp2 Rx"},
  116. {"codec_in1", NULL, "ssp2 Rx"},
  117. {"ssp2 Rx", NULL, "AIF1 Capture"},
  118. {"Headphone", NULL, "Platform Clock"},
  119. {"Headset Mic", NULL, "Platform Clock"},
  120. {"Int Mic", NULL, "Platform Clock"},
  121. {"Ext Spk", NULL, "Platform Clock"},
  122. };
  123. static const struct snd_kcontrol_new cht_mc_controls[] = {
  124. SOC_DAPM_PIN_SWITCH("Headphone"),
  125. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  126. SOC_DAPM_PIN_SWITCH("Int Mic"),
  127. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  128. };
  129. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  130. struct snd_pcm_hw_params *params)
  131. {
  132. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  133. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  134. int ret;
  135. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  136. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
  137. CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
  138. if (ret < 0) {
  139. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  140. return ret;
  141. }
  142. /* set codec sysclk source to PLL */
  143. ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
  144. params_rate(params) * 512,
  145. SND_SOC_CLOCK_IN);
  146. if (ret < 0) {
  147. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  153. {
  154. int ret;
  155. struct snd_soc_dai *codec_dai = runtime->codec_dai;
  156. struct snd_soc_codec *codec = codec_dai->codec;
  157. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  158. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
  159. if (ret < 0) {
  160. dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
  161. return ret;
  162. }
  163. /* Select codec ASRC clock source to track I2S1 clock, because codec
  164. * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot
  165. * be supported by RT5672. Otherwise, ASRC will be disabled and cause
  166. * noise.
  167. */
  168. rt5670_sel_asrc_clk_src(codec,
  169. RT5670_DA_STEREO_FILTER
  170. | RT5670_DA_MONO_L_FILTER
  171. | RT5670_DA_MONO_R_FILTER
  172. | RT5670_AD_STEREO_FILTER
  173. | RT5670_AD_MONO_L_FILTER
  174. | RT5670_AD_MONO_R_FILTER,
  175. RT5670_CLK_SEL_I2S1_ASRC);
  176. ret = snd_soc_card_jack_new(runtime->card, "Headset",
  177. SND_JACK_HEADSET | SND_JACK_BTN_0 |
  178. SND_JACK_BTN_1 | SND_JACK_BTN_2, &cht_bsw_headset,
  179. cht_bsw_headset_pins, ARRAY_SIZE(cht_bsw_headset_pins));
  180. if (ret)
  181. return ret;
  182. rt5670_set_jack_detect(codec, &cht_bsw_headset);
  183. return 0;
  184. }
  185. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  186. struct snd_pcm_hw_params *params)
  187. {
  188. struct snd_interval *rate = hw_param_interval(params,
  189. SNDRV_PCM_HW_PARAM_RATE);
  190. struct snd_interval *channels = hw_param_interval(params,
  191. SNDRV_PCM_HW_PARAM_CHANNELS);
  192. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  193. rate->min = rate->max = 48000;
  194. channels->min = channels->max = 2;
  195. /* set SSP2 to 24-bit */
  196. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  197. return 0;
  198. }
  199. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  200. {
  201. return snd_pcm_hw_constraint_single(substream->runtime,
  202. SNDRV_PCM_HW_PARAM_RATE, 48000);
  203. }
  204. static struct snd_soc_ops cht_aif1_ops = {
  205. .startup = cht_aif1_startup,
  206. };
  207. static struct snd_soc_ops cht_be_ssp2_ops = {
  208. .hw_params = cht_aif1_hw_params,
  209. };
  210. static struct snd_soc_dai_link cht_dailink[] = {
  211. /* Front End DAI links */
  212. [MERR_DPCM_AUDIO] = {
  213. .name = "Audio Port",
  214. .stream_name = "Audio",
  215. .cpu_dai_name = "media-cpu-dai",
  216. .codec_dai_name = "snd-soc-dummy-dai",
  217. .codec_name = "snd-soc-dummy",
  218. .platform_name = "sst-mfld-platform",
  219. .nonatomic = true,
  220. .dynamic = 1,
  221. .dpcm_playback = 1,
  222. .dpcm_capture = 1,
  223. .ops = &cht_aif1_ops,
  224. },
  225. [MERR_DPCM_COMPR] = {
  226. .name = "Compressed Port",
  227. .stream_name = "Compress",
  228. .cpu_dai_name = "compress-cpu-dai",
  229. .codec_dai_name = "snd-soc-dummy-dai",
  230. .codec_name = "snd-soc-dummy",
  231. .platform_name = "sst-mfld-platform",
  232. },
  233. /* Back End DAI links */
  234. {
  235. /* SSP2 - Codec */
  236. .name = "SSP2-Codec",
  237. .be_id = 1,
  238. .cpu_dai_name = "ssp2-port",
  239. .platform_name = "sst-mfld-platform",
  240. .no_pcm = 1,
  241. .nonatomic = true,
  242. .codec_dai_name = "rt5670-aif1",
  243. .codec_name = "i2c-10EC5670:00",
  244. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
  245. | SND_SOC_DAIFMT_CBS_CFS,
  246. .init = cht_codec_init,
  247. .be_hw_params_fixup = cht_codec_fixup,
  248. .dpcm_playback = 1,
  249. .dpcm_capture = 1,
  250. .ops = &cht_be_ssp2_ops,
  251. },
  252. };
  253. static int cht_suspend_pre(struct snd_soc_card *card)
  254. {
  255. struct snd_soc_codec *codec;
  256. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  257. if (!strcmp(codec->component.name, "i2c-10EC5670:00")) {
  258. dev_dbg(codec->dev, "disabling jack detect before going to suspend.\n");
  259. rt5670_jack_suspend(codec);
  260. break;
  261. }
  262. }
  263. return 0;
  264. }
  265. static int cht_resume_post(struct snd_soc_card *card)
  266. {
  267. struct snd_soc_codec *codec;
  268. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  269. if (!strcmp(codec->component.name, "i2c-10EC5670:00")) {
  270. dev_dbg(codec->dev, "enabling jack detect for resume.\n");
  271. rt5670_jack_resume(codec);
  272. break;
  273. }
  274. }
  275. return 0;
  276. }
  277. /* SoC card */
  278. static struct snd_soc_card snd_soc_card_cht = {
  279. .name = "cherrytrailcraudio",
  280. .owner = THIS_MODULE,
  281. .dai_link = cht_dailink,
  282. .num_links = ARRAY_SIZE(cht_dailink),
  283. .dapm_widgets = cht_dapm_widgets,
  284. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  285. .dapm_routes = cht_audio_map,
  286. .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
  287. .controls = cht_mc_controls,
  288. .num_controls = ARRAY_SIZE(cht_mc_controls),
  289. .suspend_pre = cht_suspend_pre,
  290. .resume_post = cht_resume_post,
  291. };
  292. static int snd_cht_mc_probe(struct platform_device *pdev)
  293. {
  294. int ret_val = 0;
  295. /* register the soc card */
  296. snd_soc_card_cht.dev = &pdev->dev;
  297. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
  298. if (ret_val) {
  299. dev_err(&pdev->dev,
  300. "snd_soc_register_card failed %d\n", ret_val);
  301. return ret_val;
  302. }
  303. platform_set_drvdata(pdev, &snd_soc_card_cht);
  304. return ret_val;
  305. }
  306. static struct platform_driver snd_cht_mc_driver = {
  307. .driver = {
  308. .name = "cht-bsw-rt5672",
  309. },
  310. .probe = snd_cht_mc_probe,
  311. };
  312. module_platform_driver(snd_cht_mc_driver);
  313. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
  314. MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
  315. MODULE_LICENSE("GPL v2");
  316. MODULE_ALIAS("platform:cht-bsw-rt5672");