xref: /linux/drivers/platform/chrome/cros_ec_lpc.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
11058ca94SEnric Balletbo i Serra // SPDX-License-Identifier: GPL-2.0
21058ca94SEnric Balletbo i Serra // LPC interface for ChromeOS Embedded Controller
31058ca94SEnric Balletbo i Serra //
41058ca94SEnric Balletbo i Serra // Copyright (C) 2012-2015 Google, Inc
51058ca94SEnric Balletbo i Serra //
61058ca94SEnric Balletbo i Serra // This driver uses the ChromeOS EC byte-level message-based protocol for
71058ca94SEnric Balletbo i Serra // communicating the keyboard state (which keys are pressed) from a keyboard EC
81058ca94SEnric Balletbo i Serra // to the AP over some bus (such as i2c, lpc, spi).  The EC does debouncing,
91058ca94SEnric Balletbo i Serra // but everything else (including deghosting) is done here.  The main
101058ca94SEnric Balletbo i Serra // motivation for this is to keep the EC firmware as simple as possible, since
111058ca94SEnric Balletbo i Serra // it cannot be easily upgraded and EC flash/IRAM space is relatively
121058ca94SEnric Balletbo i Serra // expensive.
13ec2f33abSBill Richardson 
1412278dc7SGwendal Grignou #include <linux/acpi.h>
15ec2f33abSBill Richardson #include <linux/dmi.h>
16ec2f33abSBill Richardson #include <linux/delay.h>
1718d0dc24SJavier Martinez Canillas #include <linux/io.h>
18da1cf5a1SEnrico Granata #include <linux/interrupt.h>
192cbf475aSRob Barnes #include <linux/kobject.h>
20ec2f33abSBill Richardson #include <linux/module.h>
21840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h>
22840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_proto.h>
23ec2f33abSBill Richardson #include <linux/platform_device.h>
24ec2f33abSBill Richardson #include <linux/printk.h>
25957445d7SRob Barnes #include <linux/reboot.h>
26d6542b39SWenkai Du #include <linux/suspend.h>
27ec2f33abSBill Richardson 
28034dbec1SEnric Balletbo i Serra #include "cros_ec.h"
294116fd25SEnric Balletbo i Serra #include "cros_ec_lpc_mec.h"
30cc8a4ea1SEnric Balletbo i Serra 
31bce70fefSShawn Nematbakhsh #define DRV_NAME "cros_ec_lpcs"
3212278dc7SGwendal Grignou #define ACPI_DRV_NAME "GOOG0004"
33ec2f33abSBill Richardson 
345f454bdfSEnric Balletbo i Serra /* True if ACPI device is present */
355f454bdfSEnric Balletbo i Serra static bool cros_ec_lpc_acpi_device_found;
365f454bdfSEnric Balletbo i Serra 
37e4dbf9d6SDustin L. Howett /*
38e4dbf9d6SDustin L. Howett  * Indicates that lpc_driver_data.quirk_mmio_memory_base should
39e4dbf9d6SDustin L. Howett  * be used as the base port for EC mapped memory.
40e4dbf9d6SDustin L. Howett  */
41e4dbf9d6SDustin L. Howett #define CROS_EC_LPC_QUIRK_REMAP_MEMORY              BIT(0)
42040159e0SBen Walsh /*
43040159e0SBen Walsh  * Indicates that lpc_driver_data.quirk_acpi_id should be used to find
44040159e0SBen Walsh  * the ACPI device.
45040159e0SBen Walsh  */
46040159e0SBen Walsh #define CROS_EC_LPC_QUIRK_ACPI_ID                   BIT(1)
4738c31b1dSBen Walsh /*
4838c31b1dSBen Walsh  * Indicates that lpc_driver_data.quirk_aml_mutex_name should be used
4938c31b1dSBen Walsh  * to find an AML mutex to protect access to Microchip EC.
5038c31b1dSBen Walsh  */
5138c31b1dSBen Walsh #define CROS_EC_LPC_QUIRK_AML_MUTEX                 BIT(2)
52e4dbf9d6SDustin L. Howett 
53e4dbf9d6SDustin L. Howett /**
54e4dbf9d6SDustin L. Howett  * struct lpc_driver_data - driver data attached to a DMI device ID to indicate
55e4dbf9d6SDustin L. Howett  *                          hardware quirks.
56e4dbf9d6SDustin L. Howett  * @quirks: a bitfield composed of quirks from CROS_EC_LPC_QUIRK_*
57e4dbf9d6SDustin L. Howett  * @quirk_mmio_memory_base: The first I/O port addressing EC mapped memory (used
58e4dbf9d6SDustin L. Howett  *                          when quirk ...REMAP_MEMORY is set.)
59040159e0SBen Walsh  * @quirk_acpi_id: An ACPI HID to be used to find the ACPI device.
6038c31b1dSBen Walsh  * @quirk_aml_mutex_name: The name of an AML mutex to be used to protect access
6138c31b1dSBen Walsh  *                        to Microchip EC.
62e4dbf9d6SDustin L. Howett  */
63e4dbf9d6SDustin L. Howett struct lpc_driver_data {
64e4dbf9d6SDustin L. Howett 	u32 quirks;
65e4dbf9d6SDustin L. Howett 	u16 quirk_mmio_memory_base;
66040159e0SBen Walsh 	const char *quirk_acpi_id;
6738c31b1dSBen Walsh 	const char *quirk_aml_mutex_name;
68e4dbf9d6SDustin L. Howett };
69e4dbf9d6SDustin L. Howett 
7022c040faSEnric Balletbo i Serra /**
718d4a9c69SDustin L. Howett  * struct cros_ec_lpc - LPC device-specific data
728d4a9c69SDustin L. Howett  * @mmio_memory_base: The first I/O port addressing EC mapped memory.
738d4a9c69SDustin L. Howett  */
748d4a9c69SDustin L. Howett struct cros_ec_lpc {
758d4a9c69SDustin L. Howett 	u16 mmio_memory_base;
768d4a9c69SDustin L. Howett };
778d4a9c69SDustin L. Howett 
788d4a9c69SDustin L. Howett /**
7922c040faSEnric Balletbo i Serra  * struct lpc_driver_ops - LPC driver operations
8068dbac0aSBen Walsh  * @read: Copy length bytes from EC address offset into buffer dest.
8168dbac0aSBen Walsh  *        Returns a negative error code on error, or the 8-bit checksum
8268dbac0aSBen Walsh  *        of all bytes read.
8368dbac0aSBen Walsh  * @write: Copy length bytes from buffer msg into EC address offset.
8468dbac0aSBen Walsh  *         Returns a negative error code on error, or the 8-bit checksum
8568dbac0aSBen Walsh  *         of all bytes written.
8622c040faSEnric Balletbo i Serra  */
8722c040faSEnric Balletbo i Serra struct lpc_driver_ops {
8868dbac0aSBen Walsh 	int (*read)(unsigned int offset, unsigned int length, u8 *dest);
8968dbac0aSBen Walsh 	int (*write)(unsigned int offset, unsigned int length, const u8 *msg);
9022c040faSEnric Balletbo i Serra };
9122c040faSEnric Balletbo i Serra 
9222c040faSEnric Balletbo i Serra static struct lpc_driver_ops cros_ec_lpc_ops = { };
9322c040faSEnric Balletbo i Serra 
9422c040faSEnric Balletbo i Serra /*
9522c040faSEnric Balletbo i Serra  * A generic instance of the read function of struct lpc_driver_ops, used for
9622c040faSEnric Balletbo i Serra  * the LPC EC.
9722c040faSEnric Balletbo i Serra  */
cros_ec_lpc_read_bytes(unsigned int offset,unsigned int length,u8 * dest)9868dbac0aSBen Walsh static int cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length,
9922c040faSEnric Balletbo i Serra 				  u8 *dest)
1004116fd25SEnric Balletbo i Serra {
10168dbac0aSBen Walsh 	u8 sum = 0;
1024116fd25SEnric Balletbo i Serra 	int i;
1034116fd25SEnric Balletbo i Serra 
1044116fd25SEnric Balletbo i Serra 	for (i = 0; i < length; ++i) {
1054116fd25SEnric Balletbo i Serra 		dest[i] = inb(offset + i);
1064116fd25SEnric Balletbo i Serra 		sum += dest[i];
1074116fd25SEnric Balletbo i Serra 	}
1084116fd25SEnric Balletbo i Serra 
1094116fd25SEnric Balletbo i Serra 	/* Return checksum of all bytes read */
1104116fd25SEnric Balletbo i Serra 	return sum;
1114116fd25SEnric Balletbo i Serra }
1124116fd25SEnric Balletbo i Serra 
11322c040faSEnric Balletbo i Serra /*
11422c040faSEnric Balletbo i Serra  * A generic instance of the write function of struct lpc_driver_ops, used for
11522c040faSEnric Balletbo i Serra  * the LPC EC.
11622c040faSEnric Balletbo i Serra  */
cros_ec_lpc_write_bytes(unsigned int offset,unsigned int length,const u8 * msg)11768dbac0aSBen Walsh static int cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length,
11822c040faSEnric Balletbo i Serra 				   const u8 *msg)
1194116fd25SEnric Balletbo i Serra {
12068dbac0aSBen Walsh 	u8 sum = 0;
1214116fd25SEnric Balletbo i Serra 	int i;
1224116fd25SEnric Balletbo i Serra 
1234116fd25SEnric Balletbo i Serra 	for (i = 0; i < length; ++i) {
1244116fd25SEnric Balletbo i Serra 		outb(msg[i], offset + i);
1254116fd25SEnric Balletbo i Serra 		sum += msg[i];
1264116fd25SEnric Balletbo i Serra 	}
1274116fd25SEnric Balletbo i Serra 
1284116fd25SEnric Balletbo i Serra 	/* Return checksum of all bytes written */
1294116fd25SEnric Balletbo i Serra 	return sum;
1304116fd25SEnric Balletbo i Serra }
1314116fd25SEnric Balletbo i Serra 
13222c040faSEnric Balletbo i Serra /*
13322c040faSEnric Balletbo i Serra  * An instance of the read function of struct lpc_driver_ops, used for the
13422c040faSEnric Balletbo i Serra  * MEC variant of LPC EC.
13522c040faSEnric Balletbo i Serra  */
cros_ec_lpc_mec_read_bytes(unsigned int offset,unsigned int length,u8 * dest)13668dbac0aSBen Walsh static int cros_ec_lpc_mec_read_bytes(unsigned int offset, unsigned int length,
1374116fd25SEnric Balletbo i Serra 				      u8 *dest)
1384116fd25SEnric Balletbo i Serra {
1394116fd25SEnric Balletbo i Serra 	int in_range = cros_ec_lpc_mec_in_range(offset, length);
1404116fd25SEnric Balletbo i Serra 
1414116fd25SEnric Balletbo i Serra 	if (in_range < 0)
14277a71432SDan Carpenter 		return in_range;
1434116fd25SEnric Balletbo i Serra 
1444116fd25SEnric Balletbo i Serra 	return in_range ?
1454116fd25SEnric Balletbo i Serra 		cros_ec_lpc_io_bytes_mec(MEC_IO_READ,
1464116fd25SEnric Balletbo i Serra 					 offset - EC_HOST_CMD_REGION0,
1474116fd25SEnric Balletbo i Serra 					 length, dest) :
14822c040faSEnric Balletbo i Serra 		cros_ec_lpc_read_bytes(offset, length, dest);
1494116fd25SEnric Balletbo i Serra }
1504116fd25SEnric Balletbo i Serra 
15122c040faSEnric Balletbo i Serra /*
15222c040faSEnric Balletbo i Serra  * An instance of the write function of struct lpc_driver_ops, used for the
15322c040faSEnric Balletbo i Serra  * MEC variant of LPC EC.
15422c040faSEnric Balletbo i Serra  */
cros_ec_lpc_mec_write_bytes(unsigned int offset,unsigned int length,const u8 * msg)15568dbac0aSBen Walsh static int cros_ec_lpc_mec_write_bytes(unsigned int offset, unsigned int length,
15622c040faSEnric Balletbo i Serra 				       const u8 *msg)
1574116fd25SEnric Balletbo i Serra {
1584116fd25SEnric Balletbo i Serra 	int in_range = cros_ec_lpc_mec_in_range(offset, length);
1594116fd25SEnric Balletbo i Serra 
1604116fd25SEnric Balletbo i Serra 	if (in_range < 0)
16177a71432SDan Carpenter 		return in_range;
1624116fd25SEnric Balletbo i Serra 
1634116fd25SEnric Balletbo i Serra 	return in_range ?
1644116fd25SEnric Balletbo i Serra 		cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE,
1654116fd25SEnric Balletbo i Serra 					 offset - EC_HOST_CMD_REGION0,
16622c040faSEnric Balletbo i Serra 					 length, (u8 *)msg) :
16722c040faSEnric Balletbo i Serra 		cros_ec_lpc_write_bytes(offset, length, msg);
1684116fd25SEnric Balletbo i Serra }
1694116fd25SEnric Balletbo i Serra 
ec_response_timed_out(void)170ec2f33abSBill Richardson static int ec_response_timed_out(void)
171ec2f33abSBill Richardson {
172ec2f33abSBill Richardson 	unsigned long one_second = jiffies + HZ;
173bce70fefSShawn Nematbakhsh 	u8 data;
17468dbac0aSBen Walsh 	int ret;
175ec2f33abSBill Richardson 
176ec2f33abSBill Richardson 	usleep_range(200, 300);
177ec2f33abSBill Richardson 	do {
17868dbac0aSBen Walsh 		ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_CMD, 1, &data);
17968dbac0aSBen Walsh 		if (ret < 0)
18068dbac0aSBen Walsh 			return ret;
18168dbac0aSBen Walsh 		if (!(data & EC_LPC_STATUS_BUSY_MASK))
182ec2f33abSBill Richardson 			return 0;
183ec2f33abSBill Richardson 		usleep_range(100, 200);
184ec2f33abSBill Richardson 	} while (time_before(jiffies, one_second));
185ec2f33abSBill Richardson 
186ec2f33abSBill Richardson 	return 1;
187ec2f33abSBill Richardson }
188ec2f33abSBill Richardson 
cros_ec_pkt_xfer_lpc(struct cros_ec_device * ec,struct cros_ec_command * msg)189d3654070SStephen Barber static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
190d3654070SStephen Barber 				struct cros_ec_command *msg)
191d3654070SStephen Barber {
192d3654070SStephen Barber 	struct ec_host_response response;
193bce70fefSShawn Nematbakhsh 	u8 sum;
194d3654070SStephen Barber 	int ret = 0;
195d3654070SStephen Barber 	u8 *dout;
196d3654070SStephen Barber 
197d3654070SStephen Barber 	ret = cros_ec_prepare_tx(ec, msg);
19871d3ae7fSTzung-Bi Shih 	if (ret < 0)
19971d3ae7fSTzung-Bi Shih 		goto done;
200d3654070SStephen Barber 
201d3654070SStephen Barber 	/* Write buffer */
20268dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
20368dbac0aSBen Walsh 	if (ret < 0)
20468dbac0aSBen Walsh 		goto done;
205d3654070SStephen Barber 
206d3654070SStephen Barber 	/* Here we go */
207bce70fefSShawn Nematbakhsh 	sum = EC_COMMAND_PROTOCOL_3;
20868dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
20968dbac0aSBen Walsh 	if (ret < 0)
21068dbac0aSBen Walsh 		goto done;
211d3654070SStephen Barber 
21268dbac0aSBen Walsh 	ret = ec_response_timed_out();
21368dbac0aSBen Walsh 	if (ret < 0)
21468dbac0aSBen Walsh 		goto done;
21568dbac0aSBen Walsh 	if (ret) {
216eb057514SColin Ian King 		dev_warn(ec->dev, "EC response timed out\n");
217d3654070SStephen Barber 		ret = -EIO;
218d3654070SStephen Barber 		goto done;
219d3654070SStephen Barber 	}
220d3654070SStephen Barber 
221d3654070SStephen Barber 	/* Check result */
22268dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
22368dbac0aSBen Walsh 	if (ret < 0)
22468dbac0aSBen Walsh 		goto done;
22568dbac0aSBen Walsh 	msg->result = ret;
226d3654070SStephen Barber 	ret = cros_ec_check_result(ec, msg);
227d3654070SStephen Barber 	if (ret)
228d3654070SStephen Barber 		goto done;
229d3654070SStephen Barber 
230d3654070SStephen Barber 	/* Read back response */
231d3654070SStephen Barber 	dout = (u8 *)&response;
23268dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET, sizeof(response),
233bce70fefSShawn Nematbakhsh 				   dout);
23468dbac0aSBen Walsh 	if (ret < 0)
23568dbac0aSBen Walsh 		goto done;
23668dbac0aSBen Walsh 	sum = ret;
237d3654070SStephen Barber 
238d3654070SStephen Barber 	msg->result = response.result;
239d3654070SStephen Barber 
240d3654070SStephen Barber 	if (response.data_len > msg->insize) {
241d3654070SStephen Barber 		dev_err(ec->dev,
242d3654070SStephen Barber 			"packet too long (%d bytes, expected %d)",
243d3654070SStephen Barber 			response.data_len, msg->insize);
244d3654070SStephen Barber 		ret = -EMSGSIZE;
245d3654070SStephen Barber 		goto done;
246d3654070SStephen Barber 	}
247d3654070SStephen Barber 
248d3654070SStephen Barber 	/* Read response and process checksum */
24968dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET +
250bce70fefSShawn Nematbakhsh 				   sizeof(response), response.data_len,
251bce70fefSShawn Nematbakhsh 				   msg->data);
25268dbac0aSBen Walsh 	if (ret < 0)
25368dbac0aSBen Walsh 		goto done;
25468dbac0aSBen Walsh 	sum += ret;
255d3654070SStephen Barber 
256d3654070SStephen Barber 	if (sum) {
257d3654070SStephen Barber 		dev_err(ec->dev,
258d3654070SStephen Barber 			"bad packet checksum %02x\n",
259d3654070SStephen Barber 			response.checksum);
260d3654070SStephen Barber 		ret = -EBADMSG;
261d3654070SStephen Barber 		goto done;
262d3654070SStephen Barber 	}
263d3654070SStephen Barber 
264d3654070SStephen Barber 	/* Return actual amount of data received */
265d3654070SStephen Barber 	ret = response.data_len;
266d3654070SStephen Barber done:
267d3654070SStephen Barber 	return ret;
268d3654070SStephen Barber }
269d3654070SStephen Barber 
cros_ec_cmd_xfer_lpc(struct cros_ec_device * ec,struct cros_ec_command * msg)270ec2f33abSBill Richardson static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
271ec2f33abSBill Richardson 				struct cros_ec_command *msg)
272ec2f33abSBill Richardson {
273ec2f33abSBill Richardson 	struct ec_lpc_host_args args;
274bce70fefSShawn Nematbakhsh 	u8 sum;
275ec2f33abSBill Richardson 	int ret = 0;
276ec2f33abSBill Richardson 
277ec2f33abSBill Richardson 	if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
278ec2f33abSBill Richardson 	    msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
279ec2f33abSBill Richardson 		dev_err(ec->dev,
280ec2f33abSBill Richardson 			"invalid buffer sizes (out %d, in %d)\n",
281ec2f33abSBill Richardson 			msg->outsize, msg->insize);
282ec2f33abSBill Richardson 		return -EINVAL;
283ec2f33abSBill Richardson 	}
284ec2f33abSBill Richardson 
285ec2f33abSBill Richardson 	/* Now actually send the command to the EC and get the result */
286ec2f33abSBill Richardson 	args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
287ec2f33abSBill Richardson 	args.command_version = msg->version;
288ec2f33abSBill Richardson 	args.data_size = msg->outsize;
289ec2f33abSBill Richardson 
290ec2f33abSBill Richardson 	/* Initialize checksum */
291bce70fefSShawn Nematbakhsh 	sum = msg->command + args.flags + args.command_version + args.data_size;
292ec2f33abSBill Richardson 
293ec2f33abSBill Richardson 	/* Copy data and update checksum */
29468dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PARAM, msg->outsize,
295bce70fefSShawn Nematbakhsh 				    msg->data);
29668dbac0aSBen Walsh 	if (ret < 0)
29768dbac0aSBen Walsh 		goto done;
29868dbac0aSBen Walsh 	sum += ret;
299ec2f33abSBill Richardson 
300ec2f33abSBill Richardson 	/* Finalize checksum and write args */
301bce70fefSShawn Nematbakhsh 	args.checksum = sum;
30268dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
303bce70fefSShawn Nematbakhsh 				    (u8 *)&args);
30468dbac0aSBen Walsh 	if (ret < 0)
30568dbac0aSBen Walsh 		goto done;
306ec2f33abSBill Richardson 
307ec2f33abSBill Richardson 	/* Here we go */
308bce70fefSShawn Nematbakhsh 	sum = msg->command;
30968dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
31068dbac0aSBen Walsh 	if (ret < 0)
31168dbac0aSBen Walsh 		goto done;
312ec2f33abSBill Richardson 
31368dbac0aSBen Walsh 	ret = ec_response_timed_out();
31468dbac0aSBen Walsh 	if (ret < 0)
31568dbac0aSBen Walsh 		goto done;
31668dbac0aSBen Walsh 	if (ret) {
317eb057514SColin Ian King 		dev_warn(ec->dev, "EC response timed out\n");
318ec2f33abSBill Richardson 		ret = -EIO;
319ec2f33abSBill Richardson 		goto done;
320ec2f33abSBill Richardson 	}
321ec2f33abSBill Richardson 
322ec2f33abSBill Richardson 	/* Check result */
32368dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
32468dbac0aSBen Walsh 	if (ret < 0)
32568dbac0aSBen Walsh 		goto done;
32668dbac0aSBen Walsh 	msg->result = ret;
327fbf40727SJavier Martinez Canillas 	ret = cros_ec_check_result(ec, msg);
328fbf40727SJavier Martinez Canillas 	if (ret)
329ec2f33abSBill Richardson 		goto done;
330ec2f33abSBill Richardson 
331ec2f33abSBill Richardson 	/* Read back args */
33268dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_ARGS, sizeof(args), (u8 *)&args);
33368dbac0aSBen Walsh 	if (ret < 0)
33468dbac0aSBen Walsh 		goto done;
335ec2f33abSBill Richardson 
336ec2f33abSBill Richardson 	if (args.data_size > msg->insize) {
337ec2f33abSBill Richardson 		dev_err(ec->dev,
338ec2f33abSBill Richardson 			"packet too long (%d bytes, expected %d)",
339ec2f33abSBill Richardson 			args.data_size, msg->insize);
340ec2f33abSBill Richardson 		ret = -ENOSPC;
341ec2f33abSBill Richardson 		goto done;
342ec2f33abSBill Richardson 	}
343ec2f33abSBill Richardson 
344ec2f33abSBill Richardson 	/* Start calculating response checksum */
345bce70fefSShawn Nematbakhsh 	sum = msg->command + args.flags + args.command_version + args.data_size;
346ec2f33abSBill Richardson 
347ec2f33abSBill Richardson 	/* Read response and update checksum */
34868dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PARAM, args.data_size,
349bce70fefSShawn Nematbakhsh 				   msg->data);
35068dbac0aSBen Walsh 	if (ret < 0)
35168dbac0aSBen Walsh 		goto done;
35268dbac0aSBen Walsh 	sum += ret;
353ec2f33abSBill Richardson 
354ec2f33abSBill Richardson 	/* Verify checksum */
355bce70fefSShawn Nematbakhsh 	if (args.checksum != sum) {
356ec2f33abSBill Richardson 		dev_err(ec->dev,
357ec2f33abSBill Richardson 			"bad packet checksum, expected %02x, got %02x\n",
358bce70fefSShawn Nematbakhsh 			args.checksum, sum);
359ec2f33abSBill Richardson 		ret = -EBADMSG;
360ec2f33abSBill Richardson 		goto done;
361ec2f33abSBill Richardson 	}
362ec2f33abSBill Richardson 
363ec2f33abSBill Richardson 	/* Return actual amount of data received */
364ec2f33abSBill Richardson 	ret = args.data_size;
365ec2f33abSBill Richardson done:
366ec2f33abSBill Richardson 	return ret;
367ec2f33abSBill Richardson }
368ec2f33abSBill Richardson 
369ec2f33abSBill Richardson /* Returns num bytes read, or negative on error. Doesn't need locking. */
cros_ec_lpc_readmem(struct cros_ec_device * ec,unsigned int offset,unsigned int bytes,void * dest)370ec2f33abSBill Richardson static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
371ec2f33abSBill Richardson 			       unsigned int bytes, void *dest)
372ec2f33abSBill Richardson {
3738d4a9c69SDustin L. Howett 	struct cros_ec_lpc *ec_lpc = ec->priv;
374ec2f33abSBill Richardson 	int i = offset;
375ec2f33abSBill Richardson 	char *s = dest;
376ec2f33abSBill Richardson 	int cnt = 0;
37768dbac0aSBen Walsh 	int ret;
378ec2f33abSBill Richardson 
379ec2f33abSBill Richardson 	if (offset >= EC_MEMMAP_SIZE - bytes)
380ec2f33abSBill Richardson 		return -EINVAL;
381ec2f33abSBill Richardson 
382ec2f33abSBill Richardson 	/* fixed length */
383ec2f33abSBill Richardson 	if (bytes) {
38468dbac0aSBen Walsh 		ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + offset, bytes, s);
38568dbac0aSBen Walsh 		if (ret < 0)
38668dbac0aSBen Walsh 			return ret;
387bce70fefSShawn Nematbakhsh 		return bytes;
388ec2f33abSBill Richardson 	}
389ec2f33abSBill Richardson 
390ec2f33abSBill Richardson 	/* string */
391ec2f33abSBill Richardson 	for (; i < EC_MEMMAP_SIZE; i++, s++) {
39268dbac0aSBen Walsh 		ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + i, 1, s);
39368dbac0aSBen Walsh 		if (ret < 0)
39468dbac0aSBen Walsh 			return ret;
395ec2f33abSBill Richardson 		cnt++;
396ec2f33abSBill Richardson 		if (!*s)
397ec2f33abSBill Richardson 			break;
398ec2f33abSBill Richardson 	}
399ec2f33abSBill Richardson 
400ec2f33abSBill Richardson 	return cnt;
401ec2f33abSBill Richardson }
402ec2f33abSBill Richardson 
cros_ec_lpc_acpi_notify(acpi_handle device,u32 value,void * data)403a6df7798SGwendal Grignou static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
404a6df7798SGwendal Grignou {
4052cbf475aSRob Barnes 	static const char *env[] = { "ERROR=PANIC", NULL };
406a6df7798SGwendal Grignou 	struct cros_ec_device *ec_dev = data;
4073300fdd6SEnrico Granata 	bool ec_has_more_events;
4083300fdd6SEnrico Granata 	int ret;
409a6df7798SGwendal Grignou 
41005a3c420SGwendal Grignou 	ec_dev->last_event_time = cros_ec_get_time_ns();
41105a3c420SGwendal Grignou 
412d90fa2c6SRob Barnes 	if (value == ACPI_NOTIFY_CROS_EC_PANIC) {
413d90fa2c6SRob Barnes 		dev_emerg(ec_dev->dev, "CrOS EC Panic Reported. Shutdown is imminent!");
414d90fa2c6SRob Barnes 		blocking_notifier_call_chain(&ec_dev->panic_notifier, 0, ec_dev);
4152cbf475aSRob Barnes 		kobject_uevent_env(&ec_dev->dev->kobj, KOBJ_CHANGE, (char **)env);
416f2d4dcedSRob Barnes 		/* Begin orderly shutdown. EC will force reset after a short period. */
417f2d4dcedSRob Barnes 		hw_protection_shutdown("CrOS EC Panic", -1);
418d90fa2c6SRob Barnes 		/* Do not query for other events after a panic is reported */
419d90fa2c6SRob Barnes 		return;
420d90fa2c6SRob Barnes 	}
421d90fa2c6SRob Barnes 
4223300fdd6SEnrico Granata 	if (ec_dev->mkbp_event_supported)
4233300fdd6SEnrico Granata 		do {
4243300fdd6SEnrico Granata 			ret = cros_ec_get_next_event(ec_dev, NULL,
4253300fdd6SEnrico Granata 						     &ec_has_more_events);
4263300fdd6SEnrico Granata 			if (ret > 0)
4273300fdd6SEnrico Granata 				blocking_notifier_call_chain(
4283300fdd6SEnrico Granata 						&ec_dev->event_notifier, 0,
429a6df7798SGwendal Grignou 						ec_dev);
4303300fdd6SEnrico Granata 		} while (ec_has_more_events);
431d6542b39SWenkai Du 
432d6542b39SWenkai Du 	if (value == ACPI_NOTIFY_DEVICE_WAKE)
433d6542b39SWenkai Du 		pm_system_wakeup();
434a6df7798SGwendal Grignou }
435a6df7798SGwendal Grignou 
cros_ec_lpc_parse_device(acpi_handle handle,u32 level,void * context,void ** retval)436040159e0SBen Walsh static acpi_status cros_ec_lpc_parse_device(acpi_handle handle, u32 level,
437040159e0SBen Walsh 					    void *context, void **retval)
438040159e0SBen Walsh {
439040159e0SBen Walsh 	*(struct acpi_device **)context = acpi_fetch_acpi_dev(handle);
440040159e0SBen Walsh 	return AE_CTRL_TERMINATE;
441040159e0SBen Walsh }
442040159e0SBen Walsh 
cros_ec_lpc_get_device(const char * id)443040159e0SBen Walsh static struct acpi_device *cros_ec_lpc_get_device(const char *id)
444040159e0SBen Walsh {
445040159e0SBen Walsh 	struct acpi_device *adev = NULL;
446040159e0SBen Walsh 	acpi_status status = acpi_get_devices(id, cros_ec_lpc_parse_device,
447040159e0SBen Walsh 					      &adev, NULL);
448040159e0SBen Walsh 	if (ACPI_FAILURE(status)) {
449040159e0SBen Walsh 		pr_warn(DRV_NAME ": Looking for %s failed\n", id);
450040159e0SBen Walsh 		return NULL;
451040159e0SBen Walsh 	}
452040159e0SBen Walsh 
453040159e0SBen Walsh 	return adev;
454040159e0SBen Walsh }
455040159e0SBen Walsh 
cros_ec_lpc_probe(struct platform_device * pdev)456ec2f33abSBill Richardson static int cros_ec_lpc_probe(struct platform_device *pdev)
457ec2f33abSBill Richardson {
458ec2f33abSBill Richardson 	struct device *dev = &pdev->dev;
459a6df7798SGwendal Grignou 	struct acpi_device *adev;
460a6df7798SGwendal Grignou 	acpi_status status;
461ec2f33abSBill Richardson 	struct cros_ec_device *ec_dev;
4628d4a9c69SDustin L. Howett 	struct cros_ec_lpc *ec_lpc;
463e4dbf9d6SDustin L. Howett 	struct lpc_driver_data *driver_data;
4642ae3c610STom Rix 	u8 buf[2] = {};
465da1cf5a1SEnrico Granata 	int irq, ret;
466e4dbf9d6SDustin L. Howett 	u32 quirks;
467ec2f33abSBill Richardson 
4688d4a9c69SDustin L. Howett 	ec_lpc = devm_kzalloc(dev, sizeof(*ec_lpc), GFP_KERNEL);
4698d4a9c69SDustin L. Howett 	if (!ec_lpc)
4708d4a9c69SDustin L. Howett 		return -ENOMEM;
4718d4a9c69SDustin L. Howett 
4728d4a9c69SDustin L. Howett 	ec_lpc->mmio_memory_base = EC_LPC_ADDR_MEMMAP;
4738d4a9c69SDustin L. Howett 
474e4dbf9d6SDustin L. Howett 	driver_data = platform_get_drvdata(pdev);
475e4dbf9d6SDustin L. Howett 	if (driver_data) {
476e4dbf9d6SDustin L. Howett 		quirks = driver_data->quirks;
477e4dbf9d6SDustin L. Howett 
478e4dbf9d6SDustin L. Howett 		if (quirks)
479e4dbf9d6SDustin L. Howett 			dev_info(dev, "loaded with quirks %8.08x\n", quirks);
480e4dbf9d6SDustin L. Howett 
481e4dbf9d6SDustin L. Howett 		if (quirks & CROS_EC_LPC_QUIRK_REMAP_MEMORY)
482e4dbf9d6SDustin L. Howett 			ec_lpc->mmio_memory_base = driver_data->quirk_mmio_memory_base;
483040159e0SBen Walsh 
484040159e0SBen Walsh 		if (quirks & CROS_EC_LPC_QUIRK_ACPI_ID) {
485040159e0SBen Walsh 			adev = cros_ec_lpc_get_device(driver_data->quirk_acpi_id);
486040159e0SBen Walsh 			if (!adev) {
487040159e0SBen Walsh 				dev_err(dev, "failed to get ACPI device '%s'",
488040159e0SBen Walsh 					driver_data->quirk_acpi_id);
489040159e0SBen Walsh 				return -ENODEV;
490040159e0SBen Walsh 			}
491040159e0SBen Walsh 			ACPI_COMPANION_SET(dev, adev);
492040159e0SBen Walsh 		}
49338c31b1dSBen Walsh 
49438c31b1dSBen Walsh 		if (quirks & CROS_EC_LPC_QUIRK_AML_MUTEX) {
49538c31b1dSBen Walsh 			const char *name
49638c31b1dSBen Walsh 				= driver_data->quirk_aml_mutex_name;
49738c31b1dSBen Walsh 			ret = cros_ec_lpc_mec_acpi_mutex(ACPI_COMPANION(dev), name);
49838c31b1dSBen Walsh 			if (ret) {
49938c31b1dSBen Walsh 				dev_err(dev, "failed to get AML mutex '%s'", name);
50038c31b1dSBen Walsh 				return ret;
50138c31b1dSBen Walsh 			}
50238c31b1dSBen Walsh 			dev_info(dev, "got AML mutex '%s'", name);
50338c31b1dSBen Walsh 		}
504e4dbf9d6SDustin L. Howett 	}
505e4dbf9d6SDustin L. Howett 
506c9bc1a0eSDustin L. Howett 	/*
507c9bc1a0eSDustin L. Howett 	 * The Framework Laptop (and possibly other non-ChromeOS devices)
508c9bc1a0eSDustin L. Howett 	 * only exposes the eight I/O ports that are required for the Microchip EC.
509c9bc1a0eSDustin L. Howett 	 * Requesting a larger reservation will fail.
510c9bc1a0eSDustin L. Howett 	 */
511c9bc1a0eSDustin L. Howett 	if (!devm_request_region(dev, EC_HOST_CMD_REGION0,
512c9bc1a0eSDustin L. Howett 				 EC_HOST_CMD_MEC_REGION_SIZE, dev_name(dev))) {
513c9bc1a0eSDustin L. Howett 		dev_err(dev, "couldn't reserve MEC region\n");
514ec2f33abSBill Richardson 		return -EBUSY;
515ec2f33abSBill Richardson 	}
516ec2f33abSBill Richardson 
517fdf84f9aSBrian Norris 	cros_ec_lpc_mec_init(EC_HOST_CMD_REGION0,
518fdf84f9aSBrian Norris 			     EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE);
519fdf84f9aSBrian Norris 
52022c040faSEnric Balletbo i Serra 	/*
52122c040faSEnric Balletbo i Serra 	 * Read the mapped ID twice, the first one is assuming the
52222c040faSEnric Balletbo i Serra 	 * EC is a Microchip Embedded Controller (MEC) variant, if the
52322c040faSEnric Balletbo i Serra 	 * protocol fails, fallback to the non MEC variant and try to
52422c040faSEnric Balletbo i Serra 	 * read again the ID.
52522c040faSEnric Balletbo i Serra 	 */
52622c040faSEnric Balletbo i Serra 	cros_ec_lpc_ops.read = cros_ec_lpc_mec_read_bytes;
52722c040faSEnric Balletbo i Serra 	cros_ec_lpc_ops.write = cros_ec_lpc_mec_write_bytes;
52868dbac0aSBen Walsh 	ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
52968dbac0aSBen Walsh 	if (ret < 0)
53068dbac0aSBen Walsh 		return ret;
53122c040faSEnric Balletbo i Serra 	if (buf[0] != 'E' || buf[1] != 'C') {
5328d4a9c69SDustin L. Howett 		if (!devm_request_region(dev, ec_lpc->mmio_memory_base, EC_MEMMAP_SIZE,
533c9bc1a0eSDustin L. Howett 					 dev_name(dev))) {
534c9bc1a0eSDustin L. Howett 			dev_err(dev, "couldn't reserve memmap region\n");
535c9bc1a0eSDustin L. Howett 			return -EBUSY;
536c9bc1a0eSDustin L. Howett 		}
537c9bc1a0eSDustin L. Howett 
53822c040faSEnric Balletbo i Serra 		/* Re-assign read/write operations for the non MEC variant */
53922c040faSEnric Balletbo i Serra 		cros_ec_lpc_ops.read = cros_ec_lpc_read_bytes;
54022c040faSEnric Balletbo i Serra 		cros_ec_lpc_ops.write = cros_ec_lpc_write_bytes;
54168dbac0aSBen Walsh 		ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + EC_MEMMAP_ID, 2,
54222c040faSEnric Balletbo i Serra 					   buf);
54368dbac0aSBen Walsh 		if (ret < 0)
54468dbac0aSBen Walsh 			return ret;
545bce70fefSShawn Nematbakhsh 		if (buf[0] != 'E' || buf[1] != 'C') {
546ec2f33abSBill Richardson 			dev_err(dev, "EC ID not detected\n");
547ec2f33abSBill Richardson 			return -ENODEV;
548ec2f33abSBill Richardson 		}
549ec2f33abSBill Richardson 
550c9bc1a0eSDustin L. Howett 		/* Reserve the remaining I/O ports required by the non-MEC protocol. */
551c9bc1a0eSDustin L. Howett 		if (!devm_request_region(dev, EC_HOST_CMD_REGION0 + EC_HOST_CMD_MEC_REGION_SIZE,
552c9bc1a0eSDustin L. Howett 					 EC_HOST_CMD_REGION_SIZE - EC_HOST_CMD_MEC_REGION_SIZE,
553c9bc1a0eSDustin L. Howett 					 dev_name(dev))) {
554c9bc1a0eSDustin L. Howett 			dev_err(dev, "couldn't reserve remainder of region0\n");
555ec2f33abSBill Richardson 			return -EBUSY;
556ec2f33abSBill Richardson 		}
557ec2f33abSBill Richardson 		if (!devm_request_region(dev, EC_HOST_CMD_REGION1,
558ec2f33abSBill Richardson 					 EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
559ec2f33abSBill Richardson 			dev_err(dev, "couldn't reserve region1\n");
560ec2f33abSBill Richardson 			return -EBUSY;
561ec2f33abSBill Richardson 		}
562c9bc1a0eSDustin L. Howett 	}
563ec2f33abSBill Richardson 
564ec2f33abSBill Richardson 	ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
565ec2f33abSBill Richardson 	if (!ec_dev)
566ec2f33abSBill Richardson 		return -ENOMEM;
567ec2f33abSBill Richardson 
568ec2f33abSBill Richardson 	platform_set_drvdata(pdev, ec_dev);
569ec2f33abSBill Richardson 	ec_dev->dev = dev;
570ec2f33abSBill Richardson 	ec_dev->phys_name = dev_name(dev);
571ec2f33abSBill Richardson 	ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc;
572d3654070SStephen Barber 	ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
573ec2f33abSBill Richardson 	ec_dev->cmd_readmem = cros_ec_lpc_readmem;
5742c7589afSStephen Barber 	ec_dev->din_size = sizeof(struct ec_host_response) +
5752c7589afSStephen Barber 			   sizeof(struct ec_response_get_protocol_info);
5762c7589afSStephen Barber 	ec_dev->dout_size = sizeof(struct ec_host_request);
5778d4a9c69SDustin L. Howett 	ec_dev->priv = ec_lpc;
578ec2f33abSBill Richardson 
579da1cf5a1SEnrico Granata 	/*
580da1cf5a1SEnrico Granata 	 * Some boards do not have an IRQ allotted for cros_ec_lpc,
581da1cf5a1SEnrico Granata 	 * which makes ENXIO an expected (and safe) scenario.
582da1cf5a1SEnrico Granata 	 */
583a69b4eebSEnric Balletbo i Serra 	irq = platform_get_irq_optional(pdev, 0);
584da1cf5a1SEnrico Granata 	if (irq > 0)
585da1cf5a1SEnrico Granata 		ec_dev->irq = irq;
586da1cf5a1SEnrico Granata 	else if (irq != -ENXIO) {
587da1cf5a1SEnrico Granata 		dev_err(dev, "couldn't retrieve IRQ number (%d)\n", irq);
588da1cf5a1SEnrico Granata 		return irq;
589da1cf5a1SEnrico Granata 	}
590da1cf5a1SEnrico Granata 
591ec2f33abSBill Richardson 	ret = cros_ec_register(ec_dev);
592ec2f33abSBill Richardson 	if (ret) {
593ec2f33abSBill Richardson 		dev_err(dev, "couldn't register ec_dev (%d)\n", ret);
594ec2f33abSBill Richardson 		return ret;
595ec2f33abSBill Richardson 	}
596ec2f33abSBill Richardson 
597a6df7798SGwendal Grignou 	/*
598a6df7798SGwendal Grignou 	 * Connect a notify handler to process MKBP messages if we have a
599a6df7798SGwendal Grignou 	 * companion ACPI device.
600a6df7798SGwendal Grignou 	 */
601a6df7798SGwendal Grignou 	adev = ACPI_COMPANION(dev);
602a6df7798SGwendal Grignou 	if (adev) {
603a6df7798SGwendal Grignou 		status = acpi_install_notify_handler(adev->handle,
604a6df7798SGwendal Grignou 						     ACPI_ALL_NOTIFY,
605a6df7798SGwendal Grignou 						     cros_ec_lpc_acpi_notify,
606a6df7798SGwendal Grignou 						     ec_dev);
607a6df7798SGwendal Grignou 		if (ACPI_FAILURE(status))
608a6df7798SGwendal Grignou 			dev_warn(dev, "Failed to register notifier %08x\n",
609a6df7798SGwendal Grignou 				 status);
610a6df7798SGwendal Grignou 	}
611a6df7798SGwendal Grignou 
612ec2f33abSBill Richardson 	return 0;
613ec2f33abSBill Richardson }
614ec2f33abSBill Richardson 
cros_ec_lpc_remove(struct platform_device * pdev)615e02944e8SUwe Kleine-König static void cros_ec_lpc_remove(struct platform_device *pdev)
616ec2f33abSBill Richardson {
6177aa703bbSEnric Balletbo i Serra 	struct cros_ec_device *ec_dev = platform_get_drvdata(pdev);
618a6df7798SGwendal Grignou 	struct acpi_device *adev;
619a6df7798SGwendal Grignou 
620a6df7798SGwendal Grignou 	adev = ACPI_COMPANION(&pdev->dev);
621a6df7798SGwendal Grignou 	if (adev)
622a6df7798SGwendal Grignou 		acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
623a6df7798SGwendal Grignou 					   cros_ec_lpc_acpi_notify);
624ec2f33abSBill Richardson 
625afb0a80eSUwe Kleine-König 	cros_ec_unregister(ec_dev);
626ec2f33abSBill Richardson }
627ec2f33abSBill Richardson 
62812278dc7SGwendal Grignou static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = {
62912278dc7SGwendal Grignou 	{ ACPI_DRV_NAME, 0 },
63012278dc7SGwendal Grignou 	{ }
63112278dc7SGwendal Grignou };
63212278dc7SGwendal Grignou MODULE_DEVICE_TABLE(acpi, cros_ec_lpc_acpi_device_ids);
63312278dc7SGwendal Grignou 
634*62be134aSDustin L. Howett static const struct lpc_driver_data framework_laptop_npcx_lpc_driver_data __initconst = {
635c8f460d9SDustin L. Howett 	.quirks = CROS_EC_LPC_QUIRK_REMAP_MEMORY,
636c8f460d9SDustin L. Howett 	.quirk_mmio_memory_base = 0xE00,
637c8f460d9SDustin L. Howett };
638c8f460d9SDustin L. Howett 
639*62be134aSDustin L. Howett static const struct lpc_driver_data framework_laptop_mec_lpc_driver_data __initconst = {
64004ca0a51SBen Walsh 	.quirks = CROS_EC_LPC_QUIRK_ACPI_ID|CROS_EC_LPC_QUIRK_AML_MUTEX,
64104ca0a51SBen Walsh 	.quirk_acpi_id = "PNP0C09",
64204ca0a51SBen Walsh 	.quirk_aml_mutex_name = "ECMT",
64304ca0a51SBen Walsh };
64404ca0a51SBen Walsh 
6456faadbbbSChristoph Hellwig static const struct dmi_system_id cros_ec_lpc_dmi_table[] __initconst = {
646ec2f33abSBill Richardson 	{
647ec2f33abSBill Richardson 		/*
648ec2f33abSBill Richardson 		 * Today all Chromebooks/boxes ship with Google_* as version and
649ec2f33abSBill Richardson 		 * coreboot as bios vendor. No other systems with this
650ec2f33abSBill Richardson 		 * combination are known to date.
651ec2f33abSBill Richardson 		 */
652ec2f33abSBill Richardson 		.matches = {
653ec2f33abSBill Richardson 			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
654ec2f33abSBill Richardson 			DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
655ec2f33abSBill Richardson 		},
656ec2f33abSBill Richardson 	},
657ec2f33abSBill Richardson 	{
658f56db262SSalvatore Bellizzi 		/*
659f56db262SSalvatore Bellizzi 		 * If the box is running custom coreboot firmware then the
660f56db262SSalvatore Bellizzi 		 * DMI BIOS version string will not be matched by "Google_",
661f56db262SSalvatore Bellizzi 		 * but the system vendor string will still be matched by
662f56db262SSalvatore Bellizzi 		 * "GOOGLE".
663f56db262SSalvatore Bellizzi 		 */
664f56db262SSalvatore Bellizzi 		.matches = {
665f56db262SSalvatore Bellizzi 			DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
666f56db262SSalvatore Bellizzi 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
667f56db262SSalvatore Bellizzi 		},
668f56db262SSalvatore Bellizzi 	},
669f56db262SSalvatore Bellizzi 	{
670ec2f33abSBill Richardson 		/* x86-link, the Chromebook Pixel. */
671ec2f33abSBill Richardson 		.matches = {
672ec2f33abSBill Richardson 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
673ec2f33abSBill Richardson 			DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
674ec2f33abSBill Richardson 		},
675ec2f33abSBill Richardson 	},
676ec2f33abSBill Richardson 	{
67785bba84eSJavier Martinez Canillas 		/* x86-samus, the Chromebook Pixel 2. */
67885bba84eSJavier Martinez Canillas 		.matches = {
67985bba84eSJavier Martinez Canillas 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
68085bba84eSJavier Martinez Canillas 			DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
68185bba84eSJavier Martinez Canillas 		},
68285bba84eSJavier Martinez Canillas 	},
68385bba84eSJavier Martinez Canillas 	{
684ec2f33abSBill Richardson 		/* x86-peppy, the Acer C720 Chromebook. */
685ec2f33abSBill Richardson 		.matches = {
686ec2f33abSBill Richardson 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
687ec2f33abSBill Richardson 			DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
688ec2f33abSBill Richardson 		},
689ec2f33abSBill Richardson 	},
690e6751917SThierry Escande 	{
691e6751917SThierry Escande 		/* x86-glimmer, the Lenovo Thinkpad Yoga 11e. */
692e6751917SThierry Escande 		.matches = {
693e6751917SThierry Escande 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
694e6751917SThierry Escande 			DMI_MATCH(DMI_PRODUCT_NAME, "Glimmer"),
695e6751917SThierry Escande 		},
696e6751917SThierry Escande 	},
6976a5d778eSDustin L. Howett 	/* A small number of non-Chromebook/box machines also use the ChromeOS EC */
6986a5d778eSDustin L. Howett 	{
699*62be134aSDustin L. Howett 		/* Framework Laptop (11th Gen Intel Core) */
700c8f460d9SDustin L. Howett 		.matches = {
701c8f460d9SDustin L. Howett 			DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
702*62be134aSDustin L. Howett 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Laptop"),
703c8f460d9SDustin L. Howett 		},
704*62be134aSDustin L. Howett 		.driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
705c8f460d9SDustin L. Howett 	},
706c8f460d9SDustin L. Howett 	{
707*62be134aSDustin L. Howett 		/* Framework Laptop (12th Gen Intel Core) */
7086a5d778eSDustin L. Howett 		.matches = {
7096a5d778eSDustin L. Howett 			DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
710*62be134aSDustin L. Howett 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "12th Gen Intel Core"),
7116a5d778eSDustin L. Howett 		},
712*62be134aSDustin L. Howett 		.driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
713*62be134aSDustin L. Howett 	},
714*62be134aSDustin L. Howett 	{
715*62be134aSDustin L. Howett 		/* Framework Laptop (13th Gen Intel Core) */
716*62be134aSDustin L. Howett 		.matches = {
717*62be134aSDustin L. Howett 			DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
718*62be134aSDustin L. Howett 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "13th Gen Intel Core"),
719*62be134aSDustin L. Howett 		},
720*62be134aSDustin L. Howett 		.driver_data = (void *)&framework_laptop_mec_lpc_driver_data,
721*62be134aSDustin L. Howett 	},
722*62be134aSDustin L. Howett 	{
723*62be134aSDustin L. Howett 		/*
724*62be134aSDustin L. Howett 		 * All remaining Framework Laptop models (13 AMD Ryzen, 16 AMD
725*62be134aSDustin L. Howett 		 * Ryzen, Intel Core Ultra)
726*62be134aSDustin L. Howett 		 */
727*62be134aSDustin L. Howett 		.matches = {
728*62be134aSDustin L. Howett 			DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
729*62be134aSDustin L. Howett 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Laptop"),
730*62be134aSDustin L. Howett 		},
731*62be134aSDustin L. Howett 		.driver_data = (void *)&framework_laptop_npcx_lpc_driver_data,
7326a5d778eSDustin L. Howett 	},
733ec2f33abSBill Richardson 	{ /* sentinel */ }
734ec2f33abSBill Richardson };
735ec2f33abSBill Richardson MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
736ec2f33abSBill Richardson 
737450de8f4SArchana Patni #ifdef CONFIG_PM_SLEEP
cros_ec_lpc_prepare(struct device * dev)7384b9abbc1STim Van Patten static int cros_ec_lpc_prepare(struct device *dev)
739450de8f4SArchana Patni {
740450de8f4SArchana Patni 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
74147ea0ddbSLalith Rajendran 	return cros_ec_suspend_prepare(ec_dev);
742450de8f4SArchana Patni }
743450de8f4SArchana Patni 
cros_ec_lpc_complete(struct device * dev)7444b9abbc1STim Van Patten static void cros_ec_lpc_complete(struct device *dev)
745450de8f4SArchana Patni {
746450de8f4SArchana Patni 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
74747ea0ddbSLalith Rajendran 	cros_ec_resume_complete(ec_dev);
74847ea0ddbSLalith Rajendran }
74947ea0ddbSLalith Rajendran 
cros_ec_lpc_suspend_late(struct device * dev)75047ea0ddbSLalith Rajendran static int cros_ec_lpc_suspend_late(struct device *dev)
75147ea0ddbSLalith Rajendran {
75247ea0ddbSLalith Rajendran 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
75347ea0ddbSLalith Rajendran 
75447ea0ddbSLalith Rajendran 	return cros_ec_suspend_late(ec_dev);
75547ea0ddbSLalith Rajendran }
75647ea0ddbSLalith Rajendran 
cros_ec_lpc_resume_early(struct device * dev)75747ea0ddbSLalith Rajendran static int cros_ec_lpc_resume_early(struct device *dev)
75847ea0ddbSLalith Rajendran {
75947ea0ddbSLalith Rajendran 	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
76047ea0ddbSLalith Rajendran 
76147ea0ddbSLalith Rajendran 	return cros_ec_resume_early(ec_dev);
762450de8f4SArchana Patni }
763450de8f4SArchana Patni #endif
764450de8f4SArchana Patni 
76581bc8c03SYueHaibing static const struct dev_pm_ops cros_ec_lpc_pm_ops = {
7664b9abbc1STim Van Patten #ifdef CONFIG_PM_SLEEP
7674b9abbc1STim Van Patten 	.prepare = cros_ec_lpc_prepare,
76847ea0ddbSLalith Rajendran 	.complete = cros_ec_lpc_complete,
7694b9abbc1STim Van Patten #endif
77047ea0ddbSLalith Rajendran 	SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_lpc_suspend_late, cros_ec_lpc_resume_early)
771450de8f4SArchana Patni };
772450de8f4SArchana Patni 
773ec2f33abSBill Richardson static struct platform_driver cros_ec_lpc_driver = {
774ec2f33abSBill Richardson 	.driver = {
775ec2f33abSBill Richardson 		.name = DRV_NAME,
77612278dc7SGwendal Grignou 		.acpi_match_table = cros_ec_lpc_acpi_device_ids,
777450de8f4SArchana Patni 		.pm = &cros_ec_lpc_pm_ops,
778ca821c1fSBrian Norris 		/*
779ca821c1fSBrian Norris 		 * ACPI child devices may probe before us, and they racily
780ca821c1fSBrian Norris 		 * check our drvdata pointer. Force synchronous probe until
781ca821c1fSBrian Norris 		 * those races are resolved.
782ca821c1fSBrian Norris 		 */
783ca821c1fSBrian Norris 		.probe_type = PROBE_FORCE_SYNCHRONOUS,
784ec2f33abSBill Richardson 	},
785ec2f33abSBill Richardson 	.probe = cros_ec_lpc_probe,
786e02944e8SUwe Kleine-König 	.remove_new = cros_ec_lpc_remove,
787ec2f33abSBill Richardson };
788ec2f33abSBill Richardson 
7895f454bdfSEnric Balletbo i Serra static struct platform_device cros_ec_lpc_device = {
7905f454bdfSEnric Balletbo i Serra 	.name = DRV_NAME
7915f454bdfSEnric Balletbo i Serra };
7925f454bdfSEnric Balletbo i Serra 
cros_ec_lpc_init(void)793ec2f33abSBill Richardson static int __init cros_ec_lpc_init(void)
794ec2f33abSBill Richardson {
795ec2f33abSBill Richardson 	int ret;
796c0e6ba2dSDustin L. Howett 	const struct dmi_system_id *dmi_match;
797ec2f33abSBill Richardson 
798040159e0SBen Walsh 	cros_ec_lpc_acpi_device_found = !!cros_ec_lpc_get_device(ACPI_DRV_NAME);
799b410b122SDmitry Torokhov 
800c0e6ba2dSDustin L. Howett 	dmi_match = dmi_first_match(cros_ec_lpc_dmi_table);
801c0e6ba2dSDustin L. Howett 
802c0e6ba2dSDustin L. Howett 	if (!cros_ec_lpc_acpi_device_found && !dmi_match) {
803ec2f33abSBill Richardson 		pr_err(DRV_NAME ": unsupported system.\n");
804ec2f33abSBill Richardson 		return -ENODEV;
805ec2f33abSBill Richardson 	}
806ec2f33abSBill Richardson 
807ec2f33abSBill Richardson 	/* Register the driver */
808ec2f33abSBill Richardson 	ret = platform_driver_register(&cros_ec_lpc_driver);
809ec2f33abSBill Richardson 	if (ret) {
810ec2f33abSBill Richardson 		pr_err(DRV_NAME ": can't register driver: %d\n", ret);
811ec2f33abSBill Richardson 		return ret;
812ec2f33abSBill Richardson 	}
813ec2f33abSBill Richardson 
8145f454bdfSEnric Balletbo i Serra 	if (!cros_ec_lpc_acpi_device_found) {
815c0e6ba2dSDustin L. Howett 		/* Pass the DMI match's driver data down to the platform device */
816c0e6ba2dSDustin L. Howett 		platform_set_drvdata(&cros_ec_lpc_device, dmi_match->driver_data);
817c0e6ba2dSDustin L. Howett 
8185f454bdfSEnric Balletbo i Serra 		/* Register the device, and it'll get hooked up automatically */
8195f454bdfSEnric Balletbo i Serra 		ret = platform_device_register(&cros_ec_lpc_device);
8205f454bdfSEnric Balletbo i Serra 		if (ret) {
8215f454bdfSEnric Balletbo i Serra 			pr_err(DRV_NAME ": can't register device: %d\n", ret);
8225f454bdfSEnric Balletbo i Serra 			platform_driver_unregister(&cros_ec_lpc_driver);
8235f454bdfSEnric Balletbo i Serra 		}
8245f454bdfSEnric Balletbo i Serra 	}
8255f454bdfSEnric Balletbo i Serra 
8265f454bdfSEnric Balletbo i Serra 	return ret;
827ec2f33abSBill Richardson }
828ec2f33abSBill Richardson 
cros_ec_lpc_exit(void)829ec2f33abSBill Richardson static void __exit cros_ec_lpc_exit(void)
830ec2f33abSBill Richardson {
8315f454bdfSEnric Balletbo i Serra 	if (!cros_ec_lpc_acpi_device_found)
8325f454bdfSEnric Balletbo i Serra 		platform_device_unregister(&cros_ec_lpc_device);
833ec2f33abSBill Richardson 	platform_driver_unregister(&cros_ec_lpc_driver);
834ec2f33abSBill Richardson }
835ec2f33abSBill Richardson 
836ec2f33abSBill Richardson module_init(cros_ec_lpc_init);
837ec2f33abSBill Richardson module_exit(cros_ec_lpc_exit);
838ec2f33abSBill Richardson 
839ec2f33abSBill Richardson MODULE_LICENSE("GPL");
840ec2f33abSBill Richardson MODULE_DESCRIPTION("ChromeOS EC LPC driver");
841