Merge tag 'vfs-5.8-merge-1' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[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_scu_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
97 struct pmc_usb {
98         u8 num_ports;
99         struct device *dev;
100         struct intel_scu_ipc_dev *ipc;
101         struct pmc_usb_port *port;
102 };
103
104 static int pmc_usb_command(struct pmc_usb_port *port, u8 *msg, u32 len)
105 {
106         u8 response[4];
107
108         /*
109          * Error bit will always be 0 with the USBC command.
110          * Status can be checked from the response message.
111          */
112         intel_scu_ipc_dev_command(port->pmc->ipc, PMC_USBC_CMD, 0, msg, len,
113                                   response, sizeof(response));
114         if (response[2]) {
115                 if (response[2] & BIT(1))
116                         return -EIO;
117                 return -EBUSY;
118         }
119
120         return 0;
121 }
122
123 static int
124 pmc_usb_mux_dp_hpd(struct pmc_usb_port *port, struct typec_mux_state *state)
125 {
126         struct typec_displayport_data *data = state->data;
127         u8 msg[2] = { };
128
129         msg[0] = PMC_USB_DP_HPD;
130         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
131
132         msg[1] = PMC_USB_DP_HPD_IRQ;
133
134         if (data->status & DP_STATUS_HPD_STATE)
135                 msg[1] |= PMC_USB_DP_HPD_LVL;
136
137         return pmc_usb_command(port, msg, sizeof(msg));
138 }
139
140 static int
141 pmc_usb_mux_dp(struct pmc_usb_port *port, struct typec_mux_state *state)
142 {
143         struct typec_displayport_data *data = state->data;
144         struct altmode_req req = { };
145
146         if (data->status & DP_STATUS_IRQ_HPD)
147                 return pmc_usb_mux_dp_hpd(port, state);
148
149         req.usage = PMC_USB_ALT_MODE;
150         req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
151         req.mode_type = PMC_USB_MODE_TYPE_DP << PMC_USB_MODE_TYPE_SHIFT;
152
153         req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
154         req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
155         req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
156         req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
157
158         req.mode_data |= (state->mode - TYPEC_STATE_MODAL) <<
159                          PMC_USB_ALTMODE_DP_MODE_SHIFT;
160
161         if (data->status & DP_STATUS_HPD_STATE)
162                 req.mode_data |= PMC_USB_ALTMODE_HPD_HIGH;
163
164         return pmc_usb_command(port, (void *)&req, sizeof(req));
165 }
166
167 static int
168 pmc_usb_mux_tbt(struct pmc_usb_port *port, struct typec_mux_state *state)
169 {
170         struct typec_thunderbolt_data *data = state->data;
171         u8 cable_speed = TBT_CABLE_SPEED(data->cable_mode);
172         struct altmode_req req = { };
173
174         req.usage = PMC_USB_ALT_MODE;
175         req.usage |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
176         req.mode_type = PMC_USB_MODE_TYPE_TBT << PMC_USB_MODE_TYPE_SHIFT;
177
178         req.mode_data = (port->orientation - 1) << PMC_USB_ALTMODE_ORI_SHIFT;
179         req.mode_data |= (port->role - 1) << PMC_USB_ALTMODE_UFP_SHIFT;
180         req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_AUX_SHIFT;
181         req.mode_data |= (port->orientation - 1) << PMC_USB_ALTMODE_ORI_HSL_SHIFT;
182
183         if (TBT_ADAPTER(data->device_mode) == TBT_ADAPTER_TBT3)
184                 req.mode_data |= PMC_USB_ALTMODE_TBT_TYPE;
185
186         if (data->cable_mode & TBT_CABLE_OPTICAL)
187                 req.mode_data |= PMC_USB_ALTMODE_CABLE_TYPE;
188
189         if (data->cable_mode & TBT_CABLE_LINK_TRAINING)
190                 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_LINK;
191
192         if (data->enter_vdo & TBT_ENTER_MODE_ACTIVE_CABLE)
193                 req.mode_data |= PMC_USB_ALTMODE_ACTIVE_CABLE;
194
195         req.mode_data |= PMC_USB_ALTMODE_CABLE_SPD(cable_speed);
196
197         return pmc_usb_command(port, (void *)&req, sizeof(req));
198 }
199
200 static int pmc_usb_mux_safe_state(struct pmc_usb_port *port)
201 {
202         u8 msg;
203
204         msg = PMC_USB_SAFE_MODE;
205         msg |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
206
207         return pmc_usb_command(port, &msg, sizeof(msg));
208 }
209
210 static int pmc_usb_connect(struct pmc_usb_port *port)
211 {
212         u8 msg[2];
213
214         msg[0] = PMC_USB_CONNECT;
215         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
216
217         msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
218         msg[1] |= (port->orientation - 1) << PMC_USB_MSG_ORI_HSL_SHIFT;
219         msg[1] |= (port->orientation - 1) << PMC_USB_MSG_ORI_AUX_SHIFT;
220
221         return pmc_usb_command(port, msg, sizeof(msg));
222 }
223
224 static int pmc_usb_disconnect(struct pmc_usb_port *port)
225 {
226         u8 msg[2];
227
228         msg[0] = PMC_USB_DISCONNECT;
229         msg[0] |= port->usb3_port << PMC_USB_MSG_USB3_PORT_SHIFT;
230
231         msg[1] = port->usb2_port << PMC_USB_MSG_USB2_PORT_SHIFT;
232
233         return pmc_usb_command(port, msg, sizeof(msg));
234 }
235
236 static int
237 pmc_usb_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
238 {
239         struct pmc_usb_port *port = typec_mux_get_drvdata(mux);
240
241         if (!state->alt)
242                 return 0;
243
244         if (state->mode == TYPEC_STATE_SAFE)
245                 return pmc_usb_mux_safe_state(port);
246
247         switch (state->alt->svid) {
248         case USB_TYPEC_TBT_SID:
249                 return pmc_usb_mux_tbt(port, state);
250         case USB_TYPEC_DP_SID:
251                 return pmc_usb_mux_dp(port, state);
252         }
253
254         return -EOPNOTSUPP;
255 }
256
257 static int pmc_usb_set_orientation(struct typec_switch *sw,
258                                    enum typec_orientation orientation)
259 {
260         struct pmc_usb_port *port = typec_switch_get_drvdata(sw);
261
262         if (port->orientation == orientation)
263                 return 0;
264
265         port->orientation = orientation;
266
267         if (port->role) {
268                 if (orientation == TYPEC_ORIENTATION_NONE)
269                         return pmc_usb_disconnect(port);
270                 else
271                         return pmc_usb_connect(port);
272         }
273
274         return 0;
275 }
276
277 static int pmc_usb_set_role(struct usb_role_switch *sw, enum usb_role role)
278 {
279         struct pmc_usb_port *port = usb_role_switch_get_drvdata(sw);
280
281         if (port->role == role)
282                 return 0;
283
284         port->role = role;
285
286         if (port->orientation) {
287                 if (role == USB_ROLE_NONE)
288                         return pmc_usb_disconnect(port);
289                 else
290                         return pmc_usb_connect(port);
291         }
292
293         return 0;
294 }
295
296 static int pmc_usb_register_port(struct pmc_usb *pmc, int index,
297                                  struct fwnode_handle *fwnode)
298 {
299         struct pmc_usb_port *port = &pmc->port[index];
300         struct usb_role_switch_desc desc = { };
301         struct typec_switch_desc sw_desc = { };
302         struct typec_mux_desc mux_desc = { };
303         int ret;
304
305         ret = fwnode_property_read_u8(fwnode, "usb2-port-number", &port->usb2_port);
306         if (ret)
307                 return ret;
308
309         ret = fwnode_property_read_u8(fwnode, "usb3-port-number", &port->usb3_port);
310         if (ret)
311                 return ret;
312
313         port->num = index;
314         port->pmc = pmc;
315
316         sw_desc.fwnode = fwnode;
317         sw_desc.drvdata = port;
318         sw_desc.name = fwnode_get_name(fwnode);
319         sw_desc.set = pmc_usb_set_orientation;
320
321         port->typec_sw = typec_switch_register(pmc->dev, &sw_desc);
322         if (IS_ERR(port->typec_sw))
323                 return PTR_ERR(port->typec_sw);
324
325         mux_desc.fwnode = fwnode;
326         mux_desc.drvdata = port;
327         mux_desc.name = fwnode_get_name(fwnode);
328         mux_desc.set = pmc_usb_mux_set;
329
330         port->typec_mux = typec_mux_register(pmc->dev, &mux_desc);
331         if (IS_ERR(port->typec_mux)) {
332                 ret = PTR_ERR(port->typec_mux);
333                 goto err_unregister_switch;
334         }
335
336         desc.fwnode = fwnode;
337         desc.driver_data = port;
338         desc.name = fwnode_get_name(fwnode);
339         desc.set = pmc_usb_set_role;
340
341         port->usb_sw = usb_role_switch_register(pmc->dev, &desc);
342         if (IS_ERR(port->usb_sw)) {
343                 ret = PTR_ERR(port->usb_sw);
344                 goto err_unregister_mux;
345         }
346
347         return 0;
348
349 err_unregister_mux:
350         typec_mux_unregister(port->typec_mux);
351
352 err_unregister_switch:
353         typec_switch_unregister(port->typec_sw);
354
355         return ret;
356 }
357
358 static int pmc_usb_probe(struct platform_device *pdev)
359 {
360         struct fwnode_handle *fwnode = NULL;
361         struct pmc_usb *pmc;
362         int i = 0;
363         int ret;
364
365         pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
366         if (!pmc)
367                 return -ENOMEM;
368
369         device_for_each_child_node(&pdev->dev, fwnode)
370                 pmc->num_ports++;
371
372         pmc->port = devm_kcalloc(&pdev->dev, pmc->num_ports,
373                                  sizeof(struct pmc_usb_port), GFP_KERNEL);
374         if (!pmc->port)
375                 return -ENOMEM;
376
377         pmc->ipc = devm_intel_scu_ipc_dev_get(&pdev->dev);
378         if (!pmc->ipc)
379                 return -ENODEV;
380
381         pmc->dev = &pdev->dev;
382
383         /*
384          * For every physical USB connector (USB2 and USB3 combo) there is a
385          * child ACPI device node under the PMC mux ACPI device object.
386          */
387         for (i = 0; i < pmc->num_ports; i++) {
388                 fwnode = device_get_next_child_node(pmc->dev, fwnode);
389                 if (!fwnode)
390                         break;
391
392                 ret = pmc_usb_register_port(pmc, i, fwnode);
393                 if (ret)
394                         goto err_remove_ports;
395         }
396
397         platform_set_drvdata(pdev, pmc);
398
399         return 0;
400
401 err_remove_ports:
402         for (i = 0; i < pmc->num_ports; i++) {
403                 typec_switch_unregister(pmc->port[i].typec_sw);
404                 typec_mux_unregister(pmc->port[i].typec_mux);
405         }
406
407         return ret;
408 }
409
410 static int pmc_usb_remove(struct platform_device *pdev)
411 {
412         struct pmc_usb *pmc = platform_get_drvdata(pdev);
413         int i;
414
415         for (i = 0; i < pmc->num_ports; i++) {
416                 typec_switch_unregister(pmc->port[i].typec_sw);
417                 typec_mux_unregister(pmc->port[i].typec_mux);
418         }
419
420         return 0;
421 }
422
423 static const struct acpi_device_id pmc_usb_acpi_ids[] = {
424         { "INTC105C", },
425         { }
426 };
427 MODULE_DEVICE_TABLE(acpi, pmc_usb_acpi_ids);
428
429 static struct platform_driver pmc_usb_driver = {
430         .driver = {
431                 .name = "intel_pmc_usb",
432                 .acpi_match_table = ACPI_PTR(pmc_usb_acpi_ids),
433         },
434         .probe = pmc_usb_probe,
435         .remove = pmc_usb_remove,
436 };
437
438 module_platform_driver(pmc_usb_driver);
439
440 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
441 MODULE_LICENSE("GPL v2");
442 MODULE_DESCRIPTION("Intel PMC USB mux control");