Skip to contentSkip to conversation

Add Nuvoton NCT6694 MFD drivers

PATCH Superseded Ming Yu 5 participants 26 messages Feb 7, 2025 – Apr 8, 2025

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

v7 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Ming Yu

author
#0 lore raw

v7 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 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         | 458 ++++++++++++++
 drivers/hwmon/Kconfig               |  10 +
 drivers/hwmon/Makefile              |   1 +
 drivers/hwmon/nct6694-hwmon.c       | 947 ++++++++++++++++++++++++++++
 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               | 373 +++++++++++
 drivers/net/can/usb/Kconfig         |  11 +
 drivers/net/can/usb/Makefile        |   1 +
 drivers/net/can/usb/nct6694_canfd.c | 809 ++++++++++++++++++++++++
 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      | 295 +++++++++
 include/linux/mfd/nct6694.h         | 102 +++
 23 files changed, 3525 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 v7 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       | 373 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/nct6694.h | 102 ++++++++++
 5 files changed, 502 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 +373 -0

373 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 v7 1/7] mfd: Add core driver for Nuvoton NCT6694

Le 07/02/2025 à 08:44, Ming Yu a écrit :

Quoted 11 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 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.

...

Quoted 1 line

+static struct irq_chip nct6694_irq_chip = {

This could be const.

(I'm working on a serie that should constify struct irq_chip, so this one would already be done)

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

drivers/mfd/nct6694.c line 259

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

...

CJ

author
#3 lore raw

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

Dear Christophe,

Thank you for reviewing,

Christophe JAILLET <[email protected]> 於 2025年2月8日 週六 上午2:37寫道:

Quoted 14 lines · Ming Yu
Quoted 11 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 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.

Quoted 9 lines

...

Quoted 1 line

+static struct irq_chip nct6694_irq_chip = {

This could be const.

(I'm working on a serie that should constify struct irq_chip, so this one would already be done)

Okay, I will fix it in the next patch.

Best regards,
Ming

author
#4 lore raw

PATCH v7 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 | 458 ++++++++++++++++++++++++++++++++++++
 4 files changed, 472 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 +458 -0

458 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v7 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
#6 lore raw

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

Le 07/02/2025 à 08:44, Ming Yu a écrit :

Quoted 3 lines

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

...

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

Quoted 13 lines
drivers/i2c/busses/i2c-nct6694.c new file +13 -0
+static int nct6694_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
+{
+	struct nct6694_i2c_data *data = adap->algo_data;
+	struct nct6694_i2c_deliver *deliver = &data->deliver;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_I2C_MOD,
+		.cmd = NCT6694_I2C_DELIVER,
+		.sel = NCT6694_I2C_DELIVER_SEL,
+		.len = cpu_to_le16(sizeof(*deliver))
+	};
+	int ret, i;
+
+	for (i = 0; i < num ; i++) {

Tiny tiny nitpick: unneeded extra space after num

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

Quoted 1 line
drivers/i2c/busses/i2c-nct6694.c new file +1 -0
+		struct i2c_msg *msg_temp = &msgs[i];

...

CJ

author
#7 lore raw

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

Dear Christophe,

Thank you for reviewing,

Christophe JAILLET <[email protected]> 於 2025年2月8日 週六 上午3:06寫道:

Quoted 8 lines · Ming Yu
Quoted 3 lines

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

...

Quoted 16 lines

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

Quoted 13 lines
drivers/i2c/busses/i2c-nct6694.c new file +13 -0
+static int nct6694_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
+{
+     struct nct6694_i2c_data *data = adap->algo_data;
+     struct nct6694_i2c_deliver *deliver = &data->deliver;
+     static const struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_I2C_MOD,
+             .cmd = NCT6694_I2C_DELIVER,
+             .sel = NCT6694_I2C_DELIVER_SEL,
+             .len = cpu_to_le16(sizeof(*deliver))
+     };
+     int ret, i;
+
+     for (i = 0; i < num ; i++) {

Tiny tiny nitpick: unneeded extra space after num

Okay, I will fix it in the next patch.

Best regards,
Ming

author
#8 lore raw

PATCH v7 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 | 809 ++++++++++++++++++++++++++++
 4 files changed, 822 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 +809 -0

809 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

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

Quoted 63 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 | 809 ++++++++++++++++++++++++++++
 4 files changed, 822 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 +0 -0
new file mode 100644
index 000000000000..a7527020ff71

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

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
@@ -0,0 +1,809 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 Socket CANfd driver based on USB interface.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */

/* net-dev style multiline
* comments look like this
*/

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

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

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

/* net-dev style multiline
* comments look like this
*/

or remove the comment altogether as it's stating the obvious :)

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

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+#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_CAN0	0xC0
+#define NCT6694_CAN_FRAME_TAG_CAN1	0xC1

in "include/linux/mfd/nct6694.h" it's CAN1 and CAN2, can you make it "0" based everywhere?

what about making it:

#define NCT6694_CAN_FRAME_TAG_CAN(idx) (0xC0 | (idx))

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

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

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

can you use ndev->dev_port instead?

Quoted 97 lines
drivers/net/can/usb/nct6694_canfd.c new file +97 -0
+};
+
+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;
+	struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_RECEIVE,
+		.sel = NCT6694_CAN_RECEIVE_SEL(priv->can_idx, 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;
+

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+		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 = min_t(u8, frame->length, CAN_MAX_DLEN);

use can_cc_dlc2len()

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

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

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

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

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

It seems you don't have dedicated RX and TX states, so call nct6694_can_get_berr_counter() and use can_state_get_by_berr_counter() to get the states. Then basically do that what what mcp251xfd_handle_cerrif() does, starting with "new_state = max(tx_state, rx_state);"

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

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

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

missing check:

if (skb)

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

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+	nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_tx_irq(struct net_device *ndev)
+{
+	struct net_device_stats *stats = &ndev->stats;
+
+	stats->tx_bytes += can_get_echo_skb(ndev, 0, NULL);

use can_rx_offload_get_echo_skb_queue_tail() here

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

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	struct nct6694_can_event *evt = priv->event;
+	struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_EVENT,
+		.sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, NCT6694_CAN_EVENT_MASK),
+		.len = cpu_to_le16(sizeof(priv->event))
+	};
+	irqreturn_t handled = IRQ_NONE;
+	int can_idx = priv->can_idx;
+	int ret;

it would make sense to have a event pointer here instead of the can_idx?

        const struct nct6694_can_event *event = &priv->event[priv->can_idx];
Quoted 20 lines
drivers/net/can/usb/nct6694_canfd.c new file +20 -0
+
+	ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+	if (ret < 0)
+		return handled;
+
+	if (priv->event[can_idx].rx_evt & NCT6694_CAN_EVT_RX_DATA_IN) {
+		nct6694_can_rx(ndev, priv->event[can_idx].rx_evt);
+		handled = IRQ_HANDLED;
+	}
+
+	if (priv->event[can_idx].rx_evt & NCT6694_CAN_EVT_RX_DATA_LOST) {
+		nct6694_can_handle_lost_msg(ndev);
+		handled = IRQ_HANDLED;
+	}
+
+	if (priv->event[can_idx].status) {
+		nct6694_can_handle_state_errors(ndev, priv->event[can_idx].status);
+		handled = IRQ_HANDLED;
+	}
+

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
+		nct6694_can_handle_bus_err(ndev, priv->event[can_idx].err);
+		handled = IRQ_HANDLED;
+	}
+
+	if (handled)
+		can_rx_offload_threaded_irq_finish(&priv->offload);
+
+	if (priv->event[can_idx].tx_evt & NCT6694_CAN_EVT_TX_FIFO_EMPTY)
+		nct6694_can_tx_irq(ndev);

Move this in front of the handled check and add "handled = IRQ_HANDLED;" to the if-block.

Quoted 21 lines
drivers/net/can/usb/nct6694_canfd.c new file +21 -0
+
+	priv->bec.rxerr = priv->event[can_idx].rec;
+	priv->bec.txerr = priv->event[can_idx].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))

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	};
+	u32 txid;
+	int err;
+
+	memset(frame, 0, sizeof(*frame));
+
+	if (priv->can_idx == 0)
+		frame->tag = NCT6694_CAN_FRAME_TAG_CAN0;
+	else
+		frame->tag = NCT6694_CAN_FRAME_TAG_CAN1;
        frame->tag = NCT6694_CAN_FRAME_TAG_CAN(priv->can_idx);
Quoted 64 lines
drivers/net/can/usb/nct6694_canfd.c new file +64 -0
+
+	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)
+{

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+	struct nct6694_can_setting *setting;
+	struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_SETTING,
+		.sel = priv->can_idx,
+		.len = cpu_to_le16(sizeof(*setting))
+	};
+	const struct can_bittiming *n_bt = &priv->can.bittiming;
+	const struct can_bittiming *d_bt = &priv->can.data_bittiming;

nitpick:
move these 2 right after priv and d_bt first.

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+	int ret;
+
+	setting = kzalloc(sizeof(*setting), GFP_KERNEL);
+	if (!setting)

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

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

We should move this check into the infrastructure. Please only check for CAN_CTRLMODE_FD_NON_ISO.

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

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+
+exit:
+	kfree(setting);
+	return ret;
+}
+
+static int nct6694_can_stop(struct net_device *ndev)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+

How does the device know, that it should leave the CAN bus? Is there a message you can send to it? If not put it at least into listen only mode.

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

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+	netif_stop_queue(ndev);
+	free_irq(ndev->irq, ndev);
+	destroy_workqueue(priv->wq);
+	priv->wq = NULL;

no need to set wq to NULL

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

Quoted 2 lines
drivers/net/can/usb/nct6694_canfd.c new file +2 -0
+	priv->can.state = CAN_STATE_STOPPED;
+	can_rx_offload_disable(&priv->offload);

Please change move can_rx_offload_disable() in front of "priv->can.state = CAN_STATE_STOPPED".

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

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

please add error checking

Quoted 118 lines
drivers/net/can/usb/nct6694_canfd.c new file +118 -0
+		netif_wake_queue(ndev);
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+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;
+	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) {
+		kfree(info);
+		return ret;
+	}
+
+	can_clk = le32_to_cpu(info->can_clk);
+	kfree(info);
+
+	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_CAN1 + cell->id);
+	if (!irq)
+		return irq;
+
+	ndev = alloc_candev(sizeof(struct nct6694_can_priv), 1);
+	if (!ndev)
+		return -ENOMEM;
+
+	ndev->irq = irq;
+	ndev->flags |= IFF_ECHO;
+	ndev->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);

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	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_idx = cell->id;
+	priv->can.state = CAN_STATE_STOPPED;

The device is allocated with CAN_STATE_STOPPED set, can be removed.

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

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

nitpick: one space in front of "|" please

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

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	platform_set_drvdata(pdev, priv);
+	SET_NETDEV_DEV(priv->ndev, &pdev->dev);
+
+	ret = register_candev(priv->ndev);
+	if (ret)
+		goto del_rx_offload;
+
+	return 0;
+
+del_rx_offload:

nitpick: please rename to rx_offload_del or can_rx_offload_del

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

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

The workqueue has already been destroyed in nct6694_can_stop(), so more work should be pending.

Quoted 21 lines
drivers/net/can/usb/nct6694_canfd.c new file +19 -0
+	unregister_candev(priv->ndev);
+	can_rx_offload_del(&priv->offload);
+	free_candev(priv->ndev);
+}
+
+static struct platform_driver nct6694_can_driver = {
+	.driver = {
+		.name	= DRVNAME,
+	},
+	.probe		= nct6694_can_probe,
+	.remove		= nct6694_can_remove,
+};
+
+module_platform_driver(nct6694_can_driver);
+
+MODULE_DESCRIPTION("USB-CAN FD driver for NCT6694");
+MODULE_AUTHOR("Ming Yu <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:nct6694-can");
Signature · 1 line
2.34.1

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   |
#10 lore raw

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

Quoted 1 line · Marc Kleine-Budde

On 07.02.2025 15:44:59, Ming Yu wrote:

(...)

Quoted 11 lines

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

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

What is the reason to use a work queue here? xmit() is not a hard IRQ. Also, the other USB CAN devices just directly send the USB message in their xmit() without the need to rely on such worker.

Sorry if this was discussed in the past, I can not remember if this question has already been raised.

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	return NETDEV_TX_OK;
+}

(...)

Yours sincerely,
Vincent Mailhol

#11 lore raw

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

Quoted 21 lines · Vincent Mailhol
Quoted 1 line · Marc Kleine-Budde

On 07.02.2025 15:44:59, Ming Yu wrote:

(...)

Quoted 11 lines

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

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

What is the reason to use a work queue here? xmit() is not a hard IRQ. Also, the other USB CAN devices just directly send the USB message in their xmit() without the need to rely on such worker.

Quoted 2 lines

Sorry if this was discussed in the past, I can not remember if this question has already been raised.

AFAICS xmit uses nct6694_write_msg(), which uses several usb_bulk_msg(), that can only be used from task context.

You can build a chain of usb_fill_bulk_urb(), usb_submit_urb(), callbacks instead, but that's hard to read compared to blocking usb_bulk_msg()s.

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
#12 lore raw

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

In reply to Vincent Mailhol

Dear Vincent,

Thank you for reviewing,
this part is as Marc mentioned.

Best regards,
Ming

Vincent Mailhol <[email protected]> 於 2025年2月7日 週五 下午11:01寫道:

Quoted 33 lines · Marc Kleine-Budde
Quoted 1 line · Ming Yu

(...)

Quoted 11 lines

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

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

What is the reason to use a work queue here? xmit() is not a hard IRQ. Also, the other USB CAN devices just directly send the USB message in their xmit() without the need to rely on such worker.

Sorry if this was discussed in the past, I can not remember if this question has already been raised.

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    return NETDEV_TX_OK;
+}

(...)

Yours sincerely,
Vincent Mailhol

author
#13 lore raw

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

In reply to Marc Kleine-Budde

Dear Marc,

Thank you for reviewing,

Marc Kleine-Budde <[email protected]> 於 2025年2月7日 週五 下午8:15寫道:

Quoted 12 lines

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

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 Socket CANfd driver based on USB interface.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */

/* net-dev style multiline
* comments look like this
*/

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

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

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

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

/* net-dev style multiline
* comments look like this
*/

Quoted 2 lines

or remove the comment altogether as it's stating the obvious :)

Okay, I will remove it in the next patch.

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

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

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+#define NCT6694_CAN_RECEIVE_SEL(idx, buf_cnt)        \
+     ((idx ? 0x80 : 0x00) | ((buf_cnt) & 0x7F))
+
+#define NCT6694_CAN_FRAME_TAG_CAN0   0xC0
+#define NCT6694_CAN_FRAME_TAG_CAN1   0xC1

in "include/linux/mfd/nct6694.h" it's CAN1 and CAN2, can you make it "0" based everywhere?

Understood. I will update these macros in the header.

Quoted 4 lines

what about making it:

#define NCT6694_CAN_FRAME_TAG_CAN(idx) (0xC0 | (idx))

Fix it in the v8.

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

...

Quoted 13 lines

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

Quoted 12 lines
drivers/net/can/usb/nct6694_canfd.c new file +12 -0
+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;
+     unsigned char can_idx;
Quoted 2 lines

can you use ndev->dev_port instead?

Fix it in the v8.

Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+};
+

...

Quoted 37 lines
Quoted 37 lines
drivers/net/can/usb/nct6694_canfd.c new file +37 -0
+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;
+     struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_RECEIVE,
+             .sel = NCT6694_CAN_RECEIVE_SEL(priv->can_idx, 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;
+
Quoted 9 lines

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

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+             skb = alloc_can_skb(priv->ndev, &cf);
+             if (!skb)
+                     return;
+
+             cf->can_id = le32_to_cpu(frame->id);
+             cf->len = min_t(u8, frame->length, CAN_MAX_DLEN);

use can_cc_dlc2len()

Fix it in the v8.

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

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

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

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

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+static void nct6694_can_handle_state_errors(struct net_device *ndev, u8 status)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);

It seems you don't have dedicated RX and TX states, so call nct6694_can_get_berr_counter() and use can_state_get_by_berr_counter() to get the states. Then basically do that what what mcp251xfd_handle_cerrif() does, starting with "new_state = max(tx_state, rx_state);"

Excuse me, do you mean that nct6694_can_handle_state_change() the flow should like v5?
https://lore.kernel.org/linux-can/CAMZ6RqLHEoukxDfV33iDWXjM1baK922QnWSkOP01VzZ0S_9H8g@mail.gmail.com/

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

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

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

missing check:

Quoted 2 lines

if (skb)

Fix it in the v8.

Quoted 11 lines

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

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+     nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_tx_irq(struct net_device *ndev)
+{
+     struct net_device_stats *stats = &ndev->stats;
+
+     stats->tx_bytes += can_get_echo_skb(ndev, 0, NULL);

use can_rx_offload_get_echo_skb_queue_tail() here

Fix it in the v8.

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

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+     struct nct6694_can_event *evt = priv->event;
+     struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_EVENT,
+             .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, NCT6694_CAN_EVENT_MASK),
+             .len = cpu_to_le16(sizeof(priv->event))
+     };
+     irqreturn_t handled = IRQ_NONE;
+     int can_idx = priv->can_idx;
+     int ret;

it would make sense to have a event pointer here instead of the can_idx?

Quoted 2 lines
        const struct nct6694_can_event *event = &priv->event[priv->can_idx];

Fix it in the v8.

Quoted 25 lines
Quoted 25 lines
drivers/net/can/usb/nct6694_canfd.c new file +25 -0
+
+     ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+     if (ret < 0)
+             return handled;
+
+     if (priv->event[can_idx].rx_evt & NCT6694_CAN_EVT_RX_DATA_IN) {
+             nct6694_can_rx(ndev, priv->event[can_idx].rx_evt);
+             handled = IRQ_HANDLED;
+     }
+
+     if (priv->event[can_idx].rx_evt & NCT6694_CAN_EVT_RX_DATA_LOST) {
+             nct6694_can_handle_lost_msg(ndev);
+             handled = IRQ_HANDLED;
+     }
+
+     if (priv->event[can_idx].status) {
+             nct6694_can_handle_state_errors(ndev, priv->event[can_idx].status);
+             handled = IRQ_HANDLED;
+     }
+
+     if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
+             nct6694_can_handle_bus_err(ndev, priv->event[can_idx].err);
+             handled = IRQ_HANDLED;
+     }
+
Quoted 9 lines

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

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+     if (handled)
+             can_rx_offload_threaded_irq_finish(&priv->offload);
+
+     if (priv->event[can_idx].tx_evt & NCT6694_CAN_EVT_TX_FIFO_EMPTY)
+             nct6694_can_tx_irq(ndev);

Move this in front of the handled check and add "handled = IRQ_HANDLED;" to the if-block.

Fix it in the v8.

Quoted 25 lines
Quoted 25 lines
drivers/net/can/usb/nct6694_canfd.c new file +25 -0
+
+     priv->bec.rxerr = priv->event[can_idx].rec;
+     priv->bec.txerr = priv->event[can_idx].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;
+
Quoted 9 lines

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

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+     memset(frame, 0, sizeof(*frame));
+
+     if (priv->can_idx == 0)
+             frame->tag = NCT6694_CAN_FRAME_TAG_CAN0;
+     else
+             frame->tag = NCT6694_CAN_FRAME_TAG_CAN1;
        frame->tag = NCT6694_CAN_FRAME_TAG_CAN(priv->can_idx);

Fix it in the v8.

Quoted 69 lines
Quoted 69 lines
drivers/net/can/usb/nct6694_canfd.c new file +69 -0
+
+     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);
+     struct nct6694_can_setting *setting;
+     struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_SETTING,
Quoted 9 lines

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

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+             .sel = priv->can_idx,
+             .len = cpu_to_le16(sizeof(*setting))
+     };
+     const struct can_bittiming *n_bt = &priv->can.bittiming;
+     const struct can_bittiming *d_bt = &priv->can.data_bittiming;

nitpick:
move these 2 right after priv and d_bt first.

Fix it in the v8.

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

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

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -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) &&
+         priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)

We should move this check into the infrastructure. Please only check for CAN_CTRLMODE_FD_NON_ISO.

Fix it in the v8.

Quoted 15 lines
Quoted 15 lines
drivers/net/can/usb/nct6694_canfd.c new file +15 -0
+             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)
+             goto exit;
+
+     priv->can.state = CAN_STATE_ERROR_ACTIVE;
+
+exit:
+     kfree(setting);
+     return ret;
+}
Quoted 9 lines

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

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);
+

How does the device know, that it should leave the CAN bus? Is there a message you can send to it? If not put it at least into listen only mode.

My device only resets upon receiving the initial command, so can I assign priv->can.ctrlmode = CAN_CTRLMODE_LISTENONLY directly?

Quoted 7 lines

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

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+     netif_stop_queue(ndev);
+     free_irq(ndev->irq, ndev);
+     destroy_workqueue(priv->wq);
+     priv->wq = NULL;

no need to set wq to NULL

Fix it in the v8.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+     priv->can.state = CAN_STATE_STOPPED;
+     can_rx_offload_disable(&priv->offload);

Please change move can_rx_offload_disable() in front of "priv->can.state = CAN_STATE_STOPPED".

Fix it in the v8.

Quoted 4 lines
Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+     close_candev(ndev);
+
+     return 0;
+}
Quoted 9 lines

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

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

please add error checking

Fix it in the v8.

Quoted 7 lines
Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+             netif_wake_queue(ndev);
+             return 0;
+     default:
+             return -EOPNOTSUPP;
+     }
+}
+

...

Quoted 32 lines
Quoted 32 lines
drivers/net/can/usb/nct6694_canfd.c new file +32 -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_CAN1 + cell->id);
+     if (!irq)
+             return irq;
+
+     ndev = alloc_candev(sizeof(struct nct6694_can_priv), 1);
+     if (!ndev)
+             return -ENOMEM;
+
+     ndev->irq = irq;
+     ndev->flags |= IFF_ECHO;
+     ndev->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;
Quoted 9 lines

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

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

The device is allocated with CAN_STATE_STOPPED set, can be removed.

Fix it in the v8.

Quoted 16 lines

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

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

nitpick: one space in front of "|" please

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.

Fix it in the v8.

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

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

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+     if (ret)
+             goto del_rx_offload;
+
+     return 0;
+
+del_rx_offload:

nitpick: please rename to rx_offload_del or can_rx_offload_del

Fix it in the v8.

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

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

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+static void nct6694_can_remove(struct platform_device *pdev)
+{
+     struct nct6694_can_priv *priv = platform_get_drvdata(pdev);
+
+     cancel_work_sync(&priv->tx_work);

The workqueue has already been destroyed in nct6694_can_stop(), so more work should be pending.

Fix it in the v8.

Quoted 5 lines

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

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+     unregister_candev(priv->ndev);
+     can_rx_offload_del(&priv->offload);
+     free_candev(priv->ndev);
+}
+

Best regards,
Ming

#14 lore raw

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

Quoted 10 lines · Ming Yu
Quoted 9 lines

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

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+static void nct6694_can_handle_state_errors(struct net_device *ndev, u8 status)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);

It seems you don't have dedicated RX and TX states, so call nct6694_can_get_berr_counter() and use can_state_get_by_berr_counter() to get the states. Then basically do that what what mcp251xfd_handle_cerrif() does, starting with "new_state = max(tx_state, rx_state);"

Quoted 3 lines

Excuse me, do you mean that nct6694_can_handle_state_change() the flow should like v5?
https://lore.kernel.org/linux-can/CAMZ6RqLHEoukxDfV33iDWXjM1baK922QnWSkOP01VzZ0S_9H8g@mail.gmail.com/

The handling of CAMZ6RqLHEoukxDfV33iDWXjM1baK922QnWSkOP01VzZ0S_9H8g@mail.gmail.com in v5 was better, but there were some questions by Vincent...

So let's continue the discussion from v5 here:

Quoted 6 lines
Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+static void nct6694_can_handle_state_change(struct net_device *ndev,
+                                           u8 status)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       enum can_state new_state = priv->can.state;
+       enum can_state rx_state, tx_state;
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+       struct can_berr_counter bec;
+       struct can_frame *cf;
+       struct sk_buff *skb;
+
+       nct6694_can_get_berr_counter(ndev, &bec);
+       can_state_get_by_berr_counter(ndev, &bec, &tx_state, &rx_state);

Here, you set up tx_state and rx_state...

remove the switch (status)...

Quoted 9 lines
Quoted 9 lines
(quoted diff) +9 -0
+       switch (status) {
+       case NCT6694_CAN_EVT_STS_ERROR_ACTIVE:
+               new_state = CAN_STATE_ERROR_ACTIVE;
+               break;
+       case NCT6694_CAN_EVT_STS_ERROR_PASSIVE:
+               new_state = CAN_STATE_ERROR_PASSIVE;
+               break;
+       case NCT6694_CAN_EVT_STS_BUS_OFF:
+               new_state = CAN_STATE_BUS_OFF;
Quoted 8 lines

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

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+               break;
+       case NCT6694_CAN_EVT_STS_WARNING:
+               new_state = CAN_STATE_ERROR_WARNING;
+               break;
+       default:
+               netdev_err(ndev, "Receive unknown CAN status event.\n");
+               return;
+       }

replace it by:

	new_state = max(tx_state, rx_state);
Quoted 7 lines

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

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+
+       /* state hasn't changed */
+       if (new_state == priv->can.state)
+               return;
+
+       skb = alloc_can_err_skb(ndev, &cf);
+

remove this VVV

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       tx_state = bec.txerr >= bec.rxerr ? new_state : 0;
+       rx_state = bec.txerr <= bec.rxerr ? new_state : 0;
            ^^^
Quoted 11 lines

... but you never used the values returned by can_state_get_by_berr_counter() and just overwrote the tx and rx state.

What is the logic here? Why do you need to manually adjust those two values? Isn't the logic in can_change_state() sufficient?

Quoted 3 lines
(quoted diff) +3 -0
+       can_change_state(ndev, cf, tx_state, rx_state);
+
+       if (new_state == CAN_STATE_BUS_OFF) {

if (priv->can.state == CAN_STATE_BUS_OFF) {

Quoted 10 lines

Same for the new_state. The function can_change_state() calculate the new state from tx_state and rx_state and save it under can_priv->state. But here, you do your own calculation.

Only keep one of the two. If your device already tells you the state, then fine! Just use the information from your device and do not use can_change_state(). Here, you are doing double work resulting in a weird mix.

what does your device do when it goes into bus off?

Quoted 7 lines

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

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+               can_bus_off(ndev);
+       } else if (skb) {
+               cf->can_id |= CAN_ERR_CNT;
+               cf->data[6] = bec.txerr;
+               cf->data[7] = bec.rxerr;
+       }
+

if (skb)

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       nct6694_can_rx_offload(&priv->offload, skb);
+}

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
#15 lore raw

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

In reply to Marc Kleine-Budde

Hi Marc,

Marc Kleine-Budde <[email protected]> 於 2025年2月7日 週五 下午8:15寫道:

Quoted 18 lines
Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+static irqreturn_t nct6694_can_irq(int irq, void *data)
+{
+     struct net_device *ndev = data;
+     struct nct6694_can_priv *priv = netdev_priv(ndev);

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+     struct nct6694_can_event *evt = priv->event;
+     struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_EVENT,
+             .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, NCT6694_CAN_EVENT_MASK),
+             .len = cpu_to_le16(sizeof(priv->event))
+     };
+     irqreturn_t handled = IRQ_NONE;
+     int can_idx = priv->can_idx;
+     int ret;

it would make sense to have a event pointer here instead of the can_idx?

Quoted 2 lines
        const struct nct6694_can_event *event = &priv->event[priv->can_idx];

The CAN Event command always returns 16bytes: the first 8 bytes correspond to the CAN0 event, and the last 8 bytes correspond to the CAN1 event. Therefore, the event pointer here refers to both event buffers.

Thanks,
Ming

#16 lore raw

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

Quoted 21 lines · Ming Yu

Hi Marc,

Marc Kleine-Budde <[email protected]> 於 2025年2月7日 週五 下午8:15寫道:

Quoted 18 lines
Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+static irqreturn_t nct6694_can_irq(int irq, void *data)
+{
+     struct net_device *ndev = data;
+     struct nct6694_can_priv *priv = netdev_priv(ndev);

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+     struct nct6694_can_event *evt = priv->event;
+     struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_EVENT,
+             .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, NCT6694_CAN_EVENT_MASK),
+             .len = cpu_to_le16(sizeof(priv->event))
+     };
+     irqreturn_t handled = IRQ_NONE;
+     int can_idx = priv->can_idx;
+     int ret;

it would make sense to have a event pointer here instead of the can_idx?

Quoted 6 lines
Quoted 2 lines
        const struct nct6694_can_event *event = &priv->event[priv->can_idx];

The CAN Event command always returns 16bytes: the first 8 bytes correspond to the CAN0 event, and the last 8 bytes correspond to the CAN1 event. Therefore, the event pointer here refers to both event buffers.

Yes, but in the following code uses "priv->event[can_idx]" several times, this is why I proposed to have a dedicated "struct nct6694_can_event *event" variable.

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
#17 lore raw

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

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

Quoted 25 lines · Ming Yu
Quoted 23 lines

Hi Marc,

Marc Kleine-Budde <[email protected]> 於 2025年2月7日 週五 下午8:15寫道:

Quoted 20 lines
Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+static irqreturn_t nct6694_can_irq(int irq, void *data)
+{
+     struct net_device *ndev = data;
+     struct nct6694_can_priv *priv = netdev_priv(ndev);

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

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+     struct nct6694_can_event *evt = priv->event;
+     struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_EVENT,
+             .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, NCT6694_CAN_EVENT_MASK),
+             .len = cpu_to_le16(sizeof(priv->event))
+     };
+     irqreturn_t handled = IRQ_NONE;
+     int can_idx = priv->can_idx;
+     int ret;

it would make sense to have a event pointer here instead of the can_idx?

        const struct nct6694_can_event *event = &priv->event[priv->can_idx];
Quoted 9 lines
Quoted 4 lines

The CAN Event command always returns 16bytes: the first 8 bytes correspond to the CAN0 event, and the last 8 bytes correspond to the CAN1 event. Therefore, the event pointer here refers to both event buffers.

Yes, but in the following code uses "priv->event[can_idx]" several times, this is why I proposed to have a dedicated "struct nct6694_can_event *event" variable.

Okay, I'll update the code.

Thanks,
Ming

#18 lore raw

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

In reply to Ming Yu

Le 07/02/2025 à 08:44, Ming Yu a écrit :

Quoted 2 lines

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

...

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

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+static int nct6694_can_start(struct net_device *ndev)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+	struct nct6694_can_setting *setting;

Could be:
struct nct6694_can_setting *setting __free(kfree) = NULL;

to slightly simplify code below.

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+	struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_CAN_MOD,
+		.cmd = NCT6694_CAN_SETTING,
+		.sel = priv->can_idx,
+		.len = cpu_to_le16(sizeof(*setting))
+	};

...

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

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+static int nct6694_can_get_clock(struct nct6694_can_priv *priv)
+{
+	struct nct6694_can_information *info;

Could be:
struct nct6694_can_information *info __free(kfree) = NULL;

to slightly simplify code below.

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

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

...

CJ

author
#19 lore raw

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

Dear Christophe,

Thank you for reviewing,
I will make the change in the next patch.

Best regards,
Ming

Christophe JAILLET <[email protected]> 於 2025年2月8日 週六 上午3:18寫道:

Quoted 52 lines · Ming Yu
Quoted 2 lines

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

...

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

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+static int nct6694_can_start(struct net_device *ndev)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+     struct nct6694_can_setting *setting;

Could be:
struct nct6694_can_setting *setting __free(kfree) = NULL;

to slightly simplify code below.

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+     struct nct6694_cmd_header cmd_hd = {
+             .mod = NCT6694_CAN_MOD,
+             .cmd = NCT6694_CAN_SETTING,
+             .sel = priv->can_idx,
+             .len = cpu_to_le16(sizeof(*setting))
+     };

...

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

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+static int nct6694_can_get_clock(struct nct6694_can_priv *priv)
+{
+     struct nct6694_can_information *info;

Could be:
struct nct6694_can_information *info __free(kfree) = NULL;

to slightly simplify code below.

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

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

...

CJ

author
#20 lore raw

PATCH v7 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 | 295 +++++++++++++++++++++++++++++++++
 4 files changed, 308 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 +295 -0

295 lines not on this page · raw message

Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH v7 5/7] watchdog: Add Nuvoton NCT6694 WDT support

Le 07/02/2025 à 08:45, Ming Yu a écrit :

Quoted 2 lines

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

...

Quoted 25 lines
drivers/watchdog/nct6694_wdt.c new file +25 -0
+static int nct6694_wdt_probe(struct platform_device *pdev)
+{
+	const struct mfd_cell *cell = mfd_get_cell(pdev);
+	struct device *dev = &pdev->dev;
+	struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent);
+	struct nct6694_wdt_data *data;
+	struct watchdog_device *wdev;
+
+	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->msg = devm_kzalloc(dev, sizeof(union nct6694_wdt_msg),
+				 GFP_KERNEL);
+	if (!data->msg)
+		return -ENOMEM;
+
+	data->dev = dev;
+	data->nct6694 = nct6694;
+	data->wdev_idx = cell->id;
+
+	wdev = &data->wdev;
+	wdev->info = &nct6694_wdt_info;
+	wdev->ops = &nct6694_wdt_ops;
+	wdev->timeout = timeout;

drivers/watchdog/nct6694_wdt.c line 272

Quoted 10 lines
drivers/watchdog/nct6694_wdt.c new file +10 -0
+	wdev->pretimeout = pretimeout;
+	if (timeout < pretimeout) {
+		dev_warn(data->dev, "pretimeout < timeout. Setting to zero\n");
+		wdev->pretimeout = 0;
+	}
+
+	wdev->min_timeout = 1;
+	wdev->max_timeout = 255;
+
+	devm_mutex_init(dev, &data->lock);

Error handling?
(also apply for patch 1/7 and 6/7)

drivers/watchdog/nct6694_wdt.c line 281

Quoted 9 lines
drivers/watchdog/nct6694_wdt.c new file +9 -0
+
+	platform_set_drvdata(pdev, data);
+
+	watchdog_set_drvdata(&data->wdev, data);
+	watchdog_set_nowayout(&data->wdev, nowayout);
+	watchdog_stop_on_reboot(&data->wdev);
+
+	return devm_watchdog_register_device(dev, &data->wdev);
+}

...

CJ

author
#22 lore raw

Re: [PATCH v7 5/7] watchdog: Add Nuvoton NCT6694 WDT support

Dear Christophe,

Thank you for reviewing,

Christophe JAILLET <[email protected]> 於 2025年2月8日 週六 上午4:30寫道:

Quoted 37 lines · Ming Yu
Quoted 2 lines

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

...

Quoted 30 lines
drivers/watchdog/nct6694_wdt.c new file +30 -0
+static int nct6694_wdt_probe(struct platform_device *pdev)
+{
+     const struct mfd_cell *cell = mfd_get_cell(pdev);
+     struct device *dev = &pdev->dev;
+     struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent);
+     struct nct6694_wdt_data *data;
+     struct watchdog_device *wdev;
+
+     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
+     if (!data)
+             return -ENOMEM;
+
+     data->msg = devm_kzalloc(dev, sizeof(union nct6694_wdt_msg),
+                              GFP_KERNEL);
+     if (!data->msg)
+             return -ENOMEM;
+
+     data->dev = dev;
+     data->nct6694 = nct6694;
+     data->wdev_idx = cell->id;
+
+     wdev = &data->wdev;
+     wdev->info = &nct6694_wdt_info;
+     wdev->ops = &nct6694_wdt_ops;
+     wdev->timeout = timeout;
+     wdev->pretimeout = pretimeout;
+     if (timeout < pretimeout) {
+             dev_warn(data->dev, "pretimeout < timeout. Setting to zero\n");
+             wdev->pretimeout = 0;
+     }
Quoted 9 lines

drivers/watchdog/nct6694_wdt.c line 272

Quoted 5 lines
drivers/watchdog/nct6694_wdt.c new file +5 -0
+
+     wdev->min_timeout = 1;
+     wdev->max_timeout = 255;
+
+     devm_mutex_init(dev, &data->lock);

Error handling?
(also apply for patch 1/7 and 6/7)

I will add error handling for these in the next patch.

Best regards,
Ming

author
#23 lore raw

PATCH v7 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 | 947 ++++++++++++++++++++++++++++++++++
 4 files changed, 959 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 +947 -0

947 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#24 lore raw

PATCH v7 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
maintainer Acked-by on v7 7/7
#25 lore raw

Re: [PATCH v7 7/7] rtc: Add Nuvoton NCT6694 RTC support

Quoted 4 lines · Ming Yu

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

Quoted 348 lines
---
 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
new file mode 100644
index 000000000000..892674d453d1
@@ -0,0 +1,286 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 RTC driver based on USB interface.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#include <linux/bcd.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/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/slab.h>
+
+/*
+ * USB command module type for NCT6694 RTC controller.
+ * This defines the module type used for communication with the NCT6694
+ * RTC controller over the USB interface.
+ */
+#define NCT6694_RTC_MOD		0x08
+
+/* Command 00h - RTC Time */
+#define NCT6694_RTC_TIME	0x0000
+#define NCT6694_RTC_TIME_SEL	0x00
+
+/* Command 01h - RTC Alarm */
+#define NCT6694_RTC_ALARM	0x01
+#define NCT6694_RTC_ALARM_SEL	0x00
+
+/* Command 02h - RTC Status */
+#define NCT6694_RTC_STATUS	0x02
+#define NCT6694_RTC_STATUS_SEL	0x00
+
+#define NCT6694_RTC_IRQ_INT_EN	BIT(0)	/* Transmit a USB INT-in when RTC alarm */
+#define NCT6694_RTC_IRQ_GPO_EN	BIT(5)	/* Trigger a GPO Low Pulse when RTC alarm */
+
+#define NCT6694_RTC_IRQ_EN	(NCT6694_RTC_IRQ_INT_EN | NCT6694_RTC_IRQ_GPO_EN)
+#define NCT6694_RTC_IRQ_STS	BIT(0)	/* Write 1 clear IRQ status */
+
+struct __packed nct6694_rtc_time {
+	u8 sec;
+	u8 min;
+	u8 hour;
+	u8 week;
+	u8 day;
+	u8 month;
+	u8 year;
+};
+
+struct __packed nct6694_rtc_alarm {
+	u8 sec;
+	u8 min;
+	u8 hour;
+	u8 alarm_en;
+	u8 alarm_pend;
+};
+
+struct __packed nct6694_rtc_status {
+	u8 irq_en;
+	u8 irq_pend;
+};
+
+union __packed nct6694_rtc_msg {
+	struct nct6694_rtc_time time;
+	struct nct6694_rtc_alarm alarm;
+	struct nct6694_rtc_status sts;
+};
+
+struct nct6694_rtc_data {
+	struct nct6694 *nct6694;
+	struct rtc_device *rtc;
+	union nct6694_rtc_msg *msg;
+};
+
+static int nct6694_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+	struct nct6694_rtc_data *data = dev_get_drvdata(dev);
+	struct nct6694_rtc_time *time = &data->msg->time;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_RTC_MOD,
+		.cmd = NCT6694_RTC_TIME,
+		.sel = NCT6694_RTC_TIME_SEL,
+		.len = cpu_to_le16(sizeof(*time))
+	};
+	int ret;
+
+	ret = nct6694_read_msg(data->nct6694, &cmd_hd, time);
+	if (ret)
+		return ret;
+
+	tm->tm_sec = bcd2bin(time->sec);		/* tm_sec expect 0 ~ 59 */
+	tm->tm_min = bcd2bin(time->min);		/* tm_min expect 0 ~ 59 */
+	tm->tm_hour = bcd2bin(time->hour);		/* tm_hour expect 0 ~ 23 */
+	tm->tm_wday = bcd2bin(time->week) - 1;		/* tm_wday expect 0 ~ 6 */
+	tm->tm_mday = bcd2bin(time->day);		/* tm_mday expect 1 ~ 31 */
+	tm->tm_mon = bcd2bin(time->month) - 1;		/* tm_month expect 0 ~ 11 */
+	tm->tm_year = bcd2bin(time->year) + 100;	/* tm_year expect since 1900 */
+
+	return ret;
+}
+
+static int nct6694_rtc_set_time(struct device *dev, struct rtc_time *tm)
+{
+	struct nct6694_rtc_data *data = dev_get_drvdata(dev);
+	struct nct6694_rtc_time *time = &data->msg->time;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_RTC_MOD,
+		.cmd = NCT6694_RTC_TIME,
+		.sel = NCT6694_RTC_TIME_SEL,
+		.len = cpu_to_le16(sizeof(*time))
+	};
+
+	time->sec = bin2bcd(tm->tm_sec);
+	time->min = bin2bcd(tm->tm_min);
+	time->hour = bin2bcd(tm->tm_hour);
+	time->week = bin2bcd(tm->tm_wday + 1);
+	time->day = bin2bcd(tm->tm_mday);
+	time->month = bin2bcd(tm->tm_mon + 1);
+	time->year = bin2bcd(tm->tm_year - 100);
+
+	return nct6694_write_msg(data->nct6694, &cmd_hd, time);
+}
+
+static int nct6694_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+	struct nct6694_rtc_data *data = dev_get_drvdata(dev);
+	struct nct6694_rtc_alarm *alarm = &data->msg->alarm;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_RTC_MOD,
+		.cmd = NCT6694_RTC_ALARM,
+		.sel = NCT6694_RTC_ALARM_SEL,
+		.len = cpu_to_le16(sizeof(*alarm))
+	};
+	int ret;
+
+	ret = nct6694_read_msg(data->nct6694, &cmd_hd, alarm);
+	if (ret)
+		return ret;
+
+	alrm->time.tm_sec = bcd2bin(alarm->sec);
+	alrm->time.tm_min = bcd2bin(alarm->min);
+	alrm->time.tm_hour = bcd2bin(alarm->hour);
+	alrm->enabled = alarm->alarm_en;
+	alrm->pending = alarm->alarm_pend;
+
+	return ret;
+}
+
+static int nct6694_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+	struct nct6694_rtc_data *data = dev_get_drvdata(dev);
+	struct nct6694_rtc_alarm *alarm = &data->msg->alarm;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_RTC_MOD,
+		.cmd = NCT6694_RTC_ALARM,
+		.sel = NCT6694_RTC_ALARM_SEL,
+		.len = cpu_to_le16(sizeof(*alarm))
+	};
+
+	alarm->sec = bin2bcd(alrm->time.tm_sec);
+	alarm->min = bin2bcd(alrm->time.tm_min);
+	alarm->hour = bin2bcd(alrm->time.tm_hour);
+	alarm->alarm_en = alrm->enabled ? NCT6694_RTC_IRQ_EN : 0;
+	alarm->alarm_pend = 0;
+
+	return nct6694_write_msg(data->nct6694, &cmd_hd, alarm);
+}
+
+static int nct6694_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
+{
+	struct nct6694_rtc_data *data = dev_get_drvdata(dev);
+	struct nct6694_rtc_status *sts = &data->msg->sts;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_RTC_MOD,
+		.cmd = NCT6694_RTC_STATUS,
+		.sel = NCT6694_RTC_STATUS_SEL,
+		.len = cpu_to_le16(sizeof(*sts))
+	};
+
+	if (enabled)
+		sts->irq_en |= NCT6694_RTC_IRQ_EN;
+	else
+		sts->irq_en &= ~NCT6694_RTC_IRQ_EN;
+
+	sts->irq_pend = 0;
+
+	return nct6694_write_msg(data->nct6694, &cmd_hd, sts);
+}
+
+static const struct rtc_class_ops nct6694_rtc_ops = {
+	.read_time = nct6694_rtc_read_time,
+	.set_time = nct6694_rtc_set_time,
+	.read_alarm = nct6694_rtc_read_alarm,
+	.set_alarm = nct6694_rtc_set_alarm,
+	.alarm_irq_enable = nct6694_rtc_alarm_irq_enable,
+};
+
+static irqreturn_t nct6694_irq(int irq, void *dev_id)
+{
+	struct nct6694_rtc_data *data = dev_id;
+	struct nct6694_rtc_status *sts = &data->msg->sts;
+	static const struct nct6694_cmd_header cmd_hd = {
+		.mod = NCT6694_RTC_MOD,
+		.cmd = NCT6694_RTC_STATUS,
+		.sel = NCT6694_RTC_STATUS_SEL,
+		.len = cpu_to_le16(sizeof(*sts))
+	};
+	int ret;
+
+	rtc_lock(data->rtc);
+
+	sts->irq_en = NCT6694_RTC_IRQ_EN;
+	sts->irq_pend = NCT6694_RTC_IRQ_STS;
+	ret = nct6694_write_msg(data->nct6694, &cmd_hd, sts);
+	if (ret) {
+		rtc_unlock(data->rtc);
+		return IRQ_NONE;
+	}
+
+	rtc_update_irq(data->rtc, 1, RTC_IRQF | RTC_AF);
+
+	rtc_unlock(data->rtc);
+
+	return IRQ_HANDLED;
+}
+
+static int nct6694_rtc_probe(struct platform_device *pdev)
+{
+	struct nct6694_rtc_data *data;
+	struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent);
+	int ret, irq;
+
+	irq = irq_create_mapping(nct6694->domain, NCT6694_IRQ_RTC);
+	if (!irq)
+		return -EINVAL;
+
+	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->msg = devm_kzalloc(&pdev->dev, sizeof(union nct6694_rtc_msg),
+				 GFP_KERNEL);
+	if (!data->msg)
+		return -ENOMEM;
+
+	data->rtc = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(data->rtc))
+		return PTR_ERR(data->rtc);
+
+	data->nct6694 = nct6694;
+	data->rtc->ops = &nct6694_rtc_ops;
+	data->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
+	data->rtc->range_max = RTC_TIMESTAMP_END_2099;
+
+	platform_set_drvdata(pdev, data);
+
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
+					nct6694_irq, IRQF_ONESHOT,
+					"rtc-nct6694", data);
+	if (ret < 0)
+		return dev_err_probe(&pdev->dev, ret, "Failed to request irq\n");
+
+	ret = devm_rtc_register_device(data->rtc);
+	if (ret)
+		return ret;
+
+	device_init_wakeup(&pdev->dev, true);
+	return 0;
+}
+
+static struct platform_driver nct6694_rtc_driver = {
+	.driver = {
+		.name	= "rtc-nct6694",
+	},
+	.probe		= nct6694_rtc_probe,
+};
+
+module_platform_driver(nct6694_rtc_driver);
+
+MODULE_DESCRIPTION("USB-RTC driver for NCT6694");
+MODULE_AUTHOR("Ming Yu <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:nct6694-rtc");
Signature · 1 line
2.34.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com