From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=46.4.11.11 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796234686; b=KquR3upZ4v9L9ji13RoSiqkyAHq1qUvpjzZJoqf6GXxvRjwbu8TkkS1Atv+SToTlXQ6a gi+UlM25WcCqOeLTQ91hpnWX4/J+g8XKDJKh/NEDljYLeyv7IPhSncv75y55CiZtnNbSS P8dNUfBPIPTu5Ow5lwdRByA9AugT5EUZnAAFA9WI6ztcdW9GvV56khkdtUA/V1Evrj/H1 fW2pWOdlXXmAoRPqH/jvhtJHeQT/ZDcT9mtWwRcCKmHTOwmaWEJJFuMmf3mMs5nEWldvE teS0/lJ5mMysii6bzIoDhLob8gAXaJ8RjvulhVUWQFsJe/tD8GO2SLbGB+41yky+tJQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796234686; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-Id:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=aIGvAn8WrdU9JDvXfl+/AQKxF15m6uL6uKyrPtEW7kM=; b=K/uMixuYg3lI9t0oinW8mTemjcbpsDD8N8gVkDgTgzGzSpqhQuYqwvusmtK1kStxrXqa gOkzg9iRyMw35xQ+sjs4a7QhbRC5hmelYlZ8dyqU0VPRcycf/VZ1/UwvSSl92HFL6qqG0 BWnyw+DVXWJz5F8daEibDpq7W62p8bmypLl/R+iC6Ak0W9Tgkb1YLq1FY1COp7oa21cd5 7pJVue/TykCTPPnEWEM9JL27TOm6Zivj5sc3UihqmJqAJ+QS9cioTppFW/7u7HloQWYei qLOk7VVM7kixO82DlbdvulzwWIrAo64P7pLmYuzGn2iTvJLk3FEotc6nh7e/Chlv7fQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=phrozen.org; spf=pass smtp.mailfrom=phrozen.org; arc=none smtp.remote-ip=46.4.11.11 Received: from nbd.name (nbd.name [46.4.11.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 208DC606A330; Wed, 2 Dec 2026 18:04:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 208DC606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=phrozen.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=phrozen.org Received: from [2a04:4540:1404:c8fc:172c:e560:a116:623] (helo=bertha10..) by ds12 with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1xYoh1-00BMWx-2y; Wed, 02 Dec 2026 19:04:35 +0100 From: John Crispin To: Stefan Roese , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Matthias Brugger , AngeloGioacchino Del Regno , Philipp Zabel Cc: Christian Marangi , devicetree@vger.kernel.org, lei.dong@airoha.com, Linqi.zhang@airoha.com, linux-arm-kernel@lists.infradead.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org, Rajeev.kumar@airoha.com, upstream@airoha.com, Weifeng.xu@airoha.com, zhengping.zhang@airoha.com Subject: [PATCH v5 3/3] i2c: mt7621: add support for Airoha AN7589 Date: Wed, 2 Dec 2026 19:04:30 +0100 Message-Id: <20261202180430.3572052-4-john@phrozen.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261202180430.3572052-1-john@phrozen.org> References: <20261202180430.3572052-1-john@phrozen.org> X-Mailing-List: linux-kernel@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 The Airoha AN7589 carries this controller with a second control register at 0x68. It holds a 16 bit clock divider selected by SM0CTL2_SCL_DIV_SEL, wider than the 11 bit field in SM0CTL0, and SDA wait control. Like the AN7581, the AN7589 runs without SCL stretching. Add match data for the AN7589 with a flag for the extended divider. The extended divider is set up before the controller is enabled, and the SM0CTL0 divider stays zero. With the extended divider and the 200 MHz clock the bus runs from fast mode plus down to about 3 kHz, where the SM0CTL0 divider stops at 97.7 kHz. Signed-off-by: John Crispin --- Changes in v5: - Rebased on next-20261201, which has commit 036099c2e588 ("i2c: mt7621: limit SCL_STRETCH only to Mediatek SoC"). The match data and the SCL stretch flag come from that commit now, so this patch only adds the flag for the extended divider and the AN7589 entry. Changes in v4: - Read the clock rate after the declarations (Andi). - Fail the probe for a clock rate of 0. The divider wrapped before (Andi). - Round the AN7589 divider up, so the bus never runs faster than clock-frequency. The dividers for 100 kHz, 400 kHz and 1 MHz with the 200 MHz clock do not change (Andi). - Link to the review: https://lore.kernel.org/r/aw7RHNwER6VvW7Is@zenone.zhora.eu Changes in v3: - Cap the bus rate at 1 MHz instead of the divider from below, so the limits are constant and a clock rate below 1 MHz cannot wrap the divider. Same divider for every valid rate (kernel test robot, sashiko). - Link to the report: https://lore.kernel.org/oe-kbuild-all/202611140348.p9TnfxBF-lkp@intel.com/ Changes in v2: - The extended divider supports bus rates down to about 3 kHz with the 200 MHz clock. The SM0CTL0 divider stops at 97.7 kHz. - Set up the extended divider before the controller is enabled. The SM0CTL0 divider stays zero. drivers/i2c/busses/i2c-mt7621.c | 52 ++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/drivers/i2c/busses/i2c-mt7621.c b/drivers/i2c/busses/i2c-mt7621.c index 67b053d3f57e..a0da5e522288 100644 --- a/drivers/i2c/busses/i2c-mt7621.c +++ b/drivers/i2c/busses/i2c-mt7621.c @@ -10,6 +10,7 @@ * (C) 2014 Sittisak */ +#include #include #include #include @@ -18,6 +19,7 @@ #include #include #include +#include #include #define REG_SM0CFG2_REG 0x28 @@ -28,6 +30,7 @@ #define REG_PINTEN_REG 0x5c #define REG_PINTST_REG 0x60 #define REG_PINTCL_REG 0x64 +#define REG_SM0CTL2_REG 0x68 /* REG_SM0CFG2_REG */ #define SM0CFG2_IS_AUTOMODE BIT(0) @@ -54,11 +57,18 @@ #define SM0CTL1_MODE_MASK (0x7 << 4) #define SM0CTL1_TRI BIT(0) +/* REG_SM0CTL2_REG */ +#define SM0CTL2_DATA_WAIT_SDA BIT(18) +#define SM0CTL2_INIT_WAIT_SDA BIT(17) +#define SM0CTL2_SCL_DIV_SEL BIT(16) +#define SM0CTL2_CLK_DIV_EXT GENMASK(15, 0) + /* timeout waiting for I2C devices to respond */ #define TIMEOUT_MS 1000 struct mtk_i2c_data { bool have_scl_stretch; + bool ext_clk_div; }; struct mtk_i2c { @@ -102,7 +112,13 @@ static void mtk_i2c_reset(struct mtk_i2c *i2c) * Don't set SM0CTL0_ODRAIN as its bit meaning is inverted. To * configure open-drain mode, this bit needs to be cleared. */ - reg = ((i2c->clk_div << 16) & SM0CTL0_CLK_DIV_MASK) | SM0CTL0_EN; + reg = SM0CTL0_EN; + if (i2c->data->ext_clk_div) + iowrite32(SM0CTL2_SCL_DIV_SEL | SM0CTL2_DATA_WAIT_SDA | + FIELD_PREP(SM0CTL2_CLK_DIV_EXT, i2c->clk_div), + i2c->base + REG_SM0CTL2_REG); + else + reg |= (i2c->clk_div << 16) & SM0CTL0_CLK_DIV_MASK; if (i2c->data->have_scl_stretch) reg |= SM0CTL0_SCL_STRETCH; @@ -270,23 +286,41 @@ static const struct mtk_i2c_data i2c_an7581_data = { .have_scl_stretch = false, }; +static const struct mtk_i2c_data i2c_an7589_data = { + .have_scl_stretch = false, + .ext_clk_div = true, +}; + static const struct of_device_id i2c_mtk_dt_ids[] = { { .compatible = "mediatek,mt7621-i2c", .data = &i2c_mt7621_data }, { .compatible = "airoha,an7581-i2c", .data = &i2c_an7581_data }, + { .compatible = "airoha,an7589-i2c", .data = &i2c_an7589_data }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, i2c_mtk_dt_ids); -static void mtk_i2c_init(struct mtk_i2c *i2c) +static int mtk_i2c_init(struct mtk_i2c *i2c) { - i2c->clk_div = clk_get_rate(i2c->clk) / i2c->bus_freq - 1; - if (i2c->clk_div < 99) - i2c->clk_div = 99; - if (i2c->clk_div > SM0CTL0_CLK_DIV_MAX) - i2c->clk_div = SM0CTL0_CLK_DIV_MAX; + u32 bus_freq = i2c->bus_freq; + unsigned long rate; + + rate = clk_get_rate(i2c->clk); + if (!rate) + return -EINVAL; + + if (i2c->data->ext_clk_div) { + bus_freq = min(bus_freq, I2C_MAX_FAST_MODE_PLUS_FREQ); + i2c->clk_div = min(DIV_ROUND_UP_ULL(rate, bus_freq) - 1, + FIELD_MAX(SM0CTL2_CLK_DIV_EXT)); + } else { + i2c->clk_div = clamp(rate / bus_freq - 1, 99, + SM0CTL0_CLK_DIV_MAX); + } mtk_i2c_reset(i2c); + + return 0; } static int mtk_i2c_probe(struct platform_device *pdev) @@ -337,7 +371,9 @@ static int mtk_i2c_probe(struct platform_device *pdev) platform_set_drvdata(pdev, i2c); - mtk_i2c_init(i2c); + ret = mtk_i2c_init(i2c); + if (ret) + return dev_err_probe(&pdev->dev, ret, "Invalid clock rate\n"); ret = i2c_add_adapter(adap); if (ret < 0) -- 2.34.1