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