Skip to contentSkip to conversation

Support for muting channels DAC with CA0106 - SB0570

PATCH In review Trent Piepho 2 participants 4 messages Jul 22, 2007 – Jul 24, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Support for muting channels DAC with CA0106 - SB0570

This patch adds mute controls for the analog output channels. It does this by sending SPI commands to the DAC on cards like the SB live 24-bit / Audigy SE that have an SPI DAC.

# HG changeset patch
# User Trent Piepho <[email protected]>
# Date 1185062879 25200
# Node ID 2c3514f4607b69271148337af15be55d3205af70 # Parent 04b7771f7c31709e9d1883264bcde7e34781a590
ca0106: Add analog mute controls for cards with SPI DAC

Add four mute controls for the analog output channels for cards that use a spi dac, like SB0570 SB Live! 24-bit / Audigy SE. The Wolfson dac doesn't support muting each channel so the controls are mono.

The chip state struct gets a 32-byte array to act as a shadow of the spi dac registers. Only two registers are used for mute, but more would be needed for de-emphasis, DAC power down, phase inversion, and other features.

pci/ca0106/ca0106.h +4 -1
diff -r 04b7771f7c31 -r 2c3514f4607b pci/ca0106/ca0106.h
@@ -611,6 +611,8 @@ struct snd_ca0106 {
 
 	struct snd_ca_midi midi;
 	struct snd_ca_midi midi2;
+
+	u16 spi_dac_reg[16];
 };
 
 int snd_ca0106_mixer(struct snd_ca0106 *emu);
@@ -627,4 +629,5 @@ void snd_ca0106_ptr_write(struct snd_ca0
 
 int snd_ca0106_i2c_write(struct snd_ca0106 *emu, u32 reg, u32 value);
 
-
+int snd_ca0106_spi_write(struct snd_ca0106 * emu,
+				   unsigned int data);
pci/ca0106/ca0106_main.c +4 -1
diff -r 04b7771f7c31 -r 2c3514f4607b pci/ca0106/ca0106_main.c
@@ -1475,8 +1475,11 @@ static int __devinit snd_ca0106_create(i
 		int size, n;
 
 		size = ARRAY_SIZE(spi_dac_init);
-		for (n=0; n < size; n++)
+		for (n=0; n < size; n++) {
 			snd_ca0106_spi_write(chip, spi_dac_init[n]);
+			if ((spi_dac_init[n]>>9) < ARRAY_SIZE(chip->spi_dac_reg))
+				chip->spi_dac_reg[spi_dac_init[n]>>9] = spi_dac_init[n];
+		}
 	}
 
 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
pci/ca0106/ca0106_mixer.c +59 -0

77 lines not on this page · raw message

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#1 lore raw

At Sat, 21 Jul 2007 17:10:08 -0700 (PDT),

Quoted 12 lines · Trent Piepho

This patch adds mute controls for the analog output channels. It does this by sending SPI commands to the DAC on cards like the SB live 24-bit / Audigy SE that have an SPI DAC.
[2 <text/plain; US-ASCII (base64)>]
# HG changeset patch
# User Trent Piepho <[email protected]>
# Date 1185062879 25200
# Node ID 2c3514f4607b69271148337af15be55d3205af70
# Parent 04b7771f7c31709e9d1883264bcde7e34781a590
ca0106: Add analog mute controls for cards with SPI DAC

Quoted 8 lines

Add four mute controls for the analog output channels for cards that use a spi dac, like SB0570 SB Live! 24-bit / Audigy SE. The Wolfson dac doesn't support muting each channel so the controls are mono.

The chip state struct gets a 32-byte array to act as a shadow of the spi dac registers. Only two registers are used for mute, but more would be needed for de-emphasis, DAC power down, phase inversion, and other features.

Thanks, the patch looks almost fine, but please fix the following minor issues:

- Try to avoid a magic number, e.g. >> 9.
- put callback should return 0 for non-changed value, a positive (usually 1) when the value is changed, and a negative error code for errors.
- Try to keep lines within 80 chars (to follow the standard coding style). I know that other codes there are not, but don't care now. We'll fix them later, too.

Takashi

Quoted 124 lines

diff -r 04b7771f7c31 -r 2c3514f4607b pci/ca0106/ca0106.h

pci/ca0106/ca0106.h +4 -1
@@ -611,6 +611,8 @@ struct snd_ca0106 {
 
 	struct snd_ca_midi midi;
 	struct snd_ca_midi midi2;
+
+	u16 spi_dac_reg[16];
 };
 
 int snd_ca0106_mixer(struct snd_ca0106 *emu);
@@ -627,4 +629,5 @@ void snd_ca0106_ptr_write(struct snd_ca0
 
 int snd_ca0106_i2c_write(struct snd_ca0106 *emu, u32 reg, u32 value);
 
-
+int snd_ca0106_spi_write(struct snd_ca0106 * emu,
+				   unsigned int data);

diff -r 04b7771f7c31 -r 2c3514f4607b pci/ca0106/ca0106_main.c

pci/ca0106/ca0106_main.c +4 -1
@@ -1475,8 +1475,11 @@ static int __devinit snd_ca0106_create(i
 		int size, n;
 
 		size = ARRAY_SIZE(spi_dac_init);
-		for (n=0; n < size; n++)
+		for (n=0; n < size; n++) {
 			snd_ca0106_spi_write(chip, spi_dac_init[n]);
+			if ((spi_dac_init[n]>>9) < ARRAY_SIZE(chip->spi_dac_reg))
+				chip->spi_dac_reg[spi_dac_init[n]>>9] = spi_dac_init[n];
+		}
 	}
 
 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,

diff -r 04b7771f7c31 -r 2c3514f4607b pci/ca0106/ca0106_mixer.c

pci/ca0106/ca0106_mixer.c +59 -0
@@ -470,6 +470,41 @@ static int snd_ca0106_i2c_volume_put(str
 	return change;
 }
 
+static int spi_mute_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
+{
+	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
+	uinfo->count = 1;
+	uinfo->value.integer.min = 0;
+	uinfo->value.integer.max = 1;
+	return 0;
+}
+
+static int spi_mute_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+	unsigned int reg = kcontrol->private_value >> 9;
+	unsigned int bit = kcontrol->private_value & 0x1ff;
+
+	ucontrol->value.integer.value[0] = !(emu->spi_dac_reg[reg] & bit);
+	return 0;
+}
+
+static int spi_mute_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+	unsigned int reg = kcontrol->private_value >> 9;
+	unsigned int bit = kcontrol->private_value & 0x1ff;
+	int ret;
+
+	if (ucontrol->value.integer.value[0])
+		emu->spi_dac_reg[reg] &= ~bit;
+	else
+		emu->spi_dac_reg[reg] |= bit;
+
+	ret = snd_ca0106_spi_write(emu, emu->spi_dac_reg[reg]);
+	return ret ? -1 : 1;
+}
+
 #define CA_VOLUME(xname,chid,reg) \
 {								\
 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
@@ -560,6 +595,23 @@ static struct snd_kcontrol_new snd_ca010
         I2C_VOLUME("Mic Capture Volume", 1),
         I2C_VOLUME("Line in Capture Volume", 2),
         I2C_VOLUME("Aux Capture Volume", 3),
+};
+
+#define SPI_SWITCH(xname,reg,bit) \
+{								\
+	.iface 	= SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
+	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
+	.info	= spi_mute_info,				\
+	.get	= spi_mute_get,					\
+	.put	= spi_mute_put,					\
+	.private_value = (reg<<9) | (1<<bit)			\
+}
+
+static struct snd_kcontrol_new snd_ca0106_volume_spi_dac_ctls[] __devinitdata = {
+	SPI_SWITCH("Analog Front Playback Switch", 15, 2),
+	SPI_SWITCH("Analog Rear Playback Switch", 9, 3),
+	SPI_SWITCH("Analog Center/LFE Playback Switch", 9, 5),
+	SPI_SWITCH("Analog Side Playback Switch", 9, 4),
 };
 
 static int __devinit remove_ctl(struct snd_card *card, const char *name)
@@ -658,6 +710,13 @@ int __devinit snd_ca0106_mixer(struct sn
 		if (err < 0)
 			return err;
 	}
+	if (emu->details->spi_dac == 1) {
+		for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_spi_dac_ctls); i++) {
+			err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_spi_dac_ctls[i], emu));
+			if (err < 0)
+				return err;
+		}
+	}
         return 0;
 }
 

[3 <text/plain; us-ascii (7bit)>]
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#2 lore raw
Quoted 5 lines · Takashi Iwai

At Sat, 21 Jul 2007 17:10:08 -0700 (PDT),
Thanks, the patch looks almost fine, but please fix the following minor issues:

- Try to avoid a magic number, e.g. >> 9.

Ok, I added a bunch of the fields from the WM8768 datasheet that could be useful.

Quoted 3 lines

- put callback should return 0 for non-changed value, a positive (usually 1) when the value is changed, and a negative error code for errors.

It will now return 0 if the value wasn't changed and also not do the unnecessary spi write.

Quoted 3 lines

- Try to keep lines within 80 chars (to follow the standard coding style). I know that other codes there are not, but don't care now. We'll fix them later, too.

Also done. The loops that add the controls have long lines that didn't wrap well. So I made a second patch that converts the loop I added plus the two existing loops into a macro, which IMHO looks a lot nicer and reduces code duplication.

I also upped the version numbers and used snd_ctl_boolean_mono_info.

# HG changeset patch
# User Trent Piepho <[email protected]>
# Date 1185228660 25200
# Node ID 87a07ac27d0cd5f7ce391f3f52229b6516efb3bc # Parent c30a197f63c386c9478fa242d78dd3efe7165adb
ca0106: Add analog mute controls for cards with SPI DAC

Add four mute controls for the analog output channels for cards that use an SPI DAC, like the SB0570 SB Live! 24-bit / Audigy SE. The Wolfson DAC doesn't support muting left/right so the controls are mono.

The chip state struct gets a 32-byte array to act as a shadow of the spi dac registers. Only two registers are used for mute, but more would be needed for analog gain, de-emphasis, DAC power down, phase inversion, and other features.

pci/ca0106/ca0106.h +45 -2
diff -r c30a197f63c3 -r 87a07ac27d0c pci/ca0106/ca0106.h
@@ -1,7 +1,7 @@
 /*
  *  Copyright (c) 2004 James Courtier-Dutton <[email protected]>
  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
- *  Version: 0.0.21
+ *  Version: 0.0.22
  *
  *  FEATURES currently supported:
  *    See ca0106_main.c for features.
@@ -47,6 +47,8 @@
  *    Added GPIO info for SB Live 24bit.
  *  0.0.21
  *   Implement support for Line-in capture on SB Live 24bit.
+ *  0.0.22
+ *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
  *
  *
  *  This code was initally based on code from ALSA's emu10k1x.c which is:
@@ -552,6 +554,44 @@
 #define CONTROL_CENTER_LFE_CHANNEL 1
 #define CONTROL_UNKNOWN_CHANNEL 2
 
+
+/* Based on WM8768 Datasheet Rev 4.2 page 32 */
+#define SPI_REG_MASK	0x1ff	/* 16-bit SPI writes have a 7-bit address */
+#define SPI_REG_SHIFT	9	/* followed by 9 bits of data */
+
+/* They really do label the bit for the 4th channel "4" and not "3" */
+#define SPI_DMUTE0_REG		9
+#define SPI_DMUTE1_REG		9
+#define SPI_DMUTE2_REG		9
+#define SPI_DMUTE4_REG		15
+#define SPI_DMUTE0_BIT		3
+#define SPI_DMUTE1_BIT		4
+#define SPI_DMUTE2_BIT		5
+#define SPI_DMUTE4_BIT		2
+
+#define SPI_PHASE0_REG		3
+#define SPI_PHASE1_REG		3
+#define SPI_PHASE2_REG		3
+#define SPI_PHASE4_REG		15
+#define SPI_PHASE0_BIT		6
+#define SPI_PHASE1_BIT		7
+#define SPI_PHASE2_BIT		8
+#define SPI_PHASE4_BIT		3
+
+#define SPI_PDWN_REG		2	/* power down all DACs */
+#define SPI_PDWN_BIT		2
+#define SPI_DACD0_REG		10	/* power down individual DACs */
+#define SPI_DACD1_REG		10
+#define SPI_DACD2_REG		10
+#define SPI_DACD4_REG		15
+#define SPI_DACD0_BIT		1
+#define SPI_DACD1_BIT		2
+#define SPI_DACD2_BIT		3
+#define SPI_DACD4_BIT		1
+
+#define SPI_PWRDNALL_REG	10	/* power down everything */
+#define SPI_PWRDNALL_BIT	4
+
 #include "ca_midi.h"
 
 struct snd_ca0106;
@@ -611,6 +651,8 @@ struct snd_ca0106 {
 
 	struct snd_ca_midi midi;
 	struct snd_ca_midi midi2;
+
+	u16 spi_dac_reg[16];
 };
 
 int snd_ca0106_mixer(struct snd_ca0106 *emu);
@@ -627,4 +669,5 @@ void snd_ca0106_ptr_write(struct snd_ca0
 
 int snd_ca0106_i2c_write(struct snd_ca0106 *emu, u32 reg, u32 value);
 
-
+int snd_ca0106_spi_write(struct snd_ca0106 * emu,
+				   unsigned int data);
pci/ca0106/ca0106_main.c +9 -2
diff -r c30a197f63c3 -r 87a07ac27d0c pci/ca0106/ca0106_main.c
@@ -1,7 +1,7 @@
 /*
  *  Copyright (c) 2004 James Courtier-Dutton <[email protected]>
  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
- *  Version: 0.0.23
+ *  Version: 0.0.24
  *
  *  FEATURES currently supported:
  *    Front, Rear and Center/LFE.
@@ -79,6 +79,8 @@
  *    Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
  *  0.0.23
  *    Implement support for Line-in capture on SB Live 24bit.
+ *  0.0.24
+ *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
  *
  *  BUGS:
  *    Some stability problems when unloading the snd-ca0106 kernel module.
@@ -1484,8 +1486,13 @@ static int __devinit snd_ca0106_create(i
 		int size, n;
 
 		size = ARRAY_SIZE(spi_dac_init);
-		for (n=0; n < size; n++)
+		for (n=0; n < size; n++) {
+			int reg = spi_dac_init[n] >> SPI_REG_SHIFT;
+
 			snd_ca0106_spi_write(chip, spi_dac_init[n]);
+			if (reg < ARRAY_SIZE(chip->spi_dac_reg))
+				chip->spi_dac_reg[reg] = spi_dac_init[n];
+		}
 	}
 
 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
pci/ca0106/ca0106_mixer.c +68 -1

99 lines not on this page · raw message

# HG changeset patch
# User Trent Piepho <[email protected]> # Date 1185229288 25200
# Node ID 677282fd6bcc15373480eefb52b763b363d940ee # Parent 87a07ac27d0cd5f7ce391f3f52229b6516efb3bc
ca0106: replaced control add sequences with macro

Turn a rather long lined for loop that is duplicated multiple times into a macro.

pci/ca0106/ca0106_mixer.c +11 -17
diff -r 87a07ac27d0c -r 677282fd6bcc pci/ca0106/ca0106_mixer.c
@@ -643,6 +643,13 @@ static int __devinit rename_ctl(struct s
 	return -ENOENT;
 }
 
+#define ADD_CTLS(ctls)							\
+	for (i = 0; i < ARRAY_SIZE(ctls); i++) {			\
+		err = snd_ctl_add(card, snd_ctl_new1(&ctls[i], emu));	\
+		if (err < 0)						\
+			return err;					\
+	}								\
+
 int __devinit snd_ca0106_mixer(struct snd_ca0106 *emu)
 {
 	int i, err;
@@ -692,17 +699,9 @@ int __devinit snd_ca0106_mixer(struct sn
 		rename_ctl(card, c[0], c[1]);
 #endif
 
-	for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_ctls); i++) {
-		err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_ctls[i], emu));
-		if (err < 0)
-			return err;
-	}
+	ADD_CTLS(snd_ca0106_volume_ctls);
 	if (emu->details->i2c_adc == 1) {
-		for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_i2c_adc_ctls); i++) {
-			err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_i2c_adc_ctls[i], emu));
-			if (err < 0)
-				return err;
-		}
+		ADD_CTLS(snd_ca0106_volume_i2c_adc_ctls);
 		if (emu->details->gpio_type == 1)
 			err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_mic_line_in, emu));
 		else  /* gpio_type == 2 */
@@ -710,13 +709,8 @@ int __devinit snd_ca0106_mixer(struct sn
 		if (err < 0)
 			return err;
 	}
-	if (emu->details->spi_dac == 1) {
-		for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_spi_dac_ctls); i++) {
-			err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_spi_dac_ctls[i], emu));
-			if (err < 0)
-				return err;
-		}
-	}
+	if (emu->details->spi_dac == 1)
+		ADD_CTLS(snd_ca0106_volume_spi_dac_ctls);
         return 0;
 }
 

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#3 lore raw

At Mon, 23 Jul 2007 16:43:14 -0700 (PDT),

Quoted 10 lines · Trent Piepho
Quoted 5 lines · Takashi Iwai

At Sat, 21 Jul 2007 17:10:08 -0700 (PDT),
Thanks, the patch looks almost fine, but please fix the following minor issues:

- Try to avoid a magic number, e.g. >> 9.

Ok, I added a bunch of the fields from the WM8768 datasheet that could be useful.

Thanks, that's _much_ better.

Quoted 7 lines
Quoted 3 lines

- put callback should return 0 for non-changed value, a positive (usually 1) when the value is changed, and a negative error code for errors.

It will now return 0 if the value wasn't changed and also not do the unnecessary spi write.

Quoted 8 lines
Quoted 3 lines

- Try to keep lines within 80 chars (to follow the standard coding style). I know that other codes there are not, but don't care now. We'll fix them later, too.

Also done. The loops that add the controls have long lines that didn't wrap well. So I made a second patch that converts the loop I added plus the two existing loops into a macro, which IMHO looks a lot nicer and reduces code duplication.

Yes. I fixed the macro to wrap with do {} while (0), and use temporary variables to be more safe.

Both patches are applied now to ALSA HG tree.

Takashi