[ALSA] ASoC: Add support for BCLK based on (Rate * Chn * Word Size)
[deliverable/linux.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 <sound/driver.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.12"
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, xmask, xinvert,\
63 xhandler_get, xhandler_put) \
64 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
65 .info = snd_soc_info_volsw_ext, \
66 .get = xhandler_get, .put = xhandler_put, \
67 .private_value = SOC_SINGLE_VALUE_EXT(xreg, 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
86 /*
87 * DAI hardware audio formats
88 */
89 #define SND_SOC_DAIFMT_I2S (1 << 0) /* I2S mode */
90 #define SND_SOC_DAIFMT_RIGHT_J (1 << 1) /* Right justified mode */
91 #define SND_SOC_DAIFMT_LEFT_J (1 << 2) /* Left Justified mode */
92 #define SND_SOC_DAIFMT_DSP_A (1 << 3) /* L data msb after FRM or LRC */
93 #define SND_SOC_DAIFMT_DSP_B (1 << 4) /* L data msb during FRM or LRC */
94 #define SND_SOC_DAIFMT_AC97 (1 << 5) /* AC97 */
95
96 /*
97 * DAI hardware signal inversions
98 */
99 #define SND_SOC_DAIFMT_NB_NF (1 << 8) /* normal bit clock + frame */
100 #define SND_SOC_DAIFMT_NB_IF (1 << 9) /* normal bclk + inv frm */
101 #define SND_SOC_DAIFMT_IB_NF (1 << 10) /* invert bclk + nor frm */
102 #define SND_SOC_DAIFMT_IB_IF (1 << 11) /* invert bclk + frm */
103
104 /*
105 * DAI hardware clock masters
106 * This is wrt the codec, the inverse is true for the interface
107 * i.e. if the codec is clk and frm master then the interface is
108 * clk and frame slave.
109 */
110 #define SND_SOC_DAIFMT_CBM_CFM (1 << 12) /* codec clk & frm master */
111 #define SND_SOC_DAIFMT_CBS_CFM (1 << 13) /* codec clk slave & frm master */
112 #define SND_SOC_DAIFMT_CBM_CFS (1 << 14) /* codec clk master & frame slave */
113 #define SND_SOC_DAIFMT_CBS_CFS (1 << 15) /* codec clk & frm slave */
114
115 #define SND_SOC_DAIFMT_FORMAT_MASK 0x00ff
116 #define SND_SOC_DAIFMT_INV_MASK 0x0f00
117 #define SND_SOC_DAIFMT_CLOCK_MASK 0xf000
118
119 /*
120 * DAI hardware audio direction
121 */
122 #define SND_SOC_DAIDIR_PLAYBACK 0x1
123 #define SND_SOC_DAIDIR_CAPTURE 0x2
124
125 /*
126 * DAI hardware Time Division Multiplexing (TDM) Slots
127 * Left and Right data word positions
128 * This is measured in words (sample size) and not bits.
129 */
130 #define SND_SOC_DAITDM_LRDW(l,r) ((l << 8) | r)
131
132 /*
133 * DAI hardware clock ratios
134 * bit clock can either be a generated by dividing mclk or
135 * by multiplying sample rate, hence there are 2 definitions below
136 * depending on codec type.
137 */
138 /* ratio of sample rate to mclk/sysclk */
139 #define SND_SOC_FS_ALL 0xffff /* all mclk supported */
140
141 /* bit clock dividers */
142 #define SND_SOC_FSBD(x) (1 << (x - 1)) /* ratio mclk:bclk */
143 #define SND_SOC_FSBD_REAL(x) (ffs(x))
144
145 /* bit clock ratio to (sample rate * channels * word size) */
146 #define SND_SOC_FSBW(x) (1 << (x - 1))
147 #define SND_SOC_FSBW_REAL(x) (ffs(x))
148 /* all bclk ratios supported */
149 #define SND_SOC_FSB_ALL ~0ULL
150
151 /*
152 * DAI hardware flags
153 */
154 /* use bfs mclk divider mode (BCLK = MCLK / x) */
155 #define SND_SOC_DAI_BFS_DIV 0x1
156 /* use bfs rate mulitplier (BCLK = RATE * x)*/
157 #define SND_SOC_DAI_BFS_RATE 0x2
158 /* use bfs rcw multiplier (BCLK = RATE * CHN * WORD SIZE) */
159 #define SND_SOC_DAI_BFS_RCW 0x4
160 /* capture and playback can use different clocks */
161 #define SND_SOC_DAI_ASYNC 0x8
162
163 /*
164 * AC97 codec ID's bitmask
165 */
166 #define SND_SOC_DAI_AC97_ID0 (1 << 0)
167 #define SND_SOC_DAI_AC97_ID1 (1 << 1)
168 #define SND_SOC_DAI_AC97_ID2 (1 << 2)
169 #define SND_SOC_DAI_AC97_ID3 (1 << 3)
170
171 struct snd_soc_device;
172 struct snd_soc_pcm_stream;
173 struct snd_soc_ops;
174 struct snd_soc_dai_mode;
175 struct snd_soc_pcm_runtime;
176 struct snd_soc_codec_dai;
177 struct snd_soc_cpu_dai;
178 struct snd_soc_codec;
179 struct snd_soc_machine_config;
180 struct soc_enum;
181 struct snd_soc_ac97_ops;
182 struct snd_soc_clock_info;
183
184 typedef int (*hw_write_t)(void *,const char* ,int);
185 typedef int (*hw_read_t)(void *,char* ,int);
186
187 extern struct snd_ac97_bus_ops soc_ac97_ops;
188
189 /* pcm <-> DAI connect */
190 void snd_soc_free_pcms(struct snd_soc_device *socdev);
191 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid);
192 int snd_soc_register_card(struct snd_soc_device *socdev);
193
194 /* set runtime hw params */
195 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
196 const struct snd_pcm_hardware *hw);
197 int snd_soc_get_rate(int rate);
198
199 /* codec IO */
200 #define snd_soc_read(codec, reg) codec->read(codec, reg)
201 #define snd_soc_write(codec, reg, value) codec->write(codec, reg, value)
202
203 /* codec register bit access */
204 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
205 unsigned short mask, unsigned short value);
206 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
207 unsigned short mask, unsigned short value);
208
209 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
210 struct snd_ac97_bus_ops *ops, int num);
211 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec);
212
213 /*
214 *Controls
215 */
216 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
217 void *data, char *long_name);
218 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
219 struct snd_ctl_elem_info *uinfo);
220 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
221 struct snd_ctl_elem_info *uinfo);
222 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
223 struct snd_ctl_elem_value *ucontrol);
224 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
225 struct snd_ctl_elem_value *ucontrol);
226 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
227 struct snd_ctl_elem_info *uinfo);
228 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
229 struct snd_ctl_elem_info *uinfo);
230 int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
231 struct snd_ctl_elem_info *uinfo);
232 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
233 struct snd_ctl_elem_value *ucontrol);
234 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
235 struct snd_ctl_elem_value *ucontrol);
236 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
237 struct snd_ctl_elem_info *uinfo);
238 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
239 struct snd_ctl_elem_value *ucontrol);
240 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
241 struct snd_ctl_elem_value *ucontrol);
242
243 /* SoC PCM stream information */
244 struct snd_soc_pcm_stream {
245 char *stream_name;
246 unsigned int rate_min; /* min rate */
247 unsigned int rate_max; /* max rate */
248 unsigned int channels_min; /* min channels */
249 unsigned int channels_max; /* max channels */
250 unsigned int active:1; /* stream is in use */
251 };
252
253 /* SoC audio ops */
254 struct snd_soc_ops {
255 int (*startup)(struct snd_pcm_substream *);
256 void (*shutdown)(struct snd_pcm_substream *);
257 int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
258 int (*hw_free)(struct snd_pcm_substream *);
259 int (*prepare)(struct snd_pcm_substream *);
260 int (*trigger)(struct snd_pcm_substream *, int);
261 };
262
263 /* SoC DAI hardware mode */
264 struct snd_soc_dai_mode {
265 u16 fmt; /* SND_SOC_DAIFMT_* */
266 u16 tdm; /* SND_SOC_HWTDM_* */
267 u64 pcmfmt; /* SNDRV_PCM_FMTBIT_* */
268 u16 pcmrate; /* SND_SOC_HWRATE_* */
269 u16 pcmdir:2; /* SND_SOC_HWDIR_* */
270 u16 flags:8; /* hw flags */
271 u16 fs; /* mclk to rate divider */
272 u64 bfs; /* mclk to bclk dividers */
273 unsigned long priv; /* private mode data */
274 };
275
276 /* DAI capabilities */
277 struct snd_soc_dai_cap {
278 int num_modes; /* number of DAI modes */
279 struct snd_soc_dai_mode *mode; /* array of supported DAI modes */
280 };
281
282 /* SoC Codec DAI */
283 struct snd_soc_codec_dai {
284 char *name;
285 int id;
286
287 /* DAI capabilities */
288 struct snd_soc_pcm_stream playback;
289 struct snd_soc_pcm_stream capture;
290 struct snd_soc_dai_cap caps;
291
292 /* DAI runtime info */
293 struct snd_soc_dai_mode dai_runtime;
294 struct snd_soc_ops ops;
295 unsigned int (*config_sysclk)(struct snd_soc_codec_dai*,
296 struct snd_soc_clock_info *info, unsigned int clk);
297 int (*digital_mute)(struct snd_soc_codec *,
298 struct snd_soc_codec_dai*, int);
299 unsigned int mclk; /* the audio master clock */
300 unsigned int pll_in; /* the PLL input clock */
301 unsigned int pll_out; /* the PLL output clock */
302 unsigned int clk_div; /* internal clock divider << 1 (for fractions) */
303 unsigned int active;
304 unsigned char pop_wait:1;
305
306 /* DAI private data */
307 void *private_data;
308 };
309
310 /* SoC CPU DAI */
311 struct snd_soc_cpu_dai {
312
313 /* DAI description */
314 char *name;
315 unsigned int id;
316 unsigned char type;
317
318 /* DAI callbacks */
319 int (*probe)(struct platform_device *pdev);
320 void (*remove)(struct platform_device *pdev);
321 int (*suspend)(struct platform_device *pdev,
322 struct snd_soc_cpu_dai *cpu_dai);
323 int (*resume)(struct platform_device *pdev,
324 struct snd_soc_cpu_dai *cpu_dai);
325 unsigned int (*config_sysclk)(struct snd_soc_cpu_dai *cpu_dai,
326 struct snd_soc_clock_info *info, unsigned int clk);
327
328 /* DAI capabilities */
329 struct snd_soc_pcm_stream capture;
330 struct snd_soc_pcm_stream playback;
331 struct snd_soc_dai_cap caps;
332
333 /* DAI runtime info */
334 struct snd_soc_dai_mode dai_runtime;
335 struct snd_soc_ops ops;
336 struct snd_pcm_runtime *runtime;
337 unsigned char active:1;
338 unsigned int mclk;
339 void *dma_data;
340
341 /* DAI private data */
342 void *private_data;
343 };
344
345 /* SoC Audio Codec */
346 struct snd_soc_codec {
347 char *name;
348 struct module *owner;
349 struct mutex mutex;
350
351 /* callbacks */
352 int (*dapm_event)(struct snd_soc_codec *codec, int event);
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 hw_write_t hw_write;
366 hw_read_t hw_read;
367 void *reg_cache;
368 short reg_cache_size;
369 short reg_cache_step;
370
371 /* dapm */
372 struct list_head dapm_widgets;
373 struct list_head dapm_paths;
374 unsigned int dapm_state;
375 unsigned int suspend_dapm_state;
376
377 /* codec DAI's */
378 struct snd_soc_codec_dai *dai;
379 unsigned int num_dai;
380 };
381
382 /* codec device */
383 struct snd_soc_codec_device {
384 int (*probe)(struct platform_device *pdev);
385 int (*remove)(struct platform_device *pdev);
386 int (*suspend)(struct platform_device *pdev, pm_message_t state);
387 int (*resume)(struct platform_device *pdev);
388 };
389
390 /* SoC platform interface */
391 struct snd_soc_platform {
392 char *name;
393
394 int (*probe)(struct platform_device *pdev);
395 int (*remove)(struct platform_device *pdev);
396 int (*suspend)(struct platform_device *pdev,
397 struct snd_soc_cpu_dai *cpu_dai);
398 int (*resume)(struct platform_device *pdev,
399 struct snd_soc_cpu_dai *cpu_dai);
400
401 /* pcm creation and destruction */
402 int (*pcm_new)(struct snd_card *, struct snd_soc_codec_dai *,
403 struct snd_pcm *);
404 void (*pcm_free)(struct snd_pcm *);
405
406 /* platform stream ops */
407 struct snd_pcm_ops *pcm_ops;
408 };
409
410 /* SoC machine DAI configuration, glues a codec and cpu DAI together */
411 struct snd_soc_dai_link {
412 char *name; /* Codec name */
413 char *stream_name; /* Stream name */
414
415 /* DAI */
416 struct snd_soc_codec_dai *codec_dai;
417 struct snd_soc_cpu_dai *cpu_dai;
418 u32 flags; /* DAI config preference flags */
419
420 /* codec/machine specific init - e.g. add machine controls */
421 int (*init)(struct snd_soc_codec *codec);
422
423 /* audio sysclock configuration */
424 unsigned int (*config_sysclk)(struct snd_soc_pcm_runtime *rtd,
425 struct snd_soc_clock_info *info);
426 };
427
428 /* SoC machine */
429 struct snd_soc_machine {
430 char *name;
431
432 int (*probe)(struct platform_device *pdev);
433 int (*remove)(struct platform_device *pdev);
434
435 /* the pre and post PM functions are used to do any PM work before and
436 * after the codec and DAI's do any PM work. */
437 int (*suspend_pre)(struct platform_device *pdev, pm_message_t state);
438 int (*suspend_post)(struct platform_device *pdev, pm_message_t state);
439 int (*resume_pre)(struct platform_device *pdev);
440 int (*resume_post)(struct platform_device *pdev);
441
442 /* machine stream operations */
443 struct snd_soc_ops *ops;
444
445 /* CPU <--> Codec DAI links */
446 struct snd_soc_dai_link *dai_link;
447 int num_links;
448 };
449
450 /* SoC Device - the audio subsystem */
451 struct snd_soc_device {
452 struct device *dev;
453 struct snd_soc_machine *machine;
454 struct snd_soc_platform *platform;
455 struct snd_soc_codec *codec;
456 struct snd_soc_codec_device *codec_dev;
457 void *codec_data;
458 };
459
460 /* runtime channel data */
461 struct snd_soc_pcm_runtime {
462 struct snd_soc_codec_dai *codec_dai;
463 struct snd_soc_cpu_dai *cpu_dai;
464 struct snd_soc_device *socdev;
465 };
466
467 /* enumerated kcontrol */
468 struct soc_enum {
469 unsigned short reg;
470 unsigned short reg2;
471 unsigned char shift_l;
472 unsigned char shift_r;
473 unsigned int mask;
474 const char **texts;
475 void *dapm;
476 };
477
478 /* clocking configuration data */
479 struct snd_soc_clock_info {
480 unsigned int rate;
481 unsigned int fs;
482 unsigned int bclk_master;
483 };
484
485 #endif
This page took 0.039021 seconds and 5 git commands to generate.