6aa3ecb7b6d3719e67f091f0b689667bdca0035c
[linux-2.6-block.git] / include / sound / soc-component.h
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * soc-component.h
4  *
5  * Copyright (c) 2019 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  */
7 #ifndef __SOC_COMPONENT_H
8 #define __SOC_COMPONENT_H
9
10 #include <sound/soc.h>
11
12 /*
13  * Component probe and remove ordering levels for components with runtime
14  * dependencies.
15  */
16 #define SND_SOC_COMP_ORDER_FIRST        -2
17 #define SND_SOC_COMP_ORDER_EARLY        -1
18 #define SND_SOC_COMP_ORDER_NORMAL        0
19 #define SND_SOC_COMP_ORDER_LATE          1
20 #define SND_SOC_COMP_ORDER_LAST          2
21
22 #define for_each_comp_order(order)              \
23         for (order  = SND_SOC_COMP_ORDER_FIRST; \
24              order <= SND_SOC_COMP_ORDER_LAST;  \
25              order++)
26
27 /* component interface */
28 struct snd_soc_component_driver {
29         const char *name;
30
31         /* Default control and setup, added after probe() is run */
32         const struct snd_kcontrol_new *controls;
33         unsigned int num_controls;
34         const struct snd_soc_dapm_widget *dapm_widgets;
35         unsigned int num_dapm_widgets;
36         const struct snd_soc_dapm_route *dapm_routes;
37         unsigned int num_dapm_routes;
38
39         int (*probe)(struct snd_soc_component *component);
40         void (*remove)(struct snd_soc_component *component);
41         int (*suspend)(struct snd_soc_component *component);
42         int (*resume)(struct snd_soc_component *component);
43
44         unsigned int (*read)(struct snd_soc_component *component,
45                              unsigned int reg);
46         int (*write)(struct snd_soc_component *component,
47                      unsigned int reg, unsigned int val);
48
49         /* pcm creation and destruction */
50         int (*pcm_construct)(struct snd_soc_component *component,
51                              struct snd_soc_pcm_runtime *rtd);
52         void (*pcm_destruct)(struct snd_soc_component *component,
53                              struct snd_pcm *pcm);
54
55         /* component wide operations */
56         int (*set_sysclk)(struct snd_soc_component *component,
57                           int clk_id, int source, unsigned int freq, int dir);
58         int (*set_pll)(struct snd_soc_component *component, int pll_id,
59                        int source, unsigned int freq_in, unsigned int freq_out);
60         int (*set_jack)(struct snd_soc_component *component,
61                         struct snd_soc_jack *jack,  void *data);
62
63         /* DT */
64         int (*of_xlate_dai_name)(struct snd_soc_component *component,
65                                  struct of_phandle_args *args,
66                                  const char **dai_name);
67         int (*of_xlate_dai_id)(struct snd_soc_component *comment,
68                                struct device_node *endpoint);
69         void (*seq_notifier)(struct snd_soc_component *component,
70                              enum snd_soc_dapm_type type, int subseq);
71         int (*stream_event)(struct snd_soc_component *component, int event);
72         int (*set_bias_level)(struct snd_soc_component *component,
73                               enum snd_soc_bias_level level);
74
75         int (*open)(struct snd_soc_component *component,
76                     struct snd_pcm_substream *substream);
77         int (*close)(struct snd_soc_component *component,
78                      struct snd_pcm_substream *substream);
79         int (*ioctl)(struct snd_soc_component *component,
80                      struct snd_pcm_substream *substream,
81                      unsigned int cmd, void *arg);
82         int (*hw_params)(struct snd_soc_component *component,
83                          struct snd_pcm_substream *substream,
84                          struct snd_pcm_hw_params *params);
85         int (*hw_free)(struct snd_soc_component *component,
86                        struct snd_pcm_substream *substream);
87         int (*prepare)(struct snd_soc_component *component,
88                        struct snd_pcm_substream *substream);
89         int (*trigger)(struct snd_soc_component *component,
90                        struct snd_pcm_substream *substream, int cmd);
91         snd_pcm_uframes_t (*pointer)(struct snd_soc_component *component,
92                                      struct snd_pcm_substream *substream);
93         int (*get_time_info)(struct snd_soc_component *component,
94                 struct snd_pcm_substream *substream, struct timespec *system_ts,
95                 struct timespec *audio_ts,
96                 struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
97                 struct snd_pcm_audio_tstamp_report *audio_tstamp_report);
98         int (*copy_user)(struct snd_soc_component *component,
99                          struct snd_pcm_substream *substream, int channel,
100                          unsigned long pos, void __user *buf,
101                          unsigned long bytes);
102         struct page *(*page)(struct snd_soc_component *component,
103                              struct snd_pcm_substream *substream,
104                              unsigned long offset);
105         int (*mmap)(struct snd_soc_component *component,
106                     struct snd_pcm_substream *substream,
107                     struct vm_area_struct *vma);
108
109         const struct snd_compr_ops *compr_ops;
110
111         /* probe ordering - for components with runtime dependencies */
112         int probe_order;
113         int remove_order;
114
115         /*
116          * signal if the module handling the component should not be removed
117          * if a pcm is open. Setting this would prevent the module
118          * refcount being incremented in probe() but allow it be incremented
119          * when a pcm is opened and decremented when it is closed.
120          */
121         unsigned int module_get_upon_open:1;
122
123         /* bits */
124         unsigned int idle_bias_on:1;
125         unsigned int suspend_bias_off:1;
126         unsigned int use_pmdown_time:1; /* care pmdown_time at stop */
127         unsigned int endianness:1;
128         unsigned int non_legacy_dai_naming:1;
129
130         /* this component uses topology and ignore machine driver FEs */
131         const char *ignore_machine;
132         const char *topology_name_prefix;
133         int (*be_hw_params_fixup)(struct snd_soc_pcm_runtime *rtd,
134                                   struct snd_pcm_hw_params *params);
135         bool use_dai_pcm_id;    /* use DAI link PCM ID as PCM device number */
136         int be_pcm_base;        /* base device ID for all BE PCMs */
137 };
138
139 struct snd_soc_component {
140         const char *name;
141         int id;
142         const char *name_prefix;
143         struct device *dev;
144         struct snd_soc_card *card;
145
146         unsigned int active;
147
148         unsigned int suspended:1; /* is in suspend PM state */
149
150         struct list_head list;
151         struct list_head card_aux_list; /* for auxiliary bound components */
152         struct list_head card_list;
153
154         const struct snd_soc_component_driver *driver;
155
156         struct list_head dai_list;
157         int num_dai;
158
159         struct regmap *regmap;
160         int val_bytes;
161
162         struct mutex io_mutex;
163
164         /* attached dynamic objects */
165         struct list_head dobj_list;
166
167         /*
168          * DO NOT use any of the fields below in drivers, they are temporary and
169          * are going to be removed again soon. If you use them in driver code
170          * the driver will be marked as BROKEN when these fields are removed.
171          */
172
173         /* Don't use these, use snd_soc_component_get_dapm() */
174         struct snd_soc_dapm_context dapm;
175
176         /* machine specific init */
177         int (*init)(struct snd_soc_component *component);
178
179 #ifdef CONFIG_DEBUG_FS
180         struct dentry *debugfs_root;
181         const char *debugfs_prefix;
182 #endif
183 };
184
185 #define for_each_component_dais(component, dai)\
186         list_for_each_entry(dai, &(component)->dai_list, list)
187 #define for_each_component_dais_safe(component, dai, _dai)\
188         list_for_each_entry_safe(dai, _dai, &(component)->dai_list, list)
189
190 /**
191  * snd_soc_dapm_to_component() - Casts a DAPM context to the component it is
192  *  embedded in
193  * @dapm: The DAPM context to cast to the component
194  *
195  * This function must only be used on DAPM contexts that are known to be part of
196  * a component (e.g. in a component driver). Otherwise the behavior is
197  * undefined.
198  */
199 static inline struct snd_soc_component *snd_soc_dapm_to_component(
200         struct snd_soc_dapm_context *dapm)
201 {
202         return container_of(dapm, struct snd_soc_component, dapm);
203 }
204
205 /**
206  * snd_soc_component_get_dapm() - Returns the DAPM context associated with a
207  *  component
208  * @component: The component for which to get the DAPM context
209  */
210 static inline struct snd_soc_dapm_context *snd_soc_component_get_dapm(
211         struct snd_soc_component *component)
212 {
213         return &component->dapm;
214 }
215
216 /**
217  * snd_soc_component_init_bias_level() - Initialize COMPONENT DAPM bias level
218  * @component: The COMPONENT for which to initialize the DAPM bias level
219  * @level: The DAPM level to initialize to
220  *
221  * Initializes the COMPONENT DAPM bias level. See snd_soc_dapm_init_bias_level()
222  */
223 static inline void
224 snd_soc_component_init_bias_level(struct snd_soc_component *component,
225                                   enum snd_soc_bias_level level)
226 {
227         snd_soc_dapm_init_bias_level(
228                 snd_soc_component_get_dapm(component), level);
229 }
230
231 /**
232  * snd_soc_component_get_bias_level() - Get current COMPONENT DAPM bias level
233  * @component: The COMPONENT for which to get the DAPM bias level
234  *
235  * Returns: The current DAPM bias level of the COMPONENT.
236  */
237 static inline enum snd_soc_bias_level
238 snd_soc_component_get_bias_level(struct snd_soc_component *component)
239 {
240         return snd_soc_dapm_get_bias_level(
241                 snd_soc_component_get_dapm(component));
242 }
243
244 /**
245  * snd_soc_component_force_bias_level() - Set the COMPONENT DAPM bias level
246  * @component: The COMPONENT for which to set the level
247  * @level: The level to set to
248  *
249  * Forces the COMPONENT bias level to a specific state. See
250  * snd_soc_dapm_force_bias_level().
251  */
252 static inline int
253 snd_soc_component_force_bias_level(struct snd_soc_component *component,
254                                    enum snd_soc_bias_level level)
255 {
256         return snd_soc_dapm_force_bias_level(
257                 snd_soc_component_get_dapm(component),
258                 level);
259 }
260
261 /**
262  * snd_soc_dapm_kcontrol_component() - Returns the component associated to a
263  * kcontrol
264  * @kcontrol: The kcontrol
265  *
266  * This function must only be used on DAPM contexts that are known to be part of
267  * a COMPONENT (e.g. in a COMPONENT driver). Otherwise the behavior is undefined
268  */
269 static inline struct snd_soc_component *snd_soc_dapm_kcontrol_component(
270         struct snd_kcontrol *kcontrol)
271 {
272         return snd_soc_dapm_to_component(snd_soc_dapm_kcontrol_dapm(kcontrol));
273 }
274
275 /**
276  * snd_soc_component_cache_sync() - Sync the register cache with the hardware
277  * @component: COMPONENT to sync
278  *
279  * Note: This function will call regcache_sync()
280  */
281 static inline int snd_soc_component_cache_sync(
282         struct snd_soc_component *component)
283 {
284         return regcache_sync(component->regmap);
285 }
286
287 /* component IO */
288 int snd_soc_component_read(struct snd_soc_component *component,
289                            unsigned int reg, unsigned int *val);
290 unsigned int snd_soc_component_read32(struct snd_soc_component *component,
291                                       unsigned int reg);
292 int snd_soc_component_write(struct snd_soc_component *component,
293                             unsigned int reg, unsigned int val);
294 int snd_soc_component_update_bits(struct snd_soc_component *component,
295                                   unsigned int reg, unsigned int mask,
296                                   unsigned int val);
297 int snd_soc_component_update_bits_async(struct snd_soc_component *component,
298                                         unsigned int reg, unsigned int mask,
299                                         unsigned int val);
300 void snd_soc_component_async_complete(struct snd_soc_component *component);
301 int snd_soc_component_test_bits(struct snd_soc_component *component,
302                                 unsigned int reg, unsigned int mask,
303                                 unsigned int value);
304
305 /* component wide operations */
306 int snd_soc_component_set_sysclk(struct snd_soc_component *component,
307                                  int clk_id, int source,
308                                  unsigned int freq, int dir);
309 int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
310                               int source, unsigned int freq_in,
311                               unsigned int freq_out);
312 int snd_soc_component_set_jack(struct snd_soc_component *component,
313                                struct snd_soc_jack *jack, void *data);
314
315 void snd_soc_component_seq_notifier(struct snd_soc_component *component,
316                                     enum snd_soc_dapm_type type, int subseq);
317 int snd_soc_component_stream_event(struct snd_soc_component *component,
318                                    int event);
319 int snd_soc_component_set_bias_level(struct snd_soc_component *component,
320                                      enum snd_soc_bias_level level);
321
322 #ifdef CONFIG_REGMAP
323 void snd_soc_component_init_regmap(struct snd_soc_component *component,
324                                    struct regmap *regmap);
325 void snd_soc_component_exit_regmap(struct snd_soc_component *component);
326 #endif
327
328 #define snd_soc_component_module_get_when_probe(component)\
329         snd_soc_component_module_get(component, 0)
330 #define snd_soc_component_module_get_when_open(component)       \
331         snd_soc_component_module_get(component, 1)
332 int snd_soc_component_module_get(struct snd_soc_component *component,
333                                  int upon_open);
334 #define snd_soc_component_module_put_when_remove(component)     \
335         snd_soc_component_module_put(component, 0)
336 #define snd_soc_component_module_put_when_close(component)      \
337         snd_soc_component_module_put(component, 1)
338 void snd_soc_component_module_put(struct snd_soc_component *component,
339                                   int upon_open);
340
341 static inline void snd_soc_component_set_drvdata(struct snd_soc_component *c,
342                                                  void *data)
343 {
344         dev_set_drvdata(c->dev, data);
345 }
346
347 static inline void *snd_soc_component_get_drvdata(struct snd_soc_component *c)
348 {
349         return dev_get_drvdata(c->dev);
350 }
351
352 static inline bool snd_soc_component_is_active(
353         struct snd_soc_component *component)
354 {
355         return component->active != 0;
356 }
357
358 /* component pin */
359 int snd_soc_component_enable_pin(struct snd_soc_component *component,
360                                  const char *pin);
361 int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
362                                           const char *pin);
363 int snd_soc_component_disable_pin(struct snd_soc_component *component,
364                                   const char *pin);
365 int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
366                                            const char *pin);
367 int snd_soc_component_nc_pin(struct snd_soc_component *component,
368                              const char *pin);
369 int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
370                                       const char *pin);
371 int snd_soc_component_get_pin_status(struct snd_soc_component *component,
372                                      const char *pin);
373 int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
374                                        const char *pin);
375 int snd_soc_component_force_enable_pin_unlocked(
376         struct snd_soc_component *component,
377         const char *pin);
378
379 /* component driver ops */
380 int snd_soc_component_open(struct snd_soc_component *component,
381                            struct snd_pcm_substream *substream);
382 int snd_soc_component_close(struct snd_soc_component *component,
383                             struct snd_pcm_substream *substream);
384 int snd_soc_component_prepare(struct snd_soc_component *component,
385                               struct snd_pcm_substream *substream);
386 int snd_soc_component_hw_params(struct snd_soc_component *component,
387                                 struct snd_pcm_substream *substream,
388                                 struct snd_pcm_hw_params *params);
389 int snd_soc_component_hw_free(struct snd_soc_component *component,
390                               struct snd_pcm_substream *substream);
391 int snd_soc_component_trigger(struct snd_soc_component *component,
392                               struct snd_pcm_substream *substream,
393                               int cmd);
394 void snd_soc_component_suspend(struct snd_soc_component *component);
395 void snd_soc_component_resume(struct snd_soc_component *component);
396 int snd_soc_component_is_suspended(struct snd_soc_component *component);
397 int snd_soc_component_probe(struct snd_soc_component *component);
398 void snd_soc_component_remove(struct snd_soc_component *component);
399 int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
400                                       struct device_node *ep);
401 int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
402                                         struct of_phandle_args *args,
403                                         const char **dai_name);
404
405 int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream);
406 int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
407                                 unsigned int cmd, void *arg);
408 int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
409                                     int channel, unsigned long pos,
410                                     void __user *buf, unsigned long bytes);
411 struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
412                                         unsigned long offset);
413 int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
414                                struct vm_area_struct *vma);
415 int snd_soc_pcm_component_new(struct snd_pcm *pcm);
416 void snd_soc_pcm_component_free(struct snd_pcm *pcm);
417
418 #endif /* __SOC_COMPONENT_H */