Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ChromeOS EC multi-function device (I2C) |
| 3 | * |
| 4 | * Copyright (C) 2012 Google, Inc |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
Wei-Ning Huang | abeed71 | 2018-04-18 12:24:03 +0200 | [diff] [blame^] | 16 | #include <linux/acpi.h> |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 17 | #include <linux/delay.h> |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/mfd/cros_ec.h> |
| 23 | #include <linux/mfd/cros_ec_commands.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 27 | /** |
| 28 | * Request format for protocol v3 |
| 29 | * byte 0 0xda (EC_COMMAND_PROTOCOL_3) |
| 30 | * byte 1-8 struct ec_host_request |
| 31 | * byte 10- response data |
| 32 | */ |
| 33 | struct ec_host_request_i2c { |
| 34 | /* Always 0xda to backward compatible with v2 struct */ |
| 35 | uint8_t command_protocol; |
| 36 | struct ec_host_request ec_request; |
| 37 | } __packed; |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * Response format for protocol v3 |
| 42 | * byte 0 result code |
| 43 | * byte 1 packet_length |
| 44 | * byte 2-9 struct ec_host_response |
| 45 | * byte 10- response data |
| 46 | */ |
| 47 | struct ec_host_response_i2c { |
| 48 | uint8_t result; |
| 49 | uint8_t packet_length; |
| 50 | struct ec_host_response ec_response; |
| 51 | } __packed; |
| 52 | |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 53 | static inline struct cros_ec_device *to_ec_dev(struct device *dev) |
| 54 | { |
| 55 | struct i2c_client *client = to_i2c_client(dev); |
| 56 | |
| 57 | return i2c_get_clientdata(client); |
| 58 | } |
| 59 | |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 60 | static int cros_ec_pkt_xfer_i2c(struct cros_ec_device *ec_dev, |
| 61 | struct cros_ec_command *msg) |
| 62 | { |
| 63 | struct i2c_client *client = ec_dev->priv; |
| 64 | int ret = -ENOMEM; |
| 65 | int i; |
| 66 | int packet_len; |
| 67 | u8 *out_buf = NULL; |
| 68 | u8 *in_buf = NULL; |
| 69 | u8 sum; |
| 70 | struct i2c_msg i2c_msg[2]; |
| 71 | struct ec_host_response *ec_response; |
| 72 | struct ec_host_request_i2c *ec_request_i2c; |
| 73 | struct ec_host_response_i2c *ec_response_i2c; |
| 74 | int request_header_size = sizeof(struct ec_host_request_i2c); |
| 75 | int response_header_size = sizeof(struct ec_host_response_i2c); |
| 76 | |
| 77 | i2c_msg[0].addr = client->addr; |
| 78 | i2c_msg[0].flags = 0; |
| 79 | i2c_msg[1].addr = client->addr; |
| 80 | i2c_msg[1].flags = I2C_M_RD; |
| 81 | |
| 82 | packet_len = msg->insize + response_header_size; |
| 83 | BUG_ON(packet_len > ec_dev->din_size); |
| 84 | in_buf = ec_dev->din; |
| 85 | i2c_msg[1].len = packet_len; |
| 86 | i2c_msg[1].buf = (char *) in_buf; |
| 87 | |
| 88 | packet_len = msg->outsize + request_header_size; |
| 89 | BUG_ON(packet_len > ec_dev->dout_size); |
| 90 | out_buf = ec_dev->dout; |
| 91 | i2c_msg[0].len = packet_len; |
| 92 | i2c_msg[0].buf = (char *) out_buf; |
| 93 | |
| 94 | /* create request data */ |
| 95 | ec_request_i2c = (struct ec_host_request_i2c *) out_buf; |
| 96 | ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3; |
| 97 | |
| 98 | ec_dev->dout++; |
| 99 | ret = cros_ec_prepare_tx(ec_dev, msg); |
| 100 | ec_dev->dout--; |
| 101 | |
| 102 | /* send command to EC and read answer */ |
| 103 | ret = i2c_transfer(client->adapter, i2c_msg, 2); |
| 104 | if (ret < 0) { |
| 105 | dev_dbg(ec_dev->dev, "i2c transfer failed: %d\n", ret); |
| 106 | goto done; |
| 107 | } else if (ret != 2) { |
| 108 | dev_err(ec_dev->dev, "failed to get response: %d\n", ret); |
| 109 | ret = -EIO; |
| 110 | goto done; |
| 111 | } |
| 112 | |
| 113 | ec_response_i2c = (struct ec_host_response_i2c *) in_buf; |
| 114 | msg->result = ec_response_i2c->result; |
| 115 | ec_response = &ec_response_i2c->ec_response; |
| 116 | |
| 117 | switch (msg->result) { |
| 118 | case EC_RES_SUCCESS: |
| 119 | break; |
| 120 | case EC_RES_IN_PROGRESS: |
| 121 | ret = -EAGAIN; |
| 122 | dev_dbg(ec_dev->dev, "command 0x%02x in progress\n", |
| 123 | msg->command); |
| 124 | goto done; |
| 125 | |
| 126 | default: |
| 127 | dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n", |
| 128 | msg->command, msg->result); |
| 129 | /* |
| 130 | * When we send v3 request to v2 ec, ec won't recognize the |
| 131 | * 0xda (EC_COMMAND_PROTOCOL_3) and will return with status |
| 132 | * EC_RES_INVALID_COMMAND with zero data length. |
| 133 | * |
| 134 | * In case of invalid command for v3 protocol the data length |
| 135 | * will be at least sizeof(struct ec_host_response) |
| 136 | */ |
| 137 | if (ec_response_i2c->result == EC_RES_INVALID_COMMAND && |
| 138 | ec_response_i2c->packet_length == 0) { |
| 139 | ret = -EPROTONOSUPPORT; |
| 140 | goto done; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) { |
| 145 | dev_err(ec_dev->dev, |
| 146 | "response of %u bytes too short; not a full header\n", |
| 147 | ec_response_i2c->packet_length); |
| 148 | ret = -EBADMSG; |
| 149 | goto done; |
| 150 | } |
| 151 | |
| 152 | if (msg->insize < ec_response->data_len) { |
| 153 | dev_err(ec_dev->dev, |
| 154 | "response data size is too large: expected %u, got %u\n", |
| 155 | msg->insize, |
| 156 | ec_response->data_len); |
| 157 | ret = -EMSGSIZE; |
| 158 | goto done; |
| 159 | } |
| 160 | |
| 161 | /* copy response packet payload and compute checksum */ |
| 162 | sum = 0; |
| 163 | for (i = 0; i < sizeof(struct ec_host_response); i++) |
| 164 | sum += ((u8 *)ec_response)[i]; |
| 165 | |
| 166 | memcpy(msg->data, |
| 167 | in_buf + response_header_size, |
| 168 | ec_response->data_len); |
| 169 | for (i = 0; i < ec_response->data_len; i++) |
| 170 | sum += msg->data[i]; |
| 171 | |
| 172 | /* All bytes should sum to zero */ |
| 173 | if (sum) { |
| 174 | dev_err(ec_dev->dev, "bad packet checksum\n"); |
| 175 | ret = -EBADMSG; |
| 176 | goto done; |
| 177 | } |
| 178 | |
| 179 | ret = ec_response->data_len; |
| 180 | |
| 181 | done: |
| 182 | if (msg->command == EC_CMD_REBOOT_EC) |
| 183 | msleep(EC_REBOOT_DELAY_MS); |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 188 | static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev, |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 189 | struct cros_ec_command *msg) |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 190 | { |
| 191 | struct i2c_client *client = ec_dev->priv; |
| 192 | int ret = -ENOMEM; |
| 193 | int i; |
Doug Anderson | d6c15ed | 2014-06-27 12:56:12 -0700 | [diff] [blame] | 194 | int len; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 195 | int packet_len; |
| 196 | u8 *out_buf = NULL; |
| 197 | u8 *in_buf = NULL; |
| 198 | u8 sum; |
| 199 | struct i2c_msg i2c_msg[2]; |
| 200 | |
| 201 | i2c_msg[0].addr = client->addr; |
| 202 | i2c_msg[0].flags = 0; |
| 203 | i2c_msg[1].addr = client->addr; |
| 204 | i2c_msg[1].flags = I2C_M_RD; |
| 205 | |
| 206 | /* |
| 207 | * allocate larger packet (one byte for checksum, one byte for |
| 208 | * length, and one for result code) |
| 209 | */ |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 210 | packet_len = msg->insize + 3; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 211 | in_buf = kzalloc(packet_len, GFP_KERNEL); |
| 212 | if (!in_buf) |
| 213 | goto done; |
| 214 | i2c_msg[1].len = packet_len; |
| 215 | i2c_msg[1].buf = (char *)in_buf; |
| 216 | |
| 217 | /* |
| 218 | * allocate larger packet (one byte for checksum, one for |
| 219 | * command code, one for length, and one for command version) |
| 220 | */ |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 221 | packet_len = msg->outsize + 4; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 222 | out_buf = kzalloc(packet_len, GFP_KERNEL); |
| 223 | if (!out_buf) |
| 224 | goto done; |
| 225 | i2c_msg[0].len = packet_len; |
| 226 | i2c_msg[0].buf = (char *)out_buf; |
| 227 | |
| 228 | out_buf[0] = EC_CMD_VERSION0 + msg->version; |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 229 | out_buf[1] = msg->command; |
| 230 | out_buf[2] = msg->outsize; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 231 | |
| 232 | /* copy message payload and compute checksum */ |
| 233 | sum = out_buf[0] + out_buf[1] + out_buf[2]; |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 234 | for (i = 0; i < msg->outsize; i++) { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 235 | out_buf[3 + i] = msg->data[i]; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 236 | sum += out_buf[3 + i]; |
| 237 | } |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 238 | out_buf[3 + msg->outsize] = sum; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 239 | |
| 240 | /* send command to EC and read answer */ |
| 241 | ret = i2c_transfer(client->adapter, i2c_msg, 2); |
| 242 | if (ret < 0) { |
| 243 | dev_err(ec_dev->dev, "i2c transfer failed: %d\n", ret); |
| 244 | goto done; |
| 245 | } else if (ret != 2) { |
| 246 | dev_err(ec_dev->dev, "failed to get response: %d\n", ret); |
| 247 | ret = -EIO; |
| 248 | goto done; |
| 249 | } |
| 250 | |
| 251 | /* check response error code */ |
Bill Richardson | 6db07b6 | 2014-06-18 11:14:05 -0700 | [diff] [blame] | 252 | msg->result = i2c_msg[1].buf[0]; |
| 253 | ret = cros_ec_check_result(ec_dev, msg); |
| 254 | if (ret) |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 255 | goto done; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 256 | |
Doug Anderson | d6c15ed | 2014-06-27 12:56:12 -0700 | [diff] [blame] | 257 | len = in_buf[1]; |
| 258 | if (len > msg->insize) { |
| 259 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", |
| 260 | len, msg->insize); |
| 261 | ret = -ENOSPC; |
| 262 | goto done; |
| 263 | } |
| 264 | |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 265 | /* copy response packet payload and compute checksum */ |
| 266 | sum = in_buf[0] + in_buf[1]; |
Doug Anderson | d6c15ed | 2014-06-27 12:56:12 -0700 | [diff] [blame] | 267 | for (i = 0; i < len; i++) { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 268 | msg->data[i] = in_buf[2 + i]; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 269 | sum += in_buf[2 + i]; |
| 270 | } |
| 271 | dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n", |
| 272 | i2c_msg[1].len, in_buf, sum); |
Doug Anderson | d6c15ed | 2014-06-27 12:56:12 -0700 | [diff] [blame] | 273 | if (sum != in_buf[2 + len]) { |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 274 | dev_err(ec_dev->dev, "bad packet checksum\n"); |
| 275 | ret = -EBADMSG; |
| 276 | goto done; |
| 277 | } |
| 278 | |
Doug Anderson | d6c15ed | 2014-06-27 12:56:12 -0700 | [diff] [blame] | 279 | ret = len; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 280 | done: |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 281 | kfree(in_buf); |
| 282 | kfree(out_buf); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 283 | if (msg->command == EC_CMD_REBOOT_EC) |
| 284 | msleep(EC_REBOOT_DELAY_MS); |
| 285 | |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 286 | return ret; |
| 287 | } |
| 288 | |
Thierry Reding | 192afe5 | 2013-11-25 13:22:31 +0100 | [diff] [blame] | 289 | static int cros_ec_i2c_probe(struct i2c_client *client, |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 290 | const struct i2c_device_id *dev_id) |
| 291 | { |
| 292 | struct device *dev = &client->dev; |
| 293 | struct cros_ec_device *ec_dev = NULL; |
| 294 | int err; |
| 295 | |
Lee Jones | 2756db6 | 2015-10-28 16:02:48 +0000 | [diff] [blame] | 296 | ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 297 | if (!ec_dev) |
| 298 | return -ENOMEM; |
| 299 | |
| 300 | i2c_set_clientdata(client, ec_dev); |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 301 | ec_dev->dev = dev; |
| 302 | ec_dev->priv = client; |
| 303 | ec_dev->irq = client->irq; |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 304 | ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 305 | ec_dev->pkt_xfer = cros_ec_pkt_xfer_i2c; |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 306 | ec_dev->phys_name = client->adapter->name; |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 307 | ec_dev->din_size = sizeof(struct ec_host_response_i2c) + |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 308 | sizeof(struct ec_response_get_protocol_info); |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 309 | ec_dev->dout_size = sizeof(struct ec_host_request_i2c); |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 310 | |
| 311 | err = cros_ec_register(ec_dev); |
| 312 | if (err) { |
| 313 | dev_err(dev, "cannot register EC\n"); |
| 314 | return err; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Thierry Reding | 192afe5 | 2013-11-25 13:22:31 +0100 | [diff] [blame] | 320 | static int cros_ec_i2c_remove(struct i2c_client *client) |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 321 | { |
| 322 | struct cros_ec_device *ec_dev = i2c_get_clientdata(client); |
| 323 | |
| 324 | cros_ec_remove(ec_dev); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | #ifdef CONFIG_PM_SLEEP |
| 330 | static int cros_ec_i2c_suspend(struct device *dev) |
| 331 | { |
| 332 | struct cros_ec_device *ec_dev = to_ec_dev(dev); |
| 333 | |
| 334 | return cros_ec_suspend(ec_dev); |
| 335 | } |
| 336 | |
| 337 | static int cros_ec_i2c_resume(struct device *dev) |
| 338 | { |
| 339 | struct cros_ec_device *ec_dev = to_ec_dev(dev); |
| 340 | |
| 341 | return cros_ec_resume(ec_dev); |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | static SIMPLE_DEV_PM_OPS(cros_ec_i2c_pm_ops, cros_ec_i2c_suspend, |
| 346 | cros_ec_i2c_resume); |
| 347 | |
Wei-Ning Huang | abeed71 | 2018-04-18 12:24:03 +0200 | [diff] [blame^] | 348 | #ifdef CONFIG_OF |
Javier Martinez Canillas | 385c001 | 2015-08-25 07:48:10 +0200 | [diff] [blame] | 349 | static const struct of_device_id cros_ec_i2c_of_match[] = { |
| 350 | { .compatible = "google,cros-ec-i2c", }, |
| 351 | { /* sentinel */ }, |
| 352 | }; |
| 353 | MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match); |
Wei-Ning Huang | abeed71 | 2018-04-18 12:24:03 +0200 | [diff] [blame^] | 354 | #endif |
Javier Martinez Canillas | 385c001 | 2015-08-25 07:48:10 +0200 | [diff] [blame] | 355 | |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 356 | static const struct i2c_device_id cros_ec_i2c_id[] = { |
| 357 | { "cros-ec-i2c", 0 }, |
| 358 | { } |
| 359 | }; |
| 360 | MODULE_DEVICE_TABLE(i2c, cros_ec_i2c_id); |
| 361 | |
Wei-Ning Huang | abeed71 | 2018-04-18 12:24:03 +0200 | [diff] [blame^] | 362 | #ifdef CONFIG_ACPI |
| 363 | static const struct acpi_device_id cros_ec_i2c_acpi_id[] = { |
| 364 | { "GOOG0008", 0 }, |
| 365 | { /* sentinel */ } |
| 366 | }; |
| 367 | MODULE_DEVICE_TABLE(acpi, cros_ec_i2c_acpi_id); |
| 368 | #endif |
| 369 | |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 370 | static struct i2c_driver cros_ec_driver = { |
| 371 | .driver = { |
| 372 | .name = "cros-ec-i2c", |
Wei-Ning Huang | abeed71 | 2018-04-18 12:24:03 +0200 | [diff] [blame^] | 373 | .acpi_match_table = ACPI_PTR(cros_ec_i2c_acpi_id), |
Javier Martinez Canillas | 385c001 | 2015-08-25 07:48:10 +0200 | [diff] [blame] | 374 | .of_match_table = of_match_ptr(cros_ec_i2c_of_match), |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 375 | .pm = &cros_ec_i2c_pm_ops, |
| 376 | }, |
Thierry Reding | 192afe5 | 2013-11-25 13:22:31 +0100 | [diff] [blame] | 377 | .probe = cros_ec_i2c_probe, |
| 378 | .remove = cros_ec_i2c_remove, |
Simon Glass | 8996900 | 2013-02-25 14:08:38 -0800 | [diff] [blame] | 379 | .id_table = cros_ec_i2c_id, |
| 380 | }; |
| 381 | |
| 382 | module_i2c_driver(cros_ec_driver); |
| 383 | |
| 384 | MODULE_LICENSE("GPL"); |
| 385 | MODULE_DESCRIPTION("ChromeOS EC multi function device"); |