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