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