2022-06-16 01:57:40

by Frank Zago

[permalink] [raw]
Subject: [PATCH v6 0/4] 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


2022-06-16 01:57:40

by Frank Zago

[permalink] [raw]
Subject: [PATCH v6 4/4] docs: misc: add documentation for ch341 driver

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

Signed-off-by: frank zago <[email protected]>
---
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

diff --git a/Documentation/misc-devices/ch341.rst b/Documentation/misc-devices/ch341.rst
new file mode 100644
index 000000000000..65ba293bdc2d
--- /dev/null
+++ b/Documentation/misc-devices/ch341.rst
@@ -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.
diff --git a/Documentation/misc-devices/index.rst b/Documentation/misc-devices/index.rst
index 756be15a49a4..e85531a4f354 100644
--- a/Documentation/misc-devices/index.rst
+++ b/Documentation/misc-devices/index.rst
@@ -19,6 +19,7 @@ fit into other categories.
bh1770glc
eeprom
c2port
+ ch341
dw-xdata-pcie
ibmvmc
ics932s401
--
2.32.0

2022-06-16 02:02:14

by Frank Zago

[permalink] [raw]
Subject: [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.

Signed-off-by: frank zago <[email protected]>
---
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

diff --git a/MAINTAINERS b/MAINTAINERS
index 43d3d07afccd..628eeaa9bf68 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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]
diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 3b59456f5545..893acc821a42 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -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
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index 858cacf659d6..fd615ab3929f 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -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
diff --git a/drivers/mfd/ch341-core.c b/drivers/mfd/ch341-core.c
new file mode 100644
index 000000000000..f08a67dd6074
--- /dev/null
+++ b/drivers/mfd/ch341-core.c
@@ -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");
diff --git a/include/linux/mfd/ch341.h b/include/linux/mfd/ch341.h
new file mode 100644
index 000000000000..44f5da0720bd
--- /dev/null
+++ b/include/linux/mfd/ch341.h
@@ -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;
+};
--
2.32.0

2022-06-20 10:10:45

by Johan Hovold

[permalink] [raw]
Subject: Re: [PATCH v6 0/4] add driver for the WCH CH341 in I2C/GPIO mode

On Wed, Jun 15, 2022 at 08:37:43PM -0500, frank zago wrote:
> 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".

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

What does this even mean?

Johan

2022-06-27 14:31:28

by Lee Jones

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

USB review please.

> 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.
>
> Signed-off-by: frank zago <[email protected]>
> ---
> 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
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 43d3d07afccd..628eeaa9bf68 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -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]
> diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
> index 3b59456f5545..893acc821a42 100644
> --- a/drivers/mfd/Kconfig
> +++ b/drivers/mfd/Kconfig
> @@ -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
> diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
> index 858cacf659d6..fd615ab3929f 100644
> --- a/drivers/mfd/Makefile
> +++ b/drivers/mfd/Makefile
> @@ -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
> diff --git a/drivers/mfd/ch341-core.c b/drivers/mfd/ch341-core.c
> new file mode 100644
> index 000000000000..f08a67dd6074
> --- /dev/null
> +++ b/drivers/mfd/ch341-core.c
> @@ -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");
> diff --git a/include/linux/mfd/ch341.h b/include/linux/mfd/ch341.h
> new file mode 100644
> index 000000000000..44f5da0720bd
> --- /dev/null
> +++ b/include/linux/mfd/ch341.h
> @@ -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;
> +};

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

2022-06-27 14:43:27

by Greg KH

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

On Mon, Jun 27, 2022 at 03:04:32PM +0100, Lee Jones wrote:
> USB review please.
>
> > 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.
> >
> > Signed-off-by: frank zago <[email protected]>
> > ---
> > 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
> >
> > diff --git a/MAINTAINERS b/MAINTAINERS
> > index 43d3d07afccd..628eeaa9bf68 100644
> > --- a/MAINTAINERS
> > +++ b/MAINTAINERS
> > @@ -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]
> > diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
> > index 3b59456f5545..893acc821a42 100644
> > --- a/drivers/mfd/Kconfig
> > +++ b/drivers/mfd/Kconfig
> > @@ -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
> > diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
> > index 858cacf659d6..fd615ab3929f 100644
> > --- a/drivers/mfd/Makefile
> > +++ b/drivers/mfd/Makefile
> > @@ -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
> > diff --git a/drivers/mfd/ch341-core.c b/drivers/mfd/ch341-core.c
> > new file mode 100644
> > index 000000000000..f08a67dd6074
> > --- /dev/null
> > +++ b/drivers/mfd/ch341-core.c
> > @@ -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");
> > diff --git a/include/linux/mfd/ch341.h b/include/linux/mfd/ch341.h
> > new file mode 100644
> > index 000000000000..44f5da0720bd
> > --- /dev/null
> > +++ b/include/linux/mfd/ch341.h
> > @@ -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;
> > +};


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

Acked-by: Greg Kroah-Hartman <[email protected]>

2022-06-27 15:06:10

by Lee Jones

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

On Mon, 27 Jun 2022, Greg Kroah-Hartman wrote:

> On Mon, Jun 27, 2022 at 03:04:32PM +0100, Lee Jones wrote:
> > USB review please.
> >
> > > 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.
> > >
> > > Signed-off-by: frank zago <[email protected]>
> > > ---
> > > 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
> > >
> > > diff --git a/MAINTAINERS b/MAINTAINERS
> > > index 43d3d07afccd..628eeaa9bf68 100644
> > > --- a/MAINTAINERS
> > > +++ b/MAINTAINERS
> > > @@ -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]
> > > diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
> > > index 3b59456f5545..893acc821a42 100644
> > > --- a/drivers/mfd/Kconfig
> > > +++ b/drivers/mfd/Kconfig
> > > @@ -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
> > > diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
> > > index 858cacf659d6..fd615ab3929f 100644
> > > --- a/drivers/mfd/Makefile
> > > +++ b/drivers/mfd/Makefile
> > > @@ -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
> > > diff --git a/drivers/mfd/ch341-core.c b/drivers/mfd/ch341-core.c
> > > new file mode 100644
> > > index 000000000000..f08a67dd6074
> > > --- /dev/null
> > > +++ b/drivers/mfd/ch341-core.c
> > > @@ -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");
> > > diff --git a/include/linux/mfd/ch341.h b/include/linux/mfd/ch341.h
> > > new file mode 100644
> > > index 000000000000..44f5da0720bd
> > > --- /dev/null
> > > +++ b/include/linux/mfd/ch341.h
> > > @@ -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;
> > > +};
>
>
> 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?

> Acked-by: Greg Kroah-Hartman <[email protected]>

Thanks.

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

2022-06-28 00:11:07

by Frank Zago

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

On 6/27/22 09:43, Lee Jones wrote:
> On Mon, 27 Jun 2022, Greg Kroah-Hartman wrote:
>
>> On Mon, Jun 27, 2022 at 03:04:32PM +0100, Lee Jones wrote:
>>> USB review please.
>>>
>>>> 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.
>>>>
>>>> Signed-off-by: frank zago <[email protected]>
>>>> ---
>>>> 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
>>>>
>>>> diff --git a/MAINTAINERS b/MAINTAINERS
>>>> index 43d3d07afccd..628eeaa9bf68 100644
>>>> --- a/MAINTAINERS
>>>> +++ b/MAINTAINERS
>>>> @@ -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]
>>>> diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
>>>> index 3b59456f5545..893acc821a42 100644
>>>> --- a/drivers/mfd/Kconfig
>>>> +++ b/drivers/mfd/Kconfig
>>>> @@ -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
>>>> diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
>>>> index 858cacf659d6..fd615ab3929f 100644
>>>> --- a/drivers/mfd/Makefile
>>>> +++ b/drivers/mfd/Makefile
>>>> @@ -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
>>>> diff --git a/drivers/mfd/ch341-core.c b/drivers/mfd/ch341-core.c
>>>> new file mode 100644
>>>> index 000000000000..f08a67dd6074
>>>> --- /dev/null
>>>> +++ b/drivers/mfd/ch341-core.c
>>>> @@ -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");
>>>> diff --git a/include/linux/mfd/ch341.h b/include/linux/mfd/ch341.h
>>>> new file mode 100644
>>>> index 000000000000..44f5da0720bd
>>>> --- /dev/null
>>>> +++ b/include/linux/mfd/ch341.h
>>>> @@ -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;
>>>> +};
>>
>>
>> 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?

Yes, that's correct.

Frank

2022-07-04 11:13:18

by Lee Jones

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

On Wed, 15 Jun 2022, frank zago wrote:

> 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.
>
> Signed-off-by: frank zago <[email protected]>
> ---
> 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
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 43d3d07afccd..628eeaa9bf68 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -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]
> diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
> index 3b59456f5545..893acc821a42 100644
> --- a/drivers/mfd/Kconfig
> +++ b/drivers/mfd/Kconfig
> @@ -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
> diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
> index 858cacf659d6..fd615ab3929f 100644
> --- a/drivers/mfd/Makefile
> +++ b/drivers/mfd/Makefile
> @@ -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
> diff --git a/drivers/mfd/ch341-core.c b/drivers/mfd/ch341-core.c
> new file mode 100644
> index 000000000000..f08a67dd6074
> --- /dev/null
> +++ b/drivers/mfd/ch341-core.c
> @@ -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",
> + },
> +};

These should both be on one line each.

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

Why not use the devm_* version?

> +}
> +
> +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");
> diff --git a/include/linux/mfd/ch341.h b/include/linux/mfd/ch341.h
> new file mode 100644
> index 000000000000..44f5da0720bd
> --- /dev/null
> +++ b/include/linux/mfd/ch341.h
> @@ -0,0 +1,18 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/* Definitions for the CH341 driver */

What definitions?

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

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