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