xref: /OK3568_Linux_fs/kernel/Documentation/sound/soc/codec.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=======================
2*4882a593SmuzhiyunASoC Codec Class Driver
3*4882a593Smuzhiyun=======================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThe codec class driver is generic and hardware independent code that configures
6*4882a593Smuzhiyunthe codec, FM, MODEM, BT or external DSP to provide audio capture and playback.
7*4882a593SmuzhiyunIt should contain no code that is specific to the target platform or machine.
8*4882a593SmuzhiyunAll platform and machine specific code should be added to the platform and
9*4882a593Smuzhiyunmachine drivers respectively.
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunEach codec class driver *must* provide the following features:-
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun1. Codec DAI and PCM configuration
14*4882a593Smuzhiyun2. Codec control IO - using RegMap API
15*4882a593Smuzhiyun3. Mixers and audio controls
16*4882a593Smuzhiyun4. Codec audio operations
17*4882a593Smuzhiyun5. DAPM description.
18*4882a593Smuzhiyun6. DAPM event handler.
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunOptionally, codec drivers can also provide:-
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun7. DAC Digital mute control.
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunIts probably best to use this guide in conjunction with the existing codec
25*4882a593Smuzhiyundriver code in sound/soc/codecs/
26*4882a593Smuzhiyun
27*4882a593SmuzhiyunASoC Codec driver breakdown
28*4882a593Smuzhiyun===========================
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunCodec DAI and PCM configuration
31*4882a593Smuzhiyun-------------------------------
32*4882a593SmuzhiyunEach codec driver must have a struct snd_soc_dai_driver to define its DAI and
33*4882a593SmuzhiyunPCM capabilities and operations. This struct is exported so that it can be
34*4882a593Smuzhiyunregistered with the core by your machine driver.
35*4882a593Smuzhiyun
36*4882a593Smuzhiyune.g.
37*4882a593Smuzhiyun::
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun  static struct snd_soc_dai_ops wm8731_dai_ops = {
40*4882a593Smuzhiyun	.prepare	= wm8731_pcm_prepare,
41*4882a593Smuzhiyun	.hw_params	= wm8731_hw_params,
42*4882a593Smuzhiyun	.shutdown	= wm8731_shutdown,
43*4882a593Smuzhiyun	.digital_mute	= wm8731_mute,
44*4882a593Smuzhiyun	.set_sysclk	= wm8731_set_dai_sysclk,
45*4882a593Smuzhiyun	.set_fmt	= wm8731_set_dai_fmt,
46*4882a593Smuzhiyun  };
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun  struct snd_soc_dai_driver wm8731_dai = {
49*4882a593Smuzhiyun	.name = "wm8731-hifi",
50*4882a593Smuzhiyun	.playback = {
51*4882a593Smuzhiyun		.stream_name = "Playback",
52*4882a593Smuzhiyun		.channels_min = 1,
53*4882a593Smuzhiyun		.channels_max = 2,
54*4882a593Smuzhiyun		.rates = WM8731_RATES,
55*4882a593Smuzhiyun		.formats = WM8731_FORMATS,},
56*4882a593Smuzhiyun	.capture = {
57*4882a593Smuzhiyun		.stream_name = "Capture",
58*4882a593Smuzhiyun		.channels_min = 1,
59*4882a593Smuzhiyun		.channels_max = 2,
60*4882a593Smuzhiyun		.rates = WM8731_RATES,
61*4882a593Smuzhiyun		.formats = WM8731_FORMATS,},
62*4882a593Smuzhiyun	.ops = &wm8731_dai_ops,
63*4882a593Smuzhiyun	.symmetric_rates = 1,
64*4882a593Smuzhiyun  };
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun
67*4882a593SmuzhiyunCodec control IO
68*4882a593Smuzhiyun----------------
69*4882a593SmuzhiyunThe codec can usually be controlled via an I2C or SPI style interface
70*4882a593Smuzhiyun(AC97 combines control with data in the DAI). The codec driver should use the
71*4882a593SmuzhiyunRegmap API for all codec IO. Please see include/linux/regmap.h and existing
72*4882a593Smuzhiyuncodec drivers for example regmap usage.
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunMixers and audio controls
76*4882a593Smuzhiyun-------------------------
77*4882a593SmuzhiyunAll the codec mixers and audio controls can be defined using the convenience
78*4882a593Smuzhiyunmacros defined in soc.h.
79*4882a593Smuzhiyun::
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun    #define SOC_SINGLE(xname, reg, shift, mask, invert)
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunDefines a single control as follows:-
84*4882a593Smuzhiyun::
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun  xname = Control name e.g. "Playback Volume"
87*4882a593Smuzhiyun  reg = codec register
88*4882a593Smuzhiyun  shift = control bit(s) offset in register
89*4882a593Smuzhiyun  mask = control bit size(s) e.g. mask of 7 = 3 bits
90*4882a593Smuzhiyun  invert = the control is inverted
91*4882a593Smuzhiyun
92*4882a593SmuzhiyunOther macros include:-
93*4882a593Smuzhiyun::
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun    #define SOC_DOUBLE(xname, reg, shift_left, shift_right, mask, invert)
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunA stereo control
98*4882a593Smuzhiyun::
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun    #define SOC_DOUBLE_R(xname, reg_left, reg_right, shift, mask, invert)
101*4882a593Smuzhiyun
102*4882a593SmuzhiyunA stereo control spanning 2 registers
103*4882a593Smuzhiyun::
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun    #define SOC_ENUM_SINGLE(xreg, xshift, xmask, xtexts)
106*4882a593Smuzhiyun
107*4882a593SmuzhiyunDefines an single enumerated control as follows:-
108*4882a593Smuzhiyun::
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun   xreg = register
111*4882a593Smuzhiyun   xshift = control bit(s) offset in register
112*4882a593Smuzhiyun   xmask = control bit(s) size
113*4882a593Smuzhiyun   xtexts = pointer to array of strings that describe each setting
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun   #define SOC_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmask, xtexts)
116*4882a593Smuzhiyun
117*4882a593SmuzhiyunDefines a stereo enumerated control
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun
120*4882a593SmuzhiyunCodec Audio Operations
121*4882a593Smuzhiyun----------------------
122*4882a593SmuzhiyunThe codec driver also supports the following ALSA PCM operations:-
123*4882a593Smuzhiyun::
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun  /* SoC audio ops */
126*4882a593Smuzhiyun  struct snd_soc_ops {
127*4882a593Smuzhiyun	int (*startup)(struct snd_pcm_substream *);
128*4882a593Smuzhiyun	void (*shutdown)(struct snd_pcm_substream *);
129*4882a593Smuzhiyun	int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
130*4882a593Smuzhiyun	int (*hw_free)(struct snd_pcm_substream *);
131*4882a593Smuzhiyun	int (*prepare)(struct snd_pcm_substream *);
132*4882a593Smuzhiyun  };
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunPlease refer to the ALSA driver PCM documentation for details.
135*4882a593Smuzhiyunhttp://www.alsa-project.org/~iwai/writing-an-alsa-driver/
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunDAPM description
139*4882a593Smuzhiyun----------------
140*4882a593SmuzhiyunThe Dynamic Audio Power Management description describes the codec power
141*4882a593Smuzhiyuncomponents and their relationships and registers to the ASoC core.
142*4882a593SmuzhiyunPlease read dapm.rst for details of building the description.
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunPlease also see the examples in other codec drivers.
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun
147*4882a593SmuzhiyunDAPM event handler
148*4882a593Smuzhiyun------------------
149*4882a593SmuzhiyunThis function is a callback that handles codec domain PM calls and system
150*4882a593Smuzhiyundomain PM calls (e.g. suspend and resume). It is used to put the codec
151*4882a593Smuzhiyunto sleep when not in use.
152*4882a593Smuzhiyun
153*4882a593SmuzhiyunPower states:-
154*4882a593Smuzhiyun::
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun	SNDRV_CTL_POWER_D0: /* full On */
157*4882a593Smuzhiyun	/* vref/mid, clk and osc on, active */
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun	SNDRV_CTL_POWER_D1: /* partial On */
160*4882a593Smuzhiyun	SNDRV_CTL_POWER_D2: /* partial On */
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun	SNDRV_CTL_POWER_D3hot: /* Off, with power */
163*4882a593Smuzhiyun	/* everything off except vref/vmid, inactive */
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun	SNDRV_CTL_POWER_D3cold: /* Everything Off, without power */
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun
168*4882a593SmuzhiyunCodec DAC digital mute control
169*4882a593Smuzhiyun------------------------------
170*4882a593SmuzhiyunMost codecs have a digital mute before the DACs that can be used to
171*4882a593Smuzhiyunminimise any system noise.  The mute stops any digital data from
172*4882a593Smuzhiyunentering the DAC.
173*4882a593Smuzhiyun
174*4882a593SmuzhiyunA callback can be created that is called by the core for each codec DAI
175*4882a593Smuzhiyunwhen the mute is applied or freed.
176*4882a593Smuzhiyun
177*4882a593Smuzhiyuni.e.
178*4882a593Smuzhiyun::
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun  static int wm8974_mute(struct snd_soc_dai *dai, int mute)
181*4882a593Smuzhiyun  {
182*4882a593Smuzhiyun	struct snd_soc_component *component = dai->component;
183*4882a593Smuzhiyun	u16 mute_reg = snd_soc_component_read32(component, WM8974_DAC) & 0xffbf;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun	if (mute)
186*4882a593Smuzhiyun		snd_soc_component_write(component, WM8974_DAC, mute_reg | 0x40);
187*4882a593Smuzhiyun	else
188*4882a593Smuzhiyun		snd_soc_component_write(component, WM8974_DAC, mute_reg);
189*4882a593Smuzhiyun	return 0;
190*4882a593Smuzhiyun  }
191