Kuninori Morimoto | d613a7f | 2018-07-02 06:30:44 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 |
| 2 | * |
Kuninori Morimoto | 29a43aa | 2016-12-02 05:27:30 +0000 | [diff] [blame] | 3 | * simple_card_utils.h |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 6 | */ |
Kuninori Morimoto | d613a7f | 2018-07-02 06:30:44 +0000 | [diff] [blame] | 7 | |
Kuninori Morimoto | 29a43aa | 2016-12-02 05:27:30 +0000 | [diff] [blame] | 8 | #ifndef __SIMPLE_CARD_UTILS_H |
| 9 | #define __SIMPLE_CARD_UTILS_H |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 10 | |
Daniel Baluta | 4bbee14 | 2019-10-09 18:36:14 +0300 | [diff] [blame] | 11 | #include <linux/clk.h> |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 12 | #include <sound/soc.h> |
| 13 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 14 | #define asoc_simple_init_hp(card, sjack, prefix) \ |
Shengjiu Wang | 764aafd | 2020-07-15 22:09:37 +0800 | [diff] [blame] | 15 | asoc_simple_init_jack(card, sjack, 1, prefix, NULL) |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 16 | #define asoc_simple_init_mic(card, sjack, prefix) \ |
Shengjiu Wang | 764aafd | 2020-07-15 22:09:37 +0800 | [diff] [blame] | 17 | asoc_simple_init_jack(card, sjack, 0, prefix, NULL) |
Katsuhiro Suzuki | 62c2c9f | 2018-06-11 17:32:12 +0900 | [diff] [blame] | 18 | |
Kuninori Morimoto | cecdef3 | 2016-06-30 06:02:46 +0000 | [diff] [blame] | 19 | struct asoc_simple_dai { |
| 20 | const char *name; |
| 21 | unsigned int sysclk; |
Vitaly Wool | a728f56 | 2017-08-17 13:42:36 +0200 | [diff] [blame] | 22 | int clk_direction; |
Kuninori Morimoto | cecdef3 | 2016-06-30 06:02:46 +0000 | [diff] [blame] | 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 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 30 | struct asoc_simple_data { |
Kuninori Morimoto | 13bb1cc | 2017-06-15 00:24:09 +0000 | [diff] [blame] | 31 | u32 convert_rate; |
| 32 | u32 convert_channels; |
| 33 | }; |
| 34 | |
Katsuhiro Suzuki | 62c2c9f | 2018-06-11 17:32:12 +0900 | [diff] [blame] | 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 | |
Kuninori Morimoto | e59289c | 2019-03-20 13:54:59 +0900 | [diff] [blame] | 41 | struct asoc_simple_priv { |
| 42 | struct snd_soc_card snd_card; |
| 43 | struct simple_dai_props { |
| 44 | struct asoc_simple_dai *cpu_dai; |
| 45 | struct asoc_simple_dai *codec_dai; |
Kuninori Morimoto | f107294 | 2019-06-06 13:07:35 +0900 | [diff] [blame] | 46 | struct snd_soc_dai_link_component cpus; /* single cpu */ |
Kuninori Morimoto | e59289c | 2019-03-20 13:54:59 +0900 | [diff] [blame] | 47 | struct snd_soc_dai_link_component codecs; /* single codec */ |
| 48 | struct snd_soc_dai_link_component platforms; |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 49 | struct asoc_simple_data adata; |
Kuninori Morimoto | e59289c | 2019-03-20 13:54:59 +0900 | [diff] [blame] | 50 | struct snd_soc_codec_conf *codec_conf; |
| 51 | unsigned int mclk_fs; |
| 52 | } *dai_props; |
| 53 | struct asoc_simple_jack hp_jack; |
| 54 | struct asoc_simple_jack mic_jack; |
| 55 | struct snd_soc_dai_link *dai_link; |
| 56 | struct asoc_simple_dai *dais; |
| 57 | struct snd_soc_codec_conf *codec_conf; |
| 58 | struct gpio_desc *pa_gpio; |
Sameer Pujar | d09c774 | 2020-11-02 20:40:13 +0530 | [diff] [blame] | 59 | const struct snd_soc_ops *ops; |
| 60 | unsigned int dpcm_selectable:1; |
| 61 | unsigned int force_dpcm:1; |
Kuninori Morimoto | e59289c | 2019-03-20 13:54:59 +0900 | [diff] [blame] | 62 | }; |
| 63 | #define simple_priv_to_card(priv) (&(priv)->snd_card) |
| 64 | #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i)) |
| 65 | #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev) |
| 66 | #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i)) |
| 67 | |
Kuninori Morimoto | 65a5056 | 2019-03-20 13:56:26 +0900 | [diff] [blame] | 68 | struct link_info { |
| 69 | int dais; /* number of dai */ |
| 70 | int link; /* number of link */ |
| 71 | int conf; /* number of codec_conf */ |
| 72 | int cpu; /* turn for CPU / Codec */ |
| 73 | }; |
| 74 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 75 | int asoc_simple_parse_daifmt(struct device *dev, |
| 76 | struct device_node *node, |
| 77 | struct device_node *codec, |
| 78 | char *prefix, |
| 79 | unsigned int *retfmt); |
Nicolas Iooss | e5668ca | 2016-08-23 10:51:22 +0200 | [diff] [blame] | 80 | __printf(3, 4) |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 81 | int asoc_simple_set_dailink_name(struct device *dev, |
| 82 | struct snd_soc_dai_link *dai_link, |
| 83 | const char *fmt, ...); |
| 84 | int asoc_simple_parse_card_name(struct snd_soc_card *card, |
| 85 | char *prefix); |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 86 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 87 | #define asoc_simple_parse_clk_cpu(dev, node, dai_link, simple_dai) \ |
Kuninori Morimoto | f107294 | 2019-06-06 13:07:35 +0900 | [diff] [blame] | 88 | asoc_simple_parse_clk(dev, node, simple_dai, dai_link->cpus) |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 89 | #define asoc_simple_parse_clk_codec(dev, node, dai_link, simple_dai) \ |
Kuninori Morimoto | f107294 | 2019-06-06 13:07:35 +0900 | [diff] [blame] | 90 | asoc_simple_parse_clk(dev, node, simple_dai, dai_link->codecs) |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 91 | int asoc_simple_parse_clk(struct device *dev, |
| 92 | struct device_node *node, |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 93 | struct asoc_simple_dai *simple_dai, |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 94 | struct snd_soc_dai_link_component *dlc); |
Kuninori Morimoto | f38df5b | 2019-03-20 13:55:14 +0900 | [diff] [blame] | 95 | int asoc_simple_startup(struct snd_pcm_substream *substream); |
Kuninori Morimoto | 686911b | 2019-03-20 13:55:27 +0900 | [diff] [blame] | 96 | void asoc_simple_shutdown(struct snd_pcm_substream *substream); |
Kuninori Morimoto | f48dcbb | 2019-03-20 13:55:39 +0900 | [diff] [blame] | 97 | int asoc_simple_hw_params(struct snd_pcm_substream *substream, |
| 98 | struct snd_pcm_hw_params *params); |
Kuninori Morimoto | ad934ca | 2019-03-20 13:55:52 +0900 | [diff] [blame] | 99 | int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd); |
Kuninori Morimoto | 629f754 | 2019-03-20 13:56:06 +0900 | [diff] [blame] | 100 | int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, |
| 101 | struct snd_pcm_hw_params *params); |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 102 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 103 | #define asoc_simple_parse_cpu(node, dai_link, is_single_link) \ |
Kuninori Morimoto | f107294 | 2019-06-06 13:07:35 +0900 | [diff] [blame] | 104 | asoc_simple_parse_dai(node, dai_link->cpus, is_single_link) |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 105 | #define asoc_simple_parse_codec(node, dai_link) \ |
Kuninori Morimoto | f107294 | 2019-06-06 13:07:35 +0900 | [diff] [blame] | 106 | asoc_simple_parse_dai(node, dai_link->codecs, NULL) |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 107 | #define asoc_simple_parse_platform(node, dai_link) \ |
Kuninori Morimoto | f107294 | 2019-06-06 13:07:35 +0900 | [diff] [blame] | 108 | asoc_simple_parse_dai(node, dai_link->platforms, NULL) |
Kuninori Morimoto | 1689333 | 2017-04-20 01:35:18 +0000 | [diff] [blame] | 109 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 110 | #define asoc_simple_parse_tdm(np, dai) \ |
Kuninori Morimoto | e68ba20 | 2017-06-14 00:34:35 +0000 | [diff] [blame] | 111 | snd_soc_of_parse_tdm_slot(np, &(dai)->tx_slot_mask, \ |
| 112 | &(dai)->rx_slot_mask, \ |
| 113 | &(dai)->slots, \ |
| 114 | &(dai)->slot_width); |
| 115 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 116 | void asoc_simple_canonicalize_platform(struct snd_soc_dai_link *dai_link); |
| 117 | void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link *dai_link, |
Kuninori Morimoto | 983cebd | 2016-08-10 02:20:19 +0000 | [diff] [blame] | 118 | int is_single_links); |
Kuninori Morimoto | c262c9a | 2016-08-09 05:49:41 +0000 | [diff] [blame] | 119 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 120 | int asoc_simple_clean_reference(struct snd_soc_card *card); |
Kuninori Morimoto | 0f4e071 | 2016-08-10 02:21:25 +0000 | [diff] [blame] | 121 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 122 | void asoc_simple_convert_fixup(struct asoc_simple_data *data, |
Kuninori Morimoto | 13bb1cc | 2017-06-15 00:24:09 +0000 | [diff] [blame] | 123 | struct snd_pcm_hw_params *params); |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 124 | void asoc_simple_parse_convert(struct device *dev, |
| 125 | struct device_node *np, char *prefix, |
| 126 | struct asoc_simple_data *data); |
Kuninori Morimoto | 13bb1cc | 2017-06-15 00:24:09 +0000 | [diff] [blame] | 127 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 128 | int asoc_simple_parse_routing(struct snd_soc_card *card, |
Kuninori Morimoto | 33404f3 | 2018-11-21 02:11:13 +0000 | [diff] [blame] | 129 | char *prefix); |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 130 | int asoc_simple_parse_widgets(struct snd_soc_card *card, |
Kuninori Morimoto | b31f11d | 2017-06-16 01:38:50 +0000 | [diff] [blame] | 131 | char *prefix); |
Paul Cercueil | 9019428 | 2019-04-26 04:25:49 +0200 | [diff] [blame] | 132 | int asoc_simple_parse_pin_switches(struct snd_soc_card *card, |
| 133 | char *prefix); |
Kuninori Morimoto | 3296d07 | 2017-06-15 00:25:02 +0000 | [diff] [blame] | 134 | |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 135 | int asoc_simple_init_jack(struct snd_soc_card *card, |
Katsuhiro Suzuki | 62c2c9f | 2018-06-11 17:32:12 +0900 | [diff] [blame] | 136 | struct asoc_simple_jack *sjack, |
Shengjiu Wang | 764aafd | 2020-07-15 22:09:37 +0800 | [diff] [blame] | 137 | int is_hp, char *prefix, char *pin); |
Kuninori Morimoto | ad11e59 | 2019-03-20 13:56:50 +0900 | [diff] [blame] | 138 | int asoc_simple_init_priv(struct asoc_simple_priv *priv, |
Kuninori Morimoto | 65a5056 | 2019-03-20 13:56:26 +0900 | [diff] [blame] | 139 | struct link_info *li); |
Kuninori Morimoto | 0580dde | 2019-03-20 13:54:42 +0900 | [diff] [blame] | 140 | |
| 141 | #ifdef DEBUG |
Daniel Baluta | af62195 | 2019-10-09 18:36:15 +0300 | [diff] [blame] | 142 | static inline void asoc_simple_debug_dai(struct asoc_simple_priv *priv, |
| 143 | char *name, |
| 144 | struct asoc_simple_dai *dai) |
Kuninori Morimoto | 0580dde | 2019-03-20 13:54:42 +0900 | [diff] [blame] | 145 | { |
| 146 | struct device *dev = simple_priv_to_dev(priv); |
| 147 | |
Kuninori Morimoto | 52db668 | 2019-07-10 16:59:55 +0900 | [diff] [blame] | 148 | /* dai might be NULL */ |
| 149 | if (!dai) |
| 150 | return; |
| 151 | |
Kuninori Morimoto | 0580dde | 2019-03-20 13:54:42 +0900 | [diff] [blame] | 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 | |
Daniel Baluta | af62195 | 2019-10-09 18:36:15 +0300 | [diff] [blame] | 174 | static inline void asoc_simple_debug_info(struct asoc_simple_priv *priv) |
Kuninori Morimoto | 0580dde | 2019-03-20 13:54:42 +0900 | [diff] [blame] | 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 | |
Kuninori Morimoto | 29a43aa | 2016-12-02 05:27:30 +0000 | [diff] [blame] | 216 | #endif /* __SIMPLE_CARD_UTILS_H */ |