Commit d36e42c9 authored by Andreas Sandberg's avatar Andreas Sandberg Committed by Martí Bolívar
Browse files

usbc: Use a DT alias to identify USB-C port



Samples sometimes need to identify a default USB-C port. Instead of
hard-coding a node label, use the usbc-port0 alias to point to the
correct node.

Signed-off-by: default avatarAndreas Sandberg <andreas@sandberg.uk>
parent 154023f7
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -8,6 +8,10 @@
#include <dt-bindings/usb-c/pd.h>

/ {
	aliases {
		usbc-port0 = &port1;
	};

	/* usbc.rst vbus-voltage-divider-adc start */
	vbus1: vbus {
		compatible = "zephyr,usb-c-vbus-adc";
+4 −0
Original line number Diff line number Diff line
@@ -8,6 +8,10 @@
#include <dt-bindings/usb-c/pd.h>

/ {
	aliases {
		usbc-port0 = &port1;
	};

	vbus1: vbus {
		compatible = "zephyr,usb-c-vbus-adc";
		io-channels = <&adc1 9>;
+7 −7
Original line number Diff line number Diff line
@@ -13,10 +13,10 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);

#define PORT1_NODE DT_NODELABEL(port1)
#define PORT1_POWER_ROLE		DT_ENUM_IDX(DT_NODELABEL(port1), power_role)
#define USBC_PORT0_NODE		DT_ALIAS(usbc_port0)
#define USBC_PORT0_POWER_ROLE	DT_ENUM_IDX(USBC_PORT0_NODE, power_role)

#if (PORT1_POWER_ROLE != TC_ROLE_SINK)
#if (USBC_PORT0_POWER_ROLE != TC_ROLE_SINK)
#error "Unsupported board: Only Sink device supported"
#endif

@@ -28,7 +28,7 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
 */
static struct port1_data_t {
	/** Sink Capabilities */
	uint32_t snk_caps[DT_PROP_LEN(DT_NODELABEL(port1), sink_pdos)];
	uint32_t snk_caps[DT_PROP_LEN(USBC_PORT0_NODE, sink_pdos)];
	/** Number of Sink Capabilities */
	int snk_cap_cnt;
	/** Source Capabilities */
@@ -38,8 +38,8 @@ static struct port1_data_t {
	/* Power Supply Ready flag */
	atomic_t ps_ready;
} port1_data = {
	.snk_caps = {DT_FOREACH_PROP_ELEM(DT_NODELABEL(port1), sink_pdos, SINK_PDO)},
	.snk_cap_cnt = DT_PROP_LEN(DT_NODELABEL(port1), sink_pdos),
	.snk_caps = {DT_FOREACH_PROP_ELEM(USBC_PORT0_NODE, sink_pdos, SINK_PDO)},
	.snk_cap_cnt = DT_PROP_LEN(USBC_PORT0_NODE, sink_pdos),
	.src_caps = {0},
	.src_cap_cnt = 0,
	.ps_ready = 0
@@ -298,7 +298,7 @@ int main(void)
	const struct device *usbc_port1;

	/* Get the device for this port */
	usbc_port1 = DEVICE_DT_GET(PORT1_NODE);
	usbc_port1 = DEVICE_DT_GET(USBC_PORT0_NODE);
	if (!device_is_ready(usbc_port1)) {
		LOG_ERR("PORT1 device not ready");
		return 0;
+3 −0
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@
#include <dt-bindings/usb-c/pd.h>

/ {
	aliases {
		usbc-port0 = &port1;
	};

	pwmleds {
		compatible = "pwm-leds";
+8 −8
Original line number Diff line number Diff line
@@ -15,11 +15,11 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);

#include "power_ctrl.h"

#define PORT1_NODE		DT_NODELABEL(port1)
#define PORT1_POWER_ROLE	DT_ENUM_IDX(DT_NODELABEL(port1), power_role)
#define USBC_PORT0_NODE		DT_ALIAS(usbc_port0)
#define USBC_PORT0_POWER_ROLE	DT_ENUM_IDX(USBC_PORT0_NODE, power_role)

/* A Source power role evauates to 1. See usbc_tc.h: TC_ROLE_SOURCE */
#if (PORT1_POWER_ROLE != 1)
#if (USBC_PORT0_POWER_ROLE != 1)
#error "Unsupported board: Only Source device supported"
#endif

@@ -31,7 +31,7 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
 */
static struct port1_data_t {
	/** Source Capabilities */
	uint32_t src_caps[DT_PROP_LEN(DT_NODELABEL(port1), source_pdos)];
	uint32_t src_caps[DT_PROP_LEN(USBC_PORT0_NODE, source_pdos)];
	/** Number of Source Capabilities */
	int src_cap_cnt;
	/** CC Rp value */
@@ -49,9 +49,9 @@ static struct port1_data_t {
	/** Log Sink Requested RDO to console */
	atomic_t show_sink_request;
} port1_data = {
	.rp = DT_ENUM_IDX(DT_NODELABEL(port1), typec_power_opmode),
	.src_caps = {DT_FOREACH_PROP_ELEM(DT_NODELABEL(port1), source_pdos, SOURCE_PDO)},
	.src_cap_cnt = DT_PROP_LEN(DT_NODELABEL(port1), source_pdos),
	.rp = DT_ENUM_IDX(USBC_PORT0_NODE, typec_power_opmode),
	.src_caps = {DT_FOREACH_PROP_ELEM(USBC_PORT0_NODE, source_pdos, SOURCE_PDO)},
	.src_cap_cnt = DT_PROP_LEN(USBC_PORT0_NODE, source_pdos),
};

/* usbc.rst port data object end */
@@ -318,7 +318,7 @@ int main(void)
	const struct device *usbc_port1;

	/* Get the device for this port */
	usbc_port1 = DEVICE_DT_GET(PORT1_NODE);
	usbc_port1 = DEVICE_DT_GET(USBC_PORT0_NODE);
	if (!device_is_ready(usbc_port1)) {
		LOG_ERR("PORT1 device not ready");
		return 0;