From patchwork Tue Jan 14 03:30:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 857552 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 0B1A32327BA; Tue, 14 Jan 2025 03:30:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736825432; cv=none; b=W6nSHtS/HFo9Jh6341K/XqwP4oWnAQtOtsDYXY76MCltgfYTOXfJl+UjMNg/A8DxTHgYtxmrjwSI5g2xKN622lflDgNR7BKPjqhuBvhasPAqHe2X48UBRyOjQnkIBTd30nC2kkTyvP8flzMx25l0TtF1NOzzNEzQ1rwZENghOYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736825432; c=relaxed/simple; bh=FyLZP44wzf268k/gzpAv+g8jf7O7Gl6T9OZVPXmRje4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Iotot8enTILkqBFINX4v73Kit0gD7weg9MkfnX3BgF4e6GA5fEScdLBGCU93NtDWKJ9bBYWXAsu662rgFRu4AuH6ygngv/dP61NYT3BcRzDKg+fcnISnG0+afhAzdJOnh4H+IAtcAsV4OwKdRqrox/w/cK9c3ZQPOHauXNNJqlo= 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=VP55fOSX; arc=none smtp.client-ip=209.85.214.169 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="VP55fOSX" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2166022c5caso77531335ad.2; Mon, 13 Jan 2025 19:30:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736825429; x=1737430229; 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; bh=UWiNB3BPTz8igjMWY9ghFhLX5KVkbNiReQ8lnKAyDfU=; b=VP55fOSXUqSNp+mIqgVubCzzmhT8QwKMsZ2BK1hu+lu4tmxkJv8hQHGsnqln36+uij eVqJJhB76ZwlkxZ1KmcPtFM/N8WpcTVpPJIFoNfnJZvEqBbfOB72wSnQgY/R8ZDaq+Nf kyPSUgNQvbHgF76hjXEiVTlVtBMc86Q1SBZL1mK8Cy8wY8Y6khyp+6wRgKopKJnb8q/U CAuhQ0PHFwx0Rgt8CEKQe2yjOu+HiVVQGAva9Yt/OPxoLp8g1EfwGrEw4bcGo0Vr4AXY hLhODPbn2B9rVj4BppQ72ju9/o2cd8X3v7iZZDUxVMXiRBw5WnXmA8+8dJoDqRVG42SZ kwAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736825429; x=1737430229; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UWiNB3BPTz8igjMWY9ghFhLX5KVkbNiReQ8lnKAyDfU=; b=S0BVEk4T4j1LGn+b0OGJfzO77Os+qiiY+xZxTLiFRfdDLaOHsdzli1QYmP6k02HC8L ZQWM5/F1og1CLGcB6DAQpWv0MVl9K2vN/dsuwe4IHhxaOXzQPAcVo6pHsRUU2X8qWrGt hWwGWn4sBhpf2hW473qmBI4QBSOyjJV1E1N5bz6IzKzVxfVCuW4kkjkZltTX3Xxav4G2 212/mZ9CALUnr8vuFDwR5Iz59tFlFg10SVtBzs59Ti69rK/mKfGbxuy7XngCWeB9QV9w uPz4yZb8Gd2XEFubS+s5gFKRqM31kUpsCWfTb7lAS0iZwO/qzZEPU0KAG8JdWMzW5jCK e43w== X-Forwarded-Encrypted: i=1; AJvYcCUEZALcYbSHuBqORdQDzgwDTJ1p1FqqcCZAibjTXduEO50YIk0iWCnqmhpuJwy5afaW5PKH96hqO0Q1i34=@vger.kernel.org, AJvYcCUQUUaD/33uu1NnexPZN0BiWp0WXHVACvteHRvgleDNJlTvWha/lpg5DSn3/yesAaukwe9QXeQdEuHt8Q==@vger.kernel.org, AJvYcCUYp3XoXZSf9qF2T2vU8/N1HGDVfNZwhz+SxPEzt1XsNBM/octASIH04jYj/QQ/SLMOGaeQ750qr4eL@vger.kernel.org, AJvYcCUpM14BoWARxNC5dGA+ECQ9+pxuPBqsbsZi2Qe9JiMVvMrmFNY9ox9OOGcbyJETIMP3Thvdtw0NsCGj@vger.kernel.org, AJvYcCVD41FiQIsRGn+JHy/s9ZBWxQi+OFmKhi0v6lLHc04JgujQaCy2lnNaAiTolUmPQjbPV5AAKOv0KQaLuzGs1qc=@vger.kernel.org, AJvYcCVN4Ie4BESqPBOWtkdPJ6Q8BPqugiv38K6cMcJybyie2+S46Pj++DyBnoVbaBxkHnOpaKw7tnkTggIj@vger.kernel.org, AJvYcCVm1wpBmqp1hvndAeuhbP2vU86aG8sEiwQeCUG2pXPSFw4O8T4e9lgJ3CWNym4IKvNl/PqP3n+5@vger.kernel.org, AJvYcCXGVzRKAc+DWrlO4iQFwD1g8OlsMXt6RKKQkT3nNQqo9Qw6MzJtzmMmdpZN/HmDh+Rvpb0xT6r03eU=@vger.kernel.org X-Gm-Message-State: AOJu0YyDY7dNvVW05ptd2cMSV0t/shzrT4F07qJZGydPk1HUTuBTF1HR vB7mJQ6awfRD3UxQC717/r4fgEa23W8J24vlU7GNlJ7Y7tKXuODH X-Gm-Gg: ASbGnctkBeO4PBg8mJQ51bSDO42mqKMXUtefP+ouaeO9f4Op3Qdt07wA5Q/KTSvzVLG HRE+zwKvxsuFrq/2MGki5sUYb8/R4/stSZMLtEaGUF1uV6MLqLYhehvy4I3mXInCEYHDMtUIGtG bXn23Ia1UU4HYcZugEd8d+sf0W4YZ4DfUgrFttNu4JNmLVqc/YsqtWEL959CCFo3+WMzZB2VVtX KliJ+rshNHW1Dxugr6Q/HqhztsJmBXCMgmvJds9ttCcPEYYpeBVonlMprv+OP6cNwp4cEV8CzIT kzBB8gxN8clFuNvK+2RuTXfu X-Google-Smtp-Source: AGHT+IGyW6Fa4e2V3MXi9F5xL6B9vilyejunMbp8VdxvH6utMH0lXtPQxoa52nMeUya2pLCdccElkw== X-Received: by 2002:a05:6a21:100c:b0:1e0:c50c:9838 with SMTP id adf61e73a8af0-1e88cf7f7b0mr38592841637.6.1736825429058; Mon, 13 Jan 2025 19:30:29 -0800 (PST) Received: from hcdev-d520mt2.. (60-250-196-139.hinet-ip.hinet.net. [60.250.196.139]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d40658b71sm6595400b3a.85.2025.01.13.19.30.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 19:30:28 -0800 (PST) From: Ming Yu To: tmyu0@nuvoton.com, lee@kernel.org, linus.walleij@linaro.org, brgl@bgdev.pl, andi.shyti@kernel.org, mkl@pengutronix.de, mailhol.vincent@wanadoo.fr, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, wim@linux-watchdog.org, linux@roeck-us.net, jdelvare@suse.com, alexandre.belloni@bootlin.com Cc: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, linux-i2c@vger.kernel.org, linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-watchdog@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-rtc@vger.kernel.org, linux-usb@vger.kernel.org, Ming Yu Subject: [PATCH v5 1/7] mfd: Add core driver for Nuvoton NCT6694 Date: Tue, 14 Jan 2025 11:30:04 +0800 Message-Id: <20250114033010.2445925-2-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250114033010.2445925-1-a0282524688@gmail.com> References: <20250114033010.2445925-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-watchdog@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The Nuvoton NCT6694 is a peripheral expander with 16 GPIO chips, 6 I2C controllers, 2 CANfd controllers, 2 Watchdog timers, ADC, PWM, and RTC. This driver implements USB device functionality and shares the chip's peripherals as a child device. Each child device can use the USB functions nct6694_read_msg() and nct6694_write_msg() to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe. Signed-off-by: Ming Yu --- MAINTAINERS | 7 + drivers/mfd/Kconfig | 10 + drivers/mfd/Makefile | 2 + drivers/mfd/nct6694.c | 388 ++++++++++++++++++++++++++++++++++++ include/linux/mfd/nct6694.h | 109 ++++++++++ 5 files changed, 516 insertions(+) create mode 100644 drivers/mfd/nct6694.c create mode 100644 include/linux/mfd/nct6694.h diff --git a/MAINTAINERS b/MAINTAINERS index a87ddad78e26..79568f82b98a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16717,6 +16717,13 @@ F: drivers/nubus/ F: include/linux/nubus.h F: include/uapi/linux/nubus.h +NUVOTON NCT6694 MFD DRIVER +M: Ming Yu +L: linux-kernel@vger.kernel.org +S: Supported +F: drivers/mfd/nct6694.c +F: include/linux/mfd/nct6694.h + NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER M: Antonino Daplas L: linux-fbdev@vger.kernel.org diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig index ae23b317a64e..3e641bfeeb38 100644 --- a/drivers/mfd/Kconfig +++ b/drivers/mfd/Kconfig @@ -1045,6 +1045,16 @@ config MFD_MENF21BMC This driver can also be built as a module. If so the module will be called menf21bmc. +config MFD_NCT6694 + tristate "Nuvoton NCT6694 support" + select MFD_CORE + depends on USB + help + This adds support for Nuvoton USB device NCT6694 sharing peripherals + This includes the USB device driver and core APIs. + Additional drivers must be enabled in order to use the functionality + of the device. + config MFD_OCELOT tristate "Microsemi Ocelot External Control Support" depends on SPI_MASTER diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile index e057d6d6faef..3c902d3704dc 100644 --- a/drivers/mfd/Makefile +++ b/drivers/mfd/Makefile @@ -121,6 +121,8 @@ obj-$(CONFIG_MFD_MC13XXX) += mc13xxx-core.o obj-$(CONFIG_MFD_MC13XXX_SPI) += mc13xxx-spi.o obj-$(CONFIG_MFD_MC13XXX_I2C) += mc13xxx-i2c.o +obj-$(CONFIG_MFD_NCT6694) += nct6694.o + obj-$(CONFIG_MFD_CORE) += mfd-core.o ocelot-soc-objs := ocelot-core.o ocelot-spi.o diff --git a/drivers/mfd/nct6694.c b/drivers/mfd/nct6694.c new file mode 100644 index 000000000000..294b6b7a902e --- /dev/null +++ b/drivers/mfd/nct6694.c @@ -0,0 +1,388 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Nuvoton NCT6694 MFD driver based on USB interface. + * + * Copyright (C) 2024 Nuvoton Technology Corp. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MFD_DEV_SIMPLE(_name) \ +{ \ + .name = NCT6694_DEV_##_name, \ +} \ + +#define MFD_DEV_WITH_ID(_name, _id) \ +{ \ + .name = NCT6694_DEV_##_name, \ + .id = _id, \ +} + +/* MFD device resources */ +static const struct mfd_cell nct6694_dev[] = { + MFD_DEV_WITH_ID(GPIO, 0x0), + MFD_DEV_WITH_ID(GPIO, 0x1), + MFD_DEV_WITH_ID(GPIO, 0x2), + MFD_DEV_WITH_ID(GPIO, 0x3), + MFD_DEV_WITH_ID(GPIO, 0x4), + MFD_DEV_WITH_ID(GPIO, 0x5), + MFD_DEV_WITH_ID(GPIO, 0x6), + MFD_DEV_WITH_ID(GPIO, 0x7), + MFD_DEV_WITH_ID(GPIO, 0x8), + MFD_DEV_WITH_ID(GPIO, 0x9), + MFD_DEV_WITH_ID(GPIO, 0xA), + MFD_DEV_WITH_ID(GPIO, 0xB), + MFD_DEV_WITH_ID(GPIO, 0xC), + MFD_DEV_WITH_ID(GPIO, 0xD), + MFD_DEV_WITH_ID(GPIO, 0xE), + MFD_DEV_WITH_ID(GPIO, 0xF), + + MFD_DEV_WITH_ID(I2C, 0x0), + MFD_DEV_WITH_ID(I2C, 0x1), + MFD_DEV_WITH_ID(I2C, 0x2), + MFD_DEV_WITH_ID(I2C, 0x3), + MFD_DEV_WITH_ID(I2C, 0x4), + MFD_DEV_WITH_ID(I2C, 0x5), + + MFD_DEV_WITH_ID(CAN, 0x0), + MFD_DEV_WITH_ID(CAN, 0x1), + + MFD_DEV_WITH_ID(WDT, 0x0), + MFD_DEV_WITH_ID(WDT, 0x1), + + MFD_DEV_SIMPLE(HWMON), + MFD_DEV_SIMPLE(RTC), +}; + +static int nct6694_response_err_handling(struct nct6694 *nct6694, + unsigned char err_status) +{ + struct device *dev = &nct6694->udev->dev; + + switch (err_status) { + case NCT6694_NO_ERROR: + return err_status; + case NCT6694_NOT_SUPPORT_ERROR: + dev_dbg(dev, "%s: Command is not supported!\n", __func__); + break; + case NCT6694_NO_RESPONSE_ERROR: + dev_dbg(dev, "%s: Command received no response!\n", __func__); + break; + case NCT6694_TIMEOUT_ERROR: + dev_dbg(dev, "%s: Command timed out!\n", __func__); + break; + case NCT6694_PENDING: + dev_dbg(dev, "%s: Command is pending!\n", __func__); + break; + default: + return -EINVAL; + } + + return -EIO; +} + +int nct6694_read_msg(struct nct6694 *nct6694, + struct nct6694_cmd_header *cmd_hd, + void *buf) +{ + union nct6694_usb_msg *msg = nct6694->usb_msg; + int tx_len, rx_len, ret; + + guard(mutex)(&nct6694->access_lock); + + /* Send command packet to USB device */ + memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd)); + msg->cmd_header.hctrl = NCT6694_HCTRL_GET; + + ret = usb_bulk_msg(nct6694->udev, + usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP), + &msg->cmd_header, sizeof(*msg), &tx_len, + nct6694->timeout); + if (ret) + return ret; + + /* Receive response packet from USB device */ + ret = usb_bulk_msg(nct6694->udev, + usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP), + &msg->response_header, sizeof(*msg), &rx_len, + nct6694->timeout); + if (ret) + return ret; + + /* Receive data packet from USB device */ + ret = usb_bulk_msg(nct6694->udev, + usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP), + buf, le16_to_cpu(cmd_hd->len), &rx_len, + nct6694->timeout); + if (ret) + return ret; + + if (rx_len != le16_to_cpu(cmd_hd->len)) { + dev_dbg(&nct6694->udev->dev, "%s: Received length is not match!\n", + __func__); + return -EIO; + } + + return nct6694_response_err_handling(nct6694, msg->response_header.sts); +} +EXPORT_SYMBOL(nct6694_read_msg); + +int nct6694_write_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd, + void *buf) +{ + union nct6694_usb_msg *msg = nct6694->usb_msg; + int tx_len, rx_len, ret; + + guard(mutex)(&nct6694->access_lock); + + /* Send command packet to USB device */ + memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd)); + msg->cmd_header.hctrl = NCT6694_HCTRL_SET; + + ret = usb_bulk_msg(nct6694->udev, + usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP), + &msg->cmd_header, sizeof(*msg), &tx_len, + nct6694->timeout); + if (ret) + return ret; + + /* Send data packet to USB device */ + ret = usb_bulk_msg(nct6694->udev, + usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP), + buf, le16_to_cpu(cmd_hd->len), &tx_len, + nct6694->timeout); + if (ret) + return ret; + + /* Receive response packet from USB device */ + ret = usb_bulk_msg(nct6694->udev, + usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP), + &msg->response_header, sizeof(*msg), &rx_len, + nct6694->timeout); + if (ret) + return ret; + + /* Receive data packet from USB device */ + ret = usb_bulk_msg(nct6694->udev, + usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP), + buf, le16_to_cpu(cmd_hd->len), &rx_len, + nct6694->timeout); + if (ret) + return ret; + + if (rx_len != le16_to_cpu(cmd_hd->len)) { + dev_dbg(&nct6694->udev->dev, "%s: Sent length is not match!\n", + __func__); + return -EIO; + } + + return nct6694_response_err_handling(nct6694, msg->response_header.sts); +} +EXPORT_SYMBOL(nct6694_write_msg); + +static void usb_int_callback(struct urb *urb) +{ + struct nct6694 *nct6694 = urb->context; + struct device *dev = &nct6694->udev->dev; + unsigned int *int_status = urb->transfer_buffer; + int ret; + + switch (urb->status) { + case 0: + break; + case -ECONNRESET: + case -ENOENT: + case -ESHUTDOWN: + return; + default: + goto resubmit; + } + + while (*int_status) { + int irq = __ffs(*int_status); + + generic_handle_irq_safe(irq_find_mapping(nct6694->domain, irq)); + *int_status &= ~BIT(irq); + } + +resubmit: + ret = usb_submit_urb(urb, GFP_ATOMIC); + if (ret) + dev_dbg(dev, "%s: Failed to resubmit urb, status %pe", + __func__, ERR_PTR(ret)); +} + +static void nct6694_irq_lock(struct irq_data *data) +{ + struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data); + + mutex_lock(&nct6694->irq_lock); +} + +static void nct6694_irq_sync_unlock(struct irq_data *data) +{ + struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data); + + mutex_unlock(&nct6694->irq_lock); +} + +static void nct6694_irq_enable(struct irq_data *data) +{ + struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data); + irq_hw_number_t hwirq = irqd_to_hwirq(data); + + nct6694->irq_enable |= BIT(hwirq); +} + +static void nct6694_irq_disable(struct irq_data *data) +{ + struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data); + irq_hw_number_t hwirq = irqd_to_hwirq(data); + + nct6694->irq_enable &= ~BIT(hwirq); +} + +static struct irq_chip nct6694_irq_chip = { + .name = "nct6694-irq", + .flags = IRQCHIP_SKIP_SET_WAKE, + .irq_bus_lock = nct6694_irq_lock, + .irq_bus_sync_unlock = nct6694_irq_sync_unlock, + .irq_enable = nct6694_irq_enable, + .irq_disable = nct6694_irq_disable, +}; + +static int nct6694_irq_domain_map(struct irq_domain *d, unsigned int irq, + irq_hw_number_t hw) +{ + struct nct6694 *nct6694 = d->host_data; + + irq_set_chip_data(irq, nct6694); + irq_set_chip_and_handler(irq, &nct6694_irq_chip, handle_simple_irq); + + return 0; +} + +static void nct6694_irq_domain_unmap(struct irq_domain *d, unsigned int irq) +{ + irq_set_chip_and_handler(irq, NULL, NULL); + irq_set_chip_data(irq, NULL); +} + +static const struct irq_domain_ops nct6694_irq_domain_ops = { + .map = nct6694_irq_domain_map, + .unmap = nct6694_irq_domain_unmap, +}; + +static int nct6694_usb_probe(struct usb_interface *iface, + const struct usb_device_id *id) +{ + struct usb_device *udev = interface_to_usbdev(iface); + struct usb_endpoint_descriptor *int_endpoint; + struct usb_host_interface *interface; + struct device *dev = &udev->dev; + struct nct6694 *nct6694; + int pipe, maxp; + int ret; + + interface = iface->cur_altsetting; + + int_endpoint = &interface->endpoint[0].desc; + if (!usb_endpoint_is_int_in(int_endpoint)) + return -ENODEV; + + nct6694 = devm_kzalloc(dev, sizeof(*nct6694), GFP_KERNEL); + if (!nct6694) + return -ENOMEM; + + pipe = usb_rcvintpipe(udev, NCT6694_INT_IN_EP); + maxp = usb_maxpacket(udev, pipe); + + nct6694->usb_msg = devm_kzalloc(dev, sizeof(union nct6694_usb_msg), + GFP_KERNEL); + if (!nct6694->usb_msg) + return -ENOMEM; + + nct6694->int_buffer = devm_kzalloc(dev, maxp, GFP_KERNEL); + if (!nct6694->int_buffer) + return -ENOMEM; + + nct6694->int_in_urb = usb_alloc_urb(0, GFP_KERNEL); + if (!nct6694->int_in_urb) + return -ENOMEM; + + nct6694->domain = irq_domain_add_simple(NULL, NCT6694_NR_IRQS, 0, + &nct6694_irq_domain_ops, + nct6694); + if (!nct6694->domain) { + ret = -ENODEV; + goto err_urb; + } + + nct6694->udev = udev; + nct6694->timeout = NCT6694_URB_TIMEOUT; /* Wait until urb complete */ + + devm_mutex_init(dev, &nct6694->access_lock); + devm_mutex_init(dev, &nct6694->irq_lock); + + usb_fill_int_urb(nct6694->int_in_urb, udev, pipe, + nct6694->int_buffer, maxp, usb_int_callback, + nct6694, int_endpoint->bInterval); + ret = usb_submit_urb(nct6694->int_in_urb, GFP_KERNEL); + if (ret) + goto err_urb; + + dev_set_drvdata(dev, nct6694); + usb_set_intfdata(iface, nct6694); + + ret = mfd_add_hotplug_devices(dev, nct6694_dev, ARRAY_SIZE(nct6694_dev)); + if (ret) + goto err_mfd; + + return 0; + +err_mfd: + usb_kill_urb(nct6694->int_in_urb); +err_urb: + usb_free_urb(nct6694->int_in_urb); + return ret; +} + +static void nct6694_usb_disconnect(struct usb_interface *iface) +{ + struct usb_device *udev = interface_to_usbdev(iface); + struct nct6694 *nct6694 = usb_get_intfdata(iface); + + mfd_remove_devices(&udev->dev); + usb_kill_urb(nct6694->int_in_urb); + usb_free_urb(nct6694->int_in_urb); +} + +static const struct usb_device_id nct6694_ids[] = { + { USB_DEVICE_AND_INTERFACE_INFO(NCT6694_VENDOR_ID, + NCT6694_PRODUCT_ID, + 0xFF, 0x00, 0x00)}, + {} +}; +MODULE_DEVICE_TABLE(usb, nct6694_ids); + +static struct usb_driver nct6694_usb_driver = { + .name = "nct6694", + .id_table = nct6694_ids, + .probe = nct6694_usb_probe, + .disconnect = nct6694_usb_disconnect, +}; + +module_usb_driver(nct6694_usb_driver); + +MODULE_DESCRIPTION("USB-MFD driver for NCT6694"); +MODULE_AUTHOR("Ming Yu "); +MODULE_LICENSE("GPL"); diff --git a/include/linux/mfd/nct6694.h b/include/linux/mfd/nct6694.h new file mode 100644 index 000000000000..67ca835589ad --- /dev/null +++ b/include/linux/mfd/nct6694.h @@ -0,0 +1,109 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Nuvoton NCT6694 USB transaction and data structure. + * + * Copyright (C) 2024 Nuvoton Technology Corp. + */ + +#ifndef __MFD_NCT6694_H +#define __MFD_NCT6694_H + +#define NCT6694_DEV_GPIO "nct6694-gpio" +#define NCT6694_DEV_I2C "nct6694-i2c" +#define NCT6694_DEV_CAN "nct6694-can" +#define NCT6694_DEV_WDT "nct6694-wdt" +#define NCT6694_DEV_HWMON "nct6694-hwmon" +#define NCT6694_DEV_RTC "nct6694-rtc" + +#define NCT6694_VENDOR_ID 0x0416 +#define NCT6694_PRODUCT_ID 0x200B +#define NCT6694_INT_IN_EP 0x81 +#define NCT6694_BULK_IN_EP 0x02 +#define NCT6694_BULK_OUT_EP 0x03 + +#define NCT6694_HCTRL_SET 0x40 +#define NCT6694_HCTRL_GET 0x80 + +#define NCT6694_URB_TIMEOUT 1000 + +enum nct6694_irq_id { + NCT6694_IRQ_GPIO0 = 0, + NCT6694_IRQ_GPIO1, + NCT6694_IRQ_GPIO2, + NCT6694_IRQ_GPIO3, + NCT6694_IRQ_GPIO4, + NCT6694_IRQ_GPIO5, + NCT6694_IRQ_GPIO6, + NCT6694_IRQ_GPIO7, + NCT6694_IRQ_GPIO8, + NCT6694_IRQ_GPIO9, + NCT6694_IRQ_GPIOA, + NCT6694_IRQ_GPIOB, + NCT6694_IRQ_GPIOC, + NCT6694_IRQ_GPIOD, + NCT6694_IRQ_GPIOE, + NCT6694_IRQ_GPIOF, + NCT6694_IRQ_CAN1, + NCT6694_IRQ_CAN2, + NCT6694_IRQ_RTC, + NCT6694_NR_IRQS, +}; + +enum nct6694_response_err_status { + NCT6694_NO_ERROR = 0, + NCT6694_FORMAT_ERROR, + NCT6694_RESERVED1, + NCT6694_RESERVED2, + NCT6694_NOT_SUPPORT_ERROR, + NCT6694_NO_RESPONSE_ERROR, + NCT6694_TIMEOUT_ERROR, + NCT6694_PENDING, +}; + +struct __packed nct6694_cmd_header { + u8 rsv1; + u8 mod; + union __packed { + __le16 offset; + struct __packed { + u8 cmd; + u8 sel; + }; + }; + u8 hctrl; + u8 rsv2; + __le16 len; +}; + +struct __packed nct6694_response_header { + u8 sequence_id; + u8 sts; + u8 reserved[4]; + __le16 len; +}; + +union __packed nct6694_usb_msg { + struct nct6694_cmd_header cmd_header; + struct nct6694_response_header response_header; +}; + +struct nct6694 { + struct usb_device *udev; + struct urb *int_in_urb; + struct irq_domain *domain; + struct mutex access_lock; + struct mutex irq_lock; + union nct6694_usb_msg *usb_msg; + unsigned char *int_buffer; + unsigned int irq_enable; + /* time in msec to wait for the urb to the complete */ + long timeout; +}; + +int nct6694_read_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd, + void *buf); + +int nct6694_write_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd, + void *buf); + +#endif From patchwork Tue Jan 14 03:30:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 857551 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 E1341231A54; Tue, 14 Jan 2025 03:30:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736825440; cv=none; b=PM23Fq4RfN3mGSbX/DsSXieWMndE9XATjEAog2ucJRS2RCXe8UUqKiJyGIgjpGo0m1qhsOrFA/LzCyRLB1kZGTbRHBLi8IHO6JuRm3he7Jm6mjQpwnZxllbY53NRPNZJbonjnywF6X4/2zCyChvP+i3H2VrXw+F6Q2cmnHRYDEs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736825440; c=relaxed/simple; bh=prZNoX4edYcGSUid7EY+pr0eXJn0Fg/+a5q+1fBgN6s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jc8fGuMm+kiiU4bfTVAq6VCmw2mlbYm2DinZkeUl0Qpo1eYsXePmH2H0dKOU4siwyiIseW978kriqYFB/LHo6CMNjrl6FVmlK/ZtL+x70qCkLYDfehDKE9zv08ZppXvZFjts0XxrwB08iyV4jIWezql4RvaUJWmdlZnak1tN3/Q= 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=KYaV34Af; arc=none smtp.client-ip=209.85.214.179 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="KYaV34Af" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-21654fdd5daso84886565ad.1; Mon, 13 Jan 2025 19:30:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736825438; x=1737430238; 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; bh=MIhh+CjnkRORhsX4Je/ArH+6qKnVLW1QQRzWbrojpNw=; b=KYaV34Aft5pOxapDSwYPPU+6zDkIia7mB7We3QcqT4ZHluYxO649gqJPhDTDIc3Hcz 1CieBI/MfefVBPqyCpOZUWR97p0iOjUo76/zyikumeEdP80SkKQrdxTTXFQLp8qj2+1L KBRn6MZj0LvMWA2RA/o7EOLCHHkX5gwu72P1zxiVZp7pFmgFA8ImqlwaQt458gLzyIxd KYplxF4gUqFRVASo6BA66TUlR+6e1rj6HDb0UiuOi2n4virCXV5tCSLQj5/KqYCVbBNd sXMRD8z+Ij1s+1E0mTvLzetPkfoQAw/wCTOA3WZDh3tER2FiweOItRFj30RI3QSnu4l8 nI6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736825438; x=1737430238; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MIhh+CjnkRORhsX4Je/ArH+6qKnVLW1QQRzWbrojpNw=; b=Uz4Ad+LAEIb4h1dOeJTh2Us4/LBMSe7hjboEEzajT4fkgfHUJglH/HPutRyAbysevv ICxnSPKNd5dtRIaQNb3niE0R8RcqDSSQET0FAPVr4jTHyMA6QOjJbc53qvCzkt6JnB5L ttBY6ueRgUJquepDPq04TWWuzDbfx9pznvZIzrsnYQEvSD16otYsx9MIFfmmLReSBhwj BQlXyenQkP3rvwQZOhtYShyuw0Trs7Gr/kXmpbwv3ypMOV0glswO37lyuWoe2z8zY1Uc U6GibLQqH5tLgmMLs2ZZiOkWe9JlNffxxjDFKp3CYNqhhbL3sIDTFdo6pLfxmyLCle+r Ocmw== X-Forwarded-Encrypted: i=1; AJvYcCU4Mqgz7cCxn6hxUdj2WlsHQg8Idy5o9JhKPTP9HPYxAGtx1GwjlhePQvEzHoM4U5MMmWcjOrCFPNwd@vger.kernel.org, AJvYcCVB3sRJcoIxut06O1gHfyWNRfYKDodryYHZFovdXrH54rCZYhTanr6pxcgIn6BbNmhlhrCVqdGKTNs=@vger.kernel.org, AJvYcCVX4DWMlUfWptp7oKcmRPpiFOx72oFR0n0+vTq1NizR9mdOu9ThQ+TMLJtZfv7/WGGqb+NKf9t27PGuIorW7vM=@vger.kernel.org, AJvYcCVktxYYVL4Cuef5Ob9cdxXbg5ed51M3S6bNT4bjIpxflVJwophyXoU1albTYBrOvDKodx8Mcy2tPvd1yQ==@vger.kernel.org, AJvYcCWDuM5HIuonxmF4CZQUhi++m08zJXnRwVT7FUDw4QBQ0sgMAlYc1vaf2oLYEZV1vZeNSBl2XeVbNChn@vger.kernel.org, AJvYcCX4LuHX5ahsZyUFQzjraqM/+yk3o4T1zeRKrKpZ+6ewEIRBzLY3Q4kzFdNVMxY9UrY8d5oRZzwAlmdFj+g=@vger.kernel.org, AJvYcCXB16hk6FNMTM+upmBc2W4t1oVK25+cAZeQ6M03zgV3P7pWKr0AYrJKpiQJzIQTQ2tS8uPRm7jf@vger.kernel.org, AJvYcCXowxnyEsHJSPmqwGSGGd0/ButsqD+XAnUFlBd8nJT7p8XUqQySyoGsmqSsVN3OJIrjtszwacm0l3Jv@vger.kernel.org X-Gm-Message-State: AOJu0YyVK0OQ/2fNGxRyHiU4r6rnJit4xnce9xPaCtugKZYrIFHBGpY9 GUG/YB7Fc/gq8fkAnFcGWhA5TXEdsiT31bkTTlj7cnOaJXu5DVD3 X-Gm-Gg: ASbGncvS/TVoOqR3OCeE8xXZNLveVZvQ1DpiR/8IH29JUCABxlt6wmfhTUU47onGCAf oFfceT2ZYakeKqxkcrwMOGrg4MrQW24cj+RESlsURc6OPIZa26RL+bXEj9zmRepGO6j9lODqDnA qAtbJQPEPKHmNfqrWByH3lFAsi2WDcctaTJ0/kMNCdmoKL+nnkJ8d958klb6v/3PoaPwL5YW894 sAc9Ni7G2u0XP2Wa1Zm9hlOysSqZ+nTGK7LUtASC8cZV1QC1RQldUKjtFvNy6qq1OVP7c9A19y8 Een9tp58nVgRkJ/e1QscUEg6 X-Google-Smtp-Source: AGHT+IH+vafbbYouzCkmmSvb6/L6mWNfaqk/WjTtXqMuBtrPaah2aqvmDZzONTzIY4xWgLCu6pvjnA== X-Received: by 2002:a05:6a00:39a7:b0:728:e382:5f14 with SMTP id d2e1a72fcca58-72d21f314e9mr27603927b3a.9.1736825438124; Mon, 13 Jan 2025 19:30:38 -0800 (PST) Received: from hcdev-d520mt2.. (60-250-196-139.hinet-ip.hinet.net. [60.250.196.139]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d40658b71sm6595400b3a.85.2025.01.13.19.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 19:30:37 -0800 (PST) From: Ming Yu To: tmyu0@nuvoton.com, lee@kernel.org, linus.walleij@linaro.org, brgl@bgdev.pl, andi.shyti@kernel.org, mkl@pengutronix.de, mailhol.vincent@wanadoo.fr, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, wim@linux-watchdog.org, linux@roeck-us.net, jdelvare@suse.com, alexandre.belloni@bootlin.com Cc: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, linux-i2c@vger.kernel.org, linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-watchdog@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-rtc@vger.kernel.org, linux-usb@vger.kernel.org, Ming Yu Subject: [PATCH v5 3/7] i2c: Add Nuvoton NCT6694 I2C support Date: Tue, 14 Jan 2025 11:30:06 +0800 Message-Id: <20250114033010.2445925-4-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250114033010.2445925-1-a0282524688@gmail.com> References: <20250114033010.2445925-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-watchdog@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This driver supports I2C adapter functionality for NCT6694 MFD device based on USB interface, each I2C controller use default baudrate(100K). Signed-off-by: Ming Yu --- MAINTAINERS | 1 + drivers/i2c/busses/Kconfig | 10 ++ drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-nct6694.c | 157 +++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 drivers/i2c/busses/i2c-nct6694.c diff --git a/MAINTAINERS b/MAINTAINERS index f752051f4c3b..4e72f749cdf2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16722,6 +16722,7 @@ M: Ming Yu L: linux-kernel@vger.kernel.org S: Supported F: drivers/gpio/gpio-nct6694.c +F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: include/linux/mfd/nct6694.h diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig index ceb3ecdf884b..a3f60e6953bd 100644 --- a/drivers/i2c/busses/Kconfig +++ b/drivers/i2c/busses/Kconfig @@ -1328,6 +1328,16 @@ config I2C_LJCA This driver can also be built as a module. If so, the module will be called i2c-ljca. +config I2C_NCT6694 + tristate "Nuvoton NCT6694 I2C adapter support" + depends on MFD_NCT6694 + help + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB to I2C interface. + + This driver can also be built as a module. If so, the module will + be called i2c-nct6694. + config I2C_CP2615 tristate "Silicon Labs CP2615 USB sound card and I2C adapter" depends on USB diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile index 1c2a4510abe4..c5a60116dd54 100644 --- a/drivers/i2c/busses/Makefile +++ b/drivers/i2c/busses/Makefile @@ -133,6 +133,7 @@ obj-$(CONFIG_I2C_GXP) += i2c-gxp.o obj-$(CONFIG_I2C_DIOLAN_U2C) += i2c-diolan-u2c.o obj-$(CONFIG_I2C_DLN2) += i2c-dln2.o obj-$(CONFIG_I2C_LJCA) += i2c-ljca.o +obj-$(CONFIG_I2C_NCT6694) += i2c-nct6694.o obj-$(CONFIG_I2C_CP2615) += i2c-cp2615.o obj-$(CONFIG_I2C_PARPORT) += i2c-parport.o obj-$(CONFIG_I2C_PCI1XXXX) += i2c-mchp-pci1xxxx.o diff --git a/drivers/i2c/busses/i2c-nct6694.c b/drivers/i2c/busses/i2c-nct6694.c new file mode 100644 index 000000000000..49e8110662e2 --- /dev/null +++ b/drivers/i2c/busses/i2c-nct6694.c @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Nuvoton NCT6694 I2C adapter driver based on USB interface. + * + * Copyright (C) 2024 Nuvoton Technology Corp. + */ + +#include +#include +#include +#include +#include +#include + +/* + * USB command module type for NCT6694 I2C controller. + * This defines the module type used for communication with the NCT6694 + * I2C controller over the USB interface. + */ +#define NCT6694_I2C_MOD 0x03 + +/* Command 00h - I2C Deliver */ +#define NCT6694_I2C_DELIVER 0x00 +#define NCT6694_I2C_DELIVER_SEL 0x00 + +enum i2c_baudrate { + I2C_BR_25K = 0, + I2C_BR_50K, + I2C_BR_100K, + I2C_BR_200K, + I2C_BR_400K, + I2C_BR_800K, + I2C_BR_1M +}; + +struct __packed nct6694_i2c_deliver { + u8 port; + u8 br; + u8 addr; + u8 w_cnt; + u8 r_cnt; + u8 rsv[11]; + u8 write_data[0x40]; + u8 read_data[0x40]; +}; + +struct nct6694_i2c_data { + struct nct6694 *nct6694; + struct i2c_adapter adapter; + struct nct6694_i2c_deliver *deliver; + unsigned char port; + unsigned char br; +}; + +static int nct6694_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) +{ + struct nct6694_i2c_data *data = adap->algo_data; + struct nct6694_i2c_deliver *deliver = data->deliver; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_I2C_MOD, + .cmd = NCT6694_I2C_DELIVER, + .sel = NCT6694_I2C_DELIVER_SEL, + .len = cpu_to_le16(sizeof(*deliver)) + }; + int ret, i; + + for (i = 0; i < num ; i++) { + struct i2c_msg *msg_temp = &msgs[i]; + + memset(deliver, 0, sizeof(*deliver)); + + if (msg_temp->len > 64) + return -EPROTO; + + deliver->port = data->port; + deliver->br = data->br; + deliver->addr = i2c_8bit_addr_from_msg(msg_temp); + if (msg_temp->flags & I2C_M_RD) { + deliver->r_cnt = msg_temp->len; + ret = nct6694_write_msg(data->nct6694, &cmd_hd, deliver); + if (ret < 0) + return ret; + + memcpy(msg_temp->buf, deliver->read_data, msg_temp->len); + } else { + deliver->w_cnt = msg_temp->len; + memcpy(deliver->write_data, msg_temp->buf, msg_temp->len); + ret = nct6694_write_msg(data->nct6694, &cmd_hd, deliver); + if (ret < 0) + return ret; + } + } + + return num; +} + +static u32 nct6694_func(struct i2c_adapter *adapter) +{ + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; +} + +static const struct i2c_algorithm algorithm = { + .master_xfer = nct6694_xfer, + .functionality = nct6694_func, +}; + +static int nct6694_i2c_probe(struct platform_device *pdev) +{ + const struct mfd_cell *cell = mfd_get_cell(pdev); + struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent); + struct nct6694_i2c_data *data; + + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->deliver = devm_kzalloc(&pdev->dev, sizeof(struct nct6694_i2c_deliver), + GFP_KERNEL); + if (!data->deliver) + return -ENOMEM; + + data->nct6694 = nct6694; + data->port = cell->id; + data->br = I2C_BR_100K; + + sprintf(data->adapter.name, "NCT6694 I2C Adapter %d", cell->id); + data->adapter.owner = THIS_MODULE; + data->adapter.algo = &algorithm; + data->adapter.dev.parent = &pdev->dev; + data->adapter.algo_data = data; + + platform_set_drvdata(pdev, data); + + return i2c_add_adapter(&data->adapter); +} + +static void nct6694_i2c_remove(struct platform_device *pdev) +{ + struct nct6694_i2c_data *data = platform_get_drvdata(pdev); + + i2c_del_adapter(&data->adapter); +} + +static struct platform_driver nct6694_i2c_driver = { + .driver = { + .name = "nct6694-i2c", + }, + .probe = nct6694_i2c_probe, + .remove = nct6694_i2c_remove, +}; + +module_platform_driver(nct6694_i2c_driver); + +MODULE_DESCRIPTION("USB-I2C adapter driver for NCT6694"); +MODULE_AUTHOR("Ming Yu "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:nct6694-i2c"); From patchwork Tue Jan 14 03:30:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 857550 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 C914A233547; Tue, 14 Jan 2025 03:30:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736825449; cv=none; b=HHawyZRNyRruZtaeiXnAgBBC0cZsKj7FQLR9WKStwYcVGhmg5u+5ZtWC84iW4cz3tBtOHe1J9sXFpT/cDS1i7yAHFnIJzaMHkb5ACjjjIMfSMhWE6S8dqx+gMPGpe0UEik58DcNoV1HoVvZE+4PmA2tU26+CaemWXXl5Dw4IFRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736825449; c=relaxed/simple; bh=Z5kSilz7xAHiDx/LKImNs1Daog19uV4ILduw4lp6lG4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IsDvmtSd0vs6NK9NxELyGrzFcowfkVuxLugUWCzHr+uYMfojHt855Ap9TEm2iwv3SCk8hW17hZkZC9XqR1WB65sCcotv1g1U+H/hyD94WJrEcwuE8qXrZ1DFmTKDcH0KTfJYLqyCcJtGv1JnoellgLxg6kgvf/4jypZNEkVHSgM= 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=KOIVDDPr; arc=none smtp.client-ip=209.85.214.175 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="KOIVDDPr" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2164b1f05caso87330515ad.3; Mon, 13 Jan 2025 19:30:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736825447; x=1737430247; 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; bh=IlalFqwT/qxv4dlK31znCkR1VqrjOdBXqECdfqNFglc=; b=KOIVDDPrl1GsBTrNC+HFTuZevPTtsingVlpuIiikvqThvazEj/MbWEFlDbTKEvnVJE sI8Q0oxKyoSs6tDMEP+CkY7jwpS7ZKgxX9sSA8SRKE0B9lTMp77KnHPJvMOVI/0Ac8LW EwbsjLa1Y8TmIDGdJPKc1dt3dvOoOX38oUqW2Q5k+wvftW250UCehpL6bMM0xgXMoHa+ 6rTh3p4gHXavvHntDy9FHLDsyZKuff1yxYmcdTkgrd8bxbuye0IJNxeLDKdiHC7Cxp7Q VJmECEVzDAEJQ6lMDr2tNbwP6U78Xs12ruqJNrs69kSfdxDyhPzEM8jF5Moi0GWgJQHG iHXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736825447; x=1737430247; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IlalFqwT/qxv4dlK31znCkR1VqrjOdBXqECdfqNFglc=; b=Su5AFRLNGLLdKDGxhaQ/M8h7gq7hrLeMymU/IKdyky041aFz5POrOhpiUy/UFzqdcT DCkKOq/2GIpZNbaM0lE5P5lpStRlxcGmpRz5qPwMtoBTWW75rIrHIOAujpEQCpBhYQlo qcD34NKKr7ySdPDY0dx/tEmmnhXId9uOB6+XBxhLLaP6kPge+4tLfE8a2L3zRPGjdKQC nW3HiTD4b4Ryg4NvJj7+yd8q7FiHinBH0ZkCxuuMM4ioYq4WyRt+ANcRTUQugOI1PH2O sR+wTpMYvcoxlz8uifO2XTr1b6B16gMZOKovsItkyEkLfaKPh4Z7b0ucKU6sEc63K0lw 09MA== X-Forwarded-Encrypted: i=1; AJvYcCUV1k1IX4DuVzqcA3iWiOGmV0MJJpFmcV3A8is0XCS9HY1j9eyXjim03esK+NUiSj5//rYl/V5k6Kpn@vger.kernel.org, AJvYcCUpQ615v1M8j50oiDpXleNeqSJjgIfVwzs4KG8Q4WmiiD2dBvCEbBtIpQyZRVurZl9j9IIyny6879A=@vger.kernel.org, AJvYcCUqIepyWm1tkTGP5AkIin0Bz87W+1qu2F91DDrXeLA8SZMX2NSkSqWMIj7WboWQlDKOsz07CXoHkDmo@vger.kernel.org, AJvYcCUyzBgLkRGu9uLaZtc5TjyA4EVjo7/fyHuZsUBJs75mxuv06jIc+FlvR+6Xhw5YzMfdU9DzYQpy3EZoOWR8EaQ=@vger.kernel.org, AJvYcCVho3ICha0Lpwn836PS/l8wMm16BkLnyVkoCNVF6P7gmFvFSDLEcQUFwJxASI6WJmaYJnp53Nk3vDLJeg==@vger.kernel.org, AJvYcCWNB8xjYVCS2PSfCVNDb2UeyJ7+rOxLYr89qRSTi/e97Le48pP5ec8du2kCm9yihFQey7VRT33my6L/kD0=@vger.kernel.org, AJvYcCWkDoovRpgMytLgWLXxcAMP/Wu45+INB7Ctw6lMM65C5kGedfkWOw+b3Jtpr2DU8KowpfzJjAI6cnS5@vger.kernel.org, AJvYcCXztb5qS1CR//PQPPIz8D10rIoK/m78aMv8CBzPUlgeQZddA46pednif/iV6ogGf+qiIeqnh4Zr@vger.kernel.org X-Gm-Message-State: AOJu0Yxr05SEeWWfz5J7l1hdZYgd1QZslpoA6oeJBoyT1dpfsCtflibP MFWk1Yq85VjVy2CRA4lEfHaP5M284zr9J/wJI7QXq7m80be/7BsP X-Gm-Gg: ASbGncvS0oGBmWopykwvt4KX+5B5BMEBgWOlIkXmtSQITSSeZ059jKk8YQYIrqycgOU 4P/Xz4M9oMs/qR0vNwyB6YyjCLo0rlnUDPzYMtOgPhg88dJjEllsMDJGGfMzF+R4JAuwQRlpWvq 2pAlZrOQWHOGfBaIx12dLBlSUGHKHq2n2Q1Pq1me9ZDM5xKfldupkqpMLyF/YNP5dGvKbFxwVkk FP0jKGbz5molfuYRIb4uYrN0UsYz90XKlufAyOVhJPtooyVW+KoYbJpZwztmMWbe24kx5/ltTVD M8EqaiRB3DZvsZJGqhhRgnVZ X-Google-Smtp-Source: AGHT+IG5HuZL04MDiDCm/lyc+47e56pUaFX/cYsYsPRY/kLK9KU5XC8t5Ickmz1/gwowBZko6cg9+Q== X-Received: by 2002:a05:6a20:748b:b0:1e1:ca27:89f0 with SMTP id adf61e73a8af0-1e88d0edb0cmr41485592637.37.1736825447148; Mon, 13 Jan 2025 19:30:47 -0800 (PST) Received: from hcdev-d520mt2.. (60-250-196-139.hinet-ip.hinet.net. [60.250.196.139]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d40658b71sm6595400b3a.85.2025.01.13.19.30.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 19:30:46 -0800 (PST) From: Ming Yu To: tmyu0@nuvoton.com, lee@kernel.org, linus.walleij@linaro.org, brgl@bgdev.pl, andi.shyti@kernel.org, mkl@pengutronix.de, mailhol.vincent@wanadoo.fr, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, wim@linux-watchdog.org, linux@roeck-us.net, jdelvare@suse.com, alexandre.belloni@bootlin.com Cc: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, linux-i2c@vger.kernel.org, linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-watchdog@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-rtc@vger.kernel.org, linux-usb@vger.kernel.org, Ming Yu Subject: [PATCH v5 5/7] watchdog: Add Nuvoton NCT6694 WDT support Date: Tue, 14 Jan 2025 11:30:08 +0800 Message-Id: <20250114033010.2445925-6-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250114033010.2445925-1-a0282524688@gmail.com> References: <20250114033010.2445925-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-watchdog@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This driver supports Watchdog timer functionality for NCT6694 MFD device based on USB interface. Signed-off-by: Ming Yu --- MAINTAINERS | 1 + drivers/watchdog/Kconfig | 11 ++ drivers/watchdog/Makefile | 1 + drivers/watchdog/nct6694_wdt.c | 295 +++++++++++++++++++++++++++++++++ 4 files changed, 308 insertions(+) create mode 100644 drivers/watchdog/nct6694_wdt.c diff --git a/MAINTAINERS b/MAINTAINERS index 6e9b78202d6f..a772747d493f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16725,6 +16725,7 @@ F: drivers/gpio/gpio-nct6694.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/usb/nct6694_canfd.c +F: drivers/watchdog/nct6694_wdt.c F: include/linux/mfd/nct6694.h NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index f81705f8539a..4c4f826368c4 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -748,6 +748,17 @@ config MAX77620_WATCHDOG MAX77620 chips. To compile this driver as a module, choose M here: the module will be called max77620_wdt. +config NCT6694_WATCHDOG + tristate "Nuvoton NCT6694 watchdog support" + depends on MFD_NCT6694 + select WATCHDOG_CORE + help + Say Y here to support Nuvoton NCT6694 watchdog timer + functionality. + + This driver can also be built as a module. If so, the module + will be called nct6694_wdt. + config IMX2_WDT tristate "IMX2+ Watchdog" depends on ARCH_MXC || ARCH_LAYERSCAPE || COMPILE_TEST diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index 8411626fa162..de2a04ff8a92 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -232,6 +232,7 @@ obj-$(CONFIG_WM831X_WATCHDOG) += wm831x_wdt.o obj-$(CONFIG_WM8350_WATCHDOG) += wm8350_wdt.o obj-$(CONFIG_MAX63XX_WATCHDOG) += max63xx_wdt.o obj-$(CONFIG_MAX77620_WATCHDOG) += max77620_wdt.o +obj-$(CONFIG_NCT6694_WATCHDOG) += nct6694_wdt.o obj-$(CONFIG_ZIIRAVE_WATCHDOG) += ziirave_wdt.o obj-$(CONFIG_SOFT_WATCHDOG) += softdog.o obj-$(CONFIG_MENF21BMC_WATCHDOG) += menf21bmc_wdt.o diff --git a/drivers/watchdog/nct6694_wdt.c b/drivers/watchdog/nct6694_wdt.c new file mode 100644 index 000000000000..351867c2fd1d --- /dev/null +++ b/drivers/watchdog/nct6694_wdt.c @@ -0,0 +1,295 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Nuvoton NCT6694 WDT driver based on USB interface. + * + * Copyright (C) 2024 Nuvoton Technology Corp. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define DRVNAME "nct6694-wdt" + +#define NCT6694_DEFAULT_TIMEOUT 10 +#define NCT6694_DEFAULT_PRETIMEOUT 0 + +/* + * USB command module type for NCT6694 WDT controller. + * This defines the module type used for communication with the NCT6694 + * WDT controller over the USB interface. + */ +#define NCT6694_WDT_MOD 0x07 + +/* Command 00h - WDT Setup */ +#define NCT6694_WDT_SETUP 0x00 +#define NCT6694_WDT_SETUP_SEL(idx) (idx ? 0x01 : 0x00) + +/* Command 01h - WDT Command */ +#define NCT6694_WDT_COMMAND 0x01 +#define NCT6694_WDT_COMMAND_SEL(idx) (idx ? 0x01 : 0x00) + +static unsigned int timeout = NCT6694_DEFAULT_TIMEOUT; +module_param(timeout, int, 0); +MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds"); + +static unsigned int pretimeout = NCT6694_DEFAULT_PRETIMEOUT; +module_param(pretimeout, int, 0); +MODULE_PARM_DESC(pretimeout, "Watchdog pre-timeout in seconds"); + +static bool nowayout = WATCHDOG_NOWAYOUT; +module_param(nowayout, bool, 0); +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); + +enum { + NCT6694_ACTION_NONE = 0, + NCT6694_ACTION_SIRQ, + NCT6694_ACTION_GPO, +}; + +struct __packed nct6694_wdt_setup { + __le32 pretimeout; + __le32 timeout; + u8 owner; + u8 scratch; + u8 control; + u8 status; + __le32 countdown; +}; + +struct __packed nct6694_wdt_cmd { + __le32 wdt_cmd; + __le32 reserved; +}; + +union __packed nct6694_wdt_msg { + struct nct6694_wdt_setup setup; + struct nct6694_wdt_cmd cmd; +}; + +struct nct6694_wdt_data { + struct watchdog_device wdev; + struct device *dev; + struct nct6694 *nct6694; + struct mutex lock; + union nct6694_wdt_msg *msg; + unsigned int wdev_idx; +}; + +static int nct6694_wdt_setting(struct watchdog_device *wdev, + u32 timeout_val, u8 timeout_act, + u32 pretimeout_val, u8 pretimeout_act) +{ + struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev); + struct nct6694_wdt_setup *setup = &data->msg->setup; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_WDT_MOD, + .cmd = NCT6694_WDT_SETUP, + .sel = NCT6694_WDT_SETUP_SEL(data->wdev_idx), + .len = cpu_to_le16(sizeof(*setup)) + }; + unsigned int timeout_fmt, pretimeout_fmt; + + guard(mutex)(&data->lock); + + if (pretimeout_val == 0) + pretimeout_act = NCT6694_ACTION_NONE; + + timeout_fmt = (timeout_val * 1000) | (timeout_act << 24); + pretimeout_fmt = (pretimeout_val * 1000) | (pretimeout_act << 24); + + memset(setup, 0, sizeof(*setup)); + setup->timeout = cpu_to_le32(timeout_fmt); + setup->pretimeout = cpu_to_le32(pretimeout_fmt); + + return nct6694_write_msg(data->nct6694, &cmd_hd, setup); +} + +static int nct6694_wdt_start(struct watchdog_device *wdev) +{ + struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev); + int ret; + + ret = nct6694_wdt_setting(wdev, wdev->timeout, NCT6694_ACTION_GPO, + wdev->pretimeout, NCT6694_ACTION_GPO); + if (ret) + return ret; + + dev_dbg(data->dev, "Setting WDT(%d): timeout = %d, pretimeout = %d\n", + data->wdev_idx, wdev->timeout, wdev->pretimeout); + + return ret; +} + +static int nct6694_wdt_stop(struct watchdog_device *wdev) +{ + struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev); + struct nct6694_wdt_cmd *cmd = &data->msg->cmd; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_WDT_MOD, + .cmd = NCT6694_WDT_COMMAND, + .sel = NCT6694_WDT_COMMAND_SEL(data->wdev_idx), + .len = cpu_to_le16(sizeof(*cmd)) + }; + + guard(mutex)(&data->lock); + + memcpy(&cmd->wdt_cmd, "WDTC", 4); + cmd->reserved = 0; + + return nct6694_write_msg(data->nct6694, &cmd_hd, cmd); +} + +static int nct6694_wdt_ping(struct watchdog_device *wdev) +{ + struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev); + struct nct6694_wdt_cmd *cmd = &data->msg->cmd; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_WDT_MOD, + .cmd = NCT6694_WDT_COMMAND, + .sel = NCT6694_WDT_COMMAND_SEL(data->wdev_idx), + .len = cpu_to_le16(sizeof(*cmd)) + }; + + guard(mutex)(&data->lock); + memcpy(&cmd->wdt_cmd, "WDTS", 4); + cmd->reserved = 0; + + return nct6694_write_msg(data->nct6694, &cmd_hd, cmd); +} + +static int nct6694_wdt_set_timeout(struct watchdog_device *wdev, + unsigned int timeout) +{ + int ret; + + ret = nct6694_wdt_setting(wdev, timeout, NCT6694_ACTION_GPO, + wdev->pretimeout, NCT6694_ACTION_GPO); + if (ret) + return ret; + + wdev->timeout = timeout; + + return 0; +} + +static int nct6694_wdt_set_pretimeout(struct watchdog_device *wdev, + unsigned int pretimeout) +{ + int ret; + + ret = nct6694_wdt_setting(wdev, wdev->timeout, NCT6694_ACTION_GPO, + pretimeout, NCT6694_ACTION_GPO); + if (ret) + return ret; + + wdev->pretimeout = pretimeout; + + return 0; +} + +static unsigned int nct6694_wdt_get_time(struct watchdog_device *wdev) +{ + struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev); + struct nct6694_wdt_setup *setup = &data->msg->setup; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_WDT_MOD, + .cmd = NCT6694_WDT_SETUP, + .sel = NCT6694_WDT_SETUP_SEL(data->wdev_idx), + .len = cpu_to_le16(sizeof(*setup)) + }; + unsigned int timeleft_ms; + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, &cmd_hd, setup); + if (ret) + return 0; + + timeleft_ms = le32_to_cpu(setup->countdown); + + return timeleft_ms / 1000; +} + +static const struct watchdog_info nct6694_wdt_info = { + .options = WDIOF_SETTIMEOUT | + WDIOF_KEEPALIVEPING | + WDIOF_MAGICCLOSE | + WDIOF_PRETIMEOUT, + .identity = DRVNAME, +}; + +static const struct watchdog_ops nct6694_wdt_ops = { + .owner = THIS_MODULE, + .start = nct6694_wdt_start, + .stop = nct6694_wdt_stop, + .set_timeout = nct6694_wdt_set_timeout, + .set_pretimeout = nct6694_wdt_set_pretimeout, + .get_timeleft = nct6694_wdt_get_time, + .ping = nct6694_wdt_ping, +}; + +static int nct6694_wdt_probe(struct platform_device *pdev) +{ + const struct mfd_cell *cell = mfd_get_cell(pdev); + struct device *dev = &pdev->dev; + struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent); + struct nct6694_wdt_data *data; + struct watchdog_device *wdev; + + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->msg = devm_kzalloc(dev, sizeof(union nct6694_wdt_msg), + GFP_KERNEL); + if (!data->msg) + return -ENOMEM; + + data->dev = dev; + data->nct6694 = nct6694; + data->wdev_idx = cell->id; + + wdev = &data->wdev; + wdev->info = &nct6694_wdt_info; + wdev->ops = &nct6694_wdt_ops; + wdev->timeout = timeout; + wdev->pretimeout = pretimeout; + if (timeout < pretimeout) { + dev_warn(data->dev, "pretimeout < timeout. Setting to zero\n"); + wdev->pretimeout = 0; + } + + wdev->min_timeout = 1; + wdev->max_timeout = 255; + + devm_mutex_init(dev, &data->lock); + + platform_set_drvdata(pdev, data); + + watchdog_set_drvdata(&data->wdev, data); + watchdog_set_nowayout(&data->wdev, nowayout); + watchdog_stop_on_reboot(&data->wdev); + + return devm_watchdog_register_device(dev, &data->wdev); +} + +static struct platform_driver nct6694_wdt_driver = { + .driver = { + .name = DRVNAME, + }, + .probe = nct6694_wdt_probe, +}; + +module_platform_driver(nct6694_wdt_driver); + +MODULE_DESCRIPTION("USB-WDT driver for NCT6694"); +MODULE_AUTHOR("Ming Yu "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:nct6694-wdt"); From patchwork Tue Jan 14 03:30:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 857549 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 254602451D6; Tue, 14 Jan 2025 03:30:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736825459; cv=none; b=Kj6VbUi1XMEDdQhyyvWWX0Aq9dJc0Gw4Nn5ILzSA1ABjXx6sQG2axb4fnqmm3HVMhVK3uHerFP3J3aeIznUR5/z2HRMnnvPGz/ItxiUvHVrg5lQIQNAEVSZOpxvBf+3sFhGfcli7MoBx2aiGvFfxUvng/BrBV4P57j45FBohc1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736825459; c=relaxed/simple; bh=CvxrNe8e/4BaiSfOBSwCj1KQ5/77UOBBQdLZgL/RNoA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=P0wiJxx67Eitakd1AD/eLm2tGTVbuoKCfivuMuiIq0H+0NZtUMuKCXuf7+4ngNEX+jMyCfq4oMOLwWyF4oR72MXi+Jzvdjgk3rKm0jhJp9JTE+FfzreYuaBLInmaWIoFStJKmesI8n0J0fj6wjKMe5VCfHACyRgVN7uH96TRdNo= 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=Hn0peWGJ; arc=none smtp.client-ip=209.85.214.175 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="Hn0peWGJ" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2166022c5caso77535065ad.2; Mon, 13 Jan 2025 19:30:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736825456; x=1737430256; 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; bh=5FcdLdnPNCznIFI/wzOEmfhmBl5xfGYEDQL7X7E12xg=; b=Hn0peWGJZ8r5BCMB423TitBf+1q+dJCPYDBb4pYYbjaTDF98agBhZrf1UbVF9bte+S hlODKeDFENfQoFzRU5z7UgBoiqaCIvtY6uGNsr9hCJxfc9bYuSnCC5VtaiJm0B+VCtf4 6i6lm/7Z8tr44Ak3cGG+hBVH8cz0hoQ6FFPQXWM/fJE36g22x7pfO12AZ+1nHb+Z1buP d/fFaTn4YIxEQ0O44UEOzPhoKzjxudecq+m1eF0NaxrljaODEz5e/fXJpoq6zgeWgo63 ER705fzXJYwv0noHrph6daX8QuAlcoRsxgqAhLxLE3OxxJzfVrcu7GRHNvcsZ7ATpjQ2 xMZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736825456; x=1737430256; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5FcdLdnPNCznIFI/wzOEmfhmBl5xfGYEDQL7X7E12xg=; b=SOFyYJthNsOBF4WNGG7O7Z9nHgsaD1rxR2DrUeDrBVAFpnb0HtQWV7zZ3aLr2NUggn omxWP4mIXuS08fGWY+gmXD40MJ1cOh/HSsKiYaa9W7ewXb4uS0OSP00sWu2N1SVwSIYJ 7kiL9Zg48Osoa+r4/y2AFYqqjMN+ezdefigHdOoBRXGOycl3qmMTLM4SbRfQo6ydiw68 ngapyi84FNtBHdm7wLhjkruY9jCtpNQ63JcTJ7NwMkmeps9JHVHR63u2wdSSAACUfhsV vXznvTGpggpPxpDpfhK54ycJmJxWNO6KX2dI6JKlALuepMPSFmm6iZQBxyQdzBT5WvCx e/AQ== X-Forwarded-Encrypted: i=1; AJvYcCUGOqfQPlu50YsgZlpOtA2qxeyPCpaSkU4/+r2oXq40HtVQocM4/NWN3RD1L2D3ZY98rJPMlkRY@vger.kernel.org, AJvYcCURTaA/awANtLjuJXT021WCuwjCWxLxC6F5MwoUIb+WbnSqBYD3Jz3uuJiVQrC+BNmkjw/sOc0tDFog@vger.kernel.org, AJvYcCUmngvZLfdWopiqH2kmnO/5eFuqXlH2gLPFtqXwsqS291SoQ4lHCnNaXy/1Q+tOYIc8EYbe6FgfRfNw@vger.kernel.org, AJvYcCV14kSePTj4DcJsiVMsv13JvjUO36WA3Z/7XDlDgplFrsLigexdPDWPx93hdOff3VJlf//oJzm8EKc=@vger.kernel.org, AJvYcCV9ELSxsQTi1P/kYeEt4m3v+FcIyPuW+PpjgqURzRWXvE4PHkCDhjwX/BM/XIbsO84EQddza3G5qeIN3zA=@vger.kernel.org, AJvYcCVTucoMY8gv+aLiMu9R7e70Tu1pzquW3OqDY2gXpplnpgWA1HHmmsDebE98m1K5T5JbQFcmMsBmYzFW4YUY23o=@vger.kernel.org, AJvYcCVZMhoDQDD0LJz15dguHzw/bn4p/njzhVDCniqmnJQXNSQ2iWgcm7t6+nZU89rGyAELJEJzd6aCs1SzzQ==@vger.kernel.org, AJvYcCXTT098e6U9Rw681JDuY04i3Y9oz+qEcYUhMbcA8Ja9iiS/CGIv0N5iEBhCi90X+XrtI6LbYwAVdZqq@vger.kernel.org X-Gm-Message-State: AOJu0YzdWIge+6AFuI8YWEmSqw8AiASyw5Rc7HGAfN3jhzM5nT+LBhzh fZaqjh2ihEIUIQCPMqvX7uvlu3BO6u6xsXl7//h36Du5WETUVcTr X-Gm-Gg: ASbGncvC4J3iOG5WeaqApmkF2pckfxdSZcEJAwAQYwZg588Uz0xHnGUHk+91E6nYY3x +gmDs24Ym7Ofbe2L+zyGQdyErT6e9vJRCotcK1ol0QAjlnhj7knU2LqcdVHCu/3ckQmJp2gaUsj wAWJZt9SJAfmfrckohEXR242VpT6RsxDZB99Jaa6Ny5Fo4DnwfOPv134+WTbKfb70TgVFsfIWHR 01ULKGHCn6MZsNpDshBvDGFzv4RvOCua9kUgDAhaa/2nFh/i/lPIgN4nGNCBBn+fZt451v+QLBp MyStQpHlSVZlJI7HSAvK8/EH X-Google-Smtp-Source: AGHT+IEun7RHtFjwYDJ96um2RN8ttGvuzqEME7zM4yWlHvNfi9JcwZNJ6bIjR85GG5h2n50BDbqi+g== X-Received: by 2002:a05:6a00:2908:b0:728:e745:23d8 with SMTP id d2e1a72fcca58-72d21ffab83mr36378554b3a.24.1736825456176; Mon, 13 Jan 2025 19:30:56 -0800 (PST) Received: from hcdev-d520mt2.. (60-250-196-139.hinet-ip.hinet.net. [60.250.196.139]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72d40658b71sm6595400b3a.85.2025.01.13.19.30.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 19:30:55 -0800 (PST) From: Ming Yu To: tmyu0@nuvoton.com, lee@kernel.org, linus.walleij@linaro.org, brgl@bgdev.pl, andi.shyti@kernel.org, mkl@pengutronix.de, mailhol.vincent@wanadoo.fr, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, wim@linux-watchdog.org, linux@roeck-us.net, jdelvare@suse.com, alexandre.belloni@bootlin.com Cc: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, linux-i2c@vger.kernel.org, linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-watchdog@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-rtc@vger.kernel.org, linux-usb@vger.kernel.org, Ming Yu Subject: [PATCH v5 7/7] rtc: Add Nuvoton NCT6694 RTC support Date: Tue, 14 Jan 2025 11:30:10 +0800 Message-Id: <20250114033010.2445925-8-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250114033010.2445925-1-a0282524688@gmail.com> References: <20250114033010.2445925-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-watchdog@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This driver supports RTC functionality for NCT6694 MFD device based on USB interface. Signed-off-by: Ming Yu --- MAINTAINERS | 1 + drivers/rtc/Kconfig | 10 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-nct6694.c | 286 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 298 insertions(+) create mode 100644 drivers/rtc/rtc-nct6694.c diff --git a/MAINTAINERS b/MAINTAINERS index a14a2d65b8a6..5275ff7a793c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16726,6 +16726,7 @@ F: drivers/hwmon/nct6694-hwmon.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/usb/nct6694_canfd.c +F: drivers/rtc/rtc-nct6694.c F: drivers/watchdog/nct6694_wdt.c F: include/linux/mfd/nct6694.h diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index a60bcc791a48..aeab67acbc84 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -416,6 +416,16 @@ config RTC_DRV_NCT3018Y This driver can also be built as a module, if so, the module will be called "rtc-nct3018y". +config RTC_DRV_NCT6694 + tristate "Nuvoton NCT6694 RTC support" + depends on MFD_NCT6694 + help + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB device to RTC. + + This driver can also be built as a module. If so, the module will + be called rtc-nct6694. + config RTC_DRV_RK808 tristate "Rockchip RK805/RK808/RK809/RK817/RK818 RTC" depends on MFD_RK8XX diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 489b4ab07068..d0d6f4a4972e 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -118,6 +118,7 @@ obj-$(CONFIG_RTC_DRV_MXC) += rtc-mxc.o obj-$(CONFIG_RTC_DRV_MXC_V2) += rtc-mxc_v2.o obj-$(CONFIG_RTC_DRV_GAMECUBE) += rtc-gamecube.o obj-$(CONFIG_RTC_DRV_NCT3018Y) += rtc-nct3018y.o +obj-$(CONFIG_RTC_DRV_NCT6694) += rtc-nct6694.o obj-$(CONFIG_RTC_DRV_NTXEC) += rtc-ntxec.o obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o obj-$(CONFIG_RTC_DRV_OPAL) += rtc-opal.o diff --git a/drivers/rtc/rtc-nct6694.c b/drivers/rtc/rtc-nct6694.c new file mode 100644 index 000000000000..dbb5ccae2796 --- /dev/null +++ b/drivers/rtc/rtc-nct6694.c @@ -0,0 +1,286 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Nuvoton NCT6694 RTC driver based on USB interface. + * + * Copyright (C) 2024 Nuvoton Technology Corp. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * USB command module type for NCT6694 RTC controller. + * This defines the module type used for communication with the NCT6694 + * RTC controller over the USB interface. + */ +#define NCT6694_RTC_MOD 0x08 + +/* Command 00h - RTC Time */ +#define NCT6694_RTC_TIME 0x0000 +#define NCT6694_RTC_TIME_SEL 0x00 + +/* Command 01h - RTC Alarm */ +#define NCT6694_RTC_ALARM 0x01 +#define NCT6694_RTC_ALARM_SEL 0x00 + +/* Command 02h - RTC Status */ +#define NCT6694_RTC_STATUS 0x02 +#define NCT6694_RTC_STATUS_SEL 0x00 + +#define NCT6694_RTC_IRQ_INT_EN BIT(0) /* Transmit a USB INT-in when RTC alarm */ +#define NCT6694_RTC_IRQ_GPO_EN BIT(5) /* Trigger a GPO Low Pulse when RTC alarm */ + +#define NCT6694_RTC_IRQ_EN (NCT6694_RTC_IRQ_INT_EN | NCT6694_RTC_IRQ_GPO_EN) +#define NCT6694_RTC_IRQ_STS BIT(0) /* Write 1 clear IRQ status */ + +struct __packed nct6694_rtc_time { + u8 sec; + u8 min; + u8 hour; + u8 week; + u8 day; + u8 month; + u8 year; +}; + +struct __packed nct6694_rtc_alarm { + u8 sec; + u8 min; + u8 hour; + u8 alarm_en; + u8 alarm_pend; +}; + +struct __packed nct6694_rtc_status { + u8 irq_en; + u8 irq_pend; +}; + +union __packed nct6694_rtc_msg { + struct nct6694_rtc_time time; + struct nct6694_rtc_alarm alarm; + struct nct6694_rtc_status sts; +}; + +struct nct6694_rtc_data { + struct nct6694 *nct6694; + struct rtc_device *rtc; + union nct6694_rtc_msg *msg; +}; + +static int nct6694_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct nct6694_rtc_data *data = dev_get_drvdata(dev); + struct nct6694_rtc_time *time = &data->msg->time; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_RTC_MOD, + .cmd = NCT6694_RTC_TIME, + .sel = NCT6694_RTC_TIME_SEL, + .len = cpu_to_le16(sizeof(*time)) + }; + int ret; + + ret = nct6694_read_msg(data->nct6694, &cmd_hd, time); + if (ret) + return ret; + + tm->tm_sec = bcd2bin(time->sec); /* tm_sec expect 0 ~ 59 */ + tm->tm_min = bcd2bin(time->min); /* tm_min expect 0 ~ 59 */ + tm->tm_hour = bcd2bin(time->hour); /* tm_hour expect 0 ~ 23 */ + tm->tm_wday = bcd2bin(time->week) - 1; /* tm_wday expect 0 ~ 6 */ + tm->tm_mday = bcd2bin(time->day); /* tm_mday expect 1 ~ 31 */ + tm->tm_mon = bcd2bin(time->month) - 1; /* tm_month expect 0 ~ 11 */ + tm->tm_year = bcd2bin(time->year) + 100; /* tm_year expect since 1900 */ + + return ret; +} + +static int nct6694_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + struct nct6694_rtc_data *data = dev_get_drvdata(dev); + struct nct6694_rtc_time *time = &data->msg->time; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_RTC_MOD, + .cmd = NCT6694_RTC_TIME, + .sel = NCT6694_RTC_TIME_SEL, + .len = cpu_to_le16(sizeof(*time)) + }; + + time->sec = bin2bcd(tm->tm_sec); + time->min = bin2bcd(tm->tm_min); + time->hour = bin2bcd(tm->tm_hour); + time->week = bin2bcd(tm->tm_wday + 1); + time->day = bin2bcd(tm->tm_mday); + time->month = bin2bcd(tm->tm_mon + 1); + time->year = bin2bcd(tm->tm_year - 100); + + return nct6694_write_msg(data->nct6694, &cmd_hd, time); +} + +static int nct6694_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct nct6694_rtc_data *data = dev_get_drvdata(dev); + struct nct6694_rtc_alarm *alarm = &data->msg->alarm; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_RTC_MOD, + .cmd = NCT6694_RTC_ALARM, + .sel = NCT6694_RTC_ALARM_SEL, + .len = cpu_to_le16(sizeof(*alarm)) + }; + int ret; + + ret = nct6694_read_msg(data->nct6694, &cmd_hd, alarm); + if (ret) + return ret; + + alrm->time.tm_sec = bcd2bin(alarm->sec); + alrm->time.tm_min = bcd2bin(alarm->min); + alrm->time.tm_hour = bcd2bin(alarm->hour); + alrm->enabled = alarm->alarm_en; + alrm->pending = alarm->alarm_pend; + + return ret; +} + +static int nct6694_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct nct6694_rtc_data *data = dev_get_drvdata(dev); + struct nct6694_rtc_alarm *alarm = &data->msg->alarm; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_RTC_MOD, + .cmd = NCT6694_RTC_ALARM, + .sel = NCT6694_RTC_ALARM_SEL, + .len = cpu_to_le16(sizeof(*alarm)) + }; + + alarm->sec = bin2bcd(alrm->time.tm_sec); + alarm->min = bin2bcd(alrm->time.tm_min); + alarm->hour = bin2bcd(alrm->time.tm_hour); + alarm->alarm_en = alrm->enabled ? NCT6694_RTC_IRQ_EN : 0; + alarm->alarm_pend = 0; + + return nct6694_write_msg(data->nct6694, &cmd_hd, alarm); +} + +static int nct6694_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) +{ + struct nct6694_rtc_data *data = dev_get_drvdata(dev); + struct nct6694_rtc_status *sts = &data->msg->sts; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_RTC_MOD, + .cmd = NCT6694_RTC_STATUS, + .sel = NCT6694_RTC_STATUS_SEL, + .len = cpu_to_le16(sizeof(*sts)) + }; + + if (enabled) + sts->irq_en |= NCT6694_RTC_IRQ_EN; + else + sts->irq_en &= ~NCT6694_RTC_IRQ_EN; + + sts->irq_pend = 0; + + return nct6694_write_msg(data->nct6694, &cmd_hd, sts); +} + +static const struct rtc_class_ops nct6694_rtc_ops = { + .read_time = nct6694_rtc_read_time, + .set_time = nct6694_rtc_set_time, + .read_alarm = nct6694_rtc_read_alarm, + .set_alarm = nct6694_rtc_set_alarm, + .alarm_irq_enable = nct6694_rtc_alarm_irq_enable, +}; + +static irqreturn_t nct6694_irq(int irq, void *dev_id) +{ + struct nct6694_rtc_data *data = dev_id; + struct nct6694_rtc_status *sts = &data->msg->sts; + struct nct6694_cmd_header cmd_hd = { + .mod = NCT6694_RTC_MOD, + .cmd = NCT6694_RTC_STATUS, + .sel = NCT6694_RTC_STATUS_SEL, + .len = cpu_to_le16(sizeof(*sts)) + }; + int ret; + + rtc_lock(data->rtc); + + sts->irq_en = NCT6694_RTC_IRQ_EN; + sts->irq_pend = NCT6694_RTC_IRQ_STS; + ret = nct6694_write_msg(data->nct6694, &cmd_hd, sts); + if (ret) { + rtc_unlock(data->rtc); + return IRQ_NONE; + } + + rtc_update_irq(data->rtc, 1, RTC_IRQF | RTC_AF); + + rtc_unlock(data->rtc); + + return IRQ_HANDLED; +} + +static int nct6694_rtc_probe(struct platform_device *pdev) +{ + struct nct6694_rtc_data *data; + struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent); + int ret, irq; + + irq = irq_create_mapping(nct6694->domain, NCT6694_IRQ_RTC); + if (!irq) + return -EINVAL; + + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->msg = devm_kzalloc(&pdev->dev, sizeof(union nct6694_rtc_msg), + GFP_KERNEL); + if (!data->msg) + return -ENOMEM; + + data->rtc = devm_rtc_allocate_device(&pdev->dev); + if (IS_ERR(data->rtc)) + return PTR_ERR(data->rtc); + + data->nct6694 = nct6694; + data->rtc->ops = &nct6694_rtc_ops; + data->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; + data->rtc->range_max = RTC_TIMESTAMP_END_2099; + + platform_set_drvdata(pdev, data); + + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, + nct6694_irq, IRQF_ONESHOT, + "nct6694-rtc", data); + if (ret < 0) + return dev_err_probe(&pdev->dev, ret, "Failed to request irq\n"); + + ret = devm_rtc_register_device(data->rtc); + if (ret) + return dev_err_probe(&pdev->dev, ret, "Failed to register rtc\n"); + + device_init_wakeup(&pdev->dev, true); + return 0; +} + +static struct platform_driver nct6694_rtc_driver = { + .driver = { + .name = "nct6694-rtc", + }, + .probe = nct6694_rtc_probe, +}; + +module_platform_driver(nct6694_rtc_driver); + +MODULE_DESCRIPTION("USB-RTC driver for NCT6694"); +MODULE_AUTHOR("Ming Yu "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:nct6694-rtc");