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