blob: 147b46e29ff45281327c06762e83d4d395c152d7 [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 Morimoto51aff912020-09-28 09:01:04 +090036/*
37 * We might want to check substream by using list.
38 * In such case, we can update these macros.
39 */
40#define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
41#define soc_component_mark_pop(component, substream, tgt) ((component)->mark_##tgt = NULL)
42#define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
43
Kuninori Morimoto257c4da2020-06-04 17:07:54 +090044void snd_soc_component_set_aux(struct snd_soc_component *component,
45 struct snd_soc_aux_dev *aux)
46{
47 component->init = (aux) ? aux->init : NULL;
48}
49
50int snd_soc_component_init(struct snd_soc_component *component)
51{
52 int ret = 0;
53
54 if (component->init)
55 ret = component->init(component);
56
57 return soc_component_ret(component, ret);
58}
59
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090060/**
61 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
62 * @component: COMPONENT
63 * @clk_id: DAI specific clock ID
64 * @source: Source for the clock
65 * @freq: new clock frequency in Hz
66 * @dir: new clock direction - input/output.
67 *
68 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
69 */
70int snd_soc_component_set_sysclk(struct snd_soc_component *component,
71 int clk_id, int source, unsigned int freq,
72 int dir)
73{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090074 int ret = -ENOTSUPP;
75
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090076 if (component->driver->set_sysclk)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090077 ret = component->driver->set_sysclk(component, clk_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090078 freq, dir);
79
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090080 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090081}
82EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
83
84/*
85 * snd_soc_component_set_pll - configure component PLL.
86 * @component: COMPONENT
87 * @pll_id: DAI specific PLL ID
88 * @source: DAI specific source for the PLL
89 * @freq_in: PLL input clock frequency in Hz
90 * @freq_out: requested PLL output clock frequency in Hz
91 *
92 * Configures and enables PLL to generate output clock based on input clock.
93 */
94int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
95 int source, unsigned int freq_in,
96 unsigned int freq_out)
97{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +090098 int ret = -EINVAL;
99
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900100 if (component->driver->set_pll)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900101 ret = component->driver->set_pll(component, pll_id, source,
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900102 freq_in, freq_out);
103
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900104 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900105}
106EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
107
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +0900108void snd_soc_component_seq_notifier(struct snd_soc_component *component,
109 enum snd_soc_dapm_type type, int subseq)
110{
111 if (component->driver->seq_notifier)
112 component->driver->seq_notifier(component, type, subseq);
113}
114
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900115int snd_soc_component_stream_event(struct snd_soc_component *component,
116 int event)
117{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900118 int ret = 0;
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900119
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900120 if (component->driver->stream_event)
121 ret = component->driver->stream_event(component, event);
122
123 return soc_component_ret(component, ret);
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900124}
125
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900126int snd_soc_component_set_bias_level(struct snd_soc_component *component,
127 enum snd_soc_bias_level level)
128{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900129 int ret = 0;
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900130
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900131 if (component->driver->set_bias_level)
132 ret = component->driver->set_bias_level(component, level);
133
134 return soc_component_ret(component, ret);
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900135}
136
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900137static int soc_component_pin(struct snd_soc_component *component,
138 const char *pin,
139 int (*pin_func)(struct snd_soc_dapm_context *dapm,
140 const char *pin))
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900141{
142 struct snd_soc_dapm_context *dapm =
143 snd_soc_component_get_dapm(component);
144 char *full_name;
145 int ret;
146
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900147 if (!component->name_prefix) {
148 ret = pin_func(dapm, pin);
149 goto end;
150 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900151
152 full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900153 if (!full_name) {
154 ret = -ENOMEM;
155 goto end;
156 }
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900157
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900158 ret = pin_func(dapm, full_name);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900159 kfree(full_name);
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900160end:
161 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900162}
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900163
164int snd_soc_component_enable_pin(struct snd_soc_component *component,
165 const char *pin)
166{
167 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
168}
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900169EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
170
171int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
172 const char *pin)
173{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900174 return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900175}
176EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
177
178int snd_soc_component_disable_pin(struct snd_soc_component *component,
179 const char *pin)
180{
Kuninori Morimoto4ca87012020-06-04 17:06:11 +0900181 return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900182}
183EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
184
185int snd_soc_component_disable_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_disable_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900189}
190EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
191
192int snd_soc_component_nc_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_nc_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900196}
197EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
198
199int snd_soc_component_nc_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_nc_pin_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900203}
204EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
205
206int snd_soc_component_get_pin_status(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_get_pin_status);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900210}
211EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
212
213int snd_soc_component_force_enable_pin(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_force_enable_pin);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900217}
218EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
219
220int snd_soc_component_force_enable_pin_unlocked(
221 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_unlocked);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900225}
226EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
227
228/**
229 * snd_soc_component_set_jack - configure component jack.
230 * @component: COMPONENTs
231 * @jack: structure to use for the jack
232 * @data: can be used if codec driver need extra data for configuring jack
233 *
234 * Configures and enables jack detection function.
235 */
236int snd_soc_component_set_jack(struct snd_soc_component *component,
237 struct snd_soc_jack *jack, void *data)
238{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900239 int ret = -ENOTSUPP;
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900240
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900241 if (component->driver->set_jack)
242 ret = component->driver->set_jack(component, jack, data);
243
244 return soc_component_ret(component, ret);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900245}
246EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900247
248int snd_soc_component_module_get(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900249 struct snd_pcm_substream *substream,
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900250 int upon_open)
251{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900252 int ret = 0;
253
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900254 if (component->driver->module_get_upon_open == !!upon_open &&
255 !try_module_get(component->dev->driver->owner))
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900256 ret = -ENODEV;
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900257
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900258 /* mark substream if succeeded */
259 if (ret == 0)
260 soc_component_mark_push(component, substream, module);
261
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900262 return soc_component_ret(component, ret);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900263}
264
265void snd_soc_component_module_put(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900266 struct snd_pcm_substream *substream,
267 int upon_open, int rollback)
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900268{
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900269 if (rollback && !soc_component_mark_match(component, substream, module))
270 return;
271
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900272 if (component->driver->module_get_upon_open == !!upon_open)
273 module_put(component->dev->driver->owner);
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900274
275 /* remove marked substream */
276 soc_component_mark_pop(component, substream, module);
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900277}
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900278
279int snd_soc_component_open(struct snd_soc_component *component,
280 struct snd_pcm_substream *substream)
281{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900282 int ret = 0;
283
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900284 if (component->driver->open)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900285 ret = component->driver->open(component, substream);
286
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900287 /* mark substream if succeeded */
288 if (ret == 0)
289 soc_component_mark_push(component, substream, open);
290
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900291 return soc_component_ret(component, ret);
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900292}
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900293
294int snd_soc_component_close(struct snd_soc_component *component,
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900295 struct snd_pcm_substream *substream,
296 int rollback)
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900297{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900298 int ret = 0;
299
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900300 if (rollback && !soc_component_mark_match(component, substream, open))
301 return 0;
302
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900303 if (component->driver->close)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900304 ret = component->driver->close(component, substream);
305
Kuninori Morimoto51aff912020-09-28 09:01:04 +0900306 /* remove marked substream */
307 soc_component_mark_pop(component, substream, open);
308
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900309 return soc_component_ret(component, ret);
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900310}
Kuninori Morimoto6d537232019-07-26 13:50:13 +0900311
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900312void snd_soc_component_suspend(struct snd_soc_component *component)
313{
314 if (component->driver->suspend)
315 component->driver->suspend(component);
316 component->suspended = 1;
317}
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900318
319void snd_soc_component_resume(struct snd_soc_component *component)
320{
321 if (component->driver->resume)
322 component->driver->resume(component);
323 component->suspended = 0;
324}
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900325
326int snd_soc_component_is_suspended(struct snd_soc_component *component)
327{
328 return component->suspended;
329}
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900330
331int snd_soc_component_probe(struct snd_soc_component *component)
332{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900333 int ret = 0;
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900334
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900335 if (component->driver->probe)
336 ret = component->driver->probe(component);
337
338 return soc_component_ret(component, ret);
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900339}
Kuninori Morimoto03b34dd2019-07-26 13:51:22 +0900340
341void snd_soc_component_remove(struct snd_soc_component *component)
342{
343 if (component->driver->remove)
344 component->driver->remove(component);
345}
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900346
347int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
348 struct device_node *ep)
349{
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900350 int ret = -ENOTSUPP;
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900351
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900352 if (component->driver->of_xlate_dai_id)
353 ret = component->driver->of_xlate_dai_id(component, ep);
354
355 return soc_component_ret(component, ret);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900356}
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900357
358int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
359 struct of_phandle_args *args,
360 const char **dai_name)
361{
362 if (component->driver->of_xlate_dai_name)
Jerome Brunetcc4d8ce2020-07-23 16:20:20 +0200363 return component->driver->of_xlate_dai_name(component,
364 args, dai_name);
365 /*
366 * Don't use soc_component_ret here because we may not want to report
367 * the error just yet. If a device has more than one component, the
368 * first may not match and we don't want spam the log with this.
369 */
370 return -ENOTSUPP;
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900371}
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900372
Kuninori Morimotoc7d75b52020-06-04 17:06:22 +0900373void snd_soc_component_setup_regmap(struct snd_soc_component *component)
374{
375 int val_bytes = regmap_get_val_bytes(component->regmap);
376
377 /* Errors are legitimate for non-integer byte multiples */
378 if (val_bytes > 0)
379 component->val_bytes = val_bytes;
380}
381
382#ifdef CONFIG_REGMAP
383
384/**
385 * snd_soc_component_init_regmap() - Initialize regmap instance for the
386 * component
387 * @component: The component for which to initialize the regmap instance
388 * @regmap: The regmap instance that should be used by the component
389 *
390 * This function allows deferred assignment of the regmap instance that is
391 * associated with the component. Only use this if the regmap instance is not
392 * yet ready when the component is registered. The function must also be called
393 * before the first IO attempt of the component.
394 */
395void snd_soc_component_init_regmap(struct snd_soc_component *component,
396 struct regmap *regmap)
397{
398 component->regmap = regmap;
399 snd_soc_component_setup_regmap(component);
400}
401EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
402
403/**
404 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
405 * component
406 * @component: The component for which to de-initialize the regmap instance
407 *
408 * Calls regmap_exit() on the regmap instance associated to the component and
409 * removes the regmap instance from the component.
410 *
411 * This function should only be used if snd_soc_component_init_regmap() was used
412 * to initialize the regmap instance.
413 */
414void snd_soc_component_exit_regmap(struct snd_soc_component *component)
415{
416 regmap_exit(component->regmap);
417 component->regmap = NULL;
418}
419EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
420
421#endif
422
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900423static unsigned int soc_component_read_no_lock(
424 struct snd_soc_component *component,
425 unsigned int reg)
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900426{
427 int ret;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900428 unsigned int val = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900429
430 if (component->regmap)
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900431 ret = regmap_read(component->regmap, reg, &val);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900432 else if (component->driver->read) {
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900433 ret = 0;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900434 val = component->driver->read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900435 }
436 else
437 ret = -EIO;
438
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900439 if (ret < 0)
Takashi Iwaiefc913c2020-08-10 15:46:31 +0200440 return soc_component_ret(component, ret);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900441
442 return val;
443}
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900444
445/**
446 * snd_soc_component_read() - Read register value
447 * @component: Component to read from
448 * @reg: Register to read
449 *
450 * Return: read value
451 */
452unsigned int snd_soc_component_read(struct snd_soc_component *component,
453 unsigned int reg)
454{
455 unsigned int val;
456
457 mutex_lock(&component->io_mutex);
458 val = soc_component_read_no_lock(component, reg);
459 mutex_unlock(&component->io_mutex);
460
461 return val;
462}
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900463EXPORT_SYMBOL_GPL(snd_soc_component_read);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900464
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900465static int soc_component_write_no_lock(
466 struct snd_soc_component *component,
467 unsigned int reg, unsigned int val)
468{
469 int ret = -EIO;
470
471 if (component->regmap)
472 ret = regmap_write(component->regmap, reg, val);
473 else if (component->driver->write)
474 ret = component->driver->write(component, reg, val);
475
476 return soc_component_ret(component, ret);
477}
478
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900479/**
480 * snd_soc_component_write() - Write register value
481 * @component: Component to write to
482 * @reg: Register to write
483 * @val: Value to write to the register
484 *
485 * Return: 0 on success, a negative error code otherwise.
486 */
487int snd_soc_component_write(struct snd_soc_component *component,
488 unsigned int reg, unsigned int val)
489{
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900490 int ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900491
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900492 mutex_lock(&component->io_mutex);
493 ret = soc_component_write_no_lock(component, reg, val);
494 mutex_unlock(&component->io_mutex);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900495
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900496 return ret;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900497}
498EXPORT_SYMBOL_GPL(snd_soc_component_write);
499
500static int snd_soc_component_update_bits_legacy(
501 struct snd_soc_component *component, unsigned int reg,
502 unsigned int mask, unsigned int val, bool *change)
503{
504 unsigned int old, new;
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900505 int ret = 0;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900506
507 mutex_lock(&component->io_mutex);
508
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900509 old = soc_component_read_no_lock(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900510
511 new = (old & ~mask) | (val & mask);
512 *change = old != new;
513 if (*change)
Kuninori Morimotoe8712312020-06-16 14:19:52 +0900514 ret = soc_component_write_no_lock(component, reg, new);
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900515
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900516 mutex_unlock(&component->io_mutex);
517
518 return soc_component_ret(component, ret);
519}
520
521/**
522 * snd_soc_component_update_bits() - Perform read/modify/write cycle
523 * @component: Component to update
524 * @reg: Register to update
525 * @mask: Mask that specifies which bits to update
526 * @val: New value for the bits specified by mask
527 *
528 * Return: 1 if the operation was successful and the value of the register
529 * changed, 0 if the operation was successful, but the value did not change.
530 * Returns a negative error code otherwise.
531 */
532int snd_soc_component_update_bits(struct snd_soc_component *component,
533 unsigned int reg, unsigned int mask, unsigned int val)
534{
535 bool change;
536 int ret;
537
538 if (component->regmap)
539 ret = regmap_update_bits_check(component->regmap, reg, mask,
540 val, &change);
541 else
542 ret = snd_soc_component_update_bits_legacy(component, reg,
543 mask, val, &change);
544
545 if (ret < 0)
546 return soc_component_ret(component, ret);
547 return change;
548}
549EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
550
551/**
552 * snd_soc_component_update_bits_async() - Perform asynchronous
553 * read/modify/write cycle
554 * @component: Component to update
555 * @reg: Register to update
556 * @mask: Mask that specifies which bits to update
557 * @val: New value for the bits specified by mask
558 *
559 * This function is similar to snd_soc_component_update_bits(), but the update
560 * operation is scheduled asynchronously. This means it may not be completed
561 * when the function returns. To make sure that all scheduled updates have been
562 * completed snd_soc_component_async_complete() must be called.
563 *
564 * Return: 1 if the operation was successful and the value of the register
565 * changed, 0 if the operation was successful, but the value did not change.
566 * Returns a negative error code otherwise.
567 */
568int snd_soc_component_update_bits_async(struct snd_soc_component *component,
569 unsigned int reg, unsigned int mask, unsigned int val)
570{
571 bool change;
572 int ret;
573
574 if (component->regmap)
575 ret = regmap_update_bits_check_async(component->regmap, reg,
576 mask, val, &change);
577 else
578 ret = snd_soc_component_update_bits_legacy(component, reg,
579 mask, val, &change);
580
581 if (ret < 0)
582 return soc_component_ret(component, ret);
583 return change;
584}
585EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
586
587/**
588 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
589 * @component: Component for which to wait
590 *
591 * This function blocks until all asynchronous I/O which has previously been
592 * scheduled using snd_soc_component_update_bits_async() has completed.
593 */
594void snd_soc_component_async_complete(struct snd_soc_component *component)
595{
596 if (component->regmap)
597 regmap_async_complete(component->regmap);
598}
599EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
600
601/**
602 * snd_soc_component_test_bits - Test register for change
603 * @component: component
604 * @reg: Register to test
605 * @mask: Mask that specifies which bits to test
606 * @value: Value to test against
607 *
608 * Tests a register with a new value and checks if the new value is
609 * different from the old value.
610 *
611 * Return: 1 for change, otherwise 0.
612 */
613int snd_soc_component_test_bits(struct snd_soc_component *component,
614 unsigned int reg, unsigned int mask, unsigned int value)
615{
616 unsigned int old, new;
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900617
Kuninori Morimotocf6e26c2020-06-16 14:19:41 +0900618 old = snd_soc_component_read(component, reg);
Kuninori Morimoto460b42d2020-06-04 17:08:03 +0900619 new = (old & ~mask) | value;
620 return old != new;
621}
622EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
623
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900624int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
625{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900626 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900627 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900628 int i;
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900629
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900630 /* FIXME: use 1st pointer */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900631 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900632 if (component->driver->pointer)
633 return component->driver->pointer(component, substream);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900634
635 return 0;
636}
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900637
638int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
639 unsigned int cmd, void *arg)
640{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900641 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900642 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900643 int i;
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900644
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900645 /* FIXME: use 1st ioctl */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900646 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900647 if (component->driver->ioctl)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900648 return soc_component_ret(
649 component,
650 component->driver->ioctl(component,
651 substream, cmd, arg));
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900652
653 return snd_pcm_lib_ioctl(substream, cmd, arg);
654}
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900655
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100656int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
657{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900658 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100659 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900660 int i, ret;
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100661
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900662 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotof1861a72020-02-28 10:48:35 +0900663 if (component->driver->sync_stop) {
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100664 ret = component->driver->sync_stop(component,
665 substream);
666 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800667 return soc_component_ret(component, ret);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100668 }
669 }
670
671 return 0;
672}
673
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900674int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
675 int channel, unsigned long pos,
676 void __user *buf, unsigned long bytes)
677{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900678 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900679 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900680 int i;
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900681
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900682 /* FIXME. it returns 1st copy now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900683 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900684 if (component->driver->copy_user)
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900685 return soc_component_ret(
686 component,
687 component->driver->copy_user(
688 component, substream, channel,
689 pos, buf, bytes));
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900690
691 return -EINVAL;
692}
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900693
694struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
695 unsigned long offset)
696{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900697 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900698 struct snd_soc_component *component;
699 struct page *page;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900700 int i;
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900701
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900702 /* FIXME. it returns 1st page now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900703 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900704 if (component->driver->page) {
705 page = component->driver->page(component,
706 substream, offset);
707 if (page)
708 return page;
709 }
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900710 }
711
712 return NULL;
713}
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900714
715int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
716 struct vm_area_struct *vma)
717{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900718 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900719 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900720 int i;
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900721
Kuninori Morimoto2b544dd2019-10-15 12:59:31 +0900722 /* FIXME. it returns 1st mmap now */
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900723 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900724 if (component->driver->mmap)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800725 return soc_component_ret(
Kuninori Morimotoe2329ee2020-06-04 17:06:41 +0900726 component,
727 component->driver->mmap(component,
728 substream, vma));
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900729
730 return -EINVAL;
731}
Kuninori Morimoto74842912019-07-26 13:52:08 +0900732
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +0900733int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto74842912019-07-26 13:52:08 +0900734{
Kuninori Morimoto74842912019-07-26 13:52:08 +0900735 struct snd_soc_component *component;
736 int ret;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900737 int i;
Kuninori Morimoto74842912019-07-26 13:52:08 +0900738
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900739 for_each_rtd_components(rtd, i, component) {
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +0900740 if (component->driver->pcm_construct) {
741 ret = component->driver->pcm_construct(component, rtd);
742 if (ret < 0)
Shengjiu Wangbe75db52020-07-16 13:07:08 +0800743 return soc_component_ret(component, ret);
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +0900744 }
Kuninori Morimoto74842912019-07-26 13:52:08 +0900745 }
746
747 return 0;
748}
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900749
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +0900750void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900751{
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900752 struct snd_soc_component *component;
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900753 int i;
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900754
Takashi Iwai8e3366c2020-01-07 08:09:56 +0100755 if (!rtd->pcm)
756 return;
757
Kuninori Morimoto613fb502020-01-10 11:35:21 +0900758 for_each_rtd_components(rtd, i, component)
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +0900759 if (component->driver->pcm_destruct)
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +0900760 component->driver->pcm_destruct(component, rtd->pcm);
Kuninori Morimoto79776da2019-07-26 13:52:12 +0900761}
Kuninori Morimoto4f395142020-06-04 17:06:58 +0900762
763int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
764{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900765 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto4f395142020-06-04 17:06:58 +0900766 struct snd_soc_component *component;
767 int i, ret;
768
769 for_each_rtd_components(rtd, i, component) {
770 if (component->driver->prepare) {
771 ret = component->driver->prepare(component, substream);
772 if (ret < 0)
773 return soc_component_ret(component, ret);
774 }
775 }
776
777 return 0;
778}
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +0900779
780int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
781 struct snd_pcm_hw_params *params,
782 struct snd_soc_component **last)
783{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900784 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +0900785 struct snd_soc_component *component;
786 int i, ret;
787
788 for_each_rtd_components(rtd, i, component) {
789 if (component->driver->hw_params) {
790 ret = component->driver->hw_params(component,
791 substream, params);
792 if (ret < 0) {
793 *last = component;
794 return soc_component_ret(component, ret);
795 }
796 }
797 }
798
799 *last = NULL;
800 return 0;
801}
Kuninori Morimoto04751112020-06-04 17:07:24 +0900802
803void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
804 struct snd_soc_component *last)
805{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900806 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto04751112020-06-04 17:07:24 +0900807 struct snd_soc_component *component;
808 int i, ret;
809
810 for_each_rtd_components(rtd, i, component) {
811 if (component == last)
812 break;
813
814 if (component->driver->hw_free) {
815 ret = component->driver->hw_free(component, substream);
816 if (ret < 0)
817 soc_component_ret(component, ret);
818 }
819 }
820}
Kuninori Morimoto32fd1202020-06-04 17:07:40 +0900821
822int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
823 int cmd)
824{
Kuninori Morimoto0ceef682020-07-20 10:17:39 +0900825 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto32fd1202020-06-04 17:07:40 +0900826 struct snd_soc_component *component;
827 int i, ret;
828
829 for_each_rtd_components(rtd, i, component) {
830 if (component->driver->trigger) {
831 ret = component->driver->trigger(component, substream, cmd);
832 if (ret < 0)
833 return soc_component_ret(component, ret);
834 }
835 }
836
837 return 0;
838}