Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2//
3// soc-component.c
4//
5// Copyright 2009-2011 Wolfson Microelectronics PLC.
6// Copyright (C) 2019 Renesas Electronics Corp.
7//
8// Mark Brown <broonie@opensource.wolfsonmicro.com>
9// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
10//
11#include <linux/module.h>
12#include <linux/pm_runtime.h>
13#include <sound/soc.h>
14#include <linux/bitops.h>
15
16#define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
17static inline int _soc_component_ret(struct snd_soc_component *component, const char *func, int ret)
18{
19 return snd_soc_ret(component->dev, ret,
20 "at %s() on %s\n", func, component->name);
21}
22
23#define soc_component_ret_reg_rw(dai, ret, reg) _soc_component_ret_reg_rw(dai, __func__, ret, reg)
24static inline int _soc_component_ret_reg_rw(struct snd_soc_component *component,
25 const char *func, int ret, int reg)
26{
27 return snd_soc_ret(component->dev, ret,
28 "at %s() on %s for register: [0x%08x]\n",
29 func, component->name, reg);
30}
31
32static inline int soc_component_field_shift(struct snd_soc_component *component,
33 unsigned int mask)
34{
35 if (!mask) {
36 dev_err(component->dev, "ASoC: error field mask is zero for %s\n",
37 component->name);
38 return 0;
39 }
40
41 return (ffs(mask) - 1);
42}
43
44/*
45 * We might want to check substream by using list.
46 * In such case, we can update these macros.
47 */
48#define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
49#define soc_component_mark_pop(component, tgt) ((component)->mark_##tgt = NULL)
50#define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
51
52void snd_soc_component_set_aux(struct snd_soc_component *component,
53 struct snd_soc_aux_dev *aux)
54{
55 component->init = (aux) ? aux->init : NULL;
56}
57
58int snd_soc_component_init(struct snd_soc_component *component)
59{
60 int ret = 0;
61
62 if (component->init)
63 ret = component->init(component);
64
65 return soc_component_ret(component, ret);
66}
67
68/**
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.
75 *
76 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
77 */
78int snd_soc_component_set_sysclk(struct snd_soc_component *component,
79 int clk_id, int source, unsigned int freq,
80 int dir)
81{
82 int ret = -ENOTSUPP;
83
84 if (component->driver->set_sysclk)
85 ret = component->driver->set_sysclk(component, clk_id, source,
86 freq, dir);
87
88 return soc_component_ret(component, ret);
89}
90EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
91
92/*
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
99 *
100 * Configures and enables PLL to generate output clock based on input clock.
101 */
102int 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)
105{
106 int ret = -EINVAL;
107
108 if (component->driver->set_pll)
109 ret = component->driver->set_pll(component, pll_id, source,
110 freq_in, freq_out);
111
112 return soc_component_ret(component, ret);
113}
114EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
115
116void snd_soc_component_seq_notifier(struct snd_soc_component *component,
117 enum snd_soc_dapm_type type, int subseq)
118{
119 if (component->driver->seq_notifier)
120 component->driver->seq_notifier(component, type, subseq);
121}
122
123int snd_soc_component_stream_event(struct snd_soc_component *component,
124 int event)
125{
126 int ret = 0;
127
128 if (component->driver->stream_event)
129 ret = component->driver->stream_event(component, event);
130
131 return soc_component_ret(component, ret);
132}
133
134int snd_soc_component_set_bias_level(struct snd_soc_component *component,
135 enum snd_soc_bias_level level)
136{
137 int ret = 0;
138
139 if (component->driver->set_bias_level)
140 ret = component->driver->set_bias_level(component, level);
141
142 return soc_component_ret(component, ret);
143}
144
145int snd_soc_component_enable_pin(struct snd_soc_component *component,
146 const char *pin)
147{
148 struct snd_soc_dapm_context *dapm =
149 snd_soc_component_get_dapm(component);
150 return snd_soc_dapm_enable_pin(dapm, pin);
151}
152EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
153
154int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
155 const char *pin)
156{
157 struct snd_soc_dapm_context *dapm =
158 snd_soc_component_get_dapm(component);
159 return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
160}
161EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
162
163int snd_soc_component_disable_pin(struct snd_soc_component *component,
164 const char *pin)
165{
166 struct snd_soc_dapm_context *dapm =
167 snd_soc_component_get_dapm(component);
168 return snd_soc_dapm_disable_pin(dapm, pin);
169}
170EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
171
172int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
173 const char *pin)
174{
175 struct snd_soc_dapm_context *dapm =
176 snd_soc_component_get_dapm(component);
177 return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
178}
179EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
180
181int snd_soc_component_nc_pin(struct snd_soc_component *component,
182 const char *pin)
183{
184 struct snd_soc_dapm_context *dapm =
185 snd_soc_component_get_dapm(component);
186 return snd_soc_dapm_nc_pin(dapm, pin);
187}
188EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
189
190int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
191 const char *pin)
192{
193 struct snd_soc_dapm_context *dapm =
194 snd_soc_component_get_dapm(component);
195 return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
196}
197EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
198
199int snd_soc_component_get_pin_status(struct snd_soc_component *component,
200 const char *pin)
201{
202 struct snd_soc_dapm_context *dapm =
203 snd_soc_component_get_dapm(component);
204 return snd_soc_dapm_get_pin_status(dapm, pin);
205}
206EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
207
208int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
209 const char *pin)
210{
211 struct snd_soc_dapm_context *dapm =
212 snd_soc_component_get_dapm(component);
213 return snd_soc_dapm_force_enable_pin(dapm, pin);
214}
215EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
216
217int snd_soc_component_force_enable_pin_unlocked(
218 struct snd_soc_component *component,
219 const char *pin)
220{
221 struct snd_soc_dapm_context *dapm =
222 snd_soc_component_get_dapm(component);
223 return snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
224}
225EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
226
227static void soc_get_kcontrol_name(struct snd_soc_component *component,
228 char *buf, int size, const char * const ctl)
229{
230 /* When updating, change also snd_soc_dapm_widget_name_cmp() */
231 if (component->name_prefix)
232 snprintf(buf, size, "%s %s", component->name_prefix, ctl);
233 else
234 snprintf(buf, size, "%s", ctl);
235}
236
237struct snd_kcontrol *snd_soc_component_get_kcontrol(struct snd_soc_component *component,
238 const char * const ctl)
239{
240 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
241
242 soc_get_kcontrol_name(component, name, ARRAY_SIZE(name), ctl);
243
244 return snd_soc_card_get_kcontrol(component->card, name);
245}
246EXPORT_SYMBOL_GPL(snd_soc_component_get_kcontrol);
247
248int snd_soc_component_notify_control(struct snd_soc_component *component,
249 const char * const ctl)
250{
251 struct snd_kcontrol *kctl;
252
253 kctl = snd_soc_component_get_kcontrol(component, ctl);
254 if (!kctl)
255 return soc_component_ret(component, -EINVAL);
256
257 snd_ctl_notify(component->card->snd_card,
258 SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
259
260 return 0;
261}
262EXPORT_SYMBOL_GPL(snd_soc_component_notify_control);
263
264/**
265 * snd_soc_component_set_jack - configure component jack.
266 * @component: COMPONENTs
267 * @jack: structure to use for the jack
268 * @data: can be used if codec driver need extra data for configuring jack
269 *
270 * Configures and enables jack detection function.
271 */
272int snd_soc_component_set_jack(struct snd_soc_component *component,
273 struct snd_soc_jack *jack, void *data)
274{
275 int ret = -ENOTSUPP;
276
277 if (component->driver->set_jack)
278 ret = component->driver->set_jack(component, jack, data);
279
280 return soc_component_ret(component, ret);
281}
282EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
283
284/**
285 * snd_soc_component_get_jack_type
286 * @component: COMPONENTs
287 *
288 * Returns the jack type of the component
289 * This can either be the supported type or one read from
290 * devicetree with the property: jack-type.
291 */
292int snd_soc_component_get_jack_type(
293 struct snd_soc_component *component)
294{
295 int ret = -ENOTSUPP;
296
297 if (component->driver->get_jack_type)
298 ret = component->driver->get_jack_type(component);
299
300 return soc_component_ret(component, ret);
301}
302EXPORT_SYMBOL_GPL(snd_soc_component_get_jack_type);
303
304int snd_soc_component_module_get(struct snd_soc_component *component,
305 void *mark, int upon_open)
306{
307 int ret = 0;
308
309 if (component->driver->module_get_upon_open == !!upon_open &&
310 !try_module_get(component->dev->driver->owner))
311 ret = -ENODEV;
312
313 /* mark module if succeeded */
314 if (ret == 0)
315 soc_component_mark_push(component, mark, module);
316
317 return soc_component_ret(component, ret);
318}
319
320void snd_soc_component_module_put(struct snd_soc_component *component,
321 void *mark, int upon_open, int rollback)
322{
323 if (rollback && !soc_component_mark_match(component, mark, module))
324 return;
325
326 if (component->driver->module_get_upon_open == !!upon_open)
327 module_put(component->dev->driver->owner);
328
329 /* remove the mark from module */
330 soc_component_mark_pop(component, module);
331}
332
333int snd_soc_component_open(struct snd_soc_component *component,
334 struct snd_pcm_substream *substream)
335{
336 int ret = 0;
337
338 if (component->driver->open)
339 ret = component->driver->open(component, substream);
340
341 /* mark substream if succeeded */
342 if (ret == 0)
343 soc_component_mark_push(component, substream, open);
344
345 return soc_component_ret(component, ret);
346}
347
348int snd_soc_component_close(struct snd_soc_component *component,
349 struct snd_pcm_substream *substream,
350 int rollback)
351{
352 int ret = 0;
353
354 if (rollback && !soc_component_mark_match(component, substream, open))
355 return 0;
356
357 if (component->driver->close)
358 ret = component->driver->close(component, substream);
359
360 /* remove marked substream */
361 soc_component_mark_pop(component, open);
362
363 return soc_component_ret(component, ret);
364}
365
366void snd_soc_component_suspend(struct snd_soc_component *component)
367{
368 if (component->driver->suspend)
369 component->driver->suspend(component);
370 component->suspended = 1;
371}
372
373void snd_soc_component_resume(struct snd_soc_component *component)
374{
375 if (component->driver->resume)
376 component->driver->resume(component);
377 component->suspended = 0;
378}
379
380int snd_soc_component_is_suspended(struct snd_soc_component *component)
381{
382 return component->suspended;
383}
384
385int snd_soc_component_probe(struct snd_soc_component *component)
386{
387 int ret = 0;
388
389 if (component->driver->probe)
390 ret = component->driver->probe(component);
391
392 return soc_component_ret(component, ret);
393}
394
395void snd_soc_component_remove(struct snd_soc_component *component)
396{
397 if (component->driver->remove)
398 component->driver->remove(component);
399}
400
401int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
402 struct device_node *ep)
403{
404 int ret = -ENOTSUPP;
405
406 if (component->driver->of_xlate_dai_id)
407 ret = component->driver->of_xlate_dai_id(component, ep);
408
409 return soc_component_ret(component, ret);
410}
411
412int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
413 const struct of_phandle_args *args,
414 const char **dai_name)
415{
416 if (component->driver->of_xlate_dai_name)
417 return component->driver->of_xlate_dai_name(component,
418 args, dai_name);
419 /*
420 * Don't use soc_component_ret here because we may not want to report
421 * the error just yet. If a device has more than one component, the
422 * first may not match and we don't want spam the log with this.
423 */
424 return -ENOTSUPP;
425}
426
427void snd_soc_component_setup_regmap(struct snd_soc_component *component)
428{
429 int val_bytes = regmap_get_val_bytes(component->regmap);
430
431 /* Errors are legitimate for non-integer byte multiples */
432 if (val_bytes > 0)
433 component->val_bytes = val_bytes;
434}
435
436#ifdef CONFIG_REGMAP
437
438/**
439 * snd_soc_component_init_regmap() - Initialize regmap instance for the
440 * component
441 * @component: The component for which to initialize the regmap instance
442 * @regmap: The regmap instance that should be used by the component
443 *
444 * This function allows deferred assignment of the regmap instance that is
445 * associated with the component. Only use this if the regmap instance is not
446 * yet ready when the component is registered. The function must also be called
447 * before the first IO attempt of the component.
448 */
449void snd_soc_component_init_regmap(struct snd_soc_component *component,
450 struct regmap *regmap)
451{
452 component->regmap = regmap;
453 snd_soc_component_setup_regmap(component);
454}
455EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
456
457/**
458 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
459 * component
460 * @component: The component for which to de-initialize the regmap instance
461 *
462 * Calls regmap_exit() on the regmap instance associated to the component and
463 * removes the regmap instance from the component.
464 *
465 * This function should only be used if snd_soc_component_init_regmap() was used
466 * to initialize the regmap instance.
467 */
468void snd_soc_component_exit_regmap(struct snd_soc_component *component)
469{
470 regmap_exit(component->regmap);
471 component->regmap = NULL;
472}
473EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
474
475#endif
476
477int snd_soc_component_compr_open(struct snd_soc_component *component,
478 struct snd_compr_stream *cstream)
479{
480 int ret = 0;
481
482 if (component->driver->compress_ops &&
483 component->driver->compress_ops->open)
484 ret = component->driver->compress_ops->open(component, cstream);
485
486 /* mark substream if succeeded */
487 if (ret == 0)
488 soc_component_mark_push(component, cstream, compr_open);
489
490 return soc_component_ret(component, ret);
491}
492EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
493
494void snd_soc_component_compr_free(struct snd_soc_component *component,
495 struct snd_compr_stream *cstream,
496 int rollback)
497{
498 if (rollback && !soc_component_mark_match(component, cstream, compr_open))
499 return;
500
501 if (component->driver->compress_ops &&
502 component->driver->compress_ops->free)
503 component->driver->compress_ops->free(component, cstream);
504
505 /* remove marked substream */
506 soc_component_mark_pop(component, compr_open);
507}
508EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
509
510int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
511{
512 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
513 struct snd_soc_component *component;
514 int i, ret;
515
516 for_each_rtd_components(rtd, i, component) {
517 if (component->driver->compress_ops &&
518 component->driver->compress_ops->trigger) {
519 ret = component->driver->compress_ops->trigger(
520 component, cstream, cmd);
521 if (ret < 0)
522 return soc_component_ret(component, ret);
523 }
524 }
525
526 return 0;
527}
528EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
529
530int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
531 struct snd_compr_params *params)
532{
533 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
534 struct snd_soc_component *component;
535 int i, ret;
536
537 for_each_rtd_components(rtd, i, component) {
538 if (component->driver->compress_ops &&
539 component->driver->compress_ops->set_params) {
540 ret = component->driver->compress_ops->set_params(
541 component, cstream, params);
542 if (ret < 0)
543 return soc_component_ret(component, ret);
544 }
545 }
546
547 return 0;
548}
549EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
550
551int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
552 struct snd_codec *params)
553{
554 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
555 struct snd_soc_component *component;
556 int i, ret;
557
558 for_each_rtd_components(rtd, i, component) {
559 if (component->driver->compress_ops &&
560 component->driver->compress_ops->get_params) {
561 ret = component->driver->compress_ops->get_params(
562 component, cstream, params);
563 return soc_component_ret(component, ret);
564 }
565 }
566
567 return 0;
568}
569EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
570
571int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
572 struct snd_compr_caps *caps)
573{
574 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
575 struct snd_soc_component *component;
576 int i, ret = 0;
577
578 snd_soc_dpcm_mutex_lock(rtd);
579
580 for_each_rtd_components(rtd, i, component) {
581 if (component->driver->compress_ops &&
582 component->driver->compress_ops->get_caps) {
583 ret = component->driver->compress_ops->get_caps(
584 component, cstream, caps);
585 break;
586 }
587 }
588
589 snd_soc_dpcm_mutex_unlock(rtd);
590
591 return soc_component_ret(component, ret);
592}
593EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
594
595int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
596 struct snd_compr_codec_caps *codec)
597{
598 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
599 struct snd_soc_component *component;
600 int i, ret = 0;
601
602 snd_soc_dpcm_mutex_lock(rtd);
603
604 for_each_rtd_components(rtd, i, component) {
605 if (component->driver->compress_ops &&
606 component->driver->compress_ops->get_codec_caps) {
607 ret = component->driver->compress_ops->get_codec_caps(
608 component, cstream, codec);
609 break;
610 }
611 }
612
613 snd_soc_dpcm_mutex_unlock(rtd);
614
615 return soc_component_ret(component, ret);
616}
617EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
618
619int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
620{
621 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
622 struct snd_soc_component *component;
623 int i, ret;
624
625 for_each_rtd_components(rtd, i, component) {
626 if (component->driver->compress_ops &&
627 component->driver->compress_ops->ack) {
628 ret = component->driver->compress_ops->ack(
629 component, cstream, bytes);
630 if (ret < 0)
631 return soc_component_ret(component, ret);
632 }
633 }
634
635 return 0;
636}
637EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
638
639int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
640 struct snd_compr_tstamp *tstamp)
641{
642 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
643 struct snd_soc_component *component;
644 int i, ret;
645
646 for_each_rtd_components(rtd, i, component) {
647 if (component->driver->compress_ops &&
648 component->driver->compress_ops->pointer) {
649 ret = component->driver->compress_ops->pointer(
650 component, cstream, tstamp);
651 return soc_component_ret(component, ret);
652 }
653 }
654
655 return 0;
656}
657EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
658
659int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
660 char __user *buf, size_t count)
661{
662 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
663 struct snd_soc_component *component;
664 int i, ret = 0;
665
666 snd_soc_dpcm_mutex_lock(rtd);
667
668 for_each_rtd_components(rtd, i, component) {
669 if (component->driver->compress_ops &&
670 component->driver->compress_ops->copy) {
671 ret = component->driver->compress_ops->copy(
672 component, cstream, buf, count);
673 break;
674 }
675 }
676
677 snd_soc_dpcm_mutex_unlock(rtd);
678
679 return soc_component_ret(component, ret);
680}
681EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
682
683int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
684 struct snd_compr_metadata *metadata)
685{
686 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
687 struct snd_soc_component *component;
688 int i, ret;
689
690 for_each_rtd_components(rtd, i, component) {
691 if (component->driver->compress_ops &&
692 component->driver->compress_ops->set_metadata) {
693 ret = component->driver->compress_ops->set_metadata(
694 component, cstream, metadata);
695 if (ret < 0)
696 return soc_component_ret(component, ret);
697 }
698 }
699
700 return 0;
701}
702EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
703
704int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
705 struct snd_compr_metadata *metadata)
706{
707 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
708 struct snd_soc_component *component;
709 int i, ret;
710
711 for_each_rtd_components(rtd, i, component) {
712 if (component->driver->compress_ops &&
713 component->driver->compress_ops->get_metadata) {
714 ret = component->driver->compress_ops->get_metadata(
715 component, cstream, metadata);
716 return soc_component_ret(component, ret);
717 }
718 }
719
720 return 0;
721}
722EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
723
724static unsigned int soc_component_read_no_lock(
725 struct snd_soc_component *component,
726 unsigned int reg)
727{
728 int ret;
729 unsigned int val = 0;
730
731 if (component->regmap)
732 ret = regmap_read(component->regmap, reg, &val);
733 else if (component->driver->read) {
734 ret = 0;
735 val = component->driver->read(component, reg);
736 }
737 else
738 ret = -EIO;
739
740 if (ret < 0)
741 return soc_component_ret_reg_rw(component, ret, reg);
742
743 return val;
744}
745
746/**
747 * snd_soc_component_read() - Read register value
748 * @component: Component to read from
749 * @reg: Register to read
750 *
751 * Return: read value
752 */
753unsigned int snd_soc_component_read(struct snd_soc_component *component,
754 unsigned int reg)
755{
756 unsigned int val;
757
758 mutex_lock(&component->io_mutex);
759 val = soc_component_read_no_lock(component, reg);
760 mutex_unlock(&component->io_mutex);
761
762 return val;
763}
764EXPORT_SYMBOL_GPL(snd_soc_component_read);
765
766static int soc_component_write_no_lock(
767 struct snd_soc_component *component,
768 unsigned int reg, unsigned int val)
769{
770 int ret = -EIO;
771
772 if (component->regmap)
773 ret = regmap_write(component->regmap, reg, val);
774 else if (component->driver->write)
775 ret = component->driver->write(component, reg, val);
776
777 return soc_component_ret_reg_rw(component, ret, reg);
778}
779
780/**
781 * snd_soc_component_write() - Write register value
782 * @component: Component to write to
783 * @reg: Register to write
784 * @val: Value to write to the register
785 *
786 * Return: 0 on success, a negative error code otherwise.
787 */
788int snd_soc_component_write(struct snd_soc_component *component,
789 unsigned int reg, unsigned int val)
790{
791 int ret;
792
793 mutex_lock(&component->io_mutex);
794 ret = soc_component_write_no_lock(component, reg, val);
795 mutex_unlock(&component->io_mutex);
796
797 return ret;
798}
799EXPORT_SYMBOL_GPL(snd_soc_component_write);
800
801static int snd_soc_component_update_bits_legacy(
802 struct snd_soc_component *component, unsigned int reg,
803 unsigned int mask, unsigned int val, bool *change)
804{
805 unsigned int old, new;
806 int ret = 0;
807
808 mutex_lock(&component->io_mutex);
809
810 old = soc_component_read_no_lock(component, reg);
811
812 new = (old & ~mask) | (val & mask);
813 *change = old != new;
814 if (*change)
815 ret = soc_component_write_no_lock(component, reg, new);
816
817 mutex_unlock(&component->io_mutex);
818
819 return soc_component_ret_reg_rw(component, ret, reg);
820}
821
822/**
823 * snd_soc_component_update_bits() - Perform read/modify/write cycle
824 * @component: Component to update
825 * @reg: Register to update
826 * @mask: Mask that specifies which bits to update
827 * @val: New value for the bits specified by mask
828 *
829 * Return: 1 if the operation was successful and the value of the register
830 * changed, 0 if the operation was successful, but the value did not change.
831 * Returns a negative error code otherwise.
832 */
833int snd_soc_component_update_bits(struct snd_soc_component *component,
834 unsigned int reg, unsigned int mask, unsigned int val)
835{
836 bool change;
837 int ret;
838
839 if (component->regmap)
840 ret = regmap_update_bits_check(component->regmap, reg, mask,
841 val, &change);
842 else
843 ret = snd_soc_component_update_bits_legacy(component, reg,
844 mask, val, &change);
845
846 if (ret < 0)
847 return soc_component_ret_reg_rw(component, ret, reg);
848 return change;
849}
850EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
851
852/**
853 * snd_soc_component_update_bits_async() - Perform asynchronous
854 * read/modify/write cycle
855 * @component: Component to update
856 * @reg: Register to update
857 * @mask: Mask that specifies which bits to update
858 * @val: New value for the bits specified by mask
859 *
860 * This function is similar to snd_soc_component_update_bits(), but the update
861 * operation is scheduled asynchronously. This means it may not be completed
862 * when the function returns. To make sure that all scheduled updates have been
863 * completed snd_soc_component_async_complete() must be called.
864 *
865 * Return: 1 if the operation was successful and the value of the register
866 * changed, 0 if the operation was successful, but the value did not change.
867 * Returns a negative error code otherwise.
868 */
869int snd_soc_component_update_bits_async(struct snd_soc_component *component,
870 unsigned int reg, unsigned int mask, unsigned int val)
871{
872 bool change;
873 int ret;
874
875 if (component->regmap)
876 ret = regmap_update_bits_check_async(component->regmap, reg,
877 mask, val, &change);
878 else
879 ret = snd_soc_component_update_bits_legacy(component, reg,
880 mask, val, &change);
881
882 if (ret < 0)
883 return soc_component_ret_reg_rw(component, ret, reg);
884 return change;
885}
886EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
887
888/**
889 * snd_soc_component_read_field() - Read register field value
890 * @component: Component to read from
891 * @reg: Register to read
892 * @mask: mask of the register field
893 *
894 * Return: read value of register field.
895 */
896unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
897 unsigned int reg, unsigned int mask)
898{
899 unsigned int val;
900
901 val = snd_soc_component_read(component, reg);
902
903 val = (val & mask) >> soc_component_field_shift(component, mask);
904
905 return val;
906}
907EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
908
909/**
910 * snd_soc_component_write_field() - write to register field
911 * @component: Component to write to
912 * @reg: Register to write
913 * @mask: mask of the register field to update
914 * @val: value of the field to write
915 *
916 * Return: 1 for change, otherwise 0.
917 */
918int snd_soc_component_write_field(struct snd_soc_component *component,
919 unsigned int reg, unsigned int mask,
920 unsigned int val)
921{
922
923 val = (val << soc_component_field_shift(component, mask)) & mask;
924
925 return snd_soc_component_update_bits(component, reg, mask, val);
926}
927EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
928
929/**
930 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
931 * @component: Component for which to wait
932 *
933 * This function blocks until all asynchronous I/O which has previously been
934 * scheduled using snd_soc_component_update_bits_async() has completed.
935 */
936void snd_soc_component_async_complete(struct snd_soc_component *component)
937{
938 if (component->regmap)
939 regmap_async_complete(component->regmap);
940}
941EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
942
943/**
944 * snd_soc_component_test_bits - Test register for change
945 * @component: component
946 * @reg: Register to test
947 * @mask: Mask that specifies which bits to test
948 * @value: Value to test against
949 *
950 * Tests a register with a new value and checks if the new value is
951 * different from the old value.
952 *
953 * Return: 1 for change, otherwise 0.
954 */
955int snd_soc_component_test_bits(struct snd_soc_component *component,
956 unsigned int reg, unsigned int mask, unsigned int value)
957{
958 unsigned int old, new;
959
960 old = snd_soc_component_read(component, reg);
961 new = (old & ~mask) | value;
962 return old != new;
963}
964EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
965
966int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
967{
968 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
969 struct snd_soc_component *component;
970 int i;
971
972 /* FIXME: use 1st pointer */
973 for_each_rtd_components(rtd, i, component)
974 if (component->driver->pointer)
975 return component->driver->pointer(component, substream);
976
977 return 0;
978}
979
980static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd,
981 struct snd_soc_component *component)
982{
983 struct snd_soc_dai *dai;
984 int i;
985
986 for_each_rtd_codec_dais(rtd, i, dai) {
987 if (dai->component == component)
988 return true;
989 }
990
991 return false;
992}
993
994void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
995 snd_pcm_sframes_t *cpu_delay,
996 snd_pcm_sframes_t *codec_delay)
997{
998 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
999 struct snd_soc_component *component;
1000 snd_pcm_sframes_t delay;
1001 int i;
1002
1003 /*
1004 * We're looking for the delay through the full audio path so it needs to
1005 * be the maximum of the Components doing transmit and the maximum of the
1006 * Components doing receive (ie, all CPUs and all CODECs) rather than
1007 * just the maximum of all Components.
1008 */
1009 for_each_rtd_components(rtd, i, component) {
1010 if (!component->driver->delay)
1011 continue;
1012
1013 delay = component->driver->delay(component, substream);
1014
1015 if (snd_soc_component_is_codec_on_rtd(rtd, component))
1016 *codec_delay = max(*codec_delay, delay);
1017 else
1018 *cpu_delay = max(*cpu_delay, delay);
1019 }
1020}
1021
1022int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
1023 unsigned int cmd, void *arg)
1024{
1025 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1026 struct snd_soc_component *component;
1027 int i;
1028
1029 /* FIXME: use 1st ioctl */
1030 for_each_rtd_components(rtd, i, component)
1031 if (component->driver->ioctl)
1032 return soc_component_ret(
1033 component,
1034 component->driver->ioctl(component,
1035 substream, cmd, arg));
1036
1037 return snd_pcm_lib_ioctl(substream, cmd, arg);
1038}
1039
1040int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
1041{
1042 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1043 struct snd_soc_component *component;
1044 int i, ret;
1045
1046 for_each_rtd_components(rtd, i, component) {
1047 if (component->driver->sync_stop) {
1048 ret = component->driver->sync_stop(component,
1049 substream);
1050 if (ret < 0)
1051 return soc_component_ret(component, ret);
1052 }
1053 }
1054
1055 return 0;
1056}
1057
1058int snd_soc_pcm_component_copy(struct snd_pcm_substream *substream,
1059 int channel, unsigned long pos,
1060 struct iov_iter *iter, unsigned long bytes)
1061{
1062 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1063 struct snd_soc_component *component;
1064 int i;
1065
1066 /* FIXME. it returns 1st copy now */
1067 for_each_rtd_components(rtd, i, component)
1068 if (component->driver->copy)
1069 return soc_component_ret(component,
1070 component->driver->copy(component, substream,
1071 channel, pos, iter, bytes));
1072
1073 return -EINVAL;
1074}
1075
1076struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
1077 unsigned long offset)
1078{
1079 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1080 struct snd_soc_component *component;
1081 struct page *page;
1082 int i;
1083
1084 /* FIXME. it returns 1st page now */
1085 for_each_rtd_components(rtd, i, component) {
1086 if (component->driver->page) {
1087 page = component->driver->page(component,
1088 substream, offset);
1089 if (page)
1090 return page;
1091 }
1092 }
1093
1094 return NULL;
1095}
1096
1097int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
1098 struct vm_area_struct *vma)
1099{
1100 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1101 struct snd_soc_component *component;
1102 int i;
1103
1104 /* FIXME. it returns 1st mmap now */
1105 for_each_rtd_components(rtd, i, component)
1106 if (component->driver->mmap)
1107 return soc_component_ret(
1108 component,
1109 component->driver->mmap(component,
1110 substream, vma));
1111
1112 return -EINVAL;
1113}
1114
1115int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
1116{
1117 struct snd_soc_component *component;
1118 int ret;
1119 int i;
1120
1121 for_each_rtd_components(rtd, i, component) {
1122 if (component->driver->pcm_construct) {
1123 ret = component->driver->pcm_construct(component, rtd);
1124 if (ret < 0)
1125 return soc_component_ret(component, ret);
1126 }
1127 }
1128
1129 return 0;
1130}
1131
1132void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
1133{
1134 struct snd_soc_component *component;
1135 int i;
1136
1137 if (!rtd->pcm)
1138 return;
1139
1140 for_each_rtd_components(rtd, i, component)
1141 if (component->driver->pcm_destruct)
1142 component->driver->pcm_destruct(component, rtd->pcm);
1143}
1144
1145int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1146{
1147 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1148 struct snd_soc_component *component;
1149 int i, ret;
1150
1151 for_each_rtd_components(rtd, i, component) {
1152 if (component->driver->prepare) {
1153 ret = component->driver->prepare(component, substream);
1154 if (ret < 0)
1155 return soc_component_ret(component, ret);
1156 }
1157 }
1158
1159 return 0;
1160}
1161
1162int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
1163 struct snd_pcm_hw_params *params)
1164{
1165 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1166 struct snd_soc_component *component;
1167 int i, ret;
1168
1169 for_each_rtd_components(rtd, i, component) {
1170 if (component->driver->hw_params) {
1171 ret = component->driver->hw_params(component,
1172 substream, params);
1173 if (ret < 0)
1174 return soc_component_ret(component, ret);
1175 }
1176 /* mark substream if succeeded */
1177 soc_component_mark_push(component, substream, hw_params);
1178 }
1179
1180 return 0;
1181}
1182
1183void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
1184 int rollback)
1185{
1186 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1187 struct snd_soc_component *component;
1188 int i, ret;
1189
1190 for_each_rtd_components(rtd, i, component) {
1191 if (rollback && !soc_component_mark_match(component, substream, hw_params))
1192 continue;
1193
1194 if (component->driver->hw_free) {
1195 ret = component->driver->hw_free(component, substream);
1196 if (ret < 0)
1197 soc_component_ret(component, ret);
1198 }
1199
1200 /* remove marked substream */
1201 soc_component_mark_pop(component, hw_params);
1202 }
1203}
1204
1205static int soc_component_trigger(struct snd_soc_component *component,
1206 struct snd_pcm_substream *substream,
1207 int cmd)
1208{
1209 int ret = 0;
1210
1211 if (component->driver->trigger)
1212 ret = component->driver->trigger(component, substream, cmd);
1213
1214 return soc_component_ret(component, ret);
1215}
1216
1217int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
1218 int cmd, int rollback)
1219{
1220 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1221 struct snd_soc_component *component;
1222 int i, r, ret = 0;
1223
1224 switch (cmd) {
1225 case SNDRV_PCM_TRIGGER_START:
1226 case SNDRV_PCM_TRIGGER_RESUME:
1227 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1228 for_each_rtd_components(rtd, i, component) {
1229 ret = soc_component_trigger(component, substream, cmd);
1230 if (ret < 0)
1231 break;
1232 soc_component_mark_push(component, substream, trigger);
1233 }
1234 break;
1235 case SNDRV_PCM_TRIGGER_STOP:
1236 case SNDRV_PCM_TRIGGER_SUSPEND:
1237 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1238 for_each_rtd_components(rtd, i, component) {
1239 if (rollback && !soc_component_mark_match(component, substream, trigger))
1240 continue;
1241
1242 r = soc_component_trigger(component, substream, cmd);
1243 if (r < 0)
1244 ret = r; /* use last ret */
1245 soc_component_mark_pop(component, trigger);
1246 }
1247 }
1248
1249 return ret;
1250}
1251
1252int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1253 void *stream)
1254{
1255 struct snd_soc_component *component;
1256 int i;
1257
1258 for_each_rtd_components(rtd, i, component) {
1259 int ret = pm_runtime_get_sync(component->dev);
1260 if (ret < 0 && ret != -EACCES) {
1261 pm_runtime_put_noidle(component->dev);
1262 return soc_component_ret(component, ret);
1263 }
1264 /* mark stream if succeeded */
1265 soc_component_mark_push(component, stream, pm);
1266 }
1267
1268 return 0;
1269}
1270
1271void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1272 void *stream, int rollback)
1273{
1274 struct snd_soc_component *component;
1275 int i;
1276
1277 for_each_rtd_components(rtd, i, component) {
1278 if (rollback && !soc_component_mark_match(component, stream, pm))
1279 continue;
1280
1281 pm_runtime_mark_last_busy(component->dev);
1282 pm_runtime_put_autosuspend(component->dev);
1283
1284 /* remove marked stream */
1285 soc_component_mark_pop(component, pm);
1286 }
1287}
1288
1289int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
1290{
1291 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1292 struct snd_soc_component *component;
1293 int i;
1294
1295 /* FIXME: use 1st pointer */
1296 for_each_rtd_components(rtd, i, component)
1297 if (component->driver->ack)
1298 return component->driver->ack(component, substream);
1299
1300 return 0;
1301}