soc-card.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // soc-card.c
  4. //
  5. // Copyright (C) 2019 Renesas Electronics Corp.
  6. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. //
  8. #include <linux/lockdep.h>
  9. #include <linux/rwsem.h>
  10. #include <sound/soc.h>
  11. #include <sound/jack.h>
  12. #define soc_card_ret(dai, ret) _soc_card_ret(dai, __func__, ret)
  13. static inline int _soc_card_ret(struct snd_soc_card *card,
  14. const char *func, int ret)
  15. {
  16. return snd_soc_ret(card->dev, ret,
  17. "at %s() on %s\n", func, card->name);
  18. }
  19. struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
  20. const char *name)
  21. {
  22. if (unlikely(!name))
  23. return NULL;
  24. return snd_ctl_find_id_mixer(soc_card->snd_card, name);
  25. }
  26. EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
  27. static int jack_new(struct snd_soc_card *card, const char *id, int type,
  28. struct snd_soc_jack *jack, bool initial_kctl)
  29. {
  30. mutex_init(&jack->mutex);
  31. jack->card = card;
  32. INIT_LIST_HEAD(&jack->pins);
  33. INIT_LIST_HEAD(&jack->jack_zones);
  34. BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
  35. return snd_jack_new(card->snd_card, id, type, &jack->jack, initial_kctl, false);
  36. }
  37. /**
  38. * snd_soc_card_jack_new - Create a new jack without pins
  39. * @card: ASoC card
  40. * @id: an identifying string for this jack
  41. * @type: a bitmask of enum snd_jack_type values that can be detected by
  42. * this jack
  43. * @jack: structure to use for the jack
  44. *
  45. * Creates a new jack object without pins. If adding pins later,
  46. * snd_soc_card_jack_new_pins() should be used instead with 0 as num_pins
  47. * argument.
  48. *
  49. * Returns zero if successful, or a negative error code on failure.
  50. * On success jack will be initialised.
  51. */
  52. int snd_soc_card_jack_new(struct snd_soc_card *card, const char *id, int type,
  53. struct snd_soc_jack *jack)
  54. {
  55. return soc_card_ret(card, jack_new(card, id, type, jack, true));
  56. }
  57. EXPORT_SYMBOL_GPL(snd_soc_card_jack_new);
  58. /**
  59. * snd_soc_card_jack_new_pins - Create a new jack with pins
  60. * @card: ASoC card
  61. * @id: an identifying string for this jack
  62. * @type: a bitmask of enum snd_jack_type values that can be detected by
  63. * this jack
  64. * @jack: structure to use for the jack
  65. * @pins: Array of jack pins to be added to the jack or NULL
  66. * @num_pins: Number of elements in the @pins array
  67. *
  68. * Creates a new jack object with pins. If not adding pins,
  69. * snd_soc_card_jack_new() should be used instead.
  70. *
  71. * Returns zero if successful, or a negative error code on failure.
  72. * On success jack will be initialised.
  73. */
  74. int snd_soc_card_jack_new_pins(struct snd_soc_card *card, const char *id,
  75. int type, struct snd_soc_jack *jack,
  76. struct snd_soc_jack_pin *pins,
  77. unsigned int num_pins)
  78. {
  79. int ret;
  80. ret = jack_new(card, id, type, jack, false);
  81. if (ret)
  82. goto end;
  83. if (num_pins)
  84. ret = snd_soc_jack_add_pins(jack, num_pins, pins);
  85. end:
  86. return soc_card_ret(card, ret);
  87. }
  88. EXPORT_SYMBOL_GPL(snd_soc_card_jack_new_pins);
  89. int snd_soc_card_suspend_pre(struct snd_soc_card *card)
  90. {
  91. int ret = 0;
  92. if (card->suspend_pre)
  93. ret = card->suspend_pre(card);
  94. return soc_card_ret(card, ret);
  95. }
  96. int snd_soc_card_suspend_post(struct snd_soc_card *card)
  97. {
  98. int ret = 0;
  99. if (card->suspend_post)
  100. ret = card->suspend_post(card);
  101. return soc_card_ret(card, ret);
  102. }
  103. int snd_soc_card_resume_pre(struct snd_soc_card *card)
  104. {
  105. int ret = 0;
  106. if (card->resume_pre)
  107. ret = card->resume_pre(card);
  108. return soc_card_ret(card, ret);
  109. }
  110. int snd_soc_card_resume_post(struct snd_soc_card *card)
  111. {
  112. int ret = 0;
  113. if (card->resume_post)
  114. ret = card->resume_post(card);
  115. return soc_card_ret(card, ret);
  116. }
  117. int snd_soc_card_probe(struct snd_soc_card *card)
  118. {
  119. if (card->probe) {
  120. int ret = card->probe(card);
  121. if (ret < 0)
  122. return soc_card_ret(card, ret);
  123. /*
  124. * It has "card->probe" and "card->late_probe" callbacks.
  125. * So, set "probed" flag here, because it needs to care
  126. * about "late_probe".
  127. *
  128. * see
  129. * snd_soc_bind_card()
  130. * snd_soc_card_late_probe()
  131. */
  132. card->probed = 1;
  133. }
  134. return 0;
  135. }
  136. int snd_soc_card_late_probe(struct snd_soc_card *card)
  137. {
  138. if (card->late_probe) {
  139. int ret = card->late_probe(card);
  140. if (ret < 0)
  141. return soc_card_ret(card, ret);
  142. }
  143. /*
  144. * It has "card->probe" and "card->late_probe" callbacks,
  145. * and "late_probe" callback is called after "probe".
  146. * This means, we can set "card->probed" flag afer "late_probe"
  147. * for all cases.
  148. *
  149. * see
  150. * snd_soc_bind_card()
  151. * snd_soc_card_probe()
  152. */
  153. card->probed = 1;
  154. return 0;
  155. }
  156. void snd_soc_card_fixup_controls(struct snd_soc_card *card)
  157. {
  158. if (card->fixup_controls)
  159. card->fixup_controls(card);
  160. }
  161. int snd_soc_card_remove(struct snd_soc_card *card)
  162. {
  163. int ret = 0;
  164. if (card->probed &&
  165. card->remove)
  166. ret = card->remove(card);
  167. card->probed = 0;
  168. return soc_card_ret(card, ret);
  169. }
  170. int snd_soc_card_set_bias_level(struct snd_soc_card *card,
  171. struct snd_soc_dapm_context *dapm,
  172. enum snd_soc_bias_level level)
  173. {
  174. int ret = 0;
  175. if (card->set_bias_level)
  176. ret = card->set_bias_level(card, dapm, level);
  177. return soc_card_ret(card, ret);
  178. }
  179. int snd_soc_card_set_bias_level_post(struct snd_soc_card *card,
  180. struct snd_soc_dapm_context *dapm,
  181. enum snd_soc_bias_level level)
  182. {
  183. int ret = 0;
  184. if (card->set_bias_level_post)
  185. ret = card->set_bias_level_post(card, dapm, level);
  186. return soc_card_ret(card, ret);
  187. }
  188. int snd_soc_card_add_dai_link(struct snd_soc_card *card,
  189. struct snd_soc_dai_link *dai_link)
  190. {
  191. int ret = 0;
  192. if (card->add_dai_link)
  193. ret = card->add_dai_link(card, dai_link);
  194. return soc_card_ret(card, ret);
  195. }
  196. EXPORT_SYMBOL_GPL(snd_soc_card_add_dai_link);
  197. void snd_soc_card_remove_dai_link(struct snd_soc_card *card,
  198. struct snd_soc_dai_link *dai_link)
  199. {
  200. if (card->remove_dai_link)
  201. card->remove_dai_link(card, dai_link);
  202. }
  203. EXPORT_SYMBOL_GPL(snd_soc_card_remove_dai_link);