blob: d16e894fce2b182e7502b78f266a0e058708a9f4 [file] [log] [blame]
Kuninori Morimotod5734542018-07-02 06:30:28 +00001// 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 Morimotofa558c22013-11-20 15:25:02 +09008#include <linux/clk.h>
Xiubo Li6ff62ee2014-02-14 09:34:36 +08009#include <linux/device.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070010#include <linux/module.h>
Kuninori Morimotofa558c22013-11-20 15:25:02 +090011#include <linux/of.h>
Kuninori Morimoto42bf0292019-04-04 09:52:52 +090012#include <linux/of_device.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070013#include <linux/platform_device.h>
Xiubo Lica919fe2014-01-14 12:35:32 +080014#include <linux/string.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070015#include <sound/simple_card.h>
Xiubo Li6ff62ee2014-02-14 09:34:36 +080016#include <sound/soc-dai.h>
17#include <sound/soc.h>
Kuninori Morimotof2390882012-04-08 21:17:50 -070018
Kuninori Morimoto42bf0292019-04-04 09:52:52 +090019#define DPCM_SELECTABLE 1
20
Kuninori Morimoto44c16af2016-08-08 06:02:07 +000021#define DAI "sound-dai"
22#define CELL "#sound-dai-cells"
Kuninori Morimoto548563f2016-05-31 08:59:01 +000023#define PREFIX "simple-audio-card,"
24
Kuninori Morimoto2d01a842018-12-20 10:47:34 +090025static const struct snd_soc_ops simple_ops = {
Kuninori Morimotof38df5b2019-03-20 13:55:14 +090026 .startup = asoc_simple_startup,
Kuninori Morimoto686911b2019-03-20 13:55:27 +090027 .shutdown = asoc_simple_shutdown,
Kuninori Morimotof48dcbb2019-03-20 13:55:39 +090028 .hw_params = asoc_simple_hw_params,
Andrew Lunn2942a0e2014-05-22 17:31:49 +020029};
30
Kuninori Morimotoad11e592019-03-20 13:56:50 +090031static int asoc_simple_parse_dai(struct device_node *node,
32 struct snd_soc_dai_link_component *dlc,
33 struct device_node **dai_of_node,
34 const char **dai_name,
35 int *is_single_link)
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090036{
37 struct of_phandle_args args;
38 int ret;
39
40 if (!node)
41 return 0;
42
43 /*
44 * Use snd_soc_dai_link_component instead of legacy style.
45 * It is only for codec, but cpu will be supported in the future.
46 * see
47 * soc-core.c :: snd_soc_init_multicodec()
48 */
49 if (dlc) {
50 dai_name = &dlc->dai_name;
51 dai_of_node = &dlc->of_node;
52 }
53
54 /*
55 * Get node via "sound-dai = <&phandle port>"
56 * it will be used as xxx_of_node on soc_bind_dai_link()
57 */
58 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
59 if (ret)
60 return ret;
61
62 /* Get dai->name */
63 if (dai_name) {
64 ret = snd_soc_of_get_dai_name(node, dai_name);
65 if (ret < 0)
66 return ret;
67 }
68
69 *dai_of_node = args.np;
70
71 if (is_single_link)
72 *is_single_link = !args.args_count;
73
74 return 0;
75}
76
Kuninori Morimotocdc44472019-03-18 13:50:08 +090077static void simple_parse_convert(struct device *dev,
78 struct device_node *np,
Kuninori Morimotoad11e592019-03-20 13:56:50 +090079 struct asoc_simple_data *adata)
Kuninori Morimoto7e5e1f82018-12-20 10:46:42 +090080{
81 struct device_node *top = dev->of_node;
82 struct device_node *node = of_get_parent(np);
83
Kuninori Morimotoad11e592019-03-20 13:56:50 +090084 asoc_simple_parse_convert(dev, top, PREFIX, adata);
85 asoc_simple_parse_convert(dev, node, PREFIX, adata);
86 asoc_simple_parse_convert(dev, node, NULL, adata);
87 asoc_simple_parse_convert(dev, np, NULL, adata);
Kuninori Morimoto7e5e1f82018-12-20 10:46:42 +090088
89 of_node_put(node);
90}
91
Kuninori Morimotod708d352019-03-18 13:50:17 +090092static void simple_parse_mclk_fs(struct device_node *top,
93 struct device_node *cpu,
94 struct device_node *codec,
95 struct simple_dai_props *props,
96 char *prefix)
97{
98 struct device_node *node = of_get_parent(cpu);
99 char prop[128];
100
101 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
102 of_property_read_u32(top, prop, &props->mclk_fs);
103
104 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
105 of_property_read_u32(node, prop, &props->mclk_fs);
106 of_property_read_u32(cpu, prop, &props->mclk_fs);
107 of_property_read_u32(codec, prop, &props->mclk_fs);
108
109 of_node_put(node);
110}
111
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900112static int simple_dai_link_of_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900113 struct device_node *np,
114 struct device_node *codec,
115 struct link_info *li,
116 bool is_top)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900117{
118 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900119 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
120 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900121 struct asoc_simple_dai *dai;
Kuninori Morimotocfc652a2018-12-14 11:35:24 +0900122 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900123 struct device_node *top = dev->of_node;
124 struct device_node *node = of_get_parent(np);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900125 char *prefix = "";
126 int ret;
127
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900128 /*
129 * |CPU |Codec : turn
130 * CPU |Pass |return
131 * Codec |return|Pass
132 * np
133 */
134 if (li->cpu == (np == codec))
135 return 0;
136
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900137 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
138
139 li->link++;
140
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900141 of_node_put(node);
142
Kuninori Morimotoda215352018-12-14 11:35:10 +0900143 /* For single DAI link & old style of DT node */
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900144 if (is_top)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900145 prefix = PREFIX;
146
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900147 if (li->cpu) {
Kuninori Morimotoda215352018-12-14 11:35:10 +0900148 int is_single_links = 0;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900149
150 /* BE is dummy */
Kuninori Morimotoda215352018-12-14 11:35:10 +0900151 codecs->of_node = NULL;
152 codecs->dai_name = "snd-soc-dummy-dai";
153 codecs->name = "snd-soc-dummy";
154
155 /* FE settings */
156 dai_link->dynamic = 1;
157 dai_link->dpcm_merged_format = 1;
158
159 dai =
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900160 dai_props->cpu_dai = &priv->dais[li->dais++];
Kuninori Morimotoda215352018-12-14 11:35:10 +0900161
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900162 ret = asoc_simple_parse_cpu(np, dai_link, &is_single_links);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900163 if (ret)
164 return ret;
165
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900166 ret = asoc_simple_parse_clk_cpu(dev, np, dai_link, dai);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900167 if (ret < 0)
168 return ret;
169
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900170 ret = asoc_simple_set_dailink_name(dev, dai_link,
171 "fe.%s",
172 dai_link->cpu_dai_name);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900173 if (ret < 0)
174 return ret;
175
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900176 asoc_simple_canonicalize_cpu(dai_link, is_single_links);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900177 } else {
178 struct snd_soc_codec_conf *cconf;
179
180 /* FE is dummy */
181 dai_link->cpu_of_node = NULL;
182 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
183 dai_link->cpu_name = "snd-soc-dummy";
184
185 /* BE settings */
186 dai_link->no_pcm = 1;
Kuninori Morimoto629f7542019-03-20 13:56:06 +0900187 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900188
189 dai =
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900190 dai_props->codec_dai = &priv->dais[li->dais++];
Kuninori Morimotoda215352018-12-14 11:35:10 +0900191
192 cconf =
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900193 dai_props->codec_conf = &priv->codec_conf[li->conf++];
Kuninori Morimotoda215352018-12-14 11:35:10 +0900194
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900195 ret = asoc_simple_parse_codec(np, dai_link);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900196 if (ret < 0)
197 return ret;
198
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900199 ret = asoc_simple_parse_clk_codec(dev, np, dai_link, dai);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900200 if (ret < 0)
201 return ret;
202
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900203 ret = asoc_simple_set_dailink_name(dev, dai_link,
204 "be.%s",
205 codecs->dai_name);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900206 if (ret < 0)
207 return ret;
208
209 /* check "prefix" from top node */
Kuninori Morimotocfc652a2018-12-14 11:35:24 +0900210 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
Kuninori Morimotoda215352018-12-14 11:35:10 +0900211 PREFIX "prefix");
Kuninori Morimotocfc652a2018-12-14 11:35:24 +0900212 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
213 "prefix");
214 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
215 "prefix");
Kuninori Morimotoda215352018-12-14 11:35:10 +0900216 }
217
Kuninori Morimotocdc44472019-03-18 13:50:08 +0900218 simple_parse_convert(dev, np, &dai_props->adata);
Kuninori Morimotod708d352019-03-18 13:50:17 +0900219 simple_parse_mclk_fs(top, np, codec, dai_props, prefix);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900220
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900221 asoc_simple_canonicalize_platform(dai_link);
Kuninori Morimotofe7ed4d2019-01-21 16:40:59 +0900222
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900223 ret = asoc_simple_parse_tdm(np, dai);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900224 if (ret)
225 return ret;
226
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900227 ret = asoc_simple_parse_daifmt(dev, node, codec,
228 prefix, &dai_link->dai_fmt);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900229 if (ret < 0)
230 return ret;
231
232 dai_link->dpcm_playback = 1;
233 dai_link->dpcm_capture = 1;
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900234 dai_link->ops = &simple_ops;
Kuninori Morimotoad934ca2019-03-20 13:55:52 +0900235 dai_link->init = asoc_simple_dai_init;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900236
237 return 0;
238}
239
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900240static int simple_dai_link_of(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900241 struct device_node *np,
242 struct device_node *codec,
243 struct link_info *li,
244 bool is_top)
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100245{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700246 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900247 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
248 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
Kuninori Morimoto4fb7f4d2018-12-04 08:19:43 +0000249 struct asoc_simple_dai *cpu_dai;
250 struct asoc_simple_dai *codec_dai;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900251 struct device_node *top = dev->of_node;
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700252 struct device_node *cpu = NULL;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900253 struct device_node *node = NULL;
Jun Niee0ae2252015-04-29 18:11:07 +0800254 struct device_node *plat = NULL;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200255 char prop[128];
256 char *prefix = "";
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000257 int ret, single_cpu;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100258
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900259 /*
260 * |CPU |Codec : turn
261 * CPU |Pass |return
262 * Codec |return|return
263 * np
264 */
265 if (!li->cpu || np == codec)
266 return 0;
267
268 cpu = np;
269 node = of_get_parent(np);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900270 li->link++;
271
272 dev_dbg(dev, "link_of (%pOF)\n", node);
273
Xiubo Li20804372014-09-03 10:23:39 +0800274 /* For single DAI link & old style of DT node */
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900275 if (is_top)
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000276 prefix = PREFIX;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100277
Jun Niee0ae2252015-04-29 18:11:07 +0800278 snprintf(prop, sizeof(prop), "%splat", prefix);
279 plat = of_get_child_by_name(node, prop);
280
Kuninori Morimoto4fb7f4d2018-12-04 08:19:43 +0000281 cpu_dai =
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900282 dai_props->cpu_dai = &priv->dais[li->dais++];
Kuninori Morimoto4fb7f4d2018-12-04 08:19:43 +0000283 codec_dai =
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900284 dai_props->codec_dai = &priv->dais[li->dais++];
Kuninori Morimoto4fb7f4d2018-12-04 08:19:43 +0000285
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900286 ret = asoc_simple_parse_daifmt(dev, node, codec,
287 prefix, &dai_link->dai_fmt);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700288 if (ret < 0)
289 goto dai_link_of_err;
290
Kuninori Morimotod708d352019-03-18 13:50:17 +0900291 simple_parse_mclk_fs(top, cpu, codec, dai_props, prefix);
Arnaud Pouliquen85a4bfd2015-06-05 10:19:05 +0200292
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900293 ret = asoc_simple_parse_cpu(cpu, dai_link, &single_cpu);
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000294 if (ret < 0)
295 goto dai_link_of_err;
296
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900297 ret = asoc_simple_parse_codec(codec, dai_link);
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000298 if (ret < 0)
299 goto dai_link_of_err;
300
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900301 ret = asoc_simple_parse_platform(plat, dai_link);
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000302 if (ret < 0)
303 goto dai_link_of_err;
304
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900305 ret = asoc_simple_parse_tdm(cpu, cpu_dai);
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200306 if (ret < 0)
307 goto dai_link_of_err;
308
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900309 ret = asoc_simple_parse_tdm(codec, codec_dai);
Kuninori Morimoto5fb9cb12016-05-20 09:40:41 +0000310 if (ret < 0)
311 goto dai_link_of_err;
312
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900313 ret = asoc_simple_parse_clk_cpu(dev, cpu, dai_link, cpu_dai);
Kuninori Morimoto28abd992016-07-19 02:53:13 +0000314 if (ret < 0)
315 goto dai_link_of_err;
316
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900317 ret = asoc_simple_parse_clk_codec(dev, codec, dai_link, codec_dai);
Kuninori Morimoto28abd992016-07-19 02:53:13 +0000318 if (ret < 0)
319 goto dai_link_of_err;
320
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900321 ret = asoc_simple_set_dailink_name(dev, dai_link,
322 "%s-%s",
323 dai_link->cpu_dai_name,
324 dai_link->codecs->dai_name);
Kuninori Morimoto2e8d1c72016-07-11 23:57:34 +0000325 if (ret < 0)
Vishal Thanki31f30322015-03-03 18:59:00 +0530326 goto dai_link_of_err;
Vishal Thanki31f30322015-03-03 18:59:00 +0530327
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900328 dai_link->ops = &simple_ops;
Kuninori Morimotoad934ca2019-03-20 13:55:52 +0900329 dai_link->init = asoc_simple_dai_init;
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200330
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900331 asoc_simple_canonicalize_cpu(dai_link, single_cpu);
332 asoc_simple_canonicalize_platform(dai_link);
Kuninori Morimoto179949b2014-08-27 20:08:06 -0700333
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200334dai_link_of_err:
Takashi Iwai44662f92019-02-19 16:46:48 +0100335 of_node_put(plat);
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900336 of_node_put(node);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700337
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100338 return ret;
339}
340
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900341static int simple_for_each_link(struct asoc_simple_priv *priv,
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900342 struct link_info *li,
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900343 int (*func_noml)(struct asoc_simple_priv *priv,
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900344 struct device_node *np,
345 struct device_node *codec,
346 struct link_info *li, bool is_top),
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900347 int (*func_dpcm)(struct asoc_simple_priv *priv,
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900348 struct device_node *np,
349 struct device_node *codec,
350 struct link_info *li, bool is_top))
351{
352 struct device *dev = simple_priv_to_dev(priv);
353 struct device_node *top = dev->of_node;
354 struct device_node *node;
Kuninori Morimoto42bf0292019-04-04 09:52:52 +0900355 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900356 bool is_top = 0;
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100357 int ret = 0;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900358
359 /* Check if it has dai-link */
360 node = of_get_child_by_name(top, PREFIX "dai-link");
361 if (!node) {
Daniel Baluta461d8542019-02-16 10:09:42 +0000362 node = of_node_get(top);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900363 is_top = 1;
364 }
365
366 /* loop for all dai-link */
367 do {
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900368 struct asoc_simple_data adata;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900369 struct device_node *codec;
370 struct device_node *np;
371 int num = of_get_child_count(node);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900372
373 /* get codec */
374 codec = of_get_child_by_name(node, is_top ?
375 PREFIX "codec" : "codec");
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100376 if (!codec) {
377 ret = -ENODEV;
378 goto error;
379 }
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900380
381 of_node_put(codec);
382
383 /* get convert-xxx property */
384 memset(&adata, 0, sizeof(adata));
385 for_each_child_of_node(node, np)
Kuninori Morimotocdc44472019-03-18 13:50:08 +0900386 simple_parse_convert(dev, np, &adata);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900387
388 /* loop for all CPU/Codec node */
389 for_each_child_of_node(node, np) {
390 /*
391 * It is DPCM
392 * if it has many CPUs,
393 * or has convert-xxx property
394 */
Kuninori Morimoto42bf0292019-04-04 09:52:52 +0900395 if (dpcm_selectable &&
396 (num > 2 ||
397 adata.convert_rate || adata.convert_channels))
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900398 ret = func_dpcm(priv, np, codec, li, is_top);
399 /* else normal sound */
400 else
401 ret = func_noml(priv, np, codec, li, is_top);
402
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100403 if (ret < 0) {
404 of_node_put(np);
405 goto error;
406 }
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900407 }
408
409 node = of_get_next_child(top, node);
410 } while (!is_top && node);
411
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100412 error:
413 of_node_put(node);
414 return ret;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900415}
416
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900417static int simple_parse_aux_devs(struct device_node *node,
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900418 struct asoc_simple_priv *priv)
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300419{
420 struct device *dev = simple_priv_to_dev(priv);
421 struct device_node *aux_node;
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000422 struct snd_soc_card *card = simple_priv_to_card(priv);
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300423 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 Cooka86854d2018-06-12 14:07:58 -0700432 card->aux_dev = devm_kcalloc(dev,
433 n, sizeof(*card->aux_dev), GFP_KERNEL);
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000434 if (!card->aux_dev)
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300435 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 Morimoto5be50952017-03-15 04:44:00 +0000441 card->aux_dev[i].codec_of_node = aux_node;
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300442 }
443
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000444 card->num_aux_devs = n;
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300445 return 0;
446}
447
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900448static int simple_parse_of(struct asoc_simple_priv *priv)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900449{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700450 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900451 struct device_node *top = dev->of_node;
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000452 struct snd_soc_card *card = simple_priv_to_card(priv);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900453 struct link_info li;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900454 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900455
Kuninori Morimotoda215352018-12-14 11:35:10 +0900456 if (!top)
Xiubo Li20804372014-09-03 10:23:39 +0800457 return -EINVAL;
458
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900459 ret = asoc_simple_parse_widgets(card, PREFIX);
Kuninori Morimotofa2760dd2017-06-16 01:39:11 +0000460 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900461 return ret;
Xiubo Li9d681f52014-02-08 15:59:53 +0800462
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900463 ret = asoc_simple_parse_routing(card, PREFIX);
Kuninori Morimoto1fdb5d22017-06-15 00:25:17 +0000464 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900465 return ret;
Xiubo Lid4c22092013-12-23 12:57:01 +0800466
Paul Cercueil552f2d72019-04-26 04:25:50 +0200467 ret = asoc_simple_parse_pin_switches(card, PREFIX);
468 if (ret < 0)
469 return ret;
470
Xiubo Li20804372014-09-03 10:23:39 +0800471 /* Single/Muti DAI link(s) & New style of DT node */
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900472 memset(&li, 0, sizeof(li));
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900473 for (li.cpu = 1; li.cpu >= 0; li.cpu--) {
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900474 /*
475 * Detect all CPU first, and Detect all Codec 2nd.
476 *
477 * In Normal sound case, all DAIs are detected
478 * as "CPU-Codec".
479 *
480 * In DPCM sound case,
481 * all CPUs are detected as "CPU-dummy", and
482 * all Codecs are detected as "dummy-Codec".
483 * To avoid random sub-device numbering,
484 * detect "dummy-Codec" in last;
485 */
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900486 ret = simple_for_each_link(priv, &li,
487 simple_dai_link_of,
488 simple_dai_link_of_dpcm);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900489 if (ret < 0)
490 return ret;
491 }
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900492
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900493 ret = asoc_simple_parse_card_name(card, PREFIX);
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300494 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900495 return ret;
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300496
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900497 ret = simple_parse_aux_devs(top, priv);
Kuninori Morimotoa03b0542016-08-26 03:05:16 +0000498
499 return ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900500}
501
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900502static int simple_count_noml(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900503 struct device_node *np,
504 struct device_node *codec,
505 struct link_info *li, bool is_top)
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900506{
507 li->dais++; /* CPU or Codec */
508 if (np != codec)
509 li->link++; /* CPU-Codec */
510
511 return 0;
512}
513
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900514static int simple_count_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900515 struct device_node *np,
516 struct device_node *codec,
517 struct link_info *li, bool is_top)
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900518{
519 li->dais++; /* CPU or Codec */
520 li->link++; /* CPU-dummy or dummy-Codec */
521 if (np == codec)
522 li->conf++;
523
524 return 0;
525}
526
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900527static void simple_get_dais_count(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900528 struct link_info *li)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900529{
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900530 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900531 struct device_node *top = dev->of_node;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900532
533 /*
534 * link_num : number of links.
535 * CPU-Codec / CPU-dummy / dummy-Codec
536 * dais_num : number of DAIs
537 * ccnf_num : number of codec_conf
538 * same number for "dummy-Codec"
539 *
540 * ex1)
541 * CPU0 --- Codec0 link : 5
542 * CPU1 --- Codec1 dais : 7
543 * CPU2 -/ ccnf : 1
544 * CPU3 --- Codec2
545 *
546 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
547 * => 7 DAIs = 4xCPU + 3xCodec
548 * => 1 ccnf = 1xdummy-Codec
549 *
550 * ex2)
551 * CPU0 --- Codec0 link : 5
552 * CPU1 --- Codec1 dais : 6
553 * CPU2 -/ ccnf : 1
554 * CPU3 -/
555 *
556 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
557 * => 6 DAIs = 4xCPU + 2xCodec
558 * => 1 ccnf = 1xdummy-Codec
559 *
560 * ex3)
561 * CPU0 --- Codec0 link : 6
562 * CPU1 -/ dais : 6
563 * CPU2 --- Codec1 ccnf : 2
564 * CPU3 -/
565 *
566 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
567 * => 6 DAIs = 4xCPU + 2xCodec
568 * => 2 ccnf = 2xdummy-Codec
Kuninori Morimoto7adee602018-12-20 10:46:47 +0900569 *
570 * ex4)
571 * CPU0 --- Codec0 (convert-rate) link : 3
572 * CPU1 --- Codec1 dais : 4
573 * ccnf : 1
574 *
575 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
576 * => 4 DAIs = 2xCPU + 2xCodec
577 * => 1 ccnf = 1xdummy-Codec
Kuninori Morimotoda215352018-12-14 11:35:10 +0900578 */
579 if (!top) {
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900580 li->link = 1;
581 li->dais = 2;
582 li->conf = 0;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900583 return;
584 }
585
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900586 simple_for_each_link(priv, li,
587 simple_count_noml,
588 simple_count_dpcm);
589
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900590 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
591 li->link, li->dais, li->conf);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900592}
593
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900594static int simple_soc_probe(struct snd_soc_card *card)
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900595{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900596 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900597 int ret;
598
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900599 ret = asoc_simple_init_hp(card, &priv->hp_jack, PREFIX);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900600 if (ret < 0)
601 return ret;
602
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900603 ret = asoc_simple_init_mic(card, &priv->mic_jack, PREFIX);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900604 if (ret < 0)
605 return ret;
606
607 return 0;
608}
609
Amir Goldsteinb0a821d2019-05-16 13:26:28 +0300610static int asoc_simple_probe(struct platform_device *pdev)
Kuninori Morimotof2390882012-04-08 21:17:50 -0700611{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900612 struct asoc_simple_priv *priv;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800613 struct device *dev = &pdev->dev;
Kuninori Morimotodcee9bfe2017-03-15 04:43:21 +0000614 struct device_node *np = dev->of_node;
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000615 struct snd_soc_card *card;
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900616 struct link_info li;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900617 int ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700618
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800619 /* Allocate the private data and the DAI link array */
Kuninori Morimotob0133d92016-08-26 03:10:25 +0000620 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100621 if (!priv)
Xiubo Lica919fe2014-01-14 12:35:32 +0800622 return -ENOMEM;
623
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900624 card = simple_priv_to_card(priv);
625 card->owner = THIS_MODULE;
626 card->dev = dev;
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900627 card->probe = simple_soc_probe;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900628
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900629 memset(&li, 0, sizeof(li));
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900630 simple_get_dais_count(priv, &li);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900631 if (!li.link || !li.dais)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900632 return -EINVAL;
633
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900634 ret = asoc_simple_init_priv(priv, &li);
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900635 if (ret < 0)
636 return ret;
Xiubo Lica919fe2014-01-14 12:35:32 +0800637
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100638 if (np && of_device_is_available(np)) {
639
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900640 ret = simple_parse_of(priv);
Xiubo Lica919fe2014-01-14 12:35:32 +0800641 if (ret < 0) {
642 if (ret != -EPROBE_DEFER)
643 dev_err(dev, "parse error %d\n", ret);
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100644 goto err;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900645 }
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100646
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900647 } else {
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100648 struct asoc_simple_card_info *cinfo;
Kuninori Morimoto710af912018-08-31 03:08:24 +0000649 struct snd_soc_dai_link_component *codecs;
Kuninori Morimotoe58f41e2018-08-31 03:10:33 +0000650 struct snd_soc_dai_link_component *platform;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900651 struct snd_soc_dai_link *dai_link = priv->dai_link;
652 struct simple_dai_props *dai_props = priv->dai_props;
653
Nathan Chancellor869858f2018-12-13 14:15:20 +0900654 int dai_idx = 0;
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100655
656 cinfo = dev->platform_data;
657 if (!cinfo) {
Xiubo Li34787d0a2014-01-09 17:49:40 +0800658 dev_err(dev, "no info for asoc-simple-card\n");
659 return -EINVAL;
660 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900661
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800662 if (!cinfo->name ||
663 !cinfo->codec_dai.name ||
664 !cinfo->codec ||
665 !cinfo->platform ||
Jean-Francois Moine7722f832014-01-15 16:51:33 +0100666 !cinfo->cpu_dai.name) {
667 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
668 return -EINVAL;
669 }
Jean-Francois Moine2bee9912014-01-15 16:51:37 +0100670
Nathan Chancellor869858f2018-12-13 14:15:20 +0900671 dai_props->cpu_dai = &priv->dais[dai_idx++];
672 dai_props->codec_dai = &priv->dais[dai_idx++];
673
Kuninori Morimoto710af912018-08-31 03:08:24 +0000674 codecs = dai_link->codecs;
675 codecs->name = cinfo->codec;
676 codecs->dai_name = cinfo->codec_dai.name;
677
Kuninori Morimoto910fdca2019-01-21 09:32:32 +0900678 platform = dai_link->platforms;
Kuninori Morimotoe58f41e2018-08-31 03:10:33 +0000679 platform->name = cinfo->platform;
680
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000681 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100682 dai_link->name = cinfo->name;
683 dai_link->stream_name = cinfo->name;
Jean-Francois Moine52008472014-01-15 16:51:48 +0100684 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
Lars-Peter Clausen1efb53a2015-03-24 01:07:08 +0000685 dai_link->dai_fmt = cinfo->daifmt;
Kuninori Morimotoad934ca2019-03-20 13:55:52 +0900686 dai_link->init = asoc_simple_dai_init;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900687 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
688 sizeof(*dai_props->cpu_dai));
689 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
690 sizeof(*dai_props->codec_dai));
Kuninori Morimotof2390882012-04-08 21:17:50 -0700691 }
692
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000693 snd_soc_card_set_drvdata(card, priv);
Xiubo Liba194a42014-01-13 17:08:08 +0800694
Kuninori Morimoto0580dde2019-03-20 13:54:42 +0900695 asoc_simple_debug_info(priv);
696
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000697 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimotoc6d112e2017-05-19 00:57:21 +0000698 if (ret < 0)
699 goto err;
700
701 return 0;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100702err:
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900703 asoc_simple_clean_reference(card);
Kuninori Morimoto387f5822016-08-26 03:07:59 +0000704
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100705 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700706}
707
Amir Goldsteinb0a821d2019-05-16 13:26:28 +0300708static int asoc_simple_remove(struct platform_device *pdev)
Xiubo Lie3c4a282014-09-01 14:46:52 +0800709{
Dylan Reid3fe24032014-10-01 14:25:20 -0700710 struct snd_soc_card *card = platform_get_drvdata(pdev);
Dylan Reid3fe24032014-10-01 14:25:20 -0700711
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900712 return asoc_simple_clean_reference(card);
Xiubo Lie3c4a282014-09-01 14:46:52 +0800713}
714
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900715static const struct of_device_id simple_of_match[] = {
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900716 { .compatible = "simple-audio-card", },
Kuninori Morimoto42bf0292019-04-04 09:52:52 +0900717 { .compatible = "simple-scu-audio-card",
718 .data = (void *)DPCM_SELECTABLE },
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900719 {},
720};
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900721MODULE_DEVICE_TABLE(of, simple_of_match);
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900722
Kuninori Morimotof2390882012-04-08 21:17:50 -0700723static struct platform_driver asoc_simple_card = {
724 .driver = {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800725 .name = "asoc-simple-card",
Peter Ujfalusi7c376712016-05-09 13:38:10 +0300726 .pm = &snd_soc_pm_ops,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900727 .of_match_table = simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700728 },
Amir Goldsteinb0a821d2019-05-16 13:26:28 +0300729 .probe = asoc_simple_probe,
730 .remove = asoc_simple_remove,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700731};
732
733module_platform_driver(asoc_simple_card);
734
Fabio Estevamc445be32013-08-23 14:35:17 -0300735MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotoa1dbfd02016-08-26 03:07:28 +0000736MODULE_LICENSE("GPL v2");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700737MODULE_DESCRIPTION("ASoC Simple Sound Card");
738MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");