blob: 428f88decfdba1fc2fa7a51ad16e1935f067fb42 [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 Morimoto4ff1fef2019-07-26 13:49:48 +090012#include <sound/soc.h>
13
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090014#define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
15static inline int _soc_component_ret(struct snd_soc_component *component,
16 const char *func, int ret)
17{
18 /* Positive/Zero values are not errors */
19 if (ret >= 0)
20 return ret;
21
22 /* Negative values might be errors */
23 switch (ret) {
24 case -EPROBE_DEFER:
25 case -ENOTSUPP:
26 break;
27 default:
28 dev_err(component->dev,
29 "ASoC: error at %s on %s: %d\n",
30 func, component->name, ret);
31 }
32
33 return ret;
34}
35
Kuninori Morimoto536aba12020-06-04 17:06:32 +090036int snd_soc_component_initialize(struct snd_soc_component *component,
37 const struct snd_soc_component_driver *driver,
38 struct device *dev, const char *name)
39{
40 INIT_LIST_HEAD(&component->dai_list);
41 INIT_LIST_HEAD(&component->dobj_list);
42 INIT_LIST_HEAD(&component->card_list);
43 mutex_init(&component->io_mutex);
44
45 component->name = name;
46 component->dev = dev;
47 component->driver = driver;
48
49 return 0;
50}
51
Kuninori Morimoto257c4da2020-06-04 17:07:54 +090052void snd_soc_component_set_aux(struct snd_soc_component *component,
53 struct snd_soc_aux_dev *aux)
54{
55 component->init = (aux) ? aux->init : NULL;
56}
57
58int snd_soc_component_init(struct snd_soc_component *component)
59{
60 int ret = 0;
61
62 if (component->init)
63 ret = component->init(component);
64
65 return soc_component_ret(component, ret);
66}
67
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090068/**
69 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
70 * @component: COMPONENT
71 * @clk_id: DAI specific clock ID
72 * @source: Source for the clock
73 * @freq: new clock frequency in Hz
74 * @dir: new clock direction - input/output.
75 *
76 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
77 */
78int snd_soc_component_set_sysclk(struct snd_soc_component *component,
79 int clk_id, int source, unsigned int freq,
80 int dir)
81{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090082 int ret = -ENOTSUPP;
83
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090084 if (component->driver->set_sysclk)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090085 ret = component->driver->set_sysclk(component, clk_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090086 freq, dir);
87
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090088 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090089}
90EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
91
92/*
93 * snd_soc_component_set_pll - configure component PLL.
94 * @component: COMPONENT
95 * @pll_id: DAI specific PLL ID
96 * @source: DAI specific source for the PLL
97 * @freq_in: PLL input clock frequency in Hz
98 * @freq_out: requested PLL output clock frequency in Hz
99 *
100 * Configures and enables PLL to generate output clock based on input clock.
101 */
102int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
103 int source, unsigned int freq_in,
104 unsigned int freq_out)
105{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900106 int ret = -EINVAL;
107
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900108 if (component->driver->set_pll)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900109 ret = component->driver->set_pll(component, pll_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900110 freq_in, freq_out);
111
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900112 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900113}
114EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
115
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +0900116void snd_soc_component_seq_notifier(struct snd_soc_component *component,
117 enum snd_soc_dapm_type type, int subseq)
118{
119 if (component->driver->seq_notifier)
120 component->driver->seq_notifier(component, type, subseq);
121}
122
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900123int snd_soc_component_stream_event(struct snd_soc_component *component,
124 int event)
125{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900126 int ret = 0;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900127
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900128 if (component->driver->stream_event)
129 ret = component->driver->stream_event(component, event);
130
131 return soc_component_ret(component, ret);
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900132}
133
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900134int snd_soc_component_set_bias_level(struct snd_soc_component *component,
135 enum snd_soc_bias_level level)
136{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900137 int ret = 0;
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900138
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900139 if (component->driver->set_bias_level)
140 ret = component->driver->set_bias_level(component, level);
141
142 return soc_component_ret(component, ret);
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900143}
144
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900145static int soc_component_pin(struct snd_soc_component *component,
146 const char *pin,
147 int (*pin_func)(struct snd_soc_dapm_context *dapm,
148 const char *pin))
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900149{
150 struct snd_soc_dapm_context *dapm =
151 snd_soc_component_get_dapm(component);
152 char *full_name;
153 int ret;
154
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900155 if (!component->name_prefix) {
156 ret = pin_func(dapm, pin);
157 goto end;
158 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900159
160 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900161 if (!full_name) {
162 ret = -ENOMEM;
163 goto end;
164 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900165
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900166 ret = pin_func(dapm, full_name);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900167 kfree(full_name);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900168end:
169 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900170}
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900171
172int snd_soc_component_enable_pin(struct snd_soc_component *component,
173 const char *pin)
174{
175 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
176}
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900177EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
178
179int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
180 const char *pin)
181{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900182 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900183}
184EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
185
186int snd_soc_component_disable_pin(struct snd_soc_component *component,
187 const char *pin)
188{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900189 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900190}
191EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
192
193int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
194 const char *pin)
195{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900196 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900197}
198EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
199
200int snd_soc_component_nc_pin(struct snd_soc_component *component,
201 const char *pin)
202{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900203 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900204}
205EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
206
207int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
208 const char *pin)
209{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900210 return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900211}
212EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
213
214int snd_soc_component_get_pin_status(struct snd_soc_component *component,
215 const char *pin)
216{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900217 return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900218}
219EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
220
221int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
222 const char *pin)
223{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900224 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900225}
226EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
227
228int snd_soc_component_force_enable_pin_unlocked(
229 struct snd_soc_component *component,
230 const char *pin)
231{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900232 return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900233}
234EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
235
236/**
237 * snd_soc_component_set_jack - configure component jack.
238 * @component: COMPONENTs
239 * @jack: structure to use for the jack
240 * @data: can be used if codec driver need extra data for configuring jack
241 *
242 * Configures and enables jack detection function.
243 */
244int snd_soc_component_set_jack(struct snd_soc_component *component,
245 struct snd_soc_jack *jack, void *data)
246{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900247 int ret = -ENOTSUPP;
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900248
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900249 if (component->driver->set_jack)
250 ret = component->driver->set_jack(component, jack, data);
251
252 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900253}
254EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900255
256int snd_soc_component_module_get(struct snd_soc_component *component,
257 int upon_open)
258{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900259 int ret = 0;
260
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900261 if (component->driver->module_get_upon_open == !!upon_open &&
262 !try_module_get(component->dev->driver->owner))
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900263 ret = -ENODEV;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900264
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900265 return soc_component_ret(component, ret);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900266}
267
268void snd_soc_component_module_put(struct snd_soc_component *component,
269 int upon_open)
270{
271 if (component->driver->module_get_upon_open == !!upon_open)
272 module_put(component->dev->driver->owner);
273}
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900274
275int snd_soc_component_open(struct snd_soc_component *component,
276 struct snd_pcm_substream *substream)
277{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900278 int ret = 0;
279
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900280 if (component->driver->open)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900281 ret = component->driver->open(component, substream);
282
283 return soc_component_ret(component, ret);
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900284}
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900285
286int snd_soc_component_close(struct snd_soc_component *component,
287 struct snd_pcm_substream *substream)
288{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900289 int ret = 0;
290
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900291 if (component->driver->close)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900292 ret = component->driver->close(component, substream);
293
294 return soc_component_ret(component, ret);
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900295}
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900296
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900297void snd_soc_component_suspend(struct snd_soc_component *component)
298{
299 if (component->driver->suspend)
300 component->driver->suspend(component);
301 component->suspended = 1;
302}
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900303
304void snd_soc_component_resume(struct snd_soc_component *component)
305{
306 if (component->driver->resume)
307 component->driver->resume(component);
308 component->suspended = 0;
309}
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900310
311int snd_soc_component_is_suspended(struct snd_soc_component *component)
312{
313 return component->suspended;
314}
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900315
316int snd_soc_component_probe(struct snd_soc_component *component)
317{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900318 int ret = 0;
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900319
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900320 if (component->driver->probe)
321 ret = component->driver->probe(component);
322
323 return soc_component_ret(component, ret);
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900324}
Kuninori Morimoto03b34dd2019-07-26 13:51:22 +0900325
326void snd_soc_component_remove(struct snd_soc_component *component)
327{
328 if (component->driver->remove)
329 component->driver->remove(component);
330}
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900331
332int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
333 struct device_node *ep)
334{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900335 int ret = -ENOTSUPP;
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900336
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900337 if (component->driver->of_xlate_dai_id)
338 ret = component->driver->of_xlate_dai_id(component, ep);
339
340 return soc_component_ret(component, ret);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900341}
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900342
343int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
344 struct of_phandle_args *args,
345 const char **dai_name)
346{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900347 int ret = -ENOTSUPP;
348
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900349 if (component->driver->of_xlate_dai_name)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900350 ret = component->driver->of_xlate_dai_name(component,
351 args, dai_name);
352
353 return soc_component_ret(component, ret);
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900354}
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900355
Kuninori Morimotoc7d75b52020-06-04 17:06:22 +0900356void snd_soc_component_setup_regmap(struct snd_soc_component *component)
357{
358 int val_bytes = regmap_get_val_bytes(component->regmap);
359
360 /* Errors are legitimate for non-integer byte multiples */
361 if (val_bytes > 0)
362 component->val_bytes = val_bytes;
363}
364
365#ifdef CONFIG_REGMAP
366
367/**
368 * snd_soc_component_init_regmap() - Initialize regmap instance for the
369 * component
370 * @component: The component for which to initialize the regmap instance
371 * @regmap: The regmap instance that should be used by the component
372 *
373 * This function allows deferred assignment of the regmap instance that is
374 * associated with the component. Only use this if the regmap instance is not
375 * yet ready when the component is registered. The function must also be called
376 * before the first IO attempt of the component.
377 */
378void snd_soc_component_init_regmap(struct snd_soc_component *component,
379 struct regmap *regmap)
380{
381 component->regmap = regmap;
382 snd_soc_component_setup_regmap(component);
383}
384EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
385
386/**
387 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
388 * component
389 * @component: The component for which to de-initialize the regmap instance
390 *
391 * Calls regmap_exit() on the regmap instance associated to the component and
392 * removes the regmap instance from the component.
393 *
394 * This function should only be used if snd_soc_component_init_regmap() was used
395 * to initialize the regmap instance.
396 */
397void snd_soc_component_exit_regmap(struct snd_soc_component *component)
398{
399 regmap_exit(component->regmap);
400 component->regmap = NULL;
401}
402EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
403
404#endif
405
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900406/**
407 * snd_soc_component_read() - Read register value
408 * @component: Component to read from
409 * @reg: Register to read
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900410 *
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900411 * Return: read value
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900412 */
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900413unsigned int snd_soc_component_read(struct snd_soc_component *component,
414 unsigned int reg)
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900415{
416 int ret;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900417 unsigned int val = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900418
419 if (component->regmap)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900420 ret = regmap_read(component->regmap, reg, &val);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900421 else if (component->driver->read) {
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900422 ret = 0;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900423 val = component->driver->read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900424 }
425 else
426 ret = -EIO;
427
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900428 if (ret < 0)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900429 soc_component_ret(component, ret);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900430
431 return val;
432}
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900433EXPORT_SYMBOL_GPL(snd_soc_component_read);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900434
435/**
436 * snd_soc_component_write() - Write register value
437 * @component: Component to write to
438 * @reg: Register to write
439 * @val: Value to write to the register
440 *
441 * Return: 0 on success, a negative error code otherwise.
442 */
443int snd_soc_component_write(struct snd_soc_component *component,
444 unsigned int reg, unsigned int val)
445{
446 int ret = -EIO;
447
448 if (component->regmap)
449 ret = regmap_write(component->regmap, reg, val);
450 else if (component->driver->write)
451 ret = component->driver->write(component, reg, val);
452
453 return soc_component_ret(component, ret);
454}
455EXPORT_SYMBOL_GPL(snd_soc_component_write);
456
457static int snd_soc_component_update_bits_legacy(
458 struct snd_soc_component *component, unsigned int reg,
459 unsigned int mask, unsigned int val, bool *change)
460{
461 unsigned int old, new;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900462 int ret = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900463
464 mutex_lock(&component->io_mutex);
465
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900466 old = snd_soc_component_read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900467
468 new = (old & ~mask) | (val & mask);
469 *change = old != new;
470 if (*change)
471 ret = snd_soc_component_write(component, reg, new);
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900472
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900473 mutex_unlock(&component->io_mutex);
474
475 return soc_component_ret(component, ret);
476}
477
478/**
479 * snd_soc_component_update_bits() - Perform read/modify/write cycle
480 * @component: Component to update
481 * @reg: Register to update
482 * @mask: Mask that specifies which bits to update
483 * @val: New value for the bits specified by mask
484 *
485 * Return: 1 if the operation was successful and the value of the register
486 * changed, 0 if the operation was successful, but the value did not change.
487 * Returns a negative error code otherwise.
488 */
489int snd_soc_component_update_bits(struct snd_soc_component *component,
490 unsigned int reg, unsigned int mask, unsigned int val)
491{
492 bool change;
493 int ret;
494
495 if (component->regmap)
496 ret = regmap_update_bits_check(component->regmap, reg, mask,
497 val, &change);
498 else
499 ret = snd_soc_component_update_bits_legacy(component, reg,
500 mask, val, &change);
501
502 if (ret < 0)
503 return soc_component_ret(component, ret);
504 return change;
505}
506EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
507
508/**
509 * snd_soc_component_update_bits_async() - Perform asynchronous
510 * read/modify/write cycle
511 * @component: Component to update
512 * @reg: Register to update
513 * @mask: Mask that specifies which bits to update
514 * @val: New value for the bits specified by mask
515 *
516 * This function is similar to snd_soc_component_update_bits(), but the update
517 * operation is scheduled asynchronously. This means it may not be completed
518 * when the function returns. To make sure that all scheduled updates have been
519 * completed snd_soc_component_async_complete() must be called.
520 *
521 * Return: 1 if the operation was successful and the value of the register
522 * changed, 0 if the operation was successful, but the value did not change.
523 * Returns a negative error code otherwise.
524 */
525int snd_soc_component_update_bits_async(struct snd_soc_component *component,
526 unsigned int reg, unsigned int mask, unsigned int val)
527{
528 bool change;
529 int ret;
530
531 if (component->regmap)
532 ret = regmap_update_bits_check_async(component->regmap, reg,
533 mask, val, &change);
534 else
535 ret = snd_soc_component_update_bits_legacy(component, reg,
536 mask, val, &change);
537
538 if (ret < 0)
539 return soc_component_ret(component, ret);
540 return change;
541}
542EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
543
544/**
545 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
546 * @component: Component for which to wait
547 *
548 * This function blocks until all asynchronous I/O which has previously been
549 * scheduled using snd_soc_component_update_bits_async() has completed.
550 */
551void snd_soc_component_async_complete(struct snd_soc_component *component)
552{
553 if (component->regmap)
554 regmap_async_complete(component->regmap);
555}
556EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
557
558/**
559 * snd_soc_component_test_bits - Test register for change
560 * @component: component
561 * @reg: Register to test
562 * @mask: Mask that specifies which bits to test
563 * @value: Value to test against
564 *
565 * Tests a register with a new value and checks if the new value is
566 * different from the old value.
567 *
568 * Return: 1 for change, otherwise 0.
569 */
570int snd_soc_component_test_bits(struct snd_soc_component *component,
571 unsigned int reg, unsigned int mask, unsigned int value)
572{
573 unsigned int old, new;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900574
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900575 old = snd_soc_component_read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900576 new = (old & ~mask) | value;
577 return old != new;
578}
579EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
580
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900581int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
582{
583 struct snd_soc_pcm_runtime *rtd = substream->private_data;
584 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900585 int i;
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900586
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900587 /* FIXME: use 1st pointer */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900588 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900589 if (component->driver->pointer)
590 return component->driver->pointer(component, substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900591
592 return 0;
593}
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900594
595int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
596 unsigned int cmd, void *arg)
597{
598 struct snd_soc_pcm_runtime *rtd = substream->private_data;
599 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900600 int i;
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900601
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900602 /* FIXME: use 1st ioctl */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900603 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900604 if (component->driver->ioctl)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900605 return soc_component_ret(
606 component,
607 component->driver->ioctl(component,
608 substream, cmd, arg));
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900609
610 return snd_pcm_lib_ioctl(substream, cmd, arg);
611}
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900612
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100613int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
614{
615 struct snd_soc_pcm_runtime *rtd = substream->private_data;
616 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900617 int i, ret;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100618
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900619 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof1861a72020-02-28 10:48:35 +0900620 if (component->driver->sync_stop) {
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100621 ret = component->driver->sync_stop(component,
622 substream);
623 if (ret < 0)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900624 soc_component_ret(component, ret);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100625 }
626 }
627
628 return 0;
629}
630
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900631int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
632 int channel, unsigned long pos,
633 void __user *buf, unsigned long bytes)
634{
635 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900636 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900637 int i;
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900638
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900639 /* FIXME. it returns 1st copy now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900640 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900641 if (component->driver->copy_user)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900642 return soc_component_ret(
643 component,
644 component->driver->copy_user(
645 component, substream, channel,
646 pos, buf, bytes));
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900647
648 return -EINVAL;
649}
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900650
651struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
652 unsigned long offset)
653{
654 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900655 struct snd_soc_component *component;
656 struct page *page;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900657 int i;
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900658
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900659 /* FIXME. it returns 1st page now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900660 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900661 if (component->driver->page) {
662 page = component->driver->page(component,
663 substream, offset);
664 if (page)
665 return page;
666 }
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900667 }
668
669 return NULL;
670}
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900671
672int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
673 struct vm_area_struct *vma)
674{
675 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900676 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900677 int i;
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900678
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900679 /* FIXME. it returns 1st mmap now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900680 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900681 if (component->driver->mmap)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900682 soc_component_ret(
683 component,
684 component->driver->mmap(component,
685 substream, vma));
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900686
687 return -EINVAL;
688}
Kuninori Morimoto74842912019-07-26 13:52:08 +0900689
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +0900690int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto74842912019-07-26 13:52:08 +0900691{
Kuninori Morimoto74842912019-07-26 13:52:08 +0900692 struct snd_soc_component *component;
693 int ret;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900694 int i;
Kuninori Morimoto74842912019-07-26 13:52:08 +0900695
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900696 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +0900697 if (component->driver->pcm_construct) {
698 ret = component->driver->pcm_construct(component, rtd);
699 if (ret < 0)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900700 soc_component_ret(component, ret);
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +0900701 }
Kuninori Morimoto74842912019-07-26 13:52:08 +0900702 }
703
704 return 0;
705}
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900706
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +0900707void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900708{
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900709 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900710 int i;
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900711
Takashi Iwai8e3366c2020-01-07 08:09:56 +0100712 if (!rtd->pcm)
713 return;
714
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900715 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +0900716 if (component->driver->pcm_destruct)
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +0900717 component->driver->pcm_destruct(component, rtd->pcm);
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900718}
Kuninori Morimoto4f395142020-06-04 17:06:58 +0900719
720int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
721{
722 struct snd_soc_pcm_runtime *rtd = substream->private_data;
723 struct snd_soc_component *component;
724 int i, ret;
725
726 for_each_rtd_components(rtd, i, component) {
727 if (component->driver->prepare) {
728 ret = component->driver->prepare(component, substream);
729 if (ret < 0)
730 return soc_component_ret(component, ret);
731 }
732 }
733
734 return 0;
735}
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +0900736
737int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
738 struct snd_pcm_hw_params *params,
739 struct snd_soc_component **last)
740{
741 struct snd_soc_pcm_runtime *rtd = substream->private_data;
742 struct snd_soc_component *component;
743 int i, ret;
744
745 for_each_rtd_components(rtd, i, component) {
746 if (component->driver->hw_params) {
747 ret = component->driver->hw_params(component,
748 substream, params);
749 if (ret < 0) {
750 *last = component;
751 return soc_component_ret(component, ret);
752 }
753 }
754 }
755
756 *last = NULL;
757 return 0;
758}
Kuninori Morimoto04751112020-06-04 17:07:24 +0900759
760void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
761 struct snd_soc_component *last)
762{
763 struct snd_soc_pcm_runtime *rtd = substream->private_data;
764 struct snd_soc_component *component;
765 int i, ret;
766
767 for_each_rtd_components(rtd, i, component) {
768 if (component == last)
769 break;
770
771 if (component->driver->hw_free) {
772 ret = component->driver->hw_free(component, substream);
773 if (ret < 0)
774 soc_component_ret(component, ret);
775 }
776 }
777}
Kuninori Morimoto32fd1202020-06-04 17:07:40 +0900778
779int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
780 int cmd)
781{
782 struct snd_soc_pcm_runtime *rtd = substream->private_data;
783 struct snd_soc_component *component;
784 int i, ret;
785
786 for_each_rtd_components(rtd, i, component) {
787 if (component->driver->trigger) {
788 ret = component->driver->trigger(component, substream, cmd);
789 if (ret < 0)
790 return soc_component_ret(component, ret);
791 }
792 }
793
794 return 0;
795}