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 device_node *dai_of_node, 163 struct asoc_simple_dai *simple_dai, 164 const char *dai_name, 165 struct snd_soc_dai_link_component *dlc) 166 { 167 struct clk *clk; 168 u32 val; 169 170 /* 171 * Use snd_soc_dai_link_component instead of legacy style. 172 * It is only for codec, but cpu will be supported in the future. 173 * see 174 * soc-core.c :: snd_soc_init_multicodec() 175 */ 176 if (dlc) 177 dai_of_node = dlc->of_node; 178 179 /* 180 * Parse dai->sysclk come from "clocks = <&xxx>" 181 * (if system has common clock) 182 * or "system-clock-frequency = <xxx>" 183 * or device's module clock. 184 */ 185 clk = devm_get_clk_from_child(dev, node, NULL); 186 if (!IS_ERR(clk)) { 187 simple_dai->sysclk = clk_get_rate(clk); 188 189 simple_dai->clk = clk; 190 } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) { 191 simple_dai->sysclk = val; 192 } else { 193 clk = devm_get_clk_from_child(dev, dai_of_node, NULL); 194 if (!IS_ERR(clk)) 195 simple_dai->sysclk = clk_get_rate(clk); 196 } 197 198 if (of_property_read_bool(node, "system-clock-direction-out")) 199 simple_dai->clk_direction = SND_SOC_CLOCK_OUT; 200 201 return 0; 202 } 203 EXPORT_SYMBOL_GPL(asoc_simple_parse_clk); 204 205 int asoc_simple_startup(struct snd_pcm_substream *substream) 206 { 207 struct snd_soc_pcm_runtime *rtd = substream->private_data; 208 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 209 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); 210 int ret; 211 212 ret = asoc_simple_clk_enable(dai_props->cpu_dai); 213 if (ret) 214 return ret; 215 216 ret = asoc_simple_clk_enable(dai_props->codec_dai); 217 if (ret) 218 asoc_simple_clk_disable(dai_props->cpu_dai); 219 220 return ret; 221 } 222 EXPORT_SYMBOL_GPL(asoc_simple_startup); 223 224 void asoc_simple_shutdown(struct snd_pcm_substream *substream) 225 { 226 struct snd_soc_pcm_runtime *rtd = substream->private_data; 227 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 228 struct simple_dai_props *dai_props = 229 simple_priv_to_props(priv, rtd->num); 230 231 asoc_simple_clk_disable(dai_props->cpu_dai); 232 233 asoc_simple_clk_disable(dai_props->codec_dai); 234 } 235 EXPORT_SYMBOL_GPL(asoc_simple_shutdown); 236 237 static int asoc_simple_set_clk_rate(struct asoc_simple_dai *simple_dai, 238 unsigned long rate) 239 { 240 if (!simple_dai) 241 return 0; 242 243 if (!simple_dai->clk) 244 return 0; 245 246 if (clk_get_rate(simple_dai->clk) == rate) 247 return 0; 248 249 return clk_set_rate(simple_dai->clk, rate); 250 } 251 252 int asoc_simple_hw_params(struct snd_pcm_substream *substream, 253 struct snd_pcm_hw_params *params) 254 { 255 struct snd_soc_pcm_runtime *rtd = substream->private_data; 256 struct snd_soc_dai *codec_dai = rtd->codec_dai; 257 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 258 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 259 struct simple_dai_props *dai_props = 260 simple_priv_to_props(priv, rtd->num); 261 unsigned int mclk, mclk_fs = 0; 262 int ret = 0; 263 264 if (dai_props->mclk_fs) 265 mclk_fs = dai_props->mclk_fs; 266 267 if (mclk_fs) { 268 mclk = params_rate(params) * mclk_fs; 269 270 ret = asoc_simple_set_clk_rate(dai_props->codec_dai, mclk); 271 if (ret < 0) 272 return ret; 273 274 ret = asoc_simple_set_clk_rate(dai_props->cpu_dai, mclk); 275 if (ret < 0) 276 return ret; 277 278 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, 279 SND_SOC_CLOCK_IN); 280 if (ret && ret != -ENOTSUPP) 281 goto err; 282 283 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, 284 SND_SOC_CLOCK_OUT); 285 if (ret && ret != -ENOTSUPP) 286 goto err; 287 } 288 return 0; 289 err: 290 return ret; 291 } 292 EXPORT_SYMBOL_GPL(asoc_simple_hw_params); 293 294 int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 295 struct snd_pcm_hw_params *params) 296 { 297 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 298 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); 299 300 asoc_simple_convert_fixup(&dai_props->adata, params); 301 302 return 0; 303 } 304 EXPORT_SYMBOL_GPL(asoc_simple_be_hw_params_fixup); 305 306 static int asoc_simple_init_dai(struct snd_soc_dai *dai, 307 struct asoc_simple_dai *simple_dai) 308 { 309 int ret; 310 311 if (!simple_dai) 312 return 0; 313 314 if (simple_dai->sysclk) { 315 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 316 simple_dai->clk_direction); 317 if (ret && ret != -ENOTSUPP) { 318 dev_err(dai->dev, "simple-card: set_sysclk error\n"); 319 return ret; 320 } 321 } 322 323 if (simple_dai->slots) { 324 ret = snd_soc_dai_set_tdm_slot(dai, 325 simple_dai->tx_slot_mask, 326 simple_dai->rx_slot_mask, 327 simple_dai->slots, 328 simple_dai->slot_width); 329 if (ret && ret != -ENOTSUPP) { 330 dev_err(dai->dev, "simple-card: set_tdm_slot error\n"); 331 return ret; 332 } 333 } 334 335 return 0; 336 } 337 338 int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd) 339 { 340 struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); 341 struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num); 342 int ret; 343 344 ret = asoc_simple_init_dai(rtd->codec_dai, 345 dai_props->codec_dai); 346 if (ret < 0) 347 return ret; 348 349 ret = asoc_simple_init_dai(rtd->cpu_dai, 350 dai_props->cpu_dai); 351 if (ret < 0) 352 return ret; 353 354 return 0; 355 } 356 EXPORT_SYMBOL_GPL(asoc_simple_dai_init); 357 358 void asoc_simple_canonicalize_platform(struct snd_soc_dai_link *dai_link) 359 { 360 /* Assumes platform == cpu */ 361 if (!dai_link->platforms->of_node) 362 dai_link->platforms->of_node = dai_link->cpu_of_node; 363 } 364 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_platform); 365 366 void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link *dai_link, 367 int is_single_links) 368 { 369 /* 370 * In soc_bind_dai_link() will check cpu name after 371 * of_node matching if dai_link has cpu_dai_name. 372 * but, it will never match if name was created by 373 * fmt_single_name() remove cpu_dai_name if cpu_args 374 * was 0. See: 375 * fmt_single_name() 376 * fmt_multiple_name() 377 */ 378 if (is_single_links) 379 dai_link->cpu_dai_name = NULL; 380 } 381 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_cpu); 382 383 int asoc_simple_clean_reference(struct snd_soc_card *card) 384 { 385 struct snd_soc_dai_link *dai_link; 386 int i; 387 388 for_each_card_prelinks(card, i, dai_link) { 389 of_node_put(dai_link->cpu_of_node); 390 of_node_put(dai_link->codecs->of_node); 391 } 392 return 0; 393 } 394 EXPORT_SYMBOL_GPL(asoc_simple_clean_reference); 395 396 int asoc_simple_parse_routing(struct snd_soc_card *card, 397 char *prefix) 398 { 399 struct device_node *node = card->dev->of_node; 400 char prop[128]; 401 402 if (!prefix) 403 prefix = ""; 404 405 snprintf(prop, sizeof(prop), "%s%s", prefix, "routing"); 406 407 if (!of_property_read_bool(node, prop)) 408 return 0; 409 410 return snd_soc_of_parse_audio_routing(card, prop); 411 } 412 EXPORT_SYMBOL_GPL(asoc_simple_parse_routing); 413 414 int asoc_simple_parse_widgets(struct snd_soc_card *card, 415 char *prefix) 416 { 417 struct device_node *node = card->dev->of_node; 418 char prop[128]; 419 420 if (!prefix) 421 prefix = ""; 422 423 snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets"); 424 425 if (of_property_read_bool(node, prop)) 426 return snd_soc_of_parse_audio_simple_widgets(card, prop); 427 428 /* no widgets is not error */ 429 return 0; 430 } 431 EXPORT_SYMBOL_GPL(asoc_simple_parse_widgets); 432 433 int asoc_simple_parse_pin_switches(struct snd_soc_card *card, 434 char *prefix) 435 { 436 const unsigned int nb_controls_max = 16; 437 const char **strings, *control_name; 438 struct snd_kcontrol_new *controls; 439 struct device *dev = card->dev; 440 unsigned int i, nb_controls; 441 char prop[128]; 442 int ret; 443 444 if (!prefix) 445 prefix = ""; 446 447 snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches"); 448 449 if (!of_property_read_bool(dev->of_node, prop)) 450 return 0; 451 452 strings = devm_kcalloc(dev, nb_controls_max, 453 sizeof(*strings), GFP_KERNEL); 454 if (!strings) 455 return -ENOMEM; 456 457 ret = of_property_read_string_array(dev->of_node, prop, 458 strings, nb_controls_max); 459 if (ret < 0) 460 return ret; 461 462 nb_controls = (unsigned int)ret; 463 464 controls = devm_kcalloc(dev, nb_controls, 465 sizeof(*controls), GFP_KERNEL); 466 if (!controls) 467 return -ENOMEM; 468 469 for (i = 0; i < nb_controls; i++) { 470 control_name = devm_kasprintf(dev, GFP_KERNEL, 471 "%s Switch", strings[i]); 472 if (!control_name) 473 return -ENOMEM; 474 475 controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 476 controls[i].name = control_name; 477 controls[i].info = snd_soc_dapm_info_pin_switch; 478 controls[i].get = snd_soc_dapm_get_pin_switch; 479 controls[i].put = snd_soc_dapm_put_pin_switch; 480 controls[i].private_value = (unsigned long)strings[i]; 481 } 482 483 card->controls = controls; 484 card->num_controls = nb_controls; 485 486 return 0; 487 } 488 EXPORT_SYMBOL_GPL(asoc_simple_parse_pin_switches); 489 490 int asoc_simple_init_jack(struct snd_soc_card *card, 491 struct asoc_simple_jack *sjack, 492 int is_hp, char *prefix) 493 { 494 struct device *dev = card->dev; 495 enum of_gpio_flags flags; 496 char prop[128]; 497 char *pin_name; 498 char *gpio_name; 499 int mask; 500 int det; 501 502 if (!prefix) 503 prefix = ""; 504 505 sjack->gpio.gpio = -ENOENT; 506 507 if (is_hp) { 508 snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix); 509 pin_name = "Headphones"; 510 gpio_name = "Headphone detection"; 511 mask = SND_JACK_HEADPHONE; 512 } else { 513 snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix); 514 pin_name = "Mic Jack"; 515 gpio_name = "Mic detection"; 516 mask = SND_JACK_MICROPHONE; 517 } 518 519 det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags); 520 if (det == -EPROBE_DEFER) 521 return -EPROBE_DEFER; 522 523 if (gpio_is_valid(det)) { 524 sjack->pin.pin = pin_name; 525 sjack->pin.mask = mask; 526 527 sjack->gpio.name = gpio_name; 528 sjack->gpio.report = mask; 529 sjack->gpio.gpio = det; 530 sjack->gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW); 531 sjack->gpio.debounce_time = 150; 532 533 snd_soc_card_jack_new(card, pin_name, mask, 534 &sjack->jack, 535 &sjack->pin, 1); 536 537 snd_soc_jack_add_gpios(&sjack->jack, 1, 538 &sjack->gpio); 539 } 540 541 return 0; 542 } 543 EXPORT_SYMBOL_GPL(asoc_simple_init_jack); 544 545 int asoc_simple_init_priv(struct asoc_simple_priv *priv, 546 struct link_info *li) 547 { 548 struct snd_soc_card *card = simple_priv_to_card(priv); 549 struct device *dev = simple_priv_to_dev(priv); 550 struct snd_soc_dai_link *dai_link; 551 struct simple_dai_props *dai_props; 552 struct asoc_simple_dai *dais; 553 struct snd_soc_codec_conf *cconf = NULL; 554 int i; 555 556 dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL); 557 dai_link = devm_kcalloc(dev, li->link, sizeof(*dai_link), GFP_KERNEL); 558 dais = devm_kcalloc(dev, li->dais, sizeof(*dais), GFP_KERNEL); 559 if (!dai_props || !dai_link || !dais) 560 return -ENOMEM; 561 562 if (li->conf) { 563 cconf = devm_kcalloc(dev, li->conf, sizeof(*cconf), GFP_KERNEL); 564 if (!cconf) 565 return -ENOMEM; 566 } 567 568 /* 569 * Use snd_soc_dai_link_component instead of legacy style 570 * It is codec only. but cpu/platform will be supported in the future. 571 * see 572 * soc-core.c :: snd_soc_init_multicodec() 573 * 574 * "platform" might be removed 575 * see 576 * simple-card-utils.c :: asoc_simple_canonicalize_platform() 577 */ 578 for (i = 0; i < li->link; i++) { 579 dai_link[i].codecs = &dai_props[i].codecs; 580 dai_link[i].num_codecs = 1; 581 dai_link[i].platforms = &dai_props[i].platforms; 582 dai_link[i].num_platforms = 1; 583 } 584 585 priv->dai_props = dai_props; 586 priv->dai_link = dai_link; 587 priv->dais = dais; 588 priv->codec_conf = cconf; 589 590 card->dai_link = priv->dai_link; 591 card->num_links = li->link; 592 card->codec_conf = cconf; 593 card->num_configs = li->conf; 594 595 return 0; 596 } 597 EXPORT_SYMBOL_GPL(asoc_simple_init_priv); 598 599 /* Module information */ 600 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 601 MODULE_DESCRIPTION("ALSA SoC Simple Card Utils"); 602 MODULE_LICENSE("GPL v2"); 603