soc.h 15 KB

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