1 // SPDX-License-Identifier: GPL-2.0
5 // Copyright 2009-2011 Wolfson Microelectronics PLC.
6 // Copyright (C) 2019 Renesas Electronics Corp.
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>
14 #include <linux/bitops.h>
16 #define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret, -1)
17 #define soc_component_ret_reg_rw(dai, ret, reg) _soc_component_ret(dai, __func__, ret, reg)
18 static inline int _soc_component_ret(struct snd_soc_component *component,
19 const char *func, int ret, int reg)
21 /* Positive/Zero values are not errors */
25 /* Negative values might be errors */
32 dev_err(component->dev,
33 "ASoC: error at %s on %s: %d\n",
34 func, component->name, ret);
36 dev_err(component->dev,
37 "ASoC: error at %s on %s for register: [0x%08x] %d\n",
38 func, component->name, reg, ret);
44 static inline int soc_component_field_shift(struct snd_soc_component *component,
48 dev_err(component->dev, "ASoC: error field mask is zero for %s\n",
53 return (ffs(mask) - 1);
57 * We might want to check substream by using list.
58 * In such case, we can update these macros.
60 #define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
61 #define soc_component_mark_pop(component, substream, tgt) ((component)->mark_##tgt = NULL)
62 #define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
64 void snd_soc_component_set_aux(struct snd_soc_component *component,
65 struct snd_soc_aux_dev *aux)
67 component->init = (aux) ? aux->init : NULL;
70 int snd_soc_component_init(struct snd_soc_component *component)
75 ret = component->init(component);
77 return soc_component_ret(component, ret);
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.
88 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
90 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
91 int clk_id, int source, unsigned int freq,
96 if (component->driver->set_sysclk)
97 ret = component->driver->set_sysclk(component, clk_id, source,
100 return soc_component_ret(component, ret);
102 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
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
112 * Configures and enables PLL to generate output clock based on input clock.
114 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
115 int source, unsigned int freq_in,
116 unsigned int freq_out)
120 if (component->driver->set_pll)
121 ret = component->driver->set_pll(component, pll_id, source,
124 return soc_component_ret(component, ret);
126 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
128 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
129 enum snd_soc_dapm_type type, int subseq)
131 if (component->driver->seq_notifier)
132 component->driver->seq_notifier(component, type, subseq);
135 int snd_soc_component_stream_event(struct snd_soc_component *component,
140 if (component->driver->stream_event)
141 ret = component->driver->stream_event(component, event);
143 return soc_component_ret(component, ret);
146 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
147 enum snd_soc_bias_level level)
151 if (component->driver->set_bias_level)
152 ret = component->driver->set_bias_level(component, level);
154 return soc_component_ret(component, ret);
157 int snd_soc_component_enable_pin(struct snd_soc_component *component,
160 struct snd_soc_dapm_context *dapm =
161 snd_soc_component_get_dapm(component);
162 return snd_soc_dapm_enable_pin(dapm, pin);
164 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
166 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
169 struct snd_soc_dapm_context *dapm =
170 snd_soc_component_get_dapm(component);
171 return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
173 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
175 int snd_soc_component_disable_pin(struct snd_soc_component *component,
178 struct snd_soc_dapm_context *dapm =
179 snd_soc_component_get_dapm(component);
180 return snd_soc_dapm_disable_pin(dapm, pin);
182 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
184 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
187 struct snd_soc_dapm_context *dapm =
188 snd_soc_component_get_dapm(component);
189 return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
191 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
193 int snd_soc_component_nc_pin(struct snd_soc_component *component,
196 struct snd_soc_dapm_context *dapm =
197 snd_soc_component_get_dapm(component);
198 return snd_soc_dapm_nc_pin(dapm, pin);
200 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
202 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
205 struct snd_soc_dapm_context *dapm =
206 snd_soc_component_get_dapm(component);
207 return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
209 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
211 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
214 struct snd_soc_dapm_context *dapm =
215 snd_soc_component_get_dapm(component);
216 return snd_soc_dapm_get_pin_status(dapm, pin);
218 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
220 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
223 struct snd_soc_dapm_context *dapm =
224 snd_soc_component_get_dapm(component);
225 return snd_soc_dapm_force_enable_pin(dapm, pin);
227 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
229 int snd_soc_component_force_enable_pin_unlocked(
230 struct snd_soc_component *component,
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);
237 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
240 * snd_soc_component_set_jack - configure component jack.
241 * @component: COMPONENTs
242 * @jack: structure to use for the jack
243 * @data: can be used if codec driver need extra data for configuring jack
245 * Configures and enables jack detection function.
247 int snd_soc_component_set_jack(struct snd_soc_component *component,
248 struct snd_soc_jack *jack, void *data)
252 if (component->driver->set_jack)
253 ret = component->driver->set_jack(component, jack, data);
255 return soc_component_ret(component, ret);
257 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
260 * snd_soc_component_get_jack_type
261 * @component: COMPONENTs
263 * Returns the jack type of the component
264 * This can either be the supported type or one read from
265 * devicetree with the property: jack-type.
267 int snd_soc_component_get_jack_type(
268 struct snd_soc_component *component)
272 if (component->driver->get_jack_type)
273 ret = component->driver->get_jack_type(component);
275 return soc_component_ret(component, ret);
277 EXPORT_SYMBOL_GPL(snd_soc_component_get_jack_type);
279 int snd_soc_component_module_get(struct snd_soc_component *component,
280 void *mark, int upon_open)
284 if (component->driver->module_get_upon_open == !!upon_open &&
285 !try_module_get(component->dev->driver->owner))
288 /* mark module if succeeded */
290 soc_component_mark_push(component, mark, module);
292 return soc_component_ret(component, ret);
295 void snd_soc_component_module_put(struct snd_soc_component *component,
296 void *mark, int upon_open, int rollback)
298 if (rollback && !soc_component_mark_match(component, mark, module))
301 if (component->driver->module_get_upon_open == !!upon_open)
302 module_put(component->dev->driver->owner);
304 /* remove the mark from module */
305 soc_component_mark_pop(component, mark, module);
308 int snd_soc_component_open(struct snd_soc_component *component,
309 struct snd_pcm_substream *substream)
313 if (component->driver->open)
314 ret = component->driver->open(component, substream);
316 /* mark substream if succeeded */
318 soc_component_mark_push(component, substream, open);
320 return soc_component_ret(component, ret);
323 int snd_soc_component_close(struct snd_soc_component *component,
324 struct snd_pcm_substream *substream,
329 if (rollback && !soc_component_mark_match(component, substream, open))
332 if (component->driver->close)
333 ret = component->driver->close(component, substream);
335 /* remove marked substream */
336 soc_component_mark_pop(component, substream, open);
338 return soc_component_ret(component, ret);
341 void snd_soc_component_suspend(struct snd_soc_component *component)
343 if (component->driver->suspend)
344 component->driver->suspend(component);
345 component->suspended = 1;
348 void snd_soc_component_resume(struct snd_soc_component *component)
350 if (component->driver->resume)
351 component->driver->resume(component);
352 component->suspended = 0;
355 int snd_soc_component_is_suspended(struct snd_soc_component *component)
357 return component->suspended;
360 int snd_soc_component_probe(struct snd_soc_component *component)
364 if (component->driver->probe)
365 ret = component->driver->probe(component);
367 return soc_component_ret(component, ret);
370 void snd_soc_component_remove(struct snd_soc_component *component)
372 if (component->driver->remove)
373 component->driver->remove(component);
376 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
377 struct device_node *ep)
381 if (component->driver->of_xlate_dai_id)
382 ret = component->driver->of_xlate_dai_id(component, ep);
384 return soc_component_ret(component, ret);
387 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
388 const struct of_phandle_args *args,
389 const char **dai_name)
391 if (component->driver->of_xlate_dai_name)
392 return component->driver->of_xlate_dai_name(component,
395 * Don't use soc_component_ret here because we may not want to report
396 * the error just yet. If a device has more than one component, the
397 * first may not match and we don't want spam the log with this.
402 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
404 int val_bytes = regmap_get_val_bytes(component->regmap);
406 /* Errors are legitimate for non-integer byte multiples */
408 component->val_bytes = val_bytes;
414 * snd_soc_component_init_regmap() - Initialize regmap instance for the
416 * @component: The component for which to initialize the regmap instance
417 * @regmap: The regmap instance that should be used by the component
419 * This function allows deferred assignment of the regmap instance that is
420 * associated with the component. Only use this if the regmap instance is not
421 * yet ready when the component is registered. The function must also be called
422 * before the first IO attempt of the component.
424 void snd_soc_component_init_regmap(struct snd_soc_component *component,
425 struct regmap *regmap)
427 component->regmap = regmap;
428 snd_soc_component_setup_regmap(component);
430 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
433 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
435 * @component: The component for which to de-initialize the regmap instance
437 * Calls regmap_exit() on the regmap instance associated to the component and
438 * removes the regmap instance from the component.
440 * This function should only be used if snd_soc_component_init_regmap() was used
441 * to initialize the regmap instance.
443 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
445 regmap_exit(component->regmap);
446 component->regmap = NULL;
448 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
452 int snd_soc_component_compr_open(struct snd_soc_component *component,
453 struct snd_compr_stream *cstream)
457 if (component->driver->compress_ops &&
458 component->driver->compress_ops->open)
459 ret = component->driver->compress_ops->open(component, cstream);
461 /* mark substream if succeeded */
463 soc_component_mark_push(component, cstream, compr_open);
465 return soc_component_ret(component, ret);
467 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
469 void snd_soc_component_compr_free(struct snd_soc_component *component,
470 struct snd_compr_stream *cstream,
473 if (rollback && !soc_component_mark_match(component, cstream, compr_open))
476 if (component->driver->compress_ops &&
477 component->driver->compress_ops->free)
478 component->driver->compress_ops->free(component, cstream);
480 /* remove marked substream */
481 soc_component_mark_pop(component, cstream, compr_open);
483 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
485 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
487 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
488 struct snd_soc_component *component;
491 for_each_rtd_components(rtd, i, component) {
492 if (component->driver->compress_ops &&
493 component->driver->compress_ops->trigger) {
494 ret = component->driver->compress_ops->trigger(
495 component, cstream, cmd);
497 return soc_component_ret(component, ret);
503 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
505 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
506 struct snd_compr_params *params)
508 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
509 struct snd_soc_component *component;
512 for_each_rtd_components(rtd, i, component) {
513 if (component->driver->compress_ops &&
514 component->driver->compress_ops->set_params) {
515 ret = component->driver->compress_ops->set_params(
516 component, cstream, params);
518 return soc_component_ret(component, ret);
524 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
526 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
527 struct snd_codec *params)
529 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
530 struct snd_soc_component *component;
533 for_each_rtd_components(rtd, i, component) {
534 if (component->driver->compress_ops &&
535 component->driver->compress_ops->get_params) {
536 ret = component->driver->compress_ops->get_params(
537 component, cstream, params);
538 return soc_component_ret(component, ret);
544 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
546 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
547 struct snd_compr_caps *caps)
549 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
550 struct snd_soc_component *component;
553 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
555 for_each_rtd_components(rtd, i, component) {
556 if (component->driver->compress_ops &&
557 component->driver->compress_ops->get_caps) {
558 ret = component->driver->compress_ops->get_caps(
559 component, cstream, caps);
564 mutex_unlock(&rtd->card->pcm_mutex);
566 return soc_component_ret(component, ret);
568 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
570 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
571 struct snd_compr_codec_caps *codec)
573 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
574 struct snd_soc_component *component;
577 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
579 for_each_rtd_components(rtd, i, component) {
580 if (component->driver->compress_ops &&
581 component->driver->compress_ops->get_codec_caps) {
582 ret = component->driver->compress_ops->get_codec_caps(
583 component, cstream, codec);
588 mutex_unlock(&rtd->card->pcm_mutex);
590 return soc_component_ret(component, ret);
592 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
594 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
596 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
597 struct snd_soc_component *component;
600 for_each_rtd_components(rtd, i, component) {
601 if (component->driver->compress_ops &&
602 component->driver->compress_ops->ack) {
603 ret = component->driver->compress_ops->ack(
604 component, cstream, bytes);
606 return soc_component_ret(component, ret);
612 EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
614 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
615 struct snd_compr_tstamp *tstamp)
617 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
618 struct snd_soc_component *component;
621 for_each_rtd_components(rtd, i, component) {
622 if (component->driver->compress_ops &&
623 component->driver->compress_ops->pointer) {
624 ret = component->driver->compress_ops->pointer(
625 component, cstream, tstamp);
626 return soc_component_ret(component, ret);
632 EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
634 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
635 char __user *buf, size_t count)
637 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
638 struct snd_soc_component *component;
641 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
643 for_each_rtd_components(rtd, i, component) {
644 if (component->driver->compress_ops &&
645 component->driver->compress_ops->copy) {
646 ret = component->driver->compress_ops->copy(
647 component, cstream, buf, count);
652 mutex_unlock(&rtd->card->pcm_mutex);
654 return soc_component_ret(component, ret);
656 EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
658 int snd_soc_component_compr_set_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;
665 for_each_rtd_components(rtd, i, component) {
666 if (component->driver->compress_ops &&
667 component->driver->compress_ops->set_metadata) {
668 ret = component->driver->compress_ops->set_metadata(
669 component, cstream, metadata);
671 return soc_component_ret(component, ret);
677 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
679 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
680 struct snd_compr_metadata *metadata)
682 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
683 struct snd_soc_component *component;
686 for_each_rtd_components(rtd, i, component) {
687 if (component->driver->compress_ops &&
688 component->driver->compress_ops->get_metadata) {
689 ret = component->driver->compress_ops->get_metadata(
690 component, cstream, metadata);
691 return soc_component_ret(component, ret);
697 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
699 static unsigned int soc_component_read_no_lock(
700 struct snd_soc_component *component,
704 unsigned int val = 0;
706 if (component->regmap)
707 ret = regmap_read(component->regmap, reg, &val);
708 else if (component->driver->read) {
710 val = component->driver->read(component, reg);
716 return soc_component_ret_reg_rw(component, ret, reg);
722 * snd_soc_component_read() - Read register value
723 * @component: Component to read from
724 * @reg: Register to read
728 unsigned int snd_soc_component_read(struct snd_soc_component *component,
733 mutex_lock(&component->io_mutex);
734 val = soc_component_read_no_lock(component, reg);
735 mutex_unlock(&component->io_mutex);
739 EXPORT_SYMBOL_GPL(snd_soc_component_read);
741 static int soc_component_write_no_lock(
742 struct snd_soc_component *component,
743 unsigned int reg, unsigned int val)
747 if (component->regmap)
748 ret = regmap_write(component->regmap, reg, val);
749 else if (component->driver->write)
750 ret = component->driver->write(component, reg, val);
752 return soc_component_ret_reg_rw(component, ret, reg);
756 * snd_soc_component_write() - Write register value
757 * @component: Component to write to
758 * @reg: Register to write
759 * @val: Value to write to the register
761 * Return: 0 on success, a negative error code otherwise.
763 int snd_soc_component_write(struct snd_soc_component *component,
764 unsigned int reg, unsigned int val)
768 mutex_lock(&component->io_mutex);
769 ret = soc_component_write_no_lock(component, reg, val);
770 mutex_unlock(&component->io_mutex);
774 EXPORT_SYMBOL_GPL(snd_soc_component_write);
776 static int snd_soc_component_update_bits_legacy(
777 struct snd_soc_component *component, unsigned int reg,
778 unsigned int mask, unsigned int val, bool *change)
780 unsigned int old, new;
783 mutex_lock(&component->io_mutex);
785 old = soc_component_read_no_lock(component, reg);
787 new = (old & ~mask) | (val & mask);
788 *change = old != new;
790 ret = soc_component_write_no_lock(component, reg, new);
792 mutex_unlock(&component->io_mutex);
794 return soc_component_ret_reg_rw(component, ret, reg);
798 * snd_soc_component_update_bits() - Perform read/modify/write cycle
799 * @component: Component to update
800 * @reg: Register to update
801 * @mask: Mask that specifies which bits to update
802 * @val: New value for the bits specified by mask
804 * Return: 1 if the operation was successful and the value of the register
805 * changed, 0 if the operation was successful, but the value did not change.
806 * Returns a negative error code otherwise.
808 int snd_soc_component_update_bits(struct snd_soc_component *component,
809 unsigned int reg, unsigned int mask, unsigned int val)
814 if (component->regmap)
815 ret = regmap_update_bits_check(component->regmap, reg, mask,
818 ret = snd_soc_component_update_bits_legacy(component, reg,
822 return soc_component_ret_reg_rw(component, ret, reg);
825 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
828 * snd_soc_component_update_bits_async() - Perform asynchronous
829 * read/modify/write cycle
830 * @component: Component to update
831 * @reg: Register to update
832 * @mask: Mask that specifies which bits to update
833 * @val: New value for the bits specified by mask
835 * This function is similar to snd_soc_component_update_bits(), but the update
836 * operation is scheduled asynchronously. This means it may not be completed
837 * when the function returns. To make sure that all scheduled updates have been
838 * completed snd_soc_component_async_complete() must be called.
840 * Return: 1 if the operation was successful and the value of the register
841 * changed, 0 if the operation was successful, but the value did not change.
842 * Returns a negative error code otherwise.
844 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
845 unsigned int reg, unsigned int mask, unsigned int val)
850 if (component->regmap)
851 ret = regmap_update_bits_check_async(component->regmap, reg,
854 ret = snd_soc_component_update_bits_legacy(component, reg,
858 return soc_component_ret_reg_rw(component, ret, reg);
861 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
864 * snd_soc_component_read_field() - Read register field value
865 * @component: Component to read from
866 * @reg: Register to read
867 * @mask: mask of the register field
869 * Return: read value of register field.
871 unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
872 unsigned int reg, unsigned int mask)
876 val = snd_soc_component_read(component, reg);
878 val = (val & mask) >> soc_component_field_shift(component, mask);
882 EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
885 * snd_soc_component_write_field() - write to register field
886 * @component: Component to write to
887 * @reg: Register to write
888 * @mask: mask of the register field to update
889 * @val: value of the field to write
891 * Return: 1 for change, otherwise 0.
893 int snd_soc_component_write_field(struct snd_soc_component *component,
894 unsigned int reg, unsigned int mask,
898 val = (val << soc_component_field_shift(component, mask)) & mask;
900 return snd_soc_component_update_bits(component, reg, mask, val);
902 EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
905 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
906 * @component: Component for which to wait
908 * This function blocks until all asynchronous I/O which has previously been
909 * scheduled using snd_soc_component_update_bits_async() has completed.
911 void snd_soc_component_async_complete(struct snd_soc_component *component)
913 if (component->regmap)
914 regmap_async_complete(component->regmap);
916 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
919 * snd_soc_component_test_bits - Test register for change
920 * @component: component
921 * @reg: Register to test
922 * @mask: Mask that specifies which bits to test
923 * @value: Value to test against
925 * Tests a register with a new value and checks if the new value is
926 * different from the old value.
928 * Return: 1 for change, otherwise 0.
930 int snd_soc_component_test_bits(struct snd_soc_component *component,
931 unsigned int reg, unsigned int mask, unsigned int value)
933 unsigned int old, new;
935 old = snd_soc_component_read(component, reg);
936 new = (old & ~mask) | value;
939 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
941 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
943 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
944 struct snd_soc_component *component;
947 /* FIXME: use 1st pointer */
948 for_each_rtd_components(rtd, i, component)
949 if (component->driver->pointer)
950 return component->driver->pointer(component, substream);
955 static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd,
956 struct snd_soc_component *component)
958 struct snd_soc_dai *dai;
961 for_each_rtd_codec_dais(rtd, i, dai) {
962 if (dai->component == component)
969 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
970 snd_pcm_sframes_t *cpu_delay,
971 snd_pcm_sframes_t *codec_delay)
973 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
974 struct snd_soc_component *component;
975 snd_pcm_sframes_t delay;
979 * We're looking for the delay through the full audio path so it needs to
980 * be the maximum of the Components doing transmit and the maximum of the
981 * Components doing receive (ie, all CPUs and all CODECs) rather than
982 * just the maximum of all Components.
984 for_each_rtd_components(rtd, i, component) {
985 if (!component->driver->delay)
988 delay = component->driver->delay(component, substream);
990 if (snd_soc_component_is_codec_on_rtd(rtd, component))
991 *codec_delay = max(*codec_delay, delay);
993 *cpu_delay = max(*cpu_delay, delay);
997 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
998 unsigned int cmd, void *arg)
1000 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1001 struct snd_soc_component *component;
1004 /* FIXME: use 1st ioctl */
1005 for_each_rtd_components(rtd, i, component)
1006 if (component->driver->ioctl)
1007 return soc_component_ret(
1009 component->driver->ioctl(component,
1010 substream, cmd, arg));
1012 return snd_pcm_lib_ioctl(substream, cmd, arg);
1015 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
1017 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1018 struct snd_soc_component *component;
1021 for_each_rtd_components(rtd, i, component) {
1022 if (component->driver->sync_stop) {
1023 ret = component->driver->sync_stop(component,
1026 return soc_component_ret(component, ret);
1033 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
1034 int channel, unsigned long pos,
1035 void __user *buf, unsigned long bytes)
1037 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1038 struct snd_soc_component *component;
1041 /* FIXME. it returns 1st copy now */
1042 for_each_rtd_components(rtd, i, component)
1043 if (component->driver->copy_user)
1044 return soc_component_ret(
1046 component->driver->copy_user(
1047 component, substream, channel,
1053 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
1054 unsigned long offset)
1056 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1057 struct snd_soc_component *component;
1061 /* FIXME. it returns 1st page now */
1062 for_each_rtd_components(rtd, i, component) {
1063 if (component->driver->page) {
1064 page = component->driver->page(component,
1074 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
1075 struct vm_area_struct *vma)
1077 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1078 struct snd_soc_component *component;
1081 /* FIXME. it returns 1st mmap now */
1082 for_each_rtd_components(rtd, i, component)
1083 if (component->driver->mmap)
1084 return soc_component_ret(
1086 component->driver->mmap(component,
1092 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
1094 struct snd_soc_component *component;
1098 for_each_rtd_components(rtd, i, component) {
1099 if (component->driver->pcm_construct) {
1100 ret = component->driver->pcm_construct(component, rtd);
1102 return soc_component_ret(component, ret);
1109 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
1111 struct snd_soc_component *component;
1117 for_each_rtd_components(rtd, i, component)
1118 if (component->driver->pcm_destruct)
1119 component->driver->pcm_destruct(component, rtd->pcm);
1122 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1124 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1125 struct snd_soc_component *component;
1128 for_each_rtd_components(rtd, i, component) {
1129 if (component->driver->prepare) {
1130 ret = component->driver->prepare(component, substream);
1132 return soc_component_ret(component, ret);
1139 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
1140 struct snd_pcm_hw_params *params)
1142 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1143 struct snd_soc_component *component;
1146 for_each_rtd_components(rtd, i, component) {
1147 if (component->driver->hw_params) {
1148 ret = component->driver->hw_params(component,
1151 return soc_component_ret(component, ret);
1153 /* mark substream if succeeded */
1154 soc_component_mark_push(component, substream, hw_params);
1160 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
1163 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1164 struct snd_soc_component *component;
1167 for_each_rtd_components(rtd, i, component) {
1168 if (rollback && !soc_component_mark_match(component, substream, hw_params))
1171 if (component->driver->hw_free) {
1172 ret = component->driver->hw_free(component, substream);
1174 soc_component_ret(component, ret);
1177 /* remove marked substream */
1178 soc_component_mark_pop(component, substream, hw_params);
1182 static int soc_component_trigger(struct snd_soc_component *component,
1183 struct snd_pcm_substream *substream,
1188 if (component->driver->trigger)
1189 ret = component->driver->trigger(component, substream, cmd);
1191 return soc_component_ret(component, ret);
1194 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
1195 int cmd, int rollback)
1197 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1198 struct snd_soc_component *component;
1202 case SNDRV_PCM_TRIGGER_START:
1203 case SNDRV_PCM_TRIGGER_RESUME:
1204 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1205 for_each_rtd_components(rtd, i, component) {
1206 ret = soc_component_trigger(component, substream, cmd);
1209 soc_component_mark_push(component, substream, trigger);
1212 case SNDRV_PCM_TRIGGER_STOP:
1213 case SNDRV_PCM_TRIGGER_SUSPEND:
1214 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1215 for_each_rtd_components(rtd, i, component) {
1216 if (rollback && !soc_component_mark_match(component, substream, trigger))
1219 r = soc_component_trigger(component, substream, cmd);
1221 ret = r; /* use last ret */
1222 soc_component_mark_pop(component, substream, trigger);
1229 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1232 struct snd_soc_component *component;
1235 for_each_rtd_components(rtd, i, component) {
1236 int ret = pm_runtime_get_sync(component->dev);
1237 if (ret < 0 && ret != -EACCES) {
1238 pm_runtime_put_noidle(component->dev);
1239 return soc_component_ret(component, ret);
1241 /* mark stream if succeeded */
1242 soc_component_mark_push(component, stream, pm);
1248 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1249 void *stream, int rollback)
1251 struct snd_soc_component *component;
1254 for_each_rtd_components(rtd, i, component) {
1255 if (rollback && !soc_component_mark_match(component, stream, pm))
1258 pm_runtime_mark_last_busy(component->dev);
1259 pm_runtime_put_autosuspend(component->dev);
1261 /* remove marked stream */
1262 soc_component_mark_pop(component, stream, pm);
1266 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
1268 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1269 struct snd_soc_component *component;
1272 /* FIXME: use 1st pointer */
1273 for_each_rtd_components(rtd, i, component)
1274 if (component->driver->ack)
1275 return component->driver->ack(component, substream);