[ALSA] soc - Add device level DAPM event
[safe/jmp/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 #define SND_SOC_VERSION "0.13.1"
25
26 /*
27  * Convenience kcontrol builders
28  */
29 #define SOC_SINGLE_VALUE(reg,shift,mask,invert) ((reg) | ((shift) << 8) |\
30         ((shift) << 12) | ((mask) << 16) | ((invert) << 24))
31 #define SOC_SINGLE_VALUE_EXT(reg,mask,invert) ((reg) | ((mask) << 16) |\
32         ((invert) << 31))
33 #define SOC_SINGLE(xname, reg, shift, mask, invert) \
34 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
35         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
36         .put = snd_soc_put_volsw, \
37         .private_value =  SOC_SINGLE_VALUE(reg, shift, mask, invert) }
38 #define SOC_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
39 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
40         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw, \
41         .put = snd_soc_put_volsw, \
42         .private_value = (reg) | ((shift_left) << 8) | \
43                 ((shift_right) << 12) | ((mask) << 16) | ((invert) << 24) }
44 #define SOC_DOUBLE_R(xname, reg_left, reg_right, shift, mask, invert) \
45 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
46         .info = snd_soc_info_volsw_2r, \
47         .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r, \
48         .private_value = (reg_left) | ((shift) << 8)  | \
49                 ((mask) << 12) | ((invert) << 20) | ((reg_right) << 24) }
50 #define SOC_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmask, xtexts) \
51 {       .reg = xreg, .shift_l = xshift_l, .shift_r = xshift_r, \
52         .mask = xmask, .texts = xtexts }
53 #define SOC_ENUM_SINGLE(xreg, xshift, xmask, xtexts) \
54         SOC_ENUM_DOUBLE(xreg, xshift, xshift, xmask, xtexts)
55 #define SOC_ENUM_SINGLE_EXT(xmask, xtexts) \
56 {       .mask = xmask, .texts = xtexts }
57 #define SOC_ENUM(xname, xenum) \
58 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,\
59         .info = snd_soc_info_enum_double, \
60         .get = snd_soc_get_enum_double, .put = snd_soc_put_enum_double, \
61         .private_value = (unsigned long)&xenum }
62 #define SOC_SINGLE_EXT(xname, xreg, xshift, xmask, xinvert,\
63          xhandler_get, xhandler_put) \
64 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
65         .info = snd_soc_info_volsw, \
66         .get = xhandler_get, .put = xhandler_put, \
67         .private_value = SOC_SINGLE_VALUE(xreg, xshift, xmask, xinvert) }
68 #define SOC_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
69 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
70         .info = snd_soc_info_bool_ext, \
71         .get = xhandler_get, .put = xhandler_put, \
72         .private_value = xdata }
73 #define SOC_ENUM_EXT(xname, xenum, xhandler_get, xhandler_put) \
74 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
75         .info = snd_soc_info_enum_ext, \
76         .get = xhandler_get, .put = xhandler_put, \
77         .private_value = (unsigned long)&xenum }
78
79 /*
80  * Digital Audio Interface (DAI) types
81  */
82 #define SND_SOC_DAI_AC97        0x1
83 #define SND_SOC_DAI_I2S         0x2
84 #define SND_SOC_DAI_PCM         0x4
85 #define SND_SOC_DAI_AC97_BUS    0x8     /* for custom i.e. non ac97_codec.c */
86
87 /*
88  * DAI hardware audio formats
89  */
90 #define SND_SOC_DAIFMT_I2S              0       /* I2S mode */
91 #define SND_SOC_DAIFMT_RIGHT_J  1       /* Right justified mode */
92 #define SND_SOC_DAIFMT_LEFT_J   2       /* Left Justified mode */
93 #define SND_SOC_DAIFMT_DSP_A    3       /* L data msb after FRM or LRC */
94 #define SND_SOC_DAIFMT_DSP_B    4       /* L data msb during FRM or LRC */
95 #define SND_SOC_DAIFMT_AC97             5       /* AC97 */
96
97 #define SND_SOC_DAIFMT_MSB      SND_SOC_DAIFMT_LEFT_J
98 #define SND_SOC_DAIFMT_LSB      SND_SOC_DAIFMT_RIGHT_J
99
100 /*
101  * DAI Gating
102  */
103 #define SND_SOC_DAIFMT_CONT                     (0 << 4)        /* continuous clock */
104 #define SND_SOC_DAIFMT_GATED            (1 << 4)        /* clock is gated when not Tx/Rx */
105
106 /*
107  * DAI hardware signal inversions
108  */
109 #define SND_SOC_DAIFMT_NB_NF            (0 << 8)        /* normal bit clock + frame */
110 #define SND_SOC_DAIFMT_NB_IF            (1 << 8)        /* normal bclk + inv frm */
111 #define SND_SOC_DAIFMT_IB_NF            (2 << 8)        /* invert bclk + nor frm */
112 #define SND_SOC_DAIFMT_IB_IF            (3 << 8)        /* invert bclk + frm */
113
114 /*
115  * DAI hardware clock masters
116  * This is wrt the codec, the inverse is true for the interface
117  * i.e. if the codec is clk and frm master then the interface is
118  * clk and frame slave.
119  */
120 #define SND_SOC_DAIFMT_CBM_CFM  (0 << 12) /* codec clk & frm master */
121 #define SND_SOC_DAIFMT_CBS_CFM  (1 << 12) /* codec clk slave & frm master */
122 #define SND_SOC_DAIFMT_CBM_CFS  (2 << 12) /* codec clk master & frame slave */
123 #define SND_SOC_DAIFMT_CBS_CFS  (3 << 12) /* codec clk & frm slave */
124
125 #define SND_SOC_DAIFMT_FORMAT_MASK              0x000f
126 #define SND_SOC_DAIFMT_CLOCK_MASK               0x00f0
127 #define SND_SOC_DAIFMT_INV_MASK                 0x0f00
128 #define SND_SOC_DAIFMT_MASTER_MASK              0xf000
129
130
131 /*
132  * Master Clock Directions
133  */
134 #define SND_SOC_CLOCK_IN        0
135 #define SND_SOC_CLOCK_OUT       1
136
137 /*
138  * AC97 codec ID's bitmask
139  */
140 #define SND_SOC_DAI_AC97_ID0    (1 << 0)
141 #define SND_SOC_DAI_AC97_ID1    (1 << 1)
142 #define SND_SOC_DAI_AC97_ID2    (1 << 2)
143 #define SND_SOC_DAI_AC97_ID3    (1 << 3)
144
145 struct snd_soc_device;
146 struct snd_soc_pcm_stream;
147 struct snd_soc_ops;
148 struct snd_soc_dai_mode;
149 struct snd_soc_pcm_runtime;
150 struct snd_soc_codec_dai;
151 struct snd_soc_cpu_dai;
152 struct snd_soc_codec;
153 struct snd_soc_machine_config;
154 struct soc_enum;
155 struct snd_soc_ac97_ops;
156 struct snd_soc_clock_info;
157
158 typedef int (*hw_write_t)(void *,const char* ,int);
159 typedef int (*hw_read_t)(void *,char* ,int);
160
161 extern struct snd_ac97_bus_ops soc_ac97_ops;
162
163 /* pcm <-> DAI connect */
164 void snd_soc_free_pcms(struct snd_soc_device *socdev);
165 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid);
166 int snd_soc_register_card(struct snd_soc_device *socdev);
167
168 /* set runtime hw params */
169 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
170         const struct snd_pcm_hardware *hw);
171
172 /* codec IO */
173 #define snd_soc_read(codec, reg) codec->read(codec, reg)
174 #define snd_soc_write(codec, reg, value) codec->write(codec, reg, value)
175
176 /* codec register bit access */
177 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
178                                 unsigned short mask, unsigned short value);
179 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
180                                 unsigned short mask, unsigned short value);
181
182 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
183         struct snd_ac97_bus_ops *ops, int num);
184 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec);
185
186 /*
187  *Controls
188  */
189 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
190         void *data, char *long_name);
191 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
192         struct snd_ctl_elem_info *uinfo);
193 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
194         struct snd_ctl_elem_info *uinfo);
195 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
196         struct snd_ctl_elem_value *ucontrol);
197 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
198         struct snd_ctl_elem_value *ucontrol);
199 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
200         struct snd_ctl_elem_info *uinfo);
201 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
202         struct snd_ctl_elem_info *uinfo);
203 #define snd_soc_info_bool_ext           snd_ctl_boolean_mono
204 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
205         struct snd_ctl_elem_value *ucontrol);
206 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
207         struct snd_ctl_elem_value *ucontrol);
208 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
209         struct snd_ctl_elem_info *uinfo);
210 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
211         struct snd_ctl_elem_value *ucontrol);
212 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
213         struct snd_ctl_elem_value *ucontrol);
214
215 /* SoC PCM stream information */
216 struct snd_soc_pcm_stream {
217         char *stream_name;
218         u64 formats;                    /* SNDRV_PCM_FMTBIT_* */
219         unsigned int rates;             /* SNDRV_PCM_RATE_* */
220         unsigned int rate_min;          /* min rate */
221         unsigned int rate_max;          /* max rate */
222         unsigned int channels_min;      /* min channels */
223         unsigned int channels_max;      /* max channels */
224         unsigned int active:1;          /* stream is in use */
225 };
226
227 /* SoC audio ops */
228 struct snd_soc_ops {
229         int (*startup)(struct snd_pcm_substream *);
230         void (*shutdown)(struct snd_pcm_substream *);
231         int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
232         int (*hw_free)(struct snd_pcm_substream *);
233         int (*prepare)(struct snd_pcm_substream *);
234         int (*trigger)(struct snd_pcm_substream *, int);
235 };
236
237 /* ASoC codec DAI ops */
238 struct snd_soc_codec_ops {
239         /* codec DAI clocking configuration */
240         int (*set_sysclk)(struct snd_soc_codec_dai *codec_dai,
241                 int clk_id, unsigned int freq, int dir);
242         int (*set_pll)(struct snd_soc_codec_dai *codec_dai,
243                 int pll_id, unsigned int freq_in, unsigned int freq_out);
244         int (*set_clkdiv)(struct snd_soc_codec_dai *codec_dai,
245                 int div_id, int div);
246
247         /* CPU DAI format configuration */
248         int (*set_fmt)(struct snd_soc_codec_dai *codec_dai,
249                 unsigned int fmt);
250         int (*set_tdm_slot)(struct snd_soc_codec_dai *codec_dai,
251                 unsigned int mask, int slots);
252         int (*set_tristate)(struct snd_soc_codec_dai *, int tristate);
253
254         /* digital mute */
255         int (*digital_mute)(struct snd_soc_codec_dai *, int mute);
256 };
257
258 /* ASoC cpu DAI ops */
259 struct snd_soc_cpu_ops {
260         /* CPU DAI clocking configuration */
261         int (*set_sysclk)(struct snd_soc_cpu_dai *cpu_dai,
262                 int clk_id, unsigned int freq, int dir);
263         int (*set_clkdiv)(struct snd_soc_cpu_dai *cpu_dai,
264                 int div_id, int div);
265         int (*set_pll)(struct snd_soc_cpu_dai *cpu_dai,
266                 int pll_id, unsigned int freq_in, unsigned int freq_out);
267
268         /* CPU DAI format configuration */
269         int (*set_fmt)(struct snd_soc_cpu_dai *cpu_dai,
270                 unsigned int fmt);
271         int (*set_tdm_slot)(struct snd_soc_cpu_dai *cpu_dai,
272                 unsigned int mask, int slots);
273         int (*set_tristate)(struct snd_soc_cpu_dai *, int tristate);
274 };
275
276 /* SoC Codec DAI */
277 struct snd_soc_codec_dai {
278         char *name;
279         int id;
280         unsigned char type;
281
282         /* DAI capabilities */
283         struct snd_soc_pcm_stream playback;
284         struct snd_soc_pcm_stream capture;
285
286         /* DAI runtime info */
287         struct snd_soc_codec *codec;
288         unsigned int active;
289         unsigned char pop_wait:1;
290
291         /* ops */
292         struct snd_soc_ops ops;
293         struct snd_soc_codec_ops dai_ops;
294
295         /* DAI private data */
296         void *private_data;
297 };
298
299 /* SoC CPU DAI */
300 struct snd_soc_cpu_dai {
301
302         /* DAI description */
303         char *name;
304         unsigned int id;
305         unsigned char type;
306
307         /* DAI callbacks */
308         int (*probe)(struct platform_device *pdev);
309         void (*remove)(struct platform_device *pdev);
310         int (*suspend)(struct platform_device *pdev,
311                 struct snd_soc_cpu_dai *cpu_dai);
312         int (*resume)(struct platform_device *pdev,
313                 struct snd_soc_cpu_dai *cpu_dai);
314
315         /* ops */
316         struct snd_soc_ops ops;
317         struct snd_soc_cpu_ops dai_ops;
318
319         /* DAI capabilities */
320         struct snd_soc_pcm_stream capture;
321         struct snd_soc_pcm_stream playback;
322
323         /* DAI runtime info */
324         struct snd_pcm_runtime *runtime;
325         unsigned char active:1;
326         void *dma_data;
327
328         /* DAI private data */
329         void *private_data;
330 };
331
332 /* SoC Audio Codec */
333 struct snd_soc_codec {
334         char *name;
335         struct module *owner;
336         struct mutex mutex;
337
338         /* callbacks */
339         int (*dapm_event)(struct snd_soc_codec *codec, int event);
340
341         /* runtime */
342         struct snd_card *card;
343         struct snd_ac97 *ac97;  /* for ad-hoc ac97 devices */
344         unsigned int active;
345         unsigned int pcm_devs;
346         void *private_data;
347
348         /* codec IO */
349         void *control_data; /* codec control (i2c/3wire) data */
350         unsigned int (*read)(struct snd_soc_codec *, unsigned int);
351         int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
352         hw_write_t hw_write;
353         hw_read_t hw_read;
354         void *reg_cache;
355         short reg_cache_size;
356         short reg_cache_step;
357
358         /* dapm */
359         struct list_head dapm_widgets;
360         struct list_head dapm_paths;
361         unsigned int dapm_state;
362         unsigned int suspend_dapm_state;
363         struct delayed_work delayed_work;
364
365         /* codec DAI's */
366         struct snd_soc_codec_dai *dai;
367         unsigned int num_dai;
368 };
369
370 /* codec device */
371 struct snd_soc_codec_device {
372         int (*probe)(struct platform_device *pdev);
373         int (*remove)(struct platform_device *pdev);
374         int (*suspend)(struct platform_device *pdev, pm_message_t state);
375         int (*resume)(struct platform_device *pdev);
376 };
377
378 /* SoC platform interface */
379 struct snd_soc_platform {
380         char *name;
381
382         int (*probe)(struct platform_device *pdev);
383         int (*remove)(struct platform_device *pdev);
384         int (*suspend)(struct platform_device *pdev,
385                 struct snd_soc_cpu_dai *cpu_dai);
386         int (*resume)(struct platform_device *pdev,
387                 struct snd_soc_cpu_dai *cpu_dai);
388
389         /* pcm creation and destruction */
390         int (*pcm_new)(struct snd_card *, struct snd_soc_codec_dai *,
391                 struct snd_pcm *);
392         void (*pcm_free)(struct snd_pcm *);
393
394         /* platform stream ops */
395         struct snd_pcm_ops *pcm_ops;
396 };
397
398 /* SoC machine DAI configuration, glues a codec and cpu DAI together */
399 struct snd_soc_dai_link  {
400         char *name;                     /* Codec name */
401         char *stream_name;              /* Stream name */
402
403         /* DAI */
404         struct snd_soc_codec_dai *codec_dai;
405         struct snd_soc_cpu_dai *cpu_dai;
406
407         /* machine stream operations */
408         struct snd_soc_ops *ops;
409
410         /* codec/machine specific init - e.g. add machine controls */
411         int (*init)(struct snd_soc_codec *codec);
412 };
413
414 /* SoC machine */
415 struct snd_soc_machine {
416         char *name;
417
418         int (*probe)(struct platform_device *pdev);
419         int (*remove)(struct platform_device *pdev);
420
421         /* the pre and post PM functions are used to do any PM work before and
422          * after the codec and DAI's do any PM work. */
423         int (*suspend_pre)(struct platform_device *pdev, pm_message_t state);
424         int (*suspend_post)(struct platform_device *pdev, pm_message_t state);
425         int (*resume_pre)(struct platform_device *pdev);
426         int (*resume_post)(struct platform_device *pdev);
427
428         /* callbacks */
429         int (*dapm_event)(struct snd_soc_machine *, int event);
430
431         /* CPU <--> Codec DAI links  */
432         struct snd_soc_dai_link *dai_link;
433         int num_links;
434 };
435
436 /* SoC Device - the audio subsystem */
437 struct snd_soc_device {
438         struct device *dev;
439         struct snd_soc_machine *machine;
440         struct snd_soc_platform *platform;
441         struct snd_soc_codec *codec;
442         struct snd_soc_codec_device *codec_dev;
443         struct delayed_work delayed_work;
444         void *codec_data;
445 };
446
447 /* runtime channel data */
448 struct snd_soc_pcm_runtime {
449         struct snd_soc_dai_link *dai;
450         struct snd_soc_device *socdev;
451 };
452
453 /* enumerated kcontrol */
454 struct soc_enum {
455         unsigned short reg;
456         unsigned short reg2;
457         unsigned char shift_l;
458         unsigned char shift_r;
459         unsigned int mask;
460         const char **texts;
461         void *dapm;
462 };
463
464 #endif