ASoC: soc-component: add snd_soc_component_compr_get_codec_caps()
[linux-2.6-microblaze.git] / sound / soc / soc-component.c
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
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)
18 {
19         /* Positive/Zero values are not errors */
20         if (ret >= 0)
21                 return ret;
22
23         /* Negative values might be errors */
24         switch (ret) {
25         case -EPROBE_DEFER:
26         case -ENOTSUPP:
27                 break;
28         default:
29                 dev_err(component->dev,
30                         "ASoC: error at %s on %s: %d\n",
31                         func, component->name, ret);
32         }
33
34         return ret;
35 }
36
37 /*
38  * We might want to check substream by using list.
39  * In such case, we can update these macros.
40  */
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)
44
45 void snd_soc_component_set_aux(struct snd_soc_component *component,
46                                struct snd_soc_aux_dev *aux)
47 {
48         component->init = (aux) ? aux->init : NULL;
49 }
50
51 int snd_soc_component_init(struct snd_soc_component *component)
52 {
53         int ret = 0;
54
55         if (component->init)
56                 ret = component->init(component);
57
58         return soc_component_ret(component, ret);
59 }
60
61 /**
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.
68  *
69  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
70  */
71 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
72                                  int clk_id, int source, unsigned int freq,
73                                  int dir)
74 {
75         int ret = -ENOTSUPP;
76
77         if (component->driver->set_sysclk)
78                 ret = component->driver->set_sysclk(component, clk_id, source,
79                                                      freq, dir);
80
81         return soc_component_ret(component, ret);
82 }
83 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
84
85 /*
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
92  *
93  * Configures and enables PLL to generate output clock based on input clock.
94  */
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)
98 {
99         int ret = -EINVAL;
100
101         if (component->driver->set_pll)
102                 ret = component->driver->set_pll(component, pll_id, source,
103                                                   freq_in, freq_out);
104
105         return soc_component_ret(component, ret);
106 }
107 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
108
109 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
110                                     enum snd_soc_dapm_type type, int subseq)
111 {
112         if (component->driver->seq_notifier)
113                 component->driver->seq_notifier(component, type, subseq);
114 }
115
116 int snd_soc_component_stream_event(struct snd_soc_component *component,
117                                    int event)
118 {
119         int ret = 0;
120
121         if (component->driver->stream_event)
122                 ret = component->driver->stream_event(component, event);
123
124         return soc_component_ret(component, ret);
125 }
126
127 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
128                                      enum snd_soc_bias_level level)
129 {
130         int ret = 0;
131
132         if (component->driver->set_bias_level)
133                 ret = component->driver->set_bias_level(component, level);
134
135         return soc_component_ret(component, ret);
136 }
137
138 static int soc_component_pin(struct snd_soc_component *component,
139                              const char *pin,
140                              int (*pin_func)(struct snd_soc_dapm_context *dapm,
141                                              const char *pin))
142 {
143         struct snd_soc_dapm_context *dapm =
144                 snd_soc_component_get_dapm(component);
145         char *full_name;
146         int ret;
147
148         if (!component->name_prefix) {
149                 ret = pin_func(dapm, pin);
150                 goto end;
151         }
152
153         full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
154         if (!full_name) {
155                 ret = -ENOMEM;
156                 goto end;
157         }
158
159         ret = pin_func(dapm, full_name);
160         kfree(full_name);
161 end:
162         return soc_component_ret(component, ret);
163 }
164
165 int snd_soc_component_enable_pin(struct snd_soc_component *component,
166                                  const char *pin)
167 {
168         return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
169 }
170 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
171
172 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
173                                           const char *pin)
174 {
175         return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
176 }
177 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
178
179 int snd_soc_component_disable_pin(struct snd_soc_component *component,
180                                   const char *pin)
181 {
182         return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
183 }
184 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
185
186 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
187                                            const char *pin)
188 {
189         return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
190 }
191 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
192
193 int snd_soc_component_nc_pin(struct snd_soc_component *component,
194                              const char *pin)
195 {
196         return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
197 }
198 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
199
200 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
201                                       const char *pin)
202 {
203         return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
204 }
205 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
206
207 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
208                                      const char *pin)
209 {
210         return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
211 }
212 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
213
214 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
215                                        const char *pin)
216 {
217         return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
218 }
219 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
220
221 int snd_soc_component_force_enable_pin_unlocked(
222         struct snd_soc_component *component,
223         const char *pin)
224 {
225         return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
226 }
227 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
228
229 /**
230  * snd_soc_component_set_jack - configure component jack.
231  * @component: COMPONENTs
232  * @jack: structure to use for the jack
233  * @data: can be used if codec driver need extra data for configuring jack
234  *
235  * Configures and enables jack detection function.
236  */
237 int snd_soc_component_set_jack(struct snd_soc_component *component,
238                                struct snd_soc_jack *jack, void *data)
239 {
240         int ret = -ENOTSUPP;
241
242         if (component->driver->set_jack)
243                 ret = component->driver->set_jack(component, jack, data);
244
245         return soc_component_ret(component, ret);
246 }
247 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
248
249 int snd_soc_component_module_get(struct snd_soc_component *component,
250                                  struct snd_pcm_substream *substream,
251                                  int upon_open)
252 {
253         int ret = 0;
254
255         if (component->driver->module_get_upon_open == !!upon_open &&
256             !try_module_get(component->dev->driver->owner))
257                 ret = -ENODEV;
258
259         /* mark substream if succeeded */
260         if (ret == 0)
261                 soc_component_mark_push(component, substream, module);
262
263         return soc_component_ret(component, ret);
264 }
265
266 void snd_soc_component_module_put(struct snd_soc_component *component,
267                                   struct snd_pcm_substream *substream,
268                                   int upon_open, int rollback)
269 {
270         if (rollback && !soc_component_mark_match(component, substream, module))
271                 return;
272
273         if (component->driver->module_get_upon_open == !!upon_open)
274                 module_put(component->dev->driver->owner);
275
276         /* remove marked substream */
277         soc_component_mark_pop(component, substream, module);
278 }
279
280 int snd_soc_component_open(struct snd_soc_component *component,
281                            struct snd_pcm_substream *substream)
282 {
283         int ret = 0;
284
285         if (component->driver->open)
286                 ret = component->driver->open(component, substream);
287
288         /* mark substream if succeeded */
289         if (ret == 0)
290                 soc_component_mark_push(component, substream, open);
291
292         return soc_component_ret(component, ret);
293 }
294
295 int snd_soc_component_close(struct snd_soc_component *component,
296                             struct snd_pcm_substream *substream,
297                             int rollback)
298 {
299         int ret = 0;
300
301         if (rollback && !soc_component_mark_match(component, substream, open))
302                 return 0;
303
304         if (component->driver->close)
305                 ret = component->driver->close(component, substream);
306
307         /* remove marked substream */
308         soc_component_mark_pop(component, substream, open);
309
310         return soc_component_ret(component, ret);
311 }
312
313 void snd_soc_component_suspend(struct snd_soc_component *component)
314 {
315         if (component->driver->suspend)
316                 component->driver->suspend(component);
317         component->suspended = 1;
318 }
319
320 void snd_soc_component_resume(struct snd_soc_component *component)
321 {
322         if (component->driver->resume)
323                 component->driver->resume(component);
324         component->suspended = 0;
325 }
326
327 int snd_soc_component_is_suspended(struct snd_soc_component *component)
328 {
329         return component->suspended;
330 }
331
332 int snd_soc_component_probe(struct snd_soc_component *component)
333 {
334         int ret = 0;
335
336         if (component->driver->probe)
337                 ret = component->driver->probe(component);
338
339         return soc_component_ret(component, ret);
340 }
341
342 void snd_soc_component_remove(struct snd_soc_component *component)
343 {
344         if (component->driver->remove)
345                 component->driver->remove(component);
346 }
347
348 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
349                                       struct device_node *ep)
350 {
351         int ret = -ENOTSUPP;
352
353         if (component->driver->of_xlate_dai_id)
354                 ret = component->driver->of_xlate_dai_id(component, ep);
355
356         return soc_component_ret(component, ret);
357 }
358
359 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
360                                         struct of_phandle_args *args,
361                                         const char **dai_name)
362 {
363         if (component->driver->of_xlate_dai_name)
364                 return component->driver->of_xlate_dai_name(component,
365                                                             args, dai_name);
366         /*
367          * Don't use soc_component_ret here because we may not want to report
368          * the error just yet. If a device has more than one component, the
369          * first may not match and we don't want spam the log with this.
370          */
371         return -ENOTSUPP;
372 }
373
374 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
375 {
376         int val_bytes = regmap_get_val_bytes(component->regmap);
377
378         /* Errors are legitimate for non-integer byte multiples */
379         if (val_bytes > 0)
380                 component->val_bytes = val_bytes;
381 }
382
383 #ifdef CONFIG_REGMAP
384
385 /**
386  * snd_soc_component_init_regmap() - Initialize regmap instance for the
387  *                                   component
388  * @component: The component for which to initialize the regmap instance
389  * @regmap: The regmap instance that should be used by the component
390  *
391  * This function allows deferred assignment of the regmap instance that is
392  * associated with the component. Only use this if the regmap instance is not
393  * yet ready when the component is registered. The function must also be called
394  * before the first IO attempt of the component.
395  */
396 void snd_soc_component_init_regmap(struct snd_soc_component *component,
397                                    struct regmap *regmap)
398 {
399         component->regmap = regmap;
400         snd_soc_component_setup_regmap(component);
401 }
402 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
403
404 /**
405  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
406  *                                   component
407  * @component: The component for which to de-initialize the regmap instance
408  *
409  * Calls regmap_exit() on the regmap instance associated to the component and
410  * removes the regmap instance from the component.
411  *
412  * This function should only be used if snd_soc_component_init_regmap() was used
413  * to initialize the regmap instance.
414  */
415 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
416 {
417         regmap_exit(component->regmap);
418         component->regmap = NULL;
419 }
420 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
421
422 #endif
423
424 int snd_soc_component_compr_open(struct snd_compr_stream *cstream,
425                                  struct snd_soc_component **last)
426 {
427         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
428         struct snd_soc_component *component;
429         int i, ret;
430
431         for_each_rtd_components(rtd, i, component) {
432                 if (component->driver->compress_ops &&
433                     component->driver->compress_ops->open) {
434                         ret = component->driver->compress_ops->open(component, cstream);
435                         if (ret < 0) {
436                                 *last = component;
437                                 return soc_component_ret(component, ret);
438                         }
439                 }
440         }
441
442         *last = NULL;
443         return 0;
444 }
445 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
446
447 void snd_soc_component_compr_free(struct snd_compr_stream *cstream,
448                                   struct snd_soc_component *last)
449 {
450         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
451         struct snd_soc_component *component;
452         int i;
453
454         for_each_rtd_components(rtd, i, component) {
455                 if (component == last)
456                         break;
457
458                 if (component->driver->compress_ops &&
459                     component->driver->compress_ops->free)
460                         component->driver->compress_ops->free(component, cstream);
461         }
462 }
463 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
464
465 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
466 {
467         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
468         struct snd_soc_component *component;
469         int i, ret;
470
471         for_each_rtd_components(rtd, i, component) {
472                 if (component->driver->compress_ops &&
473                     component->driver->compress_ops->trigger) {
474                         ret = component->driver->compress_ops->trigger(
475                                 component, cstream, cmd);
476                         if (ret < 0)
477                                 return soc_component_ret(component, ret);
478                 }
479         }
480
481         return 0;
482 }
483 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
484
485 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
486                                        struct snd_compr_params *params)
487 {
488         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
489         struct snd_soc_component *component;
490         int i, ret;
491
492         for_each_rtd_components(rtd, i, component) {
493                 if (component->driver->compress_ops &&
494                     component->driver->compress_ops->set_params) {
495                         ret = component->driver->compress_ops->set_params(
496                                 component, cstream, params);
497                         if (ret < 0)
498                                 return soc_component_ret(component, ret);
499                 }
500         }
501
502         return 0;
503 }
504 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
505
506 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
507                                        struct snd_codec *params)
508 {
509         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
510         struct snd_soc_component *component;
511         int i, ret;
512
513         for_each_rtd_components(rtd, i, component) {
514                 if (component->driver->compress_ops &&
515                     component->driver->compress_ops->get_params) {
516                         ret = component->driver->compress_ops->get_params(
517                                 component, cstream, params);
518                         return soc_component_ret(component, ret);
519                 }
520         }
521
522         return 0;
523 }
524 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
525
526 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
527                                      struct snd_compr_caps *caps)
528 {
529         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
530         struct snd_soc_component *component;
531         int i, ret = 0;
532
533         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
534
535         for_each_rtd_components(rtd, i, component) {
536                 if (component->driver->compress_ops &&
537                     component->driver->compress_ops->get_caps) {
538                         ret = component->driver->compress_ops->get_caps(
539                                 component, cstream, caps);
540                         break;
541                 }
542         }
543
544         mutex_unlock(&rtd->card->pcm_mutex);
545
546         return soc_component_ret(component, ret);
547 }
548 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
549
550 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
551                                            struct snd_compr_codec_caps *codec)
552 {
553         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
554         struct snd_soc_component *component;
555         int i, ret = 0;
556
557         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
558
559         for_each_rtd_components(rtd, i, component) {
560                 if (component->driver->compress_ops &&
561                     component->driver->compress_ops->get_codec_caps) {
562                         ret = component->driver->compress_ops->get_codec_caps(
563                                 component, cstream, codec);
564                         break;
565                 }
566         }
567
568         mutex_unlock(&rtd->card->pcm_mutex);
569
570         return soc_component_ret(component, ret);
571 }
572 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
573
574 static unsigned int soc_component_read_no_lock(
575         struct snd_soc_component *component,
576         unsigned int reg)
577 {
578         int ret;
579         unsigned int val = 0;
580
581         if (component->regmap)
582                 ret = regmap_read(component->regmap, reg, &val);
583         else if (component->driver->read) {
584                 ret = 0;
585                 val = component->driver->read(component, reg);
586         }
587         else
588                 ret = -EIO;
589
590         if (ret < 0)
591                 return soc_component_ret(component, ret);
592
593         return val;
594 }
595
596 /**
597  * snd_soc_component_read() - Read register value
598  * @component: Component to read from
599  * @reg: Register to read
600  *
601  * Return: read value
602  */
603 unsigned int snd_soc_component_read(struct snd_soc_component *component,
604                                     unsigned int reg)
605 {
606         unsigned int val;
607
608         mutex_lock(&component->io_mutex);
609         val = soc_component_read_no_lock(component, reg);
610         mutex_unlock(&component->io_mutex);
611
612         return val;
613 }
614 EXPORT_SYMBOL_GPL(snd_soc_component_read);
615
616 static int soc_component_write_no_lock(
617         struct snd_soc_component *component,
618         unsigned int reg, unsigned int val)
619 {
620         int ret = -EIO;
621
622         if (component->regmap)
623                 ret = regmap_write(component->regmap, reg, val);
624         else if (component->driver->write)
625                 ret = component->driver->write(component, reg, val);
626
627         return soc_component_ret(component, ret);
628 }
629
630 /**
631  * snd_soc_component_write() - Write register value
632  * @component: Component to write to
633  * @reg: Register to write
634  * @val: Value to write to the register
635  *
636  * Return: 0 on success, a negative error code otherwise.
637  */
638 int snd_soc_component_write(struct snd_soc_component *component,
639                             unsigned int reg, unsigned int val)
640 {
641         int ret;
642
643         mutex_lock(&component->io_mutex);
644         ret = soc_component_write_no_lock(component, reg, val);
645         mutex_unlock(&component->io_mutex);
646
647         return ret;
648 }
649 EXPORT_SYMBOL_GPL(snd_soc_component_write);
650
651 static int snd_soc_component_update_bits_legacy(
652         struct snd_soc_component *component, unsigned int reg,
653         unsigned int mask, unsigned int val, bool *change)
654 {
655         unsigned int old, new;
656         int ret = 0;
657
658         mutex_lock(&component->io_mutex);
659
660         old = soc_component_read_no_lock(component, reg);
661
662         new = (old & ~mask) | (val & mask);
663         *change = old != new;
664         if (*change)
665                 ret = soc_component_write_no_lock(component, reg, new);
666
667         mutex_unlock(&component->io_mutex);
668
669         return soc_component_ret(component, ret);
670 }
671
672 /**
673  * snd_soc_component_update_bits() - Perform read/modify/write cycle
674  * @component: Component to update
675  * @reg: Register to update
676  * @mask: Mask that specifies which bits to update
677  * @val: New value for the bits specified by mask
678  *
679  * Return: 1 if the operation was successful and the value of the register
680  * changed, 0 if the operation was successful, but the value did not change.
681  * Returns a negative error code otherwise.
682  */
683 int snd_soc_component_update_bits(struct snd_soc_component *component,
684                                   unsigned int reg, unsigned int mask, unsigned int val)
685 {
686         bool change;
687         int ret;
688
689         if (component->regmap)
690                 ret = regmap_update_bits_check(component->regmap, reg, mask,
691                                                val, &change);
692         else
693                 ret = snd_soc_component_update_bits_legacy(component, reg,
694                                                            mask, val, &change);
695
696         if (ret < 0)
697                 return soc_component_ret(component, ret);
698         return change;
699 }
700 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
701
702 /**
703  * snd_soc_component_update_bits_async() - Perform asynchronous
704  *  read/modify/write cycle
705  * @component: Component to update
706  * @reg: Register to update
707  * @mask: Mask that specifies which bits to update
708  * @val: New value for the bits specified by mask
709  *
710  * This function is similar to snd_soc_component_update_bits(), but the update
711  * operation is scheduled asynchronously. This means it may not be completed
712  * when the function returns. To make sure that all scheduled updates have been
713  * completed snd_soc_component_async_complete() must be called.
714  *
715  * Return: 1 if the operation was successful and the value of the register
716  * changed, 0 if the operation was successful, but the value did not change.
717  * Returns a negative error code otherwise.
718  */
719 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
720                                         unsigned int reg, unsigned int mask, unsigned int val)
721 {
722         bool change;
723         int ret;
724
725         if (component->regmap)
726                 ret = regmap_update_bits_check_async(component->regmap, reg,
727                                                      mask, val, &change);
728         else
729                 ret = snd_soc_component_update_bits_legacy(component, reg,
730                                                            mask, val, &change);
731
732         if (ret < 0)
733                 return soc_component_ret(component, ret);
734         return change;
735 }
736 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
737
738 /**
739  * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
740  * @component: Component for which to wait
741  *
742  * This function blocks until all asynchronous I/O which has previously been
743  * scheduled using snd_soc_component_update_bits_async() has completed.
744  */
745 void snd_soc_component_async_complete(struct snd_soc_component *component)
746 {
747         if (component->regmap)
748                 regmap_async_complete(component->regmap);
749 }
750 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
751
752 /**
753  * snd_soc_component_test_bits - Test register for change
754  * @component: component
755  * @reg: Register to test
756  * @mask: Mask that specifies which bits to test
757  * @value: Value to test against
758  *
759  * Tests a register with a new value and checks if the new value is
760  * different from the old value.
761  *
762  * Return: 1 for change, otherwise 0.
763  */
764 int snd_soc_component_test_bits(struct snd_soc_component *component,
765                                 unsigned int reg, unsigned int mask, unsigned int value)
766 {
767         unsigned int old, new;
768
769         old = snd_soc_component_read(component, reg);
770         new = (old & ~mask) | value;
771         return old != new;
772 }
773 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
774
775 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
776 {
777         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
778         struct snd_soc_component *component;
779         int i;
780
781         /* FIXME: use 1st pointer */
782         for_each_rtd_components(rtd, i, component)
783                 if (component->driver->pointer)
784                         return component->driver->pointer(component, substream);
785
786         return 0;
787 }
788
789 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
790                                 unsigned int cmd, void *arg)
791 {
792         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
793         struct snd_soc_component *component;
794         int i;
795
796         /* FIXME: use 1st ioctl */
797         for_each_rtd_components(rtd, i, component)
798                 if (component->driver->ioctl)
799                         return soc_component_ret(
800                                 component,
801                                 component->driver->ioctl(component,
802                                                          substream, cmd, arg));
803
804         return snd_pcm_lib_ioctl(substream, cmd, arg);
805 }
806
807 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
808 {
809         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
810         struct snd_soc_component *component;
811         int i, ret;
812
813         for_each_rtd_components(rtd, i, component) {
814                 if (component->driver->sync_stop) {
815                         ret = component->driver->sync_stop(component,
816                                                            substream);
817                         if (ret < 0)
818                                 return soc_component_ret(component, ret);
819                 }
820         }
821
822         return 0;
823 }
824
825 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
826                                     int channel, unsigned long pos,
827                                     void __user *buf, unsigned long bytes)
828 {
829         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
830         struct snd_soc_component *component;
831         int i;
832
833         /* FIXME. it returns 1st copy now */
834         for_each_rtd_components(rtd, i, component)
835                 if (component->driver->copy_user)
836                         return soc_component_ret(
837                                 component,
838                                 component->driver->copy_user(
839                                         component, substream, channel,
840                                         pos, buf, bytes));
841
842         return -EINVAL;
843 }
844
845 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
846                                         unsigned long offset)
847 {
848         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
849         struct snd_soc_component *component;
850         struct page *page;
851         int i;
852
853         /* FIXME. it returns 1st page now */
854         for_each_rtd_components(rtd, i, component) {
855                 if (component->driver->page) {
856                         page = component->driver->page(component,
857                                                        substream, offset);
858                         if (page)
859                                 return page;
860                 }
861         }
862
863         return NULL;
864 }
865
866 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
867                                struct vm_area_struct *vma)
868 {
869         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
870         struct snd_soc_component *component;
871         int i;
872
873         /* FIXME. it returns 1st mmap now */
874         for_each_rtd_components(rtd, i, component)
875                 if (component->driver->mmap)
876                         return soc_component_ret(
877                                 component,
878                                 component->driver->mmap(component,
879                                                         substream, vma));
880
881         return -EINVAL;
882 }
883
884 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
885 {
886         struct snd_soc_component *component;
887         int ret;
888         int i;
889
890         for_each_rtd_components(rtd, i, component) {
891                 if (component->driver->pcm_construct) {
892                         ret = component->driver->pcm_construct(component, rtd);
893                         if (ret < 0)
894                                 return soc_component_ret(component, ret);
895                 }
896         }
897
898         return 0;
899 }
900
901 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
902 {
903         struct snd_soc_component *component;
904         int i;
905
906         if (!rtd->pcm)
907                 return;
908
909         for_each_rtd_components(rtd, i, component)
910                 if (component->driver->pcm_destruct)
911                         component->driver->pcm_destruct(component, rtd->pcm);
912 }
913
914 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
915 {
916         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
917         struct snd_soc_component *component;
918         int i, ret;
919
920         for_each_rtd_components(rtd, i, component) {
921                 if (component->driver->prepare) {
922                         ret = component->driver->prepare(component, substream);
923                         if (ret < 0)
924                                 return soc_component_ret(component, ret);
925                 }
926         }
927
928         return 0;
929 }
930
931 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
932                                     struct snd_pcm_hw_params *params)
933 {
934         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
935         struct snd_soc_component *component;
936         int i, ret;
937
938         for_each_rtd_components(rtd, i, component) {
939                 if (component->driver->hw_params) {
940                         ret = component->driver->hw_params(component,
941                                                            substream, params);
942                         if (ret < 0)
943                                 return soc_component_ret(component, ret);
944                 }
945                 /* mark substream if succeeded */
946                 soc_component_mark_push(component, substream, hw_params);
947         }
948
949         return 0;
950 }
951
952 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
953                                    int rollback)
954 {
955         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
956         struct snd_soc_component *component;
957         int i, ret;
958
959         for_each_rtd_components(rtd, i, component) {
960                 if (rollback && !soc_component_mark_match(component, substream, hw_params))
961                         continue;
962
963                 if (component->driver->hw_free) {
964                         ret = component->driver->hw_free(component, substream);
965                         if (ret < 0)
966                                 soc_component_ret(component, ret);
967                 }
968
969                 /* remove marked substream */
970                 soc_component_mark_pop(component, substream, hw_params);
971         }
972 }
973
974 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
975                                   int cmd)
976 {
977         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
978         struct snd_soc_component *component;
979         int i, ret;
980
981         for_each_rtd_components(rtd, i, component) {
982                 if (component->driver->trigger) {
983                         ret = component->driver->trigger(component, substream, cmd);
984                         if (ret < 0)
985                                 return soc_component_ret(component, ret);
986                 }
987         }
988
989         return 0;
990 }
991
992 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
993                                          void *stream)
994 {
995         struct snd_soc_component *component;
996         int i, ret;
997
998         for_each_rtd_components(rtd, i, component) {
999                 ret = pm_runtime_get_sync(component->dev);
1000                 if (ret < 0 && ret != -EACCES) {
1001                         pm_runtime_put_noidle(component->dev);
1002                         return soc_component_ret(component, ret);
1003                 }
1004                 /* mark stream if succeeded */
1005                 soc_component_mark_push(component, stream, pm);
1006         }
1007
1008         return 0;
1009 }
1010
1011 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1012                                           void *stream, int rollback)
1013 {
1014         struct snd_soc_component *component;
1015         int i;
1016
1017         for_each_rtd_components(rtd, i, component) {
1018                 if (rollback && !soc_component_mark_match(component, stream, pm))
1019                         continue;
1020
1021                 pm_runtime_mark_last_busy(component->dev);
1022                 pm_runtime_put_autosuspend(component->dev);
1023
1024                 /* remove marked stream */
1025                 soc_component_mark_pop(component, stream, pm);
1026         }
1027 }