soc-dai.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * linux/sound/soc-dai.h -- ALSA SoC Layer
  4. *
  5. * Copyright: 2005-2008 Wolfson Microelectronics. PLC.
  6. *
  7. * Digital Audio Interface (DAI) API.
  8. */
  9. #ifndef __LINUX_SND_SOC_DAI_H
  10. #define __LINUX_SND_SOC_DAI_H
  11. #include <linux/list.h>
  12. #include <sound/asoc.h>
  13. struct snd_pcm_substream;
  14. struct snd_soc_dapm_widget;
  15. struct snd_compr_stream;
  16. /*
  17. * DAI hardware audio formats.
  18. *
  19. * Describes the physical PCM data formating and clocking. Add new formats
  20. * to the end.
  21. */
  22. #define SND_SOC_DAIFMT_I2S SND_SOC_DAI_FORMAT_I2S
  23. #define SND_SOC_DAIFMT_RIGHT_J SND_SOC_DAI_FORMAT_RIGHT_J
  24. #define SND_SOC_DAIFMT_LEFT_J SND_SOC_DAI_FORMAT_LEFT_J
  25. #define SND_SOC_DAIFMT_DSP_A SND_SOC_DAI_FORMAT_DSP_A
  26. #define SND_SOC_DAIFMT_DSP_B SND_SOC_DAI_FORMAT_DSP_B
  27. #define SND_SOC_DAIFMT_AC97 SND_SOC_DAI_FORMAT_AC97
  28. #define SND_SOC_DAIFMT_PDM SND_SOC_DAI_FORMAT_PDM
  29. /* left and right justified also known as MSB and LSB respectively */
  30. #define SND_SOC_DAIFMT_MSB SND_SOC_DAIFMT_LEFT_J
  31. #define SND_SOC_DAIFMT_LSB SND_SOC_DAIFMT_RIGHT_J
  32. /*
  33. * DAI Clock gating.
  34. *
  35. * DAI bit clocks can be be gated (disabled) when the DAI is not
  36. * sending or receiving PCM data in a frame. This can be used to save power.
  37. */
  38. #define SND_SOC_DAIFMT_CONT (1 << 4) /* continuous clock */
  39. #define SND_SOC_DAIFMT_GATED (0 << 4) /* clock is gated */
  40. /*
  41. * DAI hardware signal polarity.
  42. *
  43. * Specifies whether the DAI can also support inverted clocks for the specified
  44. * format.
  45. *
  46. * BCLK:
  47. * - "normal" polarity means signal is available at rising edge of BCLK
  48. * - "inverted" polarity means signal is available at falling edge of BCLK
  49. *
  50. * FSYNC "normal" polarity depends on the frame format:
  51. * - I2S: frame consists of left then right channel data. Left channel starts
  52. * with falling FSYNC edge, right channel starts with rising FSYNC edge.
  53. * - Left/Right Justified: frame consists of left then right channel data.
  54. * Left channel starts with rising FSYNC edge, right channel starts with
  55. * falling FSYNC edge.
  56. * - DSP A/B: Frame starts with rising FSYNC edge.
  57. * - AC97: Frame starts with rising FSYNC edge.
  58. *
  59. * "Negative" FSYNC polarity is the one opposite of "normal" polarity.
  60. */
  61. #define SND_SOC_DAIFMT_NB_NF (0 << 8) /* normal bit clock + frame */
  62. #define SND_SOC_DAIFMT_NB_IF (2 << 8) /* normal BCLK + inv FRM */
  63. #define SND_SOC_DAIFMT_IB_NF (3 << 8) /* invert BCLK + nor FRM */
  64. #define SND_SOC_DAIFMT_IB_IF (4 << 8) /* invert BCLK + FRM */
  65. /*
  66. * DAI hardware clock masters.
  67. *
  68. * This is wrt the codec, the inverse is true for the interface
  69. * i.e. if the codec is clk and FRM master then the interface is
  70. * clk and frame slave.
  71. */
  72. #define SND_SOC_DAIFMT_CBM_CFM (1 << 12) /* codec clk & FRM master */
  73. #define SND_SOC_DAIFMT_CBS_CFM (2 << 12) /* codec clk slave & FRM master */
  74. #define SND_SOC_DAIFMT_CBM_CFS (3 << 12) /* codec clk master & frame slave */
  75. #define SND_SOC_DAIFMT_CBS_CFS (4 << 12) /* codec clk & FRM slave */
  76. #define SND_SOC_DAIFMT_FORMAT_MASK 0x000f
  77. #define SND_SOC_DAIFMT_CLOCK_MASK 0x00f0
  78. #define SND_SOC_DAIFMT_INV_MASK 0x0f00
  79. #define SND_SOC_DAIFMT_MASTER_MASK 0xf000
  80. /*
  81. * Master Clock Directions
  82. */
  83. #define SND_SOC_CLOCK_IN 0
  84. #define SND_SOC_CLOCK_OUT 1
  85. #define SND_SOC_STD_AC97_FMTS (SNDRV_PCM_FMTBIT_S8 |\
  86. SNDRV_PCM_FMTBIT_S16_LE |\
  87. SNDRV_PCM_FMTBIT_S16_BE |\
  88. SNDRV_PCM_FMTBIT_S20_3LE |\
  89. SNDRV_PCM_FMTBIT_S20_3BE |\
  90. SNDRV_PCM_FMTBIT_S20_LE |\
  91. SNDRV_PCM_FMTBIT_S20_BE |\
  92. SNDRV_PCM_FMTBIT_S24_3LE |\
  93. SNDRV_PCM_FMTBIT_S24_3BE |\
  94. SNDRV_PCM_FMTBIT_S32_LE |\
  95. SNDRV_PCM_FMTBIT_S32_BE)
  96. struct snd_soc_dai_driver;
  97. struct snd_soc_dai;
  98. struct snd_ac97_bus_ops;
  99. /* Digital Audio Interface clocking API.*/
  100. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  101. unsigned int freq, int dir);
  102. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  103. int div_id, int div);
  104. int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
  105. int pll_id, int source, unsigned int freq_in, unsigned int freq_out);
  106. int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio);
  107. /* Digital Audio interface formatting */
  108. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt);
  109. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  110. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width);
  111. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  112. unsigned int tx_num, unsigned int *tx_slot,
  113. unsigned int rx_num, unsigned int *rx_slot);
  114. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate);
  115. /* Digital Audio Interface mute */
  116. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
  117. int direction);
  118. int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
  119. unsigned int *tx_num, unsigned int *tx_slot,
  120. unsigned int *rx_num, unsigned int *rx_slot);
  121. int snd_soc_dai_is_dummy(struct snd_soc_dai *dai);
  122. struct snd_soc_dai_ops {
  123. /*
  124. * DAI clocking configuration, all optional.
  125. * Called by soc_card drivers, normally in their hw_params.
  126. */
  127. int (*set_sysclk)(struct snd_soc_dai *dai,
  128. int clk_id, unsigned int freq, int dir);
  129. int (*set_pll)(struct snd_soc_dai *dai, int pll_id, int source,
  130. unsigned int freq_in, unsigned int freq_out);
  131. int (*set_clkdiv)(struct snd_soc_dai *dai, int div_id, int div);
  132. int (*set_bclk_ratio)(struct snd_soc_dai *dai, unsigned int ratio);
  133. /*
  134. * DAI format configuration
  135. * Called by soc_card drivers, normally in their hw_params.
  136. */
  137. int (*set_fmt)(struct snd_soc_dai *dai, unsigned int fmt);
  138. int (*xlate_tdm_slot_mask)(unsigned int slots,
  139. unsigned int *tx_mask, unsigned int *rx_mask);
  140. int (*set_tdm_slot)(struct snd_soc_dai *dai,
  141. unsigned int tx_mask, unsigned int rx_mask,
  142. int slots, int slot_width);
  143. int (*set_channel_map)(struct snd_soc_dai *dai,
  144. unsigned int tx_num, unsigned int *tx_slot,
  145. unsigned int rx_num, unsigned int *rx_slot);
  146. int (*get_channel_map)(struct snd_soc_dai *dai,
  147. unsigned int *tx_num, unsigned int *tx_slot,
  148. unsigned int *rx_num, unsigned int *rx_slot);
  149. int (*set_tristate)(struct snd_soc_dai *dai, int tristate);
  150. int (*set_sdw_stream)(struct snd_soc_dai *dai,
  151. void *stream, int direction);
  152. /*
  153. * DAI digital mute - optional.
  154. * Called by soc-core to minimise any pops.
  155. */
  156. int (*digital_mute)(struct snd_soc_dai *dai, int mute);
  157. int (*mute_stream)(struct snd_soc_dai *dai, int mute, int stream);
  158. /*
  159. * ALSA PCM audio operations - all optional.
  160. * Called by soc-core during audio PCM operations.
  161. */
  162. int (*startup)(struct snd_pcm_substream *,
  163. struct snd_soc_dai *);
  164. void (*shutdown)(struct snd_pcm_substream *,
  165. struct snd_soc_dai *);
  166. int (*hw_params)(struct snd_pcm_substream *,
  167. struct snd_pcm_hw_params *, struct snd_soc_dai *);
  168. int (*hw_free)(struct snd_pcm_substream *,
  169. struct snd_soc_dai *);
  170. int (*prepare)(struct snd_pcm_substream *,
  171. struct snd_soc_dai *);
  172. /*
  173. * NOTE: Commands passed to the trigger function are not necessarily
  174. * compatible with the current state of the dai. For example this
  175. * sequence of commands is possible: START STOP STOP.
  176. * So do not unconditionally use refcounting functions in the trigger
  177. * function, e.g. clk_enable/disable.
  178. */
  179. int (*trigger)(struct snd_pcm_substream *, int,
  180. struct snd_soc_dai *);
  181. int (*bespoke_trigger)(struct snd_pcm_substream *, int,
  182. struct snd_soc_dai *);
  183. /*
  184. * For hardware based FIFO caused delay reporting.
  185. * Optional.
  186. */
  187. snd_pcm_sframes_t (*delay)(struct snd_pcm_substream *,
  188. struct snd_soc_dai *);
  189. };
  190. struct snd_soc_cdai_ops {
  191. /*
  192. * for compress ops
  193. */
  194. int (*startup)(struct snd_compr_stream *,
  195. struct snd_soc_dai *);
  196. int (*shutdown)(struct snd_compr_stream *,
  197. struct snd_soc_dai *);
  198. int (*set_params)(struct snd_compr_stream *,
  199. struct snd_compr_params *, struct snd_soc_dai *);
  200. int (*get_params)(struct snd_compr_stream *,
  201. struct snd_codec *, struct snd_soc_dai *);
  202. int (*set_metadata)(struct snd_compr_stream *,
  203. struct snd_compr_metadata *, struct snd_soc_dai *);
  204. int (*get_metadata)(struct snd_compr_stream *,
  205. struct snd_compr_metadata *, struct snd_soc_dai *);
  206. int (*trigger)(struct snd_compr_stream *, int,
  207. struct snd_soc_dai *);
  208. int (*pointer)(struct snd_compr_stream *,
  209. struct snd_compr_tstamp *, struct snd_soc_dai *);
  210. int (*ack)(struct snd_compr_stream *, size_t,
  211. struct snd_soc_dai *);
  212. };
  213. /*
  214. * Digital Audio Interface Driver.
  215. *
  216. * Describes the Digital Audio Interface in terms of its ALSA, DAI and AC97
  217. * operations and capabilities. Codec and platform drivers will register this
  218. * structure for every DAI they have.
  219. *
  220. * This structure covers the clocking, formating and ALSA operations for each
  221. * interface.
  222. */
  223. struct snd_soc_dai_driver {
  224. /* DAI description */
  225. const char *name;
  226. unsigned int id;
  227. unsigned int base;
  228. struct snd_soc_dobj dobj;
  229. /* DAI driver callbacks */
  230. int (*probe)(struct snd_soc_dai *dai);
  231. int (*remove)(struct snd_soc_dai *dai);
  232. int (*suspend)(struct snd_soc_dai *dai);
  233. int (*resume)(struct snd_soc_dai *dai);
  234. /* compress dai */
  235. int (*compress_new)(struct snd_soc_pcm_runtime *rtd, int num);
  236. /* Optional Callback used at pcm creation*/
  237. int (*pcm_new)(struct snd_soc_pcm_runtime *rtd,
  238. struct snd_soc_dai *dai);
  239. /* DAI is also used for the control bus */
  240. bool bus_control;
  241. /* ops */
  242. const struct snd_soc_dai_ops *ops;
  243. const struct snd_soc_cdai_ops *cops;
  244. /* DAI capabilities */
  245. struct snd_soc_pcm_stream capture;
  246. struct snd_soc_pcm_stream playback;
  247. unsigned int symmetric_rates:1;
  248. unsigned int symmetric_channels:1;
  249. unsigned int symmetric_samplebits:1;
  250. /* probe ordering - for components with runtime dependencies */
  251. int probe_order;
  252. int remove_order;
  253. };
  254. /*
  255. * Digital Audio Interface runtime data.
  256. *
  257. * Holds runtime data for a DAI.
  258. */
  259. struct snd_soc_dai {
  260. const char *name;
  261. int id;
  262. struct device *dev;
  263. /* driver ops */
  264. struct snd_soc_dai_driver *driver;
  265. /* DAI runtime info */
  266. unsigned int capture_active; /* stream usage count */
  267. unsigned int playback_active; /* stream usage count */
  268. unsigned int probed:1;
  269. unsigned int active;
  270. struct snd_soc_dapm_widget *playback_widget;
  271. struct snd_soc_dapm_widget *capture_widget;
  272. /* DAI DMA data */
  273. void *playback_dma_data;
  274. void *capture_dma_data;
  275. /* Symmetry data - only valid if symmetry is being enforced */
  276. unsigned int rate;
  277. unsigned int channels;
  278. unsigned int sample_bits;
  279. /* parent platform/codec */
  280. struct snd_soc_component *component;
  281. /* CODEC TDM slot masks and params (for fixup) */
  282. unsigned int tx_mask;
  283. unsigned int rx_mask;
  284. struct list_head list;
  285. };
  286. static inline void *snd_soc_dai_get_dma_data(const struct snd_soc_dai *dai,
  287. const struct snd_pcm_substream *ss)
  288. {
  289. return (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  290. dai->playback_dma_data : dai->capture_dma_data;
  291. }
  292. static inline void snd_soc_dai_set_dma_data(struct snd_soc_dai *dai,
  293. const struct snd_pcm_substream *ss,
  294. void *data)
  295. {
  296. if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK)
  297. dai->playback_dma_data = data;
  298. else
  299. dai->capture_dma_data = data;
  300. }
  301. static inline void snd_soc_dai_init_dma_data(struct snd_soc_dai *dai,
  302. void *playback, void *capture)
  303. {
  304. dai->playback_dma_data = playback;
  305. dai->capture_dma_data = capture;
  306. }
  307. static inline void snd_soc_dai_set_drvdata(struct snd_soc_dai *dai,
  308. void *data)
  309. {
  310. dev_set_drvdata(dai->dev, data);
  311. }
  312. static inline void *snd_soc_dai_get_drvdata(struct snd_soc_dai *dai)
  313. {
  314. return dev_get_drvdata(dai->dev);
  315. }
  316. /**
  317. * snd_soc_dai_set_sdw_stream() - Configures a DAI for SDW stream operation
  318. * @dai: DAI
  319. * @stream: STREAM
  320. * @direction: Stream direction(Playback/Capture)
  321. * SoundWire subsystem doesn't have a notion of direction and we reuse
  322. * the ASoC stream direction to configure sink/source ports.
  323. * Playback maps to source ports and Capture for sink ports.
  324. *
  325. * This should be invoked with NULL to clear the stream set previously.
  326. * Returns 0 on success, a negative error code otherwise.
  327. */
  328. static inline int snd_soc_dai_set_sdw_stream(struct snd_soc_dai *dai,
  329. void *stream, int direction)
  330. {
  331. if (dai->driver->ops->set_sdw_stream)
  332. return dai->driver->ops->set_sdw_stream(dai, stream, direction);
  333. else
  334. return -ENOTSUPP;
  335. }
  336. #endif