Kuninori Morimoto | 9afe58f | 2018-07-02 06:30:58 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // ASoC simple SCU sound card support |
| 4 | // |
| 5 | // Copyright (C) 2015 Renesas Solutions Corp. |
| 6 | // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 7 | // |
| 8 | // based on ${LINUX}/sound/soc/generic/simple-card.c |
| 9 | |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 10 | #include <linux/clk.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of.h> |
| 14 | #include <linux/of_device.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <sound/jack.h> |
| 18 | #include <sound/soc.h> |
| 19 | #include <sound/soc-dai.h> |
Kuninori Morimoto | d6a4a9a4 | 2016-06-30 06:03:13 +0000 | [diff] [blame] | 20 | #include <sound/simple_card_utils.h> |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 21 | |
Kuninori Morimoto | 6910e86 | 2016-10-28 03:37:44 +0000 | [diff] [blame] | 22 | struct simple_card_data { |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 23 | struct snd_soc_card snd_card; |
Kuninori Morimoto | 5ece10a | 2018-08-31 03:08:38 +0000 | [diff] [blame] | 24 | struct simple_dai_props { |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 25 | struct asoc_simple_dai *cpu_dai; |
| 26 | struct asoc_simple_dai *codec_dai; |
Kuninori Morimoto | 2289cc1 | 2018-08-31 03:08:51 +0000 | [diff] [blame] | 27 | struct snd_soc_dai_link_component codecs; |
Kuninori Morimoto | 24f3bea | 2018-08-31 03:10:46 +0000 | [diff] [blame] | 28 | struct snd_soc_dai_link_component platform; |
Kuninori Morimoto | 09ee833 | 2018-12-11 03:25:18 +0000 | [diff] [blame] | 29 | struct asoc_simple_card_data adata; |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 30 | struct snd_soc_codec_conf *codec_conf; |
Kuninori Morimoto | 5ece10a | 2018-08-31 03:08:38 +0000 | [diff] [blame] | 31 | } *dai_props; |
Kuninori Morimoto | 3433bf0 | 2015-06-15 06:22:30 +0000 | [diff] [blame] | 32 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 33 | struct asoc_simple_dai *dais; |
Kuninori Morimoto | cd8957f | 2017-06-15 00:24:28 +0000 | [diff] [blame] | 34 | struct asoc_simple_card_data adata; |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 35 | struct snd_soc_codec_conf *codec_conf; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 38 | #define simple_priv_to_card(priv) (&(priv)->snd_card) |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 39 | #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i)) |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 40 | #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev) |
| 41 | #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i)) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 42 | |
Kuninori Morimoto | 5bbf3866 | 2016-08-08 06:02:31 +0000 | [diff] [blame] | 43 | #define DAI "sound-dai" |
| 44 | #define CELL "#sound-dai-cells" |
Kuninori Morimoto | 64df0e6 | 2016-08-23 01:34:43 +0000 | [diff] [blame] | 45 | #define PREFIX "simple-audio-card," |
Kuninori Morimoto | 5bbf3866 | 2016-08-08 06:02:31 +0000 | [diff] [blame] | 46 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 47 | static int asoc_simple_card_startup(struct snd_pcm_substream *substream) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 48 | { |
| 49 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | 6910e86 | 2016-10-28 03:37:44 +0000 | [diff] [blame] | 50 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 5ece10a | 2018-08-31 03:08:38 +0000 | [diff] [blame] | 51 | struct simple_dai_props *dai_props = |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 52 | simple_priv_to_props(priv, rtd->num); |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 53 | int ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 54 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 55 | ret = asoc_simple_card_clk_enable(dai_props->cpu_dai); |
| 56 | if (ret) |
| 57 | return ret; |
| 58 | |
| 59 | ret = asoc_simple_card_clk_enable(dai_props->codec_dai); |
| 60 | if (ret) |
| 61 | asoc_simple_card_clk_disable(dai_props->cpu_dai); |
| 62 | |
| 63 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 66 | static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 67 | { |
| 68 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Kuninori Morimoto | 6910e86 | 2016-10-28 03:37:44 +0000 | [diff] [blame] | 69 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 5ece10a | 2018-08-31 03:08:38 +0000 | [diff] [blame] | 70 | struct simple_dai_props *dai_props = |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 71 | simple_priv_to_props(priv, rtd->num); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 72 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 73 | asoc_simple_card_clk_disable(dai_props->cpu_dai); |
| 74 | |
| 75 | asoc_simple_card_clk_disable(dai_props->codec_dai); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Julia Lawall | 9b6fdef | 2016-10-15 16:55:49 +0200 | [diff] [blame] | 78 | static const struct snd_soc_ops asoc_simple_card_ops = { |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 79 | .startup = asoc_simple_card_startup, |
| 80 | .shutdown = asoc_simple_card_shutdown, |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 83 | static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 84 | { |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 85 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 86 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); |
| 87 | int ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 88 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 89 | ret = asoc_simple_card_init_dai(rtd->codec_dai, |
| 90 | dai_props->codec_dai); |
| 91 | if (ret < 0) |
| 92 | return ret; |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 93 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 94 | ret = asoc_simple_card_init_dai(rtd->cpu_dai, |
| 95 | dai_props->cpu_dai); |
| 96 | if (ret < 0) |
| 97 | return ret; |
| 98 | |
| 99 | return 0; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 102 | static int asoc_simple_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, |
Kuninori Morimoto | af7e2be | 2015-03-26 04:01:46 +0000 | [diff] [blame] | 103 | struct snd_pcm_hw_params *params) |
| 104 | { |
Kuninori Morimoto | 6910e86 | 2016-10-28 03:37:44 +0000 | [diff] [blame] | 105 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 09ee833 | 2018-12-11 03:25:18 +0000 | [diff] [blame] | 106 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); |
Kuninori Morimoto | af7e2be | 2015-03-26 04:01:46 +0000 | [diff] [blame] | 107 | |
Kuninori Morimoto | 09ee833 | 2018-12-11 03:25:18 +0000 | [diff] [blame] | 108 | asoc_simple_card_convert_fixup(&dai_props->adata, params); |
| 109 | |
| 110 | /* overwrite by top level adata if exist */ |
Kuninori Morimoto | cd8957f | 2017-06-15 00:24:28 +0000 | [diff] [blame] | 111 | asoc_simple_card_convert_fixup(&priv->adata, params); |
Kuninori Morimoto | af7e2be | 2015-03-26 04:01:46 +0000 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Kuninori Morimoto | dbd08fe | 2018-12-04 08:20:13 +0000 | [diff] [blame] | 116 | static int asoc_simple_card_dai_link_of(struct device_node *link, |
| 117 | struct device_node *np, |
| 118 | struct device_node *codec, |
Kuninori Morimoto | 6910e86 | 2016-10-28 03:37:44 +0000 | [diff] [blame] | 119 | struct simple_card_data *priv, |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 120 | int *dai_idx, int link_idx, |
| 121 | int *conf_idx, int is_fe, |
Kuninori Morimoto | dbd08fe | 2018-12-04 08:20:13 +0000 | [diff] [blame] | 122 | bool is_top_level_node) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 123 | { |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 124 | struct device *dev = simple_priv_to_dev(priv); |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 125 | struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, link_idx); |
| 126 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, link_idx); |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 127 | struct snd_soc_card *card = simple_priv_to_card(priv); |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 128 | struct asoc_simple_dai *dai; |
Kuninori Morimoto | dbd08fe | 2018-12-04 08:20:13 +0000 | [diff] [blame] | 129 | char *prefix = ""; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 130 | int ret; |
| 131 | |
Kuninori Morimoto | dbd08fe | 2018-12-04 08:20:13 +0000 | [diff] [blame] | 132 | /* For single DAI link & old style of DT node */ |
| 133 | if (is_top_level_node) |
| 134 | prefix = PREFIX; |
| 135 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 136 | if (is_fe) { |
Kuninori Morimoto | 27b0108 | 2016-08-10 02:21:03 +0000 | [diff] [blame] | 137 | int is_single_links = 0; |
Kuninori Morimoto | 2289cc1 | 2018-08-31 03:08:51 +0000 | [diff] [blame] | 138 | struct snd_soc_dai_link_component *codecs; |
Kuninori Morimoto | 27b0108 | 2016-08-10 02:21:03 +0000 | [diff] [blame] | 139 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 140 | /* BE is dummy */ |
Kuninori Morimoto | 2289cc1 | 2018-08-31 03:08:51 +0000 | [diff] [blame] | 141 | codecs = dai_link->codecs; |
| 142 | codecs->of_node = NULL; |
| 143 | codecs->dai_name = "snd-soc-dummy-dai"; |
| 144 | codecs->name = "snd-soc-dummy"; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 145 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 146 | /* FE settings */ |
| 147 | dai_link->dynamic = 1; |
| 148 | dai_link->dpcm_merged_format = 1; |
Kuninori Morimoto | 5bbf3866 | 2016-08-08 06:02:31 +0000 | [diff] [blame] | 149 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 150 | dai = |
| 151 | dai_props->cpu_dai = &priv->dais[(*dai_idx)++]; |
| 152 | |
Kuninori Morimoto | 5bbf3866 | 2016-08-08 06:02:31 +0000 | [diff] [blame] | 153 | ret = asoc_simple_card_parse_cpu(np, dai_link, DAI, CELL, |
| 154 | &is_single_links); |
| 155 | if (ret) |
Kuninori Morimoto | 575f1f9 | 2015-12-01 08:33:23 +0000 | [diff] [blame] | 156 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 157 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 158 | ret = asoc_simple_card_parse_clk_cpu(dev, np, dai_link, dai); |
Kuninori Morimoto | c9a235d | 2016-07-19 02:53:32 +0000 | [diff] [blame] | 159 | if (ret < 0) |
| 160 | return ret; |
| 161 | |
Kuninori Morimoto | 8a99a6b | 2016-07-11 23:58:25 +0000 | [diff] [blame] | 162 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 163 | "fe.%s", |
| 164 | dai_link->cpu_dai_name); |
| 165 | if (ret < 0) |
| 166 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 167 | |
Kuninori Morimoto | 27b0108 | 2016-08-10 02:21:03 +0000 | [diff] [blame] | 168 | asoc_simple_card_canonicalize_cpu(dai_link, is_single_links); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 169 | } else { |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 170 | struct snd_soc_codec_conf *cconf; |
| 171 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 172 | /* FE is dummy */ |
| 173 | dai_link->cpu_of_node = NULL; |
| 174 | dai_link->cpu_dai_name = "snd-soc-dummy-dai"; |
| 175 | dai_link->cpu_name = "snd-soc-dummy"; |
| 176 | |
| 177 | /* BE settings */ |
| 178 | dai_link->no_pcm = 1; |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 179 | dai_link->be_hw_params_fixup = asoc_simple_card_be_hw_params_fixup; |
Kuninori Morimoto | 5bbf3866 | 2016-08-08 06:02:31 +0000 | [diff] [blame] | 180 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 181 | dai = |
| 182 | dai_props->codec_dai = &priv->dais[(*dai_idx)++]; |
| 183 | |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 184 | cconf = |
| 185 | dai_props->codec_conf = &priv->codec_conf[(*conf_idx)++]; |
| 186 | |
Kuninori Morimoto | 5bbf3866 | 2016-08-08 06:02:31 +0000 | [diff] [blame] | 187 | ret = asoc_simple_card_parse_codec(np, dai_link, DAI, CELL); |
Kuninori Morimoto | 575f1f9 | 2015-12-01 08:33:23 +0000 | [diff] [blame] | 188 | if (ret < 0) |
| 189 | return ret; |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 190 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 191 | ret = asoc_simple_card_parse_clk_codec(dev, np, dai_link, dai); |
Kuninori Morimoto | c9a235d | 2016-07-19 02:53:32 +0000 | [diff] [blame] | 192 | if (ret < 0) |
| 193 | return ret; |
| 194 | |
Kuninori Morimoto | 8a99a6b | 2016-07-11 23:58:25 +0000 | [diff] [blame] | 195 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 196 | "be.%s", |
Kuninori Morimoto | 2289cc1 | 2018-08-31 03:08:51 +0000 | [diff] [blame] | 197 | dai_link->codecs->dai_name); |
Kuninori Morimoto | 8a99a6b | 2016-07-11 23:58:25 +0000 | [diff] [blame] | 198 | if (ret < 0) |
| 199 | return ret; |
| 200 | |
Kuninori Morimoto | ca8d95c | 2018-11-22 00:55:40 +0000 | [diff] [blame] | 201 | /* check "prefix" from top node */ |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 202 | snd_soc_of_parse_audio_prefix(card, cconf, |
Kuninori Morimoto | 2289cc1 | 2018-08-31 03:08:51 +0000 | [diff] [blame] | 203 | dai_link->codecs->of_node, |
Kuninori Morimoto | 64df0e6 | 2016-08-23 01:34:43 +0000 | [diff] [blame] | 204 | PREFIX "prefix"); |
Kuninori Morimoto | ca8d95c | 2018-11-22 00:55:40 +0000 | [diff] [blame] | 205 | /* check "prefix" from each node if top doesn't have */ |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 206 | if (!cconf->of_node) |
| 207 | snd_soc_of_parse_node_prefix(np, cconf, |
Kuninori Morimoto | ca8d95c | 2018-11-22 00:55:40 +0000 | [diff] [blame] | 208 | dai_link->codecs->of_node, |
| 209 | "prefix"); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Kuninori Morimoto | 09ee833 | 2018-12-11 03:25:18 +0000 | [diff] [blame] | 212 | asoc_simple_card_parse_convert(dev, link, prefix, &dai_props->adata); |
| 213 | |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 214 | ret = asoc_simple_card_of_parse_tdm(np, dai); |
Kuninori Morimoto | 9f64542 | 2016-08-25 01:58:55 +0000 | [diff] [blame] | 215 | if (ret) |
| 216 | return ret; |
| 217 | |
Kuninori Morimoto | a09f383 | 2016-08-09 05:50:19 +0000 | [diff] [blame] | 218 | ret = asoc_simple_card_canonicalize_dailink(dai_link); |
| 219 | if (ret < 0) |
| 220 | return ret; |
| 221 | |
Kuninori Morimoto | dbd08fe | 2018-12-04 08:20:13 +0000 | [diff] [blame] | 222 | ret = asoc_simple_card_parse_daifmt(dev, link, codec, |
| 223 | prefix, &dai_link->dai_fmt); |
| 224 | if (ret < 0) |
| 225 | return ret; |
| 226 | |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 227 | dai_link->dpcm_playback = 1; |
| 228 | dai_link->dpcm_capture = 1; |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 229 | dai_link->ops = &asoc_simple_card_ops; |
| 230 | dai_link->init = asoc_simple_card_dai_init; |
Kuninori Morimoto | 0470002 | 2015-06-15 06:24:15 +0000 | [diff] [blame] | 231 | |
Kuninori Morimoto | c9a235d | 2016-07-19 02:53:32 +0000 | [diff] [blame] | 232 | return 0; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Kuninori Morimoto | 0d6b352 | 2017-06-07 00:37:30 +0000 | [diff] [blame] | 235 | static int asoc_simple_card_parse_of(struct simple_card_data *priv) |
Kuninori Morimoto | 15a190ff | 2016-10-28 03:37:26 +0000 | [diff] [blame] | 236 | |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 237 | { |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 238 | struct device *dev = simple_priv_to_dev(priv); |
Kuninori Morimoto | da32d65 | 2018-12-11 03:24:57 +0000 | [diff] [blame] | 239 | struct device_node *top = dev->of_node; |
| 240 | struct device_node *node; |
Kuninori Morimoto | dbd08fe | 2018-12-04 08:20:13 +0000 | [diff] [blame] | 241 | struct device_node *np; |
| 242 | struct device_node *codec; |
| 243 | struct snd_soc_card *card = simple_priv_to_card(priv); |
Kuninori Morimoto | af998f8 | 2015-12-17 02:49:43 +0000 | [diff] [blame] | 244 | bool is_fe; |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 245 | int ret, loop; |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 246 | int dai_idx, link_idx, conf_idx; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 247 | |
Kuninori Morimoto | da32d65 | 2018-12-11 03:24:57 +0000 | [diff] [blame] | 248 | if (!top) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | |
Daniel Baluta | d0148eb | 2017-08-11 17:12:19 +0300 | [diff] [blame] | 251 | ret = asoc_simple_card_of_parse_widgets(card, PREFIX); |
| 252 | if (ret < 0) |
| 253 | return ret; |
| 254 | |
Kuninori Morimoto | 33404f3 | 2018-11-21 02:11:13 +0000 | [diff] [blame] | 255 | ret = asoc_simple_card_of_parse_routing(card, PREFIX); |
Kuninori Morimoto | 64df0e6 | 2016-08-23 01:34:43 +0000 | [diff] [blame] | 256 | if (ret < 0) |
| 257 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 258 | |
Kuninori Morimoto | da32d65 | 2018-12-11 03:24:57 +0000 | [diff] [blame] | 259 | asoc_simple_card_parse_convert(dev, top, PREFIX, &priv->adata); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 260 | |
Kuninori Morimoto | da32d65 | 2018-12-11 03:24:57 +0000 | [diff] [blame] | 261 | loop = 1; |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 262 | link_idx = 0; |
| 263 | dai_idx = 0; |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 264 | conf_idx = 0; |
Kuninori Morimoto | da32d65 | 2018-12-11 03:24:57 +0000 | [diff] [blame] | 265 | node = of_get_child_by_name(top, PREFIX "dai-link"); |
| 266 | if (!node) { |
| 267 | node = dev->of_node; |
| 268 | loop = 0; |
Kuninori Morimoto | 15a190ff | 2016-10-28 03:37:26 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Kuninori Morimoto | da32d65 | 2018-12-11 03:24:57 +0000 | [diff] [blame] | 271 | do { |
| 272 | codec = of_get_child_by_name(node, |
| 273 | loop ? "codec" : PREFIX "codec"); |
| 274 | if (!codec) |
| 275 | return -ENODEV; |
| 276 | |
| 277 | for_each_child_of_node(node, np) { |
| 278 | is_fe = (np != codec); |
| 279 | |
| 280 | ret = asoc_simple_card_dai_link_of(node, np, codec, priv, |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 281 | &dai_idx, link_idx++, |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 282 | &conf_idx, |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 283 | is_fe, !loop); |
Kuninori Morimoto | da32d65 | 2018-12-11 03:24:57 +0000 | [diff] [blame] | 284 | if (ret < 0) |
| 285 | return ret; |
Kuninori Morimoto | da32d65 | 2018-12-11 03:24:57 +0000 | [diff] [blame] | 286 | } |
| 287 | node = of_get_next_child(top, node); |
| 288 | } while (loop && node); |
| 289 | |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 290 | ret = asoc_simple_card_parse_card_name(card, PREFIX); |
Kuninori Morimoto | 53ae918 | 2016-07-12 00:00:00 +0000 | [diff] [blame] | 291 | if (ret < 0) |
| 292 | return ret; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
Kuninori Morimoto | b8d8f9a | 2018-12-11 03:25:08 +0000 | [diff] [blame] | 297 | static void asoc_simple_card_get_dais_count(struct device *dev, |
| 298 | int *link_num, |
| 299 | int *dais_num, |
| 300 | int *ccnf_num) |
| 301 | { |
| 302 | struct device_node *top = dev->of_node; |
| 303 | struct device_node *node; |
| 304 | int loop; |
| 305 | int num; |
| 306 | |
| 307 | /* |
| 308 | * link_num : number of links. |
| 309 | * CPU-Codec / CPU-dummy / dummy-Codec |
| 310 | * dais_num : number of DAIs |
| 311 | * ccnf_num : number of codec_conf |
| 312 | * same number for "dummy-Codec" |
| 313 | * |
| 314 | * ex1) |
| 315 | * CPU0 --- Codec0 link : 5 |
| 316 | * CPU1 --- Codec1 dais : 7 |
| 317 | * CPU2 -/ ccnf : 1 |
| 318 | * CPU3 --- Codec2 |
| 319 | * |
| 320 | * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec |
| 321 | * => 7 DAIs = 4xCPU + 3xCodec |
| 322 | * => 1 ccnf = 1xdummy-Codec |
| 323 | * |
| 324 | * ex2) |
| 325 | * CPU0 --- Codec0 link : 5 |
| 326 | * CPU1 --- Codec1 dais : 6 |
| 327 | * CPU2 -/ ccnf : 1 |
| 328 | * CPU3 -/ |
| 329 | * |
| 330 | * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec |
| 331 | * => 6 DAIs = 4xCPU + 2xCodec |
| 332 | * => 1 ccnf = 1xdummy-Codec |
| 333 | * |
| 334 | * ex3) |
| 335 | * CPU0 --- Codec0 link : 6 |
| 336 | * CPU1 -/ dais : 6 |
| 337 | * CPU2 --- Codec1 ccnf : 2 |
| 338 | * CPU3 -/ |
| 339 | * |
| 340 | * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec |
| 341 | * => 6 DAIs = 4xCPU + 2xCodec |
| 342 | * => 2 ccnf = 2xdummy-Codec |
| 343 | */ |
| 344 | if (!top) { |
| 345 | (*link_num) = 1; |
| 346 | (*dais_num) = 2; |
| 347 | (*ccnf_num) = 0; |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | loop = 1; |
| 352 | node = of_get_child_by_name(top, PREFIX "dai-link"); |
| 353 | if (!node) { |
| 354 | node = top; |
| 355 | loop = 0; |
| 356 | } |
| 357 | |
| 358 | do { |
| 359 | num = of_get_child_count(node); |
| 360 | (*dais_num) += num; |
| 361 | if (num > 2) { |
| 362 | (*link_num) += num; |
| 363 | (*ccnf_num)++; |
| 364 | } else { |
| 365 | (*link_num)++; |
| 366 | } |
| 367 | node = of_get_next_child(top, node); |
| 368 | } while (loop && node); |
| 369 | } |
| 370 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 371 | static int asoc_simple_card_probe(struct platform_device *pdev) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 372 | { |
Kuninori Morimoto | 6910e86 | 2016-10-28 03:37:44 +0000 | [diff] [blame] | 373 | struct simple_card_data *priv; |
Kuninori Morimoto | 1935992 | 2016-10-28 03:38:00 +0000 | [diff] [blame] | 374 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | 5ece10a | 2018-08-31 03:08:38 +0000 | [diff] [blame] | 375 | struct simple_dai_props *dai_props; |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 376 | struct asoc_simple_dai *dais; |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 377 | struct snd_soc_card *card; |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 378 | struct snd_soc_codec_conf *cconf; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 379 | struct device *dev = &pdev->dev; |
Kuninori Morimoto | b8d8f9a | 2018-12-11 03:25:08 +0000 | [diff] [blame] | 380 | int ret, i; |
| 381 | int lnum = 0, dnum = 0, cnum = 0; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 382 | |
| 383 | /* Allocate the private data */ |
| 384 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 385 | if (!priv) |
| 386 | return -ENOMEM; |
| 387 | |
Kuninori Morimoto | b8d8f9a | 2018-12-11 03:25:08 +0000 | [diff] [blame] | 388 | asoc_simple_card_get_dais_count(dev, &lnum, &dnum, &cnum); |
| 389 | if (!lnum || !dnum) |
| 390 | return -EINVAL; |
Kuninori Morimoto | 15a190ff | 2016-10-28 03:37:26 +0000 | [diff] [blame] | 391 | |
Kuninori Morimoto | b8d8f9a | 2018-12-11 03:25:08 +0000 | [diff] [blame] | 392 | dai_props = devm_kcalloc(dev, lnum, sizeof(*dai_props), GFP_KERNEL); |
| 393 | dai_link = devm_kcalloc(dev, lnum, sizeof(*dai_link), GFP_KERNEL); |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 394 | dais = devm_kcalloc(dev, dnum, sizeof(*dais), GFP_KERNEL); |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 395 | cconf = devm_kcalloc(dev, cnum, sizeof(*cconf), GFP_KERNEL); |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 396 | if (!dai_props || !dai_link || !dais) |
Kuninori Morimoto | 15a190ff | 2016-10-28 03:37:26 +0000 | [diff] [blame] | 397 | return -ENOMEM; |
| 398 | |
Kuninori Morimoto | 2289cc1 | 2018-08-31 03:08:51 +0000 | [diff] [blame] | 399 | /* |
| 400 | * Use snd_soc_dai_link_component instead of legacy style |
| 401 | * It is codec only. but cpu/platform will be supported in the future. |
| 402 | * see |
| 403 | * soc-core.c :: snd_soc_init_multicodec() |
| 404 | */ |
Kuninori Morimoto | b8d8f9a | 2018-12-11 03:25:08 +0000 | [diff] [blame] | 405 | for (i = 0; i < lnum; i++) { |
Kuninori Morimoto | 2289cc1 | 2018-08-31 03:08:51 +0000 | [diff] [blame] | 406 | dai_link[i].codecs = &dai_props[i].codecs; |
| 407 | dai_link[i].num_codecs = 1; |
Kuninori Morimoto | 24f3bea | 2018-08-31 03:10:46 +0000 | [diff] [blame] | 408 | dai_link[i].platform = &dai_props[i].platform; |
Kuninori Morimoto | 2289cc1 | 2018-08-31 03:08:51 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Kuninori Morimoto | 1935992 | 2016-10-28 03:38:00 +0000 | [diff] [blame] | 411 | priv->dai_props = dai_props; |
| 412 | priv->dai_link = dai_link; |
Kuninori Morimoto | e6a3ff2 | 2018-12-11 03:25:27 +0000 | [diff] [blame] | 413 | priv->dais = dais; |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 414 | priv->codec_conf = cconf; |
Kuninori Morimoto | 15a190ff | 2016-10-28 03:37:26 +0000 | [diff] [blame] | 415 | |
| 416 | /* Init snd_soc_card */ |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 417 | card = simple_priv_to_card(priv); |
| 418 | card->owner = THIS_MODULE; |
| 419 | card->dev = dev; |
| 420 | card->dai_link = priv->dai_link; |
Kuninori Morimoto | b8d8f9a | 2018-12-11 03:25:08 +0000 | [diff] [blame] | 421 | card->num_links = lnum; |
Kuninori Morimoto | 0a26d6d | 2018-12-11 03:25:37 +0000 | [diff] [blame] | 422 | card->codec_conf = cconf; |
| 423 | card->num_configs = cnum; |
Kuninori Morimoto | 15a190ff | 2016-10-28 03:37:26 +0000 | [diff] [blame] | 424 | |
Kuninori Morimoto | 0d6b352 | 2017-06-07 00:37:30 +0000 | [diff] [blame] | 425 | ret = asoc_simple_card_parse_of(priv); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 426 | if (ret < 0) { |
| 427 | if (ret != -EPROBE_DEFER) |
| 428 | dev_err(dev, "parse error %d\n", ret); |
| 429 | goto err; |
| 430 | } |
| 431 | |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 432 | snd_soc_card_set_drvdata(card, priv); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 433 | |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 434 | ret = devm_snd_soc_register_card(dev, card); |
Kuninori Morimoto | c73df77 | 2017-05-19 00:57:44 +0000 | [diff] [blame] | 435 | if (ret < 0) |
| 436 | goto err; |
| 437 | |
| 438 | return 0; |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 439 | err: |
Kuninori Morimoto | d27f3b4 | 2017-03-15 04:44:16 +0000 | [diff] [blame] | 440 | asoc_simple_card_clean_reference(card); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 445 | static int asoc_simple_card_remove(struct platform_device *pdev) |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 446 | { |
| 447 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 448 | |
Kuninori Morimoto | 239486b | 2016-08-10 02:22:01 +0000 | [diff] [blame] | 449 | return asoc_simple_card_clean_reference(card); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Kuninori Morimoto | f4d70709 | 2016-08-25 01:58:31 +0000 | [diff] [blame] | 452 | static const struct of_device_id asoc_simple_of_match[] = { |
| 453 | { .compatible = "renesas,rsrc-card", }, |
| 454 | { .compatible = "simple-scu-audio-card", }, |
| 455 | {}, |
| 456 | }; |
| 457 | MODULE_DEVICE_TABLE(of, asoc_simple_of_match); |
| 458 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 459 | static struct platform_driver asoc_simple_card = { |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 460 | .driver = { |
Kuninori Morimoto | 64df0e6 | 2016-08-23 01:34:43 +0000 | [diff] [blame] | 461 | .name = "simple-scu-audio-card", |
Kuninori Morimoto | b4e0180 | 2017-08-22 04:56:06 +0000 | [diff] [blame] | 462 | .pm = &snd_soc_pm_ops, |
Kuninori Morimoto | f4d70709 | 2016-08-25 01:58:31 +0000 | [diff] [blame] | 463 | .of_match_table = asoc_simple_of_match, |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 464 | }, |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 465 | .probe = asoc_simple_card_probe, |
| 466 | .remove = asoc_simple_card_remove, |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 467 | }; |
| 468 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 469 | module_platform_driver(asoc_simple_card); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 470 | |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 471 | MODULE_ALIAS("platform:asoc-simple-scu-card"); |
Kuninori Morimoto | 93bc047 | 2016-08-25 01:56:38 +0000 | [diff] [blame] | 472 | MODULE_LICENSE("GPL v2"); |
Kuninori Morimoto | 53e682b | 2016-08-23 01:34:17 +0000 | [diff] [blame] | 473 | MODULE_DESCRIPTION("ASoC Simple SCU Sound Card"); |
Kuninori Morimoto | 415f1cb | 2015-03-26 04:01:27 +0000 | [diff] [blame] | 474 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |