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