xref: /openbmc/linux/sound/soc/soc-component.c (revision 1edd0337)
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 /**
240  * snd_soc_component_set_jack - configure component jack.
241  * @component: COMPONENTs
242  * @jack: structure to use for the jack
243  * @data: can be used if codec driver need extra data for configuring jack
244  *
245  * Configures and enables jack detection function.
246  */
247 int snd_soc_component_set_jack(struct snd_soc_component *component,
248 			       struct snd_soc_jack *jack, void *data)
249 {
250 	int ret = -ENOTSUPP;
251 
252 	if (component->driver->set_jack)
253 		ret = component->driver->set_jack(component, jack, data);
254 
255 	return soc_component_ret(component, ret);
256 }
257 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
258 
259 /**
260  * snd_soc_component_get_jack_type
261  * @component: COMPONENTs
262  *
263  * Returns the jack type of the component
264  * This can either be the supported type or one read from
265  * devicetree with the property: jack-type.
266  */
267 int snd_soc_component_get_jack_type(
268 	struct snd_soc_component *component)
269 {
270 	int ret = -ENOTSUPP;
271 
272 	if (component->driver->get_jack_type)
273 		ret = component->driver->get_jack_type(component);
274 
275 	return soc_component_ret(component, ret);
276 }
277 EXPORT_SYMBOL_GPL(snd_soc_component_get_jack_type);
278 
279 int snd_soc_component_module_get(struct snd_soc_component *component,
280 				 void *mark, int upon_open)
281 {
282 	int ret = 0;
283 
284 	if (component->driver->module_get_upon_open == !!upon_open &&
285 	    !try_module_get(component->dev->driver->owner))
286 		ret = -ENODEV;
287 
288 	/* mark module if succeeded */
289 	if (ret == 0)
290 		soc_component_mark_push(component, mark, module);
291 
292 	return soc_component_ret(component, ret);
293 }
294 
295 void snd_soc_component_module_put(struct snd_soc_component *component,
296 				  void *mark, int upon_open, int rollback)
297 {
298 	if (rollback && !soc_component_mark_match(component, mark, module))
299 		return;
300 
301 	if (component->driver->module_get_upon_open == !!upon_open)
302 		module_put(component->dev->driver->owner);
303 
304 	/* remove the mark from module */
305 	soc_component_mark_pop(component, mark, module);
306 }
307 
308 int snd_soc_component_open(struct snd_soc_component *component,
309 			   struct snd_pcm_substream *substream)
310 {
311 	int ret = 0;
312 
313 	if (component->driver->open)
314 		ret = component->driver->open(component, substream);
315 
316 	/* mark substream if succeeded */
317 	if (ret == 0)
318 		soc_component_mark_push(component, substream, open);
319 
320 	return soc_component_ret(component, ret);
321 }
322 
323 int snd_soc_component_close(struct snd_soc_component *component,
324 			    struct snd_pcm_substream *substream,
325 			    int rollback)
326 {
327 	int ret = 0;
328 
329 	if (rollback && !soc_component_mark_match(component, substream, open))
330 		return 0;
331 
332 	if (component->driver->close)
333 		ret = component->driver->close(component, substream);
334 
335 	/* remove marked substream */
336 	soc_component_mark_pop(component, substream, open);
337 
338 	return soc_component_ret(component, ret);
339 }
340 
341 void snd_soc_component_suspend(struct snd_soc_component *component)
342 {
343 	if (component->driver->suspend)
344 		component->driver->suspend(component);
345 	component->suspended = 1;
346 }
347 
348 void snd_soc_component_resume(struct snd_soc_component *component)
349 {
350 	if (component->driver->resume)
351 		component->driver->resume(component);
352 	component->suspended = 0;
353 }
354 
355 int snd_soc_component_is_suspended(struct snd_soc_component *component)
356 {
357 	return component->suspended;
358 }
359 
360 int snd_soc_component_probe(struct snd_soc_component *component)
361 {
362 	int ret = 0;
363 
364 	if (component->driver->probe)
365 		ret = component->driver->probe(component);
366 
367 	return soc_component_ret(component, ret);
368 }
369 
370 void snd_soc_component_remove(struct snd_soc_component *component)
371 {
372 	if (component->driver->remove)
373 		component->driver->remove(component);
374 }
375 
376 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
377 				      struct device_node *ep)
378 {
379 	int ret = -ENOTSUPP;
380 
381 	if (component->driver->of_xlate_dai_id)
382 		ret = component->driver->of_xlate_dai_id(component, ep);
383 
384 	return soc_component_ret(component, ret);
385 }
386 
387 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
388 					const struct of_phandle_args *args,
389 					const char **dai_name)
390 {
391 	if (component->driver->of_xlate_dai_name)
392 		return component->driver->of_xlate_dai_name(component,
393 							    args, dai_name);
394 	/*
395 	 * Don't use soc_component_ret here because we may not want to report
396 	 * the error just yet. If a device has more than one component, the
397 	 * first may not match and we don't want spam the log with this.
398 	 */
399 	return -ENOTSUPP;
400 }
401 
402 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
403 {
404 	int val_bytes = regmap_get_val_bytes(component->regmap);
405 
406 	/* Errors are legitimate for non-integer byte multiples */
407 	if (val_bytes > 0)
408 		component->val_bytes = val_bytes;
409 }
410 
411 #ifdef CONFIG_REGMAP
412 
413 /**
414  * snd_soc_component_init_regmap() - Initialize regmap instance for the
415  *                                   component
416  * @component: The component for which to initialize the regmap instance
417  * @regmap: The regmap instance that should be used by the component
418  *
419  * This function allows deferred assignment of the regmap instance that is
420  * associated with the component. Only use this if the regmap instance is not
421  * yet ready when the component is registered. The function must also be called
422  * before the first IO attempt of the component.
423  */
424 void snd_soc_component_init_regmap(struct snd_soc_component *component,
425 				   struct regmap *regmap)
426 {
427 	component->regmap = regmap;
428 	snd_soc_component_setup_regmap(component);
429 }
430 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
431 
432 /**
433  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
434  *                                   component
435  * @component: The component for which to de-initialize the regmap instance
436  *
437  * Calls regmap_exit() on the regmap instance associated to the component and
438  * removes the regmap instance from the component.
439  *
440  * This function should only be used if snd_soc_component_init_regmap() was used
441  * to initialize the regmap instance.
442  */
443 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
444 {
445 	regmap_exit(component->regmap);
446 	component->regmap = NULL;
447 }
448 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
449 
450 #endif
451 
452 int snd_soc_component_compr_open(struct snd_soc_component *component,
453 				 struct snd_compr_stream *cstream)
454 {
455 	int ret = 0;
456 
457 	if (component->driver->compress_ops &&
458 	    component->driver->compress_ops->open)
459 		ret = component->driver->compress_ops->open(component, cstream);
460 
461 	/* mark substream if succeeded */
462 	if (ret == 0)
463 		soc_component_mark_push(component, cstream, compr_open);
464 
465 	return soc_component_ret(component, ret);
466 }
467 EXPORT_SYMBOL_GPL(snd_soc_component_compr_open);
468 
469 void snd_soc_component_compr_free(struct snd_soc_component *component,
470 				  struct snd_compr_stream *cstream,
471 				  int rollback)
472 {
473 	if (rollback && !soc_component_mark_match(component, cstream, compr_open))
474 		return;
475 
476 	if (component->driver->compress_ops &&
477 	    component->driver->compress_ops->free)
478 		component->driver->compress_ops->free(component, cstream);
479 
480 	/* remove marked substream */
481 	soc_component_mark_pop(component, cstream, compr_open);
482 }
483 EXPORT_SYMBOL_GPL(snd_soc_component_compr_free);
484 
485 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd)
486 {
487 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
488 	struct snd_soc_component *component;
489 	int i, ret;
490 
491 	for_each_rtd_components(rtd, i, component) {
492 		if (component->driver->compress_ops &&
493 		    component->driver->compress_ops->trigger) {
494 			ret = component->driver->compress_ops->trigger(
495 				component, cstream, cmd);
496 			if (ret < 0)
497 				return soc_component_ret(component, ret);
498 		}
499 	}
500 
501 	return 0;
502 }
503 EXPORT_SYMBOL_GPL(snd_soc_component_compr_trigger);
504 
505 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
506 				       struct snd_compr_params *params)
507 {
508 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
509 	struct snd_soc_component *component;
510 	int i, ret;
511 
512 	for_each_rtd_components(rtd, i, component) {
513 		if (component->driver->compress_ops &&
514 		    component->driver->compress_ops->set_params) {
515 			ret = component->driver->compress_ops->set_params(
516 				component, cstream, params);
517 			if (ret < 0)
518 				return soc_component_ret(component, ret);
519 		}
520 	}
521 
522 	return 0;
523 }
524 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_params);
525 
526 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
527 				       struct snd_codec *params)
528 {
529 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
530 	struct snd_soc_component *component;
531 	int i, ret;
532 
533 	for_each_rtd_components(rtd, i, component) {
534 		if (component->driver->compress_ops &&
535 		    component->driver->compress_ops->get_params) {
536 			ret = component->driver->compress_ops->get_params(
537 				component, cstream, params);
538 			return soc_component_ret(component, ret);
539 		}
540 	}
541 
542 	return 0;
543 }
544 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_params);
545 
546 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
547 				     struct snd_compr_caps *caps)
548 {
549 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
550 	struct snd_soc_component *component;
551 	int i, ret = 0;
552 
553 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
554 
555 	for_each_rtd_components(rtd, i, component) {
556 		if (component->driver->compress_ops &&
557 		    component->driver->compress_ops->get_caps) {
558 			ret = component->driver->compress_ops->get_caps(
559 				component, cstream, caps);
560 			break;
561 		}
562 	}
563 
564 	mutex_unlock(&rtd->card->pcm_mutex);
565 
566 	return soc_component_ret(component, ret);
567 }
568 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_caps);
569 
570 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
571 					   struct snd_compr_codec_caps *codec)
572 {
573 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
574 	struct snd_soc_component *component;
575 	int i, ret = 0;
576 
577 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
578 
579 	for_each_rtd_components(rtd, i, component) {
580 		if (component->driver->compress_ops &&
581 		    component->driver->compress_ops->get_codec_caps) {
582 			ret = component->driver->compress_ops->get_codec_caps(
583 				component, cstream, codec);
584 			break;
585 		}
586 	}
587 
588 	mutex_unlock(&rtd->card->pcm_mutex);
589 
590 	return soc_component_ret(component, ret);
591 }
592 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_codec_caps);
593 
594 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
595 {
596 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
597 	struct snd_soc_component *component;
598 	int i, ret;
599 
600 	for_each_rtd_components(rtd, i, component) {
601 		if (component->driver->compress_ops &&
602 		    component->driver->compress_ops->ack) {
603 			ret = component->driver->compress_ops->ack(
604 				component, cstream, bytes);
605 			if (ret < 0)
606 				return soc_component_ret(component, ret);
607 		}
608 	}
609 
610 	return 0;
611 }
612 EXPORT_SYMBOL_GPL(snd_soc_component_compr_ack);
613 
614 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
615 				    struct snd_compr_tstamp *tstamp)
616 {
617 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
618 	struct snd_soc_component *component;
619 	int i, ret;
620 
621 	for_each_rtd_components(rtd, i, component) {
622 		if (component->driver->compress_ops &&
623 		    component->driver->compress_ops->pointer) {
624 			ret = component->driver->compress_ops->pointer(
625 				component, cstream, tstamp);
626 			return soc_component_ret(component, ret);
627 		}
628 	}
629 
630 	return 0;
631 }
632 EXPORT_SYMBOL_GPL(snd_soc_component_compr_pointer);
633 
634 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
635 				 char __user *buf, size_t count)
636 {
637 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
638 	struct snd_soc_component *component;
639 	int i, ret = 0;
640 
641 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
642 
643 	for_each_rtd_components(rtd, i, component) {
644 		if (component->driver->compress_ops &&
645 		    component->driver->compress_ops->copy) {
646 			ret = component->driver->compress_ops->copy(
647 				component, cstream, buf, count);
648 			break;
649 		}
650 	}
651 
652 	mutex_unlock(&rtd->card->pcm_mutex);
653 
654 	return soc_component_ret(component, ret);
655 }
656 EXPORT_SYMBOL_GPL(snd_soc_component_compr_copy);
657 
658 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
659 					 struct snd_compr_metadata *metadata)
660 {
661 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
662 	struct snd_soc_component *component;
663 	int i, ret;
664 
665 	for_each_rtd_components(rtd, i, component) {
666 		if (component->driver->compress_ops &&
667 		    component->driver->compress_ops->set_metadata) {
668 			ret = component->driver->compress_ops->set_metadata(
669 				component, cstream, metadata);
670 			if (ret < 0)
671 				return soc_component_ret(component, ret);
672 		}
673 	}
674 
675 	return 0;
676 }
677 EXPORT_SYMBOL_GPL(snd_soc_component_compr_set_metadata);
678 
679 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
680 					 struct snd_compr_metadata *metadata)
681 {
682 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
683 	struct snd_soc_component *component;
684 	int i, ret;
685 
686 	for_each_rtd_components(rtd, i, component) {
687 		if (component->driver->compress_ops &&
688 		    component->driver->compress_ops->get_metadata) {
689 			ret = component->driver->compress_ops->get_metadata(
690 				component, cstream, metadata);
691 			return soc_component_ret(component, ret);
692 		}
693 	}
694 
695 	return 0;
696 }
697 EXPORT_SYMBOL_GPL(snd_soc_component_compr_get_metadata);
698 
699 static unsigned int soc_component_read_no_lock(
700 	struct snd_soc_component *component,
701 	unsigned int reg)
702 {
703 	int ret;
704 	unsigned int val = 0;
705 
706 	if (component->regmap)
707 		ret = regmap_read(component->regmap, reg, &val);
708 	else if (component->driver->read) {
709 		ret = 0;
710 		val = component->driver->read(component, reg);
711 	}
712 	else
713 		ret = -EIO;
714 
715 	if (ret < 0)
716 		return soc_component_ret_reg_rw(component, ret, reg);
717 
718 	return val;
719 }
720 
721 /**
722  * snd_soc_component_read() - Read register value
723  * @component: Component to read from
724  * @reg: Register to read
725  *
726  * Return: read value
727  */
728 unsigned int snd_soc_component_read(struct snd_soc_component *component,
729 				    unsigned int reg)
730 {
731 	unsigned int val;
732 
733 	mutex_lock(&component->io_mutex);
734 	val = soc_component_read_no_lock(component, reg);
735 	mutex_unlock(&component->io_mutex);
736 
737 	return val;
738 }
739 EXPORT_SYMBOL_GPL(snd_soc_component_read);
740 
741 static int soc_component_write_no_lock(
742 	struct snd_soc_component *component,
743 	unsigned int reg, unsigned int val)
744 {
745 	int ret = -EIO;
746 
747 	if (component->regmap)
748 		ret = regmap_write(component->regmap, reg, val);
749 	else if (component->driver->write)
750 		ret = component->driver->write(component, reg, val);
751 
752 	return soc_component_ret_reg_rw(component, ret, reg);
753 }
754 
755 /**
756  * snd_soc_component_write() - Write register value
757  * @component: Component to write to
758  * @reg: Register to write
759  * @val: Value to write to the register
760  *
761  * Return: 0 on success, a negative error code otherwise.
762  */
763 int snd_soc_component_write(struct snd_soc_component *component,
764 			    unsigned int reg, unsigned int val)
765 {
766 	int ret;
767 
768 	mutex_lock(&component->io_mutex);
769 	ret = soc_component_write_no_lock(component, reg, val);
770 	mutex_unlock(&component->io_mutex);
771 
772 	return ret;
773 }
774 EXPORT_SYMBOL_GPL(snd_soc_component_write);
775 
776 static int snd_soc_component_update_bits_legacy(
777 	struct snd_soc_component *component, unsigned int reg,
778 	unsigned int mask, unsigned int val, bool *change)
779 {
780 	unsigned int old, new;
781 	int ret = 0;
782 
783 	mutex_lock(&component->io_mutex);
784 
785 	old = soc_component_read_no_lock(component, reg);
786 
787 	new = (old & ~mask) | (val & mask);
788 	*change = old != new;
789 	if (*change)
790 		ret = soc_component_write_no_lock(component, reg, new);
791 
792 	mutex_unlock(&component->io_mutex);
793 
794 	return soc_component_ret_reg_rw(component, ret, reg);
795 }
796 
797 /**
798  * snd_soc_component_update_bits() - Perform read/modify/write cycle
799  * @component: Component to update
800  * @reg: Register to update
801  * @mask: Mask that specifies which bits to update
802  * @val: New value for the bits specified by mask
803  *
804  * Return: 1 if the operation was successful and the value of the register
805  * changed, 0 if the operation was successful, but the value did not change.
806  * Returns a negative error code otherwise.
807  */
808 int snd_soc_component_update_bits(struct snd_soc_component *component,
809 				  unsigned int reg, unsigned int mask, unsigned int val)
810 {
811 	bool change;
812 	int ret;
813 
814 	if (component->regmap)
815 		ret = regmap_update_bits_check(component->regmap, reg, mask,
816 					       val, &change);
817 	else
818 		ret = snd_soc_component_update_bits_legacy(component, reg,
819 							   mask, val, &change);
820 
821 	if (ret < 0)
822 		return soc_component_ret_reg_rw(component, ret, reg);
823 	return change;
824 }
825 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
826 
827 /**
828  * snd_soc_component_update_bits_async() - Perform asynchronous
829  *  read/modify/write cycle
830  * @component: Component to update
831  * @reg: Register to update
832  * @mask: Mask that specifies which bits to update
833  * @val: New value for the bits specified by mask
834  *
835  * This function is similar to snd_soc_component_update_bits(), but the update
836  * operation is scheduled asynchronously. This means it may not be completed
837  * when the function returns. To make sure that all scheduled updates have been
838  * completed snd_soc_component_async_complete() must be called.
839  *
840  * Return: 1 if the operation was successful and the value of the register
841  * changed, 0 if the operation was successful, but the value did not change.
842  * Returns a negative error code otherwise.
843  */
844 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
845 					unsigned int reg, unsigned int mask, unsigned int val)
846 {
847 	bool change;
848 	int ret;
849 
850 	if (component->regmap)
851 		ret = regmap_update_bits_check_async(component->regmap, reg,
852 						     mask, val, &change);
853 	else
854 		ret = snd_soc_component_update_bits_legacy(component, reg,
855 							   mask, val, &change);
856 
857 	if (ret < 0)
858 		return soc_component_ret_reg_rw(component, ret, reg);
859 	return change;
860 }
861 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
862 
863 /**
864  * snd_soc_component_read_field() - Read register field value
865  * @component: Component to read from
866  * @reg: Register to read
867  * @mask: mask of the register field
868  *
869  * Return: read value of register field.
870  */
871 unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
872 					  unsigned int reg, unsigned int mask)
873 {
874 	unsigned int val;
875 
876 	val = snd_soc_component_read(component, reg);
877 
878 	val = (val & mask) >> soc_component_field_shift(component, mask);
879 
880 	return val;
881 }
882 EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
883 
884 /**
885  * snd_soc_component_write_field() - write to register field
886  * @component: Component to write to
887  * @reg: Register to write
888  * @mask: mask of the register field to update
889  * @val: value of the field to write
890  *
891  * Return: 1 for change, otherwise 0.
892  */
893 int snd_soc_component_write_field(struct snd_soc_component *component,
894 				  unsigned int reg, unsigned int mask,
895 				  unsigned int val)
896 {
897 
898 	val = (val << soc_component_field_shift(component, mask)) & mask;
899 
900 	return snd_soc_component_update_bits(component, reg, mask, val);
901 }
902 EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
903 
904 /**
905  * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
906  * @component: Component for which to wait
907  *
908  * This function blocks until all asynchronous I/O which has previously been
909  * scheduled using snd_soc_component_update_bits_async() has completed.
910  */
911 void snd_soc_component_async_complete(struct snd_soc_component *component)
912 {
913 	if (component->regmap)
914 		regmap_async_complete(component->regmap);
915 }
916 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
917 
918 /**
919  * snd_soc_component_test_bits - Test register for change
920  * @component: component
921  * @reg: Register to test
922  * @mask: Mask that specifies which bits to test
923  * @value: Value to test against
924  *
925  * Tests a register with a new value and checks if the new value is
926  * different from the old value.
927  *
928  * Return: 1 for change, otherwise 0.
929  */
930 int snd_soc_component_test_bits(struct snd_soc_component *component,
931 				unsigned int reg, unsigned int mask, unsigned int value)
932 {
933 	unsigned int old, new;
934 
935 	old = snd_soc_component_read(component, reg);
936 	new = (old & ~mask) | value;
937 	return old != new;
938 }
939 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
940 
941 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
942 {
943 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
944 	struct snd_soc_component *component;
945 	int i;
946 
947 	/* FIXME: use 1st pointer */
948 	for_each_rtd_components(rtd, i, component)
949 		if (component->driver->pointer)
950 			return component->driver->pointer(component, substream);
951 
952 	return 0;
953 }
954 
955 static bool snd_soc_component_is_codec_on_rtd(struct snd_soc_pcm_runtime *rtd,
956 					      struct snd_soc_component *component)
957 {
958 	struct snd_soc_dai *dai;
959 	int i;
960 
961 	for_each_rtd_codec_dais(rtd, i, dai) {
962 		if (dai->component == component)
963 			return true;
964 	}
965 
966 	return false;
967 }
968 
969 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
970 				 snd_pcm_sframes_t *cpu_delay,
971 				 snd_pcm_sframes_t *codec_delay)
972 {
973 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
974 	struct snd_soc_component *component;
975 	snd_pcm_sframes_t delay;
976 	int i;
977 
978 	/*
979 	 * We're looking for the delay through the full audio path so it needs to
980 	 * be the maximum of the Components doing transmit and the maximum of the
981 	 * Components doing receive (ie, all CPUs and all CODECs) rather than
982 	 * just the maximum of all Components.
983 	 */
984 	for_each_rtd_components(rtd, i, component) {
985 		if (!component->driver->delay)
986 			continue;
987 
988 		delay = component->driver->delay(component, substream);
989 
990 		if (snd_soc_component_is_codec_on_rtd(rtd, component))
991 			*codec_delay = max(*codec_delay, delay);
992 		else
993 			*cpu_delay = max(*cpu_delay, delay);
994 	}
995 }
996 
997 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
998 				unsigned int cmd, void *arg)
999 {
1000 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1001 	struct snd_soc_component *component;
1002 	int i;
1003 
1004 	/* FIXME: use 1st ioctl */
1005 	for_each_rtd_components(rtd, i, component)
1006 		if (component->driver->ioctl)
1007 			return soc_component_ret(
1008 				component,
1009 				component->driver->ioctl(component,
1010 							 substream, cmd, arg));
1011 
1012 	return snd_pcm_lib_ioctl(substream, cmd, arg);
1013 }
1014 
1015 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
1016 {
1017 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1018 	struct snd_soc_component *component;
1019 	int i, ret;
1020 
1021 	for_each_rtd_components(rtd, i, component) {
1022 		if (component->driver->sync_stop) {
1023 			ret = component->driver->sync_stop(component,
1024 							   substream);
1025 			if (ret < 0)
1026 				return soc_component_ret(component, ret);
1027 		}
1028 	}
1029 
1030 	return 0;
1031 }
1032 
1033 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
1034 				    int channel, unsigned long pos,
1035 				    void __user *buf, unsigned long bytes)
1036 {
1037 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1038 	struct snd_soc_component *component;
1039 	int i;
1040 
1041 	/* FIXME. it returns 1st copy now */
1042 	for_each_rtd_components(rtd, i, component)
1043 		if (component->driver->copy_user)
1044 			return soc_component_ret(
1045 				component,
1046 				component->driver->copy_user(
1047 					component, substream, channel,
1048 					pos, buf, bytes));
1049 
1050 	return -EINVAL;
1051 }
1052 
1053 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
1054 					unsigned long offset)
1055 {
1056 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1057 	struct snd_soc_component *component;
1058 	struct page *page;
1059 	int i;
1060 
1061 	/* FIXME. it returns 1st page now */
1062 	for_each_rtd_components(rtd, i, component) {
1063 		if (component->driver->page) {
1064 			page = component->driver->page(component,
1065 						       substream, offset);
1066 			if (page)
1067 				return page;
1068 		}
1069 	}
1070 
1071 	return NULL;
1072 }
1073 
1074 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
1075 			       struct vm_area_struct *vma)
1076 {
1077 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1078 	struct snd_soc_component *component;
1079 	int i;
1080 
1081 	/* FIXME. it returns 1st mmap now */
1082 	for_each_rtd_components(rtd, i, component)
1083 		if (component->driver->mmap)
1084 			return soc_component_ret(
1085 				component,
1086 				component->driver->mmap(component,
1087 							substream, vma));
1088 
1089 	return -EINVAL;
1090 }
1091 
1092 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
1093 {
1094 	struct snd_soc_component *component;
1095 	int ret;
1096 	int i;
1097 
1098 	for_each_rtd_components(rtd, i, component) {
1099 		if (component->driver->pcm_construct) {
1100 			ret = component->driver->pcm_construct(component, rtd);
1101 			if (ret < 0)
1102 				return soc_component_ret(component, ret);
1103 		}
1104 	}
1105 
1106 	return 0;
1107 }
1108 
1109 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
1110 {
1111 	struct snd_soc_component *component;
1112 	int i;
1113 
1114 	if (!rtd->pcm)
1115 		return;
1116 
1117 	for_each_rtd_components(rtd, i, component)
1118 		if (component->driver->pcm_destruct)
1119 			component->driver->pcm_destruct(component, rtd->pcm);
1120 }
1121 
1122 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
1123 {
1124 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1125 	struct snd_soc_component *component;
1126 	int i, ret;
1127 
1128 	for_each_rtd_components(rtd, i, component) {
1129 		if (component->driver->prepare) {
1130 			ret = component->driver->prepare(component, substream);
1131 			if (ret < 0)
1132 				return soc_component_ret(component, ret);
1133 		}
1134 	}
1135 
1136 	return 0;
1137 }
1138 
1139 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
1140 				    struct snd_pcm_hw_params *params)
1141 {
1142 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1143 	struct snd_soc_component *component;
1144 	int i, ret;
1145 
1146 	for_each_rtd_components(rtd, i, component) {
1147 		if (component->driver->hw_params) {
1148 			ret = component->driver->hw_params(component,
1149 							   substream, params);
1150 			if (ret < 0)
1151 				return soc_component_ret(component, ret);
1152 		}
1153 		/* mark substream if succeeded */
1154 		soc_component_mark_push(component, substream, hw_params);
1155 	}
1156 
1157 	return 0;
1158 }
1159 
1160 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
1161 				   int rollback)
1162 {
1163 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1164 	struct snd_soc_component *component;
1165 	int i, ret;
1166 
1167 	for_each_rtd_components(rtd, i, component) {
1168 		if (rollback && !soc_component_mark_match(component, substream, hw_params))
1169 			continue;
1170 
1171 		if (component->driver->hw_free) {
1172 			ret = component->driver->hw_free(component, substream);
1173 			if (ret < 0)
1174 				soc_component_ret(component, ret);
1175 		}
1176 
1177 		/* remove marked substream */
1178 		soc_component_mark_pop(component, substream, hw_params);
1179 	}
1180 }
1181 
1182 static int soc_component_trigger(struct snd_soc_component *component,
1183 				 struct snd_pcm_substream *substream,
1184 				 int cmd)
1185 {
1186 	int ret = 0;
1187 
1188 	if (component->driver->trigger)
1189 		ret = component->driver->trigger(component, substream, cmd);
1190 
1191 	return soc_component_ret(component, ret);
1192 }
1193 
1194 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
1195 				  int cmd, int rollback)
1196 {
1197 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1198 	struct snd_soc_component *component;
1199 	int i, r, ret = 0;
1200 
1201 	switch (cmd) {
1202 	case SNDRV_PCM_TRIGGER_START:
1203 	case SNDRV_PCM_TRIGGER_RESUME:
1204 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1205 		for_each_rtd_components(rtd, i, component) {
1206 			ret = soc_component_trigger(component, substream, cmd);
1207 			if (ret < 0)
1208 				break;
1209 			soc_component_mark_push(component, substream, trigger);
1210 		}
1211 		break;
1212 	case SNDRV_PCM_TRIGGER_STOP:
1213 	case SNDRV_PCM_TRIGGER_SUSPEND:
1214 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1215 		for_each_rtd_components(rtd, i, component) {
1216 			if (rollback && !soc_component_mark_match(component, substream, trigger))
1217 				continue;
1218 
1219 			r = soc_component_trigger(component, substream, cmd);
1220 			if (r < 0)
1221 				ret = r; /* use last ret */
1222 			soc_component_mark_pop(component, substream, trigger);
1223 		}
1224 	}
1225 
1226 	return ret;
1227 }
1228 
1229 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
1230 					 void *stream)
1231 {
1232 	struct snd_soc_component *component;
1233 	int i;
1234 
1235 	for_each_rtd_components(rtd, i, component) {
1236 		int ret = pm_runtime_get_sync(component->dev);
1237 		if (ret < 0 && ret != -EACCES) {
1238 			pm_runtime_put_noidle(component->dev);
1239 			return soc_component_ret(component, ret);
1240 		}
1241 		/* mark stream if succeeded */
1242 		soc_component_mark_push(component, stream, pm);
1243 	}
1244 
1245 	return 0;
1246 }
1247 
1248 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
1249 					  void *stream, int rollback)
1250 {
1251 	struct snd_soc_component *component;
1252 	int i;
1253 
1254 	for_each_rtd_components(rtd, i, component) {
1255 		if (rollback && !soc_component_mark_match(component, stream, pm))
1256 			continue;
1257 
1258 		pm_runtime_mark_last_busy(component->dev);
1259 		pm_runtime_put_autosuspend(component->dev);
1260 
1261 		/* remove marked stream */
1262 		soc_component_mark_pop(component, stream, pm);
1263 	}
1264 }
1265 
1266 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
1267 {
1268 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1269 	struct snd_soc_component *component;
1270 	int i;
1271 
1272 	/* FIXME: use 1st pointer */
1273 	for_each_rtd_components(rtd, i, component)
1274 		if (component->driver->ack)
1275 			return component->driver->ack(component, substream);
1276 
1277 	return 0;
1278 }
1279