Skip to contentSkip to conversation

Add Nuvoton NCT6694 MFD drivers

PATCH Superseded Ming Yu 6 participants 41 messages Feb 25, 2025 – Apr 21, 2025

Subsystems (11)
Networking Network drivers USB GPIO +7 more subsystems

v8 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Ming Yu

author
#0 lore raw

v8 cover Add Nuvoton NCT6694 MFD drivers

This patch series introduces support for Nuvoton NCT6694, a peripheral expander based on USB interface. It models the chip as an MFD driver (1/7), GPIO driver(2/7), I2C Adapter driver(3/7), CANfd driver(4/7), WDT driver(5/7), HWMON driver(6/7), and RTC driver(7/7).

The MFD driver implements USB device functionality to issue custom-define USB bulk pipe packets for NCT6694. 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.

The following introduces the custom-define USB transactions:
nct6694_read_msg - Send bulk-out pipe to write request packet
Receive bulk-in pipe to read response packet Receive bulk-in pipe to read data packet

nct6694_write_msg - Send bulk-out pipe to write request packet
Send bulk-out pipe to write data packet Receive bulk-in pipe to read response packet Receive bulk-in pipe to read data packet

Changes since version 7:
- Add error handling for devm_mutex_init()
- Modify the name of the child devices CAN1 and CAN2 to CAN0 and CAN1.
- Fix multiline comments to net-dev style in nct6694_canfd.c

Changes since version 6:
- Fix nct6694_can_handle_state_change() in nct6694_canfd.c
- Fix warnings in nct6694_canfd.c
- Move the nct6694_can_priv's bec to the end in nct6694_canfd.c
- Fix warning in nct6694_wdt.c
- Fix temp_hyst's data type to signed variable in nct6694-hwmon.c

Changes since version 5:
- Modify the module name and the driver name consistently
- Fix mfd_cell to MFD_CELL_NAME() and MFD_CELL_BASIC()
- Drop unnecessary macros in nct6694.c
- Update private data and drop mutex in nct6694_canfd.c
- Fix nct6694_can_handle_state_change() in nct6694_canfd.c

Changes since version 4:
- Modify arguments in read/write function to a pointer to cmd_header
- Modify all callers that call the read/write function
- Move the nct6694_canfd.c to drivers/net/can/usb/
- Fix the missing rx offload function in nct6694_canfd.c
- Fix warngings in nct6694-hwmon.c

Changes since version 3:
- Modify array buffer to structure for each drivers
- Fix defines and comments for each drivers
- Add header <linux/bits.h> and use BIT macro in nct6694.c and gpio-nct6694.c
- Modify mutex_init() to devm_mutex_init()
- Add rx-offload helper in nct6694_canfd.c
- Drop watchdog_init_timeout() in nct6694_wdt.c
- Modify the division method to DIV_ROUND_CLOSEST() in nct6694-hwmon.c
- Drop private mutex and use rtc core lock in rtc-nct6694.c
- Modify device_set_wakeup_capable() to device_init_wakeup() in rtc-nct6694.c

Changes since version 2:
- Add MODULE_ALIAS() for each child driver
- Modify gpio line names be a local variable in gpio-nct6694.c
- Drop unnecessary platform_get_drvdata() in gpio-nct6694.c
- Rename each command in nct6694_canfd.c
- Modify each function name consistently in nct6694_canfd.c
- Modify the pretimeout validation procedure in nct6694_wdt.c
- Fix warnings in nct6694-hwmon.c

Changes since version 1:
- Implement IRQ domain to handle IRQ demux in nct6694.c
- Modify USB_DEVICE to USB_DEVICE_AND_INTERFACE_INFO API in nct6694.c
- Add each driver's command structure
- Fix USB functions in nct6694.c
- Fix platform driver registration in each child driver
- Sort each driver's header files alphabetically
- Drop unnecessary header in gpio-nct6694.c
- Add gpio line names in gpio-nct6694.c
- Fix errors and warnings in nct6694_canfd.c
- Fix TX-flow control in nct6694_canfd.c
- Fix warnings in nct6694_wdt.c
- Drop unnecessary logs in nct6694_wdt.c
- Modify start() function to setup device in nct6694_wdt.c
- Add voltage sensors functionality in nct6694-hwmon.c
- Add temperature sensors functionality in nct6694-hwmon.c
- Fix overwrite error return values in nct6694-hwmon.c
- Add write value limitation for each write() function in nct6694-hwmon.c
- Drop unnecessary logs in rtc-nct6694.c
- Fix overwrite error return values in rtc-nct6694.c
- Modify to use dev_err_probe API in rtc-nct6694.c

Ming Yu (7):
mfd: Add core driver for Nuvoton NCT6694
gpio: Add Nuvoton NCT6694 GPIO support
i2c: Add Nuvoton NCT6694 I2C support
can: Add Nuvoton NCT6694 CANFD support
watchdog: Add Nuvoton NCT6694 WDT support
hwmon: Add Nuvoton NCT6694 HWMON support
rtc: Add Nuvoton NCT6694 RTC support

 MAINTAINERS                         |  13 +
 drivers/gpio/Kconfig                |  12 +
 drivers/gpio/Makefile               |   1 +
 drivers/gpio/gpio-nct6694.c         | 463 ++++++++++++++
 drivers/hwmon/Kconfig               |  10 +
 drivers/hwmon/Makefile              |   1 +
 drivers/hwmon/nct6694-hwmon.c       | 949 ++++++++++++++++++++++++++++
 drivers/i2c/busses/Kconfig          |  10 +
 drivers/i2c/busses/Makefile         |   1 +
 drivers/i2c/busses/i2c-nct6694.c    | 152 +++++
 drivers/mfd/Kconfig                 |  18 +
 drivers/mfd/Makefile                |   2 +
 drivers/mfd/nct6694.c               | 378 +++++++++++
 drivers/net/can/usb/Kconfig         |  11 +
 drivers/net/can/usb/Makefile        |   1 +
 drivers/net/can/usb/nct6694_canfd.c | 799 +++++++++++++++++++++++
 drivers/rtc/Kconfig                 |  10 +
 drivers/rtc/Makefile                |   1 +
 drivers/rtc/rtc-nct6694.c           | 286 +++++++++
 drivers/watchdog/Kconfig            |  11 +
 drivers/watchdog/Makefile           |   1 +
 drivers/watchdog/nct6694_wdt.c      | 298 +++++++++
 include/linux/mfd/nct6694.h         | 102 +++
 23 files changed, 3530 insertions(+)
 create mode 100644 drivers/gpio/gpio-nct6694.c
 create mode 100644 drivers/hwmon/nct6694-hwmon.c
 create mode 100644 drivers/i2c/busses/i2c-nct6694.c
 create mode 100644 drivers/mfd/nct6694.c
 create mode 100644 drivers/net/can/usb/nct6694_canfd.c
 create mode 100644 drivers/rtc/rtc-nct6694.c
 create mode 100644 drivers/watchdog/nct6694_wdt.c
 create mode 100644 include/linux/mfd/nct6694.h
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v8 1/7 mfd: Add core driver for Nuvoton NCT6694

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.

---
 MAINTAINERS                 |   7 +
 drivers/mfd/Kconfig         |  18 ++
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/nct6694.c       | 378 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/nct6694.h | 102 ++++++++++
 5 files changed, 507 insertions(+)
 create mode 100644 drivers/mfd/nct6694.c
 create mode 100644 include/linux/mfd/nct6694.h
MAINTAINERS +7 -0
index 873aa2cce4d7..c700a0b96960 100644
@@ -16918,6 +16918,13 @@ F:	drivers/nubus/
 F:	include/linux/nubus.h
 F:	include/uapi/linux/nubus.h
 
+NUVOTON NCT6694 MFD DRIVER
+M:	Ming Yu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/mfd/nct6694.c
+F:	include/linux/mfd/nct6694.h
+
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
 M:	Antonino Daplas <[email protected]>
 L:	[email protected]
drivers/mfd/Kconfig +18 -0
index 6b0682af6e32..c97a2bdcea0b 100644
@@ -1045,6 +1045,24 @@ config MFD_MENF21BMC
 	  This driver can also be built as a module. If so the module
 	  will be called menf21bmc.
 
+config MFD_NCT6694
+	tristate "Nuvoton NCT6694 support"
+	select MFD_CORE
+	depends on USB
+	help
+	  This enables support for the Nuvoton USB device NCT6694, which shares
+	  peripherals.
+
+	  This driver provides core APIs to access the NCT6694 hardware
+	  monitoring and control features.
+
+	  The NCT6694 is a versatile multi-function device that supports
+	  functionalities such as GPIO, I2C, CAN, WDT, HWMON, and RTC
+	  management.
+
+	  Additional drivers must be enabled to utilize the specific
+	  functionalities of the device.
+
 config MFD_OCELOT
 	tristate "Microsemi Ocelot External Control Support"
 	depends on SPI_MASTER
drivers/mfd/Makefile +2 -0
index 9220eaf7cf12..7725b732e265 100644
@@ -121,6 +121,8 @@ obj-$(CONFIG_MFD_MC13XXX)	+= mc13xxx-core.o
 obj-$(CONFIG_MFD_MC13XXX_SPI)	+= mc13xxx-spi.o
 obj-$(CONFIG_MFD_MC13XXX_I2C)	+= mc13xxx-i2c.o
 
+obj-$(CONFIG_MFD_NCT6694)	+= nct6694.o
+
 obj-$(CONFIG_MFD_CORE)		+= mfd-core.o
 
 ocelot-soc-objs			:= ocelot-core.o ocelot-spi.o
drivers/mfd/nct6694.c new file +378 -0

378 lines not on this page · raw message

include/linux/mfd/nct6694.h new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

On 25.02.2025 16:16:38, Ming Yu wrote:

[...]

Quoted 31 lines
drivers/mfd/nct6694.c new file +31 -0
+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 = &iface->dev;
+	struct nct6694 *nct6694;
+	int pipe, maxp;
+	int ret;
+
+	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,

drivers/mfd/nct6694.c line 312

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+						&nct6694_irq_domain_ops,
+						nct6694);
+	if (!nct6694->domain) {
+		ret = -ENODEV;
+		goto err_urb;
+	}
+
+	nct6694->dev = dev;
+	nct6694->udev = udev;
+	nct6694->timeout = NCT6694_URB_TIMEOUT;	/* Wait until URB completes */

Why do you need this variable? You can directly use NCT6694_URB_TIMEOUT in the usb_bulk_msg() and friends calls.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#3 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

Dear Marc,

Thank you for reviewing,

Marc Kleine-Budde <[email protected]> 於 2025年2月28日 週五 下午4:52寫道:

Quoted 37 lines
Quoted 37 lines
drivers/mfd/nct6694.c new file +36 -0
+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 = &iface->dev;
+     struct nct6694 *nct6694;
+     int pipe, maxp;
+     int ret;
+
+     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;
Quoted 9 lines

drivers/mfd/nct6694.c line 312

Quoted 5 lines
drivers/mfd/nct6694.c new file +5 -0
+     }
+
+     nct6694->dev = dev;
+     nct6694->udev = udev;
+     nct6694->timeout = NCT6694_URB_TIMEOUT; /* Wait until URB completes */

Why do you need this variable? You can directly use NCT6694_URB_TIMEOUT in the usb_bulk_msg() and friends calls.

Okay, I will make the modifications in the next patch.

Best regards,
Ming

maintainer
#4 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

In reply to Ming Yu
Quoted 3 lines · Ming Yu

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 needs to go into the Kconfig help passage.

Quoted 2 lines

This driver implements USB device functionality and shares the chip's peripherals as a child device.

This driver doesn't implement USB functionality.

Quoted 6 lines

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.

Why aren't you signing off with your work address?

Quoted 15 lines
---
 MAINTAINERS                 |   7 +
 drivers/mfd/Kconfig         |  18 ++
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/nct6694.c       | 378 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/nct6694.h | 102 ++++++++++
 5 files changed, 507 insertions(+)
 create mode 100644 drivers/mfd/nct6694.c
 create mode 100644 include/linux/mfd/nct6694.h
MAINTAINERS +0 -0
index 873aa2cce4d7..c700a0b96960 100644

MAINTAINERS F: drivers/nubus/ · line 16923

Quoted 6 lines
MAINTAINERS +3 -0
@@ -16918,6 +16918,13 @@ F:	drivers/nubus/
 F:	include/linux/nubus.h
 F:	include/uapi/linux/nubus.h
 
+NUVOTON NCT6694 MFD DRIVER
+M:	Ming Yu <[email protected]>
+L:	[email protected]

This is the default list. You shouldn't need to add that here.

Quoted 17 lines
MAINTAINERS +4 -0
+S:	Supported
+F:	drivers/mfd/nct6694.c
+F:	include/linux/mfd/nct6694.h
+
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
 M:	Antonino Daplas <[email protected]>
 L:	[email protected]
drivers/mfd/Kconfig +2 -0
index 6b0682af6e32..c97a2bdcea0b 100644
@@ -1045,6 +1045,24 @@ config MFD_MENF21BMC
 	  This driver can also be built as a module. If so the module
 	  will be called menf21bmc.
 
+config MFD_NCT6694
+	tristate "Nuvoton NCT6694 support"

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1059

Quoted 10 lines
drivers/mfd/Kconfig +10 -0
+	select MFD_CORE
+	depends on USB
+	help
+	  This enables support for the Nuvoton USB device NCT6694, which shares
+	  peripherals.
+
+	  This driver provides core APIs to access the NCT6694 hardware
+	  monitoring and control features.
+
+	  The NCT6694 is a versatile multi-function device that supports

Please drop the term multi-function device and replace it what a proper description of the devices.

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1061

Quoted 2 lines
drivers/mfd/Kconfig +2 -0
+	  functionalities such as GPIO, I2C, CAN, WDT, HWMON, and RTC
+	  management.

All of these line breaks should be removed.

Quoted 25 lines
drivers/mfd/Kconfig +3 -0
+	  Additional drivers must be enabled to utilize the specific
+	  functionalities of the device.
+
 config MFD_OCELOT
 	tristate "Microsemi Ocelot External Control Support"
 	depends on SPI_MASTER
drivers/mfd/Makefile +2 -0
index 9220eaf7cf12..7725b732e265 100644
@@ -121,6 +121,8 @@ obj-$(CONFIG_MFD_MC13XXX)	+= mc13xxx-core.o
 obj-$(CONFIG_MFD_MC13XXX_SPI)	+= mc13xxx-spi.o
 obj-$(CONFIG_MFD_MC13XXX_I2C)	+= mc13xxx-i2c.o
 
+obj-$(CONFIG_MFD_NCT6694)	+= nct6694.o
+
 obj-$(CONFIG_MFD_CORE)		+= mfd-core.o
 
 ocelot-soc-objs			:= ocelot-core.o ocelot-spi.o
drivers/mfd/nct6694.c new file +0 -0
new file mode 100644
index 000000000000..c82457679ca6

drivers/mfd/nct6694.c line 6

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
@@ -0,0 +1,378 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 core driver using USB interface to provide
+ * access to the NCT6694 hardware monitoring and control features.
+ *
+ * The NCT6694 is a versatile multi-function device that supports

Here too.

drivers/mfd/nct6694.c line 10

Quoted 4 lines
drivers/mfd/nct6694.c new file +4 -0
+ * functionalities such as GPIO, I2C, CAN, WDT, HWMON and RTC
+ * management.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.

This goes at the top.

Quoted 5 lines
drivers/mfd/nct6694.c new file +5 -0
+ */
+
+#include <linux/bits.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>

drivers/mfd/nct6694.c line 25

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+#include <linux/irqdomain.h>
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+
+static const struct mfd_cell nct6694_dev[] = {
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x0),

"-gpio" usually goes on the end.

drivers/mfd/nct6694.c line 26

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x1),

IDs are usually given in base-10.

Why are you manually adding the device IDs?

PLATFORM_DEVID_AUTO doesn't work for you?

Quoted 13 lines
drivers/mfd/nct6694.c new file +13 -0
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x2),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x3),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x4),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x5),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x6),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x7),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x8),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x9),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xA),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xB),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xC),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xD),
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xE),

drivers/mfd/nct6694.c line 49

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+	MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xF),
+
+	MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x0),
+	MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x1),
+	MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x2),
+	MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x3),
+	MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x4),
+	MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x5),
+
+	MFD_CELL_BASIC("nct6694_canfd", NULL, NULL, 0, 0x0),

Why has the naming convention changed here?

drivers/mfd/nct6694.c line 56

Quoted 7 lines
drivers/mfd/nct6694.c new file +7 -0
+	MFD_CELL_BASIC("nct6694_canfd", NULL, NULL, 0, 0x1),
+
+	MFD_CELL_BASIC("nct6694_wdt", NULL, NULL, 0, 0x0),
+	MFD_CELL_BASIC("nct6694_wdt", NULL, NULL, 0, 0x1),
+
+	MFD_CELL_NAME("nct6694-hwmon"),
+	MFD_CELL_NAME("rtc-nct6694"),

There doesn't seem to be any consistency here.

drivers/mfd/nct6694.c line 64

Quoted 8 lines
drivers/mfd/nct6694.c new file +7 -0
+};
+
+static int nct6694_response_err_handling(struct nct6694 *nct6694,
 
+{
+	switch (err_status) {
+	case NCT6694_NO_ERROR:
+		return err_status;

This is odd since you already know this will be 0.

drivers/mfd/nct6694.c line 66

Quoted 2 lines
drivers/mfd/nct6694.c new file +2 -0
+	case NCT6694_NOT_SUPPORT_ERROR:
+		dev_warn(nct6694->dev, "Command is not supported!\n");

Why not dev_err()?

drivers/mfd/nct6694.c line 75

Quoted 9 lines
drivers/mfd/nct6694.c new file +9 -0
+		break;
+	case NCT6694_NO_RESPONSE_ERROR:
+		dev_warn(nct6694->dev, "Command received no response!\n");
+		break;
+	case NCT6694_TIMEOUT_ERROR:
+		dev_warn(nct6694->dev, "Command timed out!\n");
+		break;
+	case NCT6694_PENDING:
+		dev_warn(nct6694->dev, "Command is pending!\n");

Is this an error?

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return -EIO;

drivers/mfd/nct6694.c line 91

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+}
+
+int nct6694_read_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *cmd_hd, void *buf)
+{
+	union nct6694_usb_msg *msg = nct6694->usb_msg;
+	int tx_len, rx_len, ret;
+
+	guard(mutex)(&nct6694->access_lock);
+
+	/* Send command packet to USB device */

This doesn't really describe the next 2 lines.

Move it down?

drivers/mfd/nct6694.c line 95

Quoted 4 lines
drivers/mfd/nct6694.c new file +4 -0
+	memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd));
+	msg->cmd_header.hctrl = NCT6694_HCTRL_GET;
+
+	ret = usb_bulk_msg(nct6694->udev,

Since you use nct6694->udev a bunch - sometimes twice in the same call, it might be nicer to pull it into it's own variable instead of dereferencing it all the time.

drivers/mfd/nct6694.c line 105

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+			   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,

How can you read sizeof(*msg) Bytes (22?) into the smaller response_header (16?) attribute?

Quoted 86 lines
drivers/mfd/nct6694.c new file +86 -0
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	/* Receive data packet from USB device */
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+			   buf, le16_to_cpu(cmd_hd->len), &rx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	if (rx_len != le16_to_cpu(cmd_hd->len)) {
+		dev_err(nct6694->dev, "Expected received length %d, but got %d\n",
+			le16_to_cpu(cmd_hd->len), rx_len);
+		return -EIO;
+	}
+
+	return nct6694_response_err_handling(nct6694, msg->response_header.sts);
+}
+EXPORT_SYMBOL(nct6694_read_msg);
+
+int nct6694_write_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *cmd_hd, void *buf)
+{
+	union nct6694_usb_msg *msg = nct6694->usb_msg;
+	int tx_len, rx_len, ret;
+
+	guard(mutex)(&nct6694->access_lock);
+
+	/* Send command packet to USB device */
+	memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd));
+	msg->cmd_header.hctrl = NCT6694_HCTRL_SET;
+
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+			   &msg->cmd_header, sizeof(*msg), &tx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	/* Send data packet to USB device */
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+			   buf, le16_to_cpu(cmd_hd->len), &tx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	/* Receive response packet from USB device */
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+			   &msg->response_header, sizeof(*msg), &rx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	/* Receive data packet from USB device */
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+			   buf, le16_to_cpu(cmd_hd->len), &rx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	if (rx_len != le16_to_cpu(cmd_hd->len)) {
+		dev_err(nct6694->dev, "Expected transmitted length %d, but got %d\n",
+			le16_to_cpu(cmd_hd->len), rx_len);
+		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;
+	unsigned int *int_status = urb->transfer_buffer;
+	int ret;
+
+	switch (urb->status) {
+	case 0:
+		break;
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:

drivers/mfd/nct6694.c line 207

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+		return;
+	default:
+		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(nct6694->dev, "%s: Failed to resubmit urb, status %pe",

Why debug?

drivers/mfd/nct6694.c line 208

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+			__func__, ERR_PTR(ret));

Remove the __func__ part.

Quoted 94 lines
drivers/mfd/nct6694.c new file +94 -0
+}
+
+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 const 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 = &iface->dev;
+	struct nct6694 *nct6694;
+	int pipe, maxp;
+	int ret;
+
+	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,

drivers/mfd/nct6694.c line 312

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+						&nct6694_irq_domain_ops,
+						nct6694);
+	if (!nct6694->domain) {
+		ret = -ENODEV;
+		goto err_urb;
+	}
+
+	nct6694->dev = dev;
+	nct6694->udev = udev;
+	nct6694->timeout = NCT6694_URB_TIMEOUT;	/* Wait until URB completes */

No need to save this known value.

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+	ret = devm_mutex_init(dev, &nct6694->access_lock);
+	if (ret)
+		goto err_urb;
+
+	ret = devm_mutex_init(dev, &nct6694->irq_lock);
+	if (ret)
+		goto err_urb;
+
+	interface = iface->cur_altsetting;
+	int_endpoint = &interface->endpoint[0].desc;

drivers/mfd/nct6694.c line 333

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+	if (!usb_endpoint_is_int_in(int_endpoint)) {
+		ret = -ENODEV;
+		goto err_urb;
+	}
+	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;

Please unsquash these calls - space them out.

Quoted 19 lines
drivers/mfd/nct6694.c new file +19 -0
+
+	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 nct6694 *nct6694 = usb_get_intfdata(iface);

drivers/mfd/nct6694.c line 362

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+
+	mfd_remove_devices(nct6694->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)},

This should fit on one line. You can use up to 100-chars.

drivers/mfd/nct6694.c line 368

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+	{}
+};
+MODULE_DEVICE_TABLE(usb, nct6694_ids);
+
+static struct usb_driver nct6694_usb_driver = {
+	.name	= "nct6694",

Odd spaces.

drivers/mfd/nct6694.c line 373

Quoted 5 lines
drivers/mfd/nct6694.c new file +5 -0
+	.id_table = nct6694_ids,
+	.probe = nct6694_usb_probe,
+	.disconnect = nct6694_usb_disconnect,
+};
+

Remove this line.

drivers/mfd/nct6694.c line 376

Quoted 3 lines
drivers/mfd/nct6694.c new file +3 -0
+module_usb_driver(nct6694_usb_driver);
+
+MODULE_DESCRIPTION("USB core driver for NCT6694");

This is not a USB driver.

Quoted 1 line

+MODULE_AUTHOR("Ming Yu <[email protected]>");

Different to SoB.

include/linux/mfd/nct6694.h line 5

Quoted 12 lines
drivers/mfd/nct6694.c new file +1 -0
+MODULE_LICENSE("GPL");
include/linux/mfd/nct6694.h new file +5 -0
new file mode 100644
index 000000000000..8171f975761e
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Nuvoton NCT6694 USB transaction and data structure.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.

At the top.

Quoted 81 lines
include/linux/mfd/nct6694.h new file +81 -0
+ */
+
+#ifndef __MFD_NCT6694_H
+#define __MFD_NCT6694_H
+
+#define NCT6694_VENDOR_ID	0x0416
+#define NCT6694_PRODUCT_ID	0x200B
+#define NCT6694_INT_IN_EP	0x81
+#define NCT6694_BULK_IN_EP	0x02
+#define NCT6694_BULK_OUT_EP	0x03
+
+#define NCT6694_HCTRL_SET	0x40
+#define NCT6694_HCTRL_GET	0x80
+
+#define NCT6694_URB_TIMEOUT	1000
+
+enum nct6694_irq_id {
+	NCT6694_IRQ_GPIO0 = 0,
+	NCT6694_IRQ_GPIO1,
+	NCT6694_IRQ_GPIO2,
+	NCT6694_IRQ_GPIO3,
+	NCT6694_IRQ_GPIO4,
+	NCT6694_IRQ_GPIO5,
+	NCT6694_IRQ_GPIO6,
+	NCT6694_IRQ_GPIO7,
+	NCT6694_IRQ_GPIO8,
+	NCT6694_IRQ_GPIO9,
+	NCT6694_IRQ_GPIOA,
+	NCT6694_IRQ_GPIOB,
+	NCT6694_IRQ_GPIOC,
+	NCT6694_IRQ_GPIOD,
+	NCT6694_IRQ_GPIOE,
+	NCT6694_IRQ_GPIOF,
+	NCT6694_IRQ_CAN0,
+	NCT6694_IRQ_CAN1,
+	NCT6694_IRQ_RTC,
+	NCT6694_NR_IRQS,
+};
+
+enum nct6694_response_err_status {
+	NCT6694_NO_ERROR = 0,
+	NCT6694_FORMAT_ERROR,
+	NCT6694_RESERVED1,
+	NCT6694_RESERVED2,
+	NCT6694_NOT_SUPPORT_ERROR,
+	NCT6694_NO_RESPONSE_ERROR,
+	NCT6694_TIMEOUT_ERROR,
+	NCT6694_PENDING,
+};
+
+struct __packed nct6694_cmd_header {
+	u8 rsv1;
+	u8 mod;
+	union __packed {
+		__le16 offset;
+		struct __packed {
+			u8 cmd;
+			u8 sel;
+		};
+	};
+	u8 hctrl;
+	u8 rsv2;
+	__le16 len;
+};
+
+struct __packed nct6694_response_header {
+	u8 sequence_id;
+	u8 sts;
+	u8 reserved[4];
+	__le16 len;
+};
+
+union __packed nct6694_usb_msg {
+	struct nct6694_cmd_header cmd_header;
+	struct nct6694_response_header response_header;
+};
+
+struct nct6694 {
+	struct device *dev;
+	struct irq_domain *domain;
+	/* Mutex to protect access to the device */

include/linux/mfd/nct6694.h line 96

Quoted 10 lines
include/linux/mfd/nct6694.h new file +10 -0
+	struct mutex access_lock;
+	/* Mutex to protect access to the IRQ */
+	struct mutex irq_lock;
+	struct urb *int_in_urb;
+	struct usb_device *udev;
+	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;

timeout_ms

Quoted 9 lines
include/linux/mfd/nct6694.h new file +6 -0
+};
+
+int nct6694_read_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *cmd_hd, void *buf);
+int nct6694_write_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *cmd_hd, void *buf);
+
+#endif
Signature · 1 line
2.34.1
Signature · 1 line
Lee Jones [李琼斯]
author
#5 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

Dear Lee,

Thank you for reviewing,

Lee Jones <[email protected]> 於 2025年3月7日 週五 上午9:15寫道:

Quoted 9 lines · Ming Yu
Quoted 4 lines

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 needs to go into the Kconfig help passage.

Okay, I will move these to Kconfig in the next patch.

Quoted 5 lines
Quoted 2 lines

This driver implements USB device functionality and shares the chip's peripherals as a child device.

This driver doesn't implement USB functionality.

Fix it in v9.

Quoted 9 lines
Quoted 6 lines

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.

Why aren't you signing off with your work address?

Fix it in v9.

Quoted 18 lines
Quoted 18 lines
---
 MAINTAINERS                 |   7 +
 drivers/mfd/Kconfig         |  18 ++
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/nct6694.c       | 378 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/nct6694.h | 102 ++++++++++
 5 files changed, 507 insertions(+)
 create mode 100644 drivers/mfd/nct6694.c
 create mode 100644 include/linux/mfd/nct6694.h
MAINTAINERS +0 -0
index 873aa2cce4d7..c700a0b96960 100644
@@ -16918,6 +16918,13 @@ F:   drivers/nubus/
 F:   include/linux/nubus.h
 F:   include/uapi/linux/nubus.h
 
Quoted 6 lines

MAINTAINERS F: drivers/nubus/ · line 16923

Quoted 3 lines
MAINTAINERS +3 -0
+NUVOTON NCT6694 MFD DRIVER
+M:   Ming Yu <[email protected]>
+L:   [email protected]

This is the default list. You shouldn't need to add that here.

Remove it in v9.

Quoted 15 lines
Quoted 15 lines
MAINTAINERS +4 -0
+S:   Supported
+F:   drivers/mfd/nct6694.c
+F:   include/linux/mfd/nct6694.h
+
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
 M:   Antonino Daplas <[email protected]>
 L:   [email protected]
drivers/mfd/Kconfig +0 -0
index 6b0682af6e32..c97a2bdcea0b 100644
@@ -1045,6 +1045,24 @@ config MFD_MENF21BMC
        This driver can also be built as a module. If so the module
        will be called menf21bmc.
 
Quoted 16 lines

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1059

Quoted 12 lines
drivers/mfd/Kconfig +12 -0
+config MFD_NCT6694
+     tristate "Nuvoton NCT6694 support"
+     select MFD_CORE
+     depends on USB
+     help
+       This enables support for the Nuvoton USB device NCT6694, which shares
+       peripherals.
+
+       This driver provides core APIs to access the NCT6694 hardware
+       monitoring and control features.
+
+       The NCT6694 is a versatile multi-function device that supports

Please drop the term multi-function device and replace it what a proper description of the devices.

Fix it in v9.

Quoted 5 lines

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1061

Quoted 2 lines
drivers/mfd/Kconfig +2 -0
+       functionalities such as GPIO, I2C, CAN, WDT, HWMON, and RTC
+       management.

All of these line breaks should be removed.

Fix these in v9.

Quoted 18 lines
Quoted 18 lines
drivers/mfd/Kconfig +3 -0
+       Additional drivers must be enabled to utilize the specific
+       functionalities of the device.
+
 config MFD_OCELOT
      tristate "Microsemi Ocelot External Control Support"
      depends on SPI_MASTER
drivers/mfd/Makefile +2 -0
index 9220eaf7cf12..7725b732e265 100644
@@ -121,6 +121,8 @@ obj-$(CONFIG_MFD_MC13XXX) += mc13xxx-core.o
 obj-$(CONFIG_MFD_MC13XXX_SPI)        += mc13xxx-spi.o
 obj-$(CONFIG_MFD_MC13XXX_I2C)        += mc13xxx-i2c.o
 
+obj-$(CONFIG_MFD_NCT6694)    += nct6694.o
+
 obj-$(CONFIG_MFD_CORE)               += mfd-core.o
 
Quoted 16 lines

drivers/mfd/nct6694.c line 6

Quoted 13 lines
drivers/mfd/Makefile +0 -0
 ocelot-soc-objs                      := ocelot-core.o ocelot-spi.o
drivers/mfd/nct6694.c new file +6 -0
new file mode 100644
index 000000000000..c82457679ca6
@@ -0,0 +1,378 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 core driver using USB interface to provide
+ * access to the NCT6694 hardware monitoring and control features.
+ *
+ * The NCT6694 is a versatile multi-function device that supports

Here too.

Fix it in v9.

Quoted 7 lines

drivers/mfd/nct6694.c line 10

Quoted 4 lines
drivers/mfd/nct6694.c new file +4 -0
+ * functionalities such as GPIO, I2C, CAN, WDT, HWMON and RTC
+ * management.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.

This goes at the top.

Fix it in v9.

Quoted 9 lines
Quoted 9 lines
drivers/mfd/nct6694.c new file +9 -0
+ */
+
+#include <linux/bits.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irqdomain.h>
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
Quoted 9 lines

drivers/mfd/nct6694.c line 25

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+
+static const struct mfd_cell nct6694_dev[] = {
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x0),

"-gpio" usually goes on the end.

Fix it in v9.

Quoted 4 lines

drivers/mfd/nct6694.c line 26

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x1),

IDs are usually given in base-10.

Fix it in v9.

Quoted 4 lines

Why are you manually adding the device IDs?

PLATFORM_DEVID_AUTO doesn't work for you?

I need to manage these IDs to ensure that child devices can be properly utilized within their respective modules.

Quoted 17 lines
Quoted 17 lines
drivers/mfd/nct6694.c new file +17 -0
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x2),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x3),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x4),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x5),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x6),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x7),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x8),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x9),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xA),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xB),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xC),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xD),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xE),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xF),
+
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x0),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x1),
Quoted 9 lines

drivers/mfd/nct6694.c line 49

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x2),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x3),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x4),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x5),
+
+     MFD_CELL_BASIC("nct6694_canfd", NULL, NULL, 0, 0x0),

Why has the naming convention changed here?

I originally expected the child devices name to directly match its driver name. Do you think it would be better to standardize the naming as "nct6694-xxx" ?

Quoted 10 lines

drivers/mfd/nct6694.c line 56

Quoted 7 lines
drivers/mfd/nct6694.c new file +7 -0
+     MFD_CELL_BASIC("nct6694_canfd", NULL, NULL, 0, 0x1),
+
+     MFD_CELL_BASIC("nct6694_wdt", NULL, NULL, 0, 0x0),
+     MFD_CELL_BASIC("nct6694_wdt", NULL, NULL, 0, 0x1),
+
+     MFD_CELL_NAME("nct6694-hwmon"),
+     MFD_CELL_NAME("rtc-nct6694"),

There doesn't seem to be any consistency here.

Do you think these two should be changed to use MFD_CELL_BASIC()?

Quoted 11 lines

drivers/mfd/nct6694.c line 64

Quoted 8 lines
drivers/mfd/nct6694.c new file +7 -0
+};
+
+static int nct6694_response_err_handling(struct nct6694 *nct6694,
 
+{
+     switch (err_status) {
+     case NCT6694_NO_ERROR:
+             return err_status;

This is odd since you already know this will be 0.

Fix it in v9.

Quoted 5 lines

drivers/mfd/nct6694.c line 66

Quoted 2 lines
drivers/mfd/nct6694.c new file +2 -0
+     case NCT6694_NOT_SUPPORT_ERROR:
+             dev_warn(nct6694->dev, "Command is not supported!\n");

Why not dev_err()?

Fix it in v9.

Quoted 10 lines

drivers/mfd/nct6694.c line 75

Quoted 9 lines
drivers/mfd/nct6694.c new file +9 -0
+             break;
+     case NCT6694_NO_RESPONSE_ERROR:
+             dev_warn(nct6694->dev, "Command received no response!\n");
+             break;
+     case NCT6694_TIMEOUT_ERROR:
+             dev_warn(nct6694->dev, "Command timed out!\n");
+             break;
+     case NCT6694_PENDING:
+             dev_warn(nct6694->dev, "Command is pending!\n");
Quoted 2 lines

Is this an error?

Yes, I will fix it to dev_err() in the next patch.

Quoted 19 lines
Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return -EIO;

drivers/mfd/nct6694.c line 91

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+}
+
+int nct6694_read_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *cmd_hd, void *buf)
+{
+     union nct6694_usb_msg *msg = nct6694->usb_msg;
+     int tx_len, rx_len, ret;
+
+     guard(mutex)(&nct6694->access_lock);
+
+     /* Send command packet to USB device */

This doesn't really describe the next 2 lines.

Quoted 2 lines

Move it down?

Fix it in v9.

Quoted 9 lines

drivers/mfd/nct6694.c line 95

Quoted 4 lines
drivers/mfd/nct6694.c new file +4 -0
+     memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd));
+     msg->cmd_header.hctrl = NCT6694_HCTRL_GET;
+
+     ret = usb_bulk_msg(nct6694->udev,

Since you use nct6694->udev a bunch - sometimes twice in the same call, it might be nicer to pull it into it's own variable instead of dereferencing it all the time.

Fix it in v9.

Quoted 11 lines

drivers/mfd/nct6694.c line 105

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+                        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,
Quoted 3 lines

How can you read sizeof(*msg) Bytes (22?) into the smaller response_header (16?) attribute?

The size of (*msg) is the same as command_header and response_header (8 bytes).

Quoted 90 lines
Quoted 90 lines
drivers/mfd/nct6694.c new file +90 -0
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Receive data packet from USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+                        buf, le16_to_cpu(cmd_hd->len), &rx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     if (rx_len != le16_to_cpu(cmd_hd->len)) {
+             dev_err(nct6694->dev, "Expected received length %d, but got %d\n",
+                     le16_to_cpu(cmd_hd->len), rx_len);
+             return -EIO;
+     }
+
+     return nct6694_response_err_handling(nct6694, msg->response_header.sts);
+}
+EXPORT_SYMBOL(nct6694_read_msg);
+
+int nct6694_write_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *cmd_hd, void *buf)
+{
+     union nct6694_usb_msg *msg = nct6694->usb_msg;
+     int tx_len, rx_len, ret;
+
+     guard(mutex)(&nct6694->access_lock);
+
+     /* Send command packet to USB device */
+     memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd));
+     msg->cmd_header.hctrl = NCT6694_HCTRL_SET;
+
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+                        &msg->cmd_header, sizeof(*msg), &tx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Send data packet to USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+                        buf, le16_to_cpu(cmd_hd->len), &tx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Receive response packet from USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+                        &msg->response_header, sizeof(*msg), &rx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Receive data packet from USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+                        buf, le16_to_cpu(cmd_hd->len), &rx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     if (rx_len != le16_to_cpu(cmd_hd->len)) {
+             dev_err(nct6694->dev, "Expected transmitted length %d, but got %d\n",
+                     le16_to_cpu(cmd_hd->len), rx_len);
+             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;
+     unsigned int *int_status = urb->transfer_buffer;
+     int ret;
+
+     switch (urb->status) {
+     case 0:
+             break;
+     case -ECONNRESET:
+     case -ENOENT:
+     case -ESHUTDOWN:
+             return;
+     default:
+             generic_handle_irq_safe(irq_find_mapping(nct6694->domain, irq));
+             *int_status &= ~BIT(irq);
Quoted 9 lines

drivers/mfd/nct6694.c line 207

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     }
+
+resubmit:
+     ret = usb_submit_urb(urb, GFP_ATOMIC);
+     if (ret)
+             dev_dbg(nct6694->dev, "%s: Failed to resubmit urb, status %pe",

Why debug?

Excuse me, do you think it should change to dev_err()?

Quoted 4 lines
Quoted 1 line

+ __func__, ERR_PTR(ret));

Remove the __func__ part.

Understood.

Quoted 2 lines
Quoted 2 lines
drivers/mfd/nct6694.c new file +2 -0
+}
+

...

Quoted 35 lines
Quoted 35 lines
drivers/mfd/nct6694.c new file +35 -0
+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 = &iface->dev;
+     struct nct6694 *nct6694;
+     int pipe, maxp;
+     int ret;
+
+     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;
Quoted 9 lines

drivers/mfd/nct6694.c line 312

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+             goto err_urb;
+     }
+
+     nct6694->dev = dev;
+     nct6694->udev = udev;
+     nct6694->timeout = NCT6694_URB_TIMEOUT; /* Wait until URB completes */

No need to save this known value.

Understood, I will drop it in the next patch.

Quoted 14 lines
Quoted 14 lines
drivers/mfd/nct6694.c new file +14 -0
+     ret = devm_mutex_init(dev, &nct6694->access_lock);
+     if (ret)
+             goto err_urb;
+
+     ret = devm_mutex_init(dev, &nct6694->irq_lock);
+     if (ret)
+             goto err_urb;
+
+     interface = iface->cur_altsetting;
+     int_endpoint = &interface->endpoint[0].desc;
+     if (!usb_endpoint_is_int_in(int_endpoint)) {
+             ret = -ENODEV;
+             goto err_urb;
+     }
Quoted 9 lines

drivers/mfd/nct6694.c line 333

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     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;

Please unsquash these calls - space them out.

Fix these in v9.

Quoted 23 lines
Quoted 23 lines
drivers/mfd/nct6694.c new file +23 -0
+
+     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 nct6694 *nct6694 = usb_get_intfdata(iface);
+
+     mfd_remove_devices(nct6694->dev);
+     usb_kill_urb(nct6694->int_in_urb);
+     usb_free_urb(nct6694->int_in_urb);
Quoted 9 lines

drivers/mfd/nct6694.c line 362

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+}
+
+static const struct usb_device_id nct6694_ids[] = {
+     { USB_DEVICE_AND_INTERFACE_INFO(NCT6694_VENDOR_ID,
+                                     NCT6694_PRODUCT_ID,
+                                     0xFF, 0x00, 0x00)},

This should fit on one line. You can use up to 100-chars.

Fix it in v9.

Quoted 9 lines

drivers/mfd/nct6694.c line 368

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     {}
+};
+MODULE_DEVICE_TABLE(usb, nct6694_ids);
+
+static struct usb_driver nct6694_usb_driver = {
+     .name   = "nct6694",

Odd spaces.

Fix it in v9.

Quoted 8 lines

drivers/mfd/nct6694.c line 373

Quoted 5 lines
drivers/mfd/nct6694.c new file +5 -0
+     .id_table = nct6694_ids,
+     .probe = nct6694_usb_probe,
+     .disconnect = nct6694_usb_disconnect,
+};
+

Remove this line.

Fix it in v9.

Quoted 6 lines

drivers/mfd/nct6694.c line 376

Quoted 3 lines
drivers/mfd/nct6694.c new file +3 -0
+module_usb_driver(nct6694_usb_driver);
+
+MODULE_DESCRIPTION("USB core driver for NCT6694");

This is not a USB driver.

Fix it in v9.

Quoted 4 lines
Quoted 1 line

+MODULE_AUTHOR("Ming Yu <[email protected]>");

Different to SoB.

Fix it in v9.

Quoted 6 lines
Quoted 6 lines
drivers/mfd/nct6694.c new file +1 -0
+MODULE_LICENSE("GPL");
include/linux/mfd/nct6694.h new file +0 -0
new file mode 100644
index 000000000000..8171f975761e
Quoted 9 lines

include/linux/mfd/nct6694.h line 5

Quoted 6 lines
include/linux/mfd/nct6694.h new file +5 -0
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Nuvoton NCT6694 USB transaction and data structure.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.

At the top.

Fix it in v9.

Quoted 2 lines

include/linux/mfd/nct6694.h line 7

Quoted 2 lines
include/linux/mfd/nct6694.h new file +2 -0
+ */
+

Best regards,
Ming

maintainer
#6 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

Quoted 55 lines · Ming Yu

Dear Lee,

Thank you for reviewing,

Lee Jones <[email protected]> 於 2025年3月7日 週五 上午9:15寫道:

Quoted 9 lines · Ming Yu
Quoted 4 lines

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 needs to go into the Kconfig help passage.

Okay, I will move these to Kconfig in the next patch.

Quoted 5 lines
Quoted 2 lines

This driver implements USB device functionality and shares the chip's peripherals as a child device.

This driver doesn't implement USB functionality.

Fix it in v9.

Quoted 9 lines
Quoted 6 lines

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.

Why aren't you signing off with your work address?

Fix it in v9.

Quoted 18 lines
Quoted 18 lines
---
 MAINTAINERS                 |   7 +
 drivers/mfd/Kconfig         |  18 ++
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/nct6694.c       | 378 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/nct6694.h | 102 ++++++++++
 5 files changed, 507 insertions(+)
 create mode 100644 drivers/mfd/nct6694.c
 create mode 100644 include/linux/mfd/nct6694.h
MAINTAINERS +0 -0
index 873aa2cce4d7..c700a0b96960 100644
@@ -16918,6 +16918,13 @@ F:   drivers/nubus/
 F:   include/linux/nubus.h
 F:   include/uapi/linux/nubus.h
 
Quoted 7 lines
Quoted 5 lines

MAINTAINERS F: drivers/nubus/ · line 16923

Quoted 3 lines
MAINTAINERS +3 -0
+NUVOTON NCT6694 MFD DRIVER
+M:   Ming Yu <[email protected]>
+L:   [email protected]

This is the default list. You shouldn't need to add that here.

Remove it in v9.

Please snip everything that you agree with.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
MAINTAINERS +4 -0
+S:   Supported
+F:   drivers/mfd/nct6694.c
+F:   include/linux/mfd/nct6694.h
+
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
 M:   Antonino Daplas <[email protected]>
 L:   [email protected]

[...]

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x1),

IDs are usually given in base-10.

Fix it in v9.

Quoted 7 lines
Quoted 4 lines

Why are you manually adding the device IDs?

PLATFORM_DEVID_AUTO doesn't work for you?

I need to manage these IDs to ensure that child devices can be properly utilized within their respective modules.

How? Please explain.

This numbering looks sequential and arbitrary.

What does PLATFORM_DEVID_AUTO do differently such that it is not useful?

Quoted 29 lines
Quoted 16 lines
Quoted 15 lines
MAINTAINERS +14 -0
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x2),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x3),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x4),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x5),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x6),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x7),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x8),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x9),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xA),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xB),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xC),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xD),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xE),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xF),
Quoted 12 lines

MAINTAINERS

Quoted 9 lines
MAINTAINERS +9 -0
+
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x0),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x1),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x2),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x3),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x4),
+     MFD_CELL_BASIC("i2c-nct6694", NULL, NULL, 0, 0x5),
+
+     MFD_CELL_BASIC("nct6694_canfd", NULL, NULL, 0, 0x0),

Why has the naming convention changed here?

Quoted 3 lines

I originally expected the child devices name to directly match its driver name. Do you think it would be better to standardize the naming as "nct6694-xxx" ?

Yes, that is the usual procedure.

Quoted 11 lines
Quoted 10 lines

MAINTAINERS

Quoted 7 lines
MAINTAINERS +7 -0
+     MFD_CELL_BASIC("nct6694_canfd", NULL, NULL, 0, 0x1),
+
+     MFD_CELL_BASIC("nct6694_wdt", NULL, NULL, 0, 0x0),
+     MFD_CELL_BASIC("nct6694_wdt", NULL, NULL, 0, 0x1),
+
+     MFD_CELL_NAME("nct6694-hwmon"),
+     MFD_CELL_NAME("rtc-nct6694"),

There doesn't seem to be any consistency here.

Quoted 1 line

Do you think these two should be changed to use MFD_CELL_BASIC()?

No. I mean with the device nomenclature.

[...]

Quoted 26 lines
Quoted 16 lines
Quoted 16 lines
MAINTAINERS +16 -0
+static void usb_int_callback(struct urb *urb)
+{
+     struct nct6694 *nct6694 = urb->context;
+     unsigned int *int_status = urb->transfer_buffer;
+     int ret;
+
+     switch (urb->status) {
+     case 0:
+             break;
+     case -ECONNRESET:
+     case -ENOENT:
+     case -ESHUTDOWN:
+             return;
+     default:
+             generic_handle_irq_safe(irq_find_mapping(nct6694->domain, irq));
+             *int_status &= ~BIT(irq);
Quoted 9 lines

MAINTAINERS

Quoted 6 lines
MAINTAINERS +6 -0
+     }
+
+resubmit:
+     ret = usb_submit_urb(urb, GFP_ATOMIC);
+     if (ret)
+             dev_dbg(nct6694->dev, "%s: Failed to resubmit urb, status %pe",

Why debug?

Quoted 1 line

Excuse me, do you think it should change to dev_err()?

Probably a dev_warn() since you are not propagating the error.

Is this okay by the way? Is it okay to fail?

Signature · 1 line
Lee Jones [李琼斯]
author
#7 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

Lee Jones <[email protected]> 於 2025年3月20日 週四 下午10:50寫道:

Quoted 1 line

...

Quoted 12 lines
Quoted 12 lines
Quoted 4 lines
Quoted 1 line

+ MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x1),

IDs are usually given in base-10.

Fix it in v9.

Quoted 4 lines

Why are you manually adding the device IDs?

PLATFORM_DEVID_AUTO doesn't work for you?

Quoted 9 lines
Quoted 2 lines

I need to manage these IDs to ensure that child devices can be properly utilized within their respective modules.

How? Please explain.

This numbering looks sequential and arbitrary.

What does PLATFORM_DEVID_AUTO do differently such that it is not useful?

As far as I know, PLATFORM_DEVID_AUTO assigns dynamic IDs to devices, but I need fixed IDs.
For example, the GPIO driver relies on these IDs to determine the group, allowing the firmware to identify which GPIO group to operate on through the API.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 5 lines
drivers/mfd/nct6694.c new file +4 -0
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x2),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x3),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x4),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x5),

drivers/mfd/nct6694.c line 40

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x6),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x7),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x8),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x9),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xA),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xB),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xC),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xD),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xE),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xF),

Thanks,
Ming

maintainer
#8 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

Quoted 22 lines · Ming Yu

Lee Jones <[email protected]> 於 2025年3月20日 週四 下午10:50寫道:

Quoted 1 line

...

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x1),

IDs are usually given in base-10.

Fix it in v9.

Quoted 7 lines
Quoted 4 lines

Why are you manually adding the device IDs?

PLATFORM_DEVID_AUTO doesn't work for you?

I need to manage these IDs to ensure that child devices can be properly utilized within their respective modules.

How? Please explain.

This numbering looks sequential and arbitrary.

Quoted 8 lines
Quoted 2 lines

What does PLATFORM_DEVID_AUTO do differently such that it is not useful?

As far as I know, PLATFORM_DEVID_AUTO assigns dynamic IDs to devices, but I need fixed IDs.
For example, the GPIO driver relies on these IDs to determine the group, allowing the firmware to identify which GPIO group to operate on through the API.

PLATFORM_DEVID_AUTO will allocate IDs 0 through 16, the same as you've done here. These lines do not have any differentiating attributes, so either way we are not allocating specific IDs to specific pieces of the H/W. I still do not understand why you need to allocate them manually.

Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x2),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x3),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x4),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x5),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x6),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x7),
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/mfd/nct6694.c line 40

Quoted 8 lines
drivers/mfd/nct6694.c new file +8 -0
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x8),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x9),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xA),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xB),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xC),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xD),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xE),
+     MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0xF),

Thanks,
Ming

Signature · 1 line
Lee Jones [李琼斯]
author
#9 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

Lee Jones <[email protected]> 於 2025年4月4日 週五 下午10:21寫道:

Quoted 30 lines
Quoted 21 lines

...

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x1),

IDs are usually given in base-10.

Fix it in v9.

Quoted 7 lines
Quoted 4 lines

Why are you manually adding the device IDs?

PLATFORM_DEVID_AUTO doesn't work for you?

I need to manage these IDs to ensure that child devices can be properly utilized within their respective modules.

How? Please explain.

This numbering looks sequential and arbitrary.

Quoted 8 lines
Quoted 2 lines

What does PLATFORM_DEVID_AUTO do differently such that it is not useful?

As far as I know, PLATFORM_DEVID_AUTO assigns dynamic IDs to devices, but I need fixed IDs.
For example, the GPIO driver relies on these IDs to determine the group, allowing the firmware to identify which GPIO group to operate on through the API.

Quoted 5 lines

PLATFORM_DEVID_AUTO will allocate IDs 0 through 16, the same as you've done here. These lines do not have any differentiating attributes, so either way we are not allocating specific IDs to specific pieces of the H/W. I still do not understand why you need to allocate them manually.

I'm using PLATFORM_DEVID_AUTO to allocate child device IDs with MFD_CELL_NAME(), like this:

static const struct mfd_cell nct6694_dev[] = {
    MFD_CELL_NAME("nct6694-gpio"),
    MFD_CELL_NAME("nct6694-gpio"),
    ......
    MFD_CELL_NAME("nct6694-gpio"),
    MFD_CELL_NAME("nct6694-i2c"),
    MFD_CELL_NAME("nct6694-i2c"),
    ......
    MFD_CELL_NAME("nct6694-i2c"),
    ......
};

For example, the device IDs retrieved in gpio-nct6694.c is 1~16, and i2c-nct6694.c is 17~22. Does this mean each driver should independently handle its dynamically assigned IDs? Additionally, I originally referred to cgbc-core.c with i2c-cgbc.c, and ab8500-core.c with pwm-ab8500.c for associating child devices. Do you think this approach is appropriate in my case?

Thanks,
Ming

maintainer
#10 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

Quoted 52 lines · Ming Yu

Lee Jones <[email protected]> 於 2025年4月4日 週五 下午10:21寫道:

Quoted 30 lines
Quoted 21 lines

...

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x1),

IDs are usually given in base-10.

Fix it in v9.

Quoted 7 lines
Quoted 4 lines

Why are you manually adding the device IDs?

PLATFORM_DEVID_AUTO doesn't work for you?

I need to manage these IDs to ensure that child devices can be properly utilized within their respective modules.

How? Please explain.

This numbering looks sequential and arbitrary.

Quoted 8 lines
Quoted 2 lines

What does PLATFORM_DEVID_AUTO do differently such that it is not useful?

As far as I know, PLATFORM_DEVID_AUTO assigns dynamic IDs to devices, but I need fixed IDs.
For example, the GPIO driver relies on these IDs to determine the group, allowing the firmware to identify which GPIO group to operate on through the API.

Quoted 5 lines

PLATFORM_DEVID_AUTO will allocate IDs 0 through 16, the same as you've done here. These lines do not have any differentiating attributes, so either way we are not allocating specific IDs to specific pieces of the H/W. I still do not understand why you need to allocate them manually.

I'm using PLATFORM_DEVID_AUTO to allocate child device IDs with MFD_CELL_NAME(), like this:

static const struct mfd_cell nct6694_dev[] = {
    MFD_CELL_NAME("nct6694-gpio"),
    MFD_CELL_NAME("nct6694-gpio"),
    ......
    MFD_CELL_NAME("nct6694-gpio"),
    MFD_CELL_NAME("nct6694-i2c"),
    MFD_CELL_NAME("nct6694-i2c"),
    ......
    MFD_CELL_NAME("nct6694-i2c"),
    ......
};
Quoted 6 lines

For example, the device IDs retrieved in gpio-nct6694.c is 1~16, and i2c-nct6694.c is 17~22. Does this mean each driver should independently handle its dynamically assigned IDs? Additionally, I originally referred to cgbc-core.c with i2c-cgbc.c, and ab8500-core.c with pwm-ab8500.c for associating child devices. Do you think this approach is appropriate in my case?

Yes, if you _need_ the ranges to start from 0, then you will have to call mfd_add_devices() separately on those ranges. Otherwise one range will follow directly on to another range.

But wait, you're using mfd_add_hotplug_devices(), which means you are using PLATFORM_DEVID_AUTO. So your .id values that you've added are being ignored anyway. Thus, if you have tested that this works, you don't need them anyway, right?

Signature · 1 line
Lee Jones [李琼斯]
author
#11 lore raw

Re: [PATCH v8 1/7] mfd: Add core driver for Nuvoton NCT6694

Lee Jones <[email protected]> 於 2025年4月10日 週四 下午4:21寫道:

Quoted 62 lines · Ming Yu
Quoted 53 lines

Lee Jones <[email protected]> 於 2025年4月4日 週五 下午10:21寫道:

Quoted 30 lines
Quoted 21 lines

...

Quoted 19 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ MFD_CELL_BASIC("gpio-nct6694", NULL, NULL, 0, 0x1),

IDs are usually given in base-10.

Fix it in v9.

Quoted 7 lines
Quoted 4 lines

Why are you manually adding the device IDs?

PLATFORM_DEVID_AUTO doesn't work for you?

I need to manage these IDs to ensure that child devices can be properly utilized within their respective modules.

How? Please explain.

This numbering looks sequential and arbitrary.

Quoted 8 lines
Quoted 2 lines

What does PLATFORM_DEVID_AUTO do differently such that it is not useful?

As far as I know, PLATFORM_DEVID_AUTO assigns dynamic IDs to devices, but I need fixed IDs.
For example, the GPIO driver relies on these IDs to determine the group, allowing the firmware to identify which GPIO group to operate on through the API.

Quoted 5 lines

PLATFORM_DEVID_AUTO will allocate IDs 0 through 16, the same as you've done here. These lines do not have any differentiating attributes, so either way we are not allocating specific IDs to specific pieces of the H/W. I still do not understand why you need to allocate them manually.

I'm using PLATFORM_DEVID_AUTO to allocate child device IDs with MFD_CELL_NAME(), like this:

static const struct mfd_cell nct6694_dev[] = {
    MFD_CELL_NAME("nct6694-gpio"),
    MFD_CELL_NAME("nct6694-gpio"),
    ......
    MFD_CELL_NAME("nct6694-gpio"),
    MFD_CELL_NAME("nct6694-i2c"),
    MFD_CELL_NAME("nct6694-i2c"),
    ......
    MFD_CELL_NAME("nct6694-i2c"),
    ......
};
Quoted 6 lines

For example, the device IDs retrieved in gpio-nct6694.c is 1~16, and i2c-nct6694.c is 17~22. Does this mean each driver should independently handle its dynamically assigned IDs? Additionally, I originally referred to cgbc-core.c with i2c-cgbc.c, and ab8500-core.c with pwm-ab8500.c for associating child devices. Do you think this approach is appropriate in my case?

Quoted 9 lines

Yes, if you _need_ the ranges to start from 0, then you will have to call mfd_add_devices() separately on those ranges. Otherwise one range will follow directly on to another range.

But wait, you're using mfd_add_hotplug_devices(), which means you are using PLATFORM_DEVID_AUTO. So your .id values that you've added are being ignored anyway. Thus, if you have tested that this works, you don't need them anyway, right?

Yes, it uses PLATFORM_DEVID_AUTO, but in my implementation, the sub-devices use cell->id instead of platform_device->id, so it doesn't affect the current behavior.

However, if you think there's a better approach or that this should be changed for consistency or correctness, I'm happy to update it, please let me know your recommendation.

Thanks,
Ming

author
#12 lore raw

PATCH v8 2/7 gpio: Add Nuvoton NCT6694 GPIO support

In reply to Ming Yu

This driver supports GPIO and IRQ functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                 |   1 +
 drivers/gpio/Kconfig        |  12 +
 drivers/gpio/Makefile       |   1 +
 drivers/gpio/gpio-nct6694.c | 463 ++++++++++++++++++++++++++++++++++++
 4 files changed, 477 insertions(+)
 create mode 100644 drivers/gpio/gpio-nct6694.c
MAINTAINERS +1 -0
index c700a0b96960..be7cd44a4c88 100644
@@ -16922,6 +16922,7 @@ NUVOTON NCT6694 MFD DRIVER
 M:	Ming Yu <[email protected]>
 L:	[email protected]
 S:	Supported
+F:	drivers/gpio/gpio-nct6694.c
 F:	drivers/mfd/nct6694.c
 F:	include/linux/mfd/nct6694.h
 
drivers/gpio/Kconfig +12 -0
index add5ad29a673..9396de3ea86d 100644
@@ -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
drivers/gpio/Makefile +1 -0
index af3ba4d81b58..def8487540ab 100644
@@ -121,6 +121,7 @@ obj-$(CONFIG_GPIO_MT7621)		+= gpio-mt7621.o
 obj-$(CONFIG_GPIO_MVEBU)		+= gpio-mvebu.o
 obj-$(CONFIG_GPIO_MXC)			+= gpio-mxc.o
 obj-$(CONFIG_GPIO_MXS)			+= gpio-mxs.o
+obj-$(CONFIG_GPIO_NCT6694)		+= gpio-nct6694.o
 obj-$(CONFIG_GPIO_NOMADIK)		+= gpio-nomadik.o
 obj-$(CONFIG_GPIO_NPCM_SGPIO)		+= gpio-npcm-sgpio.o
 obj-$(CONFIG_GPIO_OCTEON)		+= gpio-octeon.o
drivers/gpio/gpio-nct6694.c new file +463 -0

463 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#13 lore raw

PATCH v8 3/7 i2c: Add Nuvoton NCT6694 I2C support

In reply to Ming Yu

This driver supports I2C adapter functionality for NCT6694 MFD device based on USB interface, each I2C controller use default baudrate(100K).

---
 MAINTAINERS                      |   1 +
 drivers/i2c/busses/Kconfig       |  10 ++
 drivers/i2c/busses/Makefile      |   1 +
 drivers/i2c/busses/i2c-nct6694.c | 152 +++++++++++++++++++++++++++++++
 4 files changed, 164 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-nct6694.c
MAINTAINERS +1 -0
index be7cd44a4c88..1327e7a6e507 100644
@@ -16923,6 +16923,7 @@ M:	Ming Yu <[email protected]>
 L:	[email protected]
 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
 
drivers/i2c/busses/Kconfig +10 -0
index fc438f445771..d0753d0adaae 100644
@@ -1329,6 +1329,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
drivers/i2c/busses/Makefile +1 -0
index 1c2a4510abe4..c5a60116dd54 100644
@@ -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
drivers/i2c/busses/i2c-nct6694.c new file +152 -0

152 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#14 lore raw

Re: [PATCH v8 3/7] i2c: Add Nuvoton NCT6694 I2C support

Hi Ming,

...

drivers/i2c/busses/i2c-nct6694.c line 34

Quoted 9 lines
drivers/i2c/busses/i2c-nct6694.c new file +9 -0
+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
+};

do we need all these frequencies? I don't see them use anywhere.

Besides, can you please use a proper prefix? I2C_BR_* prefix doesn't belong to this driver.

Andi

author
#15 lore raw

Re: [PATCH v8 3/7] i2c: Add Nuvoton NCT6694 I2C support

Dear Andi,

Thank you for reviewing,

Andi Shyti <[email protected]> 於 2025年3月20日 週四 上午7:58寫道:

Quoted 1 line

...

Quoted 12 lines

drivers/i2c/busses/i2c-nct6694.c line 34

Quoted 9 lines
drivers/i2c/busses/i2c-nct6694.c new file +9 -0
+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
+};

do we need all these frequencies? I don't see them use anywhere.

Originally, I used module parameters to configure these I2C's baud rate, Do you think this approach iis suitable?

Quoted 3 lines

Besides, can you please use a proper prefix? I2C_BR_* prefix doesn't belong to this driver.

Okay, I will fix these macros in the next patch.

Best regards,
Ming

author
#16 lore raw

PATCH v8 4/7 can: Add Nuvoton NCT6694 CANFD support

In reply to Ming Yu

This driver supports Socket CANFD functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                         |   1 +
 drivers/net/can/usb/Kconfig         |  11 +
 drivers/net/can/usb/Makefile        |   1 +
 drivers/net/can/usb/nct6694_canfd.c | 799 ++++++++++++++++++++++++++++
 4 files changed, 812 insertions(+)
 create mode 100644 drivers/net/can/usb/nct6694_canfd.c
MAINTAINERS +1 -0
index 1327e7a6e507..8aa611504172 100644
@@ -16925,6 +16925,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/usb/nct6694_canfd.c
 F:	include/linux/mfd/nct6694.h
 
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/net/can/usb/Kconfig +11 -0
index 9dae0c71a2e1..759e724a67cf 100644
@@ -133,6 +133,17 @@ config CAN_MCBA_USB
 	  This driver supports the CAN BUS Analyzer interface
 	  from Microchip (http://www.microchip.com/development-tools/).
 
+config CAN_NCT6694
+	tristate "Nuvoton NCT6694 Socket CANfd support"
+	depends on MFD_NCT6694
+	select CAN_RX_OFFLOAD
+	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_PEAK_USB
 	tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
 	help
drivers/net/can/usb/Makefile +1 -0
index 8b11088e9a59..fcafb1ac262e 100644
@@ -11,5 +11,6 @@ obj-$(CONFIG_CAN_F81604) += f81604.o
 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
 obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
+obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o
 obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
 obj-$(CONFIG_CAN_UCAN) += ucan.o
drivers/net/can/usb/nct6694_canfd.c new file +799 -0

799 lines not on this page · raw message

Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Hi Ming,

Thanks for the v8. There are a few last things you need to fix. These are my last comments, so if all of these are addressed, I will give you my reviewed-by tag in the v9.

Quoted 329 lines · Ming Yu

This driver supports Socket CANFD functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                         |   1 +
 drivers/net/can/usb/Kconfig         |  11 +
 drivers/net/can/usb/Makefile        |   1 +
 drivers/net/can/usb/nct6694_canfd.c | 799 ++++++++++++++++++++++++++++
 4 files changed, 812 insertions(+)
 create mode 100644 drivers/net/can/usb/nct6694_canfd.c
MAINTAINERS +1 -0
index 1327e7a6e507..8aa611504172 100644
@@ -16925,6 +16925,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/usb/nct6694_canfd.c
 F:	include/linux/mfd/nct6694.h
 
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/net/can/usb/Kconfig +11 -0
index 9dae0c71a2e1..759e724a67cf 100644
@@ -133,6 +133,17 @@ config CAN_MCBA_USB
 	  This driver supports the CAN BUS Analyzer interface
 	  from Microchip (http://www.microchip.com/development-tools/).
 
+config CAN_NCT6694
+	tristate "Nuvoton NCT6694 Socket CANfd support"
+	depends on MFD_NCT6694
+	select CAN_RX_OFFLOAD
+	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_PEAK_USB
 	tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
 	help
drivers/net/can/usb/Makefile +1 -0
index 8b11088e9a59..fcafb1ac262e 100644
@@ -11,5 +11,6 @@ obj-$(CONFIG_CAN_F81604) += f81604.o
 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
 obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
+obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o
 obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
 obj-$(CONFIG_CAN_UCAN) += ucan.o
drivers/net/can/usb/nct6694_canfd.c new file +266 -0
new file mode 100644
index 000000000000..d97fce5cdf32
@@ -0,0 +1,799 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Nuvoton NCT6694 Socket CANfd driver based on USB interface.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#include <linux/can/dev.h>
+#include <linux/can/rx-offload.h>
+#include <linux/ethtool.h>
+#include <linux/irqdomain.h>
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/platform_device.h>
+
+#define DRVNAME "nct6694_canfd"
+
+/* 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		0x00
+#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		0x01
+#define NCT6694_CAN_INFORMATION_SEL	0x00
+
+/* Command 02h - CAN Event */
+#define NCT6694_CAN_EVENT		0x02
+#define NCT6694_CAN_EVENT_SEL(idx, mask)	\
+	((idx ? 0x80 : 0x00) | ((mask) & 0x7F))
+
+#define NCT6694_CAN_EVENT_MASK		GENMASK(5, 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_DATA_IN	BIT(7)	/* Read-clear*/
+
+/* Command 10h - CAN Deliver */
+#define NCT6694_CAN_DELIVER		0x10
+#define NCT6694_CAN_DELIVER_SEL(buf_cnt)	\
+	((buf_cnt) & 0xFF)
+
+/* Command 11h - CAN Receive */
+#define NCT6694_CAN_RECEIVE		0x11
+#define NCT6694_CAN_RECEIVE_SEL(idx, buf_cnt)	\
+	((idx ? 0x80 : 0x00) | ((buf_cnt) & 0x7F))
+
+#define NCT6694_CAN_FRAME_TAG(idx)	(0xC0 | (idx))
+#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 = 0,
+	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 = 0,
+	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 {
+	u8 err;
+	u8 status;
+	u8 tx_evt;
+	u8 rx_evt;
+	u8 rec;
+	u8 tec;
+	u8 reserved[2];
+};
+
+struct __packed nct6694_can_frame {
+	u8 tag;
+	u8 flag;
+	u8 reserved;
+	u8 length;
+	__le32 id;
+	u8 data[CANFD_MAX_DLEN];
+};
+
+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 workqueue_struct *wq;
+	struct work_struct tx_work;
+	struct nct6694_can_frame tx;
+	struct nct6694_can_frame rx;
+	struct nct6694_can_event event[2];
+	struct can_berr_counter bec;
+};
+
+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;
+	const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_RECEIVE,
+		.sel = NCT6694_CAN_RECEIVE_SEL(ndev->dev_port, 1),
+		.len = cpu_to_le16(sizeof(*frame))
+	};
+	struct sk_buff *skb;
+	int ret;
+
+	ret = nct6694_read_msg(priv->nct6694, &cmd_hd, frame);
+	if (ret)
+		return;
+
+	if (frame->flag & NCT6694_CAN_FRAME_FLAG_FD) {
+		struct canfd_frame *cfd;
+
+		skb = alloc_canfd_skb(priv->ndev, &cfd);
+		if (!skb)
+			return;
+
+		cfd->can_id = le32_to_cpu(frame->id);
+		cfd->len = canfd_sanitize_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 {
+		struct can_frame *cf;
+
+		skb = alloc_can_skb(priv->ndev, &cf);
+		if (!skb)
+			return;
+
+		cf->can_id = le32_to_cpu(frame->id);
+		cf->len = can_cc_dlc2len(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;
+		else
+			memcpy(cf->data, frame->data, cf->len);
+	}
+
+	nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+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);
+
+	*bec = priv->bec;
+
+	return 0;
+}
+
+static void nct6694_can_handle_state_change(struct net_device *ndev,
+					    enum can_state new_state)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+	struct can_berr_counter bec;
+	struct can_frame *cf;
+	struct sk_buff *skb;

drivers/net/can/usb/nct6694_canfd.c line 276

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+
+	skb = alloc_can_err_skb(ndev, &cf);
+
+	nct6694_can_get_berr_counter(ndev, &bec);
+
+	switch (new_state) {
+	case CAN_STATE_ERROR_ACTIVE:
+		priv->can.can_stats.error_warning++;
+		priv->can.state = CAN_STATE_ERROR_ACTIVE;
+		if (cf)

Set the CAN_ER_CRTL flag:

		if (cf) {
			cf->can_id |= CAN_ERR_CRTL;
			cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
		}

drivers/net/can/usb/nct6694_canfd.c line 283

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+			cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
+		break;
+	case CAN_STATE_ERROR_WARNING:
+		priv->can.can_stats.error_warning++;
+		priv->can.state = CAN_STATE_ERROR_WARNING;
+		if (cf) {
+			cf->can_id |= CAN_ERR_CRTL;

Set the CAN_ERR_CNT flag when you populate cf->data[6] and cf->data[7]:

			cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;

drivers/net/can/usb/nct6694_canfd.c line 296

Quoted 13 lines
drivers/net/can/usb/nct6694_canfd.c new file +13 -0
+			if (bec.txerr > bec.rxerr)
+				cf->data[1] = CAN_ERR_CRTL_TX_WARNING;
+			else
+				cf->data[1] = CAN_ERR_CRTL_RX_WARNING;
+			cf->data[6] = bec.txerr;
+			cf->data[7] = bec.rxerr;
+		}
+		break;
+	case CAN_STATE_ERROR_PASSIVE:
+		priv->can.can_stats.error_passive++;
+		priv->can.state = CAN_STATE_ERROR_PASSIVE;
+		if (cf) {
+			cf->can_id |= CAN_ERR_CRTL;

Set the CAN_ERR_CNT flag when you populate cf->data[6] and cf->data[7]:

			cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
Quoted 290 lines
drivers/net/can/usb/nct6694_canfd.c new file +290 -0
+			cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
+			if (bec.txerr >= CAN_ERROR_PASSIVE_THRESHOLD)
+				cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
+			cf->data[6] = bec.txerr;
+			cf->data[7] = bec.rxerr;
+		}
+		break;
+	case CAN_STATE_BUS_OFF:
+		priv->can.state = CAN_STATE_BUS_OFF;
+		priv->can.can_stats.bus_off++;
+		if (cf)
+			cf->can_id |= CAN_ERR_BUSOFF;
+		can_free_echo_skb(ndev, 0, NULL);
+		netif_stop_queue(ndev);> +		can_bus_off(ndev);
+		break;
+	default:
+		break;
+	}
+
+	nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_handle_state_errors(struct net_device *ndev, u8 status)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+	if (status == NCT6694_CAN_EVT_STS_ERROR_ACTIVE &&
+	    priv->can.state != CAN_STATE_ERROR_ACTIVE) {
+		netdev_dbg(ndev, "Error, entered active state\n");
+		nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_ACTIVE);
+	}
+
+	if (status == NCT6694_CAN_EVT_STS_WARNING &&
+	    priv->can.state != CAN_STATE_ERROR_WARNING) {
+		netdev_dbg(ndev, "Error, entered warning state\n");
+		nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_WARNING);
+	}
+
+	if (status == NCT6694_CAN_EVT_STS_ERROR_PASSIVE &&
+	    priv->can.state != CAN_STATE_ERROR_PASSIVE) {
+		netdev_dbg(ndev, "Error, entered passive state\n");
+		nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_PASSIVE);
+	}
+
+	if (status == NCT6694_CAN_EVT_STS_BUS_OFF &&
+	    priv->can.state != CAN_STATE_BUS_OFF) {
+		netdev_dbg(ndev, "Error, entered bus-off state\n");
+		nct6694_can_handle_state_change(ndev, CAN_STATE_BUS_OFF);
+	}
+}
+
+static void nct6694_can_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;
+	}
+
+	if (skb)
+		nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+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;
+
+	stats->tx_bytes += can_rx_offload_get_echo_skb_queue_tail(&priv->offload,
+								  0, NULL);
+	stats->tx_packets++;
+	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 *event = &priv->event[ndev->dev_port];
+	const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_EVENT,
+		.sel = NCT6694_CAN_EVENT_SEL(ndev->dev_port, NCT6694_CAN_EVENT_MASK),
+		.len = cpu_to_le16(sizeof(priv->event))
+	};
+	irqreturn_t handled = IRQ_NONE;
+	int ret;
+
+	ret = nct6694_read_msg(priv->nct6694, &cmd_hd, priv->event);
+	if (ret < 0)
+		return handled;
+
+	if (event->rx_evt & NCT6694_CAN_EVT_RX_DATA_IN) {
+		nct6694_can_rx(ndev, event->rx_evt);
+		handled = IRQ_HANDLED;
+	}
+
+	if (event->rx_evt & NCT6694_CAN_EVT_RX_DATA_LOST) {
+		nct6694_can_handle_lost_msg(ndev);
+		handled = IRQ_HANDLED;
+	}
+
+	if (event->status) {
+		nct6694_can_handle_state_errors(ndev, event->status);
+		handled = IRQ_HANDLED;
+	}
+
+	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
+		nct6694_can_handle_bus_err(ndev, event->err);
+		handled = IRQ_HANDLED;
+	}
+
+	if (event->tx_evt & NCT6694_CAN_EVT_TX_FIFO_EMPTY) {
+		nct6694_can_tx_irq(ndev);
+		handled = IRQ_HANDLED;
+	}
+
+	if (handled)
+		can_rx_offload_threaded_irq_finish(&priv->offload);
+
+	priv->bec.rxerr = event->rec;
+	priv->bec.txerr = event->tec;
+
+	return handled;
+}
+
+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 nct6694_can_frame *frame = &priv->tx;
+	struct net_device *ndev = priv->ndev;
+	struct net_device_stats *stats = &ndev->stats;
+	struct sk_buff *skb = priv->can.echo_skb[0];
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_DELIVER,
+		.sel = NCT6694_CAN_DELIVER_SEL(1),
+		.len = cpu_to_le16(sizeof(*frame))
+	};
+	u32 txid;
+	int err;
+
+	memset(frame, 0, sizeof(*frame));
+
+	frame->tag = NCT6694_CAN_FRAME_TAG(ndev->dev_port);
+
+	if (can_is_canfd_skb(skb)) {
+		struct canfd_frame *cfd = (struct canfd_frame *)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 {
+		struct can_frame *cf = (struct can_frame *)skb->data;
+
+		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;
+		}
+
+		if (cf->can_id & CAN_RTR_FLAG)
+			frame->flag |= NCT6694_CAN_FRAME_FLAG_RTR;
+		else
+			memcpy(frame->data, cf->data, cf->len);
+
+		frame->id = cpu_to_le32(txid);
+		frame->length = cf->len;
+	}
+
+	err = nct6694_write_msg(priv->nct6694, &cmd_hd, frame);
+	if (err) {
+		netdev_err(ndev, "%s: TX FIFO is full!\n", __func__);
+		can_free_echo_skb(ndev, 0, NULL);
+		stats->tx_dropped++;
+		stats->tx_errors++;
+		netif_wake_queue(ndev);
+	}
+}
+
+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;
+
+	netif_stop_queue(ndev);
+	can_put_echo_skb(skb, ndev, 0, 0);
+	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);
+	const struct can_bittiming *d_bt = &priv->can.data_bittiming;
+	const struct can_bittiming *n_bt = &priv->can.bittiming;
+	struct nct6694_can_setting *setting __free(kfree) = NULL;> +	const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_SETTING,
+		.sel = ndev->dev_port,
+		.len = cpu_to_le16(sizeof(*setting))
+	};
+	int ret;
+
+	setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+	if (!setting)
+		return -ENOMEM;
+
+	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_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, &cmd_hd, setting);
+	if (ret)
+		return ret;
+

drivers/net/can/usb/nct6694_canfd.c line 599

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	priv->can.state = CAN_STATE_ERROR_ACTIVE;
+
+	return 0;
+}
+
+static int nct6694_can_stop(struct net_device *ndev)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+	priv->can.ctrlmode = CAN_CTRLMODE_LISTENONLY;

Hmmm, when Marc asked you to put the device in listen only mode, I think he meant that you set it on the device side (i.e. flag NCT6694_CAN_SETTING_CTRL1_MON) and not on the driver side. If you set CAN_CTRLMODE_LISTENONLY flag, that will be reported in the netlink interface. So you should not change that flag.

But before that, did you check the datasheet? Don't you have a device flag to actually turn the device off (e.g. sleep mode)?

Quoted 135 lines
drivers/net/can/usb/nct6694_canfd.c new file +135 -0
+	netif_stop_queue(ndev);
+	free_irq(ndev->irq, ndev);
+	destroy_workqueue(priv->wq);
+	can_rx_offload_disable(&priv->offload);
+	priv->can.state = CAN_STATE_STOPPED;
+	close_candev(ndev);
+
+	return 0;
+}
+
+static int nct6694_can_set_mode(struct net_device *ndev, enum can_mode mode)
+{
+	int ret;
+
+	switch (mode) {
+	case CAN_MODE_START:
+		ret = nct6694_can_start(ndev);
+		if (!ret && netif_queue_stopped(ndev))
+			netif_wake_queue(ndev);
+		break;
+
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return ret;
+}
+
+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;
+	}
+
+	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 __free(kfree) = NULL;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_INFORMATION,
+		.sel = NCT6694_CAN_INFORMATION_SEL,
+		.len = cpu_to_le16(sizeof(*info))
+	};
+	int ret, can_clk;
+
+	info = kzalloc(sizeof(*info), GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+
+	ret = nct6694_read_msg(priv->nct6694, &cmd_hd, info);
+	if (ret)
+		return ret;
+
+	can_clk = le32_to_cpu(info->can_clk);
+
+	return 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_CAN0 + 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->dev_port = cell->id;
+	ndev->netdev_ops = &nct6694_can_netdev_ops;
+	ndev->ethtool_ops = &nct6694_can_ethtool_ops;
+
+	priv = netdev_priv(ndev);
+	priv->nct6694 = nct6694;
+	priv->ndev = ndev;
+

drivers/net/can/usb/nct6694_canfd.c line 744

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	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;
+	}
+
+	INIT_WORK(&priv->tx_work, nct6694_can_tx_work);
+
+	priv->can.state = CAN_STATE_STOPPED;

Marc asked you to remove this line during the v7 review.

Quoted 45 lines
drivers/net/can/usb/nct6694_canfd.c new file +45 -0
+	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_supported = CAN_CTRLMODE_LOOPBACK |
+		CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+		CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
+
+	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 rx_offload_del;
+
+	return 0;
+
+rx_offload_del:
+	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);
+
+	unregister_candev(priv->ndev);
+	can_rx_offload_del(&priv->offload);
+	free_candev(priv->ndev);
+}
+
+static struct platform_driver nct6694_can_driver = {
+	.driver = {
+		.name	= DRVNAME,
+	},

drivers/net/can/usb/nct6694_canfd.c line 799

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	.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 <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:nct6694-can");

Yours sincerely,
Vincent Mailhol

author
#18 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Dear Vincent,

Thank you for reviewing,

Vincent Mailhol <[email protected]> 於 2025年2月27日 週四 上午10:09寫道:

Quoted 1 line

...

Quoted 20 lines
Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+static void nct6694_can_handle_state_change(struct net_device *ndev,
+                                         enum can_state new_state)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+     struct can_berr_counter bec;
+     struct can_frame *cf;
+     struct sk_buff *skb;

drivers/net/can/usb/nct6694_canfd.c line 276

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+
+     skb = alloc_can_err_skb(ndev, &cf);
+
+     nct6694_can_get_berr_counter(ndev, &bec);
+
+     switch (new_state) {
+     case CAN_STATE_ERROR_ACTIVE:
+             priv->can.can_stats.error_warning++;
+             priv->can.state = CAN_STATE_ERROR_ACTIVE;
+             if (cf)

Set the CAN_ER_CRTL flag:

Quoted 5 lines
                if (cf) {
                        cf->can_id |= CAN_ERR_CRTL;
                        cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
                }

Fix it in the v9.

Quoted 8 lines

drivers/net/can/usb/nct6694_canfd.c line 283

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+                     cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
+             break;
+     case CAN_STATE_ERROR_WARNING:
+             priv->can.can_stats.error_warning++;
+             priv->can.state = CAN_STATE_ERROR_WARNING;
+             if (cf) {
+                     cf->can_id |= CAN_ERR_CRTL;
Quoted 4 lines

Set the CAN_ERR_CNT flag when you populate cf->data[6] and cf->data[7]:

                        cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;

Fix it in the v9.

Quoted 14 lines

drivers/net/can/usb/nct6694_canfd.c line 296

Quoted 13 lines
drivers/net/can/usb/nct6694_canfd.c new file +13 -0
+                     if (bec.txerr > bec.rxerr)
+                             cf->data[1] = CAN_ERR_CRTL_TX_WARNING;
+                     else
+                             cf->data[1] = CAN_ERR_CRTL_RX_WARNING;
+                     cf->data[6] = bec.txerr;
+                     cf->data[7] = bec.rxerr;
+             }
+             break;
+     case CAN_STATE_ERROR_PASSIVE:
+             priv->can.can_stats.error_passive++;
+             priv->can.state = CAN_STATE_ERROR_PASSIVE;
+             if (cf) {
+                     cf->can_id |= CAN_ERR_CRTL;
Quoted 4 lines

Set the CAN_ERR_CNT flag when you populate cf->data[6] and cf->data[7]:

                        cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;

Fix it in the v9.

Quoted 22 lines
Quoted 22 lines
drivers/net/can/usb/nct6694_canfd.c new file +22 -0
+                     cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
+                     if (bec.txerr >= CAN_ERROR_PASSIVE_THRESHOLD)
+                             cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
+                     cf->data[6] = bec.txerr;
+                     cf->data[7] = bec.rxerr;
+             }
+             break;
+     case CAN_STATE_BUS_OFF:
+             priv->can.state = CAN_STATE_BUS_OFF;
+             priv->can.can_stats.bus_off++;
+             if (cf)
+                     cf->can_id |= CAN_ERR_BUSOFF;
+             can_free_echo_skb(ndev, 0, NULL);
+             netif_stop_queue(ndev);> +              can_bus_off(ndev);
+             break;
+     default:
+             break;
+     }
+
+     nct6694_can_rx_offload(&priv->offload, skb);
+}
+

...

Quoted 12 lines

drivers/net/can/usb/nct6694_canfd.c line 599

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+static int nct6694_can_stop(struct net_device *ndev)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+     priv->can.ctrlmode = CAN_CTRLMODE_LISTENONLY;

Hmmm, when Marc asked you to put the device in listen only mode, I think he meant that you set it on the device side (i.e. flag NCT6694_CAN_SETTING_CTRL1_MON) and not on the driver side. If you set CAN_CTRLMODE_LISTENONLY flag, that will be reported in the netlink interface. So you should not change that flag.

Quoted 3 lines

But before that, did you check the datasheet? Don't you have a device flag to actually turn the device off (e.g. sleep mode)?

Our firmware currently does not provide an interface to turn the device off, I will put the device in listen-only mode as an alternative.

Quoted 10 lines
Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+     netif_stop_queue(ndev);
+     free_irq(ndev->irq, ndev);
+     destroy_workqueue(priv->wq);
+     can_rx_offload_disable(&priv->offload);
+     priv->can.state = CAN_STATE_STOPPED;
+     close_candev(ndev);
+
+     return 0;
+}
+

...

Quoted 31 lines
Quoted 31 lines
drivers/net/can/usb/nct6694_canfd.c new file +31 -0
+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_CAN0 + 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->dev_port = cell->id;
+     ndev->netdev_ops = &nct6694_can_netdev_ops;
+     ndev->ethtool_ops = &nct6694_can_ethtool_ops;
+
+     priv = netdev_priv(ndev);
+     priv->nct6694 = nct6694;
+     priv->ndev = ndev;
+
+     can_clk = nct6694_can_get_clock(priv);
+     if (can_clk < 0) {
+             ret = dev_err_probe(&pdev->dev, can_clk,
+                                 "Failed to get clock\n");
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 744

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+             goto free_candev;
+     }
+
+     INIT_WORK(&priv->tx_work, nct6694_can_tx_work);
+
+     priv->can.state = CAN_STATE_STOPPED;

Marc asked you to remove this line during the v7 review.

Sorry, drop it in the v9.

Quoted 24 lines
Quoted 24 lines
drivers/net/can/usb/nct6694_canfd.c new file +24 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
+
+     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 rx_offload_del;
+
+     return 0;
Quoted 8 lines

drivers/net/can/usb/nct6694_canfd.c line 776

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+
+rx_offload_del:
+     can_rx_offload_del(&priv->offload);
+free_candev:
+     free_candev(ndev);
+     return ret;
+}
+

Best regards,
Ming

#19 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

In reply to Vincent Mailhol
Quoted 11 lines · Vincent Mailhol

drivers/net/can/usb/nct6694_canfd.c line 599

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+static int nct6694_can_stop(struct net_device *ndev)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+	priv->can.ctrlmode = CAN_CTRLMODE_LISTENONLY;

Hmmm, when Marc asked you to put the device in listen only mode, I think he meant that you set it on the device side (i.e. flag NCT6694_CAN_SETTING_CTRL1_MON) and not on the driver side. If you set CAN_CTRLMODE_LISTENONLY flag, that will be reported in the netlink interface. So you should not change that flag.

ACK

Quoted 2 lines

But before that, did you check the datasheet? Don't you have a device flag to actually turn the device off (e.g. sleep mode)?

Please test that the ifup -> ifdown -> ifup sequence works properly, even on a busy bus and on a bus without with a 2nd CAN station that is sending and you are the only receiver.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#20 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Dear Marc,

Thank you for reviewing,

Marc Kleine-Budde <[email protected]> 於 2025年2月27日 週四 下午10:17寫道:

Quoted 14 lines · Vincent Mailhol
Quoted 11 lines

drivers/net/can/usb/nct6694_canfd.c line 599

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+static int nct6694_can_stop(struct net_device *ndev)
+{
+   struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+   priv->can.ctrlmode = CAN_CTRLMODE_LISTENONLY;

Hmmm, when Marc asked you to put the device in listen only mode, I think he meant that you set it on the device side (i.e. flag NCT6694_CAN_SETTING_CTRL1_MON) and not on the driver side. If you set CAN_CTRLMODE_LISTENONLY flag, that will be reported in the netlink interface. So you should not change that flag.

Quoted 9 lines

ACK

Quoted 2 lines

But before that, did you check the datasheet? Don't you have a device flag to actually turn the device off (e.g. sleep mode)?

Please test that the ifup -> ifdown -> ifup sequence works properly, even on a busy bus and on a bus without with a 2nd CAN station that is sending and you are the only receiver.

Understood.

Best regards,
Ming

#21 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

In reply to Ming Yu
Quoted 233 lines · Ming Yu

This driver supports Socket CANFD functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                         |   1 +
 drivers/net/can/usb/Kconfig         |  11 +
 drivers/net/can/usb/Makefile        |   1 +
 drivers/net/can/usb/nct6694_canfd.c | 799 ++++++++++++++++++++++++++++
 4 files changed, 812 insertions(+)
 create mode 100644 drivers/net/can/usb/nct6694_canfd.c
MAINTAINERS +1 -0
index 1327e7a6e507..8aa611504172 100644
@@ -16925,6 +16925,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/usb/nct6694_canfd.c
 F:	include/linux/mfd/nct6694.h
 
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/net/can/usb/Kconfig +11 -0
index 9dae0c71a2e1..759e724a67cf 100644
@@ -133,6 +133,17 @@ config CAN_MCBA_USB
 	  This driver supports the CAN BUS Analyzer interface
 	  from Microchip (http://www.microchip.com/development-tools/).
 
+config CAN_NCT6694
+	tristate "Nuvoton NCT6694 Socket CANfd support"
+	depends on MFD_NCT6694
+	select CAN_RX_OFFLOAD
+	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_PEAK_USB
 	tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
 	help
drivers/net/can/usb/Makefile +1 -0
index 8b11088e9a59..fcafb1ac262e 100644
@@ -11,5 +11,6 @@ obj-$(CONFIG_CAN_F81604) += f81604.o
 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
 obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
+obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o
 obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
 obj-$(CONFIG_CAN_UCAN) += ucan.o
drivers/net/can/usb/nct6694_canfd.c new file +170 -0
new file mode 100644
index 000000000000..d97fce5cdf32
@@ -0,0 +1,799 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Nuvoton NCT6694 Socket CANfd driver based on USB interface.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#include <linux/can/dev.h>
+#include <linux/can/rx-offload.h>
+#include <linux/ethtool.h>
+#include <linux/irqdomain.h>
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/platform_device.h>
+
+#define DRVNAME "nct6694_canfd"
+
+/* 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		0x00
+#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		0x01
+#define NCT6694_CAN_INFORMATION_SEL	0x00
+
+/* Command 02h - CAN Event */
+#define NCT6694_CAN_EVENT		0x02
+#define NCT6694_CAN_EVENT_SEL(idx, mask)	\
+	((idx ? 0x80 : 0x00) | ((mask) & 0x7F))
+
+#define NCT6694_CAN_EVENT_MASK		GENMASK(5, 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_DATA_IN	BIT(7)	/* Read-clear*/
+
+/* Command 10h - CAN Deliver */
+#define NCT6694_CAN_DELIVER		0x10
+#define NCT6694_CAN_DELIVER_SEL(buf_cnt)	\
+	((buf_cnt) & 0xFF)
+
+/* Command 11h - CAN Receive */
+#define NCT6694_CAN_RECEIVE		0x11
+#define NCT6694_CAN_RECEIVE_SEL(idx, buf_cnt)	\
+	((idx ? 0x80 : 0x00) | ((buf_cnt) & 0x7F))
+
+#define NCT6694_CAN_FRAME_TAG(idx)	(0xC0 | (idx))
+#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 = 0,
+	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 = 0,
+	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 {
+	u8 err;
+	u8 status;
+	u8 tx_evt;
+	u8 rx_evt;
+	u8 rec;
+	u8 tec;
+	u8 reserved[2];
+};
+
+struct __packed nct6694_can_frame {
+	u8 tag;
+	u8 flag;
+	u8 reserved;
+	u8 length;
+	__le32 id;
+	u8 data[CANFD_MAX_DLEN];
+};
+
+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 workqueue_struct *wq;
+	struct work_struct tx_work;
+	struct nct6694_can_frame tx;
+	struct nct6694_can_frame rx;
+	struct nct6694_can_event event[2];
+	struct can_berr_counter bec;
+};
+
+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++;

drivers/net/can/usb/nct6694_canfd.c line 180

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+}
+
+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");

Please make it a netdev_dbg()

Quoted 26 lines
drivers/net/can/usb/nct6694_canfd.c new file +26 -0
+
+	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;
+	const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_RECEIVE,
+		.sel = NCT6694_CAN_RECEIVE_SEL(ndev->dev_port, 1),
+		.len = cpu_to_le16(sizeof(*frame))
+	};
+	struct sk_buff *skb;
+	int ret;

drivers/net/can/usb/nct6694_canfd.c line 216

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+
+	ret = nct6694_read_msg(priv->nct6694, &cmd_hd, frame);
+	if (ret)
+		return;
+
+	if (frame->flag & NCT6694_CAN_FRAME_FLAG_FD) {
+		struct canfd_frame *cfd;
+
+		skb = alloc_canfd_skb(priv->ndev, &cfd);
+		if (!skb)

stats->rx_dropped++;

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+			return;
+
+		cfd->can_id = le32_to_cpu(frame->id);
+		cfd->len = canfd_sanitize_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)

drivers/net/can/usb/nct6694_canfd.c line 233

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+			cfd->flags |= CANFD_BRS;
+		if (frame->flag & NCT6694_CAN_FRAME_FLAG_ERR)
+			cfd->flags |= CANFD_ESI;
+
+		memcpy(cfd->data, frame->data, cfd->len);
+	} else {
+		struct can_frame *cf;
+
+		skb = alloc_can_skb(priv->ndev, &cf);
+		if (!skb)

stats->rx_dropped++;

Quoted 19 lines
drivers/net/can/usb/nct6694_canfd.c new file +19 -0
+			return;
+
+		cf->can_id = le32_to_cpu(frame->id);
+		cf->len = can_cc_dlc2len(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;
+		else
+			memcpy(cf->data, frame->data, cf->len);
+	}
+
+	nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+					struct can_berr_counter *bec)
+{

drivers/net/can/usb/nct6694_canfd.c line 262

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+	*bec = priv->bec;
+
+	return 0;
+}
+
+static void nct6694_can_handle_state_change(struct net_device *ndev,
+					    enum can_state new_state)
+{

I just replied to one of your questions of the last series:

| https://lore.kernel.org/[email protected]

Quoted 36 lines
drivers/net/can/usb/nct6694_canfd.c new file +36 -0
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+	struct can_berr_counter bec;
+	struct can_frame *cf;
+	struct sk_buff *skb;
+
+	skb = alloc_can_err_skb(ndev, &cf);
+
+	nct6694_can_get_berr_counter(ndev, &bec);
+
+	switch (new_state) {
+	case CAN_STATE_ERROR_ACTIVE:
+		priv->can.can_stats.error_warning++;
+		priv->can.state = CAN_STATE_ERROR_ACTIVE;
+		if (cf)
+			cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
+		break;
+	case CAN_STATE_ERROR_WARNING:
+		priv->can.can_stats.error_warning++;
+		priv->can.state = CAN_STATE_ERROR_WARNING;
+		if (cf) {
+			cf->can_id |= CAN_ERR_CRTL;
+			if (bec.txerr > bec.rxerr)
+				cf->data[1] = CAN_ERR_CRTL_TX_WARNING;
+			else
+				cf->data[1] = CAN_ERR_CRTL_RX_WARNING;
+			cf->data[6] = bec.txerr;
+			cf->data[7] = bec.rxerr;
+		}
+		break;
+	case CAN_STATE_ERROR_PASSIVE:
+		priv->can.can_stats.error_passive++;
+		priv->can.state = CAN_STATE_ERROR_PASSIVE;
+		if (cf) {
+			cf->can_id |= CAN_ERR_CRTL;
+			cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
+			if (bec.txerr >= CAN_ERROR_PASSIVE_THRESHOLD)

drivers/net/can/usb/nct6694_canfd.c line 308

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+				cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
+			cf->data[6] = bec.txerr;
+			cf->data[7] = bec.rxerr;
+		}
+		break;
+	case CAN_STATE_BUS_OFF:
+		priv->can.state = CAN_STATE_BUS_OFF;
+		priv->can.can_stats.bus_off++;
+		if (cf)
+			cf->can_id |= CAN_ERR_BUSOFF;

no need for can_free_echo_skb() and netif_stop_queue()

drivers/net/can/usb/nct6694_canfd.c line 316

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+		can_free_echo_skb(ndev, 0, NULL);
+		netif_stop_queue(ndev);
 
+		can_bus_off(ndev);
+		break;
+	default:
+		break;
+	}
+

if (skb)

Quoted 34 lines
drivers/net/can/usb/nct6694_canfd.c new file +34 -0
+	nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_handle_state_errors(struct net_device *ndev, u8 status)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+	if (status == NCT6694_CAN_EVT_STS_ERROR_ACTIVE &&
+	    priv->can.state != CAN_STATE_ERROR_ACTIVE) {
+		netdev_dbg(ndev, "Error, entered active state\n");
+		nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_ACTIVE);
+	}
+
+	if (status == NCT6694_CAN_EVT_STS_WARNING &&
+	    priv->can.state != CAN_STATE_ERROR_WARNING) {
+		netdev_dbg(ndev, "Error, entered warning state\n");
+		nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_WARNING);
+	}
+
+	if (status == NCT6694_CAN_EVT_STS_ERROR_PASSIVE &&
+	    priv->can.state != CAN_STATE_ERROR_PASSIVE) {
+		netdev_dbg(ndev, "Error, entered passive state\n");
+		nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_PASSIVE);
+	}
+
+	if (status == NCT6694_CAN_EVT_STS_BUS_OFF &&
+	    priv->can.state != CAN_STATE_BUS_OFF) {
+		netdev_dbg(ndev, "Error, entered bus-off state\n");
+		nct6694_can_handle_state_change(ndev, CAN_STATE_BUS_OFF);
+	}
+}
+
+static void nct6694_can_handle_bus_err(struct net_device *ndev, u8 bus_err)
+{

drivers/net/can/usb/nct6694_canfd.c line 360

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	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);

make it: "if (cf)" instead of "if (skb)" then interacting with the "cf".

Quoted 70 lines
drivers/net/can/usb/nct6694_canfd.c new file +70 -0
+	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;
+	}
+
+	if (skb)
+		nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+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;
+
+	stats->tx_bytes += can_rx_offload_get_echo_skb_queue_tail(&priv->offload,
+								  0, NULL);
+	stats->tx_packets++;
+	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 *event = &priv->event[ndev->dev_port];
+	const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_EVENT,
+		.sel = NCT6694_CAN_EVENT_SEL(ndev->dev_port, NCT6694_CAN_EVENT_MASK),
+		.len = cpu_to_le16(sizeof(priv->event))

drivers/net/can/usb/nct6694_canfd.c line 440

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	};
+	irqreturn_t handled = IRQ_NONE;
+	int ret;
+
+	ret = nct6694_read_msg(priv->nct6694, &cmd_hd, priv->event);
+	if (ret < 0)
+		return handled;
+
+	if (event->rx_evt & NCT6694_CAN_EVT_RX_DATA_IN) {
+		nct6694_can_rx(ndev, event->rx_evt);

just for symmetry name rename nct6694_can_rx() to nct6694_can_handle_rx()

drivers/net/can/usb/nct6694_canfd.c line 450

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+		handled = IRQ_HANDLED;
+	}
+
+	if (event->rx_evt & NCT6694_CAN_EVT_RX_DATA_LOST) {
+		nct6694_can_handle_lost_msg(ndev);
+		handled = IRQ_HANDLED;
+	}
+
+	if (event->status) {
+		nct6694_can_handle_state_errors(ndev, event->status);

I think you should call nct6694_can_handle_state_change() directly and get rid of the code on nct6694_can_handle_state_errors().

drivers/net/can/usb/nct6694_canfd.c line 457

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+		handled = IRQ_HANDLED;
+	}
+
+	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
+		nct6694_can_handle_bus_err(ndev, event->err);
+		handled = IRQ_HANDLED;
+	}

The logic needs to be refined. If there was an IRQ you must say you've handled it, event if user space doesn't care for bus errors.

The better solution is to disabled event->err related IRQs in the hardware, if CAN_CTRLMODE_BERR_REPORTING is disabled, so you don't get an IRQ in the first place.

        if (event->err != NCT6694_CAN_EVT_ERR_NO_ERROR) {
                if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
                        nct6694_can_handle_bus_err(ndev, event->err);
		handled = IRQ_HANDLED;
        }

drivers/net/can/usb/nct6694_canfd.c line 460

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+
+	if (event->tx_evt & NCT6694_CAN_EVT_TX_FIFO_EMPTY) {
+		nct6694_can_tx_irq(ndev);

just for symmetry name rename nct6694_can_tx() to nct6694_can_handle_tx()

Quoted 39 lines
drivers/net/can/usb/nct6694_canfd.c new file +39 -0
+		handled = IRQ_HANDLED;
+	}
+
+	if (handled)
+		can_rx_offload_threaded_irq_finish(&priv->offload);
+
+	priv->bec.rxerr = event->rec;
+	priv->bec.txerr = event->tec;
+
+	return handled;
+}
+
+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 nct6694_can_frame *frame = &priv->tx;
+	struct net_device *ndev = priv->ndev;
+	struct net_device_stats *stats = &ndev->stats;
+	struct sk_buff *skb = priv->can.echo_skb[0];
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_DELIVER,
+		.sel = NCT6694_CAN_DELIVER_SEL(1),
+		.len = cpu_to_le16(sizeof(*frame))
+	};
+	u32 txid;
+	int err;
+
+	memset(frame, 0, sizeof(*frame));
+
+	frame->tag = NCT6694_CAN_FRAME_TAG(ndev->dev_port);
+
+	if (can_is_canfd_skb(skb)) {
+		struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
+
+		if (cfd->flags & CANFD_BRS)
+			frame->flag |= NCT6694_CAN_FRAME_FLAG_BRS;

drivers/net/can/usb/nct6694_canfd.c line 509

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+
+		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;

frame->length = canfd_sanitize_len(cfd->len);

drivers/net/can/usb/nct6694_canfd.c line 511

Quoted 2 lines
drivers/net/can/usb/nct6694_canfd.c new file +2 -0
+
+		memcpy(frame->data, cfd->data, cfd->len);

memcpy(frame->data, cfd->data, frame->length);

Quoted 12 lines
drivers/net/can/usb/nct6694_canfd.c new file +12 -0
+	} else {
+		struct can_frame *cf = (struct can_frame *)skb->data;
+
+		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;
+		}
+
+		if (cf->can_id & CAN_RTR_FLAG)
+			frame->flag |= NCT6694_CAN_FRAME_FLAG_RTR;

drivers/net/can/usb/nct6694_canfd.c line 533

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+		else
+			memcpy(frame->data, cf->data, cf->len);
+
+		frame->id = cpu_to_le32(txid);
+		frame->length = cf->len;
+	}
+
+	err = nct6694_write_msg(priv->nct6694, &cmd_hd, frame);
+	if (err) {
+		netdev_err(ndev, "%s: TX FIFO is full!\n", __func__);

The FIFO should not be full at this time. There also could be an USB error due to unlug or EMI. I think you should remove this error message.

Quoted 52 lines
drivers/net/can/usb/nct6694_canfd.c new file +52 -0
+		can_free_echo_skb(ndev, 0, NULL);
+		stats->tx_dropped++;
+		stats->tx_errors++;
+		netif_wake_queue(ndev);
+	}
+}
+
+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;
+
+	netif_stop_queue(ndev);
+	can_put_echo_skb(skb, ndev, 0, 0);
+	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);
+	const struct can_bittiming *d_bt = &priv->can.data_bittiming;
+	const struct can_bittiming *n_bt = &priv->can.bittiming;
+	struct nct6694_can_setting *setting __free(kfree) = NULL;
+	const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_SETTING,
+		.sel = ndev->dev_port,
+		.len = cpu_to_le16(sizeof(*setting))
+	};
+	int ret;
+
+	setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+	if (!setting)
+		return -ENOMEM;
+
+	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_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);
+

drivers/net/can/usb/nct6694_canfd.c line 595

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	ret = nct6694_write_msg(priv->nct6694, &cmd_hd, setting);
+	if (ret)
+		return ret;
+
+	priv->can.state = CAN_STATE_ERROR_ACTIVE;
+
+	return 0;
+}
+
+static int nct6694_can_stop(struct net_device *ndev)

static void

drivers/net/can/usb/nct6694_canfd.c line 599

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+	priv->can.ctrlmode = CAN_CTRLMODE_LISTENONLY;

As Vincent already pointed out, bring you device into listen only mode. Please add a comment why you do it, because it cannot be stopped.

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+	netif_stop_queue(ndev);
+	free_irq(ndev->irq, ndev);
+	destroy_workqueue(priv->wq);
+	can_rx_offload_disable(&priv->offload);
+	priv->can.state = CAN_STATE_STOPPED;
+	close_candev(ndev);
+

drivers/net/can/usb/nct6694_canfd.c line 616

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	return 0;
+}
+
+static int nct6694_can_set_mode(struct net_device *ndev, enum can_mode mode)
+{
+	int ret;
+
+	switch (mode) {
+	case CAN_MODE_START:
+		ret = nct6694_can_start(ndev);

if (ret)

        return ret;

netif_wake_queue(ndev);

Quoted 118 lines
drivers/net/can/usb/nct6694_canfd.c new file +118 -0
+		if (!ret && netif_queue_stopped(ndev))
+			netif_wake_queue(ndev);
+		break;
+
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	return ret;
+}
+
+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;
+	}
+
+	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 __free(kfree) = NULL;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_INFORMATION,
+		.sel = NCT6694_CAN_INFORMATION_SEL,
+		.len = cpu_to_le16(sizeof(*info))
+	};
+	int ret, can_clk;
+
+	info = kzalloc(sizeof(*info), GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+
+	ret = nct6694_read_msg(priv->nct6694, &cmd_hd, info);
+	if (ret)
+		return ret;
+
+	can_clk = le32_to_cpu(info->can_clk);
+
+	return 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_CAN0 + 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->dev_port = cell->id;
+	ndev->netdev_ops = &nct6694_can_netdev_ops;
+	ndev->ethtool_ops = &nct6694_can_ethtool_ops;
+
+	priv = netdev_priv(ndev);
+	priv->nct6694 = nct6694;
+	priv->ndev = ndev;
+

drivers/net/can/usb/nct6694_canfd.c line 744

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	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;
+	}
+
+	INIT_WORK(&priv->tx_work, nct6694_can_tx_work);
+
+	priv->can.state = CAN_STATE_STOPPED;

please remove CAN_STATE_STOPPED, it's not needed

drivers/net/can/usb/nct6694_canfd.c line 752

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+	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_supported = CAN_CTRLMODE_LOOPBACK |
+		CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+		CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Quoted 37 lines
drivers/net/can/usb/nct6694_canfd.c new file +37 -0
+
+	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 rx_offload_del;
+
+	return 0;
+
+rx_offload_del:
+	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);
+
+	unregister_candev(priv->ndev);
+	can_rx_offload_del(&priv->offload);
+	free_candev(priv->ndev);
+}
+
+static struct platform_driver nct6694_can_driver = {
+	.driver = {
+		.name	= DRVNAME,
+	},

drivers/net/can/usb/nct6694_canfd.c line 799

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	.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 <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:nct6694-can");

There should be no need for the module alias.

Marc

Quoted 5 lines

--
2.34.1

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#22 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Marc Kleine-Budde <[email protected]> 於 2025年2月28日 週五 下午6:44寫道:

Quoted 12 lines

drivers/net/can/usb/nct6694_canfd.c line 180

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+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");

Please make it a netdev_dbg()

Fix it in v9.

Quoted 30 lines
Quoted 30 lines
drivers/net/can/usb/nct6694_canfd.c new file +30 -0
+
+     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;
+     const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_RECEIVE,
+             .sel = NCT6694_CAN_RECEIVE_SEL(ndev->dev_port, 1),
+             .len = cpu_to_le16(sizeof(*frame))
+     };
+     struct sk_buff *skb;
+     int ret;
+
+     ret = nct6694_read_msg(priv->nct6694, &cmd_hd, frame);
+     if (ret)
+             return;
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 216

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+
+     if (frame->flag & NCT6694_CAN_FRAME_FLAG_FD) {
+             struct canfd_frame *cfd;
+
+             skb = alloc_canfd_skb(priv->ndev, &cfd);
+             if (!skb)

stats->rx_dropped++;

Add it in v9.

Quoted 11 lines
Quoted 11 lines
drivers/net/can/usb/nct6694_canfd.c new file +11 -0
+                     return;
+
+             cfd->can_id = le32_to_cpu(frame->id);
+             cfd->len = canfd_sanitize_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;
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 233

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+             memcpy(cfd->data, frame->data, cfd->len);
+     } else {
+             struct can_frame *cf;
+
+             skb = alloc_can_skb(priv->ndev, &cf);
+             if (!skb)

stats->rx_dropped++;

Add it in v9.

Quoted 32 lines
Quoted 19 lines
drivers/net/can/usb/nct6694_canfd.c new file +19 -0
+                     return;
+
+             cf->can_id = le32_to_cpu(frame->id);
+             cf->len = can_cc_dlc2len(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;
+             else
+                     memcpy(cf->data, frame->data, cf->len);
+     }
+
+     nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                     struct can_berr_counter *bec)
+{

drivers/net/can/usb/nct6694_canfd.c line 262

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+     *bec = priv->bec;
+
+     return 0;
+}
+
+static void nct6694_can_handle_state_change(struct net_device *ndev,
+                                         enum can_state new_state)
+{

I just replied to one of your questions of the last series:

Quoted 2 lines

| https://lore.kernel.org/[email protected]

Understood, I will make the modifications in the next patch.

Quoted 40 lines
Quoted 40 lines
drivers/net/can/usb/nct6694_canfd.c new file +40 -0
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+     struct can_berr_counter bec;
+     struct can_frame *cf;
+     struct sk_buff *skb;
+
+     skb = alloc_can_err_skb(ndev, &cf);
+
+     nct6694_can_get_berr_counter(ndev, &bec);
+
+     switch (new_state) {
+     case CAN_STATE_ERROR_ACTIVE:
+             priv->can.can_stats.error_warning++;
+             priv->can.state = CAN_STATE_ERROR_ACTIVE;
+             if (cf)
+                     cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
+             break;
+     case CAN_STATE_ERROR_WARNING:
+             priv->can.can_stats.error_warning++;
+             priv->can.state = CAN_STATE_ERROR_WARNING;
+             if (cf) {
+                     cf->can_id |= CAN_ERR_CRTL;
+                     if (bec.txerr > bec.rxerr)
+                             cf->data[1] = CAN_ERR_CRTL_TX_WARNING;
+                     else
+                             cf->data[1] = CAN_ERR_CRTL_RX_WARNING;
+                     cf->data[6] = bec.txerr;
+                     cf->data[7] = bec.rxerr;
+             }
+             break;
+     case CAN_STATE_ERROR_PASSIVE:
+             priv->can.can_stats.error_passive++;
+             priv->can.state = CAN_STATE_ERROR_PASSIVE;
+             if (cf) {
+                     cf->can_id |= CAN_ERR_CRTL;
+                     cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
+                     if (bec.txerr >= CAN_ERROR_PASSIVE_THRESHOLD)
+                             cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
+                     cf->data[6] = bec.txerr;
+                     cf->data[7] = bec.rxerr;
+             }
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 308

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+             break;
+     case CAN_STATE_BUS_OFF:
+             priv->can.state = CAN_STATE_BUS_OFF;
+             priv->can.can_stats.bus_off++;
+             if (cf)
+                     cf->can_id |= CAN_ERR_BUSOFF;

no need for can_free_echo_skb() and netif_stop_queue()

Fix these in v9.

Quoted 12 lines

drivers/net/can/usb/nct6694_canfd.c line 316

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+             can_free_echo_skb(ndev, 0, NULL);
+             netif_stop_queue(ndev);
 
+             can_bus_off(ndev);
+             break;
+     default:
+             break;
+     }
+

if (skb)

Add it in v9.

Quoted 38 lines
Quoted 38 lines
drivers/net/can/usb/nct6694_canfd.c new file +38 -0
+     nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_handle_state_errors(struct net_device *ndev, u8 status)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+     if (status == NCT6694_CAN_EVT_STS_ERROR_ACTIVE &&
+         priv->can.state != CAN_STATE_ERROR_ACTIVE) {
+             netdev_dbg(ndev, "Error, entered active state\n");
+             nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_ACTIVE);
+     }
+
+     if (status == NCT6694_CAN_EVT_STS_WARNING &&
+         priv->can.state != CAN_STATE_ERROR_WARNING) {
+             netdev_dbg(ndev, "Error, entered warning state\n");
+             nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_WARNING);
+     }
+
+     if (status == NCT6694_CAN_EVT_STS_ERROR_PASSIVE &&
+         priv->can.state != CAN_STATE_ERROR_PASSIVE) {
+             netdev_dbg(ndev, "Error, entered passive state\n");
+             nct6694_can_handle_state_change(ndev, CAN_STATE_ERROR_PASSIVE);
+     }
+
+     if (status == NCT6694_CAN_EVT_STS_BUS_OFF &&
+         priv->can.state != CAN_STATE_BUS_OFF) {
+             netdev_dbg(ndev, "Error, entered bus-off state\n");
+             nct6694_can_handle_state_change(ndev, CAN_STATE_BUS_OFF);
+     }
+}
+
+static void nct6694_can_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;
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 360

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+     if (bus_err == NCT6694_CAN_EVT_ERR_NO_ERROR)
+             return;
+
+     priv->can.can_stats.bus_error++;
+
+     skb = alloc_can_err_skb(ndev, &cf);

make it: "if (cf)" instead of "if (skb)" then interacting with the "cf".

Fix it in v9.

Quoted 74 lines
Quoted 74 lines
drivers/net/can/usb/nct6694_canfd.c new file +74 -0
+     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;
+     }
+
+     if (skb)
+             nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+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;
+
+     stats->tx_bytes += can_rx_offload_get_echo_skb_queue_tail(&priv->offload,
+                                                               0, NULL);
+     stats->tx_packets++;
+     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 *event = &priv->event[ndev->dev_port];
+     const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_EVENT,
+             .sel = NCT6694_CAN_EVENT_SEL(ndev->dev_port, NCT6694_CAN_EVENT_MASK),
+             .len = cpu_to_le16(sizeof(priv->event))
+     };
+     irqreturn_t handled = IRQ_NONE;
+     int ret;
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 440

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+     ret = nct6694_read_msg(priv->nct6694, &cmd_hd, priv->event);
+     if (ret < 0)
+             return handled;
+
+     if (event->rx_evt & NCT6694_CAN_EVT_RX_DATA_IN) {
+             nct6694_can_rx(ndev, event->rx_evt);

just for symmetry name rename nct6694_can_rx() to nct6694_can_handle_rx()

Fix it in v9.

Quoted 5 lines
Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+             handled = IRQ_HANDLED;
+     }
+
+     if (event->rx_evt & NCT6694_CAN_EVT_RX_DATA_LOST) {
+             nct6694_can_handle_lost_msg(ndev);
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 450

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+             handled = IRQ_HANDLED;
+     }
+
+     if (event->status) {
+             nct6694_can_handle_state_errors(ndev, event->status);

I think you should call nct6694_can_handle_state_change() directly and get rid of the code on nct6694_can_handle_state_errors().

Fix it in v9.

Quoted 15 lines

drivers/net/can/usb/nct6694_canfd.c line 457

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+             handled = IRQ_HANDLED;
+     }
+
+     if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
+             nct6694_can_handle_bus_err(ndev, event->err);
+             handled = IRQ_HANDLED;
+     }

The logic needs to be refined. If there was an IRQ you must say you've handled it, event if user space doesn't care for bus errors.

The better solution is to disabled event->err related IRQs in the hardware, if CAN_CTRLMODE_BERR_REPORTING is disabled, so you don't get an IRQ in the first place.

Quoted 6 lines
        if (event->err != NCT6694_CAN_EVT_ERR_NO_ERROR) {
                if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
                        nct6694_can_handle_bus_err(ndev, event->err);
                handled = IRQ_HANDLED;
        }

Fix it in v9.

Quoted 6 lines

drivers/net/can/usb/nct6694_canfd.c line 460

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+
+     if (event->tx_evt & NCT6694_CAN_EVT_TX_FIFO_EMPTY) {
+             nct6694_can_tx_irq(ndev);

just for symmetry name rename nct6694_can_tx() to nct6694_can_handle_tx()

Fix it in v9.

Quoted 43 lines
Quoted 43 lines
drivers/net/can/usb/nct6694_canfd.c new file +43 -0
+             handled = IRQ_HANDLED;
+     }
+
+     if (handled)
+             can_rx_offload_threaded_irq_finish(&priv->offload);
+
+     priv->bec.rxerr = event->rec;
+     priv->bec.txerr = event->tec;
+
+     return handled;
+}
+
+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 nct6694_can_frame *frame = &priv->tx;
+     struct net_device *ndev = priv->ndev;
+     struct net_device_stats *stats = &ndev->stats;
+     struct sk_buff *skb = priv->can.echo_skb[0];
+     static const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_DELIVER,
+             .sel = NCT6694_CAN_DELIVER_SEL(1),
+             .len = cpu_to_le16(sizeof(*frame))
+     };
+     u32 txid;
+     int err;
+
+     memset(frame, 0, sizeof(*frame));
+
+     frame->tag = NCT6694_CAN_FRAME_TAG(ndev->dev_port);
+
+     if (can_is_canfd_skb(skb)) {
+             struct canfd_frame *cfd = (struct canfd_frame *)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;
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 509

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+             } 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;

frame->length = canfd_sanitize_len(cfd->len);

Fix it in v9.

Quoted 5 lines

drivers/net/can/usb/nct6694_canfd.c line 511

Quoted 2 lines
drivers/net/can/usb/nct6694_canfd.c new file +2 -0
+
+             memcpy(frame->data, cfd->data, cfd->len);

memcpy(frame->data, cfd->data, frame->length);

Fix it in v9.

Quoted 17 lines
Quoted 17 lines
drivers/net/can/usb/nct6694_canfd.c new file +17 -0
+     } else {
+             struct can_frame *cf = (struct can_frame *)skb->data;
+
+             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;
+             }
+
+             if (cf->can_id & CAN_RTR_FLAG)
+                     frame->flag |= NCT6694_CAN_FRAME_FLAG_RTR;
+             else
+                     memcpy(frame->data, cf->data, cf->len);
+
+             frame->id = cpu_to_le32(txid);
+             frame->length = cf->len;
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 533

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+     }
+
+     err = nct6694_write_msg(priv->nct6694, &cmd_hd, frame);
+     if (err) {
+             netdev_err(ndev, "%s: TX FIFO is full!\n", __func__);

The FIFO should not be full at this time. There also could be an USB error due to unlug or EMI. I think you should remove this error message.

Fix it in v9.

Quoted 56 lines
Quoted 56 lines
drivers/net/can/usb/nct6694_canfd.c new file +56 -0
+             can_free_echo_skb(ndev, 0, NULL);
+             stats->tx_dropped++;
+             stats->tx_errors++;
+             netif_wake_queue(ndev);
+     }
+}
+
+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;
+
+     netif_stop_queue(ndev);
+     can_put_echo_skb(skb, ndev, 0, 0);
+     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);
+     const struct can_bittiming *d_bt = &priv->can.data_bittiming;
+     const struct can_bittiming *n_bt = &priv->can.bittiming;
+     struct nct6694_can_setting *setting __free(kfree) = NULL;
+     const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_SETTING,
+             .sel = ndev->dev_port,
+             .len = cpu_to_le16(sizeof(*setting))
+     };
+     int ret;
+
+     setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+     if (!setting)
+             return -ENOMEM;
+
+     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_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, &cmd_hd, setting);
+     if (ret)
+             return ret;
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 595

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+     priv->can.state = CAN_STATE_ERROR_ACTIVE;
+
+     return 0;
+}
+
+static int nct6694_can_stop(struct net_device *ndev)

static void

Fix it in v9.

Quoted 8 lines

drivers/net/can/usb/nct6694_canfd.c line 599

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+     priv->can.ctrlmode = CAN_CTRLMODE_LISTENONLY;

As Vincent already pointed out, bring you device into listen only mode. Please add a comment why you do it, because it cannot be stopped.

Okay! I will add the comment in the next patch.

Quoted 19 lines
Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+     netif_stop_queue(ndev);
+     free_irq(ndev->irq, ndev);
+     destroy_workqueue(priv->wq);
+     can_rx_offload_disable(&priv->offload);
+     priv->can.state = CAN_STATE_STOPPED;
+     close_candev(ndev);
+

drivers/net/can/usb/nct6694_canfd.c line 616

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+     return 0;
+}
+
+static int nct6694_can_set_mode(struct net_device *ndev, enum can_mode mode)
+{
+     int ret;
+
+     switch (mode) {
+     case CAN_MODE_START:
+             ret = nct6694_can_start(ndev);

if (ret)

Quoted 4 lines
        return ret;

netif_wake_queue(ndev);

Fix it in v9.

Quoted 122 lines
Quoted 122 lines
drivers/net/can/usb/nct6694_canfd.c new file +122 -0
+             if (!ret && netif_queue_stopped(ndev))
+                     netif_wake_queue(ndev);
+             break;
+
+     default:
+             return -EOPNOTSUPP;
+     }
+
+     return ret;
+}
+
+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;
+     }
+
+     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 __free(kfree) = NULL;
+     static const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_INFORMATION,
+             .sel = NCT6694_CAN_INFORMATION_SEL,
+             .len = cpu_to_le16(sizeof(*info))
+     };
+     int ret, can_clk;
+
+     info = kzalloc(sizeof(*info), GFP_KERNEL);
+     if (!info)
+             return -ENOMEM;
+
+     ret = nct6694_read_msg(priv->nct6694, &cmd_hd, info);
+     if (ret)
+             return ret;
+
+     can_clk = le32_to_cpu(info->can_clk);
+
+     return 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_CAN0 + 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->dev_port = cell->id;
+     ndev->netdev_ops = &nct6694_can_netdev_ops;
+     ndev->ethtool_ops = &nct6694_can_ethtool_ops;
+
+     priv = netdev_priv(ndev);
+     priv->nct6694 = nct6694;
+     priv->ndev = ndev;
+
+     can_clk = nct6694_can_get_clock(priv);
+     if (can_clk < 0) {
+             ret = dev_err_probe(&pdev->dev, can_clk,
+                                 "Failed to get clock\n");
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 744

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+             goto free_candev;
+     }
+
+     INIT_WORK(&priv->tx_work, nct6694_can_tx_work);
+
+     priv->can.state = CAN_STATE_STOPPED;

please remove CAN_STATE_STOPPED, it's not needed

Fix it in v9.

Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 752

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
Quoted 4 lines

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Quoted 41 lines
Quoted 41 lines
drivers/net/can/usb/nct6694_canfd.c new file +41 -0
+
+     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 rx_offload_del;
+
+     return 0;
+
+rx_offload_del:
+     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);
+
+     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,
+};
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 799

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+module_platform_driver(nct6694_can_driver);
+
+MODULE_DESCRIPTION("USB-CAN FD driver for NCT6694");
+MODULE_AUTHOR("Ming Yu <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:nct6694-can");

There should be no need for the module alias.

Remove it in v9.

Thanks,
Ming

#23 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

On 17.03.2025 10:24:11, Ming Yu wrote:
[...]

Quoted 14 lines
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 752

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
Quoted 4 lines

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Quoted 5 lines

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Where do you evaluate if the user has configured CAN_CTRLMODE_FD or not?

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#24 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Marc Kleine-Budde <[email protected]> 於 2025年3月17日 週一 下午5:21寫道:

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

drivers/net/can/usb/nct6694_canfd.c line 752

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Quoted 8 lines
Quoted 5 lines

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Where do you evaluate if the user has configured CAN_CTRLMODE_FD or not?

Sorry, I was previously confused about our device's control mode. I will use can_set_static_ctrlmode() to set CAN_FD mode in the next patch.

Thanks,
Ming

#25 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Quoted 22 lines · Ming Yu

Marc Kleine-Budde <[email protected]> 於 2025年3月17日 週一 下午5:21寫道:

Quoted 21 lines
Quoted 20 lines
Quoted 10 lines

drivers/net/can/usb/nct6694_canfd.c line 752

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
Quoted 4 lines

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Quoted 6 lines
Quoted 2 lines

Where do you evaluate if the user has configured CAN_CTRLMODE_FD or not?

Sorry, I was previously confused about our device's control mode. I will use can_set_static_ctrlmode() to set CAN_FD mode in the next patch.

Does your device support CAN-CC only mode? Does your device support to switch between CAN-CC only and CAN-FD mode?

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#26 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Marc Kleine-Budde <[email protected]> 於 2025年3月27日 週四 上午1:41寫道:

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 15 lines
Quoted 10 lines

drivers/net/can/usb/nct6694_canfd.c line 752

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
Quoted 4 lines

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Quoted 5 lines

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Where do you evaluate if the user has configured CAN_CTRLMODE_FD or not?

Quoted 7 lines
Quoted 3 lines

Sorry, I was previously confused about our device's control mode. I will use can_set_static_ctrlmode() to set CAN_FD mode in the next patch.

Does your device support CAN-CC only mode? Does your device support to switch between CAN-CC only and CAN-FD mode?

Our device supports both CAN-CC and CAN-FD mode.

#27 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Quoted 29 lines · Ming Yu

Marc Kleine-Budde <[email protected]> 於 2025年3月27日 週四 上午1:41寫道:

Quoted 28 lines
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

Review

Quoted 9 lines
(quoted diff) +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Quoted 8 lines
Quoted 5 lines

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Where do you evaluate if the user has configured CAN_CTRLMODE_FD or not?

Sorry, I was previously confused about our device's control mode. I will use can_set_static_ctrlmode() to set CAN_FD mode in the next patch.

Quoted 5 lines
Quoted 3 lines

Does your device support CAN-CC only mode? Does your device support to switch between CAN-CC only and CAN-FD mode?

Our device supports both CAN-CC and CAN-FD mode.

This doesn't answer my question:

Does your device support CAN-CC only mode?

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#28 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Marc Kleine-Budde <[email protected]> 於 2025年3月27日 週四 下午3:25寫道:

Quoted 35 lines · Ming Yu
Quoted 33 lines

Marc Kleine-Budde <[email protected]> 於 2025年3月27日 週四 上午1:41寫道:

Quoted 31 lines
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 10 lines

Review

Quoted 9 lines
(quoted diff) +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
Quoted 4 lines

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Quoted 6 lines
Quoted 2 lines

Where do you evaluate if the user has configured CAN_CTRLMODE_FD or not?

Sorry, I was previously confused about our device's control mode. I will use can_set_static_ctrlmode() to set CAN_FD mode in the next patch.

Does your device support CAN-CC only mode? Does your device support to switch between CAN-CC only and CAN-FD mode?

Quoted 6 lines
Quoted 1 line

Our device supports both CAN-CC and CAN-FD mode.

This doesn't answer my question:

Does your device support CAN-CC only mode?

It can dynamically switch between CAN-CC and CAN-FD mode when trasmitting or receiving, depending on whether the nct6694_can_frame passs the flag with NCT6694_CAN_FRAME_FLAG_FD.

Thanks,
Ming

#29 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Quoted 38 lines · Ming Yu

Marc Kleine-Budde <[email protected]> 於 2025年3月27日 週四 下午3:25寫道:

Quoted 37 lines · Ming Yu
Quoted 34 lines

Marc Kleine-Budde <[email protected]> 於 2025年3月27日 週四 上午1:41寫道:

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 15 lines
Quoted 10 lines

Review

Quoted 9 lines
(quoted diff) +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
Quoted 4 lines

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Quoted 5 lines

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Where do you evaluate if the user has configured CAN_CTRLMODE_FD or not?

Quoted 7 lines
Quoted 3 lines

Sorry, I was previously confused about our device's control mode. I will use can_set_static_ctrlmode() to set CAN_FD mode in the next patch.

Does your device support CAN-CC only mode? Does your device support to switch between CAN-CC only and CAN-FD mode?

Our device supports both CAN-CC and CAN-FD mode.

Quoted 7 lines
Quoted 3 lines

This doesn't answer my question:

Does your device support CAN-CC only mode?

It can dynamically switch between CAN-CC and CAN-FD mode when trasmitting or receiving, depending on whether the nct6694_can_frame passs the flag with NCT6694_CAN_FRAME_FLAG_FD.

Ok, but what about the receive path? Does the device support CAN-CC only mode? Will it throw an error, if it receives a CAN-FD frame?

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#30 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Marc Kleine-Budde <[email protected]> 於 2025年3月28日 週五 下午3:22寫道:

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

Review

Quoted 9 lines
(quoted diff) +8 -0
+     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_supported = CAN_CTRLMODE_LOOPBACK |
+             CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+             CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;

Does your device run in CAN-FD mode all the time? If so, please use can_set_static_ctrlmode() to set it after priv->can.ctrlmode_supported and remove CAN_CTRLMODE_FD from ctrlmode_supported.

Quoted 8 lines
Quoted 5 lines

Our device is designed to allow users to dynamically switch between Classical CAN and CAN-FD mode via ip link set ... fd on/off. Therefore, CAN_CTRLMODE_FD needs to remain in ctrlmode_supported, and can_set_static_ctrlmode() is not suitable in this case. Please let me know if you have any concerns about this approach.

Where do you evaluate if the user has configured CAN_CTRLMODE_FD or not?

Sorry, I was previously confused about our device's control mode. I will use can_set_static_ctrlmode() to set CAN_FD mode in the next patch.

Quoted 5 lines
Quoted 3 lines

Does your device support CAN-CC only mode? Does your device support to switch between CAN-CC only and CAN-FD mode?

Our device supports both CAN-CC and CAN-FD mode.

This doesn't answer my question:

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Does your device support CAN-CC only mode?

It can dynamically switch between CAN-CC and CAN-FD mode when trasmitting or receiving, depending on whether the nct6694_can_frame passs the flag with NCT6694_CAN_FRAME_FLAG_FD.

Ok, but what about the receive path? Does the device support CAN-CC only mode? Will it throw an error, if it receives a CAN-FD frame?

No, it can receive both CAN-CC and CAN-FD frames, if the hardware receives a CAN-FD frame, the firmware will set the NCT6694_CAN_FRAME_FLAG_FD flag.

#31 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Quoted 6 lines · Ming Yu
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

Does your device support CAN-CC only mode?

It can dynamically switch between CAN-CC and CAN-FD mode when trasmitting or receiving, depending on whether the nct6694_can_frame passs the flag with NCT6694_CAN_FRAME_FLAG_FD.

Quoted 6 lines
Quoted 2 lines

Ok, but what about the receive path? Does the device support CAN-CC only mode? Will it throw an error, if it receives a CAN-FD frame?

No, it can receive both CAN-CC and CAN-FD frames, if the hardware receives a CAN-FD frame, the firmware will set the NCT6694_CAN_FRAME_FLAG_FD flag.

Ok, then set the CAN-FD ctrl mode static.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#32 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

In reply to Ming Yu

On 25.02.2025 16:16:41, Ming Yu wrote:
[...]

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +0 -0
new file mode 100644
index 000000000000..d97fce5cdf32

[...]

Quoted 23 lines
drivers/net/can/usb/nct6694_canfd.c new file +23 -0
+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,
+};

[...]

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+static int nct6694_can_start(struct net_device *ndev)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+	const struct can_bittiming *d_bt = &priv->can.data_bittiming;
+	const struct can_bittiming *n_bt = &priv->can.bittiming;
+	struct nct6694_can_setting *setting __free(kfree) = NULL;
+	const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_SETTING,
+		.sel = ndev->dev_port,

drivers/net/can/usb/nct6694_canfd.c line 575

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+		.len = cpu_to_le16(sizeof(*setting))
+	};
+	int ret;
+
+	setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+	if (!setting)
+		return -ENOMEM;
+
+	setting->nbr = cpu_to_le32(n_bt->bitrate);
+	setting->dbr = cpu_to_le32(d_bt->bitrate);

I just noticed one thing that needs clarification/documentation.

You have nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const, but only pass the bit rates to your device.

Do the bit timing const really reflect the HW limitations of your device?

Are you sure your device uses the same algorithm as the kernel and calculates the same bit timing parameters as the kernel, so that the values given to the user space reflects the bit timing parameter chosen by your device?

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+
+	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
+		setting->ctrl1 |= cpu_to_le16(NCT6694_CAN_SETTING_CTRL1_MON);
+
+	if (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)

drivers/net/can/usb/nct6694_canfd.c line 593

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+		setting->ctrl1 |= cpu_to_le16(NCT6694_CAN_SETTING_CTRL1_LBCK);
+
+	ret = nct6694_write_msg(priv->nct6694, &cmd_hd, setting);
+	if (ret)
+		return ret;
+
+	priv->can.state = CAN_STATE_ERROR_ACTIVE;
+
+	return 0;
+}

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#33 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Marc Kleine-Budde <[email protected]> 於 2025年3月17日 週一 下午8:01寫道:

Quoted 1 line

...

Quoted 53 lines
Quoted 23 lines
drivers/net/can/usb/nct6694_canfd.c new file +23 -0
+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,
+};

[...]

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+static int nct6694_can_start(struct net_device *ndev)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+     const struct can_bittiming *d_bt = &priv->can.data_bittiming;
+     const struct can_bittiming *n_bt = &priv->can.bittiming;
+     struct nct6694_can_setting *setting __free(kfree) = NULL;
+     const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_SETTING,
+             .sel = ndev->dev_port,

drivers/net/can/usb/nct6694_canfd.c line 575

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+             .len = cpu_to_le16(sizeof(*setting))
+     };
+     int ret;
+
+     setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+     if (!setting)
+             return -ENOMEM;
+
+     setting->nbr = cpu_to_le32(n_bt->bitrate);
+     setting->dbr = cpu_to_le32(d_bt->bitrate);

I just noticed one thing that needs clarification/documentation.

You have nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const, but only pass the bit rates to your device.

Quoted 8 lines

Do the bit timing const really reflect the HW limitations of your device?

Are you sure your device uses the same algorithm as the kernel and calculates the same bit timing parameters as the kernel, so that the values given to the user space reflects the bit timing parameter chosen by your device?

Originally, I only intended to provide NBR and DBR for user configuration. In the next patch, I will add code to configure NBTP(Nominal Bit Timing Prescaler) and DBTP(Data Bit Timing Prescaler) based on the setting of nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const.

Thanks,
Ming

#34 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Quoted 37 lines · Ming Yu

Marc Kleine-Budde <[email protected]> 於 2025年3月17日 週一 下午8:01寫道:

Quoted 27 lines
Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+static int nct6694_can_start(struct net_device *ndev)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+     const struct can_bittiming *d_bt = &priv->can.data_bittiming;
+     const struct can_bittiming *n_bt = &priv->can.bittiming;
+     struct nct6694_can_setting *setting __free(kfree) = NULL;
+     const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_SETTING,
+             .sel = ndev->dev_port,

drivers/net/can/usb/nct6694_canfd.c line 575

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+             .len = cpu_to_le16(sizeof(*setting))
+     };
+     int ret;
+
+     setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+     if (!setting)
+             return -ENOMEM;
+
+     setting->nbr = cpu_to_le32(n_bt->bitrate);
+     setting->dbr = cpu_to_le32(d_bt->bitrate);

I just noticed one thing that needs clarification/documentation.

You have nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const, but only pass the bit rates to your device.

Quoted 8 lines

Do the bit timing const really reflect the HW limitations of your device?

Are you sure your device uses the same algorithm as the kernel and calculates the same bit timing parameters as the kernel, so that the values given to the user space reflects the bit timing parameter chosen by your device?

Quoted 5 lines

Originally, I only intended to provide NBR and DBR for user configuration. In the next patch, I will add code to configure NBTP(Nominal Bit Timing Prescaler) and DBTP(Data Bit Timing Prescaler) based on the setting of nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const.

Sounds good, but this doesn't answer my questions:

You have nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const, but only pass the bit rates and the prescaler to your device.

Do the bit timing const really reflect the HW limitations of your device?

Are you sure your device uses the same algorithm as the kernel and calculates the same bit timing parameters as the kernel, so that the values given to the user space reflects the bit timing parameter chosen by your device?

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
author
#35 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Marc Kleine-Budde <[email protected]> 於 2025年3月27日 週四 上午1:35寫道:

Quoted 43 lines
Quoted 37 lines
Quoted 28 lines
Quoted 11 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+static int nct6694_can_start(struct net_device *ndev)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+     const struct can_bittiming *d_bt = &priv->can.data_bittiming;
+     const struct can_bittiming *n_bt = &priv->can.bittiming;
+     struct nct6694_can_setting *setting __free(kfree) = NULL;
+     const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_SETTING,
+             .sel = ndev->dev_port,

drivers/net/can/usb/nct6694_canfd.c line 575

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+             .len = cpu_to_le16(sizeof(*setting))
+     };
+     int ret;
+
+     setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+     if (!setting)
+             return -ENOMEM;
+
+     setting->nbr = cpu_to_le32(n_bt->bitrate);
+     setting->dbr = cpu_to_le32(d_bt->bitrate);

I just noticed one thing that needs clarification/documentation.

You have nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const, but only pass the bit rates to your device.

Quoted 8 lines

Do the bit timing const really reflect the HW limitations of your device?

Are you sure your device uses the same algorithm as the kernel and calculates the same bit timing parameters as the kernel, so that the values given to the user space reflects the bit timing parameter chosen by your device?

Quoted 5 lines

Originally, I only intended to provide NBR and DBR for user configuration. In the next patch, I will add code to configure NBTP(Nominal Bit Timing Prescaler) and DBTP(Data Bit Timing Prescaler) based on the setting of nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const.

Quoted 6 lines

Sounds good, but this doesn't answer my questions:

You have nct6694_can_bittiming_nominal_const and nct6694_can_bittiming_data_const, but only pass the bit rates and the prescaler to your device.

I understand.
The prescaler field is used to calculate sjw, brp, prop_seg, phase_seg1 and phase_seg2. I will update the code in the next patch.

Thanks,
Ming

#36 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

In reply to Ming Yu

Le 25/02/2025 à 09:16, Ming Yu a écrit :

Quoted 4 lines

This driver supports Socket CANFD functionality for NCT6694 MFD device based on USB interface.

...

drivers/net/can/usb/nct6694_canfd.c line 719

Quoted 12 lines
drivers/net/can/usb/nct6694_canfd.c new file +12 -0
+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_CAN0 + cell->id);
+	if (!irq)
+		return irq;

Should irq_dispose_mapping() be caled in the error handling path and in the remove function?

Quoted 55 lines
drivers/net/can/usb/nct6694_canfd.c new file +55 -0
+
+	ndev = alloc_candev(sizeof(struct nct6694_can_priv), 1);
+	if (!ndev)
+		return -ENOMEM;
+
+	ndev->irq = irq;
+	ndev->flags |= IFF_ECHO;
+	ndev->dev_port = cell->id;
+	ndev->netdev_ops = &nct6694_can_netdev_ops;
+	ndev->ethtool_ops = &nct6694_can_ethtool_ops;
+
+	priv = netdev_priv(ndev);
+	priv->nct6694 = nct6694;
+	priv->ndev = ndev;
+
+	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;
+	}
+
+	INIT_WORK(&priv->tx_work, nct6694_can_tx_work);
+
+	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_supported = CAN_CTRLMODE_LOOPBACK |
+		CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
+		CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO;
+
+	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 rx_offload_del;
+
+	return 0;
+
+rx_offload_del:
+	can_rx_offload_del(&priv->offload);
+free_candev:
+	free_candev(ndev);
+	return ret;

drivers/net/can/usb/nct6694_canfd.c line 784

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+}
+
+static void nct6694_can_remove(struct platform_device *pdev)
+{
+	struct nct6694_can_priv *priv = platform_get_drvdata(pdev);
+
+	unregister_candev(priv->ndev);
+	can_rx_offload_del(&priv->offload);
+	free_candev(priv->ndev);
+}

...

CJ

author
#37 lore raw

Re: [PATCH v8 4/7] can: Add Nuvoton NCT6694 CANFD support

Dear Christophe,

Thank you for reviewing,

Christophe JAILLET <[email protected]> 於 2025年3月27日 週四 上午5:56寫道:

Quoted 17 lines

drivers/net/can/usb/nct6694_canfd.c line 719

Quoted 13 lines
drivers/net/can/usb/nct6694_canfd.c new file +12 -0
+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_CAN0 + cell->id);
+     if (!irq)
+             return irq;

Should irq_dispose_mapping() be caled in the error handling path and in the remove function?

I think you're right. I'll add this function in the next patch.

Best regards,
Ming

author
#38 lore raw

PATCH v8 5/7 watchdog: Add Nuvoton NCT6694 WDT support

In reply to Ming Yu

This driver supports Watchdog timer functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                    |   1 +
 drivers/watchdog/Kconfig       |  11 ++
 drivers/watchdog/Makefile      |   1 +
 drivers/watchdog/nct6694_wdt.c | 298 +++++++++++++++++++++++++++++++++
 4 files changed, 311 insertions(+)
 create mode 100644 drivers/watchdog/nct6694_wdt.c
MAINTAINERS +1 -0
index 8aa611504172..4889b618abef 100644
@@ -16926,6 +16926,7 @@ F:	drivers/gpio/gpio-nct6694.c
 F:	drivers/i2c/busses/i2c-nct6694.c
 F:	drivers/mfd/nct6694.c
 F:	drivers/net/can/usb/nct6694_canfd.c
+F:	drivers/watchdog/nct6694_wdt.c
 F:	include/linux/mfd/nct6694.h
 
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/watchdog/Kconfig +11 -0
index f81705f8539a..4c4f826368c4 100644
@@ -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
drivers/watchdog/Makefile +1 -0
index 8411626fa162..de2a04ff8a92 100644
@@ -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
drivers/watchdog/nct6694_wdt.c new file +298 -0

298 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#39 lore raw

PATCH v8 6/7 hwmon: Add Nuvoton NCT6694 HWMON support

In reply to Ming Yu

This driver supports Hardware monitor functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                   |   1 +
 drivers/hwmon/Kconfig         |  10 +
 drivers/hwmon/Makefile        |   1 +
 drivers/hwmon/nct6694-hwmon.c | 949 ++++++++++++++++++++++++++++++++++
 4 files changed, 961 insertions(+)
 create mode 100644 drivers/hwmon/nct6694-hwmon.c
MAINTAINERS +1 -0
index 4889b618abef..a9eda4530b07 100644
@@ -16923,6 +16923,7 @@ M:	Ming Yu <[email protected]>
 L:	[email protected]
 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/usb/nct6694_canfd.c
drivers/hwmon/Kconfig +10 -0
index 4cbaba15d86e..8a7de798a30a 100644
@@ -1638,6 +1638,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
drivers/hwmon/Makefile +1 -0
index b7ef0f0562d3..c73898704421 100644
@@ -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
drivers/hwmon/nct6694-hwmon.c new file +949 -0

949 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#40 lore raw

PATCH v8 7/7 rtc: Add Nuvoton NCT6694 RTC support

In reply to Ming Yu

This driver supports RTC functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS               |   1 +
 drivers/rtc/Kconfig       |  10 ++
 drivers/rtc/Makefile      |   1 +
 drivers/rtc/rtc-nct6694.c | 286 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 298 insertions(+)
 create mode 100644 drivers/rtc/rtc-nct6694.c
MAINTAINERS +1 -0
index a9eda4530b07..7eba4ffdc877 100644
@@ -16927,6 +16927,7 @@ F:	drivers/hwmon/nct6694-hwmon.c
 F:	drivers/i2c/busses/i2c-nct6694.c
 F:	drivers/mfd/nct6694.c
 F:	drivers/net/can/usb/nct6694_canfd.c
+F:	drivers/rtc/rtc-nct6694.c
 F:	drivers/watchdog/nct6694_wdt.c
 F:	include/linux/mfd/nct6694.h
 
drivers/rtc/Kconfig +10 -0
index 0bbbf778ecfa..248425bf26f3 100644
@@ -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
drivers/rtc/Makefile +1 -0
index 489b4ab07068..d0d6f4a4972e 100644
@@ -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
drivers/rtc/rtc-nct6694.c new file +286 -0

286 lines not on this page · raw message

Signature · 1 line
2.34.1