1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * Local helper functions
5  *
6  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under the terms of the GNU General Public License as published by the Free
10  *  Software Foundation; either version 2 of the License, or (at your option)
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful, but WITHOUT
14  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  *  more details.
17  *
18  *  You should have received a copy of the GNU General Public License along with
19  *  this program; if not, write to the Free Software Foundation, Inc., 59
20  *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22 
23 #ifndef __SOUND_HDA_LOCAL_H
24 #define __SOUND_HDA_LOCAL_H
25 
26 /* We abuse kcontrol_new.subdev field to pass the NID corresponding to
27  * the given new control.  If id.subdev has a bit flag HDA_SUBDEV_NID_FLAG,
28  * snd_hda_ctl_add() takes the lower-bit subdev value as a valid NID.
29  *
30  * Note that the subdevice field is cleared again before the real registration
31  * in snd_hda_ctl_add(), so that this value won't appear in the outside.
32  */
33 #define HDA_SUBDEV_NID_FLAG	(1U << 31)
34 #define HDA_SUBDEV_AMP_FLAG	(1U << 30)
35 
36 /*
37  * for mixer controls
38  */
39 #define HDA_COMPOSE_AMP_VAL_OFS(nid,chs,idx,dir,ofs)		\
40 	((nid) | ((chs)<<16) | ((dir)<<18) | ((idx)<<19) | ((ofs)<<23))
41 #define HDA_AMP_VAL_MIN_MUTE (1<<29)
42 #define HDA_COMPOSE_AMP_VAL(nid,chs,idx,dir) \
43 	HDA_COMPOSE_AMP_VAL_OFS(nid, chs, idx, dir, 0)
44 /* mono volume with index (index=0,1,...) (channel=1,2) */
45 #define HDA_CODEC_VOLUME_MONO_IDX(xname, xcidx, nid, channel, xindex, dir, flags) \
46 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xcidx,  \
47 	  .subdevice = HDA_SUBDEV_AMP_FLAG, \
48 	  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
49 	  	    SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
50 	  	    SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
51 	  .info = snd_hda_mixer_amp_volume_info, \
52 	  .get = snd_hda_mixer_amp_volume_get, \
53 	  .put = snd_hda_mixer_amp_volume_put, \
54 	  .tlv = { .c = snd_hda_mixer_amp_tlv },		\
55 	  .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, xindex, dir) | flags }
56 /* stereo volume with index */
57 #define HDA_CODEC_VOLUME_IDX(xname, xcidx, nid, xindex, direction) \
58 	HDA_CODEC_VOLUME_MONO_IDX(xname, xcidx, nid, 3, xindex, direction, 0)
59 /* mono volume */
60 #define HDA_CODEC_VOLUME_MONO(xname, nid, channel, xindex, direction) \
61 	HDA_CODEC_VOLUME_MONO_IDX(xname, 0, nid, channel, xindex, direction, 0)
62 /* stereo volume */
63 #define HDA_CODEC_VOLUME(xname, nid, xindex, direction) \
64 	HDA_CODEC_VOLUME_MONO(xname, nid, 3, xindex, direction)
65 /* stereo volume with min=mute */
66 #define HDA_CODEC_VOLUME_MIN_MUTE(xname, nid, xindex, direction) \
67 	HDA_CODEC_VOLUME_MONO_IDX(xname, 0, nid, 3, xindex, direction, \
68 				  HDA_AMP_VAL_MIN_MUTE)
69 /* mono mute switch with index (index=0,1,...) (channel=1,2) */
70 #define HDA_CODEC_MUTE_MONO_IDX(xname, xcidx, nid, channel, xindex, direction) \
71 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xcidx, \
72 	  .subdevice = HDA_SUBDEV_AMP_FLAG, \
73 	  .info = snd_hda_mixer_amp_switch_info, \
74 	  .get = snd_hda_mixer_amp_switch_get, \
75 	  .put = snd_hda_mixer_amp_switch_put, \
76 	  .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, xindex, direction) }
77 /* stereo mute switch with index */
78 #define HDA_CODEC_MUTE_IDX(xname, xcidx, nid, xindex, direction) \
79 	HDA_CODEC_MUTE_MONO_IDX(xname, xcidx, nid, 3, xindex, direction)
80 /* mono mute switch */
81 #define HDA_CODEC_MUTE_MONO(xname, nid, channel, xindex, direction) \
82 	HDA_CODEC_MUTE_MONO_IDX(xname, 0, nid, channel, xindex, direction)
83 /* stereo mute switch */
84 #define HDA_CODEC_MUTE(xname, nid, xindex, direction) \
85 	HDA_CODEC_MUTE_MONO(xname, nid, 3, xindex, direction)
86 #ifdef CONFIG_SND_HDA_INPUT_BEEP
87 /* special beep mono mute switch with index (index=0,1,...) (channel=1,2) */
88 #define HDA_CODEC_MUTE_BEEP_MONO_IDX(xname, xcidx, nid, channel, xindex, direction) \
89 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xcidx, \
90 	  .subdevice = HDA_SUBDEV_AMP_FLAG, \
91 	  .info = snd_hda_mixer_amp_switch_info, \
92 	  .get = snd_hda_mixer_amp_switch_get_beep, \
93 	  .put = snd_hda_mixer_amp_switch_put_beep, \
94 	  .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, xindex, direction) }
95 #else
96 /* no digital beep - just the standard one */
97 #define HDA_CODEC_MUTE_BEEP_MONO_IDX(xname, xcidx, nid, ch, xidx, dir) \
98 	HDA_CODEC_MUTE_MONO_IDX(xname, xcidx, nid, ch, xidx, dir)
99 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
100 /* special beep mono mute switch */
101 #define HDA_CODEC_MUTE_BEEP_MONO(xname, nid, channel, xindex, direction) \
102 	HDA_CODEC_MUTE_BEEP_MONO_IDX(xname, 0, nid, channel, xindex, direction)
103 /* special beep stereo mute switch */
104 #define HDA_CODEC_MUTE_BEEP(xname, nid, xindex, direction) \
105 	HDA_CODEC_MUTE_BEEP_MONO(xname, nid, 3, xindex, direction)
106 
107 extern const char *snd_hda_pcm_type_name[];
108 
109 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
110 				  struct snd_ctl_elem_info *uinfo);
111 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
112 				 struct snd_ctl_elem_value *ucontrol);
113 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
114 				 struct snd_ctl_elem_value *ucontrol);
115 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
116 			  unsigned int size, unsigned int __user *tlv);
117 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
118 				  struct snd_ctl_elem_info *uinfo);
119 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
120 				 struct snd_ctl_elem_value *ucontrol);
121 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
122 				 struct snd_ctl_elem_value *ucontrol);
123 #ifdef CONFIG_SND_HDA_INPUT_BEEP
124 int snd_hda_mixer_amp_switch_get_beep(struct snd_kcontrol *kcontrol,
125 				      struct snd_ctl_elem_value *ucontrol);
126 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
127 				      struct snd_ctl_elem_value *ucontrol);
128 #endif
129 /* lowlevel accessor with caching; use carefully */
130 #define snd_hda_codec_amp_read(codec, nid, ch, dir, idx) \
131 	snd_hdac_regmap_get_amp(&(codec)->core, nid, ch, dir, idx)
132 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
133 			     int ch, int dir, int idx, int mask, int val);
134 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
135 			     int dir, int idx, int mask, int val);
136 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
137 			   int direction, int idx, int mask, int val);
138 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
139 				  int dir, int idx, int mask, int val);
140 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
141 			     unsigned int *tlv);
142 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
143 					    const char *name);
144 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
145 			  unsigned int *tlv, const char * const *slaves,
146 			  const char *suffix, bool init_slave_vol,
147 			  struct snd_kcontrol **ctl_ret);
148 #define snd_hda_add_vmaster(codec, name, tlv, slaves, suffix) \
149 	__snd_hda_add_vmaster(codec, name, tlv, slaves, suffix, true, NULL)
150 int snd_hda_codec_reset(struct hda_codec *codec);
151 void snd_hda_codec_register(struct hda_codec *codec);
152 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec);
153 
154 enum {
155 	HDA_VMUTE_OFF,
156 	HDA_VMUTE_ON,
157 	HDA_VMUTE_FOLLOW_MASTER,
158 };
159 
160 struct hda_vmaster_mute_hook {
161 	/* below two fields must be filled by the caller of
162 	 * snd_hda_add_vmaster_hook() beforehand
163 	 */
164 	struct snd_kcontrol *sw_kctl;
165 	void (*hook)(void *, int);
166 	/* below are initialized automatically */
167 	unsigned int mute_mode; /* HDA_VMUTE_XXX */
168 	struct hda_codec *codec;
169 };
170 
171 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
172 			     struct hda_vmaster_mute_hook *hook,
173 			     bool expose_enum_ctl);
174 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook);
175 
176 /* amp value bits */
177 #define HDA_AMP_MUTE	0x80
178 #define HDA_AMP_UNMUTE	0x00
179 #define HDA_AMP_VOLMASK	0x7f
180 
181 /*
182  * SPDIF I/O
183  */
184 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
185 				hda_nid_t associated_nid,
186 				hda_nid_t cvt_nid, int type);
187 #define snd_hda_create_spdif_out_ctls(codec, anid, cnid) \
188 	snd_hda_create_dig_out_ctls(codec, anid, cnid, HDA_PCM_TYPE_SPDIF)
189 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid);
190 
191 /*
192  * input MUX helper
193  */
194 #define HDA_MAX_NUM_INPUTS	16
195 struct hda_input_mux_item {
196 	char label[32];
197 	unsigned int index;
198 };
199 struct hda_input_mux {
200 	unsigned int num_items;
201 	struct hda_input_mux_item items[HDA_MAX_NUM_INPUTS];
202 };
203 
204 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
205 			   struct snd_ctl_elem_info *uinfo);
206 int snd_hda_input_mux_put(struct hda_codec *codec,
207 			  const struct hda_input_mux *imux,
208 			  struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
209 			  unsigned int *cur_val);
210 int snd_hda_add_imux_item(struct hda_codec *codec,
211 			  struct hda_input_mux *imux, const char *label,
212 			  int index, int *type_index_ret);
213 
214 /*
215  * Multi-channel / digital-out PCM helper
216  */
217 
218 enum { HDA_FRONT, HDA_REAR, HDA_CLFE, HDA_SIDE }; /* index for dac_nidx */
219 enum { HDA_DIG_NONE, HDA_DIG_EXCLUSIVE, HDA_DIG_ANALOG_DUP }; /* dig_out_used */
220 
221 #define HDA_MAX_OUTS	5
222 
223 struct hda_multi_out {
224 	int num_dacs;		/* # of DACs, must be more than 1 */
225 	const hda_nid_t *dac_nids;	/* DAC list */
226 	hda_nid_t hp_nid;	/* optional DAC for HP, 0 when not exists */
227 	hda_nid_t hp_out_nid[HDA_MAX_OUTS];	/* DACs for multiple HPs */
228 	hda_nid_t extra_out_nid[HDA_MAX_OUTS];	/* other (e.g. speaker) DACs */
229 	hda_nid_t dig_out_nid;	/* digital out audio widget */
230 	const hda_nid_t *slave_dig_outs;
231 	int max_channels;	/* currently supported analog channels */
232 	int dig_out_used;	/* current usage of digital out (HDA_DIG_XXX) */
233 	int no_share_stream;	/* don't share a stream with multiple pins */
234 	int share_spdif;	/* share SPDIF pin */
235 	/* PCM information for both analog and SPDIF DACs */
236 	unsigned int analog_rates;
237 	unsigned int analog_maxbps;
238 	u64 analog_formats;
239 	unsigned int spdif_rates;
240 	unsigned int spdif_maxbps;
241 	u64 spdif_formats;
242 };
243 
244 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
245 				  struct hda_multi_out *mout);
246 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
247 			       struct hda_multi_out *mout);
248 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
249 				struct hda_multi_out *mout);
250 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
251 				  struct hda_multi_out *mout,
252 				  unsigned int stream_tag,
253 				  unsigned int format,
254 				  struct snd_pcm_substream *substream);
255 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
256 				  struct hda_multi_out *mout);
257 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
258 				  struct hda_multi_out *mout,
259 				  struct snd_pcm_substream *substream,
260 				  struct hda_pcm_stream *hinfo);
261 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
262 				     struct hda_multi_out *mout,
263 				     unsigned int stream_tag,
264 				     unsigned int format,
265 				     struct snd_pcm_substream *substream);
266 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
267 				     struct hda_multi_out *mout);
268 
269 /*
270  * generic proc interface
271  */
272 #ifdef CONFIG_SND_PROC_FS
273 int snd_hda_codec_proc_new(struct hda_codec *codec);
274 #else
snd_hda_codec_proc_new(struct hda_codec * codec)275 static inline int snd_hda_codec_proc_new(struct hda_codec *codec) { return 0; }
276 #endif
277 
278 #define SND_PRINT_BITS_ADVISED_BUFSIZE	16
279 void snd_print_pcm_bits(int pcm, char *buf, int buflen);
280 
281 /*
282  * Misc
283  */
284 int snd_hda_add_new_ctls(struct hda_codec *codec,
285 			 const struct snd_kcontrol_new *knew);
286 
287 /*
288  * Fix-up pin default configurations and add default verbs
289  */
290 
291 struct hda_pintbl {
292 	hda_nid_t nid;
293 	u32 val;
294 };
295 
296 struct hda_model_fixup {
297 	const int id;
298 	const char *name;
299 };
300 
301 struct hda_fixup {
302 	int type;
303 	bool chained:1;		/* call the chained fixup(s) after this */
304 	bool chained_before:1;	/* call the chained fixup(s) before this */
305 	int chain_id;
306 	union {
307 		const struct hda_pintbl *pins;
308 		const struct hda_verb *verbs;
309 		void (*func)(struct hda_codec *codec,
310 			     const struct hda_fixup *fix,
311 			     int action);
312 	} v;
313 };
314 
315 struct snd_hda_pin_quirk {
316 	unsigned int codec;             /* Codec vendor/device ID */
317 	unsigned short subvendor;	/* PCI subvendor ID */
318 	const struct hda_pintbl *pins;  /* list of matching pins */
319 #ifdef CONFIG_SND_DEBUG_VERBOSE
320 	const char *name;
321 #endif
322 	int value;			/* quirk value */
323 };
324 
325 #ifdef CONFIG_SND_DEBUG_VERBOSE
326 
327 #define SND_HDA_PIN_QUIRK(_codec, _subvendor, _name, _value, _pins...) \
328 	{ .codec = _codec,\
329 	  .subvendor = _subvendor,\
330 	  .name = _name,\
331 	  .value = _value,\
332 	  .pins = (const struct hda_pintbl[]) { _pins, {0, 0}} \
333 	}
334 #else
335 
336 #define SND_HDA_PIN_QUIRK(_codec, _subvendor, _name, _value, _pins...) \
337 	{ .codec = _codec,\
338 	  .subvendor = _subvendor,\
339 	  .value = _value,\
340 	  .pins = (const struct hda_pintbl[]) { _pins, {0, 0}} \
341 	}
342 
343 #endif
344 
345 #define HDA_FIXUP_ID_NOT_SET -1
346 #define HDA_FIXUP_ID_NO_FIXUP -2
347 
348 /* fixup types */
349 enum {
350 	HDA_FIXUP_INVALID,
351 	HDA_FIXUP_PINS,
352 	HDA_FIXUP_VERBS,
353 	HDA_FIXUP_FUNC,
354 	HDA_FIXUP_PINCTLS,
355 };
356 
357 /* fixup action definitions */
358 enum {
359 	HDA_FIXUP_ACT_PRE_PROBE,
360 	HDA_FIXUP_ACT_PROBE,
361 	HDA_FIXUP_ACT_INIT,
362 	HDA_FIXUP_ACT_BUILD,
363 	HDA_FIXUP_ACT_FREE,
364 };
365 
366 int snd_hda_add_verbs(struct hda_codec *codec, const struct hda_verb *list);
367 void snd_hda_apply_verbs(struct hda_codec *codec);
368 void snd_hda_apply_pincfgs(struct hda_codec *codec,
369 			   const struct hda_pintbl *cfg);
370 void snd_hda_apply_fixup(struct hda_codec *codec, int action);
371 void snd_hda_pick_fixup(struct hda_codec *codec,
372 			const struct hda_model_fixup *models,
373 			const struct snd_pci_quirk *quirk,
374 			const struct hda_fixup *fixlist);
375 void snd_hda_pick_pin_fixup(struct hda_codec *codec,
376 			    const struct snd_hda_pin_quirk *pin_quirk,
377 			    const struct hda_fixup *fixlist);
378 
379 /* helper macros to retrieve pin default-config values */
380 #define get_defcfg_connect(cfg) \
381 	((cfg & AC_DEFCFG_PORT_CONN) >> AC_DEFCFG_PORT_CONN_SHIFT)
382 #define get_defcfg_association(cfg) \
383 	((cfg & AC_DEFCFG_DEF_ASSOC) >> AC_DEFCFG_ASSOC_SHIFT)
384 #define get_defcfg_location(cfg) \
385 	((cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT)
386 #define get_defcfg_sequence(cfg) \
387 	(cfg & AC_DEFCFG_SEQUENCE)
388 #define get_defcfg_device(cfg) \
389 	((cfg & AC_DEFCFG_DEVICE) >> AC_DEFCFG_DEVICE_SHIFT)
390 #define get_defcfg_misc(cfg) \
391 	((cfg & AC_DEFCFG_MISC) >> AC_DEFCFG_MISC_SHIFT)
392 
393 /* amp values */
394 #define AMP_IN_MUTE(idx)	(0x7080 | ((idx)<<8))
395 #define AMP_IN_UNMUTE(idx)	(0x7000 | ((idx)<<8))
396 #define AMP_OUT_MUTE		0xb080
397 #define AMP_OUT_UNMUTE		0xb000
398 #define AMP_OUT_ZERO		0xb000
399 /* pinctl values */
400 #define PIN_IN			(AC_PINCTL_IN_EN)
401 #define PIN_VREFHIZ		(AC_PINCTL_IN_EN | AC_PINCTL_VREF_HIZ)
402 #define PIN_VREF50		(AC_PINCTL_IN_EN | AC_PINCTL_VREF_50)
403 #define PIN_VREFGRD		(AC_PINCTL_IN_EN | AC_PINCTL_VREF_GRD)
404 #define PIN_VREF80		(AC_PINCTL_IN_EN | AC_PINCTL_VREF_80)
405 #define PIN_VREF100		(AC_PINCTL_IN_EN | AC_PINCTL_VREF_100)
406 #define PIN_OUT			(AC_PINCTL_OUT_EN)
407 #define PIN_HP			(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN)
408 #define PIN_HP_AMP		(AC_PINCTL_HP_EN)
409 
410 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin);
411 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
412 				     hda_nid_t pin, unsigned int val);
413 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
414 			 unsigned int val, bool cached);
415 
416 /**
417  * _snd_hda_set_pin_ctl - Set a pin-control value safely
418  * @codec: the codec instance
419  * @pin: the pin NID to set the control
420  * @val: the pin-control value (AC_PINCTL_* bits)
421  *
422  * This function sets the pin-control value to the given pin, but
423  * filters out the invalid pin-control bits when the pin has no such
424  * capabilities.  For example, when PIN_HP is passed but the pin has no
425  * HP-drive capability, the HP bit is omitted.
426  *
427  * The function doesn't check the input VREF capability bits, though.
428  * Use snd_hda_get_default_vref() to guess the right value.
429  * Also, this function is only for analog pins, not for HDMI pins.
430  */
431 static inline int
snd_hda_set_pin_ctl(struct hda_codec * codec,hda_nid_t pin,unsigned int val)432 snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, unsigned int val)
433 {
434 	return _snd_hda_set_pin_ctl(codec, pin, val, false);
435 }
436 
437 /**
438  * snd_hda_set_pin_ctl_cache - Set a pin-control value safely
439  * @codec: the codec instance
440  * @pin: the pin NID to set the control
441  * @val: the pin-control value (AC_PINCTL_* bits)
442  *
443  * Just like snd_hda_set_pin_ctl() but write to cache as well.
444  */
445 static inline int
snd_hda_set_pin_ctl_cache(struct hda_codec * codec,hda_nid_t pin,unsigned int val)446 snd_hda_set_pin_ctl_cache(struct hda_codec *codec, hda_nid_t pin,
447 			  unsigned int val)
448 {
449 	return _snd_hda_set_pin_ctl(codec, pin, val, true);
450 }
451 
452 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid);
453 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
454 				 unsigned int val);
455 
456 #define for_each_hda_codec_node(nid, codec) \
457 	for ((nid) = (codec)->core.start_nid; (nid) < (codec)->core.end_nid; (nid)++)
458 
459 /*
460  * get widget capabilities
461  */
get_wcaps(struct hda_codec * codec,hda_nid_t nid)462 static inline u32 get_wcaps(struct hda_codec *codec, hda_nid_t nid)
463 {
464 	if (nid < codec->core.start_nid ||
465 	    nid >= codec->core.start_nid + codec->core.num_nodes)
466 		return 0;
467 	return codec->wcaps[nid - codec->core.start_nid];
468 }
469 
470 /* get the widget type from widget capability bits */
get_wcaps_type(unsigned int wcaps)471 static inline int get_wcaps_type(unsigned int wcaps)
472 {
473 	if (!wcaps)
474 		return -1; /* invalid type */
475 	return (wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
476 }
477 
get_wcaps_channels(u32 wcaps)478 static inline unsigned int get_wcaps_channels(u32 wcaps)
479 {
480 	unsigned int chans;
481 
482 	chans = (wcaps & AC_WCAP_CHAN_CNT_EXT) >> 13;
483 	chans = ((chans << 1) | 1) + 1;
484 
485 	return chans;
486 }
487 
snd_hda_override_wcaps(struct hda_codec * codec,hda_nid_t nid,u32 val)488 static inline void snd_hda_override_wcaps(struct hda_codec *codec,
489 					  hda_nid_t nid, u32 val)
490 {
491 	if (nid >= codec->core.start_nid &&
492 	    nid < codec->core.start_nid + codec->core.num_nodes)
493 		codec->wcaps[nid - codec->core.start_nid] = val;
494 }
495 
496 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction);
497 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
498 			      unsigned int caps);
499 /**
500  * snd_hda_query_pin_caps - Query PIN capabilities
501  * @codec: the HD-auio codec
502  * @nid: the NID to query
503  *
504  * Query PIN capabilities for the given widget.
505  * Returns the obtained capability bits.
506  *
507  * When cap bits have been already read, this doesn't read again but
508  * returns the cached value.
509  */
510 static inline u32
snd_hda_query_pin_caps(struct hda_codec * codec,hda_nid_t nid)511 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
512 {
513 	return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
514 
515 }
516 
517 /**
518  * snd_hda_override_pin_caps - Override the pin capabilities
519  * @codec: the CODEC
520  * @nid: the NID to override
521  * @caps: the capability bits to set
522  *
523  * Override the cached PIN capabilitiy bits value by the given one.
524  *
525  * Returns zero if successful or a negative error code.
526  */
527 static inline int
snd_hda_override_pin_caps(struct hda_codec * codec,hda_nid_t nid,unsigned int caps)528 snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
529 			  unsigned int caps)
530 {
531 	return snd_hdac_override_parm(&codec->core, nid, AC_PAR_PIN_CAP, caps);
532 }
533 
534 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
535 			   int dir, unsigned int bits);
536 
537 #define nid_has_mute(codec, nid, dir) \
538 	snd_hda_check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
539 #define nid_has_volume(codec, nid, dir) \
540 	snd_hda_check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
541 
542 
543 /* flags for hda_nid_item */
544 #define HDA_NID_ITEM_AMP	(1<<0)
545 
546 struct hda_nid_item {
547 	struct snd_kcontrol *kctl;
548 	unsigned int index;
549 	hda_nid_t nid;
550 	unsigned short flags;
551 };
552 
553 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
554 		    struct snd_kcontrol *kctl);
555 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
556 		    unsigned int index, hda_nid_t nid);
557 void snd_hda_ctls_clear(struct hda_codec *codec);
558 
559 /*
560  * hwdep interface
561  */
562 #ifdef CONFIG_SND_HDA_HWDEP
563 int snd_hda_create_hwdep(struct hda_codec *codec);
564 #else
snd_hda_create_hwdep(struct hda_codec * codec)565 static inline int snd_hda_create_hwdep(struct hda_codec *codec) { return 0; }
566 #endif
567 
568 void snd_hda_sysfs_init(struct hda_codec *codec);
569 void snd_hda_sysfs_clear(struct hda_codec *codec);
570 
571 extern const struct attribute_group *snd_hda_dev_attr_groups[];
572 
573 #ifdef CONFIG_SND_HDA_RECONFIG
574 const char *snd_hda_get_hint(struct hda_codec *codec, const char *key);
575 int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key);
576 int snd_hda_get_int_hint(struct hda_codec *codec, const char *key, int *valp);
577 #else
578 static inline
snd_hda_get_hint(struct hda_codec * codec,const char * key)579 const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
580 {
581 	return NULL;
582 }
583 
584 static inline
snd_hda_get_bool_hint(struct hda_codec * codec,const char * key)585 int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
586 {
587 	return -ENOENT;
588 }
589 
590 static inline
snd_hda_get_int_hint(struct hda_codec * codec,const char * key,int * valp)591 int snd_hda_get_int_hint(struct hda_codec *codec, const char *key, int *valp)
592 {
593 	return -ENOENT;
594 }
595 #endif
596 
597 /*
598  * power-management
599  */
600 
601 void snd_hda_schedule_power_save(struct hda_codec *codec);
602 
603 struct hda_amp_list {
604 	hda_nid_t nid;
605 	unsigned char dir;
606 	unsigned char idx;
607 };
608 
609 struct hda_loopback_check {
610 	const struct hda_amp_list *amplist;
611 	int power_on;
612 };
613 
614 int snd_hda_check_amp_list_power(struct hda_codec *codec,
615 				 struct hda_loopback_check *check,
616 				 hda_nid_t nid);
617 
618 /* check whether the actual power state matches with the target state */
619 static inline bool
snd_hda_check_power_state(struct hda_codec * codec,hda_nid_t nid,unsigned int target_state)620 snd_hda_check_power_state(struct hda_codec *codec, hda_nid_t nid,
621 			  unsigned int target_state)
622 {
623 	return snd_hdac_check_power_state(&codec->core, nid, target_state);
624 }
625 
snd_hda_sync_power_state(struct hda_codec * codec,hda_nid_t nid,unsigned int target_state)626 static inline unsigned int snd_hda_sync_power_state(struct hda_codec *codec,
627 						    hda_nid_t nid,
628 						    unsigned int target_state)
629 {
630 	return snd_hdac_sync_power_state(&codec->core, nid, target_state);
631 }
632 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
633 					     hda_nid_t nid,
634 					     unsigned int power_state);
635 
636 /*
637  * AMP control callbacks
638  */
639 /* retrieve parameters from private_value */
640 #define get_amp_nid_(pv)	((pv) & 0xffff)
641 #define get_amp_nid(kc)		get_amp_nid_((kc)->private_value)
642 #define get_amp_channels(kc)	(((kc)->private_value >> 16) & 0x3)
643 #define get_amp_direction_(pv)	(((pv) >> 18) & 0x1)
644 #define get_amp_direction(kc)	get_amp_direction_((kc)->private_value)
645 #define get_amp_index_(pv)	(((pv) >> 19) & 0xf)
646 #define get_amp_index(kc)	get_amp_index_((kc)->private_value)
647 #define get_amp_offset(kc)	(((kc)->private_value >> 23) & 0x3f)
648 #define get_amp_min_mute(kc)	(((kc)->private_value >> 29) & 0x1)
649 
650 /*
651  * enum control helper
652  */
653 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
654 			     struct snd_ctl_elem_info *uinfo,
655 			     int num_entries, const char * const *texts);
656 #define snd_hda_enum_bool_helper_info(kcontrol, uinfo) \
657 	snd_hda_enum_helper_info(kcontrol, uinfo, 0, NULL)
658 
659 /*
660  * CEA Short Audio Descriptor data
661  */
662 struct cea_sad {
663 	int	channels;
664 	int	format;		/* (format == 0) indicates invalid SAD */
665 	int	rates;
666 	int	sample_bits;	/* for LPCM */
667 	int	max_bitrate;	/* for AC3...ATRAC */
668 	int	profile;	/* for WMAPRO */
669 };
670 
671 #define ELD_FIXED_BYTES	20
672 #define ELD_MAX_SIZE    256
673 #define ELD_MAX_MNL	16
674 #define ELD_MAX_SAD	16
675 
676 /*
677  * ELD: EDID Like Data
678  */
679 struct parsed_hdmi_eld {
680 	/*
681 	 * all fields will be cleared before updating ELD
682 	 */
683 	int	baseline_len;
684 	int	eld_ver;
685 	int	cea_edid_ver;
686 	char	monitor_name[ELD_MAX_MNL + 1];
687 	int	manufacture_id;
688 	int	product_id;
689 	u64	port_id;
690 	int	support_hdcp;
691 	int	support_ai;
692 	int	conn_type;
693 	int	aud_synch_delay;
694 	int	spk_alloc;
695 	int	sad_count;
696 	struct cea_sad sad[ELD_MAX_SAD];
697 };
698 
699 struct hdmi_eld {
700 	bool	monitor_present;
701 	bool	eld_valid;
702 	int	eld_size;
703 	char    eld_buffer[ELD_MAX_SIZE];
704 	struct parsed_hdmi_eld info;
705 };
706 
707 int snd_hdmi_get_eld_size(struct hda_codec *codec, hda_nid_t nid);
708 int snd_hdmi_get_eld(struct hda_codec *codec, hda_nid_t nid,
709 		     unsigned char *buf, int *eld_size);
710 int snd_hdmi_parse_eld(struct hda_codec *codec, struct parsed_hdmi_eld *e,
711 		       const unsigned char *buf, int size);
712 void snd_hdmi_show_eld(struct hda_codec *codec, struct parsed_hdmi_eld *e);
713 void snd_hdmi_eld_update_pcm_info(struct parsed_hdmi_eld *e,
714 			      struct hda_pcm_stream *hinfo);
715 
716 int snd_hdmi_get_eld_ati(struct hda_codec *codec, hda_nid_t nid,
717 			 unsigned char *buf, int *eld_size,
718 			 bool rev3_or_later);
719 
720 #ifdef CONFIG_SND_PROC_FS
721 void snd_hdmi_print_eld_info(struct hdmi_eld *eld,
722 			     struct snd_info_buffer *buffer);
723 void snd_hdmi_write_eld_info(struct hdmi_eld *eld,
724 			     struct snd_info_buffer *buffer);
725 #endif
726 
727 #define SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE 80
728 void snd_print_channel_allocation(int spk_alloc, char *buf, int buflen);
729 
730 /*
731  */
732 #define codec_err(codec, fmt, args...) \
733 	dev_err(hda_codec_dev(codec), fmt, ##args)
734 #define codec_warn(codec, fmt, args...) \
735 	dev_warn(hda_codec_dev(codec), fmt, ##args)
736 #define codec_info(codec, fmt, args...) \
737 	dev_info(hda_codec_dev(codec), fmt, ##args)
738 #define codec_dbg(codec, fmt, args...) \
739 	dev_dbg(hda_codec_dev(codec), fmt, ##args)
740 
741 #endif /* __SOUND_HDA_LOCAL_H */
742