blob: a89d1cfdda327e3c5d115625008a488e0f1b26ff [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,
Kuninori Morimotoad11e592019-03-20 13:56:50 +090033 int *is_single_link)
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090034{
35 struct of_phandle_args args;
36 int ret;
37
38 if (!node)
39 return 0;
40
41 /*
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090042 * Get node via "sound-dai = <&phandle port>"
43 * it will be used as xxx_of_node on soc_bind_dai_link()
44 */
45 ret = of_parse_phandle_with_args(node, DAI, CELL, 0, &args);
46 if (ret)
47 return ret;
48
Kuninori Morimoto39c66b62019-09-03 17:15:54 +090049 /*
50 * FIXME
51 *
52 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
53 * If user unbinded CPU or Codec driver, but not for Sound Card,
54 * dlc->dai_name is keeping unbinded CPU or Codec
55 * driver's pointer.
56 *
57 * If user re-bind CPU or Codec driver again, ALSA SoC will try
58 * to rebind Card via snd_soc_try_rebind_card(), but because of
59 * above reason, it might can't bind Sound Card.
60 * Because Sound Card is pointing to released dai_name pointer.
61 *
62 * To avoid this rebind Card issue,
63 * 1) It needs to alloc memory to keep dai_name eventhough
64 * CPU or Codec driver was unbinded, or
65 * 2) user need to rebind Sound Card everytime
66 * if he unbinded CPU or Codec.
67 */
Kuninori Morimotof1072942019-06-06 13:07:35 +090068 ret = snd_soc_of_get_dai_name(node, &dlc->dai_name);
69 if (ret < 0)
70 return ret;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090071
Kuninori Morimotof1072942019-06-06 13:07:35 +090072 dlc->of_node = args.np;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090073
74 if (is_single_link)
75 *is_single_link = !args.args_count;
76
77 return 0;
78}
79
Kuninori Morimotocdc44472019-03-18 13:50:08 +090080static void simple_parse_convert(struct device *dev,
81 struct device_node *np,
Kuninori Morimotoad11e592019-03-20 13:56:50 +090082 struct asoc_simple_data *adata)
Kuninori Morimoto7e5e1f82018-12-20 10:46:42 +090083{
84 struct device_node *top = dev->of_node;
85 struct device_node *node = of_get_parent(np);
86
Kuninori Morimotofcfd7632021-04-12 08:53:00 +090087 asoc_simple_parse_convert(top, PREFIX, adata);
88 asoc_simple_parse_convert(node, PREFIX, adata);
89 asoc_simple_parse_convert(node, NULL, adata);
90 asoc_simple_parse_convert(np, NULL, adata);
Kuninori Morimoto7e5e1f82018-12-20 10:46:42 +090091
92 of_node_put(node);
93}
94
Kuninori Morimotod708d352019-03-18 13:50:17 +090095static void simple_parse_mclk_fs(struct device_node *top,
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +090096 struct device_node *np,
Kuninori Morimotod708d352019-03-18 13:50:17 +090097 struct simple_dai_props *props,
98 char *prefix)
99{
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900100 struct device_node *node = of_get_parent(np);
Kuninori Morimotod708d352019-03-18 13:50:17 +0900101 char prop[128];
102
103 snprintf(prop, sizeof(prop), "%smclk-fs", PREFIX);
104 of_property_read_u32(top, prop, &props->mclk_fs);
105
106 snprintf(prop, sizeof(prop), "%smclk-fs", prefix);
107 of_property_read_u32(node, prop, &props->mclk_fs);
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900108 of_property_read_u32(np, prop, &props->mclk_fs);
Kuninori Morimotod708d352019-03-18 13:50:17 +0900109
110 of_node_put(node);
111}
112
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900113static int simple_parse_node(struct asoc_simple_priv *priv,
114 struct device_node *np,
115 struct link_info *li,
116 char *prefix,
117 int *cpu)
118{
119 struct device *dev = simple_priv_to_dev(priv);
120 struct device_node *top = dev->of_node;
121 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
122 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
123 struct snd_soc_dai_link_component *dlc;
124 struct asoc_simple_dai *dai;
125 int ret;
126
127 if (cpu) {
128 dlc = asoc_link_to_cpu(dai_link, 0);
129 dai = simple_props_to_dai_cpu(dai_props, 0);
130 } else {
131 dlc = asoc_link_to_codec(dai_link, 0);
132 dai = simple_props_to_dai_codec(dai_props, 0);
133 }
134
135 simple_parse_mclk_fs(top, np, dai_props, prefix);
136
137 ret = asoc_simple_parse_dai(np, dlc, cpu);
138 if (ret)
139 return ret;
140
141 ret = asoc_simple_parse_clk(dev, np, dai, dlc);
142 if (ret)
143 return ret;
144
145 ret = asoc_simple_parse_tdm(np, dai);
146 if (ret)
147 return ret;
148
149 return 0;
150}
151
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900152static int simple_link_init(struct asoc_simple_priv *priv,
153 struct device_node *node,
154 struct device_node *codec,
155 struct link_info *li,
156 char *prefix, char *name)
157{
158 struct device *dev = simple_priv_to_dev(priv);
159 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
160 int ret;
161
162 ret = asoc_simple_parse_daifmt(dev, node, codec,
163 prefix, &dai_link->dai_fmt);
164 if (ret < 0)
165 return 0;
166
167 dai_link->init = asoc_simple_dai_init;
168 dai_link->ops = &simple_ops;
169
170 return asoc_simple_set_dailink_name(dev, dai_link, name);
171}
172
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900173static int simple_dai_link_of_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900174 struct device_node *np,
175 struct device_node *codec,
176 struct link_info *li,
177 bool is_top)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900178{
179 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900180 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
181 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900182 struct device_node *top = dev->of_node;
183 struct device_node *node = of_get_parent(np);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900184 char *prefix = "";
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900185 char dai_name[64];
Kuninori Morimotoda215352018-12-14 11:35:10 +0900186 int ret;
187
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900188 dev_dbg(dev, "link_of DPCM (%pOF)\n", np);
189
Kuninori Morimotoda215352018-12-14 11:35:10 +0900190 /* For single DAI link & old style of DT node */
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900191 if (is_top)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900192 prefix = PREFIX;
193
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900194 if (li->cpu) {
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900195 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
196 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100197 int is_single_links = 0;
198
Kuninori Morimoto848704f2019-10-15 11:52:41 +0900199 /* Codec is dummy */
Kuninori Morimotoda215352018-12-14 11:35:10 +0900200
201 /* FE settings */
202 dai_link->dynamic = 1;
203 dai_link->dpcm_merged_format = 1;
204
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900205 ret = simple_parse_node(priv, np, li, prefix, &is_single_links);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900206 if (ret < 0)
Wen Yang724808a2019-07-10 15:25:06 +0800207 goto out_put_node;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900208
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900209 snprintf(dai_name, sizeof(dai_name), "fe.%s", cpus->dai_name);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100210
211 asoc_simple_canonicalize_cpu(cpus, is_single_links);
212 asoc_simple_canonicalize_platform(platforms, cpus);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900213 } else {
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900214 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900215 struct snd_soc_codec_conf *cconf;
216
Kuninori Morimoto848704f2019-10-15 11:52:41 +0900217 /* CPU is dummy */
Kuninori Morimotoda215352018-12-14 11:35:10 +0900218
219 /* BE settings */
220 dai_link->no_pcm = 1;
Kuninori Morimoto629f7542019-03-20 13:56:06 +0900221 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900222
Kuninori Morimoto2c33e202021-04-14 08:20:10 +0900223 cconf = simple_props_to_codec_conf(dai_props, 0);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900224
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900225 ret = simple_parse_node(priv, np, li, prefix, NULL);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100226 if (ret < 0)
227 goto out_put_node;
228
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900229 snprintf(dai_name, sizeof(dai_name), "be.%s", codecs->dai_name);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900230
231 /* check "prefix" from top node */
Kuninori Morimotocfc652a2018-12-14 11:35:24 +0900232 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
Kuninori Morimotoda215352018-12-14 11:35:10 +0900233 PREFIX "prefix");
Kuninori Morimotocfc652a2018-12-14 11:35:24 +0900234 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
235 "prefix");
236 snd_soc_of_parse_node_prefix(np, cconf, codecs->of_node,
237 "prefix");
Kuninori Morimotoda215352018-12-14 11:35:10 +0900238 }
239
Kuninori Morimotocdc44472019-03-18 13:50:08 +0900240 simple_parse_convert(dev, np, &dai_props->adata);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100241
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500242 snd_soc_dai_link_set_capabilities(dai_link);
243
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900244 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900245
Wen Yang724808a2019-07-10 15:25:06 +0800246out_put_node:
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900247 li->link++;
248
Wen Yang724808a2019-07-10 15:25:06 +0800249 of_node_put(node);
250 return ret;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900251}
252
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900253static int simple_dai_link_of(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900254 struct device_node *np,
255 struct device_node *codec,
256 struct link_info *li,
257 bool is_top)
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100258{
Kuninori Morimotof531913f2014-09-09 21:37:57 -0700259 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900260 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
Kuninori Morimoto5bb5ac72021-04-14 08:20:04 +0900261 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
262 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
263 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700264 struct device_node *cpu = NULL;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900265 struct device_node *node = NULL;
Jun Niee0ae2252015-04-29 18:11:07 +0800266 struct device_node *plat = NULL;
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900267 char dai_name[64];
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200268 char prop[128];
269 char *prefix = "";
Mark Brown25c4a9b2021-04-23 18:07:54 +0100270 int ret, single_cpu = 0;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100271
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900272 cpu = np;
273 node = of_get_parent(np);
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900274
275 dev_dbg(dev, "link_of (%pOF)\n", node);
276
Xiubo Li20804372014-09-03 10:23:39 +0800277 /* For single DAI link & old style of DT node */
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900278 if (is_top)
Kuninori Morimoto548563f2016-05-31 08:59:01 +0000279 prefix = PREFIX;
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100280
Jun Niee0ae2252015-04-29 18:11:07 +0800281 snprintf(prop, sizeof(prop), "%splat", prefix);
282 plat = of_get_child_by_name(node, prop);
283
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900284 ret = simple_parse_node(priv, cpu, li, prefix, &single_cpu);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100285 if (ret < 0)
286 goto dai_link_of_err;
287
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900288 ret = simple_parse_node(priv, codec, li, prefix, NULL);
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000289 if (ret < 0)
290 goto dai_link_of_err;
291
Kuninori Morimoto5bb5ac72021-04-14 08:20:04 +0900292 ret = asoc_simple_parse_dai(plat, platforms, NULL);
Kuninori Morimoto44c16af2016-08-08 06:02:07 +0000293 if (ret < 0)
294 goto dai_link_of_err;
295
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900296 snprintf(dai_name, sizeof(dai_name),
297 "%s-%s", cpus->dai_name, codecs->dai_name);
Mark Brown25c4a9b2021-04-23 18:07:54 +0100298
299 asoc_simple_canonicalize_cpu(cpus, single_cpu);
300 asoc_simple_canonicalize_platform(platforms, cpus);
301
Kuninori Morimoto6ad76b52021-05-11 10:17:47 +0900302 ret = simple_link_init(priv, node, codec, li, prefix, dai_name);
303
Jyri Sarhab3ca11f2014-03-24 12:15:25 +0200304dai_link_of_err:
Takashi Iwai44662f92019-02-19 16:46:48 +0100305 of_node_put(plat);
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900306 of_node_put(node);
Kuninori Morimoto1b5721b2014-10-27 18:04:52 -0700307
Kuninori Morimoto0919a3a2021-05-11 10:17:07 +0900308 li->link++;
309
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100310 return ret;
311}
312
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900313static int __simple_for_each_link(struct asoc_simple_priv *priv,
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900314 struct link_info *li,
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900315 int (*func_noml)(struct asoc_simple_priv *priv,
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900316 struct device_node *np,
317 struct device_node *codec,
318 struct link_info *li, bool is_top),
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900319 int (*func_dpcm)(struct asoc_simple_priv *priv,
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900320 struct device_node *np,
321 struct device_node *codec,
322 struct link_info *li, bool is_top))
323{
324 struct device *dev = simple_priv_to_dev(priv);
325 struct device_node *top = dev->of_node;
326 struct device_node *node;
Kuninori Morimoto42bf0292019-04-04 09:52:52 +0900327 uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900328 bool is_top = 0;
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100329 int ret = 0;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900330
331 /* Check if it has dai-link */
332 node = of_get_child_by_name(top, PREFIX "dai-link");
333 if (!node) {
Daniel Baluta461d8542019-02-16 10:09:42 +0000334 node = of_node_get(top);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900335 is_top = 1;
336 }
337
338 /* loop for all dai-link */
339 do {
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900340 struct asoc_simple_data adata;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900341 struct device_node *codec;
Daniel Baluta5525cf02019-12-09 15:53:53 +0200342 struct device_node *plat;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900343 struct device_node *np;
344 int num = of_get_child_count(node);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900345
346 /* get codec */
347 codec = of_get_child_by_name(node, is_top ?
348 PREFIX "codec" : "codec");
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100349 if (!codec) {
350 ret = -ENODEV;
351 goto error;
352 }
Daniel Baluta5525cf02019-12-09 15:53:53 +0200353 /* get platform */
354 plat = of_get_child_by_name(node, is_top ?
355 PREFIX "plat" : "plat");
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900356
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900357 /* get convert-xxx property */
358 memset(&adata, 0, sizeof(adata));
359 for_each_child_of_node(node, np)
Kuninori Morimotocdc44472019-03-18 13:50:08 +0900360 simple_parse_convert(dev, np, &adata);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900361
362 /* loop for all CPU/Codec node */
363 for_each_child_of_node(node, np) {
Daniel Baluta5525cf02019-12-09 15:53:53 +0200364 if (plat == np)
365 continue;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900366 /*
367 * It is DPCM
368 * if it has many CPUs,
369 * or has convert-xxx property
370 */
Kuninori Morimoto42bf0292019-04-04 09:52:52 +0900371 if (dpcm_selectable &&
372 (num > 2 ||
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900373 adata.convert_rate || adata.convert_channels)) {
374 /*
375 * np
376 * |1(CPU)|0(Codec) li->cpu
377 * CPU |Pass |return
378 * Codec |return|Pass
379 */
380 if (li->cpu != (np == codec))
381 ret = func_dpcm(priv, np, codec, li, is_top);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900382 /* else normal sound */
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900383 } else {
384 /*
385 * np
386 * |1(CPU)|0(Codec) li->cpu
387 * CPU |Pass |return
388 * Codec |return|return
389 */
390 if (li->cpu && (np != codec))
391 ret = func_noml(priv, np, codec, li, is_top);
392 }
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900393
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100394 if (ret < 0) {
Wen Yang27862d52019-07-10 15:25:07 +0800395 of_node_put(codec);
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100396 of_node_put(np);
397 goto error;
398 }
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900399 }
400
Wen Yang27862d52019-07-10 15:25:07 +0800401 of_node_put(codec);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900402 node = of_get_next_child(top, node);
403 } while (!is_top && node);
404
Takashi Iwai0b9c9ed2019-02-19 16:46:49 +0100405 error:
406 of_node_put(node);
407 return ret;
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900408}
409
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900410static int simple_for_each_link(struct asoc_simple_priv *priv,
411 struct link_info *li,
412 int (*func_noml)(struct asoc_simple_priv *priv,
413 struct device_node *np,
414 struct device_node *codec,
415 struct link_info *li, bool is_top),
416 int (*func_dpcm)(struct asoc_simple_priv *priv,
417 struct device_node *np,
418 struct device_node *codec,
419 struct link_info *li, bool is_top))
420{
421 int ret;
422 /*
423 * Detect all CPU first, and Detect all Codec 2nd.
424 *
425 * In Normal sound case, all DAIs are detected
426 * as "CPU-Codec".
427 *
428 * In DPCM sound case,
429 * all CPUs are detected as "CPU-dummy", and
430 * all Codecs are detected as "dummy-Codec".
431 * To avoid random sub-device numbering,
432 * detect "dummy-Codec" in last;
433 */
434 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
435 ret = __simple_for_each_link(priv, li, func_noml, func_dpcm);
436 if (ret < 0)
437 break;
438 }
439
440 return ret;
441}
442
Thierry Redingec1af6c2021-04-19 18:41:17 +0200443static int simple_parse_of(struct asoc_simple_priv *priv, struct link_info *li)
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900444{
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000445 struct snd_soc_card *card = simple_priv_to_card(priv);
Kuninori Morimotoc39291a2018-12-20 10:47:28 +0900446 int ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900447
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900448 ret = asoc_simple_parse_widgets(card, PREFIX);
Kuninori Morimotofa2760dd2017-06-16 01:39:11 +0000449 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900450 return ret;
Xiubo Li9d681f52014-02-08 15:59:53 +0800451
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900452 ret = asoc_simple_parse_routing(card, PREFIX);
Kuninori Morimoto1fdb5d22017-06-15 00:25:17 +0000453 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900454 return ret;
Xiubo Lid4c22092013-12-23 12:57:01 +0800455
Paul Cercueil552f2d72019-04-26 04:25:50 +0200456 ret = asoc_simple_parse_pin_switches(card, PREFIX);
457 if (ret < 0)
458 return ret;
459
Xiubo Li20804372014-09-03 10:23:39 +0800460 /* Single/Muti DAI link(s) & New style of DT node */
Thierry Redingec1af6c2021-04-19 18:41:17 +0200461 memset(li, 0, sizeof(*li));
462 ret = simple_for_each_link(priv, li,
Kuninori Morimoto39af7f72021-03-26 12:26:40 +0900463 simple_dai_link_of,
464 simple_dai_link_of_dpcm);
465 if (ret < 0)
466 return ret;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900467
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900468 ret = asoc_simple_parse_card_name(card, PREFIX);
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300469 if (ret < 0)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900470 return ret;
Nikita Yushchenko899a2472016-09-26 12:56:51 +0300471
Stephan Gerholdd9ffff62020-08-01 12:02:56 +0200472 ret = snd_soc_of_parse_aux_devs(card, PREFIX "aux-devs");
Kuninori Morimotoa03b0542016-08-26 03:05:16 +0000473
474 return ret;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900475}
476
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900477static int simple_count_noml(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900478 struct device_node *np,
479 struct device_node *codec,
480 struct link_info *li, bool is_top)
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900481{
Thierry Reding343e55e2021-04-16 09:11:47 +0200482 if (li->link >= SNDRV_MAX_LINKS) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900483 struct device *dev = simple_priv_to_dev(priv);
484
485 dev_err(dev, "too many links\n");
486 return -EINVAL;
487 }
488
489 li->num[li->link].cpus = 1;
490 li->num[li->link].codecs = 1;
491 li->num[li->link].platforms = 1;
492
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900493 li->link += 1;
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900494
495 return 0;
496}
497
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900498static int simple_count_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900499 struct device_node *np,
500 struct device_node *codec,
501 struct link_info *li, bool is_top)
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900502{
Thierry Reding343e55e2021-04-16 09:11:47 +0200503 if (li->link >= SNDRV_MAX_LINKS) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900504 struct device *dev = simple_priv_to_dev(priv);
505
506 dev_err(dev, "too many links\n");
507 return -EINVAL;
508 }
509
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900510 if (li->cpu) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900511 li->num[li->link].cpus = 1;
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900512 li->num[li->link].platforms = 1;
513
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900514 li->link++; /* CPU-dummy */
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900515 } else {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900516 li->num[li->link].codecs = 1;
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900517
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900518 li->link++; /* dummy-Codec */
Kuninori Morimotoa6e87982021-03-26 12:26:33 +0900519 }
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900520
521 return 0;
522}
523
Thierry Reding0f687d82021-04-16 09:11:46 +0200524static int simple_get_dais_count(struct asoc_simple_priv *priv,
525 struct link_info *li)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900526{
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900527 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900528 struct device_node *top = dev->of_node;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900529
530 /*
531 * link_num : number of links.
532 * CPU-Codec / CPU-dummy / dummy-Codec
533 * dais_num : number of DAIs
534 * ccnf_num : number of codec_conf
535 * same number for "dummy-Codec"
536 *
537 * ex1)
538 * CPU0 --- Codec0 link : 5
539 * CPU1 --- Codec1 dais : 7
540 * CPU2 -/ ccnf : 1
541 * CPU3 --- Codec2
542 *
543 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
544 * => 7 DAIs = 4xCPU + 3xCodec
545 * => 1 ccnf = 1xdummy-Codec
546 *
547 * ex2)
548 * CPU0 --- Codec0 link : 5
549 * CPU1 --- Codec1 dais : 6
550 * CPU2 -/ ccnf : 1
551 * CPU3 -/
552 *
553 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
554 * => 6 DAIs = 4xCPU + 2xCodec
555 * => 1 ccnf = 1xdummy-Codec
556 *
557 * ex3)
558 * CPU0 --- Codec0 link : 6
559 * CPU1 -/ dais : 6
560 * CPU2 --- Codec1 ccnf : 2
561 * CPU3 -/
562 *
563 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
564 * => 6 DAIs = 4xCPU + 2xCodec
565 * => 2 ccnf = 2xdummy-Codec
Kuninori Morimoto7adee602018-12-20 10:46:47 +0900566 *
567 * ex4)
568 * CPU0 --- Codec0 (convert-rate) link : 3
569 * CPU1 --- Codec1 dais : 4
570 * ccnf : 1
571 *
572 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
573 * => 4 DAIs = 2xCPU + 2xCodec
574 * => 1 ccnf = 1xdummy-Codec
Kuninori Morimotoda215352018-12-14 11:35:10 +0900575 */
576 if (!top) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900577 li->num[0].cpus = 1;
578 li->num[0].codecs = 1;
579 li->num[0].platforms = 1;
580
Kuninori Morimoto17029e42018-12-20 10:46:53 +0900581 li->link = 1;
Thierry Reding0f687d82021-04-16 09:11:46 +0200582 return 0;
Kuninori Morimotoda215352018-12-14 11:35:10 +0900583 }
584
Thierry Reding0f687d82021-04-16 09:11:46 +0200585 return simple_for_each_link(priv, li,
586 simple_count_noml,
587 simple_count_dpcm);
Kuninori Morimotoda215352018-12-14 11:35:10 +0900588}
589
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900590static int simple_soc_probe(struct snd_soc_card *card)
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900591{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900592 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900593 int ret;
594
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900595 ret = asoc_simple_init_hp(card, &priv->hp_jack, PREFIX);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900596 if (ret < 0)
597 return ret;
598
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900599 ret = asoc_simple_init_mic(card, &priv->mic_jack, PREFIX);
Katsuhiro Suzuki8d1bd112018-06-11 17:32:13 +0900600 if (ret < 0)
601 return ret;
602
603 return 0;
604}
605
Amir Goldsteinb0a821d2019-05-16 13:26:28 +0300606static int asoc_simple_probe(struct platform_device *pdev)
Kuninori Morimotof2390882012-04-08 21:17:50 -0700607{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900608 struct asoc_simple_priv *priv;
Kuninori Morimotof89983e2012-12-25 22:52:33 -0800609 struct device *dev = &pdev->dev;
Kuninori Morimotodcee9bfe2017-03-15 04:43:21 +0000610 struct device_node *np = dev->of_node;
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000611 struct snd_soc_card *card;
Thierry Redingec1af6c2021-04-19 18:41:17 +0200612 struct link_info *li;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900613 int ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700614
Xiubo Li0dd4fc32014-09-10 09:59:55 +0800615 /* Allocate the private data and the DAI link array */
Kuninori Morimotob0133d92016-08-26 03:10:25 +0000616 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100617 if (!priv)
Xiubo Lica919fe2014-01-14 12:35:32 +0800618 return -ENOMEM;
619
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900620 card = simple_priv_to_card(priv);
621 card->owner = THIS_MODULE;
622 card->dev = dev;
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900623 card->probe = simple_soc_probe;
Guido Günther907f0a32021-06-22 10:27:09 +0200624 card->driver_name = "simple-card";
Kuninori Morimotod947cdf2018-12-20 10:47:23 +0900625
Thierry Redingec1af6c2021-04-19 18:41:17 +0200626 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
627 if (!li)
628 return -ENOMEM;
629
630 ret = simple_get_dais_count(priv, li);
Thierry Reding0f687d82021-04-16 09:11:46 +0200631 if (ret < 0)
632 return ret;
633
Thierry Redingec1af6c2021-04-19 18:41:17 +0200634 if (!li->link)
Kuninori Morimotoda215352018-12-14 11:35:10 +0900635 return -EINVAL;
636
Thierry Redingec1af6c2021-04-19 18:41:17 +0200637 ret = asoc_simple_init_priv(priv, li);
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900638 if (ret < 0)
639 return ret;
Xiubo Lica919fe2014-01-14 12:35:32 +0800640
Jean-Francois Moine201a0ea2014-01-15 16:51:41 +0100641 if (np && of_device_is_available(np)) {
642
Thierry Redingec1af6c2021-04-19 18:41:17 +0200643 ret = simple_parse_of(priv, li);
Xiubo Lica919fe2014-01-14 12:35:32 +0800644 if (ret < 0) {
Kuninori Morimoto7a0299e2021-12-14 11:08:35 +0900645 dev_err_probe(dev, ret, "parse error\n");
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100646 goto err;
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900647 }
Jean-Francois Moine6a91a17bd2014-03-20 11:49:55 +0100648
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900649 } else {
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100650 struct asoc_simple_card_info *cinfo;
Kuninori Morimotof1072942019-06-06 13:07:35 +0900651 struct snd_soc_dai_link_component *cpus;
Kuninori Morimoto710af912018-08-31 03:08:24 +0000652 struct snd_soc_dai_link_component *codecs;
Kuninori Morimotoe58f41e2018-08-31 03:10:33 +0000653 struct snd_soc_dai_link_component *platform;
Kuninori Morimoto65a50562019-03-20 13:56:26 +0900654 struct snd_soc_dai_link *dai_link = priv->dai_link;
655 struct simple_dai_props *dai_props = priv->dai_props;
656
Jean-Francois Moineca65b492014-01-15 16:51:52 +0100657 cinfo = dev->platform_data;
658 if (!cinfo) {
Xiubo Li34787d0a2014-01-09 17:49:40 +0800659 dev_err(dev, "no info for asoc-simple-card\n");
660 return -EINVAL;
661 }
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900662
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800663 if (!cinfo->name ||
664 !cinfo->codec_dai.name ||
665 !cinfo->codec ||
666 !cinfo->platform ||
Jean-Francois Moine7722f832014-01-15 16:51:33 +0100667 !cinfo->cpu_dai.name) {
668 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
669 return -EINVAL;
670 }
Jean-Francois Moine2bee9912014-01-15 16:51:37 +0100671
Kuninori Morimotof1072942019-06-06 13:07:35 +0900672 cpus = dai_link->cpus;
673 cpus->dai_name = cinfo->cpu_dai.name;
674
Kuninori Morimoto710af912018-08-31 03:08:24 +0000675 codecs = dai_link->codecs;
676 codecs->name = cinfo->codec;
677 codecs->dai_name = cinfo->codec_dai.name;
678
Kuninori Morimoto910fdca2019-01-21 09:32:32 +0900679 platform = dai_link->platforms;
Kuninori Morimotoe58f41e2018-08-31 03:10:33 +0000680 platform->name = cinfo->platform;
681
Kuninori Morimoto5be50952017-03-15 04:44:00 +0000682 card->name = (cinfo->card) ? cinfo->card : cinfo->name;
Jean-Francois Moine5ca8ba42014-01-15 16:51:45 +0100683 dai_link->name = cinfo->name;
684 dai_link->stream_name = cinfo->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
Thierry Redingec1af6c2021-04-19 18:41:17 +0200701 devm_kfree(dev, li);
Kuninori Morimotoc6d112e2017-05-19 00:57:21 +0000702 return 0;
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100703err:
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900704 asoc_simple_clean_reference(card);
Kuninori Morimoto387f5822016-08-26 03:07:59 +0000705
Jean-Francois Moinee512e002014-03-11 10:03:40 +0100706 return ret;
Kuninori Morimotof2390882012-04-08 21:17:50 -0700707}
708
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900709static const struct of_device_id simple_of_match[] = {
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900710 { .compatible = "simple-audio-card", },
Kuninori Morimoto42bf0292019-04-04 09:52:52 +0900711 { .compatible = "simple-scu-audio-card",
712 .data = (void *)DPCM_SELECTABLE },
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900713 {},
714};
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900715MODULE_DEVICE_TABLE(of, simple_of_match);
Kuninori Morimotofa558c22013-11-20 15:25:02 +0900716
Kuninori Morimotof2390882012-04-08 21:17:50 -0700717static struct platform_driver asoc_simple_card = {
718 .driver = {
Nicolin Chen781cbeb2014-04-24 19:14:00 +0800719 .name = "asoc-simple-card",
Peter Ujfalusi7c376712016-05-09 13:38:10 +0300720 .pm = &snd_soc_pm_ops,
Kuninori Morimoto2d01a842018-12-20 10:47:34 +0900721 .of_match_table = simple_of_match,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700722 },
Amir Goldsteinb0a821d2019-05-16 13:26:28 +0300723 .probe = asoc_simple_probe,
724 .remove = asoc_simple_remove,
Kuninori Morimotof2390882012-04-08 21:17:50 -0700725};
726
727module_platform_driver(asoc_simple_card);
728
Fabio Estevamc445be32013-08-23 14:35:17 -0300729MODULE_ALIAS("platform:asoc-simple-card");
Kuninori Morimotoa1dbfd02016-08-26 03:07:28 +0000730MODULE_LICENSE("GPL v2");
Kuninori Morimotof2390882012-04-08 21:17:50 -0700731MODULE_DESCRIPTION("ASoC Simple Sound Card");
732MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");