1331722Seadler/*-7
2230130Smav * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
3230130Smav * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
4230130Smav * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
5230130Smav * All rights reserved.
6230130Smav *
7230130Smav * Redistribution and use in source and binary forms, with or without
8230130Smav * modification, are permitted provided that the following conditions
9230130Smav * are met:
10230130Smav * 1. Redistributions of source code must retain the above copyright
11230130Smav *    notice, this list of conditions and the following disclaimer.
12230130Smav * 2. Redistributions in binary form must reproduce the above copyright
13230130Smav *    notice, this list of conditions and the following disclaimer in the
14230130Smav *    documentation and/or other materials provided with the distribution.
15230130Smav *
16230130Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17230130Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18230130Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19230130Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20230130Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21230130Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22230130Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23230130Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24230130Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25230130Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26230130Smav * SUCH DAMAGE.
27230130Smav *
28230130Smav * $FreeBSD$
29230130Smav */
30230130Smav
31230130Smav/*
32230130Smav * Intel High Definition Audio (Audio function quirks) driver for FreeBSD.
33230130Smav */
34230130Smav
35230130Smav#ifndef _HDAA_QUIRKS_H_
36230130Smav#define _HDAA_QUIRKS_H_
37230130Smav
38230130Smav#define HDAA_GPIO_SHIFT(n)	(n * 3)
39230130Smav#define HDAA_GPIO_MASK(n)	(0x7 << (n * 3))
40230130Smav#define HDAA_GPIO_KEEP(n)	(0x0 << (n * 3))
41230130Smav#define HDAA_GPIO_SET(n)	(0x1 << (n * 3))
42230130Smav#define HDAA_GPIO_CLEAR(n)	(0x2 << (n * 3))
43230130Smav#define HDAA_GPIO_DISABLE(n)	(0x3 << (n * 3))
44230130Smav#define HDAA_GPIO_INPUT(n)	(0x4 << (n * 3))
45230130Smav
46230130Smav/* 9 - 25 = anything else */
47230130Smav#define HDAA_QUIRK_SOFTPCMVOL	(1 << 9)
48230130Smav#define HDAA_QUIRK_FIXEDRATE	(1 << 10)
49230130Smav#define HDAA_QUIRK_FORCESTEREO	(1 << 11)
50230130Smav#define HDAA_QUIRK_EAPDINV	(1 << 12)
51230130Smav#define HDAA_QUIRK_SENSEINV	(1 << 14)
52230130Smav
53230130Smav/* 26 - 31 = vrefs */
54230130Smav#define HDAA_QUIRK_IVREF50	(1 << 26)
55230130Smav#define HDAA_QUIRK_IVREF80	(1 << 27)
56230130Smav#define HDAA_QUIRK_IVREF100	(1 << 28)
57230130Smav#define HDAA_QUIRK_OVREF50	(1 << 29)
58230130Smav#define HDAA_QUIRK_OVREF80	(1 << 30)
59258780Seadler#define HDAA_QUIRK_OVREF100	(1U << 31)
60230130Smav
61230130Smav#define HDAA_QUIRK_IVREF	(HDAA_QUIRK_IVREF50 | HDAA_QUIRK_IVREF80 | \
62230130Smav						HDAA_QUIRK_IVREF100)
63230130Smav#define HDAA_QUIRK_OVREF	(HDAA_QUIRK_OVREF50 | HDAA_QUIRK_OVREF80 | \
64230130Smav						HDAA_QUIRK_OVREF100)
65230130Smav#define HDAA_QUIRK_VREF		(HDAA_QUIRK_IVREF | HDAA_QUIRK_OVREF)
66230130Smav
67230130Smav#define HDAA_AMP_VOL_DEFAULT	(-1)
68230130Smav#define HDAA_AMP_MUTE_DEFAULT	(0xffffffff)
69230130Smav#define HDAA_AMP_MUTE_NONE	(0)
70230130Smav#define HDAA_AMP_MUTE_LEFT	(1 << 0)
71230130Smav#define HDAA_AMP_MUTE_RIGHT	(1 << 1)
72230130Smav#define HDAA_AMP_MUTE_ALL	(HDAA_AMP_MUTE_LEFT | HDAA_AMP_MUTE_RIGHT)
73230130Smav
74230130Smav#define HDAA_AMP_LEFT_MUTED(v)	((v) & (HDAA_AMP_MUTE_LEFT))
75230130Smav#define HDAA_AMP_RIGHT_MUTED(v)	(((v) & HDAA_AMP_MUTE_RIGHT) >> 1)
76230130Smav
77230451Smav/* Widget in playback receiving signal from recording. */
78230130Smav#define HDAA_ADC_MONITOR		(1 << 0)
79230451Smav/* Input mixer widget needs volume control as destination. */
80230451Smav#define HDAA_IMIX_AS_DST		(2 << 0)
81230130Smav
82230130Smav#define HDAA_CTL_OUT		1
83230130Smav#define HDAA_CTL_IN		2
84230130Smav
85230130Smav#define HDA_MAX_CONNS	32
86230130Smav#define HDA_MAX_NAMELEN	32
87230130Smav
88230451Smavstruct hdaa_audio_as;
89230451Smavstruct hdaa_audio_ctl;
90230451Smavstruct hdaa_chan;
91230451Smavstruct hdaa_devinfo;
92230451Smavstruct hdaa_pcm_devinfo;
93230451Smavstruct hdaa_widget;
94230451Smav
95230130Smavstruct hdaa_widget {
96230130Smav	nid_t nid;
97230130Smav	int type;
98230130Smav	int enable;
99230130Smav	int nconns, selconn;
100230130Smav	int waspin;
101230130Smav	uint32_t pflags;
102230130Smav	int bindas;
103230130Smav	int bindseqmask;
104230130Smav	int ossdev;
105230130Smav	uint32_t ossmask;
106230312Smav	int unsol;
107230130Smav	nid_t conns[HDA_MAX_CONNS];
108230130Smav	u_char connsenable[HDA_MAX_CONNS];
109230130Smav	char name[HDA_MAX_NAMELEN];
110230312Smav	uint8_t	*eld;
111230312Smav	int	eld_len;
112230130Smav	struct hdaa_devinfo *devinfo;
113230130Smav	struct {
114230130Smav		uint32_t widget_cap;
115230130Smav		uint32_t outamp_cap;
116230130Smav		uint32_t inamp_cap;
117230130Smav		uint32_t supp_stream_formats;
118230130Smav		uint32_t supp_pcm_size_rate;
119230130Smav		uint32_t eapdbtl;
120230130Smav	} param;
121230130Smav	union {
122230130Smav		struct {
123230130Smav			uint32_t config;
124230130Smav			uint32_t original;
125230130Smav			uint32_t newconf;
126230130Smav			uint32_t cap;
127230130Smav			uint32_t ctrl;
128230551Smav			int	connected;
129230130Smav		} pin;
130230326Smav		struct {
131230326Smav			uint8_t	stripecap;
132230326Smav		} conv;
133230130Smav	} wclass;
134230130Smav};
135230130Smav
136230130Smavstruct hdaa_audio_ctl {
137230130Smav	struct hdaa_widget *widget, *childwidget;
138230130Smav	int enable;
139230130Smav	int index, dir, ndir;
140230130Smav	int mute, step, size, offset;
141230130Smav	int left, right, forcemute;
142230130Smav	uint32_t muted;
143230451Smav	uint32_t ossmask;	/* OSS devices that may affect control. */
144230451Smav	int	devleft[SOUND_MIXER_NRDEVICES]; /* Left ampl in 1/4dB. */
145230451Smav	int	devright[SOUND_MIXER_NRDEVICES]; /* Right ampl in 1/4dB. */
146230451Smav	int	devmute[SOUND_MIXER_NRDEVICES]; /* Mutes per OSS device. */
147230130Smav};
148230130Smav
149230130Smav/* Association is a group of pins bound for some special function. */
150230130Smavstruct hdaa_audio_as {
151230130Smav	u_char enable;
152230130Smav	u_char index;
153230130Smav	u_char dir;
154230130Smav	u_char pincnt;
155230130Smav	u_char fakeredir;
156230130Smav	u_char digital;
157230130Smav	uint16_t pinset;
158230130Smav	nid_t hpredir;
159230130Smav	nid_t pins[16];
160230130Smav	nid_t dacs[2][16];
161230130Smav	int num_chans;
162230130Smav	int chans[2];
163230130Smav	int location;	/* Pins location, if all have the same */
164230130Smav	int mixed;	/* Mixed/multiplexed recording, not multichannel. */
165230451Smav	struct hdaa_pcm_devinfo *pdevinfo;
166230130Smav};
167230130Smav
168230130Smavstruct hdaa_pcm_devinfo {
169230130Smav	device_t dev;
170230130Smav	struct hdaa_devinfo *devinfo;
171230451Smav	struct	snd_mixer *mixer;
172230130Smav	int	index;
173230130Smav	int	registered;
174230130Smav	int	playas, recas;
175230130Smav	u_char	left[SOUND_MIXER_NRDEVICES];
176230130Smav	u_char	right[SOUND_MIXER_NRDEVICES];
177230451Smav	int	minamp[SOUND_MIXER_NRDEVICES]; /* Minimal amps in 1/4dB. */
178230451Smav	int	maxamp[SOUND_MIXER_NRDEVICES]; /* Maximal amps in 1/4dB. */
179230130Smav	int	chan_size;
180230130Smav	int	chan_blkcnt;
181230130Smav	u_char	digital;
182230451Smav	uint32_t	ossmask;	/* Mask of supported OSS devices. */
183230451Smav	uint32_t	recsrc;		/* Mask of supported OSS sources. */
184230551Smav	int		autorecsrc;
185230130Smav};
186230130Smav
187230130Smavstruct hdaa_devinfo {
188230130Smav	device_t		dev;
189230130Smav	struct mtx		*lock;
190230130Smav	nid_t			nid;
191230130Smav	nid_t			startnode, endnode;
192230130Smav	uint32_t		outamp_cap;
193230130Smav	uint32_t		inamp_cap;
194230130Smav	uint32_t		supp_stream_formats;
195230130Smav	uint32_t		supp_pcm_size_rate;
196230130Smav	uint32_t		gpio_cap;
197230130Smav	uint32_t		quirks;
198230130Smav	uint32_t		newquirks;
199230130Smav	uint32_t		gpio;
200230130Smav	uint32_t		newgpio;
201230130Smav	uint32_t		gpo;
202230130Smav	uint32_t		newgpo;
203230130Smav	int			nodecnt;
204230130Smav	int			ctlcnt;
205230130Smav	int			ascnt;
206230130Smav	int			num_devs;
207230130Smav	int			num_chans;
208230130Smav	struct hdaa_widget	*widget;
209230130Smav	struct hdaa_audio_ctl	*ctl;
210230130Smav	struct hdaa_audio_as	*as;
211230130Smav	struct hdaa_pcm_devinfo	*devs;
212230130Smav	struct hdaa_chan	*chans;
213230130Smav	struct callout		poll_jack;
214230130Smav	int			poll_ival;
215230130Smav};
216230130Smav
217230130Smav#define HDAA_CHN_RUNNING	0x00000001
218230130Smav#define HDAA_CHN_SUSPEND	0x00000002
219230130Smav
220230130Smavstruct hdaa_chan {
221230130Smav	struct snd_dbuf *b;
222230130Smav	struct pcm_channel *c;
223230130Smav	struct pcmchan_caps caps;
224230130Smav	struct hdaa_devinfo *devinfo;
225230130Smav	struct hdaa_pcm_devinfo *pdevinfo;
226230312Smav	uint32_t spd, fmt, fmtlist[32], pcmrates[16];
227230130Smav	uint32_t supp_stream_formats, supp_pcm_size_rate;
228230326Smav	uint32_t blkcnt, blksz;
229230130Smav	uint32_t *dmapos;
230230130Smav	uint32_t flags;
231230130Smav	int dir;
232230130Smav	int off;
233230130Smav	int sid;
234230130Smav	int bit16, bit32;
235230130Smav	int channels;	/* Number of audio channels. */
236230130Smav	int as;		/* Number of association. */
237230130Smav	int asindex;	/* Index within association. */
238230130Smav	nid_t io[16];
239230326Smav	uint8_t	stripecap;	/* AND of stripecap of all ios. */
240230326Smav	uint8_t	stripectl;	/* stripe to use to all ios. */
241230130Smav};
242230130Smav
243230451Smav#define MINQDB(ctl)							\
244230451Smav	((0 - (ctl)->offset) * ((ctl)->size + 1))
245230451Smav
246230451Smav#define MAXQDB(ctl)							\
247230451Smav	(((ctl)->step - (ctl)->offset) * ((ctl)->size + 1))
248230451Smav
249230451Smav#define RANGEQDB(ctl)							\
250230451Smav	((ctl)->step * ((ctl)->size + 1))
251230451Smav
252230451Smav#define VAL2QDB(ctl, val) 						\
253230451Smav	(((ctl)->size + 1) * ((int)(val) - (ctl)->offset))
254230451Smav
255230451Smav#define QDB2VAL(ctl, qdb) 						\
256230451Smav	imax(imin((((qdb) + (ctl)->size / 2 * ((qdb) > 0 ? 1 : -1)) /	\
257230451Smav	 ((ctl)->size + 1) + (ctl)->offset), (ctl)->step), 0)
258230451Smav
259230130Smav#define hdaa_codec_id(devinfo)						\
260230130Smav		(((uint32_t)hda_get_vendor_id(devinfo->dev) << 16) +	\
261230130Smav		hda_get_device_id(devinfo->dev))
262230130Smav
263242352Smav#define hdaa_card_id(devinfo)					\
264230532Smav		(((uint32_t)hda_get_subdevice_id(devinfo->dev) << 16) +	\
265230532Smav		hda_get_subvendor_id(devinfo->dev))
266230130Smav
267230130Smavstruct hdaa_widget	*hdaa_widget_get(struct hdaa_devinfo *, nid_t);
268230130Smavuint32_t		hdaa_widget_pin_patch(uint32_t config, const char *str);
269230130Smavuint32_t		hdaa_gpio_patch(uint32_t gpio, const char *str);
270230130Smav
271230130Smavvoid			hdaa_patch(struct hdaa_devinfo *devinfo);
272230130Smavvoid			hdaa_patch_direct(struct hdaa_devinfo *devinfo);
273230130Smav
274230130Smav#endif
275