Merge tag 'nfs-for-5.9-1' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[linux-2.6-microblaze.git] / drivers / usb / typec / mux / intel_pmc_mux.c
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 #define PMC_USB_ALTMODE_ORI_AUX_SHIFT   4
65 #define PMC_USB_ALTMODE_ORI_HSL_SHIFT   5
66
67 /* DP specific Mode Data bits */
68 #define PMC_USB_ALTMODE_DP_MODE_SHIFT   8
69
70 /* TBT specific Mode Data bits */
71 #define PMC_USB_ALTMODE_HPD_HIGH        BIT(14)
72 #define PMC_USB_ALTMODE_TBT_TYPE        BIT(17)
73 #define PMC_USB_ALTMODE_CABLE_TYPE      BIT(18)
74 #define PMC_USB_ALTMODE_ACTIVE_LINK     BIT(20)
75 #define PMC_USB_ALTMODE_FORCE_LSR       BIT(23)
76 #define PMC_USB_ALTMODE_CABLE_SPD(_s_)  (((_s_) & GENMASK(2, 0)) << 25)
77 #define   PMC_USB_ALTMODE_CABLE_USB31   1
78 #define   PMC_USB_ALTMODE_CABLE_10GPS   2
79 #define   PMC_USB_ALTMODE_CABLE_20GPS   3
80 #define PMC_USB_ALTMODE_TBT_GEN(_g_)    (((_g_) & GENMASK(1, 0)) << 28)
81
82 /* Display HPD Request bits */
83 #define PMC_USB_DP_HPD_LVL              BIT(4)
84 #define PMC_USB_DP_HPD_IRQ              BIT(5)
85
86 struct pmc_usb;
87
88 struct pmc_usb_port {
89         int num;
90         struct pmc_usb *pmc;
91         struct typec_mux *typec_mux;
92         struct typec_switch *typec_sw;
93         struct usb_role_switch *usb_sw;
94
95         enum typec_orientation orientation;
96         enum usb_role role;
97
98         u8 usb2_port;
99         u8 usb3_port;
100
101         enum typec_orientation sbu_orientation;
102         enum typec_orientation hsl_orientation;
103 };
104
105 struct pmc_usb {
106         u8 num_ports;
107         struct device *dev;
108         struct intel_scu_ipc_dev *ipc;
109         struct pmc_usb_port *port;
110 };
111
112 static int sbu_orientation(struct pmc_usb_port *port)
113 {
114         if (port->sbu_orientation)
115                 return port->sbu_orientation - 1;
116
117         return port->orientation - 1;
118 }
119
120 static int hsl_orientation(struct pmc_usb_port *port)
121 {
122         if (port->hsl_orientation)
123                 return port->hsl_orientation - 1;
124
125         return port->orientation - 1;
126 }
127
128 static int pmc_usb_command(struct pmc_usb_port *port, u8 *msg, u32 len)
129 {
130         u8 response[4];
131
132         /*
133          * Error bit will always be 0 with the USBC command.
134          * Status can be checked from the response message.
135          */
136         intel_scu_ipc_dev_command(port->pmc->ipc, PMC_USBC_CMD, 0, msg, len,
137                                   response, sizeof(response));
138         if (response[2] & PMC_USB_RESP_STATUS_FAILURE) {
139                 if (response[2] & PMC_USB_RESP_STATUS_FATAL)
140                         return -EIO;
141                 return -EBUSY;
142         }
143
144         return 0;
145 }
146
147 static int
148 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_mux_state *state)
149 {
150         struct typec_displayport_data *data = state->data;
151         u8 msg[2] = { };
152
153         msg[0] = PMC_USB_DP_HPD;
154         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
155
156         if (data->status & DP_STATUS_IRQ_HPD)
157                 msg[1] = PMC_USB_DP_HPD_IRQ;
158
159         if (data->status & DP_STATUS_HPD_STATE)
160                 msg[1] |= PMC_USB_DP_HPD_LVL;
161
162         return pmc_usb_command(port, msg, sizeof(msg));
163 }
164
165 static int
166 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state)
167 {
168         struct typec_displayport_data *data = state->data;
169         struct altmode_req req = { };
170         int ret;
171
172         if (data->status & DP_STATUS_IRQ_HPD)
173                 return pmc_usb_mux_dp_hpd(port, state);
174
175         req.usage = PMC_USB_ALT_MODE;
176         req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
177         req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT;
178
179         req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
180         req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
181
182         req.mode_data |= sbu_orientation(port) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
183         req.mode_data |= hsl_orientation(port) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
184
185         req.mode_data |= (state->mode - TYPEC_STATE_MODAL) <<
186                          PMC_USB_ALTMODE_DP_MODE_SHIFT;
187
188         if (data->status & DP_STATUS_HPD_STATE)
189                 req.mode_data |= PMC_USB_ALTMODE_HPD_HIGH;
190
191         ret = pmc_usb_command(port, (void *)&req, sizeof(req));
192         if (ret)
193                 return ret;
194
195         if (data->status & DP_STATUS_HPD_STATE)
196                 return pmc_usb_mux_dp_hpd(port, state);
197
198         return 0;
199 }
200
201 static int
202 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state)
203 {
204         struct typec_thunderbolt_data *data = state->data;
205         u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode);
206         struct altmode_req req = { };
207
208         req.usage = PMC_USB_ALT_MODE;
209         req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
210         req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
211
212         req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
213         req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
214
215         req.mode_data |= sbu_orientation(port) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
216         req.mode_data |= hsl_orientation(port) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
217
218         if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3)
219                 req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE;
220
221         if (data->cable_mode & TBT_CABLE_OPTICAL)
222                 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
223
224         if (data->cable_mode & TBT_CABLE_LINK_TRAINING)
225                 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
226
227         if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE)
228                 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
229
230         req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
231
232         return pmc_usb_command(port, (void *)&req, sizeof(req));
233 }
234
235 static int
236 pmc_usb_mux_usb4(struct pmc_usb_port *port, struct typec_mux_state *state)
237 {
238         struct enter_usb_data *data = state->data;
239         struct altmode_req req = { };
240         u8 cable_speed;
241
242         req.usage = PMC_USB_ALT_MODE;
243         req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
244         req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
245
246         /* USB4 Mode */
247         req.mode_data = PMC_USB_ALTMODE_FORCE_LSR;
248
249         if (data->active_link_training)
250                 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
251
252         req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
253         req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
254
255         switch ((data->eudo & EUDO_CABLE_TYPE_MASK) >> EUDO_CABLE_TYPE_SHIFT) {
256         case EUDO_CABLE_TYPE_PASSIVE:
257                 break;
258         case EUDO_CABLE_TYPE_OPTICAL:
259                 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
260                 fallthrough;
261         default:
262                 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
263                 break;
264         }
265
266         cable_speed = (data->eudo & EUDO_CABLE_SPEED_MASK) >> EUDO_CABLE_SPEED_SHIFT;
267         req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
268
269         return pmc_usb_command(port, (void *)&req, sizeof(req));
270 }
271
272 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port)
273 {
274         u8 msg;
275
276         msg = PMC_USB_SAFE_MODE;
277         msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
278
279         return pmc_usb_command(port, &msg, sizeof(msg));
280 }
281
282 static int pmc_usb_connect(struct pmc_usb_port *port)
283 {
284         u8 msg[2];
285
286         msg[0] = PMC_USB_CONNECT;
287         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
288
289         msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
290         msg[1] |= hsl_orientation(port) << PMC_USB_MSG_ORI_HSL_SHIFT;
291         msg[1] |= sbu_orientation(port) << PMC_USB_MSG_ORI_AUX_SHIFT;
292
293         return pmc_usb_command(port, msg, sizeof(msg));
294 }
295
296 static int pmc_usb_disconnect(struct pmc_usb_port *port)
297 {
298         u8 msg[2];
299
300         msg[0] = PMC_USB_DISCONNECT;
301         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
302
303         msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
304
305         return pmc_usb_command(port, msg, sizeof(msg));
306 }
307
308 static int
309 pmc_usb_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
310 {
311         struct pmc_usb_port *port = typec_mux_get_drvdata(mux);
312
313         if (port->orientation == TYPEC_ORIENTATION_NONE || port->role == USB_ROLE_NONE)
314                 return 0;
315
316         if (state->mode == TYPEC_STATE_SAFE)
317                 return pmc_usb_mux_safe_state(port);
318         if (state->mode == TYPEC_STATE_USB)
319                 return pmc_usb_connect(port);
320
321         if (state->alt) {
322                 switch (state->alt->svid) {
323                 case USB_TYPEC_TBT_SID:
324                         return pmc_usb_mux_tbt(port, state);
325                 case USB_TYPEC_DP_SID:
326                         return pmc_usb_mux_dp(port, state);
327                 }
328         } else {
329                 switch (state->mode) {
330                 case TYPEC_MODE_USB2:
331                         /* REVISIT: Try with usb3_port set to 0? */
332                         break;
333                 case TYPEC_MODE_USB3:
334                         return pmc_usb_connect(port);
335                 case TYPEC_MODE_USB4:
336                         return pmc_usb_mux_usb4(port, state);
337                 }
338         }
339
340         return -EOPNOTSUPP;
341 }
342
343 static int pmc_usb_set_orientation(struct typec_switch *sw,
344                                    enum typec_orientation orientation)
345 {
346         struct pmc_usb_port *port = typec_switch_get_drvdata(sw);
347
348         if (port->orientation == orientation)
349                 return 0;
350
351         port->orientation = orientation;
352
353         if (port->role) {
354                 if (orientation == TYPEC_ORIENTATION_NONE)
355                         return pmc_usb_disconnect(port);
356                 else
357                         return pmc_usb_connect(port);
358         }
359
360         return 0;
361 }
362
363 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
364 {
365         struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw);
366
367         if (port->role == role)
368                 return 0;
369
370         port->role = role;
371
372         if (port->orientation) {
373                 if (role == USB_ROLE_NONE)
374                         return pmc_usb_disconnect(port);
375                 else
376                         return pmc_usb_connect(port);
377         }
378
379         return 0;
380 }
381
382 static int pmc_usb_register_port(struct pmc_usb *pmc, int index,
383                                  struct fwnode_handle *fwnode)
384 {
385         struct pmc_usb_port *port = &pmc->port[index];
386         struct usb_role_switch_desc desc = { };
387         struct typec_switch_desc sw_desc = { };
388         struct typec_mux_desc mux_desc = { };
389         const char *str;
390         int ret;
391
392         ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port);
393         if (ret)
394                 return ret;
395
396         ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port);
397         if (ret)
398                 return ret;
399
400         ret = fwnode_property_read_string(fwnode, "sbu-orientation", &str);
401         if (!ret)
402                 port->sbu_orientation = typec_find_orientation(str);
403
404         ret = fwnode_property_read_string(fwnode, "hsl-orientation", &str);
405         if (!ret)
406                 port->hsl_orientation = typec_find_orientation(str);
407
408         port->num = index;
409         port->pmc = pmc;
410
411         sw_desc.fwnode = fwnode;
412         sw_desc.drvdata = port;
413         sw_desc.name = fwnode_get_name(fwnode);
414         sw_desc.set = pmc_usb_set_orientation;
415
416         port->typec_sw = typec_switch_register(pmc->dev, &sw_desc);
417         if (IS_ERR(port->typec_sw))
418                 return PTR_ERR(port->typec_sw);
419
420         mux_desc.fwnode = fwnode;
421         mux_desc.drvdata = port;
422         mux_desc.name = fwnode_get_name(fwnode);
423         mux_desc.set = pmc_usb_mux_set;
424
425         port->typec_mux = typec_mux_register(pmc->dev, &mux_desc);
426         if (IS_ERR(port->typec_mux)) {
427                 ret = PTR_ERR(port->typec_mux);
428                 goto err_unregister_switch;
429         }
430
431         desc.fwnode = fwnode;
432         desc.driver_data = port;
433         desc.name = fwnode_get_name(fwnode);
434         desc.set = pmc_usb_set_role;
435
436         port->usb_sw = usb_role_switch_register(pmc->dev, &desc);
437         if (IS_ERR(port->usb_sw)) {
438                 ret = PTR_ERR(port->usb_sw);
439                 goto err_unregister_mux;
440         }
441
442         return 0;
443
444 err_unregister_mux:
445         typec_mux_unregister(port->typec_mux);
446
447 err_unregister_switch:
448         typec_switch_unregister(port->typec_sw);
449
450         return ret;
451 }
452
453 static int pmc_usb_probe(struct platform_device *pdev)
454 {
455         struct fwnode_handle *fwnode = NULL;
456         struct pmc_usb *pmc;
457         int i = 0;
458         int ret;
459
460         pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
461         if (!pmc)
462                 return -ENOMEM;
463
464         device_for_each_child_node(&pdev->dev, fwnode)
465                 pmc->num_ports++;
466
467         pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
468                                  sizeof(struct pmc_usb_port), GFP_KERNEL);
469         if (!pmc->port)
470                 return -ENOMEM;
471
472         pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
473         if (!pmc->ipc)
474                 return -ENODEV;
475
476         pmc->dev = &pdev->dev;
477
478         /*
479          * For every physical USB connector (USB2 and USB3 combo) there is a
480          * child ACPI device node under the PMC mux ACPI device object.
481          */
482         for (i = 0; i < pmc->num_ports; i++) {
483                 fwnode = device_get_next_child_node(pmc->dev, fwnode);
484                 if (!fwnode)
485                         break;
486
487                 ret = pmc_usb_register_port(pmc, i, fwnode);
488                 if (ret)
489                         goto err_remove_ports;
490         }
491
492         platform_set_drvdata(pdev, pmc);
493
494         return 0;
495
496 err_remove_ports:
497         for (i = 0; i < pmc->num_ports; i++) {
498                 typec_switch_unregister(pmc->port[i].typec_sw);
499                 typec_mux_unregister(pmc->port[i].typec_mux);
500         }
501
502         return ret;
503 }
504
505 static int pmc_usb_remove(struct platform_device *pdev)
506 {
507         struct pmc_usb *pmc = platform_get_drvdata(pdev);
508         int i;
509
510         for (i = 0; i < pmc->num_ports; i++) {
511                 typec_switch_unregister(pmc->port[i].typec_sw);
512                 typec_mux_unregister(pmc->port[i].typec_mux);
513         }
514
515         return 0;
516 }
517
518 static const struct acpi_device_id pmc_usb_acpi_ids[] = {
519         { "INTC105C", },
520         { }
521 };
522 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids);
523
524 static struct platform_driver pmc_usb_driver = {
525         .driver = {
526                 .name = "intel_pmc_usb",
527                 .acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids),
528         },
529         .probe = pmc_usb_probe,
530         .remove = pmc_usb_remove,
531 };
532
533 module_platform_driver(pmc_usb_driver);
534
535 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
536 MODULE_LICENSE("GPL v2");
537 MODULE_DESCRIPTION("Intel PMC USB mux control");