ucsi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * USB Type-C Connector System Software Interface driver
  3. *
  4. * Copyright (C) 2016, Intel Corporation
  5. * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  6. *
  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.
  10. */
  11. #include <linux/platform_device.h>
  12. #include <linux/module.h>
  13. #include <linux/delay.h>
  14. #include <linux/acpi.h>
  15. #include "ucsi.h"
  16. /* Double the time defined by MIN_TIME_TO_RESPOND_WITH_BUSY */
  17. #define UCSI_TIMEOUT_MS 20
  18. enum ucsi_status {
  19. UCSI_IDLE = 0,
  20. UCSI_BUSY,
  21. UCSI_ERROR,
  22. };
  23. struct ucsi_connector {
  24. int num;
  25. struct ucsi *ucsi;
  26. struct work_struct work;
  27. struct ucsi_connector_capability cap;
  28. };
  29. struct ucsi {
  30. struct device *dev;
  31. struct ucsi_data __iomem *data;
  32. enum ucsi_status status;
  33. struct completion complete;
  34. struct ucsi_capability cap;
  35. struct ucsi_connector *connector;
  36. /* device lock */
  37. spinlock_t dev_lock;
  38. /* PPM Communication lock */
  39. struct mutex ppm_lock;
  40. /* PPM communication flags */
  41. unsigned long flags;
  42. #define EVENT_PENDING 0
  43. #define COMMAND_PENDING 1
  44. };
  45. static int ucsi_acpi_cmd(struct ucsi *ucsi, struct ucsi_control *ctrl)
  46. {
  47. uuid_le uuid = UUID_LE(0x6f8398c2, 0x7ca4, 0x11e4,
  48. 0xad, 0x36, 0x63, 0x10, 0x42, 0xb5, 0x00, 0x8f);
  49. union acpi_object *obj;
  50. ucsi->data->ctrl.raw_cmd = ctrl->raw_cmd;
  51. obj = acpi_evaluate_dsm(ACPI_HANDLE(ucsi->dev), uuid.b, 1, 1, NULL);
  52. if (!obj) {
  53. dev_err(ucsi->dev, "%s: failed to evaluate _DSM\n", __func__);
  54. return -EIO;
  55. }
  56. ACPI_FREE(obj);
  57. return 0;
  58. }
  59. static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
  60. {
  61. struct ucsi *ucsi = data;
  62. struct ucsi_cci *cci;
  63. spin_lock(&ucsi->dev_lock);
  64. ucsi->status = UCSI_IDLE;
  65. cci = &ucsi->data->cci;
  66. /*
  67. * REVISIT: This is not documented behavior, but all known PPMs ACK
  68. * asynchronous events by sending notification with cleared CCI.
  69. */
  70. if (!ucsi->data->raw_cci) {
  71. if (test_bit(EVENT_PENDING, &ucsi->flags))
  72. complete(&ucsi->complete);
  73. else
  74. dev_WARN(ucsi->dev, "spurious notification\n");
  75. goto out_unlock;
  76. }
  77. if (test_bit(COMMAND_PENDING, &ucsi->flags)) {
  78. if (cci->busy) {
  79. ucsi->status = UCSI_BUSY;
  80. complete(&ucsi->complete);
  81. goto out_unlock;
  82. } else if (cci->ack_complete || cci->cmd_complete) {
  83. /* Error Indication is only valid with commands */
  84. if (cci->error && cci->cmd_complete)
  85. ucsi->status = UCSI_ERROR;
  86. ucsi->data->ctrl.raw_cmd = 0;
  87. complete(&ucsi->complete);
  88. }
  89. }
  90. if (cci->connector_change) {
  91. struct ucsi_connector *con;
  92. /*
  93. * This is workaround for buggy PPMs that create asynchronous
  94. * event notifications before OPM has enabled them.
  95. */
  96. if (!ucsi->connector)
  97. goto out_unlock;
  98. con = ucsi->connector + (cci->connector_change - 1);
  99. /*
  100. * PPM will not clear the connector specific bit in Connector
  101. * Change Indication field of CCI until the driver has ACK it,
  102. * and the driver can not ACK it before it has been processed.
  103. * The PPM will not generate new events before the first has
  104. * been acknowledged, even if they are for an other connector.
  105. * So only one event at a time.
  106. */
  107. if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
  108. schedule_work(&con->work);
  109. }
  110. out_unlock:
  111. spin_unlock(&ucsi->dev_lock);
  112. }
  113. static int ucsi_ack(struct ucsi *ucsi, u8 cmd)
  114. {
  115. struct ucsi_control ctrl;
  116. int ret;
  117. ctrl.cmd.cmd = UCSI_ACK_CC_CI;
  118. ctrl.cmd.length = 0;
  119. ctrl.cmd.data = cmd;
  120. ret = ucsi_acpi_cmd(ucsi, &ctrl);
  121. if (ret)
  122. return ret;
  123. /* Waiting for ACK also with ACK CMD for now */
  124. ret = wait_for_completion_timeout(&ucsi->complete,
  125. msecs_to_jiffies(UCSI_TIMEOUT_MS));
  126. if (!ret)
  127. return -ETIMEDOUT;
  128. return 0;
  129. }
  130. static int ucsi_run_cmd(struct ucsi *ucsi, struct ucsi_control *ctrl,
  131. void *data, size_t size)
  132. {
  133. u16 err_value = 0;
  134. int ret;
  135. set_bit(COMMAND_PENDING, &ucsi->flags);
  136. ret = ucsi_acpi_cmd(ucsi, ctrl);
  137. if (ret)
  138. goto err_clear_flag;
  139. ret = wait_for_completion_timeout(&ucsi->complete,
  140. msecs_to_jiffies(UCSI_TIMEOUT_MS));
  141. if (!ret) {
  142. ret = -ETIMEDOUT;
  143. goto err_clear_flag;
  144. }
  145. switch (ucsi->status) {
  146. case UCSI_IDLE:
  147. if (data)
  148. memcpy(data, ucsi->data->message_in, size);
  149. ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
  150. break;
  151. case UCSI_BUSY:
  152. /* The caller decides whether to cancel or not */
  153. ret = -EBUSY;
  154. goto err_clear_flag;
  155. case UCSI_ERROR:
  156. ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
  157. if (ret)
  158. goto err_clear_flag;
  159. ctrl->cmd.cmd = UCSI_GET_ERROR_STATUS;
  160. ctrl->cmd.length = 0;
  161. ctrl->cmd.data = 0;
  162. ret = ucsi_acpi_cmd(ucsi, ctrl);
  163. if (ret)
  164. goto err_clear_flag;
  165. ret = wait_for_completion_timeout(&ucsi->complete,
  166. msecs_to_jiffies(UCSI_TIMEOUT_MS));
  167. if (!ret) {
  168. ret = -ETIMEDOUT;
  169. goto err_clear_flag;
  170. }
  171. memcpy(&err_value, ucsi->data->message_in, sizeof(err_value));
  172. /* Something has really gone wrong */
  173. if (WARN_ON(ucsi->status == UCSI_ERROR)) {
  174. ret = -ENODEV;
  175. goto err_clear_flag;
  176. }
  177. ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
  178. if (ret)
  179. goto err_clear_flag;
  180. switch (err_value) {
  181. case UCSI_ERROR_INCOMPATIBLE_PARTNER:
  182. ret = -EOPNOTSUPP;
  183. break;
  184. case UCSI_ERROR_CC_COMMUNICATION_ERR:
  185. ret = -ECOMM;
  186. break;
  187. case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
  188. ret = -EIO;
  189. break;
  190. case UCSI_ERROR_DEAD_BATTERY:
  191. dev_warn(ucsi->dev, "Dead battery condition!\n");
  192. ret = -EPERM;
  193. break;
  194. /* The following mean a bug in this driver */
  195. case UCSI_ERROR_INVALID_CON_NUM:
  196. case UCSI_ERROR_UNREGONIZED_CMD:
  197. case UCSI_ERROR_INVALID_CMD_ARGUMENT:
  198. default:
  199. dev_warn(ucsi->dev,
  200. "%s: possible UCSI driver bug - error %hu\n",
  201. __func__, err_value);
  202. ret = -EINVAL;
  203. break;
  204. }
  205. break;
  206. }
  207. ctrl->raw_cmd = 0;
  208. err_clear_flag:
  209. clear_bit(COMMAND_PENDING, &ucsi->flags);
  210. return ret;
  211. }
  212. static void ucsi_connector_change(struct work_struct *work)
  213. {
  214. struct ucsi_connector *con = container_of(work, struct ucsi_connector,
  215. work);
  216. struct ucsi_connector_status constat;
  217. struct ucsi *ucsi = con->ucsi;
  218. struct ucsi_control ctrl;
  219. int ret;
  220. mutex_lock(&ucsi->ppm_lock);
  221. ctrl.cmd.cmd = UCSI_GET_CONNECTOR_STATUS;
  222. ctrl.cmd.length = 0;
  223. ctrl.cmd.data = con->num;
  224. ret = ucsi_run_cmd(con->ucsi, &ctrl, &constat, sizeof(constat));
  225. if (ret) {
  226. dev_err(ucsi->dev, "%s: failed to read connector status (%d)\n",
  227. __func__, ret);
  228. goto out_ack_event;
  229. }
  230. /* Ignoring disconnections and Alternate Modes */
  231. if (!constat.connected || !(constat.change &
  232. (UCSI_CONSTAT_PARTNER_CHANGE | UCSI_CONSTAT_CONNECT_CHANGE)) ||
  233. constat.partner_flags & UCSI_CONSTAT_PARTNER_FLAG_ALT_MODE)
  234. goto out_ack_event;
  235. /* If the partner got USB Host role, attempting swap */
  236. if (constat.partner_type & UCSI_CONSTAT_PARTNER_TYPE_DFP) {
  237. ctrl.uor.cmd = UCSI_SET_UOR;
  238. ctrl.uor.con_num = con->num;
  239. ctrl.uor.role = UCSI_UOR_ROLE_DFP;
  240. ret = ucsi_run_cmd(con->ucsi, &ctrl, NULL, 0);
  241. if (ret)
  242. dev_err(ucsi->dev, "%s: failed to swap role (%d)\n",
  243. __func__, ret);
  244. }
  245. out_ack_event:
  246. ucsi_ack(ucsi, UCSI_ACK_EVENT);
  247. clear_bit(EVENT_PENDING, &ucsi->flags);
  248. mutex_unlock(&ucsi->ppm_lock);
  249. }
  250. static int ucsi_reset_ppm(struct ucsi *ucsi)
  251. {
  252. int timeout = UCSI_TIMEOUT_MS;
  253. struct ucsi_control ctrl;
  254. int ret;
  255. memset(&ctrl, 0, sizeof(ctrl));
  256. ctrl.cmd.cmd = UCSI_PPM_RESET;
  257. ret = ucsi_acpi_cmd(ucsi, &ctrl);
  258. if (ret)
  259. return ret;
  260. /* There is no quarantee the PPM will ever set the RESET_COMPLETE bit */
  261. while (!ucsi->data->cci.reset_complete && timeout--)
  262. usleep_range(1000, 2000);
  263. return 0;
  264. }
  265. static int ucsi_init(struct ucsi *ucsi)
  266. {
  267. struct ucsi_connector *con;
  268. struct ucsi_control ctrl;
  269. int ret;
  270. int i;
  271. init_completion(&ucsi->complete);
  272. spin_lock_init(&ucsi->dev_lock);
  273. mutex_init(&ucsi->ppm_lock);
  274. /* Reset the PPM */
  275. ret = ucsi_reset_ppm(ucsi);
  276. if (ret)
  277. return ret;
  278. /*
  279. * REVISIT: Executing second reset to WA an issue seen on some of the
  280. * Broxton based platforms, where the first reset puts the PPM into a
  281. * state where it's unable to recognise some of the commands.
  282. */
  283. ret = ucsi_reset_ppm(ucsi);
  284. if (ret)
  285. return ret;
  286. mutex_lock(&ucsi->ppm_lock);
  287. /* Enable basic notifications */
  288. ctrl.cmd.cmd = UCSI_SET_NOTIFICATION_ENABLE;
  289. ctrl.cmd.length = 0;
  290. ctrl.cmd.data = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
  291. ret = ucsi_run_cmd(ucsi, &ctrl, NULL, 0);
  292. if (ret)
  293. goto err_reset;
  294. /* Get PPM capabilities */
  295. ctrl.cmd.cmd = UCSI_GET_CAPABILITY;
  296. ret = ucsi_run_cmd(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
  297. if (ret)
  298. goto err_reset;
  299. if (!ucsi->cap.num_connectors) {
  300. ret = -ENODEV;
  301. goto err_reset;
  302. }
  303. ucsi->connector = devm_kcalloc(ucsi->dev, ucsi->cap.num_connectors,
  304. sizeof(*ucsi->connector), GFP_KERNEL);
  305. if (!ucsi->connector) {
  306. ret = -ENOMEM;
  307. goto err_reset;
  308. }
  309. for (i = 1, con = ucsi->connector; i < ucsi->cap.num_connectors + 1;
  310. i++, con++) {
  311. /* Get connector capability */
  312. ctrl.cmd.cmd = UCSI_GET_CONNECTOR_CAPABILITY;
  313. ctrl.cmd.data = i;
  314. ret = ucsi_run_cmd(ucsi, &ctrl, &con->cap, sizeof(con->cap));
  315. if (ret)
  316. goto err_reset;
  317. con->num = i;
  318. con->ucsi = ucsi;
  319. INIT_WORK(&con->work, ucsi_connector_change);
  320. }
  321. /* Enable all notifications */
  322. ctrl.cmd.cmd = UCSI_SET_NOTIFICATION_ENABLE;
  323. ctrl.cmd.data = UCSI_ENABLE_NTFY_ALL;
  324. ret = ucsi_run_cmd(ucsi, &ctrl, NULL, 0);
  325. if (ret < 0)
  326. goto err_reset;
  327. mutex_unlock(&ucsi->ppm_lock);
  328. return 0;
  329. err_reset:
  330. ucsi_reset_ppm(ucsi);
  331. mutex_unlock(&ucsi->ppm_lock);
  332. return ret;
  333. }
  334. static int ucsi_acpi_probe(struct platform_device *pdev)
  335. {
  336. struct resource *res;
  337. acpi_status status;
  338. struct ucsi *ucsi;
  339. int ret;
  340. ucsi = devm_kzalloc(&pdev->dev, sizeof(*ucsi), GFP_KERNEL);
  341. if (!ucsi)
  342. return -ENOMEM;
  343. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  344. if (!res) {
  345. dev_err(&pdev->dev, "missing memory resource\n");
  346. return -ENODEV;
  347. }
  348. /*
  349. * NOTE: ACPI has claimed the memory region as it's also an Operation
  350. * Region. It's not possible to request it in the driver.
  351. */
  352. ucsi->data = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  353. if (!ucsi->data)
  354. return -ENOMEM;
  355. ucsi->dev = &pdev->dev;
  356. status = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev),
  357. ACPI_ALL_NOTIFY,
  358. ucsi_acpi_notify, ucsi);
  359. if (ACPI_FAILURE(status))
  360. return -ENODEV;
  361. ret = ucsi_init(ucsi);
  362. if (ret) {
  363. acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
  364. ACPI_ALL_NOTIFY,
  365. ucsi_acpi_notify);
  366. return ret;
  367. }
  368. platform_set_drvdata(pdev, ucsi);
  369. return 0;
  370. }
  371. static int ucsi_acpi_remove(struct platform_device *pdev)
  372. {
  373. struct ucsi *ucsi = platform_get_drvdata(pdev);
  374. acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev),
  375. ACPI_ALL_NOTIFY, ucsi_acpi_notify);
  376. /* Make sure there are no events in the middle of being processed */
  377. if (wait_on_bit_timeout(&ucsi->flags, EVENT_PENDING,
  378. TASK_UNINTERRUPTIBLE,
  379. msecs_to_jiffies(UCSI_TIMEOUT_MS)))
  380. dev_WARN(ucsi->dev, "%s: Events still pending\n", __func__);
  381. ucsi_reset_ppm(ucsi);
  382. return 0;
  383. }
  384. static const struct acpi_device_id ucsi_acpi_match[] = {
  385. { "PNP0CA0", 0 },
  386. { },
  387. };
  388. MODULE_DEVICE_TABLE(acpi, ucsi_acpi_match);
  389. static struct platform_driver ucsi_acpi_platform_driver = {
  390. .driver = {
  391. .name = "ucsi_acpi",
  392. .acpi_match_table = ACPI_PTR(ucsi_acpi_match),
  393. },
  394. .probe = ucsi_acpi_probe,
  395. .remove = ucsi_acpi_remove,
  396. };
  397. module_platform_driver(ucsi_acpi_platform_driver);
  398. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  399. MODULE_LICENSE("GPL v2");
  400. MODULE_DESCRIPTION("USB Type-C System Software Interface (UCSI) driver");