ASoC: audio-graph: add graph_parse_mclk_fs()
[linux-2.6-block.git] / sound / soc / generic / audio-graph-card.c
CommitLineData
decd8961
KM
1// 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
2692c1c6
KM
10#include <linux/clk.h>
11#include <linux/device.h>
12#include <linux/gpio.h>
f986907c 13#include <linux/gpio/consumer.h>
2692c1c6
KM
14#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>
2692c1c6
KM
21#include <sound/simple_card_utils.h>
22
97fe6ca4 23struct graph_priv {
2692c1c6
KM
24 struct snd_soc_card snd_card;
25 struct graph_dai_props {
0e3460bc
KM
26 struct asoc_simple_dai *cpu_dai;
27 struct asoc_simple_dai *codec_dai;
8e6746db 28 struct snd_soc_dai_link_component codecs; /* single codec */
910fdcab 29 struct snd_soc_dai_link_component platforms;
ae3cb579
KM
30 struct asoc_simple_card_data adata;
31 struct snd_soc_codec_conf *codec_conf;
757652dd 32 unsigned int mclk_fs;
2692c1c6 33 } *dai_props;
f6de35cc
KS
34 struct asoc_simple_jack hp_jack;
35 struct asoc_simple_jack mic_jack;
2692c1c6 36 struct snd_soc_dai_link *dai_link;
0e3460bc 37 struct asoc_simple_dai *dais;
ae3cb579 38 struct snd_soc_codec_conf *codec_conf;
f986907c
SG
39 struct gpio_desc *pa_gpio;
40};
41
1e4771a6
KM
42struct link_info {
43 int dais; /* number of dai */
44 int link; /* number of link */
45 int conf; /* number of codec_conf */
46 int cpu; /* turn for CPU / Codec */
47};
48
64ef0817
KM
49#define graph_priv_to_card(priv) (&(priv)->snd_card)
50#define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
51#define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
52#define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
53
ae3cb579
KM
54#define PREFIX "audio-graph-card,"
55
97fe6ca4
KM
56static int graph_outdrv_event(struct snd_soc_dapm_widget *w,
57 struct snd_kcontrol *kcontrol,
58 int event)
f986907c
SG
59{
60 struct snd_soc_dapm_context *dapm = w->dapm;
97fe6ca4 61 struct graph_priv *priv = snd_soc_card_get_drvdata(dapm->card);
f986907c
SG
62
63 switch (event) {
64 case SND_SOC_DAPM_POST_PMU:
65 gpiod_set_value_cansleep(priv->pa_gpio, 1);
66 break;
67 case SND_SOC_DAPM_PRE_PMD:
68 gpiod_set_value_cansleep(priv->pa_gpio, 0);
69 break;
70 default:
71 return -EINVAL;
72 }
73
74 return 0;
75}
76
97fe6ca4 77static const struct snd_soc_dapm_widget graph_dapm_widgets[] = {
f986907c 78 SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
97fe6ca4 79 0, 0, NULL, 0, graph_outdrv_event,
f986907c 80 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
2692c1c6
KM
81};
82
97fe6ca4 83static int graph_startup(struct snd_pcm_substream *substream)
2692c1c6
KM
84{
85 struct snd_soc_pcm_runtime *rtd = substream->private_data;
97fe6ca4 86 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
2692c1c6
KM
87 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
88 int ret;
89
0e3460bc 90 ret = asoc_simple_card_clk_enable(dai_props->cpu_dai);
2692c1c6
KM
91 if (ret)
92 return ret;
93
0e3460bc 94 ret = asoc_simple_card_clk_enable(dai_props->codec_dai);
2692c1c6 95 if (ret)
0e3460bc 96 asoc_simple_card_clk_disable(dai_props->cpu_dai);
2692c1c6
KM
97
98 return ret;
99}
100
97fe6ca4 101static void graph_shutdown(struct snd_pcm_substream *substream)
2692c1c6
KM
102{
103 struct snd_soc_pcm_runtime *rtd = substream->private_data;
97fe6ca4 104 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
2692c1c6
KM
105 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
106
0e3460bc 107 asoc_simple_card_clk_disable(dai_props->cpu_dai);
2692c1c6 108
0e3460bc 109 asoc_simple_card_clk_disable(dai_props->codec_dai);
2692c1c6
KM
110}
111
97fe6ca4
KM
112static int graph_hw_params(struct snd_pcm_substream *substream,
113 struct snd_pcm_hw_params *params)
757652dd
OM
114{
115 struct snd_soc_pcm_runtime *rtd = substream->private_data;
116 struct snd_soc_dai *codec_dai = rtd->codec_dai;
117 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
97fe6ca4 118 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
757652dd
OM
119 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
120 unsigned int mclk, mclk_fs = 0;
121 int ret = 0;
122
56eb8181 123 if (dai_props->mclk_fs)
757652dd
OM
124 mclk_fs = dai_props->mclk_fs;
125
126 if (mclk_fs) {
127 mclk = params_rate(params) * mclk_fs;
128 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
129 SND_SOC_CLOCK_IN);
130 if (ret && ret != -ENOTSUPP)
131 goto err;
132
133 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
134 SND_SOC_CLOCK_OUT);
135 if (ret && ret != -ENOTSUPP)
136 goto err;
137 }
138 return 0;
139err:
140 return ret;
141}
142
97fe6ca4
KM
143static const struct snd_soc_ops graph_ops = {
144 .startup = graph_startup,
145 .shutdown = graph_shutdown,
146 .hw_params = graph_hw_params,
2692c1c6
KM
147};
148
97fe6ca4 149static int graph_dai_init(struct snd_soc_pcm_runtime *rtd)
2692c1c6 150{
97fe6ca4 151 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
0e3460bc
KM
152 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
153 int ret = 0;
2692c1c6 154
0e3460bc
KM
155 ret = asoc_simple_card_init_dai(rtd->codec_dai,
156 dai_props->codec_dai);
2692c1c6
KM
157 if (ret < 0)
158 return ret;
159
0e3460bc
KM
160 ret = asoc_simple_card_init_dai(rtd->cpu_dai,
161 dai_props->cpu_dai);
2692c1c6
KM
162 if (ret < 0)
163 return ret;
164
165 return 0;
166}
167
97fe6ca4
KM
168static int graph_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
169 struct snd_pcm_hw_params *params)
ae3cb579 170{
97fe6ca4 171 struct graph_priv *priv = snd_soc_card_get_drvdata(rtd->card);
ae3cb579
KM
172 struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
173
174 asoc_simple_card_convert_fixup(&dai_props->adata, params);
175
ae3cb579
KM
176 return 0;
177}
178
d2bf008a
KM
179static void graph_parse_convert(struct device *dev,
180 struct device_node *ep,
181 struct asoc_simple_card_data *adata)
40dfae16
KM
182{
183 struct device_node *top = dev->of_node;
184 struct device_node *port = of_get_parent(ep);
185 struct device_node *ports = of_get_parent(port);
186 struct device_node *node = of_graph_get_port_parent(ep);
187
188 asoc_simple_card_parse_convert(dev, top, NULL, adata);
189 asoc_simple_card_parse_convert(dev, node, PREFIX, adata);
190 asoc_simple_card_parse_convert(dev, ports, NULL, adata);
191 asoc_simple_card_parse_convert(dev, port, NULL, adata);
192 asoc_simple_card_parse_convert(dev, ep, NULL, adata);
d2bf008a
KM
193
194 of_node_put(port);
195 of_node_put(ports);
196 of_node_put(node);
40dfae16
KM
197}
198
4346a745
KM
199static void graph_parse_mclk_fs(struct device_node *top,
200 struct device_node *ep,
201 struct graph_dai_props *props)
202{
203 struct device_node *port = of_get_parent(ep);
204 struct device_node *ports = of_get_parent(port);
205 struct device_node *node = of_graph_get_port_parent(ep);
206
207 of_property_read_u32(top, "mclk-fs", &props->mclk_fs);
208 of_property_read_u32(ports, "mclk-fs", &props->mclk_fs);
209 of_property_read_u32(port, "mclk-fs", &props->mclk_fs);
210 of_property_read_u32(ep, "mclk-fs", &props->mclk_fs);
211
212 of_node_put(port);
213 of_node_put(ports);
214 of_node_put(node);
215}
216
97fe6ca4
KM
217static int graph_dai_link_of_dpcm(struct graph_priv *priv,
218 struct device_node *cpu_ep,
219 struct device_node *codec_ep,
220 struct link_info *li,
221 int dup_codec)
ae3cb579
KM
222{
223 struct device *dev = graph_priv_to_dev(priv);
1e4771a6
KM
224 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, li->link);
225 struct graph_dai_props *dai_props = graph_priv_to_props(priv, li->link);
dd98fbc5 226 struct device_node *top = dev->of_node;
1e4771a6 227 struct device_node *ep = li->cpu ? cpu_ep : codec_ep;
dd98fbc5
KM
228 struct device_node *port;
229 struct device_node *ports;
230 struct device_node *node;
ae3cb579 231 struct asoc_simple_dai *dai;
66164a4d 232 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
ae3cb579
KM
233 int ret;
234
dd98fbc5
KM
235 /* Do it all CPU endpoint, and 1st Codec endpoint */
236 if (!li->cpu && dup_codec)
237 return 0;
238
239 port = of_get_parent(ep);
240 ports = of_get_parent(port);
241 node = of_graph_get_port_parent(ep);
242
1e4771a6
KM
243 li->link++;
244
245 dev_dbg(dev, "link_of DPCM (%pOF)\n", ep);
ae3cb579 246
56eb8181
KM
247 of_node_put(ports);
248 of_node_put(port);
dd98fbc5 249 of_node_put(node);
56eb8181 250
1e4771a6 251 if (li->cpu) {
ae3cb579
KM
252
253 /* BE is dummy */
ae3cb579
KM
254 codecs->of_node = NULL;
255 codecs->dai_name = "snd-soc-dummy-dai";
256 codecs->name = "snd-soc-dummy";
257
258 /* FE settings */
259 dai_link->dynamic = 1;
260 dai_link->dpcm_merged_format = 1;
261
262 dai =
1e4771a6 263 dai_props->cpu_dai = &priv->dais[li->dais++];
ae3cb579
KM
264
265 ret = asoc_simple_card_parse_graph_cpu(ep, dai_link);
266 if (ret)
267 return ret;
268
269 ret = asoc_simple_card_parse_clk_cpu(dev, ep, dai_link, dai);
270 if (ret < 0)
271 return ret;
272
273 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
274 "fe.%s",
275 dai_link->cpu_dai_name);
276 if (ret < 0)
277 return ret;
278
279 /* card->num_links includes Codec */
280 asoc_simple_card_canonicalize_cpu(dai_link,
281 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
282 } else {
283 struct snd_soc_codec_conf *cconf;
284
285 /* FE is dummy */
286 dai_link->cpu_of_node = NULL;
287 dai_link->cpu_dai_name = "snd-soc-dummy-dai";
288 dai_link->cpu_name = "snd-soc-dummy";
289
290 /* BE settings */
291 dai_link->no_pcm = 1;
97fe6ca4 292 dai_link->be_hw_params_fixup = graph_be_hw_params_fixup;
ae3cb579
KM
293
294 dai =
1e4771a6 295 dai_props->codec_dai = &priv->dais[li->dais++];
ae3cb579
KM
296
297 cconf =
1e4771a6 298 dai_props->codec_conf = &priv->codec_conf[li->conf++];
ae3cb579
KM
299
300 ret = asoc_simple_card_parse_graph_codec(ep, dai_link);
301 if (ret < 0)
302 return ret;
303
304 ret = asoc_simple_card_parse_clk_codec(dev, ep, dai_link, dai);
305 if (ret < 0)
306 return ret;
307
308 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
309 "be.%s",
66164a4d 310 codecs->dai_name);
ae3cb579
KM
311 if (ret < 0)
312 return ret;
313
314 /* check "prefix" from top node */
66164a4d 315 snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node,
ae3cb579 316 "prefix");
66164a4d
KM
317 snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node,
318 PREFIX "prefix");
319 snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node,
320 "prefix");
321 snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node,
322 "prefix");
ae3cb579
KM
323 }
324
4346a745
KM
325 graph_parse_convert(dev, ep, &dai_props->adata);
326 graph_parse_mclk_fs(top, ep, dai_props);
327
fe7ed4de
KM
328 asoc_simple_card_canonicalize_platform(dai_link);
329
ae3cb579
KM
330 ret = asoc_simple_card_of_parse_tdm(ep, dai);
331 if (ret)
332 return ret;
333
ae3cb579
KM
334 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
335 NULL, &dai_link->dai_fmt);
336 if (ret < 0)
337 return ret;
338
339 dai_link->dpcm_playback = 1;
340 dai_link->dpcm_capture = 1;
97fe6ca4
KM
341 dai_link->ops = &graph_ops;
342 dai_link->init = graph_dai_init;
ae3cb579
KM
343
344 return 0;
345}
346
97fe6ca4
KM
347static int graph_dai_link_of(struct graph_priv *priv,
348 struct device_node *cpu_ep,
349 struct device_node *codec_ep,
350 struct link_info *li)
2692c1c6
KM
351{
352 struct device *dev = graph_priv_to_dev(priv);
1e4771a6
KM
353 struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, li->link);
354 struct graph_dai_props *dai_props = graph_priv_to_props(priv, li->link);
dd98fbc5 355 struct device_node *top = dev->of_node;
0e3460bc
KM
356 struct asoc_simple_dai *cpu_dai;
357 struct asoc_simple_dai *codec_dai;
2692c1c6
KM
358 int ret;
359
dd98fbc5
KM
360 /* Do it only CPU turn */
361 if (!li->cpu)
362 return 0;
363
1e4771a6
KM
364 dev_dbg(dev, "link_of (%pOF)\n", cpu_ep);
365
366 li->link++;
ae3cb579 367
0e3460bc 368 cpu_dai =
1e4771a6 369 dai_props->cpu_dai = &priv->dais[li->dais++];
0e3460bc 370 codec_dai =
1e4771a6 371 dai_props->codec_dai = &priv->dais[li->dais++];
0e3460bc 372
56eb8181 373 /* Factor to mclk, used in hw_params() */
4346a745
KM
374 graph_parse_mclk_fs(top, cpu_ep, dai_props);
375 graph_parse_mclk_fs(top, codec_ep, dai_props);
56eb8181 376
2692c1c6
KM
377 ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
378 NULL, &dai_link->dai_fmt);
379 if (ret < 0)
ae3cb579 380 return ret;
2692c1c6 381
2692c1c6
KM
382 ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link);
383 if (ret < 0)
ae3cb579 384 return ret;
2692c1c6
KM
385
386 ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link);
387 if (ret < 0)
ae3cb579 388 return ret;
2692c1c6 389
c98907d5 390 ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
2692c1c6 391 if (ret < 0)
ae3cb579 392 return ret;
2692c1c6 393
c98907d5 394 ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
2692c1c6 395 if (ret < 0)
ae3cb579 396 return ret;
2692c1c6
KM
397
398 ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
399 if (ret < 0)
ae3cb579 400 return ret;
2692c1c6
KM
401
402 ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
403 if (ret < 0)
ae3cb579 404 return ret;
2692c1c6 405
2692c1c6
KM
406 ret = asoc_simple_card_set_dailink_name(dev, dai_link,
407 "%s-%s",
408 dai_link->cpu_dai_name,
8e6746db 409 dai_link->codecs->dai_name);
2692c1c6 410 if (ret < 0)
ae3cb579 411 return ret;
2692c1c6 412
97fe6ca4
KM
413 dai_link->ops = &graph_ops;
414 dai_link->init = graph_dai_init;
2692c1c6 415
fe7ed4de 416 asoc_simple_card_canonicalize_platform(dai_link);
2692c1c6 417 asoc_simple_card_canonicalize_cpu(dai_link,
47ca9593 418 of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
2692c1c6 419
ae3cb579 420 return 0;
2692c1c6
KM
421}
422
97fe6ca4 423static int graph_for_each_link(struct graph_priv *priv,
fce9b90c 424 struct link_info *li,
97fe6ca4 425 int (*func_noml)(struct graph_priv *priv,
fce9b90c
KM
426 struct device_node *cpu_ep,
427 struct device_node *codec_ep,
428 struct link_info *li),
97fe6ca4 429 int (*func_dpcm)(struct graph_priv *priv,
fce9b90c
KM
430 struct device_node *cpu_ep,
431 struct device_node *codec_ep,
432 struct link_info *li, int dup_codec))
2692c1c6
KM
433{
434 struct of_phandle_iterator it;
435 struct device *dev = graph_priv_to_dev(priv);
fce9b90c 436 struct device_node *node = dev->of_node;
ae3cb579 437 struct device_node *cpu_port;
fce9b90c
KM
438 struct device_node *cpu_ep;
439 struct device_node *codec_ep;
440 struct device_node *codec_port;
441 struct device_node *codec_port_old = NULL;
de2949fe 442 struct asoc_simple_card_data adata;
0e3460bc 443 int rc, ret;
2692c1c6 444
fce9b90c
KM
445 /* loop for all listed CPU port */
446 of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
447 cpu_port = it.node;
448 cpu_ep = NULL;
449
450 /* loop for all CPU endpoint */
451 while (1) {
452 cpu_ep = of_get_next_child(cpu_port, cpu_ep);
453 if (!cpu_ep)
454 break;
455
456 /* get codec */
457 codec_ep = of_graph_get_remote_endpoint(cpu_ep);
458 codec_port = of_get_parent(codec_ep);
459
460 of_node_put(codec_ep);
461 of_node_put(codec_port);
462
463 /* get convert-xxx property */
464 memset(&adata, 0, sizeof(adata));
d2bf008a
KM
465 graph_parse_convert(dev, codec_ep, &adata);
466 graph_parse_convert(dev, cpu_ep, &adata);
fce9b90c
KM
467
468 /*
469 * It is DPCM
470 * if Codec port has many endpoints,
471 * or has convert-xxx property
472 */
473 if ((of_get_child_count(codec_port) > 1) ||
474 adata.convert_rate || adata.convert_channels)
475 ret = func_dpcm(priv, cpu_ep, codec_ep, li,
476 (codec_port_old == codec_port));
477 /* else normal sound */
478 else
479 ret = func_noml(priv, cpu_ep, codec_ep, li);
480
481 if (ret < 0)
482 return ret;
483
484 codec_port_old = codec_port;
485 }
486 }
487
488 return 0;
489}
490
97fe6ca4 491static int graph_parse_of(struct graph_priv *priv)
fce9b90c
KM
492{
493 struct snd_soc_card *card = graph_priv_to_card(priv);
494 struct link_info li;
495 int ret;
496
f986907c
SG
497 ret = asoc_simple_card_of_parse_widgets(card, NULL);
498 if (ret < 0)
499 return ret;
500
33404f3f 501 ret = asoc_simple_card_of_parse_routing(card, NULL);
f986907c
SG
502 if (ret < 0)
503 return ret;
504
1e4771a6 505 memset(&li, 0, sizeof(li));
1e4771a6 506 for (li.cpu = 1; li.cpu >= 0; li.cpu--) {
ae3cb579
KM
507 /*
508 * Detect all CPU first, and Detect all Codec 2nd.
509 *
510 * In Normal sound case, all DAIs are detected
511 * as "CPU-Codec".
512 *
513 * In DPCM sound case,
514 * all CPUs are detected as "CPU-dummy", and
515 * all Codecs are detected as "dummy-Codec".
516 * To avoid random sub-device numbering,
517 * detect "dummy-Codec" in last;
518 */
97fe6ca4
KM
519 ret = graph_for_each_link(priv, &li,
520 graph_dai_link_of,
521 graph_dai_link_of_dpcm);
fce9b90c
KM
522 if (ret < 0)
523 return ret;
2692c1c6
KM
524 }
525
526 return asoc_simple_card_parse_card_name(card, NULL);
527}
528
97fe6ca4
KM
529static int graph_count_noml(struct graph_priv *priv,
530 struct device_node *cpu_ep,
531 struct device_node *codec_ep,
532 struct link_info *li)
2692c1c6 533{
dd98fbc5
KM
534 struct device *dev = graph_priv_to_dev(priv);
535
536 li->link += 1; /* 1xCPU-Codec */
537 li->dais += 2; /* 1xCPU + 1xCodec */
538
539 dev_dbg(dev, "Count As Normal\n");
540
541 return 0;
542}
543
97fe6ca4
KM
544static int graph_count_dpcm(struct graph_priv *priv,
545 struct device_node *cpu_ep,
546 struct device_node *codec_ep,
547 struct link_info *li,
548 int dup_codec)
dd98fbc5
KM
549{
550 struct device *dev = graph_priv_to_dev(priv);
551
552 li->link++; /* 1xCPU-dummy */
553 li->dais++; /* 1xCPU */
554
555 if (!dup_codec) {
556 li->link++; /* 1xdummy-Codec */
557 li->conf++; /* 1xdummy-Codec */
558 li->dais++; /* 1xCodec */
559 }
560
561 dev_dbg(dev, "Count As DPCM\n");
562
563 return 0;
564}
565
97fe6ca4
KM
566static void graph_get_dais_count(struct graph_priv *priv,
567 struct link_info *li)
dd98fbc5
KM
568{
569 struct device *dev = graph_priv_to_dev(priv);
2692c1c6 570
ae3cb579
KM
571 /*
572 * link_num : number of links.
573 * CPU-Codec / CPU-dummy / dummy-Codec
574 * dais_num : number of DAIs
575 * ccnf_num : number of codec_conf
576 * same number for "dummy-Codec"
577 *
578 * ex1)
579 * CPU0 --- Codec0 link : 5
580 * CPU1 --- Codec1 dais : 7
581 * CPU2 -/ ccnf : 1
582 * CPU3 --- Codec2
583 *
584 * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec
585 * => 7 DAIs = 4xCPU + 3xCodec
586 * => 1 ccnf = 1xdummy-Codec
587 *
588 * ex2)
589 * CPU0 --- Codec0 link : 5
590 * CPU1 --- Codec1 dais : 6
591 * CPU2 -/ ccnf : 1
592 * CPU3 -/
593 *
594 * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec
595 * => 6 DAIs = 4xCPU + 2xCodec
596 * => 1 ccnf = 1xdummy-Codec
597 *
598 * ex3)
599 * CPU0 --- Codec0 link : 6
600 * CPU1 -/ dais : 6
601 * CPU2 --- Codec1 ccnf : 2
602 * CPU3 -/
603 *
604 * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec
605 * => 6 DAIs = 4xCPU + 2xCodec
606 * => 2 ccnf = 2xdummy-Codec
de2949fe
KM
607 *
608 * ex4)
609 * CPU0 --- Codec0 (convert-rate) link : 3
610 * CPU1 --- Codec1 dais : 4
611 * ccnf : 1
612 *
613 * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec
614 * => 4 DAIs = 2xCPU + 2xCodec
615 * => 1 ccnf = 1xdummy-Codec
ae3cb579 616 */
97fe6ca4
KM
617 graph_for_each_link(priv, li,
618 graph_count_noml,
619 graph_count_dpcm);
fce9b90c
KM
620 dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
621 li->link, li->dais, li->conf);
2692c1c6
KM
622}
623
97fe6ca4 624static int graph_card_probe(struct snd_soc_card *card)
f6de35cc 625{
97fe6ca4 626 struct graph_priv *priv = snd_soc_card_get_drvdata(card);
f6de35cc
KS
627 int ret;
628
629 ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL);
630 if (ret < 0)
631 return ret;
632
633 ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL);
634 if (ret < 0)
635 return ret;
636
637 return 0;
638}
639
97fe6ca4 640static int graph_probe(struct platform_device *pdev)
2692c1c6 641{
97fe6ca4 642 struct graph_priv *priv;
2692c1c6
KM
643 struct snd_soc_dai_link *dai_link;
644 struct graph_dai_props *dai_props;
0e3460bc 645 struct asoc_simple_dai *dais;
2692c1c6
KM
646 struct device *dev = &pdev->dev;
647 struct snd_soc_card *card;
ae3cb579 648 struct snd_soc_codec_conf *cconf;
1e4771a6 649 struct link_info li;
ae3cb579 650 int ret, i;
2692c1c6
KM
651
652 /* Allocate the private data and the DAI link array */
653 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
654 if (!priv)
655 return -ENOMEM;
656
dd98fbc5
KM
657 card = graph_priv_to_card(priv);
658 card->owner = THIS_MODULE;
659 card->dev = dev;
97fe6ca4
KM
660 card->dapm_widgets = graph_dapm_widgets;
661 card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets);
662 card->probe = graph_card_probe;
dd98fbc5 663
1e4771a6 664 memset(&li, 0, sizeof(li));
97fe6ca4 665 graph_get_dais_count(priv, &li);
1e4771a6 666 if (!li.link || !li.dais)
2692c1c6
KM
667 return -EINVAL;
668
1e4771a6
KM
669 dai_props = devm_kcalloc(dev, li.link, sizeof(*dai_props), GFP_KERNEL);
670 dai_link = devm_kcalloc(dev, li.link, sizeof(*dai_link), GFP_KERNEL);
671 dais = devm_kcalloc(dev, li.dais, sizeof(*dais), GFP_KERNEL);
672 cconf = devm_kcalloc(dev, li.conf, sizeof(*cconf), GFP_KERNEL);
0e3460bc 673 if (!dai_props || !dai_link || !dais)
2692c1c6
KM
674 return -ENOMEM;
675
8e6746db
KM
676 /*
677 * Use snd_soc_dai_link_component instead of legacy style
678 * It is codec only. but cpu/platform will be supported in the future.
679 * see
680 * soc-core.c :: snd_soc_init_multicodec()
681 */
1e4771a6 682 for (i = 0; i < li.link; i++) {
8e6746db
KM
683 dai_link[i].codecs = &dai_props[i].codecs;
684 dai_link[i].num_codecs = 1;
910fdcab
KM
685 dai_link[i].platforms = &dai_props[i].platforms;
686 dai_link[i].num_platforms = 1;
8e6746db
KM
687 }
688
f986907c
SG
689 priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
690 if (IS_ERR(priv->pa_gpio)) {
691 ret = PTR_ERR(priv->pa_gpio);
692 dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
693 return ret;
694 }
695
dd98fbc5
KM
696 priv->dai_props = dai_props;
697 priv->dai_link = dai_link;
698 priv->dais = dais;
699 priv->codec_conf = cconf;
2692c1c6 700
ae3cb579 701 card->dai_link = dai_link;
1e4771a6 702 card->num_links = li.link;
ae3cb579 703 card->codec_conf = cconf;
1e4771a6 704 card->num_configs = li.conf;
2692c1c6 705
97fe6ca4 706 ret = graph_parse_of(priv);
2692c1c6
KM
707 if (ret < 0) {
708 if (ret != -EPROBE_DEFER)
709 dev_err(dev, "parse error %d\n", ret);
710 goto err;
711 }
712
713 snd_soc_card_set_drvdata(card, priv);
714
715 ret = devm_snd_soc_register_card(dev, card);
ecea9313
KM
716 if (ret < 0)
717 goto err;
718
719 return 0;
2692c1c6
KM
720err:
721 asoc_simple_card_clean_reference(card);
722
723 return ret;
724}
725
97fe6ca4 726static int graph_remove(struct platform_device *pdev)
2692c1c6
KM
727{
728 struct snd_soc_card *card = platform_get_drvdata(pdev);
729
730 return asoc_simple_card_clean_reference(card);
731}
732
97fe6ca4 733static const struct of_device_id graph_of_match[] = {
2692c1c6 734 { .compatible = "audio-graph-card", },
ae3cb579 735 { .compatible = "audio-graph-scu-card", },
2692c1c6
KM
736 {},
737};
97fe6ca4 738MODULE_DEVICE_TABLE(of, graph_of_match);
2692c1c6 739
97fe6ca4 740static struct platform_driver graph_card = {
2692c1c6
KM
741 .driver = {
742 .name = "asoc-audio-graph-card",
7b828a35 743 .pm = &snd_soc_pm_ops,
97fe6ca4 744 .of_match_table = graph_of_match,
2692c1c6 745 },
97fe6ca4
KM
746 .probe = graph_probe,
747 .remove = graph_remove,
2692c1c6 748};
97fe6ca4 749module_platform_driver(graph_card);
2692c1c6
KM
750
751MODULE_ALIAS("platform:asoc-audio-graph-card");
752MODULE_LICENSE("GPL v2");
753MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
754MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");