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, 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
);
239 static void soc_get_kcontrol_name(struct snd_soc_component
*component
,
240 char *buf
, int size
, const char * const ctl
)
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
);
246 snprintf(buf
, size
, "%s", ctl
);
249 struct snd_kcontrol
*snd_soc_component_get_kcontrol(struct snd_soc_component
*component
,
250 const char * const ctl
)
252 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
254 soc_get_kcontrol_name(component
, name
, ARRAY_SIZE(name
), ctl
);
256 return snd_soc_card_get_kcontrol(component
->card
, name
);
258 EXPORT_SYMBOL_GPL(snd_soc_component_get_kcontrol
);
260 int snd_soc_component_notify_control(struct snd_soc_component
*component
,
261 const char * const ctl
)
263 struct snd_kcontrol
*kctl
;
265 kctl
= snd_soc_component_get_kcontrol(component
, ctl
);
267 return soc_component_ret(component
, -EINVAL
);
269 snd_ctl_notify(component
->card
->snd_card
,
270 SNDRV_CTL_EVENT_MASK_VALUE
, &kctl
->id
);
274 EXPORT_SYMBOL_GPL(snd_soc_component_notify_control
);
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
282 * Configures and enables jack detection function.
284 int snd_soc_component_set_jack(struct snd_soc_component
*component
,
285 struct snd_soc_jack
*jack
, void *data
)
289 if (component
->driver
->set_jack
)
290 ret
= component
->driver
->set_jack(component
, jack
, data
);
292 return soc_component_ret(component
, ret
);
294 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack
);
297 * snd_soc_component_get_jack_type
298 * @component: COMPONENTs
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.
304 int snd_soc_component_get_jack_type(
305 struct snd_soc_component
*component
)
309 if (component
->driver
->get_jack_type
)
310 ret
= component
->driver
->get_jack_type(component
);
312 return soc_component_ret(component
, ret
);
314 EXPORT_SYMBOL_GPL(snd_soc_component_get_jack_type
);
316 int snd_soc_component_module_get(struct snd_soc_component
*component
,
317 void *mark
, int upon_open
)
321 if (component
->driver
->module_get_upon_open
== !!upon_open
&&
322 !try_module_get(component
->dev
->driver
->owner
))
325 /* mark module if succeeded */
327 soc_component_mark_push(component
, mark
, module
);
329 return soc_component_ret(component
, ret
);
332 void snd_soc_component_module_put(struct snd_soc_component
*component
,
333 void *mark
, int upon_open
, int rollback
)
335 if (rollback
&& !soc_component_mark_match(component
, mark
, module
))
338 if (component
->driver
->module_get_upon_open
== !!upon_open
)
339 module_put(component
->dev
->driver
->owner
);
341 /* remove the mark from module */
342 soc_component_mark_pop(component
, module
);
345 int snd_soc_component_open(struct snd_soc_component
*component
,
346 struct snd_pcm_substream
*substream
)
350 if (component
->driver
->open
)
351 ret
= component
->driver
->open(component
, substream
);
353 /* mark substream if succeeded */
355 soc_component_mark_push(component
, substream
, open
);
357 return soc_component_ret(component
, ret
);
360 int snd_soc_component_close(struct snd_soc_component
*component
,
361 struct snd_pcm_substream
*substream
,
366 if (rollback
&& !soc_component_mark_match(component
, substream
, open
))
369 if (component
->driver
->close
)
370 ret
= component
->driver
->close(component
, substream
);
372 /* remove marked substream */
373 soc_component_mark_pop(component
, open
);
375 return soc_component_ret(component
, ret
);
378 void snd_soc_component_suspend(struct snd_soc_component
*component
)
380 if (component
->driver
->suspend
)
381 component
->driver
->suspend(component
);
382 component
->suspended
= 1;
385 void snd_soc_component_resume(struct snd_soc_component
*component
)
387 if (component
->driver
->resume
)
388 component
->driver
->resume(component
);
389 component
->suspended
= 0;
392 int snd_soc_component_is_suspended(struct snd_soc_component
*component
)
394 return component
->suspended
;
397 int snd_soc_component_probe(struct snd_soc_component
*component
)
401 if (component
->driver
->probe
)
402 ret
= component
->driver
->probe(component
);
404 return soc_component_ret(component
, ret
);
407 void snd_soc_component_remove(struct snd_soc_component
*component
)
409 if (component
->driver
->remove
)
410 component
->driver
->remove(component
);
413 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component
*component
,
414 struct device_node
*ep
)
418 if (component
->driver
->of_xlate_dai_id
)
419 ret
= component
->driver
->of_xlate_dai_id(component
, ep
);
421 return soc_component_ret(component
, ret
);
424 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component
*component
,
425 const struct of_phandle_args
*args
,
426 const char **dai_name
)
428 if (component
->driver
->of_xlate_dai_name
)
429 return component
->driver
->of_xlate_dai_name(component
,
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.
439 void snd_soc_component_setup_regmap(struct snd_soc_component
*component
)
441 int val_bytes
= regmap_get_val_bytes(component
->regmap
);
443 /* Errors are legitimate for non-integer byte multiples */
445 component
->val_bytes
= val_bytes
;
451 * snd_soc_component_init_regmap() - Initialize regmap instance for the
453 * @component: The component for which to initialize the regmap instance
454 * @regmap: The regmap instance that should be used by the component
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.
461 void snd_soc_component_init_regmap(struct snd_soc_component
*component
,
462 struct regmap
*regmap
)
464 component
->regmap
= regmap
;
465 snd_soc_component_setup_regmap(component
);
467 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap
);
470 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
472 * @component: The component for which to de-initialize the regmap instance
474 * Calls regmap_exit() on the regmap instance associated to the component and
475 * removes the regmap instance from the component.
477 * This function should only be used if snd_soc_component_init_regmap() was used
478 * to initialize the regmap instance.
480 void snd_soc_component_exit_regmap(struct snd_soc_component
*component
)
482 regmap_exit(component
->regmap
);
483 component
->regmap
= NULL
;
485 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap
);
489 int snd_soc_component_compr_open(struct snd_soc_component
*component
,
490 struct snd_compr_stream
*cstream
)
494 if (component
->driver
->compress_ops
&&
495 component
->driver
->compress_ops
->open
)
496 ret
= component
->driver
->compress_ops
->open(component
, cstream
);
498 /* mark substream if succeeded */
500 soc_component_mark_push(component
, cstream
, compr_open
);
502 return soc_component_ret(component
, ret
);
504 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open
);
506 void snd_soc_component_compr_free(struct snd_soc_component
*component
,
507 struct snd_compr_stream
*cstream
,
510 if (rollback
&& !soc_component_mark_match(component
, cstream
, compr_open
))
513 if (component
->driver
->compress_ops
&&
514 component
->driver
->compress_ops
->free
)
515 component
->driver
->compress_ops
->free(component
, cstream
);
517 /* remove marked substream */
518 soc_component_mark_pop(component
, compr_open
);
520 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free
);
522 int snd_soc_component_compr_trigger(struct snd_compr_stream
*cstream
, int cmd
)
524 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
525 struct snd_soc_component
*component
;
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
);
534 return soc_component_ret(component
, ret
);
540 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger
);
542 int snd_soc_component_compr_set_params(struct snd_compr_stream
*cstream
,
543 struct snd_compr_params
*params
)
545 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
546 struct snd_soc_component
*component
;
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
);
555 return soc_component_ret(component
, ret
);
561 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params
);
563 int snd_soc_component_compr_get_params(struct snd_compr_stream
*cstream
,
564 struct snd_codec
*params
)
566 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
567 struct snd_soc_component
*component
;
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
);
581 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params
);
583 int snd_soc_component_compr_get_caps(struct snd_compr_stream
*cstream
,
584 struct snd_compr_caps
*caps
)
586 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
587 struct snd_soc_component
*component
;
590 snd_soc_dpcm_mutex_lock(rtd
);
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
);
601 snd_soc_dpcm_mutex_unlock(rtd
);
603 return soc_component_ret(component
, ret
);
605 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps
);
607 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream
*cstream
,
608 struct snd_compr_codec_caps
*codec
)
610 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
611 struct snd_soc_component
*component
;
614 snd_soc_dpcm_mutex_lock(rtd
);
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
);
625 snd_soc_dpcm_mutex_unlock(rtd
);
627 return soc_component_ret(component
, ret
);
629 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps
);
631 int snd_soc_component_compr_ack(struct snd_compr_stream
*cstream
, size_t bytes
)
633 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
634 struct snd_soc_component
*component
;
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
);
643 return soc_component_ret(component
, ret
);
649 EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack
);
651 int snd_soc_component_compr_pointer(struct snd_compr_stream
*cstream
,
652 struct snd_compr_tstamp
*tstamp
)
654 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
655 struct snd_soc_component
*component
;
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
);
669 EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer
);
671 int snd_soc_component_compr_copy(struct snd_compr_stream
*cstream
,
672 char __user
*buf
, size_t count
)
674 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
675 struct snd_soc_component
*component
;
678 snd_soc_dpcm_mutex_lock(rtd
);
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
);
689 snd_soc_dpcm_mutex_unlock(rtd
);
691 return soc_component_ret(component
, ret
);
693 EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy
);
695 int snd_soc_component_compr_set_metadata(struct snd_compr_stream
*cstream
,
696 struct snd_compr_metadata
*metadata
)
698 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
699 struct snd_soc_component
*component
;
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
);
708 return soc_component_ret(component
, ret
);
714 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata
);
716 int snd_soc_component_compr_get_metadata(struct snd_compr_stream
*cstream
,
717 struct snd_compr_metadata
*metadata
)
719 struct snd_soc_pcm_runtime
*rtd
= cstream
->private_data
;
720 struct snd_soc_component
*component
;
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
);
734 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata
);
736 static unsigned int soc_component_read_no_lock(
737 struct snd_soc_component
*component
,
741 unsigned int val
= 0;
743 if (component
->regmap
)
744 ret
= regmap_read(component
->regmap
, reg
, &val
);
745 else if (component
->driver
->read
) {
747 val
= component
->driver
->read(component
, reg
);
753 return soc_component_ret_reg_rw(component
, ret
, reg
);
759 * snd_soc_component_read() - Read register value
760 * @component: Component to read from
761 * @reg: Register to read
765 unsigned int snd_soc_component_read(struct snd_soc_component
*component
,
770 mutex_lock(&component
->io_mutex
);
771 val
= soc_component_read_no_lock(component
, reg
);
772 mutex_unlock(&component
->io_mutex
);
776 EXPORT_SYMBOL_GPL(snd_soc_component_read
);
778 static int soc_component_write_no_lock(
779 struct snd_soc_component
*component
,
780 unsigned int reg
, unsigned int val
)
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
);
789 return soc_component_ret_reg_rw(component
, ret
, reg
);
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
798 * Return: 0 on success, a negative error code otherwise.
800 int snd_soc_component_write(struct snd_soc_component
*component
,
801 unsigned int reg
, unsigned int val
)
805 mutex_lock(&component
->io_mutex
);
806 ret
= soc_component_write_no_lock(component
, reg
, val
);
807 mutex_unlock(&component
->io_mutex
);
811 EXPORT_SYMBOL_GPL(snd_soc_component_write
);
813 static 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
)
817 unsigned int old
, new;
820 mutex_lock(&component
->io_mutex
);
822 old
= soc_component_read_no_lock(component
, reg
);
824 new = (old
& ~mask
) | (val
& mask
);
825 *change
= old
!= new;
827 ret
= soc_component_write_no_lock(component
, reg
, new);
829 mutex_unlock(&component
->io_mutex
);
831 return soc_component_ret_reg_rw(component
, ret
, reg
);
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
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.
845 int snd_soc_component_update_bits(struct snd_soc_component
*component
,
846 unsigned int reg
, unsigned int mask
, unsigned int val
)
851 if (component
->regmap
)
852 ret
= regmap_update_bits_check(component
->regmap
, reg
, mask
,
855 ret
= snd_soc_component_update_bits_legacy(component
, reg
,
859 return soc_component_ret_reg_rw(component
, ret
, reg
);
862 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits
);
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
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.
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.
881 int snd_soc_component_update_bits_async(struct snd_soc_component
*component
,
882 unsigned int reg
, unsigned int mask
, unsigned int val
)
887 if (component
->regmap
)
888 ret
= regmap_update_bits_check_async(component
->regmap
, reg
,
891 ret
= snd_soc_component_update_bits_legacy(component
, reg
,
895 return soc_component_ret_reg_rw(component
, ret
, reg
);
898 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async
);
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
906 * Return: read value of register field.
908 unsigned int snd_soc_component_read_field(struct snd_soc_component
*component
,
909 unsigned int reg
, unsigned int mask
)
913 val
= snd_soc_component_read(component
, reg
);
915 val
= (val
& mask
) >> soc_component_field_shift(component
, mask
);
919 EXPORT_SYMBOL_GPL(snd_soc_component_read_field
);
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
928 * Return: 1 for change, otherwise 0.
930 int snd_soc_component_write_field(struct snd_soc_component
*component
,
931 unsigned int reg
, unsigned int mask
,
935 val
= (val
<< soc_component_field_shift(component
, mask
)) & mask
;
937 return snd_soc_component_update_bits(component
, reg
, mask
, val
);
939 EXPORT_SYMBOL_GPL(snd_soc_component_write_field
);
942 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
943 * @component: Component for which to wait
945 * This function blocks until all asynchronous I/O which has previously been
946 * scheduled using snd_soc_component_update_bits_async() has completed.
948 void snd_soc_component_async_complete(struct snd_soc_component
*component
)
950 if (component
->regmap
)
951 regmap_async_complete(component
->regmap
);
953 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete
);
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
962 * Tests a register with a new value and checks if the new value is
963 * different from the old value.
965 * Return: 1 for change, otherwise 0.
967 int snd_soc_component_test_bits(struct snd_soc_component
*component
,
968 unsigned int reg
, unsigned int mask
, unsigned int value
)
970 unsigned int old
, new;
972 old
= snd_soc_component_read(component
, reg
);
973 new = (old
& ~mask
) | value
;
976 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits
);
978 int snd_soc_pcm_component_pointer(struct snd_pcm_substream
*substream
)
980 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
981 struct snd_soc_component
*component
;
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
);
992 static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime
*rtd
,
993 struct snd_soc_component
*component
)
995 struct snd_soc_dai
*dai
;
998 for_each_rtd_codec_dais(rtd
, i
, dai
) {
999 if (dai
->component
== component
)
1006 void snd_soc_pcm_component_delay(struct snd_pcm_substream
*substream
,
1007 snd_pcm_sframes_t
*cpu_delay
,
1008 snd_pcm_sframes_t
*codec_delay
)
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
;
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.
1021 for_each_rtd_components(rtd
, i
, component
) {
1022 if (!component
->driver
->delay
)
1025 delay
= component
->driver
->delay(component
, substream
);
1027 if (snd_soc_component_is_codec_on_rtd(rtd
, component
))
1028 *codec_delay
= max(*codec_delay
, delay
);
1030 *cpu_delay
= max(*cpu_delay
, delay
);
1034 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream
*substream
,
1035 unsigned int cmd
, void *arg
)
1037 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1038 struct snd_soc_component
*component
;
1041 /* FIXME: use 1st ioctl */
1042 for_each_rtd_components(rtd
, i
, component
)
1043 if (component
->driver
->ioctl
)
1044 return soc_component_ret(
1046 component
->driver
->ioctl(component
,
1047 substream
, cmd
, arg
));
1049 return snd_pcm_lib_ioctl(substream
, cmd
, arg
);
1052 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream
*substream
)
1054 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1055 struct snd_soc_component
*component
;
1058 for_each_rtd_components(rtd
, i
, component
) {
1059 if (component
->driver
->sync_stop
) {
1060 ret
= component
->driver
->sync_stop(component
,
1063 return soc_component_ret(component
, ret
);
1070 int snd_soc_pcm_component_copy(struct snd_pcm_substream
*substream
,
1071 int channel
, unsigned long pos
,
1072 struct iov_iter
*iter
, unsigned long bytes
)
1074 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1075 struct snd_soc_component
*component
;
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
));
1088 struct page
*snd_soc_pcm_component_page(struct snd_pcm_substream
*substream
,
1089 unsigned long offset
)
1091 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1092 struct snd_soc_component
*component
;
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
,
1109 int snd_soc_pcm_component_mmap(struct snd_pcm_substream
*substream
,
1110 struct vm_area_struct
*vma
)
1112 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1113 struct snd_soc_component
*component
;
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(
1121 component
->driver
->mmap(component
,
1127 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime
*rtd
)
1129 struct snd_soc_component
*component
;
1133 for_each_rtd_components(rtd
, i
, component
) {
1134 if (component
->driver
->pcm_construct
) {
1135 ret
= component
->driver
->pcm_construct(component
, rtd
);
1137 return soc_component_ret(component
, ret
);
1144 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime
*rtd
)
1146 struct snd_soc_component
*component
;
1152 for_each_rtd_components(rtd
, i
, component
)
1153 if (component
->driver
->pcm_destruct
)
1154 component
->driver
->pcm_destruct(component
, rtd
->pcm
);
1157 int snd_soc_pcm_component_prepare(struct snd_pcm_substream
*substream
)
1159 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1160 struct snd_soc_component
*component
;
1163 for_each_rtd_components(rtd
, i
, component
) {
1164 if (component
->driver
->prepare
) {
1165 ret
= component
->driver
->prepare(component
, substream
);
1167 return soc_component_ret(component
, ret
);
1174 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream
*substream
,
1175 struct snd_pcm_hw_params
*params
)
1177 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1178 struct snd_soc_component
*component
;
1181 for_each_rtd_components(rtd
, i
, component
) {
1182 if (component
->driver
->hw_params
) {
1183 ret
= component
->driver
->hw_params(component
,
1186 return soc_component_ret(component
, ret
);
1188 /* mark substream if succeeded */
1189 soc_component_mark_push(component
, substream
, hw_params
);
1195 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream
*substream
,
1198 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1199 struct snd_soc_component
*component
;
1202 for_each_rtd_components(rtd
, i
, component
) {
1203 if (rollback
&& !soc_component_mark_match(component
, substream
, hw_params
))
1206 if (component
->driver
->hw_free
) {
1207 ret
= component
->driver
->hw_free(component
, substream
);
1209 soc_component_ret(component
, ret
);
1212 /* remove marked substream */
1213 soc_component_mark_pop(component
, hw_params
);
1217 static int soc_component_trigger(struct snd_soc_component
*component
,
1218 struct snd_pcm_substream
*substream
,
1223 if (component
->driver
->trigger
)
1224 ret
= component
->driver
->trigger(component
, substream
, cmd
);
1226 return soc_component_ret(component
, ret
);
1229 int snd_soc_pcm_component_trigger(struct snd_pcm_substream
*substream
,
1230 int cmd
, int rollback
)
1232 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1233 struct snd_soc_component
*component
;
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
);
1244 soc_component_mark_push(component
, substream
, trigger
);
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
))
1254 r
= soc_component_trigger(component
, substream
, cmd
);
1256 ret
= r
; /* use last ret */
1257 soc_component_mark_pop(component
, trigger
);
1264 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime
*rtd
,
1267 struct snd_soc_component
*component
;
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
);
1276 /* mark stream if succeeded */
1277 soc_component_mark_push(component
, stream
, pm
);
1283 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime
*rtd
,
1284 void *stream
, int rollback
)
1286 struct snd_soc_component
*component
;
1289 for_each_rtd_components(rtd
, i
, component
) {
1290 if (rollback
&& !soc_component_mark_match(component
, stream
, pm
))
1293 pm_runtime_mark_last_busy(component
->dev
);
1294 pm_runtime_put_autosuspend(component
->dev
);
1296 /* remove marked stream */
1297 soc_component_mark_pop(component
, pm
);
1301 int snd_soc_pcm_component_ack(struct snd_pcm_substream
*substream
)
1303 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
1304 struct snd_soc_component
*component
;
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
);