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