Linux Audio

Check our new training course

Loading...
v6.13.7
  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 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
 19struct simple_util_tdm_width_map {
 20	u8 sample_bits;
 21	u8 slot_count;
 22	u16 slot_width;
 23};
 24
 25struct simple_util_dai {
 26	const char *name;
 27	unsigned int sysclk;
 28	int clk_direction;
 29	int slots;
 30	int slot_width;
 31	unsigned int tx_slot_mask;
 32	unsigned int rx_slot_mask;
 33	struct clk *clk;
 34	bool clk_fixed;
 35	struct simple_util_tdm_width_map *tdm_width_map;
 36	int n_tdm_widths;
 37};
 38
 39struct simple_util_data {
 40	u32 convert_rate;
 41	u32 convert_channels;
 42	const char *convert_sample_format;
 43};
 44
 45struct simple_util_jack {
 46	struct snd_soc_jack jack;
 47	struct snd_soc_jack_pin pin;
 48	struct snd_soc_jack_gpio gpio;
 49};
 50
 51struct prop_nums {
 52	int cpus;
 53	int codecs;
 54	int platforms;
 55};
 56
 57struct simple_util_priv {
 58	struct snd_soc_card snd_card;
 59	struct simple_dai_props {
 60		struct simple_util_dai *cpu_dai;
 61		struct simple_util_dai *codec_dai;
 62		struct simple_util_data adata;
 
 
 
 63		struct snd_soc_codec_conf *codec_conf;
 64		struct prop_nums num;
 65		unsigned int mclk_fs;
 66	} *dai_props;
 67	struct simple_util_jack hp_jack;
 68	struct simple_util_jack mic_jack;
 69	struct snd_soc_jack *aux_jacks;
 70	struct snd_soc_dai_link *dai_link;
 71	struct simple_util_dai *dais;
 72	struct snd_soc_dai_link_component *dlcs;
 
 73	struct snd_soc_codec_conf *codec_conf;
 74	struct gpio_desc *pa_gpio;
 75	const struct snd_soc_ops *ops;
 76	unsigned int dpcm_selectable:1;
 77	unsigned int force_dpcm:1;
 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
 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
 92#define for_each_prop_dlc_cpus(props, i, cpu)				\
 93	for ((i) = 0;							\
 94	     ((i) < (props)->num.cpus) &&				\
 95		     ((cpu) = simple_props_to_dlc_cpu(props, i));	\
 96	     (i)++)
 97#define for_each_prop_dlc_codecs(props, i, codec)			\
 98	for ((i) = 0;							\
 99	     ((i) < (props)->num.codecs) &&				\
100		     ((codec) = simple_props_to_dlc_codec(props, i));	\
101	     (i)++)
102#define for_each_prop_dlc_platforms(props, i, platform)			\
103	for ((i) = 0;							\
104	     ((i) < (props)->num.platforms) &&				\
105		     ((platform) = simple_props_to_dlc_platform(props, i)); \
106	     (i)++)
107#define for_each_prop_codec_conf(props, i, conf)			\
108	for ((i) = 0;							\
109	     ((i) < (props)->num.codecs) &&				\
110		     (props)->codec_conf &&				\
111		     ((conf) = simple_props_to_codec_conf(props, i));	\
112	     (i)++)
113
114#define for_each_prop_dai_cpu(props, i, cpu)				\
115	for ((i) = 0;							\
116	     ((i) < (props)->num.cpus) &&				\
117		     ((cpu) = simple_props_to_dai_cpu(props, i));	\
118	     (i)++)
119#define for_each_prop_dai_codec(props, i, codec)			\
120	for ((i) = 0;							\
121	     ((i) < (props)->num.codecs) &&				\
122		     ((codec) = simple_props_to_dai_codec(props, i));	\
123	     (i)++)
124
125#define SNDRV_MAX_LINKS 512
126
127struct link_info {
128	int link; /* number of link */
129	int cpu;  /* turn for CPU / Codec */
130	struct prop_nums num[SNDRV_MAX_LINKS];
131};
132
133int simple_util_parse_daifmt(struct device *dev,
134			     struct device_node *node,
135			     struct device_node *codec,
136			     char *prefix,
137			     unsigned int *retfmt);
138int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np,
139				    struct simple_util_dai *dai);
140
141__printf(3, 4)
142int simple_util_set_dailink_name(struct device *dev,
143				 struct snd_soc_dai_link *dai_link,
144				 const char *fmt, ...);
145int simple_util_parse_card_name(struct snd_soc_card *card,
146				char *prefix);
147
148int simple_util_parse_clk(struct device *dev,
149			  struct device_node *node,
150			  struct simple_util_dai *simple_dai,
151			  struct snd_soc_dai_link_component *dlc);
152int simple_util_startup(struct snd_pcm_substream *substream);
153void simple_util_shutdown(struct snd_pcm_substream *substream);
154int simple_util_hw_params(struct snd_pcm_substream *substream,
155			  struct snd_pcm_hw_params *params);
156int simple_util_dai_init(struct snd_soc_pcm_runtime *rtd);
157int simple_util_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
158				   struct snd_pcm_hw_params *params);
159
160#define simple_util_parse_tdm(np, dai)				\
161	snd_soc_of_parse_tdm_slot(np,	&(dai)->tx_slot_mask,	\
162					&(dai)->rx_slot_mask,	\
163					&(dai)->slots,		\
164					&(dai)->slot_width);
165
166void simple_util_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
167				       struct snd_soc_dai_link_component *cpus);
168void simple_util_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
169				  int is_single_links);
170
171void simple_util_clean_reference(struct snd_soc_card *card);
172
173void simple_util_parse_convert(struct device_node *np, char *prefix,
174			       struct simple_util_data *data);
175bool simple_util_is_convert_required(const struct simple_util_data *data);
 
176
177int simple_util_get_sample_fmt(struct simple_util_data *data);
178
179int simple_util_parse_routing(struct snd_soc_card *card,
180				      char *prefix);
181int simple_util_parse_widgets(struct snd_soc_card *card,
182				      char *prefix);
183int simple_util_parse_pin_switches(struct snd_soc_card *card,
184				   char *prefix);
185
186int simple_util_init_jack(struct snd_soc_card *card,
187			       struct simple_util_jack *sjack,
188			       int is_hp, char *prefix, char *pin);
189int simple_util_init_aux_jacks(struct simple_util_priv *priv,
190				char *prefix);
191int simple_util_init_priv(struct simple_util_priv *priv,
192			       struct link_info *li);
193void simple_util_remove(struct platform_device *pdev);
194
195int graph_util_card_probe(struct snd_soc_card *card);
196int graph_util_is_ports0(struct device_node *port);
197int graph_util_parse_dai(struct device *dev, struct device_node *ep,
198			 struct snd_soc_dai_link_component *dlc, int *is_single_link);
199
200void graph_util_parse_link_direction(struct device_node *np,
201				    bool *is_playback_only, bool *is_capture_only);
202void graph_util_parse_trigger_order(struct simple_util_priv *priv,
203				    struct device_node *np,
204				    enum snd_soc_trigger_order *trigger_start,
205				    enum snd_soc_trigger_order *trigger_stop);
206
207#ifdef DEBUG
208static inline void simple_util_debug_dai(struct simple_util_priv *priv,
209					 char *name,
210					 struct simple_util_dai *dai)
211{
212	struct device *dev = simple_priv_to_dev(priv);
213
214	/* dai might be NULL */
215	if (!dai)
216		return;
217
218	if (dai->name)
219		dev_dbg(dev, "%s dai name = %s\n",
220			name, dai->name);
221
222	if (dai->slots)
223		dev_dbg(dev, "%s slots = %d\n", name, dai->slots);
224	if (dai->slot_width)
225		dev_dbg(dev, "%s slot width = %d\n", name, dai->slot_width);
226	if (dai->tx_slot_mask)
227		dev_dbg(dev, "%s tx slot mask = %d\n", name, dai->tx_slot_mask);
228	if (dai->rx_slot_mask)
229		dev_dbg(dev, "%s rx slot mask = %d\n", name, dai->rx_slot_mask);
230	if (dai->clk)
231		dev_dbg(dev, "%s clk %luHz\n", name, clk_get_rate(dai->clk));
232	if (dai->sysclk)
233		dev_dbg(dev, "%s sysclk = %dHz\n",
234			name, dai->sysclk);
235	if (dai->clk || dai->sysclk)
236		dev_dbg(dev, "%s direction = %s\n",
237			name, dai->clk_direction ? "OUT" : "IN");
238}
239
240static inline void simple_util_debug_info(struct simple_util_priv *priv)
241{
242	struct snd_soc_card *card = simple_priv_to_card(priv);
243	struct device *dev = simple_priv_to_dev(priv);
244
245	int i;
246
247	if (card->name)
248		dev_dbg(dev, "Card Name: %s\n", card->name);
249
250	for (i = 0; i < card->num_links; i++) {
251		struct simple_dai_props *props = simple_priv_to_props(priv, i);
252		struct snd_soc_dai_link *link = simple_priv_to_link(priv, i);
253		struct simple_util_dai *dai;
254		struct snd_soc_codec_conf *cnf;
255		int j;
256
257		dev_dbg(dev, "DAI%d\n", i);
258
259		dev_dbg(dev, "cpu num = %d\n", link->num_cpus);
260		for_each_prop_dai_cpu(props, j, dai)
261			simple_util_debug_dai(priv, "cpu", dai);
262		dev_dbg(dev, "codec num = %d\n", link->num_codecs);
263		for_each_prop_dai_codec(props, j, dai)
264			simple_util_debug_dai(priv, "codec", dai);
265
266		if (link->name)
267			dev_dbg(dev, "dai name = %s\n", link->name);
268		if (link->dai_fmt)
269			dev_dbg(dev, "dai format = %04x\n", link->dai_fmt);
270		if (props->adata.convert_rate)
271			dev_dbg(dev, "convert_rate = %d\n", props->adata.convert_rate);
272		if (props->adata.convert_channels)
273			dev_dbg(dev, "convert_channels = %d\n", props->adata.convert_channels);
274		for_each_prop_codec_conf(props, j, cnf)
275			if (cnf->name_prefix)
276				dev_dbg(dev, "name prefix = %s\n", cnf->name_prefix);
277		if (props->mclk_fs)
278			dev_dbg(dev, "mclk-fs = %d\n", props->mclk_fs);
279	}
280}
281#else
282#define  simple_util_debug_info(priv)
283#endif /* DEBUG */
284
285#endif /* __SIMPLE_CARD_UTILS_H */
v5.14.15
  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
 19struct 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
 30struct asoc_simple_data {
 31	u32 convert_rate;
 32	u32 convert_channels;
 
 33};
 34
 35struct 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
 41struct prop_nums {
 42	int cpus;
 43	int codecs;
 44	int platforms;
 45};
 46
 47struct 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_dai_link_component dummy;
 66	struct snd_soc_codec_conf *codec_conf;
 67	struct gpio_desc *pa_gpio;
 68	const struct snd_soc_ops *ops;
 69	unsigned int dpcm_selectable:1;
 70	unsigned int force_dpcm:1;
 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
 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
 85#define for_each_prop_dlc_cpus(props, i, cpu)				\
 86	for ((i) = 0;							\
 87	     ((i) < (props)->num.cpus) &&				\
 88		     ((cpu) = simple_props_to_dlc_cpu(props, i));	\
 89	     (i)++)
 90#define for_each_prop_dlc_codecs(props, i, codec)			\
 91	for ((i) = 0;							\
 92	     ((i) < (props)->num.codecs) &&				\
 93		     ((codec) = simple_props_to_dlc_codec(props, i));	\
 94	     (i)++)
 95#define for_each_prop_dlc_platforms(props, i, platform)			\
 96	for ((i) = 0;							\
 97	     ((i) < (props)->num.platforms) &&				\
 98		     ((platform) = simple_props_to_dlc_platform(props, i)); \
 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 &&				\
104		     ((conf) = simple_props_to_codec_conf(props, i));	\
105	     (i)++)
106
107#define for_each_prop_dai_cpu(props, i, cpu)				\
108	for ((i) = 0;							\
109	     ((i) < (props)->num.cpus) &&				\
110		     ((cpu) = simple_props_to_dai_cpu(props, i));	\
111	     (i)++)
112#define for_each_prop_dai_codec(props, i, codec)			\
113	for ((i) = 0;							\
114	     ((i) < (props)->num.codecs) &&				\
115		     ((codec) = simple_props_to_dai_codec(props, i));	\
116	     (i)++)
117
118#define SNDRV_MAX_LINKS 128
119
120struct link_info {
121	int link; /* number of link */
122	int cpu;  /* turn for CPU / Codec */
123	struct prop_nums num[SNDRV_MAX_LINKS];
124};
125
126int asoc_simple_parse_daifmt(struct device *dev,
127			     struct device_node *node,
128			     struct device_node *codec,
129			     char *prefix,
130			     unsigned int *retfmt);
 
 
 
131__printf(3, 4)
132int asoc_simple_set_dailink_name(struct device *dev,
133				 struct snd_soc_dai_link *dai_link,
134				 const char *fmt, ...);
135int asoc_simple_parse_card_name(struct snd_soc_card *card,
136				char *prefix);
137
138int asoc_simple_parse_clk(struct device *dev,
139			  struct device_node *node,
140			  struct asoc_simple_dai *simple_dai,
141			  struct snd_soc_dai_link_component *dlc);
142int asoc_simple_startup(struct snd_pcm_substream *substream);
143void asoc_simple_shutdown(struct snd_pcm_substream *substream);
144int asoc_simple_hw_params(struct snd_pcm_substream *substream,
145			  struct snd_pcm_hw_params *params);
146int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd);
147int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
148				   struct snd_pcm_hw_params *params);
149
150#define asoc_simple_parse_tdm(np, dai)			\
151	snd_soc_of_parse_tdm_slot(np,	&(dai)->tx_slot_mask,	\
152					&(dai)->rx_slot_mask,	\
153					&(dai)->slots,		\
154					&(dai)->slot_width);
155
156void asoc_simple_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
157				       struct snd_soc_dai_link_component *cpus);
158void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
159				  int is_single_links);
160
161int asoc_simple_clean_reference(struct snd_soc_card *card);
162
163void asoc_simple_convert_fixup(struct asoc_simple_data *data,
164				      struct snd_pcm_hw_params *params);
165void asoc_simple_parse_convert(struct device_node *np, char *prefix,
166			       struct asoc_simple_data *data);
167
168int asoc_simple_parse_routing(struct snd_soc_card *card,
 
 
169				      char *prefix);
170int asoc_simple_parse_widgets(struct snd_soc_card *card,
171				      char *prefix);
172int asoc_simple_parse_pin_switches(struct snd_soc_card *card,
173				   char *prefix);
174
175int asoc_simple_init_jack(struct snd_soc_card *card,
176			       struct asoc_simple_jack *sjack,
177			       int is_hp, char *prefix, char *pin);
178int asoc_simple_init_priv(struct asoc_simple_priv *priv,
 
 
179			       struct link_info *li);
180int asoc_simple_remove(struct platform_device *pdev);
181
182int asoc_graph_card_probe(struct snd_soc_card *card);
 
 
 
 
 
 
 
 
 
 
183
184#ifdef DEBUG
185static inline void asoc_simple_debug_dai(struct asoc_simple_priv *priv,
186					 char *name,
187					 struct asoc_simple_dai *dai)
188{
189	struct device *dev = simple_priv_to_dev(priv);
190
191	/* dai might be NULL */
192	if (!dai)
193		return;
194
195	if (dai->name)
196		dev_dbg(dev, "%s dai name = %s\n",
197			name, dai->name);
198
199	if (dai->slots)
200		dev_dbg(dev, "%s slots = %d\n", name, dai->slots);
201	if (dai->slot_width)
202		dev_dbg(dev, "%s slot width = %d\n", name, dai->slot_width);
203	if (dai->tx_slot_mask)
204		dev_dbg(dev, "%s tx slot mask = %d\n", name, dai->tx_slot_mask);
205	if (dai->rx_slot_mask)
206		dev_dbg(dev, "%s rx slot mask = %d\n", name, dai->rx_slot_mask);
207	if (dai->clk)
208		dev_dbg(dev, "%s clk %luHz\n", name, clk_get_rate(dai->clk));
209	if (dai->sysclk)
210		dev_dbg(dev, "%s sysclk = %dHz\n",
211			name, dai->sysclk);
212	if (dai->clk || dai->sysclk)
213		dev_dbg(dev, "%s direction = %s\n",
214			name, dai->clk_direction ? "OUT" : "IN");
215}
216
217static inline void asoc_simple_debug_info(struct asoc_simple_priv *priv)
218{
219	struct snd_soc_card *card = simple_priv_to_card(priv);
220	struct device *dev = simple_priv_to_dev(priv);
221
222	int i;
223
224	if (card->name)
225		dev_dbg(dev, "Card Name: %s\n", card->name);
226
227	for (i = 0; i < card->num_links; i++) {
228		struct simple_dai_props *props = simple_priv_to_props(priv, i);
229		struct snd_soc_dai_link *link = simple_priv_to_link(priv, i);
230		struct asoc_simple_dai *dai;
231		struct snd_soc_codec_conf *cnf;
232		int j;
233
234		dev_dbg(dev, "DAI%d\n", i);
235
236		dev_dbg(dev, "cpu num = %d\n", link->num_cpus);
237		for_each_prop_dai_cpu(props, j, dai)
238			asoc_simple_debug_dai(priv, "cpu", dai);
239		dev_dbg(dev, "codec num = %d\n", link->num_codecs);
240		for_each_prop_dai_codec(props, j, dai)
241			asoc_simple_debug_dai(priv, "codec", dai);
242
243		if (link->name)
244			dev_dbg(dev, "dai name = %s\n", link->name);
245		if (link->dai_fmt)
246			dev_dbg(dev, "dai format = %04x\n", link->dai_fmt);
247		if (props->adata.convert_rate)
248			dev_dbg(dev, "convert_rate = %d\n", props->adata.convert_rate);
249		if (props->adata.convert_channels)
250			dev_dbg(dev, "convert_channels = %d\n", props->adata.convert_channels);
251		for_each_prop_codec_conf(props, j, cnf)
252			if (cnf->name_prefix)
253				dev_dbg(dev, "name prefix = %s\n", cnf->name_prefix);
254		if (props->mclk_fs)
255			dev_dbg(dev, "mclk-fs = %d\n", props->mclk_fs);
256	}
257}
258#else
259#define  asoc_simple_debug_info(priv)
260#endif /* DEBUG */
261
262#endif /* __SIMPLE_CARD_UTILS_H */