usb: dwc3: meson-g12a: fix USB2 PHY initialization on G12A and A1 SoCs
[platform/kernel/linux-rpi.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/role.h>
14 #include <linux/usb/typec_mux.h>
15 #include <linux/usb/typec_dp.h>
16 #include <linux/usb/typec_tbt.h>
17
18 #include <asm/intel_pmc_ipc.h>
19
20 #define PMC_USBC_CMD            0xa7
21
22 /* "Usage" OOB Message field values */
23 enum {
24         PMC_USB_CONNECT,
25         PMC_USB_DISCONNECT,
26         PMC_USB_SAFE_MODE,
27         PMC_USB_ALT_MODE,
28         PMC_USB_DP_HPD,
29 };
30
31 #define PMC_USB_MSG_USB2_PORT_SHIFT     0
32 #define PMC_USB_MSG_USB3_PORT_SHIFT     4
33 #define PMC_USB_MSG_UFP_SHIFT           4
34 #define PMC_USB_MSG_ORI_HSL_SHIFT       5
35 #define PMC_USB_MSG_ORI_AUX_SHIFT       6
36
37 /* Alt Mode Request */
38 struct altmode_req {
39         u8 usage;
40         u8 mode_type;
41         u8 mode_id;
42         u8 reserved;
43         u32 mode_data;
44 } __packed;
45
46 #define PMC_USB_MODE_TYPE_SHIFT         4
47
48 enum {
49         PMC_USB_MODE_TYPE_USB,
50         PMC_USB_MODE_TYPE_DP,
51         PMC_USB_MODE_TYPE_TBT,
52 };
53
54 /* Common Mode Data bits */
55 #define PMC_USB_ALTMODE_ACTIVE_CABLE    BIT(2)
56
57 #define PMC_USB_ALTMODE_ORI_SHIFT       1
58 #define PMC_USB_ALTMODE_UFP_SHIFT       3
59 #define PMC_USB_ALTMODE_ORI_AUX_SHIFT   4
60 #define PMC_USB_ALTMODE_ORI_HSL_SHIFT   5
61
62 /* DP specific Mode Data bits */
63 #define PMC_USB_ALTMODE_DP_MODE_SHIFT   8
64
65 /* TBT specific Mode Data bits */
66 #define PMC_USB_ALTMODE_HPD_HIGH        BIT(14)
67 #define PMC_USB_ALTMODE_TBT_TYPE        BIT(17)
68 #define PMC_USB_ALTMODE_CABLE_TYPE      BIT(18)
69 #define PMC_USB_ALTMODE_ACTIVE_LINK     BIT(20)
70 #define PMC_USB_ALTMODE_FORCE_LSR       BIT(23)
71 #define PMC_USB_ALTMODE_CABLE_SPD(_s_)  (((_s_) & GENMASK(2, 0)) << 25)
72 #define   PMC_USB_ALTMODE_CABLE_USB31   1
73 #define   PMC_USB_ALTMODE_CABLE_10GPS   2
74 #define   PMC_USB_ALTMODE_CABLE_20GPS   3
75 #define PMC_USB_ALTMODE_TBT_GEN(_g_)    (((_g_) & GENMASK(1, 0)) << 28)
76
77 /* Display HPD Request bits */
78 #define PMC_USB_DP_HPD_LVL              BIT(4)
79 #define PMC_USB_DP_HPD_IRQ              BIT(5)
80
81 struct pmc_usb;
82
83 struct pmc_usb_port {
84         int num;
85         struct pmc_usb *pmc;
86         struct typec_mux *typec_mux;
87         struct typec_switch *typec_sw;
88         struct usb_role_switch *usb_sw;
89
90         enum typec_orientation orientation;
91         enum usb_role role;
92
93         u8 usb2_port;
94         u8 usb3_port;
95
96         enum typec_orientation sbu_orientation;
97         enum typec_orientation hsl_orientation;
98 };
99
100 struct pmc_usb {
101         u8 num_ports;
102         struct device *dev;
103         struct pmc_usb_port *port;
104 };
105
106 static int sbu_orientation(struct pmc_usb_port *port)
107 {
108         if (port->sbu_orientation)
109                 return port->sbu_orientation - 1;
110
111         return port->orientation - 1;
112 }
113
114 static int hsl_orientation(struct pmc_usb_port *port)
115 {
116         if (port->hsl_orientation)
117                 return port->hsl_orientation - 1;
118
119         return port->orientation - 1;
120 }
121
122 static int pmc_usb_command(struct pmc_usb_port *port, u8 *msg, u32 len)
123 {
124         u8 response[4];
125
126         /*
127          * Error bit will always be 0 with the USBC command.
128          * Status can be checked from the response message.
129          */
130         intel_pmc_ipc_command(PMC_USBC_CMD, 0, msg, len,
131                               (void *)response, 1);
132
133         if (response[2]) {
134                 if (response[2] & BIT(1))
135                         return -EIO;
136                 return -EBUSY;
137         }
138
139         return 0;
140 }
141
142 static int
143 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_mux_state *state)
144 {
145         struct typec_displayport_data *data = state->data;
146         u8 msg[2] = { };
147
148         msg[0] = PMC_USB_DP_HPD;
149         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
150
151         msg[1] = PMC_USB_DP_HPD_IRQ;
152
153         if (data->status & DP_STATUS_HPD_STATE)
154                 msg[1] |= PMC_USB_DP_HPD_LVL;
155
156         return pmc_usb_command(port, msg, sizeof(msg));
157 }
158
159 static int
160 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state)
161 {
162         struct typec_displayport_data *data = state->data;
163         struct altmode_req req = { };
164
165         if (data->status & DP_STATUS_IRQ_HPD)
166                 return pmc_usb_mux_dp_hpd(port, state);
167
168         req.usage = PMC_USB_ALT_MODE;
169         req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
170         req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT;
171
172         req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
173         req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
174
175         req.mode_data |= sbu_orientation(port) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
176         req.mode_data |= hsl_orientation(port) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
177
178         req.mode_data |= (state->mode - TYPEC_STATE_MODAL) <<
179                          PMC_USB_ALTMODE_DP_MODE_SHIFT;
180
181         if (data->status & DP_STATUS_HPD_STATE)
182                 req.mode_data |= PMC_USB_ALTMODE_HPD_HIGH;
183
184         return pmc_usb_command(port, (void *)&req, sizeof(req));
185 }
186
187 static int
188 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state)
189 {
190         struct typec_thunderbolt_data *data = state->data;
191         u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode);
192         struct altmode_req req = { };
193
194         req.usage = PMC_USB_ALT_MODE;
195         req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
196         req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
197
198         req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
199         req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
200
201         req.mode_data |= sbu_orientation(port) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
202         req.mode_data |= hsl_orientation(port) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
203
204         if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3)
205                 req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE;
206
207         if (data->cable_mode & TBT_CABLE_OPTICAL)
208                 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
209
210         if (data->cable_mode & TBT_CABLE_LINK_TRAINING)
211                 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
212
213         if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE)
214                 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
215
216         req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
217
218         return pmc_usb_command(port, (void *)&req, sizeof(req));
219 }
220
221 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port)
222 {
223         u8 msg;
224
225         msg = PMC_USB_SAFE_MODE;
226         msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
227
228         return pmc_usb_command(port, &msg, sizeof(msg));
229 }
230
231 static int pmc_usb_connect(struct pmc_usb_port *port)
232 {
233         u8 msg[2];
234
235         msg[0] = PMC_USB_CONNECT;
236         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
237
238         msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
239         msg[1] |= hsl_orientation(port) << PMC_USB_MSG_ORI_HSL_SHIFT;
240         msg[1] |= sbu_orientation(port) << PMC_USB_MSG_ORI_AUX_SHIFT;
241
242         return pmc_usb_command(port, msg, sizeof(msg));
243 }
244
245 static int pmc_usb_disconnect(struct pmc_usb_port *port)
246 {
247         u8 msg[2];
248
249         msg[0] = PMC_USB_DISCONNECT;
250         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
251
252         msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
253
254         return pmc_usb_command(port, msg, sizeof(msg));
255 }
256
257 static int
258 pmc_usb_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
259 {
260         struct pmc_usb_port *port = typec_mux_get_drvdata(mux);
261
262         if (!state->alt)
263                 return 0;
264
265         if (state->mode == TYPEC_STATE_SAFE)
266                 return pmc_usb_mux_safe_state(port);
267
268         switch (state->alt->svid) {
269         case USB_TYPEC_TBT_SID:
270                 return pmc_usb_mux_tbt(port, state);
271         case USB_TYPEC_DP_SID:
272                 return pmc_usb_mux_dp(port, state);
273         }
274
275         return -EOPNOTSUPP;
276 }
277
278 static int pmc_usb_set_orientation(struct typec_switch *sw,
279                                    enum typec_orientation orientation)
280 {
281         struct pmc_usb_port *port = typec_switch_get_drvdata(sw);
282
283         if (port->orientation == orientation)
284                 return 0;
285
286         port->orientation = orientation;
287
288         if (port->role) {
289                 if (orientation == TYPEC_ORIENTATION_NONE)
290                         return pmc_usb_disconnect(port);
291                 else
292                         return pmc_usb_connect(port);
293         }
294
295         return 0;
296 }
297
298 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
299 {
300         struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw);
301
302         if (port->role == role)
303                 return 0;
304
305         port->role = role;
306
307         if (port->orientation) {
308                 if (role == USB_ROLE_NONE)
309                         return pmc_usb_disconnect(port);
310                 else
311                         return pmc_usb_connect(port);
312         }
313
314         return 0;
315 }
316
317 static int pmc_usb_register_port(struct pmc_usb *pmc, int index,
318                                  struct fwnode_handle *fwnode)
319 {
320         struct pmc_usb_port *port = &pmc->port[index];
321         struct usb_role_switch_desc desc = { };
322         struct typec_switch_desc sw_desc = { };
323         struct typec_mux_desc mux_desc = { };
324         const char *str;
325         int ret;
326
327         ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port);
328         if (ret)
329                 return ret;
330
331         ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port);
332         if (ret)
333                 return ret;
334
335         ret = fwnode_property_read_string(fwnode, "sbu-orientation", &str);
336         if (!ret)
337                 port->sbu_orientation = typec_find_orientation(str);
338
339         ret = fwnode_property_read_string(fwnode, "hsl-orientation", &str);
340         if (!ret)
341                 port->hsl_orientation = typec_find_orientation(str);
342
343         port->num = index;
344         port->pmc = pmc;
345
346         sw_desc.fwnode = fwnode;
347         sw_desc.drvdata = port;
348         sw_desc.name = fwnode_get_name(fwnode);
349         sw_desc.set = pmc_usb_set_orientation;
350
351         port->typec_sw = typec_switch_register(pmc->dev, &sw_desc);
352         if (IS_ERR(port->typec_sw))
353                 return PTR_ERR(port->typec_sw);
354
355         mux_desc.fwnode = fwnode;
356         mux_desc.drvdata = port;
357         mux_desc.name = fwnode_get_name(fwnode);
358         mux_desc.set = pmc_usb_mux_set;
359
360         port->typec_mux = typec_mux_register(pmc->dev, &mux_desc);
361         if (IS_ERR(port->typec_mux)) {
362                 ret = PTR_ERR(port->typec_mux);
363                 goto err_unregister_switch;
364         }
365
366         desc.fwnode = fwnode;
367         desc.driver_data = port;
368         desc.name = fwnode_get_name(fwnode);
369         desc.set = pmc_usb_set_role;
370
371         port->usb_sw = usb_role_switch_register(pmc->dev, &desc);
372         if (IS_ERR(port->usb_sw)) {
373                 ret = PTR_ERR(port->usb_sw);
374                 goto err_unregister_mux;
375         }
376
377         return 0;
378
379 err_unregister_mux:
380         typec_mux_unregister(port->typec_mux);
381
382 err_unregister_switch:
383         typec_switch_unregister(port->typec_sw);
384
385         return ret;
386 }
387
388 static int pmc_usb_probe(struct platform_device *pdev)
389 {
390         struct fwnode_handle *fwnode = NULL;
391         struct pmc_usb *pmc;
392         int i = 0;
393         int ret;
394
395         pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
396         if (!pmc)
397                 return -ENOMEM;
398
399         device_for_each_child_node(&pdev->dev, fwnode)
400                 pmc->num_ports++;
401
402         pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
403                                  sizeof(struct pmc_usb_port), GFP_KERNEL);
404         if (!pmc->port)
405                 return -ENOMEM;
406
407         pmc->dev = &pdev->dev;
408
409         /*
410          * For every physical USB connector (USB2 and USB3 combo) there is a
411          * child ACPI device node under the PMC mux ACPI device object.
412          */
413         for (i = 0; i < pmc->num_ports; i++) {
414                 fwnode = device_get_next_child_node(pmc->dev, fwnode);
415                 if (!fwnode)
416                         break;
417
418                 ret = pmc_usb_register_port(pmc, i, fwnode);
419                 if (ret)
420                         goto err_remove_ports;
421         }
422
423         platform_set_drvdata(pdev, pmc);
424
425         return 0;
426
427 err_remove_ports:
428         for (i = 0; i < pmc->num_ports; i++) {
429                 typec_switch_unregister(pmc->port[i].typec_sw);
430                 typec_mux_unregister(pmc->port[i].typec_mux);
431         }
432
433         return ret;
434 }
435
436 static int pmc_usb_remove(struct platform_device *pdev)
437 {
438         struct pmc_usb *pmc = platform_get_drvdata(pdev);
439         int i;
440
441         for (i = 0; i < pmc->num_ports; i++) {
442                 typec_switch_unregister(pmc->port[i].typec_sw);
443                 typec_mux_unregister(pmc->port[i].typec_mux);
444         }
445
446         return 0;
447 }
448
449 static const struct acpi_device_id pmc_usb_acpi_ids[] = {
450         { "INTC105C", },
451         { }
452 };
453 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids);
454
455 static struct platform_driver pmc_usb_driver = {
456         .driver = {
457                 .name = "intel_pmc_usb",
458                 .acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids),
459         },
460         .probe = pmc_usb_probe,
461         .remove = pmc_usb_remove,
462 };
463
464 module_platform_driver(pmc_usb_driver);
465
466 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
467 MODULE_LICENSE("GPL v2");
468 MODULE_DESCRIPTION("Intel PMC USB mux control");