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