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