From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 245AE3B42EB for ; Sun, 12 Jul 2026 19:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783884195; cv=none; b=LTd9huTuqi9bLrqTQbN9aMBItXLINaT2hfGi0bbVjEuX8Z+rPRCkcjBBDezvXZnzLIbyPUfLCgtEcjr9YiZH8fEqlK8uRGfK4NbNO/ttXCF365wW9sk7ug8kgN8iu7eH0n6a0uAQM2MB39e1slxY5ggc76E13FfpXHPSRJYhDWI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783884195; c=relaxed/simple; bh=5q6YxEVN0KzzheLgKA2Qge1J12BftADl1Dbt7PBWTOQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J3cdDYG8Yp0Rw7weNKf9S6WgB7C908yrTvDljFYSxAjVNBVVvjN3x8mnJz33U52VYwhtZw7tTppbDYM+SROZLaEewSZSc0wnUDZvstaoQ/LBk6x+RqPsPrPId3utstOG7EGxK3UV77vj+m2O3ydEcv0oEYG2aquHGSgDqcCTsGc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G4uFEeAI; arc=none smtp.client-ip=209.85.221.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G4uFEeAI" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-47d70879764so1481069f8f.2 for ; Sun, 12 Jul 2026 12:23:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783884190; x=1784488990; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=mBzQa8/wWXh8hBSAwcelh3Dx/UuRSdQbfC9fcdyS2MQ=; b=G4uFEeAIsUWTaHhCjXTWoyn8Hx2pFMkakfHxNlsSYX5Djn0XNLLlc7OI5vYkdM8Tsg e+YcBfF+YJfajNqxa95QAxabwjV8/CI1HfoeKKEiPEL5VWA0r4hvoY05LlZNUhSERNAE wxv1oUW6qtBlcZJW3wuZS/tSyGLfsb0STQJRLJilY5JMbst9rcpvwR1G5VzaVYw8Q+Nn 9ggpwCZYux9UVhX7OUEYY7G9nWWNqlt6Gb++S3vcwDOMfGkwaf7aEjZKFfQA/lipK9Nc sLYMyjd6VAoITuVnw1lbUEQsm98sRj5sCVPnxLFG5It0gjDb439qMDWhr6RliQKKldhL yd9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783884190; x=1784488990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=mBzQa8/wWXh8hBSAwcelh3Dx/UuRSdQbfC9fcdyS2MQ=; b=Rlbfrh4xf4y6KsAAXm4Tz6SPAILTxe/BDPUp5bTCfSkotwtyXEyQ2FJq3xp9wzYpl9 zO6BeVeLJIZYYb2CmV7gb5e2KHe0RPg+fqGb66BJpUWZAdNDs7B0j05zhv8deHbg8FOs U2zkUWC7GT1oBaXCYzPcmO3OWiHy508U7sujbgKMrm9VefKccSb0JpNFeiv4b2sEioOP JoPCb1TZ60TCsnfQzd9iXZgrTd4zgkfqQLeLsaSCAmkKF9669+Qfrqb7d3htk1lbFRCK kCH+iDw9qDchPE6ne032lvkAkZhHCGEdRd5N4mC7gu6uQpH3qJrpKSBecMjtbCkDTWbU IO/Q== X-Forwarded-Encrypted: i=1; AHgh+RqXzvSuhnnSr5YKYg7fULkEPTYUTZfMk47KplsW7r5saVXBQFN3yS2WRfHs+FHJpQ/X9RAZPtdZrFW5@vger.kernel.org X-Gm-Message-State: AOJu0YyVrY/VKmz4CQPYzmuaTtxO/Zji+74o2hCd7BbSVlxmgUNOhiz1 0p+gMmkf3M2EiboHdCX/CN9uH0ZRjD2AO8UuUFduBleh/dGX0iHd7ZIa X-Gm-Gg: AfdE7ckwUBFVSUu46ddDY+IQpdl+nWeN0p96JAA1hH4br16CXzrI+4fyR5kXsNbOQMM C3K5tbpAo2RQKDn3nxcDzRRsWrgvQuEb/aYS4LY749qp/huCJJ3bFy7NTUnH3IcJNDus+bCT8bn wlzyU2ghrbgpb4rUt8HabZhlp1674kRot70XN+ISi9Ft/kHcKU+W0avyXOOPWaSowm3rScbzAL1 pAM6RVzq6oK4JhCqlP5DUnajd20XxgvJ99p+eEPHy60CRnrhIbxloJ1H+8PtirjgFqcZTLK7cnK pZuUsjkXdYW6iRgEphXb/5z51vlXVB0L0k5kBdQAGScFD7LzFZoWMw/+KgMtElr6R6GcZHD1Y5o AErqlGZrghXcUG+VWeY70CIomQzn/TpFdY9Mw5WemzqTlHF17sYi/TLJovmpfD72C+UnUvY1fG3 BxsNW/M9y79N1rgVY= X-Received: by 2002:a05:6000:200b:b0:472:7dce:d8c7 with SMTP id ffacd0b85a97d-47f2dceb401mr6445872f8f.36.1783884190327; Sun, 12 Jul 2026 12:23:10 -0700 (PDT) Received: from builder ([2001:9e8:f120:f516:be24:11ff:fe30:5d85]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47df6a31dd5sm34883190f8f.16.2026.07.12.12.23.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jul 2026 12:23:10 -0700 (PDT) From: Jonas Jelonek To: Oleksij Rempel , Kory Maincent , Andrew Lunn , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Golle , =?UTF-8?q?Bj=C3=B8rn=20Mork?= , Jonas Jelonek Subject: [PATCH net-next v7 3/4] net: pse-pd: realtek-pse-mcu: add I2C transport Date: Sun, 12 Jul 2026 19:22:49 +0000 Message-ID: <20260712192251.1413279-4-jelonek.jonas@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260712192251.1413279-1-jelonek.jonas@gmail.com> References: <20260712192251.1413279-1-jelonek.jonas@gmail.com> Precedence: bulk X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add the I2C/SMBus transport for the Realtek PSE MCU core. It registers the MCU on an I2C bus and provides the send/recv callbacks the core uses to exchange the 12-byte frames. The MCU firmware expects one of two framings on the I2C bus, and which one is part of the compatible: '-smbus' (reads carry a leading command byte and a repeated start) or raw '-i2c' (bare block writes and reads). The match data flags the raw-I2C case; SMBus is the default because that's what the majority of devices uses. Because i2c_master_send()/i2c_master_recv() may DMA, the raw-I2C path bounces each frame through a heap buffer rather than the core's stack buffers; the SMBus path is unaffected. Signed-off-by: Jonas Jelonek --- drivers/net/pse-pd/Kconfig | 11 ++ drivers/net/pse-pd/Makefile | 1 + drivers/net/pse-pd/realtek-pse-mcu-i2c.c | 170 +++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 drivers/net/pse-pd/realtek-pse-mcu-i2c.c diff --git a/drivers/net/pse-pd/Kconfig b/drivers/net/pse-pd/Kconfig index 3b0c245a2bc7..6d14c8832e8b 100644 --- a/drivers/net/pse-pd/Kconfig +++ b/drivers/net/pse-pd/Kconfig @@ -19,6 +19,17 @@ config PSE_REALTEK_MCU Shared core for the Realtek PSE MCU driver. This is selected automatically by the transport options below. +config PSE_REALTEK_MCU_I2C + tristate "Realtek PSE MCU driver (I2C transport)" + depends on I2C + select PSE_REALTEK_MCU + help + Driver for the microcontroller (MCU) that fronts the PSE + hardware on various Realtek-based managed switches, attached + via I2C/SMBus. The MCU exposes a message-based protocol; the actual + PSE silicon is not accessed directly. To compile this driver as a + module, choose M here: the module will be called realtek-pse-mcu-i2c. + config PSE_REGULATOR tristate "Regulator based PSE controller" help diff --git a/drivers/net/pse-pd/Makefile b/drivers/net/pse-pd/Makefile index bf35e2a5b110..ef869bba5ed9 100644 --- a/drivers/net/pse-pd/Makefile +++ b/drivers/net/pse-pd/Makefile @@ -4,6 +4,7 @@ obj-$(CONFIG_PSE_CONTROLLER) += pse_core.o obj-$(CONFIG_PSE_REALTEK_MCU) += realtek-pse-mcu-core.o +obj-$(CONFIG_PSE_REALTEK_MCU_I2C) += realtek-pse-mcu-i2c.o obj-$(CONFIG_PSE_REGULATOR) += pse_regulator.o obj-$(CONFIG_PSE_PD692X0) += pd692x0.o obj-$(CONFIG_PSE_SI3474) += si3474.o diff --git a/drivers/net/pse-pd/realtek-pse-mcu-i2c.c b/drivers/net/pse-pd/realtek-pse-mcu-i2c.c new file mode 100644 index 000000000000..e10f8c633b68 --- /dev/null +++ b/drivers/net/pse-pd/realtek-pse-mcu-i2c.c @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include +#include +#include +#include + +#include "realtek-pse-mcu.h" + +/* + * The core has already waited RTPSE_MCU_RESPONSE_MS before calling us, so + * the response is normally ready on the very first read. For commands the + * MCU produces more slowly, keep polling at the typical response cadence + * up to the worst-case ceiling. + */ +#define RTPSE_MCU_I2C_RETRY_MS RTPSE_MCU_RESPONSE_MS +#define RTPSE_MCU_I2C_MAX_TRIES (RTPSE_MCU_RESPONSE_MAX_MS / RTPSE_MCU_I2C_RETRY_MS) + +static int rtpse_mcu_i2c_smbus_send(struct rtpse_mcu_ctrl *pse, const struct rtpse_mcu_msg *req) +{ + struct i2c_client *client = to_i2c_client(pse->dev); + + /* Send opcode as SMBus command byte; remaining 11 bytes as block data */ + return i2c_smbus_write_i2c_block_data(client, req->opcode, RTPSE_MCU_MSG_SIZE - 1, + (u8 *)req + 1); +} + +static int rtpse_mcu_i2c_smbus_recv(struct rtpse_mcu_ctrl *pse, const struct rtpse_mcu_msg *req, + struct rtpse_mcu_msg *resp) +{ + struct i2c_client *client = to_i2c_client(pse->dev); + int tries, ret; + + for (tries = 0; tries < RTPSE_MCU_I2C_MAX_TRIES; tries++) { + if (tries > 0) + msleep(RTPSE_MCU_I2C_RETRY_MS); + + /* MCU needs 0x00 as command byte for read */ + ret = i2c_smbus_read_i2c_block_data(client, 0x00, + RTPSE_MCU_MSG_SIZE, + (u8 *)resp); + if (ret < 0) + return ret; + if (ret == RTPSE_MCU_MSG_SIZE && rtpse_mcu_resp_is_final(req, resp)) + return 0; + } + + return -ETIMEDOUT; +} + +static const struct rtpse_mcu_transport_ops rtpse_mcu_i2c_smbus_ops = { + .send = rtpse_mcu_i2c_smbus_send, + .recv = rtpse_mcu_i2c_smbus_recv, +}; + +static int rtpse_mcu_i2c_native_send(struct rtpse_mcu_ctrl *pse, const struct rtpse_mcu_msg *req) +{ + struct i2c_client *client = to_i2c_client(pse->dev); + void *buf; + int ret; + + /* i2c_master_send() may DMA, so the buffer must not be on the stack. */ + buf = kmemdup(req, RTPSE_MCU_MSG_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + ret = i2c_master_send(client, buf, RTPSE_MCU_MSG_SIZE); + kfree(buf); + if (ret < 0) + return ret; + return ret == RTPSE_MCU_MSG_SIZE ? 0 : -EIO; +} + +static int rtpse_mcu_i2c_native_recv(struct rtpse_mcu_ctrl *pse, const struct rtpse_mcu_msg *req, + struct rtpse_mcu_msg *resp) +{ + struct i2c_client *client = to_i2c_client(pse->dev); + int tries, ret; + u8 *buf; + + /* i2c_master_recv() may DMA, so read into an off-stack buffer. */ + buf = kmalloc(RTPSE_MCU_MSG_SIZE, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + for (tries = 0; tries < RTPSE_MCU_I2C_MAX_TRIES; tries++) { + if (tries > 0) + msleep(RTPSE_MCU_I2C_RETRY_MS); + + ret = i2c_master_recv(client, buf, RTPSE_MCU_MSG_SIZE); + if (ret < 0) + goto out; + if (ret == RTPSE_MCU_MSG_SIZE) { + memcpy(resp, buf, RTPSE_MCU_MSG_SIZE); + if (rtpse_mcu_resp_is_final(req, resp)) { + ret = 0; + goto out; + } + } + } + ret = -ETIMEDOUT; +out: + kfree(buf); + return ret; +} + +static const struct rtpse_mcu_transport_ops rtpse_mcu_i2c_native_ops = { + .send = rtpse_mcu_i2c_native_send, + .recv = rtpse_mcu_i2c_native_recv, +}; + +static int rtpse_mcu_i2c_probe(struct i2c_client *client) +{ + struct device *dev = &client->dev; + const struct rtpse_mcu_match_data *match; + struct rtpse_mcu_ctrl *pse; + bool use_native; + + match = device_get_match_data(dev); + if (!match) + return dev_err_probe(dev, -ENODEV, "missing match data\n"); + + /* The framing (raw I2C vs SMBus) is carried by the match data. */ + use_native = match->native_i2c; + if (use_native) { + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) + return dev_err_probe(dev, -EOPNOTSUPP, + "plain-I2C MCU protocol requires I2C-capable adapter\n"); + } else { + if (!i2c_check_functionality(client->adapter, + I2C_FUNC_SMBUS_WRITE_I2C_BLOCK | + I2C_FUNC_SMBUS_READ_I2C_BLOCK)) + return dev_err_probe(dev, -EOPNOTSUPP, + "SMBus MCU protocol requires SMBus I2C-block support\n"); + } + + pse = devm_kzalloc(dev, sizeof(*pse), GFP_KERNEL); + if (!pse) + return -ENOMEM; + + pse->dev = dev; + pse->pcdev.owner = THIS_MODULE; + pse->transport = use_native ? &rtpse_mcu_i2c_native_ops : &rtpse_mcu_i2c_smbus_ops; + + return rtpse_mcu_register(pse); +} + +static const struct of_device_id rtpse_mcu_i2c_of_match[] = { + { .compatible = "realtek,pse-mcu-gen1-smbus", .data = &rtpse_mcu_gen1_data }, + { .compatible = "realtek,pse-mcu-gen2-smbus", .data = &rtpse_mcu_gen2_data }, + { .compatible = "realtek,pse-mcu-gen2-i2c", .data = &rtpse_mcu_gen2_i2c_data }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, rtpse_mcu_i2c_of_match); + +static struct i2c_driver rtpse_mcu_i2c_driver = { + .driver = { + .name = "realtek-pse-mcu-i2c", + .of_match_table = rtpse_mcu_i2c_of_match, + }, + .probe = rtpse_mcu_i2c_probe, +}; +module_i2c_driver(rtpse_mcu_i2c_driver); + +MODULE_AUTHOR("Jonas Jelonek "); +MODULE_DESCRIPTION("Realtek PSE MCU driver (I2C transport)"); +MODULE_LICENSE("GPL"); -- 2.53.0