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