2023-05-09 08:08:28

by Mike Looijmans

[permalink] [raw]
Subject: [PATCH 1/2] dt-bindings: usb: misc: Add microchip USB5807 HUB driver

The USB5807 is a 7-port USB 3.1 hub that can be configured by I2C.
This driver resets the chip, optionally allows D+/D- lines to be
swapped in the devicetree config, and then sends an ATTACH command to
put the device in operational mode.

Signed-off-by: Mike Looijmans <[email protected]>

---

.../devicetree/bindings/usb/usb5807.yaml | 51 +++++++++++++++++++
1 file changed, 51 insertions(+)
create mode 100644 Documentation/devicetree/bindings/usb/usb5807.yaml

diff --git a/Documentation/devicetree/bindings/usb/usb5807.yaml b/Documentation/devicetree/bindings/usb/usb5807.yaml
new file mode 100644
index 000000000000..06b94210c281
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/usb5807.yaml
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/usb/usb5807.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Microchip USB 3.1 SuperSpeed Hub Controller
+
+maintainers:
+ - Mike Looijmans <[email protected]>
+
+properties:
+ compatible:
+ enum:
+ - microchip,usb5807
+
+ reg:
+ maxItems: 1
+
+ reset-gpios:
+ description: |
+ Should specify the gpio for hub reset
+
+ swap-dx-lanes:
+ $ref: /schemas/types.yaml#/definitions/uint8-array
+ description: |
+ Specifies the ports which will swap the differential-pair (D+/D-),
+ default is not-swapped.
+
+additionalProperties: false
+
+required:
+ - compatible
+ - reg
+
+examples:
+ - |
+ #include <dt-bindings/gpio/gpio.h>
+
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ usb-hub@2d {
+ compatible = "microchip,usb5807";
+ reg = <0x2d>;
+ reset-gpios = <&gpio1 4 GPIO_ACTIVE_LOW>;
+ /* Swapped D+/D- on port 0 */
+ swap-dx-lanes = <0>;
+ };
+ };
--
2.17.1


Met vriendelijke groet / kind regards,

Mike Looijmans
System Expert


TOPIC Embedded Products B.V.
Materiaalweg 4, 5681 RJ Best
The Netherlands

T: +31 (0) 499 33 69 69
E: [email protected]
W: http://www.topic.nl

Please consider the environment before printing this e-mail


2023-05-09 08:10:22

by Mike Looijmans

[permalink] [raw]
Subject: [PATCH 2/2] usb: misc: usb5807: Add driver

The USB5807 is a 7-port USB 3.1 hub that can be configured by I2C.
This drivers resets the chip, optionally allows D+/D- lines to be
swapped in the devicetree config, and then sends an ATTACH command to
put the device in operational mode.

Signed-off-by: Mike Looijmans <[email protected]>

---

drivers/usb/misc/Kconfig | 9 ++
drivers/usb/misc/Makefile | 1 +
drivers/usb/misc/usb5807.c | 175 +++++++++++++++++++++++++++++++++++++
3 files changed, 185 insertions(+)
create mode 100644 drivers/usb/misc/usb5807.c

diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
index 99b15b77dfd5..6659e917ea26 100644
--- a/drivers/usb/misc/Kconfig
+++ b/drivers/usb/misc/Kconfig
@@ -251,6 +251,15 @@ config USB_EZUSB_FX2
Say Y here if you need EZUSB device support.
(Cypress FX/FX2/FX2LP microcontrollers)

+config USB_HUB_USB5807
+ tristate "USB5807 Hub Controller Configuration Driver"
+ depends on I2C
+ help
+ This option enables support for configuration via SMBus of the
+ Microchip USB5807 USB 3.1 Hub Controller. Configuration parameters may
+ be set in devicetree.
+ Say Y or M here if you need to configure such a device via SMBus.
+
config USB_HUB_USB251XB
tristate "USB251XB Hub Controller Configuration Driver"
depends on I2C
diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
index 1992cc284d8a..e827ed251fa5 100644
--- a/drivers/usb/misc/Makefile
+++ b/drivers/usb/misc/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_USB_USS720) += uss720.o
obj-$(CONFIG_USB_SEVSEG) += usbsevseg.o
obj-$(CONFIG_USB_YUREX) += yurex.o
obj-$(CONFIG_USB_HUB_USB251XB) += usb251xb.o
+obj-$(CONFIG_USB_HUB_USB5807) += usb5807.o
obj-$(CONFIG_USB_HSIC_USB3503) += usb3503.o
obj-$(CONFIG_USB_HSIC_USB4604) += usb4604.o
obj-$(CONFIG_USB_CHAOSKEY) += chaoskey.o
diff --git a/drivers/usb/misc/usb5807.c b/drivers/usb/misc/usb5807.c
new file mode 100644
index 000000000000..04080ca81d4d
--- /dev/null
+++ b/drivers/usb/misc/usb5807.c
@@ -0,0 +1,175 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Driver for Microchip USB5807 USB 3.1 Hub
+ * Configuration via SMBus.
+ *
+ * Copyright (c) 2023 Topic Embedded Products
+ */
+
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+
+#define USB5807_CMD_ATTACH 0xAA55
+#define USB5807_CMD_CONFIG 0x9937
+
+#define USB5807_REG_LANE_SWAP 0x30FA
+
+#define USB5807_NUM_PORTS 7
+
+
+static int usb5807_write(struct i2c_client *i2c, void *buf, u8 len)
+{
+ int ret;
+ struct i2c_msg msg = {
+ .addr = i2c->addr,
+ .flags = 0x0,
+ .len = len,
+ .buf = buf,
+ };
+
+ ret = i2c_transfer(i2c->adapter, &msg, 1);
+ return ret < 0 ? ret : 0;
+}
+
+/*
+ * Send a command sequence, which is an I2C write transaction, with the command
+ * word in big endian and a terminating "0" byte.
+ */
+static int usb5807_command(struct i2c_client *i2c, u16 cmd)
+{
+ u8 buf[3] = {cmd >> 8, cmd & 0xff, 0};
+
+ return usb5807_write(i2c, buf, sizeof(buf));
+}
+
+static int usb5807_prepare_reg_u8(struct i2c_client *i2c, u16 reg, u8 value)
+{
+ u8 buf[] = {
+ 0x00,
+ 0x00, /* Memory offset */
+ 1 + 4, /* Transaction size */
+ 0x00, /* 0 = Register write operation */
+ 1, /* Size of register data */
+ (reg >> 8) & 0xff,
+ reg & 0xff, /* Register offset */
+ value, /* Register data */
+ 0 /* Terminating zero */
+ };
+
+ return usb5807_write(i2c, buf, sizeof(buf));
+}
+
+/*
+ * Write an 8-bit register. First we must write the "set register" operation to
+ * the chip's internal memory at offset 0, then issue a command to execute said
+ * operation.
+ */
+static int usb5807_write_reg_u8(struct i2c_client *i2c, u16 reg, u8 value)
+{
+ int ret;
+
+ ret = usb5807_prepare_reg_u8(i2c, reg, value);
+ if (ret)
+ return ret;
+
+ return usb5807_command(i2c, USB5807_CMD_CONFIG);
+}
+
+/* Decode array of port numbers property into bit mask */
+static u8 usb5807_get_ports_field(struct device *dev, const char *prop_name)
+{
+ struct property *prop;
+ const __be32 *p;
+ u32 port;
+ u8 result = 0;
+
+ of_property_for_each_u32(dev->of_node, prop_name, prop, p, port) {
+ if (port < USB5807_NUM_PORTS)
+ result |= BIT(port);
+ else
+ dev_warn(dev, "%s: port %u doesn't exist\n", prop_name,
+ port);
+ }
+ return result;
+}
+
+static int usb5807_i2c_probe(struct i2c_client *i2c,
+ const struct i2c_device_id *id)
+{
+ struct device_node *np = i2c->dev.of_node;
+ struct gpio_desc *reset_gpio;
+ int ret;
+ u8 val;
+
+ /* Reset the chip to bring it into configuration mode */
+ reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset",
+ GPIOD_OUT_HIGH);
+ if (IS_ERR(reset_gpio)) {
+ return dev_err_probe(&i2c->dev, PTR_ERR(reset_gpio),
+ "Failed to request reset GPIO\n");
+ }
+ /* Reset timing: Assert for >= 5 us */
+ usleep_range(5, 10);
+
+ /* Lock the bus for >= 1ms while the hub reads the I2C strapping */
+ i2c_lock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
+
+ gpiod_set_value_cansleep(reset_gpio, 0);
+ usleep_range(1000, 2000);
+
+ i2c_unlock_bus(i2c->adapter, I2C_LOCK_SEGMENT);
+
+ /* The hub device needs additional time to boot up */
+ msleep(20);
+
+ val = usb5807_get_ports_field(&i2c->dev, "swap-dx-lanes");
+ if (val) {
+ ret = usb5807_write_reg_u8(i2c, USB5807_REG_LANE_SWAP, val);
+ if (ret < 0)
+ dev_err(&i2c->dev, "Failed writing config: %d\n", ret);
+ }
+
+ /*
+ * Send the "Attach" command which makes the device disappear from the
+ * I2C bus and starts USB enumeration.
+ */
+ ret = usb5807_command(i2c, USB5807_CMD_ATTACH);
+ if (ret) {
+ dev_err(&i2c->dev, "Failed sending ATTACH command: %d\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static const struct of_device_id usb5807_of_match[] = {
+ { .compatible = "microchip,usb5807" },
+ { } /* sentinel */
+};
+MODULE_DEVICE_TABLE(of, usb5807_of_match);
+
+static const struct i2c_device_id usb5807_id[] = {
+ { "usb5807", },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(i2c, usb5807_id);
+
+static struct i2c_driver usb5807_i2c_driver = {
+ .driver = {
+ .name = "usb5807",
+ .of_match_table = of_match_ptr(usb5807_of_match),
+ },
+ .probe = usb5807_i2c_probe,
+ .id_table = usb5807_id,
+};
+
+module_i2c_driver(usb5807_i2c_driver);
+
+MODULE_AUTHOR("Mike Looijmans <[email protected]>");
+MODULE_DESCRIPTION("USB5807 USB 3.1 Hub Controller Driver");
+MODULE_LICENSE("GPL");
--
2.17.1


Met vriendelijke groet / kind regards,

Mike Looijmans
System Expert


TOPIC Embedded Products B.V.
Materiaalweg 4, 5681 RJ Best
The Netherlands

T: +31 (0) 499 33 69 69
E: [email protected]
W: http://www.topic.nl

Please consider the environment before printing this e-mail

2023-05-09 08:10:58

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH 1/2] dt-bindings: usb: misc: Add microchip USB5807 HUB driver

On 09/05/2023 09:46, Mike Looijmans wrote:
> The USB5807 is a 7-port USB 3.1 hub that can be configured by I2C.
> This driver resets the chip, optionally allows D+/D- lines to be
> swapped in the devicetree config, and then sends an ATTACH command to
> put the device in operational mode.
>
> Signed-off-by: Mike Looijmans <[email protected]>
>

Subject: drop misc prefix.
Subject: drop "driver", you are not adding driver here.

> ---
>
> .../devicetree/bindings/usb/usb5807.yaml | 51 +++++++++++++++++++
> 1 file changed, 51 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/usb/usb5807.yaml
>
> diff --git a/Documentation/devicetree/bindings/usb/usb5807.yaml b/Documentation/devicetree/bindings/usb/usb5807.yaml
> new file mode 100644
> index 000000000000..06b94210c281
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/usb/usb5807.yaml

Filename like compatible.

> @@ -0,0 +1,51 @@
> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/usb/usb5807.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Microchip USB 3.1 SuperSpeed Hub Controller
> +
> +maintainers:
> + - Mike Looijmans <[email protected]>
> +
> +properties:
> + compatible:
> + enum:
> + - microchip,usb5807
> +
> + reg:
> + maxItems: 1
> +
> + reset-gpios:
> + description: |
> + Should specify the gpio for hub reset

Drop description, obvious. maxItems instead. If you decide to add
description, then keep it useful, e.g. mention active low.


> +
> + swap-dx-lanes:
> + $ref: /schemas/types.yaml#/definitions/uint8-array
> + description: |

Do not need '|' unless you need to preserve formatting.

> + Specifies the ports which will swap the differential-pair (D+/D-),
> + default is not-swapped.

maxItems (and minItems if it differs)

No supply? How does the hub gets power? I see at least two supplies in
datasheet.

> +
> +additionalProperties: false
> +
> +required:
> + - compatible
> + - reg

Best regards,
Krzysztof

2023-05-10 15:10:23

by Mike Looijmans

[permalink] [raw]
Subject: Re: [PATCH 1/2] dt-bindings: usb: misc: Add microchip USB5807 HUB driver

I'll fix the issues and send a v2. I'll add supply/regulators as well.
Will take a few days though.

Thanks for the quick feedback, much appreciated.
--


Met vriendelijke groet / kind regards,

Mike Looijmans
System Expert


TOPIC Embedded Products B.V.
Materiaalweg 4, 5681 RJ Best
The Netherlands

T: +31 (0) 499 33 69 69
E: [email protected]
W: http://www.topic.nl

Please consider the environment before printing this e-mail
On 09-05-2023 10:02, Krzysztof Kozlowski wrote:
> On 09/05/2023 09:46, Mike Looijmans wrote:
>> The USB5807 is a 7-port USB 3.1 hub that can be configured by I2C.
>> This driver resets the chip, optionally allows D+/D- lines to be
>> swapped in the devicetree config, and then sends an ATTACH command to
>> put the device in operational mode.
>>
>> Signed-off-by: Mike Looijmans <[email protected]>
>>
> Subject: drop misc prefix.
> Subject: drop "driver", you are not adding driver here.
>
>> ---
>>
>> .../devicetree/bindings/usb/usb5807.yaml | 51 +++++++++++++++++++
>> 1 file changed, 51 insertions(+)
>> create mode 100644 Documentation/devicetree/bindings/usb/usb5807.yaml
>>
>> diff --git a/Documentation/devicetree/bindings/usb/usb5807.yaml b/Documentation/devicetree/bindings/usb/usb5807.yaml
>> new file mode 100644
>> index 000000000000..06b94210c281
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/usb/usb5807.yaml
> Filename like compatible.
>
>> @@ -0,0 +1,51 @@
>> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
>> +%YAML 1.2
>> +---
>> +$id: http://devicetree.org/schemas/usb/usb5807.yaml#
>> +$schema: http://devicetree.org/meta-schemas/core.yaml#
>> +
>> +title: Microchip USB 3.1 SuperSpeed Hub Controller
>> +
>> +maintainers:
>> + - Mike Looijmans <[email protected]>
>> +
>> +properties:
>> + compatible:
>> + enum:
>> + - microchip,usb5807
>> +
>> + reg:
>> + maxItems: 1
>> +
>> + reset-gpios:
>> + description: |
>> + Should specify the gpio for hub reset
> Drop description, obvious. maxItems instead. If you decide to add
> description, then keep it useful, e.g. mention active low.
>
>
>> +
>> + swap-dx-lanes:
>> + $ref: /schemas/types.yaml#/definitions/uint8-array
>> + description: |
> Do not need '|' unless you need to preserve formatting.
>
>> + Specifies the ports which will swap the differential-pair (D+/D-),
>> + default is not-swapped.
> maxItems (and minItems if it differs)
>
> No supply? How does the hub gets power? I see at least two supplies in
> datasheet.
>
>> +
>> +additionalProperties: false
>> +
>> +required:
>> + - compatible
>> + - reg
> Best regards,
> Krzysztof
>

--
Mike Looijmans