aboutsummaryrefslogtreecommitdiff
path: root/Documentation/sound/alsa/hda_codec.txt
blob: 8e1b02526698c55da2c8da8964f480a108a53cb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
Notes on Universal Interface for Intel High Definition Audio Codec
------------------------------------------------------------------

Takashi Iwai <tiwai@suse.de>


[Still a draft version]


General
=======

The snd-hda-codec module supports the generic access function for the
High Definition (HD) audio codecs.  It's designed to be independent
from the controller code like ac97 codec module.  The real accessors
from/to the controller must be implemented in the lowlevel driver.

The structure of this module is similar with ac97_codec module.
Each codec chip belongs to a bus class which communicates with the
controller.


Initialization of Bus Instance
==============================

The card driver has to create struct hda_bus at first.  The template
struct should be filled and passed to the constructor:

struct hda_bus_template {
	void *private_data;
	struct pci_dev *pci;
	const char *modelname;
	struct hda_bus_ops ops;
};

The card driver can set and use the private_data field to retrieve its
own data in callback functions.  The pci field is used when the patch
needs to check the PCI subsystem IDs, so on.  For non-PCI system, it
doesn't have to be set, of course.
The modelname field specifies the board's specific configuration.  The
string is passed to the codec parser, and it depends on the parser how
the string is used.
These fields, private_data, pci and modelname are all optional.

The ops field contains the callback functions as the following:

struct hda_bus_ops {
	int (*command)(struct hda_codec *codec, hda_nid_t nid, int direct,
		       unsigned int verb, unsigned int parm);
	unsigned int (*get_response)(struct hda_codec *codec);
	void (*private_free)(struct hda_bus *);
#ifdef CONFIG_SND_HDA_POWER_SAVE
	void (*pm_notify)(struct hda_codec *codec);
#endif
};

The command callback is called when the codec module needs to send a
VERB to the controller.  It's always a single command.
The get_response callback is called when the codec requires the answer
for the last command.  These two callbacks are mandatory and have to
be given.
The third, private_free callback, is optional.  It's called in the
destructor to release any necessary data in the lowlevel driver.

The pm_notify callback is available only with
CONFIG_SND_HDA_POWER_SAVE kconfig.  It's called when the codec needs
to power up or may power down.  The controller should check the all
belonging codecs on the bus whether they are actually powered off
(check codec->power_on), and optionally the driver may power down the
contoller side, too.

The bus instance is created via snd_hda_bus_new().  You need to pass
the card instance, the template, and the pointer to store the
resultant bus instance.

int snd_hda_bus_new(struct snd_card *card, const struct hda_bus_template *temp,
		    struct hda_bus **busp);

It returns zero if successful.  A negative return value means any
error during creation.


Creation of Codec Instance
==========================

Each codec chip on the board is then created on the BUS instance.
To create a codec instance, call snd_hda_codec_new().

int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
		      struct hda_codec **codecp);

The first argument is the BUS instance, the second argument is the
address of the codec, and the last one is the pointer to store the
resultant codec instance (can be NULL if not needed).

The codec is stored in a linked list of bus instance.  You can follow
the codec list like:

	struct hda_codec *codec;
	list_for_each_entry(codec, &bus->codec_list, list) {
		...
	}

The codec isn't initialized at this stage properly.  The
initialization sequence is called when the controls are built later.


Codec Access
============

To access codec, use snd_hda_codec_read() and snd_hda_codec_write().
snd_hda_param_read() is for reading parameters.
For writing a sequence of verbs, use snd_hda_sequence_write().

There are variants of cached read/write, snd_hda_codec_write_cache(),
snd_hda_sequence_write_cache().  These are used for recording the
register states for the power-mangement resume.  When no PM is needed,
these are equivalent with non-cached version.

To retrieve the number of sub nodes connected to the given node, use
snd_hda_get_sub_nodes().  The connection list can be obtained via
snd_hda_get_connections() call.

When an unsolicited event happens, pass the event via
snd_hda_queue_unsol_event() so that the codec routines will process it
later.


(Mixer) Controls
================

To create mixer controls of all codecs, call
snd_hda_build_controls().  It then builds the mixers and does
initialization stuff on each codec.


PCM Stuff
=========

snd_hda_build_pcms() gives the necessary information to create PCM
streams.  When it's called, each codec belonging to the bus stores 
codec->num_pcms and codec->pcm_info fields.  The num_pcms indicates
the number of elements in pcm_info array.  The card driver is supposed
to traverse the codec linked list, read the pcm information in
pcm_info array, and build pcm instances according to them. 

The pcm_info array contains the following record:

/* PCM information for each substream */
struct hda_pcm_stream {
	unsigned int substreams;	/* number of substreams, 0 = not exist */
	unsigned int channels_min;	/* min. number of channels */
	unsigned int channels_max;	/* max. number of channels */
	hda_nid_t nid;	/* default NID to query rates/formats/bps, or set up */
	u32 rates;	/* supported rates */
	u64 formats;	/* supported formats (SNDRV_PCM_FMTBIT_) */
	unsigned int maxbps;	/* supported max. bit per sample */
	struct hda_pcm_ops ops;
};

/* for PCM creation */
struct hda_pcm {
	char *name;
	struct hda_pcm_stream stream[2];
};

The name can be passed to snd_pcm_new().  The stream field contains
the information  for playback (SNDRV_PCM_STREAM_PLAYBACK = 0) and
capture (SNDRV_PCM_STREAM_CAPTURE = 1) directions.  The card driver
should pass substreams to snd_pcm_new() for the number of substreams
to create.

The channels_min, channels_max, rates and formats should be copied to
runtime->hw record.  They and maxbps fields are used also to compute
the format value for the HDA codec and controller.  Call
snd_hda_calc_stream_format() to get the format value.

The ops field contains the following callback functions:

struct hda_pcm_ops {
	int (*open)(struct hda_pcm_stream *info, struct hda_codec *codec,
		    struct snd_pcm_substream *substream);
	int (*close)(struct hda_pcm_stream *info, struct hda_codec *codec,
		     struct snd_pcm_substream *substream);
	int (*prepare)(struct hda_pcm_stream *info, struct hda_codec *codec,
		       unsigned int stream_tag, unsigned int format,
		       struct snd_pcm_substream *substream);
	int (*cleanup)(struct hda_pcm_stream *info, struct hda_codec *codec,
		       struct snd_pcm_substream *substream);
};

All are non-NULL, so you can call them safely without NULL check.

The open callback should be called in PCM open after runtime->hw is
set up.  It may override some setting and constraints additionally.
Similarly, the close callback should be called in the PCM close.

The prepare callback should be called in PCM prepare.  This will set
up the codec chip properly for the operation.  The cleanup should be
called in hw_free to clean up the configuration.

The caller should check the return value, at least for open and
prepare callbacks.  When a negative value is returned, some error
occurred.


Proc Files
==========

Each codec dumps the widget node information in
/proc/asound/card*/codec#* file.  This information would be really
helpful for debugging.  Please provide its contents together with the
bug report.


Power Management
================

It's simple:
Call snd_hda_suspend() in the PM suspend callback.
Call snd_hda_resume() in the PM resume callback.


Codec Preset (Patch)
====================

To set up and handle the codec functionality fully, each codec may
have a codec preset (patch).  It's defined in struct hda_codec_preset:

	struct hda_codec_preset {
		unsigned int id;
		unsigned int mask;
		unsigned int subs;
		unsigned int subs_mask;
		unsigned int rev;
		const char *name;
		int (*patch)(struct hda_codec *codec);
	};

When the codec id and codec subsystem id match with the given id and
subs fields bitwise (with bitmask mask and subs_mask), the callback
patch is called.  The patch callback should initialize the codec and
set the codec->patch_ops field.  This is defined as below:

	struct hda_codec_ops {
		int (*build_controls)(struct hda_codec *codec);
		int (*build_pcms)(struct hda_codec *codec);
		int (*init)(struct hda_codec *codec);
		void (*free)(struct hda_codec *codec);
		void (*unsol_event)(struct hda_codec *codec, unsigned int res);
	#ifdef CONFIG_PM
		int (*suspend)(struct hda_codec *codec, pm_message_t state);
		int (*resume)(struct hda_codec *codec);
	#endif
	#ifdef CONFIG_SND_HDA_POWER_SAVE
		int (*check_power_status)(struct hda_codec *codec,
					  hda_nid_t nid);
	#endif
	};

The build_controls callback is called from snd_hda_build_controls().
Similarly, the build_pcms callback is called from
snd_hda_build_pcms().  The init callback is called after
build_controls to initialize the hardware.
The free callback is called as a destructor.

The unsol_event callback is called when an unsolicited event is
received.

The suspend and resume callbacks are for power management.
They can be NULL if no special sequence is required.  When the resume
callback is NULL, the driver calls the init callback and resumes the
registers from the cache.  If other handling is needed, you'd need to
write your own resume callback.  There, the amp values can be resumed
via
	void snd_hda_codec_resume_amp(struct hda_codec *codec);
and the other codec registers via
	void snd_hda_codec_resume_cache(struct hda_codec *codec);

The check_power_status callback is called when the amp value of the
given widget NID is changed.  The codec code can turn on/off the power
appropriately from this information.

Each entry can be NULL if not necessary to be called.


Generic Parser
==============

When the device doesn't match with any given presets, the widgets are
parsed via th generic parser (hda_generic.c).  Its support is
limited: no multi-channel support, for example.


Digital I/O
===========

Call snd_hda_create_spdif_out_ctls() from the patch to create controls
related with SPDIF out.


Helper Functions
================

snd_hda_get_codec_name() stores the codec name on the given string.

snd_hda_check_board_config() can be used to obtain the configuration
information matching with the device.  Define the model string table
and the table with struct snd_pci_quirk entries (zero-terminated),
and pass it to the function.  The function checks the modelname given
as a module parameter, and PCI subsystem IDs.  If the matching entry
is found, it returns the config field value.

snd_hda_add_new_ctls() can be used to create and add control entries.
Pass the zero-terminated array of struct snd_kcontrol_new

Macros HDA_CODEC_VOLUME(), HDA_CODEC_MUTE() and their variables can be
used for the entry of struct snd_kcontrol_new.

The input MUX helper callbacks for such a control are provided, too:
snd_hda_input_mux_info() and snd_hda_input_mux_put().  See
patch_realtek.c for example.