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