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