blob: 2b598af8feef8977e05f4b1e6cc97d23f48a70c0 [file] [log] [blame]
Kuninori Morimotodecd8962018-07-02 06:31:16 +00001// SPDX-License-Identifier: GPL-2.0
2//
3// ASoC audio graph sound card support
4//
5// Copyright (C) 2016 Renesas Solutions Corp.
6// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7//
8// based on ${LINUX}/sound/soc/generic/simple-card.c
9
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000010#include <linux/clk.h>
11#include <linux/device.h>
12#include <linux/gpio.h>
Shawn Guof9869072017-06-29 21:26:38 +080013#include <linux/gpio/consumer.h>
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000014#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/of_gpio.h>
18#include <linux/of_graph.h>
19#include <linux/platform_device.h>
20#include <linux/string.h>
Sameer Pujare32b1002020-11-02 20:40:14 +053021#include <sound/graph_card.h>
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000022
Kuninori Morimotoc2c61602019-04-04 09:52:15 +090023#define DPCM_SELECTABLE 1
24
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +090025static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
26 struct snd_kcontrol *kcontrol,
27 int event)
Shawn Guof9869072017-06-29 21:26:38 +080028{
29 struct snd_soc_dapm_context *dapm = w->dapm;
Kuninori Morimotoe59289c2019-03-20 13:54:59 +090030 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(dapm->card);
Shawn Guof9869072017-06-29 21:26:38 +080031
32 switch (event) {
33 case SND_SOC_DAPM_POST_PMU:
34 gpiod_set_value_cansleep(priv->pa_gpio, 1);
35 break;
36 case SND_SOC_DAPM_PRE_PMD:
37 gpiod_set_value_cansleep(priv->pa_gpio, 0);
38 break;
39 default:
40 return -EINVAL;
41 }
42
43 return 0;
44}
45
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +090046static const struct snd_soc_dapm_widget graph_dapm_widgets[] = {
Shawn Guof9869072017-06-29 21:26:38 +080047 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +090048 0, 0, NULL, 0, graph_outdrv_event,
Shawn Guof9869072017-06-29 21:26:38 +080049 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000050};
51
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +090052static const struct snd_soc_ops graph_ops = {
Kuninori Morimotof38df5b2019-03-20 13:55:14 +090053 .startup = asoc_simple_startup,
Kuninori Morimoto686911b2019-03-20 13:55:27 +090054 .shutdown = asoc_simple_shutdown,
Kuninori Morimotof48dcbb2019-03-20 13:55:39 +090055 .hw_params = asoc_simple_hw_params,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +000056};
57
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090058static int graph_get_dai_id(struct device_node *ep)
59{
60 struct device_node *node;
61 struct device_node *endpoint;
62 struct of_endpoint info;
63 int i, id;
Kuninori Morimotoec3042a2019-07-11 13:10:45 +090064 const u32 *reg;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090065 int ret;
66
67 /* use driver specified DAI ID if exist */
68 ret = snd_soc_get_dai_id(ep);
69 if (ret != -ENOTSUPP)
70 return ret;
71
72 /* use endpoint/port reg if exist */
73 ret = of_graph_parse_endpoint(ep, &info);
74 if (ret == 0) {
75 /*
76 * Because it will count port/endpoint if it doesn't have "reg".
77 * But, we can't judge whether it has "no reg", or "reg = <0>"
78 * only of_graph_parse_endpoint().
79 * We need to check "reg" property
80 */
81 if (of_get_property(ep, "reg", NULL))
82 return info.id;
83
84 node = of_get_parent(ep);
Wen Yangc152f842019-07-10 15:25:09 +080085 reg = of_get_property(node, "reg", NULL);
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090086 of_node_put(node);
Wen Yangc152f842019-07-10 15:25:09 +080087 if (reg)
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +090088 return info.port;
89 }
90 node = of_graph_get_port_parent(ep);
91
92 /*
93 * Non HDMI sound case, counting port/endpoint on its DT
94 * is enough. Let's count it.
95 */
96 i = 0;
97 id = -1;
98 for_each_endpoint_of_node(node, endpoint) {
99 if (endpoint == ep)
100 id = i;
101 i++;
102 }
103
104 of_node_put(node);
105
106 if (id < 0)
107 return -ENODEV;
108
109 return id;
110}
111
Sameer Pujarc21cbb522020-11-02 20:40:11 +0530112static bool soc_component_is_pcm(struct snd_soc_dai_link_component *dlc)
113{
114 struct snd_soc_dai *dai = snd_soc_find_dai_with_mutex(dlc);
115
116 if (dai && (dai->component->driver->pcm_construct ||
117 dai->driver->pcm_new))
118 return true;
119
120 return false;
121}
122
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900123static int asoc_simple_parse_dai(struct device_node *ep,
124 struct snd_soc_dai_link_component *dlc,
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900125 int *is_single_link)
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900126{
127 struct device_node *node;
128 struct of_phandle_args args;
129 int ret;
130
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900131 if (!ep)
132 return 0;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900133
134 node = of_graph_get_port_parent(ep);
135
136 /* Get dai->name */
137 args.np = node;
138 args.args[0] = graph_get_dai_id(ep);
139 args.args_count = (of_graph_get_endpoint_count(node) > 1);
140
Kuninori Morimoto157ab712019-09-03 17:16:05 +0900141 /*
142 * FIXME
143 *
144 * Here, dlc->dai_name is pointer to CPU/Codec DAI name.
145 * If user unbinded CPU or Codec driver, but not for Sound Card,
146 * dlc->dai_name is keeping unbinded CPU or Codec
147 * driver's pointer.
148 *
149 * If user re-bind CPU or Codec driver again, ALSA SoC will try
150 * to rebind Card via snd_soc_try_rebind_card(), but because of
151 * above reason, it might can't bind Sound Card.
152 * Because Sound Card is pointing to released dai_name pointer.
153 *
154 * To avoid this rebind Card issue,
155 * 1) It needs to alloc memory to keep dai_name eventhough
156 * CPU or Codec driver was unbinded, or
157 * 2) user need to rebind Sound Card everytime
158 * if he unbinded CPU or Codec.
159 */
Kuninori Morimotof1072942019-06-06 13:07:35 +0900160 ret = snd_soc_get_dai_name(&args, &dlc->dai_name);
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900161 if (ret < 0)
162 return ret;
163
Kuninori Morimotof1072942019-06-06 13:07:35 +0900164 dlc->of_node = node;
Kuninori Morimoto8f7f2982019-03-20 13:56:36 +0900165
166 if (is_single_link)
167 *is_single_link = of_graph_get_endpoint_count(node) == 1;
168
169 return 0;
170}
171
Kuninori Morimotod2bf0082019-03-18 13:50:25 +0900172static void graph_parse_convert(struct device *dev,
173 struct device_node *ep,
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900174 struct asoc_simple_data *adata)
Kuninori Morimoto40dfae12018-12-20 10:45:48 +0900175{
176 struct device_node *top = dev->of_node;
177 struct device_node *port = of_get_parent(ep);
178 struct device_node *ports = of_get_parent(port);
179 struct device_node *node = of_graph_get_port_parent(ep);
180
Kuninori Morimotofcfd7632021-04-12 08:53:00 +0900181 asoc_simple_parse_convert(top, NULL, adata);
Kuninori Morimoto6769ea12021-04-19 11:02:29 +0900182 if (of_node_name_eq(ports, "ports"))
183 asoc_simple_parse_convert(ports, NULL, adata);
Kuninori Morimotofcfd7632021-04-12 08:53:00 +0900184 asoc_simple_parse_convert(port, NULL, adata);
185 asoc_simple_parse_convert(ep, NULL, adata);
Kuninori Morimotod2bf0082019-03-18 13:50:25 +0900186
187 of_node_put(port);
188 of_node_put(ports);
189 of_node_put(node);
Kuninori Morimoto40dfae12018-12-20 10:45:48 +0900190}
191
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900192static void graph_parse_mclk_fs(struct device_node *top,
193 struct device_node *ep,
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900194 struct simple_dai_props *props)
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900195{
196 struct device_node *port = of_get_parent(ep);
197 struct device_node *ports = of_get_parent(port);
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900198
199 of_property_read_u32(top, "mclk-fs", &props->mclk_fs);
Kuninori Morimoto6769ea12021-04-19 11:02:29 +0900200 if (of_node_name_eq(ports, "ports"))
201 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900202 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
203 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
204
205 of_node_put(port);
206 of_node_put(ports);
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900207}
208
Kuninori Morimoto8859f802021-04-21 14:45:21 +0900209static int graph_parse_node(struct asoc_simple_priv *priv,
210 struct device_node *ep,
211 struct link_info *li,
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900212 int *cpu)
Kuninori Morimoto8859f802021-04-21 14:45:21 +0900213{
214 struct device *dev = simple_priv_to_dev(priv);
215 struct device_node *top = dev->of_node;
216 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
217 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
218 struct snd_soc_dai_link_component *dlc;
219 struct asoc_simple_dai *dai;
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900220 int ret;
Kuninori Morimoto8859f802021-04-21 14:45:21 +0900221
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900222 if (cpu) {
Kuninori Morimoto8859f802021-04-21 14:45:21 +0900223 dlc = asoc_link_to_cpu(dai_link, 0);
224 dai = simple_props_to_dai_cpu(dai_props, 0);
225 } else {
226 dlc = asoc_link_to_codec(dai_link, 0);
227 dai = simple_props_to_dai_codec(dai_props, 0);
228 }
229
230 graph_parse_mclk_fs(top, ep, dai_props);
231
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900232 ret = asoc_simple_parse_dai(ep, dlc, cpu);
Kuninori Morimoto8859f802021-04-21 14:45:21 +0900233 if (ret < 0)
234 return ret;
235
236 ret = asoc_simple_parse_tdm(ep, dai);
237 if (ret < 0)
238 return ret;
239
240 ret = asoc_simple_parse_clk(dev, ep, dai, dlc);
241 if (ret < 0)
242 return ret;
243
Kuninori Morimoto8859f802021-04-21 14:45:21 +0900244 return 0;
245}
246
Kuninori Morimotoe51237b2021-04-21 14:45:43 +0900247static int graph_link_init(struct asoc_simple_priv *priv,
248 struct device_node *cpu_ep,
249 struct device_node *codec_ep,
250 struct link_info *li,
251 char *name)
252{
253 struct device *dev = simple_priv_to_dev(priv);
254 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
255 int ret;
256
257 ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep,
258 NULL, &dai_link->dai_fmt);
259 if (ret < 0)
260 return ret;
261
262 dai_link->init = asoc_simple_dai_init;
263 dai_link->ops = &graph_ops;
264 if (priv->ops)
265 dai_link->ops = priv->ops;
266
267 return asoc_simple_set_dailink_name(dev, dai_link, name);
268}
269
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900270static int graph_dai_link_of_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900271 struct device_node *cpu_ep,
272 struct device_node *codec_ep,
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900273 struct link_info *li)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900274{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900275 struct device *dev = simple_priv_to_dev(priv);
276 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
277 struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link);
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900278 struct device_node *top = dev->of_node;
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900279 struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
Kuninori Morimotoe51237b2021-04-21 14:45:43 +0900280 char dai_name[64];
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900281 int ret;
282
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900283 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900284
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900285 if (li->cpu) {
Kuninori Morimotof8090ff2021-05-11 10:17:57 +0900286 struct snd_soc_card *card = simple_priv_to_card(priv);
287 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
Kuninori Morimoto5f939f42021-08-30 09:44:44 +0900288 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900289 int is_single_links = 0;
Kuninori Morimotof8090ff2021-05-11 10:17:57 +0900290
Kuninori Morimoto9764bee2019-10-15 11:52:52 +0900291 /* Codec is dummy */
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900292
293 /* FE settings */
294 dai_link->dynamic = 1;
295 dai_link->dpcm_merged_format = 1;
296
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900297 ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900298 if (ret)
Kuninori Morimotof8090ff2021-05-11 10:17:57 +0900299 return ret;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900300
Kuninori Morimotoe51237b2021-04-21 14:45:43 +0900301 snprintf(dai_name, sizeof(dai_name),
302 "fe.%pOFP.%s", cpus->of_node, cpus->dai_name);
Sameer Pujarc21cbb522020-11-02 20:40:11 +0530303 /*
304 * In BE<->BE connections it is not required to create
305 * PCM devices at CPU end of the dai link and thus 'no_pcm'
306 * flag needs to be set. It is useful when there are many
307 * BE components and some of these have to be connected to
308 * form a valid audio path.
309 *
310 * For example: FE <-> BE1 <-> BE2 <-> ... <-> BEn where
311 * there are 'n' BE components in the path.
312 */
Sameer Pujar30b428d2021-09-13 22:12:11 +0530313 if (card->component_chaining && !soc_component_is_pcm(cpus)) {
Sameer Pujarc21cbb522020-11-02 20:40:11 +0530314 dai_link->no_pcm = 1;
Sameer Pujar30b428d2021-09-13 22:12:11 +0530315 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
316 }
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900317
318 asoc_simple_canonicalize_cpu(cpus, is_single_links);
Kuninori Morimoto5f939f42021-08-30 09:44:44 +0900319 asoc_simple_canonicalize_platform(platforms, cpus);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900320 } else {
Kuninori Morimotof8090ff2021-05-11 10:17:57 +0900321 struct snd_soc_codec_conf *cconf = simple_props_to_codec_conf(dai_props, 0);
322 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
323 struct device_node *port;
324 struct device_node *ports;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900325
Kuninori Morimoto9764bee2019-10-15 11:52:52 +0900326 /* CPU is dummy */
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900327
328 /* BE settings */
329 dai_link->no_pcm = 1;
Kuninori Morimoto629f7542019-03-20 13:56:06 +0900330 dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900331
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900332 ret = graph_parse_node(priv, codec_ep, li, NULL);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900333 if (ret < 0)
Kuninori Morimotof8090ff2021-05-11 10:17:57 +0900334 return ret;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900335
Kuninori Morimotoe51237b2021-04-21 14:45:43 +0900336 snprintf(dai_name, sizeof(dai_name),
337 "be.%pOFP.%s", codecs->of_node, codecs->dai_name);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900338
339 /* check "prefix" from top node */
Kuninori Morimotof8090ff2021-05-11 10:17:57 +0900340 port = of_get_parent(ep);
341 ports = of_get_parent(port);
Kuninori Morimoto66164a42018-12-14 11:32:43 +0900342 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900343 "prefix");
Kuninori Morimoto6769ea12021-04-19 11:02:29 +0900344 if (of_node_name_eq(ports, "ports"))
345 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node, "prefix");
Kuninori Morimoto66164a42018-12-14 11:32:43 +0900346 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
347 "prefix");
Kuninori Morimotof8090ff2021-05-11 10:17:57 +0900348
349 of_node_put(ports);
350 of_node_put(port);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900351 }
352
Kuninori Morimoto4346a742019-03-18 13:50:31 +0900353 graph_parse_convert(dev, ep, &dai_props->adata);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900354
Pierre-Louis Bossart25612472020-07-07 16:04:37 -0500355 snd_soc_dai_link_set_capabilities(dai_link);
356
Kuninori Morimotoe51237b2021-04-21 14:45:43 +0900357 ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900358
Kuninori Morimoto8859f802021-04-21 14:45:21 +0900359 li->link++;
360
Wen Yangaa2e3622019-07-10 15:25:08 +0800361 return ret;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900362}
363
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900364static int graph_dai_link_of(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900365 struct device_node *cpu_ep,
366 struct device_node *codec_ep,
367 struct link_info *li)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000368{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900369 struct device *dev = simple_priv_to_dev(priv);
370 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link);
Kuninori Morimoto7ed59202021-04-14 08:20:15 +0900371 struct snd_soc_dai_link_component *cpus = asoc_link_to_cpu(dai_link, 0);
372 struct snd_soc_dai_link_component *codecs = asoc_link_to_codec(dai_link, 0);
Kuninori Morimoto5f939f42021-08-30 09:44:44 +0900373 struct snd_soc_dai_link_component *platforms = asoc_link_to_platform(dai_link, 0);
Kuninori Morimotoe51237b2021-04-21 14:45:43 +0900374 char dai_name[64];
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900375 int ret, is_single_links = 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000376
Kuninori Morimoto1e4771a2018-12-20 10:45:59 +0900377 dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
378
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900379 ret = graph_parse_node(priv, cpu_ep, li, &is_single_links);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000380 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900381 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000382
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900383 ret = graph_parse_node(priv, codec_ep, li, NULL);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000384 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900385 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000386
Kuninori Morimotoe51237b2021-04-21 14:45:43 +0900387 snprintf(dai_name, sizeof(dai_name),
388 "%s-%s", cpus->dai_name, codecs->dai_name);
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900389
390 asoc_simple_canonicalize_cpu(cpus, is_single_links);
Kuninori Morimoto5f939f42021-08-30 09:44:44 +0900391 asoc_simple_canonicalize_platform(platforms, cpus);
Kuninori Morimoto582f3502021-05-11 10:18:48 +0900392
Kuninori Morimotoe51237b2021-04-21 14:45:43 +0900393 ret = graph_link_init(priv, cpu_ep, codec_ep, li, dai_name);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000394 if (ret < 0)
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900395 return ret;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000396
Kuninori Morimoto8859f802021-04-21 14:45:21 +0900397 li->link++;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000398
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900399 return 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000400}
401
Sameer Pujard09c7742020-11-02 20:40:13 +0530402static inline bool parse_as_dpcm_link(struct asoc_simple_priv *priv,
403 struct device_node *codec_port,
404 struct asoc_simple_data *adata)
405{
406 if (priv->force_dpcm)
407 return true;
408
409 if (!priv->dpcm_selectable)
410 return false;
411
412 /*
413 * It is DPCM
414 * if Codec port has many endpoints,
415 * or has convert-xxx property
416 */
417 if ((of_get_child_count(codec_port) > 1) ||
418 (adata->convert_rate || adata->convert_channels))
419 return true;
420
421 return false;
422}
423
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900424static int __graph_for_each_link(struct asoc_simple_priv *priv,
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900425 struct link_info *li,
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900426 int (*func_noml)(struct asoc_simple_priv *priv,
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900427 struct device_node *cpu_ep,
428 struct device_node *codec_ep,
429 struct link_info *li),
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900430 int (*func_dpcm)(struct asoc_simple_priv *priv,
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900431 struct device_node *cpu_ep,
432 struct device_node *codec_ep,
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900433 struct link_info *li))
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000434{
435 struct of_phandle_iterator it;
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900436 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900437 struct device_node *node = dev->of_node;
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900438 struct device_node *cpu_port;
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900439 struct device_node *cpu_ep;
440 struct device_node *codec_ep;
441 struct device_node *codec_port;
442 struct device_node *codec_port_old = NULL;
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900443 struct asoc_simple_data adata;
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900444 int rc, ret = 0;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000445
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900446 /* loop for all listed CPU port */
447 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
448 cpu_port = it.node;
449 cpu_ep = NULL;
450
451 /* loop for all CPU endpoint */
452 while (1) {
453 cpu_ep = of_get_next_child(cpu_port, cpu_ep);
454 if (!cpu_ep)
455 break;
456
457 /* get codec */
458 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
459 codec_port = of_get_parent(codec_ep);
460
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900461 /* get convert-xxx property */
462 memset(&adata, 0, sizeof(adata));
Kuninori Morimotod2bf0082019-03-18 13:50:25 +0900463 graph_parse_convert(dev, codec_ep, &adata);
464 graph_parse_convert(dev, cpu_ep, &adata);
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900465
Sameer Pujard09c7742020-11-02 20:40:13 +0530466 /* check if link requires DPCM parsing */
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900467 if (parse_as_dpcm_link(priv, codec_port, &adata)) {
468 /*
469 * Codec endpoint can be NULL for pluggable audio HW.
470 * Platform DT can populate the Codec endpoint depending on the
471 * plugged HW.
472 */
473 /* Do it all CPU endpoint, and 1st Codec endpoint */
474 if (li->cpu ||
475 ((codec_port_old != codec_port) && codec_ep))
476 ret = func_dpcm(priv, cpu_ep, codec_ep, li);
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900477 /* else normal sound */
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900478 } else {
479 if (li->cpu)
480 ret = func_noml(priv, cpu_ep, codec_ep, li);
481 }
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900482
Wen Yang1bcc1fd2019-07-04 16:38:50 +0800483 of_node_put(codec_ep);
484 of_node_put(codec_port);
485
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900486 if (ret < 0)
487 return ret;
488
489 codec_port_old = codec_port;
490 }
491 }
492
493 return 0;
494}
495
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900496static int graph_for_each_link(struct asoc_simple_priv *priv,
497 struct link_info *li,
498 int (*func_noml)(struct asoc_simple_priv *priv,
499 struct device_node *cpu_ep,
500 struct device_node *codec_ep,
501 struct link_info *li),
502 int (*func_dpcm)(struct asoc_simple_priv *priv,
503 struct device_node *cpu_ep,
504 struct device_node *codec_ep,
505 struct link_info *li))
506{
507 int ret;
508 /*
509 * Detect all CPU first, and Detect all Codec 2nd.
510 *
511 * In Normal sound case, all DAIs are detected
512 * as "CPU-Codec".
513 *
514 * In DPCM sound case,
515 * all CPUs are detected as "CPU-dummy", and
516 * all Codecs are detected as "dummy-Codec".
517 * To avoid random sub-device numbering,
518 * detect "dummy-Codec" in last;
519 */
520 for (li->cpu = 1; li->cpu >= 0; li->cpu--) {
521 ret = __graph_for_each_link(priv, li, func_noml, func_dpcm);
522 if (ret < 0)
523 break;
524 }
525
526 return ret;
527}
528
Thierry Reding0f687d82021-04-16 09:11:46 +0200529static int graph_get_dais_count(struct asoc_simple_priv *priv,
530 struct link_info *li);
Sameer Pujare32b1002020-11-02 20:40:14 +0530531
Sameer Pujar6e4ea8a2021-02-07 23:06:57 +0530532int audio_graph_parse_of(struct asoc_simple_priv *priv, struct device *dev)
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900533{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900534 struct snd_soc_card *card = simple_priv_to_card(priv);
Thierry Redingec1af6c2021-04-19 18:41:17 +0200535 struct link_info *li;
Kuninori Morimotofce9b902018-12-20 10:46:20 +0900536 int ret;
537
Thierry Redingec1af6c2021-04-19 18:41:17 +0200538 li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
539 if (!li)
540 return -ENOMEM;
541
Sameer Pujare32b1002020-11-02 20:40:14 +0530542 card->owner = THIS_MODULE;
543 card->dev = dev;
544
Thierry Redingec1af6c2021-04-19 18:41:17 +0200545 ret = graph_get_dais_count(priv, li);
Thierry Reding0f687d82021-04-16 09:11:46 +0200546 if (ret < 0)
547 return ret;
548
Thierry Redingec1af6c2021-04-19 18:41:17 +0200549 if (!li->link)
Sameer Pujare32b1002020-11-02 20:40:14 +0530550 return -EINVAL;
551
Thierry Redingec1af6c2021-04-19 18:41:17 +0200552 ret = asoc_simple_init_priv(priv, li);
Sameer Pujare32b1002020-11-02 20:40:14 +0530553 if (ret < 0)
554 return ret;
555
556 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
557 if (IS_ERR(priv->pa_gpio)) {
558 ret = PTR_ERR(priv->pa_gpio);
559 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
560 return ret;
561 }
562
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900563 ret = asoc_simple_parse_widgets(card, NULL);
Shawn Guof9869072017-06-29 21:26:38 +0800564 if (ret < 0)
565 return ret;
566
Kuninori Morimotoad11e592019-03-20 13:56:50 +0900567 ret = asoc_simple_parse_routing(card, NULL);
Shawn Guof9869072017-06-29 21:26:38 +0800568 if (ret < 0)
569 return ret;
570
Thierry Redingec1af6c2021-04-19 18:41:17 +0200571 memset(li, 0, sizeof(*li));
572 ret = graph_for_each_link(priv, li,
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900573 graph_dai_link_of,
574 graph_dai_link_of_dpcm);
575 if (ret < 0)
576 goto err;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000577
Sameer Pujare32b1002020-11-02 20:40:14 +0530578 ret = asoc_simple_parse_card_name(card, NULL);
579 if (ret < 0)
580 goto err;
581
582 snd_soc_card_set_drvdata(card, priv);
583
584 asoc_simple_debug_info(priv);
585
586 ret = devm_snd_soc_register_card(dev, card);
587 if (ret < 0)
588 goto err;
589
Thierry Redingec1af6c2021-04-19 18:41:17 +0200590 devm_kfree(dev, li);
Sameer Pujare32b1002020-11-02 20:40:14 +0530591 return 0;
592
593err:
594 asoc_simple_clean_reference(card);
595
Kuninori Morimoto7a0299e2021-12-14 11:08:35 +0900596 return dev_err_probe(dev, ret, "parse error\n");
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000597}
Sameer Pujar6e4ea8a2021-02-07 23:06:57 +0530598EXPORT_SYMBOL_GPL(audio_graph_parse_of);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000599
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900600static int graph_count_noml(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900601 struct device_node *cpu_ep,
602 struct device_node *codec_ep,
603 struct link_info *li)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000604{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900605 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900606
Thierry Reding343e55e2021-04-16 09:11:47 +0200607 if (li->link >= SNDRV_MAX_LINKS) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900608 dev_err(dev, "too many links\n");
609 return -EINVAL;
610 }
611
612 li->num[li->link].cpus = 1;
613 li->num[li->link].codecs = 1;
Kuninori Morimoto5f939f42021-08-30 09:44:44 +0900614 li->num[li->link].platforms = 1;
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900615
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900616 li->link += 1; /* 1xCPU-Codec */
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900617
618 dev_dbg(dev, "Count As Normal\n");
619
620 return 0;
621}
622
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900623static int graph_count_dpcm(struct asoc_simple_priv *priv,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900624 struct device_node *cpu_ep,
625 struct device_node *codec_ep,
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900626 struct link_info *li)
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900627{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900628 struct device *dev = simple_priv_to_dev(priv);
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900629
Thierry Reding343e55e2021-04-16 09:11:47 +0200630 if (li->link >= SNDRV_MAX_LINKS) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900631 dev_err(dev, "too many links\n");
632 return -EINVAL;
633 }
634
Kuninori Morimoto674b9432021-03-26 12:26:11 +0900635 if (li->cpu) {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900636 li->num[li->link].cpus = 1;
Kuninori Morimoto5f939f42021-08-30 09:44:44 +0900637 li->num[li->link].platforms = 1;
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900638
Kuninori Morimoto674b9432021-03-26 12:26:11 +0900639 li->link++; /* 1xCPU-dummy */
Kuninori Morimotoe9cbcf22021-03-26 12:26:20 +0900640 } else {
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900641 li->num[li->link].codecs = 1;
Kuninori Morimotof2138aed2021-04-01 13:15:23 +0900642
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900643 li->link++; /* 1xdummy-Codec */
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900644 }
645
646 dev_dbg(dev, "Count As DPCM\n");
647
648 return 0;
649}
650
Thierry Reding0f687d82021-04-16 09:11:46 +0200651static int graph_get_dais_count(struct asoc_simple_priv *priv,
652 struct link_info *li)
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900653{
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900654 /*
655 * link_num : number of links.
656 * CPU-Codec / CPU-dummy / dummy-Codec
657 * dais_num : number of DAIs
658 * ccnf_num : number of codec_conf
659 * same number for "dummy-Codec"
660 *
661 * ex1)
662 * CPU0 --- Codec0 link : 5
663 * CPU1 --- Codec1 dais : 7
664 * CPU2 -/ ccnf : 1
665 * CPU3 --- Codec2
666 *
667 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
668 * => 7 DAIs = 4xCPU + 3xCodec
669 * => 1 ccnf = 1xdummy-Codec
670 *
671 * ex2)
672 * CPU0 --- Codec0 link : 5
673 * CPU1 --- Codec1 dais : 6
674 * CPU2 -/ ccnf : 1
675 * CPU3 -/
676 *
677 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
678 * => 6 DAIs = 4xCPU + 2xCodec
679 * => 1 ccnf = 1xdummy-Codec
680 *
681 * ex3)
682 * CPU0 --- Codec0 link : 6
683 * CPU1 -/ dais : 6
684 * CPU2 --- Codec1 ccnf : 2
685 * CPU3 -/
686 *
687 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
688 * => 6 DAIs = 4xCPU + 2xCodec
689 * => 2 ccnf = 2xdummy-Codec
Kuninori Morimotode2949f2018-12-20 10:45:54 +0900690 *
691 * ex4)
692 * CPU0 --- Codec0 (convert-rate) link : 3
693 * CPU1 --- Codec1 dais : 4
694 * ccnf : 1
695 *
696 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
697 * => 4 DAIs = 2xCPU + 2xCodec
698 * => 1 ccnf = 1xdummy-Codec
Kuninori Morimotoae3cb572018-12-14 11:32:25 +0900699 */
Thierry Reding0f687d82021-04-16 09:11:46 +0200700 return graph_for_each_link(priv, li,
701 graph_count_noml,
702 graph_count_dpcm);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000703}
704
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900705static int graph_probe(struct platform_device *pdev)
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000706{
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900707 struct asoc_simple_priv *priv;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000708 struct device *dev = &pdev->dev;
709 struct snd_soc_card *card;
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000710
711 /* Allocate the private data and the DAI link array */
712 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
713 if (!priv)
714 return -ENOMEM;
715
Kuninori Morimotoe59289c2019-03-20 13:54:59 +0900716 card = simple_priv_to_card(priv);
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900717 card->dapm_widgets = graph_dapm_widgets;
718 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets);
Kuninori Morimoto1a456b12021-04-19 11:02:19 +0900719 card->probe = asoc_graph_card_probe;
Kuninori Morimotodd98fbc2018-12-20 10:46:05 +0900720
Sameer Pujard09c7742020-11-02 20:40:13 +0530721 if (of_device_get_match_data(dev))
722 priv->dpcm_selectable = 1;
723
Sameer Pujar6e4ea8a2021-02-07 23:06:57 +0530724 return audio_graph_parse_of(priv, dev);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000725}
726
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900727static const struct of_device_id graph_of_match[] = {
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000728 { .compatible = "audio-graph-card", },
Kuninori Morimotoc2c61602019-04-04 09:52:15 +0900729 { .compatible = "audio-graph-scu-card",
730 .data = (void *)DPCM_SELECTABLE },
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000731 {},
732};
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900733MODULE_DEVICE_TABLE(of, graph_of_match);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000734
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900735static struct platform_driver graph_card = {
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000736 .driver = {
737 .name = "asoc-audio-graph-card",
Kuninori Morimoto7b828a32017-08-22 04:56:44 +0000738 .pm = &snd_soc_pm_ops,
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900739 .of_match_table = graph_of_match,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000740 },
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900741 .probe = graph_probe,
Kuninori Morimotof6fcc822021-04-19 11:02:25 +0900742 .remove = asoc_simple_remove,
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000743};
Kuninori Morimoto97fe6ca2018-12-20 10:46:33 +0900744module_platform_driver(graph_card);
Kuninori Morimoto2692c1c2017-04-20 01:36:08 +0000745
746MODULE_ALIAS("platform:asoc-audio-graph-card");
747MODULE_LICENSE("GPL v2");
748MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
749MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");