cros_ec_lpc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // SPDX-License-Identifier: GPL-2.0
  2. // LPC interface for ChromeOS Embedded Controller
  3. //
  4. // Copyright (C) 2012-2015 Google, Inc
  5. //
  6. // This driver uses the ChromeOS EC byte-level message-based protocol for
  7. // communicating the keyboard state (which keys are pressed) from a keyboard EC
  8. // to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
  9. // but everything else (including deghosting) is done here. The main
  10. // motivation for this is to keep the EC firmware as simple as possible, since
  11. // it cannot be easily upgraded and EC flash/IRAM space is relatively
  12. // expensive.
  13. #include <linux/acpi.h>
  14. #include <linux/dmi.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kobject.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_data/cros_ec_commands.h>
  21. #include <linux/platform_data/cros_ec_proto.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/printk.h>
  24. #include <linux/reboot.h>
  25. #include <linux/suspend.h>
  26. #include "cros_ec.h"
  27. #include "cros_ec_lpc_mec.h"
  28. #define DRV_NAME "cros_ec_lpcs"
  29. #define ACPI_DRV_NAME "GOOG0004"
  30. #define FRMW_ACPI_DRV_NAME "FRMWC004"
  31. /* True if ACPI device is present */
  32. static bool cros_ec_lpc_acpi_device_found;
  33. /*
  34. * Indicates that lpc_driver_data.quirk_mmio_memory_base should
  35. * be used as the base port for EC mapped memory.
  36. */
  37. #define CROS_EC_LPC_QUIRK_REMAP_MEMORY BIT(0)
  38. /*
  39. * Indicates that lpc_driver_data.quirk_acpi_id should be used to find
  40. * the ACPI device.
  41. */
  42. #define CROS_EC_LPC_QUIRK_ACPI_ID BIT(1)
  43. /*
  44. * Indicates that lpc_driver_data.quirk_aml_mutex_name should be used
  45. * to find an AML mutex to protect access to Microchip EC.
  46. */
  47. #define CROS_EC_LPC_QUIRK_AML_MUTEX BIT(2)
  48. /**
  49. * struct lpc_driver_data - driver data attached to a DMI device ID to indicate
  50. * hardware quirks.
  51. * @quirks: a bitfield composed of quirks from CROS_EC_LPC_QUIRK_*
  52. * @quirk_mmio_memory_base: The first I/O port addressing EC mapped memory (used
  53. * when quirk ...REMAP_MEMORY is set.)
  54. * @quirk_acpi_id: An ACPI HID to be used to find the ACPI device.
  55. * @quirk_aml_mutex_name: The name of an AML mutex to be used to protect access
  56. * to Microchip EC.
  57. */
  58. struct lpc_driver_data {
  59. u32 quirks;
  60. u16 quirk_mmio_memory_base;
  61. const char *quirk_acpi_id;
  62. const char *quirk_aml_mutex_name;
  63. };
  64. /**
  65. * struct cros_ec_lpc - LPC device-specific data
  66. * @mmio_memory_base: The first I/O port addressing EC mapped memory.
  67. */
  68. struct cros_ec_lpc {
  69. u16 mmio_memory_base;
  70. };
  71. /**
  72. * struct lpc_driver_ops - LPC driver operations
  73. * @read: Copy length bytes from EC address offset into buffer dest.
  74. * Returns a negative error code on error, or the 8-bit checksum
  75. * of all bytes read.
  76. * @write: Copy length bytes from buffer msg into EC address offset.
  77. * Returns a negative error code on error, or the 8-bit checksum
  78. * of all bytes written.
  79. */
  80. struct lpc_driver_ops {
  81. int (*read)(unsigned int offset, unsigned int length, u8 *dest);
  82. int (*write)(unsigned int offset, unsigned int length, const u8 *msg);
  83. };
  84. static struct lpc_driver_ops cros_ec_lpc_ops = { };
  85. /*
  86. * A generic instance of the read function of struct lpc_driver_ops, used for
  87. * the LPC EC.
  88. */
  89. static int cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length,
  90. u8 *dest)
  91. {
  92. u8 sum = 0;
  93. int i;
  94. for (i = 0; i < length; ++i) {
  95. dest[i] = inb(offset + i);
  96. sum += dest[i];
  97. }
  98. /* Return checksum of all bytes read */
  99. return sum;
  100. }
  101. /*
  102. * A generic instance of the write function of struct lpc_driver_ops, used for
  103. * the LPC EC.
  104. */
  105. static int cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length,
  106. const u8 *msg)
  107. {
  108. u8 sum = 0;
  109. int i;
  110. for (i = 0; i < length; ++i) {
  111. outb(msg[i], offset + i);
  112. sum += msg[i];
  113. }
  114. /* Return checksum of all bytes written */
  115. return sum;
  116. }
  117. /*
  118. * An instance of the read function of struct lpc_driver_ops, used for the
  119. * MEC variant of LPC EC.
  120. */
  121. static int cros_ec_lpc_mec_read_bytes(unsigned int offset, unsigned int length,
  122. u8 *dest)
  123. {
  124. int in_range = cros_ec_lpc_mec_in_range(offset, length);
  125. if (in_range < 0)
  126. return in_range;
  127. return in_range ?
  128. cros_ec_lpc_io_bytes_mec(MEC_IO_READ,
  129. offset - EC_HOST_CMD_REGION0,
  130. length, dest) :
  131. cros_ec_lpc_read_bytes(offset, length, dest);
  132. }
  133. /*
  134. * An instance of the write function of struct lpc_driver_ops, used for the
  135. * MEC variant of LPC EC.
  136. */
  137. static int cros_ec_lpc_mec_write_bytes(unsigned int offset, unsigned int length,
  138. const u8 *msg)
  139. {
  140. int in_range = cros_ec_lpc_mec_in_range(offset, length);
  141. if (in_range < 0)
  142. return in_range;
  143. return in_range ?
  144. cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE,
  145. offset - EC_HOST_CMD_REGION0,
  146. length, (u8 *)msg) :
  147. cros_ec_lpc_write_bytes(offset, length, msg);
  148. }
  149. static int ec_response_timed_out(void)
  150. {
  151. unsigned long one_second = jiffies + HZ;
  152. u8 data;
  153. int ret;
  154. usleep_range(200, 300);
  155. do {
  156. ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_CMD, 1, &data);
  157. if (ret < 0)
  158. return ret;
  159. if (!(data & EC_LPC_STATUS_BUSY_MASK))
  160. return 0;
  161. usleep_range(100, 200);
  162. } while (time_before(jiffies, one_second));
  163. return 1;
  164. }
  165. static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
  166. struct cros_ec_command *msg)
  167. {
  168. struct ec_host_response response;
  169. u8 sum;
  170. int ret = 0;
  171. u8 *dout;
  172. ret = cros_ec_prepare_tx(ec, msg);
  173. if (ret < 0)
  174. goto done;
  175. /* Write buffer */
  176. ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
  177. if (ret < 0)
  178. goto done;
  179. /* Here we go */
  180. sum = EC_COMMAND_PROTOCOL_3;
  181. ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
  182. if (ret < 0)
  183. goto done;
  184. ret = ec_response_timed_out();
  185. if (ret < 0)
  186. goto done;
  187. if (ret) {
  188. dev_warn(ec->dev, "EC response timed out\n");
  189. ret = -EIO;
  190. goto done;
  191. }
  192. /* Check result */
  193. ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
  194. if (ret < 0)
  195. goto done;
  196. msg->result = ret;
  197. ret = cros_ec_check_result(ec, msg);
  198. if (ret)
  199. goto done;
  200. /* Read back response */
  201. dout = (u8 *)&response;
  202. ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET, sizeof(response),
  203. dout);
  204. if (ret < 0)
  205. goto done;
  206. sum = ret;
  207. msg->result = response.result;
  208. if (response.data_len > msg->insize) {
  209. dev_err(ec->dev,
  210. "packet too long (%d bytes, expected %d)",
  211. response.data_len, msg->insize);
  212. ret = -EMSGSIZE;
  213. goto done;
  214. }
  215. /* Read response and process checksum */
  216. ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET +
  217. sizeof(response), response.data_len,
  218. msg->data);
  219. if (ret < 0)
  220. goto done;
  221. sum += ret;
  222. if (sum) {
  223. dev_err(ec->dev,
  224. "bad packet checksum %02x\n",
  225. response.checksum);
  226. ret = -EBADMSG;
  227. goto done;
  228. }
  229. /* Return actual amount of data received */
  230. ret = response.data_len;
  231. done:
  232. return ret;
  233. }
  234. static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
  235. struct cros_ec_command *msg)
  236. {
  237. struct ec_lpc_host_args args;
  238. u8 sum;
  239. int ret = 0;
  240. if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
  241. msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
  242. dev_err(ec->dev,
  243. "invalid buffer sizes (out %d, in %d)\n",
  244. msg->outsize, msg->insize);
  245. return -EINVAL;
  246. }
  247. /* Now actually send the command to the EC and get the result */
  248. args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
  249. args.command_version = msg->version;
  250. args.data_size = msg->outsize;
  251. /* Initialize checksum */
  252. sum = msg->command + args.flags + args.command_version + args.data_size;
  253. /* Copy data and update checksum */
  254. ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PARAM, msg->outsize,
  255. msg->data);
  256. if (ret < 0)
  257. goto done;
  258. sum += ret;
  259. /* Finalize checksum and write args */
  260. args.checksum = sum;
  261. ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
  262. (u8 *)&args);
  263. if (ret < 0)
  264. goto done;
  265. /* Here we go */
  266. sum = msg->command;
  267. ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
  268. if (ret < 0)
  269. goto done;
  270. ret = ec_response_timed_out();
  271. if (ret < 0)
  272. goto done;
  273. if (ret) {
  274. dev_warn(ec->dev, "EC response timed out\n");
  275. ret = -EIO;
  276. goto done;
  277. }
  278. /* Check result */
  279. ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
  280. if (ret < 0)
  281. goto done;
  282. msg->result = ret;
  283. ret = cros_ec_check_result(ec, msg);
  284. if (ret)
  285. goto done;
  286. /* Read back args */
  287. ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_ARGS, sizeof(args), (u8 *)&args);
  288. if (ret < 0)
  289. goto done;
  290. if (args.data_size > msg->insize) {
  291. dev_err(ec->dev,
  292. "packet too long (%d bytes, expected %d)",
  293. args.data_size, msg->insize);
  294. ret = -ENOSPC;
  295. goto done;
  296. }
  297. /* Start calculating response checksum */
  298. sum = msg->command + args.flags + args.command_version + args.data_size;
  299. /* Read response and update checksum */
  300. ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PARAM, args.data_size,
  301. msg->data);
  302. if (ret < 0)
  303. goto done;
  304. sum += ret;
  305. /* Verify checksum */
  306. if (args.checksum != sum) {
  307. dev_err(ec->dev,
  308. "bad packet checksum, expected %02x, got %02x\n",
  309. args.checksum, sum);
  310. ret = -EBADMSG;
  311. goto done;
  312. }
  313. /* Return actual amount of data received */
  314. ret = args.data_size;
  315. done:
  316. return ret;
  317. }
  318. /* Returns num bytes read, or negative on error. Doesn't need locking. */
  319. static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
  320. unsigned int bytes, void *dest)
  321. {
  322. struct cros_ec_lpc *ec_lpc = ec->priv;
  323. int i = offset;
  324. char *s = dest;
  325. int cnt = 0;
  326. int ret;
  327. if (offset >= EC_MEMMAP_SIZE - bytes)
  328. return -EINVAL;
  329. /* fixed length */
  330. if (bytes) {
  331. ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + offset, bytes, s);
  332. if (ret < 0)
  333. return ret;
  334. return bytes;
  335. }
  336. /* string */
  337. for (; i < EC_MEMMAP_SIZE; i++, s++) {
  338. ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + i, 1, s);
  339. if (ret < 0)
  340. return ret;
  341. cnt++;
  342. if (!*s)
  343. break;
  344. }
  345. return cnt;
  346. }
  347. static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
  348. {
  349. static const char *env[] = { "ERROR=PANIC", NULL };
  350. struct cros_ec_device *ec_dev = data;
  351. bool ec_has_more_events;
  352. int ret;
  353. ec_dev->last_event_time = cros_ec_get_time_ns();
  354. if (value == ACPI_NOTIFY_CROS_EC_PANIC) {
  355. dev_emerg(ec_dev->dev, "CrOS EC Panic Reported. Shutdown is imminent!");
  356. blocking_notifier_call_chain(&ec_dev->panic_notifier, 0, ec_dev);
  357. kobject_uevent_env(&ec_dev->dev->kobj, KOBJ_CHANGE, (char **)env);
  358. /* Begin orderly shutdown. EC will force reset after a short period. */
  359. hw_protection_shutdown("CrOS EC Panic", -1);
  360. /* Do not query for other events after a panic is reported */
  361. return;
  362. }
  363. if (ec_dev->mkbp_event_supported)
  364. do {
  365. ret = cros_ec_get_next_event(ec_dev, NULL,
  366. &ec_has_more_events);
  367. if (ret > 0)
  368. blocking_notifier_call_chain(
  369. &ec_dev->event_notifier, 0,
  370. ec_dev);
  371. } while (ec_has_more_events);
  372. if (value == ACPI_NOTIFY_DEVICE_WAKE)
  373. pm_system_wakeup();
  374. }
  375. static acpi_status cros_ec_lpc_parse_device(acpi_handle handle, u32 level,
  376. void *context, void **retval)
  377. {
  378. *(struct acpi_device **)context = acpi_fetch_acpi_dev(handle);
  379. return AE_CTRL_TERMINATE;
  380. }
  381. static struct acpi_device *cros_ec_lpc_get_device(const char *id)
  382. {
  383. struct acpi_device *adev = NULL;
  384. acpi_status status = acpi_get_devices(id, cros_ec_lpc_parse_device,
  385. &adev, NULL);
  386. if (ACPI_FAILURE(status)) {
  387. pr_warn(DRV_NAME ": Looking for %s failed\n", id);
  388. return NULL;
  389. }
  390. return adev;
  391. }
  392. static int cros_ec_lpc_probe(struct platform_device *pdev)
  393. {
  394. struct device *dev = &pdev->dev;
  395. struct acpi_device *adev;
  396. acpi_status status;
  397. struct cros_ec_device *ec_dev;
  398. struct cros_ec_lpc *ec_lpc;
  399. const struct lpc_driver_data *driver_data;
  400. u8 buf[2] = {};
  401. int irq, ret;
  402. u32 quirks;
  403. ec_lpc = devm_kzalloc(dev, sizeof(*ec_lpc), GFP_KERNEL);
  404. if (!ec_lpc)
  405. return -ENOMEM;
  406. ec_lpc->mmio_memory_base = EC_LPC_ADDR_MEMMAP;
  407. driver_data = platform_get_drvdata(pdev);
  408. if (!driver_data)
  409. driver_data = acpi_device_get_match_data(dev);
  410. if (driver_data) {
  411. quirks = driver_data->quirks;
  412. if (quirks)
  413. dev_info(dev, "loaded with quirks %8.08x\n", quirks);
  414. if (quirks & CROS_EC_LPC_QUIRK_REMAP_MEMORY)
  415. ec_lpc->mmio_memory_base = driver_data->quirk_mmio_memory_base;
  416. if (quirks & CROS_EC_LPC_QUIRK_ACPI_ID) {
  417. adev = cros_ec_lpc_get_device(driver_data->quirk_acpi_id);
  418. if (!adev) {
  419. dev_err(dev, "failed to get ACPI device '%s'",
  420. driver_data->quirk_acpi_id);
  421. return -ENODEV;
  422. }
  423. ACPI_COMPANION_SET(dev, adev);
  424. }
  425. if (quirks & CROS_EC_LPC_QUIRK_AML_MUTEX) {
  426. const char *name
  427. = driver_data->quirk_aml_mutex_name;
  428. ret = cros_ec_lpc_mec_acpi_mutex(ACPI_COMPANION(dev), name);
  429. if (ret) {
  430. dev_err(dev, "failed to get AML mutex '%s'", name);
  431. return ret;
  432. }
  433. dev_info(dev, "got AML mutex '%s'", name);
  434. }
  435. }
  436. /*
  437. * The Framework Laptop (and possibly other non-ChromeOS devices)
  438. * only exposes the eight I/O ports that are required for the Microchip EC.
  439. * Requesting a larger reservation will fail.
  440. */
  441. if (!devm_request_region(dev, EC_HOST_CMD_REGION0,
  442. EC_HOST_CMD_MEC_REGION_SIZE, dev_name(dev))) {
  443. dev_err(dev, "couldn't reserve MEC region\n");
  444. return -EBUSY;
  445. }
  446. cros_ec_lpc_mec_init(EC_HOST_CMD_REGION0,
  447. EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE);
  448. /*
  449. * Read the mapped ID twice, the first one is assuming the
  450. * EC is a Microchip Embedded Controller (MEC) variant, if the
  451. * protocol fails, fallback to the non MEC variant and try to
  452. * read again the ID.
  453. */
  454. cros_ec_lpc_ops.read = cros_ec_lpc_mec_read_bytes;
  455. cros_ec_lpc_ops.write = cros_ec_lpc_mec_write_bytes;
  456. ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
  457. if (ret < 0)
  458. return ret;
  459. if (buf[0] != 'E' || buf[1] != 'C') {
  460. if (!devm_request_region(dev, ec_lpc->mmio_memory_base, EC_MEMMAP_SIZE,
  461. dev_name(dev))) {
  462. dev_err(dev, "couldn't reserve memmap region\n");
  463. return -EBUSY;
  464. }
  465. /* Re-assign read/write operations for the non MEC variant */
  466. cros_ec_lpc_ops.read = cros_ec_lpc_read_bytes;
  467. cros_ec_lpc_ops.write = cros_ec_lpc_write_bytes;
  468. ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + EC_MEMMAP_ID, 2,
  469. buf);
  470. if (ret < 0)
  471. return ret;
  472. if (buf[0] != 'E' || buf[1] != 'C') {
  473. dev_err(dev, "EC ID not detected\n");
  474. return -ENODEV;
  475. }
  476. /* Reserve the remaining I/O ports required by the non-MEC protocol. */
  477. if (!devm_request_region(dev, EC_HOST_CMD_REGION0 + EC_HOST_CMD_MEC_REGION_SIZE,
  478. EC_HOST_CMD_REGION_SIZE - EC_HOST_CMD_MEC_REGION_SIZE,
  479. dev_name(dev))) {
  480. dev_err(dev, "couldn't reserve remainder of region0\n");
  481. return -EBUSY;
  482. }
  483. if (!devm_request_region(dev, EC_HOST_CMD_REGION1,
  484. EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
  485. dev_err(dev, "couldn't reserve region1\n");
  486. return -EBUSY;
  487. }
  488. }
  489. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  490. if (!ec_dev)
  491. return -ENOMEM;
  492. platform_set_drvdata(pdev, ec_dev);
  493. ec_dev->dev = dev;
  494. ec_dev->phys_name = dev_name(dev);
  495. ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc;
  496. ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
  497. ec_dev->cmd_readmem = cros_ec_lpc_readmem;
  498. ec_dev->din_size = sizeof(struct ec_host_response) +
  499. sizeof(struct ec_response_get_protocol_info);
  500. ec_dev->dout_size = sizeof(struct ec_host_request);
  501. ec_dev->priv = ec_lpc;
  502. /*
  503. * Some boards do not have an IRQ allotted for cros_ec_lpc,
  504. * which makes ENXIO an expected (and safe) scenario.
  505. */
  506. irq = platform_get_irq_optional(pdev, 0);
  507. if (irq > 0)
  508. ec_dev->irq = irq;
  509. else if (irq != -ENXIO) {
  510. dev_err(dev, "couldn't retrieve IRQ number (%d)\n", irq);
  511. return irq;
  512. }
  513. ret = cros_ec_register(ec_dev);
  514. if (ret) {
  515. dev_err(dev, "couldn't register ec_dev (%d)\n", ret);
  516. return ret;
  517. }
  518. /*
  519. * Connect a notify handler to process MKBP messages if we have a
  520. * companion ACPI device.
  521. */
  522. adev = ACPI_COMPANION(dev);
  523. if (adev) {
  524. status = acpi_install_notify_handler(adev->handle,
  525. ACPI_ALL_NOTIFY,
  526. cros_ec_lpc_acpi_notify,
  527. ec_dev);
  528. if (ACPI_FAILURE(status))
  529. dev_warn(dev, "Failed to register notifier %08x\n",
  530. status);
  531. }
  532. return 0;
  533. }
  534. static void cros_ec_lpc_remove(struct platform_device *pdev)
  535. {
  536. struct cros_ec_device *ec_dev = platform_get_drvdata(pdev);
  537. struct acpi_device *adev;
  538. adev = ACPI_COMPANION(&pdev->dev);
  539. if (adev)
  540. acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
  541. cros_ec_lpc_acpi_notify);
  542. cros_ec_unregister(ec_dev);
  543. }
  544. static const struct lpc_driver_data framework_laptop_npcx_lpc_driver_data __initconst = {
  545. .quirks = CROS_EC_LPC_QUIRK_REMAP_MEMORY,
  546. .quirk_mmio_memory_base = 0xE00,
  547. };
  548. static const struct lpc_driver_data framework_laptop_mec_lpc_driver_data __initconst = {
  549. .quirks = CROS_EC_LPC_QUIRK_ACPI_ID|CROS_EC_LPC_QUIRK_AML_MUTEX,
  550. .quirk_acpi_id = "PNP0C09",
  551. .quirk_aml_mutex_name = "ECMT",
  552. };
  553. static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = {
  554. { ACPI_DRV_NAME, 0 },
  555. { FRMW_ACPI_DRV_NAME, (kernel_ulong_t)&framework_laptop_npcx_lpc_driver_data },
  556. { }
  557. };
  558. MODULE_DEVICE_TABLE(acpi, cros_ec_lpc_acpi_device_ids);
  559. static const struct dmi_system_id cros_ec_lpc_dmi_table[] __initconst = {
  560. {
  561. /*
  562. * Today all Chromebooks/boxes ship with Google_* as version and
  563. * coreboot as bios vendor. No other systems with this
  564. * combination are known to date.
  565. */
  566. .matches = {
  567. DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
  568. DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
  569. },
  570. },
  571. {
  572. /*
  573. * If the box is running custom coreboot firmware then the
  574. * DMI BIOS version string will not be matched by "Google_",
  575. * but the system vendor string will still be matched by
  576. * "GOOGLE".
  577. */
  578. .matches = {
  579. DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
  580. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  581. },
  582. },
  583. {
  584. /* x86-link, the Chromebook Pixel. */
  585. .matches = {
  586. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  587. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  588. },
  589. },
  590. {
  591. /* x86-samus, the Chromebook Pixel 2. */
  592. .matches = {
  593. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  594. DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
  595. },
  596. },
  597. {
  598. /* x86-peppy, the Acer C720 Chromebook. */
  599. .matches = {
  600. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  601. DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
  602. },
  603. },
  604. {
  605. /* x86-glimmer, the Lenovo Thinkpad Yoga 11e. */
  606. .matches = {
  607. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  608. DMI_MATCH(DMI_PRODUCT_NAME, "Glimmer"),
  609. },
  610. },
  611. /* A small number of non-Chromebook/box machines also use the ChromeOS EC */
  612. {
  613. /* Framework Laptop (11th Gen Intel Core) */
  614. .matches = {
  615. DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
  616. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Laptop"),
  617. },
  618. .driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
  619. },
  620. {
  621. /* Framework Laptop (12th Gen Intel Core) */
  622. .matches = {
  623. DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
  624. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Laptop (12th Gen Intel Core)"),
  625. },
  626. .driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
  627. },
  628. {
  629. /* Framework Laptop (13th Gen Intel Core) */
  630. .matches = {
  631. DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
  632. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Laptop (13th Gen Intel Core)"),
  633. },
  634. .driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
  635. },
  636. {
  637. /*
  638. * All remaining Framework Laptop models (13 AMD Ryzen, 16 AMD
  639. * Ryzen, Intel Core Ultra)
  640. */
  641. .matches = {
  642. DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
  643. DMI_MATCH(DMI_PRODUCT_FAMILY, "Laptop"),
  644. },
  645. .driver_data = (void *)&framework_laptop_npcx_lpc_driver_data,
  646. },
  647. { /* sentinel */ }
  648. };
  649. MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
  650. #ifdef CONFIG_PM_SLEEP
  651. static int cros_ec_lpc_prepare(struct device *dev)
  652. {
  653. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  654. return cros_ec_suspend_prepare(ec_dev);
  655. }
  656. static void cros_ec_lpc_complete(struct device *dev)
  657. {
  658. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  659. cros_ec_resume_complete(ec_dev);
  660. }
  661. static int cros_ec_lpc_suspend_late(struct device *dev)
  662. {
  663. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  664. return cros_ec_suspend_late(ec_dev);
  665. }
  666. static int cros_ec_lpc_resume_early(struct device *dev)
  667. {
  668. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  669. return cros_ec_resume_early(ec_dev);
  670. }
  671. #endif
  672. static const struct dev_pm_ops cros_ec_lpc_pm_ops = {
  673. #ifdef CONFIG_PM_SLEEP
  674. .prepare = cros_ec_lpc_prepare,
  675. .complete = cros_ec_lpc_complete,
  676. #endif
  677. SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_lpc_suspend_late, cros_ec_lpc_resume_early)
  678. };
  679. static struct platform_driver cros_ec_lpc_driver = {
  680. .driver = {
  681. .name = DRV_NAME,
  682. .acpi_match_table = cros_ec_lpc_acpi_device_ids,
  683. .pm = &cros_ec_lpc_pm_ops,
  684. /*
  685. * ACPI child devices may probe before us, and they racily
  686. * check our drvdata pointer. Force synchronous probe until
  687. * those races are resolved.
  688. */
  689. .probe_type = PROBE_FORCE_SYNCHRONOUS,
  690. },
  691. .probe = cros_ec_lpc_probe,
  692. .remove_new = cros_ec_lpc_remove,
  693. };
  694. static struct platform_device cros_ec_lpc_device = {
  695. .name = DRV_NAME
  696. };
  697. static int __init cros_ec_lpc_init(void)
  698. {
  699. int ret;
  700. const struct dmi_system_id *dmi_match;
  701. cros_ec_lpc_acpi_device_found = !!cros_ec_lpc_get_device(ACPI_DRV_NAME) ||
  702. !!cros_ec_lpc_get_device(FRMW_ACPI_DRV_NAME);
  703. dmi_match = dmi_first_match(cros_ec_lpc_dmi_table);
  704. if (!cros_ec_lpc_acpi_device_found && !dmi_match) {
  705. pr_err(DRV_NAME ": unsupported system.\n");
  706. return -ENODEV;
  707. }
  708. /* Register the driver */
  709. ret = platform_driver_register(&cros_ec_lpc_driver);
  710. if (ret) {
  711. pr_err(DRV_NAME ": can't register driver: %d\n", ret);
  712. return ret;
  713. }
  714. if (!cros_ec_lpc_acpi_device_found) {
  715. /* Pass the DMI match's driver data down to the platform device */
  716. platform_set_drvdata(&cros_ec_lpc_device, dmi_match->driver_data);
  717. /* Register the device, and it'll get hooked up automatically */
  718. ret = platform_device_register(&cros_ec_lpc_device);
  719. if (ret) {
  720. pr_err(DRV_NAME ": can't register device: %d\n", ret);
  721. platform_driver_unregister(&cros_ec_lpc_driver);
  722. }
  723. }
  724. return ret;
  725. }
  726. static void __exit cros_ec_lpc_exit(void)
  727. {
  728. if (!cros_ec_lpc_acpi_device_found)
  729. platform_device_unregister(&cros_ec_lpc_device);
  730. platform_driver_unregister(&cros_ec_lpc_driver);
  731. }
  732. module_init(cros_ec_lpc_init);
  733. module_exit(cros_ec_lpc_exit);
  734. MODULE_LICENSE("GPL");
  735. MODULE_DESCRIPTION("ChromeOS EC LPC driver");