bytcht_da7213.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * bytcht-da7213.c - ASoc Machine driver for Intel Baytrail and
  4. * Cherrytrail-based platforms, with Dialog DA7213 codec
  5. *
  6. * Copyright (C) 2017 Intel Corporation
  7. * Author: Pierre-Louis Bossart <[email protected]>
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. */
  13. #include <linux/module.h>
  14. #include <linux/acpi.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc.h>
  20. #include <sound/soc-acpi.h>
  21. #include "../../codecs/da7213.h"
  22. #include "../atom/sst-atom-controls.h"
  23. static const struct snd_kcontrol_new controls[] = {
  24. SOC_DAPM_PIN_SWITCH("Headphone Jack"),
  25. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  26. SOC_DAPM_PIN_SWITCH("Mic"),
  27. SOC_DAPM_PIN_SWITCH("Aux In"),
  28. };
  29. static const struct snd_soc_dapm_widget dapm_widgets[] = {
  30. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  31. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  32. SND_SOC_DAPM_MIC("Mic", NULL),
  33. SND_SOC_DAPM_LINE("Aux In", NULL),
  34. };
  35. static const struct snd_soc_dapm_route audio_map[] = {
  36. {"Headphone Jack", NULL, "HPL"},
  37. {"Headphone Jack", NULL, "HPR"},
  38. {"AUXL", NULL, "Aux In"},
  39. {"AUXR", NULL, "Aux In"},
  40. /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */
  41. {"MIC1", NULL, "Headset Mic"},
  42. {"MIC2", NULL, "Mic"},
  43. /* SOC-codec link */
  44. {"ssp2 Tx", NULL, "codec_out0"},
  45. {"ssp2 Tx", NULL, "codec_out1"},
  46. {"codec_in0", NULL, "ssp2 Rx"},
  47. {"codec_in1", NULL, "ssp2 Rx"},
  48. {"Playback", NULL, "ssp2 Tx"},
  49. {"ssp2 Rx", NULL, "Capture"},
  50. };
  51. static int codec_fixup(struct snd_soc_pcm_runtime *rtd,
  52. struct snd_pcm_hw_params *params)
  53. {
  54. int ret;
  55. struct snd_interval *rate = hw_param_interval(params,
  56. SNDRV_PCM_HW_PARAM_RATE);
  57. struct snd_interval *channels = hw_param_interval(params,
  58. SNDRV_PCM_HW_PARAM_CHANNELS);
  59. /* The DSP will convert the FE rate to 48k, stereo, 24bits */
  60. rate->min = rate->max = 48000;
  61. channels->min = channels->max = 2;
  62. /* set SSP2 to 24-bit */
  63. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  64. /*
  65. * Default mode for SSP configuration is TDM 4 slot, override config
  66. * with explicit setting to I2S 2ch 24-bit. The word length is set with
  67. * dai_set_tdm_slot() since there is no other API exposed
  68. */
  69. ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
  70. SND_SOC_DAIFMT_I2S |
  71. SND_SOC_DAIFMT_NB_NF |
  72. SND_SOC_DAIFMT_BP_FP);
  73. if (ret < 0) {
  74. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  75. return ret;
  76. }
  77. ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 24);
  78. if (ret < 0) {
  79. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. static int aif1_startup(struct snd_pcm_substream *substream)
  85. {
  86. return snd_pcm_hw_constraint_single(substream->runtime,
  87. SNDRV_PCM_HW_PARAM_RATE, 48000);
  88. }
  89. static int aif1_hw_params(struct snd_pcm_substream *substream,
  90. struct snd_pcm_hw_params *params)
  91. {
  92. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  93. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  94. int ret;
  95. ret = snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK,
  96. 19200000, SND_SOC_CLOCK_IN);
  97. if (ret < 0)
  98. dev_err(codec_dai->dev, "can't set codec sysclk configuration\n");
  99. ret = snd_soc_dai_set_pll(codec_dai, 0,
  100. DA7213_SYSCLK_PLL_SRM, 0, DA7213_PLL_FREQ_OUT_98304000);
  101. if (ret < 0) {
  102. dev_err(codec_dai->dev, "failed to start PLL: %d\n", ret);
  103. return -EIO;
  104. }
  105. return ret;
  106. }
  107. static int aif1_hw_free(struct snd_pcm_substream *substream)
  108. {
  109. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  110. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  111. int ret;
  112. ret = snd_soc_dai_set_pll(codec_dai, 0,
  113. DA7213_SYSCLK_MCLK, 0, 0);
  114. if (ret < 0) {
  115. dev_err(codec_dai->dev, "failed to stop PLL: %d\n", ret);
  116. return -EIO;
  117. }
  118. return ret;
  119. }
  120. static const struct snd_soc_ops aif1_ops = {
  121. .startup = aif1_startup,
  122. };
  123. static const struct snd_soc_ops ssp2_ops = {
  124. .hw_params = aif1_hw_params,
  125. .hw_free = aif1_hw_free,
  126. };
  127. SND_SOC_DAILINK_DEF(dummy,
  128. DAILINK_COMP_ARRAY(COMP_DUMMY()));
  129. SND_SOC_DAILINK_DEF(media,
  130. DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
  131. SND_SOC_DAILINK_DEF(deepbuffer,
  132. DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
  133. SND_SOC_DAILINK_DEF(ssp2_port,
  134. DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
  135. SND_SOC_DAILINK_DEF(ssp2_codec,
  136. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7213:00",
  137. "da7213-hifi")));
  138. SND_SOC_DAILINK_DEF(platform,
  139. DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
  140. static struct snd_soc_dai_link dailink[] = {
  141. [MERR_DPCM_AUDIO] = {
  142. .name = "Audio Port",
  143. .stream_name = "Audio",
  144. .nonatomic = true,
  145. .dynamic = 1,
  146. .dpcm_playback = 1,
  147. .dpcm_capture = 1,
  148. .ops = &aif1_ops,
  149. SND_SOC_DAILINK_REG(media, dummy, platform),
  150. },
  151. [MERR_DPCM_DEEP_BUFFER] = {
  152. .name = "Deep-Buffer Audio Port",
  153. .stream_name = "Deep-Buffer Audio",
  154. .nonatomic = true,
  155. .dynamic = 1,
  156. .dpcm_playback = 1,
  157. .ops = &aif1_ops,
  158. SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
  159. },
  160. /* CODEC<->CODEC link */
  161. /* back ends */
  162. {
  163. .name = "SSP2-Codec",
  164. .id = 0,
  165. .no_pcm = 1,
  166. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  167. | SND_SOC_DAIFMT_CBC_CFC,
  168. .be_hw_params_fixup = codec_fixup,
  169. .dpcm_playback = 1,
  170. .dpcm_capture = 1,
  171. .ops = &ssp2_ops,
  172. SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
  173. },
  174. };
  175. /* use space before codec name to simplify card ID, and simplify driver name */
  176. #define SOF_CARD_NAME "bytcht da7213" /* card name will be 'sof-bytcht da7213' */
  177. #define SOF_DRIVER_NAME "SOF"
  178. #define CARD_NAME "bytcht-da7213"
  179. #define DRIVER_NAME NULL /* card name will be used for driver name */
  180. /* SoC card */
  181. static struct snd_soc_card bytcht_da7213_card = {
  182. .name = CARD_NAME,
  183. .driver_name = DRIVER_NAME,
  184. .owner = THIS_MODULE,
  185. .dai_link = dailink,
  186. .num_links = ARRAY_SIZE(dailink),
  187. .controls = controls,
  188. .num_controls = ARRAY_SIZE(controls),
  189. .dapm_widgets = dapm_widgets,
  190. .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
  191. .dapm_routes = audio_map,
  192. .num_dapm_routes = ARRAY_SIZE(audio_map),
  193. };
  194. static char codec_name[SND_ACPI_I2C_ID_LEN];
  195. static int bytcht_da7213_probe(struct platform_device *pdev)
  196. {
  197. struct snd_soc_card *card;
  198. struct snd_soc_acpi_mach *mach;
  199. const char *platform_name;
  200. struct acpi_device *adev;
  201. bool sof_parent;
  202. int dai_index = 0;
  203. int ret_val = 0;
  204. int i;
  205. mach = pdev->dev.platform_data;
  206. card = &bytcht_da7213_card;
  207. card->dev = &pdev->dev;
  208. /* fix index of codec dai */
  209. for (i = 0; i < ARRAY_SIZE(dailink); i++) {
  210. if (!strcmp(dailink[i].codecs->name, "i2c-DLGS7213:00")) {
  211. dai_index = i;
  212. break;
  213. }
  214. }
  215. /* fixup codec name based on HID */
  216. adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
  217. if (adev) {
  218. snprintf(codec_name, sizeof(codec_name),
  219. "i2c-%s", acpi_dev_name(adev));
  220. put_device(&adev->dev);
  221. dailink[dai_index].codecs->name = codec_name;
  222. }
  223. /* override platform name, if required */
  224. platform_name = mach->mach_params.platform;
  225. ret_val = snd_soc_fixup_dai_links_platform_name(card, platform_name);
  226. if (ret_val)
  227. return ret_val;
  228. sof_parent = snd_soc_acpi_sof_parent(&pdev->dev);
  229. /* set card and driver name */
  230. if (sof_parent) {
  231. bytcht_da7213_card.name = SOF_CARD_NAME;
  232. bytcht_da7213_card.driver_name = SOF_DRIVER_NAME;
  233. } else {
  234. bytcht_da7213_card.name = CARD_NAME;
  235. bytcht_da7213_card.driver_name = DRIVER_NAME;
  236. }
  237. /* set pm ops */
  238. if (sof_parent)
  239. pdev->dev.driver->pm = &snd_soc_pm_ops;
  240. ret_val = devm_snd_soc_register_card(&pdev->dev, card);
  241. if (ret_val) {
  242. dev_err(&pdev->dev,
  243. "snd_soc_register_card failed %d\n", ret_val);
  244. return ret_val;
  245. }
  246. platform_set_drvdata(pdev, card);
  247. return ret_val;
  248. }
  249. static struct platform_driver bytcht_da7213_driver = {
  250. .driver = {
  251. .name = "bytcht_da7213",
  252. },
  253. .probe = bytcht_da7213_probe,
  254. };
  255. module_platform_driver(bytcht_da7213_driver);
  256. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail+DA7213 Machine driver");
  257. MODULE_AUTHOR("Pierre-Louis Bossart");
  258. MODULE_LICENSE("GPL v2");
  259. MODULE_ALIAS("platform:bytcht_da7213");