cht_bsw_rt5645.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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/slab.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/jack.h>
  29. #include "../../codecs/rt5645.h"
  30. #include "../atom/sst-atom-controls.h"
  31. #define CHT_PLAT_CLK_3_HZ 19200000
  32. #define CHT_CODEC_DAI "rt5645-aif1"
  33. struct cht_mc_private {
  34. struct snd_soc_jack hp_jack;
  35. struct snd_soc_jack mic_jack;
  36. };
  37. static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
  38. {
  39. int i;
  40. for (i = 0; i < card->num_rtd; i++) {
  41. struct snd_soc_pcm_runtime *rtd;
  42. rtd = card->rtd + i;
  43. if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
  44. strlen(CHT_CODEC_DAI)))
  45. return rtd->codec_dai;
  46. }
  47. return NULL;
  48. }
  49. static int platform_clock_control(struct snd_soc_dapm_widget *w,
  50. struct snd_kcontrol *k, int event)
  51. {
  52. struct snd_soc_dapm_context *dapm = w->dapm;
  53. struct snd_soc_card *card = dapm->card;
  54. struct snd_soc_dai *codec_dai;
  55. int ret;
  56. codec_dai = cht_get_codec_dai(card);
  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_OFF(event))
  62. return 0;
  63. /* Set codec sysclk source to its internal clock because codec PLL will
  64. * be off when idle and MCLK will also be off by ACPI when codec is
  65. * runtime suspended. Codec needs clock for jack detection and button
  66. * press.
  67. */
  68. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
  69. 0, SND_SOC_CLOCK_IN);
  70. if (ret < 0) {
  71. dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
  72. return ret;
  73. }
  74. return 0;
  75. }
  76. static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
  77. SND_SOC_DAPM_HP("Headphone", NULL),
  78. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  79. SND_SOC_DAPM_MIC("Int Mic", NULL),
  80. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  81. SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
  82. platform_clock_control, SND_SOC_DAPM_POST_PMD),
  83. };
  84. static const struct snd_soc_dapm_route cht_audio_map[] = {
  85. {"IN1P", NULL, "Headset Mic"},
  86. {"IN1N", NULL, "Headset Mic"},
  87. {"DMIC L1", NULL, "Int Mic"},
  88. {"DMIC R1", NULL, "Int Mic"},
  89. {"Headphone", NULL, "HPOL"},
  90. {"Headphone", NULL, "HPOR"},
  91. {"Ext Spk", NULL, "SPOL"},
  92. {"Ext Spk", NULL, "SPOR"},
  93. {"AIF1 Playback", NULL, "ssp2 Tx"},
  94. {"ssp2 Tx", NULL, "codec_out0"},
  95. {"ssp2 Tx", NULL, "codec_out1"},
  96. {"codec_in0", NULL, "ssp2 Rx" },
  97. {"codec_in1", NULL, "ssp2 Rx" },
  98. {"ssp2 Rx", NULL, "AIF1 Capture"},
  99. {"Headphone", NULL, "Platform Clock"},
  100. {"Headset Mic", NULL, "Platform Clock"},
  101. {"Int Mic", NULL, "Platform Clock"},
  102. {"Ext Spk", NULL, "Platform Clock"},
  103. };
  104. static const struct snd_kcontrol_new cht_mc_controls[] = {
  105. SOC_DAPM_PIN_SWITCH("Headphone"),
  106. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  107. SOC_DAPM_PIN_SWITCH("Int Mic"),
  108. SOC_DAPM_PIN_SWITCH("Ext Spk"),
  109. };
  110. static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
  111. struct snd_pcm_hw_params *params)
  112. {
  113. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  114. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  115. int ret;
  116. /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
  117. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
  118. CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
  119. if (ret < 0) {
  120. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  121. return ret;
  122. }
  123. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
  124. params_rate(params) * 512, SND_SOC_CLOCK_IN);
  125. if (ret < 0) {
  126. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  127. return ret;
  128. }
  129. return 0;
  130. }
  131. static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
  132. {
  133. int ret;
  134. struct snd_soc_codec *codec = runtime->codec;
  135. struct snd_soc_dai *codec_dai = runtime->codec_dai;
  136. struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
  137. /* Select clk_i2s1_asrc as ASRC clock source */
  138. rt5645_sel_asrc_clk_src(codec,
  139. RT5645_DA_STEREO_FILTER |
  140. RT5645_DA_MONO_L_FILTER |
  141. RT5645_DA_MONO_R_FILTER |
  142. RT5645_AD_STEREO_FILTER,
  143. RT5645_CLK_SEL_I2S1_ASRC);
  144. /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
  145. ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
  146. if (ret < 0) {
  147. dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
  148. return ret;
  149. }
  150. ret = snd_soc_card_jack_new(runtime->card, "Headphone Jack",
  151. SND_JACK_HEADPHONE, &ctx->hp_jack,
  152. NULL, 0);
  153. if (ret) {
  154. dev_err(runtime->dev, "HP jack creation failed %d\n", ret);
  155. return ret;
  156. }
  157. ret = snd_soc_card_jack_new(runtime->card, "Mic Jack",
  158. SND_JACK_MICROPHONE, &ctx->mic_jack,
  159. NULL, 0);
  160. if (ret) {
  161. dev_err(runtime->dev, "Mic jack creation failed %d\n", ret);
  162. return ret;
  163. }
  164. rt5645_set_jack_detect(codec, &ctx->hp_jack, &ctx->mic_jack);
  165. return ret;
  166. }
  167. static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  168. struct snd_pcm_hw_params *params)
  169. {
  170. struct snd_interval *rate = hw_param_interval(params,
  171. SNDRV_PCM_HW_PARAM_RATE);
  172. struct snd_interval *channels = hw_param_interval(params,
  173. SNDRV_PCM_HW_PARAM_CHANNELS);
  174. /* The DSP will covert the FE rate to 48k, stereo, 24bits */
  175. rate->min = rate->max = 48000;
  176. channels->min = channels->max = 2;
  177. /* set SSP2 to 24-bit */
  178. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  179. return 0;
  180. }
  181. static unsigned int rates_48000[] = {
  182. 48000,
  183. };
  184. static struct snd_pcm_hw_constraint_list constraints_48000 = {
  185. .count = ARRAY_SIZE(rates_48000),
  186. .list = rates_48000,
  187. };
  188. static int cht_aif1_startup(struct snd_pcm_substream *substream)
  189. {
  190. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  191. SNDRV_PCM_HW_PARAM_RATE,
  192. &constraints_48000);
  193. }
  194. static struct snd_soc_ops cht_aif1_ops = {
  195. .startup = cht_aif1_startup,
  196. };
  197. static struct snd_soc_ops cht_be_ssp2_ops = {
  198. .hw_params = cht_aif1_hw_params,
  199. };
  200. static struct snd_soc_dai_link cht_dailink[] = {
  201. [MERR_DPCM_AUDIO] = {
  202. .name = "Audio Port",
  203. .stream_name = "Audio",
  204. .cpu_dai_name = "media-cpu-dai",
  205. .codec_dai_name = "snd-soc-dummy-dai",
  206. .codec_name = "snd-soc-dummy",
  207. .platform_name = "sst-mfld-platform",
  208. .ignore_suspend = 1,
  209. .dynamic = 1,
  210. .dpcm_playback = 1,
  211. .dpcm_capture = 1,
  212. .ops = &cht_aif1_ops,
  213. },
  214. [MERR_DPCM_COMPR] = {
  215. .name = "Compressed Port",
  216. .stream_name = "Compress",
  217. .cpu_dai_name = "compress-cpu-dai",
  218. .codec_dai_name = "snd-soc-dummy-dai",
  219. .codec_name = "snd-soc-dummy",
  220. .platform_name = "sst-mfld-platform",
  221. },
  222. /* CODEC<->CODEC link */
  223. /* back ends */
  224. {
  225. .name = "SSP2-Codec",
  226. .be_id = 1,
  227. .cpu_dai_name = "ssp2-port",
  228. .platform_name = "sst-mfld-platform",
  229. .no_pcm = 1,
  230. .codec_dai_name = "rt5645-aif1",
  231. .codec_name = "i2c-10EC5645:00",
  232. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
  233. | SND_SOC_DAIFMT_CBS_CFS,
  234. .init = cht_codec_init,
  235. .be_hw_params_fixup = cht_codec_fixup,
  236. .ignore_suspend = 1,
  237. .dpcm_playback = 1,
  238. .dpcm_capture = 1,
  239. .ops = &cht_be_ssp2_ops,
  240. },
  241. };
  242. /* SoC card */
  243. static struct snd_soc_card snd_soc_card_cht = {
  244. .name = "chtrt5645",
  245. .dai_link = cht_dailink,
  246. .num_links = ARRAY_SIZE(cht_dailink),
  247. .dapm_widgets = cht_dapm_widgets,
  248. .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
  249. .dapm_routes = cht_audio_map,
  250. .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
  251. .controls = cht_mc_controls,
  252. .num_controls = ARRAY_SIZE(cht_mc_controls),
  253. };
  254. static int snd_cht_mc_probe(struct platform_device *pdev)
  255. {
  256. int ret_val = 0;
  257. struct cht_mc_private *drv;
  258. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
  259. if (!drv)
  260. return -ENOMEM;
  261. snd_soc_card_cht.dev = &pdev->dev;
  262. snd_soc_card_set_drvdata(&snd_soc_card_cht, drv);
  263. ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
  264. if (ret_val) {
  265. dev_err(&pdev->dev,
  266. "snd_soc_register_card failed %d\n", ret_val);
  267. return ret_val;
  268. }
  269. platform_set_drvdata(pdev, &snd_soc_card_cht);
  270. return ret_val;
  271. }
  272. static struct platform_driver snd_cht_mc_driver = {
  273. .driver = {
  274. .name = "cht-bsw-rt5645",
  275. .pm = &snd_soc_pm_ops,
  276. },
  277. .probe = snd_cht_mc_probe,
  278. };
  279. module_platform_driver(snd_cht_mc_driver)
  280. MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
  281. MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
  282. MODULE_LICENSE("GPL v2");
  283. MODULE_ALIAS("platform:cht-bsw-rt5645");