From patchwork Thu Nov 21 06:40:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 844791 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.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 BF7691C9DF6; Thu, 21 Nov 2024 06:41:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732171268; cv=none; b=qbuDKx1T2ogSMw75hVfNy2ir7/a1sduL70JnUiEJE4XE7CJD61WBg/u+a9SfIrVgcRSK+X2P48SKS7OPU0nqwYCU3t3AH/cg35vxkzyd52m69K+pZSn5L7mvyxicYV8zzKBUF4oRPWBpqUiTFndJxFJ3rML9nftc8Nm7sCsU3ik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732171268; c=relaxed/simple; bh=1Ad7boXiK81bBb4hrpnHXDp6RHFHSLujsHdBxtE9+dc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HKM/JNQQCAuHgu0snVq6yn2vQWaSwDwpaUx+r89WmAjK4f/VyMv+foMGHPXTvJGSQ6i4Ysxijq0gg/g1kNRSgZHI0L0QOm7Eqke9qRNjHcmsRQKk8D0DYm/IDdjQ+cI+rRfjXjhJrg0gO6zx6fDzR8rXBXh7cMftrcOK0BTskOA= 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=PnSk+T3A; arc=none smtp.client-ip=209.85.210.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="PnSk+T3A" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-720b2d8bcd3so459614b3a.2; Wed, 20 Nov 2024 22:41:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732171266; x=1732776066; 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=/QRLyHmPfdTDtbGtNfafkBqirNBX/D0Qp3Sa4aEfp6A=; b=PnSk+T3AagKLLAi+uMk29X4TZbO2+o5KhecxsIqj68184eOq7HC3s0PZu82Hk4Fdi8 +W0pvTT/K6Jyps2Dfvco3DP62knAVGYWSK6FCenSsplig2F7g6ZwFhWlc63gU9JD2jCs MHawau7XOhJ3uv/gM8MBq9bbwx75tz/sotGYNuSSu2nFPGKi2fF6XndeIXtz9LTYNCTS d3z6C+8zos7JOJgX/f6Je5Rzj3gcAhjIYtkc3WvMUFdSfX2y/vUkVYJdQwdX+n1kiOh/ bVL98eWaunaK83M3cavXbe/sRY/n7vySpgF3UaiFpXVtgm8QnK5vIIkNDEK/FKFv2NS6 5uWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732171266; x=1732776066; 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=/QRLyHmPfdTDtbGtNfafkBqirNBX/D0Qp3Sa4aEfp6A=; b=Sgd2vyxHq7De7oEGYntIcWDEoJ2OtQqbB6snhYnlYZMh/1URc+xK9BxGRQYNi3Gz9G F0/tXxjfmAj8Mj2bUKck4e9SGdCDu7PE9Urm7Jts8P3Bi/uoVCV+HsnWudsoY0aoXJzc E2ayb0i0zhuJ33BXJRy7AyM2sXxFs4BbZ6j56J7964JiV4kgzT9/Q2A4t6E54Mh1fPUZ 4FieI57d/EIWtsbcX53xC/hWNTTOTKEl9APFrPuoigdQxe84GT7zL6n5jPzLP5JGvHDU iP1iGNyYvS4hkLyvdDWNezhiI32b+Gli61j/sJe7MjoGXb0eegMRF0PWSoxNzzOV79jL RH4Q== X-Forwarded-Encrypted: i=1; AJvYcCU4dx5wHvdMRS06SheDwUgWR5k8ozVes4K/2fZjzmV6u1mT19FqVDMR9vQdwGXNBecOsaPuRIM26fw=@vger.kernel.org, AJvYcCUYlaKv+hSiSAKgEGyUQ6524aUojyTgY7Wqc7LmTrAl+RcaT8kfpzPhG4d1yA2by4DqztbQ0Tn9@vger.kernel.org, AJvYcCV8Yq0m8eQvNu2N7eYT/ljULOXRgbh2FJGTLjKHKg/GpLFwNjuaUInVPuswziCYwkOlcRjQZg7ahC35VZ4=@vger.kernel.org, AJvYcCW2wjkTNYyC6klHNtWzVvr9y+DoKO/GU77CcmAT1aaYClj+MZC4KN9anP+UaIviyLi/ZNsX6dtucnuv@vger.kernel.org, AJvYcCW2xOE0Ff+b8fbur2EOOqkSjI5af6l1f1DC+he+V6popqrnLWVso1EAyc5sPnFrDeVecT2eJOpdsCHdZw==@vger.kernel.org, AJvYcCWSWAOQC3HFGrjMkOHGso8HGiCl3Mw2/BxVqi6efxCH8/BnMmNlJARdGSycyw7sRfx0RN/9e+eQkGMklBNC24Y=@vger.kernel.org, AJvYcCWdMCq6+viiB1lNGmDYjCU9ZcchDgalULxmrgUhS3ihhVxGVSuLk87hKj2KUSaU4dmlkYMPx9L9GFjy@vger.kernel.org X-Gm-Message-State: AOJu0YwaShS0EP+iX2Axes5z8yWrz6JrechGgkEz7CaLTiq6ENo+3CV+ CInMxpmDQQyT/wbqwd20GHdUuGMUwicfEola4hJELoS1v46gV6eV X-Gm-Gg: ASbGncv8PbtZTViBM/26IJbpdTS8WqY98gKdxFcKepGi6RRYZqiAA1j54p9f7CJFfKI yu0H3nX8CgJNE/nyvsb9DVKBKEqqL14HaWbLUr4M+noCayKp4GYh7T+GgmiA00F4Nso6OdPcCmf 7ID5LWSjBph0aldayR8osTxF5bEZ9Dw/6+LG9l6F+NZcybXMm93YtE0eLZHmUtypxjYjxNVs/Qe c72id7l12uM7gj1DZ6jxuRY2gIHjxsU307q2JVYIG7JDblFeAHPlw8C7RF2/UZUqWfX6CUixT5c kqqQiVtF4fB3 X-Google-Smtp-Source: AGHT+IFKyvoGhto6kqRGfdZYJqk0LQiUr14qPytVmRakJyFaXuQWSdCzdI4G1KNzn/yuPsZOehr0uw== X-Received: by 2002:a05:6a00:9293:b0:71e:55e2:2c54 with SMTP id d2e1a72fcca58-724becb8374mr6248484b3a.12.1732171265891; Wed, 20 Nov 2024 22:41:05 -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-724beeb83d4sm2812530b3a.40.2024.11.20.22.41.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 22:41:05 -0800 (PST) From: Ming Yu X-Google-Original-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 Subject: [PATCH v2 1/7] mfd: Add core driver for Nuvoton NCT6694 Date: Thu, 21 Nov 2024 14:40:40 +0800 Message-Id: <20241121064046.3724726-2-tmyu0@nuvoton.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241121064046.3724726-1-tmyu0@nuvoton.com> References: <20241121064046.3724726-1-tmyu0@nuvoton.com> Precedence: bulk X-Mailing-List: linux-i2c@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 | 382 ++++++++++++++++++++++++++++++++++++ include/linux/mfd/nct6694.h | 139 +++++++++++++ 5 files changed, 540 insertions(+) create mode 100644 drivers/mfd/nct6694.c create mode 100644 include/linux/mfd/nct6694.h diff --git a/MAINTAINERS b/MAINTAINERS index 67d2159406c2..9c9a94ff8f98 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16541,6 +16541,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 03c1e4e3eea4..07ccc32f9985 100644 --- a/drivers/mfd/Kconfig +++ b/drivers/mfd/Kconfig @@ -558,6 +558,16 @@ config MFD_MX25_TSADC i.MX25 processors. They consist of a conversion queue for general purpose ADC and a queue for Touchscreens. +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 devcie driver and core APIs. + Additional drivers must be enabled in order to use the functionality + of the device. + config MFD_HI6421_PMIC tristate "HiSilicon Hi6421 PMU/Codec IC" depends on OF diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile index e057d6d6faef..9d0365ba6a26 100644 --- a/drivers/mfd/Makefile +++ b/drivers/mfd/Makefile @@ -117,6 +117,8 @@ obj-$(CONFIG_TWL6040_CORE) += twl6040.o obj-$(CONFIG_MFD_MX25_TSADC) += fsl-imx25-tsadc.o +obj-$(CONFIG_MFD_NCT6694) += nct6694.o + obj-$(CONFIG_MFD_MC13XXX) += mc13xxx-core.o obj-$(CONFIG_MFD_MC13XXX_SPI) += mc13xxx-spi.o obj-$(CONFIG_MFD_MC13XXX_I2C) += mc13xxx-i2c.o diff --git a/drivers/mfd/nct6694.c b/drivers/mfd/nct6694.c new file mode 100644 index 000000000000..071333d13eae --- /dev/null +++ b/drivers/mfd/nct6694.c @@ -0,0 +1,382 @@ +// 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 + +#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 support!\n", __func__); + break; + case NCT6694_NO_RESPONSE_ERROR: + dev_dbg(dev, "%s: Command is no response!\n", __func__); + break; + case NCT6694_TIMEOUT_ERROR: + dev_dbg(dev, "%s: Command is timeout!\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, u8 mod, u16 offset, + u16 length, void *buf) +{ + struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; + struct nct6694_response_header *response_header = nct6694->response_header; + struct usb_device *udev = nct6694->udev; + int tx_len, rx_len, ret; + + guard(mutex)(&nct6694->access_lock); + + /* Send command packet to USB device */ + cmd_header->mod = mod; + cmd_header->cmd = offset & 0xFF; + cmd_header->sel = (offset >> 8) & 0xFF; + cmd_header->hctrl = NCT6694_HCTRL_GET; + cmd_header->len = length; + + ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, NCT6694_BULK_OUT_EP), + cmd_header, NCT6694_CMD_PACKET_SZ, &tx_len, + nct6694->timeout); + if (ret) + return ret; + + /* Receive response packet from USB device */ + ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, NCT6694_BULK_IN_EP), + response_header, NCT6694_CMD_PACKET_SZ, &rx_len, + nct6694->timeout); + if (ret) + return ret; + + ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, NCT6694_BULK_IN_EP), + buf, NCT6694_MAX_PACKET_SZ, &rx_len, nct6694->timeout); + if (ret) + return ret; + + return nct6694_response_err_handling(nct6694, response_header->sts); +} +EXPORT_SYMBOL(nct6694_read_msg); + +int nct6694_write_msg(struct nct6694 *nct6694, u8 mod, u16 offset, + u16 length, void *buf) +{ + struct nct6694_cmd_header *cmd_header = nct6694->cmd_header; + struct nct6694_response_header *response_header = nct6694->response_header; + struct usb_device *udev = nct6694->udev; + int tx_len, rx_len, ret; + + guard(mutex)(&nct6694->access_lock); + + /* Send command packet to USB device */ + cmd_header->mod = mod; + cmd_header->cmd = offset & 0xFF; + cmd_header->sel = (offset >> 8) & 0xFF; + cmd_header->hctrl = NCT6694_HCTRL_SET; + cmd_header->len = length; + + ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, NCT6694_BULK_OUT_EP), + cmd_header, NCT6694_CMD_PACKET_SZ, &tx_len, + nct6694->timeout); + if (ret) + return ret; + + ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, NCT6694_BULK_OUT_EP), + buf, length, &tx_len, nct6694->timeout); + if (ret) + return ret; + + /* Receive response packet from USB device */ + ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, NCT6694_BULK_IN_EP), + response_header, NCT6694_CMD_PACKET_SZ, &rx_len, + nct6694->timeout); + if (ret) + return ret; + + ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, NCT6694_BULK_IN_EP), + buf, NCT6694_MAX_PACKET_SZ, &rx_len, nct6694->timeout); + if (ret) + return ret; + + return nct6694_response_err_handling(nct6694, 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); + + if (*int_status & (1 << irq)) + generic_handle_irq_safe(irq_find_mapping(nct6694->domain, irq)); + + *int_status &= ~(1 << irq); + } + +resubmit: + ret = usb_submit_urb(urb, GFP_ATOMIC); + if (ret) + dev_dbg(dev, "%s: Failed to resubmit urb, status %d", + __func__, 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); + unsigned int irq = data->hwirq; + + nct6694->irq_enable |= (1 << irq); +} + +static void nct6694_irq_disable(struct irq_data *data) +{ + struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data); + unsigned int irq = data->hwirq; + + nct6694->irq_enable &= ~(1 << irq); +} + +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 device *dev = &udev->dev; + struct usb_host_interface *interface; + struct usb_endpoint_descriptor *int_endpoint; + struct nct6694 *nct6694; + struct nct6694_cmd_header *cmd_header; + struct nct6694_response_header *response_header; + 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); + + cmd_header = devm_kzalloc(dev, sizeof(*cmd_header), + GFP_KERNEL); + if (!cmd_header) + return -ENOMEM; + + response_header = devm_kzalloc(dev, sizeof(*response_header), + GFP_KERNEL); + if (!response_header) + return -ENOMEM; + + nct6694->int_buffer = devm_kcalloc(dev, NCT6694_MAX_PACKET_SZ, + sizeof(unsigned char), 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) + return -ENODEV; + + nct6694->udev = udev; + nct6694->timeout = NCT6694_URB_TIMEOUT; /* Wait until urb complete */ + nct6694->cmd_header = cmd_header; + nct6694->response_header = response_header; + + mutex_init(&nct6694->access_lock); + mutex_init(&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; + + dev_info(dev, "Probed device: (%04X:%04X)\n", id->idVendor, id->idProduct); + return 0; + +err_mfd: + usb_kill_urb(nct6694->int_in_urb); +err_urb: + usb_free_urb(nct6694->int_in_urb); + return dev_err_probe(dev, ret, "Probe failed\n"); +} + +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..9733d47f5a01 --- /dev/null +++ b/include/linux/mfd/nct6694.h @@ -0,0 +1,139 @@ +/* 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_MAX_PACKET_SZ 0x200 + +#define NCT6694_CMD_PACKET_SZ 0x8 +#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 nct6694 { + struct usb_device *udev; + struct urb *int_in_urb; + struct irq_domain *domain; + struct nct6694_cmd_header *cmd_header; + struct nct6694_response_header *response_header; + struct mutex access_lock; + struct mutex irq_lock; + unsigned char *int_buffer; + unsigned int irq_enable; + /* time in msec to wait for the urb to the complete */ + long timeout; +}; + +struct nct6694_cmd_header { + unsigned char rsv1; + unsigned char mod; + unsigned char cmd; + unsigned char sel; + unsigned char hctrl; + unsigned char rsv2; + unsigned short len; +} __packed; + +struct nct6694_response_header { + unsigned char sequence_id; + unsigned char sts; + unsigned int rsv; + unsigned short len; +} __packed; + +/* + * nct6694_read_msg - Receive data from NCT6694 USB device + * + * @nct6694 - Nuvoton NCT6694 structure + * @mod - Module byte + * @offset - Offset byte or (Select byte | Command byte) + * @length - Length byte + * @buf - Read data from rx buffer + * + * USB Transaction format: + * + * OUT |RSV|MOD|CMD|SEL|HCTL|RSV|LEN_L|LEN_H| + * OUT |SEQ|STS|RSV|RSV|RSV|RSV||LEN_L|LEN_H| + * IN |-------D------A------D------A-------| + * IN ...... + * IN |-------D------A------D------A-------| + */ +int nct6694_read_msg(struct nct6694 *nct6694, u8 mod, u16 offset, + u16 length, void *buf); + +/* + * nct6694_read_msg - Transmit data to NCT6694 USB device + * + * @nct6694 - Nuvoton NCT6694 structure + * @mod - Module byte + * @offset - Offset byte or (Select byte | Command byte) + * @length - Length byte + * @buf - Write data to tx buffer + * + * USB Transaction format: + * + * OUT |RSV|MOD|CMD|SEL|HCTL|RSV|LEN_L|LEN_H| + * OUT |-------D------A------D------A-------| + * OUT ...... + * OUT |-------D------A------D------A-------| + * IN |SEQ|STS|RSV|RSV|RSV|RSV||LEN_L|LEN_H| + * IN |-------D------A------D------A-------| + * IN ...... + * IN |-------D------A------D------A-------| + */ +int nct6694_write_msg(struct nct6694 *nct6694, u8 mod, u16 offset, + u16 length, void *buf); + +#endif From patchwork Thu Nov 21 06:40:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 844790 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.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 3BEF615099D; Thu, 21 Nov 2024 06:41:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732171275; cv=none; b=WPtjTY+8dY7giXofTXaMrMgb/vcGfnfmLKo2SeIepD8gF2hJHCxJm2P5o5Pr/gj2JRmrNyvvpSt3IiXETOoV68AqJ7bwN3O24zTOT0r9mppLyEumTh3an+ElKrsh5kQeK6NdzYNrNbXv0SWgQ4nVott9Q9dRymrVuXd+YBzIAAs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732171275; c=relaxed/simple; bh=44flUDwzHTOuwdS/AmpIAJD6AXf1fHpI1Z+26lfCyYk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EmPgHYJ7UKunDvIhnpFMgf4IJTvESYgUIzwHhevL0F/Id23Hz9YwVzNeXCPQue63Fy9i2RpdAQzNoTuLCho2z73wUTN9XR13GmQbzQ21DWqIbEcHXkws6JW3Bt3tb5YWoJ9J/U/1z2DukLI9DzcpGuNu0xSPc7nk5v4QmX0uAUY= 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=kDkEftL2; arc=none smtp.client-ip=209.85.215.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="kDkEftL2" Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7f46d5d1ad5so482379a12.3; Wed, 20 Nov 2024 22:41:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732171273; x=1732776073; 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=Z06lEdw1Wb06OgkCMR/MvOB5af33NE4Om1wi64Y5ueM=; b=kDkEftL2wvVXT/XWdd1TrBfjFviA4wb18j4CSUQFsvHhi97iQlDApeVMfIVq7WfA3r LrR5DF+oStYWCjVg2dfPudaHE6KQtsHuZFpF3PUNA/sh7pQJXKuog8LoNn0Zqe7Oc2zS wsE+MovHSMReJYvchRFOd6gdS4J0KZWMeRYUaeG303Zge/pr2pDK0MIyv1ngE0/s7GhE NlnHZvIhAARqdzNFellvuw6VyP0wL24+y8wZB1dn7vKSuUqDYTmSjbyVaTitU4Z+JlEl Q/9WX9VepjlcFXidz/bTU3aThHizbWkm/PJ6BbkZRQvMYcSsYgXlABMF5nFRWnX9aci2 JDXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732171273; x=1732776073; 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=Z06lEdw1Wb06OgkCMR/MvOB5af33NE4Om1wi64Y5ueM=; b=waBywvu6NFPJoO6ziK5ROfjHA/HBDrao8daWMDn77J34K4BrwiOjr6/nC+T/ow5h21 JWM7iTCospJW1ACNt6oh2OjRosoztnPEwW0co4wPFFcOADTmUQ8U0FcTnB7g1Atb2ex8 XBzPGSyd9Aw2+DKElPrFpmrFigvoUODdamCah0CIK9/xhmv54QJFbdRngmoztn2necgR xoCZawI7JOyQyrvhh2CqlaXnrkaRYsAc8DR/QzQf96vmDSzmDj8NVHC1HvGhnE9V77R8 vpp1Q9QRnQwjNQToJmmzRv+aKEGEQhdPWQv1DWsXRV/PJkHhyQ8UhgwAc6T0OIJqBuun LVEg== X-Forwarded-Encrypted: i=1; AJvYcCU2435wSl+/QZg45xqb+Fksw23Am4Xmjlh8LtkfufyyUtApCbVMOWT/jiIy5Kgvk6QXwQBKf9w8iN8TdPE=@vger.kernel.org, AJvYcCU4KaxQlG8++4qZj/F5FZ94MxFwA0WIu9toegiBFkOXoNAfZmH1Xeodn6PLTpZD3snJp9/TX3aN@vger.kernel.org, AJvYcCUZHapRQEJjKw1hgsSF0mq6CVAKeNA5bEIpxpG7YMT+ogWZMk3y98g6gnywMYqN3wSP6Sl2gN3rfC8UnA==@vger.kernel.org, AJvYcCVNlcsRVG1gM3ohe82T/D3DOveLNvr7Wg0D/qM0TC7WKzj5PZxCDXFSfipxNodtOzJEZVgk2xY2Q5s=@vger.kernel.org, AJvYcCVPI8HnnOi8ii9c21t+fYF5nIhE+FmfYRZlbIAnW8ljTmNddR13C0ahPXVdlIME6fZTRfoNxWKGcP3o@vger.kernel.org, AJvYcCWUc6sgRsP9rNsmLp+aHtCm4TH4EE2rVinv5LuY6jazXfIUW/GLSG89ZMVLJl3tfMRBAXhrgPACPLWy@vger.kernel.org, AJvYcCXJP8qMpaduXhmyf5bkJ1/Q7OwPbwQfYeDm9b2pcGHK5rC0skTWVRXx+CTBU9siyBHeitR2vuGmdkzNdVxzBPc=@vger.kernel.org X-Gm-Message-State: AOJu0YwmY1kwCwf3y1AU5u90TxLkTKuhy6aMU8Ow1ragP1+9LaIhqDEZ SYGaZoSmhZ8fW4tVXTiIBwhgdJMXmxlPf58r5t0OJCzs+hCFdf5P X-Gm-Gg: ASbGncuHQGlrZUfVxP32A5ns1EC/8Jqrffhd6ocDnlbikk2ldHqC/1X25X07GYopNCP qiVCPfUsiHJisonAfjPl6iioOttPbo2H+c0pwY7PUoBkgUOC8J44nxwxbtp/8zHEA6geO+lswGh 4/lpMJ19t+KK+BdmA9Dl+GYSCBANbHZhAv6CjjT3C79sjZ+XYsOuQchw6tb3fQAOVsIzlXuz6aw 1NCfJdIAtmkC5whUCMvVbTf4CbevG3aumZG0Lt2B5UdOJ/pjJBU8LBPD7Hz4lUAL/BEygrwZUos 59TwTwEelSE9 X-Google-Smtp-Source: AGHT+IHwkLpxRqEoEahyxYL8ckipeHn0RrOylLzacy2EPPt1/BMEDWRQZS0KNwimnpjdKEtj3rk1ng== X-Received: by 2002:a05:6a20:158f:b0:1db:edb3:ed87 with SMTP id adf61e73a8af0-1ddaecd0d51mr8338555637.22.1732171273445; Wed, 20 Nov 2024 22:41:13 -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-724beeb83d4sm2812530b3a.40.2024.11.20.22.41.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 22:41:13 -0800 (PST) From: Ming Yu X-Google-Original-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 Subject: [PATCH v2 3/7] i2c: Add Nuvoton NCT6694 I2C support Date: Thu, 21 Nov 2024 14:40:42 +0800 Message-Id: <20241121064046.3724726-4-tmyu0@nuvoton.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241121064046.3724726-1-tmyu0@nuvoton.com> References: <20241121064046.3724726-1-tmyu0@nuvoton.com> Precedence: bulk X-Mailing-List: linux-i2c@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 | 152 +++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 drivers/i2c/busses/i2c-nct6694.c diff --git a/MAINTAINERS b/MAINTAINERS index 6688c5c470b7..a190f2b08fa3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16546,6 +16546,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 4977abcd7c46..1962cf1e71f9 100644 --- a/drivers/i2c/busses/Kconfig +++ b/drivers/i2c/busses/Kconfig @@ -1325,6 +1325,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 a6bcbf2febcf..6d2fd8e56569 100644 --- a/drivers/i2c/busses/Makefile +++ b/drivers/i2c/busses/Makefile @@ -135,6 +135,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..2a5e0629f860 --- /dev/null +++ b/drivers/i2c/busses/i2c-nct6694.c @@ -0,0 +1,152 @@ +// 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 + +/* Host interface */ +#define NCT6694_I2C_MOD 0x03 + +/* Message Channel*/ +/* Command 00h */ +#define NCT6694_I2C_CMD0_OFFSET 0x0000 /* OFFSET = SEL|CMD */ +#define NCT6694_I2C_CMD0_LEN 0x90 + +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_cmd0 { + 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; + unsigned char *xmit_buf; + 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_cmd0 *cmd = (struct nct6694_i2c_cmd0 *)data->xmit_buf; + int ret, i; + + for (i = 0; i < num ; i++) { + struct i2c_msg *msg_temp = &msgs[i]; + + memset(data->xmit_buf, 0, sizeof(struct nct6694_i2c_cmd0)); + + if (msg_temp->len > 64) + return -EPROTO; + cmd->port = data->port; + cmd->br = data->br; + cmd->addr = i2c_8bit_addr_from_msg(msg_temp); + if (msg_temp->flags & I2C_M_RD) { + cmd->r_cnt = msg_temp->len; + ret = nct6694_write_msg(data->nct6694, NCT6694_I2C_MOD, + NCT6694_I2C_CMD0_OFFSET, + NCT6694_I2C_CMD0_LEN, + cmd); + if (ret < 0) + return 0; + + memcpy(msg_temp->buf, cmd->read_data, msg_temp->len); + } else { + cmd->w_cnt = msg_temp->len; + memcpy(cmd->write_data, msg_temp->buf, msg_temp->len); + ret = nct6694_write_msg(data->nct6694, NCT6694_I2C_MOD, + NCT6694_I2C_CMD0_OFFSET, + NCT6694_I2C_CMD0_LEN, + cmd); + if (ret < 0) + return 0; + } + } + + 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->xmit_buf = devm_kcalloc(&pdev->dev, NCT6694_MAX_PACKET_SZ, + sizeof(unsigned char), GFP_KERNEL); + if (!data->xmit_buf) + 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"); From patchwork Thu Nov 21 06:40:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 844789 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 D8C5C1CD1FC; Thu, 21 Nov 2024 06:41:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732171283; cv=none; b=aMMD4/+3r2DOHct3jsj/kW188j3UyJMxWHg+bUSH2FSL1WKdSTMltbhZFQod9SSSkeEserIzNNFOhtd97TsWYTqAy6lcXYaM6eJ0KX+ZPwzVGO7tta6IM+ONxGstoUOdSGrG4JCbrApCnF9War5DeESYXfCjVS7mtF5UkvsPX+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732171283; c=relaxed/simple; bh=CPXoehGwTSs45YlcqtJ+F/QvQrwzI5sbYSnD2Pak/mE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L2UOKXwtpFoVh0KeepTmWhttamUsE7pqLbjK+7PqS0IUFbS2ffxfTuoqq5wEER4eRTM1hrC7c49d5tXpzHzu3GLiO4mgwdgzzcx3Mo5qXGS1MlcFrxQhZ26V4Ri+rEKpS6cD8Hxu4xncd5lhpNheySyeRo9JtoeJx9LY6N+Jp14= 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=WVhHMXsR; arc=none smtp.client-ip=209.85.215.180 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="WVhHMXsR" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7f4325168c8so396126a12.1; Wed, 20 Nov 2024 22:41:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732171281; x=1732776081; 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=gsLP5BfEq+ny/TNJBmMYQ+aCSSVlRXL5xPNb64PDy8c=; b=WVhHMXsRnlnd9qol6ZM4YbxZNA+HOTThBF4TuPQ+zcdeze1bJYp4XBFYC1O2dlFqFF xD9YpWRjU6UpBnXY9G4erbIWZrpluXjJ6Km/YlHKXsB/lcnN5KpHgA0u5E54m0+h5iHU k/PmJVNSVRKIsNEd173KnYH7kbIh2e1M5EirTcJkTSBf2tWfFSjUhYJUMZfxhFYejwAZ y9L4+m9AZz7L/sIsIFfw76vYIqkkMdjDk7HwJG/Pyi4/rE5YxOFkny45U7RqCFiUJxD7 AqrWCTPjeP5si2BOSheLmyoMrX+itAokLM2fG3YMfwTlxF81Kh7G7GhOUoD7eKy3GSvC uzEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732171281; x=1732776081; 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=gsLP5BfEq+ny/TNJBmMYQ+aCSSVlRXL5xPNb64PDy8c=; b=mUoM1ZIJRa+gcQSU1qT3TeiZlR7t1BmX29oVPkrq3Enoa/zVPC7a9DkhgomqIYEWwa TUYe4Sles/5CW5DZL4f6bA97S0Xrg1aw0t/T+tqblUJudBtjFzoBsl1YidwcvpT09F/S 1fF7L9qdm19Fm0B8qYpOBL+dR0RHtFevcQatRPXLUEAVK+/p7cy3ykNlKScDZiXKUvN/ cIvXicgZ84ourP5/642yCwesvfXvD91RvpHE/9xLMoLamwYQy/vY6rg6Y+Pz6IbHS/pI ygQUatmvfiSVx4WmX9rUha3q9//mLwTqXgxa2/VscIHEv0V9xYhk4TFVi171EBbaR5dL Ex6A== X-Forwarded-Encrypted: i=1; AJvYcCUcf2yrw4wTju8UdcnBqoo5vd5KUMgzOPLhUz8VclTOkNgImzXrQXFLL/MIIrb9xrXTBXFTkURAKxXXyg==@vger.kernel.org, AJvYcCUhNWkwhDEtfdCYl7EdAP/gDb+OMkTr7dZDqrYic2PArnEg0sK/D2FTx+4Ds7nWLlERzXpd1EPOj2kw2m+qSh0=@vger.kernel.org, AJvYcCUsR/qzHGav9Sk+aEexkV2wx8f164L3DLkjTgheQ70EXSyfBmTsPsf8kNZAvl2CXUQVRf4sLSx8xng=@vger.kernel.org, AJvYcCVYGK7LM4m3eYiRqUQ+cTq6H/RmWcUh4vJQRwVRb8Lgr0C/ihhcGtG94RjJuDA38OIVH+bmzBUnfvRo@vger.kernel.org, AJvYcCWZwpQXHjJPdK+83pYzjAyQKO105ATlp1R/SWCICNysbXtUqHeZvDJ9DgTrBWTrxY5VRijrK8wai48VE8g=@vger.kernel.org, AJvYcCWjTCmk2/G3UCQ5zbdXUF2Q3OexwzOhvRquypS4iM9uRZG0aPWFFttJTH0WvdUsr9sKxKoh+bJN@vger.kernel.org, AJvYcCXRGl5qSI/VdzeqwnTyTeHUoGc92NsYcnWYx6UtRaBd7HTwvDPahQIGny62oJtljH0vsdIlr1Nqj8nt@vger.kernel.org X-Gm-Message-State: AOJu0YxfTwEmMKO9Eh4WLgyBGcpruR9VVtOCT9gvO9ehU+XRXUKAuopS vtjrly8gMNwW7kFmf8AlEzPFPK1hXHknOFghNSgMYXIfq2iLRVMr X-Google-Smtp-Source: AGHT+IEokQsrbNi6zBGYjcv9dBanc/vtO5pLqsDrV8MZO6La3pSabbKKLXvwdl8PKCh+QdVTOH921g== X-Received: by 2002:a05:6a20:748e:b0:1db:eb2c:a74 with SMTP id adf61e73a8af0-1ddaefbd38dmr8314594637.12.1732171281038; Wed, 20 Nov 2024 22:41:21 -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-724beeb83d4sm2812530b3a.40.2024.11.20.22.41.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 22:41:20 -0800 (PST) From: Ming Yu X-Google-Original-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 Subject: [PATCH v2 5/7] watchdog: Add Nuvoton NCT6694 WDT support Date: Thu, 21 Nov 2024 14:40:44 +0800 Message-Id: <20241121064046.3724726-6-tmyu0@nuvoton.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241121064046.3724726-1-tmyu0@nuvoton.com> References: <20241121064046.3724726-1-tmyu0@nuvoton.com> Precedence: bulk X-Mailing-List: linux-i2c@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 | 277 +++++++++++++++++++++++++++++++++ 4 files changed, 290 insertions(+) create mode 100644 drivers/watchdog/nct6694_wdt.c diff --git a/MAINTAINERS b/MAINTAINERS index eb5d46825e71..496fe7d5a23f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16549,6 +16549,7 @@ F: drivers/gpio/gpio-nct6694.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/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 2333476a42c0..851c1f17712d 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -739,6 +739,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 + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB device to watchdog timer. + + 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 386d88d89fe5..8355893b4435 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..7d2c8d5c6fa3 --- /dev/null +++ b/drivers/watchdog/nct6694_wdt.c @@ -0,0 +1,277 @@ +// 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 + +/* Host interface */ +#define NCT6694_WDT_MOD 0x07 + +/* Message Channel*/ +/* Command 00h */ +#define NCT6694_WDT_CMD0_LEN 0x0F +#define NCT6694_WDT_CMD0_OFFSET(idx) (idx ? 0x0100 : 0x0000) /* OFFSET = SEL|CMD */ + +/* Command 01h */ +#define NCT6694_WDT_CMD1_LEN 0x08 +#define NCT6694_WDT_CMD1_OFFSET(idx) (idx ? 0x0101 : 0x0001) /* OFFSET = SEL|CMD */ + +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_cmd0 { + __le32 pretimeout; + __le32 timeout; + u8 owner; + u8 scratch; + u8 control; + u8 status; + __le32 countdown; +}; + +struct __packed nct6694_wdt_cmd1 { + u32 wdt_cmd; + u32 reserved; +}; + +struct nct6694_wdt_data { + struct watchdog_device wdev; + struct device *dev; + struct nct6694 *nct6694; + struct mutex lock; + unsigned char *xmit_buf; + 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_cmd0 *buf = (struct nct6694_wdt_cmd0 *)data->xmit_buf; + struct nct6694 *nct6694 = data->nct6694; + 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(buf, 0, NCT6694_WDT_CMD0_LEN); + buf->timeout = cpu_to_le32(timeout_fmt); + buf->pretimeout = cpu_to_le32(pretimeout_fmt); + + return nct6694_write_msg(nct6694, NCT6694_WDT_MOD, + NCT6694_WDT_CMD0_OFFSET(data->wdev_idx), + NCT6694_WDT_CMD0_LEN, buf); +} + +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_info(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_cmd1 *buf = (struct nct6694_wdt_cmd1 *)data->xmit_buf; + struct nct6694 *nct6694 = data->nct6694; + + guard(mutex)(&data->lock); + + memcpy(buf, "WDTC", 4); + buf->reserved = 0; + + return nct6694_write_msg(nct6694, NCT6694_WDT_MOD, + NCT6694_WDT_CMD1_OFFSET(data->wdev_idx), + NCT6694_WDT_CMD1_LEN, buf); +} + +static int nct6694_wdt_ping(struct watchdog_device *wdev) +{ + struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev); + struct nct6694_wdt_cmd1 *buf = (struct nct6694_wdt_cmd1 *)data->xmit_buf; + struct nct6694 *nct6694 = data->nct6694; + + guard(mutex)(&data->lock); + memcpy(buf, "WDTS", 4); + buf->reserved = 0; + + return nct6694_write_msg(nct6694, NCT6694_WDT_MOD, + NCT6694_WDT_CMD1_OFFSET(data->wdev_idx), + NCT6694_WDT_CMD1_LEN, buf); +} + +static int nct6694_wdt_set_timeout(struct watchdog_device *wdev, + unsigned int timeout) +{ + struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev); + int ret; + + if (timeout < wdev->pretimeout) { + dev_warn(data->dev, "pretimeout < timeout. Setting to zero\n"); + wdev->pretimeout = 0; + } + + ret = nct6694_wdt_setting(wdev, timeout, NCT6694_ACTION_GPO, + wdev->pretimeout, NCT6694_ACTION_GPO); + if (ret) + return ret; + + wdev->timeout = timeout; + + return ret; +} + +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 ret; +} + +static unsigned int nct6694_wdt_get_time(struct watchdog_device *wdev) +{ + struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev); + struct nct6694_wdt_cmd0 *buf = (struct nct6694_wdt_cmd0 *)data->xmit_buf; + struct nct6694 *nct6694 = data->nct6694; + unsigned int timeleft_ms; + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(nct6694, NCT6694_WDT_MOD, + NCT6694_WDT_CMD0_OFFSET(data->wdev_idx), + NCT6694_WDT_CMD0_LEN, buf); + if (ret) + return ret; + + timeleft_ms = le32_to_cpu(buf->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->xmit_buf = devm_kcalloc(dev, NCT6694_MAX_PACKET_SZ, + sizeof(unsigned char), GFP_KERNEL); + if (!data->xmit_buf) + 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; + wdev->min_timeout = 1; + wdev->max_timeout = 255; + + mutex_init(&data->lock); + + platform_set_drvdata(pdev, data); + + /* Register watchdog timer device to WDT framework */ + watchdog_set_drvdata(&data->wdev, data); + watchdog_init_timeout(&data->wdev, timeout, dev); + 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"); From patchwork Thu Nov 21 06:40:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 844788 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (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 A3C411C9DDF; Thu, 21 Nov 2024 06:41:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732171291; cv=none; b=jxb241lKjnHfk3jhbyEQZT9hMbqoK0W5YGS/HI2BI315ccMEtKW4c1iqUhQajZ4L6zLoaTB38UN2ZdQNi+Z9+yLNcIElkl18F1C9NgKd1kNXb0Ajm3hHKUmYM+LFPSnNKITnl+oaYn7NYaOKyN1AVmOZEENNaHYvj/jVqtJM9oQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732171291; c=relaxed/simple; bh=VM3tcKTrNIKtcWEiY3g5a2hzNV1pFWStN+suLtgL4zk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gfbbdo2CgpGJ5hWx1PuwT3nb6Gd2hLKHI7gd/6+H3zUpKewUU5FDdevEFOYEzBge1H3Ten+0xKFkjmZioOo6KvtRXWNrVjn57Mc6rvxtJOuuCz3g38fHNm0ywwKTFrrM7S5YRNqXVrfsZ1xf3q2HG4F3hVVbeUIF8zLerdMovv4= 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=O2EyV406; arc=none smtp.client-ip=209.85.215.178 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="O2EyV406" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7ea7e250c54so503810a12.0; Wed, 20 Nov 2024 22:41:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732171289; x=1732776089; 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=PFrCg7m8aamOuaiYkktMUCP7hs/Ab5XF+r6SW6zPgUM=; b=O2EyV406jEwEvwF7Usb9d9/obwzVK78ilyxlOFweCzku6k/ej/5vYa3iYecsg/Fp/l o2wMOhBJRGV5GA+6Q2TqIIQrmdTl6Mf6llDofB95B705d7UUFeBocVrYFpv0+fYH/XWy RydZSdQh/6ASAZTAbdtubP8w5pAsdJrmUJwHyqKisFQrbzaHODQB6cLgkjKHR5/8Y6VS l6AQjp7yI/x3IHLr3Pz3Ea98fqvT1Mt/pZLkes7VDXxBAzYvQ0YzUnjxrPwQKjvme8D4 EEbXcuKjPGGzBy++8J9Jb7yimqskmesPzhfOXsXZwquuby9X0k6mn9O9cb8chmA5Dsrj mR2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732171289; x=1732776089; 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=PFrCg7m8aamOuaiYkktMUCP7hs/Ab5XF+r6SW6zPgUM=; b=tMHMCQwIe34NrDCR1KxoFJbBMSlF/+Ta+SH4NHy6pp2/3C8LIGs3Fcuzxrs5O7g/gl 2/WGdoTQXg0AX5MxhQO1NqgeqIwUtooOkSbkiRTLUEvozGVzpkNsXGx1bwqm+eQl9k6k bNfG5tW+5zZAD49G/5KhhmdQvaS2orYciAEsF9QzITtJbT4FajBLWUu+7964p9KRY31U xSu9yrTPIjKiDPPCg3BBGDSrxwlrY7LCiHvvM+eiNh1g9opNcgECSV8C0bH9wQnptkBK t+qW7rX0PXpnSYJL3sVkcR0OUW+fb1LJwNiu2imVn9+f96PTpflnsXU/xaX2tnv4EEGJ bQuQ== X-Forwarded-Encrypted: i=1; AJvYcCUmNwaMxgg82qxwQU0VZKq1OuvMnGvRwYz9M2ogQGWxFKmG5Ir5bUyQ+CFL6DJqwhtjO1694J71@vger.kernel.org, AJvYcCUqYeLJOzJNXeUQckqP0+1F+puRytpIMIVhZnBSNw5uouZ50i2zK3eLcumUcq01Yf+6AWwwve0GgjS0FsaZVso=@vger.kernel.org, AJvYcCVWTNWca7G0VBjMaWg21gTat7Jl/T/w/+59b0PYNz/HHgntLWomoQJCBKXradp5EcDC2LSyVLKWCK4kfXw=@vger.kernel.org, AJvYcCWEkUHSsu1tHoMul7Ef6BEWuD15WhZnaFrp6tkAwwQYCPNrRcDa7mqqEjPFaJBFo0YAUyF51BOe+9Y0kw==@vger.kernel.org, AJvYcCWg2hM/KjVEsdI0A9/WiLv0UJ6P8MdcgUUpfgIszGa45/uxc8OKPvDaCb0SM/MbsyNR7GXA+Rri0gxe@vger.kernel.org, AJvYcCWvKKQ3Zs84ZnvREMM4QleAHuLGOy9dzkH85prYewbVxfyLBuLWIU41UZipIxpHOagV4LBAxJQ+Xf0=@vger.kernel.org, AJvYcCX0Azd29oc/D5DAEg9L/XVSHkYK2zE5OgUzQ5voPC4iT1+BUP4OlTT/rGtD6NY6d1vVh/REbpIvm5Hi@vger.kernel.org X-Gm-Message-State: AOJu0YxzsBRI0c4/LtzwK8JcUDu2KNE0uSx1GyiSlJFcFfMYCddjTvmr taoN/HVHhUWtOzSQnt4JI64+NkPXiOv01bLTMHJhKobkzuYJzQ9t X-Google-Smtp-Source: AGHT+IHiQw2caxTW3sY1YGKi/DVCIIlxVF7EDTea2/gtWdN97dJp29cPAa2mIse1tA0QInWOyJMgbA== X-Received: by 2002:a05:6a20:841c:b0:1db:c20f:2c4d with SMTP id adf61e73a8af0-1ddae2f0277mr8333238637.2.1732171288857; Wed, 20 Nov 2024 22:41:28 -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-724beeb83d4sm2812530b3a.40.2024.11.20.22.41.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2024 22:41:28 -0800 (PST) From: Ming Yu X-Google-Original-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 Subject: [PATCH v2 7/7] rtc: Add Nuvoton NCT6694 RTC support Date: Thu, 21 Nov 2024 14:40:46 +0800 Message-Id: <20241121064046.3724726-8-tmyu0@nuvoton.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241121064046.3724726-1-tmyu0@nuvoton.com> References: <20241121064046.3724726-1-tmyu0@nuvoton.com> Precedence: bulk X-Mailing-List: linux-i2c@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 | 263 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 275 insertions(+) create mode 100644 drivers/rtc/rtc-nct6694.c diff --git a/MAINTAINERS b/MAINTAINERS index d6414eea0463..6d1cfec28076 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16550,6 +16550,7 @@ F: drivers/hwmon/nct6694-hwmon.c F: drivers/i2c/busses/i2c-nct6694.c F: drivers/mfd/nct6694.c F: drivers/net/can/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 66eb1122248b..36829d096194 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -406,6 +406,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 f62340ecc534..64443d26bb5b 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -116,6 +116,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..097555a16b4f --- /dev/null +++ b/drivers/rtc/rtc-nct6694.c @@ -0,0 +1,263 @@ +// 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 + +/* Host interface */ +#define NCT6694_RTC_MOD 0x08 + +/* Message Channel */ +/* Command 00h */ +#define NCT6694_RTC_CMD0_LEN 0x07 +#define NCT6694_RTC_CMD0_OFFSET 0x0000 /* OFFSET = SEL|CMD */ +/* Command 01h */ +#define NCT6694_RTC_CMD1_LEN 0x05 +#define NCT6694_RTC_CMD1_OFFSET 0x0001 /* OFFSET = SEL|CMD */ +/* Command 02h */ +#define NCT6694_RTC_CMD2_LEN 0x02 +#define NCT6694_RTC_CMD2_OFFSET 0x0002 /* OFFSET = SEL|CMD */ + +#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_cmd0 { + u8 sec; + u8 min; + u8 hour; + u8 week; + u8 day; + u8 month; + u8 year; +}; + +struct __packed nct6694_rtc_cmd1 { + u8 sec; + u8 min; + u8 hour; + u8 alarm_en; + u8 alarm_pend; +}; + +struct __packed nct6694_rtc_cmd2 { + u8 irq_en; + u8 irq_pend; +}; + +struct nct6694_rtc_data { + struct nct6694 *nct6694; + struct rtc_device *rtc; + struct mutex lock; + unsigned char *xmit_buf; +}; + +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_cmd0 *buf = (struct nct6694_rtc_cmd0 *)data->xmit_buf; + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_RTC_MOD, + NCT6694_RTC_CMD0_OFFSET, + NCT6694_RTC_CMD0_LEN, + buf); + if (ret) + return ret; + + tm->tm_sec = bcd2bin(buf->sec); /* tm_sec expect 0 ~ 59 */ + tm->tm_min = bcd2bin(buf->min); /* tm_min expect 0 ~ 59 */ + tm->tm_hour = bcd2bin(buf->hour); /* tm_hour expect 0 ~ 23 */ + tm->tm_wday = bcd2bin(buf->week) - 1; /* tm_wday expect 0 ~ 6 */ + tm->tm_mday = bcd2bin(buf->day); /* tm_mday expect 1 ~ 31 */ + tm->tm_mon = bcd2bin(buf->month) - 1; /* tm_month expect 0 ~ 11 */ + tm->tm_year = bcd2bin(buf->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_cmd0 *buf = (struct nct6694_rtc_cmd0 *)data->xmit_buf; + + guard(mutex)(&data->lock); + + buf->sec = bin2bcd(tm->tm_sec); + buf->min = bin2bcd(tm->tm_min); + buf->hour = bin2bcd(tm->tm_hour); + buf->week = bin2bcd(tm->tm_wday + 1); + buf->day = bin2bcd(tm->tm_mday); + buf->month = bin2bcd(tm->tm_mon + 1); + buf->year = bin2bcd(tm->tm_year - 100); + + return nct6694_write_msg(data->nct6694, NCT6694_RTC_MOD, + NCT6694_RTC_CMD0_OFFSET, + NCT6694_RTC_CMD0_LEN, + buf); +} + +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_cmd1 *buf = (struct nct6694_rtc_cmd1 *)data->xmit_buf; + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_RTC_MOD, + NCT6694_RTC_CMD1_OFFSET, + NCT6694_RTC_CMD1_LEN, + buf); + if (ret) + return ret; + + alrm->time.tm_sec = bcd2bin(buf->sec); + alrm->time.tm_min = bcd2bin(buf->min); + alrm->time.tm_hour = bcd2bin(buf->hour); + alrm->enabled = buf->alarm_en; + alrm->pending = buf->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_cmd1 *buf = (struct nct6694_rtc_cmd1 *)data->xmit_buf; + + guard(mutex)(&data->lock); + + buf->sec = bin2bcd(alrm->time.tm_sec); + buf->min = bin2bcd(alrm->time.tm_min); + buf->hour = bin2bcd(alrm->time.tm_hour); + buf->alarm_en = alrm->enabled ? NCT6694_RTC_IRQ_EN : 0; + buf->alarm_pend = 0; + + return nct6694_write_msg(data->nct6694, NCT6694_RTC_MOD, + NCT6694_RTC_CMD1_OFFSET, + NCT6694_RTC_CMD1_LEN, + buf); +} + +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_cmd2 *buf = (struct nct6694_rtc_cmd2 *)data->xmit_buf; + + guard(mutex)(&data->lock); + + if (enabled) + buf->irq_en |= NCT6694_RTC_IRQ_EN; + else + buf->irq_en &= ~NCT6694_RTC_IRQ_EN; + + buf->irq_pend = 0; + + return nct6694_write_msg(data->nct6694, NCT6694_RTC_MOD, + NCT6694_RTC_CMD2_OFFSET, + NCT6694_RTC_CMD2_LEN, + buf); +} + +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_cmd2 *buf = (struct nct6694_rtc_cmd2 *)data->xmit_buf; + int ret; + + guard(mutex)(&data->lock); + + buf->irq_en = NCT6694_RTC_IRQ_EN; + buf->irq_pend = NCT6694_RTC_IRQ_STS; + ret = nct6694_write_msg(data->nct6694, NCT6694_RTC_MOD, + NCT6694_RTC_CMD2_OFFSET, + NCT6694_RTC_CMD2_LEN, + buf); + if (ret) + return IRQ_NONE; + + rtc_update_irq(data->rtc, 1, RTC_IRQF | RTC_AF); + + 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->xmit_buf = devm_kcalloc(&pdev->dev, NCT6694_MAX_PACKET_SZ, + sizeof(unsigned char), GFP_KERNEL); + if (!data->xmit_buf) + 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; + + mutex_init(&data->lock); + + device_set_wakeup_capable(&pdev->dev, 1); + + 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"); + + /* Register rtc device to RTC framework */ + return devm_rtc_register_device(data->rtc); +} + +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");