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