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 *typec_mux;
125 struct typec_switch *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
update_port_status(struct pmc_usb_port * port)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
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 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(port->pmc->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_mux_dp_hpd(struct pmc_usb_port * port,struct typec_displayport_data * dp)207 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_displayport_data *dp)
208 {
209 u8 msg[2] = { };
210 int ret;
211
212 msg[0] = PMC_USB_DP_HPD;
213 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
214
215 /* Configure HPD first if HPD,IRQ comes together */
216 if (!IOM_PORT_HPD_ASSERTED(port->iom_status) &&
217 dp->status & DP_STATUS_IRQ_HPD &&
218 dp->status & DP_STATUS_HPD_STATE) {
219 msg[1] = PMC_USB_DP_HPD_LVL;
220 ret = pmc_usb_command(port, msg, sizeof(msg));
221 if (ret)
222 return ret;
223 }
224
225 if (dp->status & DP_STATUS_IRQ_HPD)
226 msg[1] = PMC_USB_DP_HPD_IRQ;
227
228 if (dp->status & DP_STATUS_HPD_STATE)
229 msg[1] |= PMC_USB_DP_HPD_LVL;
230
231 return pmc_usb_command(port, msg, sizeof(msg));
232 }
233
234 static int
pmc_usb_mux_dp(struct pmc_usb_port * port,struct typec_mux_state * state)235 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state)
236 {
237 struct typec_displayport_data *data = state->data;
238 struct altmode_req req = { };
239 int ret;
240
241 if (IOM_PORT_ACTIVITY_IS(port->iom_status, DP) ||
242 IOM_PORT_ACTIVITY_IS(port->iom_status, DP_MFD)) {
243 if (IOM_PORT_HPD_ASSERTED(port->iom_status) &&
244 (!(data->status & DP_STATUS_IRQ_HPD) &&
245 data->status & DP_STATUS_HPD_STATE))
246 return 0;
247
248 return pmc_usb_mux_dp_hpd(port, state->data);
249 }
250
251 req.usage = PMC_USB_ALT_MODE;
252 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
253 req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT;
254
255 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
256 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
257
258 req.mode_data |= (state->mode - TYPEC_STATE_MODAL) <<
259 PMC_USB_ALTMODE_DP_MODE_SHIFT;
260
261 ret = pmc_usb_command(port, (void *)&req, sizeof(req));
262 if (ret)
263 return ret;
264
265 if (data->status & (DP_STATUS_IRQ_HPD | DP_STATUS_HPD_STATE))
266 return pmc_usb_mux_dp_hpd(port, state->data);
267
268 return 0;
269 }
270
271 static int
pmc_usb_mux_tbt(struct pmc_usb_port * port,struct typec_mux_state * state)272 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state)
273 {
274 struct typec_thunderbolt_data *data = state->data;
275 u8 cable_rounded = TBT_CABLE_ROUNDED_SUPPORT(data->cable_mode);
276 u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode);
277 struct altmode_req req = { };
278
279 if (IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) ||
280 IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB))
281 return 0;
282
283 req.usage = PMC_USB_ALT_MODE;
284 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
285 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
286
287 req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
288 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
289
290 if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3)
291 req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE;
292
293 if (data->cable_mode & TBT_CABLE_OPTICAL)
294 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
295
296 if (data->cable_mode & TBT_CABLE_LINK_TRAINING)
297 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
298
299 if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE)
300 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
301
302 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
303
304 req.mode_data |= PMC_USB_ALTMODE_TBT_GEN(cable_rounded);
305
306 return pmc_usb_command(port, (void *)&req, sizeof(req));
307 }
308
309 static int
pmc_usb_mux_usb4(struct pmc_usb_port * port,struct typec_mux_state * state)310 pmc_usb_mux_usb4(struct pmc_usb_port *port, struct typec_mux_state *state)
311 {
312 struct enter_usb_data *data = state->data;
313 struct altmode_req req = { };
314 u8 cable_speed;
315
316 if (IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) ||
317 IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB))
318 return 0;
319
320 req.usage = PMC_USB_ALT_MODE;
321 req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
322 req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
323
324 /* USB4 Mode */
325 req.mode_data = PMC_USB_ALTMODE_FORCE_LSR;
326
327 if (data->active_link_training)
328 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
329
330 req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
331 req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
332
333 switch ((data->eudo & EUDO_CABLE_TYPE_MASK) >> EUDO_CABLE_TYPE_SHIFT) {
334 case EUDO_CABLE_TYPE_PASSIVE:
335 break;
336 case EUDO_CABLE_TYPE_OPTICAL:
337 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
338 fallthrough;
339 default:
340 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
341
342 /* Configure data rate to rounded in the case of Active TBT3
343 * and USB4 cables.
344 */
345 req.mode_data |= PMC_USB_ALTMODE_TBT_GEN(1);
346 break;
347 }
348
349 cable_speed = (data->eudo & EUDO_CABLE_SPEED_MASK) >> EUDO_CABLE_SPEED_SHIFT;
350 req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
351
352 return pmc_usb_command(port, (void *)&req, sizeof(req));
353 }
354
pmc_usb_mux_safe_state(struct pmc_usb_port * port,struct typec_mux_state * state)355 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port,
356 struct typec_mux_state *state)
357 {
358 u8 msg;
359
360 if (IOM_PORT_ACTIVITY_IS(port->iom_status, SAFE_MODE))
361 return 0;
362
363 if ((IOM_PORT_ACTIVITY_IS(port->iom_status, DP) ||
364 IOM_PORT_ACTIVITY_IS(port->iom_status, DP_MFD)) &&
365 state->alt && state->alt->svid == USB_TYPEC_DP_SID)
366 return 0;
367
368 if ((IOM_PORT_ACTIVITY_IS(port->iom_status, TBT) ||
369 IOM_PORT_ACTIVITY_IS(port->iom_status, ALT_MODE_TBT_USB)) &&
370 state->alt && state->alt->svid == USB_TYPEC_TBT_SID)
371 return 0;
372
373 msg = PMC_USB_SAFE_MODE;
374 msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
375
376 return pmc_usb_command(port, &msg, sizeof(msg));
377 }
378
pmc_usb_disconnect(struct pmc_usb_port * port)379 static int pmc_usb_disconnect(struct pmc_usb_port *port)
380 {
381 struct typec_displayport_data data = { };
382 u8 msg[2];
383
384 if (!(port->iom_status & IOM_PORT_STATUS_CONNECTED))
385 return 0;
386
387 /* Clear DisplayPort HPD if it's still asserted. */
388 if (IOM_PORT_HPD_ASSERTED(port->iom_status))
389 pmc_usb_mux_dp_hpd(port, &data);
390
391 msg[0] = PMC_USB_DISCONNECT;
392 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
393
394 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
395
396 return pmc_usb_command(port, msg, sizeof(msg));
397 }
398
pmc_usb_connect(struct pmc_usb_port * port,enum usb_role role)399 static int pmc_usb_connect(struct pmc_usb_port *port, enum usb_role role)
400 {
401 u8 ufp = role == USB_ROLE_DEVICE ? 1 : 0;
402 u8 msg[2];
403 int ret;
404
405 if (port->orientation == TYPEC_ORIENTATION_NONE)
406 return -EINVAL;
407
408 if (port->iom_status & IOM_PORT_STATUS_CONNECTED) {
409 if (port->role == role || port->role == USB_ROLE_NONE)
410 return 0;
411
412 /* Role swap */
413 ret = pmc_usb_disconnect(port);
414 if (ret)
415 return ret;
416 }
417
418 msg[0] = PMC_USB_CONNECT;
419 msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
420
421 msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
422 msg[1] |= ufp << PMC_USB_MSG_UFP_SHIFT;
423 msg[1] |= hsl_orientation(port) << PMC_USB_MSG_ORI_HSL_SHIFT;
424 msg[1] |= sbu_orientation(port) << PMC_USB_MSG_ORI_AUX_SHIFT;
425
426 return pmc_usb_command(port, msg, sizeof(msg));
427 }
428
429 static int
pmc_usb_mux_set(struct typec_mux * mux,struct typec_mux_state * state)430 pmc_usb_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
431 {
432 struct pmc_usb_port *port = typec_mux_get_drvdata(mux);
433
434 update_port_status(port);
435
436 if (port->orientation == TYPEC_ORIENTATION_NONE || port->role == USB_ROLE_NONE)
437 return 0;
438
439 if (state->mode == TYPEC_STATE_SAFE)
440 return pmc_usb_mux_safe_state(port, state);
441 if (state->mode == TYPEC_STATE_USB)
442 return pmc_usb_connect(port, port->role);
443
444 if (state->alt) {
445 switch (state->alt->svid) {
446 case USB_TYPEC_TBT_SID:
447 return pmc_usb_mux_tbt(port, state);
448 case USB_TYPEC_DP_SID:
449 return pmc_usb_mux_dp(port, state);
450 }
451 } else {
452 switch (state->mode) {
453 case TYPEC_MODE_USB2:
454 /* REVISIT: Try with usb3_port set to 0? */
455 break;
456 case TYPEC_MODE_USB3:
457 return pmc_usb_connect(port, port->role);
458 case TYPEC_MODE_USB4:
459 return pmc_usb_mux_usb4(port, state);
460 }
461 }
462
463 return -EOPNOTSUPP;
464 }
465
pmc_usb_set_orientation(struct typec_switch * sw,enum typec_orientation orientation)466 static int pmc_usb_set_orientation(struct typec_switch *sw,
467 enum typec_orientation orientation)
468 {
469 struct pmc_usb_port *port = typec_switch_get_drvdata(sw);
470
471 update_port_status(port);
472
473 port->orientation = orientation;
474
475 return 0;
476 }
477
pmc_usb_set_role(struct usb_role_switch * sw,enum usb_role role)478 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
479 {
480 struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw);
481 int ret;
482
483 update_port_status(port);
484
485 if (role == USB_ROLE_NONE)
486 ret = pmc_usb_disconnect(port);
487 else
488 ret = pmc_usb_connect(port, role);
489
490 port->role = role;
491
492 return ret;
493 }
494
pmc_usb_register_port(struct pmc_usb * pmc,int index,struct fwnode_handle * fwnode)495 static int pmc_usb_register_port(struct pmc_usb *pmc, int index,
496 struct fwnode_handle *fwnode)
497 {
498 struct pmc_usb_port *port = &pmc->port[index];
499 struct usb_role_switch_desc desc = { };
500 struct typec_switch_desc sw_desc = { };
501 struct typec_mux_desc mux_desc = { };
502 const char *str;
503 int ret;
504
505 ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port);
506 if (ret)
507 return ret;
508
509 ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port);
510 if (ret)
511 return ret;
512
513 ret = fwnode_property_read_string(fwnode, "sbu-orientation", &str);
514 if (!ret)
515 port->sbu_orientation = typec_find_orientation(str);
516
517 ret = fwnode_property_read_string(fwnode, "hsl-orientation", &str);
518 if (!ret)
519 port->hsl_orientation = typec_find_orientation(str);
520
521 port->num = index;
522 port->pmc = pmc;
523
524 sw_desc.fwnode = fwnode;
525 sw_desc.drvdata = port;
526 sw_desc.name = fwnode_get_name(fwnode);
527 sw_desc.set = pmc_usb_set_orientation;
528
529 port->typec_sw = typec_switch_register(pmc->dev, &sw_desc);
530 if (IS_ERR(port->typec_sw))
531 return PTR_ERR(port->typec_sw);
532
533 mux_desc.fwnode = fwnode;
534 mux_desc.drvdata = port;
535 mux_desc.name = fwnode_get_name(fwnode);
536 mux_desc.set = pmc_usb_mux_set;
537
538 port->typec_mux = typec_mux_register(pmc->dev, &mux_desc);
539 if (IS_ERR(port->typec_mux)) {
540 ret = PTR_ERR(port->typec_mux);
541 goto err_unregister_switch;
542 }
543
544 desc.fwnode = fwnode;
545 desc.driver_data = port;
546 desc.name = fwnode_get_name(fwnode);
547 desc.set = pmc_usb_set_role;
548
549 port->usb_sw = usb_role_switch_register(pmc->dev, &desc);
550 if (IS_ERR(port->usb_sw)) {
551 ret = PTR_ERR(port->usb_sw);
552 goto err_unregister_mux;
553 }
554
555 return 0;
556
557 err_unregister_mux:
558 typec_mux_unregister(port->typec_mux);
559
560 err_unregister_switch:
561 typec_switch_unregister(port->typec_sw);
562
563 return ret;
564 }
565
566 /* IOM ACPI IDs and IOM_PORT_STATUS_OFFSET */
567 static const struct acpi_device_id iom_acpi_ids[] = {
568 /* TigerLake */
569 { "INTC1072", 0x560, },
570
571 /* AlderLake */
572 { "INTC1079", 0x160, },
573
574 /* Meteor Lake */
575 { "INTC107A", 0x160, },
576 {}
577 };
578
pmc_usb_probe_iom(struct pmc_usb * pmc)579 static int pmc_usb_probe_iom(struct pmc_usb *pmc)
580 {
581 struct list_head resource_list;
582 struct resource_entry *rentry;
583 static const struct acpi_device_id *dev_id;
584 struct acpi_device *adev = NULL;
585 int ret;
586
587 for (dev_id = &iom_acpi_ids[0]; dev_id->id[0]; dev_id++) {
588 if (acpi_dev_present(dev_id->id, NULL, -1)) {
589 pmc->iom_port_status_offset = (u32)dev_id->driver_data;
590 adev = acpi_dev_get_first_match_dev(dev_id->id, NULL, -1);
591 break;
592 }
593 }
594
595 if (!adev)
596 return -ENODEV;
597
598 INIT_LIST_HEAD(&resource_list);
599 ret = acpi_dev_get_memory_resources(adev, &resource_list);
600 if (ret < 0) {
601 acpi_dev_put(adev);
602 return ret;
603 }
604
605 rentry = list_first_entry_or_null(&resource_list, struct resource_entry, node);
606 if (rentry)
607 pmc->iom_base = devm_ioremap_resource(pmc->dev, rentry->res);
608
609 acpi_dev_free_resource_list(&resource_list);
610
611 if (!pmc->iom_base) {
612 acpi_dev_put(adev);
613 return -ENOMEM;
614 }
615
616 if (IS_ERR(pmc->iom_base)) {
617 acpi_dev_put(adev);
618 return PTR_ERR(pmc->iom_base);
619 }
620
621 pmc->iom_adev = adev;
622
623 return 0;
624 }
625
pmc_usb_probe(struct platform_device * pdev)626 static int pmc_usb_probe(struct platform_device *pdev)
627 {
628 struct fwnode_handle *fwnode = NULL;
629 struct pmc_usb *pmc;
630 int i = 0;
631 int ret;
632
633 pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
634 if (!pmc)
635 return -ENOMEM;
636
637 device_for_each_child_node(&pdev->dev, fwnode)
638 pmc->num_ports++;
639
640 /* The IOM microcontroller has a limitation of max 4 ports. */
641 if (pmc->num_ports > 4) {
642 dev_err(&pdev->dev, "driver limited to 4 ports\n");
643 return -ERANGE;
644 }
645
646 pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
647 sizeof(struct pmc_usb_port), GFP_KERNEL);
648 if (!pmc->port)
649 return -ENOMEM;
650
651 pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
652 if (!pmc->ipc)
653 return -ENODEV;
654
655 pmc->dev = &pdev->dev;
656
657 ret = pmc_usb_probe_iom(pmc);
658 if (ret)
659 return ret;
660
661 /*
662 * For every physical USB connector (USB2 and USB3 combo) there is a
663 * child ACPI device node under the PMC mux ACPI device object.
664 */
665 for (i = 0; i < pmc->num_ports; i++) {
666 fwnode = device_get_next_child_node(pmc->dev, fwnode);
667 if (!fwnode)
668 break;
669
670 ret = pmc_usb_register_port(pmc, i, fwnode);
671 if (ret) {
672 fwnode_handle_put(fwnode);
673 goto err_remove_ports;
674 }
675 }
676
677 platform_set_drvdata(pdev, pmc);
678
679 return 0;
680
681 err_remove_ports:
682 for (i = 0; i < pmc->num_ports; i++) {
683 typec_switch_unregister(pmc->port[i].typec_sw);
684 typec_mux_unregister(pmc->port[i].typec_mux);
685 usb_role_switch_unregister(pmc->port[i].usb_sw);
686 }
687
688 acpi_dev_put(pmc->iom_adev);
689
690 return ret;
691 }
692
pmc_usb_remove(struct platform_device * pdev)693 static int pmc_usb_remove(struct platform_device *pdev)
694 {
695 struct pmc_usb *pmc = platform_get_drvdata(pdev);
696 int i;
697
698 for (i = 0; i < pmc->num_ports; i++) {
699 typec_switch_unregister(pmc->port[i].typec_sw);
700 typec_mux_unregister(pmc->port[i].typec_mux);
701 usb_role_switch_unregister(pmc->port[i].usb_sw);
702 }
703
704 acpi_dev_put(pmc->iom_adev);
705
706 return 0;
707 }
708
709 static const struct acpi_device_id pmc_usb_acpi_ids[] = {
710 { "INTC105C", },
711 { }
712 };
713 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids);
714
715 static struct platform_driver pmc_usb_driver = {
716 .driver = {
717 .name = "intel_pmc_usb",
718 .acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids),
719 },
720 .probe = pmc_usb_probe,
721 .remove = pmc_usb_remove,
722 };
723
724 module_platform_driver(pmc_usb_driver);
725
726 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
727 MODULE_LICENSE("GPL v2");
728 MODULE_DESCRIPTION("Intel PMC USB mux control");
729