1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // soc-component.c
4 //
5 // Copyright 2009-2011 Wolfson Microelectronics PLC.
6 // Copyright (C) 2019 Renesas Electronics Corp.
7 //
8 // Mark Brown <broonie@opensource.wolfsonmicro.com>
9 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
10 //
11 #include <linux/module.h>
12 #include <linux/pm_runtime.h>
13 #include <sound/soc.h>
14 #include <linux/bitops.h>
15
16 #define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
_soc_component_ret(struct snd_soc_component * component,const char * func,int ret)17 static inline int _soc_component_ret(struct snd_soc_component *component, const char *func, int ret)
18 {
19 return snd_soc_ret(component->dev, ret,
20 "at %s() on %s\n", func, component->name);
21 }
22
23 #define soc_component_ret_reg_rw(dai, ret, reg) _soc_component_ret_reg_rw(dai, __func__, ret, reg)
_soc_component_ret_reg_rw(struct snd_soc_component * component,const char * func,int ret,int reg)24 static inline int _soc_component_ret_reg_rw(struct snd_soc_component *component,
25 const char *func, int ret, int reg)
26 {
27 return snd_soc_ret(component->dev, ret,
28 "at %s() on %s for register: [0x%08x]\n",
29 func, component->name, reg);
30 }
31
snd_soc_component_alloc(struct device * dev)32 struct snd_soc_component *snd_soc_component_alloc(struct device *dev)
33 {
34 struct snd_soc_component *component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
35
36 if (!component)
37 return NULL;
38
39 component->dev = dev;
40
41 return component;
42 }
43 EXPORT_SYMBOL_GPL(snd_soc_component_alloc);
44
snd_soc_component_set_name(struct snd_soc_component * component,const char * name)45 void snd_soc_component_set_name(struct snd_soc_component *component, const char *name)
46 {
47 component->name = name;
48 }
49 EXPORT_SYMBOL_GPL(snd_soc_component_set_name);
50
snd_soc_component_name(struct snd_soc_component * component)51 const char *snd_soc_component_name(struct snd_soc_component *component)
52 {
53 return component->name;
54 }
55 EXPORT_SYMBOL_GPL(snd_soc_component_name);
56
snd_soc_component_set_priv(struct snd_soc_component * component,void * priv)57 void snd_soc_component_set_priv(struct snd_soc_component *component, void *priv)
58 {
59 component->priv = priv;
60 }
61 EXPORT_SYMBOL_GPL(snd_soc_component_set_priv);
62
snd_soc_component_to_priv(struct snd_soc_component * component)63 void *snd_soc_component_to_priv(struct snd_soc_component *component)
64 {
65 return component->priv;
66 }
67 EXPORT_SYMBOL_GPL(snd_soc_component_to_priv);
68
69 /*
70 * We might want to check substream by using list.
71 * In such case, we can update these macros.
72 */
73 #define soc_component_mark_push(component, substream, tgt) ((component)->mark_##tgt = substream)
74 #define soc_component_mark_pop(component, tgt) ((component)->mark_##tgt = NULL)
75 #define soc_component_mark_match(component, substream, tgt) ((component)->mark_##tgt == substream)
76
snd_soc_component_set_aux(struct snd_soc_component * component,struct snd_soc_aux_dev * aux)77 void snd_soc_component_set_aux(struct snd_soc_component *component,
78 struct snd_soc_aux_dev *aux)
79 {
80 component->init = (aux) ? aux->init : NULL;
81 }
82
snd_soc_component_init(struct snd_soc_component * component)83 int snd_soc_component_init(struct snd_soc_component *component)
84 {
85 int ret = 0;
86
87 if (component->init)
88 ret = component->init(component);
89
90 return soc_component_ret(component, ret);
91 }
92
93 /**
94 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
95 * @component: COMPONENT
96 * @clk_id: DAI specific clock ID
97 * @source: Source for the clock
98 * @freq: new clock frequency in Hz
99 * @dir: new clock direction - input/output.
100 *
101 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
102 */
snd_soc_component_set_sysclk(struct snd_soc_component * component,int clk_id,int source,unsigned int freq,int dir)103 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
104 int clk_id, int source, unsigned int freq,
105 int dir)
106 {
107 int ret = -ENOTSUPP;
108
109 if (component->driver->set_sysclk)
110 ret = component->driver->set_sysclk(component, clk_id, source,
111 freq, dir);
112
113 return soc_component_ret(component, ret);
114 }
115 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
116
117 /*
118 * snd_soc_component_set_pll - configure component PLL.
119 * @component: COMPONENT
120 * @pll_id: DAI specific PLL ID
121 * @source: DAI specific source for the PLL
122 * @freq_in: PLL input clock frequency in Hz
123 * @freq_out: requested PLL output clock frequency in Hz
124 *
125 * Configures and enables PLL to generate output clock based on input clock.
126 */
snd_soc_component_set_pll(struct snd_soc_component * component,int pll_id,int source,unsigned int freq_in,unsigned int freq_out)127 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
128 int source, unsigned int freq_in,
129 unsigned int freq_out)
130 {
131 int ret = -EINVAL;
132
133 if (component->driver->set_pll)
134 ret = component->driver->set_pll(component, pll_id, source,
135 freq_in, freq_out);
136
137 return soc_component_ret(component, ret);
138 }
139 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
140
snd_soc_component_seq_notifier(struct snd_soc_component * component,enum snd_soc_dapm_type type,int subseq)141 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
142 enum snd_soc_dapm_type type, int subseq)
143 {
144 if (component->driver->seq_notifier)
145 component->driver->seq_notifier(component, type, subseq);
146 }
147
snd_soc_component_stream_event(struct snd_soc_component * component,int event)148 int snd_soc_component_stream_event(struct snd_soc_component *component,
149 int event)
150 {
151 int ret = 0;
152
153 if (component->driver->stream_event)
154 ret = component->driver->stream_event(component, event);
155
156 return soc_component_ret(component, ret);
157 }
158
snd_soc_component_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)159 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
160 enum snd_soc_bias_level level)
161 {
162 int ret = 0;
163
164 if (component->driver->set_bias_level)
165 ret = component->driver->set_bias_level(component, level);
166
167 return soc_component_ret(component, ret);
168 }
169
soc_get_kcontrol_name(struct snd_soc_component * component,char * buf,int size,const char * const ctl)170 static void soc_get_kcontrol_name(struct snd_soc_component *component,
171 char *buf, int size, const char * const ctl)
172 {
173 /* When updating, change also snd_soc_dapm_widget_name_cmp() */
174 if (component->name_prefix)
175 snprintf(buf, size, "%s %s", component->name_prefix, ctl);
176 else
177 snprintf(buf, size, "%s", ctl);
178 }
179
snd_soc_component_get_kcontrol(struct snd_soc_component * component,const char * const ctl)180 struct snd_kcontrol *snd_soc_component_get_kcontrol(struct snd_soc_component *component,
181 const char * const ctl)
182 {
183 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
184
185 soc_get_kcontrol_name(component, name, ARRAY_SIZE(name), ctl);
186
187 return snd_soc_card_get_kcontrol(component->card, name);
188 }
189 EXPORT_SYMBOL_GPL(snd_soc_component_get_kcontrol);
190
snd_soc_component_notify_control(struct snd_soc_component * component,const char * const ctl)191 int snd_soc_component_notify_control(struct snd_soc_component *component,
192 const char * const ctl)
193 {
194 struct snd_kcontrol *kctl;
195
196 kctl = snd_soc_component_get_kcontrol(component, ctl);
197 if (!kctl)
198 return soc_component_ret(component, -EINVAL);
199
200 snd_ctl_notify(component->card->snd_card,
201 SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
202
203 return 0;
204 }
205 EXPORT_SYMBOL_GPL(snd_soc_component_notify_control);
206
207 /**
208 * snd_soc_component_set_jack - configure component jack.
209 * @component: COMPONENTs
210 * @jack: structure to use for the jack
211 * @data: can be used if codec driver need extra data for configuring jack
212 *
213 * Configures and enables jack detection function.
214 */
snd_soc_component_set_jack(struct snd_soc_component * component,struct snd_soc_jack * jack,void * data)215 int snd_soc_component_set_jack(struct snd_soc_component *component,
216 struct snd_soc_jack *jack, void *data)
217 {
218 int ret = -ENOTSUPP;
219
220 if (component->driver->set_jack)
221 ret = component->driver->set_jack(component, jack, data);
222
223 return soc_component_ret(component, ret);
224 }
225 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
226
227 /**
228 * snd_soc_component_get_jack_type
229 * @component: COMPONENTs
230 *
231 * Returns the jack type of the component
232 * This can either be the supported type or one read from
233 * devicetree with the property: jack-type.
234 */
snd_soc_component_get_jack_type(struct snd_soc_component * component)235 int snd_soc_component_get_jack_type(
236 struct snd_soc_component *component)
237 {
238 int ret = -ENOTSUPP;
239
240 if (component->driver->get_jack_type)
241 ret = component->driver->get_jack_type(component);
242
243 return soc_component_ret(component, ret);
244 }
245 EXPORT_SYMBOL_GPL(snd_soc_component_get_jack_type);
246
snd_soc_component_module_get(struct snd_soc_component * component,void * mark,int upon_open)247 int snd_soc_component_module_get(struct snd_soc_component *component,
248 void *mark, int upon_open)
249 {
250 int ret = 0;
251
252 if (component->driver->module_get_upon_open == !!upon_open &&
253 !try_module_get(component->dev->driver->owner))
254 ret = -ENODEV;
255
256 /* mark module if succeeded */
257 if (ret == 0)
258 soc_component_mark_push(component, mark, module);
259
260 return soc_component_ret(component, ret);
261 }
262
snd_soc_component_module_put(struct snd_soc_component * component,void * mark,int upon_open,int rollback)263 void snd_soc_component_module_put(struct snd_soc_component *component,
264 void *mark, int upon_open, int rollback)
265 {
266 if (rollback && !soc_component_mark_match(component, mark, module))
267 return;
268
269 if (component->driver->module_get_upon_open == !!upon_open)
270 module_put(component->dev->driver->owner);
271
272 /* remove the mark from module */
273 soc_component_mark_pop(component, module);
274 }
275
snd_soc_component_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)276 int snd_soc_component_open(struct snd_soc_component *component,
277 struct snd_pcm_substream *substream)
278 {
279 int ret = 0;
280
281 if (component->driver->open)
282 ret = component->driver->open(component, substream);
283
284 /* mark substream if succeeded */
285 if (ret == 0)
286 soc_component_mark_push(component, substream, open);
287
288 return soc_component_ret(component, ret);
289 }
290
snd_soc_component_close(struct snd_soc_component * component,struct snd_pcm_substream * substream,int rollback)291 int snd_soc_component_close(struct snd_soc_component *component,
292 struct snd_pcm_substream *substream,
293 int rollback)
294 {
295 int ret = 0;
296
297 if (rollback && !soc_component_mark_match(component, substream, open))
298 return 0;
299
300 if (component->driver->close)
301 ret = component->driver->close(component, substream);
302
303 /* remove marked substream */
304 soc_component_mark_pop(component, open);
305
306 return soc_component_ret(component, ret);
307 }
308
snd_soc_component_suspend(struct snd_soc_component * component)309 void snd_soc_component_suspend(struct snd_soc_component *component)
310 {
311 if (component->driver->suspend)
312 component->driver->suspend(component);
313 component->suspended = 1;
314 }
315
snd_soc_component_resume(struct snd_soc_component * component)316 void snd_soc_component_resume(struct snd_soc_component *component)
317 {
318 if (component->driver->resume)
319 component->driver->resume(component);
320 component->suspended = 0;
321 }
322
snd_soc_component_is_suspended(struct snd_soc_component * component)323 int snd_soc_component_is_suspended(struct snd_soc_component *component)
324 {
325 return component->suspended;
326 }
327
snd_soc_component_probe(struct snd_soc_component * component)328 int snd_soc_component_probe(struct snd_soc_component *component)
329 {
330 int ret = 0;
331
332 if (component->driver->probe)
333 ret = component->driver->probe(component);
334
335 return soc_component_ret(component, ret);
336 }
337
snd_soc_component_fixup_controls(struct snd_soc_component * component)338 int snd_soc_component_fixup_controls(struct snd_soc_component *component)
339 {
340 int ret = 0;
341
342 if (component->driver->fixup_controls)
343 ret = component->driver->fixup_controls(component);
344
345 return soc_component_ret(component, ret);
346 }
347
snd_soc_component_remove(struct snd_soc_component * component)348 void snd_soc_component_remove(struct snd_soc_component *component)
349 {
350 if (component->driver->remove)
351 component->driver->remove(component);
352 }
353
snd_soc_component_of_xlate_dai_id(struct snd_soc_component * component,struct device_node * ep)354 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
355 struct device_node *ep)
356 {
357 int ret = -ENOTSUPP;
358
359 if (component->driver->of_xlate_dai_id)
360 ret = component->driver->of_xlate_dai_id(component, ep);
361
362 return soc_component_ret(component, ret);
363 }
364
snd_soc_component_of_xlate_dai_name(struct snd_soc_component * component,const struct of_phandle_args * args,const char ** dai_name)365 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
366 const struct of_phandle_args *args,
367 const char **dai_name)
368 {
369 if (component->driver->of_xlate_dai_name)
370 return component->driver->of_xlate_dai_name(component,
371 args, dai_name);
372 /*
373 * Don't use soc_component_ret here because we may not want to report
374 * the error just yet. If a device has more than one component, the
375 * first may not match and we don't want spam the log with this.
376 */
377 return -ENOTSUPP;
378 }
379
snd_soc_component_regmap_val_bytes(struct snd_soc_component * component)380 int snd_soc_component_regmap_val_bytes(struct snd_soc_component *component)
381 {
382 int val_bytes;
383
384 /* Errors are legitimate for non-integer byte multiples */
385
386 if (!component->regmap)
387 return 0;
388
389 val_bytes = regmap_get_val_bytes(component->regmap);
390 if (val_bytes < 0)
391 return 0;
392
393 return val_bytes;
394 }
395 EXPORT_SYMBOL_GPL(snd_soc_component_regmap_val_bytes);
396
397 #ifdef CONFIG_REGMAP
398
399 /**
400 * snd_soc_component_init_regmap() - Initialize regmap instance for the
401 * component
402 * @component: The component for which to initialize the regmap instance
403 * @regmap: The regmap instance that should be used by the component
404 *
405 * This function allows deferred assignment of the regmap instance that is
406 * associated with the component. Only use this if the regmap instance is not
407 * yet ready when the component is registered. The function must also be called
408 * before the first IO attempt of the component.
409 */
snd_soc_component_init_regmap(struct snd_soc_component * component,struct regmap * regmap)410 void snd_soc_component_init_regmap(struct snd_soc_component *component,
411 struct regmap *regmap)
412 {
413 component->regmap = regmap;
414 }
415 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
416
417 /**
418 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
419 * component
420 * @component: The component for which to de-initialize the regmap instance
421 *
422 * Calls regmap_exit() on the regmap instance associated to the component and
423 * removes the regmap instance from the component.
424 *
425 * This function should only be used if snd_soc_component_init_regmap() was used
426 * to initialize the regmap instance.
427 */
snd_soc_component_exit_regmap(struct snd_soc_component * component)428 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
429 {
430 regmap_exit(component->regmap);
431 component->regmap = NULL;
432 }
433 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
434
435 #endif
436
snd_soc_component_compr_open(struct snd_soc_component * component,struct snd_compr_stream * cstream)437 int snd_soc_component_compr_open(struct snd_soc_component *component,
438 struct snd_compr_stream *cstream)
439 {
440 int ret = 0;
441
442 if (component->driver->compress_ops &&
443 component->driver->compress_ops->open)
444 ret = component->driver->compress_ops->open(component, cstream);
445
446 /* mark substream if succeeded */
447 if (ret == 0)
448 soc_component_mark_push(component, cstream, compr_open);
449
450 return soc_component_ret(component, ret);
451 }
452 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
453
snd_soc_component_compr_free(struct snd_soc_component * component,struct snd_compr_stream * cstream,int rollback)454 void snd_soc_component_compr_free(struct snd_soc_component *component,
455 struct snd_compr_stream *cstream,
456 int rollback)
457 {
458 if (rollback && !soc_component_mark_match(component, cstream, compr_open))
459 return;
460
461 if (component->driver->compress_ops &&
462 component->driver->compress_ops->free)
463 component->driver->compress_ops->free(component, cstream);
464
465 /* remove marked substream */
466 soc_component_mark_pop(component, compr_open);
467 }
468 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
469
snd_soc_component_compr_trigger(struct snd_compr_stream * cstream,int cmd)470 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
471 {
472 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
473 struct snd_soc_component *component;
474 int i, ret;
475
476 for_each_rtd_components(rtd, i, component) {
477 if (component->driver->compress_ops &&
478 component->driver->compress_ops->trigger) {
479 ret = component->driver->compress_ops->trigger(
480 component, cstream, cmd);
481 if (ret < 0)
482 return soc_component_ret(component, ret);
483 }
484 }
485
486 return 0;
487 }
488 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
489
snd_soc_component_compr_set_params(struct snd_compr_stream * cstream,struct snd_compr_params * params)490 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
491 struct snd_compr_params *params)
492 {
493 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
494 struct snd_soc_component *component;
495 int i, ret;
496
497 for_each_rtd_components(rtd, i, component) {
498 if (component->driver->compress_ops &&
499 component->driver->compress_ops->set_params) {
500 ret = component->driver->compress_ops->set_params(
501 component, cstream, params);
502 if (ret < 0)
503 return soc_component_ret(component, ret);
504 }
505 }
506
507 return 0;
508 }
509 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
510
snd_soc_component_compr_get_params(struct snd_compr_stream * cstream,struct snd_codec * params)511 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
512 struct snd_codec *params)
513 {
514 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
515 struct snd_soc_component *component;
516 int i, ret;
517
518 for_each_rtd_components(rtd, i, component) {
519 if (component->driver->compress_ops &&
520 component->driver->compress_ops->get_params) {
521 ret = component->driver->compress_ops->get_params(
522 component, cstream, params);
523 return soc_component_ret(component, ret);
524 }
525 }
526
527 return 0;
528 }
529 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
530
snd_soc_component_compr_get_caps(struct snd_compr_stream * cstream,struct snd_compr_caps * caps)531 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
532 struct snd_compr_caps *caps)
533 {
534 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
535 struct snd_soc_component *component;
536 int i, ret = 0;
537
538 snd_soc_dpcm_mutex_lock(rtd);
539
540 for_each_rtd_components(rtd, i, component) {
541 if (component->driver->compress_ops &&
542 component->driver->compress_ops->get_caps) {
543 ret = component->driver->compress_ops->get_caps(
544 component, cstream, caps);
545 break;
546 }
547 }
548
549 snd_soc_dpcm_mutex_unlock(rtd);
550
551 return soc_component_ret(component, ret);
552 }
553 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
554
snd_soc_component_compr_get_codec_caps(struct snd_compr_stream * cstream,struct snd_compr_codec_caps * codec)555 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
556 struct snd_compr_codec_caps *codec)
557 {
558 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
559 struct snd_soc_component *component;
560 int i, ret = 0;
561
562 snd_soc_dpcm_mutex_lock(rtd);
563
564 for_each_rtd_components(rtd, i, component) {
565 if (component->driver->compress_ops &&
566 component->driver->compress_ops->get_codec_caps) {
567 ret = component->driver->compress_ops->get_codec_caps(
568 component, cstream, codec);
569 break;
570 }
571 }
572
573 snd_soc_dpcm_mutex_unlock(rtd);
574
575 return soc_component_ret(component, ret);
576 }
577 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
578
snd_soc_component_compr_ack(struct snd_compr_stream * cstream,size_t bytes)579 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
580 {
581 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
582 struct snd_soc_component *component;
583 int i, ret;
584
585 for_each_rtd_components(rtd, i, component) {
586 if (component->driver->compress_ops &&
587 component->driver->compress_ops->ack) {
588 ret = component->driver->compress_ops->ack(
589 component, cstream, bytes);
590 if (ret < 0)
591 return soc_component_ret(component, ret);
592 }
593 }
594
595 return 0;
596 }
597 EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
598
snd_soc_component_compr_pointer(struct snd_compr_stream * cstream,struct snd_compr_tstamp64 * tstamp)599 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
600 struct snd_compr_tstamp64 *tstamp)
601 {
602 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
603 struct snd_soc_component *component;
604 int i, ret;
605
606 for_each_rtd_components(rtd, i, component) {
607 if (component->driver->compress_ops &&
608 component->driver->compress_ops->pointer) {
609 ret = component->driver->compress_ops->pointer(
610 component, cstream, tstamp);
611 return soc_component_ret(component, ret);
612 }
613 }
614
615 return 0;
616 }
617 EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
618
snd_soc_component_compr_copy(struct snd_compr_stream * cstream,char __user * buf,size_t count)619 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
620 char __user *buf, size_t count)
621 {
622 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
623 struct snd_soc_component *component;
624 int i, ret = 0;
625
626 snd_soc_dpcm_mutex_lock(rtd);
627
628 for_each_rtd_components(rtd, i, component) {
629 if (component->driver->compress_ops &&
630 component->driver->compress_ops->copy) {
631 ret = component->driver->compress_ops->copy(
632 component, cstream, buf, count);
633 break;
634 }
635 }
636
637 snd_soc_dpcm_mutex_unlock(rtd);
638
639 return soc_component_ret(component, ret);
640 }
641 EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
642
snd_soc_component_compr_set_metadata(struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)643 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
644 struct snd_compr_metadata *metadata)
645 {
646 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
647 struct snd_soc_component *component;
648 int i, ret;
649
650 for_each_rtd_components(rtd, i, component) {
651 if (component->driver->compress_ops &&
652 component->driver->compress_ops->set_metadata) {
653 ret = component->driver->compress_ops->set_metadata(
654 component, cstream, metadata);
655 if (ret < 0)
656 return soc_component_ret(component, ret);
657 }
658 }
659
660 return 0;
661 }
662 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
663
snd_soc_component_compr_get_metadata(struct snd_compr_stream * cstream,struct snd_compr_metadata * metadata)664 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
665 struct snd_compr_metadata *metadata)
666 {
667 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
668 struct snd_soc_component *component;
669 int i, ret;
670
671 for_each_rtd_components(rtd, i, component) {
672 if (component->driver->compress_ops &&
673 component->driver->compress_ops->get_metadata) {
674 ret = component->driver->compress_ops->get_metadata(
675 component, cstream, metadata);
676 return soc_component_ret(component, ret);
677 }
678 }
679
680 return 0;
681 }
682 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
683
soc_component_read_no_lock(struct snd_soc_component * component,unsigned int reg)684 static unsigned int soc_component_read_no_lock(
685 struct snd_soc_component *component,
686 unsigned int reg)
687 {
688 int ret;
689 unsigned int val = 0;
690
691 if (component->regmap)
692 ret = regmap_read(component->regmap, reg, &val);
693 else if (component->driver->read) {
694 ret = 0;
695 val = component->driver->read(component, reg);
696 }
697 else
698 ret = -EIO;
699
700 if (ret < 0)
701 return soc_component_ret_reg_rw(component, ret, reg);
702
703 return val;
704 }
705
706 /**
707 * snd_soc_component_read() - Read register value
708 * @component: Component to read from
709 * @reg: Register to read
710 *
711 * Return: read value
712 */
snd_soc_component_read(struct snd_soc_component * component,unsigned int reg)713 unsigned int snd_soc_component_read(struct snd_soc_component *component,
714 unsigned int reg)
715 {
716 unsigned int val;
717
718 mutex_lock(&component->io_mutex);
719 val = soc_component_read_no_lock(component, reg);
720 mutex_unlock(&component->io_mutex);
721
722 return val;
723 }
724 EXPORT_SYMBOL_GPL(snd_soc_component_read);
725
soc_component_write_no_lock(struct snd_soc_component * component,unsigned int reg,unsigned int val)726 static int soc_component_write_no_lock(
727 struct snd_soc_component *component,
728 unsigned int reg, unsigned int val)
729 {
730 int ret = -EIO;
731
732 if (component->regmap)
733 ret = regmap_write(component->regmap, reg, val);
734 else if (component->driver->write)
735 ret = component->driver->write(component, reg, val);
736
737 return soc_component_ret_reg_rw(component, ret, reg);
738 }
739
740 /**
741 * snd_soc_component_write() - Write register value
742 * @component: Component to write to
743 * @reg: Register to write
744 * @val: Value to write to the register
745 *
746 * Return: 0 on success, a negative error code otherwise.
747 */
snd_soc_component_write(struct snd_soc_component * component,unsigned int reg,unsigned int val)748 int snd_soc_component_write(struct snd_soc_component *component,
749 unsigned int reg, unsigned int val)
750 {
751 int ret;
752
753 mutex_lock(&component->io_mutex);
754 ret = soc_component_write_no_lock(component, reg, val);
755 mutex_unlock(&component->io_mutex);
756
757 return ret;
758 }
759 EXPORT_SYMBOL_GPL(snd_soc_component_write);
760
snd_soc_component_update_bits_legacy(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int val,bool * change)761 static int snd_soc_component_update_bits_legacy(
762 struct snd_soc_component *component, unsigned int reg,
763 unsigned int mask, unsigned int val, bool *change)
764 {
765 unsigned int old, new;
766 int ret = 0;
767
768 mutex_lock(&component->io_mutex);
769
770 old = soc_component_read_no_lock(component, reg);
771
772 new = (old & ~mask) | (val & mask);
773 *change = old != new;
774 if (*change)
775 ret = soc_component_write_no_lock(component, reg, new);
776
777 mutex_unlock(&component->io_mutex);
778
779 return soc_component_ret_reg_rw(component, ret, reg);
780 }
781
782 /**
783 * snd_soc_component_update_bits() - Perform read/modify/write cycle
784 * @component: Component to update
785 * @reg: Register to update
786 * @mask: Mask that specifies which bits to update
787 * @val: New value for the bits specified by mask
788 *
789 * Return: 1 if the operation was successful and the value of the register
790 * changed, 0 if the operation was successful, but the value did not change.
791 * Returns a negative error code otherwise.
792 */
snd_soc_component_update_bits(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int val)793 int snd_soc_component_update_bits(struct snd_soc_component *component,
794 unsigned int reg, unsigned int mask, unsigned int val)
795 {
796 bool change;
797 int ret;
798
799 if (component->regmap)
800 ret = regmap_update_bits_check(component->regmap, reg, mask,
801 val, &change);
802 else
803 ret = snd_soc_component_update_bits_legacy(component, reg,
804 mask, val, &change);
805
806 if (ret < 0)
807 return soc_component_ret_reg_rw(component, ret, reg);
808 return change;
809 }
810 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
811
812 /**
813 * snd_soc_component_update_bits_async() - Perform asynchronous
814 * read/modify/write cycle
815 * @component: Component to update
816 * @reg: Register to update
817 * @mask: Mask that specifies which bits to update
818 * @val: New value for the bits specified by mask
819 *
820 * This function is similar to snd_soc_component_update_bits(), but the update
821 * operation is scheduled asynchronously. This means it may not be completed
822 * when the function returns. To make sure that all scheduled updates have been
823 * completed snd_soc_component_async_complete() must be called.
824 *
825 * Return: 1 if the operation was successful and the value of the register
826 * changed, 0 if the operation was successful, but the value did not change.
827 * Returns a negative error code otherwise.
828 */
snd_soc_component_update_bits_async(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int val)829 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
830 unsigned int reg, unsigned int mask, unsigned int val)
831 {
832 bool change;
833 int ret;
834
835 if (component->regmap)
836 ret = regmap_update_bits_check_async(component->regmap, reg,
837 mask, val, &change);
838 else
839 ret = snd_soc_component_update_bits_legacy(component, reg,
840 mask, val, &change);
841
842 if (ret < 0)
843 return soc_component_ret_reg_rw(component, ret, reg);
844 return change;
845 }
846 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
847
soc_component_field_shift(struct snd_soc_component * component,unsigned int mask)848 static inline int soc_component_field_shift(struct snd_soc_component *component,
849 unsigned int mask)
850 {
851 if (!mask) {
852 dev_err(component->dev, "ASoC: error field mask is zero for %s\n",
853 component->name);
854 return 0;
855 }
856
857 return (ffs(mask) - 1);
858 }
859
860 /**
861 * snd_soc_component_read_field() - Read register field value
862 * @component: Component to read from
863 * @reg: Register to read
864 * @mask: mask of the register field
865 *
866 * Return: read value of register field.
867 */
snd_soc_component_read_field(struct snd_soc_component * component,unsigned int reg,unsigned int mask)868 unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
869 unsigned int reg, unsigned int mask)
870 {
871 unsigned int val;
872
873 val = snd_soc_component_read(component, reg);
874
875 val = (val & mask) >> soc_component_field_shift(component, mask);
876
877 return val;
878 }
879 EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
880
881 /**
882 * snd_soc_component_write_field() - write to register field
883 * @component: Component to write to
884 * @reg: Register to write
885 * @mask: mask of the register field to update
886 * @val: value of the field to write
887 *
888 * Return: 1 for change, otherwise 0.
889 */
snd_soc_component_write_field(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int val)890 int snd_soc_component_write_field(struct snd_soc_component *component,
891 unsigned int reg, unsigned int mask,
892 unsigned int val)
893 {
894
895 val = (val << soc_component_field_shift(component, mask)) & mask;
896
897 return snd_soc_component_update_bits(component, reg, mask, val);
898 }
899 EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
900
901 /**
902 * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
903 * @component: Component for which to wait
904 *
905 * This function blocks until all asynchronous I/O which has previously been
906 * scheduled using snd_soc_component_update_bits_async() has completed.
907 */
snd_soc_component_async_complete(struct snd_soc_component * component)908 void snd_soc_component_async_complete(struct snd_soc_component *component)
909 {
910 if (component->regmap)
911 regmap_async_complete(component->regmap);
912 }
913 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
914
915 /**
916 * snd_soc_component_test_bits - Test register for change
917 * @component: component
918 * @reg: Register to test
919 * @mask: Mask that specifies which bits to test
920 * @value: Value to test against
921 *
922 * Tests a register with a new value and checks if the new value is
923 * different from the old value.
924 *
925 * Return: 1 for change, otherwise 0.
926 */
snd_soc_component_test_bits(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int value)927 int snd_soc_component_test_bits(struct snd_soc_component *component,
928 unsigned int reg, unsigned int mask, unsigned int value)
929 {
930 unsigned int old, new;
931
932 old = snd_soc_component_read(component, reg);
933 new = (old & ~mask) | value;
934 return old != new;
935 }
936 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
937
snd_soc_pcm_component_pointer(struct snd_pcm_substream * substream)938 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
939 {
940 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
941 struct snd_soc_component *component;
942 int i;
943
944 /* FIXME: use 1st pointer */
945 for_each_rtd_components(rtd, i, component)
946 if (component->driver->pointer)
947 return component->driver->pointer(component, substream);
948
949 return 0;
950 }
951
snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime * rtd,struct snd_soc_component * component)952 static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd,
953 struct snd_soc_component *component)
954 {
955 struct snd_soc_dai *dai;
956 int i;
957
958 for_each_rtd_codec_dais(rtd, i, dai) {
959 if (dai->component == component)
960 return true;
961 }
962
963 return false;
964 }
965
snd_soc_pcm_component_delay(struct snd_pcm_substream * substream,snd_pcm_sframes_t * cpu_delay,snd_pcm_sframes_t * codec_delay)966 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
967 snd_pcm_sframes_t *cpu_delay,
968 snd_pcm_sframes_t *codec_delay)
969 {
970 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
971 struct snd_soc_component *component;
972 snd_pcm_sframes_t delay;
973 int i;
974
975 /*
976 * We're looking for the delay through the full audio path so it needs to
977 * be the maximum of the Components doing transmit and the maximum of the
978 * Components doing receive (ie, all CPUs and all CODECs) rather than
979 * just the maximum of all Components.
980 */
981 for_each_rtd_components(rtd, i, component) {
982 if (!component->driver->delay)
983 continue;
984
985 delay = component->driver->delay(component, substream);
986
987 if (snd_soc_component_is_codec_on_rtd(rtd, component))
988 *codec_delay = max(*codec_delay, delay);
989 else
990 *cpu_delay = max(*cpu_delay, delay);
991 }
992 }
993
snd_soc_pcm_component_ioctl(struct snd_pcm_substream * substream,unsigned int cmd,void * arg)994 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
995 unsigned int cmd, void *arg)
996 {
997 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
998 struct snd_soc_component *component;
999 int i;
1000
1001 /* FIXME: use 1st ioctl */
1002 for_each_rtd_components(rtd, i, component)
1003 if (component->driver->ioctl)
1004 return soc_component_ret(
1005 component,
1006 component->driver->ioctl(component,
1007 substream, cmd, arg));
1008
1009 return snd_pcm_lib_ioctl(substream, cmd, arg);
1010 }
1011
snd_soc_pcm_component_sync_stop(struct snd_pcm_substream * substream)1012 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
1013 {
1014 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1015 struct snd_soc_component *component;
1016 int i, ret;
1017
1018 for_each_rtd_components(rtd, i, component) {
1019 if (component->driver->sync_stop) {
1020 ret = component->driver->sync_stop(component,
1021 substream);
1022 if (ret < 0)
1023 return soc_component_ret(component, ret);
1024 }
1025 }
1026
1027 return 0;
1028 }
1029
snd_soc_pcm_component_copy(struct snd_pcm_substream * substream,int channel,unsigned long pos,struct iov_iter * iter,unsigned long bytes)1030 int snd_soc_pcm_component_copy(struct snd_pcm_substream *substream,
1031 int channel, unsigned long pos,
1032 struct iov_iter *iter, unsigned long bytes)
1033 {
1034 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1035 struct snd_soc_component *component;
1036 int i;
1037
1038 /* FIXME. it returns 1st copy now */
1039 for_each_rtd_components(rtd, i, component)
1040 if (component->driver->copy)
1041 return soc_component_ret(component,
1042 component->driver->copy(component, substream,
1043 channel, pos, iter, bytes));
1044
1045 return -EINVAL;
1046 }
1047
snd_soc_pcm_component_page(struct snd_pcm_substream * substream,unsigned long offset)1048 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
1049 unsigned long offset)
1050 {
1051 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1052 struct snd_soc_component *component;
1053 struct page *page;
1054 int i;
1055
1056 /* FIXME. it returns 1st page now */
1057 for_each_rtd_components(rtd, i, component) {
1058 if (component->driver->page) {
1059 page = component->driver->page(component,
1060 substream, offset);
1061 if (page)
1062 return page;
1063 }
1064 }
1065
1066 return NULL;
1067 }
1068
snd_soc_pcm_component_mmap(struct snd_pcm_substream * substream,struct vm_area_struct * vma)1069 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
1070 struct vm_area_struct *vma)
1071 {
1072 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1073 struct snd_soc_component *component;
1074 int i;
1075
1076 /* FIXME. it returns 1st mmap now */
1077 for_each_rtd_components(rtd, i, component)
1078 if (component->driver->mmap)
1079 return soc_component_ret(
1080 component,
1081 component->driver->mmap(component,
1082 substream, vma));
1083
1084 return -EINVAL;
1085 }
1086
snd_soc_pcm_component_new(struct snd_soc_pcm_runtime * rtd)1087 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
1088 {
1089 struct snd_soc_component *component;
1090 int ret;
1091 int i;
1092
1093 for_each_rtd_components(rtd, i, component) {
1094 if (component->driver->pcm_new) {
1095 ret = component->driver->pcm_new(component, rtd);
1096 if (ret < 0)
1097 return soc_component_ret(component, ret);
1098 }
1099 }
1100
1101 return 0;
1102 }
1103
snd_soc_pcm_component_free(struct snd_soc_pcm_runtime * rtd)1104 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
1105 {
1106 struct snd_soc_component *component;
1107 int i;
1108
1109 if (!rtd->pcm)
1110 return;
1111
1112 for_each_rtd_components(rtd, i, component)
1113 if (component->driver->pcm_free)
1114 component->driver->pcm_free(component, rtd->pcm);
1115 }
1116
snd_soc_pcm_component_prepare(struct snd_pcm_substream * substream)1117 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1118 {
1119 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1120 struct snd_soc_component *component;
1121 int i, ret;
1122
1123 for_each_rtd_components(rtd, i, component) {
1124 if (component->driver->prepare) {
1125 ret = component->driver->prepare(component, substream);
1126 if (ret < 0)
1127 return soc_component_ret(component, ret);
1128 }
1129 }
1130
1131 return 0;
1132 }
1133
snd_soc_pcm_component_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)1134 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
1135 struct snd_pcm_hw_params *params)
1136 {
1137 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1138 struct snd_soc_component *component;
1139 int i, ret;
1140
1141 for_each_rtd_components(rtd, i, component) {
1142 if (component->driver->hw_params) {
1143 ret = component->driver->hw_params(component,
1144 substream, params);
1145 if (ret < 0)
1146 return soc_component_ret(component, ret);
1147 }
1148 /* mark substream if succeeded */
1149 soc_component_mark_push(component, substream, hw_params);
1150 }
1151
1152 return 0;
1153 }
1154
snd_soc_pcm_component_hw_free(struct snd_pcm_substream * substream,int rollback)1155 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
1156 int rollback)
1157 {
1158 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1159 struct snd_soc_component *component;
1160 int i, ret;
1161
1162 for_each_rtd_components(rtd, i, component) {
1163 if (rollback && !soc_component_mark_match(component, substream, hw_params))
1164 continue;
1165
1166 if (component->driver->hw_free) {
1167 ret = component->driver->hw_free(component, substream);
1168 if (ret < 0)
1169 soc_component_ret(component, ret);
1170 }
1171
1172 /* remove marked substream */
1173 soc_component_mark_pop(component, hw_params);
1174 }
1175 }
1176
soc_component_trigger(struct snd_soc_component * component,struct snd_pcm_substream * substream,int cmd)1177 static int soc_component_trigger(struct snd_soc_component *component,
1178 struct snd_pcm_substream *substream,
1179 int cmd)
1180 {
1181 int ret = 0;
1182
1183 if (component->driver->trigger)
1184 ret = component->driver->trigger(component, substream, cmd);
1185
1186 return soc_component_ret(component, ret);
1187 }
1188
snd_soc_pcm_component_trigger(struct snd_pcm_substream * substream,int cmd,int rollback)1189 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
1190 int cmd, int rollback)
1191 {
1192 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1193 struct snd_soc_component *component;
1194 int i, r, ret = 0;
1195
1196 switch (cmd) {
1197 case SNDRV_PCM_TRIGGER_START:
1198 case SNDRV_PCM_TRIGGER_RESUME:
1199 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1200 for_each_rtd_components(rtd, i, component) {
1201 ret = soc_component_trigger(component, substream, cmd);
1202 if (ret < 0)
1203 break;
1204 soc_component_mark_push(component, substream, trigger);
1205 }
1206 break;
1207 case SNDRV_PCM_TRIGGER_STOP:
1208 case SNDRV_PCM_TRIGGER_SUSPEND:
1209 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1210 for_each_rtd_components(rtd, i, component) {
1211 if (rollback && !soc_component_mark_match(component, substream, trigger))
1212 continue;
1213
1214 r = soc_component_trigger(component, substream, cmd);
1215 if (r < 0)
1216 ret = r; /* use last ret */
1217 soc_component_mark_pop(component, trigger);
1218 }
1219 }
1220
1221 return ret;
1222 }
1223
snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime * rtd,void * stream)1224 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1225 void *stream)
1226 {
1227 struct snd_soc_component *component;
1228 int i;
1229
1230 for_each_rtd_components(rtd, i, component) {
1231 int ret = pm_runtime_get_sync(component->dev);
1232 if (ret < 0 && ret != -EACCES) {
1233 pm_runtime_put_noidle(component->dev);
1234 return soc_component_ret(component, ret);
1235 }
1236 /* mark stream if succeeded */
1237 soc_component_mark_push(component, stream, pm);
1238 }
1239
1240 return 0;
1241 }
1242
snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime * rtd,void * stream,int rollback)1243 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1244 void *stream, int rollback)
1245 {
1246 struct snd_soc_component *component;
1247 int i;
1248
1249 for_each_rtd_components(rtd, i, component) {
1250 if (rollback && !soc_component_mark_match(component, stream, pm))
1251 continue;
1252
1253 pm_runtime_put_autosuspend(component->dev);
1254
1255 /* remove marked stream */
1256 soc_component_mark_pop(component, pm);
1257 }
1258 }
1259
snd_soc_pcm_component_ack(struct snd_pcm_substream * substream)1260 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
1261 {
1262 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1263 struct snd_soc_component *component;
1264 int i;
1265
1266 /* FIXME: use 1st pointer */
1267 for_each_rtd_components(rtd, i, component)
1268 if (component->driver->ack)
1269 return component->driver->ack(component, substream);
1270
1271 return 0;
1272 }
1273