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