ASoC: add ESS Technology ES9039Q2M codec driver
Subsystems (3)
v3 cover ASoC: add ESS Technology ES9039Q2M codec driver
This series adds support for the ESS Technology ES9039Q2M, a 32-bit two-channel audio DAC with an asynchronous sample rate converter, controlled over I2C. The part also has a hardware mode strapped by HW0/HW1/HW2 with no control bus at all; this driver implements the software mode that MODE = GND selects.
Two things about the part shape the driver:
- Selecting an input format takes two registers, not one. INPUT_SEL chooses the port; SYS MODE CONFIG enables the matching decoder, and at reset only ENABLE_TDM_DECODE is set. Selecting DoP without also enabling ENABLE_DOP_DECODE leaves the part hunting for a marker with the marker decoder switched off, so it finds nothing and mutes. The driver writes both.
- Several registers have non-zero reserved defaults - register 88 reads 0xb8 at reset - so every write is read-modify-write.
The programmable FIR coefficient controls are write-only by design. PROG_COEFF_OUT is documented as a coefficient readback but is not a RAM read port: it returns the last coefficient written, whatever address is selected in PROG_COEFF_ADDR. Measured with the driver out of the path, five sequences, ten reads, all returning the same value. A get() built on that register would return something with the shape of data and none of its meaning, so there is none.
Every control has been verified against the silicon rather than against the driver's own read-back - written through ALSA, then read over raw I2C
- and DSD, DoP and S/PDIF input are all implemented and tested on hardware.
ESS Technology has no bindings in the tree today, so the series also adds the vendor prefix, taken from the company's domain esstech.com.
Link to v1: https://lore.kernel.org/r/[email protected]
Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Fix the MCLK selection introduced in v2. hw_params() called clk_set_rate() for 256 * FS unconditionally, which is above the part's 50 MHz MCLK maximum for any rate over 192 kHz, and the rate the clock settled on was never re-checked against that ceiling. The ratio is now taken from the highest that fits.
- Decide 64FS mode from the MCLK/FS ratio in use rather than from a rate threshold. The 128 * FS floor was applied even in 64FS mode, where the datasheet asks for 64 * FS, so the 768 kHz this driver advertises in SNDRV_PCM_RATE_8000_768000 could never be reached - 128 * 768000 is 98.304 MHz. Register 0[6] is keyed to the ratio, not the rate: 384 kHz from 24.576 MHz needs it as much as 768 kHz from 49.152 MHz does. The asynchronous floor is also strictly greater than 130 * FS now, as Table 7 note 1 has it.
- The mute and DoP controls moved their shadow state before the I2C write and left it moved if the write failed. Both short-circuit on "no change", so a retry with the same value was a no-op and the control stayed permanently at odds with the register. They now restore the old value on failure.
- Enable the datapath at component probe. SYSTEM_CONFIG[1] is clear at reset, so in software mode the analogue output was dead until something outside the driver set that bit. Written after the mute and never before it.
- Add system suspend and resume. Where a board feeds the part a free-running oscillator, nothing in the system can gate its clock and it stayed fully clocked through suspend; suspend now clears ENABLE_DAC_CLK and resume resynchronises the cache. Resume mutes first, because regcache_sync() walks registers in ascending address order and would otherwise restore DAC_MODE in register 0 long before the mute in register 86, on any board that removes the part's supplies.
- Remove board-specific references from the comments - measurement attributions, dates, and two pointers at a repository outside the tree. No functional change.
- Note on what is and is not tested: everything at 176.4 kHz and below runs on the board this driver was written for. The paths above it are derived from the datasheet and not measured - that board feeds the part a fixed 24.576 MHz oscillator as a clock consumer and cannot reach them.
Changes in v2:
- Accept SND_SOC_DAIFMT_CBP_CFP rather than requiring CBC_CFC. v1 rejected every provider mode but consumer, which was one board's choice written into the driver. CBP_CFP now programs PCM_MASTER_MODE, hw_params() calls clk_set_rate() to pull MCLK to a multiple of the sample rate where the clock allows it, and the datasheet's two MCLK floors are applied separately - 128 x Fs synchronous, 130 x Fs asynchronous - instead of 130 x Fs unconditionally, which had been refusing 192 kHz on synchronous boards that can carry it.
- ES9039_DECODE_MASK did not cover ENABLE_SPDIF_DECODE. hw_params() uses that mask to enable one decoder and clear the rest, so the S/PDIF decoder stayed enabled alongside whichever decoder was selected.
- mute_stream() and the "Master Playback Switch" control both wrote ES9039_DAC_MUTE and overwrote each other. Each now records its own intent and the register is written from the union of the two.
- The DoP control returns -EBUSY while a stream is open rather than half-applying a change hw_params() will not re-run, and both halves of automatic DoP detection go through one helper under a mutex, so AUTO_INPUT_SEL and ENABLE_DOP_DECODE can no longer disagree.
- Status controls use the symbolic source-bit names rather than bare BIT() values. Doing so found a bug: "Clock Fault" read bit 7 of register 235, which is reserved, rather than BCK_WS_FAIL_SOURCE in register 234, and could never have reported a fault.
- The volume put() raises VOLUME_HOLD around the pair of channel writes and drops it afterwards, which is what the bit is for. A stereo change previously left the channels briefly at different levels.
- Rename the vendor prefix from "ess" to "esstech", after Krzysztof Kozlowski pointed out that ESS Technology's domain is esstech.com. The binding file, its $id and the compatible string follow.
- The harmonic correction controls keep their names rather than gaining a "Volume" suffix. The justification in the comment was wrong - TLV data is not what decides - but they cancel distortion rather than set a level, so the name stands and the comment now says why.
- Style: the header block is one C++ comment, and the "return ret ? ret :
1" ternaries are plain conditionals.
- Add the Assisted-by tag that Documentation/process/coding-assistants.rst asks for, which v1 omitted. This driver was developed with AI assistance. The board it was written against, the register-level measurements quoted above, and the responsibility for what is in it, are mine.
v2 has had a run on the board the driver was written for.
Karl Asseily (4):
dt-bindings: vendor-prefixes: add ESS Technology
ASoC: dt-bindings: add ESS Technology ES9039Q2M
ASoC: es9039q2m: add ESS Technology ES9039Q2M codec driver
MAINTAINERS: add entry for the ES9039Q2M codec driver
.../bindings/sound/esstech,es9039q2m.yaml | 61 + .../devicetree/bindings/vendor-prefixes.yaml | 2 + MAINTAINERS | 7 + sound/soc/codecs/Kconfig | 14 + sound/soc/codecs/Makefile | 2 + sound/soc/codecs/es9039q2m.c | 1831 +++++++++++++++++ 6 files changed, 1917 insertions(+) create mode 100644 Documentation/devicetree/bindings/sound/esstech,es9039q2m.yaml create mode 100644 sound/soc/codecs/es9039q2m.c
Signature · 1 line
2.34.1
PATCH v3 1/4 dt-bindings: vendor-prefixes: add ESS Technology
Add the vendor prefix for ESS Technology, Inc., maker of the SABRE family of audio DACs. The prefix follows the company's domain, esstech.com.
---
Notes:
v2: rename the vendor prefix to esstech, after the company domain
Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
@@ -559,6 +559,8 @@ patternProperties: description: Seiko Epson Corp. "^esp,.*": description: Espressif Systems Co. Ltd. + "^esstech,.*": + description: ESS Technology, Inc. "^est,.*": description: ESTeem Wireless Modems "^eswin,.*":
Signature · 1 line
2.34.1
Rob Herring
maintainerRe: [PATCH v3 1/4] dt-bindings: vendor-prefixes: add ESS Technology
Quoted 5 lines · Karl Asseily
Add the vendor prefix for ESS Technology, Inc., maker of the SABRE family of audio DACs. The prefix follows the company's domain, esstech.com.
Assisted-by Claude:claude-opus-5Signed-off-by Karl Asseily <[email protected]>
Missing Krzysztof's ack...
PATCH v3 2/4 ASoC: dt-bindings: add ESS Technology ES9039Q2M
In reply to Karl AsseilyAdd a binding for the ES9039Q2M, a 32-bit two-channel audio DAC with an asynchronous sample rate converter.
The part selects between two control personalities with its MODE pin:
hardware mode, strapped by HW0/HW1/HW2 with no control bus at all, and software mode over I2C or SPI. This binding describes software mode over I2C, which MODE = GND selects.
---
Notes:
v2: rename the binding to esstech,es9039q2m and update its $id and compatible
.../bindings/sound/esstech,es9039q2m.yaml | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Documentation/devicetree/bindings/sound/esstech,es9039q2m.yaml
Documentation/devicetree/bindings/sound/esstech,es9039q2m.yaml new file +61 -0
@@ -0,0 +1,61 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/sound/esstech,es9039q2m.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ESS Technology ES9039Q2M audio DAC + +maintainers: + - Karl Asseily <[email protected]> + +description: + The ES9039Q2M is a 32-bit two-channel audio DAC supporting PCM to 768 kHz, + DSD64 to DSD1024, DoP and S/PDIF. It is controlled over I2C or SPI when the + MODE pin selects software mode; this binding covers the I2C interface. The + part contains an asynchronous sample rate converter, so the system clock need + not be synchronous with the audio bit or frame clocks. + +allOf: + - $ref: dai-common.yaml# + +properties: + compatible: + const: esstech,es9039q2m + + reg: + description: + I2C address, selected by the ADDR0 and ADDR1 pins - 0x48, 0x49, 0x4a or + 0x4b for GND/GND, GND/AVDD, AVDD/GND and AVDD/AVDD respectively. + enum: [ 0x48, 0x49, 0x4a, 0x4b ] + + clocks: + maxItems: 1 + + clock-names: + const: mclk + + "#sound-dai-cells": + const: 0 + +required: + - compatible + - reg + - "#sound-dai-cells" + +unevaluatedProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + audio-codec@48 { + compatible = "esstech,es9039q2m"; + reg = <0x48>; + #sound-dai-cells = <0>; + clocks = <&dac_mclk>; + clock-names = "mclk"; + }; + };
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v3 2/4] ASoC: dt-bindings: add ESS Technology ES9039Q2M
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] dt-bindings: esstech,es9039q2m: missing power supply properties --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] dt-bindings: esstech,es9039q2m: missing power supply properties --
commit 319b6af08837af1db6a259282cc4b550059341ed
Author: Karl Asseily <[email protected]>
ASoC: dt-bindings: add ESS Technology ES9039Q2M
Add a binding for the ES9039Q2M, a 32-bit two-channel audio DAC with an asynchronous sample rate converter.
Quoted 5 lines
Documentation/devicetree/bindings/sound/esstech,es9039q2m.yaml new file +0
-0
[ ... ]
Documentation/devicetree/bindings/sound/esstech,es9039q2m.yaml line 29
Quoted 8 lines
Documentation/devicetree/bindings/sound/esstech,es9039q2m.yaml new file +8
-0+properties: + compatible: + const: esstech,es9039q2m + + reg: + description: + I2C address, selected by the ADDR0 and ADDR1 pins - 0x48, 0x49, 0x4a or + 0x4b for GND/GND, GND/AVDD, AVDD/GND and AVDD/AVDD respectively.
[Severity: Medium]
Should there be a property for the AVDD power supply in this binding?
The description explicitly mentions the AVDD pin, confirming the hardware utilizes power supplies that are not modeled as properties in the schema. Without them, device trees cannot properly model the analogue and digital power supplies of the hardware.
If the supplies are dynamically managed, the lack of properties prevents describing this topology, potentially leaving the device unpowered on systems where the bootloader does not leave them enabled. Adding them as required properties later would break backward compatibility with device trees written against this initial schema.
Quoted 4 lines
Documentation/devicetree/bindings/sound/esstech,es9039q2m.yaml new file +4
-0+ enum: [ 0x48, 0x49, 0x4a, 0x4b ] + + clocks: + maxItems: 1
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Rob Herring
maintainerRe: [PATCH v3 2/4] ASoC: dt-bindings: add ESS Technology ES9039Q2M
In reply to Karl AsseilyQuoted 10 lines · Karl Asseily
Add a binding for the ES9039Q2M, a 32-bit two-channel audio DAC with an asynchronous sample rate converter.
The part selects between two control personalities with its MODE pin:
hardware mode, strapped by HW0/HW1/HW2 with no control bus at all, and software mode over I2C or SPI. This binding describes software mode over I2C, which MODE = GND selects.Assisted-by Claude:claude-opus-5Signed-off-by Karl Asseily <[email protected]>
Missing Krzysztof's R-by, but sashiko is right here. You should be some supplies.
Rob
PATCH v3 3/4 ASoC: es9039q2m: add ESS Technology ES9039Q2M codec driver
In reply to Karl AsseilyThe ES9039Q2M is a 32-bit two-channel audio DAC with an asynchronous sample rate converter. It selects between two control personalities with its MODE pin: hardware mode, strapped by HW0/HW1/HW2 with no control bus at all, and software mode over I2C or SPI. This driver implements software mode over I2C, which MODE = GND selects.
Three properties of the part shape the driver:
- The ASRC in front of the DAC means MCLK need not be synchronous with BCLK or LRCK, so a board can feed it a fixed oscillator and never touch the clock again. The driver derives which case it is in rather than assuming one: the part runs synchronously when it drives the bus clocks itself, or when the MCLK it was given can be set to a multiple of the sample rate, and asynchronously only when a fixed oscillator feeds it as a clock consumer. The datasheet's MCLK floor differs between the two - 128 x Fs synchronous, 130 x Fs asynchronous
- and where the clock is fixed a startup constraint derives the maximum rate from the MCLK actually present rather than advertising rates the hardware cannot honour.
- Selecting an input format takes two registers, not one. Register 57 INPUT_SEL chooses which port to listen to; register 1 SYS MODE CONFIG enables the corresponding decoder, and at reset only ENABLE_TDM_DECODE is set. Selecting DoP without also enabling ENABLE_DOP_DECODE leaves the part hunting for a marker with the marker decoder switched off, so it finds no valid DoP and mutes. The driver sets both.
- Several registers have non-zero reserved defaults - register 88 reads 0xb8 at reset - so every write is read-modify-write.
The datapath is off at reset and the driver turns it on at component probe. SYSTEM_CONFIG[1] is clear when the part comes out of reset, so in software mode nothing enables it at power-up and the analogue output stays dead until the driver writes that bit. It is written after the mute and never before it: bringing the datapath up into an unmuted part whose volume registers still sit at their 0 dB reset value is the thump the mute is there to prevent. Note when testing that a warm reboot does not reset this part - registers and coefficient RAM both survive one, and only removing power clears them - so the previous boot's value can hide whether the write happened at all.
DoP is detected by the part rather than announced by the host:AUTO_INPUT_SEL is enabled and both the TDM and DoP decoders run, so a player can simply send DoP-encoded PCM. That is the only way DoP works in practice, because no player can select a DAC mode through a kcontrol first, and material sent to a part that is not looking for the marker renders as the near-silent hiss the DoP design deliberately degrades to.
The programmable FIR coefficient controls are write-only by design. The part has a PROG_COEFF_OUT register described as "Programmable FIR coefficient readback", but it is not a RAM read port: it returns the last coefficient written, whatever address is selected in PROG_COEFF_ADDR. Measured by writing two different coefficients to addresses 0 and 1 and reading them back five ways - plain, with a settle delay, with PROG_COEFF_EN set, with the address written twice, and with a write-enable pulse after the address. All ten reads returned the value written to address 1. A get() built on that register would return something with the shape of data and none of its meaning, so there is none.
Every control was verified against the silicon rather than against the driver's own read-back: written through ALSA, then read from the chip over raw I2C. That covers the eight filter shapes, the modulator bandwidth, the IIR bandwidth, the four signed 16-bit THD compensation coefficients at both extremes of their range, and the automute enable, time, level and off-level fields including the neighbouring MUTE_RAMP_TO_GND bit that shares a register with the 11-bit time field. The coefficient upload, whose RAM cannot be read back, was verified from the I2C bus itself.
System suspend gates the part's internal clock tree. Where the part is clocked from the SoC, system suspend stops its clock as a side effect and there is nothing for a driver to do; where the board feeds it a free-running oscillator - which the ASRC makes an attractive design, since MCLK then need not track BCLK or LRCK - nothing in the system can gate that clock and the part stays fully clocked for the whole of suspend. Clearing ENABLE_DAC_CLK is the only part of its consumption software can reach; the analogue supplies are the board's business. Measured on an RK3588 board with a fixed 24.576 MHz oscillator, system suspend power fell from 1.98 W to 1.95 W and followed the register in both directions across an A/B.
Resume mutes the part before regcache_sync(), because the sync walks registers in ascending address order. A board that keeps the supplies up needs nothing, but one that removes them brings the part back at reset defaults - DAC_MODE clear, both channels unmuted, both volume registers at 0 dB - and the sync would set DAC_MODE in register 0 and only restore the real mute in register 86, lighting the datapath up at full scale in between.
---
Notes:
v3:
- fix the MCLK selection added in v2. clk_set_rate() asked for 256 * FS unconditionally, which is above the part's 50 MHz maximum for anything over 192 kHz, and the rate the clock settled on was not re-checked. The ratio is now the highest of 512/256/128/64 that fits
- 64FS mode is decided from the MCLK/FS ratio in use rather than from a rate threshold, so the advertised 768 kHz is reachable instead of always failing the 128 * FS floor, and the asynchronous floor is strictly greater than 130 * FS as the datasheet has it
- the mute and DoP controls restore their shadow state if the register write fails. Both short-circuit on "no change", so a retry with the same value was a no-op and the control stayed permanently at odds with the register
- enable the datapath at component probe. SYSTEM_CONFIG[1] is clear at reset, so in software mode the analogue output was dead until something outside the driver set that bit. Written after the mute, never before it
- add system suspend and resume. Suspend clears ENABLE_DAC_CLK, which gates the internal clock tree and is all that software can reach where MCLK is a free-running oscillator the system cannot gate. Resume mutes before regcache_sync(), because the sync walks registers in ascending address order and would otherwise restore DAC_MODE in register 0 long before the mute in register 86, on any board that removes the part's supplies
- comments: board-specific measurement attributions, dates and two pointers outside the tree removed. No functional change
v2: address Mark Brown's review of v1
- make the whole header block a C++ comment
- ES9039_DECODE_MASK now covers S/PDIF. hw_params() uses it to enable one decoder and clear the rest, so omitting it left S/PDIF enabled alongside whichever decoder was selected
- plain conditionals in place of "return ret ? ret : 1"
- mute_stream() and the Master Playback Switch no longer both write ES9039_DAC_MUTE. Each records its intent and the register is written from the union, so neither can override the other
- the DoP control returns -EBUSY while a stream is open rather than half-applying a change hw_params() will not re-run
- the status controls use the symbolic source bits; TDM_VALID gained one
- the harmonic correction controls keep their names. The TLV justification was wrong and is gone, but they cancel distortion rather than set a level
- clocking, which was one mistake in three places. SND_SOC_DAIFMT_CBP_CFP is accepted and programs PCM_MASTER_MODE; clk_set_rate() pulls MCLK to 256 * FS when the clock is settable; synchronous operation is derived rather than assumed; and the MCLK floor is 128 * FS synchronous or 130 * FS asynchronous, not 130 unconditionally, so a synchronous board is no longer refused rates it can carry
- fix the Clock Fault control, which read the byte above the status bit and so could never report a fault
- hold both DoP bits consistent under a mutex, and use VOLUME_HOLD so the two channel volumes latch together
sound/soc/codecs/Kconfig | 14 + sound/soc/codecs/Makefile | 2 + sound/soc/codecs/es9039q2m.c | 1831 ++++++++++++++++++++++++++++++++++ 3 files changed, 1847 insertions(+) create mode 100644 sound/soc/codecs/es9039q2m.c
sound/soc/codecs/Kconfig +14 -0
@@ -123,6 +123,7 @@ config SND_SOC_ALL_CODECS imply SND_SOC_ES8328_I2C imply SND_SOC_ES8375 imply SND_SOC_ES8389 + imply SND_SOC_ES9039Q2M imply SND_SOC_ES9356 imply SND_SOC_ES7134 imply SND_SOC_ES7241
@@ -1317,6 +1318,19 @@ config SND_SOC_ES8389 tristate "Everest Semi ES8389 CODEC" depends on I2C +config SND_SOC_ES9039Q2M + tristate "ESS Technology ES9039Q2M CODEC" + depends on I2C + select REGMAP_I2C + help + Enable support for the ESS Technology ES9039Q2M, a 32-bit + two-channel audio DAC with an asynchronous sample rate converter, + supporting PCM, DSD, DoP and S/PDIF input. This driver covers the + I2C control interface, which the MODE pin selects. + + To compile this driver as a module, choose M here: the module + will be called snd-soc-es9039q2m. + config SND_SOC_ES9356 tristate "Everest Semi ES9356 CODEC SDW" depends on SND_SOC_SDCA
sound/soc/codecs/Makefile +2 -0
@@ -140,6 +140,7 @@ snd-soc-es8328-i2c-y := es8328-i2c.o snd-soc-es8328-spi-y := es8328-spi.o snd-soc-es8375-y := es8375.o snd-soc-es8389-y := es8389.o +snd-soc-es9039q2m-y := es9039q2m.o snd-soc-es9356-y := es9356.o snd-soc-framer-y := framer-codec.o snd-soc-fs-amp-lib-y := fs-amp-lib.o
@@ -586,6 +587,7 @@ obj-$(CONFIG_SND_SOC_ES8328_I2C)+= snd-soc-es8328-i2c.o obj-$(CONFIG_SND_SOC_ES8328_SPI)+= snd-soc-es8328-spi.o obj-$(CONFIG_SND_SOC_ES8375) += snd-soc-es8375.o obj-$(CONFIG_SND_SOC_ES8389) += snd-soc-es8389.o +obj-$(CONFIG_SND_SOC_ES9039Q2M) += snd-soc-es9039q2m.o obj-$(CONFIG_SND_SOC_ES9356) += snd-soc-es9356.o obj-$(CONFIG_SND_SOC_FRAMER) += snd-soc-framer.o obj-$(CONFIG_SND_SOC_FS_AMP_LIB)+= snd-soc-fs-amp-lib.o
sound/soc/codecs/es9039q2m.c new file +1831 -0
1,831 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v3 3/4] ASoC: es9039q2m: add ESS Technology ES9039Q2M codec driver
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] es9039q2m: unprotected read-modify-write in es9039_wide_put - [Medium] es9039q2m: unprotected…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] es9039q2m: unprotected read-modify-write in es9039_wide_put
- [Medium] es9039q2m: unprotected VOLUME_HOLD sequence allows torn volume updates
- [Medium] es9039q2m: FIR coefficient RAM is lost on system suspend
- [Low] i2c: missing named initializer for struct i2c_device_id
--
commit b181df27142d5449ea7b02fc0c4c0aa3fd24e5a8
Author: Karl Asseily <[email protected]>
ASoC: es9039q2m: add ESS Technology ES9039Q2M codec driver
This patch adds I2C support for the ESS Technology ES9039Q2M 32-bit 2-channel audio DAC. It includes handling for software mode over I2C, an asynchronous sample rate converter, and automatic DoP detection.
Quoted 4 lines
sound/soc/codecs/es9039q2m.c new file +0
-0
[ ... ]
Quoted 13 lines
sound/soc/codecs/es9039q2m.c new file +13
-0+static int es9039_wide_put (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_value *ucontrol) +{ +struct snd_soc_component *c =snd_kcontrol_chip (kcontrol); +struct es9039q2m_priv *priv =snd_soc_component_get_drvdata (c); +struct es9039_wide_ctl *p = (void *)kcontrol->private_value; +long v = ucontrol->value.integer.value[0 ]; +u32 old; +int ret; + +if (v <0 || v > p->max) +return -EINVAL ; +
sound/soc/codecs/es9039q2m.c line 440
Quoted 10 lines
sound/soc/codecs/es9039q2m.c new file +10
-0+ ret =es9039_read_le (priv->regmap, p->reg, p->bytes, &old); +if (ret) +return ret; + +if ((old & p->mask) == (u32 )v) +return 0 ; + +/* Preserve the bits outside the field - reg 124 carries MUTE_RAMP. */ + ret =es9039_write_le (priv->regmap, p->reg, p->bytes, + (old & ~p->mask) | (u32 )v);
[Severity: Medium]
Could this manual read-modify-write sequence race with other ALSA control updates?
Because snd_ctl_elem_write() takes the ALSA controls_rwsem lock in read mode, concurrent control writes can interleave here. If another thread updates the "Mute Ramp To Ground Switch" (which shares register 125, mapped here) right after es9039_read_le() completes, es9039_write_le() will overwrite and lose that concurrent change.
Should this sequence be protected by a lock?
[ ... ]
Quoted 6 lines
sound/soc/codecs/es9039q2m.c new file +6
-0+static int es9039_vol_put (struct snd_kcontrol *kcontrol, +struct snd_ctl_elem_value *ucontrol) +{ +struct snd_soc_component *c =snd_kcontrol_chip (kcontrol); +struct es9039q2m_priv *priv =snd_soc_component_get_drvdata (c); +int ret;
sound/soc/codecs/es9039q2m.c line 910
Quoted 10 lines
sound/soc/codecs/es9039q2m.c new file +10
-0+ + ret =regmap_update_bits (priv->regmap,ES9039_IIR_SPDIF , +ES9039_VOLUME_HOLD ,ES9039_VOLUME_HOLD ); +if (ret) +return ret; + + ret =snd_soc_put_volsw (kcontrol, ucontrol); + +regmap_update_bits (priv->regmap,ES9039_IIR_SPDIF , +ES9039_VOLUME_HOLD ,0 );
[Severity: Medium]
Does this sequence need protection against concurrent volume updates?
If two threads call es9039_vol_put() concurrently, the sequences could interleave. For example, Thread 1 could clear ES9039_VOLUME_HOLD while Thread 2 is halfway through its snd_soc_put_volsw() writes.
Thread 2's remaining register writes would then apply immediately instead of being latched together, which defeats the hardware pop-suppression mechanism.
[ ... ]
Quoted 26 lines
sound/soc/codecs/es9039q2m.c new file +26
-0+static int es9039q2m_resume (struct snd_soc_component *component) +{ +struct es9039q2m_priv *priv =snd_soc_component_get_drvdata (component); +int ret; + +guard (mutex)(&priv->lock); + +regcache_cache_only (priv->regmap, false); + +/* +* Mute the part before anything else, and write it straight to the +* hardware rather than through the cache. +* +* Where the board kept the supplies up this is a no-op - the part still +* holds the mute state it went to sleep with. Where the board removed +* them it is not: the part comes back at reset defaults, which are +* DAC_MODE clear, both channels UNMUTED, and both volume registers at +* 0x00, their 0 dB setting. regcache_sync() walks registers in ascending +* address order, so it would set DAC_MODE in register 0 - lighting the +* datapath up unmuted at full scale - and only restore the real mute in +* register 86, long after. That is the thump the probe path mutes to +* avoid, arriving by the other door. +* +* The cache is bypassed so the cached mute state is untouched; the sync +* below puts it back, after the volume registers at 74 and 75. +*/
sound/soc/codecs/es9039q2m.c line 1721
Quoted 10 lines
sound/soc/codecs/es9039q2m.c new file +10
-0+regcache_cache_bypass (priv->regmap, true); + ret =regmap_update_bits (priv->regmap,ES9039_DAC_MUTE , +ES9039_MUTE_BOTH ,ES9039_MUTE_BOTH ); +regcache_cache_bypass (priv->regmap, false); + +if (ret) +dev_warn (component->dev, +"could not mute before resync: %d\n" , ret); + + ret =regcache_sync (priv->regmap);
[Severity: Medium]
Will the programmable FIR coefficients survive a system suspend?
If the device loses power during suspend, the FIR memory is cleared. When resume calls regcache_sync() here, it will restore ES9039_PROG_RAM_CTRL (which turns the custom FIR switch back on). However, the FIR taps themselves are not restored since the data port is write-only and not tracked by regmap.
Can this result in silent or distorted audio on resume?
[ ... ]
sound/soc/codecs/es9039q2m.c line 1816
Quoted 4 lines
sound/soc/codecs/es9039q2m.c new file +4
-0+static const struct i2c_device_id es9039q2m_i2c_id[] = { + {"es9039q2m" }, + { } +};
[Severity: Low]
Can we use named initializers for this array?
The I2C subsystem requires explicitly named initializers for struct i2c_device_id arrays to ensure proper device matching.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v3 3/4] ASoC: es9039q2m: add ESS Technology ES9039Q2M codec driver
Quoted 5 lines
[Severity: Low]
Can we use named initializers for this array?The I2C subsystem requires explicitly named initializers for struct
i2c_device_idarrays to ensure proper device matching.
Not taken, and I do not think the premise holds. Measured against broonie/sound for-next: sound/soc/codecs carries 202 i2c_device_id tables, and 136 of their entries use the plain positional form { "name", data }, which is not a named initializer at all. The bare { "name" } this driver uses accounts for 196 more. Matching does not read the initializer syntax - i2c_match_id() compares the name string - so the form has no bearing on device matching.
The other three findings were real and are all fixed in v4, which is on the list now. Thank you for them - the FIR-versus-suspend one was a genuine bug.
Mark Brown
maintainerRe: [PATCH v3 3/4] ASoC: es9039q2m: add ESS Technology ES9039Q2M codec driver
In reply to Karl AsseilyQuoted 36 lines · Karl Asseily
The ES9039Q2M is a 32-bit two-channel audio DAC with an asynchronous sample rate converter. It selects between two control personalities with its MODE pin: hardware mode, strapped by HW0/HW1/HW2 with no control bus at all, and software mode over I2C or SPI. This driver implements software mode over I2C, which MODE = GND selects.
sound/soc/codecs/es9039q2m.c new file +30
-0+static int es9039_fir_put (struct snd_kcontrol *kcontrol, +const unsigned int __user *bytes,unsigned int size) +{ +struct snd_soc_component *c =snd_kcontrol_chip (kcontrol); +struct es9039q2m_priv *priv =snd_soc_component_get_drvdata (c); +struct soc_bytes_ext *be = (void *)kcontrol->private_value; + struct es9039_fir_ctl *p = container_of(be, struct es9039_fir_ctl, be); +u8 *buf; +int ret, i; + +if (size != p->taps *ES9039_COEFF_BYTES ) +return -EINVAL ; + + buf =memdup_user (bytes, size); +if (IS_ERR (buf)) +return PTR_ERR (buf); + +/* +* PROG_COEFF_WE is a per-coefficient strobe, not a gate held open +* across the upload. The datasheet's sequence is address, data, raise +* WE, lower WE, once per coefficient. Holding it high for the whole +* loop also appears to work on ES9039Q2M silicon, but "appears to +* work" is not a specification. +*/ +for (i =0 ; i < p->taps; i++) { + ret =regmap_write (priv->regmap,ES9039_PROG_RAM_ADDR , + (p->stage_4x ?ES9039_PROG_STAGE_4X :0 ) | +FIELD_PREP (ES9039_PROG_ADDR_MASK , i)); +if (ret) + goto out;
...
sound/soc/codecs/es9039q2m.c line 542
Quoted 6 lines
sound/soc/codecs/es9039q2m.c new file +6
-0+out: +regmap_update_bits (priv->regmap,ES9039_PROG_RAM_CTRL , +ES9039_PROG_COEFF_WE ,0 ); +kfree (buf); +if (ret) +return ret;
How does this work with suspend and resume if the device is powered down in suspend? It looks like this is doing some windowing stuff and needs a specific write sequence.
Re: [PATCH v3 3/4] ASoC: es9039q2m: add ESS Technology ES9039Q2M codec driver
Quoted 10 lines
sound/soc/codecs/es9039q2m.c line 542
Quoted 6 lines
sound/soc/codecs/es9039q2m.c new file +6
-0+out: +regmap_update_bits (priv->regmap,ES9039_PROG_RAM_CTRL , +ES9039_PROG_COEFF_WE ,0 ); +kfree (buf); +if (ret) +return ret;How does this work with suspend and resume if the device is powered down in suspend? It looks like this is doing some windowing stuff and needs a specific write sequence.
You are right, and it is broken as posted.
The coefficient RAM is write-only: PROG_COEFF_OUT is documented as a readback register but returns the last coefficient written rather than the addressed one, as the commit message describes. So regmap cannot cache it and nothing else in the system holds a copy. On a board that keeps the part powered through suspend nothing is lost, but where the supplies go away the RAM comes back undefined while regcache_sync() restores the filter selection from the cache - so selecting the programmable filter would point the interpolator at whatever the RAM powered up holding.
For v4 the driver keeps a shadow of the last upload in its private struct, the upload loop is split into a helper, and resume calls that helper again after regcache_sync() for any stage that was ever written. Stages never uploaded keep the part's own defaults.
You are right about the sequence too. It is address, data, raise PROG_COEFF_WE, lower it, once per coefficient - a per-coefficient strobe rather than a gate held open across the upload. Splitting the helper out exposed a second problem I had missed: both FIR controls drive the same address, data and strobe registers and nothing serialised them, so two concurrent uploads could interleave into each other's RAM. The mutex that orders an upload against resume closes that as well.
For completeness: an automated review on this thread raised the same suspend question a few hours before your mail, so the fix above was already written and building by the time it arrived.
I will hold v4 until your review is finished rather than resending piecemeal.
Thanks,
Karl
Mark Brown
maintainerRe: [PATCH v3 3/4] ASoC: es9039q2m: add ESS Technology ES9039Q2M codec driver
Quoted 2 lines · Karl Asseily
I will hold v4 until your review is finished rather than resending piecemeal.
I think that was pretty much it from the pass I did.
PATCH v3 4/4 MAINTAINERS: add entry for the ES9039Q2M codec driver
In reply to Karl AsseilyAdd myself as maintainer of the ES9039Q2M codec driver and its binding.
---
Notes:
v2: follow the binding rename
MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -9699,6 +9699,13 @@ S: Maintained F: include/linux/errseq.h F: lib/errseq.c +ES9039Q2M AUDIO CODEC DRIVER +M: Karl Asseily <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/sound/esstech,es9039q2m.yaml +F: sound/soc/codecs/es9039q2m.c + ESD CAN NETWORK DRIVERS M: Stefan Mätje <[email protected]> R: [email protected]
Signature · 1 line
2.34.1