Skip to contentSkip to conversation

ALSA: Oxygen: Add the separate SPI waiting function

PATCH Superseded Roman Volkov 2 participants 24 messages Jan 17, 2014 – Jan 20, 2014

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/16 ALSA: Oxygen: Add the separate SPI waiting function

This function performs waiting when the SPI bus completes a transaction. Timeout error checking introduced and the timeout increased to 400 from 40.

---
 sound/pci/oxygen/oxygen_io.c |   15 +++++++++++++++
 1 file changed, 15 insertions(+)
sound/pci/oxygen/oxygen_io.c +15 -0
index 521eae4..a917f37 100644
@@ -194,6 +194,21 @@ void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec,
 }
 EXPORT_SYMBOL(oxygen_write_ac97_masked);
 
+static int oxygen_wait_spi(struct oxygen *chip)
+{
+	/* Higher timeout to be sure: 400 microseconds */
+	unsigned int count;
+	for (count = 100; count > 0; count--) {
+		if ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) &
+						OXYGEN_SPI_BUSY) == 0)
+			return 0;
+		udelay(4);
+		--count;
+	}
+	snd_printk(KERN_ERR "Oxygen: SPI wait timeout\n");
+	return -EIO;
+}
+
 void oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
 {
 	unsigned int count;
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v3 2/16 ALSA: Oxygen: Modify the SPI writing function

The SPI writing function modified to use the new waiting function. Return value changed from void to int, so it can return error codes. Older drivers just ignore that return value, the Xonar DG/DGX driver handles this value. We need to wait AFTER initiating the SPI transaction, otherwise read operation will not work.

---
 sound/pci/oxygen/oxygen.h    |    2 +-
 sound/pci/oxygen/oxygen_io.c |   15 ++++-----------
 2 files changed, 5 insertions(+), 12 deletions(-)
sound/pci/oxygen/oxygen.h +1 -1
index 09a24b2..c10ab07 100644
@@ -198,7 +198,7 @@ void oxygen_write_ac97(struct oxygen *chip, unsigned int codec,
 void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec,
 			      unsigned int index, u16 data, u16 mask);
 
-void oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data);
+int oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data);
 void oxygen_write_i2c(struct oxygen *chip, u8 device, u8 map, u8 data);
 
 void oxygen_reset_uart(struct oxygen *chip);
sound/pci/oxygen/oxygen_io.c +4 -11
index a917f37..fb9c826 100644
@@ -209,23 +209,16 @@ static int oxygen_wait_spi(struct oxygen *chip)
 	return -EIO;
 }
 
-void oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
+int oxygen_write_spi(struct oxygen *chip, u8 control, unsigned int data)
 {
-	unsigned int count;
-
-	/* should not need more than 30.72 us (24 * 1.28 us) */
-	count = 10;
-	while ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) & OXYGEN_SPI_BUSY)
-	       && count > 0) {
-		udelay(4);
-		--count;
-	}
-
+	/* We need to wait AFTER initiating the SPI transaction,
+	   otherwise read operations will not work */
 	oxygen_write8(chip, OXYGEN_SPI_DATA1, data);
 	oxygen_write8(chip, OXYGEN_SPI_DATA2, data >> 8);
 	if (control & OXYGEN_SPI_DATA_LENGTH_3)
 		oxygen_write8(chip, OXYGEN_SPI_DATA3, data >> 16);
 	oxygen_write8(chip, OXYGEN_SPI_CONTROL, control);
+	return oxygen_wait_spi(chip);
 }
 EXPORT_SYMBOL(oxygen_write_spi);
 
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v3 3/16 ALSA: Oxygen: Add mute mask for the OXYGEN_PLAY_ROUTING register

In reply to Roman Volkov

The Xonar DG/DGX driver needs this mask to mute unnecessary channels.

---
 sound/pci/oxygen/oxygen_regs.h |    1 +
 1 file changed, 1 insertion(+)
sound/pci/oxygen/oxygen_regs.h +1 -0
index 63dc7a0..8c191ba 100644
@@ -318,6 +318,7 @@
 #define  OXYGEN_PLAY_MUTE23		0x0002
 #define  OXYGEN_PLAY_MUTE45		0x0004
 #define  OXYGEN_PLAY_MUTE67		0x0008
+#define  OXYGEN_PLAY_MUTE_MASK		0x000f
 #define  OXYGEN_PLAY_MULTICH_MASK	0x0010
 #define  OXYGEN_PLAY_MULTICH_I2S_DAC	0x0000
 #define  OXYGEN_PLAY_MULTICH_AC97	0x0010
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v3 4/16 ALSA: Oxygen: Add CS4245_SPI_READ constant

In reply to Roman Volkov

The driver needs this constant to perform SPI read operations. The shift from CS4245_SPI_ADDRESS and CS4245_SPI_WRITE constants removed, since when we need to send two bytes instead of three in the SPI packet, we need to shift left by 8 instead of 16.

---
 sound/pci/oxygen/cs4245.h |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
sound/pci/oxygen/cs4245.h +3 -2
index 5e0197e..3dd487a 100644
@@ -103,5 +103,6 @@
 #define CS4245_ADC_UNDRFL	0x01
 
 
-#define CS4245_SPI_ADDRESS	(0x9e << 16)
-#define CS4245_SPI_WRITE	(0 << 16)
+#define CS4245_SPI_ADDRESS	0x9e
+#define CS4245_SPI_WRITE	0
+#define CS4245_SPI_READ		1
Signature · 1 line
1.7.10.4
maintainer
#4 lore raw

Re: [PATCH v3 04/16] ALSA: Oxygen: Add CS4245_SPI_READ constant

sound/pci/oxygen/cs4245.h line 107

Quoted 9 lines · Roman Volkov

The driver needs this constant to perform SPI read operations. The shift from CS4245_SPI_ADDRESS and CS4245_SPI_WRITE constants removed, since when we need to send two bytes instead of three in the SPI packet, we need to shift left by 8 instead of 16.

sound/pci/oxygen/cs4245.h +2 -2
-#define CS4245_SPI_ADDRESS	(0x9e << 16)
-#define CS4245_SPI_WRITE	(0 << 16)
+#define CS4245_SPI_ADDRESS	0x9e
+#define CS4245_SPI_WRITE	0

This change alone breaks the driver.

All patches must be self-contained, which is important to be able to apply the patch series partially during debugging.

Please merge this with the patch that changes the SPI code.

Regards,
Clemens

#5 lore raw

PATCH v3 5/16 ALSA: Oxygen: Export oxygen_update_dac_routing symbol

In reply to Roman Volkov

When the user switches stereo output to multichannel or vice versa, the driver needs to update the channel routing. Instead of creating additional subroutines, I better export existing oxygen_update_dac_routing symbol from the oxygen mixer and call this function. It calls model.adjust_dac_routing() and my function does the work.

---
 sound/pci/oxygen/oxygen_mixer.c |    1 +
 1 file changed, 1 insertion(+)
sound/pci/oxygen/oxygen_mixer.c +1 -0
index c0dbb52..5988e04 100644
@@ -190,6 +190,7 @@ void oxygen_update_dac_routing(struct oxygen *chip)
 	if (chip->model.update_center_lfe_mix)
 		chip->model.update_center_lfe_mix(chip, chip->dac_routing > 2);
 }
+EXPORT_SYMBOL(oxygen_update_dac_routing);
 
 static int upmix_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
 {
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH v3 6/16 ALSA: Oxygen: Change description of the xonar_dg.c file

In reply to Roman Volkov

Some additional information in comments and my copyright added.

---
 sound/pci/oxygen/xonar_dg.c |   20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
sound/pci/oxygen/xonar_dg.c +14 -6
index 77acd79..b806cc1 100644
@@ -1,8 +1,8 @@
 /*
- * card driver for the Xonar DG/DGX
+ * Card driver for the Xonar DG/DGX
  *
  * Copyright (c) Clemens Ladisch <[email protected]>
- *
+ * Copyright (c) Roman Volkov <[email protected]>
  *
  *  This driver is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License, version 2.
@@ -20,27 +20,35 @@
  * Xonar DG/DGX
  * ------------
  *
+ * CS4245 and CS4361 both will mute all outputs if any clock ratio
+ * is invalid.
+ *
  * CMI8788:
  *
  *   SPI 0 -> CS4245
  *
+ *   Playback:
  *   I²S 1 -> CS4245
  *   I²S 2 -> CS4361 (center/LFE)
  *   I²S 3 -> CS4361 (surround)
  *   I²S 4 -> CS4361 (front)
+ *   Capture:
+ *   I²S ADC 1 <- CS4245
  *
  *   GPIO 3 <- ?
  *   GPIO 4 <- headphone detect
- *   GPIO 5 -> route input jack to line-in (0) or mic-in (1)
- *   GPIO 6 -> route input jack to line-in (0) or mic-in (1)
- *   GPIO 7 -> enable rear headphone amp
+ *   GPIO 5 -> enable ADC analog circuit for the left channel
+ *   GPIO 6 -> enable ADC analog circuit for the right channel
+ *   GPIO 7 -> switch green rear output jack between CS4245 and
+ * and the first channel of CS4361 (mechanical relay)
  *   GPIO 8 -> enable output to speakers
  *
  * CS4245:
  *
+ *   input 0 <- mic
  *   input 1 <- aux
  *   input 2 <- front mic
- *   input 4 <- line/mic
+ *   input 4 <- line
  *   DAC out -> headphones
  *   aux out -> front panel headphones
  */
Signature · 6 lines
1.7.10.4

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

PATCH v3 7/16 ALSA: Oxygen: Additional definitions for the Xonar DG/DGX card

In reply to Roman Volkov

Some additional constants added to the xonar_dg.h file:
capture and playback sources. GPIO_* constants and the dg struct moved to the header file from the xonar_dg.c file.

---
 sound/pci/oxygen/xonar_dg.c |   14 --------------
 sound/pci/oxygen/xonar_dg.h |   23 +++++++++++++++++++++++
 2 files changed, 23 insertions(+), 14 deletions(-)
sound/pci/oxygen/xonar_dg.c +0 -14
index b806cc1..2a0ad6e 100644
@@ -64,20 +64,6 @@
 #include "xonar_dg.h"
 #include "cs4245.h"
 
-#define GPIO_MAGIC		0x0008
-#define GPIO_HP_DETECT		0x0010
-#define GPIO_INPUT_ROUTE	0x0060
-#define GPIO_HP_REAR		0x0080
-#define GPIO_OUTPUT_ENABLE	0x0100
-
-struct dg {
-	unsigned int output_sel;
-	s8 input_vol[4][2];
-	unsigned int input_sel;
-	u8 hp_vol_att;
-	u8 cs4245_regs[0x11];
-};
-
 static void cs4245_write(struct oxygen *chip, unsigned int reg, u8 value)
 {
 	struct dg *data = chip->model_data;
sound/pci/oxygen/xonar_dg.h +23 -0
index 5688d78..0812692 100644
@@ -3,6 +3,29 @@
 
 #include "oxygen.h"
 
+#define GPIO_MAGIC		0x0008
+#define GPIO_HP_DETECT		0x0010
+#define GPIO_INPUT_ROUTE	0x0060
+#define GPIO_HP_REAR		0x0080
+#define GPIO_OUTPUT_ENABLE	0x0100
+
+#define CAPTURE_SRC_MIC		0
+#define CAPTURE_SRC_FP_MIC	1
+#define CAPTURE_SRC_LINE	2
+#define CAPTURE_SRC_AUX		3
+
+#define PLAYBACK_DST_HP		0
+#define PLAYBACK_DST_HP_FP	1
+#define PLAYBACK_DST_MULTICH	2
+
+struct dg {
+	unsigned int output_sel;
+	s8 input_vol[4][2];
+	unsigned int input_sel;
+	u8 hp_vol_att;
+	u8 cs4245_regs[0x11];
+};
+
 extern struct oxygen_model model_xonar_dg;
 
 #endif
Signature · 1 line
1.7.10.4
#8 lore raw

PATCH v3 8/16 ALSA: Oxygen: Add new CS4245 SPI functions

In reply to Roman Volkov

The existing write function slightly changed. The new read function added. This read function used for creating initial registers dump and may be used for debugging purposes. SPI operations are cached, so there is a new function to work with the cache (shadow).

---
 sound/pci/oxygen/xonar_dg.c |   60 +++++++++++++++++++++++++++++++++++++++++++
 sound/pci/oxygen/xonar_dg.h |    7 +++++
 2 files changed, 67 insertions(+)
sound/pci/oxygen/xonar_dg.c +60 -0
index 2a0ad6e..0a8ebe1 100644
@@ -64,6 +64,66 @@
 #include "xonar_dg.h"
 #include "cs4245.h"
 
+int cs4245_write_spi(struct oxygen *chip, u8 reg)
+{
+	struct dg *data = chip->model_data;
+	register unsigned int packet;
+
+	packet = (u32)reg;
+	packet <<= 8;
+	packet |= (CS4245_SPI_ADDRESS | CS4245_SPI_WRITE) << 16;
+	packet |= data->cs4245_shadow[reg];
+
+	return oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
+			 OXYGEN_SPI_DATA_LENGTH_3 |
+			 OXYGEN_SPI_CLOCK_1280 |
+			 (0 << OXYGEN_SPI_CODEC_SHIFT) |
+			 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
+			 packet);
+}
+
+int cs4245_read_spi(struct oxygen *chip, u8 addr)
+{
+	struct dg *data = chip->model_data;
+	int ret;
+
+	ret = oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
+		OXYGEN_SPI_DATA_LENGTH_2 |
+		OXYGEN_SPI_CEN_LATCH_CLOCK_HI |
+		OXYGEN_SPI_CLOCK_1280 | (0 << OXYGEN_SPI_CODEC_SHIFT),
+		((CS4245_SPI_ADDRESS | CS4245_SPI_WRITE) << 8) | addr);
+	if (ret < 0)
+		return ret;
+
+	ret = oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
+		OXYGEN_SPI_DATA_LENGTH_2 |
+		OXYGEN_SPI_CEN_LATCH_CLOCK_HI |
+		OXYGEN_SPI_CLOCK_1280 | (0 << OXYGEN_SPI_CODEC_SHIFT),
+		(CS4245_SPI_ADDRESS | CS4245_SPI_READ) << 8);
+	if (ret < 0)
+		return ret;
+
+	data->cs4245_shadow[addr] = oxygen_read8(chip, OXYGEN_SPI_DATA1);
+
+	return 0;
+}
+
+int cs4245_shadow_control(struct oxygen *chip, enum cs4245_shadow_operation op)
+{
+	unsigned char addr;
+	int ret;
+
+	for (addr = 1; addr < ARRAY_SIZE((((struct dg *)0)->cs4245_shadow));
+								addr++) {
+		ret = (op == CS4245_SAVE_TO_SHADOW ?
+			cs4245_read_spi(chip, addr) :
+			cs4245_write_spi(chip, addr));
+		if (ret < 0)
+			return ret;
+	}
+	return 0;
+}
+
 static void cs4245_write(struct oxygen *chip, unsigned int reg, u8 value)
 {
 	struct dg *data = chip->model_data;
sound/pci/oxygen/xonar_dg.h +7 -0
index 0812692..f861a4c 100644
@@ -18,7 +18,14 @@
 #define PLAYBACK_DST_HP_FP	1
 #define PLAYBACK_DST_MULTICH	2
 
+enum cs4245_shadow_operation {
+	CS4245_SAVE_TO_SHADOW,
+	CS4245_LOAD_FROM_SHADOW
+};
+
 struct dg {
+	/* The shadow copy of the CS4245 register space */
+	unsigned char cs4245_shadow[17];
 	unsigned int output_sel;
 	s8 input_vol[4][2];
 	unsigned int input_sel;
Signature · 1 line
1.7.10.4
maintainer
#9 lore raw

Re: [PATCH v3 08/16] ALSA: Oxygen: Add new CS4245 SPI functions

sound/pci/oxygen/xonar_dg.c line 117

Quoted 7 lines · Roman Volkov
sound/pci/oxygen/xonar_dg.c +7 -0
+int cs4245_shadow_control(struct oxygen *chip, enum cs4245_shadow_operation op)
+{
+	unsigned char addr;
+	int ret;
+
+	for (addr = 1; addr < ARRAY_SIZE((((struct dg *)0)->cs4245_shadow));
+								addr++) {

This looks ugly (and there's one pair of parentheses too many). Just include the line "struct dg *data = chip->model_data;" in this function.

Regards,
Clemens

#10 lore raw

PATCH v3 9/16 ALSA: Oxygen: Modify initialization functions

In reply to Roman Volkov

Changes in GPIO values and sequence at startup, CS4245 initialization changed and simplified, according to datasheets and reverse-engineering information.

---
 sound/pci/oxygen/xonar_dg.c |   88 ++++++++++++++++---------------------------
 1 file changed, 33 insertions(+), 55 deletions(-)
sound/pci/oxygen/xonar_dg.c +33 -55
index 0a8ebe1..d6a8c72 100644
@@ -147,50 +147,31 @@ static void cs4245_write_cached(struct oxygen *chip, unsigned int reg, u8 value)
 		cs4245_write(chip, reg, value);
 }
 
-static void cs4245_registers_init(struct oxygen *chip)
-{
-	struct dg *data = chip->model_data;
-
-	cs4245_write(chip, CS4245_POWER_CTRL, CS4245_PDN);
-	cs4245_write(chip, CS4245_DAC_CTRL_1,
-		     data->cs4245_regs[CS4245_DAC_CTRL_1]);
-	cs4245_write(chip, CS4245_ADC_CTRL,
-		     data->cs4245_regs[CS4245_ADC_CTRL]);
-	cs4245_write(chip, CS4245_SIGNAL_SEL,
-		     data->cs4245_regs[CS4245_SIGNAL_SEL]);
-	cs4245_write(chip, CS4245_PGA_B_CTRL,
-		     data->cs4245_regs[CS4245_PGA_B_CTRL]);
-	cs4245_write(chip, CS4245_PGA_A_CTRL,
-		     data->cs4245_regs[CS4245_PGA_A_CTRL]);
-	cs4245_write(chip, CS4245_ANALOG_IN,
-		     data->cs4245_regs[CS4245_ANALOG_IN]);
-	cs4245_write(chip, CS4245_DAC_A_CTRL,
-		     data->cs4245_regs[CS4245_DAC_A_CTRL]);
-	cs4245_write(chip, CS4245_DAC_B_CTRL,
-		     data->cs4245_regs[CS4245_DAC_B_CTRL]);
-	cs4245_write(chip, CS4245_DAC_CTRL_2,
-		     CS4245_DAC_SOFT | CS4245_DAC_ZERO | CS4245_INVERT_DAC);
-	cs4245_write(chip, CS4245_INT_MASK, 0);
-	cs4245_write(chip, CS4245_POWER_CTRL, 0);
-}
-
 static void cs4245_init(struct oxygen *chip)
 {
 	struct dg *data = chip->model_data;
 
-	data->cs4245_regs[CS4245_DAC_CTRL_1] =
-		CS4245_DAC_FM_SINGLE | CS4245_DAC_DIF_LJUST;
-	data->cs4245_regs[CS4245_ADC_CTRL] =
-		CS4245_ADC_FM_SINGLE | CS4245_ADC_DIF_LJUST;
-	data->cs4245_regs[CS4245_SIGNAL_SEL] =
-		CS4245_A_OUT_SEL_HIZ | CS4245_ASYNCH;
-	data->cs4245_regs[CS4245_PGA_B_CTRL] = 0;
-	data->cs4245_regs[CS4245_PGA_A_CTRL] = 0;
-	data->cs4245_regs[CS4245_ANALOG_IN] =
-		CS4245_PGA_SOFT | CS4245_PGA_ZERO | CS4245_SEL_INPUT_4;
-	data->cs4245_regs[CS4245_DAC_A_CTRL] = 0;
-	data->cs4245_regs[CS4245_DAC_B_CTRL] = 0;
-	cs4245_registers_init(chip);
+	/* Save the initial state: codec version, registers */
+	cs4245_shadow_control(chip, CS4245_SAVE_TO_SHADOW);
+
+	/*
+	 * Power up the CODEC internals, enable soft ramp & zero cross, work
+	 * in async. mode,
+	 * Enable aux output from DAC. Invert DAC output as in the Windows
+	 * driver
+	 */
+	data->cs4245_shadow[CS4245_POWER_CTRL] = 0;
+	data->cs4245_shadow[CS4245_SIGNAL_SEL] = CS4245_A_OUT_SEL_DAC |
+		CS4245_ASYNCH;
+	data->cs4245_shadow[CS4245_DAC_CTRL_1] = 0;
+	data->cs4245_shadow[CS4245_DAC_CTRL_2] = CS4245_DAC_SOFT |
+		CS4245_DAC_ZERO | CS4245_INVERT_DAC;
+	data->cs4245_shadow[CS4245_ADC_CTRL] = 0;
+	data->cs4245_shadow[CS4245_ANALOG_IN] = CS4245_PGA_SOFT |
+		CS4245_PGA_ZERO;
+
+	cs4245_shadow_control(chip, CS4245_LOAD_FROM_SHADOW);
+
 	snd_component_add(chip->card, "CS4245");
 }
 
@@ -200,23 +181,20 @@ static void dg_output_enable(struct oxygen *chip)
 	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
 }
 
-static void dg_init(struct oxygen *chip)
+void dg_init(struct oxygen *chip)
 {
-	struct dg *data = chip->model_data;
-
-	data->output_sel = 0;
-	data->input_sel = 3;
-	data->hp_vol_att = 2 * 16;
+	/*
+	 * The pin XGPIO1 as XGPIO1, gpios 8,7,6,5,1,0 as outputs.
+	 */
 
 	cs4245_init(chip);
-
-	oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL,
-			    GPIO_MAGIC | GPIO_HP_DETECT);
-	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
-			  GPIO_INPUT_ROUTE | GPIO_HP_REAR | GPIO_OUTPUT_ENABLE);
-	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
-			    GPIO_INPUT_ROUTE | GPIO_HP_REAR);
-	dg_output_enable(chip);
+	oxygen_write16(chip, OXYGEN_GPIO_CONTROL, GPIO_OUTPUT_ENABLE |
+		GPIO_HP_REAR | GPIO_INPUT_ROUTE);
+	oxygen_write16(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR | GPIO_INPUT_ROUTE);
+	/* Anti-pop delay */
+	msleep(2500);
+	oxygen_write16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE |
+		GPIO_HP_REAR | GPIO_INPUT_ROUTE);
 }
 
 static void dg_cleanup(struct oxygen *chip)
@@ -231,7 +209,7 @@ static void dg_suspend(struct oxygen *chip)
 
 static void dg_resume(struct oxygen *chip)
 {
-	cs4245_registers_init(chip);
+	/*cs4245_registers_init(chip);*/
 	dg_output_enable(chip);
 }
 
Signature · 1 line
1.7.10.4
maintainer
#11 lore raw

Re: [PATCH v3 09/16] ALSA: Oxygen: Modify initialization functions

Quoted 23 lines · Roman Volkov

Changes in GPIO values and sequence at startup, CS4245 initialization changed and simplified, according to datasheets and reverse-engineering information.

static void cs4245_init(struct oxygen *chip)
{

sound/pci/oxygen/xonar_dg.c +2 -13
 	struct dg *data = chip->model_data;
 
-	data->cs4245_regs[CS4245_DAC_CTRL_1] =
-		CS4245_DAC_FM_SINGLE | CS4245_DAC_DIF_LJUST;
-	data->cs4245_regs[CS4245_ADC_CTRL] =
-		CS4245_ADC_FM_SINGLE | CS4245_ADC_DIF_LJUST;
-	data->cs4245_regs[CS4245_SIGNAL_SEL] =
-		CS4245_A_OUT_SEL_HIZ | CS4245_ASYNCH;
-	data->cs4245_regs[CS4245_PGA_B_CTRL] = 0;
-	data->cs4245_regs[CS4245_PGA_A_CTRL] = 0;
-	data->cs4245_regs[CS4245_ANALOG_IN] =
-		CS4245_PGA_SOFT | CS4245_PGA_ZERO | CS4245_SEL_INPUT_4;
-	data->cs4245_regs[CS4245_DAC_A_CTRL] = 0;
-	data->cs4245_regs[CS4245_DAC_B_CTRL] = 0;
-	cs4245_registers_init(chip);
+	/* Save the initial state: codec version, registers */
+	cs4245_shadow_control(chip, CS4245_SAVE_TO_SHADOW);

sound/pci/oxygen/xonar_dg.c static void cs4245_write_cached(struct oxygen *chip, unsigned int reg, u8 value) · line 171

Quoted 10 lines
sound/pci/oxygen/xonar_dg.c +10 -0
+
+	data->cs4245_shadow[CS4245_POWER_CTRL] = 0;
+	data->cs4245_shadow[CS4245_SIGNAL_SEL] = CS4245_A_OUT_SEL_DAC |
+		CS4245_ASYNCH;
+	data->cs4245_shadow[CS4245_DAC_CTRL_1] = 0;
+	data->cs4245_shadow[CS4245_DAC_CTRL_2] = CS4245_DAC_SOFT |
+		CS4245_DAC_ZERO | CS4245_INVERT_DAC;
+	data->cs4245_shadow[CS4245_ADC_CTRL] = 0;
+	data->cs4245_shadow[CS4245_ANALOG_IN] = CS4245_PGA_SOFT |
+		CS4245_PGA_ZERO;

Please don't remove symbols like "CS4245_DAC_FM_SINGLE | CS4245_DAC_DIF_LJUST"; this would imply that you don't care about these settings.

Why don't you initialize registers like PGA/DAC_A/B?

sound/pci/oxygen/xonar_dg.c static void dg_output_enable(struct oxygen *chip) · line 209

Quoted 5 lines
sound/pci/oxygen/xonar_dg.c +1 -3
+void dg_init(struct oxygen *chip)
 {
-	data->output_sel = 0;
-	data->input_sel = 3;
-	data->hp_vol_att = 2 * 16;

These initializations should not be removed until all the other code that uses them is also removed.

sound/pci/oxygen/xonar_dg.c static void dg_output_enable(struct oxygen *chip) · line 193

Quoted 3 lines
sound/pci/oxygen/xonar_dg.c +1 -2
-	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
-			    GPIO_INPUT_ROUTE | GPIO_HP_REAR);
+	oxygen_write16(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR | GPIO_INPUT_ROUTE);

Why are these bits now initialized to the opposite value?

Regards,
Clemens

#12 lore raw

Re: [PATCH v3 09/16] ALSA: Oxygen: Modify initialization functions

В Sat, 18 Jan 2014 11:50:52 +0100
Clemens Ladisch <[email protected]> пишет:

Quoted 32 lines
Quoted 31 lines · Roman Volkov

Changes in GPIO values and sequence at startup, CS4245 initialization changed and simplified, according to datasheets and reverse-engineering information.

static void cs4245_init(struct oxygen *chip)
{

sound/pci/oxygen/xonar_dg.c +0 -10
 	struct dg *data = chip->model_data;
 
-	data->cs4245_regs[CS4245_DAC_CTRL_1] =
-		CS4245_DAC_FM_SINGLE | CS4245_DAC_DIF_LJUST;
-	data->cs4245_regs[CS4245_ADC_CTRL] =
-		CS4245_ADC_FM_SINGLE | CS4245_ADC_DIF_LJUST;
-	data->cs4245_regs[CS4245_SIGNAL_SEL] =
-		CS4245_A_OUT_SEL_HIZ | CS4245_ASYNCH;
-	data->cs4245_regs[CS4245_PGA_B_CTRL] = 0;
-	data->cs4245_regs[CS4245_PGA_A_CTRL] = 0;
-	data->cs4245_regs[CS4245_ANALOG_IN] =
-		CS4245_PGA_SOFT | CS4245_PGA_ZERO |

CS4245_SEL_INPUT_4;

sound/pci/oxygen/xonar_dg.c +5 -3
-	data->cs4245_regs[CS4245_DAC_A_CTRL] = 0;
-	data->cs4245_regs[CS4245_DAC_B_CTRL] = 0;
-	cs4245_registers_init(chip);
+	/* Save the initial state: codec version, registers */
+	cs4245_shadow_control(chip, CS4245_SAVE_TO_SHADOW);
+
+	data->cs4245_shadow[CS4245_POWER_CTRL] = 0;
+	data->cs4245_shadow[CS4245_SIGNAL_SEL] =

CS4245_A_OUT_SEL_DAC |

sound/pci/oxygen/xonar_dg.c +3 -0
+		CS4245_ASYNCH;
+	data->cs4245_shadow[CS4245_DAC_CTRL_1] = 0;
+	data->cs4245_shadow[CS4245_DAC_CTRL_2] = CS4245_DAC_SOFT |
Quoted 8 lines

sound/pci/oxygen/xonar_dg.c static void cs4245_write_cached(struct oxygen *chip, unsigned int reg, u8 value) · line 171

Quoted 4 lines
sound/pci/oxygen/xonar_dg.c +4 -0
+		CS4245_DAC_ZERO | CS4245_INVERT_DAC;
+	data->cs4245_shadow[CS4245_ADC_CTRL] = 0;
+	data->cs4245_shadow[CS4245_ANALOG_IN] = CS4245_PGA_SOFT |
+		CS4245_PGA_ZERO;

Please don't remove symbols like "CS4245_DAC_FM_SINGLE | CS4245_DAC_DIF_LJUST"; this would imply that you don't care about these settings.

Okay, will remain these settings. It is better to configure them manually.

Quoted 1 line

Why don't you initialize registers like PGA/DAC_A/B?

If you meant volume control, the mixer performs initialization of these settings. Probably they can be initalized to 30% volume, 100% by default.

Quoted 7 lines
Quoted 4 lines
sound/pci/oxygen/xonar_dg.c +1 -2
-	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA,
-			    GPIO_INPUT_ROUTE | GPIO_HP_REAR);
+	oxygen_write16(chip, OXYGEN_GPIO_DATA, GPIO_HP_REAR |

GPIO_INPUT_ROUTE);

Why are these bits now initialized to the opposite value?

As I can recall, if GPIOs 5,6 are cleared, the audio input will be inactive and we can't record anything. HP_REAR switches CS4245 to the rear jack. I tried any sequences to minimize clicks\pops. Will try to don't touch the GPIO_HP_REAR, perhaps this reduce the number of relay switches at startup.

Quoted 4 lines

Regards,
Clemens

Signature · 7 lines
Kind regards,
Roman Volkov,
[email protected]
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#13 lore raw

PATCH v3 10/16 ALSA: Oxygen: Modify DAC/ADC parameters function

In reply to Roman Volkov

When selecting the audio sample rate for CS4245,
the MCLK divider should also be changed.

---
 sound/pci/oxygen/xonar_dg.c |   62 ++++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 22 deletions(-)
sound/pci/oxygen/xonar_dg.c +40 -22
index d6a8c72..7dcca01 100644
@@ -213,36 +213,54 @@ static void dg_resume(struct oxygen *chip)
 	dg_output_enable(chip);
 }
 
-static void set_cs4245_dac_params(struct oxygen *chip,
+void set_cs4245_dac_params(struct oxygen *chip,
 				  struct snd_pcm_hw_params *params)
 {
 	struct dg *data = chip->model_data;
-	u8 value;
-
-	value = data->cs4245_regs[CS4245_DAC_CTRL_1] & ~CS4245_DAC_FM_MASK;
-	if (params_rate(params) <= 50000)
-		value |= CS4245_DAC_FM_SINGLE;
-	else if (params_rate(params) <= 100000)
-		value |= CS4245_DAC_FM_DOUBLE;
-	else
-		value |= CS4245_DAC_FM_QUAD;
-	cs4245_write_cached(chip, CS4245_DAC_CTRL_1, value);
+	unsigned char dac_ctrl;
+	unsigned char mclk_freq;
+
+	dac_ctrl = data->cs4245_shadow[CS4245_DAC_CTRL_1] & ~CS4245_DAC_FM_MASK;
+	mclk_freq = data->cs4245_shadow[CS4245_MCLK_FREQ] & ~CS4245_MCLK1_MASK;
+	if (params_rate(params) <= 50000) {
+		dac_ctrl |= CS4245_DAC_FM_SINGLE;
+		mclk_freq |= CS4245_MCLK_1 << CS4245_MCLK1_SHIFT;
+	} else if (params_rate(params) <= 100000) {
+		dac_ctrl |= CS4245_DAC_FM_DOUBLE;
+		mclk_freq |= CS4245_MCLK_1 << CS4245_MCLK1_SHIFT;
+	} else {
+		dac_ctrl |= CS4245_DAC_FM_QUAD;
+		mclk_freq |= CS4245_MCLK_2 << CS4245_MCLK1_SHIFT;
+	}
+	data->cs4245_shadow[CS4245_DAC_CTRL_1] = dac_ctrl;
+	data->cs4245_shadow[CS4245_MCLK_FREQ] = mclk_freq;
+	cs4245_write_spi(chip, CS4245_DAC_CTRL_1);
+	cs4245_write_spi(chip, CS4245_MCLK_FREQ);
 }
 
-static void set_cs4245_adc_params(struct oxygen *chip,
+void set_cs4245_adc_params(struct oxygen *chip,
 				  struct snd_pcm_hw_params *params)
 {
 	struct dg *data = chip->model_data;
-	u8 value;
-
-	value = data->cs4245_regs[CS4245_ADC_CTRL] & ~CS4245_ADC_FM_MASK;
-	if (params_rate(params) <= 50000)
-		value |= CS4245_ADC_FM_SINGLE;
-	else if (params_rate(params) <= 100000)
-		value |= CS4245_ADC_FM_DOUBLE;
-	else
-		value |= CS4245_ADC_FM_QUAD;
-	cs4245_write_cached(chip, CS4245_ADC_CTRL, value);
+	unsigned char adc_ctrl;
+	unsigned char mclk_freq;
+
+	adc_ctrl = data->cs4245_shadow[CS4245_ADC_CTRL] & ~CS4245_ADC_FM_MASK;
+	mclk_freq = data->cs4245_shadow[CS4245_MCLK_FREQ] & ~CS4245_MCLK2_MASK;
+	if (params_rate(params) <= 50000) {
+		adc_ctrl |= CS4245_ADC_FM_SINGLE;
+		mclk_freq |= CS4245_MCLK_1 << CS4245_MCLK2_SHIFT;
+	} else if (params_rate(params) <= 100000) {
+		adc_ctrl |= CS4245_ADC_FM_DOUBLE;
+		mclk_freq |= CS4245_MCLK_1 << CS4245_MCLK2_SHIFT;
+	} else {
+		adc_ctrl |= CS4245_ADC_FM_QUAD;
+		mclk_freq |= CS4245_MCLK_2 << CS4245_MCLK2_SHIFT;
+	}
+	data->cs4245_shadow[CS4245_ADC_CTRL] = adc_ctrl;
+	data->cs4245_shadow[CS4245_MCLK_FREQ] = mclk_freq;
+	cs4245_write_spi(chip, CS4245_ADC_CTRL);
+	cs4245_write_spi(chip, CS4245_MCLK_FREQ);
 }
 
 static inline unsigned int shift_bits(unsigned int value,
Signature · 1 line
1.7.10.4
#14 lore raw

PATCH v3 11/16 ALSA: Oxygen: Modify adjust_dg_dac_routing function

In reply to Roman Volkov

When selecting the audio output destinations (headphones, FP headphones, multichannel output), the channel routing should be changed depending on what destination selected. Also unnecessary I2S channels are digitally muted. This function called when the user selects the destination in the ALSA mixer.

---
 sound/pci/oxygen/xonar_dg.c |   35 +++++++++++++++++++++--------------
 sound/pci/oxygen/xonar_dg.h |    2 ++
 2 files changed, 23 insertions(+), 14 deletions(-)
sound/pci/oxygen/xonar_dg.c +21 -14
index 7dcca01..3f3201e 100644
@@ -274,22 +274,29 @@ static inline unsigned int shift_bits(unsigned int value,
 		return (value >> (shift_from - shift_to)) & mask;
 }
 
-static unsigned int adjust_dg_dac_routing(struct oxygen *chip,
+unsigned int adjust_dg_dac_routing(struct oxygen *chip,
 					  unsigned int play_routing)
 {
-	return (play_routing & OXYGEN_PLAY_DAC0_SOURCE_MASK) |
-	       shift_bits(play_routing,
-			  OXYGEN_PLAY_DAC2_SOURCE_SHIFT,
-			  OXYGEN_PLAY_DAC1_SOURCE_SHIFT,
-			  OXYGEN_PLAY_DAC1_SOURCE_MASK) |
-	       shift_bits(play_routing,
-			  OXYGEN_PLAY_DAC1_SOURCE_SHIFT,
-			  OXYGEN_PLAY_DAC2_SOURCE_SHIFT,
-			  OXYGEN_PLAY_DAC2_SOURCE_MASK) |
-	       shift_bits(play_routing,
-			  OXYGEN_PLAY_DAC0_SOURCE_SHIFT,
-			  OXYGEN_PLAY_DAC3_SOURCE_SHIFT,
-			  OXYGEN_PLAY_DAC3_SOURCE_MASK);
+	struct dg *data = chip->model_data;
+	unsigned int routing = 0;
+
+	switch (data->pcm_output) {
+	case PLAYBACK_DST_HP:
+	case PLAYBACK_DST_HP_FP:
+		oxygen_write8_masked(chip, OXYGEN_PLAY_ROUTING,
+			OXYGEN_PLAY_MUTE23 | OXYGEN_PLAY_MUTE45 |
+			OXYGEN_PLAY_MUTE67, OXYGEN_PLAY_MUTE_MASK);
+		break;
+	case PLAYBACK_DST_MULTICH:
+		routing = (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
+		(2 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
+		(1 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
+		(0 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT);
+		oxygen_write8_masked(chip, OXYGEN_PLAY_ROUTING,
+			OXYGEN_PLAY_MUTE01, OXYGEN_PLAY_MUTE_MASK);
+		break;
+	}
+	return routing;
 }
 
 static int output_switch_info(struct snd_kcontrol *ctl,
sound/pci/oxygen/xonar_dg.h +2 -0
index f861a4c..844a67b 100644
@@ -26,6 +26,8 @@ enum cs4245_shadow_operation {
 struct dg {
 	/* The shadow copy of the CS4245 register space */
 	unsigned char cs4245_shadow[17];
+	/* Output select: headphone/speakers */
+	unsigned char pcm_output;
 	unsigned int output_sel;
 	s8 input_vol[4][2];
 	unsigned int input_sel;
Signature · 1 line
1.7.10.4
#15 lore raw

PATCH v3 12/16 ALSA: Oxygen: Modify CS4245 register dumping function

In reply to Roman Volkov

Change the function to read the data from the new
shadow buffer.

---
 sound/pci/oxygen/xonar_dg.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
sound/pci/oxygen/xonar_dg.c +6 -5
index 3f3201e..953ef09 100644
@@ -625,15 +625,16 @@ static int dg_mixer_init(struct oxygen *chip)
 	return 0;
 }
 
-static void dump_cs4245_registers(struct oxygen *chip,
-				  struct snd_info_buffer *buffer)
+void dump_cs4245_registers(struct oxygen *chip,
+				struct snd_info_buffer *buffer)
 {
 	struct dg *data = chip->model_data;
-	unsigned int i;
+	unsigned int addr;
 
 	snd_iprintf(buffer, "\nCS4245:");
-	for (i = 1; i <= 0x10; ++i)
-		snd_iprintf(buffer, " %02x", data->cs4245_regs[i]);
+	cs4245_read_spi(chip, CS4245_INT_STATUS);
+	for (addr = 1; addr < ARRAY_SIZE(data->cs4245_shadow); addr++)
+		snd_iprintf(buffer, " %02x", data->cs4245_shadow[addr]);
 	snd_iprintf(buffer, "\n");
 }
 
Signature · 1 line
1.7.10.4
#16 lore raw

PATCH v3 13/16 ALSA: Oxygen: Modify suspend, resume, and cleanup functions

In reply to Roman Volkov

When putting the card into suspended mode, just disable the audio output. There is no need to do more actions, since PCI slot will be already disabled (AFAIK). When resume, we need to restore erased registers of CS4245 from the memory. When cleanup, just do the same as in suspend.

---
 sound/pci/oxygen/xonar_dg.c |   13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
sound/pci/oxygen/xonar_dg.c +7 -6
index 953ef09..80fc099 100644
@@ -197,20 +197,21 @@ void dg_init(struct oxygen *chip)
 		GPIO_HP_REAR | GPIO_INPUT_ROUTE);
 }
 
-static void dg_cleanup(struct oxygen *chip)
+void dg_suspend(struct oxygen *chip)
 {
 	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
 }
 
-static void dg_suspend(struct oxygen *chip)
+void dg_resume(struct oxygen *chip)
 {
-	dg_cleanup(chip);
+	cs4245_shadow_control(chip, CS4245_LOAD_FROM_SHADOW);
+	msleep(2500);
+	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
 }
 
-static void dg_resume(struct oxygen *chip)
+void dg_cleanup(struct oxygen *chip)
 {
-	/*cs4245_registers_init(chip);*/
-	dg_output_enable(chip);
+	dg_suspend(chip);
 }
 
 void set_cs4245_dac_params(struct oxygen *chip,
Signature · 1 line
1.7.10.4
#17 lore raw

PATCH v3 14/16 ALSA: Oxygen: Remove code from the xonar_dg.c file

In reply to Roman Volkov

The mixer code and other system structures will come to the separate file. Some unused code also removed.

---
 sound/pci/oxygen/xonar_dg.c |  393 -------------------------------------------
 1 file changed, 393 deletions(-)
sound/pci/oxygen/xonar_dg.c +0 -393
index 80fc099..b0dc16e 100644
@@ -124,29 +124,6 @@ int cs4245_shadow_control(struct oxygen *chip, enum cs4245_shadow_operation op)
 	return 0;
 }
 
-static void cs4245_write(struct oxygen *chip, unsigned int reg, u8 value)
-{
-	struct dg *data = chip->model_data;
-
-	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
-			 OXYGEN_SPI_DATA_LENGTH_3 |
-			 OXYGEN_SPI_CLOCK_1280 |
-			 (0 << OXYGEN_SPI_CODEC_SHIFT) |
-			 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
-			 CS4245_SPI_ADDRESS |
-			 CS4245_SPI_WRITE |
-			 (reg << 8) | value);
-	data->cs4245_regs[reg] = value;
-}
-
-static void cs4245_write_cached(struct oxygen *chip, unsigned int reg, u8 value)
-{
-	struct dg *data = chip->model_data;
-
-	if (value != data->cs4245_regs[reg])
-		cs4245_write(chip, reg, value);
-}
-
 static void cs4245_init(struct oxygen *chip)
 {
 	struct dg *data = chip->model_data;
@@ -175,12 +152,6 @@ static void cs4245_init(struct oxygen *chip)
 	snd_component_add(chip->card, "CS4245");
 }
 
-static void dg_output_enable(struct oxygen *chip)
-{
-	msleep(2500);
-	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
-}
-
 void dg_init(struct oxygen *chip)
 {
 	/*
@@ -264,17 +235,6 @@ void set_cs4245_adc_params(struct oxygen *chip,
 	cs4245_write_spi(chip, CS4245_MCLK_FREQ);
 }
 
-static inline unsigned int shift_bits(unsigned int value,
-				      unsigned int shift_from,
-				      unsigned int shift_to,
-				      unsigned int mask)
-{
-	if (shift_from < shift_to)
-		return (value << (shift_to - shift_from)) & mask;
-	else
-		return (value >> (shift_from - shift_to)) & mask;
-}
-
 unsigned int adjust_dg_dac_routing(struct oxygen *chip,
 					  unsigned int play_routing)
 {
@@ -300,332 +260,6 @@ unsigned int adjust_dg_dac_routing(struct oxygen *chip,
 	return routing;
 }
 
-static int output_switch_info(struct snd_kcontrol *ctl,
-			      struct snd_ctl_elem_info *info)
-{
-	static const char *const names[3] = {
-		"Speakers", "Headphones", "FP Headphones"
-	};
-
-	return snd_ctl_enum_info(info, 1, 3, names);
-}
-
-static int output_switch_get(struct snd_kcontrol *ctl,
-			     struct snd_ctl_elem_value *value)
-{
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-
-	mutex_lock(&chip->mutex);
-	value->value.enumerated.item[0] = data->output_sel;
-	mutex_unlock(&chip->mutex);
-	return 0;
-}
-
-static int output_switch_put(struct snd_kcontrol *ctl,
-			     struct snd_ctl_elem_value *value)
-{
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-	u8 reg;
-	int changed;
-
-	if (value->value.enumerated.item[0] > 2)
-		return -EINVAL;
-
-	mutex_lock(&chip->mutex);
-	changed = value->value.enumerated.item[0] != data->output_sel;
-	if (changed) {
-		data->output_sel = value->value.enumerated.item[0];
-
-		reg = data->cs4245_regs[CS4245_SIGNAL_SEL] &
-						~CS4245_A_OUT_SEL_MASK;
-		reg |= data->output_sel == 2 ?
-				CS4245_A_OUT_SEL_DAC : CS4245_A_OUT_SEL_HIZ;
-		cs4245_write_cached(chip, CS4245_SIGNAL_SEL, reg);
-
-		cs4245_write_cached(chip, CS4245_DAC_A_CTRL,
-				    data->output_sel ? data->hp_vol_att : 0);
-		cs4245_write_cached(chip, CS4245_DAC_B_CTRL,
-				    data->output_sel ? data->hp_vol_att : 0);
-
-		oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
-				      data->output_sel == 1 ? GPIO_HP_REAR : 0,
-				      GPIO_HP_REAR);
-	}
-	mutex_unlock(&chip->mutex);
-	return changed;
-}
-
-static int hp_volume_offset_info(struct snd_kcontrol *ctl,
-				 struct snd_ctl_elem_info *info)
-{
-	static const char *const names[3] = {
-		"< 64 ohms", "64-150 ohms", "150-300 ohms"
-	};
-
-	return snd_ctl_enum_info(info, 1, 3, names);
-}
-
-static int hp_volume_offset_get(struct snd_kcontrol *ctl,
-				struct snd_ctl_elem_value *value)
-{
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-
-	mutex_lock(&chip->mutex);
-	if (data->hp_vol_att > 2 * 7)
-		value->value.enumerated.item[0] = 0;
-	else if (data->hp_vol_att > 0)
-		value->value.enumerated.item[0] = 1;
-	else
-		value->value.enumerated.item[0] = 2;
-	mutex_unlock(&chip->mutex);
-	return 0;
-}
-
-static int hp_volume_offset_put(struct snd_kcontrol *ctl,
-				struct snd_ctl_elem_value *value)
-{
-	static const s8 atts[3] = { 2 * 16, 2 * 7, 0 };
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-	s8 att;
-	int changed;
-
-	if (value->value.enumerated.item[0] > 2)
-		return -EINVAL;
-	att = atts[value->value.enumerated.item[0]];
-	mutex_lock(&chip->mutex);
-	changed = att != data->hp_vol_att;
-	if (changed) {
-		data->hp_vol_att = att;
-		if (data->output_sel) {
-			cs4245_write_cached(chip, CS4245_DAC_A_CTRL, att);
-			cs4245_write_cached(chip, CS4245_DAC_B_CTRL, att);
-		}
-	}
-	mutex_unlock(&chip->mutex);
-	return changed;
-}
-
-static int input_vol_info(struct snd_kcontrol *ctl,
-			  struct snd_ctl_elem_info *info)
-{
-	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
-	info->count = 2;
-	info->value.integer.min = 2 * -12;
-	info->value.integer.max = 2 * 12;
-	return 0;
-}
-
-static int input_vol_get(struct snd_kcontrol *ctl,
-			 struct snd_ctl_elem_value *value)
-{
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-	unsigned int idx = ctl->private_value;
-
-	mutex_lock(&chip->mutex);
-	value->value.integer.value[0] = data->input_vol[idx][0];
-	value->value.integer.value[1] = data->input_vol[idx][1];
-	mutex_unlock(&chip->mutex);
-	return 0;
-}
-
-static int input_vol_put(struct snd_kcontrol *ctl,
-			 struct snd_ctl_elem_value *value)
-{
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-	unsigned int idx = ctl->private_value;
-	int changed = 0;
-
-	if (value->value.integer.value[0] < 2 * -12 ||
-	    value->value.integer.value[0] > 2 * 12 ||
-	    value->value.integer.value[1] < 2 * -12 ||
-	    value->value.integer.value[1] > 2 * 12)
-		return -EINVAL;
-	mutex_lock(&chip->mutex);
-	changed = data->input_vol[idx][0] != value->value.integer.value[0] ||
-		  data->input_vol[idx][1] != value->value.integer.value[1];
-	if (changed) {
-		data->input_vol[idx][0] = value->value.integer.value[0];
-		data->input_vol[idx][1] = value->value.integer.value[1];
-		if (idx == data->input_sel) {
-			cs4245_write_cached(chip, CS4245_PGA_A_CTRL,
-					    data->input_vol[idx][0]);
-			cs4245_write_cached(chip, CS4245_PGA_B_CTRL,
-					    data->input_vol[idx][1]);
-		}
-	}
-	mutex_unlock(&chip->mutex);
-	return changed;
-}
-
-static DECLARE_TLV_DB_SCALE(cs4245_pga_db_scale, -1200, 50, 0);
-
-static int input_sel_info(struct snd_kcontrol *ctl,
-			  struct snd_ctl_elem_info *info)
-{
-	static const char *const names[4] = {
-		"Mic", "Aux", "Front Mic", "Line"
-	};
-
-	return snd_ctl_enum_info(info, 1, 4, names);
-}
-
-static int input_sel_get(struct snd_kcontrol *ctl,
-			 struct snd_ctl_elem_value *value)
-{
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-
-	mutex_lock(&chip->mutex);
-	value->value.enumerated.item[0] = data->input_sel;
-	mutex_unlock(&chip->mutex);
-	return 0;
-}
-
-static int input_sel_put(struct snd_kcontrol *ctl,
-			 struct snd_ctl_elem_value *value)
-{
-	static const u8 sel_values[4] = {
-		CS4245_SEL_MIC,
-		CS4245_SEL_INPUT_1,
-		CS4245_SEL_INPUT_2,
-		CS4245_SEL_INPUT_4
-	};
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-	int changed;
-
-	if (value->value.enumerated.item[0] > 3)
-		return -EINVAL;
-
-	mutex_lock(&chip->mutex);
-	changed = value->value.enumerated.item[0] != data->input_sel;
-	if (changed) {
-		data->input_sel = value->value.enumerated.item[0];
-
-		cs4245_write(chip, CS4245_ANALOG_IN,
-			     (data->cs4245_regs[CS4245_ANALOG_IN] &
-							~CS4245_SEL_MASK) |
-			     sel_values[data->input_sel]);
-
-		cs4245_write_cached(chip, CS4245_PGA_A_CTRL,
-				    data->input_vol[data->input_sel][0]);
-		cs4245_write_cached(chip, CS4245_PGA_B_CTRL,
-				    data->input_vol[data->input_sel][1]);
-
-		oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
-				      data->input_sel ? 0 : GPIO_INPUT_ROUTE,
-				      GPIO_INPUT_ROUTE);
-	}
-	mutex_unlock(&chip->mutex);
-	return changed;
-}
-
-static int hpf_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
-{
-	static const char *const names[2] = { "Active", "Frozen" };
-
-	return snd_ctl_enum_info(info, 1, 2, names);
-}
-
-static int hpf_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
-{
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-
-	value->value.enumerated.item[0] =
-		!!(data->cs4245_regs[CS4245_ADC_CTRL] & CS4245_HPF_FREEZE);
-	return 0;
-}
-
-static int hpf_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
-{
-	struct oxygen *chip = ctl->private_data;
-	struct dg *data = chip->model_data;
-	u8 reg;
-	int changed;
-
-	mutex_lock(&chip->mutex);
-	reg = data->cs4245_regs[CS4245_ADC_CTRL] & ~CS4245_HPF_FREEZE;
-	if (value->value.enumerated.item[0])
-		reg |= CS4245_HPF_FREEZE;
-	changed = reg != data->cs4245_regs[CS4245_ADC_CTRL];
-	if (changed)
-		cs4245_write(chip, CS4245_ADC_CTRL, reg);
-	mutex_unlock(&chip->mutex);
-	return changed;
-}
-
-#define INPUT_VOLUME(xname, index) { \
-	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
-	.name = xname, \
-	.info = input_vol_info, \
-	.get = input_vol_get, \
-	.put = input_vol_put, \
-	.tlv = { .p = cs4245_pga_db_scale }, \
-	.private_value = index, \
-}
-static const struct snd_kcontrol_new dg_controls[] = {
-	{
-		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
-		.name = "Analog Output Playback Enum",
-		.info = output_switch_info,
-		.get = output_switch_get,
-		.put = output_switch_put,
-	},
-	{
-		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
-		.name = "Headphones Impedance Playback Enum",
-		.info = hp_volume_offset_info,
-		.get = hp_volume_offset_get,
-		.put = hp_volume_offset_put,
-	},
-	INPUT_VOLUME("Mic Capture Volume", 0),
-	INPUT_VOLUME("Aux Capture Volume", 1),
-	INPUT_VOLUME("Front Mic Capture Volume", 2),
-	INPUT_VOLUME("Line Capture Volume", 3),
-	{
-		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
-		.name = "Capture Source",
-		.info = input_sel_info,
-		.get = input_sel_get,
-		.put = input_sel_put,
-	},
-	{
-		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
-		.name = "ADC High-pass Filter Capture Enum",
-		.info = hpf_info,
-		.get = hpf_get,
-		.put = hpf_put,
-	},
-};
-
-static int dg_control_filter(struct snd_kcontrol_new *template)
-{
-	if (!strncmp(template->name, "Master Playback ", 16))
-		return 1;
-	return 0;
-}
-
-static int dg_mixer_init(struct oxygen *chip)
-{
-	unsigned int i;
-	int err;
-
-	for (i = 0; i < ARRAY_SIZE(dg_controls); ++i) {
-		err = snd_ctl_add(chip->card,
-				  snd_ctl_new1(&dg_controls[i], chip));
-		if (err < 0)
-			return err;
-	}
-	return 0;
-}
-
 void dump_cs4245_registers(struct oxygen *chip,
 				struct snd_info_buffer *buffer)
 {
@@ -638,30 +272,3 @@ void dump_cs4245_registers(struct oxygen *chip,
 		snd_iprintf(buffer, " %02x", data->cs4245_shadow[addr]);
 	snd_iprintf(buffer, "\n");
 }
-
-struct oxygen_model model_xonar_dg = {
-	.longname = "C-Media Oxygen HD Audio",
-	.chip = "CMI8786",
-	.init = dg_init,
-	.control_filter = dg_control_filter,
-	.mixer_init = dg_mixer_init,
-	.cleanup = dg_cleanup,
-	.suspend = dg_suspend,
-	.resume = dg_resume,
-	.set_dac_params = set_cs4245_dac_params,
-	.set_adc_params = set_cs4245_adc_params,
-	.adjust_dac_routing = adjust_dg_dac_routing,
-	.dump_registers = dump_cs4245_registers,
-	.model_data_size = sizeof(struct dg),
-	.device_config = PLAYBACK_0_TO_I2S |
-			 PLAYBACK_1_TO_SPDIF |
-			 CAPTURE_0_FROM_I2S_2 |
-			 CAPTURE_1_FROM_SPDIF,
-	.dac_channels_pcm = 6,
-	.dac_channels_mixer = 0,
-	.function_flags = OXYGEN_FUNCTION_SPI,
-	.dac_mclks = OXYGEN_MCLKS(256, 128, 128),
-	.adc_mclks = OXYGEN_MCLKS(256, 128, 128),
-	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
-	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
-};
Signature · 1 line
1.7.10.4
maintainer
#18 lore raw

Re: [PATCH v3 14/16] ALSA: Oxygen: Remove code from the xonar_dg.c file

Quoted 5 lines · Roman Volkov

The mixer code and other system structures will come to the separate file.

 sound/pci/oxygen/xonar_dg.c |  393 -------------------------------------------
 1 file changed, 393 deletions(-)

This change breaks the driver; you need to merge patches 14..16.

Regards,
Clemens

#19 lore raw

PATCH v3 15/16 ALSA: Oxygen: Additional declarations for the new mixer implementation

In reply to Roman Volkov

The 'dg' struct changed, contains new values for the mixer controls. Functions declared in the header file to call them from the mixer.

---
 sound/pci/oxygen/xonar_dg.h |   28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
sound/pci/oxygen/xonar_dg.h +23 -5
index 844a67b..636890a 100644
@@ -28,13 +28,31 @@ struct dg {
 	unsigned char cs4245_shadow[17];
 	/* Output select: headphone/speakers */
 	unsigned char pcm_output;
-	unsigned int output_sel;
-	s8 input_vol[4][2];
-	unsigned int input_sel;
-	u8 hp_vol_att;
-	u8 cs4245_regs[0x11];
+	/* Input select: Mic, FP Mic, Line, Aux */
+	unsigned char pga_source;
+	/* Independent capture volume control values */
+	char cap_vol[4][2];
+	/* Independent mute control values */
+	bool cap_mute[4];
 };
 
+/* Xonar DG control routines */
+int cs4245_write_spi(struct oxygen *chip, u8 reg);
+int cs4245_read_spi(struct oxygen *chip, u8 reg);
+int cs4245_shadow_control(struct oxygen *chip, enum cs4245_shadow_operation op);
+void dg_init(struct oxygen *chip);
+void set_cs4245_dac_params(struct oxygen *chip,
+				  struct snd_pcm_hw_params *params);
+void set_cs4245_adc_params(struct oxygen *chip,
+				  struct snd_pcm_hw_params *params);
+unsigned int adjust_dg_dac_routing(struct oxygen *chip,
+					  unsigned int play_routing);
+void dump_cs4245_registers(struct oxygen *chip,
+				struct snd_info_buffer *buffer);
+void dg_suspend(struct oxygen *chip);
+void dg_resume(struct oxygen *chip);
+void dg_cleanup(struct oxygen *chip);
+
 extern struct oxygen_model model_xonar_dg;
 
 #endif
Signature · 1 line
1.7.10.4
#20 lore raw

PATCH v3 16/16 ALSA: Oxygen: The new mixer implementation

In reply to Roman Volkov

The mixer changed. In the new implementation, the headphone volume changes instead of having the 'impedance control'. This allows to use headphones without builtin volume control and without software volume control. Four input sources with independent mute and volume controls, but only one source can be selected.

---
 sound/pci/oxygen/Makefile         |    2 +-
 sound/pci/oxygen/xonar_dg_mixer.c |  483 +++++++++++++++++++++++++++++++++++++
 2 files changed, 484 insertions(+), 1 deletion(-)
 create mode 100644 sound/pci/oxygen/xonar_dg_mixer.c
sound/pci/oxygen/Makefile +1 -1
index 0f87265..8f4c409 100644
@@ -1,5 +1,5 @@
 snd-oxygen-lib-objs := oxygen_io.o oxygen_lib.o oxygen_mixer.o oxygen_pcm.o
-snd-oxygen-objs := oxygen.o xonar_dg.o
+snd-oxygen-objs := oxygen.o xonar_dg_mixer.o xonar_dg.o
 snd-virtuoso-objs := virtuoso.o xonar_lib.o \
 	xonar_pcm179x.o xonar_cs43xx.o xonar_wm87x6.o xonar_hdmi.o
 
sound/pci/oxygen/xonar_dg_mixer.c new file +483 -0

483 lines not on this page · raw message

Signature · 1 line
1.7.10.4
maintainer
#21 lore raw
In reply to Roman Volkov
Quoted 3 lines · Roman Volkov

This function performs waiting when the SPI bus completes a transaction. Timeout error checking introduced and the timeout increased to 400 from 40.

Why 400? SPI does not allow the codec to delay reads.

sound/pci/oxygen/oxygen_io.c void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec, · line 207

Quoted 7 lines
sound/pci/oxygen/oxygen_io.c +7 -0
+	for (count = 100; count > 0; count--) {
+		if ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) &
+						OXYGEN_SPI_BUSY) == 0)
+			return 0;
+		udelay(4);
+		--count;
+	}

This loop waits for 200 µs.

The SPI transaction will not be finished for the first 30 µs or so, so polling is not needed for that time.

With the wait coming after the transaction, it makes more sense to have the busy check not before but after the udelay.

Regards,
Clemens

#22 lore raw

В Sat, 18 Jan 2014 11:26:49 +0100
Clemens Ladisch <[email protected]> пишет:

Quoted 5 lines
Quoted 3 lines

This function performs waiting when the SPI bus completes a transaction. Timeout error checking introduced and the timeout increased to 400 from 40.

Why 400? SPI does not allow the codec to delay reads.

Yes. AFAIK, every SPI transaction will take almost the same time, depending on the SPI clock and the number of bits to transfer, regardless of what we're performing, reading or writing.

400 us is just the maximum time, exceeding this will cause an error. Okay, lets calculate, in worst case (3 bytes to transfer, 1280ns clock):

1280 * 8 * 3 = 30270ns, 30,72 us. Perhaps plus some time to initiate the transfer.

400us is greater than 40 by 10 times, but the function never waits 400 us, it always returns earlier than in 40-50 us. I always prefer to increase these timeouts much longer than needed, this guarantees that I never get false-positives and this wait is safe. Even if the error occured, 400us is very small amount of time.

Quoted 11 lines

sound/pci/oxygen/oxygen_io.c void oxygen_write_ac97_masked(struct oxygen *chip, unsigned int codec, · line 207

Quoted 8 lines
sound/pci/oxygen/oxygen_io.c +3 -0
+	for (count = 100; count > 0; count--) {
+		if ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) &
+						OXYGEN_SPI_BUSY)

== 0)

sound/pci/oxygen/oxygen_io.c +4 -0
+			return 0;
+		udelay(4);
+		--count;
+	}  

This loop waits for 200 µs.

Ah... Thanks.
Also why I prefer higher timeouts:
http://stackoverflow.com/questions/8352812/linux-kernel-udelay-returns-too-early
Even if a mistake or a bug, this works as needed :) This driver currently works on my PC, if the test succeeded, this decreases my attention :)

Quoted 2 lines

The SPI transaction will not be finished for the first 30 µs or so, so polling is not needed for that time.

We may switch the context and force CPU to
execute useful code, using something like usleep, msleep. However, this may block the thread for too long time. Please suggest something how to optimize this.

Quoted 2 lines

With the wait coming after the transaction, it makes more sense to have the busy check not before but after the udelay.

Okay, this will be:

for (count = 100; count > 0; count--) {
	udelay(4);
	if ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) &
					OXYGEN_SPI_BUSY) == 0)
		return 0;
}
Signature · 7 lines
Kind regards,
Roman Volkov,
[email protected]
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
maintainer
#23 lore raw
Quoted 8 lines · Roman Volkov

Clemens Ladisch <[email protected]> пишет:

Quoted 2 lines

The SPI transaction will not be finished for the first 30 µs or so, so polling is not needed for that time.

We may switch the context and force CPU to
execute useful code, using something like usleep, msleep. However, this may block the thread for too long time. Please suggest something how to optimize this.

Actually, SPI transactions could use 2 bytes or a faster clock, so I don't think this needs to be optimized.

Quoted 11 lines
Quoted 2 lines

With the wait coming after the transaction, it makes more sense to have the busy check not before but after the udelay.

Okay, this will be:

for (count = 100; count > 0; count--) {
	udelay(4);
	if ((oxygen_read8(chip, OXYGEN_SPI_CONTROL) &
					OXYGEN_SPI_BUSY) == 0)
		return 0;
}

Yes.

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