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