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