blob: 5d3a8dcd8d49536a5463a551c4b6ca8e1a8fcd44 [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>
14
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090015#define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
16static inline int _soc_component_ret(struct snd_soc_component *component,
17 const char *func, int ret)
18{
19 /* Positive/Zero values are not errors */
20 if (ret >= 0)
21 return ret;
22
23 /* Negative values might be errors */
24 switch (ret) {
25 case -EPROBE_DEFER:
26 case -ENOTSUPP:
27 break;
28 default:
29 dev_err(component->dev,
30 "ASoC: error at %s on %s: %d\n",
31 func, component->name, ret);
32 }
33
34 return ret;
35}
36
Kuninori Morimoto51aff912020-09-28 09:01:04 +090037/*
38 * We might want to check substream by using list.
39 * In such case, we can update these macros.
40 */
41#define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
42#define soc_component_mark_pop(component, substream, tgt) ((component)->mark_##tgt = NULL)
43#define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
44
Kuninori Morimoto257c4da2020-06-04 17:07:54 +090045void snd_soc_component_set_aux(struct snd_soc_component *component,
46 struct snd_soc_aux_dev *aux)
47{
48 component->init = (aux) ? aux->init : NULL;
49}
50
51int snd_soc_component_init(struct snd_soc_component *component)
52{
53 int ret = 0;
54
55 if (component->init)
56 ret = component->init(component);
57
58 return soc_component_ret(component, ret);
59}
60
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090061/**
62 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
63 * @component: COMPONENT
64 * @clk_id: DAI specific clock ID
65 * @source: Source for the clock
66 * @freq: new clock frequency in Hz
67 * @dir: new clock direction - input/output.
68 *
69 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
70 */
71int snd_soc_component_set_sysclk(struct snd_soc_component *component,
72 int clk_id, int source, unsigned int freq,
73 int dir)
74{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090075 int ret = -ENOTSUPP;
76
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090077 if (component->driver->set_sysclk)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090078 ret = component->driver->set_sysclk(component, clk_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090079 freq, dir);
80
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090081 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090082}
83EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
84
85/*
86 * snd_soc_component_set_pll - configure component PLL.
87 * @component: COMPONENT
88 * @pll_id: DAI specific PLL ID
89 * @source: DAI specific source for the PLL
90 * @freq_in: PLL input clock frequency in Hz
91 * @freq_out: requested PLL output clock frequency in Hz
92 *
93 * Configures and enables PLL to generate output clock based on input clock.
94 */
95int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
96 int source, unsigned int freq_in,
97 unsigned int freq_out)
98{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090099 int ret = -EINVAL;
100
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900101 if (component->driver->set_pll)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900102 ret = component->driver->set_pll(component, pll_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900103 freq_in, freq_out);
104
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900105 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900106}
107EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
108
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +0900109void snd_soc_component_seq_notifier(struct snd_soc_component *component,
110 enum snd_soc_dapm_type type, int subseq)
111{
112 if (component->driver->seq_notifier)
113 component->driver->seq_notifier(component, type, subseq);
114}
115
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900116int snd_soc_component_stream_event(struct snd_soc_component *component,
117 int event)
118{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900119 int ret = 0;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900120
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900121 if (component->driver->stream_event)
122 ret = component->driver->stream_event(component, event);
123
124 return soc_component_ret(component, ret);
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900125}
126
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900127int snd_soc_component_set_bias_level(struct snd_soc_component *component,
128 enum snd_soc_bias_level level)
129{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900130 int ret = 0;
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900131
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900132 if (component->driver->set_bias_level)
133 ret = component->driver->set_bias_level(component, level);
134
135 return soc_component_ret(component, ret);
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900136}
137
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900138static int soc_component_pin(struct snd_soc_component *component,
139 const char *pin,
140 int (*pin_func)(struct snd_soc_dapm_context *dapm,
141 const char *pin))
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900142{
143 struct snd_soc_dapm_context *dapm =
144 snd_soc_component_get_dapm(component);
145 char *full_name;
146 int ret;
147
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900148 if (!component->name_prefix) {
149 ret = pin_func(dapm, pin);
150 goto end;
151 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900152
153 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900154 if (!full_name) {
155 ret = -ENOMEM;
156 goto end;
157 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900158
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900159 ret = pin_func(dapm, full_name);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900160 kfree(full_name);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900161end:
162 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900163}
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900164
165int snd_soc_component_enable_pin(struct snd_soc_component *component,
166 const char *pin)
167{
168 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
169}
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900170EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
171
172int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
173 const char *pin)
174{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900175 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900176}
177EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
178
179int snd_soc_component_disable_pin(struct snd_soc_component *component,
180 const char *pin)
181{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900182 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900183}
184EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
185
186int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
187 const char *pin)
188{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900189 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900190}
191EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
192
193int snd_soc_component_nc_pin(struct snd_soc_component *component,
194 const char *pin)
195{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900196 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900197}
198EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
199
200int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
201 const char *pin)
202{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900203 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900204}
205EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
206
207int snd_soc_component_get_pin_status(struct snd_soc_component *component,
208 const char *pin)
209{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900210 return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
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{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900217 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900218}
219EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
220
221int snd_soc_component_force_enable_pin_unlocked(
222 struct snd_soc_component *component,
223 const char *pin)
224{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900225 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900226}
227EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
228
229/**
230 * snd_soc_component_set_jack - configure component jack.
231 * @component: COMPONENTs
232 * @jack: structure to use for the jack
233 * @data: can be used if codec driver need extra data for configuring jack
234 *
235 * Configures and enables jack detection function.
236 */
237int snd_soc_component_set_jack(struct snd_soc_component *component,
238 struct snd_soc_jack *jack, void *data)
239{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900240 int ret = -ENOTSUPP;
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900241
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900242 if (component->driver->set_jack)
243 ret = component->driver->set_jack(component, jack, data);
244
245 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900246}
247EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900248
249int snd_soc_component_module_get(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900250 struct snd_pcm_substream *substream,
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900251 int upon_open)
252{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900253 int ret = 0;
254
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900255 if (component->driver->module_get_upon_open == !!upon_open &&
256 !try_module_get(component->dev->driver->owner))
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900257 ret = -ENODEV;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900258
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900259 /* mark substream if succeeded */
260 if (ret == 0)
261 soc_component_mark_push(component, substream, module);
262
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900263 return soc_component_ret(component, ret);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900264}
265
266void snd_soc_component_module_put(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900267 struct snd_pcm_substream *substream,
268 int upon_open, int rollback)
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900269{
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900270 if (rollback && !soc_component_mark_match(component, substream, module))
271 return;
272
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900273 if (component->driver->module_get_upon_open == !!upon_open)
274 module_put(component->dev->driver->owner);
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900275
276 /* remove marked substream */
277 soc_component_mark_pop(component, substream, module);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900278}
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900279
280int snd_soc_component_open(struct snd_soc_component *component,
281 struct snd_pcm_substream *substream)
282{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900283 int ret = 0;
284
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900285 if (component->driver->open)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900286 ret = component->driver->open(component, substream);
287
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900288 /* mark substream if succeeded */
289 if (ret == 0)
290 soc_component_mark_push(component, substream, open);
291
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900292 return soc_component_ret(component, ret);
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900293}
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900294
295int snd_soc_component_close(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900296 struct snd_pcm_substream *substream,
297 int rollback)
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900298{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900299 int ret = 0;
300
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900301 if (rollback && !soc_component_mark_match(component, substream, open))
302 return 0;
303
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900304 if (component->driver->close)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900305 ret = component->driver->close(component, substream);
306
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900307 /* remove marked substream */
308 soc_component_mark_pop(component, substream, open);
309
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900310 return soc_component_ret(component, ret);
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900311}
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900312
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900313void snd_soc_component_suspend(struct snd_soc_component *component)
314{
315 if (component->driver->suspend)
316 component->driver->suspend(component);
317 component->suspended = 1;
318}
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900319
320void snd_soc_component_resume(struct snd_soc_component *component)
321{
322 if (component->driver->resume)
323 component->driver->resume(component);
324 component->suspended = 0;
325}
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900326
327int snd_soc_component_is_suspended(struct snd_soc_component *component)
328{
329 return component->suspended;
330}
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900331
332int snd_soc_component_probe(struct snd_soc_component *component)
333{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900334 int ret = 0;
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900335
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900336 if (component->driver->probe)
337 ret = component->driver->probe(component);
338
339 return soc_component_ret(component, ret);
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900340}
Kuninori Morimoto03b34dd2019-07-26 13:51:22 +0900341
342void snd_soc_component_remove(struct snd_soc_component *component)
343{
344 if (component->driver->remove)
345 component->driver->remove(component);
346}
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900347
348int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
349 struct device_node *ep)
350{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900351 int ret = -ENOTSUPP;
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900352
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900353 if (component->driver->of_xlate_dai_id)
354 ret = component->driver->of_xlate_dai_id(component, ep);
355
356 return soc_component_ret(component, ret);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900357}
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900358
359int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
360 struct of_phandle_args *args,
361 const char **dai_name)
362{
363 if (component->driver->of_xlate_dai_name)
Jerome Brunetcc4d8ce2020-07-23 16:20:20 +0200364 return component->driver->of_xlate_dai_name(component,
365 args, dai_name);
366 /*
367 * Don't use soc_component_ret here because we may not want to report
368 * the error just yet. If a device has more than one component, the
369 * first may not match and we don't want spam the log with this.
370 */
371 return -ENOTSUPP;
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900372}
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900373
Kuninori Morimotoc7d75b52020-06-04 17:06:22 +0900374void snd_soc_component_setup_regmap(struct snd_soc_component *component)
375{
376 int val_bytes = regmap_get_val_bytes(component->regmap);
377
378 /* Errors are legitimate for non-integer byte multiples */
379 if (val_bytes > 0)
380 component->val_bytes = val_bytes;
381}
382
383#ifdef CONFIG_REGMAP
384
385/**
386 * snd_soc_component_init_regmap() - Initialize regmap instance for the
387 * component
388 * @component: The component for which to initialize the regmap instance
389 * @regmap: The regmap instance that should be used by the component
390 *
391 * This function allows deferred assignment of the regmap instance that is
392 * associated with the component. Only use this if the regmap instance is not
393 * yet ready when the component is registered. The function must also be called
394 * before the first IO attempt of the component.
395 */
396void snd_soc_component_init_regmap(struct snd_soc_component *component,
397 struct regmap *regmap)
398{
399 component->regmap = regmap;
400 snd_soc_component_setup_regmap(component);
401}
402EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
403
404/**
405 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
406 * component
407 * @component: The component for which to de-initialize the regmap instance
408 *
409 * Calls regmap_exit() on the regmap instance associated to the component and
410 * removes the regmap instance from the component.
411 *
412 * This function should only be used if snd_soc_component_init_regmap() was used
413 * to initialize the regmap instance.
414 */
415void snd_soc_component_exit_regmap(struct snd_soc_component *component)
416{
417 regmap_exit(component->regmap);
418 component->regmap = NULL;
419}
420EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
421
422#endif
423
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900424int snd_soc_component_compr_open(struct snd_compr_stream *cstream,
425 struct snd_soc_component **last)
426{
427 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
428 struct snd_soc_component *component;
429 int i, ret;
430
431 for_each_rtd_components(rtd, i, component) {
432 if (component->driver->compress_ops &&
433 component->driver->compress_ops->open) {
434 ret = component->driver->compress_ops->open(component, cstream);
435 if (ret < 0) {
436 *last = component;
437 return soc_component_ret(component, ret);
438 }
439 }
440 }
441
442 *last = NULL;
443 return 0;
444}
445EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
446
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900447void snd_soc_component_compr_free(struct snd_compr_stream *cstream,
448 struct snd_soc_component *last)
449{
450 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
451 struct snd_soc_component *component;
452 int i;
453
454 for_each_rtd_components(rtd, i, component) {
455 if (component == last)
456 break;
457
458 if (component->driver->compress_ops &&
459 component->driver->compress_ops->free)
460 component->driver->compress_ops->free(component, cstream);
461 }
462}
463EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
464
Kuninori Morimoto08aee252020-11-13 13:15:33 +0900465int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
466{
467 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
468 struct snd_soc_component *component;
469 int i, ret;
470
471 for_each_rtd_components(rtd, i, component) {
472 if (component->driver->compress_ops &&
473 component->driver->compress_ops->trigger) {
474 ret = component->driver->compress_ops->trigger(
475 component, cstream, cmd);
476 if (ret < 0)
477 return soc_component_ret(component, ret);
478 }
479 }
480
481 return 0;
482}
483EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
484
Kuninori Morimotoff08cf82020-11-13 13:15:49 +0900485int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
486 struct snd_compr_params *params)
487{
488 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
489 struct snd_soc_component *component;
490 int i, ret;
491
492 for_each_rtd_components(rtd, i, component) {
493 if (component->driver->compress_ops &&
494 component->driver->compress_ops->set_params) {
495 ret = component->driver->compress_ops->set_params(
496 component, cstream, params);
497 if (ret < 0)
498 return soc_component_ret(component, ret);
499 }
500 }
501
502 return 0;
503}
504EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
505
Kuninori Morimoto77c221e2020-11-13 13:15:56 +0900506int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
507 struct snd_codec *params)
508{
509 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
510 struct snd_soc_component *component;
511 int i, ret;
512
513 for_each_rtd_components(rtd, i, component) {
514 if (component->driver->compress_ops &&
515 component->driver->compress_ops->get_params) {
516 ret = component->driver->compress_ops->get_params(
517 component, cstream, params);
518 return soc_component_ret(component, ret);
519 }
520 }
521
522 return 0;
523}
524EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
525
Kuninori Morimotod67fcb22020-11-13 13:16:03 +0900526int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
527 struct snd_compr_caps *caps)
528{
529 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
530 struct snd_soc_component *component;
531 int i, ret = 0;
532
533 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
534
535 for_each_rtd_components(rtd, i, component) {
536 if (component->driver->compress_ops &&
537 component->driver->compress_ops->get_caps) {
538 ret = component->driver->compress_ops->get_caps(
539 component, cstream, caps);
540 break;
541 }
542 }
543
544 mutex_unlock(&rtd->card->pcm_mutex);
545
546 return soc_component_ret(component, ret);
547}
548EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
549
Kuninori Morimoto0f6fe092020-11-13 13:16:11 +0900550int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
551 struct snd_compr_codec_caps *codec)
552{
553 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
554 struct snd_soc_component *component;
555 int i, ret = 0;
556
557 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
558
559 for_each_rtd_components(rtd, i, component) {
560 if (component->driver->compress_ops &&
561 component->driver->compress_ops->get_codec_caps) {
562 ret = component->driver->compress_ops->get_codec_caps(
563 component, cstream, codec);
564 break;
565 }
566 }
567
568 mutex_unlock(&rtd->card->pcm_mutex);
569
570 return soc_component_ret(component, ret);
571}
572EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
573
Kuninori Morimoto0506b882020-11-13 13:16:17 +0900574int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
575{
576 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
577 struct snd_soc_component *component;
578 int i, ret;
579
580 for_each_rtd_components(rtd, i, component) {
581 if (component->driver->compress_ops &&
582 component->driver->compress_ops->ack) {
583 ret = component->driver->compress_ops->ack(
584 component, cstream, bytes);
585 if (ret < 0)
586 return soc_component_ret(component, ret);
587 }
588 }
589
590 return 0;
591}
592EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
593
Kuninori Morimoto03ecea62020-11-13 13:16:24 +0900594int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
595 struct snd_compr_tstamp *tstamp)
596{
597 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
598 struct snd_soc_component *component;
599 int i, ret;
600
601 for_each_rtd_components(rtd, i, component) {
602 if (component->driver->compress_ops &&
603 component->driver->compress_ops->pointer) {
604 ret = component->driver->compress_ops->pointer(
605 component, cstream, tstamp);
606 return soc_component_ret(component, ret);
607 }
608 }
609
610 return 0;
611}
612EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
613
Kuninori Morimotob5852e62020-11-13 13:16:30 +0900614int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
615 char __user *buf, size_t count)
616{
617 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
618 struct snd_soc_component *component;
619 int i, ret = 0;
620
621 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
622
623 for_each_rtd_components(rtd, i, component) {
624 if (component->driver->compress_ops &&
625 component->driver->compress_ops->copy) {
626 ret = component->driver->compress_ops->copy(
627 component, cstream, buf, count);
628 break;
629 }
630 }
631
632 mutex_unlock(&rtd->card->pcm_mutex);
633
634 return soc_component_ret(component, ret);
635}
636EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
637
Kuninori Morimoto1b308fb2020-11-13 13:16:36 +0900638int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
639 struct snd_compr_metadata *metadata)
640{
641 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
642 struct snd_soc_component *component;
643 int i, ret;
644
645 for_each_rtd_components(rtd, i, component) {
646 if (component->driver->compress_ops &&
647 component->driver->compress_ops->set_metadata) {
648 ret = component->driver->compress_ops->set_metadata(
649 component, cstream, metadata);
650 if (ret < 0)
651 return soc_component_ret(component, ret);
652 }
653 }
654
655 return 0;
656}
657EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
658
Kuninori Morimotobab78c22020-11-13 13:16:41 +0900659int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
660 struct snd_compr_metadata *metadata)
661{
662 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
663 struct snd_soc_component *component;
664 int i, ret;
665
666 for_each_rtd_components(rtd, i, component) {
667 if (component->driver->compress_ops &&
668 component->driver->compress_ops->get_metadata) {
669 ret = component->driver->compress_ops->get_metadata(
670 component, cstream, metadata);
671 return soc_component_ret(component, ret);
672 }
673 }
674
675 return 0;
676}
677EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
678
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900679static unsigned int soc_component_read_no_lock(
680 struct snd_soc_component *component,
681 unsigned int reg)
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900682{
683 int ret;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900684 unsigned int val = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900685
686 if (component->regmap)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900687 ret = regmap_read(component->regmap, reg, &val);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900688 else if (component->driver->read) {
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900689 ret = 0;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900690 val = component->driver->read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900691 }
692 else
693 ret = -EIO;
694
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900695 if (ret < 0)
Takashi Iwaiefc913c2020-08-10 15:46:31 +0200696 return soc_component_ret(component, ret);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900697
698 return val;
699}
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900700
701/**
702 * snd_soc_component_read() - Read register value
703 * @component: Component to read from
704 * @reg: Register to read
705 *
706 * Return: read value
707 */
708unsigned int snd_soc_component_read(struct snd_soc_component *component,
709 unsigned int reg)
710{
711 unsigned int val;
712
713 mutex_lock(&component->io_mutex);
714 val = soc_component_read_no_lock(component, reg);
715 mutex_unlock(&component->io_mutex);
716
717 return val;
718}
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900719EXPORT_SYMBOL_GPL(snd_soc_component_read);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900720
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900721static int soc_component_write_no_lock(
722 struct snd_soc_component *component,
723 unsigned int reg, unsigned int val)
724{
725 int ret = -EIO;
726
727 if (component->regmap)
728 ret = regmap_write(component->regmap, reg, val);
729 else if (component->driver->write)
730 ret = component->driver->write(component, reg, val);
731
732 return soc_component_ret(component, ret);
733}
734
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900735/**
736 * snd_soc_component_write() - Write register value
737 * @component: Component to write to
738 * @reg: Register to write
739 * @val: Value to write to the register
740 *
741 * Return: 0 on success, a negative error code otherwise.
742 */
743int snd_soc_component_write(struct snd_soc_component *component,
744 unsigned int reg, unsigned int val)
745{
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900746 int ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900747
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900748 mutex_lock(&component->io_mutex);
749 ret = soc_component_write_no_lock(component, reg, val);
750 mutex_unlock(&component->io_mutex);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900751
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900752 return ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900753}
754EXPORT_SYMBOL_GPL(snd_soc_component_write);
755
756static int snd_soc_component_update_bits_legacy(
757 struct snd_soc_component *component, unsigned int reg,
758 unsigned int mask, unsigned int val, bool *change)
759{
760 unsigned int old, new;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900761 int ret = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900762
763 mutex_lock(&component->io_mutex);
764
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900765 old = soc_component_read_no_lock(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900766
767 new = (old & ~mask) | (val & mask);
768 *change = old != new;
769 if (*change)
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900770 ret = soc_component_write_no_lock(component, reg, new);
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900771
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900772 mutex_unlock(&component->io_mutex);
773
774 return soc_component_ret(component, ret);
775}
776
777/**
778 * snd_soc_component_update_bits() - Perform read/modify/write cycle
779 * @component: Component to update
780 * @reg: Register to update
781 * @mask: Mask that specifies which bits to update
782 * @val: New value for the bits specified by mask
783 *
784 * Return: 1 if the operation was successful and the value of the register
785 * changed, 0 if the operation was successful, but the value did not change.
786 * Returns a negative error code otherwise.
787 */
788int snd_soc_component_update_bits(struct snd_soc_component *component,
789 unsigned int reg, unsigned int mask, unsigned int val)
790{
791 bool change;
792 int ret;
793
794 if (component->regmap)
795 ret = regmap_update_bits_check(component->regmap, reg, mask,
796 val, &change);
797 else
798 ret = snd_soc_component_update_bits_legacy(component, reg,
799 mask, val, &change);
800
801 if (ret < 0)
802 return soc_component_ret(component, ret);
803 return change;
804}
805EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
806
807/**
808 * snd_soc_component_update_bits_async() - Perform asynchronous
809 * read/modify/write cycle
810 * @component: Component to update
811 * @reg: Register to update
812 * @mask: Mask that specifies which bits to update
813 * @val: New value for the bits specified by mask
814 *
815 * This function is similar to snd_soc_component_update_bits(), but the update
816 * operation is scheduled asynchronously. This means it may not be completed
817 * when the function returns. To make sure that all scheduled updates have been
818 * completed snd_soc_component_async_complete() must be called.
819 *
820 * Return: 1 if the operation was successful and the value of the register
821 * changed, 0 if the operation was successful, but the value did not change.
822 * Returns a negative error code otherwise.
823 */
824int snd_soc_component_update_bits_async(struct snd_soc_component *component,
825 unsigned int reg, unsigned int mask, unsigned int val)
826{
827 bool change;
828 int ret;
829
830 if (component->regmap)
831 ret = regmap_update_bits_check_async(component->regmap, reg,
832 mask, val, &change);
833 else
834 ret = snd_soc_component_update_bits_legacy(component, reg,
835 mask, val, &change);
836
837 if (ret < 0)
838 return soc_component_ret(component, ret);
839 return change;
840}
841EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
842
843/**
844 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
845 * @component: Component for which to wait
846 *
847 * This function blocks until all asynchronous I/O which has previously been
848 * scheduled using snd_soc_component_update_bits_async() has completed.
849 */
850void snd_soc_component_async_complete(struct snd_soc_component *component)
851{
852 if (component->regmap)
853 regmap_async_complete(component->regmap);
854}
855EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
856
857/**
858 * snd_soc_component_test_bits - Test register for change
859 * @component: component
860 * @reg: Register to test
861 * @mask: Mask that specifies which bits to test
862 * @value: Value to test against
863 *
864 * Tests a register with a new value and checks if the new value is
865 * different from the old value.
866 *
867 * Return: 1 for change, otherwise 0.
868 */
869int snd_soc_component_test_bits(struct snd_soc_component *component,
870 unsigned int reg, unsigned int mask, unsigned int value)
871{
872 unsigned int old, new;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900873
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900874 old = snd_soc_component_read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900875 new = (old & ~mask) | value;
876 return old != new;
877}
878EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
879
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900880int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
881{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900882 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900883 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900884 int i;
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900885
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900886 /* FIXME: use 1st pointer */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900887 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900888 if (component->driver->pointer)
889 return component->driver->pointer(component, substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900890
891 return 0;
892}
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900893
894int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
895 unsigned int cmd, void *arg)
896{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900897 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900898 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900899 int i;
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900900
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900901 /* FIXME: use 1st ioctl */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900902 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900903 if (component->driver->ioctl)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900904 return soc_component_ret(
905 component,
906 component->driver->ioctl(component,
907 substream, cmd, arg));
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900908
909 return snd_pcm_lib_ioctl(substream, cmd, arg);
910}
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900911
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100912int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
913{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900914 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100915 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900916 int i, ret;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100917
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900918 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof1861a72020-02-28 10:48:35 +0900919 if (component->driver->sync_stop) {
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100920 ret = component->driver->sync_stop(component,
921 substream);
922 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800923 return soc_component_ret(component, ret);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100924 }
925 }
926
927 return 0;
928}
929
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900930int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
931 int channel, unsigned long pos,
932 void __user *buf, unsigned long bytes)
933{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900934 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900935 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900936 int i;
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900937
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900938 /* FIXME. it returns 1st copy now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900939 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900940 if (component->driver->copy_user)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900941 return soc_component_ret(
942 component,
943 component->driver->copy_user(
944 component, substream, channel,
945 pos, buf, bytes));
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900946
947 return -EINVAL;
948}
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900949
950struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
951 unsigned long offset)
952{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900953 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900954 struct snd_soc_component *component;
955 struct page *page;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900956 int i;
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900957
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900958 /* FIXME. it returns 1st page now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900959 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900960 if (component->driver->page) {
961 page = component->driver->page(component,
962 substream, offset);
963 if (page)
964 return page;
965 }
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900966 }
967
968 return NULL;
969}
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900970
971int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
972 struct vm_area_struct *vma)
973{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900974 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900975 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900976 int i;
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900977
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900978 /* FIXME. it returns 1st mmap now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900979 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900980 if (component->driver->mmap)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800981 return soc_component_ret(
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900982 component,
983 component->driver->mmap(component,
984 substream, vma));
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900985
986 return -EINVAL;
987}
Kuninori Morimoto74842912019-07-26 13:52:08 +0900988
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +0900989int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto74842912019-07-26 13:52:08 +0900990{
Kuninori Morimoto74842912019-07-26 13:52:08 +0900991 struct snd_soc_component *component;
992 int ret;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900993 int i;
Kuninori Morimoto74842912019-07-26 13:52:08 +0900994
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900995 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +0900996 if (component->driver->pcm_construct) {
997 ret = component->driver->pcm_construct(component, rtd);
998 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800999 return soc_component_ret(component, ret);
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001000 }
Kuninori Morimoto74842912019-07-26 13:52:08 +09001001 }
1002
1003 return 0;
1004}
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001005
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001006void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001007{
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001008 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001009 int i;
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001010
Takashi Iwai8e3366c2020-01-07 08:09:56 +01001011 if (!rtd->pcm)
1012 return;
1013
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001014 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001015 if (component->driver->pcm_destruct)
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001016 component->driver->pcm_destruct(component, rtd->pcm);
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001017}
Kuninori Morimoto4f395142020-06-04 17:06:58 +09001018
1019int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1020{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001021 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto4f395142020-06-04 17:06:58 +09001022 struct snd_soc_component *component;
1023 int i, ret;
1024
1025 for_each_rtd_components(rtd, i, component) {
1026 if (component->driver->prepare) {
1027 ret = component->driver->prepare(component, substream);
1028 if (ret < 0)
1029 return soc_component_ret(component, ret);
1030 }
1031 }
1032
1033 return 0;
1034}
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001035
1036int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001037 struct snd_pcm_hw_params *params)
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001038{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001039 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001040 struct snd_soc_component *component;
1041 int i, ret;
1042
1043 for_each_rtd_components(rtd, i, component) {
1044 if (component->driver->hw_params) {
1045 ret = component->driver->hw_params(component,
1046 substream, params);
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001047 if (ret < 0)
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001048 return soc_component_ret(component, ret);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001049 }
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001050 /* mark substream if succeeded */
1051 soc_component_mark_push(component, substream, hw_params);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001052 }
1053
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001054 return 0;
1055}
Kuninori Morimoto04751112020-06-04 17:07:24 +09001056
1057void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001058 int rollback)
Kuninori Morimoto04751112020-06-04 17:07:24 +09001059{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001060 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto04751112020-06-04 17:07:24 +09001061 struct snd_soc_component *component;
1062 int i, ret;
1063
1064 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001065 if (rollback && !soc_component_mark_match(component, substream, hw_params))
1066 continue;
Kuninori Morimoto04751112020-06-04 17:07:24 +09001067
1068 if (component->driver->hw_free) {
1069 ret = component->driver->hw_free(component, substream);
1070 if (ret < 0)
1071 soc_component_ret(component, ret);
1072 }
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001073
1074 /* remove marked substream */
1075 soc_component_mark_pop(component, substream, hw_params);
Kuninori Morimoto04751112020-06-04 17:07:24 +09001076 }
1077}
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001078
1079int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
1080 int cmd)
1081{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001082 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001083 struct snd_soc_component *component;
1084 int i, ret;
1085
1086 for_each_rtd_components(rtd, i, component) {
1087 if (component->driver->trigger) {
1088 ret = component->driver->trigger(component, substream, cmd);
1089 if (ret < 0)
1090 return soc_component_ret(component, ret);
1091 }
1092 }
1093
1094 return 0;
1095}
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +09001096
1097int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1098 void *stream)
1099{
1100 struct snd_soc_component *component;
1101 int i, ret;
1102
1103 for_each_rtd_components(rtd, i, component) {
1104 ret = pm_runtime_get_sync(component->dev);
1105 if (ret < 0 && ret != -EACCES) {
1106 pm_runtime_put_noidle(component->dev);
1107 return soc_component_ret(component, ret);
1108 }
1109 /* mark stream if succeeded */
1110 soc_component_mark_push(component, stream, pm);
1111 }
1112
1113 return 0;
1114}
1115
1116void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1117 void *stream, int rollback)
1118{
1119 struct snd_soc_component *component;
1120 int i;
1121
1122 for_each_rtd_components(rtd, i, component) {
1123 if (rollback && !soc_component_mark_match(component, stream, pm))
1124 continue;
1125
1126 pm_runtime_mark_last_busy(component->dev);
1127 pm_runtime_put_autosuspend(component->dev);
1128
1129 /* remove marked stream */
1130 soc_component_mark_pop(component, stream, pm);
1131 }
1132}