Skip to contentSkip to conversation

ASoC: Codec driver for Texas Instruments tlv320dac33 codec

PATCH In review Peter Ujfalusi 3 participants 9 messages Oct 13, 2009 – Oct 15, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: Codec driver for Texas Instruments tlv320dac33 codec

Driver for Texas Instruments TLV320DAC33 (SLAS546) low power stereo audio DAC.

TLV320DAC33 is a stereo audio codec with integrated 24KB FIFO for low power audio playback.

The digital interface can use I2S, DSP (A or B), Right and Left justified formats.
DAC33 has stereo analog input, which can be bypassed to the analog outputs.

Regarding to the internal 24KB FIFO the driver implements 'FIFO bypass' mode (default) and nSample mode (FIFO is in use).
a) In 'FIFO bypass' mode the internal FIFO is not in use, the codec is working synchronously as a normal codec (it needs constant stream of data on the digital interface).

b) The nSample mode implementation uses one interrupt line from DAC33 to the host:
Alarm threshold is set to 10ms of audio data (limit by the driver implementation).
DAC33 will signal an interrupt, when the FIFO level goes under the Alarm threshold.
The host will write to nSample register a value (number of stereo samples), to tell DAC33 how many samples it should read in a burst from the host. When the DAC33 received the number of samples, it disables the clocks on the I2S bus. When the FIFO use again goes under the Alarm threshold, DAC33 signals the host with an interrupt, and the process is repeated.

---

I think I have addressed all the comments given for the first series. I might have missed something, but in short:
- Handling of mutex is reworked, so it is not taken for all I2C accesses, but still protects the chip_power variable.
- nsample_switch handling changed
- IRQ handling reworked
- Handling of the hard and soft power states are cleaner
- definitions are namespeced
- Some generic cleanups

 include/sound/tlv320dac33-plat.h |   20 +
 sound/soc/codecs/Kconfig         |    4 +
 sound/soc/codecs/Makefile        |    2 +
 sound/soc/codecs/tlv320dac33.c   | 1235 ++++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/tlv320dac33.h   |  267 ++++++++
 5 files changed, 1528 insertions(+), 0 deletions(-)
 create mode 100644 include/sound/tlv320dac33-plat.h
 create mode 100644 sound/soc/codecs/tlv320dac33.c
 create mode 100644 sound/soc/codecs/tlv320dac33.h
include/sound/tlv320dac33-plat.h new file +20 -0
new file mode 100644
index 0000000..5858d06
@@ -0,0 +1,20 @@
+/*
+ * Platform header for Texas Instruments TLV320DAC33 codec driver
+ *
+ * Author:	Peter Ujfalusi <[email protected]>
+ *
+ * Copyright:   (C) 2009 Nokia Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef __TLV320DAC33_PLAT_H
+#define __TLV320DAC33_PLAT_H
+
+struct tlv320dac33_platform_data {
+	int power_gpio;
+};
+
+#endif /* __TLV320DAC33_PLAT_H */
sound/soc/codecs/Kconfig +4 -0
index fab01c9..49bab93 100644
@@ -29,6 +29,7 @@ config SND_SOC_ALL_CODECS
 	select SND_SOC_TLV320AIC23 if I2C
 	select SND_SOC_TLV320AIC26 if SPI_MASTER
 	select SND_SOC_TLV320AIC3X if I2C
+	select SND_SOC_TLV320DAC33 if I2C
 	select SND_SOC_TPA6130A2 if I2C
 	select SND_SOC_TWL4030 if TWL4030_CORE
 	select SND_SOC_UDA134X
@@ -142,6 +143,9 @@ config SND_SOC_TLV320AIC26
 config SND_SOC_TLV320AIC3X
 	tristate
 
+config SND_SOC_TLV320DAC33
+	tristate
+
 config SND_SOC_TWL4030
 	tristate
 
sound/soc/codecs/Makefile +2 -0
index 2f14391..8f519ee 100644
@@ -17,6 +17,7 @@ snd-soc-stac9766-objs := stac9766.o
 snd-soc-tlv320aic23-objs := tlv320aic23.o
 snd-soc-tlv320aic26-objs := tlv320aic26.o
 snd-soc-tlv320aic3x-objs := tlv320aic3x.o
+snd-soc-tlv320dac33-objs := tlv320dac33.o
 snd-soc-twl4030-objs := twl4030.o
 snd-soc-uda134x-objs := uda134x.o
 snd-soc-uda1380-objs := uda1380.o
@@ -70,6 +71,7 @@ obj-$(CONFIG_SND_SOC_STAC9766)	+= snd-soc-stac9766.o
 obj-$(CONFIG_SND_SOC_TLV320AIC23)	+= snd-soc-tlv320aic23.o
 obj-$(CONFIG_SND_SOC_TLV320AIC26)	+= snd-soc-tlv320aic26.o
 obj-$(CONFIG_SND_SOC_TLV320AIC3X)	+= snd-soc-tlv320aic3x.o
+obj-$(CONFIG_SND_SOC_TLV320DAC33)	+= snd-soc-tlv320dac33.o
 obj-$(CONFIG_SND_SOC_TWL4030)	+= snd-soc-twl4030.o
 obj-$(CONFIG_SND_SOC_UDA134X)	+= snd-soc-uda134x.o
 obj-$(CONFIG_SND_SOC_UDA1380)	+= snd-soc-uda1380.o
sound/soc/codecs/tlv320dac33.c new file +1235 -0

1,235 lines not on this page · raw message

sound/soc/codecs/tlv320dac33.h new file +267 -0

267 lines not on this page · raw message

--
1.6.5

#1 lore raw
Quoted 18 lines · Ujfalusi Peter (Nokia-D/Tampere)
sound/soc/codecs/tlv320dac33.c new file +18 -0
+static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg,
+                      unsigned int value)
+{
+       struct tlv320dac33_priv *dac33 = codec->private_data;
+       u8 data[2];
+       int ret = 0;
+
+       /*
+        * data is
+        *   D15..D8 dac33 register offset
+        *   D7...D0 register data
+        */
+       data[0] = reg & 0xff;
+       data[1] = value & 0xff;
+
+       dac33_write_reg_cache(codec, data[0], data[1]);
+       mutex_lock(&dac33->mutex);
+       if (dac33->chip_power) {

sound/soc/codecs/tlv320dac33.c line 200

Quoted 10 lines
sound/soc/codecs/tlv320dac33.c new file +10 -0
+               ret = codec->hw_write(codec->control_data, data, 2);
+               if (ret != 2)
+                       dev_err(codec->dev, "Write failed (%d)\n", ret);
+               else
+                       ret = 0;
+       }
+       mutex_unlock(&dac33->mutex);
+
+       return ret;
+}

Mark, aren't all codec bits updated within the snd_soc_update_bits() by DAPM? It always takes the io_mutex. Does it make sense if we proposed a new item to struct snd_soc_codec, something like io_mutex? then snd_soc_update_bits() would check the presence of this codec->io_mutex, and if it was present, it would only use that instead of io_mutex. Just an idea... (I'd help here, no need for double locking like now)

#2 lore raw
Quoted 2 lines · Eero Nurkkala

Mark, aren't all codec bits updated within the snd_soc_update_bits() by DAPM? It always takes the io_mutex. Does it make sense if we proposed a

No, actually DAPM is bypassing that (which is probably a bug anyway but the holding of the CODEC lock helps a lot there). Note also that io_mutex is only held for bitwise updates.

Quoted 4 lines

new item to struct snd_soc_codec, something like io_mutex? then snd_soc_update_bits() would check the presence of this codec->io_mutex, and if it was present, it would only use that instead of io_mutex. Just an idea... (I'd help here, no need for double locking like now)

Much less convoluted would just be to move io_mutex into the CODEC unconditionally. However, note that snd_soc_write() doesn't take the lock - it's currently only used for read/modify/write cycles.

#3 lore raw
In reply to Peter Ujfalusi

On Tue, 2009-10-13 at 14:03 +0200, Ujfalusi Peter (Nokia-D/Tampere) wrote:

Having two almost identical functions probably "rang the bells" with you also.

How about:

static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg, unsigned int value)

{
	int ret;

	mutex_lock();
	ret = dac33_write(codec, reg, value);
	mutex_unlock();

	return ret;
}

(one way around it)

Quoted 55 lines
sound/soc/codecs/tlv320dac33.c new file +55 -0
+static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg,
+                      unsigned int value)
+{
+       struct tlv320dac33_priv *dac33 = codec->private_data;
+       u8 data[2];
+       int ret = 0;
+
+       /*
+        * data is
+        *   D15..D8 dac33 register offset
+        *   D7...D0 register data
+        */
+       data[0] = reg & 0xff;
+       data[1] = value & 0xff;
+
+       dac33_write_reg_cache(codec, data[0], data[1]);
+       mutex_lock(&dac33->mutex);
+       if (dac33->chip_power) {
+               ret = codec->hw_write(codec->control_data, data, 2);
+               if (ret != 2)
+                       dev_err(codec->dev, "Write failed (%d)\n", ret);
+               else
+                       ret = 0;
+       }
+       mutex_unlock(&dac33->mutex);
+
+       return ret;
+}
+
+static int dac33_write(struct snd_soc_codec *codec, unsigned int reg,
+                      unsigned int value)
+{
+       struct tlv320dac33_priv *dac33 = codec->private_data;
+       u8 data[2];
+       int ret = 0;
+
+       /*
+        * data is
+        *   D15..D8 dac33 register offset
+        *   D7...D0 register data
+        */
+       data[0] = reg & 0xff;
+       data[1] = value & 0xff;
+
+       dac33_write_reg_cache(codec, data[0], data[1]);
+       if (dac33->chip_power) {
+               ret = codec->hw_write(codec->control_data, data, 2);
+               if (ret != 2)
+                       dev_err(codec->dev, "Write failed (%d)\n", ret);
+               else
+                       ret = 0;
+       }
+
+       return ret;
+}
#4 lore raw
Quoted 20 lines · On Wednesday 14 October 2009 07:50:39 Nurkkala Eero.An (EXT-Offcode/Oulu) wrote:

On Tue, 2009-10-13 at 14:03 +0200, Ujfalusi Peter (Nokia-D/Tampere) wrote:

Having two almost identical functions probably "rang the bells" with you also.

How about:

static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg, unsigned int value)

{
	int ret;

	mutex_lock();
	ret = dac33_write(codec, reg, value);
	mutex_unlock();

	return ret;
}
Quoted 1 line

(one way around it)

Yes, good idea.
I'll send another patch, but I want to wait for Mark's comments first, and address his concerns as well, if he has.

Signature · 1 line
Péter
#5 lore raw
In reply to Peter Ujfalusi
Quoted 1 line · Peter Ujfalusi

b) The nSample mode implementation uses one interrupt line from DAC33 to

Just a thought, but could a timer based approach work here?

sound/soc/codecs/tlv320dac33.c line 497

Quoted 8 lines
sound/soc/codecs/tlv320dac33.c new file +8 -0
+	case SND_SOC_BIAS_PREPARE:
+		break;
+	case SND_SOC_BIAS_STANDBY:
+		dac33_soft_power(codec, 0);
+		break;
+	case SND_SOC_BIAS_OFF:
+		break;
+	}

It'd be nice to do the full power down in BIAS_OFF for future use.

sound/soc/codecs/tlv320dac33.c line 547

Quoted 10 lines
sound/soc/codecs/tlv320dac33.c new file +10 -0
+static irqreturn_t dac33_interrupt_handler(int irq, void *dev)
+{
+	struct snd_soc_codec *codec = dev;
+	struct tlv320dac33_priv *dac33 = codec->private_data;
+
+	if (dac33->state == DAC33_PLAYBACK)
+		queue_work(dac33->dac33_wq, &dac33->work);
+
+	return IRQ_HANDLED;
+}

Should that return IRQ_HANDLED if it's not expecting an interrupt? I'd also expect to see something that either acknowledges the interrupt to deassert it or keeps it masked until the work has had a chance to do something.

sound/soc/codecs/tlv320dac33.c line 1063

Quoted 1 line
sound/soc/codecs/tlv320dac33.c new file +1 -0
+	pdata = (struct tlv320dac33_platform_data *)client->dev.platform_data;

Unneeded cast.

sound/soc/codecs/tlv320dac33.c line 1110

Quoted 5 lines
sound/soc/codecs/tlv320dac33.c new file +5 -0
+	dac33->dac33_wq = create_rt_workqueue("tlv320dac33");
+	if (dac33->dac33_wq == NULL) {
+		ret = -ENOMEM;
+		goto error_wq;
+	}

Might be nice to skip the workqueue stuff if we don't have an IRQ. Conditional registration of the nSample controls based on having the IRQ may also be nice.

sound/soc/codecs/tlv320dac33.c line 1128

Quoted 12 lines
sound/soc/codecs/tlv320dac33.c new file +11 -0
+	ret = snd_soc_register_codec(codec);
+	if (ret != 0) {
+		dev_err(codec->dev, "Failed to register codec: %d\n", ret);
+		goto error_codec;
+	}
 
+	ret = snd_soc_register_dai(&dac33_dai);
+	if (ret != 0) {
+		dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
+		snd_soc_unregister_codec(codec);
+		goto error_codec;
+	}

I'd do these last so that we don't have ASoC level init trying to use the device only to have it yanked from underneath it by a failure in the following code.

#6 lore raw
Quoted 4 lines · On Wednesday 14 October 2009 14:14:10 ext Mark Brown wrote:
Quoted 1 line · Peter Ujfalusi

b) The nSample mode implementation uses one interrupt line from DAC33 to

Just a thought, but could a timer based approach work here?

Yes, there is a mode, when instead of interrupt, host side timer can be used. This mode needs slightly different way of configuration than the interrupt mode, and it has not been tested. So this is the reason, why it is not implemented in the driver.

Quoted 11 lines

sound/soc/codecs/tlv320dac33.c line 497

Quoted 9 lines
sound/soc/codecs/tlv320dac33.c new file +8 -0
+	case SND_SOC_BIAS_PREPARE:
+		break;
+	case SND_SOC_BIAS_STANDBY:
+		dac33_soft_power(codec, 0);
+		break;
+	case SND_SOC_BIAS_OFF:
+		break;
+	}

It'd be nice to do the full power down in BIAS_OFF for future use.

The full power off is taken when the dac33_soc_suspend is called (the hard_power is restored in the dac33_soc_resume function).
I can move it here, than add dac33_hard_power(codec, 1) to _PREPARE, if the previous state was _OFF.
Than I can remove the dac33_soc_suspend and dac33_soc_resume?

Quoted 13 lines

sound/soc/codecs/tlv320dac33.c line 547

Quoted 11 lines
sound/soc/codecs/tlv320dac33.c new file +10 -0
+static irqreturn_t dac33_interrupt_handler(int irq, void *dev)
+{
+	struct snd_soc_codec *codec = dev;
+	struct tlv320dac33_priv *dac33 = codec->private_data;
+
+	if (dac33->state == DAC33_PLAYBACK)
+		queue_work(dac33->dac33_wq, &dac33->work);
+
+	return IRQ_HANDLED;
+}

Should that return IRQ_HANDLED if it's not expecting an interrupt?

Good point. Interrupts are only coming, if the codec is running in nSample mode. The work is scheduled only, if the state is PLAYBACK, the other case would be the FLUSH state when there is a possibility to receive interrupt, but if the state already FLUSH, than we have had a work scheduled to write the command to the dac33.

The interrupt is actually handled, but there is nothing need to be done, so I think it is valid to return with IRQ_HANDLED.

Quoted 2 lines

I'd also expect to see something that either acknowledges the interrupt to deassert it or

The interrupt is self cleaning on dac33 side (in the mode, which we are using it, there is a mode, where additional write is needed to clear the interrupt status).

Quoted 2 lines

keeps it masked until the work has had a chance to do something.

Hmm, yes, but with the current implementation there is no need for that:
The interrupt is triggered on rising edge.
We only use the alarm threshold interrupt from dac33.
1. At startup I configure the dac33 to have the alarm threshold to be 10ms of audio in the buffer.
Initially dac33 will read at least 20ms of audio (it can be more)
2. dac33 will asserts the interrupt line, when the buffer usage reaches the alarm threshold, and keeps it high until the load level is under the alarm threshold value
3. We instruct the dac33 to read nSample amount (at least 20ms of audio). This is done in the dac33_work:DAC33_PLAYBACK
4. The interrupt line will be de-asserted, when the fill rate is above the alarm threshold (10ms)
5. dac33 recives the nSample amount of audio data, and only plays audio from the buffer.
6. goto 2.

Than on trigger STOP, I set the state to FLUSH and schedule the work, if an interrupt comes, it is ignored, since the work has been scheduled. In dac33_work, the FIFO flush is enabled, which means the the dac33 will play out the remaining samples and will not ask for a new one (interrupt line is not going to be asserted).

Now this part needs further tuning, since it is possible that at shutdown we actually leave some samples unplayed in the buffer and stop the dac33.

Quoted 4 lines

sound/soc/codecs/tlv320dac33.c line 1063

Quoted 2 lines
sound/soc/codecs/tlv320dac33.c new file +1 -0
+	pdata = (struct tlv320dac33_platform_data *)client->dev.platform_data;

Unneeded cast.

Oh, I've missed that.

Quoted 8 lines

sound/soc/codecs/tlv320dac33.c line 1110

Quoted 6 lines
sound/soc/codecs/tlv320dac33.c new file +5 -0
+	dac33->dac33_wq = create_rt_workqueue("tlv320dac33");
+	if (dac33->dac33_wq == NULL) {
+		ret = -ENOMEM;
+		goto error_wq;
+	}

Might be nice to skip the workqueue stuff if we don't have an IRQ.

Yes.

Quoted 2 lines

Conditional registration of the nSample controls based on having the IRQ may also be nice.

Will do that.

Quoted 7 lines
Quoted 7 lines
sound/soc/codecs/tlv320dac33.c new file +5 -0
+	ret = snd_soc_register_codec(codec);
+	if (ret != 0) {
+		dev_err(codec->dev, "Failed to register codec: %d\n", ret);
+		goto error_codec;
+	}
Quoted 10 lines

sound/soc/codecs/tlv320dac33.c line 1128

Quoted 6 lines
sound/soc/codecs/tlv320dac33.c new file +6 -0
+	ret = snd_soc_register_dai(&dac33_dai);
+	if (ret != 0) {
+		dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
+		snd_soc_unregister_codec(codec);
+		goto error_codec;
+	}

I'd do these last so that we don't have ASoC level init trying to use the device only to have it yanked from underneath it by a failure in the following code.

I'll move these

Thanks,
Péter

#7 lore raw
Quoted 6 lines · Peter Ujfalusi
Quoted 1 line

It'd be nice to do the full power down in BIAS_OFF for future use.

The full power off is taken when the dac33_soc_suspend is called (the hard_power is restored in the dac33_soc_resume function).
I can move it here, than add dac33_hard_power(codec, 1) to _PREPARE, if the previous state was _OFF.

_STANDBY but yes.

Quoted 1 line

Than I can remove the dac33_soc_suspend and dac33_soc_resume?

No, the core won't take you down to BIAS_OFF automatically except during shutdown (or suspend to disk but ARM doesn't do that). Which is fun but it's entertaining to change since it touches all the drivers. It's where we should go, though - I've been trying to do this when I write new drivers.

Quoted 6 lines
Quoted 2 lines

I'd also expect to see something that either acknowledges the interrupt to deassert it or

The interrupt is self cleaning on dac33 side (in the mode, which we are using it, there is a mode, where additional write is needed to clear the interrupt status).

That should be OK, someone can always add level triggering in future if they want to.

Quoted 2 lines

Than on trigger STOP, I set the state to FLUSH and schedule the work, if an interrupt comes, it is ignored, since the work has been scheduled.

Actually, you should just be able to queue the work since the state check will be done again in the workqueue and duplicate insertions don't hurt. That'd also avoid the need to mask IRQs while fiddling with the state (which you're not doing at the minute but probably need to).

#8 lore raw
Quoted 7 lines · On Wednesday 14 October 2009 16:24:02 ext Mark Brown wrote:
Quoted 3 lines

The interrupt is self cleaning on dac33 side (in the mode, which we are using it, there is a mode, where additional write is needed to clear the interrupt status).

That should be OK, someone can always add level triggering in future if they want to.

Quoted 7 lines
Quoted 2 lines

Than on trigger STOP, I set the state to FLUSH and schedule the work, if an interrupt comes, it is ignored, since the work has been scheduled.

Actually, you should just be able to queue the work since the state check will be done again in the workqueue and duplicate insertions don't hurt. That'd also avoid the need to mask IRQs while fiddling with the state (which you're not doing at the minute but probably need to).

Yes... I have some plans for revisiting the nSample mode (or burst mode) handling, but first I want to make sure, that the current implementation is correct from ASoC point of view.
Since the code is in it's third iteration, I think it is getting better day-by- day ;)

Signature · 1 line
Péter