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 
pmic_glink_ucsi_read(struct ucsi * __ucsi,unsigned int offset,void * val,size_t val_len)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 
pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi * ucsi,unsigned int offset,const void * val,size_t val_len)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 
pmic_glink_ucsi_async_write(struct ucsi * __ucsi,unsigned int offset,const void * val,size_t val_len)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 
pmic_glink_ucsi_sync_write(struct ucsi * __ucsi,unsigned int offset,const void * val,size_t val_len)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 		/* return 0 here and let core UCSI code handle the CCI_BUSY */
179 		ret = 0;
180 	} else if (ucsi->sync_val) {
181 		dev_err(ucsi->dev, "sync write returned: %d\n", ucsi->sync_val);
182 	}
183 
184 	ucsi->sync_pending = false;
185 
186 	return ret;
187 }
188 
189 static const struct ucsi_operations pmic_glink_ucsi_ops = {
190 	.read = pmic_glink_ucsi_read,
191 	.sync_write = pmic_glink_ucsi_sync_write,
192 	.async_write = pmic_glink_ucsi_async_write
193 };
194 
pmic_glink_ucsi_read_ack(struct pmic_glink_ucsi * ucsi,const void * data,int len)195 static void pmic_glink_ucsi_read_ack(struct pmic_glink_ucsi *ucsi, const void *data, int len)
196 {
197 	const struct ucsi_read_buf_resp_msg *resp = data;
198 
199 	if (resp->ret_code)
200 		return;
201 
202 	memcpy(ucsi->read_buf, resp->buf, UCSI_BUF_SIZE);
203 	complete(&ucsi->read_ack);
204 }
205 
pmic_glink_ucsi_write_ack(struct pmic_glink_ucsi * ucsi,const void * data,int len)206 static void pmic_glink_ucsi_write_ack(struct pmic_glink_ucsi *ucsi, const void *data, int len)
207 {
208 	const struct ucsi_write_buf_resp_msg *resp = data;
209 
210 	if (resp->ret_code)
211 		return;
212 
213 	ucsi->sync_val = resp->ret_code;
214 	complete(&ucsi->write_ack);
215 }
216 
pmic_glink_ucsi_notify(struct work_struct * work)217 static void pmic_glink_ucsi_notify(struct work_struct *work)
218 {
219 	struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, notify_work);
220 	unsigned int con_num;
221 	u32 cci;
222 	int ret;
223 
224 	ret = pmic_glink_ucsi_read(ucsi->ucsi, UCSI_CCI, &cci, sizeof(cci));
225 	if (ret) {
226 		dev_err(ucsi->dev, "failed to read CCI on notification\n");
227 		return;
228 	}
229 
230 	con_num = UCSI_CCI_CONNECTOR(cci);
231 	if (con_num) {
232 		if (con_num <= PMIC_GLINK_MAX_PORTS &&
233 		    ucsi->port_orientation[con_num - 1]) {
234 			int orientation = gpiod_get_value(ucsi->port_orientation[con_num - 1]);
235 
236 			if (orientation >= 0) {
237 				typec_switch_set(ucsi->port_switch[con_num - 1],
238 						 orientation ? TYPEC_ORIENTATION_REVERSE
239 							     : TYPEC_ORIENTATION_NORMAL);
240 			}
241 		}
242 
243 		ucsi_connector_change(ucsi->ucsi, con_num);
244 	}
245 
246 	if (ucsi->sync_pending &&
247 		   (cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))) {
248 		complete(&ucsi->sync_ack);
249 	}
250 }
251 
pmic_glink_ucsi_register(struct work_struct * work)252 static void pmic_glink_ucsi_register(struct work_struct *work)
253 {
254 	struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work);
255 	int orientation;
256 	int i;
257 
258 	for (i = 0; i < PMIC_GLINK_MAX_PORTS; i++) {
259 		if (!ucsi->port_orientation[i])
260 			continue;
261 		orientation = gpiod_get_value(ucsi->port_orientation[i]);
262 
263 		if (orientation >= 0) {
264 			typec_switch_set(ucsi->port_switch[i],
265 					 orientation ? TYPEC_ORIENTATION_REVERSE
266 					     : TYPEC_ORIENTATION_NORMAL);
267 		}
268 	}
269 
270 	ucsi_register(ucsi->ucsi);
271 }
272 
pmic_glink_ucsi_callback(const void * data,size_t len,void * priv)273 static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv)
274 {
275 	struct pmic_glink_ucsi *ucsi = priv;
276 	const struct pmic_glink_hdr *hdr = data;
277 
278 	switch (le32_to_cpu(hdr->opcode)) {
279 	case UC_UCSI_READ_BUF_REQ:
280 		pmic_glink_ucsi_read_ack(ucsi, data, len);
281 		break;
282 	case UC_UCSI_WRITE_BUF_REQ:
283 		pmic_glink_ucsi_write_ack(ucsi, data, len);
284 		break;
285 	case UC_UCSI_USBC_NOTIFY_IND:
286 		schedule_work(&ucsi->notify_work);
287 		break;
288 	};
289 }
290 
pmic_glink_ucsi_pdr_notify(void * priv,int state)291 static void pmic_glink_ucsi_pdr_notify(void *priv, int state)
292 {
293 	struct pmic_glink_ucsi *ucsi = priv;
294 
295 	if (state == SERVREG_SERVICE_STATE_UP)
296 		schedule_work(&ucsi->register_work);
297 	else if (state == SERVREG_SERVICE_STATE_DOWN)
298 		ucsi_unregister(ucsi->ucsi);
299 }
300 
pmic_glink_ucsi_destroy(void * data)301 static void pmic_glink_ucsi_destroy(void *data)
302 {
303 	struct pmic_glink_ucsi *ucsi = data;
304 
305 	/* Protect to make sure we're not in a middle of a transaction from a glink callback */
306 	mutex_lock(&ucsi->lock);
307 	ucsi_destroy(ucsi->ucsi);
308 	mutex_unlock(&ucsi->lock);
309 }
310 
pmic_glink_ucsi_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)311 static int pmic_glink_ucsi_probe(struct auxiliary_device *adev,
312 				 const struct auxiliary_device_id *id)
313 {
314 	struct pmic_glink_ucsi *ucsi;
315 	struct device *dev = &adev->dev;
316 	struct fwnode_handle *fwnode;
317 	int ret;
318 
319 	ucsi = devm_kzalloc(dev, sizeof(*ucsi), GFP_KERNEL);
320 	if (!ucsi)
321 		return -ENOMEM;
322 
323 	ucsi->dev = dev;
324 	dev_set_drvdata(dev, ucsi);
325 
326 	INIT_WORK(&ucsi->notify_work, pmic_glink_ucsi_notify);
327 	INIT_WORK(&ucsi->register_work, pmic_glink_ucsi_register);
328 	init_completion(&ucsi->read_ack);
329 	init_completion(&ucsi->write_ack);
330 	init_completion(&ucsi->sync_ack);
331 	mutex_init(&ucsi->lock);
332 
333 	ucsi->ucsi = ucsi_create(dev, &pmic_glink_ucsi_ops);
334 	if (IS_ERR(ucsi->ucsi))
335 		return PTR_ERR(ucsi->ucsi);
336 
337 	/* Make sure we destroy *after* pmic_glink unregister */
338 	ret = devm_add_action_or_reset(dev, pmic_glink_ucsi_destroy, ucsi);
339 	if (ret)
340 		return ret;
341 
342 	ucsi_set_drvdata(ucsi->ucsi, ucsi);
343 
344 	device_for_each_child_node(dev, fwnode) {
345 		struct gpio_desc *desc;
346 		u32 port;
347 
348 		ret = fwnode_property_read_u32(fwnode, "reg", &port);
349 		if (ret < 0) {
350 			dev_err(dev, "missing reg property of %pOFn\n", fwnode);
351 			return ret;
352 		}
353 
354 		if (port >= PMIC_GLINK_MAX_PORTS) {
355 			dev_warn(dev, "invalid connector number, ignoring\n");
356 			continue;
357 		}
358 
359 		desc = devm_gpiod_get_index_optional(&adev->dev, "orientation", port, GPIOD_IN);
360 
361 		/* If GPIO isn't found, continue */
362 		if (!desc)
363 			continue;
364 
365 		if (IS_ERR(desc))
366 			return dev_err_probe(dev, PTR_ERR(desc),
367 					     "unable to acquire orientation gpio\n");
368 		ucsi->port_orientation[port] = desc;
369 
370 		ucsi->port_switch[port] = fwnode_typec_switch_get(fwnode);
371 		if (IS_ERR(ucsi->port_switch[port]))
372 			return dev_err_probe(dev, PTR_ERR(ucsi->port_switch[port]),
373 					"failed to acquire orientation-switch\n");
374 	}
375 
376 	ucsi->client = devm_pmic_glink_register_client(dev,
377 						       PMIC_GLINK_OWNER_USBC,
378 						       pmic_glink_ucsi_callback,
379 						       pmic_glink_ucsi_pdr_notify,
380 						       ucsi);
381 	return PTR_ERR_OR_ZERO(ucsi->client);
382 }
383 
pmic_glink_ucsi_remove(struct auxiliary_device * adev)384 static void pmic_glink_ucsi_remove(struct auxiliary_device *adev)
385 {
386 	struct pmic_glink_ucsi *ucsi = dev_get_drvdata(&adev->dev);
387 
388 	/* Unregister first to stop having read & writes */
389 	ucsi_unregister(ucsi->ucsi);
390 }
391 
392 static const struct auxiliary_device_id pmic_glink_ucsi_id_table[] = {
393 	{ .name = "pmic_glink.ucsi", },
394 	{},
395 };
396 MODULE_DEVICE_TABLE(auxiliary, pmic_glink_ucsi_id_table);
397 
398 static struct auxiliary_driver pmic_glink_ucsi_driver = {
399 	.name = "pmic_glink_ucsi",
400 	.probe = pmic_glink_ucsi_probe,
401 	.remove = pmic_glink_ucsi_remove,
402 	.id_table = pmic_glink_ucsi_id_table,
403 };
404 
405 module_auxiliary_driver(pmic_glink_ucsi_driver);
406 
407 MODULE_DESCRIPTION("Qualcomm PMIC GLINK UCSI driver");
408 MODULE_LICENSE("GPL");
409