Linux-libre 5.4.48-gnu
[librecmc/linux-libre.git] / include / linux / platform_data / wilco-ec.h
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
17 /* Normal commands have a maximum 32 bytes of data */
18 #define EC_MAILBOX_DATA_SIZE            32
19
20 /**
21  * struct wilco_ec_device - Wilco Embedded Controller handle.
22  * @dev: Device handle.
23  * @mailbox_lock: Mutex to ensure one mailbox command at a time.
24  * @io_command: I/O port for mailbox command.  Provided by ACPI.
25  * @io_data: I/O port for mailbox data.  Provided by ACPI.
26  * @io_packet: I/O port for mailbox packet data.  Provided by ACPI.
27  * @data_buffer: Buffer used for EC communication.  The same buffer
28  *               is used to hold the request and the response.
29  * @data_size: Size of the data buffer used for EC communication.
30  * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
31  * @rtc_pdev: The child platform_device used by the RTC sub-driver.
32  * @telem_pdev: The child platform_device used by the telemetry sub-driver.
33  */
34 struct wilco_ec_device {
35         struct device *dev;
36         struct mutex mailbox_lock;
37         struct resource *io_command;
38         struct resource *io_data;
39         struct resource *io_packet;
40         void *data_buffer;
41         size_t data_size;
42         struct platform_device *debugfs_pdev;
43         struct platform_device *rtc_pdev;
44         struct platform_device *telem_pdev;
45 };
46
47 /**
48  * struct wilco_ec_request - Mailbox request message format.
49  * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
50  * @checksum: Sum of all bytes must be 0.
51  * @mailbox_id: Mailbox identifier, specifies the command set.
52  * @mailbox_version: Mailbox interface version %EC_MAILBOX_VERSION
53  * @reserved: Set to zero.
54  * @data_size: Length of following data.
55  */
56 struct wilco_ec_request {
57         u8 struct_version;
58         u8 checksum;
59         u16 mailbox_id;
60         u8 mailbox_version;
61         u8 reserved;
62         u16 data_size;
63 } __packed;
64
65 /**
66  * struct wilco_ec_response - Mailbox response message format.
67  * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
68  * @checksum: Sum of all bytes must be 0.
69  * @result: Result code from the EC.  Non-zero indicates an error.
70  * @data_size: Length of the response data buffer.
71  * @reserved: Set to zero.
72  * @data: Response data buffer.  Max size is %EC_MAILBOX_DATA_SIZE_EXTENDED.
73  */
74 struct wilco_ec_response {
75         u8 struct_version;
76         u8 checksum;
77         u16 result;
78         u16 data_size;
79         u8 reserved[2];
80         u8 data[0];
81 } __packed;
82
83 /**
84  * enum wilco_ec_msg_type - Message type to select a set of command codes.
85  * @WILCO_EC_MSG_LEGACY: Legacy EC messages for standard EC behavior.
86  * @WILCO_EC_MSG_PROPERTY: Get/Set/Sync EC controlled NVRAM property.
87  * @WILCO_EC_MSG_TELEMETRY: Request telemetry data from the EC.
88  */
89 enum wilco_ec_msg_type {
90         WILCO_EC_MSG_LEGACY = 0x00f0,
91         WILCO_EC_MSG_PROPERTY = 0x00f2,
92         WILCO_EC_MSG_TELEMETRY = 0x00f5,
93 };
94
95 /**
96  * struct wilco_ec_message - Request and response message.
97  * @type: Mailbox message type.
98  * @flags: Message flags, e.g. %WILCO_EC_FLAG_NO_RESPONSE.
99  * @request_size: Number of bytes to send to the EC.
100  * @request_data: Buffer containing the request data.
101  * @response_size: Number of bytes to read from EC.
102  * @response_data: Buffer containing the response data, should be
103  *                 response_size bytes and allocated by caller.
104  */
105 struct wilco_ec_message {
106         enum wilco_ec_msg_type type;
107         u8 flags;
108         size_t request_size;
109         void *request_data;
110         size_t response_size;
111         void *response_data;
112 };
113
114 /**
115  * wilco_ec_mailbox() - Send request to the EC and receive the response.
116  * @ec: Wilco EC device.
117  * @msg: Wilco EC message.
118  *
119  * Return: Number of bytes received or negative error code on failure.
120  */
121 int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg);
122
123 /*
124  * A Property is typically a data item that is stored to NVRAM
125  * by the EC. Each of these data items has an index associated
126  * with it, known as the Property ID (PID). Properties may have
127  * variable lengths, up to a max of WILCO_EC_PROPERTY_MAX_SIZE
128  * bytes. Properties can be simple integers, or they may be more
129  * complex binary data.
130  */
131
132 #define WILCO_EC_PROPERTY_MAX_SIZE      4
133
134 /**
135  * struct ec_property_set_msg - Message to get or set a property.
136  * @property_id: Which property to get or set.
137  * @length: Number of bytes of |data| that are used.
138  * @data: Actual property data.
139  */
140 struct wilco_ec_property_msg {
141         u32 property_id;
142         int length;
143         u8 data[WILCO_EC_PROPERTY_MAX_SIZE];
144 };
145
146 /**
147  * wilco_ec_get_property() - Retrieve a property from the EC.
148  * @ec: Embedded Controller device.
149  * @prop_msg: Message for request and response.
150  *
151  * The property_id field of |prop_msg| should be filled before calling this
152  * function. The result will be stored in the data and length fields.
153  *
154  * Return: 0 on success, negative error code on failure.
155  */
156 int wilco_ec_get_property(struct wilco_ec_device *ec,
157                           struct wilco_ec_property_msg *prop_msg);
158
159 /**
160  * wilco_ec_set_property() - Store a property on the EC.
161  * @ec: Embedded Controller device.
162  * @prop_msg: Message for request and response.
163  *
164  * The property_id, length, and data fields of |prop_msg| should be
165  * filled before calling this function.
166  *
167  * Return: 0 on success, negative error code on failure.
168  */
169 int wilco_ec_set_property(struct wilco_ec_device *ec,
170                           struct wilco_ec_property_msg *prop_msg);
171
172 /**
173  * wilco_ec_get_byte_property() - Retrieve a byte-size property from the EC.
174  * @ec: Embedded Controller device.
175  * @property_id: Which property to retrieve.
176  * @val: The result value, will be filled by this function.
177  *
178  * Return: 0 on success, negative error code on failure.
179  */
180 int wilco_ec_get_byte_property(struct wilco_ec_device *ec, u32 property_id,
181                                u8 *val);
182
183 /**
184  * wilco_ec_get_byte_property() - Store a byte-size property on the EC.
185  * @ec: Embedded Controller device.
186  * @property_id: Which property to store.
187  * @val: Value to store.
188  *
189  * Return: 0 on success, negative error code on failure.
190  */
191 int wilco_ec_set_byte_property(struct wilco_ec_device *ec, u32 property_id,
192                                u8 val);
193
194 /**
195  * wilco_ec_add_sysfs() - Create sysfs entries
196  * @ec: Wilco EC device
197  *
198  * wilco_ec_remove_sysfs() needs to be called afterwards
199  * to perform the necessary cleanup.
200  *
201  * Return: 0 on success or negative error code on failure.
202  */
203 int wilco_ec_add_sysfs(struct wilco_ec_device *ec);
204 void wilco_ec_remove_sysfs(struct wilco_ec_device *ec);
205
206 #endif /* WILCO_EC_H */