WCH CH341 GPIO and SPI support
Subsystems (5)
v4 cover WCH CH341 GPIO and SPI support
The CH341 is a multifunction chip, presenting 3 different USB PID. One of these functions is for I2C/SPI/GPIO. This new set of drivers will manage I2C and GPIO.
Changes from 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 (3):
mfd: ch341: add core driver for the WCH CH341 in I2C/GPIO mode
gpio: ch341: add MFD cell driver for the CH341
i2c: ch341: add MFD cell driver CH341 for I2C
Documentation/misc-devices/ch341.rst | 114 ++++++++ Documentation/misc-devices/index.rst | 1 + MAINTAINERS | 9 + drivers/gpio/Kconfig | 10 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-ch341.c | 421 +++++++++++++++++++++++++++ drivers/i2c/busses/Kconfig | 10 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-ch341.c | 325 +++++++++++++++++++++ drivers/mfd/Kconfig | 12 + drivers/mfd/Makefile | 1 + drivers/mfd/ch341-core.c | 109 +++++++ include/linux/mfd/ch341.h | 25 ++ 13 files changed, 1039 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
PATCH v4 1/3 mfd: ch341: add core 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.
--- Documentation/misc-devices/ch341.rst | 114 +++++++++++++++++++++++++++ Documentation/misc-devices/index.rst | 1 + MAINTAINERS | 7 ++ drivers/mfd/Kconfig | 12 +++ drivers/mfd/Makefile | 1 + drivers/mfd/ch341-core.c | 103 ++++++++++++++++++++++++ include/linux/mfd/ch341.h | 25 ++++++ 7 files changed, 263 insertions(+) create mode 100644 Documentation/misc-devices/ch341.rst create mode 100644 drivers/mfd/ch341-core.c create mode 100644 include/linux/mfd/ch341.h
Documentation/misc-devices/ch341.rst new file +114 -0
@@ -0,0 +1,114 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +=========================================================== +WinChipHead (沁恒) CH341 linux driver for I2C and GPIO mode +=========================================================== + +The CH341 is declined in several flavors, and may support one or more +of UART, SPI, I2C and GPIO, but not always simultaneously: + + - CH341 A/B/F: UART, Printer, SPI, I2C and GPIO + - CH341 C/T: UART and I2C + - CH341 H: SPI + +They work in 3 different modes, with only one being presented +depending on the USB PID: + + - 0x5523: UART mode, covered by the USB `ch341` serial driver + - 0x5512: SPI/I2C/GPIO mode, covered by the MFD `ch341-mfd` driver + - 0x5584: Parallel printer mode, covered by the USB `usblp` driver + +Mode selection is done at the hardware level by tying some +pins. Breakout boards with one of the CH341 chip usually have one or +more jumpers to select which mode they work on. At least one model +(CJMCU-341) appears to need bridging some solder pads to select a +different default. Breakout boards also don't usually offer an option +to configure the chip into printer mode; for that case, connect the +SCL and SDA lines directly together. + +The various CH341 appear to be indistinguishable from the +software. For instance the ch341 MFD driver will present a GPIO +interface for the CH341T although physical pins are not present, and +the device will accept GPIO commands. + +The ch341 MFD driver has been tested with a CH341A, CH341B and +CH341T. + +Some breakout boards work in 3.3V and 5V depending on some jumpers. + +The black chip programmer with a ZIF socket will power the CH341 at +3.3V if a jumper is set, but will only output 5V to the chips to be +programmed, which is not always desirable. A hardware hack to use 3.3V +everywhere, involving some soldering, is available at +https://eevblog.com/forum/repair/ch341a-serial-memory-programmer-power-supply-fix/ + +Some sample code for the CH341 is available at the manufacturer +website, at http://wch-ic.com/products/CH341.html + +The repository at https://github.com/boseji/CH341-Store contains a lot +of information on these chips, including datasheets. + +This driver is based on the pre-existing work at +https://github.com/gschorcht/i2c-ch341-usb + + +I2C Caveats +----------- + +The ch341 doesn't work with a Wii nunchuk, possibly because the +pull-up value is too low (1500 ohms). + +i2c AT24 eeproms can be read but not programmed properly because the +at24 linux driver tries to write a byte at a time, and doesn't wait at +all (or enough) between writes. Data corruption on writes does occur. + +The driver doesn't support detection of I2C device present on the +bus. Apparently when a device is not present at a given address, the +CH341 will return an extra byte of data, but the driver doesn't +support that. This may be addressed in the future. + + +The GPIOs +--------- + +16 GPIOs are available on the CH341 A/B/F. The first 6 are input/output, +and the last 10 are input only. + +Pinout and their names as they appear on some breakout boards:: + + CH341A/B/F GPIO Names Mode + pin line + + 15 0 D0, CS0 input/output + 16 1 D1, CS1 input/output + 17 2 D2, CS2 input/output + 18 3 D3, SCK, DCK input/output + 19 4 D4, DOUT2, CS3 input/output + 20 5 D5, MOSI, DOUT, SDO input/output + 21 6 D6, DIN2 input + 22 7 D7, MISO, DIN input + 5 8 ERR input + 6 9 PEMP input + 7 10 INT input + 8 11 SLCT (SELECT) input + 26 12 RST# (?) input + 27 13 WT (WAIT) input + 4 14 DS (Data Select?) input + 3 15 AS (Address Select?) input + + +GPIO interrupt +~~~~~~~~~~~~~~ + +The INT pin, corresponding to GPIO 10 is an input pin that can trigger +an interrupt on a rising edge. Only that pin is able to generate an +interrupt, and only on a rising edge. Trying to monitor events on +another GPIO, or that GPIO on something other than a rising edge, will +be rejected. + + +SPI +--- + +This driver doesn't offer an SPI interface (yet) due to the +impossibility of declaring an SPI device like I2C does.
Documentation/misc-devices/index.rst +1 -0
@@ -29,3 +29,4 @@ fit into other categories. spear-pcie-gadget uacce xilinx_sdfec + ch341
MAINTAINERS +7 -0
@@ -20845,6 +20845,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 +12 -0
@@ -1778,6 +1778,18 @@ 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. + + The chip's SPI mode is not supported. + + 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
@@ -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 +103 -0
103 lines not on this page · raw message
include/linux/mfd/ch341.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Definitions for CH341 MFD driver +*/ + +#include <linux/usb.h> +#include <linux/mutex.h> + +#define DEFAULT_TIMEOUT 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 ch341_device { +struct usb_device *usb_dev; +struct usb_interface *iface; +struct mutex usb_lock; + +int ep_in; +int ep_out; +int ep_intr; +u8 ep_intr_interval; +};
Signature · 1 line
2.32.0
Re: [PATCH v4 1/3] mfd: ch341: add core driver for the WCH CH341 in I2C/GPIO mode
Quoted 4 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.
...
Quoted 1 line
+The driver doesn't support detection of I2C device present on the
devices
Quoted 3 lines
Documentation/misc-devices/ch341.rst new file +3
-0+bus. Apparently when a device is not present at a given address, the +CH341 will return an extra byte of data, but the driver doesn't +support that. This may be addressed in the future.
...
Quoted 4 lines
spear-pcie-gadget
uacce
xilinx_sdfec
+ ch341
Seems you broke the order.
...
drivers/mfd/Kconfig config MFD_LOCHNAGAR · line 1782
Quoted 3 lines
drivers/mfd/Kconfig +2
-0+config MFD_CH341 + tristate "WinChipHead CH341 in I2C/SPI/GPIO mode"
(1)
drivers/mfd/Kconfig config MFD_LOCHNAGAR · line 1786
Quoted 4 lines
drivers/mfd/Kconfig +4
-0+ 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.
"chips running" (no comma needed)
Quoted 1 line
+ The chip's SPI mode is not supported.
Maybe no need to include SPI in the (1) along with dropping this line?
Quoted 2 lines
drivers/mfd/Kconfig +2
-0+ This driver can also be built as a module. If so, the + module will be called ch341-core.
...
drivers/mfd/ch341-core.c line 16
Quoted 3 lines
drivers/mfd/ch341-core.c new file +3
-0+#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h>
(2)
drivers/mfd/ch341-core.c line 19
Quoted 4 lines
drivers/mfd/ch341-core.c new file +3
-0+#include <linux/mfd/core.h> + +#include <linux/mfd/ch341.h>
Moving these two to (2) ?
...
Quoted 1 line
+ dev =
kzalloc(sizeof(*dev), GFP_KERNEL);
devm_kzalloc() ?
Quoted 2 lines
drivers/mfd/ch341-core.c new file +2
-0+if (!dev) +return -ENOMEM ;
...
drivers/mfd/ch341-core.c line 63
Quoted 7 lines
drivers/mfd/ch341-core.c new file +5
-0+ rc =mfd_add_hotplug_devices (&iface->dev, ch341_devs, +ARRAY_SIZE (ch341_devs)); +if (rc) { +dev_err (&iface->dev,"Failed to add mfd devices to core." ); + goto free_dev;
return dev_err_probe(...); ?
Quoted 1 line
+ }
...
Quoted 1 line
+
usb_set_intfdata(dev->iface, NULL);
This has been done by device driver core for the past 10+ years.
...
drivers/mfd/ch341-core.c line 89
Quoted 6 lines
drivers/mfd/ch341-core.c new file +5
-0+static const struct usb_device_id ch341_usb_table[] = { + {USB_DEVICE (0x1a86 ,0x5512 ) }, + { } +}; +
Redundant blank line.
drivers/mfd/ch341-core.c line 96
Quoted 8 lines
drivers/mfd/ch341-core.c new file +7
-0+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
Keep a comma to avoid unneeded churn in the future.
drivers/mfd/ch341-core.c line 23
Quoted 3 lines
drivers/mfd/ch341-core.c new file +2
-0+}; +
Redundant blank line.
include/linux/mfd/ch341.h line 4
Quoted 6 lines
include/linux/mfd/ch341.h new file +4
-0+module_usb_driver (ch341_usb_driver); +/* +* Definitions for CH341 MFD driver +*/
One line?
...
Quoted 1 line
+#include <
linux/usb.h>
No users of this header. Use forward declarations.
Quoted 1 line
+#include <
linux/mutex.h>
Missed types.h.
...
Quoted 1 line
+#define
DEFAULT_TIMEOUT1000 /* 1s USB requests timeout */
Use proper suffix, i.e. _MS.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v4 2/3 gpio: ch341: add MFD cell driver for the CH341
In reply to frank zagoThe 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 | 421 ++++++++++++++++++++++++++++++++++++++ drivers/mfd/ch341-core.c | 3 + 5 files changed, 436 insertions(+) create mode 100644 drivers/gpio/gpio-ch341.c
MAINTAINERS +1 -0
@@ -20849,6 +20849,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
@@ -1634,6 +1634,16 @@ endmenu menu "USB GPIO expanders" depends on USB +config GPIO_CH341 + tristate "CH341 USB adapter in GPIO/I2C/SPI mode" + depends on 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
@@ -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 +421 -0
421 lines not on this page · raw message
drivers/mfd/ch341-core.c +3 -0
@@ -19,6 +19,9 @@#include <linux/mfd/ch341.h> static const struct mfd_cell ch341_devs[] = { + { + .name ="ch341-gpio" , + }, }; static int ch341_usb_probe(struct usb_interface *iface,
Signature · 1 line
2.32.0
Re: [PATCH v4 2/3] gpio: ch341: add MFD cell driver for the CH341
Quoted 3 lines · frank zago
The GPIO interface offers 16 GPIOs. 6 are read/write, and 10 are read-only.
We use terminology of output-only and input-only. Is it what you are telling us? If it's something else, you have to elaborate much better on what's going on with these GPIO lines.
...
drivers/gpio/Kconfig endmenu · line 1638
Quoted 2 lines
drivers/gpio/Kconfig +2
-0+config GPIO_CH341 + tristate "CH341 USB adapter in GPIO/I2C/SPI mode"
How is this driver related to either SPI or I²C modes?
Quoted 1 line
+ depends on
MFD_CH341
Can't be compile tested?
Quoted 6 lines
drivers/gpio/Kconfig +6
-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.
...
Quoted 1 line
+/* Notes.
Keep the proper (not network) style for multi-line comments.
drivers/gpio/gpio-ch341.c line 24
Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +6
-0+#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/mfd/core.h> +#include <linux/gpio.h> +#include <linux/mfd/ch341.h>
If I got your intention with groups of headers, I would see rather
...ordered headers...
blank line
#include <linux/gpio.h>
But more importantly that gpio.h is the wrong header and must be replaced with the appropriate one from the include/gpio/ folder.
Also you have missed headers, like types.h.
...
drivers/gpio/gpio-ch341.c line 33
Quoted 6 lines
drivers/gpio/gpio-ch341.c new file +6
-0+#define CH341_PARA_CMD_STS 0xA0/* Get pins status */ +#define CH341_CMD_UIO_STREAM 0xAB/* UIO stream command */ + +#define CH341_CMD_UIO_STM_OUT 0x80/* UIO interface OUT command (D0~D5) */ +#define CH341_CMD_UIO_STM_DIR 0x40/* UIO interface DIR command (D0~D5) */ +#define CH341_CMD_UIO_STM_END 0x20/* UIO interface END command */
What does UIO mean here? If it is Userspace I/O in terms of Linux kernel, it's no-go we want this. Otherwise needs to be explained somewhere.
...
Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7
-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 */ +u8 gpio_buf[SEG_SIZE ];
drivers/gpio/gpio-ch341.c line 53
Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +10
-0+ +struct { +char name[32 ]; +bool enabled; +struct irq_chip irq; +int num; +struct urb *urb; +struct usb_anchor urb_out; +u8 buf[CH341_USB_MAX_INTR_SIZE ]; + } gpio_irq;
We have a specific GPIO IRQ chip structure, what is the purpose of semi-duplication of it?
Quoted 3 lines
drivers/gpio/gpio-ch341.c new file +3
-0+ +struct ch341_device *ch341; +};
...
drivers/gpio/gpio-ch341.c line 72
Quoted 7 lines
drivers/gpio/gpio-ch341.c new file +7
-0+static void ch341_gpio_dbg_show (struct seq_file *s,struct gpio_chip *chip) +{ +struct ch341_gpio *dev =gpiochip_get_data (chip); + +seq_printf (s,"pin config : %04x (0=IN, 1=OUT)\n" , dev->gpio_dir); +seq_printf (s,"last read : %04x\n" , dev->gpio_last_read); +seq_printf (s,"last written: %04x\n" , dev->gpio_last_written);
Multi-line debug output is quite non-standard among GPIO drivers.
Quoted 11 lines
drivers/gpio/gpio-ch341.c new file +10
-0+} +{ +struct ch341_device *ch341 = dev->ch341; +int actual; +int rc; + +mutex_lock (&ch341->usb_lock); + + rc = usb_bulk_msg(ch341->usb_dev, + usb_sndbulkpipe(ch341->usb_dev, ch341->ep_out),
drivers/gpio/gpio-ch341.c line 94
Quoted 10 lines
drivers/gpio/gpio-ch341.c new file +9
-0+ dev->gpio_buf, out_len, + &actual, DEFAULT_TIMEOUT); +if (rc <0 ) + goto done; + +if (in_len ==0 ) { + rc = actual; + goto done; + }
You may do it better. See below.
drivers/gpio/gpio-ch341.c line 103
Quoted 8 lines
drivers/gpio/gpio-ch341.c new file +7
-0+ rc =usb_bulk_msg (ch341->usb_dev, +usb_rcvbulkpipe (ch341->usb_dev, ch341->ep_in), + dev->gpio_buf,SEG_SIZE , &actual,DEFAULT_TIMEOUT ); + +if (rc ==0 ) + rc = actual; +done:
out_unlock: sounds better.
drivers/gpio/gpio-ch341.c line 106
Quoted 3 lines
drivers/gpio/gpio-ch341.c new file +2
-0+mutex_unlock (&ch341->usb_lock); +return rc;
if (rc < 0)
return rc;
return actual;
Quoted 1 line
+}
...
Quoted 1 line
+ int result;
rc / result / etc... Please, become consistent in naming the return code variable.
...
drivers/gpio/gpio-ch341.c line 127
Quoted 2 lines
drivers/gpio/gpio-ch341.c new file +2
-0+if (result ==6 ) + dev->gpio_last_read =le16_to_cpu (*(__le16 *)dev->gpio_buf);
So, it means you have the wrong type of gpio_but. Also you missed the pointer versions of leXX_to_cpu() helpers.
...
Quoted 1 line
+ return (result != 6) ? result : 0;
Besides redundant parentheses, this can be optimized. I will leave it for your homework (the hint is given at the top part of the review).
...
Quoted 1 line
+ return (
dev->gpio_last_read& BIT(offset)) ? 1 : 0;
!! can be used. But it's up to you and maintainers, the compiler will do its job anyway.
...
drivers/gpio/gpio-ch341.c line 193
Quoted 2 lines
drivers/gpio/gpio-ch341.c new file +2
-0+ dev->gpio_last_written &= ~*mask; + dev->gpio_last_written |= (*bits & *mask);
Can be done in one line as it's a well established pattern in Linux kernel for drivers.
...
Quoted 1 line
+ return (
dev->gpio_dir& BIT(offset)) ? 0 : 1;
! will do the job.
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!(pin_can_output & mask)) + return -EINVAL;
I don't remember if we have a valid mask for this case.
...
Quoted 1 line
+ if (!urb->status) {
Will be much better to simply do:
if (urb_status) {
...
return;
}
Quoted 3 lines
drivers/gpio/gpio-ch341.c new file +3
-0+ }else { +usb_unanchor_urb (dev->gpio_irq.urb); + }
...
drivers/gpio/gpio-ch341.c line 269
Quoted 2 lines
drivers/gpio/gpio-ch341.c new file +2
-0+if (data->irq != dev->gpio_irq.num || type !=IRQ_TYPE_EDGE_RISING ) +return -EINVAL ;
Usually we lock the handler type here while in ->probe() we assign a bad handler by default in order to filter out spurious interrupts.
...
Quoted 1 line
+
dev->gpio_irq.enabled= true;
What is the purpose of this flag? Note there is a patch to add a specific flag to the descriptor to do exactly this.
...
drivers/gpio/gpio-ch341.c line 310
Quoted 9 lines
drivers/gpio/gpio-ch341.c new file +9
-0+/* Convert the GPIO index to the IRQ number */ +static int ch341_gpio_to_irq (struct gpio_chip *chip,unsigned int offset) +{ +struct ch341_gpio *dev =gpiochip_get_data (chip); + +if (offset !=CH341_GPIO_INT_LINE ) +return -ENXIO ; + +return dev->gpio_irq.num;
In the new code we will have the special field that limits the GPIO IRQ lines (can be different to the ngpio).
Quoted 1 line
+}
...
drivers/gpio/gpio-ch341.c line 340
Quoted 4 lines
drivers/gpio/gpio-ch341.c new file +3
-0+snprintf (dev->gpio_irq.name,sizeof (dev->gpio_irq.name), +"ch341-%s-gpio" ,dev_name (&ch341->usb_dev->dev)); + dev->gpio_irq.name[sizeof (dev->gpio_irq.name) -1 ] =0 ;
This is redundant. Have you read the manual page on snprintf()?
...
drivers/gpio/gpio-ch341.c line 372
Quoted 5 lines
drivers/gpio/gpio-ch341.c new file +4
-0+ rc =devm_irq_alloc_desc (&pdev->dev,0 ); +if (rc <0 ) { +dev_err (&pdev->dev,"Cannot allocate an IRQ desc" ); +return rc;
return dev_err_probe();
drivers/gpio/gpio-ch341.c line 380
Quoted 8 lines
drivers/gpio/gpio-ch341.c new file +8
-0+ } + + dev->gpio_irq.num = rc; + dev->gpio_irq.enabled = false; + +irq_set_chip_data (dev->gpio_irq.num, dev); +irq_set_chip_and_handler (dev->gpio_irq.num, &dev->gpio_irq.irq, + handle_simple_irq);
Oh là là. Can you use the latest and greatest approach of instantiating the GPIO IRQ chip?
...
drivers/gpio/gpio-ch341.c line 386
Quoted 2 lines
drivers/gpio/gpio-ch341.c new file +2
-0+dev_err (&pdev->dev,"Cannot allocate the int URB" ); +return -ENOMEM ;
return dev_err_probe();
...
Quoted 1 line
+ rc =
gpiochip_add_data(gpio, dev);
Why not devm?
drivers/gpio/gpio-ch341.c line 399
Quoted 3 lines
drivers/gpio/gpio-ch341.c new file +3
-0+if (rc) { +dev_err (&pdev->dev,"Could not add GPIO\n" ); + goto release_urb;
return dev_err_probe();
Quoted 1 line
+ }
...
drivers/gpio/gpio-ch341.c line 415
Quoted 6 lines
drivers/gpio/gpio-ch341.c new file +6
-0+static struct platform_driver ch341_gpio_driver = { + .driver.name ="ch341-gpio" , + .probe = ch341_gpio_probe, + .remove = ch341_gpio_remove, +}; +
Redundant blank line.
Quoted 1 line
+module_platform_driver(ch341_gpio_driver);
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 2/3] gpio: ch341: add MFD cell driver for the CH341
In reply to frank zagoHi Frank,
thanks for your patch!
I see you already got a bunch of homework from Andy, I will do a more thorough review on the next iteration, just a few things:
Quoted 4 lines · frank zago
The GPIO interface offers 16 GPIOs. 6 are read/write, and 10 are read-only.
Signed-off-by frank zago <[email protected]>
(...)
drivers/gpio/Kconfig endmenu · line 1639
Quoted 3 lines
drivers/gpio/Kconfig +3
-0+config GPIO_CH341 + tristate "CH341 USB adapter in GPIO/I2C/SPI mode" + depends on MFD_CH341
I would add
default MFD_CD341
This way it gets selected automatically if the MFD module gets selected. (I suspect you should do the same with the I2C module).
drivers/gpio/gpio-ch341.c line 22
Quoted 5 lines
drivers/gpio/gpio-ch341.c new file +5
-0+#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/mfd/core.h> +#include <linux/gpio.h>
Use <linux/gpio/driver.h>
Yours,
Linus Walleij
PATCH v4 3/3 i2c: ch341: add MFD cell driver CH341 for I2C
In reply to frank zagoThe I2C interface can run at 4 different speeds. This driver currently only offer 100MHz. Tested with a variety of I2C sensors, and the IIO subsystem.
--- MAINTAINERS | 1 + drivers/i2c/busses/Kconfig | 10 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-ch341.c | 325 +++++++++++++++++++++++++++++++++ drivers/mfd/ch341-core.c | 3 + 5 files changed, 340 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ch341.c
MAINTAINERS +1 -0
@@ -20850,6 +20850,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
@@ -1178,6 +1178,16 @@ config I2C_RCAR comment "External I2C/SMBus adapter drivers" +config I2C_CH341 + tristate "CH341 USB adapter in GPIO/I2C/SPI mode" + depends on 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
@@ -123,6 +123,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 +325 -0
325 lines not on this page · raw message
drivers/mfd/ch341-core.c +3 -0
@@ -22,6 +22,9 @@ static const struct mfd_cell ch341_devs[] = { { .name = "ch341-gpio", }, + { + .name = "ch341-i2c", + }, }; static int ch341_usb_probe(struct usb_interface *iface,
Signature · 1 line
2.32.0