Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // soc-component.c
0004 //
0005 // Copyright 2009-2011 Wolfson Microelectronics PLC.
0006 // Copyright (C) 2019 Renesas Electronics Corp.
0007 //
0008 // Mark Brown <broonie@opensource.wolfsonmicro.com>
0009 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
0010 //
0011 #include <linux/module.h>
0012 #include <linux/pm_runtime.h>
0013 #include <sound/soc.h>
0014 #include <linux/bitops.h>
0015 
0016 #define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret, -1)
0017 #define soc_component_ret_reg_rw(dai, ret, reg) _soc_component_ret(dai, __func__, ret, reg)
0018 static inline int _soc_component_ret(struct snd_soc_component *component,
0019                      const char *func, int ret, int reg)
0020 {
0021     /* Positive/Zero values are not errors */
0022     if (ret >= 0)
0023         return ret;
0024 
0025     /* Negative values might be errors */
0026     switch (ret) {
0027     case -EPROBE_DEFER:
0028     case -ENOTSUPP:
0029         break;
0030     default:
0031         if (reg == -1)
0032             dev_err(component->dev,
0033                 "ASoC: error at %s on %s: %d\n",
0034                 func, component->name, ret);
0035         else
0036             dev_err(component->dev,
0037                 "ASoC: error at %s on %s for register: [0x%08x] %d\n",
0038                 func, component->name, reg, ret);
0039     }
0040 
0041     return ret;
0042 }
0043 
0044 static inline int soc_component_field_shift(struct snd_soc_component *component,
0045                         unsigned int mask)
0046 {
0047     if (!mask) {
0048         dev_err(component->dev, "ASoC: error field mask is zero for %s\n",
0049             component->name);
0050         return 0;
0051     }
0052 
0053     return (ffs(mask) - 1);
0054 }
0055 
0056 /*
0057  * We might want to check substream by using list.
0058  * In such case, we can update these macros.
0059  */
0060 #define soc_component_mark_push(component, substream, tgt)  ((component)->mark_##tgt = substream)
0061 #define soc_component_mark_pop(component, substream, tgt)   ((component)->mark_##tgt = NULL)
0062 #define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
0063 
0064 void snd_soc_component_set_aux(struct snd_soc_component *component,
0065                    struct snd_soc_aux_dev *aux)
0066 {
0067     component->init = (aux) ? aux->init : NULL;
0068 }
0069 
0070 int snd_soc_component_init(struct snd_soc_component *component)
0071 {
0072     int ret = 0;
0073 
0074     if (component->init)
0075         ret = component->init(component);
0076 
0077     return soc_component_ret(component, ret);
0078 }
0079 
0080 /**
0081  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
0082  * @component: COMPONENT
0083  * @clk_id: DAI specific clock ID
0084  * @source: Source for the clock
0085  * @freq: new clock frequency in Hz
0086  * @dir: new clock direction - input/output.
0087  *
0088  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
0089  */
0090 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
0091                  int clk_id, int source, unsigned int freq,
0092                  int dir)
0093 {
0094     int ret = -ENOTSUPP;
0095 
0096     if (component->driver->set_sysclk)
0097         ret = component->driver->set_sysclk(component, clk_id, source,
0098                              freq, dir);
0099 
0100     return soc_component_ret(component, ret);
0101 }
0102 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
0103 
0104 /*
0105  * snd_soc_component_set_pll - configure component PLL.
0106  * @component: COMPONENT
0107  * @pll_id: DAI specific PLL ID
0108  * @source: DAI specific source for the PLL
0109  * @freq_in: PLL input clock frequency in Hz
0110  * @freq_out: requested PLL output clock frequency in Hz
0111  *
0112  * Configures and enables PLL to generate output clock based on input clock.
0113  */
0114 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
0115                   int source, unsigned int freq_in,
0116                   unsigned int freq_out)
0117 {
0118     int ret = -EINVAL;
0119 
0120     if (component->driver->set_pll)
0121         ret = component->driver->set_pll(component, pll_id, source,
0122                           freq_in, freq_out);
0123 
0124     return soc_component_ret(component, ret);
0125 }
0126 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
0127 
0128 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
0129                     enum snd_soc_dapm_type type, int subseq)
0130 {
0131     if (component->driver->seq_notifier)
0132         component->driver->seq_notifier(component, type, subseq);
0133 }
0134 
0135 int snd_soc_component_stream_event(struct snd_soc_component *component,
0136                    int event)
0137 {
0138     int ret = 0;
0139 
0140     if (component->driver->stream_event)
0141         ret = component->driver->stream_event(component, event);
0142 
0143     return soc_component_ret(component, ret);
0144 }
0145 
0146 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
0147                      enum snd_soc_bias_level level)
0148 {
0149     int ret = 0;
0150 
0151     if (component->driver->set_bias_level)
0152         ret = component->driver->set_bias_level(component, level);
0153 
0154     return soc_component_ret(component, ret);
0155 }
0156 
0157 int snd_soc_component_enable_pin(struct snd_soc_component *component,
0158                  const char *pin)
0159 {
0160     struct snd_soc_dapm_context *dapm =
0161         snd_soc_component_get_dapm(component);
0162     return snd_soc_dapm_enable_pin(dapm, pin);
0163 }
0164 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
0165 
0166 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
0167                       const char *pin)
0168 {
0169     struct snd_soc_dapm_context *dapm =
0170         snd_soc_component_get_dapm(component);
0171     return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
0172 }
0173 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
0174 
0175 int snd_soc_component_disable_pin(struct snd_soc_component *component,
0176                   const char *pin)
0177 {
0178     struct snd_soc_dapm_context *dapm =
0179         snd_soc_component_get_dapm(component);
0180     return snd_soc_dapm_disable_pin(dapm, pin);
0181 }
0182 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
0183 
0184 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
0185                        const char *pin)
0186 {
0187     struct snd_soc_dapm_context *dapm = 
0188         snd_soc_component_get_dapm(component);
0189     return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
0190 }
0191 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
0192 
0193 int snd_soc_component_nc_pin(struct snd_soc_component *component,
0194                  const char *pin)
0195 {
0196     struct snd_soc_dapm_context *dapm =
0197         snd_soc_component_get_dapm(component);
0198     return snd_soc_dapm_nc_pin(dapm, pin);
0199 }
0200 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
0201 
0202 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
0203                       const char *pin)
0204 {
0205     struct snd_soc_dapm_context *dapm =
0206         snd_soc_component_get_dapm(component);
0207     return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
0208 }
0209 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
0210 
0211 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
0212                      const char *pin)
0213 {
0214     struct snd_soc_dapm_context *dapm =
0215         snd_soc_component_get_dapm(component);
0216     return snd_soc_dapm_get_pin_status(dapm, pin);
0217 }
0218 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
0219 
0220 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
0221                        const char *pin)
0222 {
0223     struct snd_soc_dapm_context *dapm =
0224         snd_soc_component_get_dapm(component);
0225     return snd_soc_dapm_force_enable_pin(dapm, pin);
0226 }
0227 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
0228 
0229 int snd_soc_component_force_enable_pin_unlocked(
0230     struct snd_soc_component *component,
0231     const char *pin)
0232 {
0233     struct snd_soc_dapm_context *dapm =
0234         snd_soc_component_get_dapm(component);
0235     return snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
0236 }
0237 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
0238 
0239 /**
0240  * snd_soc_component_set_jack - configure component jack.
0241  * @component: COMPONENTs
0242  * @jack: structure to use for the jack
0243  * @data: can be used if codec driver need extra data for configuring jack
0244  *
0245  * Configures and enables jack detection function.
0246  */
0247 int snd_soc_component_set_jack(struct snd_soc_component *component,
0248                    struct snd_soc_jack *jack, void *data)
0249 {
0250     int ret = -ENOTSUPP;
0251 
0252     if (component->driver->set_jack)
0253         ret = component->driver->set_jack(component, jack, data);
0254 
0255     return soc_component_ret(component, ret);
0256 }
0257 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
0258 
0259 int snd_soc_component_module_get(struct snd_soc_component *component,
0260                  void *mark, int upon_open)
0261 {
0262     int ret = 0;
0263 
0264     if (component->driver->module_get_upon_open == !!upon_open &&
0265         !try_module_get(component->dev->driver->owner))
0266         ret = -ENODEV;
0267 
0268     /* mark module if succeeded */
0269     if (ret == 0)
0270         soc_component_mark_push(component, mark, module);
0271 
0272     return soc_component_ret(component, ret);
0273 }
0274 
0275 void snd_soc_component_module_put(struct snd_soc_component *component,
0276                   void *mark, int upon_open, int rollback)
0277 {
0278     if (rollback && !soc_component_mark_match(component, mark, module))
0279         return;
0280 
0281     if (component->driver->module_get_upon_open == !!upon_open)
0282         module_put(component->dev->driver->owner);
0283 
0284     /* remove the mark from module */
0285     soc_component_mark_pop(component, mark, module);
0286 }
0287 
0288 int snd_soc_component_open(struct snd_soc_component *component,
0289                struct snd_pcm_substream *substream)
0290 {
0291     int ret = 0;
0292 
0293     if (component->driver->open)
0294         ret = component->driver->open(component, substream);
0295 
0296     /* mark substream if succeeded */
0297     if (ret == 0)
0298         soc_component_mark_push(component, substream, open);
0299 
0300     return soc_component_ret(component, ret);
0301 }
0302 
0303 int snd_soc_component_close(struct snd_soc_component *component,
0304                 struct snd_pcm_substream *substream,
0305                 int rollback)
0306 {
0307     int ret = 0;
0308 
0309     if (rollback && !soc_component_mark_match(component, substream, open))
0310         return 0;
0311 
0312     if (component->driver->close)
0313         ret = component->driver->close(component, substream);
0314 
0315     /* remove marked substream */
0316     soc_component_mark_pop(component, substream, open);
0317 
0318     return soc_component_ret(component, ret);
0319 }
0320 
0321 void snd_soc_component_suspend(struct snd_soc_component *component)
0322 {
0323     if (component->driver->suspend)
0324         component->driver->suspend(component);
0325     component->suspended = 1;
0326 }
0327 
0328 void snd_soc_component_resume(struct snd_soc_component *component)
0329 {
0330     if (component->driver->resume)
0331         component->driver->resume(component);
0332     component->suspended = 0;
0333 }
0334 
0335 int snd_soc_component_is_suspended(struct snd_soc_component *component)
0336 {
0337     return component->suspended;
0338 }
0339 
0340 int snd_soc_component_probe(struct snd_soc_component *component)
0341 {
0342     int ret = 0;
0343 
0344     if (component->driver->probe)
0345         ret = component->driver->probe(component);
0346 
0347     return soc_component_ret(component, ret);
0348 }
0349 
0350 void snd_soc_component_remove(struct snd_soc_component *component)
0351 {
0352     if (component->driver->remove)
0353         component->driver->remove(component);
0354 }
0355 
0356 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
0357                       struct device_node *ep)
0358 {
0359     int ret = -ENOTSUPP;
0360 
0361     if (component->driver->of_xlate_dai_id)
0362         ret = component->driver->of_xlate_dai_id(component, ep);
0363 
0364     return soc_component_ret(component, ret);
0365 }
0366 
0367 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
0368                     const struct of_phandle_args *args,
0369                     const char **dai_name)
0370 {
0371     if (component->driver->of_xlate_dai_name)
0372         return component->driver->of_xlate_dai_name(component,
0373                                 args, dai_name);
0374     /*
0375      * Don't use soc_component_ret here because we may not want to report
0376      * the error just yet. If a device has more than one component, the
0377      * first may not match and we don't want spam the log with this.
0378      */
0379     return -ENOTSUPP;
0380 }
0381 
0382 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
0383 {
0384     int val_bytes = regmap_get_val_bytes(component->regmap);
0385 
0386     /* Errors are legitimate for non-integer byte multiples */
0387     if (val_bytes > 0)
0388         component->val_bytes = val_bytes;
0389 }
0390 
0391 #ifdef CONFIG_REGMAP
0392 
0393 /**
0394  * snd_soc_component_init_regmap() - Initialize regmap instance for the
0395  *                                   component
0396  * @component: The component for which to initialize the regmap instance
0397  * @regmap: The regmap instance that should be used by the component
0398  *
0399  * This function allows deferred assignment of the regmap instance that is
0400  * associated with the component. Only use this if the regmap instance is not
0401  * yet ready when the component is registered. The function must also be called
0402  * before the first IO attempt of the component.
0403  */
0404 void snd_soc_component_init_regmap(struct snd_soc_component *component,
0405                    struct regmap *regmap)
0406 {
0407     component->regmap = regmap;
0408     snd_soc_component_setup_regmap(component);
0409 }
0410 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
0411 
0412 /**
0413  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
0414  *                                   component
0415  * @component: The component for which to de-initialize the regmap instance
0416  *
0417  * Calls regmap_exit() on the regmap instance associated to the component and
0418  * removes the regmap instance from the component.
0419  *
0420  * This function should only be used if snd_soc_component_init_regmap() was used
0421  * to initialize the regmap instance.
0422  */
0423 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
0424 {
0425     regmap_exit(component->regmap);
0426     component->regmap = NULL;
0427 }
0428 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
0429 
0430 #endif
0431 
0432 int snd_soc_component_compr_open(struct snd_soc_component *component,
0433                  struct snd_compr_stream *cstream)
0434 {
0435     int ret = 0;
0436 
0437     if (component->driver->compress_ops &&
0438         component->driver->compress_ops->open)
0439         ret = component->driver->compress_ops->open(component, cstream);
0440 
0441     /* mark substream if succeeded */
0442     if (ret == 0)
0443         soc_component_mark_push(component, cstream, compr_open);
0444 
0445     return soc_component_ret(component, ret);
0446 }
0447 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
0448 
0449 void snd_soc_component_compr_free(struct snd_soc_component *component,
0450                   struct snd_compr_stream *cstream,
0451                   int rollback)
0452 {
0453     if (rollback && !soc_component_mark_match(component, cstream, compr_open))
0454         return;
0455 
0456     if (component->driver->compress_ops &&
0457         component->driver->compress_ops->free)
0458         component->driver->compress_ops->free(component, cstream);
0459 
0460     /* remove marked substream */
0461     soc_component_mark_pop(component, cstream, compr_open);
0462 }
0463 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
0464 
0465 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
0466 {
0467     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0468     struct snd_soc_component *component;
0469     int i, ret;
0470 
0471     for_each_rtd_components(rtd, i, component) {
0472         if (component->driver->compress_ops &&
0473             component->driver->compress_ops->trigger) {
0474             ret = component->driver->compress_ops->trigger(
0475                 component, cstream, cmd);
0476             if (ret < 0)
0477                 return soc_component_ret(component, ret);
0478         }
0479     }
0480 
0481     return 0;
0482 }
0483 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
0484 
0485 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
0486                        struct snd_compr_params *params)
0487 {
0488     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0489     struct snd_soc_component *component;
0490     int i, ret;
0491 
0492     for_each_rtd_components(rtd, i, component) {
0493         if (component->driver->compress_ops &&
0494             component->driver->compress_ops->set_params) {
0495             ret = component->driver->compress_ops->set_params(
0496                 component, cstream, params);
0497             if (ret < 0)
0498                 return soc_component_ret(component, ret);
0499         }
0500     }
0501 
0502     return 0;
0503 }
0504 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
0505 
0506 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
0507                        struct snd_codec *params)
0508 {
0509     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0510     struct snd_soc_component *component;
0511     int i, ret;
0512 
0513     for_each_rtd_components(rtd, i, component) {
0514         if (component->driver->compress_ops &&
0515             component->driver->compress_ops->get_params) {
0516             ret = component->driver->compress_ops->get_params(
0517                 component, cstream, params);
0518             return soc_component_ret(component, ret);
0519         }
0520     }
0521 
0522     return 0;
0523 }
0524 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
0525 
0526 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
0527                      struct snd_compr_caps *caps)
0528 {
0529     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0530     struct snd_soc_component *component;
0531     int i, ret = 0;
0532 
0533     mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
0534 
0535     for_each_rtd_components(rtd, i, component) {
0536         if (component->driver->compress_ops &&
0537             component->driver->compress_ops->get_caps) {
0538             ret = component->driver->compress_ops->get_caps(
0539                 component, cstream, caps);
0540             break;
0541         }
0542     }
0543 
0544     mutex_unlock(&rtd->card->pcm_mutex);
0545 
0546     return soc_component_ret(component, ret);
0547 }
0548 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
0549 
0550 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
0551                        struct snd_compr_codec_caps *codec)
0552 {
0553     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0554     struct snd_soc_component *component;
0555     int i, ret = 0;
0556 
0557     mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
0558 
0559     for_each_rtd_components(rtd, i, component) {
0560         if (component->driver->compress_ops &&
0561             component->driver->compress_ops->get_codec_caps) {
0562             ret = component->driver->compress_ops->get_codec_caps(
0563                 component, cstream, codec);
0564             break;
0565         }
0566     }
0567 
0568     mutex_unlock(&rtd->card->pcm_mutex);
0569 
0570     return soc_component_ret(component, ret);
0571 }
0572 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
0573 
0574 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
0575 {
0576     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0577     struct snd_soc_component *component;
0578     int i, ret;
0579 
0580     for_each_rtd_components(rtd, i, component) {
0581         if (component->driver->compress_ops &&
0582             component->driver->compress_ops->ack) {
0583             ret = component->driver->compress_ops->ack(
0584                 component, cstream, bytes);
0585             if (ret < 0)
0586                 return soc_component_ret(component, ret);
0587         }
0588     }
0589 
0590     return 0;
0591 }
0592 EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
0593 
0594 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
0595                     struct snd_compr_tstamp *tstamp)
0596 {
0597     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0598     struct snd_soc_component *component;
0599     int i, ret;
0600 
0601     for_each_rtd_components(rtd, i, component) {
0602         if (component->driver->compress_ops &&
0603             component->driver->compress_ops->pointer) {
0604             ret = component->driver->compress_ops->pointer(
0605                 component, cstream, tstamp);
0606             return soc_component_ret(component, ret);
0607         }
0608     }
0609 
0610     return 0;
0611 }
0612 EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
0613 
0614 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
0615                  char __user *buf, size_t count)
0616 {
0617     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0618     struct snd_soc_component *component;
0619     int i, ret = 0;
0620 
0621     mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
0622 
0623     for_each_rtd_components(rtd, i, component) {
0624         if (component->driver->compress_ops &&
0625             component->driver->compress_ops->copy) {
0626             ret = component->driver->compress_ops->copy(
0627                 component, cstream, buf, count);
0628             break;
0629         }
0630     }
0631 
0632     mutex_unlock(&rtd->card->pcm_mutex);
0633 
0634     return soc_component_ret(component, ret);
0635 }
0636 EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
0637 
0638 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
0639                      struct snd_compr_metadata *metadata)
0640 {
0641     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0642     struct snd_soc_component *component;
0643     int i, ret;
0644 
0645     for_each_rtd_components(rtd, i, component) {
0646         if (component->driver->compress_ops &&
0647             component->driver->compress_ops->set_metadata) {
0648             ret = component->driver->compress_ops->set_metadata(
0649                 component, cstream, metadata);
0650             if (ret < 0)
0651                 return soc_component_ret(component, ret);
0652         }
0653     }
0654 
0655     return 0;
0656 }
0657 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
0658 
0659 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
0660                      struct snd_compr_metadata *metadata)
0661 {
0662     struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0663     struct snd_soc_component *component;
0664     int i, ret;
0665 
0666     for_each_rtd_components(rtd, i, component) {
0667         if (component->driver->compress_ops &&
0668             component->driver->compress_ops->get_metadata) {
0669             ret = component->driver->compress_ops->get_metadata(
0670                 component, cstream, metadata);
0671             return soc_component_ret(component, ret);
0672         }
0673     }
0674 
0675     return 0;
0676 }
0677 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
0678 
0679 static unsigned int soc_component_read_no_lock(
0680     struct snd_soc_component *component,
0681     unsigned int reg)
0682 {
0683     int ret;
0684     unsigned int val = 0;
0685 
0686     if (component->regmap)
0687         ret = regmap_read(component->regmap, reg, &val);
0688     else if (component->driver->read) {
0689         ret = 0;
0690         val = component->driver->read(component, reg);
0691     }
0692     else
0693         ret = -EIO;
0694 
0695     if (ret < 0)
0696         return soc_component_ret_reg_rw(component, ret, reg);
0697 
0698     return val;
0699 }
0700 
0701 /**
0702  * snd_soc_component_read() - Read register value
0703  * @component: Component to read from
0704  * @reg: Register to read
0705  *
0706  * Return: read value
0707  */
0708 unsigned int snd_soc_component_read(struct snd_soc_component *component,
0709                     unsigned int reg)
0710 {
0711     unsigned int val;
0712 
0713     mutex_lock(&component->io_mutex);
0714     val = soc_component_read_no_lock(component, reg);
0715     mutex_unlock(&component->io_mutex);
0716 
0717     return val;
0718 }
0719 EXPORT_SYMBOL_GPL(snd_soc_component_read);
0720 
0721 static int soc_component_write_no_lock(
0722     struct snd_soc_component *component,
0723     unsigned int reg, unsigned int val)
0724 {
0725     int ret = -EIO;
0726 
0727     if (component->regmap)
0728         ret = regmap_write(component->regmap, reg, val);
0729     else if (component->driver->write)
0730         ret = component->driver->write(component, reg, val);
0731 
0732     return soc_component_ret_reg_rw(component, ret, reg);
0733 }
0734 
0735 /**
0736  * snd_soc_component_write() - Write register value
0737  * @component: Component to write to
0738  * @reg: Register to write
0739  * @val: Value to write to the register
0740  *
0741  * Return: 0 on success, a negative error code otherwise.
0742  */
0743 int snd_soc_component_write(struct snd_soc_component *component,
0744                 unsigned int reg, unsigned int val)
0745 {
0746     int ret;
0747 
0748     mutex_lock(&component->io_mutex);
0749     ret = soc_component_write_no_lock(component, reg, val);
0750     mutex_unlock(&component->io_mutex);
0751 
0752     return ret;
0753 }
0754 EXPORT_SYMBOL_GPL(snd_soc_component_write);
0755 
0756 static int snd_soc_component_update_bits_legacy(
0757     struct snd_soc_component *component, unsigned int reg,
0758     unsigned int mask, unsigned int val, bool *change)
0759 {
0760     unsigned int old, new;
0761     int ret = 0;
0762 
0763     mutex_lock(&component->io_mutex);
0764 
0765     old = soc_component_read_no_lock(component, reg);
0766 
0767     new = (old & ~mask) | (val & mask);
0768     *change = old != new;
0769     if (*change)
0770         ret = soc_component_write_no_lock(component, reg, new);
0771 
0772     mutex_unlock(&component->io_mutex);
0773 
0774     return soc_component_ret_reg_rw(component, ret, reg);
0775 }
0776 
0777 /**
0778  * snd_soc_component_update_bits() - Perform read/modify/write cycle
0779  * @component: Component to update
0780  * @reg: Register to update
0781  * @mask: Mask that specifies which bits to update
0782  * @val: New value for the bits specified by mask
0783  *
0784  * Return: 1 if the operation was successful and the value of the register
0785  * changed, 0 if the operation was successful, but the value did not change.
0786  * Returns a negative error code otherwise.
0787  */
0788 int snd_soc_component_update_bits(struct snd_soc_component *component,
0789                   unsigned int reg, unsigned int mask, unsigned int val)
0790 {
0791     bool change;
0792     int ret;
0793 
0794     if (component->regmap)
0795         ret = regmap_update_bits_check(component->regmap, reg, mask,
0796                            val, &change);
0797     else
0798         ret = snd_soc_component_update_bits_legacy(component, reg,
0799                                mask, val, &change);
0800 
0801     if (ret < 0)
0802         return soc_component_ret_reg_rw(component, ret, reg);
0803     return change;
0804 }
0805 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
0806 
0807 /**
0808  * snd_soc_component_update_bits_async() - Perform asynchronous
0809  *  read/modify/write cycle
0810  * @component: Component to update
0811  * @reg: Register to update
0812  * @mask: Mask that specifies which bits to update
0813  * @val: New value for the bits specified by mask
0814  *
0815  * This function is similar to snd_soc_component_update_bits(), but the update
0816  * operation is scheduled asynchronously. This means it may not be completed
0817  * when the function returns. To make sure that all scheduled updates have been
0818  * completed snd_soc_component_async_complete() must be called.
0819  *
0820  * Return: 1 if the operation was successful and the value of the register
0821  * changed, 0 if the operation was successful, but the value did not change.
0822  * Returns a negative error code otherwise.
0823  */
0824 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
0825                     unsigned int reg, unsigned int mask, unsigned int val)
0826 {
0827     bool change;
0828     int ret;
0829 
0830     if (component->regmap)
0831         ret = regmap_update_bits_check_async(component->regmap, reg,
0832                              mask, val, &change);
0833     else
0834         ret = snd_soc_component_update_bits_legacy(component, reg,
0835                                mask, val, &change);
0836 
0837     if (ret < 0)
0838         return soc_component_ret_reg_rw(component, ret, reg);
0839     return change;
0840 }
0841 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
0842 
0843 /**
0844  * snd_soc_component_read_field() - Read register field value
0845  * @component: Component to read from
0846  * @reg: Register to read
0847  * @mask: mask of the register field
0848  *
0849  * Return: read value of register field.
0850  */
0851 unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
0852                       unsigned int reg, unsigned int mask)
0853 {
0854     unsigned int val;
0855 
0856     val = snd_soc_component_read(component, reg);
0857 
0858     val = (val & mask) >> soc_component_field_shift(component, mask);
0859 
0860     return val;
0861 }
0862 EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
0863 
0864 /**
0865  * snd_soc_component_write_field() - write to register field
0866  * @component: Component to write to
0867  * @reg: Register to write
0868  * @mask: mask of the register field to update
0869  * @val: value of the field to write
0870  *
0871  * Return: 1 for change, otherwise 0.
0872  */
0873 int snd_soc_component_write_field(struct snd_soc_component *component,
0874                   unsigned int reg, unsigned int mask,
0875                   unsigned int val)
0876 {
0877 
0878     val = (val << soc_component_field_shift(component, mask)) & mask;
0879 
0880     return snd_soc_component_update_bits(component, reg, mask, val);
0881 }
0882 EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
0883 
0884 /**
0885  * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
0886  * @component: Component for which to wait
0887  *
0888  * This function blocks until all asynchronous I/O which has previously been
0889  * scheduled using snd_soc_component_update_bits_async() has completed.
0890  */
0891 void snd_soc_component_async_complete(struct snd_soc_component *component)
0892 {
0893     if (component->regmap)
0894         regmap_async_complete(component->regmap);
0895 }
0896 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
0897 
0898 /**
0899  * snd_soc_component_test_bits - Test register for change
0900  * @component: component
0901  * @reg: Register to test
0902  * @mask: Mask that specifies which bits to test
0903  * @value: Value to test against
0904  *
0905  * Tests a register with a new value and checks if the new value is
0906  * different from the old value.
0907  *
0908  * Return: 1 for change, otherwise 0.
0909  */
0910 int snd_soc_component_test_bits(struct snd_soc_component *component,
0911                 unsigned int reg, unsigned int mask, unsigned int value)
0912 {
0913     unsigned int old, new;
0914 
0915     old = snd_soc_component_read(component, reg);
0916     new = (old & ~mask) | value;
0917     return old != new;
0918 }
0919 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
0920 
0921 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
0922 {
0923     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0924     struct snd_soc_component *component;
0925     int i;
0926 
0927     /* FIXME: use 1st pointer */
0928     for_each_rtd_components(rtd, i, component)
0929         if (component->driver->pointer)
0930             return component->driver->pointer(component, substream);
0931 
0932     return 0;
0933 }
0934 
0935 static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd,
0936                           struct snd_soc_component *component)
0937 {
0938     struct snd_soc_dai *dai;
0939     int i;
0940 
0941     for_each_rtd_codec_dais(rtd, i, dai) {
0942         if (dai->component == component)
0943             return true;
0944     }
0945 
0946     return false;
0947 }
0948 
0949 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
0950                  snd_pcm_sframes_t *cpu_delay,
0951                  snd_pcm_sframes_t *codec_delay)
0952 {
0953     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0954     struct snd_soc_component *component;
0955     snd_pcm_sframes_t delay;
0956     int i;
0957 
0958     /*
0959      * We're looking for the delay through the full audio path so it needs to
0960      * be the maximum of the Components doing transmit and the maximum of the
0961      * Components doing receive (ie, all CPUs and all CODECs) rather than
0962      * just the maximum of all Components.
0963      */
0964     for_each_rtd_components(rtd, i, component) {
0965         if (!component->driver->delay)
0966             continue;
0967 
0968         delay = component->driver->delay(component, substream);
0969 
0970         if (snd_soc_component_is_codec_on_rtd(rtd, component))
0971             *codec_delay = max(*codec_delay, delay);
0972         else
0973             *cpu_delay = max(*cpu_delay, delay);
0974     }
0975 }
0976 
0977 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
0978                 unsigned int cmd, void *arg)
0979 {
0980     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0981     struct snd_soc_component *component;
0982     int i;
0983 
0984     /* FIXME: use 1st ioctl */
0985     for_each_rtd_components(rtd, i, component)
0986         if (component->driver->ioctl)
0987             return soc_component_ret(
0988                 component,
0989                 component->driver->ioctl(component,
0990                              substream, cmd, arg));
0991 
0992     return snd_pcm_lib_ioctl(substream, cmd, arg);
0993 }
0994 
0995 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
0996 {
0997     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0998     struct snd_soc_component *component;
0999     int i, ret;
1000 
1001     for_each_rtd_components(rtd, i, component) {
1002         if (component->driver->sync_stop) {
1003             ret = component->driver->sync_stop(component,
1004                                substream);
1005             if (ret < 0)
1006                 return soc_component_ret(component, ret);
1007         }
1008     }
1009 
1010     return 0;
1011 }
1012 
1013 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
1014                     int channel, unsigned long pos,
1015                     void __user *buf, unsigned long bytes)
1016 {
1017     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1018     struct snd_soc_component *component;
1019     int i;
1020 
1021     /* FIXME. it returns 1st copy now */
1022     for_each_rtd_components(rtd, i, component)
1023         if (component->driver->copy_user)
1024             return soc_component_ret(
1025                 component,
1026                 component->driver->copy_user(
1027                     component, substream, channel,
1028                     pos, buf, bytes));
1029 
1030     return -EINVAL;
1031 }
1032 
1033 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
1034                     unsigned long offset)
1035 {
1036     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1037     struct snd_soc_component *component;
1038     struct page *page;
1039     int i;
1040 
1041     /* FIXME. it returns 1st page now */
1042     for_each_rtd_components(rtd, i, component) {
1043         if (component->driver->page) {
1044             page = component->driver->page(component,
1045                                substream, offset);
1046             if (page)
1047                 return page;
1048         }
1049     }
1050 
1051     return NULL;
1052 }
1053 
1054 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
1055                    struct vm_area_struct *vma)
1056 {
1057     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1058     struct snd_soc_component *component;
1059     int i;
1060 
1061     /* FIXME. it returns 1st mmap now */
1062     for_each_rtd_components(rtd, i, component)
1063         if (component->driver->mmap)
1064             return soc_component_ret(
1065                 component,
1066                 component->driver->mmap(component,
1067                             substream, vma));
1068 
1069     return -EINVAL;
1070 }
1071 
1072 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
1073 {
1074     struct snd_soc_component *component;
1075     int ret;
1076     int i;
1077 
1078     for_each_rtd_components(rtd, i, component) {
1079         if (component->driver->pcm_construct) {
1080             ret = component->driver->pcm_construct(component, rtd);
1081             if (ret < 0)
1082                 return soc_component_ret(component, ret);
1083         }
1084     }
1085 
1086     return 0;
1087 }
1088 
1089 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
1090 {
1091     struct snd_soc_component *component;
1092     int i;
1093 
1094     if (!rtd->pcm)
1095         return;
1096 
1097     for_each_rtd_components(rtd, i, component)
1098         if (component->driver->pcm_destruct)
1099             component->driver->pcm_destruct(component, rtd->pcm);
1100 }
1101 
1102 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1103 {
1104     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1105     struct snd_soc_component *component;
1106     int i, ret;
1107 
1108     for_each_rtd_components(rtd, i, component) {
1109         if (component->driver->prepare) {
1110             ret = component->driver->prepare(component, substream);
1111             if (ret < 0)
1112                 return soc_component_ret(component, ret);
1113         }
1114     }
1115 
1116     return 0;
1117 }
1118 
1119 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
1120                     struct snd_pcm_hw_params *params)
1121 {
1122     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1123     struct snd_soc_component *component;
1124     int i, ret;
1125 
1126     for_each_rtd_components(rtd, i, component) {
1127         if (component->driver->hw_params) {
1128             ret = component->driver->hw_params(component,
1129                                substream, params);
1130             if (ret < 0)
1131                 return soc_component_ret(component, ret);
1132         }
1133         /* mark substream if succeeded */
1134         soc_component_mark_push(component, substream, hw_params);
1135     }
1136 
1137     return 0;
1138 }
1139 
1140 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
1141                    int rollback)
1142 {
1143     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1144     struct snd_soc_component *component;
1145     int i, ret;
1146 
1147     for_each_rtd_components(rtd, i, component) {
1148         if (rollback && !soc_component_mark_match(component, substream, hw_params))
1149             continue;
1150 
1151         if (component->driver->hw_free) {
1152             ret = component->driver->hw_free(component, substream);
1153             if (ret < 0)
1154                 soc_component_ret(component, ret);
1155         }
1156 
1157         /* remove marked substream */
1158         soc_component_mark_pop(component, substream, hw_params);
1159     }
1160 }
1161 
1162 static int soc_component_trigger(struct snd_soc_component *component,
1163                  struct snd_pcm_substream *substream,
1164                  int cmd)
1165 {
1166     int ret = 0;
1167 
1168     if (component->driver->trigger)
1169         ret = component->driver->trigger(component, substream, cmd);
1170 
1171     return soc_component_ret(component, ret);
1172 }
1173 
1174 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
1175                   int cmd, int rollback)
1176 {
1177     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1178     struct snd_soc_component *component;
1179     int i, r, ret = 0;
1180 
1181     switch (cmd) {
1182     case SNDRV_PCM_TRIGGER_START:
1183     case SNDRV_PCM_TRIGGER_RESUME:
1184     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1185         for_each_rtd_components(rtd, i, component) {
1186             ret = soc_component_trigger(component, substream, cmd);
1187             if (ret < 0)
1188                 break;
1189             soc_component_mark_push(component, substream, trigger);
1190         }
1191         break;
1192     case SNDRV_PCM_TRIGGER_STOP:
1193     case SNDRV_PCM_TRIGGER_SUSPEND:
1194     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1195         for_each_rtd_components(rtd, i, component) {
1196             if (rollback && !soc_component_mark_match(component, substream, trigger))
1197                 continue;
1198 
1199             r = soc_component_trigger(component, substream, cmd);
1200             if (r < 0)
1201                 ret = r; /* use last ret */
1202             soc_component_mark_pop(component, substream, trigger);
1203         }
1204     }
1205 
1206     return ret;
1207 }
1208 
1209 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1210                      void *stream)
1211 {
1212     struct snd_soc_component *component;
1213     int i;
1214 
1215     for_each_rtd_components(rtd, i, component) {
1216         int ret = pm_runtime_get_sync(component->dev);
1217         if (ret < 0 && ret != -EACCES) {
1218             pm_runtime_put_noidle(component->dev);
1219             return soc_component_ret(component, ret);
1220         }
1221         /* mark stream if succeeded */
1222         soc_component_mark_push(component, stream, pm);
1223     }
1224 
1225     return 0;
1226 }
1227 
1228 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1229                       void *stream, int rollback)
1230 {
1231     struct snd_soc_component *component;
1232     int i;
1233 
1234     for_each_rtd_components(rtd, i, component) {
1235         if (rollback && !soc_component_mark_match(component, stream, pm))
1236             continue;
1237 
1238         pm_runtime_mark_last_busy(component->dev);
1239         pm_runtime_put_autosuspend(component->dev);
1240 
1241         /* remove marked stream */
1242         soc_component_mark_pop(component, stream, pm);
1243     }
1244 }
1245 
1246 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
1247 {
1248     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1249     struct snd_soc_component *component;
1250     int i;
1251 
1252     /* FIXME: use 1st pointer */
1253     for_each_rtd_components(rtd, i, component)
1254         if (component->driver->ack)
1255             return component->driver->ack(component, substream);
1256 
1257     return 0;
1258 }