diff mbox series

board: st: move type-c stusb1600 controller code in a driver

Message ID 20200629103404.1.Ic3786ee11f56ca7e32d47d816a3cc0db2e87d6b2@changeid
State New
Delegated to: Patrice Chotard
Headers show
Series board: st: move type-c stusb1600 controller code in a driver | expand

Commit Message

Patrick Delaunay June 29, 2020, 8:34 a.m. UTC
Migrate the ST Microelectronics STUSB160X Type-C controller code in
a generic I2C driver in st/common, based on Linux one in :
drivers/usb/typec/stusb160x.c

This patch simplifies the stm32mp1 board code and allows to reuse
this STUSB160X driver in other boards.

Signed-off-by: Patrick Delaunay <patrick.delaunay@st.com>
---

 board/st/common/Kconfig             |  7 ++++
 board/st/common/Makefile            |  2 +
 board/st/common/stusb160x.c         | 46 +++++++++++++++++++++++
 board/st/common/stusb160x.h         | 10 +++++
 board/st/stm32mp1/stm32mp1.c        | 58 +++--------------------------
 configs/stm32mp15_basic_defconfig   |  1 +
 configs/stm32mp15_trusted_defconfig |  1 +
 7 files changed, 73 insertions(+), 52 deletions(-)
 create mode 100644 board/st/common/stusb160x.c
 create mode 100644 board/st/common/stusb160x.h

Comments

Patrice CHOTARD July 2, 2020, 7:07 a.m. UTC | #1
Hi Patrick

On 6/29/20 10:34 AM, Patrick Delaunay wrote:
> Migrate the ST Microelectronics STUSB160X Type-C controller code in
> a generic I2C driver in st/common, based on Linux one in :
> drivers/usb/typec/stusb160x.c
>
> This patch simplifies the stm32mp1 board code and allows to reuse
> this STUSB160X driver in other boards.
>
> Signed-off-by: Patrick Delaunay <patrick.delaunay@st.com>
> ---
>
>  board/st/common/Kconfig             |  7 ++++
>  board/st/common/Makefile            |  2 +
>  board/st/common/stusb160x.c         | 46 +++++++++++++++++++++++
>  board/st/common/stusb160x.h         | 10 +++++
>  board/st/stm32mp1/stm32mp1.c        | 58 +++--------------------------
>  configs/stm32mp15_basic_defconfig   |  1 +
>  configs/stm32mp15_trusted_defconfig |  1 +
>  7 files changed, 73 insertions(+), 52 deletions(-)
>  create mode 100644 board/st/common/stusb160x.c
>  create mode 100644 board/st/common/stusb160x.h
>
> diff --git a/board/st/common/Kconfig b/board/st/common/Kconfig
> index 015ba40939..5280d0f9ed 100644
> --- a/board/st/common/Kconfig
> +++ b/board/st/common/Kconfig
> @@ -69,3 +69,10 @@ config DFU_ALT_RAM0
>  	depends on ARCH_STM32MP && SET_DFU_ALT_INFO
>  	help
>  	  This defines the partitions of ram used to build dfu dynamically.
> +
> +config TYPEC_STUSB160X
> +	tristate "STMicroelectronics STUSB160X Type-C controller driver"
> +	depends on DM_I2C
> +	help
> +	  Say Y if your system has STMicroelectronics STUSB160X Type-C port
> +	  controller.
> diff --git a/board/st/common/Makefile b/board/st/common/Makefile
> index aa030bacd8..1a5280e139 100644
> --- a/board/st/common/Makefile
> +++ b/board/st/common/Makefile
> @@ -9,3 +9,5 @@ ifeq ($(CONFIG_ARCH_STM32MP),y)
>  obj-$(CONFIG_SYS_MTDPARTS_RUNTIME) += stm32mp_mtdparts.o
>  obj-$(CONFIG_SET_DFU_ALT_INFO) += stm32mp_dfu.o
>  endif
> +
> +obj-$(CONFIG_TYPEC_STUSB160X) += stusb160x.o
> diff --git a/board/st/common/stusb160x.c b/board/st/common/stusb160x.c
> new file mode 100644
> index 0000000000..f1197f9faa
> --- /dev/null
> +++ b/board/st/common/stusb160x.c
> @@ -0,0 +1,46 @@
> +// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
> +/*
> + * STMicroelectronics STUSB Type-C controller driver
> + * based on Linux drivers/usb/typec/stusb160x.c
> + *
> + * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <i2c.h>
> +
> +/* REGISTER */
> +#define STUSB160X_CC_CONNECTION_STATUS		0x0E
> +
> +/* STUSB160X_CC_CONNECTION_STATUS bitfields */
> +#define STUSB160X_CC_ATTACH			BIT(0)
> +
> +int stusb160x_cable_connected(void)
> +{
> +	struct udevice *dev;
> +	int ret;
> +
> +	ret = uclass_get_device_by_driver(UCLASS_I2C_GENERIC,
> +					  DM_GET_DRIVER(stusb160x),
> +					  &dev);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = dm_i2c_reg_read(dev, STUSB160X_CC_CONNECTION_STATUS);
> +	if (ret < 0)
> +		return 0;
> +
> +	return ret & STUSB160X_CC_ATTACH;
> +}
> +
> +static const struct udevice_id stusb160x_ids[] = {
> +	{ .compatible = "st,stusb1600" },
> +	{}
> +};
> +
> +U_BOOT_DRIVER(stusb160x) = {
> +	.name = "stusb160x",
> +	.id = UCLASS_I2C_GENERIC,
> +	.of_match = stusb160x_ids,
> +};
> diff --git a/board/st/common/stusb160x.h b/board/st/common/stusb160x.h
> new file mode 100644
> index 0000000000..fe39840b41
> --- /dev/null
> +++ b/board/st/common/stusb160x.h
> @@ -0,0 +1,10 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Copyright (C) 2020, STMicroelectronics
> + */
> +
> +#ifdef CONFIG_TYPEC_STUSB160X
> +int stusb160x_cable_connected(void);
> +#else
> +int stusb160x_cable_connected(void) { return -ENODEV; }
> +#endif
> diff --git a/board/st/stm32mp1/stm32mp1.c b/board/st/stm32mp1/stm32mp1.c
> index 4553329b25..a257375807 100644
> --- a/board/st/stm32mp1/stm32mp1.c
> +++ b/board/st/stm32mp1/stm32mp1.c
> @@ -41,6 +41,8 @@
>  #include <power/regulator.h>
>  #include <usb/dwc2_udc.h>
>  
> +#include "../../st/common/stusb160x.h"
> +
>  /* SYSCFG registers */
>  #define SYSCFG_BOOTR		0x00
>  #define SYSCFG_PMCSETR		0x04
> @@ -175,64 +177,16 @@ static void board_key_check(void)
>  }
>  
>  #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
> -
> -/* STMicroelectronics STUSB1600 Type-C controller */
> -#define STUSB1600_CC_CONNECTION_STATUS		0x0E
> -
> -/* STUSB1600_CC_CONNECTION_STATUS bitfields */
> -#define STUSB1600_CC_ATTACH			BIT(0)
> -
> -static int stusb1600_init(struct udevice **dev_stusb1600)
> -{
> -	ofnode node;
> -	struct udevice *dev, *bus;
> -	int ret;
> -	u32 chip_addr;
> -
> -	*dev_stusb1600 = NULL;
> -
> -	/* if node stusb1600 is present, means DK1 or DK2 board */
> -	node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
> -	if (!ofnode_valid(node))
> -		return -ENODEV;
> -
> -	ret = ofnode_read_u32(node, "reg", &chip_addr);
> -	if (ret)
> -		return -EINVAL;
> -
> -	ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
> -					  &bus);
> -	if (ret) {
> -		printf("bus for stusb1600 not found\n");
> -		return -ENODEV;
> -	}
> -
> -	ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
> -	if (!ret)
> -		*dev_stusb1600 = dev;
> -
> -	return ret;
> -}
> -
> -static int stusb1600_cable_connected(struct udevice *dev)
> -{
> -	u8 status;
> -
> -	if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
> -		return 0;
> -
> -	return status & STUSB1600_CC_ATTACH;
> -}
> -
>  #include <usb/dwc2_udc.h>
>  int g_dnl_board_usb_cable_connected(void)
>  {
> -	struct udevice *stusb1600;
>  	struct udevice *dwc2_udc_otg;
>  	int ret;
>  
> -	if (!stusb1600_init(&stusb1600))
> -		return stusb1600_cable_connected(stusb1600);
> +	/* if typec stusb160x is present, means DK1 or DK2 board */
> +	ret = stusb160x_cable_connected();
> +	if (ret >= 0)
> +		return ret;
>  
>  	ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
>  					  DM_GET_DRIVER(dwc2_udc_otg),
> diff --git a/configs/stm32mp15_basic_defconfig b/configs/stm32mp15_basic_defconfig
> index c7dd2926c9..7d41606a98 100644
> --- a/configs/stm32mp15_basic_defconfig
> +++ b/configs/stm32mp15_basic_defconfig
> @@ -8,6 +8,7 @@ CONFIG_SPL_MMC_SUPPORT=y
>  CONFIG_SPL=y
>  CONFIG_TARGET_ST_STM32MP15x=y
>  CONFIG_CMD_STM32PROG=y
> +CONFIG_TYPEC_STUSB160X=y
>  CONFIG_ENV_OFFSET_REDUND=0x2C0000
>  CONFIG_SPL_SPI_FLASH_SUPPORT=y
>  CONFIG_SPL_SPI_SUPPORT=y
> diff --git a/configs/stm32mp15_trusted_defconfig b/configs/stm32mp15_trusted_defconfig
> index ca4a10813b..decdc2c1de 100644
> --- a/configs/stm32mp15_trusted_defconfig
> +++ b/configs/stm32mp15_trusted_defconfig
> @@ -6,6 +6,7 @@ CONFIG_ENV_OFFSET=0x280000
>  CONFIG_ENV_SECT_SIZE=0x40000
>  CONFIG_TARGET_ST_STM32MP15x=y
>  CONFIG_CMD_STM32PROG=y
> +CONFIG_TYPEC_STUSB160X=y
>  CONFIG_ENV_OFFSET_REDUND=0x2C0000
>  CONFIG_DISTRO_DEFAULTS=y
>  CONFIG_FIT=y

Reviewed-by: Patrice Chotard <patrice.chotard@st.com>

Thanks
diff mbox series

Patch

diff --git a/board/st/common/Kconfig b/board/st/common/Kconfig
index 015ba40939..5280d0f9ed 100644
--- a/board/st/common/Kconfig
+++ b/board/st/common/Kconfig
@@ -69,3 +69,10 @@  config DFU_ALT_RAM0
 	depends on ARCH_STM32MP && SET_DFU_ALT_INFO
 	help
 	  This defines the partitions of ram used to build dfu dynamically.
+
+config TYPEC_STUSB160X
+	tristate "STMicroelectronics STUSB160X Type-C controller driver"
+	depends on DM_I2C
+	help
+	  Say Y if your system has STMicroelectronics STUSB160X Type-C port
+	  controller.
diff --git a/board/st/common/Makefile b/board/st/common/Makefile
index aa030bacd8..1a5280e139 100644
--- a/board/st/common/Makefile
+++ b/board/st/common/Makefile
@@ -9,3 +9,5 @@  ifeq ($(CONFIG_ARCH_STM32MP),y)
 obj-$(CONFIG_SYS_MTDPARTS_RUNTIME) += stm32mp_mtdparts.o
 obj-$(CONFIG_SET_DFU_ALT_INFO) += stm32mp_dfu.o
 endif
+
+obj-$(CONFIG_TYPEC_STUSB160X) += stusb160x.o
diff --git a/board/st/common/stusb160x.c b/board/st/common/stusb160x.c
new file mode 100644
index 0000000000..f1197f9faa
--- /dev/null
+++ b/board/st/common/stusb160x.c
@@ -0,0 +1,46 @@ 
+// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
+/*
+ * STMicroelectronics STUSB Type-C controller driver
+ * based on Linux drivers/usb/typec/stusb160x.c
+ *
+ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <i2c.h>
+
+/* REGISTER */
+#define STUSB160X_CC_CONNECTION_STATUS		0x0E
+
+/* STUSB160X_CC_CONNECTION_STATUS bitfields */
+#define STUSB160X_CC_ATTACH			BIT(0)
+
+int stusb160x_cable_connected(void)
+{
+	struct udevice *dev;
+	int ret;
+
+	ret = uclass_get_device_by_driver(UCLASS_I2C_GENERIC,
+					  DM_GET_DRIVER(stusb160x),
+					  &dev);
+	if (ret < 0)
+		return ret;
+
+	ret = dm_i2c_reg_read(dev, STUSB160X_CC_CONNECTION_STATUS);
+	if (ret < 0)
+		return 0;
+
+	return ret & STUSB160X_CC_ATTACH;
+}
+
+static const struct udevice_id stusb160x_ids[] = {
+	{ .compatible = "st,stusb1600" },
+	{}
+};
+
+U_BOOT_DRIVER(stusb160x) = {
+	.name = "stusb160x",
+	.id = UCLASS_I2C_GENERIC,
+	.of_match = stusb160x_ids,
+};
diff --git a/board/st/common/stusb160x.h b/board/st/common/stusb160x.h
new file mode 100644
index 0000000000..fe39840b41
--- /dev/null
+++ b/board/st/common/stusb160x.h
@@ -0,0 +1,10 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020, STMicroelectronics
+ */
+
+#ifdef CONFIG_TYPEC_STUSB160X
+int stusb160x_cable_connected(void);
+#else
+int stusb160x_cable_connected(void) { return -ENODEV; }
+#endif
diff --git a/board/st/stm32mp1/stm32mp1.c b/board/st/stm32mp1/stm32mp1.c
index 4553329b25..a257375807 100644
--- a/board/st/stm32mp1/stm32mp1.c
+++ b/board/st/stm32mp1/stm32mp1.c
@@ -41,6 +41,8 @@ 
 #include <power/regulator.h>
 #include <usb/dwc2_udc.h>
 
+#include "../../st/common/stusb160x.h"
+
 /* SYSCFG registers */
 #define SYSCFG_BOOTR		0x00
 #define SYSCFG_PMCSETR		0x04
@@ -175,64 +177,16 @@  static void board_key_check(void)
 }
 
 #if defined(CONFIG_USB_GADGET) && defined(CONFIG_USB_GADGET_DWC2_OTG)
-
-/* STMicroelectronics STUSB1600 Type-C controller */
-#define STUSB1600_CC_CONNECTION_STATUS		0x0E
-
-/* STUSB1600_CC_CONNECTION_STATUS bitfields */
-#define STUSB1600_CC_ATTACH			BIT(0)
-
-static int stusb1600_init(struct udevice **dev_stusb1600)
-{
-	ofnode node;
-	struct udevice *dev, *bus;
-	int ret;
-	u32 chip_addr;
-
-	*dev_stusb1600 = NULL;
-
-	/* if node stusb1600 is present, means DK1 or DK2 board */
-	node = ofnode_by_compatible(ofnode_null(), "st,stusb1600");
-	if (!ofnode_valid(node))
-		return -ENODEV;
-
-	ret = ofnode_read_u32(node, "reg", &chip_addr);
-	if (ret)
-		return -EINVAL;
-
-	ret = uclass_get_device_by_ofnode(UCLASS_I2C, ofnode_get_parent(node),
-					  &bus);
-	if (ret) {
-		printf("bus for stusb1600 not found\n");
-		return -ENODEV;
-	}
-
-	ret = dm_i2c_probe(bus, chip_addr, 0, &dev);
-	if (!ret)
-		*dev_stusb1600 = dev;
-
-	return ret;
-}
-
-static int stusb1600_cable_connected(struct udevice *dev)
-{
-	u8 status;
-
-	if (dm_i2c_read(dev, STUSB1600_CC_CONNECTION_STATUS, &status, 1))
-		return 0;
-
-	return status & STUSB1600_CC_ATTACH;
-}
-
 #include <usb/dwc2_udc.h>
 int g_dnl_board_usb_cable_connected(void)
 {
-	struct udevice *stusb1600;
 	struct udevice *dwc2_udc_otg;
 	int ret;
 
-	if (!stusb1600_init(&stusb1600))
-		return stusb1600_cable_connected(stusb1600);
+	/* if typec stusb160x is present, means DK1 or DK2 board */
+	ret = stusb160x_cable_connected();
+	if (ret >= 0)
+		return ret;
 
 	ret = uclass_get_device_by_driver(UCLASS_USB_GADGET_GENERIC,
 					  DM_GET_DRIVER(dwc2_udc_otg),
diff --git a/configs/stm32mp15_basic_defconfig b/configs/stm32mp15_basic_defconfig
index c7dd2926c9..7d41606a98 100644
--- a/configs/stm32mp15_basic_defconfig
+++ b/configs/stm32mp15_basic_defconfig
@@ -8,6 +8,7 @@  CONFIG_SPL_MMC_SUPPORT=y
 CONFIG_SPL=y
 CONFIG_TARGET_ST_STM32MP15x=y
 CONFIG_CMD_STM32PROG=y
+CONFIG_TYPEC_STUSB160X=y
 CONFIG_ENV_OFFSET_REDUND=0x2C0000
 CONFIG_SPL_SPI_FLASH_SUPPORT=y
 CONFIG_SPL_SPI_SUPPORT=y
diff --git a/configs/stm32mp15_trusted_defconfig b/configs/stm32mp15_trusted_defconfig
index ca4a10813b..decdc2c1de 100644
--- a/configs/stm32mp15_trusted_defconfig
+++ b/configs/stm32mp15_trusted_defconfig
@@ -6,6 +6,7 @@  CONFIG_ENV_OFFSET=0x280000
 CONFIG_ENV_SECT_SIZE=0x40000
 CONFIG_TARGET_ST_STM32MP15x=y
 CONFIG_CMD_STM32PROG=y
+CONFIG_TYPEC_STUSB160X=y
 CONFIG_ENV_OFFSET_REDUND=0x2C0000
 CONFIG_DISTRO_DEFAULTS=y
 CONFIG_FIT=y