simple-card-utils.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * simple-card-core.c
  3. *
  4. * Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <sound/simple_card_utils.h>
  14. int asoc_simple_card_parse_daifmt(struct device *dev,
  15. struct device_node *node,
  16. struct device_node *codec,
  17. char *prefix,
  18. unsigned int *retfmt)
  19. {
  20. struct device_node *bitclkmaster = NULL;
  21. struct device_node *framemaster = NULL;
  22. int prefix_len = prefix ? strlen(prefix) : 0;
  23. unsigned int daifmt;
  24. daifmt = snd_soc_of_parse_daifmt(node, prefix,
  25. &bitclkmaster, &framemaster);
  26. daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  27. if (prefix_len && !bitclkmaster && !framemaster) {
  28. /*
  29. * No dai-link level and master setting was not found from
  30. * sound node level, revert back to legacy DT parsing and
  31. * take the settings from codec node.
  32. */
  33. dev_dbg(dev, "Revert to legacy daifmt parsing\n");
  34. daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
  35. (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
  36. } else {
  37. if (codec == bitclkmaster)
  38. daifmt |= (codec == framemaster) ?
  39. SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
  40. else
  41. daifmt |= (codec == framemaster) ?
  42. SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
  43. }
  44. of_node_put(bitclkmaster);
  45. of_node_put(framemaster);
  46. *retfmt = daifmt;
  47. return 0;
  48. }
  49. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt);
  50. int asoc_simple_card_set_dailink_name(struct device *dev,
  51. struct snd_soc_dai_link *dai_link,
  52. const char *fmt, ...)
  53. {
  54. va_list ap;
  55. char *name = NULL;
  56. int ret = -ENOMEM;
  57. va_start(ap, fmt);
  58. name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
  59. va_end(ap);
  60. if (name) {
  61. ret = 0;
  62. dai_link->name = name;
  63. dai_link->stream_name = name;
  64. }
  65. return ret;
  66. }
  67. EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name);
  68. int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
  69. char *prefix)
  70. {
  71. char prop[128];
  72. int ret;
  73. snprintf(prop, sizeof(prop), "%sname", prefix);
  74. /* Parse the card name from DT */
  75. ret = snd_soc_of_parse_card_name(card, prop);
  76. if (ret < 0)
  77. return ret;
  78. if (!card->name && card->dai_link)
  79. card->name = card->dai_link->name;
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
  83. int asoc_simple_card_parse_clk(struct device_node *node,
  84. struct device_node *dai_of_node,
  85. struct asoc_simple_dai *simple_dai)
  86. {
  87. struct clk *clk;
  88. u32 val;
  89. /*
  90. * Parse dai->sysclk come from "clocks = <&xxx>"
  91. * (if system has common clock)
  92. * or "system-clock-frequency = <xxx>"
  93. * or device's module clock.
  94. */
  95. clk = of_clk_get(node, 0);
  96. if (!IS_ERR(clk)) {
  97. simple_dai->sysclk = clk_get_rate(clk);
  98. simple_dai->clk = clk;
  99. } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
  100. simple_dai->sysclk = val;
  101. } else {
  102. clk = of_clk_get(dai_of_node, 0);
  103. if (!IS_ERR(clk))
  104. simple_dai->sysclk = clk_get_rate(clk);
  105. }
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
  109. int asoc_simple_card_parse_dai(struct device_node *node,
  110. struct device_node **dai_of_node,
  111. const char **dai_name,
  112. const char *list_name,
  113. const char *cells_name,
  114. int *is_single_link)
  115. {
  116. struct of_phandle_args args;
  117. int ret;
  118. if (!node)
  119. return 0;
  120. /*
  121. * Get node via "sound-dai = <&phandle port>"
  122. * it will be used as xxx_of_node on soc_bind_dai_link()
  123. */
  124. ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
  125. if (ret)
  126. return ret;
  127. /* Get dai->name */
  128. if (dai_name) {
  129. ret = snd_soc_of_get_dai_name(node, dai_name);
  130. if (ret < 0)
  131. return ret;
  132. }
  133. *dai_of_node = args.np;
  134. if (is_single_link)
  135. *is_single_link = !args.args_count;
  136. return 0;
  137. }
  138. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
  139. int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
  140. struct asoc_simple_dai *simple_dai)
  141. {
  142. int ret;
  143. if (simple_dai->sysclk) {
  144. ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0);
  145. if (ret && ret != -ENOTSUPP) {
  146. dev_err(dai->dev, "simple-card: set_sysclk error\n");
  147. return ret;
  148. }
  149. }
  150. if (simple_dai->slots) {
  151. ret = snd_soc_dai_set_tdm_slot(dai,
  152. simple_dai->tx_slot_mask,
  153. simple_dai->rx_slot_mask,
  154. simple_dai->slots,
  155. simple_dai->slot_width);
  156. if (ret && ret != -ENOTSUPP) {
  157. dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
  158. return ret;
  159. }
  160. }
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
  164. int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
  165. {
  166. if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name)
  167. return -EINVAL;
  168. /* Assumes platform == cpu */
  169. if (!dai_link->platform_of_node)
  170. dai_link->platform_of_node = dai_link->cpu_of_node;
  171. return 0;
  172. }
  173. EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
  174. void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
  175. int is_single_links)
  176. {
  177. /*
  178. * In soc_bind_dai_link() will check cpu name after
  179. * of_node matching if dai_link has cpu_dai_name.
  180. * but, it will never match if name was created by
  181. * fmt_single_name() remove cpu_dai_name if cpu_args
  182. * was 0. See:
  183. * fmt_single_name()
  184. * fmt_multiple_name()
  185. */
  186. if (is_single_links)
  187. dai_link->cpu_dai_name = NULL;
  188. }
  189. EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
  190. int asoc_simple_card_clean_reference(struct snd_soc_card *card)
  191. {
  192. struct snd_soc_dai_link *dai_link;
  193. int num_links;
  194. for (num_links = 0, dai_link = card->dai_link;
  195. num_links < card->num_links;
  196. num_links++, dai_link++) {
  197. of_node_put(dai_link->cpu_of_node);
  198. of_node_put(dai_link->codec_of_node);
  199. }
  200. return 0;
  201. }
  202. EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
  203. /* Module information */
  204. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  205. MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
  206. MODULE_LICENSE("GPL v2");