1 /*
2  * Copyright 2022 The Chromium OS Authors
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 /**
7  * @file
8  * @brief USB-C VBUS device APIs
9  *
10  * This file contains the USB-C VBUS device APIs.
11  * All USB-C VBUS measurment and control device drivers should
12  * implement the APIs described in this file.
13  */
14 
15 #ifndef ZEPHYR_INCLUDE_DRIVERS_USBC_VBUS_H_
16 #define ZEPHYR_INCLUDE_DRIVERS_USBC_VBUS_H_
17 
18 /**
19  * @brief USB-C VBUS API
20  * @defgroup usbc_vbus_api USB-C VBUS API
21  * @ingroup io_interfaces
22  * @{
23  */
24 
25 #include <zephyr/types.h>
26 #include <zephyr/device.h>
27 #include <zephyr/drivers/usb_c/usbc_tc.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 struct usbc_vbus_driver_api {
34 	bool (*check_level)(const struct device *dev, enum tc_vbus_level level);
35 	int (*measure)(const struct device *dev, int *vbus_meas);
36 	int (*discharge)(const struct device *dev, bool enable);
37 	int (*enable)(const struct device *dev, bool enable);
38 };
39 
40 /**
41  * @brief Checks if VBUS is at a particular level
42  *
43  * @param dev    Runtime device structure
44  * @param level  The level voltage to check against
45  *
46  * @retval true if VBUS is at the level voltage
47  * @retval false if VBUS is not at that level voltage
48  */
usbc_vbus_check_level(const struct device * dev,enum tc_vbus_level level)49 static inline bool usbc_vbus_check_level(const struct device *dev, enum tc_vbus_level level)
50 {
51 	const struct usbc_vbus_driver_api *api = (const struct usbc_vbus_driver_api *)dev->api;
52 
53 	return api->check_level(dev, level);
54 }
55 
56 /**
57  * @brief Reads and returns VBUS measured in mV
58  *
59  * @param dev        Runtime device structure
60  * @param meas       pointer where the measured VBUS voltage is stored
61  *
62  * @retval 0 on success
63  * @retval -EIO on failure
64  */
usbc_vbus_measure(const struct device * dev,int * meas)65 static inline int usbc_vbus_measure(const struct device *dev, int *meas)
66 {
67 	const struct usbc_vbus_driver_api *api = (const struct usbc_vbus_driver_api *)dev->api;
68 
69 	return api->measure(dev, meas);
70 }
71 
72 /**
73  * @brief Controls a pin that discharges VBUS
74  *
75  * @param dev        Runtime device structure
76  * @param enable     Discharge VBUS when true
77  *
78  * @retval 0 on success
79  * @retval -EIO on failure
80  * @retval -ENOENT if discharge pin isn't defined
81  */
usbc_vbus_discharge(const struct device * dev,bool enable)82 static inline int usbc_vbus_discharge(const struct device *dev, bool enable)
83 {
84 	const struct usbc_vbus_driver_api *api = (const struct usbc_vbus_driver_api *)dev->api;
85 
86 	return api->discharge(dev, enable);
87 }
88 
89 /**
90  * @brief Controls a pin that enables VBUS measurments
91  *
92  * @param dev     Runtime device structure
93  * @param enable  enable VBUS measurments when true
94  *
95  * @retval 0 on success
96  * @retval -EIO on failure
97  * @retval -ENOENT if enable pin isn't defined
98  */
usbc_vbus_enable(const struct device * dev,bool enable)99 static inline int usbc_vbus_enable(const struct device *dev, bool enable)
100 {
101 	const struct usbc_vbus_driver_api *api = (const struct usbc_vbus_driver_api *)dev->api;
102 
103 	return api->enable(dev, enable);
104 }
105 
106 /**
107  * @}
108  */
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 #endif /* ZEPHYR_INCLUDE_DRIVERS_USBC_VBUS_H_ */
115