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