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 int snd_soc_component_initialize(struct snd_soc_component *component,
37 const struct snd_soc_component_driver *driver,
38 struct device *dev, const char *name)
40 INIT_LIST_HEAD(&component->dai_list);
41 INIT_LIST_HEAD(&component->dobj_list);
42 INIT_LIST_HEAD(&component->card_list);
43 mutex_init(&component->io_mutex);
45 component->name = name;
47 component->driver = driver;
52 void snd_soc_component_set_aux(struct snd_soc_component *component,
53 struct snd_soc_aux_dev *aux)
55 component->init = (aux) ? aux->init : NULL;
58 int snd_soc_component_init(struct snd_soc_component *component)
63 ret = component->init(component);
65 return soc_component_ret(component, ret);
69 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
70 * @component: COMPONENT
71 * @clk_id: DAI specific clock ID
72 * @source: Source for the clock
73 * @freq: new clock frequency in Hz
74 * @dir: new clock direction - input/output.
76 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
78 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
79 int clk_id, int source, unsigned int freq,
84 if (component->driver->set_sysclk)
85 ret = component->driver->set_sysclk(component, clk_id, source,
88 return soc_component_ret(component, ret);
90 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
93 * snd_soc_component_set_pll - configure component PLL.
94 * @component: COMPONENT
95 * @pll_id: DAI specific PLL ID
96 * @source: DAI specific source for the PLL
97 * @freq_in: PLL input clock frequency in Hz
98 * @freq_out: requested PLL output clock frequency in Hz
100 * Configures and enables PLL to generate output clock based on input clock.
102 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
103 int source, unsigned int freq_in,
104 unsigned int freq_out)
108 if (component->driver->set_pll)
109 ret = component->driver->set_pll(component, pll_id, source,
112 return soc_component_ret(component, ret);
114 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
116 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
117 enum snd_soc_dapm_type type, int subseq)
119 if (component->driver->seq_notifier)
120 component->driver->seq_notifier(component, type, subseq);
123 int snd_soc_component_stream_event(struct snd_soc_component *component,
128 if (component->driver->stream_event)
129 ret = component->driver->stream_event(component, event);
131 return soc_component_ret(component, ret);
134 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
135 enum snd_soc_bias_level level)
139 if (component->driver->set_bias_level)
140 ret = component->driver->set_bias_level(component, level);
142 return soc_component_ret(component, ret);
145 static int soc_component_pin(struct snd_soc_component *component,
147 int (*pin_func)(struct snd_soc_dapm_context *dapm,
150 struct snd_soc_dapm_context *dapm =
151 snd_soc_component_get_dapm(component);
155 if (!component->name_prefix) {
156 ret = pin_func(dapm, pin);
160 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
166 ret = pin_func(dapm, full_name);
169 return soc_component_ret(component, ret);
172 int snd_soc_component_enable_pin(struct snd_soc_component *component,
175 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
177 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
179 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
182 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
184 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
186 int snd_soc_component_disable_pin(struct snd_soc_component *component,
189 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
191 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
193 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
196 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
198 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
200 int snd_soc_component_nc_pin(struct snd_soc_component *component,
203 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
205 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
207 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
210 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
212 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
214 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
217 return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
219 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
221 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
224 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
226 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
228 int snd_soc_component_force_enable_pin_unlocked(
229 struct snd_soc_component *component,
232 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
234 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
237 * snd_soc_component_set_jack - configure component jack.
238 * @component: COMPONENTs
239 * @jack: structure to use for the jack
240 * @data: can be used if codec driver need extra data for configuring jack
242 * Configures and enables jack detection function.
244 int snd_soc_component_set_jack(struct snd_soc_component *component,
245 struct snd_soc_jack *jack, void *data)
249 if (component->driver->set_jack)
250 ret = component->driver->set_jack(component, jack, data);
252 return soc_component_ret(component, ret);
254 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
256 int snd_soc_component_module_get(struct snd_soc_component *component,
261 if (component->driver->module_get_upon_open == !!upon_open &&
262 !try_module_get(component->dev->driver->owner))
265 return soc_component_ret(component, ret);
268 void snd_soc_component_module_put(struct snd_soc_component *component,
271 if (component->driver->module_get_upon_open == !!upon_open)
272 module_put(component->dev->driver->owner);
275 int snd_soc_component_open(struct snd_soc_component *component,
276 struct snd_pcm_substream *substream)
280 if (component->driver->open)
281 ret = component->driver->open(component, substream);
283 return soc_component_ret(component, ret);
286 int snd_soc_component_close(struct snd_soc_component *component,
287 struct snd_pcm_substream *substream)
291 if (component->driver->close)
292 ret = component->driver->close(component, substream);
294 return soc_component_ret(component, ret);
297 void snd_soc_component_suspend(struct snd_soc_component *component)
299 if (component->driver->suspend)
300 component->driver->suspend(component);
301 component->suspended = 1;
304 void snd_soc_component_resume(struct snd_soc_component *component)
306 if (component->driver->resume)
307 component->driver->resume(component);
308 component->suspended = 0;
311 int snd_soc_component_is_suspended(struct snd_soc_component *component)
313 return component->suspended;
316 int snd_soc_component_probe(struct snd_soc_component *component)
320 if (component->driver->probe)
321 ret = component->driver->probe(component);
323 return soc_component_ret(component, ret);
326 void snd_soc_component_remove(struct snd_soc_component *component)
328 if (component->driver->remove)
329 component->driver->remove(component);
332 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
333 struct device_node *ep)
337 if (component->driver->of_xlate_dai_id)
338 ret = component->driver->of_xlate_dai_id(component, ep);
340 return soc_component_ret(component, ret);
343 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
344 struct of_phandle_args *args,
345 const char **dai_name)
349 if (component->driver->of_xlate_dai_name)
350 ret = component->driver->of_xlate_dai_name(component,
353 return soc_component_ret(component, ret);
356 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
358 int val_bytes = regmap_get_val_bytes(component->regmap);
360 /* Errors are legitimate for non-integer byte multiples */
362 component->val_bytes = val_bytes;
368 * snd_soc_component_init_regmap() - Initialize regmap instance for the
370 * @component: The component for which to initialize the regmap instance
371 * @regmap: The regmap instance that should be used by the component
373 * This function allows deferred assignment of the regmap instance that is
374 * associated with the component. Only use this if the regmap instance is not
375 * yet ready when the component is registered. The function must also be called
376 * before the first IO attempt of the component.
378 void snd_soc_component_init_regmap(struct snd_soc_component *component,
379 struct regmap *regmap)
381 component->regmap = regmap;
382 snd_soc_component_setup_regmap(component);
384 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
387 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
389 * @component: The component for which to de-initialize the regmap instance
391 * Calls regmap_exit() on the regmap instance associated to the component and
392 * removes the regmap instance from the component.
394 * This function should only be used if snd_soc_component_init_regmap() was used
395 * to initialize the regmap instance.
397 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
399 regmap_exit(component->regmap);
400 component->regmap = NULL;
402 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
407 * snd_soc_component_read() - Read register value
408 * @component: Component to read from
409 * @reg: Register to read
410 * @val: Pointer to where the read value is stored
412 * Return: 0 on success, a negative error code otherwise.
414 int snd_soc_component_read(struct snd_soc_component *component,
415 unsigned int reg, unsigned int *val)
419 if (component->regmap)
420 ret = regmap_read(component->regmap, reg, val);
421 else if (component->driver->read) {
422 *val = component->driver->read(component, reg);
428 return soc_component_ret(component, ret);
430 EXPORT_SYMBOL_GPL(snd_soc_component_read);
432 unsigned int snd_soc_component_read32(struct snd_soc_component *component,
438 ret = snd_soc_component_read(component, reg, &val);
440 return soc_component_ret(component, -1);
444 EXPORT_SYMBOL_GPL(snd_soc_component_read32);
447 * snd_soc_component_write() - Write register value
448 * @component: Component to write to
449 * @reg: Register to write
450 * @val: Value to write to the register
452 * Return: 0 on success, a negative error code otherwise.
454 int snd_soc_component_write(struct snd_soc_component *component,
455 unsigned int reg, unsigned int val)
459 if (component->regmap)
460 ret = regmap_write(component->regmap, reg, val);
461 else if (component->driver->write)
462 ret = component->driver->write(component, reg, val);
464 return soc_component_ret(component, ret);
466 EXPORT_SYMBOL_GPL(snd_soc_component_write);
468 static int snd_soc_component_update_bits_legacy(
469 struct snd_soc_component *component, unsigned int reg,
470 unsigned int mask, unsigned int val, bool *change)
472 unsigned int old, new;
475 mutex_lock(&component->io_mutex);
477 ret = snd_soc_component_read(component, reg, &old);
481 new = (old & ~mask) | (val & mask);
482 *change = old != new;
484 ret = snd_soc_component_write(component, reg, new);
486 mutex_unlock(&component->io_mutex);
488 return soc_component_ret(component, ret);
492 * snd_soc_component_update_bits() - Perform read/modify/write cycle
493 * @component: Component to update
494 * @reg: Register to update
495 * @mask: Mask that specifies which bits to update
496 * @val: New value for the bits specified by mask
498 * Return: 1 if the operation was successful and the value of the register
499 * changed, 0 if the operation was successful, but the value did not change.
500 * Returns a negative error code otherwise.
502 int snd_soc_component_update_bits(struct snd_soc_component *component,
503 unsigned int reg, unsigned int mask, unsigned int val)
508 if (component->regmap)
509 ret = regmap_update_bits_check(component->regmap, reg, mask,
512 ret = snd_soc_component_update_bits_legacy(component, reg,
516 return soc_component_ret(component, ret);
519 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
522 * snd_soc_component_update_bits_async() - Perform asynchronous
523 * read/modify/write cycle
524 * @component: Component to update
525 * @reg: Register to update
526 * @mask: Mask that specifies which bits to update
527 * @val: New value for the bits specified by mask
529 * This function is similar to snd_soc_component_update_bits(), but the update
530 * operation is scheduled asynchronously. This means it may not be completed
531 * when the function returns. To make sure that all scheduled updates have been
532 * completed snd_soc_component_async_complete() must be called.
534 * Return: 1 if the operation was successful and the value of the register
535 * changed, 0 if the operation was successful, but the value did not change.
536 * Returns a negative error code otherwise.
538 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
539 unsigned int reg, unsigned int mask, unsigned int val)
544 if (component->regmap)
545 ret = regmap_update_bits_check_async(component->regmap, reg,
548 ret = snd_soc_component_update_bits_legacy(component, reg,
552 return soc_component_ret(component, ret);
555 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
558 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
559 * @component: Component for which to wait
561 * This function blocks until all asynchronous I/O which has previously been
562 * scheduled using snd_soc_component_update_bits_async() has completed.
564 void snd_soc_component_async_complete(struct snd_soc_component *component)
566 if (component->regmap)
567 regmap_async_complete(component->regmap);
569 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
572 * snd_soc_component_test_bits - Test register for change
573 * @component: component
574 * @reg: Register to test
575 * @mask: Mask that specifies which bits to test
576 * @value: Value to test against
578 * Tests a register with a new value and checks if the new value is
579 * different from the old value.
581 * Return: 1 for change, otherwise 0.
583 int snd_soc_component_test_bits(struct snd_soc_component *component,
584 unsigned int reg, unsigned int mask, unsigned int value)
586 unsigned int old, new;
589 ret = snd_soc_component_read(component, reg, &old);
591 return soc_component_ret(component, ret);
592 new = (old & ~mask) | value;
595 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
597 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
599 struct snd_soc_pcm_runtime *rtd = substream->private_data;
600 struct snd_soc_component *component;
603 /* FIXME: use 1st pointer */
604 for_each_rtd_components(rtd, i, component)
605 if (component->driver->pointer)
606 return component->driver->pointer(component, substream);
611 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
612 unsigned int cmd, void *arg)
614 struct snd_soc_pcm_runtime *rtd = substream->private_data;
615 struct snd_soc_component *component;
618 /* FIXME: use 1st ioctl */
619 for_each_rtd_components(rtd, i, component)
620 if (component->driver->ioctl)
621 return soc_component_ret(
623 component->driver->ioctl(component,
624 substream, cmd, arg));
626 return snd_pcm_lib_ioctl(substream, cmd, arg);
629 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
631 struct snd_soc_pcm_runtime *rtd = substream->private_data;
632 struct snd_soc_component *component;
635 for_each_rtd_components(rtd, i, component) {
636 if (component->driver->sync_stop) {
637 ret = component->driver->sync_stop(component,
640 soc_component_ret(component, ret);
647 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
648 int channel, unsigned long pos,
649 void __user *buf, unsigned long bytes)
651 struct snd_soc_pcm_runtime *rtd = substream->private_data;
652 struct snd_soc_component *component;
655 /* FIXME. it returns 1st copy now */
656 for_each_rtd_components(rtd, i, component)
657 if (component->driver->copy_user)
658 return soc_component_ret(
660 component->driver->copy_user(
661 component, substream, channel,
667 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
668 unsigned long offset)
670 struct snd_soc_pcm_runtime *rtd = substream->private_data;
671 struct snd_soc_component *component;
675 /* FIXME. it returns 1st page now */
676 for_each_rtd_components(rtd, i, component) {
677 if (component->driver->page) {
678 page = component->driver->page(component,
688 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
689 struct vm_area_struct *vma)
691 struct snd_soc_pcm_runtime *rtd = substream->private_data;
692 struct snd_soc_component *component;
695 /* FIXME. it returns 1st mmap now */
696 for_each_rtd_components(rtd, i, component)
697 if (component->driver->mmap)
700 component->driver->mmap(component,
706 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
708 struct snd_soc_component *component;
712 for_each_rtd_components(rtd, i, component) {
713 if (component->driver->pcm_construct) {
714 ret = component->driver->pcm_construct(component, rtd);
716 soc_component_ret(component, ret);
723 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
725 struct snd_soc_component *component;
731 for_each_rtd_components(rtd, i, component)
732 if (component->driver->pcm_destruct)
733 component->driver->pcm_destruct(component, rtd->pcm);
736 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
738 struct snd_soc_pcm_runtime *rtd = substream->private_data;
739 struct snd_soc_component *component;
742 for_each_rtd_components(rtd, i, component) {
743 if (component->driver->prepare) {
744 ret = component->driver->prepare(component, substream);
746 return soc_component_ret(component, ret);
753 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
754 struct snd_pcm_hw_params *params,
755 struct snd_soc_component **last)
757 struct snd_soc_pcm_runtime *rtd = substream->private_data;
758 struct snd_soc_component *component;
761 for_each_rtd_components(rtd, i, component) {
762 if (component->driver->hw_params) {
763 ret = component->driver->hw_params(component,
767 return soc_component_ret(component, ret);
776 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
777 struct snd_soc_component *last)
779 struct snd_soc_pcm_runtime *rtd = substream->private_data;
780 struct snd_soc_component *component;
783 for_each_rtd_components(rtd, i, component) {
784 if (component == last)
787 if (component->driver->hw_free) {
788 ret = component->driver->hw_free(component, substream);
790 soc_component_ret(component, ret);
795 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
798 struct snd_soc_pcm_runtime *rtd = substream->private_data;
799 struct snd_soc_component *component;
802 for_each_rtd_components(rtd, i, component) {
803 if (component->driver->trigger) {
804 ret = component->driver->trigger(component, substream, cmd);
806 return soc_component_ret(component, ret);