Linux Audio

Check our new training course

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