1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Driver for TI TPS6598x USB Power Delivery controller family
4  *
5  * Copyright (C) 2020 Purism SPC
6  * Author: Guido Günther <agx@sigxcpu.org>
7  */
8 
9 #undef TRACE_SYSTEM
10 #define TRACE_SYSTEM tps6598x
11 
12 #if !defined(_TPS6598X_TRACE_H_) || defined(TRACE_HEADER_MULTI_READ)
13 #define _TPS6598X_TRACE_H_
14 
15 #include "tps6598x.h"
16 
17 #include <linux/stringify.h>
18 #include <linux/types.h>
19 #include <linux/tracepoint.h>
20 
21 #define show_irq_flags(flags) \
22 	__print_flags_u64(flags, "|", \
23 		{ TPS_REG_INT_PD_SOFT_RESET,			"PD_SOFT_RESET" }, \
24 		{ TPS_REG_INT_HARD_RESET,			"HARD_RESET" }, \
25 		{ TPS_REG_INT_PLUG_EVENT,			"PLUG_EVENT" }, \
26 		{ TPS_REG_INT_PR_SWAP_COMPLETE,			"PR_SWAP_COMPLETE" }, \
27 		{ TPS_REG_INT_DR_SWAP_COMPLETE,			"DR_SWAP_COMPLETE" }, \
28 		{ TPS_REG_INT_RDO_RECEIVED_FROM_SINK,		"RDO_RECEIVED_FROM_SINK" }, \
29 		{ TPS_REG_INT_BIST,				"BIST" }, \
30 		{ TPS_REG_INT_OVERCURRENT,			"OVERCURRENT" }, \
31 		{ TPS_REG_INT_ATTENTION_RECEIVED,		"ATTENTION_RECEIVED" }, \
32 		{ TPS_REG_INT_VDM_RECEIVED,			"VDM_RECEIVED" }, \
33 		{ TPS_REG_INT_NEW_CONTRACT_AS_CONSUMER,		"NEW_CONTRACT_AS_CONSUMER" }, \
34 		{ TPS_REG_INT_NEW_CONTRACT_AS_PROVIDER,		"NEW_CONTRACT_AS_PROVIDER" }, \
35 		{ TPS_REG_INT_SOURCE_CAP_MESSAGE_READY,		"SOURCE_CAP_MESSAGE_READY" }, \
36 		{ TPS_REG_INT_SINK_CAP_MESSAGE_READY,		"SINK_CAP_MESSAGE_READY" }, \
37 		{ TPS_REG_INT_PR_SWAP_REQUESTED,		"PR_SWAP_REQUESTED" }, \
38 		{ TPS_REG_INT_GOTO_MIN_RECEIVED,		"GOTO_MIN_RECEIVED" }, \
39 		{ TPS_REG_INT_USB_HOST_PRESENT,			"USB_HOST_PRESENT" }, \
40 		{ TPS_REG_INT_USB_HOST_PRESENT_NO_LONGER,	"USB_HOST_PRESENT_NO_LONGER" }, \
41 		{ TPS_REG_INT_HIGH_VOLTAGE_WARNING,		"HIGH_VOLTAGE_WARNING" }, \
42 		{ TPS_REG_INT_PP_SWITCH_CHANGED,		"PP_SWITCH_CHANGED" }, \
43 		{ TPS_REG_INT_POWER_STATUS_UPDATE,		"POWER_STATUS_UPDATE" }, \
44 		{ TPS_REG_INT_DATA_STATUS_UPDATE,		"DATA_STATUS_UPDATE" }, \
45 		{ TPS_REG_INT_STATUS_UPDATE,			"STATUS_UPDATE" }, \
46 		{ TPS_REG_INT_PD_STATUS_UPDATE,			"PD_STATUS_UPDATE" }, \
47 		{ TPS_REG_INT_ADC_LOW_THRESHOLD,		"ADC_LOW_THRESHOLD" }, \
48 		{ TPS_REG_INT_ADC_HIGH_THRESHOLD,		"ADC_HIGH_THRESHOLD" }, \
49 		{ TPS_REG_INT_CMD1_COMPLETE,			"CMD1_COMPLETE" }, \
50 		{ TPS_REG_INT_CMD2_COMPLETE,			"CMD2_COMPLETE" }, \
51 		{ TPS_REG_INT_ERROR_DEVICE_INCOMPATIBLE,	"ERROR_DEVICE_INCOMPATIBLE" }, \
52 		{ TPS_REG_INT_ERROR_CANNOT_PROVIDE_PWR,		"ERROR_CANNOT_PROVIDE_PWR" }, \
53 		{ TPS_REG_INT_ERROR_CAN_PROVIDE_PWR_LATER,	"ERROR_CAN_PROVIDE_PWR_LATER" }, \
54 		{ TPS_REG_INT_ERROR_POWER_EVENT_OCCURRED,	"ERROR_POWER_EVENT_OCCURRED" }, \
55 		{ TPS_REG_INT_ERROR_MISSING_GET_CAP_MESSAGE,	"ERROR_MISSING_GET_CAP_MESSAGE" }, \
56 		{ TPS_REG_INT_ERROR_PROTOCOL_ERROR,		"ERROR_PROTOCOL_ERROR" }, \
57 		{ TPS_REG_INT_ERROR_MESSAGE_DATA,		"ERROR_MESSAGE_DATA" }, \
58 		{ TPS_REG_INT_ERROR_DISCHARGE_FAILED,		"ERROR_DISCHARGE_FAILED" }, \
59 		{ TPS_REG_INT_SRC_TRANSITION,			"SRC_TRANSITION" }, \
60 		{ TPS_REG_INT_ERROR_UNABLE_TO_SOURCE,		"ERROR_UNABLE_TO_SOURCE" }, \
61 		{ TPS_REG_INT_VDM_ENTERED_MODE,			"VDM_ENTERED_MODE" }, \
62 		{ TPS_REG_INT_VDM_MSG_SENT,			"VDM_MSG_SENT" }, \
63 		{ TPS_REG_INT_DISCOVER_MODES_COMPLETE,		"DISCOVER_MODES_COMPLETE" }, \
64 		{ TPS_REG_INT_EXIT_MODES_COMPLETE,		"EXIT_MODES_COMPLETE" }, \
65 		{ TPS_REG_INT_USER_VID_ALT_MODE_ENTERED,	"USER_VID_ALT_MODE_ENTERED" }, \
66 		{ TPS_REG_INT_USER_VID_ALT_MODE_EXIT,		"USER_VID_ALT_MODE_EXIT" }, \
67 		{ TPS_REG_INT_USER_VID_ALT_MODE_ATTN_VDM,	"USER_VID_ALT_MODE_ATTN_VDM" }, \
68 		{ TPS_REG_INT_USER_VID_ALT_MODE_OTHER_VDM,	"USER_VID_ALT_MODE_OTHER_VDM" })
69 
70 #define show_cd321x_irq_flags(flags) \
71 	__print_flags_u64(flags, "|", \
72 		{ APPLE_CD_REG_INT_PLUG_EVENT,			"PLUG_EVENT" }, \
73 		{ APPLE_CD_REG_INT_POWER_STATUS_UPDATE,		"POWER_STATUS_UPDATE" }, \
74 		{ APPLE_CD_REG_INT_DATA_STATUS_UPDATE,		"DATA_STATUS_UPDATE" }, \
75 		{ APPLE_CD_REG_INT_STATUS_UPDATE,		"STATUS_UPDATE" })
76 
77 #define TPS6598X_STATUS_FLAGS_MASK (GENMASK(31, 0) ^ (TPS_STATUS_CONN_STATE_MASK | \
78 						      TPS_STATUS_PP_5V0_SWITCH_MASK | \
79 						      TPS_STATUS_PP_HV_SWITCH_MASK | \
80 						      TPS_STATUS_PP_EXT_SWITCH_MASK | \
81 						      TPS_STATUS_PP_CABLE_SWITCH_MASK | \
82 						      TPS_STATUS_POWER_SOURCE_MASK | \
83 						      TPS_STATUS_VBUS_STATUS_MASK | \
84 						      TPS_STATUS_USB_HOST_PRESENT_MASK | \
85 						      TPS_STATUS_LEGACY_MASK))
86 
87 #define show_status_conn_state(status) \
88 	__print_symbolic(TPS_STATUS_CONN_STATE((status)), \
89 		{ TPS_STATUS_CONN_STATE_CONN_WITH_R_A,	"conn-Ra"  }, \
90 		{ TPS_STATUS_CONN_STATE_CONN_NO_R_A,	"conn-no-Ra" }, \
91 		{ TPS_STATUS_CONN_STATE_NO_CONN_R_A,	"no-conn-Ra" },	\
92 		{ TPS_STATUS_CONN_STATE_DEBUG_CONN,	"debug"	 }, \
93 		{ TPS_STATUS_CONN_STATE_AUDIO_CONN,	"audio"	 }, \
94 		{ TPS_STATUS_CONN_STATE_DISABLED,	"disabled" }, \
95 		{ TPS_STATUS_CONN_STATE_NO_CONN,	"no-conn" })
96 
97 #define show_status_pp_switch_state(status) \
98 	__print_symbolic(status, \
99 		{ TPS_STATUS_PP_SWITCH_STATE_IN,	"in" }, \
100 		{ TPS_STATUS_PP_SWITCH_STATE_OUT,	"out" }, \
101 		{ TPS_STATUS_PP_SWITCH_STATE_FAULT,	"fault" }, \
102 		{ TPS_STATUS_PP_SWITCH_STATE_DISABLED,	"off" })
103 
104 #define show_status_power_sources(status) \
105 	__print_symbolic(TPS_STATUS_POWER_SOURCE(status), \
106 		{ TPS_STATUS_POWER_SOURCE_VBUS,		"vbus" }, \
107 		{ TPS_STATUS_POWER_SOURCE_VIN_3P3,	"vin-3p3" }, \
108 		{ TPS_STATUS_POWER_SOURCE_DEAD_BAT,	"dead-battery" }, \
109 		{ TPS_STATUS_POWER_SOURCE_UNKNOWN,	"unknown" })
110 
111 #define show_status_vbus_status(status) \
112 	__print_symbolic(TPS_STATUS_VBUS_STATUS(status), \
113 		{ TPS_STATUS_VBUS_STATUS_VSAFE0V,	"vSafe0V" }, \
114 		{ TPS_STATUS_VBUS_STATUS_VSAFE5V,	"vSafe5V" }, \
115 		{ TPS_STATUS_VBUS_STATUS_PD,		"pd" }, \
116 		{ TPS_STATUS_VBUS_STATUS_FAULT,		"fault" })
117 
118 #define show_status_usb_host_present(status) \
119 	__print_symbolic(TPS_STATUS_USB_HOST_PRESENT(status), \
120 		{ TPS_STATUS_USB_HOST_PRESENT_PD_USB,	 "pd-usb" }, \
121 		{ TPS_STATUS_USB_HOST_PRESENT_NO_PD,	 "no-pd" }, \
122 		{ TPS_STATUS_USB_HOST_PRESENT_PD_NO_USB, "pd-no-usb" }, \
123 		{ TPS_STATUS_USB_HOST_PRESENT_NO,	 "no" })
124 
125 #define show_status_legacy(status) \
126 	__print_symbolic(TPS_STATUS_LEGACY(status),	     \
127 		{ TPS_STATUS_LEGACY_SOURCE,		 "source" }, \
128 		{ TPS_STATUS_LEGACY_SINK,		 "sink" }, \
129 		{ TPS_STATUS_LEGACY_NO,			 "no" })
130 
131 #define show_status_flags(flags) \
132 	__print_flags((flags & TPS6598X_STATUS_FLAGS_MASK), "|", \
133 		      { TPS_STATUS_PLUG_PRESENT,	"PLUG_PRESENT" }, \
134 		      { TPS_STATUS_PLUG_UPSIDE_DOWN,	"UPSIDE_DOWN" }, \
135 		      { TPS_STATUS_PORTROLE,		"PORTROLE" }, \
136 		      { TPS_STATUS_DATAROLE,		"DATAROLE" }, \
137 		      { TPS_STATUS_VCONN,		"VCONN" }, \
138 		      { TPS_STATUS_OVERCURRENT,		"OVERCURRENT" }, \
139 		      { TPS_STATUS_GOTO_MIN_ACTIVE,	"GOTO_MIN_ACTIVE" }, \
140 		      { TPS_STATUS_BIST,		"BIST" }, \
141 		      { TPS_STATUS_HIGH_VOLAGE_WARNING,	"HIGH_VOLAGE_WARNING" }, \
142 		      { TPS_STATUS_HIGH_LOW_VOLTAGE_WARNING, "HIGH_LOW_VOLTAGE_WARNING" })
143 
144 #define show_power_status_source_sink(power_status) \
145 	__print_symbolic(TPS_POWER_STATUS_SOURCESINK(power_status), \
146 		{ 1, "sink" }, \
147 		{ 0, "source" })
148 
149 #define show_power_status_typec_status(power_status) \
150 	__print_symbolic(TPS_POWER_STATUS_PWROPMODE(power_status), \
151 		{ TPS_POWER_STATUS_TYPEC_CURRENT_PD,  "pd" }, \
152 		{ TPS_POWER_STATUS_TYPEC_CURRENT_3A0, "3.0A" }, \
153 		{ TPS_POWER_STATUS_TYPEC_CURRENT_1A5, "1.5A" }, \
154 		{ TPS_POWER_STATUS_TYPEC_CURRENT_USB, "usb" })
155 
156 #define show_power_status_bc12_status(power_status) \
157 	__print_symbolic(TPS_POWER_STATUS_BC12_STATUS(power_status), \
158 		{ TPS_POWER_STATUS_BC12_STATUS_DCP, "dcp" }, \
159 		{ TPS_POWER_STATUS_BC12_STATUS_CDP, "cdp" }, \
160 		{ TPS_POWER_STATUS_BC12_STATUS_SDP, "sdp" })
161 
162 #define TPS_DATA_STATUS_FLAGS_MASK (GENMASK(31, 0) ^ (TPS_DATA_STATUS_DP_PIN_ASSIGNMENT_MASK | \
163 						      TPS_DATA_STATUS_TBT_CABLE_SPEED_MASK | \
164 						      TPS_DATA_STATUS_TBT_CABLE_GEN_MASK))
165 
166 #define show_data_status_flags(data_status) \
167 	__print_flags(data_status & TPS_DATA_STATUS_FLAGS_MASK, "|", \
168 		{ TPS_DATA_STATUS_DATA_CONNECTION,	"DATA_CONNECTION" }, \
169 		{ TPS_DATA_STATUS_UPSIDE_DOWN,		"DATA_UPSIDE_DOWN" }, \
170 		{ TPS_DATA_STATUS_ACTIVE_CABLE,		"ACTIVE_CABLE" }, \
171 		{ TPS_DATA_STATUS_USB2_CONNECTION,	"USB2_CONNECTION" }, \
172 		{ TPS_DATA_STATUS_USB3_CONNECTION,	"USB3_CONNECTION" }, \
173 		{ TPS_DATA_STATUS_USB3_GEN2,		"USB3_GEN2" }, \
174 		{ TPS_DATA_STATUS_USB_DATA_ROLE,	"USB_DATA_ROLE" }, \
175 		{ TPS_DATA_STATUS_DP_CONNECTION,	"DP_CONNECTION" }, \
176 		{ TPS_DATA_STATUS_DP_SINK,		"DP_SINK" }, \
177 		{ TPS_DATA_STATUS_TBT_CONNECTION,	"TBT_CONNECTION" }, \
178 		{ TPS_DATA_STATUS_TBT_TYPE,		"TBT_TYPE" }, \
179 		{ TPS_DATA_STATUS_OPTICAL_CABLE,	"OPTICAL_CABLE" }, \
180 		{ TPS_DATA_STATUS_ACTIVE_LINK_TRAIN,	"ACTIVE_LINK_TRAIN" }, \
181 		{ TPS_DATA_STATUS_FORCE_LSX,		"FORCE_LSX" }, \
182 		{ TPS_DATA_STATUS_POWER_MISMATCH,	"POWER_MISMATCH" })
183 
184 #define show_data_status_dp_pin_assignment(data_status) \
185 	__print_symbolic(TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT(data_status), \
186 		{ TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_E, "E" }, \
187 		{ TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_F, "F" }, \
188 		{ TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_C, "C" }, \
189 		{ TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_D, "D" }, \
190 		{ TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_A, "A" }, \
191 		{ TPS_DATA_STATUS_DP_SPEC_PIN_ASSIGNMENT_B, "B" })
192 
193 #define maybe_show_data_status_dp_pin_assignment(data_status) \
194 	(data_status & TPS_DATA_STATUS_DP_CONNECTION ? \
195 	 show_data_status_dp_pin_assignment(data_status) : "")
196 
197 TRACE_EVENT(tps6598x_irq,
198 	    TP_PROTO(u64 event1,
199 		     u64 event2),
200 	    TP_ARGS(event1, event2),
201 
202 	    TP_STRUCT__entry(
203 			     __field(u64, event1)
204 			     __field(u64, event2)
205 			     ),
206 
207 	    TP_fast_assign(
208 			   __entry->event1 = event1;
209 			   __entry->event2 = event2;
210 			   ),
211 
212 	    TP_printk("event1=%s, event2=%s",
213 		      show_irq_flags(__entry->event1),
214 		      show_irq_flags(__entry->event2))
215 );
216 
217 TRACE_EVENT(cd321x_irq,
218 	    TP_PROTO(u64 event),
219 	    TP_ARGS(event),
220 
221 	    TP_STRUCT__entry(
222 			     __field(u64, event)
223 			     ),
224 
225 	    TP_fast_assign(
226 			   __entry->event = event;
227 			   ),
228 
229 	    TP_printk("event=%s",
230 		      show_cd321x_irq_flags(__entry->event))
231 );
232 
233 TRACE_EVENT(tps6598x_status,
234 	    TP_PROTO(u32 status),
235 	    TP_ARGS(status),
236 
237 	    TP_STRUCT__entry(
238 			     __field(u32, status)
239 			     ),
240 
241 	    TP_fast_assign(
242 			   __entry->status = status;
243 			   ),
244 
245 	    TP_printk("conn: %s, pp_5v0: %s, pp_hv: %s, pp_ext: %s, pp_cable: %s, "
246 		      "pwr-src: %s, vbus: %s, usb-host: %s, legacy: %s, flags: %s",
247 		      show_status_conn_state(__entry->status),
248 		      show_status_pp_switch_state(TPS_STATUS_PP_5V0_SWITCH(__entry->status)),
249 		      show_status_pp_switch_state(TPS_STATUS_PP_HV_SWITCH(__entry->status)),
250 		      show_status_pp_switch_state(TPS_STATUS_PP_EXT_SWITCH(__entry->status)),
251 		      show_status_pp_switch_state(TPS_STATUS_PP_CABLE_SWITCH(__entry->status)),
252 		      show_status_power_sources(__entry->status),
253 		      show_status_vbus_status(__entry->status),
254 		      show_status_usb_host_present(__entry->status),
255 		      show_status_legacy(__entry->status),
256 		      show_status_flags(__entry->status)
257 		    )
258 );
259 
260 TRACE_EVENT(tps6598x_power_status,
261 	    TP_PROTO(u16 power_status),
262 	    TP_ARGS(power_status),
263 
264 	    TP_STRUCT__entry(
265 			     __field(u16, power_status)
266 			     ),
267 
268 	    TP_fast_assign(
269 			   __entry->power_status = power_status;
270 			   ),
271 
272 	    TP_printk("conn: %d, pwr-role: %s, typec: %s, bc: %s",
273 		      !!TPS_POWER_STATUS_CONNECTION(__entry->power_status),
274 		      show_power_status_source_sink(__entry->power_status),
275 		      show_power_status_typec_status(__entry->power_status),
276 		      show_power_status_bc12_status(__entry->power_status)
277 		    )
278 );
279 
280 TRACE_EVENT(tps6598x_data_status,
281 	    TP_PROTO(u32 data_status),
282 	    TP_ARGS(data_status),
283 
284 	    TP_STRUCT__entry(
285 			     __field(u32, data_status)
286 			     ),
287 
288 	    TP_fast_assign(
289 			   __entry->data_status = data_status;
290 			   ),
291 
292 	    TP_printk("%s%s%s",
293 		      show_data_status_flags(__entry->data_status),
294 		      __entry->data_status & TPS_DATA_STATUS_DP_CONNECTION ? ", DP pinout " : "",
295 		      maybe_show_data_status_dp_pin_assignment(__entry->data_status)
296 		    )
297 );
298 
299 #endif /* _TPS6598X_TRACE_H_ */
300 
301 /* This part must be outside protection */
302 #undef TRACE_INCLUDE_FILE
303 #define TRACE_INCLUDE_FILE trace
304 #undef TRACE_INCLUDE_PATH
305 #define TRACE_INCLUDE_PATH .
306 #include <trace/define_trace.h>
307