2 * Copyright (C) 2013 Google, Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * Expose an I2C passthrough to the ChromeOS EC.
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/mfd/cros_ec.h>
15 #include <linux/mfd/cros_ec_commands.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
20 * struct ec_i2c_device - Driver data for I2C tunnel
24 * @ec: Pointer to EC device
25 * @remote_bus: The EC bus number we tunnel to on the other side.
26 * @request_buf: Buffer for transmitting data; we expect most transfers to fit.
27 * @response_buf: Buffer for receiving data; we expect most transfers to fit.
30 struct ec_i2c_device {
32 struct i2c_adapter adap;
33 struct cros_ec_device *ec;
42 * ec_i2c_count_message - Count bytes needed for ec_i2c_construct_message
44 * @i2c_msgs: The i2c messages to read
45 * @num: The number of i2c messages.
47 * Returns the number of bytes the messages will take up.
49 static int ec_i2c_count_message(const struct i2c_msg i2c_msgs[], int num)
54 size = sizeof(struct ec_params_i2c_passthru);
55 size += num * sizeof(struct ec_params_i2c_passthru_msg);
56 for (i = 0; i < num; i++)
57 if (!(i2c_msgs[i].flags & I2C_M_RD))
58 size += i2c_msgs[i].len;
64 * ec_i2c_construct_message - construct a message to go to the EC
66 * This function effectively stuffs the standard i2c_msg format of Linux into
67 * a format that the EC understands.
69 * @buf: The buffer to fill. We assume that the buffer is big enough.
70 * @i2c_msgs: The i2c messages to read.
71 * @num: The number of i2c messages.
72 * @bus_num: The remote bus number we want to talk to.
74 * Returns 0 or a negative error number.
76 static int ec_i2c_construct_message(u8 *buf, const struct i2c_msg i2c_msgs[],
79 struct ec_params_i2c_passthru *params;
83 out_data = buf + sizeof(struct ec_params_i2c_passthru) +
84 num * sizeof(struct ec_params_i2c_passthru_msg);
86 params = (struct ec_params_i2c_passthru *)buf;
87 params->port = bus_num;
88 params->num_msgs = num;
89 for (i = 0; i < num; i++) {
90 const struct i2c_msg *i2c_msg = &i2c_msgs[i];
91 struct ec_params_i2c_passthru_msg *msg = ¶ms->msg[i];
93 msg->len = i2c_msg->len;
94 msg->addr_flags = i2c_msg->addr;
96 if (i2c_msg->flags & I2C_M_TEN)
97 msg->addr_flags |= EC_I2C_FLAG_10BIT;
99 if (i2c_msg->flags & I2C_M_RD) {
100 msg->addr_flags |= EC_I2C_FLAG_READ;
102 memcpy(out_data, i2c_msg->buf, msg->len);
103 out_data += msg->len;
111 * ec_i2c_count_response - Count bytes needed for ec_i2c_parse_response
113 * @i2c_msgs: The i2c messages to to fill up.
114 * @num: The number of i2c messages expected.
116 * Returns the number of response bytes expeced.
118 static int ec_i2c_count_response(struct i2c_msg i2c_msgs[], int num)
123 size = sizeof(struct ec_response_i2c_passthru);
124 for (i = 0; i < num; i++)
125 if (i2c_msgs[i].flags & I2C_M_RD)
126 size += i2c_msgs[i].len;
132 * ec_i2c_parse_response - Parse a response from the EC
134 * We'll take the EC's response and copy it back into msgs.
136 * @buf: The buffer to parse.
137 * @i2c_msgs: The i2c messages to to fill up.
138 * @num: The number of i2c messages; will be modified to include the actual
141 * Returns 0 or a negative error number.
143 static int ec_i2c_parse_response(const u8 *buf, struct i2c_msg i2c_msgs[],
146 const struct ec_response_i2c_passthru *resp;
150 in_data = buf + sizeof(struct ec_response_i2c_passthru);
152 resp = (const struct ec_response_i2c_passthru *)buf;
153 if (resp->i2c_status & EC_I2C_STATUS_TIMEOUT)
155 else if (resp->i2c_status & EC_I2C_STATUS_ERROR)
158 /* Other side could send us back fewer messages, but not more */
159 if (resp->num_msgs > *num)
161 *num = resp->num_msgs;
163 for (i = 0; i < *num; i++) {
164 struct i2c_msg *i2c_msg = &i2c_msgs[i];
166 if (i2c_msgs[i].flags & I2C_M_RD) {
167 memcpy(i2c_msg->buf, in_data, i2c_msg->len);
168 in_data += i2c_msg->len;
175 static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
178 struct ec_i2c_device *bus = adap->algo_data;
179 struct device *dev = bus->dev;
180 const u16 bus_num = bus->remote_bus;
186 struct cros_ec_command msg;
188 request_len = ec_i2c_count_message(i2c_msgs, num);
189 if (request_len < 0) {
190 dev_warn(dev, "Error constructing message %d\n", request_len);
191 result = request_len;
194 response_len = ec_i2c_count_response(i2c_msgs, num);
195 if (response_len < 0) {
196 /* Unexpected; no errors should come when NULL response */
197 dev_warn(dev, "Error preparing response %d\n", response_len);
198 result = response_len;
202 if (request_len <= ARRAY_SIZE(bus->request_buf)) {
203 request = bus->request_buf;
205 request = kzalloc(request_len, GFP_KERNEL);
206 if (request == NULL) {
211 if (response_len <= ARRAY_SIZE(bus->response_buf)) {
212 response = bus->response_buf;
214 response = kzalloc(response_len, GFP_KERNEL);
215 if (response == NULL) {
221 ec_i2c_construct_message(request, i2c_msgs, num, bus_num);
224 msg.command = EC_CMD_I2C_PASSTHRU;
225 msg.outdata = request;
226 msg.outsize = request_len;
227 msg.indata = response;
228 msg.insize = response_len;
230 result = bus->ec->cmd_xfer(bus->ec, &msg);
234 result = ec_i2c_parse_response(response, i2c_msgs, &num);
238 /* Indicate success by saying how many messages were sent */
241 if (request != bus->request_buf)
243 if (response != bus->response_buf)
249 static u32 ec_i2c_functionality(struct i2c_adapter *adap)
251 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
254 static const struct i2c_algorithm ec_i2c_algorithm = {
255 .master_xfer = ec_i2c_xfer,
256 .functionality = ec_i2c_functionality,
259 static int ec_i2c_probe(struct platform_device *pdev)
261 struct device_node *np = pdev->dev.of_node;
262 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
263 struct device *dev = &pdev->dev;
264 struct ec_i2c_device *bus = NULL;
269 dev_err(dev, "Missing sendrecv\n");
273 bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
277 err = of_property_read_u32(np, "google,remote-bus", &remote_bus);
279 dev_err(dev, "Couldn't read remote-bus property\n");
282 bus->remote_bus = remote_bus;
287 bus->adap.owner = THIS_MODULE;
288 strlcpy(bus->adap.name, "cros-ec-i2c-tunnel", sizeof(bus->adap.name));
289 bus->adap.algo = &ec_i2c_algorithm;
290 bus->adap.algo_data = bus;
291 bus->adap.dev.parent = &pdev->dev;
292 bus->adap.dev.of_node = np;
294 err = i2c_add_adapter(&bus->adap);
296 dev_err(dev, "cannot register i2c adapter\n");
299 platform_set_drvdata(pdev, bus);
304 static int ec_i2c_remove(struct platform_device *dev)
306 struct ec_i2c_device *bus = platform_get_drvdata(dev);
308 i2c_del_adapter(&bus->adap);
313 static struct platform_driver ec_i2c_tunnel_driver = {
314 .probe = ec_i2c_probe,
315 .remove = ec_i2c_remove,
317 .name = "cros-ec-i2c-tunnel",
321 module_platform_driver(ec_i2c_tunnel_driver);
323 MODULE_LICENSE("GPL");
324 MODULE_DESCRIPTION("EC I2C tunnel driver");
325 MODULE_ALIAS("platform:cros-ec-i2c-tunnel");