1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for Intel PMC USB mux control
4  *
5  * Copyright (C) 2020 Intel Corporation
6  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7  */
8 
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
13 #include <linux/usb/pd.h>
14 #include <linux/usb/role.h>
15 #include <linux/usb/typec_mux.h>
16 #include <linux/usb/typec_dp.h>
17 #include <linux/usb/typec_tbt.h>
18 
19 #include <asm/intel_scu_ipc.h>
20 
21 #define PMC_USBC_CMD		0xa7
22 
23 /* Response status bits */
24 #define PMC_USB_RESP_STATUS_FAILURE	BIT(0)
25 #define PMC_USB_RESP_STATUS_FATAL	BIT(1)
26 
27 /* "Usage" OOB Message field values */
28 enum {
29 	PMC_USB_CONNECT,
30 	PMC_USB_DISCONNECT,
31 	PMC_USB_SAFE_MODE,
32 	PMC_USB_ALT_MODE,
33 	PMC_USB_DP_HPD,
34 };
35 
36 #define PMC_USB_MSG_USB2_PORT_SHIFT	0
37 #define PMC_USB_MSG_USB3_PORT_SHIFT	4
38 #define PMC_USB_MSG_UFP_SHIFT		4
39 #define PMC_USB_MSG_ORI_HSL_SHIFT	5
40 #define PMC_USB_MSG_ORI_AUX_SHIFT	6
41 
42 /* Alt Mode Request */
43 struct altmode_req {
44 	u8 usage;
45 	u8 mode_type;
46 	u8 mode_id;
47 	u8 reserved;
48 	u32 mode_data;
49 } __packed;
50 
51 #define PMC_USB_MODE_TYPE_SHIFT		4
52 
53 enum {
54 	PMC_USB_MODE_TYPE_USB,
55 	PMC_USB_MODE_TYPE_DP,
56 	PMC_USB_MODE_TYPE_TBT,
57 };
58 
59 /* Common Mode Data bits */
60 #define PMC_USB_ALTMODE_ACTIVE_CABLE	BIT(2)
61 
62 #define PMC_USB_ALTMODE_ORI_SHIFT	1
63 #define PMC_USB_ALTMODE_UFP_SHIFT	3
64 
65 /* DP specific Mode Data bits */
66 #define PMC_USB_ALTMODE_DP_MODE_SHIFT	8
67 
68 /* TBT specific Mode Data bits */
69 #define PMC_USB_ALTMODE_TBT_TYPE	BIT(17)
70 #define PMC_USB_ALTMODE_CABLE_TYPE	BIT(18)
71 #define PMC_USB_ALTMODE_ACTIVE_LINK	BIT(20)
72 #define PMC_USB_ALTMODE_FORCE_LSR	BIT(23)
73 #define PMC_USB_ALTMODE_CABLE_SPD(_s_)	(((_s_) & GENMASK(2, 0)) << 25)
74 #define   PMC_USB_ALTMODE_CABLE_USB31	1
75 #define   PMC_USB_ALTMODE_CABLE_10GPS	2
76 #define   PMC_USB_ALTMODE_CABLE_20GPS	3
77 #define PMC_USB_ALTMODE_TBT_GEN(_g_)	(((_g_) & GENMASK(1, 0)) << 28)
78 
79 /* Display HPD Request bits */
80 #define PMC_USB_DP_HPD_LVL		BIT(4)
81 #define PMC_USB_DP_HPD_IRQ		BIT(5)
82 
83 /*
84  * Input Output Manager (IOM) PORT STATUS
85  */
86 #define IOM_PORT_STATUS_ACTIVITY_TYPE_MASK		GENMASK(9, 6)
87 #define IOM_PORT_STATUS_ACTIVITY_TYPE_SHIFT		6
88 #define IOM_PORT_STATUS_ACTIVITY_TYPE_USB		0x03
89 /* activity type: Safe Mode */
90 #define IOM_PORT_STATUS_ACTIVITY_TYPE_SAFE_MODE		0x04
91 /* activity type: Display Port */
92 #define IOM_PORT_STATUS_ACTIVITY_TYPE_DP		0x05
93 /* activity type: Display Port Multi Function Device */
94 #define IOM_PORT_STATUS_ACTIVITY_TYPE_DP_MFD		0x06
95 /* activity type: Thunderbolt */
96 #define IOM_PORT_STATUS_ACTIVITY_TYPE_TBT		0x07
97 #define IOM_PORT_STATUS_ACTIVITY_TYPE_ALT_MODE_USB	0x0c
98 #define IOM_PORT_STATUS_ACTIVITY_TYPE_ALT_MODE_TBT_USB	0x0d
99 /* Upstream Facing Port Information */
100 #define IOM_PORT_STATUS_UFP				BIT(10)
101 /* Display Port Hot Plug Detect status */
102 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_MASK		GENMASK(13, 12)
103 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_SHIFT		12
104 #define IOM_PORT_STATUS_DHPD_HPD_STATUS_ASSERT		0x01
105 #define IOM_PORT_STATUS_DHPD_HPD_SOURCE_TBT		BIT(14)
106 #define IOM_PORT_STATUS_CONNECTED			BIT(31)
107 
108 #define IOM_PORT_ACTIVITY_IS(_status_, _type_)				\
109 	((((_status_) & IOM_PORT_STATUS_ACTIVITY_TYPE_MASK) >>		\
110 	  IOM_PORT_STATUS_ACTIVITY_TYPE_SHIFT) ==			\
111 	 (IOM_PORT_STATUS_ACTIVITY_TYPE_##_type_))
112 
113 #define IOM_PORT_HPD_ASSERTED(_status_)					\
114 	((((_status_) & IOM_PORT_STATUS_DHPD_HPD_STATUS_MASK) >>	\
115 	  IOM_PORT_STATUS_DHPD_HPD_STATUS_SHIFT) &			\
116 	 IOM_PORT_STATUS_DHPD_HPD_STATUS_ASSERT)
117 
118 struct pmc_usb;
119 
120 struct pmc_usb_port {
121 	int num;
122 	u32 iom_status;
123 	struct pmc_usb *pmc;
124 	struct typec_mux_dev *typec_mux;
125 	struct typec_switch_dev *typec_sw;
126 	struct usb_role_switch *usb_sw;
127 
128 	enum typec_orientation orientation;
129 	enum usb_role role;
130 
131 	u8 usb2_port;
132 	u8 usb3_port;
133 
134 	enum typec_orientation sbu_orientation;
135 	enum typec_orientation hsl_orientation;
136 };
137 
138 struct pmc_usb {
139 	u8 num_ports;
140 	struct device *dev;
141 	struct intel_scu_ipc_dev *ipc;
142 	struct pmc_usb_port *port;
143 	struct acpi_device *iom_adev;
144 	void __iomem *iom_base;
145 	u32 iom_port_status_offset;
146 };
147 
148 static void update_port_status(struct pmc_usb_port *port)
149 {
150 	u8 port_num;
151 
152 	/* SoC expects the USB Type-C port numbers to start with 0 */
153 	port_num = port->usb3_port - 1;
154 
155 	port->iom_status = readl(port->pmc->iom_base +
156 				 port->pmc->iom_port_status_offset +
157 				 port_num * sizeof(u32));
158 }
159 
160 static int sbu_orientation(struct pmc_usb_port *port)
161 {
162 	if (port->sbu_orientation)
163 		return port->sbu_orientation - 1;
164 
165 	return port->orientation - 1;
166 }
167 
168 static int hsl_orientation(struct pmc_usb_port *port)
169 {
170 	if (port->hsl_orientation)
171 		return port->hsl_orientation - 1;
172 
173 	return port->orientation - 1;
174 }
175 
176 static int pmc_usb_send_command(struct intel_scu_ipc_dev *ipc, u8 *msg, u32 len)
177 {
178 	u8 response[4];
179 	u8 status_res;
180 	int ret;
181 
182 	/*
183 	 * Error bit will always be 0 with the USBC command.
184 	 * Status can be checked from the response message if the
185 	 * function intel_scu_ipc_dev_command succeeds.
186 	 */
187 	ret = intel_scu_ipc_dev_command(ipc, PMC_USBC_CMD, 0, msg,
188 					len, response, sizeof(response));
189 
190 	if (ret)
191 		return ret;
192 
193 	status_res = (msg[0] & 0xf) < PMC_USB_SAFE_MODE ?
194 		     response[2] : response[1];
195 
196 	if (status_res & PMC_USB_RESP_STATUS_FAILURE) {
197 		if (status_res & PMC_USB_RESP_STATUS_FATAL)
198 			return -EIO;
199 
200 		return -EBUSY;
201 	}
202 
203 	return 0;
204 }
205 
206 static int pmc_usb_command(struct pmc_usb_port *port, u8 *msg, u32 len)
207 {
208 	int retry_count = 3;
209 	int ret;
210 
211 	/*
212 	 * If PMC is busy then retry the command once again
213 	 */
214 	while (retry_count--) {
215 		ret = pmc_usb_send_command(port->pmc->ipc, msg, len);
216 		if (ret != -EBUSY)
217 			break;
218 	}
219 
220 	return ret;
221 }
222 
223 static int
224 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_displayport_data *dp)
225 {
226 	u8 msg[2] = { };
227 	int ret;
228 
229 	msg[0] = PMC_USB_DP_HPD;
230 	msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
231 
232 	/* Configure HPD first if HPD,IRQ comes together */
233 	if (!IOM_PORT_HPD_ASSERTED(port->iom_status) &&
234 	    dp->status & DP_STATUS_IRQ_HPD &&
235 	    dp->status & DP_STATUS_HPD_STATE) {
236 		msg[1] = PMC_USB_DP_HPD_LVL;
237 		ret = pmc_usb_command(port, msg, sizeof(msg));
238 		if (ret)
239 			return ret;
240 	}
241 
242 	if (dp->status & DP_STATUS_IRQ_HPD)
243 		msg[1] = PMC_USB_DP_HPD_IRQ;
244 
245 	if (dp->status & DP_STATUS_HPD_STATE)
246 		msg[1] |= PMC_USB_DP_HPD_LVL;
247 
248 	return pmc_usb_command(port, msg, sizeof(msg));
249 }
250 
251 static int
252 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state)
253 {
254 	struct typec_displayport_data *data = state->data;
255 	struct altmode_req req = { };
256 	int ret;
257 
258 	if (IOM_PORT_ACTIVITY_IS(port->iom_status, DP) ||
259 	    IOM_PORT_ACTIVITY_IS(port->iom_status, DP_MFD)) {
260 		if (IOM_PORT_HPD_ASSERTED(port->iom_status) &&
261 		    (!(data->status & DP_STATUS_IRQ_HPD) &&
262 		     data->status & DP_STATUS_HPD_STATE))
263 			return 0;
264 
265 		return pmc_usb_mux_dp_hpd(port, state->data);
266 	}
267 
268 	req.usage = PMC_USB_ALT_MODE;
269 	req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
270 	req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT;
271 
272 	req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
273 	req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
274 
275 	req.mode_data |= (state->mode - TYPEC_STATE_MODAL) <<
276 			 PMC_USB_ALTMODE_DP_MODE_SHIFT;
277 
278 	ret = pmc_usb_command(port, (void *)&req, sizeof(req));
279 	if (ret)
280 		return ret;
281 
282 	if (data->status & (DP_STATUS_IRQ_HPD | DP_STATUS_HPD_STATE))
283 		return pmc_usb_mux_dp_hpd(port, state->data);
284 
285 	return 0;
286 }
287 
288 static int
289 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state)
290 {
291 	struct typec_thunderbolt_data *data = state->data;
292 	u8 cable_rounded = TBT_CABLE_ROUNDED_SUPPORT(data->cable_mode);
293 	u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode);
294 	struct altmode_req req = { };
295 
296 	if (IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) ||
297 	    IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB))
298 		return 0;
299 
300 	req.usage = PMC_USB_ALT_MODE;
301 	req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
302 	req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
303 
304 	req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
305 	req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
306 
307 	if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3)
308 		req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE;
309 
310 	if (data->cable_mode & TBT_CABLE_OPTICAL)
311 		req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
312 
313 	if (data->cable_mode & TBT_CABLE_LINK_TRAINING)
314 		req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
315 
316 	if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE)
317 		req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
318 
319 	req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
320 
321 	req.mode_data |= PMC_USB_ALTMODE_TBT_GEN(cable_rounded);
322 
323 	return pmc_usb_command(port, (void *)&req, sizeof(req));
324 }
325 
326 static int
327 pmc_usb_mux_usb4(struct pmc_usb_port *port, struct typec_mux_state *state)
328 {
329 	struct enter_usb_data *data = state->data;
330 	struct altmode_req req = { };
331 	u8 cable_speed;
332 
333 	if (IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) ||
334 	    IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB))
335 		return 0;
336 
337 	req.usage = PMC_USB_ALT_MODE;
338 	req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
339 	req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
340 
341 	/* USB4 Mode */
342 	req.mode_data = PMC_USB_ALTMODE_FORCE_LSR;
343 
344 	if (data->active_link_training)
345 		req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
346 
347 	req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
348 	req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
349 
350 	switch ((data->eudo & EUDO_CABLE_TYPE_MASK) >> EUDO_CABLE_TYPE_SHIFT) {
351 	case EUDO_CABLE_TYPE_PASSIVE:
352 		break;
353 	case EUDO_CABLE_TYPE_OPTICAL:
354 		req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
355 		fallthrough;
356 	default:
357 		req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
358 
359 		/* Configure data rate to rounded in the case of Active TBT3
360 		 * and USB4 cables.
361 		 */
362 		req.mode_data |= PMC_USB_ALTMODE_TBT_GEN(1);
363 		break;
364 	}
365 
366 	cable_speed = (data->eudo & EUDO_CABLE_SPEED_MASK) >> EUDO_CABLE_SPEED_SHIFT;
367 	req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
368 
369 	return pmc_usb_command(port, (void *)&req, sizeof(req));
370 }
371 
372 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port,
373 				  struct typec_mux_state *state)
374 {
375 	u8 msg;
376 
377 	if (IOM_PORT_ACTIVITY_IS(port->iom_status, SAFE_MODE))
378 		return 0;
379 
380 	if ((IOM_PORT_ACTIVITY_IS(port->iom_status, DP) ||
381 	     IOM_PORT_ACTIVITY_IS(port->iom_status, DP_MFD)) &&
382 	     state->alt && state->alt->svid == USB_TYPEC_DP_SID)
383 		return 0;
384 
385 	if ((IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) ||
386 	     IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB)) &&
387 	     state->alt && state->alt->svid == USB_TYPEC_TBT_SID)
388 		return 0;
389 
390 	msg = PMC_USB_SAFE_MODE;
391 	msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
392 
393 	return pmc_usb_command(port, &msg, sizeof(msg));
394 }
395 
396 static int pmc_usb_disconnect(struct pmc_usb_port *port)
397 {
398 	struct typec_displayport_data data = { };
399 	u8 msg[2];
400 
401 	if (!(port->iom_status & IOM_PORT_STATUS_CONNECTED))
402 		return 0;
403 
404 	/* Clear DisplayPort HPD if it's still asserted. */
405 	if (IOM_PORT_HPD_ASSERTED(port->iom_status))
406 		pmc_usb_mux_dp_hpd(port, &data);
407 
408 	msg[0] = PMC_USB_DISCONNECT;
409 	msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
410 
411 	msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
412 
413 	return pmc_usb_command(port, msg, sizeof(msg));
414 }
415 
416 static int pmc_usb_connect(struct pmc_usb_port *port, enum usb_role role)
417 {
418 	u8 ufp = role == USB_ROLE_DEVICE ? 1 : 0;
419 	u8 msg[2];
420 	int ret;
421 
422 	if (port->orientation == TYPEC_ORIENTATION_NONE)
423 		return -EINVAL;
424 
425 	if (port->iom_status & IOM_PORT_STATUS_CONNECTED) {
426 		if (port->role == role || port->role == USB_ROLE_NONE)
427 			return 0;
428 
429 		/* Role swap */
430 		ret = pmc_usb_disconnect(port);
431 		if (ret)
432 			return ret;
433 	}
434 
435 	msg[0] = PMC_USB_CONNECT;
436 	msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
437 
438 	msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
439 	msg[1] |= ufp << PMC_USB_MSG_UFP_SHIFT;
440 	msg[1] |= hsl_orientation(port) << PMC_USB_MSG_ORI_HSL_SHIFT;
441 	msg[1] |= sbu_orientation(port) << PMC_USB_MSG_ORI_AUX_SHIFT;
442 
443 	return pmc_usb_command(port, msg, sizeof(msg));
444 }
445 
446 static int
447 pmc_usb_mux_set(struct typec_mux_dev *mux, struct typec_mux_state *state)
448 {
449 	struct pmc_usb_port *port = typec_mux_get_drvdata(mux);
450 
451 	update_port_status(port);
452 
453 	if (port->orientation == TYPEC_ORIENTATION_NONE || port->role == USB_ROLE_NONE)
454 		return 0;
455 
456 	if (state->mode == TYPEC_STATE_SAFE)
457 		return pmc_usb_mux_safe_state(port, state);
458 	if (state->mode == TYPEC_STATE_USB)
459 		return pmc_usb_connect(port, port->role);
460 
461 	if (state->alt) {
462 		switch (state->alt->svid) {
463 		case USB_TYPEC_TBT_SID:
464 			return pmc_usb_mux_tbt(port, state);
465 		case USB_TYPEC_DP_SID:
466 			return pmc_usb_mux_dp(port, state);
467 		}
468 	} else {
469 		switch (state->mode) {
470 		case TYPEC_MODE_USB2:
471 			/* REVISIT: Try with usb3_port set to 0? */
472 			break;
473 		case TYPEC_MODE_USB3:
474 			return pmc_usb_connect(port, port->role);
475 		case TYPEC_MODE_USB4:
476 			return pmc_usb_mux_usb4(port, state);
477 		}
478 	}
479 
480 	return -EOPNOTSUPP;
481 }
482 
483 static int pmc_usb_set_orientation(struct typec_switch_dev *sw,
484 				   enum typec_orientation orientation)
485 {
486 	struct pmc_usb_port *port = typec_switch_get_drvdata(sw);
487 
488 	update_port_status(port);
489 
490 	port->orientation = orientation;
491 
492 	return 0;
493 }
494 
495 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
496 {
497 	struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw);
498 	int ret;
499 
500 	update_port_status(port);
501 
502 	if (role == USB_ROLE_NONE)
503 		ret = pmc_usb_disconnect(port);
504 	else
505 		ret = pmc_usb_connect(port, role);
506 
507 	port->role = role;
508 
509 	return ret;
510 }
511 
512 static int pmc_usb_register_port(struct pmc_usb *pmc, int index,
513 				 struct fwnode_handle *fwnode)
514 {
515 	struct pmc_usb_port *port = &pmc->port[index];
516 	struct usb_role_switch_desc desc = { };
517 	struct typec_switch_desc sw_desc = { };
518 	struct typec_mux_desc mux_desc = { };
519 	const char *str;
520 	int ret;
521 
522 	ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port);
523 	if (ret)
524 		return ret;
525 
526 	ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port);
527 	if (ret)
528 		return ret;
529 
530 	ret = fwnode_property_read_string(fwnode, "sbu-orientation", &str);
531 	if (!ret)
532 		port->sbu_orientation = typec_find_orientation(str);
533 
534 	ret = fwnode_property_read_string(fwnode, "hsl-orientation", &str);
535 	if (!ret)
536 		port->hsl_orientation = typec_find_orientation(str);
537 
538 	port->num = index;
539 	port->pmc = pmc;
540 
541 	sw_desc.fwnode = fwnode;
542 	sw_desc.drvdata = port;
543 	sw_desc.name = fwnode_get_name(fwnode);
544 	sw_desc.set = pmc_usb_set_orientation;
545 
546 	port->typec_sw = typec_switch_register(pmc->dev, &sw_desc);
547 	if (IS_ERR(port->typec_sw))
548 		return PTR_ERR(port->typec_sw);
549 
550 	mux_desc.fwnode = fwnode;
551 	mux_desc.drvdata = port;
552 	mux_desc.name = fwnode_get_name(fwnode);
553 	mux_desc.set = pmc_usb_mux_set;
554 
555 	port->typec_mux = typec_mux_register(pmc->dev, &mux_desc);
556 	if (IS_ERR(port->typec_mux)) {
557 		ret = PTR_ERR(port->typec_mux);
558 		goto err_unregister_switch;
559 	}
560 
561 	desc.fwnode = fwnode;
562 	desc.driver_data = port;
563 	desc.name = fwnode_get_name(fwnode);
564 	desc.set = pmc_usb_set_role;
565 
566 	port->usb_sw = usb_role_switch_register(pmc->dev, &desc);
567 	if (IS_ERR(port->usb_sw)) {
568 		ret = PTR_ERR(port->usb_sw);
569 		goto err_unregister_mux;
570 	}
571 
572 	return 0;
573 
574 err_unregister_mux:
575 	typec_mux_unregister(port->typec_mux);
576 
577 err_unregister_switch:
578 	typec_switch_unregister(port->typec_sw);
579 
580 	return ret;
581 }
582 
583 /* IOM ACPI IDs and IOM_PORT_STATUS_OFFSET */
584 static const struct acpi_device_id iom_acpi_ids[] = {
585 	/* TigerLake */
586 	{ "INTC1072", 0x560, },
587 
588 	/* AlderLake */
589 	{ "INTC1079", 0x160, },
590 
591 	/* Meteor Lake */
592 	{ "INTC107A", 0x160, },
593 	{}
594 };
595 
596 static int pmc_usb_probe_iom(struct pmc_usb *pmc)
597 {
598 	struct list_head resource_list;
599 	struct resource_entry *rentry;
600 	static const struct acpi_device_id *dev_id;
601 	struct acpi_device *adev = NULL;
602 	int ret;
603 
604 	for (dev_id = &iom_acpi_ids[0]; dev_id->id[0]; dev_id++) {
605 		if (acpi_dev_present(dev_id->id, NULL, -1)) {
606 			pmc->iom_port_status_offset = (u32)dev_id->driver_data;
607 			adev = acpi_dev_get_first_match_dev(dev_id->id, NULL, -1);
608 			break;
609 		}
610 	}
611 
612 	if (!adev)
613 		return -ENODEV;
614 
615 	INIT_LIST_HEAD(&resource_list);
616 	ret = acpi_dev_get_memory_resources(adev, &resource_list);
617 	if (ret < 0)
618 		return ret;
619 
620 	rentry = list_first_entry_or_null(&resource_list, struct resource_entry, node);
621 	if (rentry)
622 		pmc->iom_base = devm_ioremap_resource(pmc->dev, rentry->res);
623 
624 	acpi_dev_free_resource_list(&resource_list);
625 
626 	if (!pmc->iom_base) {
627 		acpi_dev_put(adev);
628 		return -ENOMEM;
629 	}
630 
631 	if (IS_ERR(pmc->iom_base)) {
632 		acpi_dev_put(adev);
633 		return PTR_ERR(pmc->iom_base);
634 	}
635 
636 	pmc->iom_adev = adev;
637 
638 	return 0;
639 }
640 
641 static int pmc_usb_probe(struct platform_device *pdev)
642 {
643 	struct fwnode_handle *fwnode = NULL;
644 	struct pmc_usb *pmc;
645 	int i = 0;
646 	int ret;
647 
648 	pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
649 	if (!pmc)
650 		return -ENOMEM;
651 
652 	device_for_each_child_node(&pdev->dev, fwnode)
653 		pmc->num_ports++;
654 
655 	/* The IOM microcontroller has a limitation of max 4 ports. */
656 	if (pmc->num_ports > 4) {
657 		dev_err(&pdev->dev, "driver limited to 4 ports\n");
658 		return -ERANGE;
659 	}
660 
661 	pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
662 				 sizeof(struct pmc_usb_port), GFP_KERNEL);
663 	if (!pmc->port)
664 		return -ENOMEM;
665 
666 	pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
667 	if (!pmc->ipc)
668 		return -ENODEV;
669 
670 	pmc->dev = &pdev->dev;
671 
672 	ret = pmc_usb_probe_iom(pmc);
673 	if (ret)
674 		return ret;
675 
676 	/*
677 	 * For every physical USB connector (USB2 and USB3 combo) there is a
678 	 * child ACPI device node under the PMC mux ACPI device object.
679 	 */
680 	for (i = 0; i < pmc->num_ports; i++) {
681 		fwnode = device_get_next_child_node(pmc->dev, fwnode);
682 		if (!fwnode)
683 			break;
684 
685 		ret = pmc_usb_register_port(pmc, i, fwnode);
686 		if (ret) {
687 			fwnode_handle_put(fwnode);
688 			goto err_remove_ports;
689 		}
690 	}
691 
692 	platform_set_drvdata(pdev, pmc);
693 
694 	return 0;
695 
696 err_remove_ports:
697 	for (i = 0; i < pmc->num_ports; i++) {
698 		typec_switch_unregister(pmc->port[i].typec_sw);
699 		typec_mux_unregister(pmc->port[i].typec_mux);
700 		usb_role_switch_unregister(pmc->port[i].usb_sw);
701 	}
702 
703 	acpi_dev_put(pmc->iom_adev);
704 
705 	return ret;
706 }
707 
708 static int pmc_usb_remove(struct platform_device *pdev)
709 {
710 	struct pmc_usb *pmc = platform_get_drvdata(pdev);
711 	int i;
712 
713 	for (i = 0; i < pmc->num_ports; i++) {
714 		typec_switch_unregister(pmc->port[i].typec_sw);
715 		typec_mux_unregister(pmc->port[i].typec_mux);
716 		usb_role_switch_unregister(pmc->port[i].usb_sw);
717 	}
718 
719 	acpi_dev_put(pmc->iom_adev);
720 
721 	return 0;
722 }
723 
724 static const struct acpi_device_id pmc_usb_acpi_ids[] = {
725 	{ "INTC105C", },
726 	{ }
727 };
728 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids);
729 
730 static struct platform_driver pmc_usb_driver = {
731 	.driver = {
732 		.name = "intel_pmc_usb",
733 		.acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids),
734 	},
735 	.probe = pmc_usb_probe,
736 	.remove = pmc_usb_remove,
737 };
738 
739 module_platform_driver(pmc_usb_driver);
740 
741 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
742 MODULE_LICENSE("GPL v2");
743 MODULE_DESCRIPTION("Intel PMC USB mux control");
744