hdaudio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
  4. //
  5. // Authors: Cezary Rojewski <[email protected]>
  6. // Amadeusz Slawinski <[email protected]>
  7. //
  8. #include <linux/platform_device.h>
  9. #include <sound/hda_codec.h>
  10. #include <sound/hda_i915.h>
  11. #include <sound/soc.h>
  12. #include <sound/soc-acpi.h>
  13. #include "../../../codecs/hda.h"
  14. static int avs_create_dai_links(struct device *dev, struct hda_codec *codec, int pcm_count,
  15. const char *platform_name, struct snd_soc_dai_link **links)
  16. {
  17. struct snd_soc_dai_link_component *platform;
  18. struct snd_soc_dai_link *dl;
  19. struct hda_pcm *pcm;
  20. const char *cname = dev_name(&codec->core.dev);
  21. int i;
  22. dl = devm_kcalloc(dev, pcm_count, sizeof(*dl), GFP_KERNEL);
  23. platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
  24. if (!dl || !platform)
  25. return -ENOMEM;
  26. platform->name = platform_name;
  27. pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
  28. for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
  29. dl[i].name = devm_kasprintf(dev, GFP_KERNEL, "%s link%d", cname, i);
  30. if (!dl[i].name)
  31. return -ENOMEM;
  32. dl[i].id = i;
  33. dl[i].nonatomic = 1;
  34. dl[i].no_pcm = 1;
  35. dl[i].dpcm_playback = 1;
  36. dl[i].dpcm_capture = 1;
  37. dl[i].platforms = platform;
  38. dl[i].num_platforms = 1;
  39. dl[i].ignore_pmdown_time = 1;
  40. dl[i].codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
  41. dl[i].cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
  42. if (!dl[i].codecs || !dl[i].cpus)
  43. return -ENOMEM;
  44. dl[i].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d", cname, i);
  45. if (!dl[i].cpus->dai_name)
  46. return -ENOMEM;
  47. dl[i].codecs->name = devm_kstrdup(dev, cname, GFP_KERNEL);
  48. if (!dl[i].codecs->name)
  49. return -ENOMEM;
  50. dl[i].codecs->dai_name = pcm->name;
  51. dl[i].num_codecs = 1;
  52. dl[i].num_cpus = 1;
  53. }
  54. *links = dl;
  55. return 0;
  56. }
  57. static int avs_create_dapm_routes(struct device *dev, struct hda_codec *codec, int pcm_count,
  58. struct snd_soc_dapm_route **routes, int *num_routes)
  59. {
  60. struct snd_soc_dapm_route *dr;
  61. struct hda_pcm *pcm;
  62. const char *cname = dev_name(&codec->core.dev);
  63. int i, n = 0;
  64. /* at max twice the number of pcms */
  65. dr = devm_kcalloc(dev, pcm_count * 2, sizeof(*dr), GFP_KERNEL);
  66. if (!dr)
  67. return -ENOMEM;
  68. pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
  69. for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
  70. struct hda_pcm_stream *stream;
  71. int dir;
  72. dir = SNDRV_PCM_STREAM_PLAYBACK;
  73. stream = &pcm->stream[dir];
  74. if (!stream->substreams)
  75. goto capture_routes;
  76. dr[n].sink = devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
  77. snd_pcm_direction_name(dir));
  78. dr[n].source = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d Tx", cname, i);
  79. if (!dr[n].sink || !dr[n].source)
  80. return -ENOMEM;
  81. n++;
  82. capture_routes:
  83. dir = SNDRV_PCM_STREAM_CAPTURE;
  84. stream = &pcm->stream[dir];
  85. if (!stream->substreams)
  86. continue;
  87. dr[n].sink = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d Rx", cname, i);
  88. dr[n].source = devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
  89. snd_pcm_direction_name(dir));
  90. if (!dr[n].sink || !dr[n].source)
  91. return -ENOMEM;
  92. n++;
  93. }
  94. *routes = dr;
  95. *num_routes = n;
  96. return 0;
  97. }
  98. /* Should be aligned with SectionPCM's name from topology */
  99. #define FEDAI_NAME_PREFIX "HDMI"
  100. static struct snd_pcm *
  101. avs_card_hdmi_pcm_at(struct snd_soc_card *card, int hdmi_idx)
  102. {
  103. struct snd_soc_pcm_runtime *rtd;
  104. int dir = SNDRV_PCM_STREAM_PLAYBACK;
  105. for_each_card_rtds(card, rtd) {
  106. struct snd_pcm *spcm;
  107. int ret, n;
  108. spcm = rtd->pcm ? rtd->pcm->streams[dir].pcm : NULL;
  109. if (!spcm || !strstr(spcm->id, FEDAI_NAME_PREFIX))
  110. continue;
  111. ret = sscanf(spcm->id, FEDAI_NAME_PREFIX "%d", &n);
  112. if (ret != 1)
  113. continue;
  114. if (n == hdmi_idx)
  115. return rtd->pcm;
  116. }
  117. return NULL;
  118. }
  119. static int avs_card_late_probe(struct snd_soc_card *card)
  120. {
  121. struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
  122. struct hda_codec *codec = mach->pdata;
  123. struct hda_pcm *hpcm;
  124. /* Topology pcm indexing is 1-based */
  125. int i = 1;
  126. list_for_each_entry(hpcm, &codec->pcm_list_head, list) {
  127. struct snd_pcm *spcm;
  128. spcm = avs_card_hdmi_pcm_at(card, i);
  129. if (spcm) {
  130. hpcm->pcm = spcm;
  131. hpcm->device = spcm->device;
  132. dev_info(card->dev, "%s: mapping HDMI converter %d to PCM %d (%p)\n",
  133. __func__, i, hpcm->device, spcm);
  134. } else {
  135. hpcm->pcm = NULL;
  136. hpcm->device = SNDRV_PCM_INVALID_DEVICE;
  137. dev_warn(card->dev, "%s: no PCM in topology for HDMI converter %d\n",
  138. __func__, i);
  139. }
  140. i++;
  141. }
  142. return hda_codec_probe_complete(codec);
  143. }
  144. static int avs_probing_link_init(struct snd_soc_pcm_runtime *rtm)
  145. {
  146. struct snd_soc_dapm_route *routes;
  147. struct snd_soc_acpi_mach *mach;
  148. struct snd_soc_dai_link *links = NULL;
  149. struct snd_soc_card *card = rtm->card;
  150. struct hda_codec *codec;
  151. struct hda_pcm *pcm;
  152. int ret, n, pcm_count = 0;
  153. mach = dev_get_platdata(card->dev);
  154. codec = mach->pdata;
  155. if (list_empty(&codec->pcm_list_head))
  156. return -EINVAL;
  157. list_for_each_entry(pcm, &codec->pcm_list_head, list)
  158. pcm_count++;
  159. ret = avs_create_dai_links(card->dev, codec, pcm_count, mach->mach_params.platform, &links);
  160. if (ret < 0) {
  161. dev_err(card->dev, "create links failed: %d\n", ret);
  162. return ret;
  163. }
  164. for (n = 0; n < pcm_count; n++) {
  165. ret = snd_soc_add_pcm_runtime(card, &links[n]);
  166. if (ret < 0) {
  167. dev_err(card->dev, "add links failed: %d\n", ret);
  168. return ret;
  169. }
  170. }
  171. ret = avs_create_dapm_routes(card->dev, codec, pcm_count, &routes, &n);
  172. if (ret < 0) {
  173. dev_err(card->dev, "create routes failed: %d\n", ret);
  174. return ret;
  175. }
  176. ret = snd_soc_dapm_add_routes(&card->dapm, routes, n);
  177. if (ret < 0) {
  178. dev_err(card->dev, "add routes failed: %d\n", ret);
  179. return ret;
  180. }
  181. return 0;
  182. }
  183. SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
  184. static struct snd_soc_dai_link probing_link = {
  185. .name = "probing-LINK",
  186. .id = -1,
  187. .nonatomic = 1,
  188. .no_pcm = 1,
  189. .dpcm_playback = 1,
  190. .dpcm_capture = 1,
  191. .cpus = dummy,
  192. .num_cpus = ARRAY_SIZE(dummy),
  193. .init = avs_probing_link_init,
  194. };
  195. static int avs_hdaudio_probe(struct platform_device *pdev)
  196. {
  197. struct snd_soc_dai_link *binder;
  198. struct snd_soc_acpi_mach *mach;
  199. struct snd_soc_card *card;
  200. struct device *dev = &pdev->dev;
  201. struct hda_codec *codec;
  202. mach = dev_get_platdata(dev);
  203. codec = mach->pdata;
  204. /* codec may be unloaded before card's probe() fires */
  205. if (!device_is_registered(&codec->core.dev))
  206. return -ENODEV;
  207. binder = devm_kmemdup(dev, &probing_link, sizeof(probing_link), GFP_KERNEL);
  208. if (!binder)
  209. return -ENOMEM;
  210. binder->platforms = devm_kzalloc(dev, sizeof(*binder->platforms), GFP_KERNEL);
  211. binder->codecs = devm_kzalloc(dev, sizeof(*binder->codecs), GFP_KERNEL);
  212. if (!binder->platforms || !binder->codecs)
  213. return -ENOMEM;
  214. binder->codecs->name = devm_kstrdup(dev, dev_name(&codec->core.dev), GFP_KERNEL);
  215. if (!binder->codecs->name)
  216. return -ENOMEM;
  217. binder->platforms->name = mach->mach_params.platform;
  218. binder->num_platforms = 1;
  219. binder->codecs->dai_name = "codec-probing-DAI";
  220. binder->num_codecs = 1;
  221. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  222. if (!card)
  223. return -ENOMEM;
  224. card->name = binder->codecs->name;
  225. card->dev = dev;
  226. card->owner = THIS_MODULE;
  227. card->dai_link = binder;
  228. card->num_links = 1;
  229. card->fully_routed = true;
  230. if (hda_codec_is_display(codec))
  231. card->late_probe = avs_card_late_probe;
  232. return devm_snd_soc_register_card(dev, card);
  233. }
  234. static struct platform_driver avs_hdaudio_driver = {
  235. .probe = avs_hdaudio_probe,
  236. .driver = {
  237. .name = "avs_hdaudio",
  238. .pm = &snd_soc_pm_ops,
  239. },
  240. };
  241. module_platform_driver(avs_hdaudio_driver)
  242. MODULE_DESCRIPTION("Intel HD-Audio machine driver");
  243. MODULE_AUTHOR("Cezary Rojewski <[email protected]>");
  244. MODULE_LICENSE("GPL");
  245. MODULE_ALIAS("platform:avs_hdaudio");