xref: /linux/sound/hda/codecs/hdmi/hdmi_local.h (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HD-audio HDMI codec driver
4  */
5 
6 #ifndef __HDA_HDMI_LOCAL_H
7 #define __HDA_HDMI_LOCAL_H
8 
9 #include <sound/core.h>
10 #include <sound/jack.h>
11 #include <sound/hdaudio.h>
12 #include <sound/hda_i915.h>
13 #include <sound/hda_chmap.h>
14 #include <sound/hda_codec.h>
15 #include "hda_local.h"
16 
17 struct hdmi_spec_per_cvt {
18 	hda_nid_t cvt_nid;
19 	bool assigned;		/* the stream has been assigned */
20 	bool silent_stream;	/* silent stream activated */
21 	unsigned int channels_min;
22 	unsigned int channels_max;
23 	u32 rates;
24 	u64 formats;
25 	unsigned int maxbps;
26 };
27 
28 /* max. connections to a widget */
29 #define HDA_MAX_CONNECTIONS	32
30 
31 struct hdmi_spec_per_pin {
32 	hda_nid_t pin_nid;
33 	int dev_id;
34 	/* pin idx, different device entries on the same pin use the same idx */
35 	int pin_nid_idx;
36 	int num_mux_nids;
37 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
38 	int mux_idx;
39 	hda_nid_t cvt_nid;
40 
41 	struct hda_codec *codec;
42 	struct hdmi_eld sink_eld;
43 	struct mutex lock;
44 	struct delayed_work work;
45 	struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
46 	int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
47 	int prev_pcm_idx; /* previously assigned pcm index */
48 	int repoll_count;
49 	bool setup; /* the stream has been set up by prepare callback */
50 	bool silent_stream;
51 	int channels; /* current number of channels */
52 	bool non_pcm;
53 	bool chmap_set;		/* channel-map override by ALSA API? */
54 	unsigned char chmap[8]; /* ALSA API channel-map */
55 #ifdef CONFIG_SND_PROC_FS
56 	struct snd_info_entry *proc_entry;
57 #endif
58 };
59 
60 /* operations used by generic code that can be overridden by codec drivers */
61 struct hdmi_ops {
62 	int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
63 			   int dev_id, unsigned char *buf, int *eld_size);
64 
65 	void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
66 				    int dev_id,
67 				    int ca, int active_channels, int conn_type);
68 
69 	/* enable/disable HBR (HD passthrough) */
70 	int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid,
71 			     int dev_id, bool hbr);
72 
73 	int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
74 			    hda_nid_t pin_nid, int dev_id, u32 stream_tag,
75 			    int format);
76 
77 	/*
78 	 * Optional hook invoked at the beginning of the PCM prepare
79 	 * sequence, before the audio infoframe and stream format are
80 	 * (re)programmed. Used to disable keep-alive / silent stream so
81 	 * that the format change is not done while keep-alive is active.
82 	 */
83 	void (*prepare)(struct hda_codec *codec,
84 			struct hdmi_spec_per_pin *per_pin);
85 
86 	void (*pin_cvt_fixup)(struct hda_codec *codec,
87 			      struct hdmi_spec_per_pin *per_pin,
88 			      hda_nid_t cvt_nid);
89 
90 	void (*silent_stream)(struct hda_codec *codec,
91 			      struct hdmi_spec_per_pin *per_pin,
92 			      bool enable);
93 };
94 
95 struct hdmi_pcm {
96 	struct hda_pcm *pcm;
97 	struct snd_jack *jack;
98 	struct snd_kcontrol *eld_ctl;
99 };
100 
101 enum {
102 	SILENT_STREAM_OFF = 0,
103 	SILENT_STREAM_KAE,	/* use standard HDA Keep-Alive */
104 	SILENT_STREAM_I915,	/* Intel i915 extension */
105 };
106 
107 struct hdmi_spec {
108 	struct hda_codec *codec;
109 	int num_cvts;
110 	struct snd_array cvts; /* struct hdmi_spec_per_cvt */
111 	hda_nid_t cvt_nids[4]; /* only for haswell fix */
112 
113 	/*
114 	 * num_pins is the number of virtual pins
115 	 * for example, there are 3 pins, and each pin
116 	 * has 4 device entries, then the num_pins is 12
117 	 */
118 	int num_pins;
119 	/*
120 	 * num_nids is the number of real pins
121 	 * In the above example, num_nids is 3
122 	 */
123 	int num_nids;
124 	/*
125 	 * dev_num is the number of device entries
126 	 * on each pin.
127 	 * In the above example, dev_num is 4
128 	 */
129 	int dev_num;
130 	struct snd_array pins; /* struct hdmi_spec_per_pin */
131 	struct hdmi_pcm pcm_rec[8];
132 	struct mutex pcm_lock;
133 	struct mutex bind_lock; /* for audio component binding */
134 	/* pcm_bitmap means which pcms have been assigned to pins*/
135 	unsigned long pcm_bitmap;
136 	int pcm_used;	/* counter of pcm_rec[] */
137 	/* bitmap shows whether the pcm is opened in user space
138 	 * bit 0 means the first playback PCM (PCM3);
139 	 * bit 1 means the second playback PCM, and so on.
140 	 */
141 	unsigned long pcm_in_use;
142 
143 	struct hdmi_eld temp_eld;
144 	struct hdmi_ops ops;
145 
146 	bool dyn_pin_out;
147 	bool static_pcm_mapping;
148 	/* hdmi interrupt trigger control flag for Nvidia codec */
149 	bool hdmi_intr_trig_ctrl;
150 	bool nv_dp_workaround; /* workaround DP audio infoframe for Nvidia */
151 
152 	bool intel_hsw_fixup;	/* apply Intel platform-specific fixups */
153 	/*
154 	 * Non-generic VIA/NVIDIA specific
155 	 */
156 	struct hda_multi_out multiout;
157 	struct hda_pcm_stream pcm_playback;
158 
159 	bool use_acomp_notifier; /* use eld_notify callback for hotplug */
160 	bool acomp_registered; /* audio component registered in this driver */
161 	bool force_connect; /* force connectivity */
162 	struct drm_audio_component_audio_ops drm_audio_ops;
163 	int (*port2pin)(struct hda_codec *codec, int port); /* reverse port/pin mapping */
164 
165 	struct hdac_chmap chmap;
166 	hda_nid_t vendor_nid;
167 	const int *port_map;
168 	int port_num;
169 	int silent_stream_type;
170 
171 	const struct snd_pcm_hw_constraint_list *hw_constraints_channels;
172 };
173 
174 #ifdef CONFIG_SND_HDA_COMPONENT
codec_has_acomp(struct hda_codec * codec)175 static inline bool codec_has_acomp(struct hda_codec *codec)
176 {
177 	struct hdmi_spec *spec = codec->spec;
178 
179 	return spec->use_acomp_notifier;
180 }
181 #else
182 #define codec_has_acomp(codec)	false
183 #endif
184 
185 struct hdmi_audio_infoframe {
186 	u8 type; /* 0x84 */
187 	u8 ver;  /* 0x01 */
188 	u8 len;  /* 0x0a */
189 
190 	u8 checksum;
191 
192 	u8 CC02_CT47;	/* CC in bits 0:2, CT in 4:7 */
193 	u8 SS01_SF24;
194 	u8 CXT04;
195 	u8 CA;
196 	u8 LFEPBL01_LSV36_DM_INH7;
197 };
198 
199 struct dp_audio_infoframe {
200 	u8 type; /* 0x84 */
201 	u8 len;  /* 0x1b */
202 	u8 ver;  /* 0x11 << 2 */
203 
204 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
205 	u8 SS01_SF24;
206 	u8 CXT04;
207 	u8 CA;
208 	u8 LFEPBL01_LSV36_DM_INH7;
209 };
210 
211 union audio_infoframe {
212 	struct hdmi_audio_infoframe hdmi;
213 	struct dp_audio_infoframe dp;
214 	DECLARE_FLEX_ARRAY(u8, bytes);
215 };
216 
217 #ifdef LIMITED_RATE_FMT_SUPPORT
218 /* support only the safe format and rate */
219 #define SUPPORTED_RATES		SNDRV_PCM_RATE_48000
220 #define SUPPORTED_MAXBPS	16
221 #define SUPPORTED_FORMATS	SNDRV_PCM_FMTBIT_S16_LE
222 #else
223 /* support all rates and formats */
224 #define SUPPORTED_RATES \
225 	(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
226 	SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
227 	 SNDRV_PCM_RATE_192000)
228 #define SUPPORTED_MAXBPS	24
229 #define SUPPORTED_FORMATS \
230 	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
231 #endif
232 
233 /*
234  * HDMI routines
235  */
236 
237 #define get_pin(spec, idx) \
238 	((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
239 #define get_cvt(spec, idx) \
240 	((struct hdmi_spec_per_cvt  *)snd_array_elem(&spec->cvts, idx))
241 /* obtain hdmi_pcm object assigned to idx */
242 #define get_hdmi_pcm(spec, idx)	(&(spec)->pcm_rec[idx])
243 /* obtain hda_pcm object assigned to idx */
244 #define get_pcm_rec(spec, idx)	(get_hdmi_pcm(spec, idx)->pcm)
245 
246 /* Generic HDMI codec support */
247 int snd_hda_hdmi_generic_alloc(struct hda_codec *codec);
248 int snd_hda_hdmi_parse_codec(struct hda_codec *codec);
249 int snd_hda_hdmi_generic_probe(struct hda_codec *codec);
250 void snd_hda_hdmi_generic_remove(struct hda_codec *codec);
251 
252 int snd_hda_hdmi_generic_build_pcms(struct hda_codec *codec);
253 int snd_hda_hdmi_generic_build_controls(struct hda_codec *codec);
254 int snd_hda_hdmi_generic_init(struct hda_codec *codec);
255 int snd_hda_hdmi_generic_suspend(struct hda_codec *codec);
256 int snd_hda_hdmi_generic_resume(struct hda_codec *codec);
257 void snd_hda_hdmi_generic_unsol_event(struct hda_codec *codec, unsigned int res);
258 
259 int snd_hda_hdmi_pin_id_to_pin_index(struct hda_codec *codec,
260 				     hda_nid_t pin_nid, int dev_id);
261 #define pin_id_to_pin_index(codec, pin, dev) \
262 	snd_hda_hdmi_pin_id_to_pin_index(codec, pin, dev)
263 int snd_hda_hdmi_generic_init_per_pins(struct hda_codec *codec);
264 void snd_hda_hdmi_generic_spec_free(struct hda_codec *codec);
265 int snd_hda_hdmi_setup_stream(struct hda_codec *codec,
266 			      hda_nid_t cvt_nid,
267 			      hda_nid_t pin_nid, int dev_id,
268 			      u32 stream_tag, int format);
269 
270 int snd_hda_hdmi_generic_pcm_prepare(struct hda_pcm_stream *hinfo,
271 				     struct hda_codec *codec,
272 				     unsigned int stream_tag,
273 				     unsigned int format,
274 				     struct snd_pcm_substream *substream);
275 int snd_hda_hdmi_generic_pcm_cleanup(struct hda_pcm_stream *hinfo,
276 				     struct hda_codec *codec,
277 				     struct snd_pcm_substream *substream);
278 
279 void snd_hda_hdmi_check_presence_and_report(struct hda_codec *codec,
280 					    hda_nid_t nid, int dev_id);
281 void snd_hda_hdmi_setup_audio_infoframe(struct hda_codec *codec,
282 					struct hdmi_spec_per_pin *per_pin,
283 					bool non_pcm);
284 
285 /* Audio component support */
286 void snd_hda_hdmi_setup_drm_audio_ops(struct hda_codec *codec,
287 				      const struct drm_audio_component_audio_ops *ops);
288 void snd_hda_hdmi_acomp_init(struct hda_codec *codec,
289 			     const struct drm_audio_component_audio_ops *ops,
290 			     int (*port2pin)(struct hda_codec *, int));
291 void snd_hda_hdmi_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id);
292 int snd_hda_hdmi_acomp_master_bind(struct device *dev,
293 				   struct drm_audio_component *acomp);
294 void snd_hda_hdmi_acomp_master_unbind(struct device *dev,
295 				      struct drm_audio_component *acomp);
296 
297 /* Simple / legacy HDMI codec support */
298 int snd_hda_hdmi_simple_probe(struct hda_codec *codec,
299 			      hda_nid_t cvt_nid, hda_nid_t pin_nid);
300 void snd_hda_hdmi_simple_remove(struct hda_codec *codec);
301 
302 int snd_hda_hdmi_simple_build_pcms(struct hda_codec *codec);
303 int snd_hda_hdmi_simple_build_controls(struct hda_codec *codec);
304 int snd_hda_hdmi_simple_init(struct hda_codec *codec);
305 void snd_hda_hdmi_simple_unsol_event(struct hda_codec *codec,
306 				     unsigned int res);
307 int snd_hda_hdmi_simple_pcm_open(struct hda_pcm_stream *hinfo,
308 				 struct hda_codec *codec,
309 				 struct snd_pcm_substream *substream);
310 
311 #endif /* __HDA_HDMI_LOCAL_H */
312