Skip to content

Commit 6bd26a0

Browse files
barnas-michalcarlescufi
authored andcommitted
usbc: add shell commands for PPC manipulation
Add support for ppc shell command with subcommands that can print the status of PPC (dead battery, sinking, sourcing, vbus detected), dump registers and request the PPC to exit dead battery mode. Signed-off-by: Michał Barnaś <[email protected]>
1 parent 85219ed commit 6bd26a0

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

drivers/usb_c/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
add_subdirectory_ifdef(CONFIG_USBC_TCPC_DRIVER tcpc)
44
add_subdirectory_ifdef(CONFIG_USBC_VBUS_DRIVER vbus)
5+
add_subdirectory_ifdef(CONFIG_USBC_PPC_DRIVER ppc)

drivers/usb_c/ppc/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()
4+
5+
zephyr_library_sources_ifdef(CONFIG_USBC_PPC_SHELL shell.c)

drivers/usb_c/ppc/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ config USBC_PPC_INIT_PRIORITY
1616
help
1717
Initialization priority of the USB-C PPC drivers in POST_KERNEL.
1818

19+
config USBC_PPC_SHELL
20+
bool "Shell commands for PPC"
21+
help
22+
Add useful shell commands to manipulate and debug the PPCs
23+
1924
module = USBC_PPC
2025
module-str = usbc-ppc
2126
source "subsys/logging/Kconfig.template.log_config"

drivers/usb_c/ppc/shell.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/device.h>
7+
#include <zephyr/shell/shell.h>
8+
#include <zephyr/drivers/usb_c/usbc_ppc.h>
9+
10+
/** Macro used to iterate over USB-C connector and call a function if the node has PPC property */
11+
#define CALL_IF_HAS_PPC(usb_node, func) \
12+
COND_CODE_1(DT_NODE_HAS_PROP(usb_node, ppc), \
13+
(ret |= func(DEVICE_DT_GET(DT_PHANDLE_BY_IDX(usb_node, ppc, 0)));), ())
14+
15+
/**
16+
* @brief Command that dumps registers of one or all of the PPCs
17+
*
18+
* @param sh Shell structure
19+
* @param argc Arguments count
20+
* @param argv Device name
21+
* @return int ORed return values of all the functions executed, 0 in case of success
22+
*/
23+
static int cmd_ppc_dump(const struct shell *sh, size_t argc, char **argv)
24+
{
25+
int ret = 0;
26+
27+
if (argc <= 1) {
28+
DT_FOREACH_STATUS_OKAY_VARGS(usb_c_connector, CALL_IF_HAS_PPC, ppc_dump_regs);
29+
} else {
30+
const struct device *dev = device_get_binding(argv[1]);
31+
32+
ret = ppc_dump_regs(dev);
33+
}
34+
35+
return ret;
36+
}
37+
38+
/**
39+
* @brief Function used to pretty print status of the PPC
40+
*
41+
* @param dev Pointer to the PPC device structure
42+
*/
43+
static int print_status(const struct device *dev)
44+
{
45+
printk("PPC %s:\n", dev->name);
46+
printk(" Dead battery: %d\n", ppc_is_dead_battery_mode(dev));
47+
printk(" Is sourcing: %d\n", ppc_is_vbus_source(dev));
48+
printk(" Is sinking: %d\n", ppc_is_vbus_sink(dev));
49+
printk(" Is VBUS present: %d\n", ppc_is_vbus_present(dev));
50+
51+
return 0;
52+
}
53+
54+
/**
55+
* @brief Command that prints the status of one or all of the PPCs
56+
*
57+
* @param sh Shell structure
58+
* @param argc Arguments count
59+
* @param argv Device name
60+
* @return int ORed return values of all the functions executed, 0 in case of success
61+
*/
62+
static int cmd_ppc_status(const struct shell *sh, size_t argc, char **argv)
63+
{
64+
int ret = 0;
65+
66+
if (argc <= 1) {
67+
DT_FOREACH_STATUS_OKAY_VARGS(usb_c_connector, CALL_IF_HAS_PPC, print_status);
68+
} else {
69+
const struct device *dev = device_get_binding(argv[1]);
70+
71+
ret = print_status(dev);
72+
}
73+
74+
return ret;
75+
}
76+
77+
/**
78+
* @brief Command that requests one or all of the PPCs to try exiting the dead battery mode
79+
*
80+
* @param sh Shell structure
81+
* @param argc Arguments count
82+
* @param argv Device name
83+
* @return int ORed return values of all the functions executed, 0 in case of success
84+
*/
85+
static int cmd_ppc_exit_db(const struct shell *sh, size_t argc, char **argv)
86+
{
87+
int ret = 0;
88+
89+
if (argc <= 1) {
90+
DT_FOREACH_STATUS_OKAY_VARGS(usb_c_connector, CALL_IF_HAS_PPC,
91+
ppc_exit_dead_battery_mode);
92+
} else {
93+
const struct device *dev = device_get_binding(argv[1]);
94+
95+
ret = ppc_exit_dead_battery_mode(dev);
96+
}
97+
98+
return ret;
99+
}
100+
101+
/**
102+
* @brief Function used to create subcommands with devices names
103+
*
104+
* @param idx counter of devices
105+
* @param entry shell structure that will be filled
106+
*/
107+
static void device_name_get(size_t idx, struct shell_static_entry *entry)
108+
{
109+
const struct device *dev = shell_device_lookup(idx, NULL);
110+
111+
entry->syntax = (dev != NULL) ? dev->name : NULL;
112+
entry->handler = NULL;
113+
entry->help = NULL;
114+
entry->subcmd = NULL;
115+
}
116+
117+
SHELL_DYNAMIC_CMD_CREATE(list_device_names, device_name_get);
118+
119+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_ppc_cmds,
120+
SHELL_CMD_ARG(dump, &list_device_names,
121+
"Dump PPC registers\n"
122+
"Usage: ppc dump [<ppc device>]",
123+
cmd_ppc_dump, 1, 1),
124+
SHELL_CMD_ARG(status, &list_device_names,
125+
"Write PPC power status\n"
126+
"Usage: ppc statuc [<ppc device>]",
127+
cmd_ppc_status, 1, 1),
128+
SHELL_CMD_ARG(exitdb, &list_device_names,
129+
"Exit from the dead battery mode\n"
130+
"Usage: ppc exitdb [<ppc device>]",
131+
cmd_ppc_exit_db, 1, 1),
132+
SHELL_SUBCMD_SET_END);
133+
134+
SHELL_CMD_REGISTER(ppc, &sub_ppc_cmds, "PPC (USB-C PD) diagnostics", NULL);

0 commit comments

Comments
 (0)