b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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 | |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/gpio.h> |
| 13 | #include <linux/gpio/consumer.h> |
| 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> |
| 21 | #include <sound/simple_card_utils.h> |
| 22 | |
| 23 | #define DPCM_SELECTABLE 1 |
| 24 | |
| 25 | #define PREFIX "audio-graph-card," |
| 26 | |
| 27 | static int graph_outdrv_event(struct snd_soc_dapm_widget *w, |
| 28 | struct snd_kcontrol *kcontrol, |
| 29 | int event) |
| 30 | { |
| 31 | struct snd_soc_dapm_context *dapm = w->dapm; |
| 32 | struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(dapm->card); |
| 33 | |
| 34 | switch (event) { |
| 35 | case SND_SOC_DAPM_POST_PMU: |
| 36 | gpiod_set_value_cansleep(priv->pa_gpio, 1); |
| 37 | break; |
| 38 | case SND_SOC_DAPM_PRE_PMD: |
| 39 | gpiod_set_value_cansleep(priv->pa_gpio, 0); |
| 40 | break; |
| 41 | default: |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static const struct snd_soc_dapm_widget graph_dapm_widgets[] = { |
| 49 | SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM, |
| 50 | 0, 0, NULL, 0, graph_outdrv_event, |
| 51 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 52 | }; |
| 53 | |
| 54 | static const struct snd_soc_ops graph_ops = { |
| 55 | .startup = asoc_simple_startup, |
| 56 | .shutdown = asoc_simple_shutdown, |
| 57 | .hw_params = asoc_simple_hw_params, |
| 58 | }; |
| 59 | |
| 60 | static int graph_get_dai_id(struct device_node *ep) |
| 61 | { |
| 62 | struct device_node *node; |
| 63 | struct device_node *endpoint; |
| 64 | struct of_endpoint info; |
| 65 | int i, id; |
| 66 | const u32 *reg; |
| 67 | int ret; |
| 68 | |
| 69 | /* use driver specified DAI ID if exist */ |
| 70 | ret = snd_soc_get_dai_id(ep); |
| 71 | if (ret != -ENOTSUPP) |
| 72 | return ret; |
| 73 | |
| 74 | /* use endpoint/port reg if exist */ |
| 75 | ret = of_graph_parse_endpoint(ep, &info); |
| 76 | if (ret == 0) { |
| 77 | /* |
| 78 | * Because it will count port/endpoint if it doesn't have "reg". |
| 79 | * But, we can't judge whether it has "no reg", or "reg = <0>" |
| 80 | * only of_graph_parse_endpoint(). |
| 81 | * We need to check "reg" property |
| 82 | */ |
| 83 | if (of_get_property(ep, "reg", NULL)) |
| 84 | return info.id; |
| 85 | |
| 86 | node = of_get_parent(ep); |
| 87 | reg = of_get_property(node, "reg", NULL); |
| 88 | of_node_put(node); |
| 89 | if (reg) |
| 90 | return info.port; |
| 91 | } |
| 92 | node = of_graph_get_port_parent(ep); |
| 93 | |
| 94 | /* |
| 95 | * Non HDMI sound case, counting port/endpoint on its DT |
| 96 | * is enough. Let's count it. |
| 97 | */ |
| 98 | i = 0; |
| 99 | id = -1; |
| 100 | for_each_endpoint_of_node(node, endpoint) { |
| 101 | if (endpoint == ep) |
| 102 | id = i; |
| 103 | i++; |
| 104 | } |
| 105 | |
| 106 | of_node_put(node); |
| 107 | |
| 108 | if (id < 0) |
| 109 | return -ENODEV; |
| 110 | |
| 111 | return id; |
| 112 | } |
| 113 | |
| 114 | static int asoc_simple_parse_dai(struct device_node *ep, |
| 115 | struct snd_soc_dai_link_component *dlc, |
| 116 | int *is_single_link) |
| 117 | { |
| 118 | struct device_node *node; |
| 119 | struct of_phandle_args args; |
| 120 | int ret; |
| 121 | |
| 122 | if (!ep) |
| 123 | return 0; |
| 124 | |
| 125 | node = of_graph_get_port_parent(ep); |
| 126 | |
| 127 | /* Get dai->name */ |
| 128 | args.np = node; |
| 129 | args.args[0] = graph_get_dai_id(ep); |
| 130 | args.args_count = (of_graph_get_endpoint_count(node) > 1); |
| 131 | |
| 132 | /* |
| 133 | * FIXME |
| 134 | * |
| 135 | * Here, dlc->dai_name is pointer to CPU/Codec DAI name. |
| 136 | * If user unbinded CPU or Codec driver, but not for Sound Card, |
| 137 | * dlc->dai_name is keeping unbinded CPU or Codec |
| 138 | * driver's pointer. |
| 139 | * |
| 140 | * If user re-bind CPU or Codec driver again, ALSA SoC will try |
| 141 | * to rebind Card via snd_soc_try_rebind_card(), but because of |
| 142 | * above reason, it might can't bind Sound Card. |
| 143 | * Because Sound Card is pointing to released dai_name pointer. |
| 144 | * |
| 145 | * To avoid this rebind Card issue, |
| 146 | * 1) It needs to alloc memory to keep dai_name eventhough |
| 147 | * CPU or Codec driver was unbinded, or |
| 148 | * 2) user need to rebind Sound Card everytime |
| 149 | * if he unbinded CPU or Codec. |
| 150 | */ |
| 151 | ret = snd_soc_get_dai_name(&args, &dlc->dai_name); |
| 152 | if (ret < 0) { |
| 153 | of_node_put(node); |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | dlc->of_node = node; |
| 158 | |
| 159 | if (is_single_link) |
| 160 | *is_single_link = of_graph_get_endpoint_count(node) == 1; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static void graph_parse_convert(struct device *dev, |
| 166 | struct device_node *ep, |
| 167 | struct asoc_simple_data *adata) |
| 168 | { |
| 169 | struct device_node *top = dev->of_node; |
| 170 | struct device_node *port = of_get_parent(ep); |
| 171 | struct device_node *ports = of_get_parent(port); |
| 172 | struct device_node *node = of_graph_get_port_parent(ep); |
| 173 | |
| 174 | asoc_simple_parse_convert(dev, top, NULL, adata); |
| 175 | asoc_simple_parse_convert(dev, node, PREFIX, adata); |
| 176 | asoc_simple_parse_convert(dev, ports, NULL, adata); |
| 177 | asoc_simple_parse_convert(dev, port, NULL, adata); |
| 178 | asoc_simple_parse_convert(dev, ep, NULL, adata); |
| 179 | |
| 180 | of_node_put(port); |
| 181 | of_node_put(ports); |
| 182 | of_node_put(node); |
| 183 | } |
| 184 | |
| 185 | static void graph_parse_mclk_fs(struct device_node *top, |
| 186 | struct device_node *ep, |
| 187 | struct simple_dai_props *props) |
| 188 | { |
| 189 | struct device_node *port = of_get_parent(ep); |
| 190 | struct device_node *ports = of_get_parent(port); |
| 191 | struct device_node *node = of_graph_get_port_parent(ep); |
| 192 | |
| 193 | of_property_read_u32(top, "mclk-fs", &props->mclk_fs); |
| 194 | of_property_read_u32(ports, "mclk-fs", &props->mclk_fs); |
| 195 | of_property_read_u32(port, "mclk-fs", &props->mclk_fs); |
| 196 | of_property_read_u32(ep, "mclk-fs", &props->mclk_fs); |
| 197 | |
| 198 | of_node_put(port); |
| 199 | of_node_put(ports); |
| 200 | of_node_put(node); |
| 201 | } |
| 202 | |
| 203 | static int graph_dai_link_of_dpcm(struct asoc_simple_priv *priv, |
| 204 | struct device_node *cpu_ep, |
| 205 | struct device_node *codec_ep, |
| 206 | struct link_info *li, |
| 207 | int dup_codec) |
| 208 | { |
| 209 | struct device *dev = simple_priv_to_dev(priv); |
| 210 | struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link); |
| 211 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link); |
| 212 | struct device_node *top = dev->of_node; |
| 213 | struct device_node *ep = li->cpu ? cpu_ep : codec_ep; |
| 214 | struct device_node *port; |
| 215 | struct device_node *ports; |
| 216 | struct device_node *node; |
| 217 | struct asoc_simple_dai *dai; |
| 218 | struct snd_soc_dai_link_component *cpus = dai_link->cpus; |
| 219 | struct snd_soc_dai_link_component *codecs = dai_link->codecs; |
| 220 | int ret; |
| 221 | |
| 222 | /* Do it all CPU endpoint, and 1st Codec endpoint */ |
| 223 | if (!li->cpu && dup_codec) |
| 224 | return 0; |
| 225 | |
| 226 | port = of_get_parent(ep); |
| 227 | ports = of_get_parent(port); |
| 228 | node = of_graph_get_port_parent(ep); |
| 229 | |
| 230 | li->link++; |
| 231 | |
| 232 | dev_dbg(dev, "link_of DPCM (%pOF)\n", ep); |
| 233 | |
| 234 | if (li->cpu) { |
| 235 | int is_single_links = 0; |
| 236 | |
| 237 | /* BE is dummy */ |
| 238 | codecs->of_node = NULL; |
| 239 | codecs->dai_name = "snd-soc-dummy-dai"; |
| 240 | codecs->name = "snd-soc-dummy"; |
| 241 | |
| 242 | /* FE settings */ |
| 243 | dai_link->dynamic = 1; |
| 244 | dai_link->dpcm_merged_format = 1; |
| 245 | |
| 246 | dai = |
| 247 | dai_props->cpu_dai = &priv->dais[li->dais++]; |
| 248 | |
| 249 | ret = asoc_simple_parse_cpu(ep, dai_link, &is_single_links); |
| 250 | if (ret) |
| 251 | goto out_put_node; |
| 252 | |
| 253 | ret = asoc_simple_parse_clk_cpu(dev, ep, dai_link, dai); |
| 254 | if (ret < 0) |
| 255 | goto out_put_node; |
| 256 | |
| 257 | ret = asoc_simple_set_dailink_name(dev, dai_link, |
| 258 | "fe.%s", |
| 259 | cpus->dai_name); |
| 260 | if (ret < 0) |
| 261 | goto out_put_node; |
| 262 | |
| 263 | /* card->num_links includes Codec */ |
| 264 | asoc_simple_canonicalize_cpu(dai_link, is_single_links); |
| 265 | } else { |
| 266 | struct snd_soc_codec_conf *cconf; |
| 267 | |
| 268 | /* FE is dummy */ |
| 269 | cpus->of_node = NULL; |
| 270 | cpus->dai_name = "snd-soc-dummy-dai"; |
| 271 | cpus->name = "snd-soc-dummy"; |
| 272 | |
| 273 | /* BE settings */ |
| 274 | dai_link->no_pcm = 1; |
| 275 | dai_link->be_hw_params_fixup = asoc_simple_be_hw_params_fixup; |
| 276 | |
| 277 | dai = |
| 278 | dai_props->codec_dai = &priv->dais[li->dais++]; |
| 279 | |
| 280 | cconf = |
| 281 | dai_props->codec_conf = &priv->codec_conf[li->conf++]; |
| 282 | |
| 283 | ret = asoc_simple_parse_codec(ep, dai_link); |
| 284 | if (ret < 0) |
| 285 | goto out_put_node; |
| 286 | |
| 287 | ret = asoc_simple_parse_clk_codec(dev, ep, dai_link, dai); |
| 288 | if (ret < 0) |
| 289 | goto out_put_node; |
| 290 | |
| 291 | ret = asoc_simple_set_dailink_name(dev, dai_link, |
| 292 | "be.%s", |
| 293 | codecs->dai_name); |
| 294 | if (ret < 0) |
| 295 | goto out_put_node; |
| 296 | |
| 297 | /* check "prefix" from top node */ |
| 298 | snd_soc_of_parse_node_prefix(top, cconf, codecs->of_node, |
| 299 | "prefix"); |
| 300 | snd_soc_of_parse_node_prefix(node, cconf, codecs->of_node, |
| 301 | PREFIX "prefix"); |
| 302 | snd_soc_of_parse_node_prefix(ports, cconf, codecs->of_node, |
| 303 | "prefix"); |
| 304 | snd_soc_of_parse_node_prefix(port, cconf, codecs->of_node, |
| 305 | "prefix"); |
| 306 | } |
| 307 | |
| 308 | graph_parse_convert(dev, ep, &dai_props->adata); |
| 309 | graph_parse_mclk_fs(top, ep, dai_props); |
| 310 | |
| 311 | asoc_simple_canonicalize_platform(dai_link); |
| 312 | |
| 313 | ret = asoc_simple_parse_tdm(ep, dai); |
| 314 | if (ret) |
| 315 | goto out_put_node; |
| 316 | |
| 317 | ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep, |
| 318 | NULL, &dai_link->dai_fmt); |
| 319 | if (ret < 0) |
| 320 | goto out_put_node; |
| 321 | |
| 322 | dai_link->dpcm_playback = 1; |
| 323 | dai_link->dpcm_capture = 1; |
| 324 | dai_link->ops = &graph_ops; |
| 325 | dai_link->init = asoc_simple_dai_init; |
| 326 | |
| 327 | out_put_node: |
| 328 | of_node_put(ports); |
| 329 | of_node_put(port); |
| 330 | of_node_put(node); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static int graph_dai_link_of(struct asoc_simple_priv *priv, |
| 335 | struct device_node *cpu_ep, |
| 336 | struct device_node *codec_ep, |
| 337 | struct link_info *li) |
| 338 | { |
| 339 | struct device *dev = simple_priv_to_dev(priv); |
| 340 | struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, li->link); |
| 341 | struct simple_dai_props *dai_props = simple_priv_to_props(priv, li->link); |
| 342 | struct device_node *top = dev->of_node; |
| 343 | struct asoc_simple_dai *cpu_dai; |
| 344 | struct asoc_simple_dai *codec_dai; |
| 345 | int ret, single_cpu = 0; |
| 346 | |
| 347 | /* Do it only CPU turn */ |
| 348 | if (!li->cpu) |
| 349 | return 0; |
| 350 | |
| 351 | dev_dbg(dev, "link_of (%pOF)\n", cpu_ep); |
| 352 | |
| 353 | li->link++; |
| 354 | |
| 355 | cpu_dai = |
| 356 | dai_props->cpu_dai = &priv->dais[li->dais++]; |
| 357 | codec_dai = |
| 358 | dai_props->codec_dai = &priv->dais[li->dais++]; |
| 359 | |
| 360 | /* Factor to mclk, used in hw_params() */ |
| 361 | graph_parse_mclk_fs(top, cpu_ep, dai_props); |
| 362 | graph_parse_mclk_fs(top, codec_ep, dai_props); |
| 363 | |
| 364 | ret = asoc_simple_parse_daifmt(dev, cpu_ep, codec_ep, |
| 365 | NULL, &dai_link->dai_fmt); |
| 366 | if (ret < 0) |
| 367 | return ret; |
| 368 | |
| 369 | ret = asoc_simple_parse_cpu(cpu_ep, dai_link, &single_cpu); |
| 370 | if (ret < 0) |
| 371 | return ret; |
| 372 | |
| 373 | ret = asoc_simple_parse_codec(codec_ep, dai_link); |
| 374 | if (ret < 0) |
| 375 | return ret; |
| 376 | |
| 377 | ret = asoc_simple_parse_tdm(cpu_ep, cpu_dai); |
| 378 | if (ret < 0) |
| 379 | return ret; |
| 380 | |
| 381 | ret = asoc_simple_parse_tdm(codec_ep, codec_dai); |
| 382 | if (ret < 0) |
| 383 | return ret; |
| 384 | |
| 385 | ret = asoc_simple_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai); |
| 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | |
| 389 | ret = asoc_simple_parse_clk_codec(dev, codec_ep, dai_link, codec_dai); |
| 390 | if (ret < 0) |
| 391 | return ret; |
| 392 | |
| 393 | ret = asoc_simple_set_dailink_name(dev, dai_link, |
| 394 | "%s-%s", |
| 395 | dai_link->cpus->dai_name, |
| 396 | dai_link->codecs->dai_name); |
| 397 | if (ret < 0) |
| 398 | return ret; |
| 399 | |
| 400 | dai_link->ops = &graph_ops; |
| 401 | dai_link->init = asoc_simple_dai_init; |
| 402 | |
| 403 | asoc_simple_canonicalize_cpu(dai_link, single_cpu); |
| 404 | asoc_simple_canonicalize_platform(dai_link); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static int graph_for_each_link(struct asoc_simple_priv *priv, |
| 410 | struct link_info *li, |
| 411 | int (*func_noml)(struct asoc_simple_priv *priv, |
| 412 | struct device_node *cpu_ep, |
| 413 | struct device_node *codec_ep, |
| 414 | struct link_info *li), |
| 415 | int (*func_dpcm)(struct asoc_simple_priv *priv, |
| 416 | struct device_node *cpu_ep, |
| 417 | struct device_node *codec_ep, |
| 418 | struct link_info *li, int dup_codec)) |
| 419 | { |
| 420 | struct of_phandle_iterator it; |
| 421 | struct device *dev = simple_priv_to_dev(priv); |
| 422 | struct device_node *node = dev->of_node; |
| 423 | struct device_node *cpu_port; |
| 424 | struct device_node *cpu_ep; |
| 425 | struct device_node *codec_ep; |
| 426 | struct device_node *codec_port; |
| 427 | struct device_node *codec_port_old = NULL; |
| 428 | struct asoc_simple_data adata; |
| 429 | uintptr_t dpcm_selectable = (uintptr_t)of_device_get_match_data(dev); |
| 430 | int rc, ret; |
| 431 | |
| 432 | /* loop for all listed CPU port */ |
| 433 | of_for_each_phandle(&it, rc, node, "dais", NULL, 0) { |
| 434 | cpu_port = it.node; |
| 435 | cpu_ep = NULL; |
| 436 | |
| 437 | /* loop for all CPU endpoint */ |
| 438 | while (1) { |
| 439 | cpu_ep = of_get_next_child(cpu_port, cpu_ep); |
| 440 | if (!cpu_ep) |
| 441 | break; |
| 442 | |
| 443 | /* get codec */ |
| 444 | codec_ep = of_graph_get_remote_endpoint(cpu_ep); |
| 445 | codec_port = of_get_parent(codec_ep); |
| 446 | |
| 447 | /* get convert-xxx property */ |
| 448 | memset(&adata, 0, sizeof(adata)); |
| 449 | graph_parse_convert(dev, codec_ep, &adata); |
| 450 | graph_parse_convert(dev, cpu_ep, &adata); |
| 451 | |
| 452 | /* |
| 453 | * It is DPCM |
| 454 | * if Codec port has many endpoints, |
| 455 | * or has convert-xxx property |
| 456 | */ |
| 457 | if (dpcm_selectable && |
| 458 | ((of_get_child_count(codec_port) > 1) || |
| 459 | adata.convert_rate || adata.convert_channels)) |
| 460 | ret = func_dpcm(priv, cpu_ep, codec_ep, li, |
| 461 | (codec_port_old == codec_port)); |
| 462 | /* else normal sound */ |
| 463 | else |
| 464 | ret = func_noml(priv, cpu_ep, codec_ep, li); |
| 465 | |
| 466 | of_node_put(codec_ep); |
| 467 | of_node_put(codec_port); |
| 468 | |
| 469 | if (ret < 0) { |
| 470 | of_node_put(cpu_ep); |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | codec_port_old = codec_port; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int graph_parse_of(struct asoc_simple_priv *priv) |
| 482 | { |
| 483 | struct snd_soc_card *card = simple_priv_to_card(priv); |
| 484 | struct link_info li; |
| 485 | int ret; |
| 486 | |
| 487 | ret = asoc_simple_parse_widgets(card, NULL); |
| 488 | if (ret < 0) |
| 489 | return ret; |
| 490 | |
| 491 | ret = asoc_simple_parse_routing(card, NULL); |
| 492 | if (ret < 0) |
| 493 | return ret; |
| 494 | |
| 495 | memset(&li, 0, sizeof(li)); |
| 496 | for (li.cpu = 1; li.cpu >= 0; li.cpu--) { |
| 497 | /* |
| 498 | * Detect all CPU first, and Detect all Codec 2nd. |
| 499 | * |
| 500 | * In Normal sound case, all DAIs are detected |
| 501 | * as "CPU-Codec". |
| 502 | * |
| 503 | * In DPCM sound case, |
| 504 | * all CPUs are detected as "CPU-dummy", and |
| 505 | * all Codecs are detected as "dummy-Codec". |
| 506 | * To avoid random sub-device numbering, |
| 507 | * detect "dummy-Codec" in last; |
| 508 | */ |
| 509 | ret = graph_for_each_link(priv, &li, |
| 510 | graph_dai_link_of, |
| 511 | graph_dai_link_of_dpcm); |
| 512 | if (ret < 0) |
| 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | return asoc_simple_parse_card_name(card, NULL); |
| 517 | } |
| 518 | |
| 519 | static int graph_count_noml(struct asoc_simple_priv *priv, |
| 520 | struct device_node *cpu_ep, |
| 521 | struct device_node *codec_ep, |
| 522 | struct link_info *li) |
| 523 | { |
| 524 | struct device *dev = simple_priv_to_dev(priv); |
| 525 | |
| 526 | li->link += 1; /* 1xCPU-Codec */ |
| 527 | li->dais += 2; /* 1xCPU + 1xCodec */ |
| 528 | |
| 529 | dev_dbg(dev, "Count As Normal\n"); |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static int graph_count_dpcm(struct asoc_simple_priv *priv, |
| 535 | struct device_node *cpu_ep, |
| 536 | struct device_node *codec_ep, |
| 537 | struct link_info *li, |
| 538 | int dup_codec) |
| 539 | { |
| 540 | struct device *dev = simple_priv_to_dev(priv); |
| 541 | |
| 542 | li->link++; /* 1xCPU-dummy */ |
| 543 | li->dais++; /* 1xCPU */ |
| 544 | |
| 545 | if (!dup_codec) { |
| 546 | li->link++; /* 1xdummy-Codec */ |
| 547 | li->conf++; /* 1xdummy-Codec */ |
| 548 | li->dais++; /* 1xCodec */ |
| 549 | } |
| 550 | |
| 551 | dev_dbg(dev, "Count As DPCM\n"); |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static void graph_get_dais_count(struct asoc_simple_priv *priv, |
| 557 | struct link_info *li) |
| 558 | { |
| 559 | struct device *dev = simple_priv_to_dev(priv); |
| 560 | |
| 561 | /* |
| 562 | * link_num : number of links. |
| 563 | * CPU-Codec / CPU-dummy / dummy-Codec |
| 564 | * dais_num : number of DAIs |
| 565 | * ccnf_num : number of codec_conf |
| 566 | * same number for "dummy-Codec" |
| 567 | * |
| 568 | * ex1) |
| 569 | * CPU0 --- Codec0 link : 5 |
| 570 | * CPU1 --- Codec1 dais : 7 |
| 571 | * CPU2 -/ ccnf : 1 |
| 572 | * CPU3 --- Codec2 |
| 573 | * |
| 574 | * => 5 links = 2xCPU-Codec + 2xCPU-dummy + 1xdummy-Codec |
| 575 | * => 7 DAIs = 4xCPU + 3xCodec |
| 576 | * => 1 ccnf = 1xdummy-Codec |
| 577 | * |
| 578 | * ex2) |
| 579 | * CPU0 --- Codec0 link : 5 |
| 580 | * CPU1 --- Codec1 dais : 6 |
| 581 | * CPU2 -/ ccnf : 1 |
| 582 | * CPU3 -/ |
| 583 | * |
| 584 | * => 5 links = 1xCPU-Codec + 3xCPU-dummy + 1xdummy-Codec |
| 585 | * => 6 DAIs = 4xCPU + 2xCodec |
| 586 | * => 1 ccnf = 1xdummy-Codec |
| 587 | * |
| 588 | * ex3) |
| 589 | * CPU0 --- Codec0 link : 6 |
| 590 | * CPU1 -/ dais : 6 |
| 591 | * CPU2 --- Codec1 ccnf : 2 |
| 592 | * CPU3 -/ |
| 593 | * |
| 594 | * => 6 links = 0xCPU-Codec + 4xCPU-dummy + 2xdummy-Codec |
| 595 | * => 6 DAIs = 4xCPU + 2xCodec |
| 596 | * => 2 ccnf = 2xdummy-Codec |
| 597 | * |
| 598 | * ex4) |
| 599 | * CPU0 --- Codec0 (convert-rate) link : 3 |
| 600 | * CPU1 --- Codec1 dais : 4 |
| 601 | * ccnf : 1 |
| 602 | * |
| 603 | * => 3 links = 1xCPU-Codec + 1xCPU-dummy + 1xdummy-Codec |
| 604 | * => 4 DAIs = 2xCPU + 2xCodec |
| 605 | * => 1 ccnf = 1xdummy-Codec |
| 606 | */ |
| 607 | graph_for_each_link(priv, li, |
| 608 | graph_count_noml, |
| 609 | graph_count_dpcm); |
| 610 | dev_dbg(dev, "link %d, dais %d, ccnf %d\n", |
| 611 | li->link, li->dais, li->conf); |
| 612 | } |
| 613 | |
| 614 | static int graph_card_probe(struct snd_soc_card *card) |
| 615 | { |
| 616 | struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card); |
| 617 | int ret; |
| 618 | |
| 619 | ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL); |
| 620 | if (ret < 0) |
| 621 | return ret; |
| 622 | |
| 623 | ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL); |
| 624 | if (ret < 0) |
| 625 | return ret; |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int graph_probe(struct platform_device *pdev) |
| 631 | { |
| 632 | struct asoc_simple_priv *priv; |
| 633 | struct device *dev = &pdev->dev; |
| 634 | struct snd_soc_card *card; |
| 635 | struct link_info li; |
| 636 | int ret; |
| 637 | |
| 638 | /* Allocate the private data and the DAI link array */ |
| 639 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 640 | if (!priv) |
| 641 | return -ENOMEM; |
| 642 | |
| 643 | card = simple_priv_to_card(priv); |
| 644 | card->owner = THIS_MODULE; |
| 645 | card->dev = dev; |
| 646 | card->dapm_widgets = graph_dapm_widgets; |
| 647 | card->num_dapm_widgets = ARRAY_SIZE(graph_dapm_widgets); |
| 648 | card->probe = graph_card_probe; |
| 649 | |
| 650 | memset(&li, 0, sizeof(li)); |
| 651 | graph_get_dais_count(priv, &li); |
| 652 | if (!li.link || !li.dais) |
| 653 | return -EINVAL; |
| 654 | |
| 655 | ret = asoc_simple_init_priv(priv, &li); |
| 656 | if (ret < 0) |
| 657 | return ret; |
| 658 | |
| 659 | priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW); |
| 660 | if (IS_ERR(priv->pa_gpio)) { |
| 661 | ret = PTR_ERR(priv->pa_gpio); |
| 662 | dev_err(dev, "failed to get amplifier gpio: %d\n", ret); |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | ret = graph_parse_of(priv); |
| 667 | if (ret < 0) { |
| 668 | if (ret != -EPROBE_DEFER) |
| 669 | dev_err(dev, "parse error %d\n", ret); |
| 670 | goto err; |
| 671 | } |
| 672 | |
| 673 | snd_soc_card_set_drvdata(card, priv); |
| 674 | |
| 675 | asoc_simple_debug_info(priv); |
| 676 | |
| 677 | ret = devm_snd_soc_register_card(dev, card); |
| 678 | if (ret < 0) |
| 679 | goto err; |
| 680 | |
| 681 | return 0; |
| 682 | err: |
| 683 | asoc_simple_clean_reference(card); |
| 684 | |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | static int graph_remove(struct platform_device *pdev) |
| 689 | { |
| 690 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 691 | |
| 692 | return asoc_simple_clean_reference(card); |
| 693 | } |
| 694 | |
| 695 | static const struct of_device_id graph_of_match[] = { |
| 696 | { .compatible = "audio-graph-card", }, |
| 697 | { .compatible = "audio-graph-scu-card", |
| 698 | .data = (void *)DPCM_SELECTABLE }, |
| 699 | {}, |
| 700 | }; |
| 701 | MODULE_DEVICE_TABLE(of, graph_of_match); |
| 702 | |
| 703 | static struct platform_driver graph_card = { |
| 704 | .driver = { |
| 705 | .name = "asoc-audio-graph-card", |
| 706 | .pm = &snd_soc_pm_ops, |
| 707 | .of_match_table = graph_of_match, |
| 708 | }, |
| 709 | .probe = graph_probe, |
| 710 | .remove = graph_remove, |
| 711 | }; |
| 712 | module_platform_driver(graph_card); |
| 713 | |
| 714 | MODULE_ALIAS("platform:asoc-audio-graph-card"); |
| 715 | MODULE_LICENSE("GPL v2"); |
| 716 | MODULE_DESCRIPTION("ASoC Audio Graph Sound Card"); |
| 717 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |