xref: /linux/drivers/usb/typec/ucsi/cros_ec_ucsi.c (revision 40b17a345d3fe88b98acfe2637452baa32785ee0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * UCSI driver for ChromeOS EC
4  *
5  * Copyright 2024 Google LLC.
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/container_of.h>
10 #include <linux/dev_printk.h>
11 #include <linux/jiffies.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_data/cros_ec_commands.h>
16 #include <linux/platform_data/cros_usbpd_notify.h>
17 #include <linux/platform_data/cros_ec_proto.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/wait.h>
21 #include <linux/usb/typec_altmode.h>
22 
23 #include "ucsi.h"
24 
25 /*
26  * Maximum size in bytes of a UCSI message between AP and EC
27  */
28 #define MAX_EC_DATA_SIZE	256
29 
30 /*
31  * Maximum time in milliseconds the cros_ec_ucsi driver
32  * will wait for a response to a command or and ack.
33  */
34 #define WRITE_TMO_MS		5000
35 
36 /* Number of times to attempt recovery from a write timeout before giving up. */
37 #define WRITE_TMO_CTR_MAX	5
38 
39 /* Delay between mode entry/exit attempts, ms */
40 static const unsigned int mode_selection_delay = 1000;
41 /* Timeout for a mode entry attempt, ms */
42 static const unsigned int mode_selection_timeout = 4000;
43 
44 struct cros_ucsi_data {
45 	struct device *dev;
46 	struct ucsi *ucsi;
47 
48 	struct cros_ec_device *ec;
49 	struct notifier_block nb;
50 	struct work_struct work;
51 	struct delayed_work write_tmo;
52 	int tmo_counter;
53 
54 	struct completion complete;
55 	unsigned long flags;
56 };
57 
58 static int cros_ucsi_read(struct ucsi *ucsi, unsigned int offset, void *val,
59 			  size_t val_len)
60 {
61 	struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi);
62 	struct ec_params_ucsi_ppm_get req = {
63 		.offset = offset,
64 		.size = val_len,
65 	};
66 	int ret;
67 
68 	if (val_len > MAX_EC_DATA_SIZE) {
69 		dev_err(udata->dev, "Can't read %zu bytes. Too big.\n", val_len);
70 		return -EINVAL;
71 	}
72 
73 	ret = cros_ec_cmd(udata->ec, 0, EC_CMD_UCSI_PPM_GET,
74 			  &req, sizeof(req), val, val_len);
75 	if (ret < 0) {
76 		dev_warn(udata->dev, "Failed to send EC message UCSI_PPM_GET: error=%d\n", ret);
77 		return ret;
78 	}
79 	return 0;
80 }
81 
82 static int cros_ucsi_read_version(struct ucsi *ucsi, u16 *version)
83 {
84 	return cros_ucsi_read(ucsi, UCSI_VERSION, version, sizeof(*version));
85 }
86 
87 static int cros_ucsi_read_cci(struct ucsi *ucsi, u32 *cci)
88 {
89 	return cros_ucsi_read(ucsi, UCSI_CCI, cci, sizeof(*cci));
90 }
91 
92 static int cros_ucsi_read_message_in(struct ucsi *ucsi, void *val,
93 				     size_t val_len)
94 {
95 	return cros_ucsi_read(ucsi, UCSI_MESSAGE_IN, val, val_len);
96 }
97 
98 static int cros_ucsi_async_control(struct ucsi *ucsi, u64 cmd)
99 {
100 	struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi);
101 	u8 ec_buf[sizeof(struct ec_params_ucsi_ppm_set) + sizeof(cmd)];
102 	struct ec_params_ucsi_ppm_set *req = (struct ec_params_ucsi_ppm_set *) ec_buf;
103 	int ret;
104 
105 	req->offset = UCSI_CONTROL;
106 	memcpy(req->data, &cmd, sizeof(cmd));
107 	ret = cros_ec_cmd(udata->ec, 0, EC_CMD_UCSI_PPM_SET,
108 			  req, sizeof(ec_buf), NULL, 0);
109 	if (ret < 0) {
110 		dev_warn(udata->dev, "Failed to send EC message UCSI_PPM_SET: error=%d\n", ret);
111 		return ret;
112 	}
113 	return 0;
114 }
115 
116 static int cros_ucsi_sync_control(struct ucsi *ucsi, u64 cmd, u32 *cci,
117 				  void *data, size_t size)
118 {
119 	struct cros_ucsi_data *udata = ucsi_get_drvdata(ucsi);
120 	int ret;
121 
122 	ret = ucsi_sync_control_common(ucsi, cmd, cci, data, size);
123 	switch (ret) {
124 	case -EBUSY:
125 		/* EC may return -EBUSY if CCI.busy is set.
126 		 * Convert this to a timeout.
127 		 */
128 	case -ETIMEDOUT:
129 		/* Schedule recovery attempt when we timeout
130 		 * or tried to send a command while still busy.
131 		 */
132 		cancel_delayed_work_sync(&udata->write_tmo);
133 		schedule_delayed_work(&udata->write_tmo,
134 				      msecs_to_jiffies(WRITE_TMO_MS));
135 		break;
136 	case 0:
137 		/* Successful write. Cancel any pending recovery work. */
138 		cancel_delayed_work_sync(&udata->write_tmo);
139 		break;
140 	}
141 
142 	return ret;
143 }
144 
145 static void cros_ucsi_add_partner_altmodes(struct ucsi_connector *con)
146 {
147 	if (!con->typec_cap.no_mode_control)
148 		typec_mode_selection_start(con->partner,
149 					   mode_selection_delay,
150 					   mode_selection_timeout);
151 }
152 
153 static void cros_ucsi_remove_partner_altmodes(struct ucsi_connector *con)
154 {
155 	if (!con->typec_cap.no_mode_control)
156 		typec_mode_selection_delete(con->partner);
157 }
158 
159 static const struct ucsi_operations cros_ucsi_ops = {
160 	.read_version = cros_ucsi_read_version,
161 	.read_cci = cros_ucsi_read_cci,
162 	.poll_cci = cros_ucsi_read_cci,
163 	.read_message_in = cros_ucsi_read_message_in,
164 	.async_control = cros_ucsi_async_control,
165 	.sync_control = cros_ucsi_sync_control,
166 	.add_partner_altmodes = cros_ucsi_add_partner_altmodes,
167 	.remove_partner_altmodes = cros_ucsi_remove_partner_altmodes,
168 };
169 
170 static void cros_ucsi_work(struct work_struct *work)
171 {
172 	struct cros_ucsi_data *udata = container_of(work, struct cros_ucsi_data, work);
173 	u32 cci;
174 
175 	if (cros_ucsi_read_cci(udata->ucsi, &cci))
176 		return;
177 
178 	ucsi_notify_common(udata->ucsi, cci);
179 }
180 
181 static void cros_ucsi_write_timeout(struct work_struct *work)
182 {
183 	struct cros_ucsi_data *udata =
184 		container_of(work, struct cros_ucsi_data, write_tmo.work);
185 	u32 cci;
186 	u64 cmd;
187 
188 	if (cros_ucsi_read(udata->ucsi, UCSI_CCI, &cci, sizeof(cci))) {
189 		dev_err(udata->dev,
190 			"Reading CCI failed; no write timeout recovery possible.\n");
191 		return;
192 	}
193 
194 	if (cci & UCSI_CCI_BUSY) {
195 		udata->tmo_counter++;
196 
197 		if (udata->tmo_counter <= WRITE_TMO_CTR_MAX)
198 			schedule_delayed_work(&udata->write_tmo,
199 					      msecs_to_jiffies(WRITE_TMO_MS));
200 		else
201 			dev_err(udata->dev,
202 				"PPM unresponsive - too many write timeouts.\n");
203 
204 		return;
205 	}
206 
207 	/* No longer busy means we can reset our timeout counter. */
208 	udata->tmo_counter = 0;
209 
210 	/* Need to ack previous command which may have timed out. */
211 	if (cci & UCSI_CCI_COMMAND_COMPLETE) {
212 		cmd = UCSI_ACK_CC_CI | UCSI_ACK_COMMAND_COMPLETE;
213 		cros_ucsi_async_control(udata->ucsi, cmd);
214 
215 		/* Check again after a few seconds that the system has
216 		 * recovered to make sure our async write above was successful.
217 		 */
218 		schedule_delayed_work(&udata->write_tmo,
219 				      msecs_to_jiffies(WRITE_TMO_MS));
220 		return;
221 	}
222 
223 	/* We recovered from a previous timeout. Treat this as a recovery from
224 	 * suspend and call resume.
225 	 */
226 	ucsi_resume(udata->ucsi);
227 }
228 
229 static int cros_ucsi_event(struct notifier_block *nb,
230 			   unsigned long host_event, void *_notify)
231 {
232 	struct cros_ucsi_data *udata = container_of(nb, struct cros_ucsi_data, nb);
233 
234 	if (host_event & PD_EVENT_INIT) {
235 		/* Late init event received from ChromeOS EC. Treat this as a
236 		 * system resume to re-enable communication with the PPM.
237 		 */
238 		dev_dbg(udata->dev, "Late PD init received\n");
239 		ucsi_resume(udata->ucsi);
240 	}
241 
242 	if (host_event & PD_EVENT_PPM) {
243 		dev_dbg(udata->dev, "UCSI notification received\n");
244 		flush_work(&udata->work);
245 		schedule_work(&udata->work);
246 	}
247 
248 	return NOTIFY_OK;
249 }
250 
251 static void cros_ucsi_destroy(struct cros_ucsi_data *udata)
252 {
253 	cros_usbpd_unregister_notify(&udata->nb);
254 	cancel_delayed_work_sync(&udata->write_tmo);
255 	cancel_work_sync(&udata->work);
256 	ucsi_destroy(udata->ucsi);
257 }
258 
259 static int cros_ucsi_probe(struct platform_device *pdev)
260 {
261 	struct device *dev = &pdev->dev;
262 	struct cros_ucsi_data *udata;
263 	int ret;
264 
265 	udata = devm_kzalloc(dev, sizeof(*udata), GFP_KERNEL);
266 	if (!udata)
267 		return -ENOMEM;
268 
269 	/* ACPI and OF FW nodes for cros_ec_ucsi are children of the ChromeOS EC. If the
270 	 * cros_ec_ucsi device has an ACPI or OF FW node, its parent is the ChromeOS EC device.
271 	 * Platforms without a FW node for cros_ec_ucsi may add it as a subdevice of cros_ec_dev.
272 	 */
273 	udata->dev = dev;
274 	if (is_acpi_device_node(dev->fwnode) || is_of_node(dev->fwnode))
275 		udata->ec = dev_get_drvdata(dev->parent);
276 	else
277 		udata->ec = ((struct cros_ec_dev *)dev_get_drvdata(dev->parent))->ec_dev;
278 
279 	if (!udata->ec)
280 		return dev_err_probe(dev, -ENODEV, "couldn't find parent EC device\n");
281 
282 	platform_set_drvdata(pdev, udata);
283 
284 	INIT_WORK(&udata->work, cros_ucsi_work);
285 	INIT_DELAYED_WORK(&udata->write_tmo, cros_ucsi_write_timeout);
286 	init_completion(&udata->complete);
287 
288 	udata->ucsi = ucsi_create(dev, &cros_ucsi_ops);
289 	if (IS_ERR(udata->ucsi))
290 		return dev_err_probe(dev, PTR_ERR(udata->ucsi), "failed to allocate UCSI instance\n");
291 
292 	ucsi_set_drvdata(udata->ucsi, udata);
293 
294 	udata->nb.notifier_call = cros_ucsi_event;
295 	ret = cros_usbpd_register_notify(&udata->nb);
296 	if (ret) {
297 		dev_err_probe(dev, ret, "failed to register notifier\n");
298 		ucsi_destroy(udata->ucsi);
299 		return ret;
300 	}
301 
302 	ret = ucsi_register(udata->ucsi);
303 	if (ret) {
304 		dev_err_probe(dev, ret, "failed to register UCSI\n");
305 		cros_ucsi_destroy(udata);
306 		return ret;
307 	}
308 
309 	return 0;
310 }
311 
312 static void cros_ucsi_remove(struct platform_device *dev)
313 {
314 	struct cros_ucsi_data *udata = platform_get_drvdata(dev);
315 
316 	ucsi_unregister(udata->ucsi);
317 	cros_ucsi_destroy(udata);
318 }
319 
320 static int __maybe_unused cros_ucsi_suspend(struct device *dev)
321 {
322 	struct cros_ucsi_data *udata = dev_get_drvdata(dev);
323 
324 	cancel_delayed_work_sync(&udata->write_tmo);
325 	cancel_work_sync(&udata->work);
326 
327 	return 0;
328 }
329 
330 static void __maybe_unused cros_ucsi_complete(struct device *dev)
331 {
332 	struct cros_ucsi_data *udata = dev_get_drvdata(dev);
333 
334 	ucsi_resume(udata->ucsi);
335 }
336 
337 /*
338  * UCSI protocol is also used on ChromeOS platforms which reply on
339  * cros_ec_lpc.c driver for communication with embedded controller (EC).
340  * On such platforms communication with the EC is not available until
341  * the .complete() callback of the cros_ec_lpc driver is executed.
342  * For this reason we delay ucsi_resume() until the .complete() stage
343  * otherwise UCSI SET_NOTIFICATION_ENABLE command will fail and we won't
344  * receive any UCSI notifications from the EC where PPM is implemented.
345  */
346 static const struct dev_pm_ops cros_ucsi_pm_ops = {
347 #ifdef CONFIG_PM_SLEEP
348 	.suspend = cros_ucsi_suspend,
349 	.complete = cros_ucsi_complete,
350 #endif
351 };
352 
353 static const struct platform_device_id cros_ucsi_id[] = {
354 	{ KBUILD_MODNAME, 0 },
355 	{}
356 };
357 MODULE_DEVICE_TABLE(platform, cros_ucsi_id);
358 
359 static const struct acpi_device_id cros_ec_ucsi_acpi_device_ids[] = {
360 	{ "GOOG0021", 0 },
361 	{ }
362 };
363 MODULE_DEVICE_TABLE(acpi, cros_ec_ucsi_acpi_device_ids);
364 
365 static const struct of_device_id cros_ucsi_of_match[] = {
366 	{ .compatible = "google,cros-ec-ucsi", },
367 	{}
368 };
369 MODULE_DEVICE_TABLE(of, cros_ucsi_of_match);
370 
371 static struct platform_driver cros_ucsi_driver = {
372 	.driver = {
373 		.name = KBUILD_MODNAME,
374 		.pm = &cros_ucsi_pm_ops,
375 		.acpi_match_table = cros_ec_ucsi_acpi_device_ids,
376 		.of_match_table = cros_ucsi_of_match,
377 	},
378 	.id_table = cros_ucsi_id,
379 	.probe = cros_ucsi_probe,
380 	.remove = cros_ucsi_remove,
381 };
382 
383 module_platform_driver(cros_ucsi_driver);
384 
385 MODULE_LICENSE("GPL");
386 MODULE_DESCRIPTION("UCSI driver for ChromeOS EC");
387