2 * USB Type-C Connector System Software Interface driver
4 * Copyright (C) 2016, Intel Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/platform_device.h>
13 #include <linux/module.h>
14 #include <linux/delay.h>
15 #include <linux/acpi.h>
19 /* Double the time defined by MIN_TIME_TO_RESPOND_WITH_BUSY */
20 #define UCSI_TIMEOUT_MS 20
28 struct ucsi_connector {
31 struct work_struct work;
32 struct ucsi_connector_capability cap;
37 struct ucsi_data __iomem *data;
39 enum ucsi_status status;
40 struct completion complete;
41 struct ucsi_capability cap;
42 struct ucsi_connector *connector;
47 /* PPM Communication lock */
48 struct mutex ppm_lock;
50 /* PPM communication flags */
52 #define EVENT_PENDING 0
53 #define COMMAND_PENDING 1
56 static int ucsi_acpi_cmd(struct ucsi *ucsi, struct ucsi_control *ctrl)
58 uuid_le uuid = UUID_LE(0x6f8398c2, 0x7ca4, 0x11e4,
59 0xad, 0x36, 0x63, 0x10, 0x42, 0xb5, 0x00, 0x8f);
60 union acpi_object *obj;
62 ucsi->data->ctrl.raw_cmd = ctrl->raw_cmd;
64 obj = acpi_evaluate_dsm(ACPI_HANDLE(ucsi->dev), uuid.b, 1, 1, NULL);
66 dev_err(ucsi->dev, "%s: failed to evaluate _DSM\n", __func__);
74 static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
76 struct ucsi *ucsi = data;
79 spin_lock(&ucsi->dev_lock);
81 ucsi->status = UCSI_IDLE;
82 cci = &ucsi->data->cci;
85 * REVISIT: This is not documented behavior, but all known PPMs ACK
86 * asynchronous events by sending notification with cleared CCI.
88 if (!ucsi->data->raw_cci) {
89 if (test_bit(EVENT_PENDING, &ucsi->flags))
90 complete(&ucsi->complete);
92 dev_WARN(ucsi->dev, "spurious notification\n");
96 if (test_bit(COMMAND_PENDING, &ucsi->flags)) {
98 ucsi->status = UCSI_BUSY;
99 complete(&ucsi->complete);
102 } else if (cci->ack_complete || cci->cmd_complete) {
103 /* Error Indication is only valid with commands */
104 if (cci->error && cci->cmd_complete)
105 ucsi->status = UCSI_ERROR;
107 ucsi->data->ctrl.raw_cmd = 0;
108 complete(&ucsi->complete);
112 if (cci->connector_change) {
113 struct ucsi_connector *con;
116 * This is workaround for buggy PPMs that create asynchronous
117 * event notifications before OPM has enabled them.
119 if (!ucsi->connector)
122 con = ucsi->connector + (cci->connector_change - 1);
125 * PPM will not clear the connector specific bit in Connector
126 * Change Indication field of CCI until the driver has ACK it,
127 * and the driver can not ACK it before it has been processed.
128 * The PPM will not generate new events before the first has
129 * been acknowledged, even if they are for an other connector.
130 * So only one event at a time.
132 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
133 schedule_work(&con->work);
136 spin_unlock(&ucsi->dev_lock);
139 static int ucsi_ack(struct ucsi *ucsi, u8 cmd)
141 struct ucsi_control ctrl;
144 ctrl.cmd.cmd = UCSI_ACK_CC_CI;
147 ret = ucsi_acpi_cmd(ucsi, &ctrl);
151 /* Waiting for ACK also with ACK CMD for now */
152 ret = wait_for_completion_timeout(&ucsi->complete,
153 msecs_to_jiffies(UCSI_TIMEOUT_MS));
159 static int ucsi_run_cmd(struct ucsi *ucsi, struct ucsi_control *ctrl,
160 void *data, size_t size)
165 set_bit(COMMAND_PENDING, &ucsi->flags);
167 ret = ucsi_acpi_cmd(ucsi, ctrl);
171 ret = wait_for_completion_timeout(&ucsi->complete,
172 msecs_to_jiffies(UCSI_TIMEOUT_MS));
178 switch (ucsi->status) {
181 memcpy(data, ucsi->data->message_in, size);
183 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
186 /* The caller decides whether to cancel or not */
190 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
194 ctrl->cmd.cmd = UCSI_GET_ERROR_STATUS;
195 ctrl->cmd.length = 0;
197 ret = ucsi_acpi_cmd(ucsi, ctrl);
201 ret = wait_for_completion_timeout(&ucsi->complete,
202 msecs_to_jiffies(UCSI_TIMEOUT_MS));
208 memcpy(&err_value, ucsi->data->message_in, sizeof(err_value));
210 /* Something has really gone wrong */
211 if (WARN_ON(ucsi->status == UCSI_ERROR)) {
216 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
221 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
224 case UCSI_ERROR_CC_COMMUNICATION_ERR:
227 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
230 case UCSI_ERROR_DEAD_BATTERY:
231 dev_warn(ucsi->dev, "Dead battery condition!\n");
234 /* The following mean a bug in this driver */
235 case UCSI_ERROR_INVALID_CON_NUM:
236 case UCSI_ERROR_UNREGONIZED_CMD:
237 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
240 "%s: possible UCSI driver bug - error %hu\n",
241 __func__, err_value);
249 clear_bit(COMMAND_PENDING, &ucsi->flags);
253 static void ucsi_connector_change(struct work_struct *work)
255 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
257 struct ucsi_connector_status constat;
258 struct ucsi *ucsi = con->ucsi;
259 struct ucsi_control ctrl;
262 mutex_lock(&ucsi->ppm_lock);
264 ctrl.cmd.cmd = UCSI_GET_CONNECTOR_STATUS;
266 ctrl.cmd.data = con->num;
267 ret = ucsi_run_cmd(con->ucsi, &ctrl, &constat, sizeof(constat));
269 dev_err(ucsi->dev, "%s: failed to read connector status (%d)\n",
274 /* Ignoring disconnections and Alternate Modes */
275 if (!constat.connected || !(constat.change &
276 (UCSI_CONSTAT_PARTNER_CHANGE | UCSI_CONSTAT_CONNECT_CHANGE)) ||
277 constat.partner_flags & UCSI_CONSTAT_PARTNER_FLAG_ALT_MODE)
280 /* If the partner got USB Host role, attempting swap */
281 if (constat.partner_type & UCSI_CONSTAT_PARTNER_TYPE_DFP) {
282 ctrl.uor.cmd = UCSI_SET_UOR;
283 ctrl.uor.con_num = con->num;
284 ctrl.uor.role = UCSI_UOR_ROLE_DFP;
286 ret = ucsi_run_cmd(con->ucsi, &ctrl, NULL, 0);
288 dev_err(ucsi->dev, "%s: failed to swap role (%d)\n",
292 ucsi_ack(ucsi, UCSI_ACK_EVENT);
293 clear_bit(EVENT_PENDING, &ucsi->flags);
294 mutex_unlock(&ucsi->ppm_lock);
297 static int ucsi_reset_ppm(struct ucsi *ucsi)
299 int timeout = UCSI_TIMEOUT_MS;
300 struct ucsi_control ctrl;
303 memset(&ctrl, 0, sizeof(ctrl));
304 ctrl.cmd.cmd = UCSI_PPM_RESET;
305 ret = ucsi_acpi_cmd(ucsi, &ctrl);
309 /* There is no quarantee the PPM will ever set the RESET_COMPLETE bit */
310 while (!ucsi->data->cci.reset_complete && timeout--)
311 usleep_range(1000, 2000);
315 static int ucsi_init(struct ucsi *ucsi)
317 struct ucsi_connector *con;
318 struct ucsi_control ctrl;
322 init_completion(&ucsi->complete);
323 spin_lock_init(&ucsi->dev_lock);
324 mutex_init(&ucsi->ppm_lock);
327 ret = ucsi_reset_ppm(ucsi);
332 * REVISIT: Executing second reset to WA an issue seen on some of the
333 * Broxton based platforms, where the first reset puts the PPM into a
334 * state where it's unable to recognise some of the commands.
336 ret = ucsi_reset_ppm(ucsi);
340 mutex_lock(&ucsi->ppm_lock);
342 /* Enable basic notifications */
343 ctrl.cmd.cmd = UCSI_SET_NOTIFICATION_ENABLE;
345 ctrl.cmd.data = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
346 ret = ucsi_run_cmd(ucsi, &ctrl, NULL, 0);
350 /* Get PPM capabilities */
351 ctrl.cmd.cmd = UCSI_GET_CAPABILITY;
352 ret = ucsi_run_cmd(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
356 if (!ucsi->cap.num_connectors) {
361 ucsi->connector = devm_kcalloc(ucsi->dev, ucsi->cap.num_connectors,
362 sizeof(*ucsi->connector), GFP_KERNEL);
363 if (!ucsi->connector) {
368 for (i = 1, con = ucsi->connector; i < ucsi->cap.num_connectors + 1;
370 /* Get connector capability */
371 ctrl.cmd.cmd = UCSI_GET_CONNECTOR_CAPABILITY;
373 ret = ucsi_run_cmd(ucsi, &ctrl, &con->cap, sizeof(con->cap));
379 INIT_WORK(&con->work, ucsi_connector_change);
382 /* Enable all notifications */
383 ctrl.cmd.cmd = UCSI_SET_NOTIFICATION_ENABLE;
384 ctrl.cmd.data = UCSI_ENABLE_NTFY_ALL;
385 ret = ucsi_run_cmd(ucsi, &ctrl, NULL, 0);
389 mutex_unlock(&ucsi->ppm_lock);
392 ucsi_reset_ppm(ucsi);
393 mutex_unlock(&ucsi->ppm_lock);
397 static int ucsi_acpi_probe(struct platform_device *pdev)
399 struct resource *res;
404 ucsi = devm_kzalloc(&pdev->dev, sizeof(*ucsi), GFP_KERNEL);
408 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
410 dev_err(&pdev->dev, "missing memory resource\n");
415 * NOTE: ACPI has claimed the memory region as it's also an Operation
416 * Region. It's not possible to request it in the driver.
418 ucsi->data = devm_ioremap(&pdev->dev, res->start, resource_size(res));
422 ucsi->dev = &pdev->dev;
424 status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
426 ucsi_acpi_notify, ucsi);
427 if (ACPI_FAILURE(status))
430 ret = ucsi_init(ucsi);
432 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
438 platform_set_drvdata(pdev, ucsi);
442 static int ucsi_acpi_remove(struct platform_device *pdev)
444 struct ucsi *ucsi = platform_get_drvdata(pdev);
446 acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
447 ACPI_ALL_NOTIFY, ucsi_acpi_notify);
449 /* Make sure there are no events in the middle of being processed */
450 if (wait_on_bit_timeout(&ucsi->flags, EVENT_PENDING,
451 TASK_UNINTERRUPTIBLE,
452 msecs_to_jiffies(UCSI_TIMEOUT_MS)))
453 dev_WARN(ucsi->dev, "%s: Events still pending\n", __func__);
455 ucsi_reset_ppm(ucsi);
459 static const struct acpi_device_id ucsi_acpi_match[] = {
463 MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
465 static struct platform_driver ucsi_acpi_platform_driver = {
468 .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
470 .probe = ucsi_acpi_probe,
471 .remove = ucsi_acpi_remove,
474 module_platform_driver(ucsi_acpi_platform_driver);
477 MODULE_LICENSE("GPL v2");
478 MODULE_DESCRIPTION("USB Type-C System Software Interface (UCSI) driver");