blob: 9f70de1e4c70567ae9906e59df4b49c798f37b73 [file] [log] [blame]
Simon Glass89969002013-02-25 14:08:38 -08001/*
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
Stephen Barberd3654072015-06-09 13:04:46 +020016#include <linux/delay.h>
Simon Glass89969002013-02-25 14:08:38 -080017#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/i2c.h>
20#include <linux/interrupt.h>
21#include <linux/mfd/cros_ec.h>
22#include <linux/mfd/cros_ec_commands.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25
Stephen Barberd3654072015-06-09 13:04:46 +020026/**
27 * Request format for protocol v3
28 * byte 0 0xda (EC_COMMAND_PROTOCOL_3)
29 * byte 1-8 struct ec_host_request
30 * byte 10- response data
31 */
32struct ec_host_request_i2c {
33 /* Always 0xda to backward compatible with v2 struct */
34 uint8_t command_protocol;
35 struct ec_host_request ec_request;
36} __packed;
37
38
39/*
40 * Response format for protocol v3
41 * byte 0 result code
42 * byte 1 packet_length
43 * byte 2-9 struct ec_host_response
44 * byte 10- response data
45 */
46struct ec_host_response_i2c {
47 uint8_t result;
48 uint8_t packet_length;
49 struct ec_host_response ec_response;
50} __packed;
51
Simon Glass89969002013-02-25 14:08:38 -080052static inline struct cros_ec_device *to_ec_dev(struct device *dev)
53{
54 struct i2c_client *client = to_i2c_client(dev);
55
56 return i2c_get_clientdata(client);
57}
58
Stephen Barberd3654072015-06-09 13:04:46 +020059static int cros_ec_pkt_xfer_i2c(struct cros_ec_device *ec_dev,
60 struct cros_ec_command *msg)
61{
62 struct i2c_client *client = ec_dev->priv;
63 int ret = -ENOMEM;
64 int i;
65 int packet_len;
66 u8 *out_buf = NULL;
67 u8 *in_buf = NULL;
68 u8 sum;
69 struct i2c_msg i2c_msg[2];
70 struct ec_host_response *ec_response;
71 struct ec_host_request_i2c *ec_request_i2c;
72 struct ec_host_response_i2c *ec_response_i2c;
73 int request_header_size = sizeof(struct ec_host_request_i2c);
74 int response_header_size = sizeof(struct ec_host_response_i2c);
75
76 i2c_msg[0].addr = client->addr;
77 i2c_msg[0].flags = 0;
78 i2c_msg[1].addr = client->addr;
79 i2c_msg[1].flags = I2C_M_RD;
80
81 packet_len = msg->insize + response_header_size;
82 BUG_ON(packet_len > ec_dev->din_size);
83 in_buf = ec_dev->din;
84 i2c_msg[1].len = packet_len;
85 i2c_msg[1].buf = (char *) in_buf;
86
87 packet_len = msg->outsize + request_header_size;
88 BUG_ON(packet_len > ec_dev->dout_size);
89 out_buf = ec_dev->dout;
90 i2c_msg[0].len = packet_len;
91 i2c_msg[0].buf = (char *) out_buf;
92
93 /* create request data */
94 ec_request_i2c = (struct ec_host_request_i2c *) out_buf;
95 ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3;
96
97 ec_dev->dout++;
98 ret = cros_ec_prepare_tx(ec_dev, msg);
99 ec_dev->dout--;
100
101 /* send command to EC and read answer */
102 ret = i2c_transfer(client->adapter, i2c_msg, 2);
103 if (ret < 0) {
104 dev_dbg(ec_dev->dev, "i2c transfer failed: %d\n", ret);
105 goto done;
106 } else if (ret != 2) {
107 dev_err(ec_dev->dev, "failed to get response: %d\n", ret);
108 ret = -EIO;
109 goto done;
110 }
111
112 ec_response_i2c = (struct ec_host_response_i2c *) in_buf;
113 msg->result = ec_response_i2c->result;
114 ec_response = &ec_response_i2c->ec_response;
115
116 switch (msg->result) {
117 case EC_RES_SUCCESS:
118 break;
119 case EC_RES_IN_PROGRESS:
120 ret = -EAGAIN;
121 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
122 msg->command);
123 goto done;
124
125 default:
126 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
127 msg->command, msg->result);
128 /*
129 * When we send v3 request to v2 ec, ec won't recognize the
130 * 0xda (EC_COMMAND_PROTOCOL_3) and will return with status
131 * EC_RES_INVALID_COMMAND with zero data length.
132 *
133 * In case of invalid command for v3 protocol the data length
134 * will be at least sizeof(struct ec_host_response)
135 */
136 if (ec_response_i2c->result == EC_RES_INVALID_COMMAND &&
137 ec_response_i2c->packet_length == 0) {
138 ret = -EPROTONOSUPPORT;
139 goto done;
140 }
141 }
142
143 if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) {
144 dev_err(ec_dev->dev,
145 "response of %u bytes too short; not a full header\n",
146 ec_response_i2c->packet_length);
147 ret = -EBADMSG;
148 goto done;
149 }
150
151 if (msg->insize < ec_response->data_len) {
152 dev_err(ec_dev->dev,
153 "response data size is too large: expected %u, got %u\n",
154 msg->insize,
155 ec_response->data_len);
156 ret = -EMSGSIZE;
157 goto done;
158 }
159
160 /* copy response packet payload and compute checksum */
161 sum = 0;
162 for (i = 0; i < sizeof(struct ec_host_response); i++)
163 sum += ((u8 *)ec_response)[i];
164
165 memcpy(msg->data,
166 in_buf + response_header_size,
167 ec_response->data_len);
168 for (i = 0; i < ec_response->data_len; i++)
169 sum += msg->data[i];
170
171 /* All bytes should sum to zero */
172 if (sum) {
173 dev_err(ec_dev->dev, "bad packet checksum\n");
174 ret = -EBADMSG;
175 goto done;
176 }
177
178 ret = ec_response->data_len;
179
180done:
181 if (msg->command == EC_CMD_REBOOT_EC)
182 msleep(EC_REBOOT_DELAY_MS);
183
184 return ret;
185}
186
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700187static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700188 struct cros_ec_command *msg)
Simon Glass89969002013-02-25 14:08:38 -0800189{
190 struct i2c_client *client = ec_dev->priv;
191 int ret = -ENOMEM;
192 int i;
Doug Andersond6c15ed2014-06-27 12:56:12 -0700193 int len;
Simon Glass89969002013-02-25 14:08:38 -0800194 int packet_len;
195 u8 *out_buf = NULL;
196 u8 *in_buf = NULL;
197 u8 sum;
198 struct i2c_msg i2c_msg[2];
199
200 i2c_msg[0].addr = client->addr;
201 i2c_msg[0].flags = 0;
202 i2c_msg[1].addr = client->addr;
203 i2c_msg[1].flags = I2C_M_RD;
204
205 /*
206 * allocate larger packet (one byte for checksum, one byte for
207 * length, and one for result code)
208 */
Bill Richardson5d4773e2014-06-18 11:14:02 -0700209 packet_len = msg->insize + 3;
Simon Glass89969002013-02-25 14:08:38 -0800210 in_buf = kzalloc(packet_len, GFP_KERNEL);
211 if (!in_buf)
212 goto done;
213 i2c_msg[1].len = packet_len;
214 i2c_msg[1].buf = (char *)in_buf;
215
216 /*
217 * allocate larger packet (one byte for checksum, one for
218 * command code, one for length, and one for command version)
219 */
Bill Richardson5d4773e2014-06-18 11:14:02 -0700220 packet_len = msg->outsize + 4;
Simon Glass89969002013-02-25 14:08:38 -0800221 out_buf = kzalloc(packet_len, GFP_KERNEL);
222 if (!out_buf)
223 goto done;
224 i2c_msg[0].len = packet_len;
225 i2c_msg[0].buf = (char *)out_buf;
226
227 out_buf[0] = EC_CMD_VERSION0 + msg->version;
Bill Richardson5d4773e2014-06-18 11:14:02 -0700228 out_buf[1] = msg->command;
229 out_buf[2] = msg->outsize;
Simon Glass89969002013-02-25 14:08:38 -0800230
231 /* copy message payload and compute checksum */
232 sum = out_buf[0] + out_buf[1] + out_buf[2];
Bill Richardson5d4773e2014-06-18 11:14:02 -0700233 for (i = 0; i < msg->outsize; i++) {
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200234 out_buf[3 + i] = msg->data[i];
Simon Glass89969002013-02-25 14:08:38 -0800235 sum += out_buf[3 + i];
236 }
Bill Richardson5d4773e2014-06-18 11:14:02 -0700237 out_buf[3 + msg->outsize] = sum;
Simon Glass89969002013-02-25 14:08:38 -0800238
239 /* send command to EC and read answer */
240 ret = i2c_transfer(client->adapter, i2c_msg, 2);
241 if (ret < 0) {
242 dev_err(ec_dev->dev, "i2c transfer failed: %d\n", ret);
243 goto done;
244 } else if (ret != 2) {
245 dev_err(ec_dev->dev, "failed to get response: %d\n", ret);
246 ret = -EIO;
247 goto done;
248 }
249
250 /* check response error code */
Bill Richardson6db07b62014-06-18 11:14:05 -0700251 msg->result = i2c_msg[1].buf[0];
252 ret = cros_ec_check_result(ec_dev, msg);
253 if (ret)
Simon Glass89969002013-02-25 14:08:38 -0800254 goto done;
Simon Glass89969002013-02-25 14:08:38 -0800255
Doug Andersond6c15ed2014-06-27 12:56:12 -0700256 len = in_buf[1];
257 if (len > msg->insize) {
258 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
259 len, msg->insize);
260 ret = -ENOSPC;
261 goto done;
262 }
263
Simon Glass89969002013-02-25 14:08:38 -0800264 /* copy response packet payload and compute checksum */
265 sum = in_buf[0] + in_buf[1];
Doug Andersond6c15ed2014-06-27 12:56:12 -0700266 for (i = 0; i < len; i++) {
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200267 msg->data[i] = in_buf[2 + i];
Simon Glass89969002013-02-25 14:08:38 -0800268 sum += in_buf[2 + i];
269 }
270 dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n",
271 i2c_msg[1].len, in_buf, sum);
Doug Andersond6c15ed2014-06-27 12:56:12 -0700272 if (sum != in_buf[2 + len]) {
Simon Glass89969002013-02-25 14:08:38 -0800273 dev_err(ec_dev->dev, "bad packet checksum\n");
274 ret = -EBADMSG;
275 goto done;
276 }
277
Doug Andersond6c15ed2014-06-27 12:56:12 -0700278 ret = len;
Stephen Barberd3654072015-06-09 13:04:46 +0200279done:
Simon Glass89969002013-02-25 14:08:38 -0800280 kfree(in_buf);
281 kfree(out_buf);
Stephen Barberd3654072015-06-09 13:04:46 +0200282 if (msg->command == EC_CMD_REBOOT_EC)
283 msleep(EC_REBOOT_DELAY_MS);
284
Simon Glass89969002013-02-25 14:08:38 -0800285 return ret;
286}
287
Thierry Reding192afe52013-11-25 13:22:31 +0100288static int cros_ec_i2c_probe(struct i2c_client *client,
Simon Glass89969002013-02-25 14:08:38 -0800289 const struct i2c_device_id *dev_id)
290{
291 struct device *dev = &client->dev;
292 struct cros_ec_device *ec_dev = NULL;
293 int err;
294
Lee Jones2756db62015-10-28 16:02:48 +0000295 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
Simon Glass89969002013-02-25 14:08:38 -0800296 if (!ec_dev)
297 return -ENOMEM;
298
299 i2c_set_clientdata(client, ec_dev);
Simon Glass89969002013-02-25 14:08:38 -0800300 ec_dev->dev = dev;
301 ec_dev->priv = client;
302 ec_dev->irq = client->irq;
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700303 ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c;
Stephen Barberd3654072015-06-09 13:04:46 +0200304 ec_dev->pkt_xfer = cros_ec_pkt_xfer_i2c;
Simon Glass89969002013-02-25 14:08:38 -0800305 ec_dev->phys_name = client->adapter->name;
Stephen Barberd3654072015-06-09 13:04:46 +0200306 ec_dev->din_size = sizeof(struct ec_host_response_i2c) +
Stephen Barber2c7589a2015-06-09 13:04:45 +0200307 sizeof(struct ec_response_get_protocol_info);
Stephen Barberd3654072015-06-09 13:04:46 +0200308 ec_dev->dout_size = sizeof(struct ec_host_request_i2c);
Simon Glass89969002013-02-25 14:08:38 -0800309
310 err = cros_ec_register(ec_dev);
311 if (err) {
312 dev_err(dev, "cannot register EC\n");
313 return err;
314 }
315
316 return 0;
317}
318
Thierry Reding192afe52013-11-25 13:22:31 +0100319static int cros_ec_i2c_remove(struct i2c_client *client)
Simon Glass89969002013-02-25 14:08:38 -0800320{
321 struct cros_ec_device *ec_dev = i2c_get_clientdata(client);
322
323 cros_ec_remove(ec_dev);
324
325 return 0;
326}
327
328#ifdef CONFIG_PM_SLEEP
329static int cros_ec_i2c_suspend(struct device *dev)
330{
331 struct cros_ec_device *ec_dev = to_ec_dev(dev);
332
333 return cros_ec_suspend(ec_dev);
334}
335
336static int cros_ec_i2c_resume(struct device *dev)
337{
338 struct cros_ec_device *ec_dev = to_ec_dev(dev);
339
340 return cros_ec_resume(ec_dev);
341}
342#endif
343
344static SIMPLE_DEV_PM_OPS(cros_ec_i2c_pm_ops, cros_ec_i2c_suspend,
345 cros_ec_i2c_resume);
346
Javier Martinez Canillas385c0012015-08-25 07:48:10 +0200347static const struct of_device_id cros_ec_i2c_of_match[] = {
348 { .compatible = "google,cros-ec-i2c", },
349 { /* sentinel */ },
350};
351MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
352
Simon Glass89969002013-02-25 14:08:38 -0800353static const struct i2c_device_id cros_ec_i2c_id[] = {
354 { "cros-ec-i2c", 0 },
355 { }
356};
357MODULE_DEVICE_TABLE(i2c, cros_ec_i2c_id);
358
359static struct i2c_driver cros_ec_driver = {
360 .driver = {
361 .name = "cros-ec-i2c",
Javier Martinez Canillas385c0012015-08-25 07:48:10 +0200362 .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
Simon Glass89969002013-02-25 14:08:38 -0800363 .pm = &cros_ec_i2c_pm_ops,
364 },
Thierry Reding192afe52013-11-25 13:22:31 +0100365 .probe = cros_ec_i2c_probe,
366 .remove = cros_ec_i2c_remove,
Simon Glass89969002013-02-25 14:08:38 -0800367 .id_table = cros_ec_i2c_id,
368};
369
370module_i2c_driver(cros_ec_driver);
371
372MODULE_LICENSE("GPL");
373MODULE_DESCRIPTION("ChromeOS EC multi function device");