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