0001
0002
0003
0004
0005
0006 #include <linux/acpi.h>
0007 #include <linux/delay.h>
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/i2c.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/platform_data/cros_ec_commands.h>
0013 #include <linux/platform_data/cros_ec_proto.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/slab.h>
0016
0017 #include "cros_ec.h"
0018
0019
0020
0021
0022
0023
0024
0025 struct ec_host_request_i2c {
0026
0027 uint8_t command_protocol;
0028 struct ec_host_request ec_request;
0029 } __packed;
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 struct ec_host_response_i2c {
0040 uint8_t result;
0041 uint8_t packet_length;
0042 struct ec_host_response ec_response;
0043 } __packed;
0044
0045 static inline struct cros_ec_device *to_ec_dev(struct device *dev)
0046 {
0047 struct i2c_client *client = to_i2c_client(dev);
0048
0049 return i2c_get_clientdata(client);
0050 }
0051
0052 static int cros_ec_pkt_xfer_i2c(struct cros_ec_device *ec_dev,
0053 struct cros_ec_command *msg)
0054 {
0055 struct i2c_client *client = ec_dev->priv;
0056 int ret = -ENOMEM;
0057 int i;
0058 int packet_len;
0059 u8 *out_buf = NULL;
0060 u8 *in_buf = NULL;
0061 u8 sum;
0062 struct i2c_msg i2c_msg[2];
0063 struct ec_host_response *ec_response;
0064 struct ec_host_request_i2c *ec_request_i2c;
0065 struct ec_host_response_i2c *ec_response_i2c;
0066 int request_header_size = sizeof(struct ec_host_request_i2c);
0067 int response_header_size = sizeof(struct ec_host_response_i2c);
0068
0069 i2c_msg[0].addr = client->addr;
0070 i2c_msg[0].flags = 0;
0071 i2c_msg[1].addr = client->addr;
0072 i2c_msg[1].flags = I2C_M_RD;
0073
0074 packet_len = msg->insize + response_header_size;
0075 if (packet_len > ec_dev->din_size) {
0076 ret = -EINVAL;
0077 goto done;
0078 }
0079 in_buf = ec_dev->din;
0080 i2c_msg[1].len = packet_len;
0081 i2c_msg[1].buf = (char *) in_buf;
0082
0083 packet_len = msg->outsize + request_header_size;
0084 if (packet_len > ec_dev->dout_size) {
0085 ret = -EINVAL;
0086 goto done;
0087 }
0088 out_buf = ec_dev->dout;
0089 i2c_msg[0].len = packet_len;
0090 i2c_msg[0].buf = (char *) out_buf;
0091
0092
0093 ec_request_i2c = (struct ec_host_request_i2c *) out_buf;
0094 ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3;
0095
0096 ec_dev->dout++;
0097 ret = cros_ec_prepare_tx(ec_dev, msg);
0098 if (ret < 0)
0099 goto done;
0100 ec_dev->dout--;
0101
0102
0103 ret = i2c_transfer(client->adapter, i2c_msg, 2);
0104 if (ret < 0) {
0105 dev_dbg(ec_dev->dev, "i2c transfer failed: %d\n", ret);
0106 goto done;
0107 } else if (ret != 2) {
0108 dev_err(ec_dev->dev, "failed to get response: %d\n", ret);
0109 ret = -EIO;
0110 goto done;
0111 }
0112
0113 ec_response_i2c = (struct ec_host_response_i2c *) in_buf;
0114 msg->result = ec_response_i2c->result;
0115 ec_response = &ec_response_i2c->ec_response;
0116
0117 switch (msg->result) {
0118 case EC_RES_SUCCESS:
0119 break;
0120 case EC_RES_IN_PROGRESS:
0121 ret = -EAGAIN;
0122 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
0123 msg->command);
0124 goto done;
0125
0126 default:
0127 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
0128 msg->command, msg->result);
0129
0130
0131
0132
0133
0134
0135
0136
0137 if (ec_response_i2c->result == EC_RES_INVALID_COMMAND &&
0138 ec_response_i2c->packet_length == 0) {
0139 ret = -EPROTONOSUPPORT;
0140 goto done;
0141 }
0142 }
0143
0144 if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) {
0145 dev_err(ec_dev->dev,
0146 "response of %u bytes too short; not a full header\n",
0147 ec_response_i2c->packet_length);
0148 ret = -EBADMSG;
0149 goto done;
0150 }
0151
0152 if (msg->insize < ec_response->data_len) {
0153 dev_err(ec_dev->dev,
0154 "response data size is too large: expected %u, got %u\n",
0155 msg->insize,
0156 ec_response->data_len);
0157 ret = -EMSGSIZE;
0158 goto done;
0159 }
0160
0161
0162 sum = 0;
0163 for (i = 0; i < sizeof(struct ec_host_response); i++)
0164 sum += ((u8 *)ec_response)[i];
0165
0166 memcpy(msg->data,
0167 in_buf + response_header_size,
0168 ec_response->data_len);
0169 for (i = 0; i < ec_response->data_len; i++)
0170 sum += msg->data[i];
0171
0172
0173 if (sum) {
0174 dev_err(ec_dev->dev, "bad packet checksum\n");
0175 ret = -EBADMSG;
0176 goto done;
0177 }
0178
0179 ret = ec_response->data_len;
0180
0181 done:
0182 if (msg->command == EC_CMD_REBOOT_EC)
0183 msleep(EC_REBOOT_DELAY_MS);
0184
0185 return ret;
0186 }
0187
0188 static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
0189 struct cros_ec_command *msg)
0190 {
0191 struct i2c_client *client = ec_dev->priv;
0192 int ret = -ENOMEM;
0193 int i;
0194 int len;
0195 int packet_len;
0196 u8 *out_buf = NULL;
0197 u8 *in_buf = NULL;
0198 u8 sum;
0199 struct i2c_msg i2c_msg[2];
0200
0201 i2c_msg[0].addr = client->addr;
0202 i2c_msg[0].flags = 0;
0203 i2c_msg[1].addr = client->addr;
0204 i2c_msg[1].flags = I2C_M_RD;
0205
0206
0207
0208
0209
0210 packet_len = msg->insize + 3;
0211 in_buf = kzalloc(packet_len, GFP_KERNEL);
0212 if (!in_buf)
0213 goto done;
0214 i2c_msg[1].len = packet_len;
0215 i2c_msg[1].buf = (char *)in_buf;
0216
0217
0218
0219
0220
0221 packet_len = msg->outsize + 4;
0222 out_buf = kzalloc(packet_len, GFP_KERNEL);
0223 if (!out_buf)
0224 goto done;
0225 i2c_msg[0].len = packet_len;
0226 i2c_msg[0].buf = (char *)out_buf;
0227
0228 out_buf[0] = EC_CMD_VERSION0 + msg->version;
0229 out_buf[1] = msg->command;
0230 out_buf[2] = msg->outsize;
0231
0232
0233 sum = out_buf[0] + out_buf[1] + out_buf[2];
0234 for (i = 0; i < msg->outsize; i++) {
0235 out_buf[3 + i] = msg->data[i];
0236 sum += out_buf[3 + i];
0237 }
0238 out_buf[3 + msg->outsize] = sum;
0239
0240
0241 ret = i2c_transfer(client->adapter, i2c_msg, 2);
0242 if (ret < 0) {
0243 dev_err(ec_dev->dev, "i2c transfer failed: %d\n", ret);
0244 goto done;
0245 } else if (ret != 2) {
0246 dev_err(ec_dev->dev, "failed to get response: %d\n", ret);
0247 ret = -EIO;
0248 goto done;
0249 }
0250
0251
0252 msg->result = i2c_msg[1].buf[0];
0253 ret = cros_ec_check_result(ec_dev, msg);
0254 if (ret)
0255 goto done;
0256
0257 len = in_buf[1];
0258 if (len > msg->insize) {
0259 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
0260 len, msg->insize);
0261 ret = -ENOSPC;
0262 goto done;
0263 }
0264
0265
0266 sum = in_buf[0] + in_buf[1];
0267 for (i = 0; i < len; i++) {
0268 msg->data[i] = in_buf[2 + i];
0269 sum += in_buf[2 + i];
0270 }
0271 dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n",
0272 i2c_msg[1].len, in_buf, sum);
0273 if (sum != in_buf[2 + len]) {
0274 dev_err(ec_dev->dev, "bad packet checksum\n");
0275 ret = -EBADMSG;
0276 goto done;
0277 }
0278
0279 ret = len;
0280 done:
0281 kfree(in_buf);
0282 kfree(out_buf);
0283 if (msg->command == EC_CMD_REBOOT_EC)
0284 msleep(EC_REBOOT_DELAY_MS);
0285
0286 return ret;
0287 }
0288
0289 static int cros_ec_i2c_probe(struct i2c_client *client,
0290 const struct i2c_device_id *dev_id)
0291 {
0292 struct device *dev = &client->dev;
0293 struct cros_ec_device *ec_dev = NULL;
0294 int err;
0295
0296 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
0297 if (!ec_dev)
0298 return -ENOMEM;
0299
0300 i2c_set_clientdata(client, ec_dev);
0301 ec_dev->dev = dev;
0302 ec_dev->priv = client;
0303 ec_dev->irq = client->irq;
0304 ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c;
0305 ec_dev->pkt_xfer = cros_ec_pkt_xfer_i2c;
0306 ec_dev->phys_name = client->adapter->name;
0307 ec_dev->din_size = sizeof(struct ec_host_response_i2c) +
0308 sizeof(struct ec_response_get_protocol_info);
0309 ec_dev->dout_size = sizeof(struct ec_host_request_i2c);
0310
0311 err = cros_ec_register(ec_dev);
0312 if (err) {
0313 dev_err(dev, "cannot register EC\n");
0314 return err;
0315 }
0316
0317 return 0;
0318 }
0319
0320 static int cros_ec_i2c_remove(struct i2c_client *client)
0321 {
0322 struct cros_ec_device *ec_dev = i2c_get_clientdata(client);
0323
0324 cros_ec_unregister(ec_dev);
0325
0326 return 0;
0327 }
0328
0329 #ifdef CONFIG_PM_SLEEP
0330 static int cros_ec_i2c_suspend(struct device *dev)
0331 {
0332 struct cros_ec_device *ec_dev = to_ec_dev(dev);
0333
0334 return cros_ec_suspend(ec_dev);
0335 }
0336
0337 static int cros_ec_i2c_resume(struct device *dev)
0338 {
0339 struct cros_ec_device *ec_dev = to_ec_dev(dev);
0340
0341 return cros_ec_resume(ec_dev);
0342 }
0343 #endif
0344
0345 static const struct dev_pm_ops cros_ec_i2c_pm_ops = {
0346 SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_i2c_suspend, cros_ec_i2c_resume)
0347 };
0348
0349 #ifdef CONFIG_OF
0350 static const struct of_device_id cros_ec_i2c_of_match[] = {
0351 { .compatible = "google,cros-ec-i2c", },
0352 { },
0353 };
0354 MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
0355 #endif
0356
0357 static const struct i2c_device_id cros_ec_i2c_id[] = {
0358 { "cros-ec-i2c", 0 },
0359 { }
0360 };
0361 MODULE_DEVICE_TABLE(i2c, cros_ec_i2c_id);
0362
0363 #ifdef CONFIG_ACPI
0364 static const struct acpi_device_id cros_ec_i2c_acpi_id[] = {
0365 { "GOOG0008", 0 },
0366 { }
0367 };
0368 MODULE_DEVICE_TABLE(acpi, cros_ec_i2c_acpi_id);
0369 #endif
0370
0371 static struct i2c_driver cros_ec_driver = {
0372 .driver = {
0373 .name = "cros-ec-i2c",
0374 .acpi_match_table = ACPI_PTR(cros_ec_i2c_acpi_id),
0375 .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
0376 .pm = &cros_ec_i2c_pm_ops,
0377 },
0378 .probe = cros_ec_i2c_probe,
0379 .remove = cros_ec_i2c_remove,
0380 .id_table = cros_ec_i2c_id,
0381 };
0382
0383 module_i2c_driver(cros_ec_driver);
0384
0385 MODULE_LICENSE("GPL v2");
0386 MODULE_DESCRIPTION("I2C interface for ChromeOS Embedded Controller");