Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * ChromeOS Embedded Controller protocol interface.
0004  *
0005  * Copyright (C) 2012 Google, Inc
0006  */
0007 
0008 #ifndef __LINUX_CROS_EC_PROTO_H
0009 #define __LINUX_CROS_EC_PROTO_H
0010 
0011 #include <linux/device.h>
0012 #include <linux/mutex.h>
0013 #include <linux/notifier.h>
0014 
0015 #include <linux/platform_data/cros_ec_commands.h>
0016 
0017 #define CROS_EC_DEV_NAME    "cros_ec"
0018 #define CROS_EC_DEV_FP_NAME "cros_fp"
0019 #define CROS_EC_DEV_ISH_NAME    "cros_ish"
0020 #define CROS_EC_DEV_PD_NAME "cros_pd"
0021 #define CROS_EC_DEV_SCP_NAME    "cros_scp"
0022 #define CROS_EC_DEV_SCP_C1_NAME "cros_scp_c1"
0023 #define CROS_EC_DEV_TP_NAME "cros_tp"
0024 
0025 #define CROS_EC_DEV_EC_INDEX 0
0026 #define CROS_EC_DEV_PD_INDEX 1
0027 
0028 /*
0029  * The EC is unresponsive for a time after a reboot command.  Add a
0030  * simple delay to make sure that the bus stays locked.
0031  */
0032 #define EC_REBOOT_DELAY_MS      50
0033 
0034 /*
0035  * Max bus-specific overhead incurred by request/responses.
0036  * I2C requires 1 additional byte for requests.
0037  * I2C requires 2 additional bytes for responses.
0038  * SPI requires up to 32 additional bytes for responses.
0039  */
0040 #define EC_PROTO_VERSION_UNKNOWN    0
0041 #define EC_MAX_REQUEST_OVERHEAD     1
0042 #define EC_MAX_RESPONSE_OVERHEAD    32
0043 
0044 /*
0045  * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
0046  */
0047 enum {
0048     EC_MSG_TX_HEADER_BYTES  = 3,
0049     EC_MSG_TX_TRAILER_BYTES = 1,
0050     EC_MSG_TX_PROTO_BYTES   = EC_MSG_TX_HEADER_BYTES +
0051                   EC_MSG_TX_TRAILER_BYTES,
0052     EC_MSG_RX_PROTO_BYTES   = 3,
0053 
0054     /* Max length of messages for proto 2*/
0055     EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
0056                   EC_MSG_TX_PROTO_BYTES,
0057 
0058     EC_MAX_MSG_BYTES    = 64 * 1024,
0059 };
0060 
0061 /**
0062  * struct cros_ec_command - Information about a ChromeOS EC command.
0063  * @version: Command version number (often 0).
0064  * @command: Command to send (EC_CMD_...).
0065  * @outsize: Outgoing length in bytes.
0066  * @insize: Max number of bytes to accept from the EC.
0067  * @result: EC's response to the command (separate from communication failure).
0068  * @data: Where to put the incoming data from EC and outgoing data to EC.
0069  */
0070 struct cros_ec_command {
0071     uint32_t version;
0072     uint32_t command;
0073     uint32_t outsize;
0074     uint32_t insize;
0075     uint32_t result;
0076     uint8_t data[];
0077 };
0078 
0079 /**
0080  * struct cros_ec_device - Information about a ChromeOS EC device.
0081  * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
0082  * @dev: Device pointer for physical comms device
0083  * @cros_class: The class structure for this device.
0084  * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
0085  *     @offset: Is within EC_LPC_ADDR_MEMMAP region.
0086  *     @bytes: Number of bytes to read. zero means "read a string" (including
0087  *             the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
0088  *             read. Caller must ensure that the buffer is large enough for the
0089  *             result when reading a string.
0090  * @max_request: Max size of message requested.
0091  * @max_response: Max size of message response.
0092  * @max_passthru: Max sice of passthru message.
0093  * @proto_version: The protocol version used for this device.
0094  * @priv: Private data.
0095  * @irq: Interrupt to use.
0096  * @id: Device id.
0097  * @din: Input buffer (for data from EC). This buffer will always be
0098  *       dword-aligned and include enough space for up to 7 word-alignment
0099  *       bytes also, so we can ensure that the body of the message is always
0100  *       dword-aligned (64-bit). We use this alignment to keep ARM and x86
0101  *       happy. Probably word alignment would be OK, there might be a small
0102  *       performance advantage to using dword.
0103  * @dout: Output buffer (for data to EC). This buffer will always be
0104  *        dword-aligned and include enough space for up to 7 word-alignment
0105  *        bytes also, so we can ensure that the body of the message is always
0106  *        dword-aligned (64-bit). We use this alignment to keep ARM and x86
0107  *        happy. Probably word alignment would be OK, there might be a small
0108  *        performance advantage to using dword.
0109  * @din_size: Size of din buffer to allocate (zero to use static din).
0110  * @dout_size: Size of dout buffer to allocate (zero to use static dout).
0111  * @wake_enabled: True if this device can wake the system from sleep.
0112  * @suspended: True if this device had been suspended.
0113  * @cmd_xfer: Send command to EC and get response.
0114  *            Returns the number of bytes received if the communication
0115  *            succeeded, but that doesn't mean the EC was happy with the
0116  *            command. The caller should check msg.result for the EC's result
0117  *            code.
0118  * @pkt_xfer: Send packet to EC and get response.
0119  * @lock: One transaction at a time.
0120  * @mkbp_event_supported: 0 if MKBP not supported. Otherwise its value is
0121  *                        the maximum supported version of the MKBP host event
0122  *                        command + 1.
0123  * @host_sleep_v1: True if this EC supports the sleep v1 command.
0124  * @event_notifier: Interrupt event notifier for transport devices.
0125  * @event_data: Raw payload transferred with the MKBP event.
0126  * @event_size: Size in bytes of the event data.
0127  * @host_event_wake_mask: Mask of host events that cause wake from suspend.
0128  * @last_event_time: exact time from the hard irq when we got notified of
0129  *     a new event.
0130  * @notifier_ready: The notifier_block to let the kernel re-query EC
0131  *          communication protocol when the EC sends
0132  *          EC_HOST_EVENT_INTERFACE_READY.
0133  * @ec: The platform_device used by the mfd driver to interface with the
0134  *      main EC.
0135  * @pd: The platform_device used by the mfd driver to interface with the
0136  *      PD behind an EC.
0137  */
0138 struct cros_ec_device {
0139     /* These are used by other drivers that want to talk to the EC */
0140     const char *phys_name;
0141     struct device *dev;
0142     struct class *cros_class;
0143     int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
0144                unsigned int bytes, void *dest);
0145 
0146     /* These are used to implement the platform-specific interface */
0147     u16 max_request;
0148     u16 max_response;
0149     u16 max_passthru;
0150     u16 proto_version;
0151     void *priv;
0152     int irq;
0153     u8 *din;
0154     u8 *dout;
0155     int din_size;
0156     int dout_size;
0157     bool wake_enabled;
0158     bool suspended;
0159     int (*cmd_xfer)(struct cros_ec_device *ec,
0160             struct cros_ec_command *msg);
0161     int (*pkt_xfer)(struct cros_ec_device *ec,
0162             struct cros_ec_command *msg);
0163     struct mutex lock;
0164     u8 mkbp_event_supported;
0165     bool host_sleep_v1;
0166     struct blocking_notifier_head event_notifier;
0167 
0168     struct ec_response_get_next_event_v1 event_data;
0169     int event_size;
0170     u32 host_event_wake_mask;
0171     u32 last_resume_result;
0172     ktime_t last_event_time;
0173     struct notifier_block notifier_ready;
0174 
0175     /* The platform devices used by the mfd driver */
0176     struct platform_device *ec;
0177     struct platform_device *pd;
0178 };
0179 
0180 /**
0181  * struct cros_ec_platform - ChromeOS EC platform information.
0182  * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
0183  *           used in /dev/ and sysfs.
0184  * @cmd_offset: Offset to apply for each command. Set when
0185  *              registering a device behind another one.
0186  */
0187 struct cros_ec_platform {
0188     const char *ec_name;
0189     u16 cmd_offset;
0190 };
0191 
0192 /**
0193  * struct cros_ec_dev - ChromeOS EC device entry point.
0194  * @class_dev: Device structure used in sysfs.
0195  * @ec_dev: cros_ec_device structure to talk to the physical device.
0196  * @dev: Pointer to the platform device.
0197  * @debug_info: cros_ec_debugfs structure for debugging information.
0198  * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
0199  * @cmd_offset: Offset to apply for each command.
0200  * @features: Features supported by the EC.
0201  */
0202 struct cros_ec_dev {
0203     struct device class_dev;
0204     struct cros_ec_device *ec_dev;
0205     struct device *dev;
0206     struct cros_ec_debugfs *debug_info;
0207     bool has_kb_wake_angle;
0208     u16 cmd_offset;
0209     struct ec_response_get_features features;
0210 };
0211 
0212 #define to_cros_ec_dev(dev)  container_of(dev, struct cros_ec_dev, class_dev)
0213 
0214 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
0215                struct cros_ec_command *msg);
0216 
0217 int cros_ec_check_result(struct cros_ec_device *ec_dev,
0218              struct cros_ec_command *msg);
0219 
0220 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
0221              struct cros_ec_command *msg);
0222 
0223 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
0224                 struct cros_ec_command *msg);
0225 
0226 int cros_ec_query_all(struct cros_ec_device *ec_dev);
0227 
0228 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
0229                bool *wake_event,
0230                bool *has_more_events);
0231 
0232 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
0233 
0234 bool cros_ec_check_features(struct cros_ec_dev *ec, int feature);
0235 
0236 int cros_ec_get_sensor_count(struct cros_ec_dev *ec);
0237 
0238 int cros_ec_cmd(struct cros_ec_device *ec_dev, unsigned int version, int command, void *outdata,
0239             size_t outsize, void *indata, size_t insize);
0240 
0241 /**
0242  * cros_ec_get_time_ns() - Return time in ns.
0243  *
0244  * This is the function used to record the time for last_event_time in struct
0245  * cros_ec_device during the hard irq.
0246  *
0247  * Return: ktime_t format since boot.
0248  */
0249 static inline ktime_t cros_ec_get_time_ns(void)
0250 {
0251     return ktime_get_boottime_ns();
0252 }
0253 
0254 #endif /* __LINUX_CROS_EC_PROTO_H */