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 void snd_soc_component_remove(struct snd_soc_component *component) 314 { 315 if (component->driver->remove) 316 component->driver->remove(component); 317 } 318 319 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component, 320 struct device_node *ep) 321 { 322 int ret = -ENOTSUPP; 323 324 if (component->driver->of_xlate_dai_id) 325 ret = component->driver->of_xlate_dai_id(component, ep); 326 327 return soc_component_ret(component, ret); 328 } 329 330 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component, 331 const struct of_phandle_args *args, 332 const char **dai_name) 333 { 334 if (component->driver->of_xlate_dai_name) 335 return component->driver->of_xlate_dai_name(component, 336 args, dai_name); 337 /* 338 * Don't use soc_component_ret here because we may not want to report 339 * the error just yet. If a device has more than one component, the 340 * first may not match and we don't want spam the log with this. 341 */ 342 return -ENOTSUPP; 343 } 344 345 int snd_soc_component_regmap_val_bytes(struct snd_soc_component *component) 346 { 347 int val_bytes; 348 349 /* Errors are legitimate for non-integer byte multiples */ 350 351 if (!component->regmap) 352 return 0; 353 354 val_bytes = regmap_get_val_bytes(component->regmap); 355 if (val_bytes < 0) 356 return 0; 357 358 return val_bytes; 359 } 360 EXPORT_SYMBOL_GPL(snd_soc_component_regmap_val_bytes); 361 362 #ifdef CONFIG_REGMAP 363 364 /** 365 * snd_soc_component_init_regmap() - Initialize regmap instance for the 366 * component 367 * @component: The component for which to initialize the regmap instance 368 * @regmap: The regmap instance that should be used by the component 369 * 370 * This function allows deferred assignment of the regmap instance that is 371 * associated with the component. Only use this if the regmap instance is not 372 * yet ready when the component is registered. The function must also be called 373 * before the first IO attempt of the component. 374 */ 375 void snd_soc_component_init_regmap(struct snd_soc_component *component, 376 struct regmap *regmap) 377 { 378 component->regmap = regmap; 379 } 380 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); 381 382 /** 383 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the 384 * component 385 * @component: The component for which to de-initialize the regmap instance 386 * 387 * Calls regmap_exit() on the regmap instance associated to the component and 388 * removes the regmap instance from the component. 389 * 390 * This function should only be used if snd_soc_component_init_regmap() was used 391 * to initialize the regmap instance. 392 */ 393 void snd_soc_component_exit_regmap(struct snd_soc_component *component) 394 { 395 regmap_exit(component->regmap); 396 component->regmap = NULL; 397 } 398 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); 399 400 #endif 401 402 int snd_soc_component_compr_open(struct snd_soc_component *component, 403 struct snd_compr_stream *cstream) 404 { 405 int ret = 0; 406 407 if (component->driver->compress_ops && 408 component->driver->compress_ops->open) 409 ret = component->driver->compress_ops->open(component, cstream); 410 411 /* mark substream if succeeded */ 412 if (ret == 0) 413 soc_component_mark_push(component, cstream, compr_open); 414 415 return soc_component_ret(component, ret); 416 } 417 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open); 418 419 void snd_soc_component_compr_free(struct snd_soc_component *component, 420 struct snd_compr_stream *cstream, 421 int rollback) 422 { 423 if (rollback && !soc_component_mark_match(component, cstream, compr_open)) 424 return; 425 426 if (component->driver->compress_ops && 427 component->driver->compress_ops->free) 428 component->driver->compress_ops->free(component, cstream); 429 430 /* remove marked substream */ 431 soc_component_mark_pop(component, compr_open); 432 } 433 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free); 434 435 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd) 436 { 437 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 438 struct snd_soc_component *component; 439 int i, ret; 440 441 for_each_rtd_components(rtd, i, component) { 442 if (component->driver->compress_ops && 443 component->driver->compress_ops->trigger) { 444 ret = component->driver->compress_ops->trigger( 445 component, cstream, cmd); 446 if (ret < 0) 447 return soc_component_ret(component, ret); 448 } 449 } 450 451 return 0; 452 } 453 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger); 454 455 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream, 456 struct snd_compr_params *params) 457 { 458 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 459 struct snd_soc_component *component; 460 int i, ret; 461 462 for_each_rtd_components(rtd, i, component) { 463 if (component->driver->compress_ops && 464 component->driver->compress_ops->set_params) { 465 ret = component->driver->compress_ops->set_params( 466 component, cstream, params); 467 if (ret < 0) 468 return soc_component_ret(component, ret); 469 } 470 } 471 472 return 0; 473 } 474 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params); 475 476 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream, 477 struct snd_codec *params) 478 { 479 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 480 struct snd_soc_component *component; 481 int i, ret; 482 483 for_each_rtd_components(rtd, i, component) { 484 if (component->driver->compress_ops && 485 component->driver->compress_ops->get_params) { 486 ret = component->driver->compress_ops->get_params( 487 component, cstream, params); 488 return soc_component_ret(component, ret); 489 } 490 } 491 492 return 0; 493 } 494 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params); 495 496 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream, 497 struct snd_compr_caps *caps) 498 { 499 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 500 struct snd_soc_component *component; 501 int i, ret = 0; 502 503 snd_soc_dpcm_mutex_lock(rtd); 504 505 for_each_rtd_components(rtd, i, component) { 506 if (component->driver->compress_ops && 507 component->driver->compress_ops->get_caps) { 508 ret = component->driver->compress_ops->get_caps( 509 component, cstream, caps); 510 break; 511 } 512 } 513 514 snd_soc_dpcm_mutex_unlock(rtd); 515 516 return soc_component_ret(component, ret); 517 } 518 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps); 519 520 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream, 521 struct snd_compr_codec_caps *codec) 522 { 523 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 524 struct snd_soc_component *component; 525 int i, ret = 0; 526 527 snd_soc_dpcm_mutex_lock(rtd); 528 529 for_each_rtd_components(rtd, i, component) { 530 if (component->driver->compress_ops && 531 component->driver->compress_ops->get_codec_caps) { 532 ret = component->driver->compress_ops->get_codec_caps( 533 component, cstream, codec); 534 break; 535 } 536 } 537 538 snd_soc_dpcm_mutex_unlock(rtd); 539 540 return soc_component_ret(component, ret); 541 } 542 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps); 543 544 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes) 545 { 546 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 547 struct snd_soc_component *component; 548 int i, ret; 549 550 for_each_rtd_components(rtd, i, component) { 551 if (component->driver->compress_ops && 552 component->driver->compress_ops->ack) { 553 ret = component->driver->compress_ops->ack( 554 component, cstream, bytes); 555 if (ret < 0) 556 return soc_component_ret(component, ret); 557 } 558 } 559 560 return 0; 561 } 562 EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack); 563 564 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream, 565 struct snd_compr_tstamp64 *tstamp) 566 { 567 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 568 struct snd_soc_component *component; 569 int i, ret; 570 571 for_each_rtd_components(rtd, i, component) { 572 if (component->driver->compress_ops && 573 component->driver->compress_ops->pointer) { 574 ret = component->driver->compress_ops->pointer( 575 component, cstream, tstamp); 576 return soc_component_ret(component, ret); 577 } 578 } 579 580 return 0; 581 } 582 EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer); 583 584 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream, 585 char __user *buf, size_t count) 586 { 587 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 588 struct snd_soc_component *component; 589 int i, ret = 0; 590 591 snd_soc_dpcm_mutex_lock(rtd); 592 593 for_each_rtd_components(rtd, i, component) { 594 if (component->driver->compress_ops && 595 component->driver->compress_ops->copy) { 596 ret = component->driver->compress_ops->copy( 597 component, cstream, buf, count); 598 break; 599 } 600 } 601 602 snd_soc_dpcm_mutex_unlock(rtd); 603 604 return soc_component_ret(component, ret); 605 } 606 EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy); 607 608 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream, 609 struct snd_compr_metadata *metadata) 610 { 611 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 612 struct snd_soc_component *component; 613 int i, ret; 614 615 for_each_rtd_components(rtd, i, component) { 616 if (component->driver->compress_ops && 617 component->driver->compress_ops->set_metadata) { 618 ret = component->driver->compress_ops->set_metadata( 619 component, cstream, metadata); 620 if (ret < 0) 621 return soc_component_ret(component, ret); 622 } 623 } 624 625 return 0; 626 } 627 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata); 628 629 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream, 630 struct snd_compr_metadata *metadata) 631 { 632 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 633 struct snd_soc_component *component; 634 int i, ret; 635 636 for_each_rtd_components(rtd, i, component) { 637 if (component->driver->compress_ops && 638 component->driver->compress_ops->get_metadata) { 639 ret = component->driver->compress_ops->get_metadata( 640 component, cstream, metadata); 641 return soc_component_ret(component, ret); 642 } 643 } 644 645 return 0; 646 } 647 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata); 648 649 static unsigned int soc_component_read_no_lock( 650 struct snd_soc_component *component, 651 unsigned int reg) 652 { 653 int ret; 654 unsigned int val = 0; 655 656 if (component->regmap) 657 ret = regmap_read(component->regmap, reg, &val); 658 else if (component->driver->read) { 659 ret = 0; 660 val = component->driver->read(component, reg); 661 } 662 else 663 ret = -EIO; 664 665 if (ret < 0) 666 return soc_component_ret_reg_rw(component, ret, reg); 667 668 return val; 669 } 670 671 /** 672 * snd_soc_component_read() - Read register value 673 * @component: Component to read from 674 * @reg: Register to read 675 * 676 * Return: read value 677 */ 678 unsigned int snd_soc_component_read(struct snd_soc_component *component, 679 unsigned int reg) 680 { 681 unsigned int val; 682 683 mutex_lock(&component->io_mutex); 684 val = soc_component_read_no_lock(component, reg); 685 mutex_unlock(&component->io_mutex); 686 687 return val; 688 } 689 EXPORT_SYMBOL_GPL(snd_soc_component_read); 690 691 static int soc_component_write_no_lock( 692 struct snd_soc_component *component, 693 unsigned int reg, unsigned int val) 694 { 695 int ret = -EIO; 696 697 if (component->regmap) 698 ret = regmap_write(component->regmap, reg, val); 699 else if (component->driver->write) 700 ret = component->driver->write(component, reg, val); 701 702 return soc_component_ret_reg_rw(component, ret, reg); 703 } 704 705 /** 706 * snd_soc_component_write() - Write register value 707 * @component: Component to write to 708 * @reg: Register to write 709 * @val: Value to write to the register 710 * 711 * Return: 0 on success, a negative error code otherwise. 712 */ 713 int snd_soc_component_write(struct snd_soc_component *component, 714 unsigned int reg, unsigned int val) 715 { 716 int ret; 717 718 mutex_lock(&component->io_mutex); 719 ret = soc_component_write_no_lock(component, reg, val); 720 mutex_unlock(&component->io_mutex); 721 722 return ret; 723 } 724 EXPORT_SYMBOL_GPL(snd_soc_component_write); 725 726 static int snd_soc_component_update_bits_legacy( 727 struct snd_soc_component *component, unsigned int reg, 728 unsigned int mask, unsigned int val, bool *change) 729 { 730 unsigned int old, new; 731 int ret = 0; 732 733 mutex_lock(&component->io_mutex); 734 735 old = soc_component_read_no_lock(component, reg); 736 737 new = (old & ~mask) | (val & mask); 738 *change = old != new; 739 if (*change) 740 ret = soc_component_write_no_lock(component, reg, new); 741 742 mutex_unlock(&component->io_mutex); 743 744 return soc_component_ret_reg_rw(component, ret, reg); 745 } 746 747 /** 748 * snd_soc_component_update_bits() - Perform read/modify/write cycle 749 * @component: Component to update 750 * @reg: Register to update 751 * @mask: Mask that specifies which bits to update 752 * @val: New value for the bits specified by mask 753 * 754 * Return: 1 if the operation was successful and the value of the register 755 * changed, 0 if the operation was successful, but the value did not change. 756 * Returns a negative error code otherwise. 757 */ 758 int snd_soc_component_update_bits(struct snd_soc_component *component, 759 unsigned int reg, unsigned int mask, unsigned int val) 760 { 761 bool change; 762 int ret; 763 764 if (component->regmap) 765 ret = regmap_update_bits_check(component->regmap, reg, mask, 766 val, &change); 767 else 768 ret = snd_soc_component_update_bits_legacy(component, reg, 769 mask, val, &change); 770 771 if (ret < 0) 772 return soc_component_ret_reg_rw(component, ret, reg); 773 return change; 774 } 775 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits); 776 777 /** 778 * snd_soc_component_update_bits_async() - Perform asynchronous 779 * read/modify/write cycle 780 * @component: Component to update 781 * @reg: Register to update 782 * @mask: Mask that specifies which bits to update 783 * @val: New value for the bits specified by mask 784 * 785 * This function is similar to snd_soc_component_update_bits(), but the update 786 * operation is scheduled asynchronously. This means it may not be completed 787 * when the function returns. To make sure that all scheduled updates have been 788 * completed snd_soc_component_async_complete() must be called. 789 * 790 * Return: 1 if the operation was successful and the value of the register 791 * changed, 0 if the operation was successful, but the value did not change. 792 * Returns a negative error code otherwise. 793 */ 794 int snd_soc_component_update_bits_async(struct snd_soc_component *component, 795 unsigned int reg, unsigned int mask, unsigned int val) 796 { 797 bool change; 798 int ret; 799 800 if (component->regmap) 801 ret = regmap_update_bits_check_async(component->regmap, reg, 802 mask, val, &change); 803 else 804 ret = snd_soc_component_update_bits_legacy(component, reg, 805 mask, val, &change); 806 807 if (ret < 0) 808 return soc_component_ret_reg_rw(component, ret, reg); 809 return change; 810 } 811 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async); 812 813 /** 814 * snd_soc_component_read_field() - Read register field value 815 * @component: Component to read from 816 * @reg: Register to read 817 * @mask: mask of the register field 818 * 819 * Return: read value of register field. 820 */ 821 unsigned int snd_soc_component_read_field(struct snd_soc_component *component, 822 unsigned int reg, unsigned int mask) 823 { 824 unsigned int val; 825 826 val = snd_soc_component_read(component, reg); 827 828 val = (val & mask) >> soc_component_field_shift(component, mask); 829 830 return val; 831 } 832 EXPORT_SYMBOL_GPL(snd_soc_component_read_field); 833 834 /** 835 * snd_soc_component_write_field() - write to register field 836 * @component: Component to write to 837 * @reg: Register to write 838 * @mask: mask of the register field to update 839 * @val: value of the field to write 840 * 841 * Return: 1 for change, otherwise 0. 842 */ 843 int snd_soc_component_write_field(struct snd_soc_component *component, 844 unsigned int reg, unsigned int mask, 845 unsigned int val) 846 { 847 848 val = (val << soc_component_field_shift(component, mask)) & mask; 849 850 return snd_soc_component_update_bits(component, reg, mask, val); 851 } 852 EXPORT_SYMBOL_GPL(snd_soc_component_write_field); 853 854 /** 855 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed 856 * @component: Component for which to wait 857 * 858 * This function blocks until all asynchronous I/O which has previously been 859 * scheduled using snd_soc_component_update_bits_async() has completed. 860 */ 861 void snd_soc_component_async_complete(struct snd_soc_component *component) 862 { 863 if (component->regmap) 864 regmap_async_complete(component->regmap); 865 } 866 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete); 867 868 /** 869 * snd_soc_component_test_bits - Test register for change 870 * @component: component 871 * @reg: Register to test 872 * @mask: Mask that specifies which bits to test 873 * @value: Value to test against 874 * 875 * Tests a register with a new value and checks if the new value is 876 * different from the old value. 877 * 878 * Return: 1 for change, otherwise 0. 879 */ 880 int snd_soc_component_test_bits(struct snd_soc_component *component, 881 unsigned int reg, unsigned int mask, unsigned int value) 882 { 883 unsigned int old, new; 884 885 old = snd_soc_component_read(component, reg); 886 new = (old & ~mask) | value; 887 return old != new; 888 } 889 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits); 890 891 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream) 892 { 893 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 894 struct snd_soc_component *component; 895 int i; 896 897 /* FIXME: use 1st pointer */ 898 for_each_rtd_components(rtd, i, component) 899 if (component->driver->pointer) 900 return component->driver->pointer(component, substream); 901 902 return 0; 903 } 904 905 static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd, 906 struct snd_soc_component *component) 907 { 908 struct snd_soc_dai *dai; 909 int i; 910 911 for_each_rtd_codec_dais(rtd, i, dai) { 912 if (dai->component == component) 913 return true; 914 } 915 916 return false; 917 } 918 919 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream, 920 snd_pcm_sframes_t *cpu_delay, 921 snd_pcm_sframes_t *codec_delay) 922 { 923 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 924 struct snd_soc_component *component; 925 snd_pcm_sframes_t delay; 926 int i; 927 928 /* 929 * We're looking for the delay through the full audio path so it needs to 930 * be the maximum of the Components doing transmit and the maximum of the 931 * Components doing receive (ie, all CPUs and all CODECs) rather than 932 * just the maximum of all Components. 933 */ 934 for_each_rtd_components(rtd, i, component) { 935 if (!component->driver->delay) 936 continue; 937 938 delay = component->driver->delay(component, substream); 939 940 if (snd_soc_component_is_codec_on_rtd(rtd, component)) 941 *codec_delay = max(*codec_delay, delay); 942 else 943 *cpu_delay = max(*cpu_delay, delay); 944 } 945 } 946 947 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream, 948 unsigned int cmd, void *arg) 949 { 950 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 951 struct snd_soc_component *component; 952 int i; 953 954 /* FIXME: use 1st ioctl */ 955 for_each_rtd_components(rtd, i, component) 956 if (component->driver->ioctl) 957 return soc_component_ret( 958 component, 959 component->driver->ioctl(component, 960 substream, cmd, arg)); 961 962 return snd_pcm_lib_ioctl(substream, cmd, arg); 963 } 964 965 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream) 966 { 967 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 968 struct snd_soc_component *component; 969 int i, ret; 970 971 for_each_rtd_components(rtd, i, component) { 972 if (component->driver->sync_stop) { 973 ret = component->driver->sync_stop(component, 974 substream); 975 if (ret < 0) 976 return soc_component_ret(component, ret); 977 } 978 } 979 980 return 0; 981 } 982 983 int snd_soc_pcm_component_copy(struct snd_pcm_substream *substream, 984 int channel, unsigned long pos, 985 struct iov_iter *iter, unsigned long bytes) 986 { 987 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 988 struct snd_soc_component *component; 989 int i; 990 991 /* FIXME. it returns 1st copy now */ 992 for_each_rtd_components(rtd, i, component) 993 if (component->driver->copy) 994 return soc_component_ret(component, 995 component->driver->copy(component, substream, 996 channel, pos, iter, bytes)); 997 998 return -EINVAL; 999 } 1000 1001 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream, 1002 unsigned long offset) 1003 { 1004 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1005 struct snd_soc_component *component; 1006 struct page *page; 1007 int i; 1008 1009 /* FIXME. it returns 1st page now */ 1010 for_each_rtd_components(rtd, i, component) { 1011 if (component->driver->page) { 1012 page = component->driver->page(component, 1013 substream, offset); 1014 if (page) 1015 return page; 1016 } 1017 } 1018 1019 return NULL; 1020 } 1021 1022 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream, 1023 struct vm_area_struct *vma) 1024 { 1025 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1026 struct snd_soc_component *component; 1027 int i; 1028 1029 /* FIXME. it returns 1st mmap now */ 1030 for_each_rtd_components(rtd, i, component) 1031 if (component->driver->mmap) 1032 return soc_component_ret( 1033 component, 1034 component->driver->mmap(component, 1035 substream, vma)); 1036 1037 return -EINVAL; 1038 } 1039 1040 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd) 1041 { 1042 struct snd_soc_component *component; 1043 int ret; 1044 int i; 1045 1046 for_each_rtd_components(rtd, i, component) { 1047 if (component->driver->pcm_new) { 1048 ret = component->driver->pcm_new(component, rtd); 1049 if (ret < 0) 1050 return soc_component_ret(component, ret); 1051 } 1052 } 1053 1054 return 0; 1055 } 1056 1057 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd) 1058 { 1059 struct snd_soc_component *component; 1060 int i; 1061 1062 if (!rtd->pcm) 1063 return; 1064 1065 for_each_rtd_components(rtd, i, component) 1066 if (component->driver->pcm_free) 1067 component->driver->pcm_free(component, rtd->pcm); 1068 } 1069 1070 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream) 1071 { 1072 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1073 struct snd_soc_component *component; 1074 int i, ret; 1075 1076 for_each_rtd_components(rtd, i, component) { 1077 if (component->driver->prepare) { 1078 ret = component->driver->prepare(component, substream); 1079 if (ret < 0) 1080 return soc_component_ret(component, ret); 1081 } 1082 } 1083 1084 return 0; 1085 } 1086 1087 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream, 1088 struct snd_pcm_hw_params *params) 1089 { 1090 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1091 struct snd_soc_component *component; 1092 int i, ret; 1093 1094 for_each_rtd_components(rtd, i, component) { 1095 if (component->driver->hw_params) { 1096 ret = component->driver->hw_params(component, 1097 substream, params); 1098 if (ret < 0) 1099 return soc_component_ret(component, ret); 1100 } 1101 /* mark substream if succeeded */ 1102 soc_component_mark_push(component, substream, hw_params); 1103 } 1104 1105 return 0; 1106 } 1107 1108 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream, 1109 int rollback) 1110 { 1111 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1112 struct snd_soc_component *component; 1113 int i, ret; 1114 1115 for_each_rtd_components(rtd, i, component) { 1116 if (rollback && !soc_component_mark_match(component, substream, hw_params)) 1117 continue; 1118 1119 if (component->driver->hw_free) { 1120 ret = component->driver->hw_free(component, substream); 1121 if (ret < 0) 1122 soc_component_ret(component, ret); 1123 } 1124 1125 /* remove marked substream */ 1126 soc_component_mark_pop(component, hw_params); 1127 } 1128 } 1129 1130 static int soc_component_trigger(struct snd_soc_component *component, 1131 struct snd_pcm_substream *substream, 1132 int cmd) 1133 { 1134 int ret = 0; 1135 1136 if (component->driver->trigger) 1137 ret = component->driver->trigger(component, substream, cmd); 1138 1139 return soc_component_ret(component, ret); 1140 } 1141 1142 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream, 1143 int cmd, int rollback) 1144 { 1145 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1146 struct snd_soc_component *component; 1147 int i, r, ret = 0; 1148 1149 switch (cmd) { 1150 case SNDRV_PCM_TRIGGER_START: 1151 case SNDRV_PCM_TRIGGER_RESUME: 1152 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1153 for_each_rtd_components(rtd, i, component) { 1154 ret = soc_component_trigger(component, substream, cmd); 1155 if (ret < 0) 1156 break; 1157 soc_component_mark_push(component, substream, trigger); 1158 } 1159 break; 1160 case SNDRV_PCM_TRIGGER_STOP: 1161 case SNDRV_PCM_TRIGGER_SUSPEND: 1162 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1163 for_each_rtd_components(rtd, i, component) { 1164 if (rollback && !soc_component_mark_match(component, substream, trigger)) 1165 continue; 1166 1167 r = soc_component_trigger(component, substream, cmd); 1168 if (r < 0) 1169 ret = r; /* use last ret */ 1170 soc_component_mark_pop(component, trigger); 1171 } 1172 } 1173 1174 return ret; 1175 } 1176 1177 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd, 1178 void *stream) 1179 { 1180 struct snd_soc_component *component; 1181 int i; 1182 1183 for_each_rtd_components(rtd, i, component) { 1184 int ret = pm_runtime_get_sync(component->dev); 1185 if (ret < 0 && ret != -EACCES) { 1186 pm_runtime_put_noidle(component->dev); 1187 return soc_component_ret(component, ret); 1188 } 1189 /* mark stream if succeeded */ 1190 soc_component_mark_push(component, stream, pm); 1191 } 1192 1193 return 0; 1194 } 1195 1196 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd, 1197 void *stream, int rollback) 1198 { 1199 struct snd_soc_component *component; 1200 int i; 1201 1202 for_each_rtd_components(rtd, i, component) { 1203 if (rollback && !soc_component_mark_match(component, stream, pm)) 1204 continue; 1205 1206 pm_runtime_put_autosuspend(component->dev); 1207 1208 /* remove marked stream */ 1209 soc_component_mark_pop(component, pm); 1210 } 1211 } 1212 1213 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream) 1214 { 1215 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1216 struct snd_soc_component *component; 1217 int i; 1218 1219 /* FIXME: use 1st pointer */ 1220 for_each_rtd_components(rtd, i, component) 1221 if (component->driver->ack) 1222 return component->driver->ack(component, substream); 1223 1224 return 0; 1225 } 1226