soc.h 15 KB

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