From patchwork Fri Dec 27 09:57:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 13921656 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 EB6CE1581F0; Fri, 27 Dec 2024 09:57:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293473; cv=none; b=Z5nk/3/h5PlAAz86hBRajWnH/NJniPOYr5L25Odf/Z4+cZ5ZRdrHwHU7ILezJ3mkVq7Ello9iZxK6SQyLLDS4yzPyPYdUWTYU2XQTO4mMTX/IQMGklOLWiw6eB6JKq0F7TiA1BJqf6RR/vRA7CMuqY8Aq58JIbd4g6qvykl8nNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293473; c=relaxed/simple; bh=Jsx1eI6l7oNyRpON1/g56AAnfUO2wIdZpe12QTynu7w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jh51D4A5AuQIrQxjFM3gbvvaIfuXLF6H4Ai8psg1w7d/Lnaw/xqMcf2QgNzWbemAa2DgkY4bfltyohRVLcWjhw4ue0FrUTZTjcJ83xb9GaBI56lEvQg3ojPb9PI5rQCUCe+COC33t5N/vYmjXgqty6YSuZjpurMHPupCReSEGrs= 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=QAcDIdJ4; arc=none smtp.client-ip=209.85.214.177 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="QAcDIdJ4" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-216401de828so82375975ad.3; Fri, 27 Dec 2024 01:57:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735293470; x=1735898270; 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=Cdu04QpC6j0mTfK4Ird3hwwe7JmPciyjX7rFy0WMqgs=; b=QAcDIdJ4aP2bUyh9WgAOihquX9sRDpum+9Hpv1WUuQ/C+ODd/0Azjy8AeQBX+DR+GF exac0LEZWwEG8S+aWKXDe8gd+EKXdxiaD8I2hpLO3FG4yyFapgT/4pihujGcq7oVQTMz IuDJUq5SY5dbxyCtj03I5kVQiamz/KCRXpJBXfGyAHYWa7EHopDPHD89H/oNeTa6Qwfr bDv4cwoOw066Gdy2kAxuUcb4iBex/xmujT4lthTtQhYW/L9PpYTfwC/8vIu4/GzusZC6 SWbfu48B4Kgj71/ZfCADczrBuVfxoCnIVnSzi1GfgisMlHGqlZEsSZnwnUIAhwxiiKjx 1LtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735293470; x=1735898270; 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=Cdu04QpC6j0mTfK4Ird3hwwe7JmPciyjX7rFy0WMqgs=; b=oIfSA8iTeu43nl6eBavK4VZcGVqFx4kPiIiNwC9Y9cXpDuoIP/J80Ou9vKmQH+7SaB zE1kCCJCaXHjjZohIkOgAKEfYYBitMwg3uBK6jzJIw0di28Y0lgO3atonTsCv02yCgkV 8KNdskDplFDUmliUt6PUqHFZ/ritrNgNXKq8DFmNwk2t33dVd+N0ABGJX3Lz8sO/uatu zI5wOiSw2JaTj86ixb69f2qAyjT4eBNDxQpB9FZ3zq3cS9Tz1HLjjJkqWY48W7aTLsm8 gzn87LQ/cAJWyGqlzpXghzFjkEcNOXEaYW4gj957acyC6OCwCyayQpfV3gEFvoxbn5BR v3Ew== X-Forwarded-Encrypted: i=1; AJvYcCU6UpKx7Un4sDdwZg++CD/eLMLiMH19C1s7ZI+eyYcPCH5mGszCskrotWosg8Wgeu3Lu9aESUBueyztkBo=@vger.kernel.org, AJvYcCUid6lZq7+yn+5mWPBhd14TBWgLKI3zzZ8+frH4CtTyI1GbddP23qK8IvFfwk10YKfp3gnfAYxKdkTyEg==@vger.kernel.org, AJvYcCUwzgnTFRAfDZt0czmMd+b7zeuF1MfMmAQe8FMfjnUY6R7QTOOZOtGzmbKATry8fZFFiTVK3jzSgxis@vger.kernel.org, AJvYcCUyk5A5ksLEn+D7KFUzfjaPoEiObCbSSLNt+60IIHxT22BYldXWrypoW0oUP75mbw0LsWqWteI8@vger.kernel.org, AJvYcCVdUlF6By90yW/nDejWXpbC4/s42hT4TFWRaBQb1Uo68uiYCHnfZm+/JKeYM0ayqsrhJ6hUTWqfFB6IU0/rgX8=@vger.kernel.org, AJvYcCVfWmKsAbggjngMC+c3BG/rshOSlbl/efPDuWnZwR558AXo8SWRFOcOsHfPXd9uMmIGzr/XNGnj2t3/@vger.kernel.org, AJvYcCWfa/WdV9XxjC5SIPJ+wGqiFp1SXxg95m1sDv2j1V9HWONTqdMcu9mdCa43iPhRq1blu85NgHJnwRY=@vger.kernel.org X-Gm-Message-State: AOJu0YyL+IKXuOCHZlkhPNMjoX1Z5dViXQYw0p9I38szB4lyPoydLFoM ul/KDdwFmpxZmxkvTqnTUs+hg5RctSwJxPwckWhjLBgqC4E/P25r X-Gm-Gg: ASbGncs8Koib5t+IQ6Yu4CevzwRzNkagZgG2gypQka6CJWNARIItnFGHeAzdvjaZ5ji cb5sJ16rGLKUdXfsxMNC49AfIF+Cs1uLrYFIFNaqYxIZuY0gPT04lQ/I3R2/RH9teftpuYUFuOe b8mFJBQx4gc0AIwJEL91gpJnv+ioyyXIKr1H/j/M1Nz//6kQZWxBPN0ibAdI0dOv139O2o6j/QJ nqStJdERfIDaobcLHQh/t7P/XFANFwXm3ScqsjxakdVIMwHNWsS53Tk8CFQ2/4mG9LRkMKc+1Ek YwRQlf44ZmlMbD7gFhw/JWTS X-Google-Smtp-Source: AGHT+IGAO3L9c1yK2qluZBnPHL/w4G2vF8U6AyH30aJvuqm4egXRq86NRVKi3U5wyHvffZZAPr4jPw== X-Received: by 2002:a17:903:120e:b0:215:a179:14d2 with SMTP id d9443c01a7336-219e6f27fc3mr304961765ad.50.1735293470151; Fri, 27 Dec 2024 01:57:50 -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 d9443c01a7336-219dc9f51a0sm131581135ad.187.2024.12.27.01.57.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Dec 2024 01:57:49 -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, Ming Yu Subject: [PATCH v4 1/7] mfd: Add core driver for Nuvoton NCT6694 Date: Fri, 27 Dec 2024 17:57:21 +0800 Message-Id: <20241227095727.2401257-2-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241227095727.2401257-1-a0282524688@gmail.com> References: <20241227095727.2401257-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-hwmon@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 | 394 ++++++++++++++++++++++++++++++++++++ include/linux/mfd/nct6694.h | 142 +++++++++++++ 5 files changed, 555 insertions(+) create mode 100644 drivers/mfd/nct6694.c create mode 100644 include/linux/mfd/nct6694.h diff --git a/MAINTAINERS b/MAINTAINERS index 910305c11e8a..acb270037642 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16722,6 +16722,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..5429ba97b457 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..0f31489ef9fa --- /dev/null +++ b/drivers/mfd/nct6694.c @@ -0,0 +1,394 @@ +// 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 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) +{ + union nct6694_usb_msg *msg = nct6694->usb_msg; + int tx_len, rx_len, ret; + + guard(mutex)(&nct6694->access_lock); + + memset(msg, 0, sizeof(*msg)); + + /* Send command packet to USB device */ + msg->cmd_header.mod = mod; + msg->cmd_header.cmd = offset & 0xFF; + msg->cmd_header.sel = (offset >> 8) & 0xFF; + msg->cmd_header.hctrl = NCT6694_HCTRL_GET; + msg->cmd_header.len = cpu_to_le16(length); + + 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, NCT6694_MAX_PACKET_SZ, &rx_len, nct6694->timeout); + if (ret) + return ret; + + if (rx_len != length) { + 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, u8 mod, u16 offset, + u16 length, void *buf) +{ + union nct6694_usb_msg *msg = nct6694->usb_msg; + int tx_len, rx_len, ret; + + guard(mutex)(&nct6694->access_lock); + + memset(msg, 0, sizeof(*msg)); + + /* Send command packet to USB device */ + msg->cmd_header.mod = mod; + msg->cmd_header.cmd = offset & 0xFF; + msg->cmd_header.sel = (offset >> 8) & 0xFF; + msg->cmd_header.hctrl = NCT6694_HCTRL_SET; + msg->cmd_header.len = cpu_to_le16(length); + + 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, length, &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, NCT6694_MAX_PACKET_SZ, &rx_len, nct6694->timeout); + if (ret) + return ret; + + if (rx_len != length) { + 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 %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); + 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..38c8c7af10d5 --- /dev/null +++ b/include/linux/mfd/nct6694.h @@ -0,0 +1,142 @@ +/* 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 512 + +#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_cmd_header { + u8 rsv1; + u8 mod; + u8 cmd; + u8 sel; + u8 hctrl; + u8 rsv2; + __le16 len; +} __packed; + +struct nct6694_response_header { + u8 sequence_id; + u8 sts; + u8 reserved[4]; + __le16 len; +} __packed; + +union 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; +}; + +/* + * 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 Fri Dec 27 09:57:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 13921657 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 30F72156887; Fri, 27 Dec 2024 09:57:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293477; cv=none; b=sqsWLfYvenHZZzO9LuDFMrpgFTgtfn6kORazqsFgo6YDzz1xJLyam2C8yGPXRodwx4t0rhJlSOXZRlMjr6FsbgTSifFH5ZXPgEWHSCszIxzB9AHHqdpGaOCoQVHogiRXZTVkjwiSrfBUSpn7sIHOuCVB7nfqKf6bdedtyXu8UrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293477; c=relaxed/simple; bh=TTr9/UF69eBvGH1icxAjRSWqCAA9DjbfFySV+xlmv8E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=vGO6n1J4RaDxxibo2QSPSlHfMqj4Eb1Ybis8Gnz1suTBmGh5xu2Q+rg/8D9qS/9NQ3MwuOiFKzUlj3CLZoWBSk9HB2R8tMV7dcNB1+YqB7G2+DI4qw//9/DVM3r5bSksoUrRrd6E5Vzw+RGrtM6sVWaOizXOsDWo0Xu9Cyze4W8= 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=LFTwB1nH; arc=none smtp.client-ip=209.85.214.182 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="LFTwB1nH" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2167141dfa1so83545255ad.1; Fri, 27 Dec 2024 01:57:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735293474; x=1735898274; 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=lPtNprQJYmyjGMRtUZxHdgQa89Grkgt3EAfh6HucewQ=; b=LFTwB1nH8WX1W6fhUXMgR13h9D1z07yiBnP1YTdoMEEeDr/EYGy673lAbdaD2mZRmN DI6Ljq0plN6PG9gybsGagYYtRoA0/aOo0+FEVruyymqEMiGl7fst9wobU6ZNP6CxH+6Z XWEcmTe7h0GZ4KtAdC+XIc+UxcjHyrv6Dt/hD1g0Pk0ReNmgxg4TH5SJg/AlHfvrSPZp 4CF3lyIse+QGcrXB3KdUZrSUkTLAQyJlk9pujqbDrJGQtClmjTv0lzsG0AjO2WHRDGj7 FmKIDfpTAL7/fEmZAHYsuA+nf7hEXhAuopHOFGvAvzFIWsRA1LF6w5KfdeCdvEvRIuM+ qZPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735293474; x=1735898274; 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=lPtNprQJYmyjGMRtUZxHdgQa89Grkgt3EAfh6HucewQ=; b=k2W+fFfl8gU+xMEusR09tBliYSAspJcsc3okNIfd342hXUoiV4cyaberYbRm6i5PpK IRS1qhLtUUbW1O6nE7Svnt4CTiFGE6J5gd6O4UGfsLl+BEbP7a4vuF9bSYDtH+EyJ2Ps 5mLHhYTtbDQMfU+aH4QSXzf2hcqLV4sr526ch68+RVBwPO2HwU9TXVjqiRIxP+JczcTu wFkS9l4ZK5xaRRbtAbVSEF+d40+cdc7PK4pp8P0yQZSJ1TuRDr/lkqEIWiFma+eOMvnF ZbXn2ICTNfkIChnyO8vCQ/k6QoTHbTyDtJ55xk8bIr5lHqLK0TsC6Olu8IL++/UpU7ck O2UQ== X-Forwarded-Encrypted: i=1; AJvYcCUOP9FHdbuL7XnW7TknHoUA0CqIsRgF/PnNaPu1ctgde7IvFUlVhIKDq1PsXb0l850rlxnveZ1cozP2njlGu8U=@vger.kernel.org, AJvYcCUx+DB+QAdVckpmJvuqVmu88N0SFSXvsblI66DX0e60m3rPGeHOTrfWEIpvvP3FcnutApIcP7ZS3+fOOA==@vger.kernel.org, AJvYcCV/HNbW+8vXEKWvK79sHamF0v2GMwmaRjoXbc0oG2w/XOr9rkaz1y7WNq7A1lhoFWUS6lXtepXyP5Y/iEw=@vger.kernel.org, AJvYcCVYd4uVOGj+H9PyheL3WdzmL9L5G4u6rok7AccXgLMKACT28VafPCZlgT5HgXwAdZWQ1j0k3pv4tjBv@vger.kernel.org, AJvYcCWKiGzKzFW946QXnhKsS8aUOeBnjEcMPlwtwP/mSRwUnfmG2YwIwkLCKxTrpxRrLhSTdBLMFUZ8@vger.kernel.org, AJvYcCXIHmNCOzJT+hUYOIVfGt2nihz/t77nxDo5es8fEx/npVF20PMJXpj9sOzFFUNQwtl5y8d5V+A9LT4=@vger.kernel.org, AJvYcCXvCYmVmJSRCnM55nSlCY+2A6uyEGGtdsuZnzGHkMI7neSkNxMFbF72SJnO0lljfRCGUJaNRXla3Wnp@vger.kernel.org X-Gm-Message-State: AOJu0YzjEr9YaHUB2gJf1CJqfUqGJyFMqOBCUSXL7jnC90CWKQZxtHdg HmzA6WsfFXbN/TTX0Zdl1tN4s5C9VeUymaSSR8bu3cDz2IUx6FEr X-Gm-Gg: ASbGnct0kEG29OP7QIGg7FdZw8gftG4dP07cRqRagXYT6DaHtvEinD+wxbM+jwEKVX2 ERFNmBnIe/9ZevYx0JlrK233QSA1oYOWMXqpViq07XXTlQOTKkOIoKWUioeIJwghjy0fjQ5j4TC xcUIy2NmRQWCmnBAxulnd3/nGElRM5Uj4aZcNmXSGzslAvCnHbbj3kPhi5wsQ3qSC9unB523CrJ segfeijsYTEkN6/uxsm9nVIqdcBYpfhbyl25o6/MsyB/edbJi+NbaoZKM+BkNOeJRU8h1WM8fp6 KkyAlVLeCdyN0YPjWLey7C7T X-Google-Smtp-Source: AGHT+IFHgnFONjSXV/1UCyWo6AfPdrGbVrFbNczdmD7ktCOZTHkWCP+RFxKwD4fkYLx9QJFnbeWJNg== X-Received: by 2002:a17:903:41c8:b0:215:8847:4377 with SMTP id d9443c01a7336-219e6cf8584mr415795515ad.15.1735293474310; Fri, 27 Dec 2024 01:57:54 -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 d9443c01a7336-219dc9f51a0sm131581135ad.187.2024.12.27.01.57.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Dec 2024 01:57:53 -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, Ming Yu Subject: [PATCH v4 2/7] gpio: Add Nuvoton NCT6694 GPIO support Date: Fri, 27 Dec 2024 17:57:22 +0800 Message-Id: <20241227095727.2401257-3-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241227095727.2401257-1-a0282524688@gmail.com> References: <20241227095727.2401257-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This driver supports GPIO and IRQ functionality for NCT6694 MFD device based on USB interface. Signed-off-by: Ming Yu --- MAINTAINERS | 1 + drivers/gpio/Kconfig | 12 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-nct6694.c | 462 ++++++++++++++++++++++++++++++++++++ 4 files changed, 476 insertions(+) create mode 100644 drivers/gpio/gpio-nct6694.c diff --git a/MAINTAINERS b/MAINTAINERS index acb270037642..18bf57f47792 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16726,6 +16726,7 @@ NUVOTON NCT6694 MFD DRIVER M: Ming Yu L: linux-kernel@vger.kernel.org S: Supported +F: drivers/gpio/gpio-nct6694.c F: drivers/mfd/nct6694.c F: include/linux/mfd/nct6694.h diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 93ee3aa092f8..808fbfdbff3b 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -1461,6 +1461,18 @@ config GPIO_MAX77650 GPIO driver for MAX77650/77651 PMIC from Maxim Semiconductor. These chips have a single pin that can be configured as GPIO. +config GPIO_NCT6694 + tristate "Nuvoton NCT6694 GPIO controller support" + depends on MFD_NCT6694 + select GENERIC_IRQ_CHIP + select GPIOLIB_IRQCHIP + help + This driver supports 8 GPIO pins per bank that can all be interrupt + sources. + + This driver can also be built as a module. If so, the module will be + called gpio-nct6694. + config GPIO_PALMAS bool "TI PALMAS series PMICs GPIO" depends on MFD_PALMAS diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index af3ba4d81b58..ad80a078b27b 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -123,6 +123,7 @@ obj-$(CONFIG_GPIO_MXC) += gpio-mxc.o obj-$(CONFIG_GPIO_MXS) += gpio-mxs.o obj-$(CONFIG_GPIO_NOMADIK) += gpio-nomadik.o obj-$(CONFIG_GPIO_NPCM_SGPIO) += gpio-npcm-sgpio.o +obj-$(CONFIG_GPIO_NCT6694) += gpio-nct6694.o obj-$(CONFIG_GPIO_OCTEON) += gpio-octeon.o obj-$(CONFIG_GPIO_OMAP) += gpio-omap.o obj-$(CONFIG_GPIO_PALMAS) += gpio-palmas.o diff --git a/drivers/gpio/gpio-nct6694.c b/drivers/gpio/gpio-nct6694.c new file mode 100644 index 000000000000..14c8acfcf5c3 --- /dev/null +++ b/drivers/gpio/gpio-nct6694.c @@ -0,0 +1,462 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Nuvoton NCT6694 GPIO controller driver based on USB interface. + * + * Copyright (C) 2024 Nuvoton Technology Corp. + */ + +#include +#include +#include +#include +#include +#include +#include + +/* + * USB command module type for NCT6694 GPIO controller. + * This defines the module type used for communication with the NCT6694 + * GPIO controller over the USB interface. + */ +#define NCT6694_GPIO_MOD 0xFF + +#define NCT6694_GPIO_VER 0x90 +#define NCT6694_GPIO_VALID 0x110 +#define NCT6694_GPI_DATA 0x120 +#define NCT6694_GPO_DIR 0x170 +#define NCT6694_GPO_TYPE 0x180 +#define NCT6694_GPO_DATA 0x190 + +#define NCT6694_GPI_STS 0x130 +#define NCT6694_GPI_CLR 0x140 +#define NCT6694_GPI_FALLING 0x150 +#define NCT6694_GPI_RISING 0x160 + +#define NCT6694_NR_GPIO 8 + +struct nct6694_gpio_data { + struct nct6694 *nct6694; + struct gpio_chip gpio; + struct mutex lock; + /* Protect irq operation */ + struct mutex irq_lock; + + unsigned char reg_val; + unsigned char irq_trig_falling; + unsigned char irq_trig_rising; + + /* Current gpio group */ + unsigned char group; +}; + +static int nct6694_get_direction(struct gpio_chip *gpio, unsigned int offset) +{ + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DIR + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + return !(BIT(offset) & data->reg_val); +} + +static int nct6694_direction_input(struct gpio_chip *gpio, unsigned int offset) +{ + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DIR + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + data->reg_val &= ~BIT(offset); + + return nct6694_write_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DIR + data->group, + sizeof(data->reg_val), + &data->reg_val); +} + +static int nct6694_direction_output(struct gpio_chip *gpio, + unsigned int offset, int val) +{ + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + int ret; + + guard(mutex)(&data->lock); + + /* Set direction to output */ + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DIR + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + data->reg_val |= BIT(offset); + ret = nct6694_write_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DIR + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + /* Then set output level */ + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DATA + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + if (val) + data->reg_val |= BIT(offset); + else + data->reg_val &= ~BIT(offset); + + return nct6694_write_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DATA + data->group, + sizeof(data->reg_val), + &data->reg_val); +} + +static int nct6694_get_value(struct gpio_chip *gpio, unsigned int offset) +{ + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DIR + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + if (BIT(offset) & data->reg_val) { + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DATA + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + return !!(BIT(offset) & data->reg_val); + } + + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_DATA + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + return !!(BIT(offset) & data->reg_val); +} + +static void nct6694_set_value(struct gpio_chip *gpio, unsigned int offset, + int val) +{ + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + + guard(mutex)(&data->lock); + + nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DATA + data->group, + sizeof(data->reg_val), + &data->reg_val); + + if (val) + data->reg_val |= BIT(offset); + else + data->reg_val &= ~BIT(offset); + + nct6694_write_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_DATA + data->group, + sizeof(data->reg_val), + &data->reg_val); +} + +static int nct6694_set_config(struct gpio_chip *gpio, unsigned int offset, + unsigned long config) +{ + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_TYPE + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + switch (pinconf_to_config_param(config)) { + case PIN_CONFIG_DRIVE_OPEN_DRAIN: + data->reg_val |= BIT(offset); + break; + case PIN_CONFIG_DRIVE_PUSH_PULL: + data->reg_val &= ~BIT(offset); + break; + default: + return -ENOTSUPP; + } + + return nct6694_write_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPO_TYPE + data->group, + sizeof(data->reg_val), + &data->reg_val); +} + +static int nct6694_init_valid_mask(struct gpio_chip *gpio, + unsigned long *valid_mask, + unsigned int ngpios) +{ + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPIO_VALID + data->group, + sizeof(data->reg_val), + &data->reg_val); + if (ret < 0) + return ret; + + *valid_mask = data->reg_val; + + return ret; +} + +static irqreturn_t nct6694_irq_handler(int irq, void *priv) +{ + struct nct6694_gpio_data *data = priv; + unsigned char status; + + guard(mutex)(&data->lock); + + nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_STS + data->group, + sizeof(data->reg_val), + &data->reg_val); + + status = data->reg_val; + + while (status) { + int bit = __ffs(status); + + data->reg_val = BIT(bit); + handle_nested_irq(irq_find_mapping(data->gpio.irq.domain, bit)); + status &= ~BIT(bit); + nct6694_write_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_CLR + data->group, + sizeof(data->reg_val), + &data->reg_val); + } + + return IRQ_HANDLED; +} + +static int nct6694_get_irq_trig(struct nct6694_gpio_data *data) +{ + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_FALLING + data->group, + sizeof(data->irq_trig_falling), + &data->irq_trig_falling); + if (ret) + return ret; + + return nct6694_read_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_RISING + data->group, + sizeof(data->irq_trig_rising), + &data->irq_trig_rising); +} + +static void nct6694_irq_mask(struct irq_data *d) +{ + struct gpio_chip *gpio = irq_data_get_irq_chip_data(d); + irq_hw_number_t hwirq = irqd_to_hwirq(d); + + gpiochip_disable_irq(gpio, hwirq); +} + +static void nct6694_irq_unmask(struct irq_data *d) +{ + struct gpio_chip *gpio = irq_data_get_irq_chip_data(d); + irq_hw_number_t hwirq = irqd_to_hwirq(d); + + gpiochip_enable_irq(gpio, hwirq); +} + +static int nct6694_irq_set_type(struct irq_data *d, unsigned int type) +{ + struct gpio_chip *gpio = irq_data_get_irq_chip_data(d); + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + irq_hw_number_t hwirq = irqd_to_hwirq(d); + + guard(mutex)(&data->lock); + + switch (type) { + case IRQ_TYPE_EDGE_RISING: + data->irq_trig_rising |= BIT(hwirq); + break; + + case IRQ_TYPE_EDGE_FALLING: + data->irq_trig_falling |= BIT(hwirq); + break; + + case IRQ_TYPE_EDGE_BOTH: + data->irq_trig_rising |= BIT(hwirq); + data->irq_trig_falling |= BIT(hwirq); + break; + + default: + return -ENOTSUPP; + } + + return 0; +} + +static void nct6694_irq_bus_lock(struct irq_data *d) +{ + struct gpio_chip *gpio = irq_data_get_irq_chip_data(d); + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + + mutex_lock(&data->irq_lock); +} + +static void nct6694_irq_bus_sync_unlock(struct irq_data *d) +{ + struct gpio_chip *gpio = irq_data_get_irq_chip_data(d); + struct nct6694_gpio_data *data = gpiochip_get_data(gpio); + + scoped_guard(mutex, &data->lock) { + nct6694_write_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_FALLING + data->group, + sizeof(data->irq_trig_falling), + &data->irq_trig_falling); + + nct6694_write_msg(data->nct6694, NCT6694_GPIO_MOD, + NCT6694_GPI_RISING + data->group, + sizeof(data->irq_trig_rising), + &data->irq_trig_rising); + } + + mutex_unlock(&data->irq_lock); +} + +static const struct irq_chip nct6694_irq_chip = { + .name = "nct6694-gpio", + .irq_mask = nct6694_irq_mask, + .irq_unmask = nct6694_irq_unmask, + .irq_set_type = nct6694_irq_set_type, + .irq_bus_lock = nct6694_irq_bus_lock, + .irq_bus_sync_unlock = nct6694_irq_bus_sync_unlock, + .flags = IRQCHIP_IMMUTABLE, + GPIOCHIP_IRQ_RESOURCE_HELPERS, +}; + +static int nct6694_gpio_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_gpio_data *data; + struct gpio_irq_chip *girq; + int ret, irq, i; + char **names; + + irq = irq_create_mapping(nct6694->domain, + NCT6694_IRQ_GPIO0 + cell->id); + if (!irq) + return -EINVAL; + + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + names = devm_kcalloc(dev, NCT6694_NR_GPIO, sizeof(char *), + GFP_KERNEL); + if (!names) + return -ENOMEM; + + for (i = 0; i < NCT6694_NR_GPIO; i++) { + names[i] = devm_kasprintf(dev, GFP_KERNEL, "GPIO%X%d", + cell->id, i); + if (!names[i]) + return -ENOMEM; + } + + data->nct6694 = nct6694; + data->group = cell->id; + + data->gpio.names = (const char * const*)names; + data->gpio.label = pdev->name; + data->gpio.direction_input = nct6694_direction_input; + data->gpio.get = nct6694_get_value; + data->gpio.direction_output = nct6694_direction_output; + data->gpio.set = nct6694_set_value; + data->gpio.get_direction = nct6694_get_direction; + data->gpio.set_config = nct6694_set_config; + data->gpio.init_valid_mask = nct6694_init_valid_mask; + data->gpio.base = -1; + data->gpio.can_sleep = false; + data->gpio.owner = THIS_MODULE; + data->gpio.ngpio = NCT6694_NR_GPIO; + + devm_mutex_init(dev, &data->lock); + devm_mutex_init(dev, &data->irq_lock); + + ret = nct6694_get_irq_trig(data); + if (ret) + return dev_err_probe(dev, ret, "Failed to get irq trigger type\n"); + + girq = &data->gpio.irq; + gpio_irq_chip_set_chip(girq, &nct6694_irq_chip); + girq->parent_handler = NULL; + girq->num_parents = 0; + girq->parents = NULL; + girq->default_type = IRQ_TYPE_NONE; + girq->handler = handle_level_irq; + girq->threaded = true; + + ret = devm_request_threaded_irq(dev, irq, NULL, nct6694_irq_handler, + IRQF_ONESHOT | IRQF_SHARED, + "nct6694-gpio", data); + if (ret) + return dev_err_probe(dev, ret, "Failed to request irq\n"); + + return devm_gpiochip_add_data(dev, &data->gpio, data); +} + +static struct platform_driver nct6694_gpio_driver = { + .driver = { + .name = "nct6694-gpio", + }, + .probe = nct6694_gpio_probe, +}; + +module_platform_driver(nct6694_gpio_driver); + +MODULE_DESCRIPTION("USB-GPIO controller driver for NCT6694"); +MODULE_AUTHOR("Ming Yu "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:nct6694-gpio"); From patchwork Fri Dec 27 09:57:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 13921658 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 1B3C2156C79; Fri, 27 Dec 2024 09:57:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293480; cv=none; b=idt9gZNXgC+VDdc5haRlpCeZnqODJXfNCqTCYts/AYaYDcRkRJkL+gPp8Wu5f83LYsLvwGXk3jIcyu18C8sfacKZFfbwtXDT/mJ4DYpSyc4lPIAFrkiG1wB6S72hr8Rs+eBLGSeGbG0oW6EGGAZKkg3QMA6/oQvZbCDqq5FlS9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293480; c=relaxed/simple; bh=Oz+8IjTT6yvTIlGG+orS45ROIUXnrAoDAXRuuB9/ILM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Wevvcb2RDaFuwUdNpea5j0FLuztq9sZBp26lEHBJDrf55DTp8TskK1CA+2q7hltIXpPywG8Eb964WdjwgVHhzxroEsZ+aB7l7yVvv2XKTaQO6Qw+OXxHwBVHtyR48nmZUiQ3zQp9LMXoACodNoP6WM310RwGcfQW6pbRR0w/h18= 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=bAArV/EY; arc=none smtp.client-ip=209.85.214.177 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="bAArV/EY" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-216634dd574so58237235ad.2; Fri, 27 Dec 2024 01:57:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735293478; x=1735898278; 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=K1uSCfkmb77gn2td6kC3hJU0Smi+Gx6i6fZ+I8uFJY0=; b=bAArV/EYJEQ23DhDWPAxZ7f9A0vLMwOWxrnPgmHz16cWuNUKsHoM1UK94sHvj/GRs7 7UUWJmRpA5/Obz8XkzNcE9fCAbj/pP+ey4jEHW+Nidkj9gRmWL7h+n0jcaS/wGNSXRi0 /5IOnmMceDobpOrfgq1lRjPuQJu5MSLbgubUoMJ1xsMeX1CK8qxdIR0RPrelHgfVCwRT Nj+zEPR1eivncd/G9KAXFWtnAKx6+A03KHPzm+qjVq1me8evkC0IVLUypJR582agkizy eEFp2kDoZ777RuHex+Llzhr9CSqBUTt8Edb16NlGDJMFUbPYjJvwTDW+3ie/q2JxEiN+ 9ZgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735293478; x=1735898278; 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=K1uSCfkmb77gn2td6kC3hJU0Smi+Gx6i6fZ+I8uFJY0=; b=eCdpNcdRRN4TSR5U3dTfp22ytMLgix0YrKDDd5ldPm3qSsQ2Yg39nHCokBVrhF2ZK8 mLdlSVRWIhdqz1Uv5Xjn7ZZ7Q02RciEvbZjT0HwkE60nQSiWLMxsqg23wTA3xfwxIcIq PAZ8vgxIQU1MZxH6Eo2uwEayXoR/lnUGS9TaHPjUy9Tgi8FBWVB+FgXH9DuejqoPiK7U jwtYBM826hfgl5c3uow5+Onfa6cIKFP+QPRqg//mPFCUthE54e9QnAYYM/r1NJ9ZeHLP Iq0xFn9eE96/jl6jAbnUvewcLABG3TkkYvdPZcV8HfOZ+RnlM9IkIRvqzIVDPGE2SxtZ 4fOg== X-Forwarded-Encrypted: i=1; AJvYcCV7qJ1RMYfYElU3ePCLqoyQ/tcmrPV/Qv0e50ZtbimDGWpvHCH591Rkk1CzJ/XNQ/uaPxuYUZNVosOaeNve3S4=@vger.kernel.org, AJvYcCVMGhi2423oPVy8iGCZBHmSuJYIC8B3NiHdk8XwFPgT35B5aS6Uwke0tVvxq0cpyg3ixWmT6zmtzwQFJnw=@vger.kernel.org, AJvYcCVTrWJXH4Rgw8I/P4Pb4200/p6AhyLhLTW+oT/zyf7K5PrAc3GTpDv7E6NxMNGYcptfAAestrbwRaJCgQ==@vger.kernel.org, AJvYcCWM8cQWny4WS6PSIiCHR4WK0eSz+DP/ptCTf8mKkDshe4/mWW/PZSLuEQmc8RAF2ivCBQhenEYGygG8@vger.kernel.org, AJvYcCWsytIOuYQlD2xV/WHjeUtDsi/BRN6F3/PVR2Abc+eEp0GZ38kmauJwQ2+hNfYXfB8Gqfc3Apjn@vger.kernel.org, AJvYcCWzDLjq3ORw/3Msu6uMb4uZJidjvH/Qwe36PzqmuZEGzdnEVh9OcxVTXSqfoEMk2Xr1i0LWDB4xdtsy@vger.kernel.org, AJvYcCX/mCkCvEFnv85jTm0OpfXd40SFJyafHrCGImObnnlcTwa14UsFpV/t8aYgCtLNQwIG6dehIWdluwA=@vger.kernel.org X-Gm-Message-State: AOJu0YxJDFGSFBaAucPvHbpYa3idkBgmyVnhbLQdFy81MpcNB5PTkXJe tEP7FAjVvD5+4z7ILuGDT8k5L60CEE2qSqIH+8tDZYzst8RxbrCa X-Gm-Gg: ASbGncs40cwyqdxAccy5ImVpG0+wjVHaBsoY4ksAhLylOcLYADLT3jvicXAfmq2l/Lj QdWKrT7e/inpo7vVNYhfIAr/+ULkSUPyEUOLxdrDsMu0M0fuWy/Ad4mRGeTdswkSCcAbw7X0pKO yHhYFlP/76zAyXEYqwHqg74HKYD1EDJsqdrSCHSU/JewFnCRJGRd9ICZYZvu49+CKoCZqn0nHwi aro4TkGtt43b/YAZ9z2UzioDF4hCtktLioJV4RtjAZ6Jd5BgwxkTD1CxIgF4yv+J+j8BlZ2I/EB OYaNXZeeR45ASv2EZiI6IyXi X-Google-Smtp-Source: AGHT+IG+4Flc0GtkcQwfWGzUoSkAcfpcMhb8X0TDEfx6Fer8z3QjWg3Q8YZMxvofvXGTphaOOI87xw== X-Received: by 2002:a17:902:d592:b0:215:97a3:5ec5 with SMTP id d9443c01a7336-219e6e9fdf2mr406764105ad.22.1735293478432; Fri, 27 Dec 2024 01:57:58 -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 d9443c01a7336-219dc9f51a0sm131581135ad.187.2024.12.27.01.57.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Dec 2024 01:57:58 -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, Ming Yu Subject: [PATCH v4 3/7] i2c: Add Nuvoton NCT6694 I2C support Date: Fri, 27 Dec 2024 17:57:23 +0800 Message-Id: <20241227095727.2401257-4-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241227095727.2401257-1-a0282524688@gmail.com> References: <20241227095727.2401257-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-hwmon@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 | 156 +++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 drivers/i2c/busses/i2c-nct6694.c diff --git a/MAINTAINERS b/MAINTAINERS index 18bf57f47792..5d63542233c4 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16727,6 +16727,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..9add97c407d8 --- /dev/null +++ b/drivers/i2c/busses/i2c-nct6694.c @@ -0,0 +1,156 @@ +// 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 0x0000 /* SEL|CMD */ + +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; + 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, NCT6694_I2C_MOD, + NCT6694_I2C_DELIVER, + sizeof(*deliver), + 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, NCT6694_I2C_MOD, + NCT6694_I2C_DELIVER, + sizeof(*deliver), + 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 Fri Dec 27 09:57:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 13921659 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 54C31176FB6; Fri, 27 Dec 2024 09:58:03 +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=1735293485; cv=none; b=FjywAzg1I9HClkgXs1qx6Ni84Dq0EGQqCtlMzg5oBHWPKkAubAnONwIRB6s+AxTf51xUr7Jyu7Fp6BUAGVctsSith04tzBhWYsnLfnI1R8pQFA9L5rdAhW3EGf8Oa4z5E+mtxLj8Nc69nM63fis8dDd+PB83Ko9PMEo5K1IY3jw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293485; c=relaxed/simple; bh=BkFBmZ1ZdukncilBcFxOKk56QMLh7qc5fabfJZJyKu4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lXCnvL35neAe4RHqFRYs2z3TSYCTjn3Q6hCyNUS2mJ4HREVp9aBjFGmR3VQdx+wEJsaBvFu9z4QviB1Y/PaQy1OvraAfUtfgsvgtwHbiFSItsNpWKKGGXqNu9gAVVxuz2JksTtt76wXwTdRcvrPziG6L4a/p3nq46vyyVT29R30= 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=f1QKlsiK; 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="f1QKlsiK" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-216395e151bso62311645ad.0; Fri, 27 Dec 2024 01:58:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735293483; x=1735898283; 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=FtRmfQanZGFRZ6imS5Y3svETArcOiW7CuXG6aN1fbi4=; b=f1QKlsiK3JGyHWNbU82+gIcH5PEM2/dFRn5rPE1LZ7WZQTfW79yVqYeYmt+IhS6RfI PELUkPmX3EavCl3Xoltc58FUEn8avn7iMNkFbwmrt5/oGgTQTjX++Z0T0t6qaZnPQ+ky 3VaSMXNGESSp0P4lthk0Kq1WBzgbVGIJhTfEIpK8PSTFwUp9DgESTcZwgAgrgUaC/TLM Y6bGGX8uTCsbI7wDCNsU7eNmFh0CQr3zg1p2rMB8xO4y2ghlWZnRtdgpLzJi4+CujX+D l7MnlPBWqXr/M89BYATjolN8nbb5fxL791DXQfVFMaFaqQ9bkSTKYf8JssO99nm7Rq1Z f47Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735293483; x=1735898283; 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=FtRmfQanZGFRZ6imS5Y3svETArcOiW7CuXG6aN1fbi4=; b=hdbmS9aMu7I7huUI/oLpGUgwUygrj+LUMowsTlMAVJ30Xb4yzP3GtAw45DsBjqsoh1 LJES3rAv075mKm+0MXFvW0SNSAQCmHwHuyrR3LpXSVsGBEu3MifdKwrHFH7mm0cPW15u /jizBA2/++P3acoDgkga+oJh0j96xifsLfUD9pJHPbFuELxh5PzOy3jbHbxdeHptWw14 z8uoaD1aCajeACNl4+H0Q4PUM9BfUrehmpCyI4af0T3kUidsITqNaDVyQM5J2jcACxXL KSZ2YVDlfo77nFMWXM+UgraQy3O3C+RVInrhGwXjT3G2BYMirOLMqGHAdpm8Y2q++qzJ n5rw== X-Forwarded-Encrypted: i=1; AJvYcCUGZAN4NTyt0jc8T9J8EZaCh/gXaThahW5OMwadONnHnnQuc1c90ElvkQlRLqaeF+/lKyZxY8zjI9cEgw==@vger.kernel.org, AJvYcCUGw8OT0giBbINnG5nyI0M9DsiiI1SEsw3XegnzEoCAyXFsA5xl3EIwHN+ldVth0kDssMdLbctkbBTnneo=@vger.kernel.org, AJvYcCUnjivf8xJSIhfh32aW6y5GcOasAy6ZeWhjFwFhjrITtKcCr1233gUO6IWTQ+0pM5aBvlaJ8h5v@vger.kernel.org, AJvYcCV5qNKw7zx2ym8uU+LY1b78k34j1MZrFnPx2r5z4gyTSSCs9GHJDgOZfUrFmLjqMaoLAkaHbjWh/b3nn1WyWOk=@vger.kernel.org, AJvYcCWHdPg2NL6/fYBnIJhf8RL8/E5oAvfNl7RxF3gPGUMTQCGlVeA/Rs2qV9hX4oUZJ64KzzGvqJXP9N6V@vger.kernel.org, AJvYcCWkxTxak4xPvSr5HUuEQ3xflcTGM9sssK24vMGu1tQ9F69ORKzydjWYIka85YEBlrJUnM1GRbE1Riqd@vger.kernel.org, AJvYcCX2Pin6C6Z1hZ98NR791PcF9FWalWoPZmDdLopXcNalHk2X9819WMQ82T5NNaIPJ2/3pQ3s7s1amwo=@vger.kernel.org X-Gm-Message-State: AOJu0YzEmQSWMQjmP83f9kQCb+UPZaWiX+VRXdG++g1wzD/DlC1quvMx mU/LdGE29DcxdOKiX+pPHwxqXterBjNVmN743JDMONIT3Vwc6Z3d X-Gm-Gg: ASbGnctRsGj5h3x52PQf19BI1VxUqqxPkGuQg/aYPgcivS7S5HObTDqSRBwrVnlHJ8R o86dVTif9jQ89pOdhSfvkjqnBVMqGAyXY6Fy489NpM/KcRaZaE1Dzvsa/hFkfWedI8Dan21KiE0 ixZqx4KGi+2Ga6BLYHYFkdLP9Teh1pNv+BPKFU8qlMOgojY/vaqU+x53x+VMxSs7LhY4iGsv8vQ WdsXRIL3JiyQln+9i6dGfiUQsNPxrwNMOfIFQHBJSw5TgryTR4b8xA0VJX9NglU1Qznmq9FqD42 w3bWEdgZNJXLu3UbWgNl5nXP X-Google-Smtp-Source: AGHT+IGwftSGYnpHhQ11IxRdxlXYZg9wFr8x7mNt9TY90nzZKOnVxiTHQ3MWICVvDdjY32hJqrgQPQ== X-Received: by 2002:a17:902:f64d:b0:216:6db1:1a6e with SMTP id d9443c01a7336-219da5cdf93mr484166175ad.10.1735293482575; Fri, 27 Dec 2024 01:58:02 -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 d9443c01a7336-219dc9f51a0sm131581135ad.187.2024.12.27.01.57.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Dec 2024 01:58:02 -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, Ming Yu Subject: [PATCH v4 4/7] can: Add Nuvoton NCT6694 CAN support Date: Fri, 27 Dec 2024 17:57:24 +0800 Message-Id: <20241227095727.2401257-5-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241227095727.2401257-1-a0282524688@gmail.com> References: <20241227095727.2401257-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This driver supports Socket CANfd functionality for NCT6694 MFD device based on USB interface. Signed-off-by: Ming Yu --- MAINTAINERS | 1 + drivers/net/can/Kconfig | 10 + drivers/net/can/Makefile | 1 + drivers/net/can/nct6694_canfd.c | 826 ++++++++++++++++++++++++++++++++ 4 files changed, 838 insertions(+) create mode 100644 drivers/net/can/nct6694_canfd.c diff --git a/MAINTAINERS b/MAINTAINERS index 5d63542233c4..b961c4827648 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16729,6 +16729,7 @@ S: Supported 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: include/linux/mfd/nct6694.h NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER diff --git a/drivers/net/can/Kconfig b/drivers/net/can/Kconfig index cf989bea9aa3..130e98ec28a5 100644 --- a/drivers/net/can/Kconfig +++ b/drivers/net/can/Kconfig @@ -200,6 +200,16 @@ config CAN_SUN4I To compile this driver as a module, choose M here: the module will be called sun4i_can. +config CAN_NCT6694 + tristate "Nuvoton NCT6694 Socket CANfd support" + depends on MFD_NCT6694 + help + If you say yes to this option, support will be included for Nuvoton + NCT6694, a USB device to socket CANfd controller. + + This driver can also be built as a module. If so, the module will + be called nct6694_canfd. + config CAN_TI_HECC depends on ARM tristate "TI High End CAN Controller" diff --git a/drivers/net/can/Makefile b/drivers/net/can/Makefile index a71db2cfe990..4a6b5b9d6c2b 100644 --- a/drivers/net/can/Makefile +++ b/drivers/net/can/Makefile @@ -28,6 +28,7 @@ obj-$(CONFIG_CAN_JANZ_ICAN3) += janz-ican3.o obj-$(CONFIG_CAN_KVASER_PCIEFD) += kvaser_pciefd.o obj-$(CONFIG_CAN_MSCAN) += mscan/ obj-$(CONFIG_CAN_M_CAN) += m_can/ +obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o obj-$(CONFIG_CAN_PEAK_PCIEFD) += peak_canfd/ obj-$(CONFIG_CAN_SJA1000) += sja1000/ obj-$(CONFIG_CAN_SUN4I) += sun4i_can.o diff --git a/drivers/net/can/nct6694_canfd.c b/drivers/net/can/nct6694_canfd.c new file mode 100644 index 000000000000..94cfff288c14 --- /dev/null +++ b/drivers/net/can/nct6694_canfd.c @@ -0,0 +1,826 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Nuvoton NCT6694 Socket CANfd driver based on USB interface. + * + * Copyright (C) 2024 Nuvoton Technology Corp. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DRVNAME "nct6694-can" + +/* + * USB command module type for NCT6694 CANfd controller. + * This defines the module type used for communication with the NCT6694 + * CANfd controller over the USB interface. + */ +#define NCT6694_CAN_MOD 0x05 + +/* Command 00h - CAN Setting and Initialization */ +#define NCT6694_CAN_SETTING(idx) (idx ? 0x0100 : 0x0000) /* CMD|SEL */ +#define NCT6694_CAN_SETTING_CTRL1_MON BIT(0) +#define NCT6694_CAN_SETTING_CTRL1_NISO BIT(1) +#define NCT6694_CAN_SETTING_CTRL1_LBCK BIT(2) + +/* Command 01h - CAN Information */ +#define NCT6694_CAN_INFORMATION 0x0001 /* CMD|SEL */ + +/* Command 02h - CAN Event */ +#define NCT6694_CAN_EVENT(idx, mask) \ + ((((idx ? 0x80 : 0x00) | \ + ((mask) & 0xFF)) << 8) | 0x02) /* CMD|SEL */ +#define NCT6694_CAN_EVENT_ERR BIT(0) +#define NCT6694_CAN_EVENT_STATUS BIT(1) +#define NCT6694_CAN_EVENT_TX_EVT BIT(2) +#define NCT6694_CAN_EVENT_RX_EVT BIT(3) +#define NCT6694_CAN_EVENT_REC BIT(4) +#define NCT6694_CAN_EVENT_TEC BIT(5) +#define NCT6694_CAN_EVENT_MASK GENMASK(3, 0) +#define NCT6694_CAN_EVT_TX_FIFO_EMPTY BIT(7) /* Read-clear */ +#define NCT6694_CAN_EVT_RX_DATA_LOST BIT(5) /* Read-clear */ +#define NCT6694_CAN_EVT_RX_HALF_FULL BIT(6) /* Read-clear */ +#define NCT6694_CAN_EVT_RX_DATA_IN BIT(7) /* Read-clear*/ + +/* Command 10h - CAN Deliver */ +#define NCT6694_CAN_DELIVER(buf_cnt) \ + ((((buf_cnt) & 0xFF) << 8) | 0x10) /* CMD|SEL */ + +/* Command 11h - CAN Receive */ +#define NCT6694_CAN_RECEIVE(idx, buf_cnt) \ + ((((idx ? 0x80 : 0x00) | \ + ((buf_cnt) & 0xFF)) << 8) | 0x11) /* CMD|SEL */ + +#define NCT6694_CAN_FRAME_TAG_CAN0 0xC0 +#define NCT6694_CAN_FRAME_TAG_CAN1 0xC1 +#define NCT6694_CAN_FRAME_FLAG_EFF BIT(0) +#define NCT6694_CAN_FRAME_FLAG_RTR BIT(1) +#define NCT6694_CAN_FRAME_FLAG_FD BIT(2) +#define NCT6694_CAN_FRAME_FLAG_BRS BIT(3) +#define NCT6694_CAN_FRAME_FLAG_ERR BIT(4) + +#define NCT6694_NAPI_WEIGHT 32 + +enum nct6694_event_err { + NCT6694_CAN_EVT_ERR_NO_ERROR, + NCT6694_CAN_EVT_ERR_CRC_ERROR, + NCT6694_CAN_EVT_ERR_STUFF_ERROR, + NCT6694_CAN_EVT_ERR_ACK_ERROR, + NCT6694_CAN_EVT_ERR_FORM_ERROR, + NCT6694_CAN_EVT_ERR_BIT_ERROR, + NCT6694_CAN_EVT_ERR_TIMEOUT_ERROR, + NCT6694_CAN_EVT_ERR_UNKNOWN_ERROR, +}; + +enum nct6694_event_status { + NCT6694_CAN_EVT_STS_ERROR_ACTIVE, + NCT6694_CAN_EVT_STS_ERROR_PASSIVE, + NCT6694_CAN_EVT_STS_BUS_OFF, + NCT6694_CAN_EVT_STS_WARNING, +}; + +struct __packed nct6694_can_setting { + __le32 nbr; + __le32 dbr; + u8 active; + u8 reserved[3]; + __le16 ctrl1; + __le16 ctrl2; + __le32 nbtp; + __le32 dbtp; +}; + +struct __packed nct6694_can_information { + u8 tx_fifo_cnt; + u8 rx_fifo_cnt; + u8 reserved[2]; + __le32 can_clk; +}; + +struct __packed nct6694_can_event_channel { + u8 err; + u8 status; + u8 tx_evt; + u8 rx_evt; + u8 rec; + u8 tec; + u8 reserved[2]; +}; + +struct __packed nct6694_can_event { + struct nct6694_can_event_channel evt_ch[2]; +}; + +struct __packed nct6694_can_frame { + u8 tag; + u8 flag; + u8 reserved; + u8 length; + __le32 id; + u8 data[64]; +}; + +union nct6694_can_tx { + struct nct6694_can_frame frame; + struct nct6694_can_setting setting; +}; + +union nct6694_can_rx { + struct nct6694_can_event event; + struct nct6694_can_frame frame; + struct nct6694_can_information info; +}; + +struct nct6694_can_priv { + struct can_priv can; /* must be the first member */ + struct can_rx_offload offload; + struct net_device *ndev; + struct nct6694 *nct6694; + struct mutex lock; + struct sk_buff *tx_skb; + struct workqueue_struct *wq; + struct work_struct tx_work; + union nct6694_can_tx *tx; + union nct6694_can_rx *rx; + unsigned char can_idx; + bool tx_busy; +}; + +static inline struct nct6694_can_priv *rx_offload_to_priv(struct can_rx_offload *offload) +{ + return container_of(offload, struct nct6694_can_priv, offload); +} + +static const struct can_bittiming_const nct6694_can_bittiming_nominal_const = { + .name = DRVNAME, + .tseg1_min = 2, + .tseg1_max = 256, + .tseg2_min = 2, + .tseg2_max = 128, + .sjw_max = 128, + .brp_min = 1, + .brp_max = 511, + .brp_inc = 1, +}; + +static const struct can_bittiming_const nct6694_can_bittiming_data_const = { + .name = DRVNAME, + .tseg1_min = 1, + .tseg1_max = 32, + .tseg2_min = 1, + .tseg2_max = 16, + .sjw_max = 16, + .brp_min = 1, + .brp_max = 31, + .brp_inc = 1, +}; + +static void nct6694_can_rx_offload(struct can_rx_offload *offload, + struct sk_buff *skb) +{ + struct nct6694_can_priv *priv = rx_offload_to_priv(offload); + int ret; + + ret = can_rx_offload_queue_tail(offload, skb); + if (ret) + priv->ndev->stats.rx_fifo_errors++; +} + +static void nct6694_can_handle_lost_msg(struct net_device *ndev) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + struct net_device_stats *stats = &ndev->stats; + struct can_frame *cf; + struct sk_buff *skb; + + netdev_err(ndev, "RX FIFO overflow, message(s) lost.\n"); + + stats->rx_errors++; + stats->rx_over_errors++; + + skb = alloc_can_err_skb(ndev, &cf); + if (!skb) + return; + + cf->can_id |= CAN_ERR_CRTL; + cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; + + nct6694_can_rx_offload(&priv->offload, skb); +} + +static void nct6694_can_rx(struct net_device *ndev, u8 rx_evt) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + struct nct6694_can_frame *frame = &priv->rx->frame; + struct canfd_frame *cfd; + struct can_frame *cf; + struct sk_buff *skb; + int ret; + + ret = nct6694_read_msg(priv->nct6694, NCT6694_CAN_MOD, + NCT6694_CAN_RECEIVE(priv->can_idx, 1), + sizeof(*frame), frame); + if (ret) + return; + + if (frame->flag & NCT6694_CAN_FRAME_FLAG_FD) { + skb = alloc_canfd_skb(priv->ndev, &cfd); + if (!skb) + return; + + cfd->can_id = le32_to_cpu(frame->id); + cfd->len = frame->length; + if (frame->flag & NCT6694_CAN_FRAME_FLAG_EFF) + cfd->can_id |= CAN_EFF_FLAG; + if (frame->flag & NCT6694_CAN_FRAME_FLAG_BRS) + cfd->flags |= CANFD_BRS; + if (frame->flag & NCT6694_CAN_FRAME_FLAG_ERR) + cfd->flags |= CANFD_ESI; + + memcpy(cfd->data, frame->data, cfd->len); + } else { + skb = alloc_can_skb(priv->ndev, &cf); + if (!skb) + return; + + cf->can_id = le32_to_cpu(frame->id); + cf->len = frame->length; + if (frame->flag & NCT6694_CAN_FRAME_FLAG_EFF) + cf->can_id |= CAN_EFF_FLAG; + if (frame->flag & NCT6694_CAN_FRAME_FLAG_RTR) + cf->can_id |= CAN_RTR_FLAG; + + memcpy(cf->data, frame->data, cf->len); + } + + nct6694_can_rx_offload(&priv->offload, skb); +} + +static void nct6694_can_clean(struct net_device *ndev) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + + if (priv->tx_skb || priv->tx_busy) + ndev->stats.tx_errors++; + dev_kfree_skb(priv->tx_skb); + if (priv->tx_busy) + can_free_echo_skb(priv->ndev, 0, NULL); + priv->tx_skb = NULL; + priv->tx_busy = false; +} + +static int nct6694_can_get_berr_counter(const struct net_device *ndev, + struct can_berr_counter *bec) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + struct nct6694_can_event *evt = &priv->rx->event; + u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC; + int ret; + + guard(mutex)(&priv->lock); + + ret = nct6694_read_msg(priv->nct6694, NCT6694_CAN_MOD, + NCT6694_CAN_EVENT(priv->can_idx, mask), + sizeof(*evt), + evt); + if (ret < 0) + return ret; + + bec->rxerr = evt->evt_ch[priv->can_idx].rec; + bec->txerr = evt->evt_ch[priv->can_idx].tec; + + return 0; +} + +static void nct6694_can_handle_state_change(struct net_device *ndev, + u8 status) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + enum can_state new_state = priv->can.state; + enum can_state rx_state, tx_state; + struct can_berr_counter bec; + struct can_frame *cf; + struct sk_buff *skb; + + nct6694_can_get_berr_counter(ndev, &bec); + can_state_get_by_berr_counter(ndev, &bec, &tx_state, &rx_state); + + if (status & NCT6694_CAN_EVT_STS_BUS_OFF) + new_state = CAN_STATE_BUS_OFF; + else if (status & NCT6694_CAN_EVT_STS_ERROR_PASSIVE) + new_state = CAN_STATE_ERROR_PASSIVE; + else if (status & NCT6694_CAN_EVT_STS_WARNING) + new_state = CAN_STATE_ERROR_WARNING; + + /* state hasn't changed */ + if (new_state == priv->can.state) + return; + + skb = alloc_can_err_skb(ndev, &cf); + + tx_state = bec.txerr >= bec.rxerr ? new_state : 0; + rx_state = bec.txerr <= bec.rxerr ? new_state : 0; + can_change_state(ndev, cf, tx_state, rx_state); + + if (new_state == CAN_STATE_BUS_OFF) { + can_bus_off(ndev); + } else if (skb) { + cf->can_id |= CAN_ERR_CNT; + cf->data[6] = bec.txerr; + cf->data[7] = bec.rxerr; + } + + nct6694_can_rx_offload(&priv->offload, skb); +} + +static void nct6694_handle_bus_err(struct net_device *ndev, u8 bus_err) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + struct can_frame *cf; + struct sk_buff *skb; + + if (bus_err == NCT6694_CAN_EVT_ERR_NO_ERROR) + return; + + priv->can.can_stats.bus_error++; + + skb = alloc_can_err_skb(ndev, &cf); + if (skb) + cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; + + switch (bus_err) { + case NCT6694_CAN_EVT_ERR_CRC_ERROR: + netdev_dbg(ndev, "CRC error\n"); + ndev->stats.rx_errors++; + if (skb) + cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ; + break; + + case NCT6694_CAN_EVT_ERR_STUFF_ERROR: + netdev_dbg(ndev, "Stuff error\n"); + ndev->stats.rx_errors++; + if (skb) + cf->data[2] |= CAN_ERR_PROT_STUFF; + break; + + case NCT6694_CAN_EVT_ERR_ACK_ERROR: + netdev_dbg(ndev, "Ack error\n"); + ndev->stats.tx_errors++; + if (skb) { + cf->can_id |= CAN_ERR_ACK; + cf->data[2] |= CAN_ERR_PROT_TX; + } + break; + + case NCT6694_CAN_EVT_ERR_FORM_ERROR: + netdev_dbg(ndev, "Form error\n"); + ndev->stats.rx_errors++; + if (skb) + cf->data[2] |= CAN_ERR_PROT_FORM; + break; + + case NCT6694_CAN_EVT_ERR_BIT_ERROR: + netdev_dbg(ndev, "Bit error\n"); + ndev->stats.tx_errors++; + if (skb) + cf->data[2] |= CAN_ERR_PROT_TX | CAN_ERR_PROT_BIT; + break; + + default: + break; + } +} + +static void nct6694_can_tx_irq(struct net_device *ndev) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + struct net_device_stats *stats = &ndev->stats; + + guard(mutex)(&priv->lock); + stats->tx_bytes += can_get_echo_skb(ndev, 0, NULL); + stats->tx_packets++; + priv->tx_busy = false; + netif_wake_queue(ndev); +} + +static irqreturn_t nct6694_can_irq(int irq, void *data) +{ + struct net_device *ndev = data; + struct nct6694_can_priv *priv = netdev_priv(ndev); + struct nct6694_can_event *evt = &priv->rx->event; + u8 tx_evt, rx_evt, bus_err, can_status; + u8 mask_sts = NCT6694_CAN_EVENT_MASK; + irqreturn_t handled = IRQ_NONE; + int can_idx = priv->can_idx; + int ret; + + scoped_guard(mutex, &priv->lock) { + ret = nct6694_read_msg(priv->nct6694, NCT6694_CAN_MOD, + NCT6694_CAN_EVENT(can_idx, mask_sts), + sizeof(*evt), evt); + if (ret < 0) + return handled; + + tx_evt = evt->evt_ch[can_idx].tx_evt; + rx_evt = evt->evt_ch[can_idx].rx_evt; + bus_err = evt->evt_ch[can_idx].err; + can_status = evt->evt_ch[can_idx].status; + } + + if (rx_evt & NCT6694_CAN_EVT_RX_DATA_IN) { + nct6694_can_rx(ndev, rx_evt); + handled = IRQ_HANDLED; + } + + if (rx_evt & NCT6694_CAN_EVT_RX_DATA_LOST) { + nct6694_can_handle_lost_msg(ndev); + handled = IRQ_HANDLED; + } + + if (can_status) { + nct6694_can_handle_state_change(ndev, can_status); + handled = IRQ_HANDLED; + } + + if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) { + nct6694_handle_bus_err(ndev, bus_err); + handled = IRQ_HANDLED; + } + + if (handled) + can_rx_offload_threaded_irq_finish(&priv->offload); + + if (tx_evt & NCT6694_CAN_EVT_TX_FIFO_EMPTY) + nct6694_can_tx_irq(ndev); + + return handled; +} + +static void nct6694_can_tx(struct net_device *ndev) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + struct nct6694_can_frame *frame = &priv->tx->frame; + struct net_device_stats *stats = &ndev->stats; + struct sk_buff *skb = priv->tx_skb; + struct canfd_frame *cfd; + struct can_frame *cf; + u32 txid; + int err; + + memset(frame, 0, sizeof(*frame)); + + if (priv->can_idx == 0) + frame->tag = NCT6694_CAN_FRAME_TAG_CAN0; + else + frame->tag = NCT6694_CAN_FRAME_TAG_CAN1; + + if (can_is_canfd_skb(skb)) { + cfd = (struct canfd_frame *)priv->tx_skb->data; + + if (cfd->flags & CANFD_BRS) + frame->flag |= NCT6694_CAN_FRAME_FLAG_BRS; + + if (cfd->can_id & CAN_EFF_FLAG) { + txid = cfd->can_id & CAN_EFF_MASK; + frame->flag |= NCT6694_CAN_FRAME_FLAG_EFF; + } else { + txid = cfd->can_id & CAN_SFF_MASK; + } + frame->flag |= NCT6694_CAN_FRAME_FLAG_FD; + frame->id = cpu_to_le32(txid); + frame->length = cfd->len; + + memcpy(frame->data, cfd->data, cfd->len); + } else { + cf = (struct can_frame *)priv->tx_skb->data; + + if (cf->can_id & CAN_RTR_FLAG) + frame->flag |= NCT6694_CAN_FRAME_FLAG_RTR; + + if (cf->can_id & CAN_EFF_FLAG) { + txid = cf->can_id & CAN_EFF_MASK; + frame->flag |= NCT6694_CAN_FRAME_FLAG_EFF; + } else { + txid = cf->can_id & CAN_SFF_MASK; + } + frame->id = cpu_to_le32(txid); + frame->length = cf->len; + + memcpy(frame->data, cf->data, cf->len); + } + + err = nct6694_write_msg(priv->nct6694, NCT6694_CAN_MOD, + NCT6694_CAN_DELIVER(1), + sizeof(*frame), + frame); + if (err) { + netdev_err(ndev, "%s: Tx FIFO full!\n", __func__); + can_free_echo_skb(ndev, 0, NULL); + stats->tx_dropped++; + stats->tx_errors++; + netif_wake_queue(ndev); + } +} + +static void nct6694_can_tx_work(struct work_struct *work) +{ + struct nct6694_can_priv *priv = container_of(work, + struct nct6694_can_priv, + tx_work); + struct net_device *ndev = priv->ndev; + + guard(mutex)(&priv->lock); + + if (priv->tx_skb) { + if (priv->can.state == CAN_STATE_BUS_OFF) { + nct6694_can_clean(ndev); + } else { + nct6694_can_tx(ndev); + can_put_echo_skb(priv->tx_skb, ndev, 0, 0); + priv->tx_busy = true; + priv->tx_skb = NULL; + } + } +} + +static netdev_tx_t nct6694_can_start_xmit(struct sk_buff *skb, + struct net_device *ndev) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + + if (can_dev_dropped_skb(ndev, skb)) + return NETDEV_TX_OK; + + if (priv->tx_skb || priv->tx_busy) { + netdev_err(ndev, "hard_xmit called while tx busy\n"); + return NETDEV_TX_BUSY; + } + + netif_stop_queue(ndev); + priv->tx_skb = skb; + queue_work(priv->wq, &priv->tx_work); + + return NETDEV_TX_OK; +} + +static int nct6694_can_start(struct net_device *ndev) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + struct nct6694_can_setting *setting = &priv->tx->setting; + const struct can_bittiming *n_bt = &priv->can.bittiming; + const struct can_bittiming *d_bt = &priv->can.data_bittiming; + int ret; + + guard(mutex)(&priv->lock); + + memset(setting, 0, sizeof(*setting)); + setting->nbr = cpu_to_le32(n_bt->bitrate); + setting->dbr = cpu_to_le32(d_bt->bitrate); + + if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) + setting->ctrl1 |= cpu_to_le16(NCT6694_CAN_SETTING_CTRL1_MON); + + if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && + priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) + setting->ctrl1 |= cpu_to_le16(NCT6694_CAN_SETTING_CTRL1_NISO); + + if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) + setting->ctrl1 |= cpu_to_le16(NCT6694_CAN_SETTING_CTRL1_LBCK); + + ret = nct6694_write_msg(priv->nct6694, NCT6694_CAN_MOD, + NCT6694_CAN_SETTING(priv->can_idx), + sizeof(*setting), setting); + if (ret) + return ret; + + priv->can.state = CAN_STATE_ERROR_ACTIVE; + + return ret; +} + +static int nct6694_can_stop(struct net_device *ndev) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + + netif_stop_queue(ndev); + free_irq(ndev->irq, ndev); + destroy_workqueue(priv->wq); + priv->wq = NULL; + nct6694_can_clean(ndev); + priv->can.state = CAN_STATE_STOPPED; + can_rx_offload_disable(&priv->offload); + close_candev(ndev); + + return 0; +} + +static int nct6694_can_set_mode(struct net_device *ndev, enum can_mode mode) +{ + switch (mode) { + case CAN_MODE_START: + nct6694_can_clean(ndev); + nct6694_can_start(ndev); + netif_wake_queue(ndev); + break; + default: + return -EOPNOTSUPP; + } + + return 0; +} + +static int nct6694_can_open(struct net_device *ndev) +{ + struct nct6694_can_priv *priv = netdev_priv(ndev); + int ret; + + ret = open_candev(ndev); + if (ret) + return ret; + + can_rx_offload_enable(&priv->offload); + + ret = request_threaded_irq(ndev->irq, NULL, + nct6694_can_irq, IRQF_ONESHOT, + "nct6694_can", ndev); + if (ret) { + netdev_err(ndev, "Failed to request IRQ\n"); + goto close_candev; + } + + priv->wq = alloc_ordered_workqueue("%s-nct6694_wq", + WQ_FREEZABLE | WQ_MEM_RECLAIM, + ndev->name); + if (!priv->wq) { + ret = -ENOMEM; + goto free_irq; + } + + priv->tx_skb = NULL; + priv->tx_busy = false; + + ret = nct6694_can_start(ndev); + if (ret) + goto destroy_wq; + + netif_start_queue(ndev); + + return 0; + +destroy_wq: + destroy_workqueue(priv->wq); +free_irq: + free_irq(ndev->irq, ndev); +close_candev: + can_rx_offload_disable(&priv->offload); + close_candev(ndev); + return ret; +} + +static const struct net_device_ops nct6694_can_netdev_ops = { + .ndo_open = nct6694_can_open, + .ndo_stop = nct6694_can_stop, + .ndo_start_xmit = nct6694_can_start_xmit, + .ndo_change_mtu = can_change_mtu, +}; + +static const struct ethtool_ops nct6694_can_ethtool_ops = { + .get_ts_info = ethtool_op_get_ts_info, +}; + +static int nct6694_can_get_clock(struct nct6694_can_priv *priv) +{ + struct nct6694_can_information *info = &priv->rx->info; + int ret; + + ret = nct6694_read_msg(priv->nct6694, NCT6694_CAN_MOD, + NCT6694_CAN_INFORMATION, + sizeof(*info), + info); + if (ret) + return ret; + + return le32_to_cpu(info->can_clk); +} + +static int nct6694_can_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_can_priv *priv; + struct net_device *ndev; + int ret, irq, can_clk; + + irq = irq_create_mapping(nct6694->domain, + NCT6694_IRQ_CAN1 + cell->id); + if (!irq) + return irq; + + ndev = alloc_candev(sizeof(struct nct6694_can_priv), 1); + if (!ndev) + return -ENOMEM; + + ndev->irq = irq; + ndev->flags |= IFF_ECHO; + ndev->netdev_ops = &nct6694_can_netdev_ops; + ndev->ethtool_ops = &nct6694_can_ethtool_ops; + + priv = netdev_priv(ndev); + priv->nct6694 = nct6694; + priv->ndev = ndev; + + priv->tx = devm_kzalloc(&pdev->dev, sizeof(union nct6694_can_tx), + GFP_KERNEL); + if (!priv->tx) { + ret = -ENOMEM; + goto free_candev; + } + + priv->rx = devm_kzalloc(&pdev->dev, sizeof(union nct6694_can_rx), + GFP_KERNEL); + if (!priv->rx) { + ret = -ENOMEM; + goto free_candev; + } + + can_clk = nct6694_can_get_clock(priv); + if (can_clk < 0) { + ret = dev_err_probe(&pdev->dev, can_clk, + "Failed to get clock\n"); + goto free_candev; + } + + devm_mutex_init(&pdev->dev, &priv->lock); + INIT_WORK(&priv->tx_work, nct6694_can_tx_work); + + priv->can_idx = cell->id; + priv->can.state = CAN_STATE_STOPPED; + priv->can.clock.freq = can_clk; + priv->can.bittiming_const = &nct6694_can_bittiming_nominal_const; + priv->can.data_bittiming_const = &nct6694_can_bittiming_data_const; + priv->can.do_set_mode = nct6694_can_set_mode; + priv->can.do_get_berr_counter = nct6694_can_get_berr_counter; + + priv->can.ctrlmode = CAN_CTRLMODE_FD; + + priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | + CAN_CTRLMODE_LISTENONLY | + CAN_CTRLMODE_FD | + CAN_CTRLMODE_FD_NON_ISO | + CAN_CTRLMODE_BERR_REPORTING; + + ret = can_rx_offload_add_manual(ndev, &priv->offload, + NCT6694_NAPI_WEIGHT); + if (ret) { + dev_err_probe(&pdev->dev, ret, "Failed to add rx_offload\n"); + goto free_candev; + } + + platform_set_drvdata(pdev, priv); + SET_NETDEV_DEV(priv->ndev, &pdev->dev); + + ret = register_candev(priv->ndev); + if (ret) + goto del_rx_offload; + + return 0; + +del_rx_offload: + can_rx_offload_del(&priv->offload); +free_candev: + free_candev(ndev); + return ret; +} + +static void nct6694_can_remove(struct platform_device *pdev) +{ + struct nct6694_can_priv *priv = platform_get_drvdata(pdev); + + cancel_work_sync(&priv->tx_work); + unregister_candev(priv->ndev); + can_rx_offload_del(&priv->offload); + free_candev(priv->ndev); +} + +static struct platform_driver nct6694_can_driver = { + .driver = { + .name = DRVNAME, + }, + .probe = nct6694_can_probe, + .remove = nct6694_can_remove, +}; + +module_platform_driver(nct6694_can_driver); + +MODULE_DESCRIPTION("USB-CAN FD driver for NCT6694"); +MODULE_AUTHOR("Ming Yu "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:nct6694-can"); From patchwork Fri Dec 27 09:57:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 13921660 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 52129157485; Fri, 27 Dec 2024 09:58:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293489; cv=none; b=E5wFc+68DP9ji1kGumGL3bRil4gnUgI16FtHtF13k+mLRcNXRhIAQatfrRCtlVa6TCPtxLiMcG3nHZFj/p0JD6nkItr/WuZUSyLU122BcjKUQ4l73eceLY1sNg5BAAO/y4YvNHdwE2LxSJram4KZ6/QrvHj3eKrck7il7Optkz4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293489; c=relaxed/simple; bh=Ld9u3Aoym82RSzgXXmcekrIoGKetolbVjamaQpbhS2s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NM48eTJPfK+BKiUGYAFErJEyXtMyn0aHF2vezwxdOdZBdi94e6zEqAhu5PfG2woUHNDpg6A3m/fC2xZYyK6JOjWPZCNFIgqAKyzonSgR2HwyyM0XX7TB2CEeZAZgqZF8CG2xxJ2kxmaRfSWZ7RMpU2OiikKh0FHm6oQc9iy3Yeg= 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=V7sXr44/; arc=none smtp.client-ip=209.85.214.174 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="V7sXr44/" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2164b662090so76100345ad.1; Fri, 27 Dec 2024 01:58:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735293487; x=1735898287; 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=SqRk5S6yW3NsUs+vZjO2HLDWNmt9cKJ0aYTZj+k0juY=; b=V7sXr44/ex5xCzMxLQPCv6guv8qMb5+BhTHo8gcpui6FtpsOYrZRmcC715fOOnN2dP MN6CpnDyU8vSkkGsUAXcJ5hyOcwDn6yfOeSVpNI3vE0UR+QeZiESJP7RSyeouG++2g6q r6ZAzb3LeJLexZZ6No8OYhljo0Iyi3JnQN6ERlin5+alKNOHqLuVVIPsXSk2a2LEZblI jhbExeoEDJtgE6uBblRmY5mVrUbdLOrM/MMj2BEQXCeld2jjqpWEzEIOJ+AhLipCAKUr NHx4wQ0AoqYpR2yHPexuAvyWg1JsMvqUG/wdaZIALrQMvjFsB8aUsjP66UQPU2DHtEUf LJvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735293487; x=1735898287; 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=SqRk5S6yW3NsUs+vZjO2HLDWNmt9cKJ0aYTZj+k0juY=; b=Ga1nMDzfthdYo9UCDgCVfpniNAva155r2ww9wrql1Z8Os88hxWwo3UEJS2g3VI1nBa N90U7gyouIJgBlPq0zTet+9S88L/9MispSmnRtSMSj6EiYlUIToMcjI6MPqFcE460naD GPL2h8+fqj46aKpb9TSdhYQLADD4qJSpixJe1Lx3VIkkV3Ih/zA4rtLCztCDHYDnUHzt C6wbkBOfd6ci6QTpO1rOD2rz23l8AFelSEb/SqprydZfu4xL4r7Y4pLgW1wk6uNpu8ry Py1cSRgJAvA/Z0No7qWWG73v49GE/AfXz7Vsd4b+6qzjiDeWHRtOnKDBLD7XAXi//M+A Fh5w== X-Forwarded-Encrypted: i=1; AJvYcCUkDIGN3KIEyJFrZQB5BMrjQpQ6fhnnCsoZ05QlfZ44ZnwgMInq13+ErXMReHhjqH9s0NGjbMiepHxl@vger.kernel.org, AJvYcCUtTaiVoFqQhuwjcfz3hvYq9w9jDCG4zT8TIX2ceWHYuAa+NYJUlrv4DkRR03ubG2Qlg9ijFUH/DTT2bg==@vger.kernel.org, AJvYcCUwrO3n4b6Da4lgyAbN05+7LRo9qmnPvJ5gUcz4f1nFi/atNe1B4TyAFUDcRpwa5F0+Kciwx7DTcSOz@vger.kernel.org, AJvYcCV9FnN3Rkptc1+7tXRZymbXtph+19KDk3cr83W9JgVOXCYmydnUBZOl/37CsyqoCcbqUL7A0qVSJ11B89ySDpA=@vger.kernel.org, AJvYcCVvQRfr+SSPqDpezcqFWVXbjHMnIBakUcnfHhL4uS7fjZ0KqdHgy97EgTGWluiVK8+SmwFAhr2C@vger.kernel.org, AJvYcCX7Y1m1pQTbzLYHyIKmFiKsl7j5QQR9c3NYyjVAhTSFAPlc2QTV7YOSQUNVtGHd9DexlnnU1BE1bwZqMEE=@vger.kernel.org, AJvYcCXgZsyp8bqmj3BSf9D2HIj4wPJGrF9uuBSv+wFqAz49YC2MseqnviAiMiLYKJdQYcECGidUyNG/VYc=@vger.kernel.org X-Gm-Message-State: AOJu0YxUntZbloBkUiBizz9yxfk+FO3AGCfSV4/33n/BiZG/VVJQ+AJU M+vUvZ+L4RDy4+QY0q1/BdhEczLxUV/uqKhfc8o5Cu13f+NF4BlB X-Gm-Gg: ASbGnctNePlw/AU5WDjmhNqH6l7VkgMF++6t4Dubh/baTbVNvZFP3VftK1mNYByVljH RAhFe5FyFs13jsbhNHm/BHa4Wl0nrRhcFLHtCfp/K0xmWSXy0nX4m0eUt/6QAn7PyPRPr8COAM+ fN7fH4s3my/rs60ClprI9DC7RFDVo2DE6HMy/hd20NNfx2wjPz56N0w5vEciWwC0PTuzGqPiNQq eDXTFghk8N+vykCe6exX4WzNJm2X9JGaXTBUAfdpYEyaSYAL8M4VmaE7kRnFHAQ9KFlPCN/ctBo DgLhLNjD7tfDp78d44w//umL X-Google-Smtp-Source: AGHT+IE1nFWdsRiIr4L8ey3LUmPvgb1tfJu/MCyLVfIe5kp8z0V5Wzly1sJrrx/RD/M4+3jGoPfk1Q== X-Received: by 2002:a17:903:946:b0:216:2af7:a2a3 with SMTP id d9443c01a7336-219e6f27157mr452121025ad.53.1735293486621; Fri, 27 Dec 2024 01:58:06 -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 d9443c01a7336-219dc9f51a0sm131581135ad.187.2024.12.27.01.58.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Dec 2024 01:58:06 -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, Ming Yu Subject: [PATCH v4 5/7] watchdog: Add Nuvoton NCT6694 WDT support Date: Fri, 27 Dec 2024 17:57:25 +0800 Message-Id: <20241227095727.2401257-6-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241227095727.2401257-1-a0282524688@gmail.com> References: <20241227095727.2401257-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-hwmon@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 | 281 +++++++++++++++++++++++++++++++++ 4 files changed, 294 insertions(+) create mode 100644 drivers/watchdog/nct6694_wdt.c diff --git a/MAINTAINERS b/MAINTAINERS index b961c4827648..254c01a6bba1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16730,6 +16730,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 f81705f8539a..9471a39b97ab 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..58171b9de982 --- /dev/null +++ b/drivers/watchdog/nct6694_wdt.c @@ -0,0 +1,281 @@ +// 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(idx) (idx ? 0x0100 : 0x0000) /* SEL|CMD */ + +/* Command 01h - WDT Command */ +#define NCT6694_WDT_COMMAND(idx) (idx ? 0x0101 : 0x0001) /* 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_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 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 *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(setup, 0, sizeof(*setup)); + setup->timeout = cpu_to_le32(timeout_fmt); + setup->pretimeout = cpu_to_le32(pretimeout_fmt); + + return nct6694_write_msg(nct6694, NCT6694_WDT_MOD, + NCT6694_WDT_SETUP(data->wdev_idx), + sizeof(*setup), 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 *nct6694 = data->nct6694; + + guard(mutex)(&data->lock); + + memcpy(&cmd->wdt_cmd, "WDTC", 4); + cmd->reserved = 0; + + return nct6694_write_msg(nct6694, NCT6694_WDT_MOD, + NCT6694_WDT_COMMAND(data->wdev_idx), + sizeof(*cmd), 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 *nct6694 = data->nct6694; + + guard(mutex)(&data->lock); + memcpy(&cmd->wdt_cmd, "WDTS", 4); + cmd->reserved = 0; + + return nct6694_write_msg(nct6694, NCT6694_WDT_MOD, + NCT6694_WDT_COMMAND(data->wdev_idx), + sizeof(*cmd), 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 *nct6694 = data->nct6694; + unsigned int timeleft_ms; + int ret; + + guard(mutex)(&data->lock); + + ret = nct6694_read_msg(nct6694, NCT6694_WDT_MOD, + NCT6694_WDT_SETUP(data->wdev_idx), + sizeof(*setup), 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 Fri Dec 27 09:57:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 13921661 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 8EAD219005D; Fri, 27 Dec 2024 09:58:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293493; cv=none; b=io0/x2RxyIVPpAVQSsy/8+JlpP03y50kND0WcDc7Xsv51fVX8S1A5QImtpcyeuS9ORv28Sw7twUAiu/Cbc5v4K1tbscoHcJBOINd5MUV4V6WA0SvzwIyNJarhgF1k0HtL8QUn7S/EGWGYup5fMz6j2gP41l8NrEMOevuosr2eaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293493; c=relaxed/simple; bh=3wd6YDMCoq6xQENr/p9KfioqU56nDfOHXwHoB2GfPBI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XsgNXoqL9xljy0UVawr4uGZyOih4pTNpXag2gwSnDABIToeQaHKEBDFTPZn/zOrKd+jdSHMlZzadJ44kJZuQOMTQjHqDj+EdwAY2dQDpWUvNBDsTOYw2qudiwu3+ejA01yQ5qv6U+AJdIIJvmxyuQmjeohlpzOKKc47OChXnLyo= 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=YGIcjcxg; arc=none smtp.client-ip=209.85.216.48 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="YGIcjcxg" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2f44353649aso7028910a91.0; Fri, 27 Dec 2024 01:58:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735293491; x=1735898291; 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=mme3B7VdhA7hPX6xHY7jG4qoa95mGYpdP3Rmc8mP1PM=; b=YGIcjcxgibT8S3L/+xt76OSq+2acXVhOxSoM4shDT5p/wBlXrhJxSueJncv8jPo9jr 0O6OZlUcPqYtz81PhpMDvbR5/qw1HYWAIxCC6+O9p4mPcCnjtEazvn1On35cihiuqh0b exhiangNwMgnXBqPG7scEtadkBeefpLqGJ6EGzaAjveo9VZUJesCL5OGOTV64lUDO6UY DhR93Z2rrAdnvADdL29cOuaSyJHZN5GYD+74xJDncE4OPx9+5ElfvcXWkW+l+PmANvaQ OUFdg+gn/OGe7yjafU3VEtotecPEVvMFD7XFeNv7cw4A8GdGKA4bIBc3qSac0JzEuNSg ngBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735293491; x=1735898291; 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=mme3B7VdhA7hPX6xHY7jG4qoa95mGYpdP3Rmc8mP1PM=; b=RO0BavofjWr0uwtmMuqMbZXbk/ZKBNNuHBb5w1djeqDr50I5XcKiT3e8IlNlwNHjn7 CCoT9yRFYHEJ8v28+Dp3rRN7xT4w5NUQnF5dSil62SIcs4Gve2FuAPv1G4kHCnxRPVO7 AH+uW/8gBXt+pHg/BoCYoyTlpzEzz1duxATx9b9wmAR3wJ9UBnLFHaJoRZaBWoWw/4SB cT+FOhXOsX6fPy9uxJMQI6egznBFpqkS1y2S+wezIzXKIH4hbaCE/PZkuNYWF4y9Mgu1 kMl3ChRfkjssIiPSF20dN8YbLEV+7sTr3L+FPRWvEsddlBNh/hjVn6L26DcWmSe3fMV0 6JnQ== X-Forwarded-Encrypted: i=1; AJvYcCU/8I8fGL7vAL5pgfDP2B08DqKx/PIpigzHzjLwYf/B4+FYnCBAj0yJU4Wa+NZ6gd5IzlaKVCtI1veB@vger.kernel.org, AJvYcCUM6PnFhmGt22qreut6xlTSlLapuqcly+NVc+E1S4obnazj4DvPWiPoZQNa+qRpldazh+bpecr6v5MhNUg=@vger.kernel.org, AJvYcCVM3mAPkbHy6oOY7pVEbX7Fda2WJE3bMJrfVTOdPNhPERmdXMzFGZYfhbIIQ4QrM28gz8GdMn2Rk5E=@vger.kernel.org, AJvYcCVk1eyJV1udUG9qJ4Scq2sR5KM6dSi8wS+1tb97rQ5XjRxyRk51hclDOfXVYZNa13gm6MgTdWLE@vger.kernel.org, AJvYcCW1E7C61SukydLvOkNOJ+KorzWnp1sKoVdQujA+vSxp1KvedZgw5F0zjpaqTBVX863n0nMSDsT7x9FUBEC9YrQ=@vger.kernel.org, AJvYcCX3lsZhBGpRACHgjnnd+zmHFV4rOVnGIvmwp0G669lph1hDfbXkVkpNZ4oqrcoTRKnSSQX9b4sejRcJOw==@vger.kernel.org, AJvYcCXofrJYr4QGcGwUowrj7l92XXB1YPcVkS0H9dEwoVGWxtwAZHP/9ccujq3XvYUvwSTCQ/DZNJF0lvAN@vger.kernel.org X-Gm-Message-State: AOJu0Yx2XBE+26HHfbnkdF4rQsqhjG4XpWwe1yi2M7WKL4zZU0Ftmolp SV7ybCgLa/lTAIGYtnSg7prczJeCMUcaFM3v9EiWAxQQj/jkx/xq X-Gm-Gg: ASbGnctEiTQqcjQ3h5y4JoVozTx+SdqmGmDjKoNBqfn/JCg6d7qW8itf6qdCzRcpmXL kuiS25jH6VmSo40x7E4MMAhlYdhhr11MyxePZlntUpozQMlM/LetVc42tZRcvD2TcJ4dae3XKdn /DHOskVXfoGHYdTd7VeFYXPF5x2iajEjcxTmgad0IK+uYz6GouywlfvyvoNxrYuiSScGaQKz5FN QHYZsiHq54r0fi2xDZvRsltbsob3Qnh2XMpfLYoCEpCVkx2iLCcpCvVgwi7T1DlJfiUHuUMgUAt 77sdBtdjCqEgAsa5KHVCSD/Z X-Google-Smtp-Source: AGHT+IFsRWVcAe4IKdUxg4C4JalCFSB245PXQ8PR8vnuMazUIoR6ePzZEP1n2s4kX0swFGm1O9RM/w== X-Received: by 2002:a17:90b:54d0:b0:2ee:6d08:7936 with SMTP id 98e67ed59e1d1-2f452e4d0bdmr39672931a91.20.1735293490670; Fri, 27 Dec 2024 01:58:10 -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 d9443c01a7336-219dc9f51a0sm131581135ad.187.2024.12.27.01.58.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Dec 2024 01:58:10 -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, Ming Yu Subject: [PATCH v4 6/7] hwmon: Add Nuvoton NCT6694 HWMON support Date: Fri, 27 Dec 2024 17:57:26 +0800 Message-Id: <20241227095727.2401257-7-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241227095727.2401257-1-a0282524688@gmail.com> References: <20241227095727.2401257-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This driver supports Hardware monitor functionality for NCT6694 MFD device based on USB interface. Signed-off-by: Ming Yu --- MAINTAINERS | 1 + drivers/hwmon/Kconfig | 10 + drivers/hwmon/Makefile | 1 + drivers/hwmon/nct6694-hwmon.c | 851 ++++++++++++++++++++++++++++++++++ 4 files changed, 863 insertions(+) create mode 100644 drivers/hwmon/nct6694-hwmon.c diff --git a/MAINTAINERS b/MAINTAINERS index 254c01a6bba1..e5c65e382141 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16727,6 +16727,7 @@ M: Ming Yu L: linux-kernel@vger.kernel.org S: Supported F: drivers/gpio/gpio-nct6694.c +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 diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index dd376602f3f1..df40986424bd 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1636,6 +1636,16 @@ config SENSORS_NCT6683 This driver can also be built as a module. If so, the module will be called nct6683. +config SENSORS_NCT6694 + tristate "Nuvoton NCT6694 Hardware Monitor support" + depends on MFD_NCT6694 + help + Say Y here to support Nuvoton NCT6694 hardware monitoring + functionality. + + This driver can also be built as a module. If so, the module + will be called nct6694-hwmon. + config SENSORS_NCT6775_CORE tristate select REGMAP diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index b827b92f2a78..27a43e67cdb7 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -168,6 +168,7 @@ obj-$(CONFIG_SENSORS_MLXREG_FAN) += mlxreg-fan.o obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o obj-$(CONFIG_SENSORS_MR75203) += mr75203.o obj-$(CONFIG_SENSORS_NCT6683) += nct6683.o +obj-$(CONFIG_SENSORS_NCT6694) += nct6694-hwmon.o obj-$(CONFIG_SENSORS_NCT6775_CORE) += nct6775-core.o nct6775-objs := nct6775-platform.o obj-$(CONFIG_SENSORS_NCT6775) += nct6775.o diff --git a/drivers/hwmon/nct6694-hwmon.c b/drivers/hwmon/nct6694-hwmon.c new file mode 100644 index 000000000000..a11d495e2875 --- /dev/null +++ b/drivers/hwmon/nct6694-hwmon.c @@ -0,0 +1,851 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Nuvoton NCT6694 HWMON 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 report channel + * This defines the module type used for communication with the NCT6694 + * report channel over the USB interface. + */ +#define NCT6694_RPT_MOD 0xFF + +/* Report channel */ +/* + * The report channel is used to report the status of the hardware monitor + * devices, such as voltage, temperature, fan speed, and PWM. + */ +#define NCT6694_VIN_IDX(x) (0x00 + (x)) /* OFFSET */ +#define NCT6694_TIN_IDX(x) \ + ({ typeof(x) (_x) = (x); \ + ((_x) < 10) ? (0x10 + ((_x) * 2)) : \ + (0x30 + (((_x) - 10) * 2)); }) /* OFFSET */ +#define NCT6694_FIN_IDX(x) (0x50 + ((x) * 2)) /* OFFSET */ +#define NCT6694_PWM_IDX(x) (0x70 + (x)) /* OFFSET */ +#define NCT6694_VIN_STS(x) (0x68 + (x)) /* OFFSET */ +#define NCT6694_TIN_STS(x) (0x6A + (x)) /* OFFSET */ +#define NCT6694_FIN_STS(x) (0x6E + (x)) /* OFFSET */ + +/* + * USB command module type for NCT6694 HWMON controller. + * This defines the module type used for communication with the NCT6694 + * HWMON controller over the USB interface. + */ +#define NCT6694_HWMON_MOD 0x00 + +/* Command 00h - Hardware Monitor Control */ +#define NCT6694_HWMON_CONTROL 0x0000 /* SEL|CMD */ + +/* Command 02h - Alarm Control */ +#define NCT6694_HWMON_ALARM 0x0002 /* SEL|CMD */ + +/* + * USB command module type for NCT6694 PWM controller. + * This defines the module type used for communication with the NCT6694 + * PWM controller over the USB interface. + */ +#define NCT6694_PWM_MOD 0x01 + +/* PWM Command - Manual Control */ +#define NCT6694_PWM_CONTROL 0x0001 /* SEL|CMD */ + +#define NCT6694_FREQ_FROM_REG(reg) ((reg) * 25000 / 255) +#define NCT6694_FREQ_TO_REG(val) \ + (DIV_ROUND_CLOSEST(clamp_val((val), 100, 25000) * 255, 25000)) + +#define NCT6694_LSB_REG_MASK GENMASK(7, 5) +#define NCT6694_TIN_HYST_MASK GENMASK(7, 5) + +enum nct6694_hwmon_temp_mode { + NCT6694_HWMON_TWOTIME_IRQ = 0, + NCT6694_HWMON_ONETIME_IRQ, + NCT6694_HWMON_REALTIME_IRQ, + NCT6694_HWMON_COMPARE_IRQ, +}; + +struct __packed nct6694_hwmon_control { + u8 vin_en[2]; + u8 tin_en[2]; + u8 fin_en[2]; + u8 pwm_en[2]; + u8 reserved1[40]; + u8 pwm_freq[10]; + u8 reserved2[6]; +}; + +struct __packed nct6694_hwmon_alarm { + u8 smi_ctrl; + u8 reserved1[15]; + struct { + u8 hl; + u8 ll; + } vin_limit[16]; + struct { + u8 hyst; + s8 hl; + } tin_cfg[32]; + __be16 fin_ll[10]; + u8 reserved2[4]; +}; + +struct __packed nct6694_pwm_control { + u8 mal_en[2]; + u8 mal_val[10]; + u8 reserved[12]; +}; + +union nct6694_hwmon_rpt { + u8 vin; + struct { + u8 msb; + u8 lsb; + } tin; + __be16 fin; + u8 pwm; + u8 status; +}; + +union nct6694_hwmon_msg { + struct nct6694_hwmon_control hwmon_ctrl; + struct nct6694_hwmon_alarm hwmon_alarm; + struct nct6694_pwm_control pwm_ctrl; +}; + +struct nct6694_hwmon_data { + struct nct6694 *nct6694; + struct mutex lock; + struct nct6694_hwmon_control hwmon_en; + union nct6694_hwmon_rpt *rpt; + union nct6694_hwmon_msg *msg; +}; + +static inline long in_from_reg(u8 reg) +{ + return reg * 16; +} + +static inline u8 in_to_reg(long val) +{ + return DIV_ROUND_CLOSEST(val, 16); +} + +static inline long temp_from_reg(s8 reg) +{ + return reg * 1000; +} + +static inline s8 temp_to_reg(long val) +{ + return DIV_ROUND_CLOSEST(val, 1000); +} + +#define NCT6694_HWMON_IN_CONFIG (HWMON_I_INPUT | HWMON_I_ENABLE | \ + HWMON_I_MAX | HWMON_I_MIN | \ + HWMON_I_ALARM) +#define NCT6694_HWMON_TEMP_CONFIG (HWMON_T_INPUT | HWMON_T_ENABLE | \ + HWMON_T_MAX | HWMON_T_MAX_HYST | \ + HWMON_T_MAX_ALARM) +#define NCT6694_HWMON_FAN_CONFIG (HWMON_F_INPUT | HWMON_F_ENABLE | \ + HWMON_F_MIN | HWMON_F_MIN_ALARM) +#define NCT6694_HWMON_PWM_CONFIG (HWMON_PWM_INPUT | HWMON_PWM_ENABLE | \ + HWMON_PWM_FREQ) +static const struct hwmon_channel_info *nct6694_info[] = { + HWMON_CHANNEL_INFO(in, + NCT6694_HWMON_IN_CONFIG, /* VIN0 */ + NCT6694_HWMON_IN_CONFIG, /* VIN1 */ + NCT6694_HWMON_IN_CONFIG, /* VIN2 */ + NCT6694_HWMON_IN_CONFIG, /* VIN3 */ + NCT6694_HWMON_IN_CONFIG, /* VIN5 */ + NCT6694_HWMON_IN_CONFIG, /* VIN6 */ + NCT6694_HWMON_IN_CONFIG, /* VIN7 */ + NCT6694_HWMON_IN_CONFIG, /* VIN14 */ + NCT6694_HWMON_IN_CONFIG, /* VIN15 */ + NCT6694_HWMON_IN_CONFIG, /* VIN16 */ + NCT6694_HWMON_IN_CONFIG, /* VBAT */ + NCT6694_HWMON_IN_CONFIG, /* VSB */ + NCT6694_HWMON_IN_CONFIG, /* AVSB */ + NCT6694_HWMON_IN_CONFIG, /* VCC */ + NCT6694_HWMON_IN_CONFIG, /* VHIF */ + NCT6694_HWMON_IN_CONFIG), /* VTT */ + + HWMON_CHANNEL_INFO(temp, + NCT6694_HWMON_TEMP_CONFIG, /* THR1 */ + NCT6694_HWMON_TEMP_CONFIG, /* THR2 */ + NCT6694_HWMON_TEMP_CONFIG, /* THR14 */ + NCT6694_HWMON_TEMP_CONFIG, /* THR15 */ + NCT6694_HWMON_TEMP_CONFIG, /* THR16 */ + NCT6694_HWMON_TEMP_CONFIG, /* TDP0 */ + NCT6694_HWMON_TEMP_CONFIG, /* TDP1 */ + NCT6694_HWMON_TEMP_CONFIG, /* TDP2 */ + NCT6694_HWMON_TEMP_CONFIG, /* TDP3 */ + NCT6694_HWMON_TEMP_CONFIG, /* TDP4 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN0 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN1 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN2 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN3 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN4 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN5 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN6 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN7 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN8 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN9 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN10 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN11 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN12 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN13 */ + NCT6694_HWMON_TEMP_CONFIG, /* DTIN14 */ + NCT6694_HWMON_TEMP_CONFIG), /* DTIN15 */ + + HWMON_CHANNEL_INFO(fan, + NCT6694_HWMON_FAN_CONFIG, /* FIN0 */ + NCT6694_HWMON_FAN_CONFIG, /* FIN1 */ + NCT6694_HWMON_FAN_CONFIG, /* FIN2 */ + NCT6694_HWMON_FAN_CONFIG, /* FIN3 */ + NCT6694_HWMON_FAN_CONFIG, /* FIN4 */ + NCT6694_HWMON_FAN_CONFIG, /* FIN5 */ + NCT6694_HWMON_FAN_CONFIG, /* FIN6 */ + NCT6694_HWMON_FAN_CONFIG, /* FIN7 */ + NCT6694_HWMON_FAN_CONFIG, /* FIN8 */ + NCT6694_HWMON_FAN_CONFIG), /* FIN9 */ + + HWMON_CHANNEL_INFO(pwm, + NCT6694_HWMON_PWM_CONFIG, /* PWM0 */ + NCT6694_HWMON_PWM_CONFIG, /* PWM1 */ + NCT6694_HWMON_PWM_CONFIG, /* PWM2 */ + NCT6694_HWMON_PWM_CONFIG, /* PWM3 */ + NCT6694_HWMON_PWM_CONFIG, /* PWM4 */ + NCT6694_HWMON_PWM_CONFIG, /* PWM5 */ + NCT6694_HWMON_PWM_CONFIG, /* PWM6 */ + NCT6694_HWMON_PWM_CONFIG, /* PWM7 */ + NCT6694_HWMON_PWM_CONFIG, /* PWM8 */ + NCT6694_HWMON_PWM_CONFIG), /* PWM9 */ + NULL +}; + +static int nct6694_in_read(struct device *dev, u32 attr, int channel, + long *val) +{ + struct nct6694_hwmon_data *data = dev_get_drvdata(dev); + unsigned char vin_en; + int ret; + + guard(mutex)(&data->lock); + + switch (attr) { + case hwmon_in_enable: + vin_en = data->hwmon_en.vin_en[(channel / 8)]; + *val = !!(vin_en & BIT(channel % 8)); + + return 0; + case hwmon_in_input: + ret = nct6694_read_msg(data->nct6694, NCT6694_RPT_MOD, + NCT6694_VIN_IDX(channel), + sizeof(data->rpt->vin), + &data->rpt->vin); + if (ret) + return ret; + + *val = in_from_reg(data->rpt->vin); + + return 0; + case hwmon_in_max: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + *val = in_from_reg(data->msg->hwmon_alarm.vin_limit[channel].hl); + + return 0; + case hwmon_in_min: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + *val = in_from_reg(data->msg->hwmon_alarm.vin_limit[channel].ll); + + return 0; + case hwmon_in_alarm: + ret = nct6694_read_msg(data->nct6694, NCT6694_RPT_MOD, + NCT6694_VIN_STS(channel / 8), + sizeof(data->rpt->status), + &data->rpt->status); + if (ret) + return ret; + + *val = !!(data->rpt->status & BIT(channel % 8)); + + return 0; + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_temp_read(struct device *dev, u32 attr, int channel, + long *val) +{ + struct nct6694_hwmon_data *data = dev_get_drvdata(dev); + unsigned char temp_en, temp_hyst; + signed char temp_max; + int ret, temp_raw; + + guard(mutex)(&data->lock); + + switch (attr) { + case hwmon_temp_enable: + temp_en = data->hwmon_en.tin_en[channel / 8]; + *val = !!(temp_en & BIT(channel % 8)); + + return 0; + case hwmon_temp_input: + ret = nct6694_read_msg(data->nct6694, NCT6694_RPT_MOD, + NCT6694_TIN_IDX(channel), + sizeof(data->rpt->tin), + &data->rpt->tin); + if (ret) + return ret; + + temp_raw = data->rpt->tin.msb << 3; + temp_raw |= FIELD_GET(NCT6694_LSB_REG_MASK, data->rpt->tin.lsb); + + /* Real temperature(milli degrees Celsius) = temp_raw * 1000 * 0.125 */ + *val = sign_extend32(temp_raw, 10) * 125; + + return 0; + case hwmon_temp_max: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + *val = temp_from_reg(data->msg->hwmon_alarm.tin_cfg[channel].hl); + + return 0; + case hwmon_temp_max_hyst: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + temp_max = data->msg->hwmon_alarm.tin_cfg[channel].hl; + temp_hyst = FIELD_GET(NCT6694_TIN_HYST_MASK, + data->msg->hwmon_alarm.tin_cfg[channel].hyst); + *val = temp_from_reg(temp_max - temp_hyst); + + return 0; + case hwmon_temp_max_alarm: + ret = nct6694_read_msg(data->nct6694, NCT6694_RPT_MOD, + NCT6694_TIN_STS(channel / 8), + sizeof(data->rpt->status), + &data->rpt->status); + if (ret) + return ret; + + *val = !!(data->rpt->status & BIT(channel % 8)); + + return 0; + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_fan_read(struct device *dev, u32 attr, int channel, + long *val) +{ + struct nct6694_hwmon_data *data = dev_get_drvdata(dev); + unsigned char fanin_en; + int ret; + + guard(mutex)(&data->lock); + + switch (attr) { + case hwmon_fan_enable: + fanin_en = data->hwmon_en.fin_en[channel / 8]; + *val = !!(fanin_en & BIT(channel % 8)); + + return 0; + case hwmon_fan_input: + ret = nct6694_read_msg(data->nct6694, NCT6694_RPT_MOD, + NCT6694_FIN_IDX(channel), + sizeof(data->rpt->fin), + &data->rpt->fin); + if (ret) + return ret; + + *val = be16_to_cpu(data->rpt->fin); + + return 0; + case hwmon_fan_min: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + *val = be16_to_cpu(data->msg->hwmon_alarm.fin_ll[channel]); + + return 0; + case hwmon_fan_min_alarm: + ret = nct6694_read_msg(data->nct6694, NCT6694_RPT_MOD, + NCT6694_FIN_STS(channel / 8), + sizeof(data->rpt->status), + &data->rpt->status); + if (ret) + return ret; + + *val = !!(data->rpt->status & BIT(channel % 8)); + + return 0; + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_pwm_read(struct device *dev, u32 attr, int channel, + long *val) +{ + struct nct6694_hwmon_data *data = dev_get_drvdata(dev); + unsigned char pwm_en; + int ret; + + guard(mutex)(&data->lock); + + switch (attr) { + case hwmon_pwm_enable: + pwm_en = data->hwmon_en.pwm_en[channel / 8]; + *val = !!(pwm_en & BIT(channel % 8)); + + return 0; + case hwmon_pwm_input: + ret = nct6694_read_msg(data->nct6694, NCT6694_RPT_MOD, + NCT6694_PWM_IDX(channel), + sizeof(data->rpt->fin), + &data->rpt->fin); + if (ret) + return ret; + + *val = data->rpt->fin; + + return 0; + case hwmon_pwm_freq: + *val = NCT6694_FREQ_FROM_REG(data->hwmon_en.pwm_freq[channel]); + + return 0; + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_in_write(struct device *dev, u32 attr, int channel, + long val) +{ + struct nct6694_hwmon_data *data = dev_get_drvdata(dev); + int ret; + + guard(mutex)(&data->lock); + + switch (attr) { + case hwmon_in_enable: + if (val == 0) + data->hwmon_en.vin_en[channel / 8] &= ~BIT(channel % 8); + else if (val == 1) + data->hwmon_en.vin_en[channel / 8] |= BIT(channel % 8); + else + return -EINVAL; + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_CONTROL, + sizeof(data->msg->hwmon_ctrl), + &data->hwmon_en); + case hwmon_in_max: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + val = clamp_val(val, 0, 2032); + data->msg->hwmon_alarm.vin_limit[channel].hl = in_to_reg(val); + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + case hwmon_in_min: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + val = clamp_val(val, 0, 2032); + data->msg->hwmon_alarm.vin_limit[channel].ll = in_to_reg(val); + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_temp_write(struct device *dev, u32 attr, int channel, + long val) +{ + struct nct6694_hwmon_data *data = dev_get_drvdata(dev); + unsigned char temp_hyst; + signed char temp_max; + int ret; + + guard(mutex)(&data->lock); + + switch (attr) { + case hwmon_temp_enable: + if (val == 0) + data->hwmon_en.tin_en[channel / 8] &= ~BIT(channel % 8); + else if (val == 1) + data->hwmon_en.tin_en[channel / 8] |= BIT(channel % 8); + else + return -EINVAL; + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_CONTROL, + sizeof(data->msg->hwmon_ctrl), + &data->hwmon_en); + case hwmon_temp_max: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + val = clamp_val(val, -127000, 127000); + data->msg->hwmon_alarm.tin_cfg[channel].hl = temp_to_reg(val); + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + case hwmon_temp_max_hyst: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + + val = clamp_val(val, -127000, 127000); + temp_max = data->msg->hwmon_alarm.tin_cfg[channel].hl; + temp_hyst = temp_max - temp_to_reg(val); + temp_hyst = clamp_val(temp_hyst, 0, 7); + data->msg->hwmon_alarm.tin_cfg[channel].hyst = + (data->msg->hwmon_alarm.tin_cfg[channel].hyst & ~NCT6694_TIN_HYST_MASK) | + FIELD_PREP(NCT6694_TIN_HYST_MASK, temp_hyst); + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_fan_write(struct device *dev, u32 attr, int channel, + long val) +{ + struct nct6694_hwmon_data *data = dev_get_drvdata(dev); + int ret; + + guard(mutex)(&data->lock); + + switch (attr) { + case hwmon_fan_enable: + if (val == 0) + data->hwmon_en.fin_en[channel / 8] &= ~BIT(channel % 8); + else if (val == 1) + data->hwmon_en.fin_en[channel / 8] |= BIT(channel % 8); + else + return -EINVAL; + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_CONTROL, + sizeof(data->msg->hwmon_ctrl), + &data->hwmon_en); + case hwmon_fan_min: + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + val = clamp_val(val, 1, 65535); + data->msg->hwmon_alarm.fin_ll[channel] = (u16)cpu_to_be16(val); + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_pwm_write(struct device *dev, u32 attr, int channel, + long val) +{ + struct nct6694_hwmon_data *data = dev_get_drvdata(dev); + int ret; + + guard(mutex)(&data->lock); + + switch (attr) { + case hwmon_pwm_enable: + if (val == 0) + data->hwmon_en.pwm_en[channel / 8] &= ~BIT(channel % 8); + else if (val == 1) + data->hwmon_en.pwm_en[channel / 8] |= BIT(channel % 8); + else + return -EINVAL; + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_CONTROL, + sizeof(data->msg->hwmon_ctrl), + &data->hwmon_en); + case hwmon_pwm_input: + if (val < 0 || val > 255) + return -EINVAL; + + ret = nct6694_read_msg(data->nct6694, NCT6694_PWM_MOD, + NCT6694_PWM_CONTROL, + sizeof(data->msg->pwm_ctrl), + &data->msg->pwm_ctrl); + if (ret) + return ret; + + data->msg->pwm_ctrl.mal_val[channel] = val; + + return nct6694_write_msg(data->nct6694, NCT6694_PWM_MOD, + NCT6694_PWM_CONTROL, + sizeof(data->msg->pwm_ctrl), + &data->msg->pwm_ctrl); + case hwmon_pwm_freq: + data->hwmon_en.pwm_freq[channel] = NCT6694_FREQ_TO_REG(val); + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_CONTROL, + sizeof(data->msg->hwmon_ctrl), + &data->hwmon_en); + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_read(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long *val) +{ + switch (type) { + case hwmon_in: + /* in mV */ + return nct6694_in_read(dev, attr, channel, val); + case hwmon_temp: + /* in mC */ + return nct6694_temp_read(dev, attr, channel, val); + case hwmon_fan: + /* in RPM */ + return nct6694_fan_read(dev, attr, channel, val); + case hwmon_pwm: + /* in value 0~255 */ + return nct6694_pwm_read(dev, attr, channel, val); + default: + return -EOPNOTSUPP; + } +} + +static int nct6694_write(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long val) +{ + switch (type) { + case hwmon_in: + return nct6694_in_write(dev, attr, channel, val); + case hwmon_temp: + return nct6694_temp_write(dev, attr, channel, val); + case hwmon_fan: + return nct6694_fan_write(dev, attr, channel, val); + case hwmon_pwm: + return nct6694_pwm_write(dev, attr, channel, val); + default: + return -EOPNOTSUPP; + } +} + +static umode_t nct6694_is_visible(const void *data, + enum hwmon_sensor_types type, + u32 attr, int channel) +{ + switch (type) { + case hwmon_in: + switch (attr) { + case hwmon_in_enable: + case hwmon_in_max: + case hwmon_in_min: + return 0644; + case hwmon_in_alarm: + case hwmon_in_input: + return 0444; + default: + return 0; + } + case hwmon_temp: + switch (attr) { + case hwmon_temp_enable: + case hwmon_temp_max: + case hwmon_temp_max_hyst: + return 0644; + case hwmon_temp_input: + case hwmon_temp_max_alarm: + return 0444; + default: + return 0; + } + case hwmon_fan: + switch (attr) { + case hwmon_fan_enable: + case hwmon_fan_min: + return 0644; + case hwmon_fan_input: + case hwmon_fan_min_alarm: + return 0444; + default: + return 0; + } + case hwmon_pwm: + switch (attr) { + case hwmon_pwm_enable: + case hwmon_pwm_freq: + case hwmon_pwm_input: + return 0644; + default: + return 0; + } + default: + return 0; + } +} + +static const struct hwmon_ops nct6694_hwmon_ops = { + .is_visible = nct6694_is_visible, + .read = nct6694_read, + .write = nct6694_write, +}; + +static const struct hwmon_chip_info nct6694_chip_info = { + .ops = &nct6694_hwmon_ops, + .info = nct6694_info, +}; + +static int nct6694_hwmon_init(struct nct6694_hwmon_data *data) +{ + int ret; + + /* + * Record each Hardware Monitor Channel enable status + * and PWM frequency register + */ + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_CONTROL, + sizeof(data->msg->hwmon_ctrl), + &data->hwmon_en); + if (ret) + return ret; + + /* Select hwmon device alarm mode */ + ret = nct6694_read_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); + if (ret) + return ret; + + data->msg->hwmon_alarm.smi_ctrl = NCT6694_HWMON_REALTIME_IRQ; + + return nct6694_write_msg(data->nct6694, NCT6694_HWMON_MOD, + NCT6694_HWMON_ALARM, + sizeof(data->msg->hwmon_alarm), + &data->msg->hwmon_alarm); +} + +static int nct6694_hwmon_probe(struct platform_device *pdev) +{ + struct nct6694_hwmon_data *data; + struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent); + struct device *hwmon_dev; + int ret; + + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + data->rpt = devm_kzalloc(&pdev->dev, sizeof(union nct6694_hwmon_rpt), + GFP_KERNEL); + if (!data->rpt) + return -ENOMEM; + + data->msg = devm_kzalloc(&pdev->dev, sizeof(union nct6694_hwmon_msg), + GFP_KERNEL); + if (!data->msg) + return -ENOMEM; + + data->nct6694 = nct6694; + devm_mutex_init(&pdev->dev, &data->lock); + + ret = nct6694_hwmon_init(data); + if (ret) + return ret; + + /* Register hwmon device to HWMON framework */ + hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, + "nct6694", data, + &nct6694_chip_info, + NULL); + return PTR_ERR_OR_ZERO(hwmon_dev); +} + +static struct platform_driver nct6694_hwmon_driver = { + .driver = { + .name = "nct6694-hwmon", + }, + .probe = nct6694_hwmon_probe, +}; + +module_platform_driver(nct6694_hwmon_driver); + +MODULE_DESCRIPTION("USB-HWMON driver for NCT6694"); +MODULE_AUTHOR("Ming Yu "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:nct6694-hwmon"); From patchwork Fri Dec 27 09:57:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Yu X-Patchwork-Id: 13921662 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (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 85F67158555; Fri, 27 Dec 2024 09:58:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293497; cv=none; b=MTSPAeULLP+zOrX08wOGqU0WQuhx/tVNpoCHGlctELxz/AchQll9MqvHqoIxQFdCqqqYyYdiQNhIwK6EpjzARjeu/oug1e+s3g9ZqJy8JQiqqhHuyA3xOvjxaGRNfgUPyom/HKUV/qOCe6SxJwY88K9NI+XwQIwvX2cyTtiNgg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735293497; c=relaxed/simple; bh=GvT4BWDw5+8Gee0TOROTKU9jyBZIvEwjeaGgrsdlZPw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UZcNHggfjY31zzL70YhqyfHzSVoYVjyxYH/8rB7BCK+7IJs2L94XlVze2FvUmkYwSF1VIAUpidkI73YoZT8HfBp6R6CQ+OHevEHDWp99CbwEWRcS9uujabpjYr5ND5XEaFwS2tEcJhoAboIy02xx8WcGb2TxxxT0AfpiTD7aXTw= 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=awHXfniy; arc=none smtp.client-ip=209.85.216.54 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="awHXfniy" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2ee786b3277so6376923a91.1; Fri, 27 Dec 2024 01:58:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735293495; x=1735898295; 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=WZTAeE6qWcwJUNnxTnxrs2KZnvYlBCv3e3fLLBfhd2k=; b=awHXfniyO8i1h6HeDt2bp/3ibV6MyTS4oCh7vE9Ubzbe6dqmmj9zsCwAgtlXMIZK5Z 16cHm8U9zGh97bh3SsBDfYQTCX4Quf2G6GYuMnu13Fz/ZzNJrqW2OHy0pqaUx5Crfacc fTZkr9UDOv7ooqvW5O+RBj8d5TFEkfeGmWOojgSATqb2l6mFAB+69QdrB2Zacotbt6CI ORIVOUdo0glcw0UZuthQrIAoPMbPSqrvOe8320fh4YEX5VkHBMMHYGQBVRTqBT7ON7+c 7klI7bfVF+P/CRh7LUIvvDNYNgVmkM9g/50JbOo4pD0Uws5fCG7cRPf7aGiYG5fd2m3M Kgiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735293495; x=1735898295; 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=WZTAeE6qWcwJUNnxTnxrs2KZnvYlBCv3e3fLLBfhd2k=; b=JX6EehRx209CZBJS/DbJLzD8xsYI+8DYFOBW2ZjnbYLf70GYHv3qQWzkUpvoMgyJRQ j6B7kYqji6p8NB/4zzfSafvAHjpiDETdihJfU1J1lqR2jslpWovgoPAO0nUJyscbonDL FwJ9RhOAfLWoXhsIy44dvKZkLN19/OgDbvLddIU1aM/UGXDPCVLHiL796yGGohhQ265l E3lsYyXCZwxy0w1iHaGDt3V3/juulv3eQ1qjjO2ocB8XyiJkNtmNy++y/vrG7ovfdtE/ bsMfeU5Y4Vja0C2Cnp9gzh8qZlOoqJ3s/O0xjdJ3llKs2gWzv7w75f56vVFVau3BiiOW 80Fg== X-Forwarded-Encrypted: i=1; AJvYcCV8whMfmN+PM/KZ/bT17PoVzJXkgNeXMH+eoDOerL4moJZ1mhfAaFyasJCOu0yWRkDIBJZLrEQawbjFrshwFxg=@vger.kernel.org, AJvYcCVM5Q4RGBPZizwJC1j64B+Fbhm5hu4dS048DGWf6lt8ujEUzZ/2crufpTNKEnNtsLnXlFzV1LSOv8dg@vger.kernel.org, AJvYcCX2+yASB1Xx8P8NT5/iC9EeB8bpu4qgxVkHp+coZeiBIwuua1jnyq2lgW5KxizxrJS83pOwhhBlkP0=@vger.kernel.org, AJvYcCXkl23GRHP3yPb0psEIrLoGVqbHOK1ZOOF6PfNPhaIYis/QO8SVm0flbWlrlU81ip6PY2rl6+1INF2HIA==@vger.kernel.org, AJvYcCXqASpjHp3d1XqfZm0lPlPiEmOvW4ukYh/28loLMSzRv9UtFHc0N2RywGYwH/kY6RuOHPu1m9qi@vger.kernel.org, AJvYcCXyKNbX+Mhl+8Tut3cKrl+rzVybPopSq7nyw4yvc8ghC7YkRcxS3S/ivhknYrGfWifzxgyPB12lIVG5@vger.kernel.org, AJvYcCXz2QgTk86ENQCMoJd5yp9VEaSlnVMU7EzOioIkOcEbHd5AcICFZ+Q2LZcjeakxSiEiLTiDge8Y/sEhtYI=@vger.kernel.org X-Gm-Message-State: AOJu0YwVXdN+tG/3Os2rfvCnLf9N+25qqnV69dC4LhVYxQxUv9yMISMs YGdJe6G0EBt6leeB+zRs/CiSk9dZ993q9ZY7RdHiyRzKgiVih3UG X-Gm-Gg: ASbGncvfzPqSJ9zTzlA225eL46Ou69ENteDgfspmqQgYcFJyxSSp8AwcUGGTO1FFAcl WwsvCYOzzlovEXbNdIGMlW7xRrXc+pcWotyUs9AV9XXaDrEn1Z+sjDPmvRaYgO1ylEahTGaEBCm uNDPhyRRJIlNPrFLGtX9jSKWKnulZHWL+SzuOmAZ3PuI0UL7PW3IZ4RbI0ljGYjOpHnJIwf7Jju Dalx95x/82yZj4BiyomNTjRj8/KAPwXePkaRzR0YoXIj3hiqS2M43lZkL17xHnotC97V5xI9EnF EtFESBPLE6U9KLX5c/Z2D3YO X-Google-Smtp-Source: AGHT+IE/BxicRXP/2yiiUOPaQVK7Kp9pRaplWsxdyrRMR51dmHOqk8mKeolwbBrgJg8OdwL1MJkQ+A== X-Received: by 2002:a17:90b:2c84:b0:2ee:bbe0:98c6 with SMTP id 98e67ed59e1d1-2f452dfcb28mr43073326a91.8.1735293494739; Fri, 27 Dec 2024 01:58:14 -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 d9443c01a7336-219dc9f51a0sm131581135ad.187.2024.12.27.01.58.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Dec 2024 01:58:14 -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, Ming Yu Subject: [PATCH v4 7/7] rtc: Add Nuvoton NCT6694 RTC support Date: Fri, 27 Dec 2024 17:57:27 +0800 Message-Id: <20241227095727.2401257-8-a0282524688@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241227095727.2401257-1-a0282524688@gmail.com> References: <20241227095727.2401257-1-a0282524688@gmail.com> Precedence: bulk X-Mailing-List: linux-hwmon@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 e5c65e382141..6a5164e42700 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16731,6 +16731,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 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..9465ab895c6d --- /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 + +/* + * 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 /* SEL|CMD */ +/* Command 01h - RTC Alarm */ +#define NCT6694_RTC_ALARM 0x0001 /* SEL|CMD */ +/* Command 02h - RTC Status */ +#define NCT6694_RTC_STATUS 0x0002 /* 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_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 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; + int ret; + + ret = nct6694_read_msg(data->nct6694, NCT6694_RTC_MOD, + NCT6694_RTC_TIME, + sizeof(*time), + 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; + + 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, NCT6694_RTC_MOD, + NCT6694_RTC_TIME, + sizeof(*time), + 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; + int ret; + + ret = nct6694_read_msg(data->nct6694, NCT6694_RTC_MOD, + NCT6694_RTC_ALARM, + sizeof(*alarm), + 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; + + 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, NCT6694_RTC_MOD, + NCT6694_RTC_ALARM, + sizeof(*alarm), + 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; + + 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, NCT6694_RTC_MOD, + NCT6694_RTC_STATUS, + sizeof(*sts), + 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; + 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, NCT6694_RTC_MOD, + NCT6694_RTC_STATUS, + sizeof(*sts), + 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");