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.0" 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 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 int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol, 204 struct snd_ctl_elem_info *uinfo); 205 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, 206 struct snd_ctl_elem_value *ucontrol); 207 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, 208 struct snd_ctl_elem_value *ucontrol); 209 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol, 210 struct snd_ctl_elem_info *uinfo); 211 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol, 212 struct snd_ctl_elem_value *ucontrol); 213 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol, 214 struct snd_ctl_elem_value *ucontrol); 215 216 /* SoC PCM stream information */ 217 struct snd_soc_pcm_stream { 218 char *stream_name; 219 u64 formats; /* SNDRV_PCM_FMTBIT_* */ 220 unsigned int rates; /* SNDRV_PCM_RATE_* */ 221 unsigned int rate_min; /* min rate */ 222 unsigned int rate_max; /* max rate */ 223 unsigned int channels_min; /* min channels */ 224 unsigned int channels_max; /* max channels */ 225 unsigned int active:1; /* stream is in use */ 226 }; 227 228 /* SoC audio ops */ 229 struct snd_soc_ops { 230 int (*startup)(struct snd_pcm_substream *); 231 void (*shutdown)(struct snd_pcm_substream *); 232 int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *); 233 int (*hw_free)(struct snd_pcm_substream *); 234 int (*prepare)(struct snd_pcm_substream *); 235 int (*trigger)(struct snd_pcm_substream *, int); 236 }; 237 238 /* ASoC codec DAI ops */ 239 struct snd_soc_codec_ops { 240 /* codec DAI clocking configuration */ 241 int (*set_sysclk)(struct snd_soc_codec_dai *codec_dai, 242 int clk_id, unsigned int freq, int dir); 243 int (*set_pll)(struct snd_soc_codec_dai *codec_dai, 244 int pll_id, unsigned int freq_in, unsigned int freq_out); 245 int (*set_clkdiv)(struct snd_soc_codec_dai *codec_dai, 246 int div_id, int div); 247 248 /* CPU DAI format configuration */ 249 int (*set_fmt)(struct snd_soc_codec_dai *codec_dai, 250 unsigned int fmt); 251 int (*set_tdm_slot)(struct snd_soc_codec_dai *codec_dai, 252 unsigned int mask, int slots); 253 int (*set_tristate)(struct snd_soc_codec_dai *, int tristate); 254 255 /* digital mute */ 256 int (*digital_mute)(struct snd_soc_codec_dai *, int mute); 257 }; 258 259 /* ASoC cpu DAI ops */ 260 struct snd_soc_cpu_ops { 261 /* CPU DAI clocking configuration */ 262 int (*set_sysclk)(struct snd_soc_cpu_dai *cpu_dai, 263 int clk_id, unsigned int freq, int dir); 264 int (*set_clkdiv)(struct snd_soc_cpu_dai *cpu_dai, 265 int div_id, int div); 266 int (*set_pll)(struct snd_soc_cpu_dai *cpu_dai, 267 int pll_id, unsigned int freq_in, unsigned int freq_out); 268 269 /* CPU DAI format configuration */ 270 int (*set_fmt)(struct snd_soc_cpu_dai *cpu_dai, 271 unsigned int fmt); 272 int (*set_tdm_slot)(struct snd_soc_cpu_dai *cpu_dai, 273 unsigned int mask, int slots); 274 int (*set_tristate)(struct snd_soc_cpu_dai *, int tristate); 275 }; 276 277 /* SoC Codec DAI */ 278 struct snd_soc_codec_dai { 279 char *name; 280 int id; 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 /* CPU <--> Codec DAI links */ 429 struct snd_soc_dai_link *dai_link; 430 int num_links; 431 }; 432 433 /* SoC Device - the audio subsystem */ 434 struct snd_soc_device { 435 struct device *dev; 436 struct snd_soc_machine *machine; 437 struct snd_soc_platform *platform; 438 struct snd_soc_codec *codec; 439 struct snd_soc_codec_device *codec_dev; 440 struct delayed_work delayed_work; 441 void *codec_data; 442 }; 443 444 /* runtime channel data */ 445 struct snd_soc_pcm_runtime { 446 struct snd_soc_dai_link *dai; 447 struct snd_soc_device *socdev; 448 }; 449 450 /* enumerated kcontrol */ 451 struct soc_enum { 452 unsigned short reg; 453 unsigned short reg2; 454 unsigned char shift_l; 455 unsigned char shift_r; 456 unsigned int mask; 457 const char **texts; 458 void *dapm; 459 }; 460 461 #endif 462