xref: /linux-6.15/include/linux/usb/phy.h (revision 9d64fc08)
1 /*
2  * USB PHY defines
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/extcon.h>
13 #include <linux/notifier.h>
14 #include <linux/usb.h>
15 #include <uapi/linux/usb/charger.h>
16 
17 enum usb_phy_interface {
18 	USBPHY_INTERFACE_MODE_UNKNOWN,
19 	USBPHY_INTERFACE_MODE_UTMI,
20 	USBPHY_INTERFACE_MODE_UTMIW,
21 	USBPHY_INTERFACE_MODE_ULPI,
22 	USBPHY_INTERFACE_MODE_SERIAL,
23 	USBPHY_INTERFACE_MODE_HSIC,
24 };
25 
26 enum usb_phy_events {
27 	USB_EVENT_NONE,         /* no events or cable disconnected */
28 	USB_EVENT_VBUS,         /* vbus valid event */
29 	USB_EVENT_ID,           /* id was grounded */
30 	USB_EVENT_CHARGER,      /* usb dedicated charger */
31 	USB_EVENT_ENUMERATED,   /* gadget driver enumerated */
32 };
33 
34 /* associate a type with PHY */
35 enum usb_phy_type {
36 	USB_PHY_TYPE_UNDEFINED,
37 	USB_PHY_TYPE_USB2,
38 	USB_PHY_TYPE_USB3,
39 };
40 
41 /* OTG defines lots of enumeration states before device reset */
42 enum usb_otg_state {
43 	OTG_STATE_UNDEFINED = 0,
44 
45 	/* single-role peripheral, and dual-role default-b */
46 	OTG_STATE_B_IDLE,
47 	OTG_STATE_B_SRP_INIT,
48 	OTG_STATE_B_PERIPHERAL,
49 
50 	/* extra dual-role default-b states */
51 	OTG_STATE_B_WAIT_ACON,
52 	OTG_STATE_B_HOST,
53 
54 	/* dual-role default-a */
55 	OTG_STATE_A_IDLE,
56 	OTG_STATE_A_WAIT_VRISE,
57 	OTG_STATE_A_WAIT_BCON,
58 	OTG_STATE_A_HOST,
59 	OTG_STATE_A_SUSPEND,
60 	OTG_STATE_A_PERIPHERAL,
61 	OTG_STATE_A_WAIT_VFALL,
62 	OTG_STATE_A_VBUS_ERR,
63 };
64 
65 struct usb_phy;
66 struct usb_otg;
67 
68 /* for phys connected thru an ULPI interface, the user must
69  * provide access ops
70  */
71 struct usb_phy_io_ops {
72 	int (*read)(struct usb_phy *x, u32 reg);
73 	int (*write)(struct usb_phy *x, u32 val, u32 reg);
74 };
75 
76 struct usb_charger_current {
77 	unsigned int sdp_min;
78 	unsigned int sdp_max;
79 	unsigned int dcp_min;
80 	unsigned int dcp_max;
81 	unsigned int cdp_min;
82 	unsigned int cdp_max;
83 	unsigned int aca_min;
84 	unsigned int aca_max;
85 };
86 
87 struct usb_phy {
88 	struct device		*dev;
89 	const char		*label;
90 	unsigned int		 flags;
91 
92 	enum usb_phy_type	type;
93 	enum usb_phy_events	last_event;
94 
95 	struct usb_otg		*otg;
96 
97 	struct device		*io_dev;
98 	struct usb_phy_io_ops	*io_ops;
99 	void __iomem		*io_priv;
100 
101 	/* to support extcon device */
102 	struct extcon_dev	*edev;
103 	struct extcon_dev	*id_edev;
104 	struct notifier_block	vbus_nb;
105 	struct notifier_block	id_nb;
106 	struct notifier_block	type_nb;
107 
108 	/* Support USB charger */
109 	enum usb_charger_type	chg_type;
110 	enum usb_charger_state	chg_state;
111 	struct usb_charger_current	chg_cur;
112 	struct work_struct		chg_work;
113 
114 	/* for notification of usb_phy_events */
115 	struct atomic_notifier_head	notifier;
116 
117 	/* to pass extra port status to the root hub */
118 	u16			port_status;
119 	u16			port_change;
120 
121 	/* to support controllers that have multiple phys */
122 	struct list_head	head;
123 
124 	/* initialize/shutdown the phy */
125 	int	(*init)(struct usb_phy *x);
126 	void	(*shutdown)(struct usb_phy *x);
127 
128 	/* enable/disable VBUS */
129 	int	(*set_vbus)(struct usb_phy *x, int on);
130 
131 	/* effective for B devices, ignored for A-peripheral */
132 	int	(*set_power)(struct usb_phy *x,
133 				unsigned mA);
134 
135 	/* Set phy into suspend mode */
136 	int	(*set_suspend)(struct usb_phy *x,
137 				int suspend);
138 
139 	/*
140 	 * Set wakeup enable for PHY, in that case, the PHY can be
141 	 * woken up from suspend status due to external events,
142 	 * like vbus change, dp/dm change and id.
143 	 */
144 	int	(*set_wakeup)(struct usb_phy *x, bool enabled);
145 
146 	/* notify phy connect status change */
147 	int	(*notify_connect)(struct usb_phy *x,
148 			enum usb_device_speed speed);
149 	int	(*notify_disconnect)(struct usb_phy *x,
150 			enum usb_device_speed speed);
151 
152 	/*
153 	 * Charger detection method can be implemented if you need to
154 	 * manually detect the charger type.
155 	 */
156 	enum usb_charger_type (*charger_detect)(struct usb_phy *x);
157 };
158 
159 /**
160  * struct usb_phy_bind - represent the binding for the phy
161  * @dev_name: the device name of the device that will bind to the phy
162  * @phy_dev_name: the device name of the phy
163  * @index: used if a single controller uses multiple phys
164  * @phy: reference to the phy
165  * @list: to maintain a linked list of the binding information
166  */
167 struct usb_phy_bind {
168 	const char	*dev_name;
169 	const char	*phy_dev_name;
170 	u8		index;
171 	struct usb_phy	*phy;
172 	struct list_head list;
173 };
174 
175 /* for board-specific init logic */
176 extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
177 extern int usb_add_phy_dev(struct usb_phy *);
178 extern void usb_remove_phy(struct usb_phy *);
179 
180 /* helpers for direct access thru low-level io interface */
181 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
182 {
183 	if (x && x->io_ops && x->io_ops->read)
184 		return x->io_ops->read(x, reg);
185 
186 	return -EINVAL;
187 }
188 
189 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
190 {
191 	if (x && x->io_ops && x->io_ops->write)
192 		return x->io_ops->write(x, val, reg);
193 
194 	return -EINVAL;
195 }
196 
197 static inline int
198 usb_phy_init(struct usb_phy *x)
199 {
200 	if (x && x->init)
201 		return x->init(x);
202 
203 	return 0;
204 }
205 
206 static inline void
207 usb_phy_shutdown(struct usb_phy *x)
208 {
209 	if (x && x->shutdown)
210 		x->shutdown(x);
211 }
212 
213 static inline int
214 usb_phy_vbus_on(struct usb_phy *x)
215 {
216 	if (!x || !x->set_vbus)
217 		return 0;
218 
219 	return x->set_vbus(x, true);
220 }
221 
222 static inline int
223 usb_phy_vbus_off(struct usb_phy *x)
224 {
225 	if (!x || !x->set_vbus)
226 		return 0;
227 
228 	return x->set_vbus(x, false);
229 }
230 
231 /* for usb host and peripheral controller drivers */
232 #if IS_ENABLED(CONFIG_USB_PHY)
233 extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
234 extern struct usb_phy *devm_usb_get_phy(struct device *dev,
235 	enum usb_phy_type type);
236 extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
237 extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
238 extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
239 	const char *phandle, u8 index);
240 extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
241 	struct device_node *node, struct notifier_block *nb);
242 extern void usb_put_phy(struct usb_phy *);
243 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
244 extern int usb_bind_phy(const char *dev_name, u8 index,
245 				const char *phy_dev_name);
246 extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
247 extern void usb_phy_set_charger_current(struct usb_phy *usb_phy,
248 					unsigned int mA);
249 extern void usb_phy_get_charger_current(struct usb_phy *usb_phy,
250 					unsigned int *min, unsigned int *max);
251 extern void usb_phy_set_charger_state(struct usb_phy *usb_phy,
252 				      enum usb_charger_state state);
253 #else
254 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
255 {
256 	return ERR_PTR(-ENXIO);
257 }
258 
259 static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
260 	enum usb_phy_type type)
261 {
262 	return ERR_PTR(-ENXIO);
263 }
264 
265 static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
266 {
267 	return ERR_PTR(-ENXIO);
268 }
269 
270 static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
271 {
272 	return ERR_PTR(-ENXIO);
273 }
274 
275 static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
276 	const char *phandle, u8 index)
277 {
278 	return ERR_PTR(-ENXIO);
279 }
280 
281 static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
282 	struct device_node *node, struct notifier_block *nb)
283 {
284 	return ERR_PTR(-ENXIO);
285 }
286 
287 static inline void usb_put_phy(struct usb_phy *x)
288 {
289 }
290 
291 static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
292 {
293 }
294 
295 static inline int usb_bind_phy(const char *dev_name, u8 index,
296 				const char *phy_dev_name)
297 {
298 	return -EOPNOTSUPP;
299 }
300 
301 static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
302 {
303 }
304 
305 static inline void usb_phy_set_charger_current(struct usb_phy *usb_phy,
306 					       unsigned int mA)
307 {
308 }
309 
310 static inline void usb_phy_get_charger_current(struct usb_phy *usb_phy,
311 					       unsigned int *min,
312 					       unsigned int *max)
313 {
314 }
315 
316 static inline void usb_phy_set_charger_state(struct usb_phy *usb_phy,
317 					     enum usb_charger_state state)
318 {
319 }
320 #endif
321 
322 static inline int
323 usb_phy_set_power(struct usb_phy *x, unsigned mA)
324 {
325 	if (!x)
326 		return 0;
327 
328 	usb_phy_set_charger_current(x, mA);
329 
330 	if (x->set_power)
331 		return x->set_power(x, mA);
332 	return 0;
333 }
334 
335 /* Context: can sleep */
336 static inline int
337 usb_phy_set_suspend(struct usb_phy *x, int suspend)
338 {
339 	if (x && x->set_suspend != NULL)
340 		return x->set_suspend(x, suspend);
341 	else
342 		return 0;
343 }
344 
345 static inline int
346 usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
347 {
348 	if (x && x->set_wakeup)
349 		return x->set_wakeup(x, enabled);
350 	else
351 		return 0;
352 }
353 
354 static inline int
355 usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
356 {
357 	if (x && x->notify_connect)
358 		return x->notify_connect(x, speed);
359 	else
360 		return 0;
361 }
362 
363 static inline int
364 usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
365 {
366 	if (x && x->notify_disconnect)
367 		return x->notify_disconnect(x, speed);
368 	else
369 		return 0;
370 }
371 
372 /* notifiers */
373 static inline int
374 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
375 {
376 	return atomic_notifier_chain_register(&x->notifier, nb);
377 }
378 
379 static inline void
380 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
381 {
382 	atomic_notifier_chain_unregister(&x->notifier, nb);
383 }
384 
385 static inline const char *usb_phy_type_string(enum usb_phy_type type)
386 {
387 	switch (type) {
388 	case USB_PHY_TYPE_USB2:
389 		return "USB2 PHY";
390 	case USB_PHY_TYPE_USB3:
391 		return "USB3 PHY";
392 	default:
393 		return "UNKNOWN PHY TYPE";
394 	}
395 }
396 #endif /* __LINUX_USB_PHY_H */
397