xref: /linux-6.15/include/linux/usb/phy.h (revision 8fdff1dc)
1 /* USB OTG (On The Go) defines */
2 /*
3  *
4  * These APIs may be used between USB controllers.  USB device drivers
5  * (for either host or peripheral roles) don't use these calls; they
6  * continue to use just usb_device and usb_gadget.
7  */
8 
9 #ifndef __LINUX_USB_PHY_H
10 #define __LINUX_USB_PHY_H
11 
12 #include <linux/notifier.h>
13 #include <linux/usb.h>
14 
15 enum usb_phy_events {
16 	USB_EVENT_NONE,         /* no events or cable disconnected */
17 	USB_EVENT_VBUS,         /* vbus valid event */
18 	USB_EVENT_ID,           /* id was grounded */
19 	USB_EVENT_CHARGER,      /* usb dedicated charger */
20 	USB_EVENT_ENUMERATED,   /* gadget driver enumerated */
21 };
22 
23 /* associate a type with PHY */
24 enum usb_phy_type {
25 	USB_PHY_TYPE_UNDEFINED,
26 	USB_PHY_TYPE_USB2,
27 	USB_PHY_TYPE_USB3,
28 };
29 
30 /* OTG defines lots of enumeration states before device reset */
31 enum usb_otg_state {
32 	OTG_STATE_UNDEFINED = 0,
33 
34 	/* single-role peripheral, and dual-role default-b */
35 	OTG_STATE_B_IDLE,
36 	OTG_STATE_B_SRP_INIT,
37 	OTG_STATE_B_PERIPHERAL,
38 
39 	/* extra dual-role default-b states */
40 	OTG_STATE_B_WAIT_ACON,
41 	OTG_STATE_B_HOST,
42 
43 	/* dual-role default-a */
44 	OTG_STATE_A_IDLE,
45 	OTG_STATE_A_WAIT_VRISE,
46 	OTG_STATE_A_WAIT_BCON,
47 	OTG_STATE_A_HOST,
48 	OTG_STATE_A_SUSPEND,
49 	OTG_STATE_A_PERIPHERAL,
50 	OTG_STATE_A_WAIT_VFALL,
51 	OTG_STATE_A_VBUS_ERR,
52 };
53 
54 struct usb_phy;
55 struct usb_otg;
56 
57 /* for transceivers connected thru an ULPI interface, the user must
58  * provide access ops
59  */
60 struct usb_phy_io_ops {
61 	int (*read)(struct usb_phy *x, u32 reg);
62 	int (*write)(struct usb_phy *x, u32 val, u32 reg);
63 };
64 
65 struct usb_phy {
66 	struct device		*dev;
67 	const char		*label;
68 	unsigned int		 flags;
69 
70 	enum usb_phy_type	type;
71 	enum usb_otg_state	state;
72 	enum usb_phy_events	last_event;
73 
74 	struct usb_otg		*otg;
75 
76 	struct device		*io_dev;
77 	struct usb_phy_io_ops	*io_ops;
78 	void __iomem		*io_priv;
79 
80 	/* for notification of usb_phy_events */
81 	struct atomic_notifier_head	notifier;
82 
83 	/* to pass extra port status to the root hub */
84 	u16			port_status;
85 	u16			port_change;
86 
87 	/* to support controllers that have multiple transceivers */
88 	struct list_head	head;
89 
90 	/* initialize/shutdown the OTG controller */
91 	int	(*init)(struct usb_phy *x);
92 	void	(*shutdown)(struct usb_phy *x);
93 
94 	/* effective for B devices, ignored for A-peripheral */
95 	int	(*set_power)(struct usb_phy *x,
96 				unsigned mA);
97 
98 	/* for non-OTG B devices: set transceiver into suspend mode */
99 	int	(*set_suspend)(struct usb_phy *x,
100 				int suspend);
101 
102 	/* notify phy connect status change */
103 	int	(*notify_connect)(struct usb_phy *x,
104 			enum usb_device_speed speed);
105 	int	(*notify_disconnect)(struct usb_phy *x,
106 			enum usb_device_speed speed);
107 };
108 
109 
110 /* for board-specific init logic */
111 extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
112 extern void usb_remove_phy(struct usb_phy *);
113 
114 /* helpers for direct access thru low-level io interface */
115 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
116 {
117 	if (x->io_ops && x->io_ops->read)
118 		return x->io_ops->read(x, reg);
119 
120 	return -EINVAL;
121 }
122 
123 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
124 {
125 	if (x->io_ops && x->io_ops->write)
126 		return x->io_ops->write(x, val, reg);
127 
128 	return -EINVAL;
129 }
130 
131 static inline int
132 usb_phy_init(struct usb_phy *x)
133 {
134 	if (x->init)
135 		return x->init(x);
136 
137 	return 0;
138 }
139 
140 static inline void
141 usb_phy_shutdown(struct usb_phy *x)
142 {
143 	if (x->shutdown)
144 		x->shutdown(x);
145 }
146 
147 /* for usb host and peripheral controller drivers */
148 #ifdef CONFIG_USB_OTG_UTILS
149 extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
150 extern struct usb_phy *devm_usb_get_phy(struct device *dev,
151 	enum usb_phy_type type);
152 extern void usb_put_phy(struct usb_phy *);
153 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
154 #else
155 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
156 {
157 	return NULL;
158 }
159 
160 static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
161 	enum usb_phy_type type)
162 {
163 	return NULL;
164 }
165 
166 static inline void usb_put_phy(struct usb_phy *x)
167 {
168 }
169 
170 static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
171 {
172 }
173 
174 #endif
175 
176 static inline int
177 usb_phy_set_power(struct usb_phy *x, unsigned mA)
178 {
179 	if (x && x->set_power)
180 		return x->set_power(x, mA);
181 	return 0;
182 }
183 
184 /* Context: can sleep */
185 static inline int
186 usb_phy_set_suspend(struct usb_phy *x, int suspend)
187 {
188 	if (x->set_suspend != NULL)
189 		return x->set_suspend(x, suspend);
190 	else
191 		return 0;
192 }
193 
194 static inline int
195 usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
196 {
197 	if (x->notify_connect)
198 		return x->notify_connect(x, speed);
199 	else
200 		return 0;
201 }
202 
203 static inline int
204 usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
205 {
206 	if (x->notify_disconnect)
207 		return x->notify_disconnect(x, speed);
208 	else
209 		return 0;
210 }
211 
212 /* notifiers */
213 static inline int
214 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
215 {
216 	return atomic_notifier_chain_register(&x->notifier, nb);
217 }
218 
219 static inline void
220 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
221 {
222 	atomic_notifier_chain_unregister(&x->notifier, nb);
223 }
224 
225 static inline const char *usb_phy_type_string(enum usb_phy_type type)
226 {
227 	switch (type) {
228 	case USB_PHY_TYPE_USB2:
229 		return "USB2 PHY";
230 	case USB_PHY_TYPE_USB3:
231 		return "USB3 PHY";
232 	default:
233 		return "UNKNOWN PHY TYPE";
234 	}
235 }
236 #endif /* __LINUX_USB_PHY_H */
237