Skip to contentSkip to conversation

WCH CH341 GPIO and SPI support

PATCH Superseded frank zago 6 participants 18 messages Apr 1, 2022 – Jun 20, 2022

Subsystems (5)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

frank zago

author
#0 lore raw

v5 cover WCH CH341 GPIO and SPI support

The CH341 is a multifunction chip, presenting 3 different USB PID. One of these functions is for I2C/SPI/GPIO. This new set of drivers will manage I2C and GPIO.

Changes from v4:
I should have addressed all the comments: rework of the GPIO interrupt handling code to be more modern, changes in Kconfig wording, some code cleanup.
Driver was tested again with up to 4 of these devices. No error seen.

Changes from v3:
- really converted to an MFD driver. Driver is now split into 3

    modules (MFD+I2C+GPIO).

- minor code cleanups

Changes from v2:
- bug fixes
- more robust USB enumeration
- Changed to an MFD driver as suggested

frank zago (3):
mfd: ch341: add core driver for the WCH CH341 in I2C/SPI/GPIO mode
gpio: ch341: add GPIO MFD cell driver for the CH341
i2c: ch341: add I2C MFD cell driver for the CH341

 Documentation/misc-devices/ch341.rst | 114 ++++++++
 Documentation/misc-devices/index.rst |   1 +
 MAINTAINERS                          |   9 +
 drivers/gpio/Kconfig                 |  10 +
 drivers/gpio/Makefile                |   1 +
 drivers/gpio/gpio-ch341.c            | 383 +++++++++++++++++++++++++++
 drivers/i2c/busses/Kconfig           |  10 +
 drivers/i2c/busses/Makefile          |   1 +
 drivers/i2c/busses/i2c-ch341.c       | 331 +++++++++++++++++++++++
 drivers/mfd/Kconfig                  |  10 +
 drivers/mfd/Makefile                 |   1 +
 drivers/mfd/ch341-core.c             | 105 ++++++++
 include/linux/mfd/ch341.h            |  26 ++
 13 files changed, 1002 insertions(+)
 create mode 100644 Documentation/misc-devices/ch341.rst
 create mode 100644 drivers/gpio/gpio-ch341.c
 create mode 100644 drivers/i2c/busses/i2c-ch341.c
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h

--
2.32.0

author
#1 lore raw

PATCH v5 1/3 mfd: ch341: add core driver for the WCH CH341 in I2C/SPI/GPIO mode

The CH341 is a multifunction chip, presenting 3 different USB PID. One of these functions is for I2C/SPI/GPIO. This new set of drivers will manage I2C and GPIO.

---
 Documentation/misc-devices/ch341.rst | 114 +++++++++++++++++++++++++++
 Documentation/misc-devices/index.rst |   1 +
 MAINTAINERS                          |   7 ++
 drivers/mfd/Kconfig                  |  10 +++
 drivers/mfd/Makefile                 |   1 +
 drivers/mfd/ch341-core.c             |  99 +++++++++++++++++++++++
 include/linux/mfd/ch341.h            |  26 ++++++
 7 files changed, 258 insertions(+)
 create mode 100644 Documentation/misc-devices/ch341.rst
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h
Documentation/misc-devices/ch341.rst new file +114 -0
new file mode 100644
index 000000000000..bf0b83f2eb85
@@ -0,0 +1,114 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+===========================================================
+WinChipHead (沁恒) CH341 linux driver for I2C and GPIO mode
+===========================================================
+
+The CH341 is declined in several flavors, and may support one or more
+of UART, SPI, I2C and GPIO, but not always simultaneously:
+
+  - CH341 A/B/F: UART, Printer, SPI, I2C and GPIO
+  - CH341 C/T: UART and I2C
+  - CH341 H: SPI
+
+They work in 3 different modes, with only one being presented
+depending on the USB PID:
+
+  - 0x5523: UART mode, covered by the USB `ch341` serial driver
+  - 0x5512: SPI/I2C/GPIO mode, covered by the ch341 MFD drivers
+  - 0x5584: Parallel printer mode, covered by the USB `usblp` driver
+
+Mode selection is done at the hardware level by tying some
+pins. Breakout boards with one of the CH341 chip usually have one or
+more jumpers to select which mode they work on. At least one model
+(CJMCU-341) appears to need bridging some solder pads to select a
+different default. Breakout boards also don't usually offer an option
+to configure the chip into printer mode; for that case, connect the
+SCL and SDA lines directly together.
+
+The various CH341 appear to be indistinguishable from the
+software. For instance the ch341 MFD driver will present a GPIO
+interface for the CH341T although physical pins are not present, and
+the device will accept GPIO commands.
+
+The ch341 MFD driver has been tested with a CH341A, CH341B and
+CH341T.
+
+Some breakout boards work in 3.3V and 5V depending on some jumpers.
+
+The black chip programmer with a ZIF socket will power the CH341 at
+3.3V if a jumper is set, but will only output 5V to the chips to be
+programmed, which is not always desirable. A hardware hack to use 3.3V
+everywhere, involving some soldering, is available at
+https://eevblog.com/forum/repair/ch341a-serial-memory-programmer-power-supply-fix/
+
+Some sample code for the CH341 is available at the manufacturer
+website, at http://wch-ic.com/products/CH341.html
+
+The repository at https://github.com/boseji/CH341-Store contains a lot
+of information on these chips, including datasheets.
+
+This driver is based on the pre-existing work at
+https://github.com/gschorcht/i2c-ch341-usb
+
+
+I2C Caveats
+-----------
+
+The ch341 doesn't work with a Wii nunchuk, possibly because the
+pull-up value is too low (1500 ohms).
+
+i2c AT24 eeproms can be read but not programmed properly because the
+at24 linux driver tries to write a byte at a time, and doesn't wait at
+all (or enough) between writes. Data corruption on writes does occur.
+
+The driver doesn't support detection of I2C devices present on the
+bus. Apparently when a device is not present at a given address, the
+CH341 will return an extra byte of data, but the driver doesn't
+support that. This may be addressed in the future.
+
+
+The GPIOs
+---------
+
+16 GPIOs are available on the CH341 A/B/F. The first 6 are input/output,
+and the last 10 are input only.
+
+Pinout and their names as they appear on some breakout boards::
+
+  CH341A/B/F     GPIO  Names                    Mode
+    pin          line
+
+   15             0     D0, CS0                  input/output
+   16             1     D1, CS1                  input/output
+   17             2     D2, CS2                  input/output
+   18             3     D3, SCK, DCK             input/output
+   19             4     D4, DOUT2, CS3           input/output
+   20             5     D5, MOSI, DOUT, SDO      input/output
+   21             6     D6, DIN2                 input
+   22             7     D7, MISO, DIN            input
+    5             8     ERR                      input
+    6             9     PEMP                     input
+    7            10     INT                      input
+    8            11     SLCT (SELECT)            input
+   26            12     RST# (?)                 input
+   27            13     WT (WAIT)                input
+    4            14     DS (Data Select?)        input
+    3            15     AS (Address Select?)     input
+
+
+GPIO interrupt
+~~~~~~~~~~~~~~
+
+The INT pin, corresponding to GPIO 10 is an input pin that can trigger
+an interrupt on a rising edge. Only that pin is able to generate an
+interrupt, and only on a rising edge. Trying to monitor events on
+another GPIO, or that GPIO on something other than a rising edge, will
+be rejected.
+
+
+SPI
+---
+
+This driver doesn't offer an SPI interface (yet) due to the
+impossibility of declaring an SPI device like I2C does.
Documentation/misc-devices/index.rst +1 -0
index 30ac58f81901..52d03715601e 100644
@@ -19,6 +19,7 @@ fit into other categories.
    bh1770glc
    eeprom
    c2port
+   ch341
    dw-xdata-pcie
    ibmvmc
    ics932s401
MAINTAINERS +7 -0
index cbbd3ce7e0c2..b61af813fb9f 100644
@@ -21211,6 +21211,13 @@ M:	David Härdeman <[email protected]>
 S:	Maintained
 F:	drivers/media/rc/winbond-cir.c
 
+WINCHIPHEAD CH341 I2C/GPIO MFD DRIVER
+M:	Frank Zago <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/mfd/ch341-core.c
+F:	include/linux/mfd/ch341.h
+
 WINSYSTEMS EBC-C384 WATCHDOG DRIVER
 M:	William Breathitt Gray <[email protected]>
 L:	[email protected]
drivers/mfd/Kconfig +10 -0
index 3b59456f5545..893acc821a42 100644
@@ -1784,6 +1784,16 @@ config MFD_LOCHNAGAR
 	help
 	  Support for Cirrus Logic Lochnagar audio development board.
 
+config MFD_CH341
+	tristate "WinChipHead CH341 in I2C/SPI/GPIO mode"
+	depends on USB
+	help
+	  If you say yes to this option, support for the CH341 series
+	  of chips, running in I2C/SPI/GPIO mode will be included.
+
+	  This driver can also be built as a module.  If so, the
+	  module will be called ch341-core.
+
 config MFD_ARIZONA
 	select REGMAP
 	select REGMAP_IRQ
drivers/mfd/Makefile +1 -0
index 858cacf659d6..fd615ab3929f 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_MFD_ASIC3)		+= asic3.o tmio_core.o
 obj-$(CONFIG_ARCH_BCM2835)	+= bcm2835-pm.o
 obj-$(CONFIG_MFD_BCM590XX)	+= bcm590xx.o
 obj-$(CONFIG_MFD_BD9571MWV)	+= bd9571mwv.o
+obj-$(CONFIG_MFD_CH341)		+= ch341-core.o
 obj-$(CONFIG_MFD_CROS_EC_DEV)	+= cros_ec_dev.o
 obj-$(CONFIG_MFD_ENE_KB3930)	+= ene-kb3930.o
 obj-$(CONFIG_MFD_EXYNOS_LPASS)	+= exynos-lpass.o
drivers/mfd/ch341-core.c new file +99 -0

99 lines not on this page · raw message

include/linux/mfd/ch341.h new file +26 -0
new file mode 100644
index 000000000000..a87b23e30123
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Definitions for the CH341 MFD driver */
+
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+#define DEFAULT_TIMEOUT_MS 1000	/* 1s USB requests timeout */
+
+/* All commands fit inside a 32-byte segment. There may be several
+ * of these segments in a USB command.
+ */
+#define SEG_SIZE 32
+
+struct usb_device;
+struct usb_interface;
+
+struct ch341_device {
+	struct usb_device *usb_dev;
+	struct usb_interface *iface;
+	struct mutex usb_lock;
+
+	int ep_in;
+	int ep_out;
+	int ep_intr;
+	u8 ep_intr_interval;
+};
Signature · 1 line
2.32.0
#2 lore raw

Re: [PATCH v5 1/3] mfd: ch341: add core driver for the WCH CH341 in I2C/SPI/GPIO mode

Quoted 8 lines · frank zago

The CH341 is a multifunction chip, presenting 3 different USB PID. One of these functions is for I2C/SPI/GPIO. This new set of drivers will manage I2C and GPIO.

---
 Documentation/misc-devices/ch341.rst | 114 +++++++++++++++++++++++++++
 Documentation/misc-devices/index.rst |   1 +

Not sure I've seen this bundled with an MFD driver before.

Please separate them into their own patches.

Quoted 217 lines
 MAINTAINERS                          |   7 ++
 drivers/mfd/Kconfig                  |  10 +++
 drivers/mfd/Makefile                 |   1 +
 drivers/mfd/ch341-core.c             |  99 +++++++++++++++++++++++
 include/linux/mfd/ch341.h            |  26 ++++++
 7 files changed, 258 insertions(+)
 create mode 100644 Documentation/misc-devices/ch341.rst
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h
Documentation/misc-devices/ch341.rst new file +114 -0
new file mode 100644
index 000000000000..bf0b83f2eb85
@@ -0,0 +1,114 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+===========================================================
+WinChipHead (沁恒) CH341 linux driver for I2C and GPIO mode
+===========================================================
+
+The CH341 is declined in several flavors, and may support one or more
+of UART, SPI, I2C and GPIO, but not always simultaneously:
+
+  - CH341 A/B/F: UART, Printer, SPI, I2C and GPIO
+  - CH341 C/T: UART and I2C
+  - CH341 H: SPI
+
+They work in 3 different modes, with only one being presented
+depending on the USB PID:
+
+  - 0x5523: UART mode, covered by the USB `ch341` serial driver
+  - 0x5512: SPI/I2C/GPIO mode, covered by the ch341 MFD drivers
+  - 0x5584: Parallel printer mode, covered by the USB `usblp` driver
+
+Mode selection is done at the hardware level by tying some
+pins. Breakout boards with one of the CH341 chip usually have one or
+more jumpers to select which mode they work on. At least one model
+(CJMCU-341) appears to need bridging some solder pads to select a
+different default. Breakout boards also don't usually offer an option
+to configure the chip into printer mode; for that case, connect the
+SCL and SDA lines directly together.
+
+The various CH341 appear to be indistinguishable from the
+software. For instance the ch341 MFD driver will present a GPIO
+interface for the CH341T although physical pins are not present, and
+the device will accept GPIO commands.
+
+The ch341 MFD driver has been tested with a CH341A, CH341B and
+CH341T.
+
+Some breakout boards work in 3.3V and 5V depending on some jumpers.
+
+The black chip programmer with a ZIF socket will power the CH341 at
+3.3V if a jumper is set, but will only output 5V to the chips to be
+programmed, which is not always desirable. A hardware hack to use 3.3V
+everywhere, involving some soldering, is available at
+https://eevblog.com/forum/repair/ch341a-serial-memory-programmer-power-supply-fix/
+
+Some sample code for the CH341 is available at the manufacturer
+website, at http://wch-ic.com/products/CH341.html
+
+The repository at https://github.com/boseji/CH341-Store contains a lot
+of information on these chips, including datasheets.
+
+This driver is based on the pre-existing work at
+https://github.com/gschorcht/i2c-ch341-usb
+
+
+I2C Caveats
+-----------
+
+The ch341 doesn't work with a Wii nunchuk, possibly because the
+pull-up value is too low (1500 ohms).
+
+i2c AT24 eeproms can be read but not programmed properly because the
+at24 linux driver tries to write a byte at a time, and doesn't wait at
+all (or enough) between writes. Data corruption on writes does occur.
+
+The driver doesn't support detection of I2C devices present on the
+bus. Apparently when a device is not present at a given address, the
+CH341 will return an extra byte of data, but the driver doesn't
+support that. This may be addressed in the future.
+
+
+The GPIOs
+---------
+
+16 GPIOs are available on the CH341 A/B/F. The first 6 are input/output,
+and the last 10 are input only.
+
+Pinout and their names as they appear on some breakout boards::
+
+  CH341A/B/F     GPIO  Names                    Mode
+    pin          line
+
+   15             0     D0, CS0                  input/output
+   16             1     D1, CS1                  input/output
+   17             2     D2, CS2                  input/output
+   18             3     D3, SCK, DCK             input/output
+   19             4     D4, DOUT2, CS3           input/output
+   20             5     D5, MOSI, DOUT, SDO      input/output
+   21             6     D6, DIN2                 input
+   22             7     D7, MISO, DIN            input
+    5             8     ERR                      input
+    6             9     PEMP                     input
+    7            10     INT                      input
+    8            11     SLCT (SELECT)            input
+   26            12     RST# (?)                 input
+   27            13     WT (WAIT)                input
+    4            14     DS (Data Select?)        input
+    3            15     AS (Address Select?)     input
+
+
+GPIO interrupt
+~~~~~~~~~~~~~~
+
+The INT pin, corresponding to GPIO 10 is an input pin that can trigger
+an interrupt on a rising edge. Only that pin is able to generate an
+interrupt, and only on a rising edge. Trying to monitor events on
+another GPIO, or that GPIO on something other than a rising edge, will
+be rejected.
+
+
+SPI
+---
+
+This driver doesn't offer an SPI interface (yet) due to the
+impossibility of declaring an SPI device like I2C does.
Documentation/misc-devices/index.rst +1 -0
index 30ac58f81901..52d03715601e 100644
@@ -19,6 +19,7 @@ fit into other categories.
    bh1770glc
    eeprom
    c2port
+   ch341
    dw-xdata-pcie
    ibmvmc
    ics932s401
MAINTAINERS +7 -0
index cbbd3ce7e0c2..b61af813fb9f 100644
@@ -21211,6 +21211,13 @@ M:	David Härdeman <[email protected]>
 S:	Maintained
 F:	drivers/media/rc/winbond-cir.c
 
+WINCHIPHEAD CH341 I2C/GPIO MFD DRIVER
+M:	Frank Zago <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/mfd/ch341-core.c
+F:	include/linux/mfd/ch341.h
+
 WINSYSTEMS EBC-C384 WATCHDOG DRIVER
 M:	William Breathitt Gray <[email protected]>
 L:	[email protected]
drivers/mfd/Kconfig +10 -0
index 3b59456f5545..893acc821a42 100644
@@ -1784,6 +1784,16 @@ config MFD_LOCHNAGAR
 	help
 	  Support for Cirrus Logic Lochnagar audio development board.
 
+config MFD_CH341
+	tristate "WinChipHead CH341 in I2C/SPI/GPIO mode"
+	depends on USB
+	help
+	  If you say yes to this option, support for the CH341 series
+	  of chips, running in I2C/SPI/GPIO mode will be included.
+
+	  This driver can also be built as a module.  If so, the
+	  module will be called ch341-core.
+
 config MFD_ARIZONA
 	select REGMAP
 	select REGMAP_IRQ
drivers/mfd/Makefile +1 -0
index 858cacf659d6..fd615ab3929f 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_MFD_ASIC3)		+= asic3.o tmio_core.o
 obj-$(CONFIG_ARCH_BCM2835)	+= bcm2835-pm.o
 obj-$(CONFIG_MFD_BCM590XX)	+= bcm590xx.o
 obj-$(CONFIG_MFD_BD9571MWV)	+= bd9571mwv.o
+obj-$(CONFIG_MFD_CH341)		+= ch341-core.o
 obj-$(CONFIG_MFD_CROS_EC_DEV)	+= cros_ec_dev.o
 obj-$(CONFIG_MFD_ENE_KB3930)	+= ene-kb3930.o
 obj-$(CONFIG_MFD_EXYNOS_LPASS)	+= exynos-lpass.o
drivers/mfd/ch341-core.c new file +18 -0
new file mode 100644
index 000000000000..0bb6eb8057e9
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Core driver for the CH341A, CH341B and CH341T in I2C/SPI/GPIO
+ * mode. There are cell drivers available for I2C and GPIO. SPI is not
+ * yet supported.
+ *
+ * Copyright 2022, Frank Zago
+ * Copyright (c) 2017 Gunar Schorcht ([email protected])
+ * Copyright (c) 2016 Tse Lun Bien
+ * Copyright (c) 2014 Marco Gittler
+ * Copyright (c) 2006-2007 Till Harbaum ([email protected])
+ */
+
+#include <linux/kernel.h>
+#include <linux/mfd/ch341.h>
+#include <linux/mfd/core.h>
+#include <linux/module.h>
+#include <linux/slab.h>

drivers/mfd/ch341-core.c line 28

Quoted 10 lines
drivers/mfd/ch341-core.c new file +10 -0
+#include <linux/usb.h>
+
+static const struct mfd_cell ch341_devs[] = {
+};
+
+static int ch341_usb_probe(struct usb_interface *iface,
+			   const struct usb_device_id *usb_id)
+{
+	struct usb_host_endpoint *endpoints;
+	struct ch341_device *dev;

Please rename this struct 'ch341_ddata' and s/dev/ddata/.

drivers/mfd/ch341-core.c line 29

Quoted 1 line
drivers/mfd/ch341-core.c new file +1 -0
+	int rc;

'ret' please.

drivers/mfd/ch341-core.c line 35

Quoted 5 lines
drivers/mfd/ch341-core.c new file +5 -0
+	dev = devm_kzalloc(&iface->dev, sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	dev->usb_dev = usb_get_dev(interface_to_usbdev(iface));

Can this come later?

It would save on the goto dance.

drivers/mfd/ch341-core.c line 39

Quoted 4 lines
drivers/mfd/ch341-core.c new file +4 -0
+	dev->iface = iface;
+	mutex_init(&dev->usb_lock);
+
+	if (iface->cur_altsetting->desc.bNumEndpoints != 3) {

Why 3?

drivers/mfd/ch341-core.c line 47

Quoted 8 lines
drivers/mfd/ch341-core.c new file +8 -0
+		rc = -ENODEV;
+		goto free_dev;
+	}
+
+	endpoints = iface->cur_altsetting->endpoint;
+	if (!usb_endpoint_is_bulk_in(&endpoints[0].desc) ||
+	    !usb_endpoint_is_bulk_out(&endpoints[1].desc) ||
+	    !usb_endpoint_xfer_int(&endpoints[2].desc)) {

What has happened if we get here?

Perhaps a comment would be useful?

drivers/mfd/ch341-core.c line 59

Quoted 12 lines
drivers/mfd/ch341-core.c new file +12 -0
+		rc = -ENODEV;
+		goto free_dev;
+	}
+
+	dev->ep_in = endpoints[0].desc.bEndpointAddress;
+	dev->ep_out = endpoints[1].desc.bEndpointAddress;
+	dev->ep_intr = endpoints[2].desc.bEndpointAddress;
+	dev->ep_intr_interval = endpoints[2].desc.bInterval;
+
+	usb_set_intfdata(iface, dev);
+
+	rc = mfd_add_hotplug_devices(&iface->dev, ch341_devs,

Why are you using 'hotplug' here?

ch341_devs is empty right?

So no child devices are registered.

In which case this is not (yet) an MFD and cannot be accepted.

Please add the children.

drivers/mfd/ch341-core.c line 63

Quoted 4 lines
drivers/mfd/ch341-core.c new file +4 -0
+				     ARRAY_SIZE(ch341_devs));
+	if (rc) {
+		rc = dev_err_probe(&iface->dev, rc,
+				   "Failed to add mfd devices to core\n");

I'm not even sure what this means. Should be:

"Failed to register child devices\n"

Quoted 24 lines
drivers/mfd/ch341-core.c new file +24 -0
+		goto free_dev;
+	}
+
+	return 0;
+
+free_dev:
+	usb_put_dev(dev->usb_dev);
+
+	return rc;
+}
+
+static void ch341_usb_disconnect(struct usb_interface *usb_if)
+{
+	struct ch341_device *dev = usb_get_intfdata(usb_if);
+
+	mfd_remove_devices(&usb_if->dev);
+	usb_put_dev(dev->usb_dev);
+}
+
+static const struct usb_device_id ch341_usb_table[] = {
+	{ USB_DEVICE(0x1a86, 0x5512) },
+	{ }
+};
+MODULE_DEVICE_TABLE(usb, ch341_usb_table);

drivers/mfd/ch341-core.c line 97

Quoted 10 lines
drivers/mfd/ch341-core.c new file +10 -0
+
+static struct usb_driver ch341_usb_driver = {
+	.name       = "ch341-mfd",
+	.id_table   = ch341_usb_table,
+	.probe      = ch341_usb_probe,
+	.disconnect = ch341_usb_disconnect,
+};
+module_usb_driver(ch341_usb_driver);
+
+MODULE_AUTHOR("Various");

This does not look valid. Please drop it.

drivers/mfd/ch341-core.c line 98

Quoted 1 line
drivers/mfd/ch341-core.c new file +1 -0
+MODULE_DESCRIPTION("CH341 USB to I2C/SPI/GPIO adapter");

Is it? What makes it one of those?

include/linux/mfd/ch341.h line 2

Quoted 9 lines
drivers/mfd/ch341-core.c new file +1 -0
+MODULE_LICENSE("GPL");
include/linux/mfd/ch341.h new file +2 -0
new file mode 100644
index 000000000000..a87b23e30123
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Definitions for the CH341 MFD driver */

Drop the term MFD please.

include/linux/mfd/ch341.h line 7

Quoted 4 lines
include/linux/mfd/ch341.h new file +4 -0
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+#define DEFAULT_TIMEOUT_MS 1000	/* 1s USB requests timeout */

Where is this used?

include/linux/mfd/ch341.h line 11

Quoted 3 lines
include/linux/mfd/ch341.h new file +3 -0
+/* All commands fit inside a 32-byte segment. There may be several
+ * of these segments in a USB command.
+ */

This is not a properly formatted multi-line comment.

Quoted 15 lines
include/linux/mfd/ch341.h new file +15 -0
+#define SEG_SIZE 32
+
+struct usb_device;
+struct usb_interface;
+
+struct ch341_device {
+	struct usb_device *usb_dev;
+	struct usb_interface *iface;
+	struct mutex usb_lock;
+
+	int ep_in;
+	int ep_out;
+	int ep_intr;
+	u8 ep_intr_interval;
+};
Signature · 4 lines
Lee Jones [李琼斯]
Principal Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog
author
#3 lore raw

Re: [PATCH v5 1/3] mfd: ch341: add core driver for the WCH CH341 in I2C/SPI/GPIO mode

Hi Lee,

Quoted 10 lines

drivers/mfd/ch341-core.c line 59

Quoted 8 lines
drivers/mfd/ch341-core.c new file +8 -0
+	dev->ep_in = endpoints[0].desc.bEndpointAddress;
+	dev->ep_out = endpoints[1].desc.bEndpointAddress;
+	dev->ep_intr = endpoints[2].desc.bEndpointAddress;
+	dev->ep_intr_interval = endpoints[2].desc.bInterval;
+
+	usb_set_intfdata(iface, dev);
+
+	rc = mfd_add_hotplug_devices(&iface->dev, ch341_devs,

Why are you using 'hotplug' here?

I replaced with mfd_add_devices, however I'm not sure what the difference
is since mfd_add_hotplug_devices() is just a helper with less parameters.
That's what the viperboard driver does; I just copied since both devices are similar in functionality.

Quoted 9 lines

ch341_devs is empty right?

So no child devices are registered.

In which case this is not (yet) an MFD and cannot be accepted.

Please add the children.

Quoted 8 lines

drivers/mfd/ch341-core.c line 63

Quoted 4 lines
drivers/mfd/ch341-core.c new file +4 -0
+				     ARRAY_SIZE(ch341_devs));
+	if (rc) {
+		rc = dev_err_probe(&iface->dev, rc,
+				   "Failed to add mfd devices to core\n");

I'm not even sure what this means. Should be:

"Failed to register child devices\n"

Changed. The original string was also from the viperboard driver.

Quoted 4 lines
Quoted 2 lines

+MODULE_DESCRIPTION("CH341 USB to I2C/SPI/GPIO adapter");

Is it? What makes it one of those?

That's what this chip does, in addition to UART and printer modes. See patch 4.

Frank.

#4 lore raw

Re: [PATCH v5 1/3] mfd: ch341: add core driver for the WCH CH341 in I2C/SPI/GPIO mode

In reply to frank zago
Quoted 9 lines · frank zago

The CH341 is a multifunction chip, presenting 3 different USB PID. One of these functions is for I2C/SPI/GPIO. This new set of drivers will manage I2C and GPIO.

---

drivers/mfd/ch341-core.c new file +2 -0
+static int ch341_usb_probe(struct usb_interface *iface,
+			   const struct usb_device_id *usb_id)

drivers/mfd/ch341-core.c line 35

Quoted 10 lines
drivers/mfd/ch341-core.c new file +10 -0
+{
+	struct usb_host_endpoint *endpoints;
+	struct ch341_device *dev;
+	int rc;
+
+	dev = devm_kzalloc(&iface->dev, sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	dev->usb_dev = usb_get_dev(interface_to_usbdev(iface));

No need to grab a reference unless you're going to hold on to it past disconnect().

Quoted 5 lines
drivers/mfd/ch341-core.c new file +5 -0
+	dev->iface = iface;
+	mutex_init(&dev->usb_lock);
+
+	if (iface->cur_altsetting->desc.bNumEndpoints != 3) {
+		rc = -ENODEV;

drivers/mfd/ch341-core.c line 50

Quoted 10 lines
drivers/mfd/ch341-core.c new file +10 -0
+		goto free_dev;
+	}
+
+	endpoints = iface->cur_altsetting->endpoint;
+	if (!usb_endpoint_is_bulk_in(&endpoints[0].desc) ||
+	    !usb_endpoint_is_bulk_out(&endpoints[1].desc) ||
+	    !usb_endpoint_xfer_int(&endpoints[2].desc)) {
+		rc = -ENODEV;
+		goto free_dev;
+	}

Please use usb_find_common_endpoints() for the above.

Quoted 21 lines
drivers/mfd/ch341-core.c new file +21 -0
+
+	dev->ep_in = endpoints[0].desc.bEndpointAddress;
+	dev->ep_out = endpoints[1].desc.bEndpointAddress;
+	dev->ep_intr = endpoints[2].desc.bEndpointAddress;
+	dev->ep_intr_interval = endpoints[2].desc.bInterval;
+
+	usb_set_intfdata(iface, dev);
+
+	rc = mfd_add_hotplug_devices(&iface->dev, ch341_devs,
+				     ARRAY_SIZE(ch341_devs));
+	if (rc) {
+		rc = dev_err_probe(&iface->dev, rc,
+				   "Failed to add mfd devices to core\n");
+		goto free_dev;
+	}
+
+	return 0;
+
+free_dev:
+	usb_put_dev(dev->usb_dev);
+

drivers/mfd/ch341-core.c line 81

Quoted 10 lines
drivers/mfd/ch341-core.c new file +10 -0
+	return rc;
+}
+
+static void ch341_usb_disconnect(struct usb_interface *usb_if)
+{
+	struct ch341_device *dev = usb_get_intfdata(usb_if);
+
+	mfd_remove_devices(&usb_if->dev);
+	usb_put_dev(dev->usb_dev);
+}

Johan

author
#5 lore raw

Re: [PATCH v5 1/3] mfd: ch341: add core driver for the WCH CH341 in I2C/SPI/GPIO mode

Hi Johan,

Quoted 9 lines · Johan Hovold

drivers/mfd/ch341-core.c line 35

Quoted 6 lines
drivers/mfd/ch341-core.c new file +6 -0
+
+	dev = devm_kzalloc(&iface->dev, sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	dev->usb_dev = usb_get_dev(interface_to_usbdev(iface));

No need to grab a reference unless you're going to hold on to it past disconnect().

I removed that.

Quoted 10 lines

drivers/mfd/ch341-core.c line 50

Quoted 8 lines
drivers/mfd/ch341-core.c new file +8 -0
+
+	endpoints = iface->cur_altsetting->endpoint;
+	if (!usb_endpoint_is_bulk_in(&endpoints[0].desc) ||
+	    !usb_endpoint_is_bulk_out(&endpoints[1].desc) ||
+	    !usb_endpoint_xfer_int(&endpoints[2].desc)) {
+		rc = -ENODEV;
+		goto free_dev;
+	}

Please use usb_find_common_endpoints() for the above.

Thanks. I wasn't aware on that API. It simplifies things a bit.

Regards,
Frank.

author
#6 lore raw

PATCH v5 2/3 gpio: ch341: add GPIO MFD cell driver for the CH341

In reply to frank zago

The GPIO interface offers 16 GPIOs. 6 are read/write, and 10 are read-only.

---
 MAINTAINERS               |   1 +
 drivers/gpio/Kconfig      |  10 +
 drivers/gpio/Makefile     |   1 +
 drivers/gpio/gpio-ch341.c | 383 ++++++++++++++++++++++++++++++++++++++
 drivers/mfd/ch341-core.c  |   3 +
 5 files changed, 398 insertions(+)
 create mode 100644 drivers/gpio/gpio-ch341.c
MAINTAINERS +1 -0
index b61af813fb9f..757ab4f6f9f6 100644
@@ -21215,6 +21215,7 @@ WINCHIPHEAD CH341 I2C/GPIO MFD DRIVER
 M:	Frank Zago <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	drivers/gpio/gpio-ch341.c
 F:	drivers/mfd/ch341-core.c
 F:	include/linux/mfd/ch341.h
 
drivers/gpio/Kconfig +10 -0
index 45764ec3b2eb..0e868c26daf6 100644
@@ -1652,6 +1652,16 @@ endmenu
 menu "USB GPIO expanders"
 	depends on USB
 
+config GPIO_CH341
+	tristate "CH341 USB to GPIO support"
+	select MFD_CH341
+	help
+	  If you say yes to this option, GPIO support will be included for the
+	  WCH CH341, a USB to I2C/SPI/GPIO interface.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called gpio-ch341.
+
 config GPIO_VIPERBOARD
 	tristate "Viperboard GPIO a & b support"
 	depends on MFD_VIPERBOARD
drivers/gpio/Makefile +1 -0
index 14352f6dfe8e..beef802cbfb1 100644
@@ -44,6 +44,7 @@ obj-$(CONFIG_GPIO_BD9571MWV)		+= gpio-bd9571mwv.o
 obj-$(CONFIG_GPIO_BRCMSTB)		+= gpio-brcmstb.o
 obj-$(CONFIG_GPIO_BT8XX)		+= gpio-bt8xx.o
 obj-$(CONFIG_GPIO_CADENCE)		+= gpio-cadence.o
+obj-$(CONFIG_GPIO_CH341)		+= gpio-ch341.o
 obj-$(CONFIG_GPIO_CLPS711X)		+= gpio-clps711x.o
 obj-$(CONFIG_GPIO_SNPS_CREG)		+= gpio-creg-snps.o
 obj-$(CONFIG_GPIO_CRYSTAL_COVE)		+= gpio-crystalcove.o
drivers/gpio/gpio-ch341.c new file +383 -0

383 lines not on this page · raw message

drivers/mfd/ch341-core.c +3 -0
index 0bb6eb8057e9..e919f6901a14 100644
@@ -19,6 +19,9 @@
 #include <linux/usb.h>
 
 static const struct mfd_cell ch341_devs[] = {
+	{
+		.name = "ch341-gpio",
+	},
 };
 
 static int ch341_usb_probe(struct usb_interface *iface,
Signature · 1 line
2.32.0
#8 lore raw

Re: [PATCH v5 2/3] gpio: ch341: add GPIO MFD cell driver for the CH341

In reply to frank zago
Quoted 22 lines · frank zago

The GPIO interface offers 16 GPIOs. 6 are read/write, and 10 are read-only.

---

drivers/gpio/gpio-ch341.c new file +16 -0
+struct ch341_gpio {
+	struct gpio_chip gpio;
+	struct mutex gpio_lock;
+	u16 gpio_dir;		/* 1 bit per pin, 0=IN, 1=OUT. */
+	u16 gpio_last_read;	/* last GPIO values read */
+	u16 gpio_last_written;	/* last GPIO values written */
+	union {
+		u8 gpio_buf[SEG_SIZE];
+		__le16 gpio_buf_status;
+	};
+
+	struct urb *irq_urb;
+	struct usb_anchor irq_urb_out;
+	u8 irq_buf[CH341_USB_MAX_INTR_SIZE];
+	struct irq_chip irq_chip;
+

drivers/gpio/gpio-ch341.c

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +9 -0
+	struct ch341_device *ch341;
+};
 
+static void ch341_complete_intr_urb(struct urb *urb)
+{
+	struct ch341_gpio *dev = urb->context;
+	int rc;
+
+	if (urb->status) {
+		usb_unanchor_urb(dev->irq_urb);

URBs are unanchored by USB core on completion.

Quoted 26 lines
drivers/gpio/gpio-ch341.c new file +25 -0
+	} else {
+		/*
+		 * Data is 8 bytes. Byte 0 might be the length of
+		 * significant data, which is 3 more bytes. Bytes 1
+		 * and 2, and possibly 3, are the pin status. The byte
+		 * order is different than for the GET_STATUS
+		 * command. Byte 1 is GPIOs 8 to 15, and byte 2 is
+		 * GPIOs 0 to 7.
+		 */
+
+		handle_nested_irq(irq_find_mapping(dev->gpio.irq.domain,
+						   CH341_GPIO_INT_LINE));
+
+		rc = usb_submit_urb(dev->irq_urb, GFP_ATOMIC);
+		if (rc)
+			usb_unanchor_urb(dev->irq_urb);
+	}
+}
 
+static void ch341_gpio_irq_enable(struct irq_data *data)
+{
+	struct ch341_gpio *dev = irq_data_get_irq_chip_data(data);
+	int rc;
+
+	/*
+	 * The URB might have just been unlinked in

drivers/gpio/gpio-ch341.c line 285

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10 -0
+	 * ch341_gpio_irq_disable, but the completion handler hasn't
+	 * been called yet.
+	 */
+	if (!usb_wait_anchor_empty_timeout(&dev->irq_urb_out, 5000))
+		usb_kill_anchored_urbs(&dev->irq_urb_out);
+
+	usb_anchor_urb(dev->irq_urb, &dev->irq_urb_out);
+	rc = usb_submit_urb(dev->irq_urb, GFP_ATOMIC);
+	if (rc)
+		usb_unanchor_urb(dev->irq_urb);

This looks confused and broken.

usb_kill_anchored_urbs() can sleep so either calling it is broken or using GFP_ATOMIC is unnecessary.

And isn't this function called multiple times when enabling more than one irq?!

drivers/gpio/gpio-ch341.c line 292

Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+}
+
+static void ch341_gpio_irq_disable(struct irq_data *data)
+{
+	struct ch341_gpio *dev = irq_data_get_irq_chip_data(data);
+
+	usb_unlink_urb(dev->irq_urb);

Same here...

drivers/gpio/gpio-ch341.c line 299

Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+}
+
+static int ch341_gpio_remove(struct platform_device *pdev)
+{
+	struct ch341_gpio *dev = platform_get_drvdata(pdev);
+
+	usb_kill_anchored_urbs(&dev->irq_urb_out);

You only have one URB...

And what prevents it from being resubmitted here?

Quoted 62 lines
drivers/gpio/gpio-ch341.c new file +62 -0
+	gpiochip_remove(&dev->gpio);
+	usb_free_urb(dev->irq_urb);
+
+	return 0;
+}
+
+static int ch341_gpio_probe(struct platform_device *pdev)
+{
+	struct ch341_device *ch341 = dev_get_drvdata(pdev->dev.parent);
+	struct gpio_irq_chip *girq;
+	struct ch341_gpio *dev;
+	struct gpio_chip *gpio;
+	int rc;
+
+	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
+	if (dev == NULL)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, dev);
+	dev->ch341 = ch341;
+	mutex_init(&dev->gpio_lock);
+
+	gpio = &dev->gpio;
+	gpio->label = dev_name(&pdev->dev);
+	gpio->parent = &pdev->dev;
+	gpio->owner = THIS_MODULE;
+	gpio->get_direction = ch341_gpio_get_direction;
+	gpio->direction_input = ch341_gpio_direction_input;
+	gpio->direction_output = ch341_gpio_direction_output;
+	gpio->get = ch341_gpio_get;
+	gpio->get_multiple = ch341_gpio_get_multiple;
+	gpio->set = ch341_gpio_set;
+	gpio->set_multiple = ch341_gpio_set_multiple;
+	gpio->base = -1;
+	gpio->ngpio = CH341_GPIO_NUM_PINS;
+	gpio->can_sleep = true;
+
+	dev->irq_chip.name = dev_name(&pdev->dev);
+	dev->irq_chip.irq_set_type = ch341_gpio_irq_set_type;
+	dev->irq_chip.irq_enable = ch341_gpio_irq_enable;
+	dev->irq_chip.irq_disable = ch341_gpio_irq_disable;
+
+	girq = &gpio->irq;
+	girq->chip = &dev->irq_chip;
+	girq->handler = handle_simple_irq;
+	girq->default_type = IRQ_TYPE_NONE;
+
+	/* Create an URB for handling interrupt */
+	dev->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
+	if (!dev->irq_urb)
+		return dev_err_probe(&pdev->dev, -ENOMEM, "Cannot allocate the int URB\n");
+
+	usb_fill_int_urb(dev->irq_urb, ch341->usb_dev,
+			 usb_rcvintpipe(ch341->usb_dev, ch341->ep_intr),
+			 dev->irq_buf, CH341_USB_MAX_INTR_SIZE,
+			 ch341_complete_intr_urb, dev, ch341->ep_intr_interval);
+
+	init_usb_anchor(&dev->irq_urb_out);
+
+	rc = gpiochip_add_data(gpio, dev);
+	if (rc) {
+		rc = dev_err_probe(&pdev->dev, rc, "Could not add GPIO\n");

drivers/gpio/gpio-ch341.c line 371

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10 -0
+		goto release_urb;
+	}
+
+	return 0;
+
+release_urb:
+	usb_free_urb(dev->irq_urb);
+
+	return rc;
+}

Johan

author
#9 lore raw

Re: [PATCH v5 2/3] gpio: ch341: add GPIO MFD cell driver for the CH341

Quoted 9 lines · Johan Hovold

drivers/gpio/gpio-ch341.c line 239

Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+static void ch341_complete_intr_urb(struct urb *urb)
+{
+	struct ch341_gpio *dev = urb->context;
+	int rc;
+
+	if (urb->status) {
+		usb_unanchor_urb(dev->irq_urb);

URBs are unanchored by USB core on completion.

Fixed.

Quoted 21 lines
Quoted 8 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+static void ch341_gpio_irq_enable(struct irq_data *data)
+{
+	struct ch341_gpio *dev = irq_data_get_irq_chip_data(data);
+	int rc;
+
+	/*
+	 * The URB might have just been unlinked in

drivers/gpio/gpio-ch341.c line 285

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10 -0
+	 * ch341_gpio_irq_disable, but the completion handler hasn't
+	 * been called yet.
+	 */
+	if (!usb_wait_anchor_empty_timeout(&dev->irq_urb_out, 5000))
+		usb_kill_anchored_urbs(&dev->irq_urb_out);
+
+	usb_anchor_urb(dev->irq_urb, &dev->irq_urb_out);
+	rc = usb_submit_urb(dev->irq_urb, GFP_ATOMIC);
+	if (rc)
+		usb_unanchor_urb(dev->irq_urb);

This looks confused and broken.

Quoted 2 lines

usb_kill_anchored_urbs() can sleep so either calling it is broken or using GFP_ATOMIC is unnecessary.

Right, that function can sleep. I changed GFP_ATOMIC to GFP_KERNEL.

Quoted 3 lines

And isn't this function called multiple times when enabling more than one irq?!

There's only one IRQ, so only one URB will be posted at a time. It is reposted as soon as it comes back unless the IRQ is disabled or the device stops.

Quoted 21 lines

drivers/gpio/gpio-ch341.c line 292

Quoted 8 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+}
+
+static void ch341_gpio_irq_disable(struct irq_data *data)
+{
+	struct ch341_gpio *dev = irq_data_get_irq_chip_data(data);
+
+	usb_unlink_urb(dev->irq_urb);

Same here...

drivers/gpio/gpio-ch341.c line 299

Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+}
+
+static int ch341_gpio_remove(struct platform_device *pdev)
+{
+	struct ch341_gpio *dev = platform_get_drvdata(pdev);
+
+	usb_kill_anchored_urbs(&dev->irq_urb_out);

You only have one URB...

Quoted 1 line

And what prevents it from being resubmitted here?

I don't see what would resubmit it here. The gpio is being released.

Frank.

#10 lore raw

Re: [PATCH v5 2/3] gpio: ch341: add GPIO MFD cell driver for the CH341

Quoted 22 lines · Frank Zago
Quoted 20 lines · Johan Hovold
Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+static void ch341_gpio_irq_enable(struct irq_data *data)
+{
+	struct ch341_gpio *dev = irq_data_get_irq_chip_data(data);
+	int rc;
+
+	/*
+	 * The URB might have just been unlinked in

drivers/gpio/gpio-ch341.c line 285

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10 -0
+	 * ch341_gpio_irq_disable, but the completion handler hasn't
+	 * been called yet.
+	 */
+	if (!usb_wait_anchor_empty_timeout(&dev->irq_urb_out, 5000))
+		usb_kill_anchored_urbs(&dev->irq_urb_out);
+
+	usb_anchor_urb(dev->irq_urb, &dev->irq_urb_out);
+	rc = usb_submit_urb(dev->irq_urb, GFP_ATOMIC);
+	if (rc)
+		usb_unanchor_urb(dev->irq_urb);

This looks confused and broken.

Quoted 4 lines
Quoted 2 lines

usb_kill_anchored_urbs() can sleep so either calling it is broken or using GFP_ATOMIC is unnecessary.

Right, that function can sleep. I changed GFP_ATOMIC to GFP_KERNEL.

These callbacks can be called in atomic context so that's not an option, I'm afraid.

Quoted 6 lines
Quoted 2 lines

And isn't this function called multiple times when enabling more than one irq?!

There's only one IRQ, so only one URB will be posted at a time. It is reposted as soon as it comes back unless the IRQ is disabled or the device stops.

AFAICT you have up to 16 (CH341_GPIO_NUM_PINS) interrupts, not one. So I still say this is broken.

Quoted 20 lines
Quoted 20 lines

drivers/gpio/gpio-ch341.c line 292

Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+}
+
+static void ch341_gpio_irq_disable(struct irq_data *data)
+{
+	struct ch341_gpio *dev = irq_data_get_irq_chip_data(data);
+
+	usb_unlink_urb(dev->irq_urb);

Same here...

drivers/gpio/gpio-ch341.c line 299

Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7 -0
+}
+
+static int ch341_gpio_remove(struct platform_device *pdev)
+{
+	struct ch341_gpio *dev = platform_get_drvdata(pdev);
+
+	usb_kill_anchored_urbs(&dev->irq_urb_out);

You only have one URB...

Quoted 3 lines
Quoted 1 line

And what prevents it from being resubmitted here?

I don't see what would resubmit it here. The gpio is being released.

Your implementation needs to handle racing requests. The gpio chip is still registered here.

Johan

author
#11 lore raw

PATCH v5 3/3 i2c: ch341: add I2C MFD cell driver for the CH341

In reply to frank zago

The I2C interface can run at 4 different speeds. This driver currently only offer 100MHz. Tested with a variety of I2C sensors, and the IIO subsystem.

---
 MAINTAINERS                    |   1 +
 drivers/i2c/busses/Kconfig     |  10 +
 drivers/i2c/busses/Makefile    |   1 +
 drivers/i2c/busses/i2c-ch341.c | 331 +++++++++++++++++++++++++++++++++
 drivers/mfd/ch341-core.c       |   3 +
 5 files changed, 346 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-ch341.c
MAINTAINERS +1 -0
index 757ab4f6f9f6..04ec5095e1d0 100644
@@ -21216,6 +21216,7 @@ M:	Frank Zago <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	drivers/gpio/gpio-ch341.c
+F:	drivers/i2c/busses/i2c-ch341.c
 F:	drivers/mfd/ch341-core.c
 F:	include/linux/mfd/ch341.h
 
drivers/i2c/busses/Kconfig +10 -0
index a1bae59208e3..db9797345ad5 100644
@@ -1199,6 +1199,16 @@ config I2C_RCAR
 
 comment "External I2C/SMBus adapter drivers"
 
+config I2C_CH341
+	tristate "CH341 USB to I2C support"
+	select MFD_CH341
+	help
+	  If you say yes to this option, I2C support will be included for the
+	  WCH CH341, a USB to I2C/SPI/GPIO interface.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called i2c-ch341.
+
 config I2C_DIOLAN_U2C
 	tristate "Diolan U2C-12 USB adapter"
 	depends on USB
drivers/i2c/busses/Makefile +1 -0
index 479f60e4ee3d..e83ca4a472f2 100644
@@ -127,6 +127,7 @@ obj-$(CONFIG_I2C_XLP9XX)	+= i2c-xlp9xx.o
 obj-$(CONFIG_I2C_RCAR)		+= i2c-rcar.o
 
 # External I2C/SMBus adapter drivers
+obj-$(CONFIG_I2C_CH341)		+= i2c-ch341.o
 obj-$(CONFIG_I2C_DIOLAN_U2C)	+= i2c-diolan-u2c.o
 obj-$(CONFIG_I2C_DLN2)		+= i2c-dln2.o
 obj-$(CONFIG_I2C_CP2615) += i2c-cp2615.o
drivers/i2c/busses/i2c-ch341.c new file +331 -0

331 lines not on this page · raw message

drivers/mfd/ch341-core.c +3 -0
index e919f6901a14..6a326cac2247 100644
@@ -22,6 +22,9 @@ static const struct mfd_cell ch341_devs[] = {
 	{
 		.name = "ch341-gpio",
 	},
+	{
+		.name = "ch341-i2c",
+	},
 };
 
 static int ch341_usb_probe(struct usb_interface *iface,
Signature · 1 line
2.32.0
#12 lore raw

Re: [PATCH v5 3/3] i2c: ch341: add I2C MFD cell driver for the CH341

Hello!

Quoted 2 lines · frank zago

The I2C interface can run at 4 different speeds. This driver currently only offer 100MHz. Tested with a variety of I2C sensors, and the IIO

Maybe 100 KHz? It's a more common I2C speed. :-)

Quoted 3 lines

subsystem.

[...]

MBR, Sergey

#13 lore raw

Re: [PATCH v5 3/3] i2c: ch341: add I2C MFD cell driver for the CH341

In reply to frank zago

Hi Frank,

I am not super familiar with USB drivers, so mostly some high level review questions first:

Quoted 2 lines · frank zago

The I2C interface can run at 4 different speeds. This driver currently only offer 100MHz. Tested with a variety of I2C sensors, and the IIO

100kHz.

Quoted 3 lines

subsystem.

...

drivers/i2c/busses/Kconfig config I2C_RCAR · line 1204

Quoted 11 lines
drivers/i2c/busses/Kconfig +3 -0
index a1bae59208e3..db9797345ad5 100644
@@ -1199,6 +1199,16 @@ config I2C_RCAR
 
 comment "External I2C/SMBus adapter drivers"
 
+config I2C_CH341
+	tristate "CH341 USB to I2C support"
+	select MFD_CH341

Hmm, it selects a symbol which depends on USB. Not good AFAIK. I think this driver should depend on MFD_CH341.

Quoted 10 lines
drivers/i2c/busses/Kconfig +7 -0
+	help
+	  If you say yes to this option, I2C support will be included for the
+	  WCH CH341, a USB to I2C/SPI/GPIO interface.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called i2c-ch341.
+
 config I2C_DIOLAN_U2C
 	tristate "Diolan U2C-12 USB adapter"
 	depends on USB

...

Quoted 15 lines
drivers/i2c/busses/i2c-ch341.c new file +9 -0
new file mode 100644
index 000000000000..3da11e358976
@@ -0,0 +1,331 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * I2C cell driver for the CH341A, CH341B and CH341T.
+ *
+ * Copyright 2022, Frank Zago
+ * Copyright (c) 2016 Tse Lun Bien
+ * Copyright (c) 2014 Marco Gittler
+ * Copyright (C) 2006-2007 Till Harbaum ([email protected])
+ */

drivers/i2c/busses/i2c-ch341.c line 19

Quoted 10 lines
drivers/i2c/busses/i2c-ch341.c new file +10 -0
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/types.h>
+#include <linux/usb.h>
+
+#include <linux/i2c.h>
+
+#include <linux/mfd/ch341.h>

Please sort the includes. No need for emtpy lines.

Quoted 14 lines
drivers/i2c/busses/i2c-ch341.c new file +14 -0
+
+/* I2C bus speed. Speed selection is not implemented. */
+#define CH341_I2C_20KHZ  0
+#define CH341_I2C_100KHZ 1
+#define CH341_I2C_400KHZ 2
+#define CH341_I2C_750KHZ 3
+
+/* I2C chip commands */
+#define CH341_CMD_I2C_STREAM 0xAA
+#define CH341_CMD_I2C_STM_END 0x00
+
+#define CH341_CMD_I2C_STM_STA 0x74
+#define CH341_CMD_I2C_STM_STO 0x75
+#define CH341_CMD_I2C_STM_OUT 0x80

drivers/i2c/busses/i2c-ch341.c line 43

Quoted 10 lines
drivers/i2c/busses/i2c-ch341.c new file +10 -0
+#define CH341_CMD_I2C_STM_IN 0xC0
+#define CH341_CMD_I2C_STM_SET 0x60
+
+/*
+ * The maximum request size is 4096 bytes, both for reading and
+ * writing, split in up to 128 32-byte segments. The I2C stream must
+ * start and stop in each 32-byte segment. Reading must also be split,
+ * with up to 32-byte per segment.
+ */
+#define SEG_COUNT 128

You mean between every 32 bytes, there is a START and STOP condition on the bus? Then, the maximum message size is 32 byte only, sadly. Or did I misunderstand?

Can the driver send an arbitrary number of messages within one transfer? E.g. write, read, read, write, read? All connected with a REPEATED START and not with STOP and START?

...

drivers/i2c/busses/i2c-ch341.c line 270

Quoted 4 lines
drivers/i2c/busses/i2c-ch341.c new file +4 -0
+static u32 ch341_i2c_func(struct i2c_adapter *adap)
+{
+	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
+}

Have you also tested zero length messages AKA SMBus Quick commands?

...

drivers/i2c/busses/i2c-ch341.c line 328

Quoted 2 lines
drivers/i2c/busses/i2c-ch341.c new file +2 -0
+
+MODULE_AUTHOR("Various");

Please name the relevant authors. Only the ones which directly worked on this driver.

drivers/i2c/busses/i2c-ch341.c line 330

Quoted 2 lines
drivers/i2c/busses/i2c-ch341.c new file +2 -0
+MODULE_DESCRIPTION("CH341 USB to I2C");
+MODULE_LICENSE("GPL");

SPDX header says "GPL v2".

So much for now, thanks for your submission!

Wolfram

#14 lore raw

Re: [PATCH v5 3/3] i2c: ch341: add I2C MFD cell driver for the CH341

Quoted 4 lines · Wolfram Sang

drivers/i2c/busses/i2c-ch341.c line 330

Quoted 2 lines
drivers/i2c/busses/i2c-ch341.c new file +2 -0
+MODULE_DESCRIPTION("CH341 USB to I2C");
+MODULE_LICENSE("GPL");

SPDX header says "GPL v2".

There's no (longer) any difference. See bf7fbeeae6db ("module: Cure the MODULE_LICENSE "GPL" vs. "GPL v2" bogosity").

Johan

#15 lore raw

Re: [PATCH v5 3/3] i2c: ch341: add I2C MFD cell driver for the CH341

Quoted 3 lines

There's no (longer) any difference. See bf7fbeeae6db ("module: Cure the MODULE_LICENSE "GPL" vs. "GPL v2" bogosity").

Wow, cool. Thanks for the heads up! Sidenote: just from the title of this commit I guessed the author correctly ;)

author
#16 lore raw

Re: [PATCH v5 3/3] i2c: ch341: add I2C MFD cell driver for the CH341

In reply to Wolfram Sang

Hi Wolfram,

Quoted 23 lines · Wolfram Sang

Hi Frank,

I am not super familiar with USB drivers, so mostly some high level review questions first:

Quoted 2 lines · frank zago

The I2C interface can run at 4 different speeds. This driver currently only offer 100MHz. Tested with a variety of I2C sensors, and the IIO

100kHz.

Quoted 3 lines

subsystem.

...

Quoted 6 lines
drivers/i2c/busses/Kconfig +0 -0
index a1bae59208e3..db9797345ad5 100644
@@ -1199,6 +1199,16 @@ config I2C_RCAR
 
Quoted 8 lines

drivers/i2c/busses/Kconfig config I2C_RCAR · line 1204

Quoted 5 lines
drivers/i2c/busses/Kconfig +3 -0
 comment "External I2C/SMBus adapter drivers"
 
+config I2C_CH341
+	tristate "CH341 USB to I2C support"
+	select MFD_CH341

Hmm, it selects a symbol which depends on USB. Not good AFAIK. I think this driver should depend on MFD_CH341.

I've been asked earlier to change it to select.

Quoted 4 lines
Quoted 4 lines
drivers/i2c/busses/Kconfig +4 -0
+
+/*
+ * The maximum request size is 4096 bytes, both for reading and
+ * writing, split in up to 128 32-byte segments. The I2C stream must
Quoted 8 lines

drivers/i2c/busses/Kconfig config I2C_RCAR · line 1212

Quoted 4 lines
drivers/i2c/busses/Kconfig +4 -0
+ * start and stop in each 32-byte segment. Reading must also be split,
+ * with up to 32-byte per segment.
+ */
+#define SEG_COUNT 128

You mean between every 32 bytes, there is a START and STOP condition on the bus? Then, the maximum message size is 32 byte only, sadly. Or did I misunderstand?

I've tried to reword that section. The usb command is up to 4kb, but each 32-byte section is a command to the chip, not i2c.

Quoted 4 lines

Can the driver send an arbitrary number of messages within one transfer? E.g. write, read, read, write, read? All connected with a REPEATED START and not with STOP and START?

Yes it can.

Quoted 9 lines

...

drivers/i2c/busses/Kconfig

Quoted 4 lines
drivers/i2c/busses/Kconfig +4 -0
+static u32 ch341_i2c_func(struct i2c_adapter *adap)
+{
+	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
+}

Have you also tested zero length messages AKA SMBus Quick commands?

Not properly at the time, although this didn't break the driver. I've fixed that by adding the special case.

Regards,
Frank

#17 lore raw

Re: [PATCH v5 3/3] i2c: ch341: add I2C MFD cell driver for the CH341

In reply to frank zago
Quoted 4 lines · frank zago

The I2C interface can run at 4 different speeds. This driver currently only offer 100MHz. Tested with a variety of I2C sensors, and the IIO subsystem.

Quoted 8 lines
---
 MAINTAINERS                    |   1 +
 drivers/i2c/busses/Kconfig     |  10 +
 drivers/i2c/busses/Makefile    |   1 +
 drivers/i2c/busses/i2c-ch341.c | 331 +++++++++++++++++++++++++++++++++
 drivers/mfd/ch341-core.c       |   3 +

And this one in its own patch please.

Signature · 4 lines
Lee Jones [李琼斯]
Principal Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog