Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=80.241.56.151 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796181229; b=DG5/0YRilnExty2Yw82DXLorggOVZHh4bAmn75B4RXZBgUgyRSqj5cKKAAi3Kmx8ophC v2VgJ5qLJHBSasmAjiX14qp4j1VsNS/U596uwVmlpit/40hWn6tZ+Vv2pwtT4Ryro6swn 5Gz//GXm8mzWAtt4NHfKX+EwdFwqB5a2SXrAOS8fJMEIYVuW7eiLoGh1qoSv3WHoIbGm3 I5+yXGPt6GVrwEqw1f5Onbj3bMVJ8EC3z8ppylM+kgVJHuk1NldNWegrsVzqzoftaa2YV 7CTnBwU+nDI1sjL9z6+VqqVl0yj4OqEqhpLaCOeigXfPrNfdIV/FVC8PxuG+V6RC10Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181229; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Rspamd-Queue-Id; bh=7btTbNau1qYLQ9l12cXft7xwzQmZC+8TiPit4iQlitw=; b=qo/W7XQu+Oq4dAk8YiJTBpSKidXAnThNRUPD1CH3cIq8ynTCU+Llng7JdIQRVmMy6/3G M500UlnGh7jRgLN872vJv/KkrqgHbo2OM/jAQIu8/CgoQ+orr9my1Ns7F2ShAMg9tC57c OGSR1JDyymiZawqPDzNWPAW9MvqBBej83PyYTAlF5K8EfecF3OGz1V97hdUC4jvShQJlI xBmRFmGrTT9064MF3poJDIZCyt7ZUN4GUJy6GOy7dYmq8DZZax55PollHw2zL7FCdWb31 XSaHEOk5asnz3yq9JlZ/JS3v9Be4hgdZTonFsMuRBE2Ml9op4gR4JfeLwacZj0q2Upg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=slensky.com; spf=pass smtp.mailfrom=slensky.com; dkim=pass header.d=slensky.com header.i=@slensky.com header.a=rsa-sha256 header.s=MBO0001 header.b=GizjQaXA; arc=none smtp.remote-ip=80.241.56.151 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39C49606A32D; Wed, 2 Dec 2026 03:13:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 39C49606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=slensky.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=slensky.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=slensky.com header.i=@slensky.com header.a=rsa-sha256 header.s=MBO0001 header.b=GizjQaXA Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4jfNBf0zYPz8tf5; Wed, 02 Dec 2026 04:13:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=slensky.com; s=MBO0001; t=1796181226; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7btTbNau1qYLQ9l12cXft7xwzQmZC+8TiPit4iQlitw=; b=GizjQaXA3/srUebBgpnm3UDvpwJ8pz3DR2xKUqUQMKQlV4bdzbMpQE4MfbK3GITr3x9ulk nvFTngzye41dyIxGEvfGu0M/42bCveS+BKjyp5U8/o/9Te4paGRcraz10H7mB1LIf774CA vMBMF512B9aQZOzSrTuwDYdT82JSSXwjrD/rkM2NBYRxD8HNE6zWveTZBJA2cszVGN4Wi3 A4hAY/gakYicKTbi9tvavI92ZNASeoYsOgZnlsC+j1J3AZFm7rzudW9b6LzTfrnxxHvnbg JmSxnM7Tkm9CxrhPyTBf9OQrxAGi7JNvHf0Ep/AWlHXSQI3aVfY627G9Qw33XQ== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of linux@slensky.com designates 2001:67c:2050:b231:465::2 as permitted sender) smtp.mailfrom=linux@slensky.com From: Stephan Lensky To: Takashi Iwai Cc: Jaroslav Kysela , Mark Brown , Liam Girdwood , "Rafael J . Wysocki" , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Hans de Goede , Len Brown , Kevin Cuperus , linux-sound@vger.kernel.org, linux-acpi@vger.kernel.org, platform-driver-x86@vger.kernel.org, Stephan Lensky Subject: [PATCH 3/4] ALSA: hda: Add MAX98390 side codec driver Date: Tue, 1 Dec 2026 22:12:32 -0500 Message-ID: <20261202031233.171555-4-linux@slensky.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261202031233.171555-1-linux@slensky.com> References: <20261202031233.171555-1-linux@slensky.com> X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4jfNBf0zYPz8tf5 The Samsung Galaxy Book4 Pro 360 (NP960QGK) pairs a Realtek ALC298 with four MAX98390 boosted class-D amplifiers on I2C, a woofer and a tweeter per channel. The platform firmware does not configure the amplifiers, so the internal speakers stay silent. Add an HDA side codec driver for them, structured like the existing CS35L41, TAS2781 and AW88399 side codec drivers. It binds to the "max98390-hda" clients created by serial-multi-instantiate and to the HDA codec through the component framework. The PCM interface is fixed at 48 kHz, the only rate the SOF analog PCMs offer on this machine. The amplifiers are enabled when a playback stream is opened, before the codec starts the I2S clocks. This is safe because the clock monitor restarts automatically: the amplifier stays off until valid BCLK and LRCLK arrive. On close, the pre-playback hook clears the global enable on all amplifiers first, so the 50 ms the datasheet requires before clearing the speaker enable is paid once rather than per amplifier. The speaker tuning lives in the amplifier's Dynamic Speaker Management (DSM) block. On bind, the driver requests maxim/max98390-hda--.bin e.g. maxim/max98390-hda-144dc892-0.bin, in the format of the ASoC driver's dsm_param.bin: a little-endian 16-bit start register, a 16-bit payload size and the payload at offset 16. The payload must lie within registers 0x2050 to 0x23e0, which excludes the DSP and global enables. A valid file is written on top of the default tuning and reapplied on resume. No tuning files exist yet. The default is a second-order Butterworth high-pass filter at 100 Hz on the woofers and 3 kHz on the tweeters, and a DSM volume of -7.5 dB. Thermal and excursion protection stay disabled because they need per-speaker parameters, which a tuning file can provide. Without protection, the DSM volume is the only limit on output power: -7.5 dB caps a full-scale signal at about 3-4 W per speaker, down from roughly 17 W into the tweeters at 0 dB. This driver is based on work by Kevin Cuperus in the SOF pull request linked below. The rework and this commit message were done with the help of an AI coding assistant (Claude Code). All changes were reviewed by the submitter and tested on an NP960QGK. Link: https://github.com/thesofproject/linux/pull/5616 Co-developed-by: Kevin Cuperus Signed-off-by: Kevin Cuperus Signed-off-by: Stephan Lensky --- MAINTAINERS | 6 + sound/hda/codecs/side-codecs/Kconfig | 18 + sound/hda/codecs/side-codecs/Makefile | 4 + sound/hda/codecs/side-codecs/max98390_hda.c | 435 ++++++++++++++++++ sound/hda/codecs/side-codecs/max98390_hda.h | 18 + .../hda/codecs/side-codecs/max98390_hda_i2c.c | 80 ++++ 6 files changed, 561 insertions(+) create mode 100644 sound/hda/codecs/side-codecs/max98390_hda.c create mode 100644 sound/hda/codecs/side-codecs/max98390_hda.h create mode 100644 sound/hda/codecs/side-codecs/max98390_hda_i2c.c diff --git a/MAINTAINERS b/MAINTAINERS index 2ce1f5b71db7..a17f6b45c8e7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16584,6 +16584,12 @@ S: Supported F: Documentation/devicetree/bindings/power/supply/maxim,max77976.yaml F: drivers/power/supply/max77976_charger.c +MAXIM MAX98390 HDA SIDE CODEC DRIVER +M: Stephan Lensky +L: linux-sound@vger.kernel.org +S: Maintained +F: sound/hda/codecs/side-codecs/max98390_hda* + MAXIM MUIC CHARGER DRIVERS FOR EXYNOS BASED BOARDS M: Krzysztof Kozlowski S: Maintained diff --git a/sound/hda/codecs/side-codecs/Kconfig b/sound/hda/codecs/side-codecs/Kconfig index b7e2a6fa4258..de9379a749a8 100644 --- a/sound/hda/codecs/side-codecs/Kconfig +++ b/sound/hda/codecs/side-codecs/Kconfig @@ -124,6 +124,24 @@ config SND_HDA_SCODEC_CS35L56_CAL_DEBUGFS If unsure select "N". endmenu +config SND_HDA_SCODEC_MAX98390 + tristate + select SND_HDA_GENERIC + +config SND_HDA_SCODEC_MAX98390_I2C + tristate "Build MAX98390 HD-audio side codec support for I2C Bus" + depends on I2C + depends on ACPI + imply SERIAL_MULTI_INSTANTIATE + select REGMAP_I2C + select SND_HDA_SCODEC_MAX98390 + help + Say Y or M here to include MAX98390 I2C HD-audio side codec support + in snd-hda-intel driver, such as ALC298. + +comment "Set to Y if you want auto-loading the side codec driver" + depends on SND_HDA=y && SND_HDA_SCODEC_MAX98390_I2C=m + config SND_HDA_SCODEC_TAS2781 tristate select SND_HDA_GENERIC diff --git a/sound/hda/codecs/side-codecs/Makefile b/sound/hda/codecs/side-codecs/Makefile index 7dd010c68f78..0e06573ab9c0 100644 --- a/sound/hda/codecs/side-codecs/Makefile +++ b/sound/hda/codecs/side-codecs/Makefile @@ -12,6 +12,8 @@ snd-hda-scodec-cs35l56-y := cs35l56_hda.o snd-hda-scodec-cs35l56-i2c-y := cs35l56_hda_i2c.o snd-hda-scodec-cs35l56-spi-y := cs35l56_hda_spi.o snd-hda-scodec-component-y := hda_component.o +snd-hda-scodec-max98390-y := max98390_hda.o +snd-hda-scodec-max98390-i2c-y := max98390_hda_i2c.o snd-hda-scodec-tas2781-y := tas2781_hda.o snd-hda-scodec-tas2781-i2c-y := tas2781_hda_i2c.o snd-hda-scodec-tas2781-spi-y := tas2781_hda_spi.o @@ -27,6 +29,8 @@ obj-$(CONFIG_SND_HDA_SCODEC_CS35L56) += snd-hda-scodec-cs35l56.o obj-$(CONFIG_SND_HDA_SCODEC_CS35L56_I2C) += snd-hda-scodec-cs35l56-i2c.o obj-$(CONFIG_SND_HDA_SCODEC_CS35L56_SPI) += snd-hda-scodec-cs35l56-spi.o obj-$(CONFIG_SND_HDA_SCODEC_COMPONENT) += snd-hda-scodec-component.o +obj-$(CONFIG_SND_HDA_SCODEC_MAX98390) += snd-hda-scodec-max98390.o +obj-$(CONFIG_SND_HDA_SCODEC_MAX98390_I2C) += snd-hda-scodec-max98390-i2c.o obj-$(CONFIG_SND_HDA_SCODEC_TAS2781) += snd-hda-scodec-tas2781.o obj-$(CONFIG_SND_HDA_SCODEC_TAS2781_I2C) += snd-hda-scodec-tas2781-i2c.o obj-$(CONFIG_SND_HDA_SCODEC_TAS2781_SPI) += snd-hda-scodec-tas2781-spi.o diff --git a/sound/hda/codecs/side-codecs/max98390_hda.c b/sound/hda/codecs/side-codecs/max98390_hda.c new file mode 100644 index 000000000000..15ae3c5a9a67 --- /dev/null +++ b/sound/hda/codecs/side-codecs/max98390_hda.c @@ -0,0 +1,435 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// MAX98390 HDA driver +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "hda_component.h" +#include "../generic.h" +#include "max98390_hda.h" + +#define MAX98390_HDA_NUM_AMPS 4 +#define MAX98390_HDA_AMP_EN_DEM BIT(7) +#define MAX98390_HDA_DSP_GLOBAL_EN BIT(0) +#define MAX98390_HDA_DSMIG_EQ8_EN BIT(5) +#define MAX98390_HDA_DSM_VOL_MINUS_7_5DB 0x91 +#define MAX98390_HDA_AMP_OFF_DELAY_US (50 * USEC_PER_MSEC) +#define MAX98390_HDA_BIQUAD_COEFS 5 +#define MAX98390_HDA_COEF_STRIDE (DSM_EQ_BQ1_B1_BYTE0 - DSM_EQ_BQ1_B0_BYTE0) +#define MAX98390_HDA_BIQUAD_STRIDE (DSM_EQ_BQ2_B0_BYTE0 - DSM_EQ_BQ1_B0_BYTE0) + +struct max98390_hda_priv { + struct device *dev; + struct regmap *regmap; + const struct firmware *dsm_fw; + struct mutex lock; /* Protects amp enable state, playback_started and dsm_fw */ + ktime_t global_disable_time; + int index; + unsigned int channel; + bool is_tweeter; + bool playback_started; +}; + +static const struct reg_sequence max98390_hda_init_regs[] = { + { MAX98390_CLK_MON, 0x6f }, + { MAX98390_DAT_MON, 0x00 }, + { MAX98390_PWR_GATE_CTL, 0x00 }, + { MAX98390_PCM_RX_EN_A, 0x03 }, + { MAX98390_ENV_TRACK_VOUT_HEADROOM, 0x0e }, + { MAX98390_BOOST_BYPASS1, 0x46 }, + { MAX98390_FET_SCALING3, 0x03 }, + { MAX98390_PCM_MODE_CFG, 0xc0 }, + { MAX98390_PCM_MASTER_MODE, 0x1c }, + { MAX98390_PCM_CLK_SETUP, 0x44 }, + { MAX98390_PCM_SR_SETUP, 0x08 }, + { MAX98390_R23FF_GLOBAL_EN, 0x00 }, + { MAX98390_R203A_AMP_EN, MAX98390_HDA_AMP_EN_DEM }, + { MAX98390_R23E1_DSP_GLOBAL_EN, 0x00 }, +}; + +/* 2nd-order Butterworth high-pass at 100 Hz */ +static const u32 max98390_hda_hpf_100hz[MAX98390_HDA_BIQUAD_COEFS] = { + 0x0fda43, 0xe04b7a, 0x0fda43, 0xe04bd3, 0x0fb4df, +}; + +/* 2nd-order Butterworth high-pass at 3 kHz */ +static const u32 max98390_hda_hpf_3khz[MAX98390_HDA_BIQUAD_COEFS] = { + 0x0c1cfc, 0xe7c608, 0x0c1cfc, 0xe8bb6b, 0x092f5c, +}; + +static const u32 max98390_hda_unity[MAX98390_HDA_BIQUAD_COEFS] = { + 0x100000, 0, 0, 0, 0, +}; + +static int max98390_hda_set_global_enable(struct max98390_hda_priv *priv, bool enable) +{ + if (!enable) + priv->global_disable_time = ktime_get(); + + return regmap_write(priv->regmap, MAX98390_R23FF_GLOBAL_EN, + enable ? MAX98390_GLOBAL_EN_MASK : 0); +} + +static int max98390_hda_set_speaker_enable(struct max98390_hda_priv *priv, bool enable) +{ + unsigned int val = MAX98390_HDA_AMP_EN_DEM; + s64 elapsed_us; + + if (enable) { + val |= MAX98390_AMP_EN_MASK; + } else { + elapsed_us = ktime_us_delta(ktime_get(), priv->global_disable_time); + if (elapsed_us < MAX98390_HDA_AMP_OFF_DELAY_US) + fsleep(MAX98390_HDA_AMP_OFF_DELAY_US - elapsed_us); + } + + return regmap_write(priv->regmap, MAX98390_R203A_AMP_EN, val); +} + +static int max98390_hda_set_amp_enable(struct max98390_hda_priv *priv, bool enable) +{ + int ret; + + if (enable) { + ret = max98390_hda_set_speaker_enable(priv, true); + if (ret) + return ret; + + return max98390_hda_set_global_enable(priv, true); + } + + ret = max98390_hda_set_global_enable(priv, false); + if (ret) + return ret; + + return max98390_hda_set_speaker_enable(priv, false); +} + +static void max98390_hda_pre_playback_hook(struct device *dev, int action) +{ + struct max98390_hda_priv *priv = dev_get_drvdata(dev); + int ret; + + if (action != HDA_GEN_PCM_ACT_CLOSE) + return; + + guard(mutex)(&priv->lock); + + priv->playback_started = false; + ret = max98390_hda_set_global_enable(priv, false); + if (ret) + dev_err(dev, "Failed to disable amp: %d\n", ret); +} + +static void max98390_hda_playback_hook(struct device *dev, int action) +{ + struct max98390_hda_priv *priv = dev_get_drvdata(dev); + int ret; + + guard(mutex)(&priv->lock); + + switch (action) { + case HDA_GEN_PCM_ACT_OPEN: + priv->playback_started = true; + ret = max98390_hda_set_amp_enable(priv, true); + break; + case HDA_GEN_PCM_ACT_CLOSE: + ret = max98390_hda_set_speaker_enable(priv, false); + break; + default: + return; + } + + if (ret) + dev_err(dev, "Failed to set amp state: %d\n", ret); +} + +static int max98390_hda_check_dsm_fw(const struct firmware *fw) +{ + unsigned int start, size; + + if (fw->size < MAX98390_DSM_PAYLOAD_OFFSET) + return -EINVAL; + + start = get_unaligned_le16(fw->data); + size = get_unaligned_le16(fw->data + 2); + + if (!size || start < MAX98390_PWR_GATE_CTL || start + size - 1 > DSMIG_EN || + fw->size < MAX98390_DSM_PAYLOAD_OFFSET + size) + return -EINVAL; + + return 0; +} + +static void max98390_hda_get_dsm_fw(struct max98390_hda_priv *priv, struct hda_codec *codec) +{ + const struct firmware *fw; + int ret; + + char *name __free(kfree) = kasprintf(GFP_KERNEL, "maxim/max98390-hda-%08x-%d.bin", + codec->core.subsystem_id, priv->index); + if (!name) + return; + + ret = firmware_request_nowarn(&fw, name, priv->dev); + if (ret) { + dev_dbg(priv->dev, "%s not found, using default tuning\n", name); + return; + } + + if (max98390_hda_check_dsm_fw(fw)) { + dev_err(priv->dev, "Invalid %s, using default tuning\n", name); + release_firmware(fw); + return; + } + + dev_info(priv->dev, "Loaded %s\n", name); + priv->dsm_fw = fw; +} + +static int max98390_hda_set_biquad(struct max98390_hda_priv *priv, unsigned int reg, + const u32 *coefs) +{ + u8 buf[3]; + int ret; + int i; + + for (i = 0; i < MAX98390_HDA_BIQUAD_COEFS; i++) { + put_unaligned_le24(coefs[i], buf); + ret = regmap_bulk_write(priv->regmap, reg + i * MAX98390_HDA_COEF_STRIDE, + buf, sizeof(buf)); + if (ret) + return ret; + } + + return 0; +} + +static int max98390_hda_set_default_dsm_params(struct max98390_hda_priv *priv) +{ + const u32 *hpf = priv->is_tweeter ? max98390_hda_hpf_3khz : max98390_hda_hpf_100hz; + unsigned int reg; + int ret; + + ret = max98390_hda_set_biquad(priv, DSM_EQ_BQ1_B0_BYTE0, hpf); + if (ret) + return ret; + + for (reg = DSM_EQ_BQ2_B0_BYTE0; reg <= DSM_EQ_BQ8_B0_BYTE0; + reg += MAX98390_HDA_BIQUAD_STRIDE) { + ret = max98390_hda_set_biquad(priv, reg, max98390_hda_unity); + if (ret) + return ret; + } + + ret = regmap_write(priv->regmap, DSM_VOL_CTRL, MAX98390_HDA_DSM_VOL_MINUS_7_5DB); + if (ret) + return ret; + + return regmap_write(priv->regmap, DSMIG_EN, MAX98390_HDA_DSMIG_EQ8_EN); +} + +static int max98390_hda_set_dsm_params(struct max98390_hda_priv *priv) +{ + const u8 *data; + int ret; + + ret = max98390_hda_set_default_dsm_params(priv); + if (ret) + return ret; + + if (priv->dsm_fw) { + data = priv->dsm_fw->data; + ret = regmap_bulk_write(priv->regmap, get_unaligned_le16(data), + data + MAX98390_DSM_PAYLOAD_OFFSET, + get_unaligned_le16(data + 2)); + if (ret) + return ret; + } + + return regmap_write(priv->regmap, MAX98390_R23E1_DSP_GLOBAL_EN, MAX98390_HDA_DSP_GLOBAL_EN); +} + +static int max98390_hda_init(struct max98390_hda_priv *priv) +{ + int ret; + + ret = regmap_write(priv->regmap, MAX98390_SOFTWARE_RESET, MAX98390_SOFT_RESET_MASK); + if (ret) + return ret; + + msleep(20); + + ret = regmap_multi_reg_write(priv->regmap, max98390_hda_init_regs, + ARRAY_SIZE(max98390_hda_init_regs)); + if (ret) + return ret; + + ret = regmap_write(priv->regmap, MAX98390_PCM_CH_SRC_1, + priv->channel << MAX98390_PCM_RX_CH_SRC_SHIFT); + if (ret) + return ret; + + return max98390_hda_set_dsm_params(priv); +} + +static int max98390_hda_bind(struct device *dev, struct device *master, void *master_data) +{ + struct max98390_hda_priv *priv = dev_get_drvdata(dev); + struct hda_component_parent *parent = master_data; + struct hda_component *comp; + int ret; + + comp = hda_component_from_index(parent, priv->index); + if (!comp) + return -EINVAL; + + if (comp->dev) + return -EBUSY; + + scoped_guard(mutex, &priv->lock) { + max98390_hda_get_dsm_fw(priv, parent->codec); + ret = max98390_hda_init(priv); + if (ret) { + release_firmware(priv->dsm_fw); + priv->dsm_fw = NULL; + return ret; + } + } + + comp->dev = dev; + strscpy(comp->name, dev_name(dev), sizeof(comp->name)); + comp->pre_playback_hook = max98390_hda_pre_playback_hook; + comp->playback_hook = max98390_hda_playback_hook; + + return 0; +} + +static void max98390_hda_unbind(struct device *dev, struct device *master, void *master_data) +{ + struct max98390_hda_priv *priv = dev_get_drvdata(dev); + struct hda_component_parent *parent = master_data; + struct hda_component *comp; + + comp = hda_component_from_index(parent, priv->index); + if (comp && comp->dev == dev) + memset(comp, 0, sizeof(*comp)); + + guard(mutex)(&priv->lock); + + release_firmware(priv->dsm_fw); + priv->dsm_fw = NULL; +} + +static const struct component_ops max98390_hda_comp_ops = { + .bind = max98390_hda_bind, + .unbind = max98390_hda_unbind, +}; + +int max98390_hda_probe(struct device *dev, int index, struct regmap *regmap) +{ + struct max98390_hda_priv *priv; + unsigned int rev_id; + int ret; + + if (IS_ERR(regmap)) + return dev_err_probe(dev, PTR_ERR(regmap), "Failed to allocate regmap\n"); + + priv = devm_kzalloc_obj(dev, *priv); + if (!priv) + return -ENOMEM; + + ret = devm_mutex_init(dev, &priv->lock); + if (ret) + return ret; + + priv->dev = dev; + priv->regmap = regmap; + priv->index = index; + priv->channel = index % 2; + priv->is_tweeter = index >= MAX98390_HDA_NUM_AMPS / 2; + dev_set_drvdata(dev, priv); + + ret = regmap_read(regmap, MAX98390_R24FF_REV_ID, &rev_id); + if (ret) + return dev_err_probe(dev, ret, "Failed to read revision ID\n"); + + dev_dbg(dev, "Revision ID 0x%02x\n", rev_id); + + return component_add(dev, &max98390_hda_comp_ops); +} +EXPORT_SYMBOL_NS_GPL(max98390_hda_probe, "SND_HDA_SCODEC_MAX98390"); + +void max98390_hda_remove(struct device *dev) +{ + struct max98390_hda_priv *priv = dev_get_drvdata(dev); + + component_del(dev, &max98390_hda_comp_ops); + + guard(mutex)(&priv->lock); + + if (priv->playback_started) + max98390_hda_set_amp_enable(priv, false); +} +EXPORT_SYMBOL_NS_GPL(max98390_hda_remove, "SND_HDA_SCODEC_MAX98390"); + +static int max98390_hda_system_suspend(struct device *dev) +{ + struct max98390_hda_priv *priv = dev_get_drvdata(dev); + int ret; + + guard(mutex)(&priv->lock); + + if (!priv->playback_started) + return 0; + + ret = max98390_hda_set_amp_enable(priv, false); + if (ret) + dev_err(dev, "Failed to disable amp: %d\n", ret); + + return 0; +} + +static int max98390_hda_system_resume(struct device *dev) +{ + struct max98390_hda_priv *priv = dev_get_drvdata(dev); + int ret; + + guard(mutex)(&priv->lock); + + ret = max98390_hda_init(priv); + if (ret) + return ret; + + if (priv->playback_started) + return max98390_hda_set_amp_enable(priv, true); + + return 0; +} + +EXPORT_NS_GPL_DEV_SLEEP_PM_OPS(max98390_hda_pm_ops, SND_HDA_SCODEC_MAX98390) = { + SYSTEM_SLEEP_PM_OPS(max98390_hda_system_suspend, max98390_hda_system_resume) +}; + +MODULE_DESCRIPTION("HDA MAX98390 side codec library"); +MODULE_AUTHOR("Kevin Cuperus "); +MODULE_AUTHOR("Stephan Lensky "); +MODULE_LICENSE("GPL"); diff --git a/sound/hda/codecs/side-codecs/max98390_hda.h b/sound/hda/codecs/side-codecs/max98390_hda.h new file mode 100644 index 000000000000..9dc7e056d4f4 --- /dev/null +++ b/sound/hda/codecs/side-codecs/max98390_hda.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * MAX98390 HDA driver + */ + +#ifndef __MAX98390_HDA_H__ +#define __MAX98390_HDA_H__ + +struct device; +struct dev_pm_ops; +struct regmap; + +int max98390_hda_probe(struct device *dev, int index, struct regmap *regmap); +void max98390_hda_remove(struct device *dev); + +extern const struct dev_pm_ops max98390_hda_pm_ops; + +#endif /* __MAX98390_HDA_H__ */ diff --git a/sound/hda/codecs/side-codecs/max98390_hda_i2c.c b/sound/hda/codecs/side-codecs/max98390_hda_i2c.c new file mode 100644 index 000000000000..c628d0c4ab9c --- /dev/null +++ b/sound/hda/codecs/side-codecs/max98390_hda_i2c.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// MAX98390 HDA I2C driver +// + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "max98390_hda.h" + +static const struct regmap_config max98390_hda_regmap = { + .reg_bits = 16, + .val_bits = 8, + .max_register = MAX98390_R24FF_REV_ID, + .cache_type = REGCACHE_NONE, +}; + +static int max98390_hda_i2c_probe(struct i2c_client *clt) +{ + const char *index_suffix; + int index; + int ret; + + if (!strstr(dev_name(&clt->dev), "MAX98390")) + return -ENODEV; + + index_suffix = strrchr(dev_name(&clt->dev), '.'); + if (!index_suffix) + return dev_err_probe(&clt->dev, -EINVAL, "Missing index in %s\n", + dev_name(&clt->dev)); + + ret = kstrtoint(index_suffix + 1, 10, &index); + if (ret) + return dev_err_probe(&clt->dev, ret, "Failed to parse index from %s\n", + dev_name(&clt->dev)); + + return max98390_hda_probe(&clt->dev, index, + devm_regmap_init_i2c(clt, &max98390_hda_regmap)); +} + +static void max98390_hda_i2c_remove(struct i2c_client *clt) +{ + max98390_hda_remove(&clt->dev); +} + +static const struct i2c_device_id max98390_hda_i2c_id[] = { + { .name = "max98390-hda" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, max98390_hda_i2c_id); + +static const struct acpi_device_id max98390_acpi_hda_match[] = { + { .id = "MAX98390" }, + { } +}; +MODULE_DEVICE_TABLE(acpi, max98390_acpi_hda_match); + +static struct i2c_driver max98390_hda_i2c_driver = { + .driver = { + .name = "max98390-hda", + .acpi_match_table = max98390_acpi_hda_match, + .pm = pm_sleep_ptr(&max98390_hda_pm_ops), + }, + .id_table = max98390_hda_i2c_id, + .probe = max98390_hda_i2c_probe, + .remove = max98390_hda_i2c_remove, +}; +module_i2c_driver(max98390_hda_i2c_driver); + +MODULE_DESCRIPTION("HDA MAX98390 I2C driver"); +MODULE_IMPORT_NS("SND_HDA_SCODEC_MAX98390"); +MODULE_AUTHOR("Kevin Cuperus "); +MODULE_AUTHOR("Stephan Lensky "); +MODULE_LICENSE("GPL"); -- 2.55.0