blob: da442558d9de76aba06072229c035552b3b1bbe2 [file] [log] [blame]
Mark Browna47cbe72008-07-23 14:03:07 +01001/*
2 * linux/sound/soc-dai.h -- ALSA SoC Layer
3 *
4 * Copyright: 2005-2008 Wolfson Microelectronics. PLC.
Duy Truong790f06d2013-02-13 16:38:12 -08005 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
Mark Browna47cbe72008-07-23 14:03:07 +01006 *
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
14#ifndef __LINUX_SND_SOC_DAI_H
15#define __LINUX_SND_SOC_DAI_H
16
17
18#include <linux/list.h>
19
20struct snd_pcm_substream;
Mark Brown888df392012-02-16 19:37:51 -080021struct snd_soc_dapm_widget;
Vinod Koul0ff23612012-08-16 17:10:40 +053022struct snd_compr_stream;
Mark Browna47cbe72008-07-23 14:03:07 +010023
24/*
25 * DAI hardware audio formats.
26 *
27 * Describes the physical PCM data formating and clocking. Add new formats
28 * to the end.
29 */
Mark Brown75d9ac42011-09-27 16:41:01 +010030#define SND_SOC_DAIFMT_I2S 1 /* I2S mode */
31#define SND_SOC_DAIFMT_RIGHT_J 2 /* Right Justified mode */
32#define SND_SOC_DAIFMT_LEFT_J 3 /* Left Justified mode */
33#define SND_SOC_DAIFMT_DSP_A 4 /* L data MSB after FRM LRC */
34#define SND_SOC_DAIFMT_DSP_B 5 /* L data MSB during FRM LRC */
35#define SND_SOC_DAIFMT_AC97 6 /* AC97 */
36#define SND_SOC_DAIFMT_PDM 7 /* Pulse density modulation */
Mark Browna47cbe72008-07-23 14:03:07 +010037
38/* left and right justified also known as MSB and LSB respectively */
39#define SND_SOC_DAIFMT_MSB SND_SOC_DAIFMT_LEFT_J
40#define SND_SOC_DAIFMT_LSB SND_SOC_DAIFMT_RIGHT_J
41
42/*
43 * DAI Clock gating.
44 *
Peter Meerwald47db8e82009-07-13 23:05:11 +010045 * DAI bit clocks can be be gated (disabled) when the DAI is not
Mark Browna47cbe72008-07-23 14:03:07 +010046 * sending or receiving PCM data in a frame. This can be used to save power.
47 */
Mark Brown75d9ac42011-09-27 16:41:01 +010048#define SND_SOC_DAIFMT_CONT (1 << 4) /* continuous clock */
49#define SND_SOC_DAIFMT_GATED (2 << 4) /* clock is gated */
Mark Browna47cbe72008-07-23 14:03:07 +010050
51/*
Mark Browna47cbe72008-07-23 14:03:07 +010052 * DAI hardware signal inversions.
53 *
54 * Specifies whether the DAI can also support inverted clocks for the specified
55 * format.
56 */
Mark Brown75d9ac42011-09-27 16:41:01 +010057#define SND_SOC_DAIFMT_NB_NF (1 << 8) /* normal bit clock + frame */
58#define SND_SOC_DAIFMT_NB_IF (2 << 8) /* normal BCLK + inv FRM */
59#define SND_SOC_DAIFMT_IB_NF (3 << 8) /* invert BCLK + nor FRM */
60#define SND_SOC_DAIFMT_IB_IF (4 << 8) /* invert BCLK + FRM */
Mark Browna47cbe72008-07-23 14:03:07 +010061
62/*
63 * DAI hardware clock masters.
64 *
65 * This is wrt the codec, the inverse is true for the interface
Peter Meerwald47db8e82009-07-13 23:05:11 +010066 * i.e. if the codec is clk and FRM master then the interface is
Mark Browna47cbe72008-07-23 14:03:07 +010067 * clk and frame slave.
68 */
Mark Brown75d9ac42011-09-27 16:41:01 +010069#define SND_SOC_DAIFMT_CBM_CFM (1 << 12) /* codec clk & FRM master */
70#define SND_SOC_DAIFMT_CBS_CFM (2 << 12) /* codec clk slave & FRM master */
71#define SND_SOC_DAIFMT_CBM_CFS (3 << 12) /* codec clk master & frame slave */
72#define SND_SOC_DAIFMT_CBS_CFS (4 << 12) /* codec clk & FRM slave */
Mark Browna47cbe72008-07-23 14:03:07 +010073
74#define SND_SOC_DAIFMT_FORMAT_MASK 0x000f
75#define SND_SOC_DAIFMT_CLOCK_MASK 0x00f0
76#define SND_SOC_DAIFMT_INV_MASK 0x0f00
77#define SND_SOC_DAIFMT_MASTER_MASK 0xf000
78
79/*
80 * Master Clock Directions
81 */
82#define SND_SOC_CLOCK_IN 0
83#define SND_SOC_CLOCK_OUT 1
84
Mark Brown8f738d52009-08-09 20:08:31 +010085#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_S24_3LE |\
91 SNDRV_PCM_FMTBIT_S24_3BE |\
Jon Smirld34c4302009-05-13 21:59:14 -040092 SNDRV_PCM_FMTBIT_S32_LE |\
93 SNDRV_PCM_FMTBIT_S32_BE)
Mark Brown33f503c2009-05-02 12:24:55 +010094
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000095struct snd_soc_dai_driver;
Mark Browna47cbe72008-07-23 14:03:07 +010096struct snd_soc_dai;
97struct snd_ac97_bus_ops;
98
Mark Brown91151712008-11-30 23:31:24 +000099/* Digital Audio Interface registration */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000100int snd_soc_register_dai(struct device *dev,
101 struct snd_soc_dai_driver *dai_drv);
102void snd_soc_unregister_dai(struct device *dev);
103int snd_soc_register_dais(struct device *dev,
104 struct snd_soc_dai_driver *dai_drv, size_t count);
105void snd_soc_unregister_dais(struct device *dev, size_t count);
Mark Brown91151712008-11-30 23:31:24 +0000106
Mark Browna47cbe72008-07-23 14:03:07 +0100107/* Digital Audio Interface clocking API.*/
108int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
109 unsigned int freq, int dir);
110
111int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
112 int div_id, int div);
113
114int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
Mark Brown85488032009-09-05 18:52:16 +0100115 int pll_id, int source, unsigned int freq_in, unsigned int freq_out);
Mark Browna47cbe72008-07-23 14:03:07 +0100116
117/* Digital Audio interface formatting */
118int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt);
119
120int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -0300121 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width);
Mark Browna47cbe72008-07-23 14:03:07 +0100122
Barry Song472df3c2009-09-12 01:16:29 +0800123int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
124 unsigned int tx_num, unsigned int *tx_slot,
125 unsigned int rx_num, unsigned int *rx_slot);
126
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -0800127int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
128 unsigned int *tx_num, unsigned int *tx_slot,
129 unsigned int *rx_num, unsigned int *rx_slot);
130
Mark Browna47cbe72008-07-23 14:03:07 +0100131int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate);
132
133/* Digital Audio Interface mute */
134int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute);
135
Mark Browna47cbe72008-07-23 14:03:07 +0100136struct snd_soc_dai_ops {
137 /*
138 * DAI clocking configuration, all optional.
139 * Called by soc_card drivers, normally in their hw_params.
140 */
141 int (*set_sysclk)(struct snd_soc_dai *dai,
142 int clk_id, unsigned int freq, int dir);
Mark Brown85488032009-09-05 18:52:16 +0100143 int (*set_pll)(struct snd_soc_dai *dai, int pll_id, int source,
144 unsigned int freq_in, unsigned int freq_out);
Mark Browna47cbe72008-07-23 14:03:07 +0100145 int (*set_clkdiv)(struct snd_soc_dai *dai, int div_id, int div);
146
147 /*
148 * DAI format configuration
149 * Called by soc_card drivers, normally in their hw_params.
150 */
151 int (*set_fmt)(struct snd_soc_dai *dai, unsigned int fmt);
152 int (*set_tdm_slot)(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -0300153 unsigned int tx_mask, unsigned int rx_mask,
154 int slots, int slot_width);
Barry Song472df3c2009-09-12 01:16:29 +0800155 int (*set_channel_map)(struct snd_soc_dai *dai,
156 unsigned int tx_num, unsigned int *tx_slot,
157 unsigned int rx_num, unsigned int *rx_slot);
Mark Browna47cbe72008-07-23 14:03:07 +0100158 int (*set_tristate)(struct snd_soc_dai *dai, int tristate);
159
Bharath Ramachandramurthy9c79f132011-11-28 11:18:57 -0800160 int (*get_channel_map)(struct snd_soc_dai *dai,
161 unsigned int *tx_num, unsigned int *tx_slot,
162 unsigned int *rx_num, unsigned int *rx_slot);
Mark Browna47cbe72008-07-23 14:03:07 +0100163 /*
164 * DAI digital mute - optional.
165 * Called by soc-core to minimise any pops.
166 */
167 int (*digital_mute)(struct snd_soc_dai *dai, int mute);
Mark Browndee89c42008-11-18 22:11:38 +0000168
169 /*
170 * ALSA PCM audio operations - all optional.
171 * Called by soc-core during audio PCM operations.
172 */
173 int (*startup)(struct snd_pcm_substream *,
174 struct snd_soc_dai *);
175 void (*shutdown)(struct snd_pcm_substream *,
176 struct snd_soc_dai *);
177 int (*hw_params)(struct snd_pcm_substream *,
178 struct snd_pcm_hw_params *, struct snd_soc_dai *);
179 int (*hw_free)(struct snd_pcm_substream *,
180 struct snd_soc_dai *);
181 int (*prepare)(struct snd_pcm_substream *,
182 struct snd_soc_dai *);
183 int (*trigger)(struct snd_pcm_substream *, int,
184 struct snd_soc_dai *);
Liam Girdwood59c96f12011-03-08 15:11:02 +0000185 int (*bespoke_trigger)(struct snd_pcm_substream *, int,
186 struct snd_soc_dai *);
Peter Ujfalusi258020d2010-03-03 15:08:07 +0200187 /*
188 * For hardware based FIFO caused delay reporting.
189 * Optional.
190 */
191 snd_pcm_sframes_t (*delay)(struct snd_pcm_substream *,
192 struct snd_soc_dai *);
Mark Browna47cbe72008-07-23 14:03:07 +0100193};
194
195/*
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000196 * Digital Audio Interface Driver.
Mark Browna47cbe72008-07-23 14:03:07 +0100197 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000198 * Describes the Digital Audio Interface in terms of its ALSA, DAI and AC97
199 * operations and capabilities. Codec and platform drivers will register this
200 * structure for every DAI they have.
201 *
202 * This structure covers the clocking, formating and ALSA operations for each
203 * interface.
Mark Browna47cbe72008-07-23 14:03:07 +0100204 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000205struct snd_soc_dai_driver {
Mark Browna47cbe72008-07-23 14:03:07 +0100206 /* DAI description */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000207 const char *name;
Mark Browna47cbe72008-07-23 14:03:07 +0100208 unsigned int id;
Mark Brown3ba9e102008-11-24 18:01:05 +0000209 int ac97_control;
Mark Browna47cbe72008-07-23 14:03:07 +0100210
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000211 /* DAI driver callbacks */
212 int (*probe)(struct snd_soc_dai *dai);
213 int (*remove)(struct snd_soc_dai *dai);
Mark Browndc7d7b82008-12-03 18:21:52 +0000214 int (*suspend)(struct snd_soc_dai *dai);
215 int (*resume)(struct snd_soc_dai *dai);
Vinod Koul0ff23612012-08-16 17:10:40 +0530216 /* compress dai */
217 bool compress_dai;
Mark Browna47cbe72008-07-23 14:03:07 +0100218
219 /* ops */
Mark Brown1ee46eb2010-12-02 16:10:09 +0000220 const struct snd_soc_dai_ops *ops;
Mark Browna47cbe72008-07-23 14:03:07 +0100221
222 /* DAI capabilities */
223 struct snd_soc_pcm_stream capture;
224 struct snd_soc_pcm_stream playback;
Mark Brown06f409d2009-04-07 18:10:13 +0100225 unsigned int symmetric_rates:1;
Liam Girdwood0168bf02011-06-07 16:08:05 +0100226
227 /* probe ordering - for components with runtime dependencies */
228 int probe_order;
229 int remove_order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000230};
231
232/*
233 * Digital Audio Interface runtime data.
234 *
235 * Holds runtime data for a DAI.
236 */
237struct snd_soc_dai {
238 const char *name;
239 int id;
240 struct device *dev;
241 void *ac97_pdata; /* platform_data for the ac97 codec */
242
243 /* driver ops */
244 struct snd_soc_dai_driver *driver;
Mark Browna47cbe72008-07-23 14:03:07 +0100245
246 /* DAI runtime info */
Helen Zeng5749b092012-06-10 11:50:29 -0700247 unsigned int capture_active; /* stream is in use */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000248 unsigned int playback_active:1; /* stream is in use */
249 unsigned int symmetric_rates:1;
250 struct snd_pcm_runtime *runtime;
Mark Browna47cbe72008-07-23 14:03:07 +0100251 unsigned int active;
252 unsigned char pop_wait:1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000253 unsigned char probed:1;
Mark Browna47cbe72008-07-23 14:03:07 +0100254
Mark Brown888df392012-02-16 19:37:51 -0800255 struct snd_soc_dapm_widget *playback_widget;
256 struct snd_soc_dapm_widget *capture_widget;
257
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000258 /* DAI DMA data */
259 void *playback_dma_data;
260 void *capture_dma_data;
Mark Browna47cbe72008-07-23 14:03:07 +0100261
Dong Aisheng17841022011-08-29 17:15:14 +0800262 /* Symmetry data - only valid if symmetry is being enforced */
263 unsigned int rate;
264
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000265 /* parent platform/codec */
Mark Brown2466ab92012-03-02 16:18:30 +0000266 struct snd_soc_platform *platform;
267 struct snd_soc_codec *codec;
268
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000269 struct snd_soc_card *card;
Mark Browna47cbe72008-07-23 14:03:07 +0100270
271 struct list_head list;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000272 struct list_head card_list;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700273
274 /* runtime AIF widget and channel mmap updates */
275 u64 playback_channel_map;
276 u64 capture_channel_map;
277 struct snd_soc_dapm_widget *playback_aif;
278 struct snd_soc_dapm_widget *capture_aif;
279 bool channel_map_instanciated;
Mark Browna47cbe72008-07-23 14:03:07 +0100280};
281
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000282static inline void *snd_soc_dai_get_dma_data(const struct snd_soc_dai *dai,
283 const struct snd_pcm_substream *ss)
284{
285 return (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000286 dai->playback_dma_data : dai->capture_dma_data;
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000287}
288
289static inline void snd_soc_dai_set_dma_data(struct snd_soc_dai *dai,
290 const struct snd_pcm_substream *ss,
291 void *data)
292{
293 if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000294 dai->playback_dma_data = data;
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000295 else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000296 dai->capture_dma_data = data;
297}
298
299static inline void snd_soc_dai_set_drvdata(struct snd_soc_dai *dai,
300 void *data)
301{
302 dev_set_drvdata(dai->dev, data);
303}
304
305static inline void *snd_soc_dai_get_drvdata(struct snd_soc_dai *dai)
306{
307 return dev_get_drvdata(dai->dev);
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000308}
309
Liam Girdwoodbfe7dc42011-01-31 21:26:35 +0000310/* Backend DAI PCM ops */
311static inline int snd_soc_dai_startup(struct snd_pcm_substream *substream,
312 struct snd_soc_dai *dai)
313{
314 struct snd_soc_pcm_runtime *rtd = substream->private_data;
315 int ret = 0;
316
317 mutex_lock(&rtd->pcm_mutex);
318
319 if (dai->driver->ops->startup)
320 ret = dai->driver->ops->startup(substream, dai);
321
322 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
323 dai->playback_active++;
324 else
325 dai->capture_active++;
326
327 dai->active++;
328
329 mutex_unlock(&rtd->pcm_mutex);
330 return ret;
331}
332
333static inline void snd_soc_dai_shutdown(struct snd_pcm_substream *substream,
334 struct snd_soc_dai *dai)
335{
336 struct snd_soc_pcm_runtime *rtd = substream->private_data;
337
338 mutex_lock(&rtd->pcm_mutex);
339
340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
341 dai->playback_active--;
342 else
343 dai->capture_active--;
344
345 dai->active--;
346
347 if (dai->driver->ops->shutdown)
348 dai->driver->ops->shutdown(substream, dai);
349 mutex_unlock(&rtd->pcm_mutex);
350}
351
352static inline int snd_soc_dai_hw_params(struct snd_pcm_substream * substream,
353 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
354{
355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
356 int ret = 0;
357
358 mutex_lock(&rtd->pcm_mutex);
359
360 if (dai->driver->ops->hw_params)
361 ret = dai->driver->ops->hw_params(substream, hw_params, dai);
362
363 mutex_unlock(&rtd->pcm_mutex);
364 return ret;
365}
366
367static inline int snd_soc_dai_hw_free(struct snd_pcm_substream *substream,
368 struct snd_soc_dai *dai)
369{
370 struct snd_soc_pcm_runtime *rtd = substream->private_data;
371 int ret = 0;
372
373 mutex_lock(&rtd->pcm_mutex);
374
375 if (dai->driver->ops->hw_free)
376 ret = dai->driver->ops->hw_free(substream, dai);
377
378 mutex_unlock(&rtd->pcm_mutex);
379 return ret;
380}
381
382static inline int snd_soc_dai_prepare(struct snd_pcm_substream *substream,
383 struct snd_soc_dai *dai)
384{
385 struct snd_soc_pcm_runtime *rtd = substream->private_data;
386 int ret = 0;
387
388 mutex_lock(&rtd->pcm_mutex);
389
390 if (dai->driver->ops->prepare)
391 ret = dai->driver->ops->prepare(substream, dai);
392
393 mutex_unlock(&rtd->pcm_mutex);
394 return ret;
395}
396
397static inline int snd_soc_dai_trigger(struct snd_pcm_substream *substream,
398 int cmd, struct snd_soc_dai *dai)
399{
400 if (dai->driver->ops->trigger)
401 return dai->driver->ops->trigger(substream, cmd, dai);
402 return 0;
403}
Mark Browna47cbe72008-07-23 14:03:07 +0100404#endif