4 The codec driver is generic and hardware independent code that configures the
5 codec to provide audio capture and playback. It should contain no code that is
6 specific to the target platform or machine. All platform and machine specific
7 code should be added to the platform and machine drivers respectively.
9 Each codec driver *must* provide the following features:-
11 1) Codec DAI and PCM configuration
12 2) Codec control IO - using I2C, 3 Wire(SPI) or both APIs
13 3) Mixers and audio controls
14 4) Codec audio operations
16 Optionally, codec drivers can also provide:-
19 6) DAPM event handler.
20 7) DAC Digital mute control.
22 Its probably best to use this guide in conjunction with the existing codec
23 driver code in sound/soc/codecs/
25 ASoC Codec driver breakdown
26 ===========================
28 1 - Codec DAI and PCM configuration
29 -----------------------------------
30 Each codec driver must have a struct snd_soc_dai_driver to define its DAI and
31 PCM capabilities and operations. This struct is exported so that it can be
32 registered with the core by your machine driver.
36 static struct snd_soc_dai_ops wm8731_dai_ops = {
37 .prepare = wm8731_pcm_prepare,
38 .hw_params = wm8731_hw_params,
39 .shutdown = wm8731_shutdown,
40 .digital_mute = wm8731_mute,
41 .set_sysclk = wm8731_set_dai_sysclk,
42 .set_fmt = wm8731_set_dai_fmt,
45 struct snd_soc_dai_driver wm8731_dai = {
46 .name = "wm8731-hifi",
48 .stream_name = "Playback",
51 .rates = WM8731_RATES,
52 .formats = WM8731_FORMATS,},
54 .stream_name = "Capture",
57 .rates = WM8731_RATES,
58 .formats = WM8731_FORMATS,},
59 .ops = &wm8731_dai_ops,
66 The codec can usually be controlled via an I2C or SPI style interface
67 (AC97 combines control with data in the DAI). The codec drivers provide
68 functions to read and write the codec registers along with supplying a
71 /* IO control data and register cache */
72 void *control_data; /* codec control (i2c/3wire) data */
75 Codec read/write should do any data formatting and call the hardware
76 read write below to perform the IO. These functions are called by the
77 core and ALSA when performing DAPM or changing the mixer:-
79 unsigned int (*read)(struct snd_soc_codec *, unsigned int);
80 int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
82 Codec hardware IO functions - usually points to either the I2C, SPI or AC97
89 3 - Mixers and audio controls
90 -----------------------------
91 All the codec mixers and audio controls can be defined using the convenience
92 macros defined in soc.h.
94 #define SOC_SINGLE(xname, reg, shift, mask, invert)
96 Defines a single control as follows:-
98 xname = Control name e.g. "Playback Volume"
100 shift = control bit(s) offset in register
101 mask = control bit size(s) e.g. mask of 7 = 3 bits
102 invert = the control is inverted
104 Other macros include:-
106 #define SOC_DOUBLE(xname, reg, shift_left, shift_right, mask, invert)
110 #define SOC_DOUBLE_R(xname, reg_left, reg_right, shift, mask, invert)
112 A stereo control spanning 2 registers
114 #define SOC_ENUM_SINGLE(xreg, xshift, xmask, xtexts)
116 Defines an single enumerated control as follows:-
119 xshift = control bit(s) offset in register
120 xmask = control bit(s) size
121 xtexts = pointer to array of strings that describe each setting
123 #define SOC_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmask, xtexts)
125 Defines a stereo enumerated control
128 4 - Codec Audio Operations
129 --------------------------
130 The codec driver also supports the following ALSA operations:-
134 int (*startup)(struct snd_pcm_substream *);
135 void (*shutdown)(struct snd_pcm_substream *);
136 int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
137 int (*hw_free)(struct snd_pcm_substream *);
138 int (*prepare)(struct snd_pcm_substream *);
141 Please refer to the ALSA driver PCM documentation for details.
142 http://www.alsa-project.org/~iwai/writing-an-alsa-driver/
145 5 - DAPM description.
146 ---------------------
147 The Dynamic Audio Power Management description describes the codec power
148 components and their relationships and registers to the ASoC core.
149 Please read dapm.txt for details of building the description.
151 Please also see the examples in other codec drivers.
154 6 - DAPM event handler
155 ----------------------
156 This function is a callback that handles codec domain PM calls and system
157 domain PM calls (e.g. suspend and resume). It is used to put the codec
158 to sleep when not in use.
162 SNDRV_CTL_POWER_D0: /* full On */
163 /* vref/mid, clk and osc on, active */
165 SNDRV_CTL_POWER_D1: /* partial On */
166 SNDRV_CTL_POWER_D2: /* partial On */
168 SNDRV_CTL_POWER_D3hot: /* Off, with power */
169 /* everything off except vref/vmid, inactive */
171 SNDRV_CTL_POWER_D3cold: /* Everything Off, without power */
174 7 - Codec DAC digital mute control
175 ----------------------------------
176 Most codecs have a digital mute before the DACs that can be used to
177 minimise any system noise. The mute stops any digital data from
180 A callback can be created that is called by the core for each codec DAI
181 when the mute is applied or freed.
185 static int wm8974_mute(struct snd_soc_dai *dai, int mute)
187 struct snd_soc_codec *codec = dai->codec;
188 u16 mute_reg = snd_soc_read(codec, WM8974_DAC) & 0xffbf;
191 snd_soc_write(codec, WM8974_DAC, mute_reg | 0x40);
193 snd_soc_write(codec, WM8974_DAC, mute_reg);