1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include "soc/soc_caps.h"
12 #include "hal/usb_phy_types.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * @brief USB PHY status
20  */
21 typedef enum {
22     USB_PHY_STATUS_FREE,             /**< PHY is not being used */
23     USB_PHY_STATUS_IN_USE,           /**< PHY is in use */
24 } usb_phy_status_t;
25 
26 /**
27  * @brief USB PHY available actions
28  */
29 typedef enum {
30     USB_PHY_ACTION_HOST_ALLOW_CONN,             /**< Enable physical connection when operating as an OTG Host */
31     USB_PHY_ACTION_HOST_FORCE_DISCONN,          /**< Disable physical connection when operating as an OTG Host */
32     USB_PHY_ACTION_MAX,
33 } usb_phy_action_t;
34 
35 /**
36  * @brief USB external PHY iopins configure struct
37  */
38 typedef struct {
39     int vp_io_num;             /**< GPIO pin to USB_EXTPHY_VP_IDX */
40     int vm_io_num;             /**< GPIO pin to USB_EXTPHY_VM_IDX */
41     int rcv_io_num;            /**< GPIO pin to USB_EXTPHY_RCV_IDX */
42     int oen_io_num;            /**< GPIO pin to USB_EXTPHY_OEN_IDX */
43     int vpo_io_num;            /**< GPIO pin to USB_EXTPHY_VPO_IDX */
44     int vmo_io_num;            /**< GPIO pin to USB_EXTPHY_VMO_IDX */
45 } usb_phy_gpio_conf_t;
46 
47 /**
48  * @brief USB PHY configure struct
49  *
50  * At minimum the PHY controller and PHY target must be initialized.
51  */
52 typedef struct {
53     usb_phy_controller_t controller;    /**< USB PHY controller */
54     usb_phy_target_t target;            /**< USB PHY target INT/EXT */
55     usb_otg_mode_t otg_mode;            /**< USB OTG mode */
56     usb_phy_speed_t otg_speed;          /**< USB OTG speed */
57     usb_phy_gpio_conf_t *gpio_conf;     /**< USB external PHY iopins configure */
58 } usb_phy_config_t;
59 
60 typedef struct phy_context_t *usb_phy_handle_t;    /**< USB PHY context handle */
61 
62 /**
63  * @brief Initialize a new USB PHY
64  *        Configure at least PHY source.
65  *
66  * This function will enable the OTG Controller
67  *
68  * @param[in] config USB PHY configurtion struct
69  * @param[out] handle_ret USB PHY context handle
70  *
71  * @return
72  *     - ESP_OK Success
73  *     - ESP_FAIL USB PHY init error.
74  *     - ESP_ERR_INVALID_STATE USB PHY not installed.
75  *     - ESP_ERR_NO_MEM USB_OTG installation failed due to no mem.
76  */
77 esp_err_t usb_new_phy(const usb_phy_config_t *config, usb_phy_handle_t *handle_ret);
78 
79 /**
80  * @brief Configure OTG mode for a USB PHY
81  *
82  * @param handle Pointer of USB PHY context handle
83  * @param mode USB OTG mode
84  *
85  * @return
86  *     - ESP_OK Success
87  *     - ESP_ERR_INVALID_ARG Parameter error.
88  *     - ESP_FAIL OTG set mode fail.
89  */
90 esp_err_t usb_phy_otg_set_mode(usb_phy_handle_t handle, usb_otg_mode_t mode);
91 
92 /**
93  * @brief Configure USB speed for a USB PHY that is operating as an OTG Device
94  *
95  * @param handle Pointer of USB PHY context handle
96  * @param mode USB speed
97  *
98  * @return
99  *     - ESP_OK Success
100  *     - ESP_ERR_INVALID_ARG Parameter error.
101  *     - ESP_FAIL OTG set speed fail.
102  */
103 esp_err_t usb_phy_otg_dev_set_speed(usb_phy_handle_t handle, usb_phy_speed_t speed);
104 
105 /**
106  * @brief Take a action for a USB PHY
107  *
108  * @param handle Pointer of USB PHY context handle
109  * @param action USB PHY action
110  *
111  * @return
112  *     - ESP_OK Success
113  *     - ESP_ERR_INVALID_ARG Parameter error.
114  *     - ESP_FAIL Action cannot be performed.
115  */
116 esp_err_t usb_phy_action(usb_phy_handle_t handle, usb_phy_action_t action);
117 
118 /**
119  * @brief Delete a USB PHY
120  *
121  * @param handle Pointer of USB PHY context handle
122  *
123  * @return
124  *     - ESP_OK Success
125  *     - ESP_ERR_INVALID_ARG Parameter error.
126  */
127 esp_err_t usb_del_phy(usb_phy_handle_t handle);
128 
129 /**
130  * @brief Get status of a USB PHY
131  *
132  * @param[in] target The specific PHY target to check
133  * @param[out] status Status of the PHY
134  *
135  * @return
136  *     - ESP_OK Success
137  *     - ESP_ERR_INVALID_ARG Parameter error.
138  *     - ESP_ERR_INVALID_STATE USB PHY not installed.
139  */
140 esp_err_t usb_phy_get_phy_status(usb_phy_target_t target, usb_phy_status_t *status);
141 
142 #ifdef __cplusplus
143 }
144 #endif
145