cht_bsw_rt5672.c 10 KB

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