GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / usb / typec / ucsi / ucsi_glink.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2023, Linaro Ltd
5  */
6 #include <linux/auxiliary_bus.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/property.h>
10 #include <linux/soc/qcom/pdr.h>
11 #include <linux/usb/typec_mux.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/soc/qcom/pmic_glink.h>
14 #include "ucsi.h"
15
16 #define PMIC_GLINK_MAX_PORTS    2
17
18 #define UCSI_BUF_SIZE                   48
19
20 #define MSG_TYPE_REQ_RESP               1
21 #define UCSI_BUF_SIZE                   48
22
23 #define UC_NOTIFY_RECEIVER_UCSI         0x0
24 #define UC_UCSI_READ_BUF_REQ            0x11
25 #define UC_UCSI_WRITE_BUF_REQ           0x12
26 #define UC_UCSI_USBC_NOTIFY_IND         0x13
27
28 struct ucsi_read_buf_req_msg {
29         struct pmic_glink_hdr   hdr;
30 };
31
32 struct ucsi_read_buf_resp_msg {
33         struct pmic_glink_hdr   hdr;
34         u8                      buf[UCSI_BUF_SIZE];
35         u32                     ret_code;
36 };
37
38 struct ucsi_write_buf_req_msg {
39         struct pmic_glink_hdr   hdr;
40         u8                      buf[UCSI_BUF_SIZE];
41         u32                     reserved;
42 };
43
44 struct ucsi_write_buf_resp_msg {
45         struct pmic_glink_hdr   hdr;
46         u32                     ret_code;
47 };
48
49 struct ucsi_notify_ind_msg {
50         struct pmic_glink_hdr   hdr;
51         u32                     notification;
52         u32                     receiver;
53         u32                     reserved;
54 };
55
56 struct pmic_glink_ucsi {
57         struct device *dev;
58
59         struct gpio_desc *port_orientation[PMIC_GLINK_MAX_PORTS];
60         struct typec_switch *port_switch[PMIC_GLINK_MAX_PORTS];
61
62         struct pmic_glink_client *client;
63
64         struct ucsi *ucsi;
65         struct completion read_ack;
66         struct completion write_ack;
67         struct completion sync_ack;
68         bool sync_pending;
69         struct mutex lock;      /* protects concurrent access to PMIC Glink interface */
70
71         int sync_val;
72
73         struct work_struct notify_work;
74         struct work_struct register_work;
75
76         u8 read_buf[UCSI_BUF_SIZE];
77 };
78
79 static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset,
80                                 void *val, size_t val_len)
81 {
82         struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi);
83         struct ucsi_read_buf_req_msg req = {};
84         unsigned long left;
85         int ret;
86
87         req.hdr.owner = PMIC_GLINK_OWNER_USBC;
88         req.hdr.type = MSG_TYPE_REQ_RESP;
89         req.hdr.opcode = UC_UCSI_READ_BUF_REQ;
90
91         mutex_lock(&ucsi->lock);
92         memset(ucsi->read_buf, 0, sizeof(ucsi->read_buf));
93         reinit_completion(&ucsi->read_ack);
94
95         ret = pmic_glink_send(ucsi->client, &req, sizeof(req));
96         if (ret < 0) {
97                 dev_err(ucsi->dev, "failed to send UCSI read request: %d\n", ret);
98                 goto out_unlock;
99         }
100
101         left = wait_for_completion_timeout(&ucsi->read_ack, 5 * HZ);
102         if (!left) {
103                 dev_err(ucsi->dev, "timeout waiting for UCSI read response\n");
104                 ret = -ETIMEDOUT;
105                 goto out_unlock;
106         }
107
108         memcpy(val, &ucsi->read_buf[offset], val_len);
109         ret = 0;
110
111 out_unlock:
112         mutex_unlock(&ucsi->lock);
113
114         return ret;
115 }
116
117 static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned int offset,
118                                         const void *val, size_t val_len)
119 {
120         struct ucsi_write_buf_req_msg req = {};
121         unsigned long left;
122         int ret;
123
124         req.hdr.owner = PMIC_GLINK_OWNER_USBC;
125         req.hdr.type = MSG_TYPE_REQ_RESP;
126         req.hdr.opcode = UC_UCSI_WRITE_BUF_REQ;
127         memcpy(&req.buf[offset], val, val_len);
128
129         reinit_completion(&ucsi->write_ack);
130
131         ret = pmic_glink_send(ucsi->client, &req, sizeof(req));
132         if (ret < 0) {
133                 dev_err(ucsi->dev, "failed to send UCSI write request: %d\n", ret);
134                 return ret;
135         }
136
137         left = wait_for_completion_timeout(&ucsi->write_ack, 5 * HZ);
138         if (!left) {
139                 dev_err(ucsi->dev, "timeout waiting for UCSI write response\n");
140                 return -ETIMEDOUT;
141         }
142
143         return 0;
144 }
145
146 static int pmic_glink_ucsi_async_write(struct ucsi *__ucsi, unsigned int offset,
147                                        const void *val, size_t val_len)
148 {
149         struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi);
150         int ret;
151
152         mutex_lock(&ucsi->lock);
153         ret = pmic_glink_ucsi_locked_write(ucsi, offset, val, val_len);
154         mutex_unlock(&ucsi->lock);
155
156         return ret;
157 }
158
159 static int pmic_glink_ucsi_sync_write(struct ucsi *__ucsi, unsigned int offset,
160                                       const void *val, size_t val_len)
161 {
162         struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi);
163         unsigned long left;
164         int ret;
165
166         /* TOFIX: Downstream forces recipient to CON when UCSI_GET_ALTERNATE_MODES command */
167
168         mutex_lock(&ucsi->lock);
169         ucsi->sync_val = 0;
170         reinit_completion(&ucsi->sync_ack);
171         ucsi->sync_pending = true;
172         ret = pmic_glink_ucsi_locked_write(ucsi, offset, val, val_len);
173         mutex_unlock(&ucsi->lock);
174
175         left = wait_for_completion_timeout(&ucsi->sync_ack, 5 * HZ);
176         if (!left) {
177                 dev_err(ucsi->dev, "timeout waiting for UCSI sync write response\n");
178                 ret = -ETIMEDOUT;
179         } else if (ucsi->sync_val) {
180                 dev_err(ucsi->dev, "sync write returned: %d\n", ucsi->sync_val);
181         }
182
183         ucsi->sync_pending = false;
184
185         return ret;
186 }
187
188 static const struct ucsi_operations pmic_glink_ucsi_ops = {
189         .read = pmic_glink_ucsi_read,
190         .sync_write = pmic_glink_ucsi_sync_write,
191         .async_write = pmic_glink_ucsi_async_write
192 };
193
194 static void pmic_glink_ucsi_read_ack(struct pmic_glink_ucsi *ucsi, const void *data, int len)
195 {
196         const struct ucsi_read_buf_resp_msg *resp = data;
197
198         if (resp->ret_code)
199                 return;
200
201         memcpy(ucsi->read_buf, resp->buf, UCSI_BUF_SIZE);
202         complete(&ucsi->read_ack);
203 }
204
205 static void pmic_glink_ucsi_write_ack(struct pmic_glink_ucsi *ucsi, const void *data, int len)
206 {
207         const struct ucsi_write_buf_resp_msg *resp = data;
208
209         if (resp->ret_code)
210                 return;
211
212         ucsi->sync_val = resp->ret_code;
213         complete(&ucsi->write_ack);
214 }
215
216 static void pmic_glink_ucsi_notify(struct work_struct *work)
217 {
218         struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, notify_work);
219         unsigned int con_num;
220         u32 cci;
221         int ret;
222
223         ret = pmic_glink_ucsi_read(ucsi->ucsi, UCSI_CCI, &cci, sizeof(cci));
224         if (ret) {
225                 dev_err(ucsi->dev, "failed to read CCI on notification\n");
226                 return;
227         }
228
229         con_num = UCSI_CCI_CONNECTOR(cci);
230         if (con_num) {
231                 if (con_num <= PMIC_GLINK_MAX_PORTS &&
232                     ucsi->port_orientation[con_num - 1]) {
233                         int orientation = gpiod_get_value(ucsi->port_orientation[con_num - 1]);
234
235                         if (orientation >= 0) {
236                                 typec_switch_set(ucsi->port_switch[con_num - 1],
237                                                  orientation ? TYPEC_ORIENTATION_REVERSE
238                                                              : TYPEC_ORIENTATION_NORMAL);
239                         }
240                 }
241
242                 ucsi_connector_change(ucsi->ucsi, con_num);
243         }
244
245         if (ucsi->sync_pending && cci & UCSI_CCI_BUSY) {
246                 ucsi->sync_val = -EBUSY;
247                 complete(&ucsi->sync_ack);
248         } else if (ucsi->sync_pending &&
249                    (cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))) {
250                 complete(&ucsi->sync_ack);
251         }
252 }
253
254 static void pmic_glink_ucsi_register(struct work_struct *work)
255 {
256         struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work);
257
258         ucsi_register(ucsi->ucsi);
259 }
260
261 static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv)
262 {
263         struct pmic_glink_ucsi *ucsi = priv;
264         const struct pmic_glink_hdr *hdr = data;
265
266         switch (le32_to_cpu(hdr->opcode)) {
267         case UC_UCSI_READ_BUF_REQ:
268                 pmic_glink_ucsi_read_ack(ucsi, data, len);
269                 break;
270         case UC_UCSI_WRITE_BUF_REQ:
271                 pmic_glink_ucsi_write_ack(ucsi, data, len);
272                 break;
273         case UC_UCSI_USBC_NOTIFY_IND:
274                 schedule_work(&ucsi->notify_work);
275                 break;
276         };
277 }
278
279 static void pmic_glink_ucsi_pdr_notify(void *priv, int state)
280 {
281         struct pmic_glink_ucsi *ucsi = priv;
282
283         if (state == SERVREG_SERVICE_STATE_UP)
284                 schedule_work(&ucsi->register_work);
285         else if (state == SERVREG_SERVICE_STATE_DOWN)
286                 ucsi_unregister(ucsi->ucsi);
287 }
288
289 static void pmic_glink_ucsi_destroy(void *data)
290 {
291         struct pmic_glink_ucsi *ucsi = data;
292
293         /* Protect to make sure we're not in a middle of a transaction from a glink callback */
294         mutex_lock(&ucsi->lock);
295         ucsi_destroy(ucsi->ucsi);
296         mutex_unlock(&ucsi->lock);
297 }
298
299 static int pmic_glink_ucsi_probe(struct auxiliary_device *adev,
300                                  const struct auxiliary_device_id *id)
301 {
302         struct pmic_glink_ucsi *ucsi;
303         struct device *dev = &adev->dev;
304         struct fwnode_handle *fwnode;
305         int ret;
306
307         ucsi = devm_kzalloc(dev, sizeof(*ucsi), GFP_KERNEL);
308         if (!ucsi)
309                 return -ENOMEM;
310
311         ucsi->dev = dev;
312         dev_set_drvdata(dev, ucsi);
313
314         INIT_WORK(&ucsi->notify_work, pmic_glink_ucsi_notify);
315         INIT_WORK(&ucsi->register_work, pmic_glink_ucsi_register);
316         init_completion(&ucsi->read_ack);
317         init_completion(&ucsi->write_ack);
318         init_completion(&ucsi->sync_ack);
319         mutex_init(&ucsi->lock);
320
321         ucsi->ucsi = ucsi_create(dev, &pmic_glink_ucsi_ops);
322         if (IS_ERR(ucsi->ucsi))
323                 return PTR_ERR(ucsi->ucsi);
324
325         /* Make sure we destroy *after* pmic_glink unregister */
326         ret = devm_add_action_or_reset(dev, pmic_glink_ucsi_destroy, ucsi);
327         if (ret)
328                 return ret;
329
330         ucsi_set_drvdata(ucsi->ucsi, ucsi);
331
332         device_for_each_child_node(dev, fwnode) {
333                 struct gpio_desc *desc;
334                 u32 port;
335
336                 ret = fwnode_property_read_u32(fwnode, "reg", &port);
337                 if (ret < 0) {
338                         dev_err(dev, "missing reg property of %pOFn\n", fwnode);
339                         return ret;
340                 }
341
342                 if (port >= PMIC_GLINK_MAX_PORTS) {
343                         dev_warn(dev, "invalid connector number, ignoring\n");
344                         continue;
345                 }
346
347                 desc = devm_gpiod_get_index_optional(&adev->dev, "orientation", port, GPIOD_IN);
348
349                 /* If GPIO isn't found, continue */
350                 if (!desc)
351                         continue;
352
353                 if (IS_ERR(desc))
354                         return dev_err_probe(dev, PTR_ERR(desc),
355                                              "unable to acquire orientation gpio\n");
356                 ucsi->port_orientation[port] = desc;
357
358                 ucsi->port_switch[port] = fwnode_typec_switch_get(fwnode);
359                 if (IS_ERR(ucsi->port_switch[port]))
360                         return dev_err_probe(dev, PTR_ERR(ucsi->port_switch[port]),
361                                         "failed to acquire orientation-switch\n");
362         }
363
364         ucsi->client = devm_pmic_glink_register_client(dev,
365                                                        PMIC_GLINK_OWNER_USBC,
366                                                        pmic_glink_ucsi_callback,
367                                                        pmic_glink_ucsi_pdr_notify,
368                                                        ucsi);
369         return PTR_ERR_OR_ZERO(ucsi->client);
370 }
371
372 static void pmic_glink_ucsi_remove(struct auxiliary_device *adev)
373 {
374         struct pmic_glink_ucsi *ucsi = dev_get_drvdata(&adev->dev);
375
376         /* Unregister first to stop having read & writes */
377         ucsi_unregister(ucsi->ucsi);
378 }
379
380 static const struct auxiliary_device_id pmic_glink_ucsi_id_table[] = {
381         { .name = "pmic_glink.ucsi", },
382         {},
383 };
384 MODULE_DEVICE_TABLE(auxiliary, pmic_glink_ucsi_id_table);
385
386 static struct auxiliary_driver pmic_glink_ucsi_driver = {
387         .name = "pmic_glink_ucsi",
388         .probe = pmic_glink_ucsi_probe,
389         .remove = pmic_glink_ucsi_remove,
390         .id_table = pmic_glink_ucsi_id_table,
391 };
392
393 module_auxiliary_driver(pmic_glink_ucsi_driver);
394
395 MODULE_DESCRIPTION("Qualcomm PMIC GLINK UCSI driver");
396 MODULE_LICENSE("GPL");