1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // soc-component.c 4 // 5 // Copyright 2009-2011 Wolfson Microelectronics PLC. 6 // Copyright (C) 2019 Renesas Electronics Corp. 7 // 8 // Mark Brown <broonie@opensource.wolfsonmicro.com> 9 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 10 // 11 #include <linux/module.h> 12 #include <linux/pm_runtime.h> 13 #include <sound/soc.h> 14 #include <linux/bitops.h> 15 16 #define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret) 17 static inline int _soc_component_ret(struct snd_soc_component *component, const char *func, int ret) 18 { 19 return snd_soc_ret(component->dev, ret, 20 "at %s() on %s\n", func, component->name); 21 } 22 23 #define soc_component_ret_reg_rw(dai, ret, reg) _soc_component_ret_reg_rw(dai, __func__, ret, reg) 24 static inline int _soc_component_ret_reg_rw(struct snd_soc_component *component, 25 const char *func, int ret, int reg) 26 { 27 return snd_soc_ret(component->dev, ret, 28 "at %s() on %s for register: [0x%08x]\n", 29 func, component->name, reg); 30 } 31 32 static inline int soc_component_field_shift(struct snd_soc_component *component, 33 unsigned int mask) 34 { 35 if (!mask) { 36 dev_err(component->dev, "ASoC: error field mask is zero for %s\n", 37 component->name); 38 return 0; 39 } 40 41 return (ffs(mask) - 1); 42 } 43 44 /* 45 * We might want to check substream by using list. 46 * In such case, we can update these macros. 47 */ 48 #define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream) 49 #define soc_component_mark_pop(component, tgt) ((component)->mark_##tgt = NULL) 50 #define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream) 51 52 void snd_soc_component_set_aux(struct snd_soc_component *component, 53 struct snd_soc_aux_dev *aux) 54 { 55 component->init = (aux) ? aux->init : NULL; 56 } 57 58 int snd_soc_component_init(struct snd_soc_component *component) 59 { 60 int ret = 0; 61 62 if (component->init) 63 ret = component->init(component); 64 65 return soc_component_ret(component, ret); 66 } 67 68 /** 69 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock. 70 * @component: COMPONENT 71 * @clk_id: DAI specific clock ID 72 * @source: Source for the clock 73 * @freq: new clock frequency in Hz 74 * @dir: new clock direction - input/output. 75 * 76 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. 77 */ 78 int snd_soc_component_set_sysclk(struct snd_soc_component *component, 79 int clk_id, int source, unsigned int freq, 80 int dir) 81 { 82 int ret = -ENOTSUPP; 83 84 if (component->driver->set_sysclk) 85 ret = component->driver->set_sysclk(component, clk_id, source, 86 freq, dir); 87 88 return soc_component_ret(component, ret); 89 } 90 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk); 91 92 /* 93 * snd_soc_component_set_pll - configure component PLL. 94 * @component: COMPONENT 95 * @pll_id: DAI specific PLL ID 96 * @source: DAI specific source for the PLL 97 * @freq_in: PLL input clock frequency in Hz 98 * @freq_out: requested PLL output clock frequency in Hz 99 * 100 * Configures and enables PLL to generate output clock based on input clock. 101 */ 102 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id, 103 int source, unsigned int freq_in, 104 unsigned int freq_out) 105 { 106 int ret = -EINVAL; 107 108 if (component->driver->set_pll) 109 ret = component->driver->set_pll(component, pll_id, source, 110 freq_in, freq_out); 111 112 return soc_component_ret(component, ret); 113 } 114 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll); 115 116 void snd_soc_component_seq_notifier(struct snd_soc_component *component, 117 enum snd_soc_dapm_type type, int subseq) 118 { 119 if (component->driver->seq_notifier) 120 component->driver->seq_notifier(component, type, subseq); 121 } 122 123 int snd_soc_component_stream_event(struct snd_soc_component *component, 124 int event) 125 { 126 int ret = 0; 127 128 if (component->driver->stream_event) 129 ret = component->driver->stream_event(component, event); 130 131 return soc_component_ret(component, ret); 132 } 133 134 int snd_soc_component_set_bias_level(struct snd_soc_component *component, 135 enum snd_soc_bias_level level) 136 { 137 int ret = 0; 138 139 if (component->driver->set_bias_level) 140 ret = component->driver->set_bias_level(component, level); 141 142 return soc_component_ret(component, ret); 143 } 144 145 static void soc_get_kcontrol_name(struct snd_soc_component *component, 146 char *buf, int size, const char * const ctl) 147 { 148 /* When updating, change also snd_soc_dapm_widget_name_cmp() */ 149 if (component->name_prefix) 150 snprintf(buf, size, "%s %s", component->name_prefix, ctl); 151 else 152 snprintf(buf, size, "%s", ctl); 153 } 154 155 struct snd_kcontrol *snd_soc_component_get_kcontrol(struct snd_soc_component *component, 156 const char * const ctl) 157 { 158 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 159 160 soc_get_kcontrol_name(component, name, ARRAY_SIZE(name), ctl); 161 162 return snd_soc_card_get_kcontrol(component->card, name); 163 } 164 EXPORT_SYMBOL_GPL(snd_soc_component_get_kcontrol); 165 166 int snd_soc_component_notify_control(struct snd_soc_component *component, 167 const char * const ctl) 168 { 169 struct snd_kcontrol *kctl; 170 171 kctl = snd_soc_component_get_kcontrol(component, ctl); 172 if (!kctl) 173 return soc_component_ret(component, -EINVAL); 174 175 snd_ctl_notify(component->card->snd_card, 176 SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id); 177 178 return 0; 179 } 180 EXPORT_SYMBOL_GPL(snd_soc_component_notify_control); 181 182 /** 183 * snd_soc_component_set_jack - configure component jack. 184 * @component: COMPONENTs 185 * @jack: structure to use for the jack 186 * @data: can be used if codec driver need extra data for configuring jack 187 * 188 * Configures and enables jack detection function. 189 */ 190 int snd_soc_component_set_jack(struct snd_soc_component *component, 191 struct snd_soc_jack *jack, void *data) 192 { 193 int ret = -ENOTSUPP; 194 195 if (component->driver->set_jack) 196 ret = component->driver->set_jack(component, jack, data); 197 198 return soc_component_ret(component, ret); 199 } 200 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack); 201 202 /** 203 * snd_soc_component_get_jack_type 204 * @component: COMPONENTs 205 * 206 * Returns the jack type of the component 207 * This can either be the supported type or one read from 208 * devicetree with the property: jack-type. 209 */ 210 int snd_soc_component_get_jack_type( 211 struct snd_soc_component *component) 212 { 213 int ret = -ENOTSUPP; 214 215 if (component->driver->get_jack_type) 216 ret = component->driver->get_jack_type(component); 217 218 return soc_component_ret(component, ret); 219 } 220 EXPORT_SYMBOL_GPL(snd_soc_component_get_jack_type); 221 222 int snd_soc_component_module_get(struct snd_soc_component *component, 223 void *mark, int upon_open) 224 { 225 int ret = 0; 226 227 if (component->driver->module_get_upon_open == !!upon_open && 228 !try_module_get(component->dev->driver->owner)) 229 ret = -ENODEV; 230 231 /* mark module if succeeded */ 232 if (ret == 0) 233 soc_component_mark_push(component, mark, module); 234 235 return soc_component_ret(component, ret); 236 } 237 238 void snd_soc_component_module_put(struct snd_soc_component *component, 239 void *mark, int upon_open, int rollback) 240 { 241 if (rollback && !soc_component_mark_match(component, mark, module)) 242 return; 243 244 if (component->driver->module_get_upon_open == !!upon_open) 245 module_put(component->dev->driver->owner); 246 247 /* remove the mark from module */ 248 soc_component_mark_pop(component, module); 249 } 250 251 int snd_soc_component_open(struct snd_soc_component *component, 252 struct snd_pcm_substream *substream) 253 { 254 int ret = 0; 255 256 if (component->driver->open) 257 ret = component->driver->open(component, substream); 258 259 /* mark substream if succeeded */ 260 if (ret == 0) 261 soc_component_mark_push(component, substream, open); 262 263 return soc_component_ret(component, ret); 264 } 265 266 int snd_soc_component_close(struct snd_soc_component *component, 267 struct snd_pcm_substream *substream, 268 int rollback) 269 { 270 int ret = 0; 271 272 if (rollback && !soc_component_mark_match(component, substream, open)) 273 return 0; 274 275 if (component->driver->close) 276 ret = component->driver->close(component, substream); 277 278 /* remove marked substream */ 279 soc_component_mark_pop(component, open); 280 281 return soc_component_ret(component, ret); 282 } 283 284 void snd_soc_component_suspend(struct snd_soc_component *component) 285 { 286 if (component->driver->suspend) 287 component->driver->suspend(component); 288 component->suspended = 1; 289 } 290 291 void snd_soc_component_resume(struct snd_soc_component *component) 292 { 293 if (component->driver->resume) 294 component->driver->resume(component); 295 component->suspended = 0; 296 } 297 298 int snd_soc_component_is_suspended(struct snd_soc_component *component) 299 { 300 return component->suspended; 301 } 302 303 int snd_soc_component_probe(struct snd_soc_component *component) 304 { 305 int ret = 0; 306 307 if (component->driver->probe) 308 ret = component->driver->probe(component); 309 310 return soc_component_ret(component, ret); 311 } 312 313 int snd_soc_component_fixup_controls(struct snd_soc_component *component) 314 { 315 int ret = 0; 316 317 if (component->driver->fixup_controls) 318 ret = component->driver->fixup_controls(component); 319 320 return soc_component_ret(component, ret); 321 } 322 323 void snd_soc_component_remove(struct snd_soc_component *component) 324 { 325 if (component->driver->remove) 326 component->driver->remove(component); 327 } 328 329 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component, 330 struct device_node *ep) 331 { 332 int ret = -ENOTSUPP; 333 334 if (component->driver->of_xlate_dai_id) 335 ret = component->driver->of_xlate_dai_id(component, ep); 336 337 return soc_component_ret(component, ret); 338 } 339 340 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component, 341 const struct of_phandle_args *args, 342 const char **dai_name) 343 { 344 if (component->driver->of_xlate_dai_name) 345 return component->driver->of_xlate_dai_name(component, 346 args, dai_name); 347 /* 348 * Don't use soc_component_ret here because we may not want to report 349 * the error just yet. If a device has more than one component, the 350 * first may not match and we don't want spam the log with this. 351 */ 352 return -ENOTSUPP; 353 } 354 355 int snd_soc_component_regmap_val_bytes(struct snd_soc_component *component) 356 { 357 int val_bytes; 358 359 /* Errors are legitimate for non-integer byte multiples */ 360 361 if (!component->regmap) 362 return 0; 363 364 val_bytes = regmap_get_val_bytes(component->regmap); 365 if (val_bytes < 0) 366 return 0; 367 368 return val_bytes; 369 } 370 EXPORT_SYMBOL_GPL(snd_soc_component_regmap_val_bytes); 371 372 #ifdef CONFIG_REGMAP 373 374 /** 375 * snd_soc_component_init_regmap() - Initialize regmap instance for the 376 * component 377 * @component: The component for which to initialize the regmap instance 378 * @regmap: The regmap instance that should be used by the component 379 * 380 * This function allows deferred assignment of the regmap instance that is 381 * associated with the component. Only use this if the regmap instance is not 382 * yet ready when the component is registered. The function must also be called 383 * before the first IO attempt of the component. 384 */ 385 void snd_soc_component_init_regmap(struct snd_soc_component *component, 386 struct regmap *regmap) 387 { 388 component->regmap = regmap; 389 } 390 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); 391 392 /** 393 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the 394 * component 395 * @component: The component for which to de-initialize the regmap instance 396 * 397 * Calls regmap_exit() on the regmap instance associated to the component and 398 * removes the regmap instance from the component. 399 * 400 * This function should only be used if snd_soc_component_init_regmap() was used 401 * to initialize the regmap instance. 402 */ 403 void snd_soc_component_exit_regmap(struct snd_soc_component *component) 404 { 405 regmap_exit(component->regmap); 406 component->regmap = NULL; 407 } 408 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); 409 410 #endif 411 412 int snd_soc_component_compr_open(struct snd_soc_component *component, 413 struct snd_compr_stream *cstream) 414 { 415 int ret = 0; 416 417 if (component->driver->compress_ops && 418 component->driver->compress_ops->open) 419 ret = component->driver->compress_ops->open(component, cstream); 420 421 /* mark substream if succeeded */ 422 if (ret == 0) 423 soc_component_mark_push(component, cstream, compr_open); 424 425 return soc_component_ret(component, ret); 426 } 427 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open); 428 429 void snd_soc_component_compr_free(struct snd_soc_component *component, 430 struct snd_compr_stream *cstream, 431 int rollback) 432 { 433 if (rollback && !soc_component_mark_match(component, cstream, compr_open)) 434 return; 435 436 if (component->driver->compress_ops && 437 component->driver->compress_ops->free) 438 component->driver->compress_ops->free(component, cstream); 439 440 /* remove marked substream */ 441 soc_component_mark_pop(component, compr_open); 442 } 443 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free); 444 445 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd) 446 { 447 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 448 struct snd_soc_component *component; 449 int i, ret; 450 451 for_each_rtd_components(rtd, i, component) { 452 if (component->driver->compress_ops && 453 component->driver->compress_ops->trigger) { 454 ret = component->driver->compress_ops->trigger( 455 component, cstream, cmd); 456 if (ret < 0) 457 return soc_component_ret(component, ret); 458 } 459 } 460 461 return 0; 462 } 463 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger); 464 465 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream, 466 struct snd_compr_params *params) 467 { 468 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 469 struct snd_soc_component *component; 470 int i, ret; 471 472 for_each_rtd_components(rtd, i, component) { 473 if (component->driver->compress_ops && 474 component->driver->compress_ops->set_params) { 475 ret = component->driver->compress_ops->set_params( 476 component, cstream, params); 477 if (ret < 0) 478 return soc_component_ret(component, ret); 479 } 480 } 481 482 return 0; 483 } 484 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params); 485 486 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream, 487 struct snd_codec *params) 488 { 489 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 490 struct snd_soc_component *component; 491 int i, ret; 492 493 for_each_rtd_components(rtd, i, component) { 494 if (component->driver->compress_ops && 495 component->driver->compress_ops->get_params) { 496 ret = component->driver->compress_ops->get_params( 497 component, cstream, params); 498 return soc_component_ret(component, ret); 499 } 500 } 501 502 return 0; 503 } 504 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params); 505 506 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream, 507 struct snd_compr_caps *caps) 508 { 509 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 510 struct snd_soc_component *component; 511 int i, ret = 0; 512 513 snd_soc_dpcm_mutex_lock(rtd); 514 515 for_each_rtd_components(rtd, i, component) { 516 if (component->driver->compress_ops && 517 component->driver->compress_ops->get_caps) { 518 ret = component->driver->compress_ops->get_caps( 519 component, cstream, caps); 520 break; 521 } 522 } 523 524 snd_soc_dpcm_mutex_unlock(rtd); 525 526 return soc_component_ret(component, ret); 527 } 528 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps); 529 530 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream, 531 struct snd_compr_codec_caps *codec) 532 { 533 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 534 struct snd_soc_component *component; 535 int i, ret = 0; 536 537 snd_soc_dpcm_mutex_lock(rtd); 538 539 for_each_rtd_components(rtd, i, component) { 540 if (component->driver->compress_ops && 541 component->driver->compress_ops->get_codec_caps) { 542 ret = component->driver->compress_ops->get_codec_caps( 543 component, cstream, codec); 544 break; 545 } 546 } 547 548 snd_soc_dpcm_mutex_unlock(rtd); 549 550 return soc_component_ret(component, ret); 551 } 552 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps); 553 554 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes) 555 { 556 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 557 struct snd_soc_component *component; 558 int i, ret; 559 560 for_each_rtd_components(rtd, i, component) { 561 if (component->driver->compress_ops && 562 component->driver->compress_ops->ack) { 563 ret = component->driver->compress_ops->ack( 564 component, cstream, bytes); 565 if (ret < 0) 566 return soc_component_ret(component, ret); 567 } 568 } 569 570 return 0; 571 } 572 EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack); 573 574 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream, 575 struct snd_compr_tstamp64 *tstamp) 576 { 577 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 578 struct snd_soc_component *component; 579 int i, ret; 580 581 for_each_rtd_components(rtd, i, component) { 582 if (component->driver->compress_ops && 583 component->driver->compress_ops->pointer) { 584 ret = component->driver->compress_ops->pointer( 585 component, cstream, tstamp); 586 return soc_component_ret(component, ret); 587 } 588 } 589 590 return 0; 591 } 592 EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer); 593 594 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream, 595 char __user *buf, size_t count) 596 { 597 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 598 struct snd_soc_component *component; 599 int i, ret = 0; 600 601 snd_soc_dpcm_mutex_lock(rtd); 602 603 for_each_rtd_components(rtd, i, component) { 604 if (component->driver->compress_ops && 605 component->driver->compress_ops->copy) { 606 ret = component->driver->compress_ops->copy( 607 component, cstream, buf, count); 608 break; 609 } 610 } 611 612 snd_soc_dpcm_mutex_unlock(rtd); 613 614 return soc_component_ret(component, ret); 615 } 616 EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy); 617 618 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream, 619 struct snd_compr_metadata *metadata) 620 { 621 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 622 struct snd_soc_component *component; 623 int i, ret; 624 625 for_each_rtd_components(rtd, i, component) { 626 if (component->driver->compress_ops && 627 component->driver->compress_ops->set_metadata) { 628 ret = component->driver->compress_ops->set_metadata( 629 component, cstream, metadata); 630 if (ret < 0) 631 return soc_component_ret(component, ret); 632 } 633 } 634 635 return 0; 636 } 637 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata); 638 639 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream, 640 struct snd_compr_metadata *metadata) 641 { 642 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 643 struct snd_soc_component *component; 644 int i, ret; 645 646 for_each_rtd_components(rtd, i, component) { 647 if (component->driver->compress_ops && 648 component->driver->compress_ops->get_metadata) { 649 ret = component->driver->compress_ops->get_metadata( 650 component, cstream, metadata); 651 return soc_component_ret(component, ret); 652 } 653 } 654 655 return 0; 656 } 657 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata); 658 659 static unsigned int soc_component_read_no_lock( 660 struct snd_soc_component *component, 661 unsigned int reg) 662 { 663 int ret; 664 unsigned int val = 0; 665 666 if (component->regmap) 667 ret = regmap_read(component->regmap, reg, &val); 668 else if (component->driver->read) { 669 ret = 0; 670 val = component->driver->read(component, reg); 671 } 672 else 673 ret = -EIO; 674 675 if (ret < 0) 676 return soc_component_ret_reg_rw(component, ret, reg); 677 678 return val; 679 } 680 681 /** 682 * snd_soc_component_read() - Read register value 683 * @component: Component to read from 684 * @reg: Register to read 685 * 686 * Return: read value 687 */ 688 unsigned int snd_soc_component_read(struct snd_soc_component *component, 689 unsigned int reg) 690 { 691 unsigned int val; 692 693 mutex_lock(&component->io_mutex); 694 val = soc_component_read_no_lock(component, reg); 695 mutex_unlock(&component->io_mutex); 696 697 return val; 698 } 699 EXPORT_SYMBOL_GPL(snd_soc_component_read); 700 701 static int soc_component_write_no_lock( 702 struct snd_soc_component *component, 703 unsigned int reg, unsigned int val) 704 { 705 int ret = -EIO; 706 707 if (component->regmap) 708 ret = regmap_write(component->regmap, reg, val); 709 else if (component->driver->write) 710 ret = component->driver->write(component, reg, val); 711 712 return soc_component_ret_reg_rw(component, ret, reg); 713 } 714 715 /** 716 * snd_soc_component_write() - Write register value 717 * @component: Component to write to 718 * @reg: Register to write 719 * @val: Value to write to the register 720 * 721 * Return: 0 on success, a negative error code otherwise. 722 */ 723 int snd_soc_component_write(struct snd_soc_component *component, 724 unsigned int reg, unsigned int val) 725 { 726 int ret; 727 728 mutex_lock(&component->io_mutex); 729 ret = soc_component_write_no_lock(component, reg, val); 730 mutex_unlock(&component->io_mutex); 731 732 return ret; 733 } 734 EXPORT_SYMBOL_GPL(snd_soc_component_write); 735 736 static int snd_soc_component_update_bits_legacy( 737 struct snd_soc_component *component, unsigned int reg, 738 unsigned int mask, unsigned int val, bool *change) 739 { 740 unsigned int old, new; 741 int ret = 0; 742 743 mutex_lock(&component->io_mutex); 744 745 old = soc_component_read_no_lock(component, reg); 746 747 new = (old & ~mask) | (val & mask); 748 *change = old != new; 749 if (*change) 750 ret = soc_component_write_no_lock(component, reg, new); 751 752 mutex_unlock(&component->io_mutex); 753 754 return soc_component_ret_reg_rw(component, ret, reg); 755 } 756 757 /** 758 * snd_soc_component_update_bits() - Perform read/modify/write cycle 759 * @component: Component to update 760 * @reg: Register to update 761 * @mask: Mask that specifies which bits to update 762 * @val: New value for the bits specified by mask 763 * 764 * Return: 1 if the operation was successful and the value of the register 765 * changed, 0 if the operation was successful, but the value did not change. 766 * Returns a negative error code otherwise. 767 */ 768 int snd_soc_component_update_bits(struct snd_soc_component *component, 769 unsigned int reg, unsigned int mask, unsigned int val) 770 { 771 bool change; 772 int ret; 773 774 if (component->regmap) 775 ret = regmap_update_bits_check(component->regmap, reg, mask, 776 val, &change); 777 else 778 ret = snd_soc_component_update_bits_legacy(component, reg, 779 mask, val, &change); 780 781 if (ret < 0) 782 return soc_component_ret_reg_rw(component, ret, reg); 783 return change; 784 } 785 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits); 786 787 /** 788 * snd_soc_component_update_bits_async() - Perform asynchronous 789 * read/modify/write cycle 790 * @component: Component to update 791 * @reg: Register to update 792 * @mask: Mask that specifies which bits to update 793 * @val: New value for the bits specified by mask 794 * 795 * This function is similar to snd_soc_component_update_bits(), but the update 796 * operation is scheduled asynchronously. This means it may not be completed 797 * when the function returns. To make sure that all scheduled updates have been 798 * completed snd_soc_component_async_complete() must be called. 799 * 800 * Return: 1 if the operation was successful and the value of the register 801 * changed, 0 if the operation was successful, but the value did not change. 802 * Returns a negative error code otherwise. 803 */ 804 int snd_soc_component_update_bits_async(struct snd_soc_component *component, 805 unsigned int reg, unsigned int mask, unsigned int val) 806 { 807 bool change; 808 int ret; 809 810 if (component->regmap) 811 ret = regmap_update_bits_check_async(component->regmap, reg, 812 mask, val, &change); 813 else 814 ret = snd_soc_component_update_bits_legacy(component, reg, 815 mask, val, &change); 816 817 if (ret < 0) 818 return soc_component_ret_reg_rw(component, ret, reg); 819 return change; 820 } 821 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async); 822 823 /** 824 * snd_soc_component_read_field() - Read register field value 825 * @component: Component to read from 826 * @reg: Register to read 827 * @mask: mask of the register field 828 * 829 * Return: read value of register field. 830 */ 831 unsigned int snd_soc_component_read_field(struct snd_soc_component *component, 832 unsigned int reg, unsigned int mask) 833 { 834 unsigned int val; 835 836 val = snd_soc_component_read(component, reg); 837 838 val = (val & mask) >> soc_component_field_shift(component, mask); 839 840 return val; 841 } 842 EXPORT_SYMBOL_GPL(snd_soc_component_read_field); 843 844 /** 845 * snd_soc_component_write_field() - write to register field 846 * @component: Component to write to 847 * @reg: Register to write 848 * @mask: mask of the register field to update 849 * @val: value of the field to write 850 * 851 * Return: 1 for change, otherwise 0. 852 */ 853 int snd_soc_component_write_field(struct snd_soc_component *component, 854 unsigned int reg, unsigned int mask, 855 unsigned int val) 856 { 857 858 val = (val << soc_component_field_shift(component, mask)) & mask; 859 860 return snd_soc_component_update_bits(component, reg, mask, val); 861 } 862 EXPORT_SYMBOL_GPL(snd_soc_component_write_field); 863 864 /** 865 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed 866 * @component: Component for which to wait 867 * 868 * This function blocks until all asynchronous I/O which has previously been 869 * scheduled using snd_soc_component_update_bits_async() has completed. 870 */ 871 void snd_soc_component_async_complete(struct snd_soc_component *component) 872 { 873 if (component->regmap) 874 regmap_async_complete(component->regmap); 875 } 876 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete); 877 878 /** 879 * snd_soc_component_test_bits - Test register for change 880 * @component: component 881 * @reg: Register to test 882 * @mask: Mask that specifies which bits to test 883 * @value: Value to test against 884 * 885 * Tests a register with a new value and checks if the new value is 886 * different from the old value. 887 * 888 * Return: 1 for change, otherwise 0. 889 */ 890 int snd_soc_component_test_bits(struct snd_soc_component *component, 891 unsigned int reg, unsigned int mask, unsigned int value) 892 { 893 unsigned int old, new; 894 895 old = snd_soc_component_read(component, reg); 896 new = (old & ~mask) | value; 897 return old != new; 898 } 899 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits); 900 901 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream) 902 { 903 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 904 struct snd_soc_component *component; 905 int i; 906 907 /* FIXME: use 1st pointer */ 908 for_each_rtd_components(rtd, i, component) 909 if (component->driver->pointer) 910 return component->driver->pointer(component, substream); 911 912 return 0; 913 } 914 915 static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd, 916 struct snd_soc_component *component) 917 { 918 struct snd_soc_dai *dai; 919 int i; 920 921 for_each_rtd_codec_dais(rtd, i, dai) { 922 if (dai->component == component) 923 return true; 924 } 925 926 return false; 927 } 928 929 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream, 930 snd_pcm_sframes_t *cpu_delay, 931 snd_pcm_sframes_t *codec_delay) 932 { 933 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 934 struct snd_soc_component *component; 935 snd_pcm_sframes_t delay; 936 int i; 937 938 /* 939 * We're looking for the delay through the full audio path so it needs to 940 * be the maximum of the Components doing transmit and the maximum of the 941 * Components doing receive (ie, all CPUs and all CODECs) rather than 942 * just the maximum of all Components. 943 */ 944 for_each_rtd_components(rtd, i, component) { 945 if (!component->driver->delay) 946 continue; 947 948 delay = component->driver->delay(component, substream); 949 950 if (snd_soc_component_is_codec_on_rtd(rtd, component)) 951 *codec_delay = max(*codec_delay, delay); 952 else 953 *cpu_delay = max(*cpu_delay, delay); 954 } 955 } 956 957 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream, 958 unsigned int cmd, void *arg) 959 { 960 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 961 struct snd_soc_component *component; 962 int i; 963 964 /* FIXME: use 1st ioctl */ 965 for_each_rtd_components(rtd, i, component) 966 if (component->driver->ioctl) 967 return soc_component_ret( 968 component, 969 component->driver->ioctl(component, 970 substream, cmd, arg)); 971 972 return snd_pcm_lib_ioctl(substream, cmd, arg); 973 } 974 975 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream) 976 { 977 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 978 struct snd_soc_component *component; 979 int i, ret; 980 981 for_each_rtd_components(rtd, i, component) { 982 if (component->driver->sync_stop) { 983 ret = component->driver->sync_stop(component, 984 substream); 985 if (ret < 0) 986 return soc_component_ret(component, ret); 987 } 988 } 989 990 return 0; 991 } 992 993 int snd_soc_pcm_component_copy(struct snd_pcm_substream *substream, 994 int channel, unsigned long pos, 995 struct iov_iter *iter, unsigned long bytes) 996 { 997 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 998 struct snd_soc_component *component; 999 int i; 1000 1001 /* FIXME. it returns 1st copy now */ 1002 for_each_rtd_components(rtd, i, component) 1003 if (component->driver->copy) 1004 return soc_component_ret(component, 1005 component->driver->copy(component, substream, 1006 channel, pos, iter, bytes)); 1007 1008 return -EINVAL; 1009 } 1010 1011 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream, 1012 unsigned long offset) 1013 { 1014 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1015 struct snd_soc_component *component; 1016 struct page *page; 1017 int i; 1018 1019 /* FIXME. it returns 1st page now */ 1020 for_each_rtd_components(rtd, i, component) { 1021 if (component->driver->page) { 1022 page = component->driver->page(component, 1023 substream, offset); 1024 if (page) 1025 return page; 1026 } 1027 } 1028 1029 return NULL; 1030 } 1031 1032 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream, 1033 struct vm_area_struct *vma) 1034 { 1035 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1036 struct snd_soc_component *component; 1037 int i; 1038 1039 /* FIXME. it returns 1st mmap now */ 1040 for_each_rtd_components(rtd, i, component) 1041 if (component->driver->mmap) 1042 return soc_component_ret( 1043 component, 1044 component->driver->mmap(component, 1045 substream, vma)); 1046 1047 return -EINVAL; 1048 } 1049 1050 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd) 1051 { 1052 struct snd_soc_component *component; 1053 int ret; 1054 int i; 1055 1056 for_each_rtd_components(rtd, i, component) { 1057 if (component->driver->pcm_new) { 1058 ret = component->driver->pcm_new(component, rtd); 1059 if (ret < 0) 1060 return soc_component_ret(component, ret); 1061 } 1062 } 1063 1064 return 0; 1065 } 1066 1067 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd) 1068 { 1069 struct snd_soc_component *component; 1070 int i; 1071 1072 if (!rtd->pcm) 1073 return; 1074 1075 for_each_rtd_components(rtd, i, component) 1076 if (component->driver->pcm_free) 1077 component->driver->pcm_free(component, rtd->pcm); 1078 } 1079 1080 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream) 1081 { 1082 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1083 struct snd_soc_component *component; 1084 int i, ret; 1085 1086 for_each_rtd_components(rtd, i, component) { 1087 if (component->driver->prepare) { 1088 ret = component->driver->prepare(component, substream); 1089 if (ret < 0) 1090 return soc_component_ret(component, ret); 1091 } 1092 } 1093 1094 return 0; 1095 } 1096 1097 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream, 1098 struct snd_pcm_hw_params *params) 1099 { 1100 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1101 struct snd_soc_component *component; 1102 int i, ret; 1103 1104 for_each_rtd_components(rtd, i, component) { 1105 if (component->driver->hw_params) { 1106 ret = component->driver->hw_params(component, 1107 substream, params); 1108 if (ret < 0) 1109 return soc_component_ret(component, ret); 1110 } 1111 /* mark substream if succeeded */ 1112 soc_component_mark_push(component, substream, hw_params); 1113 } 1114 1115 return 0; 1116 } 1117 1118 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream, 1119 int rollback) 1120 { 1121 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1122 struct snd_soc_component *component; 1123 int i, ret; 1124 1125 for_each_rtd_components(rtd, i, component) { 1126 if (rollback && !soc_component_mark_match(component, substream, hw_params)) 1127 continue; 1128 1129 if (component->driver->hw_free) { 1130 ret = component->driver->hw_free(component, substream); 1131 if (ret < 0) 1132 soc_component_ret(component, ret); 1133 } 1134 1135 /* remove marked substream */ 1136 soc_component_mark_pop(component, hw_params); 1137 } 1138 } 1139 1140 static int soc_component_trigger(struct snd_soc_component *component, 1141 struct snd_pcm_substream *substream, 1142 int cmd) 1143 { 1144 int ret = 0; 1145 1146 if (component->driver->trigger) 1147 ret = component->driver->trigger(component, substream, cmd); 1148 1149 return soc_component_ret(component, ret); 1150 } 1151 1152 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream, 1153 int cmd, int rollback) 1154 { 1155 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1156 struct snd_soc_component *component; 1157 int i, r, ret = 0; 1158 1159 switch (cmd) { 1160 case SNDRV_PCM_TRIGGER_START: 1161 case SNDRV_PCM_TRIGGER_RESUME: 1162 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1163 for_each_rtd_components(rtd, i, component) { 1164 ret = soc_component_trigger(component, substream, cmd); 1165 if (ret < 0) 1166 break; 1167 soc_component_mark_push(component, substream, trigger); 1168 } 1169 break; 1170 case SNDRV_PCM_TRIGGER_STOP: 1171 case SNDRV_PCM_TRIGGER_SUSPEND: 1172 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1173 for_each_rtd_components(rtd, i, component) { 1174 if (rollback && !soc_component_mark_match(component, substream, trigger)) 1175 continue; 1176 1177 r = soc_component_trigger(component, substream, cmd); 1178 if (r < 0) 1179 ret = r; /* use last ret */ 1180 soc_component_mark_pop(component, trigger); 1181 } 1182 } 1183 1184 return ret; 1185 } 1186 1187 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd, 1188 void *stream) 1189 { 1190 struct snd_soc_component *component; 1191 int i; 1192 1193 for_each_rtd_components(rtd, i, component) { 1194 int ret = pm_runtime_get_sync(component->dev); 1195 if (ret < 0 && ret != -EACCES) { 1196 pm_runtime_put_noidle(component->dev); 1197 return soc_component_ret(component, ret); 1198 } 1199 /* mark stream if succeeded */ 1200 soc_component_mark_push(component, stream, pm); 1201 } 1202 1203 return 0; 1204 } 1205 1206 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd, 1207 void *stream, int rollback) 1208 { 1209 struct snd_soc_component *component; 1210 int i; 1211 1212 for_each_rtd_components(rtd, i, component) { 1213 if (rollback && !soc_component_mark_match(component, stream, pm)) 1214 continue; 1215 1216 pm_runtime_put_autosuspend(component->dev); 1217 1218 /* remove marked stream */ 1219 soc_component_mark_pop(component, pm); 1220 } 1221 } 1222 1223 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream) 1224 { 1225 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1226 struct snd_soc_component *component; 1227 int i; 1228 1229 /* FIXME: use 1st pointer */ 1230 for_each_rtd_components(rtd, i, component) 1231 if (component->driver->ack) 1232 return component->driver->ack(component, substream); 1233 1234 return 0; 1235 } 1236