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