^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Expose an I2C passthrough to the ChromeOS EC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) // Copyright (C) 2013 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/platform_data/cros_ec_commands.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/platform_data/cros_ec_proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define I2C_MAX_RETRIES 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * struct ec_i2c_device - Driver data for I2C tunnel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @dev: Device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @adap: I2C adapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @ec: Pointer to EC device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @remote_bus: The EC bus number we tunnel to on the other side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @request_buf: Buffer for transmitting data; we expect most transfers to fit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @response_buf: Buffer for receiving data; we expect most transfers to fit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct ec_i2c_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct i2c_adapter adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct cros_ec_device *ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u16 remote_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u8 request_buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) u8 response_buf[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * ec_i2c_count_message - Count bytes needed for ec_i2c_construct_message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @i2c_msgs: The i2c messages to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @num: The number of i2c messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Returns the number of bytes the messages will take up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int ec_i2c_count_message(const struct i2c_msg i2c_msgs[], int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) size = sizeof(struct ec_params_i2c_passthru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) size += num * sizeof(struct ec_params_i2c_passthru_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) for (i = 0; i < num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!(i2c_msgs[i].flags & I2C_M_RD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) size += i2c_msgs[i].len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * ec_i2c_construct_message - construct a message to go to the EC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * This function effectively stuffs the standard i2c_msg format of Linux into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * a format that the EC understands.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @buf: The buffer to fill. We assume that the buffer is big enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @i2c_msgs: The i2c messages to read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * @num: The number of i2c messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * @bus_num: The remote bus number we want to talk to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Returns 0 or a negative error number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static int ec_i2c_construct_message(u8 *buf, const struct i2c_msg i2c_msgs[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int num, u16 bus_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct ec_params_i2c_passthru *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) u8 *out_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) out_data = buf + sizeof(struct ec_params_i2c_passthru) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) num * sizeof(struct ec_params_i2c_passthru_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) params = (struct ec_params_i2c_passthru *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) params->port = bus_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) params->num_msgs = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) const struct i2c_msg *i2c_msg = &i2c_msgs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct ec_params_i2c_passthru_msg *msg = ¶ms->msg[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) msg->len = i2c_msg->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) msg->addr_flags = i2c_msg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (i2c_msg->flags & I2C_M_TEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (i2c_msg->flags & I2C_M_RD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) msg->addr_flags |= EC_I2C_FLAG_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) memcpy(out_data, i2c_msg->buf, msg->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) out_data += msg->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * ec_i2c_count_response - Count bytes needed for ec_i2c_parse_response
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * @i2c_msgs: The i2c messages to to fill up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * @num: The number of i2c messages expected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Returns the number of response bytes expeced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int ec_i2c_count_response(struct i2c_msg i2c_msgs[], int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) size = sizeof(struct ec_response_i2c_passthru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) for (i = 0; i < num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (i2c_msgs[i].flags & I2C_M_RD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) size += i2c_msgs[i].len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * ec_i2c_parse_response - Parse a response from the EC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * We'll take the EC's response and copy it back into msgs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @buf: The buffer to parse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * @i2c_msgs: The i2c messages to to fill up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @num: The number of i2c messages; will be modified to include the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * number received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Returns 0 or a negative error number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int ec_i2c_parse_response(const u8 *buf, struct i2c_msg i2c_msgs[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int *num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) const struct ec_response_i2c_passthru *resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) const u8 *in_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) in_data = buf + sizeof(struct ec_response_i2c_passthru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) resp = (const struct ec_response_i2c_passthru *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (resp->i2c_status & EC_I2C_STATUS_TIMEOUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) else if (resp->i2c_status & EC_I2C_STATUS_NAK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) else if (resp->i2c_status & EC_I2C_STATUS_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* Other side could send us back fewer messages, but not more */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (resp->num_msgs > *num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) *num = resp->num_msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) for (i = 0; i < *num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct i2c_msg *i2c_msg = &i2c_msgs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (i2c_msgs[i].flags & I2C_M_RD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) memcpy(i2c_msg->buf, in_data, i2c_msg->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) in_data += i2c_msg->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct ec_i2c_device *bus = adap->algo_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct device *dev = bus->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) const u16 bus_num = bus->remote_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) int request_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int response_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct cros_ec_command *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) request_len = ec_i2c_count_message(i2c_msgs, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (request_len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) dev_warn(dev, "Error constructing message %d\n", request_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return request_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) response_len = ec_i2c_count_response(i2c_msgs, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (response_len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* Unexpected; no errors should come when NULL response */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) dev_warn(dev, "Error preparing response %d\n", response_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return response_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) alloc_size = max(request_len, response_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) msg = kmalloc(sizeof(*msg) + alloc_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) result = ec_i2c_construct_message(msg->data, i2c_msgs, num, bus_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) dev_err(dev, "Error constructing EC i2c message %d\n", result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) msg->version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) msg->command = EC_CMD_I2C_PASSTHRU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) msg->outsize = request_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) msg->insize = response_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) result = cros_ec_cmd_xfer_status(bus->ec, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) dev_err(dev, "Error transferring EC i2c message %d\n", result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) result = ec_i2c_parse_response(msg->data, i2c_msgs, &num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* Indicate success by saying how many messages were sent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) result = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) kfree(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static u32 ec_i2c_functionality(struct i2c_adapter *adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static const struct i2c_algorithm ec_i2c_algorithm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) .master_xfer = ec_i2c_xfer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) .functionality = ec_i2c_functionality,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static int ec_i2c_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct ec_i2c_device *bus = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) u32 remote_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!ec->cmd_xfer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) dev_err(dev, "Missing sendrecv\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (bus == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) err = device_property_read_u32(dev, "google,remote-bus", &remote_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) dev_err(dev, "Couldn't read remote-bus property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) bus->remote_bus = remote_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) bus->ec = ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) bus->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) bus->adap.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) strlcpy(bus->adap.name, "cros-ec-i2c-tunnel", sizeof(bus->adap.name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) bus->adap.algo = &ec_i2c_algorithm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) bus->adap.algo_data = bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) bus->adap.dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) bus->adap.dev.of_node = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) bus->adap.retries = I2C_MAX_RETRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ACPI_COMPANION_SET(&bus->adap.dev, ACPI_COMPANION(&pdev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) err = i2c_add_adapter(&bus->adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) platform_set_drvdata(pdev, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int ec_i2c_remove(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct ec_i2c_device *bus = platform_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) i2c_del_adapter(&bus->adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static const struct of_device_id cros_ec_i2c_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) { .compatible = "google,cros-ec-i2c-tunnel" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static const struct acpi_device_id cros_ec_i2c_tunnel_acpi_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) { "GOOG0012", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) MODULE_DEVICE_TABLE(acpi, cros_ec_i2c_tunnel_acpi_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static struct platform_driver ec_i2c_tunnel_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .probe = ec_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) .remove = ec_i2c_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) .name = "cros-ec-i2c-tunnel",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) .acpi_match_table = ACPI_PTR(cros_ec_i2c_tunnel_acpi_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) module_platform_driver(ec_i2c_tunnel_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MODULE_DESCRIPTION("EC I2C tunnel driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) MODULE_ALIAS("platform:cros-ec-i2c-tunnel");