blob: 8415e9bd2932185a9ee2c1abe38e031210d97fb9 [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 +0900151static int soc_component_pin(struct snd_soc_component *component,
152 const char *pin,
153 int (*pin_func)(struct snd_soc_dapm_context *dapm,
154 const char *pin))
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900155{
156 struct snd_soc_dapm_context *dapm =
157 snd_soc_component_get_dapm(component);
158 char *full_name;
159 int ret;
160
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900161 if (!component->name_prefix) {
162 ret = pin_func(dapm, pin);
163 goto end;
164 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900165
166 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900167 if (!full_name) {
168 ret = -ENOMEM;
169 goto end;
170 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900171
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900172 ret = pin_func(dapm, full_name);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900173 kfree(full_name);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900174end:
175 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900176}
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900177
178int snd_soc_component_enable_pin(struct snd_soc_component *component,
179 const char *pin)
180{
181 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
182}
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900183EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
184
185int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
186 const char *pin)
187{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900188 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900189}
190EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
191
192int snd_soc_component_disable_pin(struct snd_soc_component *component,
193 const char *pin)
194{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900195 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900196}
197EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
198
199int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
200 const char *pin)
201{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900202 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900203}
204EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
205
206int snd_soc_component_nc_pin(struct snd_soc_component *component,
207 const char *pin)
208{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900209 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900210}
211EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
212
213int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
214 const char *pin)
215{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900216 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900217}
218EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
219
220int snd_soc_component_get_pin_status(struct snd_soc_component *component,
221 const char *pin)
222{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900223 return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900224}
225EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
226
227int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
228 const char *pin)
229{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900230 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900231}
232EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
233
234int snd_soc_component_force_enable_pin_unlocked(
235 struct snd_soc_component *component,
236 const char *pin)
237{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900238 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900239}
240EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
241
242/**
243 * snd_soc_component_set_jack - configure component jack.
244 * @component: COMPONENTs
245 * @jack: structure to use for the jack
246 * @data: can be used if codec driver need extra data for configuring jack
247 *
248 * Configures and enables jack detection function.
249 */
250int snd_soc_component_set_jack(struct snd_soc_component *component,
251 struct snd_soc_jack *jack, void *data)
252{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900253 int ret = -ENOTSUPP;
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900254
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900255 if (component->driver->set_jack)
256 ret = component->driver->set_jack(component, jack, data);
257
258 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900259}
260EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900261
262int snd_soc_component_module_get(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900263 struct snd_pcm_substream *substream,
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900264 int upon_open)
265{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900266 int ret = 0;
267
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900268 if (component->driver->module_get_upon_open == !!upon_open &&
269 !try_module_get(component->dev->driver->owner))
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900270 ret = -ENODEV;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900271
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900272 /* mark substream if succeeded */
273 if (ret == 0)
274 soc_component_mark_push(component, substream, module);
275
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900276 return soc_component_ret(component, ret);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900277}
278
279void snd_soc_component_module_put(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900280 struct snd_pcm_substream *substream,
281 int upon_open, int rollback)
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900282{
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900283 if (rollback && !soc_component_mark_match(component, substream, module))
284 return;
285
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900286 if (component->driver->module_get_upon_open == !!upon_open)
287 module_put(component->dev->driver->owner);
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900288
289 /* remove marked substream */
290 soc_component_mark_pop(component, substream, module);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900291}
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900292
293int snd_soc_component_open(struct snd_soc_component *component,
294 struct snd_pcm_substream *substream)
295{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900296 int ret = 0;
297
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900298 if (component->driver->open)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900299 ret = component->driver->open(component, substream);
300
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900301 /* mark substream if succeeded */
302 if (ret == 0)
303 soc_component_mark_push(component, substream, open);
304
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900305 return soc_component_ret(component, ret);
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900306}
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900307
308int snd_soc_component_close(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900309 struct snd_pcm_substream *substream,
310 int rollback)
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900311{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900312 int ret = 0;
313
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900314 if (rollback && !soc_component_mark_match(component, substream, open))
315 return 0;
316
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900317 if (component->driver->close)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900318 ret = component->driver->close(component, substream);
319
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900320 /* remove marked substream */
321 soc_component_mark_pop(component, substream, open);
322
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900323 return soc_component_ret(component, ret);
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900324}
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900325
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900326void snd_soc_component_suspend(struct snd_soc_component *component)
327{
328 if (component->driver->suspend)
329 component->driver->suspend(component);
330 component->suspended = 1;
331}
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900332
333void snd_soc_component_resume(struct snd_soc_component *component)
334{
335 if (component->driver->resume)
336 component->driver->resume(component);
337 component->suspended = 0;
338}
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900339
340int snd_soc_component_is_suspended(struct snd_soc_component *component)
341{
342 return component->suspended;
343}
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900344
345int snd_soc_component_probe(struct snd_soc_component *component)
346{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900347 int ret = 0;
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900348
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900349 if (component->driver->probe)
350 ret = component->driver->probe(component);
351
352 return soc_component_ret(component, ret);
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900353}
Kuninori Morimoto03b34dd2019-07-26 13:51:22 +0900354
355void snd_soc_component_remove(struct snd_soc_component *component)
356{
357 if (component->driver->remove)
358 component->driver->remove(component);
359}
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900360
361int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
362 struct device_node *ep)
363{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900364 int ret = -ENOTSUPP;
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900365
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900366 if (component->driver->of_xlate_dai_id)
367 ret = component->driver->of_xlate_dai_id(component, ep);
368
369 return soc_component_ret(component, ret);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900370}
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900371
372int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
Krzysztof Kozlowski933f98b2021-02-21 16:30:24 +0100373 const struct of_phandle_args *args,
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900374 const char **dai_name)
375{
376 if (component->driver->of_xlate_dai_name)
Jerome Brunetcc4d8ce2020-07-23 16:20:20 +0200377 return component->driver->of_xlate_dai_name(component,
378 args, dai_name);
379 /*
380 * Don't use soc_component_ret here because we may not want to report
381 * the error just yet. If a device has more than one component, the
382 * first may not match and we don't want spam the log with this.
383 */
384 return -ENOTSUPP;
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900385}
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900386
Kuninori Morimotoc7d75b52020-06-04 17:06:22 +0900387void snd_soc_component_setup_regmap(struct snd_soc_component *component)
388{
389 int val_bytes = regmap_get_val_bytes(component->regmap);
390
391 /* Errors are legitimate for non-integer byte multiples */
392 if (val_bytes > 0)
393 component->val_bytes = val_bytes;
394}
395
396#ifdef CONFIG_REGMAP
397
398/**
399 * snd_soc_component_init_regmap() - Initialize regmap instance for the
400 * component
401 * @component: The component for which to initialize the regmap instance
402 * @regmap: The regmap instance that should be used by the component
403 *
404 * This function allows deferred assignment of the regmap instance that is
405 * associated with the component. Only use this if the regmap instance is not
406 * yet ready when the component is registered. The function must also be called
407 * before the first IO attempt of the component.
408 */
409void snd_soc_component_init_regmap(struct snd_soc_component *component,
410 struct regmap *regmap)
411{
412 component->regmap = regmap;
413 snd_soc_component_setup_regmap(component);
414}
415EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
416
417/**
418 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
419 * component
420 * @component: The component for which to de-initialize the regmap instance
421 *
422 * Calls regmap_exit() on the regmap instance associated to the component and
423 * removes the regmap instance from the component.
424 *
425 * This function should only be used if snd_soc_component_init_regmap() was used
426 * to initialize the regmap instance.
427 */
428void snd_soc_component_exit_regmap(struct snd_soc_component *component)
429{
430 regmap_exit(component->regmap);
431 component->regmap = NULL;
432}
433EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
434
435#endif
436
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900437int snd_soc_component_compr_open(struct snd_compr_stream *cstream)
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900438{
439 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
440 struct snd_soc_component *component;
441 int i, ret;
442
443 for_each_rtd_components(rtd, i, component) {
444 if (component->driver->compress_ops &&
445 component->driver->compress_ops->open) {
446 ret = component->driver->compress_ops->open(component, cstream);
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900447 if (ret < 0)
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900448 return soc_component_ret(component, ret);
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900449 }
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900450 soc_component_mark_push(component, cstream, compr_open);
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900451 }
452
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900453 return 0;
454}
455EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
456
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900457void snd_soc_component_compr_free(struct snd_compr_stream *cstream,
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900458 int rollback)
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900459{
460 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
461 struct snd_soc_component *component;
462 int i;
463
464 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900465 if (rollback && !soc_component_mark_match(component, cstream, compr_open))
466 continue;
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900467
468 if (component->driver->compress_ops &&
469 component->driver->compress_ops->free)
470 component->driver->compress_ops->free(component, cstream);
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900471
472 soc_component_mark_pop(component, cstream, compr_open);
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900473 }
474}
475EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
476
Kuninori Morimoto08aee252020-11-13 13:15:33 +0900477int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
478{
479 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
480 struct snd_soc_component *component;
481 int i, ret;
482
483 for_each_rtd_components(rtd, i, component) {
484 if (component->driver->compress_ops &&
485 component->driver->compress_ops->trigger) {
486 ret = component->driver->compress_ops->trigger(
487 component, cstream, cmd);
488 if (ret < 0)
489 return soc_component_ret(component, ret);
490 }
491 }
492
493 return 0;
494}
495EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
496
Kuninori Morimotoff08cf82020-11-13 13:15:49 +0900497int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
498 struct snd_compr_params *params)
499{
500 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
501 struct snd_soc_component *component;
502 int i, ret;
503
504 for_each_rtd_components(rtd, i, component) {
505 if (component->driver->compress_ops &&
506 component->driver->compress_ops->set_params) {
507 ret = component->driver->compress_ops->set_params(
508 component, cstream, params);
509 if (ret < 0)
510 return soc_component_ret(component, ret);
511 }
512 }
513
514 return 0;
515}
516EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
517
Kuninori Morimoto77c221e2020-11-13 13:15:56 +0900518int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
519 struct snd_codec *params)
520{
521 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
522 struct snd_soc_component *component;
523 int i, ret;
524
525 for_each_rtd_components(rtd, i, component) {
526 if (component->driver->compress_ops &&
527 component->driver->compress_ops->get_params) {
528 ret = component->driver->compress_ops->get_params(
529 component, cstream, params);
530 return soc_component_ret(component, ret);
531 }
532 }
533
534 return 0;
535}
536EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
537
Kuninori Morimotod67fcb22020-11-13 13:16:03 +0900538int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
539 struct snd_compr_caps *caps)
540{
541 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
542 struct snd_soc_component *component;
543 int i, ret = 0;
544
545 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
546
547 for_each_rtd_components(rtd, i, component) {
548 if (component->driver->compress_ops &&
549 component->driver->compress_ops->get_caps) {
550 ret = component->driver->compress_ops->get_caps(
551 component, cstream, caps);
552 break;
553 }
554 }
555
556 mutex_unlock(&rtd->card->pcm_mutex);
557
558 return soc_component_ret(component, ret);
559}
560EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
561
Kuninori Morimoto0f6fe092020-11-13 13:16:11 +0900562int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
563 struct snd_compr_codec_caps *codec)
564{
565 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
566 struct snd_soc_component *component;
567 int i, ret = 0;
568
569 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
570
571 for_each_rtd_components(rtd, i, component) {
572 if (component->driver->compress_ops &&
573 component->driver->compress_ops->get_codec_caps) {
574 ret = component->driver->compress_ops->get_codec_caps(
575 component, cstream, codec);
576 break;
577 }
578 }
579
580 mutex_unlock(&rtd->card->pcm_mutex);
581
582 return soc_component_ret(component, ret);
583}
584EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
585
Kuninori Morimoto0506b882020-11-13 13:16:17 +0900586int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
587{
588 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
589 struct snd_soc_component *component;
590 int i, ret;
591
592 for_each_rtd_components(rtd, i, component) {
593 if (component->driver->compress_ops &&
594 component->driver->compress_ops->ack) {
595 ret = component->driver->compress_ops->ack(
596 component, cstream, bytes);
597 if (ret < 0)
598 return soc_component_ret(component, ret);
599 }
600 }
601
602 return 0;
603}
604EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
605
Kuninori Morimoto03ecea62020-11-13 13:16:24 +0900606int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
607 struct snd_compr_tstamp *tstamp)
608{
609 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
610 struct snd_soc_component *component;
611 int i, ret;
612
613 for_each_rtd_components(rtd, i, component) {
614 if (component->driver->compress_ops &&
615 component->driver->compress_ops->pointer) {
616 ret = component->driver->compress_ops->pointer(
617 component, cstream, tstamp);
618 return soc_component_ret(component, ret);
619 }
620 }
621
622 return 0;
623}
624EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
625
Kuninori Morimotob5852e62020-11-13 13:16:30 +0900626int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
627 char __user *buf, size_t count)
628{
629 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
630 struct snd_soc_component *component;
631 int i, ret = 0;
632
633 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
634
635 for_each_rtd_components(rtd, i, component) {
636 if (component->driver->compress_ops &&
637 component->driver->compress_ops->copy) {
638 ret = component->driver->compress_ops->copy(
639 component, cstream, buf, count);
640 break;
641 }
642 }
643
644 mutex_unlock(&rtd->card->pcm_mutex);
645
646 return soc_component_ret(component, ret);
647}
648EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
649
Kuninori Morimoto1b308fb2020-11-13 13:16:36 +0900650int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
651 struct snd_compr_metadata *metadata)
652{
653 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
654 struct snd_soc_component *component;
655 int i, ret;
656
657 for_each_rtd_components(rtd, i, component) {
658 if (component->driver->compress_ops &&
659 component->driver->compress_ops->set_metadata) {
660 ret = component->driver->compress_ops->set_metadata(
661 component, cstream, metadata);
662 if (ret < 0)
663 return soc_component_ret(component, ret);
664 }
665 }
666
667 return 0;
668}
669EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
670
Kuninori Morimotobab78c22020-11-13 13:16:41 +0900671int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
672 struct snd_compr_metadata *metadata)
673{
674 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
675 struct snd_soc_component *component;
676 int i, ret;
677
678 for_each_rtd_components(rtd, i, component) {
679 if (component->driver->compress_ops &&
680 component->driver->compress_ops->get_metadata) {
681 ret = component->driver->compress_ops->get_metadata(
682 component, cstream, metadata);
683 return soc_component_ret(component, ret);
684 }
685 }
686
687 return 0;
688}
689EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
690
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900691static unsigned int soc_component_read_no_lock(
692 struct snd_soc_component *component,
693 unsigned int reg)
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900694{
695 int ret;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900696 unsigned int val = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900697
698 if (component->regmap)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900699 ret = regmap_read(component->regmap, reg, &val);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900700 else if (component->driver->read) {
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900701 ret = 0;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900702 val = component->driver->read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900703 }
704 else
705 ret = -EIO;
706
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900707 if (ret < 0)
Takashi Iwaiefc913c2020-08-10 15:46:31 +0200708 return soc_component_ret(component, ret);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900709
710 return val;
711}
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900712
713/**
714 * snd_soc_component_read() - Read register value
715 * @component: Component to read from
716 * @reg: Register to read
717 *
718 * Return: read value
719 */
720unsigned int snd_soc_component_read(struct snd_soc_component *component,
721 unsigned int reg)
722{
723 unsigned int val;
724
725 mutex_lock(&component->io_mutex);
726 val = soc_component_read_no_lock(component, reg);
727 mutex_unlock(&component->io_mutex);
728
729 return val;
730}
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900731EXPORT_SYMBOL_GPL(snd_soc_component_read);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900732
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900733static int soc_component_write_no_lock(
734 struct snd_soc_component *component,
735 unsigned int reg, unsigned int val)
736{
737 int ret = -EIO;
738
739 if (component->regmap)
740 ret = regmap_write(component->regmap, reg, val);
741 else if (component->driver->write)
742 ret = component->driver->write(component, reg, val);
743
744 return soc_component_ret(component, ret);
745}
746
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900747/**
748 * snd_soc_component_write() - Write register value
749 * @component: Component to write to
750 * @reg: Register to write
751 * @val: Value to write to the register
752 *
753 * Return: 0 on success, a negative error code otherwise.
754 */
755int snd_soc_component_write(struct snd_soc_component *component,
756 unsigned int reg, unsigned int val)
757{
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900758 int ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900759
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900760 mutex_lock(&component->io_mutex);
761 ret = soc_component_write_no_lock(component, reg, val);
762 mutex_unlock(&component->io_mutex);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900763
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900764 return ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900765}
766EXPORT_SYMBOL_GPL(snd_soc_component_write);
767
768static int snd_soc_component_update_bits_legacy(
769 struct snd_soc_component *component, unsigned int reg,
770 unsigned int mask, unsigned int val, bool *change)
771{
772 unsigned int old, new;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900773 int ret = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900774
775 mutex_lock(&component->io_mutex);
776
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900777 old = soc_component_read_no_lock(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900778
779 new = (old & ~mask) | (val & mask);
780 *change = old != new;
781 if (*change)
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900782 ret = soc_component_write_no_lock(component, reg, new);
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900783
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900784 mutex_unlock(&component->io_mutex);
785
786 return soc_component_ret(component, ret);
787}
788
789/**
790 * snd_soc_component_update_bits() - Perform read/modify/write cycle
791 * @component: Component to update
792 * @reg: Register to update
793 * @mask: Mask that specifies which bits to update
794 * @val: New value for the bits specified by mask
795 *
796 * Return: 1 if the operation was successful and the value of the register
797 * changed, 0 if the operation was successful, but the value did not change.
798 * Returns a negative error code otherwise.
799 */
800int snd_soc_component_update_bits(struct snd_soc_component *component,
801 unsigned int reg, unsigned int mask, unsigned int val)
802{
803 bool change;
804 int ret;
805
806 if (component->regmap)
807 ret = regmap_update_bits_check(component->regmap, reg, mask,
808 val, &change);
809 else
810 ret = snd_soc_component_update_bits_legacy(component, reg,
811 mask, val, &change);
812
813 if (ret < 0)
814 return soc_component_ret(component, ret);
815 return change;
816}
817EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
818
819/**
820 * snd_soc_component_update_bits_async() - Perform asynchronous
821 * read/modify/write cycle
822 * @component: Component to update
823 * @reg: Register to update
824 * @mask: Mask that specifies which bits to update
825 * @val: New value for the bits specified by mask
826 *
827 * This function is similar to snd_soc_component_update_bits(), but the update
828 * operation is scheduled asynchronously. This means it may not be completed
829 * when the function returns. To make sure that all scheduled updates have been
830 * completed snd_soc_component_async_complete() must be called.
831 *
832 * Return: 1 if the operation was successful and the value of the register
833 * changed, 0 if the operation was successful, but the value did not change.
834 * Returns a negative error code otherwise.
835 */
836int snd_soc_component_update_bits_async(struct snd_soc_component *component,
837 unsigned int reg, unsigned int mask, unsigned int val)
838{
839 bool change;
840 int ret;
841
842 if (component->regmap)
843 ret = regmap_update_bits_check_async(component->regmap, reg,
844 mask, val, &change);
845 else
846 ret = snd_soc_component_update_bits_legacy(component, reg,
847 mask, val, &change);
848
849 if (ret < 0)
850 return soc_component_ret(component, ret);
851 return change;
852}
853EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
854
855/**
Srinivas Kandagatla1da0b982021-01-26 17:17:48 +0000856 * snd_soc_component_read_field() - Read register field value
857 * @component: Component to read from
858 * @reg: Register to read
859 * @mask: mask of the register field
860 *
861 * Return: read value of register field.
862 */
863unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
864 unsigned int reg, unsigned int mask)
865{
866 unsigned int val;
867
868 val = snd_soc_component_read(component, reg);
869
870 val = (val & mask) >> soc_component_field_shift(component, mask);
871
872 return val;
873}
874EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
875
876/**
877 * snd_soc_component_write_field() - write to register field
878 * @component: Component to write to
879 * @reg: Register to write
880 * @mask: mask of the register field to update
881 * @val: value of the field to write
882 *
883 * Return: 1 for change, otherwise 0.
884 */
885int snd_soc_component_write_field(struct snd_soc_component *component,
886 unsigned int reg, unsigned int mask,
887 unsigned int val)
888{
889
890 val = (val << soc_component_field_shift(component, mask)) & mask;
891
892 return snd_soc_component_update_bits(component, reg, mask, val);
893}
894EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
895
896/**
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900897 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
898 * @component: Component for which to wait
899 *
900 * This function blocks until all asynchronous I/O which has previously been
901 * scheduled using snd_soc_component_update_bits_async() has completed.
902 */
903void snd_soc_component_async_complete(struct snd_soc_component *component)
904{
905 if (component->regmap)
906 regmap_async_complete(component->regmap);
907}
908EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
909
910/**
911 * snd_soc_component_test_bits - Test register for change
912 * @component: component
913 * @reg: Register to test
914 * @mask: Mask that specifies which bits to test
915 * @value: Value to test against
916 *
917 * Tests a register with a new value and checks if the new value is
918 * different from the old value.
919 *
920 * Return: 1 for change, otherwise 0.
921 */
922int snd_soc_component_test_bits(struct snd_soc_component *component,
923 unsigned int reg, unsigned int mask, unsigned int value)
924{
925 unsigned int old, new;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900926
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900927 old = snd_soc_component_read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900928 new = (old & ~mask) | value;
929 return old != new;
930}
931EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
932
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900933int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
934{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900935 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900936 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900937 int i;
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900938
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900939 /* FIXME: use 1st pointer */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900940 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900941 if (component->driver->pointer)
942 return component->driver->pointer(component, substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900943
944 return 0;
945}
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900946
947int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
948 unsigned int cmd, void *arg)
949{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900950 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900951 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900952 int i;
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900953
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900954 /* FIXME: use 1st ioctl */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900955 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900956 if (component->driver->ioctl)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900957 return soc_component_ret(
958 component,
959 component->driver->ioctl(component,
960 substream, cmd, arg));
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900961
962 return snd_pcm_lib_ioctl(substream, cmd, arg);
963}
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900964
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100965int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
966{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900967 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100968 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900969 int i, ret;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100970
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900971 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof1861a72020-02-28 10:48:35 +0900972 if (component->driver->sync_stop) {
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100973 ret = component->driver->sync_stop(component,
974 substream);
975 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800976 return soc_component_ret(component, ret);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100977 }
978 }
979
980 return 0;
981}
982
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900983int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
984 int channel, unsigned long pos,
985 void __user *buf, unsigned long bytes)
986{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900987 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900988 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900989 int i;
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900990
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900991 /* FIXME. it returns 1st copy now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900992 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900993 if (component->driver->copy_user)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900994 return soc_component_ret(
995 component,
996 component->driver->copy_user(
997 component, substream, channel,
998 pos, buf, bytes));
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900999
1000 return -EINVAL;
1001}
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001002
1003struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
1004 unsigned long offset)
1005{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001006 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001007 struct snd_soc_component *component;
1008 struct page *page;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001009 int i;
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001010
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001011 /* FIXME. it returns 1st page now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001012 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +09001013 if (component->driver->page) {
1014 page = component->driver->page(component,
1015 substream, offset);
1016 if (page)
1017 return page;
1018 }
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001019 }
1020
1021 return NULL;
1022}
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001023
1024int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
1025 struct vm_area_struct *vma)
1026{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001027 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001028 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001029 int i;
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001030
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001031 /* FIXME. it returns 1st mmap now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001032 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +09001033 if (component->driver->mmap)
Shengjiu Wangbe75db52020-07-16 13:07:08 +08001034 return soc_component_ret(
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +09001035 component,
1036 component->driver->mmap(component,
1037 substream, vma));
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001038
1039 return -EINVAL;
1040}
Kuninori Morimoto74842912019-07-26 13:52:08 +09001041
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001042int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto74842912019-07-26 13:52:08 +09001043{
Kuninori Morimoto74842912019-07-26 13:52:08 +09001044 struct snd_soc_component *component;
1045 int ret;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001046 int i;
Kuninori Morimoto74842912019-07-26 13:52:08 +09001047
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001048 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001049 if (component->driver->pcm_construct) {
1050 ret = component->driver->pcm_construct(component, rtd);
1051 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +08001052 return soc_component_ret(component, ret);
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001053 }
Kuninori Morimoto74842912019-07-26 13:52:08 +09001054 }
1055
1056 return 0;
1057}
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001058
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001059void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001060{
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001061 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001062 int i;
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001063
Takashi Iwai8e3366c2020-01-07 08:09:56 +01001064 if (!rtd->pcm)
1065 return;
1066
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001067 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001068 if (component->driver->pcm_destruct)
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001069 component->driver->pcm_destruct(component, rtd->pcm);
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001070}
Kuninori Morimoto4f395142020-06-04 17:06:58 +09001071
1072int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1073{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001074 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto4f395142020-06-04 17:06:58 +09001075 struct snd_soc_component *component;
1076 int i, ret;
1077
1078 for_each_rtd_components(rtd, i, component) {
1079 if (component->driver->prepare) {
1080 ret = component->driver->prepare(component, substream);
1081 if (ret < 0)
1082 return soc_component_ret(component, ret);
1083 }
1084 }
1085
1086 return 0;
1087}
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001088
1089int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001090 struct snd_pcm_hw_params *params)
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001091{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001092 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001093 struct snd_soc_component *component;
1094 int i, ret;
1095
1096 for_each_rtd_components(rtd, i, component) {
1097 if (component->driver->hw_params) {
1098 ret = component->driver->hw_params(component,
1099 substream, params);
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001100 if (ret < 0)
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001101 return soc_component_ret(component, ret);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001102 }
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001103 /* mark substream if succeeded */
1104 soc_component_mark_push(component, substream, hw_params);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001105 }
1106
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001107 return 0;
1108}
Kuninori Morimoto04751112020-06-04 17:07:24 +09001109
1110void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001111 int rollback)
Kuninori Morimoto04751112020-06-04 17:07:24 +09001112{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001113 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto04751112020-06-04 17:07:24 +09001114 struct snd_soc_component *component;
1115 int i, ret;
1116
1117 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001118 if (rollback && !soc_component_mark_match(component, substream, hw_params))
1119 continue;
Kuninori Morimoto04751112020-06-04 17:07:24 +09001120
1121 if (component->driver->hw_free) {
1122 ret = component->driver->hw_free(component, substream);
1123 if (ret < 0)
1124 soc_component_ret(component, ret);
1125 }
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001126
1127 /* remove marked substream */
1128 soc_component_mark_pop(component, substream, hw_params);
Kuninori Morimoto04751112020-06-04 17:07:24 +09001129 }
1130}
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001131
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001132static int soc_component_trigger(struct snd_soc_component *component,
1133 struct snd_pcm_substream *substream,
1134 int cmd)
1135{
1136 int ret = 0;
1137
1138 if (component->driver->trigger)
1139 ret = component->driver->trigger(component, substream, cmd);
1140
1141 return soc_component_ret(component, ret);
1142}
1143
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001144int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001145 int cmd, int rollback)
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001146{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001147 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001148 struct snd_soc_component *component;
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001149 int i, r, ret = 0;
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001150
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001151 switch (cmd) {
1152 case SNDRV_PCM_TRIGGER_START:
1153 case SNDRV_PCM_TRIGGER_RESUME:
1154 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1155 for_each_rtd_components(rtd, i, component) {
1156 ret = soc_component_trigger(component, substream, cmd);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001157 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001158 break;
1159 soc_component_mark_push(component, substream, trigger);
1160 }
1161 break;
1162 case SNDRV_PCM_TRIGGER_STOP:
1163 case SNDRV_PCM_TRIGGER_SUSPEND:
1164 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1165 for_each_rtd_components(rtd, i, component) {
1166 if (rollback && !soc_component_mark_match(component, substream, trigger))
1167 continue;
1168
1169 r = soc_component_trigger(component, substream, cmd);
1170 if (r < 0)
1171 ret = r; /* use last ret */
1172 soc_component_mark_pop(component, substream, trigger);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001173 }
1174 }
1175
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001176 return ret;
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001177}
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +09001178
1179int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1180 void *stream)
1181{
1182 struct snd_soc_component *component;
1183 int i, ret;
1184
1185 for_each_rtd_components(rtd, i, component) {
1186 ret = pm_runtime_get_sync(component->dev);
1187 if (ret < 0 && ret != -EACCES) {
1188 pm_runtime_put_noidle(component->dev);
1189 return soc_component_ret(component, ret);
1190 }
1191 /* mark stream if succeeded */
1192 soc_component_mark_push(component, stream, pm);
1193 }
1194
1195 return 0;
1196}
1197
1198void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1199 void *stream, int rollback)
1200{
1201 struct snd_soc_component *component;
1202 int i;
1203
1204 for_each_rtd_components(rtd, i, component) {
1205 if (rollback && !soc_component_mark_match(component, stream, pm))
1206 continue;
1207
1208 pm_runtime_mark_last_busy(component->dev);
1209 pm_runtime_put_autosuspend(component->dev);
1210
1211 /* remove marked stream */
1212 soc_component_mark_pop(component, stream, pm);
1213 }
1214}