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>
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 */
23 /* Negative values might be errors */
29 dev_err(component->dev,
30 "ASoC: error at %s on %s: %d\n",
31 func, component->name, 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)
56 ret = component->init(component);
58 return soc_component_ret(component, ret);
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,
77 if (component->driver->set_sysclk)
78 ret = component->driver->set_sysclk(component, clk_id, source,
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)
101 if (component->driver->set_pll)
102 ret = component->driver->set_pll(component, pll_id, source,
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,
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)
132 if (component->driver->set_bias_level)
133 ret = component->driver->set_bias_level(component, level);
135 return soc_component_ret(component, ret);
138 int snd_soc_component_enable_pin(struct snd_soc_component *component,
141 struct snd_soc_dapm_context *dapm =
142 snd_soc_component_get_dapm(component);
143 return snd_soc_dapm_enable_pin(dapm, pin);
145 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
147 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
150 struct snd_soc_dapm_context *dapm =
151 snd_soc_component_get_dapm(component);
152 return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
154 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
156 int snd_soc_component_disable_pin(struct snd_soc_component *component,
159 struct snd_soc_dapm_context *dapm =
160 snd_soc_component_get_dapm(component);
161 return snd_soc_dapm_disable_pin(dapm, pin);
163 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
165 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
168 struct snd_soc_dapm_context *dapm =
169 snd_soc_component_get_dapm(component);
170 return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
172 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
174 int snd_soc_component_nc_pin(struct snd_soc_component *component,
177 struct snd_soc_dapm_context *dapm =
178 snd_soc_component_get_dapm(component);
179 return snd_soc_dapm_nc_pin(dapm, pin);
181 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
183 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
186 struct snd_soc_dapm_context *dapm =
187 snd_soc_component_get_dapm(component);
188 return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
190 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
192 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
195 struct snd_soc_dapm_context *dapm =
196 snd_soc_component_get_dapm(component);
197 return snd_soc_dapm_get_pin_status(dapm, pin);
199 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
201 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
204 struct snd_soc_dapm_context *dapm =
205 snd_soc_component_get_dapm(component);
206 return snd_soc_dapm_force_enable_pin(dapm, pin);
208 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
210 int snd_soc_component_force_enable_pin_unlocked(
211 struct snd_soc_component *component,
214 struct snd_soc_dapm_context *dapm =
215 snd_soc_component_get_dapm(component);
216 return snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
218 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
221 * snd_soc_component_set_jack - configure component jack.
222 * @component: COMPONENTs
223 * @jack: structure to use for the jack
224 * @data: can be used if codec driver need extra data for configuring jack
226 * Configures and enables jack detection function.
228 int snd_soc_component_set_jack(struct snd_soc_component *component,
229 struct snd_soc_jack *jack, void *data)
233 if (component->driver->set_jack)
234 ret = component->driver->set_jack(component, jack, data);
236 return soc_component_ret(component, ret);
238 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
240 int snd_soc_component_module_get(struct snd_soc_component *component,
241 struct snd_pcm_substream *substream,
246 if (component->driver->module_get_upon_open == !!upon_open &&
247 !try_module_get(component->dev->driver->owner))
250 /* mark substream if succeeded */
252 soc_component_mark_push(component, substream, module);
254 return soc_component_ret(component, ret);
257 void snd_soc_component_module_put(struct snd_soc_component *component,
258 struct snd_pcm_substream *substream,
259 int upon_open, int rollback)
261 if (rollback && !soc_component_mark_match(component, substream, module))
264 if (component->driver->module_get_upon_open == !!upon_open)
265 module_put(component->dev->driver->owner);
267 /* remove marked substream */
268 soc_component_mark_pop(component, substream, module);
271 int snd_soc_component_open(struct snd_soc_component *component,
272 struct snd_pcm_substream *substream)
276 if (component->driver->open)
277 ret = component->driver->open(component, substream);
279 /* mark substream if succeeded */
281 soc_component_mark_push(component, substream, open);
283 return soc_component_ret(component, ret);
286 int snd_soc_component_close(struct snd_soc_component *component,
287 struct snd_pcm_substream *substream,
292 if (rollback && !soc_component_mark_match(component, substream, open))
295 if (component->driver->close)
296 ret = component->driver->close(component, substream);
298 /* remove marked substream */
299 soc_component_mark_pop(component, substream, open);
301 return soc_component_ret(component, ret);
304 void snd_soc_component_suspend(struct snd_soc_component *component)
306 if (component->driver->suspend)
307 component->driver->suspend(component);
308 component->suspended = 1;
311 void snd_soc_component_resume(struct snd_soc_component *component)
313 if (component->driver->resume)
314 component->driver->resume(component);
315 component->suspended = 0;
318 int snd_soc_component_is_suspended(struct snd_soc_component *component)
320 return component->suspended;
323 int snd_soc_component_probe(struct snd_soc_component *component)
327 if (component->driver->probe)
328 ret = component->driver->probe(component);
330 return soc_component_ret(component, ret);
333 void snd_soc_component_remove(struct snd_soc_component *component)
335 if (component->driver->remove)
336 component->driver->remove(component);
339 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
340 struct device_node *ep)
344 if (component->driver->of_xlate_dai_id)
345 ret = component->driver->of_xlate_dai_id(component, ep);
347 return soc_component_ret(component, ret);
350 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
351 struct of_phandle_args *args,
352 const char **dai_name)
354 if (component->driver->of_xlate_dai_name)
355 return component->driver->of_xlate_dai_name(component,
358 * Don't use soc_component_ret here because we may not want to report
359 * the error just yet. If a device has more than one component, the
360 * first may not match and we don't want spam the log with this.
365 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
367 int val_bytes = regmap_get_val_bytes(component->regmap);
369 /* Errors are legitimate for non-integer byte multiples */
371 component->val_bytes = val_bytes;
377 * snd_soc_component_init_regmap() - Initialize regmap instance for the
379 * @component: The component for which to initialize the regmap instance
380 * @regmap: The regmap instance that should be used by the component
382 * This function allows deferred assignment of the regmap instance that is
383 * associated with the component. Only use this if the regmap instance is not
384 * yet ready when the component is registered. The function must also be called
385 * before the first IO attempt of the component.
387 void snd_soc_component_init_regmap(struct snd_soc_component *component,
388 struct regmap *regmap)
390 component->regmap = regmap;
391 snd_soc_component_setup_regmap(component);
393 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
396 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
398 * @component: The component for which to de-initialize the regmap instance
400 * Calls regmap_exit() on the regmap instance associated to the component and
401 * removes the regmap instance from the component.
403 * This function should only be used if snd_soc_component_init_regmap() was used
404 * to initialize the regmap instance.
406 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
408 regmap_exit(component->regmap);
409 component->regmap = NULL;
411 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
415 static unsigned int soc_component_read_no_lock(
416 struct snd_soc_component *component,
420 unsigned int val = 0;
422 if (component->regmap)
423 ret = regmap_read(component->regmap, reg, &val);
424 else if (component->driver->read) {
426 val = component->driver->read(component, reg);
432 return soc_component_ret(component, ret);
438 * snd_soc_component_read() - Read register value
439 * @component: Component to read from
440 * @reg: Register to read
444 unsigned int snd_soc_component_read(struct snd_soc_component *component,
449 mutex_lock(&component->io_mutex);
450 val = soc_component_read_no_lock(component, reg);
451 mutex_unlock(&component->io_mutex);
455 EXPORT_SYMBOL_GPL(snd_soc_component_read);
457 static int soc_component_write_no_lock(
458 struct snd_soc_component *component,
459 unsigned int reg, unsigned int val)
463 if (component->regmap)
464 ret = regmap_write(component->regmap, reg, val);
465 else if (component->driver->write)
466 ret = component->driver->write(component, reg, val);
468 return soc_component_ret(component, ret);
472 * snd_soc_component_write() - Write register value
473 * @component: Component to write to
474 * @reg: Register to write
475 * @val: Value to write to the register
477 * Return: 0 on success, a negative error code otherwise.
479 int snd_soc_component_write(struct snd_soc_component *component,
480 unsigned int reg, unsigned int val)
484 mutex_lock(&component->io_mutex);
485 ret = soc_component_write_no_lock(component, reg, val);
486 mutex_unlock(&component->io_mutex);
490 EXPORT_SYMBOL_GPL(snd_soc_component_write);
492 static int snd_soc_component_update_bits_legacy(
493 struct snd_soc_component *component, unsigned int reg,
494 unsigned int mask, unsigned int val, bool *change)
496 unsigned int old, new;
499 mutex_lock(&component->io_mutex);
501 old = soc_component_read_no_lock(component, reg);
503 new = (old & ~mask) | (val & mask);
504 *change = old != new;
506 ret = soc_component_write_no_lock(component, reg, new);
508 mutex_unlock(&component->io_mutex);
510 return soc_component_ret(component, ret);
514 * snd_soc_component_update_bits() - Perform read/modify/write cycle
515 * @component: Component to update
516 * @reg: Register to update
517 * @mask: Mask that specifies which bits to update
518 * @val: New value for the bits specified by mask
520 * Return: 1 if the operation was successful and the value of the register
521 * changed, 0 if the operation was successful, but the value did not change.
522 * Returns a negative error code otherwise.
524 int snd_soc_component_update_bits(struct snd_soc_component *component,
525 unsigned int reg, unsigned int mask, unsigned int val)
530 if (component->regmap)
531 ret = regmap_update_bits_check(component->regmap, reg, mask,
534 ret = snd_soc_component_update_bits_legacy(component, reg,
538 return soc_component_ret(component, ret);
541 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
544 * snd_soc_component_update_bits_async() - Perform asynchronous
545 * read/modify/write cycle
546 * @component: Component to update
547 * @reg: Register to update
548 * @mask: Mask that specifies which bits to update
549 * @val: New value for the bits specified by mask
551 * This function is similar to snd_soc_component_update_bits(), but the update
552 * operation is scheduled asynchronously. This means it may not be completed
553 * when the function returns. To make sure that all scheduled updates have been
554 * completed snd_soc_component_async_complete() must be called.
556 * Return: 1 if the operation was successful and the value of the register
557 * changed, 0 if the operation was successful, but the value did not change.
558 * Returns a negative error code otherwise.
560 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
561 unsigned int reg, unsigned int mask, unsigned int val)
566 if (component->regmap)
567 ret = regmap_update_bits_check_async(component->regmap, reg,
570 ret = snd_soc_component_update_bits_legacy(component, reg,
574 return soc_component_ret(component, ret);
577 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
580 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
581 * @component: Component for which to wait
583 * This function blocks until all asynchronous I/O which has previously been
584 * scheduled using snd_soc_component_update_bits_async() has completed.
586 void snd_soc_component_async_complete(struct snd_soc_component *component)
588 if (component->regmap)
589 regmap_async_complete(component->regmap);
591 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
594 * snd_soc_component_test_bits - Test register for change
595 * @component: component
596 * @reg: Register to test
597 * @mask: Mask that specifies which bits to test
598 * @value: Value to test against
600 * Tests a register with a new value and checks if the new value is
601 * different from the old value.
603 * Return: 1 for change, otherwise 0.
605 int snd_soc_component_test_bits(struct snd_soc_component *component,
606 unsigned int reg, unsigned int mask, unsigned int value)
608 unsigned int old, new;
610 old = snd_soc_component_read(component, reg);
611 new = (old & ~mask) | value;
614 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
616 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
618 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
619 struct snd_soc_component *component;
622 /* FIXME: use 1st pointer */
623 for_each_rtd_components(rtd, i, component)
624 if (component->driver->pointer)
625 return component->driver->pointer(component, substream);
630 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
631 unsigned int cmd, void *arg)
633 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
634 struct snd_soc_component *component;
637 /* FIXME: use 1st ioctl */
638 for_each_rtd_components(rtd, i, component)
639 if (component->driver->ioctl)
640 return soc_component_ret(
642 component->driver->ioctl(component,
643 substream, cmd, arg));
645 return snd_pcm_lib_ioctl(substream, cmd, arg);
648 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
650 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
651 struct snd_soc_component *component;
654 for_each_rtd_components(rtd, i, component) {
655 if (component->driver->sync_stop) {
656 ret = component->driver->sync_stop(component,
659 return soc_component_ret(component, ret);
666 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
667 int channel, unsigned long pos,
668 void __user *buf, unsigned long bytes)
670 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
671 struct snd_soc_component *component;
674 /* FIXME. it returns 1st copy now */
675 for_each_rtd_components(rtd, i, component)
676 if (component->driver->copy_user)
677 return soc_component_ret(
679 component->driver->copy_user(
680 component, substream, channel,
686 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
687 unsigned long offset)
689 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
690 struct snd_soc_component *component;
694 /* FIXME. it returns 1st page now */
695 for_each_rtd_components(rtd, i, component) {
696 if (component->driver->page) {
697 page = component->driver->page(component,
707 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
708 struct vm_area_struct *vma)
710 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
711 struct snd_soc_component *component;
714 /* FIXME. it returns 1st mmap now */
715 for_each_rtd_components(rtd, i, component)
716 if (component->driver->mmap)
717 return soc_component_ret(
719 component->driver->mmap(component,
725 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
727 struct snd_soc_component *component;
731 for_each_rtd_components(rtd, i, component) {
732 if (component->driver->pcm_construct) {
733 ret = component->driver->pcm_construct(component, rtd);
735 return soc_component_ret(component, ret);
742 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
744 struct snd_soc_component *component;
750 for_each_rtd_components(rtd, i, component)
751 if (component->driver->pcm_destruct)
752 component->driver->pcm_destruct(component, rtd->pcm);
755 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
757 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
758 struct snd_soc_component *component;
761 for_each_rtd_components(rtd, i, component) {
762 if (component->driver->prepare) {
763 ret = component->driver->prepare(component, substream);
765 return soc_component_ret(component, ret);
772 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
773 struct snd_pcm_hw_params *params,
774 struct snd_soc_component **last)
776 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
777 struct snd_soc_component *component;
780 for_each_rtd_components(rtd, i, component) {
781 if (component->driver->hw_params) {
782 ret = component->driver->hw_params(component,
786 return soc_component_ret(component, ret);
795 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
796 struct snd_soc_component *last)
798 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
799 struct snd_soc_component *component;
802 for_each_rtd_components(rtd, i, component) {
803 if (component == last)
806 if (component->driver->hw_free) {
807 ret = component->driver->hw_free(component, substream);
809 soc_component_ret(component, ret);
814 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
817 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
818 struct snd_soc_component *component;
821 for_each_rtd_components(rtd, i, component) {
822 if (component->driver->trigger) {
823 ret = component->driver->trigger(component, substream, cmd);
825 return soc_component_ret(component, ret);
832 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
835 struct snd_soc_component *component;
838 for_each_rtd_components(rtd, i, component) {
839 ret = pm_runtime_get_sync(component->dev);
840 if (ret < 0 && ret != -EACCES) {
841 pm_runtime_put_noidle(component->dev);
842 return soc_component_ret(component, ret);
844 /* mark stream if succeeded */
845 soc_component_mark_push(component, stream, pm);
851 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
852 void *stream, int rollback)
854 struct snd_soc_component *component;
857 for_each_rtd_components(rtd, i, component) {
858 if (rollback && !soc_component_mark_match(component, stream, pm))
861 pm_runtime_mark_last_busy(component->dev);
862 pm_runtime_put_autosuspend(component->dev);
864 /* remove marked stream */
865 soc_component_mark_pop(component, stream, pm);