1 /*
2  * Copyright 2022 The Chromium OS Authors
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 /**
7  * @file
8  * @brief USBC Type-C Port Controller device APIs
9  *
10  * This file contains the USB Type-C Port Controller device APIs.
11  * All Type-C Port Controller device drivers should implement the
12  * APIs described in this file.
13  */
14 
15 #ifndef ZEPHYR_INCLUDE_DRIVERS_USBC_USBC_TCPC_H_
16 #define ZEPHYR_INCLUDE_DRIVERS_USBC_USBC_TCPC_H_
17 
18 /**
19  * @brief USB Type-C Port Controller API
20  * @defgroup usb_type_c_port_controller_api USB Type-C Port Controller API
21  * @since 3.1
22  * @version 0.1.0
23  * @ingroup io_interfaces
24  * @{
25  */
26 
27 #include <zephyr/types.h>
28 #include <zephyr/device.h>
29 #include <errno.h>
30 
31 #include "usbc_tc.h"
32 #include "usbc_pd.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @brief TCPC Alert bits
40  */
41 enum tcpc_alert {
42 	/** CC status changed */
43 	TCPC_ALERT_CC_STATUS,
44 	/** Power status changed */
45 	TCPC_ALERT_POWER_STATUS,
46 	/** Receive Buffer register changed */
47 	TCPC_ALERT_MSG_STATUS,
48 	/** Received Hard Reset message */
49 	TCPC_ALERT_HARD_RESET_RECEIVED,
50 	/** SOP* message transmission not successful */
51 	TCPC_ALERT_TRANSMIT_MSG_FAILED,
52 	/**
53 	 * Reset or SOP* message transmission not sent
54 	 * due to an incoming receive message
55 	 */
56 	TCPC_ALERT_TRANSMIT_MSG_DISCARDED,
57 	/** Reset or SOP* message transmission successful */
58 	TCPC_ALERT_TRANSMIT_MSG_SUCCESS,
59 	/** A high-voltage alarm has occurred */
60 	TCPC_ALERT_VBUS_ALARM_HI,
61 	/** A low-voltage alarm has occurred */
62 	TCPC_ALERT_VBUS_ALARM_LO,
63 	/** A fault has occurred. Read the FAULT_STATUS register */
64 	TCPC_ALERT_FAULT_STATUS,
65 	/** TCPC RX buffer has overflowed */
66 	TCPC_ALERT_RX_BUFFER_OVERFLOW,
67 	/** The TCPC in Attached.SNK state has detected a sink disconnect */
68 	TCPC_ALERT_VBUS_SNK_DISCONNECT,
69 	/** Receive buffer register changed */
70 	TCPC_ALERT_BEGINNING_MSG_STATUS,
71 	/** Extended status changed */
72 	TCPC_ALERT_EXTENDED_STATUS,
73 	/**
74 	 * An extended interrupt event has occurred. Read the alert_extended
75 	 * register
76 	 */
77 	TCPC_ALERT_EXTENDED,
78 	/** A vendor defined alert has been detected */
79 	TCPC_ALERT_VENDOR_DEFINED
80 };
81 
82 /**
83  * @brief TCPC Status register
84  */
85 enum tcpc_status_reg {
86 	/** The CC Status register */
87 	TCPC_CC_STATUS,
88 	/** The Power Status register */
89 	TCPC_POWER_STATUS,
90 	/** The Fault Status register */
91 	TCPC_FAULT_STATUS,
92 	/** The Extended Status register */
93 	TCPC_EXTENDED_STATUS,
94 	/** The Extended Alert Status register */
95 	TCPC_EXTENDED_ALERT_STATUS,
96 	/** The Vendor Defined Status register */
97 	TCPC_VENDOR_DEFINED_STATUS
98 };
99 
100 /**
101  * @brief TCPC Chip Information
102  */
103 struct tcpc_chip_info {
104 	/** Vendor Id */
105 	uint16_t vendor_id;
106 	/** Product Id */
107 	uint16_t product_id;
108 	/** Device Id */
109 	uint16_t device_id;
110 	/** Firmware version number */
111 	uint64_t fw_version_number;
112 
113 	union {
114 		/** Minimum Required firmware version string */
115 		uint8_t min_req_fw_version_string[8];
116 		/** Minimum Required firmware version number */
117 		uint64_t min_req_fw_version_number;
118 	};
119 };
120 
121 typedef int (*tcpc_vconn_control_cb_t)(const struct device *dev, enum tc_cc_polarity pol,
122 				       bool enable);
123 typedef int (*tcpc_vconn_discharge_cb_t)(const struct device *dev, enum tc_cc_polarity pol,
124 					 bool enable);
125 typedef void (*tcpc_alert_handler_cb_t)(const struct device *dev, void *data,
126 					enum tcpc_alert alert);
127 
128 __subsystem struct tcpc_driver_api {
129 	int (*init)(const struct device *dev);
130 	int (*get_cc)(const struct device *dev, enum tc_cc_voltage_state *cc1,
131 		      enum tc_cc_voltage_state *cc2);
132 	int (*select_rp_value)(const struct device *dev, enum tc_rp_value rp);
133 	int (*get_rp_value)(const struct device *dev, enum tc_rp_value *rp);
134 	int (*set_cc)(const struct device *dev, enum tc_cc_pull pull);
135 	void (*set_vconn_discharge_cb)(const struct device *dev, tcpc_vconn_discharge_cb_t cb);
136 	void (*set_vconn_cb)(const struct device *dev, tcpc_vconn_control_cb_t vconn_cb);
137 	int (*vconn_discharge)(const struct device *dev, bool enable);
138 	int (*set_vconn)(const struct device *dev, bool enable);
139 	int (*set_roles)(const struct device *dev, enum tc_power_role power_role,
140 			 enum tc_data_role data_role);
141 	int (*get_rx_pending_msg)(const struct device *dev, struct pd_msg *msg);
142 	int (*set_rx_enable)(const struct device *dev, bool enable);
143 	int (*set_cc_polarity)(const struct device *dev, enum tc_cc_polarity polarity);
144 	int (*transmit_data)(const struct device *dev, struct pd_msg *msg);
145 	int (*dump_std_reg)(const struct device *dev);
146 	void (*alert_handler_cb)(const struct device *dev, void *data, enum tcpc_alert alert);
147 	int (*get_status_register)(const struct device *dev, enum tcpc_status_reg reg,
148 				   uint32_t *status);
149 	int (*clear_status_register)(const struct device *dev, enum tcpc_status_reg reg,
150 				     uint32_t mask);
151 	int (*mask_status_register)(const struct device *dev, enum tcpc_status_reg reg,
152 				    uint32_t mask);
153 	int (*set_debug_accessory)(const struct device *dev, bool enable);
154 	int (*set_debug_detach)(const struct device *dev);
155 	int (*set_drp_toggle)(const struct device *dev, bool enable);
156 	int (*get_snk_ctrl)(const struct device *dev);
157 	int (*set_snk_ctrl)(const struct device *dev, bool enable);
158 	int (*get_src_ctrl)(const struct device *dev);
159 	int (*set_src_ctrl)(const struct device *dev, bool enable);
160 	int (*get_chip_info)(const struct device *dev, struct tcpc_chip_info *chip_info);
161 	int (*set_low_power_mode)(const struct device *dev, bool enable);
162 	int (*sop_prime_enable)(const struct device *dev, bool enable);
163 	int (*set_bist_test_mode)(const struct device *dev, bool enable);
164 	int (*set_alert_handler_cb)(const struct device *dev, tcpc_alert_handler_cb_t handler,
165 				    void *data);
166 };
167 
168 /**
169  * @brief Returns whether the sink has detected a Rp resistor on the other side
170  */
tcpc_is_cc_rp(enum tc_cc_voltage_state cc)171 static inline int tcpc_is_cc_rp(enum tc_cc_voltage_state cc)
172 {
173 	return (cc == TC_CC_VOLT_RP_DEF) || (cc == TC_CC_VOLT_RP_1A5) || (cc == TC_CC_VOLT_RP_3A0);
174 }
175 
176 /**
177  * @brief Returns true if both CC lines are completely open
178  */
tcpc_is_cc_open(enum tc_cc_voltage_state cc1,enum tc_cc_voltage_state cc2)179 static inline int tcpc_is_cc_open(enum tc_cc_voltage_state cc1, enum tc_cc_voltage_state cc2)
180 {
181 	return (cc1 < TC_CC_VOLT_RD) && (cc2 < TC_CC_VOLT_RD);
182 }
183 
184 /**
185  * @brief Returns true if we detect the port partner is a snk debug accessory
186  */
tcpc_is_cc_snk_dbg_acc(enum tc_cc_voltage_state cc1,enum tc_cc_voltage_state cc2)187 static inline int tcpc_is_cc_snk_dbg_acc(enum tc_cc_voltage_state cc1, enum tc_cc_voltage_state cc2)
188 {
189 	return cc1 == TC_CC_VOLT_RD && cc2 == TC_CC_VOLT_RD;
190 }
191 
192 /**
193  * @brief Returns true if we detect the port partner is a src debug accessory
194  */
tcpc_is_cc_src_dbg_acc(enum tc_cc_voltage_state cc1,enum tc_cc_voltage_state cc2)195 static inline int tcpc_is_cc_src_dbg_acc(enum tc_cc_voltage_state cc1, enum tc_cc_voltage_state cc2)
196 {
197 	return tcpc_is_cc_rp(cc1) && tcpc_is_cc_rp(cc2);
198 }
199 
200 /**
201  * @brief Returns true if the port partner is an audio accessory
202  */
tcpc_is_cc_audio_acc(enum tc_cc_voltage_state cc1,enum tc_cc_voltage_state cc2)203 static inline int tcpc_is_cc_audio_acc(enum tc_cc_voltage_state cc1, enum tc_cc_voltage_state cc2)
204 {
205 	return cc1 == TC_CC_VOLT_RA && cc2 == TC_CC_VOLT_RA;
206 }
207 
208 /**
209  * @brief Returns true if the port partner is presenting at least one Rd
210  */
tcpc_is_cc_at_least_one_rd(enum tc_cc_voltage_state cc1,enum tc_cc_voltage_state cc2)211 static inline int tcpc_is_cc_at_least_one_rd(enum tc_cc_voltage_state cc1,
212 					     enum tc_cc_voltage_state cc2)
213 {
214 	return cc1 == TC_CC_VOLT_RD || cc2 == TC_CC_VOLT_RD;
215 }
216 
217 /**
218  * @brief Returns true if the port partner is presenting Rd on only one CC line
219  */
tcpc_is_cc_only_one_rd(enum tc_cc_voltage_state cc1,enum tc_cc_voltage_state cc2)220 static inline int tcpc_is_cc_only_one_rd(enum tc_cc_voltage_state cc1, enum tc_cc_voltage_state cc2)
221 {
222 	return tcpc_is_cc_at_least_one_rd(cc1, cc2) && cc1 != cc2;
223 }
224 
225 /**
226  * @brief Initializes the TCPC
227  *
228  * @param dev Runtime device structure
229  *
230  * @retval 0 on success
231  * @retval -EIO on failure
232  * @retval -EAGAIN if initialization should be postponed
233  */
tcpc_init(const struct device * dev)234 static inline int tcpc_init(const struct device *dev)
235 {
236 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
237 
238 	__ASSERT(api->init != NULL, "Callback pointer should not be NULL");
239 
240 	return api->init(dev);
241 }
242 
243 /**
244  * @brief Reads the status of the CC lines
245  *
246  * @param dev  Runtime device structure
247  * @param cc1  A pointer where the CC1 status is written
248  * @param cc2  A pointer where the CC2 status is written
249  *
250  * @retval 0 on success
251  * @retval -EIO on failure
252  * @retval -ENOSYS if not implemented
253  */
tcpc_get_cc(const struct device * dev,enum tc_cc_voltage_state * cc1,enum tc_cc_voltage_state * cc2)254 static inline int tcpc_get_cc(const struct device *dev, enum tc_cc_voltage_state *cc1,
255 			      enum tc_cc_voltage_state *cc2)
256 {
257 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
258 
259 	if (api->get_cc == NULL) {
260 		return -ENOSYS;
261 	}
262 
263 	return api->get_cc(dev, cc1, cc2);
264 }
265 
266 /**
267  * @brief Sets the value of CC pull up resistor used when operating as a Source
268  *
269  * @param dev   Runtime device structure
270  * @param rp    Value of the Pull-Up Resistor.
271  *
272  * @retval 0 on success
273  * @retval -ENOSYS
274  * @retval -EIO on failure
275  */
tcpc_select_rp_value(const struct device * dev,enum tc_rp_value rp)276 static inline int tcpc_select_rp_value(const struct device *dev, enum tc_rp_value rp)
277 {
278 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
279 
280 	if (api->select_rp_value == NULL) {
281 		return -ENOSYS;
282 	}
283 
284 	return api->select_rp_value(dev, rp);
285 }
286 
287 /**
288  * @brief Gets the value of the CC pull up resistor used when operating as a Source
289  *
290  * @param dev   Runtime device structure
291  * @param rp    pointer where the value of the Pull-Up Resistor is stored
292  *
293  * @retval 0 on success
294  * @retval -ENOSYS
295  * @retval -EIO on failure
296  */
tcpc_get_rp_value(const struct device * dev,enum tc_rp_value * rp)297 static inline int tcpc_get_rp_value(const struct device *dev, enum tc_rp_value *rp)
298 {
299 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
300 
301 	if (api->get_rp_value == NULL) {
302 		return -ENOSYS;
303 	}
304 
305 	return api->get_rp_value(dev, rp);
306 }
307 
308 /**
309  * @brief Sets the CC pull resistor and sets the role as either Source or Sink
310  *
311  * @param dev   Runtime device structure
312  * @param pull  The pull resistor to set
313  *
314  * @retval 0 on success
315  * @retval -EIO on failure
316  */
tcpc_set_cc(const struct device * dev,enum tc_cc_pull pull)317 static inline int tcpc_set_cc(const struct device *dev, enum tc_cc_pull pull)
318 {
319 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
320 
321 	__ASSERT(api->set_cc != NULL, "Callback pointer should not be NULL");
322 
323 	return api->set_cc(dev, pull);
324 }
325 
326 /**
327  * @brief Sets a callback that can enable or disable VCONN if the TCPC is
328  *	  unable to or the system is configured in a way that does not use
329  *	  the VCONN control capabilities of the TCPC
330  *
331  * The callback is called in the tcpc_set_vconn function if vconn_cb isn't NULL
332  *
333  * @param dev       Runtime device structure
334  * @param vconn_cb  pointer to the callback function that controls vconn
335  */
tcpc_set_vconn_cb(const struct device * dev,tcpc_vconn_control_cb_t vconn_cb)336 static inline void tcpc_set_vconn_cb(const struct device *dev, tcpc_vconn_control_cb_t vconn_cb)
337 {
338 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
339 
340 	__ASSERT(api->set_vconn_cb != NULL, "Callback pointer should not be NULL");
341 
342 	api->set_vconn_cb(dev, vconn_cb);
343 }
344 
345 /**
346  * @brief Sets a callback that can enable or discharge VCONN if the TCPC is
347  *	  unable to or the system is configured in a way that does not use
348  *	  the VCONN control capabilities of the TCPC
349  *
350  * The callback is called in the tcpc_vconn_discharge function if cb isn't NULL
351  *
352  * @param dev       Runtime device structure
353  * @param cb  pointer to the callback function that discharges vconn
354  */
tcpc_set_vconn_discharge_cb(const struct device * dev,tcpc_vconn_discharge_cb_t cb)355 static inline void tcpc_set_vconn_discharge_cb(const struct device *dev,
356 					       tcpc_vconn_discharge_cb_t cb)
357 {
358 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
359 
360 	__ASSERT(api->set_vconn_discharge_cb != NULL, "Callback pointer should not be NULL");
361 
362 	api->set_vconn_discharge_cb(dev, cb);
363 }
364 
365 /**
366  * @brief Discharges VCONN
367  *
368  * This function uses the TCPC to discharge VCONN if possible or calls the
369  * callback function set by tcpc_set_vconn_cb
370  *
371  * @param dev     Runtime device structure
372  * @param enable  VCONN discharge is enabled when true, it's disabled
373  *
374  * @retval 0 on success
375  * @retval -EIO on failure
376  * @retval -ENOSYS if not implemented
377  */
tcpc_vconn_discharge(const struct device * dev,bool enable)378 static inline int tcpc_vconn_discharge(const struct device *dev, bool enable)
379 {
380 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
381 
382 	if (api->vconn_discharge == NULL) {
383 		return -ENOSYS;
384 	}
385 
386 	return api->vconn_discharge(dev, enable);
387 }
388 
389 /**
390  * @brief Enables or disables VCONN
391  *
392  * This function uses the TCPC to measure VCONN if possible or calls the
393  * callback function set by tcpc_set_vconn_cb
394  *
395  * @param dev     Runtime device structure
396  * @param enable  VCONN is enabled when true, it's disabled
397  *
398  * @retval 0 on success
399  * @retval -EIO on failure
400  * @retval -ENOSYS if not implemented
401  */
tcpc_set_vconn(const struct device * dev,bool enable)402 static inline int tcpc_set_vconn(const struct device *dev, bool enable)
403 {
404 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
405 
406 	if (api->set_vconn == NULL) {
407 		return -ENOSYS;
408 	}
409 
410 	return api->set_vconn(dev, enable);
411 }
412 
413 /**
414  * @brief Sets the Power and Data Role of the PD message header
415  *
416  * This function only needs to be called once per data / power role change
417  *
418  * @param dev         Runtime device structure
419  * @param power_role  current power role
420  * @param data_role   current data role
421  *
422  * @retval 0 on success
423  * @retval -EIO on failure
424  * @retval -ENOSYS if not implemented
425  */
tcpc_set_roles(const struct device * dev,enum tc_power_role power_role,enum tc_data_role data_role)426 static inline int tcpc_set_roles(const struct device *dev, enum tc_power_role power_role,
427 				 enum tc_data_role data_role)
428 {
429 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
430 
431 	if (api->set_roles == NULL) {
432 		return -ENOSYS;
433 	}
434 
435 	return api->set_roles(dev, power_role, data_role);
436 }
437 
438 /**
439  * @brief Retrieves the Power Delivery message from the TCPC.
440  * If buf is NULL, then only the status is returned, where 0 means there is a message pending and
441  * -ENODATA means there is no pending message.
442  *
443  * @param dev Runtime device structure
444  * @param buf pointer where the pd_buf pointer is written, NULL if only checking the status
445  *
446  * @retval Greater or equal to 0 is the number of bytes received if buf parameter is provided
447  * @retval 0 if there is a message pending and buf parameter is NULL
448  * @retval -EIO on failure
449  * @retval -ENODATA if no message is pending
450  */
tcpc_get_rx_pending_msg(const struct device * dev,struct pd_msg * buf)451 static inline int tcpc_get_rx_pending_msg(const struct device *dev, struct pd_msg *buf)
452 {
453 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
454 
455 	__ASSERT(api->get_rx_pending_msg != NULL, "Callback pointer should not be NULL");
456 
457 	return api->get_rx_pending_msg(dev, buf);
458 }
459 
460 /**
461  * @brief Enables the reception of SOP* message types
462  *
463  * @param dev     Runtime device structure
464  * @param enable  Enable Power Delivery when true, else it's
465  *		  disabled
466  *
467  * @retval 0 on success
468  * @retval -EIO on failure
469  * @retval -ENOSYS if not implemented
470  */
tcpc_set_rx_enable(const struct device * dev,bool enable)471 static inline int tcpc_set_rx_enable(const struct device *dev, bool enable)
472 {
473 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
474 
475 	if (api->set_rx_enable == NULL) {
476 		return -ENOSYS;
477 	}
478 
479 	return api->set_rx_enable(dev, enable);
480 }
481 
482 /**
483  * @brief Sets the polarity of the CC lines
484  *
485  * @param dev       Runtime device structure
486  * @param polarity  Polarity of the cc line
487  *
488  * @retval 0 on success
489  * @retval -EIO on failure
490  */
tcpc_set_cc_polarity(const struct device * dev,enum tc_cc_polarity polarity)491 static inline int tcpc_set_cc_polarity(const struct device *dev, enum tc_cc_polarity polarity)
492 {
493 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
494 
495 	__ASSERT(api->set_cc_polarity != NULL, "Callback pointer should not be NULL");
496 
497 	return api->set_cc_polarity(dev, polarity);
498 }
499 
500 /**
501  * @brief Transmits a Power Delivery message
502  *
503  * @param dev     Runtime device structure
504  * @param msg     Power Delivery message to transmit
505  *
506  * @retval 0 on success
507  * @retval -EIO on failure
508  * @retval -ENOSYS if not implemented
509  */
tcpc_transmit_data(const struct device * dev,struct pd_msg * msg)510 static inline int tcpc_transmit_data(const struct device *dev, struct pd_msg *msg)
511 {
512 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
513 
514 	if (api->transmit_data == NULL) {
515 		return -ENOSYS;
516 	}
517 
518 	return api->transmit_data(dev, msg);
519 }
520 
521 /**
522  * @brief Dump a set of TCPC registers
523  *
524  * @param dev Runtime device structure
525  *
526  * @retval 0 on success
527  * @retval -EIO on failure
528  * @retval -ENOSYS if not implemented
529  */
tcpc_dump_std_reg(const struct device * dev)530 static inline int tcpc_dump_std_reg(const struct device *dev)
531 {
532 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
533 
534 	if (api->dump_std_reg == NULL) {
535 		return -ENOSYS;
536 	}
537 
538 	return api->dump_std_reg(dev);
539 }
540 
541 /**
542  * @brief Sets the alert function that's called when an interrupt is triggered
543  *	  due to an alert bit
544  *
545  * Calling this function enables the particular alert bit
546  *
547  * @param dev      Runtime device structure
548  * @param handler  The callback function called when the bit is set
549  * @param data     user data passed to the callback
550  *
551  * @retval 0 on success
552  * @retval -EINVAL on failure
553  */
tcpc_set_alert_handler_cb(const struct device * dev,tcpc_alert_handler_cb_t handler,void * data)554 static inline int tcpc_set_alert_handler_cb(const struct device *dev,
555 					    tcpc_alert_handler_cb_t handler, void *data)
556 {
557 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
558 
559 	__ASSERT(api->set_alert_handler_cb != NULL, "Callback pointer should not be NULL");
560 
561 	return api->set_alert_handler_cb(dev, handler, data);
562 }
563 
564 /**
565  * @brief Gets a status register
566  *
567  * @param dev     Runtime device structure
568  * @param reg     The status register to read
569  * @param status  Pointer where the status is stored
570  *
571  * @retval 0 on success
572  * @retval -EIO on failure
573  * @retval -ENOSYS if not implemented
574  */
tcpc_get_status_register(const struct device * dev,enum tcpc_status_reg reg,uint32_t * status)575 static inline int tcpc_get_status_register(const struct device *dev, enum tcpc_status_reg reg,
576 					   uint32_t *status)
577 {
578 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
579 
580 	if (api->get_status_register == NULL) {
581 		return -ENOSYS;
582 	}
583 
584 	return api->get_status_register(dev, reg, status);
585 }
586 
587 /**
588  * @brief Clears a TCPC status register
589  *
590  * @param dev   Runtime device structure
591  * @param reg   The status register to read
592  * @param mask  A bit mask of the status register to clear.
593  *		A status bit is cleared when it's set to 1.
594  *
595  * @retval 0 on success
596  * @retval -EIO on failure
597  * @retval -ENOSYS if not implemented
598  */
tcpc_clear_status_register(const struct device * dev,enum tcpc_status_reg reg,uint32_t mask)599 static inline int tcpc_clear_status_register(const struct device *dev, enum tcpc_status_reg reg,
600 					     uint32_t mask)
601 {
602 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
603 
604 	if (api->clear_status_register == NULL) {
605 		return -ENOSYS;
606 	}
607 
608 	return api->clear_status_register(dev, reg, mask);
609 }
610 
611 /**
612  * @brief Sets the mask of a TCPC status register
613  *
614  * @param dev   Runtime device structure
615  * @param reg   The status register to read
616  * @param mask  A bit mask of the status register to mask.
617  *		The status bit is masked if it's 0, else it's unmasked.
618  *
619  * @retval 0 on success
620  * @retval -EIO on failure
621  * @retval -ENOSYS if not implemented
622  */
tcpc_mask_status_register(const struct device * dev,enum tcpc_status_reg reg,uint32_t mask)623 static inline int tcpc_mask_status_register(const struct device *dev, enum tcpc_status_reg reg,
624 					    uint32_t mask)
625 {
626 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
627 
628 	if (api->mask_status_register == NULL) {
629 		return -ENOSYS;
630 	}
631 
632 	return api->mask_status_register(dev, reg, mask);
633 }
634 
635 /**
636  * @brief Manual control of TCPC DebugAccessory control
637  *
638  * @param dev     Runtime device structure
639  * @param enable  Enable Debug Accessory when true, else it's disabled
640  *
641  * @retval 0 on success
642  * @retval -EIO on failure
643  * @retval -ENOSYS if not implemented
644  */
tcpc_set_debug_accessory(const struct device * dev,bool enable)645 static inline int tcpc_set_debug_accessory(const struct device *dev, bool enable)
646 {
647 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
648 
649 	if (api->set_debug_accessory == NULL) {
650 		return -ENOSYS;
651 	}
652 
653 	return api->set_debug_accessory(dev, enable);
654 }
655 
656 /**
657  * @brief Detach from a debug connection
658  *
659  * @param dev Runtime device structure
660  *
661  * @retval 0 on success
662  * @retval -EIO on failure
663  * @retval -ENOSYS if not implemented
664  */
tcpc_set_debug_detach(const struct device * dev)665 static inline int tcpc_set_debug_detach(const struct device *dev)
666 {
667 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
668 
669 	if (api->set_debug_detach == NULL) {
670 		return -ENOSYS;
671 	}
672 
673 	return api->set_debug_detach(dev);
674 }
675 
676 /**
677  * @brief Enable TCPC auto dual role toggle
678  *
679  * @param dev     Runtime device structure
680  * @param enable  Auto dual role toggle is active when true, else it's disabled
681  *
682  * @retval 0 on success
683  * @retval -EIO on failure
684  * @retval -ENOSYS if not implemented
685  */
tcpc_set_drp_toggle(const struct device * dev,bool enable)686 static inline int tcpc_set_drp_toggle(const struct device *dev, bool enable)
687 {
688 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
689 
690 	if (api->set_drp_toggle == NULL) {
691 		return -ENOSYS;
692 	}
693 
694 	return api->set_drp_toggle(dev, enable);
695 }
696 
697 /**
698  * @brief Queries the current sinking state of the TCPC
699  *
700  * @param dev Runtime device structure
701  *
702  * @retval true if sinking power
703  * @retval false if not sinking power
704  * @retval -ENOSYS if not implemented
705  */
tcpc_get_snk_ctrl(const struct device * dev)706 static inline int tcpc_get_snk_ctrl(const struct device *dev)
707 {
708 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
709 
710 	if (api->get_snk_ctrl == NULL) {
711 		return -ENOSYS;
712 	}
713 
714 	return api->get_snk_ctrl(dev);
715 }
716 
717 /**
718  * @brief Set the VBUS sinking state of the TCPC
719  *
720  * @param dev Runtime device structure
721  * @param enable True if sinking should be enabled, false if disabled
722  * @retval 0 on success
723  * @retval -ENOSYS if not implemented
724  */
tcpc_set_snk_ctrl(const struct device * dev,bool enable)725 static inline int tcpc_set_snk_ctrl(const struct device *dev, bool enable)
726 {
727 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
728 
729 	if (api->set_snk_ctrl == NULL) {
730 		return -ENOSYS;
731 	}
732 
733 	return api->set_snk_ctrl(dev, enable);
734 }
735 
736 /**
737  * @brief Queries the current sourcing state of the TCPC
738  *
739  * @param dev Runtime device structure
740  *
741  * @retval true if sourcing power
742  * @retval false if not sourcing power
743  * @retval -ENOSYS if not implemented
744  */
tcpc_get_src_ctrl(const struct device * dev)745 static inline int tcpc_get_src_ctrl(const struct device *dev)
746 {
747 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
748 
749 	if (api->get_src_ctrl == NULL) {
750 		return -ENOSYS;
751 	}
752 
753 	return api->get_src_ctrl(dev);
754 }
755 
756 /**
757  * @brief Set the VBUS sourcing state of the TCPC
758  *
759  * @param dev Runtime device structure
760  * @param enable True if sourcing should be enabled, false if disabled
761  * @retval 0 on success
762  * @retval -ENOSYS if not implemented
763  */
tcpc_set_src_ctrl(const struct device * dev,bool enable)764 static inline int tcpc_set_src_ctrl(const struct device *dev, bool enable)
765 {
766 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
767 
768 	if (api->set_src_ctrl == NULL) {
769 		return -ENOSYS;
770 	}
771 
772 	return api->set_src_ctrl(dev, enable);
773 }
774 
775 /**
776  * @brief Controls the BIST Mode of the TCPC. It disables RX alerts while the
777  *	  mode is active.
778  *
779  * @param dev Runtime device structure
780  * @param enable The TCPC enters BIST TEST Mode when true
781  *
782  * @retval 0 on success
783  * @retval -EIO on failure
784  * @retval -ENOSYS if not implemented
785  */
tcpc_set_bist_test_mode(const struct device * dev,bool enable)786 static inline int tcpc_set_bist_test_mode(const struct device *dev, bool enable)
787 {
788 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
789 
790 	if (api->set_bist_test_mode == NULL) {
791 		return -ENOSYS;
792 	}
793 
794 	return api->set_bist_test_mode(dev, enable);
795 }
796 
797 /**
798  * @brief Gets the TCPC firmware version
799  *
800  * @param dev        Runtime device structure
801  * @param chip_info  Pointer to TCPC chip info where the version is stored
802  *
803  * @retval 0 on success
804  * @retval -EIO on failure
805  * @retval -ENOSYS if not implemented
806  */
tcpc_get_chip_info(const struct device * dev,struct tcpc_chip_info * chip_info)807 static inline int tcpc_get_chip_info(const struct device *dev, struct tcpc_chip_info *chip_info)
808 {
809 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
810 
811 	if (api->get_chip_info == NULL) {
812 		return -ENOSYS;
813 	}
814 
815 	return api->get_chip_info(dev, chip_info);
816 }
817 
818 /**
819  * @brief Instructs the TCPC to enter or exit low power mode
820  *
821  * @param dev     Runtime device structure
822  * @param enable  The TCPC enters low power mode when true, else it exits it
823  *
824  * @retval 0 on success
825  * @retval -EIO on failure
826  * @retval -ENOSYS if not implemented
827  */
tcpc_set_low_power_mode(const struct device * dev,bool enable)828 static inline int tcpc_set_low_power_mode(const struct device *dev, bool enable)
829 {
830 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
831 
832 	if (api->set_low_power_mode == NULL) {
833 		return -ENOSYS;
834 	}
835 
836 	return api->set_low_power_mode(dev, enable);
837 }
838 
839 /**
840  * @brief Enables the reception of SOP Prime messages
841  *
842  * @param dev     Runtime device structure
843  * @param enable  Can receive SOP Prime messages when true, else it can not
844  *
845  * @retval 0 on success
846  * @retval -EIO on failure
847  * @retval -ENOSYS if not implemented
848  */
tcpc_sop_prime_enable(const struct device * dev,bool enable)849 static inline int tcpc_sop_prime_enable(const struct device *dev, bool enable)
850 {
851 	const struct tcpc_driver_api *api = (const struct tcpc_driver_api *)dev->api;
852 
853 	if (api->sop_prime_enable == NULL) {
854 		return -ENOSYS;
855 	}
856 
857 	return api->sop_prime_enable(dev, enable);
858 }
859 
860 /**
861  * @}
862  */
863 
864 #ifdef __cplusplus
865 }
866 #endif
867 
868 #endif /* ZEPHYR_INCLUDE_DRIVERS_USBC_USBC_TCPC_H_ */
869