• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
_soc_component_ret(struct snd_soc_component * component,const char * func,int ret)16 static inline int _soc_component_ret(struct snd_soc_component *component,
17 				     const char *func, int ret)
18 {
19 	/* Positive/Zero values are not errors */
20 	if (ret >= 0)
21 		return ret;
22 
23 	/* Negative values might be errors */
24 	switch (ret) {
25 	case -EPROBE_DEFER:
26 	case -ENOTSUPP:
27 		break;
28 	default:
29 		dev_err(component->dev,
30 			"ASoC: error at %s on %s: %d\n",
31 			func, component->name, ret);
32 	}
33 
34 	return ret;
35 }
36 
37 /*
38  * We might want to check substream by using list.
39  * In such case, we can update these macros.
40  */
41 #define soc_component_mark_push(component, substream, tgt)	((component)->mark_##tgt = substream)
42 #define soc_component_mark_pop(component, substream, tgt)	((component)->mark_##tgt = NULL)
43 #define soc_component_mark_match(component, substream, tgt)	((component)->mark_##tgt == substream)
44 
snd_soc_component_set_aux(struct snd_soc_component * component,struct snd_soc_aux_dev * aux)45 void snd_soc_component_set_aux(struct snd_soc_component *component,
46 			       struct snd_soc_aux_dev *aux)
47 {
48 	component->init = (aux) ? aux->init : NULL;
49 }
50 
snd_soc_component_init(struct snd_soc_component * component)51 int snd_soc_component_init(struct snd_soc_component *component)
52 {
53 	int ret = 0;
54 
55 	if (component->init)
56 		ret = component->init(component);
57 
58 	return soc_component_ret(component, ret);
59 }
60 
61 /**
62  * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
63  * @component: COMPONENT
64  * @clk_id: DAI specific clock ID
65  * @source: Source for the clock
66  * @freq: new clock frequency in Hz
67  * @dir: new clock direction - input/output.
68  *
69  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
70  */
snd_soc_component_set_sysclk(struct snd_soc_component * component,int clk_id,int source,unsigned int freq,int dir)71 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
72 				 int clk_id, int source, unsigned int freq,
73 				 int dir)
74 {
75 	int ret = -ENOTSUPP;
76 
77 	if (component->driver->set_sysclk)
78 		ret = component->driver->set_sysclk(component, clk_id, source,
79 						     freq, dir);
80 
81 	return soc_component_ret(component, ret);
82 }
83 EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);
84 
85 /*
86  * snd_soc_component_set_pll - configure component PLL.
87  * @component: COMPONENT
88  * @pll_id: DAI specific PLL ID
89  * @source: DAI specific source for the PLL
90  * @freq_in: PLL input clock frequency in Hz
91  * @freq_out: requested PLL output clock frequency in Hz
92  *
93  * Configures and enables PLL to generate output clock based on input clock.
94  */
snd_soc_component_set_pll(struct snd_soc_component * component,int pll_id,int source,unsigned int freq_in,unsigned int freq_out)95 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
96 			      int source, unsigned int freq_in,
97 			      unsigned int freq_out)
98 {
99 	int ret = -EINVAL;
100 
101 	if (component->driver->set_pll)
102 		ret = component->driver->set_pll(component, pll_id, source,
103 						  freq_in, freq_out);
104 
105 	return soc_component_ret(component, ret);
106 }
107 EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);
108 
snd_soc_component_seq_notifier(struct snd_soc_component * component,enum snd_soc_dapm_type type,int subseq)109 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
110 				    enum snd_soc_dapm_type type, int subseq)
111 {
112 	if (component->driver->seq_notifier)
113 		component->driver->seq_notifier(component, type, subseq);
114 }
115 
snd_soc_component_stream_event(struct snd_soc_component * component,int event)116 int snd_soc_component_stream_event(struct snd_soc_component *component,
117 				   int event)
118 {
119 	int ret = 0;
120 
121 	if (component->driver->stream_event)
122 		ret = component->driver->stream_event(component, event);
123 
124 	return soc_component_ret(component, ret);
125 }
126 
snd_soc_component_set_bias_level(struct snd_soc_component * component,enum snd_soc_bias_level level)127 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
128 				     enum snd_soc_bias_level level)
129 {
130 	int ret = 0;
131 
132 	if (component->driver->set_bias_level)
133 		ret = component->driver->set_bias_level(component, level);
134 
135 	return soc_component_ret(component, ret);
136 }
137 
snd_soc_component_enable_pin(struct snd_soc_component * component,const char * pin)138 int snd_soc_component_enable_pin(struct snd_soc_component *component,
139 				 const char *pin)
140 {
141 	struct snd_soc_dapm_context *dapm =
142 		snd_soc_component_get_dapm(component);
143 	return snd_soc_dapm_enable_pin(dapm, pin);
144 }
145 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
146 
snd_soc_component_enable_pin_unlocked(struct snd_soc_component * component,const char * pin)147 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
148 					  const char *pin)
149 {
150 	struct snd_soc_dapm_context *dapm =
151 		snd_soc_component_get_dapm(component);
152 	return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
153 }
154 EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
155 
snd_soc_component_disable_pin(struct snd_soc_component * component,const char * pin)156 int snd_soc_component_disable_pin(struct snd_soc_component *component,
157 				  const char *pin)
158 {
159 	struct snd_soc_dapm_context *dapm =
160 		snd_soc_component_get_dapm(component);
161 	return snd_soc_dapm_disable_pin(dapm, pin);
162 }
163 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
164 
snd_soc_component_disable_pin_unlocked(struct snd_soc_component * component,const char * pin)165 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
166 					   const char *pin)
167 {
168 	struct snd_soc_dapm_context *dapm =
169 		snd_soc_component_get_dapm(component);
170 	return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
171 }
172 EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
173 
snd_soc_component_nc_pin(struct snd_soc_component * component,const char * pin)174 int snd_soc_component_nc_pin(struct snd_soc_component *component,
175 			     const char *pin)
176 {
177 	struct snd_soc_dapm_context *dapm =
178 		snd_soc_component_get_dapm(component);
179 	return snd_soc_dapm_nc_pin(dapm, pin);
180 }
181 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
182 
snd_soc_component_nc_pin_unlocked(struct snd_soc_component * component,const char * pin)183 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
184 				      const char *pin)
185 {
186 	struct snd_soc_dapm_context *dapm =
187 		snd_soc_component_get_dapm(component);
188 	return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
189 }
190 EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
191 
snd_soc_component_get_pin_status(struct snd_soc_component * component,const char * pin)192 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
193 				     const char *pin)
194 {
195 	struct snd_soc_dapm_context *dapm =
196 		snd_soc_component_get_dapm(component);
197 	return snd_soc_dapm_get_pin_status(dapm, pin);
198 }
199 EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
200 
snd_soc_component_force_enable_pin(struct snd_soc_component * component,const char * pin)201 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
202 				       const char *pin)
203 {
204 	struct snd_soc_dapm_context *dapm =
205 		snd_soc_component_get_dapm(component);
206 	return snd_soc_dapm_force_enable_pin(dapm, pin);
207 }
208 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
209 
snd_soc_component_force_enable_pin_unlocked(struct snd_soc_component * component,const char * pin)210 int snd_soc_component_force_enable_pin_unlocked(
211 	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_force_enable_pin_unlocked(dapm, pin);
217 }
218 EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
219 
220 /**
221  * snd_soc_component_set_jack - configure component jack.
222  * @component: COMPONENTs
223  * @jack: structure to use for the jack
224  * @data: can be used if codec driver need extra data for configuring jack
225  *
226  * Configures and enables jack detection function.
227  */
snd_soc_component_set_jack(struct snd_soc_component * component,struct snd_soc_jack * jack,void * data)228 int snd_soc_component_set_jack(struct snd_soc_component *component,
229 			       struct snd_soc_jack *jack, void *data)
230 {
231 	int ret = -ENOTSUPP;
232 
233 	if (component->driver->set_jack)
234 		ret = component->driver->set_jack(component, jack, data);
235 
236 	return soc_component_ret(component, ret);
237 }
238 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
239 
snd_soc_component_module_get(struct snd_soc_component * component,struct snd_pcm_substream * substream,int upon_open)240 int snd_soc_component_module_get(struct snd_soc_component *component,
241 				 struct snd_pcm_substream *substream,
242 				 int upon_open)
243 {
244 	int ret = 0;
245 
246 	if (component->driver->module_get_upon_open == !!upon_open &&
247 	    !try_module_get(component->dev->driver->owner))
248 		ret = -ENODEV;
249 
250 	/* mark substream if succeeded */
251 	if (ret == 0)
252 		soc_component_mark_push(component, substream, module);
253 
254 	return soc_component_ret(component, ret);
255 }
256 
snd_soc_component_module_put(struct snd_soc_component * component,struct snd_pcm_substream * substream,int upon_open,int rollback)257 void snd_soc_component_module_put(struct snd_soc_component *component,
258 				  struct snd_pcm_substream *substream,
259 				  int upon_open, int rollback)
260 {
261 	if (rollback && !soc_component_mark_match(component, substream, module))
262 		return;
263 
264 	if (component->driver->module_get_upon_open == !!upon_open)
265 		module_put(component->dev->driver->owner);
266 
267 	/* remove marked substream */
268 	soc_component_mark_pop(component, substream, module);
269 }
270 
snd_soc_component_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)271 int snd_soc_component_open(struct snd_soc_component *component,
272 			   struct snd_pcm_substream *substream)
273 {
274 	int ret = 0;
275 
276 	if (component->driver->open)
277 		ret = component->driver->open(component, substream);
278 
279 	/* mark substream if succeeded */
280 	if (ret == 0)
281 		soc_component_mark_push(component, substream, open);
282 
283 	return soc_component_ret(component, ret);
284 }
285 
snd_soc_component_close(struct snd_soc_component * component,struct snd_pcm_substream * substream,int rollback)286 int snd_soc_component_close(struct snd_soc_component *component,
287 			    struct snd_pcm_substream *substream,
288 			    int rollback)
289 {
290 	int ret = 0;
291 
292 	if (rollback && !soc_component_mark_match(component, substream, open))
293 		return 0;
294 
295 	if (component->driver->close)
296 		ret = component->driver->close(component, substream);
297 
298 	/* remove marked substream */
299 	soc_component_mark_pop(component, substream, open);
300 
301 	return soc_component_ret(component, ret);
302 }
303 
snd_soc_component_suspend(struct snd_soc_component * component)304 void snd_soc_component_suspend(struct snd_soc_component *component)
305 {
306 	if (component->driver->suspend)
307 		component->driver->suspend(component);
308 	component->suspended = 1;
309 }
310 
snd_soc_component_resume(struct snd_soc_component * component)311 void snd_soc_component_resume(struct snd_soc_component *component)
312 {
313 	if (component->driver->resume)
314 		component->driver->resume(component);
315 	component->suspended = 0;
316 }
317 
snd_soc_component_is_suspended(struct snd_soc_component * component)318 int snd_soc_component_is_suspended(struct snd_soc_component *component)
319 {
320 	return component->suspended;
321 }
322 
snd_soc_component_probe(struct snd_soc_component * component)323 int snd_soc_component_probe(struct snd_soc_component *component)
324 {
325 	int ret = 0;
326 
327 	if (component->driver->probe)
328 		ret = component->driver->probe(component);
329 
330 	return soc_component_ret(component, ret);
331 }
332 
snd_soc_component_remove(struct snd_soc_component * component)333 void snd_soc_component_remove(struct snd_soc_component *component)
334 {
335 	if (component->driver->remove)
336 		component->driver->remove(component);
337 }
338 
snd_soc_component_of_xlate_dai_id(struct snd_soc_component * component,struct device_node * ep)339 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
340 				      struct device_node *ep)
341 {
342 	int ret = -ENOTSUPP;
343 
344 	if (component->driver->of_xlate_dai_id)
345 		ret = component->driver->of_xlate_dai_id(component, ep);
346 
347 	return soc_component_ret(component, ret);
348 }
349 
snd_soc_component_of_xlate_dai_name(struct snd_soc_component * component,struct of_phandle_args * args,const char ** dai_name)350 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
351 					struct of_phandle_args *args,
352 					const char **dai_name)
353 {
354 	if (component->driver->of_xlate_dai_name)
355 		return component->driver->of_xlate_dai_name(component,
356 							    args, dai_name);
357 	/*
358 	 * Don't use soc_component_ret here because we may not want to report
359 	 * the error just yet. If a device has more than one component, the
360 	 * first may not match and we don't want spam the log with this.
361 	 */
362 	return -ENOTSUPP;
363 }
364 
snd_soc_component_setup_regmap(struct snd_soc_component * component)365 void snd_soc_component_setup_regmap(struct snd_soc_component *component)
366 {
367 	int val_bytes = regmap_get_val_bytes(component->regmap);
368 
369 	/* Errors are legitimate for non-integer byte multiples */
370 	if (val_bytes > 0)
371 		component->val_bytes = val_bytes;
372 }
373 
374 #ifdef CONFIG_REGMAP
375 
376 /**
377  * snd_soc_component_init_regmap() - Initialize regmap instance for the
378  *                                   component
379  * @component: The component for which to initialize the regmap instance
380  * @regmap: The regmap instance that should be used by the component
381  *
382  * This function allows deferred assignment of the regmap instance that is
383  * associated with the component. Only use this if the regmap instance is not
384  * yet ready when the component is registered. The function must also be called
385  * before the first IO attempt of the component.
386  */
snd_soc_component_init_regmap(struct snd_soc_component * component,struct regmap * regmap)387 void snd_soc_component_init_regmap(struct snd_soc_component *component,
388 				   struct regmap *regmap)
389 {
390 	component->regmap = regmap;
391 	snd_soc_component_setup_regmap(component);
392 }
393 EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);
394 
395 /**
396  * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
397  *                                   component
398  * @component: The component for which to de-initialize the regmap instance
399  *
400  * Calls regmap_exit() on the regmap instance associated to the component and
401  * removes the regmap instance from the component.
402  *
403  * This function should only be used if snd_soc_component_init_regmap() was used
404  * to initialize the regmap instance.
405  */
snd_soc_component_exit_regmap(struct snd_soc_component * component)406 void snd_soc_component_exit_regmap(struct snd_soc_component *component)
407 {
408 	regmap_exit(component->regmap);
409 	component->regmap = NULL;
410 }
411 EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);
412 
413 #endif
414 
soc_component_read_no_lock(struct snd_soc_component * component,unsigned int reg)415 static unsigned int soc_component_read_no_lock(
416 	struct snd_soc_component *component,
417 	unsigned int reg)
418 {
419 	int ret;
420 	unsigned int val = 0;
421 
422 	if (component->regmap)
423 		ret = regmap_read(component->regmap, reg, &val);
424 	else if (component->driver->read) {
425 		ret = 0;
426 		val = component->driver->read(component, reg);
427 	}
428 	else
429 		ret = -EIO;
430 
431 	if (ret < 0)
432 		return soc_component_ret(component, ret);
433 
434 	return val;
435 }
436 
437 /**
438  * snd_soc_component_read() - Read register value
439  * @component: Component to read from
440  * @reg: Register to read
441  *
442  * Return: read value
443  */
snd_soc_component_read(struct snd_soc_component * component,unsigned int reg)444 unsigned int snd_soc_component_read(struct snd_soc_component *component,
445 				    unsigned int reg)
446 {
447 	unsigned int val;
448 
449 	mutex_lock(&component->io_mutex);
450 	val = soc_component_read_no_lock(component, reg);
451 	mutex_unlock(&component->io_mutex);
452 
453 	return val;
454 }
455 EXPORT_SYMBOL_GPL(snd_soc_component_read);
456 
soc_component_write_no_lock(struct snd_soc_component * component,unsigned int reg,unsigned int val)457 static int soc_component_write_no_lock(
458 	struct snd_soc_component *component,
459 	unsigned int reg, unsigned int val)
460 {
461 	int ret = -EIO;
462 
463 	if (component->regmap)
464 		ret = regmap_write(component->regmap, reg, val);
465 	else if (component->driver->write)
466 		ret = component->driver->write(component, reg, val);
467 
468 	return soc_component_ret(component, ret);
469 }
470 
471 /**
472  * snd_soc_component_write() - Write register value
473  * @component: Component to write to
474  * @reg: Register to write
475  * @val: Value to write to the register
476  *
477  * Return: 0 on success, a negative error code otherwise.
478  */
snd_soc_component_write(struct snd_soc_component * component,unsigned int reg,unsigned int val)479 int snd_soc_component_write(struct snd_soc_component *component,
480 			    unsigned int reg, unsigned int val)
481 {
482 	int ret;
483 
484 	mutex_lock(&component->io_mutex);
485 	ret = soc_component_write_no_lock(component, reg, val);
486 	mutex_unlock(&component->io_mutex);
487 
488 	return ret;
489 }
490 EXPORT_SYMBOL_GPL(snd_soc_component_write);
491 
snd_soc_component_update_bits_legacy(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int val,bool * change)492 static int snd_soc_component_update_bits_legacy(
493 	struct snd_soc_component *component, unsigned int reg,
494 	unsigned int mask, unsigned int val, bool *change)
495 {
496 	unsigned int old, new;
497 	int ret = 0;
498 
499 	mutex_lock(&component->io_mutex);
500 
501 	old = soc_component_read_no_lock(component, reg);
502 
503 	new = (old & ~mask) | (val & mask);
504 	*change = old != new;
505 	if (*change)
506 		ret = soc_component_write_no_lock(component, reg, new);
507 
508 	mutex_unlock(&component->io_mutex);
509 
510 	return soc_component_ret(component, ret);
511 }
512 
513 /**
514  * snd_soc_component_update_bits() - Perform read/modify/write cycle
515  * @component: Component to update
516  * @reg: Register to update
517  * @mask: Mask that specifies which bits to update
518  * @val: New value for the bits specified by mask
519  *
520  * Return: 1 if the operation was successful and the value of the register
521  * changed, 0 if the operation was successful, but the value did not change.
522  * Returns a negative error code otherwise.
523  */
snd_soc_component_update_bits(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int val)524 int snd_soc_component_update_bits(struct snd_soc_component *component,
525 				  unsigned int reg, unsigned int mask, unsigned int val)
526 {
527 	bool change;
528 	int ret;
529 
530 	if (component->regmap)
531 		ret = regmap_update_bits_check(component->regmap, reg, mask,
532 					       val, &change);
533 	else
534 		ret = snd_soc_component_update_bits_legacy(component, reg,
535 							   mask, val, &change);
536 
537 	if (ret < 0)
538 		return soc_component_ret(component, ret);
539 	return change;
540 }
541 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits);
542 
543 /**
544  * snd_soc_component_update_bits_async() - Perform asynchronous
545  *  read/modify/write cycle
546  * @component: Component to update
547  * @reg: Register to update
548  * @mask: Mask that specifies which bits to update
549  * @val: New value for the bits specified by mask
550  *
551  * This function is similar to snd_soc_component_update_bits(), but the update
552  * operation is scheduled asynchronously. This means it may not be completed
553  * when the function returns. To make sure that all scheduled updates have been
554  * completed snd_soc_component_async_complete() must be called.
555  *
556  * Return: 1 if the operation was successful and the value of the register
557  * changed, 0 if the operation was successful, but the value did not change.
558  * Returns a negative error code otherwise.
559  */
snd_soc_component_update_bits_async(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int val)560 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
561 					unsigned int reg, unsigned int mask, unsigned int val)
562 {
563 	bool change;
564 	int ret;
565 
566 	if (component->regmap)
567 		ret = regmap_update_bits_check_async(component->regmap, reg,
568 						     mask, val, &change);
569 	else
570 		ret = snd_soc_component_update_bits_legacy(component, reg,
571 							   mask, val, &change);
572 
573 	if (ret < 0)
574 		return soc_component_ret(component, ret);
575 	return change;
576 }
577 EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);
578 
579 /**
580  * snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
581  * @component: Component for which to wait
582  *
583  * This function blocks until all asynchronous I/O which has previously been
584  * scheduled using snd_soc_component_update_bits_async() has completed.
585  */
snd_soc_component_async_complete(struct snd_soc_component * component)586 void snd_soc_component_async_complete(struct snd_soc_component *component)
587 {
588 	if (component->regmap)
589 		regmap_async_complete(component->regmap);
590 }
591 EXPORT_SYMBOL_GPL(snd_soc_component_async_complete);
592 
593 /**
594  * snd_soc_component_test_bits - Test register for change
595  * @component: component
596  * @reg: Register to test
597  * @mask: Mask that specifies which bits to test
598  * @value: Value to test against
599  *
600  * Tests a register with a new value and checks if the new value is
601  * different from the old value.
602  *
603  * Return: 1 for change, otherwise 0.
604  */
snd_soc_component_test_bits(struct snd_soc_component * component,unsigned int reg,unsigned int mask,unsigned int value)605 int snd_soc_component_test_bits(struct snd_soc_component *component,
606 				unsigned int reg, unsigned int mask, unsigned int value)
607 {
608 	unsigned int old, new;
609 
610 	old = snd_soc_component_read(component, reg);
611 	new = (old & ~mask) | value;
612 	return old != new;
613 }
614 EXPORT_SYMBOL_GPL(snd_soc_component_test_bits);
615 
snd_soc_pcm_component_pointer(struct snd_pcm_substream * substream)616 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
617 {
618 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
619 	struct snd_soc_component *component;
620 	int i;
621 
622 	/* FIXME: use 1st pointer */
623 	for_each_rtd_components(rtd, i, component)
624 		if (component->driver->pointer)
625 			return component->driver->pointer(component, substream);
626 
627 	return 0;
628 }
629 
snd_soc_pcm_component_ioctl(struct snd_pcm_substream * substream,unsigned int cmd,void * arg)630 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
631 				unsigned int cmd, void *arg)
632 {
633 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
634 	struct snd_soc_component *component;
635 	int i;
636 
637 	/* FIXME: use 1st ioctl */
638 	for_each_rtd_components(rtd, i, component)
639 		if (component->driver->ioctl)
640 			return soc_component_ret(
641 				component,
642 				component->driver->ioctl(component,
643 							 substream, cmd, arg));
644 
645 	return snd_pcm_lib_ioctl(substream, cmd, arg);
646 }
647 
snd_soc_pcm_component_sync_stop(struct snd_pcm_substream * substream)648 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
649 {
650 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
651 	struct snd_soc_component *component;
652 	int i, ret;
653 
654 	for_each_rtd_components(rtd, i, component) {
655 		if (component->driver->sync_stop) {
656 			ret = component->driver->sync_stop(component,
657 							   substream);
658 			if (ret < 0)
659 				return soc_component_ret(component, ret);
660 		}
661 	}
662 
663 	return 0;
664 }
665 
snd_soc_pcm_component_copy_user(struct snd_pcm_substream * substream,int channel,unsigned long pos,void __user * buf,unsigned long bytes)666 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
667 				    int channel, unsigned long pos,
668 				    void __user *buf, unsigned long bytes)
669 {
670 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
671 	struct snd_soc_component *component;
672 	int i;
673 
674 	/* FIXME. it returns 1st copy now */
675 	for_each_rtd_components(rtd, i, component)
676 		if (component->driver->copy_user)
677 			return soc_component_ret(
678 				component,
679 				component->driver->copy_user(
680 					component, substream, channel,
681 					pos, buf, bytes));
682 
683 	return -EINVAL;
684 }
685 
snd_soc_pcm_component_page(struct snd_pcm_substream * substream,unsigned long offset)686 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
687 					unsigned long offset)
688 {
689 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
690 	struct snd_soc_component *component;
691 	struct page *page;
692 	int i;
693 
694 	/* FIXME. it returns 1st page now */
695 	for_each_rtd_components(rtd, i, component) {
696 		if (component->driver->page) {
697 			page = component->driver->page(component,
698 						       substream, offset);
699 			if (page)
700 				return page;
701 		}
702 	}
703 
704 	return NULL;
705 }
706 
snd_soc_pcm_component_mmap(struct snd_pcm_substream * substream,struct vm_area_struct * vma)707 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
708 			       struct vm_area_struct *vma)
709 {
710 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
711 	struct snd_soc_component *component;
712 	int i;
713 
714 	/* FIXME. it returns 1st mmap now */
715 	for_each_rtd_components(rtd, i, component)
716 		if (component->driver->mmap)
717 			return soc_component_ret(
718 				component,
719 				component->driver->mmap(component,
720 							substream, vma));
721 
722 	return -EINVAL;
723 }
724 
snd_soc_pcm_component_new(struct snd_soc_pcm_runtime * rtd)725 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
726 {
727 	struct snd_soc_component *component;
728 	int ret;
729 	int i;
730 
731 	for_each_rtd_components(rtd, i, component) {
732 		if (component->driver->pcm_construct) {
733 			ret = component->driver->pcm_construct(component, rtd);
734 			if (ret < 0)
735 				return soc_component_ret(component, ret);
736 		}
737 	}
738 
739 	return 0;
740 }
741 
snd_soc_pcm_component_free(struct snd_soc_pcm_runtime * rtd)742 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
743 {
744 	struct snd_soc_component *component;
745 	int i;
746 
747 	if (!rtd->pcm)
748 		return;
749 
750 	for_each_rtd_components(rtd, i, component)
751 		if (component->driver->pcm_destruct)
752 			component->driver->pcm_destruct(component, rtd->pcm);
753 }
754 
snd_soc_pcm_component_prepare(struct snd_pcm_substream * substream)755 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
756 {
757 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
758 	struct snd_soc_component *component;
759 	int i, ret;
760 
761 	for_each_rtd_components(rtd, i, component) {
762 		if (component->driver->prepare) {
763 			ret = component->driver->prepare(component, substream);
764 			if (ret < 0)
765 				return soc_component_ret(component, ret);
766 		}
767 	}
768 
769 	return 0;
770 }
771 
snd_soc_pcm_component_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_component ** last)772 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
773 				    struct snd_pcm_hw_params *params,
774 				    struct snd_soc_component **last)
775 {
776 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
777 	struct snd_soc_component *component;
778 	int i, ret;
779 
780 	for_each_rtd_components(rtd, i, component) {
781 		if (component->driver->hw_params) {
782 			ret = component->driver->hw_params(component,
783 							   substream, params);
784 			if (ret < 0) {
785 				*last = component;
786 				return soc_component_ret(component, ret);
787 			}
788 		}
789 	}
790 
791 	*last = NULL;
792 	return 0;
793 }
794 
snd_soc_pcm_component_hw_free(struct snd_pcm_substream * substream,struct snd_soc_component * last)795 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
796 				   struct snd_soc_component *last)
797 {
798 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
799 	struct snd_soc_component *component;
800 	int i, ret;
801 
802 	for_each_rtd_components(rtd, i, component) {
803 		if (component == last)
804 			break;
805 
806 		if (component->driver->hw_free) {
807 			ret = component->driver->hw_free(component, substream);
808 			if (ret < 0)
809 				soc_component_ret(component, ret);
810 		}
811 	}
812 }
813 
snd_soc_pcm_component_trigger(struct snd_pcm_substream * substream,int cmd)814 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
815 				  int cmd)
816 {
817 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
818 	struct snd_soc_component *component;
819 	int i, ret;
820 
821 	for_each_rtd_components(rtd, i, component) {
822 		if (component->driver->trigger) {
823 			ret = component->driver->trigger(component, substream, cmd);
824 			if (ret < 0)
825 				return soc_component_ret(component, ret);
826 		}
827 	}
828 
829 	return 0;
830 }
831 
snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime * rtd,void * stream)832 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
833 					 void *stream)
834 {
835 	struct snd_soc_component *component;
836 	int i, ret;
837 
838 	for_each_rtd_components(rtd, i, component) {
839 		ret = pm_runtime_get_sync(component->dev);
840 		if (ret < 0 && ret != -EACCES) {
841 			pm_runtime_put_noidle(component->dev);
842 			return soc_component_ret(component, ret);
843 		}
844 		/* mark stream if succeeded */
845 		soc_component_mark_push(component, stream, pm);
846 	}
847 
848 	return 0;
849 }
850 
snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime * rtd,void * stream,int rollback)851 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
852 					  void *stream, int rollback)
853 {
854 	struct snd_soc_component *component;
855 	int i;
856 
857 	for_each_rtd_components(rtd, i, component) {
858 		if (rollback && !soc_component_mark_match(component, stream, pm))
859 			continue;
860 
861 		pm_runtime_mark_last_busy(component->dev);
862 		pm_runtime_put_autosuspend(component->dev);
863 
864 		/* remove marked stream */
865 		soc_component_mark_pop(component, stream, pm);
866 	}
867 }
868 
snd_soc_pcm_component_ack(struct snd_pcm_substream * substream)869 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream)
870 {
871 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
872 	struct snd_soc_component *component;
873 	int i;
874 
875 	/* FIXME: use 1st pointer */
876 	for_each_rtd_components(rtd, i, component)
877 		if (component->driver->ack)
878 			return component->driver->ack(component, substream);
879 
880 	return 0;
881 }
882