Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-block.git] / include / sound / simple_card_utils.h
CommitLineData
d613a7f4
KM
1/* SPDX-License-Identifier: GPL-2.0
2 *
29a43aa9 3 * simple_card_utils.h
abd3147e
KM
4 *
5 * Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
abd3147e 6 */
d613a7f4 7
29a43aa9
KM
8#ifndef __SIMPLE_CARD_UTILS_H
9#define __SIMPLE_CARD_UTILS_H
abd3147e 10
4bbee14d 11#include <linux/clk.h>
abd3147e
KM
12#include <sound/soc.h>
13
b5a95c5b
KM
14#define simple_util_init_hp(card, sjack, prefix) \
15 simple_util_init_jack(card, sjack, 1, prefix, NULL)
16#define simple_util_init_mic(card, sjack, prefix) \
17 simple_util_init_jack(card, sjack, 0, prefix, NULL)
18
b5a95c5b 19struct simple_util_tdm_width_map {
1e974e5b
RF
20 u8 sample_bits;
21 u8 slot_count;
22 u16 slot_width;
23};
24
b5a95c5b 25struct simple_util_dai {
cecdef36
KM
26 const char *name;
27 unsigned int sysclk;
a728f560 28 int clk_direction;
cecdef36
KM
29 int slots;
30 int slot_width;
31 unsigned int tx_slot_mask;
32 unsigned int rx_slot_mask;
33 struct clk *clk;
5ca2ab45 34 bool clk_fixed;
b5a95c5b 35 struct simple_util_tdm_width_map *tdm_width_map;
1e974e5b 36 int n_tdm_widths;
cecdef36
KM
37};
38
b5a95c5b 39struct simple_util_data {
13bb1cc0
KM
40 u32 convert_rate;
41 u32 convert_channels;
047a0536 42 const char *convert_sample_format;
13bb1cc0
KM
43};
44
b5a95c5b 45struct simple_util_jack {
62c2c9fc
KS
46 struct snd_soc_jack jack;
47 struct snd_soc_jack_pin pin;
48 struct snd_soc_jack_gpio gpio;
49};
50
f2138aed
KM
51struct prop_nums {
52 int cpus;
53 int codecs;
54 int platforms;
55};
56
b5a95c5b 57struct simple_util_priv {
e59289cd
KM
58 struct snd_soc_card snd_card;
59 struct simple_dai_props {
b5a95c5b
KM
60 struct simple_util_dai *cpu_dai;
61 struct simple_util_dai *codec_dai;
62 struct simple_util_data adata;
e59289cd 63 struct snd_soc_codec_conf *codec_conf;
f2138aed 64 struct prop_nums num;
e59289cd
KM
65 unsigned int mclk_fs;
66 } *dai_props;
b5a95c5b
KM
67 struct simple_util_jack hp_jack;
68 struct simple_util_jack mic_jack;
9b271207 69 struct snd_soc_jack *aux_jacks;
e59289cd 70 struct snd_soc_dai_link *dai_link;
b5a95c5b 71 struct simple_util_dai *dais;
050c7950 72 struct snd_soc_dai_link_component *dlcs;
e59289cd
KM
73 struct snd_soc_codec_conf *codec_conf;
74 struct gpio_desc *pa_gpio;
d09c774f
SP
75 const struct snd_soc_ops *ops;
76 unsigned int dpcm_selectable:1;
77 unsigned int force_dpcm:1;
e59289cd
KM
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
9830d3e9
KM
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
04e97fa7
LM
92/* has the same effect as simple_priv_to_props(). Preferred over
93 * simple_priv_to_props() when dealing with PCM runtime data as
94 * the ID stored in rtd->id may not be a valid array index.
95 */
96#define runtime_simple_priv_to_props(priv, rtd) \
97 ((priv)->dai_props + ((rtd)->dai_link - (priv)->dai_link))
98
fafc05aa
KM
99#define for_each_prop_dlc_cpus(props, i, cpu) \
100 for ((i) = 0; \
9830d3e9
KM
101 ((i) < (props)->num.cpus) && \
102 ((cpu) = simple_props_to_dlc_cpu(props, i)); \
fafc05aa 103 (i)++)
9830d3e9 104#define for_each_prop_dlc_codecs(props, i, codec) \
fafc05aa 105 for ((i) = 0; \
9830d3e9
KM
106 ((i) < (props)->num.codecs) && \
107 ((codec) = simple_props_to_dlc_codec(props, i)); \
fafc05aa
KM
108 (i)++)
109#define for_each_prop_dlc_platforms(props, i, platform) \
110 for ((i) = 0; \
9830d3e9
KM
111 ((i) < (props)->num.platforms) && \
112 ((platform) = simple_props_to_dlc_platform(props, i)); \
fafc05aa
KM
113 (i)++)
114#define for_each_prop_codec_conf(props, i, conf) \
115 for ((i) = 0; \
116 ((i) < (props)->num.codecs) && \
117 (props)->codec_conf && \
9830d3e9 118 ((conf) = simple_props_to_codec_conf(props, i)); \
fafc05aa
KM
119 (i)++)
120
121#define for_each_prop_dai_cpu(props, i, cpu) \
122 for ((i) = 0; \
9830d3e9
KM
123 ((i) < (props)->num.cpus) && \
124 ((cpu) = simple_props_to_dai_cpu(props, i)); \
fafc05aa
KM
125 (i)++)
126#define for_each_prop_dai_codec(props, i, codec) \
127 for ((i) = 0; \
9830d3e9
KM
128 ((i) < (props)->num.codecs) && \
129 ((codec) = simple_props_to_dai_codec(props, i)); \
fafc05aa
KM
130 (i)++)
131
7a226f2e 132#define SNDRV_MAX_LINKS 512
343e55e7 133
65a5056b 134struct link_info {
65a5056b 135 int link; /* number of link */
65a5056b 136 int cpu; /* turn for CPU / Codec */
343e55e7 137 struct prop_nums num[SNDRV_MAX_LINKS];
65a5056b
KM
138};
139
b5a95c5b 140int simple_util_parse_daifmt(struct device *dev,
ad11e59f
KM
141 struct device_node *node,
142 struct device_node *codec,
143 char *prefix,
144 unsigned int *retfmt);
2d7395b2 145int simple_util_parse_tdm_width_map(struct simple_util_priv *priv, struct device_node *np,
b5a95c5b 146 struct simple_util_dai *dai);
1e974e5b 147
e5668cae 148__printf(3, 4)
2d7395b2 149int simple_util_set_dailink_name(struct simple_util_priv *priv,
ad11e59f
KM
150 struct snd_soc_dai_link *dai_link,
151 const char *fmt, ...);
2d7395b2 152int simple_util_parse_card_name(struct simple_util_priv *priv,
ad11e59f
KM
153 char *prefix);
154
b5a95c5b 155int simple_util_parse_clk(struct device *dev,
ad11e59f 156 struct device_node *node,
b5a95c5b 157 struct simple_util_dai *simple_dai,
ad11e59f 158 struct snd_soc_dai_link_component *dlc);
b5a95c5b
KM
159int simple_util_startup(struct snd_pcm_substream *substream);
160void simple_util_shutdown(struct snd_pcm_substream *substream);
161int simple_util_hw_params(struct snd_pcm_substream *substream,
f48dcbb6 162 struct snd_pcm_hw_params *params);
b5a95c5b
KM
163int simple_util_dai_init(struct snd_soc_pcm_runtime *rtd);
164int simple_util_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
629f7544 165 struct snd_pcm_hw_params *params);
bb6fc620 166
b5a95c5b 167#define simple_util_parse_tdm(np, dai) \
e68ba207
KM
168 snd_soc_of_parse_tdm_slot(np, &(dai)->tx_slot_mask, \
169 &(dai)->rx_slot_mask, \
170 &(dai)->slots, \
171 &(dai)->slot_width);
172
b5a95c5b 173void simple_util_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
c826ec03 174 struct snd_soc_dai_link_component *cpus);
b5a95c5b 175void simple_util_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
c826ec03 176 int is_single_links);
c262c9ab 177
b5a95c5b 178void simple_util_clean_reference(struct snd_soc_card *card);
0f4e0711 179
b5a95c5b
KM
180void simple_util_parse_convert(struct device_node *np, char *prefix,
181 struct simple_util_data *data);
182bool simple_util_is_convert_required(const struct simple_util_data *data);
13bb1cc0 183
8cb3aeeb
MK
184int simple_util_get_sample_fmt(struct simple_util_data *data);
185
b5a95c5b 186int simple_util_parse_routing(struct snd_soc_card *card,
33404f3f 187 char *prefix);
b5a95c5b 188int simple_util_parse_widgets(struct snd_soc_card *card,
b31f11d0 189 char *prefix);
b5a95c5b 190int simple_util_parse_pin_switches(struct snd_soc_card *card,
90194281 191 char *prefix);
3296d078 192
b5a95c5b
KM
193int simple_util_init_jack(struct snd_soc_card *card,
194 struct simple_util_jack *sjack,
764aafdb 195 int is_hp, char *prefix, char *pin);
b5a95c5b 196int simple_util_init_aux_jacks(struct simple_util_priv *priv,
9b271207 197 char *prefix);
b5a95c5b 198int simple_util_init_priv(struct simple_util_priv *priv,
65a5056b 199 struct link_info *li);
393df6f3 200void simple_util_remove(struct platform_device *pdev);
0580dde5 201
b5a95c5b
KM
202int graph_util_card_probe(struct snd_soc_card *card);
203int graph_util_is_ports0(struct device_node *port);
2d7395b2 204int graph_util_parse_dai(struct simple_util_priv *priv, struct device_node *ep,
90de551c 205 struct snd_soc_dai_link_component *dlc, int *is_single_link);
1a456b1c 206
45919c28 207void graph_util_parse_link_direction(struct device_node *np,
d29351e8 208 bool *is_playback_only, bool *is_capture_only);
5d9cacdc
KM
209void graph_util_parse_trigger_order(struct simple_util_priv *priv,
210 struct device_node *np,
211 enum snd_soc_trigger_order *trigger_start,
212 enum snd_soc_trigger_order *trigger_stop);
d29351e8 213
0580dde5 214#ifdef DEBUG
b5a95c5b 215static inline void simple_util_debug_dai(struct simple_util_priv *priv,
af621959 216 char *name,
b5a95c5b 217 struct simple_util_dai *dai)
0580dde5
KM
218{
219 struct device *dev = simple_priv_to_dev(priv);
220
52db6685
KM
221 /* dai might be NULL */
222 if (!dai)
223 return;
224
0580dde5
KM
225 if (dai->name)
226 dev_dbg(dev, "%s dai name = %s\n",
227 name, dai->name);
0580dde5
KM
228
229 if (dai->slots)
230 dev_dbg(dev, "%s slots = %d\n", name, dai->slots);
231 if (dai->slot_width)
232 dev_dbg(dev, "%s slot width = %d\n", name, dai->slot_width);
233 if (dai->tx_slot_mask)
234 dev_dbg(dev, "%s tx slot mask = %d\n", name, dai->tx_slot_mask);
235 if (dai->rx_slot_mask)
236 dev_dbg(dev, "%s rx slot mask = %d\n", name, dai->rx_slot_mask);
237 if (dai->clk)
238 dev_dbg(dev, "%s clk %luHz\n", name, clk_get_rate(dai->clk));
33cd6b19
KM
239 if (dai->sysclk)
240 dev_dbg(dev, "%s sysclk = %dHz\n",
241 name, dai->sysclk);
242 if (dai->clk || dai->sysclk)
243 dev_dbg(dev, "%s direction = %s\n",
244 name, dai->clk_direction ? "OUT" : "IN");
0580dde5
KM
245}
246
b5a95c5b 247static inline void simple_util_debug_info(struct simple_util_priv *priv)
0580dde5
KM
248{
249 struct snd_soc_card *card = simple_priv_to_card(priv);
250 struct device *dev = simple_priv_to_dev(priv);
251
252 int i;
253
254 if (card->name)
255 dev_dbg(dev, "Card Name: %s\n", card->name);
256
257 for (i = 0; i < card->num_links; i++) {
258 struct simple_dai_props *props = simple_priv_to_props(priv, i);
259 struct snd_soc_dai_link *link = simple_priv_to_link(priv, i);
b5a95c5b 260 struct simple_util_dai *dai;
fafc05aa
KM
261 struct snd_soc_codec_conf *cnf;
262 int j;
0580dde5
KM
263
264 dev_dbg(dev, "DAI%d\n", i);
265
40d8cbe7 266 dev_dbg(dev, "cpu num = %d\n", link->num_cpus);
fafc05aa 267 for_each_prop_dai_cpu(props, j, dai)
b5a95c5b 268 simple_util_debug_dai(priv, "cpu", dai);
40d8cbe7 269 dev_dbg(dev, "codec num = %d\n", link->num_codecs);
fafc05aa 270 for_each_prop_dai_codec(props, j, dai)
b5a95c5b 271 simple_util_debug_dai(priv, "codec", dai);
0580dde5
KM
272
273 if (link->name)
8c588fe9 274 dev_dbg(dev, "link name = %s\n", link->name);
ac813c62 275 if (link->dai_fmt)
8c588fe9 276 dev_dbg(dev, "link format = %04x\n", link->dai_fmt);
f1a92bb8
SG
277 if (link->playback_only)
278 dev_dbg(dev, "link has playback_only");
279 if (link->capture_only)
280 dev_dbg(dev, "link has capture_only");
0580dde5 281 if (props->adata.convert_rate)
3919249e 282 dev_dbg(dev, "convert_rate = %d\n", props->adata.convert_rate);
0580dde5 283 if (props->adata.convert_channels)
3919249e 284 dev_dbg(dev, "convert_channels = %d\n", props->adata.convert_channels);
fafc05aa
KM
285 for_each_prop_codec_conf(props, j, cnf)
286 if (cnf->name_prefix)
287 dev_dbg(dev, "name prefix = %s\n", cnf->name_prefix);
0580dde5 288 if (props->mclk_fs)
3919249e 289 dev_dbg(dev, "mclk-fs = %d\n", props->mclk_fs);
0580dde5
KM
290 }
291}
292#else
b5a95c5b 293#define simple_util_debug_info(priv)
0580dde5
KM
294#endif /* DEBUG */
295
29a43aa9 296#endif /* __SIMPLE_CARD_UTILS_H */