common.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <linux/module.h>
  5. #include <sound/jack.h>
  6. #include <linux/input-event-codes.h>
  7. #include "qdsp6/q6afe.h"
  8. #include "common.h"
  9. int qcom_snd_parse_of(struct snd_soc_card *card)
  10. {
  11. struct device_node *np;
  12. struct device_node *codec = NULL;
  13. struct device_node *platform = NULL;
  14. struct device_node *cpu = NULL;
  15. struct device *dev = card->dev;
  16. struct snd_soc_dai_link *link;
  17. struct of_phandle_args args;
  18. struct snd_soc_dai_link_component *dlc;
  19. int ret, num_links;
  20. ret = snd_soc_of_parse_card_name(card, "model");
  21. if (ret == 0 && !card->name)
  22. /* Deprecated, only for compatibility with old device trees */
  23. ret = snd_soc_of_parse_card_name(card, "qcom,model");
  24. if (ret) {
  25. dev_err(dev, "Error parsing card name: %d\n", ret);
  26. return ret;
  27. }
  28. if (of_property_read_bool(dev->of_node, "widgets")) {
  29. ret = snd_soc_of_parse_audio_simple_widgets(card, "widgets");
  30. if (ret)
  31. return ret;
  32. }
  33. /* DAPM routes */
  34. if (of_property_read_bool(dev->of_node, "audio-routing")) {
  35. ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
  36. if (ret)
  37. return ret;
  38. }
  39. /* Deprecated, only for compatibility with old device trees */
  40. if (of_property_read_bool(dev->of_node, "qcom,audio-routing")) {
  41. ret = snd_soc_of_parse_audio_routing(card, "qcom,audio-routing");
  42. if (ret)
  43. return ret;
  44. }
  45. ret = snd_soc_of_parse_pin_switches(card, "pin-switches");
  46. if (ret)
  47. return ret;
  48. ret = snd_soc_of_parse_aux_devs(card, "aux-devs");
  49. if (ret)
  50. return ret;
  51. /* Populate links */
  52. num_links = of_get_available_child_count(dev->of_node);
  53. /* Allocate the DAI link array */
  54. card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
  55. if (!card->dai_link)
  56. return -ENOMEM;
  57. card->num_links = num_links;
  58. link = card->dai_link;
  59. for_each_available_child_of_node(dev->of_node, np) {
  60. dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
  61. if (!dlc) {
  62. ret = -ENOMEM;
  63. goto err_put_np;
  64. }
  65. link->cpus = &dlc[0];
  66. link->platforms = &dlc[1];
  67. link->num_cpus = 1;
  68. link->num_platforms = 1;
  69. ret = of_property_read_string(np, "link-name", &link->name);
  70. if (ret) {
  71. dev_err(card->dev, "error getting codec dai_link name\n");
  72. goto err_put_np;
  73. }
  74. cpu = of_get_child_by_name(np, "cpu");
  75. platform = of_get_child_by_name(np, "platform");
  76. codec = of_get_child_by_name(np, "codec");
  77. if (!cpu) {
  78. dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
  79. ret = -EINVAL;
  80. goto err;
  81. }
  82. ret = of_parse_phandle_with_args(cpu, "sound-dai",
  83. "#sound-dai-cells", 0, &args);
  84. if (ret) {
  85. dev_err(card->dev, "%s: error getting cpu phandle\n", link->name);
  86. goto err;
  87. }
  88. link->cpus->of_node = args.np;
  89. link->id = args.args[0];
  90. ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
  91. if (ret) {
  92. dev_err_probe(card->dev, ret,
  93. "%s: error getting cpu dai name\n", link->name);
  94. goto err;
  95. }
  96. if (platform) {
  97. link->platforms->of_node = of_parse_phandle(platform,
  98. "sound-dai",
  99. 0);
  100. if (!link->platforms->of_node) {
  101. dev_err(card->dev, "%s: platform dai not found\n", link->name);
  102. ret = -EINVAL;
  103. goto err;
  104. }
  105. } else {
  106. link->platforms->of_node = link->cpus->of_node;
  107. }
  108. if (codec) {
  109. ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
  110. if (ret < 0) {
  111. dev_err_probe(card->dev, ret,
  112. "%s: codec dai not found\n", link->name);
  113. goto err;
  114. }
  115. if (platform) {
  116. /* DPCM backend */
  117. link->no_pcm = 1;
  118. link->ignore_pmdown_time = 1;
  119. }
  120. } else {
  121. /* DPCM frontend */
  122. dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
  123. if (!dlc) {
  124. ret = -ENOMEM;
  125. goto err;
  126. }
  127. link->codecs = dlc;
  128. link->num_codecs = 1;
  129. link->codecs->dai_name = "snd-soc-dummy-dai";
  130. link->codecs->name = "snd-soc-dummy";
  131. link->dynamic = 1;
  132. }
  133. if (platform || !codec) {
  134. /* DPCM */
  135. snd_soc_dai_link_set_capabilities(link);
  136. link->ignore_suspend = 1;
  137. link->nonatomic = 1;
  138. }
  139. link->stream_name = link->name;
  140. link++;
  141. of_node_put(cpu);
  142. of_node_put(codec);
  143. of_node_put(platform);
  144. }
  145. return 0;
  146. err:
  147. of_node_put(cpu);
  148. of_node_put(codec);
  149. of_node_put(platform);
  150. err_put_np:
  151. of_node_put(np);
  152. return ret;
  153. }
  154. EXPORT_SYMBOL_GPL(qcom_snd_parse_of);
  155. int qcom_snd_wcd_jack_setup(struct snd_soc_pcm_runtime *rtd,
  156. struct snd_soc_jack *jack, bool *jack_setup)
  157. {
  158. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  159. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  160. struct snd_soc_card *card = rtd->card;
  161. int rval, i;
  162. if (!*jack_setup) {
  163. rval = snd_soc_card_jack_new(card, "Headset Jack",
  164. SND_JACK_HEADSET | SND_JACK_LINEOUT |
  165. SND_JACK_MECHANICAL |
  166. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  167. SND_JACK_BTN_2 | SND_JACK_BTN_3 |
  168. SND_JACK_BTN_4 | SND_JACK_BTN_5,
  169. jack);
  170. if (rval < 0) {
  171. dev_err(card->dev, "Unable to add Headphone Jack\n");
  172. return rval;
  173. }
  174. snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_MEDIA);
  175. snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
  176. snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
  177. snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
  178. *jack_setup = true;
  179. }
  180. switch (cpu_dai->id) {
  181. case TX_CODEC_DMA_TX_0:
  182. case TX_CODEC_DMA_TX_1:
  183. case TX_CODEC_DMA_TX_2:
  184. case TX_CODEC_DMA_TX_3:
  185. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  186. rval = snd_soc_component_set_jack(codec_dai->component,
  187. jack, NULL);
  188. if (rval != 0 && rval != -ENOTSUPP) {
  189. dev_warn(card->dev, "Failed to set jack: %d\n", rval);
  190. return rval;
  191. }
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(qcom_snd_wcd_jack_setup);
  200. MODULE_LICENSE("GPL v2");