xref: /linux/include/linux/usb/typec_mux.h (revision 6c8c1406)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #ifndef __USB_TYPEC_MUX
4 #define __USB_TYPEC_MUX
5 
6 #include <linux/property.h>
7 #include <linux/usb/typec.h>
8 
9 struct device;
10 struct typec_mux;
11 struct typec_mux_dev;
12 struct typec_switch;
13 struct typec_switch_dev;
14 struct typec_altmode;
15 struct fwnode_handle;
16 
17 typedef int (*typec_switch_set_fn_t)(struct typec_switch_dev *sw,
18 				     enum typec_orientation orientation);
19 
20 struct typec_switch_desc {
21 	struct fwnode_handle *fwnode;
22 	typec_switch_set_fn_t set;
23 	const char *name;
24 	void *drvdata;
25 };
26 
27 struct typec_switch *fwnode_typec_switch_get(struct fwnode_handle *fwnode);
28 void typec_switch_put(struct typec_switch *sw);
29 int typec_switch_set(struct typec_switch *sw,
30 		     enum typec_orientation orientation);
31 
32 static inline struct typec_switch *typec_switch_get(struct device *dev)
33 {
34 	return fwnode_typec_switch_get(dev_fwnode(dev));
35 }
36 
37 struct typec_switch_dev *
38 typec_switch_register(struct device *parent,
39 		      const struct typec_switch_desc *desc);
40 void typec_switch_unregister(struct typec_switch_dev *sw);
41 
42 void typec_switch_set_drvdata(struct typec_switch_dev *sw, void *data);
43 void *typec_switch_get_drvdata(struct typec_switch_dev *sw);
44 
45 struct typec_mux_state {
46 	struct typec_altmode *alt;
47 	unsigned long mode;
48 	void *data;
49 };
50 
51 typedef int (*typec_mux_set_fn_t)(struct typec_mux_dev *mux,
52 				  struct typec_mux_state *state);
53 
54 struct typec_mux_desc {
55 	struct fwnode_handle *fwnode;
56 	typec_mux_set_fn_t set;
57 	const char *name;
58 	void *drvdata;
59 };
60 
61 #if IS_ENABLED(CONFIG_TYPEC)
62 
63 struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode,
64 				       const struct typec_altmode_desc *desc);
65 void typec_mux_put(struct typec_mux *mux);
66 int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state);
67 
68 struct typec_mux_dev *
69 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc);
70 void typec_mux_unregister(struct typec_mux_dev *mux);
71 
72 void typec_mux_set_drvdata(struct typec_mux_dev *mux, void *data);
73 void *typec_mux_get_drvdata(struct typec_mux_dev *mux);
74 
75 #else
76 
77 static inline struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode,
78 				       const struct typec_altmode_desc *desc)
79 {
80 	return NULL;
81 }
82 
83 static inline void typec_mux_put(struct typec_mux *mux) {}
84 
85 static inline int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
86 {
87 	return 0;
88 }
89 
90 static inline struct typec_mux_dev *
91 typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
92 {
93 	return ERR_PTR(-EOPNOTSUPP);
94 }
95 static inline void typec_mux_unregister(struct typec_mux_dev *mux) {}
96 
97 static inline void typec_mux_set_drvdata(struct typec_mux_dev *mux, void *data) {}
98 static inline void *typec_mux_get_drvdata(struct typec_mux_dev *mux)
99 {
100 	return ERR_PTR(-EOPNOTSUPP);
101 }
102 
103 #endif /* CONFIG_TYPEC */
104 
105 static inline struct typec_mux *
106 typec_mux_get(struct device *dev, const struct typec_altmode_desc *desc)
107 {
108 	return fwnode_typec_mux_get(dev_fwnode(dev), desc);
109 }
110 
111 #endif /* __USB_TYPEC_MUX */
112