ASoC: Begin to factor out register cache I/O functions
[pandora-kernel.git] / 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 <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/control.h>
24 #include <sound/ac97_codec.h>
25
26 /*
27  * Convenience kcontrol builders
28  */
29 #define SOC_SINGLE_VALUE(xreg, xshift, xmax, xinvert) \
30         ((unsigned long)&(struct soc_mixer_control) \
31         {.reg = xreg, .shift = xshift, .rshift = xshift, .max = xmax, \
32         .invert = xinvert})
33 #define SOC_SINGLE_VALUE_EXT(xreg, xmax, xinvert) \
34         ((unsigned long)&(struct soc_mixer_control) \
35         {.reg = xreg, .max = xmax, .invert = xinvert})
36 #define SOC_SINGLE(xname, reg, shift, max, invert) \
37 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
38         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
39         .put = snd_soc_put_volsw, \
40         .private_value =  SOC_SINGLE_VALUE(reg, shift, max, invert) }
41 #define SOC_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \
42 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
43         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
44                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
45         .tlv.p = (tlv_array), \
46         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
47         .put = snd_soc_put_volsw, \
48         .private_value =  SOC_SINGLE_VALUE(reg, shift, max, invert) }
49 #define SOC_DOUBLE(xname, xreg, shift_left, shift_right, xmax, xinvert) \
50 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
51         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw, \
52         .put = snd_soc_put_volsw, \
53         .private_value = (unsigned long)&(struct soc_mixer_control) \
54                 {.reg = xreg, .shift = shift_left, .rshift = shift_right, \
55                  .max = xmax, .invert = xinvert} }
56 #define SOC_DOUBLE_R(xname, reg_left, reg_right, xshift, xmax, xinvert) \
57 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
58         .info = snd_soc_info_volsw_2r, \
59         .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r, \
60         .private_value = (unsigned long)&(struct soc_mixer_control) \
61                 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
62                 .max = xmax, .invert = xinvert} }
63 #define SOC_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax, xinvert, tlv_array) \
64 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
65         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
66                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
67         .tlv.p = (tlv_array), \
68         .info = snd_soc_info_volsw, .get = snd_soc_get_volsw, \
69         .put = snd_soc_put_volsw, \
70         .private_value = (unsigned long)&(struct soc_mixer_control) \
71                 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
72                  .max = xmax, .invert = xinvert} }
73 #define SOC_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax, xinvert, tlv_array) \
74 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
75         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
76                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
77         .tlv.p = (tlv_array), \
78         .info = snd_soc_info_volsw_2r, \
79         .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r, \
80         .private_value = (unsigned long)&(struct soc_mixer_control) \
81                 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
82                 .max = xmax, .invert = xinvert} }
83 #define SOC_DOUBLE_S8_TLV(xname, xreg, xmin, xmax, tlv_array) \
84 {       .iface  = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
85         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
86                   SNDRV_CTL_ELEM_ACCESS_READWRITE, \
87         .tlv.p  = (tlv_array), \
88         .info   = snd_soc_info_volsw_s8, .get = snd_soc_get_volsw_s8, \
89         .put    = snd_soc_put_volsw_s8, \
90         .private_value = (unsigned long)&(struct soc_mixer_control) \
91                 {.reg = xreg, .min = xmin, .max = xmax} }
92 #define SOC_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmax, xtexts) \
93 {       .reg = xreg, .shift_l = xshift_l, .shift_r = xshift_r, \
94         .max = xmax, .texts = xtexts }
95 #define SOC_ENUM_SINGLE(xreg, xshift, xmax, xtexts) \
96         SOC_ENUM_DOUBLE(xreg, xshift, xshift, xmax, xtexts)
97 #define SOC_ENUM_SINGLE_EXT(xmax, xtexts) \
98 {       .max = xmax, .texts = xtexts }
99 #define SOC_VALUE_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmask, xmax, xtexts, xvalues) \
100 {       .reg = xreg, .shift_l = xshift_l, .shift_r = xshift_r, \
101         .mask = xmask, .max = xmax, .texts = xtexts, .values = xvalues}
102 #define SOC_VALUE_ENUM_SINGLE(xreg, xshift, xmask, xmax, xtexts, xvalues) \
103         SOC_VALUE_ENUM_DOUBLE(xreg, xshift, xshift, xmask, xmax, xtexts, xvalues)
104 #define SOC_ENUM(xname, xenum) \
105 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,\
106         .info = snd_soc_info_enum_double, \
107         .get = snd_soc_get_enum_double, .put = snd_soc_put_enum_double, \
108         .private_value = (unsigned long)&xenum }
109 #define SOC_VALUE_ENUM(xname, xenum) \
110 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,\
111         .info = snd_soc_info_enum_double, \
112         .get = snd_soc_get_value_enum_double, \
113         .put = snd_soc_put_value_enum_double, \
114         .private_value = (unsigned long)&xenum }
115 #define SOC_SINGLE_EXT(xname, xreg, xshift, xmax, xinvert,\
116          xhandler_get, xhandler_put) \
117 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
118         .info = snd_soc_info_volsw, \
119         .get = xhandler_get, .put = xhandler_put, \
120         .private_value = SOC_SINGLE_VALUE(xreg, xshift, xmax, xinvert) }
121 #define SOC_DOUBLE_EXT(xname, xreg, shift_left, shift_right, xmax, xinvert,\
122          xhandler_get, xhandler_put) \
123 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
124         .info = snd_soc_info_volsw, \
125         .get = xhandler_get, .put = xhandler_put, \
126         .private_value = (unsigned long)&(struct soc_mixer_control) \
127                 {.reg = xreg, .shift = shift_left, .rshift = shift_right, \
128                  .max = xmax, .invert = xinvert} }
129 #define SOC_SINGLE_EXT_TLV(xname, xreg, xshift, xmax, xinvert,\
130          xhandler_get, xhandler_put, tlv_array) \
131 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
132         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
133                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
134         .tlv.p = (tlv_array), \
135         .info = snd_soc_info_volsw, \
136         .get = xhandler_get, .put = xhandler_put, \
137         .private_value = SOC_SINGLE_VALUE(xreg, xshift, xmax, xinvert) }
138 #define SOC_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
139 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
140         .info = snd_soc_info_bool_ext, \
141         .get = xhandler_get, .put = xhandler_put, \
142         .private_value = xdata }
143 #define SOC_ENUM_EXT(xname, xenum, xhandler_get, xhandler_put) \
144 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
145         .info = snd_soc_info_enum_ext, \
146         .get = xhandler_get, .put = xhandler_put, \
147         .private_value = (unsigned long)&xenum }
148
149 /*
150  * Bias levels
151  *
152  * @ON:      Bias is fully on for audio playback and capture operations.
153  * @PREPARE: Prepare for audio operations. Called before DAPM switching for
154  *           stream start and stop operations.
155  * @STANDBY: Low power standby state when no playback/capture operations are
156  *           in progress. NOTE: The transition time between STANDBY and ON
157  *           should be as fast as possible and no longer than 10ms.
158  * @OFF:     Power Off. No restrictions on transition times.
159  */
160 enum snd_soc_bias_level {
161         SND_SOC_BIAS_ON,
162         SND_SOC_BIAS_PREPARE,
163         SND_SOC_BIAS_STANDBY,
164         SND_SOC_BIAS_OFF,
165 };
166
167 struct snd_jack;
168 struct snd_soc_card;
169 struct snd_soc_device;
170 struct snd_soc_pcm_stream;
171 struct snd_soc_ops;
172 struct snd_soc_dai_mode;
173 struct snd_soc_pcm_runtime;
174 struct snd_soc_dai;
175 struct snd_soc_platform;
176 struct snd_soc_codec;
177 struct soc_enum;
178 struct snd_soc_ac97_ops;
179 struct snd_soc_jack;
180 struct snd_soc_jack_pin;
181 #ifdef CONFIG_GPIOLIB
182 struct snd_soc_jack_gpio;
183 #endif
184
185 typedef int (*hw_write_t)(void *,const char* ,int);
186 typedef int (*hw_read_t)(void *,char* ,int);
187
188 extern struct snd_ac97_bus_ops soc_ac97_ops;
189
190 int snd_soc_register_platform(struct snd_soc_platform *platform);
191 void snd_soc_unregister_platform(struct snd_soc_platform *platform);
192 int snd_soc_register_codec(struct snd_soc_codec *codec);
193 void snd_soc_unregister_codec(struct snd_soc_codec *codec);
194 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg);
195 int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
196                                int addr_bits, int data_bits);
197
198 #ifdef CONFIG_PM
199 int snd_soc_suspend_device(struct device *dev);
200 int snd_soc_resume_device(struct device *dev);
201 #endif
202
203 /* pcm <-> DAI connect */
204 void snd_soc_free_pcms(struct snd_soc_device *socdev);
205 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid);
206 int snd_soc_init_card(struct snd_soc_device *socdev);
207
208 /* set runtime hw params */
209 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
210         const struct snd_pcm_hardware *hw);
211
212 /* Jack reporting */
213 int snd_soc_jack_new(struct snd_soc_card *card, const char *id, int type,
214                      struct snd_soc_jack *jack);
215 void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask);
216 int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
217                           struct snd_soc_jack_pin *pins);
218 #ifdef CONFIG_GPIOLIB
219 int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
220                         struct snd_soc_jack_gpio *gpios);
221 void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
222                         struct snd_soc_jack_gpio *gpios);
223 #endif
224
225 /* codec register bit access */
226 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
227                                 unsigned int mask, unsigned int value);
228 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
229                                 unsigned int mask, unsigned int value);
230
231 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
232         struct snd_ac97_bus_ops *ops, int num);
233 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec);
234
235 /*
236  *Controls
237  */
238 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
239         void *data, char *long_name);
240 int snd_soc_add_controls(struct snd_soc_codec *codec,
241         const struct snd_kcontrol_new *controls, int num_controls);
242 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
243         struct snd_ctl_elem_info *uinfo);
244 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
245         struct snd_ctl_elem_info *uinfo);
246 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
247         struct snd_ctl_elem_value *ucontrol);
248 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
249         struct snd_ctl_elem_value *ucontrol);
250 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
251         struct snd_ctl_elem_value *ucontrol);
252 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
253         struct snd_ctl_elem_value *ucontrol);
254 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
255         struct snd_ctl_elem_info *uinfo);
256 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
257         struct snd_ctl_elem_info *uinfo);
258 #define snd_soc_info_bool_ext           snd_ctl_boolean_mono_info
259 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
260         struct snd_ctl_elem_value *ucontrol);
261 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
262         struct snd_ctl_elem_value *ucontrol);
263 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
264         struct snd_ctl_elem_info *uinfo);
265 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
266         struct snd_ctl_elem_value *ucontrol);
267 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
268         struct snd_ctl_elem_value *ucontrol);
269 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
270         struct snd_ctl_elem_info *uinfo);
271 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
272         struct snd_ctl_elem_value *ucontrol);
273 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
274         struct snd_ctl_elem_value *ucontrol);
275
276 /**
277  * struct snd_soc_jack_pin - Describes a pin to update based on jack detection
278  *
279  * @pin:    name of the pin to update
280  * @mask:   bits to check for in reported jack status
281  * @invert: if non-zero then pin is enabled when status is not reported
282  */
283 struct snd_soc_jack_pin {
284         struct list_head list;
285         const char *pin;
286         int mask;
287         bool invert;
288 };
289
290 /**
291  * struct snd_soc_jack_gpio - Describes a gpio pin for jack detection
292  *
293  * @gpio:         gpio number
294  * @name:         gpio name
295  * @report:       value to report when jack detected
296  * @invert:       report presence in low state
297  * @debouce_time: debouce time in ms
298  */
299 #ifdef CONFIG_GPIOLIB
300 struct snd_soc_jack_gpio {
301         unsigned int gpio;
302         const char *name;
303         int report;
304         int invert;
305         int debounce_time;
306         struct snd_soc_jack *jack;
307         struct work_struct work;
308 };
309 #endif
310
311 struct snd_soc_jack {
312         struct snd_jack *jack;
313         struct snd_soc_card *card;
314         struct list_head pins;
315         int status;
316 };
317
318 /* SoC PCM stream information */
319 struct snd_soc_pcm_stream {
320         char *stream_name;
321         u64 formats;                    /* SNDRV_PCM_FMTBIT_* */
322         unsigned int rates;             /* SNDRV_PCM_RATE_* */
323         unsigned int rate_min;          /* min rate */
324         unsigned int rate_max;          /* max rate */
325         unsigned int channels_min;      /* min channels */
326         unsigned int channels_max;      /* max channels */
327         unsigned int active:1;          /* stream is in use */
328 };
329
330 /* SoC audio ops */
331 struct snd_soc_ops {
332         int (*startup)(struct snd_pcm_substream *);
333         void (*shutdown)(struct snd_pcm_substream *);
334         int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
335         int (*hw_free)(struct snd_pcm_substream *);
336         int (*prepare)(struct snd_pcm_substream *);
337         int (*trigger)(struct snd_pcm_substream *, int);
338 };
339
340 /* SoC Audio Codec */
341 struct snd_soc_codec {
342         char *name;
343         struct module *owner;
344         struct mutex mutex;
345         struct device *dev;
346         struct snd_soc_device *socdev;
347
348         struct list_head list;
349
350         /* callbacks */
351         int (*set_bias_level)(struct snd_soc_codec *,
352                               enum snd_soc_bias_level level);
353
354         /* runtime */
355         struct snd_card *card;
356         struct snd_ac97 *ac97;  /* for ad-hoc ac97 devices */
357         unsigned int active;
358         unsigned int pcm_devs;
359         void *private_data;
360
361         /* codec IO */
362         void *control_data; /* codec control (i2c/3wire) data */
363         unsigned int (*read)(struct snd_soc_codec *, unsigned int);
364         int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
365         int (*display_register)(struct snd_soc_codec *, char *,
366                                 size_t, unsigned int);
367         int (*volatile_register)(unsigned int);
368         hw_write_t hw_write;
369         hw_read_t hw_read;
370         void *reg_cache;
371         short reg_cache_size;
372         short reg_cache_step;
373
374         /* dapm */
375         u32 pop_time;
376         struct list_head dapm_widgets;
377         struct list_head dapm_paths;
378         enum snd_soc_bias_level bias_level;
379         enum snd_soc_bias_level suspend_bias_level;
380         struct delayed_work delayed_work;
381
382         /* codec DAI's */
383         struct snd_soc_dai *dai;
384         unsigned int num_dai;
385
386 #ifdef CONFIG_DEBUG_FS
387         struct dentry *debugfs_reg;
388         struct dentry *debugfs_pop_time;
389 #endif
390 };
391
392 /* codec device */
393 struct snd_soc_codec_device {
394         int (*probe)(struct platform_device *pdev);
395         int (*remove)(struct platform_device *pdev);
396         int (*suspend)(struct platform_device *pdev, pm_message_t state);
397         int (*resume)(struct platform_device *pdev);
398 };
399
400 /* SoC platform interface */
401 struct snd_soc_platform {
402         char *name;
403         struct list_head list;
404
405         int (*probe)(struct platform_device *pdev);
406         int (*remove)(struct platform_device *pdev);
407         int (*suspend)(struct snd_soc_dai *dai);
408         int (*resume)(struct snd_soc_dai *dai);
409
410         /* pcm creation and destruction */
411         int (*pcm_new)(struct snd_card *, struct snd_soc_dai *,
412                 struct snd_pcm *);
413         void (*pcm_free)(struct snd_pcm *);
414
415         /* platform stream ops */
416         struct snd_pcm_ops *pcm_ops;
417 };
418
419 /* SoC machine DAI configuration, glues a codec and cpu DAI together */
420 struct snd_soc_dai_link  {
421         char *name;                     /* Codec name */
422         char *stream_name;              /* Stream name */
423
424         /* DAI */
425         struct snd_soc_dai *codec_dai;
426         struct snd_soc_dai *cpu_dai;
427
428         /* machine stream operations */
429         struct snd_soc_ops *ops;
430
431         /* codec/machine specific init - e.g. add machine controls */
432         int (*init)(struct snd_soc_codec *codec);
433
434         /* Symmetry requirements */
435         unsigned int symmetric_rates:1;
436
437         /* Symmetry data - only valid if symmetry is being enforced */
438         unsigned int rate;
439
440         /* DAI pcm */
441         struct snd_pcm *pcm;
442 };
443
444 /* SoC card */
445 struct snd_soc_card {
446         char *name;
447         struct device *dev;
448
449         struct list_head list;
450
451         int instantiated;
452
453         int (*probe)(struct platform_device *pdev);
454         int (*remove)(struct platform_device *pdev);
455
456         /* the pre and post PM functions are used to do any PM work before and
457          * after the codec and DAI's do any PM work. */
458         int (*suspend_pre)(struct platform_device *pdev, pm_message_t state);
459         int (*suspend_post)(struct platform_device *pdev, pm_message_t state);
460         int (*resume_pre)(struct platform_device *pdev);
461         int (*resume_post)(struct platform_device *pdev);
462
463         /* callbacks */
464         int (*set_bias_level)(struct snd_soc_card *,
465                               enum snd_soc_bias_level level);
466
467         /* CPU <--> Codec DAI links  */
468         struct snd_soc_dai_link *dai_link;
469         int num_links;
470
471         struct snd_soc_device *socdev;
472
473         struct snd_soc_codec *codec;
474
475         struct snd_soc_platform *platform;
476         struct delayed_work delayed_work;
477         struct work_struct deferred_resume_work;
478 };
479
480 /* SoC Device - the audio subsystem */
481 struct snd_soc_device {
482         struct device *dev;
483         struct snd_soc_card *card;
484         struct snd_soc_codec_device *codec_dev;
485         void *codec_data;
486 };
487
488 /* runtime channel data */
489 struct snd_soc_pcm_runtime {
490         struct snd_soc_dai_link *dai;
491         struct snd_soc_device *socdev;
492 };
493
494 /* mixer control */
495 struct soc_mixer_control {
496         int min, max;
497         unsigned int reg, rreg, shift, rshift, invert;
498 };
499
500 /* enumerated kcontrol */
501 struct soc_enum {
502         unsigned short reg;
503         unsigned short reg2;
504         unsigned char shift_l;
505         unsigned char shift_r;
506         unsigned int max;
507         unsigned int mask;
508         const char **texts;
509         const unsigned int *values;
510         void *dapm;
511 };
512
513 /* codec IO */
514 static inline unsigned int snd_soc_read(struct snd_soc_codec *codec,
515                                         unsigned int reg)
516 {
517         return codec->read(codec, reg);
518 }
519
520 static inline unsigned int snd_soc_write(struct snd_soc_codec *codec,
521                                          unsigned int reg, unsigned int val)
522 {
523         return codec->write(codec, reg, val);
524 }
525
526 #include <sound/soc-dai.h>
527
528 #endif