1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * ChromeOS Wilco Embedded Controller
4  *
5  * Copyright 2018 Google LLC
6  */
7 
8 #ifndef WILCO_EC_H
9 #define WILCO_EC_H
10 
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 
14 /* Message flags for using the mailbox() interface */
15 #define WILCO_EC_FLAG_NO_RESPONSE	BIT(0) /* EC does not respond */
16 #define WILCO_EC_FLAG_EXTENDED_DATA	BIT(1) /* EC returns 256 data bytes */
17 
18 /* Normal commands have a maximum 32 bytes of data */
19 #define EC_MAILBOX_DATA_SIZE		32
20 /* Extended commands have 256 bytes of response data */
21 #define EC_MAILBOX_DATA_SIZE_EXTENDED	256
22 
23 /**
24  * struct wilco_ec_device - Wilco Embedded Controller handle.
25  * @dev: Device handle.
26  * @mailbox_lock: Mutex to ensure one mailbox command at a time.
27  * @io_command: I/O port for mailbox command.  Provided by ACPI.
28  * @io_data: I/O port for mailbox data.  Provided by ACPI.
29  * @io_packet: I/O port for mailbox packet data.  Provided by ACPI.
30  * @data_buffer: Buffer used for EC communication.  The same buffer
31  *               is used to hold the request and the response.
32  * @data_size: Size of the data buffer used for EC communication.
33  * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
34  * @rtc_pdev: The child platform_device used by the RTC sub-driver.
35  */
36 struct wilco_ec_device {
37 	struct device *dev;
38 	struct mutex mailbox_lock;
39 	struct resource *io_command;
40 	struct resource *io_data;
41 	struct resource *io_packet;
42 	void *data_buffer;
43 	size_t data_size;
44 	struct platform_device *debugfs_pdev;
45 	struct platform_device *rtc_pdev;
46 };
47 
48 /**
49  * struct wilco_ec_request - Mailbox request message format.
50  * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
51  * @checksum: Sum of all bytes must be 0.
52  * @mailbox_id: Mailbox identifier, specifies the command set.
53  * @mailbox_version: Mailbox interface version %EC_MAILBOX_VERSION
54  * @reserved: Set to zero.
55  * @data_size: Length of following data.
56  */
57 struct wilco_ec_request {
58 	u8 struct_version;
59 	u8 checksum;
60 	u16 mailbox_id;
61 	u8 mailbox_version;
62 	u8 reserved;
63 	u16 data_size;
64 } __packed;
65 
66 /**
67  * struct wilco_ec_response - Mailbox response message format.
68  * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
69  * @checksum: Sum of all bytes must be 0.
70  * @result: Result code from the EC.  Non-zero indicates an error.
71  * @data_size: Length of the response data buffer.
72  * @reserved: Set to zero.
73  * @data: Response data buffer.  Max size is %EC_MAILBOX_DATA_SIZE_EXTENDED.
74  */
75 struct wilco_ec_response {
76 	u8 struct_version;
77 	u8 checksum;
78 	u16 result;
79 	u16 data_size;
80 	u8 reserved[2];
81 	u8 data[0];
82 } __packed;
83 
84 /**
85  * enum wilco_ec_msg_type - Message type to select a set of command codes.
86  * @WILCO_EC_MSG_LEGACY: Legacy EC messages for standard EC behavior.
87  * @WILCO_EC_MSG_PROPERTY: Get/Set/Sync EC controlled NVRAM property.
88  * @WILCO_EC_MSG_TELEMETRY_SHORT: 32 bytes of telemetry data provided by the EC.
89  * @WILCO_EC_MSG_TELEMETRY_LONG: 256 bytes of telemetry data provided by the EC.
90  */
91 enum wilco_ec_msg_type {
92 	WILCO_EC_MSG_LEGACY = 0x00f0,
93 	WILCO_EC_MSG_PROPERTY = 0x00f2,
94 	WILCO_EC_MSG_TELEMETRY_SHORT = 0x00f5,
95 	WILCO_EC_MSG_TELEMETRY_LONG = 0x00f6,
96 };
97 
98 /**
99  * struct wilco_ec_message - Request and response message.
100  * @type: Mailbox message type.
101  * @flags: Message flags, e.g. %WILCO_EC_FLAG_NO_RESPONSE.
102  * @request_size: Number of bytes to send to the EC.
103  * @request_data: Buffer containing the request data.
104  * @response_size: Number of bytes to read from EC.
105  * @response_data: Buffer containing the response data, should be
106  *                 response_size bytes and allocated by caller.
107  */
108 struct wilco_ec_message {
109 	enum wilco_ec_msg_type type;
110 	u8 flags;
111 	size_t request_size;
112 	void *request_data;
113 	size_t response_size;
114 	void *response_data;
115 };
116 
117 /**
118  * wilco_ec_mailbox() - Send request to the EC and receive the response.
119  * @ec: Wilco EC device.
120  * @msg: Wilco EC message.
121  *
122  * Return: Number of bytes received or negative error code on failure.
123  */
124 int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg);
125 
126 #endif /* WILCO_EC_H */
127