common.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018, Linaro Limited.
  3. // Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. #include <dt-bindings/sound/qcom,q6afe.h>
  5. #include <linux/module.h>
  6. #include <sound/jack.h>
  7. #include <linux/input-event-codes.h>
  8. #include "common.h"
  9. #define NAME_SIZE 32
  10. static const struct snd_soc_dapm_widget qcom_jack_snd_widgets[] = {
  11. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  12. SND_SOC_DAPM_MIC("Mic Jack", NULL),
  13. SND_SOC_DAPM_SPK("DP0 Jack", NULL),
  14. SND_SOC_DAPM_SPK("DP1 Jack", NULL),
  15. SND_SOC_DAPM_SPK("DP2 Jack", NULL),
  16. SND_SOC_DAPM_SPK("DP3 Jack", NULL),
  17. SND_SOC_DAPM_SPK("DP4 Jack", NULL),
  18. SND_SOC_DAPM_SPK("DP5 Jack", NULL),
  19. SND_SOC_DAPM_SPK("DP6 Jack", NULL),
  20. SND_SOC_DAPM_SPK("DP7 Jack", NULL),
  21. };
  22. int qcom_snd_parse_of(struct snd_soc_card *card)
  23. {
  24. struct device_node *np;
  25. struct device_node *codec = NULL;
  26. struct device_node *platform = NULL;
  27. struct device_node *cpu = NULL;
  28. struct device *dev = card->dev;
  29. struct snd_soc_dai_link *link;
  30. struct of_phandle_args args;
  31. struct snd_soc_dai_link_component *dlc;
  32. int ret, num_links;
  33. ret = snd_soc_of_parse_card_name(card, "model");
  34. if (ret == 0 && !card->name)
  35. /* Deprecated, only for compatibility with old device trees */
  36. ret = snd_soc_of_parse_card_name(card, "qcom,model");
  37. if (ret) {
  38. dev_err(dev, "Error parsing card name: %d\n", ret);
  39. return ret;
  40. }
  41. if (of_property_read_bool(dev->of_node, "widgets")) {
  42. ret = snd_soc_of_parse_audio_simple_widgets(card, "widgets");
  43. if (ret)
  44. return ret;
  45. }
  46. /* DAPM routes */
  47. if (of_property_read_bool(dev->of_node, "audio-routing")) {
  48. ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
  49. if (ret)
  50. return ret;
  51. }
  52. /* Deprecated, only for compatibility with old device trees */
  53. if (of_property_read_bool(dev->of_node, "qcom,audio-routing")) {
  54. ret = snd_soc_of_parse_audio_routing(card, "qcom,audio-routing");
  55. if (ret)
  56. return ret;
  57. }
  58. ret = snd_soc_of_parse_pin_switches(card, "pin-switches");
  59. if (ret)
  60. return ret;
  61. ret = snd_soc_of_parse_aux_devs(card, "aux-devs");
  62. if (ret)
  63. return ret;
  64. /* Populate links */
  65. num_links = of_get_available_child_count(dev->of_node);
  66. /* Allocate the DAI link array */
  67. card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
  68. if (!card->dai_link)
  69. return -ENOMEM;
  70. card->num_links = num_links;
  71. link = card->dai_link;
  72. for_each_available_child_of_node(dev->of_node, np) {
  73. dlc = devm_kcalloc(dev, 2, sizeof(*dlc), GFP_KERNEL);
  74. if (!dlc) {
  75. ret = -ENOMEM;
  76. goto err_put_np;
  77. }
  78. link->cpus = &dlc[0];
  79. link->platforms = &dlc[1];
  80. link->num_cpus = 1;
  81. link->num_platforms = 1;
  82. ret = of_property_read_string(np, "link-name", &link->name);
  83. if (ret) {
  84. dev_err(card->dev, "error getting codec dai_link name\n");
  85. goto err_put_np;
  86. }
  87. cpu = of_get_child_by_name(np, "cpu");
  88. platform = of_get_child_by_name(np, "platform");
  89. codec = of_get_child_by_name(np, "codec");
  90. if (!cpu) {
  91. dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
  92. ret = -EINVAL;
  93. goto err;
  94. }
  95. ret = snd_soc_of_get_dlc(cpu, &args, link->cpus, 0);
  96. if (ret) {
  97. dev_err_probe(card->dev, ret,
  98. "%s: error getting cpu dai name\n", link->name);
  99. goto err;
  100. }
  101. link->id = args.args[0];
  102. if (platform) {
  103. link->platforms->of_node = of_parse_phandle(platform,
  104. "sound-dai",
  105. 0);
  106. if (!link->platforms->of_node) {
  107. dev_err(card->dev, "%s: platform dai not found\n", link->name);
  108. ret = -EINVAL;
  109. goto err;
  110. }
  111. } else {
  112. link->platforms->of_node = link->cpus->of_node;
  113. }
  114. if (codec) {
  115. ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
  116. if (ret < 0) {
  117. dev_err_probe(card->dev, ret,
  118. "%s: codec dai not found\n", link->name);
  119. goto err;
  120. }
  121. if (platform) {
  122. /* DPCM backend */
  123. link->no_pcm = 1;
  124. link->ignore_pmdown_time = 1;
  125. }
  126. } else {
  127. /* DPCM frontend */
  128. link->codecs = &snd_soc_dummy_dlc;
  129. link->num_codecs = 1;
  130. link->dynamic = 1;
  131. }
  132. if (platform || !codec) {
  133. /* DPCM */
  134. link->ignore_suspend = 1;
  135. link->nonatomic = 1;
  136. }
  137. link->stream_name = link->name;
  138. link++;
  139. of_node_put(cpu);
  140. of_node_put(codec);
  141. of_node_put(platform);
  142. }
  143. if (!card->dapm_widgets) {
  144. card->dapm_widgets = qcom_jack_snd_widgets;
  145. card->num_dapm_widgets = ARRAY_SIZE(qcom_jack_snd_widgets);
  146. }
  147. return 0;
  148. err:
  149. of_node_put(cpu);
  150. of_node_put(codec);
  151. of_node_put(platform);
  152. err_put_np:
  153. of_node_put(np);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL_GPL(qcom_snd_parse_of);
  157. static struct snd_soc_jack_pin qcom_headset_jack_pins[] = {
  158. /* Headset */
  159. {
  160. .pin = "Mic Jack",
  161. .mask = SND_JACK_MICROPHONE,
  162. },
  163. {
  164. .pin = "Headphone Jack",
  165. .mask = SND_JACK_HEADPHONE,
  166. },
  167. };
  168. int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd,
  169. struct snd_soc_jack *jack, bool *jack_setup)
  170. {
  171. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  172. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
  173. struct snd_soc_card *card = rtd->card;
  174. int rval, i;
  175. if (!*jack_setup) {
  176. rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
  177. SND_JACK_HEADSET | SND_JACK_LINEOUT |
  178. SND_JACK_MECHANICAL |
  179. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  180. SND_JACK_BTN_2 | SND_JACK_BTN_3 |
  181. SND_JACK_BTN_4 | SND_JACK_BTN_5,
  182. jack, qcom_headset_jack_pins,
  183. ARRAY_SIZE(qcom_headset_jack_pins));
  184. if (rval < 0) {
  185. dev_err(card->dev, "Unable to add Headphone Jack\n");
  186. return rval;
  187. }
  188. snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
  189. snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
  190. snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
  191. snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
  192. *jack_setup = true;
  193. }
  194. switch (cpu_dai->id) {
  195. case TX_CODEC_DMA_TX_0:
  196. case TX_CODEC_DMA_TX_1:
  197. case TX_CODEC_DMA_TX_2:
  198. case TX_CODEC_DMA_TX_3:
  199. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  200. rval = snd_soc_component_set_jack(codec_dai->component,
  201. jack, NULL);
  202. if (rval != 0 && rval != -ENOTSUPP) {
  203. dev_warn(card->dev, "Failed to set jack: %d\n", rval);
  204. return rval;
  205. }
  206. }
  207. break;
  208. default:
  209. break;
  210. }
  211. return 0;
  212. }
  213. EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup);
  214. int qcom_snd_dp_jack_setup(struct snd_soc_pcm_runtime *rtd,
  215. struct snd_soc_jack *dp_jack, int dp_pcm_id)
  216. {
  217. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
  218. struct snd_soc_card *card = rtd->card;
  219. char jack_name[NAME_SIZE];
  220. int rval, i;
  221. snprintf(jack_name, sizeof(jack_name), "DP%d Jack", dp_pcm_id);
  222. rval = snd_soc_card_jack_new(card, jack_name, SND_JACK_AVOUT, dp_jack);
  223. if (rval)
  224. return rval;
  225. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  226. rval = snd_soc_component_set_jack(codec_dai->component, dp_jack, NULL);
  227. if (rval != 0 && rval != -ENOTSUPP) {
  228. dev_warn(card->dev, "Failed to set jack: %d\n", rval);
  229. return rval;
  230. }
  231. }
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(qcom_snd_dp_jack_setup);
  235. MODULE_DESCRIPTION("ASoC Qualcomm helper functions");
  236. MODULE_LICENSE("GPL");