cros_ec_lpc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * cros_ec_lpc - LPC access to the Chrome OS Embedded Controller
  3. *
  4. * Copyright (C) 2012-2015 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * This driver uses the Chrome OS EC byte-level message-based protocol for
  16. * communicating the keyboard state (which keys are pressed) from a keyboard EC
  17. * to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
  18. * but everything else (including deghosting) is done here. The main
  19. * motivation for this is to keep the EC firmware as simple as possible, since
  20. * it cannot be easily upgraded and EC flash/IRAM space is relatively
  21. * expensive.
  22. */
  23. #include <linux/acpi.h>
  24. #include <linux/dmi.h>
  25. #include <linux/delay.h>
  26. #include <linux/io.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/mfd/cros_ec.h>
  29. #include <linux/mfd/cros_ec_commands.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/printk.h>
  33. #include <linux/suspend.h>
  34. #include "cros_ec_lpc_reg.h"
  35. #define DRV_NAME "cros_ec_lpcs"
  36. #define ACPI_DRV_NAME "GOOG0004"
  37. /* True if ACPI device is present */
  38. static bool cros_ec_lpc_acpi_device_found;
  39. static int ec_response_timed_out(void)
  40. {
  41. unsigned long one_second = jiffies + HZ;
  42. u8 data;
  43. usleep_range(200, 300);
  44. do {
  45. if (!(cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_CMD, 1, &data) &
  46. EC_LPC_STATUS_BUSY_MASK))
  47. return 0;
  48. usleep_range(100, 200);
  49. } while (time_before(jiffies, one_second));
  50. return 1;
  51. }
  52. static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
  53. struct cros_ec_command *msg)
  54. {
  55. struct ec_host_response response;
  56. u8 sum;
  57. int ret = 0;
  58. u8 *dout;
  59. ret = cros_ec_prepare_tx(ec, msg);
  60. /* Write buffer */
  61. cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
  62. /* Here we go */
  63. sum = EC_COMMAND_PROTOCOL_3;
  64. cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_CMD, 1, &sum);
  65. if (ec_response_timed_out()) {
  66. dev_warn(ec->dev, "EC responsed timed out\n");
  67. ret = -EIO;
  68. goto done;
  69. }
  70. /* Check result */
  71. msg->result = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_DATA, 1, &sum);
  72. ret = cros_ec_check_result(ec, msg);
  73. if (ret)
  74. goto done;
  75. /* Read back response */
  76. dout = (u8 *)&response;
  77. sum = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PACKET, sizeof(response),
  78. dout);
  79. msg->result = response.result;
  80. if (response.data_len > msg->insize) {
  81. dev_err(ec->dev,
  82. "packet too long (%d bytes, expected %d)",
  83. response.data_len, msg->insize);
  84. ret = -EMSGSIZE;
  85. goto done;
  86. }
  87. /* Read response and process checksum */
  88. sum += cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PACKET +
  89. sizeof(response), response.data_len,
  90. msg->data);
  91. if (sum) {
  92. dev_err(ec->dev,
  93. "bad packet checksum %02x\n",
  94. response.checksum);
  95. ret = -EBADMSG;
  96. goto done;
  97. }
  98. /* Return actual amount of data received */
  99. ret = response.data_len;
  100. done:
  101. return ret;
  102. }
  103. static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
  104. struct cros_ec_command *msg)
  105. {
  106. struct ec_lpc_host_args args;
  107. u8 sum;
  108. int ret = 0;
  109. if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
  110. msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
  111. dev_err(ec->dev,
  112. "invalid buffer sizes (out %d, in %d)\n",
  113. msg->outsize, msg->insize);
  114. return -EINVAL;
  115. }
  116. /* Now actually send the command to the EC and get the result */
  117. args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
  118. args.command_version = msg->version;
  119. args.data_size = msg->outsize;
  120. /* Initialize checksum */
  121. sum = msg->command + args.flags + args.command_version + args.data_size;
  122. /* Copy data and update checksum */
  123. sum += cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_PARAM, msg->outsize,
  124. msg->data);
  125. /* Finalize checksum and write args */
  126. args.checksum = sum;
  127. cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
  128. (u8 *)&args);
  129. /* Here we go */
  130. sum = msg->command;
  131. cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_CMD, 1, &sum);
  132. if (ec_response_timed_out()) {
  133. dev_warn(ec->dev, "EC responsed timed out\n");
  134. ret = -EIO;
  135. goto done;
  136. }
  137. /* Check result */
  138. msg->result = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_DATA, 1, &sum);
  139. ret = cros_ec_check_result(ec, msg);
  140. if (ret)
  141. goto done;
  142. /* Read back args */
  143. cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
  144. (u8 *)&args);
  145. if (args.data_size > msg->insize) {
  146. dev_err(ec->dev,
  147. "packet too long (%d bytes, expected %d)",
  148. args.data_size, msg->insize);
  149. ret = -ENOSPC;
  150. goto done;
  151. }
  152. /* Start calculating response checksum */
  153. sum = msg->command + args.flags + args.command_version + args.data_size;
  154. /* Read response and update checksum */
  155. sum += cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PARAM, args.data_size,
  156. msg->data);
  157. /* Verify checksum */
  158. if (args.checksum != sum) {
  159. dev_err(ec->dev,
  160. "bad packet checksum, expected %02x, got %02x\n",
  161. args.checksum, sum);
  162. ret = -EBADMSG;
  163. goto done;
  164. }
  165. /* Return actual amount of data received */
  166. ret = args.data_size;
  167. done:
  168. return ret;
  169. }
  170. /* Returns num bytes read, or negative on error. Doesn't need locking. */
  171. static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
  172. unsigned int bytes, void *dest)
  173. {
  174. int i = offset;
  175. char *s = dest;
  176. int cnt = 0;
  177. if (offset >= EC_MEMMAP_SIZE - bytes)
  178. return -EINVAL;
  179. /* fixed length */
  180. if (bytes) {
  181. cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + offset, bytes, s);
  182. return bytes;
  183. }
  184. /* string */
  185. for (; i < EC_MEMMAP_SIZE; i++, s++) {
  186. cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + i, 1, s);
  187. cnt++;
  188. if (!*s)
  189. break;
  190. }
  191. return cnt;
  192. }
  193. static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
  194. {
  195. struct cros_ec_device *ec_dev = data;
  196. if (ec_dev->mkbp_event_supported &&
  197. cros_ec_get_next_event(ec_dev, NULL) > 0)
  198. blocking_notifier_call_chain(&ec_dev->event_notifier, 0,
  199. ec_dev);
  200. if (value == ACPI_NOTIFY_DEVICE_WAKE)
  201. pm_system_wakeup();
  202. }
  203. static int cros_ec_lpc_probe(struct platform_device *pdev)
  204. {
  205. struct device *dev = &pdev->dev;
  206. struct acpi_device *adev;
  207. acpi_status status;
  208. struct cros_ec_device *ec_dev;
  209. u8 buf[2];
  210. int irq, ret;
  211. if (!devm_request_region(dev, EC_LPC_ADDR_MEMMAP, EC_MEMMAP_SIZE,
  212. dev_name(dev))) {
  213. dev_err(dev, "couldn't reserve memmap region\n");
  214. return -EBUSY;
  215. }
  216. cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
  217. if (buf[0] != 'E' || buf[1] != 'C') {
  218. dev_err(dev, "EC ID not detected\n");
  219. return -ENODEV;
  220. }
  221. if (!devm_request_region(dev, EC_HOST_CMD_REGION0,
  222. EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
  223. dev_err(dev, "couldn't reserve region0\n");
  224. return -EBUSY;
  225. }
  226. if (!devm_request_region(dev, EC_HOST_CMD_REGION1,
  227. EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
  228. dev_err(dev, "couldn't reserve region1\n");
  229. return -EBUSY;
  230. }
  231. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  232. if (!ec_dev)
  233. return -ENOMEM;
  234. platform_set_drvdata(pdev, ec_dev);
  235. ec_dev->dev = dev;
  236. ec_dev->phys_name = dev_name(dev);
  237. ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc;
  238. ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
  239. ec_dev->cmd_readmem = cros_ec_lpc_readmem;
  240. ec_dev->din_size = sizeof(struct ec_host_response) +
  241. sizeof(struct ec_response_get_protocol_info);
  242. ec_dev->dout_size = sizeof(struct ec_host_request);
  243. /*
  244. * Some boards do not have an IRQ allotted for cros_ec_lpc,
  245. * which makes ENXIO an expected (and safe) scenario.
  246. */
  247. irq = platform_get_irq(pdev, 0);
  248. if (irq > 0)
  249. ec_dev->irq = irq;
  250. else if (irq != -ENXIO) {
  251. dev_err(dev, "couldn't retrieve IRQ number (%d)\n", irq);
  252. return irq;
  253. }
  254. ret = cros_ec_register(ec_dev);
  255. if (ret) {
  256. dev_err(dev, "couldn't register ec_dev (%d)\n", ret);
  257. return ret;
  258. }
  259. /*
  260. * Connect a notify handler to process MKBP messages if we have a
  261. * companion ACPI device.
  262. */
  263. adev = ACPI_COMPANION(dev);
  264. if (adev) {
  265. status = acpi_install_notify_handler(adev->handle,
  266. ACPI_ALL_NOTIFY,
  267. cros_ec_lpc_acpi_notify,
  268. ec_dev);
  269. if (ACPI_FAILURE(status))
  270. dev_warn(dev, "Failed to register notifier %08x\n",
  271. status);
  272. }
  273. return 0;
  274. }
  275. static int cros_ec_lpc_remove(struct platform_device *pdev)
  276. {
  277. struct cros_ec_device *ec_dev;
  278. struct acpi_device *adev;
  279. adev = ACPI_COMPANION(&pdev->dev);
  280. if (adev)
  281. acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
  282. cros_ec_lpc_acpi_notify);
  283. ec_dev = platform_get_drvdata(pdev);
  284. cros_ec_remove(ec_dev);
  285. return 0;
  286. }
  287. static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = {
  288. { ACPI_DRV_NAME, 0 },
  289. { }
  290. };
  291. MODULE_DEVICE_TABLE(acpi, cros_ec_lpc_acpi_device_ids);
  292. static const struct dmi_system_id cros_ec_lpc_dmi_table[] __initconst = {
  293. {
  294. /*
  295. * Today all Chromebooks/boxes ship with Google_* as version and
  296. * coreboot as bios vendor. No other systems with this
  297. * combination are known to date.
  298. */
  299. .matches = {
  300. DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
  301. DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
  302. },
  303. },
  304. {
  305. /*
  306. * If the box is running custom coreboot firmware then the
  307. * DMI BIOS version string will not be matched by "Google_",
  308. * but the system vendor string will still be matched by
  309. * "GOOGLE".
  310. */
  311. .matches = {
  312. DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
  313. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  314. },
  315. },
  316. {
  317. /* x86-link, the Chromebook Pixel. */
  318. .matches = {
  319. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  320. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  321. },
  322. },
  323. {
  324. /* x86-samus, the Chromebook Pixel 2. */
  325. .matches = {
  326. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  327. DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
  328. },
  329. },
  330. {
  331. /* x86-peppy, the Acer C720 Chromebook. */
  332. .matches = {
  333. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  334. DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
  335. },
  336. },
  337. {
  338. /* x86-glimmer, the Lenovo Thinkpad Yoga 11e. */
  339. .matches = {
  340. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  341. DMI_MATCH(DMI_PRODUCT_NAME, "Glimmer"),
  342. },
  343. },
  344. { /* sentinel */ }
  345. };
  346. MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
  347. #ifdef CONFIG_PM_SLEEP
  348. static int cros_ec_lpc_suspend(struct device *dev)
  349. {
  350. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  351. return cros_ec_suspend(ec_dev);
  352. }
  353. static int cros_ec_lpc_resume(struct device *dev)
  354. {
  355. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  356. return cros_ec_resume(ec_dev);
  357. }
  358. #endif
  359. const struct dev_pm_ops cros_ec_lpc_pm_ops = {
  360. SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_lpc_suspend, cros_ec_lpc_resume)
  361. };
  362. static struct platform_driver cros_ec_lpc_driver = {
  363. .driver = {
  364. .name = DRV_NAME,
  365. .acpi_match_table = cros_ec_lpc_acpi_device_ids,
  366. .pm = &cros_ec_lpc_pm_ops,
  367. },
  368. .probe = cros_ec_lpc_probe,
  369. .remove = cros_ec_lpc_remove,
  370. };
  371. static struct platform_device cros_ec_lpc_device = {
  372. .name = DRV_NAME
  373. };
  374. static acpi_status cros_ec_lpc_parse_device(acpi_handle handle, u32 level,
  375. void *context, void **retval)
  376. {
  377. *(bool *)context = true;
  378. return AE_CTRL_TERMINATE;
  379. }
  380. static int __init cros_ec_lpc_init(void)
  381. {
  382. int ret;
  383. acpi_status status;
  384. status = acpi_get_devices(ACPI_DRV_NAME, cros_ec_lpc_parse_device,
  385. &cros_ec_lpc_acpi_device_found, NULL);
  386. if (ACPI_FAILURE(status))
  387. pr_warn(DRV_NAME ": Looking for %s failed\n", ACPI_DRV_NAME);
  388. if (!cros_ec_lpc_acpi_device_found &&
  389. !dmi_check_system(cros_ec_lpc_dmi_table)) {
  390. pr_err(DRV_NAME ": unsupported system.\n");
  391. return -ENODEV;
  392. }
  393. cros_ec_lpc_reg_init();
  394. /* Register the driver */
  395. ret = platform_driver_register(&cros_ec_lpc_driver);
  396. if (ret) {
  397. pr_err(DRV_NAME ": can't register driver: %d\n", ret);
  398. cros_ec_lpc_reg_destroy();
  399. return ret;
  400. }
  401. if (!cros_ec_lpc_acpi_device_found) {
  402. /* Register the device, and it'll get hooked up automatically */
  403. ret = platform_device_register(&cros_ec_lpc_device);
  404. if (ret) {
  405. pr_err(DRV_NAME ": can't register device: %d\n", ret);
  406. platform_driver_unregister(&cros_ec_lpc_driver);
  407. cros_ec_lpc_reg_destroy();
  408. }
  409. }
  410. return ret;
  411. }
  412. static void __exit cros_ec_lpc_exit(void)
  413. {
  414. if (!cros_ec_lpc_acpi_device_found)
  415. platform_device_unregister(&cros_ec_lpc_device);
  416. platform_driver_unregister(&cros_ec_lpc_driver);
  417. cros_ec_lpc_reg_destroy();
  418. }
  419. module_init(cros_ec_lpc_init);
  420. module_exit(cros_ec_lpc_exit);
  421. MODULE_LICENSE("GPL");
  422. MODULE_DESCRIPTION("ChromeOS EC LPC driver");