blob: a2898bdd0a3c576ec0ea398228eddcb572f157fd [file] [log] [blame]
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +09001/* SPDX-License-Identifier: GPL-2.0
2 *
3 * soc-component.h
4 *
5 * Copyright (c) 2019 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +09006 */
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 */
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +090028struct snd_compress_ops {
29 int (*open)(struct snd_soc_component *component,
30 struct snd_compr_stream *stream);
31 int (*free)(struct snd_soc_component *component,
32 struct snd_compr_stream *stream);
33 int (*set_params)(struct snd_soc_component *component,
34 struct snd_compr_stream *stream,
35 struct snd_compr_params *params);
36 int (*get_params)(struct snd_soc_component *component,
37 struct snd_compr_stream *stream,
38 struct snd_codec *params);
39 int (*set_metadata)(struct snd_soc_component *component,
40 struct snd_compr_stream *stream,
41 struct snd_compr_metadata *metadata);
42 int (*get_metadata)(struct snd_soc_component *component,
43 struct snd_compr_stream *stream,
44 struct snd_compr_metadata *metadata);
45 int (*trigger)(struct snd_soc_component *component,
46 struct snd_compr_stream *stream, int cmd);
47 int (*pointer)(struct snd_soc_component *component,
48 struct snd_compr_stream *stream,
49 struct snd_compr_tstamp *tstamp);
50 int (*copy)(struct snd_soc_component *component,
51 struct snd_compr_stream *stream, char __user *buf,
52 size_t count);
53 int (*mmap)(struct snd_soc_component *component,
54 struct snd_compr_stream *stream,
55 struct vm_area_struct *vma);
56 int (*ack)(struct snd_soc_component *component,
57 struct snd_compr_stream *stream, size_t bytes);
58 int (*get_caps)(struct snd_soc_component *component,
59 struct snd_compr_stream *stream,
60 struct snd_compr_caps *caps);
61 int (*get_codec_caps)(struct snd_soc_component *component,
62 struct snd_compr_stream *stream,
63 struct snd_compr_codec_caps *codec);
64};
65
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090066struct snd_soc_component_driver {
67 const char *name;
68
69 /* Default control and setup, added after probe() is run */
70 const struct snd_kcontrol_new *controls;
71 unsigned int num_controls;
72 const struct snd_soc_dapm_widget *dapm_widgets;
73 unsigned int num_dapm_widgets;
74 const struct snd_soc_dapm_route *dapm_routes;
75 unsigned int num_dapm_routes;
76
77 int (*probe)(struct snd_soc_component *component);
78 void (*remove)(struct snd_soc_component *component);
79 int (*suspend)(struct snd_soc_component *component);
80 int (*resume)(struct snd_soc_component *component);
81
82 unsigned int (*read)(struct snd_soc_component *component,
83 unsigned int reg);
84 int (*write)(struct snd_soc_component *component,
85 unsigned int reg, unsigned int val);
86
87 /* pcm creation and destruction */
Kuninori Morimotoc64bfc92019-10-02 14:30:59 +090088 int (*pcm_construct)(struct snd_soc_component *component,
89 struct snd_soc_pcm_runtime *rtd);
90 void (*pcm_destruct)(struct snd_soc_component *component,
91 struct snd_pcm *pcm);
92
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +090093 /* component wide operations */
94 int (*set_sysclk)(struct snd_soc_component *component,
95 int clk_id, int source, unsigned int freq, int dir);
96 int (*set_pll)(struct snd_soc_component *component, int pll_id,
97 int source, unsigned int freq_in, unsigned int freq_out);
98 int (*set_jack)(struct snd_soc_component *component,
99 struct snd_soc_jack *jack, void *data);
100
101 /* DT */
102 int (*of_xlate_dai_name)(struct snd_soc_component *component,
103 struct of_phandle_args *args,
104 const char **dai_name);
105 int (*of_xlate_dai_id)(struct snd_soc_component *comment,
106 struct device_node *endpoint);
107 void (*seq_notifier)(struct snd_soc_component *component,
108 enum snd_soc_dapm_type type, int subseq);
109 int (*stream_event)(struct snd_soc_component *component, int event);
110 int (*set_bias_level)(struct snd_soc_component *component,
111 enum snd_soc_bias_level level);
112
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900113 int (*open)(struct snd_soc_component *component,
114 struct snd_pcm_substream *substream);
115 int (*close)(struct snd_soc_component *component,
116 struct snd_pcm_substream *substream);
117 int (*ioctl)(struct snd_soc_component *component,
118 struct snd_pcm_substream *substream,
119 unsigned int cmd, void *arg);
120 int (*hw_params)(struct snd_soc_component *component,
121 struct snd_pcm_substream *substream,
122 struct snd_pcm_hw_params *params);
123 int (*hw_free)(struct snd_soc_component *component,
124 struct snd_pcm_substream *substream);
125 int (*prepare)(struct snd_soc_component *component,
126 struct snd_pcm_substream *substream);
127 int (*trigger)(struct snd_soc_component *component,
128 struct snd_pcm_substream *substream, int cmd);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100129 int (*sync_stop)(struct snd_soc_component *component,
130 struct snd_pcm_substream *substream);
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900131 snd_pcm_uframes_t (*pointer)(struct snd_soc_component *component,
132 struct snd_pcm_substream *substream);
133 int (*get_time_info)(struct snd_soc_component *component,
Baolin Wangfcae40c2018-04-24 20:06:08 +0800134 struct snd_pcm_substream *substream, struct timespec64 *system_ts,
135 struct timespec64 *audio_ts,
Kuninori Morimotoe2cb4a12019-10-02 14:30:48 +0900136 struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
137 struct snd_pcm_audio_tstamp_report *audio_tstamp_report);
138 int (*copy_user)(struct snd_soc_component *component,
139 struct snd_pcm_substream *substream, int channel,
140 unsigned long pos, void __user *buf,
141 unsigned long bytes);
142 struct page *(*page)(struct snd_soc_component *component,
143 struct snd_pcm_substream *substream,
144 unsigned long offset);
145 int (*mmap)(struct snd_soc_component *component,
146 struct snd_pcm_substream *substream,
147 struct vm_area_struct *vma);
148
Kuninori Morimotoc6cb5222020-04-20 16:07:50 +0900149 const struct snd_compress_ops *compress_ops;
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900150
151 /* probe ordering - for components with runtime dependencies */
152 int probe_order;
153 int remove_order;
154
155 /*
156 * signal if the module handling the component should not be removed
157 * if a pcm is open. Setting this would prevent the module
158 * refcount being incremented in probe() but allow it be incremented
159 * when a pcm is opened and decremented when it is closed.
160 */
161 unsigned int module_get_upon_open:1;
162
163 /* bits */
164 unsigned int idle_bias_on:1;
165 unsigned int suspend_bias_off:1;
166 unsigned int use_pmdown_time:1; /* care pmdown_time at stop */
167 unsigned int endianness:1;
168 unsigned int non_legacy_dai_naming:1;
169
170 /* this component uses topology and ignore machine driver FEs */
171 const char *ignore_machine;
172 const char *topology_name_prefix;
173 int (*be_hw_params_fixup)(struct snd_soc_pcm_runtime *rtd,
174 struct snd_pcm_hw_params *params);
175 bool use_dai_pcm_id; /* use DAI link PCM ID as PCM device number */
176 int be_pcm_base; /* base device ID for all BE PCMs */
177};
178
179struct snd_soc_component {
180 const char *name;
181 int id;
182 const char *name_prefix;
183 struct device *dev;
184 struct snd_soc_card *card;
185
186 unsigned int active;
187
188 unsigned int suspended:1; /* is in suspend PM state */
189
190 struct list_head list;
191 struct list_head card_aux_list; /* for auxiliary bound components */
192 struct list_head card_list;
193
194 const struct snd_soc_component_driver *driver;
195
196 struct list_head dai_list;
197 int num_dai;
198
199 struct regmap *regmap;
200 int val_bytes;
201
202 struct mutex io_mutex;
203
204 /* attached dynamic objects */
205 struct list_head dobj_list;
206
207 /*
208 * DO NOT use any of the fields below in drivers, they are temporary and
209 * are going to be removed again soon. If you use them in driver code
210 * the driver will be marked as BROKEN when these fields are removed.
211 */
212
213 /* Don't use these, use snd_soc_component_get_dapm() */
214 struct snd_soc_dapm_context dapm;
215
216 /* machine specific init */
217 int (*init)(struct snd_soc_component *component);
218
219#ifdef CONFIG_DEBUG_FS
220 struct dentry *debugfs_root;
221 const char *debugfs_prefix;
222#endif
223};
224
225#define for_each_component_dais(component, dai)\
226 list_for_each_entry(dai, &(component)->dai_list, list)
227#define for_each_component_dais_safe(component, dai, _dai)\
228 list_for_each_entry_safe(dai, _dai, &(component)->dai_list, list)
229
230/**
231 * snd_soc_dapm_to_component() - Casts a DAPM context to the component it is
232 * embedded in
233 * @dapm: The DAPM context to cast to the component
234 *
235 * This function must only be used on DAPM contexts that are known to be part of
236 * a component (e.g. in a component driver). Otherwise the behavior is
237 * undefined.
238 */
239static inline struct snd_soc_component *snd_soc_dapm_to_component(
240 struct snd_soc_dapm_context *dapm)
241{
242 return container_of(dapm, struct snd_soc_component, dapm);
243}
244
245/**
246 * snd_soc_component_get_dapm() - Returns the DAPM context associated with a
247 * component
248 * @component: The component for which to get the DAPM context
249 */
250static inline struct snd_soc_dapm_context *snd_soc_component_get_dapm(
251 struct snd_soc_component *component)
252{
253 return &component->dapm;
254}
255
256/**
257 * snd_soc_component_init_bias_level() - Initialize COMPONENT DAPM bias level
258 * @component: The COMPONENT for which to initialize the DAPM bias level
259 * @level: The DAPM level to initialize to
260 *
261 * Initializes the COMPONENT DAPM bias level. See snd_soc_dapm_init_bias_level()
262 */
263static inline void
264snd_soc_component_init_bias_level(struct snd_soc_component *component,
265 enum snd_soc_bias_level level)
266{
267 snd_soc_dapm_init_bias_level(
268 snd_soc_component_get_dapm(component), level);
269}
270
271/**
272 * snd_soc_component_get_bias_level() - Get current COMPONENT DAPM bias level
273 * @component: The COMPONENT for which to get the DAPM bias level
274 *
275 * Returns: The current DAPM bias level of the COMPONENT.
276 */
277static inline enum snd_soc_bias_level
278snd_soc_component_get_bias_level(struct snd_soc_component *component)
279{
280 return snd_soc_dapm_get_bias_level(
281 snd_soc_component_get_dapm(component));
282}
283
284/**
285 * snd_soc_component_force_bias_level() - Set the COMPONENT DAPM bias level
286 * @component: The COMPONENT for which to set the level
287 * @level: The level to set to
288 *
289 * Forces the COMPONENT bias level to a specific state. See
290 * snd_soc_dapm_force_bias_level().
291 */
292static inline int
293snd_soc_component_force_bias_level(struct snd_soc_component *component,
294 enum snd_soc_bias_level level)
295{
296 return snd_soc_dapm_force_bias_level(
297 snd_soc_component_get_dapm(component),
298 level);
299}
300
301/**
302 * snd_soc_dapm_kcontrol_component() - Returns the component associated to a
303 * kcontrol
304 * @kcontrol: The kcontrol
305 *
306 * This function must only be used on DAPM contexts that are known to be part of
307 * a COMPONENT (e.g. in a COMPONENT driver). Otherwise the behavior is undefined
308 */
309static inline struct snd_soc_component *snd_soc_dapm_kcontrol_component(
310 struct snd_kcontrol *kcontrol)
311{
312 return snd_soc_dapm_to_component(snd_soc_dapm_kcontrol_dapm(kcontrol));
313}
314
315/**
316 * snd_soc_component_cache_sync() - Sync the register cache with the hardware
317 * @component: COMPONENT to sync
318 *
319 * Note: This function will call regcache_sync()
320 */
321static inline int snd_soc_component_cache_sync(
322 struct snd_soc_component *component)
323{
324 return regcache_sync(component->regmap);
325}
326
Kuninori Morimoto536aba12020-06-04 17:06:32 +0900327int snd_soc_component_initialize(struct snd_soc_component *component,
328 const struct snd_soc_component_driver *driver,
329 struct device *dev, const char *name);
330
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900331/* component IO */
332int snd_soc_component_read(struct snd_soc_component *component,
333 unsigned int reg, unsigned int *val);
334unsigned int snd_soc_component_read32(struct snd_soc_component *component,
335 unsigned int reg);
336int snd_soc_component_write(struct snd_soc_component *component,
337 unsigned int reg, unsigned int val);
338int snd_soc_component_update_bits(struct snd_soc_component *component,
339 unsigned int reg, unsigned int mask,
340 unsigned int val);
341int snd_soc_component_update_bits_async(struct snd_soc_component *component,
342 unsigned int reg, unsigned int mask,
343 unsigned int val);
344void snd_soc_component_async_complete(struct snd_soc_component *component);
345int snd_soc_component_test_bits(struct snd_soc_component *component,
346 unsigned int reg, unsigned int mask,
347 unsigned int value);
348
349/* component wide operations */
350int snd_soc_component_set_sysclk(struct snd_soc_component *component,
351 int clk_id, int source,
352 unsigned int freq, int dir);
353int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
354 int source, unsigned int freq_in,
355 unsigned int freq_out);
356int snd_soc_component_set_jack(struct snd_soc_component *component,
357 struct snd_soc_jack *jack, void *data);
358
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +0900359void snd_soc_component_seq_notifier(struct snd_soc_component *component,
360 enum snd_soc_dapm_type type, int subseq);
Kuninori Morimoto8e2a9902019-07-26 13:51:39 +0900361int snd_soc_component_stream_event(struct snd_soc_component *component,
362 int event);
Kuninori Morimoto7951b1462019-07-26 13:51:43 +0900363int snd_soc_component_set_bias_level(struct snd_soc_component *component,
364 enum snd_soc_bias_level level);
Kuninori Morimoto9d415fb2019-07-26 13:51:35 +0900365
Kuninori Morimotoc7d75b52020-06-04 17:06:22 +0900366void snd_soc_component_setup_regmap(struct snd_soc_component *component);
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900367#ifdef CONFIG_REGMAP
368void snd_soc_component_init_regmap(struct snd_soc_component *component,
369 struct regmap *regmap);
370void snd_soc_component_exit_regmap(struct snd_soc_component *component);
371#endif
372
Kuninori Morimoto4a81e8f2019-07-26 13:49:54 +0900373#define snd_soc_component_module_get_when_probe(component)\
374 snd_soc_component_module_get(component, 0)
375#define snd_soc_component_module_get_when_open(component) \
376 snd_soc_component_module_get(component, 1)
377int snd_soc_component_module_get(struct snd_soc_component *component,
378 int upon_open);
379#define snd_soc_component_module_put_when_remove(component) \
380 snd_soc_component_module_put(component, 0)
381#define snd_soc_component_module_put_when_close(component) \
382 snd_soc_component_module_put(component, 1)
383void snd_soc_component_module_put(struct snd_soc_component *component,
384 int upon_open);
385
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900386static inline void snd_soc_component_set_drvdata(struct snd_soc_component *c,
387 void *data)
388{
389 dev_set_drvdata(c->dev, data);
390}
391
392static inline void *snd_soc_component_get_drvdata(struct snd_soc_component *c)
393{
394 return dev_get_drvdata(c->dev);
395}
396
Kuninori Morimoto488b2ca2020-05-15 09:46:42 +0900397static inline unsigned int
398snd_soc_component_active(struct snd_soc_component *component)
399{
400 return component->active;
401}
402
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900403/* component pin */
404int snd_soc_component_enable_pin(struct snd_soc_component *component,
405 const char *pin);
406int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
407 const char *pin);
408int snd_soc_component_disable_pin(struct snd_soc_component *component,
409 const char *pin);
410int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
411 const char *pin);
412int snd_soc_component_nc_pin(struct snd_soc_component *component,
413 const char *pin);
414int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
415 const char *pin);
416int snd_soc_component_get_pin_status(struct snd_soc_component *component,
417 const char *pin);
418int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
419 const char *pin);
420int snd_soc_component_force_enable_pin_unlocked(
421 struct snd_soc_component *component,
422 const char *pin);
423
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900424/* component driver ops */
425int snd_soc_component_open(struct snd_soc_component *component,
426 struct snd_pcm_substream *substream);
Kuninori Morimoto3672beb2019-07-26 13:50:07 +0900427int snd_soc_component_close(struct snd_soc_component *component,
428 struct snd_pcm_substream *substream);
Kuninori Morimotoeae71362019-07-26 13:50:24 +0900429int snd_soc_component_hw_free(struct snd_soc_component *component,
430 struct snd_pcm_substream *substream);
Kuninori Morimoto5693d502019-07-26 13:50:29 +0900431int snd_soc_component_trigger(struct snd_soc_component *component,
432 struct snd_pcm_substream *substream,
433 int cmd);
Kuninori Morimoto66c51572019-07-26 13:50:34 +0900434void snd_soc_component_suspend(struct snd_soc_component *component);
Kuninori Morimoto9a840cb2019-07-26 13:51:08 +0900435void snd_soc_component_resume(struct snd_soc_component *component);
Kuninori Morimotoe40fadb2019-07-26 13:51:13 +0900436int snd_soc_component_is_suspended(struct snd_soc_component *component);
Kuninori Morimoto08e837d2019-07-26 13:51:17 +0900437int snd_soc_component_probe(struct snd_soc_component *component);
Kuninori Morimoto03b34dd2019-07-26 13:51:22 +0900438void snd_soc_component_remove(struct snd_soc_component *component);
Kuninori Morimoto2c7b1702019-07-26 13:51:26 +0900439int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
440 struct device_node *ep);
Kuninori Morimotoa2a34172019-07-26 13:51:31 +0900441int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
442 struct of_phandle_args *args,
443 const char **dai_name);
Kuninori Morimotoae2f4842019-07-26 13:50:01 +0900444
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900445int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream);
Kuninori Morimoto96a47902019-07-26 13:51:51 +0900446int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
447 unsigned int cmd, void *arg);
Takashi Iwai1e5ddb62019-11-21 20:07:09 +0100448int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream);
Kuninori Morimoto82d81f52019-07-26 13:51:56 +0900449int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
450 int channel, unsigned long pos,
451 void __user *buf, unsigned long bytes);
Kuninori Morimoto9c712e42019-07-26 13:52:00 +0900452struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
453 unsigned long offset);
Kuninori Morimoto205875e2019-07-26 13:52:04 +0900454int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
455 struct vm_area_struct *vma);
Kuninori Morimotob2b2afb2019-11-18 10:50:32 +0900456int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd);
457void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd);
Kuninori Morimoto4f395142020-06-04 17:06:58 +0900458int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream);
Kuninori Morimotoe1bafa82020-06-04 17:07:11 +0900459int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
460 struct snd_pcm_hw_params *params,
461 struct snd_soc_component **last);
Kuninori Morimoto0035e252019-07-26 13:51:47 +0900462
Kuninori Morimoto4ff1fef2019-07-26 13:49:48 +0900463#endif /* __SOC_COMPONENT_H */