475f8cb144924032ba63d1334a3b3c7949e71019
[linux-2.6-microblaze.git] / include / sound / simple_card_utils.h
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_dai {
20         const char *name;
21         unsigned int sysclk;
22         int clk_direction;
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
30 struct asoc_simple_data {
31         u32 convert_rate;
32         u32 convert_channels;
33 };
34
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
41 struct prop_nums {
42         int cpus;
43         int codecs;
44         int platforms;
45 };
46
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;
52                 struct snd_soc_dai_link_component *cpus;
53                 struct snd_soc_dai_link_component *codecs;
54                 struct snd_soc_dai_link_component *platforms;
55                 struct asoc_simple_data adata;
56                 struct snd_soc_codec_conf *codec_conf;
57                 struct prop_nums num;
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;
64         struct snd_soc_dai_link_component *dlcs;
65         struct snd_soc_codec_conf *codec_conf;
66         struct gpio_desc *pa_gpio;
67         const struct snd_soc_ops *ops;
68         unsigned int dpcm_selectable:1;
69         unsigned int force_dpcm:1;
70 };
71 #define simple_priv_to_card(priv)       (&(priv)->snd_card)
72 #define simple_priv_to_props(priv, i)   ((priv)->dai_props + (i))
73 #define simple_priv_to_dev(priv)        (simple_priv_to_card(priv)->dev)
74 #define simple_priv_to_link(priv, i)    (simple_priv_to_card(priv)->dai_link + (i))
75
76 struct link_info {
77         int dais; /* number of dai  */
78         int link; /* number of link */
79         int conf; /* number of codec_conf */
80         int cpu;  /* turn for CPU / Codec */
81         struct prop_nums num[SNDRV_MINOR_DEVICES];
82 };
83
84 int asoc_simple_parse_daifmt(struct device *dev,
85                              struct device_node *node,
86                              struct device_node *codec,
87                              char *prefix,
88                              unsigned int *retfmt);
89 __printf(3, 4)
90 int asoc_simple_set_dailink_name(struct device *dev,
91                                  struct snd_soc_dai_link *dai_link,
92                                  const char *fmt, ...);
93 int asoc_simple_parse_card_name(struct snd_soc_card *card,
94                                 char *prefix);
95
96 #define asoc_simple_parse_clk_cpu(dev, node, dai_link, simple_dai)              \
97         asoc_simple_parse_clk(dev, node, simple_dai, dai_link->cpus)
98 #define asoc_simple_parse_clk_codec(dev, node, dai_link, simple_dai)    \
99         asoc_simple_parse_clk(dev, node, simple_dai, dai_link->codecs)
100 int asoc_simple_parse_clk(struct device *dev,
101                           struct device_node *node,
102                           struct asoc_simple_dai *simple_dai,
103                           struct snd_soc_dai_link_component *dlc);
104 int asoc_simple_startup(struct snd_pcm_substream *substream);
105 void asoc_simple_shutdown(struct snd_pcm_substream *substream);
106 int asoc_simple_hw_params(struct snd_pcm_substream *substream,
107                           struct snd_pcm_hw_params *params);
108 int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd);
109 int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
110                                    struct snd_pcm_hw_params *params);
111
112 #define asoc_simple_parse_cpu(node, dai_link, is_single_link)   \
113         asoc_simple_parse_dai(node, dai_link->cpus, is_single_link)
114 #define asoc_simple_parse_codec(node, dai_link) \
115         asoc_simple_parse_dai(node, dai_link->codecs, NULL)
116 #define asoc_simple_parse_platform(node, dai_link)      \
117         asoc_simple_parse_dai(node, dai_link->platforms, NULL)
118
119 #define asoc_simple_parse_tdm(np, dai)                  \
120         snd_soc_of_parse_tdm_slot(np,   &(dai)->tx_slot_mask,   \
121                                         &(dai)->rx_slot_mask,   \
122                                         &(dai)->slots,          \
123                                         &(dai)->slot_width);
124
125 void asoc_simple_canonicalize_platform(struct snd_soc_dai_link *dai_link);
126 void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
127                                       int is_single_links);
128
129 int asoc_simple_clean_reference(struct snd_soc_card *card);
130
131 void asoc_simple_convert_fixup(struct asoc_simple_data *data,
132                                       struct snd_pcm_hw_params *params);
133 void asoc_simple_parse_convert(struct device *dev,
134                                struct device_node *np, char *prefix,
135                                struct asoc_simple_data *data);
136
137 int asoc_simple_parse_routing(struct snd_soc_card *card,
138                                       char *prefix);
139 int asoc_simple_parse_widgets(struct snd_soc_card *card,
140                                       char *prefix);
141 int asoc_simple_parse_pin_switches(struct snd_soc_card *card,
142                                    char *prefix);
143
144 int asoc_simple_init_jack(struct snd_soc_card *card,
145                                struct asoc_simple_jack *sjack,
146                                int is_hp, char *prefix, char *pin);
147 int asoc_simple_init_priv(struct asoc_simple_priv *priv,
148                                struct link_info *li);
149
150 #ifdef DEBUG
151 static inline void asoc_simple_debug_dai(struct asoc_simple_priv *priv,
152                                          char *name,
153                                          struct asoc_simple_dai *dai)
154 {
155         struct device *dev = simple_priv_to_dev(priv);
156
157         /* dai might be NULL */
158         if (!dai)
159                 return;
160
161         if (dai->name)
162                 dev_dbg(dev, "%s dai name = %s\n",
163                         name, dai->name);
164         if (dai->sysclk)
165                 dev_dbg(dev, "%s sysclk = %d\n",
166                         name, dai->sysclk);
167
168         dev_dbg(dev, "%s direction = %s\n",
169                 name, dai->clk_direction ? "OUT" : "IN");
170
171         if (dai->slots)
172                 dev_dbg(dev, "%s slots = %d\n", name, dai->slots);
173         if (dai->slot_width)
174                 dev_dbg(dev, "%s slot width = %d\n", name, dai->slot_width);
175         if (dai->tx_slot_mask)
176                 dev_dbg(dev, "%s tx slot mask = %d\n", name, dai->tx_slot_mask);
177         if (dai->rx_slot_mask)
178                 dev_dbg(dev, "%s rx slot mask = %d\n", name, dai->rx_slot_mask);
179         if (dai->clk)
180                 dev_dbg(dev, "%s clk %luHz\n", name, clk_get_rate(dai->clk));
181 }
182
183 static inline void asoc_simple_debug_info(struct asoc_simple_priv *priv)
184 {
185         struct snd_soc_card *card = simple_priv_to_card(priv);
186         struct device *dev = simple_priv_to_dev(priv);
187
188         int i;
189
190         if (card->name)
191                 dev_dbg(dev, "Card Name: %s\n", card->name);
192
193         for (i = 0; i < card->num_links; i++) {
194                 struct simple_dai_props *props = simple_priv_to_props(priv, i);
195                 struct snd_soc_dai_link *link = simple_priv_to_link(priv, i);
196
197                 dev_dbg(dev, "DAI%d\n", i);
198
199                 asoc_simple_debug_dai(priv, "cpu", props->cpu_dai);
200                 asoc_simple_debug_dai(priv, "codec", props->codec_dai);
201
202                 if (link->name)
203                         dev_dbg(dev, "dai name = %s\n", link->name);
204
205                 dev_dbg(dev, "dai format = %04x\n", link->dai_fmt);
206
207                 if (props->adata.convert_rate)
208                         dev_dbg(dev, "convert_rate = %d\n",
209                                 props->adata.convert_rate);
210                 if (props->adata.convert_channels)
211                         dev_dbg(dev, "convert_channels = %d\n",
212                                 props->adata.convert_channels);
213                 if (props->codec_conf && props->codec_conf->name_prefix)
214                         dev_dbg(dev, "name prefix = %s\n",
215                                 props->codec_conf->name_prefix);
216                 if (props->mclk_fs)
217                         dev_dbg(dev, "mclk-fs = %d\n",
218                                 props->mclk_fs);
219         }
220 }
221 #else
222 #define  asoc_simple_debug_info(priv)
223 #endif /* DEBUG */
224
225 #endif /* __SIMPLE_CARD_UTILS_H */