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