| 1 | |
| 2 | *
|
| 3 | * simple_card_utils.h
|
| 4 | *
|
| 5 | * Copyright (c) 2016 Kuninori Morimoto <[email protected]>
|
| 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 |
|
| 19 | struct simple_util_tdm_width_map {
|
| 20 | u8 sample_bits;
|
| 21 | u8 slot_count;
|
| 22 | u16 slot_width;
|
| 23 | };
|
| 24 |
|
| 25 | struct 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 |
|
| 39 | struct simple_util_data {
|
| 40 | u32 convert_rate;
|
| 41 | u32 convert_channels;
|
| 42 | const char *convert_sample_format;
|
| 43 | };
|
| 44 |
|
| 45 | struct 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 |
|
| 51 | struct prop_nums {
|
| 52 | int cpus;
|
| 53 | int codecs;
|
| 54 | int platforms;
|
| 55 | };
|
| 56 |
|
| 57 | enum simple_util_sysclk_order {
|
| 58 | SIMPLE_SYSCLK_ORDER_CODEC_FIRST = 0,
|
| 59 | SIMPLE_SYSCLK_ORDER_CPU_FIRST,
|
| 60 | };
|
| 61 |
|
| 62 | struct simple_util_priv {
|
| 63 | struct snd_soc_card snd_card;
|
| 64 | struct simple_dai_props {
|
| 65 | struct simple_util_dai *cpu_dai;
|
| 66 | struct simple_util_dai *codec_dai;
|
| 67 | struct simple_util_data adata;
|
| 68 | struct snd_soc_codec_conf *codec_conf;
|
| 69 | struct prop_nums num;
|
| 70 | unsigned int mclk_fs;
|
| 71 | enum simple_util_sysclk_order sysclk_order;
|
| 72 | } *dai_props;
|
| 73 | struct simple_util_jack hp_jack;
|
| 74 | struct simple_util_jack mic_jack;
|
| 75 | struct snd_soc_jack *aux_jacks;
|
| 76 | struct snd_soc_dai_link *dai_link;
|
| 77 | struct simple_util_dai *dais;
|
| 78 | struct snd_soc_dai_link_component *dlcs;
|
| 79 | struct snd_soc_codec_conf *codec_conf;
|
| 80 | struct gpio_desc *pa_gpio;
|
| 81 | const struct snd_soc_ops *ops;
|
| 82 | unsigned int dpcm_selectable:1;
|
| 83 | unsigned int force_dpcm:1;
|
| 84 | };
|
| 85 | #define simple_priv_to_card(priv) (&(priv)->snd_card)
|
| 86 | #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i))
|
| 87 | #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev)
|
| 88 | #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i))
|
| 89 |
|
| 90 | #define simple_props_to_dlc_cpu(props, i) ((props)->cpus + i)
|
| 91 | #define simple_props_to_dlc_codec(props, i) ((props)->codecs + i)
|
| 92 | #define simple_props_to_dlc_platform(props, i) ((props)->platforms + i)
|
| 93 |
|
| 94 | #define simple_props_to_dai_cpu(props, i) ((props)->cpu_dai + i)
|
| 95 | #define simple_props_to_dai_codec(props, i) ((props)->codec_dai + i)
|
| 96 | #define simple_props_to_codec_conf(props, i) ((props)->codec_conf + i)
|
| 97 |
|
| 98 | |
| 99 | * simple_priv_to_props() when dealing with PCM runtime data as
|
| 100 | * the ID stored in rtd->id may not be a valid array index.
|
| 101 |
|
| 102 | #define runtime_simple_priv_to_props(priv, rtd) \
|
| 103 | ((priv)->dai_props + ((rtd)->dai_link - (priv)->dai_link))
|
| 104 |
|
| 105 | #define for_each_prop_dlc_cpus(props, i, cpu) \
|
| 106 | for ((i) = 0; \
|
| 107 | ((i) < (props)->num.cpus) && \
|
| 108 | ((cpu) = simple_props_to_dlc_cpu(props, i)); \
|
| 109 | (i)++)
|
| 110 | #define for_each_prop_dlc_codecs(props, i, codec) \
|
| 111 | for ((i) = 0; \
|
| 112 | ((i) < (props)->num.codecs) && \
|
| 113 | ((codec) = simple_props_to_dlc_codec(props, i)); \
|
| 114 | (i)++)
|
| 115 | #define for_each_prop_dlc_platforms(props, i, platform) \
|
| 116 | for ((i) = 0; \
|
| 117 | ((i) < (props)->num.platforms) && \
|
| 118 | ((platform) = simple_props_to_dlc_platform(props, i)); \
|
| 119 | (i)++)
|
| 120 | #define for_each_prop_codec_conf(props, i, conf) \
|
| 121 | for ((i) = 0; \
|
| 122 | ((i) < (props)->num.codecs) && \
|
| 123 | (props)->codec_conf && \
|
| 124 | ((conf) = simple_props_to_codec_conf(props, i)); \
|
| 125 | (i)++)
|
| 126 |
|
| 127 | #define for_each_prop_dai_cpu(props, i, cpu) \
|
| 128 | for ((i) = 0; \
|
| 129 | ((i) < (props)->num.cpus) && \
|
| 130 | ((cpu) = simple_props_to_dai_cpu(props, i)); \
|
| 131 | (i)++)
|
| 132 | #define for_each_prop_dai_codec(props, i, codec) \
|
| 133 | for ((i) = 0; \
|
| 134 | ((i) < (props)->num.codecs) && \
|
| 135 | ((codec) = simple_props_to_dai_codec(props, i)); \
|
| 136 | (i)++)
|
| 137 |
|
| 138 | #define SNDRV_MAX_LINKS 512
|
| 139 |
|
| 140 | struct link_info {
|
| 141 | int link;
|
| 142 | int cpu;
|
| 143 | struct prop_nums num[SNDRV_MAX_LINKS];
|
| 144 | };
|
| 145 |
|
| 146 | int simple_util_parse_daifmt(struct device *dev,
|
| 147 | struct device_node *node,
|
| 148 | struct device_node *codec,
|
| 149 | char *prefix,
|
| 150 | unsigned int *retfmt);
|
| 151 | int simple_util_parse_tdm_width_map(struct simple_util_priv *priv, struct device_node *np,
|
| 152 | struct simple_util_dai *dai);
|
| 153 |
|
| 154 | __printf(3, 4)
|
| 155 | int simple_util_set_dailink_name(struct simple_util_priv *priv,
|
| 156 | struct snd_soc_dai_link *dai_link,
|
| 157 | const char *fmt, ...);
|
| 158 | int simple_util_parse_card_name(struct simple_util_priv *priv,
|
| 159 | char *prefix);
|
| 160 |
|
| 161 | int simple_util_parse_clk(struct device *dev,
|
| 162 | struct device_node *node,
|
| 163 | struct simple_util_dai *simple_dai,
|
| 164 | struct snd_soc_dai_link_component *dlc);
|
| 165 | int simple_util_startup(struct snd_pcm_substream *substream);
|
| 166 | void simple_util_shutdown(struct snd_pcm_substream *substream);
|
| 167 | int simple_util_hw_params(struct snd_pcm_substream *substream,
|
| 168 | struct snd_pcm_hw_params *params);
|
| 169 | int simple_util_dai_init(struct snd_soc_pcm_runtime *rtd);
|
| 170 | int simple_util_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
|
| 171 | struct snd_pcm_hw_params *params);
|
| 172 |
|
| 173 | #define simple_util_parse_tdm(np, dai) \
|
| 174 | snd_soc_of_parse_tdm_slot(np, &(dai)->tx_slot_mask, \
|
| 175 | &(dai)->rx_slot_mask, \
|
| 176 | &(dai)->slots, \
|
| 177 | &(dai)->slot_width);
|
| 178 |
|
| 179 | void simple_util_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
|
| 180 | struct snd_soc_dai_link_component *cpus);
|
| 181 | void simple_util_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
|
| 182 | int is_single_links);
|
| 183 |
|
| 184 | void simple_util_clean_reference(struct simple_util_priv *priv);
|
| 185 |
|
| 186 | void simple_util_parse_convert(struct device_node *np, char *prefix,
|
| 187 | struct simple_util_data *data);
|
| 188 | bool simple_util_is_convert_required(const struct simple_util_data *data);
|
| 189 |
|
| 190 | int simple_util_get_sample_fmt(struct simple_util_data *data);
|
| 191 |
|
| 192 | int simple_util_parse_property(struct simple_util_priv *priv,
|
| 193 | int (*func)(struct snd_soc_card *card, const char *propname),
|
| 194 | char *prefix, char *property);
|
| 195 | static inline int simple_util_parse_routing(struct simple_util_priv *priv, char *prefix)
|
| 196 | {
|
| 197 | return simple_util_parse_property(priv, snd_soc_of_parse_audio_routing,
|
| 198 | prefix, "routing");
|
| 199 | }
|
| 200 |
|
| 201 | static inline int simple_util_parse_widgets(struct simple_util_priv *priv, char *prefix)
|
| 202 | {
|
| 203 | return simple_util_parse_property(priv, snd_soc_of_parse_audio_simple_widgets,
|
| 204 | prefix, "widgets");
|
| 205 | }
|
| 206 |
|
| 207 | static inline int simple_util_parse_pin_switches(struct simple_util_priv *priv, char *prefix)
|
| 208 | {
|
| 209 | return simple_util_parse_property(priv, snd_soc_of_parse_pin_switches,
|
| 210 | prefix, "pin-switches");
|
| 211 | }
|
| 212 |
|
| 213 | static inline int simple_util_parse_aux_devs(struct simple_util_priv *priv, char *prefix)
|
| 214 | {
|
| 215 | return simple_util_parse_property(priv, snd_soc_of_parse_aux_devs,
|
| 216 | prefix, "aux-devs");
|
| 217 | }
|
| 218 |
|
| 219 | int simple_util_init_jack(struct snd_soc_card *card,
|
| 220 | struct simple_util_jack *sjack,
|
| 221 | int is_hp, char *prefix, char *pin);
|
| 222 | int simple_util_init_aux_jacks(struct snd_soc_card *card, char *prefix);
|
| 223 | int simple_util_init_priv(struct simple_util_priv *priv,
|
| 224 | struct link_info *li);
|
| 225 | void simple_util_remove(struct platform_device *pdev);
|
| 226 |
|
| 227 | int graph_util_card_probe(struct snd_soc_card *card);
|
| 228 | int graph_util_is_ports0(struct device_node *port);
|
| 229 | int graph_util_parse_dai(struct simple_util_priv *priv, struct device_node *ep,
|
| 230 | struct snd_soc_dai_link_component *dlc, int *is_single_link);
|
| 231 |
|
| 232 | void graph_util_parse_link_direction(struct device_node *np,
|
| 233 | bool *is_playback_only, bool *is_capture_only);
|
| 234 | void graph_util_parse_trigger_order(struct simple_util_priv *priv,
|
| 235 | struct device_node *np,
|
| 236 | enum snd_soc_trigger_order *trigger_start,
|
| 237 | enum snd_soc_trigger_order *trigger_stop);
|
| 238 |
|
| 239 | #ifdef DEBUG
|
| 240 | static inline void simple_util_debug_dai(struct simple_util_priv *priv,
|
| 241 | char *name,
|
| 242 | struct simple_util_dai *dai)
|
| 243 | {
|
| 244 | struct device *dev = simple_priv_to_dev(priv);
|
| 245 |
|
| 246 |
|
| 247 | if (!dai)
|
| 248 | return;
|
| 249 |
|
| 250 | if (dai->name)
|
| 251 | dev_dbg(dev, "%s dai name = %s\n",
|
| 252 | name, dai->name);
|
| 253 |
|
| 254 | if (dai->slots)
|
| 255 | dev_dbg(dev, "%s slots = %d\n", name, dai->slots);
|
| 256 | if (dai->slot_width)
|
| 257 | dev_dbg(dev, "%s slot width = %d\n", name, dai->slot_width);
|
| 258 | if (dai->tx_slot_mask)
|
| 259 | dev_dbg(dev, "%s tx slot mask = %d\n", name, dai->tx_slot_mask);
|
| 260 | if (dai->rx_slot_mask)
|
| 261 | dev_dbg(dev, "%s rx slot mask = %d\n", name, dai->rx_slot_mask);
|
| 262 | if (dai->clk)
|
| 263 | dev_dbg(dev, "%s clk %luHz\n", name, clk_get_rate(dai->clk));
|
| 264 | if (dai->sysclk)
|
| 265 | dev_dbg(dev, "%s sysclk = %dHz\n",
|
| 266 | name, dai->sysclk);
|
| 267 | if (dai->clk || dai->sysclk)
|
| 268 | dev_dbg(dev, "%s direction = %s\n",
|
| 269 | name, dai->clk_direction ? "OUT" : "IN");
|
| 270 | }
|
| 271 |
|
| 272 | static inline void simple_util_debug_info(struct simple_util_priv *priv)
|
| 273 | {
|
| 274 | struct snd_soc_card *card = simple_priv_to_card(priv);
|
| 275 | struct device *dev = simple_priv_to_dev(priv);
|
| 276 |
|
| 277 | int i;
|
| 278 |
|
| 279 | if (card->name)
|
| 280 | dev_dbg(dev, "Card Name: %s\n", card->name);
|
| 281 |
|
| 282 | for (i = 0; i < card->num_links; i++) {
|
| 283 | struct simple_dai_props *props = simple_priv_to_props(priv, i);
|
| 284 | struct snd_soc_dai_link *link = simple_priv_to_link(priv, i);
|
| 285 | struct simple_util_dai *dai;
|
| 286 | struct snd_soc_codec_conf *cnf;
|
| 287 | int j;
|
| 288 |
|
| 289 | dev_dbg(dev, "DAI%d\n", i);
|
| 290 |
|
| 291 | dev_dbg(dev, "cpu num = %d\n", link->num_cpus);
|
| 292 | for_each_prop_dai_cpu(props, j, dai)
|
| 293 | simple_util_debug_dai(priv, "cpu", dai);
|
| 294 | dev_dbg(dev, "codec num = %d\n", link->num_codecs);
|
| 295 | for_each_prop_dai_codec(props, j, dai)
|
| 296 | simple_util_debug_dai(priv, "codec", dai);
|
| 297 |
|
| 298 | if (link->name)
|
| 299 | dev_dbg(dev, "link name = %s\n", link->name);
|
| 300 | if (link->dai_fmt)
|
| 301 | dev_dbg(dev, "link format = %04x\n", link->dai_fmt);
|
| 302 | if (link->playback_only)
|
| 303 | dev_dbg(dev, "link has playback_only");
|
| 304 | if (link->capture_only)
|
| 305 | dev_dbg(dev, "link has capture_only");
|
| 306 | if (props->adata.convert_rate)
|
| 307 | dev_dbg(dev, "convert_rate = %d\n", props->adata.convert_rate);
|
| 308 | if (props->adata.convert_channels)
|
| 309 | dev_dbg(dev, "convert_channels = %d\n", props->adata.convert_channels);
|
| 310 | for_each_prop_codec_conf(props, j, cnf)
|
| 311 | if (cnf->name_prefix)
|
| 312 | dev_dbg(dev, "name prefix = %s\n", cnf->name_prefix);
|
| 313 | if (props->mclk_fs)
|
| 314 | dev_dbg(dev, "mclk-fs = %d\n", props->mclk_fs);
|
| 315 | }
|
| 316 | }
|
| 317 | #else
|
| 318 | #define simple_util_debug_info(priv)
|
| 319 | #endif
|
| 320 |
|
| 321 | #endif
|