--- /dev/null
+// 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,
+};
#include <power/regulator.h>
#include <usb/dwc2_udc.h>
+#include "../../st/common/stusb160x.h"
+
/* SYSCFG registers */
#define SYSCFG_BOOTR 0x00
#define SYSCFG_PMCSETR 0x04
}
#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),