simple_card_utils.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * simple_card_utils.h
  4. *
  5. * Copyright (c) 2016 Kuninori Morimoto <[email protected]>
  6. */
  7. #ifndef __SIMPLE_CARD_UTILS_H
  8. #define __SIMPLE_CARD_UTILS_H
  9. #include <linux/clk.h>
  10. #include <sound/soc.h>
  11. #define asoc_simple_init_hp(card, sjack, prefix) \
  12. asoc_simple_init_jack(card, sjack, 1, prefix, NULL)
  13. #define asoc_simple_init_mic(card, sjack, prefix) \
  14. asoc_simple_init_jack(card, sjack, 0, prefix, NULL)
  15. struct asoc_simple_tdm_width_map {
  16. u8 sample_bits;
  17. u8 slot_count;
  18. u16 slot_width;
  19. };
  20. struct asoc_simple_dai {
  21. const char *name;
  22. unsigned int sysclk;
  23. int clk_direction;
  24. int slots;
  25. int slot_width;
  26. unsigned int tx_slot_mask;
  27. unsigned int rx_slot_mask;
  28. struct clk *clk;
  29. bool clk_fixed;
  30. struct asoc_simple_tdm_width_map *tdm_width_map;
  31. int n_tdm_widths;
  32. };
  33. struct asoc_simple_data {
  34. u32 convert_rate;
  35. u32 convert_channels;
  36. const char *convert_sample_format;
  37. };
  38. struct asoc_simple_jack {
  39. struct snd_soc_jack jack;
  40. struct snd_soc_jack_pin pin;
  41. struct snd_soc_jack_gpio gpio;
  42. };
  43. struct prop_nums {
  44. int cpus;
  45. int codecs;
  46. int platforms;
  47. };
  48. struct asoc_simple_priv {
  49. struct snd_soc_card snd_card;
  50. struct simple_dai_props {
  51. struct asoc_simple_dai *cpu_dai;
  52. struct asoc_simple_dai *codec_dai;
  53. struct snd_soc_dai_link_component *cpus;
  54. struct snd_soc_dai_link_component *codecs;
  55. struct snd_soc_dai_link_component *platforms;
  56. struct asoc_simple_data adata;
  57. struct snd_soc_codec_conf *codec_conf;
  58. struct prop_nums num;
  59. unsigned int mclk_fs;
  60. } *dai_props;
  61. struct asoc_simple_jack hp_jack;
  62. struct asoc_simple_jack mic_jack;
  63. struct snd_soc_dai_link *dai_link;
  64. struct asoc_simple_dai *dais;
  65. struct snd_soc_dai_link_component *dlcs;
  66. struct snd_soc_dai_link_component dummy;
  67. struct snd_soc_codec_conf *codec_conf;
  68. struct gpio_desc *pa_gpio;
  69. const struct snd_soc_ops *ops;
  70. unsigned int dpcm_selectable:1;
  71. unsigned int force_dpcm:1;
  72. };
  73. #define simple_priv_to_card(priv) (&(priv)->snd_card)
  74. #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i))
  75. #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev)
  76. #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i))
  77. #define simple_props_to_dlc_cpu(props, i) ((props)->cpus + i)
  78. #define simple_props_to_dlc_codec(props, i) ((props)->codecs + i)
  79. #define simple_props_to_dlc_platform(props, i) ((props)->platforms + i)
  80. #define simple_props_to_dai_cpu(props, i) ((props)->cpu_dai + i)
  81. #define simple_props_to_dai_codec(props, i) ((props)->codec_dai + i)
  82. #define simple_props_to_codec_conf(props, i) ((props)->codec_conf + i)
  83. #define for_each_prop_dlc_cpus(props, i, cpu) \
  84. for ((i) = 0; \
  85. ((i) < (props)->num.cpus) && \
  86. ((cpu) = simple_props_to_dlc_cpu(props, i)); \
  87. (i)++)
  88. #define for_each_prop_dlc_codecs(props, i, codec) \
  89. for ((i) = 0; \
  90. ((i) < (props)->num.codecs) && \
  91. ((codec) = simple_props_to_dlc_codec(props, i)); \
  92. (i)++)
  93. #define for_each_prop_dlc_platforms(props, i, platform) \
  94. for ((i) = 0; \
  95. ((i) < (props)->num.platforms) && \
  96. ((platform) = simple_props_to_dlc_platform(props, i)); \
  97. (i)++)
  98. #define for_each_prop_codec_conf(props, i, conf) \
  99. for ((i) = 0; \
  100. ((i) < (props)->num.codecs) && \
  101. (props)->codec_conf && \
  102. ((conf) = simple_props_to_codec_conf(props, i)); \
  103. (i)++)
  104. #define for_each_prop_dai_cpu(props, i, cpu) \
  105. for ((i) = 0; \
  106. ((i) < (props)->num.cpus) && \
  107. ((cpu) = simple_props_to_dai_cpu(props, i)); \
  108. (i)++)
  109. #define for_each_prop_dai_codec(props, i, codec) \
  110. for ((i) = 0; \
  111. ((i) < (props)->num.codecs) && \
  112. ((codec) = simple_props_to_dai_codec(props, i)); \
  113. (i)++)
  114. #define SNDRV_MAX_LINKS 512
  115. struct link_info {
  116. int link; /* number of link */
  117. int cpu; /* turn for CPU / Codec */
  118. struct prop_nums num[SNDRV_MAX_LINKS];
  119. };
  120. int asoc_simple_parse_daifmt(struct device *dev,
  121. struct device_node *node,
  122. struct device_node *codec,
  123. char *prefix,
  124. unsigned int *retfmt);
  125. int asoc_simple_parse_tdm_width_map(struct device *dev, struct device_node *np,
  126. struct asoc_simple_dai *dai);
  127. __printf(3, 4)
  128. int asoc_simple_set_dailink_name(struct device *dev,
  129. struct snd_soc_dai_link *dai_link,
  130. const char *fmt, ...);
  131. int asoc_simple_parse_card_name(struct snd_soc_card *card,
  132. char *prefix);
  133. int asoc_simple_parse_clk(struct device *dev,
  134. struct device_node *node,
  135. struct asoc_simple_dai *simple_dai,
  136. struct snd_soc_dai_link_component *dlc);
  137. int asoc_simple_startup(struct snd_pcm_substream *substream);
  138. void asoc_simple_shutdown(struct snd_pcm_substream *substream);
  139. int asoc_simple_hw_params(struct snd_pcm_substream *substream,
  140. struct snd_pcm_hw_params *params);
  141. int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd);
  142. int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  143. struct snd_pcm_hw_params *params);
  144. #define asoc_simple_parse_tdm(np, dai) \
  145. snd_soc_of_parse_tdm_slot(np, &(dai)->tx_slot_mask, \
  146. &(dai)->rx_slot_mask, \
  147. &(dai)->slots, \
  148. &(dai)->slot_width);
  149. void asoc_simple_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
  150. struct snd_soc_dai_link_component *cpus);
  151. void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
  152. int is_single_links);
  153. void asoc_simple_clean_reference(struct snd_soc_card *card);
  154. void asoc_simple_convert_fixup(struct asoc_simple_data *data,
  155. struct snd_pcm_hw_params *params);
  156. void asoc_simple_parse_convert(struct device_node *np, char *prefix,
  157. struct asoc_simple_data *data);
  158. bool asoc_simple_is_convert_required(const struct asoc_simple_data *data);
  159. int asoc_simple_parse_routing(struct snd_soc_card *card,
  160. char *prefix);
  161. int asoc_simple_parse_widgets(struct snd_soc_card *card,
  162. char *prefix);
  163. int asoc_simple_parse_pin_switches(struct snd_soc_card *card,
  164. char *prefix);
  165. int asoc_simple_init_jack(struct snd_soc_card *card,
  166. struct asoc_simple_jack *sjack,
  167. int is_hp, char *prefix, char *pin);
  168. int asoc_simple_init_priv(struct asoc_simple_priv *priv,
  169. struct link_info *li);
  170. int asoc_simple_remove(struct platform_device *pdev);
  171. int asoc_graph_card_probe(struct snd_soc_card *card);
  172. int asoc_graph_is_ports0(struct device_node *port);
  173. #ifdef DEBUG
  174. static inline void asoc_simple_debug_dai(struct asoc_simple_priv *priv,
  175. char *name,
  176. struct asoc_simple_dai *dai)
  177. {
  178. struct device *dev = simple_priv_to_dev(priv);
  179. /* dai might be NULL */
  180. if (!dai)
  181. return;
  182. if (dai->name)
  183. dev_dbg(dev, "%s dai name = %s\n",
  184. name, dai->name);
  185. if (dai->slots)
  186. dev_dbg(dev, "%s slots = %d\n", name, dai->slots);
  187. if (dai->slot_width)
  188. dev_dbg(dev, "%s slot width = %d\n", name, dai->slot_width);
  189. if (dai->tx_slot_mask)
  190. dev_dbg(dev, "%s tx slot mask = %d\n", name, dai->tx_slot_mask);
  191. if (dai->rx_slot_mask)
  192. dev_dbg(dev, "%s rx slot mask = %d\n", name, dai->rx_slot_mask);
  193. if (dai->clk)
  194. dev_dbg(dev, "%s clk %luHz\n", name, clk_get_rate(dai->clk));
  195. if (dai->sysclk)
  196. dev_dbg(dev, "%s sysclk = %dHz\n",
  197. name, dai->sysclk);
  198. if (dai->clk || dai->sysclk)
  199. dev_dbg(dev, "%s direction = %s\n",
  200. name, dai->clk_direction ? "OUT" : "IN");
  201. }
  202. static inline void asoc_simple_debug_info(struct asoc_simple_priv *priv)
  203. {
  204. struct snd_soc_card *card = simple_priv_to_card(priv);
  205. struct device *dev = simple_priv_to_dev(priv);
  206. int i;
  207. if (card->name)
  208. dev_dbg(dev, "Card Name: %s\n", card->name);
  209. for (i = 0; i < card->num_links; i++) {
  210. struct simple_dai_props *props = simple_priv_to_props(priv, i);
  211. struct snd_soc_dai_link *link = simple_priv_to_link(priv, i);
  212. struct asoc_simple_dai *dai;
  213. struct snd_soc_codec_conf *cnf;
  214. int j;
  215. dev_dbg(dev, "DAI%d\n", i);
  216. dev_dbg(dev, "cpu num = %d\n", link->num_cpus);
  217. for_each_prop_dai_cpu(props, j, dai)
  218. asoc_simple_debug_dai(priv, "cpu", dai);
  219. dev_dbg(dev, "codec num = %d\n", link->num_codecs);
  220. for_each_prop_dai_codec(props, j, dai)
  221. asoc_simple_debug_dai(priv, "codec", dai);
  222. if (link->name)
  223. dev_dbg(dev, "dai name = %s\n", link->name);
  224. if (link->dai_fmt)
  225. dev_dbg(dev, "dai format = %04x\n", link->dai_fmt);
  226. if (props->adata.convert_rate)
  227. dev_dbg(dev, "convert_rate = %d\n", props->adata.convert_rate);
  228. if (props->adata.convert_channels)
  229. dev_dbg(dev, "convert_channels = %d\n", props->adata.convert_channels);
  230. for_each_prop_codec_conf(props, j, cnf)
  231. if (cnf->name_prefix)
  232. dev_dbg(dev, "name prefix = %s\n", cnf->name_prefix);
  233. if (props->mclk_fs)
  234. dev_dbg(dev, "mclk-fs = %d\n", props->mclk_fs);
  235. }
  236. }
  237. #else
  238. #define asoc_simple_debug_info(priv)
  239. #endif /* DEBUG */
  240. #endif /* __SIMPLE_CARD_UTILS_H */