blob: 361a79d655e37c2cb2ab297ce0356a4277257663 [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
Srinivas Kandagatla1da0b982021-01-26 17:17:48 +000037static inline int soc_component_field_shift(struct snd_soc_component *component,
38 unsigned int mask)
39{
40 if (!mask) {
41 dev_err(component->dev, "ASoC: error field mask is zero for %s\n",
42 component->name);
43 return 0;
44 }
45
46 return (__builtin_ffs(mask) - 1);
47}
48
Kuninori Morimoto51aff912020-09-28 09:01:04 +090049/*
50 * We might want to check substream by using list.
51 * In such case, we can update these macros.
52 */
53#define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
54#define soc_component_mark_pop(component, substream, tgt) ((component)->mark_##tgt = NULL)
55#define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
56
Kuninori Morimoto257c4da2020-06-04 17:07:54 +090057void snd_soc_component_set_aux(struct snd_soc_component *component,
58 struct snd_soc_aux_dev *aux)
59{
60 component->init = (aux) ? aux->init : NULL;
61}
62
63int snd_soc_component_init(struct snd_soc_component *component)
64{
65 int ret = 0;
66
67 if (component->init)
68 ret = component->init(component);
69
70 return soc_component_ret(component, ret);
71}
72
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090073/**
74 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
75 * @component: COMPONENT
76 * @clk_id: DAI specific clock ID
77 * @source: Source for the clock
78 * @freq: new clock frequency in Hz
79 * @dir: new clock direction - input/output.
80 *
81 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
82 */
83int snd_soc_component_set_sysclk(struct snd_soc_component *component,
84 int clk_id, int source, unsigned int freq,
85 int dir)
86{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090087 int ret = -ENOTSUPP;
88
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090089 if (component->driver->set_sysclk)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090090 ret = component->driver->set_sysclk(component, clk_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090091 freq, dir);
92
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090093 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090094}
95EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
96
97/*
98 * snd_soc_component_set_pll - configure component PLL.
99 * @component: COMPONENT
100 * @pll_id: DAI specific PLL ID
101 * @source: DAI specific source for the PLL
102 * @freq_in: PLL input clock frequency in Hz
103 * @freq_out: requested PLL output clock frequency in Hz
104 *
105 * Configures and enables PLL to generate output clock based on input clock.
106 */
107int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
108 int source, unsigned int freq_in,
109 unsigned int freq_out)
110{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900111 int ret = -EINVAL;
112
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900113 if (component->driver->set_pll)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900114 ret = component->driver->set_pll(component, pll_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900115 freq_in, freq_out);
116
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900117 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900118}
119EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
120
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +0900121void snd_soc_component_seq_notifier(struct snd_soc_component *component,
122 enum snd_soc_dapm_type type, int subseq)
123{
124 if (component->driver->seq_notifier)
125 component->driver->seq_notifier(component, type, subseq);
126}
127
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900128int snd_soc_component_stream_event(struct snd_soc_component *component,
129 int event)
130{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900131 int ret = 0;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900132
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900133 if (component->driver->stream_event)
134 ret = component->driver->stream_event(component, event);
135
136 return soc_component_ret(component, ret);
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900137}
138
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900139int snd_soc_component_set_bias_level(struct snd_soc_component *component,
140 enum snd_soc_bias_level level)
141{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900142 int ret = 0;
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900143
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900144 if (component->driver->set_bias_level)
145 ret = component->driver->set_bias_level(component, level);
146
147 return soc_component_ret(component, ret);
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900148}
149
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900150static int soc_component_pin(struct snd_soc_component *component,
151 const char *pin,
152 int (*pin_func)(struct snd_soc_dapm_context *dapm,
153 const char *pin))
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900154{
155 struct snd_soc_dapm_context *dapm =
156 snd_soc_component_get_dapm(component);
157 char *full_name;
158 int ret;
159
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900160 if (!component->name_prefix) {
161 ret = pin_func(dapm, pin);
162 goto end;
163 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900164
165 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900166 if (!full_name) {
167 ret = -ENOMEM;
168 goto end;
169 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900170
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900171 ret = pin_func(dapm, full_name);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900172 kfree(full_name);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900173end:
174 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900175}
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900176
177int snd_soc_component_enable_pin(struct snd_soc_component *component,
178 const char *pin)
179{
180 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
181}
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900182EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
183
184int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
185 const char *pin)
186{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900187 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900188}
189EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
190
191int snd_soc_component_disable_pin(struct snd_soc_component *component,
192 const char *pin)
193{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900194 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900195}
196EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
197
198int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
199 const char *pin)
200{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900201 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900202}
203EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
204
205int snd_soc_component_nc_pin(struct snd_soc_component *component,
206 const char *pin)
207{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900208 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900209}
210EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
211
212int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
213 const char *pin)
214{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900215 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900216}
217EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
218
219int snd_soc_component_get_pin_status(struct snd_soc_component *component,
220 const char *pin)
221{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900222 return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900223}
224EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
225
226int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
227 const char *pin)
228{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900229 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900230}
231EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
232
233int snd_soc_component_force_enable_pin_unlocked(
234 struct snd_soc_component *component,
235 const char *pin)
236{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900237 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900238}
239EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
240
241/**
242 * snd_soc_component_set_jack - configure component jack.
243 * @component: COMPONENTs
244 * @jack: structure to use for the jack
245 * @data: can be used if codec driver need extra data for configuring jack
246 *
247 * Configures and enables jack detection function.
248 */
249int snd_soc_component_set_jack(struct snd_soc_component *component,
250 struct snd_soc_jack *jack, void *data)
251{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900252 int ret = -ENOTSUPP;
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900253
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900254 if (component->driver->set_jack)
255 ret = component->driver->set_jack(component, jack, data);
256
257 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900258}
259EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900260
261int snd_soc_component_module_get(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900262 struct snd_pcm_substream *substream,
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900263 int upon_open)
264{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900265 int ret = 0;
266
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900267 if (component->driver->module_get_upon_open == !!upon_open &&
268 !try_module_get(component->dev->driver->owner))
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900269 ret = -ENODEV;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900270
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900271 /* mark substream if succeeded */
272 if (ret == 0)
273 soc_component_mark_push(component, substream, module);
274
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900275 return soc_component_ret(component, ret);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900276}
277
278void snd_soc_component_module_put(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900279 struct snd_pcm_substream *substream,
280 int upon_open, int rollback)
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900281{
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900282 if (rollback && !soc_component_mark_match(component, substream, module))
283 return;
284
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900285 if (component->driver->module_get_upon_open == !!upon_open)
286 module_put(component->dev->driver->owner);
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900287
288 /* remove marked substream */
289 soc_component_mark_pop(component, substream, module);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900290}
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900291
292int snd_soc_component_open(struct snd_soc_component *component,
293 struct snd_pcm_substream *substream)
294{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900295 int ret = 0;
296
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900297 if (component->driver->open)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900298 ret = component->driver->open(component, substream);
299
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900300 /* mark substream if succeeded */
301 if (ret == 0)
302 soc_component_mark_push(component, substream, open);
303
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900304 return soc_component_ret(component, ret);
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900305}
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900306
307int snd_soc_component_close(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900308 struct snd_pcm_substream *substream,
309 int rollback)
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900310{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900311 int ret = 0;
312
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900313 if (rollback && !soc_component_mark_match(component, substream, open))
314 return 0;
315
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900316 if (component->driver->close)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900317 ret = component->driver->close(component, substream);
318
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900319 /* remove marked substream */
320 soc_component_mark_pop(component, substream, open);
321
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900322 return soc_component_ret(component, ret);
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900323}
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900324
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900325void snd_soc_component_suspend(struct snd_soc_component *component)
326{
327 if (component->driver->suspend)
328 component->driver->suspend(component);
329 component->suspended = 1;
330}
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900331
332void snd_soc_component_resume(struct snd_soc_component *component)
333{
334 if (component->driver->resume)
335 component->driver->resume(component);
336 component->suspended = 0;
337}
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900338
339int snd_soc_component_is_suspended(struct snd_soc_component *component)
340{
341 return component->suspended;
342}
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900343
344int snd_soc_component_probe(struct snd_soc_component *component)
345{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900346 int ret = 0;
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900347
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900348 if (component->driver->probe)
349 ret = component->driver->probe(component);
350
351 return soc_component_ret(component, ret);
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900352}
Kuninori Morimoto03b34dd2019-07-26 13:51:22 +0900353
354void snd_soc_component_remove(struct snd_soc_component *component)
355{
356 if (component->driver->remove)
357 component->driver->remove(component);
358}
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900359
360int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
361 struct device_node *ep)
362{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900363 int ret = -ENOTSUPP;
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900364
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900365 if (component->driver->of_xlate_dai_id)
366 ret = component->driver->of_xlate_dai_id(component, ep);
367
368 return soc_component_ret(component, ret);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900369}
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900370
371int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
372 struct of_phandle_args *args,
373 const char **dai_name)
374{
375 if (component->driver->of_xlate_dai_name)
Jerome Brunetcc4d8ce2020-07-23 16:20:20 +0200376 return component->driver->of_xlate_dai_name(component,
377 args, dai_name);
378 /*
379 * Don't use soc_component_ret here because we may not want to report
380 * the error just yet. If a device has more than one component, the
381 * first may not match and we don't want spam the log with this.
382 */
383 return -ENOTSUPP;
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900384}
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900385
Kuninori Morimotoc7d75b52020-06-04 17:06:22 +0900386void snd_soc_component_setup_regmap(struct snd_soc_component *component)
387{
388 int val_bytes = regmap_get_val_bytes(component->regmap);
389
390 /* Errors are legitimate for non-integer byte multiples */
391 if (val_bytes > 0)
392 component->val_bytes = val_bytes;
393}
394
395#ifdef CONFIG_REGMAP
396
397/**
398 * snd_soc_component_init_regmap() - Initialize regmap instance for the
399 * component
400 * @component: The component for which to initialize the regmap instance
401 * @regmap: The regmap instance that should be used by the component
402 *
403 * This function allows deferred assignment of the regmap instance that is
404 * associated with the component. Only use this if the regmap instance is not
405 * yet ready when the component is registered. The function must also be called
406 * before the first IO attempt of the component.
407 */
408void snd_soc_component_init_regmap(struct snd_soc_component *component,
409 struct regmap *regmap)
410{
411 component->regmap = regmap;
412 snd_soc_component_setup_regmap(component);
413}
414EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
415
416/**
417 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
418 * component
419 * @component: The component for which to de-initialize the regmap instance
420 *
421 * Calls regmap_exit() on the regmap instance associated to the component and
422 * removes the regmap instance from the component.
423 *
424 * This function should only be used if snd_soc_component_init_regmap() was used
425 * to initialize the regmap instance.
426 */
427void snd_soc_component_exit_regmap(struct snd_soc_component *component)
428{
429 regmap_exit(component->regmap);
430 component->regmap = NULL;
431}
432EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
433
434#endif
435
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900436int snd_soc_component_compr_open(struct snd_compr_stream *cstream)
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900437{
438 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
439 struct snd_soc_component *component;
440 int i, ret;
441
442 for_each_rtd_components(rtd, i, component) {
443 if (component->driver->compress_ops &&
444 component->driver->compress_ops->open) {
445 ret = component->driver->compress_ops->open(component, cstream);
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900446 if (ret < 0)
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900447 return soc_component_ret(component, ret);
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900448 }
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900449 soc_component_mark_push(component, cstream, compr_open);
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900450 }
451
Kuninori Morimotoa4e427c2020-11-13 13:15:20 +0900452 return 0;
453}
454EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
455
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900456void snd_soc_component_compr_free(struct snd_compr_stream *cstream,
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900457 int rollback)
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900458{
459 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
460 struct snd_soc_component *component;
461 int i;
462
463 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900464 if (rollback && !soc_component_mark_match(component, cstream, compr_open))
465 continue;
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900466
467 if (component->driver->compress_ops &&
468 component->driver->compress_ops->free)
469 component->driver->compress_ops->free(component, cstream);
Kuninori Morimotof94ba9a2020-11-19 08:50:09 +0900470
471 soc_component_mark_pop(component, cstream, compr_open);
Kuninori Morimotodbde5e212020-11-13 13:15:26 +0900472 }
473}
474EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
475
Kuninori Morimoto08aee252020-11-13 13:15:33 +0900476int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
477{
478 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
479 struct snd_soc_component *component;
480 int i, ret;
481
482 for_each_rtd_components(rtd, i, component) {
483 if (component->driver->compress_ops &&
484 component->driver->compress_ops->trigger) {
485 ret = component->driver->compress_ops->trigger(
486 component, cstream, cmd);
487 if (ret < 0)
488 return soc_component_ret(component, ret);
489 }
490 }
491
492 return 0;
493}
494EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
495
Kuninori Morimotoff08cf82020-11-13 13:15:49 +0900496int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
497 struct snd_compr_params *params)
498{
499 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
500 struct snd_soc_component *component;
501 int i, ret;
502
503 for_each_rtd_components(rtd, i, component) {
504 if (component->driver->compress_ops &&
505 component->driver->compress_ops->set_params) {
506 ret = component->driver->compress_ops->set_params(
507 component, cstream, params);
508 if (ret < 0)
509 return soc_component_ret(component, ret);
510 }
511 }
512
513 return 0;
514}
515EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
516
Kuninori Morimoto77c221e2020-11-13 13:15:56 +0900517int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
518 struct snd_codec *params)
519{
520 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
521 struct snd_soc_component *component;
522 int i, ret;
523
524 for_each_rtd_components(rtd, i, component) {
525 if (component->driver->compress_ops &&
526 component->driver->compress_ops->get_params) {
527 ret = component->driver->compress_ops->get_params(
528 component, cstream, params);
529 return soc_component_ret(component, ret);
530 }
531 }
532
533 return 0;
534}
535EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
536
Kuninori Morimotod67fcb22020-11-13 13:16:03 +0900537int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
538 struct snd_compr_caps *caps)
539{
540 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
541 struct snd_soc_component *component;
542 int i, ret = 0;
543
544 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
545
546 for_each_rtd_components(rtd, i, component) {
547 if (component->driver->compress_ops &&
548 component->driver->compress_ops->get_caps) {
549 ret = component->driver->compress_ops->get_caps(
550 component, cstream, caps);
551 break;
552 }
553 }
554
555 mutex_unlock(&rtd->card->pcm_mutex);
556
557 return soc_component_ret(component, ret);
558}
559EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
560
Kuninori Morimoto0f6fe092020-11-13 13:16:11 +0900561int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
562 struct snd_compr_codec_caps *codec)
563{
564 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
565 struct snd_soc_component *component;
566 int i, ret = 0;
567
568 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
569
570 for_each_rtd_components(rtd, i, component) {
571 if (component->driver->compress_ops &&
572 component->driver->compress_ops->get_codec_caps) {
573 ret = component->driver->compress_ops->get_codec_caps(
574 component, cstream, codec);
575 break;
576 }
577 }
578
579 mutex_unlock(&rtd->card->pcm_mutex);
580
581 return soc_component_ret(component, ret);
582}
583EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
584
Kuninori Morimoto0506b882020-11-13 13:16:17 +0900585int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
586{
587 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
588 struct snd_soc_component *component;
589 int i, ret;
590
591 for_each_rtd_components(rtd, i, component) {
592 if (component->driver->compress_ops &&
593 component->driver->compress_ops->ack) {
594 ret = component->driver->compress_ops->ack(
595 component, cstream, bytes);
596 if (ret < 0)
597 return soc_component_ret(component, ret);
598 }
599 }
600
601 return 0;
602}
603EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
604
Kuninori Morimoto03ecea62020-11-13 13:16:24 +0900605int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
606 struct snd_compr_tstamp *tstamp)
607{
608 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
609 struct snd_soc_component *component;
610 int i, ret;
611
612 for_each_rtd_components(rtd, i, component) {
613 if (component->driver->compress_ops &&
614 component->driver->compress_ops->pointer) {
615 ret = component->driver->compress_ops->pointer(
616 component, cstream, tstamp);
617 return soc_component_ret(component, ret);
618 }
619 }
620
621 return 0;
622}
623EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
624
Kuninori Morimotob5852e62020-11-13 13:16:30 +0900625int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
626 char __user *buf, size_t count)
627{
628 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
629 struct snd_soc_component *component;
630 int i, ret = 0;
631
632 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
633
634 for_each_rtd_components(rtd, i, component) {
635 if (component->driver->compress_ops &&
636 component->driver->compress_ops->copy) {
637 ret = component->driver->compress_ops->copy(
638 component, cstream, buf, count);
639 break;
640 }
641 }
642
643 mutex_unlock(&rtd->card->pcm_mutex);
644
645 return soc_component_ret(component, ret);
646}
647EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
648
Kuninori Morimoto1b308fb2020-11-13 13:16:36 +0900649int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
650 struct snd_compr_metadata *metadata)
651{
652 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
653 struct snd_soc_component *component;
654 int i, ret;
655
656 for_each_rtd_components(rtd, i, component) {
657 if (component->driver->compress_ops &&
658 component->driver->compress_ops->set_metadata) {
659 ret = component->driver->compress_ops->set_metadata(
660 component, cstream, metadata);
661 if (ret < 0)
662 return soc_component_ret(component, ret);
663 }
664 }
665
666 return 0;
667}
668EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
669
Kuninori Morimotobab78c22020-11-13 13:16:41 +0900670int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
671 struct snd_compr_metadata *metadata)
672{
673 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
674 struct snd_soc_component *component;
675 int i, ret;
676
677 for_each_rtd_components(rtd, i, component) {
678 if (component->driver->compress_ops &&
679 component->driver->compress_ops->get_metadata) {
680 ret = component->driver->compress_ops->get_metadata(
681 component, cstream, metadata);
682 return soc_component_ret(component, ret);
683 }
684 }
685
686 return 0;
687}
688EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
689
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900690static unsigned int soc_component_read_no_lock(
691 struct snd_soc_component *component,
692 unsigned int reg)
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900693{
694 int ret;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900695 unsigned int val = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900696
697 if (component->regmap)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900698 ret = regmap_read(component->regmap, reg, &val);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900699 else if (component->driver->read) {
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900700 ret = 0;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900701 val = component->driver->read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900702 }
703 else
704 ret = -EIO;
705
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900706 if (ret < 0)
Takashi Iwaiefc913c2020-08-10 15:46:31 +0200707 return soc_component_ret(component, ret);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900708
709 return val;
710}
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900711
712/**
713 * snd_soc_component_read() - Read register value
714 * @component: Component to read from
715 * @reg: Register to read
716 *
717 * Return: read value
718 */
719unsigned int snd_soc_component_read(struct snd_soc_component *component,
720 unsigned int reg)
721{
722 unsigned int val;
723
724 mutex_lock(&component->io_mutex);
725 val = soc_component_read_no_lock(component, reg);
726 mutex_unlock(&component->io_mutex);
727
728 return val;
729}
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900730EXPORT_SYMBOL_GPL(snd_soc_component_read);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900731
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900732static int soc_component_write_no_lock(
733 struct snd_soc_component *component,
734 unsigned int reg, unsigned int val)
735{
736 int ret = -EIO;
737
738 if (component->regmap)
739 ret = regmap_write(component->regmap, reg, val);
740 else if (component->driver->write)
741 ret = component->driver->write(component, reg, val);
742
743 return soc_component_ret(component, ret);
744}
745
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900746/**
747 * snd_soc_component_write() - Write register value
748 * @component: Component to write to
749 * @reg: Register to write
750 * @val: Value to write to the register
751 *
752 * Return: 0 on success, a negative error code otherwise.
753 */
754int snd_soc_component_write(struct snd_soc_component *component,
755 unsigned int reg, unsigned int val)
756{
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900757 int ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900758
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900759 mutex_lock(&component->io_mutex);
760 ret = soc_component_write_no_lock(component, reg, val);
761 mutex_unlock(&component->io_mutex);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900762
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900763 return ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900764}
765EXPORT_SYMBOL_GPL(snd_soc_component_write);
766
767static int snd_soc_component_update_bits_legacy(
768 struct snd_soc_component *component, unsigned int reg,
769 unsigned int mask, unsigned int val, bool *change)
770{
771 unsigned int old, new;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900772 int ret = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900773
774 mutex_lock(&component->io_mutex);
775
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900776 old = soc_component_read_no_lock(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900777
778 new = (old & ~mask) | (val & mask);
779 *change = old != new;
780 if (*change)
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900781 ret = soc_component_write_no_lock(component, reg, new);
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900782
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900783 mutex_unlock(&component->io_mutex);
784
785 return soc_component_ret(component, ret);
786}
787
788/**
789 * snd_soc_component_update_bits() - Perform read/modify/write cycle
790 * @component: Component to update
791 * @reg: Register to update
792 * @mask: Mask that specifies which bits to update
793 * @val: New value for the bits specified by mask
794 *
795 * Return: 1 if the operation was successful and the value of the register
796 * changed, 0 if the operation was successful, but the value did not change.
797 * Returns a negative error code otherwise.
798 */
799int snd_soc_component_update_bits(struct snd_soc_component *component,
800 unsigned int reg, unsigned int mask, unsigned int val)
801{
802 bool change;
803 int ret;
804
805 if (component->regmap)
806 ret = regmap_update_bits_check(component->regmap, reg, mask,
807 val, &change);
808 else
809 ret = snd_soc_component_update_bits_legacy(component, reg,
810 mask, val, &change);
811
812 if (ret < 0)
813 return soc_component_ret(component, ret);
814 return change;
815}
816EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
817
818/**
819 * snd_soc_component_update_bits_async() - Perform asynchronous
820 * read/modify/write cycle
821 * @component: Component to update
822 * @reg: Register to update
823 * @mask: Mask that specifies which bits to update
824 * @val: New value for the bits specified by mask
825 *
826 * This function is similar to snd_soc_component_update_bits(), but the update
827 * operation is scheduled asynchronously. This means it may not be completed
828 * when the function returns. To make sure that all scheduled updates have been
829 * completed snd_soc_component_async_complete() must be called.
830 *
831 * Return: 1 if the operation was successful and the value of the register
832 * changed, 0 if the operation was successful, but the value did not change.
833 * Returns a negative error code otherwise.
834 */
835int snd_soc_component_update_bits_async(struct snd_soc_component *component,
836 unsigned int reg, unsigned int mask, unsigned int val)
837{
838 bool change;
839 int ret;
840
841 if (component->regmap)
842 ret = regmap_update_bits_check_async(component->regmap, reg,
843 mask, val, &change);
844 else
845 ret = snd_soc_component_update_bits_legacy(component, reg,
846 mask, val, &change);
847
848 if (ret < 0)
849 return soc_component_ret(component, ret);
850 return change;
851}
852EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
853
854/**
Srinivas Kandagatla1da0b982021-01-26 17:17:48 +0000855 * snd_soc_component_read_field() - Read register field value
856 * @component: Component to read from
857 * @reg: Register to read
858 * @mask: mask of the register field
859 *
860 * Return: read value of register field.
861 */
862unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
863 unsigned int reg, unsigned int mask)
864{
865 unsigned int val;
866
867 val = snd_soc_component_read(component, reg);
868
869 val = (val & mask) >> soc_component_field_shift(component, mask);
870
871 return val;
872}
873EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
874
875/**
876 * snd_soc_component_write_field() - write to register field
877 * @component: Component to write to
878 * @reg: Register to write
879 * @mask: mask of the register field to update
880 * @val: value of the field to write
881 *
882 * Return: 1 for change, otherwise 0.
883 */
884int snd_soc_component_write_field(struct snd_soc_component *component,
885 unsigned int reg, unsigned int mask,
886 unsigned int val)
887{
888
889 val = (val << soc_component_field_shift(component, mask)) & mask;
890
891 return snd_soc_component_update_bits(component, reg, mask, val);
892}
893EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
894
895/**
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900896 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
897 * @component: Component for which to wait
898 *
899 * This function blocks until all asynchronous I/O which has previously been
900 * scheduled using snd_soc_component_update_bits_async() has completed.
901 */
902void snd_soc_component_async_complete(struct snd_soc_component *component)
903{
904 if (component->regmap)
905 regmap_async_complete(component->regmap);
906}
907EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
908
909/**
910 * snd_soc_component_test_bits - Test register for change
911 * @component: component
912 * @reg: Register to test
913 * @mask: Mask that specifies which bits to test
914 * @value: Value to test against
915 *
916 * Tests a register with a new value and checks if the new value is
917 * different from the old value.
918 *
919 * Return: 1 for change, otherwise 0.
920 */
921int snd_soc_component_test_bits(struct snd_soc_component *component,
922 unsigned int reg, unsigned int mask, unsigned int value)
923{
924 unsigned int old, new;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900925
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900926 old = snd_soc_component_read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900927 new = (old & ~mask) | value;
928 return old != new;
929}
930EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
931
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900932int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
933{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900934 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900935 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900936 int i;
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900937
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900938 /* FIXME: use 1st pointer */
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->pointer)
941 return component->driver->pointer(component, substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900942
943 return 0;
944}
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900945
946int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
947 unsigned int cmd, void *arg)
948{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900949 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900950 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900951 int i;
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900952
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900953 /* FIXME: use 1st ioctl */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900954 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900955 if (component->driver->ioctl)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900956 return soc_component_ret(
957 component,
958 component->driver->ioctl(component,
959 substream, cmd, arg));
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900960
961 return snd_pcm_lib_ioctl(substream, cmd, arg);
962}
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900963
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100964int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
965{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900966 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100967 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900968 int i, ret;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100969
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900970 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof1861a72020-02-28 10:48:35 +0900971 if (component->driver->sync_stop) {
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100972 ret = component->driver->sync_stop(component,
973 substream);
974 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800975 return soc_component_ret(component, ret);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100976 }
977 }
978
979 return 0;
980}
981
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900982int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
983 int channel, unsigned long pos,
984 void __user *buf, unsigned long bytes)
985{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900986 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900987 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900988 int i;
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900989
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900990 /* FIXME. it returns 1st copy now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900991 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900992 if (component->driver->copy_user)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900993 return soc_component_ret(
994 component,
995 component->driver->copy_user(
996 component, substream, channel,
997 pos, buf, bytes));
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900998
999 return -EINVAL;
1000}
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001001
1002struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
1003 unsigned long offset)
1004{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001005 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001006 struct snd_soc_component *component;
1007 struct page *page;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001008 int i;
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001009
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001010 /* FIXME. it returns 1st page now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001011 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +09001012 if (component->driver->page) {
1013 page = component->driver->page(component,
1014 substream, offset);
1015 if (page)
1016 return page;
1017 }
Kuninori Morimoto9c712e42019-07-26 13:52:00 +09001018 }
1019
1020 return NULL;
1021}
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001022
1023int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
1024 struct vm_area_struct *vma)
1025{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001026 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001027 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001028 int i;
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001029
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +09001030 /* FIXME. it returns 1st mmap now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001031 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +09001032 if (component->driver->mmap)
Shengjiu Wangbe75db52020-07-16 13:07:08 +08001033 return soc_component_ret(
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +09001034 component,
1035 component->driver->mmap(component,
1036 substream, vma));
Kuninori Morimoto205875e2019-07-26 13:52:04 +09001037
1038 return -EINVAL;
1039}
Kuninori Morimoto74842912019-07-26 13:52:08 +09001040
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001041int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto74842912019-07-26 13:52:08 +09001042{
Kuninori Morimoto74842912019-07-26 13:52:08 +09001043 struct snd_soc_component *component;
1044 int ret;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001045 int i;
Kuninori Morimoto74842912019-07-26 13:52:08 +09001046
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001047 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001048 if (component->driver->pcm_construct) {
1049 ret = component->driver->pcm_construct(component, rtd);
1050 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +08001051 return soc_component_ret(component, ret);
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001052 }
Kuninori Morimoto74842912019-07-26 13:52:08 +09001053 }
1054
1055 return 0;
1056}
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001057
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001058void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001059{
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001060 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001061 int i;
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001062
Takashi Iwai8e3366c2020-01-07 08:09:56 +01001063 if (!rtd->pcm)
1064 return;
1065
Kuninori Morimoto613fb502020-01-10 11:35:21 +09001066 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +09001067 if (component->driver->pcm_destruct)
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +09001068 component->driver->pcm_destruct(component, rtd->pcm);
Kuninori Morimoto79776da2019-07-26 13:52:12 +09001069}
Kuninori Morimoto4f395142020-06-04 17:06:58 +09001070
1071int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1072{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001073 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto4f395142020-06-04 17:06:58 +09001074 struct snd_soc_component *component;
1075 int i, ret;
1076
1077 for_each_rtd_components(rtd, i, component) {
1078 if (component->driver->prepare) {
1079 ret = component->driver->prepare(component, substream);
1080 if (ret < 0)
1081 return soc_component_ret(component, ret);
1082 }
1083 }
1084
1085 return 0;
1086}
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001087
1088int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001089 struct snd_pcm_hw_params *params)
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001090{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001091 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001092 struct snd_soc_component *component;
1093 int i, ret;
1094
1095 for_each_rtd_components(rtd, i, component) {
1096 if (component->driver->hw_params) {
1097 ret = component->driver->hw_params(component,
1098 substream, params);
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001099 if (ret < 0)
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001100 return soc_component_ret(component, ret);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001101 }
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001102 /* mark substream if succeeded */
1103 soc_component_mark_push(component, substream, hw_params);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001104 }
1105
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +09001106 return 0;
1107}
Kuninori Morimoto04751112020-06-04 17:07:24 +09001108
1109void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001110 int rollback)
Kuninori Morimoto04751112020-06-04 17:07:24 +09001111{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001112 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto04751112020-06-04 17:07:24 +09001113 struct snd_soc_component *component;
1114 int i, ret;
1115
1116 for_each_rtd_components(rtd, i, component) {
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001117 if (rollback && !soc_component_mark_match(component, substream, hw_params))
1118 continue;
Kuninori Morimoto04751112020-06-04 17:07:24 +09001119
1120 if (component->driver->hw_free) {
1121 ret = component->driver->hw_free(component, substream);
1122 if (ret < 0)
1123 soc_component_ret(component, ret);
1124 }
Kuninori Morimoto3a36a642020-09-29 13:31:41 +09001125
1126 /* remove marked substream */
1127 soc_component_mark_pop(component, substream, hw_params);
Kuninori Morimoto04751112020-06-04 17:07:24 +09001128 }
1129}
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001130
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001131static int soc_component_trigger(struct snd_soc_component *component,
1132 struct snd_pcm_substream *substream,
1133 int cmd)
1134{
1135 int ret = 0;
1136
1137 if (component->driver->trigger)
1138 ret = component->driver->trigger(component, substream, cmd);
1139
1140 return soc_component_ret(component, ret);
1141}
1142
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001143int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001144 int cmd, int rollback)
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001145{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +09001146 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001147 struct snd_soc_component *component;
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001148 int i, r, ret = 0;
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001149
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001150 switch (cmd) {
1151 case SNDRV_PCM_TRIGGER_START:
1152 case SNDRV_PCM_TRIGGER_RESUME:
1153 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1154 for_each_rtd_components(rtd, i, component) {
1155 ret = soc_component_trigger(component, substream, cmd);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001156 if (ret < 0)
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001157 break;
1158 soc_component_mark_push(component, substream, trigger);
1159 }
1160 break;
1161 case SNDRV_PCM_TRIGGER_STOP:
1162 case SNDRV_PCM_TRIGGER_SUSPEND:
1163 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1164 for_each_rtd_components(rtd, i, component) {
1165 if (rollback && !soc_component_mark_match(component, substream, trigger))
1166 continue;
1167
1168 r = soc_component_trigger(component, substream, cmd);
1169 if (r < 0)
1170 ret = r; /* use last ret */
1171 soc_component_mark_pop(component, substream, trigger);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001172 }
1173 }
1174
Kuninori Morimoto6374f492020-12-01 08:51:33 +09001175 return ret;
Kuninori Morimoto32fd1202020-06-04 17:07:40 +09001176}
Kuninori Morimoto939a5cf2020-09-28 09:01:17 +09001177
1178int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1179 void *stream)
1180{
1181 struct snd_soc_component *component;
1182 int i, ret;
1183
1184 for_each_rtd_components(rtd, i, component) {
1185 ret = pm_runtime_get_sync(component->dev);
1186 if (ret < 0 && ret != -EACCES) {
1187 pm_runtime_put_noidle(component->dev);
1188 return soc_component_ret(component, ret);
1189 }
1190 /* mark stream if succeeded */
1191 soc_component_mark_push(component, stream, pm);
1192 }
1193
1194 return 0;
1195}
1196
1197void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1198 void *stream, int rollback)
1199{
1200 struct snd_soc_component *component;
1201 int i;
1202
1203 for_each_rtd_components(rtd, i, component) {
1204 if (rollback && !soc_component_mark_match(component, stream, pm))
1205 continue;
1206
1207 pm_runtime_mark_last_busy(component->dev);
1208 pm_runtime_put_autosuspend(component->dev);
1209
1210 /* remove marked stream */
1211 soc_component_mark_pop(component, stream, pm);
1212 }
1213}