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 <sound/soc.h>
14 #define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
15 static inline int _soc_component_ret(struct snd_soc_component *component,
16 const char *func, int ret)
18 /* Positive/Zero values are not errors */
22 /* Negative values might be errors */
28 dev_err(component->dev,
29 "ASoC: error at %s on %s: %d\n",
30 func, component->name, ret);
36 void snd_soc_component_set_aux(struct snd_soc_component *component,
37 struct snd_soc_aux_dev *aux)
39 component->init = (aux) ? aux->init : NULL;
42 int snd_soc_component_init(struct snd_soc_component *component)
47 ret = component->init(component);
49 return soc_component_ret(component, ret);
53 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
54 * @component: COMPONENT
55 * @clk_id: DAI specific clock ID
56 * @source: Source for the clock
57 * @freq: new clock frequency in Hz
58 * @dir: new clock direction - input/output.
60 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
62 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
63 int clk_id, int source, unsigned int freq,
68 if (component->driver->set_sysclk)
69 ret = component->driver->set_sysclk(component, clk_id, source,
72 return soc_component_ret(component, ret);
74 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
77 * snd_soc_component_set_pll - configure component PLL.
78 * @component: COMPONENT
79 * @pll_id: DAI specific PLL ID
80 * @source: DAI specific source for the PLL
81 * @freq_in: PLL input clock frequency in Hz
82 * @freq_out: requested PLL output clock frequency in Hz
84 * Configures and enables PLL to generate output clock based on input clock.
86 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
87 int source, unsigned int freq_in,
88 unsigned int freq_out)
92 if (component->driver->set_pll)
93 ret = component->driver->set_pll(component, pll_id, source,
96 return soc_component_ret(component, ret);
98 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
100 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
101 enum snd_soc_dapm_type type, int subseq)
103 if (component->driver->seq_notifier)
104 component->driver->seq_notifier(component, type, subseq);
107 int snd_soc_component_stream_event(struct snd_soc_component *component,
112 if (component->driver->stream_event)
113 ret = component->driver->stream_event(component, event);
115 return soc_component_ret(component, ret);
118 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
119 enum snd_soc_bias_level level)
123 if (component->driver->set_bias_level)
124 ret = component->driver->set_bias_level(component, level);
126 return soc_component_ret(component, ret);
129 static int soc_component_pin(struct snd_soc_component *component,
131 int (*pin_func)(struct snd_soc_dapm_context *dapm,
134 struct snd_soc_dapm_context *dapm =
135 snd_soc_component_get_dapm(component);
139 if (!component->name_prefix) {
140 ret = pin_func(dapm, pin);
144 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
150 ret = pin_func(dapm, full_name);
153 return soc_component_ret(component, ret);
156 int snd_soc_component_enable_pin(struct snd_soc_component *component,
159 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
161 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
163 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
166 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
168 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
170 int snd_soc_component_disable_pin(struct snd_soc_component *component,
173 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
175 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
177 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
180 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
182 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
184 int snd_soc_component_nc_pin(struct snd_soc_component *component,
187 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
189 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
191 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
194 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
196 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
198 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
201 return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
203 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
205 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
208 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
210 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
212 int snd_soc_component_force_enable_pin_unlocked(
213 struct snd_soc_component *component,
216 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
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,
245 if (component->driver->module_get_upon_open == !!upon_open &&
246 !try_module_get(component->dev->driver->owner))
249 return soc_component_ret(component, ret);
252 void snd_soc_component_module_put(struct snd_soc_component *component,
255 if (component->driver->module_get_upon_open == !!upon_open)
256 module_put(component->dev->driver->owner);
259 int snd_soc_component_open(struct snd_soc_component *component,
260 struct snd_pcm_substream *substream)
264 if (component->driver->open)
265 ret = component->driver->open(component, substream);
267 return soc_component_ret(component, ret);
270 int snd_soc_component_close(struct snd_soc_component *component,
271 struct snd_pcm_substream *substream)
275 if (component->driver->close)
276 ret = component->driver->close(component, substream);
278 return soc_component_ret(component, ret);
281 void snd_soc_component_suspend(struct snd_soc_component *component)
283 if (component->driver->suspend)
284 component->driver->suspend(component);
285 component->suspended = 1;
288 void snd_soc_component_resume(struct snd_soc_component *component)
290 if (component->driver->resume)
291 component->driver->resume(component);
292 component->suspended = 0;
295 int snd_soc_component_is_suspended(struct snd_soc_component *component)
297 return component->suspended;
300 int snd_soc_component_probe(struct snd_soc_component *component)
304 if (component->driver->probe)
305 ret = component->driver->probe(component);
307 return soc_component_ret(component, ret);
310 void snd_soc_component_remove(struct snd_soc_component *component)
312 if (component->driver->remove)
313 component->driver->remove(component);
316 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
317 struct device_node *ep)
321 if (component->driver->of_xlate_dai_id)
322 ret = component->driver->of_xlate_dai_id(component, ep);
324 return soc_component_ret(component, ret);
327 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
328 struct of_phandle_args *args,
329 const char **dai_name)
331 if (component->driver->of_xlate_dai_name)
332 return component->driver->of_xlate_dai_name(component,
335 * Don't use soc_component_ret here because we may not want to report
336 * the error just yet. If a device has more than one component, the
337 * first may not match and we don't want spam the log with this.
342 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
344 int val_bytes = regmap_get_val_bytes(component->regmap);
346 /* Errors are legitimate for non-integer byte multiples */
348 component->val_bytes = val_bytes;
354 * snd_soc_component_init_regmap() - Initialize regmap instance for the
356 * @component: The component for which to initialize the regmap instance
357 * @regmap: The regmap instance that should be used by the component
359 * This function allows deferred assignment of the regmap instance that is
360 * associated with the component. Only use this if the regmap instance is not
361 * yet ready when the component is registered. The function must also be called
362 * before the first IO attempt of the component.
364 void snd_soc_component_init_regmap(struct snd_soc_component *component,
365 struct regmap *regmap)
367 component->regmap = regmap;
368 snd_soc_component_setup_regmap(component);
370 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
373 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
375 * @component: The component for which to de-initialize the regmap instance
377 * Calls regmap_exit() on the regmap instance associated to the component and
378 * removes the regmap instance from the component.
380 * This function should only be used if snd_soc_component_init_regmap() was used
381 * to initialize the regmap instance.
383 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
385 regmap_exit(component->regmap);
386 component->regmap = NULL;
388 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
392 static unsigned int soc_component_read_no_lock(
393 struct snd_soc_component *component,
397 unsigned int val = 0;
399 if (component->regmap)
400 ret = regmap_read(component->regmap, reg, &val);
401 else if (component->driver->read) {
403 val = component->driver->read(component, reg);
409 soc_component_ret(component, ret);
415 * snd_soc_component_read() - Read register value
416 * @component: Component to read from
417 * @reg: Register to read
421 unsigned int snd_soc_component_read(struct snd_soc_component *component,
426 mutex_lock(&component->io_mutex);
427 val = soc_component_read_no_lock(component, reg);
428 mutex_unlock(&component->io_mutex);
432 EXPORT_SYMBOL_GPL(snd_soc_component_read);
434 static int soc_component_write_no_lock(
435 struct snd_soc_component *component,
436 unsigned int reg, unsigned int val)
440 if (component->regmap)
441 ret = regmap_write(component->regmap, reg, val);
442 else if (component->driver->write)
443 ret = component->driver->write(component, reg, val);
445 return soc_component_ret(component, ret);
449 * snd_soc_component_write() - Write register value
450 * @component: Component to write to
451 * @reg: Register to write
452 * @val: Value to write to the register
454 * Return: 0 on success, a negative error code otherwise.
456 int snd_soc_component_write(struct snd_soc_component *component,
457 unsigned int reg, unsigned int val)
461 mutex_lock(&component->io_mutex);
462 ret = soc_component_write_no_lock(component, reg, val);
463 mutex_unlock(&component->io_mutex);
467 EXPORT_SYMBOL_GPL(snd_soc_component_write);
469 static int snd_soc_component_update_bits_legacy(
470 struct snd_soc_component *component, unsigned int reg,
471 unsigned int mask, unsigned int val, bool *change)
473 unsigned int old, new;
476 mutex_lock(&component->io_mutex);
478 old = soc_component_read_no_lock(component, reg);
480 new = (old & ~mask) | (val & mask);
481 *change = old != new;
483 ret = soc_component_write_no_lock(component, reg, new);
485 mutex_unlock(&component->io_mutex);
487 return soc_component_ret(component, ret);
491 * snd_soc_component_update_bits() - Perform read/modify/write cycle
492 * @component: Component to update
493 * @reg: Register to update
494 * @mask: Mask that specifies which bits to update
495 * @val: New value for the bits specified by mask
497 * Return: 1 if the operation was successful and the value of the register
498 * changed, 0 if the operation was successful, but the value did not change.
499 * Returns a negative error code otherwise.
501 int snd_soc_component_update_bits(struct snd_soc_component *component,
502 unsigned int reg, unsigned int mask, unsigned int val)
507 if (component->regmap)
508 ret = regmap_update_bits_check(component->regmap, reg, mask,
511 ret = snd_soc_component_update_bits_legacy(component, reg,
515 return soc_component_ret(component, ret);
518 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
521 * snd_soc_component_update_bits_async() - Perform asynchronous
522 * read/modify/write cycle
523 * @component: Component to update
524 * @reg: Register to update
525 * @mask: Mask that specifies which bits to update
526 * @val: New value for the bits specified by mask
528 * This function is similar to snd_soc_component_update_bits(), but the update
529 * operation is scheduled asynchronously. This means it may not be completed
530 * when the function returns. To make sure that all scheduled updates have been
531 * completed snd_soc_component_async_complete() must be called.
533 * Return: 1 if the operation was successful and the value of the register
534 * changed, 0 if the operation was successful, but the value did not change.
535 * Returns a negative error code otherwise.
537 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
538 unsigned int reg, unsigned int mask, unsigned int val)
543 if (component->regmap)
544 ret = regmap_update_bits_check_async(component->regmap, reg,
547 ret = snd_soc_component_update_bits_legacy(component, reg,
551 return soc_component_ret(component, ret);
554 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
557 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
558 * @component: Component for which to wait
560 * This function blocks until all asynchronous I/O which has previously been
561 * scheduled using snd_soc_component_update_bits_async() has completed.
563 void snd_soc_component_async_complete(struct snd_soc_component *component)
565 if (component->regmap)
566 regmap_async_complete(component->regmap);
568 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
571 * snd_soc_component_test_bits - Test register for change
572 * @component: component
573 * @reg: Register to test
574 * @mask: Mask that specifies which bits to test
575 * @value: Value to test against
577 * Tests a register with a new value and checks if the new value is
578 * different from the old value.
580 * Return: 1 for change, otherwise 0.
582 int snd_soc_component_test_bits(struct snd_soc_component *component,
583 unsigned int reg, unsigned int mask, unsigned int value)
585 unsigned int old, new;
587 old = snd_soc_component_read(component, reg);
588 new = (old & ~mask) | value;
591 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
593 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
595 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
596 struct snd_soc_component *component;
599 /* FIXME: use 1st pointer */
600 for_each_rtd_components(rtd, i, component)
601 if (component->driver->pointer)
602 return component->driver->pointer(component, substream);
607 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
608 unsigned int cmd, void *arg)
610 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
611 struct snd_soc_component *component;
614 /* FIXME: use 1st ioctl */
615 for_each_rtd_components(rtd, i, component)
616 if (component->driver->ioctl)
617 return soc_component_ret(
619 component->driver->ioctl(component,
620 substream, cmd, arg));
622 return snd_pcm_lib_ioctl(substream, cmd, arg);
625 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
627 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
628 struct snd_soc_component *component;
631 for_each_rtd_components(rtd, i, component) {
632 if (component->driver->sync_stop) {
633 ret = component->driver->sync_stop(component,
636 return soc_component_ret(component, ret);
643 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
644 int channel, unsigned long pos,
645 void __user *buf, unsigned long bytes)
647 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
648 struct snd_soc_component *component;
651 /* FIXME. it returns 1st copy now */
652 for_each_rtd_components(rtd, i, component)
653 if (component->driver->copy_user)
654 return soc_component_ret(
656 component->driver->copy_user(
657 component, substream, channel,
663 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
664 unsigned long offset)
666 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
667 struct snd_soc_component *component;
671 /* FIXME. it returns 1st page now */
672 for_each_rtd_components(rtd, i, component) {
673 if (component->driver->page) {
674 page = component->driver->page(component,
684 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
685 struct vm_area_struct *vma)
687 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
688 struct snd_soc_component *component;
691 /* FIXME. it returns 1st mmap now */
692 for_each_rtd_components(rtd, i, component)
693 if (component->driver->mmap)
694 return soc_component_ret(
696 component->driver->mmap(component,
702 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
704 struct snd_soc_component *component;
708 for_each_rtd_components(rtd, i, component) {
709 if (component->driver->pcm_construct) {
710 ret = component->driver->pcm_construct(component, rtd);
712 return soc_component_ret(component, ret);
719 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
721 struct snd_soc_component *component;
727 for_each_rtd_components(rtd, i, component)
728 if (component->driver->pcm_destruct)
729 component->driver->pcm_destruct(component, rtd->pcm);
732 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
734 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
735 struct snd_soc_component *component;
738 for_each_rtd_components(rtd, i, component) {
739 if (component->driver->prepare) {
740 ret = component->driver->prepare(component, substream);
742 return soc_component_ret(component, ret);
749 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
750 struct snd_pcm_hw_params *params,
751 struct snd_soc_component **last)
753 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
754 struct snd_soc_component *component;
757 for_each_rtd_components(rtd, i, component) {
758 if (component->driver->hw_params) {
759 ret = component->driver->hw_params(component,
763 return soc_component_ret(component, ret);
772 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
773 struct snd_soc_component *last)
775 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
776 struct snd_soc_component *component;
779 for_each_rtd_components(rtd, i, component) {
780 if (component == last)
783 if (component->driver->hw_free) {
784 ret = component->driver->hw_free(component, substream);
786 soc_component_ret(component, ret);
791 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
794 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
795 struct snd_soc_component *component;
798 for_each_rtd_components(rtd, i, component) {
799 if (component->driver->trigger) {
800 ret = component->driver->trigger(component, substream, cmd);
802 return soc_component_ret(component, ret);