hda.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
  4. //
  5. // Author: Cezary Rojewski <[email protected]>
  6. //
  7. #include <linux/module.h>
  8. #include <linux/pm_runtime.h>
  9. #include <sound/soc.h>
  10. #include <sound/hdaudio_ext.h>
  11. #include <sound/hda_i915.h>
  12. #include <sound/hda_codec.h>
  13. #include "hda.h"
  14. static int hda_codec_create_dais(struct hda_codec *codec, int pcm_count,
  15. struct snd_soc_dai_driver **drivers)
  16. {
  17. struct device *dev = &codec->core.dev;
  18. struct snd_soc_dai_driver *drvs;
  19. struct hda_pcm *pcm;
  20. int i;
  21. drvs = devm_kcalloc(dev, pcm_count, sizeof(*drvs), GFP_KERNEL);
  22. if (!drvs)
  23. return -ENOMEM;
  24. pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
  25. for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
  26. struct snd_soc_pcm_stream *stream;
  27. int dir;
  28. dev_info(dev, "creating for %s %d\n", pcm->name, i);
  29. drvs[i].id = i;
  30. drvs[i].name = pcm->name;
  31. drvs[i].ops = &snd_soc_hda_codec_dai_ops;
  32. dir = SNDRV_PCM_STREAM_PLAYBACK;
  33. stream = &drvs[i].playback;
  34. if (!pcm->stream[dir].substreams) {
  35. dev_info(dev, "skipping playback dai for %s\n", pcm->name);
  36. goto capture_dais;
  37. }
  38. stream->stream_name =
  39. devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
  40. snd_pcm_direction_name(dir));
  41. if (!stream->stream_name)
  42. return -ENOMEM;
  43. stream->channels_min = pcm->stream[dir].channels_min;
  44. stream->channels_max = pcm->stream[dir].channels_max;
  45. stream->rates = pcm->stream[dir].rates;
  46. stream->formats = pcm->stream[dir].formats;
  47. stream->sig_bits = pcm->stream[dir].maxbps;
  48. capture_dais:
  49. dir = SNDRV_PCM_STREAM_CAPTURE;
  50. stream = &drvs[i].capture;
  51. if (!pcm->stream[dir].substreams) {
  52. dev_info(dev, "skipping capture dai for %s\n", pcm->name);
  53. continue;
  54. }
  55. stream->stream_name =
  56. devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
  57. snd_pcm_direction_name(dir));
  58. if (!stream->stream_name)
  59. return -ENOMEM;
  60. stream->channels_min = pcm->stream[dir].channels_min;
  61. stream->channels_max = pcm->stream[dir].channels_max;
  62. stream->rates = pcm->stream[dir].rates;
  63. stream->formats = pcm->stream[dir].formats;
  64. stream->sig_bits = pcm->stream[dir].maxbps;
  65. }
  66. *drivers = drvs;
  67. return 0;
  68. }
  69. static int hda_codec_register_dais(struct hda_codec *codec, struct snd_soc_component *component)
  70. {
  71. struct snd_soc_dai_driver *drvs = NULL;
  72. struct snd_soc_dapm_context *dapm;
  73. struct hda_pcm *pcm;
  74. int ret, pcm_count = 0;
  75. if (list_empty(&codec->pcm_list_head))
  76. return -EINVAL;
  77. list_for_each_entry(pcm, &codec->pcm_list_head, list)
  78. pcm_count++;
  79. ret = hda_codec_create_dais(codec, pcm_count, &drvs);
  80. if (ret < 0)
  81. return ret;
  82. dapm = snd_soc_component_get_dapm(component);
  83. list_for_each_entry(pcm, &codec->pcm_list_head, list) {
  84. struct snd_soc_dai *dai;
  85. dai = snd_soc_register_dai(component, drvs, false);
  86. if (!dai) {
  87. dev_err(component->dev, "register dai for %s failed\n", pcm->name);
  88. return -EINVAL;
  89. }
  90. ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
  91. if (ret < 0) {
  92. dev_err(component->dev, "create widgets failed: %d\n", ret);
  93. snd_soc_unregister_dai(dai);
  94. return ret;
  95. }
  96. snd_soc_dai_init_dma_data(dai, &pcm->stream[0], &pcm->stream[1]);
  97. drvs++;
  98. }
  99. return 0;
  100. }
  101. static void hda_codec_unregister_dais(struct hda_codec *codec,
  102. struct snd_soc_component *component)
  103. {
  104. struct snd_soc_dai *dai, *save;
  105. struct hda_pcm *pcm;
  106. for_each_component_dais_safe(component, dai, save) {
  107. list_for_each_entry(pcm, &codec->pcm_list_head, list) {
  108. if (strcmp(dai->driver->name, pcm->name))
  109. continue;
  110. if (dai->playback_widget)
  111. snd_soc_dapm_free_widget(dai->playback_widget);
  112. if (dai->capture_widget)
  113. snd_soc_dapm_free_widget(dai->capture_widget);
  114. snd_soc_unregister_dai(dai);
  115. break;
  116. }
  117. }
  118. }
  119. int hda_codec_probe_complete(struct hda_codec *codec)
  120. {
  121. struct hdac_device *hdev = &codec->core;
  122. struct hdac_bus *bus = hdev->bus;
  123. int ret;
  124. ret = snd_hda_codec_build_controls(codec);
  125. if (ret < 0) {
  126. dev_err(&hdev->dev, "unable to create controls %d\n", ret);
  127. goto out;
  128. }
  129. /* Bus suspended codecs as it does not manage their pm */
  130. pm_runtime_set_active(&hdev->dev);
  131. /* rpm was forbidden in snd_hda_codec_device_new() */
  132. snd_hda_codec_set_power_save(codec, 2000);
  133. snd_hda_codec_register(codec);
  134. out:
  135. /* Complement pm_runtime_get_sync(bus) in probe */
  136. pm_runtime_mark_last_busy(bus->dev);
  137. pm_runtime_put_autosuspend(bus->dev);
  138. return ret;
  139. }
  140. EXPORT_SYMBOL_GPL(hda_codec_probe_complete);
  141. /* Expects codec with usage_count=1 and status=suspended */
  142. static int hda_codec_probe(struct snd_soc_component *component)
  143. {
  144. struct hda_codec *codec = dev_to_hda_codec(component->dev);
  145. struct hdac_device *hdev = &codec->core;
  146. struct hdac_bus *bus = hdev->bus;
  147. struct hdac_ext_link *hlink;
  148. hda_codec_patch_t patch;
  149. int ret;
  150. #ifdef CONFIG_PM
  151. WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
  152. !pm_runtime_status_suspended(&hdev->dev));
  153. #endif
  154. hlink = snd_hdac_ext_bus_link_at(bus, hdev->addr);
  155. if (!hlink) {
  156. dev_err(&hdev->dev, "hdac link not found\n");
  157. return -EIO;
  158. }
  159. pm_runtime_get_sync(bus->dev);
  160. if (hda_codec_is_display(codec))
  161. snd_hdac_display_power(bus, hdev->addr, true);
  162. snd_hdac_ext_bus_link_get(bus, hlink);
  163. ret = snd_hda_codec_device_new(codec->bus, component->card->snd_card, hdev->addr, codec,
  164. false);
  165. if (ret < 0) {
  166. dev_err(&hdev->dev, "create hda codec failed: %d\n", ret);
  167. goto device_new_err;
  168. }
  169. ret = snd_hda_codec_set_name(codec, codec->preset->name);
  170. if (ret < 0) {
  171. dev_err(&hdev->dev, "name failed %s\n", codec->preset->name);
  172. goto err;
  173. }
  174. ret = snd_hdac_regmap_init(&codec->core);
  175. if (ret < 0) {
  176. dev_err(&hdev->dev, "regmap init failed\n");
  177. goto err;
  178. }
  179. patch = (hda_codec_patch_t)codec->preset->driver_data;
  180. if (!patch) {
  181. dev_err(&hdev->dev, "no patch specified?\n");
  182. ret = -EINVAL;
  183. goto err;
  184. }
  185. ret = patch(codec);
  186. if (ret < 0) {
  187. dev_err(&hdev->dev, "patch failed %d\n", ret);
  188. goto err;
  189. }
  190. ret = snd_hda_codec_parse_pcms(codec);
  191. if (ret < 0) {
  192. dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
  193. goto parse_pcms_err;
  194. }
  195. ret = hda_codec_register_dais(codec, component);
  196. if (ret < 0) {
  197. dev_err(&hdev->dev, "update dais failed: %d\n", ret);
  198. goto parse_pcms_err;
  199. }
  200. if (!hda_codec_is_display(codec)) {
  201. ret = hda_codec_probe_complete(codec);
  202. if (ret < 0)
  203. goto complete_err;
  204. }
  205. codec->core.lazy_cache = true;
  206. return 0;
  207. complete_err:
  208. hda_codec_unregister_dais(codec, component);
  209. parse_pcms_err:
  210. if (codec->patch_ops.free)
  211. codec->patch_ops.free(codec);
  212. err:
  213. snd_hda_codec_cleanup_for_unbind(codec);
  214. device_new_err:
  215. if (hda_codec_is_display(codec))
  216. snd_hdac_display_power(bus, hdev->addr, false);
  217. snd_hdac_ext_bus_link_put(bus, hlink);
  218. pm_runtime_mark_last_busy(bus->dev);
  219. pm_runtime_put_autosuspend(bus->dev);
  220. return ret;
  221. }
  222. /* Leaves codec with usage_count=1 and status=suspended */
  223. static void hda_codec_remove(struct snd_soc_component *component)
  224. {
  225. struct hda_codec *codec = dev_to_hda_codec(component->dev);
  226. struct hdac_device *hdev = &codec->core;
  227. struct hdac_bus *bus = hdev->bus;
  228. struct hdac_ext_link *hlink;
  229. bool was_registered = codec->core.registered;
  230. /* Don't allow any more runtime suspends */
  231. pm_runtime_forbid(&hdev->dev);
  232. hda_codec_unregister_dais(codec, component);
  233. if (codec->patch_ops.free)
  234. codec->patch_ops.free(codec);
  235. snd_hda_codec_cleanup_for_unbind(codec);
  236. pm_runtime_put_noidle(&hdev->dev);
  237. /* snd_hdac_device_exit() is only called on bus remove */
  238. pm_runtime_set_suspended(&hdev->dev);
  239. if (hda_codec_is_display(codec))
  240. snd_hdac_display_power(bus, hdev->addr, false);
  241. hlink = snd_hdac_ext_bus_link_at(bus, hdev->addr);
  242. if (hlink)
  243. snd_hdac_ext_bus_link_put(bus, hlink);
  244. /*
  245. * HDMI card's hda_codec_probe_complete() (see late_probe()) may
  246. * not be called due to early error, leaving bus uc unbalanced
  247. */
  248. if (!was_registered) {
  249. pm_runtime_mark_last_busy(bus->dev);
  250. pm_runtime_put_autosuspend(bus->dev);
  251. }
  252. #ifdef CONFIG_PM
  253. WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
  254. !pm_runtime_status_suspended(&hdev->dev));
  255. #endif
  256. }
  257. static const struct snd_soc_dapm_route hda_dapm_routes[] = {
  258. {"AIF1TX", NULL, "Codec Input Pin1"},
  259. {"AIF2TX", NULL, "Codec Input Pin2"},
  260. {"AIF3TX", NULL, "Codec Input Pin3"},
  261. {"Codec Output Pin1", NULL, "AIF1RX"},
  262. {"Codec Output Pin2", NULL, "AIF2RX"},
  263. {"Codec Output Pin3", NULL, "AIF3RX"},
  264. };
  265. static const struct snd_soc_dapm_widget hda_dapm_widgets[] = {
  266. /* Audio Interface */
  267. SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
  268. SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, SND_SOC_NOPM, 0, 0),
  269. SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
  270. SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
  271. SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, SND_SOC_NOPM, 0, 0),
  272. SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
  273. /* Input Pins */
  274. SND_SOC_DAPM_INPUT("Codec Input Pin1"),
  275. SND_SOC_DAPM_INPUT("Codec Input Pin2"),
  276. SND_SOC_DAPM_INPUT("Codec Input Pin3"),
  277. /* Output Pins */
  278. SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
  279. SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
  280. SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
  281. };
  282. static struct snd_soc_dai_driver card_binder_dai = {
  283. .id = -1,
  284. .name = "codec-probing-DAI",
  285. };
  286. static int hda_hdev_attach(struct hdac_device *hdev)
  287. {
  288. struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
  289. struct snd_soc_component_driver *comp_drv;
  290. comp_drv = devm_kzalloc(&hdev->dev, sizeof(*comp_drv), GFP_KERNEL);
  291. if (!comp_drv)
  292. return -ENOMEM;
  293. /*
  294. * It's save to rely on dev_name() rather than a copy as component
  295. * driver's lifetime is directly tied to hda codec one
  296. */
  297. comp_drv->name = dev_name(&hdev->dev);
  298. comp_drv->probe = hda_codec_probe;
  299. comp_drv->remove = hda_codec_remove;
  300. comp_drv->idle_bias_on = false;
  301. if (!hda_codec_is_display(codec)) {
  302. comp_drv->dapm_widgets = hda_dapm_widgets;
  303. comp_drv->num_dapm_widgets = ARRAY_SIZE(hda_dapm_widgets);
  304. comp_drv->dapm_routes = hda_dapm_routes;
  305. comp_drv->num_dapm_routes = ARRAY_SIZE(hda_dapm_routes);
  306. }
  307. return snd_soc_register_component(&hdev->dev, comp_drv, &card_binder_dai, 1);
  308. }
  309. static int hda_hdev_detach(struct hdac_device *hdev)
  310. {
  311. struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
  312. if (codec->core.registered)
  313. cancel_delayed_work_sync(&codec->jackpoll_work);
  314. snd_soc_unregister_component(&hdev->dev);
  315. return 0;
  316. }
  317. const struct hdac_ext_bus_ops soc_hda_ext_bus_ops = {
  318. .hdev_attach = hda_hdev_attach,
  319. .hdev_detach = hda_hdev_detach,
  320. };
  321. EXPORT_SYMBOL_GPL(soc_hda_ext_bus_ops);
  322. MODULE_DESCRIPTION("HD-Audio codec driver");
  323. MODULE_AUTHOR("Cezary Rojewski <[email protected]>");
  324. MODULE_LICENSE("GPL");