xref: /openbmc/linux/include/sound/soc-component.h (revision 66201cacc33d740057bd64af6371ad846cff376f)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * soc-component.h
4  *
5  * Copyright (C) 2019 Renesas Electronics Corp.
6  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7  */
8 #ifndef __SOC_COMPONENT_H
9 #define __SOC_COMPONENT_H
10 
11 #include <sound/soc.h>
12 
13 /*
14  * Component probe and remove ordering levels for components with runtime
15  * dependencies.
16  */
17 #define SND_SOC_COMP_ORDER_FIRST	-2
18 #define SND_SOC_COMP_ORDER_EARLY	-1
19 #define SND_SOC_COMP_ORDER_NORMAL	 0
20 #define SND_SOC_COMP_ORDER_LATE		 1
21 #define SND_SOC_COMP_ORDER_LAST		 2
22 
23 #define for_each_comp_order(order)		\
24 	for (order  = SND_SOC_COMP_ORDER_FIRST;	\
25 	     order <= SND_SOC_COMP_ORDER_LAST;	\
26 	     order++)
27 
28 /* component interface */
29 struct snd_compress_ops {
30 	int (*open)(struct snd_soc_component *component,
31 		    struct snd_compr_stream *stream);
32 	int (*free)(struct snd_soc_component *component,
33 		    struct snd_compr_stream *stream);
34 	int (*set_params)(struct snd_soc_component *component,
35 			  struct snd_compr_stream *stream,
36 			  struct snd_compr_params *params);
37 	int (*get_params)(struct snd_soc_component *component,
38 			  struct snd_compr_stream *stream,
39 			  struct snd_codec *params);
40 	int (*set_metadata)(struct snd_soc_component *component,
41 			    struct snd_compr_stream *stream,
42 			    struct snd_compr_metadata *metadata);
43 	int (*get_metadata)(struct snd_soc_component *component,
44 			    struct snd_compr_stream *stream,
45 			    struct snd_compr_metadata *metadata);
46 	int (*trigger)(struct snd_soc_component *component,
47 		       struct snd_compr_stream *stream, int cmd);
48 	int (*pointer)(struct snd_soc_component *component,
49 		       struct snd_compr_stream *stream,
50 		       struct snd_compr_tstamp *tstamp);
51 	int (*copy)(struct snd_soc_component *component,
52 		    struct snd_compr_stream *stream, char __user *buf,
53 		    size_t count);
54 	int (*mmap)(struct snd_soc_component *component,
55 		    struct snd_compr_stream *stream,
56 		    struct vm_area_struct *vma);
57 	int (*ack)(struct snd_soc_component *component,
58 		   struct snd_compr_stream *stream, size_t bytes);
59 	int (*get_caps)(struct snd_soc_component *component,
60 			struct snd_compr_stream *stream,
61 			struct snd_compr_caps *caps);
62 	int (*get_codec_caps)(struct snd_soc_component *component,
63 			      struct snd_compr_stream *stream,
64 			      struct snd_compr_codec_caps *codec);
65 };
66 
67 struct snd_soc_component_driver {
68 	const char *name;
69 
70 	/* Default control and setup, added after probe() is run */
71 	const struct snd_kcontrol_new *controls;
72 	unsigned int num_controls;
73 	const struct snd_soc_dapm_widget *dapm_widgets;
74 	unsigned int num_dapm_widgets;
75 	const struct snd_soc_dapm_route *dapm_routes;
76 	unsigned int num_dapm_routes;
77 
78 	int (*probe)(struct snd_soc_component *component);
79 	void (*remove)(struct snd_soc_component *component);
80 	int (*suspend)(struct snd_soc_component *component);
81 	int (*resume)(struct snd_soc_component *component);
82 
83 	unsigned int (*read)(struct snd_soc_component *component,
84 			     unsigned int reg);
85 	int (*write)(struct snd_soc_component *component,
86 		     unsigned int reg, unsigned int val);
87 
88 	/* pcm creation and destruction */
89 	int (*pcm_construct)(struct snd_soc_component *component,
90 			     struct snd_soc_pcm_runtime *rtd);
91 	void (*pcm_destruct)(struct snd_soc_component *component,
92 			     struct snd_pcm *pcm);
93 
94 	/* component wide operations */
95 	int (*set_sysclk)(struct snd_soc_component *component,
96 			  int clk_id, int source, unsigned int freq, int dir);
97 	int (*set_pll)(struct snd_soc_component *component, int pll_id,
98 		       int source, unsigned int freq_in, unsigned int freq_out);
99 	int (*set_jack)(struct snd_soc_component *component,
100 			struct snd_soc_jack *jack,  void *data);
101 	int (*get_jack_type)(struct snd_soc_component *component);
102 
103 	/* DT */
104 	int (*of_xlate_dai_name)(struct snd_soc_component *component,
105 				 const struct of_phandle_args *args,
106 				 const char **dai_name);
107 	int (*of_xlate_dai_id)(struct snd_soc_component *comment,
108 			       struct device_node *endpoint);
109 	void (*seq_notifier)(struct snd_soc_component *component,
110 			     enum snd_soc_dapm_type type, int subseq);
111 	int (*stream_event)(struct snd_soc_component *component, int event);
112 	int (*set_bias_level)(struct snd_soc_component *component,
113 			      enum snd_soc_bias_level level);
114 
115 	int (*open)(struct snd_soc_component *component,
116 		    struct snd_pcm_substream *substream);
117 	int (*close)(struct snd_soc_component *component,
118 		     struct snd_pcm_substream *substream);
119 	int (*ioctl)(struct snd_soc_component *component,
120 		     struct snd_pcm_substream *substream,
121 		     unsigned int cmd, void *arg);
122 	int (*hw_params)(struct snd_soc_component *component,
123 			 struct snd_pcm_substream *substream,
124 			 struct snd_pcm_hw_params *params);
125 	int (*hw_free)(struct snd_soc_component *component,
126 		       struct snd_pcm_substream *substream);
127 	int (*prepare)(struct snd_soc_component *component,
128 		       struct snd_pcm_substream *substream);
129 	int (*trigger)(struct snd_soc_component *component,
130 		       struct snd_pcm_substream *substream, int cmd);
131 	int (*sync_stop)(struct snd_soc_component *component,
132 			 struct snd_pcm_substream *substream);
133 	snd_pcm_uframes_t (*pointer)(struct snd_soc_component *component,
134 				     struct snd_pcm_substream *substream);
135 	int (*get_time_info)(struct snd_soc_component *component,
136 		struct snd_pcm_substream *substream, struct timespec64 *system_ts,
137 		struct timespec64 *audio_ts,
138 		struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
139 		struct snd_pcm_audio_tstamp_report *audio_tstamp_report);
140 	int (*copy_user)(struct snd_soc_component *component,
141 			 struct snd_pcm_substream *substream, int channel,
142 			 unsigned long pos, void __user *buf,
143 			 unsigned long bytes);
144 	int (*copy)(struct snd_soc_component *component,
145 		    struct snd_pcm_substream *substream, int channel,
146 		    unsigned long pos, struct iov_iter *buf,
147 		    unsigned long bytes);
148 	struct page *(*page)(struct snd_soc_component *component,
149 			     struct snd_pcm_substream *substream,
150 			     unsigned long offset);
151 	int (*mmap)(struct snd_soc_component *component,
152 		    struct snd_pcm_substream *substream,
153 		    struct vm_area_struct *vma);
154 	int (*ack)(struct snd_soc_component *component,
155 		   struct snd_pcm_substream *substream);
156 	snd_pcm_sframes_t (*delay)(struct snd_soc_component *component,
157 				   struct snd_pcm_substream *substream);
158 
159 	const struct snd_compress_ops *compress_ops;
160 
161 	/* probe ordering - for components with runtime dependencies */
162 	int probe_order;
163 	int remove_order;
164 
165 	/*
166 	 * soc_pcm_trigger() start/stop sequence.
167 	 * see also
168 	 *	snd_soc_dai_link
169 	 *	soc_pcm_trigger()
170 	 */
171 	enum snd_soc_trigger_order trigger_start;
172 	enum snd_soc_trigger_order trigger_stop;
173 
174 	/*
175 	 * signal if the module handling the component should not be removed
176 	 * if a pcm is open. Setting this would prevent the module
177 	 * refcount being incremented in probe() but allow it be incremented
178 	 * when a pcm is opened and decremented when it is closed.
179 	 */
180 	unsigned int module_get_upon_open:1;
181 
182 	/* bits */
183 	unsigned int idle_bias_on:1;
184 	unsigned int suspend_bias_off:1;
185 	unsigned int use_pmdown_time:1; /* care pmdown_time at stop */
186 	/*
187 	 * Indicates that the component does not care about the endianness of
188 	 * PCM audio data and the core will ensure that both LE and BE variants
189 	 * of each used format are present. Typically this is because the
190 	 * component sits behind a bus that abstracts away the endian of the
191 	 * original data, ie. one for which the transmission endian is defined
192 	 * (I2S/SLIMbus/SoundWire), or the concept of endian doesn't exist (PDM,
193 	 * analogue).
194 	 */
195 	unsigned int endianness:1;
196 	unsigned int legacy_dai_naming:1;
197 
198 	/* this component uses topology and ignore machine driver FEs */
199 	const char *ignore_machine;
200 	const char *topology_name_prefix;
201 	int (*be_hw_params_fixup)(struct snd_soc_pcm_runtime *rtd,
202 				  struct snd_pcm_hw_params *params);
203 	bool use_dai_pcm_id;	/* use DAI link PCM ID as PCM device number */
204 	int be_pcm_base;	/* base device ID for all BE PCMs */
205 
206 #ifdef CONFIG_DEBUG_FS
207 	const char *debugfs_prefix;
208 #endif
209 };
210 
211 struct snd_soc_component {
212 	const char *name;
213 	int id;
214 	const char *name_prefix;
215 	struct device *dev;
216 	struct snd_soc_card *card;
217 
218 	unsigned int active;
219 
220 	unsigned int suspended:1; /* is in suspend PM state */
221 
222 	struct list_head list;
223 	struct list_head card_aux_list; /* for auxiliary bound components */
224 	struct list_head card_list;
225 
226 	const struct snd_soc_component_driver *driver;
227 
228 	struct list_head dai_list;
229 	int num_dai;
230 
231 	struct regmap *regmap;
232 	int val_bytes;
233 
234 	struct mutex io_mutex;
235 
236 	/* attached dynamic objects */
237 	struct list_head dobj_list;
238 
239 	/*
240 	 * DO NOT use any of the fields below in drivers, they are temporary and
241 	 * are going to be removed again soon. If you use them in driver code
242 	 * the driver will be marked as BROKEN when these fields are removed.
243 	 */
244 
245 	/* Don't use these, use snd_soc_component_get_dapm() */
246 	struct snd_soc_dapm_context dapm;
247 
248 	/* machine specific init */
249 	int (*init)(struct snd_soc_component *component);
250 
251 	/* function mark */
252 	void *mark_module;
253 	struct snd_pcm_substream *mark_open;
254 	struct snd_pcm_substream *mark_hw_params;
255 	struct snd_pcm_substream *mark_trigger;
256 	struct snd_compr_stream  *mark_compr_open;
257 	void *mark_pm;
258 
259 	struct dentry *debugfs_root;
260 	const char *debugfs_prefix;
261 };
262 
263 #define for_each_component_dais(component, dai)\
264 	list_for_each_entry(dai, &(component)->dai_list, list)
265 #define for_each_component_dais_safe(component, dai, _dai)\
266 	list_for_each_entry_safe(dai, _dai, &(component)->dai_list, list)
267 
268 /**
269  * snd_soc_dapm_to_component() - Casts a DAPM context to the component it is
270  *  embedded in
271  * @dapm: The DAPM context to cast to the component
272  *
273  * This function must only be used on DAPM contexts that are known to be part of
274  * a component (e.g. in a component driver). Otherwise the behavior is
275  * undefined.
276  */
277 static inline struct snd_soc_component *snd_soc_dapm_to_component(
278 	struct snd_soc_dapm_context *dapm)
279 {
280 	return container_of(dapm, struct snd_soc_component, dapm);
281 }
282 
283 /**
284  * snd_soc_component_get_dapm() - Returns the DAPM context associated with a
285  *  component
286  * @component: The component for which to get the DAPM context
287  */
288 static inline struct snd_soc_dapm_context *snd_soc_component_get_dapm(
289 	struct snd_soc_component *component)
290 {
291 	return &component->dapm;
292 }
293 
294 /**
295  * snd_soc_component_init_bias_level() - Initialize COMPONENT DAPM bias level
296  * @component: The COMPONENT for which to initialize the DAPM bias level
297  * @level: The DAPM level to initialize to
298  *
299  * Initializes the COMPONENT DAPM bias level. See snd_soc_dapm_init_bias_level()
300  */
301 static inline void
302 snd_soc_component_init_bias_level(struct snd_soc_component *component,
303 				  enum snd_soc_bias_level level)
304 {
305 	snd_soc_dapm_init_bias_level(
306 		snd_soc_component_get_dapm(component), level);
307 }
308 
309 /**
310  * snd_soc_component_get_bias_level() - Get current COMPONENT DAPM bias level
311  * @component: The COMPONENT for which to get the DAPM bias level
312  *
313  * Returns: The current DAPM bias level of the COMPONENT.
314  */
315 static inline enum snd_soc_bias_level
316 snd_soc_component_get_bias_level(struct snd_soc_component *component)
317 {
318 	return snd_soc_dapm_get_bias_level(
319 		snd_soc_component_get_dapm(component));
320 }
321 
322 /**
323  * snd_soc_component_force_bias_level() - Set the COMPONENT DAPM bias level
324  * @component: The COMPONENT for which to set the level
325  * @level: The level to set to
326  *
327  * Forces the COMPONENT bias level to a specific state. See
328  * snd_soc_dapm_force_bias_level().
329  */
330 static inline int
331 snd_soc_component_force_bias_level(struct snd_soc_component *component,
332 				   enum snd_soc_bias_level level)
333 {
334 	return snd_soc_dapm_force_bias_level(
335 		snd_soc_component_get_dapm(component),
336 		level);
337 }
338 
339 /**
340  * snd_soc_dapm_kcontrol_component() - Returns the component associated to a
341  * kcontrol
342  * @kcontrol: The kcontrol
343  *
344  * This function must only be used on DAPM contexts that are known to be part of
345  * a COMPONENT (e.g. in a COMPONENT driver). Otherwise the behavior is undefined
346  */
347 static inline struct snd_soc_component *snd_soc_dapm_kcontrol_component(
348 	struct snd_kcontrol *kcontrol)
349 {
350 	return snd_soc_dapm_to_component(snd_soc_dapm_kcontrol_dapm(kcontrol));
351 }
352 
353 /**
354  * snd_soc_component_cache_sync() - Sync the register cache with the hardware
355  * @component: COMPONENT to sync
356  *
357  * Note: This function will call regcache_sync()
358  */
359 static inline int snd_soc_component_cache_sync(
360 	struct snd_soc_component *component)
361 {
362 	return regcache_sync(component->regmap);
363 }
364 
365 void snd_soc_component_set_aux(struct snd_soc_component *component,
366 			       struct snd_soc_aux_dev *aux);
367 int snd_soc_component_init(struct snd_soc_component *component);
368 int snd_soc_component_is_dummy(struct snd_soc_component *component);
369 
370 /* component IO */
371 unsigned int snd_soc_component_read(struct snd_soc_component *component,
372 				      unsigned int reg);
373 int snd_soc_component_write(struct snd_soc_component *component,
374 			    unsigned int reg, unsigned int val);
375 int snd_soc_component_update_bits(struct snd_soc_component *component,
376 				  unsigned int reg, unsigned int mask,
377 				  unsigned int val);
378 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
379 					unsigned int reg, unsigned int mask,
380 					unsigned int val);
381 void snd_soc_component_async_complete(struct snd_soc_component *component);
382 int snd_soc_component_test_bits(struct snd_soc_component *component,
383 				unsigned int reg, unsigned int mask,
384 				unsigned int value);
385 
386 unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
387 					  unsigned int reg, unsigned int mask);
388 int snd_soc_component_write_field(struct snd_soc_component *component,
389 				  unsigned int reg, unsigned int mask,
390 				  unsigned int val);
391 
392 /* component wide operations */
393 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
394 				 int clk_id, int source,
395 				 unsigned int freq, int dir);
396 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
397 			      int source, unsigned int freq_in,
398 			      unsigned int freq_out);
399 int snd_soc_component_set_jack(struct snd_soc_component *component,
400 			       struct snd_soc_jack *jack, void *data);
401 int snd_soc_component_get_jack_type(struct snd_soc_component *component);
402 
403 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
404 				    enum snd_soc_dapm_type type, int subseq);
405 int snd_soc_component_stream_event(struct snd_soc_component *component,
406 				   int event);
407 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
408 				     enum snd_soc_bias_level level);
409 
410 void snd_soc_component_setup_regmap(struct snd_soc_component *component);
411 #ifdef CONFIG_REGMAP
412 void snd_soc_component_init_regmap(struct snd_soc_component *component,
413 				   struct regmap *regmap);
414 void snd_soc_component_exit_regmap(struct snd_soc_component *component);
415 #endif
416 
417 #define snd_soc_component_module_get_when_probe(component)\
418 	snd_soc_component_module_get(component, NULL, 0)
419 #define snd_soc_component_module_get_when_open(component, substream)	\
420 	snd_soc_component_module_get(component, substream, 1)
421 int snd_soc_component_module_get(struct snd_soc_component *component,
422 				 void *mark, int upon_open);
423 #define snd_soc_component_module_put_when_remove(component)	\
424 	snd_soc_component_module_put(component, NULL, 0, 0)
425 #define snd_soc_component_module_put_when_close(component, substream, rollback) \
426 	snd_soc_component_module_put(component, substream, 1, rollback)
427 void snd_soc_component_module_put(struct snd_soc_component *component,
428 				  void *mark, int upon_open, int rollback);
429 
430 static inline void snd_soc_component_set_drvdata(struct snd_soc_component *c,
431 						 void *data)
432 {
433 	dev_set_drvdata(c->dev, data);
434 }
435 
436 static inline void *snd_soc_component_get_drvdata(struct snd_soc_component *c)
437 {
438 	return dev_get_drvdata(c->dev);
439 }
440 
441 static inline unsigned int
442 snd_soc_component_active(struct snd_soc_component *component)
443 {
444 	return component->active;
445 }
446 
447 /* component pin */
448 int snd_soc_component_enable_pin(struct snd_soc_component *component,
449 				 const char *pin);
450 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
451 					  const char *pin);
452 int snd_soc_component_disable_pin(struct snd_soc_component *component,
453 				  const char *pin);
454 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
455 					   const char *pin);
456 int snd_soc_component_nc_pin(struct snd_soc_component *component,
457 			     const char *pin);
458 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
459 				      const char *pin);
460 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
461 				     const char *pin);
462 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
463 				       const char *pin);
464 int snd_soc_component_force_enable_pin_unlocked(
465 	struct snd_soc_component *component,
466 	const char *pin);
467 
468 /* component controls */
469 int snd_soc_component_notify_control(struct snd_soc_component *component,
470 				     const char * const ctl);
471 
472 /* component driver ops */
473 int snd_soc_component_open(struct snd_soc_component *component,
474 			   struct snd_pcm_substream *substream);
475 int snd_soc_component_close(struct snd_soc_component *component,
476 			    struct snd_pcm_substream *substream,
477 			    int rollback);
478 void snd_soc_component_suspend(struct snd_soc_component *component);
479 void snd_soc_component_resume(struct snd_soc_component *component);
480 int snd_soc_component_is_suspended(struct snd_soc_component *component);
481 int snd_soc_component_probe(struct snd_soc_component *component);
482 void snd_soc_component_remove(struct snd_soc_component *component);
483 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
484 				      struct device_node *ep);
485 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
486 					const struct of_phandle_args *args,
487 					const char **dai_name);
488 int snd_soc_component_compr_open(struct snd_soc_component *component,
489 				 struct snd_compr_stream *cstream);
490 void snd_soc_component_compr_free(struct snd_soc_component *component,
491 				  struct snd_compr_stream *cstream,
492 				  int rollback);
493 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd);
494 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
495 				       struct snd_compr_params *params);
496 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
497 				       struct snd_codec *params);
498 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
499 				     struct snd_compr_caps *caps);
500 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
501 					   struct snd_compr_codec_caps *codec);
502 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes);
503 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
504 				    struct snd_compr_tstamp *tstamp);
505 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
506 				 char __user *buf, size_t count);
507 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
508 					 struct snd_compr_metadata *metadata);
509 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
510 					 struct snd_compr_metadata *metadata);
511 
512 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream);
513 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
514 				unsigned int cmd, void *arg);
515 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream);
516 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
517 				    int channel, unsigned long pos,
518 				    void __user *buf, unsigned long bytes);
519 int snd_soc_pcm_component_copy(struct snd_pcm_substream *substream,
520 			       int channel, unsigned long pos,
521 			       struct iov_iter *buf, unsigned long bytes);
522 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
523 					unsigned long offset);
524 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
525 			       struct vm_area_struct *vma);
526 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd);
527 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd);
528 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream);
529 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
530 				    struct snd_pcm_hw_params *params);
531 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
532 				   int rollback);
533 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
534 				  int cmd, int rollback);
535 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
536 					 void *stream);
537 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
538 					  void *stream, int rollback);
539 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream);
540 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
541 				 snd_pcm_sframes_t *cpu_delay, snd_pcm_sframes_t *codec_delay);
542 
543 #endif /* __SOC_COMPONENT_H */
544