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/simple_card_utils.h> 16 17 void asoc_simple_convert_fixup(struct asoc_simple_data *data, 18 struct snd_pcm_hw_params *params) 19 { 20 struct snd_interval *rate = hw_param_interval(params, 21 SNDRV_PCM_HW_PARAM_RATE); 22 struct snd_interval *channels = hw_param_interval(params, 23 SNDRV_PCM_HW_PARAM_CHANNELS); 24 25 if (data->convert_rate) 26 rate->min = 27 rate->max = data->convert_rate; 28 29 if (data->convert_channels) 30 channels->min = 31 channels->max = data->convert_channels; 32 } 33 EXPORT_SYMBOL_GPL(asoc_simple_convert_fixup); 34 35 void asoc_simple_parse_convert(struct device *dev, 36 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_of_parse_daifmt(node, prefix, 66 &bitclkmaster, &framemaster); 67 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK; 68 69 if (!bitclkmaster && !framemaster) { 70 /* 71 * No dai-link level and master setting was not found from 72 * sound node level, revert back to legacy DT parsing and 73 * take the settings from codec node. 74 */ 75 dev_dbg(dev, "Revert to legacy daifmt parsing\n"); 76 77 daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) | 78 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK); 79 } else { 80 if (codec == bitclkmaster) 81 daifmt |= (codec == framemaster) ? 82 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS; 83 else 84 daifmt |= (codec == framemaster) ? 85 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS; 86 } 87 88 of_node_put(bitclkmaster); 89 of_node_put(framemaster); 90 91 *retfmt = daifmt; 92 93 return 0; 94 } 95 EXPORT_SYMBOL_GPL(asoc_simple_parse_daifmt); 96 97 int asoc_simple_set_dailink_name(struct device *dev, 98 struct snd_soc_dai_link *dai_link, 99 const char *fmt, ...) 100 { 101 va_list ap; 102 char *name = NULL; 103 int ret = -ENOMEM; 104 105 va_start(ap, fmt); 106 name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap); 107 va_end(ap); 108 109 if (name) { 110 ret = 0; 111 112 dai_link->name = name; 113 dai_link->stream_name = name; 114 } 115 116 return ret; 117 } 118 EXPORT_SYMBOL_GPL(asoc_simple_set_dailink_name); 119 120 int asoc_simple_parse_card_name(struct snd_soc_card *card, 121 char *prefix) 122 { 123 int ret; 124 125 if (!prefix) 126 prefix = ""; 127 128 /* Parse the card name from DT */ 129 ret = snd_soc_of_parse_card_name(card, "label"); 130 if (ret < 0 || !card->name) { 131 char prop[128]; 132 133 snprintf(prop, sizeof(prop), "%sname", prefix); 134 ret = snd_soc_of_parse_card_name(card, prop); 135 if (ret < 0) 136 return ret; 137 } 138 139 if (!card->name && card->dai_link) 140 card->name = card->dai_link->name; 141 142 return 0; 143 } 144 EXPORT_SYMBOL_GPL(asoc_simple_parse_card_name); 145 146 static int asoc_simple_clk_enable(struct asoc_simple_dai *dai) 147 { 148 if (dai) 149 return clk_prepare_enable(dai->clk); 150 151 return 0; 152 } 153 154 static void asoc_simple_clk_disable(struct asoc_simple_dai *dai) 155 { 156 if (dai) 157 clk_disable_unprepare(dai->clk); 158 } 159 160 int asoc_simple_parse_clk(struct device *dev, 161 struct device_node *node, 162 struct asoc_simple_dai *simple_dai, 163 struct snd_soc_dai_link_component *dlc) 164 { 165 struct clk *clk; 166 u32 val; 167 168 /* 169 * Parse dai->sysclk come from "clocks = <&xxx>" 170 * (if system has common clock) 171 * or "system-clock-frequency = <xxx>" 172 * or device's module clock. 173 */ 174 clk = devm_get_clk_from_child(dev, node, NULL); 175 if (IS_ERR(clk)) 176 clk = devm_get_clk_from_child(dev, dlc->of_node, NULL); 177 178 if (!IS_ERR(clk)) { 179 simple_dai->clk = clk; 180 simple_dai->sysclk = clk_get_rate(clk); 181 } else if (!of_property_read_u32(node, "system-clock-frequency", 182 &val)) { 183 simple_dai->sysclk = val; 184 } 185 186 if (of_property_read_bool(node, "system-clock-direction-out")) 187 simple_dai->clk_direction = SND_SOC_CLOCK_OUT; 188 189 return 0; 190 } 191 EXPORT_SYMBOL_GPL(asoc_simple_parse_clk); 192 193 int asoc_simple_startup(struct snd_pcm_substream *substream) 194 { 195 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 196 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 197 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); 198 int ret; 199 200 ret = asoc_simple_clk_enable(dai_props->cpu_dai); 201 if (ret) 202 return ret; 203 204 ret = asoc_simple_clk_enable(dai_props->codec_dai); 205 if (ret) 206 asoc_simple_clk_disable(dai_props->cpu_dai); 207 208 return ret; 209 } 210 EXPORT_SYMBOL_GPL(asoc_simple_startup); 211 212 void asoc_simple_shutdown(struct snd_pcm_substream *substream) 213 { 214 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 215 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 216 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 217 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 218 struct simple_dai_props *dai_props = 219 simple_priv_to_props(priv, rtd->num); 220 221 if (dai_props->mclk_fs) { 222 snd_soc_dai_set_sysclk(codec_dai, 0, 0, SND_SOC_CLOCK_IN); 223 snd_soc_dai_set_sysclk(cpu_dai, 0, 0, SND_SOC_CLOCK_OUT); 224 } 225 226 asoc_simple_clk_disable(dai_props->cpu_dai); 227 228 asoc_simple_clk_disable(dai_props->codec_dai); 229 } 230 EXPORT_SYMBOL_GPL(asoc_simple_shutdown); 231 232 static int asoc_simple_set_clk_rate(struct asoc_simple_dai *simple_dai, 233 unsigned long rate) 234 { 235 if (!simple_dai) 236 return 0; 237 238 if (!simple_dai->clk) 239 return 0; 240 241 if (clk_get_rate(simple_dai->clk) == rate) 242 return 0; 243 244 return clk_set_rate(simple_dai->clk, rate); 245 } 246 247 int asoc_simple_hw_params(struct snd_pcm_substream *substream, 248 struct snd_pcm_hw_params *params) 249 { 250 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 251 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 252 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 253 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 254 struct simple_dai_props *dai_props = 255 simple_priv_to_props(priv, rtd->num); 256 unsigned int mclk, mclk_fs = 0; 257 int ret = 0; 258 259 if (dai_props->mclk_fs) 260 mclk_fs = dai_props->mclk_fs; 261 262 if (mclk_fs) { 263 mclk = params_rate(params) * mclk_fs; 264 265 ret = asoc_simple_set_clk_rate(dai_props->codec_dai, mclk); 266 if (ret < 0) 267 return ret; 268 269 ret = asoc_simple_set_clk_rate(dai_props->cpu_dai, mclk); 270 if (ret < 0) 271 return ret; 272 273 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, 274 SND_SOC_CLOCK_IN); 275 if (ret && ret != -ENOTSUPP) 276 goto err; 277 278 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, 279 SND_SOC_CLOCK_OUT); 280 if (ret && ret != -ENOTSUPP) 281 goto err; 282 } 283 return 0; 284 err: 285 return ret; 286 } 287 EXPORT_SYMBOL_GPL(asoc_simple_hw_params); 288 289 int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 290 struct snd_pcm_hw_params *params) 291 { 292 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 293 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); 294 295 asoc_simple_convert_fixup(&dai_props->adata, params); 296 297 return 0; 298 } 299 EXPORT_SYMBOL_GPL(asoc_simple_be_hw_params_fixup); 300 301 static int asoc_simple_init_dai(struct snd_soc_dai *dai, 302 struct asoc_simple_dai *simple_dai) 303 { 304 int ret; 305 306 if (!simple_dai) 307 return 0; 308 309 if (simple_dai->sysclk) { 310 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 311 simple_dai->clk_direction); 312 if (ret && ret != -ENOTSUPP) { 313 dev_err(dai->dev, "simple-card: set_sysclk error\n"); 314 return ret; 315 } 316 } 317 318 if (simple_dai->slots) { 319 ret = snd_soc_dai_set_tdm_slot(dai, 320 simple_dai->tx_slot_mask, 321 simple_dai->rx_slot_mask, 322 simple_dai->slots, 323 simple_dai->slot_width); 324 if (ret && ret != -ENOTSUPP) { 325 dev_err(dai->dev, "simple-card: set_tdm_slot error\n"); 326 return ret; 327 } 328 } 329 330 return 0; 331 } 332 333 static int asoc_simple_init_dai_link_params(struct snd_soc_pcm_runtime *rtd, 334 struct simple_dai_props *dai_props) 335 { 336 struct snd_soc_dai_link *dai_link = rtd->dai_link; 337 struct snd_soc_component *component; 338 struct snd_soc_pcm_stream *params; 339 struct snd_pcm_hardware hw; 340 int i, ret, stream; 341 342 /* Only codecs should have non_legacy_dai_naming set. */ 343 for_each_rtd_components(rtd, i, component) { 344 if (!component->driver->non_legacy_dai_naming) 345 return 0; 346 } 347 348 /* Assumes the capabilities are the same for all supported streams */ 349 for_each_pcm_streams(stream) { 350 ret = snd_soc_runtime_calc_hw(rtd, &hw, stream); 351 if (ret == 0) 352 break; 353 } 354 355 if (ret < 0) { 356 dev_err(rtd->dev, "simple-card: no valid dai_link params\n"); 357 return ret; 358 } 359 360 params = devm_kzalloc(rtd->dev, sizeof(*params), GFP_KERNEL); 361 if (!params) 362 return -ENOMEM; 363 364 params->formats = hw.formats; 365 params->rates = hw.rates; 366 params->rate_min = hw.rate_min; 367 params->rate_max = hw.rate_max; 368 params->channels_min = hw.channels_min; 369 params->channels_max = hw.channels_max; 370 371 dai_link->params = params; 372 dai_link->num_params = 1; 373 374 return 0; 375 } 376 377 int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd) 378 { 379 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 380 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); 381 int ret; 382 383 ret = asoc_simple_init_dai(asoc_rtd_to_codec(rtd, 0), 384 dai_props->codec_dai); 385 if (ret < 0) 386 return ret; 387 388 ret = asoc_simple_init_dai(asoc_rtd_to_cpu(rtd, 0), 389 dai_props->cpu_dai); 390 if (ret < 0) 391 return ret; 392 393 ret = asoc_simple_init_dai_link_params(rtd, dai_props); 394 if (ret < 0) 395 return ret; 396 397 return 0; 398 } 399 EXPORT_SYMBOL_GPL(asoc_simple_dai_init); 400 401 void asoc_simple_canonicalize_platform(struct snd_soc_dai_link *dai_link) 402 { 403 /* Assumes platform == cpu */ 404 if (!dai_link->platforms->of_node) 405 dai_link->platforms->of_node = dai_link->cpus->of_node; 406 407 /* 408 * DPCM BE can be no platform. 409 * Alloced memory will be waste, but not leak. 410 */ 411 if (!dai_link->platforms->of_node) 412 dai_link->num_platforms = 0; 413 } 414 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_platform); 415 416 void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link *dai_link, 417 int is_single_links) 418 { 419 /* 420 * In soc_bind_dai_link() will check cpu name after 421 * of_node matching if dai_link has cpu_dai_name. 422 * but, it will never match if name was created by 423 * fmt_single_name() remove cpu_dai_name if cpu_args 424 * was 0. See: 425 * fmt_single_name() 426 * fmt_multiple_name() 427 */ 428 if (is_single_links) 429 dai_link->cpus->dai_name = NULL; 430 } 431 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_cpu); 432 433 int asoc_simple_clean_reference(struct snd_soc_card *card) 434 { 435 struct snd_soc_dai_link *dai_link; 436 int i; 437 438 for_each_card_prelinks(card, i, dai_link) { 439 of_node_put(dai_link->cpus->of_node); 440 of_node_put(dai_link->codecs->of_node); 441 } 442 return 0; 443 } 444 EXPORT_SYMBOL_GPL(asoc_simple_clean_reference); 445 446 int asoc_simple_parse_routing(struct snd_soc_card *card, 447 char *prefix) 448 { 449 struct device_node *node = card->dev->of_node; 450 char prop[128]; 451 452 if (!prefix) 453 prefix = ""; 454 455 snprintf(prop, sizeof(prop), "%s%s", prefix, "routing"); 456 457 if (!of_property_read_bool(node, prop)) 458 return 0; 459 460 return snd_soc_of_parse_audio_routing(card, prop); 461 } 462 EXPORT_SYMBOL_GPL(asoc_simple_parse_routing); 463 464 int asoc_simple_parse_widgets(struct snd_soc_card *card, 465 char *prefix) 466 { 467 struct device_node *node = card->dev->of_node; 468 char prop[128]; 469 470 if (!prefix) 471 prefix = ""; 472 473 snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets"); 474 475 if (of_property_read_bool(node, prop)) 476 return snd_soc_of_parse_audio_simple_widgets(card, prop); 477 478 /* no widgets is not error */ 479 return 0; 480 } 481 EXPORT_SYMBOL_GPL(asoc_simple_parse_widgets); 482 483 int asoc_simple_parse_pin_switches(struct snd_soc_card *card, 484 char *prefix) 485 { 486 const unsigned int nb_controls_max = 16; 487 const char **strings, *control_name; 488 struct snd_kcontrol_new *controls; 489 struct device *dev = card->dev; 490 unsigned int i, nb_controls; 491 char prop[128]; 492 int ret; 493 494 if (!prefix) 495 prefix = ""; 496 497 snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches"); 498 499 if (!of_property_read_bool(dev->of_node, prop)) 500 return 0; 501 502 strings = devm_kcalloc(dev, nb_controls_max, 503 sizeof(*strings), GFP_KERNEL); 504 if (!strings) 505 return -ENOMEM; 506 507 ret = of_property_read_string_array(dev->of_node, prop, 508 strings, nb_controls_max); 509 if (ret < 0) 510 return ret; 511 512 nb_controls = (unsigned int)ret; 513 514 controls = devm_kcalloc(dev, nb_controls, 515 sizeof(*controls), GFP_KERNEL); 516 if (!controls) 517 return -ENOMEM; 518 519 for (i = 0; i < nb_controls; i++) { 520 control_name = devm_kasprintf(dev, GFP_KERNEL, 521 "%s Switch", strings[i]); 522 if (!control_name) 523 return -ENOMEM; 524 525 controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 526 controls[i].name = control_name; 527 controls[i].info = snd_soc_dapm_info_pin_switch; 528 controls[i].get = snd_soc_dapm_get_pin_switch; 529 controls[i].put = snd_soc_dapm_put_pin_switch; 530 controls[i].private_value = (unsigned long)strings[i]; 531 } 532 533 card->controls = controls; 534 card->num_controls = nb_controls; 535 536 return 0; 537 } 538 EXPORT_SYMBOL_GPL(asoc_simple_parse_pin_switches); 539 540 int asoc_simple_init_jack(struct snd_soc_card *card, 541 struct asoc_simple_jack *sjack, 542 int is_hp, char *prefix, 543 char *pin) 544 { 545 struct device *dev = card->dev; 546 enum of_gpio_flags flags; 547 char prop[128]; 548 char *pin_name; 549 char *gpio_name; 550 int mask; 551 int det; 552 553 if (!prefix) 554 prefix = ""; 555 556 sjack->gpio.gpio = -ENOENT; 557 558 if (is_hp) { 559 snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix); 560 pin_name = pin ? pin : "Headphones"; 561 gpio_name = "Headphone detection"; 562 mask = SND_JACK_HEADPHONE; 563 } else { 564 snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix); 565 pin_name = pin ? pin : "Mic Jack"; 566 gpio_name = "Mic detection"; 567 mask = SND_JACK_MICROPHONE; 568 } 569 570 det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags); 571 if (det == -EPROBE_DEFER) 572 return -EPROBE_DEFER; 573 574 if (gpio_is_valid(det)) { 575 sjack->pin.pin = pin_name; 576 sjack->pin.mask = mask; 577 578 sjack->gpio.name = gpio_name; 579 sjack->gpio.report = mask; 580 sjack->gpio.gpio = det; 581 sjack->gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW); 582 sjack->gpio.debounce_time = 150; 583 584 snd_soc_card_jack_new(card, pin_name, mask, 585 &sjack->jack, 586 &sjack->pin, 1); 587 588 snd_soc_jack_add_gpios(&sjack->jack, 1, 589 &sjack->gpio); 590 } 591 592 return 0; 593 } 594 EXPORT_SYMBOL_GPL(asoc_simple_init_jack); 595 596 int asoc_simple_init_priv(struct asoc_simple_priv *priv, 597 struct link_info *li) 598 { 599 struct snd_soc_card *card = simple_priv_to_card(priv); 600 struct device *dev = simple_priv_to_dev(priv); 601 struct snd_soc_dai_link *dai_link; 602 struct simple_dai_props *dai_props; 603 struct asoc_simple_dai *dais; 604 struct snd_soc_codec_conf *cconf = NULL; 605 int i; 606 607 dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL); 608 dai_link = devm_kcalloc(dev, li->link, sizeof(*dai_link), GFP_KERNEL); 609 dais = devm_kcalloc(dev, li->dais, sizeof(*dais), GFP_KERNEL); 610 if (!dai_props || !dai_link || !dais) 611 return -ENOMEM; 612 613 if (li->conf) { 614 cconf = devm_kcalloc(dev, li->conf, sizeof(*cconf), GFP_KERNEL); 615 if (!cconf) 616 return -ENOMEM; 617 } 618 619 /* 620 * Use snd_soc_dai_link_component instead of legacy style 621 * It is codec only. but cpu/platform will be supported in the future. 622 * see 623 * soc-core.c :: snd_soc_init_multicodec() 624 * 625 * "platform" might be removed 626 * see 627 * simple-card-utils.c :: asoc_simple_canonicalize_platform() 628 */ 629 for (i = 0; i < li->link; i++) { 630 dai_link[i].cpus = &dai_props[i].cpus; 631 dai_link[i].num_cpus = 1; 632 dai_link[i].codecs = &dai_props[i].codecs; 633 dai_link[i].num_codecs = 1; 634 dai_link[i].platforms = &dai_props[i].platforms; 635 dai_link[i].num_platforms = 1; 636 } 637 638 priv->dai_props = dai_props; 639 priv->dai_link = dai_link; 640 priv->dais = dais; 641 priv->codec_conf = cconf; 642 643 card->dai_link = priv->dai_link; 644 card->num_links = li->link; 645 card->codec_conf = cconf; 646 card->num_configs = li->conf; 647 648 return 0; 649 } 650 EXPORT_SYMBOL_GPL(asoc_simple_init_priv); 651 652 /* Module information */ 653 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 654 MODULE_DESCRIPTION("ALSA SoC Simple Card Utils"); 655 MODULE_LICENSE("GPL v2"); 656