simple-card-utils.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. const char *str;
  89. int ret;
  90. u32 val;
  91. /*
  92. * Parse dai->sysclk come from "clocks = <&xxx>"
  93. * (if system has common clock)
  94. * or "system-clock-frequency = <xxx>"
  95. * or device's module clock.
  96. */
  97. clk = of_clk_get(node, 0);
  98. if (!IS_ERR(clk)) {
  99. simple_dai->sysclk = clk_get_rate(clk);
  100. simple_dai->clk = clk;
  101. } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
  102. simple_dai->sysclk = val;
  103. } else {
  104. clk = of_clk_get(dai_of_node, 0);
  105. if (!IS_ERR(clk))
  106. simple_dai->sysclk = clk_get_rate(clk);
  107. }
  108. ret = of_property_read_string(node, "system-clock-direction", &str);
  109. if (ret == 0) {
  110. if (!strcmp(str, "out"))
  111. simple_dai->sysclk_dir = SND_SOC_CLOCK_OUT;
  112. else if (!strcmp(str, "in"))
  113. simple_dai->sysclk_dir = SND_SOC_CLOCK_IN;
  114. else
  115. return -EINVAL;
  116. }
  117. if (!of_property_read_u32(node, "system-clock-id", &val))
  118. simple_dai->sysclk_id = val;
  119. return 0;
  120. }
  121. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
  122. int asoc_simple_card_parse_dai(struct device_node *node,
  123. struct device_node **dai_of_node,
  124. const char **dai_name,
  125. const char *list_name,
  126. const char *cells_name,
  127. int *is_single_link)
  128. {
  129. struct of_phandle_args args;
  130. int ret;
  131. if (!node)
  132. return 0;
  133. /*
  134. * Get node via "sound-dai = <&phandle port>"
  135. * it will be used as xxx_of_node on soc_bind_dai_link()
  136. */
  137. ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
  138. if (ret)
  139. return ret;
  140. /* Get dai->name */
  141. if (dai_name) {
  142. ret = snd_soc_of_get_dai_name(node, dai_name);
  143. if (ret < 0)
  144. return ret;
  145. }
  146. *dai_of_node = args.np;
  147. if (is_single_link)
  148. *is_single_link = !args.args_count;
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
  152. int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
  153. struct asoc_simple_dai *simple_dai)
  154. {
  155. int ret;
  156. if (simple_dai->sysclk) {
  157. ret = snd_soc_dai_set_sysclk(dai, simple_dai->sysclk_id,
  158. simple_dai->sysclk,
  159. simple_dai->sysclk_dir);
  160. if (ret && ret != -ENOTSUPP) {
  161. dev_err(dai->dev, "simple-card: set_sysclk error\n");
  162. return ret;
  163. }
  164. }
  165. if (simple_dai->slots) {
  166. ret = snd_soc_dai_set_tdm_slot(dai,
  167. simple_dai->tx_slot_mask,
  168. simple_dai->rx_slot_mask,
  169. simple_dai->slots,
  170. simple_dai->slot_width);
  171. if (ret && ret != -ENOTSUPP) {
  172. dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
  173. return ret;
  174. }
  175. }
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
  179. int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
  180. {
  181. if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name)
  182. return -EINVAL;
  183. /* Assumes platform == cpu */
  184. if (!dai_link->platform_of_node)
  185. dai_link->platform_of_node = dai_link->cpu_of_node;
  186. return 0;
  187. }
  188. EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
  189. void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
  190. int is_single_links)
  191. {
  192. /*
  193. * In soc_bind_dai_link() will check cpu name after
  194. * of_node matching if dai_link has cpu_dai_name.
  195. * but, it will never match if name was created by
  196. * fmt_single_name() remove cpu_dai_name if cpu_args
  197. * was 0. See:
  198. * fmt_single_name()
  199. * fmt_multiple_name()
  200. */
  201. if (is_single_links)
  202. dai_link->cpu_dai_name = NULL;
  203. }
  204. EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
  205. int asoc_simple_card_clean_reference(struct snd_soc_card *card)
  206. {
  207. struct snd_soc_dai_link *dai_link;
  208. int num_links;
  209. for (num_links = 0, dai_link = card->dai_link;
  210. num_links < card->num_links;
  211. num_links++, dai_link++) {
  212. of_node_put(dai_link->cpu_of_node);
  213. of_node_put(dai_link->codec_of_node);
  214. }
  215. return 0;
  216. }
  217. EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
  218. /* Module information */
  219. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  220. MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
  221. MODULE_LICENSE("GPL v2");