blob: 78419e18717d65d7bfb56033be72eb2c1629691b [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
Robert Hancocka64067f2022-01-07 15:47:10 -060031static int asoc_simple_parse_platform(struct device_node *node,
32 struct snd_soc_dai_link_component *dlc)
33{
34 struct of_phandle_args args;
35 int ret;
36
37 if (!node)
38 return 0;
39
40 /*
41 * Get node via "sound-dai = <&phandle port>"
42 * it will be used as xxx_of_node on soc_bind_dai_link()
43 */
44 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
45 if (ret)
46 return ret;
47
48 /* dai_name is not required and may not exist for plat component */
49
50 dlc->of_node = args.np;
51
52 return 0;
53}
54
Kuninori Morimotoad11e592019-03-20 13:56:50 +090055static int asoc_simple_parse_dai(struct device_node *node,
56 struct snd_soc_dai_link_component *dlc,
Kuninori Morimotoad11e592019-03-20 13:56:50 +090057 int *is_single_link)
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090058{
59 struct of_phandle_args args;
60 int ret;
61
62 if (!node)
63 return 0;
64
65 /*
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090066 * Get node via "sound-dai = <&phandle port>"
67 * it will be used as xxx_of_node on soc_bind_dai_link()
68 */
69 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
70 if (ret)
71 return ret;
72
Kuninori Morimoto39c66b62019-09-03 17:15:54 +090073 /*
74 * FIXME
75 *
76 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
77 * If user unbinded CPU or Codec driver, but not for Sound Card,
78 * dlc->dai_name is keeping unbinded CPU or Codec
79 * driver's pointer.
80 *
81 * If user re-bind CPU or Codec driver again, ALSA SoC will try
82 * to rebind Card via snd_soc_try_rebind_card(), but because of
83 * above reason, it might can't bind Sound Card.
84 * Because Sound Card is pointing to released dai_name pointer.
85 *
86 * To avoid this rebind Card issue,
87 * 1) It needs to alloc memory to keep dai_name eventhough
88 * CPU or Codec driver was unbinded, or
89 * 2) user need to rebind Sound Card everytime
90 * if he unbinded CPU or Codec.
91 */
Kuninori Morimotof1072942019-06-06 13:07:35 +090092 ret = snd_soc_of_get_dai_name(node, &dlc->dai_name);
93 if (ret < 0)
94 return ret;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090095
Kuninori Morimotof1072942019-06-06 13:07:35 +090096 dlc->of_node = args.np;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090097
98 if (is_single_link)
99 *is_single_link = !args.args_count;
100
101 return 0;
102}
103
Kuninori Morimotocdc44472019-03-18 13:50:08 +0900104static void simple_parse_convert(struct device *dev,
105 struct device_node *np,
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900106 struct asoc_simple_data *adata)
Kuninori Morimoto7e5e1f82018-12-20 10:46:42 +0900107{
108 struct device_node *top = dev->of_node;
109 struct device_node *node = of_get_parent(np);
110
Kuninori Morimotofcfd7632021-04-12 08:53:00 +0900111 asoc_simple_parse_convert(top, PREFIX, adata);
112 asoc_simple_parse_convert(node, PREFIX, adata);
113 asoc_simple_parse_convert(node, NULL, adata);
114 asoc_simple_parse_convert(np, NULL, adata);
Kuninori Morimoto7e5e1f82018-12-20 10:46:42 +0900115
116 of_node_put(node);
117}
118
Kuninori Morimotod708d352019-03-18 13:50:17 +0900119static void simple_parse_mclk_fs(struct device_node *top,
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900120 struct device_node *np,
Kuninori Morimotod708d352019-03-18 13:50:17 +0900121 struct simple_dai_props *props,
122 char *prefix)
123{
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900124 struct device_node *node = of_get_parent(np);
Kuninori Morimotod708d352019-03-18 13:50:17 +0900125 char prop[128];
126
127 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
128 of_property_read_u32(top, prop, &props->mclk_fs);
129
130 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
131 of_property_read_u32(node, prop, &props->mclk_fs);
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900132 of_property_read_u32(np, prop, &props->mclk_fs);
Kuninori Morimotod708d352019-03-18 13:50:17 +0900133
134 of_node_put(node);
135}
136
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900137static int simple_parse_node(struct asoc_simple_priv *priv,
138 struct device_node *np,
139 struct link_info *li,
140 char *prefix,
141 int *cpu)
142{
143 struct device *dev = simple_priv_to_dev(priv);
144 struct device_node *top = dev->of_node;
145 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
146 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
147 struct snd_soc_dai_link_component *dlc;
148 struct asoc_simple_dai *dai;
149 int ret;
150
151 if (cpu) {
152 dlc = asoc_link_to_cpu(dai_link, 0);
153 dai = simple_props_to_dai_cpu(dai_props, 0);
154 } else {
155 dlc = asoc_link_to_codec(dai_link, 0);
156 dai = simple_props_to_dai_codec(dai_props, 0);
157 }
158
159 simple_parse_mclk_fs(top, np, dai_props, prefix);
160
161 ret = asoc_simple_parse_dai(np, dlc, cpu);
162 if (ret)
163 return ret;
164
165 ret = asoc_simple_parse_clk(dev, np, dai, dlc);
166 if (ret)
167 return ret;
168
169 ret = asoc_simple_parse_tdm(np, dai);
170 if (ret)
171 return ret;
172
173 return 0;
174}
175
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900176static int simple_link_init(struct asoc_simple_priv *priv,
177 struct device_node *node,
178 struct device_node *codec,
179 struct link_info *li,
180 char *prefix, char *name)
181{
182 struct device *dev = simple_priv_to_dev(priv);
183 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
184 int ret;
185
186 ret = asoc_simple_parse_daifmt(dev, node, codec,
187 prefix, &dai_link->dai_fmt);
188 if (ret < 0)
189 return 0;
190
191 dai_link->init = asoc_simple_dai_init;
192 dai_link->ops = &simple_ops;
193
194 return asoc_simple_set_dailink_name(dev, dai_link, name);
195}
196
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900197static int simple_dai_link_of_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900198 struct device_node *np,
199 struct device_node *codec,
200 struct link_info *li,
201 bool is_top)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900202{
203 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900204 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
205 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900206 struct device_node *top = dev->of_node;
207 struct device_node *node = of_get_parent(np);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900208 char *prefix = "";
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900209 char dai_name[64];
Kuninori Morimotoda215352018-12-14 11:35:10 +0900210 int ret;
211
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900212 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
213
Kuninori Morimotoda215352018-12-14 11:35:10 +0900214 /* For single DAI link & old style of DT node */
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900215 if (is_top)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900216 prefix = PREFIX;
217
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900218 if (li->cpu) {
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900219 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
220 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100221 int is_single_links = 0;
222
Kuninori Morimoto848704f2019-10-15 11:52:41 +0900223 /* Codec is dummy */
Kuninori Morimotoda215352018-12-14 11:35:10 +0900224
225 /* FE settings */
226 dai_link->dynamic = 1;
227 dai_link->dpcm_merged_format = 1;
228
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900229 ret = simple_parse_node(priv, np, li, prefix, &is_single_links);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900230 if (ret < 0)
Wen Yang724808a2019-07-10 15:25:06 +0800231 goto out_put_node;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900232
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900233 snprintf(dai_name, sizeof(dai_name), "fe.%s", cpus->dai_name);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100234
235 asoc_simple_canonicalize_cpu(cpus, is_single_links);
236 asoc_simple_canonicalize_platform(platforms, cpus);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900237 } else {
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900238 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900239 struct snd_soc_codec_conf *cconf;
240
Kuninori Morimoto848704f2019-10-15 11:52:41 +0900241 /* CPU is dummy */
Kuninori Morimotoda215352018-12-14 11:35:10 +0900242
243 /* BE settings */
244 dai_link->no_pcm = 1;
Kuninori Morimoto629f7542019-03-20 13:56:06 +0900245 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900246
Kuninori Morimoto2c33e202021-04-14 08:20:10 +0900247 cconf = simple_props_to_codec_conf(dai_props, 0);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900248
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900249 ret = simple_parse_node(priv, np, li, prefix, NULL);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100250 if (ret < 0)
251 goto out_put_node;
252
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900253 snprintf(dai_name, sizeof(dai_name), "be.%s", codecs->dai_name);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900254
255 /* check "prefix" from top node */
Kuninori Morimotocfc652a2018-12-14 11:35:24 +0900256 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
Kuninori Morimotoda215352018-12-14 11:35:10 +0900257 PREFIX "prefix");
Kuninori Morimotocfc652a2018-12-14 11:35:24 +0900258 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
259 "prefix");
260 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
261 "prefix");
Kuninori Morimotoda215352018-12-14 11:35:10 +0900262 }
263
Kuninori Morimotocdc44472019-03-18 13:50:08 +0900264 simple_parse_convert(dev, np, &dai_props->adata);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100265
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500266 snd_soc_dai_link_set_capabilities(dai_link);
267
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900268 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900269
Wen Yang724808a2019-07-10 15:25:06 +0800270out_put_node:
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900271 li->link++;
272
Wen Yang724808a2019-07-10 15:25:06 +0800273 of_node_put(node);
274 return ret;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900275}
276
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900277static int simple_dai_link_of(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900278 struct device_node *np,
279 struct device_node *codec,
280 struct link_info *li,
281 bool is_top)
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100282{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700283 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900284 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
Kuninori Morimoto5bb5ac72021-04-14 08:20:04 +0900285 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
286 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
287 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700288 struct device_node *cpu = NULL;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900289 struct device_node *node = NULL;
Jun Niee0ae2252015-04-29 18:11:07 +0800290 struct device_node *plat = NULL;
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900291 char dai_name[64];
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200292 char prop[128];
293 char *prefix = "";
Mark Brown25c4a9b2021-04-23 18:07:54 +0100294 int ret, single_cpu = 0;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100295
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900296 cpu = np;
297 node = of_get_parent(np);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900298
299 dev_dbg(dev, "link_of (%pOF)\n", node);
300
Xiubo Li20804372014-09-03 10:23:39 +0800301 /* For single DAI link & old style of DT node */
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900302 if (is_top)
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000303 prefix = PREFIX;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100304
Jun Niee0ae2252015-04-29 18:11:07 +0800305 snprintf(prop, sizeof(prop), "%splat", prefix);
306 plat = of_get_child_by_name(node, prop);
307
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900308 ret = simple_parse_node(priv, cpu, li, prefix, &single_cpu);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100309 if (ret < 0)
310 goto dai_link_of_err;
311
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900312 ret = simple_parse_node(priv, codec, li, prefix, NULL);
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000313 if (ret < 0)
314 goto dai_link_of_err;
315
Robert Hancocka64067f2022-01-07 15:47:10 -0600316 ret = asoc_simple_parse_platform(plat, platforms);
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000317 if (ret < 0)
318 goto dai_link_of_err;
319
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900320 snprintf(dai_name, sizeof(dai_name),
321 "%s-%s", cpus->dai_name, codecs->dai_name);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100322
323 asoc_simple_canonicalize_cpu(cpus, single_cpu);
324 asoc_simple_canonicalize_platform(platforms, cpus);
325
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900326 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
327
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200328dai_link_of_err:
Takashi Iwai44662f92019-02-19 16:46:48 +0100329 of_node_put(plat);
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900330 of_node_put(node);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700331
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900332 li->link++;
333
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100334 return ret;
335}
336
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900337static int __simple_for_each_link(struct asoc_simple_priv *priv,
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900338 struct link_info *li,
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900339 int (*func_noml)(struct asoc_simple_priv *priv,
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900340 struct device_node *np,
341 struct device_node *codec,
342 struct link_info *li, bool is_top),
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900343 int (*func_dpcm)(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))
347{
348 struct device *dev = simple_priv_to_dev(priv);
349 struct device_node *top = dev->of_node;
350 struct device_node *node;
Kuninori Morimoto42bf0292019-04-04 09:52:52 +0900351 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900352 bool is_top = 0;
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100353 int ret = 0;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900354
355 /* Check if it has dai-link */
356 node = of_get_child_by_name(top, PREFIX "dai-link");
357 if (!node) {
Daniel Baluta461d8542019-02-16 10:09:42 +0000358 node = of_node_get(top);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900359 is_top = 1;
360 }
361
362 /* loop for all dai-link */
363 do {
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900364 struct asoc_simple_data adata;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900365 struct device_node *codec;
Daniel Baluta5525cf02019-12-09 15:53:53 +0200366 struct device_node *plat;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900367 struct device_node *np;
368 int num = of_get_child_count(node);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900369
370 /* get codec */
371 codec = of_get_child_by_name(node, is_top ?
372 PREFIX "codec" : "codec");
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100373 if (!codec) {
374 ret = -ENODEV;
375 goto error;
376 }
Daniel Baluta5525cf02019-12-09 15:53:53 +0200377 /* get platform */
378 plat = of_get_child_by_name(node, is_top ?
379 PREFIX "plat" : "plat");
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900380
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900381 /* get convert-xxx property */
382 memset(&adata, 0, sizeof(adata));
383 for_each_child_of_node(node, np)
Kuninori Morimotocdc44472019-03-18 13:50:08 +0900384 simple_parse_convert(dev, np, &adata);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900385
386 /* loop for all CPU/Codec node */
387 for_each_child_of_node(node, np) {
Daniel Baluta5525cf02019-12-09 15:53:53 +0200388 if (plat == np)
389 continue;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900390 /*
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 ||
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900397 adata.convert_rate || adata.convert_channels)) {
398 /*
399 * np
400 * |1(CPU)|0(Codec) li->cpu
401 * CPU |Pass |return
402 * Codec |return|Pass
403 */
404 if (li->cpu != (np == codec))
405 ret = func_dpcm(priv, np, codec, li, is_top);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900406 /* else normal sound */
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900407 } else {
408 /*
409 * np
410 * |1(CPU)|0(Codec) li->cpu
411 * CPU |Pass |return
412 * Codec |return|return
413 */
414 if (li->cpu && (np != codec))
415 ret = func_noml(priv, np, codec, li, is_top);
416 }
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900417
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100418 if (ret < 0) {
Wen Yang27862d52019-07-10 15:25:07 +0800419 of_node_put(codec);
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100420 of_node_put(np);
421 goto error;
422 }
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900423 }
424
Wen Yang27862d52019-07-10 15:25:07 +0800425 of_node_put(codec);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900426 node = of_get_next_child(top, node);
427 } while (!is_top && node);
428
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100429 error:
430 of_node_put(node);
431 return ret;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900432}
433
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900434static int simple_for_each_link(struct asoc_simple_priv *priv,
435 struct link_info *li,
436 int (*func_noml)(struct asoc_simple_priv *priv,
437 struct device_node *np,
438 struct device_node *codec,
439 struct link_info *li, bool is_top),
440 int (*func_dpcm)(struct asoc_simple_priv *priv,
441 struct device_node *np,
442 struct device_node *codec,
443 struct link_info *li, bool is_top))
444{
445 int ret;
446 /*
447 * Detect all CPU first, and Detect all Codec 2nd.
448 *
449 * In Normal sound case, all DAIs are detected
450 * as "CPU-Codec".
451 *
452 * In DPCM sound case,
453 * all CPUs are detected as "CPU-dummy", and
454 * all Codecs are detected as "dummy-Codec".
455 * To avoid random sub-device numbering,
456 * detect "dummy-Codec" in last;
457 */
458 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
459 ret = __simple_for_each_link(priv, li, func_noml, func_dpcm);
460 if (ret < 0)
461 break;
462 }
463
464 return ret;
465}
466
Thierry Redingec1af6c2021-04-19 18:41:17 +0200467static int simple_parse_of(struct asoc_simple_priv *priv, struct link_info *li)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900468{
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000469 struct snd_soc_card *card = simple_priv_to_card(priv);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900470 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900471
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900472 ret = asoc_simple_parse_widgets(card, PREFIX);
Kuninori Morimotofa2760dd2017-06-16 01:39:11 +0000473 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900474 return ret;
Xiubo Li9d681f52014-02-08 15:59:53 +0800475
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900476 ret = asoc_simple_parse_routing(card, PREFIX);
Kuninori Morimoto1fdb5d22017-06-15 00:25:17 +0000477 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900478 return ret;
Xiubo Lid4c22092013-12-23 12:57:01 +0800479
Paul Cercueil552f2d72019-04-26 04:25:50 +0200480 ret = asoc_simple_parse_pin_switches(card, PREFIX);
481 if (ret < 0)
482 return ret;
483
Xiubo Li20804372014-09-03 10:23:39 +0800484 /* Single/Muti DAI link(s) & New style of DT node */
Thierry Redingec1af6c2021-04-19 18:41:17 +0200485 memset(li, 0, sizeof(*li));
486 ret = simple_for_each_link(priv, li,
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900487 simple_dai_link_of,
488 simple_dai_link_of_dpcm);
489 if (ret < 0)
490 return ret;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900491
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900492 ret = asoc_simple_parse_card_name(card, PREFIX);
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300493 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900494 return ret;
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300495
Stephan Gerholdd9ffff62020-08-01 12:02:56 +0200496 ret = snd_soc_of_parse_aux_devs(card, PREFIX "aux-devs");
Kuninori Morimotoa03b0542016-08-26 03:05:16 +0000497
498 return ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900499}
500
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900501static int simple_count_noml(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900502 struct device_node *np,
503 struct device_node *codec,
504 struct link_info *li, bool is_top)
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900505{
Thierry Reding343e55e2021-04-16 09:11:47 +0200506 if (li->link >= SNDRV_MAX_LINKS) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900507 struct device *dev = simple_priv_to_dev(priv);
508
509 dev_err(dev, "too many links\n");
510 return -EINVAL;
511 }
512
513 li->num[li->link].cpus = 1;
514 li->num[li->link].codecs = 1;
515 li->num[li->link].platforms = 1;
516
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900517 li->link += 1;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900518
519 return 0;
520}
521
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900522static int simple_count_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900523 struct device_node *np,
524 struct device_node *codec,
525 struct link_info *li, bool is_top)
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900526{
Thierry Reding343e55e2021-04-16 09:11:47 +0200527 if (li->link >= SNDRV_MAX_LINKS) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900528 struct device *dev = simple_priv_to_dev(priv);
529
530 dev_err(dev, "too many links\n");
531 return -EINVAL;
532 }
533
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900534 if (li->cpu) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900535 li->num[li->link].cpus = 1;
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900536 li->num[li->link].platforms = 1;
537
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900538 li->link++; /* CPU-dummy */
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900539 } else {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900540 li->num[li->link].codecs = 1;
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900541
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900542 li->link++; /* dummy-Codec */
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900543 }
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900544
545 return 0;
546}
547
Thierry Reding0f687d82021-04-16 09:11:46 +0200548static int simple_get_dais_count(struct asoc_simple_priv *priv,
549 struct link_info *li)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900550{
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900551 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900552 struct device_node *top = dev->of_node;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900553
554 /*
555 * link_num : number of links.
556 * CPU-Codec / CPU-dummy / dummy-Codec
557 * dais_num : number of DAIs
558 * ccnf_num : number of codec_conf
559 * same number for "dummy-Codec"
560 *
561 * ex1)
562 * CPU0 --- Codec0 link : 5
563 * CPU1 --- Codec1 dais : 7
564 * CPU2 -/ ccnf : 1
565 * CPU3 --- Codec2
566 *
567 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
568 * => 7 DAIs = 4xCPU + 3xCodec
569 * => 1 ccnf = 1xdummy-Codec
570 *
571 * ex2)
572 * CPU0 --- Codec0 link : 5
573 * CPU1 --- Codec1 dais : 6
574 * CPU2 -/ ccnf : 1
575 * CPU3 -/
576 *
577 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
578 * => 6 DAIs = 4xCPU + 2xCodec
579 * => 1 ccnf = 1xdummy-Codec
580 *
581 * ex3)
582 * CPU0 --- Codec0 link : 6
583 * CPU1 -/ dais : 6
584 * CPU2 --- Codec1 ccnf : 2
585 * CPU3 -/
586 *
587 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
588 * => 6 DAIs = 4xCPU + 2xCodec
589 * => 2 ccnf = 2xdummy-Codec
Kuninori Morimoto7adee602018-12-20 10:46:47 +0900590 *
591 * ex4)
592 * CPU0 --- Codec0 (convert-rate) link : 3
593 * CPU1 --- Codec1 dais : 4
594 * ccnf : 1
595 *
596 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
597 * => 4 DAIs = 2xCPU + 2xCodec
598 * => 1 ccnf = 1xdummy-Codec
Kuninori Morimotoda215352018-12-14 11:35:10 +0900599 */
600 if (!top) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900601 li->num[0].cpus = 1;
602 li->num[0].codecs = 1;
603 li->num[0].platforms = 1;
604
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900605 li->link = 1;
Thierry Reding0f687d82021-04-16 09:11:46 +0200606 return 0;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900607 }
608
Thierry Reding0f687d82021-04-16 09:11:46 +0200609 return simple_for_each_link(priv, li,
610 simple_count_noml,
611 simple_count_dpcm);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900612}
613
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900614static int simple_soc_probe(struct snd_soc_card *card)
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900615{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900616 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900617 int ret;
618
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900619 ret = asoc_simple_init_hp(card, &priv->hp_jack, PREFIX);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900620 if (ret < 0)
621 return ret;
622
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900623 ret = asoc_simple_init_mic(card, &priv->mic_jack, PREFIX);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900624 if (ret < 0)
625 return ret;
626
627 return 0;
628}
629
Amir Goldsteinb0a821d2019-05-16 13:26:28 +0300630static int asoc_simple_probe(struct platform_device *pdev)
Kuninori Morimotof2390882012-04-08 21:17:50 -0700631{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900632 struct asoc_simple_priv *priv;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800633 struct device *dev = &pdev->dev;
Kuninori Morimotodcee9bfe2017-03-15 04:43:21 +0000634 struct device_node *np = dev->of_node;
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000635 struct snd_soc_card *card;
Thierry Redingec1af6c2021-04-19 18:41:17 +0200636 struct link_info *li;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900637 int ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700638
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800639 /* Allocate the private data and the DAI link array */
Kuninori Morimotob0133d92016-08-26 03:10:25 +0000640 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100641 if (!priv)
Xiubo Lica919fe2014-01-14 12:35:32 +0800642 return -ENOMEM;
643
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900644 card = simple_priv_to_card(priv);
645 card->owner = THIS_MODULE;
646 card->dev = dev;
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900647 card->probe = simple_soc_probe;
Guido Günther907f0a32021-06-22 10:27:09 +0200648 card->driver_name = "simple-card";
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900649
Thierry Redingec1af6c2021-04-19 18:41:17 +0200650 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
651 if (!li)
652 return -ENOMEM;
653
654 ret = simple_get_dais_count(priv, li);
Thierry Reding0f687d82021-04-16 09:11:46 +0200655 if (ret < 0)
656 return ret;
657
Thierry Redingec1af6c2021-04-19 18:41:17 +0200658 if (!li->link)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900659 return -EINVAL;
660
Thierry Redingec1af6c2021-04-19 18:41:17 +0200661 ret = asoc_simple_init_priv(priv, li);
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900662 if (ret < 0)
663 return ret;
Xiubo Lica919fe2014-01-14 12:35:32 +0800664
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100665 if (np && of_device_is_available(np)) {
666
Thierry Redingec1af6c2021-04-19 18:41:17 +0200667 ret = simple_parse_of(priv, li);
Xiubo Lica919fe2014-01-14 12:35:32 +0800668 if (ret < 0) {
Kuninori Morimoto7a0299e2021-12-14 11:08:35 +0900669 dev_err_probe(dev, ret, "parse error\n");
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100670 goto err;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900671 }
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100672
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900673 } else {
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100674 struct asoc_simple_card_info *cinfo;
Kuninori Morimotof1072942019-06-06 13:07:35 +0900675 struct snd_soc_dai_link_component *cpus;
Kuninori Morimoto710af912018-08-31 03:08:24 +0000676 struct snd_soc_dai_link_component *codecs;
Kuninori Morimotoe58f41e2018-08-31 03:10:33 +0000677 struct snd_soc_dai_link_component *platform;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900678 struct snd_soc_dai_link *dai_link = priv->dai_link;
679 struct simple_dai_props *dai_props = priv->dai_props;
680
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100681 cinfo = dev->platform_data;
682 if (!cinfo) {
Xiubo Li34787d0a2014-01-09 17:49:40 +0800683 dev_err(dev, "no info for asoc-simple-card\n");
684 return -EINVAL;
685 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900686
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800687 if (!cinfo->name ||
688 !cinfo->codec_dai.name ||
689 !cinfo->codec ||
690 !cinfo->platform ||
Jean-Francois Moine7722f832014-01-15 16:51:33 +0100691 !cinfo->cpu_dai.name) {
692 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
693 return -EINVAL;
694 }
Jean-Francois Moine2bee9912014-01-15 16:51:37 +0100695
Kuninori Morimotof1072942019-06-06 13:07:35 +0900696 cpus = dai_link->cpus;
697 cpus->dai_name = cinfo->cpu_dai.name;
698
Kuninori Morimoto710af912018-08-31 03:08:24 +0000699 codecs = dai_link->codecs;
700 codecs->name = cinfo->codec;
701 codecs->dai_name = cinfo->codec_dai.name;
702
Kuninori Morimoto910fdca2019-01-21 09:32:32 +0900703 platform = dai_link->platforms;
Kuninori Morimotoe58f41e2018-08-31 03:10:33 +0000704 platform->name = cinfo->platform;
705
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000706 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100707 dai_link->name = cinfo->name;
708 dai_link->stream_name = cinfo->name;
Lars-Peter Clausen1efb53a2015-03-24 01:07:08 +0000709 dai_link->dai_fmt = cinfo->daifmt;
Kuninori Morimotoad934ca2019-03-20 13:55:52 +0900710 dai_link->init = asoc_simple_dai_init;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900711 memcpy(dai_props->cpu_dai, &cinfo->cpu_dai,
712 sizeof(*dai_props->cpu_dai));
713 memcpy(dai_props->codec_dai, &cinfo->codec_dai,
714 sizeof(*dai_props->codec_dai));
Kuninori Morimotof2390882012-04-08 21:17:50 -0700715 }
716
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000717 snd_soc_card_set_drvdata(card, priv);
Xiubo Liba194a42014-01-13 17:08:08 +0800718
Kuninori Morimoto0580dde2019-03-20 13:54:42 +0900719 asoc_simple_debug_info(priv);
720
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000721 ret = devm_snd_soc_register_card(dev, card);
Kuninori Morimotoc6d112e2017-05-19 00:57:21 +0000722 if (ret < 0)
723 goto err;
724
Thierry Redingec1af6c2021-04-19 18:41:17 +0200725 devm_kfree(dev, li);
Kuninori Morimotoc6d112e2017-05-19 00:57:21 +0000726 return 0;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100727err:
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900728 asoc_simple_clean_reference(card);
Kuninori Morimoto387f5822016-08-26 03:07:59 +0000729
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100730 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700731}
732
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900733static const struct of_device_id simple_of_match[] = {
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900734 { .compatible = "simple-audio-card", },
Kuninori Morimoto42bf0292019-04-04 09:52:52 +0900735 { .compatible = "simple-scu-audio-card",
736 .data = (void *)DPCM_SELECTABLE },
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900737 {},
738};
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900739MODULE_DEVICE_TABLE(of, simple_of_match);
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900740
Kuninori Morimotof2390882012-04-08 21:17:50 -0700741static struct platform_driver asoc_simple_card = {
742 .driver = {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800743 .name = "asoc-simple-card",
Peter Ujfalusi7c376712016-05-09 13:38:10 +0300744 .pm = &snd_soc_pm_ops,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900745 .of_match_table = simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700746 },
Amir Goldsteinb0a821d2019-05-16 13:26:28 +0300747 .probe = asoc_simple_probe,
748 .remove = asoc_simple_remove,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700749};
750
751module_platform_driver(asoc_simple_card);
752
Fabio Estevamc445be32013-08-23 14:35:17 -0300753MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotoa1dbfd02016-08-26 03:07:28 +0000754MODULE_LICENSE("GPL v2");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700755MODULE_DESCRIPTION("ASoC Simple Sound Card");
756MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");