soc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.11.8"
  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. #define SND_SOC_FSBD_ALL 0xffff /* all bit clock dividers supported */
  132. /* bit clock ratio to sample rate */
  133. #define SND_SOC_FSB(x) (1 << ((x - 16) / 16))
  134. #define SND_SOC_FSB_REAL(x) (((ffs(x) - 1) * 16) + 16)
  135. /* all bclk ratios supported */
  136. #define SND_SOC_FSB_ALL SND_SOC_FSBD_ALL
  137. /*
  138. * DAI hardware flags
  139. */
  140. /* use bfs mclk divider mode, else sample rate ratio */
  141. #define SND_SOC_DAI_BFS_DIV 0x1
  142. /*
  143. * AC97 codec ID's bitmask
  144. */
  145. #define SND_SOC_DAI_AC97_ID0 (1 << 0)
  146. #define SND_SOC_DAI_AC97_ID1 (1 << 1)
  147. #define SND_SOC_DAI_AC97_ID2 (1 << 2)
  148. #define SND_SOC_DAI_AC97_ID3 (1 << 3)
  149. struct snd_soc_device;
  150. struct snd_soc_pcm_stream;
  151. struct snd_soc_ops;
  152. struct snd_soc_dai_mode;
  153. struct snd_soc_pcm_runtime;
  154. struct snd_soc_codec_dai;
  155. struct snd_soc_cpu_dai;
  156. struct snd_soc_codec;
  157. struct snd_soc_machine_config;
  158. struct soc_enum;
  159. struct snd_soc_ac97_ops;
  160. struct snd_soc_clock_info;
  161. typedef int (*hw_write_t)(void *,const char* ,int);
  162. typedef int (*hw_read_t)(void *,char* ,int);
  163. extern struct snd_ac97_bus_ops soc_ac97_ops;
  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. /* set runtime hw params */
  169. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  170. const struct snd_pcm_hardware *hw);
  171. int snd_soc_get_rate(int rate);
  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. /* codec register bit access */
  176. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  177. unsigned short mask, unsigned short value);
  178. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  179. unsigned short mask, unsigned short value);
  180. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  181. struct snd_ac97_bus_ops *ops, int num);
  182. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec);
  183. /*
  184. *Controls
  185. */
  186. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  187. void *data, char *long_name);
  188. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  189. struct snd_ctl_elem_info *uinfo);
  190. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  191. struct snd_ctl_elem_info *uinfo);
  192. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  193. struct snd_ctl_elem_value *ucontrol);
  194. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  195. struct snd_ctl_elem_value *ucontrol);
  196. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  197. struct snd_ctl_elem_info *uinfo);
  198. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  199. struct snd_ctl_elem_info *uinfo);
  200. int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
  201. struct snd_ctl_elem_info *uinfo);
  202. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  203. struct snd_ctl_elem_value *ucontrol);
  204. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  205. struct snd_ctl_elem_value *ucontrol);
  206. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  207. struct snd_ctl_elem_info *uinfo);
  208. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  209. struct snd_ctl_elem_value *ucontrol);
  210. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  211. struct snd_ctl_elem_value *ucontrol);
  212. /* SoC PCM stream information */
  213. struct snd_soc_pcm_stream {
  214. char *stream_name;
  215. unsigned int rate_min; /* min rate */
  216. unsigned int rate_max; /* max rate */
  217. unsigned int channels_min; /* min channels */
  218. unsigned int channels_max; /* max channels */
  219. unsigned int active:1; /* stream is in use */
  220. };
  221. /* SoC audio ops */
  222. struct snd_soc_ops {
  223. int (*startup)(struct snd_pcm_substream *);
  224. void (*shutdown)(struct snd_pcm_substream *);
  225. int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
  226. int (*hw_free)(struct snd_pcm_substream *);
  227. int (*prepare)(struct snd_pcm_substream *);
  228. int (*trigger)(struct snd_pcm_substream *, int);
  229. };
  230. /* SoC DAI hardware mode */
  231. struct snd_soc_dai_mode {
  232. u16 fmt; /* SND_SOC_DAIFMT_* */
  233. u16 tdm; /* SND_SOC_HWTDM_* */
  234. u64 pcmfmt; /* SNDRV_PCM_FMTBIT_* */
  235. u16 pcmrate; /* SND_SOC_HWRATE_* */
  236. u16 pcmdir:2; /* SND_SOC_HWDIR_* */
  237. u16 flags:8; /* hw flags */
  238. u16 fs; /* mclk to rate divider */
  239. u32 bfs; /* mclk to bclk dividers */
  240. unsigned long priv; /* private mode data */
  241. };
  242. /* DAI capabilities */
  243. struct snd_soc_dai_cap {
  244. int num_modes; /* number of DAI modes */
  245. struct snd_soc_dai_mode *mode; /* array of supported DAI modes */
  246. };
  247. /* SoC Codec DAI */
  248. struct snd_soc_codec_dai {
  249. char *name;
  250. int id;
  251. /* DAI capabilities */
  252. struct snd_soc_pcm_stream playback;
  253. struct snd_soc_pcm_stream capture;
  254. struct snd_soc_dai_cap caps;
  255. /* DAI runtime info */
  256. struct snd_soc_dai_mode dai_runtime;
  257. struct snd_soc_ops ops;
  258. unsigned int (*config_sysclk)(struct snd_soc_codec_dai*,
  259. struct snd_soc_clock_info *info, unsigned int clk);
  260. int (*digital_mute)(struct snd_soc_codec *,
  261. struct snd_soc_codec_dai*, int);
  262. unsigned int mclk; /* the audio master clock */
  263. unsigned int pll_in; /* the PLL input clock */
  264. unsigned int pll_out; /* the PLL output clock */
  265. unsigned int clk_div; /* internal clock divider << 1 (for fractions) */
  266. unsigned int active;
  267. unsigned char pop_wait:1;
  268. /* DAI private data */
  269. void *private_data;
  270. };
  271. /* SoC CPU DAI */
  272. struct snd_soc_cpu_dai {
  273. /* DAI description */
  274. char *name;
  275. unsigned int id;
  276. unsigned char type;
  277. /* DAI callbacks */
  278. int (*probe)(struct platform_device *pdev);
  279. void (*remove)(struct platform_device *pdev);
  280. int (*suspend)(struct platform_device *pdev,
  281. struct snd_soc_cpu_dai *cpu_dai);
  282. int (*resume)(struct platform_device *pdev,
  283. struct snd_soc_cpu_dai *cpu_dai);
  284. unsigned int (*config_sysclk)(struct snd_soc_cpu_dai *cpu_dai,
  285. struct snd_soc_clock_info *info, unsigned int clk);
  286. /* DAI capabilities */
  287. struct snd_soc_pcm_stream capture;
  288. struct snd_soc_pcm_stream playback;
  289. struct snd_soc_dai_cap caps;
  290. /* DAI runtime info */
  291. struct snd_soc_dai_mode dai_runtime;
  292. struct snd_soc_ops ops;
  293. struct snd_pcm_runtime *runtime;
  294. unsigned char active:1;
  295. unsigned int mclk;
  296. void *dma_data;
  297. /* DAI private data */
  298. void *private_data;
  299. };
  300. /* SoC Audio Codec */
  301. struct snd_soc_codec {
  302. char *name;
  303. struct module *owner;
  304. struct mutex mutex;
  305. /* callbacks */
  306. int (*dapm_event)(struct snd_soc_codec *codec, int event);
  307. /* runtime */
  308. struct snd_card *card;
  309. struct snd_ac97 *ac97; /* for ad-hoc ac97 devices */
  310. unsigned int active;
  311. unsigned int pcm_devs;
  312. void *private_data;
  313. /* codec IO */
  314. void *control_data; /* codec control (i2c/3wire) data */
  315. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  316. int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
  317. hw_write_t hw_write;
  318. hw_read_t hw_read;
  319. void *reg_cache;
  320. short reg_cache_size;
  321. short reg_cache_step;
  322. /* dapm */
  323. struct list_head dapm_widgets;
  324. struct list_head dapm_paths;
  325. unsigned int dapm_state;
  326. unsigned int suspend_dapm_state;
  327. /* codec DAI's */
  328. struct snd_soc_codec_dai *dai;
  329. unsigned int num_dai;
  330. };
  331. /* codec device */
  332. struct snd_soc_codec_device {
  333. int (*probe)(struct platform_device *pdev);
  334. int (*remove)(struct platform_device *pdev);
  335. int (*suspend)(struct platform_device *pdev, pm_message_t state);
  336. int (*resume)(struct platform_device *pdev);
  337. };
  338. /* SoC platform interface */
  339. struct snd_soc_platform {
  340. char *name;
  341. int (*probe)(struct platform_device *pdev);
  342. int (*remove)(struct platform_device *pdev);
  343. int (*suspend)(struct platform_device *pdev,
  344. struct snd_soc_cpu_dai *cpu_dai);
  345. int (*resume)(struct platform_device *pdev,
  346. struct snd_soc_cpu_dai *cpu_dai);
  347. /* pcm creation and destruction */
  348. int (*pcm_new)(struct snd_card *, struct snd_soc_codec_dai *,
  349. struct snd_pcm *);
  350. void (*pcm_free)(struct snd_pcm *);
  351. /* platform stream ops */
  352. struct snd_pcm_ops *pcm_ops;
  353. };
  354. /* SoC machine DAI configuration, glues a codec and cpu DAI together */
  355. struct snd_soc_dai_link {
  356. char *name; /* Codec name */
  357. char *stream_name; /* Stream name */
  358. /* DAI */
  359. struct snd_soc_codec_dai *codec_dai;
  360. struct snd_soc_cpu_dai *cpu_dai;
  361. u32 flags; /* DAI config preference flags */
  362. /* codec/machine specific init - e.g. add machine controls */
  363. int (*init)(struct snd_soc_codec *codec);
  364. /* audio sysclock configuration */
  365. unsigned int (*config_sysclk)(struct snd_soc_pcm_runtime *rtd,
  366. struct snd_soc_clock_info *info);
  367. };
  368. /* SoC machine */
  369. struct snd_soc_machine {
  370. char *name;
  371. int (*probe)(struct platform_device *pdev);
  372. int (*remove)(struct platform_device *pdev);
  373. /* the pre and post PM functions are used to do any PM work before and
  374. * after the codec and DAI's do any PM work. */
  375. int (*suspend_pre)(struct platform_device *pdev, pm_message_t state);
  376. int (*suspend_post)(struct platform_device *pdev, pm_message_t state);
  377. int (*resume_pre)(struct platform_device *pdev);
  378. int (*resume_post)(struct platform_device *pdev);
  379. /* machine stream operations */
  380. struct snd_soc_ops *ops;
  381. /* CPU <--> Codec DAI links */
  382. struct snd_soc_dai_link *dai_link;
  383. int num_links;
  384. };
  385. /* SoC Device - the audio subsystem */
  386. struct snd_soc_device {
  387. struct device *dev;
  388. struct snd_soc_machine *machine;
  389. struct snd_soc_platform *platform;
  390. struct snd_soc_codec *codec;
  391. struct snd_soc_codec_device *codec_dev;
  392. void *codec_data;
  393. };
  394. /* runtime channel data */
  395. struct snd_soc_pcm_runtime {
  396. struct snd_soc_codec_dai *codec_dai;
  397. struct snd_soc_cpu_dai *cpu_dai;
  398. struct snd_soc_device *socdev;
  399. };
  400. /* enumerated kcontrol */
  401. struct soc_enum {
  402. unsigned short reg;
  403. unsigned short reg2;
  404. unsigned char shift_l;
  405. unsigned char shift_r;
  406. unsigned int mask;
  407. const char **texts;
  408. void *dapm;
  409. };
  410. /* clocking configuration data */
  411. struct snd_soc_clock_info {
  412. unsigned int rate;
  413. unsigned int fs;
  414. unsigned int bclk_master;
  415. };
  416. #endif