Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / soc / soc-component.c
blob760523382f3c56dbd7f2b6d898309f83a0cb1534
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>
11 #include <linux/module.h>
12 #include <linux/pm_runtime.h>
13 #include <sound/soc.h>
15 #define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
16 static inline int _soc_component_ret(struct snd_soc_component *component,
17 const char *func, int ret)
19 /* Positive/Zero values are not errors */
20 if (ret >= 0)
21 return ret;
23 /* Negative values might be errors */
24 switch (ret) {
25 case -EPROBE_DEFER:
26 case -ENOTSUPP:
27 break;
28 default:
29 dev_err(component->dev,
30 "ASoC: error at %s on %s: %d\n",
31 func, component->name, ret);
34 return ret;
38 * We might want to check substream by using list.
39 * In such case, we can update these macros.
41 #define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
42 #define soc_component_mark_pop(component, substream, tgt) ((component)->mark_##tgt = NULL)
43 #define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
45 void snd_soc_component_set_aux(struct snd_soc_component *component,
46 struct snd_soc_aux_dev *aux)
48 component->init = (aux) ? aux->init : NULL;
51 int snd_soc_component_init(struct snd_soc_component *component)
53 int ret = 0;
55 if (component->init)
56 ret = component->init(component);
58 return soc_component_ret(component, ret);
61 /**
62 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
63 * @component: COMPONENT
64 * @clk_id: DAI specific clock ID
65 * @source: Source for the clock
66 * @freq: new clock frequency in Hz
67 * @dir: new clock direction - input/output.
69 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
71 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
72 int clk_id, int source, unsigned int freq,
73 int dir)
75 int ret = -ENOTSUPP;
77 if (component->driver->set_sysclk)
78 ret = component->driver->set_sysclk(component, clk_id, source,
79 freq, dir);
81 return soc_component_ret(component, ret);
83 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
86 * snd_soc_component_set_pll - configure component PLL.
87 * @component: COMPONENT
88 * @pll_id: DAI specific PLL ID
89 * @source: DAI specific source for the PLL
90 * @freq_in: PLL input clock frequency in Hz
91 * @freq_out: requested PLL output clock frequency in Hz
93 * Configures and enables PLL to generate output clock based on input clock.
95 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
96 int source, unsigned int freq_in,
97 unsigned int freq_out)
99 int ret = -EINVAL;
101 if (component->driver->set_pll)
102 ret = component->driver->set_pll(component, pll_id, source,
103 freq_in, freq_out);
105 return soc_component_ret(component, ret);
107 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
109 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
110 enum snd_soc_dapm_type type, int subseq)
112 if (component->driver->seq_notifier)
113 component->driver->seq_notifier(component, type, subseq);
116 int snd_soc_component_stream_event(struct snd_soc_component *component,
117 int event)
119 int ret = 0;
121 if (component->driver->stream_event)
122 ret = component->driver->stream_event(component, event);
124 return soc_component_ret(component, ret);
127 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
128 enum snd_soc_bias_level level)
130 int ret = 0;
132 if (component->driver->set_bias_level)
133 ret = component->driver->set_bias_level(component, level);
135 return soc_component_ret(component, ret);
138 static int soc_component_pin(struct snd_soc_component *component,
139 const char *pin,
140 int (*pin_func)(struct snd_soc_dapm_context *dapm,
141 const char *pin))
143 struct snd_soc_dapm_context *dapm =
144 snd_soc_component_get_dapm(component);
145 char *full_name;
146 int ret;
148 if (!component->name_prefix) {
149 ret = pin_func(dapm, pin);
150 goto end;
153 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
154 if (!full_name) {
155 ret = -ENOMEM;
156 goto end;
159 ret = pin_func(dapm, full_name);
160 kfree(full_name);
161 end:
162 return soc_component_ret(component, ret);
165 int snd_soc_component_enable_pin(struct snd_soc_component *component,
166 const char *pin)
168 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
170 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
172 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
173 const char *pin)
175 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
177 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
179 int snd_soc_component_disable_pin(struct snd_soc_component *component,
180 const char *pin)
182 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
184 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
186 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
187 const char *pin)
189 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
191 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
193 int snd_soc_component_nc_pin(struct snd_soc_component *component,
194 const char *pin)
196 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
198 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
200 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
201 const char *pin)
203 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
205 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
207 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
208 const char *pin)
210 return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
212 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
214 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
215 const char *pin)
217 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
219 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
221 int snd_soc_component_force_enable_pin_unlocked(
222 struct snd_soc_component *component,
223 const char *pin)
225 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
227 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
230 * snd_soc_component_set_jack - configure component jack.
231 * @component: COMPONENTs
232 * @jack: structure to use for the jack
233 * @data: can be used if codec driver need extra data for configuring jack
235 * Configures and enables jack detection function.
237 int snd_soc_component_set_jack(struct snd_soc_component *component,
238 struct snd_soc_jack *jack, void *data)
240 int ret = -ENOTSUPP;
242 if (component->driver->set_jack)
243 ret = component->driver->set_jack(component, jack, data);
245 return soc_component_ret(component, ret);
247 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
249 int snd_soc_component_module_get(struct snd_soc_component *component,
250 struct snd_pcm_substream *substream,
251 int upon_open)
253 int ret = 0;
255 if (component->driver->module_get_upon_open == !!upon_open &&
256 !try_module_get(component->dev->driver->owner))
257 ret = -ENODEV;
259 /* mark substream if succeeded */
260 if (ret == 0)
261 soc_component_mark_push(component, substream, module);
263 return soc_component_ret(component, ret);
266 void snd_soc_component_module_put(struct snd_soc_component *component,
267 struct snd_pcm_substream *substream,
268 int upon_open, int rollback)
270 if (rollback && !soc_component_mark_match(component, substream, module))
271 return;
273 if (component->driver->module_get_upon_open == !!upon_open)
274 module_put(component->dev->driver->owner);
276 /* remove marked substream */
277 soc_component_mark_pop(component, substream, module);
280 int snd_soc_component_open(struct snd_soc_component *component,
281 struct snd_pcm_substream *substream)
283 int ret = 0;
285 if (component->driver->open)
286 ret = component->driver->open(component, substream);
288 /* mark substream if succeeded */
289 if (ret == 0)
290 soc_component_mark_push(component, substream, open);
292 return soc_component_ret(component, ret);
295 int snd_soc_component_close(struct snd_soc_component *component,
296 struct snd_pcm_substream *substream,
297 int rollback)
299 int ret = 0;
301 if (rollback && !soc_component_mark_match(component, substream, open))
302 return 0;
304 if (component->driver->close)
305 ret = component->driver->close(component, substream);
307 /* remove marked substream */
308 soc_component_mark_pop(component, substream, open);
310 return soc_component_ret(component, ret);
313 void snd_soc_component_suspend(struct snd_soc_component *component)
315 if (component->driver->suspend)
316 component->driver->suspend(component);
317 component->suspended = 1;
320 void snd_soc_component_resume(struct snd_soc_component *component)
322 if (component->driver->resume)
323 component->driver->resume(component);
324 component->suspended = 0;
327 int snd_soc_component_is_suspended(struct snd_soc_component *component)
329 return component->suspended;
332 int snd_soc_component_probe(struct snd_soc_component *component)
334 int ret = 0;
336 if (component->driver->probe)
337 ret = component->driver->probe(component);
339 return soc_component_ret(component, ret);
342 void snd_soc_component_remove(struct snd_soc_component *component)
344 if (component->driver->remove)
345 component->driver->remove(component);
348 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
349 struct device_node *ep)
351 int ret = -ENOTSUPP;
353 if (component->driver->of_xlate_dai_id)
354 ret = component->driver->of_xlate_dai_id(component, ep);
356 return soc_component_ret(component, ret);
359 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
360 struct of_phandle_args *args,
361 const char **dai_name)
363 if (component->driver->of_xlate_dai_name)
364 return component->driver->of_xlate_dai_name(component,
365 args, dai_name);
367 * Don't use soc_component_ret here because we may not want to report
368 * the error just yet. If a device has more than one component, the
369 * first may not match and we don't want spam the log with this.
371 return -ENOTSUPP;
374 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
376 int val_bytes = regmap_get_val_bytes(component->regmap);
378 /* Errors are legitimate for non-integer byte multiples */
379 if (val_bytes > 0)
380 component->val_bytes = val_bytes;
383 #ifdef CONFIG_REGMAP
386 * snd_soc_component_init_regmap() - Initialize regmap instance for the
387 * component
388 * @component: The component for which to initialize the regmap instance
389 * @regmap: The regmap instance that should be used by the component
391 * This function allows deferred assignment of the regmap instance that is
392 * associated with the component. Only use this if the regmap instance is not
393 * yet ready when the component is registered. The function must also be called
394 * before the first IO attempt of the component.
396 void snd_soc_component_init_regmap(struct snd_soc_component *component,
397 struct regmap *regmap)
399 component->regmap = regmap;
400 snd_soc_component_setup_regmap(component);
402 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
405 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
406 * component
407 * @component: The component for which to de-initialize the regmap instance
409 * Calls regmap_exit() on the regmap instance associated to the component and
410 * removes the regmap instance from the component.
412 * This function should only be used if snd_soc_component_init_regmap() was used
413 * to initialize the regmap instance.
415 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
417 regmap_exit(component->regmap);
418 component->regmap = NULL;
420 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
422 #endif
424 int snd_soc_component_compr_open(struct snd_compr_stream *cstream)
426 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
427 struct snd_soc_component *component;
428 int i, ret;
430 for_each_rtd_components(rtd, i, component) {
431 if (component->driver->compress_ops &&
432 component->driver->compress_ops->open) {
433 ret = component->driver->compress_ops->open(component, cstream);
434 if (ret < 0)
435 return soc_component_ret(component, ret);
437 soc_component_mark_push(component, cstream, compr_open);
440 return 0;
442 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
444 void snd_soc_component_compr_free(struct snd_compr_stream *cstream,
445 int rollback)
447 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
448 struct snd_soc_component *component;
449 int i;
451 for_each_rtd_components(rtd, i, component) {
452 if (rollback && !soc_component_mark_match(component, cstream, compr_open))
453 continue;
455 if (component->driver->compress_ops &&
456 component->driver->compress_ops->free)
457 component->driver->compress_ops->free(component, cstream);
459 soc_component_mark_pop(component, cstream, compr_open);
462 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
464 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
466 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
467 struct snd_soc_component *component;
468 int i, ret;
470 for_each_rtd_components(rtd, i, component) {
471 if (component->driver->compress_ops &&
472 component->driver->compress_ops->trigger) {
473 ret = component->driver->compress_ops->trigger(
474 component, cstream, cmd);
475 if (ret < 0)
476 return soc_component_ret(component, ret);
480 return 0;
482 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
484 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
485 struct snd_compr_params *params)
487 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
488 struct snd_soc_component *component;
489 int i, ret;
491 for_each_rtd_components(rtd, i, component) {
492 if (component->driver->compress_ops &&
493 component->driver->compress_ops->set_params) {
494 ret = component->driver->compress_ops->set_params(
495 component, cstream, params);
496 if (ret < 0)
497 return soc_component_ret(component, ret);
501 return 0;
503 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
505 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
506 struct snd_codec *params)
508 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
509 struct snd_soc_component *component;
510 int i, ret;
512 for_each_rtd_components(rtd, i, component) {
513 if (component->driver->compress_ops &&
514 component->driver->compress_ops->get_params) {
515 ret = component->driver->compress_ops->get_params(
516 component, cstream, params);
517 return soc_component_ret(component, ret);
521 return 0;
523 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
525 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
526 struct snd_compr_caps *caps)
528 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
529 struct snd_soc_component *component;
530 int i, ret = 0;
532 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
534 for_each_rtd_components(rtd, i, component) {
535 if (component->driver->compress_ops &&
536 component->driver->compress_ops->get_caps) {
537 ret = component->driver->compress_ops->get_caps(
538 component, cstream, caps);
539 break;
543 mutex_unlock(&rtd->card->pcm_mutex);
545 return soc_component_ret(component, ret);
547 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
549 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
550 struct snd_compr_codec_caps *codec)
552 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
553 struct snd_soc_component *component;
554 int i, ret = 0;
556 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
558 for_each_rtd_components(rtd, i, component) {
559 if (component->driver->compress_ops &&
560 component->driver->compress_ops->get_codec_caps) {
561 ret = component->driver->compress_ops->get_codec_caps(
562 component, cstream, codec);
563 break;
567 mutex_unlock(&rtd->card->pcm_mutex);
569 return soc_component_ret(component, ret);
571 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
573 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
575 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
576 struct snd_soc_component *component;
577 int i, ret;
579 for_each_rtd_components(rtd, i, component) {
580 if (component->driver->compress_ops &&
581 component->driver->compress_ops->ack) {
582 ret = component->driver->compress_ops->ack(
583 component, cstream, bytes);
584 if (ret < 0)
585 return soc_component_ret(component, ret);
589 return 0;
591 EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
593 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
594 struct snd_compr_tstamp *tstamp)
596 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
597 struct snd_soc_component *component;
598 int i, ret;
600 for_each_rtd_components(rtd, i, component) {
601 if (component->driver->compress_ops &&
602 component->driver->compress_ops->pointer) {
603 ret = component->driver->compress_ops->pointer(
604 component, cstream, tstamp);
605 return soc_component_ret(component, ret);
609 return 0;
611 EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
613 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
614 char __user *buf, size_t count)
616 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
617 struct snd_soc_component *component;
618 int i, ret = 0;
620 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
622 for_each_rtd_components(rtd, i, component) {
623 if (component->driver->compress_ops &&
624 component->driver->compress_ops->copy) {
625 ret = component->driver->compress_ops->copy(
626 component, cstream, buf, count);
627 break;
631 mutex_unlock(&rtd->card->pcm_mutex);
633 return soc_component_ret(component, ret);
635 EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
637 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
638 struct snd_compr_metadata *metadata)
640 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
641 struct snd_soc_component *component;
642 int i, ret;
644 for_each_rtd_components(rtd, i, component) {
645 if (component->driver->compress_ops &&
646 component->driver->compress_ops->set_metadata) {
647 ret = component->driver->compress_ops->set_metadata(
648 component, cstream, metadata);
649 if (ret < 0)
650 return soc_component_ret(component, ret);
654 return 0;
656 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
658 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
659 struct snd_compr_metadata *metadata)
661 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
662 struct snd_soc_component *component;
663 int i, ret;
665 for_each_rtd_components(rtd, i, component) {
666 if (component->driver->compress_ops &&
667 component->driver->compress_ops->get_metadata) {
668 ret = component->driver->compress_ops->get_metadata(
669 component, cstream, metadata);
670 return soc_component_ret(component, ret);
674 return 0;
676 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
678 static unsigned int soc_component_read_no_lock(
679 struct snd_soc_component *component,
680 unsigned int reg)
682 int ret;
683 unsigned int val = 0;
685 if (component->regmap)
686 ret = regmap_read(component->regmap, reg, &val);
687 else if (component->driver->read) {
688 ret = 0;
689 val = component->driver->read(component, reg);
691 else
692 ret = -EIO;
694 if (ret < 0)
695 return soc_component_ret(component, ret);
697 return val;
701 * snd_soc_component_read() - Read register value
702 * @component: Component to read from
703 * @reg: Register to read
705 * Return: read value
707 unsigned int snd_soc_component_read(struct snd_soc_component *component,
708 unsigned int reg)
710 unsigned int val;
712 mutex_lock(&component->io_mutex);
713 val = soc_component_read_no_lock(component, reg);
714 mutex_unlock(&component->io_mutex);
716 return val;
718 EXPORT_SYMBOL_GPL(snd_soc_component_read);
720 static int soc_component_write_no_lock(
721 struct snd_soc_component *component,
722 unsigned int reg, unsigned int val)
724 int ret = -EIO;
726 if (component->regmap)
727 ret = regmap_write(component->regmap, reg, val);
728 else if (component->driver->write)
729 ret = component->driver->write(component, reg, val);
731 return soc_component_ret(component, ret);
735 * snd_soc_component_write() - Write register value
736 * @component: Component to write to
737 * @reg: Register to write
738 * @val: Value to write to the register
740 * Return: 0 on success, a negative error code otherwise.
742 int snd_soc_component_write(struct snd_soc_component *component,
743 unsigned int reg, unsigned int val)
745 int ret;
747 mutex_lock(&component->io_mutex);
748 ret = soc_component_write_no_lock(component, reg, val);
749 mutex_unlock(&component->io_mutex);
751 return ret;
753 EXPORT_SYMBOL_GPL(snd_soc_component_write);
755 static int snd_soc_component_update_bits_legacy(
756 struct snd_soc_component *component, unsigned int reg,
757 unsigned int mask, unsigned int val, bool *change)
759 unsigned int old, new;
760 int ret = 0;
762 mutex_lock(&component->io_mutex);
764 old = soc_component_read_no_lock(component, reg);
766 new = (old & ~mask) | (val & mask);
767 *change = old != new;
768 if (*change)
769 ret = soc_component_write_no_lock(component, reg, new);
771 mutex_unlock(&component->io_mutex);
773 return soc_component_ret(component, ret);
777 * snd_soc_component_update_bits() - Perform read/modify/write cycle
778 * @component: Component to update
779 * @reg: Register to update
780 * @mask: Mask that specifies which bits to update
781 * @val: New value for the bits specified by mask
783 * Return: 1 if the operation was successful and the value of the register
784 * changed, 0 if the operation was successful, but the value did not change.
785 * Returns a negative error code otherwise.
787 int snd_soc_component_update_bits(struct snd_soc_component *component,
788 unsigned int reg, unsigned int mask, unsigned int val)
790 bool change;
791 int ret;
793 if (component->regmap)
794 ret = regmap_update_bits_check(component->regmap, reg, mask,
795 val, &change);
796 else
797 ret = snd_soc_component_update_bits_legacy(component, reg,
798 mask, val, &change);
800 if (ret < 0)
801 return soc_component_ret(component, ret);
802 return change;
804 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
807 * snd_soc_component_update_bits_async() - Perform asynchronous
808 * read/modify/write cycle
809 * @component: Component to update
810 * @reg: Register to update
811 * @mask: Mask that specifies which bits to update
812 * @val: New value for the bits specified by mask
814 * This function is similar to snd_soc_component_update_bits(), but the update
815 * operation is scheduled asynchronously. This means it may not be completed
816 * when the function returns. To make sure that all scheduled updates have been
817 * completed snd_soc_component_async_complete() must be called.
819 * Return: 1 if the operation was successful and the value of the register
820 * changed, 0 if the operation was successful, but the value did not change.
821 * Returns a negative error code otherwise.
823 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
824 unsigned int reg, unsigned int mask, unsigned int val)
826 bool change;
827 int ret;
829 if (component->regmap)
830 ret = regmap_update_bits_check_async(component->regmap, reg,
831 mask, val, &change);
832 else
833 ret = snd_soc_component_update_bits_legacy(component, reg,
834 mask, val, &change);
836 if (ret < 0)
837 return soc_component_ret(component, ret);
838 return change;
840 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
843 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
844 * @component: Component for which to wait
846 * This function blocks until all asynchronous I/O which has previously been
847 * scheduled using snd_soc_component_update_bits_async() has completed.
849 void snd_soc_component_async_complete(struct snd_soc_component *component)
851 if (component->regmap)
852 regmap_async_complete(component->regmap);
854 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
857 * snd_soc_component_test_bits - Test register for change
858 * @component: component
859 * @reg: Register to test
860 * @mask: Mask that specifies which bits to test
861 * @value: Value to test against
863 * Tests a register with a new value and checks if the new value is
864 * different from the old value.
866 * Return: 1 for change, otherwise 0.
868 int snd_soc_component_test_bits(struct snd_soc_component *component,
869 unsigned int reg, unsigned int mask, unsigned int value)
871 unsigned int old, new;
873 old = snd_soc_component_read(component, reg);
874 new = (old & ~mask) | value;
875 return old != new;
877 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
879 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
881 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
882 struct snd_soc_component *component;
883 int i;
885 /* FIXME: use 1st pointer */
886 for_each_rtd_components(rtd, i, component)
887 if (component->driver->pointer)
888 return component->driver->pointer(component, substream);
890 return 0;
893 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
894 unsigned int cmd, void *arg)
896 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
897 struct snd_soc_component *component;
898 int i;
900 /* FIXME: use 1st ioctl */
901 for_each_rtd_components(rtd, i, component)
902 if (component->driver->ioctl)
903 return soc_component_ret(
904 component,
905 component->driver->ioctl(component,
906 substream, cmd, arg));
908 return snd_pcm_lib_ioctl(substream, cmd, arg);
911 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
913 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
914 struct snd_soc_component *component;
915 int i, ret;
917 for_each_rtd_components(rtd, i, component) {
918 if (component->driver->sync_stop) {
919 ret = component->driver->sync_stop(component,
920 substream);
921 if (ret < 0)
922 return soc_component_ret(component, ret);
926 return 0;
929 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
930 int channel, unsigned long pos,
931 void __user *buf, unsigned long bytes)
933 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
934 struct snd_soc_component *component;
935 int i;
937 /* FIXME. it returns 1st copy now */
938 for_each_rtd_components(rtd, i, component)
939 if (component->driver->copy_user)
940 return soc_component_ret(
941 component,
942 component->driver->copy_user(
943 component, substream, channel,
944 pos, buf, bytes));
946 return -EINVAL;
949 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
950 unsigned long offset)
952 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
953 struct snd_soc_component *component;
954 struct page *page;
955 int i;
957 /* FIXME. it returns 1st page now */
958 for_each_rtd_components(rtd, i, component) {
959 if (component->driver->page) {
960 page = component->driver->page(component,
961 substream, offset);
962 if (page)
963 return page;
967 return NULL;
970 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
971 struct vm_area_struct *vma)
973 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
974 struct snd_soc_component *component;
975 int i;
977 /* FIXME. it returns 1st mmap now */
978 for_each_rtd_components(rtd, i, component)
979 if (component->driver->mmap)
980 return soc_component_ret(
981 component,
982 component->driver->mmap(component,
983 substream, vma));
985 return -EINVAL;
988 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
990 struct snd_soc_component *component;
991 int ret;
992 int i;
994 for_each_rtd_components(rtd, i, component) {
995 if (component->driver->pcm_construct) {
996 ret = component->driver->pcm_construct(component, rtd);
997 if (ret < 0)
998 return soc_component_ret(component, ret);
1002 return 0;
1005 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
1007 struct snd_soc_component *component;
1008 int i;
1010 if (!rtd->pcm)
1011 return;
1013 for_each_rtd_components(rtd, i, component)
1014 if (component->driver->pcm_destruct)
1015 component->driver->pcm_destruct(component, rtd->pcm);
1018 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1020 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1021 struct snd_soc_component *component;
1022 int i, ret;
1024 for_each_rtd_components(rtd, i, component) {
1025 if (component->driver->prepare) {
1026 ret = component->driver->prepare(component, substream);
1027 if (ret < 0)
1028 return soc_component_ret(component, ret);
1032 return 0;
1035 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
1036 struct snd_pcm_hw_params *params)
1038 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1039 struct snd_soc_component *component;
1040 int i, ret;
1042 for_each_rtd_components(rtd, i, component) {
1043 if (component->driver->hw_params) {
1044 ret = component->driver->hw_params(component,
1045 substream, params);
1046 if (ret < 0)
1047 return soc_component_ret(component, ret);
1049 /* mark substream if succeeded */
1050 soc_component_mark_push(component, substream, hw_params);
1053 return 0;
1056 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
1057 int rollback)
1059 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1060 struct snd_soc_component *component;
1061 int i, ret;
1063 for_each_rtd_components(rtd, i, component) {
1064 if (rollback && !soc_component_mark_match(component, substream, hw_params))
1065 continue;
1067 if (component->driver->hw_free) {
1068 ret = component->driver->hw_free(component, substream);
1069 if (ret < 0)
1070 soc_component_ret(component, ret);
1073 /* remove marked substream */
1074 soc_component_mark_pop(component, substream, hw_params);
1078 static int soc_component_trigger(struct snd_soc_component *component,
1079 struct snd_pcm_substream *substream,
1080 int cmd)
1082 int ret = 0;
1084 if (component->driver->trigger)
1085 ret = component->driver->trigger(component, substream, cmd);
1087 return soc_component_ret(component, ret);
1090 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
1091 int cmd, int rollback)
1093 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1094 struct snd_soc_component *component;
1095 int i, r, ret = 0;
1097 switch (cmd) {
1098 case SNDRV_PCM_TRIGGER_START:
1099 case SNDRV_PCM_TRIGGER_RESUME:
1100 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1101 for_each_rtd_components(rtd, i, component) {
1102 ret = soc_component_trigger(component, substream, cmd);
1103 if (ret < 0)
1104 break;
1105 soc_component_mark_push(component, substream, trigger);
1107 break;
1108 case SNDRV_PCM_TRIGGER_STOP:
1109 case SNDRV_PCM_TRIGGER_SUSPEND:
1110 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1111 for_each_rtd_components(rtd, i, component) {
1112 if (rollback && !soc_component_mark_match(component, substream, trigger))
1113 continue;
1115 r = soc_component_trigger(component, substream, cmd);
1116 if (r < 0)
1117 ret = r; /* use last ret */
1118 soc_component_mark_pop(component, substream, trigger);
1122 return ret;
1125 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1126 void *stream)
1128 struct snd_soc_component *component;
1129 int i, ret;
1131 for_each_rtd_components(rtd, i, component) {
1132 ret = pm_runtime_get_sync(component->dev);
1133 if (ret < 0 && ret != -EACCES) {
1134 pm_runtime_put_noidle(component->dev);
1135 return soc_component_ret(component, ret);
1137 /* mark stream if succeeded */
1138 soc_component_mark_push(component, stream, pm);
1141 return 0;
1144 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1145 void *stream, int rollback)
1147 struct snd_soc_component *component;
1148 int i;
1150 for_each_rtd_components(rtd, i, component) {
1151 if (rollback && !soc_component_mark_match(component, stream, pm))
1152 continue;
1154 pm_runtime_mark_last_busy(component->dev);
1155 pm_runtime_put_autosuspend(component->dev);
1157 /* remove marked stream */
1158 soc_component_mark_pop(component, stream, pm);