xref: /openbmc/linux/sound/pci/ice1712/wm8766.c (revision 8dd06ef34b6e2f41b29fbf5fc1663780f2524285)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
245d44e5aSOndrej Zary /*
345d44e5aSOndrej Zary  *   ALSA driver for ICEnsemble VT17xx
445d44e5aSOndrej Zary  *
545d44e5aSOndrej Zary  *   Lowlevel functions for WM8766 codec
645d44e5aSOndrej Zary  *
745d44e5aSOndrej Zary  *	Copyright (c) 2012 Ondrej Zary <linux@rainbow-software.org>
845d44e5aSOndrej Zary  */
945d44e5aSOndrej Zary 
1045d44e5aSOndrej Zary #include <linux/delay.h>
1145d44e5aSOndrej Zary #include <sound/core.h>
1245d44e5aSOndrej Zary #include <sound/control.h>
1345d44e5aSOndrej Zary #include <sound/tlv.h>
1445d44e5aSOndrej Zary #include "wm8766.h"
1545d44e5aSOndrej Zary 
1645d44e5aSOndrej Zary /* low-level access */
1745d44e5aSOndrej Zary 
snd_wm8766_write(struct snd_wm8766 * wm,u16 addr,u16 data)1845d44e5aSOndrej Zary static void snd_wm8766_write(struct snd_wm8766 *wm, u16 addr, u16 data)
1945d44e5aSOndrej Zary {
20b57a895fSDan Carpenter 	if (addr < WM8766_REG_COUNT)
2145d44e5aSOndrej Zary 		wm->regs[addr] = data;
2245d44e5aSOndrej Zary 	wm->ops.write(wm, addr, data);
2345d44e5aSOndrej Zary }
2445d44e5aSOndrej Zary 
2545d44e5aSOndrej Zary /* mixer controls */
2645d44e5aSOndrej Zary 
2745d44e5aSOndrej Zary static const DECLARE_TLV_DB_SCALE(wm8766_tlv, -12750, 50, 1);
2845d44e5aSOndrej Zary 
29*4f8ce982STakashi Iwai static const struct snd_wm8766_ctl snd_wm8766_default_ctl[WM8766_CTL_COUNT] = {
3045d44e5aSOndrej Zary 	[WM8766_CTL_CH1_VOL] = {
3145d44e5aSOndrej Zary 		.name = "Channel 1 Playback Volume",
3245d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_INTEGER,
3345d44e5aSOndrej Zary 		.tlv = wm8766_tlv,
3445d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACL1,
3545d44e5aSOndrej Zary 		.reg2 = WM8766_REG_DACR1,
3645d44e5aSOndrej Zary 		.mask1 = WM8766_VOL_MASK,
3745d44e5aSOndrej Zary 		.mask2 = WM8766_VOL_MASK,
3845d44e5aSOndrej Zary 		.max = 0xff,
3945d44e5aSOndrej Zary 		.flags = WM8766_FLAG_STEREO | WM8766_FLAG_VOL_UPDATE,
4045d44e5aSOndrej Zary 	},
4145d44e5aSOndrej Zary 	[WM8766_CTL_CH2_VOL] = {
4245d44e5aSOndrej Zary 		.name = "Channel 2 Playback Volume",
4345d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_INTEGER,
4445d44e5aSOndrej Zary 		.tlv = wm8766_tlv,
4545d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACL2,
4645d44e5aSOndrej Zary 		.reg2 = WM8766_REG_DACR2,
4745d44e5aSOndrej Zary 		.mask1 = WM8766_VOL_MASK,
4845d44e5aSOndrej Zary 		.mask2 = WM8766_VOL_MASK,
4945d44e5aSOndrej Zary 		.max = 0xff,
5045d44e5aSOndrej Zary 		.flags = WM8766_FLAG_STEREO | WM8766_FLAG_VOL_UPDATE,
5145d44e5aSOndrej Zary 	},
5245d44e5aSOndrej Zary 	[WM8766_CTL_CH3_VOL] = {
5345d44e5aSOndrej Zary 		.name = "Channel 3 Playback Volume",
5445d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_INTEGER,
5545d44e5aSOndrej Zary 		.tlv = wm8766_tlv,
5645d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACL3,
5745d44e5aSOndrej Zary 		.reg2 = WM8766_REG_DACR3,
5845d44e5aSOndrej Zary 		.mask1 = WM8766_VOL_MASK,
5945d44e5aSOndrej Zary 		.mask2 = WM8766_VOL_MASK,
6045d44e5aSOndrej Zary 		.max = 0xff,
6145d44e5aSOndrej Zary 		.flags = WM8766_FLAG_STEREO | WM8766_FLAG_VOL_UPDATE,
6245d44e5aSOndrej Zary 	},
6345d44e5aSOndrej Zary 	[WM8766_CTL_CH1_SW] = {
6445d44e5aSOndrej Zary 		.name = "Channel 1 Playback Switch",
6545d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
6645d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACCTRL2,
6745d44e5aSOndrej Zary 		.mask1 = WM8766_DAC2_MUTE1,
6845d44e5aSOndrej Zary 		.flags = WM8766_FLAG_INVERT,
6945d44e5aSOndrej Zary 	},
7045d44e5aSOndrej Zary 	[WM8766_CTL_CH2_SW] = {
7145d44e5aSOndrej Zary 		.name = "Channel 2 Playback Switch",
7245d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
7345d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACCTRL2,
7445d44e5aSOndrej Zary 		.mask1 = WM8766_DAC2_MUTE2,
7545d44e5aSOndrej Zary 		.flags = WM8766_FLAG_INVERT,
7645d44e5aSOndrej Zary 	},
7745d44e5aSOndrej Zary 	[WM8766_CTL_CH3_SW] = {
7845d44e5aSOndrej Zary 		.name = "Channel 3 Playback Switch",
7945d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
8045d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACCTRL2,
8145d44e5aSOndrej Zary 		.mask1 = WM8766_DAC2_MUTE3,
8245d44e5aSOndrej Zary 		.flags = WM8766_FLAG_INVERT,
8345d44e5aSOndrej Zary 	},
8445d44e5aSOndrej Zary 	[WM8766_CTL_PHASE1_SW] = {
8545d44e5aSOndrej Zary 		.name = "Channel 1 Phase Invert Playback Switch",
8645d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
8745d44e5aSOndrej Zary 		.reg1 = WM8766_REG_IFCTRL,
8845d44e5aSOndrej Zary 		.mask1 = WM8766_PHASE_INVERT1,
8945d44e5aSOndrej Zary 	},
9045d44e5aSOndrej Zary 	[WM8766_CTL_PHASE2_SW] = {
9145d44e5aSOndrej Zary 		.name = "Channel 2 Phase Invert Playback Switch",
9245d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
9345d44e5aSOndrej Zary 		.reg1 = WM8766_REG_IFCTRL,
9445d44e5aSOndrej Zary 		.mask1 = WM8766_PHASE_INVERT2,
9545d44e5aSOndrej Zary 	},
9645d44e5aSOndrej Zary 	[WM8766_CTL_PHASE3_SW] = {
9745d44e5aSOndrej Zary 		.name = "Channel 3 Phase Invert Playback Switch",
9845d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
9945d44e5aSOndrej Zary 		.reg1 = WM8766_REG_IFCTRL,
10045d44e5aSOndrej Zary 		.mask1 = WM8766_PHASE_INVERT3,
10145d44e5aSOndrej Zary 	},
10245d44e5aSOndrej Zary 	[WM8766_CTL_DEEMPH1_SW] = {
10345d44e5aSOndrej Zary 		.name = "Channel 1 Deemphasis Playback Switch",
10445d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
10545d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACCTRL2,
10645d44e5aSOndrej Zary 		.mask1 = WM8766_DAC2_DEEMP1,
10745d44e5aSOndrej Zary 	},
10845d44e5aSOndrej Zary 	[WM8766_CTL_DEEMPH2_SW] = {
10945d44e5aSOndrej Zary 		.name = "Channel 2 Deemphasis Playback Switch",
11045d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
11145d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACCTRL2,
11245d44e5aSOndrej Zary 		.mask1 = WM8766_DAC2_DEEMP2,
11345d44e5aSOndrej Zary 	},
11445d44e5aSOndrej Zary 	[WM8766_CTL_DEEMPH3_SW] = {
11545d44e5aSOndrej Zary 		.name = "Channel 3 Deemphasis Playback Switch",
11645d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
11745d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACCTRL2,
11845d44e5aSOndrej Zary 		.mask1 = WM8766_DAC2_DEEMP3,
11945d44e5aSOndrej Zary 	},
12045d44e5aSOndrej Zary 	[WM8766_CTL_IZD_SW] = {
12145d44e5aSOndrej Zary 		.name = "Infinite Zero Detect Playback Switch",
12245d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
12345d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACCTRL1,
12445d44e5aSOndrej Zary 		.mask1 = WM8766_DAC_IZD,
12545d44e5aSOndrej Zary 	},
12645d44e5aSOndrej Zary 	[WM8766_CTL_ZC_SW] = {
12745d44e5aSOndrej Zary 		.name = "Zero Cross Detect Playback Switch",
12845d44e5aSOndrej Zary 		.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
12945d44e5aSOndrej Zary 		.reg1 = WM8766_REG_DACCTRL2,
13045d44e5aSOndrej Zary 		.mask1 = WM8766_DAC2_ZCD,
13145d44e5aSOndrej Zary 		.flags = WM8766_FLAG_INVERT,
13245d44e5aSOndrej Zary 	},
13345d44e5aSOndrej Zary };
13445d44e5aSOndrej Zary 
13545d44e5aSOndrej Zary /* exported functions */
13645d44e5aSOndrej Zary 
snd_wm8766_init(struct snd_wm8766 * wm)13745d44e5aSOndrej Zary void snd_wm8766_init(struct snd_wm8766 *wm)
13845d44e5aSOndrej Zary {
13945d44e5aSOndrej Zary 	int i;
14045d44e5aSOndrej Zary 	static const u16 default_values[] = {
14145d44e5aSOndrej Zary 		0x000, 0x100,
14245d44e5aSOndrej Zary 		0x120, 0x000,
14345d44e5aSOndrej Zary 		0x000, 0x100, 0x000, 0x100, 0x000,
14445d44e5aSOndrej Zary 		0x000, 0x080,
14545d44e5aSOndrej Zary 	};
14645d44e5aSOndrej Zary 
14745d44e5aSOndrej Zary 	memcpy(wm->ctl, snd_wm8766_default_ctl, sizeof(wm->ctl));
14845d44e5aSOndrej Zary 
14945d44e5aSOndrej Zary 	snd_wm8766_write(wm, WM8766_REG_RESET, 0x00); /* reset */
15045d44e5aSOndrej Zary 	udelay(10);
15145d44e5aSOndrej Zary 	/* load defaults */
15245d44e5aSOndrej Zary 	for (i = 0; i < ARRAY_SIZE(default_values); i++)
15345d44e5aSOndrej Zary 		snd_wm8766_write(wm, i, default_values[i]);
15445d44e5aSOndrej Zary }
15545d44e5aSOndrej Zary 
snd_wm8766_resume(struct snd_wm8766 * wm)15645d44e5aSOndrej Zary void snd_wm8766_resume(struct snd_wm8766 *wm)
15745d44e5aSOndrej Zary {
15845d44e5aSOndrej Zary 	int i;
15945d44e5aSOndrej Zary 
16045d44e5aSOndrej Zary 	for (i = 0; i < WM8766_REG_COUNT; i++)
16145d44e5aSOndrej Zary 		snd_wm8766_write(wm, i, wm->regs[i]);
16245d44e5aSOndrej Zary }
16345d44e5aSOndrej Zary 
snd_wm8766_set_if(struct snd_wm8766 * wm,u16 dac)16445d44e5aSOndrej Zary void snd_wm8766_set_if(struct snd_wm8766 *wm, u16 dac)
16545d44e5aSOndrej Zary {
16645d44e5aSOndrej Zary 	u16 val = wm->regs[WM8766_REG_IFCTRL] & ~WM8766_IF_MASK;
16745d44e5aSOndrej Zary 
16845d44e5aSOndrej Zary 	dac &= WM8766_IF_MASK;
16945d44e5aSOndrej Zary 	snd_wm8766_write(wm, WM8766_REG_IFCTRL, val | dac);
17045d44e5aSOndrej Zary }
17145d44e5aSOndrej Zary 
snd_wm8766_volume_restore(struct snd_wm8766 * wm)17245d44e5aSOndrej Zary void snd_wm8766_volume_restore(struct snd_wm8766 *wm)
17345d44e5aSOndrej Zary {
17445d44e5aSOndrej Zary 	u16 val = wm->regs[WM8766_REG_DACR1];
17545d44e5aSOndrej Zary 	/* restore volume after MCLK stopped */
17645d44e5aSOndrej Zary 	snd_wm8766_write(wm, WM8766_REG_DACR1, val | WM8766_VOL_UPDATE);
17745d44e5aSOndrej Zary }
17845d44e5aSOndrej Zary 
17945d44e5aSOndrej Zary /* mixer callbacks */
18045d44e5aSOndrej Zary 
snd_wm8766_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)18145d44e5aSOndrej Zary static int snd_wm8766_volume_info(struct snd_kcontrol *kcontrol,
18245d44e5aSOndrej Zary 				   struct snd_ctl_elem_info *uinfo)
18345d44e5aSOndrej Zary {
18445d44e5aSOndrej Zary 	struct snd_wm8766 *wm = snd_kcontrol_chip(kcontrol);
18545d44e5aSOndrej Zary 	int n = kcontrol->private_value;
18645d44e5aSOndrej Zary 
18745d44e5aSOndrej Zary 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
18845d44e5aSOndrej Zary 	uinfo->count = (wm->ctl[n].flags & WM8766_FLAG_STEREO) ? 2 : 1;
18945d44e5aSOndrej Zary 	uinfo->value.integer.min = wm->ctl[n].min;
19045d44e5aSOndrej Zary 	uinfo->value.integer.max = wm->ctl[n].max;
19145d44e5aSOndrej Zary 
19245d44e5aSOndrej Zary 	return 0;
19345d44e5aSOndrej Zary }
19445d44e5aSOndrej Zary 
snd_wm8766_enum_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)19545d44e5aSOndrej Zary static int snd_wm8766_enum_info(struct snd_kcontrol *kcontrol,
19645d44e5aSOndrej Zary 				      struct snd_ctl_elem_info *uinfo)
19745d44e5aSOndrej Zary {
19845d44e5aSOndrej Zary 	struct snd_wm8766 *wm = snd_kcontrol_chip(kcontrol);
19945d44e5aSOndrej Zary 	int n = kcontrol->private_value;
20045d44e5aSOndrej Zary 
20145d44e5aSOndrej Zary 	return snd_ctl_enum_info(uinfo, 1, wm->ctl[n].max,
20245d44e5aSOndrej Zary 						wm->ctl[n].enum_names);
20345d44e5aSOndrej Zary }
20445d44e5aSOndrej Zary 
snd_wm8766_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)20545d44e5aSOndrej Zary static int snd_wm8766_ctl_get(struct snd_kcontrol *kcontrol,
20645d44e5aSOndrej Zary 				  struct snd_ctl_elem_value *ucontrol)
20745d44e5aSOndrej Zary {
20845d44e5aSOndrej Zary 	struct snd_wm8766 *wm = snd_kcontrol_chip(kcontrol);
20945d44e5aSOndrej Zary 	int n = kcontrol->private_value;
21045d44e5aSOndrej Zary 	u16 val1, val2;
21145d44e5aSOndrej Zary 
21245d44e5aSOndrej Zary 	if (wm->ctl[n].get)
21345d44e5aSOndrej Zary 		wm->ctl[n].get(wm, &val1, &val2);
21445d44e5aSOndrej Zary 	else {
21545d44e5aSOndrej Zary 		val1 = wm->regs[wm->ctl[n].reg1] & wm->ctl[n].mask1;
21645d44e5aSOndrej Zary 		val1 >>= __ffs(wm->ctl[n].mask1);
21745d44e5aSOndrej Zary 		if (wm->ctl[n].flags & WM8766_FLAG_STEREO) {
21845d44e5aSOndrej Zary 			val2 = wm->regs[wm->ctl[n].reg2] & wm->ctl[n].mask2;
21945d44e5aSOndrej Zary 			val2 >>= __ffs(wm->ctl[n].mask2);
22045d44e5aSOndrej Zary 			if (wm->ctl[n].flags & WM8766_FLAG_VOL_UPDATE)
22145d44e5aSOndrej Zary 				val2 &= ~WM8766_VOL_UPDATE;
22245d44e5aSOndrej Zary 		}
22345d44e5aSOndrej Zary 	}
22445d44e5aSOndrej Zary 	if (wm->ctl[n].flags & WM8766_FLAG_INVERT) {
22545d44e5aSOndrej Zary 		val1 = wm->ctl[n].max - (val1 - wm->ctl[n].min);
2264c88b7f2STakashi Iwai 		if (wm->ctl[n].flags & WM8766_FLAG_STEREO)
22745d44e5aSOndrej Zary 			val2 = wm->ctl[n].max - (val2 - wm->ctl[n].min);
22845d44e5aSOndrej Zary 	}
22945d44e5aSOndrej Zary 	ucontrol->value.integer.value[0] = val1;
23045d44e5aSOndrej Zary 	if (wm->ctl[n].flags & WM8766_FLAG_STEREO)
23145d44e5aSOndrej Zary 		ucontrol->value.integer.value[1] = val2;
23245d44e5aSOndrej Zary 
23345d44e5aSOndrej Zary 	return 0;
23445d44e5aSOndrej Zary }
23545d44e5aSOndrej Zary 
snd_wm8766_ctl_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)23645d44e5aSOndrej Zary static int snd_wm8766_ctl_put(struct snd_kcontrol *kcontrol,
23745d44e5aSOndrej Zary 				  struct snd_ctl_elem_value *ucontrol)
23845d44e5aSOndrej Zary {
23945d44e5aSOndrej Zary 	struct snd_wm8766 *wm = snd_kcontrol_chip(kcontrol);
24045d44e5aSOndrej Zary 	int n = kcontrol->private_value;
24145d44e5aSOndrej Zary 	u16 val, regval1, regval2;
24245d44e5aSOndrej Zary 
24303440c4eSMasahiro Yamada 	/* this also works for enum because value is a union */
24445d44e5aSOndrej Zary 	regval1 = ucontrol->value.integer.value[0];
24545d44e5aSOndrej Zary 	regval2 = ucontrol->value.integer.value[1];
24645d44e5aSOndrej Zary 	if (wm->ctl[n].flags & WM8766_FLAG_INVERT) {
24745d44e5aSOndrej Zary 		regval1 = wm->ctl[n].max - (regval1 - wm->ctl[n].min);
24845d44e5aSOndrej Zary 		regval2 = wm->ctl[n].max - (regval2 - wm->ctl[n].min);
24945d44e5aSOndrej Zary 	}
25045d44e5aSOndrej Zary 	if (wm->ctl[n].set)
25145d44e5aSOndrej Zary 		wm->ctl[n].set(wm, regval1, regval2);
25245d44e5aSOndrej Zary 	else {
25345d44e5aSOndrej Zary 		val = wm->regs[wm->ctl[n].reg1] & ~wm->ctl[n].mask1;
25445d44e5aSOndrej Zary 		val |= regval1 << __ffs(wm->ctl[n].mask1);
25545d44e5aSOndrej Zary 		/* both stereo controls in one register */
25645d44e5aSOndrej Zary 		if (wm->ctl[n].flags & WM8766_FLAG_STEREO &&
25745d44e5aSOndrej Zary 				wm->ctl[n].reg1 == wm->ctl[n].reg2) {
25845d44e5aSOndrej Zary 			val &= ~wm->ctl[n].mask2;
25945d44e5aSOndrej Zary 			val |= regval2 << __ffs(wm->ctl[n].mask2);
26045d44e5aSOndrej Zary 		}
26145d44e5aSOndrej Zary 		snd_wm8766_write(wm, wm->ctl[n].reg1, val);
26245d44e5aSOndrej Zary 		/* stereo controls in different registers */
26345d44e5aSOndrej Zary 		if (wm->ctl[n].flags & WM8766_FLAG_STEREO &&
26445d44e5aSOndrej Zary 				wm->ctl[n].reg1 != wm->ctl[n].reg2) {
26545d44e5aSOndrej Zary 			val = wm->regs[wm->ctl[n].reg2] & ~wm->ctl[n].mask2;
26645d44e5aSOndrej Zary 			val |= regval2 << __ffs(wm->ctl[n].mask2);
26745d44e5aSOndrej Zary 			if (wm->ctl[n].flags & WM8766_FLAG_VOL_UPDATE)
26845d44e5aSOndrej Zary 				val |= WM8766_VOL_UPDATE;
26945d44e5aSOndrej Zary 			snd_wm8766_write(wm, wm->ctl[n].reg2, val);
27045d44e5aSOndrej Zary 		}
27145d44e5aSOndrej Zary 	}
27245d44e5aSOndrej Zary 
27345d44e5aSOndrej Zary 	return 0;
27445d44e5aSOndrej Zary }
27545d44e5aSOndrej Zary 
snd_wm8766_add_control(struct snd_wm8766 * wm,int num)27645d44e5aSOndrej Zary static int snd_wm8766_add_control(struct snd_wm8766 *wm, int num)
27745d44e5aSOndrej Zary {
27845d44e5aSOndrej Zary 	struct snd_kcontrol_new cont;
27945d44e5aSOndrej Zary 	struct snd_kcontrol *ctl;
28045d44e5aSOndrej Zary 
28145d44e5aSOndrej Zary 	memset(&cont, 0, sizeof(cont));
28245d44e5aSOndrej Zary 	cont.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
28345d44e5aSOndrej Zary 	cont.private_value = num;
28445d44e5aSOndrej Zary 	cont.name = wm->ctl[num].name;
28545d44e5aSOndrej Zary 	cont.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
28645d44e5aSOndrej Zary 	if (wm->ctl[num].flags & WM8766_FLAG_LIM ||
28745d44e5aSOndrej Zary 	    wm->ctl[num].flags & WM8766_FLAG_ALC)
28845d44e5aSOndrej Zary 		cont.access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
28945d44e5aSOndrej Zary 	cont.tlv.p = NULL;
29045d44e5aSOndrej Zary 	cont.get = snd_wm8766_ctl_get;
29145d44e5aSOndrej Zary 	cont.put = snd_wm8766_ctl_put;
29245d44e5aSOndrej Zary 
29345d44e5aSOndrej Zary 	switch (wm->ctl[num].type) {
29445d44e5aSOndrej Zary 	case SNDRV_CTL_ELEM_TYPE_INTEGER:
29545d44e5aSOndrej Zary 		cont.info = snd_wm8766_volume_info;
29645d44e5aSOndrej Zary 		cont.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
29745d44e5aSOndrej Zary 		cont.tlv.p = wm->ctl[num].tlv;
29845d44e5aSOndrej Zary 		break;
29945d44e5aSOndrej Zary 	case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
30045d44e5aSOndrej Zary 		wm->ctl[num].max = 1;
30145d44e5aSOndrej Zary 		if (wm->ctl[num].flags & WM8766_FLAG_STEREO)
30245d44e5aSOndrej Zary 			cont.info = snd_ctl_boolean_stereo_info;
30345d44e5aSOndrej Zary 		else
30445d44e5aSOndrej Zary 			cont.info = snd_ctl_boolean_mono_info;
30545d44e5aSOndrej Zary 		break;
30645d44e5aSOndrej Zary 	case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
30745d44e5aSOndrej Zary 		cont.info = snd_wm8766_enum_info;
30845d44e5aSOndrej Zary 		break;
30945d44e5aSOndrej Zary 	default:
31045d44e5aSOndrej Zary 		return -EINVAL;
31145d44e5aSOndrej Zary 	}
31245d44e5aSOndrej Zary 	ctl = snd_ctl_new1(&cont, wm);
31345d44e5aSOndrej Zary 	if (!ctl)
31445d44e5aSOndrej Zary 		return -ENOMEM;
31545d44e5aSOndrej Zary 	wm->ctl[num].kctl = ctl;
31645d44e5aSOndrej Zary 
31745d44e5aSOndrej Zary 	return snd_ctl_add(wm->card, ctl);
31845d44e5aSOndrej Zary }
31945d44e5aSOndrej Zary 
snd_wm8766_build_controls(struct snd_wm8766 * wm)32045d44e5aSOndrej Zary int snd_wm8766_build_controls(struct snd_wm8766 *wm)
32145d44e5aSOndrej Zary {
32245d44e5aSOndrej Zary 	int err, i;
32345d44e5aSOndrej Zary 
32445d44e5aSOndrej Zary 	for (i = 0; i < WM8766_CTL_COUNT; i++)
32545d44e5aSOndrej Zary 		if (wm->ctl[i].name) {
32645d44e5aSOndrej Zary 			err = snd_wm8766_add_control(wm, i);
32745d44e5aSOndrej Zary 			if (err < 0)
32845d44e5aSOndrej Zary 				return err;
32945d44e5aSOndrej Zary 		}
33045d44e5aSOndrej Zary 
33145d44e5aSOndrej Zary 	return 0;
33245d44e5aSOndrej Zary }
333