bytcht_es8316.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * bytcht_es8316.c - ASoc Machine driver for Intel Baytrail/Cherrytrail
  3. * platforms with Everest ES8316 SoC
  4. *
  5. * Copyright (C) 2017 Endless Mobile, Inc.
  6. * Authors: David Yang <yangxiaohua@everest-semi.com>,
  7. * Daniel Drake <drake@endlessm.com>
  8. *
  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/init.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/device.h>
  26. #include <linux/slab.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/soc-acpi.h>
  33. #include "../atom/sst-atom-controls.h"
  34. #include "../common/sst-dsp.h"
  35. struct byt_cht_es8316_private {
  36. struct clk *mclk;
  37. };
  38. static const struct snd_soc_dapm_widget byt_cht_es8316_widgets[] = {
  39. SND_SOC_DAPM_HP("Headphone", NULL),
  40. /*
  41. * The codec supports two analog microphone inputs. I have only
  42. * tested MIC1. A DMIC route could also potentially be added
  43. * if such functionality is found on another platform.
  44. */
  45. SND_SOC_DAPM_MIC("Microphone 1", NULL),
  46. SND_SOC_DAPM_MIC("Microphone 2", NULL),
  47. };
  48. static const struct snd_soc_dapm_route byt_cht_es8316_audio_map[] = {
  49. {"MIC1", NULL, "Microphone 1"},
  50. {"MIC2", NULL, "Microphone 2"},
  51. {"Headphone", NULL, "HPOL"},
  52. {"Headphone", NULL, "HPOR"},
  53. {"Playback", NULL, "ssp2 Tx"},
  54. {"ssp2 Tx", NULL, "codec_out0"},
  55. {"ssp2 Tx", NULL, "codec_out1"},
  56. {"codec_in0", NULL, "ssp2 Rx" },
  57. {"codec_in1", NULL, "ssp2 Rx" },
  58. {"ssp2 Rx", NULL, "Capture"},
  59. };
  60. static const struct snd_kcontrol_new byt_cht_es8316_controls[] = {
  61. SOC_DAPM_PIN_SWITCH("Headphone"),
  62. SOC_DAPM_PIN_SWITCH("Microphone 1"),
  63. SOC_DAPM_PIN_SWITCH("Microphone 2"),
  64. };
  65. static int byt_cht_es8316_init(struct snd_soc_pcm_runtime *runtime)
  66. {
  67. struct snd_soc_card *card = runtime->card;
  68. struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
  69. int ret;
  70. card->dapm.idle_bias_off = true;
  71. /*
  72. * The firmware might enable the clock at boot (this information
  73. * may or may not be reflected in the enable clock register).
  74. * To change the rate we must disable the clock first to cover these
  75. * cases. Due to common clock framework restrictions that do not allow
  76. * to disable a clock that has not been enabled, we need to enable
  77. * the clock first.
  78. */
  79. ret = clk_prepare_enable(priv->mclk);
  80. if (!ret)
  81. clk_disable_unprepare(priv->mclk);
  82. ret = clk_set_rate(priv->mclk, 19200000);
  83. if (ret)
  84. dev_err(card->dev, "unable to set MCLK rate\n");
  85. ret = clk_prepare_enable(priv->mclk);
  86. if (ret)
  87. dev_err(card->dev, "unable to enable MCLK\n");
  88. ret = snd_soc_dai_set_sysclk(runtime->codec_dai, 0, 19200000,
  89. SND_SOC_CLOCK_IN);
  90. if (ret < 0) {
  91. dev_err(card->dev, "can't set codec clock %d\n", ret);
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. static const struct snd_soc_pcm_stream byt_cht_es8316_dai_params = {
  97. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  98. .rate_min = 48000,
  99. .rate_max = 48000,
  100. .channels_min = 2,
  101. .channels_max = 2,
  102. };
  103. static int byt_cht_es8316_codec_fixup(struct snd_soc_pcm_runtime *rtd,
  104. struct snd_pcm_hw_params *params)
  105. {
  106. struct snd_interval *rate = hw_param_interval(params,
  107. SNDRV_PCM_HW_PARAM_RATE);
  108. struct snd_interval *channels = hw_param_interval(params,
  109. SNDRV_PCM_HW_PARAM_CHANNELS);
  110. int ret;
  111. /* The DSP will covert the FE rate to 48k, stereo */
  112. rate->min = rate->max = 48000;
  113. channels->min = channels->max = 2;
  114. /* set SSP2 to 24-bit */
  115. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  116. /*
  117. * Default mode for SSP configuration is TDM 4 slot, override config
  118. * with explicit setting to I2S 2ch 24-bit. The word length is set with
  119. * dai_set_tdm_slot() since there is no other API exposed
  120. */
  121. ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
  122. SND_SOC_DAIFMT_I2S |
  123. SND_SOC_DAIFMT_NB_NF |
  124. SND_SOC_DAIFMT_CBS_CFS
  125. );
  126. if (ret < 0) {
  127. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  128. return ret;
  129. }
  130. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
  131. if (ret < 0) {
  132. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. static int byt_cht_es8316_aif1_startup(struct snd_pcm_substream *substream)
  138. {
  139. return snd_pcm_hw_constraint_single(substream->runtime,
  140. SNDRV_PCM_HW_PARAM_RATE, 48000);
  141. }
  142. static const struct snd_soc_ops byt_cht_es8316_aif1_ops = {
  143. .startup = byt_cht_es8316_aif1_startup,
  144. };
  145. static struct snd_soc_dai_link byt_cht_es8316_dais[] = {
  146. [MERR_DPCM_AUDIO] = {
  147. .name = "Audio Port",
  148. .stream_name = "Audio",
  149. .cpu_dai_name = "media-cpu-dai",
  150. .codec_dai_name = "snd-soc-dummy-dai",
  151. .codec_name = "snd-soc-dummy",
  152. .platform_name = "sst-mfld-platform",
  153. .nonatomic = true,
  154. .dynamic = 1,
  155. .dpcm_playback = 1,
  156. .dpcm_capture = 1,
  157. .ops = &byt_cht_es8316_aif1_ops,
  158. },
  159. [MERR_DPCM_DEEP_BUFFER] = {
  160. .name = "Deep-Buffer Audio Port",
  161. .stream_name = "Deep-Buffer Audio",
  162. .cpu_dai_name = "deepbuffer-cpu-dai",
  163. .codec_dai_name = "snd-soc-dummy-dai",
  164. .codec_name = "snd-soc-dummy",
  165. .platform_name = "sst-mfld-platform",
  166. .nonatomic = true,
  167. .dynamic = 1,
  168. .dpcm_playback = 1,
  169. .ops = &byt_cht_es8316_aif1_ops,
  170. },
  171. /* back ends */
  172. {
  173. /* Only SSP2 has been tested here, so BYT-CR platforms that
  174. * require SSP0 will not work.
  175. */
  176. .name = "SSP2-Codec",
  177. .id = 0,
  178. .cpu_dai_name = "ssp2-port",
  179. .platform_name = "sst-mfld-platform",
  180. .no_pcm = 1,
  181. .codec_dai_name = "ES8316 HiFi",
  182. .codec_name = "i2c-ESSX8316:00",
  183. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  184. | SND_SOC_DAIFMT_CBS_CFS,
  185. .be_hw_params_fixup = byt_cht_es8316_codec_fixup,
  186. .nonatomic = true,
  187. .dpcm_playback = 1,
  188. .dpcm_capture = 1,
  189. .init = byt_cht_es8316_init,
  190. },
  191. };
  192. /* SoC card */
  193. static struct snd_soc_card byt_cht_es8316_card = {
  194. .name = "bytcht-es8316",
  195. .owner = THIS_MODULE,
  196. .dai_link = byt_cht_es8316_dais,
  197. .num_links = ARRAY_SIZE(byt_cht_es8316_dais),
  198. .dapm_widgets = byt_cht_es8316_widgets,
  199. .num_dapm_widgets = ARRAY_SIZE(byt_cht_es8316_widgets),
  200. .dapm_routes = byt_cht_es8316_audio_map,
  201. .num_dapm_routes = ARRAY_SIZE(byt_cht_es8316_audio_map),
  202. .controls = byt_cht_es8316_controls,
  203. .num_controls = ARRAY_SIZE(byt_cht_es8316_controls),
  204. .fully_routed = true,
  205. };
  206. static char codec_name[SND_ACPI_I2C_ID_LEN];
  207. static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
  208. {
  209. struct byt_cht_es8316_private *priv;
  210. struct snd_soc_acpi_mach *mach;
  211. const char *i2c_name = NULL;
  212. int dai_index = 0;
  213. int i;
  214. int ret = 0;
  215. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  216. if (!priv)
  217. return -ENOMEM;
  218. mach = (&pdev->dev)->platform_data;
  219. /* fix index of codec dai */
  220. for (i = 0; i < ARRAY_SIZE(byt_cht_es8316_dais); i++) {
  221. if (!strcmp(byt_cht_es8316_dais[i].codec_name,
  222. "i2c-ESSX8316:00")) {
  223. dai_index = i;
  224. break;
  225. }
  226. }
  227. /* fixup codec name based on HID */
  228. i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
  229. if (i2c_name) {
  230. snprintf(codec_name, sizeof(codec_name),
  231. "%s%s", "i2c-", i2c_name);
  232. byt_cht_es8316_dais[dai_index].codec_name = codec_name;
  233. }
  234. /* register the soc card */
  235. byt_cht_es8316_card.dev = &pdev->dev;
  236. snd_soc_card_set_drvdata(&byt_cht_es8316_card, priv);
  237. priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
  238. if (IS_ERR(priv->mclk)) {
  239. ret = PTR_ERR(priv->mclk);
  240. dev_err(&pdev->dev,
  241. "Failed to get MCLK from pmc_plt_clk_3: %d\n",
  242. ret);
  243. return ret;
  244. }
  245. ret = devm_snd_soc_register_card(&pdev->dev, &byt_cht_es8316_card);
  246. if (ret) {
  247. dev_err(&pdev->dev, "snd_soc_register_card failed %d\n", ret);
  248. return ret;
  249. }
  250. platform_set_drvdata(pdev, &byt_cht_es8316_card);
  251. return ret;
  252. }
  253. static struct platform_driver snd_byt_cht_es8316_mc_driver = {
  254. .driver = {
  255. .name = "bytcht_es8316",
  256. },
  257. .probe = snd_byt_cht_es8316_mc_probe,
  258. };
  259. module_platform_driver(snd_byt_cht_es8316_mc_driver);
  260. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail Machine driver");
  261. MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
  262. MODULE_LICENSE("GPL v2");
  263. MODULE_ALIAS("platform:bytcht_es8316");