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