soc_sdw_maxim.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // This file incorporates work covered by the following copyright notice:
  3. // Copyright (c) 2020 Intel Corporation
  4. // Copyright (c) 2024 Advanced Micro Devices, Inc.
  5. //
  6. // soc_sdw_maxim - Helpers to handle maxim codecs
  7. // codec devices from generic machine driver
  8. #include <linux/device.h>
  9. #include <linux/errno.h>
  10. #include <sound/control.h>
  11. #include <sound/soc.h>
  12. #include <sound/soc-acpi.h>
  13. #include <sound/soc-dapm.h>
  14. #include <sound/soc_sdw_utils.h>
  15. static int maxim_part_id;
  16. #define SOC_SDW_PART_ID_MAX98363 0x8363
  17. #define SOC_SDW_PART_ID_MAX98373 0x8373
  18. static const struct snd_soc_dapm_route max_98373_dapm_routes[] = {
  19. { "Left Spk", NULL, "Left BE_OUT" },
  20. { "Right Spk", NULL, "Right BE_OUT" },
  21. };
  22. int asoc_sdw_maxim_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
  23. {
  24. struct snd_soc_card *card = rtd->card;
  25. int ret;
  26. card->components = devm_kasprintf(card->dev, GFP_KERNEL,
  27. "%s spk:mx%04x",
  28. card->components, maxim_part_id);
  29. if (!card->components)
  30. return -ENOMEM;
  31. dev_dbg(card->dev, "soundwire maxim card components assigned : %s\n",
  32. card->components);
  33. ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes, 2);
  34. if (ret)
  35. dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
  36. return ret;
  37. }
  38. EXPORT_SYMBOL_NS(asoc_sdw_maxim_spk_rtd_init, SND_SOC_SDW_UTILS);
  39. static int asoc_sdw_mx8373_enable_spk_pin(struct snd_pcm_substream *substream, bool enable)
  40. {
  41. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  42. struct snd_soc_dai *codec_dai;
  43. struct snd_soc_dai *cpu_dai;
  44. int ret;
  45. int j;
  46. /* set spk pin by playback only */
  47. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  48. return 0;
  49. cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  50. for_each_rtd_codec_dais(rtd, j, codec_dai) {
  51. struct snd_soc_dapm_context *dapm =
  52. snd_soc_component_get_dapm(cpu_dai->component);
  53. char pin_name[16];
  54. snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
  55. codec_dai->component->name_prefix);
  56. if (enable)
  57. ret = snd_soc_dapm_enable_pin(dapm, pin_name);
  58. else
  59. ret = snd_soc_dapm_disable_pin(dapm, pin_name);
  60. if (!ret)
  61. snd_soc_dapm_sync(dapm);
  62. }
  63. return 0;
  64. }
  65. static int asoc_sdw_mx8373_prepare(struct snd_pcm_substream *substream)
  66. {
  67. int ret;
  68. /* according to soc_pcm_prepare dai link prepare is called first */
  69. ret = asoc_sdw_prepare(substream);
  70. if (ret < 0)
  71. return ret;
  72. return asoc_sdw_mx8373_enable_spk_pin(substream, true);
  73. }
  74. static int asoc_sdw_mx8373_hw_free(struct snd_pcm_substream *substream)
  75. {
  76. int ret;
  77. /* according to soc_pcm_hw_free dai link free is called first */
  78. ret = asoc_sdw_hw_free(substream);
  79. if (ret < 0)
  80. return ret;
  81. return asoc_sdw_mx8373_enable_spk_pin(substream, false);
  82. }
  83. static const struct snd_soc_ops max_98373_sdw_ops = {
  84. .startup = asoc_sdw_startup,
  85. .prepare = asoc_sdw_mx8373_prepare,
  86. .trigger = asoc_sdw_trigger,
  87. .hw_params = asoc_sdw_hw_params,
  88. .hw_free = asoc_sdw_mx8373_hw_free,
  89. .shutdown = asoc_sdw_shutdown,
  90. };
  91. static int asoc_sdw_mx8373_sdw_late_probe(struct snd_soc_card *card)
  92. {
  93. struct snd_soc_dapm_context *dapm = &card->dapm;
  94. /* Disable Left and Right Spk pin after boot */
  95. snd_soc_dapm_disable_pin(dapm, "Left Spk");
  96. snd_soc_dapm_disable_pin(dapm, "Right Spk");
  97. return snd_soc_dapm_sync(dapm);
  98. }
  99. int asoc_sdw_maxim_init(struct snd_soc_card *card,
  100. struct snd_soc_dai_link *dai_links,
  101. struct asoc_sdw_codec_info *info,
  102. bool playback)
  103. {
  104. info->amp_num++;
  105. maxim_part_id = info->part_id;
  106. switch (maxim_part_id) {
  107. case SOC_SDW_PART_ID_MAX98363:
  108. /* Default ops are set in function init_dai_link.
  109. * called as part of function create_sdw_dailink
  110. */
  111. break;
  112. case SOC_SDW_PART_ID_MAX98373:
  113. info->codec_card_late_probe = asoc_sdw_mx8373_sdw_late_probe;
  114. dai_links->ops = &max_98373_sdw_ops;
  115. break;
  116. default:
  117. dev_err(card->dev, "Invalid maxim_part_id %#x\n", maxim_part_id);
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_NS(asoc_sdw_maxim_init, SND_SOC_SDW_UTILS);