Skip to contentSkip to conversation

add driver for the WCH CH341 in I2C/GPIO mode

PATCH In review frank zago 9 participants 22 messages Jun 16, 2022 – Nov 9, 2024

Subsystems (5)
lore
Share & tools
lore

frank zago

author
#0 lore raw

v6 cover add driver for the WCH CH341 in I2C/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.

Changes from v5:
Addressed reviewers' comments.
Better handling of 0-bytes i2c commands
Use of better USB API.

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

During testing I found that i2c handles hot removal, but not gpio. The gpio subsystem will complain with 'REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED', but it's a gpiolib issue.

Changes from v1:
- Removed double Signed-off-by
- Move Kconfig into the same directory as the driver

frank zago (4):
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
docs: misc: add documentation for ch341 driver

 Documentation/misc-devices/ch341.rst | 109 ++++++++
 Documentation/misc-devices/index.rst |   1 +
 MAINTAINERS                          |   9 +
 drivers/gpio/Kconfig                 |  10 +
 drivers/gpio/Makefile                |   1 +
 drivers/gpio/gpio-ch341.c            | 385 +++++++++++++++++++++++++++
 drivers/i2c/busses/Kconfig           |  10 +
 drivers/i2c/busses/Makefile          |   1 +
 drivers/i2c/busses/i2c-ch341.c       | 377 ++++++++++++++++++++++++++
 drivers/mfd/Kconfig                  |  10 +
 drivers/mfd/Makefile                 |   1 +
 drivers/mfd/ch341-core.c             |  90 +++++++
 include/linux/mfd/ch341.h            |  26 ++
 13 files changed, 1030 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 v6 1/4 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.

---
 MAINTAINERS               |  7 +++
 drivers/mfd/Kconfig       | 10 +++++
 drivers/mfd/Makefile      |  1 +
 drivers/mfd/ch341-core.c  | 90 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ch341.h | 18 ++++++++
 5 files changed, 126 insertions(+)
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h
MAINTAINERS +7 -0
index 43d3d07afccd..628eeaa9bf68 100644
@@ -21475,6 +21475,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 +90 -0

90 lines not on this page · raw message

include/linux/mfd/ch341.h new file +18 -0
new file mode 100644
index 000000000000..44f5da0720bd
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Definitions for the CH341 driver */
+
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+struct usb_device;
+struct usb_interface;
+
+struct ch341_ddata {
+	struct usb_device *usb_dev;
+	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 v6 1/4] mfd: ch341: add core driver for the WCH CH341 in I2C/SPI/GPIO mode

USB review please.

Quoted 187 lines

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.

---
 MAINTAINERS               |  7 +++
 drivers/mfd/Kconfig       | 10 +++++
 drivers/mfd/Makefile      |  1 +
 drivers/mfd/ch341-core.c  | 90 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ch341.h | 18 ++++++++
 5 files changed, 126 insertions(+)
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h
MAINTAINERS +7 -0
index 43d3d07afccd..628eeaa9bf68 100644
@@ -21475,6 +21475,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 +90 -0
new file mode 100644
index 000000000000..f08a67dd6074
@@ -0,0 +1,90 @@
+// 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>
+#include <linux/usb.h>
+
+static const struct mfd_cell ch341_devs[] = {
+	{
+		.name = "ch341-gpio",
+	},
+	{
+		.name = "ch341-i2c",
+	},
+};
+
+static int ch341_usb_probe(struct usb_interface *iface,
+			   const struct usb_device_id *usb_id)
+{
+	struct usb_endpoint_descriptor *bulk_out;
+	struct usb_endpoint_descriptor *bulk_in;
+	struct usb_endpoint_descriptor *intr_in;
+	struct ch341_ddata *ddata;
+	int ret;
+
+	ddata = devm_kzalloc(&iface->dev, sizeof(*ddata), GFP_KERNEL);
+	if (!ddata)
+		return -ENOMEM;
+
+	ddata->usb_dev = interface_to_usbdev(iface);
+	mutex_init(&ddata->usb_lock);
+
+	ret = usb_find_common_endpoints(iface->cur_altsetting, &bulk_in,
+					&bulk_out, &intr_in, NULL);
+	if (ret) {
+		dev_err(&iface->dev, "Could not find all endpoints\n");
+		return -ENODEV;
+	}
+
+	ddata->ep_in = bulk_in->bEndpointAddress;
+	ddata->ep_out = bulk_out->bEndpointAddress;
+	ddata->ep_intr = intr_in->bEndpointAddress;
+	ddata->ep_intr_interval = intr_in->bInterval;
+
+	usb_set_intfdata(iface, ddata);
+
+	ret = mfd_add_devices(&iface->dev, PLATFORM_DEVID_AUTO, ch341_devs,
+			      ARRAY_SIZE(ch341_devs), NULL, 0, NULL);
+	if (ret)
+		return dev_err_probe(&iface->dev, ret,
+				     "Failed to register child devices\n");
+
+	return 0;
+}
+
+static void ch341_usb_disconnect(struct usb_interface *usb_if)
+{
+	mfd_remove_devices(&usb_if->dev);
+}
+
+static const struct usb_device_id ch341_usb_table[] = {
+	{ USB_DEVICE(0x1a86, 0x5512) },
+	{ }
+};
+MODULE_DEVICE_TABLE(usb, ch341_usb_table);
+
+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("Frank Zago <[email protected]>");
+MODULE_DESCRIPTION("CH341 USB to I2C/SPI/GPIO adapter");
+MODULE_LICENSE("GPL");
include/linux/mfd/ch341.h new file +18 -0
new file mode 100644
index 000000000000..44f5da0720bd
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Definitions for the CH341 driver */
+
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+struct usb_device;
+struct usb_interface;
+
+struct ch341_ddata {
+	struct usb_device *usb_dev;
+	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
maintainer Acked-by
#3 lore raw

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

Quoted 181 lines · Lee Jones

USB review please.

Quoted 179 lines

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.

---
 MAINTAINERS               |  7 +++
 drivers/mfd/Kconfig       | 10 +++++
 drivers/mfd/Makefile      |  1 +
 drivers/mfd/ch341-core.c  | 90 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ch341.h | 18 ++++++++
 5 files changed, 126 insertions(+)
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h
MAINTAINERS +7 -0
index 43d3d07afccd..628eeaa9bf68 100644
@@ -21475,6 +21475,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 +90 -0
new file mode 100644
index 000000000000..f08a67dd6074
@@ -0,0 +1,90 @@
+// 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>
+#include <linux/usb.h>
+
+static const struct mfd_cell ch341_devs[] = {
+	{
+		.name = "ch341-gpio",
+	},
+	{
+		.name = "ch341-i2c",
+	},
+};
+
+static int ch341_usb_probe(struct usb_interface *iface,
+			   const struct usb_device_id *usb_id)
+{
+	struct usb_endpoint_descriptor *bulk_out;
+	struct usb_endpoint_descriptor *bulk_in;
+	struct usb_endpoint_descriptor *intr_in;
+	struct ch341_ddata *ddata;
+	int ret;
+
+	ddata = devm_kzalloc(&iface->dev, sizeof(*ddata), GFP_KERNEL);
+	if (!ddata)
+		return -ENOMEM;
+
+	ddata->usb_dev = interface_to_usbdev(iface);
+	mutex_init(&ddata->usb_lock);
+
+	ret = usb_find_common_endpoints(iface->cur_altsetting, &bulk_in,
+					&bulk_out, &intr_in, NULL);
+	if (ret) {
+		dev_err(&iface->dev, "Could not find all endpoints\n");
+		return -ENODEV;
+	}
+
+	ddata->ep_in = bulk_in->bEndpointAddress;
+	ddata->ep_out = bulk_out->bEndpointAddress;
+	ddata->ep_intr = intr_in->bEndpointAddress;
+	ddata->ep_intr_interval = intr_in->bInterval;
+
+	usb_set_intfdata(iface, ddata);
+
+	ret = mfd_add_devices(&iface->dev, PLATFORM_DEVID_AUTO, ch341_devs,
+			      ARRAY_SIZE(ch341_devs), NULL, 0, NULL);
+	if (ret)
+		return dev_err_probe(&iface->dev, ret,
+				     "Failed to register child devices\n");
+
+	return 0;
+}
+
+static void ch341_usb_disconnect(struct usb_interface *usb_if)
+{
+	mfd_remove_devices(&usb_if->dev);
+}
+
+static const struct usb_device_id ch341_usb_table[] = {
+	{ USB_DEVICE(0x1a86, 0x5512) },
+	{ }
+};
+MODULE_DEVICE_TABLE(usb, ch341_usb_table);
+
+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("Frank Zago <[email protected]>");
+MODULE_DESCRIPTION("CH341 USB to I2C/SPI/GPIO adapter");
+MODULE_LICENSE("GPL");
include/linux/mfd/ch341.h new file +10 -0
new file mode 100644
index 000000000000..44f5da0720bd
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Definitions for the CH341 driver */
+
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+struct usb_device;
+struct usb_interface;
+
+struct ch341_ddata {
Quoted 8 lines

include/linux/mfd/ch341.h line 18

Quoted 8 lines
include/linux/mfd/ch341.h new file +8 -0
+	struct usb_device *usb_dev;
+	struct mutex usb_lock;
+
+	int ep_in;
+	int ep_out;
+	int ep_intr;
+	u8 ep_intr_interval;
+};

Looks sane enough, but doesn't actually do any USB data transfers, maybe that happens somewhere else...

#4 lore raw

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

Quoted 186 lines · Greg Kroah-Hartman
Quoted 185 lines · Lee Jones

USB review please.

Quoted 183 lines

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.

---
 MAINTAINERS               |  7 +++
 drivers/mfd/Kconfig       | 10 +++++
 drivers/mfd/Makefile      |  1 +
 drivers/mfd/ch341-core.c  | 90 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ch341.h | 18 ++++++++
 5 files changed, 126 insertions(+)
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h
MAINTAINERS +7 -0
index 43d3d07afccd..628eeaa9bf68 100644
@@ -21475,6 +21475,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 +90 -0
new file mode 100644
index 000000000000..f08a67dd6074
@@ -0,0 +1,90 @@
+// 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>
+#include <linux/usb.h>
+
+static const struct mfd_cell ch341_devs[] = {
+	{
+		.name = "ch341-gpio",
+	},
+	{
+		.name = "ch341-i2c",
+	},
+};
+
+static int ch341_usb_probe(struct usb_interface *iface,
+			   const struct usb_device_id *usb_id)
+{
+	struct usb_endpoint_descriptor *bulk_out;
+	struct usb_endpoint_descriptor *bulk_in;
+	struct usb_endpoint_descriptor *intr_in;
+	struct ch341_ddata *ddata;
+	int ret;
+
+	ddata = devm_kzalloc(&iface->dev, sizeof(*ddata), GFP_KERNEL);
+	if (!ddata)
+		return -ENOMEM;
+
+	ddata->usb_dev = interface_to_usbdev(iface);
+	mutex_init(&ddata->usb_lock);
+
+	ret = usb_find_common_endpoints(iface->cur_altsetting, &bulk_in,
+					&bulk_out, &intr_in, NULL);
+	if (ret) {
+		dev_err(&iface->dev, "Could not find all endpoints\n");
+		return -ENODEV;
+	}
+
+	ddata->ep_in = bulk_in->bEndpointAddress;
+	ddata->ep_out = bulk_out->bEndpointAddress;
+	ddata->ep_intr = intr_in->bEndpointAddress;
+	ddata->ep_intr_interval = intr_in->bInterval;
+
+	usb_set_intfdata(iface, ddata);
+
+	ret = mfd_add_devices(&iface->dev, PLATFORM_DEVID_AUTO, ch341_devs,
+			      ARRAY_SIZE(ch341_devs), NULL, 0, NULL);
+	if (ret)
+		return dev_err_probe(&iface->dev, ret,
+				     "Failed to register child devices\n");
+
+	return 0;
+}
+
+static void ch341_usb_disconnect(struct usb_interface *usb_if)
+{
+	mfd_remove_devices(&usb_if->dev);
+}
+
+static const struct usb_device_id ch341_usb_table[] = {
+	{ USB_DEVICE(0x1a86, 0x5512) },
+	{ }
+};
+MODULE_DEVICE_TABLE(usb, ch341_usb_table);
+
+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("Frank Zago <[email protected]>");
+MODULE_DESCRIPTION("CH341 USB to I2C/SPI/GPIO adapter");
+MODULE_LICENSE("GPL");
include/linux/mfd/ch341.h new file +14 -0
new file mode 100644
index 000000000000..44f5da0720bd
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Definitions for the CH341 driver */
+
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+struct usb_device;
+struct usb_interface;
+
+struct ch341_ddata {
+	struct usb_device *usb_dev;
+	struct mutex usb_lock;
+
+	int ep_in;
Quoted 8 lines
Quoted 4 lines

include/linux/mfd/ch341.h line 18

Quoted 4 lines
include/linux/mfd/ch341.h new file +4 -0
+	int ep_out;
+	int ep_intr;
+	u8 ep_intr_interval;
+};

Looks sane enough, but doesn't actually do any USB data transfers, maybe that happens somewhere else...

I expect those to happen in *both* of these:

  static const struct mfd_cell ch341_devs[] = {
	{
		.name = "ch341-gpio",
	},
	{
		.name = "ch341-i2c",
	},

};

Is that correct Frank?

Quoted 1 line

Thanks.

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

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

Quoted 206 lines · Lee Jones
Quoted 186 lines · Greg Kroah-Hartman
Quoted 185 lines · Lee Jones

USB review please.

Quoted 183 lines

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.

---
 MAINTAINERS               |  7 +++
 drivers/mfd/Kconfig       | 10 +++++
 drivers/mfd/Makefile      |  1 +
 drivers/mfd/ch341-core.c  | 90 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ch341.h | 18 ++++++++
 5 files changed, 126 insertions(+)
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h
MAINTAINERS +7 -0
index 43d3d07afccd..628eeaa9bf68 100644
@@ -21475,6 +21475,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 +90 -0
new file mode 100644
index 000000000000..f08a67dd6074
@@ -0,0 +1,90 @@
+// 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>
+#include <linux/usb.h>
+
+static const struct mfd_cell ch341_devs[] = {
+	{
+		.name = "ch341-gpio",
+	},
+	{
+		.name = "ch341-i2c",
+	},
+};
+
+static int ch341_usb_probe(struct usb_interface *iface,
+			   const struct usb_device_id *usb_id)
+{
+	struct usb_endpoint_descriptor *bulk_out;
+	struct usb_endpoint_descriptor *bulk_in;
+	struct usb_endpoint_descriptor *intr_in;
+	struct ch341_ddata *ddata;
+	int ret;
+
+	ddata = devm_kzalloc(&iface->dev, sizeof(*ddata), GFP_KERNEL);
+	if (!ddata)
+		return -ENOMEM;
+
+	ddata->usb_dev = interface_to_usbdev(iface);
+	mutex_init(&ddata->usb_lock);
+
+	ret = usb_find_common_endpoints(iface->cur_altsetting, &bulk_in,
+					&bulk_out, &intr_in, NULL);
+	if (ret) {
+		dev_err(&iface->dev, "Could not find all endpoints\n");
+		return -ENODEV;
+	}
+
+	ddata->ep_in = bulk_in->bEndpointAddress;
+	ddata->ep_out = bulk_out->bEndpointAddress;
+	ddata->ep_intr = intr_in->bEndpointAddress;
+	ddata->ep_intr_interval = intr_in->bInterval;
+
+	usb_set_intfdata(iface, ddata);
+
+	ret = mfd_add_devices(&iface->dev, PLATFORM_DEVID_AUTO, ch341_devs,
+			      ARRAY_SIZE(ch341_devs), NULL, 0, NULL);
+	if (ret)
+		return dev_err_probe(&iface->dev, ret,
+				     "Failed to register child devices\n");
+
+	return 0;
+}
+
+static void ch341_usb_disconnect(struct usb_interface *usb_if)
+{
+	mfd_remove_devices(&usb_if->dev);
+}
+
+static const struct usb_device_id ch341_usb_table[] = {
+	{ USB_DEVICE(0x1a86, 0x5512) },
+	{ }
+};
+MODULE_DEVICE_TABLE(usb, ch341_usb_table);
+
+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("Frank Zago <[email protected]>");
+MODULE_DESCRIPTION("CH341 USB to I2C/SPI/GPIO adapter");
+MODULE_LICENSE("GPL");
include/linux/mfd/ch341.h new file +14 -0
new file mode 100644
index 000000000000..44f5da0720bd
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Definitions for the CH341 driver */
+
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+struct usb_device;
+struct usb_interface;
+
+struct ch341_ddata {
+	struct usb_device *usb_dev;
+	struct mutex usb_lock;
+
+	int ep_in;
Quoted 8 lines
Quoted 4 lines

include/linux/mfd/ch341.h line 18

Quoted 4 lines
include/linux/mfd/ch341.h new file +4 -0
+	int ep_out;
+	int ep_intr;
+	u8 ep_intr_interval;
+};

Looks sane enough, but doesn't actually do any USB data transfers, maybe that happens somewhere else...

I expect those to happen in *both* of these:

  static const struct mfd_cell ch341_devs[] = {
	{
		.name = "ch341-gpio",
	},
	{
		.name = "ch341-i2c",
	},
Quoted 3 lines

};

Is that correct Frank?

Yes, that's correct.

Frank

#6 lore raw

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

In reply to frank zago
Quoted 90 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.

---
 MAINTAINERS               |  7 +++
 drivers/mfd/Kconfig       | 10 +++++
 drivers/mfd/Makefile      |  1 +
 drivers/mfd/ch341-core.c  | 90 +++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ch341.h | 18 ++++++++
 5 files changed, 126 insertions(+)
 create mode 100644 drivers/mfd/ch341-core.c
 create mode 100644 include/linux/mfd/ch341.h
MAINTAINERS +7 -0
index 43d3d07afccd..628eeaa9bf68 100644
@@ -21475,6 +21475,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..f08a67dd6074
@@ -0,0 +1,90 @@
+// 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[] = {
+	{
+		.name = "ch341-gpio",
+	},
+	{
+		.name = "ch341-i2c",
+	},
+};

These should both be on one line each.

Quoted 32 lines
drivers/mfd/ch341-core.c new file +32 -0
+static int ch341_usb_probe(struct usb_interface *iface,
+			   const struct usb_device_id *usb_id)
+{
+	struct usb_endpoint_descriptor *bulk_out;
+	struct usb_endpoint_descriptor *bulk_in;
+	struct usb_endpoint_descriptor *intr_in;
+	struct ch341_ddata *ddata;
+	int ret;
+
+	ddata = devm_kzalloc(&iface->dev, sizeof(*ddata), GFP_KERNEL);
+	if (!ddata)
+		return -ENOMEM;
+
+	ddata->usb_dev = interface_to_usbdev(iface);
+	mutex_init(&ddata->usb_lock);
+
+	ret = usb_find_common_endpoints(iface->cur_altsetting, &bulk_in,
+					&bulk_out, &intr_in, NULL);
+	if (ret) {
+		dev_err(&iface->dev, "Could not find all endpoints\n");
+		return -ENODEV;
+	}
+
+	ddata->ep_in = bulk_in->bEndpointAddress;
+	ddata->ep_out = bulk_out->bEndpointAddress;
+	ddata->ep_intr = intr_in->bEndpointAddress;
+	ddata->ep_intr_interval = intr_in->bInterval;
+
+	usb_set_intfdata(iface, ddata);
+
+	ret = mfd_add_devices(&iface->dev, PLATFORM_DEVID_AUTO, ch341_devs,
+			      ARRAY_SIZE(ch341_devs), NULL, 0, NULL);

drivers/mfd/ch341-core.c line 71

Quoted 10 lines
drivers/mfd/ch341-core.c new file +10 -0
+	if (ret)
+		return dev_err_probe(&iface->dev, ret,
+				     "Failed to register child devices\n");
+
+	return 0;
+}
+
+static void ch341_usb_disconnect(struct usb_interface *usb_if)
+{
+	mfd_remove_devices(&usb_if->dev);

Why not use the devm_* version?

Quoted 25 lines
drivers/mfd/ch341-core.c new file +19 -0
+}
+
+static const struct usb_device_id ch341_usb_table[] = {
+	{ USB_DEVICE(0x1a86, 0x5512) },
+	{ }
+};
+MODULE_DEVICE_TABLE(usb, ch341_usb_table);
+
+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("Frank Zago <[email protected]>");
+MODULE_DESCRIPTION("CH341 USB to I2C/SPI/GPIO adapter");
+MODULE_LICENSE("GPL");
include/linux/mfd/ch341.h new file +0 -0
new file mode 100644
index 000000000000..44f5da0720bd

include/linux/mfd/ch341.h line 2

Quoted 2 lines
include/linux/mfd/ch341.h new file +2 -0
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Definitions for the CH341 driver */

What definitions?

Quoted 16 lines
include/linux/mfd/ch341.h new file +16 -0
+
+#include <linux/mutex.h>
+#include <linux/types.h>
+
+struct usb_device;
+struct usb_interface;
+
+struct ch341_ddata {
+	struct usb_device *usb_dev;
+	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
#7 lore raw

PATCH v6 2/4 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 | 385 ++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ch341.h |   8 +
 5 files changed, 405 insertions(+)
 create mode 100644 drivers/gpio/gpio-ch341.c
MAINTAINERS +1 -0
index 628eeaa9bf68..8b93f6192704 100644
@@ -21479,6 +21479,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 b01961999ced..473b6e7226ca 100644
@@ -1653,6 +1653,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 +385 -0

385 lines not on this page · raw message

include/linux/mfd/ch341.h +8 -0
index 44f5da0720bd..804d671fbe45 100644
@@ -4,6 +4,14 @@
 #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;
 
Signature · 1 line
2.32.0
Acked-by on v6 2/4
#8 lore raw

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

Quoted 4 lines · frank zago

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

Please preserve my ACK on subsequent postings, USB changes does not concern me, I trust Johan to review those for you.

Yours,
Linus Walleij

#9 lore raw

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

In reply to frank zago
Quoted 2 lines · frank zago

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

Please mention the single interrupt line here too.

Quoted 27 lines
---
 MAINTAINERS               |   1 +
 drivers/gpio/Kconfig      |  10 +
 drivers/gpio/Makefile     |   1 +
 drivers/gpio/gpio-ch341.c | 385 ++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/ch341.h |   8 +
 5 files changed, 405 insertions(+)
 create mode 100644 drivers/gpio/gpio-ch341.c
MAINTAINERS +1 -0
index 628eeaa9bf68..8b93f6192704 100644
@@ -21479,6 +21479,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 +0 -0
index b01961999ced..473b6e7226ca 100644

drivers/gpio/Kconfig endmenu · line 1658

Quoted 6 lines
drivers/gpio/Kconfig +3 -0
@@ -1653,6 +1653,16 @@ endmenu
 menu "USB GPIO expanders"
 	depends on USB
 
+config GPIO_CH341
+	tristate "CH341 USB to GPIO support"
+	select MFD_CH341

This should be "depends" (for all subdrivers).

drivers/gpio/Kconfig endmenu · line 1668

Quoted 10 lines
drivers/gpio/Kconfig +7 -0
+	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

[ and other MFD drivers as you see here ]

Quoted 18 lines
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 +0 -0
new file mode 100644
index 000000000000..233dfe1f2ae8

drivers/gpio/gpio-ch341.c line 5

Quoted 5 lines
drivers/gpio/gpio-ch341.c new file +5 -0
@@ -0,0 +1,385 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * GPIO cell driver for the CH341A and CH341B chips.
+ *
+ * Copyright 2022, Frank Zago

How about using a unified format for these lines?

Quoted 33 lines
drivers/gpio/gpio-ch341.c new file +33 -0
+ * 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])
+ */
+
+/*
+ * Notes.
+ *
+ * For the CH341, 0=IN, 1=OUT, but for the GPIO subsystem, 1=IN and
+ * 0=OUT. Translation happens in a couple places.
+ */
+
+#include <linux/gpio/driver.h>
+#include <linux/kernel.h>
+#include <linux/mfd/ch341.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/types.h>
+#include <linux/usb.h>
+
+#define CH341_GPIO_NUM_PINS         16    /* Number of GPIO pins */
+
+/* GPIO chip commands */
+#define CH341_PARA_CMD_STS          0xA0  /* Get pins status */
+#define CH341_CMD_UIO_STREAM        0xAB  /* pin IO stream command */
+
+#define CH341_CMD_UIO_STM_OUT       0x80  /* pin IO interface OUT command (D0~D5) */
+#define CH341_CMD_UIO_STM_DIR       0x40  /* pin IO interface DIR command (D0~D5) */
+#define CH341_CMD_UIO_STM_END       0x20  /* pin IO interface END command */
+
+#define CH341_USB_MAX_INTR_SIZE 8
+

drivers/gpio/gpio-ch341.c line 48

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

This one should be allocated separately from the containing structure since it may be mapped for DMA.

drivers/gpio/gpio-ch341.c line 52

Quoted 4 lines
drivers/gpio/gpio-ch341.c new file +4 -0
+
+	struct urb *irq_urb;
+	struct usb_anchor irq_urb_out;
+	u8 irq_buf[CH341_USB_MAX_INTR_SIZE];

Same here.

You may need to check other drivers too.

Quoted 43 lines
drivers/gpio/gpio-ch341.c new file +43 -0
+	struct irq_chip irq_chip;
+
+	struct ch341_ddata *ddata;
+};
+
+/*
+ * Masks to describe the 16 GPIOs. Pins D0 to D5 (mapped to GPIOs 0 to
+ * 5) can do input/output, but the other pins are input-only.
+ */
+static const u16 pin_can_output = 0b111111;
+
+/* Only GPIO 10 (INT# line) has hardware interrupt */
+#define CH341_GPIO_INT_LINE 10
+
+/* Send a command and get a reply if requested */
+static int gpio_transfer(struct ch341_gpio *dev, int out_len, int in_len)
+{
+	struct ch341_ddata *ddata = dev->ddata;
+	int actual;
+	int ret;
+
+	mutex_lock(&ddata->usb_lock);
+
+	ret = usb_bulk_msg(ddata->usb_dev,
+			   usb_sndbulkpipe(ddata->usb_dev, ddata->ep_out),
+			   dev->gpio_buf, out_len,
+			   &actual, DEFAULT_TIMEOUT_MS);
+	if (ret < 0)
+		goto out_unlock;
+
+	if (in_len == 0)
+		goto out_unlock;
+
+	ret = usb_bulk_msg(ddata->usb_dev,
+			   usb_rcvbulkpipe(ddata->usb_dev, ddata->ep_in),
+			   dev->gpio_buf, SEG_SIZE, &actual, DEFAULT_TIMEOUT_MS);
+
+out_unlock:
+	mutex_unlock(&ddata->usb_lock);
+
+	if (ret < 0)
+		return ret;
+

drivers/gpio/gpio-ch341.c

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

Again, why is this here?

Quoted 6 lines
drivers/gpio/gpio-ch341.c new file +6 -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

drivers/gpio/gpio-ch341.c line 253

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10 -0
+		 * 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));
+
+		ret = usb_submit_urb(dev->irq_urb, GFP_ATOMIC);
+		if (ret)
+			usb_unanchor_urb(dev->irq_urb);

Ditto. This isn't how anchors are used.

drivers/gpio/gpio-ch341.c line 262

Quoted 9 lines
drivers/gpio/gpio-ch341.c new file +9 -0
+	}
+}
+
+static int ch341_gpio_irq_set_type(struct irq_data *data, unsigned int flow_type)
+{
+	const unsigned long offset = irqd_to_hwirq(data);
+
+	if (offset != CH341_GPIO_INT_LINE || flow_type != IRQ_TYPE_EDGE_RISING)
+		return -EINVAL;

Ok, I missed this check before, sorry. So you do have a single interrupt line.

You should probably add a comment in the enable() callback since this is easy to miss.

Quoted 6 lines
drivers/gpio/gpio-ch341.c new file +6 -0
+
+	return 0;
+}
+
+static void ch341_gpio_irq_enable(struct irq_data *data)
+{

drivers/gpio/gpio-ch341.c line 278

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10 -0
+	struct ch341_gpio *dev = irq_data_get_irq_chip_data(data);
+	int ret;
+
+	/*
+	 * The URB might have just been unlinked in
+	 * 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);

You cannot sleep in this function.

Quoted 9 lines
drivers/gpio/gpio-ch341.c new file +9 -0
+
+	usb_anchor_urb(dev->irq_urb, &dev->irq_urb_out);
+	ret = usb_submit_urb(dev->irq_urb, GFP_KERNEL);
+	if (ret)
+		usb_unanchor_urb(dev->irq_urb);
+}
+
+static void ch341_gpio_irq_disable(struct irq_data *data)
+{

drivers/gpio/gpio-ch341.c line 297

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10 -0
+	struct ch341_gpio *dev = irq_data_get_irq_chip_data(data);
+
+	usb_unlink_urb(dev->irq_urb);
+}
+
+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);

Again, this is racy as the gpio chip is still registered. And you don't need an anchor to stop a single URB.

Quoted 45 lines
drivers/gpio/gpio-ch341.c new file +45 -0
+	gpiochip_remove(&dev->gpio);
+	usb_free_urb(dev->irq_urb);
+
+	return 0;
+}
+
+static const struct irq_chip ch341_irqchip = {
+	.name = "CH341",
+	.irq_set_type = ch341_gpio_irq_set_type,
+	.irq_enable = ch341_gpio_irq_enable,
+	.irq_disable = ch341_gpio_irq_disable,
+	.flags = IRQCHIP_IMMUTABLE,
+	GPIOCHIP_IRQ_RESOURCE_HELPERS,
+};
+
+static int ch341_gpio_probe(struct platform_device *pdev)
+{
+	struct ch341_ddata *ddata = dev_get_drvdata(pdev->dev.parent);
+	struct gpio_irq_chip *girq;
+	struct ch341_gpio *dev;
+	struct gpio_chip *gpio;
+	int ret;
+
+	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
+	if (dev == NULL)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, dev);
+	dev->ddata = ddata;
+	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;

drivers/gpio/gpio-ch341.c line 352

Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10 -0
+
+	girq = &gpio->irq;
+	gpio_irq_chip_set_chip(girq, &ch341_irqchip);
+	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");

This isn't how dev_err_probe() is used.

And allocation failures are already logged so just return -ENOMEM here.

Quoted 11 lines
drivers/gpio/gpio-ch341.c new file +11 -0
+
+	usb_fill_int_urb(dev->irq_urb, ddata->usb_dev,
+			 usb_rcvintpipe(ddata->usb_dev, ddata->ep_intr),
+			 dev->irq_buf, CH341_USB_MAX_INTR_SIZE,
+			 ch341_complete_intr_urb, dev, ddata->ep_intr_interval);
+
+	init_usb_anchor(&dev->irq_urb_out);
+
+	ret = gpiochip_add_data(gpio, dev);
+	if (ret) {
+		ret = dev_err_probe(&pdev->dev, ret, "Could not add GPIO\n");

drivers/gpio/gpio-ch341.c line 373

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 ret;
+}

Johan

#10 lore raw

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

Quoted 1 line · Johan Hovold

On Wed, Jun 15, 2022 at 08:37:45PM -0500, frank zago wrote:

...

Quoted 6 lines

drivers/gpio/gpio-ch341.c line 352

Quoted 4 lines
drivers/gpio/gpio-ch341.c new file +4 -0
+     /* 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");

This isn't how dev_err_probe() is used.

While I agree on the below comment, what does this imply?

Quoted 1 line

And allocation failures are already logged so just return -ENOMEM here.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

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

Quoted 5 lines · Andy Shevchenko
Quoted 1 line · Johan Hovold

On Wed, Jun 15, 2022 at 08:37:45PM -0500, frank zago wrote:

...

Quoted 8 lines
Quoted 6 lines

drivers/gpio/gpio-ch341.c line 352

Quoted 4 lines
drivers/gpio/gpio-ch341.c new file +4 -0
+     /* 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");

This isn't how dev_err_probe() is used.

While I agree on the below comment, what does this imply?

That you should only use dev_err_probe() to handle -EPROBE_DEFER. That's what it is was added for, documented as, and it is what the implementation still tells you.

I see now that there has recently been some mission creep:

7065f92255bb ("driver core: Clarify that dev_err_probe() is OK even w/out -EPROBE_DEFER")

I'm not sure I agree with that, but fortunately we don't need to have that debate in this case due to the below.

Quoted 1 line
Quoted 1 line

And allocation failures are already logged so just return -ENOMEM here.

Johan

author
#12 lore raw

PATCH v6 3/4 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 100kHz. 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 | 377 +++++++++++++++++++++++++++++++++
 4 files changed, 389 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-ch341.c
MAINTAINERS +1 -0
index 8b93f6192704..837065fd3150 100644
@@ -21480,6 +21480,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 +377 -0

377 lines not on this page · raw message

Signature · 1 line
2.32.0
reviewer
#13 lore raw

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

Hi--

Quoted 21 lines · frank zago

The I2C interface can run at 4 different speeds. This driver currently only offer 100kHz. 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 | 377 +++++++++++++++++++++++++++++++++
 4 files changed, 389 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-ch341.c
drivers/i2c/busses/Kconfig +0 -0
index a1bae59208e3..db9797345ad5 100644
@@ -1199,6 +1199,16 @@ config I2C_RCAR
 
 comment "External I2C/SMBus adapter drivers"

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

Quoted 10 lines
drivers/i2c/busses/Kconfig +9 -0
 
+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.

I haven't tested it, but just from reading, it looks like this one needs a "depends on USB" since it selects MFD_CH341, which depends on USB, and since 'select' ignores dependency chains.

The GPIO driver (patch 2/4) already depends on USB.

Signature · 1 line
~Randy
#14 lore raw

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

In reply to frank zago
Thank you for the patch! Yet something to improve:

Hi frank,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on lee-mfd/for-mfd-next]
[also build test ERROR on brgl/gpio/for-next wsa/i2c/for-next linus/master v5.19-rc2 next-20220615] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/frank-zago/add-driver-for-the-WCH-CH341-in-I2C-GPIO-mode/20220616-094113
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
config: nios2-randconfig-r026-20220616 (https://download.01.org/0day-ci/archive/20220616/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/46c68aee86d34ee630272146a73ad7c3147bb094
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review frank-zago/add-driver-for-the-WCH-CH341-in-I2C-GPIO-mode/20220616-094113 git checkout 46c68aee86d34ee630272146a73ad7c3147bb094
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=nios2 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

nios2-linux-ld: drivers/mfd/ch341-core.o: in function `ch341_usb_probe':

>> drivers/mfd/ch341-core.c:46: undefined reference to `usb_find_common_endpoints'
   drivers/mfd/ch341-core.c:46:(.text+0x8c): relocation truncated to fit: R_NIOS2_CALL26 against `usb_find_common_endpoints'
   nios2-linux-ld: drivers/mfd/ch341-core.o: in function `ch341_usb_driver_init':
>> drivers/mfd/ch341-core.c:86: undefined reference to `usb_register_driver'
   drivers/mfd/ch341-core.c:86:(.init.text+0x1c): relocation truncated to fit: R_NIOS2_CALL26 against `usb_register_driver'
   nios2-linux-ld: drivers/i2c/busses/i2c-ch341.o: in function `no_data_xfer':
>> drivers/i2c/busses/i2c-ch341.c:81: undefined reference to `usb_bulk_msg'
   drivers/i2c/busses/i2c-ch341.c:81:(.text+0xe0): relocation truncated to fit: R_NIOS2_CALL26 against `usb_bulk_msg'
>> nios2-linux-ld: drivers/i2c/busses/i2c-ch341.c:89: undefined reference to `usb_bulk_msg'

drivers/i2c/busses/i2c-ch341.c:89:(.text+0x134): relocation truncated to fit: R_NIOS2_CALL26 against `usb_bulk_msg'
nios2-linux-ld: drivers/i2c/busses/i2c-ch341.o: in function `ch341_i2c_xfer':
drivers/i2c/busses/i2c-ch341.c:278: undefined reference to usb_bulk_msg' drivers/i2c/busses/i2c-ch341.c:278:(.text+0x4e0): relocation truncated to fit: R_NIOS2_CALL26 against usb_bulk_msg'
nios2-linux-ld: drivers/i2c/busses/i2c-ch341.c:290: undefined reference to usb_bulk_msg' drivers/i2c/busses/i2c-ch341.c:290:(.text+0x558): relocation truncated to fit: R_NIOS2_CALL26 against usb_bulk_msg'
nios2-linux-ld: drivers/i2c/busses/i2c-ch341.o: in function `ch341_i2c_probe':
drivers/i2c/busses/i2c-ch341.c:357: undefined reference to usb_bulk_msg' drivers/i2c/busses/i2c-ch341.c:357:(.text+0x778): relocation truncated to fit: R_NIOS2_CALL26 against usb_bulk_msg'
pahole: .tmp_vmlinux.btf: No such file or directory
.btf.vmlinux.bin.o: file not recognized: file format not recognized

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for MFD_CH341 Depends on HAS_IOMEM && USB
Selected by
- I2C_CH341 && I2C && HAS_IOMEM

vim +46 drivers/mfd/ch341-core.c

f33ee28d4d85a8 frank zago 2022-06-15  29  
f33ee28d4d85a8 frank zago 2022-06-15  30  static int ch341_usb_probe(struct usb_interface *iface,
f33ee28d4d85a8 frank zago 2022-06-15  31  			   const struct usb_device_id *usb_id)
f33ee28d4d85a8 frank zago 2022-06-15  32  {
f33ee28d4d85a8 frank zago 2022-06-15  33  	struct usb_endpoint_descriptor *bulk_out;
f33ee28d4d85a8 frank zago 2022-06-15  34  	struct usb_endpoint_descriptor *bulk_in;
f33ee28d4d85a8 frank zago 2022-06-15  35  	struct usb_endpoint_descriptor *intr_in;
f33ee28d4d85a8 frank zago 2022-06-15  36  	struct ch341_ddata *ddata;
f33ee28d4d85a8 frank zago 2022-06-15  37  	int ret;
f33ee28d4d85a8 frank zago 2022-06-15  38  
f33ee28d4d85a8 frank zago 2022-06-15  39  	ddata = devm_kzalloc(&iface->dev, sizeof(*ddata), GFP_KERNEL);
f33ee28d4d85a8 frank zago 2022-06-15  40  	if (!ddata)
f33ee28d4d85a8 frank zago 2022-06-15  41  		return -ENOMEM;
f33ee28d4d85a8 frank zago 2022-06-15  42  
f33ee28d4d85a8 frank zago 2022-06-15  43  	ddata->usb_dev = interface_to_usbdev(iface);
f33ee28d4d85a8 frank zago 2022-06-15  44  	mutex_init(&ddata->usb_lock);
f33ee28d4d85a8 frank zago 2022-06-15  45  
f33ee28d4d85a8 frank zago 2022-06-15 @46  	ret = usb_find_common_endpoints(iface->cur_altsetting, &bulk_in,
f33ee28d4d85a8 frank zago 2022-06-15  47  					&bulk_out, &intr_in, NULL);
f33ee28d4d85a8 frank zago 2022-06-15  48  	if (ret) {
f33ee28d4d85a8 frank zago 2022-06-15  49  		dev_err(&iface->dev, "Could not find all endpoints\n");
f33ee28d4d85a8 frank zago 2022-06-15  50  		return -ENODEV;
f33ee28d4d85a8 frank zago 2022-06-15  51  	}
f33ee28d4d85a8 frank zago 2022-06-15  52  
f33ee28d4d85a8 frank zago 2022-06-15  53  	ddata->ep_in = bulk_in->bEndpointAddress;
f33ee28d4d85a8 frank zago 2022-06-15  54  	ddata->ep_out = bulk_out->bEndpointAddress;
f33ee28d4d85a8 frank zago 2022-06-15  55  	ddata->ep_intr = intr_in->bEndpointAddress;
f33ee28d4d85a8 frank zago 2022-06-15  56  	ddata->ep_intr_interval = intr_in->bInterval;
f33ee28d4d85a8 frank zago 2022-06-15  57  
f33ee28d4d85a8 frank zago 2022-06-15  58  	usb_set_intfdata(iface, ddata);
f33ee28d4d85a8 frank zago 2022-06-15  59  
f33ee28d4d85a8 frank zago 2022-06-15  60  	ret = mfd_add_devices(&iface->dev, PLATFORM_DEVID_AUTO, ch341_devs,
f33ee28d4d85a8 frank zago 2022-06-15  61  			      ARRAY_SIZE(ch341_devs), NULL, 0, NULL);
f33ee28d4d85a8 frank zago 2022-06-15  62  	if (ret)
f33ee28d4d85a8 frank zago 2022-06-15  63  		return dev_err_probe(&iface->dev, ret,
f33ee28d4d85a8 frank zago 2022-06-15  64  				     "Failed to register child devices\n");
f33ee28d4d85a8 frank zago 2022-06-15  65  
f33ee28d4d85a8 frank zago 2022-06-15  66  	return 0;
f33ee28d4d85a8 frank zago 2022-06-15  67  }
f33ee28d4d85a8 frank zago 2022-06-15  68  
f33ee28d4d85a8 frank zago 2022-06-15  69  static void ch341_usb_disconnect(struct usb_interface *usb_if)
f33ee28d4d85a8 frank zago 2022-06-15  70  {
f33ee28d4d85a8 frank zago 2022-06-15  71  	mfd_remove_devices(&usb_if->dev);
f33ee28d4d85a8 frank zago 2022-06-15  72  }
f33ee28d4d85a8 frank zago 2022-06-15  73  
f33ee28d4d85a8 frank zago 2022-06-15  74  static const struct usb_device_id ch341_usb_table[] = {
f33ee28d4d85a8 frank zago 2022-06-15  75  	{ USB_DEVICE(0x1a86, 0x5512) },
f33ee28d4d85a8 frank zago 2022-06-15  76  	{ }
f33ee28d4d85a8 frank zago 2022-06-15  77  };
f33ee28d4d85a8 frank zago 2022-06-15  78  MODULE_DEVICE_TABLE(usb, ch341_usb_table);
f33ee28d4d85a8 frank zago 2022-06-15  79  
f33ee28d4d85a8 frank zago 2022-06-15  80  static struct usb_driver ch341_usb_driver = {
f33ee28d4d85a8 frank zago 2022-06-15  81  	.name       = "ch341-mfd",
f33ee28d4d85a8 frank zago 2022-06-15  82  	.id_table   = ch341_usb_table,
f33ee28d4d85a8 frank zago 2022-06-15  83  	.probe      = ch341_usb_probe,
f33ee28d4d85a8 frank zago 2022-06-15  84  	.disconnect = ch341_usb_disconnect,
f33ee28d4d85a8 frank zago 2022-06-15  85  };

f33ee28d4d85a8 frank zago 2022-06-15 @86 module_usb_driver(ch341_usb_driver); f33ee28d4d85a8 frank zago 2022-06-15 87

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#15 lore raw

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

In reply to frank zago
Thank you for the patch! Yet something to improve:

Hi frank,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on lee-mfd/for-mfd-next]
[also build test ERROR on brgl/gpio/for-next wsa/i2c/for-next linus/master v5.19-rc2 next-20220616] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/frank-zago/add-driver-for-the-WCH-CH341-in-I2C-GPIO-mode/20220616-094113
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
config: arc-buildonly-randconfig-r004-20220616 (https://download.01.org/0day-ci/archive/20220616/[email protected]/config)
compiler: arc-elf-gcc (GCC) 11.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/46c68aee86d34ee630272146a73ad7c3147bb094
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review frank-zago/add-driver-for-the-WCH-CH341-in-I2C-GPIO-mode/20220616-094113 git checkout 46c68aee86d34ee630272146a73ad7c3147bb094
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "usb_bulk_msg" [drivers/i2c/busses/i2c-ch341.ko] undefined!
>> ERROR: modpost: "usb_deregister" [drivers/mfd/ch341-core.ko] undefined!
>> ERROR: modpost: "usb_register_driver" [drivers/mfd/ch341-core.ko] undefined!
>> ERROR: modpost: "usb_find_common_endpoints" [drivers/mfd/ch341-core.ko] undefined!

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for MFD_CH341 Depends on HAS_IOMEM && USB
Selected by
- I2C_CH341 && I2C && HAS_IOMEM

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
author
#16 lore raw

PATCH v6 4/4 docs: misc: add documentation for ch341 driver

In reply to frank zago

Document the multifunction CH341 chip driver, in GPIO and I2C mode.

---
 Documentation/misc-devices/ch341.rst | 109 +++++++++++++++++++++++++++
 Documentation/misc-devices/index.rst |   1 +
 2 files changed, 110 insertions(+)
 create mode 100644 Documentation/misc-devices/ch341.rst
Documentation/misc-devices/ch341.rst new file +109 -0
new file mode 100644
index 000000000000..65ba293bdc2d
@@ -0,0 +1,109 @@
+.. 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 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 756be15a49a4..e85531a4f354 100644
@@ -19,6 +19,7 @@ fit into other categories.
    bh1770glc
    eeprom
    c2port
+   ch341
    dw-xdata-pcie
    ibmvmc
    ics932s401
Signature · 1 line
2.32.0
#17 lore raw
In reply to frank zago
Quoted 6 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.

Changes from v5:
Addressed reviewers' comments.

Please be more specific in your changelogs. This essentially just says "changed stuff".

Quoted 2 lines

Better handling of 0-bytes i2c commands
Use of better USB API.

What does this even mean?

Johan

#18 lore raw
In reply to frank zago

Hi Frank,

Are you going to further proceed with this patch set? As far as I can see, there were no updates since 2022.

16.06.2022 04:37, frank zago пишет:

Quoted 61 lines

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 v5:
Addressed reviewers' comments.
Better handling of 0-bytes i2c commands
Use of better USB API.

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

During testing I found that i2c handles hot removal, but not gpio. The gpio subsystem will complain with 'REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED', but it's a gpiolib issue.

Changes from v1:
- Removed double Signed-off-by
- Move Kconfig into the same directory as the driver

frank zago (4):
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
docs: misc: add documentation for ch341 driver

  Documentation/misc-devices/ch341.rst | 109 ++++++++
  Documentation/misc-devices/index.rst |   1 +
  MAINTAINERS                          |   9 +
  drivers/gpio/Kconfig                 |  10 +
  drivers/gpio/Makefile                |   1 +
  drivers/gpio/gpio-ch341.c            | 385 +++++++++++++++++++++++++++
  drivers/i2c/busses/Kconfig           |  10 +
  drivers/i2c/busses/Makefile          |   1 +
  drivers/i2c/busses/i2c-ch341.c       | 377 ++++++++++++++++++++++++++
  drivers/mfd/Kconfig                  |  10 +
  drivers/mfd/Makefile                 |   1 +
  drivers/mfd/ch341-core.c             |  90 +++++++
  include/linux/mfd/ch341.h            |  26 ++
  13 files changed, 1030 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

#19 lore raw
Quoted 4 lines · Matwey V. Kornilov

Hi Frank,

Are you going to further proceed with this patch set? As far as I can see, there were no updates since 2022.

I suggest you take it over if there is no reply in a week or so.

IIRC Howard Harte also has this very device inside the USR8200 router, but I don't know if it uses the I2C/GPIO portions in that device.

Yours,
Linus Walleij

author
#20 lore raw
In reply to Matwey V. Kornilov
Quoted 6 lines · Matwey V. Kornilov

Hi Frank,

Are you going to further proceed with this patch set? As far as I can see, there were no updates since 2022.

Hi Matwey,

I've been maintaining it at https://github.com/frank-zago/ch341-i2c-spi-gpio, but I have had no desire to try again to upstream it. Feel free to do it if you'd like.

Also there's been an SPI only driver that was upstreamed a couple revisions ago, which is incompatible with this driver.

Regards,
Frank.

#21 lore raw

сб, 9 нояб. 2024 г. в 02:15, Frank Zago <[email protected]>:

Quoted 10 lines · Matwey V. Kornilov
Quoted 6 lines

Hi Frank,

Are you going to further proceed with this patch set? As far as I can see, there were no updates since 2022.

Hi Matwey,

Hi Frank,

Quoted 2 lines

I've been maintaining it at https://github.com/frank-zago/ch341-i2c-spi-gpio, but I have had no desire to try again to upstream it.

Thank you for the response and for the code.

Quoted 1 line

Feel free to do it if you'd like.

Just to make it clear for other people who will find this discussion while looking for a CH341 I2C driver: currently I don't have motivation to make this kind of contribution.

Quoted 7 lines

Also there's been an SPI only driver that was upstreamed a couple revisions ago, which is incompatible with this driver.

Regards,
Frank.

Signature · 2 lines
With best regards,
Matwey V. Kornilov