ASoC: Add platform registration API
[linux-2.6] / include / sound / soc.h
1 /*
2  * linux/sound/soc.h -- ALSA SoC Layer
3  *
4  * Author:              Liam Girdwood
5  * Created:             Aug 11th 2005
6  * Copyright:   Wolfson Microelectronics. PLC.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #ifndef __LINUX_SND_SOC_H
14 #define __LINUX_SND_SOC_H
15
16 #include <linux/platform_device.h>
17 #include <linux/types.h>
18 #include <linux/workqueue.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/control.h>
22 #include <sound/ac97_codec.h>
23
24 /*
25  * Convenience kcontrol builders
26  */
27 #define SOC_SINGLE_VALUE(xreg, xshift, xmax, xinvert) \
28         ((unsigned long)&(struct soc_mixer_control) \
29         {.reg = xreg, .shift = xshift, .rshift = xshift, .max = xmax, \
30         .invert = xinvert})
31 #define SOC_SINGLE_VALUE_EXT(xreg, xmax, xinvert) \
32         ((unsigned long)&(struct soc_mixer_control) \
33         {.reg = xreg, .max = xmax, .invert = xinvert})
34 #define SOC_SINGLE(xname, reg, shift, max, invert) \
35 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
36         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
37         .put = snd_soc_put_volsw, \
38         .private_value =  SOC_SINGLE_VALUE(reg, shift, max, invert) }
39 #define SOC_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \
40 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
41         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
42                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
43         .tlv.p = (tlv_array), \
44         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
45         .put = snd_soc_put_volsw, \
46         .private_value =  SOC_SINGLE_VALUE(reg, shift, max, invert) }
47 #define SOC_DOUBLE(xname, xreg, shift_left, shift_right, xmax, xinvert) \
48 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
49         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw, \
50         .put = snd_soc_put_volsw, \
51         .private_value = (unsigned long)&(struct soc_mixer_control) \
52                 {.reg = xreg, .shift = shift_left, .rshift = shift_right, \
53                  .max = xmax, .invert = xinvert} }
54 #define SOC_DOUBLE_R(xname, reg_left, reg_right, xshift, xmax, xinvert) \
55 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
56         .info = snd_soc_info_volsw_2r, \
57         .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r, \
58         .private_value = (unsigned long)&(struct soc_mixer_control) \
59                 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
60                 .max = xmax, .invert = xinvert} }
61 #define SOC_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax, xinvert, tlv_array) \
62 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
63         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
64                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
65         .tlv.p = (tlv_array), \
66         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw, \
67         .put = snd_soc_put_volsw, \
68         .private_value = (unsigned long)&(struct soc_mixer_control) \
69                 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
70                  .max = xmax, .invert = xinvert} }
71 #define SOC_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax, xinvert, tlv_array) \
72 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
73         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
74                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
75         .tlv.p = (tlv_array), \
76         .info = snd_soc_info_volsw_2r, \
77         .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r, \
78         .private_value = (unsigned long)&(struct soc_mixer_control) \
79                 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
80                 .max = xmax, .invert = xinvert} }
81 #define SOC_DOUBLE_S8_TLV(xname, xreg, xmin, xmax, tlv_array) \
82 {       .iface  = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
83         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
84                   SNDRV_CTL_ELEM_ACCESS_READWRITE, \
85         .tlv.p  = (tlv_array), \
86         .info   = snd_soc_info_volsw_s8, .get = snd_soc_get_volsw_s8, \
87         .put    = snd_soc_put_volsw_s8, \
88         .private_value = (unsigned long)&(struct soc_mixer_control) \
89                 {.reg = xreg, .min = xmin, .max = xmax} }
90 #define SOC_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmax, xtexts) \
91 {       .reg = xreg, .shift_l = xshift_l, .shift_r = xshift_r, \
92         .max = xmax, .texts = xtexts }
93 #define SOC_ENUM_SINGLE(xreg, xshift, xmax, xtexts) \
94         SOC_ENUM_DOUBLE(xreg, xshift, xshift, xmax, xtexts)
95 #define SOC_ENUM_SINGLE_EXT(xmax, xtexts) \
96 {       .max = xmax, .texts = xtexts }
97 #define SOC_ENUM(xname, xenum) \
98 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,\
99         .info = snd_soc_info_enum_double, \
100         .get = snd_soc_get_enum_double, .put = snd_soc_put_enum_double, \
101         .private_value = (unsigned long)&xenum }
102 #define SOC_SINGLE_EXT(xname, xreg, xshift, xmax, xinvert,\
103          xhandler_get, xhandler_put) \
104 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
105         .info = snd_soc_info_volsw, \
106         .get = xhandler_get, .put = xhandler_put, \
107         .private_value = SOC_SINGLE_VALUE(xreg, xshift, xmax, xinvert) }
108 #define SOC_SINGLE_EXT_TLV(xname, xreg, xshift, xmax, xinvert,\
109          xhandler_get, xhandler_put, tlv_array) \
110 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
111         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
112                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
113         .tlv.p = (tlv_array), \
114         .info = snd_soc_info_volsw, \
115         .get = xhandler_get, .put = xhandler_put, \
116         .private_value = SOC_SINGLE_VALUE(xreg, xshift, xmax, xinvert) }
117 #define SOC_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
118 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
119         .info = snd_soc_info_bool_ext, \
120         .get = xhandler_get, .put = xhandler_put, \
121         .private_value = xdata }
122 #define SOC_ENUM_EXT(xname, xenum, xhandler_get, xhandler_put) \
123 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
124         .info = snd_soc_info_enum_ext, \
125         .get = xhandler_get, .put = xhandler_put, \
126         .private_value = (unsigned long)&xenum }
127
128 /*
129  * Bias levels
130  *
131  * @ON:      Bias is fully on for audio playback and capture operations.
132  * @PREPARE: Prepare for audio operations. Called before DAPM switching for
133  *           stream start and stop operations.
134  * @STANDBY: Low power standby state when no playback/capture operations are
135  *           in progress. NOTE: The transition time between STANDBY and ON
136  *           should be as fast as possible and no longer than 10ms.
137  * @OFF:     Power Off. No restrictions on transition times.
138  */
139 enum snd_soc_bias_level {
140         SND_SOC_BIAS_ON,
141         SND_SOC_BIAS_PREPARE,
142         SND_SOC_BIAS_STANDBY,
143         SND_SOC_BIAS_OFF,
144 };
145
146 struct snd_soc_device;
147 struct snd_soc_pcm_stream;
148 struct snd_soc_ops;
149 struct snd_soc_dai_mode;
150 struct snd_soc_pcm_runtime;
151 struct snd_soc_dai;
152 struct snd_soc_platform;
153 struct snd_soc_codec;
154 struct soc_enum;
155 struct snd_soc_ac97_ops;
156
157 typedef int (*hw_write_t)(void *,const char* ,int);
158 typedef int (*hw_read_t)(void *,char* ,int);
159
160 extern struct snd_ac97_bus_ops soc_ac97_ops;
161
162 int snd_soc_register_platform(struct snd_soc_platform *platform);
163 void snd_soc_unregister_platform(struct snd_soc_platform *platform);
164
165 /* pcm <-> DAI connect */
166 void snd_soc_free_pcms(struct snd_soc_device *socdev);
167 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid);
168 int snd_soc_init_card(struct snd_soc_device *socdev);
169
170 /* set runtime hw params */
171 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
172         const struct snd_pcm_hardware *hw);
173
174 /* codec IO */
175 #define snd_soc_read(codec, reg) codec->read(codec, reg)
176 #define snd_soc_write(codec, reg, value) codec->write(codec, reg, value)
177
178 /* codec register bit access */
179 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
180                                 unsigned short mask, unsigned short value);
181 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
182                                 unsigned short mask, unsigned short value);
183
184 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
185         struct snd_ac97_bus_ops *ops, int num);
186 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec);
187
188 /*
189  *Controls
190  */
191 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
192         void *data, char *long_name);
193 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
194         struct snd_ctl_elem_info *uinfo);
195 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
196         struct snd_ctl_elem_info *uinfo);
197 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
198         struct snd_ctl_elem_value *ucontrol);
199 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
200         struct snd_ctl_elem_value *ucontrol);
201 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
202         struct snd_ctl_elem_info *uinfo);
203 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
204         struct snd_ctl_elem_info *uinfo);
205 #define snd_soc_info_bool_ext           snd_ctl_boolean_mono_info
206 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
207         struct snd_ctl_elem_value *ucontrol);
208 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
209         struct snd_ctl_elem_value *ucontrol);
210 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
211         struct snd_ctl_elem_info *uinfo);
212 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
213         struct snd_ctl_elem_value *ucontrol);
214 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
215         struct snd_ctl_elem_value *ucontrol);
216 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
217         struct snd_ctl_elem_info *uinfo);
218 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
219         struct snd_ctl_elem_value *ucontrol);
220 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
221         struct snd_ctl_elem_value *ucontrol);
222
223 /* SoC PCM stream information */
224 struct snd_soc_pcm_stream {
225         char *stream_name;
226         u64 formats;                    /* SNDRV_PCM_FMTBIT_* */
227         unsigned int rates;             /* SNDRV_PCM_RATE_* */
228         unsigned int rate_min;          /* min rate */
229         unsigned int rate_max;          /* max rate */
230         unsigned int channels_min;      /* min channels */
231         unsigned int channels_max;      /* max channels */
232         unsigned int active:1;          /* stream is in use */
233 };
234
235 /* SoC audio ops */
236 struct snd_soc_ops {
237         int (*startup)(struct snd_pcm_substream *);
238         void (*shutdown)(struct snd_pcm_substream *);
239         int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
240         int (*hw_free)(struct snd_pcm_substream *);
241         int (*prepare)(struct snd_pcm_substream *);
242         int (*trigger)(struct snd_pcm_substream *, int);
243 };
244
245 /* SoC Audio Codec */
246 struct snd_soc_codec {
247         char *name;
248         struct module *owner;
249         struct mutex mutex;
250
251         /* callbacks */
252         int (*set_bias_level)(struct snd_soc_codec *,
253                               enum snd_soc_bias_level level);
254
255         /* runtime */
256         struct snd_card *card;
257         struct snd_ac97 *ac97;  /* for ad-hoc ac97 devices */
258         unsigned int active;
259         unsigned int pcm_devs;
260         void *private_data;
261
262         /* codec IO */
263         void *control_data; /* codec control (i2c/3wire) data */
264         unsigned int (*read)(struct snd_soc_codec *, unsigned int);
265         int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
266         int (*display_register)(struct snd_soc_codec *, char *,
267                                 size_t, unsigned int);
268         hw_write_t hw_write;
269         hw_read_t hw_read;
270         void *reg_cache;
271         short reg_cache_size;
272         short reg_cache_step;
273
274         /* dapm */
275         u32 pop_time;
276         struct list_head dapm_widgets;
277         struct list_head dapm_paths;
278         enum snd_soc_bias_level bias_level;
279         enum snd_soc_bias_level suspend_bias_level;
280         struct delayed_work delayed_work;
281
282         /* codec DAI's */
283         struct snd_soc_dai *dai;
284         unsigned int num_dai;
285
286 #ifdef CONFIG_DEBUG_FS
287         struct dentry *debugfs_reg;
288         struct dentry *debugfs_pop_time;
289 #endif
290 };
291
292 /* codec device */
293 struct snd_soc_codec_device {
294         int (*probe)(struct platform_device *pdev);
295         int (*remove)(struct platform_device *pdev);
296         int (*suspend)(struct platform_device *pdev, pm_message_t state);
297         int (*resume)(struct platform_device *pdev);
298 };
299
300 /* SoC platform interface */
301 struct snd_soc_platform {
302         char *name;
303         struct list_head list;
304
305         int (*probe)(struct platform_device *pdev);
306         int (*remove)(struct platform_device *pdev);
307         int (*suspend)(struct snd_soc_dai *dai);
308         int (*resume)(struct snd_soc_dai *dai);
309
310         /* pcm creation and destruction */
311         int (*pcm_new)(struct snd_card *, struct snd_soc_dai *,
312                 struct snd_pcm *);
313         void (*pcm_free)(struct snd_pcm *);
314
315         /* platform stream ops */
316         struct snd_pcm_ops *pcm_ops;
317 };
318
319 /* SoC machine DAI configuration, glues a codec and cpu DAI together */
320 struct snd_soc_dai_link  {
321         char *name;                     /* Codec name */
322         char *stream_name;              /* Stream name */
323
324         /* DAI */
325         struct snd_soc_dai *codec_dai;
326         struct snd_soc_dai *cpu_dai;
327
328         /* machine stream operations */
329         struct snd_soc_ops *ops;
330
331         /* codec/machine specific init - e.g. add machine controls */
332         int (*init)(struct snd_soc_codec *codec);
333
334         /* DAI pcm */
335         struct snd_pcm *pcm;
336 };
337
338 /* SoC card */
339 struct snd_soc_card {
340         char *name;
341         struct device *dev;
342
343         struct list_head list;
344
345         int instantiated;
346
347         int (*probe)(struct platform_device *pdev);
348         int (*remove)(struct platform_device *pdev);
349
350         /* the pre and post PM functions are used to do any PM work before and
351          * after the codec and DAI's do any PM work. */
352         int (*suspend_pre)(struct platform_device *pdev, pm_message_t state);
353         int (*suspend_post)(struct platform_device *pdev, pm_message_t state);
354         int (*resume_pre)(struct platform_device *pdev);
355         int (*resume_post)(struct platform_device *pdev);
356
357         /* callbacks */
358         int (*set_bias_level)(struct snd_soc_card *,
359                               enum snd_soc_bias_level level);
360
361         /* CPU <--> Codec DAI links  */
362         struct snd_soc_dai_link *dai_link;
363         int num_links;
364
365         struct snd_soc_device *socdev;
366
367         struct snd_soc_platform *platform;
368         struct delayed_work delayed_work;
369         struct work_struct deferred_resume_work;
370 };
371
372 /* SoC Device - the audio subsystem */
373 struct snd_soc_device {
374         struct device *dev;
375         struct snd_soc_card *card;
376         struct snd_soc_codec *codec;
377         struct snd_soc_codec_device *codec_dev;
378         void *codec_data;
379 };
380
381 /* runtime channel data */
382 struct snd_soc_pcm_runtime {
383         struct snd_soc_dai_link *dai;
384         struct snd_soc_device *socdev;
385 };
386
387 /* mixer control */
388 struct soc_mixer_control {
389         int min, max;
390         unsigned int reg, rreg, shift, rshift, invert;
391 };
392
393 /* enumerated kcontrol */
394 struct soc_enum {
395         unsigned short reg;
396         unsigned short reg2;
397         unsigned char shift_l;
398         unsigned char shift_r;
399         unsigned int max;
400         const char **texts;
401         void *dapm;
402 };
403
404 #include <sound/soc-dai.h>
405
406 #endif