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