1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // simple-card-utils.c 4 // 5 // Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 6 7 #include <linux/clk.h> 8 #include <linux/gpio.h> 9 #include <linux/gpio/consumer.h> 10 #include <linux/module.h> 11 #include <linux/of.h> 12 #include <linux/of_gpio.h> 13 #include <linux/of_graph.h> 14 #include <sound/jack.h> 15 #include <sound/pcm_params.h> 16 #include <sound/simple_card_utils.h> 17 18 void asoc_simple_convert_fixup(struct asoc_simple_data *data, 19 struct snd_pcm_hw_params *params) 20 { 21 struct snd_interval *rate = hw_param_interval(params, 22 SNDRV_PCM_HW_PARAM_RATE); 23 struct snd_interval *channels = hw_param_interval(params, 24 SNDRV_PCM_HW_PARAM_CHANNELS); 25 26 if (data->convert_rate) 27 rate->min = 28 rate->max = data->convert_rate; 29 30 if (data->convert_channels) 31 channels->min = 32 channels->max = data->convert_channels; 33 } 34 EXPORT_SYMBOL_GPL(asoc_simple_convert_fixup); 35 36 void asoc_simple_parse_convert(struct device_node *np, 37 char *prefix, 38 struct asoc_simple_data *data) 39 { 40 char prop[128]; 41 42 if (!prefix) 43 prefix = ""; 44 45 /* sampling rate convert */ 46 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate"); 47 of_property_read_u32(np, prop, &data->convert_rate); 48 49 /* channels transfer */ 50 snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels"); 51 of_property_read_u32(np, prop, &data->convert_channels); 52 } 53 EXPORT_SYMBOL_GPL(asoc_simple_parse_convert); 54 55 int asoc_simple_parse_daifmt(struct device *dev, 56 struct device_node *node, 57 struct device_node *codec, 58 char *prefix, 59 unsigned int *retfmt) 60 { 61 struct device_node *bitclkmaster = NULL; 62 struct device_node *framemaster = NULL; 63 unsigned int daifmt; 64 65 daifmt = snd_soc_daifmt_parse_format(node, prefix); 66 67 snd_soc_daifmt_parse_clock_provider_as_phandle(node, prefix, &bitclkmaster, &framemaster); 68 if (!bitclkmaster && !framemaster) { 69 /* 70 * No dai-link level and master setting was not found from 71 * sound node level, revert back to legacy DT parsing and 72 * take the settings from codec node. 73 */ 74 dev_dbg(dev, "Revert to legacy daifmt parsing\n"); 75 76 daifmt |= snd_soc_daifmt_parse_clock_provider_as_flag(codec, NULL); 77 } else { 78 daifmt |= snd_soc_daifmt_clock_provider_from_bitmap( 79 ((codec == bitclkmaster) << 4) | (codec == framemaster)); 80 } 81 82 of_node_put(bitclkmaster); 83 of_node_put(framemaster); 84 85 *retfmt = daifmt; 86 87 return 0; 88 } 89 EXPORT_SYMBOL_GPL(asoc_simple_parse_daifmt); 90 91 int asoc_simple_parse_tdm_width_map(struct device *dev, struct device_node *np, 92 struct asoc_simple_dai *dai) 93 { 94 u32 *array_values, *p; 95 int n, i, ret; 96 97 if (!of_property_read_bool(np, "dai-tdm-slot-width-map")) 98 return 0; 99 100 n = of_property_count_elems_of_size(np, "dai-tdm-slot-width-map", sizeof(u32)); 101 if (n % 3) { 102 dev_err(dev, "Invalid number of cells for dai-tdm-slot-width-map\n"); 103 return -EINVAL; 104 } 105 106 dai->tdm_width_map = devm_kcalloc(dev, n, sizeof(*dai->tdm_width_map), GFP_KERNEL); 107 if (!dai->tdm_width_map) 108 return -ENOMEM; 109 110 array_values = kcalloc(n, sizeof(*array_values), GFP_KERNEL); 111 if (!array_values) 112 return -ENOMEM; 113 114 ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n); 115 if (ret < 0) { 116 dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret); 117 goto out; 118 } 119 120 p = array_values; 121 for (i = 0; i < n / 3; ++i) { 122 dai->tdm_width_map[i].sample_bits = *p++; 123 dai->tdm_width_map[i].slot_width = *p++; 124 dai->tdm_width_map[i].slot_count = *p++; 125 } 126 127 dai->n_tdm_widths = i; 128 ret = 0; 129 out: 130 kfree(array_values); 131 132 return ret; 133 } 134 EXPORT_SYMBOL_GPL(asoc_simple_parse_tdm_width_map); 135 136 int asoc_simple_set_dailink_name(struct device *dev, 137 struct snd_soc_dai_link *dai_link, 138 const char *fmt, ...) 139 { 140 va_list ap; 141 char *name = NULL; 142 int ret = -ENOMEM; 143 144 va_start(ap, fmt); 145 name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap); 146 va_end(ap); 147 148 if (name) { 149 ret = 0; 150 151 dai_link->name = name; 152 dai_link->stream_name = name; 153 } 154 155 return ret; 156 } 157 EXPORT_SYMBOL_GPL(asoc_simple_set_dailink_name); 158 159 int asoc_simple_parse_card_name(struct snd_soc_card *card, 160 char *prefix) 161 { 162 int ret; 163 164 if (!prefix) 165 prefix = ""; 166 167 /* Parse the card name from DT */ 168 ret = snd_soc_of_parse_card_name(card, "label"); 169 if (ret < 0 || !card->name) { 170 char prop[128]; 171 172 snprintf(prop, sizeof(prop), "%sname", prefix); 173 ret = snd_soc_of_parse_card_name(card, prop); 174 if (ret < 0) 175 return ret; 176 } 177 178 if (!card->name && card->dai_link) 179 card->name = card->dai_link->name; 180 181 return 0; 182 } 183 EXPORT_SYMBOL_GPL(asoc_simple_parse_card_name); 184 185 static int asoc_simple_clk_enable(struct asoc_simple_dai *dai) 186 { 187 if (dai) 188 return clk_prepare_enable(dai->clk); 189 190 return 0; 191 } 192 193 static void asoc_simple_clk_disable(struct asoc_simple_dai *dai) 194 { 195 if (dai) 196 clk_disable_unprepare(dai->clk); 197 } 198 199 int asoc_simple_parse_clk(struct device *dev, 200 struct device_node *node, 201 struct asoc_simple_dai *simple_dai, 202 struct snd_soc_dai_link_component *dlc) 203 { 204 struct clk *clk; 205 u32 val; 206 207 /* 208 * Parse dai->sysclk come from "clocks = <&xxx>" 209 * (if system has common clock) 210 * or "system-clock-frequency = <xxx>" 211 * or device's module clock. 212 */ 213 clk = devm_get_clk_from_child(dev, node, NULL); 214 simple_dai->clk_fixed = of_property_read_bool( 215 node, "system-clock-fixed"); 216 if (!IS_ERR(clk)) { 217 simple_dai->sysclk = clk_get_rate(clk); 218 219 simple_dai->clk = clk; 220 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) { 221 simple_dai->sysclk = val; 222 simple_dai->clk_fixed = true; 223 } else { 224 clk = devm_get_clk_from_child(dev, dlc->of_node, NULL); 225 if (!IS_ERR(clk)) 226 simple_dai->sysclk = clk_get_rate(clk); 227 } 228 229 if (of_property_read_bool(node, "system-clock-direction-out")) 230 simple_dai->clk_direction = SND_SOC_CLOCK_OUT; 231 232 return 0; 233 } 234 EXPORT_SYMBOL_GPL(asoc_simple_parse_clk); 235 236 static int asoc_simple_check_fixed_sysclk(struct device *dev, 237 struct asoc_simple_dai *dai, 238 unsigned int *fixed_sysclk) 239 { 240 if (dai->clk_fixed) { 241 if (*fixed_sysclk && *fixed_sysclk != dai->sysclk) { 242 dev_err(dev, "inconsistent fixed sysclk rates (%u vs %u)\n", 243 *fixed_sysclk, dai->sysclk); 244 return -EINVAL; 245 } 246 *fixed_sysclk = dai->sysclk; 247 } 248 249 return 0; 250 } 251 252 int asoc_simple_startup(struct snd_pcm_substream *substream) 253 { 254 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 255 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 256 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num); 257 struct asoc_simple_dai *dai; 258 unsigned int fixed_sysclk = 0; 259 int i1, i2, i; 260 int ret; 261 262 for_each_prop_dai_cpu(props, i1, dai) { 263 ret = asoc_simple_clk_enable(dai); 264 if (ret) 265 goto cpu_err; 266 ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk); 267 if (ret) 268 goto cpu_err; 269 } 270 271 for_each_prop_dai_codec(props, i2, dai) { 272 ret = asoc_simple_clk_enable(dai); 273 if (ret) 274 goto codec_err; 275 ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk); 276 if (ret) 277 goto codec_err; 278 } 279 280 if (fixed_sysclk && props->mclk_fs) { 281 unsigned int fixed_rate = fixed_sysclk / props->mclk_fs; 282 283 if (fixed_sysclk % props->mclk_fs) { 284 dev_err(rtd->dev, "fixed sysclk %u not divisible by mclk_fs %u\n", 285 fixed_sysclk, props->mclk_fs); 286 return -EINVAL; 287 } 288 ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE, 289 fixed_rate, fixed_rate); 290 if (ret) 291 goto codec_err; 292 } 293 294 return 0; 295 296 codec_err: 297 for_each_prop_dai_codec(props, i, dai) { 298 if (i >= i2) 299 break; 300 asoc_simple_clk_disable(dai); 301 } 302 cpu_err: 303 for_each_prop_dai_cpu(props, i, dai) { 304 if (i >= i1) 305 break; 306 asoc_simple_clk_disable(dai); 307 } 308 return ret; 309 } 310 EXPORT_SYMBOL_GPL(asoc_simple_startup); 311 312 void asoc_simple_shutdown(struct snd_pcm_substream *substream) 313 { 314 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 315 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 316 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num); 317 struct asoc_simple_dai *dai; 318 int i; 319 320 for_each_prop_dai_cpu(props, i, dai) { 321 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, i); 322 323 if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(cpu_dai)) 324 snd_soc_dai_set_sysclk(cpu_dai, 325 0, 0, SND_SOC_CLOCK_OUT); 326 327 asoc_simple_clk_disable(dai); 328 } 329 for_each_prop_dai_codec(props, i, dai) { 330 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, i); 331 332 if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(codec_dai)) 333 snd_soc_dai_set_sysclk(codec_dai, 334 0, 0, SND_SOC_CLOCK_IN); 335 336 asoc_simple_clk_disable(dai); 337 } 338 } 339 EXPORT_SYMBOL_GPL(asoc_simple_shutdown); 340 341 static int asoc_simple_set_clk_rate(struct device *dev, 342 struct asoc_simple_dai *simple_dai, 343 unsigned long rate) 344 { 345 if (!simple_dai) 346 return 0; 347 348 if (simple_dai->clk_fixed && rate != simple_dai->sysclk) { 349 dev_err(dev, "dai %s invalid clock rate %lu\n", simple_dai->name, rate); 350 return -EINVAL; 351 } 352 353 if (!simple_dai->clk) 354 return 0; 355 356 if (clk_get_rate(simple_dai->clk) == rate) 357 return 0; 358 359 return clk_set_rate(simple_dai->clk, rate); 360 } 361 362 static int asoc_simple_set_tdm(struct snd_soc_dai *dai, 363 struct asoc_simple_dai *simple_dai, 364 struct snd_pcm_hw_params *params) 365 { 366 int sample_bits = params_width(params); 367 int slot_width, slot_count; 368 int i, ret; 369 370 if (!simple_dai || !simple_dai->tdm_width_map) 371 return 0; 372 373 slot_width = simple_dai->slot_width; 374 slot_count = simple_dai->slots; 375 376 if (slot_width == 0) 377 slot_width = sample_bits; 378 379 for (i = 0; i < simple_dai->n_tdm_widths; ++i) { 380 if (simple_dai->tdm_width_map[i].sample_bits == sample_bits) { 381 slot_width = simple_dai->tdm_width_map[i].slot_width; 382 slot_count = simple_dai->tdm_width_map[i].slot_count; 383 break; 384 } 385 } 386 387 ret = snd_soc_dai_set_tdm_slot(dai, 388 simple_dai->tx_slot_mask, 389 simple_dai->rx_slot_mask, 390 slot_count, 391 slot_width); 392 if (ret && ret != -ENOTSUPP) { 393 dev_err(dai->dev, "simple-card: set_tdm_slot error: %d\n", ret); 394 return ret; 395 } 396 397 return 0; 398 } 399 400 int asoc_simple_hw_params(struct snd_pcm_substream *substream, 401 struct snd_pcm_hw_params *params) 402 { 403 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 404 struct asoc_simple_dai *pdai; 405 struct snd_soc_dai *sdai; 406 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 407 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num); 408 unsigned int mclk, mclk_fs = 0; 409 int i, ret; 410 411 if (props->mclk_fs) 412 mclk_fs = props->mclk_fs; 413 414 if (mclk_fs) { 415 struct snd_soc_component *component; 416 mclk = params_rate(params) * mclk_fs; 417 418 for_each_prop_dai_codec(props, i, pdai) { 419 ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk); 420 if (ret < 0) 421 return ret; 422 } 423 424 for_each_prop_dai_cpu(props, i, pdai) { 425 ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk); 426 if (ret < 0) 427 return ret; 428 } 429 430 /* Ensure sysclk is set on all components in case any 431 * (such as platform components) are missed by calls to 432 * snd_soc_dai_set_sysclk. 433 */ 434 for_each_rtd_components(rtd, i, component) { 435 ret = snd_soc_component_set_sysclk(component, 0, 0, 436 mclk, SND_SOC_CLOCK_IN); 437 if (ret && ret != -ENOTSUPP) 438 return ret; 439 } 440 441 for_each_rtd_codec_dais(rtd, i, sdai) { 442 ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_IN); 443 if (ret && ret != -ENOTSUPP) 444 return ret; 445 } 446 447 for_each_rtd_cpu_dais(rtd, i, sdai) { 448 ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_OUT); 449 if (ret && ret != -ENOTSUPP) 450 return ret; 451 } 452 } 453 454 for_each_prop_dai_codec(props, i, pdai) { 455 sdai = asoc_rtd_to_codec(rtd, i); 456 ret = asoc_simple_set_tdm(sdai, pdai, params); 457 if (ret < 0) 458 return ret; 459 } 460 461 for_each_prop_dai_cpu(props, i, pdai) { 462 sdai = asoc_rtd_to_cpu(rtd, i); 463 ret = asoc_simple_set_tdm(sdai, pdai, params); 464 if (ret < 0) 465 return ret; 466 } 467 468 return 0; 469 } 470 EXPORT_SYMBOL_GPL(asoc_simple_hw_params); 471 472 int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 473 struct snd_pcm_hw_params *params) 474 { 475 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 476 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); 477 478 asoc_simple_convert_fixup(&dai_props->adata, params); 479 480 return 0; 481 } 482 EXPORT_SYMBOL_GPL(asoc_simple_be_hw_params_fixup); 483 484 static int asoc_simple_init_dai(struct snd_soc_dai *dai, 485 struct asoc_simple_dai *simple_dai) 486 { 487 int ret; 488 489 if (!simple_dai) 490 return 0; 491 492 if (simple_dai->sysclk) { 493 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 494 simple_dai->clk_direction); 495 if (ret && ret != -ENOTSUPP) { 496 dev_err(dai->dev, "simple-card: set_sysclk error\n"); 497 return ret; 498 } 499 } 500 501 if (simple_dai->slots) { 502 ret = snd_soc_dai_set_tdm_slot(dai, 503 simple_dai->tx_slot_mask, 504 simple_dai->rx_slot_mask, 505 simple_dai->slots, 506 simple_dai->slot_width); 507 if (ret && ret != -ENOTSUPP) { 508 dev_err(dai->dev, "simple-card: set_tdm_slot error\n"); 509 return ret; 510 } 511 } 512 513 return 0; 514 } 515 516 static inline int asoc_simple_component_is_codec(struct snd_soc_component *component) 517 { 518 return component->driver->endianness; 519 } 520 521 static int asoc_simple_init_for_codec2codec(struct snd_soc_pcm_runtime *rtd, 522 struct simple_dai_props *dai_props) 523 { 524 struct snd_soc_dai_link *dai_link = rtd->dai_link; 525 struct snd_soc_component *component; 526 struct snd_soc_pcm_stream *params; 527 struct snd_pcm_hardware hw; 528 int i, ret, stream; 529 530 /* Do nothing if it already has Codec2Codec settings */ 531 if (dai_link->params) 532 return 0; 533 534 /* Do nothing if it was DPCM :: BE */ 535 if (dai_link->no_pcm) 536 return 0; 537 538 /* Only Codecs */ 539 for_each_rtd_components(rtd, i, component) { 540 if (!asoc_simple_component_is_codec(component)) 541 return 0; 542 } 543 544 /* Assumes the capabilities are the same for all supported streams */ 545 for_each_pcm_streams(stream) { 546 ret = snd_soc_runtime_calc_hw(rtd, &hw, stream); 547 if (ret == 0) 548 break; 549 } 550 551 if (ret < 0) { 552 dev_err(rtd->dev, "simple-card: no valid dai_link params\n"); 553 return ret; 554 } 555 556 params = devm_kzalloc(rtd->dev, sizeof(*params), GFP_KERNEL); 557 if (!params) 558 return -ENOMEM; 559 560 params->formats = hw.formats; 561 params->rates = hw.rates; 562 params->rate_min = hw.rate_min; 563 params->rate_max = hw.rate_max; 564 params->channels_min = hw.channels_min; 565 params->channels_max = hw.channels_max; 566 567 dai_link->params = params; 568 dai_link->num_params = 1; 569 570 return 0; 571 } 572 573 int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd) 574 { 575 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 576 struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num); 577 struct asoc_simple_dai *dai; 578 int i, ret; 579 580 for_each_prop_dai_codec(props, i, dai) { 581 ret = asoc_simple_init_dai(asoc_rtd_to_codec(rtd, i), dai); 582 if (ret < 0) 583 return ret; 584 } 585 for_each_prop_dai_cpu(props, i, dai) { 586 ret = asoc_simple_init_dai(asoc_rtd_to_cpu(rtd, i), dai); 587 if (ret < 0) 588 return ret; 589 } 590 591 ret = asoc_simple_init_for_codec2codec(rtd, props); 592 if (ret < 0) 593 return ret; 594 595 return 0; 596 } 597 EXPORT_SYMBOL_GPL(asoc_simple_dai_init); 598 599 void asoc_simple_canonicalize_platform(struct snd_soc_dai_link_component *platforms, 600 struct snd_soc_dai_link_component *cpus) 601 { 602 /* Assumes platform == cpu */ 603 if (!platforms->of_node) 604 platforms->of_node = cpus->of_node; 605 } 606 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_platform); 607 608 void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link_component *cpus, 609 int is_single_links) 610 { 611 /* 612 * In soc_bind_dai_link() will check cpu name after 613 * of_node matching if dai_link has cpu_dai_name. 614 * but, it will never match if name was created by 615 * fmt_single_name() remove cpu_dai_name if cpu_args 616 * was 0. See: 617 * fmt_single_name() 618 * fmt_multiple_name() 619 */ 620 if (is_single_links) 621 cpus->dai_name = NULL; 622 } 623 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_cpu); 624 625 void asoc_simple_clean_reference(struct snd_soc_card *card) 626 { 627 struct snd_soc_dai_link *dai_link; 628 struct snd_soc_dai_link_component *cpu; 629 struct snd_soc_dai_link_component *codec; 630 int i, j; 631 632 for_each_card_prelinks(card, i, dai_link) { 633 for_each_link_cpus(dai_link, j, cpu) 634 of_node_put(cpu->of_node); 635 for_each_link_codecs(dai_link, j, codec) 636 of_node_put(codec->of_node); 637 } 638 } 639 EXPORT_SYMBOL_GPL(asoc_simple_clean_reference); 640 641 int asoc_simple_parse_routing(struct snd_soc_card *card, 642 char *prefix) 643 { 644 struct device_node *node = card->dev->of_node; 645 char prop[128]; 646 647 if (!prefix) 648 prefix = ""; 649 650 snprintf(prop, sizeof(prop), "%s%s", prefix, "routing"); 651 652 if (!of_property_read_bool(node, prop)) 653 return 0; 654 655 return snd_soc_of_parse_audio_routing(card, prop); 656 } 657 EXPORT_SYMBOL_GPL(asoc_simple_parse_routing); 658 659 int asoc_simple_parse_widgets(struct snd_soc_card *card, 660 char *prefix) 661 { 662 struct device_node *node = card->dev->of_node; 663 char prop[128]; 664 665 if (!prefix) 666 prefix = ""; 667 668 snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets"); 669 670 if (of_property_read_bool(node, prop)) 671 return snd_soc_of_parse_audio_simple_widgets(card, prop); 672 673 /* no widgets is not error */ 674 return 0; 675 } 676 EXPORT_SYMBOL_GPL(asoc_simple_parse_widgets); 677 678 int asoc_simple_parse_pin_switches(struct snd_soc_card *card, 679 char *prefix) 680 { 681 char prop[128]; 682 683 if (!prefix) 684 prefix = ""; 685 686 snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches"); 687 688 return snd_soc_of_parse_pin_switches(card, prop); 689 } 690 EXPORT_SYMBOL_GPL(asoc_simple_parse_pin_switches); 691 692 int asoc_simple_init_jack(struct snd_soc_card *card, 693 struct asoc_simple_jack *sjack, 694 int is_hp, char *prefix, 695 char *pin) 696 { 697 struct device *dev = card->dev; 698 enum of_gpio_flags flags; 699 char prop[128]; 700 char *pin_name; 701 char *gpio_name; 702 int mask; 703 int det; 704 705 if (!prefix) 706 prefix = ""; 707 708 sjack->gpio.gpio = -ENOENT; 709 710 if (is_hp) { 711 snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix); 712 pin_name = pin ? pin : "Headphones"; 713 gpio_name = "Headphone detection"; 714 mask = SND_JACK_HEADPHONE; 715 } else { 716 snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix); 717 pin_name = pin ? pin : "Mic Jack"; 718 gpio_name = "Mic detection"; 719 mask = SND_JACK_MICROPHONE; 720 } 721 722 det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags); 723 if (det == -EPROBE_DEFER) 724 return -EPROBE_DEFER; 725 726 if (gpio_is_valid(det)) { 727 sjack->pin.pin = pin_name; 728 sjack->pin.mask = mask; 729 730 sjack->gpio.name = gpio_name; 731 sjack->gpio.report = mask; 732 sjack->gpio.gpio = det; 733 sjack->gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW); 734 sjack->gpio.debounce_time = 150; 735 736 snd_soc_card_jack_new_pins(card, pin_name, mask, &sjack->jack, 737 &sjack->pin, 1); 738 739 snd_soc_jack_add_gpios(&sjack->jack, 1, 740 &sjack->gpio); 741 } 742 743 return 0; 744 } 745 EXPORT_SYMBOL_GPL(asoc_simple_init_jack); 746 747 int asoc_simple_init_priv(struct asoc_simple_priv *priv, 748 struct link_info *li) 749 { 750 struct snd_soc_card *card = simple_priv_to_card(priv); 751 struct device *dev = simple_priv_to_dev(priv); 752 struct snd_soc_dai_link *dai_link; 753 struct simple_dai_props *dai_props; 754 struct asoc_simple_dai *dais; 755 struct snd_soc_dai_link_component *dlcs; 756 struct snd_soc_codec_conf *cconf = NULL; 757 int i, dai_num = 0, dlc_num = 0, cnf_num = 0; 758 759 dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL); 760 dai_link = devm_kcalloc(dev, li->link, sizeof(*dai_link), GFP_KERNEL); 761 if (!dai_props || !dai_link) 762 return -ENOMEM; 763 764 /* 765 * dais (= CPU+Codec) 766 * dlcs (= CPU+Codec+Platform) 767 */ 768 for (i = 0; i < li->link; i++) { 769 int cc = li->num[i].cpus + li->num[i].codecs; 770 771 dai_num += cc; 772 dlc_num += cc + li->num[i].platforms; 773 774 if (!li->num[i].cpus) 775 cnf_num += li->num[i].codecs; 776 } 777 778 dais = devm_kcalloc(dev, dai_num, sizeof(*dais), GFP_KERNEL); 779 dlcs = devm_kcalloc(dev, dlc_num, sizeof(*dlcs), GFP_KERNEL); 780 if (!dais || !dlcs) 781 return -ENOMEM; 782 783 if (cnf_num) { 784 cconf = devm_kcalloc(dev, cnf_num, sizeof(*cconf), GFP_KERNEL); 785 if (!cconf) 786 return -ENOMEM; 787 } 788 789 dev_dbg(dev, "link %d, dais %d, ccnf %d\n", 790 li->link, dai_num, cnf_num); 791 792 /* dummy CPU/Codec */ 793 priv->dummy.of_node = NULL; 794 priv->dummy.dai_name = "snd-soc-dummy-dai"; 795 priv->dummy.name = "snd-soc-dummy"; 796 797 priv->dai_props = dai_props; 798 priv->dai_link = dai_link; 799 priv->dais = dais; 800 priv->dlcs = dlcs; 801 priv->codec_conf = cconf; 802 803 card->dai_link = priv->dai_link; 804 card->num_links = li->link; 805 card->codec_conf = cconf; 806 card->num_configs = cnf_num; 807 808 for (i = 0; i < li->link; i++) { 809 if (li->num[i].cpus) { 810 /* Normal CPU */ 811 dai_props[i].cpus = 812 dai_link[i].cpus = dlcs; 813 dai_props[i].num.cpus = 814 dai_link[i].num_cpus = li->num[i].cpus; 815 dai_props[i].cpu_dai = dais; 816 817 dlcs += li->num[i].cpus; 818 dais += li->num[i].cpus; 819 } else { 820 /* DPCM Be's CPU = dummy */ 821 dai_props[i].cpus = 822 dai_link[i].cpus = &priv->dummy; 823 dai_props[i].num.cpus = 824 dai_link[i].num_cpus = 1; 825 } 826 827 if (li->num[i].codecs) { 828 /* Normal Codec */ 829 dai_props[i].codecs = 830 dai_link[i].codecs = dlcs; 831 dai_props[i].num.codecs = 832 dai_link[i].num_codecs = li->num[i].codecs; 833 dai_props[i].codec_dai = dais; 834 835 dlcs += li->num[i].codecs; 836 dais += li->num[i].codecs; 837 838 if (!li->num[i].cpus) { 839 /* DPCM Be's Codec */ 840 dai_props[i].codec_conf = cconf; 841 cconf += li->num[i].codecs; 842 } 843 } else { 844 /* DPCM Fe's Codec = dummy */ 845 dai_props[i].codecs = 846 dai_link[i].codecs = &priv->dummy; 847 dai_props[i].num.codecs = 848 dai_link[i].num_codecs = 1; 849 } 850 851 if (li->num[i].platforms) { 852 /* Have Platform */ 853 dai_props[i].platforms = 854 dai_link[i].platforms = dlcs; 855 dai_props[i].num.platforms = 856 dai_link[i].num_platforms = li->num[i].platforms; 857 858 dlcs += li->num[i].platforms; 859 } else { 860 /* Doesn't have Platform */ 861 dai_props[i].platforms = 862 dai_link[i].platforms = NULL; 863 dai_props[i].num.platforms = 864 dai_link[i].num_platforms = 0; 865 } 866 } 867 868 return 0; 869 } 870 EXPORT_SYMBOL_GPL(asoc_simple_init_priv); 871 872 int asoc_simple_remove(struct platform_device *pdev) 873 { 874 struct snd_soc_card *card = platform_get_drvdata(pdev); 875 876 asoc_simple_clean_reference(card); 877 878 return 0; 879 } 880 EXPORT_SYMBOL_GPL(asoc_simple_remove); 881 882 int asoc_graph_card_probe(struct snd_soc_card *card) 883 { 884 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card); 885 int ret; 886 887 ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL); 888 if (ret < 0) 889 return ret; 890 891 ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL); 892 if (ret < 0) 893 return ret; 894 895 return 0; 896 } 897 EXPORT_SYMBOL_GPL(asoc_graph_card_probe); 898 899 int asoc_graph_is_ports0(struct device_node *np) 900 { 901 struct device_node *port, *ports, *ports0, *top; 902 int ret; 903 904 /* np is "endpoint" or "port" */ 905 if (of_node_name_eq(np, "endpoint")) { 906 port = of_get_parent(np); 907 } else { 908 port = np; 909 of_node_get(port); 910 } 911 912 ports = of_get_parent(port); 913 top = of_get_parent(ports); 914 ports0 = of_get_child_by_name(top, "ports"); 915 916 ret = ports0 == ports; 917 918 of_node_put(port); 919 of_node_put(ports); 920 of_node_put(ports0); 921 of_node_put(top); 922 923 return ret; 924 } 925 EXPORT_SYMBOL_GPL(asoc_graph_is_ports0); 926 927 /* Module information */ 928 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 929 MODULE_DESCRIPTION("ALSA SoC Simple Card Utils"); 930 MODULE_LICENSE("GPL v2"); 931