blob: 8e8d917d22f8ffe9a982642f03a27be6335ae91e [file] [log] [blame]
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +09001// SPDX-License-Identifier: GPL-2.0
2//
3// soc-component.c
4//
Kuninori Morimoto460b42d2020-06-04 17:08:03 +09005// Copyright 2009-2011 Wolfson Microelectronics PLC.
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +09006// Copyright (C) 2019 Renesas Electronics Corp.
Kuninori Morimoto460b42d2020-06-04 17:08:03 +09007//
8// Mark Brown <broonie@opensource.wolfsonmicro.com>
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +09009// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
10//
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +090011#include <linux/module.h>
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +090012#include <linux/pm_runtime.h>
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090013#include <sound/soc.h>
Srinivas Kandagatla8ac9e472021-01-29 10:05:39 +000014#include <linux/bitops.h>
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090015
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090016#define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
17static inline int _soc_component_ret(struct snd_soc_component *component,
18 const char *func, int ret)
19{
20 /* Positive/Zero values are not errors */
21 if (ret >= 0)
22 return ret;
23
24 /* Negative values might be errors */
25 switch (ret) {
26 case -EPROBE_DEFER:
27 case -ENOTSUPP:
28 break;
29 default:
30 dev_err(component->dev,
31 "ASoC: error at %s on %s: %d\n",
32 func, component->name, ret);
33 }
34
35 return ret;
36}
37
Srinivas Kandagatla1da0b982021-01-26 17:17:48 +000038static inline int soc_component_field_shift(struct snd_soc_component *component,
39 unsigned int mask)
40{
41 if (!mask) {
42 dev_err(component->dev, "ASoC: error field mask is zero for %s\n",
43 component->name);
44 return 0;
45 }
46
Srinivas Kandagatla8ac9e472021-01-29 10:05:39 +000047 return (ffs(mask) - 1);
Srinivas Kandagatla1da0b982021-01-26 17:17:48 +000048}
49
Kuninori Morimoto51aff912020-09-28 09:01:04 +090050/*
51 * We might want to check substream by using list.
52 * In such case, we can update these macros.
53 */
54#define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
55#define soc_component_mark_pop(component, substream, tgt) ((component)->mark_##tgt = NULL)
56#define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
57
Kuninori Morimoto257c4da2020-06-04 17:07:54 +090058void snd_soc_component_set_aux(struct snd_soc_component *component,
59 struct snd_soc_aux_dev *aux)
60{
61 component->init = (aux) ? aux->init : NULL;
62}
63
64int snd_soc_component_init(struct snd_soc_component *component)
65{
66 int ret = 0;
67
68 if (component->init)
69 ret = component->init(component);
70
71 return soc_component_ret(component, ret);
72}
73
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090074/**
75 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
76 * @component: COMPONENT
77 * @clk_id: DAI specific clock ID
78 * @source: Source for the clock
79 * @freq: new clock frequency in Hz
80 * @dir: new clock direction - input/output.
81 *
82 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
83 */
84int snd_soc_component_set_sysclk(struct snd_soc_component *component,
85 int clk_id, int source, unsigned int freq,
86 int dir)
87{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090088 int ret = -ENOTSUPP;
89
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090090 if (component->driver->set_sysclk)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090091 ret = component->driver->set_sysclk(component, clk_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090092 freq, dir);
93
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090094 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090095}
96EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
97
98/*
99 * snd_soc_component_set_pll - configure component PLL.
100 * @component: COMPONENT
101 * @pll_id: DAI specific PLL ID
102 * @source: DAI specific source for the PLL
103 * @freq_in: PLL input clock frequency in Hz
104 * @freq_out: requested PLL output clock frequency in Hz
105 *
106 * Configures and enables PLL to generate output clock based on input clock.
107 */
108int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
109 int source, unsigned int freq_in,
110 unsigned int freq_out)
111{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900112 int ret = -EINVAL;
113
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900114 if (component->driver->set_pll)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900115 ret = component->driver->set_pll(component, pll_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900116 freq_in, freq_out);
117
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900118 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900119}
120EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
121
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +0900122void snd_soc_component_seq_notifier(struct snd_soc_component *component,
123 enum snd_soc_dapm_type type, int subseq)
124{
125 if (component->driver->seq_notifier)
126 component->driver->seq_notifier(component, type, subseq);
127}
128
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900129int snd_soc_component_stream_event(struct snd_soc_component *component,
130 int event)
131{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900132 int ret = 0;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900133
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900134 if (component->driver->stream_event)
135 ret = component->driver->stream_event(component, event);
136
137 return soc_component_ret(component, ret);
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900138}
139
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900140int snd_soc_component_set_bias_level(struct snd_soc_component *component,
141 enum snd_soc_bias_level level)
142{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900143 int ret = 0;
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900144
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900145 if (component->driver->set_bias_level)
146 ret = component->driver->set_bias_level(component, level);
147
148 return soc_component_ret(component, ret);
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900149}
150
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900151int snd_soc_component_enable_pin(struct snd_soc_component *component,
152 const char *pin)
153{
Mark Brown31428c72021-07-26 20:41:23 +0100154 struct snd_soc_dapm_context *dapm =
155 snd_soc_component_get_dapm(component);
156 return snd_soc_dapm_enable_pin(dapm, pin);
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900157}
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900158EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
159
160int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
161 const char *pin)
162{
Mark Brown31428c72021-07-26 20:41:23 +0100163 struct snd_soc_dapm_context *dapm =
164 snd_soc_component_get_dapm(component);
165 return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900166}
167EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
168
169int snd_soc_component_disable_pin(struct snd_soc_component *component,
170 const char *pin)
171{
Mark Brown31428c72021-07-26 20:41:23 +0100172 struct snd_soc_dapm_context *dapm =
173 snd_soc_component_get_dapm(component);
174 return snd_soc_dapm_disable_pin(dapm, pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900175}
176EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
177
178int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
179 const char *pin)
180{
Mark Brown31428c72021-07-26 20:41:23 +0100181 struct snd_soc_dapm_context *dapm =
182 snd_soc_component_get_dapm(component);
183 return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900184}
185EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
186
187int snd_soc_component_nc_pin(struct snd_soc_component *component,
188 const char *pin)
189{
Mark Brown31428c72021-07-26 20:41:23 +0100190 struct snd_soc_dapm_context *dapm =
191 snd_soc_component_get_dapm(component);
192 return snd_soc_dapm_nc_pin(dapm, pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900193}
194EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
195
196int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
197 const char *pin)
198{
Mark Brown31428c72021-07-26 20:41:23 +0100199 struct snd_soc_dapm_context *dapm =
200 snd_soc_component_get_dapm(component);
201 return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900202}
203EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
204
205int snd_soc_component_get_pin_status(struct snd_soc_component *component,
206 const char *pin)
207{
Mark Brown31428c72021-07-26 20:41:23 +0100208 struct snd_soc_dapm_context *dapm =
209 snd_soc_component_get_dapm(component);
210 return snd_soc_dapm_get_pin_status(dapm, pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900211}
212EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
213
214int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
215 const char *pin)
216{
Mark Brown31428c72021-07-26 20:41:23 +0100217 struct snd_soc_dapm_context *dapm =
218 snd_soc_component_get_dapm(component);
219 return snd_soc_dapm_force_enable_pin(dapm, pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900220}
221EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
222
223int snd_soc_component_force_enable_pin_unlocked(
224 struct snd_soc_component *component,
225 const char *pin)
226{
Mark Brown31428c72021-07-26 20:41:23 +0100227 struct snd_soc_dapm_context *dapm =
228 snd_soc_component_get_dapm(component);
229 return snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900230}
231EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
232
233/**
234 * snd_soc_component_set_jack - configure component jack.
235 * @component: COMPONENTs
236 * @jack: structure to use for the jack
237 * @data: can be used if codec driver need extra data for configuring jack
238 *
239 * Configures and enables jack detection function.
240 */
241int snd_soc_component_set_jack(struct snd_soc_component *component,
242 struct snd_soc_jack *jack, void *data)
243{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900244 int ret = -ENOTSUPP;
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900245
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900246 if (component->driver->set_jack)
247 ret = component->driver->set_jack(component, jack, data);
248
249 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900250}
251EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900252
253int snd_soc_component_module_get(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900254 struct snd_pcm_substream *substream,
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900255 int upon_open)
256{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900257 int ret = 0;
258
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900259 if (component->driver->module_get_upon_open == !!upon_open &&
260 !try_module_get(component->dev->driver->owner))
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900261 ret = -ENODEV;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900262
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900263 /* mark substream if succeeded */
264 if (ret == 0)
265 soc_component_mark_push(component, substream, module);
266
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900267 return soc_component_ret(component, ret);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900268}
269
270void snd_soc_component_module_put(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900271 struct snd_pcm_substream *substream,
272 int upon_open, int rollback)
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900273{
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900274 if (rollback && !soc_component_mark_match(component, substream, module))
275 return;
276
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900277 if (component->driver->module_get_upon_open == !!upon_open)
278 module_put(component->dev->driver->owner);
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900279
280 /* remove marked substream */
281 soc_component_mark_pop(component, substream, module);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900282}
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900283
284int snd_soc_component_open(struct snd_soc_component *component,
285 struct snd_pcm_substream *substream)
286{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900287 int ret = 0;
288
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900289 if (component->driver->open)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900290 ret = component->driver->open(component, substream);
291
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900292 /* mark substream if succeeded */
293 if (ret == 0)
294 soc_component_mark_push(component, substream, open);
295
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900296 return soc_component_ret(component, ret);
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900297}
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900298
299int snd_soc_component_close(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900300 struct snd_pcm_substream *substream,
301 int rollback)
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900302{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900303 int ret = 0;
304
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900305 if (rollback && !soc_component_mark_match(component, substream, open))
306 return 0;
307
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900308 if (component->driver->close)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900309 ret = component->driver->close(component, substream);
310
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900311 /* remove marked substream */
312 soc_component_mark_pop(component, substream, open);
313
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900314 return soc_component_ret(component, ret);
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900315}
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900316
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900317void snd_soc_component_suspend(struct snd_soc_component *component)
318{
319 if (component->driver->suspend)
320 component->driver->suspend(component);
321 component->suspended = 1;
322}
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900323
324void snd_soc_component_resume(struct snd_soc_component *component)
325{
326 if (component->driver->resume)
327 component->driver->resume(component);
328 component->suspended = 0;
329}
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900330
331int snd_soc_component_is_suspended(struct snd_soc_component *component)
332{
333 return component->suspended;
334}
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900335
336int snd_soc_component_probe(struct snd_soc_component *component)
337{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900338 int ret = 0;
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900339
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900340 if (component->driver->probe)
341 ret = component->driver->probe(component);
342
343 return soc_component_ret(component, ret);
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900344}
Kuninori Morimoto03b34dd2019-07-26 13:51:22 +0900345
346void snd_soc_component_remove(struct snd_soc_component *component)
347{
348 if (component->driver->remove)
349 component->driver->remove(component);
350}
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900351
352int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
353 struct device_node *ep)
354{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900355 int ret = -ENOTSUPP;
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900356
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900357 if (component->driver->of_xlate_dai_id)
358 ret = component->driver->of_xlate_dai_id(component, ep);
359
360 return soc_component_ret(component, ret);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900361}
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900362
363int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
Krzysztof Kozlowski933f98b2021-02-21 16:30:24 +0100364 const struct of_phandle_args *args,
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900365 const char **dai_name)
366{
367 if (component->driver->of_xlate_dai_name)
Jerome Brunetcc4d8ce2020-07-23 16:20:20 +0200368 return component->driver->of_xlate_dai_name(component,
369 args, dai_name);
370 /*
371 * Don't use soc_component_ret here because we may not want to report
372 * the error just yet. If a device has more than one component, the
373 * first may not match and we don't want spam the log with this.
374 */
375 return -ENOTSUPP;
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900376}
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900377
Kuninori Morimotoc7d75b52020-06-04 17:06:22 +0900378void snd_soc_component_setup_regmap(struct snd_soc_component *component)
379{
380 int val_bytes = regmap_get_val_bytes(component->regmap);
381
382 /* Errors are legitimate for non-integer byte multiples */
383 if (val_bytes > 0)
384 component->val_bytes = val_bytes;
385}
386
387#ifdef CONFIG_REGMAP
388
389/**
390 * snd_soc_component_init_regmap() - Initialize regmap instance for the
391 * component
392 * @component: The component for which to initialize the regmap instance
393 * @regmap: The regmap instance that should be used by the component
394 *
395 * This function allows deferred assignment of the regmap instance that is
396 * associated with the component. Only use this if the regmap instance is not
397 * yet ready when the component is registered. The function must also be called
398 * before the first IO attempt of the component.
399 */
400void snd_soc_component_init_regmap(struct snd_soc_component *component,
401 struct regmap *regmap)
402{
403 component->regmap = regmap;
404 snd_soc_component_setup_regmap(component);
405}
406EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
407
408/**
409 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
410 * component
411 * @component: The component for which to de-initialize the regmap instance
412 *
413 * Calls regmap_exit() on the regmap instance associated to the component and
414 * removes the regmap instance from the component.
415 *
416 * This function should only be used if snd_soc_component_init_regmap() was used
417 * to initialize the regmap instance.
418 */
419void snd_soc_component_exit_regmap(struct snd_soc_component *component)
420{
421 regmap_exit(component->regmap);
422 component->regmap = NULL;
423}
424EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
425
426#endif
427
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900428int snd_soc_component_compr_open(struct snd_compr_stream *cstream)
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900429{
430 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
431 struct snd_soc_component *component;
432 int i, ret;
433
434 for_each_rtd_components(rtd, i, component) {
435 if (component->driver->compress_ops &&
436 component->driver->compress_ops->open) {
437 ret = component->driver->compress_ops->open(component, cstream);
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900438 if (ret < 0)
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900439 return soc_component_ret(component, ret);
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900440 }
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900441 soc_component_mark_push(component, cstream, compr_open);
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900442 }
443
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900444 return 0;
445}
446EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
447
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900448void snd_soc_component_compr_free(struct snd_compr_stream *cstream,
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900449 int rollback)
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900450{
451 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
452 struct snd_soc_component *component;
453 int i;
454
455 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900456 if (rollback && !soc_component_mark_match(component, cstream, compr_open))
457 continue;
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900458
459 if (component->driver->compress_ops &&
460 component->driver->compress_ops->free)
461 component->driver->compress_ops->free(component, cstream);
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900462
463 soc_component_mark_pop(component, cstream, compr_open);
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900464 }
465}
466EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
467
Kuninori Morimoto08aee252020-11-13 13:15:33 +0900468int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
469{
470 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
471 struct snd_soc_component *component;
472 int i, ret;
473
474 for_each_rtd_components(rtd, i, component) {
475 if (component->driver->compress_ops &&
476 component->driver->compress_ops->trigger) {
477 ret = component->driver->compress_ops->trigger(
478 component, cstream, cmd);
479 if (ret < 0)
480 return soc_component_ret(component, ret);
481 }
482 }
483
484 return 0;
485}
486EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
487
Kuninori Morimotoff08cf82020-11-13 13:15:49 +0900488int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
489 struct snd_compr_params *params)
490{
491 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
492 struct snd_soc_component *component;
493 int i, ret;
494
495 for_each_rtd_components(rtd, i, component) {
496 if (component->driver->compress_ops &&
497 component->driver->compress_ops->set_params) {
498 ret = component->driver->compress_ops->set_params(
499 component, cstream, params);
500 if (ret < 0)
501 return soc_component_ret(component, ret);
502 }
503 }
504
505 return 0;
506}
507EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
508
Kuninori Morimoto77c221e2020-11-13 13:15:56 +0900509int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
510 struct snd_codec *params)
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->get_params) {
519 ret = component->driver->compress_ops->get_params(
520 component, cstream, params);
521 return soc_component_ret(component, ret);
522 }
523 }
524
525 return 0;
526}
527EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
528
Kuninori Morimotod67fcb22020-11-13 13:16:03 +0900529int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
530 struct snd_compr_caps *caps)
531{
532 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
533 struct snd_soc_component *component;
534 int i, ret = 0;
535
536 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
537
538 for_each_rtd_components(rtd, i, component) {
539 if (component->driver->compress_ops &&
540 component->driver->compress_ops->get_caps) {
541 ret = component->driver->compress_ops->get_caps(
542 component, cstream, caps);
543 break;
544 }
545 }
546
547 mutex_unlock(&rtd->card->pcm_mutex);
548
549 return soc_component_ret(component, ret);
550}
551EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
552
Kuninori Morimoto0f6fe092020-11-13 13:16:11 +0900553int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
554 struct snd_compr_codec_caps *codec)
555{
556 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
557 struct snd_soc_component *component;
558 int i, ret = 0;
559
560 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
561
562 for_each_rtd_components(rtd, i, component) {
563 if (component->driver->compress_ops &&
564 component->driver->compress_ops->get_codec_caps) {
565 ret = component->driver->compress_ops->get_codec_caps(
566 component, cstream, codec);
567 break;
568 }
569 }
570
571 mutex_unlock(&rtd->card->pcm_mutex);
572
573 return soc_component_ret(component, ret);
574}
575EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
576
Kuninori Morimoto0506b882020-11-13 13:16:17 +0900577int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
578{
579 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
580 struct snd_soc_component *component;
581 int i, ret;
582
583 for_each_rtd_components(rtd, i, component) {
584 if (component->driver->compress_ops &&
585 component->driver->compress_ops->ack) {
586 ret = component->driver->compress_ops->ack(
587 component, cstream, bytes);
588 if (ret < 0)
589 return soc_component_ret(component, ret);
590 }
591 }
592
593 return 0;
594}
595EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
596
Kuninori Morimoto03ecea62020-11-13 13:16:24 +0900597int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
598 struct snd_compr_tstamp *tstamp)
599{
600 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
601 struct snd_soc_component *component;
602 int i, ret;
603
604 for_each_rtd_components(rtd, i, component) {
605 if (component->driver->compress_ops &&
606 component->driver->compress_ops->pointer) {
607 ret = component->driver->compress_ops->pointer(
608 component, cstream, tstamp);
609 return soc_component_ret(component, ret);
610 }
611 }
612
613 return 0;
614}
615EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
616
Kuninori Morimotob5852e62020-11-13 13:16:30 +0900617int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
618 char __user *buf, size_t count)
619{
620 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
621 struct snd_soc_component *component;
622 int i, ret = 0;
623
624 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
625
626 for_each_rtd_components(rtd, i, component) {
627 if (component->driver->compress_ops &&
628 component->driver->compress_ops->copy) {
629 ret = component->driver->compress_ops->copy(
630 component, cstream, buf, count);
631 break;
632 }
633 }
634
635 mutex_unlock(&rtd->card->pcm_mutex);
636
637 return soc_component_ret(component, ret);
638}
639EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
640
Kuninori Morimoto1b308fb2020-11-13 13:16:36 +0900641int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
642 struct snd_compr_metadata *metadata)
643{
644 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
645 struct snd_soc_component *component;
646 int i, ret;
647
648 for_each_rtd_components(rtd, i, component) {
649 if (component->driver->compress_ops &&
650 component->driver->compress_ops->set_metadata) {
651 ret = component->driver->compress_ops->set_metadata(
652 component, cstream, metadata);
653 if (ret < 0)
654 return soc_component_ret(component, ret);
655 }
656 }
657
658 return 0;
659}
660EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
661
Kuninori Morimotobab78c22020-11-13 13:16:41 +0900662int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
663 struct snd_compr_metadata *metadata)
664{
665 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
666 struct snd_soc_component *component;
667 int i, ret;
668
669 for_each_rtd_components(rtd, i, component) {
670 if (component->driver->compress_ops &&
671 component->driver->compress_ops->get_metadata) {
672 ret = component->driver->compress_ops->get_metadata(
673 component, cstream, metadata);
674 return soc_component_ret(component, ret);
675 }
676 }
677
678 return 0;
679}
680EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
681
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900682static unsigned int soc_component_read_no_lock(
683 struct snd_soc_component *component,
684 unsigned int reg)
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900685{
686 int ret;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900687 unsigned int val = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900688
689 if (component->regmap)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900690 ret = regmap_read(component->regmap, reg, &val);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900691 else if (component->driver->read) {
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900692 ret = 0;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900693 val = component->driver->read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900694 }
695 else
696 ret = -EIO;
697
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900698 if (ret < 0)
Takashi Iwaiefc913c2020-08-10 15:46:31 +0200699 return soc_component_ret(component, ret);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900700
701 return val;
702}
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900703
704/**
705 * snd_soc_component_read() - Read register value
706 * @component: Component to read from
707 * @reg: Register to read
708 *
709 * Return: read value
710 */
711unsigned int snd_soc_component_read(struct snd_soc_component *component,
712 unsigned int reg)
713{
714 unsigned int val;
715
716 mutex_lock(&component->io_mutex);
717 val = soc_component_read_no_lock(component, reg);
718 mutex_unlock(&component->io_mutex);
719
720 return val;
721}
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900722EXPORT_SYMBOL_GPL(snd_soc_component_read);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900723
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900724static int soc_component_write_no_lock(
725 struct snd_soc_component *component,
726 unsigned int reg, unsigned int val)
727{
728 int ret = -EIO;
729
730 if (component->regmap)
731 ret = regmap_write(component->regmap, reg, val);
732 else if (component->driver->write)
733 ret = component->driver->write(component, reg, val);
734
735 return soc_component_ret(component, ret);
736}
737
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900738/**
739 * snd_soc_component_write() - Write register value
740 * @component: Component to write to
741 * @reg: Register to write
742 * @val: Value to write to the register
743 *
744 * Return: 0 on success, a negative error code otherwise.
745 */
746int snd_soc_component_write(struct snd_soc_component *component,
747 unsigned int reg, unsigned int val)
748{
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900749 int ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900750
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900751 mutex_lock(&component->io_mutex);
752 ret = soc_component_write_no_lock(component, reg, val);
753 mutex_unlock(&component->io_mutex);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900754
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900755 return ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900756}
757EXPORT_SYMBOL_GPL(snd_soc_component_write);
758
759static int snd_soc_component_update_bits_legacy(
760 struct snd_soc_component *component, unsigned int reg,
761 unsigned int mask, unsigned int val, bool *change)
762{
763 unsigned int old, new;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900764 int ret = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900765
766 mutex_lock(&component->io_mutex);
767
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900768 old = soc_component_read_no_lock(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900769
770 new = (old & ~mask) | (val & mask);
771 *change = old != new;
772 if (*change)
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900773 ret = soc_component_write_no_lock(component, reg, new);
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900774
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900775 mutex_unlock(&component->io_mutex);
776
777 return soc_component_ret(component, ret);
778}
779
780/**
781 * snd_soc_component_update_bits() - Perform read/modify/write cycle
782 * @component: Component to update
783 * @reg: Register to update
784 * @mask: Mask that specifies which bits to update
785 * @val: New value for the bits specified by mask
786 *
787 * Return: 1 if the operation was successful and the value of the register
788 * changed, 0 if the operation was successful, but the value did not change.
789 * Returns a negative error code otherwise.
790 */
791int snd_soc_component_update_bits(struct snd_soc_component *component,
792 unsigned int reg, unsigned int mask, unsigned int val)
793{
794 bool change;
795 int ret;
796
797 if (component->regmap)
798 ret = regmap_update_bits_check(component->regmap, reg, mask,
799 val, &change);
800 else
801 ret = snd_soc_component_update_bits_legacy(component, reg,
802 mask, val, &change);
803
804 if (ret < 0)
805 return soc_component_ret(component, ret);
806 return change;
807}
808EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
809
810/**
811 * snd_soc_component_update_bits_async() - Perform asynchronous
812 * read/modify/write cycle
813 * @component: Component to update
814 * @reg: Register to update
815 * @mask: Mask that specifies which bits to update
816 * @val: New value for the bits specified by mask
817 *
818 * This function is similar to snd_soc_component_update_bits(), but the update
819 * operation is scheduled asynchronously. This means it may not be completed
820 * when the function returns. To make sure that all scheduled updates have been
821 * completed snd_soc_component_async_complete() must be called.
822 *
823 * Return: 1 if the operation was successful and the value of the register
824 * changed, 0 if the operation was successful, but the value did not change.
825 * Returns a negative error code otherwise.
826 */
827int snd_soc_component_update_bits_async(struct snd_soc_component *component,
828 unsigned int reg, unsigned int mask, unsigned int val)
829{
830 bool change;
831 int ret;
832
833 if (component->regmap)
834 ret = regmap_update_bits_check_async(component->regmap, reg,
835 mask, val, &change);
836 else
837 ret = snd_soc_component_update_bits_legacy(component, reg,
838 mask, val, &change);
839
840 if (ret < 0)
841 return soc_component_ret(component, ret);
842 return change;
843}
844EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
845
846/**
Srinivas Kandagatla1da0b982021-01-26 17:17:48 +0000847 * snd_soc_component_read_field() - Read register field value
848 * @component: Component to read from
849 * @reg: Register to read
850 * @mask: mask of the register field
851 *
852 * Return: read value of register field.
853 */
854unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
855 unsigned int reg, unsigned int mask)
856{
857 unsigned int val;
858
859 val = snd_soc_component_read(component, reg);
860
861 val = (val & mask) >> soc_component_field_shift(component, mask);
862
863 return val;
864}
865EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
866
867/**
868 * snd_soc_component_write_field() - write to register field
869 * @component: Component to write to
870 * @reg: Register to write
871 * @mask: mask of the register field to update
872 * @val: value of the field to write
873 *
874 * Return: 1 for change, otherwise 0.
875 */
876int snd_soc_component_write_field(struct snd_soc_component *component,
877 unsigned int reg, unsigned int mask,
878 unsigned int val)
879{
880
881 val = (val << soc_component_field_shift(component, mask)) & mask;
882
883 return snd_soc_component_update_bits(component, reg, mask, val);
884}
885EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
886
887/**
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900888 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
889 * @component: Component for which to wait
890 *
891 * This function blocks until all asynchronous I/O which has previously been
892 * scheduled using snd_soc_component_update_bits_async() has completed.
893 */
894void snd_soc_component_async_complete(struct snd_soc_component *component)
895{
896 if (component->regmap)
897 regmap_async_complete(component->regmap);
898}
899EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
900
901/**
902 * snd_soc_component_test_bits - Test register for change
903 * @component: component
904 * @reg: Register to test
905 * @mask: Mask that specifies which bits to test
906 * @value: Value to test against
907 *
908 * Tests a register with a new value and checks if the new value is
909 * different from the old value.
910 *
911 * Return: 1 for change, otherwise 0.
912 */
913int snd_soc_component_test_bits(struct snd_soc_component *component,
914 unsigned int reg, unsigned int mask, unsigned int value)
915{
916 unsigned int old, new;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900917
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900918 old = snd_soc_component_read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900919 new = (old & ~mask) | value;
920 return old != new;
921}
922EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
923
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900924int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
925{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900926 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900927 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900928 int i;
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900929
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900930 /* FIXME: use 1st pointer */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900931 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900932 if (component->driver->pointer)
933 return component->driver->pointer(component, substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900934
935 return 0;
936}
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900937
938int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
939 unsigned int cmd, void *arg)
940{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900941 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900942 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900943 int i;
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900944
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900945 /* FIXME: use 1st ioctl */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900946 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900947 if (component->driver->ioctl)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900948 return soc_component_ret(
949 component,
950 component->driver->ioctl(component,
951 substream, cmd, arg));
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900952
953 return snd_pcm_lib_ioctl(substream, cmd, arg);
954}
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900955
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100956int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
957{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900958 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100959 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900960 int i, ret;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100961
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900962 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof1861a72020-02-28 10:48:35 +0900963 if (component->driver->sync_stop) {
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100964 ret = component->driver->sync_stop(component,
965 substream);
966 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800967 return soc_component_ret(component, ret);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100968 }
969 }
970
971 return 0;
972}
973
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900974int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
975 int channel, unsigned long pos,
976 void __user *buf, unsigned long bytes)
977{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900978 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900979 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900980 int i;
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900981
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900982 /* FIXME. it returns 1st copy now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900983 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900984 if (component->driver->copy_user)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900985 return soc_component_ret(
986 component,
987 component->driver->copy_user(
988 component, substream, channel,
989 pos, buf, bytes));
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900990
991 return -EINVAL;
992}
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900993
994struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
995 unsigned long offset)
996{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900997 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900998 struct snd_soc_component *component;
999 struct page *page;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001000 int i;
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001001
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001002 /* FIXME. it returns 1st page now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001003 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +09001004 if (component->driver->page) {
1005 page = component->driver->page(component,
1006 substream, offset);
1007 if (page)
1008 return page;
1009 }
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001010 }
1011
1012 return NULL;
1013}
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001014
1015int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
1016 struct vm_area_struct *vma)
1017{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001018 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001019 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001020 int i;
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001021
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001022 /* FIXME. it returns 1st mmap now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001023 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +09001024 if (component->driver->mmap)
Shengjiu Wangbe75db52020-07-16 13:07:08 +08001025 return soc_component_ret(
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +09001026 component,
1027 component->driver->mmap(component,
1028 substream, vma));
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001029
1030 return -EINVAL;
1031}
Kuninori Morimoto74842912019-07-26 13:52:08 +09001032
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001033int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto74842912019-07-26 13:52:08 +09001034{
Kuninori Morimoto74842912019-07-26 13:52:08 +09001035 struct snd_soc_component *component;
1036 int ret;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001037 int i;
Kuninori Morimoto74842912019-07-26 13:52:08 +09001038
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001039 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001040 if (component->driver->pcm_construct) {
1041 ret = component->driver->pcm_construct(component, rtd);
1042 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +08001043 return soc_component_ret(component, ret);
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001044 }
Kuninori Morimoto74842912019-07-26 13:52:08 +09001045 }
1046
1047 return 0;
1048}
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001049
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001050void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001051{
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001052 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001053 int i;
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001054
Takashi Iwai8e3366c2020-01-07 08:09:56 +01001055 if (!rtd->pcm)
1056 return;
1057
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001058 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001059 if (component->driver->pcm_destruct)
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001060 component->driver->pcm_destruct(component, rtd->pcm);
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001061}
Kuninori Morimoto4f395142020-06-04 17:06:58 +09001062
1063int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1064{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001065 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto4f395142020-06-04 17:06:58 +09001066 struct snd_soc_component *component;
1067 int i, ret;
1068
1069 for_each_rtd_components(rtd, i, component) {
1070 if (component->driver->prepare) {
1071 ret = component->driver->prepare(component, substream);
1072 if (ret < 0)
1073 return soc_component_ret(component, ret);
1074 }
1075 }
1076
1077 return 0;
1078}
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001079
1080int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001081 struct snd_pcm_hw_params *params)
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001082{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001083 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001084 struct snd_soc_component *component;
1085 int i, ret;
1086
1087 for_each_rtd_components(rtd, i, component) {
1088 if (component->driver->hw_params) {
1089 ret = component->driver->hw_params(component,
1090 substream, params);
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001091 if (ret < 0)
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001092 return soc_component_ret(component, ret);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001093 }
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001094 /* mark substream if succeeded */
1095 soc_component_mark_push(component, substream, hw_params);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001096 }
1097
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001098 return 0;
1099}
Kuninori Morimoto04751112020-06-04 17:07:24 +09001100
1101void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001102 int rollback)
Kuninori Morimoto04751112020-06-04 17:07:24 +09001103{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001104 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto04751112020-06-04 17:07:24 +09001105 struct snd_soc_component *component;
1106 int i, ret;
1107
1108 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001109 if (rollback && !soc_component_mark_match(component, substream, hw_params))
1110 continue;
Kuninori Morimoto04751112020-06-04 17:07:24 +09001111
1112 if (component->driver->hw_free) {
1113 ret = component->driver->hw_free(component, substream);
1114 if (ret < 0)
1115 soc_component_ret(component, ret);
1116 }
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001117
1118 /* remove marked substream */
1119 soc_component_mark_pop(component, substream, hw_params);
Kuninori Morimoto04751112020-06-04 17:07:24 +09001120 }
1121}
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001122
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001123static int soc_component_trigger(struct snd_soc_component *component,
1124 struct snd_pcm_substream *substream,
1125 int cmd)
1126{
1127 int ret = 0;
1128
1129 if (component->driver->trigger)
1130 ret = component->driver->trigger(component, substream, cmd);
1131
1132 return soc_component_ret(component, ret);
1133}
1134
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001135int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001136 int cmd, int rollback)
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001137{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001138 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001139 struct snd_soc_component *component;
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001140 int i, r, ret = 0;
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001141
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001142 switch (cmd) {
1143 case SNDRV_PCM_TRIGGER_START:
1144 case SNDRV_PCM_TRIGGER_RESUME:
1145 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1146 for_each_rtd_components(rtd, i, component) {
1147 ret = soc_component_trigger(component, substream, cmd);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001148 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001149 break;
1150 soc_component_mark_push(component, substream, trigger);
1151 }
1152 break;
1153 case SNDRV_PCM_TRIGGER_STOP:
1154 case SNDRV_PCM_TRIGGER_SUSPEND:
1155 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1156 for_each_rtd_components(rtd, i, component) {
1157 if (rollback && !soc_component_mark_match(component, substream, trigger))
1158 continue;
1159
1160 r = soc_component_trigger(component, substream, cmd);
1161 if (r < 0)
1162 ret = r; /* use last ret */
1163 soc_component_mark_pop(component, substream, trigger);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001164 }
1165 }
1166
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001167 return ret;
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001168}
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +09001169
1170int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1171 void *stream)
1172{
1173 struct snd_soc_component *component;
Kuninori Morimoto500b39d2021-08-16 13:56:19 +09001174 int i;
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +09001175
1176 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto500b39d2021-08-16 13:56:19 +09001177 int ret = pm_runtime_get_sync(component->dev);
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +09001178 if (ret < 0 && ret != -EACCES) {
1179 pm_runtime_put_noidle(component->dev);
1180 return soc_component_ret(component, ret);
1181 }
1182 /* mark stream if succeeded */
1183 soc_component_mark_push(component, stream, pm);
1184 }
1185
1186 return 0;
1187}
1188
1189void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1190 void *stream, int rollback)
1191{
1192 struct snd_soc_component *component;
1193 int i;
1194
1195 for_each_rtd_components(rtd, i, component) {
1196 if (rollback && !soc_component_mark_match(component, stream, pm))
1197 continue;
1198
1199 pm_runtime_mark_last_busy(component->dev);
1200 pm_runtime_put_autosuspend(component->dev);
1201
1202 /* remove marked stream */
1203 soc_component_mark_pop(component, stream, pm);
1204 }
1205}
Shengjiu Wang8bdfc042021-03-12 10:38:40 +08001206
1207int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
1208{
1209 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1210 struct snd_soc_component *component;
1211 int i;
1212
1213 /* FIXME: use 1st pointer */
1214 for_each_rtd_components(rtd, i, component)
1215 if (component->driver->ack)
1216 return component->driver->ack(component, substream);
1217
1218 return 0;
1219}