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 measurement 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  * @since 3.3
22  * @version 0.1.0
23  * @ingroup io_interfaces
24  * @{
25  */
26 
27 #include <zephyr/types.h>
28 #include <zephyr/device.h>
29 #include <zephyr/drivers/usb_c/usbc_tc.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 __subsystem struct usbc_vbus_driver_api {
36 	bool (*check_level)(const struct device *dev, enum tc_vbus_level level);
37 	int (*measure)(const struct device *dev, int *vbus_meas);
38 	int (*discharge)(const struct device *dev, bool enable);
39 	int (*enable)(const struct device *dev, bool enable);
40 };
41 
42 /**
43  * @brief Checks if VBUS is at a particular level
44  *
45  * @param dev    Runtime device structure
46  * @param level  The level voltage to check against
47  *
48  * @retval true if VBUS is at the level voltage
49  * @retval false if VBUS is not at that level voltage
50  */
usbc_vbus_check_level(const struct device * dev,enum tc_vbus_level level)51 static inline bool usbc_vbus_check_level(const struct device *dev, enum tc_vbus_level level)
52 {
53 	const struct usbc_vbus_driver_api *api = (const struct usbc_vbus_driver_api *)dev->api;
54 
55 	return api->check_level(dev, level);
56 }
57 
58 /**
59  * @brief Reads and returns VBUS measured in mV
60  *
61  * @param dev        Runtime device structure
62  * @param meas       pointer where the measured VBUS voltage is stored
63  *
64  * @retval 0 on success
65  * @retval -EIO on failure
66  */
usbc_vbus_measure(const struct device * dev,int * meas)67 static inline int usbc_vbus_measure(const struct device *dev, int *meas)
68 {
69 	const struct usbc_vbus_driver_api *api = (const struct usbc_vbus_driver_api *)dev->api;
70 
71 	return api->measure(dev, meas);
72 }
73 
74 /**
75  * @brief Controls a pin that discharges VBUS
76  *
77  * @param dev        Runtime device structure
78  * @param enable     Discharge VBUS when true
79  *
80  * @retval 0 on success
81  * @retval -EIO on failure
82  * @retval -ENOENT if discharge pin isn't defined
83  */
usbc_vbus_discharge(const struct device * dev,bool enable)84 static inline int usbc_vbus_discharge(const struct device *dev, bool enable)
85 {
86 	const struct usbc_vbus_driver_api *api = (const struct usbc_vbus_driver_api *)dev->api;
87 
88 	return api->discharge(dev, enable);
89 }
90 
91 /**
92  * @brief Controls a pin that enables VBUS measurements
93  *
94  * @param dev     Runtime device structure
95  * @param enable  enable VBUS measurements when true
96  *
97  * @retval 0 on success
98  * @retval -EIO on failure
99  * @retval -ENOENT if enable pin isn't defined
100  */
usbc_vbus_enable(const struct device * dev,bool enable)101 static inline int usbc_vbus_enable(const struct device *dev, bool enable)
102 {
103 	const struct usbc_vbus_driver_api *api = (const struct usbc_vbus_driver_api *)dev->api;
104 
105 	return api->enable(dev, enable);
106 }
107 
108 /**
109  * @}
110  */
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* ZEPHYR_INCLUDE_DRIVERS_USBC_VBUS_H_ */
117