Kuninori Morimoto | d573454 | 2018-07-02 06:30:28 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // ASoC simple sound card support |
| 4 | // |
| 5 | // Copyright (C) 2012 Renesas Solutions Corp. |
| 6 | // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 7 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 8 | #include <linux/clk.h> |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 9 | #include <linux/device.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 11 | #include <linux/of.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 12 | #include <linux/platform_device.h> |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 13 | #include <linux/string.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 14 | #include <sound/simple_card.h> |
Xiubo Li | 6ff62ee | 2014-02-14 09:34:36 +0800 | [diff] [blame] | 15 | #include <sound/soc-dai.h> |
| 16 | #include <sound/soc.h> |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 17 | |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 18 | struct simple_card_data { |
| 19 | struct snd_soc_card snd_card; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 20 | struct simple_dai_props { |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 21 | struct asoc_simple_dai *cpu_dai; |
| 22 | struct asoc_simple_dai *codec_dai; |
Kuninori Morimoto | 710af91 | 2018-08-31 03:08:24 +0000 | [diff] [blame] | 23 | struct snd_soc_dai_link_component codecs; /* single codec */ |
Kuninori Morimoto | e58f41e | 2018-08-31 03:10:33 +0000 | [diff] [blame] | 24 | struct snd_soc_dai_link_component platform; |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 25 | struct asoc_simple_card_data adata; |
| 26 | struct snd_soc_codec_conf *codec_conf; |
Arnaud Pouliquen | 85a4bfd | 2015-06-05 10:19:05 +0200 | [diff] [blame] | 27 | unsigned int mclk_fs; |
Jean-Francois Moine | cf7dc23 | 2014-03-20 10:52:41 +0100 | [diff] [blame] | 28 | } *dai_props; |
Kuninori Morimoto | 9eac361 | 2016-05-31 08:59:46 +0000 | [diff] [blame] | 29 | struct asoc_simple_jack hp_jack; |
| 30 | struct asoc_simple_jack mic_jack; |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 31 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 32 | struct asoc_simple_dai *dais; |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 33 | struct snd_soc_codec_conf *codec_conf; |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 36 | #define simple_priv_to_card(priv) (&(priv)->snd_card) |
Kuninori Morimoto | 7e3353d | 2016-08-26 03:06:23 +0000 | [diff] [blame] | 37 | #define simple_priv_to_props(priv, i) ((priv)->dai_props + (i)) |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 38 | #define simple_priv_to_dev(priv) (simple_priv_to_card(priv)->dev) |
| 39 | #define simple_priv_to_link(priv, i) (simple_priv_to_card(priv)->dai_link + (i)) |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 40 | |
Kuninori Morimoto | 44c16af | 2016-08-08 06:02:07 +0000 | [diff] [blame] | 41 | #define DAI "sound-dai" |
| 42 | #define CELL "#sound-dai-cells" |
Kuninori Morimoto | 548563f | 2016-05-31 08:59:01 +0000 | [diff] [blame] | 43 | #define PREFIX "simple-audio-card," |
| 44 | |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 45 | static int asoc_simple_card_startup(struct snd_pcm_substream *substream) |
| 46 | { |
| 47 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 48 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 49 | struct simple_dai_props *dai_props = |
Kuninori Morimoto | c9a23ea | 2016-08-26 03:06:51 +0000 | [diff] [blame] | 50 | simple_priv_to_props(priv, rtd->num); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 51 | int ret; |
| 52 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 53 | ret = asoc_simple_card_clk_enable(dai_props->cpu_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 54 | if (ret) |
| 55 | return ret; |
Kuninori Morimoto | 387f582 | 2016-08-26 03:07:59 +0000 | [diff] [blame] | 56 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 57 | ret = asoc_simple_card_clk_enable(dai_props->codec_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 58 | if (ret) |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 59 | asoc_simple_card_clk_disable(dai_props->cpu_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream) |
| 65 | { |
| 66 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 67 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 68 | struct simple_dai_props *dai_props = |
Kuninori Morimoto | c9a23ea | 2016-08-26 03:06:51 +0000 | [diff] [blame] | 69 | simple_priv_to_props(priv, rtd->num); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 70 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 71 | asoc_simple_card_clk_disable(dai_props->cpu_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 72 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 73 | asoc_simple_card_clk_disable(dai_props->codec_dai); |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Daniel Mack | e9be4ff | 2018-05-30 21:45:56 +0200 | [diff] [blame] | 76 | static int asoc_simple_set_clk_rate(struct asoc_simple_dai *simple_dai, |
| 77 | unsigned long rate) |
| 78 | { |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 79 | if (!simple_dai) |
| 80 | return 0; |
| 81 | |
Daniel Mack | e9be4ff | 2018-05-30 21:45:56 +0200 | [diff] [blame] | 82 | if (!simple_dai->clk) |
| 83 | return 0; |
| 84 | |
| 85 | if (clk_get_rate(simple_dai->clk) == rate) |
| 86 | return 0; |
| 87 | |
| 88 | return clk_set_rate(simple_dai->clk, rate); |
| 89 | } |
| 90 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 91 | static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream, |
| 92 | struct snd_pcm_hw_params *params) |
| 93 | { |
| 94 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 95 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
Arnaud Pouliquen | e225797 | 2015-06-05 10:19:06 +0200 | [diff] [blame] | 96 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 97 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | c9a23ea | 2016-08-26 03:06:51 +0000 | [diff] [blame] | 98 | struct simple_dai_props *dai_props = |
| 99 | simple_priv_to_props(priv, rtd->num); |
Arnaud Pouliquen | 85a4bfd | 2015-06-05 10:19:05 +0200 | [diff] [blame] | 100 | unsigned int mclk, mclk_fs = 0; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 101 | int ret = 0; |
| 102 | |
Kuninori Morimoto | 79e8349 | 2018-12-14 11:35:15 +0900 | [diff] [blame] | 103 | if (dai_props->mclk_fs) |
Arnaud Pouliquen | 85a4bfd | 2015-06-05 10:19:05 +0200 | [diff] [blame] | 104 | mclk_fs = dai_props->mclk_fs; |
| 105 | |
| 106 | if (mclk_fs) { |
| 107 | mclk = params_rate(params) * mclk_fs; |
Daniel Mack | e9be4ff | 2018-05-30 21:45:56 +0200 | [diff] [blame] | 108 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 109 | ret = asoc_simple_set_clk_rate(dai_props->codec_dai, mclk); |
Daniel Mack | e9be4ff | 2018-05-30 21:45:56 +0200 | [diff] [blame] | 110 | if (ret < 0) |
| 111 | return ret; |
| 112 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 113 | ret = asoc_simple_set_clk_rate(dai_props->cpu_dai, mclk); |
Daniel Mack | e9be4ff | 2018-05-30 21:45:56 +0200 | [diff] [blame] | 114 | if (ret < 0) |
| 115 | return ret; |
| 116 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 117 | ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
| 118 | SND_SOC_CLOCK_IN); |
Arnaud Pouliquen | e225797 | 2015-06-05 10:19:06 +0200 | [diff] [blame] | 119 | if (ret && ret != -ENOTSUPP) |
| 120 | goto err; |
| 121 | |
| 122 | ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, |
| 123 | SND_SOC_CLOCK_OUT); |
| 124 | if (ret && ret != -ENOTSUPP) |
| 125 | goto err; |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 126 | } |
Aaro Koskinen | ee43a1a | 2016-01-24 00:36:40 +0200 | [diff] [blame] | 127 | return 0; |
Arnaud Pouliquen | e225797 | 2015-06-05 10:19:06 +0200 | [diff] [blame] | 128 | err: |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 129 | return ret; |
| 130 | } |
| 131 | |
Julia Lawall | 9b6fdef | 2016-10-15 16:55:49 +0200 | [diff] [blame] | 132 | static const struct snd_soc_ops asoc_simple_card_ops = { |
Jyri Sarha | f991180 | 2015-01-13 21:16:34 +0200 | [diff] [blame] | 133 | .startup = asoc_simple_card_startup, |
| 134 | .shutdown = asoc_simple_card_shutdown, |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 135 | .hw_params = asoc_simple_card_hw_params, |
| 136 | }; |
| 137 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 138 | static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 139 | { |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 140 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 141 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 142 | int ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 143 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 144 | ret = asoc_simple_card_init_dai(rtd->codec_dai, |
| 145 | dai_props->codec_dai); |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 146 | if (ret < 0) |
| 147 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 148 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 149 | ret = asoc_simple_card_init_dai(rtd->cpu_dai, |
| 150 | dai_props->cpu_dai); |
Kuninori Morimoto | a4a2992 | 2013-01-10 16:49:11 -0800 | [diff] [blame] | 151 | if (ret < 0) |
| 152 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 157 | static int asoc_simple_card_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, |
| 158 | struct snd_pcm_hw_params *params) |
| 159 | { |
| 160 | struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 161 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); |
| 162 | |
| 163 | asoc_simple_card_convert_fixup(&dai_props->adata, params); |
| 164 | |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Kuninori Morimoto | 7e5e1f8 | 2018-12-20 10:46:42 +0900 | [diff] [blame^] | 168 | static void asoc_simple_card_get_conversion(struct device *dev, |
| 169 | struct device_node *np, |
| 170 | struct asoc_simple_card_data *adata) |
| 171 | { |
| 172 | struct device_node *top = dev->of_node; |
| 173 | struct device_node *node = of_get_parent(np); |
| 174 | |
| 175 | asoc_simple_card_parse_convert(dev, top, PREFIX, adata); |
| 176 | asoc_simple_card_parse_convert(dev, node, PREFIX, adata); |
| 177 | asoc_simple_card_parse_convert(dev, node, NULL, adata); |
| 178 | asoc_simple_card_parse_convert(dev, np, NULL, adata); |
| 179 | |
| 180 | of_node_put(node); |
| 181 | } |
| 182 | |
Kuninori Morimoto | 79e8349 | 2018-12-14 11:35:15 +0900 | [diff] [blame] | 183 | static int asoc_simple_card_dai_link_of_dpcm(struct device_node *top, |
| 184 | struct device_node *node, |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 185 | struct device_node *np, |
| 186 | struct device_node *codec, |
| 187 | struct simple_card_data *priv, |
| 188 | int *dai_idx, int link_idx, |
| 189 | int *conf_idx, int is_fe, |
| 190 | bool is_top_level_node) |
| 191 | { |
| 192 | struct device *dev = simple_priv_to_dev(priv); |
| 193 | struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, link_idx); |
| 194 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, link_idx); |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 195 | struct asoc_simple_dai *dai; |
Kuninori Morimoto | cfc652a | 2018-12-14 11:35:24 +0900 | [diff] [blame] | 196 | struct snd_soc_dai_link_component *codecs = dai_link->codecs; |
| 197 | |
Kuninori Morimoto | 79e8349 | 2018-12-14 11:35:15 +0900 | [diff] [blame] | 198 | char prop[128]; |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 199 | char *prefix = ""; |
| 200 | int ret; |
| 201 | |
| 202 | /* For single DAI link & old style of DT node */ |
| 203 | if (is_top_level_node) |
| 204 | prefix = PREFIX; |
| 205 | |
| 206 | if (is_fe) { |
| 207 | int is_single_links = 0; |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 208 | |
| 209 | /* BE is dummy */ |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 210 | codecs->of_node = NULL; |
| 211 | codecs->dai_name = "snd-soc-dummy-dai"; |
| 212 | codecs->name = "snd-soc-dummy"; |
| 213 | |
| 214 | /* FE settings */ |
| 215 | dai_link->dynamic = 1; |
| 216 | dai_link->dpcm_merged_format = 1; |
| 217 | |
| 218 | dai = |
| 219 | dai_props->cpu_dai = &priv->dais[(*dai_idx)++]; |
| 220 | |
| 221 | ret = asoc_simple_card_parse_cpu(np, dai_link, DAI, CELL, |
| 222 | &is_single_links); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | |
| 226 | ret = asoc_simple_card_parse_clk_cpu(dev, np, dai_link, dai); |
| 227 | if (ret < 0) |
| 228 | return ret; |
| 229 | |
| 230 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 231 | "fe.%s", |
| 232 | dai_link->cpu_dai_name); |
| 233 | if (ret < 0) |
| 234 | return ret; |
| 235 | |
| 236 | asoc_simple_card_canonicalize_cpu(dai_link, is_single_links); |
| 237 | } else { |
| 238 | struct snd_soc_codec_conf *cconf; |
| 239 | |
| 240 | /* FE is dummy */ |
| 241 | dai_link->cpu_of_node = NULL; |
| 242 | dai_link->cpu_dai_name = "snd-soc-dummy-dai"; |
| 243 | dai_link->cpu_name = "snd-soc-dummy"; |
| 244 | |
| 245 | /* BE settings */ |
| 246 | dai_link->no_pcm = 1; |
| 247 | dai_link->be_hw_params_fixup = asoc_simple_card_be_hw_params_fixup; |
| 248 | |
| 249 | dai = |
| 250 | dai_props->codec_dai = &priv->dais[(*dai_idx)++]; |
| 251 | |
| 252 | cconf = |
| 253 | dai_props->codec_conf = &priv->codec_conf[(*conf_idx)++]; |
| 254 | |
| 255 | ret = asoc_simple_card_parse_codec(np, dai_link, DAI, CELL); |
| 256 | if (ret < 0) |
| 257 | return ret; |
| 258 | |
| 259 | ret = asoc_simple_card_parse_clk_codec(dev, np, dai_link, dai); |
| 260 | if (ret < 0) |
| 261 | return ret; |
| 262 | |
| 263 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 264 | "be.%s", |
Kuninori Morimoto | cfc652a | 2018-12-14 11:35:24 +0900 | [diff] [blame] | 265 | codecs->dai_name); |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 266 | if (ret < 0) |
| 267 | return ret; |
| 268 | |
| 269 | /* check "prefix" from top node */ |
Kuninori Morimoto | cfc652a | 2018-12-14 11:35:24 +0900 | [diff] [blame] | 270 | snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node, |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 271 | PREFIX "prefix"); |
Kuninori Morimoto | cfc652a | 2018-12-14 11:35:24 +0900 | [diff] [blame] | 272 | snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node, |
| 273 | "prefix"); |
| 274 | snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node, |
| 275 | "prefix"); |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 276 | } |
| 277 | |
Kuninori Morimoto | 7e5e1f8 | 2018-12-20 10:46:42 +0900 | [diff] [blame^] | 278 | asoc_simple_card_get_conversion(dev, np, &dai_props->adata); |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 279 | |
| 280 | ret = asoc_simple_card_of_parse_tdm(np, dai); |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | |
| 284 | ret = asoc_simple_card_canonicalize_dailink(dai_link); |
| 285 | if (ret < 0) |
| 286 | return ret; |
| 287 | |
Kuninori Morimoto | 79e8349 | 2018-12-14 11:35:15 +0900 | [diff] [blame] | 288 | snprintf(prop, sizeof(prop), "%smclk-fs", prefix); |
| 289 | of_property_read_u32(top, PREFIX "mclk-fs", &dai_props->mclk_fs); |
| 290 | of_property_read_u32(node, prop, &dai_props->mclk_fs); |
| 291 | of_property_read_u32(np, prop, &dai_props->mclk_fs); |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 292 | |
| 293 | ret = asoc_simple_card_parse_daifmt(dev, node, codec, |
| 294 | prefix, &dai_link->dai_fmt); |
| 295 | if (ret < 0) |
| 296 | return ret; |
| 297 | |
| 298 | dai_link->dpcm_playback = 1; |
| 299 | dai_link->dpcm_capture = 1; |
| 300 | dai_link->ops = &asoc_simple_card_ops; |
| 301 | dai_link->init = asoc_simple_card_dai_init; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
Kuninori Morimoto | 79e8349 | 2018-12-14 11:35:15 +0900 | [diff] [blame] | 306 | static int asoc_simple_card_dai_link_of(struct device_node *top, |
| 307 | struct device_node *node, |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 308 | struct simple_card_data *priv, |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 309 | int *dai_idx, int link_idx, |
Kuninori Morimoto | 2d82eeb | 2014-08-27 20:07:46 -0700 | [diff] [blame] | 310 | bool is_top_level_node) |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 311 | { |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 312 | struct device *dev = simple_priv_to_dev(priv); |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 313 | struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, link_idx); |
| 314 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, link_idx); |
| 315 | struct asoc_simple_dai *cpu_dai; |
| 316 | struct asoc_simple_dai *codec_dai; |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 317 | struct device_node *cpu = NULL; |
Jun Nie | e0ae225 | 2015-04-29 18:11:07 +0800 | [diff] [blame] | 318 | struct device_node *plat = NULL; |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 319 | struct device_node *codec = NULL; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 320 | char prop[128]; |
| 321 | char *prefix = ""; |
Kuninori Morimoto | 44c16af | 2016-08-08 06:02:07 +0000 | [diff] [blame] | 322 | int ret, single_cpu; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 323 | |
Xiubo Li | 2080437 | 2014-09-03 10:23:39 +0800 | [diff] [blame] | 324 | /* For single DAI link & old style of DT node */ |
Jyri Sarha | 6487221 | 2014-04-24 19:42:00 +0300 | [diff] [blame] | 325 | if (is_top_level_node) |
Kuninori Morimoto | 548563f | 2016-05-31 08:59:01 +0000 | [diff] [blame] | 326 | prefix = PREFIX; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 327 | |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 328 | snprintf(prop, sizeof(prop), "%scpu", prefix); |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 329 | cpu = of_get_child_by_name(node, prop); |
| 330 | |
Julian Scheel | 7ac45d1 | 2017-05-24 12:28:23 +0200 | [diff] [blame] | 331 | if (!cpu) { |
| 332 | ret = -EINVAL; |
| 333 | dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop); |
| 334 | goto dai_link_of_err; |
| 335 | } |
| 336 | |
Jun Nie | e0ae225 | 2015-04-29 18:11:07 +0800 | [diff] [blame] | 337 | snprintf(prop, sizeof(prop), "%splat", prefix); |
| 338 | plat = of_get_child_by_name(node, prop); |
| 339 | |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 340 | snprintf(prop, sizeof(prop), "%scodec", prefix); |
| 341 | codec = of_get_child_by_name(node, prop); |
| 342 | |
Julian Scheel | 7ac45d1 | 2017-05-24 12:28:23 +0200 | [diff] [blame] | 343 | if (!codec) { |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 344 | ret = -EINVAL; |
Nicolin Chen | 966b806 | 2014-04-24 19:13:59 +0800 | [diff] [blame] | 345 | dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 346 | goto dai_link_of_err; |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 347 | } |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 348 | |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 349 | cpu_dai = |
| 350 | dai_props->cpu_dai = &priv->dais[(*dai_idx)++]; |
| 351 | codec_dai = |
| 352 | dai_props->codec_dai = &priv->dais[(*dai_idx)++]; |
| 353 | |
Kuninori Morimoto | cecdef3 | 2016-06-30 06:02:46 +0000 | [diff] [blame] | 354 | ret = asoc_simple_card_parse_daifmt(dev, node, codec, |
| 355 | prefix, &dai_link->dai_fmt); |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 356 | if (ret < 0) |
| 357 | goto dai_link_of_err; |
| 358 | |
Kuninori Morimoto | 79e8349 | 2018-12-14 11:35:15 +0900 | [diff] [blame] | 359 | snprintf(prop, sizeof(prop), "%smclk-fs", prefix); |
| 360 | of_property_read_u32(top, PREFIX "mclk-fs", &dai_props->mclk_fs); |
| 361 | of_property_read_u32(node, prop, &dai_props->mclk_fs); |
| 362 | of_property_read_u32(cpu, prop, &dai_props->mclk_fs); |
| 363 | of_property_read_u32(codec, prop, &dai_props->mclk_fs); |
Arnaud Pouliquen | 85a4bfd | 2015-06-05 10:19:05 +0200 | [diff] [blame] | 364 | |
Kuninori Morimoto | 44c16af | 2016-08-08 06:02:07 +0000 | [diff] [blame] | 365 | ret = asoc_simple_card_parse_cpu(cpu, dai_link, |
| 366 | DAI, CELL, &single_cpu); |
| 367 | if (ret < 0) |
| 368 | goto dai_link_of_err; |
| 369 | |
| 370 | ret = asoc_simple_card_parse_codec(codec, dai_link, DAI, CELL); |
| 371 | if (ret < 0) |
| 372 | goto dai_link_of_err; |
| 373 | |
| 374 | ret = asoc_simple_card_parse_platform(plat, dai_link, DAI, CELL); |
| 375 | if (ret < 0) |
| 376 | goto dai_link_of_err; |
| 377 | |
Kuninori Morimoto | b93d2cf | 2017-06-14 00:34:53 +0000 | [diff] [blame] | 378 | ret = asoc_simple_card_of_parse_tdm(cpu, cpu_dai); |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 379 | if (ret < 0) |
| 380 | goto dai_link_of_err; |
| 381 | |
Kuninori Morimoto | b93d2cf | 2017-06-14 00:34:53 +0000 | [diff] [blame] | 382 | ret = asoc_simple_card_of_parse_tdm(codec, codec_dai); |
Kuninori Morimoto | 5fb9cb1 | 2016-05-20 09:40:41 +0000 | [diff] [blame] | 383 | if (ret < 0) |
| 384 | goto dai_link_of_err; |
| 385 | |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 386 | ret = asoc_simple_card_parse_clk_cpu(dev, cpu, dai_link, cpu_dai); |
Kuninori Morimoto | 28abd99 | 2016-07-19 02:53:13 +0000 | [diff] [blame] | 387 | if (ret < 0) |
| 388 | goto dai_link_of_err; |
| 389 | |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 390 | ret = asoc_simple_card_parse_clk_codec(dev, codec, dai_link, codec_dai); |
Kuninori Morimoto | 28abd99 | 2016-07-19 02:53:13 +0000 | [diff] [blame] | 391 | if (ret < 0) |
| 392 | goto dai_link_of_err; |
| 393 | |
Kuninori Morimoto | c958374 | 2016-08-09 05:50:02 +0000 | [diff] [blame] | 394 | ret = asoc_simple_card_canonicalize_dailink(dai_link); |
| 395 | if (ret < 0) |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 396 | goto dai_link_of_err; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 397 | |
Kuninori Morimoto | 2e8d1c7 | 2016-07-11 23:57:34 +0000 | [diff] [blame] | 398 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 399 | "%s-%s", |
| 400 | dai_link->cpu_dai_name, |
Kuninori Morimoto | 710af91 | 2018-08-31 03:08:24 +0000 | [diff] [blame] | 401 | dai_link->codecs->dai_name); |
Kuninori Morimoto | 2e8d1c7 | 2016-07-11 23:57:34 +0000 | [diff] [blame] | 402 | if (ret < 0) |
Vishal Thanki | 31f3032 | 2015-03-03 18:59:00 +0530 | [diff] [blame] | 403 | goto dai_link_of_err; |
Vishal Thanki | 31f3032 | 2015-03-03 18:59:00 +0530 | [diff] [blame] | 404 | |
Andrew Lunn | 2942a0e | 2014-05-22 17:31:49 +0200 | [diff] [blame] | 405 | dai_link->ops = &asoc_simple_card_ops; |
Kuninori Morimoto | a5960bd | 2014-08-27 20:08:27 -0700 | [diff] [blame] | 406 | dai_link->init = asoc_simple_card_dai_init; |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 407 | |
Kuninori Morimoto | 16f1de6 | 2016-08-10 02:20:43 +0000 | [diff] [blame] | 408 | asoc_simple_card_canonicalize_cpu(dai_link, single_cpu); |
Kuninori Morimoto | 179949b | 2014-08-27 20:08:06 -0700 | [diff] [blame] | 409 | |
Jyri Sarha | b3ca11f | 2014-03-24 12:15:25 +0200 | [diff] [blame] | 410 | dai_link_of_err: |
Kuninori Morimoto | 1b5721b | 2014-10-27 18:04:52 -0700 | [diff] [blame] | 411 | of_node_put(cpu); |
| 412 | of_node_put(codec); |
| 413 | |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 414 | return ret; |
| 415 | } |
| 416 | |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 417 | static int asoc_simple_card_parse_aux_devs(struct device_node *node, |
| 418 | struct simple_card_data *priv) |
| 419 | { |
| 420 | struct device *dev = simple_priv_to_dev(priv); |
| 421 | struct device_node *aux_node; |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 422 | struct snd_soc_card *card = simple_priv_to_card(priv); |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 423 | int i, n, len; |
| 424 | |
| 425 | if (!of_find_property(node, PREFIX "aux-devs", &len)) |
| 426 | return 0; /* Ok to have no aux-devs */ |
| 427 | |
| 428 | n = len / sizeof(__be32); |
| 429 | if (n <= 0) |
| 430 | return -EINVAL; |
| 431 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 432 | card->aux_dev = devm_kcalloc(dev, |
| 433 | n, sizeof(*card->aux_dev), GFP_KERNEL); |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 434 | if (!card->aux_dev) |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 435 | return -ENOMEM; |
| 436 | |
| 437 | for (i = 0; i < n; i++) { |
| 438 | aux_node = of_parse_phandle(node, PREFIX "aux-devs", i); |
| 439 | if (!aux_node) |
| 440 | return -EINVAL; |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 441 | card->aux_dev[i].codec_of_node = aux_node; |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 442 | } |
| 443 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 444 | card->num_aux_devs = n; |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
Kuninori Morimoto | d2fdcc2 | 2017-06-07 00:37:05 +0000 | [diff] [blame] | 448 | static int asoc_simple_card_parse_of(struct simple_card_data *priv) |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 449 | { |
Kuninori Morimoto | f531913f | 2014-09-09 21:37:57 -0700 | [diff] [blame] | 450 | struct device *dev = simple_priv_to_dev(priv); |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 451 | struct device_node *top = dev->of_node; |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 452 | struct snd_soc_card *card = simple_priv_to_card(priv); |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 453 | struct device_node *node; |
| 454 | struct device_node *np; |
| 455 | struct device_node *codec; |
| 456 | bool is_fe; |
| 457 | int ret, loop; |
| 458 | int dai_idx, link_idx, conf_idx; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 459 | |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 460 | if (!top) |
Xiubo Li | 2080437 | 2014-09-03 10:23:39 +0800 | [diff] [blame] | 461 | return -EINVAL; |
| 462 | |
Kuninori Morimoto | fa2760dd | 2017-06-16 01:39:11 +0000 | [diff] [blame] | 463 | ret = asoc_simple_card_of_parse_widgets(card, PREFIX); |
| 464 | if (ret < 0) |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 465 | return ret; |
Xiubo Li | 9d681f5 | 2014-02-08 15:59:53 +0800 | [diff] [blame] | 466 | |
Kuninori Morimoto | 33404f3 | 2018-11-21 02:11:13 +0000 | [diff] [blame] | 467 | ret = asoc_simple_card_of_parse_routing(card, PREFIX); |
Kuninori Morimoto | 1fdb5d2 | 2017-06-15 00:25:17 +0000 | [diff] [blame] | 468 | if (ret < 0) |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 469 | return ret; |
Xiubo Li | d4c2209 | 2013-12-23 12:57:01 +0800 | [diff] [blame] | 470 | |
Xiubo Li | 2080437 | 2014-09-03 10:23:39 +0800 | [diff] [blame] | 471 | /* Single/Muti DAI link(s) & New style of DT node */ |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 472 | loop = 1; |
| 473 | link_idx = 0; |
| 474 | dai_idx = 0; |
| 475 | conf_idx = 0; |
| 476 | node = of_get_child_by_name(top, PREFIX "dai-link"); |
| 477 | if (!node) { |
| 478 | node = dev->of_node; |
| 479 | loop = 0; |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 480 | } |
Xiubo Li | dd41e0c | 2013-12-20 14:39:50 +0800 | [diff] [blame] | 481 | |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 482 | do { |
| 483 | /* DPCM */ |
| 484 | if (of_get_child_count(node) > 2) { |
| 485 | for_each_child_of_node(node, np) { |
| 486 | codec = of_get_child_by_name(node, |
| 487 | loop ? "codec" : |
| 488 | PREFIX "codec"); |
| 489 | if (!codec) |
| 490 | return -ENODEV; |
| 491 | |
| 492 | is_fe = (np != codec); |
| 493 | |
| 494 | ret = asoc_simple_card_dai_link_of_dpcm( |
Kuninori Morimoto | 79e8349 | 2018-12-14 11:35:15 +0900 | [diff] [blame] | 495 | top, node, np, codec, priv, |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 496 | &dai_idx, link_idx++, &conf_idx, |
| 497 | is_fe, !loop); |
| 498 | } |
| 499 | } else { |
| 500 | ret = asoc_simple_card_dai_link_of( |
Kuninori Morimoto | 79e8349 | 2018-12-14 11:35:15 +0900 | [diff] [blame] | 501 | top, node, priv, |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 502 | &dai_idx, link_idx++, !loop); |
| 503 | } |
| 504 | if (ret < 0) |
| 505 | return ret; |
| 506 | |
| 507 | node = of_get_next_child(top, node); |
| 508 | } while (loop && node); |
| 509 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 510 | ret = asoc_simple_card_parse_card_name(card, PREFIX); |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 511 | if (ret < 0) |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 512 | return ret; |
Nikita Yushchenko | 899a247 | 2016-09-26 12:56:51 +0300 | [diff] [blame] | 513 | |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 514 | ret = asoc_simple_card_parse_aux_devs(top, priv); |
Kuninori Morimoto | a03b054 | 2016-08-26 03:05:16 +0000 | [diff] [blame] | 515 | |
| 516 | return ret; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 517 | } |
| 518 | |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 519 | static void asoc_simple_card_get_dais_count(struct device *dev, |
| 520 | int *link_num, |
| 521 | int *dais_num, |
| 522 | int *ccnf_num) |
| 523 | { |
| 524 | struct device_node *top = dev->of_node; |
| 525 | struct device_node *node; |
| 526 | int loop; |
| 527 | int num; |
| 528 | |
| 529 | /* |
| 530 | * link_num : number of links. |
| 531 | * CPU-Codec / CPU-dummy / dummy-Codec |
| 532 | * dais_num : number of DAIs |
| 533 | * ccnf_num : number of codec_conf |
| 534 | * same number for "dummy-Codec" |
| 535 | * |
| 536 | * ex1) |
| 537 | * CPU0 --- Codec0 link : 5 |
| 538 | * CPU1 --- Codec1 dais : 7 |
| 539 | * CPU2 -/ ccnf : 1 |
| 540 | * CPU3 --- Codec2 |
| 541 | * |
| 542 | * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec |
| 543 | * => 7 DAIs = 4xCPU + 3xCodec |
| 544 | * => 1 ccnf = 1xdummy-Codec |
| 545 | * |
| 546 | * ex2) |
| 547 | * CPU0 --- Codec0 link : 5 |
| 548 | * CPU1 --- Codec1 dais : 6 |
| 549 | * CPU2 -/ ccnf : 1 |
| 550 | * CPU3 -/ |
| 551 | * |
| 552 | * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec |
| 553 | * => 6 DAIs = 4xCPU + 2xCodec |
| 554 | * => 1 ccnf = 1xdummy-Codec |
| 555 | * |
| 556 | * ex3) |
| 557 | * CPU0 --- Codec0 link : 6 |
| 558 | * CPU1 -/ dais : 6 |
| 559 | * CPU2 --- Codec1 ccnf : 2 |
| 560 | * CPU3 -/ |
| 561 | * |
| 562 | * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec |
| 563 | * => 6 DAIs = 4xCPU + 2xCodec |
| 564 | * => 2 ccnf = 2xdummy-Codec |
| 565 | */ |
| 566 | if (!top) { |
| 567 | (*link_num) = 1; |
| 568 | (*dais_num) = 2; |
| 569 | (*ccnf_num) = 0; |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | loop = 1; |
| 574 | node = of_get_child_by_name(top, PREFIX "dai-link"); |
| 575 | if (!node) { |
| 576 | node = top; |
| 577 | loop = 0; |
| 578 | } |
| 579 | |
| 580 | do { |
| 581 | num = of_get_child_count(node); |
| 582 | (*dais_num) += num; |
| 583 | if (num > 2) { |
| 584 | (*link_num) += num; |
| 585 | (*ccnf_num)++; |
| 586 | } else { |
| 587 | (*link_num)++; |
| 588 | } |
| 589 | node = of_get_next_child(top, node); |
| 590 | } while (loop && node); |
| 591 | } |
| 592 | |
Katsuhiro Suzuki | 8d1bd11 | 2018-06-11 17:32:13 +0900 | [diff] [blame] | 593 | static int asoc_simple_soc_card_probe(struct snd_soc_card *card) |
| 594 | { |
| 595 | struct simple_card_data *priv = snd_soc_card_get_drvdata(card); |
| 596 | int ret; |
| 597 | |
| 598 | ret = asoc_simple_card_init_hp(card, &priv->hp_jack, PREFIX); |
| 599 | if (ret < 0) |
| 600 | return ret; |
| 601 | |
| 602 | ret = asoc_simple_card_init_mic(card, &priv->mic_jack, PREFIX); |
| 603 | if (ret < 0) |
| 604 | return ret; |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 609 | static int asoc_simple_card_probe(struct platform_device *pdev) |
| 610 | { |
Jean-Francois Moine | 45fce59 | 2014-01-15 16:51:56 +0100 | [diff] [blame] | 611 | struct simple_card_data *priv; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 612 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 613 | struct simple_dai_props *dai_props; |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 614 | struct asoc_simple_dai *dais; |
Kuninori Morimoto | f89983e | 2012-12-25 22:52:33 -0800 | [diff] [blame] | 615 | struct device *dev = &pdev->dev; |
Kuninori Morimoto | dcee9bfe | 2017-03-15 04:43:21 +0000 | [diff] [blame] | 616 | struct device_node *np = dev->of_node; |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 617 | struct snd_soc_card *card; |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 618 | struct snd_soc_codec_conf *cconf; |
| 619 | int lnum = 0, dnum = 0, cnum = 0; |
| 620 | int ret, i; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 621 | |
Xiubo Li | 0dd4fc3 | 2014-09-10 09:59:55 +0800 | [diff] [blame] | 622 | /* Allocate the private data and the DAI link array */ |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 623 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 624 | if (!priv) |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 625 | return -ENOMEM; |
| 626 | |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 627 | asoc_simple_card_get_dais_count(dev, &lnum, &dnum, &cnum); |
| 628 | if (!lnum || !dnum) |
| 629 | return -EINVAL; |
| 630 | |
| 631 | dai_props = devm_kcalloc(dev, lnum, sizeof(*dai_props), GFP_KERNEL); |
| 632 | dai_link = devm_kcalloc(dev, lnum, sizeof(*dai_link), GFP_KERNEL); |
| 633 | dais = devm_kcalloc(dev, dnum, sizeof(*dais), GFP_KERNEL); |
| 634 | cconf = devm_kcalloc(dev, cnum, sizeof(*cconf), GFP_KERNEL); |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 635 | if (!dai_props || !dai_link || !dais) |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 636 | return -ENOMEM; |
| 637 | |
Kuninori Morimoto | 710af91 | 2018-08-31 03:08:24 +0000 | [diff] [blame] | 638 | /* |
| 639 | * Use snd_soc_dai_link_component instead of legacy style |
| 640 | * It is codec only. but cpu/platform will be supported in the future. |
| 641 | * see |
| 642 | * soc-core.c :: snd_soc_init_multicodec() |
| 643 | */ |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 644 | for (i = 0; i < lnum; i++) { |
Kuninori Morimoto | 710af91 | 2018-08-31 03:08:24 +0000 | [diff] [blame] | 645 | dai_link[i].codecs = &dai_props[i].codecs; |
| 646 | dai_link[i].num_codecs = 1; |
Kuninori Morimoto | e58f41e | 2018-08-31 03:10:33 +0000 | [diff] [blame] | 647 | dai_link[i].platform = &dai_props[i].platform; |
Kuninori Morimoto | 710af91 | 2018-08-31 03:08:24 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 650 | priv->dai_props = dai_props; |
| 651 | priv->dai_link = dai_link; |
Kuninori Morimoto | 4fb7f4d | 2018-12-04 08:19:43 +0000 | [diff] [blame] | 652 | priv->dais = dais; |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 653 | priv->codec_conf = cconf; |
Kuninori Morimoto | b0133d9 | 2016-08-26 03:10:25 +0000 | [diff] [blame] | 654 | |
Xiubo Li | 0dd4fc3 | 2014-09-10 09:59:55 +0800 | [diff] [blame] | 655 | /* Init snd_soc_card */ |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 656 | card = simple_priv_to_card(priv); |
| 657 | card->owner = THIS_MODULE; |
| 658 | card->dev = dev; |
| 659 | card->dai_link = priv->dai_link; |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 660 | card->num_links = lnum; |
| 661 | card->codec_conf = cconf; |
| 662 | card->num_configs = cnum; |
Katsuhiro Suzuki | 8d1bd11 | 2018-06-11 17:32:13 +0900 | [diff] [blame] | 663 | card->probe = asoc_simple_soc_card_probe; |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 664 | |
Jean-Francois Moine | 201a0ea | 2014-01-15 16:51:41 +0100 | [diff] [blame] | 665 | if (np && of_device_is_available(np)) { |
| 666 | |
Kuninori Morimoto | d2fdcc2 | 2017-06-07 00:37:05 +0000 | [diff] [blame] | 667 | ret = asoc_simple_card_parse_of(priv); |
Xiubo Li | ca919fe | 2014-01-14 12:35:32 +0800 | [diff] [blame] | 668 | if (ret < 0) { |
| 669 | if (ret != -EPROBE_DEFER) |
| 670 | dev_err(dev, "parse error %d\n", ret); |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 671 | goto err; |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 672 | } |
Jean-Francois Moine | 6a91a17bd | 2014-03-20 11:49:55 +0100 | [diff] [blame] | 673 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 674 | } else { |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 675 | struct asoc_simple_card_info *cinfo; |
Kuninori Morimoto | 710af91 | 2018-08-31 03:08:24 +0000 | [diff] [blame] | 676 | struct snd_soc_dai_link_component *codecs; |
Kuninori Morimoto | e58f41e | 2018-08-31 03:10:33 +0000 | [diff] [blame] | 677 | struct snd_soc_dai_link_component *platform; |
Nathan Chancellor | 869858f | 2018-12-13 14:15:20 +0900 | [diff] [blame] | 678 | int dai_idx = 0; |
Jean-Francois Moine | ca65b49 | 2014-01-15 16:51:52 +0100 | [diff] [blame] | 679 | |
| 680 | cinfo = dev->platform_data; |
| 681 | if (!cinfo) { |
Xiubo Li | 34787d0a | 2014-01-09 17:49:40 +0800 | [diff] [blame] | 682 | dev_err(dev, "no info for asoc-simple-card\n"); |
| 683 | return -EINVAL; |
| 684 | } |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 685 | |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 686 | if (!cinfo->name || |
| 687 | !cinfo->codec_dai.name || |
| 688 | !cinfo->codec || |
| 689 | !cinfo->platform || |
Jean-Francois Moine | 7722f83 | 2014-01-15 16:51:33 +0100 | [diff] [blame] | 690 | !cinfo->cpu_dai.name) { |
| 691 | dev_err(dev, "insufficient asoc_simple_card_info settings\n"); |
| 692 | return -EINVAL; |
| 693 | } |
Jean-Francois Moine | 2bee991 | 2014-01-15 16:51:37 +0100 | [diff] [blame] | 694 | |
Nathan Chancellor | 869858f | 2018-12-13 14:15:20 +0900 | [diff] [blame] | 695 | dai_props->cpu_dai = &priv->dais[dai_idx++]; |
| 696 | dai_props->codec_dai = &priv->dais[dai_idx++]; |
| 697 | |
Kuninori Morimoto | 710af91 | 2018-08-31 03:08:24 +0000 | [diff] [blame] | 698 | codecs = dai_link->codecs; |
| 699 | codecs->name = cinfo->codec; |
| 700 | codecs->dai_name = cinfo->codec_dai.name; |
| 701 | |
Kuninori Morimoto | e58f41e | 2018-08-31 03:10:33 +0000 | [diff] [blame] | 702 | platform = dai_link->platform; |
| 703 | platform->name = cinfo->platform; |
| 704 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 705 | card->name = (cinfo->card) ? cinfo->card : cinfo->name; |
Jean-Francois Moine | 5ca8ba4 | 2014-01-15 16:51:45 +0100 | [diff] [blame] | 706 | dai_link->name = cinfo->name; |
| 707 | dai_link->stream_name = cinfo->name; |
Jean-Francois Moine | 5200847 | 2014-01-15 16:51:48 +0100 | [diff] [blame] | 708 | dai_link->cpu_dai_name = cinfo->cpu_dai.name; |
Lars-Peter Clausen | 1efb53a | 2015-03-24 01:07:08 +0000 | [diff] [blame] | 709 | dai_link->dai_fmt = cinfo->daifmt; |
Kuninori Morimoto | a5960bd | 2014-08-27 20:08:27 -0700 | [diff] [blame] | 710 | dai_link->init = asoc_simple_card_dai_init; |
Nathan Chancellor | 869858f | 2018-12-13 14:15:20 +0900 | [diff] [blame] | 711 | memcpy(priv->dai_props->cpu_dai, &cinfo->cpu_dai, |
| 712 | sizeof(*priv->dai_props->cpu_dai)); |
| 713 | memcpy(priv->dai_props->codec_dai, &cinfo->codec_dai, |
| 714 | sizeof(*priv->dai_props->codec_dai)); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 717 | snd_soc_card_set_drvdata(card, priv); |
Xiubo Li | ba194a4 | 2014-01-13 17:08:08 +0800 | [diff] [blame] | 718 | |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 719 | ret = devm_snd_soc_register_card(dev, card); |
Kuninori Morimoto | c6d112e | 2017-05-19 00:57:21 +0000 | [diff] [blame] | 720 | if (ret < 0) |
| 721 | goto err; |
| 722 | |
| 723 | return 0; |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 724 | err: |
Kuninori Morimoto | 5be5095 | 2017-03-15 04:44:00 +0000 | [diff] [blame] | 725 | asoc_simple_card_clean_reference(card); |
Kuninori Morimoto | 387f582 | 2016-08-26 03:07:59 +0000 | [diff] [blame] | 726 | |
Jean-Francois Moine | e512e00 | 2014-03-11 10:03:40 +0100 | [diff] [blame] | 727 | return ret; |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Xiubo Li | e3c4a28 | 2014-09-01 14:46:52 +0800 | [diff] [blame] | 730 | static int asoc_simple_card_remove(struct platform_device *pdev) |
| 731 | { |
Dylan Reid | 3fe2403 | 2014-10-01 14:25:20 -0700 | [diff] [blame] | 732 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
Dylan Reid | 3fe2403 | 2014-10-01 14:25:20 -0700 | [diff] [blame] | 733 | |
Kuninori Morimoto | 885fc05 | 2016-08-10 02:21:42 +0000 | [diff] [blame] | 734 | return asoc_simple_card_clean_reference(card); |
Xiubo Li | e3c4a28 | 2014-09-01 14:46:52 +0800 | [diff] [blame] | 735 | } |
| 736 | |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 737 | static const struct of_device_id asoc_simple_of_match[] = { |
| 738 | { .compatible = "simple-audio-card", }, |
Kuninori Morimoto | da21535 | 2018-12-14 11:35:10 +0900 | [diff] [blame] | 739 | { .compatible = "simple-scu-audio-card", }, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 740 | {}, |
| 741 | }; |
| 742 | MODULE_DEVICE_TABLE(of, asoc_simple_of_match); |
| 743 | |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 744 | static struct platform_driver asoc_simple_card = { |
| 745 | .driver = { |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 746 | .name = "asoc-simple-card", |
Peter Ujfalusi | 7c37671 | 2016-05-09 13:38:10 +0300 | [diff] [blame] | 747 | .pm = &snd_soc_pm_ops, |
Kuninori Morimoto | fa558c2 | 2013-11-20 15:25:02 +0900 | [diff] [blame] | 748 | .of_match_table = asoc_simple_of_match, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 749 | }, |
Nicolin Chen | 781cbeb | 2014-04-24 19:14:00 +0800 | [diff] [blame] | 750 | .probe = asoc_simple_card_probe, |
Xiubo Li | e3c4a28 | 2014-09-01 14:46:52 +0800 | [diff] [blame] | 751 | .remove = asoc_simple_card_remove, |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 752 | }; |
| 753 | |
| 754 | module_platform_driver(asoc_simple_card); |
| 755 | |
Fabio Estevam | c445be3 | 2013-08-23 14:35:17 -0300 | [diff] [blame] | 756 | MODULE_ALIAS("platform:asoc-simple-card"); |
Kuninori Morimoto | a1dbfd0 | 2016-08-26 03:07:28 +0000 | [diff] [blame] | 757 | MODULE_LICENSE("GPL v2"); |
Kuninori Morimoto | f239088 | 2012-04-08 21:17:50 -0700 | [diff] [blame] | 758 | MODULE_DESCRIPTION("ASoC Simple Sound Card"); |
| 759 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |