xref: /linux/include/sound/soc-component.h (revision c5e90e8844692deb7bbcd029e8b92b3a20441903)
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_tstamp64 *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_new)(struct snd_soc_component *component,
90 		       struct snd_soc_pcm_runtime *rtd);
91 	void (*pcm_free)(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)(struct snd_soc_component *component,
141 		    struct snd_pcm_substream *substream, int channel,
142 		    unsigned long pos, struct iov_iter *iter,
143 		    unsigned long bytes);
144 	struct page *(*page)(struct snd_soc_component *component,
145 			     struct snd_pcm_substream *substream,
146 			     unsigned long offset);
147 	int (*mmap)(struct snd_soc_component *component,
148 		    struct snd_pcm_substream *substream,
149 		    struct vm_area_struct *vma);
150 	int (*ack)(struct snd_soc_component *component,
151 		   struct snd_pcm_substream *substream);
152 	snd_pcm_sframes_t (*delay)(struct snd_soc_component *component,
153 				   struct snd_pcm_substream *substream);
154 
155 	const struct snd_compress_ops *compress_ops;
156 
157 	/* probe ordering - for components with runtime dependencies */
158 	int probe_order;
159 	int remove_order;
160 
161 	/*
162 	 * soc_pcm_trigger() start/stop sequence.
163 	 * see also
164 	 *	snd_soc_dai_link
165 	 *	soc_pcm_trigger()
166 	 */
167 	enum snd_soc_trigger_order trigger_start;
168 	enum snd_soc_trigger_order trigger_stop;
169 
170 	/*
171 	 * signal if the module handling the component should not be removed
172 	 * if a pcm is open. Setting this would prevent the module
173 	 * refcount being incremented in probe() but allow it be incremented
174 	 * when a pcm is opened and decremented when it is closed.
175 	 */
176 	unsigned int module_get_upon_open:1;
177 
178 	/* bits */
179 	unsigned int idle_bias_on:1;
180 	unsigned int suspend_bias_off:1;
181 	unsigned int use_pmdown_time:1; /* care pmdown_time at stop */
182 	/*
183 	 * Indicates that the component does not care about the endianness of
184 	 * PCM audio data and the core will ensure that both LE and BE variants
185 	 * of each used format are present. Typically this is because the
186 	 * component sits behind a bus that abstracts away the endian of the
187 	 * original data, ie. one for which the transmission endian is defined
188 	 * (I2S/SLIMbus/SoundWire), or the concept of endian doesn't exist (PDM,
189 	 * analogue).
190 	 */
191 	unsigned int endianness:1;
192 	unsigned int legacy_dai_naming:1;
193 
194 	/* this component uses topology and ignore machine driver FEs */
195 	const char *ignore_machine;
196 	const char *topology_name_prefix;
197 	int (*be_hw_params_fixup)(struct snd_soc_pcm_runtime *rtd,
198 				  struct snd_pcm_hw_params *params);
199 	bool use_dai_pcm_id;	/* use DAI link PCM ID as PCM device number */
200 	int be_pcm_base;	/* base device ID for all BE PCMs */
201 
202 	const char *debugfs_prefix;
203 };
204 
205 struct snd_soc_component {
206 	const char *name;
207 	const char *name_prefix;
208 	struct device *dev;
209 	struct snd_soc_card *card;
210 
211 	unsigned int active;
212 
213 	unsigned int suspended:1; /* is in suspend PM state */
214 
215 	struct list_head list;
216 	struct list_head card_aux_list; /* for auxiliary bound components */
217 	struct list_head card_list;
218 
219 	const struct snd_soc_component_driver *driver;
220 
221 	struct list_head dai_list;
222 	int num_dai;
223 
224 	struct regmap *regmap;
225 
226 	struct mutex io_mutex;
227 
228 	/* attached dynamic objects */
229 	struct list_head dobj_list;
230 
231 	/*
232 	 * DO NOT use any of the fields below in drivers, they are temporary and
233 	 * are going to be removed again soon. If you use them in driver code
234 	 * the driver will be marked as BROKEN when these fields are removed.
235 	 */
236 
237 	struct snd_soc_dapm_context *dapm;
238 
239 	/* machine specific init */
240 	int (*init)(struct snd_soc_component *component);
241 
242 	/* function mark */
243 	void *mark_module;
244 	struct snd_pcm_substream *mark_open;
245 	struct snd_pcm_substream *mark_hw_params;
246 	struct snd_pcm_substream *mark_trigger;
247 	struct snd_compr_stream  *mark_compr_open;
248 	void *mark_pm;
249 
250 	struct dentry *debugfs_root;
251 };
252 
253 #define for_each_component_dais(component, dai)\
254 	list_for_each_entry(dai, &(component)->dai_list, list)
255 #define for_each_component_dais_safe(component, dai, _dai)\
256 	list_for_each_entry_safe(dai, _dai, &(component)->dai_list, list)
257 
258 /**
259  * snd_soc_component_to_dapm() - Returns the DAPM context associated with a
260  *  component
261  * @component: The component for which to get the DAPM context
262  */
263 static inline struct snd_soc_dapm_context *snd_soc_component_to_dapm(
264 	struct snd_soc_component *component)
265 {
266 	return component->dapm;
267 }
268 
269 /**
270  * snd_soc_component_cache_sync() - Sync the register cache with the hardware
271  * @component: COMPONENT to sync
272  *
273  * Note: This function will call regcache_sync()
274  */
275 static inline int snd_soc_component_cache_sync(
276 	struct snd_soc_component *component)
277 {
278 	return regcache_sync(component->regmap);
279 }
280 
281 void snd_soc_component_set_aux(struct snd_soc_component *component,
282 			       struct snd_soc_aux_dev *aux);
283 int snd_soc_component_init(struct snd_soc_component *component);
284 int snd_soc_component_is_dummy(struct snd_soc_component *component);
285 
286 /* component IO */
287 unsigned int snd_soc_component_read(struct snd_soc_component *component,
288 				      unsigned int reg);
289 int snd_soc_component_write(struct snd_soc_component *component,
290 			    unsigned int reg, unsigned int val);
291 int snd_soc_component_update_bits(struct snd_soc_component *component,
292 				  unsigned int reg, unsigned int mask,
293 				  unsigned int val);
294 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
295 					unsigned int reg, unsigned int mask,
296 					unsigned int val);
297 void snd_soc_component_async_complete(struct snd_soc_component *component);
298 int snd_soc_component_test_bits(struct snd_soc_component *component,
299 				unsigned int reg, unsigned int mask,
300 				unsigned int value);
301 
302 unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
303 					  unsigned int reg, unsigned int mask);
304 int snd_soc_component_write_field(struct snd_soc_component *component,
305 				  unsigned int reg, unsigned int mask,
306 				  unsigned int val);
307 
308 /* component wide operations */
309 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
310 				 int clk_id, int source,
311 				 unsigned int freq, int dir);
312 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
313 			      int source, unsigned int freq_in,
314 			      unsigned int freq_out);
315 int snd_soc_component_set_jack(struct snd_soc_component *component,
316 			       struct snd_soc_jack *jack, void *data);
317 int snd_soc_component_get_jack_type(struct snd_soc_component *component);
318 
319 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
320 				    enum snd_soc_dapm_type type, int subseq);
321 int snd_soc_component_stream_event(struct snd_soc_component *component,
322 				   int event);
323 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
324 				     enum snd_soc_bias_level level);
325 
326 int snd_soc_component_regmap_val_bytes(struct snd_soc_component *component);
327 #ifdef CONFIG_REGMAP
328 void snd_soc_component_init_regmap(struct snd_soc_component *component,
329 				   struct regmap *regmap);
330 void snd_soc_component_exit_regmap(struct snd_soc_component *component);
331 #endif
332 
333 #define snd_soc_component_module_get_when_probe(component)\
334 	snd_soc_component_module_get(component, NULL, 0)
335 #define snd_soc_component_module_get_when_open(component, substream)	\
336 	snd_soc_component_module_get(component, substream, 1)
337 int snd_soc_component_module_get(struct snd_soc_component *component,
338 				 void *mark, int upon_open);
339 #define snd_soc_component_module_put_when_remove(component)	\
340 	snd_soc_component_module_put(component, NULL, 0, 0)
341 #define snd_soc_component_module_put_when_close(component, substream, rollback) \
342 	snd_soc_component_module_put(component, substream, 1, rollback)
343 void snd_soc_component_module_put(struct snd_soc_component *component,
344 				  void *mark, int upon_open, int rollback);
345 
346 static inline void snd_soc_component_set_drvdata(struct snd_soc_component *c,
347 						 void *data)
348 {
349 	dev_set_drvdata(c->dev, data);
350 }
351 
352 static inline void *snd_soc_component_get_drvdata(struct snd_soc_component *c)
353 {
354 	return dev_get_drvdata(c->dev);
355 }
356 
357 static inline unsigned int
358 snd_soc_component_active(struct snd_soc_component *component)
359 {
360 	return component->active;
361 }
362 
363 /* component controls */
364 struct snd_kcontrol *snd_soc_component_get_kcontrol(struct snd_soc_component *component,
365 						    const char * const ctl);
366 int snd_soc_component_notify_control(struct snd_soc_component *component,
367 				     const char * const ctl);
368 
369 /* component driver ops */
370 int snd_soc_component_open(struct snd_soc_component *component,
371 			   struct snd_pcm_substream *substream);
372 int snd_soc_component_close(struct snd_soc_component *component,
373 			    struct snd_pcm_substream *substream,
374 			    int rollback);
375 void snd_soc_component_suspend(struct snd_soc_component *component);
376 void snd_soc_component_resume(struct snd_soc_component *component);
377 int snd_soc_component_is_suspended(struct snd_soc_component *component);
378 int snd_soc_component_probe(struct snd_soc_component *component);
379 void snd_soc_component_remove(struct snd_soc_component *component);
380 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
381 				      struct device_node *ep);
382 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
383 					const struct of_phandle_args *args,
384 					const char **dai_name);
385 int snd_soc_component_compr_open(struct snd_soc_component *component,
386 				 struct snd_compr_stream *cstream);
387 void snd_soc_component_compr_free(struct snd_soc_component *component,
388 				  struct snd_compr_stream *cstream,
389 				  int rollback);
390 int snd_soc_component_compr_trigger(struct snd_compr_stream *cstream, int cmd);
391 int snd_soc_component_compr_set_params(struct snd_compr_stream *cstream,
392 				       struct snd_compr_params *params);
393 int snd_soc_component_compr_get_params(struct snd_compr_stream *cstream,
394 				       struct snd_codec *params);
395 int snd_soc_component_compr_get_caps(struct snd_compr_stream *cstream,
396 				     struct snd_compr_caps *caps);
397 int snd_soc_component_compr_get_codec_caps(struct snd_compr_stream *cstream,
398 					   struct snd_compr_codec_caps *codec);
399 int snd_soc_component_compr_ack(struct snd_compr_stream *cstream, size_t bytes);
400 int snd_soc_component_compr_pointer(struct snd_compr_stream *cstream,
401 				    struct snd_compr_tstamp64 *tstamp);
402 int snd_soc_component_compr_copy(struct snd_compr_stream *cstream,
403 				 char __user *buf, size_t count);
404 int snd_soc_component_compr_set_metadata(struct snd_compr_stream *cstream,
405 					 struct snd_compr_metadata *metadata);
406 int snd_soc_component_compr_get_metadata(struct snd_compr_stream *cstream,
407 					 struct snd_compr_metadata *metadata);
408 
409 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream);
410 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
411 				unsigned int cmd, void *arg);
412 int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream);
413 int snd_soc_pcm_component_copy(struct snd_pcm_substream *substream,
414 			       int channel, unsigned long pos,
415 			       struct iov_iter *iter, unsigned long bytes);
416 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
417 					unsigned long offset);
418 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
419 			       struct vm_area_struct *vma);
420 int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd);
421 void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd);
422 int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream);
423 int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
424 				    struct snd_pcm_hw_params *params);
425 void snd_soc_pcm_component_hw_free(struct snd_pcm_substream *substream,
426 				   int rollback);
427 int snd_soc_pcm_component_trigger(struct snd_pcm_substream *substream,
428 				  int cmd, int rollback);
429 int snd_soc_pcm_component_pm_runtime_get(struct snd_soc_pcm_runtime *rtd,
430 					 void *stream);
431 void snd_soc_pcm_component_pm_runtime_put(struct snd_soc_pcm_runtime *rtd,
432 					  void *stream, int rollback);
433 int snd_soc_pcm_component_ack(struct snd_pcm_substream *substream);
434 void snd_soc_pcm_component_delay(struct snd_pcm_substream *substream,
435 				 snd_pcm_sframes_t *cpu_delay, snd_pcm_sframes_t *codec_delay);
436 
437 #endif /* __SOC_COMPONENT_H */
438