soc-dai.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * linux/sound/soc-dai.h -- ALSA SoC Layer
  3. *
  4. * Copyright: 2005-2008 Wolfson Microelectronics. PLC.
  5. * Copyright (c) 2012, The Linux Foundation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Digital Audio Interface (DAI) API.
  12. */
  13. #ifndef __LINUX_SND_SOC_DAI_H
  14. #define __LINUX_SND_SOC_DAI_H
  15. #include <linux/list.h>
  16. struct snd_pcm_substream;
  17. struct snd_soc_dapm_widget;
  18. struct snd_compr_stream;
  19. /*
  20. * DAI hardware audio formats.
  21. *
  22. * Describes the physical PCM data formating and clocking. Add new formats
  23. * to the end.
  24. */
  25. #define SND_SOC_DAIFMT_I2S 1 /* I2S mode */
  26. #define SND_SOC_DAIFMT_RIGHT_J 2 /* Right Justified mode */
  27. #define SND_SOC_DAIFMT_LEFT_J 3 /* Left Justified mode */
  28. #define SND_SOC_DAIFMT_DSP_A 4 /* L data MSB after FRM LRC */
  29. #define SND_SOC_DAIFMT_DSP_B 5 /* L data MSB during FRM LRC */
  30. #define SND_SOC_DAIFMT_AC97 6 /* AC97 */
  31. #define SND_SOC_DAIFMT_PDM 7 /* Pulse density modulation */
  32. /* left and right justified also known as MSB and LSB respectively */
  33. #define SND_SOC_DAIFMT_MSB SND_SOC_DAIFMT_LEFT_J
  34. #define SND_SOC_DAIFMT_LSB SND_SOC_DAIFMT_RIGHT_J
  35. /*
  36. * DAI Clock gating.
  37. *
  38. * DAI bit clocks can be be gated (disabled) when the DAI is not
  39. * sending or receiving PCM data in a frame. This can be used to save power.
  40. */
  41. #define SND_SOC_DAIFMT_CONT (1 << 4) /* continuous clock */
  42. #define SND_SOC_DAIFMT_GATED (2 << 4) /* clock is gated */
  43. /*
  44. * DAI hardware signal inversions.
  45. *
  46. * Specifies whether the DAI can also support inverted clocks for the specified
  47. * format.
  48. */
  49. #define SND_SOC_DAIFMT_NB_NF (1 << 8) /* normal bit clock + frame */
  50. #define SND_SOC_DAIFMT_NB_IF (2 << 8) /* normal BCLK + inv FRM */
  51. #define SND_SOC_DAIFMT_IB_NF (3 << 8) /* invert BCLK + nor FRM */
  52. #define SND_SOC_DAIFMT_IB_IF (4 << 8) /* invert BCLK + FRM */
  53. /*
  54. * DAI hardware clock masters.
  55. *
  56. * This is wrt the codec, the inverse is true for the interface
  57. * i.e. if the codec is clk and FRM master then the interface is
  58. * clk and frame slave.
  59. */
  60. #define SND_SOC_DAIFMT_CBM_CFM (1 << 12) /* codec clk & FRM master */
  61. #define SND_SOC_DAIFMT_CBS_CFM (2 << 12) /* codec clk slave & FRM master */
  62. #define SND_SOC_DAIFMT_CBM_CFS (3 << 12) /* codec clk master & frame slave */
  63. #define SND_SOC_DAIFMT_CBS_CFS (4 << 12) /* codec clk & FRM slave */
  64. #define SND_SOC_DAIFMT_FORMAT_MASK 0x000f
  65. #define SND_SOC_DAIFMT_CLOCK_MASK 0x00f0
  66. #define SND_SOC_DAIFMT_INV_MASK 0x0f00
  67. #define SND_SOC_DAIFMT_MASTER_MASK 0xf000
  68. /*
  69. * Master Clock Directions
  70. */
  71. #define SND_SOC_CLOCK_IN 0
  72. #define SND_SOC_CLOCK_OUT 1
  73. #define SND_SOC_STD_AC97_FMTS (SNDRV_PCM_FMTBIT_S8 |\
  74. SNDRV_PCM_FMTBIT_S16_LE |\
  75. SNDRV_PCM_FMTBIT_S16_BE |\
  76. SNDRV_PCM_FMTBIT_S20_3LE |\
  77. SNDRV_PCM_FMTBIT_S20_3BE |\
  78. SNDRV_PCM_FMTBIT_S24_3LE |\
  79. SNDRV_PCM_FMTBIT_S24_3BE |\
  80. SNDRV_PCM_FMTBIT_S32_LE |\
  81. SNDRV_PCM_FMTBIT_S32_BE)
  82. struct snd_soc_dai_driver;
  83. struct snd_soc_dai;
  84. struct snd_ac97_bus_ops;
  85. /* Digital Audio Interface registration */
  86. int snd_soc_register_dai(struct device *dev,
  87. struct snd_soc_dai_driver *dai_drv);
  88. void snd_soc_unregister_dai(struct device *dev);
  89. int snd_soc_register_dais(struct device *dev,
  90. struct snd_soc_dai_driver *dai_drv, size_t count);
  91. void snd_soc_unregister_dais(struct device *dev, size_t count);
  92. /* Digital Audio Interface clocking API.*/
  93. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  94. unsigned int freq, int dir);
  95. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  96. int div_id, int div);
  97. int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
  98. int pll_id, int source, unsigned int freq_in, unsigned int freq_out);
  99. /* Digital Audio interface formatting */
  100. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt);
  101. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  102. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width);
  103. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  104. unsigned int tx_num, unsigned int *tx_slot,
  105. unsigned int rx_num, unsigned int *rx_slot);
  106. int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
  107. unsigned int *tx_num, unsigned int *tx_slot,
  108. unsigned int *rx_num, unsigned int *rx_slot);
  109. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate);
  110. /* Digital Audio Interface mute */
  111. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute);
  112. struct snd_soc_dai_ops {
  113. /*
  114. * DAI clocking configuration, all optional.
  115. * Called by soc_card drivers, normally in their hw_params.
  116. */
  117. int (*set_sysclk)(struct snd_soc_dai *dai,
  118. int clk_id, unsigned int freq, int dir);
  119. int (*set_pll)(struct snd_soc_dai *dai, int pll_id, int source,
  120. unsigned int freq_in, unsigned int freq_out);
  121. int (*set_clkdiv)(struct snd_soc_dai *dai, int div_id, int div);
  122. /*
  123. * DAI format configuration
  124. * Called by soc_card drivers, normally in their hw_params.
  125. */
  126. int (*set_fmt)(struct snd_soc_dai *dai, unsigned int fmt);
  127. int (*set_tdm_slot)(struct snd_soc_dai *dai,
  128. unsigned int tx_mask, unsigned int rx_mask,
  129. int slots, int slot_width);
  130. int (*set_channel_map)(struct snd_soc_dai *dai,
  131. unsigned int tx_num, unsigned int *tx_slot,
  132. unsigned int rx_num, unsigned int *rx_slot);
  133. int (*set_tristate)(struct snd_soc_dai *dai, int tristate);
  134. int (*get_channel_map)(struct snd_soc_dai *dai,
  135. unsigned int *tx_num, unsigned int *tx_slot,
  136. unsigned int *rx_num, unsigned int *rx_slot);
  137. /*
  138. * DAI digital mute - optional.
  139. * Called by soc-core to minimise any pops.
  140. */
  141. int (*digital_mute)(struct snd_soc_dai *dai, int mute);
  142. /*
  143. * ALSA PCM audio operations - all optional.
  144. * Called by soc-core during audio PCM operations.
  145. */
  146. int (*startup)(struct snd_pcm_substream *,
  147. struct snd_soc_dai *);
  148. void (*shutdown)(struct snd_pcm_substream *,
  149. struct snd_soc_dai *);
  150. int (*hw_params)(struct snd_pcm_substream *,
  151. struct snd_pcm_hw_params *, struct snd_soc_dai *);
  152. int (*hw_free)(struct snd_pcm_substream *,
  153. struct snd_soc_dai *);
  154. int (*prepare)(struct snd_pcm_substream *,
  155. struct snd_soc_dai *);
  156. int (*trigger)(struct snd_pcm_substream *, int,
  157. struct snd_soc_dai *);
  158. int (*bespoke_trigger)(struct snd_pcm_substream *, int,
  159. struct snd_soc_dai *);
  160. /*
  161. * For hardware based FIFO caused delay reporting.
  162. * Optional.
  163. */
  164. snd_pcm_sframes_t (*delay)(struct snd_pcm_substream *,
  165. struct snd_soc_dai *);
  166. };
  167. /*
  168. * Digital Audio Interface Driver.
  169. *
  170. * Describes the Digital Audio Interface in terms of its ALSA, DAI and AC97
  171. * operations and capabilities. Codec and platform drivers will register this
  172. * structure for every DAI they have.
  173. *
  174. * This structure covers the clocking, formating and ALSA operations for each
  175. * interface.
  176. */
  177. struct snd_soc_dai_driver {
  178. /* DAI description */
  179. const char *name;
  180. unsigned int id;
  181. int ac97_control;
  182. /* DAI driver callbacks */
  183. int (*probe)(struct snd_soc_dai *dai);
  184. int (*remove)(struct snd_soc_dai *dai);
  185. int (*suspend)(struct snd_soc_dai *dai);
  186. int (*resume)(struct snd_soc_dai *dai);
  187. /* compress dai */
  188. bool compress_dai;
  189. /* ops */
  190. const struct snd_soc_dai_ops *ops;
  191. /* DAI capabilities */
  192. struct snd_soc_pcm_stream capture;
  193. struct snd_soc_pcm_stream playback;
  194. unsigned int symmetric_rates:1;
  195. /* probe ordering - for components with runtime dependencies */
  196. int probe_order;
  197. int remove_order;
  198. };
  199. /*
  200. * Digital Audio Interface runtime data.
  201. *
  202. * Holds runtime data for a DAI.
  203. */
  204. struct snd_soc_dai {
  205. const char *name;
  206. int id;
  207. struct device *dev;
  208. void *ac97_pdata; /* platform_data for the ac97 codec */
  209. /* driver ops */
  210. struct snd_soc_dai_driver *driver;
  211. /* DAI runtime info */
  212. unsigned int capture_active; /* stream is in use */
  213. unsigned int playback_active:1; /* stream is in use */
  214. unsigned int symmetric_rates:1;
  215. struct snd_pcm_runtime *runtime;
  216. unsigned int active;
  217. unsigned char pop_wait:1;
  218. unsigned char probed:1;
  219. struct snd_soc_dapm_widget *playback_widget;
  220. struct snd_soc_dapm_widget *capture_widget;
  221. /* DAI DMA data */
  222. void *playback_dma_data;
  223. void *capture_dma_data;
  224. /* Symmetry data - only valid if symmetry is being enforced */
  225. unsigned int rate;
  226. /* parent platform/codec */
  227. struct snd_soc_platform *platform;
  228. struct snd_soc_codec *codec;
  229. struct snd_soc_card *card;
  230. struct list_head list;
  231. struct list_head card_list;
  232. /* runtime AIF widget and channel mmap updates */
  233. u64 playback_channel_map;
  234. u64 capture_channel_map;
  235. struct snd_soc_dapm_widget *playback_aif;
  236. struct snd_soc_dapm_widget *capture_aif;
  237. bool channel_map_instanciated;
  238. };
  239. static inline void *snd_soc_dai_get_dma_data(const struct snd_soc_dai *dai,
  240. const struct snd_pcm_substream *ss)
  241. {
  242. return (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  243. dai->playback_dma_data : dai->capture_dma_data;
  244. }
  245. static inline void snd_soc_dai_set_dma_data(struct snd_soc_dai *dai,
  246. const struct snd_pcm_substream *ss,
  247. void *data)
  248. {
  249. if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK)
  250. dai->playback_dma_data = data;
  251. else
  252. dai->capture_dma_data = data;
  253. }
  254. static inline void snd_soc_dai_set_drvdata(struct snd_soc_dai *dai,
  255. void *data)
  256. {
  257. dev_set_drvdata(dai->dev, data);
  258. }
  259. static inline void *snd_soc_dai_get_drvdata(struct snd_soc_dai *dai)
  260. {
  261. return dev_get_drvdata(dai->dev);
  262. }
  263. /* Backend DAI PCM ops */
  264. static inline int snd_soc_dai_startup(struct snd_pcm_substream *substream,
  265. struct snd_soc_dai *dai)
  266. {
  267. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  268. int ret = 0;
  269. mutex_lock(&rtd->pcm_mutex);
  270. if (dai->driver->ops->startup)
  271. ret = dai->driver->ops->startup(substream, dai);
  272. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  273. dai->playback_active++;
  274. else
  275. dai->capture_active++;
  276. dai->active++;
  277. mutex_unlock(&rtd->pcm_mutex);
  278. return ret;
  279. }
  280. static inline void snd_soc_dai_shutdown(struct snd_pcm_substream *substream,
  281. struct snd_soc_dai *dai)
  282. {
  283. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  284. mutex_lock(&rtd->pcm_mutex);
  285. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  286. dai->playback_active--;
  287. else
  288. dai->capture_active--;
  289. dai->active--;
  290. if (dai->driver->ops->shutdown)
  291. dai->driver->ops->shutdown(substream, dai);
  292. mutex_unlock(&rtd->pcm_mutex);
  293. }
  294. static inline int snd_soc_dai_hw_params(struct snd_pcm_substream * substream,
  295. struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
  296. {
  297. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  298. int ret = 0;
  299. mutex_lock(&rtd->pcm_mutex);
  300. if (dai->driver->ops->hw_params)
  301. ret = dai->driver->ops->hw_params(substream, hw_params, dai);
  302. mutex_unlock(&rtd->pcm_mutex);
  303. return ret;
  304. }
  305. static inline int snd_soc_dai_hw_free(struct snd_pcm_substream *substream,
  306. struct snd_soc_dai *dai)
  307. {
  308. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  309. int ret = 0;
  310. mutex_lock(&rtd->pcm_mutex);
  311. if (dai->driver->ops->hw_free)
  312. ret = dai->driver->ops->hw_free(substream, dai);
  313. mutex_unlock(&rtd->pcm_mutex);
  314. return ret;
  315. }
  316. static inline int snd_soc_dai_prepare(struct snd_pcm_substream *substream,
  317. struct snd_soc_dai *dai)
  318. {
  319. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  320. int ret = 0;
  321. mutex_lock(&rtd->pcm_mutex);
  322. if (dai->driver->ops->prepare)
  323. ret = dai->driver->ops->prepare(substream, dai);
  324. mutex_unlock(&rtd->pcm_mutex);
  325. return ret;
  326. }
  327. static inline int snd_soc_dai_trigger(struct snd_pcm_substream *substream,
  328. int cmd, struct snd_soc_dai *dai)
  329. {
  330. if (dai->driver->ops->trigger)
  331. return dai->driver->ops->trigger(substream, cmd, dai);
  332. return 0;
  333. }
  334. #endif