soc-card.c 5.8 KB

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