1840d9f13SEnric Balletbo i Serra /* SPDX-License-Identifier: GPL-2.0 */
2840d9f13SEnric Balletbo i Serra /*
3840d9f13SEnric Balletbo i Serra * ChromeOS Embedded Controller protocol interface.
4840d9f13SEnric Balletbo i Serra *
5840d9f13SEnric Balletbo i Serra * Copyright (C) 2012 Google, Inc
6840d9f13SEnric Balletbo i Serra */
7840d9f13SEnric Balletbo i Serra
8840d9f13SEnric Balletbo i Serra #ifndef __LINUX_CROS_EC_PROTO_H
9840d9f13SEnric Balletbo i Serra #define __LINUX_CROS_EC_PROTO_H
10840d9f13SEnric Balletbo i Serra
11840d9f13SEnric Balletbo i Serra #include <linux/device.h>
12961a325bSChen-Yu Tsai #include <linux/lockdep_types.h>
13840d9f13SEnric Balletbo i Serra #include <linux/mutex.h>
14840d9f13SEnric Balletbo i Serra #include <linux/notifier.h>
15840d9f13SEnric Balletbo i Serra
16840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h>
17840d9f13SEnric Balletbo i Serra
18840d9f13SEnric Balletbo i Serra #define CROS_EC_DEV_NAME "cros_ec"
19840d9f13SEnric Balletbo i Serra #define CROS_EC_DEV_FP_NAME "cros_fp"
20840d9f13SEnric Balletbo i Serra #define CROS_EC_DEV_ISH_NAME "cros_ish"
21840d9f13SEnric Balletbo i Serra #define CROS_EC_DEV_PD_NAME "cros_pd"
22840d9f13SEnric Balletbo i Serra #define CROS_EC_DEV_SCP_NAME "cros_scp"
23840d9f13SEnric Balletbo i Serra #define CROS_EC_DEV_TP_NAME "cros_tp"
24840d9f13SEnric Balletbo i Serra
253db0c9e5STzung-Bi Shih #define CROS_EC_DEV_EC_INDEX 0
263db0c9e5STzung-Bi Shih #define CROS_EC_DEV_PD_INDEX 1
273db0c9e5STzung-Bi Shih
28840d9f13SEnric Balletbo i Serra /*
29840d9f13SEnric Balletbo i Serra * The EC is unresponsive for a time after a reboot command. Add a
30840d9f13SEnric Balletbo i Serra * simple delay to make sure that the bus stays locked.
31840d9f13SEnric Balletbo i Serra */
32840d9f13SEnric Balletbo i Serra #define EC_REBOOT_DELAY_MS 50
33840d9f13SEnric Balletbo i Serra
34840d9f13SEnric Balletbo i Serra /*
35840d9f13SEnric Balletbo i Serra * Max bus-specific overhead incurred by request/responses.
36840d9f13SEnric Balletbo i Serra * I2C requires 1 additional byte for requests.
37840d9f13SEnric Balletbo i Serra * I2C requires 2 additional bytes for responses.
38840d9f13SEnric Balletbo i Serra * SPI requires up to 32 additional bytes for responses.
39840d9f13SEnric Balletbo i Serra */
40840d9f13SEnric Balletbo i Serra #define EC_PROTO_VERSION_UNKNOWN 0
41840d9f13SEnric Balletbo i Serra #define EC_MAX_REQUEST_OVERHEAD 1
42840d9f13SEnric Balletbo i Serra #define EC_MAX_RESPONSE_OVERHEAD 32
43840d9f13SEnric Balletbo i Serra
44840d9f13SEnric Balletbo i Serra /*
45*fb1e4934SRob Barnes * ACPI notify value for MKBP host event.
46*fb1e4934SRob Barnes */
47*fb1e4934SRob Barnes #define ACPI_NOTIFY_CROS_EC_MKBP 0x80
48*fb1e4934SRob Barnes
49*fb1e4934SRob Barnes /*
50d90fa2c6SRob Barnes * EC panic is not covered by the standard (0-F) ACPI notify values.
51d90fa2c6SRob Barnes * Arbitrarily choosing B0 to notify ec panic, which is in the 84-BF
52d90fa2c6SRob Barnes * device specific ACPI notify range.
53d90fa2c6SRob Barnes */
54d90fa2c6SRob Barnes #define ACPI_NOTIFY_CROS_EC_PANIC 0xB0
55d90fa2c6SRob Barnes
56d90fa2c6SRob Barnes /*
57840d9f13SEnric Balletbo i Serra * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
58840d9f13SEnric Balletbo i Serra */
59840d9f13SEnric Balletbo i Serra enum {
60840d9f13SEnric Balletbo i Serra EC_MSG_TX_HEADER_BYTES = 3,
61840d9f13SEnric Balletbo i Serra EC_MSG_TX_TRAILER_BYTES = 1,
62840d9f13SEnric Balletbo i Serra EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
63840d9f13SEnric Balletbo i Serra EC_MSG_TX_TRAILER_BYTES,
64840d9f13SEnric Balletbo i Serra EC_MSG_RX_PROTO_BYTES = 3,
65840d9f13SEnric Balletbo i Serra
66840d9f13SEnric Balletbo i Serra /* Max length of messages for proto 2*/
67840d9f13SEnric Balletbo i Serra EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
68840d9f13SEnric Balletbo i Serra EC_MSG_TX_PROTO_BYTES,
69840d9f13SEnric Balletbo i Serra
70840d9f13SEnric Balletbo i Serra EC_MAX_MSG_BYTES = 64 * 1024,
71840d9f13SEnric Balletbo i Serra };
72840d9f13SEnric Balletbo i Serra
73840d9f13SEnric Balletbo i Serra /**
74840d9f13SEnric Balletbo i Serra * struct cros_ec_command - Information about a ChromeOS EC command.
75840d9f13SEnric Balletbo i Serra * @version: Command version number (often 0).
76840d9f13SEnric Balletbo i Serra * @command: Command to send (EC_CMD_...).
77840d9f13SEnric Balletbo i Serra * @outsize: Outgoing length in bytes.
78840d9f13SEnric Balletbo i Serra * @insize: Max number of bytes to accept from the EC.
79840d9f13SEnric Balletbo i Serra * @result: EC's response to the command (separate from communication failure).
80840d9f13SEnric Balletbo i Serra * @data: Where to put the incoming data from EC and outgoing data to EC.
81840d9f13SEnric Balletbo i Serra */
82840d9f13SEnric Balletbo i Serra struct cros_ec_command {
83840d9f13SEnric Balletbo i Serra uint32_t version;
84840d9f13SEnric Balletbo i Serra uint32_t command;
85840d9f13SEnric Balletbo i Serra uint32_t outsize;
86840d9f13SEnric Balletbo i Serra uint32_t insize;
87840d9f13SEnric Balletbo i Serra uint32_t result;
8812008883SGustavo A. R. Silva uint8_t data[];
89840d9f13SEnric Balletbo i Serra };
90840d9f13SEnric Balletbo i Serra
91840d9f13SEnric Balletbo i Serra /**
92840d9f13SEnric Balletbo i Serra * struct cros_ec_device - Information about a ChromeOS EC device.
93840d9f13SEnric Balletbo i Serra * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
94840d9f13SEnric Balletbo i Serra * @dev: Device pointer for physical comms device
95840d9f13SEnric Balletbo i Serra * @cros_class: The class structure for this device.
96840d9f13SEnric Balletbo i Serra * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
97840d9f13SEnric Balletbo i Serra * @offset: Is within EC_LPC_ADDR_MEMMAP region.
98840d9f13SEnric Balletbo i Serra * @bytes: Number of bytes to read. zero means "read a string" (including
99840d9f13SEnric Balletbo i Serra * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
100840d9f13SEnric Balletbo i Serra * read. Caller must ensure that the buffer is large enough for the
101840d9f13SEnric Balletbo i Serra * result when reading a string.
102840d9f13SEnric Balletbo i Serra * @max_request: Max size of message requested.
103840d9f13SEnric Balletbo i Serra * @max_response: Max size of message response.
104840d9f13SEnric Balletbo i Serra * @max_passthru: Max sice of passthru message.
105840d9f13SEnric Balletbo i Serra * @proto_version: The protocol version used for this device.
106840d9f13SEnric Balletbo i Serra * @priv: Private data.
107840d9f13SEnric Balletbo i Serra * @irq: Interrupt to use.
108840d9f13SEnric Balletbo i Serra * @id: Device id.
109840d9f13SEnric Balletbo i Serra * @din: Input buffer (for data from EC). This buffer will always be
110840d9f13SEnric Balletbo i Serra * dword-aligned and include enough space for up to 7 word-alignment
111840d9f13SEnric Balletbo i Serra * bytes also, so we can ensure that the body of the message is always
112840d9f13SEnric Balletbo i Serra * dword-aligned (64-bit). We use this alignment to keep ARM and x86
113840d9f13SEnric Balletbo i Serra * happy. Probably word alignment would be OK, there might be a small
114840d9f13SEnric Balletbo i Serra * performance advantage to using dword.
115840d9f13SEnric Balletbo i Serra * @dout: Output buffer (for data to EC). This buffer will always be
116840d9f13SEnric Balletbo i Serra * dword-aligned and include enough space for up to 7 word-alignment
117840d9f13SEnric Balletbo i Serra * bytes also, so we can ensure that the body of the message is always
118840d9f13SEnric Balletbo i Serra * dword-aligned (64-bit). We use this alignment to keep ARM and x86
119840d9f13SEnric Balletbo i Serra * happy. Probably word alignment would be OK, there might be a small
120840d9f13SEnric Balletbo i Serra * performance advantage to using dword.
121840d9f13SEnric Balletbo i Serra * @din_size: Size of din buffer to allocate (zero to use static din).
122840d9f13SEnric Balletbo i Serra * @dout_size: Size of dout buffer to allocate (zero to use static dout).
123840d9f13SEnric Balletbo i Serra * @wake_enabled: True if this device can wake the system from sleep.
124840d9f13SEnric Balletbo i Serra * @suspended: True if this device had been suspended.
125840d9f13SEnric Balletbo i Serra * @cmd_xfer: Send command to EC and get response.
126840d9f13SEnric Balletbo i Serra * Returns the number of bytes received if the communication
127840d9f13SEnric Balletbo i Serra * succeeded, but that doesn't mean the EC was happy with the
128840d9f13SEnric Balletbo i Serra * command. The caller should check msg.result for the EC's result
129840d9f13SEnric Balletbo i Serra * code.
130840d9f13SEnric Balletbo i Serra * @pkt_xfer: Send packet to EC and get response.
131961a325bSChen-Yu Tsai * @lockdep_key: Lockdep class for each instance. Unused if CONFIG_LOCKDEP is
132961a325bSChen-Yu Tsai * not enabled.
133840d9f13SEnric Balletbo i Serra * @lock: One transaction at a time.
1343300fdd6SEnrico Granata * @mkbp_event_supported: 0 if MKBP not supported. Otherwise its value is
1353300fdd6SEnrico Granata * the maximum supported version of the MKBP host event
1363300fdd6SEnrico Granata * command + 1.
137840d9f13SEnric Balletbo i Serra * @host_sleep_v1: True if this EC supports the sleep v1 command.
138840d9f13SEnric Balletbo i Serra * @event_notifier: Interrupt event notifier for transport devices.
139840d9f13SEnric Balletbo i Serra * @event_data: Raw payload transferred with the MKBP event.
140840d9f13SEnric Balletbo i Serra * @event_size: Size in bytes of the event data.
141840d9f13SEnric Balletbo i Serra * @host_event_wake_mask: Mask of host events that cause wake from suspend.
14220eb556dSTzung-Bi Shih * @suspend_timeout_ms: The timeout in milliseconds between when sleep event
14320eb556dSTzung-Bi Shih * is received and when the EC will declare sleep
14420eb556dSTzung-Bi Shih * transition failure if the sleep signal is not
14520eb556dSTzung-Bi Shih * asserted. See also struct
14620eb556dSTzung-Bi Shih * ec_params_host_sleep_event_v1 in cros_ec_commands.h.
147212c9b9cSTzung-Bi Shih * @last_resume_result: The number of sleep power signal transitions that
148212c9b9cSTzung-Bi Shih * occurred since the suspend message. The high bit
149212c9b9cSTzung-Bi Shih * indicates a timeout occurred. See also struct
150212c9b9cSTzung-Bi Shih * ec_response_host_sleep_event_v1 in cros_ec_commands.h.
15105a3c420SGwendal Grignou * @last_event_time: exact time from the hard irq when we got notified of
15205a3c420SGwendal Grignou * a new event.
15342cd0ab4SYicheng Li * @notifier_ready: The notifier_block to let the kernel re-query EC
15442cd0ab4SYicheng Li * communication protocol when the EC sends
15542cd0ab4SYicheng Li * EC_HOST_EVENT_INTERFACE_READY.
156840d9f13SEnric Balletbo i Serra * @ec: The platform_device used by the mfd driver to interface with the
157840d9f13SEnric Balletbo i Serra * main EC.
158840d9f13SEnric Balletbo i Serra * @pd: The platform_device used by the mfd driver to interface with the
159840d9f13SEnric Balletbo i Serra * PD behind an EC.
16016d73129STzung-Bi Shih * @panic_notifier: EC panic notifier.
161840d9f13SEnric Balletbo i Serra */
162840d9f13SEnric Balletbo i Serra struct cros_ec_device {
163840d9f13SEnric Balletbo i Serra /* These are used by other drivers that want to talk to the EC */
164840d9f13SEnric Balletbo i Serra const char *phys_name;
165840d9f13SEnric Balletbo i Serra struct device *dev;
166840d9f13SEnric Balletbo i Serra struct class *cros_class;
167840d9f13SEnric Balletbo i Serra int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
168840d9f13SEnric Balletbo i Serra unsigned int bytes, void *dest);
169840d9f13SEnric Balletbo i Serra
170840d9f13SEnric Balletbo i Serra /* These are used to implement the platform-specific interface */
171840d9f13SEnric Balletbo i Serra u16 max_request;
172840d9f13SEnric Balletbo i Serra u16 max_response;
173840d9f13SEnric Balletbo i Serra u16 max_passthru;
174840d9f13SEnric Balletbo i Serra u16 proto_version;
175840d9f13SEnric Balletbo i Serra void *priv;
176840d9f13SEnric Balletbo i Serra int irq;
177840d9f13SEnric Balletbo i Serra u8 *din;
178840d9f13SEnric Balletbo i Serra u8 *dout;
179840d9f13SEnric Balletbo i Serra int din_size;
180840d9f13SEnric Balletbo i Serra int dout_size;
181840d9f13SEnric Balletbo i Serra bool wake_enabled;
182840d9f13SEnric Balletbo i Serra bool suspended;
183840d9f13SEnric Balletbo i Serra int (*cmd_xfer)(struct cros_ec_device *ec,
184840d9f13SEnric Balletbo i Serra struct cros_ec_command *msg);
185840d9f13SEnric Balletbo i Serra int (*pkt_xfer)(struct cros_ec_device *ec,
186840d9f13SEnric Balletbo i Serra struct cros_ec_command *msg);
187961a325bSChen-Yu Tsai struct lock_class_key lockdep_key;
188840d9f13SEnric Balletbo i Serra struct mutex lock;
1893300fdd6SEnrico Granata u8 mkbp_event_supported;
190840d9f13SEnric Balletbo i Serra bool host_sleep_v1;
191840d9f13SEnric Balletbo i Serra struct blocking_notifier_head event_notifier;
192840d9f13SEnric Balletbo i Serra
193106d6739SDaisuke Nojiri struct ec_response_get_next_event_v3 event_data;
194840d9f13SEnric Balletbo i Serra int event_size;
195840d9f13SEnric Balletbo i Serra u32 host_event_wake_mask;
196840d9f13SEnric Balletbo i Serra u32 last_resume_result;
197e8bf17d5SEvan Green u16 suspend_timeout_ms;
19805a3c420SGwendal Grignou ktime_t last_event_time;
19942cd0ab4SYicheng Li struct notifier_block notifier_ready;
200840d9f13SEnric Balletbo i Serra
201840d9f13SEnric Balletbo i Serra /* The platform devices used by the mfd driver */
202840d9f13SEnric Balletbo i Serra struct platform_device *ec;
203840d9f13SEnric Balletbo i Serra struct platform_device *pd;
204d90fa2c6SRob Barnes
205d90fa2c6SRob Barnes struct blocking_notifier_head panic_notifier;
206840d9f13SEnric Balletbo i Serra };
207840d9f13SEnric Balletbo i Serra
208840d9f13SEnric Balletbo i Serra /**
209840d9f13SEnric Balletbo i Serra * struct cros_ec_platform - ChromeOS EC platform information.
210840d9f13SEnric Balletbo i Serra * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
211840d9f13SEnric Balletbo i Serra * used in /dev/ and sysfs.
212840d9f13SEnric Balletbo i Serra * @cmd_offset: Offset to apply for each command. Set when
213840d9f13SEnric Balletbo i Serra * registering a device behind another one.
214840d9f13SEnric Balletbo i Serra */
215840d9f13SEnric Balletbo i Serra struct cros_ec_platform {
216840d9f13SEnric Balletbo i Serra const char *ec_name;
217840d9f13SEnric Balletbo i Serra u16 cmd_offset;
218840d9f13SEnric Balletbo i Serra };
219840d9f13SEnric Balletbo i Serra
22011f1eabeSEnric Balletbo i Serra /**
22111f1eabeSEnric Balletbo i Serra * struct cros_ec_dev - ChromeOS EC device entry point.
22211f1eabeSEnric Balletbo i Serra * @class_dev: Device structure used in sysfs.
22311f1eabeSEnric Balletbo i Serra * @ec_dev: cros_ec_device structure to talk to the physical device.
22411f1eabeSEnric Balletbo i Serra * @dev: Pointer to the platform device.
22511f1eabeSEnric Balletbo i Serra * @debug_info: cros_ec_debugfs structure for debugging information.
22611f1eabeSEnric Balletbo i Serra * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
22711f1eabeSEnric Balletbo i Serra * @cmd_offset: Offset to apply for each command.
22811f1eabeSEnric Balletbo i Serra * @features: Features supported by the EC.
22911f1eabeSEnric Balletbo i Serra */
23011f1eabeSEnric Balletbo i Serra struct cros_ec_dev {
23111f1eabeSEnric Balletbo i Serra struct device class_dev;
23211f1eabeSEnric Balletbo i Serra struct cros_ec_device *ec_dev;
23311f1eabeSEnric Balletbo i Serra struct device *dev;
23411f1eabeSEnric Balletbo i Serra struct cros_ec_debugfs *debug_info;
23511f1eabeSEnric Balletbo i Serra bool has_kb_wake_angle;
23611f1eabeSEnric Balletbo i Serra u16 cmd_offset;
2377ff22787SPrashant Malani struct ec_response_get_features features;
23811f1eabeSEnric Balletbo i Serra };
23911f1eabeSEnric Balletbo i Serra
24011f1eabeSEnric Balletbo i Serra #define to_cros_ec_dev(dev) container_of(dev, struct cros_ec_dev, class_dev)
24111f1eabeSEnric Balletbo i Serra
242840d9f13SEnric Balletbo i Serra int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
243840d9f13SEnric Balletbo i Serra struct cros_ec_command *msg);
244840d9f13SEnric Balletbo i Serra
245840d9f13SEnric Balletbo i Serra int cros_ec_check_result(struct cros_ec_device *ec_dev,
246840d9f13SEnric Balletbo i Serra struct cros_ec_command *msg);
247840d9f13SEnric Balletbo i Serra
24857b888caSGuenter Roeck int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
24957b888caSGuenter Roeck struct cros_ec_command *msg);
25057b888caSGuenter Roeck
251840d9f13SEnric Balletbo i Serra int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
252840d9f13SEnric Balletbo i Serra struct cros_ec_command *msg);
253840d9f13SEnric Balletbo i Serra
2545ffa0dbfSDawid Niedzwiecki int cros_ec_rwsig_continue(struct cros_ec_device *ec_dev);
2555ffa0dbfSDawid Niedzwiecki
256840d9f13SEnric Balletbo i Serra int cros_ec_query_all(struct cros_ec_device *ec_dev);
257840d9f13SEnric Balletbo i Serra
2583300fdd6SEnrico Granata int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
2593300fdd6SEnrico Granata bool *wake_event,
2603300fdd6SEnrico Granata bool *has_more_events);
261840d9f13SEnric Balletbo i Serra
262840d9f13SEnric Balletbo i Serra u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
263840d9f13SEnric Balletbo i Serra
264d50497c4SPrashant Malani bool cros_ec_check_features(struct cros_ec_dev *ec, int feature);
265a16b2e28SGwendal Grignou
266a16b2e28SGwendal Grignou int cros_ec_get_sensor_count(struct cros_ec_dev *ec);
267a16b2e28SGwendal Grignou
2682f3dd39eSStephen Boyd int cros_ec_cmd(struct cros_ec_device *ec_dev, unsigned int version, int command, const void *outdata,
269f87e15fbSPrashant Malani size_t outsize, void *indata, size_t insize);
2707101c839SPrashant Malani
271a14a569aSThomas Weißschuh int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, void *dest);
272a14a569aSThomas Weißschuh
27369a13742SThomas Weißschuh int cros_ec_get_cmd_versions(struct cros_ec_device *ec_dev, u16 cmd);
27469a13742SThomas Weißschuh
27505a3c420SGwendal Grignou /**
27605a3c420SGwendal Grignou * cros_ec_get_time_ns() - Return time in ns.
27705a3c420SGwendal Grignou *
27805a3c420SGwendal Grignou * This is the function used to record the time for last_event_time in struct
27905a3c420SGwendal Grignou * cros_ec_device during the hard irq.
28005a3c420SGwendal Grignou *
28105a3c420SGwendal Grignou * Return: ktime_t format since boot.
28205a3c420SGwendal Grignou */
cros_ec_get_time_ns(void)28305a3c420SGwendal Grignou static inline ktime_t cros_ec_get_time_ns(void)
28405a3c420SGwendal Grignou {
28505a3c420SGwendal Grignou return ktime_get_boottime_ns();
28605a3c420SGwendal Grignou }
28705a3c420SGwendal Grignou
288840d9f13SEnric Balletbo i Serra #endif /* __LINUX_CROS_EC_PROTO_H */
289