bytcht_da7213.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * bytcht-da7213.c - ASoc Machine driver for Intel Baytrail and
  3. * Cherrytrail-based platforms, with Dialog DA7213 codec
  4. *
  5. * Copyright (C) 2017 Intel Corporation
  6. * Author: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. */
  21. #include <linux/module.h>
  22. #include <linux/acpi.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <asm/platform_sst_audio.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/soc-acpi.h>
  30. #include "../../codecs/da7213.h"
  31. #include "../atom/sst-atom-controls.h"
  32. static const struct snd_kcontrol_new controls[] = {
  33. SOC_DAPM_PIN_SWITCH("Headphone Jack"),
  34. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  35. SOC_DAPM_PIN_SWITCH("Mic"),
  36. SOC_DAPM_PIN_SWITCH("Aux In"),
  37. };
  38. static const struct snd_soc_dapm_widget dapm_widgets[] = {
  39. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  40. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  41. SND_SOC_DAPM_MIC("Mic", NULL),
  42. SND_SOC_DAPM_LINE("Aux In", NULL),
  43. };
  44. static const struct snd_soc_dapm_route audio_map[] = {
  45. {"Headphone Jack", NULL, "HPL"},
  46. {"Headphone Jack", NULL, "HPR"},
  47. {"AUXL", NULL, "Aux In"},
  48. {"AUXR", NULL, "Aux In"},
  49. /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */
  50. {"MIC1", NULL, "Headset Mic"},
  51. {"MIC2", NULL, "Mic"},
  52. /* SOC-codec link */
  53. {"ssp2 Tx", NULL, "codec_out0"},
  54. {"ssp2 Tx", NULL, "codec_out1"},
  55. {"codec_in0", NULL, "ssp2 Rx"},
  56. {"codec_in1", NULL, "ssp2 Rx"},
  57. {"Playback", NULL, "ssp2 Tx"},
  58. {"ssp2 Rx", NULL, "Capture"},
  59. };
  60. static int codec_fixup(struct snd_soc_pcm_runtime *rtd,
  61. struct snd_pcm_hw_params *params)
  62. {
  63. int ret;
  64. struct snd_interval *rate = hw_param_interval(params,
  65. SNDRV_PCM_HW_PARAM_RATE);
  66. struct snd_interval *channels = hw_param_interval(params,
  67. SNDRV_PCM_HW_PARAM_CHANNELS);
  68. /* The DSP will convert the FE rate to 48k, stereo, 24bits */
  69. rate->min = rate->max = 48000;
  70. channels->min = channels->max = 2;
  71. /* set SSP2 to 24-bit */
  72. params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
  73. /*
  74. * Default mode for SSP configuration is TDM 4 slot, override config
  75. * with explicit setting to I2S 2ch 24-bit. The word length is set with
  76. * dai_set_tdm_slot() since there is no other API exposed
  77. */
  78. ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
  79. SND_SOC_DAIFMT_I2S |
  80. SND_SOC_DAIFMT_NB_NF |
  81. SND_SOC_DAIFMT_CBS_CFS);
  82. if (ret < 0) {
  83. dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
  84. return ret;
  85. }
  86. ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
  87. if (ret < 0) {
  88. dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. static int aif1_startup(struct snd_pcm_substream *substream)
  94. {
  95. return snd_pcm_hw_constraint_single(substream->runtime,
  96. SNDRV_PCM_HW_PARAM_RATE, 48000);
  97. }
  98. static int aif1_hw_params(struct snd_pcm_substream *substream,
  99. struct snd_pcm_hw_params *params)
  100. {
  101. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  102. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  103. int ret;
  104. ret = snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK,
  105. 19200000, SND_SOC_CLOCK_IN);
  106. if (ret < 0)
  107. dev_err(codec_dai->dev, "can't set codec sysclk configuration\n");
  108. ret = snd_soc_dai_set_pll(codec_dai, 0,
  109. DA7213_SYSCLK_PLL_SRM, 0, DA7213_PLL_FREQ_OUT_98304000);
  110. if (ret < 0) {
  111. dev_err(codec_dai->dev, "failed to start PLL: %d\n", ret);
  112. return -EIO;
  113. }
  114. return ret;
  115. }
  116. static int aif1_hw_free(struct snd_pcm_substream *substream)
  117. {
  118. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  119. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  120. int ret;
  121. ret = snd_soc_dai_set_pll(codec_dai, 0,
  122. DA7213_SYSCLK_MCLK, 0, 0);
  123. if (ret < 0) {
  124. dev_err(codec_dai->dev, "failed to stop PLL: %d\n", ret);
  125. return -EIO;
  126. }
  127. return ret;
  128. }
  129. static const struct snd_soc_ops aif1_ops = {
  130. .startup = aif1_startup,
  131. };
  132. static const struct snd_soc_ops ssp2_ops = {
  133. .hw_params = aif1_hw_params,
  134. .hw_free = aif1_hw_free,
  135. };
  136. static struct snd_soc_dai_link dailink[] = {
  137. [MERR_DPCM_AUDIO] = {
  138. .name = "Audio Port",
  139. .stream_name = "Audio",
  140. .cpu_dai_name = "media-cpu-dai",
  141. .codec_dai_name = "snd-soc-dummy-dai",
  142. .codec_name = "snd-soc-dummy",
  143. .platform_name = "sst-mfld-platform",
  144. .nonatomic = true,
  145. .dynamic = 1,
  146. .dpcm_playback = 1,
  147. .dpcm_capture = 1,
  148. .ops = &aif1_ops,
  149. },
  150. [MERR_DPCM_DEEP_BUFFER] = {
  151. .name = "Deep-Buffer Audio Port",
  152. .stream_name = "Deep-Buffer Audio",
  153. .cpu_dai_name = "deepbuffer-cpu-dai",
  154. .codec_dai_name = "snd-soc-dummy-dai",
  155. .codec_name = "snd-soc-dummy",
  156. .platform_name = "sst-mfld-platform",
  157. .nonatomic = true,
  158. .dynamic = 1,
  159. .dpcm_playback = 1,
  160. .ops = &aif1_ops,
  161. },
  162. /* CODEC<->CODEC link */
  163. /* back ends */
  164. {
  165. .name = "SSP2-Codec",
  166. .id = 0,
  167. .cpu_dai_name = "ssp2-port",
  168. .platform_name = "sst-mfld-platform",
  169. .no_pcm = 1,
  170. .codec_dai_name = "da7213-hifi",
  171. .codec_name = "i2c-DLGS7213:00",
  172. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  173. | SND_SOC_DAIFMT_CBS_CFS,
  174. .be_hw_params_fixup = codec_fixup,
  175. .nonatomic = true,
  176. .dpcm_playback = 1,
  177. .dpcm_capture = 1,
  178. .ops = &ssp2_ops,
  179. },
  180. };
  181. /* SoC card */
  182. static struct snd_soc_card bytcht_da7213_card = {
  183. .name = "bytcht-da7213",
  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 *i2c_name = NULL;
  200. int dai_index = 0;
  201. int ret_val = 0;
  202. int i;
  203. mach = (&pdev->dev)->platform_data;
  204. card = &bytcht_da7213_card;
  205. card->dev = &pdev->dev;
  206. /* fix index of codec dai */
  207. for (i = 0; i < ARRAY_SIZE(dailink); i++) {
  208. if (!strcmp(dailink[i].codec_name, "i2c-DLGS7213:00")) {
  209. dai_index = i;
  210. break;
  211. }
  212. }
  213. /* fixup codec name based on HID */
  214. i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
  215. if (i2c_name) {
  216. snprintf(codec_name, sizeof(codec_name),
  217. "%s%s", "i2c-", i2c_name);
  218. dailink[dai_index].codec_name = codec_name;
  219. }
  220. ret_val = devm_snd_soc_register_card(&pdev->dev, card);
  221. if (ret_val) {
  222. dev_err(&pdev->dev,
  223. "snd_soc_register_card failed %d\n", ret_val);
  224. return ret_val;
  225. }
  226. platform_set_drvdata(pdev, card);
  227. return ret_val;
  228. }
  229. static struct platform_driver bytcht_da7213_driver = {
  230. .driver = {
  231. .name = "bytcht_da7213",
  232. },
  233. .probe = bytcht_da7213_probe,
  234. };
  235. module_platform_driver(bytcht_da7213_driver);
  236. MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail+DA7213 Machine driver");
  237. MODULE_AUTHOR("Pierre-Louis Bossart");
  238. MODULE_LICENSE("GPL v2");
  239. MODULE_ALIAS("platform:bytcht_da7213");