1 /*
2  * Copyright (c) 2023 The Chromium OS Authors
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SUBSYS_USBC_PRL_H_
8 #define ZEPHYR_SUBSYS_USBC_PRL_H_
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/usb_c/usbc.h>
12 #include <zephyr/drivers/usb_c/usbc_tcpc.h>
13 #include <zephyr/smf.h>
14 
15 #include "usbc_pe_common_internal.h"
16 #include "usbc_timer.h"
17 
18 /**
19  * @brief PD counter definitions
20  *	  See Table 6-63 Counter parameters
21  *	  Parameter Name: nMessageIDCount
22  */
23 #define PD_MESSAGE_ID_COUNT 7
24 
25 /**
26  * @brief Message Reception State Machine Object
27  */
28 struct protocol_layer_rx_t {
29 	/** state machine flags */
30 	atomic_t flags;
31 	/** message ids for all valid port partners */
32 	int msg_id[NUM_SOP_STAR_TYPES];
33 	/** Received Power Delivery Messages are stored in emsg */
34 	struct pd_msg emsg;
35 };
36 
37 /**
38  * @brief Message Transmission State Machine Object
39  */
40 struct protocol_layer_tx_t {
41 	/** state machine context */
42 	struct smf_ctx ctx;
43 	/** Port device */
44 	const struct device *dev;
45 	/** state machine flags */
46 	atomic_t flags;
47 	/** last packet type we transmitted */
48 	enum pd_packet_type last_xmit_type;
49 	/** Current message type to transmit */
50 	uint8_t msg_type;
51 	/**
52 	 * Power Delivery Messages meant for transmission are stored
53 	 * in emsg
54 	 */
55 	struct pd_msg emsg;
56 
57 	/* Counters */
58 
59 	/** message id counters for all 6 port partners */
60 	uint32_t msg_id_counter[NUM_SOP_STAR_TYPES];
61 
62 	/* Timers */
63 
64 	/** tTxTimeout timer */
65 	struct usbc_timer_t pd_t_tx_timeout;
66 	/** tSinkTx timer */
67 	struct usbc_timer_t pd_t_sink_tx;
68 };
69 
70 /**
71  * @brief Hard Reset State Machine Object
72  */
73 struct protocol_hard_reset_t {
74 	/** state machine context */
75 	struct smf_ctx ctx;
76 	/** Port device */
77 	const struct device *dev;
78 	/** state machine flags */
79 	atomic_t flags;
80 
81 	/* Timers */
82 
83 	/** tHardResetComplete timer */
84 	struct usbc_timer_t pd_t_hard_reset_complete;
85 };
86 
87 /**
88  * @brief This function must only be called in the subsystem init function.
89  *
90  * @param dev Pointer to the device structure for the driver instance.
91  */
92 void prl_subsys_init(const struct device *dev);
93 
94 /**
95  * @brief Start the PRL Layer state machine. This is only called from the
96  *	  Type-C state machine.
97  *
98  * @param dev Pointer to the device structure for the driver instance
99  */
100 void prl_start(const struct device *dev);
101 
102 /**
103  * @brief Inform the PRL that the first message in an AMS is being sent
104  *
105  * @param dev Pointer to the device structure for the driver instance
106  */
107 void prl_first_msg_notificaiton(const struct device *dev);
108 
109 /**
110  * @brief Suspends the PRL Layer state machine. This is only called from the
111  *	  Type-C state machine.
112  *
113  * @param dev Pointer to the device structure for the driver instance
114  */
115 void prl_suspend(const struct device *dev);
116 
117 /**
118  * @brief Reset the PRL Layer state machine
119  *
120  * @param dev Pointer to the device structure for the driver instance
121  */
122 void prl_reset(const struct device *dev);
123 
124 /**
125  * @brief Run the PRL Layer state machine. This is called from the subsystems
126  *	  port stack thread
127  *
128  * @param dev Pointer to the device structure for the driver instance
129  */
130 void prl_run(const struct device *dev);
131 
132 /**
133  * @brief Called from the Policy Engine to signal that a hard reset is complete
134  *
135  * @param dev Pointer to the device structure for the driver instance
136  */
137 void prl_hard_reset_complete(const struct device *dev);
138 
139 /**
140  * @brief Sets the revision received from the port partner
141  *
142  * @param dev Pointer to the device structure for the driver instance
143  * @param type SOP* packet sent from port partner
144  * @param rev Revision sent from the port partner
145  */
146 void prl_set_rev(const struct device *dev, const enum pd_packet_type type,
147 		 const enum pd_rev_type rev);
148 
149 /**
150  * @brief Gets the revision received assciated with a packet type
151  *
152  * @param dev Pointer to the device structure for the driver instance
153  * @param type SOP* packet type to get the revision for
154  *
155  * @retval revsion associated with the packet type
156  */
157 enum pd_rev_type prl_get_rev(const struct device *dev, const enum pd_packet_type type);
158 
159 /**
160  * @brief Instructs the Protocol Layer to send a Power Delivery control message
161  *
162  * @param dev Pointer to the device structure for the driver instance
163  * @param type The port partner to send this message to
164  * @param msg The control message to send
165  */
166 void prl_send_ctrl_msg(const struct device *dev, const enum pd_packet_type type,
167 		       const enum pd_ctrl_msg_type msg);
168 
169 /**
170  * @brief Instructs the Protocol Layer to send a Power Delivery data message
171  *
172  * @param dev Pointer to the device structure for the driver instance
173  * @param type The port partner to send this message to
174  * @param msg The data message to send
175  */
176 void prl_send_data_msg(const struct device *dev, const enum pd_packet_type type,
177 		       const enum pd_data_msg_type msg);
178 
179 /**
180  * @brief Instructs the Protocol Layer to execute a hard reset
181  *
182  * @param dev Pointer to the device structure for the driver instance
183  */
184 void prl_execute_hard_reset(const struct device *dev);
185 
186 /**
187  * @brief Query if the Protocol Layer is running
188  *
189  * @param dev Pointer to the device structure for the driver instance
190  *
191  * @retval TRUE if the Protocol Layer is running
192  * @retval FALSE if the Protocol Layer is not running
193  */
194 bool prl_is_running(const struct device *dev);
195 
196 #endif /* ZEPHYR_SUBSYS_USBC_PRL_H_ */
197