soc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/control.h>
  20. #include <sound/ac97_codec.h>
  21. #define SND_SOC_VERSION "0.13.2"
  22. /*
  23. * Convenience kcontrol builders
  24. */
  25. #define SOC_SINGLE_VALUE(reg, shift, max, invert) ((reg) | ((shift) << 8) |\
  26. ((shift) << 12) | ((max) << 16) | ((invert) << 24))
  27. #define SOC_SINGLE_VALUE_EXT(reg, max, invert) ((reg) | ((max) << 16) |\
  28. ((invert) << 31))
  29. #define SOC_SINGLE(xname, reg, shift, max, 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, max, invert) }
  34. #define SOC_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \
  35. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  36. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
  37. SNDRV_CTL_ELEM_ACCESS_READWRITE,\
  38. .tlv.p = (tlv_array), \
  39. .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
  40. .put = snd_soc_put_volsw, \
  41. .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
  42. #define SOC_DOUBLE(xname, reg, shift_left, shift_right, max, invert) \
  43. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
  44. .info = snd_soc_info_volsw, .get = snd_soc_get_volsw, \
  45. .put = snd_soc_put_volsw, \
  46. .private_value = (reg) | ((shift_left) << 8) | \
  47. ((shift_right) << 12) | ((max) << 16) | ((invert) << 24) }
  48. #define SOC_DOUBLE_R(xname, reg_left, reg_right, shift, max, invert) \
  49. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
  50. .info = snd_soc_info_volsw_2r, \
  51. .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r, \
  52. .private_value = (reg_left) | ((shift) << 8) | \
  53. ((max) << 12) | ((invert) << 20) | ((reg_right) << 24) }
  54. #define SOC_DOUBLE_TLV(xname, reg, shift_left, shift_right, max, invert, tlv_array) \
  55. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
  56. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
  57. SNDRV_CTL_ELEM_ACCESS_READWRITE,\
  58. .tlv.p = (tlv_array), \
  59. .info = snd_soc_info_volsw, .get = snd_soc_get_volsw, \
  60. .put = snd_soc_put_volsw, \
  61. .private_value = (reg) | ((shift_left) << 8) | \
  62. ((shift_right) << 12) | ((max) << 16) | ((invert) << 24) }
  63. #define SOC_DOUBLE_R_TLV(xname, reg_left, reg_right, shift, max, invert, tlv_array) \
  64. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
  65. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
  66. SNDRV_CTL_ELEM_ACCESS_READWRITE,\
  67. .tlv.p = (tlv_array), \
  68. .info = snd_soc_info_volsw_2r, \
  69. .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r, \
  70. .private_value = (reg_left) | ((shift) << 8) | \
  71. ((max) << 12) | ((invert) << 20) | ((reg_right) << 24) }
  72. #define SOC_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmask, xtexts) \
  73. { .reg = xreg, .shift_l = xshift_l, .shift_r = xshift_r, \
  74. .mask = xmask, .texts = xtexts }
  75. #define SOC_ENUM_SINGLE(xreg, xshift, xmask, xtexts) \
  76. SOC_ENUM_DOUBLE(xreg, xshift, xshift, xmask, xtexts)
  77. #define SOC_ENUM_SINGLE_EXT(xmask, xtexts) \
  78. { .mask = xmask, .texts = xtexts }
  79. #define SOC_ENUM(xname, xenum) \
  80. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,\
  81. .info = snd_soc_info_enum_double, \
  82. .get = snd_soc_get_enum_double, .put = snd_soc_put_enum_double, \
  83. .private_value = (unsigned long)&xenum }
  84. #define SOC_SINGLE_EXT(xname, xreg, xshift, xmask, xinvert,\
  85. xhandler_get, xhandler_put) \
  86. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  87. .info = snd_soc_info_volsw, \
  88. .get = xhandler_get, .put = xhandler_put, \
  89. .private_value = SOC_SINGLE_VALUE(xreg, xshift, xmask, xinvert) }
  90. #define SOC_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
  91. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  92. .info = snd_soc_info_bool_ext, \
  93. .get = xhandler_get, .put = xhandler_put, \
  94. .private_value = xdata }
  95. #define SOC_ENUM_EXT(xname, xenum, xhandler_get, xhandler_put) \
  96. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  97. .info = snd_soc_info_enum_ext, \
  98. .get = xhandler_get, .put = xhandler_put, \
  99. .private_value = (unsigned long)&xenum }
  100. /*
  101. * Bias levels
  102. *
  103. * @ON: Bias is fully on for audio playback and capture operations.
  104. * @PREPARE: Prepare for audio operations. Called before DAPM switching for
  105. * stream start and stop operations.
  106. * @STANDBY: Low power standby state when no playback/capture operations are
  107. * in progress. NOTE: The transition time between STANDBY and ON
  108. * should be as fast as possible and no longer than 10ms.
  109. * @OFF: Power Off. No restrictions on transition times.
  110. */
  111. enum snd_soc_bias_level {
  112. SND_SOC_BIAS_ON,
  113. SND_SOC_BIAS_PREPARE,
  114. SND_SOC_BIAS_STANDBY,
  115. SND_SOC_BIAS_OFF,
  116. };
  117. /*
  118. * Digital Audio Interface (DAI) types
  119. */
  120. #define SND_SOC_DAI_AC97 0x1
  121. #define SND_SOC_DAI_I2S 0x2
  122. #define SND_SOC_DAI_PCM 0x4
  123. #define SND_SOC_DAI_AC97_BUS 0x8 /* for custom i.e. non ac97_codec.c */
  124. /*
  125. * DAI hardware audio formats
  126. */
  127. #define SND_SOC_DAIFMT_I2S 0 /* I2S mode */
  128. #define SND_SOC_DAIFMT_RIGHT_J 1 /* Right justified mode */
  129. #define SND_SOC_DAIFMT_LEFT_J 2 /* Left Justified mode */
  130. #define SND_SOC_DAIFMT_DSP_A 3 /* L data msb after FRM or LRC */
  131. #define SND_SOC_DAIFMT_DSP_B 4 /* L data msb during FRM or LRC */
  132. #define SND_SOC_DAIFMT_AC97 5 /* AC97 */
  133. #define SND_SOC_DAIFMT_MSB SND_SOC_DAIFMT_LEFT_J
  134. #define SND_SOC_DAIFMT_LSB SND_SOC_DAIFMT_RIGHT_J
  135. /*
  136. * DAI Gating
  137. */
  138. #define SND_SOC_DAIFMT_CONT (0 << 4) /* continuous clock */
  139. #define SND_SOC_DAIFMT_GATED (1 << 4) /* clock is gated when not Tx/Rx */
  140. /*
  141. * DAI Sync
  142. * Synchronous LR (Left Right) clocks and Frame signals.
  143. */
  144. #define SND_SOC_DAIFMT_SYNC (0 << 5) /* Tx FRM = Rx FRM */
  145. #define SND_SOC_DAIFMT_ASYNC (1 << 5) /* Tx FRM ~ Rx FRM */
  146. /*
  147. * TDM
  148. */
  149. #define SND_SOC_DAIFMT_TDM (1 << 6)
  150. /*
  151. * DAI hardware signal inversions
  152. */
  153. #define SND_SOC_DAIFMT_NB_NF (0 << 8) /* normal bclk + frm */
  154. #define SND_SOC_DAIFMT_NB_IF (1 << 8) /* normal bclk + inv frm */
  155. #define SND_SOC_DAIFMT_IB_NF (2 << 8) /* invert bclk + nor frm */
  156. #define SND_SOC_DAIFMT_IB_IF (3 << 8) /* invert bclk + frm */
  157. /*
  158. * DAI hardware clock masters
  159. * This is wrt the codec, the inverse is true for the interface
  160. * i.e. if the codec is clk and frm master then the interface is
  161. * clk and frame slave.
  162. */
  163. #define SND_SOC_DAIFMT_CBM_CFM (0 << 12) /* codec clk & frm master */
  164. #define SND_SOC_DAIFMT_CBS_CFM (1 << 12) /* codec clk slave & frm master */
  165. #define SND_SOC_DAIFMT_CBM_CFS (2 << 12) /* codec clk master & frame slave */
  166. #define SND_SOC_DAIFMT_CBS_CFS (3 << 12) /* codec clk & frm slave */
  167. #define SND_SOC_DAIFMT_FORMAT_MASK 0x000f
  168. #define SND_SOC_DAIFMT_CLOCK_MASK 0x00f0
  169. #define SND_SOC_DAIFMT_INV_MASK 0x0f00
  170. #define SND_SOC_DAIFMT_MASTER_MASK 0xf000
  171. /*
  172. * Master Clock Directions
  173. */
  174. #define SND_SOC_CLOCK_IN 0
  175. #define SND_SOC_CLOCK_OUT 1
  176. /*
  177. * AC97 codec ID's bitmask
  178. */
  179. #define SND_SOC_DAI_AC97_ID0 (1 << 0)
  180. #define SND_SOC_DAI_AC97_ID1 (1 << 1)
  181. #define SND_SOC_DAI_AC97_ID2 (1 << 2)
  182. #define SND_SOC_DAI_AC97_ID3 (1 << 3)
  183. struct snd_soc_device;
  184. struct snd_soc_pcm_stream;
  185. struct snd_soc_ops;
  186. struct snd_soc_dai_mode;
  187. struct snd_soc_pcm_runtime;
  188. struct snd_soc_codec_dai;
  189. struct snd_soc_cpu_dai;
  190. struct snd_soc_codec;
  191. struct snd_soc_machine_config;
  192. struct soc_enum;
  193. struct snd_soc_ac97_ops;
  194. struct snd_soc_clock_info;
  195. typedef int (*hw_write_t)(void *,const char* ,int);
  196. typedef int (*hw_read_t)(void *,char* ,int);
  197. extern struct snd_ac97_bus_ops soc_ac97_ops;
  198. /* pcm <-> DAI connect */
  199. void snd_soc_free_pcms(struct snd_soc_device *socdev);
  200. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid);
  201. int snd_soc_register_card(struct snd_soc_device *socdev);
  202. /* set runtime hw params */
  203. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  204. const struct snd_pcm_hardware *hw);
  205. /* codec IO */
  206. #define snd_soc_read(codec, reg) codec->read(codec, reg)
  207. #define snd_soc_write(codec, reg, value) codec->write(codec, reg, value)
  208. /* codec register bit access */
  209. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  210. unsigned short mask, unsigned short value);
  211. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  212. unsigned short mask, unsigned short value);
  213. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  214. struct snd_ac97_bus_ops *ops, int num);
  215. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec);
  216. /*
  217. *Controls
  218. */
  219. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  220. void *data, char *long_name);
  221. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  222. struct snd_ctl_elem_info *uinfo);
  223. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  224. struct snd_ctl_elem_info *uinfo);
  225. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  226. struct snd_ctl_elem_value *ucontrol);
  227. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  228. struct snd_ctl_elem_value *ucontrol);
  229. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  230. struct snd_ctl_elem_info *uinfo);
  231. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  232. struct snd_ctl_elem_info *uinfo);
  233. #define snd_soc_info_bool_ext snd_ctl_boolean_mono_info
  234. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  235. struct snd_ctl_elem_value *ucontrol);
  236. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  237. struct snd_ctl_elem_value *ucontrol);
  238. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  239. struct snd_ctl_elem_info *uinfo);
  240. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  241. struct snd_ctl_elem_value *ucontrol);
  242. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  243. struct snd_ctl_elem_value *ucontrol);
  244. /* SoC PCM stream information */
  245. struct snd_soc_pcm_stream {
  246. char *stream_name;
  247. u64 formats; /* SNDRV_PCM_FMTBIT_* */
  248. unsigned int rates; /* SNDRV_PCM_RATE_* */
  249. unsigned int rate_min; /* min rate */
  250. unsigned int rate_max; /* max rate */
  251. unsigned int channels_min; /* min channels */
  252. unsigned int channels_max; /* max channels */
  253. unsigned int active:1; /* stream is in use */
  254. };
  255. /* SoC audio ops */
  256. struct snd_soc_ops {
  257. int (*startup)(struct snd_pcm_substream *);
  258. void (*shutdown)(struct snd_pcm_substream *);
  259. int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
  260. int (*hw_free)(struct snd_pcm_substream *);
  261. int (*prepare)(struct snd_pcm_substream *);
  262. int (*trigger)(struct snd_pcm_substream *, int);
  263. };
  264. /* ASoC DAI ops */
  265. struct snd_soc_dai_ops {
  266. /* DAI clocking configuration */
  267. int (*set_sysclk)(struct snd_soc_codec_dai *codec_dai,
  268. int clk_id, unsigned int freq, int dir);
  269. int (*set_pll)(struct snd_soc_codec_dai *codec_dai,
  270. int pll_id, unsigned int freq_in, unsigned int freq_out);
  271. int (*set_clkdiv)(struct snd_soc_codec_dai *codec_dai,
  272. int div_id, int div);
  273. /* DAI format configuration */
  274. int (*set_fmt)(struct snd_soc_codec_dai *codec_dai,
  275. unsigned int fmt);
  276. int (*set_tdm_slot)(struct snd_soc_codec_dai *codec_dai,
  277. unsigned int mask, int slots);
  278. int (*set_tristate)(struct snd_soc_codec_dai *, int tristate);
  279. /* digital mute */
  280. int (*digital_mute)(struct snd_soc_codec_dai *, int mute);
  281. };
  282. /* SoC Codec DAI */
  283. struct snd_soc_codec_dai {
  284. char *name;
  285. int id;
  286. unsigned char type;
  287. /* DAI capabilities */
  288. struct snd_soc_pcm_stream playback;
  289. struct snd_soc_pcm_stream capture;
  290. /* DAI runtime info */
  291. struct snd_soc_codec *codec;
  292. unsigned int active;
  293. unsigned char pop_wait:1;
  294. /* ops */
  295. struct snd_soc_ops ops;
  296. struct snd_soc_dai_ops dai_ops;
  297. /* DAI private data */
  298. void *private_data;
  299. };
  300. /* SoC CPU DAI */
  301. struct snd_soc_cpu_dai {
  302. /* DAI description */
  303. char *name;
  304. unsigned int id;
  305. unsigned char type;
  306. /* DAI callbacks */
  307. int (*probe)(struct platform_device *pdev);
  308. void (*remove)(struct platform_device *pdev);
  309. int (*suspend)(struct platform_device *pdev,
  310. struct snd_soc_cpu_dai *cpu_dai);
  311. int (*resume)(struct platform_device *pdev,
  312. struct snd_soc_cpu_dai *cpu_dai);
  313. /* ops */
  314. struct snd_soc_ops ops;
  315. struct snd_soc_dai_ops dai_ops;
  316. /* DAI capabilities */
  317. struct snd_soc_pcm_stream capture;
  318. struct snd_soc_pcm_stream playback;
  319. /* DAI runtime info */
  320. struct snd_pcm_runtime *runtime;
  321. unsigned char active:1;
  322. void *dma_data;
  323. /* DAI private data */
  324. void *private_data;
  325. };
  326. /* SoC Audio Codec */
  327. struct snd_soc_codec {
  328. char *name;
  329. struct module *owner;
  330. struct mutex mutex;
  331. /* callbacks */
  332. int (*set_bias_level)(struct snd_soc_codec *,
  333. enum snd_soc_bias_level level);
  334. /* runtime */
  335. struct snd_card *card;
  336. struct snd_ac97 *ac97; /* for ad-hoc ac97 devices */
  337. unsigned int active;
  338. unsigned int pcm_devs;
  339. void *private_data;
  340. /* codec IO */
  341. void *control_data; /* codec control (i2c/3wire) data */
  342. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  343. int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
  344. hw_write_t hw_write;
  345. hw_read_t hw_read;
  346. void *reg_cache;
  347. short reg_cache_size;
  348. short reg_cache_step;
  349. /* dapm */
  350. struct list_head dapm_widgets;
  351. struct list_head dapm_paths;
  352. enum snd_soc_bias_level bias_level;
  353. enum snd_soc_bias_level suspend_bias_level;
  354. struct delayed_work delayed_work;
  355. /* codec DAI's */
  356. struct snd_soc_codec_dai *dai;
  357. unsigned int num_dai;
  358. };
  359. /* codec device */
  360. struct snd_soc_codec_device {
  361. int (*probe)(struct platform_device *pdev);
  362. int (*remove)(struct platform_device *pdev);
  363. int (*suspend)(struct platform_device *pdev, pm_message_t state);
  364. int (*resume)(struct platform_device *pdev);
  365. };
  366. /* SoC platform interface */
  367. struct snd_soc_platform {
  368. char *name;
  369. int (*probe)(struct platform_device *pdev);
  370. int (*remove)(struct platform_device *pdev);
  371. int (*suspend)(struct platform_device *pdev,
  372. struct snd_soc_cpu_dai *cpu_dai);
  373. int (*resume)(struct platform_device *pdev,
  374. struct snd_soc_cpu_dai *cpu_dai);
  375. /* pcm creation and destruction */
  376. int (*pcm_new)(struct snd_card *, struct snd_soc_codec_dai *,
  377. struct snd_pcm *);
  378. void (*pcm_free)(struct snd_pcm *);
  379. /* platform stream ops */
  380. struct snd_pcm_ops *pcm_ops;
  381. };
  382. /* SoC machine DAI configuration, glues a codec and cpu DAI together */
  383. struct snd_soc_dai_link {
  384. char *name; /* Codec name */
  385. char *stream_name; /* Stream name */
  386. /* DAI */
  387. struct snd_soc_codec_dai *codec_dai;
  388. struct snd_soc_cpu_dai *cpu_dai;
  389. /* machine stream operations */
  390. struct snd_soc_ops *ops;
  391. /* codec/machine specific init - e.g. add machine controls */
  392. int (*init)(struct snd_soc_codec *codec);
  393. /* DAI pcm */
  394. struct snd_pcm *pcm;
  395. };
  396. /* SoC machine */
  397. struct snd_soc_machine {
  398. char *name;
  399. int (*probe)(struct platform_device *pdev);
  400. int (*remove)(struct platform_device *pdev);
  401. /* the pre and post PM functions are used to do any PM work before and
  402. * after the codec and DAI's do any PM work. */
  403. int (*suspend_pre)(struct platform_device *pdev, pm_message_t state);
  404. int (*suspend_post)(struct platform_device *pdev, pm_message_t state);
  405. int (*resume_pre)(struct platform_device *pdev);
  406. int (*resume_post)(struct platform_device *pdev);
  407. /* callbacks */
  408. int (*set_bias_level)(struct snd_soc_machine *,
  409. enum snd_soc_bias_level level);
  410. /* CPU <--> Codec DAI links */
  411. struct snd_soc_dai_link *dai_link;
  412. int num_links;
  413. };
  414. /* SoC Device - the audio subsystem */
  415. struct snd_soc_device {
  416. struct device *dev;
  417. struct snd_soc_machine *machine;
  418. struct snd_soc_platform *platform;
  419. struct snd_soc_codec *codec;
  420. struct snd_soc_codec_device *codec_dev;
  421. struct delayed_work delayed_work;
  422. void *codec_data;
  423. };
  424. /* runtime channel data */
  425. struct snd_soc_pcm_runtime {
  426. struct snd_soc_dai_link *dai;
  427. struct snd_soc_device *socdev;
  428. };
  429. /* enumerated kcontrol */
  430. struct soc_enum {
  431. unsigned short reg;
  432. unsigned short reg2;
  433. unsigned char shift_l;
  434. unsigned char shift_r;
  435. unsigned int mask;
  436. const char **texts;
  437. void *dapm;
  438. };
  439. #endif